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