ShaderReflection; stripping information from an Effect

Started by
27 comments, last by MJP 11 years, 6 months ago
You don't have to create a separate file for each shader if you want to reuse data, the D3D compiler requires entry points for each shader which are the 'shader functions' you talked about. This means you can have multiple shaders in a file and the compiler will compile the correct one based on the entry point you passed it.
This also means that you can have 'shared' constant buffers or shared helper functions.
You could also just define your constant buffers in an include file and include them in all of the shader files which need them, the compiler will do the rest.

I gets all your texture budgets!

Advertisement

You don't have to create a separate file for each shader if you want to reuse data, the D3D compiler requires entry points for each shader which are the 'shader functions' you talked about. This means you can have multiple shaders in a file and the compiler will compile the correct one based on the entry point you passed it.
This also means that you can have 'shared' constant buffers or shared helper functions.
You could also just define your constant buffers in an include file and include them in all of the shader files which need them, the compiler will do the rest.


Bingo... that's the type of answer I was hoping for. Wow, that will make things FAR easier... If I do it that way, what file format/extension do I have to use? Can I continue using .fx format, or do I have to use ____ ?
_______________________________________________________________________________
CEO & Lead Developer at ATCWARE™
"Project X-1"; a 100% managed, platform-agnostic game & simulation engine

Please visit our new forums and help us test them and break the ice!
___________________________________________________________________________________
The file extension doesn't play any role, so use whatever you want :)

I gets all your texture budgets!


The file extension doesn't play any role, so use whatever you want :)


Sweet... I thought it did for some reason, but perhaps I'm just thinking of FX Composer... That's another thing... I'm wondering how difficult its going to be to use FXC now that I'm not using the standard "Effects" way of doing things... But oh well, that's the least of my concerns right now. Right now I'm writing a mini-game from scratch to get used to shaders without the Effects Framework... I'll pop up here again if I have any trouble...
_______________________________________________________________________________
CEO & Lead Developer at ATCWARE™
"Project X-1"; a 100% managed, platform-agnostic game & simulation engine

Please visit our new forums and help us test them and break the ice!
___________________________________________________________________________________
For the shared constant buffers topic, when you're processing the shaders, you can do a comparison of their resources (names, sizes, variables contained, their members, etc, etc) to get a list of unique resources used by all the shaders in the effect. You don't necessarily have to restructure any of your existing FX files.

Each specific shader will have some information on how that resource is bound to the pipeline though (e.g. the bindpoint - its index in XXXSetConstantBuffers()). That would be found in the InputBindingDescription that you'd query from your shader reflection object.
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..
_______________________________________________________________________________
CEO & Lead Developer at ATCWARE™
"Project X-1"; a 100% managed, platform-agnostic game & simulation engine

Please visit our new forums and help us test them and break the ice!
___________________________________________________________________________________
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?
_______________________________________________________________________________
CEO & Lead Developer at ATCWARE™
"Project X-1"; a 100% managed, platform-agnostic game & simulation engine

Please visit our new forums and help us test them and break the ice!
___________________________________________________________________________________
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

I gets all your texture budgets!

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:


cbuffer Constants : register(b0)
{
float4x4 World;
float4x4 ViewProjection;
}


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:


cbuffer Constants : register(b0)
{
float4x4 World;
}


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.

This topic is closed to new replies.

Advertisement