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
117ShaderSource programFromISFFragmentShaderPath(const QString& fsFilename, QByteArray fsData);
118ShaderSource
119programFromVSAVertexShaderPath(const QString& vertexFilename, QByteArray vertexData);
120}
121
122namespace std
123{
124template <>
125struct hash<Gfx::ShaderSource>
126{
127 std::size_t operator()(const Gfx::ShaderSource& program) const noexcept
128 {
129 constexpr const QtPrivate::QHashCombine combine{
130#if QT_VERSION >= QT_VERSION_CHECK(6, 11, 0)
131 0
132#endif
133 };
134 std::size_t seed{};
135 seed = combine(seed, program.vertex);
136 seed = combine(seed, program.fragment);
137 return seed;
138 }
139};
140}
141
142namespace Gfx
143{
145{
146 isf::descriptor descriptor;
147};
148
149struct SCORE_PLUGIN_GFX_EXPORT ProgramCache
150{
151 static ProgramCache& instance() noexcept;
152 std::pair<std::optional<ProcessedProgram>, QString>
153 get(const ShaderSource& program) noexcept;
154
155 ossia::hash_map<ShaderSource, ProcessedProgram> programs;
156};
157
158}
159
160Q_DECLARE_METATYPE(Gfx::ShaderSource)
161W_REGISTER_ARGTYPE(Gfx::ShaderSource)
162Q_DECLARE_METATYPE(Gfx::ProcessedProgram)
163W_REGISTER_ARGTYPE(Gfx::ProcessedProgram)
Binds the rendering pipeline to ossia processes.
Definition CameraDevice.cpp:24
STL namespace.
Definition ShaderProgram.hpp:145
Definition ShaderProgram.hpp:150
Definition ShaderProgram.hpp:79
Definition ShaderProgram.hpp:27