Jump to content
  • Advertisement
Sign in to follow this  
RyxiaN

[D3D11] HLSL, StructuredBuffer and System Values

This topic is 2360 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi! I'm trying to create my own SpriteBatch class, actually made one a while back, but had some lousy implementation. I've seen some code around here that used a structured buffer, something like this:
StructuredBuffer<Sprite> SpriteData;

struct Sprite {
float2 Position;
float2 Size;
byte TextureIndex;
};


Now, I'm not looking for help implementing this, I'm just curious if anybody know any good guides, examples or tutorials on structured buffers. I get the structured buffer thing, having one sprite struct instance per sprite. But what vertices would you send to the graphics card? I mean, if all the data you need is in the structured buffer, what would the vertices contain? They're obviously needed, else there would be nothing to process in the vertex shader. And how do you take use of the system-generated values, such as SV_InstanceID and SV_VertexID? On the samples I saw, SV_InstanceID (I think) were used to fetch the right Sprite instance from the structured buffer, but can't really get how this works.

Again: I'm not asking anyone here to help implement a SpriteBatch class/shader, only if there is any good guides or similar. Havn't found much searching around...

Thanks in advance!

Share this post


Link to post
Share on other sites
Advertisement
You don't actually need any vertices or vertex buffers to render anything. You can bind a NULL vertex buffer and then say "draw 4 vertices", and then the GPU will invoke your vertex shader 4 times. Since you don't have any vertex data to pull in you typically use SV_VertexID and/or SV_InstanceID to decide what to do. Here's a really simple example of using SV_VertexID to generate a triangle that would cover the entire screen:


float4 VSMain(in uint VtxID : SV_VertexID) : SV_Position
{
float4 output = float4(-1.0f, 1.0f, 1.0f, 1.0f);

if(VtxID == 1)
output = float4(3.0f, 1.0f, 1.0f, 1.0f);
else if(VtxID == 2)
output = float4(-1.0f, -3.0f, 1.0f, 1.0f);

return output;
}


So to invoke this you could just bind a NULL VB + IB, and call Draw with a vertex count of 3. If you want, you can also use an index buffer with a NULL vertex buffer. So for instance if you wanted to draw an indexed quad, you could have an index buffer with 6 indices that forms 2 triangles from 4 vertices. In this case SV_VertexID will give you the value from the index buffer, so in this case it would be 0 through 3. Then you could do something similar to the code above to generate one of the 4 corners of your quad for a sprite. SV_InstanceID works the same way, except it gives you the index of the instance being drawn. So in your case you would probably just use it to directly index into your structured buffer of sprite data.

Unfortunately I don't know of any good tutorials or other resources regarding structured buffers. The D3D11 book that I collaborated on (link is in my signature) has a good overview of D3D11 resource types, but that's obviously not freely available. However if you any questions I can certainly try and answer them for you. Edited by MJP

Share this post


Link to post
Share on other sites
You might find http://software.intel.com/en-us/articles/microsoft-directcompute-on-intel-ivy-bridge-processor-graphics/ useful.

Share this post


Link to post
Share on other sites

You don't actually need any vertices or vertex buffers to render anything. You can bind a NULL vertex buffer and then say "draw 4 vertices", and then the GPU will invoke your vertex shader 4 times. Since you don't have any vertex data to pull in you typically use SV_VertexID and/or SV_InstanceID to decide what to do. Here's a really simple example of using SV_VertexID to generate a triangle that would cover the entire screen:


float4 VSMain(in uint VtxID : SV_VertexID) : SV_Position
{
float4 output = float4(-1.0f, 1.0f, 1.0f, 1.0f);

if(VtxID == 1)
output = float4(3.0f, 1.0f, 1.0f, 1.0f);
else if(VtxID == 2)
output = float4(-1.0f, -3.0f, 1.0f, 1.0f);

return output;
}


So to invoke this you could just bind a NULL VB + IB, and call Draw with a vertex count of 3. If you want, you can also use an index buffer with a NULL vertex buffer. So for instance if you wanted to draw an indexed quad, you could have an index buffer with 6 indices that forms 2 triangles from 4 vertices. In this case SV_VertexID will give you the value from the index buffer, so in this case it would be 0 through 3. Then you could do something similar to the code above to generate one of the 4 corners of your quad for a sprite. SV_InstanceID works the same way, except it gives you the index of the instance being drawn. So in your case you would probably just use it to directly index into your structured buffer of sprite data.

Unfortunately I don't know of any good tutorials or other resources regarding structured buffers. The D3D11 book that I collaborated on (link is in my signature) has a good overview of D3D11 resource types, but that's obviously not freely available. However if you any questions I can certainly try and answer them for you.


Wow, this cleared up ALOT! Thanks a bunch!
To set a structured buffer, you have to create a ShaderResourceView to wrap it first? And about SV_InstanceID, you say it gives the index of the instance being drawn. What does an instance mean? Does it have to do with the draw instanced call? Havn't really looked into that yet, but if those are connected, I'll get right to it. biggrin.png


You might find http://software.inte...essor-graphics/ useful.


Looks very interesting! Will look more into it tomorrow when I get the time! Thanks a lot. smile.png

Share this post


Link to post
Share on other sites

To set a structured buffer, you have to create a ShaderResourceView to wrap it first?


Yes. First you create the buffer using CreateBuffer, making sure that you specify D3D11_BIND_SHADER_RESOURCE as a bind flag. Then you pass the buffer to CreateShaderResourceView to create create an SRV for that buffer. This is the code that I use in my samples:


void StructuredBuffer::Initialize(ID3D11Device* device, UINT stride, UINT numElements, BOOL useAsUAV,
BOOL appendConsume, BOOL useAsDrawIndirect, const void* initData)
{
Size = stride * numElements;
Stride = stride;
NumElements = numElements;

if(appendConsume || useAsDrawIndirect)
useAsUAV = true;

D3D11_BUFFER_DESC bufferDesc;
bufferDesc.ByteWidth = stride * numElements;
bufferDesc.Usage = D3D11_USAGE_DEFAULT;
bufferDesc.BindFlags = D3D11_BIND_SHADER_RESOURCE;
bufferDesc.BindFlags |= useAsUAV ? D3D11_BIND_UNORDERED_ACCESS : 0;
bufferDesc.CPUAccessFlags = 0;
bufferDesc.MiscFlags = D3D11_RESOURCE_MISC_BUFFER_STRUCTURED;
bufferDesc.MiscFlags |= useAsDrawIndirect ? D3D11_RESOURCE_MISC_DRAWINDIRECT_ARGS : 0;
bufferDesc.StructureByteStride = stride;

D3D11_SUBRESOURCE_DATA subresourceData;
subresourceData.pSysMem = initData;
subresourceData.SysMemPitch = 0;
subresourceData.SysMemSlicePitch = 0;

DXCall(device->CreateBuffer(&bufferDesc, initData != NULL ? &subresourceData : NULL, &Buffer));

D3D11_SHADER_RESOURCE_VIEW_DESC srvDesc;
srvDesc.Format = DXGI_FORMAT_UNKNOWN;
srvDesc.ViewDimension = D3D11_SRV_DIMENSION_BUFFER;
srvDesc.Buffer.ElementOffset = 0;
srvDesc.Buffer.ElementWidth = numElements;
DXCall(device->CreateShaderResourceView(Buffer, &srvDesc, &SRView));

if(useAsUAV)
{
D3D11_UNORDERED_ACCESS_VIEW_DESC uavDesc;
uavDesc.Format = DXGI_FORMAT_UNKNOWN;
uavDesc.ViewDimension = D3D11_UAV_DIMENSION_BUFFER;
uavDesc.Buffer.FirstElement = 0;
uavDesc.Buffer.Flags = appendConsume ? D3D11_BUFFER_UAV_FLAG_APPEND : 0;
uavDesc.Buffer.NumElements = numElements;
DXCall(device->CreateUnorderedAccessView(Buffer, &uavDesc, &UAView));
}
}


For passing instance data to a shader you will probably want to use D3D11_USAGE_DYNAMIC instead of DEFAULT, and also D3D11_CPU_ACCESS_WRITE as the CPUAccessFlag. This will tell the driver that the CPU will be updating the contents of the buffer every frame. You also probably don't want to mess around with UAV's or DrawIndirect until you're doing more advanced things.


And about SV_InstanceID, you say it gives the index of the instance being drawn. What does an instance mean? Does it have to do with the draw instanced call? Havn't really looked into that yet, but if those are connected, I'll get right to it. biggrin.png


Instancing lets you draw multiple instances of the same mesh multiple times. So for instance, lets say you wanted to draw a quad 10 times. You would have a vertex buffer with 4 verts, and an index buffer with 6 indices forming 2 triangles. You would then call DrawIndexedInstanced with an IndexCountPerInstance=6 and InstanceCount=10, and you would draw that quad 10 times. Your vertex shader will then receive the same 4 vertices for the first instance (with SV_InstanceID=0), then the 4 vertices for the second instance (with SV_InstanceID=1), and so on.

With instancing you can also store your per-instance data in a secondary vertex buffer, and the GPU will automatically pass the right data to the vertex shader based on the instance being drawn. To do this your input layout needs to indicate the per-instance data by using D3D11_INPUT_PER_INSTANCE_DATA for the input classification. But you can also just use the instance ID to index into a buffer, which is exactly what you're attempting to do.

EDIT: Sorry, I forgot that you were using SlimDX and not C++. If you need help translating anything let me know. Edited by MJP

Share this post


Link to post
Share on other sites
Wow, yet another awesome reply. Translation won't be a problem, I think. smile.png Yeah, instancing it is, seems to be exactly what I'm looking for. I got that working. There's just one problem, with the StructuredBuffer. I think I'm creating it right, and I think I'm setting it right aswell, but it doesn't seem to get set for some reason.

Here's some code (keep in mind it's all just for testing purposes, so it's no final code:
Shader2D.hlsl:
struct Sprite {
float2 TopLeft;
float2 TopRight;
float2 BotLeft;
float2 BotRight;
float4 Color;
};

StructuredBuffer<Sprite> Sprites : register(s0);

float4 VShader(in uint VID : SV_VertexID) : SV_POSITION {
Sprite sprite = Sprites[0];

float2 pos;
if (VID == 0)
pos = float2(-1.0f, 1.0f);// sprite.TopLeft;
else if (VID == 1)
pos = float2(0.0f, 1.0f);// sprite.TopRight;
else if (VID == 2)
pos = float2(-1.0f, 0.0f);// sprite.BotLeft;
else
pos = float2(0.0f, 0.0f);// sprite.BotRight;

return float4(pos, 1.0f, 1.0f);
};



float4 PShader(in float4 position : SV_POSITION, in uint IID : SV_InstanceID) : SV_TARGET {
return Sprites[IID].Color;
}


Buffer, SRV and index buffer creation:
Sprite sprite1 = new Sprite {
TopLeft = new Vector2(-1.0f, 1.0f),
TopRight = new Vector2(0.0f, 1.0f),
BotLeft = new Vector2(-1.0f, 0.0f),
BotRight = new Vector2(0.0f, 0.0f),
Color = new Vector4(1.0f, 1.0f, 0.0f, 1.0f)
};

BufferDescription bufferDescription = new BufferDescription();
bufferDescription.BindFlags = BindFlags.ShaderResource;
bufferDescription.CpuAccessFlags = CpuAccessFlags.Write;
bufferDescription.OptionFlags = ResourceOptionFlags.StructuredBuffer;
bufferDescription.SizeInBytes = MaxSprites * Sprite.SizeInBytes;
bufferDescription.StructureByteStride = Sprite.SizeInBytes;
bufferDescription.Usage = ResourceUsage.Dynamic;

using (DataStream data = new DataStream(MaxSprites * Sprite.SizeInBytes, true, true)) {
data.Write(sprite1);
data.Position = 0;

structuredBuffer = new Buffer(graphics.Device, data, bufferDescription);
}

ShaderResourceViewDescription spritesDescription = new ShaderResourceViewDescription();
spritesDescription.Dimension = ShaderResourceViewDimension.ExtendedBuffer;
spritesDescription.FirstElement = 0;
spritesDescription.Format = Format.Unknown;
spritesDescription.ElementCount = MaxSprites;
sprites = new ShaderResourceView(graphics.Device, structuredBuffer, spritesDescription);

using (DataStream dataStream = new DataStream(sizeof(uint) * 6, true, true)) {
dataStream.Write(0);
dataStream.Write(1);
dataStream.Write(2);
dataStream.Write(2);
dataStream.Write(1);
dataStream.Write(3);

dataStream.Position = 0;

indexBuffer = new Buffer(graphics.Device, dataStream, sizeof(uint) * 6, ResourceUsage.Default, BindFlags.IndexBuffer, CpuAccessFlags.None, ResourceOptionFlags.None, 0);
}


Setting the SRV and rendering:
DeviceContext context = graphics.Context;
InputAssemblerWrapper inputAssembler = context.InputAssembler;

inputAssembler.InputLayout = inputLayout;
inputAssembler.PrimitiveTopology = PrimitiveTopology.TriangleList;
inputAssembler.SetVertexBuffers(0, new VertexBufferBinding(null, 0, 0));
inputAssembler.SetIndexBuffer(indexBuffer, Format.R32_UInt, 0);

context.PixelShader.Set(pixelShader);
context.VertexShader.Set(vertexShader);
context.VertexShader.SetShaderResource(sprites, 0);

context.DrawIndexedInstanced(6, 1, 0, 0, 0);


What I get is a black square. Should be (1.0f, 1.0f, 0.0f, 1.0f) colored. Also, if I use the points from the structured buffer, it doesnt render anything (or probably everything at (0.0f, 0.0f). So looks like the StructuredBuffer doesnt get set...

The only clear difference I see between your code and mine in the creation part is that I set srvDesc.Dimension to ExtendedBuffer instead of Buffer. I saw some other tutorial which did set it to Extended, and if I don't, the SRV won't create, returning a Direct3D11Exception.

Do I set it right? Perhaps it's something with the slot (0), in the SetShaderResource call?

EDIT: Removed the qoute, was so messy. Edited by RyxiaN

Share this post


Link to post
Share on other sites
Hi!


The only clear difference I see between your code and mine in the creation part is that I set srvDesc.Dimension to ExtendedBuffer instead of Buffer. I saw some other tutorial which did set it to Extended, and if I don't, the SRV won't create, returning a Direct3D11Exception.

AFAIK, StructuredBuffers and ByteAddressBuffers go under ExtendedBuffer, so yes, your change is correct.

I think, on the HLSL side, the shader resource view on the structured buffer should be assigned to a t-register. (s are for samplers)
StructuredBuffer<Sprite> Sprites : register(t0);

Hope it helps. smile.png
Cheers!

Share this post


Link to post
Share on other sites

Hi!

[quote name='RyxiaN' timestamp='1336114904' post='4937305']
The only clear difference I see between your code and mine in the creation part is that I set srvDesc.Dimension to ExtendedBuffer instead of Buffer. I saw some other tutorial which did set it to Extended, and if I don't, the SRV won't create, returning a Direct3D11Exception.

AFAIK, StructuredBuffers and ByteAddressBuffers go under ExtendedBuffer, so yes, your change is correct.

I think, on the HLSL side, the shader resource view on the structured buffer should be assigned to a t-register. (s are for samplers)
StructuredBuffer<Sprite> Sprites : register(t0);

Hope it helps. smile.png
Cheers!
[/quote]

Made no difference. :/ Still renders black. But I still think you're right though, so I'll keep it on register(t0). :)
Thanks for the input!

Share this post


Link to post
Share on other sites
So, the quad is visible right?
Does it receive the right color, if you hard code it in the pixel shader?

Hm, you access the structured buffer in the pixel shader, too.
Have you bound it to the pixel shader stage as well?
context.PixelShader.SetShaderResource(sprites, 0);

Besides, I think the SV_InstanceID is input to the vertex shader, not the pixel shader. You could as well read the color in the vertex shader and pass it by to the pixel shader.

Cheers! Edited by Tsus

Share this post


Link to post
Share on other sites

So, the quad is visible right?
Does it receive the right color, if you hard code it in the pixel shader?

Hm, you access the structured buffer in the pixel shader, too.
Have you bound it to the pixel shader stage as well?
context.PixelShader.SetShaderResource(sprites, 0);

Besides, I think the SV_InstanceID is input to the vertex shader, not the pixel shader. You could as well read the color in the vertex shader and pass it by to the pixel shader.

Cheers!


Wow, so stupid. No I hadn't bound it to the pixel shader. biggrin.png And yeah, apparantly SV_InstanceID wasn't sent to the pixel shader. Now it works like a charm! Time to mess around a little. smile.png

Thank you both!

EDIT: Is binding a resource very time consuming? I assume they are already on the GPU so shouldn't be much right? Edited by RyxiaN

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!