Sign in to follow this  
Quat

Structured Buffers and Vertex Buffers

Recommended Posts

Quat    568
I am porting my 2D wave equation solver to the GPU using the Compute Shader. I compute the new height values of a grid to a buffer of float. Side question, is:

Buffer<float> buffer;

considered a structured buffer? Or does the buffer have to store a struct?

Now, after I compute the heights, I do another compute pass to the grid, this time generating the actual vertex data:

struct Vertex
{
float3 Pos;
float3 Normal;
float2 Tex;
};

RWStructuredBuffer<Vertex> gVertexBuffer;

This is a simple shader that transfers over the wave heights to Pos.y, generates Pos.xz, texture coordinates, and estimates normals using finite differences. However, I get an error that I cannot create a structured buffer that is also a vertex buffer.

ID3D11Device::CreateBuffer: Buffers created with D3D11_RESOURCE_MISC_BUFFER_STRUCTURED cannot specify any of the following listed bind flags. The following BindFlags bits (0x81) are set: D3D11_BIND_VERTEX_BUFFER (1)

Why can't you compute out vertices from a compute shader that will be fed in as input into a vertex buffer. This seems like a common usage of structured buffers.

Share this post


Link to post
Share on other sites
MJP    19755
[quote name='Quat' timestamp='1306422586' post='4816066']
I am porting my 2D wave equation solver to the GPU using the Compute Shader. I compute the new height values of a grid to a buffer of float. Side question, is:

Buffer<float> buffer;

considered a structured buffer?
[/quote]

No, it's a regular typed buffer. You would create the buffer [i]without[/i] the D3D11_RESOURCE_MISC_BUFFER_STRUCTURED flag, and you would create shader resource view (or unordered access view) for that buffer with DXGI_FORMAT_R32F.

[quote name='Quat' timestamp='1306422586' post='4816066']
Now, after I compute the heights, I do another compute pass to the grid, this time generating the actual vertex data:

struct Vertex
{
float3 Pos;
float3 Normal;
float2 Tex;
};

RWStructuredBuffer<Vertex> gVertexBuffer;

This is a simple shader that transfers over the wave heights to Pos.y, generates Pos.xz, texture coordinates, and estimates normals using finite differences. However, I get an error that I cannot create a structured buffer that is also a vertex buffer.

ID3D11Device::CreateBuffer: Buffers created with D3D11_RESOURCE_MISC_BUFFER_STRUCTURED cannot specify any of the following listed bind flags. The following BindFlags bits (0x81) are set: D3D11_BIND_VERTEX_BUFFER (1)

Why can't you compute out vertices from a compute shader that will be fed in as input into a vertex buffer. This seems like a common usage of structured buffers.
[/quote]

Yeah you can't have a structured buffer that's also a vertex buffer. However it's very easy to work around. You can issue a draw call with no vertex bound to the context, specifying the number of verts/primitives you want to draw. Then in your vertex shader, you can use SV_VertexID as index to sample from your StructuredBuffer as a shader resource. Then you just use the data like regular vertex data.

Share this post


Link to post
Share on other sites
Quat    568
Thanks for the reply.

[quote]
No, it's a regular typed buffer.
[/quote]

Is a typed buffer different from a raw buffer (ByteAddressBuffer) other than you don't have to do byte offsets? Why would anyone use a raw buffer when it seems the typed version is easier to use.

[quote]
Yeah you can't have a structured buffer that's also a vertex buffer. However it's very easy to work around. You can issue a draw call with no vertex bound to the context, specifying the number of verts/primitives you want to draw. Then in your vertex shader, you can use SV_VertexID as index to sample from your StructuredBuffer as a shader resource. Then you just use the data like regular vertex data.
[/quote]

Can you have a raw buffer that's also a vertex buffer? I noticed the flag D3D11_BUFFEREX_SRV_FLAG_RAW, which is described as "Bind a raw buffer to the input-assembler stage."

Share this post


Link to post
Share on other sites
MJP    19755
[quote name='Quat' timestamp='1306468929' post='4816290']
Is a typed buffer different from a raw buffer (ByteAddressBuffer) other than you don't have to do byte offsets? Why would anyone use a raw buffer when it seems the typed version is easier to use.
[/quote]

Aside from that, when you access a typed buffer the data is automatically interpreted as the type specified by the DXGI_FORMAT. With a byte-address buffer, you have to manually convert to and from the storage format when reading or writing. So for example if you wanted to store data at half precision, you would have to use f16tof32 and f32tof26 to do the conversions.

I'd imagine a raw buffer is useful if you want to store non-homogeneous data in your buffer. That way you your shader could freely convert to the storage formats, and read or write to arbitrary locations.

[quote]
Can you have a raw buffer that's also a vertex buffer? I noticed the flag D3D11_BUFFEREX_SRV_FLAG_RAW, which is described as "Bind a raw buffer to the input-assembler stage."
[/quote]

I don't think so, but I've never tried. I have no idea what that flag does...I've never noticed it until you pointed it out because I've never really had a use for byte address buffers. If you do try it out, let us know if it works!

Share this post


Link to post
Share on other sites
Jason Z    6434
I know that a resource created with a vertex buffer bind flag can't be used with the other bind flags (i.e. for SRVs or UAVs). With that in mind, I don't think it is possible to directly reinterpret a raw buffer as a vertex buffer. It should be possible to modify the contents of a raw buffer and then copy the results to another raw buffer for use as a vertex buffer, but I haven't personally tried it out.

As MJP mentioned, it is possible to use the memory of a raw buffer in a customized way. This is the attraction of it - you can make a custom data structure with not much effort, but still be able to link the structure to the rest of the rendering and computation pipelines.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this