Sign in to follow this  
Chris_F

HLSL Dynamic Linking

Recommended Posts

I noticed that they have added dynamic linking to shader model 5, which can be used through classes and interfaces. I did a quick search but didn't find much on the subject. Is it just not catching on?

Any way my real question is this. Lets say I want to implement a deferred shading renderer that stores a material ID in the gbuffer and applies different BDRFs based on that ID. What kind of performance difference, if any, would I likely see implementing it in these ways:

[code]
switch(matID)
{
case 0:
return BlinnPhongBRDF(NdotL, NdotE);
break;
case 1:
return AnisotropicBRDF(NdotL, NdotE);
break
...
}
[/code]

vs.

[code]
interface BRDF
{
float4 CalculateLighting(float NdotL, float NdotE);
};

...

return BRDFArray[matID].CalculateLighting(NdotL, NdotE);
[/code]

Share this post


Link to post
Share on other sites
According to my experience you won't see any real performance difference. In the switch example, the compiler will probably either branch or flatten, in the dynamic linking example it will look-up in some kind of function table and a jump to a correct subroutine. Given that probably rather large blocks of fragments will have the same material anyway, the performance will be very similar (not much divergence). Nevertheless I guess dynamic linking might be a tiny bit faster here - just my guess :)

Share this post


Link to post
Share on other sites
When I first saw the DX11 dynamic linking mentioned a couple years ago, I immediately started fantasising about being able to "plug" pieces of shaders (functions, methods?) into existing shaders on-the-fly, while just complying with an implied "function header". How terribly sad I was to find out how foolish I was and that it actually is just a "clearer" way of dynamic branching [img]http://public.gamedev.net//public/style_emoticons/default/sad.png[/img] All the shader with all the possible classes still has to compile at once [img]http://public.gamedev.net//public/style_emoticons/default/sad.png[/img]

Share this post


Link to post
Share on other sites
The best part is that the Cg compiler did this ages ago in software and doesn't require SM5, while the D3D implementation does [img]http://public.gamedev.net//public/style_emoticons/default/sad.png[/img]

EDIT: It really is something of a non-feature.

Share this post


Link to post
Share on other sites
Dynamic linkage isn't a replacement for dynamic branching. Binding of class implementations to interfaces happens in your application code, before you issue a draw call. So it doesn't make sense for a deferred light shader, where the implementation of a light might change for each pixel based on the BRDF. It would make more sense in a forward renderer, where you would pick the BRDF based on the material bound for the draw call.

Share this post


Link to post
Share on other sites
[quote name='InvalidPointer' timestamp='1333733227' post='4928835']The best part is that the Cg compiler did this ages ago in software and doesn't require SM5, while the D3D implementation does [img]http://public.gamedev.net//public/style_emoticons/default/sad.png[/img][/quote]You might be interested in this HLSL SM3 approach: [url="http://code4k.blogspot.com.au/2011/11/advanced-hlsl-using-closures-and.html"]http://code4k.blogsp...osures-and.html[/url][quote]EDIT: It really is something of a non-feature.[/quote]As MJP points out above, it lets you dictate the branch value on the CPU before issuing a draw-call, eliminating branches from shaders, while still being able to write them in a branchy way.

The earlier equivalent to this was branching on boolean registers, which some GPU drivers use to eliminate the branch in software ([i]and other drivers don't, but do still branch faster than a regular branch-on-ALU-results[/i]).

Share this post


Link to post
Share on other sites
Dynamically linked methods' performance matches that of static function calls; also, the linking overhead is just that of setting one constant buffer.

The feature is designed to reduce the combinatory explosion of number of shader types for different techniques, without sacrificing performance. For example, you can have a single abstract shader for all your material types but just switch the BRDF implementation and light implementations on the fly.

Share this post


Link to post
Share on other sites
[quote name='Hodgman' timestamp='1333775822' post='4928970']
You might be interested in this HLSL SM3 approach: http://code4k.blogsp...osures-and.html
[/quote]

Not even. It [url="http://http.developer.nvidia.com/GPUGems/gpugems_ch32.html"]literally works the same way that dynamic linkage does[/url] (at least from an API standpoint) except it doesn't require any form of hardware or driver support. If the D3D implementation worked like this it'd be *awesome,* but as it stands you need to target SM5 for it to even work which is basically a deal-killer.

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