Loading...
Searching...
No Matches
ShaderProgram.hpp
1#pragma once
2#include <Gfx/Graph/RenderState.hpp>
3
4#include <score/tools/Debug.hpp>
5
6#include <ossia/detail/hash_map.hpp>
7
8#include <QDebug>
9#include <QObject>
10#include <QString>
11#include <QtGui/private/qshader_p.h>
12
13#include <isf.hpp>
14#include <score_plugin_gfx_export.h>
15
16#include <array>
17#include <optional>
18#include <verdigris>
19namespace isf
20{
21struct descriptor;
22}
23
24namespace Gfx
25{
26struct SCORE_PLUGIN_GFX_EXPORT ShaderSource
27{
28 using ProgramType = isf::parser::ShaderType;
29 ShaderSource() = default;
30 ~ShaderSource() = default;
31 ShaderSource(const ShaderSource&) = default;
32 ShaderSource(ShaderSource&&) = default;
33
34 ShaderSource(const QString& vert, const QString& frag)
35 : type{isf::parser::ShaderType::ISF}
36 , vertex{vert}
37 , fragment{frag}
38 {
39 }
40
41 ShaderSource(const std::vector<QString>& vec)
42 {
43 SCORE_ASSERT(vec.size() == 2);
44 type = isf::parser::ShaderType::ISF;
45 fragment = vec[0];
46 vertex = vec[1];
47 }
48 ShaderSource(ProgramType tp, const QString& vert, const QString& frag)
49 : type{tp}
50 , vertex{vert}
51 , fragment{frag}
52 {
53 }
54
55 ShaderSource(ProgramType tp, const std::vector<QString>& vec)
56 {
57 SCORE_ASSERT(vec.size() == 2);
58 type = tp;
59 fragment = vec[0];
60 vertex = vec[1];
61 }
62
63 ShaderSource(ProgramType tp, std::vector<QString>&& vec)
64 {
65 SCORE_ASSERT(vec.size() == 2);
66 type = tp;
67 fragment = std::move(vec[0]);
68 vertex = std::move(vec[1]);
69 }
70
71 ShaderSource& operator=(const ShaderSource&) = default;
72 ShaderSource& operator=(ShaderSource&&) = default;
73
74 ProgramType type{};
75 QString vertex;
76 QString fragment;
77
79 {
80 const QString name;
81 const QString ShaderSource::*pointer;
82 const std::string_view language{};
83 };
84
85 static const inline std::array<MemberSpec, 2> specification{
86 MemberSpec{QObject::tr("Fragment"), &ShaderSource::fragment, "GLSL"},
87 MemberSpec{QObject::tr("Vertex"), &ShaderSource::vertex, "GLSL"},
88 };
89
90 friend QDebug& operator<<(QDebug& d, const ShaderSource& sp)
91 {
92 return (d << sp.vertex << sp.fragment);
93 }
94 friend bool operator==(const ShaderSource& lhs, const ShaderSource& rhs) noexcept
95 {
96 return lhs.vertex == rhs.vertex && lhs.fragment == rhs.fragment;
97 }
98 friend bool operator!=(const ShaderSource& lhs, const ShaderSource& rhs) noexcept
99 {
100 return !(lhs == rhs);
101 }
102
103 friend bool
104 operator==(const std::vector<QString>& lhs, const ShaderSource& rhs) noexcept
105 {
106 SCORE_ASSERT(lhs.size() == 2);
107 return lhs[0] == rhs.*(ShaderSource::specification[0].pointer)
108 && lhs[1] == rhs.*(ShaderSource::specification[1].pointer);
109 }
110 friend bool
111 operator!=(const std::vector<QString>& lhs, const ShaderSource& rhs) noexcept
112 {
113 return !(lhs == rhs);
114 }
115};
116
117SCORE_PLUGIN_GFX_EXPORT ShaderSource
118programFromISFFragmentShaderPath(const QString& fsFilename, QByteArray fsData);
119SCORE_PLUGIN_GFX_EXPORT ShaderSource
120programFromVSAVertexShaderPath(const QString& vertexFilename, QByteArray vertexData);
121}
122
123namespace std
124{
125template <>
126struct hash<Gfx::ShaderSource>
127{
128 std::size_t operator()(const Gfx::ShaderSource& program) const noexcept
129 {
130 constexpr const QtPrivate::QHashCombine combine{
131#if QT_VERSION >= QT_VERSION_CHECK(6, 10, 0)
132 0
133#endif
134 };
135 std::size_t seed{};
136 seed = combine(seed, program.vertex);
137 seed = combine(seed, program.fragment);
138 return seed;
139 }
140};
141}
142
143namespace Gfx
144{
146{
147 isf::descriptor descriptor;
148};
149
150struct SCORE_PLUGIN_GFX_EXPORT ProgramCache
151{
152 static ProgramCache& instance() noexcept;
153 std::pair<std::optional<ProcessedProgram>, QString>
154 get(const ShaderSource& program) noexcept;
155
156 ossia::hash_map<ShaderSource, ProcessedProgram> programs;
157};
158
159}
160
161Q_DECLARE_METATYPE(Gfx::ShaderSource)
162W_REGISTER_ARGTYPE(Gfx::ShaderSource)
163Q_DECLARE_METATYPE(Gfx::ProcessedProgram)
164W_REGISTER_ARGTYPE(Gfx::ProcessedProgram)
Binds the rendering pipeline to ossia processes.
Definition CameraDevice.cpp:31
STL namespace.
Definition ShaderProgram.hpp:146
Definition ShaderProgram.hpp:151
Definition ShaderProgram.hpp:79
Definition ShaderProgram.hpp:27