Coverage Report

Created: 2025-07-18 07:08

/src/ogre/OgreMain/include/OgreTexture.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 _Texture_H__
29
#define _Texture_H__
30
31
#include "OgrePrerequisites.h"
32
#include "OgreHardwareBuffer.h"
33
#include "OgreResource.h"
34
#include "OgreImage.h"
35
#include "OgreHeaderPrefix.h"
36
#include "OgreSharedPtr.h"
37
38
namespace Ogre {
39
40
    /** \addtogroup Core
41
     *  @{
42
     */
43
    /** \addtogroup Resources
44
     *  @{
45
     */
46
    /** Enum identifying the texture usage
47
     */
48
    enum TextureUsage
49
    {
50
        /// same as #HBU_GPU_TO_CPU
51
        TU_STATIC = HBU_GPU_TO_CPU,
52
        /// same as #HBU_CPU_ONLY
53
        TU_DYNAMIC = HBU_CPU_ONLY,
54
        /// same as #HBU_DETAIL_WRITE_ONLY
55
        TU_WRITE_ONLY = HBU_DETAIL_WRITE_ONLY,
56
        /// same as #HBU_GPU_ONLY
57
        TU_STATIC_WRITE_ONLY = HBU_GPU_ONLY,
58
        /// same as #HBU_CPU_TO_GPU
59
        TU_DYNAMIC_WRITE_ONLY = HBU_CPU_TO_GPU,
60
        /// @deprecated do not use
61
        TU_DYNAMIC_WRITE_ONLY_DISCARDABLE = HBU_CPU_TO_GPU,
62
        /// Mipmaps will be automatically generated for this texture
63
        TU_AUTOMIPMAP = 0x10,
64
        /** This texture will be a render target, i.e. used as a target for render to texture
65
            setting this flag will ignore all other texture usages except TU_AUTOMIPMAP, TU_UNORDERED_ACCESS, TU_NOT_SAMPLED */
66
        TU_RENDERTARGET = 0x20,
67
        /// Texture will not be sampled inside a shader i.e. used as regular texture.
68
        /// When used with TU_RENDERTARGET this can improve performance and compatibility with FL9.1 hardware
69
        TU_NOT_SAMPLED = 0x40,
70
        /// Texture can be bound as an Unordered Access View
71
        /// (imageStore/imageRead/glBindImageTexture in GL jargon)
72
        TU_UNORDERED_ACCESS = 0x80,
73
        /// Create only a single render target which will be used for all layers
74
        /// only relevant for layered textures like 2D arrays and cube maps
75
        TU_TARGET_ALL_LAYERS = 0x100,
76
        /// Default to automatic mipmap generation static textures
77
        TU_DEFAULT = TU_AUTOMIPMAP | HBU_GPU_ONLY,
78
79
        /// @deprecated
80
        TU_UAV_NOT_SRV = TU_UNORDERED_ACCESS | TU_NOT_SAMPLED,
81
        /// @deprecated
82
        TU_NOT_SRV = TU_NOT_SAMPLED,
83
        /// @deprecated
84
        TU_NOTSHADERRESOURCE = TU_NOT_SAMPLED,
85
        /// @deprecated
86
        TU_UAV = TU_UNORDERED_ACCESS
87
    };
88
89
    /** Enum identifying the texture access privilege
90
     */
91
    enum TextureAccess
92
    {
93
        TA_READ = 0x01,
94
        TA_WRITE = 0x10,
95
        TA_READ_WRITE = TA_READ | TA_WRITE
96
    };
97
98
    /** Enum identifying the texture type
99
    */
100
    enum TextureType : uint8
101
    {
102
        /// 1D texture, used in combination with 1D texture coordinates
103
        TEX_TYPE_1D = 1,
104
        /// 2D texture, used in combination with 2D texture coordinates (default)
105
        TEX_TYPE_2D = 2,
106
        /// 3D volume texture, used in combination with 3D texture coordinates
107
        TEX_TYPE_3D = 3,
108
        /// cube map (six two dimensional textures, one for each cube face), used in combination with 3D
109
        /// texture coordinates
110
        TEX_TYPE_CUBE_MAP = 4,
111
        /// 2D texture array
112
        TEX_TYPE_2D_ARRAY = 5,
113
        /// GLES2 only OES texture type
114
        TEX_TYPE_EXTERNAL_OES = 6
115
    };
116
117
    /** Enum identifying special mipmap numbers
118
    */
119
    enum TextureMipmap
120
    {
121
        /// Generate mipmaps up to 1x1
122
        MIP_UNLIMITED = 0x7FFFFFFF,
123
        /// Use TextureManager default
124
        MIP_DEFAULT = -1
125
    };
126
127
    /** Abstract class representing a Texture resource.
128
129
        The actual concrete subclass which will exist for a texture
130
        is dependent on the rendering system in use (Direct3D, OpenGL etc).
131
        This class represents the commonalities, and is the one 'used'
132
        by programmers even though the real implementation could be
133
        different in reality. Texture objects are created through
134
        the 'create' method of the TextureManager concrete subclass.
135
     */
136
    class _OgreExport Texture : public Resource
137
    {
138
    public:
139
        Texture(ResourceManager* creator, const String& name, ResourceHandle handle,
140
            const String& group, bool isManual = false, ManualResourceLoader* loader = 0);
141
142
0
        virtual ~Texture() {}
143
        
144
        /** Sets the type of texture; can only be changed before load() 
145
        */
146
0
        void setTextureType(TextureType ttype ) { mTextureType = ttype; }
147
148
        /** Gets the type of texture 
149
        */
150
0
        TextureType getTextureType(void) const { return mTextureType; }
151
152
        /** D3D11 only: set a shared surface to use for this texture before loading
153
         *
154
         * Useful for WPF interop
155
         */
156
0
        virtual void _setD3D11Surface(void* surface) {}
157
158
        /** Gets the number of mipmaps to be used for this texture.
159
        */
160
0
        uint32 getNumMipmaps(void) const {return mNumMipmaps;}
161
162
        /** Sets the number of mipmaps to be used for this texture.
163
            @note
164
                Must be set before calling any 'load' method.
165
        */
166
        void setNumMipmaps(uint32 num)
167
0
        {
168
0
            mNumRequestedMipmaps = mNumMipmaps = num;
169
0
            if (!num)
170
0
                mUsage &= ~TU_AUTOMIPMAP;
171
0
        }
172
173
        /** Are mipmaps hardware generated?
174
175
            Will only be accurate after texture load, or createInternalResources
176
        */
177
0
        bool getMipmapsHardwareGenerated(void) const { return mMipmapsHardwareGenerated; }
178
179
        /** Returns the gamma adjustment factor applied to this texture on loading.
180
        */
181
0
        float getGamma(void) const { return mGamma; }
182
183
        /** Sets the gamma adjustment factor applied to this texture on loading the
184
            data.
185
            @note
186
                Must be called before any 'load' method. This gamma factor will
187
                be premultiplied in and may reduce the precision of your textures.
188
                You can use setHardwareGamma if supported to apply gamma on 
189
                sampling the texture instead.
190
        */
191
0
        void setGamma(float g) { mGamma = g; }
192
193
        /** Sets whether this texture will be set up so that on sampling it, 
194
            hardware gamma correction is applied.
195
196
            24-bit textures are often saved in gamma colour space; this preserves
197
            precision in the 'darks'. However, if you're performing blending on 
198
            the sampled colours, you really want to be doing it in linear space. 
199
            One way is to apply a gamma correction value on loading (see setGamma),
200
            but this means you lose precision in those dark colours. An alternative
201
            is to get the hardware to do the gamma correction when reading the 
202
            texture and converting it to a floating point value for the rest of
203
            the pipeline. This option allows you to do that; it's only supported
204
            in relatively recent hardware (others will ignore it) but can improve
205
            the quality of colour reproduction.
206
        @note
207
            Must be called before any 'load' method since it may affect the
208
            construction of the underlying hardware resources.
209
            Also note this only useful on textures using 8-bit colour channels.
210
        */
211
0
        void setHardwareGammaEnabled(bool enabled) { mHwGamma = enabled; }
212
213
        /** Gets whether this texture will be set up so that on sampling it, 
214
        hardware gamma correction is applied.
215
        */
216
0
        bool isHardwareGammaEnabled() const { return mHwGamma; }
217
218
        /** Set the level of multisample AA to be used if this texture is a 
219
            rendertarget.
220
        @note This option will be ignored if TU_RENDERTARGET is not part of the
221
            usage options on this texture, or if the hardware does not support it. 
222
        @param fsaa The number of samples
223
        @param fsaaHint Any hinting text (see Root::createRenderWindow)
224
        */
225
0
        void setFSAA(uint fsaa, const String& fsaaHint) { mFSAA = fsaa; mFSAAHint = fsaaHint; }
226
227
        /** Get the level of multisample AA to be used if this texture is a 
228
        rendertarget.
229
        */
230
0
        uint getFSAA() const { return mFSAA; }
231
232
        /** Get the multisample AA hint if this texture is a rendertarget.
233
        */
234
0
        const String& getFSAAHint() const { return mFSAAHint; }
235
236
        /** Returns the height of the texture.
237
        */
238
0
        uint32 getHeight(void) const { return mHeight; }
239
240
        /** Returns the width of the texture.
241
        */
242
0
        uint32 getWidth(void) const { return mWidth; }
243
244
        /** Returns the depth of the texture (only applicable for 3D textures).
245
        */
246
0
        uint32 getDepth(void) const { return mDepth; }
247
248
        /** Returns the height of the original input texture (may differ due to hardware requirements).
249
        */
250
0
        uint32 getSrcHeight(void) const { return mSrcHeight; }
251
252
        /** Returns the width of the original input texture (may differ due to hardware requirements).
253
        */
254
0
        uint32 getSrcWidth(void) const { return mSrcWidth; }
255
256
        /** Returns the original depth of the input texture (only applicable for 3D textures).
257
        */
258
0
        uint32 getSrcDepth(void) const { return mSrcDepth; }
259
260
        /** Set the height of the texture; can only do this before load();
261
        */
262
0
        void setHeight(uint32 h) { mHeight = mSrcHeight = h; }
263
264
        /** Set the width of the texture; can only do this before load();
265
        */
266
0
        void setWidth(uint32 w) { mWidth = mSrcWidth = w; }
267
268
        /** Set the depth of the texture (only applicable for 3D textures);
269
            can only do this before load();
270
        */
271
0
        void setDepth(uint32 d)  { mDepth = mSrcDepth = d; }
272
273
        /** Returns the TextureUsage identifier for this Texture
274
        */
275
        int getUsage() const
276
0
        {
277
0
            return mUsage;
278
0
        }
279
280
        /** Sets the TextureUsage identifier for this Texture; only useful before load()
281
            
282
            @param u is a combination of TU_STATIC, TU_DYNAMIC, TU_WRITE_ONLY 
283
                TU_AUTOMIPMAP and TU_RENDERTARGET (see TextureUsage enum). You are
284
                strongly advised to use HBU_GPU_ONLY wherever possible, if you need to
285
                update regularly, consider HBU_CPU_TO_GPU.
286
        */
287
0
        void setUsage(int u) { mUsage = u; }
288
289
        /** Creates the internal texture resources for this texture. 
290
291
            This method creates the internal texture resources (pixel buffers, 
292
            texture surfaces etc) required to begin using this texture. You do
293
            not need to call this method directly unless you are manually creating
294
            a texture, in which case something must call it, after having set the
295
            size and format of the texture (e.g. the ManualResourceLoader might
296
            be the best one to call it). If you are not defining a manual texture,
297
            or if you use one of the self-contained load...() methods, then it will be
298
            called for you.
299
        */
300
        void createInternalResources(void);
301
        
302
        /** Copies (and maybe scales to fit) the contents of this texture to
303
            another texture. */
304
        virtual void copyToTexture( TexturePtr& target );
305
306
        /** Loads the data from an image.
307
        @attention only call this from outside the load() routine of a 
308
            Resource. Don't call it within (including ManualResourceLoader) - use
309
            _loadImages() instead. This method is designed to be external, 
310
            performs locking and checks the load status before loading.
311
        */
312
        void loadImage( const Image &img );
313
            
314
        /** Loads the data from a raw stream.
315
        @attention only call this from outside the load() routine of a 
316
            Resource. Don't call it within (including ManualResourceLoader) - use
317
            _loadImages() instead. This method is designed to be external, 
318
            performs locking and checks the load status before loading.
319
        @param stream Data stream containing the raw pixel data
320
        @param uWidth Width of the image
321
        @param uHeight Height of the image
322
        @param eFormat The format of the pixel data
323
        */
324
        void loadRawData( DataStreamPtr& stream,
325
            ushort uWidth, ushort uHeight, PixelFormat eFormat);
326
327
        /** Internal method to load the texture from a set of images. 
328
        @attention Do NOT call this method unless you are inside the load() routine
329
            already, e.g. a ManualResourceLoader. It is not threadsafe and does
330
            not check or update resource loading status.
331
        */
332
        void _loadImages( const ConstImagePtrList& images );
333
334
        /** Returns the pixel format for the texture surface. */
335
        PixelFormat getFormat() const
336
0
        {
337
0
            return mFormat;
338
0
        }
339
340
        /** Returns the desired pixel format for the texture surface. */
341
        PixelFormat getDesiredFormat(void) const
342
0
        {
343
0
            return mDesiredFormat;
344
0
        }
345
346
        /** Returns the pixel format of the original input texture (may differ due to
347
            hardware requirements and pixel format conversion).
348
        */
349
        PixelFormat getSrcFormat(void) const
350
0
        {
351
0
            return mSrcFormat;
352
0
        }
353
354
        /** Sets the desired pixel format for the texture surface; can only be set before load(). */
355
        void setFormat(PixelFormat pf);
356
357
        /** Returns true if the texture has an alpha layer. */
358
        bool hasAlpha(void) const;
359
360
        /** Sets desired bit depth for integer pixel format textures.
361
362
            Available values: 0, 16 and 32, where 0 (the default) means keep original format
363
            as it is. This value is number of bits for the pixel.
364
        */
365
        void setDesiredIntegerBitDepth(ushort bits);
366
367
        /** gets desired bit depth for integer pixel format textures.
368
        */
369
        ushort getDesiredIntegerBitDepth(void) const;
370
371
        /** Sets desired bit depth for float pixel format textures.
372
373
            Available values: 0, 16 and 32, where 0 (the default) means keep original format
374
            as it is. This value is number of bits for a channel of the pixel.
375
        */
376
        void setDesiredFloatBitDepth(ushort bits);
377
378
        /** gets desired bit depth for float pixel format textures.
379
        */
380
        ushort getDesiredFloatBitDepth(void) const;
381
382
        /** Sets desired bit depth for integer and float pixel format.
383
        */
384
        void setDesiredBitDepths(ushort integerBits, ushort floatBits);
385
386
        /// @deprecated use setFormat(PF_A8)
387
        OGRE_DEPRECATED void setTreatLuminanceAsAlpha(bool asAlpha);
388
389
        /** Return the number of faces this texture has. This will be 6 for a cubemap
390
            texture and 1 for a 1D, 2D or 3D one.
391
        */
392
0
        uint32 getNumFaces() const { return mTextureType == TEX_TYPE_CUBE_MAP ? 6 : 1; }
393
394
        /// Returns 6 for cubemaps and the the depth otherwise
395
0
        uint32 getNumLayers() const { return mTextureType == TEX_TYPE_CUBE_MAP ? 6 : mDepth; }
396
397
        /// Convenience method for unified cubemap and 2D array access
398
        RenderTarget* getRenderTarget(size_t slice=0, size_t mipmap=0);
399
400
        /** Return hardware pixel buffer for a surface. This buffer can then
401
            be used to copy data from and to a particular level of the texture.
402
            @param face Face number, in case of a cubemap texture. Must be 0
403
            for other types of textures.
404
            For cubemaps, this is one of 
405
            +X (0), -X (1), +Y (2), -Y (3), +Z (4), -Z (5)
406
            @param mipmap Mipmap level. This goes from 0 for the first, largest
407
            mipmap level to getNumMipmaps()-1 for the smallest.
408
            @return A shared pointer to a hardware pixel buffer.
409
            @remarks The buffer is invalidated when the resource is unloaded or destroyed.
410
            Do not use it after the lifetime of the containing texture.
411
        */
412
        virtual const HardwarePixelBufferPtr& getBuffer(size_t face=0, size_t mipmap=0);
413
414
415
        /** Populate an Image with the contents of this texture. 
416
            @param destImage The target image (contents will be overwritten)
417
            @param includeMipMaps Whether to embed mipmaps in the image
418
        */
419
        void convertToImage(Image& destImage, bool includeMipMaps = false);
420
        
421
        /** Retrieve a platform or API-specific piece of information from this texture.
422
            This method of retrieving information should only be used if you know what you're doing.
423
424
            | Name        | Description                  |
425
            |-------------|------------------------------|
426
            | GLID        | The OpenGL texture object id |
427
428
            @param name The name of the attribute to retrieve.
429
            @param pData Pointer to memory matching the type of data you want to retrieve.
430
        */
431
        virtual void getCustomAttribute(const String& name, void* pData);
432
        
433
        /** simplified API for bindings
434
         * 
435
         * @overload
436
         */
437
        uint getCustomAttribute(const String& name)
438
0
        {
439
0
            uint ret = 0;
440
0
            getCustomAttribute(name, &ret);
441
0
            return ret;
442
0
        }
443
444
        /** Enable read and/or write privileges to the texture from shaders.
445
            @param bindPoint The buffer binding location for shader access. For OpenGL this must be unique and is not related to the texture binding point.
446
            @param access The texture access privileges given to the shader.
447
            @param mipmapLevel The texture mipmap level to use.
448
            @param textureArrayIndex The index of the texture array to use. If texture is not a texture array, set to 0.
449
            @param format Texture format to be read in by shader. For OpenGL this may be different than the bound texture format.
450
        */
451
        virtual void createShaderAccessPoint(uint bindPoint, TextureAccess access = TA_READ_WRITE,
452
                                        int mipmapLevel = 0, int textureArrayIndex = 0,
453
0
                                        PixelFormat format = PF_UNKNOWN) {}
454
        /** Set image names to be loaded as layers (3d & texture array) or cubemap faces
455
         */
456
        void setLayerNames(const std::vector<String>& names)
457
0
        {
458
0
            mLayerNames = names;
459
0
        }
460
461
    protected:
462
        uint32 mHeight;
463
        uint32 mWidth;
464
        uint32 mDepth;
465
466
        uint32 mNumRequestedMipmaps;
467
        uint32 mNumMipmaps;
468
469
        float mGamma;
470
        uint mFSAA;
471
472
        PixelFormat mFormat;
473
        int mUsage; /// Bit field, so this can't be TextureUsage
474
475
        PixelFormat mSrcFormat;
476
        uint32 mSrcWidth, mSrcHeight, mSrcDepth;
477
478
        bool mTreatLuminanceAsAlpha;
479
        bool mInternalResourcesCreated;
480
        bool mMipmapsHardwareGenerated;
481
        bool mHwGamma;
482
483
        String mFSAAHint;
484
485
        /// Vector of pointers to subsurfaces
486
        typedef std::vector<HardwarePixelBufferSharedPtr> SurfaceList;
487
        SurfaceList mSurfaceList;
488
489
        TextureType mTextureType;
490
491
        void prepareImpl() override;
492
        void unprepareImpl() override;
493
        void loadImpl() override;
494
495
        /// @copydoc Resource::calculateSize
496
        size_t calculateSize(void) const override;
497
        
498
499
        /** Implementation of creating internal texture resources 
500
        */
501
        virtual void createInternalResourcesImpl(void) = 0;
502
503
        /** Implementation of freeing internal texture resources 
504
        */
505
        virtual void freeInternalResourcesImpl(void) = 0;
506
507
        virtual HardwarePixelBufferPtr createSurface(uint32 face, uint32 mip, uint32 width, uint32 height, uint32 depth)
508
0
        {
509
0
            return nullptr;
510
0
        }
511
512
        /// internal method, create HardwarePixelBuffers for every face and
513
        /// mipmap level. This method must be called after the texture object was created
514
        void createSurfaceList(void);
515
516
        /** Default implementation of unload which calls freeInternalResources */
517
        void unloadImpl(void) override;
518
519
        /** Returns the maximum number of Mipmaps that can be generated until we reach
520
        the mininum possible size. This does not count the base level.
521
522
        @return how many times we can divide this texture in 2 until we reach 1x1.
523
        */
524
        uint32 getMaxMipmaps() const;
525
526
    private:
527
        uchar mDesiredIntegerBitDepth;
528
        uchar mDesiredFloatBitDepth;
529
        PixelFormat mDesiredFormat;
530
531
        /// vector of images that should be loaded (cubemap/ texture array)
532
        std::vector<String> mLayerNames;
533
534
        /** Vector of images that were pulled from disk by
535
            prepareLoad but have yet to be pushed into texture memory
536
            by loadImpl.  Images should be deleted by loadImpl and unprepareImpl.
537
        */
538
        typedef std::vector<Image> LoadedImages;
539
        LoadedImages mLoadedImages;
540
541
        void readImage(LoadedImages& imgs, const String& name, const String& ext, bool haveNPOT);
542
        void freeInternalResources(void);
543
    };
544
    /** @} */
545
    /** @} */
546
547
}
548
549
#include "OgreHeaderSuffix.h"
550
551
#endif