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>
19 namespace isf
20 {
21 struct descriptor;
22 }
23 
24 namespace Gfx
25 {
26 struct SCORE_PLUGIN_GFX_EXPORT ShaderSource
27 {
28  ShaderSource() = default;
29  ~ShaderSource() = default;
30  ShaderSource(const ShaderSource&) = default;
31  ShaderSource(ShaderSource&&) = default;
32 
33  ShaderSource(const QString& vert, const QString& frag)
34  : vertex{vert}
35  , fragment{frag}
36  {
37  }
38 
39  ShaderSource(const std::vector<QString>& vec)
40  {
41  SCORE_ASSERT(vec.size() == 2);
42  fragment = vec[0];
43  vertex = vec[1];
44  }
45 
46  ShaderSource(std::vector<QString>&& vec)
47  {
48  SCORE_ASSERT(vec.size() == 2);
49  fragment = std::move(vec[0]);
50  vertex = std::move(vec[1]);
51  }
52 
53  ShaderSource& operator=(const ShaderSource&) = default;
54  ShaderSource& operator=(ShaderSource&&) = default;
55 
56  QString vertex;
57  QString fragment;
58 
59  struct MemberSpec
60  {
61  const QString name;
62  const QString ShaderSource::*pointer;
63  const std::string_view language{};
64  };
65 
66  static const inline std::array<MemberSpec, 2> specification{
67  MemberSpec{QObject::tr("Fragment"), &ShaderSource::fragment, "GLSL"},
68  MemberSpec{QObject::tr("Vertex"), &ShaderSource::vertex, "GLSL"},
69  };
70 
71  friend QDebug& operator<<(QDebug& d, const ShaderSource& sp)
72  {
73  return (d << sp.vertex << sp.fragment);
74  }
75  friend bool operator==(const ShaderSource& lhs, const ShaderSource& rhs) noexcept
76  {
77  return lhs.vertex == rhs.vertex && lhs.fragment == rhs.fragment;
78  }
79  friend bool operator!=(const ShaderSource& lhs, const ShaderSource& rhs) noexcept
80  {
81  return !(lhs == rhs);
82  }
83 
84  friend bool
85  operator==(const std::vector<QString>& lhs, const ShaderSource& rhs) noexcept
86  {
87  SCORE_ASSERT(lhs.size() == 2);
88  return lhs[0] == rhs.*(ShaderSource::specification[0].pointer)
89  && lhs[1] == rhs.*(ShaderSource::specification[1].pointer);
90  }
91  friend bool
92  operator!=(const std::vector<QString>& lhs, const ShaderSource& rhs) noexcept
93  {
94  return !(lhs == rhs);
95  }
96 };
97 
98 ShaderSource programFromFragmentShaderPath(const QString& fsFilename, QByteArray fsData);
99 }
100 
101 namespace std
102 {
103 template <>
104 struct hash<Gfx::ShaderSource>
105 {
106  std::size_t operator()(const Gfx::ShaderSource& program) const noexcept
107  {
108  constexpr const QtPrivate::QHashCombine combine;
109  std::size_t seed{};
110  seed = combine(seed, program.vertex);
111  seed = combine(seed, program.fragment);
112  return seed;
113  }
114 };
115 }
116 
117 namespace Gfx
118 {
120 {
121  isf::descriptor descriptor;
122 };
123 
124 struct SCORE_PLUGIN_GFX_EXPORT ProgramCache
125 {
126  static ProgramCache& instance() noexcept;
127  std::pair<std::optional<ProcessedProgram>, QString>
128  get(const score::gfx::GraphicsApi api, QShaderVersion version,
129  const ShaderSource& program) noexcept;
130 
131  ossia::hash_map<ShaderSource, ProcessedProgram> programs;
132 };
133 
134 }
135 
136 Q_DECLARE_METATYPE(Gfx::ShaderSource)
137 W_REGISTER_ARGTYPE(Gfx::ShaderSource)
138 Q_DECLARE_METATYPE(Gfx::ProcessedProgram)
139 W_REGISTER_ARGTYPE(Gfx::ProcessedProgram)
Binds the rendering pipeline to ossia processes.
Definition: CameraDevice.cpp:28
GraphicsApi
Available graphics APIs to use.
Definition: RenderState.hpp:17
Definition: ShaderProgram.hpp:120
Definition: ShaderProgram.hpp:125
Definition: ShaderProgram.hpp:60
Definition: ShaderProgram.hpp:27