Creating Shaders

User Shaders

CTR vertex shaders can be programmed by users.
Using 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 in $(NW4C_ROOT)\SampleData\Graphics\Shader\UserShader or $(NW4C_ROOT)\SampleData\Graphics\Shader\MultiplyShader.
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 to customize and the header file from the $(NW4C_ROOT)\sources\shaders\gfx\DefaultShader directory. Now, copy the files in the following table and rename them as shown.
    Files to CopyRename As
    gfx_DefaultShader0.vshgfx_UserShader.vsh
    TransformMatrix.vshUserTransformMatrix.vsh
    CalcColor.vshUserCalcColor.vsh
    ShaderConfig.hDo not change (must be copied for each shader)
  2. Change the Shader Register List in ShaderConfig.h.
    First, eliminate two vertex lights (Register 12).
    Change the line define new_user_register_start from a value of c60 to a value of c48.
    Next, add register c22 to extension_deform and c74 to extension_color.

  3. Define symbols for the registers to add in the gfx_UserShader.vsh file.
    Now, define #pragma bind_symbol(NDeform, c22, c22) as a register for extending a vertex in the normal vector direction, and add #pragma bind_symbol(ExColor, c74, c74) as a register for adjusting the output color. You can also reduce the number of vertex lights to make the universal register region available for users.
    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.
    In this tutorial, we have revised UserTransformMatrix.vsh and UserCalcColor.vsh.

  5. Execute the following commands to convert the shader created into a shader binary.
    In this tutorial, files that are not customized are referenced from environmental variables.
    The order in which ctr_VertexShaderLinker32 links shaders is the same as when shader types are assigned when creating the shader intermediate file.

    $(CTRSDK_ROOT)\tools\CommandLineTools\ctr_VertexShaderAssembler32.exe gfx_UserShader.vsh -I$(NW4C_ROOT)\sources\shaders\gfx
    $(CTRSDK_ROOT)\tools\CommandLineTools\ctr_VertexShaderAssembler32.exe UserTransformMatrix.vsh -I$(NW4C_ROOT)\sources\shaders\gfx
    $(CTRSDK_ROOT)\tools\CommandLineTools\ctr_VertexShaderAssembler32.exe UserCalcColor.vsh -I$(NW4C_ROOT)\sources\shaders\gfx
    $(CTRSDK_ROOT)\tools\CommandLineTools\ctr_VertexShaderAssembler32.exe $(NW4C_ROOT)\sources\shaders\gfx\DefaultShader\CalcTexcoord0.vsh -I$(NW4C_ROOT)\sources\shaders\gfx
    $(CTRSDK_ROOT)\tools\CommandLineTools\ctr_VertexShaderAssembler32.exe $(NW4C_ROOT)\sources\shaders\gfx\DefaultShader\CalcTexcoord1.vsh -I$(NW4C_ROOT)\sources\shaders\gfx
    $(CTRSDK_ROOT)\tools\CommandLineTools\ctr_VertexShaderAssembler32.exe $(NW4C_ROOT)\sources\shaders\gfx\DefaultShader\CalcTexcoord2.vsh -I$(NW4C_ROOT)\sources\shaders\gfx
    $(CTRSDK_ROOT)\tools\CommandLineTools\ctr_VertexShaderAssembler32.exe $(NW4C_ROOT)\sources\shaders\gfx\DefaultShader\CalcQuaternion.vsh -I$(NW4C_ROOT)\sources\shaders\gfx
    $(CTRSDK_ROOT)\tools\CommandLineTools\ctr_VertexShaderAssembler32.exe $(NW4C_ROOT)\sources\shaders\gfx\DefaultShader\GetTexcoordSource.vsh -I$(NW4C_ROOT)\sources\shaders\gfx
    $(CTRSDK_ROOT)\tools\CommandLineTools\ctr_VertexShaderAssembler32.exe $(NW4C_ROOT)\sources\shaders\gfx\DefaultShader\CalcVertexLighting.vsh -I$(NW4C_ROOT)\sources\shaders\gfx
    $(CTRSDK_ROOT)\tools\CommandLineTools\ctr_VertexShaderAssembler32.exe $(NW4C_ROOT)\sources\shaders\gfx\DefaultShader\CalcHemisphereLighting.vsh -I$(NW4C_ROOT)\sources\shaders\gfx
    $(CTRSDK_ROOT)\tools\CommandLineTools\ctr_VertexShaderAssembler32.exe $(NW4C_ROOT)\sources\shaders\gfx\DefaultShader\GenTexcoordReflection.vsh -I$(NW4C_ROOT)\sources\shaders\gfx
    $(CTRSDK_ROOT)\tools\CommandLineTools\ctr_VertexShaderAssembler32.exe $(NW4C_ROOT)\sources\shaders\gfx\DefaultShader\GenTexcoordSphereReflection.vsh -I$(NW4C_ROOT)\sources\shaders\gfx

    $(CTRSDK_ROOT)\tools\CommandLineTools\ctr_VertexShaderLinker32.exe gfx_UserShader.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. Use CreativeStudio to create an intermediate file from UserShader.shbin.
    For information on how to create an intermediate file, refer to the Procedure for Switching Shaders.
  7. Bind the created intermediate file to a model and check using Viewer or another tool.
    The figure below shows the results of applying UserShader to Male.




CONFIDENTIAL