1 /*
2 * Copyright (c) 2015-2019 The Khronos Group Inc.
3 * Copyright (c) 2015-2019 Valve Corporation
4 * Copyright (c) 2015-2019 LunarG, Inc.
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *
18 * Author: Chia-I Wu <olv@lunarg.com>
19 * Author: Courtney Goeltzenleuchter <courtney@LunarG.com>
20 * Author: Ian Elliott <ian@LunarG.com>
21 * Author: Ian Elliott <ianelliott@google.com>
22 * Author: Jon Ashburn <jon@lunarg.com>
23 * Author: Gwan-gyeong Mun <elongbug@gmail.com>
24 * Author: Tony Barbour <tony@LunarG.com>
25 * Author: Bill Hollings <bill.hollings@brenwill.com>
26 */
27
28 #define _GNU_SOURCE
29 #include <stdio.h>
30 #include <stdarg.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <stdbool.h>
34 #include <assert.h>
35 #include <signal.h>
36 #if defined(VK_USE_PLATFORM_XLIB_KHR) || defined(VK_USE_PLATFORM_XCB_KHR)
37 #include <X11/Xutil.h>
38 #elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
39 #include <linux/input.h>
40 #include "xdg-shell-client-header.h"
41 #include "xdg-decoration-client-header.h"
42 #endif
43
44 #ifdef _WIN32
45 #ifdef _MSC_VER
46 #pragma comment(linker, "/subsystem:windows")
47 #endif // MSVC
48 #define APP_NAME_STR_LEN 80
49 #endif // _WIN32
50
51 #ifdef ANDROID
52 #include "vulkan_wrapper.h"
53 #else
54 #include <vulkan/vulkan.h>
55 #endif
56
57 #include <vulkan/vk_sdk_platform.h>
58 #include "linmath.h"
59 #include "object_type_string_helper.h"
60
61 #include "gettime.h"
62 #include "inttypes.h"
63 #define MILLION 1000000L
64 #define BILLION 1000000000L
65
66 #define DEMO_TEXTURE_COUNT 1
67 #define APP_SHORT_NAME "vkcube"
68 #define APP_LONG_NAME "Vulkan Cube"
69
70 // Allow a maximum of two outstanding presentation operations.
71 #define FRAME_LAG 2
72
73 #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
74
75 #if defined(NDEBUG) && defined(__GNUC__)
76 #define U_ASSERT_ONLY __attribute__((unused))
77 #else
78 #define U_ASSERT_ONLY
79 #endif
80
81 #if defined(__GNUC__)
82 #define UNUSED __attribute__((unused))
83 #else
84 #define UNUSED
85 #endif
86
87 #ifdef _WIN32
88 bool in_callback = false;
89 #define ERR_EXIT(err_msg, err_class) \
90 do { \
91 if (!demo->suppress_popups) MessageBox(NULL, err_msg, err_class, MB_OK); \
92 exit(1); \
93 } while (0)
DbgMsg(char * fmt,...)94 void DbgMsg(char *fmt, ...) {
95 va_list va;
96 va_start(va, fmt);
97 vprintf(fmt, va);
98 va_end(va);
99 fflush(stdout);
100 }
101
102 #elif defined __ANDROID__
103 #include <android/log.h>
104 #define ERR_EXIT(err_msg, err_class) \
105 do { \
106 ((void)__android_log_print(ANDROID_LOG_INFO, "Vulkan Cube", err_msg)); \
107 exit(1); \
108 } while (0)
109 #ifdef VARARGS_WORKS_ON_ANDROID
DbgMsg(const char * fmt,...)110 void DbgMsg(const char *fmt, ...) {
111 va_list va;
112 va_start(va, fmt);
113 __android_log_print(ANDROID_LOG_INFO, "Vulkan Cube", fmt, va);
114 va_end(va);
115 }
116 #else // VARARGS_WORKS_ON_ANDROID
117 #define DbgMsg(fmt, ...) \
118 do { \
119 ((void)__android_log_print(ANDROID_LOG_INFO, "Vulkan Cube", fmt, ##__VA_ARGS__)); \
120 } while (0)
121 #endif // VARARGS_WORKS_ON_ANDROID
122 #else
123 #define ERR_EXIT(err_msg, err_class) \
124 do { \
125 printf("%s\n", err_msg); \
126 fflush(stdout); \
127 exit(1); \
128 } while (0)
DbgMsg(char * fmt,...)129 void DbgMsg(char *fmt, ...) {
130 va_list va;
131 va_start(va, fmt);
132 vprintf(fmt, va);
133 va_end(va);
134 fflush(stdout);
135 }
136 #endif
137
138 #define GET_INSTANCE_PROC_ADDR(inst, entrypoint) \
139 { \
140 demo->fp##entrypoint = (PFN_vk##entrypoint)vkGetInstanceProcAddr(inst, "vk" #entrypoint); \
141 if (demo->fp##entrypoint == NULL) { \
142 ERR_EXIT("vkGetInstanceProcAddr failed to find vk" #entrypoint, "vkGetInstanceProcAddr Failure"); \
143 } \
144 }
145
146 static PFN_vkGetDeviceProcAddr g_gdpa = NULL;
147
148 #define GET_DEVICE_PROC_ADDR(dev, entrypoint) \
149 { \
150 if (!g_gdpa) g_gdpa = (PFN_vkGetDeviceProcAddr)vkGetInstanceProcAddr(demo->inst, "vkGetDeviceProcAddr"); \
151 demo->fp##entrypoint = (PFN_vk##entrypoint)g_gdpa(dev, "vk" #entrypoint); \
152 if (demo->fp##entrypoint == NULL) { \
153 ERR_EXIT("vkGetDeviceProcAddr failed to find vk" #entrypoint, "vkGetDeviceProcAddr Failure"); \
154 } \
155 }
156
157 /*
158 * structure to track all objects related to a texture.
159 */
160 struct texture_object {
161 VkSampler sampler;
162
163 VkImage image;
164 VkBuffer buffer;
165 VkImageLayout imageLayout;
166
167 VkMemoryAllocateInfo mem_alloc;
168 VkDeviceMemory mem;
169 VkImageView view;
170 int32_t tex_width, tex_height;
171 };
172
173 static char *tex_files[] = {"lunarg.ppm"};
174
175 static int validation_error = 0;
176
177 struct vktexcube_vs_uniform {
178 // Must start with MVP
179 float mvp[4][4];
180 float position[12 * 3][4];
181 float attr[12 * 3][4];
182 };
183
184 //--------------------------------------------------------------------------------------
185 // Mesh and VertexFormat Data
186 //--------------------------------------------------------------------------------------
187 // clang-format off
188 static const float g_vertex_buffer_data[] = {
189 -1.0f,-1.0f,-1.0f, // -X side
190 -1.0f,-1.0f, 1.0f,
191 -1.0f, 1.0f, 1.0f,
192 -1.0f, 1.0f, 1.0f,
193 -1.0f, 1.0f,-1.0f,
194 -1.0f,-1.0f,-1.0f,
195
196 -1.0f,-1.0f,-1.0f, // -Z side
197 1.0f, 1.0f,-1.0f,
198 1.0f,-1.0f,-1.0f,
199 -1.0f,-1.0f,-1.0f,
200 -1.0f, 1.0f,-1.0f,
201 1.0f, 1.0f,-1.0f,
202
203 -1.0f,-1.0f,-1.0f, // -Y side
204 1.0f,-1.0f,-1.0f,
205 1.0f,-1.0f, 1.0f,
206 -1.0f,-1.0f,-1.0f,
207 1.0f,-1.0f, 1.0f,
208 -1.0f,-1.0f, 1.0f,
209
210 -1.0f, 1.0f,-1.0f, // +Y side
211 -1.0f, 1.0f, 1.0f,
212 1.0f, 1.0f, 1.0f,
213 -1.0f, 1.0f,-1.0f,
214 1.0f, 1.0f, 1.0f,
215 1.0f, 1.0f,-1.0f,
216
217 1.0f, 1.0f,-1.0f, // +X side
218 1.0f, 1.0f, 1.0f,
219 1.0f,-1.0f, 1.0f,
220 1.0f,-1.0f, 1.0f,
221 1.0f,-1.0f,-1.0f,
222 1.0f, 1.0f,-1.0f,
223
224 -1.0f, 1.0f, 1.0f, // +Z side
225 -1.0f,-1.0f, 1.0f,
226 1.0f, 1.0f, 1.0f,
227 -1.0f,-1.0f, 1.0f,
228 1.0f,-1.0f, 1.0f,
229 1.0f, 1.0f, 1.0f,
230 };
231
232 static const float g_uv_buffer_data[] = {
233 0.0f, 1.0f, // -X side
234 1.0f, 1.0f,
235 1.0f, 0.0f,
236 1.0f, 0.0f,
237 0.0f, 0.0f,
238 0.0f, 1.0f,
239
240 1.0f, 1.0f, // -Z side
241 0.0f, 0.0f,
242 0.0f, 1.0f,
243 1.0f, 1.0f,
244 1.0f, 0.0f,
245 0.0f, 0.0f,
246
247 1.0f, 0.0f, // -Y side
248 1.0f, 1.0f,
249 0.0f, 1.0f,
250 1.0f, 0.0f,
251 0.0f, 1.0f,
252 0.0f, 0.0f,
253
254 1.0f, 0.0f, // +Y side
255 0.0f, 0.0f,
256 0.0f, 1.0f,
257 1.0f, 0.0f,
258 0.0f, 1.0f,
259 1.0f, 1.0f,
260
261 1.0f, 0.0f, // +X side
262 0.0f, 0.0f,
263 0.0f, 1.0f,
264 0.0f, 1.0f,
265 1.0f, 1.0f,
266 1.0f, 0.0f,
267
268 0.0f, 0.0f, // +Z side
269 0.0f, 1.0f,
270 1.0f, 0.0f,
271 0.0f, 1.0f,
272 1.0f, 1.0f,
273 1.0f, 0.0f,
274 };
275 // clang-format on
276
dumpMatrix(const char * note,mat4x4 MVP)277 void dumpMatrix(const char *note, mat4x4 MVP) {
278 int i;
279
280 printf("%s: \n", note);
281 for (i = 0; i < 4; i++) {
282 printf("%f, %f, %f, %f\n", MVP[i][0], MVP[i][1], MVP[i][2], MVP[i][3]);
283 }
284 printf("\n");
285 fflush(stdout);
286 }
287
dumpVec4(const char * note,vec4 vector)288 void dumpVec4(const char *note, vec4 vector) {
289 printf("%s: \n", note);
290 printf("%f, %f, %f, %f\n", vector[0], vector[1], vector[2], vector[3]);
291 printf("\n");
292 fflush(stdout);
293 }
294
295 typedef struct {
296 VkImage image;
297 VkCommandBuffer cmd;
298 VkCommandBuffer graphics_to_present_cmd;
299 VkImageView view;
300 VkBuffer uniform_buffer;
301 VkDeviceMemory uniform_memory;
302 void *uniform_memory_ptr;
303 VkFramebuffer framebuffer;
304 VkDescriptorSet descriptor_set;
305 } SwapchainImageResources;
306
307 struct demo {
308 #if defined(VK_USE_PLATFORM_WIN32_KHR)
309 #define APP_NAME_STR_LEN 80
310 HINSTANCE connection; // hInstance - Windows Instance
311 char name[APP_NAME_STR_LEN]; // Name to put on the window/icon
312 HWND window; // hWnd - window handle
313 POINT minsize; // minimum window size
314 #elif defined(VK_USE_PLATFORM_XLIB_KHR)
315 Display *display;
316 Window xlib_window;
317 Atom xlib_wm_delete_window;
318 #elif defined(VK_USE_PLATFORM_XCB_KHR)
319 Display *display;
320 xcb_connection_t *connection;
321 xcb_screen_t *screen;
322 xcb_window_t xcb_window;
323 xcb_intern_atom_reply_t *atom_wm_delete_window;
324 #elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
325 struct wl_display *display;
326 struct wl_registry *registry;
327 struct wl_compositor *compositor;
328 struct wl_surface *window;
329 struct xdg_wm_base *xdg_wm_base;
330 struct zxdg_decoration_manager_v1 *xdg_decoration_mgr;
331 struct zxdg_toplevel_decoration_v1 *toplevel_decoration;
332 struct xdg_surface *xdg_surface;
333 int xdg_surface_has_been_configured;
334 struct xdg_toplevel *xdg_toplevel;
335 struct wl_seat *seat;
336 struct wl_pointer *pointer;
337 struct wl_keyboard *keyboard;
338 #elif defined(VK_USE_PLATFORM_DIRECTFB_EXT)
339 IDirectFB *dfb;
340 IDirectFBSurface *window;
341 IDirectFBEventBuffer *event_buffer;
342 #elif defined(VK_USE_PLATFORM_ANDROID_KHR)
343 struct ANativeWindow *window;
344 #elif defined(VK_USE_PLATFORM_METAL_EXT)
345 void *caMetalLayer;
346 #endif
347 VkSurfaceKHR surface;
348 bool prepared;
349 bool use_staging_buffer;
350 bool separate_present_queue;
351 bool is_minimized;
352 int32_t gpu_number;
353
354 bool VK_KHR_incremental_present_enabled;
355
356 bool VK_GOOGLE_display_timing_enabled;
357 bool syncd_with_actual_presents;
358 uint64_t refresh_duration;
359 uint64_t refresh_duration_multiplier;
360 uint64_t target_IPD; // image present duration (inverse of frame rate)
361 uint64_t prev_desired_present_time;
362 uint32_t next_present_id;
363 uint32_t last_early_id; // 0 if no early images
364 uint32_t last_late_id; // 0 if no late images
365
366 VkInstance inst;
367 VkPhysicalDevice gpu;
368 VkDevice device;
369 VkQueue graphics_queue;
370 VkQueue present_queue;
371 uint32_t graphics_queue_family_index;
372 uint32_t present_queue_family_index;
373 VkSemaphore image_acquired_semaphores[FRAME_LAG];
374 VkSemaphore draw_complete_semaphores[FRAME_LAG];
375 VkSemaphore image_ownership_semaphores[FRAME_LAG];
376 VkPhysicalDeviceProperties gpu_props;
377 VkQueueFamilyProperties *queue_props;
378 VkPhysicalDeviceMemoryProperties memory_properties;
379
380 uint32_t enabled_extension_count;
381 uint32_t enabled_layer_count;
382 char *extension_names[64];
383 char *enabled_layers[64];
384
385 int width, height;
386 VkFormat format;
387 VkColorSpaceKHR color_space;
388
389 PFN_vkGetPhysicalDeviceSurfaceSupportKHR fpGetPhysicalDeviceSurfaceSupportKHR;
390 PFN_vkGetPhysicalDeviceSurfaceCapabilitiesKHR fpGetPhysicalDeviceSurfaceCapabilitiesKHR;
391 PFN_vkGetPhysicalDeviceSurfaceFormatsKHR fpGetPhysicalDeviceSurfaceFormatsKHR;
392 PFN_vkGetPhysicalDeviceSurfacePresentModesKHR fpGetPhysicalDeviceSurfacePresentModesKHR;
393 PFN_vkCreateSwapchainKHR fpCreateSwapchainKHR;
394 PFN_vkDestroySwapchainKHR fpDestroySwapchainKHR;
395 PFN_vkGetSwapchainImagesKHR fpGetSwapchainImagesKHR;
396 PFN_vkAcquireNextImageKHR fpAcquireNextImageKHR;
397 PFN_vkQueuePresentKHR fpQueuePresentKHR;
398 PFN_vkGetRefreshCycleDurationGOOGLE fpGetRefreshCycleDurationGOOGLE;
399 PFN_vkGetPastPresentationTimingGOOGLE fpGetPastPresentationTimingGOOGLE;
400 uint32_t swapchainImageCount;
401 VkSwapchainKHR swapchain;
402 SwapchainImageResources *swapchain_image_resources;
403 VkPresentModeKHR presentMode;
404 VkFence fences[FRAME_LAG];
405 int frame_index;
406
407 VkCommandPool cmd_pool;
408 VkCommandPool present_cmd_pool;
409
410 struct {
411 VkFormat format;
412
413 VkImage image;
414 VkMemoryAllocateInfo mem_alloc;
415 VkDeviceMemory mem;
416 VkImageView view;
417 } depth;
418
419 struct texture_object textures[DEMO_TEXTURE_COUNT];
420 struct texture_object staging_texture;
421
422 VkCommandBuffer cmd; // Buffer for initialization commands
423 VkPipelineLayout pipeline_layout;
424 VkDescriptorSetLayout desc_layout;
425 VkPipelineCache pipelineCache;
426 VkRenderPass render_pass;
427 VkPipeline pipeline;
428
429 mat4x4 projection_matrix;
430 mat4x4 view_matrix;
431 mat4x4 model_matrix;
432
433 float spin_angle;
434 float spin_increment;
435 bool pause;
436
437 VkShaderModule vert_shader_module;
438 VkShaderModule frag_shader_module;
439
440 VkDescriptorPool desc_pool;
441
442 bool quit;
443 int32_t curFrame;
444 int32_t frameCount;
445 bool validate;
446 bool validate_checks_disabled;
447 bool use_break;
448 bool suppress_popups;
449 bool force_errors;
450
451 PFN_vkCreateDebugUtilsMessengerEXT CreateDebugUtilsMessengerEXT;
452 PFN_vkDestroyDebugUtilsMessengerEXT DestroyDebugUtilsMessengerEXT;
453 PFN_vkSubmitDebugUtilsMessageEXT SubmitDebugUtilsMessageEXT;
454 PFN_vkCmdBeginDebugUtilsLabelEXT CmdBeginDebugUtilsLabelEXT;
455 PFN_vkCmdEndDebugUtilsLabelEXT CmdEndDebugUtilsLabelEXT;
456 PFN_vkCmdInsertDebugUtilsLabelEXT CmdInsertDebugUtilsLabelEXT;
457 PFN_vkSetDebugUtilsObjectNameEXT SetDebugUtilsObjectNameEXT;
458 VkDebugUtilsMessengerEXT dbg_messenger;
459
460 uint32_t current_buffer;
461 uint32_t queue_family_count;
462 };
463
debug_messenger_callback(VkDebugUtilsMessageSeverityFlagBitsEXT messageSeverity,VkDebugUtilsMessageTypeFlagsEXT messageType,const VkDebugUtilsMessengerCallbackDataEXT * pCallbackData,void * pUserData)464 VKAPI_ATTR VkBool32 VKAPI_CALL debug_messenger_callback(VkDebugUtilsMessageSeverityFlagBitsEXT messageSeverity,
465 VkDebugUtilsMessageTypeFlagsEXT messageType,
466 const VkDebugUtilsMessengerCallbackDataEXT *pCallbackData,
467 void *pUserData) {
468 char prefix[64] = "";
469 char *message = (char *)malloc(strlen(pCallbackData->pMessage) + 5000);
470 assert(message);
471 struct demo *demo = (struct demo *)pUserData;
472
473 if (demo->use_break) {
474 #ifndef WIN32
475 raise(SIGTRAP);
476 #else
477 DebugBreak();
478 #endif
479 }
480
481 if (messageSeverity & VK_DEBUG_UTILS_MESSAGE_SEVERITY_VERBOSE_BIT_EXT) {
482 strcat(prefix, "VERBOSE : ");
483 } else if (messageSeverity & VK_DEBUG_UTILS_MESSAGE_SEVERITY_INFO_BIT_EXT) {
484 strcat(prefix, "INFO : ");
485 } else if (messageSeverity & VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT) {
486 strcat(prefix, "WARNING : ");
487 } else if (messageSeverity & VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT) {
488 strcat(prefix, "ERROR : ");
489 }
490
491 if (messageType & VK_DEBUG_UTILS_MESSAGE_TYPE_GENERAL_BIT_EXT) {
492 strcat(prefix, "GENERAL");
493 } else {
494 if (messageType & VK_DEBUG_UTILS_MESSAGE_TYPE_VALIDATION_BIT_EXT) {
495 strcat(prefix, "VALIDATION");
496 validation_error = 1;
497 }
498 if (messageType & VK_DEBUG_UTILS_MESSAGE_TYPE_PERFORMANCE_BIT_EXT) {
499 if (messageType & VK_DEBUG_UTILS_MESSAGE_TYPE_VALIDATION_BIT_EXT) {
500 strcat(prefix, "|");
501 }
502 strcat(prefix, "PERFORMANCE");
503 }
504 }
505
506 sprintf(message, "%s - Message Id Number: %d | Message Id Name: %s\n\t%s\n", prefix, pCallbackData->messageIdNumber,
507 pCallbackData->pMessageIdName, pCallbackData->pMessage);
508 if (pCallbackData->objectCount > 0) {
509 char tmp_message[500];
510 sprintf(tmp_message, "\n\tObjects - %d\n", pCallbackData->objectCount);
511 strcat(message, tmp_message);
512 for (uint32_t object = 0; object < pCallbackData->objectCount; ++object) {
513 if (NULL != pCallbackData->pObjects[object].pObjectName && strlen(pCallbackData->pObjects[object].pObjectName) > 0) {
514 sprintf(tmp_message, "\t\tObject[%d] - %s, Handle %p, Name \"%s\"\n", object,
515 string_VkObjectType(pCallbackData->pObjects[object].objectType),
516 (void *)(pCallbackData->pObjects[object].objectHandle), pCallbackData->pObjects[object].pObjectName);
517 } else {
518 sprintf(tmp_message, "\t\tObject[%d] - %s, Handle %p\n", object,
519 string_VkObjectType(pCallbackData->pObjects[object].objectType),
520 (void *)(pCallbackData->pObjects[object].objectHandle));
521 }
522 strcat(message, tmp_message);
523 }
524 }
525 if (pCallbackData->cmdBufLabelCount > 0) {
526 char tmp_message[500];
527 sprintf(tmp_message, "\n\tCommand Buffer Labels - %d\n", pCallbackData->cmdBufLabelCount);
528 strcat(message, tmp_message);
529 for (uint32_t cmd_buf_label = 0; cmd_buf_label < pCallbackData->cmdBufLabelCount; ++cmd_buf_label) {
530 sprintf(tmp_message, "\t\tLabel[%d] - %s { %f, %f, %f, %f}\n", cmd_buf_label,
531 pCallbackData->pCmdBufLabels[cmd_buf_label].pLabelName, pCallbackData->pCmdBufLabels[cmd_buf_label].color[0],
532 pCallbackData->pCmdBufLabels[cmd_buf_label].color[1], pCallbackData->pCmdBufLabels[cmd_buf_label].color[2],
533 pCallbackData->pCmdBufLabels[cmd_buf_label].color[3]);
534 strcat(message, tmp_message);
535 }
536 }
537
538 #ifdef _WIN32
539
540 in_callback = true;
541 if (!demo->suppress_popups) MessageBox(NULL, message, "Alert", MB_OK);
542 in_callback = false;
543
544 #elif defined(ANDROID)
545
546 if (messageSeverity & VK_DEBUG_UTILS_MESSAGE_SEVERITY_INFO_BIT_EXT) {
547 __android_log_print(ANDROID_LOG_INFO, APP_SHORT_NAME, "%s", message);
548 } else if (messageSeverity & VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT) {
549 __android_log_print(ANDROID_LOG_WARN, APP_SHORT_NAME, "%s", message);
550 } else if (messageSeverity & VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT) {
551 __android_log_print(ANDROID_LOG_ERROR, APP_SHORT_NAME, "%s", message);
552 } else if (messageSeverity & VK_DEBUG_UTILS_MESSAGE_SEVERITY_VERBOSE_BIT_EXT) {
553 __android_log_print(ANDROID_LOG_VERBOSE, APP_SHORT_NAME, "%s", message);
554 } else {
555 __android_log_print(ANDROID_LOG_INFO, APP_SHORT_NAME, "%s", message);
556 }
557
558 #else
559
560 printf("%s\n", message);
561 fflush(stdout);
562
563 #endif
564
565 free(message);
566
567 // Don't bail out, but keep going.
568 return false;
569 }
570
ActualTimeLate(uint64_t desired,uint64_t actual,uint64_t rdur)571 bool ActualTimeLate(uint64_t desired, uint64_t actual, uint64_t rdur) {
572 // The desired time was the earliest time that the present should have
573 // occured. In almost every case, the actual time should be later than the
574 // desired time. We should only consider the actual time "late" if it is
575 // after "desired + rdur".
576 if (actual <= desired) {
577 // The actual time was before or equal to the desired time. This will
578 // probably never happen, but in case it does, return false since the
579 // present was obviously NOT late.
580 return false;
581 }
582 uint64_t deadline = desired + rdur;
583 if (actual > deadline) {
584 return true;
585 } else {
586 return false;
587 }
588 }
CanPresentEarlier(uint64_t earliest,uint64_t actual,uint64_t margin,uint64_t rdur)589 bool CanPresentEarlier(uint64_t earliest, uint64_t actual, uint64_t margin, uint64_t rdur) {
590 if (earliest < actual) {
591 // Consider whether this present could have occured earlier. Make sure
592 // that earliest time was at least 2msec earlier than actual time, and
593 // that the margin was at least 2msec:
594 uint64_t diff = actual - earliest;
595 if ((diff >= (2 * MILLION)) && (margin >= (2 * MILLION))) {
596 // This present could have occured earlier because both: 1) the
597 // earliest time was at least 2 msec before actual time, and 2) the
598 // margin was at least 2msec.
599 return true;
600 }
601 }
602 return false;
603 }
604
605 // Forward declarations:
606 static void demo_resize(struct demo *demo);
607 static void demo_create_surface(struct demo *demo);
608
memory_type_from_properties(struct demo * demo,uint32_t typeBits,VkFlags requirements_mask,uint32_t * typeIndex)609 static bool memory_type_from_properties(struct demo *demo, uint32_t typeBits, VkFlags requirements_mask, uint32_t *typeIndex) {
610 // Search memtypes to find first index with those properties
611 for (uint32_t i = 0; i < VK_MAX_MEMORY_TYPES; i++) {
612 if ((typeBits & 1) == 1) {
613 // Type is available, does it match user properties?
614 if ((demo->memory_properties.memoryTypes[i].propertyFlags & requirements_mask) == requirements_mask) {
615 *typeIndex = i;
616 return true;
617 }
618 }
619 typeBits >>= 1;
620 }
621 // No memory types matched, return failure
622 return false;
623 }
624
demo_flush_init_cmd(struct demo * demo)625 static void demo_flush_init_cmd(struct demo *demo) {
626 VkResult U_ASSERT_ONLY err;
627
628 // This function could get called twice if the texture uses a staging buffer
629 // In that case the second call should be ignored
630 if (demo->cmd == VK_NULL_HANDLE) return;
631
632 err = vkEndCommandBuffer(demo->cmd);
633 assert(!err);
634
635 VkFence fence;
636 VkFenceCreateInfo fence_ci = {.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO, .pNext = NULL, .flags = 0};
637 if (demo->force_errors) {
638 // Remove sType to intentionally force validation layer errors.
639 fence_ci.sType = 0;
640 }
641 err = vkCreateFence(demo->device, &fence_ci, NULL, &fence);
642 assert(!err);
643
644 const VkCommandBuffer cmd_bufs[] = {demo->cmd};
645 VkSubmitInfo submit_info = {.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO,
646 .pNext = NULL,
647 .waitSemaphoreCount = 0,
648 .pWaitSemaphores = NULL,
649 .pWaitDstStageMask = NULL,
650 .commandBufferCount = 1,
651 .pCommandBuffers = cmd_bufs,
652 .signalSemaphoreCount = 0,
653 .pSignalSemaphores = NULL};
654
655 err = vkQueueSubmit(demo->graphics_queue, 1, &submit_info, fence);
656 assert(!err);
657
658 err = vkWaitForFences(demo->device, 1, &fence, VK_TRUE, UINT64_MAX);
659 assert(!err);
660
661 vkFreeCommandBuffers(demo->device, demo->cmd_pool, 1, cmd_bufs);
662 vkDestroyFence(demo->device, fence, NULL);
663 demo->cmd = VK_NULL_HANDLE;
664 }
665
demo_set_image_layout(struct demo * demo,VkImage image,VkImageAspectFlags aspectMask,VkImageLayout old_image_layout,VkImageLayout new_image_layout,VkAccessFlagBits srcAccessMask,VkPipelineStageFlags src_stages,VkPipelineStageFlags dest_stages)666 static void demo_set_image_layout(struct demo *demo, VkImage image, VkImageAspectFlags aspectMask, VkImageLayout old_image_layout,
667 VkImageLayout new_image_layout, VkAccessFlagBits srcAccessMask, VkPipelineStageFlags src_stages,
668 VkPipelineStageFlags dest_stages) {
669 assert(demo->cmd);
670
671 VkImageMemoryBarrier image_memory_barrier = {.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
672 .pNext = NULL,
673 .srcAccessMask = srcAccessMask,
674 .dstAccessMask = 0,
675 .srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
676 .dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
677 .oldLayout = old_image_layout,
678 .newLayout = new_image_layout,
679 .image = image,
680 .subresourceRange = {aspectMask, 0, 1, 0, 1}};
681
682 switch (new_image_layout) {
683 case VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL:
684 /* Make sure anything that was copying from this image has completed */
685 image_memory_barrier.dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
686 break;
687
688 case VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL:
689 image_memory_barrier.dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
690 break;
691
692 case VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL:
693 image_memory_barrier.dstAccessMask = VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT;
694 break;
695
696 case VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL:
697 image_memory_barrier.dstAccessMask = VK_ACCESS_SHADER_READ_BIT | VK_ACCESS_INPUT_ATTACHMENT_READ_BIT;
698 break;
699
700 case VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL:
701 image_memory_barrier.dstAccessMask = VK_ACCESS_TRANSFER_READ_BIT;
702 break;
703
704 case VK_IMAGE_LAYOUT_PRESENT_SRC_KHR:
705 image_memory_barrier.dstAccessMask = VK_ACCESS_MEMORY_READ_BIT;
706 break;
707
708 default:
709 image_memory_barrier.dstAccessMask = 0;
710 break;
711 }
712
713 VkImageMemoryBarrier *pmemory_barrier = &image_memory_barrier;
714
715 vkCmdPipelineBarrier(demo->cmd, src_stages, dest_stages, 0, 0, NULL, 0, NULL, 1, pmemory_barrier);
716 }
717
demo_draw_build_cmd(struct demo * demo,VkCommandBuffer cmd_buf)718 static void demo_draw_build_cmd(struct demo *demo, VkCommandBuffer cmd_buf) {
719 VkDebugUtilsLabelEXT label;
720 memset(&label, 0, sizeof(label));
721 const VkCommandBufferBeginInfo cmd_buf_info = {
722 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
723 .pNext = NULL,
724 .flags = VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT,
725 .pInheritanceInfo = NULL,
726 };
727 const VkClearValue clear_values[2] = {
728 [0] = {.color.float32 = {0.2f, 0.2f, 0.2f, 0.2f}},
729 [1] = {.depthStencil = {1.0f, 0}},
730 };
731 const VkRenderPassBeginInfo rp_begin = {
732 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO,
733 .pNext = NULL,
734 .renderPass = demo->render_pass,
735 .framebuffer = demo->swapchain_image_resources[demo->current_buffer].framebuffer,
736 .renderArea.offset.x = 0,
737 .renderArea.offset.y = 0,
738 .renderArea.extent.width = demo->width,
739 .renderArea.extent.height = demo->height,
740 .clearValueCount = 2,
741 .pClearValues = clear_values,
742 };
743 VkResult U_ASSERT_ONLY err;
744
745 err = vkBeginCommandBuffer(cmd_buf, &cmd_buf_info);
746
747 if (demo->validate) {
748 // Set a name for the command buffer
749 VkDebugUtilsObjectNameInfoEXT cmd_buf_name = {
750 .sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_OBJECT_NAME_INFO_EXT,
751 .pNext = NULL,
752 .objectType = VK_OBJECT_TYPE_COMMAND_BUFFER,
753 .objectHandle = (uint64_t)cmd_buf,
754 .pObjectName = "CubeDrawCommandBuf",
755 };
756 demo->SetDebugUtilsObjectNameEXT(demo->device, &cmd_buf_name);
757
758 label.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_LABEL_EXT;
759 label.pNext = NULL;
760 label.pLabelName = "DrawBegin";
761 label.color[0] = 0.4f;
762 label.color[1] = 0.3f;
763 label.color[2] = 0.2f;
764 label.color[3] = 0.1f;
765 demo->CmdBeginDebugUtilsLabelEXT(cmd_buf, &label);
766 }
767
768 assert(!err);
769 vkCmdBeginRenderPass(cmd_buf, &rp_begin, VK_SUBPASS_CONTENTS_INLINE);
770
771 if (demo->validate) {
772 label.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_LABEL_EXT;
773 label.pNext = NULL;
774 label.pLabelName = "InsideRenderPass";
775 label.color[0] = 8.4f;
776 label.color[1] = 7.3f;
777 label.color[2] = 6.2f;
778 label.color[3] = 7.1f;
779 demo->CmdBeginDebugUtilsLabelEXT(cmd_buf, &label);
780 }
781
782 vkCmdBindPipeline(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS, demo->pipeline);
783 vkCmdBindDescriptorSets(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS, demo->pipeline_layout, 0, 1,
784 &demo->swapchain_image_resources[demo->current_buffer].descriptor_set, 0, NULL);
785 VkViewport viewport;
786 memset(&viewport, 0, sizeof(viewport));
787 float viewport_dimension;
788 if (demo->width < demo->height) {
789 viewport_dimension = (float)demo->width;
790 viewport.y = (demo->height - demo->width) / 2.0f;
791 } else {
792 viewport_dimension = (float)demo->height;
793 viewport.x = (demo->width - demo->height) / 2.0f;
794 }
795 viewport.height = viewport_dimension;
796 viewport.width = viewport_dimension;
797 viewport.minDepth = (float)0.0f;
798 viewport.maxDepth = (float)1.0f;
799 vkCmdSetViewport(cmd_buf, 0, 1, &viewport);
800
801 VkRect2D scissor;
802 memset(&scissor, 0, sizeof(scissor));
803 scissor.extent.width = demo->width;
804 scissor.extent.height = demo->height;
805 scissor.offset.x = 0;
806 scissor.offset.y = 0;
807 vkCmdSetScissor(cmd_buf, 0, 1, &scissor);
808
809 if (demo->validate) {
810 label.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_LABEL_EXT;
811 label.pNext = NULL;
812 label.pLabelName = "ActualDraw";
813 label.color[0] = -0.4f;
814 label.color[1] = -0.3f;
815 label.color[2] = -0.2f;
816 label.color[3] = -0.1f;
817 demo->CmdBeginDebugUtilsLabelEXT(cmd_buf, &label);
818 }
819
820 vkCmdDraw(cmd_buf, 12 * 3, 1, 0, 0);
821 if (demo->validate) {
822 demo->CmdEndDebugUtilsLabelEXT(cmd_buf);
823 }
824
825 // Note that ending the renderpass changes the image's layout from
826 // COLOR_ATTACHMENT_OPTIMAL to PRESENT_SRC_KHR
827 vkCmdEndRenderPass(cmd_buf);
828 if (demo->validate) {
829 demo->CmdEndDebugUtilsLabelEXT(cmd_buf);
830 }
831
832 if (demo->separate_present_queue) {
833 // We have to transfer ownership from the graphics queue family to the
834 // present queue family to be able to present. Note that we don't have
835 // to transfer from present queue family back to graphics queue family at
836 // the start of the next frame because we don't care about the image's
837 // contents at that point.
838 VkImageMemoryBarrier image_ownership_barrier = {.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
839 .pNext = NULL,
840 .srcAccessMask = 0,
841 .dstAccessMask = 0,
842 .oldLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
843 .newLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
844 .srcQueueFamilyIndex = demo->graphics_queue_family_index,
845 .dstQueueFamilyIndex = demo->present_queue_family_index,
846 .image = demo->swapchain_image_resources[demo->current_buffer].image,
847 .subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1}};
848
849 vkCmdPipelineBarrier(cmd_buf, VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT, VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT, 0, 0, NULL, 0,
850 NULL, 1, &image_ownership_barrier);
851 }
852 if (demo->validate) {
853 demo->CmdEndDebugUtilsLabelEXT(cmd_buf);
854 }
855 err = vkEndCommandBuffer(cmd_buf);
856 assert(!err);
857 }
858
demo_build_image_ownership_cmd(struct demo * demo,int i)859 void demo_build_image_ownership_cmd(struct demo *demo, int i) {
860 VkResult U_ASSERT_ONLY err;
861
862 const VkCommandBufferBeginInfo cmd_buf_info = {
863 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
864 .pNext = NULL,
865 .flags = VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT,
866 .pInheritanceInfo = NULL,
867 };
868 err = vkBeginCommandBuffer(demo->swapchain_image_resources[i].graphics_to_present_cmd, &cmd_buf_info);
869 assert(!err);
870
871 VkImageMemoryBarrier image_ownership_barrier = {.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
872 .pNext = NULL,
873 .srcAccessMask = 0,
874 .dstAccessMask = 0,
875 .oldLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
876 .newLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
877 .srcQueueFamilyIndex = demo->graphics_queue_family_index,
878 .dstQueueFamilyIndex = demo->present_queue_family_index,
879 .image = demo->swapchain_image_resources[i].image,
880 .subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1}};
881
882 vkCmdPipelineBarrier(demo->swapchain_image_resources[i].graphics_to_present_cmd, VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT,
883 VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT, 0, 0, NULL, 0, NULL, 1, &image_ownership_barrier);
884 err = vkEndCommandBuffer(demo->swapchain_image_resources[i].graphics_to_present_cmd);
885 assert(!err);
886 }
887
demo_update_data_buffer(struct demo * demo)888 void demo_update_data_buffer(struct demo *demo) {
889 mat4x4 MVP, Model, VP;
890 int matrixSize = sizeof(MVP);
891
892 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
893
894 // Rotate around the Y axis
895 mat4x4_dup(Model, demo->model_matrix);
896 mat4x4_rotate_Y(demo->model_matrix, Model, (float)degreesToRadians(demo->spin_angle));
897 mat4x4_orthonormalize(demo->model_matrix, demo->model_matrix);
898 mat4x4_mul(MVP, VP, demo->model_matrix);
899
900 memcpy(demo->swapchain_image_resources[demo->current_buffer].uniform_memory_ptr, (const void *)&MVP[0][0], matrixSize);
901 }
902
DemoUpdateTargetIPD(struct demo * demo)903 void DemoUpdateTargetIPD(struct demo *demo) {
904 // Look at what happened to previous presents, and make appropriate
905 // adjustments in timing:
906 VkResult U_ASSERT_ONLY err;
907 VkPastPresentationTimingGOOGLE *past = NULL;
908 uint32_t count = 0;
909
910 err = demo->fpGetPastPresentationTimingGOOGLE(demo->device, demo->swapchain, &count, NULL);
911 assert(!err);
912 if (count) {
913 past = (VkPastPresentationTimingGOOGLE *)malloc(sizeof(VkPastPresentationTimingGOOGLE) * count);
914 assert(past);
915 err = demo->fpGetPastPresentationTimingGOOGLE(demo->device, demo->swapchain, &count, past);
916 assert(!err);
917
918 bool early = false;
919 bool late = false;
920 bool calibrate_next = false;
921 for (uint32_t i = 0; i < count; i++) {
922 if (!demo->syncd_with_actual_presents) {
923 // This is the first time that we've received an
924 // actualPresentTime for this swapchain. In order to not
925 // perceive these early frames as "late", we need to sync-up
926 // our future desiredPresentTime's with the
927 // actualPresentTime(s) that we're receiving now.
928 calibrate_next = true;
929
930 // So that we don't suspect any pending presents as late,
931 // record them all as suspected-late presents:
932 demo->last_late_id = demo->next_present_id - 1;
933 demo->last_early_id = 0;
934 demo->syncd_with_actual_presents = true;
935 break;
936 } else if (CanPresentEarlier(past[i].earliestPresentTime, past[i].actualPresentTime, past[i].presentMargin,
937 demo->refresh_duration)) {
938 // This image could have been presented earlier. We don't want
939 // to decrease the target_IPD until we've seen early presents
940 // for at least two seconds.
941 if (demo->last_early_id == past[i].presentID) {
942 // We've now seen two seconds worth of early presents.
943 // Flag it as such, and reset the counter:
944 early = true;
945 demo->last_early_id = 0;
946 } else if (demo->last_early_id == 0) {
947 // This is the first early present we've seen.
948 // Calculate the presentID for two seconds from now.
949 uint64_t lastEarlyTime = past[i].actualPresentTime + (2 * BILLION);
950 uint32_t howManyPresents = (uint32_t)((lastEarlyTime - past[i].actualPresentTime) / demo->target_IPD);
951 demo->last_early_id = past[i].presentID + howManyPresents;
952 } else {
953 // We are in the midst of a set of early images,
954 // and so we won't do anything.
955 }
956 late = false;
957 demo->last_late_id = 0;
958 } else if (ActualTimeLate(past[i].desiredPresentTime, past[i].actualPresentTime, demo->refresh_duration)) {
959 // This image was presented after its desired time. Since
960 // there's a delay between calling vkQueuePresentKHR and when
961 // we get the timing data, several presents may have been late.
962 // Thus, we need to threat all of the outstanding presents as
963 // being likely late, so that we only increase the target_IPD
964 // once for all of those presents.
965 if ((demo->last_late_id == 0) || (demo->last_late_id < past[i].presentID)) {
966 late = true;
967 // Record the last suspected-late present:
968 demo->last_late_id = demo->next_present_id - 1;
969 } else {
970 // We are in the midst of a set of likely-late images,
971 // and so we won't do anything.
972 }
973 early = false;
974 demo->last_early_id = 0;
975 } else {
976 // Since this image was not presented early or late, reset
977 // any sets of early or late presentIDs:
978 early = false;
979 late = false;
980 calibrate_next = true;
981 demo->last_early_id = 0;
982 demo->last_late_id = 0;
983 }
984 }
985
986 if (early) {
987 // Since we've seen at least two-seconds worth of presnts that
988 // could have occured earlier than desired, let's decrease the
989 // target_IPD (i.e. increase the frame rate):
990 //
991 // TODO(ianelliott): Try to calculate a better target_IPD based
992 // on the most recently-seen present (this is overly-simplistic).
993 demo->refresh_duration_multiplier--;
994 if (demo->refresh_duration_multiplier == 0) {
995 // This should never happen, but in case it does, don't
996 // try to go faster.
997 demo->refresh_duration_multiplier = 1;
998 }
999 demo->target_IPD = demo->refresh_duration * demo->refresh_duration_multiplier;
1000 }
1001 if (late) {
1002 // Since we found a new instance of a late present, we want to
1003 // increase the target_IPD (i.e. decrease the frame rate):
1004 //
1005 // TODO(ianelliott): Try to calculate a better target_IPD based
1006 // on the most recently-seen present (this is overly-simplistic).
1007 demo->refresh_duration_multiplier++;
1008 demo->target_IPD = demo->refresh_duration * demo->refresh_duration_multiplier;
1009 }
1010
1011 if (calibrate_next) {
1012 int64_t multiple = demo->next_present_id - past[count - 1].presentID;
1013 demo->prev_desired_present_time = (past[count - 1].actualPresentTime + (multiple * demo->target_IPD));
1014 }
1015 free(past);
1016 }
1017 }
1018
demo_draw(struct demo * demo)1019 static void demo_draw(struct demo *demo) {
1020 VkResult U_ASSERT_ONLY err;
1021
1022 // Ensure no more than FRAME_LAG renderings are outstanding
1023 vkWaitForFences(demo->device, 1, &demo->fences[demo->frame_index], VK_TRUE, UINT64_MAX);
1024 vkResetFences(demo->device, 1, &demo->fences[demo->frame_index]);
1025
1026 do {
1027 // Get the index of the next available swapchain image:
1028 err =
1029 demo->fpAcquireNextImageKHR(demo->device, demo->swapchain, UINT64_MAX,
1030 demo->image_acquired_semaphores[demo->frame_index], VK_NULL_HANDLE, &demo->current_buffer);
1031
1032 if (err == VK_ERROR_OUT_OF_DATE_KHR) {
1033 // demo->swapchain is out of date (e.g. the window was resized) and
1034 // must be recreated:
1035 demo_resize(demo);
1036 } else if (err == VK_SUBOPTIMAL_KHR) {
1037 // demo->swapchain is not as optimal as it could be, but the platform's
1038 // presentation engine will still present the image correctly.
1039 break;
1040 } else if (err == VK_ERROR_SURFACE_LOST_KHR) {
1041 vkDestroySurfaceKHR(demo->inst, demo->surface, NULL);
1042 demo_create_surface(demo);
1043 demo_resize(demo);
1044 } else {
1045 assert(!err);
1046 }
1047 } while (err != VK_SUCCESS);
1048
1049 demo_update_data_buffer(demo);
1050
1051 if (demo->VK_GOOGLE_display_timing_enabled) {
1052 // Look at what happened to previous presents, and make appropriate
1053 // adjustments in timing:
1054 DemoUpdateTargetIPD(demo);
1055
1056 // Note: a real application would position its geometry to that it's in
1057 // the correct locatoin for when the next image is presented. It might
1058 // also wait, so that there's less latency between any input and when
1059 // the next image is rendered/presented. This demo program is so
1060 // simple that it doesn't do either of those.
1061 }
1062
1063 // Wait for the image acquired semaphore to be signaled to ensure
1064 // that the image won't be rendered to until the presentation
1065 // engine has fully released ownership to the application, and it is
1066 // okay to render to the image.
1067 VkPipelineStageFlags pipe_stage_flags;
1068 VkSubmitInfo submit_info;
1069 submit_info.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
1070 submit_info.pNext = NULL;
1071 pipe_stage_flags = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
1072 submit_info.pWaitDstStageMask = &pipe_stage_flags;
1073 submit_info.waitSemaphoreCount = 1;
1074 submit_info.pWaitSemaphores = &demo->image_acquired_semaphores[demo->frame_index];
1075 submit_info.commandBufferCount = 1;
1076 submit_info.pCommandBuffers = &demo->swapchain_image_resources[demo->current_buffer].cmd;
1077 submit_info.signalSemaphoreCount = 1;
1078 submit_info.pSignalSemaphores = &demo->draw_complete_semaphores[demo->frame_index];
1079 err = vkQueueSubmit(demo->graphics_queue, 1, &submit_info, demo->fences[demo->frame_index]);
1080 assert(!err);
1081
1082 if (demo->separate_present_queue) {
1083 // If we are using separate queues, change image ownership to the
1084 // present queue before presenting, waiting for the draw complete
1085 // semaphore and signalling the ownership released semaphore when finished
1086 VkFence nullFence = VK_NULL_HANDLE;
1087 pipe_stage_flags = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
1088 submit_info.waitSemaphoreCount = 1;
1089 submit_info.pWaitSemaphores = &demo->draw_complete_semaphores[demo->frame_index];
1090 submit_info.commandBufferCount = 1;
1091 submit_info.pCommandBuffers = &demo->swapchain_image_resources[demo->current_buffer].graphics_to_present_cmd;
1092 submit_info.signalSemaphoreCount = 1;
1093 submit_info.pSignalSemaphores = &demo->image_ownership_semaphores[demo->frame_index];
1094 err = vkQueueSubmit(demo->present_queue, 1, &submit_info, nullFence);
1095 assert(!err);
1096 }
1097
1098 // If we are using separate queues we have to wait for image ownership,
1099 // otherwise wait for draw complete
1100 VkPresentInfoKHR present = {
1101 .sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR,
1102 .pNext = NULL,
1103 .waitSemaphoreCount = 1,
1104 .pWaitSemaphores = (demo->separate_present_queue) ? &demo->image_ownership_semaphores[demo->frame_index]
1105 : &demo->draw_complete_semaphores[demo->frame_index],
1106 .swapchainCount = 1,
1107 .pSwapchains = &demo->swapchain,
1108 .pImageIndices = &demo->current_buffer,
1109 };
1110
1111 VkRectLayerKHR rect;
1112 VkPresentRegionKHR region;
1113 VkPresentRegionsKHR regions;
1114 if (demo->VK_KHR_incremental_present_enabled) {
1115 // If using VK_KHR_incremental_present, we provide a hint of the region
1116 // that contains changed content relative to the previously-presented
1117 // image. The implementation can use this hint in order to save
1118 // work/power (by only copying the region in the hint). The
1119 // implementation is free to ignore the hint though, and so we must
1120 // ensure that the entire image has the correctly-drawn content.
1121 uint32_t eighthOfWidth = demo->width / 8;
1122 uint32_t eighthOfHeight = demo->height / 8;
1123
1124 rect.offset.x = eighthOfWidth;
1125 rect.offset.y = eighthOfHeight;
1126 rect.extent.width = eighthOfWidth * 6;
1127 rect.extent.height = eighthOfHeight * 6;
1128 rect.layer = 0;
1129
1130 region.rectangleCount = 1;
1131 region.pRectangles = ▭
1132
1133 regions.sType = VK_STRUCTURE_TYPE_PRESENT_REGIONS_KHR;
1134 regions.pNext = present.pNext;
1135 regions.swapchainCount = present.swapchainCount;
1136 regions.pRegions = ®ion;
1137 present.pNext = ®ions;
1138 }
1139
1140 if (demo->VK_GOOGLE_display_timing_enabled) {
1141 VkPresentTimeGOOGLE ptime;
1142 if (demo->prev_desired_present_time == 0) {
1143 // This must be the first present for this swapchain.
1144 //
1145 // We don't know where we are relative to the presentation engine's
1146 // display's refresh cycle. We also don't know how long rendering
1147 // takes. Let's make a grossly-simplified assumption that the
1148 // desiredPresentTime should be half way between now and
1149 // now+target_IPD. We will adjust over time.
1150 uint64_t curtime = getTimeInNanoseconds();
1151 if (curtime == 0) {
1152 // Since we didn't find out the current time, don't give a
1153 // desiredPresentTime:
1154 ptime.desiredPresentTime = 0;
1155 } else {
1156 ptime.desiredPresentTime = curtime + (demo->target_IPD >> 1);
1157 }
1158 } else {
1159 ptime.desiredPresentTime = (demo->prev_desired_present_time + demo->target_IPD);
1160 }
1161 ptime.presentID = demo->next_present_id++;
1162 demo->prev_desired_present_time = ptime.desiredPresentTime;
1163
1164 VkPresentTimesInfoGOOGLE present_time = {
1165 .sType = VK_STRUCTURE_TYPE_PRESENT_TIMES_INFO_GOOGLE,
1166 .pNext = present.pNext,
1167 .swapchainCount = present.swapchainCount,
1168 .pTimes = &ptime,
1169 };
1170 if (demo->VK_GOOGLE_display_timing_enabled) {
1171 present.pNext = &present_time;
1172 }
1173 }
1174
1175 err = demo->fpQueuePresentKHR(demo->present_queue, &present);
1176 demo->frame_index += 1;
1177 demo->frame_index %= FRAME_LAG;
1178
1179 if (err == VK_ERROR_OUT_OF_DATE_KHR) {
1180 // demo->swapchain is out of date (e.g. the window was resized) and
1181 // must be recreated:
1182 demo_resize(demo);
1183 } else if (err == VK_SUBOPTIMAL_KHR) {
1184 // SUBOPTIMAL could be due to a resize
1185 VkSurfaceCapabilitiesKHR surfCapabilities;
1186 err = demo->fpGetPhysicalDeviceSurfaceCapabilitiesKHR(demo->gpu, demo->surface, &surfCapabilities);
1187 assert(!err);
1188 if (surfCapabilities.currentExtent.width != (uint32_t)demo->width ||
1189 surfCapabilities.currentExtent.height != (uint32_t)demo->height) {
1190 demo_resize(demo);
1191 }
1192 } else if (err == VK_ERROR_SURFACE_LOST_KHR) {
1193 vkDestroySurfaceKHR(demo->inst, demo->surface, NULL);
1194 demo_create_surface(demo);
1195 demo_resize(demo);
1196 } else {
1197 assert(!err);
1198 }
1199 }
1200
demo_prepare_buffers(struct demo * demo)1201 static void demo_prepare_buffers(struct demo *demo) {
1202 VkResult U_ASSERT_ONLY err;
1203 VkSwapchainKHR oldSwapchain = demo->swapchain;
1204
1205 // Check the surface capabilities and formats
1206 VkSurfaceCapabilitiesKHR surfCapabilities;
1207 err = demo->fpGetPhysicalDeviceSurfaceCapabilitiesKHR(demo->gpu, demo->surface, &surfCapabilities);
1208 assert(!err);
1209
1210 uint32_t presentModeCount;
1211 err = demo->fpGetPhysicalDeviceSurfacePresentModesKHR(demo->gpu, demo->surface, &presentModeCount, NULL);
1212 assert(!err);
1213 VkPresentModeKHR *presentModes = (VkPresentModeKHR *)malloc(presentModeCount * sizeof(VkPresentModeKHR));
1214 assert(presentModes);
1215 err = demo->fpGetPhysicalDeviceSurfacePresentModesKHR(demo->gpu, demo->surface, &presentModeCount, presentModes);
1216 assert(!err);
1217
1218 VkExtent2D swapchainExtent;
1219 // width and height are either both 0xFFFFFFFF, or both not 0xFFFFFFFF.
1220 if (surfCapabilities.currentExtent.width == 0xFFFFFFFF) {
1221 // If the surface size is undefined, the size is set to the size
1222 // of the images requested, which must fit within the minimum and
1223 // maximum values.
1224 swapchainExtent.width = demo->width;
1225 swapchainExtent.height = demo->height;
1226
1227 if (swapchainExtent.width < surfCapabilities.minImageExtent.width) {
1228 swapchainExtent.width = surfCapabilities.minImageExtent.width;
1229 } else if (swapchainExtent.width > surfCapabilities.maxImageExtent.width) {
1230 swapchainExtent.width = surfCapabilities.maxImageExtent.width;
1231 }
1232
1233 if (swapchainExtent.height < surfCapabilities.minImageExtent.height) {
1234 swapchainExtent.height = surfCapabilities.minImageExtent.height;
1235 } else if (swapchainExtent.height > surfCapabilities.maxImageExtent.height) {
1236 swapchainExtent.height = surfCapabilities.maxImageExtent.height;
1237 }
1238 } else {
1239 // If the surface size is defined, the swap chain size must match
1240 swapchainExtent = surfCapabilities.currentExtent;
1241 demo->width = surfCapabilities.currentExtent.width;
1242 demo->height = surfCapabilities.currentExtent.height;
1243 }
1244
1245 if (surfCapabilities.maxImageExtent.width == 0 || surfCapabilities.maxImageExtent.height == 0) {
1246 demo->is_minimized = true;
1247 return;
1248 } else {
1249 demo->is_minimized = false;
1250 }
1251
1252 // The FIFO present mode is guaranteed by the spec to be supported
1253 // and to have no tearing. It's a great default present mode to use.
1254 VkPresentModeKHR swapchainPresentMode = VK_PRESENT_MODE_FIFO_KHR;
1255
1256 // There are times when you may wish to use another present mode. The
1257 // following code shows how to select them, and the comments provide some
1258 // reasons you may wish to use them.
1259 //
1260 // It should be noted that Vulkan 1.0 doesn't provide a method for
1261 // synchronizing rendering with the presentation engine's display. There
1262 // is a method provided for throttling rendering with the display, but
1263 // there are some presentation engines for which this method will not work.
1264 // If an application doesn't throttle its rendering, and if it renders much
1265 // faster than the refresh rate of the display, this can waste power on
1266 // mobile devices. That is because power is being spent rendering images
1267 // that may never be seen.
1268
1269 // VK_PRESENT_MODE_IMMEDIATE_KHR is for applications that don't care about
1270 // tearing, or have some way of synchronizing their rendering with the
1271 // display.
1272 // VK_PRESENT_MODE_MAILBOX_KHR may be useful for applications that
1273 // generally render a new presentable image every refresh cycle, but are
1274 // occasionally early. In this case, the application wants the new image
1275 // to be displayed instead of the previously-queued-for-presentation image
1276 // that has not yet been displayed.
1277 // VK_PRESENT_MODE_FIFO_RELAXED_KHR is for applications that generally
1278 // render a new presentable image every refresh cycle, but are occasionally
1279 // late. In this case (perhaps because of stuttering/latency concerns),
1280 // the application wants the late image to be immediately displayed, even
1281 // though that may mean some tearing.
1282
1283 if (demo->presentMode != swapchainPresentMode) {
1284 for (size_t i = 0; i < presentModeCount; ++i) {
1285 if (presentModes[i] == demo->presentMode) {
1286 swapchainPresentMode = demo->presentMode;
1287 break;
1288 }
1289 }
1290 }
1291 if (swapchainPresentMode != demo->presentMode) {
1292 ERR_EXIT("Present mode specified is not supported\n", "Present mode unsupported");
1293 }
1294
1295 // Determine the number of VkImages to use in the swap chain.
1296 // Application desires to acquire 3 images at a time for triple
1297 // buffering
1298 uint32_t desiredNumOfSwapchainImages = 3;
1299 if (desiredNumOfSwapchainImages < surfCapabilities.minImageCount) {
1300 desiredNumOfSwapchainImages = surfCapabilities.minImageCount;
1301 }
1302 // If maxImageCount is 0, we can ask for as many images as we want;
1303 // otherwise we're limited to maxImageCount
1304 if ((surfCapabilities.maxImageCount > 0) && (desiredNumOfSwapchainImages > surfCapabilities.maxImageCount)) {
1305 // Application must settle for fewer images than desired:
1306 desiredNumOfSwapchainImages = surfCapabilities.maxImageCount;
1307 }
1308
1309 VkSurfaceTransformFlagsKHR preTransform;
1310 if (surfCapabilities.supportedTransforms & VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR) {
1311 preTransform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
1312 } else {
1313 preTransform = surfCapabilities.currentTransform;
1314 }
1315
1316 // Find a supported composite alpha mode - one of these is guaranteed to be set
1317 VkCompositeAlphaFlagBitsKHR compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR;
1318 VkCompositeAlphaFlagBitsKHR compositeAlphaFlags[4] = {
1319 VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR,
1320 VK_COMPOSITE_ALPHA_PRE_MULTIPLIED_BIT_KHR,
1321 VK_COMPOSITE_ALPHA_POST_MULTIPLIED_BIT_KHR,
1322 VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR,
1323 };
1324 for (uint32_t i = 0; i < ARRAY_SIZE(compositeAlphaFlags); i++) {
1325 if (surfCapabilities.supportedCompositeAlpha & compositeAlphaFlags[i]) {
1326 compositeAlpha = compositeAlphaFlags[i];
1327 break;
1328 }
1329 }
1330
1331 VkSwapchainCreateInfoKHR swapchain_ci = {
1332 .sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR,
1333 .pNext = NULL,
1334 .surface = demo->surface,
1335 .minImageCount = desiredNumOfSwapchainImages,
1336 .imageFormat = demo->format,
1337 .imageColorSpace = demo->color_space,
1338 .imageExtent =
1339 {
1340 .width = swapchainExtent.width,
1341 .height = swapchainExtent.height,
1342 },
1343 .imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
1344 .preTransform = preTransform,
1345 .compositeAlpha = compositeAlpha,
1346 .imageArrayLayers = 1,
1347 .imageSharingMode = VK_SHARING_MODE_EXCLUSIVE,
1348 .queueFamilyIndexCount = 0,
1349 .pQueueFamilyIndices = NULL,
1350 .presentMode = swapchainPresentMode,
1351 .oldSwapchain = oldSwapchain,
1352 .clipped = true,
1353 };
1354 uint32_t i;
1355 err = demo->fpCreateSwapchainKHR(demo->device, &swapchain_ci, NULL, &demo->swapchain);
1356 assert(!err);
1357
1358 // If we just re-created an existing swapchain, we should destroy the old
1359 // swapchain at this point.
1360 // Note: destroying the swapchain also cleans up all its associated
1361 // presentable images once the platform is done with them.
1362 if (oldSwapchain != VK_NULL_HANDLE) {
1363 demo->fpDestroySwapchainKHR(demo->device, oldSwapchain, NULL);
1364 }
1365
1366 err = demo->fpGetSwapchainImagesKHR(demo->device, demo->swapchain, &demo->swapchainImageCount, NULL);
1367 assert(!err);
1368
1369 VkImage *swapchainImages = (VkImage *)malloc(demo->swapchainImageCount * sizeof(VkImage));
1370 assert(swapchainImages);
1371 err = demo->fpGetSwapchainImagesKHR(demo->device, demo->swapchain, &demo->swapchainImageCount, swapchainImages);
1372 assert(!err);
1373
1374 demo->swapchain_image_resources =
1375 (SwapchainImageResources *)malloc(sizeof(SwapchainImageResources) * demo->swapchainImageCount);
1376 assert(demo->swapchain_image_resources);
1377
1378 for (i = 0; i < demo->swapchainImageCount; i++) {
1379 VkImageViewCreateInfo color_image_view = {
1380 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
1381 .pNext = NULL,
1382 .format = demo->format,
1383 .components =
1384 {
1385 .r = VK_COMPONENT_SWIZZLE_IDENTITY,
1386 .g = VK_COMPONENT_SWIZZLE_IDENTITY,
1387 .b = VK_COMPONENT_SWIZZLE_IDENTITY,
1388 .a = VK_COMPONENT_SWIZZLE_IDENTITY,
1389 },
1390 .subresourceRange =
1391 {.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT, .baseMipLevel = 0, .levelCount = 1, .baseArrayLayer = 0, .layerCount = 1},
1392 .viewType = VK_IMAGE_VIEW_TYPE_2D,
1393 .flags = 0,
1394 };
1395
1396 demo->swapchain_image_resources[i].image = swapchainImages[i];
1397
1398 color_image_view.image = demo->swapchain_image_resources[i].image;
1399
1400 err = vkCreateImageView(demo->device, &color_image_view, NULL, &demo->swapchain_image_resources[i].view);
1401 assert(!err);
1402 }
1403
1404 if (demo->VK_GOOGLE_display_timing_enabled) {
1405 VkRefreshCycleDurationGOOGLE rc_dur;
1406 err = demo->fpGetRefreshCycleDurationGOOGLE(demo->device, demo->swapchain, &rc_dur);
1407 assert(!err);
1408 demo->refresh_duration = rc_dur.refreshDuration;
1409
1410 demo->syncd_with_actual_presents = false;
1411 // Initially target 1X the refresh duration:
1412 demo->target_IPD = demo->refresh_duration;
1413 demo->refresh_duration_multiplier = 1;
1414 demo->prev_desired_present_time = 0;
1415 demo->next_present_id = 1;
1416 }
1417
1418 if (NULL != swapchainImages) {
1419 free(swapchainImages);
1420 }
1421
1422 if (NULL != presentModes) {
1423 free(presentModes);
1424 }
1425 }
1426
demo_prepare_depth(struct demo * demo)1427 static void demo_prepare_depth(struct demo *demo) {
1428 const VkFormat depth_format = VK_FORMAT_D16_UNORM;
1429 const VkImageCreateInfo image = {
1430 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
1431 .pNext = NULL,
1432 .imageType = VK_IMAGE_TYPE_2D,
1433 .format = depth_format,
1434 .extent = {demo->width, demo->height, 1},
1435 .mipLevels = 1,
1436 .arrayLayers = 1,
1437 .samples = VK_SAMPLE_COUNT_1_BIT,
1438 .tiling = VK_IMAGE_TILING_OPTIMAL,
1439 .usage = VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT,
1440 .flags = 0,
1441 };
1442
1443 VkImageViewCreateInfo view = {
1444 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
1445 .pNext = NULL,
1446 .image = VK_NULL_HANDLE,
1447 .format = depth_format,
1448 .subresourceRange =
1449 {.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT, .baseMipLevel = 0, .levelCount = 1, .baseArrayLayer = 0, .layerCount = 1},
1450 .flags = 0,
1451 .viewType = VK_IMAGE_VIEW_TYPE_2D,
1452 };
1453
1454
1455 if (demo->force_errors) {
1456 // Intentionally force a bad pNext value to generate a validation layer error
1457 view.pNext = ℑ
1458 }
1459
1460 VkMemoryRequirements mem_reqs;
1461 VkResult U_ASSERT_ONLY err;
1462 bool U_ASSERT_ONLY pass;
1463
1464 demo->depth.format = depth_format;
1465
1466 /* create image */
1467 err = vkCreateImage(demo->device, &image, NULL, &demo->depth.image);
1468 assert(!err);
1469
1470 vkGetImageMemoryRequirements(demo->device, demo->depth.image, &mem_reqs);
1471 assert(!err);
1472
1473 demo->depth.mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
1474 demo->depth.mem_alloc.pNext = NULL;
1475 demo->depth.mem_alloc.allocationSize = mem_reqs.size;
1476 demo->depth.mem_alloc.memoryTypeIndex = 0;
1477
1478 pass = memory_type_from_properties(demo, mem_reqs.memoryTypeBits, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT,
1479 &demo->depth.mem_alloc.memoryTypeIndex);
1480 assert(pass);
1481
1482 /* allocate memory */
1483 err = vkAllocateMemory(demo->device, &demo->depth.mem_alloc, NULL, &demo->depth.mem);
1484 assert(!err);
1485
1486 /* bind memory */
1487 err = vkBindImageMemory(demo->device, demo->depth.image, demo->depth.mem, 0);
1488 assert(!err);
1489
1490 /* create image view */
1491 view.image = demo->depth.image;
1492 err = vkCreateImageView(demo->device, &view, NULL, &demo->depth.view);
1493 assert(!err);
1494 }
1495
1496 /* Convert ppm image data from header file into RGBA texture image */
1497 #include "lunarg.ppm.h"
loadTexture(const char * filename,uint8_t * rgba_data,VkSubresourceLayout * layout,int32_t * width,int32_t * height)1498 bool loadTexture(const char *filename, uint8_t *rgba_data, VkSubresourceLayout *layout, int32_t *width, int32_t *height) {
1499 (void)filename;
1500 char *cPtr;
1501 cPtr = (char *)lunarg_ppm;
1502 if ((unsigned char *)cPtr >= (lunarg_ppm + lunarg_ppm_len) || strncmp(cPtr, "P6\n", 3)) {
1503 return false;
1504 }
1505 while (strncmp(cPtr++, "\n", 1))
1506 ;
1507 sscanf(cPtr, "%u %u", width, height);
1508 if (rgba_data == NULL) {
1509 return true;
1510 }
1511 while (strncmp(cPtr++, "\n", 1))
1512 ;
1513 if ((unsigned char *)cPtr >= (lunarg_ppm + lunarg_ppm_len) || strncmp(cPtr, "255\n", 4)) {
1514 return false;
1515 }
1516 while (strncmp(cPtr++, "\n", 1))
1517 ;
1518 for (int y = 0; y < *height; y++) {
1519 uint8_t *rowPtr = rgba_data;
1520 for (int x = 0; x < *width; x++) {
1521 memcpy(rowPtr, cPtr, 3);
1522 rowPtr[3] = 255; /* Alpha of 1 */
1523 rowPtr += 4;
1524 cPtr += 3;
1525 }
1526 rgba_data += layout->rowPitch;
1527 }
1528 return true;
1529 }
1530
demo_prepare_texture_buffer(struct demo * demo,const char * filename,struct texture_object * tex_obj)1531 static void demo_prepare_texture_buffer(struct demo *demo, const char *filename, struct texture_object *tex_obj) {
1532 int32_t tex_width;
1533 int32_t tex_height;
1534 VkResult U_ASSERT_ONLY err;
1535 bool U_ASSERT_ONLY pass;
1536
1537 if (!loadTexture(filename, NULL, NULL, &tex_width, &tex_height)) {
1538 ERR_EXIT("Failed to load textures", "Load Texture Failure");
1539 }
1540
1541 tex_obj->tex_width = tex_width;
1542 tex_obj->tex_height = tex_height;
1543
1544 const VkBufferCreateInfo buffer_create_info = {.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,
1545 .pNext = NULL,
1546 .flags = 0,
1547 .size = tex_width * tex_height * 4,
1548 .usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT,
1549 .sharingMode = VK_SHARING_MODE_EXCLUSIVE,
1550 .queueFamilyIndexCount = 0,
1551 .pQueueFamilyIndices = NULL};
1552
1553 err = vkCreateBuffer(demo->device, &buffer_create_info, NULL, &tex_obj->buffer);
1554 assert(!err);
1555
1556 VkMemoryRequirements mem_reqs;
1557 vkGetBufferMemoryRequirements(demo->device, tex_obj->buffer, &mem_reqs);
1558
1559 tex_obj->mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
1560 tex_obj->mem_alloc.pNext = NULL;
1561 tex_obj->mem_alloc.allocationSize = mem_reqs.size;
1562 tex_obj->mem_alloc.memoryTypeIndex = 0;
1563
1564 VkFlags requirements = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT;
1565 pass = memory_type_from_properties(demo, mem_reqs.memoryTypeBits, requirements, &tex_obj->mem_alloc.memoryTypeIndex);
1566 assert(pass);
1567
1568 err = vkAllocateMemory(demo->device, &tex_obj->mem_alloc, NULL, &(tex_obj->mem));
1569 assert(!err);
1570
1571 /* bind memory */
1572 err = vkBindBufferMemory(demo->device, tex_obj->buffer, tex_obj->mem, 0);
1573 assert(!err);
1574
1575 VkSubresourceLayout layout;
1576 memset(&layout, 0, sizeof(layout));
1577 layout.rowPitch = tex_width * 4;
1578
1579 void *data;
1580 err = vkMapMemory(demo->device, tex_obj->mem, 0, tex_obj->mem_alloc.allocationSize, 0, &data);
1581 assert(!err);
1582
1583 if (!loadTexture(filename, data, &layout, &tex_width, &tex_height)) {
1584 fprintf(stderr, "Error loading texture: %s\n", filename);
1585 }
1586
1587 vkUnmapMemory(demo->device, tex_obj->mem);
1588 }
1589
demo_prepare_texture_image(struct demo * demo,const char * filename,struct texture_object * tex_obj,VkImageTiling tiling,VkImageUsageFlags usage,VkFlags required_props)1590 static void demo_prepare_texture_image(struct demo *demo, const char *filename, struct texture_object *tex_obj,
1591 VkImageTiling tiling, VkImageUsageFlags usage, VkFlags required_props) {
1592 const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM;
1593 int32_t tex_width;
1594 int32_t tex_height;
1595 VkResult U_ASSERT_ONLY err;
1596 bool U_ASSERT_ONLY pass;
1597
1598 if (!loadTexture(filename, NULL, NULL, &tex_width, &tex_height)) {
1599 ERR_EXIT("Failed to load textures", "Load Texture Failure");
1600 }
1601
1602 tex_obj->tex_width = tex_width;
1603 tex_obj->tex_height = tex_height;
1604
1605 const VkImageCreateInfo image_create_info = {
1606 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
1607 .pNext = NULL,
1608 .imageType = VK_IMAGE_TYPE_2D,
1609 .format = tex_format,
1610 .extent = {tex_width, tex_height, 1},
1611 .mipLevels = 1,
1612 .arrayLayers = 1,
1613 .samples = VK_SAMPLE_COUNT_1_BIT,
1614 .tiling = tiling,
1615 .usage = usage,
1616 .flags = 0,
1617 .initialLayout = VK_IMAGE_LAYOUT_PREINITIALIZED,
1618 };
1619
1620 VkMemoryRequirements mem_reqs;
1621
1622 err = vkCreateImage(demo->device, &image_create_info, NULL, &tex_obj->image);
1623 assert(!err);
1624
1625 vkGetImageMemoryRequirements(demo->device, tex_obj->image, &mem_reqs);
1626
1627 tex_obj->mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
1628 tex_obj->mem_alloc.pNext = NULL;
1629 tex_obj->mem_alloc.allocationSize = mem_reqs.size;
1630 tex_obj->mem_alloc.memoryTypeIndex = 0;
1631
1632 pass = memory_type_from_properties(demo, mem_reqs.memoryTypeBits, required_props, &tex_obj->mem_alloc.memoryTypeIndex);
1633 assert(pass);
1634
1635 /* allocate memory */
1636 err = vkAllocateMemory(demo->device, &tex_obj->mem_alloc, NULL, &(tex_obj->mem));
1637 assert(!err);
1638
1639 /* bind memory */
1640 err = vkBindImageMemory(demo->device, tex_obj->image, tex_obj->mem, 0);
1641 assert(!err);
1642
1643 if (required_props & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) {
1644 const VkImageSubresource subres = {
1645 .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
1646 .mipLevel = 0,
1647 .arrayLayer = 0,
1648 };
1649 VkSubresourceLayout layout;
1650 void *data;
1651
1652 vkGetImageSubresourceLayout(demo->device, tex_obj->image, &subres, &layout);
1653
1654 err = vkMapMemory(demo->device, tex_obj->mem, 0, tex_obj->mem_alloc.allocationSize, 0, &data);
1655 assert(!err);
1656
1657 if (!loadTexture(filename, data, &layout, &tex_width, &tex_height)) {
1658 fprintf(stderr, "Error loading texture: %s\n", filename);
1659 }
1660
1661 vkUnmapMemory(demo->device, tex_obj->mem);
1662 }
1663
1664 tex_obj->imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
1665 }
1666
demo_destroy_texture(struct demo * demo,struct texture_object * tex_objs)1667 static void demo_destroy_texture(struct demo *demo, struct texture_object *tex_objs) {
1668 /* clean up staging resources */
1669 vkFreeMemory(demo->device, tex_objs->mem, NULL);
1670 if (tex_objs->image) vkDestroyImage(demo->device, tex_objs->image, NULL);
1671 if (tex_objs->buffer) vkDestroyBuffer(demo->device, tex_objs->buffer, NULL);
1672 }
1673
demo_prepare_textures(struct demo * demo)1674 static void demo_prepare_textures(struct demo *demo) {
1675 const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM;
1676 VkFormatProperties props;
1677 uint32_t i;
1678
1679 vkGetPhysicalDeviceFormatProperties(demo->gpu, tex_format, &props);
1680
1681 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
1682 VkResult U_ASSERT_ONLY err;
1683
1684 if ((props.linearTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) && !demo->use_staging_buffer) {
1685 /* Device can texture using linear textures */
1686 demo_prepare_texture_image(demo, tex_files[i], &demo->textures[i], VK_IMAGE_TILING_LINEAR, VK_IMAGE_USAGE_SAMPLED_BIT,
1687 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);
1688 // Nothing in the pipeline needs to be complete to start, and don't allow fragment
1689 // shader to run until layout transition completes
1690 demo_set_image_layout(demo, demo->textures[i].image, VK_IMAGE_ASPECT_COLOR_BIT, VK_IMAGE_LAYOUT_PREINITIALIZED,
1691 demo->textures[i].imageLayout, 0, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT,
1692 VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT);
1693 demo->staging_texture.image = 0;
1694 } else if (props.optimalTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) {
1695 /* Must use staging buffer to copy linear texture to optimized */
1696
1697 memset(&demo->staging_texture, 0, sizeof(demo->staging_texture));
1698 demo_prepare_texture_buffer(demo, tex_files[i], &demo->staging_texture);
1699
1700 demo_prepare_texture_image(demo, tex_files[i], &demo->textures[i], VK_IMAGE_TILING_OPTIMAL,
1701 (VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT),
1702 VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
1703
1704 demo_set_image_layout(demo, demo->textures[i].image, VK_IMAGE_ASPECT_COLOR_BIT, VK_IMAGE_LAYOUT_PREINITIALIZED,
1705 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 0, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT,
1706 VK_PIPELINE_STAGE_TRANSFER_BIT);
1707
1708 VkBufferImageCopy copy_region = {
1709 .bufferOffset = 0,
1710 .bufferRowLength = demo->staging_texture.tex_width,
1711 .bufferImageHeight = demo->staging_texture.tex_height,
1712 .imageSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1},
1713 .imageOffset = {0, 0, 0},
1714 .imageExtent = {demo->staging_texture.tex_width, demo->staging_texture.tex_height, 1},
1715 };
1716
1717 vkCmdCopyBufferToImage(demo->cmd, demo->staging_texture.buffer, demo->textures[i].image,
1718 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, ©_region);
1719
1720 demo_set_image_layout(demo, demo->textures[i].image, VK_IMAGE_ASPECT_COLOR_BIT, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
1721 demo->textures[i].imageLayout, VK_ACCESS_TRANSFER_WRITE_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT,
1722 VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT);
1723
1724 } else {
1725 /* Can't support VK_FORMAT_R8G8B8A8_UNORM !? */
1726 assert(!"No support for R8G8B8A8_UNORM as texture image format");
1727 }
1728
1729 const VkSamplerCreateInfo sampler = {
1730 .sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO,
1731 .pNext = NULL,
1732 .magFilter = VK_FILTER_NEAREST,
1733 .minFilter = VK_FILTER_NEAREST,
1734 .mipmapMode = VK_SAMPLER_MIPMAP_MODE_NEAREST,
1735 .addressModeU = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
1736 .addressModeV = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
1737 .addressModeW = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
1738 .mipLodBias = 0.0f,
1739 .anisotropyEnable = VK_FALSE,
1740 .maxAnisotropy = 1,
1741 .compareOp = VK_COMPARE_OP_NEVER,
1742 .minLod = 0.0f,
1743 .maxLod = 0.0f,
1744 .borderColor = VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE,
1745 .unnormalizedCoordinates = VK_FALSE,
1746 };
1747
1748 VkImageViewCreateInfo view = {
1749 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
1750 .pNext = NULL,
1751 .image = VK_NULL_HANDLE,
1752 .viewType = VK_IMAGE_VIEW_TYPE_2D,
1753 .format = tex_format,
1754 .components =
1755 {
1756 VK_COMPONENT_SWIZZLE_IDENTITY,
1757 VK_COMPONENT_SWIZZLE_IDENTITY,
1758 VK_COMPONENT_SWIZZLE_IDENTITY,
1759 VK_COMPONENT_SWIZZLE_IDENTITY,
1760 },
1761 .subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1},
1762 .flags = 0,
1763 };
1764
1765 /* create sampler */
1766 err = vkCreateSampler(demo->device, &sampler, NULL, &demo->textures[i].sampler);
1767 assert(!err);
1768
1769 /* create image view */
1770 view.image = demo->textures[i].image;
1771 err = vkCreateImageView(demo->device, &view, NULL, &demo->textures[i].view);
1772 assert(!err);
1773 }
1774 }
1775
demo_prepare_cube_data_buffers(struct demo * demo)1776 void demo_prepare_cube_data_buffers(struct demo *demo) {
1777 VkBufferCreateInfo buf_info;
1778 VkMemoryRequirements mem_reqs;
1779 VkMemoryAllocateInfo mem_alloc;
1780 mat4x4 MVP, VP;
1781 VkResult U_ASSERT_ONLY err;
1782 bool U_ASSERT_ONLY pass;
1783 struct vktexcube_vs_uniform data;
1784
1785 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
1786 mat4x4_mul(MVP, VP, demo->model_matrix);
1787 memcpy(data.mvp, MVP, sizeof(MVP));
1788 // dumpMatrix("MVP", MVP);
1789
1790 for (unsigned int i = 0; i < 12 * 3; i++) {
1791 data.position[i][0] = g_vertex_buffer_data[i * 3];
1792 data.position[i][1] = g_vertex_buffer_data[i * 3 + 1];
1793 data.position[i][2] = g_vertex_buffer_data[i * 3 + 2];
1794 data.position[i][3] = 1.0f;
1795 data.attr[i][0] = g_uv_buffer_data[2 * i];
1796 data.attr[i][1] = g_uv_buffer_data[2 * i + 1];
1797 data.attr[i][2] = 0;
1798 data.attr[i][3] = 0;
1799 }
1800
1801 memset(&buf_info, 0, sizeof(buf_info));
1802 buf_info.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
1803 buf_info.usage = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT;
1804 buf_info.size = sizeof(data);
1805
1806 for (unsigned int i = 0; i < demo->swapchainImageCount; i++) {
1807 err = vkCreateBuffer(demo->device, &buf_info, NULL, &demo->swapchain_image_resources[i].uniform_buffer);
1808 assert(!err);
1809
1810 vkGetBufferMemoryRequirements(demo->device, demo->swapchain_image_resources[i].uniform_buffer, &mem_reqs);
1811
1812 mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
1813 mem_alloc.pNext = NULL;
1814 mem_alloc.allocationSize = mem_reqs.size;
1815 mem_alloc.memoryTypeIndex = 0;
1816
1817 pass = memory_type_from_properties(demo, mem_reqs.memoryTypeBits,
1818 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
1819 &mem_alloc.memoryTypeIndex);
1820 assert(pass);
1821
1822 err = vkAllocateMemory(demo->device, &mem_alloc, NULL, &demo->swapchain_image_resources[i].uniform_memory);
1823 assert(!err);
1824
1825 err = vkMapMemory(demo->device, demo->swapchain_image_resources[i].uniform_memory, 0, VK_WHOLE_SIZE, 0,
1826 &demo->swapchain_image_resources[i].uniform_memory_ptr);
1827 assert(!err);
1828
1829 memcpy(demo->swapchain_image_resources[i].uniform_memory_ptr, &data, sizeof data);
1830
1831 err = vkBindBufferMemory(demo->device, demo->swapchain_image_resources[i].uniform_buffer,
1832 demo->swapchain_image_resources[i].uniform_memory, 0);
1833 assert(!err);
1834 }
1835 }
1836
demo_prepare_descriptor_layout(struct demo * demo)1837 static void demo_prepare_descriptor_layout(struct demo *demo) {
1838 const VkDescriptorSetLayoutBinding layout_bindings[2] = {
1839 [0] =
1840 {
1841 .binding = 0,
1842 .descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
1843 .descriptorCount = 1,
1844 .stageFlags = VK_SHADER_STAGE_VERTEX_BIT,
1845 .pImmutableSamplers = NULL,
1846 },
1847 [1] =
1848 {
1849 .binding = 1,
1850 .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
1851 .descriptorCount = DEMO_TEXTURE_COUNT,
1852 .stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT,
1853 .pImmutableSamplers = NULL,
1854 },
1855 };
1856 const VkDescriptorSetLayoutCreateInfo descriptor_layout = {
1857 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
1858 .pNext = NULL,
1859 .bindingCount = 2,
1860 .pBindings = layout_bindings,
1861 };
1862 VkResult U_ASSERT_ONLY err;
1863
1864 err = vkCreateDescriptorSetLayout(demo->device, &descriptor_layout, NULL, &demo->desc_layout);
1865 assert(!err);
1866
1867 const VkPipelineLayoutCreateInfo pPipelineLayoutCreateInfo = {
1868 .sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
1869 .pNext = NULL,
1870 .setLayoutCount = 1,
1871 .pSetLayouts = &demo->desc_layout,
1872 };
1873
1874 err = vkCreatePipelineLayout(demo->device, &pPipelineLayoutCreateInfo, NULL, &demo->pipeline_layout);
1875 assert(!err);
1876 }
1877
demo_prepare_render_pass(struct demo * demo)1878 static void demo_prepare_render_pass(struct demo *demo) {
1879 // The initial layout for the color and depth attachments will be LAYOUT_UNDEFINED
1880 // because at the start of the renderpass, we don't care about their contents.
1881 // At the start of the subpass, the color attachment's layout will be transitioned
1882 // to LAYOUT_COLOR_ATTACHMENT_OPTIMAL and the depth stencil attachment's layout
1883 // will be transitioned to LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL. At the end of
1884 // the renderpass, the color attachment's layout will be transitioned to
1885 // LAYOUT_PRESENT_SRC_KHR to be ready to present. This is all done as part of
1886 // the renderpass, no barriers are necessary.
1887 const VkAttachmentDescription attachments[2] = {
1888 [0] =
1889 {
1890 .format = demo->format,
1891 .flags = 0,
1892 .samples = VK_SAMPLE_COUNT_1_BIT,
1893 .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
1894 .storeOp = VK_ATTACHMENT_STORE_OP_STORE,
1895 .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
1896 .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1897 .initialLayout = VK_IMAGE_LAYOUT_UNDEFINED,
1898 .finalLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
1899 },
1900 [1] =
1901 {
1902 .format = demo->depth.format,
1903 .flags = 0,
1904 .samples = VK_SAMPLE_COUNT_1_BIT,
1905 .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
1906 .storeOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1907 .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
1908 .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1909 .initialLayout = VK_IMAGE_LAYOUT_UNDEFINED,
1910 .finalLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1911 },
1912 };
1913 const VkAttachmentReference color_reference = {
1914 .attachment = 0,
1915 .layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
1916 };
1917 const VkAttachmentReference depth_reference = {
1918 .attachment = 1,
1919 .layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1920 };
1921 const VkSubpassDescription subpass = {
1922 .pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS,
1923 .flags = 0,
1924 .inputAttachmentCount = 0,
1925 .pInputAttachments = NULL,
1926 .colorAttachmentCount = 1,
1927 .pColorAttachments = &color_reference,
1928 .pResolveAttachments = NULL,
1929 .pDepthStencilAttachment = &depth_reference,
1930 .preserveAttachmentCount = 0,
1931 .pPreserveAttachments = NULL,
1932 };
1933
1934 VkSubpassDependency attachmentDependencies[2] = {
1935 [0] =
1936 {
1937 // Depth buffer is shared between swapchain images
1938 .srcSubpass = VK_SUBPASS_EXTERNAL,
1939 .dstSubpass = 0,
1940 .srcStageMask = VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT | VK_PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT,
1941 .dstStageMask = VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT | VK_PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT,
1942 .srcAccessMask = VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT,
1943 .dstAccessMask = VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT | VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT,
1944 .dependencyFlags = 0,
1945 },
1946 [1] =
1947 {
1948 // Image Layout Transition
1949 .srcSubpass = VK_SUBPASS_EXTERNAL,
1950 .dstSubpass = 0,
1951 .srcStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT,
1952 .dstStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT,
1953 .srcAccessMask = 0,
1954 .dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT | VK_ACCESS_COLOR_ATTACHMENT_READ_BIT,
1955 .dependencyFlags = 0,
1956 },
1957 };
1958
1959 const VkRenderPassCreateInfo rp_info = {
1960 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO,
1961 .pNext = NULL,
1962 .flags = 0,
1963 .attachmentCount = 2,
1964 .pAttachments = attachments,
1965 .subpassCount = 1,
1966 .pSubpasses = &subpass,
1967 .dependencyCount = 2,
1968 .pDependencies = attachmentDependencies,
1969 };
1970 VkResult U_ASSERT_ONLY err;
1971
1972 err = vkCreateRenderPass(demo->device, &rp_info, NULL, &demo->render_pass);
1973 assert(!err);
1974 }
1975
demo_prepare_shader_module(struct demo * demo,const uint32_t * code,size_t size)1976 static VkShaderModule demo_prepare_shader_module(struct demo *demo, const uint32_t *code, size_t size) {
1977 VkShaderModule module;
1978 VkShaderModuleCreateInfo moduleCreateInfo;
1979 VkResult U_ASSERT_ONLY err;
1980
1981 moduleCreateInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
1982 moduleCreateInfo.pNext = NULL;
1983 moduleCreateInfo.flags = 0;
1984 moduleCreateInfo.codeSize = size;
1985 moduleCreateInfo.pCode = code;
1986
1987 err = vkCreateShaderModule(demo->device, &moduleCreateInfo, NULL, &module);
1988 assert(!err);
1989
1990 return module;
1991 }
1992
demo_prepare_vs(struct demo * demo)1993 static void demo_prepare_vs(struct demo *demo) {
1994 const uint32_t vs_code[] = {
1995 #include "cube.vert.inc"
1996 };
1997 demo->vert_shader_module = demo_prepare_shader_module(demo, vs_code, sizeof(vs_code));
1998 }
1999
demo_prepare_fs(struct demo * demo)2000 static void demo_prepare_fs(struct demo *demo) {
2001 const uint32_t fs_code[] = {
2002 #include "cube.frag.inc"
2003 };
2004 demo->frag_shader_module = demo_prepare_shader_module(demo, fs_code, sizeof(fs_code));
2005 }
2006
demo_prepare_pipeline(struct demo * demo)2007 static void demo_prepare_pipeline(struct demo *demo) {
2008 #define NUM_DYNAMIC_STATES 2 /*Viewport + Scissor*/
2009
2010 VkGraphicsPipelineCreateInfo pipeline;
2011 VkPipelineCacheCreateInfo pipelineCache;
2012 VkPipelineVertexInputStateCreateInfo vi;
2013 VkPipelineInputAssemblyStateCreateInfo ia;
2014 VkPipelineRasterizationStateCreateInfo rs;
2015 VkPipelineColorBlendStateCreateInfo cb;
2016 VkPipelineDepthStencilStateCreateInfo ds;
2017 VkPipelineViewportStateCreateInfo vp;
2018 VkPipelineMultisampleStateCreateInfo ms;
2019 VkDynamicState dynamicStateEnables[NUM_DYNAMIC_STATES];
2020 VkPipelineDynamicStateCreateInfo dynamicState;
2021 VkResult U_ASSERT_ONLY err;
2022
2023 memset(dynamicStateEnables, 0, sizeof dynamicStateEnables);
2024 memset(&dynamicState, 0, sizeof dynamicState);
2025 dynamicState.sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO;
2026 dynamicState.pDynamicStates = dynamicStateEnables;
2027
2028 memset(&pipeline, 0, sizeof(pipeline));
2029 pipeline.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
2030 pipeline.layout = demo->pipeline_layout;
2031
2032 memset(&vi, 0, sizeof(vi));
2033 vi.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
2034
2035 memset(&ia, 0, sizeof(ia));
2036 ia.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
2037 ia.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
2038
2039 memset(&rs, 0, sizeof(rs));
2040 rs.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO;
2041 rs.polygonMode = VK_POLYGON_MODE_FILL;
2042 rs.cullMode = VK_CULL_MODE_BACK_BIT;
2043 rs.frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE;
2044 rs.depthClampEnable = VK_FALSE;
2045 rs.rasterizerDiscardEnable = VK_FALSE;
2046 rs.depthBiasEnable = VK_FALSE;
2047 rs.lineWidth = 1.0f;
2048
2049 memset(&cb, 0, sizeof(cb));
2050 cb.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
2051 VkPipelineColorBlendAttachmentState att_state[1];
2052 memset(att_state, 0, sizeof(att_state));
2053 att_state[0].colorWriteMask = 0xf;
2054 att_state[0].blendEnable = VK_FALSE;
2055 cb.attachmentCount = 1;
2056 cb.pAttachments = att_state;
2057
2058 memset(&vp, 0, sizeof(vp));
2059 vp.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
2060 vp.viewportCount = 1;
2061 dynamicStateEnables[dynamicState.dynamicStateCount++] = VK_DYNAMIC_STATE_VIEWPORT;
2062 vp.scissorCount = 1;
2063 dynamicStateEnables[dynamicState.dynamicStateCount++] = VK_DYNAMIC_STATE_SCISSOR;
2064
2065 memset(&ds, 0, sizeof(ds));
2066 ds.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
2067 ds.depthTestEnable = VK_TRUE;
2068 ds.depthWriteEnable = VK_TRUE;
2069 ds.depthCompareOp = VK_COMPARE_OP_LESS_OR_EQUAL;
2070 ds.depthBoundsTestEnable = VK_FALSE;
2071 ds.back.failOp = VK_STENCIL_OP_KEEP;
2072 ds.back.passOp = VK_STENCIL_OP_KEEP;
2073 ds.back.compareOp = VK_COMPARE_OP_ALWAYS;
2074 ds.stencilTestEnable = VK_FALSE;
2075 ds.front = ds.back;
2076
2077 memset(&ms, 0, sizeof(ms));
2078 ms.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
2079 ms.pSampleMask = NULL;
2080 ms.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT;
2081
2082 demo_prepare_vs(demo);
2083 demo_prepare_fs(demo);
2084
2085 // Two stages: vs and fs
2086 VkPipelineShaderStageCreateInfo shaderStages[2];
2087 memset(&shaderStages, 0, 2 * sizeof(VkPipelineShaderStageCreateInfo));
2088
2089 shaderStages[0].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
2090 shaderStages[0].stage = VK_SHADER_STAGE_VERTEX_BIT;
2091 shaderStages[0].module = demo->vert_shader_module;
2092 shaderStages[0].pName = "main";
2093
2094 shaderStages[1].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
2095 shaderStages[1].stage = VK_SHADER_STAGE_FRAGMENT_BIT;
2096 shaderStages[1].module = demo->frag_shader_module;
2097 shaderStages[1].pName = "main";
2098
2099 memset(&pipelineCache, 0, sizeof(pipelineCache));
2100 pipelineCache.sType = VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO;
2101
2102 err = vkCreatePipelineCache(demo->device, &pipelineCache, NULL, &demo->pipelineCache);
2103 assert(!err);
2104
2105 pipeline.pVertexInputState = &vi;
2106 pipeline.pInputAssemblyState = &ia;
2107 pipeline.pRasterizationState = &rs;
2108 pipeline.pColorBlendState = &cb;
2109 pipeline.pMultisampleState = &ms;
2110 pipeline.pViewportState = &vp;
2111 pipeline.pDepthStencilState = &ds;
2112 pipeline.stageCount = ARRAY_SIZE(shaderStages);
2113 pipeline.pStages = shaderStages;
2114 pipeline.renderPass = demo->render_pass;
2115 pipeline.pDynamicState = &dynamicState;
2116
2117 pipeline.renderPass = demo->render_pass;
2118
2119 err = vkCreateGraphicsPipelines(demo->device, demo->pipelineCache, 1, &pipeline, NULL, &demo->pipeline);
2120 assert(!err);
2121
2122 vkDestroyShaderModule(demo->device, demo->frag_shader_module, NULL);
2123 vkDestroyShaderModule(demo->device, demo->vert_shader_module, NULL);
2124 }
2125
demo_prepare_descriptor_pool(struct demo * demo)2126 static void demo_prepare_descriptor_pool(struct demo *demo) {
2127 const VkDescriptorPoolSize type_counts[2] = {
2128 [0] =
2129 {
2130 .type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
2131 .descriptorCount = demo->swapchainImageCount,
2132 },
2133 [1] =
2134 {
2135 .type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
2136 .descriptorCount = demo->swapchainImageCount * DEMO_TEXTURE_COUNT,
2137 },
2138 };
2139 const VkDescriptorPoolCreateInfo descriptor_pool = {
2140 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO,
2141 .pNext = NULL,
2142 .maxSets = demo->swapchainImageCount,
2143 .poolSizeCount = 2,
2144 .pPoolSizes = type_counts,
2145 };
2146 VkResult U_ASSERT_ONLY err;
2147
2148 err = vkCreateDescriptorPool(demo->device, &descriptor_pool, NULL, &demo->desc_pool);
2149 assert(!err);
2150 }
2151
demo_prepare_descriptor_set(struct demo * demo)2152 static void demo_prepare_descriptor_set(struct demo *demo) {
2153 VkDescriptorImageInfo tex_descs[DEMO_TEXTURE_COUNT];
2154 VkWriteDescriptorSet writes[2];
2155 VkResult U_ASSERT_ONLY err;
2156
2157 VkDescriptorSetAllocateInfo alloc_info = {.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO,
2158 .pNext = NULL,
2159 .descriptorPool = demo->desc_pool,
2160 .descriptorSetCount = 1,
2161 .pSetLayouts = &demo->desc_layout};
2162
2163 VkDescriptorBufferInfo buffer_info;
2164 buffer_info.offset = 0;
2165 buffer_info.range = sizeof(struct vktexcube_vs_uniform);
2166
2167 memset(&tex_descs, 0, sizeof(tex_descs));
2168 for (unsigned int i = 0; i < DEMO_TEXTURE_COUNT; i++) {
2169 tex_descs[i].sampler = demo->textures[i].sampler;
2170 tex_descs[i].imageView = demo->textures[i].view;
2171 tex_descs[i].imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
2172 }
2173
2174 memset(&writes, 0, sizeof(writes));
2175
2176 writes[0].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
2177 writes[0].descriptorCount = 1;
2178 writes[0].descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
2179 writes[0].pBufferInfo = &buffer_info;
2180
2181 writes[1].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
2182 writes[1].dstBinding = 1;
2183 writes[1].descriptorCount = DEMO_TEXTURE_COUNT;
2184 writes[1].descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
2185 writes[1].pImageInfo = tex_descs;
2186
2187 for (unsigned int i = 0; i < demo->swapchainImageCount; i++) {
2188 err = vkAllocateDescriptorSets(demo->device, &alloc_info, &demo->swapchain_image_resources[i].descriptor_set);
2189 assert(!err);
2190 buffer_info.buffer = demo->swapchain_image_resources[i].uniform_buffer;
2191 writes[0].dstSet = demo->swapchain_image_resources[i].descriptor_set;
2192 writes[1].dstSet = demo->swapchain_image_resources[i].descriptor_set;
2193 vkUpdateDescriptorSets(demo->device, 2, writes, 0, NULL);
2194 }
2195 }
2196
demo_prepare_framebuffers(struct demo * demo)2197 static void demo_prepare_framebuffers(struct demo *demo) {
2198 VkImageView attachments[2];
2199 attachments[1] = demo->depth.view;
2200
2201 const VkFramebufferCreateInfo fb_info = {
2202 .sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO,
2203 .pNext = NULL,
2204 .renderPass = demo->render_pass,
2205 .attachmentCount = 2,
2206 .pAttachments = attachments,
2207 .width = demo->width,
2208 .height = demo->height,
2209 .layers = 1,
2210 };
2211 VkResult U_ASSERT_ONLY err;
2212 uint32_t i;
2213
2214 for (i = 0; i < demo->swapchainImageCount; i++) {
2215 attachments[0] = demo->swapchain_image_resources[i].view;
2216 err = vkCreateFramebuffer(demo->device, &fb_info, NULL, &demo->swapchain_image_resources[i].framebuffer);
2217 assert(!err);
2218 }
2219 }
2220
demo_prepare(struct demo * demo)2221 static void demo_prepare(struct demo *demo) {
2222 demo_prepare_buffers(demo);
2223
2224 if (demo->is_minimized) {
2225 demo->prepared = false;
2226 return;
2227 }
2228
2229 VkResult U_ASSERT_ONLY err;
2230 if (demo->cmd_pool == VK_NULL_HANDLE) {
2231 const VkCommandPoolCreateInfo cmd_pool_info = {
2232 .sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,
2233 .pNext = NULL,
2234 .queueFamilyIndex = demo->graphics_queue_family_index,
2235 .flags = 0,
2236 };
2237 err = vkCreateCommandPool(demo->device, &cmd_pool_info, NULL, &demo->cmd_pool);
2238 assert(!err);
2239 }
2240
2241 const VkCommandBufferAllocateInfo cmd = {
2242 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,
2243 .pNext = NULL,
2244 .commandPool = demo->cmd_pool,
2245 .level = VK_COMMAND_BUFFER_LEVEL_PRIMARY,
2246 .commandBufferCount = 1,
2247 };
2248 err = vkAllocateCommandBuffers(demo->device, &cmd, &demo->cmd);
2249 assert(!err);
2250 VkCommandBufferBeginInfo cmd_buf_info = {
2251 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
2252 .pNext = NULL,
2253 .flags = 0,
2254 .pInheritanceInfo = NULL,
2255 };
2256 err = vkBeginCommandBuffer(demo->cmd, &cmd_buf_info);
2257 assert(!err);
2258
2259 demo_prepare_depth(demo);
2260 demo_prepare_textures(demo);
2261 demo_prepare_cube_data_buffers(demo);
2262
2263 demo_prepare_descriptor_layout(demo);
2264 demo_prepare_render_pass(demo);
2265 demo_prepare_pipeline(demo);
2266
2267 for (uint32_t i = 0; i < demo->swapchainImageCount; i++) {
2268 err = vkAllocateCommandBuffers(demo->device, &cmd, &demo->swapchain_image_resources[i].cmd);
2269 assert(!err);
2270 }
2271
2272 if (demo->separate_present_queue) {
2273 const VkCommandPoolCreateInfo present_cmd_pool_info = {
2274 .sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,
2275 .pNext = NULL,
2276 .queueFamilyIndex = demo->present_queue_family_index,
2277 .flags = 0,
2278 };
2279 err = vkCreateCommandPool(demo->device, &present_cmd_pool_info, NULL, &demo->present_cmd_pool);
2280 assert(!err);
2281 const VkCommandBufferAllocateInfo present_cmd_info = {
2282 .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,
2283 .pNext = NULL,
2284 .commandPool = demo->present_cmd_pool,
2285 .level = VK_COMMAND_BUFFER_LEVEL_PRIMARY,
2286 .commandBufferCount = 1,
2287 };
2288 for (uint32_t i = 0; i < demo->swapchainImageCount; i++) {
2289 err = vkAllocateCommandBuffers(demo->device, &present_cmd_info,
2290 &demo->swapchain_image_resources[i].graphics_to_present_cmd);
2291 assert(!err);
2292 demo_build_image_ownership_cmd(demo, i);
2293 }
2294 }
2295
2296 demo_prepare_descriptor_pool(demo);
2297 demo_prepare_descriptor_set(demo);
2298
2299 demo_prepare_framebuffers(demo);
2300
2301 for (uint32_t i = 0; i < demo->swapchainImageCount; i++) {
2302 demo->current_buffer = i;
2303 demo_draw_build_cmd(demo, demo->swapchain_image_resources[i].cmd);
2304 }
2305
2306 /*
2307 * Prepare functions above may generate pipeline commands
2308 * that need to be flushed before beginning the render loop.
2309 */
2310 demo_flush_init_cmd(demo);
2311 if (demo->staging_texture.buffer) {
2312 demo_destroy_texture(demo, &demo->staging_texture);
2313 }
2314
2315 demo->current_buffer = 0;
2316 demo->prepared = true;
2317 }
2318
demo_cleanup(struct demo * demo)2319 static void demo_cleanup(struct demo *demo) {
2320 uint32_t i;
2321
2322 demo->prepared = false;
2323 vkDeviceWaitIdle(demo->device);
2324
2325 // Wait for fences from present operations
2326 for (i = 0; i < FRAME_LAG; i++) {
2327 vkWaitForFences(demo->device, 1, &demo->fences[i], VK_TRUE, UINT64_MAX);
2328 vkDestroyFence(demo->device, demo->fences[i], NULL);
2329 vkDestroySemaphore(demo->device, demo->image_acquired_semaphores[i], NULL);
2330 vkDestroySemaphore(demo->device, demo->draw_complete_semaphores[i], NULL);
2331 if (demo->separate_present_queue) {
2332 vkDestroySemaphore(demo->device, demo->image_ownership_semaphores[i], NULL);
2333 }
2334 }
2335
2336 // If the window is currently minimized, demo_resize has already done some cleanup for us.
2337 if (!demo->is_minimized) {
2338 for (i = 0; i < demo->swapchainImageCount; i++) {
2339 vkDestroyFramebuffer(demo->device, demo->swapchain_image_resources[i].framebuffer, NULL);
2340 }
2341 vkDestroyDescriptorPool(demo->device, demo->desc_pool, NULL);
2342
2343 vkDestroyPipeline(demo->device, demo->pipeline, NULL);
2344 vkDestroyPipelineCache(demo->device, demo->pipelineCache, NULL);
2345 vkDestroyRenderPass(demo->device, demo->render_pass, NULL);
2346 vkDestroyPipelineLayout(demo->device, demo->pipeline_layout, NULL);
2347 vkDestroyDescriptorSetLayout(demo->device, demo->desc_layout, NULL);
2348
2349 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
2350 vkDestroyImageView(demo->device, demo->textures[i].view, NULL);
2351 vkDestroyImage(demo->device, demo->textures[i].image, NULL);
2352 vkFreeMemory(demo->device, demo->textures[i].mem, NULL);
2353 vkDestroySampler(demo->device, demo->textures[i].sampler, NULL);
2354 }
2355 demo->fpDestroySwapchainKHR(demo->device, demo->swapchain, NULL);
2356
2357 vkDestroyImageView(demo->device, demo->depth.view, NULL);
2358 vkDestroyImage(demo->device, demo->depth.image, NULL);
2359 vkFreeMemory(demo->device, demo->depth.mem, NULL);
2360
2361 for (i = 0; i < demo->swapchainImageCount; i++) {
2362 vkDestroyImageView(demo->device, demo->swapchain_image_resources[i].view, NULL);
2363 vkFreeCommandBuffers(demo->device, demo->cmd_pool, 1, &demo->swapchain_image_resources[i].cmd);
2364 vkDestroyBuffer(demo->device, demo->swapchain_image_resources[i].uniform_buffer, NULL);
2365 vkUnmapMemory(demo->device, demo->swapchain_image_resources[i].uniform_memory);
2366 vkFreeMemory(demo->device, demo->swapchain_image_resources[i].uniform_memory, NULL);
2367 }
2368 free(demo->swapchain_image_resources);
2369 free(demo->queue_props);
2370 vkDestroyCommandPool(demo->device, demo->cmd_pool, NULL);
2371
2372 if (demo->separate_present_queue) {
2373 vkDestroyCommandPool(demo->device, demo->present_cmd_pool, NULL);
2374 }
2375 }
2376 vkDeviceWaitIdle(demo->device);
2377 vkDestroyDevice(demo->device, NULL);
2378 if (demo->validate) {
2379 demo->DestroyDebugUtilsMessengerEXT(demo->inst, demo->dbg_messenger, NULL);
2380 }
2381 vkDestroySurfaceKHR(demo->inst, demo->surface, NULL);
2382
2383 #if defined(VK_USE_PLATFORM_XLIB_KHR)
2384 XDestroyWindow(demo->display, demo->xlib_window);
2385 XCloseDisplay(demo->display);
2386 #elif defined(VK_USE_PLATFORM_XCB_KHR)
2387 xcb_destroy_window(demo->connection, demo->xcb_window);
2388 xcb_disconnect(demo->connection);
2389 free(demo->atom_wm_delete_window);
2390 #elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
2391 wl_keyboard_destroy(demo->keyboard);
2392 wl_pointer_destroy(demo->pointer);
2393 wl_seat_destroy(demo->seat);
2394 xdg_toplevel_destroy(demo->xdg_toplevel);
2395 xdg_surface_destroy(demo->xdg_surface);
2396 wl_surface_destroy(demo->window);
2397 xdg_wm_base_destroy(demo->xdg_wm_base);
2398 if (demo->xdg_decoration_mgr) {
2399 zxdg_toplevel_decoration_v1_destroy(demo->toplevel_decoration);
2400 zxdg_decoration_manager_v1_destroy(demo->xdg_decoration_mgr);
2401 }
2402 wl_compositor_destroy(demo->compositor);
2403 wl_registry_destroy(demo->registry);
2404 wl_display_disconnect(demo->display);
2405 #elif defined(VK_USE_PLATFORM_DIRECTFB_EXT)
2406 demo->event_buffer->Release(demo->event_buffer);
2407 demo->window->Release(demo->window);
2408 demo->dfb->Release(demo->dfb);
2409 #endif
2410
2411 vkDestroyInstance(demo->inst, NULL);
2412 }
2413
demo_resize(struct demo * demo)2414 static void demo_resize(struct demo *demo) {
2415 uint32_t i;
2416
2417 // Don't react to resize until after first initialization.
2418 if (!demo->prepared) {
2419 if (demo->is_minimized) {
2420 demo_prepare(demo);
2421 }
2422 return;
2423 }
2424 // In order to properly resize the window, we must re-create the swapchain
2425 // AND redo the command buffers, etc.
2426 //
2427 // First, perform part of the demo_cleanup() function:
2428 demo->prepared = false;
2429 vkDeviceWaitIdle(demo->device);
2430
2431 for (i = 0; i < demo->swapchainImageCount; i++) {
2432 vkDestroyFramebuffer(demo->device, demo->swapchain_image_resources[i].framebuffer, NULL);
2433 }
2434 vkDestroyDescriptorPool(demo->device, demo->desc_pool, NULL);
2435
2436 vkDestroyPipeline(demo->device, demo->pipeline, NULL);
2437 vkDestroyPipelineCache(demo->device, demo->pipelineCache, NULL);
2438 vkDestroyRenderPass(demo->device, demo->render_pass, NULL);
2439 vkDestroyPipelineLayout(demo->device, demo->pipeline_layout, NULL);
2440 vkDestroyDescriptorSetLayout(demo->device, demo->desc_layout, NULL);
2441
2442 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
2443 vkDestroyImageView(demo->device, demo->textures[i].view, NULL);
2444 vkDestroyImage(demo->device, demo->textures[i].image, NULL);
2445 vkFreeMemory(demo->device, demo->textures[i].mem, NULL);
2446 vkDestroySampler(demo->device, demo->textures[i].sampler, NULL);
2447 }
2448
2449 vkDestroyImageView(demo->device, demo->depth.view, NULL);
2450 vkDestroyImage(demo->device, demo->depth.image, NULL);
2451 vkFreeMemory(demo->device, demo->depth.mem, NULL);
2452
2453 for (i = 0; i < demo->swapchainImageCount; i++) {
2454 vkDestroyImageView(demo->device, demo->swapchain_image_resources[i].view, NULL);
2455 vkFreeCommandBuffers(demo->device, demo->cmd_pool, 1, &demo->swapchain_image_resources[i].cmd);
2456 vkDestroyBuffer(demo->device, demo->swapchain_image_resources[i].uniform_buffer, NULL);
2457 vkUnmapMemory(demo->device, demo->swapchain_image_resources[i].uniform_memory);
2458 vkFreeMemory(demo->device, demo->swapchain_image_resources[i].uniform_memory, NULL);
2459 }
2460 vkDestroyCommandPool(demo->device, demo->cmd_pool, NULL);
2461 demo->cmd_pool = VK_NULL_HANDLE;
2462 if (demo->separate_present_queue) {
2463 vkDestroyCommandPool(demo->device, demo->present_cmd_pool, NULL);
2464 }
2465 free(demo->swapchain_image_resources);
2466
2467 // Second, re-perform the demo_prepare() function, which will re-create the
2468 // swapchain:
2469 demo_prepare(demo);
2470 }
2471
2472 // On MS-Windows, make this a global, so it's available to WndProc()
2473 struct demo demo;
2474
2475 #if defined(VK_USE_PLATFORM_WIN32_KHR)
demo_run(struct demo * demo)2476 static void demo_run(struct demo *demo) {
2477 if (!demo->prepared) return;
2478
2479 demo_draw(demo);
2480 demo->curFrame++;
2481 if (demo->frameCount != INT32_MAX && demo->curFrame == demo->frameCount) {
2482 PostQuitMessage(validation_error);
2483 }
2484 }
2485
2486 // MS-Windows event handling function:
WndProc(HWND hWnd,UINT uMsg,WPARAM wParam,LPARAM lParam)2487 LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
2488 switch (uMsg) {
2489 case WM_CLOSE:
2490 PostQuitMessage(validation_error);
2491 break;
2492 case WM_PAINT:
2493 // The validation callback calls MessageBox which can generate paint
2494 // events - don't make more Vulkan calls if we got here from the
2495 // callback
2496 if (!in_callback) {
2497 demo_run(&demo);
2498 }
2499 break;
2500 case WM_GETMINMAXINFO: // set window's minimum size
2501 ((MINMAXINFO *)lParam)->ptMinTrackSize = demo.minsize;
2502 return 0;
2503 case WM_ERASEBKGND:
2504 return 1;
2505 case WM_SIZE:
2506 // Resize the application to the new window size, except when
2507 // it was minimized. Vulkan doesn't support images or swapchains
2508 // with width=0 and height=0.
2509 if (wParam != SIZE_MINIMIZED) {
2510 demo.width = lParam & 0xffff;
2511 demo.height = (lParam & 0xffff0000) >> 16;
2512 demo_resize(&demo);
2513 }
2514 break;
2515 case WM_KEYDOWN:
2516 switch (wParam) {
2517 case VK_ESCAPE:
2518 PostQuitMessage(validation_error);
2519 break;
2520 case VK_LEFT:
2521 demo.spin_angle -= demo.spin_increment;
2522 break;
2523 case VK_RIGHT:
2524 demo.spin_angle += demo.spin_increment;
2525 break;
2526 case VK_SPACE:
2527 demo.pause = !demo.pause;
2528 break;
2529 }
2530 return 0;
2531 default:
2532 break;
2533 }
2534 return (DefWindowProc(hWnd, uMsg, wParam, lParam));
2535 }
2536
demo_create_window(struct demo * demo)2537 static void demo_create_window(struct demo *demo) {
2538 WNDCLASSEX win_class;
2539
2540 // Initialize the window class structure:
2541 win_class.cbSize = sizeof(WNDCLASSEX);
2542 win_class.style = CS_HREDRAW | CS_VREDRAW;
2543 win_class.lpfnWndProc = WndProc;
2544 win_class.cbClsExtra = 0;
2545 win_class.cbWndExtra = 0;
2546 win_class.hInstance = demo->connection; // hInstance
2547 win_class.hIcon = LoadIcon(NULL, IDI_APPLICATION);
2548 win_class.hCursor = LoadCursor(NULL, IDC_ARROW);
2549 win_class.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
2550 win_class.lpszMenuName = NULL;
2551 win_class.lpszClassName = demo->name;
2552 win_class.hIconSm = LoadIcon(NULL, IDI_WINLOGO);
2553 // Register window class:
2554 if (!RegisterClassEx(&win_class)) {
2555 // It didn't work, so try to give a useful error:
2556 printf("Unexpected error trying to start the application!\n");
2557 fflush(stdout);
2558 exit(1);
2559 }
2560 // Create window with the registered class:
2561 RECT wr = {0, 0, demo->width, demo->height};
2562 AdjustWindowRect(&wr, WS_OVERLAPPEDWINDOW, FALSE);
2563 demo->window = CreateWindowEx(0,
2564 demo->name, // class name
2565 demo->name, // app name
2566 WS_OVERLAPPEDWINDOW | // window style
2567 WS_VISIBLE | WS_SYSMENU,
2568 100, 100, // x/y coords
2569 wr.right - wr.left, // width
2570 wr.bottom - wr.top, // height
2571 NULL, // handle to parent
2572 NULL, // handle to menu
2573 demo->connection, // hInstance
2574 NULL); // no extra parameters
2575 if (!demo->window) {
2576 // It didn't work, so try to give a useful error:
2577 printf("Cannot create a window in which to draw!\n");
2578 fflush(stdout);
2579 exit(1);
2580 }
2581 // Window client area size must be at least 1 pixel high, to prevent crash.
2582 demo->minsize.x = GetSystemMetrics(SM_CXMINTRACK);
2583 demo->minsize.y = GetSystemMetrics(SM_CYMINTRACK) + 1;
2584 }
2585 #elif defined(VK_USE_PLATFORM_XLIB_KHR)
demo_create_xlib_window(struct demo * demo)2586 static void demo_create_xlib_window(struct demo *demo) {
2587 const char *display_envar = getenv("DISPLAY");
2588 if (display_envar == NULL || display_envar[0] == '\0') {
2589 printf("Environment variable DISPLAY requires a valid value.\nExiting ...\n");
2590 fflush(stdout);
2591 exit(1);
2592 }
2593
2594 XInitThreads();
2595 demo->display = XOpenDisplay(NULL);
2596 long visualMask = VisualScreenMask;
2597 int numberOfVisuals;
2598 XVisualInfo vInfoTemplate = {};
2599 vInfoTemplate.screen = DefaultScreen(demo->display);
2600 XVisualInfo *visualInfo = XGetVisualInfo(demo->display, visualMask, &vInfoTemplate, &numberOfVisuals);
2601
2602 Colormap colormap =
2603 XCreateColormap(demo->display, RootWindow(demo->display, vInfoTemplate.screen), visualInfo->visual, AllocNone);
2604
2605 XSetWindowAttributes windowAttributes = {};
2606 windowAttributes.colormap = colormap;
2607 windowAttributes.background_pixel = 0xFFFFFFFF;
2608 windowAttributes.border_pixel = 0;
2609 windowAttributes.event_mask = KeyPressMask | KeyReleaseMask | StructureNotifyMask | ExposureMask;
2610
2611 demo->xlib_window = XCreateWindow(demo->display, RootWindow(demo->display, vInfoTemplate.screen), 0, 0, demo->width,
2612 demo->height, 0, visualInfo->depth, InputOutput, visualInfo->visual,
2613 CWBackPixel | CWBorderPixel | CWEventMask | CWColormap, &windowAttributes);
2614
2615 XSelectInput(demo->display, demo->xlib_window, ExposureMask | KeyPressMask);
2616 XMapWindow(demo->display, demo->xlib_window);
2617 XFlush(demo->display);
2618 demo->xlib_wm_delete_window = XInternAtom(demo->display, "WM_DELETE_WINDOW", False);
2619 }
demo_handle_xlib_event(struct demo * demo,const XEvent * event)2620 static void demo_handle_xlib_event(struct demo *demo, const XEvent *event) {
2621 switch (event->type) {
2622 case ClientMessage:
2623 if ((Atom)event->xclient.data.l[0] == demo->xlib_wm_delete_window) demo->quit = true;
2624 break;
2625 case KeyPress:
2626 switch (event->xkey.keycode) {
2627 case 0x9: // Escape
2628 demo->quit = true;
2629 break;
2630 case 0x71: // left arrow key
2631 demo->spin_angle -= demo->spin_increment;
2632 break;
2633 case 0x72: // right arrow key
2634 demo->spin_angle += demo->spin_increment;
2635 break;
2636 case 0x41: // space bar
2637 demo->pause = !demo->pause;
2638 break;
2639 }
2640 break;
2641 case ConfigureNotify:
2642 if ((demo->width != event->xconfigure.width) || (demo->height != event->xconfigure.height)) {
2643 demo->width = event->xconfigure.width;
2644 demo->height = event->xconfigure.height;
2645 demo_resize(demo);
2646 }
2647 break;
2648 default:
2649 break;
2650 }
2651 }
2652
demo_run_xlib(struct demo * demo)2653 static void demo_run_xlib(struct demo *demo) {
2654 while (!demo->quit) {
2655 XEvent event;
2656
2657 if (demo->pause) {
2658 XNextEvent(demo->display, &event);
2659 demo_handle_xlib_event(demo, &event);
2660 }
2661 while (XPending(demo->display) > 0) {
2662 XNextEvent(demo->display, &event);
2663 demo_handle_xlib_event(demo, &event);
2664 }
2665
2666 demo_draw(demo);
2667 demo->curFrame++;
2668 if (demo->frameCount != INT32_MAX && demo->curFrame == demo->frameCount) demo->quit = true;
2669 }
2670 }
2671 #elif defined(VK_USE_PLATFORM_XCB_KHR)
demo_handle_xcb_event(struct demo * demo,const xcb_generic_event_t * event)2672 static void demo_handle_xcb_event(struct demo *demo, const xcb_generic_event_t *event) {
2673 uint8_t event_code = event->response_type & 0x7f;
2674 switch (event_code) {
2675 case XCB_EXPOSE:
2676 // TODO: Resize window
2677 break;
2678 case XCB_CLIENT_MESSAGE:
2679 if ((*(xcb_client_message_event_t *)event).data.data32[0] == (*demo->atom_wm_delete_window).atom) {
2680 demo->quit = true;
2681 }
2682 break;
2683 case XCB_KEY_RELEASE: {
2684 const xcb_key_release_event_t *key = (const xcb_key_release_event_t *)event;
2685
2686 switch (key->detail) {
2687 case 0x9: // Escape
2688 demo->quit = true;
2689 break;
2690 case 0x71: // left arrow key
2691 demo->spin_angle -= demo->spin_increment;
2692 break;
2693 case 0x72: // right arrow key
2694 demo->spin_angle += demo->spin_increment;
2695 break;
2696 case 0x41: // space bar
2697 demo->pause = !demo->pause;
2698 break;
2699 }
2700 } break;
2701 case XCB_CONFIGURE_NOTIFY: {
2702 const xcb_configure_notify_event_t *cfg = (const xcb_configure_notify_event_t *)event;
2703 if ((demo->width != cfg->width) || (demo->height != cfg->height)) {
2704 demo->width = cfg->width;
2705 demo->height = cfg->height;
2706 demo_resize(demo);
2707 }
2708 } break;
2709 default:
2710 break;
2711 }
2712 }
2713
demo_run_xcb(struct demo * demo)2714 static void demo_run_xcb(struct demo *demo) {
2715 xcb_flush(demo->connection);
2716
2717 while (!demo->quit) {
2718 xcb_generic_event_t *event;
2719
2720 if (demo->pause) {
2721 event = xcb_wait_for_event(demo->connection);
2722 } else {
2723 event = xcb_poll_for_event(demo->connection);
2724 }
2725 while (event) {
2726 demo_handle_xcb_event(demo, event);
2727 free(event);
2728 event = xcb_poll_for_event(demo->connection);
2729 }
2730
2731 demo_draw(demo);
2732 demo->curFrame++;
2733 if (demo->frameCount != INT32_MAX && demo->curFrame == demo->frameCount) demo->quit = true;
2734 }
2735 }
2736
demo_create_xcb_window(struct demo * demo)2737 static void demo_create_xcb_window(struct demo *demo) {
2738 uint32_t value_mask, value_list[32];
2739
2740 demo->xcb_window = xcb_generate_id(demo->connection);
2741
2742 value_mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK;
2743 value_list[0] = demo->screen->black_pixel;
2744 value_list[1] = XCB_EVENT_MASK_KEY_RELEASE | XCB_EVENT_MASK_EXPOSURE | XCB_EVENT_MASK_STRUCTURE_NOTIFY;
2745
2746 xcb_create_window(demo->connection, XCB_COPY_FROM_PARENT, demo->xcb_window, demo->screen->root, 0, 0, demo->width, demo->height,
2747 0, XCB_WINDOW_CLASS_INPUT_OUTPUT, demo->screen->root_visual, value_mask, value_list);
2748
2749 /* Magic code that will send notification when window is destroyed */
2750 xcb_intern_atom_cookie_t cookie = xcb_intern_atom(demo->connection, 1, 12, "WM_PROTOCOLS");
2751 xcb_intern_atom_reply_t *reply = xcb_intern_atom_reply(demo->connection, cookie, 0);
2752
2753 xcb_intern_atom_cookie_t cookie2 = xcb_intern_atom(demo->connection, 0, 16, "WM_DELETE_WINDOW");
2754 demo->atom_wm_delete_window = xcb_intern_atom_reply(demo->connection, cookie2, 0);
2755
2756 xcb_change_property(demo->connection, XCB_PROP_MODE_REPLACE, demo->xcb_window, (*reply).atom, 4, 32, 1,
2757 &(*demo->atom_wm_delete_window).atom);
2758 free(reply);
2759
2760 xcb_map_window(demo->connection, demo->xcb_window);
2761
2762 // Force the x/y coordinates to 100,100 results are identical in consecutive
2763 // runs
2764 const uint32_t coords[] = {100, 100};
2765 xcb_configure_window(demo->connection, demo->xcb_window, XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y, coords);
2766 }
2767 // VK_USE_PLATFORM_XCB_KHR
2768 #elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
demo_run(struct demo * demo)2769 static void demo_run(struct demo *demo) {
2770 while (!demo->quit) {
2771 if (demo->pause) {
2772 wl_display_dispatch(demo->display); // block and wait for input
2773 } else {
2774 wl_display_dispatch_pending(demo->display); // don't block
2775 demo_draw(demo);
2776 demo->curFrame++;
2777 if (demo->frameCount != INT32_MAX && demo->curFrame == demo->frameCount) demo->quit = true;
2778 }
2779 }
2780 }
2781
handle_surface_configure(void * data,struct xdg_surface * xdg_surface,uint32_t serial)2782 static void handle_surface_configure(void *data, struct xdg_surface *xdg_surface, uint32_t serial) {
2783 struct demo *demo = (struct demo *)data;
2784 xdg_surface_ack_configure(xdg_surface, serial);
2785 if (demo->xdg_surface_has_been_configured) {
2786 demo_resize(demo);
2787 }
2788 demo->xdg_surface_has_been_configured = 1;
2789 }
2790
2791 static const struct xdg_surface_listener xdg_surface_listener = {handle_surface_configure};
2792
handle_toplevel_configure(void * data,struct xdg_toplevel * xdg_toplevel UNUSED,int32_t width,int32_t height,struct wl_array * states UNUSED)2793 static void handle_toplevel_configure(void *data, struct xdg_toplevel *xdg_toplevel UNUSED, int32_t width, int32_t height,
2794 struct wl_array *states UNUSED) {
2795 struct demo *demo = (struct demo *)data;
2796 /* zero values imply the program may choose its own size, so in that case
2797 * stay with the existing value (which on startup is the default) */
2798 if (width > 0) {
2799 demo->width = width;
2800 }
2801 if (height > 0) {
2802 demo->height = height;
2803 }
2804 /* This should be followed by a surface configure */
2805 }
2806
handle_toplevel_close(void * data,struct xdg_toplevel * xdg_toplevel UNUSED)2807 static void handle_toplevel_close(void *data, struct xdg_toplevel *xdg_toplevel UNUSED) {
2808 struct demo *demo = (struct demo *)data;
2809 demo->quit = true;
2810 }
2811
2812 static const struct xdg_toplevel_listener xdg_toplevel_listener = {handle_toplevel_configure, handle_toplevel_close};
2813
demo_create_window(struct demo * demo)2814 static void demo_create_window(struct demo *demo) {
2815 if (!demo->xdg_wm_base) {
2816 printf("Compositor did not provide the standard protocol xdg-wm-base\n");
2817 fflush(stdout);
2818 exit(1);
2819 }
2820
2821 demo->window = wl_compositor_create_surface(demo->compositor);
2822 if (!demo->window) {
2823 printf("Can not create wayland_surface from compositor!\n");
2824 fflush(stdout);
2825 exit(1);
2826 }
2827
2828 demo->xdg_surface = xdg_wm_base_get_xdg_surface(demo->xdg_wm_base, demo->window);
2829 if (!demo->xdg_surface) {
2830 printf("Can not get xdg_surface from wayland_surface!\n");
2831 fflush(stdout);
2832 exit(1);
2833 }
2834 demo->xdg_toplevel = xdg_surface_get_toplevel(demo->xdg_surface);
2835 if (!demo->xdg_toplevel) {
2836 printf("Can not allocate xdg_toplevel for xdg_surface!\n");
2837 fflush(stdout);
2838 exit(1);
2839 }
2840 xdg_surface_add_listener(demo->xdg_surface, &xdg_surface_listener, demo);
2841 xdg_toplevel_add_listener(demo->xdg_toplevel, &xdg_toplevel_listener, demo);
2842 xdg_toplevel_set_title(demo->xdg_toplevel, APP_SHORT_NAME);
2843 if (demo->xdg_decoration_mgr) {
2844 // if supported, let the compositor render titlebars for us
2845 demo->toplevel_decoration =
2846 zxdg_decoration_manager_v1_get_toplevel_decoration(demo->xdg_decoration_mgr, demo->xdg_toplevel);
2847 zxdg_toplevel_decoration_v1_set_mode(demo->toplevel_decoration, ZXDG_TOPLEVEL_DECORATION_V1_MODE_SERVER_SIDE);
2848 }
2849
2850 wl_surface_commit(demo->window);
2851 }
2852 #elif defined(VK_USE_PLATFORM_DIRECTFB_EXT)
demo_create_directfb_window(struct demo * demo)2853 static void demo_create_directfb_window(struct demo *demo) {
2854 DFBResult ret;
2855
2856 ret = DirectFBInit(NULL, NULL);
2857 if (ret) {
2858 printf("DirectFBInit failed to initialize DirectFB!\n");
2859 fflush(stdout);
2860 exit(1);
2861 }
2862
2863 ret = DirectFBCreate(&demo->dfb);
2864 if (ret) {
2865 printf("DirectFBCreate failed to create main interface of DirectFB!\n");
2866 fflush(stdout);
2867 exit(1);
2868 }
2869
2870 DFBSurfaceDescription desc;
2871 desc.flags = DSDESC_CAPS | DSDESC_WIDTH | DSDESC_HEIGHT;
2872 desc.caps = DSCAPS_PRIMARY;
2873 desc.width = demo->width;
2874 desc.height = demo->height;
2875 ret = demo->dfb->CreateSurface(demo->dfb, &desc, &demo->window);
2876 if (ret) {
2877 printf("CreateSurface failed to create DirectFB surface interface!\n");
2878 fflush(stdout);
2879 exit(1);
2880 }
2881
2882 ret = demo->dfb->CreateInputEventBuffer(demo->dfb, DICAPS_KEYS, DFB_FALSE, &demo->event_buffer);
2883 if (ret) {
2884 printf("CreateInputEventBuffer failed to create DirectFB event buffer interface!\n");
2885 fflush(stdout);
2886 exit(1);
2887 }
2888 }
2889
demo_handle_directfb_event(struct demo * demo,const DFBInputEvent * event)2890 static void demo_handle_directfb_event(struct demo *demo, const DFBInputEvent *event) {
2891 if (event->type != DIET_KEYPRESS) return;
2892 switch (event->key_symbol) {
2893 case DIKS_ESCAPE: // Escape
2894 demo->quit = true;
2895 break;
2896 case DIKS_CURSOR_LEFT: // left arrow key
2897 demo->spin_angle -= demo->spin_increment;
2898 break;
2899 case DIKS_CURSOR_RIGHT: // right arrow key
2900 demo->spin_angle += demo->spin_increment;
2901 break;
2902 case DIKS_SPACE: // space bar
2903 demo->pause = !demo->pause;
2904 break;
2905 default:
2906 break;
2907 }
2908 }
2909
demo_run_directfb(struct demo * demo)2910 static void demo_run_directfb(struct demo *demo) {
2911 while (!demo->quit) {
2912 DFBInputEvent event;
2913
2914 if (demo->pause) {
2915 demo->event_buffer->WaitForEvent(demo->event_buffer);
2916 if (!demo->event_buffer->GetEvent(demo->event_buffer, DFB_EVENT(&event))) demo_handle_directfb_event(demo, &event);
2917 } else {
2918 if (!demo->event_buffer->GetEvent(demo->event_buffer, DFB_EVENT(&event))) demo_handle_directfb_event(demo, &event);
2919
2920 demo_draw(demo);
2921 demo->curFrame++;
2922 if (demo->frameCount != INT32_MAX && demo->curFrame == demo->frameCount) demo->quit = true;
2923 }
2924 }
2925 }
2926 #elif defined(VK_USE_PLATFORM_ANDROID_KHR)
demo_run(struct demo * demo)2927 static void demo_run(struct demo *demo) {
2928 if (!demo->prepared) return;
2929
2930 demo_draw(demo);
2931 demo->curFrame++;
2932 }
2933 #elif defined(VK_USE_PLATFORM_METAL_EXT)
demo_run(struct demo * demo)2934 static void demo_run(struct demo *demo) {
2935 demo_draw(demo);
2936 demo->curFrame++;
2937 if (demo->frameCount != INT32_MAX && demo->curFrame == demo->frameCount) {
2938 demo->quit = TRUE;
2939 }
2940 }
2941 #elif defined(VK_USE_PLATFORM_DISPLAY_KHR)
demo_create_display_surface(struct demo * demo)2942 static VkResult demo_create_display_surface(struct demo *demo) {
2943 VkResult U_ASSERT_ONLY err;
2944 uint32_t display_count;
2945 uint32_t mode_count;
2946 uint32_t plane_count;
2947 VkDisplayPropertiesKHR display_props;
2948 VkDisplayKHR display;
2949 VkDisplayModePropertiesKHR mode_props;
2950 VkDisplayPlanePropertiesKHR *plane_props;
2951 VkBool32 found_plane = VK_FALSE;
2952 uint32_t plane_index;
2953 VkExtent2D image_extent;
2954 VkDisplaySurfaceCreateInfoKHR create_info;
2955
2956 // Get the first display
2957 display_count = 1;
2958 err = vkGetPhysicalDeviceDisplayPropertiesKHR(demo->gpu, &display_count, &display_props);
2959 assert(!err || (err == VK_INCOMPLETE));
2960
2961 display = display_props.display;
2962
2963 // Get the first mode of the display
2964 err = vkGetDisplayModePropertiesKHR(demo->gpu, display, &mode_count, NULL);
2965 assert(!err);
2966
2967 if (mode_count == 0) {
2968 printf("Cannot find any mode for the display!\n");
2969 fflush(stdout);
2970 exit(1);
2971 }
2972
2973 mode_count = 1;
2974 err = vkGetDisplayModePropertiesKHR(demo->gpu, display, &mode_count, &mode_props);
2975 assert(!err || (err == VK_INCOMPLETE));
2976
2977 // Get the list of planes
2978 err = vkGetPhysicalDeviceDisplayPlanePropertiesKHR(demo->gpu, &plane_count, NULL);
2979 assert(!err);
2980
2981 if (plane_count == 0) {
2982 printf("Cannot find any plane!\n");
2983 fflush(stdout);
2984 exit(1);
2985 }
2986
2987 plane_props = malloc(sizeof(VkDisplayPlanePropertiesKHR) * plane_count);
2988 assert(plane_props);
2989
2990 err = vkGetPhysicalDeviceDisplayPlanePropertiesKHR(demo->gpu, &plane_count, plane_props);
2991 assert(!err);
2992
2993 // Find a plane compatible with the display
2994 for (plane_index = 0; plane_index < plane_count; plane_index++) {
2995 uint32_t supported_count;
2996 VkDisplayKHR *supported_displays;
2997
2998 // Disqualify planes that are bound to a different display
2999 if ((plane_props[plane_index].currentDisplay != VK_NULL_HANDLE) && (plane_props[plane_index].currentDisplay != display)) {
3000 continue;
3001 }
3002
3003 err = vkGetDisplayPlaneSupportedDisplaysKHR(demo->gpu, plane_index, &supported_count, NULL);
3004 assert(!err);
3005
3006 if (supported_count == 0) {
3007 continue;
3008 }
3009
3010 supported_displays = malloc(sizeof(VkDisplayKHR) * supported_count);
3011 assert(supported_displays);
3012
3013 err = vkGetDisplayPlaneSupportedDisplaysKHR(demo->gpu, plane_index, &supported_count, supported_displays);
3014 assert(!err);
3015
3016 for (uint32_t i = 0; i < supported_count; i++) {
3017 if (supported_displays[i] == display) {
3018 found_plane = VK_TRUE;
3019 break;
3020 }
3021 }
3022
3023 free(supported_displays);
3024
3025 if (found_plane) {
3026 break;
3027 }
3028 }
3029
3030 if (!found_plane) {
3031 printf("Cannot find a plane compatible with the display!\n");
3032 fflush(stdout);
3033 exit(1);
3034 }
3035
3036 free(plane_props);
3037
3038 VkDisplayPlaneCapabilitiesKHR planeCaps;
3039 vkGetDisplayPlaneCapabilitiesKHR(demo->gpu, mode_props.displayMode, plane_index, &planeCaps);
3040 // Find a supported alpha mode
3041 VkDisplayPlaneAlphaFlagBitsKHR alphaMode = VK_DISPLAY_PLANE_ALPHA_OPAQUE_BIT_KHR;
3042 VkDisplayPlaneAlphaFlagBitsKHR alphaModes[4] = {
3043 VK_DISPLAY_PLANE_ALPHA_OPAQUE_BIT_KHR,
3044 VK_DISPLAY_PLANE_ALPHA_GLOBAL_BIT_KHR,
3045 VK_DISPLAY_PLANE_ALPHA_PER_PIXEL_BIT_KHR,
3046 VK_DISPLAY_PLANE_ALPHA_PER_PIXEL_PREMULTIPLIED_BIT_KHR,
3047 };
3048 for (uint32_t i = 0; i < sizeof(alphaModes); i++) {
3049 if (planeCaps.supportedAlpha & alphaModes[i]) {
3050 alphaMode = alphaModes[i];
3051 break;
3052 }
3053 }
3054 image_extent.width = mode_props.parameters.visibleRegion.width;
3055 image_extent.height = mode_props.parameters.visibleRegion.height;
3056
3057 create_info.sType = VK_STRUCTURE_TYPE_DISPLAY_SURFACE_CREATE_INFO_KHR;
3058 create_info.pNext = NULL;
3059 create_info.flags = 0;
3060 create_info.displayMode = mode_props.displayMode;
3061 create_info.planeIndex = plane_index;
3062 create_info.planeStackIndex = plane_props[plane_index].currentStackIndex;
3063 create_info.transform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
3064 create_info.alphaMode = alphaMode;
3065 create_info.globalAlpha = 1.0f;
3066 create_info.imageExtent = image_extent;
3067
3068 return vkCreateDisplayPlaneSurfaceKHR(demo->inst, &create_info, NULL, &demo->surface);
3069 }
3070
demo_run_display(struct demo * demo)3071 static void demo_run_display(struct demo *demo) {
3072 while (!demo->quit) {
3073 demo_draw(demo);
3074 demo->curFrame++;
3075
3076 if (demo->frameCount != INT32_MAX && demo->curFrame == demo->frameCount) {
3077 demo->quit = true;
3078 }
3079 }
3080 }
3081 #endif
3082
3083 /*
3084 * Return 1 (true) if all layer names specified in check_names
3085 * can be found in given layer properties.
3086 */
demo_check_layers(uint32_t check_count,char ** check_names,uint32_t layer_count,VkLayerProperties * layers)3087 static VkBool32 demo_check_layers(uint32_t check_count, char **check_names, uint32_t layer_count, VkLayerProperties *layers) {
3088 for (uint32_t i = 0; i < check_count; i++) {
3089 VkBool32 found = 0;
3090 for (uint32_t j = 0; j < layer_count; j++) {
3091 if (!strcmp(check_names[i], layers[j].layerName)) {
3092 found = 1;
3093 break;
3094 }
3095 }
3096 if (!found) {
3097 fprintf(stderr, "Cannot find layer: %s\n", check_names[i]);
3098 return 0;
3099 }
3100 }
3101 return 1;
3102 }
3103 #if defined(VK_USE_PLATFORM_DISPLAY_KHR)
find_display_gpu(int gpu_number,uint32_t gpu_count,VkPhysicalDevice * physical_devices)3104 int find_display_gpu(int gpu_number, uint32_t gpu_count, VkPhysicalDevice *physical_devices) {
3105 uint32_t display_count = 0;
3106 VkResult result;
3107 int gpu_return = gpu_number;
3108 if (gpu_number >= 0) {
3109 result = vkGetPhysicalDeviceDisplayPropertiesKHR(physical_devices[gpu_number], &display_count, NULL);
3110 assert(!result);
3111 } else {
3112 for (uint32_t i = 0; i < gpu_count; i++) {
3113 result = vkGetPhysicalDeviceDisplayPropertiesKHR(physical_devices[i], &display_count, NULL);
3114 assert(!result);
3115 if (display_count) {
3116 gpu_return = i;
3117 break;
3118 }
3119 }
3120 }
3121 if (display_count > 0)
3122 return gpu_return;
3123 else
3124 return -1;
3125 }
3126 #endif
demo_init_vk(struct demo * demo)3127 static void demo_init_vk(struct demo *demo) {
3128 VkResult err;
3129 uint32_t instance_extension_count = 0;
3130 uint32_t instance_layer_count = 0;
3131 char *instance_validation_layers[] = {"VK_LAYER_KHRONOS_validation"};
3132 demo->enabled_extension_count = 0;
3133 demo->enabled_layer_count = 0;
3134 demo->is_minimized = false;
3135 demo->cmd_pool = VK_NULL_HANDLE;
3136
3137 // Look for validation layers
3138 VkBool32 validation_found = 0;
3139 if (demo->validate) {
3140 err = vkEnumerateInstanceLayerProperties(&instance_layer_count, NULL);
3141 assert(!err);
3142
3143 if (instance_layer_count > 0) {
3144 VkLayerProperties *instance_layers = malloc(sizeof(VkLayerProperties) * instance_layer_count);
3145 err = vkEnumerateInstanceLayerProperties(&instance_layer_count, instance_layers);
3146 assert(!err);
3147
3148 validation_found = demo_check_layers(ARRAY_SIZE(instance_validation_layers), instance_validation_layers,
3149 instance_layer_count, instance_layers);
3150 if (validation_found) {
3151 demo->enabled_layer_count = ARRAY_SIZE(instance_validation_layers);
3152 demo->enabled_layers[0] = "VK_LAYER_KHRONOS_validation";
3153 }
3154 free(instance_layers);
3155 }
3156
3157 if (!validation_found) {
3158 ERR_EXIT(
3159 "vkEnumerateInstanceLayerProperties failed to find required validation layer.\n\n"
3160 "Please look at the Getting Started guide for additional information.\n",
3161 "vkCreateInstance Failure");
3162 }
3163 }
3164
3165 /* Look for instance extensions */
3166 VkBool32 surfaceExtFound = 0;
3167 VkBool32 platformSurfaceExtFound = 0;
3168 memset(demo->extension_names, 0, sizeof(demo->extension_names));
3169
3170 err = vkEnumerateInstanceExtensionProperties(NULL, &instance_extension_count, NULL);
3171 assert(!err);
3172
3173 if (instance_extension_count > 0) {
3174 VkExtensionProperties *instance_extensions = malloc(sizeof(VkExtensionProperties) * instance_extension_count);
3175 err = vkEnumerateInstanceExtensionProperties(NULL, &instance_extension_count, instance_extensions);
3176 assert(!err);
3177 for (uint32_t i = 0; i < instance_extension_count; i++) {
3178 if (!strcmp(VK_KHR_SURFACE_EXTENSION_NAME, instance_extensions[i].extensionName)) {
3179 surfaceExtFound = 1;
3180 demo->extension_names[demo->enabled_extension_count++] = VK_KHR_SURFACE_EXTENSION_NAME;
3181 }
3182 #if defined(VK_USE_PLATFORM_WIN32_KHR)
3183 if (!strcmp(VK_KHR_WIN32_SURFACE_EXTENSION_NAME, instance_extensions[i].extensionName)) {
3184 platformSurfaceExtFound = 1;
3185 demo->extension_names[demo->enabled_extension_count++] = VK_KHR_WIN32_SURFACE_EXTENSION_NAME;
3186 }
3187 #elif defined(VK_USE_PLATFORM_XLIB_KHR)
3188 if (!strcmp(VK_KHR_XLIB_SURFACE_EXTENSION_NAME, instance_extensions[i].extensionName)) {
3189 platformSurfaceExtFound = 1;
3190 demo->extension_names[demo->enabled_extension_count++] = VK_KHR_XLIB_SURFACE_EXTENSION_NAME;
3191 }
3192 #elif defined(VK_USE_PLATFORM_XCB_KHR)
3193 if (!strcmp(VK_KHR_XCB_SURFACE_EXTENSION_NAME, instance_extensions[i].extensionName)) {
3194 platformSurfaceExtFound = 1;
3195 demo->extension_names[demo->enabled_extension_count++] = VK_KHR_XCB_SURFACE_EXTENSION_NAME;
3196 }
3197 #elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
3198 if (!strcmp(VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME, instance_extensions[i].extensionName)) {
3199 platformSurfaceExtFound = 1;
3200 demo->extension_names[demo->enabled_extension_count++] = VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME;
3201 }
3202 #elif defined(VK_USE_PLATFORM_DIRECTFB_EXT)
3203 if (!strcmp(VK_EXT_DIRECTFB_SURFACE_EXTENSION_NAME, instance_extensions[i].extensionName)) {
3204 platformSurfaceExtFound = 1;
3205 demo->extension_names[demo->enabled_extension_count++] = VK_EXT_DIRECTFB_SURFACE_EXTENSION_NAME;
3206 }
3207 #elif defined(VK_USE_PLATFORM_DISPLAY_KHR)
3208 if (!strcmp(VK_KHR_DISPLAY_EXTENSION_NAME, instance_extensions[i].extensionName)) {
3209 platformSurfaceExtFound = 1;
3210 demo->extension_names[demo->enabled_extension_count++] = VK_KHR_DISPLAY_EXTENSION_NAME;
3211 }
3212 #elif defined(VK_USE_PLATFORM_ANDROID_KHR)
3213 if (!strcmp(VK_KHR_ANDROID_SURFACE_EXTENSION_NAME, instance_extensions[i].extensionName)) {
3214 platformSurfaceExtFound = 1;
3215 demo->extension_names[demo->enabled_extension_count++] = VK_KHR_ANDROID_SURFACE_EXTENSION_NAME;
3216 }
3217 #elif defined(VK_USE_PLATFORM_METAL_EXT)
3218 if (!strcmp(VK_EXT_METAL_SURFACE_EXTENSION_NAME, instance_extensions[i].extensionName)) {
3219 platformSurfaceExtFound = 1;
3220 demo->extension_names[demo->enabled_extension_count++] = VK_EXT_METAL_SURFACE_EXTENSION_NAME;
3221 }
3222 #endif
3223 if (!strcmp(VK_KHR_GET_PHYSICAL_DEVICE_PROPERTIES_2_EXTENSION_NAME, instance_extensions[i].extensionName)) {
3224 demo->extension_names[demo->enabled_extension_count++] = VK_KHR_GET_PHYSICAL_DEVICE_PROPERTIES_2_EXTENSION_NAME;
3225 }
3226 if (!strcmp(VK_EXT_DEBUG_UTILS_EXTENSION_NAME, instance_extensions[i].extensionName)) {
3227 if (demo->validate) {
3228 demo->extension_names[demo->enabled_extension_count++] = VK_EXT_DEBUG_UTILS_EXTENSION_NAME;
3229 }
3230 }
3231 assert(demo->enabled_extension_count < 64);
3232 }
3233
3234 free(instance_extensions);
3235 }
3236
3237 if (!surfaceExtFound) {
3238 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find the " VK_KHR_SURFACE_EXTENSION_NAME
3239 " extension.\n\n"
3240 "Do you have a compatible Vulkan installable client driver (ICD) installed?\n"
3241 "Please look at the Getting Started guide for additional information.\n",
3242 "vkCreateInstance Failure");
3243 }
3244 if (!platformSurfaceExtFound) {
3245 #if defined(VK_USE_PLATFORM_WIN32_KHR)
3246 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find the " VK_KHR_WIN32_SURFACE_EXTENSION_NAME
3247 " extension.\n\n"
3248 "Do you have a compatible Vulkan installable client driver (ICD) installed?\n"
3249 "Please look at the Getting Started guide for additional information.\n",
3250 "vkCreateInstance Failure");
3251 #elif defined(VK_USE_PLATFORM_METAL_EXT)
3252 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find the " VK_EXT_METAL_SURFACE_EXTENSION_NAME
3253 " extension.\n\n"
3254 "Do you have a compatible Vulkan installable client driver (ICD) installed?\n"
3255 "Please look at the Getting Started guide for additional information.\n",
3256 "vkCreateInstance Failure");
3257 #elif defined(VK_USE_PLATFORM_XCB_KHR)
3258 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find the " VK_KHR_XCB_SURFACE_EXTENSION_NAME
3259 " extension.\n\n"
3260 "Do you have a compatible Vulkan installable client driver (ICD) installed?\n"
3261 "Please look at the Getting Started guide for additional information.\n",
3262 "vkCreateInstance Failure");
3263 #elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
3264 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find the " VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME
3265 " extension.\n\n"
3266 "Do you have a compatible Vulkan installable client driver (ICD) installed?\n"
3267 "Please look at the Getting Started guide for additional information.\n",
3268 "vkCreateInstance Failure");
3269 #elif defined(VK_USE_PLATFORM_DISPLAY_KHR)
3270 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find the " VK_KHR_DISPLAY_EXTENSION_NAME
3271 " extension.\n\n"
3272 "Do you have a compatible Vulkan installable client driver (ICD) installed?\n"
3273 "Please look at the Getting Started guide for additional information.\n",
3274 "vkCreateInstance Failure");
3275 #elif defined(VK_USE_PLATFORM_ANDROID_KHR)
3276 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find the " VK_KHR_ANDROID_SURFACE_EXTENSION_NAME
3277 " extension.\n\n"
3278 "Do you have a compatible Vulkan installable client driver (ICD) installed?\n"
3279 "Please look at the Getting Started guide for additional information.\n",
3280 "vkCreateInstance Failure");
3281 #elif defined(VK_USE_PLATFORM_XLIB_KHR)
3282 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find the " VK_KHR_XLIB_SURFACE_EXTENSION_NAME
3283 " extension.\n\n"
3284 "Do you have a compatible Vulkan installable client driver (ICD) installed?\n"
3285 "Please look at the Getting Started guide for additional information.\n",
3286 "vkCreateInstance Failure");
3287 #elif defined(VK_USE_PLATFORM_DIRECTFB_EXT)
3288 ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find the " VK_EXT_DIRECTFB_SURFACE_EXTENSION_NAME
3289 " extension.\n\n"
3290 "Do you have a compatible Vulkan installable client driver (ICD) installed?\n"
3291 "Please look at the Getting Started guide for additional information.\n",
3292 "vkCreateInstance Failure");
3293 #endif
3294 }
3295 const VkApplicationInfo app = {
3296 .sType = VK_STRUCTURE_TYPE_APPLICATION_INFO,
3297 .pNext = NULL,
3298 .pApplicationName = APP_SHORT_NAME,
3299 .applicationVersion = 0,
3300 .pEngineName = APP_SHORT_NAME,
3301 .engineVersion = 0,
3302 .apiVersion = VK_API_VERSION_1_0,
3303 };
3304 VkInstanceCreateInfo inst_info = {
3305 .sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
3306 .pNext = NULL,
3307 .pApplicationInfo = &app,
3308 .enabledLayerCount = demo->enabled_layer_count,
3309 .ppEnabledLayerNames = (const char *const *)instance_validation_layers,
3310 .enabledExtensionCount = demo->enabled_extension_count,
3311 .ppEnabledExtensionNames = (const char *const *)demo->extension_names,
3312 };
3313
3314 /*
3315 * This is info for a temp callback to use during CreateInstance.
3316 * After the instance is created, we use the instance-based
3317 * function to register the final callback.
3318 */
3319 VkDebugUtilsMessengerCreateInfoEXT dbg_messenger_create_info;
3320 if (demo->validate) {
3321 // VK_EXT_debug_utils style
3322 dbg_messenger_create_info.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_MESSENGER_CREATE_INFO_EXT;
3323 dbg_messenger_create_info.pNext = NULL;
3324 dbg_messenger_create_info.flags = 0;
3325 dbg_messenger_create_info.messageSeverity =
3326 VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT | VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT;
3327 dbg_messenger_create_info.messageType = VK_DEBUG_UTILS_MESSAGE_TYPE_GENERAL_BIT_EXT |
3328 VK_DEBUG_UTILS_MESSAGE_TYPE_VALIDATION_BIT_EXT |
3329 VK_DEBUG_UTILS_MESSAGE_TYPE_PERFORMANCE_BIT_EXT;
3330 dbg_messenger_create_info.pfnUserCallback = debug_messenger_callback;
3331 dbg_messenger_create_info.pUserData = demo;
3332 inst_info.pNext = &dbg_messenger_create_info;
3333 }
3334
3335 err = vkCreateInstance(&inst_info, NULL, &demo->inst);
3336 if (err == VK_ERROR_INCOMPATIBLE_DRIVER) {
3337 ERR_EXIT(
3338 "Cannot find a compatible Vulkan installable client driver (ICD).\n\n"
3339 "Please look at the Getting Started guide for additional information.\n",
3340 "vkCreateInstance Failure");
3341 } else if (err == VK_ERROR_EXTENSION_NOT_PRESENT) {
3342 ERR_EXIT(
3343 "Cannot find a specified extension library.\n"
3344 "Make sure your layers path is set appropriately.\n",
3345 "vkCreateInstance Failure");
3346 } else if (err) {
3347 ERR_EXIT(
3348 "vkCreateInstance failed.\n\n"
3349 "Do you have a compatible Vulkan installable client driver (ICD) installed?\n"
3350 "Please look at the Getting Started guide for additional information.\n",
3351 "vkCreateInstance Failure");
3352 }
3353
3354 /* Make initial call to query gpu_count, then second call for gpu info */
3355 uint32_t gpu_count = 0;
3356 err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, NULL);
3357 assert(!err);
3358
3359 if (gpu_count <= 0) {
3360 ERR_EXIT(
3361 "vkEnumeratePhysicalDevices reported zero accessible devices.\n\n"
3362 "Do you have a compatible Vulkan installable client driver (ICD) installed?\n"
3363 "Please look at the Getting Started guide for additional information.\n",
3364 "vkEnumeratePhysicalDevices Failure");
3365 }
3366
3367 VkPhysicalDevice *physical_devices = malloc(sizeof(VkPhysicalDevice) * gpu_count);
3368 err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, physical_devices);
3369 assert(!err);
3370 if (demo->gpu_number >= 0 && !((uint32_t)demo->gpu_number < gpu_count)) {
3371 fprintf(stderr, "GPU %d specified is not present, GPU count = %u\n", demo->gpu_number, gpu_count);
3372 ERR_EXIT("Specified GPU number is not present", "User Error");
3373 }
3374
3375 #if defined(VK_USE_PLATFORM_DISPLAY_KHR)
3376 demo->gpu_number = find_display_gpu(demo->gpu_number, gpu_count, physical_devices);
3377 if (demo->gpu_number < 0) {
3378 printf("Cannot find any display!\n");
3379 fflush(stdout);
3380 exit(1);
3381 }
3382 #else
3383 /* Try to auto select most suitable device */
3384 if (demo->gpu_number == -1) {
3385 uint32_t count_device_type[VK_PHYSICAL_DEVICE_TYPE_CPU + 1];
3386 memset(count_device_type, 0, sizeof(count_device_type));
3387
3388 VkPhysicalDeviceProperties physicalDeviceProperties;
3389 for (uint32_t i = 0; i < gpu_count; i++) {
3390 vkGetPhysicalDeviceProperties(physical_devices[i], &physicalDeviceProperties);
3391 assert(physicalDeviceProperties.deviceType <= VK_PHYSICAL_DEVICE_TYPE_CPU);
3392 count_device_type[physicalDeviceProperties.deviceType]++;
3393 }
3394
3395 VkPhysicalDeviceType search_for_device_type = VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU;
3396 if (count_device_type[VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU]) {
3397 search_for_device_type = VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU;
3398 } else if (count_device_type[VK_PHYSICAL_DEVICE_TYPE_INTEGRATED_GPU]) {
3399 search_for_device_type = VK_PHYSICAL_DEVICE_TYPE_INTEGRATED_GPU;
3400 } else if (count_device_type[VK_PHYSICAL_DEVICE_TYPE_VIRTUAL_GPU]) {
3401 search_for_device_type = VK_PHYSICAL_DEVICE_TYPE_VIRTUAL_GPU;
3402 } else if (count_device_type[VK_PHYSICAL_DEVICE_TYPE_CPU]) {
3403 search_for_device_type = VK_PHYSICAL_DEVICE_TYPE_CPU;
3404 } else if (count_device_type[VK_PHYSICAL_DEVICE_TYPE_OTHER]) {
3405 search_for_device_type = VK_PHYSICAL_DEVICE_TYPE_OTHER;
3406 }
3407
3408 for (uint32_t i = 0; i < gpu_count; i++) {
3409 vkGetPhysicalDeviceProperties(physical_devices[i], &physicalDeviceProperties);
3410 if (physicalDeviceProperties.deviceType == search_for_device_type) {
3411 demo->gpu_number = i;
3412 break;
3413 }
3414 }
3415 }
3416 #endif
3417 assert(demo->gpu_number >= 0);
3418 demo->gpu = physical_devices[demo->gpu_number];
3419 {
3420 VkPhysicalDeviceProperties physicalDeviceProperties;
3421 vkGetPhysicalDeviceProperties(demo->gpu, &physicalDeviceProperties);
3422 fprintf(stderr, "Selected GPU %d: %s, type: %u\n", demo->gpu_number, physicalDeviceProperties.deviceName,
3423 physicalDeviceProperties.deviceType);
3424 }
3425 free(physical_devices);
3426
3427 /* Look for device extensions */
3428 uint32_t device_extension_count = 0;
3429 VkBool32 swapchainExtFound = 0;
3430 demo->enabled_extension_count = 0;
3431 memset(demo->extension_names, 0, sizeof(demo->extension_names));
3432
3433 err = vkEnumerateDeviceExtensionProperties(demo->gpu, NULL, &device_extension_count, NULL);
3434 assert(!err);
3435
3436 if (device_extension_count > 0) {
3437 VkExtensionProperties *device_extensions = malloc(sizeof(VkExtensionProperties) * device_extension_count);
3438 err = vkEnumerateDeviceExtensionProperties(demo->gpu, NULL, &device_extension_count, device_extensions);
3439 assert(!err);
3440
3441 for (uint32_t i = 0; i < device_extension_count; i++) {
3442 if (!strcmp(VK_KHR_SWAPCHAIN_EXTENSION_NAME, device_extensions[i].extensionName)) {
3443 swapchainExtFound = 1;
3444 demo->extension_names[demo->enabled_extension_count++] = VK_KHR_SWAPCHAIN_EXTENSION_NAME;
3445 }
3446 if (!strcmp("VK_KHR_portability_subset", device_extensions[i].extensionName)) {
3447 demo->extension_names[demo->enabled_extension_count++] = "VK_KHR_portability_subset";
3448 }
3449 assert(demo->enabled_extension_count < 64);
3450 }
3451
3452 if (demo->VK_KHR_incremental_present_enabled) {
3453 // Even though the user "enabled" the extension via the command
3454 // line, we must make sure that it's enumerated for use with the
3455 // device. Therefore, disable it here, and re-enable it again if
3456 // enumerated.
3457 demo->VK_KHR_incremental_present_enabled = false;
3458 for (uint32_t i = 0; i < device_extension_count; i++) {
3459 if (!strcmp(VK_KHR_INCREMENTAL_PRESENT_EXTENSION_NAME, device_extensions[i].extensionName)) {
3460 demo->extension_names[demo->enabled_extension_count++] = VK_KHR_INCREMENTAL_PRESENT_EXTENSION_NAME;
3461 demo->VK_KHR_incremental_present_enabled = true;
3462 DbgMsg("VK_KHR_incremental_present extension enabled\n");
3463 }
3464 assert(demo->enabled_extension_count < 64);
3465 }
3466 if (!demo->VK_KHR_incremental_present_enabled) {
3467 DbgMsg("VK_KHR_incremental_present extension NOT AVAILABLE\n");
3468 }
3469 }
3470
3471 if (demo->VK_GOOGLE_display_timing_enabled) {
3472 // Even though the user "enabled" the extension via the command
3473 // line, we must make sure that it's enumerated for use with the
3474 // device. Therefore, disable it here, and re-enable it again if
3475 // enumerated.
3476 demo->VK_GOOGLE_display_timing_enabled = false;
3477 for (uint32_t i = 0; i < device_extension_count; i++) {
3478 if (!strcmp(VK_GOOGLE_DISPLAY_TIMING_EXTENSION_NAME, device_extensions[i].extensionName)) {
3479 demo->extension_names[demo->enabled_extension_count++] = VK_GOOGLE_DISPLAY_TIMING_EXTENSION_NAME;
3480 demo->VK_GOOGLE_display_timing_enabled = true;
3481 DbgMsg("VK_GOOGLE_display_timing extension enabled\n");
3482 }
3483 assert(demo->enabled_extension_count < 64);
3484 }
3485 if (!demo->VK_GOOGLE_display_timing_enabled) {
3486 DbgMsg("VK_GOOGLE_display_timing extension NOT AVAILABLE\n");
3487 }
3488 }
3489
3490 free(device_extensions);
3491 }
3492
3493 if (!swapchainExtFound) {
3494 ERR_EXIT("vkEnumerateDeviceExtensionProperties failed to find the " VK_KHR_SWAPCHAIN_EXTENSION_NAME
3495 " extension.\n\nDo you have a compatible Vulkan installable client driver (ICD) installed?\n"
3496 "Please look at the Getting Started guide for additional information.\n",
3497 "vkCreateInstance Failure");
3498 }
3499
3500 if (demo->validate) {
3501 // Setup VK_EXT_debug_utils function pointers always (we use them for
3502 // debug labels and names).
3503 demo->CreateDebugUtilsMessengerEXT =
3504 (PFN_vkCreateDebugUtilsMessengerEXT)vkGetInstanceProcAddr(demo->inst, "vkCreateDebugUtilsMessengerEXT");
3505 demo->DestroyDebugUtilsMessengerEXT =
3506 (PFN_vkDestroyDebugUtilsMessengerEXT)vkGetInstanceProcAddr(demo->inst, "vkDestroyDebugUtilsMessengerEXT");
3507 demo->SubmitDebugUtilsMessageEXT =
3508 (PFN_vkSubmitDebugUtilsMessageEXT)vkGetInstanceProcAddr(demo->inst, "vkSubmitDebugUtilsMessageEXT");
3509 demo->CmdBeginDebugUtilsLabelEXT =
3510 (PFN_vkCmdBeginDebugUtilsLabelEXT)vkGetInstanceProcAddr(demo->inst, "vkCmdBeginDebugUtilsLabelEXT");
3511 demo->CmdEndDebugUtilsLabelEXT =
3512 (PFN_vkCmdEndDebugUtilsLabelEXT)vkGetInstanceProcAddr(demo->inst, "vkCmdEndDebugUtilsLabelEXT");
3513 demo->CmdInsertDebugUtilsLabelEXT =
3514 (PFN_vkCmdInsertDebugUtilsLabelEXT)vkGetInstanceProcAddr(demo->inst, "vkCmdInsertDebugUtilsLabelEXT");
3515 demo->SetDebugUtilsObjectNameEXT =
3516 (PFN_vkSetDebugUtilsObjectNameEXT)vkGetInstanceProcAddr(demo->inst, "vkSetDebugUtilsObjectNameEXT");
3517 if (NULL == demo->CreateDebugUtilsMessengerEXT || NULL == demo->DestroyDebugUtilsMessengerEXT ||
3518 NULL == demo->SubmitDebugUtilsMessageEXT || NULL == demo->CmdBeginDebugUtilsLabelEXT ||
3519 NULL == demo->CmdEndDebugUtilsLabelEXT || NULL == demo->CmdInsertDebugUtilsLabelEXT ||
3520 NULL == demo->SetDebugUtilsObjectNameEXT) {
3521 ERR_EXIT("GetProcAddr: Failed to init VK_EXT_debug_utils\n", "GetProcAddr: Failure");
3522 }
3523
3524 err = demo->CreateDebugUtilsMessengerEXT(demo->inst, &dbg_messenger_create_info, NULL, &demo->dbg_messenger);
3525 switch (err) {
3526 case VK_SUCCESS:
3527 break;
3528 case VK_ERROR_OUT_OF_HOST_MEMORY:
3529 ERR_EXIT("CreateDebugUtilsMessengerEXT: out of host memory\n", "CreateDebugUtilsMessengerEXT Failure");
3530 break;
3531 default:
3532 ERR_EXIT("CreateDebugUtilsMessengerEXT: unknown failure\n", "CreateDebugUtilsMessengerEXT Failure");
3533 break;
3534 }
3535 }
3536 vkGetPhysicalDeviceProperties(demo->gpu, &demo->gpu_props);
3537
3538 /* Call with NULL data to get count */
3539 vkGetPhysicalDeviceQueueFamilyProperties(demo->gpu, &demo->queue_family_count, NULL);
3540 assert(demo->queue_family_count >= 1);
3541
3542 demo->queue_props = (VkQueueFamilyProperties *)malloc(demo->queue_family_count * sizeof(VkQueueFamilyProperties));
3543 vkGetPhysicalDeviceQueueFamilyProperties(demo->gpu, &demo->queue_family_count, demo->queue_props);
3544
3545 // Query fine-grained feature support for this device.
3546 // If app has specific feature requirements it should check supported
3547 // features based on this query
3548 VkPhysicalDeviceFeatures physDevFeatures;
3549 vkGetPhysicalDeviceFeatures(demo->gpu, &physDevFeatures);
3550
3551 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfaceSupportKHR);
3552 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfaceCapabilitiesKHR);
3553 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfaceFormatsKHR);
3554 GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfacePresentModesKHR);
3555 GET_INSTANCE_PROC_ADDR(demo->inst, GetSwapchainImagesKHR);
3556 }
3557
demo_create_device(struct demo * demo)3558 static void demo_create_device(struct demo *demo) {
3559 VkResult U_ASSERT_ONLY err;
3560 float queue_priorities[1] = {0.0};
3561 VkDeviceQueueCreateInfo queues[2];
3562 queues[0].sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
3563 queues[0].pNext = NULL;
3564 queues[0].queueFamilyIndex = demo->graphics_queue_family_index;
3565 queues[0].queueCount = 1;
3566 queues[0].pQueuePriorities = queue_priorities;
3567 queues[0].flags = 0;
3568
3569 VkDeviceCreateInfo device = {
3570 .sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
3571 .pNext = NULL,
3572 .queueCreateInfoCount = 1,
3573 .pQueueCreateInfos = queues,
3574 .enabledLayerCount = 0,
3575 .ppEnabledLayerNames = NULL,
3576 .enabledExtensionCount = demo->enabled_extension_count,
3577 .ppEnabledExtensionNames = (const char *const *)demo->extension_names,
3578 .pEnabledFeatures = NULL, // If specific features are required, pass them in here
3579 };
3580 if (demo->separate_present_queue) {
3581 queues[1].sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
3582 queues[1].pNext = NULL;
3583 queues[1].queueFamilyIndex = demo->present_queue_family_index;
3584 queues[1].queueCount = 1;
3585 queues[1].pQueuePriorities = queue_priorities;
3586 queues[1].flags = 0;
3587 device.queueCreateInfoCount = 2;
3588 }
3589 err = vkCreateDevice(demo->gpu, &device, NULL, &demo->device);
3590 assert(!err);
3591 }
3592
demo_create_surface(struct demo * demo)3593 static void demo_create_surface(struct demo *demo) {
3594 VkResult U_ASSERT_ONLY err;
3595
3596 // Create a WSI surface for the window:
3597 #if defined(VK_USE_PLATFORM_WIN32_KHR)
3598 VkWin32SurfaceCreateInfoKHR createInfo;
3599 createInfo.sType = VK_STRUCTURE_TYPE_WIN32_SURFACE_CREATE_INFO_KHR;
3600 createInfo.pNext = NULL;
3601 createInfo.flags = 0;
3602 createInfo.hinstance = demo->connection;
3603 createInfo.hwnd = demo->window;
3604
3605 err = vkCreateWin32SurfaceKHR(demo->inst, &createInfo, NULL, &demo->surface);
3606 #elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
3607 VkWaylandSurfaceCreateInfoKHR createInfo;
3608 createInfo.sType = VK_STRUCTURE_TYPE_WAYLAND_SURFACE_CREATE_INFO_KHR;
3609 createInfo.pNext = NULL;
3610 createInfo.flags = 0;
3611 createInfo.display = demo->display;
3612 createInfo.surface = demo->window;
3613
3614 err = vkCreateWaylandSurfaceKHR(demo->inst, &createInfo, NULL, &demo->surface);
3615 #elif defined(VK_USE_PLATFORM_ANDROID_KHR)
3616 VkAndroidSurfaceCreateInfoKHR createInfo;
3617 createInfo.sType = VK_STRUCTURE_TYPE_ANDROID_SURFACE_CREATE_INFO_KHR;
3618 createInfo.pNext = NULL;
3619 createInfo.flags = 0;
3620 createInfo.window = (struct ANativeWindow *)(demo->window);
3621
3622 err = vkCreateAndroidSurfaceKHR(demo->inst, &createInfo, NULL, &demo->surface);
3623 #elif defined(VK_USE_PLATFORM_XLIB_KHR)
3624 VkXlibSurfaceCreateInfoKHR createInfo;
3625 createInfo.sType = VK_STRUCTURE_TYPE_XLIB_SURFACE_CREATE_INFO_KHR;
3626 createInfo.pNext = NULL;
3627 createInfo.flags = 0;
3628 createInfo.dpy = demo->display;
3629 createInfo.window = demo->xlib_window;
3630
3631 err = vkCreateXlibSurfaceKHR(demo->inst, &createInfo, NULL, &demo->surface);
3632 #elif defined(VK_USE_PLATFORM_XCB_KHR)
3633 VkXcbSurfaceCreateInfoKHR createInfo;
3634 createInfo.sType = VK_STRUCTURE_TYPE_XCB_SURFACE_CREATE_INFO_KHR;
3635 createInfo.pNext = NULL;
3636 createInfo.flags = 0;
3637 createInfo.connection = demo->connection;
3638 createInfo.window = demo->xcb_window;
3639
3640 err = vkCreateXcbSurfaceKHR(demo->inst, &createInfo, NULL, &demo->surface);
3641 #elif defined(VK_USE_PLATFORM_DIRECTFB_EXT)
3642 VkDirectFBSurfaceCreateInfoEXT createInfo;
3643 createInfo.sType = VK_STRUCTURE_TYPE_DIRECTFB_SURFACE_CREATE_INFO_EXT;
3644 createInfo.pNext = NULL;
3645 createInfo.flags = 0;
3646 createInfo.dfb = demo->dfb;
3647 createInfo.surface = demo->window;
3648
3649 err = vkCreateDirectFBSurfaceEXT(demo->inst, &createInfo, NULL, &demo->surface);
3650 #elif defined(VK_USE_PLATFORM_DISPLAY_KHR)
3651 err = demo_create_display_surface(demo);
3652 #elif defined(VK_USE_PLATFORM_METAL_EXT)
3653 VkMetalSurfaceCreateInfoEXT surface;
3654 surface.sType = VK_STRUCTURE_TYPE_METAL_SURFACE_CREATE_INFO_EXT;
3655 surface.pNext = NULL;
3656 surface.flags = 0;
3657 surface.pLayer = demo->caMetalLayer;
3658
3659 err = vkCreateMetalSurfaceEXT(demo->inst, &surface, NULL, &demo->surface);
3660 #endif
3661 assert(!err);
3662 }
3663
pick_surface_format(const VkSurfaceFormatKHR * surfaceFormats,uint32_t count)3664 static VkSurfaceFormatKHR pick_surface_format(const VkSurfaceFormatKHR *surfaceFormats, uint32_t count) {
3665 // Prefer non-SRGB formats...
3666 for (uint32_t i = 0; i < count; i++) {
3667 const VkFormat format = surfaceFormats[i].format;
3668
3669 if (format == VK_FORMAT_R8G8B8A8_UNORM || format == VK_FORMAT_B8G8R8A8_UNORM ||
3670 format == VK_FORMAT_A2B10G10R10_UNORM_PACK32 || format == VK_FORMAT_A2R10G10B10_UNORM_PACK32 ||
3671 format == VK_FORMAT_R16G16B16A16_SFLOAT) {
3672 return surfaceFormats[i];
3673 }
3674 }
3675
3676 printf("Can't find our preferred formats... Falling back to first exposed format. Rendering may be incorrect.\n");
3677
3678 assert(count >= 1);
3679 return surfaceFormats[0];
3680 }
3681
demo_init_vk_swapchain(struct demo * demo)3682 static void demo_init_vk_swapchain(struct demo *demo) {
3683 VkResult U_ASSERT_ONLY err;
3684
3685 demo_create_surface(demo);
3686
3687 // Iterate over each queue to learn whether it supports presenting:
3688 VkBool32 *supportsPresent = (VkBool32 *)malloc(demo->queue_family_count * sizeof(VkBool32));
3689 for (uint32_t i = 0; i < demo->queue_family_count; i++) {
3690 demo->fpGetPhysicalDeviceSurfaceSupportKHR(demo->gpu, i, demo->surface, &supportsPresent[i]);
3691 }
3692
3693 // Search for a graphics and a present queue in the array of queue
3694 // families, try to find one that supports both
3695 uint32_t graphicsQueueFamilyIndex = UINT32_MAX;
3696 uint32_t presentQueueFamilyIndex = UINT32_MAX;
3697 for (uint32_t i = 0; i < demo->queue_family_count; i++) {
3698 if ((demo->queue_props[i].queueFlags & VK_QUEUE_GRAPHICS_BIT) != 0) {
3699 if (graphicsQueueFamilyIndex == UINT32_MAX) {
3700 graphicsQueueFamilyIndex = i;
3701 }
3702
3703 if (supportsPresent[i] == VK_TRUE) {
3704 graphicsQueueFamilyIndex = i;
3705 presentQueueFamilyIndex = i;
3706 break;
3707 }
3708 }
3709 }
3710
3711 if (presentQueueFamilyIndex == UINT32_MAX) {
3712 // If didn't find a queue that supports both graphics and present, then
3713 // find a separate present queue.
3714 for (uint32_t i = 0; i < demo->queue_family_count; ++i) {
3715 if (supportsPresent[i] == VK_TRUE) {
3716 presentQueueFamilyIndex = i;
3717 break;
3718 }
3719 }
3720 }
3721
3722 // Generate error if could not find both a graphics and a present queue
3723 if (graphicsQueueFamilyIndex == UINT32_MAX || presentQueueFamilyIndex == UINT32_MAX) {
3724 ERR_EXIT("Could not find both graphics and present queues\n", "Swapchain Initialization Failure");
3725 }
3726
3727 demo->graphics_queue_family_index = graphicsQueueFamilyIndex;
3728 demo->present_queue_family_index = presentQueueFamilyIndex;
3729 demo->separate_present_queue = (demo->graphics_queue_family_index != demo->present_queue_family_index);
3730 free(supportsPresent);
3731
3732 demo_create_device(demo);
3733
3734 GET_DEVICE_PROC_ADDR(demo->device, CreateSwapchainKHR);
3735 GET_DEVICE_PROC_ADDR(demo->device, DestroySwapchainKHR);
3736 GET_DEVICE_PROC_ADDR(demo->device, GetSwapchainImagesKHR);
3737 GET_DEVICE_PROC_ADDR(demo->device, AcquireNextImageKHR);
3738 GET_DEVICE_PROC_ADDR(demo->device, QueuePresentKHR);
3739 if (demo->VK_GOOGLE_display_timing_enabled) {
3740 GET_DEVICE_PROC_ADDR(demo->device, GetRefreshCycleDurationGOOGLE);
3741 GET_DEVICE_PROC_ADDR(demo->device, GetPastPresentationTimingGOOGLE);
3742 }
3743
3744 vkGetDeviceQueue(demo->device, demo->graphics_queue_family_index, 0, &demo->graphics_queue);
3745
3746 if (!demo->separate_present_queue) {
3747 demo->present_queue = demo->graphics_queue;
3748 } else {
3749 vkGetDeviceQueue(demo->device, demo->present_queue_family_index, 0, &demo->present_queue);
3750 }
3751
3752 // Get the list of VkFormat's that are supported:
3753 uint32_t formatCount;
3754 err = demo->fpGetPhysicalDeviceSurfaceFormatsKHR(demo->gpu, demo->surface, &formatCount, NULL);
3755 assert(!err);
3756 VkSurfaceFormatKHR *surfFormats = (VkSurfaceFormatKHR *)malloc(formatCount * sizeof(VkSurfaceFormatKHR));
3757 err = demo->fpGetPhysicalDeviceSurfaceFormatsKHR(demo->gpu, demo->surface, &formatCount, surfFormats);
3758 assert(!err);
3759 VkSurfaceFormatKHR surfaceFormat = pick_surface_format(surfFormats, formatCount);
3760 demo->format = surfaceFormat.format;
3761 demo->color_space = surfaceFormat.colorSpace;
3762 free(surfFormats);
3763
3764 demo->quit = false;
3765 demo->curFrame = 0;
3766
3767 // Create semaphores to synchronize acquiring presentable buffers before
3768 // rendering and waiting for drawing to be complete before presenting
3769 VkSemaphoreCreateInfo semaphoreCreateInfo = {
3770 .sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO,
3771 .pNext = NULL,
3772 .flags = 0,
3773 };
3774
3775 // Create fences that we can use to throttle if we get too far
3776 // ahead of the image presents
3777 VkFenceCreateInfo fence_ci = {
3778 .sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO, .pNext = NULL, .flags = VK_FENCE_CREATE_SIGNALED_BIT};
3779 for (uint32_t i = 0; i < FRAME_LAG; i++) {
3780 err = vkCreateFence(demo->device, &fence_ci, NULL, &demo->fences[i]);
3781 assert(!err);
3782
3783 err = vkCreateSemaphore(demo->device, &semaphoreCreateInfo, NULL, &demo->image_acquired_semaphores[i]);
3784 assert(!err);
3785
3786 err = vkCreateSemaphore(demo->device, &semaphoreCreateInfo, NULL, &demo->draw_complete_semaphores[i]);
3787 assert(!err);
3788
3789 if (demo->separate_present_queue) {
3790 err = vkCreateSemaphore(demo->device, &semaphoreCreateInfo, NULL, &demo->image_ownership_semaphores[i]);
3791 assert(!err);
3792 }
3793 }
3794 demo->frame_index = 0;
3795
3796 // Get Memory information and properties
3797 vkGetPhysicalDeviceMemoryProperties(demo->gpu, &demo->memory_properties);
3798 }
3799
3800 #if defined(VK_USE_PLATFORM_WAYLAND_KHR)
pointer_handle_enter(void * data,struct wl_pointer * pointer,uint32_t serial,struct wl_surface * surface,wl_fixed_t sx,wl_fixed_t sy)3801 static void pointer_handle_enter(void *data, struct wl_pointer *pointer, uint32_t serial, struct wl_surface *surface, wl_fixed_t sx,
3802 wl_fixed_t sy) {}
3803
pointer_handle_leave(void * data,struct wl_pointer * pointer,uint32_t serial,struct wl_surface * surface)3804 static void pointer_handle_leave(void *data, struct wl_pointer *pointer, uint32_t serial, struct wl_surface *surface) {}
3805
pointer_handle_motion(void * data,struct wl_pointer * pointer,uint32_t time,wl_fixed_t sx,wl_fixed_t sy)3806 static void pointer_handle_motion(void *data, struct wl_pointer *pointer, uint32_t time, wl_fixed_t sx, wl_fixed_t sy) {}
3807
pointer_handle_button(void * data,struct wl_pointer * wl_pointer,uint32_t serial,uint32_t time,uint32_t button,uint32_t state)3808 static void pointer_handle_button(void *data, struct wl_pointer *wl_pointer, uint32_t serial, uint32_t time, uint32_t button,
3809 uint32_t state) {
3810 struct demo *demo = data;
3811 if (button == BTN_LEFT && state == WL_POINTER_BUTTON_STATE_PRESSED) {
3812 xdg_toplevel_move(demo->xdg_toplevel, demo->seat, serial);
3813 }
3814 }
3815
pointer_handle_axis(void * data,struct wl_pointer * wl_pointer,uint32_t time,uint32_t axis,wl_fixed_t value)3816 static void pointer_handle_axis(void *data, struct wl_pointer *wl_pointer, uint32_t time, uint32_t axis, wl_fixed_t value) {}
3817
3818 static const struct wl_pointer_listener pointer_listener = {
3819 pointer_handle_enter, pointer_handle_leave, pointer_handle_motion, pointer_handle_button, pointer_handle_axis,
3820 };
3821
keyboard_handle_keymap(void * data,struct wl_keyboard * keyboard,uint32_t format,int fd,uint32_t size)3822 static void keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard, uint32_t format, int fd, uint32_t size) {}
3823
keyboard_handle_enter(void * data,struct wl_keyboard * keyboard,uint32_t serial,struct wl_surface * surface,struct wl_array * keys)3824 static void keyboard_handle_enter(void *data, struct wl_keyboard *keyboard, uint32_t serial, struct wl_surface *surface,
3825 struct wl_array *keys) {}
3826
keyboard_handle_leave(void * data,struct wl_keyboard * keyboard,uint32_t serial,struct wl_surface * surface)3827 static void keyboard_handle_leave(void *data, struct wl_keyboard *keyboard, uint32_t serial, struct wl_surface *surface) {}
3828
keyboard_handle_key(void * data,struct wl_keyboard * keyboard,uint32_t serial,uint32_t time,uint32_t key,uint32_t state)3829 static void keyboard_handle_key(void *data, struct wl_keyboard *keyboard, uint32_t serial, uint32_t time, uint32_t key,
3830 uint32_t state) {
3831 if (state != WL_KEYBOARD_KEY_STATE_RELEASED) return;
3832 struct demo *demo = data;
3833 switch (key) {
3834 case KEY_ESC: // Escape
3835 demo->quit = true;
3836 break;
3837 case KEY_LEFT: // left arrow key
3838 demo->spin_angle -= demo->spin_increment;
3839 break;
3840 case KEY_RIGHT: // right arrow key
3841 demo->spin_angle += demo->spin_increment;
3842 break;
3843 case KEY_SPACE: // space bar
3844 demo->pause = !demo->pause;
3845 break;
3846 }
3847 }
3848
keyboard_handle_modifiers(void * data,struct wl_keyboard * keyboard,uint32_t serial,uint32_t mods_depressed,uint32_t mods_latched,uint32_t mods_locked,uint32_t group)3849 static void keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard, uint32_t serial, uint32_t mods_depressed,
3850 uint32_t mods_latched, uint32_t mods_locked, uint32_t group) {}
3851
3852 static const struct wl_keyboard_listener keyboard_listener = {
3853 keyboard_handle_keymap, keyboard_handle_enter, keyboard_handle_leave, keyboard_handle_key, keyboard_handle_modifiers,
3854 };
3855
seat_handle_capabilities(void * data,struct wl_seat * seat,enum wl_seat_capability caps)3856 static void seat_handle_capabilities(void *data, struct wl_seat *seat, enum wl_seat_capability caps) {
3857 // Subscribe to pointer events
3858 struct demo *demo = data;
3859 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !demo->pointer) {
3860 demo->pointer = wl_seat_get_pointer(seat);
3861 wl_pointer_add_listener(demo->pointer, &pointer_listener, demo);
3862 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && demo->pointer) {
3863 wl_pointer_destroy(demo->pointer);
3864 demo->pointer = NULL;
3865 }
3866 // Subscribe to keyboard events
3867 if (caps & WL_SEAT_CAPABILITY_KEYBOARD) {
3868 demo->keyboard = wl_seat_get_keyboard(seat);
3869 wl_keyboard_add_listener(demo->keyboard, &keyboard_listener, demo);
3870 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD)) {
3871 wl_keyboard_destroy(demo->keyboard);
3872 demo->keyboard = NULL;
3873 }
3874 }
3875
3876 static const struct wl_seat_listener seat_listener = {
3877 seat_handle_capabilities,
3878 };
3879
wm_base_ping(void * data UNUSED,struct xdg_wm_base * xdg_wm_base,uint32_t serial)3880 static void wm_base_ping(void *data UNUSED, struct xdg_wm_base *xdg_wm_base, uint32_t serial) {
3881 xdg_wm_base_pong(xdg_wm_base, serial);
3882 }
3883
3884 static const struct xdg_wm_base_listener wm_base_listener = {wm_base_ping};
3885
registry_handle_global(void * data,struct wl_registry * registry,uint32_t id,const char * interface,uint32_t version UNUSED)3886 static void registry_handle_global(void *data, struct wl_registry *registry, uint32_t id, const char *interface,
3887 uint32_t version UNUSED) {
3888 struct demo *demo = data;
3889 // pickup wayland objects when they appear
3890 if (strcmp(interface, wl_compositor_interface.name) == 0) {
3891 uint32_t minVersion = version < 4 ? version : 4;
3892 demo->compositor = wl_registry_bind(registry, id, &wl_compositor_interface, minVersion);
3893 if (demo->VK_KHR_incremental_present_enabled && minVersion < 4) {
3894 fprintf(stderr, "Wayland compositor doesn't support VK_KHR_incremental_present, disabling.\n");
3895 demo->VK_KHR_incremental_present_enabled = false;
3896 }
3897 } else if (strcmp(interface, xdg_wm_base_interface.name) == 0) {
3898 demo->xdg_wm_base = wl_registry_bind(registry, id, &xdg_wm_base_interface, 1);
3899 xdg_wm_base_add_listener(demo->xdg_wm_base, &wm_base_listener, NULL);
3900 } else if (strcmp(interface, wl_seat_interface.name) == 0) {
3901 demo->seat = wl_registry_bind(registry, id, &wl_seat_interface, 1);
3902 wl_seat_add_listener(demo->seat, &seat_listener, demo);
3903 } else if (strcmp(interface, zxdg_decoration_manager_v1_interface.name) == 0) {
3904 demo->xdg_decoration_mgr = wl_registry_bind(registry, id, &zxdg_decoration_manager_v1_interface, 1);
3905 }
3906 }
3907
registry_handle_global_remove(void * data UNUSED,struct wl_registry * registry UNUSED,uint32_t name UNUSED)3908 static void registry_handle_global_remove(void *data UNUSED, struct wl_registry *registry UNUSED, uint32_t name UNUSED) {}
3909
3910 static const struct wl_registry_listener registry_listener = {registry_handle_global, registry_handle_global_remove};
3911 #endif
3912
demo_init_connection(struct demo * demo)3913 static void demo_init_connection(struct demo *demo) {
3914 #if defined(VK_USE_PLATFORM_XCB_KHR)
3915 const xcb_setup_t *setup;
3916 xcb_screen_iterator_t iter;
3917 int scr;
3918
3919 const char *display_envar = getenv("DISPLAY");
3920 if (display_envar == NULL || display_envar[0] == '\0') {
3921 printf("Environment variable DISPLAY requires a valid value.\nExiting ...\n");
3922 fflush(stdout);
3923 exit(1);
3924 }
3925
3926 demo->connection = xcb_connect(NULL, &scr);
3927 if (xcb_connection_has_error(demo->connection) > 0) {
3928 printf("Cannot connect to XCB.\nExiting ...\n");
3929 fflush(stdout);
3930 exit(1);
3931 }
3932
3933 setup = xcb_get_setup(demo->connection);
3934 iter = xcb_setup_roots_iterator(setup);
3935 while (scr-- > 0) xcb_screen_next(&iter);
3936
3937 demo->screen = iter.data;
3938 #elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
3939 demo->display = wl_display_connect(NULL);
3940
3941 if (demo->display == NULL) {
3942 printf("Cannot connect to wayland.\nExiting ...\n");
3943 fflush(stdout);
3944 exit(1);
3945 }
3946
3947 demo->registry = wl_display_get_registry(demo->display);
3948 wl_registry_add_listener(demo->registry, ®istry_listener, demo);
3949 wl_display_dispatch(demo->display);
3950 #endif
3951 }
3952
demo_init(struct demo * demo,int argc,char ** argv)3953 static void demo_init(struct demo *demo, int argc, char **argv) {
3954 vec3 eye = {0.0f, 3.0f, 5.0f};
3955 vec3 origin = {0, 0, 0};
3956 vec3 up = {0.0f, 1.0f, 0.0};
3957
3958 memset(demo, 0, sizeof(*demo));
3959 demo->presentMode = VK_PRESENT_MODE_FIFO_KHR;
3960 demo->frameCount = INT32_MAX;
3961 /* Autodetect suitable / best GPU by default */
3962 demo->gpu_number = -1;
3963 demo->width = 500;
3964 demo->height = 500;
3965
3966 for (int i = 1; i < argc; i++) {
3967 if (strcmp(argv[i], "--use_staging") == 0) {
3968 demo->use_staging_buffer = true;
3969 continue;
3970 }
3971 if ((strcmp(argv[i], "--present_mode") == 0) && (i < argc - 1)) {
3972 demo->presentMode = atoi(argv[i + 1]);
3973 i++;
3974 continue;
3975 }
3976 if (strcmp(argv[i], "--break") == 0) {
3977 demo->use_break = true;
3978 continue;
3979 }
3980 if (strcmp(argv[i], "--validate") == 0) {
3981 demo->validate = true;
3982 continue;
3983 }
3984 if (strcmp(argv[i], "--validate-checks-disabled") == 0) {
3985 demo->validate = true;
3986 demo->validate_checks_disabled = true;
3987 continue;
3988 }
3989 if (strcmp(argv[i], "--xlib") == 0) {
3990 fprintf(stderr, "--xlib is deprecated and no longer does anything");
3991 continue;
3992 }
3993 if (strcmp(argv[i], "--c") == 0 && demo->frameCount == INT32_MAX && i < argc - 1 &&
3994 sscanf(argv[i + 1], "%d", &demo->frameCount) == 1 && demo->frameCount >= 0) {
3995 i++;
3996 continue;
3997 }
3998 if (strcmp(argv[i], "--width") == 0 && i < argc - 1 && sscanf(argv[i + 1], "%d", &demo->width) == 1 && demo->width > 0) {
3999 i++;
4000 continue;
4001 }
4002 if (strcmp(argv[i], "--height") == 0 && i < argc - 1 && sscanf(argv[i + 1], "%d", &demo->height) == 1 && demo->height > 0) {
4003 i++;
4004 continue;
4005 }
4006 if (strcmp(argv[i], "--suppress_popups") == 0) {
4007 demo->suppress_popups = true;
4008 continue;
4009 }
4010 if (strcmp(argv[i], "--display_timing") == 0) {
4011 demo->VK_GOOGLE_display_timing_enabled = true;
4012 continue;
4013 }
4014 if (strcmp(argv[i], "--incremental_present") == 0) {
4015 demo->VK_KHR_incremental_present_enabled = true;
4016 continue;
4017 }
4018 if ((strcmp(argv[i], "--gpu_number") == 0) && (i < argc - 1)) {
4019 demo->gpu_number = atoi(argv[i + 1]);
4020 assert(demo->gpu_number >= 0);
4021 i++;
4022 continue;
4023 }
4024 if (strcmp(argv[i], "--force_errors") == 0) {
4025 demo->force_errors = true;
4026 continue;
4027 }
4028
4029 #if defined(ANDROID)
4030 ERR_EXIT("Usage: vkcube [--validate]\n", "Usage");
4031 #else
4032 char *message =
4033 "Usage:\n %s\t[--use_staging] [--validate] [--validate-checks-disabled]\n"
4034 "\t[--break] [--c <framecount>] [--suppress_popups]\n"
4035 "\t[--incremental_present] [--display_timing]\n"
4036 "\t[--gpu_number <index of physical device>]\n"
4037 "\t[--present_mode <present mode enum>]\n"
4038 "\t[--width <width>] [--height <height>]\n"
4039 "\t[--force_errors]\n"
4040 "\t<present_mode_enum>\n"
4041 "\t\tVK_PRESENT_MODE_IMMEDIATE_KHR = %d\n"
4042 "\t\tVK_PRESENT_MODE_MAILBOX_KHR = %d\n"
4043 "\t\tVK_PRESENT_MODE_FIFO_KHR = %d\n"
4044 "\t\tVK_PRESENT_MODE_FIFO_RELAXED_KHR = %d\n";
4045 int length = snprintf(NULL, 0, message, APP_SHORT_NAME, VK_PRESENT_MODE_IMMEDIATE_KHR, VK_PRESENT_MODE_MAILBOX_KHR,
4046 VK_PRESENT_MODE_FIFO_KHR, VK_PRESENT_MODE_FIFO_RELAXED_KHR);
4047 char *usage = (char *)malloc(length + 1);
4048 if (!usage) {
4049 exit(1);
4050 }
4051 snprintf(usage, length + 1, message, APP_SHORT_NAME, VK_PRESENT_MODE_IMMEDIATE_KHR, VK_PRESENT_MODE_MAILBOX_KHR,
4052 VK_PRESENT_MODE_FIFO_KHR, VK_PRESENT_MODE_FIFO_RELAXED_KHR);
4053 #if defined(_WIN32)
4054 if (!demo->suppress_popups) MessageBox(NULL, usage, "Usage Error", MB_OK);
4055 #else
4056 fprintf(stderr, "%s", usage);
4057 fflush(stderr);
4058 #endif
4059 free(usage);
4060 exit(1);
4061 #endif
4062 }
4063
4064 demo_init_connection(demo);
4065
4066 demo_init_vk(demo);
4067
4068 demo->spin_angle = 4.0f;
4069 demo->spin_increment = 0.2f;
4070 demo->pause = false;
4071
4072 mat4x4_perspective(demo->projection_matrix, (float)degreesToRadians(45.0f), 1.0f, 0.1f, 100.0f);
4073 mat4x4_look_at(demo->view_matrix, eye, origin, up);
4074 mat4x4_identity(demo->model_matrix);
4075
4076 demo->projection_matrix[1][1] *= -1; // Flip projection matrix from GL to Vulkan orientation.
4077 }
4078
4079 #if defined(VK_USE_PLATFORM_WIN32_KHR)
4080 // Include header required for parsing the command line options.
4081 #include <shellapi.h>
4082
WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR pCmdLine,int nCmdShow)4083 int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR pCmdLine, int nCmdShow) {
4084 MSG msg; // message
4085 bool done; // flag saying when app is complete
4086 int argc;
4087 char **argv;
4088
4089 // Ensure wParam is initialized.
4090 msg.wParam = 0;
4091
4092 // Use the CommandLine functions to get the command line arguments.
4093 // Unfortunately, Microsoft outputs
4094 // this information as wide characters for Unicode, and we simply want the
4095 // Ascii version to be compatible
4096 // with the non-Windows side. So, we have to convert the information to
4097 // Ascii character strings.
4098 LPWSTR *commandLineArgs = CommandLineToArgvW(GetCommandLineW(), &argc);
4099 if (NULL == commandLineArgs) {
4100 argc = 0;
4101 }
4102
4103 if (argc > 0) {
4104 argv = (char **)malloc(sizeof(char *) * argc);
4105 if (argv == NULL) {
4106 argc = 0;
4107 } else {
4108 for (int iii = 0; iii < argc; iii++) {
4109 size_t wideCharLen = wcslen(commandLineArgs[iii]);
4110 size_t numConverted = 0;
4111
4112 argv[iii] = (char *)malloc(sizeof(char) * (wideCharLen + 1));
4113 if (argv[iii] != NULL) {
4114 wcstombs_s(&numConverted, argv[iii], wideCharLen + 1, commandLineArgs[iii], wideCharLen + 1);
4115 }
4116 }
4117 }
4118 } else {
4119 argv = NULL;
4120 }
4121
4122 demo_init(&demo, argc, argv);
4123
4124 // Free up the items we had to allocate for the command line arguments.
4125 if (argc > 0 && argv != NULL) {
4126 for (int iii = 0; iii < argc; iii++) {
4127 if (argv[iii] != NULL) {
4128 free(argv[iii]);
4129 }
4130 }
4131 free(argv);
4132 }
4133
4134 demo.connection = hInstance;
4135 strncpy(demo.name, "Vulkan Cube", APP_NAME_STR_LEN);
4136 demo_create_window(&demo);
4137 demo_init_vk_swapchain(&demo);
4138
4139 demo_prepare(&demo);
4140
4141 done = false; // initialize loop condition variable
4142
4143 // main message loop
4144 while (!done) {
4145 if (demo.pause) {
4146 const BOOL succ = WaitMessage();
4147
4148 if (!succ) {
4149 struct demo *tmp = &demo;
4150 struct demo *demo = tmp;
4151 ERR_EXIT("WaitMessage() failed on paused demo", "event loop error");
4152 }
4153 }
4154 PeekMessage(&msg, NULL, 0, 0, PM_REMOVE);
4155 if (msg.message == WM_QUIT) // check for a quit message
4156 {
4157 done = true; // if found, quit app
4158 } else {
4159 /* Translate and dispatch to event queue*/
4160 TranslateMessage(&msg);
4161 DispatchMessage(&msg);
4162 }
4163 RedrawWindow(demo.window, NULL, NULL, RDW_INTERNALPAINT);
4164 }
4165
4166 demo_cleanup(&demo);
4167
4168 return (int)msg.wParam;
4169 }
4170
4171 #elif defined(VK_USE_PLATFORM_METAL_EXT)
demo_main(struct demo * demo,void * caMetalLayer,int argc,const char * argv[])4172 static void demo_main(struct demo *demo, void *caMetalLayer, int argc, const char *argv[]) {
4173 demo_init(demo, argc, (char **)argv);
4174 demo->caMetalLayer = caMetalLayer;
4175 demo_init_vk_swapchain(demo);
4176 demo_prepare(demo);
4177 demo->spin_angle = 0.4f;
4178 }
4179
4180 #elif defined(VK_USE_PLATFORM_ANDROID_KHR)
4181 #include <android/log.h>
4182 #include <android_native_app_glue.h>
4183 #include "android_util.h"
4184
4185 static bool initialized = false;
4186 static bool active = false;
4187 struct demo demo;
4188
processInput(struct android_app * app,AInputEvent * event)4189 static int32_t processInput(struct android_app *app, AInputEvent *event) { return 0; }
4190
processCommand(struct android_app * app,int32_t cmd)4191 static void processCommand(struct android_app *app, int32_t cmd) {
4192 switch (cmd) {
4193 case APP_CMD_INIT_WINDOW: {
4194 if (app->window) {
4195 // We're getting a new window. If the app is starting up, we
4196 // need to initialize. If the app has already been
4197 // initialized, that means that we lost our previous window,
4198 // which means that we have a lot of work to do. At a minimum,
4199 // we need to destroy the swapchain and surface associated with
4200 // the old window, and create a new surface and swapchain.
4201 // However, since there are a lot of other objects/state that
4202 // is tied to the swapchain, it's easiest to simply cleanup and
4203 // start over (i.e. use a brute-force approach of re-starting
4204 // the app)
4205 if (demo.prepared) {
4206 demo_cleanup(&demo);
4207 }
4208
4209 // Parse Intents into argc, argv
4210 // Use the following key to send arguments, i.e.
4211 // --es args "--validate"
4212 const char key[] = "args";
4213 char *appTag = (char *)APP_SHORT_NAME;
4214 int argc = 0;
4215 char **argv = get_args(app, key, appTag, &argc);
4216
4217 __android_log_print(ANDROID_LOG_INFO, appTag, "argc = %i", argc);
4218 for (int i = 0; i < argc; i++) __android_log_print(ANDROID_LOG_INFO, appTag, "argv[%i] = %s", i, argv[i]);
4219
4220 demo_init(&demo, argc, argv);
4221
4222 // Free the argv malloc'd by get_args
4223 for (int i = 0; i < argc; i++) free(argv[i]);
4224
4225 demo.window = (void *)app->window;
4226 demo_init_vk_swapchain(&demo);
4227 demo_prepare(&demo);
4228 initialized = true;
4229 }
4230 break;
4231 }
4232 case APP_CMD_GAINED_FOCUS: {
4233 active = true;
4234 break;
4235 }
4236 case APP_CMD_LOST_FOCUS: {
4237 active = false;
4238 break;
4239 }
4240 }
4241 }
4242
android_main(struct android_app * app)4243 void android_main(struct android_app *app) {
4244 #ifdef ANDROID
4245 int vulkanSupport = InitVulkan();
4246 if (vulkanSupport == 0) return;
4247 #endif
4248
4249 demo.prepared = false;
4250
4251 app->onAppCmd = processCommand;
4252 app->onInputEvent = processInput;
4253
4254 while (1) {
4255 int events;
4256 struct android_poll_source *source;
4257 while (ALooper_pollAll(active ? 0 : -1, NULL, &events, (void **)&source) >= 0) {
4258 if (source) {
4259 source->process(app, source);
4260 }
4261
4262 if (app->destroyRequested != 0) {
4263 demo_cleanup(&demo);
4264 return;
4265 }
4266 }
4267 if (initialized && active) {
4268 demo_run(&demo);
4269 }
4270 }
4271 }
4272 #else
main(int argc,char ** argv)4273 int main(int argc, char **argv) {
4274 struct demo demo;
4275
4276 demo_init(&demo, argc, argv);
4277 #if defined(VK_USE_PLATFORM_XCB_KHR)
4278 demo_create_xcb_window(&demo);
4279 #elif defined(VK_USE_PLATFORM_XLIB_KHR)
4280 demo_create_xlib_window(&demo);
4281 #elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
4282 demo_create_window(&demo);
4283 #elif defined(VK_USE_PLATFORM_DIRECTFB_EXT)
4284 demo_create_directfb_window(&demo);
4285 #endif
4286
4287 demo_init_vk_swapchain(&demo);
4288
4289 demo_prepare(&demo);
4290
4291 #if defined(VK_USE_PLATFORM_XCB_KHR)
4292 demo_run_xcb(&demo);
4293 #elif defined(VK_USE_PLATFORM_XLIB_KHR)
4294 demo_run_xlib(&demo);
4295 #elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
4296 demo_run(&demo);
4297 #elif defined(VK_USE_PLATFORM_DIRECTFB_EXT)
4298 demo_run_directfb(&demo);
4299 #elif defined(VK_USE_PLATFORM_DISPLAY_KHR)
4300 demo_run_display(&demo);
4301 #endif
4302
4303 demo_cleanup(&demo);
4304
4305 return validation_error;
4306 }
4307 #endif
4308