TSE/Shaders

From TDN

This page is a Work In Progress.

Introduction

Shader support is handled two different ways in the TGEA. The first way is for the engine itself to procedurally generate shaders from base Material properties. The second way is for programmers to specify their own shaders by using CustomMaterials and ShaderData structures.

Contents


Procedural Shader Generation

Procedural shaders are output into human readable HLSL form in the example/shaders directory. The vertex and pixel shaders have the respective shaderVxxx.hlsl and shaderPxxx.hlsl filenames where xxx is an incremental number. Each number is the same for matching vertex and pixel shaders, so shaderV010.hlsl and shaderP010.hlsl are the complete shader pair.

The procedurally generated shaders are created and compiled at run time every time the engine starts. This will likely change in the future such that they are cached. The shaders are generated specifically for whatever hardware they are running on. As a result, the number and length of the shader files will vary depending on the video card being used.

The engine can be set so new shaders are not generated. This is useful for debugging and modifying procedural shaders.

Specifying Hand Written Shaders

To use hand written shaders, a ShaderData datablock must be used. This datablock refers only to the vertex and pixel shader filenames and a hardware target level. Shaders are API specific, so DirectX and OpenGL shaders must be explicitly identified. At the time of this writing, only Direct3D is implemented so only the following data is relevant:

DXVertexShaderFile

Indicates a filename that contains a DirectX vertex shader program. It must contain only one program and no pixel shader, just the vertex shader. It can be either an HLSL or assembly level shader. The former must have a filename extension of .hlsl, otherwise it assumes it is an assembly file.

DXPixelShaderFile

Indicates a filename that contains a DirectX pixel shader program. It must contain only one program and no vertex shader, just the pixel shader. It can be either an HLSL or assembly level shader. The former must have a filename extension of .hlsl, otherwise it assumes it is an assembly file.

pixVersion

This indicates what target level of shader should be compiled. Valid numbers at the time of this writing are 1.1, 1.4, 2.0, and 3.0. The shader will not run properly if your hardware does not support the level of shader you have compiled.

Sample ShaderData

Here is a sample ShaderData datablock. This is used in conjuction with CustomMaterials. Multiple CustomMaterials can reference the same ShaderData structure. See shaders.cs for more examples.

datablock ShaderData( BumpCubemap )
 {
    DXVertexShaderFile 	= "shaders/bumpCubeV.hlsl";
    DXPixelShaderFile 	= "shaders/bumpCubeP.hlsl";
    pixVersion = 1.1;
 };