Switch to true modern opengl (DSA)

This commit is contained in:
rexy712 2020-10-01 05:01:30 -07:00
parent c45875c108
commit b2c8b0b3b5
10 changed files with 215 additions and 562 deletions

View File

@ -30,8 +30,6 @@ namespace gfx{
{ {
private: private:
GLuint m_buffer; GLuint m_buffer;
frame_buffer_accessor m_draw;
frame_buffer_accessor m_read;
public: public:
fbo(); fbo();
@ -50,12 +48,6 @@ namespace gfx{
bool bind()const; bool bind()const;
bool bind(gl_frame_buffer_manager& b)const; bool bind(gl_frame_buffer_manager& b)const;
bool bind_lock(gl_frame_buffer_manager& b)const;
void bind_unlock(gl_frame_buffer_manager& b)const;
void unbind()const;
gl_frame_buffer_manager* get_bound_buffer()const;
GLenum get_bound_id()const;
}; };
} }

View File

@ -101,7 +101,7 @@ namespace gfx{
//bind to GL_TEXTURE_2D //bind to GL_TEXTURE_2D
void bind()const; void bind()const;
//bind to given texture unit and load into given program uniform location //bind to given texture unit and load into given program uniform location
void use(GLuint tunit, GLint uloc)const; void bind_unit(GLuint tunit)const;
}; };
} }

View File

@ -21,6 +21,7 @@
#include "gl_include.hpp" #include "gl_include.hpp"
#include "math/math.hpp" #include "math/math.hpp"
#include "vbo.hpp"
namespace gfx{ namespace gfx{
using namespace math; using namespace math;
@ -45,6 +46,9 @@ namespace gfx{
GLuint raw()const; GLuint raw()const;
void bind_buffer(const vbo& buffer, GLuint index, GLuint offset, GLuint stride);
void bind_element_buffer(const vbo& buffer);
//Get access to a generic vertex attribute within this vao //Get access to a generic vertex attribute within this vao
vertex_attribute get_attribute(int index); vertex_attribute get_attribute(int index);
@ -72,60 +76,22 @@ namespace gfx{
constexpr vertex_attribute& operator=(vertex_attribute&&) = default; constexpr vertex_attribute& operator=(vertex_attribute&&) = default;
//enable use as an array attribute (most likely what is desired) //enable use as an array attribute (most likely what is desired)
void enable_array_mode(); void enable();
//disable use as an array attribute. used for static data in attributes during rendering //disable use as an array attribute. used for static data in attributes during rendering
void disable_array_mode(); void disable();
void associate_with(GLuint buffer_binding);
//setters for use in array mode. Same arguments as glVertexAttribPointer but these take arguments //setters for use in array mode. Same arguments as glVertexAttribPointer but these take arguments
//as object counts rather than bytes. a valid vbo must be bound to buffers::array_buffer before these are called. //as object counts rather than bytes. a valid vbo must be bound to buffers::array_buffer before these are called.
void set_float_pointer(GLint size, GLsizei stride, GLsizei offset); void set_float_array(GLint size, GLsizei offset);
void set_double_pointer(GLint size, GLsizei stride, GLsizei offset); void set_double_array(GLint size, GLsizei offset);
void set_byte_pointer(GLint size, GLsizei stride, GLsizei offset); void set_byte_array(GLint size, GLsizei offset);
void set_ubyte_pointer(GLint size, GLsizei stride, GLsizei offset); void set_ubyte_array(GLint size, GLsizei offset);
void set_short_pointer(GLint size, GLsizei stride, GLsizei offset); void set_short_array(GLint size, GLsizei offset);
void set_ushort_pointer(GLint size, GLsizei stride, GLsizei offset); void set_ushort_array(GLint size, GLsizei offset);
void set_int_pointer(GLint size, GLsizei stride, GLsizei offset); void set_int_array(GLint size, GLsizei offset);
void set_uint_pointer(GLint size, GLsizei stride, GLsizei offset); void set_uint_array(GLint size, GLsizei offset);
//setters for use in non array mode
void set(GLfloat);
void set(GLshort);
void set(GLdouble);
void set(GLint);
void set(GLuint);
void set(GLfloat, GLfloat);
void set(GLshort, GLshort);
void set(GLdouble, GLdouble);
void set(GLint, GLint);
void set(GLuint, GLuint);
void set(GLfloat, GLfloat, GLfloat);
void set(GLshort, GLshort, GLshort);
void set(GLdouble, GLdouble, GLdouble);
void set(GLint, GLint, GLint);
void set(GLuint, GLuint, GLuint);
void set(GLfloat, GLfloat, GLfloat, GLfloat);
void set(GLshort, GLshort, GLshort, GLshort);
void set(GLdouble, GLdouble, GLdouble, GLdouble);
void set(GLint, GLint, GLint, GLint);
void set(GLuint, GLuint, GLuint, GLuint);
void set(const vec2<GLfloat>&);
void set(const vec2<GLshort>&);
void set(const vec2<GLdouble>&);
void set(const vec2<GLint>&);
void set(const vec2<GLuint>&);
void set(const vec3<GLfloat>&);
void set(const vec3<GLshort>&);
void set(const vec3<GLdouble>&);
void set(const vec3<GLint>&);
void set(const vec3<GLuint>&);
void set(const vec4<GLfloat>&);
void set(const vec4<GLshort>&);
void set(const vec4<GLdouble>&);
void set(const vec4<GLint>&);
void set(const vec4<GLuint>&);
void set(const vec4<GLbyte>&);
void set(const vec4<GLubyte>&);
void set(const vec4<GLushort>&);
}; };
} }

View File

@ -21,8 +21,6 @@
#include "gl_include.hpp" #include "gl_include.hpp"
#include "gl_buffers.hpp" #include "gl_buffers.hpp"
#include "scoped_buffer_bind.hpp"
#include <cstdlib> //size_t, ptrdiff_t #include <cstdlib> //size_t, ptrdiff_t
#include <utility> //exchange, swap #include <utility> //exchange, swap
@ -56,7 +54,6 @@ namespace gfx{
private: private:
GLuint m_buffer; //handle to vbo GLuint m_buffer; //handle to vbo
size_t m_buffer_size; //amount of buffer currently in use size_t m_buffer_size; //amount of buffer currently in use
buffer_accessor m_bi; //interface with gl_buffer stuff
public: public:
//create a buffer with given capacity with predicted usage case 't' //create a buffer with given capacity with predicted usage case 't'
@ -70,12 +67,6 @@ namespace gfx{
//attempt binding to given gl_buffer_manager. returns true on success //attempt binding to given gl_buffer_manager. returns true on success
bool bind(gl_buffer_manager& b)const; bool bind(gl_buffer_manager& b)const;
//attempt binding to given gl_buffer_manager and locking that buffer. returns true on success
bool bind_lock(gl_buffer_manager& b)const;
//unlock associated gl_buffer_manager but don't unbind
void bind_unlock()const;
//unbind and unlock associated gl_buffer_manager
void unbind()const;
//add data to the end of this buffer (starts loading at data+get_size()) //add data to the end of this buffer (starts loading at data+get_size())
bool buffer(const void* data, size_t datasize); bool buffer(const void* data, size_t datasize);
//change capacity of buffer //change capacity of buffer
@ -84,7 +75,7 @@ namespace gfx{
GLuint raw()const; GLuint raw()const;
//glMapBuffer to given target with map style 'm' //glMapBuffer to given target with map style 'm'
scoped_vbo_map<void> map(gl_buffer_manager& target, maptype m)const; scoped_vbo_map<void> map(maptype m)const;
//emties the buffer. does not clear contents, just sets size to 0. //emties the buffer. does not clear contents, just sets size to 0.
void clear(); void clear();
@ -92,9 +83,6 @@ namespace gfx{
size_t get_cap()const; size_t get_cap()const;
usage get_usage()const; usage get_usage()const;
//check the currently bound buffer target
gl_buffer_manager* get_bound_buffer()const;
GLenum get_bound_id()const;
private: private:
static bool copy_buffer(vbo& dest, const vbo& src); static bool copy_buffer(vbo& dest, const vbo& src);
}; };
@ -114,12 +102,11 @@ namespace gfx{
private: private:
pointer m_data = nullptr; //pointer to the mapped region pointer m_data = nullptr; //pointer to the mapped region
scoped_buffer_bind<buffer_accessor> m_bind; GLuint m_buffer;
public: public:
//create a mapping for 'v' on 'targ' with map style 'm' //create a mapping for 'v' on 'targ' with map style 'm'
explicit scoped_vbo_map(const buffer_accessor& ba, gl_buffer_manager& bm, GLuint bid, vbo::maptype m); explicit scoped_vbo_map(GLuint bid, vbo::maptype m);
scoped_vbo_map(const scoped_vbo_map&) = delete; scoped_vbo_map(const scoped_vbo_map&) = delete;
scoped_vbo_map(scoped_vbo_map&&); scoped_vbo_map(scoped_vbo_map&&);
~scoped_vbo_map(); ~scoped_vbo_map();
@ -161,10 +148,10 @@ namespace gfx{
private: private:
pointer m_data = nullptr; pointer m_data = nullptr;
scoped_buffer_bind<buffer_accessor> m_bind; GLuint m_buffer;
public: public:
explicit scoped_vbo_map(const buffer_accessor& ba, gl_buffer_manager& bm, GLuint bid, vbo::maptype m); explicit scoped_vbo_map(GLuint bid, vbo::maptype m);
scoped_vbo_map(const scoped_vbo_map&) = delete; scoped_vbo_map(const scoped_vbo_map&) = delete;
scoped_vbo_map(scoped_vbo_map&&); scoped_vbo_map(scoped_vbo_map&&);
~scoped_vbo_map(); ~scoped_vbo_map();
@ -184,37 +171,36 @@ namespace gfx{
}; };
template<typename T> template<typename T>
scoped_vbo_map<T>::scoped_vbo_map(const buffer_accessor& ba, gl_buffer_manager& bm, GLuint bid, vbo::maptype m): scoped_vbo_map<T>::scoped_vbo_map(GLuint bid, vbo::maptype m):
m_bind(ba, bm, bid) m_buffer(bid)
{ {
if(!m_bind.valid()) m_data = reinterpret_cast<pointer>(glMapNamedBuffer(m_buffer, static_cast<GLenum>(m)));
return;
m_data = reinterpret_cast<pointer>(glMapBuffer(m_bind.get_bound_id(), static_cast<GLenum>(m)));
} }
template<typename T> template<typename T>
scoped_vbo_map<T>::scoped_vbo_map(scoped_vbo_map&& m): scoped_vbo_map<T>::scoped_vbo_map(scoped_vbo_map&& m):
m_data(std::exchange(m.m_data, nullptr)), m_data(std::exchange(m.m_data, nullptr)),
m_bind(std::move(m.m_bind)){} m_buffer(std::exchange(m.m_buffer, 0)){}
template<typename T> template<typename T>
scoped_vbo_map<T>::~scoped_vbo_map(){ scoped_vbo_map<T>::~scoped_vbo_map(){
if(m_data){ if(m_data){
glUnmapBuffer(m_bind.get_bound_id()); glUnmapNamedBuffer(m_buffer);
} }
} }
template<typename T> template<typename T>
scoped_vbo_map<T>& scoped_vbo_map<T>::operator=(scoped_vbo_map&& m){ scoped_vbo_map<T>& scoped_vbo_map<T>::operator=(scoped_vbo_map&& m){
std::swap(m_data, m.m_data); std::swap(m_data, m.m_data);
m_bind = std::move(m.m_bind); std::swap(m_buffer, m.m_buffer);
return *this; return *this;
} }
template<typename T> template<typename T>
auto scoped_vbo_map<T>::length()const -> size_type{ auto scoped_vbo_map<T>::length()const -> size_type{
GLint64 retval; GLint64 retval;
glGetBufferParameteri64v(m_bind.get_bound_id(), GL_BUFFER_MAP_LENGTH, &retval); glGetNamedBufferParameteri64v(m_buffer, GL_BUFFER_MAP_LENGTH, &retval);
return retval; return retval;
} }
template<typename T> template<typename T>
auto scoped_vbo_map<T>::release() -> pointer{ auto scoped_vbo_map<T>::release() -> pointer{
m_buffer = 0;
return std::exchange(m_data, nullptr); return std::exchange(m_data, nullptr);
} }
template<typename T> template<typename T>

View File

@ -23,14 +23,10 @@
namespace gfx{ namespace gfx{
fbo::fbo(){ fbo::fbo(){
glGenFramebuffers(1, &m_buffer); glCreateFramebuffers(1, &m_buffer);
m_draw.set_buffer_id(m_buffer);
m_read.set_buffer_id(m_buffer);
} }
fbo::fbo(fbo&& f): fbo::fbo(fbo&& f):
m_buffer(std::exchange(f.m_buffer, 0)), m_buffer(std::exchange(f.m_buffer, 0)){}
m_draw(std::move(f.m_draw)),
m_read(std::move(f.m_read)){}
fbo::~fbo(){ fbo::~fbo(){
if(m_buffer) if(m_buffer)
glDeleteFramebuffers(1, &m_buffer); glDeleteFramebuffers(1, &m_buffer);
@ -46,72 +42,25 @@ namespace gfx{
} }
bool fbo::detach(GLenum point){ bool fbo::detach(GLenum point){
scoped_buffer_bind draw_bind(m_draw, buffer::fb_draw); glNamedFramebufferRenderbuffer(m_buffer, point, GL_RENDERBUFFER, 0);
if(!draw_bind.valid())
return false;
glFramebufferRenderbuffer(m_draw.get_bound_id(), point, GL_RENDERBUFFER, 0);
return true; return true;
} }
bool fbo::attach(const texture& tex, GLenum point){ bool fbo::attach(const texture& tex, GLenum point){
scoped_buffer_bind draw_bind(m_draw, buffer::fb_draw); glNamedFramebufferTexture(m_buffer, point, tex.raw(), 0);
if(!draw_bind.valid())
return false;
tex.bind();
glFramebufferTexture2D(m_draw.get_bound_id(), point, GL_TEXTURE_2D, tex.raw(), 0);
return true; return true;
} }
bool fbo::attach(const rbo& r, GLenum point){ bool fbo::attach(const rbo& r, GLenum point){
scoped_buffer_bind draw_bind(m_draw, buffer::fb_draw); glNamedFramebufferRenderbuffer(m_buffer, point, GL_RENDERBUFFER, r.raw());
if(!draw_bind.valid())
return false;
r.bind();
glFramebufferRenderbuffer(m_draw.get_bound_id(), point, GL_RENDERBUFFER, r.raw());
return true; return true;
} }
bool fbo::bind()const{ bool fbo::bind()const{
return (bind(buffer::fb_draw) && bind(buffer::fb_read)); glBindFramebuffer(GL_FRAMEBUFFER, m_buffer);
return true;
} }
bool fbo::bind(gl_frame_buffer_manager& b)const{ bool fbo::bind(gl_frame_buffer_manager& b)const{
if(GL_DRAW_FRAMEBUFFER == b.get_buffer_id()){ glBindFramebuffer(b.get_buffer_id(), m_buffer);
if(!m_draw.bind(b))
return false;
}else{
if(!m_read.bind(b))
return false;
}
return true; return true;
} }
bool fbo::bind_lock(gl_frame_buffer_manager& b)const{
if(GL_DRAW_FRAMEBUFFER == b.get_buffer_id()){
if(!m_draw.bind_lock(b))
return false;
}else{
if(!m_read.bind_lock(b))
return false;
}
return true;
}
void fbo::bind_unlock(gl_frame_buffer_manager& b)const{
if(GL_DRAW_FRAMEBUFFER == b.get_buffer_id()){
m_draw.unlock();
}else{
m_read.unlock();
}
}
void fbo::unbind()const{
m_draw.unbind();
m_read.unbind();
}
GLenum fbo::get_bound_id()const{
if(m_read.get_bound_buffer() && m_draw.get_bound_buffer())
return GL_FRAMEBUFFER;
if(m_read.get_bound_buffer())
return m_read.get_bound_id();
if(m_draw.get_bound_buffer())
return m_draw.get_bound_id();
return 0;
}
} }

View File

@ -425,20 +425,16 @@ namespace gfx{
set_uniform(get_uniform_loc(name), f1, f2, f3, f4); set_uniform(get_uniform_loc(name), f1, f2, f3, f4);
} }
void shader_program::set_uniform(GLuint loc, GLfloat f){ void shader_program::set_uniform(GLuint loc, GLfloat f){
use(); glProgramUniform1f(m_shader_id, loc, f);
glUniform1f(loc, f);
} }
void shader_program::set_uniform(GLuint loc, GLfloat f1, GLfloat f2){ void shader_program::set_uniform(GLuint loc, GLfloat f1, GLfloat f2){
use(); glProgramUniform2f(m_shader_id, loc, f1, f2);
glUniform2f(loc, f1, f2);
} }
void shader_program::set_uniform(GLuint loc, GLfloat f1, GLfloat f2, GLfloat f3){ void shader_program::set_uniform(GLuint loc, GLfloat f1, GLfloat f2, GLfloat f3){
use(); glProgramUniform3f(m_shader_id, loc, f1, f2, f3);
glUniform3f(loc, f1, f2, f3);
} }
void shader_program::set_uniform(GLuint loc, GLfloat f1, GLfloat f2, GLfloat f3, GLfloat f4){ void shader_program::set_uniform(GLuint loc, GLfloat f1, GLfloat f2, GLfloat f3, GLfloat f4){
use(); glProgramUniform4f(m_shader_id, loc, f1, f2, f3, f4);
glUniform4f(loc, f1, f2, f3, f4);
} }
void shader_program::set_uniform(const char* name, GLint i){ void shader_program::set_uniform(const char* name, GLint i){
@ -454,20 +450,16 @@ namespace gfx{
set_uniform(get_uniform_loc(name), i1, i2, i3, i4); set_uniform(get_uniform_loc(name), i1, i2, i3, i4);
} }
void shader_program::set_uniform(GLuint loc, GLint i){ void shader_program::set_uniform(GLuint loc, GLint i){
use(); glProgramUniform1i(m_shader_id, loc, i);
glUniform1i(loc, i);
} }
void shader_program::set_uniform(GLuint loc, GLint i1, GLint i2){ void shader_program::set_uniform(GLuint loc, GLint i1, GLint i2){
use(); glProgramUniform2i(m_shader_id, loc, i1, i2);
glUniform2i(loc, i1, i2);
} }
void shader_program::set_uniform(GLuint loc, GLint i1, GLint i2, GLint i3){ void shader_program::set_uniform(GLuint loc, GLint i1, GLint i2, GLint i3){
use(); glProgramUniform3i(m_shader_id, loc, i1, i2, i3);
glUniform3i(loc, i1, i2, i3);
} }
void shader_program::set_uniform(GLuint loc, GLint i1, GLint i2, GLint i3, GLint i4){ void shader_program::set_uniform(GLuint loc, GLint i1, GLint i2, GLint i3, GLint i4){
use(); glProgramUniform4i(m_shader_id, loc, i1, i2, i3, i4);
glUniform4i(loc, i1, i2, i3, i4);
} }
void shader_program::set_uniform(const char* name, GLuint i){ void shader_program::set_uniform(const char* name, GLuint i){
@ -483,28 +475,24 @@ namespace gfx{
set_uniform(get_uniform_loc(name), i1, i2, i3, i4); set_uniform(get_uniform_loc(name), i1, i2, i3, i4);
} }
void shader_program::set_uniform(GLuint loc, GLuint i){ void shader_program::set_uniform(GLuint loc, GLuint i){
use(); glProgramUniform1ui(m_shader_id, loc, i);
glUniform1ui(loc, i);
} }
void shader_program::set_uniform(GLuint loc, GLuint i1, GLuint i2){ void shader_program::set_uniform(GLuint loc, GLuint i1, GLuint i2){
use(); glProgramUniform2ui(m_shader_id, loc, i1, i2);
glUniform2ui(loc, i1, i2);
} }
void shader_program::set_uniform(GLuint loc, GLuint i1, GLuint i2, GLuint i3){ void shader_program::set_uniform(GLuint loc, GLuint i1, GLuint i2, GLuint i3){
use(); glProgramUniform3ui(m_shader_id, loc, i1, i2, i3);
glUniform3ui(loc, i1, i2, i3);
} }
void shader_program::set_uniform(GLuint loc, GLuint i1, GLuint i2, GLuint i3, GLuint i4){ void shader_program::set_uniform(GLuint loc, GLuint i1, GLuint i2, GLuint i3, GLuint i4){
use(); glProgramUniform4ui(m_shader_id, loc, i1, i2, i3, i4);
glUniform4ui(loc, i1, i2, i3, i4);
} }
void shader_program::set_uniform(const char* name, const texture& t, GLuint tex_unit){ void shader_program::set_uniform(const char* name, const texture& t, GLuint tex_unit){
set_uniform(get_uniform_loc(name), t, tex_unit); set_uniform(get_uniform_loc(name), t, tex_unit);
} }
void shader_program::set_uniform(GLuint loc, const texture& t, GLuint tex_unit){ void shader_program::set_uniform(GLuint loc, const texture& t, GLuint tex_unit){
use(); t.bind_unit(tex_unit);
t.use(tex_unit, loc); glProgramUniform1i(m_shader_id, loc, tex_unit);
} }
void shader_program::set_uniform(const char* name, const vec2<GLfloat>& v){ void shader_program::set_uniform(const char* name, const vec2<GLfloat>& v){
@ -517,41 +505,36 @@ namespace gfx{
set_uniform(get_uniform_loc(name), v); set_uniform(get_uniform_loc(name), v);
} }
void shader_program::set_uniform(GLuint loc, const vec2<GLfloat>& v){ void shader_program::set_uniform(GLuint loc, const vec2<GLfloat>& v){
use(); glProgramUniform2fv(m_shader_id, loc, 1, v);
glUniform2fv(loc, 1, v);
} }
void shader_program::set_uniform(GLuint loc, const vec3<GLfloat>& v){ void shader_program::set_uniform(GLuint loc, const vec3<GLfloat>& v){
use(); glProgramUniform3fv(m_shader_id, loc, 1, v);
glUniform3fv(loc, 1, v);
} }
void shader_program::set_uniform(GLuint loc, const vec4<GLfloat>& v){ void shader_program::set_uniform(GLuint loc, const vec4<GLfloat>& v){
use(); glProgramUniform4fv(m_shader_id, loc, 1, v);
glUniform4fv(loc, 1, v);
} }
void shader_program::set_uniform(const char* name, GLsizei count, const vec2<GLfloat>* v_arr){ void shader_program::set_uniform(const char* name, GLsizei count, const vec2<GLfloat>* v_arr){
set_uniform(get_uniform_loc(name), count, v_arr); set_uniform(get_uniform_loc(name), count, v_arr);
} }
void shader_program::set_uniform(GLuint loc, GLsizei count, const vec2<GLfloat>* v_arr){ void shader_program::set_uniform(GLuint loc, GLsizei count, const vec2<GLfloat>* v_arr){
use();
if constexpr(sizeof(v_arr[0]) == sizeof(GLfloat[2])){ if constexpr(sizeof(v_arr[0]) == sizeof(GLfloat[2])){
glUniform2fv(loc, count, reinterpret_cast<const GLfloat*>(v_arr)); glProgramUniform2fv(m_shader_id, loc, count, reinterpret_cast<const GLfloat*>(v_arr));
}else{ }else{
std::unique_ptr<GLfloat[]> arr(new GLfloat[count * 2]); std::unique_ptr<GLfloat[]> arr(new GLfloat[count * 2]);
for(GLsizei i = 0; i < count; ++i){ for(GLsizei i = 0; i < count; ++i){
arr[i] = v_arr[i].get(0); arr[i] = v_arr[i].get(0);
arr[i + 1] = v_arr[i].get(1); arr[i + 1] = v_arr[i].get(1);
} }
glUniform2fv(loc, count, arr.get()); glProgramUniform2fv(m_shader_id, loc, count, arr.get());
} }
} }
void shader_program::set_uniform(const char* name, GLsizei count, const vec3<GLfloat>* v_arr){ void shader_program::set_uniform(const char* name, GLsizei count, const vec3<GLfloat>* v_arr){
set_uniform(get_uniform_loc(name), count, v_arr); set_uniform(get_uniform_loc(name), count, v_arr);
} }
void shader_program::set_uniform(GLuint loc, GLsizei count, const vec3<GLfloat>* v_arr){ void shader_program::set_uniform(GLuint loc, GLsizei count, const vec3<GLfloat>* v_arr){
use();
if constexpr(sizeof(v_arr[0]) == sizeof(GLfloat[3])){ if constexpr(sizeof(v_arr[0]) == sizeof(GLfloat[3])){
glUniform3fv(loc, count, reinterpret_cast<const GLfloat*>(v_arr)); glProgramUniform3fv(m_shader_id, loc, count, reinterpret_cast<const GLfloat*>(v_arr));
}else{ }else{
std::unique_ptr<GLfloat[]> arr(new GLfloat[count * 3]); std::unique_ptr<GLfloat[]> arr(new GLfloat[count * 3]);
for(GLsizei i = 0; i < count; ++i){ for(GLsizei i = 0; i < count; ++i){
@ -559,16 +542,15 @@ namespace gfx{
arr[i + 1] = v_arr[i].get(1); arr[i + 1] = v_arr[i].get(1);
arr[i + 2] = v_arr[i].get(2); arr[i + 2] = v_arr[i].get(2);
} }
glUniform3fv(loc, count, arr.get()); glProgramUniform3fv(m_shader_id, loc, count, arr.get());
} }
} }
void shader_program::set_uniform(const char* name, GLsizei count, const vec4<GLfloat>* v_arr){ void shader_program::set_uniform(const char* name, GLsizei count, const vec4<GLfloat>* v_arr){
set_uniform(get_uniform_loc(name), count, v_arr); set_uniform(get_uniform_loc(name), count, v_arr);
} }
void shader_program::set_uniform(GLuint loc, GLsizei count, const vec4<GLfloat>* v_arr){ void shader_program::set_uniform(GLuint loc, GLsizei count, const vec4<GLfloat>* v_arr){
use();
if constexpr(sizeof(v_arr[0]) == sizeof(GLfloat[4])){ if constexpr(sizeof(v_arr[0]) == sizeof(GLfloat[4])){
glUniform4fv(loc, count, reinterpret_cast<const GLfloat*>(v_arr)); glProgramUniform4fv(m_shader_id, loc, count, reinterpret_cast<const GLfloat*>(v_arr));
}else{ }else{
std::unique_ptr<GLfloat[]> arr(new GLfloat[count * 4]); std::unique_ptr<GLfloat[]> arr(new GLfloat[count * 4]);
for(GLsizei i = 0; i < count; ++i){ for(GLsizei i = 0; i < count; ++i){
@ -577,7 +559,7 @@ namespace gfx{
arr[i + 2] = v_arr[i].get(2); arr[i + 2] = v_arr[i].get(2);
arr[i + 3] = v_arr[i].get(3); arr[i + 3] = v_arr[i].get(3);
} }
glUniform4fv(loc, count, arr.get()); glProgramUniform4fv(m_shader_id, loc, count, arr.get());
} }
} }
@ -594,20 +576,16 @@ namespace gfx{
set_uniform_4(get_uniform_loc(name), count, v_arr); set_uniform_4(get_uniform_loc(name), count, v_arr);
} }
void shader_program::set_uniform_1(GLuint loc, GLsizei count, const GLfloat* v_arr){ void shader_program::set_uniform_1(GLuint loc, GLsizei count, const GLfloat* v_arr){
use(); glProgramUniform1fv(m_shader_id, loc, count, v_arr);
glUniform1fv(loc, count, v_arr);
} }
void shader_program::set_uniform_2(GLuint loc, GLsizei count, const GLfloat* v_arr){ void shader_program::set_uniform_2(GLuint loc, GLsizei count, const GLfloat* v_arr){
use(); glProgramUniform2fv(m_shader_id, loc, count, v_arr);
glUniform2fv(loc, count, v_arr);
} }
void shader_program::set_uniform_3(GLuint loc, GLsizei count, const GLfloat* v_arr){ void shader_program::set_uniform_3(GLuint loc, GLsizei count, const GLfloat* v_arr){
use(); glProgramUniform3fv(m_shader_id, loc, count, v_arr);
glUniform3fv(loc, count, v_arr);
} }
void shader_program::set_uniform_4(GLuint loc, GLsizei count, const GLfloat* v_arr){ void shader_program::set_uniform_4(GLuint loc, GLsizei count, const GLfloat* v_arr){
use(); glProgramUniform4fv(m_shader_id, loc, count, v_arr);
glUniform4fv(loc, count, v_arr);
} }
void shader_program::set_uniform(const char* name, const vec2<GLint>& v){ void shader_program::set_uniform(const char* name, const vec2<GLint>& v){
@ -620,41 +598,36 @@ namespace gfx{
set_uniform(get_uniform_loc(name), v); set_uniform(get_uniform_loc(name), v);
} }
void shader_program::set_uniform(GLuint loc, const vec2<GLint>& v){ void shader_program::set_uniform(GLuint loc, const vec2<GLint>& v){
use(); glProgramUniform2iv(m_shader_id, loc, 1, v);
glUniform2iv(loc, 1, v);
} }
void shader_program::set_uniform(GLuint loc, const vec3<GLint>& v){ void shader_program::set_uniform(GLuint loc, const vec3<GLint>& v){
use(); glProgramUniform3iv(m_shader_id, loc, 1, v);
glUniform3iv(loc, 1, v);
} }
void shader_program::set_uniform(GLuint loc, const vec4<GLint>& v){ void shader_program::set_uniform(GLuint loc, const vec4<GLint>& v){
use(); glProgramUniform4iv(m_shader_id, loc, 1, v);
glUniform4iv(loc, 1, v);
} }
void shader_program::set_uniform(const char* name, GLsizei count, const vec2<GLint>* v_arr){ void shader_program::set_uniform(const char* name, GLsizei count, const vec2<GLint>* v_arr){
set_uniform(get_uniform_loc(name), count, v_arr); set_uniform(get_uniform_loc(name), count, v_arr);
} }
void shader_program::set_uniform(GLuint loc, GLsizei count, const vec2<GLint>* v_arr){ void shader_program::set_uniform(GLuint loc, GLsizei count, const vec2<GLint>* v_arr){
use();
if constexpr(sizeof(v_arr[0]) == sizeof(GLint[2])){ if constexpr(sizeof(v_arr[0]) == sizeof(GLint[2])){
glUniform2iv(loc, count, reinterpret_cast<const GLint*>(v_arr)); glProgramUniform2iv(m_shader_id, loc, count, reinterpret_cast<const GLint*>(v_arr));
}else{ }else{
std::unique_ptr<GLint[]> arr(new GLint[count * 2]); std::unique_ptr<GLint[]> arr(new GLint[count * 2]);
for(GLsizei i = 0; i < count; ++i){ for(GLsizei i = 0; i < count; ++i){
arr[i] = v_arr[i].get(0); arr[i] = v_arr[i].get(0);
arr[i + 1] = v_arr[i].get(1); arr[i + 1] = v_arr[i].get(1);
} }
glUniform2iv(loc, count, arr.get()); glProgramUniform2iv(m_shader_id, loc, count, arr.get());
} }
} }
void shader_program::set_uniform(const char* name, GLsizei count, const vec3<GLint>* v_arr){ void shader_program::set_uniform(const char* name, GLsizei count, const vec3<GLint>* v_arr){
set_uniform(get_uniform_loc(name), count, v_arr); set_uniform(get_uniform_loc(name), count, v_arr);
} }
void shader_program::set_uniform(GLuint loc, GLsizei count, const vec3<GLint>* v_arr){ void shader_program::set_uniform(GLuint loc, GLsizei count, const vec3<GLint>* v_arr){
use();
if constexpr(sizeof(v_arr[0]) == sizeof(GLint[3])){ if constexpr(sizeof(v_arr[0]) == sizeof(GLint[3])){
glUniform3iv(loc, count, reinterpret_cast<const GLint*>(v_arr)); glProgramUniform3iv(m_shader_id, loc, count, reinterpret_cast<const GLint*>(v_arr));
}else{ }else{
std::unique_ptr<GLint[]> arr(new GLint[count * 3]); std::unique_ptr<GLint[]> arr(new GLint[count * 3]);
for(GLsizei i = 0; i < count; ++i){ for(GLsizei i = 0; i < count; ++i){
@ -662,16 +635,15 @@ namespace gfx{
arr[i + 1] = v_arr[i].get(1); arr[i + 1] = v_arr[i].get(1);
arr[i + 2] = v_arr[i].get(2); arr[i + 2] = v_arr[i].get(2);
} }
glUniform3iv(loc, count, arr.get()); glProgramUniform3iv(m_shader_id, loc, count, arr.get());
} }
} }
void shader_program::set_uniform(const char* name, GLsizei count, const vec4<GLint>* v_arr){ void shader_program::set_uniform(const char* name, GLsizei count, const vec4<GLint>* v_arr){
set_uniform(get_uniform_loc(name), count, v_arr); set_uniform(get_uniform_loc(name), count, v_arr);
} }
void shader_program::set_uniform(GLuint loc, GLsizei count, const vec4<GLint>* v_arr){ void shader_program::set_uniform(GLuint loc, GLsizei count, const vec4<GLint>* v_arr){
use();
if constexpr(sizeof(v_arr[0]) == sizeof(GLint[4])){ if constexpr(sizeof(v_arr[0]) == sizeof(GLint[4])){
glUniform4iv(loc, count, reinterpret_cast<const GLint*>(v_arr)); glProgramUniform4iv(m_shader_id, loc, count, reinterpret_cast<const GLint*>(v_arr));
}else{ }else{
std::unique_ptr<GLint[]> arr(new GLint[count * 4]); std::unique_ptr<GLint[]> arr(new GLint[count * 4]);
for(GLsizei i = 0; i < count; ++i){ for(GLsizei i = 0; i < count; ++i){
@ -680,7 +652,7 @@ namespace gfx{
arr[i + 2] = v_arr[i].get(2); arr[i + 2] = v_arr[i].get(2);
arr[i + 3] = v_arr[i].get(3); arr[i + 3] = v_arr[i].get(3);
} }
glUniform4iv(loc, count, arr.get()); glProgramUniform4iv(m_shader_id, loc, count, arr.get());
} }
} }
@ -697,20 +669,16 @@ namespace gfx{
set_uniform_4(get_uniform_loc(name), count, v_arr); set_uniform_4(get_uniform_loc(name), count, v_arr);
} }
void shader_program::set_uniform_1(GLuint loc, GLsizei count, const GLint* v_arr){ void shader_program::set_uniform_1(GLuint loc, GLsizei count, const GLint* v_arr){
use(); glProgramUniform1iv(m_shader_id, loc, count, v_arr);
glUniform1iv(loc, count, v_arr);
} }
void shader_program::set_uniform_2(GLuint loc, GLsizei count, const GLint* v_arr){ void shader_program::set_uniform_2(GLuint loc, GLsizei count, const GLint* v_arr){
use(); glProgramUniform2iv(m_shader_id, loc, count, v_arr);
glUniform2iv(loc, count, v_arr);
} }
void shader_program::set_uniform_3(GLuint loc, GLsizei count, const GLint* v_arr){ void shader_program::set_uniform_3(GLuint loc, GLsizei count, const GLint* v_arr){
use(); glProgramUniform3iv(m_shader_id, loc, count, v_arr);
glUniform3iv(loc, count, v_arr);
} }
void shader_program::set_uniform_4(GLuint loc, GLsizei count, const GLint* v_arr){ void shader_program::set_uniform_4(GLuint loc, GLsizei count, const GLint* v_arr){
use(); glProgramUniform4iv(m_shader_id, loc, count, v_arr);
glUniform4iv(loc, count, v_arr);
} }
void shader_program::set_uniform(const char* name, const vec2<GLuint>& v){ void shader_program::set_uniform(const char* name, const vec2<GLuint>& v){
@ -723,41 +691,36 @@ namespace gfx{
set_uniform(get_uniform_loc(name), v); set_uniform(get_uniform_loc(name), v);
} }
void shader_program::set_uniform(GLuint loc, const vec2<GLuint>& v){ void shader_program::set_uniform(GLuint loc, const vec2<GLuint>& v){
use(); glProgramUniform2uiv(m_shader_id, loc, 1, v);
glUniform2uiv(loc, 1, v);
} }
void shader_program::set_uniform(GLuint loc, const vec3<GLuint>& v){ void shader_program::set_uniform(GLuint loc, const vec3<GLuint>& v){
use(); glProgramUniform3uiv(m_shader_id, loc, 1, v);
glUniform3uiv(loc, 1, v);
} }
void shader_program::set_uniform(GLuint loc, const vec4<GLuint>& v){ void shader_program::set_uniform(GLuint loc, const vec4<GLuint>& v){
use(); glProgramUniform4uiv(m_shader_id, loc, 1, v);
glUniform4uiv(loc, 1, v);
} }
void shader_program::set_uniform(const char* name, GLsizei count, const vec2<GLuint>* v_arr){ void shader_program::set_uniform(const char* name, GLsizei count, const vec2<GLuint>* v_arr){
set_uniform(get_uniform_loc(name), count, v_arr); set_uniform(get_uniform_loc(name), count, v_arr);
} }
void shader_program::set_uniform(GLuint loc, GLsizei count, const vec2<GLuint>* v_arr){ void shader_program::set_uniform(GLuint loc, GLsizei count, const vec2<GLuint>* v_arr){
use();
if constexpr(sizeof(v_arr[0]) == sizeof(GLuint[2])){ if constexpr(sizeof(v_arr[0]) == sizeof(GLuint[2])){
glUniform2uiv(loc, count, reinterpret_cast<const GLuint*>(v_arr)); glProgramUniform2uiv(m_shader_id, loc, count, reinterpret_cast<const GLuint*>(v_arr));
}else{ }else{
std::unique_ptr<GLuint[]> arr(new GLuint[count * 2]); std::unique_ptr<GLuint[]> arr(new GLuint[count * 2]);
for(GLsizei i = 0; i < count; ++i){ for(GLsizei i = 0; i < count; ++i){
arr[i] = v_arr[i].get(0); arr[i] = v_arr[i].get(0);
arr[i + 1] = v_arr[i].get(1); arr[i + 1] = v_arr[i].get(1);
} }
glUniform2uiv(loc, count, arr.get()); glProgramUniform2uiv(m_shader_id, loc, count, arr.get());
} }
} }
void shader_program::set_uniform(const char* name, GLsizei count, const vec3<GLuint>* v_arr){ void shader_program::set_uniform(const char* name, GLsizei count, const vec3<GLuint>* v_arr){
set_uniform(get_uniform_loc(name), count, v_arr); set_uniform(get_uniform_loc(name), count, v_arr);
} }
void shader_program::set_uniform(GLuint loc, GLsizei count, const vec3<GLuint>* v_arr){ void shader_program::set_uniform(GLuint loc, GLsizei count, const vec3<GLuint>* v_arr){
use();
if constexpr(sizeof(v_arr[0]) == sizeof(GLuint[3])){ if constexpr(sizeof(v_arr[0]) == sizeof(GLuint[3])){
glUniform3uiv(loc, count, reinterpret_cast<const GLuint*>(v_arr)); glProgramUniform3uiv(m_shader_id, loc, count, reinterpret_cast<const GLuint*>(v_arr));
}else{ }else{
std::unique_ptr<GLuint[]> arr(new GLuint[count * 3]); std::unique_ptr<GLuint[]> arr(new GLuint[count * 3]);
for(GLsizei i = 0; i < count; ++i){ for(GLsizei i = 0; i < count; ++i){
@ -765,16 +728,15 @@ namespace gfx{
arr[i + 1] = v_arr[i].get(1); arr[i + 1] = v_arr[i].get(1);
arr[i + 2] = v_arr[i].get(2); arr[i + 2] = v_arr[i].get(2);
} }
glUniform3uiv(loc, count, arr.get()); glProgramUniform3uiv(m_shader_id, loc, count, arr.get());
} }
} }
void shader_program::set_uniform(const char* name, GLsizei count, const vec4<GLuint>* v_arr){ void shader_program::set_uniform(const char* name, GLsizei count, const vec4<GLuint>* v_arr){
set_uniform(get_uniform_loc(name), count, v_arr); set_uniform(get_uniform_loc(name), count, v_arr);
} }
void shader_program::set_uniform(GLuint loc, GLsizei count, const vec4<GLuint>* v_arr){ void shader_program::set_uniform(GLuint loc, GLsizei count, const vec4<GLuint>* v_arr){
use();
if constexpr(sizeof(v_arr[0]) == sizeof(GLuint[4])){ if constexpr(sizeof(v_arr[0]) == sizeof(GLuint[4])){
glUniform4uiv(loc, count, reinterpret_cast<const GLuint*>(v_arr)); glProgramUniform4uiv(m_shader_id, loc, count, reinterpret_cast<const GLuint*>(v_arr));
}else{ }else{
std::unique_ptr<GLuint[]> arr(new GLuint[count * 4]); std::unique_ptr<GLuint[]> arr(new GLuint[count * 4]);
for(GLsizei i = 0; i < count; ++i){ for(GLsizei i = 0; i < count; ++i){
@ -783,7 +745,7 @@ namespace gfx{
arr[i + 2] = v_arr[i].get(2); arr[i + 2] = v_arr[i].get(2);
arr[i + 3] = v_arr[i].get(3); arr[i + 3] = v_arr[i].get(3);
} }
glUniform4uiv(loc, count, arr.get()); glProgramUniform4uiv(m_shader_id, loc, count, arr.get());
} }
} }
@ -800,20 +762,16 @@ namespace gfx{
set_uniform_4(get_uniform_loc(name), count, v_arr); set_uniform_4(get_uniform_loc(name), count, v_arr);
} }
void shader_program::set_uniform_1(GLuint loc, GLsizei count, const GLuint* v_arr){ void shader_program::set_uniform_1(GLuint loc, GLsizei count, const GLuint* v_arr){
use(); glProgramUniform1uiv(m_shader_id, loc, count, v_arr);
glUniform1uiv(loc, count, v_arr);
} }
void shader_program::set_uniform_2(GLuint loc, GLsizei count, const GLuint* v_arr){ void shader_program::set_uniform_2(GLuint loc, GLsizei count, const GLuint* v_arr){
use(); glProgramUniform2uiv(m_shader_id, loc, count, v_arr);
glUniform2uiv(loc, count, v_arr);
} }
void shader_program::set_uniform_3(GLuint loc, GLsizei count, const GLuint* v_arr){ void shader_program::set_uniform_3(GLuint loc, GLsizei count, const GLuint* v_arr){
use(); glProgramUniform3uiv(m_shader_id, loc, count, v_arr);
glUniform3uiv(loc, count, v_arr);
} }
void shader_program::set_uniform_4(GLuint loc, GLsizei count, const GLuint* v_arr){ void shader_program::set_uniform_4(GLuint loc, GLsizei count, const GLuint* v_arr){
use(); glProgramUniform4uiv(m_shader_id, loc, count, v_arr);
glUniform4uiv(loc, count, v_arr);
} }
void shader_program::set_uniform(const char* name, const mat2<GLfloat>& m){ void shader_program::set_uniform(const char* name, const mat2<GLfloat>& m){
@ -826,25 +784,21 @@ namespace gfx{
set_uniform(get_uniform_loc(name), m); set_uniform(get_uniform_loc(name), m);
} }
void shader_program::set_uniform(GLuint loc, const mat2<GLfloat>& m){ void shader_program::set_uniform(GLuint loc, const mat2<GLfloat>& m){
use(); glProgramUniformMatrix2fv(m_shader_id, loc, 1, GL_FALSE, m);
glUniformMatrix2fv(loc, 1, GL_FALSE, m);
} }
void shader_program::set_uniform(GLuint loc, const mat3<GLfloat>& m){ void shader_program::set_uniform(GLuint loc, const mat3<GLfloat>& m){
use(); glProgramUniformMatrix3fv(m_shader_id, loc, 1, GL_FALSE, m);
glUniformMatrix3fv(loc, 1, GL_FALSE, m);
} }
void shader_program::set_uniform(GLuint loc, const mat4<GLfloat>& m){ void shader_program::set_uniform(GLuint loc, const mat4<GLfloat>& m){
use(); glProgramUniformMatrix4fv(m_shader_id, loc, 1, GL_FALSE, m);
glUniformMatrix4fv(loc, 1, GL_FALSE, m);
} }
void shader_program::set_uniform(const char* name, GLsizei count, const mat2<GLfloat>* m_arr){ void shader_program::set_uniform(const char* name, GLsizei count, const mat2<GLfloat>* m_arr){
set_uniform(get_uniform_loc(name), count, m_arr); set_uniform(get_uniform_loc(name), count, m_arr);
} }
void shader_program::set_uniform(GLuint loc, GLsizei count, const mat2<GLfloat>* m_arr){ void shader_program::set_uniform(GLuint loc, GLsizei count, const mat2<GLfloat>* m_arr){
use();
if constexpr(sizeof(m_arr[0]) == sizeof(GLfloat[4])){ if constexpr(sizeof(m_arr[0]) == sizeof(GLfloat[4])){
glUniformMatrix2fv(loc, count, GL_FALSE, reinterpret_cast<const GLfloat*>(m_arr)); glProgramUniformMatrix2fv(m_shader_id, loc, count, GL_FALSE, reinterpret_cast<const GLfloat*>(m_arr));
}else{ }else{
std::unique_ptr<GLfloat[]> arr(new GLfloat[count * 4]); std::unique_ptr<GLfloat[]> arr(new GLfloat[count * 4]);
for(GLsizei i = 0; i < count; ++i){ for(GLsizei i = 0; i < count; ++i){
@ -853,16 +807,15 @@ namespace gfx{
arr[i + 2] = m_arr[i].get(2); arr[i + 2] = m_arr[i].get(2);
arr[i + 3] = m_arr[i].get(3); arr[i + 3] = m_arr[i].get(3);
} }
glUniformMatrix2fv(loc, count, GL_FALSE, arr.get()); glProgramUniformMatrix2fv(m_shader_id, loc, count, GL_FALSE, arr.get());
} }
} }
void shader_program::set_uniform(const char* name, GLsizei count, const mat3<GLfloat>* m_arr){ void shader_program::set_uniform(const char* name, GLsizei count, const mat3<GLfloat>* m_arr){
set_uniform(get_uniform_loc(name), count, m_arr); set_uniform(get_uniform_loc(name), count, m_arr);
} }
void shader_program::set_uniform(GLuint loc, GLsizei count, const mat3<GLfloat>* m_arr){ void shader_program::set_uniform(GLuint loc, GLsizei count, const mat3<GLfloat>* m_arr){
use();
if constexpr(sizeof(m_arr[0]) == sizeof(GLfloat[9])){ if constexpr(sizeof(m_arr[0]) == sizeof(GLfloat[9])){
glUniformMatrix3fv(loc, count, GL_FALSE, reinterpret_cast<const GLfloat*>(m_arr)); glProgramUniformMatrix3fv(m_shader_id, loc, count, GL_FALSE, reinterpret_cast<const GLfloat*>(m_arr));
}else{ }else{
std::unique_ptr<GLfloat[]> arr(new GLfloat[count * 9]); std::unique_ptr<GLfloat[]> arr(new GLfloat[count * 9]);
for(GLsizei i = 0; i < count; ++i){ for(GLsizei i = 0; i < count; ++i){
@ -876,16 +829,15 @@ namespace gfx{
arr[i + 7] = m_arr[i].get(7); arr[i + 7] = m_arr[i].get(7);
arr[i + 8] = m_arr[i].get(8); arr[i + 8] = m_arr[i].get(8);
} }
glUniformMatrix3fv(loc, count, GL_FALSE, arr.get()); glProgramUniformMatrix3fv(m_shader_id, loc, count, GL_FALSE, arr.get());
} }
} }
void shader_program::set_uniform(const char* name, GLsizei count, const mat4<GLfloat>* m_arr){ void shader_program::set_uniform(const char* name, GLsizei count, const mat4<GLfloat>* m_arr){
set_uniform(get_uniform_loc(name), count, m_arr); set_uniform(get_uniform_loc(name), count, m_arr);
} }
void shader_program::set_uniform(GLuint loc, GLsizei count, const mat4<GLfloat>* m_arr){ void shader_program::set_uniform(GLuint loc, GLsizei count, const mat4<GLfloat>* m_arr){
use();
if constexpr(sizeof(m_arr[0]) == sizeof(GLfloat[16])){ if constexpr(sizeof(m_arr[0]) == sizeof(GLfloat[16])){
glUniformMatrix4fv(loc, count, GL_FALSE, reinterpret_cast<const GLfloat*>(m_arr)); glProgramUniformMatrix4fv(m_shader_id, loc, count, GL_FALSE, reinterpret_cast<const GLfloat*>(m_arr));
}else{ }else{
std::unique_ptr<GLfloat[]> arr(new GLfloat[count * 16]); std::unique_ptr<GLfloat[]> arr(new GLfloat[count * 16]);
for(GLsizei i = 0; i < count; ++i){ for(GLsizei i = 0; i < count; ++i){
@ -906,7 +858,7 @@ namespace gfx{
arr[i + 14] = m_arr[i].get(14); arr[i + 14] = m_arr[i].get(14);
arr[i + 15] = m_arr[i].get(15); arr[i + 15] = m_arr[i].get(15);
} }
glUniformMatrix4fv(loc, count, GL_FALSE, arr.get()); glProgramUniformMatrix4fv(m_shader_id, loc, count, GL_FALSE, arr.get());
} }
} }
@ -938,39 +890,30 @@ namespace gfx{
set_uniform_mat4x3(get_uniform_loc(name), count, m_arr); set_uniform_mat4x3(get_uniform_loc(name), count, m_arr);
} }
void shader_program::set_uniform_mat2(GLuint loc, GLsizei count, const GLfloat* m_arr){ void shader_program::set_uniform_mat2(GLuint loc, GLsizei count, const GLfloat* m_arr){
use(); glProgramUniformMatrix2fv(m_shader_id, loc, count, GL_FALSE, m_arr);
glUniformMatrix2fv(loc, count, GL_FALSE, m_arr);
} }
void shader_program::set_uniform_mat3(GLuint loc, GLsizei count, const GLfloat* m_arr){ void shader_program::set_uniform_mat3(GLuint loc, GLsizei count, const GLfloat* m_arr){
use(); glProgramUniformMatrix3fv(m_shader_id, loc, count, GL_FALSE, m_arr);
glUniformMatrix3fv(loc, count, GL_FALSE, m_arr);
} }
void shader_program::set_uniform_mat4(GLuint loc, GLsizei count, const GLfloat* m_arr){ void shader_program::set_uniform_mat4(GLuint loc, GLsizei count, const GLfloat* m_arr){
use(); glProgramUniformMatrix4fv(m_shader_id, loc, count, GL_FALSE, m_arr);
glUniformMatrix4fv(loc, count, GL_FALSE, m_arr);
} }
void shader_program::set_uniform_mat2x3(GLuint loc, GLsizei count, const GLfloat* m_arr){ void shader_program::set_uniform_mat2x3(GLuint loc, GLsizei count, const GLfloat* m_arr){
use(); glProgramUniformMatrix2x3fv(m_shader_id, loc, count, GL_FALSE, m_arr);
glUniformMatrix2x3fv(loc, count, GL_FALSE, m_arr);
} }
void shader_program::set_uniform_mat3x2(GLuint loc, GLsizei count, const GLfloat* m_arr){ void shader_program::set_uniform_mat3x2(GLuint loc, GLsizei count, const GLfloat* m_arr){
use(); glProgramUniformMatrix3x2fv(m_shader_id, loc, count, GL_FALSE, m_arr);
glUniformMatrix3x2fv(loc, count, GL_FALSE, m_arr);
} }
void shader_program::set_uniform_mat2x4(GLuint loc, GLsizei count, const GLfloat* m_arr){ void shader_program::set_uniform_mat2x4(GLuint loc, GLsizei count, const GLfloat* m_arr){
use(); glProgramUniformMatrix2x4fv(m_shader_id, loc, count, GL_FALSE, m_arr);
glUniformMatrix2x4fv(loc, count, GL_FALSE, m_arr);
} }
void shader_program::set_uniform_mat4x2(GLuint loc, GLsizei count, const GLfloat* m_arr){ void shader_program::set_uniform_mat4x2(GLuint loc, GLsizei count, const GLfloat* m_arr){
use(); glProgramUniformMatrix4x2fv(m_shader_id, loc, count, GL_FALSE, m_arr);
glUniformMatrix4x2fv(loc, count, GL_FALSE, m_arr);
} }
void shader_program::set_uniform_mat3x4(GLuint loc, GLsizei count, const GLfloat* m_arr){ void shader_program::set_uniform_mat3x4(GLuint loc, GLsizei count, const GLfloat* m_arr){
use(); glProgramUniformMatrix3x4fv(m_shader_id, loc, count, GL_FALSE, m_arr);
glUniformMatrix3x4fv(loc, count, GL_FALSE, m_arr);
} }
void shader_program::set_uniform_mat4x3(GLuint loc, GLsizei count, const GLfloat* m_arr){ void shader_program::set_uniform_mat4x3(GLuint loc, GLsizei count, const GLfloat* m_arr){
use(); glProgramUniformMatrix4x3fv(m_shader_id, loc, count, GL_FALSE, m_arr);
glUniformMatrix4x3fv(loc, count, GL_FALSE, m_arr);
} }
} }

View File

@ -23,12 +23,12 @@
namespace gfx{ namespace gfx{
texture::texture(){ texture::texture(){
glGenTextures(1, &m_tex_id); glCreateTextures(GL_TEXTURE_2D, 1, &m_tex_id);
} }
texture::texture(GLenum format, GLsizei w, GLsizei h, GLenum type): texture::texture(GLenum format, GLsizei w, GLsizei h, GLenum type):
m_width(w), m_height(h) m_width(w), m_height(h)
{ {
glGenTextures(1, &m_tex_id); glCreateTextures(GL_TEXTURE_2D, 1, &m_tex_id);
resize(format, w, h, type); resize(format, w, h, type);
} }
texture::texture(const image& i): texture::texture(const image& i):
@ -55,33 +55,49 @@ namespace gfx{
return false; return false;
if(!m_tex_id) if(!m_tex_id)
glGenTextures(1, &m_tex_id); glCreateTextures(GL_TEXTURE_2D, 1, &m_tex_id);
m_width = i.get_width(); m_width = i.get_width();
m_height = i.get_height(); m_height = i.get_height();
bind();
switch(i.get_channels()){ switch(i.get_channels()){
case 1: case 1:
glTexImage2D(GL_TEXTURE_2D, 0, GL_RED, m_width, m_height, 0, GL_RED, GL_UNSIGNED_BYTE, i.data()); glTextureStorage2D(m_tex_id, 1, GL_R8, m_width, m_height);
glTextureSubImage2D(m_tex_id, 0, 0, 0, m_width, m_height, GL_RED, GL_UNSIGNED_BYTE, i.data());
break; break;
case 2: case 2:
glTexImage2D(GL_TEXTURE_2D, 0, GL_RG, m_width, m_height, 0, GL_RG, GL_UNSIGNED_BYTE, i.data()); glTextureStorage2D(m_tex_id, 1, GL_RG8, m_width, m_height);
glTextureSubImage2D(m_tex_id, 0, 0, 0, m_width, m_height, GL_RG, GL_UNSIGNED_BYTE, i.data());
break; break;
case 3: case 3:
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, m_width, m_height, 0, GL_RGB, GL_UNSIGNED_BYTE, i.data()); glTextureStorage2D(m_tex_id, 1, GL_RGB8, m_width, m_height);
glTextureSubImage2D(m_tex_id, 0, 0, 0, m_width, m_height, GL_RGB, GL_UNSIGNED_BYTE, i.data());
break; break;
case 4: case 4:
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, m_width, m_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, i.data()); glTextureStorage2D(m_tex_id, 1, GL_RGBA8, m_width, m_height);
glTextureSubImage2D(m_tex_id, 0, 0, 0, m_width, m_height, GL_RGBA, GL_UNSIGNED_BYTE, i.data());
break; break;
default: default:
return false; return false;
} }
glGenerateMipmap(GL_TEXTURE_2D); glGenerateTextureMipmap(m_tex_id);
return true; return true;
} }
void texture::resize(GLenum format, GLsizei w, GLsizei h, GLenum type){ void texture::resize(GLenum format, GLsizei w, GLsizei h, GLenum type){
bind(); switch(format){
glTexImage2D(GL_TEXTURE_2D, 0, format, w, h, 0, format, type, NULL); case GL_RED:
glGenerateMipmap(GL_TEXTURE_2D); glTextureStorage2D(m_tex_id, 1, GL_R8, w, h);
break;
case GL_RG:
glTextureStorage2D(m_tex_id, 1, GL_RG8, w, h);
break;
case GL_RGB:
glTextureStorage2D(m_tex_id, 1, GL_RGB8, w, h);
break;
case GL_RGBA:
glTextureStorage2D(m_tex_id, 1, GL_RGBA8, w, h);
break;
};
glTextureSubImage2D(m_tex_id, 0, 0, 0, w, h, format, type, NULL);
glGenerateTextureMipmap(m_tex_id);
} }
void texture::set_width(int w){ void texture::set_width(int w){
@ -96,41 +112,33 @@ namespace gfx{
} }
void texture::set_wrap_mode(wrap w){ void texture::set_wrap_mode(wrap w){
bind(); glTextureParameteri(m_tex_id, GL_TEXTURE_WRAP_S, static_cast<GLint>(w));
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, static_cast<GLint>(w)); glTextureParameteri(m_tex_id, GL_TEXTURE_WRAP_T, static_cast<GLint>(w));
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, static_cast<GLint>(w));
} }
void texture::set_wrap_mode(wrap w, wrap h){ void texture::set_wrap_mode(wrap w, wrap h){
bind(); glTextureParameteri(m_tex_id, GL_TEXTURE_WRAP_S, static_cast<GLint>(w));
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, static_cast<GLint>(w)); glTextureParameteri(m_tex_id, GL_TEXTURE_WRAP_T, static_cast<GLint>(h));
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, static_cast<GLint>(h));
} }
void texture::set_wrap_x(wrap w){ void texture::set_wrap_x(wrap w){
bind(); glTexParameteri(m_tex_id, GL_TEXTURE_WRAP_S, static_cast<GLint>(w));
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, static_cast<GLint>(w));
} }
void texture::set_wrap_y(wrap h){ void texture::set_wrap_y(wrap h){
bind(); glTextureParameteri(m_tex_id, GL_TEXTURE_WRAP_T, static_cast<GLint>(h));
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, static_cast<GLint>(h));
} }
void texture::set_border_color(GLfloat r, GLfloat g, GLfloat b, GLfloat a){ void texture::set_border_color(GLfloat r, GLfloat g, GLfloat b, GLfloat a){
GLfloat arr[4] = {r, g, b, a}; GLfloat arr[4] = {r, g, b, a};
bind(); glTextureParameterfv(m_tex_id, GL_TEXTURE_BORDER_COLOR, arr);
glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, arr);
} }
void texture::set_filter(minfilter min, magfilter mag){ void texture::set_filter(minfilter min, magfilter mag){
bind(); glTextureParameteri(m_tex_id, GL_TEXTURE_MIN_FILTER, static_cast<GLint>(min));
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, static_cast<GLint>(min)); glTextureParameteri(m_tex_id, GL_TEXTURE_MAG_FILTER, static_cast<GLint>(mag));
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, static_cast<GLint>(mag));
} }
void texture::set_mag_filter(magfilter m){ void texture::set_mag_filter(magfilter m){
bind(); glTextureParameteri(m_tex_id, GL_TEXTURE_MAG_FILTER, static_cast<GLint>(m));
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, static_cast<GLint>(m));
} }
void texture::set_min_filter(minfilter m){ void texture::set_min_filter(minfilter m){
bind(); glTextureParameteri(m_tex_id, GL_TEXTURE_MIN_FILTER, static_cast<GLint>(m));
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, static_cast<GLint>(m));
} }
int texture::get_width()const{ int texture::get_width()const{
@ -147,10 +155,8 @@ namespace gfx{
void texture::bind()const{ void texture::bind()const{
glBindTexture(GL_TEXTURE_2D, m_tex_id); glBindTexture(GL_TEXTURE_2D, m_tex_id);
} }
void texture::use(GLuint tunit, GLint uloc)const{ void texture::bind_unit(GLuint tunit)const{
glActiveTexture(GL_TEXTURE0 + tunit); glBindTextureUnit(tunit, m_tex_id);
bind();
glUniform1i(uloc, tunit);
} }

View File

@ -21,202 +21,44 @@
namespace gfx{ namespace gfx{
void vertex_attribute::enable_array_mode(){ void vertex_attribute::enable(){
glBindVertexArray(m_vao); glEnableVertexArrayAttrib(m_vao, m_index);
glEnableVertexAttribArray(m_index);
} }
void vertex_attribute::disable_array_mode(){ void vertex_attribute::disable(){
glBindVertexArray(m_vao); glDisableVertexArrayAttrib(m_vao, m_index);
glEnableVertexAttribArray(m_index);
} }
void vertex_attribute::set_float_pointer(GLint size, GLsizei stride, GLsizei offset){ void vertex_attribute::associate_with(GLuint buffer_binding){
glBindVertexArray(m_vao); glVertexArrayAttribBinding(m_vao, m_index, buffer_binding);
glVertexAttribPointer(m_index, size, GL_FLOAT, GL_FALSE, sizeof(GLfloat) * stride, (void*)(sizeof(GLfloat) * offset));
} }
void vertex_attribute::set_double_pointer(GLint size, GLsizei stride, GLsizei offset){
glBindVertexArray(m_vao); void vertex_attribute::set_float_array(GLint size, GLsizei offset){
glVertexAttribPointer(m_index, size, GL_DOUBLE, GL_FALSE, sizeof(GLdouble) * stride, (void*)(sizeof(GLdouble) * offset)); glVertexArrayAttribFormat(m_vao, m_index, size, GL_FLOAT, GL_FALSE, sizeof(GLfloat) * offset);
} }
void vertex_attribute::set_byte_pointer(GLint size, GLsizei stride, GLsizei offset){ void vertex_attribute::set_double_array(GLint size, GLsizei offset){
glBindVertexArray(m_vao); glVertexArrayAttribFormat(m_vao, m_index, size, GL_DOUBLE, GL_FALSE, sizeof(GLdouble) * offset);
glVertexAttribPointer(m_index, size, GL_BYTE, GL_FALSE, sizeof(GLbyte) * stride, (void*)(sizeof(GLbyte) * offset));
} }
void vertex_attribute::set_ubyte_pointer(GLint size, GLsizei stride, GLsizei offset){ void vertex_attribute::set_byte_array(GLint size, GLsizei offset){
glBindVertexArray(m_vao); glVertexArrayAttribFormat(m_vao, m_index, size, GL_BYTE, GL_FALSE, sizeof(GLbyte) * offset);
glVertexAttribPointer(m_index, size, GL_UNSIGNED_BYTE, GL_FALSE, sizeof(GLubyte) * stride, (void*)(sizeof(GLubyte) * offset));
} }
void vertex_attribute::set_short_pointer(GLint size, GLsizei stride, GLsizei offset){ void vertex_attribute::set_ubyte_array(GLint size, GLsizei offset){
glBindVertexArray(m_vao); glVertexArrayAttribFormat(m_vao, m_index, size, GL_UNSIGNED_BYTE, GL_FALSE, sizeof(GLubyte) * offset);
glVertexAttribPointer(m_index, size, GL_SHORT, GL_FALSE, sizeof(GLshort) * stride, (void*)(sizeof(GLshort) * offset));
} }
void vertex_attribute::set_ushort_pointer(GLint size, GLsizei stride, GLsizei offset){ void vertex_attribute::set_short_array(GLint size, GLsizei offset){
glBindVertexArray(m_vao); glVertexArrayAttribFormat(m_vao, m_index, size, GL_SHORT, GL_FALSE, sizeof(GLshort) * offset);
glVertexAttribPointer(m_index, size, GL_UNSIGNED_SHORT, GL_FALSE, sizeof(GLushort) * stride, (void*)(sizeof(GLushort) * offset));
} }
void vertex_attribute::set_int_pointer(GLint size, GLsizei stride, GLsizei offset){ void vertex_attribute::set_ushort_array(GLint size, GLsizei offset){
glBindVertexArray(m_vao); glVertexArrayAttribFormat(m_vao, m_index, size, GL_UNSIGNED_SHORT, GL_FALSE, sizeof(GLushort) * offset);
glVertexAttribPointer(m_index, size, GL_INT, GL_FALSE, sizeof(GLint) * stride, (void*)(sizeof(GLint) * offset));
} }
void vertex_attribute::set_uint_pointer(GLint size, GLsizei stride, GLsizei offset){ void vertex_attribute::set_int_array(GLint size, GLsizei offset){
glBindVertexArray(m_vao); glVertexArrayAttribFormat(m_vao, m_index, size, GL_INT, GL_FALSE, sizeof(GLint) * offset);
glVertexAttribPointer(m_index, size, GL_UNSIGNED_INT, GL_FALSE, sizeof(GLuint) * stride, (void*)(sizeof(GLuint) * offset));
} }
void vertex_attribute::set(GLfloat x){ void vertex_attribute::set_uint_array(GLint size, GLsizei offset){
glBindVertexArray(m_vao); glVertexArrayAttribFormat(m_vao, m_index, size, GL_UNSIGNED_INT, GL_FALSE, sizeof(GLuint) * offset);
glVertexAttrib1f(m_index, x);
}
void vertex_attribute::set(GLshort x){
glBindVertexArray(m_vao);
glVertexAttrib1s(m_index, x);
}
void vertex_attribute::set(GLdouble x){
glBindVertexArray(m_vao);
glVertexAttrib1d(m_index, x);
}
void vertex_attribute::set(GLint x){
glBindVertexArray(m_vao);
glVertexAttribI1i(m_index, x);
}
void vertex_attribute::set(GLuint x){
glBindVertexArray(m_vao);
glVertexAttribI1ui(m_index, x);
}
void vertex_attribute::set(GLfloat x, GLfloat y){
glBindVertexArray(m_vao);
glVertexAttrib2f(m_index, x, y);
}
void vertex_attribute::set(GLshort x, GLshort y){
glBindVertexArray(m_vao);
glVertexAttrib2s(m_index, x, y);
}
void vertex_attribute::set(GLdouble x, GLdouble y){
glBindVertexArray(m_vao);
glVertexAttrib2d(m_index, x, y);
}
void vertex_attribute::set(GLint x, GLint y){
glBindVertexArray(m_vao);
glVertexAttribI2i(m_index, x, y);
}
void vertex_attribute::set(GLuint x, GLuint y){
glBindVertexArray(m_vao);
glVertexAttribI2ui(m_index, x, y);
}
void vertex_attribute::set(GLfloat x, GLfloat y, GLfloat z){
glBindVertexArray(m_vao);
glVertexAttrib3f(m_index, x, y, z);
}
void vertex_attribute::set(GLshort x, GLshort y, GLshort z){
glBindVertexArray(m_vao);
glVertexAttrib3s(m_index, x, y, z);
}
void vertex_attribute::set(GLdouble x, GLdouble y, GLdouble z){
glBindVertexArray(m_vao);
glVertexAttrib3d(m_index, x, y, z);
}
void vertex_attribute::set(GLint x, GLint y, GLint z){
glBindVertexArray(m_vao);
glVertexAttribI3i(m_index, x, y, z);
}
void vertex_attribute::set(GLuint x, GLuint y, GLuint z){
glBindVertexArray(m_vao);
glVertexAttribI3ui(m_index, x, y, z);
}
void vertex_attribute::set(GLfloat x, GLfloat y, GLfloat z, GLfloat w){
glBindVertexArray(m_vao);
glVertexAttrib4f(m_index, x, y, z, w);
}
void vertex_attribute::set(GLshort x, GLshort y, GLshort z, GLshort w){
glBindVertexArray(m_vao);
glVertexAttrib4s(m_index, x, y, z, w);
}
void vertex_attribute::set(GLdouble x, GLdouble y, GLdouble z, GLdouble w){
glBindVertexArray(m_vao);
glVertexAttrib4d(m_index, x, y, z, w);
}
void vertex_attribute::set(GLint x, GLint y, GLint z, GLint w){
glBindVertexArray(m_vao);
glVertexAttribI4i(m_index, x, y, z, w);
}
void vertex_attribute::set(GLuint x, GLuint y, GLuint z, GLuint w){
glBindVertexArray(m_vao);
glVertexAttribI4ui(m_index, x, y, z, w);
}
void vertex_attribute::set(const vec2<GLfloat>& v){
glBindVertexArray(m_vao);
glVertexAttrib2fv(m_index, v);
}
void vertex_attribute::set(const vec2<GLshort>& v){
glBindVertexArray(m_vao);
glVertexAttrib2sv(m_index, v);
}
void vertex_attribute::set(const vec2<GLdouble>& v){
glBindVertexArray(m_vao);
glVertexAttrib2dv(m_index, v);
}
void vertex_attribute::set(const vec2<GLint>& v){
glBindVertexArray(m_vao);
glVertexAttribI2iv(m_index, v);
}
void vertex_attribute::set(const vec2<GLuint>& v){
glBindVertexArray(m_vao);
glVertexAttribI2uiv(m_index, v);
}
void vertex_attribute::set(const vec3<GLfloat>& v){
glBindVertexArray(m_vao);
glVertexAttrib3fv(m_index, v);
}
void vertex_attribute::set(const vec3<GLshort>& v){
glBindVertexArray(m_vao);
glVertexAttrib3sv(m_index, v);
}
void vertex_attribute::set(const vec3<GLdouble>& v){
glBindVertexArray(m_vao);
glVertexAttrib3dv(m_index, v);
}
void vertex_attribute::set(const vec3<GLint>& v){
glBindVertexArray(m_vao);
glVertexAttribI3iv(m_index, v);
}
void vertex_attribute::set(const vec3<GLuint>& v){
glBindVertexArray(m_vao);
glVertexAttribI3uiv(m_index, v);
}
void vertex_attribute::set(const vec4<GLfloat>& v){
glBindVertexArray(m_vao);
glVertexAttrib4fv(m_index, v);
}
void vertex_attribute::set(const vec4<GLshort>& v){
glBindVertexArray(m_vao);
glVertexAttrib4sv(m_index, v);
}
void vertex_attribute::set(const vec4<GLdouble>& v){
glBindVertexArray(m_vao);
glVertexAttrib4dv(m_index, v);
}
void vertex_attribute::set(const vec4<GLint>& v){
glBindVertexArray(m_vao);
glVertexAttrib4iv(m_index, v);
}
void vertex_attribute::set(const vec4<GLuint>& v){
glBindVertexArray(m_vao);
glVertexAttrib4uiv(m_index, v);
}
void vertex_attribute::set(const vec4<GLbyte>& v){
glBindVertexArray(m_vao);
glVertexAttrib4bv(m_index, v);
}
void vertex_attribute::set(const vec4<GLubyte>& v){
glBindVertexArray(m_vao);
glVertexAttrib4ubv(m_index, v);
}
void vertex_attribute::set(const vec4<GLushort>& v){
glBindVertexArray(m_vao);
glVertexAttrib4usv(m_index, v);
} }
vao::vao(){ vao::vao(){
glGenVertexArrays(1, &m_buffer); glCreateVertexArrays(1, &m_buffer);
} }
vao::vao(vao&& v): vao::vao(vao&& v):
m_buffer(std::exchange(v.m_buffer, 0)){} m_buffer(std::exchange(v.m_buffer, 0)){}
@ -232,6 +74,12 @@ namespace gfx{
return m_buffer; return m_buffer;
} }
void vao::bind_buffer(const vbo& buffer, GLuint bind_point, GLuint offset, GLuint stride){
glVertexArrayVertexBuffer(m_buffer, bind_point, buffer.raw(), offset, stride);
}
void vao::bind_element_buffer(const vbo& buffer){
glVertexArrayElementBuffer(m_buffer, buffer.raw());
}
vertex_attribute vao::get_attribute(int index){ vertex_attribute vao::get_attribute(int index){
return vertex_attribute(m_buffer, index); return vertex_attribute(m_buffer, index);
} }

View File

@ -18,7 +18,6 @@
#include "graphics/vbo.hpp" #include "graphics/vbo.hpp"
#include "util/minmax.hpp" #include "util/minmax.hpp"
#include "graphics/scoped_buffer_bind.hpp"
#include <utility> //exchange, swap, move #include <utility> //exchange, swap, move
@ -27,28 +26,21 @@ namespace gfx{
vbo::vbo(size_t size, usage t): vbo::vbo(size_t size, usage t):
m_buffer_size(0) m_buffer_size(0)
{ {
glGenBuffers(1, &m_buffer); glCreateBuffers(1, &m_buffer);
m_bi.set_buffer_id(m_buffer); glNamedBufferData(m_buffer, size, NULL, static_cast<GLenum>(t));
bind(buffer::copy_write);
glBufferData(m_bi.get_bound_id(), size, NULL, static_cast<GLenum>(t));
} }
vbo::vbo(const vbo& v): vbo::vbo(const vbo& v):
m_buffer_size(v.m_buffer_size) m_buffer_size(v.m_buffer_size)
{ {
glGenBuffers(1, &m_buffer); glCreateBuffers(1, &m_buffer);
m_bi.set_buffer_id(m_buffer); scoped_vbo_map<void> data = v.map(maptype::READ);
scoped_buffer_bind copy_bind(m_bi, buffer::copy_read);
if(!copy_bind.valid())
return;
scoped_vbo_map<void> data = v.map(buffer::copy_read, maptype::READ);
if(!data) if(!data)
return; return;
glBufferData(m_bi.get_bound_id(), v.get_size(), data, static_cast<GLenum>(v.get_usage())); glNamedBufferData(m_buffer, v.get_size(), data, static_cast<GLenum>(v.get_usage()));
} }
vbo::vbo(vbo&& v): vbo::vbo(vbo&& v):
m_buffer(std::exchange(v.m_buffer, 0)), m_buffer(std::exchange(v.m_buffer, 0)),
m_buffer_size(v.m_buffer_size), m_buffer_size(v.m_buffer_size){}
m_bi(std::move(v.m_bi)){}
vbo::~vbo(){ vbo::~vbo(){
if(m_buffer) if(m_buffer)
glDeleteBuffers(1, &m_buffer); glDeleteBuffers(1, &m_buffer);
@ -59,45 +51,20 @@ namespace gfx{
} }
vbo& vbo::operator=(vbo&& v){ vbo& vbo::operator=(vbo&& v){
std::swap(m_buffer, v.m_buffer); std::swap(m_buffer, v.m_buffer);
m_bi = std::move(v.m_bi);
m_buffer_size = v.m_buffer_size; m_buffer_size = v.m_buffer_size;
return *this; return *this;
} }
bool vbo::bind(gl_buffer_manager& buf)const{ bool vbo::bind(gl_buffer_manager& buf)const{
if(get_bound_id() == buf.get_buffer_id()) glBindBuffer(buf.get_buffer_id(), m_buffer);
return true;
if(!m_bi.bind(buf))
return false;
return true; return true;
} }
bool vbo::bind_lock(gl_buffer_manager& buf)const{
if(!m_bi.bind_lock(buf))
return false;
return true;
}
void vbo::bind_unlock()const{
m_bi.unlock();
}
void vbo::unbind()const{
m_bi.unbind();
}
gl_buffer_manager* vbo::get_bound_buffer()const{
return m_bi.get_bound_buffer();
}
GLenum vbo::get_bound_id()const{
return m_bi.get_bound_id();
}
bool vbo::buffer(const void* data, size_t datasize){ bool vbo::buffer(const void* data, size_t datasize){
if(datasize + m_buffer_size > get_cap()){ if(datasize + m_buffer_size > get_cap()){
if(!resize(util::max(datasize + m_buffer_size, get_cap() * 2))) if(!resize(util::max(datasize + m_buffer_size, get_cap() * 2)))
return false; return false;
} }
scoped_buffer_bind copy_bind(m_bi, buffer::copy_write); glNamedBufferSubData(m_buffer, get_size(), datasize, data);
if(!copy_bind.valid())
return false;
glBufferSubData(get_bound_id(), get_size(), datasize, data);
m_buffer_size += datasize; m_buffer_size += datasize;
return true; return true;
} }
@ -111,8 +78,8 @@ namespace gfx{
GLuint vbo::raw()const{ GLuint vbo::raw()const{
return m_buffer; return m_buffer;
} }
scoped_vbo_map<void> vbo::map(gl_buffer_manager& target, maptype m)const{ scoped_vbo_map<void> vbo::map(maptype m)const{
return scoped_vbo_map<void>(m_bi, target, m_buffer, m); return scoped_vbo_map<void>(m_buffer, m);
} }
void vbo::clear(){ void vbo::clear(){
@ -122,57 +89,50 @@ namespace gfx{
return m_buffer_size; return m_buffer_size;
} }
size_t vbo::get_cap()const{ size_t vbo::get_cap()const{
scoped_buffer_bind copy_bind(m_bi, buffer::copy_read);
GLint retval; GLint retval;
glGetBufferParameteriv(get_bound_id(), GL_BUFFER_SIZE, &retval); glGetNamedBufferParameteriv(m_buffer, GL_BUFFER_SIZE, &retval);
return retval; return retval;
} }
auto vbo::get_usage()const -> usage{ auto vbo::get_usage()const -> usage{
scoped_buffer_bind copy_bind(m_bi, buffer::copy_read);
GLint retval; GLint retval;
glGetBufferParameteriv(get_bound_id(), GL_BUFFER_USAGE, &retval); glGetNamedBufferParameteriv(m_buffer, GL_BUFFER_USAGE, &retval);
return static_cast<usage>(retval); return static_cast<usage>(retval);
} }
bool vbo::copy_buffer(vbo& dest, const vbo& src){ bool vbo::copy_buffer(vbo& dest, const vbo& src){
scoped_buffer_bind copy_bind(dest.m_bi, buffer::copy_write);
if(!copy_bind.valid())
return false;
size_t bytes = util::min(dest.get_cap(), src.get_size()); size_t bytes = util::min(dest.get_cap(), src.get_size());
scoped_vbo_map<void> data(src.m_bi, buffer::copy_read, src.m_buffer, maptype::READ); scoped_vbo_map<void> data(src.m_buffer, maptype::READ);
if(!data.valid()) if(!data.valid())
return false; return false;
return dest.buffer(data, bytes); return dest.buffer(data, bytes);
} }
scoped_vbo_map<void>::scoped_vbo_map(const buffer_accessor& ba, gl_buffer_manager& bm, GLuint bid, vbo::maptype m): scoped_vbo_map<void>::scoped_vbo_map(GLuint bid, vbo::maptype m):
m_bind(ba, bm, bid) m_buffer(bid)
{ {
if(!m_bind.valid()) m_data = reinterpret_cast<pointer>(glMapNamedBuffer(m_buffer, static_cast<GLenum>(m)));
return;
m_data = reinterpret_cast<pointer>(glMapBuffer(m_bind.get_bound_id(), static_cast<GLenum>(m)));
} }
scoped_vbo_map<void>::scoped_vbo_map(scoped_vbo_map&& m): scoped_vbo_map<void>::scoped_vbo_map(scoped_vbo_map&& m):
m_data(std::exchange(m.m_data, nullptr)), m_data(std::exchange(m.m_data, nullptr)),
m_bind(std::move(m.m_bind)){} m_buffer(std::exchange(m.m_buffer, 0)){}
scoped_vbo_map<void>::~scoped_vbo_map(){ scoped_vbo_map<void>::~scoped_vbo_map(){
if(m_data){ if(m_data){
glUnmapBuffer(m_bind.get_bound_id()); glUnmapNamedBuffer(m_buffer);
} }
} }
scoped_vbo_map<void>& scoped_vbo_map<void>::operator=(scoped_vbo_map&& m){ scoped_vbo_map<void>& scoped_vbo_map<void>::operator=(scoped_vbo_map&& m){
std::swap(m_data, m.m_data); std::swap(m_data, m.m_data);
m_bind = std::move(m.m_bind); std::swap(m_buffer, m.m_buffer);
return *this; return *this;
} }
auto scoped_vbo_map<void>::length()const -> size_type{ auto scoped_vbo_map<void>::length()const -> size_type{
GLint64 retval; GLint64 retval;
glGetBufferParameteri64v(m_bind.get_bound_id(), GL_BUFFER_MAP_LENGTH, &retval); glGetNamedBufferParameteri64v(m_buffer, GL_BUFFER_MAP_LENGTH, &retval);
return retval; return retval;
} }
auto scoped_vbo_map<void>::release() -> pointer{ auto scoped_vbo_map<void>::release() -> pointer{
m_buffer = 0;
return std::exchange(m_data, nullptr); return std::exchange(m_data, nullptr);
} }
scoped_vbo_map<void>::operator pointer(){ scoped_vbo_map<void>::operator pointer(){

View File

@ -320,11 +320,11 @@ int main(){
} }
//tell gl how to interpret the data in the vbo //tell gl how to interpret the data in the vbo
normal_vao.bind(); normal_vao.bind_buffer(normal_vbo, 0, 0, sizeof(GLfloat) * 3);
normal_vbo.bind(gfx::buffer::array);
auto attrib = normal_vao.get_attribute(0); auto attrib = normal_vao.get_attribute(0);
attrib.set_float_pointer(3, 3, 0); attrib.set_float_array(3, 0);
attrib.enable_array_mode(); attrib.associate_with(0);
attrib.enable();
constexpr GLfloat square_coords[] = { constexpr GLfloat square_coords[] = {
-1, 1, 0, 1, -1, 1, 0, 1,
@ -341,14 +341,16 @@ int main(){
screen_vbo.buffer(square_coords, sizeof(square_coords)); screen_vbo.buffer(square_coords, sizeof(square_coords));
screen_vao.bind();
screen_vbo.bind(gfx::buffer::array); screen_vao.bind_buffer(screen_vbo, 0, 0, sizeof(GLfloat) * 4);
attrib = screen_vao.get_attribute(0); attrib = screen_vao.get_attribute(0);
attrib.set_float_pointer(2, 4, 0); attrib.set_float_array(2, 0);
attrib.enable_array_mode(); attrib.associate_with(0);
attrib.enable();
attrib = screen_vao.get_attribute(1); attrib = screen_vao.get_attribute(1);
attrib.set_float_pointer(2, 4, 2); attrib.set_float_array(2, 2);
attrib.enable_array_mode(); attrib.associate_with(0);
attrib.enable();
while(!manager.should_close()){ while(!manager.should_close()){
//render using program 'prog' with data from 'vao' //render using program 'prog' with data from 'vao'
@ -358,7 +360,8 @@ int main(){
normal_shader.use(); normal_shader.use();
normal_vao.bind(); normal_vao.bind();
glDrawArrays(GL_POINTS, 0, 9); glDrawArrays(GL_POINTS, 0, 9);
fbo.unbind();
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glDisable(GL_DEPTH_TEST); glDisable(GL_DEPTH_TEST);
screen_shader.use(); screen_shader.use();
screen_shader.set_uniform("fbtexture", tex, 0); screen_shader.set_uniform("fbtexture", tex, 0);