summaryrefslogtreecommitdiffstats
path: root/Graphics/GraphicsEngineOpenGL/include/ShaderVariableManagerGL.hpp
blob: cadf1eee6ff37b4d05c81a7f2958beb1e581aa4a (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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
/*
 *  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

// ShaderVariableManagerGL class manages static resources of a pipeline resource signature, and
// all types of resources for an SRB.

//
//        .-==========================-.              _______________________________________________________________________________________________________________
//        ||                          ||             |           |           |       |            |            |       |            |         |           |          |
//      __|| ShaderVariableManagerGL  ||------------>| UBInfo[0] | UBInfo[1] |  ...  | TexInfo[0] | TexInfo[1] |  ...  | ImgInfo[0] |   ...   |  SSBO[0]  |   ...    |
//     |  ||                          ||             |___________|___________|_______|____________|____________|_______|____________|_________|___________|__________|
//     |  '-==========================-'                          /                         \                              |
//     |                 |                                 m_ResIndex                   m_ResIndex                    m_ResIndex
//     |            m_pSignature                               /                              \                            |
//     |    _____________V___________________         ________V________________________________V___________________________V__________________________________________
//     |   |                                 |       |          |          |       |        |        |       |        |        |       |          |          |       |
//     |   | PipelineResourceSignatureGLImpl |------>|   UB[0]  |   UB[1]  |  ...  | Tex[0] | Tex[1] |  ...  | Img[0] | Img[1] |  ...  | SSBOs[0] | SSBOs[1] |  ...  |
//     |   |_________________________________|       |__________|__________|_______|________|________|_______|________|________|_______|__________|__________|_______|
//     |                                                  |           |                |         |                |        |                |           |
// m_ResourceCache                                     Binding     Binding          Binding    Binding          Binding  Binding         Binding      Binding
//     |                                                  |           |                |         |                |        |                |           |
//     |    _______________________                   ____V___________V________________V_________V________________V________V________________V___________V_____________
//     |   |                       |                 |                           |                           |                           |                           |
//     '-->| ShaderResourceCacheGL |--------     --->|      Uinform Buffers      |          Textures         |          Images           |       Storge Buffers      |
//         |_______________________|                 |___________________________|___________________________|___________________________|___________________________|
//

#include <array>

#include "Object.h"
#include "PipelineResourceAttribsGL.hpp"
#include "ShaderResourceVariableBase.hpp"
#include "ShaderResourceCacheGL.hpp"

namespace Diligent
{

class PipelineResourceSignatureGLImpl;

// sizeof(ShaderVariableManagerGL) == 40 (x64, msvc, Release)
class ShaderVariableManagerGL
{
public:
    ShaderVariableManagerGL(IObject& Owner, ShaderResourceCacheGL& ResourceCache) noexcept :
        m_Owner(Owner),
        m_ResourceCache{ResourceCache}
    {}

    ~ShaderVariableManagerGL();

    void Destroy(IMemoryAllocator& Allocator);

    // No copies, only moves are allowed
    // clang-format off
    ShaderVariableManagerGL             (const ShaderVariableManagerGL&)  = delete;
    ShaderVariableManagerGL& operator = (const ShaderVariableManagerGL&)  = delete;
    ShaderVariableManagerGL             (      ShaderVariableManagerGL&&) = default;
    ShaderVariableManagerGL& operator = (      ShaderVariableManagerGL&&) = delete;
    // clang-format on

    void Initialize(const PipelineResourceSignatureGLImpl& Signature,
                    IMemoryAllocator&                      Allocator,
                    const SHADER_RESOURCE_VARIABLE_TYPE*   AllowedVarTypes,
                    Uint32                                 NumAllowedTypes,
                    SHADER_TYPE                            ShaderType);

    static size_t GetRequiredMemorySize(const PipelineResourceSignatureGLImpl& Signature,
                                        const SHADER_RESOURCE_VARIABLE_TYPE*   AllowedVarTypes,
                                        Uint32                                 NumAllowedTypes,
                                        SHADER_TYPE                            ShaderType);

    using ResourceAttribs = PipelineResourceAttribsGL;

    // These two methods can't be implemented in the header because they depend on PipelineResourceSignatureGLImpl
    const PipelineResourceDesc& GetResourceDesc(Uint32 Index) const;
    const ResourceAttribs&      GetResourceAttribs(Uint32 Index) const;

    template <typename ThisImplType>
    struct GLVariableBase : public ShaderVariableBase<ThisImplType, ShaderVariableManagerGL>
    {
    public:
        GLVariableBase(ShaderVariableManagerGL& ParentLayout, Uint32 ResIndex) :
            ShaderVariableBase<ThisImplType, ShaderVariableManagerGL>{ParentLayout, ResIndex}
        {}

        const ResourceAttribs& GetAttribs() const { return this->m_ParentManager.GetResourceAttribs(this->m_ResIndex); }
    };


    struct UniformBuffBindInfo final : GLVariableBase<UniformBuffBindInfo>
    {
        UniformBuffBindInfo(ShaderVariableManagerGL& ParentLayout, Uint32 ResIndex) :
            GLVariableBase<UniformBuffBindInfo>{ParentLayout, ResIndex}
        {}

        void BindResource(IDeviceObject* pObject, Uint32 ArrayIndex);

        virtual bool DILIGENT_CALL_TYPE IsBound(Uint32 ArrayIndex) const override final
        {
            VERIFY_EXPR(ArrayIndex < GetDesc().ArraySize);
            return m_ParentManager.m_ResourceCache.IsUBBound(GetAttribs().CacheOffset + ArrayIndex);
        }
    };


    struct TextureBindInfo final : GLVariableBase<TextureBindInfo>
    {
        TextureBindInfo(ShaderVariableManagerGL& ParentLayout, Uint32 ResIndex) :
            GLVariableBase<TextureBindInfo>{ParentLayout, ResIndex}
        {}

        void BindResource(IDeviceObject* pObject, Uint32 ArrayIndex);

        virtual bool DILIGENT_CALL_TYPE IsBound(Uint32 ArrayIndex) const override final
        {
            const auto& Desc = GetDesc();
            VERIFY_EXPR(ArrayIndex < Desc.ArraySize);
            const bool IsTexView = (Desc.ResourceType == SHADER_RESOURCE_TYPE_TEXTURE_SRV || Desc.ResourceType == SHADER_RESOURCE_TYPE_INPUT_ATTACHMENT);
            return m_ParentManager.m_ResourceCache.IsTextureBound(GetAttribs().CacheOffset + ArrayIndex, IsTexView);
        }
    };


    struct ImageBindInfo final : GLVariableBase<ImageBindInfo>
    {
        ImageBindInfo(ShaderVariableManagerGL& ParentLayout, Uint32 ResIndex) :
            GLVariableBase<ImageBindInfo>{ParentLayout, ResIndex}
        {}

        void BindResource(IDeviceObject* pObject, Uint32 ArrayIndex);

        virtual bool DILIGENT_CALL_TYPE IsBound(Uint32 ArrayIndex) const override final
        {
            const auto& Desc = GetDesc();
            VERIFY_EXPR(ArrayIndex < Desc.ArraySize);
            const bool IsImgView = (Desc.ResourceType == SHADER_RESOURCE_TYPE_TEXTURE_SRV || Desc.ResourceType == SHADER_RESOURCE_TYPE_TEXTURE_UAV);
            return m_ParentManager.m_ResourceCache.IsImageBound(GetAttribs().CacheOffset + ArrayIndex, IsImgView);
        }
    };


    struct StorageBufferBindInfo final : GLVariableBase<StorageBufferBindInfo>
    {
        StorageBufferBindInfo(ShaderVariableManagerGL& ParentLayout, Uint32 ResIndex) :
            GLVariableBase<StorageBufferBindInfo>{ParentLayout, ResIndex}
        {}

        void BindResource(IDeviceObject* pObject, Uint32 ArrayIndex);

        virtual bool DILIGENT_CALL_TYPE IsBound(Uint32 ArrayIndex) const override final
        {
            VERIFY_EXPR(ArrayIndex < GetDesc().ArraySize);
            return m_ParentManager.m_ResourceCache.IsSSBOBound(GetAttribs().CacheOffset + ArrayIndex);
        }
    };

    void BindResources(IResourceMapping* pResourceMapping, Uint32 Flags);

    IShaderResourceVariable* GetVariable(const Char* Name) const;
    IShaderResourceVariable* GetVariable(Uint32 Index) const;

    IObject& GetOwner() { return m_Owner; }

    Uint32 GetVariableCount() const
    {
        return GetNumUBs() + GetNumTextures() + GetNumImages() + GetNumStorageBuffers();
    }

    // clang-format off
    Uint32 GetNumUBs()            const { return (m_TextureOffset       - m_UBOffset)            / sizeof(UniformBuffBindInfo);    }
    Uint32 GetNumTextures()       const { return (m_ImageOffset         - m_TextureOffset)       / sizeof(TextureBindInfo);        }
    Uint32 GetNumImages()         const { return (m_StorageBufferOffset - m_ImageOffset)         / sizeof(ImageBindInfo) ;         }
    Uint32 GetNumStorageBuffers() const { return (m_VariableEndOffset   - m_StorageBufferOffset) / sizeof(StorageBufferBindInfo);  }
    // clang-format on

    template <typename ResourceType> Uint32 GetNumResources() const;

    template <typename ResourceType>
    const ResourceType& GetConstResource(Uint32 ResIndex) const
    {
        VERIFY(ResIndex < GetNumResources<ResourceType>(), "Resource index (", ResIndex, ") exceeds max allowed value (", GetNumResources<ResourceType>(), ")");
        auto Offset = GetResourceOffset<ResourceType>();
        return reinterpret_cast<const ResourceType*>(reinterpret_cast<const Uint8*>(m_ResourceBuffer) + Offset)[ResIndex];
    }

    Uint32 GetVariableIndex(const IShaderResourceVariable& Var) const;

private:
    struct ResourceCounters
    {
        Uint32 NumUBs           = 0;
        Uint32 NumTextures      = 0;
        Uint32 NumImages        = 0;
        Uint32 NumStorageBlocks = 0;
    };
    static ResourceCounters CountResources(const PipelineResourceSignatureGLImpl& Signature,
                                           const SHADER_RESOURCE_VARIABLE_TYPE*   AllowedVarTypes,
                                           Uint32                                 NumAllowedTypes,
                                           SHADER_TYPE                            ShaderType);

    // Offsets in bytes
    using OffsetType = Uint16;

    template <typename ResourceType> OffsetType GetResourceOffset() const;

    template <typename ResourceType>
    ResourceType& GetResource(Uint32 ResIndex) const
    {
        VERIFY(ResIndex < GetNumResources<ResourceType>(), "Resource index (", ResIndex, ") exceeds max allowed value (", GetNumResources<ResourceType>() - 1, ")");
        auto Offset = GetResourceOffset<ResourceType>();
        return reinterpret_cast<ResourceType*>(reinterpret_cast<Uint8*>(m_ResourceBuffer) + Offset)[ResIndex];
    }

    template <typename ResourceType>
    IShaderResourceVariable* GetResourceByName(const Char* Name) const;

    template <typename THandleUB,
              typename THandleTexture,
              typename THandleImage,
              typename THandleStorageBuffer>
    void HandleResources(THandleUB            HandleUB,
                         THandleTexture       HandleTexture,
                         THandleImage         HandleImage,
                         THandleStorageBuffer HandleStorageBuffer)
    {
        for (Uint32 ub = 0; ub < GetNumResources<UniformBuffBindInfo>(); ++ub)
            HandleUB(GetResource<UniformBuffBindInfo>(ub));

        for (Uint32 s = 0; s < GetNumResources<TextureBindInfo>(); ++s)
            HandleTexture(GetResource<TextureBindInfo>(s));

        for (Uint32 i = 0; i < GetNumResources<ImageBindInfo>(); ++i)
            HandleImage(GetResource<ImageBindInfo>(i));

        for (Uint32 s = 0; s < GetNumResources<StorageBufferBindInfo>(); ++s)
            HandleStorageBuffer(GetResource<StorageBufferBindInfo>(s));
    }

    template <typename THandleUB,
              typename THandleTexture,
              typename THandleImage,
              typename THandleStorageBuffer>
    void HandleConstResources(THandleUB            HandleUB,
                              THandleTexture       HandleTexture,
                              THandleImage         HandleImage,
                              THandleStorageBuffer HandleStorageBuffer) const
    {
        for (Uint32 ub = 0; ub < GetNumResources<UniformBuffBindInfo>(); ++ub)
            HandleUB(GetConstResource<UniformBuffBindInfo>(ub));

        for (Uint32 s = 0; s < GetNumResources<TextureBindInfo>(); ++s)
            HandleTexture(GetConstResource<TextureBindInfo>(s));

        for (Uint32 i = 0; i < GetNumResources<ImageBindInfo>(); ++i)
            HandleImage(GetConstResource<ImageBindInfo>(i));

        for (Uint32 s = 0; s < GetNumResources<StorageBufferBindInfo>(); ++s)
            HandleStorageBuffer(GetConstResource<StorageBufferBindInfo>(s));
    }

    friend class ShaderVariableIndexLocator;
    friend class ShaderVariableLocator;

private:
    PipelineResourceSignatureGLImpl const* m_pSignature = nullptr;

    IObject& m_Owner;
    // No need to use shared pointer, as the resource cache is either part of the same
    // ShaderGLImpl object, or ShaderResourceBindingGLImpl object
    ShaderResourceCacheGL& m_ResourceCache;
    void*                  m_ResourceBuffer = nullptr;

    static constexpr OffsetType m_UBOffset            = 0;
    OffsetType                  m_TextureOffset       = 0;
    OffsetType                  m_ImageOffset         = 0;
    OffsetType                  m_StorageBufferOffset = 0;
    OffsetType                  m_VariableEndOffset   = 0;

#ifdef DILIGENT_DEBUG
    IMemoryAllocator* m_pDbgAllocator = nullptr;
#endif
};


template <>
inline Uint32 ShaderVariableManagerGL::GetNumResources<ShaderVariableManagerGL::UniformBuffBindInfo>() const
{
    return GetNumUBs();
}

template <>
inline Uint32 ShaderVariableManagerGL::GetNumResources<ShaderVariableManagerGL::TextureBindInfo>() const
{
    return GetNumTextures();
}

template <>
inline Uint32 ShaderVariableManagerGL::GetNumResources<ShaderVariableManagerGL::ImageBindInfo>() const
{
    return GetNumImages();
}

template <>
inline Uint32 ShaderVariableManagerGL::GetNumResources<ShaderVariableManagerGL::StorageBufferBindInfo>() const
{
    return GetNumStorageBuffers();
}



template <>
inline ShaderVariableManagerGL::OffsetType ShaderVariableManagerGL::
    GetResourceOffset<ShaderVariableManagerGL::UniformBuffBindInfo>() const
{
    return m_UBOffset;
}

template <>
inline ShaderVariableManagerGL::OffsetType ShaderVariableManagerGL::
    GetResourceOffset<ShaderVariableManagerGL::TextureBindInfo>() const
{
    return m_TextureOffset;
}

template <>
inline ShaderVariableManagerGL::OffsetType ShaderVariableManagerGL::
    GetResourceOffset<ShaderVariableManagerGL::ImageBindInfo>() const
{
    return m_ImageOffset;
}

template <>
inline ShaderVariableManagerGL::OffsetType ShaderVariableManagerGL::
    GetResourceOffset<ShaderVariableManagerGL::StorageBufferBindInfo>() const
{
    return m_StorageBufferOffset;
}

} // namespace Diligent