Wow, yet another awesome reply. Translation won't be a problem, I think.
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.