• Advertisement
Sign in to follow this  

ShaderReflection; stripping information from an Effect

This topic is 1926 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

Thanks... taking all of this information and suggestions to heart. Working on a test/play app right now to get the feel for it before I take a sledge hammer to my engine interfaces lol..

Share this post


Link to post
Share on other sites
Advertisement
Question: if two shaders (e.g., a vertex and pixel shader) share the same constant buffer(s), like so:

// HLSL file:
var var1;
var var2;
var var2;

PS_INPUT someVertexShader( VS_INPUT input );

float4 somePixelShader( PS_INPUT input);

...and I compile each shader:

[source lang="csharp"]var psByteCode = ShaderByteCode.Compile( ... );
var vsByteCode = ShaderByteCode.Compile( ... );[/source]

...does setting the constant buffer(s) of one shader instance effect the other? Or does the two only share a duplicate of the same constant buffers, and the values must be written to both individually?

Share this post


Link to post
Share on other sites
You compile 2 separate shaders, how could the constant buffer of one shader suddenly affect the constant buffer of another? You'll have to set them both separately

Share this post


Link to post
Share on other sites
When you declare a constant buffer in a shader, the shader doesn't really care about the actual D3D resources that you use to provide the data. So for instance if you have a shader with this constant buffer layout:

[code]
cbuffer Constants : register(b0)
{
float4x4 World;
float4x4 ViewProjection;
}
[/code]

When you compile a shader with this code, there's no allocation of resources for that constant buffer or anything like that. All that code says is "when this shader runs, I expect that constant buffer with 128 bytes (32 floats * 4 bytes) should be bound to slot 0 of the appropriate shader stage". It's then your application code's responsibility to actually create a constant buffer using the Buffer class with the appropriate size and binding flags, fill that that buffer with the data needed by the shader, and then bind that buffer to the appropriate slot using DeviceContext.<ShaderType>.SetConstantBuffer. If you do that correctly, your shader will pull the data from your Buffer and use it.

Now let's say you have two vertex shaders that you compile, and both use the same constant buffer layout in both shaders. In this case there is no "duplication" or anything like that, since it's your responsibility to allocate and manage constant buffer resources. So if you wanted to, it's possible to share the same Buffer between draw calls using your two different shaders. You could bind the buffer, draw with shader A, and then draw with shader B, and both shaders will pull the same data from the buffer. Or if you wanted, you could set new data into the buffer after drawing with shader A, and then shader B will use the new contents of the buffer. Or if you wanted you could create two buffers of the same size, and bind one buffer for shader A and bind the other for shader B.

An interesting consequence of this setup is that you don't necessarily need the exact same constant buffer layout in two shaders in order to shader a constant buffer. For instance shader B could just have this:

[code]
cbuffer Constants : register(b0)
{
float4x4 World;
}
[/code]

In that case it would be okay to still use the same constant buffer as shader A, since the size of the buffer expected by shader B is still less than or equal to the size of the constant buffer that was bound. But it's up to you to make sure that in all cases the right data gets to the right shader. In practice I wouldn't really recommend doing something like I just mentioned, since it can easily lead to bugs if you update a constant buffer layout in one shader but forget to do it in another. Instead I would recommend defining shared layouts in a header file, and then using #include to share it between different shaders.

Share this post


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

  • Advertisement