From 2dd53bc97beae51ec8abf8475492a8d7f7f925c4 Mon Sep 17 00:00:00 2001 From: pwarszawski Date: Sat, 14 Nov 2026 17:46:40 +0100 Subject: [PATCH] Make the glare of a FreeSpotLight as bright as the part of its point seen The glare was drawn whole or not at all, depending on whether any sample of the light point passed the depth test. The point is a few pixels wide, so where it sits at the edge of something in front of it (the hood of the lamp, a mast, a wire) the large glare switched on and off as the camera moved and the signal seemed to flash. The opengl renderer now counts the samples of the point which are seen and, drawing the point once more without the depth test and without colour, all of its samples. The gpu writes both counts into a small storage buffer (GLARE_SSBO_BINDING) and billboard.frag scales the glare by their ratio, so a partly hidden lamp gets a fainter glare instead of a flashing one. The conditional render still leaves out the glare of a point not seen at all. The vulkan renderer is unchanged. --- gl/glsl_common.cpp | 1 + gl/ubo.h | 4 +++ rendering/common/scenewalk.cpp | 1 + rendering/common/scenewalk.h | 6 ++++- rendering/opengl46/opengl46renderer.cpp | 35 ++++++++++++++++++++++++- rendering/opengl46/opengl46renderer.h | 8 ++++++ shaders/ogl46/billboard.frag | 12 +++++++++ shaders/ogl46/common.glsl | 1 + 8 files changed, 66 insertions(+), 2 deletions(-) diff --git a/gl/glsl_common.cpp b/gl/glsl_common.cpp index f6f3df06d..9a5c9a895 100644 --- a/gl/glsl_common.cpp +++ b/gl/glsl_common.cpp @@ -110,6 +110,7 @@ void gl::glsl_common_setup() "#define SKY_OCCLUSION_HEIGHT_TEX " + std::to_string(SKY_OCCLUSION_HEIGHT_TEX) + "\n" + "#define SKY_OCCLUSION_TEX " + std::to_string(SKY_OCCLUSION_TEX) + "\n" + "#define CAB_PROBE_SSBO_BINDING " + std::to_string(CAB_PROBE_SSBO_BINDING) + "\n" + + "#define GLARE_SSBO_BINDING " + std::to_string(GLARE_SSBO_BINDING) + "\n" + "#define CAB_PROBE_SH_SIZE " + std::to_string(CAB_PROBE_SH_SIZE) + "\n" + "#define CAB_PROBE_SH_GROUP_SIZE " + std::to_string(CAB_PROBE_SH_GROUP_SIZE) + "\n" + "#define WEATHER_UBO_BINDING " + std::to_string(WEATHER_UBO_BINDING) + "\n" + diff --git a/gl/ubo.h b/gl/ubo.h index c933cf523..83a6ac71d 100644 --- a/gl/ubo.h +++ b/gl/ubo.h @@ -446,6 +446,10 @@ namespace gl }; // binding point of the opengl renderer, bound with the colour passes const GLuint CAB_PROBE_SSBO_BINDING = 17; + // layout std430: the samples of the point of a lamp its occlusion queries counted, written there by the gpu (GLSL: glare_ssbo, + // read by billboard.frag): seen, which passed the depth test, and all, drawn without it. binding point of the opengl renderer, + // bound with the glare of a lamp + const GLuint GLARE_SSBO_BINDING = 18; // the face of the level of the probe the spherical harmonics are taken from, at most, and the work group of cabprobe_sh.comp const size_t CAB_PROBE_SH_SIZE = 16; // the face of the level of the probe CAB_PROBE_MASK_TEX is a copy of, at most diff --git a/rendering/common/scenewalk.cpp b/rendering/common/scenewalk.cpp index ced5318af..d75bb40ca 100644 --- a/rendering/common/scenewalk.cpp +++ b/rendering/common/scenewalk.cpp @@ -3276,6 +3276,7 @@ void scene_walk::Render_Alpha(TSubModel *Submodel) walk_draw(Submodel->m_geometry.handle); walk_light_query(Submodel, false); + walk_light_reference(Submodel->m_geometry.handle); pointdrawn = true; // post-draw reset diff --git a/rendering/common/scenewalk.h b/rendering/common/scenewalk.h index ac22f7d99..697e1ae68 100644 --- a/rendering/common/scenewalk.h +++ b/rendering/common/scenewalk.h @@ -267,10 +267,14 @@ class scene_walk virtual void walk_cull_face(bool const Enabled) = 0; // the glare of a lamp: drawn without the depth test, added to what is there; drawn only where the point of the light was // seen (walk_light_query() of this point drawn just before it, when the backend has it: the opengl renderer uses - // the query at once, the vulkan renderer the result of the last frame) + // the query at once and makes the glare as bright as the part of the point seen, the vulkan renderer uses the result of the + // last frame) virtual void walk_glare(bool const Enabled) = 0; virtual void walk_glare_draw(TSubModel *Submodel, gfx::geometry_handle const &Geometry) = 0; virtual void walk_light_query(TSubModel *Submodel, bool const Begin) = 0; + // the point of the light drawn once more right after walk_light_query(), unseen and whole, for the backend which makes the + // glare as bright as the part of the point seen (the opengl renderer); by default nothing + virtual void walk_light_reference(gfx::geometry_handle const &Geometry) {} // what the backend may draw itself (the gpu culling of the opengl renderer); by default the walk draws everything // the start of the walk of a region virtual void walk_region_begin() {} diff --git a/rendering/opengl46/opengl46renderer.cpp b/rendering/opengl46/opengl46renderer.cpp index 4e45137a5..e6a2854c6 100644 --- a/rendering/opengl46/opengl46renderer.cpp +++ b/rendering/opengl46/opengl46renderer.cpp @@ -5058,6 +5058,11 @@ void opengl46_renderer::walk_glare(bool const Enabled) void opengl46_renderer::walk_glare_draw(TSubModel *Submodel, gfx::geometry_handle const &Geometry) { + // billboard.frag scales the glare by the part of the point seen, when the samples were counted for it + auto const parameter{model_ubs.param[1].w}; + model_ubs.param[1].w = (m_glaresamples ? 1.f : 0.f); + if (m_glaresamples) + m_glaressbo->bind_base(); if (Submodel->occlusion_query) { glBeginConditionalRender(*Submodel->occlusion_query, GL_QUERY_WAIT); draw(Geometry); @@ -5065,6 +5070,8 @@ void opengl46_renderer::walk_glare_draw(TSubModel *Submodel, gfx::geometry_handl } else draw(Geometry); + model_ubs.param[1].w = parameter; + m_glaresamples = false; } void opengl46_renderer::walk_light_query(TSubModel *Submodel, bool const Begin) @@ -5072,14 +5079,40 @@ void opengl46_renderer::walk_light_query(TSubModel *Submodel, bool const Begin) if (false == gl::vao::use_vao) return; if (Begin) { + m_glaresamples = false; if (!Submodel->occlusion_query) - Submodel->occlusion_query.emplace(gl::query::ANY_SAMPLES_PASSED); + Submodel->occlusion_query.emplace(gl::query::SAMPLES_PASSED); Submodel->occlusion_query->begin(); + m_glarequerysubmodel = Submodel; } else Submodel->occlusion_query->end(); } +void opengl46_renderer::walk_light_reference(gfx::geometry_handle const &Geometry) +{ + if ((false == gl::vao::use_vao) || (m_glarequerysubmodel == nullptr) || (false == m_glarequerysubmodel->occlusion_query.has_value())) + return; + auto const &seen{*m_glarequerysubmodel->occlusion_query}; + m_glarequerysubmodel = nullptr; + if (!m_glarequery) + m_glarequery.emplace(gl::query::SAMPLES_PASSED); + if (!m_glaressbo) + m_glaressbo = std::make_unique(static_cast(2 * sizeof(GLuint)), gl::GLARE_SSBO_BINDING); + // the whole point, unseen: what the samples seen are compared with + glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE); + glDisable(GL_DEPTH_TEST); + m_glarequery->begin(); + draw(Geometry); + m_glarequery->end(); + glEnable(GL_DEPTH_TEST); + glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); + // the gpu writes the counts when they're done, no wait here + glGetQueryBufferObjectuiv(seen, *m_glaressbo, GL_QUERY_RESULT, 0); + glGetQueryBufferObjectuiv(*m_glarequery, *m_glaressbo, GL_QUERY_RESULT, static_cast(sizeof(GLuint))); + m_glaresamples = true; +} + void opengl46_renderer::walk_region_begin() { // the gpu culling runs along the first color pass of the main viewport of a frame, and the passes of the cascades of the diff --git a/rendering/opengl46/opengl46renderer.h b/rendering/opengl46/opengl46renderer.h index a49c8dd5a..fa46ef3c0 100644 --- a/rendering/opengl46/opengl46renderer.h +++ b/rendering/opengl46/opengl46renderer.h @@ -228,6 +228,7 @@ class opengl46_renderer : public gfx_renderer, public gfx::scene_walk { void walk_glare(bool const Enabled) override; void walk_glare_draw(TSubModel *Submodel, gfx::geometry_handle const &Geometry) override; void walk_light_query(TSubModel *Submodel, bool const Begin) override; + void walk_light_reference(gfx::geometry_handle const &Geometry) override; // the gpu culling of the instances, models and sleepers, the depth pass of the whole scene void walk_region_begin() override; void walk_editor_cursor() override; @@ -848,6 +849,13 @@ class opengl46_renderer : public gfx_renderer, public gfx::scene_walk { unsigned int m_editor_overlay_revision{0}; std::unique_ptr m_freespot_shader; std::unique_ptr m_billboard_shader; + // the glare of a lamp as bright as the part of its point seen: the submodel of the point whose query is under way + // (walk_light_query()), the samples of the point drawn without the depth test (walk_light_reference()), the samples counted + // for the point (seen and all) written there by the gpu, and whether they were for the glare which follows + TSubModel *m_glarequerysubmodel { nullptr }; + std::optional m_glarequery; + std::unique_ptr m_glaressbo; + bool m_glaresamples { false }; std::unique_ptr m_celestial_shader; std::unique_ptr m_hiddenarea_shader; std::unique_ptr m_copy_shader; diff --git a/shaders/ogl46/billboard.frag b/shaders/ogl46/billboard.frag index 42f3e64d3..1182d06dd 100644 --- a/shaders/ogl46/billboard.frag +++ b/shaders/ogl46/billboard.frag @@ -11,9 +11,21 @@ layout(binding = 0) uniform sampler2D tex1; layout(location = 0) out vec4 out_color; layout(location = 1) out vec4 out_motion; +// the samples of the point of the lamp its occlusion queries counted (the opengl renderer, with param[1].w 1): the glare is as +// bright as the part of the point which is seen +layout(std430, binding = GLARE_SSBO_BINDING) readonly buffer glare_ssbo +{ + uint glare_seen; + uint glare_all; +}; + void main() { vec4 tex_color = texture(tex1, f_coord); + if (param[1].w > 0.5f) + { + tex_color.a *= (glare_all > 0u ? clamp(float(glare_seen) / float(glare_all), 0.0f, 1.0f) : 0.0f); + } if (POSTFX_ENABLED) { out_color = tex_color * param[0]; diff --git a/shaders/ogl46/common.glsl b/shaders/ogl46/common.glsl index 9c0a2ada4..7fbaca131 100644 --- a/shaders/ogl46/common.glsl +++ b/shaders/ogl46/common.glsl @@ -119,6 +119,7 @@ #define SKY_OCCLUSION_TEX 30 #define CAB_PROBE_MASK_TEX 31 #define CAB_PROBE_SSBO_BINDING 17 +#define GLARE_SSBO_BINDING 18 #define CAB_PROBE_SH_SIZE 16 #define CAB_PROBE_SH_GROUP_SIZE 64 #define WEATHER_UBO_BINDING 5