Sign in to follow this  

Speed up shader compilation (HLSL)

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

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

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
[quote name='Xoliul' timestamp='1337705111' post='4942252']
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.[/quote]

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.

[quote name='Xoliul' timestamp='1337705111' post='4942252']
BTW, I'm curious what you are doing exactly in your shader that is so heavy and improves the quality so drastically?
[/quote]

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

Share this post


Link to post
Share on other sites
[quote name='Meltac' timestamp='1337372080' post='4941268']
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?
[/quote]

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):
[CODE]
// 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
[/CODE]

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

Share this post


Link to post
Share on other sites
[quote name='Meltac' timestamp='1337940590' post='4943190']
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?
[/quote]
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. Edited by xoofx

Share this post


Link to post
Share on other sites
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...

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Sign in to follow this