C++ Directx 11 CBuffers and HLSL.

Started by
15 comments, last by MJP 11 years, 9 months ago
In the C++ code, do
__declspec( align( 16 ) ) struct ConstantBuffer
{
/*...*/
};
Advertisement

In the C++ code, do
__declspec( align( 16 ) ) struct ConstantBuffer
{
/*...*/
};


Ok, i never saw that function, but ill research it... thanks!

FastCall22: "I want to make the distinction that my laptop is a whore-box that connects to different network"

Blog about... stuff (GDNet, WordPress): www.gamedev.net/blog/1882-the-cuboid-zone/, cuboidzone.wordpress.com/

__declspec(align(16)) seems like a simpler option, but will that translate over to video memory? You're aligning it in system memory just fine, but I'm not sure if it is treated the same way when you map it.

Please confirm.
It's not going to matter because when you map it you're getting a pointer to a different chunk of memory that has already been properly aligned/sized/etc when the cbuffer was created.

Direct3D has need of instancing, but we do not. We have plenty of glVertexAttrib calls.

I figured. Thanks for the confirmation. I was nodding my head throughout this thread until I came across that claim and was pretty sure that's not how it worked.
in my opinion
first. make your data full with 4 float every unit both in hlsl and c++, forget your c++ ruler and habit, what you contact with is gpu not cpu, and also hlsl is just like c but not c, naturally there are big differents at asm level. if ok, forget order of readableness just align it.
second. Updatesubresource sounds great, i agree, but map and unmap is handy, obviously. next time, try map, you will like it
third. i do not know why i still find d3dx*** at directx11, is it pure one, if it is, en, d3dx*** is history.
ok, i wish it can help you, if someone has different opinion please let me know
Aligning a struct to 16-bytes will affect the alignment and size of that structure, but not the members within it. The reason it's useful this case is because it will round up your structure size to the next multiple of 16-bytes (which is a requirement for constant buffers), but you could also do that just by rounding it up yourself. What you really need to be careful about with constant buffers is the alignment of the individual variables within the constant buffer. HLSL has its own particular rules for this that differ from the defaults for 32-bit and 64-bit C++, which means if you want to use a C++ struct to fill a constant buffer then you need to pay attention to member alignment. One way to do this will be to use __declspec(align(16)) to align individual members to the next 16-byte boundary. Here's a simple example:


struct ConstantBuffer
{
float3 position;
__declspec(align(16)) float3 color;
};


Without the alignment on the "color" member, it will be placed 12-bytes from the start of the constant buffer while HLSL requires it to be placed at the next 16-byte boundary.

This topic is closed to new replies.

Advertisement