top of page

Add a per-material and a per-instance constant and add a generic builder program

  • itzvnodm
  • Sep 30, 2014
  • 3 min read
Purpose:

The assignment was to

  • Create and use Per-Material and Per-Instance constants in shader files.

  • Modify the build assets so that now it invokes generic builder executable with appropriate arguments to build assets.

Extra:

Before I talk about the actual assignment, I want to mention some of the architecting that has gone into this assignment.

  • Every object on screen is an actor controlled by actor controller.

  • There are three systems in the engine, RenderingSystem, WorldSystem and PhysicsSystem that share the created actors using shaderdpointers.

  • Update loop calls update on every system with delta time as input.

Regular requirements:
  • There is now a shared pointer of mesh class and a material class for every actor that the RenderingSystem stores for the lifetime of the actor.

  • Data for the mesh class is set in from Game project (Which should eventually be auto read from a Lua file).

  • Material class stores the path for shaders, compiled shaders, constant table and vector of MaterialConstantInfo.

  • MaterialConstantInfo class is a collection of constant name, constant handle, and default value.

  • Per-Material constants are parsed from the material file and set once at the initialization time.

  • Per-Instance constants are set by an exposed material function that takes input name and loops through all the stored Per-Instances and sets those if the data in valid.

Moving the rectangle around screen: (Arrow keys)

Arrow keys move the rectangle. Rectangle is frame rate dependent and moves smoothly around the screen with acceleration being set each frame the arrow keys are pressed. Additionally frictional force is being applied every frame to decelerate the rectangle. In order to move the rectangle internally the value of per-instance constant is being set with the actor’s position every frame before rendering it.

Per-Instance constants are the constants that are specific for each actor in the screen. At the time of vertex shader compilation the handle to this constant is stored. Each time before the render() call the value of this constant is set by the rendering system based off of the actor position, which in turn updates the position of the actor on screen.

PerInstance.PNG

Per-Material constants are overridden in material file, and are specific to each actor that use that material. Constant is set once at initialization time. Below is an example of g_colorModifier constant to set the color of an actor that uses this material which overrides the g_colorModifier from fragment shader file. This enables multiple actors to use the same fragment shader while setting different default colors.

Constants.PNG
PerMaterial.PNG

How to add constants to my material file:

Add a key-value pair under constants table in the lua material file. Key being a string and value being a table of floats.

Note: Value has to be a table even if it is just one value.

Program output:
Output1.PNG

g_colorModifier = { 0.0, 0.5, 1.0 }

Output2.PNG

g_colorModifier = { 0.0, 2.5, 1.0 }

Parts that gave me trouble: Pretty much everything that is related to integrating and debugging the old code from engineering one. I was trying to figure out what code is necessary and what is not. How the existing architecture has to be changed to make the solution to handle multiple objects on screen. As of writing this, I am guessing that the architecting will reduce the amount of work required for my next assignment by having separate mesh and material class shared among each actor who uses them.

Modifying the build asset pipeline:

Now the build asset flow is, BuildAsset project calls AssetBuilder.exe, which in turn calls BuildAsset function from BuildAsset.lua script file. This function invokes the GenericBuilder.exe process, which now does the processing of assets from authored asset directory to output game data directory. Eventually I will have different .exe for different types of assets (Example compile shaders using FXC-Effect-compiler tool in a Shaderbuilder.exe which takes .hlsl files as arguments).

Time Spent:

Extra: 4hrs

Adding timer and user input to move rectangle: 2hr

Architecting Mesh class and Material Class to use constants: 4hr

Lua generic constants reading: 2hr

Modifying the build asset pipeline: 1hr

Write-up: 1hr

Total: 14hr


 
 
 

Comentarios


Recent Posts
Archive
Search By Tags
bottom of page