• Advertisement
Sign in to follow this  

HLSL Dynamic Linking

This topic is 2118 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

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:


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


vs.


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

...

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

Share this post


Link to post
Share on other sites
Advertisement
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
I'd wager the HLSL compiler would reduce it basically to example #1. It's primarily a maintenance/code brevity thing, not performance.

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 sad.png All the shader with all the possible classes still has to compile at once sad.png

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 sad.png

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
The best part is that the Cg compiler did this ages ago in software and doesn't require SM5, while the D3D implementation does sad.png
You might be interested in this HLSL SM3 approach: http://code4k.blogsp...osures-and.html
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 (and other drivers don't, but do still branch faster than a regular branch-on-ALU-results).

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

You might be interested in this HLSL SM3 approach: http://code4k.blogsp...osures-and.html


Not even. It literally works the same way that dynamic linkage does (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
Sign in to follow this  

  • Advertisement