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