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