aboutsummaryrefslogtreecommitdiff
path: root/source/CpuMesh.cpp
diff options
context:
space:
mode:
authorrtk0c <[email protected]>2022-04-08 22:30:12 -0700
committerrtk0c <[email protected]>2022-04-08 22:30:12 -0700
commite7ef3f208c109357538b1f68af10bcd78db95c95 (patch)
tree066d614ae0f079e53602d7c0fd972998c546c8c1 /source/CpuMesh.cpp
parentf163e8f37123e651ea80b690793845b31ddb8639 (diff)
Changeset: 3 More work
Diffstat (limited to 'source/CpuMesh.cpp')
-rw-r--r--source/CpuMesh.cpp58
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);
+}