• entries
    422
  • comments
    1540
  • views
    488870

A quick tip...

Sign in to follow this  

112 views

I'll take a break from my coverage of the HDRI demo I'm working on. This little snippet is connected, but more general-purpose.

You can use Visual Studio to compile your pixel and vertex shaders

It started to annoy me that if I made some stupid syntactic error with one of my shaders that it would, for lack of a better term, explode my application at runtime.

So I looked into using the "Custom Build Step" feature in Visual Studio (iirc, this exists in all of the recent/future versions). When I had this working, any syntactic errors pop up as regular build errors in the IDE. As per usual, double clicking on the entry jumps to the relevant line in the HLSL source code.

  1. Import the pixel shader, vertex shader or effect files into your solution.

  2. Select one of the files, right click on it, select "Properties" from the menu

  3. In the first 'General' page, make sure "Excluded From Build" is set to No. Make sure the "Tool" is set to "Custom Build Tool"

  4. In the 'General' page found inside the 'Custom Build Step' folder you need to do the real work

  5. Set the "Command Line" to whatever is appropriate for how you wish to compile your shader. A neat trick is that you can use the %DXSDK_DIR% macro to get a relative rather than absolute path. I use the following example for the "Debug" configuration: "%DXSDK_DIR%Utilities\Bin\x86\fxc.exe" /Tps_2_0 /E"main" /Fc"$(OutDir)\[ps_2_0] FinalPass.html" /Cc "Shader Code\FinalPass.psh" /nologo /Zi and "%DXSDK_DIR%Utilities\Bin\x86\fxc.exe" /Tps_2_0 /E"main" /Fc"$(OutDir)\[ps_2_0] FinalPass.html" /Cc "Shader Code\FinalPass.psh" /nologo for the "Release" configuration.

  6. Next up, set a friendly message in the "Description" field - I use "Compiling 'FinalPass.psh' as ps_2_0"

  7. Finally, you need to specify the 'Outputs'. This one is important as VStudio tends to scream a lot if it's wrong [smile]. Basically you just need to list all of the files that you're expecting the "Command Line" to create: "$(OutDir)\[ps_2_0] FinalPass.html" is what I'm using.
  8. Now, go back to step #1 for the next file [oh]


So, if you now screw up your shaders you might see this in your build log:
------ Build started: Project: HDR Demo, Configuration: Debug Win32 ------

Compiling 'FinalPass.psh' as ps_2_0
Shader Code\FinalPass.psh(49): error X3333: Error occured between keyboard and chair.
compilation failed; no code produced
Project : error PRJ0019: A tool returned an error code from "Compiling 'FinalPass.psh' as ps_2_0"

Build Time 0:00
Build log was saved at "file://k:\Development\Projects\HDR Demo\Debug\BuildLog.htm"
HDR Demo - 2 error(s), 0 warning(s)


---------------------- Done ----------------------

Build: 0 succeeded, 1 failed, 0 skipped


I'm not using it fully, as my code also recompiles the files at load-time, but it is a fairly simple step to make sure you're not making any more stupid mistakes than you have to... [wink]
Sign in to follow this  


3 Comments


Recommended Comments

Guest Anonymous Poster

Posted

Nice one. Though I use glsl, it might be possible to set VS for this one too.. possibly using 3d labs compiler so that anything that is non-standard glsl is caught. Btw, what I use currently is my app has the ability to reload and rebuild all the shaders at runtime. So when developing a new shader, I just make it a simple lighting shader initially, which accepts all the uniforms and attributes from the application that are required for the final shader. Then while my app is running, I change the shader and I can immediately see the results of the incremental changes inside the application, I can check the console output for the compilation log if there are any errors. Works pretty well.

Share this comment


Link to comment
Guest Anonymous Poster

Posted

Thanks for the comments!

Quote:
what I use currently is my app has the ability to reload and rebuild all the shaders at runtime.

That, I suppose, would be one of the ideals of a good shader-based system.

I might be wrong, but I think I read somewhere that the Doom3 engine allows something close to this.

Quote:
I can check the console output for the compilation log if there are any errors. Works pretty well.

I was thinking about that - the last (?) param for the D3DX compiler used from the code will retrieve any text/errors that it generated. Perfect for streaming out to a log file [grin]

Jack

Share this comment


Link to comment

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