From c68d2967d37656054a2f73248a4c8aa05a833a20 Mon Sep 17 00:00:00 2001 From: Jay Robson Date: Wed, 10 Jul 2024 15:35:19 +1000 Subject: [PATCH] pre refactor --- CMakeLists.txt | 7 --- assets/image/missing.png | Bin 0 -> 552 bytes assets/image/white.png | Bin 0 -> 546 bytes assets/shader/header.glsl | 7 +++ assets/shader/math.glsl | 2 +- assets/shader/particles.comp | 62 +++++++++++++++++++ assets/shader/random.glsl | 15 +++++ src/graphics/gl/array_buffer.cpp | 2 +- src/graphics/gl/array_model.cpp | 18 ++++++ src/graphics/gl/array_model.hpp | 18 ++++++ src/graphics/gl/buffer_base.cpp | 7 +++ src/graphics/gl/buffer_base.hpp | 2 + src/graphics/gl/frame_buffer.cpp | 2 +- src/graphics/gl/model.cpp | 13 ++-- src/graphics/gl/model.hpp | 1 - src/graphics/gl/render_buffer.cpp | 2 +- src/graphics/gl/texture.cpp | 4 +- src/graphics/gl/texture.hpp | 2 +- src/graphics/gl/vertex_array.cpp | 2 +- src/graphics/particles.cpp | 76 ++++++++++++++++++++++++ src/graphics/particles.hpp | 43 ++++++++++++++ src/graphics/pipeline.cpp | 14 +++-- src/graphics/pipeline.hpp | 5 +- src/graphics/shader.hpp | 3 + src/graphics/shader/compile.cpp | 6 ++ src/graphics/shader/compile.hpp | 3 + src/graphics/shader/link.cpp | 6 ++ src/graphics/shader/link.hpp | 2 + src/graphics/texture.hpp | 8 ++- src/graphics/texture/generate_atlas.cpp | 6 +- src/graphics/texture/generate_atlas.hpp | 15 +++++ src/graphics/texture/image.cpp | 6 +- src/graphics/texture/image.hpp | 4 +- src/graphics/vertex.cpp | 13 ++-- src/graphics/vertex.hpp | 5 +- src/util/debug.hpp | 16 +++++ src/util/pointer_diff.cpp | 6 +- src/world/builder.cpp | 3 - src/world/chunk.cpp | 7 +-- 39 files changed, 352 insertions(+), 61 deletions(-) create mode 100644 assets/image/missing.png create mode 100644 assets/image/white.png create mode 100644 assets/shader/particles.comp create mode 100644 assets/shader/random.glsl create mode 100644 src/graphics/gl/array_model.cpp create mode 100644 src/graphics/gl/array_model.hpp create mode 100644 src/graphics/particles.cpp create mode 100644 src/graphics/particles.hpp create mode 100644 src/graphics/texture/generate_atlas.hpp create mode 100644 src/util/debug.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index c8b908d..27e9335 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -9,18 +9,11 @@ if(NOT WIN32) endif() if(NOT WIN32) - if(NOT DEFINED SIGFPE_ENABLED) - set(SIGFPE_ENABLED 1) - endif() set(libs stdc++ m GLEW glfw GL freetype assimp jsoncpp openal vorbisfile alut atomic) else() set(libs stdc++ m brotlidec assimp-5 glew32 opengl32 glfw3 freetype jsoncpp zlibstatic soft_oal vorbisfile vorbis ogg alut atomic) endif() -if(SIGFPE_ENABLED) - add_compile_definitions(SIGFPE_ENABLED=1) -endif() - if(DEBUG_ENABLED) message("-- Enabled: Runtime debug checks") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g") diff --git a/assets/image/missing.png b/assets/image/missing.png new file mode 100644 index 0000000000000000000000000000000000000000..1890fd2421d2886519db299a4aaffb1f8440d12f GIT binary patch literal 552 zcmV+@0@wYCP)EX>4Tx04R}tkv&MmKpe$iQ>7}E4t5ZA$WV2$i;6hbDionYsTEpvFuC*#nlvOS zE{=k0!NHHks)LKOt`4q(Aou~|?BJy6A|?JWDYS_3;J6>}?mh0_0Yam~RI@7vsG4P@ z6LB${TNMMZ2%r}s3}akkraqTSCE+=~?&0I>U6f~epZjz4D|wRvK9M-abi*RvAfDN@ zbk6(4VOEk9;&b8&gDyz?$aUG}H_ki6e@tQNECM zS>e3JS*_Gq>z@3D!MwJz%ypV0NMI35NI`^*8p^1^LX=jG6ccIMk9+us9Dj;jGP%lN zEX>4Tx04R}tkv&MmKpe$iQ>7}E4t5ZA$WV2$i;6hbDionYsTEpvFuC*#nlvOS zE{=k0!NHHks)LKOt`4q(Aou~|?BJy6A|?JWDYS_3;J6>}?mh0_0Yam~RI@7vsG4P@ z6LB${TNMMZ2%r}s3}akkraqTSCE+=~?&0I>U6f~epZjz4D|wRvK9M-abi*RvAfDN@ zbk6(4VOEk9;&b8&gDyz?$aUG}H_ki6e@tQNECM zS>e3JS*_Gq>z@3D!MwJz%ypV0NMI35NI`^*8p^1^LX=jG6ccIMk9+us9Dj;jGP%lN zPXGV_ literal 0 HcmV?d00001 diff --git a/assets/shader/header.glsl b/assets/shader/header.glsl index ba3222c..69f97a4 100644 --- a/assets/shader/header.glsl +++ b/assets/shader/header.glsl @@ -2,5 +2,12 @@ #version 430 core #define SSBO_ATLAS_BUFFER 1 +#define SSBO_PARTICLES_IN 2 +#define SSBO_PARTICLES_OUT 3 +#define UBO_PARTICLES_INFO 4 + #define TEX_ATLAS 1 +#define TEXID_MISSING 0 +#define TEXID_WHITE 1 + diff --git a/assets/shader/math.glsl b/assets/shader/math.glsl index 4575cb5..583972e 100644 --- a/assets/shader/math.glsl +++ b/assets/shader/math.glsl @@ -1,5 +1,5 @@ -float Map(float x, float i_min, float i_max, float o_min, float o_max) { +float map(float x, float i_min, float i_max, float o_min, float o_max) { return (x - i_min) * (o_max - o_min) / (i_max - i_min) + o_min; } diff --git a/assets/shader/particles.comp b/assets/shader/particles.comp new file mode 100644 index 0000000..cb7954f --- /dev/null +++ b/assets/shader/particles.comp @@ -0,0 +1,62 @@ + +#include "header.glsl" +#include "random.glsl" + +layout (local_size_x = 32, local_size_y = 1, local_size_z = 1) in; + +struct VertType { + vec4 pos; + vec4 colour; + vec2 uv; + float texid; +}; + +struct ParticleType { + vec4 pos; // xyz: pos, w: vel random scale + vec4 velocity; // xyz: vel, w: colour random scale + vec4 colour; + float time_start; + float duration; + float size; + int seed; +}; + +layout (binding = SSBO_PARTICLES_IN) readonly buffer ParticleBufferIn { + ParticleType particles_in[]; +}; + +layout (binding = SSBO_PARTICLES_OUT) writeonly buffer ParticleBufferOut { + VertType vertices_out[]; +}; + +layout (binding = UBO_PARTICLES_INFO) uniform ParticleInfo { + float p_time; + int p_size; +}; + +void main() { + ParticleType particle = particles_in[gl_WorkGroupID.x]; + int seed = particle.seed; + rand_mix(seed, int(gl_LocalInvocationID.x)); + + float duration = clamp((p_time - particle.time_start) / particle.duration, 0, 1); + vec3 velocity = normalize(vec3( + rand_float(seed) * 2 - 1, + rand_float(seed) * 2 - 1, + rand_float(seed) * 2 - 1 + )) * particle.pos.w + particle.velocity.xyz; + vec4 colour = normalize(vec4( + rand_float(seed) * 2 - 1, + rand_float(seed) * 2 - 1, + rand_float(seed) * 2 - 1, + rand_float(seed) * 2 - 1 + )) * particle.velocity.w + particle.colour; + vec3 pos = particle.pos.xyz + velocity * duration; + colour.a *= (1 - duration); + + uint v_id = gl_GlobalInvocationID.x; + vertices_out[v_id].pos = vec4(pos, 1); + vertices_out[v_id].colour = colour; + vertices_out[v_id].texid = TEXID_WHITE; +} + diff --git a/assets/shader/random.glsl b/assets/shader/random.glsl new file mode 100644 index 0000000..bdf0a44 --- /dev/null +++ b/assets/shader/random.glsl @@ -0,0 +1,15 @@ + +int rand_gen(in int n) { + n = (n << 13) ^ n; + return (n * (n*n*15731+789221) + 1376312589) & 0x7fffffff; +} + +float rand_float(inout int n) { + n = rand_gen(n); + return float(n) / float(0x7fffffff); +} + +void rand_mix(inout int n, int v) { + n = rand_gen(n ^ v); +} + diff --git a/src/graphics/gl/array_buffer.cpp b/src/graphics/gl/array_buffer.cpp index 185937e..a53fd1c 100644 --- a/src/graphics/gl/array_buffer.cpp +++ b/src/graphics/gl/array_buffer.cpp @@ -7,7 +7,7 @@ using Graphics::GL::ArrayBuffer; ArrayBuffer::ArrayBuffer() { - glGenBuffers(1, &m_handle); + glCreateBuffers(1, &m_handle); } ArrayBuffer::ArrayBuffer(ArrayBuffer&& o) : BufferBase(std::move(o)) { diff --git a/src/graphics/gl/array_model.cpp b/src/graphics/gl/array_model.cpp new file mode 100644 index 0000000..dcd3612 --- /dev/null +++ b/src/graphics/gl/array_model.cpp @@ -0,0 +1,18 @@ + +#include +#include +#include "array_model.hpp" + +using Graphics::GL::ArrayModel; + +void ArrayModel::set(const std::vector& vertices, int usage) { + Vertex::set_vertex_attribs(m_vao, m_vbo); + glNamedBufferData(m_vbo, vertices.size() * sizeof(vertices[0]), vertices.data(), usage); + m_size = vertices.size(); +} + +void ArrayModel::render(int mode) const { + glBindVertexArray(m_vao); + glDrawArrays(mode, 0, m_size); +} + diff --git a/src/graphics/gl/array_model.hpp b/src/graphics/gl/array_model.hpp new file mode 100644 index 0000000..bc30483 --- /dev/null +++ b/src/graphics/gl/array_model.hpp @@ -0,0 +1,18 @@ + +#pragma once + +#include "array_buffer.hpp" +#include "vertex_array.hpp" +#include "../vertex.hpp" +#include + +namespace Graphics::GL { + struct ArrayModel { + VertexArray m_vao; + ArrayBuffer m_vbo; + unsigned int m_size; + + void set(const std::vector& vertices, int usage); + void render(int mode) const; + }; +}; diff --git a/src/graphics/gl/buffer_base.cpp b/src/graphics/gl/buffer_base.cpp index 9c3fc3c..bf31803 100644 --- a/src/graphics/gl/buffer_base.cpp +++ b/src/graphics/gl/buffer_base.cpp @@ -1,4 +1,5 @@ +#include #include "buffer_base.hpp" using Graphics::GL::BufferBase; @@ -12,3 +13,9 @@ BufferBase::operator unsigned int() const { return m_handle; } +bool BufferBase::check_binding(int mode) { + int binding; + glGetIntegerv(mode, &binding); + return binding == 0; +} + diff --git a/src/graphics/gl/buffer_base.hpp b/src/graphics/gl/buffer_base.hpp index 2640fd7..bf6aedb 100644 --- a/src/graphics/gl/buffer_base.hpp +++ b/src/graphics/gl/buffer_base.hpp @@ -9,6 +9,8 @@ namespace Graphics::GL { BufferBase(const BufferBase& o) = delete; operator unsigned int() const; + static bool check_binding(int mode); + protected: BufferBase(BufferBase&& o); }; diff --git a/src/graphics/gl/frame_buffer.cpp b/src/graphics/gl/frame_buffer.cpp index 47d2008..1fe12e8 100644 --- a/src/graphics/gl/frame_buffer.cpp +++ b/src/graphics/gl/frame_buffer.cpp @@ -6,7 +6,7 @@ using Graphics::GL::FrameBuffer; FrameBuffer::FrameBuffer() { - glGenFramebuffers(1, &m_handle); + glCreateFramebuffers(1, &m_handle); } FrameBuffer::FrameBuffer(FrameBuffer&& o) : BufferBase(std::move(o)) { diff --git a/src/graphics/gl/model.cpp b/src/graphics/gl/model.cpp index 089b9b5..6c281a3 100644 --- a/src/graphics/gl/model.cpp +++ b/src/graphics/gl/model.cpp @@ -5,18 +5,15 @@ using Graphics::GL::Model; void Model::set(const Mesh& mesh, int usage) { - glBufferData(GL_ARRAY_BUFFER, mesh.m_vertices.size() * sizeof(mesh.m_vertices[0]), mesh.m_vertices.data(), usage); - glBufferData(GL_ELEMENT_ARRAY_BUFFER, mesh.m_indices.size() * sizeof(mesh.m_indices[0]), mesh.m_indices.data(), usage); + Vertex::set_vertex_attribs(m_vao, m_vbo); + glVertexArrayElementBuffer(m_vao, m_ebo); + glNamedBufferData(m_vbo, mesh.m_vertices.size() * sizeof(mesh.m_vertices[0]), mesh.m_vertices.data(), usage); + glNamedBufferData(m_ebo, mesh.m_indices.size() * sizeof(mesh.m_indices[0]), mesh.m_indices.data(), usage); m_size = mesh.m_indices.size(); } -void Model::bind() const { - glBindVertexArray(m_vao); - glBindBuffer(GL_ARRAY_BUFFER, m_vbo); - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_ebo); -} - void Model::render(int mode) const { + glBindVertexArray(m_vao); glDrawElements(mode, m_size, GL_UNSIGNED_INT, 0); } diff --git a/src/graphics/gl/model.hpp b/src/graphics/gl/model.hpp index 4d16ee5..33d1dc7 100644 --- a/src/graphics/gl/model.hpp +++ b/src/graphics/gl/model.hpp @@ -12,7 +12,6 @@ namespace Graphics::GL { ArrayBuffer m_ebo; unsigned int m_size; - void bind() const; void set(const Mesh& mesh, int usage); void render(int mode) const; }; diff --git a/src/graphics/gl/render_buffer.cpp b/src/graphics/gl/render_buffer.cpp index dd90065..5e8618f 100644 --- a/src/graphics/gl/render_buffer.cpp +++ b/src/graphics/gl/render_buffer.cpp @@ -6,7 +6,7 @@ using Graphics::GL::RenderBuffer; RenderBuffer::RenderBuffer() { - glGenRenderbuffers(1, &m_handle); + glCreateRenderbuffers(1, &m_handle); } RenderBuffer::RenderBuffer(RenderBuffer&& o) : BufferBase(std::move(o)) { diff --git a/src/graphics/gl/texture.cpp b/src/graphics/gl/texture.cpp index b8480b5..98809b6 100644 --- a/src/graphics/gl/texture.cpp +++ b/src/graphics/gl/texture.cpp @@ -5,8 +5,8 @@ using Graphics::GL::Texture; -Texture::Texture() { - glGenTextures(1, &m_handle); +Texture::Texture(int type) { + glCreateTextures(type, 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 885cd7d..16d229b 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(); + Texture(int type); Texture(Texture&& o); ~Texture(); }; diff --git a/src/graphics/gl/vertex_array.cpp b/src/graphics/gl/vertex_array.cpp index fc226c4..7cea817 100644 --- a/src/graphics/gl/vertex_array.cpp +++ b/src/graphics/gl/vertex_array.cpp @@ -6,7 +6,7 @@ using Graphics::GL::VertexArray; VertexArray::VertexArray() { - glGenVertexArrays(1, &m_handle); + glCreateVertexArrays(1, &m_handle); } VertexArray::VertexArray(VertexArray&& o) : BufferBase(std::move(o)) { diff --git a/src/graphics/particles.cpp b/src/graphics/particles.cpp new file mode 100644 index 0000000..0515622 --- /dev/null +++ b/src/graphics/particles.cpp @@ -0,0 +1,76 @@ + +#include "particles.hpp" +#include "context.hpp" +#include "pipeline.hpp" +#include "shader.hpp" +#include "vertex.hpp" +#include +#include + +using Graphics::Particles; + + +struct InfoType { + float m_time; + int m_size; +}; + +Particles::Particles() { + m_time_started = glfwGetTime(); + + glNamedBufferStorage(m_ssbo_particles, sizeof(Type) * MAX_PARTICLES, nullptr, GL_MAP_PERSISTENT_BIT | GL_MAP_READ_BIT | GL_MAP_WRITE_BIT); + glNamedBufferData(m_ssbo_vertices, sizeof(Vertex) * MAX_PARTICLES, nullptr, GL_DYNAMIC_COPY); + 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(); + glBindBuffer(GL_ARRAY_BUFFER, m_ssbo_vertices); + Vertex::set_vertex_attribs(); +} + +void Particles::update(const Pipeline& pipeline) { + double now = get_time_diff(); + int j = 0; + for(int i = 0; i < m_particles_mapping_size; i++) { + Type& p = m_particles_mapping[i]; + if(p.m_time_start + p.m_duration < now) { + if(i > j) { + m_particles_mapping[j] = m_particles_mapping[i]; + } + j++; + } + } + m_particles_mapping_size = j; + + InfoType info { + .m_time = (float)now, + .m_size = m_particles_mapping_size, + }; + + glNamedBufferSubData(m_ubo_info, 0, sizeof(InfoType), &info); + glBindBufferBase(GL_SHADER_STORAGE_BUFFER, Shader::SSBO_PARTICLES_IN, m_ssbo_particles); + glBindBufferBase(GL_SHADER_STORAGE_BUFFER, Shader::SSBO_PARTICLES_OUT, m_ssbo_vertices); + glBindBufferBase(GL_UNIFORM_BUFFER, Shader::UBO_PARTICLES_INFO, m_ubo_info); + glUseProgram(pipeline.m_program_particles); + glDispatchCompute(m_particles_mapping_size, 1, 1); +} + +void Particles::render(const Context& ctx) const { + glMemoryBarrier(GL_SHADER_STORAGE_BARRIER_BIT); + ctx.set_colour_matrix(glm::mat4(1)); + ctx.set_model_matrix(glm::mat4(1)); + + auto b = m_vao.bind(); + glDrawArrays(GL_POINTS, 0, m_particles_mapping_size); +} + +void Particles::add(const Type& type) { + if(m_particles_mapping_size < MAX_PARTICLES) { + m_particles_mapping[m_particles_mapping_size++] = type; + } +} + +double Particles::get_time_diff() { + return m_time_started - glfwGetTime(); +} + diff --git a/src/graphics/particles.hpp b/src/graphics/particles.hpp new file mode 100644 index 0000000..ca8b4e9 --- /dev/null +++ b/src/graphics/particles.hpp @@ -0,0 +1,43 @@ + +#pragma once + +#include "context.hpp" +#include "gl/array_buffer.hpp" +#include "gl/vertex_array.hpp" +#include + +namespace Graphics { + struct Pipeline; + struct Particles { + struct Type { + glm::vec3 m_pos; + float m_random_velocity_scale; + glm::vec3 m_velocity; + float m_random_colour_scale; + glm::vec4 m_colour; + float m_time_start; + float m_duration; + float m_size; + int m_seed; + }; + + static_assert(sizeof(Type) == 64); + + static constexpr int MAX_PARTICLES = 1024; + + GL::VertexArray m_vao; + GL::ArrayBuffer m_ssbo_particles; + GL::ArrayBuffer m_ssbo_vertices; + GL::ArrayBuffer m_ubo_info; + Type* m_particles_mapping; + int m_particles_mapping_size = 0; + double m_time_started; + + Particles(); + void add(const Type& type); + double get_time_diff(); + void update(const Pipeline& pipeline); + void render(const Context& ctx) const; + }; +}; + diff --git a/src/graphics/pipeline.cpp b/src/graphics/pipeline.cpp index f878823..99d7891 100644 --- a/src/graphics/pipeline.cpp +++ b/src/graphics/pipeline.cpp @@ -10,28 +10,30 @@ using Graphics::Pipeline; Pipeline::Pipeline() { - Graphics::GL::Shader model_vert(GL_VERTEX_SHADER); - Graphics::GL::Shader model_frag(GL_FRAGMENT_SHADER); - Graphics::Shader::compile(model_vert, "model.vert"); - Graphics::Shader::compile(model_frag, "model.frag"); + 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"); + Graphics::Shader::link(m_program_model, {model_vert, model_frag}); + Graphics::Shader::link(m_program_particles, {particles_comp}); } bool Pipeline::should_close() const { return m_window.should_close(); } -void Pipeline::update() const { +void Pipeline::update() { m_window.update(); + m_particles.update(*this); } void Pipeline::render(const World::State& state, const Context& ctx) const { - glUseProgram(m_program_model); glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT); glClearColor(0.1, 0.1, 0.12, 1); state.render(ctx); + m_particles.render(ctx); m_window.swap_buffers(); } diff --git a/src/graphics/pipeline.hpp b/src/graphics/pipeline.hpp index 2c5820d..ea1ca5e 100644 --- a/src/graphics/pipeline.hpp +++ b/src/graphics/pipeline.hpp @@ -3,16 +3,19 @@ #include "context.hpp" #include "gl/program.hpp" +#include "particles.hpp" #include "window.hpp" #include "../world/state.hpp" namespace Graphics { struct Pipeline { Window m_window; + Particles m_particles; GL::Program m_program_model; + GL::Program m_program_particles; Pipeline(); - void update() const; + void update(); void render(const World::State& state, const Context& ctx) const; bool should_close() const; diff --git a/src/graphics/shader.hpp b/src/graphics/shader.hpp index f827b4f..1fdc1fe 100644 --- a/src/graphics/shader.hpp +++ b/src/graphics/shader.hpp @@ -3,6 +3,9 @@ namespace Graphics::Shader { constexpr int SSBO_ATLAS_BUFFER = 1; + constexpr int SSBO_PARTICLES_IN = 2; + constexpr int SSBO_PARTICLES_OUT = 3; + constexpr int UBO_PARTICLES_INFO = 4; constexpr int TEX_ATLAS = 1; }; diff --git a/src/graphics/shader/compile.cpp b/src/graphics/shader/compile.cpp index 19d7a23..bc940bf 100644 --- a/src/graphics/shader/compile.cpp +++ b/src/graphics/shader/compile.cpp @@ -142,3 +142,9 @@ void Graphics::Shader::compile(unsigned int shader, const char* filename) { } } +Graphics::GL::Shader Graphics::Shader::compile_new(int type, const char* filename) { + GL::Shader shader(type); + compile(shader, filename); + return shader; +} + diff --git a/src/graphics/shader/compile.hpp b/src/graphics/shader/compile.hpp index 8c05bd2..e1ac97c 100644 --- a/src/graphics/shader/compile.hpp +++ b/src/graphics/shader/compile.hpp @@ -1,7 +1,10 @@ #pragma once +#include "../gl/shader.hpp" + namespace Graphics::Shader { void compile(unsigned int shader, const char* path); + GL::Shader compile_new(int type, const char* path); }; diff --git a/src/graphics/shader/link.cpp b/src/graphics/shader/link.cpp index b4de3b5..d1d9a75 100644 --- a/src/graphics/shader/link.cpp +++ b/src/graphics/shader/link.cpp @@ -21,3 +21,9 @@ void Graphics::Shader::link(unsigned int program, const std::vector &shaders) { + GL::Program program; + link(program, shaders); + return program; +} + diff --git a/src/graphics/shader/link.hpp b/src/graphics/shader/link.hpp index da04451..1a1d56e 100644 --- a/src/graphics/shader/link.hpp +++ b/src/graphics/shader/link.hpp @@ -2,8 +2,10 @@ #pragma once #include +#include "../gl/program.hpp" namespace Graphics::Shader { void link(unsigned int program, const std::vector& shaders); + GL::Program link_new(const std::vector& shaders); } diff --git a/src/graphics/texture.hpp b/src/graphics/texture.hpp index 7f2be15..252755d 100644 --- a/src/graphics/texture.hpp +++ b/src/graphics/texture.hpp @@ -4,12 +4,14 @@ #include "texture/image.hpp" namespace Graphics::Texture { - inline Image YELLOW_BRICK_WALL {"yellow_brick_wall.png", Image::Edge::CLAMP}; + inline Image MISSING {"missing.png", Image::Edge::REPEATING}; + inline Image WHITE {"white.png", Image::Edge::REPEATING}; + inline Image YELLOW_BRICK_WALL {"yellow_brick_wall.png", Image::Edge::REPEATING}; inline Image* const IMAGES[] { + &MISSING, + &WHITE, &YELLOW_BRICK_WALL, }; - - void generate_atlas(); }; diff --git a/src/graphics/texture/generate_atlas.cpp b/src/graphics/texture/generate_atlas.cpp index afc9f89..f543170 100644 --- a/src/graphics/texture/generate_atlas.cpp +++ b/src/graphics/texture/generate_atlas.cpp @@ -71,13 +71,11 @@ void Graphics::Texture::generate_atlas() { } } - static GL::ArrayBuffer ssbo_id; - static GL::Texture tex_id; - glBindBuffer(GL_SHADER_STORAGE_BUFFER, ssbo_id); - glBufferData(GL_SHADER_STORAGE_BUFFER, 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); 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()); diff --git a/src/graphics/texture/generate_atlas.hpp b/src/graphics/texture/generate_atlas.hpp new file mode 100644 index 0000000..5415051 --- /dev/null +++ b/src/graphics/texture/generate_atlas.hpp @@ -0,0 +1,15 @@ + +#pragma once + +#include "../gl/array_buffer.hpp" +#include "../gl/texture.hpp" + +namespace Graphics::Texture { + struct GeneratedAtlas { + GL::ArrayBuffer m_ssbo_id; + GL::Texture m_tex_id; + }; + + GeneratedAtlas generate_atlas(); +}; + diff --git a/src/graphics/texture/image.cpp b/src/graphics/texture/image.cpp index 9d7d6d1..51e6c84 100644 --- a/src/graphics/texture/image.cpp +++ b/src/graphics/texture/image.cpp @@ -1,6 +1,6 @@ #include "image.hpp" -#include +#include "../../util/debug.hpp" #include #include #include @@ -46,13 +46,13 @@ unsigned char* Image::get(glm::vec<2, int> pos) const { } void Image::clear_host() { - assert(m_data); + CHECK(m_data); free(m_data); m_data = nullptr; } Image::operator unsigned int() { - assert(m_id != -1); + CHECK(m_id != -1); return m_id; } diff --git a/src/graphics/texture/image.hpp b/src/graphics/texture/image.hpp index 8b8b93f..5154aa6 100644 --- a/src/graphics/texture/image.hpp +++ b/src/graphics/texture/image.hpp @@ -6,8 +6,8 @@ namespace Graphics::Texture { struct Image { enum Edge : int { - CLAMP = 0, - REPEATING = 1, + REPEATING = 0, + CLAMP = 1, }; unsigned char* m_data; diff --git a/src/graphics/vertex.cpp b/src/graphics/vertex.cpp index ff786ff..8add702 100644 --- a/src/graphics/vertex.cpp +++ b/src/graphics/vertex.cpp @@ -5,16 +5,17 @@ using Graphics::Vertex; -void Vertex::set_vertex_attribs() { +void Vertex::set_vertex_attribs(unsigned int vao, unsigned int vbo) { Vertex v; - glVertexAttribPointer(0, 4, GL_FLOAT, false, sizeof(v), (void*)Util::pointer_diff(&v, &v.m_pos)); - glVertexAttribPointer(1, 4, GL_FLOAT, false, sizeof(v), (void*)Util::pointer_diff(&v, &v.m_colour)); - glVertexAttribPointer(2, 2, GL_FLOAT, false, sizeof(v), (void*)Util::pointer_diff(&v, &v.m_uv)); - glVertexAttribIPointer(3, 1, GL_UNSIGNED_INT, sizeof(v), (void*)Util::pointer_diff(&v, &v.m_texid)); + 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)); for(int i = 0; i < 4; i++) { - glEnableVertexAttribArray(i); + glVertexArrayAttribBinding(vao, i, 0); } } diff --git a/src/graphics/vertex.hpp b/src/graphics/vertex.hpp index 9b6e2b3..2c51820 100644 --- a/src/graphics/vertex.hpp +++ b/src/graphics/vertex.hpp @@ -9,8 +9,11 @@ namespace Graphics { glm::vec4 m_colour = {1, 1, 1, 1}; glm::vec2 m_uv; unsigned int m_texid = 0; + int _padding_1 = 0; - static void set_vertex_attribs(); + static void set_vertex_attribs(unsigned int vao, unsigned int vbo); }; + + static_assert(sizeof(Vertex) == 48); }; diff --git a/src/util/debug.hpp b/src/util/debug.hpp new file mode 100644 index 0000000..f7b3716 --- /dev/null +++ b/src/util/debug.hpp @@ -0,0 +1,16 @@ + +#pragma once + +#include + +#ifdef NO_DEBUG +#define DEBUG(f) +#define CHECK(f) f +#define CHECK_DEBUG(f) +#else +#define DEBUG(f) f +#define CHECK(f) assert(f) +#define CHECK_DEBUG(f) assert(f) +#endif + + diff --git a/src/util/pointer_diff.cpp b/src/util/pointer_diff.cpp index 3c9c0df..521e828 100644 --- a/src/util/pointer_diff.cpp +++ b/src/util/pointer_diff.cpp @@ -1,12 +1,12 @@ #include "pointer_diff.hpp" -#include +#include "debug.hpp" #include #include uint64_t Util::pointer_diff(void* a, void* b) { - assert(a); - assert(b); + CHECK(a); + CHECK(b); return std::abs((int64_t)a - (int64_t)b); } diff --git a/src/world/builder.cpp b/src/world/builder.cpp index c06240b..e9c6056 100644 --- a/src/world/builder.cpp +++ b/src/world/builder.cpp @@ -27,9 +27,7 @@ Builder::Builder() { mesh.add_primitive(prim); } - m_model.bind(); m_model.set(mesh, GL_STATIC_DRAW); - Graphics::Vertex::set_vertex_attribs(); } void Builder::update(Map& map, const Graphics::Context& ctx) { @@ -92,7 +90,6 @@ void Builder::render(const Graphics::Context& ctx) const { break; } - m_model.bind(); m_model.render(GL_TRIANGLES); } diff --git a/src/world/chunk.cpp b/src/world/chunk.cpp index c31b8a9..7e8a49a 100644 --- a/src/world/chunk.cpp +++ b/src/world/chunk.cpp @@ -19,8 +19,6 @@ constexpr glm::vec<2, int> get_pos_mod(glm::vec<2, int> pos) { Chunk::Chunk(glm::vec<2, int> pos) { m_pos = pos; - m_model.bind(); - Graphics::Vertex::set_vertex_attribs(); } TileBase* Chunk::get(glm::vec<2, int> p) { @@ -81,7 +79,7 @@ void Chunk::update(Map& map) { auto prim = PRIMITIVE_B; prim.m_offset = {t_off, 0, 0}; - prim.m_texid = Graphics::Texture::YELLOW_BRICK_WALL; + prim.m_texid = Graphics::Texture::MISSING; mesh.add_primitive(prim); for(int i = 0; i < std::size(neighbours); i++) { @@ -105,7 +103,6 @@ void Chunk::update(Map& map) { } } } - m_model.bind(); m_model.set(mesh, GL_DYNAMIC_DRAW); m_dirty = false; } @@ -113,8 +110,8 @@ void Chunk::update(Map& map) { void Chunk::render(const Graphics::Context& ctx) const { ctx.set_model_matrix(glm::translate(glm::mat4(1), {glm::vec<2, double>(m_pos) - ctx.m_transform, 0})); ctx.set_colour_matrix(glm::mat4(1)); - m_model.bind(); m_model.render(GL_TRIANGLES); + for(auto& tile : m_tiles) { if(tile) { tile->render();