Jump to content
  • Advertisement
Sign in to follow this  
Finoli

OpenGL Using different shaders in DirectX

This topic is 467 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello! 

New to the forum and generally bad at posting straight-forward questions but here I go..

 

So, last semester (im a Technical Artist student) we had a 3D-programming course in which we had an assignment to use DirectX or OpenGL to render a textured quad, spinning around in model-space. All fine and dandy, just used a simple VS and FS.
Now we're starting a project course in which we will be developing a small game. Since we have four Game programmers and two Technical Artists, most of my work will be modeling, texturing and animating, but I really want to understand the programming theory as well.

So here's what I havent learned which seems quite crucial to me:
How are different shaders used within a single project? 
Let's say I have a basic VS and FS for basic drawing of meshes. Alright. Great!

Now, i implement a VS for creating waves on a water mesh/plane. How would that fit together with the other VS? Do we somehow join them together and have some condition to check if the vertex belongs to a water mesh or a basic static mesh? Do we switch shaders between draw-calls? Some other programming magic? 
Not having a somewhat clear idea about how these things are done really hinders my confidence and ability to contribute to our game.

Sorry for the ambiguity, as previously stated I'm not very good at explaining this kind of stuff, but I hope it made some sense.

Thanks in advance ^_^ 
 

Share this post


Link to post
Share on other sites
Advertisement
You switch shaders between draw calls, yes.

Dealing with all the various shader combinations and connotations this leads to is considered a bit of an unsolved problem in graphics programming.

Google shader combinations for more info than you could possibly want.

Share this post


Link to post
Share on other sites

You switch shaders between draw calls, yes.

Dealing with all the various shader combinations and connotations this leads to is considered a bit of an unsolved problem in graphics programming.

Google shader combinations for more info than you could possibly want.

Thank you so much! I've tried googling before but never found a good phrase to google; yours generated so many good results!  :D

Share this post


Link to post
Share on other sites
Now, i implement a VS for creating waves on a water mesh/plane. How would that fit together with the other VS? Do we somehow join them together and have some condition to check if the vertex belongs to a water mesh or a basic static mesh? Do we switch shaders between draw-calls? Some other programming magic? Not having a somewhat clear idea about how these things are done really hinders my confidence and ability to contribute to our game.

The main idea - VS and PS are bound to pipeline. Only [0 or 1] VS and [0 or 1]PS maybe bound.

Each time you do context->PSSetShader, prev PS shader will be unbond, and new will be bound.

RenderScene()
    RenderWater()
        context->VSSetShader(vsWater);
        context->PSSetShader(psWater)
        context->Draw();

    RenderGround()
        context->PsSetShader(psGround)
        context->Draw() //will use vsWater shader, already bound in RenderWater()

    RenderSolders()
        context->VSSetShader(vsSolder); //switch from vsWater
        context->PSSetShader(psSolder)  //switch from psGround
        for (int i = 0; i < solders; ++i)
        {
            <update solder buffer>
            context->Draw();  //Use the same shaders , no need to Set() them again here
        }

m_swapChain->Present();  
Edited by Happy SDE

Share this post


Link to post
Share on other sites

 

Now, i implement a VS for creating waves on a water mesh/plane. How would that fit together with the other VS? Do we somehow join them together and have some condition to check if the vertex belongs to a water mesh or a basic static mesh? Do we switch shaders between draw-calls? Some other programming magic? Not having a somewhat clear idea about how these things are done really hinders my confidence and ability to contribute to our game.

The main idea - VS and PS are bound to pipeline. Only [0 or 1] VS and [0 or 1]PS maybe bound.

Each time you do context->PSSetShader, prev PS shader will be unbond, and new will be bound.

RenderScene()
    RenderWater()
        context->VSSetShader(vsWater);
        context->PSSetShader(psWater)
        context->Draw();

    RenderGround()
        context->PsSetShader(psGround)
        context->Draw() //will use vsWater shader, already bound in RenderWater()

    RenderSolders()
        context->VSSetShader(vsSolder); //switch from vsWater
        context->PSSetShader(psSolder)  //switch from psGround
        for (int i = 0; i < solders; ++i)
        {
            <update solder buffer>
            context->Draw();  //Use the same shaders , no need to Set() them again here
        }

m_swapChain->Present();  

Yeah I do get that. Probably one of the reasons that I questioned this method was that I was told that one "should minimze API calls", which makes perfect sense, but I took it too far. Like, wanting to only needing to set new shaders once or twice during each frame, but I get the sense that it's not that performance impacting.

Share this post


Link to post
Share on other sites

GPU are fines running different shaders at the same time, the cost of changing shaders is merely CPU. It can be a problem of course, but to reach the case, you first need hundreds to thousands of shader change a frame with complex behavior and binding.

If you sort your draws per shader, you should be fine unless you draw the equivalent of the most stunning complex games :)

Share this post


Link to post
Share on other sites

I was told that one "should minimze API calls", which makes perfect sense, but I took it too far. Like, wanting to only needing to set new shaders once or twice during each frame, but I get the sense that it's not that performance impacting.

Every API call has a direct CPU-side cost, but you should be able to use as many as 10k API calls without blowing the budget (depending on how much cpu time the rest of your game needs). It's good practice to sort your objects into an order that will minimise calls, e.g. drawing objects that share a material at the same time.

Share this post


Link to post
Share on other sites

 

I was told that one "should minimze API calls", which makes perfect sense, but I took it too far. Like, wanting to only needing to set new shaders once or twice during each frame, but I get the sense that it's not that performance impacting.

Every API call has a direct CPU-side cost, but you should be able to use as many as 10k API calls without blowing the budget (depending on how much cpu time the rest of your game needs). It's good practice to sort your objects into an order that will minimise calls, e.g. drawing objects that share a material at the same time.

 

I would add that there is a direct AND indirect CPU cost. With D3D11, a lot of black magic and lazy evaluation will happen, it usually make your Present call a big fat black box. It is better with D3D12 where the API has more defined behavior ( like shader  compilation happen at CreatePSO, nowhere else, ... ).

 

But again, unless you diagnostics a performance issue within your engine or something really dumb and easy to fixm all you are doing is early over optimisation, and this is just a waste of time :)

Share this post


Link to post
Share on other sites

 

 

I was told that one "should minimze API calls", which makes perfect sense, but I took it too far. Like, wanting to only needing to set new shaders once or twice during each frame, but I get the sense that it's not that performance impacting.

Every API call has a direct CPU-side cost, but you should be able to use as many as 10k API calls without blowing the budget (depending on how much cpu time the rest of your game needs). It's good practice to sort your objects into an order that will minimise calls, e.g. drawing objects that share a material at the same time.

 

I would add that there is a direct AND indirect CPU cost. With D3D11, a lot of black magic and lazy evaluation will happen, it usually make your Present call a big fat black box. It is better with D3D12 where the API has more defined behavior ( like shader  compilation happen at CreatePSO, nowhere else, ... ).

 

But again, unless you diagnostics a performance issue within your engine or something really dumb and easy to fixm all you are doing is early over optimisation, and this is just a waste of time :)

 

Indeed that has been my most prominent problem thus far when it comes to programming; over optimizing and underestimating the power of processing power.  ^_^

Share this post


Link to post
Share on other sites

in  your case the standard approach would be to draw all static meshes using one shader, then switch to your water shader, and draw all your water.

 

over optimizing and underestimating the power of processing power.
 

 

always try brute force first.  computers are getting fast enough they can do many things in 3D graphics without resorting to "glory coding" like in the days of quake etc.

FYI, i can routinely do 30K draw calls per render in dx9 no problem.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!