BsASTFX.c 11 KB

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