Neatware Header
Home   Products   Forums   Partners   Buy   Company   Contact   Blog   

2. Framework

Below section described the mCL shader framework. At the beginning, package require mcl loads the latest mcl engine. The inline command with a name specifies the source code of vertex or pixel shader. The class command defines a class with a name called shader. variable specifies a variable used in the class scope. constructor with parameters defines the initial commands during a creation of the class. Create, Run, and Release are defined methods of the class. The main is the starting of the execution of a shader.

  package require mcl

  inline VertexShader 	{ ... }
  inline PixelShader 	{ ... }

  class shader {
    variable app
    variable vt
    variable pl

    constructor {args} 	{
	set app [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

To write a mCL shader it is very important to understand the use of the three pairs of callback functions. They are InitStartup and FinalCleanup, InitDeviceObjects and DeleteDeviceObjects, RestoreDeviceObjects and InvalidateDeviceObjects.

The InitStartup and FinalCleanup pair is used to load and delete device independent data. They are called once when start and end a shader application.

The InitDeviceObjects and DeleteDeviceObjects pair creates and destroys vertex buffer, index buffer, and textures. They will be called when the device is changed.

The RestoreDeviceObjects and InvalidateDeviceObjects pair responses to the change 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 and Render. Maximize window will not execute following functions.

  • InvalidateDeviceObjects
  • RestoreDeviceObjects

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. Then the execution of following procedure allows you to select a new file.

  • 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 the debugger in Ladybug Studio XP. 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.