Skip to content

Commit

Permalink
Made CPU wait for N-1 GPU to finish
Browse files Browse the repository at this point in the history
  • Loading branch information
Supercomet committed Sep 26, 2023
1 parent 19ec1b6 commit d817657
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
2 changes: 2 additions & 0 deletions Application/src/TestApplication.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -770,6 +770,8 @@ void TestApplication::Run()
while (mainWindow.windowShouldClose == false)
{
PROFILE_FRAME("MainThread");
int64_t frame = std::max<int64_t>(int64_t(m_ApplicationFrame) - 1, 0);
gs_RenderEngine->CPUwaitforGPU(frame);

auto now = std::chrono::high_resolution_clock::now();
float deltaTime = std::chrono::duration<float>(now - lastTime).count();
Expand Down
16 changes: 12 additions & 4 deletions OO_Vulkan/src/VulkanRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1327,6 +1327,12 @@ int32_t VulkanRenderer::GetPixelValue(uint32_t fbID, glm::vec2 uv)

}

void VulkanRenderer::CPUwaitforGPU(size_t frameNum)
{
std::unique_lock l(cpuMutex);
cpuCV.wait(l, [f = frameNum, &gpu = CurrentFrameGPU] { return gpu > f; });
}

void VulkanRenderer::CreateLightingBuffers()
{
//oGFX::CreateBuffer(m_device.physicalDevice, m_device.logicalDevice, sizeof(CB::LightUBO),
Expand Down Expand Up @@ -2226,10 +2232,12 @@ void VulkanRenderer::BeginDraw()
{
PROFILE_SCOPED();

//vkWaitForFences(m_device.logicalDevice, 1, &drawFences[getFrame()], VK_TRUE, UINT64_MAX);
uint64_t res{};
VK_CHK(vkGetSemaphoreCounterValue(m_device.logicalDevice, frameCountSemaphore, &res));
//printf("[FRAME COUNTER %5llu]\n", res);
{
std::scoped_lock l{cpuMutex};
VK_CHK(vkGetSemaphoreCounterValue(m_device.logicalDevice, frameCountSemaphore, &CurrentFrameGPU));
//printf("[FRAME COUNTER %5llu]\n", res);
}
cpuCV.notify_one(); // wake up the CPU waiting for N-1 frame

//wait for given fence to signal from last draw before continuing
VK_CHK(vkWaitForFences(m_device.logicalDevice, 1, &drawFences[getFrame()], VK_TRUE, std::numeric_limits<uint64_t>::max()));
Expand Down
5 changes: 5 additions & 0 deletions OO_Vulkan/src/VulkanRenderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,8 @@ class VulkanRenderer

bool deferredRendering = true;

void CPUwaitforGPU(size_t frameNum);

void CreateLightingBuffers();
void UploadLights();
void UploadBones();
Expand Down Expand Up @@ -441,6 +443,9 @@ class VulkanRenderer
std::vector<VkSemaphore> renderSemaphore;
std::vector<VkFence> drawFences;
VkSemaphore frameCountSemaphore;
size_t CurrentFrameGPU{};
std::condition_variable cpuCV;
std::mutex cpuMutex;

// - Pipeline
VkPipeline pso_utilFullscreenBlit;
Expand Down

0 comments on commit d817657

Please sign in to comment.