/** This file is a part of our_dick Copyright (C) 2020 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 . */ #ifndef OUR_DICK_GRAPHICS_GL_BUFFERS_HPP #define OUR_DICK_GRAPHICS_GL_BUFFERS_HPP #include "gl_include.hpp" namespace gfx{ class gl_buffer; class gl_frame_buffer; //handle interfacing with the gl_buffer objects. Used as intermediary so that multiple classes have the ability to //access gl_buffers easily class buffer_iface { protected: mutable gl_buffer* m_bound = nullptr; //pointer to currently bound target public: buffer_iface() = default; buffer_iface(const buffer_iface&); buffer_iface(buffer_iface&&); ~buffer_iface(); buffer_iface& operator=(const buffer_iface&); buffer_iface& operator=(buffer_iface&&); //set the given gl_buffer target to use this buffer bool bind(gl_buffer& b)const; //same as bind, but also disallow rebinding until unlock is called bool bind_lock(gl_buffer& b)const; //release the lock held on the target void unlock()const; //set the associated target to point to nothing. also calls unlock void unbind()const; //check if a lock is held on the associated target bool is_locked()const; //get raw access to opengl target id GLenum get_bound_id()const; //get access to opengl target buffer gl_buffer* get_bound_buffer()const; }; class frame_buffer_iface : protected buffer_iface { public: frame_buffer_iface() = default; frame_buffer_iface(const frame_buffer_iface&) = default; frame_buffer_iface(frame_buffer_iface&&) = default; ~frame_buffer_iface() = default; frame_buffer_iface& operator=(const frame_buffer_iface&) = default; frame_buffer_iface& operator=(frame_buffer_iface&&) = default; bool bind(gl_frame_buffer& b)const; bool bind_lock(gl_frame_buffer& b)const; using buffer_iface::unlock; using buffer_iface::unbind; using buffer_iface::is_locked; using buffer_iface::get_bound_id; gl_frame_buffer* get_bound_buffer()const; }; //Manage active opengl buffers and targets class gl_buffer { friend class buffer; //access to constructor/destructor friend class buffer_iface; //access to bind protected: const buffer_iface* bound = nullptr; //vbo bound to this buffer const GLenum m_buffer; //target this object represents bool m_lock = false; //if the target disallows ownership changes protected: gl_buffer(GLenum buf); ~gl_buffer() = default; void bind(const buffer_iface* newbind); void lock(bool b); bool is_locked()const; public: //return id of the target this represents GLenum get_buffer_id()const; }; class gl_frame_buffer : protected gl_buffer { friend class framebuffer; friend class frame_buffer_iface; protected: using gl_buffer::gl_buffer; ~gl_frame_buffer() = default; public: using gl_buffer::get_buffer_id; }; class buffer //glorified namespace for added access control { //list of all the standard opengl buffer targets public: static inline gl_buffer array{GL_ARRAY_BUFFER}; static inline gl_buffer atomic_counter{GL_ATOMIC_COUNTER_BUFFER}; static inline gl_buffer copy_read{GL_COPY_READ_BUFFER}; static inline gl_buffer copy_write{GL_COPY_WRITE_BUFFER}; static inline gl_buffer dispatch_indirect{GL_DISPATCH_INDIRECT_BUFFER}; static inline gl_buffer draw_indirect{GL_DRAW_INDIRECT_BUFFER}; static inline gl_buffer element_array{GL_ELEMENT_ARRAY_BUFFER}; static inline gl_buffer pixel_pack{GL_PIXEL_PACK_BUFFER}; static inline gl_buffer pixel_unpack{GL_PIXEL_UNPACK_BUFFER}; static inline gl_buffer query{GL_QUERY_BUFFER}; static inline gl_buffer shader_storage{GL_SHADER_STORAGE_BUFFER}; static inline gl_buffer texture{GL_TEXTURE_BUFFER}; static inline gl_buffer transform_feedback{GL_TRANSFORM_FEEDBACK_BUFFER}; static inline gl_buffer uniform{GL_UNIFORM_BUFFER}; private: buffer() = default; ~buffer() = default; }; class framebuffer { //list of all the standard opengl framebuffer targets public: static inline gl_frame_buffer draw{GL_DRAW_FRAMEBUFFER}; static inline gl_frame_buffer read{GL_READ_FRAMEBUFFER}; private: framebuffer() = default; ~framebuffer() = default; }; } #endif