Coverage Report

Created: 2026-01-09 06:47

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