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;
24 int64_t dirtyGeometryIndex{-1};
25 explicit CustomMesh(
const ossia::mesh_list& g) { reload(g); }
27 [[nodiscard]]
MeshBuffers init(QRhi& rhi)
const noexcept
override
29 if(geom.meshes.empty())
31 if(geom.meshes[0].buffers.empty())
34 const auto vtx_buf_size = geom.meshes[0].buffers[0].size;
36 = rhi.newBuffer(QRhiBuffer::Dynamic, QRhiBuffer::VertexBuffer, vtx_buf_size);
37 mesh_buf->setName(
"Mesh::mesh_buf");
40 QRhiBuffer* idx_buf{};
41 if(geom.meshes[0].buffers.size() > 1)
43 if(
const auto idx_buf_size = geom.meshes[0].buffers[1].size; idx_buf_size > 0)
46 = rhi.newBuffer(QRhiBuffer::Dynamic, QRhiBuffer::IndexBuffer, idx_buf_size);
47 idx_buf->setName(
"Mesh::idx_buf");
56 void update(
MeshBuffers& meshbuf, QRhiResourceUpdateBatch& rb)
const noexcept
override
58 if(geom.meshes.empty())
60 if(geom.meshes[0].buffers.empty())
63 void* idx_buf_data =
nullptr;
64 const auto vtx_buf = geom.meshes[0].buffers[0];
65 if(
auto sz = vtx_buf.size; sz != meshbuf.mesh->size())
67 meshbuf.mesh->destroy();
68 meshbuf.mesh->setSize(sz);
69 meshbuf.mesh->create();
74 const auto idx_buf = geom.meshes[0].buffers[1];
75 if(geom.meshes[0].buffers.size() > 1)
77 if(
const auto idx_buf_size = idx_buf.size; idx_buf_size > 0)
79 idx_buf_data = idx_buf.data.get();
81 if(
auto sz = idx_buf.size; sz != meshbuf.index->size())
83 meshbuf.index->destroy();
84 meshbuf.index->setSize(sz);
85 meshbuf.index->create();
98 rb.updateDynamicBuffer(meshbuf.mesh, 0, meshbuf.mesh->size(), vtx_buf.data.get());
101 rb.updateDynamicBuffer(meshbuf.index, 0, meshbuf.index->size(), idx_buf_data);
105 Flags flags()
const noexcept
override
108 for(
auto& attr : vertexAttributes)
110 switch(attr.location())
134 vertexBindings.clear();
135 vertexAttributes.clear();
138 void preparePipeline(QRhiGraphicsPipeline& pip)
const noexcept
override
140 if(cullMode == QRhiGraphicsPipeline::None)
142 pip.setDepthTest(
false);
143 pip.setDepthWrite(
false);
147 pip.setDepthTest(
true);
148 pip.setDepthWrite(
true);
151 pip.setTopology(this->topology);
152 pip.setCullMode(this->cullMode);
153 pip.setFrontFace(this->frontFace);
155 QRhiVertexInputLayout inputLayout;
156 inputLayout.setBindings(this->vertexBindings.begin(), this->vertexBindings.end());
157 inputLayout.setAttributes(
158 this->vertexAttributes.begin(), this->vertexAttributes.end());
159 pip.setVertexInputLayout(inputLayout);
162 void reload(
const ossia::mesh_list& ml)
166 if(this->geom.meshes.size() == 0)
168 qDebug() <<
"Clearing geometry: ";
173 auto& g = this->geom.meshes[0];
175 vertexBindings.clear();
176 for(
auto& binding : g.bindings)
178 vertexBindings.emplace_back(
179 binding.stride, (QRhiVertexInputBinding::Classification)binding.classification,
183 vertexAttributes.clear();
184 for(
auto& attr : g.attributes)
186 vertexAttributes.emplace_back(
187 attr.binding, attr.location, (QRhiVertexInputAttribute::Format)attr.format,
191 if(g.buffers.empty())
193 qDebug() <<
"Error: empty buffer !";
197 topology = (QRhiGraphicsPipeline::Topology)g.topology;
198 cullMode = (QRhiGraphicsPipeline::CullMode)g.cull_mode;
199 frontFace = (QRhiGraphicsPipeline::FrontFace)g.front_face;
202 void draw(
const MeshBuffers& bufs, QRhiCommandBuffer& cb)
const noexcept
override
204 for(
auto& g : this->geom.meshes)
206 const auto sz = g.input.size();
208 QVarLengthArray<QRhiCommandBuffer::VertexInput> bindings(sz);
211 for(
auto& in : g.input)
213 bindings[i++] = {bufs.mesh, in.offset};
216 if(g.index.buffer >= 0)
218 const auto idxFmt = g.index.format == decltype(g.index)::uint16
219 ? QRhiCommandBuffer::IndexUInt32
220 : QRhiCommandBuffer::IndexUInt32;
221 cb.setVertexInput(0, sz, bindings.data(), bufs.index, g.index.offset, idxFmt);
225 cb.setVertexInput(0, sz, bindings.data());
228 if(g.index.buffer > -1)
230 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:239
Graphics rendering pipeline for ossia score.
Definition: PreviewWidget.hpp:12
Data model for meshes.
Definition: Mesh.hpp:22