Speed up shader compilation (HLSL)

Started by
33 comments, last by Meltac 11 years, 11 months ago
I tried replacing the used D3DCompiler_43.dll with the Win 8 SDK's d3dcompiler_44.dll (renamed it of course), but it didn't speed up anything at all.

Are there maybe any other files that I need to copy over in order to get the new compiler running without fxc.exe? And does the new compiler even speed up DX9 shaders?

EDIT:
Another failed test: I thought I should be able to use at least the attributes such as [loop] with the new compiler, but to my surprise it still doesn't recognize them, complaining about the leading bracket as it always did. How could that be? Is there some compiler directive or switch that must be activated to enable HLSL attributes?

And yes, I confirmed that I replaced the right compiler file which is definitely used by the game.
Advertisement
Does anybody know something about what I was asking for? I would appreciate it!
They are not probably neither using d3dcompiler nor a recent version of d3dcompiler (but d3dx9 functions), that's why loop attributes are not recognized. Though, loop attributes won't help either. Did you check on your shader that if you comment all your texture fetches, it compiles faster?

They are not probably neither using d3dcompiler nor a recent version of d3dcompiler (but d3dx9 functions), that's why loop attributes are not recognized. Though, loop attributes won't help either. Did you check on your shader that if you comment all your texture fetches, it compiles faster?


Thanks. They ARE using one specific version of d3dcompiler because the game crashes upon start-up if I deleted d3dcompiler_43.dll from SysWOW64 directory. So my guess was if I replaced that file with a newer version (e.g. the one from the Win8 SDK) I should benefit from the newer compiler features such as attributes. Doesn't seem to be the case, though.

Anyway, the attributes would have been nice to have, but what I'm really after is the speed increase that the new compiler is said to provide. This especially because, as you say, textures fetches/samplings slow down compilation significantly in my case, but are somewhat difficult to avoid in my scenario. So, it would be great to have that super-fast Win8 compiler running, instead of spending vast amounts of time trying to make some tiny optimizations that might not even speed up compilation enough to allow me to ship my shaders.
This is a long shot, but what about doing a "hybrid" precompiled version?
Here's an example from someone that's been doing that for 3DS Max for a long time: http://www.poopinmymouth.com/3d/sdk/agusturinn-shader.zip
It basically means you replace the actual vertex- and pixelshaders with compiled asm code, and then feed the variables back in as PixelShaderConstants and the like. (3DS Max requires this or you can't interface with it)

I'm looking into this as well as i've got a 10-minute compile in 3DS Max, while the (incomplete) hybrid precompiled version loads instantly.

It's a bit annoying to generate, I'm trying to contact the author of that example to see if he has a better way than manually doing it (which is a bunch of mindless copy paste work).
Hmm, it was said earlier in this thread that inline assembly is not possible in HLSL shader files.

So, how would I use those precompiled asm code (since the engine I'm using doesn't understand asm files)?
I'm only talking from 3DS Max experience here, but it works fine with Max' DXSAS compiler. So it's not exactly true that this isn't possible in general.
Have you looked at the example? I can't tell you for sure if it will work in the Xray engine, I don't know what you have tried exactly so far, but it might be worth the try to create a very simple shader and then compile an ASM version to see if the engine will take it.

BTW, I'm curious what you are doing exactly in your shader that is so heavy and improves the quality so drastically?

Have you looked at the example? I can't tell you for sure if it will work in the Xray engine, I don't know what you have tried exactly so far, but it might be worth the try to create a very simple shader and then compile an ASM version to see if the engine will take it.


Yeah, I have, and it doesn't work. The compiler / engine doesn't seem to recognize the asm { } command that he is using in his example. The compiler seems to think that I wanted to define my own function called "asm", thus exiting with error

error X3064: object literals are not allowed inside functions

I don't know what D3D or compiler version is required to allow such inline asm blocks, or if some special compiler switch / option would need to be activated in order to use it. However I think it's just not possible with the xRay engine I'm running.


BTW, I'm curious what you are doing exactly in your shader that is so heavy and improves the quality so drastically?


Several different things which are not supported by xRay itself, and which require both lots of texture lookups and complex functions, such as dynamic depth of field (focus blur), adaptive night vision, and dynamic wet surfaces including object reflections - everything done purely in one single pixel shader (with several includes, of course).

For a "causual" windows or web application my code wouldn't be very complex, but for the HLSL compiler it seems to max out the limits...

I tried replacing the used D3DCompiler_43.dll with the Win 8 SDK's d3dcompiler_44.dll (renamed it of course), but it didn't speed up anything at all.

Are there maybe any other files that I need to copy over in order to get the new compiler running without fxc.exe? And does the new compiler even speed up DX9 shaders?


Could anybody give some additional information that might help sorting out why the new compiler doesn't increase compilation speed in my case?

EDIT:
After further investigation, I've found that compilation time depends heavily on the code order. Consider the following example (simplified):

// 1. Do some basic color calculations and store them into the local variable "Color"

// 2. Create fake reflections
#ifdef WET_OBJREFLECT
Color = objreflect(Color, viewspace_P, uv, dist_factor, wind, WET_OBJMAXCOL, WET_OBJMAXREF);
#endif

// 3. Add blur to everything
#ifdef WET_REFBLUR
float contrib=1.h;
float total_blur = amount * WET_REFBLUR; // e_barrier.y * 1000; //1000 * (e_kernel.x-0.4); // --> 0.401
float f=0.f;
float inc=total_blur/WET_REFBLURQUALITY;
float3 sum = 0;
for (int i=0;i<WET_REFBLURQUALITY;i++){sum+=gaussblur(uv,f);contrib++;f+=inc;}
Color.rgb += (sum/contrib) - tex2D(s_image,uv);
#endif


This version takes nearly 6 seconds to compile, whereas the compilation time is only around 3 seconds if I swap code block 2 and 3 (applying blur BEFORE reflections).

As you can see these two code blocks do not depend directly on each other, both of them just modify the previously calculated Color, like simple process stack. So why compiles that code twice as fast when order is swapped?

As you can see these two code blocks do not depend directly on each other, both of them just modify the previously calculated Color, like simple process stack. So why compiles that code twice as fast when order is swapped?

Assuming that you didn't change color assignment, Part 3 [font=courier new,courier,monospace]"Color.rgb += (sum/contrib) - tex2D(s_image,uv);"[/font] will be override by Part 2 [font=courier new,courier,monospace]"Color = objreflect(Color, viewspace_P, uv, dist_factor, wind, WET_OBJMAXCOL, WET_OBJMAXREF);"[/font], so the compiler won't bother to compile part 3. Check the output of fxc and you will see that part3 was gone.

Again, the only way to have a faster compilation is to simplify your shader by replacing plain texture sampling with their respective grad/ ddx/ddy or calculate the mipmap levels yourself.

This topic is closed to new replies.

Advertisement