Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


Don't forget to read Tuesday's email newsletter for your chance to win a free copy of Construct 2!


[D3D11] HLSL, StructuredBuffer and System Values


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
12 replies to this topic

#1 RyxiaN   Members   -  Reputation: 137

Like
0Likes
Like

Posted 03 May 2012 - 04:16 AM

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!

Sponsor:

#2 MJP   Moderators   -  Reputation: 11585

Like
2Likes
Like

Posted 03 May 2012 - 12:21 PM

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, 03 May 2012 - 12:22 PM.


#3 Adam_42   Crossbones+   -  Reputation: 2568

Like
1Likes
Like

Posted 03 May 2012 - 12:58 PM

You might find http://software.intel.com/en-us/articles/microsoft-directcompute-on-intel-ivy-bridge-processor-graphics/ useful.

#4 RyxiaN   Members   -  Reputation: 137

Like
0Likes
Like

Posted 03 May 2012 - 02:52 PM

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. Posted Image

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. Posted Image

#5 MJP   Moderators   -  Reputation: 11585

Like
2Likes
Like

Posted 03 May 2012 - 03:50 PM

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. Posted Image


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, 03 May 2012 - 03:50 PM.


#6 RyxiaN   Members   -  Reputation: 137

Like
0Likes
Like

Posted 04 May 2012 - 01:01 AM

Wow, yet another awesome reply. Translation won't be a problem, I think. Posted Image 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, 04 May 2012 - 01:04 AM.


#7 Tsus   Members   -  Reputation: 1049

Like
1Likes
Like

Posted 04 May 2012 - 02:17 AM

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. Posted Image
Cheers!

#8 RyxiaN   Members   -  Reputation: 137

Like
0Likes
Like

Posted 04 May 2012 - 02:49 AM

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. Posted Image
Cheers!


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!

#9 Tsus   Members   -  Reputation: 1049

Like
1Likes
Like

Posted 04 May 2012 - 03:27 AM

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, 04 May 2012 - 03:32 AM.


#10 RyxiaN   Members   -  Reputation: 137

Like
0Likes
Like

Posted 04 May 2012 - 04:09 AM

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. Posted Image And yeah, apparantly SV_InstanceID wasn't sent to the pixel shader. Now it works like a charm! Time to mess around a little. Posted Image

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, 04 May 2012 - 04:42 AM.


#11 Tsus   Members   -  Reputation: 1049

Like
1Likes
Like

Posted 04 May 2012 - 05:41 AM

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


It depends very, very much on the hardware, but in general you should minimize state changes. E.g. when binding a resource the caches are invalidated and several things are validated (are all mipmaps available (for textures), have the formats changed etc). Probably even worth is changing a shader, since it internally changes much more states, potentially invalidating various caches (including the texture cache) as well as inlining of functions in case you use dynamic shader linkage etc.

Writing a system that keeps on the CPU side track of the states and binds them when you submit a draw or dispatch call, isn’t worth it btw, since the GPU already does that for you. Posted Image

We have many experts here in the forum, so if you open up a new topic, you can get much more (and better Posted Image) information on this.

#12 RyxiaN   Members   -  Reputation: 137

Like
0Likes
Like

Posted 04 May 2012 - 06:45 AM


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


It depends very, very much on the hardware, but in general you should minimize state changes. E.g. when binding a resource the caches are invalidated and several things are validated (are all mipmaps available (for textures), have the formats changed etc). Probably even worth is changing a shader, since it internally changes much more states, potentially invalidating various caches (including the texture cache) as well as inlining of functions in case you use dynamic shader linkage etc.

Writing a system that keeps on the CPU side track of the states and binds them when you submit a draw or dispatch call, isn’t worth it btw, since the GPU already does that for you. Posted Image

We have many experts here in the forum, so if you open up a new topic, you can get much more (and better Posted Image) information on this.


Allright. :) I think I'll just leave it to the experts for now. x) Doubt it will make a difference anyway. My biggest thought were more if it was worth binding the StructuredBuffer to both the vertex shader and the pixel shader, or just send along the variables from the vertex shader to the pixel shader through parameters with semantics, which I think I'll end up doing instead. :)

#13 MJP   Moderators   -  Reputation: 11585

Like
0Likes
Like

Posted 04 May 2012 - 03:00 PM

Binding a resource is going to be almost entirely CPU overhead. You'll pay some cost for the managed/native transition, for making the underlying D3D call, and possibly some driver overhead. In terms of GPU performance, it's likely that accessing the color in the vertex shader will be cheaper than accessing it in the pixel shader. This is because doing it in the vertex shader means you'll only pay for the memory access 4 times, instead of once for each pixel rendered.

At any rate, these performance differences are going to be very small unless you render a LOT of sprites.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS