Jump to content

  • Log In with Google      Sign In   
  • Create Account

RyxiaN

Member Since 21 Dec 2005
Offline Last Active May 29 2012 04:21 AM

Posts I've Made

In Topic: Instancing my terrain patches

26 May 2012 - 03:15 AM

Hmm, yeah I thought a little about that. But they are stored as ONE object in the application, am I right? Like so:
Texture2DDescription texture2DDescription = new Texture2DDescription();
            texture2DDescription.ArraySize = 8;

That means it has to be of dynamic usage and write to before each draw call. Isn't that kind of performance heavy, if I have say an array size of 8 and the height maps would be something like 256 * 256? But maybe it's worth the fact that it's 7 less draw calls if you fill a batch with 8 patches...? Thoughts? :D

Thanks in advance!

In Topic: [D3D11] HLSL, StructuredBuffer and System Values

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. :)

In Topic: [D3D11] HLSL, StructuredBuffer and System Values

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?

In Topic: [D3D11] HLSL, StructuredBuffer and System Values

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!

In Topic: [D3D11] HLSL, StructuredBuffer and System Values

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.

PARTNERS