1 /*
2 -----------------------------------------------------------------------------
3 This source file is part of OGRE
4     (Object-oriented Graphics Rendering Engine)
5 For the latest info, see http://www.ogre3d.org/
6 
7 Copyright (c) 2000-2014 Torus Knot Software Ltd
8 
9 Permission is hereby granted, free of charge, to any person obtaining a copy
10 of this software and associated documentation files (the "Software"), to deal
11 in the Software without restriction, including without limitation the rights
12 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 copies of the Software, and to permit persons to whom the Software is
14 furnished to do so, subject to the following conditions:
15 
16 The above copyright notice and this permission notice shall be included in
17 all copies or substantial portions of the Software.
18 
19 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 THE SOFTWARE.
26 -----------------------------------------------------------------------------
27 */
28 #ifndef __CompositorChain_H__
29 #define __CompositorChain_H__
30 
31 #include "OgrePrerequisites.h"
32 #include "OgreRenderTargetListener.h"
33 #include "OgreRenderQueueListener.h"
34 #include "OgreCompositorInstance.h"
35 #include "OgreViewport.h"
36 #include "OgreHeaderPrefix.h"
37 
38 namespace Ogre {
39     /** \addtogroup Core
40     *  @{
41     */
42     /** \addtogroup Effects
43     *  @{
44     */
45     /** Chain of compositor effects applying to one viewport.
46     */
47     class _OgreExport CompositorChain : public RenderTargetListener, public Viewport::Listener, public CompositorInstAlloc
48     {
49     public:
50         CompositorChain(Viewport *vp);
51         /** Another gcc warning here, which is no problem because RenderTargetListener is never used
52             to delete an object.
53         */
54         virtual ~CompositorChain();
55 
56         /// Data types
57         typedef std::vector<CompositorInstance*> Instances;
58         typedef VectorIterator<Instances> InstanceIterator;
59 
60         enum {
61             /// Identifier for best technique.
62             BEST = 0,
63             /// Identifier for "last" compositor in chain.
64             LAST = (size_t)-1,
65             NPOS = LAST
66         };
67 
68         /** Apply a compositor. Initially, the filter is enabled.
69         @param filter
70             Filter to apply.
71         @param addPosition
72             Position in filter chain to insert this filter at; defaults to the end (last applied filter).
73         @param scheme
74             Scheme to use (blank means default).
75         */
76         CompositorInstance* addCompositor(CompositorPtr filter, size_t addPosition=LAST, const String& scheme = BLANKSTRING);
77 
78         /** Remove a compositor.
79         @param position
80             Position in filter chain of filter to remove; defaults to the end (last applied filter)
81         */
82         void removeCompositor(size_t position=LAST);
83 
84         /// @deprecated use getCompositorInstances
85         OGRE_DEPRECATED size_t getNumCompositors();
86 
87         /** Remove all compositors.
88         */
89         void removeAllCompositors();
90 
91         /// @deprecated use getCompositorInstances
92         OGRE_DEPRECATED CompositorInstance *getCompositor(size_t index);
93 
94         /** Get compositor instance by name. Returns null if not found.
95         */
96         CompositorInstance* getCompositor(const String& name);
97 
98         /// Get compositor position by name. Returns #NPOS if not found.
99         size_t getCompositorPosition(const String& name);
100 
101         /** Get the original scene compositor instance for this chain (internal use).
102         */
_getOriginalSceneCompositor(void)103         CompositorInstance* _getOriginalSceneCompositor(void) { return mOriginalScene; }
104 
105         /** The compositor instances. The first compositor in this list is applied first, the last one is applied last.
106         */
getCompositorInstances()107         const Instances& getCompositorInstances() const { return mInstances; }
108 
109         /// @deprecated use getCompositorInstances
110         OGRE_DEPRECATED InstanceIterator getCompositors();
111 
112         /** Enable or disable a compositor, by position. Disabling a compositor stops it from rendering
113             but does not free any resources. This can be more efficient than using removeCompositor and
114             addCompositor in cases the filter is switched on and off a lot.
115         @param position
116             Position in filter chain of filter
117         */
118         void setCompositorEnabled(size_t position, bool state);
119 
120         void preRenderTargetUpdate(const RenderTargetEvent& evt);
121         void postRenderTargetUpdate(const RenderTargetEvent& evt);
122         void preViewportUpdate(const RenderTargetViewportEvent& evt);
123         void postViewportUpdate(const RenderTargetViewportEvent& evt);
124         void viewportCameraChanged(Viewport* viewport);
125         void viewportDimensionsChanged(Viewport* viewport);
126         void viewportDestroyed(Viewport* viewport);
127 
128         /** Mark state as dirty, and to be recompiled next frame.
129         */
130         void _markDirty();
131 
132         /** Get viewport that is the target of this chain
133         */
134         Viewport *getViewport();
135         /** Set viewport that is the target of this chain
136         */
137         void _notifyViewport(Viewport* vp);
138 
139         /** Remove a compositor by pointer. This is internally used by CompositionTechnique to
140             "weak" remove any instanced of a deleted technique.
141         */
142         void _removeInstance(CompositorInstance *i);
143 
144         /** Internal method for registering a queued operation for deletion later **/
145         void _queuedOperation(CompositorInstance::RenderSystemOperation* op);
146 
147         /** Compile this Composition chain into a series of RenderTarget operations.
148         */
149         void _compile();
150 
151         /** Get the previous instance in this chain to the one specified.
152         */
153         CompositorInstance* getPreviousInstance(CompositorInstance* curr, bool activeOnly = true);
154         /** Get the next instance in this chain to the one specified.
155         */
156         CompositorInstance* getNextInstance(CompositorInstance* curr, bool activeOnly = true);
157 
158     protected:
159         /// Viewport affected by this CompositorChain
160         Viewport *mViewport;
161 
162         /** Plainly renders the scene; implicit first compositor in the chain.
163         */
164         CompositorInstance *mOriginalScene;
165 
166         /// Postfilter instances in this chain
167         Instances mInstances;
168 
169         /// State needs recompile
170         bool mDirty;
171         /// Any compositors enabled?
172         bool mAnyCompositorsEnabled;
173 
174         String mOriginalSceneScheme;
175 
176         /// Compiled state (updated with _compile)
177         CompositorInstance::CompiledState mCompiledState;
178         CompositorInstance::TargetOperation mOutputOperation;
179         /// Render System operations queued by last compile, these are created by this
180         /// instance thus managed and deleted by it. The list is cleared with
181         /// clearCompilationState()
182         typedef std::vector<CompositorInstance::RenderSystemOperation*> RenderSystemOperations;
183         RenderSystemOperations mRenderSystemOperations;
184 
185         /** Clear compiled state */
186         void clearCompiledState();
187 
188         /** Prepare a viewport, the camera and the scene for a rendering operation
189         */
190         void preTargetOperation(CompositorInstance::TargetOperation &op, Viewport *vp, Camera *cam);
191 
192         /** Restore a viewport, the camera and the scene after a rendering operation
193         */
194         void postTargetOperation(CompositorInstance::TargetOperation &op, Viewport *vp, Camera *cam);
195 
196         void createOriginalScene();
197         void destroyOriginalScene();
198 
199         /// destroy internal resources
200         void destroyResources(void);
201 
202         /** Internal method to get a unique name of a compositor
203         */
204         const String getCompositorName() const;
205 
206         /** Render queue listener used to set up rendering events. */
207         class _OgreExport RQListener: public RenderQueueListener
208         {
209         public:
RQListener()210             RQListener() : mOperation(0), mSceneManager(0), mRenderSystem(0), mViewport(0) {}
211 
212             void renderQueueStarted(uint8 queueGroupId, const String& invocation, bool& skipThisInvocation);
213             void renderQueueEnded(uint8 queueGroupId, const String& invocation, bool& repeatThisInvocation);
214 
215             /** Set current operation and target. */
216             void setOperation(CompositorInstance::TargetOperation *op,SceneManager *sm,RenderSystem *rs);
217 
218             /** Notify current destination viewport. */
notifyViewport(Viewport * vp)219             void notifyViewport(Viewport* vp) { mViewport = vp; }
220 
221             /** Flush remaining render system operations. */
222             void flushUpTo(uint8 id);
223         private:
224             CompositorInstance::TargetOperation *mOperation;
225             SceneManager *mSceneManager;
226             RenderSystem *mRenderSystem;
227             Viewport* mViewport;
228             CompositorInstance::RenderSystemOpPairs::iterator currentOp, lastOp;
229         };
230         RQListener mOurListener;
231         /// Old viewport settings
232         unsigned int mOldClearEveryFrameBuffers;
233         /// Store old scene visibility mask
234         uint32 mOldVisibilityMask;
235         /// Store old find visible objects
236         bool mOldFindVisibleObjects;
237         /// Store old camera LOD bias
238         float mOldLodBias;
239         /// Store old viewport material scheme
240         String mOldMaterialScheme;
241         /// Store old shadows enabled flag
242         bool mOldShadowsEnabled;
243 
244     };
245     /** @} */
246     /** @} */
247 } // namespace Ogre
248 
249 #include "OgreHeaderSuffix.h"
250 
251 #endif // __CompositorChain_H__
252