aboutsummaryrefslogtreecommitdiff
path: root/source/Mesh.hpp
blob: a1c098476bb3417cca1bb003c881d8c76711a916 (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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#pragma once

#include "GraphicsTags.hpp"
#include "RcPtr.hpp"
#include "SmallVector.hpp"

#include <glad/glad.h>
#include <cstddef>
#include <cstdint>
#include <vector>

struct GpuVertexBuffer : public RefCounted {
	GLuint handle;
	int sizeInBytes;

	GpuVertexBuffer();
	~GpuVertexBuffer();

	void Upload(const std::byte* data, size_t sizeInBytes);
};

struct GpuIndexBuffer : public RefCounted {
	GLuint handle;
	Tags::IndexType indexType;
	int sizeInBytes;

	GpuIndexBuffer();
	~GpuIndexBuffer();

	void Upload(const std::byte* data, size_t count);
	void Upload(const std::byte* data, Tags::IndexType type, size_t count);
};

struct BufferBindings : public RefCounted {
	SmallVector<RcPtr<GpuVertexBuffer>, 4> bindings;

	int GetMaxBindingIndex() const;

	/// Safe. Returns nullptr if the index is not bound to any buffers.
	GpuVertexBuffer* GetBinding(int index) const;
	/// Adds or updates a buffer binding. Setting a binding to nullptr effectively removes the binding.
	void SetBinding(int index, GpuVertexBuffer* buffer);
	void Clear();
};

struct VertexElementFormat {
	int offset;
	int bindingIndex;
	Tags::VertexElementType type;
	Tags::VertexElementSemantic semantic;

	int GetStride() const;

	auto operator<=>(const VertexElementFormat&) const = default;
};

struct VertexFormat : public RefCounted {
	std::vector<VertexElementFormat> elements;
	int vertexSize = 0;

	const std::vector<VertexElementFormat>& GetElements() { return elements; }
	void AddElement(VertexElementFormat element);
	void RemoveElement(int index);

	void Sort();
	void CompactBindingIndex();
};

class GpuMesh : public RefCounted {
public:
	RcPtr<VertexFormat> vertFormat;
	RcPtr<BufferBindings> vertBufBindings;
	RcPtr<GpuIndexBuffer> indexBuf;

public:
	GpuMesh(VertexFormat* vertexFormat, BufferBindings* bindings, GpuIndexBuffer* indexBuffer);

	bool IsEmpty() const;
	void SetVertex(VertexFormat* vertexFormat, BufferBindings* bindings);
	VertexFormat* GetVertexFormat() const;
	BufferBindings* GetVertexBufferBindings() const;
	void SetIndex(GpuIndexBuffer* buffer);
	GpuIndexBuffer* GetIndexBuffer() const;
};