Neatware Company

An ISV for Mobile, Cloud, and Video Technologies and Software.

Shader Framework

We start to describe MCL Shader Framework. The [ ... ] represents commands in the following description.

At the beginning, [package require mcl] loads the latest mcl package. The [MCL::procedure ... ] command specifies the source code of vertex or pixel shader described by a shading language such as Cg. Where "MCL" is the namespace of mcl package, "procedure" is a function definition in MCL, and :: symbol links them together. The [proc main] specifies the main function. main starts the execution of the program and "exit" terminates the execution finally.

The class command defines a class with a name shader. variable specifies a variable used in the class scope. constructor defines the initial commands during a creation of the class. Create, Run, and Release are basic methods of the class. Other methods are callback methods.

  package require mcl

  MCL::procedure VertexShader { ... }
  MCL::procedure PixelShader { ... }

  class shader {
    variable app
    variable sh
    variable vt

    constructor {args} {
      set x 3.2348998
      set app [MCL::application]
      ...
    }

    method Create {} {$app create $this}
    method Run {} {$app run}
    method Release {} {$app release; delete object $this}

    method InitStartup {} { ... }
    method InitDeviceObjects {} { ... }
    method RestoreDeviceObjects {} { ... }

    method FrameMove {} { ... }
    method Render {} { ... }

    method InvalidateDeviceObjects {} { ... }
    method DeleteDeviceObjects {} { ... }
    method FinalCleanup {} { ... }
  }

  proc main {} {
    set obj [shader name]
    $obj Create 
    $obj Run
    $obj Release
  }

  main
  exit

InitStartup : FinalCleanup, InitDeviceObjects : DeleteDeviceObjects, and RestoreDeviceObjects : InvalidateDeviceObjects are paired callback functions. The IniStartup : FinalCleanup pair is used to load and delete device independent data. They are called once when start and end a shader application. The InitDeviceObjects : DeleteDeviceObjects pair creates and destroys vertex buffer, index buffer, and textures. They will be called when the device is changed. The RestoreDeviceObjects : InvalidateDeviceObjects pair responses to the changes of the window size. So the vertex and pixel shaders are built and released here. And you may set the projection matrix here as well.

The application starts execution from main. It first creates an app object, then [$app create] builds a window and initializes objects in the following order:

  • InitStartup
  • InitDeviceObjects
  • RestoreDeviceObjects

[$app run] makes the application enter the following message loop

  • FrameMove
  • Render

When you resize the window, following functions are called in sequence and reenter the message loop of FrameMove. Maximize window will not execute following functions.

  • InvalidateDeviceObjects
  • RestoreDeviceObjects

To restart application you can press F4 key, appplication will execute in the following sequence. For example, you may use the [$app dialog file *.jpg] command in the file input argument, so you can select a new file from the File Open dialog.

  • InvalidateDeviceObjects()
  • DeleteDeviceObjects()
  • InitDeviceObjects()
  • RestoreDeviceObjects()

When you click the exit button on the window's title bar, the application will execute following functions and exit to leave.

  • InvalidateDeviceObjects()
  • DeleteDeviceObjects()
  • FinalCleanup()

To understand the framework well, you can try to run a sample with debugger. By setting the breakpoints with mouse and using F6 step-in as well as F7 step-over, you can watch the execution step by step.