diff options
Diffstat (limited to 'source/CpuMesh.cpp')
-rw-r--r-- | source/CpuMesh.cpp | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/source/CpuMesh.cpp b/source/CpuMesh.cpp new file mode 100644 index 0000000..8e65395 --- /dev/null +++ b/source/CpuMesh.cpp @@ -0,0 +1,58 @@ +#include "CpuMesh.hpp" + +bool CpuMesh::IsEmpty() const { + return !mVertexFormat->elements.empty(); +} + +std::byte* CpuMesh::GetVertices() const { + return mVertexData.get(); +} + +int CpuMesh::GetVertexNumBytes() const { + return mVertexByteCount; +} + +std::byte* CpuMesh::GetIndices() const { + return mIndexData.get(); +} + +int CpuMesh::GetIndexNumBytes() const { + return mIndexCount * Tags::SizeOf(mIndexType); +} + +GpuMesh* CpuMesh::SyncToGpuCreate() const { + if (IsEmpty()) return nullptr; + + auto vertexBuffer = new GpuVertexBuffer(); + vertexBuffer->Upload(mVertexData.get(), GetVertexNumBytes()); + + auto bindings = new BufferBindings(); + for (auto& elm : mVertexFormat->elements) { + bindings->SetBinding(elm.bindingIndex, vertexBuffer); + } + + auto indexBuffer = new GpuIndexBuffer(); + indexBuffer->Upload(mIndexData.get(), mIndexType, mIndexCount); + + return new GpuMesh(mVertexFormat.Get(), bindings, indexBuffer); +} + +void CpuMesh::SyncToGpu(GpuMesh& mesh) const { + if (IsEmpty()) return; + + auto& oldFormat = mesh.vertFormat; + auto& newFormat = this->mVertexFormat; + if (oldFormat != newFormat) { + auto buffer = new GpuVertexBuffer(); + buffer->Upload(mVertexData.get(), GetVertexNumBytes()); + + mesh.vertBufBindings->Clear(); + for (auto& elm : newFormat->elements) { + mesh.vertBufBindings->SetBinding(elm.bindingIndex, buffer); + } + + oldFormat = newFormat; + } + + mesh.indexBuf->Upload(mIndexData.get(), mIndexType, mIndexCount); +} |