Sign in to follow this  

DirectX10 + HLSL Constantbuffer problem

Recommended Posts


I don't know why but dx10 always give me back a false result when I try to create buffer, in this case a constantbuffer.

[source lang="cpp"]struct SPSSSAOPerFrameBuffer
float m_sample_rad;
float m_scale;
float m_intensity;
float m_bias;
float m_screen_size_x;
float m_screen_size_y;
float m_random_size;

ID3D10Buffer* g_pPSSSAOPerFrameBuffer = 0;

D3D10_BUFFER_DESC PSSSAOPerFrameBufferDesciptor;

PSPerFrameBufferDesciptor.ByteWidth = sizeof(SPSSSAOPerFrameBuffer);
PSPerFrameBufferDesciptor.Usage = D3D10_USAGE_DYNAMIC;
PSPerFrameBufferDesciptor.BindFlags = D3D10_BIND_CONSTANT_BUFFER;
PSPerFrameBufferDesciptor.CPUAccessFlags = D3D10_CPU_ACCESS_WRITE; PSPerFrameBufferDesciptor.MiscFlags = 0;

Result = g_pDevice->CreateBuffer(&PSSSAOPerFrameBufferDesciptor, 0, &g_pPSSSAOPerFrameBuffer);

if (Result != S_OK)
return false;

I'm getting back, in Result, the value E_INVALIDARG.
And thats the constantbuffer in hlsl.
[source lang="cpp"]cbuffer CBPSSSAOPerFrameBuffer
float1 m_sample_rad;
float1 m_scale;
float1 m_intensity;
float1 m_bias;
float1 m_screen_size_x;
float1 m_screen_size_y;
float1 m_random_size;

Before you think I'm just too lazy to find a solution, I've already read the part for constantbuffers in the msdn ( :).
And they are doing exactly the same. I really don't know what I'm doing wrong :(

I hope somebody can help me, thx so far.

Share this post

Link to post
Share on other sites
Your problem is that constant buffer sizes need to be multiples of 16 bytes, which in your case means that you need to round up the size from 28 to 32 bytes. You can round it up by doing (size + 15) / 16.

Either way if you enable the debug layer like Radikalizm suggests (which you do by passing D3D10_CREATE_DEVICE_DEBUG when creating your device), the runtime will tell you what the problem is.

Share this post

Link to post
Share on other sites
Radikalizm, could you maybe tell me how?^^

ahh I knew that was the problem... but I wasnt sure, I'll try that, thx guys :)

Share this post

Link to post
Share on other sites
The functions[url=""] D3D10CreateDevice[/url] and [url=""]D3D10CreateDeviceAndSwapChain[/url] take a parameter called flags which takes a [url=""]D3D10_CREATE_DEVICE_FLAG[/url] enum value which can enable certain API layers for your device.
Passing in the value D3D10_CREATE_DEVICE_DEBUG (as MJP said) will enable the debug layer for your device which will give you loads of information in your debug output when you encounter an issue.

Share this post

Link to post
Share on other sites
I'd suggest adding an 8th float to your HLSL cbuffer declaration too, just as padding to make it match the size of your C++ struct, otherwise your GPU and/or driver may get unhappy with you. Also consider adding packoffset and register declarations (see last example at [url=""][/url]). All of these will make things much more robust for you.

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