BsSLFXCompiler.h 14 KB

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