BsSLFXCompiler.h 14 KB

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