Shader pipeline differences OGL DX

Started by
11 comments, last by remigius 13 years, 6 months ago
I've been wanting to write an engine with both an OpenGL and an Directx path (primarily DX10 and OGL 2/3). Having programmed OpenGL for a long time I'm having some problems creating my "Graphics Abstraction Layer". The biggest problem for me currently are shaders.

The biggest difference I've noticed is the lack of an program a la glCreateProgram in DX10. That in it self is not an issue but I'm starting to worry about costs for different operations. I'm wondring when DX10 does its shader linkage and how costly it is. For example if swap shaders for different passes I would change using glUseProgram. This does not make sense in a DX10 context (at least not to my knowledge). So my question:

If I wish to swap shaders ~5 times a frame is linkage so cheap on DX10 hardware that it's just to attach my new vertex/pixel shader setup and in OpenGL do the same and then call glLinkProgram (which I avoid during my main loop otherwise). Is there a similar DX10 call? Is an ubershader the only practically viable way? How does the DX9 pipeline differ (for future branches)?

A link for a read up or just a straight answer would be nice. I haven't found anything that truly clarifies this for me on Google...
Advertisement
I don't have a clear vision of this performance as well, but I think you're overrating GL's linking stage.
There's chance GL's linking stage is yet another relic of an already conceptually distant past, you might read about ARB_separate_shader_objects.
I am sure I'll get bashed about performance by saying this, but considering that there's an official extension to do that, looks like nobody really cares about the old extra perf from binding stages together once.

To deal with the mix-and-match approach you probably have to "meta-link" the shaders yourself and expand each combination in a single program. Let me say I never liked this approach since day 1.

I sincerely wish you'll never have to go back to D3D9, but in the unfortunate case you'll need to, you'll find out its shading pipe works more or less like a very stupid D3D10 pipe.

Previously "Krohm"

Quote:
The biggest difference I've noticed is the lack of an program a la glCreateProgram in DX10.


HLSL's Effect is equivalent to GLSL's Program. They can be created with D3DX10CreateEffectFromFile.

With that said, you can use different shaders for different passes with Effects :
technique10 ShaderModel4_Technique{    pass P0    {        SetVertexShader( CompileShader( vs_4_0, Main_VS1() ) );        SetGeometryShader( NULL );        SetPixelShader( CompileShader( ps_4_0, Main_PS1() ) );    }    pass P1    {        SetVertexShader( CompileShader( vs_4_0, Main_VS2() ) );        SetGeometryShader( NULL );        SetPixelShader( CompileShader( ps_4_0, Main_PS2() ) );    }}


since the above code sample is compiled through one API interface (D3DX10CreateEffectFromFile) as opposed to the multiple calls with glAttachShader, I would assume that D3D10 does it shader linkage slighty faster than OpenGL.
Quote:Original post by 16bit_port
HLSL's Effect is equivalent to GLSL's Program. They can be created with D3DX10CreateEffectFromFile.
Except that effects are purely a convenience layer built on top of the underlying shaders - they don't actually add any functionality of their own.

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

Effect files are a decent way to prototype complex shaders, but for in-game use they're just a pain in the ass.

If your engine just services requests from effects files then you will see pathetic performance and be spending half your dev time adding lighting options and stuff.

Better to just write the core pixel generating code in a file and reference functions to do lighting and shadows and stuff. And by the time you get that far, the power of effects files becomes lost anyway. They're just the wrong tool for the job.

Playing in rendermonkey has a place, but that's not how your game should run imo.
------------------------------Great Little War Game
Quote:
but for in-game use they're just a pain in the ass.


They weren't that bad in D3D9, but they did changed stuff around in D3D10 where it did become kind of annoying.

Quote:
If your engine just services requests from effects files then you will see pathetic performance and be spending half your dev time adding lighting options and stuff.

How bad of a performance are we talking about here?

Quote:
Better to just write the core pixel generating code in a file and reference functions to do lighting and shadows and stuff.

What do you mean? Do you mean write the shaders without Effect, and use these :

ID3D10Device::CreateVertexShader
ID3D10Device::CreatePixelShader

instead?
Quote:Original post by 16bit_port
How bad of a performance are we talking about here?
I know a 360 game (that I can't name) that spends 30% of it's CPU time inside the effect API... ;(
We gave up on effects after that projcet.
Effects are mostly designed for convenience. They totally abstract away shader parameter management, which allows you to freely set parameters without having to worry at all about pipeline state or data management. Naturally this doesn't scale for setting lots of effects and parameters, since you'll start to spend significant amounts of time touch data from disparate memory locations (or on PC you can start to incur significant API overhead). If you want less overhead, you need to design your shader constant (constant buffer) layout and the layout of CPU-side data structures to match the actual usage patterns. This means clearly separating parameters by how often they change, and storing the CPU data in memory in the same layout used by the shaders so that you can quickly transfer it.

Any in regards to the OP's question...from what I know linkage between the different shader stages is not a big deal. I don't think most GPU's do anything fancy in that regard. There will probably be some shader-side work for unpacking vertex attributes, but that should be handled by input layouts. Either way...5 changes a frame is nothing at all to worry about.
Quote:Original post by Hodgman
I know a 360 game (that I can't name) that spends 30% of it's CPU time inside the effect API... ;(
We gave up on effects after that projcet.


Can you disclose what the main sinks were for this lost CPU time? With my hobbyist tinkering I found that the effects framework can be made to run quite a bit smoother by hanging on to the native handles of constants instead of addressing them by their names, but otherwise I haven't seen such performance drains. Just curious mind you, not arguing effects are super efficient when you hang on to the handles [smile]
Rim van Wersch [ MDXInfo ] [ XNAInfo ] [ YouTube ] - Do yourself a favor and bookmark this excellent free online D3D/shader book!
Quote:Original post by 16bit_port
ID3D10Device::CreateVertexShader
ID3D10Device::CreatePixelShader
instead?
Short version: Yes. :)

------------------------------Great Little War Game

This topic is closed to new replies.

Advertisement