• FEATURED

View more

View more

View more

### Image of the Day Submit

IOTD | Top Screenshots

### The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

# Speed up shader compilation (HLSL)

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

34 replies to this topic

### #21Meltac  Members

Posted 18 May 2012 - 02:14 PM

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.

Edited by Meltac, 18 May 2012 - 02:29 PM.

### #22Meltac  Members

Posted 20 May 2012 - 12:48 PM

Does anybody know something about what I was asking for? I would appreciate it!

### #23xoofx  Members

Posted 20 May 2012 - 03:44 PM

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?

### #24Meltac  Members

Posted 21 May 2012 - 06:15 AM

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.

### #25Xoliul  Members

Posted 22 May 2012 - 08:29 AM

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).

### #26Meltac  Members

Posted 22 May 2012 - 09:17 AM

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)?

### #27Xoliul  Members

Posted 22 May 2012 - 10:45 AM

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?

### #28Meltac  Members

Posted 22 May 2012 - 01:36 PM

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

Edited by Meltac, 22 May 2012 - 01:37 PM.

### #29Meltac  Members

Posted 25 May 2012 - 04:09 AM

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?

Edited by Meltac, 25 May 2012 - 08:57 AM.

### #30xoofx  Members

Posted 25 May 2012 - 04:28 PM

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 "Color.rgb += (sum/contrib) - tex2D(s_image,uv);" will be override by Part 2 "Color = objreflect(Color, viewspace_P, uv, dist_factor, wind, WET_OBJMAXCOL, WET_OBJMAXREF);", 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.

Edited by xoofx, 26 May 2012 - 06:49 AM.

### #31Meltac  Members

Posted 27 May 2012 - 04:13 AM

Sorry, I don't get you. In Part 2, the previously processed "Color" is passed in as a function parameter "Color", so this shouldn't override anything, but just takes the Color as input and returns a modified Color as output. So why should the compiler omit the previous statements? They are significant to get the final result, and it would be misbehavior of the compiler to exclude them.

Of course, I still have to figure out how to use the grad/ ddx/ddy texture functions to produce the correct result.

However, another discovery I made that the described compilation time differences are only so huge when using fxc. When triggering compilation instead through the d3dx9 library the difference is only about one seconds (instead of 3), but compilation seems to take more time overall in that case (up to ten seconds).

What brings me to another question: Might it be that the default behavior of the compiler is different when using fxc.exe or d3dx9.dll or d3dcompiler.dll directly? Or, in other words, do I need to specify some special compiler options such as shader profiles or switches (in the case of fxc) or function arguments (in the case of triggering compilation through one of the dll's) in order to get the exact same result?

Otherwise I see no reason for these obvious compilation differences...

### #32xoofx  Members

Posted 27 May 2012 - 07:25 AM

oops, my apologize, I missed the Color passed by argument! Better look at the asm why it is optimizing faster. But anyway, I would quickly try to replace all tex2D by tex2Dlod and check how much It will reduce the compilation time. Also, if you are sampling a previous render target/backbuffer to apply some posteffects and that there is no mipmap, it is safe to use tex2Dlod on the mipmap 0 (t.w = 0).

fxc is using both d3dx9_43.dll and D3DCOMPILER_43.dll internally. Do you know the settings STALKER is using to compile your shaders? What profile are you using to compile your shaders? If you are using the same, you shouldn't have any differences.

### #33Meltac  Members

Posted 29 May 2012 - 02:32 AM

Wow, I love tex2Dlod! Now THAT gave the thing a real boost! Thanks for the hint! Btw, does it speed up runtime as well or only compilation?

Now the next step would be find a way to make compilation of loops faster. Here making the [loop] attribute working would be best, don't know why it's not.

I don't know what settings STALKER is using to compile its shaders, nor do I have a clue how to figure that out (there's not much dev-internal information public available). All I know is that the game includes d3dx9_34.dll (not d3dx9_43.dll) which in turn triggers d3dcompiler.dll (don't remember the version) to compile the shaders. I can see the use of the method D3DXCompileShader in the dissambly but don't see what parameters actually are passed. All I know is they are compiling against vs_3_0 and ps_3_0.

Using the latest DX9 binaries (d3dx9_43.dll and the respective d3dcompiler.dll) doesn't seem to change a thing - although I can see the replaced new dll's are being used by the game engine, that doesn't speed up anything nor provide access to newer features such as attributes, strangely. Would enabling attributes require to call some other compiler method instead, or some special settings or something?

Edited by Meltac, 29 May 2012 - 02:32 AM.

### #34pcmaster  Members

Posted 30 May 2012 - 07:09 AM

tex2Dlod doesn't need to compute the lod (mip-map) level, you pass it in manually, which means that no screen partial derivatives are needed. Things are then easier for both the compiler and run-time. While reading your questions, I gained the impression you needed to sample correct (automatic) mip-map levels, you wouldn't be able to use this instruction otherwise. If you can, use it pretty much "everywhere" :-)

Edited by pcmaster, 30 May 2012 - 07:09 AM.

### #35Meltac  Members

Posted 30 May 2012 - 02:26 PM

I didn't have an idea like that before, but since xoofx noted that sampling from a backbuffer wouldn't require tex2D with mipmaps anyway I guess this is what saved my day.

Thanks again everbody!

Edited by Meltac, 30 May 2012 - 02:29 PM.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.