mirror of
https://github.com/MobileGL-Dev/MobileGL
synced 2026-09-09 04:38:30 +09:00
- Track every graphics-queue submission with a real fence: pooled fences for mid-frame flushes, the frame slot's fence for Present and readback. Completion advances a submit counter via vkGetFenceStatus polls, slot-fence waits, and device-idle points, and raises the buffer-manager serial floor from the frame serial each submission carried. - GL sync objects now capture the submission index that will carry the commands recorded so far; ClientWaitSync honors GL_SYNC_FLUSH_COMMANDS_BIT with a mid-frame submit (gated on the index still being unsubmitted so poll loops cannot split the render pass), and blocking waits flush then vkWaitForFences with the caller timeout. - FlushPendingCommands retires the submitted command buffer and restarts recording on a fresh one; retired buffers are freed once the slot fence is next waited, so an executing buffer is never reset. - Rewind descriptor-set cursors exactly once per frame in Present (after the slot-fence wait), plus after the synchronous readback drain, replacing the ten lazy per-draw-path rewinds. Verified: host tests 168/168, trace-replay 70/70.
110 lines
5.2 KiB
C++
110 lines
5.2 KiB
C++
// MobileGL - MobileGL/MG_Backend/DirectVulkan/Renderer/FrameContext.h
|
|
// Copyright (c) 2025-2026 MobileGL-Dev
|
|
// Licensed under the GNU Lesser General Public License v3.0:
|
|
// https://www.gnu.org/licenses/gpl-3.0.txt
|
|
// https://www.gnu.org/licenses/lgpl-3.0.txt
|
|
// SPDX-License-Identifier: LGPL-3.0-only
|
|
// End of Source File Header
|
|
|
|
#pragma once
|
|
|
|
#include "../VkIncludes.h"
|
|
#include <Includes.h>
|
|
|
|
namespace MobileGL::MG_Backend::DirectVulkan {
|
|
class FrameContext {
|
|
public:
|
|
// Notified immediately after a frame command buffer begins recording
|
|
// (before any render pass has been begun); every BeginCommandRecording
|
|
// caller funnels through this single seam. Implemented by the renderer
|
|
// to prepare per-frame timer-query pools (vkCmdResetQueryPool must be
|
|
// recorded outside a render pass).
|
|
class IRecordingObserver {
|
|
public:
|
|
virtual ~IRecordingObserver() = default;
|
|
virtual void OnFrameCommandRecordingBegan(VkCommandBuffer commandBuffer) = 0;
|
|
};
|
|
|
|
struct SubmitInfoPacket {
|
|
VkPipelineStageFlags waitDstStageMask = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
|
|
VkSemaphore waitSemaphore = VK_NULL_HANDLE;
|
|
VkSemaphore signalSemaphore = VK_NULL_HANDLE;
|
|
VkCommandBuffer commandBuffer = VK_NULL_HANDLE;
|
|
VkSubmitInfo submitInfo{VK_STRUCTURE_TYPE_SUBMIT_INFO};
|
|
};
|
|
|
|
struct PresentInfoPacket {
|
|
VkSemaphore waitSemaphore = VK_NULL_HANDLE;
|
|
VkSwapchainKHR swapchain = VK_NULL_HANDLE;
|
|
Uint32 imageIndex = 0;
|
|
VkPresentInfoKHR presentInfo{VK_STRUCTURE_TYPE_PRESENT_INFO_KHR};
|
|
};
|
|
|
|
struct FrameData {
|
|
VkCommandBuffer commandBuffer = VK_NULL_HANDLE;
|
|
VkSemaphore imageAvailableSemaphore = VK_NULL_HANDLE;
|
|
VkFence imageInFlightFence = VK_NULL_HANDLE;
|
|
Bool isCommandRecording = false;
|
|
Bool hasCommandBufferRecorded = false;
|
|
Bool imageAvailableSemaphoreConsumed = false;
|
|
// Command buffers submitted mid-frame (FlushPendingCommands) whose
|
|
// execution is only known complete once this slot's fence has been
|
|
// waited again; freed at that point.
|
|
Vector<VkCommandBuffer> retiredCommandBuffers;
|
|
// Submit-tracker index of this slot's most recent queue submission
|
|
// (written by the renderer at submit time).
|
|
Uint64 lastSubmitIndex = 0;
|
|
};
|
|
|
|
VkResult Initialize(VkDevice device, VkCommandPool commandPool, Uint32 frameCount);
|
|
void Destroy(VkDevice device, VkCommandPool commandPool);
|
|
|
|
// Lifecycle functions
|
|
FrameData& GetCurrent();
|
|
const FrameData& GetCurrent() const;
|
|
Bool IsCommandRecording() const;
|
|
void AdvanceToNext();
|
|
VkCommandBuffer& BeginCommandRecording(VkCommandBufferUsageFlags flags = 0,
|
|
const VkCommandBufferInheritanceInfo* pInheritanceInfo = nullptr);
|
|
void EndCommandRecording();
|
|
VkResult InitializeSwapchainSemaphores(VkDevice device, Uint32 swapchainImageCount);
|
|
void DestroySwapchainSemaphores(VkDevice device);
|
|
Bool TransitionToPresent(VkImage image, VkImageLayout oldLayout,
|
|
VkImageLayout presentLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR);
|
|
SubmitInfoPacket GetSubmitInfo(Bool shouldSubmitCommandBuffer, Uint32 swapchainImageIndex) const;
|
|
PresentInfoPacket GetPresentInfo(VkSwapchainKHR swapchain, Uint32 imageIndex) const;
|
|
VkResult WaitAndAcquireNextImage(VkDevice device, VkSwapchainKHR swapchain, Uint32& outImageIndex,
|
|
Uint64 timeout = UINT64_MAX, VkFence acquireFence = VK_NULL_HANDLE);
|
|
|
|
// Parks the current (already ended and submitted) command buffer on the
|
|
// slot's retired list and installs a freshly allocated one, so recording
|
|
// can restart while the submitted buffer is still executing. Retired
|
|
// buffers are freed after the slot's fence is next waited.
|
|
VkResult RetireCurrentCommandBuffer();
|
|
|
|
Uint32 GetCurrentFrameIndex() const;
|
|
Uint32 GetFrameCount() const;
|
|
|
|
// Observer may be null (no notifications). Not owned.
|
|
void SetRecordingObserver(IRecordingObserver* observer);
|
|
|
|
private:
|
|
void AssertValidFrameIndex(Uint32 frameIndex) const;
|
|
void AssertValidSwapchainImageIndex(Uint32 imageIndex) const;
|
|
|
|
VkResult CreateSyncObjectsForFrame(VkDevice device, Uint32 frameIndex,
|
|
const VkSemaphoreCreateInfo& semaphoreInfo,
|
|
const VkFenceCreateInfo& fenceInfo);
|
|
void DestroySyncObjectsForFrame(VkDevice device, Uint32 frameIndex);
|
|
void FreeRetiredCommandBuffers(FrameData& frame);
|
|
|
|
Vector<FrameData> m_frames;
|
|
Vector<VkSemaphore> m_swapchainImageRenderFinishedSemaphores;
|
|
Uint32 currentFrameIndex = 0;
|
|
IRecordingObserver* m_recordingObserver = nullptr;
|
|
// Stored at Initialize for retired-command-buffer management.
|
|
VkDevice m_device = VK_NULL_HANDLE;
|
|
VkCommandPool m_commandPool = VK_NULL_HANDLE;
|
|
};
|
|
} // namespace MobileGL::MG_Backend::DirectVulkan
|