BsSLFXCompiler.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #pragma once
  4. #include "BsSLPrerequisites.h"
  5. #include "Material/BsShader.h"
  6. #include "RenderAPI/BsGpuProgram.h"
  7. #include "RenderAPI/BsRasterizerState.h"
  8. #include "RenderAPI/BsDepthStencilState.h"
  9. #include "RenderAPI/BsBlendState.h"
  10. extern "C" {
  11. #include "BsASTFX.h"
  12. }
  13. namespace bs
  14. {
  15. /** @addtogroup BansheeSL
  16. * @{
  17. */
  18. /** Contains the results of compilation returned from the BSLFXCompiler. */
  19. struct BSLFXCompileResult
  20. {
  21. SPtr<Shader> shader; /**< Resulting shader if compilation was successful. Null if error occurred. */
  22. String errorMessage; /**< Error message if compilation failed. */
  23. int errorLine = 0; /**< Line of the error if one occurred. */
  24. int errorColumn = 0; /**< Column of the error if one occurred. */
  25. String errorFile; /**< File in which the error occurred. Empty if root file. */
  26. };
  27. /** Transforms a source file written in BSL FX syntax into a Shader object. */
  28. class BSLFXCompiler
  29. {
  30. /** Possible types of code blocks within a shader. */
  31. enum class CodeBlockType
  32. {
  33. Vertex, Fragment, Geometry, Hull, Domain, Compute, Common
  34. };
  35. /** Temporary data describing a pass during parsing. */
  36. struct PassData
  37. {
  38. BLEND_STATE_DESC blendDesc;
  39. RASTERIZER_STATE_DESC rasterizerDesc;
  40. DEPTH_STENCIL_STATE_DESC depthStencilDesc;
  41. UINT32 stencilRefValue = 0;
  42. UINT32 seqIdx = 0;
  43. bool blendIsDefault = true;
  44. bool rasterizerIsDefault = true;
  45. bool depthStencilIsDefault = true;
  46. String code; // Parsed code block
  47. String vertexCode;
  48. String fragmentCode;
  49. String geometryCode;
  50. String hullCode;
  51. String domainCode;
  52. String computeCode;
  53. };
  54. /** Information about different variations of a single technique. */
  55. struct VariationData
  56. {
  57. String identifier;
  58. Vector<UINT32> values;
  59. };
  60. /** Information describing a technique, without the actual contents. */
  61. struct TechniqueMetaData
  62. {
  63. String name;
  64. Vector<String> includes;
  65. bool isMixin;
  66. String language;
  67. String featureSet;
  68. Vector<StringID> tags;
  69. Vector<VariationData> variations;
  70. };
  71. /** Temporary data for describing a technique during parsing. */
  72. struct TechniqueData
  73. {
  74. TechniqueMetaData metaData;
  75. Vector<PassData> passes;
  76. };
  77. public:
  78. /** Transforms a source file written in BSL FX syntax into a Shader object. */
  79. static BSLFXCompileResult compile(const String& name, const String& source,
  80. const UnorderedMap<String, String>& defines);
  81. private:
  82. /** Converts the provided source into an abstract syntax tree using the lexer & parser for BSL FX syntax. */
  83. static BSLFXCompileResult parseFX(ParseState* parseState, const char* source,
  84. const UnorderedMap<String, String>& defines);
  85. /** Parses the technique node and outputs the relevant meta-data. */
  86. static TechniqueMetaData parseTechniqueMetaData(ASTFXNode* technique);
  87. /** Parses technique variations and writes them to the provided meta-data object. */
  88. static void parseVariations(TechniqueMetaData& metaData, ASTFXNode* variations);
  89. /** Maps BSL queue sort type enum into in-engine queue sort type mode. */
  90. static QueueSortType parseSortType(CullAndSortModeValue sortType);
  91. /** Maps BSL comparison function enum into in-engine compare function. */
  92. static CompareFunction parseCompFunc(CompFuncValue compFunc);
  93. /** Maps BSL operation to in-engine blend factor. */
  94. static BlendFactor parseBlendFactor(OpValue factor);
  95. /** Maps BSL blend operation to in-engine blend operation. */
  96. static BlendOperation parseBlendOp(BlendOpValue op);
  97. /** Maps BSL operation to in-engine stencil operation. */
  98. static StencilOperation parseStencilOp(OpValue op);
  99. /** Maps BSL cull mode enum to in-engine cull mode. */
  100. static CullingMode parseCullMode(CullAndSortModeValue cm);
  101. /** Maps BSL fill mode enum to in-engine fill mode. */
  102. static PolygonMode parseFillMode(FillModeValue fm);
  103. /**
  104. * Populates the front facing operation portion of the depth-stencil state descriptor from the provided stencil-op
  105. * AST node.
  106. */
  107. static void parseStencilFront(DEPTH_STENCIL_STATE_DESC& desc, ASTFXNode* stencilOpNode);
  108. /**
  109. * Populates the back backing operation portion of the depth-stencil state descriptor from the provided stencil-op
  110. * AST node.
  111. */
  112. static void parseStencilBack(DEPTH_STENCIL_STATE_DESC& desc, ASTFXNode* stencilOpNode);
  113. /** Populates the color (RGB) portion of the blend state descriptor from the provided blend definition AST node. */
  114. static void parseColorBlendDef(RENDER_TARGET_BLEND_STATE_DESC& desc, ASTFXNode* blendDefNode);
  115. /** Populates the alpha portion of the blend state descriptor from the provided blend definition AST node. */
  116. static void parseAlphaBlendDef(RENDER_TARGET_BLEND_STATE_DESC& desc, ASTFXNode* blendDefNode);
  117. /**
  118. * Populates blend state descriptor for a single render target from the provided AST node. Which target gets
  119. * updated depends on the index set in the AST node.
  120. */
  121. static void parseRenderTargetBlendState(BLEND_STATE_DESC& desc, ASTFXNode* targetNode);
  122. /**
  123. * Parses the blend state AST node and populates the pass' blend state descriptor. Returns false if the descriptor
  124. * wasn't modified.
  125. */
  126. static bool parseBlendState(PassData& passData, ASTFXNode* blendNode);
  127. /**
  128. * Parses the rasterizer state AST node and populates the pass' rasterizer state descriptor. Returns false if the
  129. * descriptor wasn't modified.
  130. */
  131. static bool parseRasterizerState(PassData& passData, ASTFXNode* rasterNode);
  132. /**
  133. * Parses the depth state AST node and populates the pass' depth-stencil state descriptor. Returns false if the
  134. * descriptor wasn't modified.
  135. */
  136. static bool parseDepthState(PassData& passData, ASTFXNode* depthNode);
  137. /**
  138. * Parses the stencil state AST node and populates the pass' depth-stencil state descriptor. Returns false if the
  139. * descriptor wasn't modified.
  140. */
  141. static bool parseStencilState(PassData& passData, ASTFXNode* stencilNode);
  142. /**
  143. * Parses a code AST node and outputs the result in one of the streams within the provided pass data.
  144. *
  145. * @param[in] codeNode AST node to parse
  146. * @param[in] codeBlocks GPU program source code.
  147. * @param[in] passData Pass data containing temporary pass data, including the code streams that the code
  148. * block code will be written to.
  149. */
  150. static void parseCodeBlock(ASTFXNode* codeNode, const Vector<String>& codeBlocks, PassData& passData);
  151. /**
  152. * Parses the pass AST node and populates the provided @p passData with all relevant pass parameters.
  153. *
  154. * @param[in] passNode Node to parse.
  155. * @param[in] codeBlocks GPU program source code.
  156. * @param[out] passData Will contain pass data after parsing.
  157. */
  158. static void parsePass(ASTFXNode* passNode, const Vector<String>& codeBlocks, PassData& passData);
  159. /**
  160. * Parses the technique AST node and generates a single technique object.
  161. *
  162. * @param[in] techniqueNode Node to parse.
  163. * @param[in] codeBlocks GPU program source code.
  164. * @param[out] techniqueData Will contain technique data after parsing.
  165. */
  166. static void parseTechnique(ASTFXNode* techniqueNode, const Vector<String>& codeBlocks, TechniqueData& techniqueData);
  167. /**
  168. * Parser the options AST node that contains global shader options.
  169. *
  170. * @param[in] optionsNode Node to parse.
  171. * @param[in] shaderDesc Descriptor to apply the found options to.
  172. */
  173. static void parseOptions(ASTFXNode* optionsNode, SHADER_DESC& shaderDesc);
  174. /**
  175. * Parses the AST node hierarchy and generates a variation of the of the named technique.
  176. *
  177. * @param[in, out] parseState Parser state object that has previously been initialized with the AST using
  178. * parseFX().
  179. * @param[in] name Name of the technique to generate the variation for.
  180. * @param[in] codeBlocks Blocks containing GPU program source code that are referenced by the AST.
  181. * @param[in] variation Shader variation the AST was parsed with.
  182. * @param[out] techniques Vector to append newly found techniques to.
  183. * @param[out] includes Set to append newly found includes to.
  184. * @param[out] shaderDesc Shader descriptor too append new parameters to.
  185. * @return A result object containing an error message if not successful.
  186. */
  187. static BSLFXCompileResult parseTechnique(ParseState* parseState, const String& name,
  188. const Vector<String>& codeBlocks, const ShaderVariation& variation, Vector<SPtr<Technique>>& techniques,
  189. UnorderedSet<String>& includes, SHADER_DESC& shaderDesc);
  190. /**
  191. * Converts a null-terminated string into a standard string, and eliminates quotes that are assumed to be at the
  192. * first and last index.
  193. */
  194. static String removeQuotes(const char* input);
  195. };
  196. /** @} */
  197. }