switching of shader technique

Started by
3 comments, last by undead 14 years, 3 months ago
Hello, i read that switch of shaders is "expensive". So what's the cost in dx9 ? lets say i have 10 switches per frame (scene,cloth,player,put all together (deffered shading),3 types of light,SSAO,reflect,refract) is it many ? What about creating only one technique but many passes : so instead of :

technique Technique0
{
    pass Pass0
    {
		VertexShader = compile vs_2_0 myvs();
		PixelShader  = compile ps_2_0 myps();
    }
}

technique post
{
    pass Pass0
    {
    	VertexShader = compile vs_2_0 postVS();
		PixelShader  = compile ps_2_0 postPS();
    }
}
it will be :

technique Technique0
{
    pass Pass0
    {
		VertexShader = compile vs_2_0 myvs();
		PixelShader  = compile ps_2_0 myps();
    }

    pass Pass1
    {
    	        VertexShader = compile vs_2_0 postVS();
		PixelShader  = compile ps_2_0 postPS();
    }

}
and in code i will change pass instead of technique ? or performance will be same ?
Advertisement
10 switches isn't too much. Even several times that should be okay.

Changing from many techniques to many passes won't change the the number of shader changes, so there's no reason to do it.
Quote:Original post by ET3D
10 switches isn't too much. Even several times that should be okay.

Changing from many techniques to many passes won't change the the number of shader changes, so there's no reason to do it.

I agree but I have a doubt.

I subspect the 2-pass technique has the potential to be a bit faster on D3D9 because a smart graphics driver could avoid an extra "vertex format/vertex shader input" linkage (as long as the VB is untouched and the vertex shader inputs are the same for both passes).

From what I saw performing speed tests on D3D9 vs D3D10, I suppose changing a VB in D3D9 always issues a new linking task (regardless of the fact the vertex format is the same), but I don't know if the same applies to techniques/passes.

To me, if one of the two is more likely to be optimized it is the pass, not the technique.

Which is your opinion?
Quote:Original post by undead

I subspect the 2-pass technique has the potential to be a bit faster on D3D9 because a smart graphics driver could avoid an extra "vertex format/vertex shader input" linkage (as long as the VB is untouched and the vertex shader inputs are the same for both passes).



That doesn't really make sense, since the driver has no knowledge of techniques or passes. Those are purely user-side constructs that are part of the effects framework. Either way the driver will only know that you switched shaders.

[Edited by - MJP on December 29, 2009 3:27:31 AM]
Quote:Original post by MJP
Quote:Original post by undead

I subspect the 2-pass technique has the potential to be a bit faster on D3D9 because a smart graphics driver could avoid an extra "vertex format/vertex shader input" linkage (as long as the VB is untouched and the vertex shader inputs are the same for both passes).



That doesn't really make sense, since the driver has no knowledge of techniques are passes. Those are purely user-side constructs that are part of the effects framework. Either way the driver will only know that you switched shaders.

It seems you have a (strong) point.

I still wonder if the single technique is faster, as my reasoning can be applied to the effect framework.

I was referring to this part of D3D10 documentation:

Quote:
Mapping the vertex data to the shader inputs with an input layout is a new way of doing things in Direct3D 10 that improves performance.

In Direct3D 10 the vertex data is mapped to the shader inputs when the input layout object is created, whereas in Direct3D 9 this mapping was done at Draw time based on the currently bound vertex declarations, vertex buffers, and vertex shaders. Doing this mapping when the input layout object is created reduces or eliminates extra linkage work for drivers at Draw time because this re-mapping is no longer necessary.


I was wondering if somewhere (it could be the effect framework itself) a part of code could verify if the vertex shader inputs are the same for both passes and skip the unnecessary linking for the second pass until another technique is in use.

After all a technique is in use between Begin/End pairs and the technique itself can know if its passes have compatible vertex shader inputs. So to just have a list of vertex shader input formats would allow the effect framework to perform such an optimization. 3 passes, one vertex shader input format.. et voila!

If it's impossible to do it because D3D API always performs linkage after a draw call, then I'm forced to believe sending a set of N draw calls, one after another without changing shader or VB forces N re-links!!!
Wow.. it would be a very bad way to draw stuff!!!

If it's the case, then I understand why they changed this mechanism... :)

This topic is closed to new replies.

Advertisement