Lesson 3 - Const

From Bo3b's School for Shaderhackers
Revision as of 00:32, 1 September 2014 by Bo3b admin (Talk | contribs)

Jump to: navigation, search

Summary

This lesson will show how to create and use constants, from DX9Settings.ini all the way into the shader code itself.


Level of difficulty: Easy
Time required: 30 minutes

Video Walkthrough on YouTube

Objective

Learn how constants are defined and used, including possible problems and workarounds.
Learn how HelixMod constants work to make an on/off mechanism from the keyboard.

Quiz

Make a version that uses a different key, changes the color when on, and is only active when held down.


For this Lesson, let's take a quick look at another HelixMod feature that we are going to use extensively in the future. Our use here will be a simple version, but a good introduction.

In the HelixMod DX9Settings.ini file, we can add Constants, and we can then use those constants in any Pixel or Vertex Shader. They are provided by the tool as a convenience, and for any purpose that we wish. The constants can be used to make it easy to change the HUD depth for example, or specify how transparent to make it.

These constants can also be chosen based on a given key or mouse press, so that we can change effects in the shader, like changing viewpoints on key press, or changing convergence to zero when the right mouse is down.

Note that this version of constants is for the latest HelixMod DLL, and that the syntax changed from the older versions. The original documentation is thus misleading for the newer DLLs.


For our example here, we'll make a simple on/off mechanism, where we can decide in each shader whether to show the original shader, or our disabled version. Then by a keypress, Numpad 0 by default, we can toggle it from broken to disabled.

There is no particular advantage to do this, other than it's interesting and makes a great demo, but it can make it easier to decide whether something needs to be fixed or if disabled is good enough.


  • Create an on/off mechanism using HelixMod constants
    1. Edit the DX9Settings.ini file and set DumpAll=false for speed.
    2. Look at the DX9Settings.ini that we'll now use, at Default_DX9Settings.ini
    3. Copy and paste that default .ini into our Ball DX9Settings.ini.
    4. Open the A977E84A.txt shader where we'll see how to use the constants we just made.
    5. In the def section, add a new constant as:[1]
      //def c220, Const1, Const2, Const3, Const4
      def c200, 0, 1, 0.0625, 0 // x=0 for comparison to Const1
    6. At the bottom, change our mov oC0 code to be the if statement:[2]
      // if Const1 = 0 disable effect, else leave it on
      mov r30.x, c220.x
      if_eq r30.x, c200.x
      mov oC0.xyzw, c200.wwww
      endif
    7. Run The Ball and see if it works.



With an understanding of how the constants work, it's time to demonstrate that you can alter this technique to suit your needs.

This quiz will be harder than the previous ones, as not every step will be detailed, and you will be expected to apply what you've learned.


For this Quiz, you'll need to change 3 characteristics of the toggle mechanism, then upload your example code and a screen snapshot.


  • Modify the toggle code, then upload your changes.
    1. Change the key used to toggle the effect from Numpad 0 to any key of your choice.
    2. Change the mechanism to not toggle the effect, but only be active when held down.
    3. When the effect is being displayed, change its color.
    4. With those all working, go to Special:Upload and upload a screen shot.
    5. Go to your personal page and add a new section for == Lesson 3 ==
    6. Add your code changes into a text section bracketed with <nowiki></nowiki>
    7. Add a link to your screenshot showing an alternate color with [[File:filename|700px]]



Were you able to navigate the DX9Settings.ini and shader code to change the behavior? Excellent! We nearly always use constants of some form in game fixes.