Loading...
Searching...
No Matches
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 <span>
7
8#include <private/qrhi_p.h>
9
10#include <score_plugin_gfx_export.h>
11
12namespace score::gfx
13{
15{
16 QRhiBuffer* handle{};
17 int64_t byte_offset{};
18 int64_t byte_size{};
19
20#if QT_VERSION >= QT_VERSION_CHECK(6, 12, 0)
21 enum class Usage : uint8_t
22 {
23 Direct,
24 IndirectDraw,
25 IndirectDrawIndexed
26 };
27 Usage usage{Usage::Direct};
28#endif
29
30 inline operator bool() const noexcept { return handle; }
31};
33{
34 ossia::small_vector<BufferView, 2> buffers;
35
36#if QT_VERSION >= QT_VERSION_CHECK(6, 12, 0)
37 QRhiBuffer* indirectDrawBuffer{};
38 bool useIndirectDraw{false};
39 bool indirectDrawIndexed{false};
40#endif
41};
45struct SCORE_PLUGIN_GFX_EXPORT Mesh
46{
47public:
48 explicit Mesh();
49 virtual ~Mesh();
50
51 enum Flag
52 {
53 HasPosition = SCORE_FLAG(1),
54 HasTexCoord = SCORE_FLAG(2),
55 HasColor = SCORE_FLAG(3),
56 HasNormals = SCORE_FLAG(4),
57 HasTangents = SCORE_FLAG(5),
58 };
59 using Flags = QFlags<Flag>;
60
61 [[nodiscard]] virtual Flags flags() const noexcept = 0;
62
63 [[nodiscard]] virtual MeshBuffers init(QRhi& rhi) const noexcept = 0;
64
65 virtual void
66 update(QRhi& rhi, MeshBuffers& bufs, QRhiResourceUpdateBatch& cb) const noexcept
67 = 0;
68 virtual void preparePipeline(QRhiGraphicsPipeline& pip) const noexcept = 0;
69 virtual void draw(const MeshBuffers& bufs, QRhiCommandBuffer& cb) const noexcept = 0;
70
72 virtual const char* defaultVertexShader() const noexcept = 0;
73
80 virtual const ossia::geometry* semanticGeometry() const noexcept { return nullptr; }
81
82 ossia::geometry_filter_list_ptr filters;
83
84 std::atomic_int64_t dirtyGeometryIndex{-1};
85
86 bool hasGeometryChanged(int64_t& renderer) const noexcept
87 {
88 int64_t res = dirtyGeometryIndex.load(std::memory_order_acquire);
89 if(renderer != res)
90 {
91 renderer = res;
92 return true;
93 }
94 return false;
95 }
96
97protected:
98 /*
99*/
100private:
101 Mesh(const Mesh&) = delete;
102 Mesh(Mesh&&) = delete;
103 Mesh& operator=(const Mesh&) = delete;
104 Mesh& operator=(Mesh&&) = delete;
105};
106
107Q_DECLARE_OPERATORS_FOR_FLAGS(Mesh::Flags);
108
109struct SCORE_PLUGIN_GFX_EXPORT BasicMesh : Mesh
110{
111 using Mesh::Mesh;
112 [[nodiscard]] virtual MeshBuffers init(QRhi& rhi) const noexcept override;
113 void update(
114 QRhi& rhi, MeshBuffers& bufs, QRhiResourceUpdateBatch& cb) const noexcept override;
115 void preparePipeline(QRhiGraphicsPipeline& pip) const noexcept override;
116 void draw(const MeshBuffers& bufs, QRhiCommandBuffer& cb) const noexcept override;
117 virtual void
118 setupBindings(const MeshBuffers& bufs, QRhiCommandBuffer& cb) const noexcept = 0;
119
120 using pip = QRhiGraphicsPipeline;
121 pip::Topology topology = pip::Topology::TriangleStrip;
122 pip::CullMode cullMode = pip::CullMode::None;
123 pip::FrontFace frontFace = pip::FrontFace::CW;
124
125 ossia::small_vector<QRhiVertexInputBinding, 2> vertexBindings;
126 ossia::small_vector<QRhiVertexInputAttribute, 2> vertexAttributes;
127
128 std::span<const float> vertexArray;
129 int vertexCount{};
130};
131/*
132* @brief A dummy mesh for only accessing a gl_VertexId without caring about attributes
133*/
134struct SCORE_PLUGIN_GFX_EXPORT DummyMesh : BasicMesh
135{
136 explicit DummyMesh(int count);
137 [[nodiscard]] Flags flags() const noexcept override { return Flags{}; }
138 const char* defaultVertexShader() const noexcept override;
139 void
140 setupBindings(const MeshBuffers& bufs, QRhiCommandBuffer& cb) const noexcept override;
141 void draw(const MeshBuffers& bufs, QRhiCommandBuffer& cb) const noexcept override;
142};
143
147struct SCORE_PLUGIN_GFX_EXPORT PlainMesh : BasicMesh
148{
149 explicit PlainMesh(std::span<const float> vtx, int count);
150 [[nodiscard]] Flags flags() const noexcept override { return HasPosition; }
151 const char* defaultVertexShader() const noexcept override;
152 void
153 setupBindings(const MeshBuffers& bufs, QRhiCommandBuffer& cb) const noexcept override;
154};
155
159struct SCORE_PLUGIN_GFX_EXPORT TexturedMesh : BasicMesh
160{
161 explicit TexturedMesh(std::span<const float> vtx, int count);
162 [[nodiscard]] Flags flags() const noexcept override
163 {
164 return HasPosition | HasTexCoord;
165 }
166
167 const char* defaultVertexShader() const noexcept override;
168};
169
173struct SCORE_PLUGIN_GFX_EXPORT PlainTriangle final : PlainMesh
174{
175 static const constexpr float data[] = {-1, -1, 3, -1, -1, 3};
176
177 explicit PlainTriangle();
178 static const PlainTriangle& instance() noexcept;
179};
180
186struct SCORE_PLUGIN_GFX_EXPORT TexturedTriangle final : TexturedMesh
187{
188 static const constexpr float data[] = {// positions
189 -1, -1, 3, -1, -1, 3,
190 // tex coords
191 0, 0, 2, 0, 0, 2};
192 static const constexpr float flipped_y_data[] = {// positions
193 -1, -1, 3, -1, -1, 3,
194 // tex coords
195 0, 2, 2, 2, 0, 0};
196
197 explicit TexturedTriangle(bool flipped = false);
198
199 void
200 setupBindings(const MeshBuffers& bufs, QRhiCommandBuffer& cb) const noexcept override;
201};
202
207struct SCORE_PLUGIN_GFX_EXPORT TexturedQuad final : TexturedMesh
208{
209 static const constexpr float data[] = {// positions
210 -1, -1, +1, -1, -1, +1, +1, +1,
211 // tex coords
212 0, 0, 1, 0, 0, 1, 1, 1};
213
214 static const constexpr float flipped_y_data[] = {// positions
215 -1, -1, +1, -1, -1, +1, +1, +1,
216 // tex coords
217 0, 1, 1, 1, 0, 0, 1, 0};
218
219 explicit TexturedQuad(bool flipped = false);
220
221 void
222 setupBindings(const MeshBuffers& bufs, QRhiCommandBuffer& cb) const noexcept override;
223};
224
225}
Graphics rendering pipeline for ossia score.
Definition Filter/PreviewWidget.hpp:12
Definition Mesh.hpp:110
Definition Mesh.hpp:15
Definition Mesh.hpp:135
Definition Mesh.hpp:33
Data model for meshes.
Definition Mesh.hpp:46
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:148
A triangle mesh with only positions.
Definition Mesh.hpp:174
A mesh with positions and texture coordinates.
Definition Mesh.hpp:160
A quad mesh with positions and texture coordinates.
Definition Mesh.hpp:208
A triangle mesh with positions and texture coordinates.
Definition Mesh.hpp:187
Definition Uniforms.hpp:8