• 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
korvax

Shader Functions

7 posts in this topic

Hi,
first let me state that im quiet new to shaders and directx 11 so this might be an easy or even "stupid" question.Is it possible to control what shaderfunctions in the shader file that will be executed and possible what input variables.. Let me show you and example.

[source lang="cpp"]float4 Texture(PS_INPUT input ) : SV_Target
{
return tex.Sample(linearSampler, input.Tex );
}

float4 noTexture( PS_INPUT input ) : SV_Target
{
return input.Color;
}
[/source]

How can I tell my program what function of the two it should execute. Can I have the same PixleShader for this
or do I need to compile two diffrent PixelShaders with diffrent entrypoints, "Texture" and "noTexture".. Or can
I simple do something like m_pContext->PSSetShader(m_pPixelShader, blbblal ,"Texture")?

And my secon question is it posible to send values to the diffrent shader functions so I can send a bool forexample?
[source lang="cpp"]float4 Texture(PS_INPUT input, bool bTex ) : SV_Target
{
if (bTex)
return tex.Sample(linearSampler, input.Tex );
else
return input.Color;

}[/source]
I have done some searching but on the subject but I have mainly found some effect solutions for DX10
and some angrey post about the lacking effects system in directx 11 (even if the source for it exist in the sdk).
I hope someone can help pls or even have some examples (im quiet new to this as I stated
). Edited by korvax
0

Share this post


Link to post
Share on other sites
In your first example, you would compile two different shaders. When you compile a shader you specify the entry point function, so you would compile one with "Texture" as the entry point and then the other with "noTexture" as the entry point.

To pass values to a shader, you use a constant buffer. The syntax looks like this:

[code]
cbuffer Constants
{
bool bTex;
}
float4 Texture(PS_INPUT input) : SV_Target
{
if (bTex)
return tex.Sample(linearSampler, input.Tex );
else
return input.Color;
}
[/code]

Then you also need to handle creating a constant buffer, filling it with data, and binding it in your C++ app code. If you're not familiar with constant buffers, I would simple recommend consulting some of the simple tutorials and samples that come with the SDK.
1

Share this post


Link to post
Share on other sites
Not sure how you compile your shader code but if you use this function you can state for example the name of the pixel shader function to use.
Like this:
[CODE]
D3DX11CompileFromFile(PATH, 0, 0, NAME, MODEL, NULL, 0, 0, &this->PS_Buffer, NULL, 0);
[/CODE]

Where PATH is the path where you shader file is located at, NAME is the name (string) of your shader function (like in your example "Texture") and MODEL the specific shader model you want to use.
For a more detailed description on how this function works:
[url="http://msdn.microsoft.com/en-us/library/windows/desktop/ff476261(v=vs.85).aspx"]http://msdn.microsoft.com/en-us/library/windows/desktop/ff476261(v=vs.85).aspx[/url]

I don't think you can set specific shader functions during run-time if that's what you were asking.
The compiler has to know which function the entry function should be.

I believe you could also do something like (haven't tried it)
[CODE]
#ifdef USETEXTURES
float4 PS(PS_input input) : SV_TARGET
{
return texture.Sample(...);
}
#else
float4 PS(PS_input input) : SV_TARGET
{
return 1.0f;
}
#endif
[/CODE]

But that would still need to be done during the compile Edited by lipsryme
0

Share this post


Link to post
Share on other sites
[quote name='MJP' timestamp='1353535473' post='5003037']
In your first example, you would compile two different shaders. When you compile a shader you specify the entry point function, so you would compile one with "Texture" as the entry point and then the other with "noTexture" as the entry point.
[/quote]

I can imagine that the with a big render application with a lot of effects like different lighting techniques or different surface materials you would end up with a x^2 number of different sharers to cover all the cases if you should have one compile of each entry point. And that would take a lot of resources to handle. Not very scalable solution I'm I right? And wouldn't it be a simalier problem with constant buffer approach. A lot of variables in the constant buffer. So i guess what im asking now is how would you solve this in a more real program.. Lets say you have one object that should be render with light shader 1 and material 2 and another with shader 1 material 1 and the last one with light shader 2 and material shader 2 I know this wasn't my original question but you answer lead me here :).



[quote name='lipsryme' timestamp='1353537875' post='5003048']
I believe you could also do something like (haven't tried it)
[CODE]
#ifdef USETEXTURES
float4 PS(PS_input input) : SV_TARGET
{
return texture.Sample(...);
}
#else
float4 PS(PS_input input) : SV_TARGET
{
return 1.0f;
}
#endif
[/CODE]
[/quote]

cool I will try this out!
0

Share this post


Link to post
Share on other sites
If i remeber correct. #ifndef/#endif are far more efficent to use rather than an "if-branch" in the pixelshader.
and you could set your defines in an global header file that you include to all your shaders. you could allso rewrite that file at runtime to set diffrent graphics options.
1

Share this post


Link to post
Share on other sites
To summarize, there are a few ways to “select functions” at render time.
You could create an “ubershader”, which means all functionality is implemented in a single shader. You can either pre-compile the code paths by using different sets of macros, e.g.
[CODE]#if DO_TEXTURE
color *= tx.Sample(input.tcoord);
#endif[/CODE]
This gives you tons of precompiled shader files. First, it is hard to obtain an overview of all the files and second, the ubershader is kind of hard to read.

The second option for an ubershader is to branch, depended on constant buffer flags.
[CODE]If (g_doTexture)
color *= tx.Sample(input.tcoord);[/CODE]
This is also not the best idea, since your shader must always be prepared for the worst case code path. Therefore, it needs to allocate the registers for the worst case code path. Less registers available means first, less threads executed in parallel, and second more memory necessary to put thread groups (warps) to rest, when waiting for memory accesses. If less thread groups can go to sleep, the latency hiding is not working at its best, thus you have slower execution times.

Option three is to write tons of independent shader files, which is really hard to maintain.

The intended solution to this dilemma is called “[url="http://msdn.microsoft.com/en-us/library/windows/desktop/ff471421%28v=vs.85%29.aspx"]dynamic shader linkage[/url]”, which allows you to dynamically link functions at bind time. In Dx11 this is implemented as inheritance in the shader code. You define an abstract class, write a few implementations and select the implementation to use at bind time of the shader. The function will be inlined and the registers are optimally allocated for your code. If I recall correctly, there was a talk at Gamefest on dynamic shader linkage.
In OpenGL 4 this feature is called subroutine functions. Actually, it’s been around since Dx9 in the CG language.

Best regards! Edited by Tsus
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