summaryrefslogtreecommitdiffstats
path: root/src/Runtime/Source/Qt3DSRuntimeRender/Include/Qt3DSRenderShaderCache.h
blob: 47e76f36b0c967d000fcbec980dc6f484ac7a09f (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
/****************************************************************************
**
** 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_CACHE_H
#define QT3DS_RENDER_SHADER_CACHE_H
#include "Qt3DSRender.h"
#include "foundation/Qt3DSRefCounted.h"
#include "foundation/Qt3DSFlags.h"
#include "foundation/StringTable.h"

namespace qt3ds {
namespace render {
    struct ShaderCacheProgramFlagValues
    {
        enum Enum {
            TessellationEnabled = 1 << 0, // tessellation enabled
            GeometryShaderEnabled = 1 << 1, // geometry shader enabled
        };
    };
    struct SShaderCacheProgramFlags : public NVFlags<ShaderCacheProgramFlagValues::Enum, QT3DSU32>
    {
        // tessellation enabled
        void SetTessellationEnabled(bool inValue)
        {
            clearOrSet(inValue, ShaderCacheProgramFlagValues::TessellationEnabled);
        }
        bool IsTessellationEnabled() const
        {
            return this->operator&(ShaderCacheProgramFlagValues::TessellationEnabled);
        }
        // geometry shader enabled
        void SetGeometryShaderEnabled(bool inValue)
        {
            clearOrSet(inValue, ShaderCacheProgramFlagValues::GeometryShaderEnabled);
        }
        bool IsGeometryShaderEnabled() const
        {
            return this->operator&(ShaderCacheProgramFlagValues::GeometryShaderEnabled);
        }
    };
    // There are a number of macros used to turn on or off various features.  This allows those
    // features
    // to be propagated into the shader cache's caching mechanism.  They will be translated into
    //#define name value where value is 1 or zero depending on if the feature is enabled or not.
    struct SShaderPreprocessorFeature
    {
        CRegisteredString m_Name;
        bool m_Enabled;
        SShaderPreprocessorFeature()
            : m_Enabled(false)
        {
        }
        SShaderPreprocessorFeature(CRegisteredString name, bool val)
            : m_Name(name)
            , m_Enabled(val)
        {
        }
        bool operator<(const SShaderPreprocessorFeature &inOther) const;
        bool operator==(const SShaderPreprocessorFeature &inOther) const;
    };

    typedef NVConstDataRef<SShaderPreprocessorFeature> TShaderFeatureSet;

    inline TShaderFeatureSet ShaderCacheNoFeatures() { return TShaderFeatureSet(); }

    // Hash is dependent on the order of the keys; so make sure their order is consistent!!
    size_t HashShaderFeatureSet(NVConstDataRef<SShaderPreprocessorFeature> inFeatureSet);

    class IShaderCache : public NVRefCounted
    {
    protected:
        virtual ~IShaderCache() {}
    public:
        // If directory is nonnull, then we attempt to load any shaders from shadercache.xml in
        // inDirectory
        // and save any new ones out to the same file.  The shaders are marked by the gl version
        // used when saving.
        // If we can't open shadercache.xml from inDirectory for writing (at least), then we still
        // consider the
        // shadercache to be disabled.
        // This call immediately blocks and attempts to load all applicable shaders from the
        // shadercache.xml file in
        // the given directory.
        virtual void SetShaderCachePersistenceEnabled(const char8_t *inDirectory) = 0;
        virtual bool IsShaderCachePersistenceEnabled() const = 0;
        // It is up to the caller to ensure that inFeatures contains unique keys.
        // It is also up the the caller to ensure the keys are ordered in some way.
        virtual NVRenderShaderProgram *
        GetProgram(CRegisteredString inKey,
                   NVConstDataRef<SShaderPreprocessorFeature> inFeatures) = 0;

        // Replace an existing program in the cache for the same key with this program.
        // The shaders returned by *CompileProgram functions can be released by this object
        // due to ForceCompileProgram or SetProjectDirectory, so clients need to either not
        // hold on to them or they need to addref/release them to ensure they still have
        // access to them.
        // The flags just tell us under what gl state to compile the program in order to hopefully
        // reduce program compilations.
        // It is up to the caller to ensure that inFeatures contains unique keys.
        // It is also up the the caller to ensure the keys are ordered in some way.
        virtual NVRenderShaderProgram *
        ForceCompileProgram(CRegisteredString inKey, const char8_t *inVert, const char8_t *inFrag,
                            const char8_t *inTessCtrl, const char8_t *inTessEval,
                            const char8_t *inGeom, const SShaderCacheProgramFlags &inFlags,
                            TShaderFeatureSet inFeatures, bool separableProgram,
                            bool fromDisk = false) = 0;

        // It is up to the caller to ensure that inFeatures contains unique keys.
        // It is also up the the caller to ensure the keys are ordered in some way.
        virtual NVRenderShaderProgram *
        CompileProgram(CRegisteredString inKey, const char8_t *inVert, const char8_t *inFrag,
                       const char8_t *inTessCtrl, const char8_t *inTessEval, const char8_t *inGeom,
                       const SShaderCacheProgramFlags &inFlags, TShaderFeatureSet inFeatures,
                       bool separableProgram = false) = 0;

        // Used to disable any shader compilation during loading.  This is used when we are just
        // interested in going from uia->binary
        // and we expect to run on a headless server of sorts.  See the UICCompiler project for its
        // only current use case.
        virtual void SetShaderCompilationEnabled(bool inEnableShaderCompilation) = 0;

        // Upping the shader version invalidates all previous cache files.
        static QT3DSU32 GetShaderVersion() { return 4; }
        static const char8_t *GetShaderCacheFileName() { return "shadercache.xml"; }

        static IShaderCache &CreateShaderCache(NVRenderContext &inContext,
                                               IInputStreamFactory &inInputStreamFactory,
                                               IPerfTimer &inPerfTimer);
    };
}
}

#endif