BsSLFXCompiler.h 9.9 KB

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