18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-12-02 22:25:23 +01:00

Glare of light points: occlusion queries of each light point of each model, read without waiting

The glare waited for the occlusion query of its point in the same frame (a conditional render with GL_QUERY_WAIT and the counts
written to a buffer for the shader), which drains the gpu at every lamp. Each light point of each model (the submodel and the
instance drawn) now has a pair of queries of its own in the main view: the samples of the point which passed the depth test, and
all of them drawn once more without it. Their results are read once the gpu has them, without waiting, and the glare is as
bright as the part of the point seen by them, a frame or two late. A point not drawn for more than two frames starts over unseen,
so a glare doesn't show through what came in front of it meanwhile. The walk, the shaders and the vulkan renderer are as they
were.
This commit is contained in:
pwarszawski
2026-11-16 16:44:44 +01:00
parent e3584e706b
commit 0367a04f22
8 changed files with 170 additions and 131 deletions
-4
View File
@@ -2,8 +2,6 @@
#include "glsl_common.h"
#include "rendering/common/shadersource.h"
#include <format>
std::string gl::glsl_common;
void gl::glsl_common_setup()
@@ -120,8 +118,6 @@ void gl::glsl_common_setup()
"#define FOG_HORIZON_SAMPLES " + std::to_string(FOG_HORIZON_SAMPLES) + "\n" +
"#define WEATHER_GROUP_SIZE " + std::to_string(WEATHER_GROUP_SIZE) + "\n" +
"#define FOG_VOLUME_BIAS " + std::to_string(FOG_VOLUME_BIAS) + "\n";
// the counts of the samples of the light points seen, which their glares are made as bright as
glsl_common += std::format("#define GLARE_SSBO_BINDING {}\n", GLARE_SSBO_BINDING);
}
std::string gl::glsl_settings_defines()
-4
View File
@@ -446,10 +446,6 @@ 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
+42 -47
View File
@@ -3110,44 +3110,6 @@ void scene_walk::Render_Alpha_text(TSubModel *Submodel)
}
}
void scene_walk::Render_Glare(TSubModel *Submodel, glm::vec4 const &Lightcenter, bool const Pointlight)
{
// luminosity at night is at level of ~0.1, so the overall resulting transparency in clear conditions is ~0.5 at full 'brightness'
auto glarelevel{std::clamp(std::max<float>(static_cast<float>(0.6f - Global.fLuminance), // reduce the glare in bright daylight
Global.Overcast - 1.f), // ensure some glare in rainy/foggy conditions
0.f, 1.f)};
// view angle attenuation
float const anglefactor{Pointlight ? 1.f : std::clamp((Submodel->fCosViewAngle - Submodel->fCosFalloffAngle) / (Submodel->fCosHotspotAngle - Submodel->fCosFalloffAngle), 0.f, 1.f)};
glarelevel *= anglefactor;
if (glarelevel > 0.0f)
{
walk_glare(true);
::glPushMatrix();
::glLoadIdentity(); // macierz jedynkowa
::glTranslatef(Lightcenter.x, Lightcenter.y, Lightcenter.z); // początek układu zostaje bez zmian
::glRotated(std::atan2(Lightcenter.x, Lightcenter.z) * 180.0 / M_PI, 0.0, 1.0, 0.0); // jedynie obracamy w pionie o kąt
auto const lightcolor = glm::vec3(Submodel->DiffuseOverride.r < 0.f ? // -1 indicates no override
Submodel->f4Diffuse :
Submodel->DiffuseOverride);
walk_program( walk_shader::billboard );
walk_texture(0, m_glaretexture);
model_ubs.param[0] = glm::vec4(glm::vec3(lightcolor), Submodel->fVisible * glarelevel);
// main draw call
// drawn where the point of the light was seen just above (an occlusion query of the opengl renderer)
walk_glare_draw(Submodel, m_billboardgeometry);
walk_glare(false);
::glPopMatrix();
}
}
void scene_walk::Render_Alpha(TSubModel *Submodel)
{
// renderowanie przezroczystych przez DL
@@ -3256,8 +3218,48 @@ void scene_walk::Render_Alpha(TSubModel *Submodel)
// a point light (FalloffAngle 360) is seen all around
auto const pointlight{Submodel->fCosFalloffAngle <= -1.f};
// the glare is drawn after the point, where the occlusion query of this very point (of this instance, in this frame) saw it
auto pointdrawn{false};
if (Global.fLuminance < Submodel->fLight || Global.Overcast > 1.0f)
{
if (pointlight || (Submodel->fCosViewAngle > Submodel->fCosFalloffAngle))
{
// only bother if the viewer is inside the visibility cone
// luminosity at night is at level of ~0.1, so the overall resulting transparency in clear conditions is ~0.5 at full 'brightness'
auto glarelevel{std::clamp(std::max<float>(0.6f - Global.fLuminance, // reduce the glare in bright daylight
Global.Overcast - 1.f), // ensure some glare in rainy/foggy conditions
0.f, 1.f)};
// view angle attenuation
float const anglefactor{pointlight ? 1.f : std::clamp((Submodel->fCosViewAngle - Submodel->fCosFalloffAngle) / (Submodel->fCosHotspotAngle - Submodel->fCosFalloffAngle), 0.f, 1.f)};
glarelevel *= anglefactor;
if (glarelevel > 0.0f)
{
walk_glare(true);
::glPushMatrix();
::glLoadIdentity(); // macierz jedynkowa
::glTranslatef(lightcenter.x, lightcenter.y, lightcenter.z); // początek układu zostaje bez zmian
::glRotated(std::atan2(lightcenter.x, lightcenter.z) * 180.0 / M_PI, 0.0, 1.0, 0.0); // jedynie obracamy w pionie o kąt
auto const lightcolor = glm::vec3(Submodel->DiffuseOverride.r < 0.f ? // -1 indicates no override
Submodel->f4Diffuse :
Submodel->DiffuseOverride);
walk_program( walk_shader::billboard );
walk_texture(0, m_glaretexture);
model_ubs.param[0] = glm::vec4(glm::vec3(lightcolor), Submodel->fVisible * glarelevel);
// main draw call
// drawn where the light itself was seen in the last frame (an occlusion query of the opengl renderer)
walk_glare_draw(Submodel, m_billboardgeometry);
walk_glare(false);
::glPopMatrix();
}
}
}
if (pointlight || (Submodel->fCosViewAngle > Submodel->fCosFalloffAngle))
{
// kąt większy niż maksymalny stożek swiatła
@@ -3314,8 +3316,6 @@ 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
model_ubs.emission = 0.0f;
@@ -3324,11 +3324,6 @@ void scene_walk::Render_Alpha(TSubModel *Submodel)
::glPopMatrix();
}
}
// the point is drawn only inside its visibility cone, so the glare is too
if (pointdrawn && (Global.fLuminance < Submodel->fLight || Global.Overcast > 1.0f))
{
Render_Glare(Submodel, lightcenter, pointlight);
}
}
if (Submodel->Child != nullptr)
+1 -11
View File
@@ -266,18 +266,10 @@ class scene_walk
virtual void walk_timbers_expand(std::vector<glm::mat4> const &Origins, std::vector<gl::timber_range> const &Ranges) {}
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 and makes the glare as bright as the part of the point seen, the vulkan renderer uses the result of the
// last frame)
// seen (walk_light_query() of the last frame, when the backend has it)
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)
{
// a backend which doesn't scale the glare by the part of the point seen has nothing to draw here
}
// 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() {}
@@ -640,8 +632,6 @@ class scene_walk
private:
static float editor_ortho_reach();
// the glare around a light point drawn just before, Lightcenter in view space
void Render_Glare(TSubModel *Submodel, glm::vec4 const &Lightcenter, bool Pointlight);
};
} // namespace gfx
+92 -44
View File
@@ -5056,61 +5056,109 @@ void opengl46_renderer::walk_glare(bool const Enabled)
}
}
// the frames a result of the glare queries is good for: a point not drawn for longer may have something else in front of it now
constexpr unsigned int GLARE_RESULT_FRAMES{2};
// the frames after which the entries of points no longer drawn are dropped
constexpr unsigned int GLARE_KEEP_FRAMES{600};
opengl46_renderer::glare_visibility *opengl46_renderer::glare_entry(TSubModel const *Submodel, bool const Create)
{
glare_key const key{Submodel, TSubModel::iInstance};
if (false == Create)
{
auto const lookup{m_glares.find(key)};
return (lookup != m_glares.end() ? &lookup->second : nullptr);
}
if (m_framestamp - m_glarepruned > GLARE_KEEP_FRAMES)
{
m_glarepruned = m_framestamp;
auto const framestamp{m_framestamp};
// a query whose result nobody reads any more is deleted along
std::erase_if(m_glares, [framestamp](auto const &Entry) { return framestamp - Entry.second.frame > GLARE_KEEP_FRAMES; });
}
return &m_glares.try_emplace(key).first->second;
}
void opengl46_renderer::glare_read(glare_visibility &Entry)
{
// the results once the gpu has both of them, nothing waits for them
auto const seen{Entry.seen.result()};
if (false == seen.has_value())
{
return;
}
auto const all{Entry.all.result()};
if (false == all.has_value())
{
return;
}
Entry.pending = false;
Entry.share = (*all > 0 ? std::clamp(static_cast<float>(*seen) / static_cast<float>(*all), 0.f, 1.f) : 0.f);
}
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);
if (false == gl::vao::use_vao)
{
draw(Geometry);
glEndConditionalRender();
return;
}
else
draw(Geometry);
model_ubs.param[1].w = parameter;
m_glaresamples = false;
// as bright as the part of the point seen in the main view, by the results of an earlier frame; none for a point not seen
// lately
auto const *entry{glare_entry(Submodel, false)};
if ((entry == nullptr) || (m_framestamp - entry->frame > GLARE_RESULT_FRAMES) || (entry->share <= 0.f))
{
return;
}
auto const opacity{model_ubs.param[0].w};
model_ubs.param[0].w = opacity * entry->share;
draw(Geometry);
model_ubs.param[0].w = opacity;
}
void opengl46_renderer::walk_light_query(TSubModel *Submodel, bool const Begin)
{
if (false == gl::vao::use_vao)
if ((false == gl::vao::use_vao) || (m_renderpass.draw_mode != rendermode::color) || (m_current_viewport == nullptr) || (false == m_current_viewport->main))
{
return;
if (Begin) {
m_glaresamples = false;
if (!Submodel->occlusion_query)
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()))
if (false == Begin)
{
if (m_glarequery == nullptr)
{
return;
}
m_glarequery->seen.end();
// 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->all.begin();
draw(Submodel->m_geometry.handle);
m_glarequery->all.end();
glEnable(GL_DEPTH_TEST);
glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
m_glarequery->pending = true;
m_glarequery = nullptr;
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<gl::ssbo>(static_cast<GLsizeiptr>(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<GLintptr>(sizeof(GLuint)));
m_glaresamples = true;
}
auto &entry{*glare_entry(Submodel, true)};
if (m_framestamp - entry.frame > GLARE_RESULT_FRAMES)
{
// not drawn lately: what it saw then is no good, it starts over unseen
entry.share = 0.f;
entry.pending = false;
}
entry.frame = m_framestamp;
if (entry.pending)
{
glare_read(entry);
}
if (false == entry.pending)
{
// new queries once the results of the last ones are read
entry.seen.begin();
m_glarequery = &entry;
}
}
void opengl46_renderer::walk_region_begin()
+35 -8
View File
@@ -53,6 +53,10 @@ http://mozilla.org/MPL/2.0/.
#include "rendering/common/temporalaa.h"
#include "rendering/common/screenreflections.h"
#include <cstdint>
#include <functional>
#include <unordered_map>
// bare-bones render controller, in lack of anything better yet
class opengl46_renderer : public gfx_renderer, public gfx::scene_walk {
public:
@@ -228,7 +232,6 @@ 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;
@@ -849,13 +852,37 @@ class opengl46_renderer : public gfx_renderer, public gfx::scene_walk {
unsigned int m_editor_overlay_revision{0};
std::unique_ptr<gl::program> m_freespot_shader;
std::unique_ptr<gl::program> 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<gl::query> m_glarequery;
std::unique_ptr<gl::ssbo> m_glaressbo;
bool m_glaresamples { false };
// the glare of a light point is as bright as the part of its point seen in the main view, counted by a pair of occlusion
// queries of its own for each light point of each model (the submodel and the instance drawn, TSubModel::iInstance): the
// samples which passed the depth test and all of them. the results are read once the gpu has them, without waiting, so the
// glare follows its point a frame or two late
struct glare_key
{
TSubModel const *submodel{nullptr};
std::uintptr_t instance{0};
bool operator==(glare_key const &) const = default;
};
struct glare_key_hash
{
std::size_t operator()(glare_key const &Key) const
{
return std::hash<void const *>{}(Key.submodel) ^ (std::hash<std::uintptr_t>{}(Key.instance) * 31u);
}
};
struct glare_visibility
{
gl::query seen{gl::query::SAMPLES_PASSED};
gl::query all{gl::query::SAMPLES_PASSED};
bool pending{false}; // the queries were issued and their results aren't read yet
float share{0.f}; // the part of the point seen, by the last results read
unsigned int frame{0}; // the frame the point was last drawn in the main view
};
// the visibility of the point the walk draws, for the queries around it (walk_light_query())
glare_visibility *glare_entry(TSubModel const *Submodel, bool Create);
static void glare_read(glare_visibility &Entry);
std::unordered_map<glare_key, glare_visibility, glare_key_hash> m_glares;
glare_visibility *m_glarequery{nullptr}; // the entry whose queries are under way
unsigned int m_glarepruned{0}; // the frame the entries of points no longer drawn were last dropped
std::unique_ptr<gl::program> m_celestial_shader;
std::unique_ptr<gl::program> m_hiddenarea_shader;
std::unique_ptr<gl::program> m_copy_shader;
-12
View File
@@ -11,21 +11,9 @@ 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];
-1
View File
@@ -119,7 +119,6 @@
#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