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