Compute buffer and structured buffer

Whats the difference between:

Buffer g_Buffer;

And unity’s documentation saying to use Structured Buffer.

StructuredBuffer g_Buffer;

Which one would i use? Are they the same? The documentation is a bit vague with this stuff from both websites…neither explain how to really use them in any detail.

I’d suggest StructuredBuffer for items you only want to read in the Compute Shader or RWStructuredBuffer for things you want to update in the shader and then potentially read back onto the CPU with bufName.GetData(1DArray).

Why is there no mention of StructuredBuffer type on microsoft’s website ? Only Buffer?

I think StructuredBuffer is Unity’s implementation - BTW have been using them for a few years without problems.

1 Like

Buffer can only use scalar, vector or matrix types as elements and max size of one element is 128 bits.

Buffer<float> _FloatBuffer;
Buffer<float4> _VectorBuffer;
Buffer<float2x2> _Matrix2x2Buffer;
// this won't compile because float4x4 is larger than 128bits
Buffer<float4x4> _Matrix4x4Bufer;

StructuredBuffer can use structure as an element.

struct MyStruct
{
    float4 color;
    float3 normal;
    bool flag;
}

StructuredBuffer<MyStruct> MyStructBuffer;

...
float4 color = MyStructBuffer[4].color;
...

So basically, use Buffer for scalars and vectors and StructuredBuffer for everything else.

1 Like

Ah thank you - this is what i was trying to understand. So if i am using the default shader types - i can just use Buffer instead of StructuredBuffer.

Yes but my point was unity documentation never mentions just regular buffer:
https://docs.microsoft.com/en-us/windows/win32/direct3dhlsl/sm5-object-buffer

At least not that i saw - so it led me to assume they were not supported.

It doesn’t mention them because not every platform supports them, for some reason. I had to replace some Buffer<> with StructuredBuffer<> to get the shaders to compile in such cases.