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