💿🐜 Antkeeper source code https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

170 lines
4.4 KiB

/*
* Copyright (C) 2023 Christopher J. Howard
*
* This file is part of Antkeeper source code.
*
* Antkeeper source code is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Antkeeper source code is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Antkeeper source code. If not, see <http://www.gnu.org/licenses/>.
*/
#include <engine/gl/vertex-buffer.hpp>
#include <stdexcept>
#include <glad/gl.h>
#include <utility>
namespace gl {
namespace
{
static constexpr GLenum buffer_usage_lut[] =
{
GL_STREAM_DRAW,
GL_STREAM_READ,
GL_STREAM_COPY,
GL_STATIC_DRAW,
GL_STATIC_READ,
GL_STATIC_COPY,
GL_DYNAMIC_DRAW,
GL_DYNAMIC_READ,
GL_DYNAMIC_COPY
};
}
vertex_buffer::vertex_buffer(buffer_usage usage, std::size_t size, std::span<const std::byte> data):
m_usage{usage},
m_size{size}
{
if (!data.empty())
{
// Bounds check
if (data.size() < size)
{
throw std::out_of_range("Vertex buffer construct operation exceeded data bounds.");
}
}
const GLenum gl_usage = buffer_usage_lut[std::to_underlying(m_usage)];
glCreateBuffers(1, &m_gl_named_buffer);
glNamedBufferData(m_gl_named_buffer, static_cast<GLsizeiptr>(m_size), data.empty() ? nullptr : data.data(), gl_usage);
}
vertex_buffer::vertex_buffer(const vertex_buffer& buffer):
vertex_buffer(buffer.m_usage, buffer.m_size)
{
copy(buffer, m_size);
}
vertex_buffer::vertex_buffer(vertex_buffer&& buffer):
m_gl_named_buffer{std::exchange(buffer.m_gl_named_buffer, 0)},
m_usage{std::move(buffer.m_usage)},
m_size{std::exchange(buffer.m_size, 0)}
{}
vertex_buffer::~vertex_buffer()
{
if (m_gl_named_buffer)
{
glDeleteBuffers(1, &m_gl_named_buffer);
}
}
vertex_buffer& vertex_buffer::operator=(const vertex_buffer& buffer)
{
repurpose(buffer.m_usage, buffer.m_size);
copy(buffer, m_size);
return *this;
}
vertex_buffer& vertex_buffer::operator=(vertex_buffer&& buffer)
{
if (m_gl_named_buffer)
{
glDeleteBuffers(1, &m_gl_named_buffer);
}
m_gl_named_buffer = std::exchange(buffer.m_gl_named_buffer, 0);
m_usage = std::move(buffer.m_usage);
m_size = std::exchange(buffer.m_size, 0);
return *this;
}
void vertex_buffer::repurpose(buffer_usage usage, std::size_t size, std::span<const std::byte> data)
{
if (!data.empty())
{
// Bounds check
if (data.size() < size)
{
throw std::out_of_range("Vertex buffer resize operation exceeded data bounds.");
}
}
m_usage = usage;
m_size = size;
const auto gl_usage = buffer_usage_lut[std::to_underlying(m_usage)];
glNamedBufferData(m_gl_named_buffer, static_cast<GLsizeiptr>(m_size), data.empty() ? nullptr : data.data(), gl_usage);
}
void vertex_buffer::write(std::size_t offset, std::span<const std::byte> data)
{
// Ignore empty write operations
if (data.empty())
{
return;
}
// Bounds check
if (offset + data.size() > m_size)
{
throw std::out_of_range("Vertex buffer write operation exceeded buffer bounds.");
}
glNamedBufferSubData(m_gl_named_buffer, static_cast<GLintptr>(offset), static_cast<GLsizeiptr>(data.size()), data.data());
}
void vertex_buffer::read(std::size_t offset, std::span<std::byte> data) const
{
// Ignore empty read operations
if (data.empty())
{
return;
}
// Bounds check
if (offset + data.size() > m_size)
{
throw std::out_of_range("Vertex buffer read operation exceeded buffer bounds.");
}
glGetNamedBufferSubData(m_gl_named_buffer, static_cast<GLintptr>(offset), static_cast<GLsizeiptr>(data.size()), data.data());
}
void vertex_buffer::copy(const vertex_buffer& read_buffer, std::size_t copy_size, std::size_t read_offset, std::size_t write_offset)
{
// Bounds check
if (read_offset + copy_size > read_buffer.m_size)
{
throw std::out_of_range("Vertex buffer copy operation exceeded read buffer bounds.");
}
if (write_offset + copy_size > m_size)
{
throw std::out_of_range("Vertex buffer copy operation exceeded write buffer bounds.");
}
glCopyNamedBufferSubData(read_buffer.m_gl_named_buffer, m_gl_named_buffer, static_cast<GLintptr>(read_offset), static_cast<GLintptr>(write_offset), static_cast<GLsizeiptr>(copy_size));
}
} // namespace gl