HLSL semantics for d3d11 on cbuffers and resources

Started by
6 comments, last by MadrMan 14 years, 5 months ago
I can't seem to be able to find out which semantics global variables and resources in D3D11 use. I'm looping trough all the variables in a shader by getting a list of all the cbuffers, and then checking each variable in it. D3D11_SHADER_VARIABLE_DESC is what i end up with eventually and it seems to have all kinds of info about the variables being used, except the semantic name. (same thing for the resources) For example:

cbuffer TransformCB
{
	matrix mWorld : World;
	matrix mView : View;
	matrix mProjection : Projection;
	matrix mWVP : WorldViewProjection;
};

Texture2D basetexture : Texture0;

Using the D3D11_SHADER_VARIABLE_DESC I can get the name of the cbuffer, the names of the variables (mWorld, mView, etc) but not World, View, Projection. Any ideas on how to get them? The only reference to semantics i see in the d3d11 reflection structures seems to be in D3D11_SIGNATURE_PARAMETER_DESC.SemanticName but that struct is for the shader input and can't be used for cbuffers/global variables/resources The effect framework (which I'm not using) has ID3DX11Effect::GetVariableBySemantic() so it has to be possible somehow..
Advertisement
Semtantics are only attached to stream variables so that D3D can ensure alignment between shader stages. Constant inputs do not use semantics.

No semantics:
- cbuffers
- tbuffers
- textures
- samplers
- UAVs
- Global Variables

Semantics:
- Streamed data from the input assembler or from another shader stage.
- Accepted as an input or output parameter to a shader program.
- Cover data such as position, texture coords, colors, and other attributes.
- Located on per-vertex, per-patch, per-control point, per-primitive, or per-pixel data.
Well yes, but it's possible to feed values into those 'No semantics' you listed by checking which semantic is has and manually putting values in.

That is exactly what ID3DX11Effect::GetVariableBySemantic() is for, but since i'm not using the effect framework (or is that something else?) i'd like to know how to do it with just the normal d3d11 functions.

Would i need to write my own shader preprocessor or something? any idea how ID3DX11Effect::GetVariableBySemantic() does it?

[Edited by - MadrMan on October 31, 2009 12:46:15 PM]
If you go into the d3d sdk directory and go to:

\Utilities\Source\Effects11

you can see the entire source for the effects framework and see how they implemented it.
Looks like that reads it directly from the binary blob.
Presumably d3dx does the exact same as d3dreflect does, but it gives more complete info, and you're stuck with using the effect framework.

Guess I should try to write my own reflect function to somewhat mimic the d3dx behaviour and read it directly from the binary blob. Not exactly pretty but theres a nice explanation in one of the effect headers (EffectBinaryFormat.h to be exact) so i guess its doable.

Would be nice if d3dreflect was a bit more 'complete' though and returned more than just the bare essentials of what it reflected..
I found the same issues when I built my D3D11 renderer. Coming from D3D9, I used semantics as a linkage to my engine for all shader parameters, but I couldn't do it in D3D11. I was working with the beta version, and thought the reflection API would be built up a little more for the final release.

Instead of using the sematics I just use better variable names instead. It requires more work but still gets it done.
D3D10 and D3D11 use semantics only for stream inputs. The constant inputs can be reflected directly by variable name, as can any other shader variable. Again, Semantics are just for D3D to validate stream inputs between shader stages, or to attach a shader variable to a specific hardware feature. A good question is, if you already have a named constant variable, why give it yet another name? Seems that it just adds an extra level of complications. I'm not as familiar with the D3D9 Effects system, but in this area there may well be more differences between D3D9, and D3D10+. I suggest looking over more of the API and reviewing the differences between the shader models as you will find that there is plenty of information available in reflection and in Effects to know everything about a constant input, including type, var var name, binding location, default value, etc.

If you need more, you can look at the annotation system which lets you attach arbitrary meta data to any reflectable variable.
Jason Z:
Good idea that, using the variable names is probably what I'm gonna do too, as it requires barely any change and if the semantics are ever added to the reflection then it should be easy to change it back.

DieterVW:
It's nice to be able to call a texture a 'texNormal' in the shader to make it clear its used as a normal map and give it a semantic 'Texture2'.
Then the renderer can identify the texNormal using the semantic and you wouldn't need to name the texture 'Texture2', as 'texNormal' is a lot more clear to whoever reads the shader. Using 'texNormal' as an identifier wouldn't work so much because then the renderer would have to be written in a very specific way for the shaders.

Using annotations would be a possibility, but unless I've overlooked them it seems they also can't be reflected?..

This topic is closed to new replies.

Advertisement