| 1 | //************************************ bs::framework - Copyright 2018 Marko Pintera **************************************// |
| 2 | //*********** Licensed under the MIT license. See LICENSE.md for full terms. This notice is not to be removed. ***********// |
| 3 | #ifndef __ASTFX_H__ |
| 4 | #define __ASTFX_H__ |
| 5 | |
| 6 | #include <stdlib.h> |
| 7 | #include <string.h> |
| 8 | |
| 9 | enum tagNodeType |
| 10 | { |
| 11 | NT_Root, |
| 12 | NT_Options, |
| 13 | NT_Shader, |
| 14 | NT_SubShader, |
| 15 | NT_Mixin, |
| 16 | NT_Pass, |
| 17 | NT_Blend, |
| 18 | NT_Raster, |
| 19 | NT_Depth, |
| 20 | NT_Stencil, |
| 21 | NT_Target, |
| 22 | NT_StencilOp, |
| 23 | NT_BlendDef, |
| 24 | NT_Tags, |
| 25 | NT_Code, |
| 26 | NT_Variations, |
| 27 | NT_Variation, |
| 28 | NT_VariationOption, |
| 29 | NT_Attributes |
| 30 | }; |
| 31 | |
| 32 | enum tagOptionType |
| 33 | { |
| 34 | OT_None = 0, |
| 35 | OT_Options, |
| 36 | OT_Separable, |
| 37 | OT_Priority, |
| 38 | OT_Sort, |
| 39 | OT_Transparent, |
| 40 | OT_Shader, |
| 41 | OT_SubShader, |
| 42 | OT_Mixin, |
| 43 | OT_Raster, |
| 44 | OT_Depth, |
| 45 | OT_Stencil, |
| 46 | OT_Blend, |
| 47 | OT_FeatureSet, |
| 48 | OT_Pass, |
| 49 | OT_FillMode, |
| 50 | OT_CullMode, |
| 51 | OT_DepthBias, |
| 52 | OT_SDepthBias, |
| 53 | OT_DepthClip, |
| 54 | OT_Scissor, |
| 55 | OT_Multisample, |
| 56 | OT_AALine, |
| 57 | OT_DepthRead, |
| 58 | OT_DepthWrite, |
| 59 | OT_CompareFunc, |
| 60 | OT_StencilReadMask, |
| 61 | OT_StencilWriteMask, |
| 62 | OT_StencilOpFront, |
| 63 | OT_StencilOpBack, |
| 64 | OT_PassOp, |
| 65 | OT_Fail, |
| 66 | OT_ZFail, |
| 67 | OT_AlphaToCoverage, |
| 68 | OT_IndependantBlend, |
| 69 | OT_Target, |
| 70 | OT_Index, |
| 71 | OT_Enabled, |
| 72 | OT_Color, |
| 73 | OT_Alpha, |
| 74 | OT_WriteMask, |
| 75 | OT_Source, |
| 76 | OT_Dest, |
| 77 | OT_Op, |
| 78 | OT_Identifier, |
| 79 | OT_Code, |
| 80 | OT_StencilRef, |
| 81 | OT_Tags, |
| 82 | OT_TagValue, |
| 83 | OT_Variations, |
| 84 | OT_Variation, |
| 85 | OT_VariationValue, |
| 86 | OT_Forward, |
| 87 | OT_Attributes, |
| 88 | OT_AttrName, |
| 89 | OT_VariationOption, |
| 90 | OT_AttrShow, |
| 91 | OT_Count |
| 92 | }; |
| 93 | |
| 94 | enum tagOptionDataType |
| 95 | { |
| 96 | ODT_Int, |
| 97 | ODT_Float, |
| 98 | ODT_Bool, |
| 99 | ODT_String, |
| 100 | ODT_Complex, |
| 101 | ODT_Matrix |
| 102 | }; |
| 103 | |
| 104 | enum tagFillModeValue |
| 105 | { |
| 106 | FMV_Wire, FMV_Solid |
| 107 | }; |
| 108 | |
| 109 | enum tagCullAndSortModeValue |
| 110 | { |
| 111 | CASV_None, CASV_CW, CASV_CCW, CASV_FrontToBack, CASV_BackToFront |
| 112 | }; |
| 113 | |
| 114 | enum tagCompFuncValue |
| 115 | { |
| 116 | CFV_Fail, CFV_Pass, CFV_LT, CFV_LTE, |
| 117 | CFV_EQ, CFV_NEQ, CFV_GTE, CFV_GT |
| 118 | }; |
| 119 | |
| 120 | enum tagOpValue |
| 121 | { |
| 122 | OV_Keep, OV_Zero, OV_Replace, OV_Incr, OV_Decr, |
| 123 | OV_IncrWrap, OV_DecrWrap, OV_Invert, OV_One, OV_DestColor, |
| 124 | OV_SrcColor, OV_InvDestColor, OV_InvSrcColor, OV_DestAlpha, |
| 125 | OV_SrcAlpha, OV_InvDestAlpha, OV_InvSrcAlpha |
| 126 | }; |
| 127 | |
| 128 | enum tagBlendOpValue |
| 129 | { |
| 130 | BOV_Add, BOV_Subtract, BOV_RevSubtract, |
| 131 | BOV_Min, BOV_Max |
| 132 | }; |
| 133 | |
| 134 | enum tagRawCodeType |
| 135 | { |
| 136 | RCT_CodeBlock, |
| 137 | RCT_SubShaderBlock, |
| 138 | RCT_Count |
| 139 | }; |
| 140 | |
| 141 | enum tagConditionalOp |
| 142 | { |
| 143 | CO_None, CO_Equals, CO_NotEquals, CO_Lesser, CO_Greater, CO_LesserEqual, CO_GreaterEqual |
| 144 | }; |
| 145 | |
| 146 | typedef enum tagNodeType NodeType; |
| 147 | typedef enum tagOptionType OptionType; |
| 148 | typedef enum tagOptionDataType OptionDataType; |
| 149 | typedef struct tagParseState ParseState; |
| 150 | typedef struct tagOptionInfo OptionInfo; |
| 151 | typedef union tagOptionData OptionData; |
| 152 | typedef struct tagNodeOptions NodeOptions; |
| 153 | typedef struct tagNodeOption NodeOption; |
| 154 | typedef struct tagASTFXNode ASTFXNode; |
| 155 | typedef struct tagNodeLink NodeLink; |
| 156 | typedef struct tagIncludeData IncludeData; |
| 157 | typedef struct tagIncludeLink IncludeLink; |
| 158 | typedef struct tagConditionalData ConditionalData; |
| 159 | typedef struct tagRawCode RawCode; |
| 160 | typedef struct tagDefineEntry DefineEntry; |
| 161 | typedef enum tagFillModeValue FillModeValue; |
| 162 | typedef enum tagCullAndSortModeValue CullAndSortModeValue; |
| 163 | typedef enum tagCompFuncValue CompFuncValue; |
| 164 | typedef enum tagOpValue OpValue; |
| 165 | typedef enum tagBlendOpValue BlendOpValue; |
| 166 | typedef enum tagRawCodeType RawCodeType; |
| 167 | typedef enum tagConditionalOp ConditionalOp; |
| 168 | |
| 169 | struct tagNodeLink |
| 170 | { |
| 171 | ASTFXNode* node; |
| 172 | NodeLink* next; |
| 173 | }; |
| 174 | |
| 175 | struct tagIncludeData |
| 176 | { |
| 177 | char* filename; |
| 178 | char* buffer; |
| 179 | }; |
| 180 | |
| 181 | struct tagIncludeLink |
| 182 | { |
| 183 | IncludeData* data; |
| 184 | IncludeLink* next; |
| 185 | }; |
| 186 | |
| 187 | struct tagConditionalData |
| 188 | { |
| 189 | char* name; |
| 190 | int selfEnabled; |
| 191 | int enabled; |
| 192 | ConditionalOp op; |
| 193 | char* value; |
| 194 | |
| 195 | ConditionalData* next; |
| 196 | }; |
| 197 | |
| 198 | struct tagRawCode |
| 199 | { |
| 200 | char* code; |
| 201 | int index; |
| 202 | int size; |
| 203 | int capacity; |
| 204 | |
| 205 | RawCode* next; |
| 206 | }; |
| 207 | |
| 208 | struct tagDefineEntry |
| 209 | { |
| 210 | char* name; |
| 211 | char* expr; |
| 212 | }; |
| 213 | |
| 214 | struct tagParseState |
| 215 | { |
| 216 | ASTFXNode* rootNode; |
| 217 | ASTFXNode* topNode; |
| 218 | void* memContext; |
| 219 | |
| 220 | int hasError; |
| 221 | int errorLine; |
| 222 | int errorColumn; |
| 223 | const char* errorMessage; |
| 224 | char* errorFile; |
| 225 | |
| 226 | NodeLink* nodeStack; |
| 227 | IncludeLink* includeStack; |
| 228 | IncludeLink* includes; |
| 229 | RawCode* rawCodeBlock[RCT_Count]; |
| 230 | int numRawCodeBlocks[RCT_Count]; |
| 231 | int numOpenBrackets; |
| 232 | |
| 233 | DefineEntry* defines; |
| 234 | int numDefines; |
| 235 | int defineCapacity; |
| 236 | ConditionalData* conditionalStack; |
| 237 | }; |
| 238 | |
| 239 | struct tagOptionInfo |
| 240 | { |
| 241 | OptionType type; |
| 242 | OptionDataType dataType; |
| 243 | }; |
| 244 | |
| 245 | union tagOptionData |
| 246 | { |
| 247 | int intValue; |
| 248 | float floatValue; |
| 249 | const char* strValue; |
| 250 | float matrixValue[16]; |
| 251 | int intVectorValue[4]; |
| 252 | ASTFXNode* nodePtr; |
| 253 | }; |
| 254 | |
| 255 | struct tagNodeOption |
| 256 | { |
| 257 | OptionType type; |
| 258 | OptionData value; |
| 259 | }; |
| 260 | |
| 261 | struct tagNodeOptions |
| 262 | { |
| 263 | NodeOption* entries; |
| 264 | |
| 265 | int count; |
| 266 | int bufferSize; |
| 267 | }; |
| 268 | |
| 269 | struct tagASTFXNode |
| 270 | { |
| 271 | NodeType type; |
| 272 | NodeOptions* options; |
| 273 | }; |
| 274 | |
| 275 | extern OptionInfo OPTION_LOOKUP[OT_Count]; |
| 276 | |
| 277 | NodeOptions* nodeOptionsCreate(void* context); |
| 278 | void nodeOptionDelete(NodeOption* option); |
| 279 | void nodeOptionsDelete(NodeOptions* options); |
| 280 | void nodeOptionsResize(void* context, NodeOptions* options, int size); |
| 281 | void nodeOptionsGrowIfNeeded(void* context, NodeOptions* options); |
| 282 | void nodeOptionsAdd(void* context, NodeOptions* options, const NodeOption* option); |
| 283 | |
| 284 | ASTFXNode* nodeCreate(void* context, NodeType type); |
| 285 | void nodeDelete(ASTFXNode* node); |
| 286 | |
| 287 | void nodePush(ParseState* parseState, ASTFXNode* node); |
| 288 | void nodePop(ParseState* parseState); |
| 289 | |
| 290 | void beginCodeBlock(ParseState* parseState, RawCodeType type); |
| 291 | void appendCodeBlock(ParseState* parseState, RawCodeType type, const char* value, int size); |
| 292 | int getCodeBlockIndex(ParseState* parseState, RawCodeType type); |
| 293 | |
| 294 | void addDefine(ParseState* parseState, const char* value); |
| 295 | void addDefineExpr(ParseState* parseState, const char* value); |
| 296 | int hasDefine(ParseState* parseState, const char* value); |
| 297 | void removeDefine(ParseState* parseState, const char* value); |
| 298 | |
| 299 | int pushConditionalDef(ParseState* parseState, int state); |
| 300 | void pushConditional(ParseState* parseState, const char* name); |
| 301 | void setConditionalOp(ParseState* parseState, ConditionalOp op); |
| 302 | void setConditionalVal(ParseState* parseState, const char* value); |
| 303 | int evalConditional(ParseState* parseState); |
| 304 | int switchConditional(ParseState* parseState); |
| 305 | void setConditional(ParseState* parseState, const char* name); |
| 306 | int popConditional(ParseState* parseState); |
| 307 | |
| 308 | char* getCurrentFilename(ParseState* parseState); |
| 309 | |
| 310 | ParseState* parseStateCreate(); |
| 311 | void parseStateDelete(ParseState* parseState); |
| 312 | |
| 313 | #endif |