Creating Shaders

User Shaders

With CTR, vertex shaders can be programmed by users.
Under NW4C, some registers can be used by users to customize shaders. This type of shader is called a user shader. Refer to the sample data for a user shader in $(NW4C_ROOT)\SampleData\Graphics\Shader\UserShader.
For a list of registers available for use by users, see Shader Register List.
For information on switching shaders under CreativeStudio, see Procedure for Switching Shaders.

Shader Creation Tutorial

This section describes the procedure up to and including creating $(NW4C_ROOT)\SampleData\Graphics\Shader\UserShader.
UserShader adds a feature for extending vertex coordinates in the normal vector direction and a feature for adding a particular color to the output color.
The features are added by limiting the number of vertex lights and hemispherical light capability.

  1. Copy the default shader serving as the shader source from $(NW4C_ROOT)\sources\shaders\gfx\DefaultShader and the header file from $(NW4C_ROOT)\sources\shaders\gfx.
    Now, copy the files in the following table and rename them as shown.
    Files to copyRename as
    gfx_DefaultShader0.vshgfx_UserShader0.vsh
    gfx_DefaultShader1.vshgfx_UserShader1.vsh
    gfx_DefaultShader2.vshgfx_UserShader2.vsh
    gfx_DefaultShader3.vshgfx_UserShader3.vsh
    TransformMatrix.vshUserTransformMatrix.vsh
    CalcColor.vshUserCalcColor.vsh
    CalcHemisphereLighting.vshNo change
    CalcQuaternion.vshNo change
    CalcVertexLighting.vshNo change
    CalcTexcoord0.vshNo change
    CalcTexcoord1.vshNo change
    CalcTexcoord2.vshNo change
    GenTexcoordReflection.vshNo change
    GenTexcoordSphereReflection.vshNo change
    GetTexcoordSource.vshNo change
    VertexShader.hNo change
    ShaderConfig.hNo change
  2. Change the Shader Register List in ShaderConfig.h.
    First, illuminate two vertex lights (Register 12).
    On the line, define new_user_register_start, change c60 to c48.
    Next, for registers to be added, add c22 to extension_deform and c74 to extension_color.

  3. Define all register symbols to be added to gfx_UserShader0.vsh, gfx_UserShader1.vsh, gfx_UserShader2.vsh, and gfx_UserShader3.vsh.
    Now, define #pragma bind_symbol(NDeform, c22, c22) as a register for extending vertex coordinates in the normal vector direction, and add #pragma bind_symbol(ExColor, c74, c74) as a register for adjusting the output color. Also, users may use the universal register region to reduce the number of vertex lights.
    Change #pragma bind_symbol(UnivReg, c25, c84) to #pragma bind_symbol(UnivReg, c25, c73).

  4. Add a process that uses the registers that have been added.
    Here, we have revised UserTransformMatrix.vsh and UserCalcColor.vsh.

  5. Exectue the following command to convert the shader created into a shader binary.
    The order in which shaders are linked by ctr_VertexShaderLinker32 is the same as that when shader types are assigned when creating the shader intermediate file.

    $(CTRSDK_ROOT)\tools\CommandLineTools\ctr_VertexShaderAssembler32.exe gfx_UserShader0.vsh
    $(CTRSDK_ROOT)\tools\CommandLineTools\ctr_VertexShaderAssembler32.exe gfx_UserShader1.vsh
    $(CTRSDK_ROOT)\tools\CommandLineTools\ctr_VertexShaderAssembler32.exe gfx_UserShader2.vsh
    $(CTRSDK_ROOT)\tools\CommandLineTools\ctr_VertexShaderAssembler32.exe gfx_UserShader3.vsh
    $(CTRSDK_ROOT)\tools\CommandLineTools\ctr_VertexShaderAssembler32.exe UserTransformMatrix.vsh
    $(CTRSDK_ROOT)\tools\CommandLineTools\ctr_VertexShaderAssembler32.exe UserCalcColor.vsh
    $(CTRSDK_ROOT)\tools\CommandLineTools\ctr_VertexShaderAssembler32.exe CalcHemisphereLighting.vsh
    $(CTRSDK_ROOT)\tools\CommandLineTools\ctr_VertexShaderAssembler32.exe CalcQuaternion.vsh
    $(CTRSDK_ROOT)\tools\CommandLineTools\ctr_VertexShaderAssembler32.exe CalcVertexLighting.vsh
    $(CTRSDK_ROOT)\tools\CommandLineTools\ctr_VertexShaderAssembler32.exe CalcTexcoord0.vsh
    $(CTRSDK_ROOT)\tools\CommandLineTools\ctr_VertexShaderAssembler32.exe CalcTexcoord1.vsh
    $(CTRSDK_ROOT)\tools\CommandLineTools\ctr_VertexShaderAssembler32.exe CalcTexcoord2.vsh
    $(CTRSDK_ROOT)\tools\CommandLineTools\ctr_VertexShaderAssembler32.exe GenTexcoordReflection.vsh
    $(CTRSDK_ROOT)\tools\CommandLineTools\ctr_VertexShaderAssembler32.exe GenTexcoordSphereReflection.vsh
    $(CTRSDK_ROOT)\tools\CommandLineTools\ctr_VertexShaderAssembler32.exe GetTexcoordSource.vsh

    $(CTRSDK_ROOT)\tools\CommandLineTools\ctr_VertexShaderLinker32.exe gfx_UserShader0.obj gfx_UserShader1.obj gfx_UserShader2.obj gfx_UserShader3.obj UserTransformMatrix.obj UserCalcColor.obj CalcTexcoord0.obj CalcTexcoord1.obj CalcTexcoord2.obj CalcQuaternion.obj GetTexcoordSource.obj CalcHemisphereLighting.obj CalcVertexLighting.obj GenTexcoordReflection.obj GenTexcoordSphereReflection.obj -OUserShader.shbin

  6. Create an intermediate file from UserShader.shbin using CreativeStudio.
    For information on how to create an intermediate file, refer to the procedure for switching shaders.
  7. If you bind the created intermediate file to a model, check using gfxviewer.
    In the figure below, UserSharder has been applied to human_lite_walk.

    human_heavy_walk.png(41242 byte)


CONFIDENTIAL