mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-11-27 23:57:09 +01:00
* 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 <noreply@anthropic.com> * 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 <noreply@anthropic.com> * 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 <noreply@anthropic.com> * 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 <noreply@anthropic.com> * 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 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01QRgQ1LqB4YXWorLDBBL6hD --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
354 lines
12 KiB
C++
354 lines
12 KiB
C++
/*
|
|
This Source Code Form is subject to the
|
|
terms of the Mozilla Public License, v.
|
|
2.0. If a copy of the MPL was not
|
|
distributed with this file, You can
|
|
obtain one at
|
|
http://mozilla.org/MPL/2.0/.
|
|
*/
|
|
|
|
#include "application/scenarioloaderuilayer.h"
|
|
|
|
#include "utilities/Globals.h"
|
|
#include "rendering/renderer.h"
|
|
#include "utilities/translation.h"
|
|
#include <nlohmann/json.hpp>
|
|
#include "utilities/Logs.h"
|
|
#include <sstream>
|
|
|
|
using json = nlohmann::json;
|
|
|
|
// Helper function to count UTF-8 characters (not bytes)
|
|
size_t utf8_char_count(const std::string& str)
|
|
{
|
|
size_t count = 0;
|
|
for (size_t i = 0; i < str.length(); )
|
|
{
|
|
unsigned char c = static_cast<unsigned char>(str[i]);
|
|
if ((c & 0x80) == 0x00)
|
|
{
|
|
// 1-byte character (ASCII)
|
|
i += 1;
|
|
}
|
|
else if ((c & 0xE0) == 0xC0)
|
|
{
|
|
// 2-byte character
|
|
i += 2;
|
|
}
|
|
else if ((c & 0xF0) == 0xE0)
|
|
{
|
|
// 3-byte character (most Chinese characters)
|
|
i += 3;
|
|
}
|
|
else if ((c & 0xF8) == 0xF0)
|
|
{
|
|
// 4-byte character
|
|
i += 4;
|
|
}
|
|
else
|
|
{
|
|
// Invalid UTF-8, skip
|
|
i += 1;
|
|
}
|
|
count++;
|
|
}
|
|
return count;
|
|
}
|
|
|
|
scenarioloader_ui::scenarioloader_ui()
|
|
{
|
|
m_suppress_menu = true;
|
|
generate_gradient_tex();
|
|
load_wheel_frames();
|
|
|
|
m_trivia = get_random_trivia();
|
|
}
|
|
|
|
|
|
std::vector<std::string> scenarioloader_ui::get_random_trivia()
|
|
{
|
|
WriteLog("Loading random trivia...");
|
|
std::vector<std::string> trivia = std::vector<std::string>();
|
|
|
|
if (!FileExists("lang/trivia_" + Global.asLang + ".json")
|
|
&& !FileExists("lang/trivia_en.json"))
|
|
{
|
|
ErrorLog("Trivia file not found!");
|
|
return trivia;
|
|
}
|
|
|
|
std::string triviaFile = FileExists("lang/trivia_" + Global.asLang + ".json") ? "lang/trivia_" + Global.asLang + ".json" : "lang/trivia_en.json";
|
|
|
|
//std::string lang = Global.asLang;
|
|
WriteLog("Selected language: " + Global.asLang);
|
|
// Read file in binary mode to preserve UTF-8 encoding
|
|
std::ifstream f(triviaFile, std::ios::binary);
|
|
std::stringstream buffer;
|
|
buffer << f.rdbuf();
|
|
std::string fileContent = buffer.str();
|
|
json triviaData = json::parse(fileContent);
|
|
|
|
// select random
|
|
int i = RandomInt(0, static_cast<int>(triviaData.size()) - 1);
|
|
std::string triviaStr = triviaData[i]["text"];
|
|
std::string background = triviaData[i]["background"];
|
|
if (triviaData[i].contains("scenery"))
|
|
sceneryName = triviaData[i]["scenery"];
|
|
|
|
// divide trivia into multiple lines - UTF-8 safe implementation
|
|
// Different languages need different character limits due to character width differences
|
|
int max_line_length = 100; // Default for Latin languages
|
|
if (Global.asLang == "zh")
|
|
{
|
|
max_line_length = 60; // Reduced for Chinese
|
|
}
|
|
|
|
// Find byte position corresponding to character count
|
|
auto get_byte_pos_for_char_count = [&](const std::string& str, size_t char_count) -> size_t {
|
|
size_t byte_pos = 0;
|
|
size_t current_chars = 0;
|
|
for (size_t i = 0; i < str.length() && current_chars < char_count; )
|
|
{
|
|
unsigned char c = static_cast<unsigned char>(str[i]);
|
|
if ((c & 0x80) == 0x00)
|
|
{
|
|
i += 1;
|
|
}
|
|
else if ((c & 0xE0) == 0xC0)
|
|
{
|
|
i += 2;
|
|
}
|
|
else if ((c & 0xF0) == 0xE0)
|
|
{
|
|
i += 3;
|
|
}
|
|
else if ((c & 0xF8) == 0xF0)
|
|
{
|
|
i += 4;
|
|
}
|
|
else
|
|
{
|
|
i += 1;
|
|
}
|
|
current_chars++;
|
|
byte_pos = i;
|
|
}
|
|
return byte_pos;
|
|
};
|
|
|
|
// UTF-8 safe find space within character limit
|
|
auto find_space_before_char_count = [&](const std::string& str, size_t max_chars) -> size_t {
|
|
size_t byte_limit = get_byte_pos_for_char_count(str, max_chars);
|
|
size_t search_limit = std::min(str.length(), byte_limit);
|
|
|
|
// Search backwards for a space that's not in the middle of a UTF-8 sequence
|
|
for (int idx = static_cast<int>(search_limit); idx >= 0; idx--) {
|
|
// Check if this position is a valid place to split (not in middle of UTF-8 char)
|
|
// A UTF-8 continuation byte has the form 10xxxxxx (0x80-0xBF)
|
|
if (idx == 0 || (static_cast<unsigned char>(str[idx - 1]) & 0xC0) != 0x80) {
|
|
// This is the start of a character (or beginning of string), check if it's a space
|
|
if (idx > 0 && str[idx - 1] == ' ') {
|
|
return idx - 1; // Position of the space
|
|
}
|
|
}
|
|
}
|
|
return std::string::npos; // No suitable space found
|
|
};
|
|
|
|
// Use character count for proper UTF-8 text wrapping
|
|
while (utf8_char_count(triviaStr) > static_cast<size_t>(max_line_length))
|
|
{
|
|
size_t split_pos = find_space_before_char_count(triviaStr, static_cast<size_t>(max_line_length));
|
|
if (split_pos == std::string::npos)
|
|
{
|
|
// No space found, split at max character count
|
|
split_pos = get_byte_pos_for_char_count(triviaStr, static_cast<size_t>(max_line_length));
|
|
}
|
|
trivia.push_back(triviaStr.substr(0, split_pos));
|
|
triviaStr = triviaStr.substr(split_pos);
|
|
// Skip leading whitespace in the next line
|
|
while (!triviaStr.empty() && triviaStr[0] == ' ')
|
|
{
|
|
triviaStr = triviaStr.substr(1);
|
|
}
|
|
}
|
|
|
|
// if triviaStr is not empty add this as last line
|
|
if (!triviaStr.empty())
|
|
trivia.push_back(triviaStr);
|
|
|
|
// now override background if trivia is set
|
|
if (!trivia.empty())
|
|
{
|
|
set_background("textures/ui/backgrounds/" + background);
|
|
}
|
|
|
|
return trivia;
|
|
}
|
|
|
|
void scenarioloader_ui::render_()
|
|
{
|
|
// For some reason, ImGui windows have some padding. Offset it.
|
|
// TODO: Find out a way to exactly adjust the position.
|
|
constexpr int padding = 12;
|
|
ImVec2 screen_size(Global.window_size.x, Global.window_size.y);
|
|
ImGui::SetNextWindowPos(ImVec2(-padding, -padding));
|
|
ImGui::SetNextWindowSize(ImVec2(Global.window_size.x + padding * 2, Global.window_size.y + padding * 2));
|
|
// it reaches past the edges of the main window, which would make it a window of its own with the panels able to leave the main one
|
|
ImGui::SetNextWindowViewport(ImGui::GetMainViewport()->ID);
|
|
ImGui::Begin("Neo Loading Screen", nullptr, ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoScrollbar | ImGuiWindowFlags_NoSavedSettings | ImGuiWindowFlags_NoInputs | ImGuiWindowFlags_NoBackground);
|
|
ImDrawList* draw_list = ImGui::GetWindowDrawList();
|
|
ImGui::PushFont(font_loading);
|
|
ImGui::SetWindowFontScale(1);
|
|
const float scale = Global.window_size.x / 1920.f;
|
|
const float font_scale_mult = 48 / ImGui::GetFontSize() * scale;
|
|
|
|
// Gradient at the lower half of the screen
|
|
const ImTextureID tex = (ImTextureID)(intptr_t)m_gradient_overlay_tex; // See https://github.com/ocornut/imgui/wiki/Image-Loading-and-Displaying-Examples#example-for-opengl-users
|
|
draw_list->AddImage(tex, ImVec2(0, Global.window_size.y / 2), ImVec2(Global.window_size.x, Global.window_size.y), ImVec2(0, 0), ImVec2(1, 1));
|
|
|
|
// [O] Loading...
|
|
const float margin_left_icon = 35.0f * scale;
|
|
const float margin_bottom_loading = 80.0f * scale;
|
|
const float spacing = 10.0f * scale; // odstęp między ikoną a tekstem
|
|
|
|
// Loading icon
|
|
deferred_image *img = &m_loading_wheel_frames[38];
|
|
const auto loading_tex = img->get();
|
|
const auto loading_size = glm::vec2(img->size()) * glm::vec2(scale);
|
|
|
|
ImVec2 icon_pos(margin_left_icon, screen_size.y - margin_bottom_loading - loading_size.y);
|
|
|
|
// Loading text
|
|
ImGui::SetWindowFontScale(font_scale_mult * 0.8f);
|
|
ImVec2 text_size = ImGui::CalcTextSize(m_progresstext.c_str());
|
|
|
|
// Vertical centering of text relative to icon
|
|
float icon_center_y = icon_pos.y + loading_size.y * 0.5f;
|
|
ImVec2 text_pos(icon_pos.x + loading_size.x + spacing, // tuż obok ikony
|
|
icon_center_y - text_size.y * 0.5f);
|
|
|
|
// Draw
|
|
//draw_list->AddImage(reinterpret_cast<ImTextureID>(loading_tex), icon_pos, ImVec2(icon_pos.x + loading_size.x, icon_pos.y + loading_size.y), ImVec2(0, 0), ImVec2(1, 1));
|
|
draw_list->AddText(text_pos, IM_COL32_WHITE, m_progresstext.c_str());
|
|
|
|
// Trivia
|
|
// draw only if we have any trivia loaded
|
|
if (m_trivia.size() > 0)
|
|
{
|
|
const float margin_right = 80.0f * scale;
|
|
const float margin_bottom = 80.0f * scale;
|
|
const float line_spacing = 25.0f * scale;
|
|
const float header_gap = 10.0f * scale;
|
|
|
|
// Measure width of trivia lines
|
|
ImGui::SetWindowFontScale(font_scale_mult * 0.6f);
|
|
float max_width = 0.0f;
|
|
for (const std::string &line : m_trivia)
|
|
{
|
|
ImVec2 size = ImGui::CalcTextSize(line.c_str());
|
|
if (size.x > max_width)
|
|
max_width = size.x;
|
|
}
|
|
|
|
// Measure header width
|
|
ImGui::SetWindowFontScale(font_scale_mult * 1.0f);
|
|
ImVec2 header_size = ImGui::CalcTextSize(STR_C("Did you know?"));
|
|
if (header_size.x > max_width)
|
|
max_width = header_size.x; // blok musi też pomieścić nagłówek
|
|
|
|
// Calculate block position
|
|
float content_height = (float)m_trivia.size() * line_spacing;
|
|
float total_height = header_size.y + header_gap + content_height;
|
|
|
|
float block_left = screen_size.x - margin_right - max_width;
|
|
float block_top = screen_size.y - margin_bottom - total_height;
|
|
|
|
// Draw header
|
|
ImVec2 header_pos(block_left + (max_width - header_size.x) * 0.5f, block_top);
|
|
draw_list->AddText(header_pos, IM_COL32_WHITE, STR_C("Did you know?"));
|
|
|
|
// Draw trivia lines
|
|
ImGui::SetWindowFontScale(font_scale_mult * 0.6f);
|
|
for (int i = 0; i < m_trivia.size(); i++)
|
|
{
|
|
const std::string &line = m_trivia[i];
|
|
ImVec2 text_size = ImGui::CalcTextSize(line.c_str());
|
|
ImVec2 text_pos(block_left + (max_width - text_size.x) * 0.5f, block_top + header_size.y + header_gap + i * line_spacing);
|
|
draw_list->AddText(text_pos, IM_COL32_WHITE, line.c_str());
|
|
}
|
|
}
|
|
|
|
// Scenery name
|
|
// Draw only if defined
|
|
if (!sceneryName.empty())
|
|
{
|
|
ImVec2 text_size = ImGui::CalcTextSize(sceneryName.c_str());
|
|
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);
|
|
draw_list->AddRectFilled(p1, p2, ImColor(40, 210, 60, 255));
|
|
ImGui::PopFont();
|
|
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<float>(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;
|
|
constexpr int image_height = 1024;
|
|
const auto image_data = new char[image_width * image_height * 4];
|
|
for (int x = 0; x < image_width; x++)
|
|
for (int y = 0; y < image_height; y++)
|
|
{
|
|
image_data[(y * image_width + x) * 4] = 0;
|
|
image_data[(y * image_width + x) * 4 + 1] = 0;
|
|
image_data[(y * image_width + x) * 4 + 2] = 0;
|
|
image_data[(y * image_width + x) * 4 + 3] = std::clamp(static_cast<int>(pow(y / 1023.f, 0.7) * 255), 0, 255);
|
|
}
|
|
|
|
// an image of the user interface: the values as they are (not srgb), the nearest texel
|
|
imgui_renderer::image_options options;
|
|
options.srgb = false;
|
|
options.nearest = true;
|
|
std::uint64_t image_texture{0};
|
|
if (auto *renderer{GfxRenderer->GetImguiRenderer()})
|
|
image_texture = renderer->Create_Image(reinterpret_cast<std::uint8_t const *>(image_data), image_width, image_height, options);
|
|
|
|
delete[] image_data;
|
|
|
|
m_gradient_overlay_width = image_width;
|
|
m_gradient_overlay_height = image_height;
|
|
m_gradient_overlay_tex = image_texture;
|
|
}
|
|
|
|
void scenarioloader_ui::load_wheel_frames()
|
|
{
|
|
for (int i = 0; i < 60; i++)
|
|
m_loading_wheel_frames[i] = deferred_image("ui/loading_wheel/" + std::to_string(i + 1));
|
|
}
|