Jump to content

  • Log In with Google      Sign In   
  • Create Account


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


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
11 replies to this topic

#1 Narf the Mouse   Members   -  Reputation: 318

Like
0Likes
Like

Posted 03 April 2012 - 11:14 AM

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.

Sponsor:

#2 Washu   Senior Moderators   -  Reputation: 4464

Like
0Likes
Like

Posted 03 April 2012 - 02:37 PM

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.
ScapeCode - Blog | SlimDX


#3 Narf the Mouse   Members   -  Reputation: 318

Like
-1Likes
Like

Posted 03 April 2012 - 02:46 PM

I believe you should be using cbuffers with DX11.

...How is making something harder, progress?

#4 kunos   Crossbones+   -  Reputation: 2184

Like
0Likes
Like

Posted 04 April 2012 - 01:24 AM


I believe you should be using cbuffers with DX11.

...How is making something harder, progress?


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
Lead Programmer
TWITTER: @KunosStefano
AssettoCorsa - netKar PRO - Kunos Simulazioni

#5 MJP   Moderators   -  Reputation: 10224

Like
0Likes
Like

Posted 04 April 2012 - 02:09 AM

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.

#6 Narf the Mouse   Members   -  Reputation: 318

Like
0Likes
Like

Posted 04 April 2012 - 03:24 AM

I learned the basics of them, but speed is not an issue at this point.

#7 Narf the Mouse   Members   -  Reputation: 318

Like
0Likes
Like

Posted 04 April 2012 - 03:14 PM

For future reference, how do you set shader variables without going through Effect in DX9?

Thansk.

#8 NightCreature83   Crossbones+   -  Reputation: 2670

Like
0Likes
Like

Posted 04 April 2012 - 04:15 PM

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, Mad Max

#9 MJP   Moderators   -  Reputation: 10224

Like
0Likes
Like

Posted 04 April 2012 - 07:07 PM

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.

#10 Narf the Mouse   Members   -  Reputation: 318

Like
0Likes
Like

Posted 05 April 2012 - 05:10 PM

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?

#11 MJP   Moderators   -  Reputation: 10224

Like
0Likes
Like

Posted 06 April 2012 - 11:01 PM

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.

#12 Narf the Mouse   Members   -  Reputation: 318

Like
1Likes
Like

Posted 07 April 2012 - 12:43 PM

Ok, think I got it now, thanks.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS