Sign in to follow this  
Narf the Mouse

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

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
[quote name='Narf the Mouse' timestamp='1333485990' post='4927998']
[quote name='Washu' timestamp='1333485454' post='4927994']
I believe you should be using cbuffers with DX11.
[/quote]
...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
[code]
D3DXCompileShaderFromFile(W_STR(shaderfilename), NULL, NULL, C_STR(functionName), C_STR(versionString), shaderflags, &m_vertexShaderSourceCode, &compileErrors, &m_constantTable);
[/code]

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 [url="http://msdn.microsoft.com/en-us/library/windows/desktop/bb205762(v=vs.85).aspx"]this link[/url] 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: [url="http://code.google.com/p/dxengine/source/browse/trunk/src/Graphics/D3D9/D3D9VertexShader.h"]h[/url], [url="http://code.google.com/p/dxengine/source/browse/trunk/src/Graphics/D3D9/D3D9VertexShader.cpp"]cpp[/url]

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:
[code]
float3 Color : register(c0);
[/code]

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
[quote name='MJP' timestamp='1333588047' post='4928352']
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:
[code]
float3 Color : register(c0);
[/code]

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.
[/quote]
Ok; so why is it slower if it's still justusing a constant buffer?

Share this post


Link to post
Share on other sites
It's not using a constant buffer, it's using a set of global registers that's shared among all shaders. In D3D10/D3D11 a constant buffer is an actual Buffer resource, much like a vertex buffer or a texture. This means that if shader A uses registers 0-10 for a bunch of variables and then shader B uses register 0-10 for an entirely different set of variables, then all of those registers have to be set before both draw calls. This is true even if those variables never change during the lifetime of the program. With constant buffers, you could make two static constant buffers, fill them with data during initialization, and then at runtime you could just bind each constant buffer before drawing with each shader. Even setting the variable values are slower in D3D9, since all of the register values have to get copied into the command buffer. With constant buffers you can use the same dynamic resource update methods you use for other buffer types, which can make it a lot more efficient.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this