I would like to open a discussion about a declarativ shading language for OpenGL I am working on now. Basically, I would like to hear some suggestions (and critic) that may help me in developing the language.
First of all, why am I doing it? I always wanted to write an own compiler - this is a nive opportunity. Secunde, I search for easier and more intuitive ways of programming the shading hardware.
Basically, the SL is a functional language, where variables and functions are defined only once (no rewrite possible). Also, there is no distinction between vertex and fragment shaders. The compiler will determine which expression has to be computed in which stage and setup varying variables accordingly. There is no dynamic branching or looping, but we can use list operations. The type system and standart library are similar to ones of GLSL.
Each type o fthe language is a list, so we can use list operations (sum, mul, anything else?) on everything. List components are accessed using integer index where list identifier is used just like a function. When calling functions with just one argument, the parenthesis may be emited. To access x component of a vector v we can use v.x, v 0 or v(0)
Here is a sample code that illustrates basic points:
# vertex position attribute vec4 position # texture coordinate attribute vec2 tex # bone weight attribute vec4 weight # array of bone matrices uniform mat4 bonemat uniform tex2D base # do we need to apply the skin? const boolean applySkin = true # transformed vertex position # compiler will determine the # variable type based on the expression(here vec4) # also illustrates blocks out vpos: # skinned skinned_vpos = sum([weight i * position * bonemat i for i in range(0, 3)]) # not skinned normal_vpos = position * bonemat 0 # evaluate vpos return if applySkin skinned_vpos else normal_vpos # texture lookup component(just to illustrate functions) texLookup(coord as vec2) = tex2D(base, coord) # fragment color out color = texLookup tex
Also, there is an idea of incapsulating computation into different “objects” and binding them together in the last phase, but I haven’t had much thoughts about it. Basically, it would be something like:
object model1 var1 = ... var2 = ... end object model2 requires model1 var3 = model1.var1 + ... end object model3 extends model2 var3 = ... end out vpos = if modelblabla model1.var1 else model3.var3 color = model2.var2 end
but i don’t know if it can be usefull at all :-/