Coverage Report

Created: 2025-07-11 06:36

/src/ogre/OgreMain/include/OgreTextureManager.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 _TextureManager_H__
29
#define _TextureManager_H__
30
31
32
#include "OgrePrerequisites.h"
33
34
#include "OgreResourceManager.h"
35
#include "OgreTexture.h"
36
#include "OgreSingleton.h"
37
#include "OgreTextureUnitState.h"
38
39
namespace Ogre {
40
41
    /** \addtogroup Core
42
    *  @{
43
    */
44
    /** \addtogroup Resources
45
    *  @{
46
    */
47
    /** Class for loading & managing textures.
48
49
        Note that this class is abstract - the particular
50
        RenderSystem that is in use at the time will create
51
        a concrete subclass of this. Note that the concrete
52
        class will be available via the abstract singleton
53
        obtained from TextureManager::getSingleton(), but
54
        you should not assume that it is available until you
55
        have a) initialised Ogre (after selecting a RenderSystem
56
        and calling initialise from the Root object), and b)
57
        created at least one window - this may be done at the
58
        same time as part a if you allow Ogre to autocreate one.
59
     */
60
    class _OgreExport TextureManager : public ResourceManager, public Singleton<TextureManager>
61
    {
62
    public:
63
64
        TextureManager(void);
65
        virtual ~TextureManager();
66
67
        /// create a new sampler
68
        SamplerPtr createSampler(const String& name = BLANKSTRING);
69
70
        /// retrieve an named sampler
71
        const SamplerPtr& getSampler(const String& name) const;
72
73
        /// clear the list of named samplers
74
        /// @copydetails removeAll()
75
0
        void removeAllNamedSamplers() { mNamedSamplers.clear(); }
76
77
        /// Create a new texture
78
        /// @copydetails ResourceManager::createResource
79
        TexturePtr create (const String& name, const String& group,
80
                            bool isManual = false, ManualResourceLoader* loader = 0,
81
                            const NameValuePairList* createParams = 0);
82
        /// @copydoc ResourceManager::getResourceByName
83
        TexturePtr getByName(const String& name, const String& groupName OGRE_RESOURCE_GROUP_INIT) const;
84
85
        using ResourceManager::createOrRetrieve;
86
87
        /** @overload createOrRetrieve
88
            
89
            @copydetails ResourceManager::createResource
90
91
            @param
92
                texType The type of texture to load/create, defaults to normal 2D textures
93
            @param
94
                numMipmaps The number of pre-filtered mipmaps to generate. If left to MIP_DEFAULT then
95
                the TextureManager's default number of mipmaps will be used (see setDefaultNumMipmaps())
96
                If set to MIP_UNLIMITED mipmaps will be generated until the lowest possible
97
                level, 1x1x1.
98
            @param
99
                gamma The gamma adjustment factor to apply to this texture (brightening/darkening)
100
            @param 
101
                isAlpha deprecated: same as specifying #PF_A8 for @c desiredFormat
102
            @param 
103
                desiredFormat The format you would like to have used instead of
104
                the format being based on the contents of the texture
105
            @param hwGammaCorrection Pass 'true' to enable hardware gamma correction
106
                (sRGB) on this texture. The hardware will convert from gamma space
107
                to linear space when reading from this texture. Only applicable for 
108
                8-bits per channel textures, will be ignored for other types. Has the advantage
109
                over pre-applied gamma that the texture precision is maintained.
110
        */
111
        ResourceCreateOrRetrieveResult createOrRetrieve(
112
            const String &name, const String& group, bool isManual,
113
            ManualResourceLoader* loader, const NameValuePairList* createParams,
114
            TextureType texType, int numMipmaps = MIP_DEFAULT,
115
            Real gamma = 1.0f, bool isAlpha = false,
116
            PixelFormat desiredFormat = PF_UNKNOWN, bool hwGammaCorrection = false);
117
118
        /** Prepares to loads a texture from a file.
119
            @copydetails TextureManager::load
120
            @param isAlpha deprecated: same as specifying #PF_A8 for @c desiredFormat
121
        */
122
        TexturePtr prepare(
123
            const String& name, const String& group,
124
            TextureType texType = TEX_TYPE_2D, int numMipmaps = MIP_DEFAULT,
125
            Real gamma = 1.0f, bool isAlpha = false,
126
            PixelFormat desiredFormat = PF_UNKNOWN, bool hwGammaCorrection = false);
127
128
        /** Loads a texture from a file.
129
            @param
130
                name The file to load, or a String identifier in some cases
131
            @param
132
                group The name of the resource group to assign the texture to
133
            @param
134
                texType The type of texture to load/create, defaults to normal 2D textures
135
            @param
136
                numMipmaps The number of pre-filtered mipmaps to generate. If left to MIP_DEFAULT then
137
                the TextureManager's default number of mipmaps will be used (see setDefaultNumMipmaps())
138
                If set to MIP_UNLIMITED mipmaps will be generated until the lowest possible
139
                level, 1x1x1.
140
            @param
141
                gamma The gamma adjustment factor to apply to this texture (brightening/darkening)
142
143
            @param 
144
                desiredFormat The format you would like to have used instead of
145
                the format being based on the contents of the texture; the manager reserves
146
                the right to create a different format for the texture if the 
147
                original format is not available in this context.
148
            @param hwGammaCorrection pass 'true' to enable hardware gamma correction
149
                (sRGB) on this texture. The hardware will convert from gamma space
150
                to linear space when reading from this texture. Only applicable for 
151
                8-bits per channel textures, will be ignored for other types. Has the advantage
152
                over pre-applied gamma that the texture precision is maintained.
153
        */
154
        TexturePtr load(const String& name, const String& group, TextureType texType = TEX_TYPE_2D,
155
                        int numMipmaps = MIP_DEFAULT, Real gamma = 1.0f,
156
                        PixelFormat desiredFormat = PF_UNKNOWN, bool hwGammaCorrection = false);
157
        /// @deprecated
158
        OGRE_DEPRECATED TexturePtr load(const String& name, const String& group, TextureType texType,
159
                                        int numMipmaps, Real gamma, bool isAlpha,
160
                                        PixelFormat desiredFormat = PF_UNKNOWN,
161
                                        bool hwGammaCorrection = false);
162
163
        /** Loads a texture from an Image object.
164
            @note
165
                The texture will create as manual texture without loader.
166
            @copydetails TextureManager::prepare
167
            @param
168
                img The Image object which contains the data to load
169
        */
170
        virtual TexturePtr loadImage( 
171
            const String &name, const String& group, const Image &img, 
172
            TextureType texType = TEX_TYPE_2D,
173
            int numMipmaps = MIP_DEFAULT, Real gamma = 1.0f, bool isAlpha = false,
174
            PixelFormat desiredFormat = PF_UNKNOWN, bool hwGammaCorrection = false);
175
            
176
        /** Loads a texture from a raw data stream.
177
            @note
178
                The texture will create as manual texture without loader.
179
            @param name
180
                The name to give the resulting texture
181
            @param group
182
                The name of the resource group to assign the texture to
183
            @param stream
184
                Incoming data stream
185
            @param width
186
                The width of the texture
187
            @param height
188
                The height of the texture
189
            @param format
190
                The format of the data being passed in; the manager reserves
191
                the right to create a different format for the texture if the 
192
                original format is not available in this context.
193
            @param texType
194
                The type of texture to load/create, defaults to normal 2D textures
195
            @param numMipmaps
196
                The number of pre-filtered mipmaps to generate. If left to MIP_DEFAULT then
197
                the TextureManager's default number of mipmaps will be used (see setDefaultNumMipmaps())
198
                If set to MIP_UNLIMITED mipmaps will be generated until the lowest possible
199
                level, 1x1x1.
200
            @param gamma
201
                The gamma adjustment factor to apply to this texture (brightening/darkening)
202
                while loading
203
            @param hwGammaCorrection Pass 'true' to enable hardware gamma correction
204
                 (sRGB) on this texture. The hardware will convert from gamma space
205
                 to linear space when reading from this texture. Only applicable for 
206
                 8-bits per channel textures, will be ignored for other types. Has the advantage
207
                 over pre-applied gamma that the texture precision is maintained.
208
        */
209
        virtual TexturePtr loadRawData(const String &name, const String& group,
210
            DataStreamPtr& stream, ushort width, ushort height, 
211
            PixelFormat format, TextureType texType = TEX_TYPE_2D, 
212
            int numMipmaps = MIP_DEFAULT, Real gamma = 1.0f, bool hwGammaCorrection = false);
213
214
        /** Create a manual texture with specified width, height and depth (not loaded from a file).
215
            @param
216
                name The name to give the resulting texture
217
            @param
218
                group The name of the resource group to assign the texture to
219
            @param
220
                texType The type of texture to load/create, defaults to normal 2D textures
221
            @param width
222
                The width of the texture
223
            @param height
224
                The height of the texture
225
            @param depth
226
                The depth of the texture
227
            @param
228
                numMipmaps The number of pre-filtered mipmaps to generate. If left to MIP_DEFAULT then
229
                the TextureManager's default number of mipmaps will be used (see setDefaultNumMipmaps())
230
                If set to MIP_UNLIMITED mipmaps will be generated until the lowest possible
231
                level, 1x1x1.
232
            @param
233
                format The internal format you wish to request; the manager reserves
234
                the right to create a different format if the one you select is
235
                not available in this context.
236
            @param 
237
                usage The kind of usage this texture is intended for. It 
238
                is a combination of TU_STATIC, TU_DYNAMIC, TU_WRITE_ONLY, 
239
                TU_AUTOMIPMAP and TU_RENDERTARGET (see TextureUsage enum). You are
240
                strongly advised to use HBU_GPU_ONLY wherever possible, if you need to
241
                update regularly, consider HBU_CPU_TO_GPU.
242
            @param
243
                loader If you intend the contents of the manual texture to be 
244
                regularly updated, to the extent that you don't need to recover 
245
                the contents if the texture content is lost somehow, you can leave
246
                this parameter as 0. However, if you intend to populate the
247
                texture only once, then you should implement ManualResourceLoader
248
                and pass a pointer to it in this parameter; this means that if the
249
                manual texture ever needs to be reloaded, the ManualResourceLoader
250
                will be called to do it.
251
            @param hwGammaCorrection pass 'true' to enable hardware gamma correction
252
                (sRGB) on this texture. The hardware will convert from gamma space
253
                to linear space when reading from this texture. Only applicable for 
254
                8-bits per channel textures, will be ignored for other types. Has the advantage
255
                over pre-applied gamma that the texture precision is maintained.
256
            @param fsaa The level of multisampling to use if this is a render target. Ignored
257
                if usage does not include TU_RENDERTARGET or if the device does
258
                not support it.
259
            @param fsaaHint @copybrief RenderTarget::getFSAAHint
260
        */
261
        virtual TexturePtr createManual(const String & name, const String& group,
262
            TextureType texType, uint width, uint height, uint depth, 
263
            int numMipmaps, PixelFormat format, int usage = TU_DEFAULT, ManualResourceLoader* loader = 0,
264
            bool hwGammaCorrection = false, uint fsaa = 0, const String& fsaaHint = BLANKSTRING);
265
            
266
        /** @overload
267
        */
268
        TexturePtr createManual(const String & name, const String& group,
269
            TextureType texType, uint width, uint height, int numMipmaps,
270
            PixelFormat format, int usage = TU_DEFAULT, ManualResourceLoader* loader = 0,
271
            bool hwGammaCorrection = false, uint fsaa = 0, const String& fsaaHint = BLANKSTRING)
272
0
        {
273
0
            return createManual(name, group, texType, width, height, 1, 
274
0
                numMipmaps, format, usage, loader, hwGammaCorrection, fsaa, fsaaHint);
275
0
        }
276
277
        /** Sets preferred bit depth for integer pixel format textures.
278
        @param
279
            bits Number of bits. Available values: 0, 16 and 32, where 0 (the default) means keep
280
            original format as it is. This value is number of bits for the pixel.
281
        @param
282
            reloadTextures If true (the default), will reloading all reloadable textures.
283
        */
284
        virtual void setPreferredIntegerBitDepth(ushort bits, bool reloadTextures = true);
285
286
        /** Gets preferred bit depth for integer pixel format textures.
287
        */
288
        virtual ushort getPreferredIntegerBitDepth(void) const;
289
290
        /** Sets preferred bit depth for float pixel format textures.
291
        @param
292
            bits Number of bits. Available values: 0, 16 and 32, where 0 (the default) means keep
293
            original format as it is. This value is number of bits for a channel of the pixel.
294
        @param
295
            reloadTextures If true (the default), will reloading all reloadable textures.
296
        */
297
        virtual void setPreferredFloatBitDepth(ushort bits, bool reloadTextures = true);
298
299
        /** Gets preferred bit depth for float pixel format textures.
300
        */
301
        virtual ushort getPreferredFloatBitDepth(void) const;
302
303
        /** Sets preferred bit depth for integer and float pixel format.
304
        @param
305
            integerBits Number of bits. Available values: 0, 16 and 32, where 0 (the default) means keep
306
            original format as it is. This value is number of bits for the pixel.
307
        @param
308
            floatBits Number of bits. Available values: 0, 16 and 32, where 0 (the default) means keep
309
            original format as it is. This value is number of bits for a channel of the pixel.
310
        @param
311
            reloadTextures If true (the default), will reloading all reloadable textures.
312
        */
313
        virtual void setPreferredBitDepths(ushort integerBits, ushort floatBits, bool reloadTextures = true);
314
315
        /** Returns whether this render system can natively support the precise texture 
316
            format requested with the given usage options.
317
318
            You can still create textures with this format even if this method returns
319
            false; the texture format will just be altered to one which the device does
320
            support.
321
        @note
322
            Sometimes the device may just slightly change the format, such as reordering the 
323
            channels or packing the channels differently, without it making and qualitative 
324
            differences to the texture. If you want to just detect whether the quality of a
325
            given texture will be reduced, use isEquivalentFormatSupport instead.
326
        @param ttype The type of texture
327
        @param format The pixel format requested
328
        @param usage The kind of usage this texture is intended for, a combination of 
329
            the TextureUsage flags.
330
        @return true if the format is natively supported, false if a fallback would be used.
331
        */
332
        virtual bool isFormatSupported(TextureType ttype, PixelFormat format, int usage);
333
334
        /** Returns whether this render system can support the texture format requested
335
            with the given usage options, or another format with no quality reduction.
336
        */
337
        virtual bool isEquivalentFormatSupported(TextureType ttype, PixelFormat format, int usage);
338
339
        /** Gets the format which will be natively used for a requested format given the
340
            constraints of the current device.
341
        */
342
        virtual PixelFormat getNativeFormat(TextureType ttype, PixelFormat format, int usage) = 0;
343
344
        /** Returns whether this render system has hardware filtering supported for the
345
            texture format requested with the given usage options.
346
347
            Not all texture format are supports filtering by the hardware, i.e. some
348
            cards support floating point format, but it doesn't supports filtering on
349
            the floating point texture at all, or only a subset floating point formats
350
            have flitering supported.
351
        @par
352
            In the case you want to write shader to work with floating point texture, and
353
            you want to produce better visual quality, it's necessary to flitering the
354
            texture manually in shader (potential requires four or more texture fetch
355
            instructions, plus several arithmetic instructions) if filtering doesn't
356
            supported by hardware. But in case on the hardware that supports floating
357
            point filtering natively, it had better to adopt this capability for
358
            performance (because only one texture fetch instruction are required) and
359
            doesn't loss visual quality.
360
        @par
361
            This method allow you queries hardware texture filtering capability to deciding
362
            which version of the shader to be used. Note it's up to you to write multi-version
363
            shaders for support various hardware, internal engine can't do that for you
364
            automatically.
365
        @note
366
            Under GL, texture filtering are always supported by driver, but if it's not
367
            supported by hardware natively, software simulation will be used, and you
368
            will end up with very slow speed (less than 0.1 fps for example). To slove
369
            this performance problem, you must disable filtering manually (by use
370
            <b>filtering none</b> in the material script's texture_unit section, or
371
            call TextureUnitState::setTextureFiltering with TFO_NONE if populate
372
            material in code).
373
        @param ttype The texture type requested
374
        @param format The pixel format requested
375
        @param usage The kind of usage this texture is intended for, a combination of 
376
            the TextureUsage flags.
377
        @param preciseFormatOnly Whether precise or fallback format mode is used to detecting.
378
            In case the pixel format doesn't supported by device, false will be returned
379
            if in precise mode, and natively used pixel format will be actually use to
380
            check if in fallback mode.
381
        @return true if the texture filtering is supported.
382
        */
383
        virtual bool isHardwareFilteringSupported(TextureType ttype, PixelFormat format, int usage,
384
            bool preciseFormatOnly = false);
385
386
        /** Sets the default number of mipmaps to be used for loaded textures, for when textures are
387
            loaded automatically (e.g. by Material class) or when 'load' is called with the default
388
            parameters by the application.
389
            If set to MIP_UNLIMITED mipmaps will be generated until the lowest possible
390
                level, 1x1x1.
391
            @note
392
                The default value is 0.
393
        */
394
        virtual void setDefaultNumMipmaps(uint32 num);
395
396
        /** Gets the default number of mipmaps to be used for loaded textures.
397
        */
398
        virtual uint32 getDefaultNumMipmaps()
399
0
        {
400
0
            return mDefaultNumMipmaps;
401
0
        }
402
403
        /// Internal method to create a warning texture (bound when a texture unit is blank)
404
        const TexturePtr& _getWarningTexture();
405
406
        /// get the default sampler
407
        const SamplerPtr& getDefaultSampler();
408
409
        /// @copydoc Singleton::getSingleton()
410
        static TextureManager& getSingleton(void);
411
        /// @copydoc Singleton::getSingleton()
412
        static TextureManager* getSingletonPtr(void);
413
414
    protected:
415
416
0
        virtual SamplerPtr _createSamplerImpl() { return std::make_shared<Sampler>(); }
417
418
        ushort mPreferredIntegerBitDepth;
419
        ushort mPreferredFloatBitDepth;
420
        uint32 mDefaultNumMipmaps;
421
        TexturePtr mWarningTexture;
422
        SamplerPtr mDefaultSampler;
423
        std::map<String, SamplerPtr> mNamedSamplers;
424
    };
425
426
    /// Specialisation of TextureManager for offline processing. Cannot be used with an active RenderSystem.
427
    class _OgreExport DefaultTextureManager : public TextureManager
428
    {
429
        /// noop implementation
430
        class NullTexture : public Texture
431
        {
432
        public:
433
            NullTexture(ResourceManager* creator, const String& name, ResourceHandle handle,
434
                        const String& group)
435
                : Texture(creator, name, handle, group)
436
0
            {
437
0
            }
438
        protected:
439
0
            void createInternalResourcesImpl() override { createSurfaceList(); }
440
0
            void freeInternalResourcesImpl() override {}
441
0
            void loadImpl() override {}
442
        };
443
444
        Resource* createImpl(const String& name, ResourceHandle handle, const String& group, bool,
445
                             ManualResourceLoader*, const NameValuePairList*) override
446
0
        {
447
0
            return new NullTexture(this, name, handle, group);
448
0
        }
449
450
    public:
451
0
        bool isHardwareFilteringSupported(TextureType, PixelFormat, int, bool) override { return false; }
452
0
        PixelFormat getNativeFormat(TextureType, PixelFormat, int) override { return PF_UNKNOWN; }
453
    };
454
    /** @} */
455
    /** @} */
456
}// Namespace
457
458
#endif