Sign in to follow this  
jozsef.horvath

Update ConstantBuffers using Context::MAP

Recommended Posts

jozsef.horvath    110
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!

Share this post


Link to post
Share on other sites
mhagain    13430
Here's one of your problems:[code] HRESULT Result(E_FAIL);
m_Context->Map(*Buffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &BufferContents);
if (FAILED(Result))
return Result;[/code]

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

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

You need to use memcpy instead, like so:[code]memcpy (BufferContents.pData, &m_cPerObject, sizeof (m_cPerObject));[/code] Edited by mhagain

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this