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