From 4ba8d508cafdc1de87d09e553f803ea66ad83fd8 Mon Sep 17 00:00:00 2001 From: pol102 Date: Wed, 25 Nov 2026 05:14:33 +0100 Subject: [PATCH] fix(gl46, vk14): keep the loading screen up while the shaders and pipelines are prepared, instead of a black window (#476) * fix(gl46): the loading screen stays until the shaders are compiled, no black window at the first start of a new version At the first start of a new build (the driver keeps the compiled shaders of each executable apart, and every build has a name of its own) the window went black and didn't answer for about half a minute after the scenery was loaded. Measured on calkowo_osobowy with a copy of the executable under a new name: 33 of those seconds were the first step of the water on the windscreens, which compiled its nine programs (water_*.comp) in the first frame of the simulation. - After the scenery is loaded the loader keeps its screen, with the bar going and the note on preparing the graphics, until the renderer says the programs are ready (gfx_renderer::Prewarm_Shaders(), about 50 ms a frame): the programs of the materials are drawn with once each into a small target of the formats of the colour pass, as the opaque things and as the translucent ones, then the programs of the windscreen water are compiled one a frame (opengl46_windscreen_water::prepare_next()), which gpu_init() then keeps. The window answers in between. - The Vulkan renderer, which has its pipeline cache, says it's all done at once (the default of the interface). Checked with a copy of the executable under a new name (nothing kept by the driver): the loading screen with the note from the end of the loading (19 s) to the first frame (58 s), the window answering all the time; the first frame no longer stalls (the water 12 ms instead of 33 s). With the shaders kept by the driver the preparing takes no time. No asset changes. Co-Authored-By: Claude Opus 5.5 * fix(gl46): the missing program of the windscreen water found by find_if, which GCC 13 and MSVC 14.4x compile Co-Authored-By: Claude Opus 5.5 * fix(loading): the notice that the graphics are being prepared at the top of the screen The notice stood at the bottom left, beside where the loading icon goes, over the trivia and the texts some of the backgrounds carry at the bottom. It is now centred at the top of the screen on a dark plate, so it reads on any background. The text and its translation are the same. Co-Authored-By: Claude Opus 5.5 * feat(vk14): the loading screen stays until the pipelines of the scene are made, as with OpenGL 4.6 vulkan14_renderer::Prewarm_Shaders() lists the pipelines the loaded scenery draws with most: those of the programs of its materials for the colour pass of the main view, opaque and blended, and those of the shadows. With the background threads (gfx.vulkan.asyncresources) they are handed to them, made ahead and not counted as made late; without them they are made on the main thread for about 50 ms a frame. The loading screen, with its notice and progress bar, waits for them and for the pipelines of the last session the background threads make ahead. Before, a pipeline first asked for by a draw either left the draw out for a few frames or held the frame while it was made. The images of a viewport are made by make_viewport_targets(), taken out of render_scene() unchanged, so the main view's formats are known before the first frame. The key and the settings of a pipeline come from the same helpers bind_scene_pipeline() uses. Co-Authored-By: Claude Opus 5.5 * fix(gl46, vk14): the review of #476: the multi draw variants prewarmed with a model block, the last session's list in the progress OpenGL 4.6: with gfx.gpucull 1 and the multi draws on, Prewarm_Shaders() also draws the multi draw variants of the material programs. Their shaders read the model block from the storage buffer at MODEL_DRAW_SSBO_BINDING, which nothing bound before the first frame, so the draw was undefined. The prewarm now binds a buffer of one model block there. With the default gfx.gpucull 0 there are no such variants and nothing changes. Vulkan: the loading screen waits for the background threads, and so for the list of the last session, up to 4096 pipelines. With a cold cache the bar stood at n/(n+1) all that time and looked stuck. The pipelines of that list are now counted in the progress as they are done with, made or not. The wait still ends when the background threads are idle, as before. make_viewport_targets() returns false without a context, which Sonar reported as a null pointer dereference. Its callers checked the context already. Co-Authored-By: Claude Fable 5.2 Claude-Session: https://claude.ai/code/session_01QRgQ1LqB4YXWorLDBBL6hD --------- Co-authored-by: Claude Opus 5.5 --- application/scenarioloadermode.cpp | 24 +++- application/scenarioloadermode.h | 2 + application/scenarioloaderuilayer.cpp | 23 +++- application/scenarioloaderuilayer.h | 7 ++ lang/editor_pl.po | 3 + rendering/opengl46/opengl46renderer.cpp | 89 +++++++++++++++ rendering/opengl46/opengl46renderer.h | 20 ++++ rendering/opengl46/opengl46water.cpp | 41 ++++++- rendering/opengl46/opengl46water.h | 4 + rendering/renderer.h | 7 ++ rendering/vulkan14/vulkan14context.h | 22 +++- rendering/vulkan14/vulkan14frame.cpp | 58 ++++++---- rendering/vulkan14/vulkan14renderer.h | 5 + rendering/vulkan14/vulkan14scene.cpp | 145 +++++++++++++++++++++++- 14 files changed, 419 insertions(+), 31 deletions(-) diff --git a/application/scenarioloadermode.cpp b/application/scenarioloadermode.cpp index 568966503..85f81a337 100644 --- a/application/scenarioloadermode.cpp +++ b/application/scenarioloadermode.cpp @@ -40,6 +40,20 @@ bool scenarioloader_mode::update() { // waiting for network connection return true; + if (m_closing > 0) { + // the programs of the materials made ready a few at a frame, the loading screen with its bar kept on the screen and the + // window answering meanwhile; then on to the simulation + auto const [done, total] { GfxRenderer->Prewarm_Shaders( 50.0 ) }; + if( done < total ) { + m_userinterface->set_progress( static_cast( done ) * 100.f / static_cast( total ), 0.f ); + return true; + } + m_closing = 0; + Application.pop_mode(); + Application.push_mode( eu07_application::mode::driver ); + return true; + } + if (!state) { WriteLog("using simulation seed: " + std::to_string(Global.random_seed), logtype::generic); WriteLog("using simulation starting timestamp: " + std::to_string(Global.starting_timestamp), logtype::generic); @@ -98,8 +112,12 @@ bool scenarioloader_mode::update() { WriteLog( "Scenario loading time: " + std::to_string( std::chrono::duration_cast( std::chrono::system_clock::now() - timestart ).count() ) + " seconds" ); // TODO: implement and use next mode cue - Application.pop_mode(); - Application.push_mode( eu07_application::mode::driver ); + // the loading screen stays, saying the graphics are being prepared, until the programs are ready (above). + // at the top of the screen: the bottom is taken by the trivia and by the texts some backgrounds carry. the interface + // of this mode is always the loader's, made in the constructor + m_userinterface->set_progress(std::string{}); + static_cast(*m_userinterface).set_notice(STR("Preparing the graphics (takes longer at the first start of a new version)")); + m_closing = 1; return true; } @@ -117,6 +135,8 @@ void scenarioloader_mode::enter() { Application.set_title( Global.AppName + " (" + Global.SceneryFile + ")" ); m_userinterface->set_progress(STR("Loading scenery")); + // the notice of a load before is gone + static_cast(*m_userinterface).set_notice({}); } // maintenance method, called when the mode is deactivated diff --git a/application/scenarioloadermode.h b/application/scenarioloadermode.h index db23458dd..41ae75f68 100644 --- a/application/scenarioloadermode.h +++ b/application/scenarioloadermode.h @@ -15,6 +15,8 @@ http://mozilla.org/MPL/2.0/. class scenarioloader_mode : public application_mode { std::shared_ptr state; std::chrono::system_clock::time_point timestart; + // the scenery is loaded: the loading screen kept while the renderer prepares the programs (gfx_renderer::Prewarm_Shaders()) + int m_closing{0}; public: // constructors diff --git a/application/scenarioloaderuilayer.cpp b/application/scenarioloaderuilayer.cpp index 75a8af3cb..cb7f1045b 100644 --- a/application/scenarioloaderuilayer.cpp +++ b/application/scenarioloaderuilayer.cpp @@ -287,7 +287,9 @@ void scenarioloader_ui::render_() ImVec2 text_pos = ImVec2(screen_size.x - 16 * scale - text_size.x, 16 * scale); draw_list->AddText(text_pos, IM_COL32_WHITE, sceneryName.c_str()); } - + + render_notice(draw_list, scale, font_scale_mult); + // Progress bar at the bottom of the screen const auto p1 = ImVec2(0, Global.window_size.y - 2); const auto p2 = ImVec2(Global.window_size.x * m_progress, Global.window_size.y); @@ -296,6 +298,25 @@ void scenarioloader_ui::render_() ImGui::End(); } +// the notice centred at the top of the screen on a dark plate, so it reads on any background. the bottom of the screen +// is taken by the trivia and by the texts some backgrounds carry +void scenarioloader_ui::render_notice( ImDrawList *Drawlist, float const Scale, float const Fontscale ) const +{ + if (m_notice.empty()) + return; + + ImGui::SetWindowFontScale(Fontscale * 0.6f); + auto const text_size{ImGui::CalcTextSize(m_notice.c_str())}; + auto const padding{16.f * Scale}; + auto const top{48.f * Scale}; + ImVec2 const text_pos{(static_cast(Global.window_size.x) - text_size.x) * 0.5f, top + padding}; + Drawlist->AddRectFilled( + ImVec2{text_pos.x - padding, top}, + ImVec2{text_pos.x + text_size.x + padding, text_pos.y + text_size.y + padding}, + IM_COL32(0, 0, 0, 170), 8.f * Scale); + Drawlist->AddText(text_pos, IM_COL32_WHITE, m_notice.c_str()); +} + void scenarioloader_ui::generate_gradient_tex() { constexpr int image_width = 1; diff --git a/application/scenarioloaderuilayer.h b/application/scenarioloaderuilayer.h index 45a0ba5bd..edea429f8 100644 --- a/application/scenarioloaderuilayer.h +++ b/application/scenarioloaderuilayer.h @@ -12,16 +12,23 @@ http://mozilla.org/MPL/2.0/. #include "application/uilayer.h" #include "launcher/deferred_image.h" +#include +#include + class scenarioloader_ui : public ui_layer { int m_gradient_overlay_width; int m_gradient_overlay_height; std::uint64_t m_gradient_overlay_tex; // imgui_renderer image deferred_image m_loading_wheel_frames[60]; std::vector m_trivia; + std::string m_notice; // shown at the top of the screen, clear of the trivia and the texts of the backgrounds at the bottom public: scenarioloader_ui(); void render_() override; + // a notice for the player at the top of the screen, such as that the graphics are being prepared; empty hides it + void set_notice( std::string_view const Text ) { m_notice = Text; } private: + void render_notice( ImDrawList *Drawlist, float Scale, float Fontscale ) const; /// /// Loads random trivia from lang/trivia.json diff --git a/lang/editor_pl.po b/lang/editor_pl.po index e7332801e..34bceaf7b 100644 --- a/lang/editor_pl.po +++ b/lang/editor_pl.po @@ -16285,3 +16285,6 @@ msgstr "Ortofoto: %zu kafli widoku pobieranych w pierwszej kolejności (Ctrl+~)" msgid "Orthophoto: the view is all shown already (Ctrl+~)" msgstr "Ortofoto: cały widok jest już pokazany (Ctrl+~)" + +msgid "Preparing the graphics (takes longer at the first start of a new version)" +msgstr "Przygotowanie grafiki (przy pierwszym uruchomieniu nowej wersji trwa dłużej)" diff --git a/rendering/opengl46/opengl46renderer.cpp b/rendering/opengl46/opengl46renderer.cpp index 4180d3b12..6a81bb9f0 100644 --- a/rendering/opengl46/opengl46renderer.cpp +++ b/rendering/opengl46/opengl46renderer.cpp @@ -5760,6 +5760,95 @@ std::string const &opengl46_renderer::info_stats() const return m_debugstatstext; } +std::pair opengl46_renderer::Prewarm_Shaders( double const Milliseconds ) +{ + auto &prewarm { m_prewarm }; + if( false == prewarm.started ) { + prewarm.started = true; + // the programs of the materials and their variants for the multi draws of the instances + for( auto const &shader : m_shaders ) { + if( shader.second != nullptr ) { prewarm.programs.emplace_back( shader.second.get() ); } + } + for( auto const &variant : m_multidrawprograms ) { + if( variant.second != nullptr ) { prewarm.programs.emplace_back( variant.second.get() ); } + } + // the formats and the samples of the colour pass (init_viewport()), 4 x 4 pixels + auto const samples { 1 << Global.iMultisampling }; + prewarm.color = std::make_unique(); + prewarm.color->alloc( Global.gfx_format_color, 4, 4, samples ); + prewarm.depth = std::make_unique(); + prewarm.depth->alloc( Global.gfx_format_depth, 4, 4, samples ); + prewarm.target = std::make_unique(); + prewarm.target->attach( *prewarm.color, GL_COLOR_ATTACHMENT0 ); + prewarm.target->attach( *prewarm.depth, GL_DEPTH_ATTACHMENT ); + if( Global.gfx_motion_vectors() ) { + prewarm.motion = std::make_unique(); + prewarm.motion->alloc( Global.gfx_postfx_motionblur_format, 4, 4, samples ); + prewarm.target->attach( *prewarm.motion, GL_COLOR_ATTACHMENT1 ); + } + // a triangle with no area: drawn, nothing comes of it, but the program is made ready for the state of the draw + gfx::vertex_array vertices( 3, gfx::basic_vertex( glm::vec3( 0.f ), glm::vec3( 0.f, 1.f, 0.f ), glm::vec2( 0.f ) ) ); + gfx::userdata_array userdata; + prewarm.bank = Create_Bank(); + prewarm.triangle = Insert( vertices, userdata, prewarm.bank, GL_TRIANGLES ); + if( false == m_multidrawprograms.empty() ) { + // the variants of the multi draws read the model block of the draw from a storage buffer, which the frames bind for + // their multi draws only. drawn here with a plain draw, they read the block of the draw 0; without a buffer there + // the draw would be undefined + prewarm.draws = std::make_unique( static_cast( sizeof( gl::model_ubs ) ), gl::MODEL_DRAW_SSBO_BINDING ); + prewarm.draws->update( model_ubs ); + } + if( false == prewarm.target->is_complete() ) { + ErrorLog( "the programs of the materials weren't prepared: the target of the drawing can't be made" ); + prewarm.next = prewarm.programs.size(); + } + WriteLog( "preparing " + std::to_string( prewarm.programs.size() ) + " programs of the materials before the first frame" ); + } + auto const total { prewarm.programs.size() + opengl46_windscreen_water::program_count() }; + if( prewarm.next >= prewarm.programs.size() ) { + // then the programs of the water on the windscreens, one at a time: the longest of all to compile, the first frame of the + // simulation made them before + if( ( false == prewarm.water_done ) && m_windscreenwater.prepare_next() ) { + ++prewarm.water; + } + else if( false == prewarm.water_done ) { + prewarm.water_done = true; + WriteLog( "the programs of the windscreen water prepared" ); + } + return { prewarm.water_done ? total : prewarm.next + prewarm.water, total }; + } + + auto const until { std::chrono::steady_clock::now() + std::chrono::duration( Milliseconds ) }; + prewarm.target->bind(); + prewarm.target->setup_drawing( prewarm.motion ? 2 : 1 ); + ::glViewport( 0, 0, 4, 4 ); + ::glEnable( GL_DEPTH_TEST ); + model_ubo->update( model_ubs ); + if( prewarm.draws != nullptr ) { + // every multi draw of a frame binds its own blocks there afterwards (Bind_Indirect_Blocks()) + prewarm.draws->bind_base(); + } + while( ( prewarm.next < prewarm.programs.size() ) && ( std::chrono::steady_clock::now() < until ) ) { + prewarm.programs[ prewarm.next ]->bind(); + // as the opaque things are drawn, and as the translucent ones are + ::glDisable( GL_BLEND ); + ::glDepthMask( GL_TRUE ); + m_geometry.draw( prewarm.triangle ); + ::glEnable( GL_BLEND ); + ::glDepthMask( GL_FALSE ); + m_geometry.draw( prewarm.triangle ); + ++prewarm.next; + } + ::glDisable( GL_BLEND ); + ::glDepthMask( GL_TRUE ); + gl::program::unbind(); + gl::framebuffer::unbind(); + if( prewarm.next >= prewarm.programs.size() ) { + WriteLog( "the programs of the materials prepared" ); + } + return { prewarm.next, total }; +} + void opengl46_renderer::MakeScreenshot() { // rows are tightly packed RGB: the renderer sets GL_PACK_ALIGNMENT to 1 diff --git a/rendering/opengl46/opengl46renderer.h b/rendering/opengl46/opengl46renderer.h index 08d92e4b7..5f5ec776a 100644 --- a/rendering/opengl46/opengl46renderer.h +++ b/rendering/opengl46/opengl46renderer.h @@ -183,6 +183,7 @@ class opengl46_renderer : public gfx_renderer, public gfx::scene_walk { std::vector const & info_sections() const override; void MakeScreenshot() override; + std::pair Prewarm_Shaders( double const Milliseconds ) override; bool Render_Preview( TAnimModel *Instance, int const Size, int const Margin, bool const Shadows, std::vector &Image ) override; @@ -650,6 +651,25 @@ class opengl46_renderer : public gfx_renderer, public gfx::scene_walk { // programs of the multi draws: the vertex shader of the materials, variants of the material programs, of the depth prepass std::unique_ptr m_vertex_multidraw_shader; std::unordered_map> m_multidrawprograms; + // Prewarm_Shaders(): the programs to draw with, the next one, and a small target of the formats of the colour pass with a + // triangle of no area to draw + struct shader_prewarm + { + bool started{ false }; + std::vector programs; + std::size_t next{ 0 }; + std::size_t water{ 0 }; // programs of the windscreen water made ahead + bool water_done{ false }; + std::unique_ptr color; + std::unique_ptr depth; + std::unique_ptr motion; + std::unique_ptr target; + // one model block for the variants of the multi draws, which read theirs from a storage buffer (MODEL_DRAW_SSBO_BINDING) + std::unique_ptr draws; + gfx::geometrybank_handle bank{}; + gfx::geometry_handle triangle{}; + }; + shader_prewarm m_prewarm; std::unique_ptr m_prepass_multidraw_shader; std::unique_ptr m_prepass_alpha_multidraw_shader; std::unique_ptr m_shadow_multidraw_shader; // and of the sun shadows diff --git a/rendering/opengl46/opengl46water.cpp b/rendering/opengl46/opengl46water.cpp index 805ae1bd8..a48b9edfb 100644 --- a/rendering/opengl46/opengl46water.cpp +++ b/rendering/opengl46/opengl46water.cpp @@ -13,6 +13,10 @@ http://mozilla.org/MPL/2.0/. #include "rendering/renderer.h" #include "utilities/Logs.h" +#include +#include +#include + namespace { @@ -23,15 +27,44 @@ GLuint groups(std::size_t const Count) } // namespace +namespace +{ +// the programs of the passes, in their order (opengl46_windscreen_water::pass) +constexpr std::array PROGRAM_NAMES{"water_trail.comp", "water_spawn.comp", "water_move.comp", "water_clear.comp", "water_grid.comp", + "water_merge.comp", "water_gather.comp", "water_settle.comp", "water_bake.comp"}; +} // namespace + +bool opengl46_windscreen_water::prepare_next() +{ + static_assert(PROGRAM_NAMES.size() == pass::count, "a name for every pass of the water"); + auto const missing{std::ranges::find_if(m_programs, [](std::unique_ptr const &Program) { return Program == nullptr; })}; + if (missing == m_programs.end()) + return false; + auto const index{static_cast(std::distance(m_programs.begin(), missing))}; + try + { + gl::shader shader(PROGRAM_NAMES[index]); + *missing = std::make_unique(std::vector>{shader}); + } + catch (gl::shader_exception const &e) + { + // left to gpu_init(), which tells of it and goes without the simulation + ErrorLog("windscreen water: " + std::string(PROGRAM_NAMES[index]) + " not made ahead: " + std::string(e.what())); + return false; + } + return true; +} + bool opengl46_windscreen_water::gpu_init() { try { - std::array const names{"water_trail.comp", "water_spawn.comp", "water_move.comp", "water_clear.comp", "water_grid.comp", - "water_merge.comp", "water_gather.comp", "water_settle.comp", "water_bake.comp"}; - for (std::size_t i = 0; i < names.size(); ++i) + // the ones made ahead (prepare_next()) are kept + for (std::size_t i = 0; i < PROGRAM_NAMES.size(); ++i) { - gl::shader shader(names[i]); + if (m_programs[i]) + continue; + gl::shader shader(PROGRAM_NAMES[i]); m_programs[i] = std::make_unique(std::vector>{shader}); } } diff --git a/rendering/opengl46/opengl46water.h b/rendering/opengl46/opengl46water.h index 851464e80..f609291c9 100644 --- a/rendering/opengl46/opengl46water.h +++ b/rendering/opengl46/opengl46water.h @@ -27,6 +27,10 @@ class opengl46_windscreen_water : public gfx::windscreen_water // the storage bound to its point for the glasses drawn next (the bindings are kept by every context of its own) void bind() const; + // the next of the programs of the simulation compiled ahead of its first step (a few seconds each where the driver has none + // of them kept, at the first start of a new version), so the loading screen stays while they're made. false once all are + bool prepare_next(); + static constexpr std::size_t program_count() { return pass::count; } protected: bool gpu_init() override; diff --git a/rendering/renderer.h b/rendering/renderer.h index 7cea7928b..d84035b68 100644 --- a/rendering/renderer.h +++ b/rendering/renderer.h @@ -10,7 +10,9 @@ http://mozilla.org/MPL/2.0/. #pragma once #include +#include #include +#include #include "rendering/geometrybank.h" #include "interfaces/IMaterial.h" @@ -84,6 +86,11 @@ public: virtual void Shutdown() = 0; // main draw call. returns false on error virtual auto Render() -> bool = 0; + // after the scenery is loaded, while the loading screen is shown: the programs of the materials drawn with once each, for + // about Milliseconds, so the driver compiles them now and not in the first frame of the simulation (a black window that + // doesn't answer for half a minute at the first start of a new version). returns how many are done and how many there are; + // a renderer which needs none of it says it's all done + virtual std::pair Prewarm_Shaders( double const Milliseconds ) { return { 0, 0 }; } virtual void SwapBuffers() = 0; virtual auto Framerate() -> float = 0; // geometry methods diff --git a/rendering/vulkan14/vulkan14context.h b/rendering/vulkan14/vulkan14context.h index bdc57dc75..1196722b2 100644 --- a/rendering/vulkan14/vulkan14context.h +++ b/rendering/vulkan14/vulkan14context.h @@ -579,7 +579,26 @@ struct vulkan14_renderer::context std::vector warm_records; std::unordered_set warm_lines; std::atomic warm_made{0}; + // the pipelines of the list of the last session handed to the background threads, and those of them done with, made or not: + // the progress of the loading screen (vulkan14_renderer::Prewarm_Shaders()) + std::atomic warm_queued{0}; + std::atomic warm_finished{0}; std::atomic pipelines_late{0}; + // the pipelines made behind the loading screen once the scenery is loaded (vulkan14_renderer::Prewarm_Shaders()): each with the + // key bind_scene_pipeline() looks it up by in the map of its program, and the next one made on the main thread without the + // background threads + struct prewarm_pipeline + { + std::shared_ptr program; + vk14::target_formats targets; + vk14::blend_mode blend{vk14::blend_mode::none}; + std::uint32_t settings{0}; + bool single{false}; + std::uint64_t key{0}; + }; + std::vector prewarm; + std::size_t prewarm_next{0}; + bool prewarm_listed{false}; // [0]: the main window, then the windows of the extra viewports std::vector> windows; std::vector frames; @@ -681,8 +700,9 @@ struct vulkan14_renderer::context VkPipeline make_scene_pipeline(vk14::scene_program &Program, vk14::target_formats const &Targets, vk14::blend_mode Blend, VkPrimitiveTopology Primitiveclass, std::uint32_t Settings, bool Single); bool find_scene_pipeline(vk14::scene_program &Program, vk14::target_formats const &Targets, vk14::blend_mode Blend, VkPrimitiveTopology Primitiveclass, std::uint32_t Settings, bool Single, VkPipeline &Pipeline); + // Late: a draw waits for it (counted as made late); otherwise it's made ahead void queue_scene_pipeline(vk14::scene_program &Program, vk14::target_formats const &Targets, vk14::blend_mode Blend, VkPrimitiveTopology Primitiveclass, std::uint32_t Settings, bool Single, - std::uint64_t Key); + std::uint64_t Key, bool Late = true); void warm_scene_pipelines(); void save_scene_pipelines(); // the value of the timeline the gpu reached, 0 when it can't be read diff --git a/rendering/vulkan14/vulkan14frame.cpp b/rendering/vulkan14/vulkan14frame.cpp index 82aa2645d..cc2d8839f 100644 --- a/rendering/vulkan14/vulkan14frame.cpp +++ b/rendering/vulkan14/vulkan14frame.cpp @@ -1039,6 +1039,44 @@ bool vulkan14_renderer::Render() return true; } +bool vulkan14_renderer::make_viewport_targets(std::size_t const Index) +{ + if (m_context == nullptr) + { + return false; + } + auto &ctx{*m_context}; + auto &scene{ctx.scene}; + if (scene.targets.size() < m_viewports.size()) + { + scene.targets.resize(m_viewports.size()); + } + auto const &viewport{m_viewports[Index]}; + auto &targets{scene.targets[Index]}; + auto const srgb{false == Global.gfx_shadergamma}; + auto const *history{targets.taa_history[0].get()}; + auto const *upscaled{targets.dlss_output.get()}; + auto const *reflections{targets.ssr_history.get()}; + auto const dlss{viewport.main && dlss_active() && (viewport.output_width > 0) && (viewport.output_height > 0)}; + if (false == make_targets(ctx.memory, ctx.deletions, ctx.uploads.current_value(), scene.formats_count, ctx.device.caps().limits.framebufferColorSampleCounts & ctx.device.caps().limits.framebufferDepthSampleCounts, scene.bloom_format, + scene.history_format, targets, viewport.width, viewport.height, viewport.output_width, viewport.output_height, srgb, viewport.main && Global.gfx_taa(), + viewport.main && (false == Global.gfx_skippipeline) && ((Global.gfx_ssr > 0) || (m_raytracingon && scene.raytracing)), dlss)) + { + return false; + } + if (viewport.main && ((targets.taa_history[0].get() != history) || (targets.dlss_output.get() != upscaled))) + { + // new images of the history, nothing in them yet + m_taa.reset(); + m_dlssreset = true; + } + if (targets.ssr_history.get() != reflections) + { + m_ssr.reset(); + } + return true; +} + void vulkan14_renderer::render_scene() { auto &ctx{*m_context}; @@ -1051,29 +1089,11 @@ void vulkan14_renderer::render_scene() } for (std::size_t i = 0; i < m_viewports.size(); ++i) { - auto const &viewport{m_viewports[i]}; - auto const srgb{false == Global.gfx_shadergamma}; - auto const *history{scene.targets[i].taa_history[0].get()}; - auto const *upscaled{scene.targets[i].dlss_output.get()}; - auto const *reflections{scene.targets[i].ssr_history.get()}; - auto const dlss{viewport.main && dlss_active() && (viewport.output_width > 0) && (viewport.output_height > 0)}; - if (false == make_targets(ctx.memory, ctx.deletions, ctx.uploads.current_value(), scene.formats_count, ctx.device.caps().limits.framebufferColorSampleCounts & ctx.device.caps().limits.framebufferDepthSampleCounts, scene.bloom_format, - scene.history_format, scene.targets[i], viewport.width, viewport.height, viewport.output_width, viewport.output_height, srgb, viewport.main && Global.gfx_taa(), - viewport.main && (false == Global.gfx_skippipeline) && ((Global.gfx_ssr > 0) || (m_raytracingon && scene.raytracing)), dlss)) + if (false == make_viewport_targets(i)) { continue; } - if (viewport.main && ((scene.targets[i].taa_history[0].get() != history) || (scene.targets[i].dlss_output.get() != upscaled))) - { - // new images of the history, nothing in them yet - m_taa.reset(); - m_dlssreset = true; - } auto &targets{scene.targets[i]}; - if (targets.ssr_history.get() != reflections) - { - m_ssr.reset(); - } if (targets.ssr_history && (false == targets.ssr_cleared)) { // the pass sets hold the history from now on, it's read (by none of the pixels) before the first frame goes to it diff --git a/rendering/vulkan14/vulkan14renderer.h b/rendering/vulkan14/vulkan14renderer.h index 66cf70fd3..22807fb4b 100644 --- a/rendering/vulkan14/vulkan14renderer.h +++ b/rendering/vulkan14/vulkan14renderer.h @@ -71,6 +71,7 @@ class vulkan14_renderer : public gfx_renderer, public gfx::scene_walk bool AddViewport(global_settings::extraviewport_config const &conf) override; void Shutdown() override; bool Render() override; + std::pair Prewarm_Shaders(double const Milliseconds) override; void SwapBuffers() override; float Framerate() override; // geometry @@ -512,6 +513,10 @@ class vulkan14_renderer : public gfx_renderer, public gfx::scene_walk // its own, for the gpu times). Repeated: a bit for each input read texel by texel and repeated (the noise of the ssao) void post_pass(char const *Fragment, vk14::image &Target, std::vector const &Inputs, vk14::draw_constants const &Constants, char const *Pass = nullptr, unsigned const Repeated = 0); bool bind_scene_pipeline(vk14::scene_program &Program, vk14::target_formats const &Targets, vk14::blend_mode const Blend, int const Topology); + // the images of a viewport for its size and the settings, made anew when they changed (render_scene(), Prewarm_Shaders()) + bool make_viewport_targets(std::size_t const Index); + // Prewarm_Shaders(): the pipelines the scene loaded will draw with, listed the first time + void list_prewarm_pipelines(); // Triangles: the draw is of triangles, which the prepass draws (keepdepth) void apply_draw_state(bool const Triangles = false); void push_textures(); diff --git a/rendering/vulkan14/vulkan14scene.cpp b/rendering/vulkan14/vulkan14scene.cpp index 335d4faeb..5b5878086 100644 --- a/rendering/vulkan14/vulkan14scene.cpp +++ b/rendering/vulkan14/vulkan14scene.cpp @@ -26,8 +26,11 @@ http://mozilla.org/MPL/2.0/. #include "utilities/utilities.h" #include +#include #include #include +#include +#include #include namespace @@ -2275,6 +2278,31 @@ bool vk14::scene_pipeline_record::parse(std::string const &Line) return true; } +namespace +{ + +// the settings of the game the shaders of the scene take as specialization constants, for the targets of a pass +std::uint32_t scene_pipeline_settings(vk14::target_formats const &Targets) +{ + auto const motion{Targets.colours > 1}; + return (Global.gfx_shadergamma ? 1u : 0u) | (Global.gfx_shadowmap_enabled ? 2u : 0u) | (Global.gfx_envmap_enabled ? 4u : 0u) | (motion ? 8u : 0u) | 16u | (Global.gfx_extraeffects ? 32u : 0u); +} + +// the key a pipeline of a program is kept by in the map of the program (vk14::scene_program::pipelines) +std::uint64_t scene_pipeline_key(vk14::target_formats const &Targets, vk14::blend_mode const Blend, VkPrimitiveTopology const Primitiveclass, std::uint32_t const Settings) +{ + return (static_cast(Targets.id)) | (static_cast(Blend) << 16) | (static_cast(Primitiveclass) << 24) | (static_cast(Settings) << 32); +} + +// the program has the pipeline of the key, made or found unable to be made +bool has_scene_pipeline(vk14::scene_program &Program, std::uint64_t const Key) +{ + std::scoped_lock lock(Program.mutex); + return Program.pipelines.contains(Key); +} + +} // namespace + // the pipeline of the program for the targets of the pass, the blending and the class of the primitive, made the first time it's // asked for; bound with the primitive as dynamic state. false: it can't be made, or it's being made by the background threads for // the walk of the scene, which leaves out the draw till it's there (gfx.vulkan.asyncresources, the stage M5) @@ -2286,8 +2314,8 @@ bool vulkan14_renderer::bind_scene_pipeline(vk14::scene_program &Program, vk14:: auto const primitiveclass{topology_class(topology)}; // the settings of the game the shaders take as specialization constants auto const motion{Targets.colours > 1}; - std::uint32_t const settings{(Global.gfx_shadergamma ? 1u : 0u) | (Global.gfx_shadowmap_enabled ? 2u : 0u) | (Global.gfx_envmap_enabled ? 4u : 0u) | (motion ? 8u : 0u) | 16u | (Global.gfx_extraeffects ? 32u : 0u)}; - auto const key{(static_cast(Targets.id)) | (static_cast(Blend) << 16) | (static_cast(primitiveclass) << 24) | (static_cast(settings) << 32)}; + auto const settings{scene_pipeline_settings(Targets)}; + auto const key{scene_pipeline_key(Targets, Blend, primitiveclass, settings)}; VkPipeline pipeline{VK_NULL_HANDLE}; if ((draw.lastprogram == &Program) && (draw.lastkey == key)) { @@ -2443,7 +2471,7 @@ bool vulkan14_renderer::context::find_scene_pipeline(vk14::scene_program &Progra } void vulkan14_renderer::context::queue_scene_pipeline(vk14::scene_program &Program, vk14::target_formats const &Targets, vk14::blend_mode const Blend, VkPrimitiveTopology const Primitiveclass, - std::uint32_t const Settings, bool const Single, std::uint64_t const Key) + std::uint32_t const Settings, bool const Single, std::uint64_t const Key, bool const Late) { auto &ctx{*this}; { @@ -2454,7 +2482,10 @@ void vulkan14_renderer::context::queue_scene_pipeline(vk14::scene_program &Progr return; } } - ++ctx.pipelines_late; + if (Late) + { + ++ctx.pipelines_late; + } // the program and the renderer outlive the background threads (stopped first when the scene goes) ctx.background.add([this, program = &Program, targets = Targets, Blend, Primitiveclass, Settings, Single, Key]() { auto const pipeline{make_scene_pipeline(*program, targets, Blend, Primitiveclass, Settings, Single)}; @@ -2464,6 +2495,110 @@ void vulkan14_renderer::context::queue_scene_pipeline(vk14::scene_program &Progr }); } +// the pipelines the scene loaded will draw with most: those of the programs of its materials for the colour of the main view, opaque +// and blended, and those of the shadows. what else a frame asks for (the variants of the gpu culling, the passes of the extra +// views, the reflections) is made when it's first needed, as before +void vulkan14_renderer::list_prewarm_pipelines() +{ + auto &ctx{*m_context}; + auto &scene{ctx.scene}; + ctx.prewarm_listed = true; + auto const add = [&ctx](std::shared_ptr const &Program, vk14::target_formats const &Targets, vk14::blend_mode const Blend) { + if ((Program == nullptr) || (Program->vertices != vk14::vertex_kind::scene) || (Targets.id == 0)) + { + return; + } + context::prewarm_pipeline pipeline; + pipeline.program = Program; + pipeline.targets = Targets; + pipeline.blend = Blend; + pipeline.settings = scene_pipeline_settings(Targets); + // a shader writing the velocity without its target is reported by the validation layer (bind_scene_pipeline()) + pipeline.single = (Targets.colours == 1); + pipeline.key = scene_pipeline_key(Targets, Blend, VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST, pipeline.settings); + ctx.prewarm.push_back(std::move(pipeline)); + }; + auto const main{std::ranges::find_if(m_viewports, [](viewport_config const &Viewport) { return Viewport.main; })}; + auto const index{static_cast(std::distance(m_viewports.begin(), main))}; + if ((main != m_viewports.end()) && make_viewport_targets(index)) + { + auto const &targets{scene.targets[index].formats}; + for (auto const &material : scene.materials) + { + add(material.second, targets, vk14::blend_mode::none); + add(material.second, targets, vk14::blend_mode::scene_alpha); + } + } + if (Global.gfx_shadowmap_enabled) + { + add(scene.shadow, scene.shadow_formats, vk14::blend_mode::none); + add(scene.alpha_shadow, scene.shadow_formats, vk14::blend_mode::none); + } + vk14::log_info("pipelines made behind the loading screen: " + std::to_string(ctx.prewarm.size())); +} + +// the pipelines the scene loaded will draw with, made while the loading screen is shown: by the background threads where they run, +// otherwise here, for about Milliseconds a call. a pipeline made for a draw instead would leave the draw out for a few frames (the +// background threads) or hold the frame while it's made. the pipelines of the list of the last session are waited for as well, +// and counted in what's returned +std::pair vulkan14_renderer::Prewarm_Shaders(double const Milliseconds) +{ + if (m_context == nullptr) + { + return {0, 0}; + } + auto &ctx{*m_context}; + if (false == ctx.prewarm_listed) + { + list_prewarm_pipelines(); + } + auto const background{Global.gfx_vulkan_asyncresources && ctx.background.running()}; + if (background) + { + // handed over once, made ahead of what the draws ask for later + for (; ctx.prewarm_next < ctx.prewarm.size(); ++ctx.prewarm_next) + { + auto const &pipeline{ctx.prewarm[ctx.prewarm_next]}; + if (false == has_scene_pipeline(*pipeline.program, pipeline.key)) + { + ctx.queue_scene_pipeline(*pipeline.program, pipeline.targets, pipeline.blend, VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST, pipeline.settings, pipeline.single, pipeline.key, false); + } + } + } + else + { + auto const start{std::chrono::steady_clock::now()}; + while ((ctx.prewarm_next < ctx.prewarm.size()) && (std::chrono::duration(std::chrono::steady_clock::now() - start).count() < Milliseconds)) + { + auto const &pipeline{ctx.prewarm[ctx.prewarm_next]}; + ++ctx.prewarm_next; + if (has_scene_pipeline(*pipeline.program, pipeline.key)) + { + continue; + } + auto const made{ctx.make_scene_pipeline(*pipeline.program, pipeline.targets, pipeline.blend, VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST, pipeline.settings, pipeline.single)}; + std::scoped_lock lock(pipeline.program->mutex); + pipeline.program->pipelines.emplace(pipeline.key, made); + } + } + auto const done{static_cast(std::ranges::count_if(ctx.prewarm, [](context::prewarm_pipeline const &Pipeline) { return has_scene_pipeline(*Pipeline.program, Pipeline.key); }))}; + // the pipelines of the list of the last session count too, up to 4096 of them from any scenery played before: with a cold + // cache of the driver they take most of the time, and the bar moves with them. then one more step: the background threads + // idle, which ends the wait whatever the counts say (a task dropped is never counted as done) + auto const listed{ctx.warm_queued.load()}; + auto const warmed{std::min(ctx.warm_finished.load(), listed)}; + auto const idle{(false == background) || (ctx.background.pending() == 0)}; + auto const total{ctx.prewarm.size() + listed + 1}; + if (idle && (done == ctx.prewarm.size())) + { + // all made: the programs aren't held past the loading screen + ctx.prewarm.clear(); + ctx.prewarm_next = 0; + return {total, total}; + } + return {done + warmed, total}; +} + // the pipelines of the list of the last session made ahead by the background threads, after the ones the draws wait for void vulkan14_renderer::context::warm_scene_pipelines() { @@ -2500,6 +2635,7 @@ void vulkan14_renderer::context::warm_scene_pipelines() } vk14::log_info("pipelines of the last session made ahead: " + std::to_string(records.size())); auto const layout{ctx.scene.scene_layout}; + ctx.warm_queued += records.size(); for (auto const &record : records) { ctx.background.add( @@ -2518,6 +2654,7 @@ void vulkan14_renderer::context::warm_scene_pipelines() // the shader went or changed meanwhile vk14::log_info(std::string("a pipeline of the last session can't be made ahead: ") + Exception.what()); } + ++ctx.warm_finished; }, false); }