diff --git a/CMakeLists.txt b/CMakeLists.txt index 9b52558..920cac6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,6 @@ cmake_minimum_required(VERSION 3.25) -set(BLT_GRAPHICS_VERSION 0.13.6) +set(BLT_GRAPHICS_VERSION 0.13.7) set(BLT_GRAPHICS_TEST_VERSION 0.0.1) project(BLT_WITH_GRAPHICS VERSION ${BLT_GRAPHICS_VERSION}) diff --git a/include/blt/gfx/renderer/postprocess.h b/include/blt/gfx/renderer/postprocess.h index 27c4c05..6e45a54 100644 --- a/include/blt/gfx/renderer/postprocess.h +++ b/include/blt/gfx/renderer/postprocess.h @@ -94,12 +94,19 @@ namespace blt::gfx pp_in_place_t(frame_buffer_t::attachment_t from, frame_buffer_t::attachment_t to): from(from), to(to) {} + void draw(frame_buffer_t& previous) override; + + void post_draw(frame_buffer_t& previous) override; + + void create() override; + bool requests(blt::gfx::pp_request_t request) override { return static_cast(request) & ~(static_cast(pp_request_t::BIND_BUFFER) | static_cast(pp_request_t::CLEAR_BUFFER)); } protected: + std::unique_ptr shader_pass; frame_buffer_t::attachment_t from; frame_buffer_t::attachment_t to; }; @@ -142,76 +149,37 @@ namespace blt::gfx matrix_state_manager& manager; }; - class pp_blur_step_base_t : public pp_step_t - { - public: - pp_blur_step_base_t(frame_buffer_t::attachment_t from, i32 x_blur = 2, i32 y_blur = 2): - from(from), to(from), x_blur(x_blur), y_blur(y_blur) - {} - - pp_blur_step_base_t(frame_buffer_t::attachment_t from, frame_buffer_t::attachment_t to, i32 x_blur = 2, i32 y_blur = 2): - from(from), to(to), x_blur(x_blur), y_blur(y_blur) - {} - - protected: - frame_buffer_t::attachment_t from; - frame_buffer_t::attachment_t to; - i32 x_blur; - i32 y_blur; - }; - - class pp_blur_step_t : public pp_blur_step_base_t - { - public: - pp_blur_step_t(frame_buffer_t::attachment_t from, i32 x_blur = 2, i32 y_blur = 2): pp_blur_step_base_t(from, x_blur, y_blur) - {} - - pp_blur_step_t(frame_buffer_t::attachment_t from, frame_buffer_t::attachment_t to, i32 x_blur = 2, i32 y_blur = 2): - pp_blur_step_base_t(from, to, x_blur, y_blur) - {} - - void create() override; - - void draw(frame_buffer_t& previous); - }; - - class pp_blur_step_inplace_t : public pp_blur_step_base_t + class pp_blur_step_inplace_t : public pp_in_place_t { public: pp_blur_step_inplace_t(matrix_state_manager& manager, frame_buffer_t::attachment_t from, i32 x_blur = 2, i32 y_blur = 2): - pp_blur_step_base_t(from, x_blur, y_blur), manager(manager) + pp_in_place_t(from), manager(manager), x_blur(x_blur), y_blur(y_blur) {} pp_blur_step_inplace_t(matrix_state_manager& manager, frame_buffer_t::attachment_t from, frame_buffer_t::attachment_t to, i32 x_blur = 2, i32 y_blur = 2): - pp_blur_step_base_t(from, to, x_blur, y_blur), manager(manager) + pp_in_place_t(from, to), manager(manager), x_blur(x_blur), y_blur(y_blur) {} void create() override; void draw(frame_buffer_t& previous); - - void post_draw(frame_buffer_t& previous) override; - - bool requests(blt::gfx::pp_request_t request) override - { - return static_cast(request) & ~(static_cast(pp_request_t::BIND_BUFFER) | static_cast(pp_request_t::CLEAR_BUFFER)); - } private: - std::unique_ptr shader_pass; matrix_state_manager& manager; + i32 x_blur; + i32 y_blur; }; class pp_expansion_step_inplace_t : public pp_in_place_t { public: - pp_expansion_step_inplace_t(frame_buffer_t::attachment_t from, const vec4& multiplier = vec4{2,2,2,2}): + pp_expansion_step_inplace_t(frame_buffer_t::attachment_t from, const vec4& multiplier = vec4{2, 2, 2, 2}): pp_in_place_t(from), multiplier(multiplier) {} pp_expansion_step_inplace_t(frame_buffer_t::attachment_t from, frame_buffer_t::attachment_t to, - const vec4& multiplier = vec4{2,2,2,2}): + const vec4& multiplier = vec4{2, 2, 2, 2}): pp_in_place_t(from, to), multiplier(multiplier) {} diff --git a/src/blt/gfx/framebuffer.cpp b/src/blt/gfx/framebuffer.cpp index a1d4a69..f68cd95 100644 --- a/src/blt/gfx/framebuffer.cpp +++ b/src/blt/gfx/framebuffer.cpp @@ -154,6 +154,8 @@ namespace blt::gfx { if (width == width_ && height == height_) return; + if (fboID == 0) + return; width_ = width; height_ = height; @@ -208,7 +210,7 @@ namespace blt::gfx //glReadBuffer(GL_COLOR_ATTACHMENT0); //glDrawBuffer(GL_COLOR_ATTACHMENT0); glReadBuffer(static_cast(attachment_t::COLOR0)); - GLenum buffers[] {static_cast(attachment_t::COLOR0)}; + GLenum buffers[] {GL_BACK}; glDrawBuffers(1, buffers); glBlitFramebuffer(0, 0, width_, height_, 0, 0, width, height, GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT, GL_NEAREST); } diff --git a/src/blt/gfx/renderer/postprocess.cpp b/src/blt/gfx/renderer/postprocess.cpp index b5ed8a3..093dd20 100644 --- a/src/blt/gfx/renderer/postprocess.cpp +++ b/src/blt/gfx/renderer/postprocess.cpp @@ -137,6 +137,41 @@ namespace blt::gfx glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0); } + void pp_in_place_t::create() + { + shader_pass = pp_engine_t::createShader(shader_pp_screen_frag); + draw_buffer = frame_buffer_t::make_render_texture(getWindowWidth(), getWindowHeight()); + } + + void pp_in_place_t::post_draw(frame_buffer_t& previous) + { + //draw_buffer.blitTexture(previous, 0, 0, 0, 0, GL_NEAREST, from, to); + previous.bind(); + GLenum buf[32]; + auto buff_val = static_cast(to); + auto size = buff_val - GL_COLOR_ATTACHMENT0; + for (GLenum i = 0; i < size; i++) + buf[i] = GL_NONE; + buf[size] = buff_val; + glDrawBuffers(static_cast(size) + 1, buf); + glClear(GL_COLOR_BUFFER_BIT); + shader_pass->bind(); + glActiveTexture(GL_TEXTURE0); + draw_buffer.getTexture(frame_buffer_t::attachment_t::COLOR0).bind(); + pp_engine_t::render_quad(); + } + + void pp_in_place_t::draw(frame_buffer_t& previous) + { + draw_buffer.updateBuffersStorage(getWindowWidth(), getWindowHeight()); + glActiveTexture(GL_TEXTURE0); + previous.getTexture(from).bind(); + draw_buffer.bind(); + GLenum buf[]{static_cast(frame_buffer_t::attachment_t::COLOR0)}; + glDrawBuffers(1, buf); + glClear(GL_COLOR_BUFFER_BIT); + } + void pp_to_screen_step_t::create() { shader = pp_engine_t::createShader(shader_pp_screen_frag); @@ -146,7 +181,7 @@ namespace blt::gfx { glActiveTexture(GL_TEXTURE0); previous.getTexture(frame_buffer_t::attachment_t::COLOR0).bind(); - GLenum buf[]{static_cast(frame_buffer_t::attachment_t::COLOR0)}; + GLenum buf[]{GL_BACK}; glDrawBuffers(1, buf); } @@ -204,78 +239,38 @@ namespace blt::gfx glActiveTexture(GL_TEXTURE1); previous.getTexture(frame_buffer_t::attachment_t::COLOR1).bind(); shader->bind(); - auto v = vec2(getWindowWidth(), getWindowHeight()) * (1.0 / blt::make_vec2(manager.getScale2D())); + //auto v = vec2(getWindowWidth(), getWindowHeight()) * (1.0 / blt::make_vec2(manager.getScale2D())); //BLT_TRACE_STREAM << v << '\n'; //shader->setVec2("viewportSize", static_cast(v.x()), static_cast(v.y())); GLenum buf[]{static_cast(frame_buffer_t::attachment_t::COLOR0)}; glDrawBuffers(1, buf); } - void pp_blur_step_t::create() - { - draw_buffer = frame_buffer_t::make_render_texture(getWindowWidth(), getWindowHeight()); - shader = pp_engine_t::createShader(shader_gaussian_blur_frag); - } - - void pp_blur_step_t::draw(frame_buffer_t& previous) - { - draw_buffer.updateBuffersStorage(getWindowWidth(), getWindowHeight()); - shader->bind(); - shader->setVec4i("size", {getWindowWidth(), getWindowHeight(), x_blur, y_blur}); - glActiveTexture(GL_TEXTURE0); - previous.getTexture(from).bind(); - GLenum buf[]{static_cast(to)}; - glDrawBuffers(1, buf); - } - void pp_blur_step_inplace_t::create() { - draw_buffer = frame_buffer_t::make_render_texture(getWindowWidth(), getWindowHeight()); + pp_in_place_t::create(); shader = pp_engine_t::createShader(shader_gaussian_blur_frag); - shader_pass = pp_engine_t::createShader(shader_pp_screen_frag); } void pp_blur_step_inplace_t::draw(frame_buffer_t& previous) { - draw_buffer.updateBuffersStorage(getWindowWidth(), getWindowHeight()); + pp_in_place_t::draw(previous); shader->bind(); //auto v = vec2(getWindowWidth(), getWindowHeight()) * (1.0 / blt::make_vec2(manager.getScale2D())); auto v = vec2(getWindowWidth(), getWindowHeight()); shader->setVec4i("size", {static_cast(v.x()), static_cast(v.y()), x_blur, y_blur}); - glActiveTexture(GL_TEXTURE0); - previous.getTexture(from).bind(); - //GLenum buf[]{static_cast(to)}; - GLenum buf[]{static_cast(frame_buffer_t::attachment_t::COLOR0)}; - glDrawBuffers(1, buf); - draw_buffer.bind(); - glClear(GL_COLOR_BUFFER_BIT); - } - - void pp_blur_step_inplace_t::post_draw(frame_buffer_t& previous) - { - //draw_buffer.blitTexture(previous, 0, 0, 0, 0, GL_NEAREST, from, to); - previous.bind(); - GLenum buf[]{static_cast(to)}; - glDrawBuffers(1, buf); - glClear(GL_COLOR_BUFFER_BIT); - shader_pass->bind(); - glActiveTexture(GL_TEXTURE0); - draw_buffer.getTexture(frame_buffer_t::attachment_t::COLOR0).bind(); - pp_engine_t::render_quad(); } void pp_expansion_step_inplace_t::create() { + pp_in_place_t::create(); shader = pp_engine_t::createShader(shader_multiplier_frag); } void pp_expansion_step_inplace_t::draw(frame_buffer_t& previous) { + pp_in_place_t::draw(previous); shader->bind(); shader->setVec4("multiplier", multiplier); - glActiveTexture(GL_TEXTURE0); - previous.getTexture(from).bind(); - GLenum buf[]{static_cast(to)}; - glDrawBuffers(1, buf); } } \ No newline at end of file