constant buffers direct x 11

Fd80f81596aa1cf809ceb1c2077e190b
0
rouncer 104 May 11, 2012 at 07:45

for some reason

struct CB0
{
VEC brush_pos;
float brush_radius;
MAT wvp;
MAT view;
float zoff;
VEC col;
VEC chunk_pos;
float tex_size;
};

(system struct)

doesnt match up with

cbuffer cb0 : register( b1 )
{
float3 brush_pos;
float brush_radius;
matrix wvp;
matrix view;
float zoff;
float3 col;
float3 chunk_pos;
float tex_size;
};

in the shader… like it unaligns or something, what could cause this? (even though this example works, but sometimes when i add a variable all hell breaks loose)

3 Replies

Please log in or register to post a reply.

A8433b04cb41dd57113740b779f61acb
0
Reedbeta 167 May 11, 2012 at 16:25

Yes, HLSL has specific rules for alignment of vector quantities in constant buffers, which can result in differing alignment to what you’d get in C/C++. This page explains the rules. Basically, you cannot split a vector (like a float3) across a 16-byte (4-float) boundary, so padding will be added to prevent this. You often have to add this padding to the C/C++ version of the struct yourself.

How are your VEC, MAT and “matrix” types defined?

Fd80f81596aa1cf809ceb1c2077e190b
0
rouncer 104 May 11, 2012 at 17:28

ahhh thanks man! i was always troubled by this for a long time… i thought something screwy was going on.

VEC is the 3 component vector, and MAT is a 4x4… so i guess the 3 component vector was the strange one.

Fd80f81596aa1cf809ceb1c2077e190b
0
rouncer 104 May 11, 2012 at 17:36

i get it, so you cant put two 3 component vectors in a row (sometimes), cause it crosses a 16 byte boundary… ? you can if it doesnt. if you look at the structure provided, youll see i havent crossed a boundary, just with naive trial and error, i see now.

thanks reed - you the man.