our_dick/include/gfx/ogl/buffer_map.hpp
2022-02-10 16:56:01 -08:00

137 lines
3.6 KiB
C++

/**
This file is a part of our_dick
Copyright (C) 2020-2022 rexy712
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program 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 Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef OUR_DICK_GRAPHICS_OGL_BUFFER_MAP_HPP
#define OUR_DICK_GRAPHICS_OGL_BUFFER_MAP_HPP
#include "gl_include.hpp"
#include <cstdlib> //size_t, ptrdiff_t
namespace gfx::ogl{
namespace buffer{
//strongly typed enum for different mapping styles
enum class maptype : GLenum{
READ = GL_READ_ONLY,
WRITE = GL_WRITE_ONLY,
RW = GL_READ_WRITE,
};
//strongly typed enum for different usages
enum class usage : GLenum{
STATIC_DRAW = GL_STATIC_DRAW,
DYNAMIC_DRAW = GL_DYNAMIC_DRAW,
STREAM_DRAW = GL_STREAM_DRAW,
STATIC_READ = GL_STATIC_READ,
DYNAMIC_READ = GL_DYNAMIC_READ,
STREAM_READ = GL_STREAM_READ,
STATIC_COPY = GL_STATIC_COPY,
DYNAMIC_COPY = GL_DYNAMIC_COPY,
STREAM_COPY = GL_STREAM_COPY,
};
}
//RAII wrapper for mapping vbo's
template<typename T>
class scoped_buffer_map
{
public:
using value_type = T;
using size_type = size_t;
using difference_type = ptrdiff_t;
using reference = T&;
using const_reference = const T&;
using pointer = T*;
using const_pointer = const T*;
private:
pointer m_data = nullptr; //pointer to the mapped region
GLuint m_buffer;
public:
//create a mapping for 'v' on 'targ' with map style 'm'
explicit scoped_buffer_map(GLuint bid, buffer::maptype m);
scoped_buffer_map(const scoped_buffer_map&) = delete;
scoped_buffer_map(scoped_buffer_map&&);
~scoped_buffer_map(void);
scoped_buffer_map& operator=(const scoped_buffer_map&) = delete;
scoped_buffer_map& operator=(scoped_buffer_map&&);
//size of the mapped region as reported by glGetBufferParameteriv
size_type length(void)const;
//release ownership of the mapping. unsafe
pointer release(void);
//direct access to the underlaying data region
operator pointer(void);
operator const_pointer(void)const;
pointer raw(void);
const_pointer raw(void)const;
bool valid(void)const;
//indexed access to the data region
reference operator[](size_type i);
const_reference operator[](size_type i)const;
};
//specialization for void pointers. The same in every way except no subscript operators
template<>
class scoped_buffer_map<void>
{
public:
using value_type = void;
using size_type = size_t;
using difference_type = ptrdiff_t;
using reference = void;
using const_reference = void;
using pointer = void*;
using const_pointer = const void*;
private:
pointer m_data = nullptr;
GLuint m_buffer;
public:
explicit scoped_buffer_map(GLuint bid, buffer::maptype m);
scoped_buffer_map(const scoped_buffer_map&) = delete;
scoped_buffer_map(scoped_buffer_map&&);
~scoped_buffer_map(void);
scoped_buffer_map& operator=(const scoped_buffer_map&) = delete;
scoped_buffer_map& operator=(scoped_buffer_map&&);
size_type length(void)const;
pointer release(void);
operator pointer(void);
operator const_pointer(void)const;
pointer raw(void);
const_pointer raw(void)const;
bool valid(void)const;
};
}
#include "buffer_map.tpp"
#endif