Files

73 lines
9.9 KiB
Markdown
Raw Permalink Normal View History

# Vulkan Backend (Vulkan::Graphics)
> The Vulkan implementation: device/queue setup, resource creation, validation, and the capability gates for mesh shading and ray tracing.
This is a **navigation** doc: it tells you where things live, how data actually flows, and the sharp edges — so you can orient in minutes instead of hours. It complements the repository's [AGENTS.md](../../AGENTS.md) (map + rules) and the [cross-repo guide](../cross-repo.md) (engine ↔ demo contract). Everything below is grounded in the checked-out source at commit `d3ed573`.
## Key types & where they live
| Type | Defined in |
|------|------------|
| `Vulkan::Graphics` | `src/Engine/Graphics/Graphics.h` |
| `Vulkan::Window` | `src/Engine/Graphics/Metal/Window.h` |
| `Vulkan::Viewport` | `src/Editor/Window/ViewportControl.h` (search) |
| `Vulkan::RenderPass` | `src/Engine/Graphics/Metal/RenderPass.h` |
| `Vulkan::Framebuffer` | `src/Engine/Graphics/Vulkan/Framebuffer.h` |
| `Vulkan::Buffer` | `src/Engine/Graphics/Buffer.h` |
| `Vulkan::ShaderBuffer/UniformBuffer/VertexBuffer/IndexBuffer` | `git grep` in `src/` |
| `Vulkan::Texture/Texture2D/2DArray/3D/Cube/TextureView/Sampler` | `src/Engine/Graphics/Metal/Texture.h` |
| `Vulkan::Shader (all stages)` | `git grep` in `src/` |
| `Vulkan::DescriptorLayout/DescriptorSet/PipelineLayout` | `git grep` in `src/` |
| `Vulkan::GraphicsPipeline/ComputePipeline/RayTracingPipeline` | `git grep` in `src/` |
| `Vulkan::CommandBuffer (RenderCommand/ComputeCommand)` | `git grep` in `src/` |
| `Vulkan::OcclusionQuery/PipelineStatisticsQuery/TimestampQuery` | `git grep` in `src/` |
| `Vulkan::BottomLevelAS/TopLevelAS` | `git grep` in `src/` |
| `Vulkan::Allocator (VMA)` | `git grep` in `src/` |
| `Vulkan::Queue` | `src/Engine/Graphics/Vulkan/Queue.h` |
| `Vulkan::PipelineCache` | `src/Engine/Graphics/Metal/PipelineCache.h` |
| `Vulkan::Debug (layer)` | `git grep` in `src/` |
Paths above are resolved against the actual source tree (namespace ≈ directory in this codebase). A `(search)` marker means the file contains the name but may also define others — open it and grep. `git grep` is always a fallback. The full per-file map is in **Where to look** below.
## Data flow / lifecycle
Vulkan::Graphics (src/Engine/Graphics/Vulkan/Graphics.h + Graphics.cpp, 10.7KB header + 52KB impl) implements Gfx::Graphics. init(GraphicsInitializer) creates the VkInstance (with validation layers when ENABLE_VALIDATION, debug callbacks via Debug.h/.cpp), VkSurface (from the OS window handle), selects a physical device + queue families (graphics/async/transfer + optional ray tracing queue), creates VkDevice with all required extensions (VK_KHR_ray_tracing_pipeline, VK_NV_mesh_shader, VK_EXT_mesh_shader, VK_KHR_acceleration_structure, VK_KHR_deferred_host_operations, VK_EXT_vertex_input_dynamic_state, VK_EXT_tooling_info, swapchain, sync2, timeline semaphore, etc.). QueueFamilyMapping records which queue index hosts graphics/async/transfer/RT work. The VkDevice is stored alongside a Vulkan::Allocator (VMA-based memory allocator, Allocator.h + Allocator.cpp, 13KB) for all buffer/texture memory. Window.cpp (18KB) handles the OS window (X11 on Linux via GLFW or raw X11, Win32 on Windows) + VkSurface + swapchain management + frame loop (acquire next image, present). Viewport is a sub-rectangle of a Window (multiple viewports can share one window). RenderPass.cpp (12KB) creates VkRenderPass + VkFramebuffer objects (Framebuffer.h + .cpp, 3.8KB) — framebuffers are per-swapchain-image. Command.cpp (26KB) implements the command buffer pool: RenderCommand and ComputeCommand wrap VkCommandBuffer, with beginRenderPass/endRenderPass, draw/drawIndexed/drawMeshTasksNV/drawMeshTasksEXT, dispatch/dispatchBase, bindPipeline/bindDescriptorSets/bindVertexBuffers/bindIndexBuffer, beginQuery/endQuery, copyBuffer/copyImage/blendImage, buildAccelerationStructure, etc. Descriptor.cpp (20KB) implements descriptor pool + layout + set allocation. PipelineCache.cpp (37KB) loads/saves VkPipelineCacheData to disk and caches VkPipeline objects keyed by a hash. RayTracing.cpp (11KB) implements BLAS/TLAS creation + building (vkCmdBuildAccelerationStructuresKHR) and the 6 RT shader stages (raygen/anyhit/closesthit/miss/intersection/callable). Query.cpp (6.5KB) implements occlusion queries (VkQueryPool + vkGetQueryPoolResults), pipeline statistics queries (VkPipelineStatisticsQuery), and timestamp queries. Texture.cpp (27KB) implements all texture types (1D/2D/3D/cube/array) with mipmap generation (vkCmdGenerateMipmaps), sampling (VkSampler), and texture views (VkImageView). Buffer.cpp (22KB) implements all buffer types with VMA allocation + vkCmdCopyBuffer for uploads. Graphics.cpp also implements: createRenderPass (full VkRenderPass creation from RenderTargetLayout + SubPassDependency array), executeCommands (submit to VkQueue with fence), waitDeviceIdle (vkDeviceWaitIdle), resolveTexture (vkCmdBlitImage), copyTexture (vkCmdCopyImage), copyBuffer (vkCmdCopyBuffer), all RT factory methods, and the capability flags (meshShadingEnabled from VK_NV_mesh_shader/VK_EXT_mesh_shader extension presence, rayTracingEnabled from VK_KHR_ray_tracing_pipeline).
## Where to look
| Path | What's there |
|------|--------------|
| `src/Engine/Graphics/Vulkan/Graphics.h` | 10.7KB — full Vulkan backend interface + members |
| `src/Engine/Graphics/Vulkan/Graphics.cpp` | 52KB — device creation, all factory methods |
| `src/Engine/Graphics/Vulkan/Window.h + .cpp` | OS window + swapchain, 18KB impl |
| `src/Engine/Graphics/Vulkan/RenderPass.h + .cpp` | VkRenderPass + framebuffer creation |
| `src/Engine/Graphics/Vulkan/Framebuffer.h + .cpp` | VkFramebuffer wrapper |
| `src/Engine/Graphics/Vulkan/Command.h + .cpp` | command buffer pool, 26KB impl — all draw/dispatch/AS build commands |
| `src/Engine/Graphics/Vulkan/Descriptor.h + .cpp` | descriptor pool/layout/set, 20KB impl |
| `src/Engine/Graphics/Vulkan/PipelineCache.h + .cpp` | pipeline cache, 37KB impl |
| `src/Engine/Graphics/Vulkan/RayTracing.h + .cpp` | BLAS/TLAS + RT shaders, 11KB impl |
| `src/Engine/Graphics/Vulkan/Query.h + .cpp` | occlusion/stats/timestamp queries |
| `src/Engine/Graphics/Vulkan/Texture.h + .cpp` | all texture types, 27KB impl |
| `src/Engine/Graphics/Vulkan/Buffer.h + .cpp` | all buffer types + VMA, 22KB impl |
| `src/Engine/Graphics/Vulkan/Allocator.h + .cpp` | VMA memory allocator |
| `src/Engine/Graphics/Vulkan/Queue.h + .cpp` | VkQueue wrapper + fence |
| `src/Engine/Graphics/Vulkan/Debug.h + .cpp` | Vulkan validation layer callbacks |
| `src/Engine/Graphics/Vulkan/Shader.h + .cpp` | VkShaderModule creation |
## Gotchas & sharp edges
- The Vulkan backend uses VMA (Vulkan Memory Allocator) for ALL buffer and texture memory. The Allocator wraps a VmaAllocator. You cannot free memory directly — you must go through the allocator's destroy methods. VMA must be compiled and linked (it is a sub-dependency, not in the Seele source tree).
- Ray tracing requires VK_KHR_ray_tracing_pipeline AND a physical device that exposes VK_KHR_acceleration_structure. The capability flag rayTracingEnabled is set at init() time based on device extension availability. If RT is not supported, RayTracingPass is never added to the render graph (GameView.cpp line 39: if graphics->supportRayTracing()).
- Mesh shading (VK_NV_mesh_shader or VK_EXT_mesh_shader) is detected at init() and sets meshShadingEnabled. If false, Graphics::createGraphicsPipeline(MeshPipelineCreateInfo) will assert/fail. The Vulkan backend must have the extension enabled in VkDeviceCreateInfo.
- The command buffer pool in Command.cpp is a per-queue-type (graphics/async/transfer) pool of VkCommandBuffer objects. Each RenderCommand/ComputeCommand checks out a buffer, records, and checks back in. The pool is sized at init() based on the max in-flight frames. If the pool is exhausted, the backend will stall.
- Framebuffer creation is PER SWAPCHAIN IMAGE — each VkFramebuffer in Framebuffer.cpp corresponds to one swapchain image. The RenderPass::createRenderPass method creates one framebuffer per image. If the swapchain is resized, all framebuffers are destroyed and recreated (applyArea path).
- The Vulkan backend does NOT use the Vulkan Validation Layer in release builds — ENABLE_VALIDATION in Graphics.h (line 7) is a compile-time define that is always on in this codebase. In production you would want to #undef it or make it a build flag.
- Queue.cpp wraps a single VkQueue + VkFence. There is ONE fence per queue. waitDeviceIdle() calls vkDeviceWaitIdle on the entire device (all queues). There is no per-queue synchronization primitive exposed through the Gfx::Graphics interface — the backend manages fences internally.
- The debug layer (Debug.h/.cpp) installs VK_EXT_debug_utils callbacks at device creation time. All validation layer messages are routed through a single callback that forwards to stderr. There is no UI for viewing validation errors — they go to the console.
- Texture.cpp handles mipmap generation via vkCmdGenerateMipmaps. For texture arrays and cubes, the entire array/cube is generated in one call. The texture's initial layout must be TRANSFER_DST_OPTIMAL for this to work — the Texture constructor sets the correct initial layout based on the usage flags in TextureCreateInfo.
- The Vulkan backend's Window.cpp handles BOTH X11 (Linux) and Win32 (Windows) natively. On Linux it uses GLFW for window creation + X11 for the surface (glfwCreateWindow + glfwGetX11Window). On Windows it uses Win32 directly (CreateWindow + CreateSwapChainHcu). There is no platform abstraction layer — the #ifdef WIN32 blocks are in the source.
- PipelineCache.cpp uses a hash of the pipeline create-info struct + shader SPIR-V bytes as the cache key. If ANY field of the create-info changes (even a padding byte), the cache miss occurs. The cache file is stored next to the executable (or in a user-specified path from GraphicsInitializer).
---
*Generated from a full read of the checked-out source at commit `d3ed573`. If a path has moved, `git grep` for the type name — the engine is small enough to find anything in seconds.*