c++ - Constant Buffer DirectX 11 -
getting frustrated cbuffer in hlsl d3d11 not updating, initial values set upon application launch updating no go, used updatesubresource, tried id3d11devicecontext::map & id3d11devicecontext::unmap.
note: cbuffer setup d3d11_usage_dynamic & d3d11_cpu_access_write.
my id3d11buffer (constant buffer) returning 4 bytes when it's size queried.... sounds part of problem
struct vs_cbuffer_data { xmfloat4x4 world; xmfloat4x4 view; xmfloat4x4 projection; vs_cbuffer_data() { xmstorefloat4x4(&world, directx::xmmatrixidentity()); xmstorefloat4x4(&view, directx::xmmatrixidentity()); xmstorefloat4x4(&projection, directx::xmmatrixidentity()); } }; d3d11_buffer_desc cbufferdesc; memset(&cbufferdesc, 0, sizeof(cbufferdesc)); cbufferdesc.bindflags = d3d11_bind_constant_buffer cbufferdesc.usage = d3d11_usage_dynamic; cbufferdesc.bytewidth = sizeof(m_cbufferdata); cbufferdesc.cpuaccessflags = d3d11_cpu_access_write; cbufferdesc.miscflags = 0; cbufferdesc.structurebytestride = 0; d3d11_subresource_data subdata; memset(&subdata, 0, sizeof(subdata)); subdata.psysmem = &m_cbufferdata; if (failed(prenderertemp->createbuffer(cbufferdesc { outputdebugstring("failed create cbuffer!") goto failed; } void cube::updateviewprojection(__in const renderer* prenderer, __in const xmfloat4x4 &view, __in const xmfloat4x4 &proj) { d3d11_mapped_subresource mappedsubresource; memset(&mappedsubresource, 0, sizeof(mappedsubresource)); if (succeeded(prenderer->getdevcontext()->map(m_pcbuffer, 0, d3d11_map_write_no_overwrite, 0, &mappedsubresource))) { primitive::updateviewprojection(null, view, proj); vs_cbuffer_data* cbdata = (vs_cbuffer_data*)&mappedsubresource.pdata; memcpy(cbdata, &m_cbufferdata, sizeof(cbdata)); prenderer->getdevcontext()->unmap(m_pcbuffer, 0); setbuffers(prenderer); } } // shader.vsh cbuffer cbmatrixbuffer : register(b0) { float4x4 world; float4x4 view; float4x4 projection; };
your problem in memcpy
. line here:
memcpy(cbdata, &m_cbufferdata, sizeof(cbdata));
is copying number of bytes equal size of vs_cbuffer_data
pointer, on 32 bit systems 4 bytes (8 bytes on x64). code should read:
memcpy(cbdata, &m_cbufferdata, sizeof(vs_cbuffer_data));
which copy 48 bytes of data opposed 4/8 (whatever sizeof(void*)
evaluates to).
in general, avoid using sizeof
query size of array, , avoid in regards pointers (unless have need that).
Comments
Post a Comment