Loading...
Searching...
No Matches
ShaderCache.hpp
1#pragma once
2
3#include <array>
4#include <Gfx/Graph/RenderState.hpp>
5
6#include <score/gfx/OpenGL.hpp>
7#include <score/tools/Debug.hpp>
8#include <score/tools/std/StringHash.hpp>
9
10#include <ossia/detail/hash_map.hpp>
11
12#if __has_include(<QtShaderTools/rhi/qshaderbaker.h>)
13#include <QtShaderTools/rhi/qshaderbaker.h>
14#else
15#include <QtShaderTools/private/qshaderbaker_p.h>
16#endif
17
18namespace score::gfx
19{
20
41SCORE_PLUGIN_GFX_EXPORT
42QByteArray renameReservedIdentifiers(const QByteArray& src, GraphicsApi api);
43SCORE_PLUGIN_GFX_EXPORT
44QByteArray renameHlslReservedIdentifiers(const QByteArray& src);
45SCORE_PLUGIN_GFX_EXPORT
46QByteArray renameMslReservedIdentifiers(const QByteArray& src);
47SCORE_PLUGIN_GFX_EXPORT
48QByteArray renameGlslReservedIdentifiers(const QByteArray& src);
49
53struct SCORE_PLUGIN_GFX_EXPORT ShaderCache
54{
55public:
61 static const std::pair<QShader, QString>& get(
62 const RenderState& v, const QByteArray& shader, QShader::Stage stage,
63 int multiViewCount = 0);
64 static const std::pair<QShader, QString>& get(
65 GraphicsApi api, const QShaderVersion& v, const QByteArray& shader,
66 QShader::Stage stage, int multiViewCount = 0);
67
68private:
70
71 struct Baker
72 {
73 explicit Baker(GraphicsApi api, const QShaderVersion& v, int multiViewCount);
74
75 GraphicsApi api;
76 QShaderVersion version;
77 int multiViewCount{};
78 QShaderBaker baker;
79
80 // Keyed by SOURCE, per STAGE. The stage has to be part of the key: the same
81 // source text baked as a vertex shader and as a fragment shader would
82 // otherwise share one entry, handing back whichever stage was baked first
83 // -- with the wrong stage's SPIR-V inside. The baker itself is already per
84 // (api, version, multiViewCount).
85 //
86 // An array rather than a composite key: QShader::Stage is a small dense
87 // enum, so this costs one indirection and, unlike appending the stage to
88 // the QByteArray, does not copy the whole shader source on every lookup.
89 static constexpr int stageCount = 6; // Vertex..Compute, QShader::Stage
90 std::array<ossia::hash_map<QByteArray, std::pair<QShader, QString>>, stageCount>
91 shaders;
92
93 auto& forStage(QShader::Stage s) noexcept
94 {
95 const int i = int(s);
96 SCORE_ASSERT(i >= 0 && i < stageCount);
97 return shaders[i];
98 }
99 };
100
101 std::vector<std::unique_ptr<Baker>> m_bakers;
102};
103}
Graphics rendering pipeline for ossia score.
Definition Filter/PreviewWidget.hpp:11
GraphicsApi
Available graphics APIs to use.
Definition RenderState.hpp:25
QByteArray renameReservedIdentifiers(const QByteArray &src, GraphicsApi api)
Rename the identifiers of a GLSL source that the target language of a bake reserves,...
Definition ShaderCache.cpp:430
Global state associated to a rendering context.
Definition RenderState.hpp:40
Cache of baked QShader instances.
Definition ShaderCache.hpp:54