| 1 |
#pragma once |
| 2 |
|
| 3 |
#include "../RhiTypes.h" |
| 4 |
#include "../../../Primitives/Rect.h" |
| 5 |
#include "../../../Primitives/Colorf.h" |
| 6 |
|
| 7 |
#include <cstdint> |
| 8 |
|
| 9 |
// The Vulkan headers (vulkan/vulkan.h) and the loader are pulled in only by VulkanDevice.cpp — every Vulkan |
| 10 |
// handle (VkInstance, VkDevice, VkSwapchainKHR, ...) is kept as a file-static there, so this contract header |
| 11 |
// (transitively included across the whole pipeline through Rhi.h) stays free of the Vulkan headers and does |
| 12 |
// not force the Vulkan-Headers include directory onto unrelated translation units. |
| 13 |
|
| 14 |
namespace nCine::RHI::Vulkan |
| 15 |
{ |
| 16 |
class VulkanShaderProgram; |
| 17 |
class VulkanRenderTarget; |
| 18 |
class VulkanTexture; |
| 19 |
|
| 20 |
/** |
| 21 |
@brief Pipeline-state and draw-call facade of the Vulkan backend (aliased as `RHI::Device`) |
| 22 |
|
| 23 |
Exposes the OpenGL device's surface (blending, depth, cull, scissor, viewport, clear and the draw |
| 24 |
calls) so the backend-neutral render pipeline drives it unchanged. |
| 25 |
|
| 26 |
The device also owns the real `VkInstance`, `VkDevice`, graphics/present `VkQueue` and the |
| 27 |
`VkSwapchainKHR` created from the SDL window (via @ref CreateSwapchain(), called by the SDL window |
| 28 |
backend). @ref PresentFrame() acquires a swap-chain image, blits the rendered scene into it and |
| 29 |
presents it (the buffer-swap equivalent). |
| 30 |
*/ |
| 31 |
class VulkanDevice |
| 32 |
{ |
| 33 |
public: |
| 34 |
VulkanDevice() = delete; |
| 35 |
~VulkanDevice() = delete; |
| 36 |
|
| 37 |
struct ScissorState |
| 38 |
{ |
| 39 |
bool Enabled = false; |
| 40 |
Recti Rect = Recti(0, 0, 0, 0); |
| 41 |
}; |
| 42 |
|
| 43 |
struct BlendingState |
| 44 |
{ |
| 45 |
bool Enabled = false; |
| 46 |
nCine::BlendingFactor SrcRgb = nCine::BlendingFactor::One; |
| 47 |
nCine::BlendingFactor DstRgb = nCine::BlendingFactor::Zero; |
| 48 |
nCine::BlendingFactor SrcAlpha = nCine::BlendingFactor::One; |
| 49 |
nCine::BlendingFactor DstAlpha = nCine::BlendingFactor::Zero; |
| 50 |
}; |
| 51 |
|
| 52 |
struct DepthTestState |
| 53 |
{ |
| 54 |
bool TestEnabled = false; |
| 55 |
bool MaskEnabled = true; |
| 56 |
}; |
| 57 |
|
| 58 |
struct CullFaceState |
| 59 |
{ |
| 60 |
bool Enabled = false; |
| 61 |
CullFaceMode Mode = CullFaceMode::Back; |
| 62 |
}; |
| 63 |
|
| 64 |
static void SetBlendingEnabled(bool enabled); |
| 65 |
static void SetBlendingFactors(nCine::BlendingFactor srcRgb, nCine::BlendingFactor dstRgb, nCine::BlendingFactor srcAlpha, nCine::BlendingFactor dstAlpha); |
| 66 |
static BlendingState GetBlendingState(); |
| 67 |
static void SetBlendingState(const BlendingState& state); |
| 68 |
|
| 69 |
static void SetDepthTestEnabled(bool enabled); |
| 70 |
static void SetDepthMaskEnabled(bool enabled); |
| 71 |
static DepthTestState GetDepthTestState(); |
| 72 |
static void SetDepthTestState(const DepthTestState& state); |
| 73 |
|
| 74 |
static void SetCullFaceEnabled(bool enabled); |
| 75 |
static CullFaceState GetCullFaceState(); |
| 76 |
static void SetCullFaceState(const CullFaceState& state); |
| 77 |
|
| 78 |
static ScissorState GetScissorState(); |
| 79 |
static void SetScissorState(const ScissorState& state); |
| 80 |
static void SetScissor(const Recti& rect); |
| 81 |
static void SetScissorTestEnabled(bool enabled); |
| 82 |
|
| 83 |
static Recti GetViewport(); |
| 84 |
static void SetViewport(const Recti& rect); |
| 85 |
static void InitViewport(std::int32_t x, std::int32_t y, std::int32_t width, std::int32_t height); |
| 86 |
|
| 87 |
static Colorf GetClearColor(); |
| 88 |
static void SetClearColor(const Colorf& color); |
| 89 |
static void Clear(ClearFlags flags); |
| 90 |
|
| 91 |
static void DrawArrays(PrimitiveType primitive, std::int32_t firstVertex, std::int32_t numVertices); |
| 92 |
static void DrawArraysInstanced(PrimitiveType primitive, std::int32_t firstVertex, std::int32_t numVertices, std::int32_t numInstances); |
| 93 |
static void DrawElements(PrimitiveType primitive, std::uint32_t numIndices, IndexFormat indexFormat, std::uintptr_t indexOffset, std::int32_t baseVertex); |
| 94 |
static inline void DrawElements(PrimitiveType primitive, std::uint32_t numIndices, std::uintptr_t indexOffset, std::int32_t baseVertex) { |
| 95 |
DrawElements(primitive, numIndices, IndexFormat::UInt16, indexOffset, baseVertex); |
| 96 |
} |
| 97 |
static void DrawElementsInstanced(PrimitiveType primitive, std::uint32_t numIndices, IndexFormat indexFormat, std::uintptr_t indexOffset, std::int32_t numInstances, std::int32_t baseVertex); |
| 98 |
static inline void DrawElementsInstanced(PrimitiveType primitive, std::uint32_t numIndices, std::uintptr_t indexOffset, std::int32_t numInstances, std::int32_t baseVertex) { |
| 99 |
DrawElementsInstanced(primitive, numIndices, IndexFormat::UInt16, indexOffset, numInstances, baseVertex); |
| 100 |
} |
| 101 |
|
| 102 |
static FenceHandle InsertFence(); |
| 103 |
static void DeleteFence(FenceHandle& fence); |
| 104 |
static bool ClientWaitFence(FenceHandle fence, std::uint64_t timeoutNs); |
| 105 |
|
| 106 |
static void SetupInitialState(); |
| 107 |
|
| 108 |
// -- Backend extensions (called by the resource types) -- |
| 109 |
|
| 110 |
/** @brief Records the currently bound shader program */ |
| 111 |
static void BindProgram(VulkanShaderProgram* program); |
| 112 |
/** @brief Returns the currently bound shader program */ |
| 113 |
static VulkanShaderProgram* CurrentProgram(); |
| 114 |
/** @brief Records the texture bound to a texture unit */ |
| 115 |
static void BindTexture(std::uint32_t unit, const VulkanTexture* texture); |
| 116 |
/** @brief Clears a texture from every unit it is bound to (called from ~VulkanTexture to avoid a dangling pointer) */ |
| 117 |
static void UnbindTexture(const VulkanTexture* texture); |
| 118 |
/** @brief Returns the texture bound to a texture unit */ |
| 119 |
static const VulkanTexture* GetBoundTexture(std::uint32_t unit); |
| 120 |
/** @brief Records the host data range bound to a uniform binding point */ |
| 121 |
static void BindUniformRange(std::uint32_t index, const std::uint8_t* data, std::uint32_t size); |
| 122 |
/** @brief Clears a render target from the device if it is the current one (called from ~VulkanRenderTarget) */ |
| 123 |
static void UnbindRenderTarget(const VulkanRenderTarget* renderTarget); |
| 124 |
/** @brief Records the current draw render target (its color attachment 0 receives the pixels) */ |
| 125 |
static void SetRenderTarget(VulkanRenderTarget* renderTarget); |
| 126 |
/** @brief Drops any cached graphics pipelines keyed on a shader program being destroyed/reset */ |
| 127 |
static void OnShaderProgramDestroyed(VulkanShaderProgram* program); |
| 128 |
|
| 129 |
// Internal accessors used by the backend's draw path (no Vulkan types, so callable from the .cpp's |
| 130 |
// anonymous-namespace draw helpers without exposing the private static state) |
| 131 |
/** @brief Returns the currently bound draw render target (nullptr = the screen) */ |
| 132 |
static VulkanRenderTarget* currentRenderTargetInternal(); |
| 133 |
/** @brief Number of uniform binding slots tracked by @ref BindUniformRange() */ |
| 134 |
static std::uint32_t MaxUniformBindingsPublic(); |
| 135 |
/** @brief Returns the host data range bound to a uniform binding slot */ |
| 136 |
static void GetUniformRange(std::uint32_t index, const std::uint8_t*& data, std::uint32_t& size); |
| 137 |
|
| 138 |
// -- Vulkan device / swap-chain lifecycle (called by the window backend) -- |
| 139 |
|
| 140 |
/** |
| 141 |
@brief Creates the Vulkan instance, device and swap chain for the given SDL window |
| 142 |
|
| 143 |
@param windowHandle The `SDL_Window*` (created with `SDL_WINDOW_VULKAN`), passed as a `void*` so the |
| 144 |
window backend does not need the Vulkan headers. The backend queries the required |
| 145 |
instance extensions and the presentation surface from it through the SDL Vulkan API. |
| 146 |
@param width Swap-chain image width in pixels |
| 147 |
@param height Swap-chain image height in pixels |
| 148 |
@param vsync Whether @ref PresentFrame() presents with vertical sync (FIFO vs. MAILBOX/IMMEDIATE) |
| 149 |
@returns `true` if the instance, device and swap chain were created |
| 150 |
*/ |
| 151 |
static bool CreateSwapchain(void* windowHandle, std::int32_t width, std::int32_t height, bool vsync); |
| 152 |
/** @brief Releases the swap chain, device and instance */ |
| 153 |
static void DestroySwapchain(); |
| 154 |
/** @brief Recreates the swap chain at the new drawable size (waits for the device to go idle first) */ |
| 155 |
static void ResizeSwapchain(std::int32_t width, std::int32_t height); |
| 156 |
/** @brief Acquires a swap-chain image, blits the rendered scene into it and presents it (the buffer-swap equivalent) */ |
| 157 |
static void PresentFrame(); |
| 158 |
|
| 159 |
/** |
| 160 |
@brief Creates an additional surface and swap chain for a secondary window |
| 161 |
|
| 162 |
Used by the ImGui multi-viewport support for the windows it spawns when a panel is dragged out of the |
| 163 |
main one. Only their presentation lives in the backend: the contents are rendered through the ordinary |
| 164 |
RHI path into an off-screen render target, whose texture is handed over for the frame with |
| 165 |
@ref QueueSecondaryPresent(). @ref PresentFrame() then blits each queued texture into its window's |
| 166 |
acquired image and joins it into the frame's single submit and present. |
| 167 |
|
| 168 |
@param windowHandle `SDL_Window*` of the secondary window, passed as a `void*` |
| 169 |
@param width Swap-chain image width in pixels |
| 170 |
@param height Swap-chain image height in pixels |
| 171 |
@returns An opaque handle to pass to the other `*Secondary*` functions, or `nullptr` if the surface failed |
| 172 |
*/ |
| 173 |
static void* CreateSecondarySwapchain(void* windowHandle, std::int32_t width, std::int32_t height); |
| 174 |
/** @brief Releases a secondary surface and swap chain created by @ref CreateSecondarySwapchain() */ |
| 175 |
static void DestroySecondarySwapchain(void* handle); |
| 176 |
/** @brief Recreates a secondary swap chain at a new size (waits for the device to go idle first) */ |
| 177 |
static void ResizeSecondarySwapchain(void* handle, std::int32_t width, std::int32_t height); |
| 178 |
/** @brief Hands the texture holding a secondary window's contents over to the next @ref PresentFrame() */ |
| 179 |
static void QueueSecondaryPresent(void* handle, const VulkanTexture* source); |
| 180 |
|
| 181 |
// -- Physical-device limits (consumed by VulkanRhiCapabilities to publish the backend's real values) -- |
| 182 |
|
| 183 |
/** @brief Returns the device's largest supported 2D image dimension (`maxImageDimension2D`); a safe default before device creation */ |
| 184 |
static std::int32_t GetMaxTextureDimension(); |
| 185 |
/** @brief Returns the device's minimum uniform-buffer bind-offset alignment in bytes (`minUniformBufferOffsetAlignment`) */ |
| 186 |
static std::int32_t GetUniformBufferOffsetAlignment(); |
| 187 |
/** @brief Returns the device's largest allowed uniform-buffer descriptor range in bytes (`maxUniformBufferRange`) */ |
| 188 |
static std::int32_t GetMaxUniformBufferRange(); |
| 189 |
|
| 190 |
private: |
| 191 |
static constexpr std::uint32_t MaxTextureUnits = 8; |
| 192 |
static constexpr std::uint32_t MaxUniformBindings = 8; |
| 193 |
|
| 194 |
struct UniformRange |
| 195 |
{ |
| 196 |
const std::uint8_t* Data = nullptr; |
| 197 |
std::uint32_t Size = 0; |
| 198 |
}; |
| 199 |
|
| 200 |
static BlendingState _blending; |
| 201 |
static DepthTestState _depthTest; |
| 202 |
static CullFaceState _cullFace; |
| 203 |
static ScissorState _scissor; |
| 204 |
static Recti _viewport; |
| 205 |
static Colorf _clearColor; |
| 206 |
|
| 207 |
static VulkanShaderProgram* _currentProgram; |
| 208 |
static const VulkanTexture* _boundTextures[MaxTextureUnits]; |
| 209 |
static UniformRange _boundUniformRanges[MaxUniformBindings]; |
| 210 |
static VulkanRenderTarget* _currentRenderTarget; |
| 211 |
|
| 212 |
// Loader bootstrap (resolves the entry points declared in VulkanCommon.h). Void signatures, so these |
| 213 |
// stay declarable in this vulkan.h-free contract header. |
| 214 |
static void LoadGlobalFunctions(); |
| 215 |
static void LoadInstanceFunctions(); |
| 216 |
static void LoadDeviceFunctions(); |
| 217 |
}; |
| 218 |
} |