17
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-10-17 14:29:20 +02:00
Files
maszyna/gl/buffer.cpp
jerrrrycho 43fcda05a0 refactor(sonar): use std::array for local fixed-size arrays (cpp:S5945)
Arrays that are class members or part of headers are left unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-10 17:44:14 +02:00

97 lines
2.3 KiB
C++

#include "stdafx.h"
#include "buffer.h"
GLenum gl::buffer::glenum_target(gl::buffer::targets target)
{
static std::array<GLenum, 13> const mapping =
{
GL_ARRAY_BUFFER,
GL_ATOMIC_COUNTER_BUFFER,
GL_COPY_READ_BUFFER,
GL_COPY_WRITE_BUFFER,
GL_DISPATCH_INDIRECT_BUFFER,
GL_DRAW_INDIRECT_BUFFER,
GL_ELEMENT_ARRAY_BUFFER,
GL_PIXEL_PACK_BUFFER,
GL_PIXEL_UNPACK_BUFFER,
GL_SHADER_STORAGE_BUFFER,
GL_TEXTURE_BUFFER,
GL_TRANSFORM_FEEDBACK_BUFFER,
GL_UNIFORM_BUFFER
};
return mapping[target];
}
void gl::buffer::bind(targets target) const
{
if (binding_points[target] == *this)
return;
glBindBuffer(glenum_target(target), *this);
binding_points[target] = *this;
}
void gl::buffer::bind_base(targets target, GLuint index) const
{
glBindBufferBase(glenum_target(target), index, *this);
binding_points[target] = *this;
}
void gl::buffer::unbind(targets target)
{
if( binding_points[ target ] == 0 ) { return; }
glBindBuffer(glenum_target(target), 0);
binding_points[target] = 0;
}
void gl::buffer::unbind()
{
for (size_t i = 0; i < std::size(binding_points); i++)
unbind((targets)i);
}
gl::buffer::buffer()
{
glGenBuffers(1, *this);
}
gl::buffer::~buffer()
{
// forget the bindings of this buffer; gl can hand its name to a new buffer,
// which the tracker would then treat as already bound and skip the bind
for (auto &point : binding_points)
if (point == *this)
point = 0;
glDeleteBuffers(1, *this);
}
void gl::buffer::allocate(targets target, GLsizeiptr size, GLenum hint)
{
bind(target);
glBufferData(glenum_target(target), size, nullptr, hint);
}
void gl::buffer::upload(targets target, const void *data, int offset, GLsizeiptr size)
{
bind(target);
glBufferSubData(glenum_target(target), offset, size, data);
}
void gl::buffer::download(targets target, void *data, int offset, GLsizeiptr size)
{
bind(target);
if (GLAD_GL_VERSION_3_3)
{
glGetBufferSubData(glenum_target(target), offset, size, data);
}
else
{
void const *glbuf = glMapBufferRange(glenum_target(target), offset, size, GL_MAP_READ_BIT);
memcpy(data, glbuf, size);
glUnmapBuffer(glenum_target(target));
}
}
thread_local GLuint gl::buffer::binding_points[13];