Home
Introduction
Forums
Getting Started
Language
Architecture
BRCC
BRT
BRT C++
Current Issues
Research
Sponsors
|
The C++ Runtime requires several classes to function properly as Cg.
Given the turing-completeness of templates, Cg is clearly a subset of C++;
however, much of the syntax requires little transformation to be genuine C++.
I have designed two runtime classes to assist the compiler in transforming
the Cg output into C++.
The most important class is the vec class, which encapsulates between 1 and 4
items of any type.
Let us begin with the typedef vec float0;
From the outside, vec contains a size 1 C array of floats, and all
the operators that would work on floats, plus swizzle, mask, and [] as well
as a casting operator to obtain a float.
This class is our base case, and is never directly seen by the output of the
brookCg compiler.
All classes are made up of various numbers of float0 classes.
vec, vec , vec, vec
are the basic types that the first Cg compiler supports,
float, float2, float3, float4 respectively.
for these types, the [] operator returns an internal float0,
which may be casted up or down as the case may be (array offsets would require
casting down)
Additionally these classes have the swizzle operator, which takes in from 1 to 4
ints as template arguments, and returns a float0 vector of the appropriate size.
It rearranges the input with respect to the swizzle arguments like so:
float3(1,2,3,4).swizzle3<2,2,1>() returns a float3 containing {3,3,2}.
They are all straightforward to apply to the incoming Cg code; however,
masking is a bit more complicated.
in Cg you are allowed to rearrange the input vector on the lvalue, and assign
to it (or increment/multiply it) and have the return value be restored.
Given the functional nature of Cg, the lval += rval operator is replaced by
lval = lval+rval;
This gives the compiler the flexibility to change the = operator into a mask
function call, taking in the result of the addition and reordering the assignment
appropriately.
lval.mask3(rval,maskZ,maskY,maskX) (where maskN are all constants defined in our header)
will assign the rval to the 3 appropriate spaces of the lval,
and then return a new vector of size 3 for further operation.
these template classes are all recursive and may be stacked upon each other
to produce
typedef float4x4;
and so forth...the matrix operations work in an analagous way to the vector ones.
Matrix multiplication is componentwise.
TODO:
Explain type promotion and int0,int2,int3,int4 classes
and how they interact with floats.
Explain the template magic behind this innocent-looking class.
|