diff --git a/model/Model3d.h b/model/Model3d.h index 9be9a4f0f..f7a09dc03 100644 --- a/model/Model3d.h +++ b/model/Model3d.h @@ -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 @@ -265,7 +264,6 @@ public: void ReplaceMaterial(const std::string &name); float MaxY( float4x4 const &m ); std::shared_ptr> screen_touch_list; // for python screen touching - std::optional occlusion_query; glm::mat4 future_transform; void deserialize(std::istream&); diff --git a/rendering/opengl46/opengl46renderer.cpp b/rendering/opengl46/opengl46renderer.cpp index 0d4c90399..d7b1cb24f 100644 --- a/rendering/opengl46/opengl46renderer.cpp +++ b/rendering/opengl46/opengl46renderer.cpp @@ -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(*seen) / static_cast(*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() diff --git a/rendering/opengl46/opengl46renderer.h b/rendering/opengl46/opengl46renderer.h index 51cafb603..2f50985d1 100644 --- a/rendering/opengl46/opengl46renderer.h +++ b/rendering/opengl46/opengl46renderer.h @@ -54,6 +54,10 @@ http://mozilla.org/MPL/2.0/. #include "rendering/common/freecameramotion.h" #include "rendering/common/screenreflections.h" +#include +#include +#include + // 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 m_freespot_shader; std::unique_ptr 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{}(Key.submodel) ^ (std::hash{}(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 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 m_celestial_shader; std::unique_ptr m_hiddenarea_shader; std::unique_ptr m_copy_shader;