BsSLFXCompiler.h 13 KB

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