First, what is a buffer:
The buffer is the effect of the. fx file (. ps. VS also) a data structure that defines it. Data communication files for. FX and CPP.
Cases:
--------------------------------------------------------------------------------------//Constant Buffer variables//--------------------------------------------------------------------------------------Cbuffer Constantbuffer {matrix World;matrix view;matrix Projection;}; Cbuffer lightbuffer{ float3 cameraposition; float padding;};
Cbuffer is keyword. Similar to struct
Here, 2 buffers are defined.
At the time of execution of the program.
Effect file. FX needs to get the world matrix Observation matrix projection matrix and so on. Therefore, you need to assign a value to this buffer. On the one hand can be directly written in the. fx file in the polygon.
But
Write directly after the effect file compilation can not be changed dynamically, there is no flexibility.
Here are 2 types of buffers.
1: Static constant buffer
The so-called static is when the renderer initializes, the buffer is assigned a good value. It won't change any more.
Create the constant BUFFERD3D11_BUFFER_DESC BD; ZeroMemory (&BD, sizeof (BD)); Bd. Usage = d3d11_usage_default;bd. Bytewidth = sizeof (Constantbuffer); Bd. Bindflags = d3d11_bind_constant_buffer;bd. Cpuaccessflags = 0;HR = Device->createbuffer (&BD, NULL, &m_matrixbuffer); if (FAILED (HR)) return HR;
Above is a list of the creation of a static buffer
Middle Bd. Usage Property
D3d11_usage_default
This means that the CPU is not able to read and write data during execution.
Bd. Couaccessflags=0 must also be set to 0.
Once the buffer structure is defined, it is initialized.
Update Variables//constantbuffer Cb;cb.mworld = Xmmatrixtranspose (Worldmatrix); Cb.mview = Xmmatrixtranspose ( Viewmatrix) cb.mprojection = Xmmatrixtranspose (ProjectionMatrix);d Evicecontext->updatesubresource (m_ Matrixbuffer, 0, NULL, &CB, 0, 0);d evicecontext->vssetconstantbuffers (0, 1, &m_matrixbuffer);
Initialize very easy here take the world matrix View matrix projection matrix as a sample
2: Dynamic vertex buffer
is different from the static buffer.
Dynamic buffers are used for each frame that needs to dynamically change the rendering data.
For example, the water wave, every second, his vertex structure is changing, so it is necessary to use dynamic buffer dynamic assignment, rather than in the initialization of the assignment
Create the Light bufferzeromemory (&BD, sizeof (BD)); Bd. Usage = d3d11_usage_dynamic;bd. Bytewidth = sizeof (Lightbuffer); Bd. Bindflags = d3d11_bind_constant_buffer;bd. Cpuaccessflags = D3D11_CPU_ACCESS_WRITE;HR = Device->createbuffer (&BD, NULL, &m_lightmaterialbuffer); FAILED (HR)) return Hr;return S_OK;
The definition is basically the same. , only the USAGE is set to D3d11_usage_dynamic and Cpuaccessflags is also set to D3d11_cpu_access_write
And it's not the same when you update the data.
You must first lock with map to prevent the device from rendering when it is not updated. Inconsistent data caused by errors
D3d11_mapped_subresource Mappedresource; Constantbuffer *data0; Lightbuffer *data1;////Update Variables////constantbuffer cb;//cb.mworld = Xmmatrixtranspose (WorldMatrix);// Cb.mview = Xmmatrixtranspose (Viewmatrix);//cb.mprojection = Xmmatrixtranspose (ProjectionMatrix);//Lock constant buffer hr ( Devicecontext->map (M_matrixbuffer,0,d3d11_map_write_discard,0,&mappedresource));//deviceContext-> Updatesubresource (m_matrixbuffer, 0, NULL, &CB, 0, 0);d ata0= (constantbuffer*) mappedresource.pdata;data0-> Mworld=xmmatrixtranspose (Worldmatrix);d ata0->mview=xmmatrixtranspose (Viewmatrix);d ata0->mprojection= Xmmatrixtranspose (ProjectionMatrix);d evicecontext->unmap (m_matrixbuffer,0);d evicecontext-> Vssetconstantbuffers (0, 1, &m_matrixbuffer);//lightbuffer lb;//lb.cameraposition=camerapos;//Lock light buffer hr ( Devicecontext->map (M_lightmaterialbuffer,0,d3d11_map_write_discard,0,&mappedresource));d ata1= ( lightbuffer*) Mappedresource.pdata;data1->cameraposition=camerapos;deviceconText->unmap (m_lightmaterialbuffer,0);//devicecontext->updatesubresource (M_lightmaterialbuffer, 0, NULL, &LB, 0, 0);d evicecontext->vssetconstantbuffers (1, 1, &m_lightmaterialbuffer);
Here are 2 examples that render 2 buffers at the same time.
Note that when rendering two buffers, it is not easy to write the first and then write the second one.
A little bit different.
is in Devicecontext->vssetconstantbuffers () when to join the first parameter 1. It represents a second buffer. Everything else is the same.
Copyright notice: This article blog original articles, blogs, without consent, may not be reproduced.
DIRECTX11 Learning Note 9-dynamic vertex buffers and static vertex buffers