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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
|
#pragma once
#include <assert.h>
#include <vulkan/vulkan.h>
#include <vulkan/vulkan_core.h>
#include "darray.h"
#include "defines.h"
#include "log.h"
#include "str.h"
#define VULKAN_PHYS_DEVICE_MAX_EXTENSION_NAMES 36
DECL_TYPED_ARRAY(const char*, cstr)
static void plat_get_required_extension_names(cstr_darray* extensions) {
#ifdef CEL_PLATFORM_LINUX
cstr_darray_push(extensions, "VK_KHR_xcb_surface");
#endif
}
// TODO(omni): port to using internal assert functions
#define VK_CHECK(vulkan_expr) \
do { \
VkResult res = vulkan_expr; \
if (res != VK_SUCCESS) { \
ERROR_EXIT("Vulkan error: %u (%s:%d)", res, __FILE__, __LINE__); \
} \
} while (0)
// TODO: typedef struct vk_debugger {} vk_debugger;
typedef struct vulkan_physical_device_requirements {
bool graphics;
bool present;
bool compute;
bool transfer;
str8 device_ext_names[VULKAN_PHYS_DEVICE_MAX_EXTENSION_NAMES];
size_t device_ext_name_count;
bool sampler_anistropy;
bool discrete_gpu;
} vulkan_physical_device_requirements;
#define VULKAN_MAX_DEFAULT 32
typedef struct vulkan_swapchain_support_info {
VkSurfaceCapabilitiesKHR capabilities;
VkSurfaceFormatKHR formats[VULKAN_MAX_DEFAULT];
u32 format_count;
VkPresentModeKHR present_modes[VULKAN_MAX_DEFAULT];
u32 mode_count;
} vulkan_swapchain_support_info;
VKAPI_ATTR VkBool32 VKAPI_CALL vk_debug_callback(
VkDebugUtilsMessageSeverityFlagBitsEXT severity, VkDebugUtilsMessageTypeFlagsEXT flags,
const VkDebugUtilsMessengerCallbackDataEXT* callback_data, void* user_data);
static void vulkan_device_query_swapchain_support(VkPhysicalDevice device, VkSurfaceKHR surface,
vulkan_swapchain_support_info* out_support_info) {
// TODO: add VK_CHECK to these calls!
// Surface capabilities
vkGetPhysicalDeviceSurfaceCapabilitiesKHR(device, surface, &out_support_info->capabilities);
// Surface formats
vkGetPhysicalDeviceSurfaceFormatsKHR(device, surface, &out_support_info->format_count,
0); // Get number of formats
if (out_support_info->format_count > 0) {
vkGetPhysicalDeviceSurfaceFormatsKHR(device, surface, &out_support_info->format_count,
out_support_info->formats);
}
// Present Modes
vkGetPhysicalDeviceSurfacePresentModesKHR(device, surface, &out_support_info->mode_count,
0); // Get number of formats
if (out_support_info->mode_count > 0) {
vkGetPhysicalDeviceSurfacePresentModesKHR(device, surface, &out_support_info->mode_count,
out_support_info->present_modes);
}
}
static VkSurfaceFormatKHR choose_swapchain_format(
vulkan_swapchain_support_info* swapchain_support) {
assert(swapchain_support->format_count > 0);
// find a format
for (u32 i = 0; i < swapchain_support->format_count; i++) {
VkSurfaceFormatKHR format = swapchain_support->formats[i];
if (format.format == VK_FORMAT_B8G8R8A8_SRGB &&
format.colorSpace == VK_COLOR_SPACE_SRGB_NONLINEAR_KHR) {
return format;
}
}
return swapchain_support->formats[0];
}
// static bool physical_device_meets_requirements(
// VkPhysicalDevice device, VkSurfaceKHR surface, const VkPhysicalDeviceProperties* properties,
// const VkPhysicalDeviceFeatures* features,
// const vulkan_physical_device_requirements* requirements,
// vulkan_physical_device_queue_family_info* out_queue_info,
// vulkan_swapchain_support_info* out_swapchain_support) {
// // TODO: pass in an arena
// out_queue_info->graphics_family_index = -1;
// out_queue_info->present_family_index = -1;
// out_queue_info->compute_family_index = -1;
// out_queue_info->transfer_family_index = -1;
// if (requirements->discrete_gpu) {
// if (properties->deviceType != VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU) {
// TRACE("Device is not a physical GPU. Skipping.");
// return false;
// }
// }
// u32 queue_family_count = 0;
// vkGetPhysicalDeviceQueueFamilyProperties(device, &queue_family_count, 0);
// VkQueueFamilyProperties queue_families[queue_family_count];
// vkGetPhysicalDeviceQueueFamilyProperties(device, &queue_family_count, queue_families);
// INFO("Graphics | Present | Compute | Transfer | Name");
// u8 min_transfer_score = 255;
// for (u32 i = 0; i < queue_family_count; i++) {
// u8 current_transfer_score = 0;
// // Graphics queue
// if (queue_families[i].queueFlags & VK_QUEUE_GRAPHICS_BIT) {
// out_queue_info->graphics_family_index = i;
// current_transfer_score++;
// }
// // Compute queue
// if (queue_families[i].queueFlags & VK_QUEUE_COMPUTE_BIT) {
// out_queue_info->compute_family_index = i;
// current_transfer_score++;
// }
// // Transfer queue
// if (queue_families[i].queueFlags & VK_QUEUE_TRANSFER_BIT) {
// // always take the lowest score transfer index
// if (current_transfer_score <= min_transfer_score) {
// min_transfer_score = current_transfer_score;
// out_queue_info->transfer_family_index = i;
// }
// }
// // Present Queue
// VkBool32 supports_present = VK_FALSE;
// vkGetPhysicalDeviceSurfaceSupportKHR(device, i, surface, &supports_present);
// if (supports_present) {
// out_queue_info->present_family_index = i;
// }
// }
// INFO(" %d | %d | %d | %d | %s",
// out_queue_info->graphics_family_index != -1, out_queue_info->present_family_index != -1,
// out_queue_info->compute_family_index != -1, out_queue_info->transfer_family_index != -1,
// properties->deviceName);
// TRACE("Graphics Family queue index: %d", out_queue_info->graphics_family_index);
// TRACE("Present Family queue index: %d", out_queue_info->present_family_index);
// TRACE("Compute Family queue index: %d", out_queue_info->compute_family_index);
// TRACE("Transfer Family queue index: %d", out_queue_info->transfer_family_index);
// if ((!requirements->graphics ||
// (requirements->graphics && out_queue_info->graphics_family_index != -1))) {
// INFO("Physical device meets our requirements! Proceed.");
// vulkan_device_query_swapchain_support(
// device, surface, out_swapchain_support
// // TODO: error handling i.e. format count = 0 or present mode = 0
// );
// return true;
// }
// return false;
// }
VKAPI_ATTR VkBool32 VKAPI_CALL vk_debug_callback(
VkDebugUtilsMessageSeverityFlagBitsEXT severity, VkDebugUtilsMessageTypeFlagsEXT flags,
const VkDebugUtilsMessengerCallbackDataEXT* callback_data, void* user_data) {
switch (severity) {
default:
case VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT:
ERROR("%s", callback_data->pMessage);
break;
case VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT:
WARN("%s", callback_data->pMessage);
break;
case VK_DEBUG_UTILS_MESSAGE_SEVERITY_INFO_BIT_EXT:
INFO("%s", callback_data->pMessage);
break;
case VK_DEBUG_UTILS_MESSAGE_SEVERITY_VERBOSE_BIT_EXT:
TRACE("%s", callback_data->pMessage);
break;
}
return VK_FALSE;
}
|