BsASTFX.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "BsASTFX.h"
  4. #include "BsMMAlloc.h"
  5. #include <assert.h>
  6. OptionInfo OPTION_LOOKUP[] =
  7. {
  8. { OT_None, ODT_Int },
  9. { OT_Options, ODT_Complex },
  10. { OT_Separable, ODT_Bool },
  11. { OT_Priority, ODT_Int },
  12. { OT_Sort, ODT_Int },
  13. { OT_Transparent, ODT_Bool },
  14. { OT_Technique, ODT_Complex },
  15. { OT_Mixin, ODT_String },
  16. { OT_Raster, ODT_Complex },
  17. { OT_Depth, ODT_Complex },
  18. { OT_Stencil, ODT_Complex },
  19. { OT_Blend, ODT_Complex },
  20. { OT_Renderer, ODT_String },
  21. { OT_Pass, ODT_Complex },
  22. { OT_FillMode, ODT_Int },
  23. { OT_CullMode, ODT_Int },
  24. { OT_DepthBias, ODT_Float },
  25. { OT_SDepthBias, ODT_Float },
  26. { OT_DepthClip, ODT_Bool },
  27. { OT_Scissor, ODT_Bool },
  28. { OT_Multisample, ODT_Bool },
  29. { OT_AALine, ODT_Bool },
  30. { OT_DepthRead, ODT_Bool },
  31. { OT_DepthWrite, ODT_Bool },
  32. { OT_CompareFunc, ODT_Int },
  33. { OT_StencilReadMask, ODT_Int },
  34. { OT_StencilWriteMask, ODT_Int },
  35. { OT_StencilOpFront, ODT_Complex },
  36. { OT_StencilOpBack, ODT_Complex },
  37. { OT_PassOp, ODT_Int },
  38. { OT_Fail, ODT_Int },
  39. { OT_ZFail, ODT_Int },
  40. { OT_AlphaToCoverage, ODT_Bool },
  41. { OT_IndependantBlend, ODT_Bool },
  42. { OT_Target, ODT_Complex },
  43. { OT_Index, ODT_Int },
  44. { OT_Enabled, ODT_Bool },
  45. { OT_Color, ODT_Complex },
  46. { OT_Alpha, ODT_Complex },
  47. { OT_WriteMask, ODT_Int },
  48. { OT_Source, ODT_Int },
  49. { OT_Dest, ODT_Int },
  50. { OT_Op, ODT_Int },
  51. { OT_Identifier, ODT_String },
  52. { OT_Code, ODT_Complex },
  53. { OT_StencilRef, ODT_Int },
  54. { OT_Tags, ODT_Complex },
  55. { OT_TagValue, ODT_String },
  56. };
  57. NodeOptions* nodeOptionsCreate(void* context)
  58. {
  59. static const int BUFFER_SIZE = 5;
  60. NodeOptions* options = (NodeOptions*)mmalloc(context, sizeof(NodeOptions));
  61. options->count = 0;
  62. options->bufferSize = BUFFER_SIZE;
  63. options->entries = (NodeOption*)mmalloc(context, sizeof(NodeOption) * options->bufferSize);
  64. memset(options->entries, 0, sizeof(NodeOption) * options->bufferSize);
  65. return options;
  66. }
  67. void nodeOptionDelete(NodeOption* option)
  68. {
  69. if (OPTION_LOOKUP[(int)option->type].dataType == ODT_Complex)
  70. {
  71. nodeDelete(option->value.nodePtr);
  72. option->value.nodePtr = 0;
  73. }
  74. else if (OPTION_LOOKUP[(int)option->type].dataType == ODT_String)
  75. {
  76. mmfree((void*)option->value.strValue);
  77. option->value.strValue = 0;
  78. }
  79. }
  80. void nodeOptionsDelete(NodeOptions* options)
  81. {
  82. int i = 0;
  83. for (i = 0; i < options->count; i++)
  84. nodeOptionDelete(&options->entries[i]);
  85. mmfree(options->entries);
  86. mmfree(options);
  87. }
  88. void nodeOptionsResize(void* context, NodeOptions* options, int size)
  89. {
  90. NodeOption* originalEntries = options->entries;
  91. int originalSize = options->bufferSize;
  92. int elementsToCopy = originalSize;
  93. int sizeToCopy = 0;
  94. options->bufferSize = size;
  95. if (options->count > options->bufferSize)
  96. options->count = options->bufferSize;
  97. if (elementsToCopy > size)
  98. elementsToCopy = size;
  99. sizeToCopy = elementsToCopy * sizeof(NodeOption);
  100. options->entries = (NodeOption*)mmalloc(context, sizeof(NodeOption) * options->bufferSize);
  101. memcpy(options->entries, originalEntries, sizeToCopy);
  102. memset(options->entries + elementsToCopy, 0, sizeof(NodeOption) * options->bufferSize - sizeToCopy);
  103. mmfree(originalEntries);
  104. }
  105. void nodeOptionsGrowIfNeeded(void* context, NodeOptions* options)
  106. {
  107. static const int BUFFER_GROW = 10;
  108. if (options->count == options->bufferSize)
  109. nodeOptionsResize(context, options, options->bufferSize + BUFFER_GROW);
  110. }
  111. void nodeOptionsAdd(void* context, NodeOptions* options, const NodeOption* option)
  112. {
  113. nodeOptionsGrowIfNeeded(context, options);
  114. options->entries[options->count] = *option;
  115. options->count++;
  116. }
  117. ASTFXNode* nodeCreate(void* context, NodeType type)
  118. {
  119. ASTFXNode* node = (ASTFXNode*)mmalloc(context, sizeof(ASTFXNode));
  120. node->options = nodeOptionsCreate(context);
  121. node->type = type;
  122. return node;
  123. }
  124. void nodeDelete(ASTFXNode* node)
  125. {
  126. nodeOptionsDelete(node->options);
  127. mmfree(node);
  128. }
  129. void nodePush(ParseState* parseState, ASTFXNode* node)
  130. {
  131. NodeLink* linkNode = (NodeLink*)mmalloc(parseState->memContext, sizeof(NodeLink));
  132. linkNode->next = parseState->nodeStack;
  133. linkNode->node = node;
  134. parseState->nodeStack = linkNode;
  135. parseState->topNode = node;
  136. }
  137. void nodePop(ParseState* parseState)
  138. {
  139. if (!parseState->nodeStack)
  140. return;
  141. NodeLink* toRemove = parseState->nodeStack;
  142. parseState->nodeStack = toRemove->next;
  143. if (parseState->nodeStack)
  144. parseState->topNode = parseState->nodeStack->node;
  145. else
  146. parseState->topNode = 0;
  147. mmfree(toRemove);
  148. }
  149. void beginCodeBlock(ParseState* parseState)
  150. {
  151. CodeString* codeString = (CodeString*)mmalloc(parseState->memContext, sizeof(CodeString));
  152. codeString->index = parseState->numCodeStrings;
  153. codeString->size = 0;
  154. codeString->capacity = 4096;
  155. codeString->code = mmalloc(parseState->memContext, codeString->capacity);
  156. codeString->next = parseState->codeStrings;
  157. parseState->numCodeStrings++;
  158. parseState->codeStrings = codeString;
  159. // Insert defines for code-blocks as we don't perform pre-processing within code blocks but we still want outer defines
  160. // to be recognized by them (Performing pre-processing for code blocks is problematic because it would require parsing
  161. // of all the language syntax in order to properly handle macro replacement).
  162. for (int i = 0; i < parseState->numDefines; i++)
  163. {
  164. const char* define = "#define ";
  165. appendCodeBlock(parseState, define, (int)strlen(define));
  166. appendCodeBlock(parseState, parseState->defines[i].name, (int)strlen(parseState->defines[i].name));
  167. if (parseState->defines[i].expr != 0)
  168. {
  169. appendCodeBlock(parseState, " ", 1);
  170. appendCodeBlock(parseState, parseState->defines[i].expr, (int)strlen(parseState->defines[i].expr));
  171. }
  172. appendCodeBlock(parseState, "\n", 1);
  173. }
  174. }
  175. void appendCodeBlock(ParseState* parseState, const char* value, int size)
  176. {
  177. CodeString* codeString = parseState->codeStrings;
  178. if ((codeString->size + size) > codeString->capacity)
  179. {
  180. int newCapacity = codeString->capacity;
  181. do
  182. {
  183. newCapacity *= 2;
  184. } while ((codeString->size + size) > newCapacity);
  185. char* newBuffer = mmalloc(parseState->memContext, newCapacity);
  186. memcpy(newBuffer, codeString->code, codeString->size);
  187. mmfree(codeString->code);
  188. codeString->code = newBuffer;
  189. codeString->capacity = newCapacity;
  190. }
  191. memcpy(&codeString->code[codeString->size], value, size);
  192. codeString->size += size;
  193. }
  194. int getCodeBlockIndex(ParseState* parseState)
  195. {
  196. return parseState->codeStrings->index;
  197. }
  198. char* getCurrentFilename(ParseState* parseState)
  199. {
  200. if (!parseState->includeStack)
  201. return NULL;
  202. return parseState->includeStack->data->filename;
  203. }
  204. void addDefine(ParseState* parseState, const char* value)
  205. {
  206. int defineIdx = parseState->numDefines;
  207. parseState->numDefines++;
  208. if(parseState->numDefines > parseState->defineCapacity)
  209. {
  210. int newCapacity = parseState->defineCapacity * 2;
  211. DefineEntry* newDefines = mmalloc(parseState->memContext, newCapacity * sizeof(DefineEntry));
  212. memcpy(newDefines, parseState->defines, parseState->defineCapacity * sizeof(DefineEntry));
  213. mmfree(parseState->defines);
  214. parseState->defines = newDefines;
  215. parseState->defineCapacity = newCapacity;
  216. }
  217. parseState->defines[defineIdx].name = mmalloc_strdup(parseState->memContext, value);
  218. parseState->defines[defineIdx].expr = 0;
  219. }
  220. void addDefineExpr(ParseState* parseState, const char* value)
  221. {
  222. int defineIdx = parseState->numDefines - 1;
  223. if(defineIdx < 0)
  224. {
  225. assert(0);
  226. return;
  227. }
  228. parseState->defines[defineIdx].expr = mmalloc_strdup(parseState->memContext, value);
  229. }
  230. int hasDefine(ParseState* parseState, const char* value)
  231. {
  232. for (int i = 0; i < parseState->numDefines; i++)
  233. {
  234. if (strcmp(parseState->defines[i].name, value) == 0)
  235. return 1;
  236. }
  237. return 0;
  238. }
  239. void removeDefine(ParseState* parseState, const char* value)
  240. {
  241. for (int i = 0; i < parseState->numDefines; i++)
  242. {
  243. if (strcmp(parseState->defines[i].name, value) == 0)
  244. {
  245. int remaining = parseState->numDefines - (i + 1);
  246. if(remaining > 0)
  247. memcpy(&parseState->defines[i], &parseState->defines[i + 1], remaining * sizeof(DefineEntry));
  248. parseState->numDefines--;
  249. }
  250. }
  251. }
  252. int pushConditional(ParseState* parseState, int state)
  253. {
  254. ConditionalData* conditional = mmalloc(parseState->memContext, sizeof(ConditionalData));
  255. conditional->enabled = state && (parseState->conditionalStack == 0 || parseState->conditionalStack->enabled);
  256. conditional->selfEnabled = state;
  257. conditional->next = parseState->conditionalStack;
  258. parseState->conditionalStack = conditional;
  259. return conditional->enabled;
  260. }
  261. int switchConditional(ParseState* parseState)
  262. {
  263. if (parseState->conditionalStack == 0)
  264. return 1;
  265. ConditionalData* conditional = parseState->conditionalStack;
  266. return setConditional(parseState, !conditional->selfEnabled);
  267. }
  268. int setConditional(ParseState* parseState, int state)
  269. {
  270. if (parseState->conditionalStack == 0)
  271. return 1;
  272. ConditionalData* conditional = parseState->conditionalStack;
  273. ConditionalData* parent = conditional->next;
  274. conditional->enabled = state && (parent == 0 || parent->enabled);
  275. conditional->selfEnabled = state;
  276. return conditional->enabled;
  277. }
  278. int popConditional(ParseState* parseState)
  279. {
  280. if (parseState->conditionalStack == 0)
  281. return 1;
  282. ConditionalData* conditional = parseState->conditionalStack;
  283. parseState->conditionalStack = conditional->next;
  284. mmfree(conditional);
  285. return parseState->conditionalStack == 0 || parseState->conditionalStack->enabled;
  286. }
  287. ParseState* parseStateCreate()
  288. {
  289. ParseState* parseState = (ParseState*)malloc(sizeof(ParseState));
  290. parseState->memContext = mmalloc_new_context();
  291. parseState->rootNode = nodeCreate(parseState->memContext, NT_Shader);
  292. parseState->topNode = 0;
  293. parseState->nodeStack = 0;
  294. parseState->includeStack = 0;
  295. parseState->includes = 0;
  296. parseState->codeStrings = 0;
  297. parseState->numCodeStrings = 0;
  298. parseState->numOpenBrackets = 0;
  299. parseState->hasError = 0;
  300. parseState->errorLine = 0;
  301. parseState->errorColumn = 0;
  302. parseState->errorMessage = 0;
  303. parseState->errorFile = 0;
  304. parseState->conditionalStack = 0;
  305. parseState->defineCapacity = 10;
  306. parseState->numDefines = 0;
  307. parseState->defines = mmalloc(parseState->memContext, parseState->defineCapacity * sizeof(DefineEntry));
  308. nodePush(parseState, parseState->rootNode);
  309. return parseState;
  310. }
  311. void parseStateDelete(ParseState* parseState)
  312. {
  313. while (parseState->nodeStack != 0)
  314. nodePop(parseState);
  315. nodeDelete(parseState->rootNode);
  316. mmalloc_free_context(parseState->memContext);
  317. free(parseState);
  318. }