BsASTFX.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. #include "BsASTFX.h"
  2. #include "BsMMAlloc.h"
  3. OptionInfo OPTION_LOOKUP[] =
  4. {
  5. { OT_None, ODT_Int },
  6. { OT_Separable, ODT_Bool },
  7. { OT_Priority, ODT_Int },
  8. { OT_Queue, ODT_Int },
  9. { OT_Technique, ODT_Complex },
  10. { OT_Renderer, ODT_String },
  11. { OT_Language, ODT_String },
  12. { OT_Include, ODT_String },
  13. { OT_Pass, ODT_Complex },
  14. { OT_FillMode, ODT_Int },
  15. { OT_CullMode, ODT_Int },
  16. { OT_DepthBias, ODT_Float },
  17. { OT_SDepthBias, ODT_Float },
  18. { OT_DepthClip, ODT_Bool },
  19. { OT_Scissor, ODT_Bool },
  20. { OT_Multisample, ODT_Bool },
  21. { OT_AALine, ODT_Bool },
  22. { OT_DepthRead, ODT_Bool },
  23. { OT_DepthWrite, ODT_Bool },
  24. { OT_CompareFunc, ODT_Int },
  25. { OT_Stencil, ODT_Bool },
  26. { OT_StencilReadMask, ODT_Int },
  27. { OT_StencilWriteMask, ODT_Int },
  28. { OT_StencilOpFront, ODT_Complex },
  29. { OT_StencilOpBack, ODT_Complex },
  30. { OT_PassOp, ODT_Int },
  31. { OT_Fail, ODT_Int },
  32. { OT_ZFail, ODT_Int },
  33. { OT_AlphaToCoverage, ODT_Bool },
  34. { OT_IndependantBlend, ODT_Bool },
  35. { OT_Target, ODT_Complex },
  36. { OT_Index, ODT_Int },
  37. { OT_Blend, ODT_Bool },
  38. { OT_Color, ODT_Complex },
  39. { OT_Alpha, ODT_Complex },
  40. { OT_WriteMask, ODT_Int },
  41. { OT_Source, ODT_Int },
  42. { OT_Dest, ODT_Int },
  43. { OT_Op, ODT_Int },
  44. { OT_AddrMode, ODT_Complex },
  45. { OT_MinFilter, ODT_Int },
  46. { OT_MagFilter, ODT_Int },
  47. { OT_MipFilter, ODT_Int },
  48. { OT_MaxAniso, ODT_Int },
  49. { OT_MipBias, ODT_Float },
  50. { OT_MipMin, ODT_Float },
  51. { OT_MipMax, ODT_Float },
  52. { OT_BorderColor, ODT_Matrix },
  53. { OT_U, ODT_Int },
  54. { OT_V, ODT_Int },
  55. { OT_W, ODT_Int },
  56. { OT_Alias, ODT_String },
  57. { OT_Auto, ODT_String },
  58. { OT_Shared, ODT_Bool },
  59. { OT_Usage, ODT_Int },
  60. { OT_ParamType, ODT_Int },
  61. { OT_Identifier, ODT_String },
  62. { OT_ParamValue, ODT_Matrix },
  63. { OT_Parameters, ODT_Complex },
  64. { OT_Blocks, ODT_Complex },
  65. { OT_Parameter, ODT_Complex },
  66. { OT_Block, ODT_Complex },
  67. { OT_SamplerState, ODT_Complex },
  68. { OT_Code, ODT_Complex },
  69. { OT_StencilRef, ODT_Int }
  70. };
  71. NodeOptions* nodeOptionsCreate(void* context)
  72. {
  73. static const int BUFFER_SIZE = 5;
  74. NodeOptions* options = (NodeOptions*)mmalloc(context, sizeof(NodeOptions));
  75. options->count = 0;
  76. options->bufferSize = BUFFER_SIZE;
  77. options->entries = (NodeOption*)mmalloc(context, sizeof(NodeOption) * options->bufferSize);
  78. memset(options->entries, 0, sizeof(NodeOption) * options->bufferSize);
  79. return options;
  80. }
  81. void nodeOptionDelete(NodeOption* option)
  82. {
  83. if (OPTION_LOOKUP[(int)option->type].dataType == ODT_Complex)
  84. {
  85. nodeDelete(option->value.nodePtr);
  86. option->value.nodePtr = 0;
  87. }
  88. else if (OPTION_LOOKUP[(int)option->type].dataType == ODT_String)
  89. {
  90. mmfree((void*)option->value.strValue);
  91. option->value.strValue = 0;
  92. }
  93. }
  94. void nodeOptionsDelete(NodeOptions* options)
  95. {
  96. int i = 0;
  97. for (i = 0; i < options->count; i++)
  98. nodeOptionDelete(&options->entries[i]);
  99. mmfree(options->entries);
  100. mmfree(options);
  101. }
  102. void nodeOptionsResize(void* context, NodeOptions* options, int size)
  103. {
  104. NodeOption* originalEntries = options->entries;
  105. int originalSize = options->bufferSize;
  106. int originalCount = options->count;
  107. int i = 0;
  108. int elementsToCopy = originalSize;
  109. int sizeToCopy = 0;
  110. options->bufferSize = size;
  111. if (options->count > options->bufferSize)
  112. options->count = options->bufferSize;
  113. if (elementsToCopy > size)
  114. elementsToCopy = size;
  115. sizeToCopy = elementsToCopy * sizeof(NodeOption);
  116. options->entries = (NodeOption*)mmalloc(context, sizeof(NodeOption) * options->bufferSize);
  117. memcpy(options->entries, originalEntries, sizeToCopy);
  118. memset(options->entries + elementsToCopy, 0, sizeof(NodeOption) * options->bufferSize - sizeToCopy);
  119. mmfree(originalEntries);
  120. }
  121. void nodeOptionsGrowIfNeeded(void* context, NodeOptions* options)
  122. {
  123. static const int BUFFER_GROW = 10;
  124. if (options->count == options->bufferSize)
  125. nodeOptionsResize(context, options, options->bufferSize + BUFFER_GROW);
  126. }
  127. void nodeOptionsAdd(void* context, NodeOptions* options, const NodeOption* option)
  128. {
  129. nodeOptionsGrowIfNeeded(context, options);
  130. options->entries[options->count] = *option;
  131. options->count++;
  132. }
  133. ASTFXNode* nodeCreate(void* context, NodeType type)
  134. {
  135. ASTFXNode* node = (ASTFXNode*)mmalloc(context, sizeof(ASTFXNode));
  136. node->options = nodeOptionsCreate(context);
  137. node->type = type;
  138. return node;
  139. }
  140. void nodeDelete(ASTFXNode* node)
  141. {
  142. nodeOptionsDelete(node->options);
  143. mmfree(node);
  144. }
  145. void nodePush(ParseState* parseState, ASTFXNode* node)
  146. {
  147. NodeLink* linkNode = (NodeLink*)mmalloc(parseState->memContext, sizeof(NodeLink));
  148. linkNode->next = parseState->nodeStack;
  149. linkNode->node = node;
  150. parseState->nodeStack = linkNode;
  151. parseState->topNode = node;
  152. }
  153. void nodePop(ParseState* parseState)
  154. {
  155. if (!parseState->nodeStack)
  156. return;
  157. NodeLink* toRemove = parseState->nodeStack;
  158. parseState->nodeStack = toRemove->next;
  159. if (parseState->nodeStack)
  160. parseState->topNode = parseState->nodeStack->node;
  161. else
  162. parseState->topNode = 0;
  163. mmfree(toRemove);
  164. }
  165. ParseState* parseStateCreate()
  166. {
  167. ParseState* parseState = (ParseState*)malloc(sizeof(ParseState));
  168. parseState->memContext = mmalloc_new_context();
  169. parseState->rootNode = nodeCreate(parseState->memContext, NT_Shader);
  170. parseState->topNode = 0;
  171. parseState->nodeStack = 0;
  172. parseState->hasError = 0;
  173. parseState->errorLine = 0;
  174. parseState->errorColumn = 0;
  175. parseState->errorMessage = 0;
  176. nodePush(parseState, parseState->rootNode);
  177. return parseState;
  178. }
  179. void parseStateDelete(ParseState* parseState)
  180. {
  181. while (parseState->nodeStack != 0)
  182. nodePop(parseState);
  183. nodeDelete(parseState->rootNode);
  184. mmalloc_free_context(parseState->memContext);
  185. free(parseState);
  186. }