Skip to content

Commit

Permalink
[vulkan] fixed all the remaining validation errors related to multi-t…
Browse files Browse the repository at this point in the history
…hreaded access of the same gpu queue
  • Loading branch information
PanosK92 committed Dec 11, 2024
1 parent cb75598 commit e6413c6
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 14 deletions.
4 changes: 2 additions & 2 deletions editor/EditorWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,7 @@ namespace

void world_on_download_finished()
{
Spartan::ProgressTracker::SetLoadingStateGlobal(false);
Spartan::ProgressTracker::SetGlobalLoadingState(false);
visible = true;
}

Expand All @@ -468,7 +468,7 @@ namespace
if (ImGui::Button("Yes"))
{
Spartan::FileSystem::Command("python download_assets.py", world_on_download_finished, false);
Spartan::ProgressTracker::SetLoadingStateGlobal(true);
Spartan::ProgressTracker::SetGlobalLoadingState(true);
visible_download = false;
}

Expand Down
2 changes: 1 addition & 1 deletion runtime/Core/ProgressTracker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ namespace Spartan
return false;
}

void ProgressTracker::SetLoadingStateGlobal(const bool is_loading)
void ProgressTracker::SetGlobalLoadingState(const bool is_loading)
{
anonymous_jobs = is_loading ? (anonymous_jobs + 1) : (anonymous_jobs - 1);
}
Expand Down
2 changes: 1 addition & 1 deletion runtime/Core/ProgressTracker.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,6 @@ namespace Spartan
public:
static Progress& GetProgress(const ProgressType progress_type);
static bool IsLoading();
static void SetLoadingStateGlobal(const bool is_loading);
static void SetGlobalLoadingState(const bool is_loading);
};
}
4 changes: 2 additions & 2 deletions runtime/Game/Game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1214,7 +1214,7 @@ namespace Spartan
{
ThreadPool::AddTask([default_world]()
{
ProgressTracker::SetLoadingStateGlobal(true);
ProgressTracker::SetGlobalLoadingState(true);

switch (default_world)
{
Expand All @@ -1229,7 +1229,7 @@ namespace Spartan
default: SP_ASSERT_MSG(false, "Unhandled default world"); break;
}

ProgressTracker::SetLoadingStateGlobal(false);
ProgressTracker::SetGlobalLoadingState(false);
});
}
}
7 changes: 6 additions & 1 deletion runtime/RHI/RHI_Texture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#include "RHI_CommandList.h"
#include "../IO/FileStream.h"
#include "../Resource/Import/ImageImporter.h"
#include "../Core/ProgressTracker.h"
SP_WARNINGS_OFF
#include "compressonator.h"
SP_WARNINGS_ON
Expand Down Expand Up @@ -230,7 +231,7 @@ namespace Spartan

RHI_Texture::RHI_Texture(const string& file_path) : IResource(ResourceType::Texture)
{
LoadFromFile(file_path);
LoadFromFile(file_path);
}

RHI_Texture::~RHI_Texture()
Expand Down Expand Up @@ -311,6 +312,8 @@ namespace Spartan
return;
}

ProgressTracker::SetGlobalLoadingState(true);

m_type = RHI_Texture_Type::Type2D;
m_depth = 1;
m_flags |= RHI_Texture_Srv;
Expand Down Expand Up @@ -390,6 +393,8 @@ namespace Spartan
{
PrepareForGpu();
}

ProgressTracker::SetGlobalLoadingState(false);
}

RHI_Texture_Mip& RHI_Texture::GetMip(const uint32_t array_index, const uint32_t mip_index)
Expand Down
6 changes: 0 additions & 6 deletions runtime/RHI/Vulkan/Vulkan_Device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#include "pch.h"
#include "../../Profiling/Profiler.h"
#include "../Core/Debugging.h"
#include "../Core/ProgressTracker.h"
#include "../Rendering/Renderer.h"
#include "../RHI_Device.h"
#include "../RHI_Implementation.h"
Expand Down Expand Up @@ -2139,9 +2138,6 @@ namespace Spartan
queues::condition_variable_immediate_execution.wait(lock, [] { return !queues::is_immediate_executing; });
queues::is_immediate_executing = true;

// immediate command lists are used to stage data when loading textures
ProgressTracker::SetLoadingStateGlobal(true);

// get command pool
queues::queue = queues::immediate[static_cast<uint32_t>(queue_type)].get();
queues::queue->NextCommandList();
Expand All @@ -2155,8 +2151,6 @@ namespace Spartan
cmd_list->Submit(queues::queue, 0);
cmd_list->WaitForExecution();

ProgressTracker::SetLoadingStateGlobal(false);

// signal that it's safe to proceed with the next ImmediateBegin()
queues::is_immediate_executing = false;
queues::condition_variable_immediate_execution.notify_one();
Expand Down
16 changes: 15 additions & 1 deletion runtime/RHI/Vulkan/Vulkan_Queue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,12 +144,19 @@ namespace Spartan

void RHI_Queue::Wait()
{
// when loading textures (other threads) the queue will be used to submit data for staging
unique_lock<mutex> lock;
if (ProgressTracker::IsLoading())
{
lock = unique_lock<mutex>(get_mutex(this));
}

SP_ASSERT_VK(vkQueueWaitIdle(static_cast<VkQueue>(RHI_Device::GetQueueRhiResource(m_type))));
}

void RHI_Queue::Submit(void* cmd_buffer, const uint32_t wait_flags, RHI_SyncPrimitive* semaphore, RHI_SyncPrimitive* semaphore_timeline)
{
// locking only during texture loading to stage data without race conditions
// when loading textures (other threads) the queue will be used to submit data for staging
unique_lock<mutex> lock;
if (ProgressTracker::IsLoading())
{
Expand Down Expand Up @@ -211,6 +218,13 @@ namespace Spartan

void RHI_Queue::Present(void* swapchain, const uint32_t image_index, vector<RHI_SyncPrimitive*>& wait_semaphores)
{
// when loading textures (other threads) the queue will be used to submit data for staging
unique_lock<mutex> lock;
if (ProgressTracker::IsLoading())
{
lock = unique_lock<mutex>(get_mutex(this));
}

// get semaphore vulkan resources
array<VkSemaphore, 3> vk_wait_semaphores = { nullptr };
uint32_t semaphore_count = static_cast<uint32_t>(wait_semaphores.size());
Expand Down

0 comments on commit e6413c6

Please sign in to comment.