summaryrefslogtreecommitdiffstats
path: root/src/Runtime/ogl-runtime/src/render/Qt3DSRenderShaderConstant.h
blob: 7fb57ffeeff9f07fad425e6290ccf8281b00e588 (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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
/****************************************************************************
**
** Copyright (C) 2008-2012 NVIDIA Corporation.
** Copyright (C) 2017 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt 3D Studio.
**
** $QT_BEGIN_LICENSE:GPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 or (at your option) any later version
** approved by the KDE Free Qt Foundation. The licenses are as published by
** the Free Software Foundation and appearing in the file LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/
#pragma once
#ifndef QT3DS_RENDER_SHADER_CONSTANT_H
#define QT3DS_RENDER_SHADER_CONSTANT_H

#include "foundation/Qt3DSFoundation.h"
#include "foundation/Qt3DSAtomic.h"
#include "foundation/Qt3DSMat33.h"
#include "foundation/Qt3DSMat44.h"
#include "EASTL/string.h"
#include "EASTL/utility.h"
#include "render/Qt3DSRenderContext.h"

namespace qt3ds {
namespace render {
    using namespace foundation;

    ///< forward declarations
    class NVRenderContextImpl;
    class NVRenderConstantBuffer;

    ///< A shader constant belongs to a program
    class QT3DS_AUTOTEST_EXPORT NVRenderShaderConstantBase
    {
    public:
        NVRenderBackend *m_Backend; ///< pointer to backend
        CRegisteredString m_Name; ///< register constant name
        QT3DSI32 m_Location; ///< constant index
        QT3DSI32 m_ElementCount; ///< constant element count for arrays
        NVRenderShaderDataTypes::Enum m_Type; ///< constant type
        QT3DSI32 m_Binding; ///< sampler/imnage binding point

    public:
        NVRenderShaderConstantBase(NVRenderBackend *backend, CRegisteredString name, QT3DSI32 location,
                                   QT3DSI32 elementCount, NVRenderShaderDataTypes::Enum type,
                                   QT3DSI32 binding)
            : m_Backend(backend)
            , m_Name(name)
            , m_Location(location)
            , m_ElementCount(elementCount)
            , m_Type(type)
            , m_Binding(binding)
        {
        }

        NVRenderShaderDataTypes::Enum GetShaderConstantType() const { return m_Type; }

        virtual void Release() = 0;
    };

    ///< A general class for shader types
    template <typename TDataType>
    class NVRenderShaderConstant : public NVRenderShaderConstantBase
    {
    public:
        NVFoundationBase &m_Foundation; ///< allocator
        TDataType m_Value; ///< constant value

    public:
        NVRenderShaderConstant(NVRenderBackend *backend, CRegisteredString name, QT3DSI32 location,
                               QT3DSI32 elementCount, NVRenderShaderDataTypes::Enum type,
                               QT3DSI32 binding, NVFoundationBase &allocator)
            : NVRenderShaderConstantBase(backend, name, location, elementCount, type, binding)
            , m_Foundation(allocator)
        {
            memset(&m_Value, 0, sizeof(TDataType));
        }

        NVFoundationBase &GetFoundation() { return m_Foundation; }

        void Release() override { NVDelete(GetFoundation().getAllocator(), this); }
    };

    ///< A specialized class for textures
    template <>
    class NVRenderShaderConstant<NVRenderTexture2DPtr> : public NVRenderShaderConstantBase
    {
    public:
        NVFoundationBase &m_Foundation; ///< allocator
        QT3DSU32 m_Value; ///< constant value

    public:
        NVRenderShaderConstant(NVRenderBackend *backend, CRegisteredString name, QT3DSI32 location,
                               QT3DSI32 elementCount, NVRenderShaderDataTypes::Enum type,
                               QT3DSI32 binding, NVFoundationBase &allocator)
            : NVRenderShaderConstantBase(backend, name, location, elementCount, type, binding)
            , m_Foundation(allocator)
        {
            m_Value = QT3DS_MAX_U32;
        }

        NVFoundationBase &GetFoundation() { return m_Foundation; }

        void Release() override { NVDelete(GetFoundation().getAllocator(), this); }
    };

    ///< A specialized class for textures
    template <>
    class NVRenderShaderConstant<NVRenderTexture2DHandle> : public NVRenderShaderConstantBase
    {
    public:
        NVFoundationBase &m_Foundation; ///< allocator
        QVector<QT3DSU32> m_Value; ///< constant value

    public:
        NVRenderShaderConstant(NVRenderBackend *backend, CRegisteredString name, QT3DSI32 location,
                               QT3DSI32 elementCount, NVRenderShaderDataTypes::Enum type,
                               QT3DSI32 binding, NVFoundationBase &allocator)
            : NVRenderShaderConstantBase(backend, name, location, elementCount, type, binding)
            , m_Foundation(allocator)
        {
            m_Value.resize(elementCount);
            m_Value.fill(QT3DS_MAX_U32);
        }

        NVFoundationBase &GetFoundation() { return m_Foundation; }

        void Release() override { NVDelete(GetFoundation().getAllocator(), this); }
    };

    ///< A specialized class for texture arrays
    template <>
    class NVRenderShaderConstant<NVRenderTexture2DArrayPtr> : public NVRenderShaderConstantBase
    {
    public:
        NVFoundationBase &m_Foundation; ///< allocator
        QT3DSU32 m_Value; ///< constant value

    public:
        NVRenderShaderConstant(NVRenderBackend *backend, CRegisteredString name, QT3DSI32 location,
                               QT3DSI32 elementCount, NVRenderShaderDataTypes::Enum type,
                               QT3DSI32 binding, NVFoundationBase &allocator)
            : NVRenderShaderConstantBase(backend, name, location, elementCount, type, binding)
            , m_Foundation(allocator)
        {
            m_Value = QT3DS_MAX_U32;
        }

        NVFoundationBase &GetFoundation() { return m_Foundation; }

        void Release() override { NVDelete(GetFoundation().getAllocator(), this); }
    };

    ///< A specialized class for cubemap textures
    template <>
    class NVRenderShaderConstant<NVRenderTextureCubePtr> : public NVRenderShaderConstantBase
    {
    public:
        NVFoundationBase &m_Foundation; ///< allocator
        QT3DSU32 m_Value; ///< constant value

    public:
        NVRenderShaderConstant(NVRenderBackend *backend, CRegisteredString name, QT3DSI32 location,
                               QT3DSI32 elementCount, NVRenderShaderDataTypes::Enum type,
                               QT3DSI32 binding, NVFoundationBase &allocator)
            : NVRenderShaderConstantBase(backend, name, location, elementCount, type, binding)
            , m_Foundation(allocator)
        {
            m_Value = QT3DS_MAX_U32;
        }

        NVFoundationBase &GetFoundation() { return m_Foundation; }

        void Release() override { NVDelete(GetFoundation().getAllocator(), this); }
    };

    ///< A specialized class for cubemap textures
    template <>
    class NVRenderShaderConstant<NVRenderTextureCubeHandle> : public NVRenderShaderConstantBase
    {
    public:
        NVFoundationBase &m_Foundation; ///< allocator
        QVector<QT3DSU32> m_Value; ///< constant value

    public:
        NVRenderShaderConstant(NVRenderBackend *backend, CRegisteredString name, QT3DSI32 location,
                               QT3DSI32 elementCount, NVRenderShaderDataTypes::Enum type,
                               QT3DSI32 binding, NVFoundationBase &allocator)
            : NVRenderShaderConstantBase(backend, name, location, elementCount, type, binding)
            , m_Foundation(allocator)
        {
            m_Value.resize(elementCount);
            m_Value.fill(QT3DS_MAX_U32);
        }

        NVFoundationBase &GetFoundation() { return m_Foundation; }

        void Release() override { NVDelete(GetFoundation().getAllocator(), this); }
    };

    ///< A specialized class for texture image buffer
    template <>
    class NVRenderShaderConstant<NVRenderImage2DPtr> : public NVRenderShaderConstantBase
    {
    public:
        NVFoundationBase &m_Foundation; ///< allocator
        QT3DSU32 m_Value; ///< constant value

    public:
        NVRenderShaderConstant(NVRenderBackend *backend, CRegisteredString name, QT3DSI32 location,
                               QT3DSI32 elementCount, NVRenderShaderDataTypes::Enum type,
                               QT3DSI32 binding, NVFoundationBase &allocator)
            : NVRenderShaderConstantBase(backend, name, location, elementCount, type, binding)
            , m_Foundation(allocator)
        {
            m_Value = QT3DS_MAX_U32;
        }

        NVFoundationBase &GetFoundation() { return m_Foundation; }

        void Release() override { NVDelete(GetFoundation().getAllocator(), this); }
    };

    ///< Base for any buffer ( constant, texture, ... ) which is used by this program
    class NVRenderShaderBufferBase
    {
    public:
        NVRenderContextImpl &m_Context; ///< pointer to context
        CRegisteredString m_Name; ///< buffer name
        QT3DSU32 m_Location; ///< program buffer block location
        QT3DSU32 m_Binding; ///< program buffer binding
        QT3DSI32 m_Size; ///< buffer size

    public:
        NVRenderShaderBufferBase(NVRenderContextImpl &context, CRegisteredString name,
                                 QT3DSI32 location, QT3DSI32 binding, QT3DSI32 size)
            : m_Context(context)
            , m_Name(name)
            , m_Location(location)
            , m_Binding(binding)
            , m_Size(size)
        {
        }

        virtual void Release() = 0;

        virtual void Validate(NVRenderShaderProgram *inShader) = 0;
        virtual void BindToProgram(NVRenderShaderProgram *inShader) = 0;
        virtual void Update() = 0;
    };

    class NVRenderShaderConstantBuffer : public NVRenderShaderBufferBase
    {
    public:
        NVFoundationBase &m_Foundation; ///< allocator
        QT3DSI32 m_ParamCount; ///< count of parameters contained in the constant buffer
        NVRenderConstantBuffer *m_pCB; ///< pointer to constant buffer

    public:
        NVRenderShaderConstantBuffer(NVRenderContextImpl &context, CRegisteredString name,
                                     QT3DSU32 location, QT3DSI32 binding, QT3DSI32 size, QT3DSI32 count,
                                     NVRenderConstantBuffer *pCB, NVFoundationBase &allocator)
            : NVRenderShaderBufferBase(context, name, location, binding, size)
            , m_Foundation(allocator)
            , m_ParamCount(count)
            , m_pCB(pCB)
        {
        }

        NVFoundationBase &GetFoundation() { return m_Foundation; }

        void Release() override
        {
            if (m_pCB)
                m_pCB->release();

            NVDelete(GetFoundation().getAllocator(), this);
        }

        void Validate(NVRenderShaderProgram *inShader) override
        {
            // A constant buffer might not be set at first call
            // due to the fact that they are compiled from a cache file
            // Now it must exists.
            if (m_pCB)
                return;

            NVRenderConstantBuffer *cb = m_Context.GetConstantBuffer(m_Name);
            if (cb) {
                cb->SetupBuffer(inShader, m_Location, m_Size, m_ParamCount);
                cb->addRef();
                m_pCB = cb;
            } else {
                QT3DS_ASSERT(false);
            }
        }

        void Update() override
        {
            if (m_pCB)
                m_pCB->Update();
        }

        void BindToProgram(NVRenderShaderProgram *inShader) override
        {
            if (m_pCB)
                m_pCB->BindToShaderProgram(inShader, m_Location, m_Binding);
        }
    };

    class NVRenderShaderStorageBuffer : public NVRenderShaderBufferBase
    {
    public:
        NVFoundationBase &m_Foundation; ///< allocator
        QT3DSI32 m_ParamCount; ///< count of parameters contained in the constant buffer
        NVRenderStorageBuffer *m_pSB; ///< pointer to storage buffer

    public:
        NVRenderShaderStorageBuffer(NVRenderContextImpl &context, CRegisteredString name,
                                    QT3DSU32 location, QT3DSI32 binding, QT3DSI32 size, QT3DSI32 count,
                                    NVRenderStorageBuffer *pSB, NVFoundationBase &allocator)
            : NVRenderShaderBufferBase(context, name, location, binding, size)
            , m_Foundation(allocator)
            , m_ParamCount(count)
            , m_pSB(pSB)
        {
        }

        NVFoundationBase &GetFoundation() { return m_Foundation; }

        void Release() override
        {
            if (m_pSB)
                m_pSB->release();

            NVDelete(GetFoundation().getAllocator(), this);
        }

        void Validate(NVRenderShaderProgram * /*inShader*/) override
        {
            // A constant buffer might not be set at first call
            // due to the fact that they are compile from a cache file
            // Now it must exists.
            if (m_pSB)
                return;

            NVRenderStorageBuffer *sb = m_Context.GetStorageBuffer(m_Name);
            if (sb) {
                sb->addRef();
                m_pSB = sb;
            } else {
                QT3DS_ASSERT(false);
            }
        }

        void Update() override
        {
            if (m_pSB)
                m_pSB->Update();
        }

        void BindToProgram(NVRenderShaderProgram * /*inShader*/) override
        {
            if (m_pSB)
                m_pSB->BindToShaderProgram(m_Location);
        }
    };

    class NVRenderShaderAtomicCounterBuffer : public NVRenderShaderBufferBase
    {
    public:
        NVFoundationBase &m_Foundation; ///< allocator
        QT3DSI32 m_ParamCount; ///< count of parameters contained in the constant buffer
        NVRenderAtomicCounterBuffer *m_pAcB; ///< pointer to atomic counter buffer

    public:
        NVRenderShaderAtomicCounterBuffer(NVRenderContextImpl &context, CRegisteredString name,
                                          QT3DSU32 location, QT3DSI32 binding, QT3DSI32 size, QT3DSI32 count,
                                          NVRenderAtomicCounterBuffer *pAcB,
                                          NVFoundationBase &allocator)
            : NVRenderShaderBufferBase(context, name, location, binding, size)
            , m_Foundation(allocator)
            , m_ParamCount(count)
            , m_pAcB(pAcB)
        {
        }

        NVFoundationBase &GetFoundation() { return m_Foundation; }

        void Release() override
        {
            if (m_pAcB)
                m_pAcB->release();

            NVDelete(GetFoundation().getAllocator(), this);
        }

        void Validate(NVRenderShaderProgram * /*inShader*/) override
        {
            // A constant buffer might not be set at first call
            // due to the fact that they are compile from a cache file
            // Now it must exists.
            if (m_pAcB)
                return;

            NVRenderAtomicCounterBuffer *acb = m_Context.GetAtomicCounterBuffer(m_Name);
            if (acb) {
                acb->addRef();
                m_pAcB = acb;
            } else {
                QT3DS_ASSERT(false);
            }
        }

        void Update() override
        {
            if (m_pAcB)
                m_pAcB->Update();
        }

        void BindToProgram(NVRenderShaderProgram * /*inShader*/) override
        {
            if (m_pAcB)
                m_pAcB->BindToShaderProgram(m_Location);
        }
    };
}
}

#endif