Mesh.hpp
1 #pragma once
2 #include <Process/ProcessFlags.hpp>
3 
4 #include <ossia/dataflow/geometry_port.hpp>
5 #include <ossia/detail/small_vector.hpp>
6 #include <ossia/detail/span.hpp>
7 
8 #include <private/qrhi_p.h>
9 
10 #include <score_plugin_gfx_export.h>
11 
12 namespace score::gfx
13 {
15 {
16  QRhiBuffer* mesh{};
17  QRhiBuffer* index{};
18 };
22 struct SCORE_PLUGIN_GFX_EXPORT Mesh
23 {
24 public:
25  explicit Mesh();
26  virtual ~Mesh();
27 
28  enum Flag
29  {
30  HasPosition = SCORE_FLAG(1),
31  HasTexCoord = SCORE_FLAG(2),
32  HasColor = SCORE_FLAG(3),
33  HasNormals = SCORE_FLAG(4),
34  HasTangents = SCORE_FLAG(5),
35  };
36  using Flags = QFlags<Flag>;
37 
38  [[nodiscard]] virtual Flags flags() const noexcept = 0;
39 
40  [[nodiscard]] virtual MeshBuffers init(QRhi& rhi) const noexcept = 0;
41 
42  virtual void update(MeshBuffers& bufs, QRhiResourceUpdateBatch& cb) const noexcept = 0;
43  virtual void preparePipeline(QRhiGraphicsPipeline& pip) const noexcept = 0;
44  virtual void draw(const MeshBuffers& bufs, QRhiCommandBuffer& cb) const noexcept = 0;
45 
47  virtual const char* defaultVertexShader() const noexcept = 0;
48 
49  ossia::geometry_filter_list_ptr filters;
50 
51  std::atomic_int64_t dirtyGeometryIndex{-1};
52 
53  bool hasGeometryChanged(int64_t& renderer) const noexcept
54  {
55  int64_t res = dirtyGeometryIndex.load(std::memory_order_acquire);
56  if(renderer != res)
57  {
58  renderer = res;
59  return true;
60  }
61  return false;
62  }
63 
64 protected:
65  /*
66 */
67 private:
68  Mesh(const Mesh&) = delete;
69  Mesh(Mesh&&) = delete;
70  Mesh& operator=(const Mesh&) = delete;
71  Mesh& operator=(Mesh&&) = delete;
72 };
73 
74 Q_DECLARE_OPERATORS_FOR_FLAGS(Mesh::Flags);
75 
76 struct SCORE_PLUGIN_GFX_EXPORT BasicMesh : Mesh
77 {
78  using Mesh::Mesh;
79  [[nodiscard]] virtual MeshBuffers init(QRhi& rhi) const noexcept override;
80  void update(MeshBuffers& bufs, QRhiResourceUpdateBatch& cb) const noexcept override;
81  void preparePipeline(QRhiGraphicsPipeline& pip) const noexcept override;
82  void draw(const MeshBuffers& bufs, QRhiCommandBuffer& cb) const noexcept override;
83  virtual void
84  setupBindings(const MeshBuffers& bufs, QRhiCommandBuffer& cb) const noexcept = 0;
85 
86  using pip = QRhiGraphicsPipeline;
87  pip::Topology topology = pip::Topology::TriangleStrip;
88  pip::CullMode cullMode = pip::CullMode::None;
89  pip::FrontFace frontFace = pip::FrontFace::CW;
90 
91  ossia::small_vector<QRhiVertexInputBinding, 2> vertexBindings;
92  ossia::small_vector<QRhiVertexInputAttribute, 2> vertexAttributes;
93 
94  tcb::span<const float> vertexArray;
95  int vertexCount{};
96 };
97 
101 struct SCORE_PLUGIN_GFX_EXPORT PlainMesh : BasicMesh
102 {
103  explicit PlainMesh(tcb::span<const float> vtx, int count);
104  [[nodiscard]] Flags flags() const noexcept override { return HasPosition; }
105  const char* defaultVertexShader() const noexcept override;
106  void
107  setupBindings(const MeshBuffers& bufs, QRhiCommandBuffer& cb) const noexcept override;
108 };
109 
113 struct SCORE_PLUGIN_GFX_EXPORT TexturedMesh : BasicMesh
114 {
115  explicit TexturedMesh(tcb::span<const float> vtx, int count);
116  [[nodiscard]] Flags flags() const noexcept override
117  {
118  return HasPosition | HasTexCoord;
119  }
120 
121  const char* defaultVertexShader() const noexcept override;
122 };
123 
127 struct SCORE_PLUGIN_GFX_EXPORT PlainTriangle final : PlainMesh
128 {
129  static const constexpr float data[] = {-1, -1, 3, -1, -1, 3};
130 
131  explicit PlainTriangle();
132  static const PlainTriangle& instance() noexcept;
133 };
134 
140 struct SCORE_PLUGIN_GFX_EXPORT TexturedTriangle final : TexturedMesh
141 {
142  static const constexpr float data[] = {// positions
143  -1, -1, 3, -1, -1, 3,
144  // tex coords
145  0, 0, 2, 0, 0, 2};
146  static const constexpr float flipped_y_data[] = {// positions
147  -1, -1, 3, -1, -1, 3,
148  // tex coords
149  0, 2, 2, 2, 0, 0};
150 
151  explicit TexturedTriangle(bool flipped = false);
152 
153  void
154  setupBindings(const MeshBuffers& bufs, QRhiCommandBuffer& cb) const noexcept override;
155 };
156 
161 struct SCORE_PLUGIN_GFX_EXPORT TexturedQuad final : TexturedMesh
162 {
163  static const constexpr float data[] = {// positions
164  -1, -1, +1, -1, -1, +1, +1, +1,
165  // tex coords
166  0, 0, 1, 0, 0, 1, 1, 1};
167 
168  static const constexpr float flipped_y_data[] = {// positions
169  -1, -1, +1, -1, -1, +1, +1, +1,
170  // tex coords
171  0, 1, 1, 1, 0, 0, 1, 0};
172 
173  explicit TexturedQuad(bool flipped = false);
174 
175  void
176  setupBindings(const MeshBuffers& bufs, QRhiCommandBuffer& cb) const noexcept override;
177 };
178 
179 }
Graphics rendering pipeline for ossia score.
Definition: Filter/PreviewWidget.hpp:12
Definition: Mesh.hpp:77
Definition: Mesh.hpp:15
Data model for meshes.
Definition: Mesh.hpp:23
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:102
A triangle mesh with only positions.
Definition: Mesh.hpp:128
A mesh with positions and texture coordinates.
Definition: Mesh.hpp:114
A quad mesh with positions and texture coordinates.
Definition: Mesh.hpp:162
A triangle mesh with positions and texture coordinates.
Definition: Mesh.hpp:141