Shader authoring/Quick Start: Difference between revisions
No edit summary |
No edit summary |
||
Line 9: | Line 9: | ||
* [[HLSL]] code is compiled into files that are put under a mod directory's <code>shaders</code> directory. The specifics of HLSL are described in the [http://msdn.microsoft.com/library/default.asp?url=/library/en-us/directx9_c/directx/graphics/programmingguide/hlslshaders/programmablehlslshaders.asp DirectX docs here]. | * [[HLSL]] code is compiled into files that are put under a mod directory's <code>shaders</code> directory. The specifics of HLSL are described in the [http://msdn.microsoft.com/library/default.asp?url=/library/en-us/directx9_c/directx/graphics/programmingguide/hlslshaders/programmablehlslshaders.asp DirectX docs here]. | ||
* The C++ code is compiled into a "shader DLL" that fits the pattern | * The C++ code is compiled into a "shader DLL" that fits the pattern <code>game_shader_dx*.dll</code> or <code>game_shader_generic*.dll</code>. The shader DLL goes in the mod's bin directory (the same place where the client.dll and server.dll goes). The C++ code describes the high-level operation of the shader to the Source Engine. It tells the engine things like: | ||
** Which textures the shader wants to use (texture names usually come from the .VMT [material] file). | ** Which textures the shader wants to use (texture names usually come from the .VMT [material] file). | ||
** How many rendering passes the shader will use. | ** How many rendering passes the shader will use. | ||
Line 20: | Line 20: | ||
=Quick Start - Sample Shaders= | =Quick Start - Sample Shaders= | ||
{{note|As of | {{note|As of the Source SDK update on 9/14/2006 the sdkshaders that were shipped previously have been replaced by a subset of our production shaders located in the <code>src\materialsystem\stdshaders</code> directory under the mod's root dir.}} | ||
The Source SDK ships with some | The Source SDK ships with some of our production shaders that can be copied or modified to use in your own mod. To compile them and use them in a game read the following instructions: | ||
'''1.''' Install a copy of the source code by running the '''Create a Mod '''from the '''SDK launcher'''. If the latest version of the source code is already installed, this step can be skipped. | '''1.''' Install a copy of the source code by running the '''Create a Mod '''from the '''SDK launcher'''. If the latest version of the source code is already installed, this step can be skipped. | ||
{{note|In this document, we refer to the mod's source directory as <code>C:\MyMod\src</code> (replace | {{note|In this document, we refer to the mod's source directory as <code>C:\MyMod\src</code> and the mod's game directory as <code>C:\Steam\steamapps\SourceMods\MyMod</code> (replace them as appropriate).}} | ||
'''2.''' Open a Windows command prompt (enter <code>cmd</code> in the start menu 'run' dialogue, change to the <code>C:\MyMod\src\ | '''2.''' Open a Windows command prompt (enter <code>cmd</code> in the start menu 'run' dialogue, change to the <code>C:\MyMod\src\materialsystem\stdshaders</code> directory with the '''cd''' command. Then type: | ||
<pre> | <pre>buildsdkshaders.bat stdshader_dx8 -game C:\Steam\steamapps\SourceMods\MyMod -source C:\MyMod\src</pre> | ||
This will compile the sample HLSL code and copy the resulting files into a directory called shaders in the specified mod directory. | This will compile the DirectX 8 sample HLSL code and copy the resulting files into a directory called shaders in the specified mod game directory. | ||
'''3.''' | '''3.''' If you wish to build the example DirectX 9 shaders type the following from a command window: | ||
<pre>buildsdkshaders.bat stdshader_dx9 -game C:\Steam\steamapps\SourceMods\MyMod -source C:\MyMod\src</pre> | |||
This will compile the DirectX 9 sample HLSL code and copy the resulting files into a directory called shaders in the specified mod game directory. | |||
'''4.''' | '''4.''' Open <code>stdshader_dx8.vcproj</code> in Visual Studio 2003 and build it. This will build the C++ part of each shader into a DLL and it will automatically copy the DLL into the game's bin directory (where the client.dll and server.dll are stored). {{note|If this is the first time that you are building this project you will need to delete the 'd3dx9.lib' library from the project and add it from the proper location depending on the location of your DirectX SDK installation. This is an annoying step, but we decided it would be best not to distribute DirectX binaries anymore since they are freely available and may be updated independent of the Source SDK.}} | ||
Build the solution and verify that game_shader_dx8.dll is now in your game's bin directory. | |||
" | '''5.''' If you wish to build a DirectX 9 shader DLL open <code>stdshader_dx9.vcproj</code> in Visual Studio 2003 and build it. This will build the C++ part of each shader into a DLL and it will automatically copy the DLL into the game's bin directory (where the client.dll and server.dll are stored). {{note|If this is the first time that you are building this project you will need to delete the 'd3dx9.lib' and 'd3d9.lib' libraries from the project and add it from the proper location depending on the location of your DirectX SDK installation. }} | ||
Build the solution and verify that game_shader_dx9.dll is now in your game's bin directory. | |||
'''6.''' Now modify a material (.VMT) file to use the new shaders at the top. Something like the following: | |||
"SDK_UnlitGeneric" | |||
{ | { | ||
"$basetexture" "Brick/brickwall003a" | "$basetexture" "Brick/brickwall003a" | ||
Line 41: | Line 49: | ||
} | } | ||
''' | '''7.''' The SDK includes some example files that can be copied into a mod to see how to refer to the sample shaders that were just compiled. The relevant sample files are listed below: | ||
{| | {| |
Revision as of 17:02, 13 September 2006
High Level Concepts
The two major pieces of code that have to be written to create a new shader are the HLSL code and the C++ code:
- HLSL code is compiled into files that are put under a mod directory's
shaders
directory. The specifics of HLSL are described in the DirectX docs here. - The C++ code is compiled into a "shader DLL" that fits the pattern
game_shader_dx*.dll
orgame_shader_generic*.dll
. The shader DLL goes in the mod's bin directory (the same place where the client.dll and server.dll goes). The C++ code describes the high-level operation of the shader to the Source Engine. It tells the engine things like:- Which textures the shader wants to use (texture names usually come from the .VMT [material] file).
- How many rendering passes the shader will use.
- Which HLSL code the shader will use.
- Which parameters to pass into the HLSL code from the material file.
- What shader to fallback to if the user's system can't support the shader.
Every shader has one C++ class and one or more HLSL files that it uses to render.
Quick Start - Sample Shaders

src\materialsystem\stdshaders
directory under the mod's root dir.The Source SDK ships with some of our production shaders that can be copied or modified to use in your own mod. To compile them and use them in a game read the following instructions:
1. Install a copy of the source code by running the Create a Mod from the SDK launcher. If the latest version of the source code is already installed, this step can be skipped.

C:\MyMod\src
and the mod's game directory as C:\Steam\steamapps\SourceMods\MyMod
(replace them as appropriate).2. Open a Windows command prompt (enter cmd
in the start menu 'run' dialogue, change to the C:\MyMod\src\materialsystem\stdshaders
directory with the cd command. Then type:
buildsdkshaders.bat stdshader_dx8 -game C:\Steam\steamapps\SourceMods\MyMod -source C:\MyMod\src
This will compile the DirectX 8 sample HLSL code and copy the resulting files into a directory called shaders in the specified mod game directory.
3. If you wish to build the example DirectX 9 shaders type the following from a command window:
buildsdkshaders.bat stdshader_dx9 -game C:\Steam\steamapps\SourceMods\MyMod -source C:\MyMod\src
This will compile the DirectX 9 sample HLSL code and copy the resulting files into a directory called shaders in the specified mod game directory.
4. Open stdshader_dx8.vcproj
in Visual Studio 2003 and build it. This will build the C++ part of each shader into a DLL and it will automatically copy the DLL into the game's bin directory (where the client.dll and server.dll are stored).

Build the solution and verify that game_shader_dx8.dll is now in your game's bin directory.
5. If you wish to build a DirectX 9 shader DLL open stdshader_dx9.vcproj
in Visual Studio 2003 and build it. This will build the C++ part of each shader into a DLL and it will automatically copy the DLL into the game's bin directory (where the client.dll and server.dll are stored).

Build the solution and verify that game_shader_dx9.dll is now in your game's bin directory.
6. Now modify a material (.VMT) file to use the new shaders at the top. Something like the following:
"SDK_UnlitGeneric" { "$basetexture" "Brick/brickwall003a" "$surfaceprop" "brick" }
7. The SDK includes some example files that can be copied into a mod to see how to refer to the sample shaders that were just compiled. The relevant sample files are listed below:
Filename | Use |
---|---|
[steam]\half-life 2\hl2\maps\sdk_shader_samples.bsp | Small map that refers to the shaders |
[steam]\sourcesdk_content\hl2\mapsrc\sdk_shader_samples.vmf | Map source |
[steam]\half-life 2\hl2\materials\sdk\sdk_lightmap.vmt | Material sample 1 |
[steam]\half-life 2\hl2\materials\sdk\sdk_particle.vmt | Material sample 2 |
These files are automatically installed by the SDK, but they will need to be copied into the same relative locations in the mod's own folder. For example, if Steam was installed in C:\Program Files\Valve\Steam
, and the mod is called MyMod, the files would have to be copied like this:
C:\Program Files\Valve\Steam\steamapps\username\half-life 2\hl2\materials\sdk\sdk_lightmap.vmt
into
C:\Program Files\Valve\Steam\steamapps\SourceMods\MyMod\materials\sdk\sdk_lightmap.vmt
