summaryrefslogtreecommitdiffstats
path: root/Graphics/GraphicsTools/interface/DynamicTextureAtlas.h
blob: 4bb96ff412b0a2ae7ed690856d01c25d0d1fba2e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/*
 *  Copyright 2019-2021 Diligent Graphics LLC
 *  Copyright 2015-2019 Egor Yusov
 *  
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *  
 *      http://www.apache.org/licenses/LICENSE-2.0
 *  
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 *  In no event and under no legal theory, whether in tort (including negligence), 
 *  contract, or otherwise, unless required by applicable law (such as deliberate 
 *  and grossly negligent acts) or agreed to in writing, shall any Contributor be
 *  liable for any damages, including any direct, indirect, special, incidental, 
 *  or consequential damages of any character arising as a result of this License or 
 *  out of the use or inability to use the software (including but not limited to damages 
 *  for loss of goodwill, work stoppage, computer failure or malfunction, or any and 
 *  all other commercial damages or losses), even if such Contributor has been advised 
 *  of the possibility of such damages.
 */

#pragma once

/// \file
/// Declaration of DynamicTextureAtlas interface and related data structures

#include "../../GraphicsEngine/interface/RenderDevice.h"
#include "../../GraphicsEngine/interface/DeviceContext.h"
#include "../../GraphicsEngine/interface/Texture.h"
#include "../../../Common/interface/BasicMath.hpp"

namespace Diligent
{

struct IDynamicTextureAtlas;

// {F7ACDCFB-074F-4E31-94C5-DD4C988E4532}
static const INTERFACE_ID IID_TextureAtlasSuballocation =
    {0xf7acdcfb, 0x74f, 0x4e31, {0x94, 0xc5, 0xdd, 0x4c, 0x98, 0x8e, 0x45, 0x32}};


// {00E1D6FA-47B4-4062-B96C-D3E191A02351}
static const INTERFACE_ID IID_DynamicTextureAtlas =
    {0xe1d6fa, 0x47b4, 0x4062, {0xb9, 0x6c, 0xd3, 0xe1, 0x91, 0xa0, 0x23, 0x51}};


/// Dynamic texture atlas suballocation.
struct ITextureAtlasSuballocation : public IObject
{
    /// Returns the suballocation origin.
    virtual uint2 GetOrigin() const = 0;

    /// Returns the suballocation slice.
    virtual Uint32 GetSlice() const = 0;

    /// Returns the suballocation size.
    virtual uint2 GetSize() const = 0;

    /// Returns the texture coordinate scale (xy) and bias (zw).
    virtual float4 GetUVScaleBias() const = 0;

    /// Returns the pointer to the parent texture atlas.
    virtual IDynamicTextureAtlas* GetAtlas() = 0;

    /// Stores a pointer to the user-provided data object, which
    /// may later be retreived through GetUserData().
    ///
    /// \param [in] pUserData - Pointer to the user data object to store.
    ///
    /// \note   The method is not thread-safe and an application
    ///         must externally synchronize the access.
    virtual void SetUserData(IObject* pUserData) = 0;

    /// Returns the pointer to the user data object previously
    /// set with SetUserData() method.
    ///
    /// \return     Pointer to the user data object
    virtual IObject* GetUserData() const = 0;
};

/// Dynamic texture atlas.
struct IDynamicTextureAtlas : public IObject
{
    /// Returns the pointer to the internal texture object.

    /// \param[in]  pDevice  - Pointer to the render device that will be used to
    ///                        create a new internal texture array, if necessary.
    /// \param[in]  pContext - Pointer to the device context that will be used to
    ///                        copy existing contents to the new texture array, if
    ///                        necessary.
    ///
    /// \remarks    If the internal texture needs to be resized, pDevice and pContext will
    ///             be used to create a new texture and copy existing contents to it.
    ///
    ///             The method is not thread safe. An application must externally synchronize
    ///             the access.
    virtual ITexture* GetTexture(IRenderDevice* pDevice, IDeviceContext* pContext) = 0;


    /// Performs suballocation from the atlas.

    /// \param[in]  Width           - Suballocation width.
    /// \param[in]  Height          - Suballocation height.
    /// \param[out] ppSuballocation - Memory location where pointer to the new suballocation will be
    ///                               stored.
    ///
    /// \remarks    The method is thread-safe and can be called from multiple threads simultaneously.
    ///
    ///             Internal texture array may need to be extended after the allocation happend.
    ///             An application may call GetTexture() to ensure that the texture is resized and old
    ///             contents is copied.
    virtual void Allocate(Uint32                       Width,
                          Uint32                       Height,
                          ITextureAtlasSuballocation** ppSuballocation) = 0;


    /// Returns the texture atlas description
    virtual const TextureDesc& GetAtlasDesc() const = 0;

    /// Returns internal texture array version. The version is incremented every time
    /// the array is expanded.
    virtual Uint32 GetVersion() const = 0;
};


/// Dynamic texture atlas create information.
struct DynamicTextureAtlasCreateInfo
{
    /// Texture description

    /// Texture type must be 2D or 2D array. When the type is
    /// texture 2D, resizes will be disabled.
    TextureDesc Desc;


    /// Texture region allocation granularity.

    /// The width and height of the texture region will be aligned to the
    /// TextureGranularity value.
    /// Texture granularity must be power of two.
    Uint32 TextureGranularity = 128;

    /// The number of extra slices.

    /// When non-zero, the array will be expanded by the specified number of slices every time
    /// there is insufficient space. If zero, the array size will be doubled when
    /// more space is needed.
    Uint32 ExtraSliceCount = 0;


    /// Maximum number of slices in texture array.
    Uint32 MaxSliceCount = 2048;


    /// Allocation granularity for ITextureAtlasSuballocation objects.

    /// Texture atlas uses FixedBlockMemoryAllocator to allocate instances
    /// of ITextureAtlasSuballocation implementation class. This memeber defines
    /// the number of objects in one page.
    Uint32 SuballocationObjAllocationGranularity = 64;
};

/// Creates a new dynamic texture atlas.

/// \param[in] pDevice    - Pointer to the render device that will be used to create internal
///                         texture array. If this parameter is null, the texture will be created
///                         when GetTexture() is called.
/// \param[in] CreateInfo - Atlas create info, see Diligent::DynamicTextureAtlasCreateInfo.
/// \param[in] ppAtlas    - Memory location where pointer to the texture atlas object will be written.
void CreateDynamicTextureAtlas(IRenderDevice*                       pDevice,
                               const DynamicTextureAtlasCreateInfo& CreateInfo,
                               IDynamicTextureAtlas**               ppAtlas);

} // namespace Diligent