summaryrefslogtreecommitdiff
path: root/gfx/gl/instanceVertices.h
blob: cf3b75b501344a36ba10668a8c004928002c8512 (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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#pragma once

#include "glArrays.h"
#include <iterator>
#include <span>
#include <special_members.hpp>
#include <utility>
#include <vector>

template<typename T> class InstanceVertices {
public:
	InstanceVertices(size_t initialSize = 16) : data {allocBuffer(buffer, initialSize)}, next {} { }

	~InstanceVertices()
	{
		glUnmapNamedBuffer(buffer);
	}

	class InstanceProxy {
	public:
		InstanceProxy(InstanceVertices * iv, std::size_t idx) : instances {iv}, index {idx} { }
		InstanceProxy(InstanceProxy && other) : instances {std::exchange(other.instances, nullptr)}, index {other.index}
		{
		}
		NO_COPY(InstanceProxy);

		~InstanceProxy()
		{
			if (instances) {
				instances->release(*this);
			}
		}

		InstanceProxy &
		operator=(InstanceProxy && other)
		{
			instances = std::exchange(other.instances, nullptr);
			index = other.index;
		}

		operator T &()
		{
			return *get();
		}
		operator const T &() const
		{
			return *get();
		}
		template<typename U>
		T &
		operator=(U && v)
		{
			return instances->data[index] = std::forward<U>(v);
		}

		T *
		get()
		{
			return &instances->data[index];
		}
		const T *
		get() const
		{
			return &instances->data[index];
		}
		T *
		operator->()
		{
			return get();
		}
		const T *
		operator->() const
		{
			return get();
		}
		T &
		operator*()
		{
			return *get();
		}
		const T &
		operator*() const
		{
			return *get();
		}

	private:
		friend InstanceVertices;

		InstanceVertices<T> * instances;
		std::size_t index;
	};

	template<typename... Params>
	InstanceProxy
	acquire(Params &&... params)
	{
		if (!unused.empty()) {
			auto idx = unused.back();
			unused.pop_back();
			new (&data[idx]) T(std::forward<Params>(params)...);
			return InstanceProxy {this, idx};
		}
		if (next >= data.size()) {
			resize(data.size() * 2);
		}
		new (&data[next]) T(std::forward<Params>(params)...);
		return InstanceProxy {this, next++};
	}

	void
	release(const InstanceProxy & p)
	{
		data[p.index].~T();
		unused.push_back(p.index);
	}

protected:
	friend InstanceProxy;

	static std::span<T>
	allocBuffer(const glBuffer & buffer, std::size_t count)
	{
		glBindBuffer(GL_ARRAY_BUFFER, buffer);
		glBufferData(GL_ARRAY_BUFFER, static_cast<GLsizeiptr>(sizeof(T) * count), nullptr, GL_DYNAMIC_DRAW);
		auto data = static_cast<T *>(glMapNamedBuffer(buffer, GL_READ_WRITE));
		glBindBuffer(GL_ARRAY_BUFFER, 0);
		return {data, count};
	}

	void
	resize(size_t newCapacity)
	{
		const auto maintain = std::min(newCapacity, data.size());
		const auto maintaind = static_cast<typename decltype(data)::difference_type>(maintain);
		std::vector<T> existing;
		existing.reserve(maintain);
		std::move(data.begin(), data.begin() + maintaind, std::back_inserter(existing));
		data = allocBuffer(buffer, newCapacity);
		std::move(existing.begin(), existing.begin() + maintaind, data.begin());
	}

	glBuffer buffer;
	std::span<T> data;
	std::size_t next;
	std::vector<size_t> unused;
};