Skip to content

Commit

Permalink
multi-threaded loading
Browse files Browse the repository at this point in the history
  • Loading branch information
beaumanvienna committed Sep 11, 2024
1 parent 396561c commit 8a2ef83
Show file tree
Hide file tree
Showing 16 changed files with 161 additions and 104 deletions.
2 changes: 1 addition & 1 deletion application/lucre/sceneDescriptions/night.json
Original file line number Diff line number Diff line change
Expand Up @@ -1087,7 +1087,7 @@
"nodes":
[
{
"name": "0::SceneWithDuck::duck",
"name": "SceneWithDuck::duck",
"walkSpeed": 0,
"rigidBody": false,
"script-component": "application/lucre/scripts/duck/duck.cpp"
Expand Down
25 changes: 18 additions & 7 deletions application/lucre/scenes/beachScene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -187,18 +187,29 @@ namespace LucreApp
}
}

void BeachScene::LoadScripts() {}

void BeachScene::StartScripts()
void BeachScene::LoadScripts()
{
auto duck =
m_Dictionary.Retrieve("application/lucre/models/external_3D_files/duck/duck.gltf::0::SceneWithDuck::duck");
if (duck != entt::null)
if ((duck != entt::null) && m_Registry.all_of<ScriptComponent>(duck))
{
auto& duckScriptComponent = m_Registry.get<ScriptComponent>(duck);

duckScriptComponent.m_Script = std::make_shared<DuckScript>(duck, this);
LOG_APP_INFO("scripts loaded");
}
}

void BeachScene::StartScripts()
{
auto view = m_Registry.view<ScriptComponent>();
for (auto& entity : view)
{
if (m_Registry.all_of<ScriptComponent>(duck))
auto& scriptComponent = m_Registry.get<ScriptComponent>(entity);
if (scriptComponent.m_Script)
{
auto& duckScriptComponent = m_Registry.get<ScriptComponent>(duck);
duckScriptComponent.m_Script = std::make_shared<DuckScript>(duck, this);
LOG_APP_INFO("starting script {0}", scriptComponent.m_Filepath);
scriptComponent.m_Script->Start();
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion application/lucre/scenes/mainScene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ namespace LucreApp
{
auto duck =
m_Dictionary.Retrieve("application/lucre/models/external_3D_files/duck/duck.gltf::0::SceneWithDuck::duck");
if (duck != entt::null)
if ((duck != entt::null) && m_Registry.all_of<ScriptComponent>(duck))
{
auto& duckScriptComponent = m_Registry.get<ScriptComponent>(duck);

Expand Down
21 changes: 16 additions & 5 deletions application/lucre/scenes/nightScene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -219,8 +219,7 @@ namespace LucreApp
0, /* margin */
0.01f /* scale) */
);
m_VolcanoSmoke =
std::make_shared<ParticleSystem>(poolSize, &m_SpritesheetSmoke, 5.0f /*amplification*/);
m_VolcanoSmoke = std::make_shared<ParticleSystem>(poolSize, &m_SpritesheetSmoke, 5.0f /*amplification*/);
}
}

Expand Down Expand Up @@ -304,9 +303,7 @@ namespace LucreApp
}
}

void NightScene::LoadScripts() {}

void NightScene::StartScripts()
void NightScene::LoadScripts()
{
auto duck =
m_Dictionary.Retrieve("application/lucre/models/external_3D_files/duck/duck.gltf::0::SceneWithDuck::duck");
Expand All @@ -319,6 +316,20 @@ namespace LucreApp
}
}

void NightScene::StartScripts()
{
auto view = m_Registry.view<ScriptComponent>();
for (auto& entity : view)
{
auto& scriptComponent = m_Registry.get<ScriptComponent>(entity);
if (scriptComponent.m_Script)
{
LOG_APP_INFO("starting script {0}", scriptComponent.m_Filepath);
scriptComponent.m_Script->Start();
}
}
}

void NightScene::Stop()
{
m_IsRunning = false;
Expand Down
12 changes: 8 additions & 4 deletions engine/platform/Vulkan/VKdescriptor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ namespace GfxRenderEngine

VK_DescriptorPool::~VK_DescriptorPool()
{
vkDeviceWaitIdle(m_Device);
VK_Core::m_Device->WaitIdle();
vkDestroyDescriptorPool(m_Device, m_DescriptorPool, nullptr);
}

Expand Down Expand Up @@ -159,8 +159,12 @@ namespace GfxRenderEngine

// *************** Descriptor Writer *********************

VK_DescriptorWriter::VK_DescriptorWriter(VK_DescriptorSetLayout& setLayout, VK_DescriptorPool& pool)
: m_SetLayout{setLayout}, m_Pool{pool}
VK_DescriptorWriter::VK_DescriptorWriter(VK_DescriptorSetLayout& setLayout, VK_DescriptorPool& descriptorPool)
: m_SetLayout{setLayout}, m_DescriptorPool{descriptorPool}
{
}
VK_DescriptorWriter::VK_DescriptorWriter(VK_DescriptorSetLayout& setLayout)
: m_SetLayout{setLayout}, m_DescriptorPool{VK_Core::m_Device.get()->GetLoadPool().get()->GetDescriptorPool()}
{
}

Expand Down Expand Up @@ -232,7 +236,7 @@ namespace GfxRenderEngine

bool VK_DescriptorWriter::Build(VkDescriptorSet& set)
{
bool success = m_Pool.AllocateDescriptorSet(m_SetLayout.GetDescriptorSetLayout(), set);
bool success = m_DescriptorPool.AllocateDescriptorSet(m_SetLayout.GetDescriptorSetLayout(), set);
if (!success)
{
return false;
Expand Down
5 changes: 3 additions & 2 deletions engine/platform/Vulkan/VKdescriptor.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,8 @@ namespace GfxRenderEngine
{

public:
VK_DescriptorWriter(VK_DescriptorSetLayout& setLayout, VK_DescriptorPool& pool);
VK_DescriptorWriter(VK_DescriptorSetLayout& setLayout, VK_DescriptorPool& descriptorPool);
VK_DescriptorWriter(VK_DescriptorSetLayout& setLayout);

VK_DescriptorWriter& WriteBuffer(uint binding, const VkDescriptorBufferInfo& bufferInfo);
VK_DescriptorWriter& WriteImage(uint binding, const VkDescriptorImageInfo& imageInfo);
Expand All @@ -137,7 +138,7 @@ namespace GfxRenderEngine

private:
VK_DescriptorSetLayout& m_SetLayout;
VK_DescriptorPool& m_Pool;
VK_DescriptorPool& m_DescriptorPool;
std::vector<VkWriteDescriptorSet> m_Writes;
};
} // namespace GfxRenderEngine
8 changes: 6 additions & 2 deletions engine/platform/Vulkan/VKdevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,6 @@ namespace GfxRenderEngine
{
LOG_CORE_CRITICAL("failed to find GPUs with Vulkan support!");
}
// std::cout << "Device count: " << deviceCount << std::endl;
std::vector<VkPhysicalDevice> devices(deviceCount);
vkEnumeratePhysicalDevices(m_Instance, &deviceCount, devices.data());

Expand Down Expand Up @@ -712,7 +711,6 @@ namespace GfxRenderEngine
submitInfo.commandBufferCount = 1;
submitInfo.pCommandBuffers = &commandBuffer;
std::lock_guard<std::mutex> guard(m_QueueAccessMutex);

{
vkQueueSubmit(GraphicsQueue(), 1, &submitInfo, VK_NULL_HANDLE);
vkQueueWaitIdle(GraphicsQueue());
Expand Down Expand Up @@ -824,6 +822,12 @@ namespace GfxRenderEngine
}
}

void VK_Device::WaitIdle()
{
std::lock_guard<std::mutex> guard(m_QueueAccessMutex);
vkDeviceWaitIdle(m_Device);
}

void VK_Device::PrintAllSupportedFormats()
{
// clang-format off
Expand Down
4 changes: 3 additions & 1 deletion engine/platform/Vulkan/VKdevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ namespace GfxRenderEngine

VkInstance GetInstance() const { return m_Instance; }
bool MultiThreadingSupport() const { return true; }
std::mutex m_QueueAccessMutex;
std::shared_ptr<VK_Pool> GetLoadPool() { return m_LoadPool; }
void WaitIdle();

private:
static constexpr int NO_ASSIGNED = -1;
Expand Down Expand Up @@ -125,7 +128,6 @@ namespace GfxRenderEngine
VK_Window* m_Window;
VkCommandPool m_GraphicsCommandPool;
std::shared_ptr<VK_Pool> m_LoadPool;
std::mutex m_QueueAccessMutex;
VkDevice m_Device;
VkSurfaceKHR m_Surface;

Expand Down
6 changes: 2 additions & 4 deletions engine/platform/Vulkan/VKmaterialDescriptor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ namespace GfxRenderEngine
auto& imageInfo4 = static_cast<VK_Texture*>(roughnessMap.get())->GetDescriptorImageInfo();
auto& imageInfo5 = static_cast<VK_Texture*>(metallicMap.get())->GetDescriptorImageInfo();

VK_DescriptorWriter descriptorWriter(*localDescriptorSetLayout, *VK_Renderer::m_DescriptorPool);
VK_DescriptorWriter descriptorWriter(*localDescriptorSetLayout);
descriptorWriter.WriteImage(0, imageInfo0)
.WriteImage(1, imageInfo1)
.WriteImage(2, imageInfo2)
Expand Down Expand Up @@ -108,9 +108,7 @@ namespace GfxRenderEngine

VkDescriptorImageInfo cubemapInfo = static_cast<VK_Cubemap*>(cubemap.get())->GetDescriptorImageInfo();

VK_DescriptorWriter(*localDescriptorSetLayout, *VK_Renderer::m_DescriptorPool)
.WriteImage(0, cubemapInfo)
.Build(m_DescriptorSet);
VK_DescriptorWriter(*localDescriptorSetLayout).WriteImage(0, cubemapInfo).Build(m_DescriptorSet);
break;
}
default:
Expand Down
35 changes: 11 additions & 24 deletions engine/platform/Vulkan/VKrenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,10 @@ namespace GfxRenderEngine
std::shared_ptr<Texture> gTextureFontAtlas;
std::shared_ptr<Buffer> gDummyBuffer;

std::unique_ptr<VK_DescriptorPool> VK_Renderer::m_DescriptorPool;

VK_Renderer::VK_Renderer(VK_Window* window)
: m_Window{window}, m_FrameCounter{0}, m_CurrentImageIndex{0}, m_AmbientLightIntensity{0.0f}, m_CurrentFrameIndex{0},
m_ShowDebugShadowMap{false}, m_FrameInProgress{false}, m_ShadersCompiled{false}, m_Device{VK_Core::m_Device}
m_ShowDebugShadowMap{false}, m_FrameInProgress{false}, m_ShadersCompiled{false}, m_Device{VK_Core::m_Device},
m_LoadPool{VK_Core::m_Device.get()->GetLoadPool()}, m_DescriptorPool{m_LoadPool.get()->GetDescriptorPool()}
{
CompileShaders(); // runs in a parallel thread and sets m_ShadersCompiled
}
Expand Down Expand Up @@ -86,16 +85,6 @@ namespace GfxRenderEngine
m_UniformBuffers[i]->Map();
}

// create a global pool for desciptor sets
static constexpr uint POOL_SIZE = 500;
m_DescriptorPool =
VK_DescriptorPool::Builder(m_Device->Device())
.SetMaxSets(VK_SwapChain::MAX_FRAMES_IN_FLIGHT * POOL_SIZE)
.AddPoolSize(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, VK_SwapChain::MAX_FRAMES_IN_FLIGHT * 50)
.AddPoolSize(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, VK_SwapChain::MAX_FRAMES_IN_FLIGHT * 7500)
.AddPoolSize(VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT, VK_SwapChain::MAX_FRAMES_IN_FLIGHT * 2450)
.Build();

std::unique_ptr<VK_DescriptorSetLayout> shadowUniformBufferDescriptorSetLayout =
VK_DescriptorSetLayout::Builder()
.AddBinding(0, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, VK_SHADER_STAGE_VERTEX_BIT)
Expand Down Expand Up @@ -262,23 +251,23 @@ namespace GfxRenderEngine
for (uint i = 0; i < VK_SwapChain::MAX_FRAMES_IN_FLIGHT; i++)
{
VkDescriptorBufferInfo shadowUBObufferInfo = m_ShadowUniformBuffers0[i]->DescriptorInfo();
VK_DescriptorWriter(*shadowUniformBufferDescriptorSetLayout, *m_DescriptorPool)
VK_DescriptorWriter(*shadowUniformBufferDescriptorSetLayout, m_DescriptorPool)
.WriteBuffer(0, shadowUBObufferInfo)
.Build(m_ShadowDescriptorSets0[i]);
}

for (uint i = 0; i < VK_SwapChain::MAX_FRAMES_IN_FLIGHT; i++)
{
VkDescriptorBufferInfo shadowUBObufferInfo = m_ShadowUniformBuffers1[i]->DescriptorInfo();
VK_DescriptorWriter(*shadowUniformBufferDescriptorSetLayout, *m_DescriptorPool)
VK_DescriptorWriter(*shadowUniformBufferDescriptorSetLayout, m_DescriptorPool)
.WriteBuffer(0, shadowUBObufferInfo)
.Build(m_ShadowDescriptorSets1[i]);
}

for (uint i = 0; i < VK_SwapChain::MAX_FRAMES_IN_FLIGHT; i++)
{
VkDescriptorBufferInfo bufferInfo = m_UniformBuffers[i]->DescriptorInfo();
VK_DescriptorWriter(*globalDescriptorSetLayout, *m_DescriptorPool)
VK_DescriptorWriter(*globalDescriptorSetLayout, m_DescriptorPool)
.WriteBuffer(0, bufferInfo)
.WriteImage(1, imageInfo0)
.WriteImage(2, imageInfo1)
Expand Down Expand Up @@ -333,7 +322,7 @@ namespace GfxRenderEngine

void VK_Renderer::CreateRenderSystemBloom()
{
m_RenderSystemBloom = std::make_unique<VK_RenderSystemBloom>(*m_RenderPass, *m_DescriptorPool);
m_RenderSystemBloom = std::make_unique<VK_RenderSystemBloom>(*m_RenderPass, m_DescriptorPool);
}

void VK_Renderer::CreateShadowMapDescriptorSets()
Expand All @@ -346,7 +335,7 @@ namespace GfxRenderEngine
VkDescriptorBufferInfo shadowUBObufferInfo0 = m_ShadowUniformBuffers0[i]->DescriptorInfo();
VkDescriptorBufferInfo shadowUBObufferInfo1 = m_ShadowUniformBuffers1[i]->DescriptorInfo();

VK_DescriptorWriter(*m_ShadowMapDescriptorSetLayout, *m_DescriptorPool)
VK_DescriptorWriter(*m_ShadowMapDescriptorSetLayout, m_DescriptorPool)
.WriteImage(0, shadowMapInfo0)
.WriteImage(1, shadowMapInfo1)
.WriteBuffer(2, shadowUBObufferInfo0)
Expand Down Expand Up @@ -379,7 +368,7 @@ namespace GfxRenderEngine
imageInfoGBufferEmissionInputAttachment.imageView = m_RenderPass->GetImageViewGBufferEmission();
imageInfoGBufferEmissionInputAttachment.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;

VK_DescriptorWriter(*m_LightingDescriptorSetLayout, *m_DescriptorPool)
VK_DescriptorWriter(*m_LightingDescriptorSetLayout, m_DescriptorPool)
.WriteImage(0, imageInfoGBufferPositionInputAttachment)
.WriteImage(1, imageInfoGBufferNormalInputAttachment)
.WriteImage(2, imageInfoGBufferColorInputAttachment)
Expand All @@ -401,7 +390,7 @@ namespace GfxRenderEngine
imageInfoGBufferEmissionInputAttachment.imageView = m_RenderPass->GetImageViewGBufferEmission();
imageInfoGBufferEmissionInputAttachment.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;

VK_DescriptorWriter(*m_PostProcessingDescriptorSetLayout, *m_DescriptorPool)
VK_DescriptorWriter(*m_PostProcessingDescriptorSetLayout, m_DescriptorPool)
.WriteImage(0, imageInfoColorInputAttachment)
.WriteImage(1, imageInfoGBufferEmissionInputAttachment)
.Build(m_PostProcessingDescriptorSets[frameIndex]);
Expand All @@ -418,8 +407,7 @@ namespace GfxRenderEngine
extent = m_Window->GetExtent();
glfwWaitEvents();
}

vkDeviceWaitIdle(m_Device->Device());
m_Device->WaitIdle();

// create the swapchain
if (m_SwapChain == nullptr)
Expand Down Expand Up @@ -611,8 +599,7 @@ namespace GfxRenderEngine
vkCmdSetScissor(commandBuffer, 0, 1, &scissor);
}

void VK_Renderer::SubmitShadows(Registry& registry,
const std::vector<DirectionalLightComponent*>& directionalLights)
void VK_Renderer::SubmitShadows(Registry& registry, const std::vector<DirectionalLightComponent*>& directionalLights)
{
// this function supports one directional light
// with a high-resolution and
Expand Down
4 changes: 2 additions & 2 deletions engine/platform/Vulkan/VKrenderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,6 @@ namespace GfxRenderEngine
void ToggleDebugWindow(const GenericCallback& callback = nullptr) { m_Imgui = Imgui::ToggleDebugWindow(callback); }

public:
static std::unique_ptr<VK_DescriptorPool> m_DescriptorPool;

private:
void CreateCommandBuffers();
void FreeCommandBuffers();
Expand All @@ -130,6 +128,8 @@ namespace GfxRenderEngine
void Recreate();

private:
std::shared_ptr<VK_Pool> m_LoadPool;
VK_DescriptorPool& m_DescriptorPool;
bool m_ShadersCompiled;
VK_Window* m_Window;
std::shared_ptr<VK_Device> m_Device;
Expand Down
2 changes: 1 addition & 1 deletion engine/platform/Vulkan/VKresourceDescriptor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ namespace GfxRenderEngine
}
std::unique_ptr<VK_DescriptorSetLayout> localDescriptorSetLayout = builder.Build();

VK_DescriptorWriter descriptorWriter(*localDescriptorSetLayout, *VK_Renderer::m_DescriptorPool);
VK_DescriptorWriter descriptorWriter(*localDescriptorSetLayout);
if (instBuffer || skelBuffer || hBuffer || mPurposeBuffer)
{
descriptorWriter.WriteBuffer(0, instanceBufferInfo);
Expand Down
16 changes: 11 additions & 5 deletions engine/platform/Vulkan/VKswapChain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,13 @@ namespace GfxRenderEngine
submitInfo.signalSemaphoreCount = 1;
submitInfo.pSignalSemaphores = signalSemaphores;

vkResetFences(m_Device->Device(), 1, &m_InFlightFences[m_CurrentFrame]);
if (vkQueueSubmit(m_Device->GraphicsQueue(), 1, &submitInfo, m_InFlightFences[m_CurrentFrame]) != VK_SUCCESS)
{
LOG_CORE_CRITICAL("failed to submit draw command buffer!");
std::lock_guard<std::mutex> guard(VK_Core::m_Device->m_QueueAccessMutex);
vkResetFences(m_Device->Device(), 1, &m_InFlightFences[m_CurrentFrame]);
if (vkQueueSubmit(m_Device->GraphicsQueue(), 1, &submitInfo, m_InFlightFences[m_CurrentFrame]) != VK_SUCCESS)
{
LOG_CORE_CRITICAL("failed to submit draw command buffer!");
}
}

VkPresentInfoKHR presentInfo = {};
Expand All @@ -129,7 +132,11 @@ namespace GfxRenderEngine

presentInfo.pImageIndices = imageIndex;

auto result = vkQueuePresentKHR(m_Device->PresentQueue(), &presentInfo);
VkResult result{};
{
std::lock_guard<std::mutex> guard(VK_Core::m_Device->m_QueueAccessMutex);
result = vkQueuePresentKHR(m_Device->PresentQueue(), &presentInfo);
}

m_CurrentFrame = (m_CurrentFrame + 1) % MAX_FRAMES_IN_FLIGHT;

Expand Down Expand Up @@ -271,7 +278,6 @@ namespace GfxRenderEngine

VkPresentModeKHR VK_SwapChain::ChooseSwapPresentMode(const std::vector<VkPresentModeKHR>& availablePresentModes)
{
// std::cout << "Present mode: V-Sync" << std::endl;
return VK_PRESENT_MODE_FIFO_KHR;
}

Expand Down
Loading

0 comments on commit 8a2ef83

Please sign in to comment.