RenderPath.h 9.4 KB

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