Jump to content
  • Advertisement
Sign in to follow this  
Narf the Mouse

How do I do something like Effect.SetValue<T> in DX11?

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

In DX9 (SlimDX), there's this great Effect.SetValue<T> function which allows me to set a variable with any value type.

In DX11, there doesn't seem to be any way to do that directly, although there is a way to set a variable to a stream.

So...I'm supposed to keep a stream around to write arbitrary variables in, so I can write the stream to the effect variable? That...Seems kinda backward.

Help? Thanks.

Share this post


Link to post
Share on other sites
Advertisement

[quote name='Washu' timestamp='1333485454' post='4927994']
I believe you should be using cbuffers with DX11.

...How is making something harder, progress?
[/quote]

because cbuffers are not about ( arguably , I find they clean up the design of CPU/GPU communication but it is just me ) making things easier, it's about making things faster.

Share this post


Link to post
Share on other sites
The interface for effects changed in D3D10. Instead of being able to just set a value using a string, you have to explicitly ask for an effect variable (which you can do by string name, or by index). Once you have this variable you can interpret it as a variable type (vector, matrix, texture, etc.) and then set a value for it. See the documentation Effect.GetVariableByName, and the EffectVariable class.

Effects mostly abstract away constant buffers, so you don't have to really care about them if you don't want to. However if you get into performance problems you may have to learn how they work, and the best practices for using them when authoring shaders and updating their contents. If used right they're much faster than the old shader constant register system used in D3D9. But again, if you're using effects you can just set variables by name and not worry about it.

Share this post


Link to post
Share on other sites

D3DXCompileShaderFromFile(W_STR(shaderfilename), NULL, NULL, C_STR(functionName), C_STR(versionString), shaderflags, &m_vertexShaderSourceCode, &compileErrors, &m_constantTable);


When you compile the shader with D3DXCompileShaderFromFile function you can pass it a pointer to a constant table. Once you have that pointer it works the same way as setting a variable on an effect. See this link for MSDN documentation on the ID3DXConstantTable.

PS:
Ignore the W_STR and C_STR macros in that piece of code they just enforce a unicode or ansi code string as my string class stores either unicode or ansi characters but converts between them when needed.

You can find the full source for my DX9 shader class here: h, cpp

Share this post


Link to post
Share on other sites
In DX9 there are 2 sets of constant registers, one for the vertex shader and one for the pixel shader. Each register is a float4. The way it worked is when you compile a shader, the compiler assigns your constants to a register, and that register is used in the resulting assembly. Then at runtime when you want to run that shader, you have to set the value of the corresponding register by calling SetVertexShaderConstantF or SetPixelShaderConstantF. Larger variables could also span multiple registers, so for instance a float4x4 will take up 4 consecutive registers.

There are two ways to know which register a variable is mapped to. The first way is to manually assign a register to your variable in your HLSL code. The syntax is like this:

float3 Color : register(c0);


The other way is to ID3DXConstantTable to reflect the shader, and get the register index. Like the above poster mentioned, ID3DXConstantTable can also set values for you by string or by handle so that you don't need to explicitly query the index and call Set*ShaderConstantF yourself. Under the hood an ID3DXEffect maintains its own constant table for all compiled shaders, which is how it's able to set variables for you.

Share this post


Link to post
Share on other sites

In DX9 there are 2 sets of constant registers, one for the vertex shader and one for the pixel shader. Each register is a float4. The way it worked is when you compile a shader, the compiler assigns your constants to a register, and that register is used in the resulting assembly. Then at runtime when you want to run that shader, you have to set the value of the corresponding register by calling SetVertexShaderConstantF or SetPixelShaderConstantF. Larger variables could also span multiple registers, so for instance a float4x4 will take up 4 consecutive registers.

There are two ways to know which register a variable is mapped to. The first way is to manually assign a register to your variable in your HLSL code. The syntax is like this:

float3 Color : register(c0);


The other way is to ID3DXConstantTable to reflect the shader, and get the register index. Like the above poster mentioned, ID3DXConstantTable can also set values for you by string or by handle so that you don't need to explicitly query the index and call Set*ShaderConstantF yourself. Under the hood an ID3DXEffect maintains its own constant table for all compiled shaders, which is how it's able to set variables for you.

Ok; so why is it slower if it's still justusing a constant buffer?

Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!