aligned_malloc() vs alignas() for Constant Buffers

懵懂的女人 提交于 2021-02-05 08:59:06

问题


In C++, we have the keyword alignas(n) and we have the _aligned_malloc(m,n) function.
alignas works on the type while aligned_malloc works on whatever you call it.
Can I use alignas(16) to fullfil the 16-byte alignment requirement for Direct3D Constant Buffers?


回答1:


Yes, you could use it like this:

struct SceneConstantBuffer
{
    alignas(16) DirectX::XMFLOAT4X4 ViewProjection[2];
    alignas(16) DirectX::XMFLOAT4 EyePosition[2];
    alignas(16) DirectX::XMFLOAT3 LightDirection{};
    alignas(16) DirectX::XMFLOAT3 LightDiffuseColor{};
    alignas(16) int NumSpecularMipLevels{ 1 };
};

What won't work is __declspec(align)...

EDIT: If you want to use it on the struct itself something similar to this should work too:

struct alignas(16) SceneConstantBuffer
{
    DirectX::XMMATRIX ViewProjection; // 16-bytes
    ...
    DirectX::XMFLOAT3 LightDiffuseColor{};
}


来源:https://stackoverflow.com/questions/59798721/aligned-malloc-vs-alignas-for-constant-buffers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!