If you want to help us maintaining this wiki, check out our discord server: https://discord.gg/3u69jMa 

Hardware Shaders

From SWRC Wiki
Jump to navigation Jump to search

Note: This is not a guide for the shading assembly language. You can find the available instructions and what they do in Microsoft's Asm Shader Reference.
RC can use vs 1.1 and ps 1.1 to ps 1.4 so make sure you use the correct instructions for the version you want to use.

Creating a new HardwareShader

A HardwareShader is a built-in material class and a new one can be created via the texture browser. Note that this might crash UnrealEd unless you have applied either of the following fixes:

  • Install the Republic Commando Fix
  • Edit the properties of the HardwareShader class
    1. Go to the Actor class browser and uncheck Use "Actor" as Parent? and Placeable classes Only?
    2. Go to Object -> Material -> RenderedMaterial -> HardwareShader and open its properties
    3. Insert the following for the VertexShaderText and PixelShaderText properties (Edit them via the ... button and hit Apply):
      • VertexShaderText:

        vs.1.1

        mov oPos, v0

      • PixelShaderText:

        ps.1.1

        mov r0, c0


Now you are ready to create a new shader object:

  • Go to the texture browser and select File -> New...
  • Fill in the Package, Group and Name fields
  • For the MaterialClass select HardwareShader from the list
    CreateShader.jpg
  • Click on New and the properties window for the new material will appear. (You can ignore the error at the bottom)
    NewShader.jpg
  • Depending on what kind of shader you're creating you might want to set ZTest and ZWrite to true as they're not set by default.

Writing Shader Code

Before you start editing the shader code, it is a good idea to open the Editor's log window as errors will appear there.

First you should add the vertex inputs you need to the StreamMapping array. Usually you want the vertex position at the very least so add a new entry and it will be set to FVF_Position by default. The dropdown here is hidden by the insert button so to change the value of the entry just double click it. You can reference those inputs with the v0, v1... registers in the vertex shader.
Other user-defined inputs are passed to the shader with the VSConstants and PSConstants properties. They are arrays where each element defines the type of the constant and four float values (X, Y, Z, W) that are used only if the type of the constant is EVC_MaterialDefined. These values can also be changed in UnrealScript or C++ code so the inputs can be made dynamic.
One important point to note is that all matrix constants (identifiable by their name) take up four slots. This means after a matrix constant you should leave three entries that are set to EVC_Unused. The reason is that each slot has four values but a 4x4 matrix needs 16.
Constants are referenced with c0, c1... in the shader code. There is also the def instruction to create constants in the shader source but that can't be used and will lead to compile errors since the constants are supplied via the arrays.

When editing shader code, it makes sense to place an object in the map and assign the shader as its skin so you can view the changes as you're editing. The shader updates each time you hit Apply in the text editor.
The following shader simply transforms the model to view space and draws it with a texture that is set to the first element of the Textures array in the shader properties:
ShaderProperties.jpg

  • VertexShaderText:
    vs.1.1
    
    m4x4 oPos, v0, c0  ;Multiply the vertex position with the transformation matrix and write it to oPos
    mov oT0, v1        ;Pass through the texture coordinate
  • PixelShaderText:
    ps.1.4
    
    texld r0, t0  ;Write texture 0 to r0 (t0 is the texture coordinate, the texture index is based on the destination register r0)