mirror of
https://github.com/MadeOfJelly/MushMachine.git
synced 2024-10-30 06:45:36 +01:00
86 lines
1.6 KiB
C++
86 lines
1.6 KiB
C++
//
|
|
// Created by FlaXxy on 25.08.2018.
|
|
//
|
|
|
|
#pragma once
|
|
|
|
#ifdef MM_OPENGL_3_GLES
|
|
#include <GLES3/gl3.h>
|
|
#else
|
|
#include <glad/glad.h>
|
|
#endif
|
|
|
|
#include <cstdint>
|
|
//#include <logger.hpp>
|
|
|
|
namespace MM::OpenGL {
|
|
|
|
template<typename TInstance>
|
|
class InstanceBuffer {
|
|
private:
|
|
GLuint _handle;
|
|
size_t _size;
|
|
|
|
public:
|
|
explicit InstanceBuffer(void) : _handle(0), _size(0) {
|
|
glGenBuffers(1, &_handle);
|
|
}
|
|
|
|
~InstanceBuffer(void) {
|
|
glDeleteBuffers(1, &_handle);
|
|
}
|
|
|
|
void bind(GLenum target = GL_ARRAY_BUFFER) const {
|
|
glBindBuffer(target, _handle);
|
|
}
|
|
|
|
void unbind(GLenum target = GL_ARRAY_BUFFER) const {
|
|
glBindBuffer(target, 0);
|
|
}
|
|
|
|
void resize(size_t size, GLenum usage) {
|
|
glBindBuffer(GL_ARRAY_BUFFER, _handle);
|
|
glBufferData(GL_ARRAY_BUFFER, size * sizeof(TInstance), nullptr, usage);
|
|
_size = size;
|
|
}
|
|
|
|
size_t getSize(void) const {
|
|
return _size;
|
|
}
|
|
|
|
size_t getSizeBytes(void) const {
|
|
return _size * sizeof(TInstance);
|
|
}
|
|
|
|
TInstance* map(size_t size, GLenum usage = GL_DYNAMIC_DRAW, bool shrink = false) {
|
|
if (size > _size || (shrink && (size < _size)))
|
|
resize(size, usage);
|
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, _handle);
|
|
|
|
auto* res = static_cast<TInstance*>(
|
|
glMapBufferRange(
|
|
GL_ARRAY_BUFFER,
|
|
0,
|
|
size * sizeof(TInstance),
|
|
GL_MAP_WRITE_BIT | GL_MAP_INVALIDATE_BUFFER_BIT
|
|
)
|
|
);
|
|
//auto* res = static_cast<TInstance*>(
|
|
//glMapBuffer(
|
|
//GL_ARRAY_BUFFER,
|
|
//GL_WRITE_ONLY
|
|
//)
|
|
//);
|
|
|
|
return res;
|
|
}
|
|
|
|
void unmap(void) {
|
|
glBindBuffer(GL_ARRAY_BUFFER,_handle);
|
|
glUnmapBuffer(GL_ARRAY_BUFFER);
|
|
}
|
|
};
|
|
} // MM::OpenGL
|
|
|