Archive for the ‘dynamically generated code’ tag

Material System Design

without comments

Where I’m heading with the LxEngine material system design…


MaterialType will be composed of:
- a unique string name
- a vertex, geometry, frag, etc. shader set
- an optional JSON list of default named parameter values for some or all of the attributes & uniforms in the shaders

MaterialInstance will be composed of:
- an optional unique string name
- a string name identifying the MaterialType to use
- a JSON list of named parameter values


The overall approach is to allow an arbitrary shader chain to be compiled into a program. The code will then analyze the list of uniforms and attributes to automatically set the values for the MaterialInstance when it is activated.

The set of actions necessary to setup a particular shader is handled by “compiling” at std::vector> of instructions.

On the first use of a MaterialType:
- the material type will be compiled into a GLSL program (nothing special)

On the first use of a MaterialInstance:
- query the MaterialType’s list of uniforms and attributes
- for each parameter…
- if the parameter list specifies a value for that parameter
- ..if a direct value is specified and the types match
- ….generate a std::function<> to set that value
- ..if a indirect value is specified
- ….generate a std::function<> to set the value from the current context
- else
- …check the above in the material default parameters
- else check if it is a “standard” name (e.g. unifProjectionMatrix)
- …generate a std::function<> to set the context default
- else
- …report an error that a parameter cannot be set for the material instance

The above may involve pulling data from the material type, the material instance, the geometry, etc.

On use of the MaterialInstance:
- Call each std::function<> generated on the first use

This basically amounts to dynamically generated code. The std::function<> incurs some overhead, but it’s very easy to use and the code is relatively elegant.

Written by arthur

March 8th, 2012 at 11:10 am