mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-12-02 08:32:45 +01:00
Merge pull request #374 from pwarszawski/fix/glare-occlusion
fix(gl46): no more glares of light points through walls
This commit is contained in:
@@ -13,7 +13,6 @@ http://mozilla.org/MPL/2.0/.
|
||||
#include "utilities/Float3d.h"
|
||||
#include "rendering/geometrybank.h"
|
||||
#include "model/material.h"
|
||||
#include "gl/query.h"
|
||||
|
||||
#include <string_view>
|
||||
|
||||
@@ -265,7 +264,6 @@ public:
|
||||
void ReplaceMaterial(const std::string &name);
|
||||
float MaxY( float4x4 const &m );
|
||||
std::shared_ptr<std::vector<glm::vec2>> screen_touch_list; // for python screen touching
|
||||
std::optional<gl::query> occlusion_query;
|
||||
glm::mat4 future_transform;
|
||||
|
||||
void deserialize(std::istream&);
|
||||
|
||||
@@ -5347,28 +5347,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)
|
||||
{
|
||||
if (Submodel->occlusion_query) {
|
||||
glBeginConditionalRender(*Submodel->occlusion_query, GL_QUERY_WAIT);
|
||||
if (false == gl::vao::use_vao)
|
||||
{
|
||||
draw(Geometry);
|
||||
glEndConditionalRender();
|
||||
return;
|
||||
}
|
||||
else
|
||||
draw(Geometry);
|
||||
// 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) {
|
||||
if (!Submodel->occlusion_query)
|
||||
Submodel->occlusion_query.emplace(gl::query::ANY_SAMPLES_PASSED);
|
||||
Submodel->occlusion_query->begin();
|
||||
}
|
||||
else
|
||||
Submodel->occlusion_query->end();
|
||||
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 &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()
|
||||
|
||||
@@ -54,6 +54,10 @@ http://mozilla.org/MPL/2.0/.
|
||||
#include "rendering/common/freecameramotion.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:
|
||||
@@ -892,6 +896,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 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;
|
||||
|
||||
Reference in New Issue
Block a user