RenderPath.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. // Copyright (c) 2008-2023 the Urho3D project
  2. // License: MIT
  3. /// \file
  4. #pragma once
  5. #include "../Container/Ptr.h"
  6. #include "../Container/RefCounted.h"
  7. #include "../Core/Variant.h"
  8. #include "../GraphicsAPI/GraphicsDefs.h"
  9. #include "../Math/Color.h"
  10. #include "../Math/Vector4.h"
  11. namespace Urho3D
  12. {
  13. class Variant;
  14. class XMLElement;
  15. class XMLFile;
  16. /// Rendering path command types.
  17. enum RenderCommandType
  18. {
  19. CMD_NONE = 0,
  20. CMD_CLEAR,
  21. CMD_SCENEPASS,
  22. CMD_QUAD,
  23. CMD_FORWARDLIGHTS,
  24. CMD_LIGHTVOLUMES,
  25. CMD_RENDERUI,
  26. CMD_SENDEVENT
  27. };
  28. /// Rendering path sorting modes.
  29. enum RenderCommandSortMode
  30. {
  31. SORT_FRONTTOBACK = 0,
  32. SORT_BACKTOFRONT
  33. };
  34. /// Rendertarget size mode.
  35. enum RenderTargetSizeMode
  36. {
  37. SIZE_ABSOLUTE = 0,
  38. SIZE_VIEWPORTDIVISOR,
  39. SIZE_VIEWPORTMULTIPLIER
  40. };
  41. /// Rendertarget definition.
  42. struct URHO3D_API RenderTargetInfo
  43. {
  44. /// Read from an XML element.
  45. void Load(const XMLElement& element);
  46. /// Name.
  47. String name_;
  48. /// Tag name.
  49. String tag_;
  50. /// Texture format.
  51. unsigned format_{};
  52. /// Absolute size or multiplier.
  53. Vector2 size_;
  54. /// Size mode.
  55. RenderTargetSizeMode sizeMode_{SIZE_ABSOLUTE};
  56. /// Multisampling level (1 = no multisampling).
  57. int multiSample_{1};
  58. /// Multisampling autoresolve flag.
  59. bool autoResolve_{true};
  60. /// Enabled flag.
  61. bool enabled_{true};
  62. /// Cube map flag.
  63. bool cubemap_{};
  64. /// Filtering flag.
  65. bool filtered_{};
  66. /// sRGB sampling/writing mode flag.
  67. bool sRGB_{};
  68. /// Should be persistent and not shared/reused between other buffers of same size.
  69. bool persistent_{};
  70. };
  71. /// Rendering path command.
  72. struct URHO3D_API RenderPathCommand
  73. {
  74. /// Read from an XML element.
  75. void Load(const XMLElement& element);
  76. /// Set a texture resource name. Can also refer to a rendertarget defined in the rendering path.
  77. /// @property{set_textureNames}
  78. void SetTextureName(TextureUnit unit, const String& name);
  79. /// Set a shader parameter.
  80. /// @property{set_shaderParameters}
  81. void SetShaderParameter(const String& name, const Variant& value);
  82. /// Remove a shader parameter.
  83. void RemoveShaderParameter(const String& name);
  84. /// Set number of output rendertargets.
  85. /// @property
  86. void SetNumOutputs(i32 num);
  87. /// Set output rendertarget name and face index for cube maps.
  88. void SetOutput(i32 index, const String& name, CubeMapFace face = FACE_POSITIVE_X);
  89. /// Set output rendertarget name.
  90. /// @property{set_outputNames}
  91. void SetOutputName(i32 index, const String& name);
  92. /// Set output rendertarget face index for cube maps.
  93. /// @property{set_outputFaces}
  94. void SetOutputFace(i32 index, CubeMapFace face);
  95. /// Set depth-stencil output name. When empty, will assign a depth-stencil buffer automatically.
  96. /// @property
  97. void SetDepthStencilName(const String& name);
  98. /// Return texture resource name.
  99. /// @property{get_textureNames}
  100. const String& GetTextureName(TextureUnit unit) const;
  101. /// Return shader parameter.
  102. /// @property{get_shaderParameters}
  103. const Variant& GetShaderParameter(const String& name) const;
  104. /// Return number of output rendertargets.
  105. /// @property
  106. i32 GetNumOutputs() const { return outputs_.Size(); }
  107. /// Return output rendertarget name.
  108. /// @property{get_outputNames}
  109. const String& GetOutputName(i32 index) const;
  110. /// Return output rendertarget face index.
  111. /// @property{get_outputFaces}
  112. CubeMapFace GetOutputFace(i32 index) const;
  113. /// Return depth-stencil output name.
  114. /// @property
  115. const String& GetDepthStencilName() const { return depthStencilName_; }
  116. /// Tag name.
  117. String tag_;
  118. /// Command type.
  119. RenderCommandType type_{};
  120. /// Sorting mode.
  121. RenderCommandSortMode sortMode_{};
  122. /// Scene pass name.
  123. String pass_;
  124. /// Scene pass index. Filled by View.
  125. unsigned passIndex_{};
  126. /// Command/pass metadata.
  127. String metadata_;
  128. /// Vertex shader name.
  129. String vertexShaderName_;
  130. /// Pixel shader name.
  131. String pixelShaderName_;
  132. /// Vertex shader defines.
  133. String vertexShaderDefines_;
  134. /// Pixel shader defines.
  135. String pixelShaderDefines_;
  136. /// Textures.
  137. String textureNames_[MAX_TEXTURE_UNITS];
  138. /// %Shader parameters.
  139. HashMap<StringHash, Variant> shaderParameters_;
  140. /// Output rendertarget names and faces.
  141. Vector<Pair<String, CubeMapFace>> outputs_;
  142. /// Depth-stencil output name.
  143. String depthStencilName_;
  144. /// Clear flags. Affects clear command only.
  145. ClearTargetFlags clearFlags_{};
  146. /// Clear color. Affects clear command only.
  147. Color clearColor_;
  148. /// Clear depth. Affects clear command only.
  149. float clearDepth_{};
  150. /// Clear stencil value. Affects clear command only.
  151. unsigned clearStencil_{};
  152. /// Blend mode. Affects quad command only.
  153. BlendMode blendMode_{BLEND_REPLACE};
  154. /// Enabled flag.
  155. bool enabled_{true};
  156. /// Use fog color for clearing.
  157. bool useFogColor_{};
  158. /// Mark to stencil flag.
  159. bool markToStencil_{};
  160. /// Use lit base pass optimization for forward per-pixel lights.
  161. bool useLitBase_{true};
  162. /// Vertex lights flag.
  163. bool vertexLights_{};
  164. /// Event name.
  165. String eventName_;
  166. };
  167. /// Rendering path definition. A sequence of commands (e.g. clear screen, draw objects with specific pass) that yields the scene rendering result.
  168. class URHO3D_API RenderPath : public RefCounted
  169. {
  170. public:
  171. /// Construct.
  172. RenderPath();
  173. /// Destruct.
  174. ~RenderPath() override;
  175. /// Clone the rendering path.
  176. SharedPtr<RenderPath> Clone();
  177. /// Clear existing data and load from an XML file. Return true if successful.
  178. bool Load(XMLFile* file);
  179. /// Append data from an XML file. Return true if successful.
  180. bool Append(XMLFile* file);
  181. /// Enable/disable commands and rendertargets by tag.
  182. void SetEnabled(const String& tag, bool active);
  183. /// Return true of any of render targets or commands with specified tag are enabled.
  184. /// @property
  185. bool IsEnabled(const String& tag) const;
  186. /// Return true if renderpath or command with given tag exists.
  187. /// @property
  188. bool IsAdded(const String& tag) const;
  189. /// Toggle enabled state of commands and rendertargets by tag.
  190. void ToggleEnabled(const String& tag);
  191. /// Assign rendertarget at index.
  192. /// @property{set_renderTargets}
  193. void SetRenderTarget(unsigned index, const RenderTargetInfo& info);
  194. /// Add a rendertarget.
  195. void AddRenderTarget(const RenderTargetInfo& info);
  196. /// Remove a rendertarget by index.
  197. void RemoveRenderTarget(unsigned index);
  198. /// Remove a rendertarget by name.
  199. void RemoveRenderTarget(const String& name);
  200. /// Remove rendertargets by tag name.
  201. void RemoveRenderTargets(const String& tag);
  202. /// Assign command at index.
  203. /// @property{set_commands}
  204. void SetCommand(unsigned index, const RenderPathCommand& command);
  205. /// Add a command to the end of the list.
  206. void AddCommand(const RenderPathCommand& command);
  207. /// Insert a command at a position.
  208. void InsertCommand(unsigned index, const RenderPathCommand& command);
  209. /// Remove a command by index.
  210. void RemoveCommand(unsigned index);
  211. /// Remove commands by tag name.
  212. void RemoveCommands(const String& tag);
  213. /// Set a shader parameter in all commands that define it.
  214. /// @property{set_shaderParameters}
  215. void SetShaderParameter(const String& name, const Variant& value);
  216. /// Return number of rendertargets.
  217. /// @property
  218. i32 GetNumRenderTargets() const { return renderTargets_.Size(); }
  219. /// Return number of commands.
  220. /// @property
  221. i32 GetNumCommands() const { return commands_.Size(); }
  222. /// Return command at index, or null if does not exist.
  223. RenderPathCommand* GetCommand(i32 index)
  224. {
  225. assert(index >= 0);
  226. return index < commands_.Size() ? &commands_[index] : nullptr;
  227. }
  228. /// Return a shader parameter (first appearance in any command).
  229. /// @property{get_shaderParameters}
  230. const Variant& GetShaderParameter(const String& name) const;
  231. /// Rendertargets.
  232. Vector<RenderTargetInfo> renderTargets_;
  233. /// Rendering commands.
  234. Vector<RenderPathCommand> commands_;
  235. };
  236. }