BsASTFX.c 5.5 KB

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