BsASTFX.c 10 KB

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