summaryrefslogtreecommitdiff
path: root/gfx/gl/instanceVertices.h
blob: 036b48ecca57c1fb86613d578e5de9995098bc5b (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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#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)
	{
		allocBuffer(initialSize);
	}

	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)
		{
			if (instances) {
				instances->release(*this);
			}
			instances = std::exchange(other.instances, nullptr);
			index = other.index;
			return *this;
		}
		template<typename U>
		T &
		operator=(U && v)
		{
			return instances->at(index) = std::forward<U>(v);
		}

		operator T &()
		{
			return instances->at(index);
		}
		operator const T &() const
		{
			return instances->at(index);
		}

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

	private:
		friend InstanceVertices;

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

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

	const auto &
	bufferName() const
	{
		unmap();
		return buffer;
	}

	auto
	count() const
	{
		unmap();
		return next;
	}

protected:
	friend InstanceProxy;

	void
	release(const InstanceProxy & p)
	{
		// Destroy p's object
		at(p.index).~T();
		if (next-- > index[p.index]) {
			// Remember p.index is free index now
			unused.push_back(p.index);
			// Move last object into p's slot
			new (&at(p.index)) T {std::move(data[next])};
			(data[next]).~T();
			for (auto & i : index) {
				if (i == next) {
					i = index[p.index];
					break;
				}
			}
			index[p.index] = 100000;
		}
		else {
			index.pop_back();
		}
	}

	void
	allocBuffer(std::size_t newCapacity)
	{
		glBindBuffer(GL_ARRAY_BUFFER, buffer);
		glBufferData(GL_ARRAY_BUFFER, static_cast<GLsizeiptr>(sizeof(T) * newCapacity), nullptr, GL_DYNAMIC_DRAW);
		glBindBuffer(GL_ARRAY_BUFFER, 0);
		capacity = newCapacity;
		data = nullptr;
	}

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

	T &
	at(size_t pindex)
	{
		map();
		return data[index[pindex]];
	}

	void
	map() const
	{
		if (!data) {
			data = static_cast<T *>(glMapNamedBuffer(buffer, GL_READ_WRITE));
		}
	}

	void
	unmap() const
	{
		if (data) {
			glUnmapNamedBuffer(buffer);
			data = nullptr;
		}
	}

	glBuffer buffer;
	mutable T * data {};
	// Size of buffer
	std::size_t capacity {};
	// # used of capacity
	std::size_t next {};
	// Index into buffer given to nth proxy
	std::vector<size_t> index;
	// List of free spaces in index
	std::vector<size_t> unused;
};