yum-mirror/slang

Making it easier to work with shaders

git clone https://git.yummers.dev/yum-mirror/slang

Ellie Hermaszewskaformatf65d756bf

master
2.9 KiB106 linesraw
1// vk-device-queue.h
2#pragma once
3
4#include "vk-api.h"
5#include "vk-descriptor-allocator.h"
6
7namespace gfx
8{
9
10struct VulkanDeviceQueue
11{
12    enum
13    {
14        kMaxCommandBuffers = 8,
15    };
16
17    enum class EventType
18    {
19        BeginFrame,
20        EndFrame,
21        CountOf,
22    };
23
24    /// Initialize - must be called before anything else can be done
25    SlangResult init(const VulkanApi& api, VkQueue queue, int queueIndex);
26
27    /// Flushes the current command list, and steps to next (internally this is equivalent to a
28    /// stepA followed by stepB)
29    void flush();
30    /// Performs a full flush, and then waits for idle.
31    void flushAndWait();
32
33    /// Blocks until all work submitted to GPU has completed
34    void waitForIdle() { m_api->vkQueueWaitIdle(m_queue); }
35
36    /// Get the graphics queue index (as set on init)
37    int getQueueIndex() const { return m_queueIndex; }
38
39    /// Make the specified event 'current' - meaning it's semaphore must be waited on
40    VkSemaphore makeCurrent(EventType eventType);
41    VkSemaphore getSemaphore(EventType eventType);
42    /// Makes the event no longer required to be waited on
43    void makeCompleted(EventType eventType);
44    /// Returns true if the event is already current
45    SLANG_FORCE_INLINE bool isCurrent(EventType eventType) const
46    {
47        return m_currentSemaphores[int(eventType)] != VK_NULL_HANDLE;
48    }
49
50    /// Get the command buffer
51    VkCommandBuffer getCommandBuffer() const { return m_commandBuffer; }
52
53    /// Get the queue
54    VkQueue getQueue() const { return m_queue; }
55
56    /// Get the API
57    const VulkanApi* getApi() const { return m_api; }
58
59    /// Flushes the current command list
60    void flushStepA();
61    /// Steps to next command buffer and opens. May block if command buffer is still in use
62    void flushStepB();
63
64    /// Destroy the device queue
65    void destroy();
66
67    /// True if the queue appears to be valid and has been initialized
68    bool isValid() const { return m_api != nullptr; }
69
70    /// Dtor
71    ~VulkanDeviceQueue();
72
73protected:
74    struct Fence
75    {
76        VkFence fence;
77        bool active;
78        uint64_t value;
79    };
80
81    void _updateFenceAtIndex(int fenceIndex, bool blocking);
82
83    VkQueue m_queue = VK_NULL_HANDLE;
84
85    int m_numCommandBuffers = 0;
86    int m_commandBufferIndex = 0;
87    // There are the same amount of command buffers as fences
88    VkCommandPool m_commandPools[kMaxCommandBuffers] = {VK_NULL_HANDLE};
89    VkCommandBuffer m_commandBuffers[kMaxCommandBuffers] = {VK_NULL_HANDLE};
90
91    Fence m_fences[kMaxCommandBuffers] = {{VK_NULL_HANDLE, 0, 0u}};
92
93    VkCommandBuffer m_commandBuffer = VK_NULL_HANDLE;
94    VkCommandPool m_commandPool = VK_NULL_HANDLE;
95    VkSemaphore m_semaphores[int(EventType::CountOf)];
96    VkSemaphore m_currentSemaphores[int(EventType::CountOf)];
97
98    uint64_t m_lastFenceCompleted = 1;
99    uint64_t m_nextFenceValue = 2;
100
101    int m_queueIndex = 0;
102
103    const VulkanApi* m_api = nullptr;
104};
105
106} // namespace gfx