diff --git a/src/CRE/fbo.cpp b/src/CRE/fbo.cpp index 4d7f774e..7b142958 100644 --- a/src/CRE/fbo.cpp +++ b/src/CRE/fbo.cpp @@ -14,6 +14,9 @@ fbo::~fbo() { free_data(); } +#pragma warning(push) +#pragma warning(disable: 6385) +#pragma warning(disable: 6386) void fbo::init_data(int32_t width, int32_t height, GLuint* color_textures, int32_t count, GLuint depth_texture) { if (buffer) { @@ -58,6 +61,7 @@ void fbo::init_data(int32_t width, int32_t height, glDrawBuffers(count, color_attachments); glCheckFramebufferStatus(GL_FRAMEBUFFER); } +#pragma warning(pop) void fbo::free_data() { if (buffer) { diff --git a/src/CRE/post_process.cpp b/src/CRE/post_process.cpp index 346003ec..aab3d995 100644 --- a/src/CRE/post_process.cpp +++ b/src/CRE/post_process.cpp @@ -51,6 +51,7 @@ screen_x_offset(), screen_y_offset(), screen_width(), screen_height(), mag_filte dof = new post_process_dof(); exposure = new post_process_exposure(); tone_map = new post_process_tone_map(); + transparency = new post_process_transparency(); stage_index = -1; stage_index_prev = -1; @@ -140,6 +141,11 @@ post_process::~post_process() { tone_map = 0; } + if (transparency) { + delete transparency; + transparency = 0; + } + if (mlaa_area_texture) { glDeleteTextures(1, &mlaa_area_texture); mlaa_area_texture = 0; @@ -681,7 +687,11 @@ void post_process::init_fbo(int32_t render_width, int32_t render_height, texture_free(aet_back_tex); aet_back_tex = texture_load_tex_2d(texture_id(0x25, texture_counter++), GL_RGBA8, render_width, render_height, 0, 0, false); aet_back_texture.SetColorDepthTextures(aet_back_tex->tex, 0, rend_texture.GetDepthTex()); - transparency_texture.Init(render_width, render_height, 0, GL_RGBA16F, GL_DEPTH_COMPONENT32F); + transparency_texture.Init(render_width, render_height, 0, GL_RGBA16F, 0); + + transparency->init_fbo(transparency_texture.GetColorTex(), + rend_texture.GetDepthTex(), render_width, render_height); + mlaa_buffer.Init(render_width, render_height, 0, GL_RGBA8, GL_DEPTH_COMPONENT32F); temp_buffer.Init(render_width, render_height, 0, GL_RGBA8, 0); sss_contour_texture = &mlaa_buffer; diff --git a/src/CRE/post_process.hpp b/src/CRE/post_process.hpp index 0a4cb5fd..b3238725 100644 --- a/src/CRE/post_process.hpp +++ b/src/CRE/post_process.hpp @@ -12,6 +12,7 @@ #include "post_process/dof.hpp" #include "post_process/exposure.hpp" #include "post_process/tone_map.hpp" +#include "post_process/transparency.hpp" #include "camera.hpp" enum post_process_frame_texture_type { @@ -73,6 +74,7 @@ struct post_process { post_process_dof* dof; post_process_exposure* exposure; post_process_tone_map* tone_map; + post_process_transparency* transparency; GLuint samplers[3]; GLuint query_vao; GLuint lens_ghost_vao; diff --git a/src/CRE/post_process/transparency.cpp b/src/CRE/post_process/transparency.cpp new file mode 100644 index 00000000..28752f2e --- /dev/null +++ b/src/CRE/post_process/transparency.cpp @@ -0,0 +1,84 @@ +/* + by korenkonder + GitHub/GitLab: korenkonder +*/ + +#include "transparency.hpp" +#include "../../KKdLib/str_utils.hpp" +#include "../rob/rob.hpp" +#include "../gl_state.hpp" +#include "../shader_ft.hpp" + +static void post_process_transparency_free_fbo(post_process_transparency* transparency); + +post_process_transparency::post_process_transparency() : sampler(), vao() { + +} + +post_process_transparency::~post_process_transparency() { + post_process_transparency_free_fbo(this); +} + +void post_process_transparency::combine(RenderTexture* rt, + RenderTexture* buf_rt, float_t alpha, void* rctx) { + transparency_batch_shader_data shader_data = {}; + shader_data.g_opacity = { alpha, 0.0f, 0.0f, 0.0f }; + ((render_context*)rctx)->transparency_batch_ubo.WriteMemory(shader_data); + + gl_state_disable_blend(); + gl_state_disable_depth_test(); + buf_rt->Bind(); + shaders_ft.set(SHADER_FT_TRANSPARENCY); + ((render_context*)rctx)->transparency_batch_ubo.Bind(0); + gl_state_active_bind_texture_2d(0, fbo.textures[0]); + gl_state_active_bind_texture_2d(1, rt->GetColorTex()); + RenderTexture::DrawCustom(); + gl_state_enable_depth_test(); + + fbo::blit(buf_rt->fbos[0], rt->fbos[0], + 0, 0, buf_rt->GetWidth(), buf_rt->GetHeight(), + 0, 0, rt->GetWidth(), rt->GetHeight(), + GL_COLOR_BUFFER_BIT, GL_NEAREST); +} + +void post_process_transparency::copy(RenderTexture* rt) { + fbo::blit(rt->fbos[0], fbo.buffer, + 0, 0, rt->GetWidth(), rt->GetHeight(), + 0, 0, fbo.width, fbo.height, + GL_COLOR_BUFFER_BIT, GL_NEAREST); +} + +void post_process_transparency::init_fbo(GLuint color_texture, GLuint depth_texture, int32_t width, int32_t height) { + if (fbo.textures && (fbo.textures[0] == color_texture + && fbo.textures[1] == depth_texture + && fbo.width == width && fbo.height == height)) + return; + + post_process_transparency_free_fbo(this); + + GLuint color_textures[1]; + color_textures[0] = color_texture; + fbo.init_data(width, height, color_textures, 1, depth_texture); + + glGenSamplers(1, &sampler); + glSamplerParameteri(sampler, GL_TEXTURE_MAG_FILTER, GL_NEAREST); + glSamplerParameteri(sampler, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST); + glSamplerParameteri(sampler, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); + glSamplerParameteri(sampler, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); + + glGenVertexArrays(1, &vao); +} + +static void post_process_transparency_free_fbo(post_process_transparency* transparency) { + if (transparency->vao) { + glDeleteVertexArrays(1, &transparency->vao); + transparency->vao = 0; + } + + if (transparency->sampler) { + glDeleteSamplers(1, &transparency->sampler); + transparency->sampler = 0; + } + + transparency->fbo.free_data(); +} diff --git a/src/CRE/post_process/transparency.hpp b/src/CRE/post_process/transparency.hpp new file mode 100644 index 00000000..e5cf06e1 --- /dev/null +++ b/src/CRE/post_process/transparency.hpp @@ -0,0 +1,25 @@ +/* + by korenkonder + GitHub/GitLab: korenkonder +*/ + +#pragma once + +#include "../../KKdLib/default.hpp" +#include "../../KKdLib/vec.hpp" +#include "../fbo.hpp" +#include "../render_texture.hpp" +#include "../shared.hpp" + +struct post_process_transparency { + fbo fbo; + GLuint sampler; + GLuint vao; + + post_process_transparency(); + ~post_process_transparency(); + + void combine(RenderTexture* rt, RenderTexture* buf_rt, float_t alpha, void* rctx); + void copy(RenderTexture* rt); + void init_fbo(GLuint color_texture, GLuint depth_texture, int32_t width, int32_t height); +}; diff --git a/src/CRE/render_manager.cpp b/src/CRE/render_manager.cpp index a91bf28f..8de5ea8e 100644 --- a/src/CRE/render_manager.cpp +++ b/src/CRE/render_manager.cpp @@ -1465,11 +1465,7 @@ static void draw_pass_3d_translucent(render_context* rctx, bool opaque_enable, alpha_array, opaque, transparent, translucent); for (int32_t i = 0; i < count; i++) { int32_t alpha = alpha_array[i]; - fbo::blit(pp->rend_texture.fbos[0], pp->transparency_texture.fbos[0], - 0, 0, pp->render_width, pp->render_height, - 0, 0, pp->render_width, pp->render_height, - GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT, GL_NEAREST); - + pp->transparency->copy(&pp->rend_texture); if (opaque_enable && rctx->disp_manager.get_obj_count(opaque)) rctx->disp_manager.draw_translucent(opaque, alpha); if (transparent_enable && rctx->disp_manager.get_obj_count(transparent)) @@ -1479,22 +1475,8 @@ static void draw_pass_3d_translucent(render_context* rctx, bool opaque_enable, rctx->disp_manager.draw_translucent(translucent, alpha); gl_state_disable_blend(); } - - transparency_batch_shader_data shader_data = {}; - shader_data.g_opacity = { (float_t)alpha * (float_t)(1.0 / 255.0), 0.0f, 0.0f, 0.0f }; - rctx->transparency_batch_ubo.WriteMemory(shader_data); - - pp->buf_texture.Bind(); - shaders_ft.set(SHADER_FT_TRANSPARENCY); - rctx->transparency_batch_ubo.Bind(0); - gl_state_active_bind_texture_2d(0, pp->transparency_texture.color_texture->tex); - gl_state_active_bind_texture_2d(1, pp->rend_texture.color_texture->tex); - RenderTexture::DrawCustom(); - - fbo::blit(pp->buf_texture.fbos[0], pp->rend_texture.fbos[0], - 0, 0, pp->render_width, pp->render_height, - 0, 0, pp->render_width, pp->render_height, - GL_COLOR_BUFFER_BIT, GL_NEAREST); + pp->transparency->combine(&pp->rend_texture, &pp->buf_texture, + (float_t)alpha * (float_t)(1.0 / 255.0), rctx); } }