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-2013 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 "OgreCompositor.h"
36 #include "OgreViewport.h"
37 #include "OgreHeaderPrefix.h"
38 
39 namespace Ogre {
40     /** \addtogroup Core
41     *  @{
42     */
43     /** \addtogroup Effects
44     *  @{
45     */
46     /** Chain of compositor effects applying to one viewport.
47     */
48     class _OgreExport CompositorChain : public RenderTargetListener, public Viewport::Listener, public CompositorInstAlloc
49     {
50     public:
51         CompositorChain(Viewport *vp);
52         /** Another gcc warning here, which is no problem because RenderTargetListener is never used
53             to delete an object.
54         */
55         virtual ~CompositorChain();
56 
57         /// Data types
58         typedef vector<CompositorInstance*>::type Instances;
59         typedef VectorIterator<Instances> InstanceIterator;
60 
61         /// Identifier for "last" compositor in chain.
62         static const size_t LAST = (size_t)-1;
63         /// Identifier for best technique.
64         static const size_t BEST = 0;
65 
66         /** Apply a compositor. Initially, the filter is enabled.
67         @param filter
68             Filter to apply.
69         @param addPosition
70             Position in filter chain to insert this filter at; defaults to the end (last applied filter).
71         @param scheme
72             Scheme to use (blank means default).
73         */
74         CompositorInstance* addCompositor(CompositorPtr filter, size_t addPosition=LAST, const String& scheme = StringUtil::BLANK);
75 
76         /** Remove a compositor.
77         @param position
78             Position in filter chain of filter to remove; defaults to the end (last applied filter)
79         */
80         void removeCompositor(size_t position=LAST);
81 
82         /** Get the number of compositors.
83         */
84         size_t getNumCompositors();
85 
86         /** Remove all compositors.
87         */
88         void removeAllCompositors();
89 
90         /** Get compositor instance by position.
91         */
92         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 the original scene compositor instance for this chain (internal use).
99         */
_getOriginalSceneCompositor(void)100         CompositorInstance* _getOriginalSceneCompositor(void) { return mOriginalScene; }
101 
102         /** Get an iterator over the compositor instances. The first compositor in this list is applied first, the last one is applied last.
103         */
104         InstanceIterator getCompositors();
105 
106         /** Enable or disable a compositor, by position. Disabling a compositor stops it from rendering
107             but does not free any resources. This can be more efficient than using removeCompositor and
108             addCompositor in cases the filter is switched on and off a lot.
109         @param position
110             Position in filter chain of filter
111         */
112         void setCompositorEnabled(size_t position, bool state);
113 
114         /** @see RenderTargetListener::preRenderTargetUpdate */
115         virtual void preRenderTargetUpdate(const RenderTargetEvent& evt);
116         /** @see RenderTargetListener::postRenderTargetUpdate */
117         virtual void postRenderTargetUpdate(const RenderTargetEvent& evt);
118         /** @see RenderTargetListener::preViewportUpdate */
119         virtual void preViewportUpdate(const RenderTargetViewportEvent& evt);
120         /** @see RenderTargetListener::postViewportUpdate */
121         virtual void postViewportUpdate(const RenderTargetViewportEvent& evt);
122 
123         /** @see Viewport::Listener::viewportCameraChanged */
124         virtual void viewportCameraChanged(Viewport* viewport);
125         /** @see Viewport::Listener::viewportDimensionsChanged */
126         virtual void viewportDimensionsChanged(Viewport* viewport);
127         /** @see Viewport::Listener::viewportDestroyed */
128         virtual void viewportDestroyed(Viewport* viewport);
129 
130         /** Mark state as dirty, and to be recompiled next frame.
131         */
132         void _markDirty();
133 
134         /** Get viewport that is the target of this chain
135         */
136         Viewport *getViewport();
137 
138         /** Remove a compositor by pointer. This is internally used by CompositionTechnique to
139             "weak" remove any instanced of a deleted technique.
140         */
141         void _removeInstance(CompositorInstance *i);
142 
143         /** Internal method for registering a queued operation for deletion later **/
144         void _queuedOperation(CompositorInstance::RenderSystemOperation* op);
145 
146         /** Compile this Composition chain into a series of RenderTarget operations.
147         */
148         void _compile();
149 
150         /** Get the previous instance in this chain to the one specified.
151         */
152         CompositorInstance* getPreviousInstance(CompositorInstance* curr, bool activeOnly = true);
153         /** Get the next instance in this chain to the one specified.
154         */
155         CompositorInstance* getNextInstance(CompositorInstance* curr, bool activeOnly = true);
156 
157     protected:
158         /// Viewport affected by this CompositorChain
159         Viewport *mViewport;
160 
161         /** Plainly renders the scene; implicit first compositor in the chain.
162         */
163         CompositorInstance *mOriginalScene;
164 
165         /// Postfilter instances in this chain
166         Instances mInstances;
167 
168         /// State needs recompile
169         bool mDirty;
170         /// Any compositors enabled?
171         bool mAnyCompositorsEnabled;
172 
173         String mOriginalSceneScheme;
174 
175         /// Compiled state (updated with _compile)
176         CompositorInstance::CompiledState mCompiledState;
177         CompositorInstance::TargetOperation mOutputOperation;
178         /// Render System operations queued by last compile, these are created by this
179         /// instance thus managed and deleted by it. The list is cleared with
180         /// clearCompilationState()
181         typedef vector<CompositorInstance::RenderSystemOperation*>::type RenderSystemOperations;
182         RenderSystemOperations mRenderSystemOperations;
183 
184         /** Clear compiled state */
185         void clearCompiledState();
186 
187         /** Prepare a viewport, the camera and the scene for a rendering operation
188         */
189         void preTargetOperation(CompositorInstance::TargetOperation &op, Viewport *vp, Camera *cam);
190 
191         /** Restore a viewport, the camera and the scene after a rendering operation
192         */
193         void postTargetOperation(CompositorInstance::TargetOperation &op, Viewport *vp, Camera *cam);
194 
195         void createOriginalScene();
196         void destroyOriginalScene();
197 
198         /// destroy internal resources
199         void destroyResources(void);
200 
201         /** Render queue listener used to set up rendering events. */
202         class _OgreExport RQListener: public RenderQueueListener
203         {
204         public:
RQListener()205             RQListener() : mOperation(0), mSceneManager(0), mRenderSystem(0), mViewport(0) {}
206 
207             /** @copydoc RenderQueueListener::renderQueueStarted
208             */
209             virtual void renderQueueStarted(uint8 queueGroupId, const String& invocation, bool& skipThisInvocation);
210             /** @copydoc RenderQueueListener::renderQueueEnded
211             */
212             virtual void renderQueueEnded(uint8 queueGroupId, const String& invocation, bool& repeatThisInvocation);
213 
214             /** Set current operation and target. */
215             void setOperation(CompositorInstance::TargetOperation *op,SceneManager *sm,RenderSystem *rs);
216 
217             /** Notify current destination viewport. */
notifyViewport(Viewport * vp)218             void notifyViewport(Viewport* vp) { mViewport = vp; }
219 
220             /** Flush remaining render system operations. */
221             void flushUpTo(uint8 id);
222         private:
223             CompositorInstance::TargetOperation *mOperation;
224             SceneManager *mSceneManager;
225             RenderSystem *mRenderSystem;
226             Viewport* mViewport;
227             CompositorInstance::RenderSystemOpPairs::iterator currentOp, lastOp;
228         };
229         RQListener mOurListener;
230         /// Old viewport settings
231         unsigned int mOldClearEveryFrameBuffers;
232         /// Store old scene visibility mask
233         uint32 mOldVisibilityMask;
234         /// Store old find visible objects
235         bool mOldFindVisibleObjects;
236         /// Store old camera LOD bias
237         float mOldLodBias;
238         /// Store old viewport material scheme
239         String mOldMaterialScheme;
240         /// Store old shadows enabled flag
241         bool mOldShadowsEnabled;
242 
243     };
244     /** @} */
245     /** @} */
246 } // namespace Ogre
247 
248 #include "OgreHeaderSuffix.h"
249 
250 #endif // __CompositorChain_H__
251