aboutsummaryrefslogtreecommitdiff
path: root/source/CpuMesh.cpp
blob: 8e65395bfecb8496e6aa7985fcaa3805ce8257ff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
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);
}