In-depth: Shader generator

Aug. 3, 2012
In-depth: Shader generator

[In this reprinted #altdevblogaday opinion piece, game programer Simon Yeung describes how to generate vertex and pixel shader source codes for different render passes by defining a surface shader.] In the last few weeks, I was busy with rewriting my iPhone engine so that it can also run on the Windows platform (so that I can use Visual Studio in stead of Xcode~) and most importantly, I can play around with D3D11. During the rewrite, I want to improve the process of writing shaders so that I don't need to write similar shaders multiple times for each shader permutation (say, for each surface, I have to write a shader for static mesh, skinned mesh, instanced static mesh… multiplied by the number of render passes), and instead I can focus on coding how the surface would looks like. So I decided to write a shader generator that will generate those shaders, which is similar to the surface shader in Unity. I choose the surface shader approach instead of a graph based approach like Unreal Engine because being a programmer, I feel more comfortable (and faster) writing code than dragging tree nodes using the GUI. In the current implementation of the shader generator, it can only generate vertex and pixel shaders for the light pre pass renderer, which is the lighting model used before.

Defining the surface

To generate the target vertex and pixel shaders by the shader generator, we need to define how the surface looks like by writing surface shader. In my version of surface shader, I need to define three functions: vertex function, surface function and lighting function. The vertex function defines the vertex properties like position and texture coordinates.

  1. VTX_FUNC_OUTPUT vtxFunc(VTX_FUNC_INPUT input)

  2. {

  3. VTX_FUNC_OUTPUT output;

  4. output.position = mul( float4(input.position, 1), worldViewProj );

  5. output.normal = mul( worldInv, float4(input.normal, 0) ).xyz;

  6. output.uv0 = input.uv0;

  7. return output;

  8. }

The surface function describes how the surface looks like by defining the diffuse color of the surface, glossiness and the surface normal.

  1. SUF_FUNC_OUTPUT sufFunc(SUF_FUNC_INPUT input)

  2. {

  3. SUF_FUNC_OUTPUT output;

Tags:

No tags.

JikGuard.com, a high-tech security service provider focusing on game protection and anti-cheat, is committed to helping game companies solve the problem of cheats and hacks, and providing deeply integrated encryption protection solutions for games.

Explore Features>>