Say these two days I wrote the program encountered a bug, is about how to create a id3d11buffer should be in a way to render cache resources in each frame of the update
I summarize for the following:
first, when buffer is a CPU access write and is dynamic
The Fill (vertex) cache describes the structure and sub-resource data structures and creates a vertex cache (which is used for dynamic caching)
D3d11_buffer_desc Vertexbufferdesc;
Vertexbufferdesc.usage = d3d11_usage_dynamic;
Vertexbufferdesc.bytewidth = sizeof (VERTEX) * MVERTEXCOUNT;
Vertexbufferdesc.bindflags = D3d11_bind_vertex_buffer;
Vertexbufferdesc.cpuaccessflags = D3d11_cpu_access_write;
vertexbufferdesc.miscflags = 0;
vertexbufferdesc.structurebytestride = 0;
At this time, each frame of the cache resource update should be updated with map and umap resources
vertexbufferdesc.usage = d3d11_usage_dynamic;
Vertexbufferdesc.cpuaccessflags = D3d11_cpu_access_write;
Lock the vertex cache in order to be able to write (dynamic cache cannot be written with updatesubresources)
D3d11_mapped_subresource Mappedresource;
HR (D3ddevicecontext->map (md3dvertexbuffer, 0, D3d11_map_write_discard, 0, &mappedresource));
Gets the pointer to the vertex cache
vertex* verticesptr;
Verticesptr = (vertex*) mappedresource.pdata;
Copy the data into the vertex cache
memcpy (verticesptr, (void*) Vertexs, (sizeof (VERTEX) * mvertexcount));
Unlock vertex cache
D3ddevicecontext->unmap (md3dvertexbuffer, 0);
second, when buffer is not CPU-accessible write and is default, use Updatesubresouce
, set (constant) Cache descriptor structure, and create constant cache
D3d11_buffer_desc Matrixbufferdesc;
ZeroMemory (&matrixbufferdesc, sizeof (MATRIXBUFFERDESC));
Matrixbufferdesc.usage =d3d11_usage_default; Cache
matrixbufferdesc.bytewidth = sizeof (Cbmatrix); struct size, must be 16 byte multiples
matrixbufferdesc.bindflags = d3d11_bind_constant_buffer;
matrixbufferdesc.cpuaccessflags = 0; CPU Access Write
matrixbufferdesc.miscflags = 0;
matrixbufferdesc.structurebytestride = 0;
HR (D3ddevice->createbuffer (&matrixbufferdesc, NULL, &mcbmatrixbuffer));
Here Matrixbufferdesc.usage =d3d11_usage_default; Cache
matrixbufferdesc.cpuaccessflags = 0; CPU Access Write
Cbmatrix CB;
Xmmatrix WORLDMA = Xmmatrixtranspose (Worldmatrix);
Xmmatrix viewma = Xmmatrixtranspose (Viewmatrix);
Xmmatrix Projma = Xmmatrixtranspose (Projmatrix);
Cb.mworldmatrix = WORLDMA;
Cb.mviewmatrix = viewma;
Cb.mprojmatrix = Projma;
D3ddevicecontext->updatesubresource (mcbmatrixbuffer, 0, NULL, &CB, 0, 0);
The flags that create the buffer setting above are not the same, and the way to update resources is different, and if you do not update the resources as specified, the result is that resources cannot be written to the cache.
More specific and so I have to improve the level of research in addition to follow-up content, update each frame dynamic changes as far as possible or with map and Umap bar