From 3518e9ce80ccd6e1265993b1ebb9a632ef21f972 Mon Sep 17 00:00:00 2001 From: korenkonder Date: Tue, 28 Nov 2023 22:36:23 +0300 Subject: [PATCH] Replaced glTexParameter with samplers when possible --- src/CRE/post_process.cpp | 36 ++++++++++++++++++++----------- src/CRE/post_process.hpp | 2 +- src/CRE/post_process/tone_map.cpp | 17 +++++---------- src/CRE/render_manager.cpp | 28 ++++++------------------ src/CRE/task_effect.cpp | 23 ++++++++++++++------ 5 files changed, 54 insertions(+), 52 deletions(-) diff --git a/src/CRE/post_process.cpp b/src/CRE/post_process.cpp index 15bd4623..346003ec 100644 --- a/src/CRE/post_process.cpp +++ b/src/CRE/post_process.cpp @@ -54,15 +54,28 @@ screen_x_offset(), screen_y_offset(), screen_width(), screen_height(), mag_filte stage_index = -1; stage_index_prev = -1; - glGenSamplers(2, samplers); - glSamplerParameteri(samplers[0], GL_TEXTURE_MAG_FILTER, GL_LINEAR); - glSamplerParameteri(samplers[0], GL_TEXTURE_MIN_FILTER, GL_LINEAR); - glSamplerParameteri(samplers[0], GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); - glSamplerParameteri(samplers[0], GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); - glSamplerParameteri(samplers[1], GL_TEXTURE_MAG_FILTER, GL_NEAREST); - glSamplerParameteri(samplers[1], GL_TEXTURE_MIN_FILTER, GL_NEAREST); - glSamplerParameteri(samplers[1], GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); - glSamplerParameteri(samplers[1], GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); + GLuint sampler; + glGenSamplers(3, samplers); + sampler = samplers[0]; + glSamplerParameteri(sampler, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + glSamplerParameteri(sampler, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + glSamplerParameteri(sampler, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); + glSamplerParameteri(sampler, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); + + sampler = samplers[1]; + glSamplerParameteri(sampler, GL_TEXTURE_MAG_FILTER, GL_NEAREST); + glSamplerParameteri(sampler, GL_TEXTURE_MIN_FILTER, GL_NEAREST); + glSamplerParameteri(sampler, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); + glSamplerParameteri(sampler, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); + + static const vec4 border_color = 0.0f; + + sampler = samplers[2]; + glSamplerParameteri(sampler, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + glSamplerParameteri(sampler, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + glSamplerParameteri(sampler, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER); + glSamplerParameteri(sampler, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER); + glSamplerParameterfv(sampler, GL_TEXTURE_BORDER_COLOR, (GLfloat*)&border_color); glGenVertexArrays(1, &query_vao); @@ -133,7 +146,7 @@ post_process::~post_process() { } if (samplers[0]) { - glDeleteSamplers(2, samplers); + glDeleteSamplers(3, samplers); samplers[0] = 0; } @@ -794,8 +807,7 @@ void post_process::get_frame_texture(GLuint tex, post_process_frame_texture_type texture* dst_tex = j.texture; gl_state_active_bind_texture_2d(0, tex); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + gl_state_bind_sampler(0, samplers[0]); glViewport(0, 0, dst_tex->width, dst_tex->height); uniform_value[U_REDUCE] = 0; shaders_ft.set(SHADER_FT_REDUCE); diff --git a/src/CRE/post_process.hpp b/src/CRE/post_process.hpp index 996bbb6e..0a4cb5fd 100644 --- a/src/CRE/post_process.hpp +++ b/src/CRE/post_process.hpp @@ -73,7 +73,7 @@ struct post_process { post_process_dof* dof; post_process_exposure* exposure; post_process_tone_map* tone_map; - GLuint samplers[2]; + GLuint samplers[3]; GLuint query_vao; GLuint lens_ghost_vao; GLuint lens_ghost_vbo; diff --git a/src/CRE/post_process/tone_map.cpp b/src/CRE/post_process/tone_map.cpp index fcb55f99..cfc3cfa9 100644 --- a/src/CRE/post_process/tone_map.cpp +++ b/src/CRE/post_process/tone_map.cpp @@ -117,9 +117,7 @@ void post_process_tone_map::apply(RenderTexture* in_tex, texture* light_proj_tex uniform_value[U_FLARE] = 1; gl_state_active_bind_texture_2d(4, pp->lens_flare_texture->tex); - glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, (GLfloat*)&border_color); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER); + gl_state_bind_sampler(4, pp->samplers[2]); mat4 mat; mat4_translate(0.5f, 0.5f, 0.0f, &mat); mat4_scale_rot(&mat, 0.75f, 0.75f, 1.0f, &mat); @@ -136,9 +134,7 @@ void post_process_tone_map::apply(RenderTexture* in_tex, texture* light_proj_tex if (pp->lens_shaft_scale < 50.0f) { uniform_value[U_FLARE] = 2; gl_state_active_bind_texture_2d(5, pp->lens_shaft_texture->tex); - glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, (GLfloat*)&border_color); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER); + gl_state_bind_sampler(5, pp->samplers[2]); mat4_translate(0.5f, 0.5f, 0.0f, &mat); mat4_scale_rot(&mat, pp->lens_shaft_scale, pp->lens_shaft_scale, 1.0f, &mat); mat4_mul_rotate_z(&mat, (pp->lens_flare_pos.x / (float_t)pp->render_width) @@ -165,14 +161,11 @@ void post_process_tone_map::apply(RenderTexture* in_tex, texture* light_proj_tex if (npr_param == 1) { /*gl_state_active_bind_texture_2d(16, contour_rt->GetColorTex()); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); + gl_state_bind_sampler(16, pp->samplers[1]); gl_state_active_bind_texture_2d(17, contour_rt->GetDepthTex()); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);*/ + gl_state_bind_sampler(17, pp->samplers[1]);*/ gl_state_active_bind_texture_2d(14, rt->GetDepthTex()); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); + gl_state_bind_sampler(14, pp->samplers[1]); } tone_map_ubo.WriteMemory(shader_data); diff --git a/src/CRE/render_manager.cpp b/src/CRE/render_manager.cpp index 57bfb25d..a91bf28f 100644 --- a/src/CRE/render_manager.cpp +++ b/src/CRE/render_manager.cpp @@ -856,14 +856,11 @@ namespace rndr { shaders_ft.set(SHADER_FT_CONTOUR_NPR); gl_state_active_bind_texture_2d(16, contour_rt->GetColorTex()); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); + gl_state_bind_sampler(16, rctx->post_process.samplers[1]); gl_state_active_bind_texture_2d(17, contour_rt->GetDepthTex()); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); + gl_state_bind_sampler(17, rctx->post_process.samplers[1]); gl_state_active_bind_texture_2d(14, rt->GetDepthTex()); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); + gl_state_bind_sampler(14, rctx->post_process.samplers[1]); rctx->quad_ubo.Bind(0); rctx->contour_params_ubo.Bind(2); RenderTexture::Draw(&shaders_ft); @@ -1158,16 +1155,11 @@ static bool draw_pass_shadow_litproj(render_context* rctx, light_proj* litproj) return false; } - static const vec4 border_color = 0.0f; rctx->draw_state.shader_index = SHADER_FT_LITPROJ; gl_state_active_bind_texture_2d(17, tex->tex); - glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, (GLfloat*)&border_color); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER); + gl_state_bind_sampler(17, rctx->post_process.samplers[2]); gl_state_active_bind_texture_2d(18, litproj->shadow_texture[0].GetColorTex()); - glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, (GLfloat*)&border_color); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER); + gl_state_bind_sampler(18, rctx->post_process.samplers[2]); gl_state_active_texture(0); return true; } @@ -1180,15 +1172,9 @@ static void draw_pass_sss_contour(render_context* rctx, post_process* pp) { gl_state_set_depth_mask(GL_TRUE); glViewport(0, 0, pp->render_width, pp->render_height); gl_state_active_bind_texture_2d(0, pp->rend_texture.GetColorTex()); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); + gl_state_bind_sampler(0, rctx->post_process.samplers[1]); gl_state_active_bind_texture_2d(1, pp->rend_texture.GetDepthTex()); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); + gl_state_bind_sampler(1, rctx->post_process.samplers[1]); gl_state_active_texture(0); camera* cam = rctx->camera; diff --git a/src/CRE/task_effect.cpp b/src/CRE/task_effect.cpp index 73c72621..bca86055 100644 --- a/src/CRE/task_effect.cpp +++ b/src/CRE/task_effect.cpp @@ -157,6 +157,7 @@ struct star_catalog_milky_way { GLuint vbo; GLuint ebo; GLuint vao; + GLuint sampler; uint16_t restart_index; int32_t idx_count; int32_t longitude_degs_10; @@ -3323,8 +3324,8 @@ particle_data::particle_data() : size(), alpha(), life_time() { } -star_catalog_milky_way::star_catalog_milky_way() : vbo(), ebo(), vao(), restart_index(), -idx_count(), longitude_degs_10(), latitude_degs_10(), longitude_offset_degs_10(), +star_catalog_milky_way::star_catalog_milky_way() : vbo(), ebo(), vao(), sampler(), +restart_index(), idx_count(), longitude_degs_10(), latitude_degs_10(), longitude_offset_degs_10(), latitude_offset_degs_10(), latitude(), longitude(), uv_rec_scale_u(), uv_rec_scale_v() { reset(); } @@ -3446,6 +3447,13 @@ void star_catalog_milky_way::create_buffers(int32_t subdivs, float_t uv_rec_scal (GLsizeiptr)(sizeof(uint16_t) * ebo_count), ebo_data, GL_STATIC_DRAW); free_def(ebo_data); + if (!sampler) + glGenSamplers(1, &sampler); + glSamplerParameteri(sampler, GL_TEXTURE_MIN_FILTER, GL_NEAREST); + glSamplerParameteri(sampler, GL_TEXTURE_MAG_FILTER, GL_NEAREST); + glSamplerParameteri(sampler, GL_TEXTURE_WRAP_S, GL_REPEAT); + glSamplerParameteri(sampler, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); + gl_state_bind_vertex_array(0); gl_state_bind_array_buffer(0); gl_state_bind_element_array_buffer(0); @@ -3457,6 +3465,11 @@ void star_catalog_milky_way::create_default_sphere() { } void star_catalog_milky_way::delete_buffers() { + if (sampler) { + glDeleteSamplers(1, &sampler); + sampler = 0; + } + if (vbo) { glDeleteBuffers(1, &vbo); vbo = 0; @@ -3521,12 +3534,9 @@ void star_catalog_milky_way::draw(const mat4& vp, const mat4& mat, texture* tex, gl_state_disable_blend(); gl_state_set_depth_mask(GL_FALSE); gl_state_active_bind_texture_2d(0, tex->tex); + gl_state_bind_sampler(0, sampler); gl_state_enable_primitive_restart(); gl_state_set_primitive_restart_index(restart_index); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); uniform_value[U_STAR] = 1; shaders_ft.set(SHADER_FT_STAR); @@ -3535,6 +3545,7 @@ void star_catalog_milky_way::draw(const mat4& vp, const mat4& mat, texture* tex, shaders_ft.draw_elements(GL_TRIANGLE_STRIP, idx_count, GL_UNSIGNED_SHORT, 0); gl_state_bind_vertex_array(0); + gl_state_bind_sampler(0, 0); gl_state_active_bind_texture_2d(0, 0); gl_state_disable_primitive_restart(); gl_state_set_depth_mask(GL_TRUE);