#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); }