Coverage Report

Created: 2025-08-25 06:48

/src/ogre/OgreMain/include/OgreCompositorInstance.h
Line
Count
Source (jump to first uncovered line)
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)
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
        typedef std::vector<TargetOperation> CompiledState;
172
        
173
        /** Set enabled flag. The compositor instance will only render if it is
174
            enabled, otherwise it is pass-through. Resources are only created if
175
            they weren't alive when enabling.
176
        */
177
        void setEnabled(bool value);
178
        
179
        /** Get enabled flag.
180
        */
181
0
        bool getEnabled() const { return mEnabled; }
182
183
        /** Set alive/active flag. The compositor instance will create resources when alive,
184
            and destroy them when inactive.
185
186
            Killing an instance means also disabling it: setAlive(false) implies
187
            setEnabled(false)
188
        */
189
        void setAlive(bool value);
190
191
        /** Get alive flag.
192
        */
193
0
        bool getAlive() const { return mAlive; }
194
195
        /** Get the instance name for a local texture.
196
        @note It is only valid to call this when local textures have been loaded, 
197
            which in practice means that the compositor instance is active. Calling
198
            it at other times will cause an exception. Note that since textures
199
            are cleaned up aggressively, this name is not guaranteed to stay the
200
            same if you disable and re-enable the compositor instance.
201
        @param name
202
            The name of the texture in the original compositor definition.
203
        @param mrtIndex
204
            If name identifies a MRT, which texture attachment to retrieve.
205
        @return
206
            The instance name for the texture, corresponds to a real texture.
207
        */
208
        const String& getTextureInstanceName(const String& name, size_t mrtIndex);
209
210
        /** Get the instance of a local texture.
211
        @note Textures are only valid when local textures have been loaded, 
212
            which in practice means that the compositor instance is active. Calling
213
            this method at other times will return null pointers. Note that since textures
214
            are cleaned up aggressively, this pointer is not guaranteed to stay the
215
            same if you disable and re-enable the compositor instance.
216
        @param name
217
            The name of the texture in the original compositor definition.
218
        @param mrtIndex
219
            If name identifies a MRT, which texture attachment to retrieve.
220
        @return
221
            The texture pointer, corresponds to a real texture.
222
        */
223
        const TexturePtr& getTextureInstance(const String& name, size_t mrtIndex);
224
225
        /** Get the render target for a given render texture name. 
226
227
            You can use this to add listeners etc, but do not use it to update the
228
            targets manually or any other modifications, the compositor instance 
229
            is in charge of this.
230
        */
231
        RenderTarget* getRenderTarget(const String& name, int slice = 0);
232
233
       
234
        /** Recursively collect target states (except for final Pass).
235
        @param compiledState
236
            This vector will contain a list of TargetOperation objects.
237
        */
238
        virtual void _compileTargetOperations(CompiledState &compiledState);
239
        
240
        /** Compile the final (output) operation. This is done separately because this
241
            is combined with the input in chained filters.
242
        */
243
        virtual void _compileOutputOperation(TargetOperation &finalState);
244
        
245
        /** Get Compositor of which this is an instance
246
        */
247
0
        Compositor *getCompositor() const { return mCompositor; }
248
        
249
        /** Get CompositionTechnique used by this instance
250
        */
251
0
        CompositionTechnique *getTechnique() const { return mTechnique; }
252
253
        /** Change the technique we're using to render this compositor. 
254
        @param tech
255
            The technique to use (must be supported and from the same Compositor)
256
        @param reuseTextures
257
            If textures have already been created for the current
258
            technique, whether to try to re-use them if sizes & formats match.
259
        */
260
        void setTechnique(CompositionTechnique* tech, bool reuseTextures = true);
261
262
        /** Pick a technique to use to render this compositor based on a scheme. 
263
264
            If there is no specific supported technique with this scheme name, 
265
            then the first supported technique with no specific scheme will be used.
266
        @see CompositionTechnique::setSchemeName
267
        @param schemeName
268
            The scheme to use 
269
        @param reuseTextures
270
            If textures have already been created for the current
271
            technique, whether to try to re-use them if sizes & formats match.
272
            Note that for this feature to be of benefit, the textures must have been created
273
            with the 'pooled' option enabled.
274
        */
275
        void setScheme(const String& schemeName, bool reuseTextures = true);
276
277
        /// Returns the name of the scheme this compositor is using.
278
0
        const String& getScheme() const { return mTechnique ? mTechnique->getSchemeName() : BLANKSTRING; }
279
280
        /** Notify this instance that the primary surface has been resized. 
281
282
            This will allow the instance to recreate its resources that 
283
            are dependent on the size. 
284
        */
285
        void notifyResized();
286
287
        /** Get Chain that this instance is part of
288
        */
289
        CompositorChain *getChain();
290
291
        /** Add a listener. Listeners provide an interface to "listen in" to to render system 
292
            operations executed by this CompositorInstance so that materials can be 
293
            programmatically set up.
294
        @see CompositorInstance::Listener
295
        */
296
        void addListener(Listener *l);
297
298
        /** Remove a listener.
299
        @see CompositorInstance::Listener
300
        */
301
        void removeListener(Listener *l);
302
303
        /** Notify listeners of a material compilation.
304
        */
305
        void _fireNotifyMaterialSetup(uint32 pass_id, MaterialPtr &mat);
306
307
        /** Notify listeners of a material render.
308
        */
309
        void _fireNotifyMaterialRender(uint32 pass_id, MaterialPtr &mat);
310
311
        /** Notify listeners of a material render.
312
        */
313
        void _fireNotifyResourcesCreated(bool forResizeOnly);
314
        
315
        /** Notify listeners resources
316
        */
317
        void _fireNotifyResourcesReleased(bool forResizeOnly);
318
    private:
319
        /// Compositor of which this is an instance.
320
        Compositor *mCompositor;
321
        /// Composition technique used by this instance.
322
        CompositionTechnique *mTechnique;
323
        /// Composition chain of which this instance is part.
324
        CompositorChain *mChain;
325
        /// Is this instance enabled?
326
        bool mEnabled;
327
        /// Is this instance allocating resources?
328
        bool mAlive;
329
        /// Map from name->local texture.
330
        typedef std::unordered_map<String,TexturePtr> LocalTextureMap;
331
        LocalTextureMap mLocalTextures;
332
        /// Store a list of MRTs we've created.
333
        typedef std::unordered_map<String,MultiRenderTarget*> LocalMRTMap;
334
        LocalMRTMap mLocalMRTs;
335
        typedef std::map<CompositionTechnique::TextureDefinition*, TexturePtr> ReserveTextureMap;
336
        /** Textures that are not currently in use, but that we want to keep for now,
337
            for example if we switch techniques but want to keep all textures available
338
            in case we switch back. 
339
        */
340
        ReserveTextureMap mReserveTextures;
341
342
        /// Vector of listeners.
343
        typedef std::vector<Listener*> Listeners;
344
        Listeners mListeners;
345
        
346
        /// Previous instance (set by chain).
347
        CompositorInstance *mPreviousInstance;
348
        
349
        /** Collect rendering passes. Here, passes are converted into render target operations
350
            and queued with queueRenderSystemOp.
351
        */
352
        virtual void collectPasses(TargetOperation &finalState, const CompositionTargetPass *target);
353
354
        TexturePtr getLocalTexture(const CompositionTechnique::TextureDefinition& def, PixelFormat p,
355
                                   const String& fsaaHint, const String& localName,
356
                                   std::set<Texture*>& assignedTextures);
357
358
        /** Create local rendertextures and other resources. Builds mLocalTextures.
359
        */
360
        void createResources(bool forResizeOnly);
361
362
        void setupRenderTarget(RenderTarget* target, uint16 depthBufferId);
363
        
364
        /** Destroy local rendertextures and other resources.
365
        */
366
        void freeResources(bool forResizeOnly, bool clearReserveTextures);
367
368
        CompositionTechnique::TextureDefinition*
369
        resolveTexReference(const CompositionTechnique::TextureDefinition* texDef);
370
371
        /** Get source texture name for a named local texture.
372
        @param name
373
            The local name of the texture as given to it in the compositor.
374
        @param mrtIndex
375
            For MRTs, which attached surface to retrieve.
376
        */
377
        const TexturePtr &getSourceForTex(const String &name, size_t mrtIndex = 0);
378
379
        /** Queue a render system operation.
380
        */
381
        void queueRenderSystemOp(TargetOperation &finalState, RenderSystemOperation *op);
382
383
        /// Util method for assigning a local texture name to a MRT attachment
384
        static String getMRTTexLocalName(const String& baseName, size_t attachment);
385
386
        /** Search for options like AA and hardware gamma which we may want to 
387
            inherit from the main render target to which we're attached. 
388
        */
389
        void deriveOptionsFromRenderTarget(CompositionTechnique::TextureDefinition& def, String& fsaaHint);
390
391
        /// Notify this instance that the primary viewport's camera has changed.
392
        void notifyCameraChanged(Camera* camera);
393
394
        friend class CompositorChain;
395
        friend class Compositor;
396
    };
397
    /** @} */
398
    /** @} */
399
400
} // namespace Ogre
401
402
#include "OgreHeaderSuffix.h"
403
404
#endif // __CompositorInstance_H__