2 minutes ago, Magogan said:
Yes and no. __declspec(align(16)) does only work for some stack allocations (normal definitions, but apparently not on function args), not on the heap - it adds padding at the end to make the size a multiple of 16 though. But the structure itself doesn't even need to be aligned on the CPU side unless you use XMVECTOR or XMMATRIX (which you shouldn't do, just use XMFLOAT(n) or XMFLOAT(n)X(m)). You need to align the vectors so they do not lie in multiple 16 byte chunks, e.g. float3 float3 float float wouldn't work because the first element of the second float3 is in the first 16 bytes and the other 2 elements are in the second 16 byte chunk. Just add padding such that this doesn't happen (in this case float3 float float3 float) and you'll be fine.
You should group together the data in the constant buffers based on when you update them (once per frame, multiple times per frame, etc.) instead of using a lot of constant buffers.
This is just really new stuff to me so I'm trying to get a hang of it. I currently use XMVECTOR and XMMATRIX liberally on the cpu structures anytime I need computations, you're saying I shouldn't do that for speed reasons? So what can I use then because XMFLOAT doesn't allow any kind of math operations (can't add them multiply etc).
Also you're saying that I can have unaligned structures on the CPU but then have them aligned on the GPU, I just want to make sure I understand that correctly. So on the cpu it's sufficient to just use declspec, but I dont' necessarily have to add the elements to the structure?
This is what I mean:
//CPU
__declspec(align(16)) struct DirectionalLight
{
DirectX::XMFLOAT3 LightDirection;
DirectX::XMFLOAT4 LightColor;
};
//SHADER
cbuffer dlight
{
float3 direction;
float _padding;
float4 color;
};
So that should be fine and it's not going to mess with the buffer copying or anything like that (since the cpu structure has one less element)?
Quote
You should group together the data in the constant buffers based on when you update them (once per frame, multiple times per frame, etc.) instead of using a lot of constant buffers.
But what if I want to update the buffer in various places in my code? For example lets say I have this buffer:
__declspec(align(16)) struct cbuff_perframe
{
DirectX::XMMATRIX View;
DirectX::XMMATRIX Projection;
DirectX::XMMATRIX World;
DirectX::XMFLOAT3 CameraEyePosition;
};
And I want to update the View and Projection inside main Update method of the app, but I'd like to update the World matrix and the CamEyePosition inside the update method of other objects, how would I do that?
So in other words how can I just update lets say the CameraEyePosition by itself in one place and World in other place if they're in the same buffer?