title: Constant Buffer DirectX 11
body: <p>getting really frustrated with my CBuffer in HLSL D3D11 not updating, the initial values get set upon application launch but updating is a no go, used UpdateSubResource, also tried ID3D11DeviceContext::Map &amp; ID3D11DeviceContext::UnMap.</p>
  
  <p>NOTE: CBuffer setup with D3D11_USAGE_DYNAMIC &amp; D3D11_CPU_ACCESS_WRITE.</p>
  
  <p>My ID3D11Buffer (constant buffer) only returning 4 bytes when it's size is queried.... sounds like part of the problem</p>
  
  <pre><code>struct VS_CBUFFER_DATA
  {
      XMFLOAT4X4 world;
      XMFLOAT4X4 view;
      XMFLOAT4X4 projection;
  
      VS_CBUFFER_DATA()
      {
          XMStoreFloat4x4(&amp;world, DirectX::XMMatrixIdentity());
          XMStoreFloat4x4(&amp;view, DirectX::XMMatrixIdentity());
          XMStoreFloat4x4(&amp;projection, DirectX::XMMatrixIdentity());
      }
  };
  
  D3D11_BUFFER_DESC cbufferDesc;
  memset(&amp;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(&amp;subdata, 0, sizeof(subdata));
  
  subdata.pSysMem = &amp;m_CBufferData;
  
  if (FAILED(pRendererTemp-&gt;CreateBuffer(cbufferDesc
  {
      OutputDebugString("Failed to create CBuffer!")
      goto failed;
  }
  
  void Cube::UpdateViewProjection(__in const Renderer* pRenderer, __in const    XMFLOAT4X4   &amp;view, __in const XMFLOAT4X4 &amp;proj)
  {
      D3D11_MAPPED_SUBRESOURCE mappedSubResource;
      memset(&amp;mappedSubResource, 0, sizeof(mappedSubResource));
  
      if (SUCCEEDED(pRenderer-&gt;GetDevContext()-&gt;Map(m_pCBuffer, 0, D3D11_MAP_WRITE_NO_OVERWRITE,  0, &amp;mappedSubResource))) 
      {
          Primitive::UpdateViewProjection(NULL, view, proj);
          VS_CBUFFER_DATA* cbData = (VS_CBUFFER_DATA*)&amp;mappedSubResource.pData;
  
          memcpy(cbData, &amp;m_CBufferData, sizeof(cbData));
          pRenderer-&gt;GetDevContext()-&gt;Unmap(m_pCBuffer, 0);
          SetBuffers(pRenderer);
      }
  }
  
  // SHADER.vsh
  cbuffer cbMatrixBuffer : register(b0)
  {
      float4x4 world;
      float4x4 view;
      float4x4 projection;
  };
  </code></pre>