RenderPath.h 9.1 KB

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