TachyonUI 0.1.0
High-performance UI for building tools.
Loading...
Searching...
No Matches
vk_window_context.h
Go to the documentation of this file.
1#pragma once
2
3#include <chrono>
4
7
10
11namespace tachyon
12{
13 class GraphicsContext;
14}
15
16
17namespace tachyon::vulkan
18{
21
23
24 constexpr unsigned int FRAME_OVERLAP = 2;
25 static_assert(FRAME_DELETION_SLOT_COUNT == FRAME_OVERLAP + 1,
26 "Frame-scope deletion ring needs one more slot than FRAME_OVERLAP");
27
28 struct FrameData
29 {
30 VkSemaphore swapchainSemaphore = {};
31 VkFence renderFence = {};
32
36
37 VkCommandPool commandPool = {};
38 VkCommandBuffer mainCommandBuffer = {};
39
43 VkDeviceAddress primitiveUploadAddress = 0;
46
52
55 VkQueryPool timestamps = VK_NULL_HANDLE;
56 bool timestampsWritten = false;
57 };
58
60 {
61 // swapchain
62 VkSwapchainKHR swapchain = {};
63 VkFormat swapchainImageFormat = {};
64 VkExtent2D swapchainExtent = {};
65 ::std::vector<VkImage> swapchainImages;
66 ::std::vector<VkImageView> swapchainImageViews;
67 // Signaled by the frame's submit, waited on by its present; one per image because
68 // no fence covers the present's wait, so a frame slot's semaphore could still be pending.
69 ::std::vector<VkSemaphore> presentSemaphores;
70 uint32_t swapchainImageIndex = 0;
71 VkPresentModeKHR presentMode = VK_PRESENT_MODE_FIFO_KHR;
72 };
73
75 {
76 // commands
78 VkCommandBuffer commandBuffer = {};
79 };
80
82 {
83 public:
87 void* nativeHandle { nullptr };
89
91
94
95 // surface
96 VkSurfaceKHR surface = {};
97 VkExtent2D windowExtent = {};
98 VkExtent2D drawExtent = {};
99
100 bool resize_requested{ false };
101 bool freeze_rendering{ false };
103 int frameNumber{ 0 };
104
107 std::chrono::steady_clock::time_point lastPresentEnd{};
108 bool havePresentEnd{ false };
109 double lastBuildSeconds{ 0.0 };
110 bool lastBuildValid{ false };
112 bool presentPending{ false };
115
117 bool needsReadback{ false };
118 bool renderToImage{ false };
119 VkImage targetImage{ VK_NULL_HANDLE };
120 VkExtent2D targetExtent{};
121 bool targetWritten{ false };
123 std::vector<uint8_t> cachedBitmap;
124
127
129
134
137 void CreateDrawDepthImages(VulkanImage& drawImage, VulkanImage& depthImage, VkFormat colorFormat);
138 void DestroyDrawDepthImages(VulkanImage& drawImage, VulkanImage& depthImage);
139
140 VkCommandBuffer BeginWindowFrame(FrameData& currentFrame);
141 void EndWindowFrame(FrameData& currentFrame);
142
143 void PerformReadback(const VulkanImage& drawImage);
144
147 double LastGpuFrameSeconds() const { return lastGpuFrameSeconds; }
149 float timestampPeriodNs = 0.0f;
150 bool ReadPixels(std::span<uint8_t> bgra) override;
152 void SetImageTarget(VkImage image, VkExtent2D extent);
153 void TransitionTarget(VkCommandBuffer cmd, VkImageLayout from, VkImageLayout to,
154 VkPipelineStageFlags2 srcStage, VkAccessFlags2 srcAccess,
155 VkPipelineStageFlags2 dstStage, VkAccessFlags2 dstAccess);
156 void UpdateLayeredWindowFromCache(int screenX, int screenY);
157
159 void Destroy();
160
161
162 tachyon::ID GetID() const override { return id; }
163
164 void RenderWindowFrame(tachyon::GraphicsContext* graphicsContext, tachyon::IntRect rect, bool resizing) override;
165 void RecordWindowFrame(tachyon::GraphicsContext* graphicsContext, tachyon::IntRect rect) override;
166 void PresentWindowFrame() override;
167 void RecordFrame(tachyon::GraphicsContext* graphicsContext, tachyon::IntRect rect, bool resizing);
168
169 void UpdateTransparentWindow(tachyon::int2 screenPos) override;
170
172 };
173
175}
A graphics backend: the device, the window contexts that render the GUI's windows,...
Definition gui_graphics_context.h:60
A window's rendering state: made by GraphicsContext::CreateWindowContext for each OS window,...
Definition gui_window_backend.h:72
The Vulkan device the GUI renders with: its own, or a host's (SetExternalDevice).
Definition vk_device_context.h:118
The default graphics backend, on Vulkan 1.3.
Definition vk_graphics_context.h:33
Definition vk_image.h:17
Definition vk_window_context.h:82
void RecordFrame(tachyon::GraphicsContext *graphicsContext, tachyon::IntRect rect, bool resizing)
bool ReadPixels(std::span< uint8_t > bgra) override
The last rendered frame, BGRA8 with tightly packed rows; bgra holds width x height x 4 bytes of the w...
bool renderToImage
WindowFlags::RenderToImage: no surface.
Definition vk_window_context.h:118
VkExtent2D targetExtent
Definition vk_window_context.h:120
static VulkanWindowContext * Create(tachyon::ID id, void *nativeHandle, tachyon::OSWindowHandle osHandle, tachyon::int2 size, tachyon::WindowFlags windowFlags, VulkanDeviceContext *deviceContext, tachyon::OnGuiFunc onGUI)
void DestroyDrawDepthImages(VulkanImage &drawImage, VulkanImage &depthImage)
bool presentPending
Recorded and submitted, waiting for PresentWindowFrame.
Definition vk_window_context.h:112
void CreateDrawDepthImages(VulkanImage &drawImage, VulkanImage &depthImage, VkFormat colorFormat)
Creates a draw and depth image pair sized to windowExtent; each FRAME_OVERLAP slot has its own.
std::chrono::steady_clock::time_point lastPresentEnd
When the last vkQueuePresentKHR returned.
Definition vk_window_context.h:107
VulkanDeviceContext * deviceContext
Definition vk_window_context.h:85
SwapchainState swapchain
Definition vk_window_context.h:92
bool havePresentEnd
Definition vk_window_context.h:108
double lastBuildSeconds
Definition vk_window_context.h:109
bool IsTransparent() const override
Whether the window has WindowFlags::Transparent.
Definition vk_window_context.h:128
void PerformReadback(const VulkanImage &drawImage)
double lastGpuFrameSeconds
Definition vk_window_context.h:148
tachyon::WindowFlags windowFlags
Definition vk_window_context.h:90
bool needsReadback
Transparent window: each frame is read back for the Windows layered window.
Definition vk_window_context.h:117
FrameData * readbackPending
Definition vk_window_context.h:113
VkExtent2D windowExtent
Definition vk_window_context.h:97
bool resize_requested
Definition vk_window_context.h:100
WindowVulkanState state
Definition vk_window_context.h:93
void RenderWindowFrame(tachyon::GraphicsContext *graphicsContext, tachyon::IntRect rect, bool resizing) override
A frame is recorded (drawn and submitted), then presented.
float timestampPeriodNs
nanoseconds per timestamp tick; 0: no timestamps
Definition vk_window_context.h:149
VkCommandBuffer BeginWindowFrame(FrameData &currentFrame)
void UpdateLayeredWindowFromCache(int screenX, int screenY)
VkImage targetImage
host image each frame is copied into
Definition vk_window_context.h:119
bool lastBuildValid
Definition vk_window_context.h:110
tachyon::vulkan::AllocatedBuffer readbackBuffer
Definition vk_window_context.h:122
tachyon::ID GetID() const override
The GUI window's ID; the backend passes it with the window's input.
Definition vk_window_context.h:162
VkSurfaceKHR surface
Definition vk_window_context.h:96
std::vector< uint8_t > cachedBitmap
Definition vk_window_context.h:123
void SetImageTarget(VkImage image, VkExtent2D extent)
The host image each frame is copied into (render-to-image windows); VK_NULL_HANDLE stops.
tachyon::ID id
Definition vk_window_context.h:84
void UpdateTransparentWindow(tachyon::int2 screenPos) override
A transparent window the OS composites from a CPU copy (a layered window on Windows): shows its last ...
void PresentWindowFrame() override
May wait for vsync; nothing to do for a render-to-image window.
VkExtent2D drawExtent
Definition vk_window_context.h:98
double LastGpuFrameSeconds() const
GPU time of the drawing of this window's most recent finished frame, in seconds; 0 until one finished...
Definition vk_window_context.h:147
bool targetWritten
targetImage holds a previous frame
Definition vk_window_context.h:121
tachyon::OnGuiFunc onGui
Definition vk_window_context.h:88
void ResizeSwapchain(tachyon::int2 size)
int frameNumber
Definition vk_window_context.h:103
bool freeze_rendering
Definition vk_window_context.h:101
tachyon::int2 recordedPosition
Definition vk_window_context.h:114
void EndWindowFrame(FrameData &currentFrame)
void * nativeHandle
Definition vk_window_context.h:87
tachyon::OSWindowHandle osHandle
Definition vk_window_context.h:86
void RecordWindowFrame(tachyon::GraphicsContext *graphicsContext, tachyon::IntRect rect) override
Records and submits a frame without presenting it; PresentWindowFrame presents.
tachyon::OSWindowHandle GetWindowHandle() const override
The window it was created for.
Definition vk_window_context.h:171
tachyon::int2 mouse_pos
Definition vk_window_context.h:102
void TransitionTarget(VkCommandBuffer cmd, VkImageLayout from, VkImageLayout to, VkPipelineStageFlags2 srcStage, VkAccessFlags2 srcAccess, VkPipelineStageFlags2 dstStage, VkAccessFlags2 dstAccess)
constexpr unsigned int FRAME_OVERLAP
Definition vk_window_context.h:24
constexpr unsigned int FRAME_DELETION_SLOT_COUNT
Frame deletion queue slots: FRAME_OVERLAP + 1, so the slot pushed to is never the one flushed in the ...
Definition vk_device_context.h:113
std::function< void()> OnGuiFunc
Definition gui_base_types.h:14
WindowFlags
How an OS window is made and behaves (IWindowBackend::CreateWindow); flags combine with |.
Definition gui_window_flags.h:12
void * OSWindowHandle
An OS window, opaque; what it points to is up to the window backend.
Definition gui_window_backend.h:37
@ Transparent
Definition gui_window_flags.h:25
@ None
Definition gui_window_flags.h:13
Definition vk_defaults.h:4
Definition gui_logging.h:24
A 64-bit element ID whose bit layout depends on its IDMode.
Definition gui_id.h:23
static constexpr ID Invalid()
The "no element" ID (all bits zero); also the default for optional ID parameters.
Definition gui_id.h:66
Definition gui_base_types.h:407
Integer 2D vector.
Definition gui_base_types.h:19
Definition vk_types.h:30
Definition vk_meshes.h:14
Library internal: destroy functions that run later, newest first.
Definition vk_device_context.h:47
Definition vk_descriptors.h:54
Definition vk_window_context.h:29
DeletionQueue deletionQueue
Definition vk_window_context.h:35
VkCommandPool commandPool
Definition vk_window_context.h:37
VulkanImage depthImage
Definition vk_window_context.h:51
size_t primitiveUploadCapacity
Definition vk_window_context.h:44
VkFence renderFence
Definition vk_window_context.h:31
Buffer primitiveUploadBuffer
Fence-protected, persistently mapped upload arena for GUI primitive streams, reused each time this fr...
Definition vk_window_context.h:42
VkSemaphore swapchainSemaphore
Definition vk_window_context.h:30
DeletionQueue flushQueue
Definition vk_window_context.h:34
VkCommandBuffer mainCommandBuffer
Definition vk_window_context.h:38
VulkanImage drawImage
Per-slot draw and depth images, so in-flight frames don't share them (frame N's blit reads drawImage ...
Definition vk_window_context.h:50
VkQueryPool timestamps
GPU timestamps at the start and the end of the frame's drawing; none when the queue can't time.
Definition vk_window_context.h:55
VkDeviceAddress primitiveUploadAddress
Definition vk_window_context.h:43
DescriptorAllocatorGrowable frameDescriptors
Definition vk_window_context.h:33
bool timestampsWritten
by the last frame in this slot
Definition vk_window_context.h:56
size_t primitiveUploadOffset
Definition vk_window_context.h:45
Definition vk_window_context.h:60
uint32_t swapchainImageIndex
Definition vk_window_context.h:70
::std::vector< VkImageView > swapchainImageViews
Definition vk_window_context.h:66
::std::vector< VkSemaphore > presentSemaphores
Definition vk_window_context.h:69
VkPresentModeKHR presentMode
Definition vk_window_context.h:71
VkExtent2D swapchainExtent
Definition vk_window_context.h:64
VkFormat swapchainImageFormat
Definition vk_window_context.h:63
VkSwapchainKHR swapchain
Definition vk_window_context.h:62
::std::vector< VkImage > swapchainImages
Definition vk_window_context.h:65
Definition vk_window_context.h:75
FrameData frames[FRAME_OVERLAP]
Definition vk_window_context.h:77
VkCommandBuffer commandBuffer
Definition vk_window_context.h:78