• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Narf the Mouse

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

11 posts in this topic

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

Share this post


Link to post
Share on other sites
I believe you should be using cbuffers with DX11.
0

Share this post


Link to post
Share on other sites
[quote name='Washu' timestamp='1333485454' post='4927994']
I believe you should be using cbuffers with DX11.
[/quote]
...How is making something harder, progress?
-1

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

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

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]
0

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

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?
0

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

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  
Followers 0