BsSLFXCompiler.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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. };
  23. /** Transforms a source file written in BSL FX syntax into a Shader object. */
  24. class BSLFXCompiler
  25. {
  26. /** Possible types of code blocks within a shader. */
  27. enum class CodeBlockType
  28. {
  29. Vertex, Fragment, Geometry, Hull, Domain, Compute, Common
  30. };
  31. /**
  32. * Represents a block of code written in a GPU program language for a specific GPU program type. (i.e. non-FX code)
  33. */
  34. struct CodeBlock
  35. {
  36. CodeBlockType type;
  37. String code;
  38. };
  39. /** Temporary data describing a pass during parsing. */
  40. struct PassData
  41. {
  42. BLEND_STATE_DESC blendDesc;
  43. RASTERIZER_STATE_DESC rasterizerDesc;
  44. DEPTH_STENCIL_STATE_DESC depthStencilDesc;
  45. bool blendIsDefault = true;
  46. bool rasterizerIsDefault = true;
  47. bool depthStencilIsDefault = true;
  48. String commonCode;
  49. String vertexCode;
  50. String fragmentCode;
  51. String geometryCode;
  52. String hullCode;
  53. String domainCode;
  54. String computeCode;
  55. };
  56. public:
  57. /** Transforms a source file written in BSL FX syntax into a Shader object. */
  58. static BSLFXCompileResult compile(const String& source);
  59. private:
  60. /** Converts the provided source into an abstract syntax tree using the lexer & parser for BSL FX syntax. */
  61. static void parseFX(ParseState* parseState, const char* source);
  62. /**
  63. * Retrieves non-FX code blocks (i.e. actual shader code) from the source code and removes them from the input so
  64. * all that remains is a pure FX code. Found blocks are returned so they may be compiled using their appropriate
  65. * compiler.
  66. */
  67. static Vector<CodeBlock> parseCodeBlocks(String& source);
  68. /**
  69. * Merges two shader AST nodes. The first node will contain the combination of both after the method executes.
  70. *
  71. * @param[in, out] mergeInto Parse state containing the node to be merged into.
  72. * @param[in] mergeFrom Second of the nodes to be merged. All the contents of this node will be merged
  73. * into the first node. This node and its children will remain unmodified.
  74. * @param[in] codeBlockOffset Offset to apply to any code block indexes belonging to the second node.
  75. */
  76. static void mergeAST(ParseState* mergeInto, ASTFXNode* mergeFrom, UINT32 codeBlockOffset);
  77. /**
  78. * Finds the blocks node in the root node of the provided parse state, and merges any entries from @p blocksNode
  79. * into it. A new node is created if one doesn't exist.
  80. */
  81. static void mergeBlocks(ParseState* mergeInto, ASTFXNode* blocksNode);
  82. /**
  83. * Finds the parameters node in the root node of the provided parse state, and merges any entries from
  84. * @p paramsNode into it. A new node is created if one doesn't exist.
  85. */
  86. static void mergeParameters(ParseState* mergeInto, ASTFXNode* paramsNode);
  87. /**
  88. * Retrieves the renderer and language specified for the technique. These two values are considered a unique
  89. * identifier for a technique.
  90. */
  91. static void getTechniqueIdentifier(ASTFXNode* technique, StringID& renderer, String& language);
  92. /** Checks if two techniques can be matched based on the options specified in their child nodes. */
  93. static bool isTechniqueMergeValid(ASTFXNode* into, ASTFXNode* from);
  94. /**
  95. * Copies an existing AST node option and inserts it into another node options list.
  96. *
  97. * @param[in, out] into Parse state of the into which the node option will be inserted to.
  98. * @param[in] parent A set of node options to insert the node option copy into.
  99. * @param[in] option Node option to copy.
  100. * @return True if the copied node was a complex type.
  101. */
  102. static bool copyNodeOption(ParseState* into, NodeOptions* parent, NodeOption* option);
  103. /**
  104. * Merges pass states and code blocks. All code blocks from @p mergeFromNode will have their indexes incremented by
  105. * @p codeBlockOffset.
  106. */
  107. static void mergePass(ParseState* mergeInto, ASTFXNode* mergeIntoNode, ASTFXNode* mergeFromNode,
  108. UINT32 codeBlockOffset);
  109. /**
  110. * Merges code blocks. All code blocks from @p mergeFromNode will have their indexes incremented by
  111. * @p codeBlockOffset.
  112. */
  113. static void mergeCode(ParseState* mergeInto, ASTFXNode* mergeIntoNode, ASTFXNode* mergeFromNode,
  114. UINT32 codeBlockOffset);
  115. /**
  116. * Merges all pass states by copying all child nodes and their options to the destination node.
  117. *
  118. * @note
  119. * Certain node types are ignored as we handle their merging specially. Should only be called on Technique nodes or
  120. * its children.
  121. */
  122. static void mergePassStates(ParseState* mergeInto, ASTFXNode* mergeIntoNode, ASTFXNode* mergeFromNode);
  123. /**
  124. * Merges two techniques. All technique states, code blocks and passes will be merged. Passes will be merged
  125. * according to the pass index (new passes will be inserted if the destination doesn't already have a pass with an
  126. * index belonging to the source pass). All code blocks from @p mergeFromNode will have their indexes incremented
  127. * by @p codeBlockOffset.
  128. */
  129. static void mergeTechnique(ParseState* mergeInto, ASTFXNode* mergeIntoNode, ASTFXNode* mergeFromNode,
  130. UINT32 codeBlockOffset);
  131. /**
  132. * Find matching techniques from the root shader node in @p mergeInto and merges them with @p techniqueNode.
  133. * All code blocks from @p techniqueNode will have their indexes incremented by @p codeBlockOffset.
  134. *
  135. * @see BSLFXCompiler::mergeTechnique
  136. */
  137. static void mergeTechniques(ParseState* mergeInto, ASTFXNode* techniqueNode, UINT32 codeBlockOffset);
  138. /** Converts FX renderer name into an in-engine renderer identifier. */
  139. static StringID parseRenderer(const String& name);
  140. /**
  141. * Converts FX language into an in-engine shader language (e.g. hlsl, glsl) and a rendering API that supports the
  142. * provided language.
  143. */
  144. static void parseLanguage(const String& name, StringID& renderAPI, String& language);
  145. /** Maps FX buffer usage enum into in-engine param block usage. */
  146. static GpuParamBlockUsage parseBlockUsage(BufferUsageValue usage);
  147. /** Maps FX filter mode enum into in-engine filter mode. */
  148. static UINT32 parseFilterMode(FilterValue filter);
  149. /** Maps FX comparison function enum into in-engine compare function. */
  150. static CompareFunction parseCompFunc(CompFuncValue compFunc);
  151. /** Maps FX addressing mode enum into in-engine addressing mode. */
  152. static TextureAddressingMode parseAddrMode(AddrModeValue addrMode);
  153. /** Maps FX operation to in-engine blend factor. */
  154. static BlendFactor parseBlendFactor(OpValue factor);
  155. /** Maps FX blend operation to in-engine blend operation. */
  156. static BlendOperation parseBlendOp(BlendOpValue op);
  157. /**
  158. * Maps FX parameter type to in-engine shader parameter.
  159. *
  160. * @param[in] type Input FX parameter type.
  161. * @param[in] isObjType Output parameter signaling whether the in-engine parameter is a data or an object type.
  162. * @param[in] typeId Type ID corresponding to a value of in-game GpuParamDataType or GpuParamObjectType
  163. * enum (depending on isObjType()).
  164. */
  165. static void parseParamType(ParamType type, bool& isObjType, UINT32& typeId);
  166. /** Maps FX operation to in-engine stencil operation. */
  167. static StencilOperation parseStencilOp(OpValue op);
  168. /** Maps FX cull mode enum to in-engine cull mode. */
  169. static CullingMode parseCullMode(CullModeValue cm);
  170. /** Maps FX fill mode enum to in-engine fill mode. */
  171. static PolygonMode parseFillMode(FillModeValue fm);
  172. /**
  173. * Populates the front facing operation portion of the depth-stencil state descriptor from the provided stencil-op
  174. * AST node.
  175. */
  176. static void parseStencilFront(DEPTH_STENCIL_STATE_DESC& desc, ASTFXNode* stencilOpNode);
  177. /**
  178. * Populates the back backing operation portion of the depth-stencil state descriptor from the provided stencil-op
  179. * AST node.
  180. */
  181. static void parseStencilBack(DEPTH_STENCIL_STATE_DESC& desc, ASTFXNode* stencilOpNode);
  182. /**
  183. * Populates the addressing mode portion of the sampler state descriptor for U/V/W axes from the provided addressing
  184. * mode AST node.
  185. */
  186. static void parseAddrMode(SAMPLER_STATE_DESC& desc, ASTFXNode* addrModeNode);
  187. /** Populates the color (RGB) portion of the blend state descriptor from the provided blend definition AST node. */
  188. static void parseColorBlendDef(RENDER_TARGET_BLEND_STATE_DESC& desc, ASTFXNode* blendDefNode);
  189. /** Populates the alpha portion of the blend state descriptor from the provided blend definition AST node. */
  190. static void parseAlphaBlendDef(RENDER_TARGET_BLEND_STATE_DESC& desc, ASTFXNode* blendDefNode);
  191. /**
  192. * Populates blend state descriptor for a single render target from the provided AST node. Which target gets
  193. * updated depends on the index set in the AST node.
  194. */
  195. static void parseRenderTargetBlendState(BLEND_STATE_DESC& desc, ASTFXNode* targetNode);
  196. /**
  197. * Parses the blend state AST node and outputs a blend state descriptor. Returns false if the descriptor wasn't
  198. * modified.
  199. */
  200. static bool parseBlendState(BLEND_STATE_DESC& desc, ASTFXNode* passNode);
  201. /**
  202. * Parses the rasterizer state AST node and outputs a rasterizer state descriptor. Returns false if the descriptor
  203. * wasn't modified.
  204. */
  205. static bool parseRasterizerState(RASTERIZER_STATE_DESC& desc, ASTFXNode* passNode);
  206. /**
  207. * Parses the depth-stencil state AST node and outputs a depth-stencil state descriptor. Returns false if the
  208. * descriptor wasn't modified.
  209. */
  210. static bool parseDepthStencilState(DEPTH_STENCIL_STATE_DESC& desc, ASTFXNode* passNode);
  211. /** Parses the sampler state AST node and outputs a sampler state object, or a nullptr in case AST node is empty. */
  212. static SamplerStatePtr parseSamplerState(ASTFXNode* samplerStateNode);
  213. /**
  214. * Parses a code AST node and outputs the result in one of the streams within the provided pass data.
  215. *
  216. * @param[in] codeNode AST node to parse
  217. * @param[in] codeBlocks GPU program source code retrieved from parseCodeBlocks().
  218. * @param[in] passData Pass data containing temporary pass data, including the code streams that the code
  219. * block code will be written to.
  220. */
  221. static void parseCodeBlock(ASTFXNode* codeNode, const Vector<CodeBlock>& codeBlocks, PassData& passData);
  222. /**
  223. * Parses the pass AST node and generates a single pass object. Returns null if no pass can be parsed. This method
  224. * will generate any child state objects and compile any child GPU programs.
  225. *
  226. * @param[in] passNode Node to parse.
  227. * @param[in] codeBlocks GPU program source code retrieved from parseCodeBlocks().
  228. * @param[in] passData Data containing pass render state descriptors.
  229. * @param[in] renderAPI API to use for compiling the GPU programs.
  230. * @param[in] language GPU program language to use for parsing the provided code blocks.
  231. * @param[in] seqIdx Output sequential index of the pass that determines its rendering order.
  232. */
  233. static PassPtr parsePass(ASTFXNode* passNode, const Vector<CodeBlock>& codeBlocks, PassData& passData,
  234. const StringID& renderAPI, const String& language, UINT32& seqIdx);
  235. /**
  236. * Parses the technique AST node and generates a single technique object. Returns null if no technique can be
  237. * parsed.
  238. *
  239. * @param[in] techniqueNode Node to parse.
  240. * @param[in] codeBlocks GPU program source code retrieved from parseCodeBlocks().
  241. */
  242. static TechniquePtr parseTechnique(ASTFXNode* techniqueNode, const Vector<CodeBlock>& codeBlocks);
  243. /**
  244. * Parses the parameters AST node and populates the shader descriptor with information about GPU program parameters
  245. * and their default values.
  246. */
  247. static void parseParameters(SHADER_DESC& desc, ASTFXNode* parametersNode);
  248. /**
  249. * Parses the blocks AST node and populates the shader descriptor with information about GPU program parameter
  250. * blocks.
  251. */
  252. static void parseBlocks(SHADER_DESC& desc, ASTFXNode* blocksNode);
  253. /**
  254. * Parses the AST node hierarchy and generates a shader object.
  255. *
  256. * @param[in] name Optional name for the shader.
  257. * @param[in, out] parseState Parser state object that has previously been initialized with the AST using
  258. * parseFX().
  259. * @param codeBlocks GPU program source code retrieved from parseCodeBlocks().
  260. * @return A result object containing the shader if successful, or error message if not.
  261. */
  262. static BSLFXCompileResult parseShader(const String& name, ParseState* parseState, Vector<CodeBlock>& codeBlocks);
  263. /**
  264. * Converts a null-terminated string into a standard string, and eliminates quotes that are assumed to be at the
  265. * first and last index.
  266. */
  267. static String removeQuotes(const char* input);
  268. /** Retrieves a GPU program profile to use with the specified API and GPU program type. */
  269. static GpuProgramProfile getProfile(const StringID& renderAPI, GpuProgramType type);
  270. /** Returns one of the builtin textures based on their name. */
  271. static HTexture getBuiltinTexture(const String& name);
  272. };
  273. }