Jump to content

  • Log In with Google      Sign In   
  • Create Account


#ActualMJP

Posted 09 December 2012 - 02:36 AM

This isn't your problem, but you don't need to set the "ElementOffset" or "ElementWidth" members of D3D11_BUFFER_SRV. The structure is defined like this:

typedef struct D3D11_BUFFER_SRV
	{
	union
		{
		UINT FirstElement;
		UINT ElementOffset;
		}  ;
	union
		{
		UINT NumElements;
		UINT ElementWidth;
		}  ;
	}  D3D11_BUFFER_SRV;

Since they're in unions, you can only set one or the other in each union pair. For a structured buffer you want to use "FirstElement" and "NumElements".

Anyway, I suspect your problem is with your C++ struct. If you check sizeof(SInstance), you'll find that it's 80 bytes in size. This is because the alignment requirements of XMMATRIX (XMMATRIX is already 16-byte aligned, you don't need to add the alignment manually) will cause the struct to have 16-byte alignment, which will cause the compiler to insert 12 bytes of padding after dTemplateType. However if you were to declare something like this in HLSL..

struct SInstance
{
	float4x4 matLocation;
	int dTemplateType;
};

...this struct will be 68 bytes in size. Structs for structured buffers don't have 16-byte alignment requirements, that was an incorrect assumption on your part. HLSL really only works in terms of 4-byte values, so structs used for structured buffers will pretty much always have 4-byte alignment. If you stick to using 4-byte types in your C++ struct, you should be fine. This means you should avoid the DirectXMath SIMD types like XMVECTOR and XMMATRIX, since they have 16-byte alignment. Try changing your struct to this:

struct SInstance
{
	XMFLOAT4x4 matLocation;
	int dTemplateType;
};

If you do this, the stride of your structured buffer should match the stride expected by your shader. If you have a mismatched stride, the runtime will transparently set your buffer to NULL which will cause you to get 0's when your shader attempts to access it. If you create the device with the DEBUG flag, the runtime will output an error message to tell you that this has occurred.

#2MJP

Posted 08 December 2012 - 09:07 PM

This isn't your problem, but you don't need to set the "ElementOffset" or "ElementWidth" members of D3D11_BUFFER_SRV. The structure is defined like this:

typedef struct D3D11_BUFFER_SRV
	{
	union
		{
		UINT FirstElement;
		UINT ElementOffset;
		}  ;
	union
		{
		UINT NumElements;
		UINT ElementWidth;
		}  ;
	}  D3D11_BUFFER_SRV;

Since they're in unions, you can only set one or the other in each union pair. For a structured buffer you want to use "FirstElement" and "NumElements".

Anyway, I suspect your problem is with your C++ struct. If you check sizeof(SInstance), you'll find that it's 32 bytes in size. This is because the alignment requirements of XMMATRIX (XMMATRIX is already 16-byte aligned, you don't need to add the alignment manually) will cause the struct to have 16-byte alignment, which will cause the compiler to insert 12 bytes of padding after dTemplateType. However if you were to declare something like this in HLSL..

struct SInstance
{
	float4x4 matLocation;
	int dTemplateType;
};

...this struct will be 20 bytes in size. Structs for structured buffers don't have 16-byte alignment requirements, that was an incorrect assumption on your part. HLSL really only works in terms of 4-byte values, so structs used for structured buffers will pretty much always have 4-byte alignment. If you stick to using 4-byte types in your C++ struct, you should be fine. This means you should avoid the DirectXMath SIMD types like XMVECTOR and XMMATRIX, since they have 16-byte alignment. Try changing your struct to this:

struct SInstance
{
	XMFLOAT4x4 matLocation;
	int dTemplateType;
};

If you do this, the stride of your structured buffer should match the stride expected by your shader. If you have a mismatched stride, the runtime will transparently set your buffer to NULL which will cause you to get 0's when your shader attempts to access it. If you create the device with the DEBUG flag, the runtime will output an error message to tell you that this has occurred.

#1MJP

Posted 08 December 2012 - 06:16 PM

This isn't your problem, but you don't need to set the "ElementOffset" or "ElementWidth" members of D3D11_BUFFER_SRV. The structure is defined like this:

typedef struct D3D11_BUFFER_SRV
    {
    union
	    {
	    UINT FirstElement;
	    UINT ElementOffset;
	    }  ;
    union
	    {
	    UINT NumElements;
	    UINT ElementWidth;
	    }  ;
    }  D3D11_BUFFER_SRV;

Since they're in unions, you can only set one or the other in each union pair. For a structured buffer you want to use "FirstElement" and "NumElements".

Anyway, I suspect your problem is with your C++ struct. If you check sizeof(SInstance), you'll find that it's 32 bytes in size. This is because the alignment requirements of XMMATRIX (XMMATRIX is already 16-byte aligned, you don't need to add the alignment manually) will cause the struct to have 16-byte alignment, which will cause the compile to insert 12 bytes of padding after dTemplateType. However if you were to declare something like this in HLSL..

struct SInstance
{
    float4x4 matLocation;
    int dTemplateType;
};

...this struct will be 20 bytes in size. Structs for structured buffers don't have 16-byte alignment requirements, that was an incorrect assumption on your part. HLSL really only works in terms of 4-byte values, so structs used for structured buffers will pretty much always have 4-byte alignment. If you stick to using 4-byte types in your C++ struct, you should be fine. This means you should avoid the DirectXMath SIMD types like XMVECTOR and XMMATRIX, since they have 16-byte alignment. Try changing your struct to this:

struct SInstance
{
    XMFLOAT4x4 matLocation;
    int dTemplateType;
};

If you do this, the stride of your structured buffer should match the stride expected by your shader. If you have a mismatched stride, the runtime will transparently set your buffer to NULL which will cause you to get 0's when your shader attempts to access it. If you create the device with the DEBUG flag, the runtime will output an error message to tell you that this has occurred.

PARTNERS