1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
|
#ifndef RENDER_VULKAN_H
#define RENDER_VULKAN_H
// NOTE: vulkan 1.3, dynamic rendering + synchronization2, no render passes or framebuffers.
// one pipeline, one descriptor (the glyph atlas), one instanced draw per clip rect.
//
// per frame in flight: a command buffer, a fence, an acquire semaphore, a persistently mapped
// instance buffer and a staging buffer for atlas uploads. the cpu builds frame N+1 while the
// gpu still draws frame N. "render done" semaphores are per swapchain image, since the
// presentation engine holds on to them until that image comes back.
#define VK_FRAMES_IN_FLIGHT 2
#define VK_MAX_SWAPCHAIN_IMAGES 8
typedef struct VulkanFrame VulkanFrame;
struct VulkanFrame {
VkCommandBuffer cmd;
VkFence fence;
VkSemaphore acquired;
VkBuffer instances;
VkDeviceMemory instances_memory;
void *instances_mapped;
VkBuffer staging;
VkDeviceMemory staging_memory;
void *staging_mapped;
};
typedef struct VulkanRenderer VulkanRenderer;
struct VulkanRenderer {
VkInstance instance;
VkDebugUtilsMessengerEXT messenger;
VkSurfaceKHR surface;
VkPhysicalDevice gpu;
VkDevice device;
VkQueue queue;
u32 queue_family;
VkPhysicalDeviceMemoryProperties memory;
VkSwapchainKHR swapchain;
VkFormat format;
VkColorSpaceKHR color_space;
VkCompositeAlphaFlagBitsKHR composite_alpha;
VkPresentModeKHR present_mode;
VkExtent2D extent;
u32 image_count;
VkImage images[VK_MAX_SWAPCHAIN_IMAGES];
VkImageView views[VK_MAX_SWAPCHAIN_IMAGES];
VkSemaphore rendered[VK_MAX_SWAPCHAIN_IMAGES];
b32 swapchain_dirty;
b32 transparent; // compositor blends our alpha (rounded corners)
VkCommandPool pool;
VulkanFrame frames[VK_FRAMES_IN_FLIGHT];
u32 frame_index;
VkDescriptorSetLayout set_layout;
VkDescriptorPool descriptor_pool;
VkDescriptorSet descriptor_set;
VkPipelineLayout pipeline_layout;
VkPipeline pipeline;
//- debug capture (--screenshot): read the next frame back and write it as a ppm
const char *capture_path;
b32 captured;
b32 can_capture;
VkBuffer readback;
VkDeviceMemory readback_memory;
void *readback_mapped;
//- profiling
f32 wait_ms; // fence: waiting for the gpu to finish the frame before last
f32 acquire_ms;
f32 record_ms;
f32 submit_ms; // submit + present
u64 gpu_bytes; // every buffer / image we allocated
u32 batches; // draw calls last frame
VkImage atlas;
VkDeviceMemory atlas_memory;
VkImageView atlas_view;
VkSampler sampler;
b32 atlas_ready;
};
internal b32 vk_init(VulkanRenderer *renderer, struct wl_display *display, struct wl_surface *surface, u32 width, u32 height);
internal void vk_resize(VulkanRenderer *renderer, u32 width, u32 height);
internal DrawInstance *vk_frame_begin(VulkanRenderer *renderer); // waits for the frame slot, returns its mapped instance buffer
internal b32 vk_render(VulkanRenderer *renderer, DrawList *list, FontSystem *fonts, u32 clear_rgba);
internal void vk_shutdown(VulkanRenderer *renderer);
#endif // RENDER_VULKAN_H
|