BsASTFX.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  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. { OT_Tags, ODT_Complex },
  75. { OT_TagValue, ODT_String },
  76. { OT_Base, ODT_String },
  77. { OT_Inherits, ODT_String }
  78. };
  79. NodeOptions* nodeOptionsCreate(void* context)
  80. {
  81. static const int BUFFER_SIZE = 5;
  82. NodeOptions* options = (NodeOptions*)mmalloc(context, sizeof(NodeOptions));
  83. options->count = 0;
  84. options->bufferSize = BUFFER_SIZE;
  85. options->entries = (NodeOption*)mmalloc(context, sizeof(NodeOption) * options->bufferSize);
  86. memset(options->entries, 0, sizeof(NodeOption) * options->bufferSize);
  87. return options;
  88. }
  89. void nodeOptionDelete(NodeOption* option)
  90. {
  91. if (OPTION_LOOKUP[(int)option->type].dataType == ODT_Complex)
  92. {
  93. nodeDelete(option->value.nodePtr);
  94. option->value.nodePtr = 0;
  95. }
  96. else if (OPTION_LOOKUP[(int)option->type].dataType == ODT_String)
  97. {
  98. mmfree((void*)option->value.strValue);
  99. option->value.strValue = 0;
  100. }
  101. }
  102. void nodeOptionsDelete(NodeOptions* options)
  103. {
  104. int i = 0;
  105. for (i = 0; i < options->count; i++)
  106. nodeOptionDelete(&options->entries[i]);
  107. mmfree(options->entries);
  108. mmfree(options);
  109. }
  110. void nodeOptionsResize(void* context, NodeOptions* options, int size)
  111. {
  112. NodeOption* originalEntries = options->entries;
  113. int originalSize = options->bufferSize;
  114. int elementsToCopy = originalSize;
  115. int sizeToCopy = 0;
  116. options->bufferSize = size;
  117. if (options->count > options->bufferSize)
  118. options->count = options->bufferSize;
  119. if (elementsToCopy > size)
  120. elementsToCopy = size;
  121. sizeToCopy = elementsToCopy * sizeof(NodeOption);
  122. options->entries = (NodeOption*)mmalloc(context, sizeof(NodeOption) * options->bufferSize);
  123. memcpy(options->entries, originalEntries, sizeToCopy);
  124. memset(options->entries + elementsToCopy, 0, sizeof(NodeOption) * options->bufferSize - sizeToCopy);
  125. mmfree(originalEntries);
  126. }
  127. void nodeOptionsGrowIfNeeded(void* context, NodeOptions* options)
  128. {
  129. static const int BUFFER_GROW = 10;
  130. if (options->count == options->bufferSize)
  131. nodeOptionsResize(context, options, options->bufferSize + BUFFER_GROW);
  132. }
  133. void nodeOptionsAdd(void* context, NodeOptions* options, const NodeOption* option)
  134. {
  135. nodeOptionsGrowIfNeeded(context, options);
  136. options->entries[options->count] = *option;
  137. options->count++;
  138. }
  139. ASTFXNode* nodeCreate(void* context, NodeType type)
  140. {
  141. ASTFXNode* node = (ASTFXNode*)mmalloc(context, sizeof(ASTFXNode));
  142. node->options = nodeOptionsCreate(context);
  143. node->type = type;
  144. return node;
  145. }
  146. void nodeDelete(ASTFXNode* node)
  147. {
  148. nodeOptionsDelete(node->options);
  149. mmfree(node);
  150. }
  151. void nodePush(ParseState* parseState, ASTFXNode* node)
  152. {
  153. NodeLink* linkNode = (NodeLink*)mmalloc(parseState->memContext, sizeof(NodeLink));
  154. linkNode->next = parseState->nodeStack;
  155. linkNode->node = node;
  156. parseState->nodeStack = linkNode;
  157. parseState->topNode = node;
  158. }
  159. void nodePop(ParseState* parseState)
  160. {
  161. if (!parseState->nodeStack)
  162. return;
  163. NodeLink* toRemove = parseState->nodeStack;
  164. parseState->nodeStack = toRemove->next;
  165. if (parseState->nodeStack)
  166. parseState->topNode = parseState->nodeStack->node;
  167. else
  168. parseState->topNode = 0;
  169. mmfree(toRemove);
  170. }
  171. void beginCodeBlock(ParseState* parseState)
  172. {
  173. CodeString* codeString = (CodeString*)mmalloc(parseState->memContext, sizeof(CodeString));
  174. codeString->index = parseState->numCodeStrings;
  175. codeString->size = 0;
  176. codeString->capacity = 4096;
  177. codeString->code = mmalloc(parseState->memContext, codeString->capacity);
  178. codeString->next = parseState->codeStrings;
  179. parseState->numCodeStrings++;
  180. parseState->codeStrings = codeString;
  181. // Insert defines for code-blocks as we don't perform pre-processing within code blocks but we still want outer defines
  182. // to be recognized by them (Performing pre-processing for code blocks is problematic because it would require parsing
  183. // of all the language syntax in order to properly handle macro replacement).
  184. for (int i = 0; i < parseState->numDefines; i++)
  185. {
  186. const char* define = "#define ";
  187. appendCodeBlock(parseState, define, (int)strlen(define));
  188. appendCodeBlock(parseState, parseState->defines[i].name, (int)strlen(parseState->defines[i].name));
  189. if (parseState->defines[i].expr != 0)
  190. {
  191. appendCodeBlock(parseState, " ", 1);
  192. appendCodeBlock(parseState, parseState->defines[i].expr, (int)strlen(parseState->defines[i].expr));
  193. }
  194. appendCodeBlock(parseState, "\n", 1);
  195. }
  196. }
  197. void appendCodeBlock(ParseState* parseState, const char* value, int size)
  198. {
  199. CodeString* codeString = parseState->codeStrings;
  200. if ((codeString->size + size) > codeString->capacity)
  201. {
  202. int newCapacity = codeString->capacity;
  203. do
  204. {
  205. newCapacity *= 2;
  206. } while ((codeString->size + size) > newCapacity);
  207. char* newBuffer = mmalloc(parseState->memContext, newCapacity);
  208. memcpy(newBuffer, codeString->code, codeString->size);
  209. mmfree(codeString->code);
  210. codeString->code = newBuffer;
  211. codeString->capacity = newCapacity;
  212. }
  213. memcpy(&codeString->code[codeString->size], value, size);
  214. codeString->size += size;
  215. }
  216. int getCodeBlockIndex(ParseState* parseState)
  217. {
  218. return parseState->codeStrings->index;
  219. }
  220. char* getCurrentFilename(ParseState* parseState)
  221. {
  222. if (!parseState->includeStack)
  223. return NULL;
  224. return parseState->includeStack->data->filename;
  225. }
  226. void addDefine(ParseState* parseState, const char* value)
  227. {
  228. int defineIdx = parseState->numDefines;
  229. parseState->numDefines++;
  230. if(parseState->numDefines > parseState->defineCapacity)
  231. {
  232. int newCapacity = parseState->defineCapacity * 2;
  233. DefineEntry* newDefines = mmalloc(parseState->memContext, newCapacity * sizeof(DefineEntry));
  234. memcpy(newDefines, parseState->defines, parseState->defineCapacity * sizeof(DefineEntry));
  235. mmfree(parseState->defines);
  236. parseState->defines = newDefines;
  237. parseState->defineCapacity = newCapacity;
  238. }
  239. parseState->defines[defineIdx].name = mmalloc_strdup(parseState->memContext, value);
  240. parseState->defines[defineIdx].expr = 0;
  241. }
  242. void addDefineExpr(ParseState* parseState, const char* value)
  243. {
  244. int defineIdx = parseState->numDefines - 1;
  245. if(defineIdx < 0)
  246. {
  247. assert(0);
  248. return;
  249. }
  250. parseState->defines[defineIdx].expr = mmalloc_strdup(parseState->memContext, value);
  251. }
  252. int hasDefine(ParseState* parseState, const char* value)
  253. {
  254. for (int i = 0; i < parseState->numDefines; i++)
  255. {
  256. if (strcmp(parseState->defines[i].name, value) == 0)
  257. return 1;
  258. }
  259. return 0;
  260. }
  261. void removeDefine(ParseState* parseState, const char* value)
  262. {
  263. for (int i = 0; i < parseState->numDefines; i++)
  264. {
  265. if (strcmp(parseState->defines[i].name, value) == 0)
  266. {
  267. int remaining = parseState->numDefines - (i + 1);
  268. if(remaining > 0)
  269. memcpy(&parseState->defines[i], &parseState->defines[i + 1], remaining * sizeof(DefineEntry));
  270. parseState->numDefines--;
  271. }
  272. }
  273. }
  274. int pushConditional(ParseState* parseState, int state)
  275. {
  276. ConditionalData* conditional = mmalloc(parseState->memContext, sizeof(ConditionalData));
  277. conditional->enabled = state && (parseState->conditionalStack == 0 || parseState->conditionalStack->enabled);
  278. conditional->selfEnabled = state;
  279. conditional->next = parseState->conditionalStack;
  280. parseState->conditionalStack = conditional;
  281. return conditional->enabled;
  282. }
  283. int switchConditional(ParseState* parseState)
  284. {
  285. if (parseState->conditionalStack == 0)
  286. return 1;
  287. ConditionalData* conditional = parseState->conditionalStack;
  288. return setConditional(parseState, !conditional->selfEnabled);
  289. }
  290. int setConditional(ParseState* parseState, int state)
  291. {
  292. if (parseState->conditionalStack == 0)
  293. return 1;
  294. ConditionalData* conditional = parseState->conditionalStack;
  295. ConditionalData* parent = conditional->next;
  296. conditional->enabled = state && (parent == 0 || parent->enabled);
  297. conditional->selfEnabled = state;
  298. return conditional->enabled;
  299. }
  300. int popConditional(ParseState* parseState)
  301. {
  302. if (parseState->conditionalStack == 0)
  303. return 1;
  304. ConditionalData* conditional = parseState->conditionalStack;
  305. parseState->conditionalStack = conditional->next;
  306. mmfree(conditional);
  307. return parseState->conditionalStack == 0 || parseState->conditionalStack->enabled;
  308. }
  309. ParseState* parseStateCreate()
  310. {
  311. ParseState* parseState = (ParseState*)malloc(sizeof(ParseState));
  312. parseState->memContext = mmalloc_new_context();
  313. parseState->rootNode = nodeCreate(parseState->memContext, NT_Shader);
  314. parseState->topNode = 0;
  315. parseState->nodeStack = 0;
  316. parseState->includeStack = 0;
  317. parseState->includes = 0;
  318. parseState->codeStrings = 0;
  319. parseState->numCodeStrings = 0;
  320. parseState->numOpenBrackets = 0;
  321. parseState->hasError = 0;
  322. parseState->errorLine = 0;
  323. parseState->errorColumn = 0;
  324. parseState->errorMessage = 0;
  325. parseState->errorFile = 0;
  326. parseState->conditionalStack = 0;
  327. parseState->defineCapacity = 10;
  328. parseState->numDefines = 0;
  329. parseState->defines = mmalloc(parseState->memContext, parseState->defineCapacity * sizeof(DefineEntry));
  330. nodePush(parseState, parseState->rootNode);
  331. return parseState;
  332. }
  333. void parseStateDelete(ParseState* parseState)
  334. {
  335. while (parseState->nodeStack != 0)
  336. nodePop(parseState);
  337. nodeDelete(parseState->rootNode);
  338. mmalloc_free_context(parseState->memContext);
  339. free(parseState);
  340. }