2 #include <Gfx/Graph/Mesh.hpp>
4 #include <ossia/dataflow/geometry_port.hpp>
6 #include <QtGui/private/qrhi_p.h>
13 ossia::mesh_list geom;
15 using pip = QRhiGraphicsPipeline;
16 pip::Topology topology = pip::Topology::TriangleStrip;
17 pip::CullMode cullMode = pip::CullMode::None;
18 pip::FrontFace frontFace = pip::FrontFace::CW;
20 ossia::small_vector<QRhiVertexInputBinding, 2> vertexBindings;
21 ossia::small_vector<QRhiVertexInputAttribute, 2> vertexAttributes;
25 const ossia::mesh_list& g,
const ossia::geometry_filter_list_ptr& f)
30 [[nodiscard]]
MeshBuffers init(QRhi& rhi)
const noexcept
override
32 if(geom.meshes.empty())
34 if(geom.meshes[0].buffers.empty())
37 const auto vtx_buf_size = geom.meshes[0].buffers[0].size;
39 = rhi.newBuffer(QRhiBuffer::Dynamic, QRhiBuffer::VertexBuffer, vtx_buf_size);
40 mesh_buf->setName(
"Mesh::mesh_buf");
43 QRhiBuffer* idx_buf{};
44 if(geom.meshes[0].buffers.size() > 1)
46 if(
const auto idx_buf_size = geom.meshes[0].buffers[1].size; idx_buf_size > 0)
49 = rhi.newBuffer(QRhiBuffer::Dynamic, QRhiBuffer::IndexBuffer, idx_buf_size);
50 idx_buf->setName(
"Mesh::idx_buf");
59 void update(
MeshBuffers& meshbuf, QRhiResourceUpdateBatch& rb)
const noexcept
override
61 if(geom.meshes.empty())
63 if(geom.meshes[0].buffers.empty())
66 void* idx_buf_data =
nullptr;
67 const auto vtx_buf = geom.meshes[0].buffers[0];
68 if(
auto sz = vtx_buf.size; sz != meshbuf.mesh->size())
70 meshbuf.mesh->destroy();
71 meshbuf.mesh->setSize(sz);
72 meshbuf.mesh->create();
77 const auto idx_buf = geom.meshes[0].buffers[1];
78 if(geom.meshes[0].buffers.size() > 1)
80 if(
const auto idx_buf_size = idx_buf.size; idx_buf_size > 0)
82 idx_buf_data = idx_buf.data.get();
84 if(
auto sz = idx_buf.size; sz != meshbuf.index->size())
86 meshbuf.index->destroy();
87 meshbuf.index->setSize(sz);
88 meshbuf.index->create();
101 rb.updateDynamicBuffer(meshbuf.mesh, 0, meshbuf.mesh->size(), vtx_buf.data.get());
104 rb.updateDynamicBuffer(meshbuf.index, 0, meshbuf.index->size(), idx_buf_data);
108 Flags flags()
const noexcept
override
111 for(
auto& attr : vertexAttributes)
113 switch(attr.location())
137 vertexBindings.clear();
138 vertexAttributes.clear();
141 void preparePipeline(QRhiGraphicsPipeline& pip)
const noexcept
override
143 if(cullMode == QRhiGraphicsPipeline::None)
145 pip.setDepthTest(
false);
146 pip.setDepthWrite(
false);
150 pip.setDepthTest(
true);
151 pip.setDepthWrite(
true);
154 pip.setTopology(this->topology);
155 pip.setCullMode(this->cullMode);
156 pip.setFrontFace(this->frontFace);
158 QRhiVertexInputLayout inputLayout;
159 inputLayout.setBindings(this->vertexBindings.begin(), this->vertexBindings.end());
160 inputLayout.setAttributes(
161 this->vertexAttributes.begin(), this->vertexAttributes.end());
162 pip.setVertexInputLayout(inputLayout);
165 void reload(
const ossia::mesh_list& ml,
const ossia::geometry_filter_list_ptr& f)
167 dirtyGeometryIndex++;
171 if(this->geom.meshes.size() == 0)
173 qDebug() <<
"Clearing geometry: ";
178 auto& g = this->geom.meshes[0];
180 vertexBindings.clear();
181 for(
auto& binding : g.bindings)
183 vertexBindings.emplace_back(
184 binding.stride, (QRhiVertexInputBinding::Classification)binding.classification,
188 vertexAttributes.clear();
189 for(
auto& attr : g.attributes)
191 vertexAttributes.emplace_back(
192 attr.binding, attr.location, (QRhiVertexInputAttribute::Format)attr.format,
196 if(g.buffers.empty())
198 qDebug() <<
"Error: empty buffer !";
202 topology = (QRhiGraphicsPipeline::Topology)g.topology;
203 cullMode = (QRhiGraphicsPipeline::CullMode)g.cull_mode;
204 frontFace = (QRhiGraphicsPipeline::FrontFace)g.front_face;
207 void draw(
const MeshBuffers& bufs, QRhiCommandBuffer& cb)
const noexcept
override
209 for(
auto& g : this->geom.meshes)
211 const auto sz = g.input.size();
213 QVarLengthArray<QRhiCommandBuffer::VertexInput> bindings(sz);
216 for(
auto& in : g.input)
218 bindings[i++] = {bufs.mesh, in.offset};
221 if(g.index.buffer >= 0)
223 const auto idxFmt = g.index.format == decltype(g.index)::uint16
224 ? QRhiCommandBuffer::IndexUInt32
225 : QRhiCommandBuffer::IndexUInt32;
226 cb.setVertexInput(0, sz, bindings.data(), bufs.index, g.index.offset, idxFmt);
230 cb.setVertexInput(0, sz, bindings.data());
233 if(g.index.buffer > -1)
235 cb.drawIndexed(g.indices);
Definition: CustomMesh.hpp:12
const char * defaultVertexShader() const noexcept override
A basic vertex shader that is going to work with this mesh.
Definition: CustomMesh.hpp:244
Graphics rendering pipeline for ossia score.
Definition: Filter/PreviewWidget.hpp:12
Data model for meshes.
Definition: Mesh.hpp:23