BsSLFXCompiler.h 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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 "BsShader.h"
  6. #include "BsGpuProgram.h"
  7. #include "BsRasterizerState.h"
  8. #include "BsDepthStencilState.h"
  9. #include "BsBlendState.h"
  10. extern "C" {
  11. #include "BsASTFX.h"
  12. }
  13. namespace BansheeEngine
  14. {
  15. /** Contains the results of compilation returned from the BSLFXCompiler. */
  16. struct BSLFXCompileResult
  17. {
  18. ShaderPtr shader; /**< Resulting shader if compilation was successful. Null if error occurred. */
  19. String errorMessage; /**< Error message if compilation failed. */
  20. int errorLine = 0; /**< Line of the error if one occurred. */
  21. int errorColumn = 0; /**< Column of the error if one occurred. */
  22. String errorFile; /**< File in which the error occurred. Empty if root file. */
  23. };
  24. /** Transforms a source file written in BSL FX syntax into a Shader object. */
  25. class BSLFXCompiler
  26. {
  27. /** Possible types of code blocks within a shader. */
  28. enum class CodeBlockType
  29. {
  30. Vertex, Fragment, Geometry, Hull, Domain, Compute, Common
  31. };
  32. /** Temporary data describing a pass during parsing. */
  33. struct PassData
  34. {
  35. BLEND_STATE_DESC blendDesc;
  36. RASTERIZER_STATE_DESC rasterizerDesc;
  37. DEPTH_STENCIL_STATE_DESC depthStencilDesc;
  38. UINT32 stencilRefValue = 0;
  39. UINT32 seqIdx = 0;
  40. bool blendIsDefault = true;
  41. bool rasterizerIsDefault = true;
  42. bool depthStencilIsDefault = true;
  43. String commonCode;
  44. String vertexCode;
  45. String fragmentCode;
  46. String geometryCode;
  47. String hullCode;
  48. String domainCode;
  49. String computeCode;
  50. };
  51. /** Temporary data for describing a technique during parsing. */
  52. struct TechniqueData
  53. {
  54. StringID renderer = RendererAny;
  55. StringID renderAPI = RenderAPIAny;
  56. String language;
  57. PassData commonPassData;
  58. Vector<PassData> passes;
  59. };
  60. public:
  61. /** Transforms a source file written in BSL FX syntax into a Shader object. */
  62. static BSLFXCompileResult compile(const String& source);
  63. private:
  64. /** Converts the provided source into an abstract syntax tree using the lexer & parser for BSL FX syntax. */
  65. static void parseFX(ParseState* parseState, const char* source);
  66. /**
  67. * Retrieves the renderer and language specified for the technique. These two values are considered a unique
  68. * identifier for a technique.
  69. */
  70. static void getTechniqueIdentifier(ASTFXNode* technique, StringID& renderer, String& language);
  71. /** Checks if two techniques can be matched based on the options specified in their child nodes. */
  72. static bool doTechniquesMatch(ASTFXNode* into, ASTFXNode* from);
  73. /** Converts FX renderer name into an in-engine renderer identifier. */
  74. static StringID parseRenderer(const String& name);
  75. /**
  76. * Converts FX language into an in-engine shader language (e.g. hlsl, glsl) and a rendering API that supports the
  77. * provided language.
  78. */
  79. static void parseLanguage(const String& name, StringID& renderAPI, String& language);
  80. /** Maps FX buffer usage enum into in-engine param block usage. */
  81. static GpuParamBlockUsage parseBlockUsage(BufferUsageValue usage);
  82. /** Maps FX filter mode enum into in-engine filter mode. */
  83. static UINT32 parseFilterMode(FilterValue filter);
  84. /** Maps FX comparison function enum into in-engine compare function. */
  85. static CompareFunction parseCompFunc(CompFuncValue compFunc);
  86. /** Maps FX addressing mode enum into in-engine addressing mode. */
  87. static TextureAddressingMode parseAddrMode(AddrModeValue addrMode);
  88. /** Maps FX operation to in-engine blend factor. */
  89. static BlendFactor parseBlendFactor(OpValue factor);
  90. /** Maps FX blend operation to in-engine blend operation. */
  91. static BlendOperation parseBlendOp(BlendOpValue op);
  92. /**
  93. * Maps FX parameter type to in-engine shader parameter.
  94. *
  95. * @param[in] type Input FX parameter type.
  96. * @param[in] isObjType Output parameter signaling whether the in-engine parameter is a data or an object type.
  97. * @param[in] typeId Type ID corresponding to a value of in-game GpuParamDataType or GpuParamObjectType
  98. * enum (depending on isObjType()).
  99. */
  100. static void parseParamType(ParamType type, bool& isObjType, UINT32& typeId);
  101. /** Maps FX operation to in-engine stencil operation. */
  102. static StencilOperation parseStencilOp(OpValue op);
  103. /** Maps FX cull mode enum to in-engine cull mode. */
  104. static CullingMode parseCullMode(CullModeValue cm);
  105. /** Maps FX fill mode enum to in-engine fill mode. */
  106. static PolygonMode parseFillMode(FillModeValue fm);
  107. /**
  108. * Populates the front facing operation portion of the depth-stencil state descriptor from the provided stencil-op
  109. * AST node.
  110. */
  111. static void parseStencilFront(DEPTH_STENCIL_STATE_DESC& desc, ASTFXNode* stencilOpNode);
  112. /**
  113. * Populates the back backing operation portion of the depth-stencil state descriptor from the provided stencil-op
  114. * AST node.
  115. */
  116. static void parseStencilBack(DEPTH_STENCIL_STATE_DESC& desc, ASTFXNode* stencilOpNode);
  117. /**
  118. * Populates the addressing mode portion of the sampler state descriptor for U/V/W axes from the provided addressing
  119. * mode AST node.
  120. */
  121. static void parseAddrMode(SAMPLER_STATE_DESC& desc, ASTFXNode* addrModeNode);
  122. /** Populates the color (RGB) portion of the blend state descriptor from the provided blend definition AST node. */
  123. static void parseColorBlendDef(RENDER_TARGET_BLEND_STATE_DESC& desc, ASTFXNode* blendDefNode);
  124. /** Populates the alpha portion of the blend state descriptor from the provided blend definition AST node. */
  125. static void parseAlphaBlendDef(RENDER_TARGET_BLEND_STATE_DESC& desc, ASTFXNode* blendDefNode);
  126. /**
  127. * Populates blend state descriptor for a single render target from the provided AST node. Which target gets
  128. * updated depends on the index set in the AST node.
  129. */
  130. static void parseRenderTargetBlendState(BLEND_STATE_DESC& desc, ASTFXNode* targetNode);
  131. /**
  132. * Parses the blend state AST node and outputs a blend state descriptor. Returns false if the descriptor wasn't
  133. * modified.
  134. */
  135. static bool parseBlendState(BLEND_STATE_DESC& desc, ASTFXNode* passNode);
  136. /**
  137. * Parses the rasterizer state AST node and outputs a rasterizer state descriptor. Returns false if the descriptor
  138. * wasn't modified.
  139. */
  140. static bool parseRasterizerState(RASTERIZER_STATE_DESC& desc, ASTFXNode* passNode);
  141. /**
  142. * Parses the depth-stencil state AST node and outputs a depth-stencil state descriptor. Returns false if the
  143. * descriptor wasn't modified.
  144. */
  145. static bool parseDepthStencilState(DEPTH_STENCIL_STATE_DESC& desc, ASTFXNode* passNode);
  146. /** Parses the sampler state AST node and outputs a sampler state object, or a nullptr in case AST node is empty. */
  147. static SamplerStatePtr parseSamplerState(ASTFXNode* samplerStateNode);
  148. /**
  149. * Parses a code AST node and outputs the result in one of the streams within the provided pass data.
  150. *
  151. * @param[in] codeNode AST node to parse
  152. * @param[in] codeBlocks GPU program source code.
  153. * @param[in] passData Pass data containing temporary pass data, including the code streams that the code
  154. * block code will be written to.
  155. */
  156. static void parseCodeBlock(ASTFXNode* codeNode, const Vector<String>& codeBlocks, PassData& passData);
  157. /**
  158. * Parses the pass AST node and populates the provided @passData with all relevant pass parameters.
  159. *
  160. * @param[in] passNode Node to parse.
  161. * @param[in] codeBlocks GPU program source code.
  162. * @param[out] passData Will contain pass data after parsing.
  163. */
  164. static void parsePass(ASTFXNode* passNode, const Vector<String>& codeBlocks, PassData& passData);
  165. /**
  166. * Parses the technique AST node and generates a single technique object. Returns null if no technique can be
  167. * parsed.
  168. *
  169. * @param[in] techniqueNode Node to parse.
  170. * @param[in] codeBlocks GPU program source code.
  171. * @param[out] techniqueData Will contain technique data after parsing.
  172. */
  173. static void parseTechnique(ASTFXNode* techniqueNode, const Vector<String>& codeBlocks, TechniqueData& techniqueData);
  174. /**
  175. * Parses the parameters AST node and populates the shader descriptor with information about GPU program parameters
  176. * and their default values.
  177. */
  178. static void parseParameters(SHADER_DESC& desc, ASTFXNode* parametersNode);
  179. /**
  180. * Parses the blocks AST node and populates the shader descriptor with information about GPU program parameter
  181. * blocks.
  182. */
  183. static void parseBlocks(SHADER_DESC& desc, ASTFXNode* blocksNode);
  184. /**
  185. * Parses the AST node hierarchy and generates a shader object.
  186. *
  187. * @param[in] name Optional name for the shader.
  188. * @param[in, out] parseState Parser state object that has previously been initialized with the AST using
  189. * parseFX().
  190. * @param codeBlocks GPU program source code.
  191. * @return A result object containing the shader if successful, or error message if not.
  192. */
  193. static BSLFXCompileResult parseShader(const String& name, ParseState* parseState, Vector<String>& codeBlocks);
  194. /**
  195. * Converts a null-terminated string into a standard string, and eliminates quotes that are assumed to be at the
  196. * first and last index.
  197. */
  198. static String removeQuotes(const char* input);
  199. /** Retrieves a GPU program profile to use with the specified API and GPU program type. */
  200. static GpuProgramProfile getProfile(const StringID& renderAPI, GpuProgramType type);
  201. /** Returns one of the builtin textures based on their name. */
  202. static HTexture getBuiltinTexture(const String& name);
  203. };
  204. }