|
Design_Shaders
Design and implementation of Fog-SL - Fog Shader Language.
Phase-Design IntroductionThis document contains design and implementation notes of incoming 2d shaders module. It should cover the shader language, all functionality and possibility for future extensions to improve the performance. Shader Assembly LanguageTheir are many variations of shader assembly language:
We are starting with the !!ARBfp1.0 syntax. We imagine that shaders may be built with assembly, or generated via Cg. LanguageScalar variable types:
Vector variable types:
Mike: I am not sure if the array braces syntax is available for !!ARBfp1.0. I now think that the array braces are used exclusively for PARAM array and program variable access operations. Shader Assembly Language Quick Reference Guild. Structures:
Control FlowI think that basic control flow blocks should be supported. I'm not sure about loops, but I'm sure about IFs. Fog Shader Extensions / HintsIt is needed to define some hints that will allow us to generate integer only code, instead of floating point code. Nearly all graphics operations are currently in integer only math and there must be possibility to do that in shaders too (to fit into pipeline the Fog can generate). We can include typedef extension (for example called pixel) which can be used instead of int or float. For 8-bit pixel formats the width of the pixel type must be 16 bits and for 16-bit formats it must be 32, because the pixel value can be multiplied by other pixel value, dividing it later by 255 or 65535 to match floating point implementation. The following code should work with 8-bit pixels, 16-bit pixels and 32-bit pixels (floats). If the engine will locate pixel type then this will be signal that implementation is generic and doesn't need floating point unit. pixel4 shader(pixel4 dst, pixel4 src)
{
pixel4 result = dst * src;
return result;
}Fog-SL Program TypeAt this time there are only two program types:
Fog-SL Program StructureComposite Shader: pixel4 shader(pixel4 dst, pixel4 src)
{
pixel4 result;
...
return result;
}Source Shader: pixel4 source()
{
pixel4 result;
...
return result;
}Portability with OpenGLThis document should reveal all portability issues. The Fog-SL is defined to be shader language for 2d graphics, so for example we don't work with full 3d (4x4 matrix) transformations. The Fog-SL should be directly compilable into any language which can be executed by the GPU, so, this is reason why there are similar types and why there isn't loops. Create Shader with CgUsing the Windows or Linux Cg SDK: CGprogram myCgFragmentProgram;
myCgFragmentProgram = cgCreateProgram(
myCgContext, /* Cg runtime context */
CG_SOURCE, /* Program in human-readable form */
pcszCode, /* Cg Code */
CG_PROFILE_ARBFP1, /* Profile: ARBfp1.0 */
pcszFuncName, /* Entry function name */
NULL); /* No extra compiler options */
const char *pcszARB = cgGetProgramString(myCgFragmentProgram, CG_COMPILED_PROGRAM);
// pcszARB now points to string of ARBfp1 Assembly Language
// Use the Code
// Cleanup
cgDestroyProgram(myCgFragmentProgram);Project DetailsThis module (library) requires AsmJit for the code generator. The parser is built with yard (a public domain template based parsing library). Parser GrammarYard is a templated parser. It is very easy to use, and understand. It is also public domain, so there are no licenses to worry about. The Yard Parser library uses RTTI to discover what rule resolved by the parser. I decided to change this to remove the RTTI. Now, I use a static const int to identify the rule. Some of the rules used: struct ARBComment;
struct NewLine
: Or<CharSeq<'\r', '\n'>, CharSeq<'\n'> >
{
};This will match '\r\n' or '\n'. struct WhiteSpace
: Star<Or<Char<' '>, Char<'\t'>, NewLine, ARBComment, Char<'\r'>, Char<'\v'>, Char<'\f'> > >
{
};Note: these don't have a const int because I don't process them. struct DecNumber
: Seq<Opt<Char<'-'> >, Plus<Digit>, Opt<Seq<Char<'.'>, Star<Digit> > >, NotAlphaNum, WhiteSpace>
{
RULE_TYPE(ARB_DecNumber);
};
#define RULE_TYPE(p) \
static const int eType_ = p; \
static int type() { return eType_; } This uses an enum with the rule names. In the Match method, this is used to figure out what rule satisfied the input. Code GeneratorTBC External APITBC |