summaryrefslogtreecommitdiffstats
path: root/Graphics/GraphicsEngineOpenGL/include/ShaderResourcesGL.hpp
blob: 7d7a954c4e601677a7485fc23963c473e4347abf (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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
/*
 *  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

// ShaderResourcesGL class allocates single continuous chunk of memory to store all program resources, as follows:
//
//
//       m_UniformBuffers        m_Textures                 m_Images                   m_StorageBlocks
//        |                       |                          |                          |                         |                  |
//        |  UB[0]  ... UB[Nu-1]  |  Tex[0]  ...  Tex[Ns-1]  |  Img[0]  ...  Img[Ni-1]  |  SB[0]  ...  SB[Nsb-1]  |  Resource Names  |
//
//  Nu  - number of uniform buffers
//  Ns  - number of samplers
//  Ni  - number of images
//  Nsb - number of storage blocks

#include <vector>

#include "Object.h"
#include "StringPool.hpp"
#include "HashUtils.hpp"
#include "ShaderResourceVariableBase.hpp"

namespace Diligent
{

class ShaderResourcesGL
{
public:
    ShaderResourcesGL() {}
    ~ShaderResourcesGL();
    // clang-format off
    ShaderResourcesGL             (ShaderResourcesGL&& Program)noexcept;

    ShaderResourcesGL             (const ShaderResourcesGL&)  = delete;
    ShaderResourcesGL& operator = (const ShaderResourcesGL&)  = delete;
    ShaderResourcesGL& operator = (      ShaderResourcesGL&&) = delete;
    // clang-format on

    /// Loads program uniforms and assigns bindings
    void LoadUniforms(SHADER_TYPE                           ShaderStages,
                      const GLObjectWrappers::GLProgramObj& GLProgram,
                      class GLContextState&                 State);

    struct GLResourceAttribs
    {
        // clang-format off
/*  0 */    const Char*                             Name;
/*  8 */    const SHADER_TYPE                       ShaderStages;
/* 12 */    const SHADER_RESOURCE_TYPE              ResourceType;
/* 16 */          Uint32                            ArraySize;
/* 20 */
/* 24 */    // End of data
        // clang-format on

        GLResourceAttribs(const Char*          _Name,
                          SHADER_TYPE          _ShaderStages,
                          SHADER_RESOURCE_TYPE _ResourceType,
                          Uint32               _ArraySize) noexcept :
            // clang-format off
            Name         {_Name        },
            ShaderStages {_ShaderStages},
            ResourceType {_ResourceType},
            ArraySize    {_ArraySize   }
        // clang-format on
        {
            VERIFY(_ShaderStages != SHADER_TYPE_UNKNOWN, "At least one shader stage must be specified");
            VERIFY(_ResourceType != SHADER_RESOURCE_TYPE_UNKNOWN, "Unknown shader resource type");
            VERIFY(_ArraySize >= 1, "Array size must be greater than 1");
        }

        GLResourceAttribs(const GLResourceAttribs& Attribs,
                          StringPool&              NamesPool) noexcept :
            // clang-format off
            GLResourceAttribs
            {
                NamesPool.CopyString(Attribs.Name),
                Attribs.ShaderStages,
                Attribs.ResourceType,
                Attribs.ArraySize
            }
            // clang-format on 
        {
        }

        ShaderResourceDesc GetResourceDesc() const
        {
            ShaderResourceDesc ResourceDesc;
            ResourceDesc.Name      = Name;
            ResourceDesc.ArraySize = ArraySize;
            ResourceDesc.Type      = ResourceType;
            return ResourceDesc;
        }
    };

    struct UniformBufferInfo final : GLResourceAttribs
    {
        // clang-format off
        UniformBufferInfo            (const UniformBufferInfo&)  = delete;
        UniformBufferInfo& operator= (const UniformBufferInfo&)  = delete;
        UniformBufferInfo            (      UniformBufferInfo&&) = default;
        UniformBufferInfo& operator= (      UniformBufferInfo&&) = delete;

        UniformBufferInfo(const Char*           _Name, 
                          SHADER_TYPE           _ShaderStages,
                          SHADER_RESOURCE_TYPE  _ResourceType,
                          Uint32                _ArraySize,
                          GLuint                _UBIndex)noexcept :
            GLResourceAttribs{_Name, _ShaderStages, _ResourceType, _ArraySize},
            UBIndex          {_UBIndex}
        {}

        UniformBufferInfo(const UniformBufferInfo& UB,
                          StringPool&              NamesPool)noexcept :
            GLResourceAttribs{UB, NamesPool},
            UBIndex          {UB.UBIndex   }
        {}
        // clang-format on

        const GLuint UBIndex;
    };
    static_assert((sizeof(UniformBufferInfo) % sizeof(void*)) == 0, "sizeof(UniformBufferInfo) must be multiple of sizeof(void*)");


    struct TextureInfo final : GLResourceAttribs
    {
        // clang-format off
        TextureInfo            (const TextureInfo&)  = delete;
        TextureInfo& operator= (const TextureInfo&)  = delete;
        TextureInfo            (      TextureInfo&&) = default;
        TextureInfo& operator= (      TextureInfo&&) = delete;

        TextureInfo(const Char*           _Name, 
                    SHADER_TYPE           _ShaderStages,
                    SHADER_RESOURCE_TYPE  _ResourceType,
                    Uint32                _ArraySize,
                    GLenum                _TextureType,
                    RESOURCE_DIMENSION    _ResourceDim,
                    bool                  _IsMultisample) noexcept :
            GLResourceAttribs{_Name, _ShaderStages, _ResourceType, _ArraySize},
            TextureType      {_TextureType},
            ResourceDim      {_ResourceDim},
            IsMultisample    {_IsMultisample}
        {}

        TextureInfo(const TextureInfo& Tex,
                    StringPool&        NamesPool) noexcept :
            GLResourceAttribs{Tex, NamesPool},
            TextureType      {Tex.TextureType},
            ResourceDim      {Tex.ResourceDim},
            IsMultisample    {Tex.IsMultisample}
        {}
        // clang-format on

        const GLenum             TextureType;
        const RESOURCE_DIMENSION ResourceDim;
        const bool               IsMultisample;
    };
    static_assert((sizeof(TextureInfo) % sizeof(void*)) == 0, "sizeof(TextureInfo) must be multiple of sizeof(void*)");


    struct ImageInfo final : GLResourceAttribs
    {
        // clang-format off
        ImageInfo            (const ImageInfo&)  = delete;
        ImageInfo& operator= (const ImageInfo&)  = delete;
        ImageInfo            (      ImageInfo&&) = default;
        ImageInfo& operator= (      ImageInfo&&) = delete;

        ImageInfo(const Char*           _Name, 
                  SHADER_TYPE           _ShaderStages,
                  SHADER_RESOURCE_TYPE  _ResourceType,
                  Uint32                _ArraySize,
                  GLenum                _ImageType,
                  RESOURCE_DIMENSION    _ResourceDim,
                  bool                  _IsMultisample) noexcept :
            GLResourceAttribs{_Name,  _ShaderStages, _ResourceType, _ArraySize},
            ImageType        {_ImageType},
            ResourceDim      {_ResourceDim},
            IsMultisample    {_IsMultisample}
        {}

        ImageInfo(const ImageInfo& Img, 
                  StringPool&      NamesPool) noexcept :
            GLResourceAttribs{Img, NamesPool},
            ImageType        {Img.ImageType},
            ResourceDim      {Img.ResourceDim},
            IsMultisample    {Img.IsMultisample}
        {}
        // clang-format on

        const GLenum             ImageType;
        const RESOURCE_DIMENSION ResourceDim;
        const bool               IsMultisample;
    };
    static_assert((sizeof(ImageInfo) % sizeof(void*)) == 0, "sizeof(ImageInfo) must be multiple of sizeof(void*)");


    struct StorageBlockInfo final : GLResourceAttribs
    {
        // clang-format off
        StorageBlockInfo            (const StorageBlockInfo&)  = delete;
        StorageBlockInfo& operator= (const StorageBlockInfo&)  = delete;
        StorageBlockInfo            (      StorageBlockInfo&&) = default;
        StorageBlockInfo& operator= (      StorageBlockInfo&&) = delete;

        StorageBlockInfo(const Char*            _Name, 
                         SHADER_TYPE            _ShaderStages,
                         SHADER_RESOURCE_TYPE   _ResourceType,
                         Uint32                 _ArraySize,
                         GLint                  _SBIndex)noexcept :
            GLResourceAttribs{_Name, _ShaderStages, _ResourceType, _ArraySize},
            SBIndex          {_SBIndex}
        {}

        StorageBlockInfo(const StorageBlockInfo& SB,
                         StringPool&             NamesPool)noexcept :
            GLResourceAttribs{SB, NamesPool},
            SBIndex          {SB.SBIndex}
        {}
        // clang-format on

        const GLint SBIndex;
    };
    static_assert((sizeof(StorageBlockInfo) % sizeof(void*)) == 0, "sizeof(StorageBlockInfo) must be multiple of sizeof(void*)");


    // clang-format off
    Uint32 GetNumUniformBuffers()const { return m_NumUniformBuffers; }
    Uint32 GetNumTextures()      const { return m_NumTextures;       }
    Uint32 GetNumImages()        const { return m_NumImages;         }
    Uint32 GetNumStorageBlocks() const { return m_NumStorageBlocks;  }
    // clang-format on

    UniformBufferInfo& GetUniformBuffer(Uint32 Index)
    {
        VERIFY(Index < m_NumUniformBuffers, "Uniform buffer index (", Index, ") is out of range");
        return m_UniformBuffers[Index];
    }

    TextureInfo& GetTexture(Uint32 Index)
    {
        VERIFY(Index < m_NumTextures, "Texture index (", Index, ") is out of range");
        return m_Textures[Index];
    }

    ImageInfo& GetImage(Uint32 Index)
    {
        VERIFY(Index < m_NumImages, "Image index (", Index, ") is out of range");
        return m_Images[Index];
    }

    StorageBlockInfo& GetStorageBlock(Uint32 Index)
    {
        VERIFY(Index < m_NumStorageBlocks, "Storage block index (", Index, ") is out of range");
        return m_StorageBlocks[Index];
    }


    const UniformBufferInfo& GetUniformBuffer(Uint32 Index) const
    {
        VERIFY(Index < m_NumUniformBuffers, "Uniform buffer index (", Index, ") is out of range");
        return m_UniformBuffers[Index];
    }

    const TextureInfo& GetTexture(Uint32 Index) const
    {
        VERIFY(Index < m_NumTextures, "Texture index (", Index, ") is out of range");
        return m_Textures[Index];
    }

    const ImageInfo& GetImage(Uint32 Index) const
    {
        VERIFY(Index < m_NumImages, "Image index (", Index, ") is out of range");
        return m_Images[Index];
    }

    const StorageBlockInfo& GetStorageBlock(Uint32 Index) const
    {
        VERIFY(Index < m_NumStorageBlocks, "Storage block index (", Index, ") is out of range");
        return m_StorageBlocks[Index];
    }

    Uint32 GetVariableCount() const
    {
        return m_NumUniformBuffers + m_NumTextures + m_NumImages + m_NumStorageBlocks;
    }

    ShaderResourceDesc GetResourceDesc(Uint32 Index) const;

    SHADER_TYPE GetShaderStages() const { return m_ShaderStages; }

    template <typename THandleUB,
              typename THandleTexture,
              typename THandleImg,
              typename THandleSB>
    void ProcessConstResources(THandleUB                            HandleUB,
                               THandleTexture                       HandleTexture,
                               THandleImg                           HandleImg,
                               THandleSB                            HandleSB,
                               const PipelineResourceLayoutDesc*    pResourceLayout = nullptr,
                               const SHADER_RESOURCE_VARIABLE_TYPE* AllowedVarTypes = nullptr,
                               Uint32                               NumAllowedTypes = 0) const
    {
        const Uint32 AllowedTypeBits = GetAllowedTypeBits(AllowedVarTypes, NumAllowedTypes);

        auto CheckResourceType = [&](const char* Name) //
        {
            if (pResourceLayout == nullptr)
                return true;
            else
            {
                auto VarType = GetShaderVariableType(m_ShaderStages, Name, *pResourceLayout);
                return IsAllowedType(VarType, AllowedTypeBits);
            }
        };

        for (Uint32 ub = 0; ub < m_NumUniformBuffers; ++ub)
        {
            const auto& UB = GetUniformBuffer(ub);
            if (CheckResourceType(UB.Name))
                HandleUB(UB);
        }

        for (Uint32 s = 0; s < m_NumTextures; ++s)
        {
            const auto& Sam = GetTexture(s);
            if (CheckResourceType(Sam.Name))
                HandleTexture(Sam);
        }

        for (Uint32 img = 0; img < m_NumImages; ++img)
        {
            const auto& Img = GetImage(img);
            if (CheckResourceType(Img.Name))
                HandleImg(Img);
        }

        for (Uint32 sb = 0; sb < m_NumStorageBlocks; ++sb)
        {
            const auto& SB = GetStorageBlock(sb);
            if (CheckResourceType(SB.Name))
                HandleSB(SB);
        }
    }

    template <typename THandleUB,
              typename THandleTexture,
              typename THandleImg,
              typename THandleSB>
    void ProcessResources(THandleUB      HandleUB,
                          THandleTexture HandleTexture,
                          THandleImg     HandleImg,
                          THandleSB      HandleSB)
    {
        for (Uint32 ub = 0; ub < m_NumUniformBuffers; ++ub)
            HandleUB(GetUniformBuffer(ub));

        for (Uint32 s = 0; s < m_NumTextures; ++s)
            HandleTexture(GetTexture(s));

        for (Uint32 img = 0; img < m_NumImages; ++img)
            HandleImg(GetImage(img));

        for (Uint32 sb = 0; sb < m_NumStorageBlocks; ++sb)
            HandleSB(GetStorageBlock(sb));
    }

private:
    void AllocateResources(std::vector<UniformBufferInfo>& UniformBlocks,
                           std::vector<TextureInfo>&       Textures,
                           std::vector<ImageInfo>&         Images,
                           std::vector<StorageBlockInfo>&  StorageBlocks);

    // clang-format off
    // There could be more than one stage if using non-separable programs
    SHADER_TYPE         m_ShaderStages = SHADER_TYPE_UNKNOWN; 

    // Memory layout:
    // 
    //  |  Uniform buffers  |   Textures  |   Images   |   Storage Blocks   |    String Pool Data   |
    //

    UniformBufferInfo*  m_UniformBuffers = nullptr;
    TextureInfo*        m_Textures       = nullptr;
    ImageInfo*          m_Images         = nullptr;
    StorageBlockInfo*   m_StorageBlocks  = nullptr;

    Uint32              m_NumUniformBuffers = 0;
    Uint32              m_NumTextures       = 0;
    Uint32              m_NumImages         = 0;
    Uint32              m_NumStorageBlocks  = 0;
    // clang-format on
    // When adding new member DO NOT FORGET TO UPDATE ShaderResourcesGL( ShaderResourcesGL&& ProgramResources )!!!
};

} // namespace Diligent