From 00a72a7b30436b9c85f73408301756fff97c8217 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tur=C3=A1nszki=20J=C3=A1nos?= Date: Tue, 4 Jun 2024 07:32:26 +0200 Subject: [PATCH] allocation removals and fixes --- Editor/Editor.h | 2 +- Editor/GeneralWindow.cpp | 4 --- Editor/GraphicsWindow.cpp | 2 +- Editor/main_Windows.cpp | 33 +++++++++++++---------- WickedEngine/wiApplication.cpp | 4 ++- WickedEngine/wiApplication.h | 1 + WickedEngine/wiHelper.cpp | 3 ++- WickedEngine/wiResourceManager.cpp | 43 ++++++++++++++++++++---------- WickedEngine/wiScene.cpp | 10 +++++++ WickedEngine/wiVersion.cpp | 4 +-- 10 files changed, 68 insertions(+), 38 deletions(-) diff --git a/Editor/Editor.h b/Editor/Editor.h index f8dd9369e..a7914e28f 100644 --- a/Editor/Editor.h +++ b/Editor/Editor.h @@ -229,7 +229,7 @@ public: void Initialize() override; - ~Editor() + ~Editor() override { config.Commit(); } diff --git a/Editor/GeneralWindow.cpp b/Editor/GeneralWindow.cpp index 2108471d3..fbd8368d4 100644 --- a/Editor/GeneralWindow.cpp +++ b/Editor/GeneralWindow.cpp @@ -279,7 +279,6 @@ void GeneralWindow::Create(EditorComponent* _editor) { editor->SetLocalization(editor->default_localization); editor->main->config.GetSection("options").Set("language", "English"); - editor->main->config.Commit(); return; } @@ -289,7 +288,6 @@ void GeneralWindow::Create(EditorComponent* _editor) { editor->SetLocalization(editor->current_localization); editor->main->config.GetSection("options").Set("language", language); - editor->main->config.Commit(); } else { @@ -376,8 +374,6 @@ void GeneralWindow::Create(EditorComponent* _editor) theme.font.shadow_color = wi::Color::Shadow(); break; } - - editor->main->config.Commit(); theme.tooltipImage = theme.image; theme.tooltipImage.color = theme_color_idle; diff --git a/Editor/GraphicsWindow.cpp b/Editor/GraphicsWindow.cpp index 9d1b71e37..5a6eecc11 100644 --- a/Editor/GraphicsWindow.cpp +++ b/Editor/GraphicsWindow.cpp @@ -1525,7 +1525,7 @@ void GraphicsWindow::Update() resolutionScaleSlider.SetValue(editor->resolutionScale); streamingSlider.SetValue(wi::resourcemanager::GetStreamingMemoryThreshold()); MSAAComboBox.SetSelectedByUserdataWithoutCallback(editor->renderPath->getMSAASampleCount()); - tonemapCombo.SetSelected((int)editor->renderPath->getTonemap()); + tonemapCombo.SetSelectedByUserdataWithoutCallback((int)editor->renderPath->getTonemap()); exposureSlider.SetValue(editor->renderPath->getExposure()); brightnessSlider.SetValue(editor->renderPath->getBrightness()); contrastSlider.SetValue(editor->renderPath->getContrast()); diff --git a/Editor/main_Windows.cpp b/Editor/main_Windows.cpp index 245d3495c..94f0abce6 100644 --- a/Editor/main_Windows.cpp +++ b/Editor/main_Windows.cpp @@ -278,20 +278,25 @@ LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) editor.is_window_active = true; if (wi::shadercompiler::GetRegisteredShaderCount() > 0) { - std::thread([] { - wi::backlog::post("[Shader check] Started checking " + std::to_string(wi::shadercompiler::GetRegisteredShaderCount()) + " registered shaders for changes..."); - if (wi::shadercompiler::CheckRegisteredShadersOutdated()) - { - wi::backlog::post("[Shader check] Changes detected, initiating reload..."); - wi::eventhandler::Subscribe_Once(wi::eventhandler::EVENT_THREAD_SAFE_POINT, [](uint64_t userdata) { - wi::renderer::ReloadShaders(); - }); - } - else - { - wi::backlog::post("[Shader check] All up to date"); - } - }).detach(); + static wi::jobsystem::context shader_check_ctx; + if (!wi::jobsystem::IsBusy(shader_check_ctx)) + { + shader_check_ctx.priority = wi::jobsystem::Priority::Low; + wi::jobsystem::Execute(shader_check_ctx, [](wi::jobsystem::JobArgs args) { + wi::backlog::post("[Shader check] Started checking " + std::to_string(wi::shadercompiler::GetRegisteredShaderCount()) + " registered shaders for changes..."); + if (wi::shadercompiler::CheckRegisteredShadersOutdated()) + { + wi::backlog::post("[Shader check] Changes detected, initiating reload..."); + wi::eventhandler::Subscribe_Once(wi::eventhandler::EVENT_THREAD_SAFE_POINT, [](uint64_t userdata) { + wi::renderer::ReloadShaders(); + }); + } + else + { + wi::backlog::post("[Shader check] All up to date"); + } + }); + } } editor.renderComponent.ReloadLanguage(); break; diff --git a/WickedEngine/wiApplication.cpp b/WickedEngine/wiApplication.cpp index c32bb467b..360cd6730 100644 --- a/WickedEngine/wiApplication.cpp +++ b/WickedEngine/wiApplication.cpp @@ -50,6 +50,8 @@ namespace wi initialized = true; wi::initializer::InitializeComponentsAsync(); + + alwaysactive = wi::arguments::HasArgument("alwaysactive"); } void Application::ActivatePath(RenderPath* component, float fadeSeconds, wi::Color fadeColor) @@ -132,7 +134,7 @@ namespace wi } } - if (!is_window_active && !wi::arguments::HasArgument("alwaysactive")) + if (!is_window_active && !alwaysactive) { // If the application is not active, disable Update loops: deltaTimeAccumulator = 0; diff --git a/WickedEngine/wiApplication.h b/WickedEngine/wiApplication.h index 1e077521c..be800a4c4 100644 --- a/WickedEngine/wiApplication.h +++ b/WickedEngine/wiApplication.h @@ -29,6 +29,7 @@ namespace wi bool frameskip = true; bool framerate_lock = false; bool initialized = false; + bool alwaysactive = false; wi::FadeManager fadeManager; diff --git a/WickedEngine/wiHelper.cpp b/WickedEngine/wiHelper.cpp index a1ffa0954..2ff98e9ee 100644 --- a/WickedEngine/wiHelper.cpp +++ b/WickedEngine/wiHelper.cpp @@ -1022,7 +1022,7 @@ namespace wi::helper #endif // PLATFORM_LINUX || PLATFORM_PS5 if (file.is_open()) { - size_t dataSize = (size_t)file.tellg(); + size_t dataSize = (size_t)file.tellg() - offset; dataSize = std::min(dataSize, max_read); file.seekg((std::streampos)offset); data.resize(dataSize); @@ -1169,6 +1169,7 @@ namespace wi::helper } #endif // PLATFORM_UWP + wi::backlog::post("File couln't be written: " + fileName, wi::backlog::LogLevel::Error); return false; } diff --git a/WickedEngine/wiResourceManager.cpp b/WickedEngine/wiResourceManager.cpp index c5cfe4435..ec1116e64 100644 --- a/WickedEngine/wiResourceManager.cpp +++ b/WickedEngine/wiResourceManager.cpp @@ -762,19 +762,6 @@ namespace wi desc.swizzle.a = ComponentSwizzle::ONE; } - wi::vector initdata; - initdata.reserve(desc.array_size * desc.mip_levels); - for (uint32_t slice = 0; slice < desc.array_size; ++slice) - { - for (uint32_t mip = 0; mip < desc.mip_levels; ++mip) - { - SubresourceData& subresourceData = initdata.emplace_back(); - subresourceData.data_ptr = filedata + header.mip_offset(mip, slice); - subresourceData.row_pitch = header.row_pitch(mip); - subresourceData.slice_pitch = header.slice_pitch(mip); - } - } - if (header.is_1d()) { desc.type = TextureDesc::Type::TEXTURE_1D; @@ -790,6 +777,33 @@ namespace wi desc.height = AlignTo(desc.height, GetFormatBlockSize(desc.format)); } + wi::vector initdata_heap; + SubresourceData initdata_stack[16] = {}; + SubresourceData* initdata = nullptr; + + // Determine if we need heap allocation for initdata, or it is small enough for stack: + if (desc.array_size * desc.mip_levels < arraysize(initdata_stack)) + { + initdata = initdata_stack; + } + else + { + initdata_heap.resize(desc.array_size * desc.mip_levels); + initdata = initdata_heap.data(); + } + + uint32_t subresource_index = 0; + for (uint32_t slice = 0; slice < desc.array_size; ++slice) + { + for (uint32_t mip = 0; mip < desc.mip_levels; ++mip) + { + SubresourceData& subresourceData = initdata_stack[subresource_index++]; + subresourceData.data_ptr = filedata + header.mip_offset(mip, slice); + subresourceData.row_pitch = header.row_pitch(mip); + subresourceData.slice_pitch = header.slice_pitch(mip); + } + } + int mip_offset = 0; if (has_flag(flags, Flags::STREAMING)) { @@ -806,6 +820,7 @@ namespace wi streaming_data.slice_pitch = header.slice_pitch(mip); } } + // Reduce mip map count that will be uploaded to GPU: while (desc.mip_levels > 1 && desc.depth == 1 && desc.array_size == 1 && ComputeTextureMemorySizeInBytes(desc) > streaming_texture_min_size) { desc.width >>= 1; @@ -816,7 +831,7 @@ namespace wi resource->streaming_texture.min_lod_clamp_absolute = (float)mip_offset; } - success = device->CreateTexture(&desc, initdata.data() + mip_offset, &resource->texture); + success = device->CreateTexture(&desc, initdata + mip_offset, &resource->texture); device->SetName(&resource->texture, name.c_str()); Format srgb_format = GetFormatSRGB(desc.format); diff --git a/WickedEngine/wiScene.cpp b/WickedEngine/wiScene.cpp index 71fe00393..5425b4f3e 100644 --- a/WickedEngine/wiScene.cpp +++ b/WickedEngine/wiScene.cpp @@ -5021,6 +5021,8 @@ namespace wi::scene if ((filterMask & FILTER_COLLIDER) && collider_bvh.IsValid()) { collider_bvh.Intersects(ray, 0, [&](uint32_t collider_index) { + if (colliders.GetCount() <= collider_index) + return; const ColliderComponent& collider = colliders_cpu[collider_index]; if ((collider.layerMask & layerMask) == 0) @@ -5246,6 +5248,8 @@ namespace wi::scene if ((filterMask & FILTER_COLLIDER) && collider_bvh.IsValid()) { collider_bvh.IntersectsFirst(ray, [&](uint32_t collider_index) { + if (colliders.GetCount() <= collider_index) + return false; const ColliderComponent& collider = colliders_cpu[collider_index]; if ((collider.layerMask & layerMask) == 0) @@ -5413,6 +5417,8 @@ namespace wi::scene if ((filterMask & FILTER_COLLIDER) && collider_bvh.IsValid()) { collider_bvh.Intersects(sphere, 0, [&](uint32_t collider_index) { + if (colliders.GetCount() <= collider_index) + return; const ColliderComponent& collider = colliders_cpu[collider_index]; if ((collider.layerMask & layerMask) == 0) @@ -5694,6 +5700,8 @@ namespace wi::scene if ((filterMask & FILTER_COLLIDER) && collider_bvh.IsValid()) { collider_bvh.Intersects(capsule_aabb, 0, [&](uint32_t collider_index) { + if (colliders.GetCount() <= collider_index) + return; const ColliderComponent& collider = colliders_cpu[collider_index]; if ((collider.layerMask & layerMask) == 0) @@ -6877,6 +6885,8 @@ namespace wi::scene if (colliders_cpu != nullptr) { collider_bvh.Intersects(tail_sphere, 0, [&](uint32_t collider_index) { + if (colliders.GetCount() <= collider_index) + return; const ColliderComponent& collider = colliders_cpu[collider_index]; float dist = 0; diff --git a/WickedEngine/wiVersion.cpp b/WickedEngine/wiVersion.cpp index de8daeb2c..6ce4da7e7 100644 --- a/WickedEngine/wiVersion.cpp +++ b/WickedEngine/wiVersion.cpp @@ -9,7 +9,7 @@ namespace wi::version // minor features, major updates, breaking compatibility changes const int minor = 71; // minor bug fixes, alterations, refactors, updates - const int revision = 475; + const int revision = 476; const std::string version_string = std::to_string(major) + "." + std::to_string(minor) + "." + std::to_string(revision); @@ -50,7 +50,7 @@ All contributors: https://github.com/turanszkij/WickedEngine/graphs/contributors Patreon supporters --------------------------- -Nemerle, James Webb, Quifeng Jin, TheGameCreators, Joseph Goldin, Yuri, Sergey K, Yukawa Kanta, Dragon Josh, John, LurkingNinja, Bernardo Del Castillo, Invictus, Scott Hunt, Yazan Altaki, Tuan NV, Robert MacGregor, cybernescence, Alexander Dahlin, blueapples, Delhills, NI NI, Sherief, ktopoet, Justin Macklin, Cédric Fabre, TogetherTeam, Bartosz Boczula, Arne Koenig, Ivan Trajchev, nathants, Fahd Ahmed, Gabriel Jadderson, SAS_Controller, Dominik Madarász, Segfault, Mike amanfo, Dennis Brakhane, rookie, Peter Moore, therealjtgill, Nicolas Embleton, Desuuc, radino1977, Anthony Curtis, manni heck, Matthias Hölzl, Phyffer, Lucas Pinheiro, Tapkaara, gpman, Anthony Python, Gnowos, Klaus, slaughternaut, Paul Brain, Connor Greaves, Alexandr, Lee Bamber, MCAlarm MC2, Titoutan, Willow, Aldo, lokimx, K. Osterman, Nomad, ykl, Alex Krokos, Timmy, Avaflow, mat, Hexegonel Samael Michael, Joe Spataro, soru +Nemerle, James Webb, Quifeng Jin, TheGameCreators, Joseph Goldin, Yuri, Sergey K, Yukawa Kanta, Dragon Josh, John, LurkingNinja, Bernardo Del Castillo, Invictus, Scott Hunt, Yazan Altaki, Tuan NV, Robert MacGregor, cybernescence, Alexander Dahlin, blueapples, Delhills, NI NI, Sherief, ktopoet, Justin Macklin, Cédric Fabre, TogetherTeam, Bartosz Boczula, Arne Koenig, Ivan Trajchev, nathants, Fahd Ahmed, Gabriel Jadderson, SAS_Controller, Dominik Madarász, Segfault, Mike amanfo, Dennis Brakhane, rookie, Peter Moore, therealjtgill, Nicolas Embleton, Desuuc, radino1977, Anthony Curtis, manni heck, Matthias Hölzl, Phyffer, Lucas Pinheiro, Tapkaara, gpman, Anthony Python, Gnowos, Klaus, slaughternaut, Paul Brain, Connor Greaves, Alexandr, Lee Bamber, MCAlarm MC2, Titoutan, Willow, Aldo, lokimx, K. Osterman, Nomad, ykl, Alex Krokos, Timmy, Avaflow, mat, Hexegonel Samael Michael, Joe Spataro, soru, GeniokV, Mammoth )"; return credits;