From 65ecd0195b7fb5c9514a52c117eb8c7e6e83779e Mon Sep 17 00:00:00 2001 From: assiduous Date: Mon, 14 Oct 2024 12:35:50 -0700 Subject: [PATCH 1/4] ThreadPool: allow task cancellation in EnqueueAsyncWork --- Common/interface/ThreadPool.hpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Common/interface/ThreadPool.hpp b/Common/interface/ThreadPool.hpp index ae69cd507..4b80b7b27 100644 --- a/Common/interface/ThreadPool.hpp +++ b/Common/interface/ThreadPool.hpp @@ -185,7 +185,9 @@ RefCntAutoPtr EnqueueAsyncWork(IThreadPool* pThreadPool, virtual void DILIGENT_CALL_TYPE Run(Uint32 ThreadId) override final { - ASYNC_TASK_STATUS TaskStatus = m_Handler(ThreadId); + ASYNC_TASK_STATUS TaskStatus = !m_bSafelyCancel.load() ? + m_Handler(ThreadId) : + ASYNC_TASK_STATUS_CANCELLED; SetStatus(TaskStatus); } From 48f769e8b36f0e803a5683ecbfbeedbc52eb556a Mon Sep 17 00:00:00 2001 From: assiduous Date: Mon, 14 Oct 2024 14:28:06 -0700 Subject: [PATCH 2/4] GraphicsAccessories: added GetStagingTextureDataSize function --- .../GraphicsAccessories/interface/GraphicsAccessories.hpp | 6 ++++++ Graphics/GraphicsEngineOpenGL/src/TextureBaseGL.cpp | 2 +- Graphics/GraphicsEngineVulkan/src/TextureVkImpl.cpp | 4 ++-- 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/Graphics/GraphicsAccessories/interface/GraphicsAccessories.hpp b/Graphics/GraphicsAccessories/interface/GraphicsAccessories.hpp index 6dfb81510..ba1d8af12 100644 --- a/Graphics/GraphicsAccessories/interface/GraphicsAccessories.hpp +++ b/Graphics/GraphicsAccessories/interface/GraphicsAccessories.hpp @@ -697,6 +697,12 @@ inline Uint64 GetStagingTextureSubresourceOffset(const TextureDesc& TexDesc, return GetStagingTextureLocationOffset(TexDesc, ArraySlice, MipLevel, Alignment, 0, 0, 0); } +/// Returns the total memory size required to store the staging texture data. +inline Uint64 GetStagingTextureDataSize(const TextureDesc& TexDesc, + Uint32 Alignment = 4) +{ + return GetStagingTextureSubresourceOffset(TexDesc, TexDesc.GetArraySize(), 0, Alignment); +} /// Information required to perform a copy operation between a buffer and a texture struct BufferToTextureCopyInfo diff --git a/Graphics/GraphicsEngineOpenGL/src/TextureBaseGL.cpp b/Graphics/GraphicsEngineOpenGL/src/TextureBaseGL.cpp index 6c7e78330..151198109 100644 --- a/Graphics/GraphicsEngineOpenGL/src/TextureBaseGL.cpp +++ b/Graphics/GraphicsEngineOpenGL/src/TextureBaseGL.cpp @@ -75,7 +75,7 @@ TextureBaseGL::TextureBaseGL(IReferenceCounters* pRefCounters, StagingBuffName += '\''; StagingBufferDesc.Name = StagingBuffName.c_str(); - StagingBufferDesc.Size = GetStagingTextureSubresourceOffset(m_Desc, m_Desc.GetArraySize(), 0, PBOOffsetAlignment); + StagingBufferDesc.Size = GetStagingTextureDataSize(m_Desc, PBOOffsetAlignment); StagingBufferDesc.Usage = USAGE_STAGING; StagingBufferDesc.CPUAccessFlags = TexDesc.CPUAccessFlags; diff --git a/Graphics/GraphicsEngineVulkan/src/TextureVkImpl.cpp b/Graphics/GraphicsEngineVulkan/src/TextureVkImpl.cpp index 288c50c13..2ba6ef339 100644 --- a/Graphics/GraphicsEngineVulkan/src/TextureVkImpl.cpp +++ b/Graphics/GraphicsEngineVulkan/src/TextureVkImpl.cpp @@ -1,5 +1,5 @@ /* - * Copyright 2019-2023 Diligent Graphics LLC + * Copyright 2019-2024 Diligent Graphics LLC * Copyright 2015-2019 Egor Yusov * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -427,7 +427,7 @@ void TextureVkImpl::CreateStagingTexture(const TextureData* pInitData, const Tex VkStagingBuffCI.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO; VkStagingBuffCI.pNext = nullptr; VkStagingBuffCI.flags = 0; - VkStagingBuffCI.size = GetStagingTextureSubresourceOffset(m_Desc, m_Desc.GetArraySize(), 0, StagingBufferOffsetAlignment); + VkStagingBuffCI.size = GetStagingTextureDataSize(m_Desc, StagingBufferOffsetAlignment); // clang-format off DEV_CHECK_ERR((m_Desc.CPUAccessFlags & (CPU_ACCESS_READ | CPU_ACCESS_WRITE)) == CPU_ACCESS_READ || From bdf1f7cd76387ce36cb6c3fb66b3add6fbf6d061 Mon Sep 17 00:00:00 2001 From: assiduous Date: Mon, 14 Oct 2024 17:46:55 -0700 Subject: [PATCH 3/4] DynamicTextureAtlas: added growth factor parameter --- .../GraphicsTools/interface/DynamicTextureAtlas.h | 11 ++++++++--- Graphics/GraphicsTools/src/DynamicTextureAtlas.cpp | 6 ++++-- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/Graphics/GraphicsTools/interface/DynamicTextureAtlas.h b/Graphics/GraphicsTools/interface/DynamicTextureAtlas.h index f1e805c16..61303b30a 100644 --- a/Graphics/GraphicsTools/interface/DynamicTextureAtlas.h +++ b/Graphics/GraphicsTools/interface/DynamicTextureAtlas.h @@ -1,5 +1,5 @@ /* - * Copyright 2019-2023 Diligent Graphics LLC + * Copyright 2019-2024 Diligent Graphics LLC * Copyright 2015-2019 Egor Yusov * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -224,10 +224,15 @@ struct DynamicTextureAtlasCreateInfo /// The number of extra slices. /// When non-zero, the array will be expanded by the specified number of slices every time - /// there is insufficient space. If zero, the array size will be doubled when - /// more space is needed. + /// there is insufficient space. If zero, the array size will be expanded by the growth factor. Uint32 ExtraSliceCount = 0; + /// Growth factor. + + /// If ExtraSliceCount is zero, defines the factor by which the array size will be expanded. + /// The factor must be in the range (1, 2]. For example, if the factor is 2.0, the array size + /// will be doubled every time there is insufficient space. + float GrowthFactor = 2.0f; /// Maximum number of slices in texture array. Uint32 MaxSliceCount = 2048; diff --git a/Graphics/GraphicsTools/src/DynamicTextureAtlas.cpp b/Graphics/GraphicsTools/src/DynamicTextureAtlas.cpp index 075e776da..5c29d712c 100644 --- a/Graphics/GraphicsTools/src/DynamicTextureAtlas.cpp +++ b/Graphics/GraphicsTools/src/DynamicTextureAtlas.cpp @@ -1,5 +1,5 @@ /* - * Copyright 2019-2023 Diligent Graphics LLC + * Copyright 2019-2024 Diligent Graphics LLC * Copyright 2015-2019 Egor Yusov * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -384,6 +384,7 @@ class DynamicTextureAtlasImpl final : public ObjectBase // clang-format off m_MinAlignment {CreateInfo.MinAlignment}, m_ExtraSliceCount {CreateInfo.ExtraSliceCount}, + m_ExtraSliceFactor{clamp(CreateInfo.GrowthFactor, 1.f, 2.f) - 1.f}, m_MaxSliceCount {CreateInfo.Desc.Type == RESOURCE_DIM_TEX_2D_ARRAY ? std::min(CreateInfo.MaxSliceCount, Uint32{2048}) : 1}, m_Silent {CreateInfo.Silent}, m_SuballocationsAllocator @@ -688,7 +689,7 @@ class DynamicTextureAtlasImpl final : public ObjectBase { const auto ExtraSliceCount = m_ExtraSliceCount != 0 ? m_ExtraSliceCount : - std::max(m_TexArraySize.load(), 1u); + std::max(static_cast(static_cast(m_TexArraySize.load()) * m_ExtraSliceFactor), 1u); m_TexArraySize.store(std::min(m_TexArraySize + ExtraSliceCount, m_MaxSliceCount)); } @@ -721,6 +722,7 @@ class DynamicTextureAtlasImpl final : public ObjectBase const Uint32 m_MinAlignment; const Uint32 m_ExtraSliceCount; + const float m_ExtraSliceFactor; const Uint32 m_MaxSliceCount; const bool m_Silent; From 55500dbeff0241a518ae10b6c823a94ed6e87ae6 Mon Sep 17 00:00:00 2001 From: assiduous Date: Mon, 14 Oct 2024 21:53:12 -0700 Subject: [PATCH 4/4] ObjectsRegistry: added Clear() method --- Common/interface/ObjectsRegistry.hpp | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/Common/interface/ObjectsRegistry.hpp b/Common/interface/ObjectsRegistry.hpp index ce5f5411e..dcab0a0b9 100644 --- a/Common/interface/ObjectsRegistry.hpp +++ b/Common/interface/ObjectsRegistry.hpp @@ -264,6 +264,14 @@ class ObjectsRegistry } } + /// Removes all objects from the cache. + void Clear() + { + std::lock_guard Guard{m_CacheMtx}; + m_Cache.clear(); + m_NumRequestsSinceLastPurge.store(0); + } + private: class ObjectWrapper {