Mesh.hpp
1 #pragma once
2 #include <Process/ProcessFlags.hpp>
3 
4 #include <ossia/detail/small_vector.hpp>
5 #include <ossia/detail/span.hpp>
6 
7 #include <private/qrhi_p.h>
8 
9 #include <score_plugin_gfx_export.h>
10 
11 namespace score::gfx
12 {
14 {
15  QRhiBuffer* mesh{};
16  QRhiBuffer* index{};
17 };
21 struct SCORE_PLUGIN_GFX_EXPORT Mesh
22 {
23 public:
24  explicit Mesh();
25  virtual ~Mesh();
26 
27  enum Flag
28  {
29  HasPosition = SCORE_FLAG(1),
30  HasTexCoord = SCORE_FLAG(2),
31  HasColor = SCORE_FLAG(3),
32  HasNormals = SCORE_FLAG(4),
33  HasTangents = SCORE_FLAG(5),
34  };
35  using Flags = QFlags<Flag>;
36 
37  [[nodiscard]] virtual Flags flags() const noexcept = 0;
38 
39  [[nodiscard]] virtual MeshBuffers init(QRhi& rhi) const noexcept = 0;
40 
41  virtual void update(MeshBuffers& bufs, QRhiResourceUpdateBatch& cb) const noexcept = 0;
42  virtual void preparePipeline(QRhiGraphicsPipeline& pip) const noexcept = 0;
43  virtual void draw(const MeshBuffers& bufs, QRhiCommandBuffer& cb) const noexcept = 0;
44 
46  virtual const char* defaultVertexShader() const noexcept = 0;
47 
48 protected:
49  /*
50 */
51 private:
52  Mesh(const Mesh&) = delete;
53  Mesh(Mesh&&) = delete;
54  Mesh& operator=(const Mesh&) = delete;
55  Mesh& operator=(Mesh&&) = delete;
56 };
57 
58 Q_DECLARE_OPERATORS_FOR_FLAGS(Mesh::Flags);
59 
60 struct SCORE_PLUGIN_GFX_EXPORT BasicMesh : Mesh
61 {
62  using Mesh::Mesh;
63  [[nodiscard]] virtual MeshBuffers init(QRhi& rhi) const noexcept override;
64  void update(MeshBuffers& bufs, QRhiResourceUpdateBatch& cb) const noexcept override;
65  void preparePipeline(QRhiGraphicsPipeline& pip) const noexcept override;
66  void draw(const MeshBuffers& bufs, QRhiCommandBuffer& cb) const noexcept override;
67  virtual void
68  setupBindings(const MeshBuffers& bufs, QRhiCommandBuffer& cb) const noexcept = 0;
69 
70  using pip = QRhiGraphicsPipeline;
71  pip::Topology topology = pip::Topology::TriangleStrip;
72  pip::CullMode cullMode = pip::CullMode::None;
73  pip::FrontFace frontFace = pip::FrontFace::CW;
74 
75  ossia::small_vector<QRhiVertexInputBinding, 2> vertexBindings;
76  ossia::small_vector<QRhiVertexInputAttribute, 2> vertexAttributes;
77 
78  tcb::span<const float> vertexArray;
79  int vertexCount{};
80 };
81 
85 struct SCORE_PLUGIN_GFX_EXPORT PlainMesh : BasicMesh
86 {
87  explicit PlainMesh(tcb::span<const float> vtx, int count);
88  [[nodiscard]] Flags flags() const noexcept override { return HasPosition; }
89  const char* defaultVertexShader() const noexcept override;
90  void
91  setupBindings(const MeshBuffers& bufs, QRhiCommandBuffer& cb) const noexcept override;
92 };
93 
97 struct SCORE_PLUGIN_GFX_EXPORT TexturedMesh : BasicMesh
98 {
99  explicit TexturedMesh(tcb::span<const float> vtx, int count);
100  [[nodiscard]] Flags flags() const noexcept override
101  {
102  return HasPosition | HasTexCoord;
103  }
104 
105  const char* defaultVertexShader() const noexcept override;
106 };
107 
111 struct SCORE_PLUGIN_GFX_EXPORT PlainTriangle final : PlainMesh
112 {
113  static const constexpr float data[] = {-1, -1, 3, -1, -1, 3};
114 
115  explicit PlainTriangle();
116  static const PlainTriangle& instance() noexcept;
117 };
118 
124 struct SCORE_PLUGIN_GFX_EXPORT TexturedTriangle final : TexturedMesh
125 {
126  static const constexpr float data[] = {// positions
127  -1, -1, 3, -1, -1, 3,
128  // tex coords
129  0, 0, 2, 0, 0, 2};
130  static const constexpr float flipped_y_data[] = {// positions
131  -1, -1, 3, -1, -1, 3,
132  // tex coords
133  0, 2, 2, 2, 0, 0};
134 
135  explicit TexturedTriangle(bool flipped = false);
136 
137  void
138  setupBindings(const MeshBuffers& bufs, QRhiCommandBuffer& cb) const noexcept override;
139 };
140 
145 struct SCORE_PLUGIN_GFX_EXPORT TexturedQuad final : TexturedMesh
146 {
147  static const constexpr float data[] = {// positions
148  -1, -1, +1, -1, -1, +1, +1, +1,
149  // tex coords
150  0, 0, 1, 0, 0, 1, 1, 1};
151 
152  static const constexpr float flipped_y_data[] = {// positions
153  -1, -1, +1, -1, -1, +1, +1, +1,
154  // tex coords
155  0, 1, 1, 1, 0, 0, 1, 0};
156 
157  explicit TexturedQuad(bool flipped = false);
158 
159  void
160  setupBindings(const MeshBuffers& bufs, QRhiCommandBuffer& cb) const noexcept override;
161 };
162 
163 }
Graphics rendering pipeline for ossia score.
Definition: PreviewWidget.hpp:12
Definition: Mesh.hpp:61
Definition: Mesh.hpp:14
Data model for meshes.
Definition: Mesh.hpp:22
virtual const char * defaultVertexShader() const noexcept=0
A basic vertex shader that is going to work with this mesh.
A mesh with only position attributes.
Definition: Mesh.hpp:86
A triangle mesh with only positions.
Definition: Mesh.hpp:112
A mesh with positions and texture coordinates.
Definition: Mesh.hpp:98
A quad mesh with positions and texture coordinates.
Definition: Mesh.hpp:146
A triangle mesh with positions and texture coordinates.
Definition: Mesh.hpp:125