Coverage Report

Created: 2025-11-25 06:29

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ogre/OgreMain/include/OgreCompositorInstance.h
Line
Count
Source
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 __CompositorInstance_H__
29
#define __CompositorInstance_H__
30
31
#include "OgrePrerequisites.h"
32
#include "OgreMaterialManager.h"
33
#include "OgreRenderQueue.h"
34
#include "OgreCompositionTechnique.h"
35
#include "OgreHeaderPrefix.h"
36
37
#include <bitset>
38
39
namespace Ogre {
40
41
    /** \addtogroup Core
42
    *  @{
43
    */
44
    /** \addtogroup Effects
45
    *  @{
46
    */
47
            
48
    /** An instance of a Compositor object for one Viewport. It is part of the CompositorChain
49
        for a Viewport.
50
    */
51
    class _OgreExport CompositorInstance : public CompositorInstAlloc
52
    {
53
    public:
54
        CompositorInstance(CompositionTechnique *technique, CompositorChain *chain);
55
        virtual ~CompositorInstance();
56
        /** Provides an interface to "listen in" to to render system operations executed by this 
57
            CompositorInstance.
58
        */
59
        class _OgreExport Listener
60
        {
61
        public:
62
            virtual ~Listener();
63
64
            /** Notification of when a render target operation involving a material (like
65
                rendering a quad) is compiled, so that miscellaneous parameters that are different
66
                per Compositor instance can be set up.
67
            @param pass_id
68
                Pass identifier within Compositor instance, this is specified 
69
                by the user by CompositionPass::setIdentifier().
70
            @param mat
71
                Material, this may be changed at will and will only affect
72
                the current instance of the Compositor, not the global material
73
                it was cloned from.
74
             */
75
            virtual void notifyMaterialSetup(uint32 pass_id, MaterialPtr &mat);
76
77
            /** Notification before a render target operation involving a material (like
78
                rendering a quad), so that material parameters can be varied.
79
            @param pass_id
80
                Pass identifier within Compositor instance, this is specified 
81
                by the user by CompositionPass::setIdentifier().
82
            @param mat
83
                Material, this may be changed at will and will only affect
84
                the current instance of the Compositor, not the global material
85
                it was cloned from.
86
             */
87
            virtual void notifyMaterialRender(uint32 pass_id, MaterialPtr &mat);
88
89
            /** Notification after resources have been created (or recreated).
90
            @param forResizeOnly
91
                Was the creation because the viewport was resized?
92
            */
93
            virtual void notifyResourcesCreated(bool forResizeOnly);
94
95
            /** Notification before resources have been destructed.
96
              @param forResizeOnly Was the creation because the viewport was resized?
97
             */
98
            virtual void notifyResourcesReleased(bool forResizeOnly);
99
        };
100
        /** Specific render system operation. A render target operation does special operations
101
            between render queues like rendering a quad, clearing the frame buffer or 
102
            setting stencil state.
103
        */
104
        class _OgreExport RenderSystemOperation : public CompositorInstAlloc
105
        {
106
        public:
107
            virtual ~RenderSystemOperation();
108
            /// Set state to SceneManager and RenderSystem
109
            virtual void execute(SceneManager *sm, RenderSystem *rs) = 0;
110
        };
111
        typedef std::pair<int, RenderSystemOperation*> RenderSystemOpPair;
112
        typedef std::vector<RenderSystemOpPair> RenderSystemOpPairs;
113
        /** Operation setup for a RenderTarget (collected).
114
        */
115
        class TargetOperation
116
        {
117
        public:
118
            TargetOperation()
119
0
            { 
120
0
            }
121
            TargetOperation(RenderTarget* inTarget)
122
0
                : target(inTarget), currentQueueGroupID(0), visibilityMask(0xFFFFFFFF), lodBias(1.0f),
123
0
                  onlyInitial(false), hasBeenRendered(false), findVisibleObjects(false),
124
0
                  materialScheme(MaterialManager::DEFAULT_SCHEME_NAME), shadowsEnabled(true),
125
0
                  alignCameraToFace(-1), swapBuffers(true)
126
0
            {
127
0
            }
128
            /// Target
129
            RenderTarget *target;
130
131
            /// Current group ID
132
            int currentQueueGroupID;
133
134
            /// RenderSystem operations to queue into the scene manager, by
135
            /// uint8
136
            RenderSystemOpPairs renderSystemOperations;
137
138
            /// Scene visibility mask
139
            /// If this is 0, the scene is not rendered at all
140
            uint32 visibilityMask;
141
            
142
            /// LOD offset. This is multiplied with the camera LOD offset
143
            /// 1.0 is default, lower means lower detail, higher means higher detail
144
            float lodBias;
145
            
146
            /** A set of render queues to either include or exclude certain render queues.
147
            */
148
            typedef std::bitset<RENDER_QUEUE_COUNT> RenderQueueBitSet;
149
150
            /// Which renderqueues to render from scene
151
            RenderQueueBitSet renderQueues;
152
            
153
            /** @see CompositionTargetPass::mOnlyInitial
154
            */
155
            bool onlyInitial;
156
            /** "Has been rendered" flag; used in combination with
157
                onlyInitial to determine whether to skip this target operation.
158
            */
159
            bool hasBeenRendered;
160
            /** Whether this op needs to find visible scene objects or not 
161
            */
162
            bool findVisibleObjects;
163
            /** Which material scheme this op will use */
164
            String materialScheme;
165
            /** Whether shadows will be enabled */
166
            bool shadowsEnabled;
167
168
            String cameraOverride;
169
            int alignCameraToFace;
170
171
            bool swapBuffers;
172
        };
173
        typedef std::vector<TargetOperation> CompiledState;
174
        
175
        /** Set enabled flag. The compositor instance will only render if it is
176
            enabled, otherwise it is pass-through. Resources are only created if
177
            they weren't alive when enabling.
178
        */
179
        void setEnabled(bool value);
180
        
181
        /** Get enabled flag.
182
        */
183
0
        bool getEnabled() const { return mEnabled; }
184
185
        /** Set alive/active flag. The compositor instance will create resources when alive,
186
            and destroy them when inactive.
187
188
            Killing an instance means also disabling it: setAlive(false) implies
189
            setEnabled(false)
190
        */
191
        void setAlive(bool value);
192
193
        /** Get alive flag.
194
        */
195
0
        bool getAlive() const { return mAlive; }
196
197
        /** Get the instance name for a local texture.
198
        @note It is only valid to call this when local textures have been loaded, 
199
            which in practice means that the compositor instance is active. Calling
200
            it at other times will cause an exception. Note that since textures
201
            are cleaned up aggressively, this name is not guaranteed to stay the
202
            same if you disable and re-enable the compositor instance.
203
        @param name
204
            The name of the texture in the original compositor definition.
205
        @param mrtIndex
206
            If name identifies a MRT, which texture attachment to retrieve.
207
        @return
208
            The instance name for the texture, corresponds to a real texture.
209
        */
210
        const String& getTextureInstanceName(const String& name, size_t mrtIndex);
211
212
        /** Get the instance of a local texture.
213
        @note Textures are only valid when local textures have been loaded, 
214
            which in practice means that the compositor instance is active. Calling
215
            this method at other times will return null pointers. Note that since textures
216
            are cleaned up aggressively, this pointer is not guaranteed to stay the
217
            same if you disable and re-enable the compositor instance.
218
        @param name
219
            The name of the texture in the original compositor definition.
220
        @param mrtIndex
221
            If name identifies a MRT, which texture attachment to retrieve.
222
        @return
223
            The texture pointer, corresponds to a real texture.
224
        */
225
        const TexturePtr& getTextureInstance(const String& name, size_t mrtIndex);
226
227
        /** Get the render target for a given render texture name. 
228
229
            You can use this to add listeners etc, but do not use it to update the
230
            targets manually or any other modifications, the compositor instance 
231
            is in charge of this.
232
        */
233
        RenderTarget* getRenderTarget(const String& name, int slice = 0);
234
235
       
236
        /** Recursively collect target states (except for final Pass).
237
        @param compiledState
238
            This vector will contain a list of TargetOperation objects.
239
        */
240
        virtual void _compileTargetOperations(CompiledState &compiledState);
241
        
242
        /** Compile the final (output) operation. This is done separately because this
243
            is combined with the input in chained filters.
244
        */
245
        virtual void _compileOutputOperation(TargetOperation &finalState);
246
        
247
        /** Get Compositor of which this is an instance
248
        */
249
0
        Compositor *getCompositor() const { return mCompositor; }
250
        
251
        /** Get CompositionTechnique used by this instance
252
        */
253
0
        CompositionTechnique *getTechnique() const { return mTechnique; }
254
255
        /** Change the technique we're using to render this compositor. 
256
        @param tech
257
            The technique to use (must be supported and from the same Compositor)
258
        @param reuseTextures
259
            If textures have already been created for the current
260
            technique, whether to try to re-use them if sizes & formats match.
261
        */
262
        void setTechnique(CompositionTechnique* tech, bool reuseTextures = true);
263
264
        /** Pick a technique to use to render this compositor based on a scheme. 
265
266
            If there is no specific supported technique with this scheme name, 
267
            then the first supported technique with no specific scheme will be used.
268
        @see CompositionTechnique::setSchemeName
269
        @param schemeName
270
            The scheme to use 
271
        @param reuseTextures
272
            If textures have already been created for the current
273
            technique, whether to try to re-use them if sizes & formats match.
274
            Note that for this feature to be of benefit, the textures must have been created
275
            with the 'pooled' option enabled.
276
        */
277
        void setScheme(const String& schemeName, bool reuseTextures = true);
278
279
        /// Returns the name of the scheme this compositor is using.
280
0
        const String& getScheme() const { return mTechnique ? mTechnique->getSchemeName() : BLANKSTRING; }
281
282
        /** Notify this instance that the primary surface has been resized. 
283
284
            This will allow the instance to recreate its resources that 
285
            are dependent on the size. 
286
        */
287
        void notifyResized();
288
289
        /** Get Chain that this instance is part of
290
        */
291
        CompositorChain *getChain();
292
293
        /** Add a listener. Listeners provide an interface to "listen in" to to render system 
294
            operations executed by this CompositorInstance so that materials can be 
295
            programmatically set up.
296
        @see CompositorInstance::Listener
297
        */
298
        void addListener(Listener *l);
299
300
        /** Remove a listener.
301
        @see CompositorInstance::Listener
302
        */
303
        void removeListener(Listener *l);
304
305
        /** Notify listeners of a material compilation.
306
        */
307
        void _fireNotifyMaterialSetup(uint32 pass_id, MaterialPtr &mat);
308
309
        /** Notify listeners of a material render.
310
        */
311
        void _fireNotifyMaterialRender(uint32 pass_id, MaterialPtr &mat);
312
313
        /** Notify listeners of a material render.
314
        */
315
        void _fireNotifyResourcesCreated(bool forResizeOnly);
316
        
317
        /** Notify listeners resources
318
        */
319
        void _fireNotifyResourcesReleased(bool forResizeOnly);
320
    private:
321
        /// Compositor of which this is an instance.
322
        Compositor *mCompositor;
323
        /// Composition technique used by this instance.
324
        CompositionTechnique *mTechnique;
325
        /// Composition chain of which this instance is part.
326
        CompositorChain *mChain;
327
        /// Is this instance enabled?
328
        bool mEnabled;
329
        /// Is this instance allocating resources?
330
        bool mAlive;
331
        /// Map from name->local texture.
332
        typedef std::unordered_map<String,TexturePtr> LocalTextureMap;
333
        LocalTextureMap mLocalTextures;
334
        /// Store a list of MRTs we've created.
335
        typedef std::unordered_map<String,MultiRenderTarget*> LocalMRTMap;
336
        LocalMRTMap mLocalMRTs;
337
        typedef std::map<CompositionTechnique::TextureDefinition*, TexturePtr> ReserveTextureMap;
338
        /** Textures that are not currently in use, but that we want to keep for now,
339
            for example if we switch techniques but want to keep all textures available
340
            in case we switch back. 
341
        */
342
        ReserveTextureMap mReserveTextures;
343
344
        /// Vector of listeners.
345
        typedef std::vector<Listener*> Listeners;
346
        Listeners mListeners;
347
        
348
        /// Previous instance (set by chain).
349
        CompositorInstance *mPreviousInstance;
350
        
351
        /** Collect rendering passes. Here, passes are converted into render target operations
352
            and queued with queueRenderSystemOp.
353
        */
354
        virtual void collectPasses(TargetOperation &finalState, const CompositionTargetPass *target);
355
356
        TexturePtr getLocalTexture(const CompositionTechnique::TextureDefinition& def, PixelFormat p,
357
                                   const String& fsaaHint, const String& localName,
358
                                   std::set<Texture*>& assignedTextures);
359
360
        /** Create local rendertextures and other resources. Builds mLocalTextures.
361
        */
362
        void createResources(bool forResizeOnly);
363
364
        void setupRenderTarget(RenderTarget* target, uint16 depthBufferId);
365
        
366
        /** Destroy local rendertextures and other resources.
367
        */
368
        void freeResources(bool forResizeOnly, bool clearReserveTextures);
369
370
        CompositionTechnique::TextureDefinition*
371
        resolveTexReference(const CompositionTechnique::TextureDefinition* texDef);
372
373
        /** Get source texture name for a named local texture.
374
        @param name
375
            The local name of the texture as given to it in the compositor.
376
        @param mrtIndex
377
            For MRTs, which attached surface to retrieve.
378
        */
379
        const TexturePtr &getSourceForTex(const String &name, size_t mrtIndex = 0);
380
381
        /** Queue a render system operation.
382
        */
383
        void queueRenderSystemOp(TargetOperation &finalState, RenderSystemOperation *op);
384
385
        /// Util method for assigning a local texture name to a MRT attachment
386
        static String getMRTTexLocalName(const String& baseName, size_t attachment);
387
388
        /** Search for options like AA and hardware gamma which we may want to 
389
            inherit from the main render target to which we're attached. 
390
        */
391
        void deriveOptionsFromRenderTarget(CompositionTechnique::TextureDefinition& def, String& fsaaHint);
392
393
        /// Notify this instance that the primary viewport's camera has changed.
394
        void notifyCameraChanged(Camera* camera);
395
396
        friend class CompositorChain;
397
        friend class Compositor;
398
    };
399
    /** @} */
400
    /** @} */
401
402
} // namespace Ogre
403
404
#include "OgreHeaderSuffix.h"
405
406
#endif // __CompositorInstance_H__