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

Started by
10 comments, last by Narf the Mouse 12 years ago
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.
Advertisement
I believe you should be using cbuffers with DX11.

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.


I believe you should be using cbuffers with DX11.

...How is making something harder, progress?

[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.

Stefano Casillo
TWITTER: [twitter]KunosStefano[/twitter]
AssettoCorsa - netKar PRO - Kunos Simulazioni

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.
I learned the basics of them, but speed is not an issue at this point.
For future reference, how do you set shader variables without going through Effect in DX9?

Thansk.

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

Worked on titles: CMR:DiRT2, DiRT 3, DiRT: Showdown, GRID 2, theHunter, theHunter: Primal, Mad Max, Watch Dogs: Legion

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.

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?

This topic is closed to new replies.

Advertisement