summaryrefslogtreecommitdiff
path: root/lib/glAllocator.cpp
blob: 633f7ab9eaf60d974bdb68eb42ad05f108b03acf (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
#include "glAllocator.h"

namespace Detail {
	std::pair<void *, GLuint>
	allocateBuffer(size_t count, size_t objSize)
	{
		constexpr static GLbitfield MAPPING_FLAGS
				= GL_MAP_WRITE_BIT | GL_MAP_READ_BIT | GL_MAP_PERSISTENT_BIT | GL_MAP_COHERENT_BIT;
		constexpr static GLbitfield STORAGE_FLAGS = GL_DYNAMIC_STORAGE_BIT | MAPPING_FLAGS;
		GLuint name = 0;
		glCreateBuffers(1, &name);
		const auto size = static_cast<GLsizeiptr>(count * objSize);
		glNamedBufferStorage(name, size, nullptr, STORAGE_FLAGS);
		const auto data = (glMapNamedBufferRange(name, 0, size, MAPPING_FLAGS));
		if (!data) {
			glDeleteBuffers(1, &name);
			throw std::bad_alloc();
		}
		return {data, name};
	}

	void
	deallocateBuffer(GLuint name)
	{
		glUnmapNamedBuffer(name);
		glDeleteBuffers(1, &name);
	}
}