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
31SCORE_PLUGIN_GFX_EXPORT
32QByteArray hlslIntrinsicCollision(const QByteArray& src) noexcept;
33
37struct SCORE_PLUGIN_GFX_EXPORT ShaderCache
38{
39public:
45 static const std::pair<QShader, QString>& get(
46 const RenderState& v, const QByteArray& shader, QShader::Stage stage,
47 int multiViewCount = 0);
48 static const std::pair<QShader, QString>& get(
49 GraphicsApi api, const QShaderVersion& v, const QByteArray& shader,
50 QShader::Stage stage, int multiViewCount = 0);
51
52private:
54
55 struct Baker
56 {
57 explicit Baker(GraphicsApi api, const QShaderVersion& v, int multiViewCount);
58
59 GraphicsApi api;
60 QShaderVersion version;
61 int multiViewCount{};
62 QShaderBaker baker;
63
64 // Keyed by SOURCE, per STAGE. The stage has to be part of the key: the same
65 // source text baked as a vertex shader and as a fragment shader would
66 // otherwise share one entry, handing back whichever stage was baked first
67 // -- with the wrong stage's SPIR-V inside. The baker itself is already per
68 // (api, version, multiViewCount).
69 //
70 // An array rather than a composite key: QShader::Stage is a small dense
71 // enum, so this costs one indirection and, unlike appending the stage to
72 // the QByteArray, does not copy the whole shader source on every lookup.
73 static constexpr int stageCount = 6; // Vertex..Compute, QShader::Stage
74 std::array<ossia::hash_map<QByteArray, std::pair<QShader, QString>>, stageCount>
75 shaders;
76
77 auto& forStage(QShader::Stage s) noexcept
78 {
79 const int i = int(s);
80 SCORE_ASSERT(i >= 0 && i < stageCount);
81 return shaders[i];
82 }
83 };
84
85 std::vector<std::unique_ptr<Baker>> m_bakers;
86};
87}
Graphics rendering pipeline for ossia score.
Definition Filter/PreviewWidget.hpp:11
GraphicsApi
Available graphics APIs to use.
Definition RenderState.hpp:22
QByteArray hlslIntrinsicCollision(const QByteArray &raw) noexcept
The identifier a shader declares that collides with an HLSL intrinsic.
Definition ShaderCache.cpp:250
Global state associated to a rendering context.
Definition RenderState.hpp:37
Cache of baked QShader instances.
Definition ShaderCache.hpp:38