Jump to content
  • Advertisement
Sign in to follow this  
jozsef.horvath

Update ConstantBuffers using Context::MAP

This topic is 2154 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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
Advertisement
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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!