Jump to content

  • Log In with Google      Sign In   
  • Create Account


Update ConstantBuffers using Context::MAP


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
2 replies to this topic

#1 Wrath87   Members   -  Reputation: 110

Like
0Likes
Like

Posted 30 June 2012 - 09:22 AM

Greetings,

I've some problems updating my constant buffers. If I don't use cb in my Vertex Shader, then my Triangle is drawn else nothing is drawn on the screen. No errors but blank d3d window.

HLSL:
[source lang="cpp"]cbuffer PerFrame{ float4x4 WorldMatrix; float4x4 ViewMatrix; float4x4 ProjectionMatrix;};struct VS_OUT{ float4 Position : SV_POSITION; float4 Color : COLOR;};VS_OUT MainVS(float3 Position : POSITION, float4 Color : COLOR){ VS_OUT output = (VS_OUT)0; output.Position = mul(mul(mul(float4(Position, 1.0f), WorldMatrix), ViewMatrix), ProjectionMatrix); output.Color = Color; return output;}[/source]

As you can see its as simple as it can be in this case.

Cpp side cBuffer struct:
[source lang="cpp"] struct VS_CONSTANTS { private: XMFLOAT4X4 World; XMFLOAT4X4 View; XMFLOAT4X4 Projection; public: VS_CONSTANTS(void) { } VS_CONSTANTS(XMFLOAT4X4 _World, XMFLOAT4X4 _View, XMFLOAT4X4 _Projection) { World = _World; View = _View; Projection = _Projection; } }; VS_CONSTANTS m_cPerObject;[/source]

CONSTANT Buffer Creation:

[source lang="cpp"]D3D11_BUFFER_DESC Cb_DESCRIPTOR;ZeroMemory(&Cb_DESCRIPTOR, sizeof(D3D11_BUFFER_DESC));Cb_DESCRIPTOR.ByteWidth = sizeof(VS_CONSTANTS);Cb_DESCRIPTOR.BindFlags = D3D11_BIND_CONSTANT_BUFFER;Cb_DESCRIPTOR.Usage = D3D11_USAGE_DYNAMIC;Cb_DESCRIPTOR.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;HRESULT Result(E_FAIL);Result = m_Device->CreateBuffer(Buffer_DESCRIPTOR, InitialData, m_ConstantBuffer));if (FAILED(Result)) return Result;[/source]

And finally here's how I try to update the contents of the buffer:
[source lang="cpp"]template <class Type>HRESULT Demo::SetShaderResource(ID3D11Buffer** Buffer, const Type* Data){ D3D11_MAPPED_SUBRESOURCE BufferContents; assert(m_Context); HRESULT Result(E_FAIL); m_Context->Map(*Buffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &BufferContents); if (FAILED(Result)) return Result; BufferContents.pData = &m_cPerObject; m_Context->Unmap(*Buffer, 0); return S_OK;}[/source]

Data contains the following values:
[source lang="cpp"] m_SimpleCamera = new CameraManager; XMStoreFloat4x4(&m_WorldMatrix, XMMatrixIdentity()); XMStoreFloat4x4(&m_ViewMatrix, m_SimpleCamera->GetViewMatrix()); XMStoreFloat4x4(&m_ProjectionMatrix, XMMatrixIdentity()); m_cPerObject = VS_CONSTANTS(m_WorldMatrix, m_ViewMatrix, m_ProjectionMatrix);[/source]

Phew, that's quite a lot of code right here. I hope I gave every information needed. The method that should be used it this but I can't seem to find the answer what I did wrong, but I searched for nearly 3 hours now...

Any help would be appreciated!

Sponsor:

#2 mhagain   Crossbones+   -  Reputation: 6303

Like
2Likes
Like

Posted 30 June 2012 - 09:46 AM

Here's one of your problems:
HRESULT Result(E_FAIL);
m_Context->Map(*Buffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &BufferContents);
if (FAILED(Result))
  return Result;

The second line should be:
Result = m_Context->Map(*Buffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &BufferContents);

For more fun, this doesn't actually copy data, it just changes a pointer:
BufferContents.pData = &m_cPerObject;

You need to use memcpy instead, like so:
memcpy (BufferContents.pData, &m_cPerObject, sizeof (m_cPerObject));

Edited by mhagain, 30 June 2012 - 09:46 AM.

It appears that the gentleman thought C++ was extremely difficult and he was overjoyed that the machine was absorbing it; he understood that good C++ is difficult but the best C++ is well-nigh unintelligible.


#3 Wrath87   Members   -  Reputation: 110

Like
0Likes
Like

Posted 30 June 2012 - 10:07 AM

Awesome! I knew it'll be something like this... Thanks a lot mate, you saved my day! ;)




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS