From 82146468f3fc70bd7d76bb57d63c5937a032ee88 Mon Sep 17 00:00:00 2001 From: Jay Robson Date: Wed, 10 Jul 2024 16:22:42 +1000 Subject: [PATCH] less gl state --- src/graphics/gl/texture.cpp | 4 ++-- src/graphics/gl/texture.hpp | 2 +- src/graphics/particles.cpp | 7 ++++--- src/graphics/pipeline.cpp | 3 +++ src/graphics/pipeline.hpp | 2 ++ src/graphics/texture/generate_atlas.cpp | 14 ++++++-------- src/graphics/texture/generate_atlas.hpp | 2 +- src/graphics/vertex.cpp | 7 ++++--- src/main.cpp | 2 -- 9 files changed, 23 insertions(+), 20 deletions(-) diff --git a/src/graphics/gl/texture.cpp b/src/graphics/gl/texture.cpp index 98809b6..b8480b5 100644 --- a/src/graphics/gl/texture.cpp +++ b/src/graphics/gl/texture.cpp @@ -5,8 +5,8 @@ using Graphics::GL::Texture; -Texture::Texture(int type) { - glCreateTextures(type, 1, &m_handle); +Texture::Texture() { + glGenTextures(1, &m_handle); } Texture::Texture(Texture&& o) : BufferBase(std::move(o)) { diff --git a/src/graphics/gl/texture.hpp b/src/graphics/gl/texture.hpp index 16d229b..885cd7d 100644 --- a/src/graphics/gl/texture.hpp +++ b/src/graphics/gl/texture.hpp @@ -4,7 +4,7 @@ #include "buffer_base.hpp" namespace Graphics::GL { struct Texture : BufferBase { - Texture(int type); + Texture(); Texture(Texture&& o); ~Texture(); }; diff --git a/src/graphics/particles.cpp b/src/graphics/particles.cpp index 0515622..ec26138 100644 --- a/src/graphics/particles.cpp +++ b/src/graphics/particles.cpp @@ -23,9 +23,9 @@ Particles::Particles() { glNamedBufferData(m_ubo_info, sizeof(InfoType), nullptr, GL_DYNAMIC_DRAW); m_particles_mapping = (Type*)glMapNamedBuffer(m_ssbo_particles, GL_READ_WRITE); - auto b = m_vao.bind(); + glBindVertexArray(m_vao); glBindBuffer(GL_ARRAY_BUFFER, m_ssbo_vertices); - Vertex::set_vertex_attribs(); + glBindVertexArray(0); } void Particles::update(const Pipeline& pipeline) { @@ -60,8 +60,9 @@ void Particles::render(const Context& ctx) const { ctx.set_colour_matrix(glm::mat4(1)); ctx.set_model_matrix(glm::mat4(1)); - auto b = m_vao.bind(); + glBindVertexArray(m_vao); glDrawArrays(GL_POINTS, 0, m_particles_mapping_size); + glBindVertexArray(0); } void Particles::add(const Type& type) { diff --git a/src/graphics/pipeline.cpp b/src/graphics/pipeline.cpp index 99d7891..39a5a8c 100644 --- a/src/graphics/pipeline.cpp +++ b/src/graphics/pipeline.cpp @@ -6,10 +6,13 @@ #include "gl/shader.hpp" #include "shader/compile.hpp" #include "shader/link.hpp" +#include "texture/generate_atlas.hpp" using Graphics::Pipeline; Pipeline::Pipeline() { + Graphics::Texture::generate_atlas(m_generated_atlas); + Graphics::GL::Shader model_vert = Graphics::Shader::compile_new(GL_VERTEX_SHADER, "model.vert"); Graphics::GL::Shader model_frag = Graphics::Shader::compile_new(GL_FRAGMENT_SHADER, "model.frag"); Graphics::GL::Shader particles_comp = Graphics::Shader::compile_new(GL_COMPUTE_SHADER, "particles.comp"); diff --git a/src/graphics/pipeline.hpp b/src/graphics/pipeline.hpp index ea1ca5e..e1106b7 100644 --- a/src/graphics/pipeline.hpp +++ b/src/graphics/pipeline.hpp @@ -4,12 +4,14 @@ #include "context.hpp" #include "gl/program.hpp" #include "particles.hpp" +#include "texture/generate_atlas.hpp" #include "window.hpp" #include "../world/state.hpp" namespace Graphics { struct Pipeline { Window m_window; + Texture::GeneratedAtlas m_generated_atlas; Particles m_particles; GL::Program m_program_model; GL::Program m_program_particles; diff --git a/src/graphics/texture/generate_atlas.cpp b/src/graphics/texture/generate_atlas.cpp index f543170..b169afe 100644 --- a/src/graphics/texture/generate_atlas.cpp +++ b/src/graphics/texture/generate_atlas.cpp @@ -1,16 +1,15 @@ +#include "generate_atlas.hpp" #include "../texture.hpp" #include "../shader.hpp" #include "atlas.hpp" #include "image.hpp" #include "uv.hpp" -#include "../gl/array_buffer.hpp" -#include "../gl/texture.hpp" #include #include #include -void Graphics::Texture::generate_atlas() { +void Graphics::Texture::generate_atlas(GeneratedAtlas& ga) { int total_area = 0; int min_size = 1; int padding = 0; @@ -71,18 +70,17 @@ void Graphics::Texture::generate_atlas() { } } + glBindBufferBase(GL_SHADER_STORAGE_BUFFER, Graphics::Shader::SSBO_ATLAS_BUFFER, ga.m_ssbo_id); + glNamedBufferData(ga.m_ssbo_id, sizeof(UV) * uvs.size(), uvs.data(), GL_STATIC_DRAW); - glBindBufferBase(GL_SHADER_STORAGE_BUFFER, Graphics::Shader::SSBO_ATLAS_BUFFER, ssbo_id); - glNamedBufferData(ssbo_id, sizeof(UV) * uvs.size(), uvs.data(), GL_STATIC_DRAW); - - glTextureStorage2D(tex_id, glActiveTexture(GL_TEXTURE0 + Graphics::Shader::TEX_ATLAS); - glBindTexture(GL_TEXTURE_2D_ARRAY, tex_id); + glBindTexture(GL_TEXTURE_2D_ARRAY, ga.m_tex_id); glTexImage3D(GL_TEXTURE_2D_ARRAY, 0, GL_RGBA8, atlas.m_size.x, atlas.m_size.y, atlas.m_size.z, 0, GL_RGBA, GL_UNSIGNED_BYTE, atlas.m_data.data()); glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glGenerateMipmap(GL_TEXTURE_2D_ARRAY); + glActiveTexture(GL_TEXTURE0); } diff --git a/src/graphics/texture/generate_atlas.hpp b/src/graphics/texture/generate_atlas.hpp index 5415051..d987f43 100644 --- a/src/graphics/texture/generate_atlas.hpp +++ b/src/graphics/texture/generate_atlas.hpp @@ -10,6 +10,6 @@ namespace Graphics::Texture { GL::Texture m_tex_id; }; - GeneratedAtlas generate_atlas(); + void generate_atlas(GeneratedAtlas& ga); }; diff --git a/src/graphics/vertex.cpp b/src/graphics/vertex.cpp index 8add702..c951881 100644 --- a/src/graphics/vertex.cpp +++ b/src/graphics/vertex.cpp @@ -10,11 +10,12 @@ void Vertex::set_vertex_attribs(unsigned int vao, unsigned int vbo) { glVertexArrayVertexBuffer(vao, 0, vbo, 0, sizeof(v)); glVertexArrayAttribFormat(vao, 0, 4, GL_FLOAT, false, Util::pointer_diff(&v, &v.m_pos)); - glVertexArrayAttribFormat(vao, 1, 4, GL_FLOAT, false, Util::pointer_diff(&v, &v.m_pos)); - glVertexArrayAttribFormat(vao, 2, 2, GL_FLOAT, false, Util::pointer_diff(&v, &v.m_pos)); - glVertexArrayAttribIFormat(vao, 3, 1, GL_UNSIGNED_INT, Util::pointer_diff(&v, &v.m_pos)); + glVertexArrayAttribFormat(vao, 1, 4, GL_FLOAT, false, Util::pointer_diff(&v, &v.m_colour)); + glVertexArrayAttribFormat(vao, 2, 2, GL_FLOAT, false, Util::pointer_diff(&v, &v.m_uv)); + glVertexArrayAttribIFormat(vao, 3, 1, GL_UNSIGNED_INT, Util::pointer_diff(&v, &v.m_texid)); for(int i = 0; i < 4; i++) { + glEnableVertexArrayAttrib(vao, i); glVertexArrayAttribBinding(vao, i, 0); } } diff --git a/src/main.cpp b/src/main.cpp index 7594bed..012402b 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,13 +1,11 @@ #include "graphics/context.hpp" #include "graphics/pipeline.hpp" -#include "graphics/texture.hpp" #include "world/state.hpp" #include int main() { Graphics::Pipeline pipeline; - Graphics::Texture::generate_atlas(); World::State state; while(!pipeline.should_close()) {