Monthly Archives: June 2011

Batch script to compile pixel shaders + mixing batch & C#

If you want to build a pixel shader for WPF there are some good examples and tools – some of which are:

  • NShader – that lets you edit HLSL code in VS with syntax highlighting
  • Shazzam – that helps edit and quickly test HLSL code + provides a few samples + generates WPF C#/VB wrapper classes automatically
  • Shader Effect BuildTask – that makes the shader build automatically when you build your project

The build task is a helpful thing, since it builds the effect every time you build your project, so your shader source code is treated the same way your C# source is but not really so helpful if you want to share your code with someone who does not necessarily have DirectX SDK or the build task installed – since it will break their build. That is why I usually include both the fx source code and the compiled shader in my VS projects – this also lets me dig up my old WPF projects and build them without the need for having DirectX SDK installed, since I write shaders less frequently than I set up new dev. machines.

Now, I do not want to remember the fxc.exe build commands or manually compile each shader, so I also include a batch script in the shaders folder that I can manually trigger when I modify and want to recompile a shader.

Since even minor changes in your shader files are error prone – I wanted to have a clear message in my script to so I know if my compiler worked without having to read all output. When I first wrote it I thought I could use colors – green for success and red for failure, but I found setting these on a per-line basis is not supported in regular command-line windows, so I just used full screen color changes “color 0A” for green and “color 0C” for red.

Today I wanted to play with shaders again and as I looked at that script I remembered I found some interesting code here (copied from here) one day, that allows you to have a single file with batch script and C# code. I found a few lines of C# code on StackOverflow that let you change the colors in the console window and here is what I have:

Note 1: This script requires DirectX SDK to be installed.

Note 2: This would probably be easier in PowerShell and I might port it one day, but it was just more interesting to me to do it this hacky way. + I don’t really know PowerShell or if it is available everywhere yet.

/* >NUL
@REM Open the containing folder in Windows Explorer and double click this file to compile shader
CLS
@CALL :COMPILE_CECHO %0
@CALL :COMPILE_FX HueEffect.fx HueEffect.ps
@DEL /q cecho.exe
@GOTO :EOF

*/
using System;
using System.Linq;

class cecho
{
    static void Main(string[] args)
    {
        try
        {
            Console.BackgroundColor = (ConsoleColor)Enum.Parse(typeof(ConsoleColor), args[0], true);
            Console.ForegroundColor = (ConsoleColor)Enum.Parse(typeof(ConsoleColor), args[1], true);
            Console.WriteLine(string.Join(" ", args.Skip(2)));
            Console.ResetColor();
        }
        catch
        {
            Console.WriteLine("Usage: cecho background foreground text\r\nColors:");
            foreach (var name in Enum.GetNames(typeof(ConsoleColor)))
                Console.WriteLine(name + @": " + (int)Enum.Parse(typeof(ConsoleColor), name));
            return;
        }
    }
}

/*
More batch code follows:

:COMPILE_CECHO
@SET WinDirNet=%WinDir%\Microsoft.NET\Framework
@IF EXIST "%WinDirNet%\v2.0.50727\csc.exe" set csc="%WinDirNet%\v2.0.50727\csc.exe"
@IF EXIST "%WinDirNet%\v3.5\csc.exe" set csc="%WinDirNet%\v3.5\csc.exe"
@IF EXIST "%WinDirNet%\v4.0.30319\csc.exe" set csc="%WinDirNet%\v4.0.30319\csc.exe"
@%csc% /nologo /out:"cecho.exe" %1
@GOTO :EOF

:COMPILE_FX
@SET fxc="%DXSDK_DIR%Utilities\bin\x86\fxc.exe"
@SET FXCPARAMS=/nologo /T ps_2_0 /E main /Fo%2 %1
@REM ECHO %FXC% %FXCPARAMS%
@%FXC% %fxcparams% 1> NUL
@IF [%ERRORLEVEL%] EQU [0] (cecho 0 10 %1 - COMPILATION SUCCESSFULL!) ELSE (cecho 0 12 %1 - ERROR! FIX SHADER!)
@GOTO :EOF
*/

Advertisements
Tagged , , , ,