Ian Elliott | a748eaf | 2015-04-28 15:50:36 -0600 | [diff] [blame] | 1 | /* |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 2 | * Copyright (c) 2015-2016 The Khronos Group Inc. |
| 3 | * Copyright (c) 2015-2016 Valve Corporation |
| 4 | * Copyright (c) 2015-2016 LunarG, Inc. |
Ian Elliott | a748eaf | 2015-04-28 15:50:36 -0600 | [diff] [blame] | 5 | * |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | * of this software and/or associated documentation files (the "Materials"), to |
| 8 | * deal in the Materials without restriction, including without limitation the |
| 9 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
| 10 | * sell copies of the Materials, and to permit persons to whom the Materials are |
| 11 | * furnished to do so, subject to the following conditions: |
Ian Elliott | a748eaf | 2015-04-28 15:50:36 -0600 | [diff] [blame] | 12 | * |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 13 | * The above copyright notice(s) and this permission notice shall be included in |
| 14 | * all copies or substantial portions of the Materials. |
Ian Elliott | a748eaf | 2015-04-28 15:50:36 -0600 | [diff] [blame] | 15 | * |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 16 | * THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
Ian Elliott | a748eaf | 2015-04-28 15:50:36 -0600 | [diff] [blame] | 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 19 | * |
| 20 | * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
| 21 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR |
| 22 | * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE MATERIALS OR THE |
| 23 | * USE OR OTHER DEALINGS IN THE MATERIALS. |
Courtney Goeltzenleuchter | 3732898 | 2015-10-30 11:14:30 -0600 | [diff] [blame] | 24 | * |
| 25 | * Author: Chia-I Wu <olv@lunarg.com> |
| 26 | * Author: Courtney Goeltzenleuchter <courtney@LunarG.com> |
| 27 | * Author: Ian Elliott <ian@LunarG.com> |
| 28 | * Author: Jon Ashburn <jon@lunarg.com> |
Ian Elliott | a748eaf | 2015-04-28 15:50:36 -0600 | [diff] [blame] | 29 | */ |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 30 | |
Courtney Goeltzenleuchter | 6836939 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 31 | #define _GNU_SOURCE |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 32 | #include <stdio.h> |
| 33 | #include <stdlib.h> |
| 34 | #include <string.h> |
| 35 | #include <stdbool.h> |
| 36 | #include <assert.h> |
Courtney Goeltzenleuchter | af1951c | 2015-12-01 14:11:38 -0700 | [diff] [blame] | 37 | #include <signal.h> |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 38 | |
Ian Elliott | 639ca47 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 39 | #ifdef _WIN32 |
| 40 | #pragma comment(linker, "/subsystem:windows") |
Ian Elliott | 639ca47 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 41 | #define APP_NAME_STR_LEN 80 |
Ian Elliott | 639ca47 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 42 | #endif // _WIN32 |
| 43 | |
David Pinedo | c5193c1 | 2015-11-06 12:54:48 -0700 | [diff] [blame] | 44 | #include <vulkan/vulkan.h> |
Ian Elliott | b08e0d9 | 2015-11-20 11:55:46 -0700 | [diff] [blame] | 45 | |
David Pinedo | 541526f | 2015-11-24 09:00:24 -0700 | [diff] [blame] | 46 | #include <vulkan/vk_sdk_platform.h> |
Courtney Goeltzenleuchter | fc3b953 | 2014-10-28 10:32:57 -0600 | [diff] [blame] | 47 | #include "linmath.h" |
| 48 | |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 49 | #define DEMO_TEXTURE_COUNT 1 |
Ian Elliott | 44e33f7 | 2015-04-28 10:52:52 -0600 | [diff] [blame] | 50 | #define APP_SHORT_NAME "cube" |
| 51 | #define APP_LONG_NAME "The Vulkan Cube Demo Program" |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 52 | |
Courtney Goeltzenleuchter | 886f76c | 2015-07-06 17:46:11 -0600 | [diff] [blame] | 53 | #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) |
| 54 | |
Tony Barbour | fdc2d35 | 2015-04-22 09:02:32 -0600 | [diff] [blame] | 55 | #if defined(NDEBUG) && defined(__GNUC__) |
| 56 | #define U_ASSERT_ONLY __attribute__((unused)) |
| 57 | #else |
| 58 | #define U_ASSERT_ONLY |
| 59 | #endif |
| 60 | |
Ian Elliott | 0726413 | 2015-04-28 11:35:02 -0600 | [diff] [blame] | 61 | #ifdef _WIN32 |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 62 | #define ERR_EXIT(err_msg, err_class) \ |
| 63 | do { \ |
| 64 | MessageBox(NULL, err_msg, err_class, MB_OK); \ |
| 65 | exit(1); \ |
| 66 | } while (0) |
Ian Elliott | 0726413 | 2015-04-28 11:35:02 -0600 | [diff] [blame] | 67 | |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 68 | #else // _WIN32 |
Ian Elliott | 0726413 | 2015-04-28 11:35:02 -0600 | [diff] [blame] | 69 | |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 70 | #define ERR_EXIT(err_msg, err_class) \ |
| 71 | do { \ |
| 72 | printf(err_msg); \ |
| 73 | fflush(stdout); \ |
| 74 | exit(1); \ |
| 75 | } while (0) |
Ian Elliott | 0726413 | 2015-04-28 11:35:02 -0600 | [diff] [blame] | 76 | #endif // _WIN32 |
| 77 | |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 78 | #define GET_INSTANCE_PROC_ADDR(inst, entrypoint) \ |
| 79 | { \ |
| 80 | demo->fp##entrypoint = \ |
| 81 | (PFN_vk##entrypoint)vkGetInstanceProcAddr(inst, "vk" #entrypoint); \ |
| 82 | if (demo->fp##entrypoint == NULL) { \ |
| 83 | ERR_EXIT("vkGetInstanceProcAddr failed to find vk" #entrypoint, \ |
| 84 | "vkGetInstanceProcAddr Failure"); \ |
| 85 | } \ |
| 86 | } |
Ian Elliott | aaae535 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 87 | |
Jon Ashburn | 7d8342c | 2015-10-08 15:58:23 -0600 | [diff] [blame] | 88 | static PFN_vkGetDeviceProcAddr g_gdpa = NULL; |
| 89 | |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 90 | #define GET_DEVICE_PROC_ADDR(dev, entrypoint) \ |
| 91 | { \ |
| 92 | if (!g_gdpa) \ |
| 93 | g_gdpa = (PFN_vkGetDeviceProcAddr)vkGetInstanceProcAddr( \ |
| 94 | demo->inst, "vkGetDeviceProcAddr"); \ |
| 95 | demo->fp##entrypoint = \ |
| 96 | (PFN_vk##entrypoint)g_gdpa(dev, "vk" #entrypoint); \ |
| 97 | if (demo->fp##entrypoint == NULL) { \ |
| 98 | ERR_EXIT("vkGetDeviceProcAddr failed to find vk" #entrypoint, \ |
| 99 | "vkGetDeviceProcAddr Failure"); \ |
| 100 | } \ |
| 101 | } |
Ian Elliott | 673898b | 2015-06-22 15:07:49 -0600 | [diff] [blame] | 102 | |
Courtney Goeltzenleuchter | 97db1c1 | 2014-10-30 15:14:16 -0600 | [diff] [blame] | 103 | /* |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 104 | * structure to track all objects related to a texture. |
| 105 | */ |
Courtney Goeltzenleuchter | 752eb8e | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 106 | struct texture_object { |
Courtney Goeltzenleuchter | 78351a6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 107 | VkSampler sampler; |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 108 | |
Courtney Goeltzenleuchter | 78351a6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 109 | VkImage image; |
| 110 | VkImageLayout imageLayout; |
Courtney Goeltzenleuchter | 752eb8e | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 111 | |
Chia-I Wu | a8fe78a | 2015-10-27 18:04:07 +0800 | [diff] [blame] | 112 | VkMemoryAllocateInfo mem_alloc; |
Mark Lobodzinski | 2dcdfd7 | 2015-05-29 09:32:35 -0500 | [diff] [blame] | 113 | VkDeviceMemory mem; |
Courtney Goeltzenleuchter | 78351a6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 114 | VkImageView view; |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 115 | int32_t tex_width, tex_height; |
| 116 | }; |
| 117 | |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 118 | static char *tex_files[] = {"lunarg.ppm"}; |
Courtney Goeltzenleuchter | 6836939 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 119 | |
Karl Schultz | 0218c00 | 2016-03-22 17:06:13 -0600 | [diff] [blame] | 120 | static int validation_error = 0; |
| 121 | |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 122 | struct vkcube_vs_uniform { |
Courtney Goeltzenleuchter | 4984d15 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 123 | // Must start with MVP |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 124 | float mvp[4][4]; |
| 125 | float position[12 * 3][4]; |
| 126 | float color[12 * 3][4]; |
Courtney Goeltzenleuchter | 4984d15 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 127 | }; |
| 128 | |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 129 | struct vktexcube_vs_uniform { |
Courtney Goeltzenleuchter | 4984d15 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 130 | // Must start with MVP |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 131 | float mvp[4][4]; |
| 132 | float position[12 * 3][4]; |
| 133 | float attr[12 * 3][4]; |
Courtney Goeltzenleuchter | 4984d15 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 134 | }; |
Courtney Goeltzenleuchter | fc3b953 | 2014-10-28 10:32:57 -0600 | [diff] [blame] | 135 | |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 136 | //-------------------------------------------------------------------------------------- |
| 137 | // Mesh and VertexFormat Data |
| 138 | //-------------------------------------------------------------------------------------- |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 139 | // clang-format off |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 140 | struct Vertex |
| 141 | { |
Mark Lobodzinski | c4fffc7 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 142 | float posX, posY, posZ, posW; // Position data |
| 143 | float r, g, b, a; // Color |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 144 | }; |
| 145 | |
Courtney Goeltzenleuchter | 4984d15 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 146 | struct VertexPosTex |
| 147 | { |
Mark Lobodzinski | c4fffc7 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 148 | float posX, posY, posZ, posW; // Position data |
| 149 | float u, v, s, t; // Texcoord |
Courtney Goeltzenleuchter | 4984d15 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 150 | }; |
| 151 | |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 152 | #define XYZ1(_x_, _y_, _z_) (_x_), (_y_), (_z_), 1.f |
Courtney Goeltzenleuchter | 4984d15 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 153 | #define UV(_u_, _v_) (_u_), (_v_), 0.f, 1.f |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 154 | |
Mark Lobodzinski | c4fffc7 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 155 | static const float g_vertex_buffer_data[] = { |
Chia-I Wu | ae3b55d | 2015-04-22 14:56:17 +0800 | [diff] [blame] | 156 | -1.0f,-1.0f,-1.0f, // -X side |
Courtney Goeltzenleuchter | 6836939 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 157 | -1.0f,-1.0f, 1.0f, |
| 158 | -1.0f, 1.0f, 1.0f, |
Chia-I Wu | ae3b55d | 2015-04-22 14:56:17 +0800 | [diff] [blame] | 159 | -1.0f, 1.0f, 1.0f, |
Courtney Goeltzenleuchter | 6836939 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 160 | -1.0f, 1.0f,-1.0f, |
| 161 | -1.0f,-1.0f,-1.0f, |
| 162 | |
Chia-I Wu | ae3b55d | 2015-04-22 14:56:17 +0800 | [diff] [blame] | 163 | -1.0f,-1.0f,-1.0f, // -Z side |
Courtney Goeltzenleuchter | 6836939 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 164 | 1.0f, 1.0f,-1.0f, |
| 165 | 1.0f,-1.0f,-1.0f, |
Chia-I Wu | ae3b55d | 2015-04-22 14:56:17 +0800 | [diff] [blame] | 166 | -1.0f,-1.0f,-1.0f, |
Courtney Goeltzenleuchter | 6836939 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 167 | -1.0f, 1.0f,-1.0f, |
Mike Stroyan | 16b3d98 | 2015-03-19 14:29:04 -0600 | [diff] [blame] | 168 | 1.0f, 1.0f,-1.0f, |
Courtney Goeltzenleuchter | 6836939 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 169 | |
Chia-I Wu | ae3b55d | 2015-04-22 14:56:17 +0800 | [diff] [blame] | 170 | -1.0f,-1.0f,-1.0f, // -Y side |
Courtney Goeltzenleuchter | 6836939 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 171 | 1.0f,-1.0f,-1.0f, |
Mike Stroyan | 16b3d98 | 2015-03-19 14:29:04 -0600 | [diff] [blame] | 172 | 1.0f,-1.0f, 1.0f, |
Chia-I Wu | ae3b55d | 2015-04-22 14:56:17 +0800 | [diff] [blame] | 173 | -1.0f,-1.0f,-1.0f, |
Courtney Goeltzenleuchter | 6836939 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 174 | 1.0f,-1.0f, 1.0f, |
Mike Stroyan | 16b3d98 | 2015-03-19 14:29:04 -0600 | [diff] [blame] | 175 | -1.0f,-1.0f, 1.0f, |
Courtney Goeltzenleuchter | 6836939 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 176 | |
Chia-I Wu | ae3b55d | 2015-04-22 14:56:17 +0800 | [diff] [blame] | 177 | -1.0f, 1.0f,-1.0f, // +Y side |
Courtney Goeltzenleuchter | 6836939 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 178 | -1.0f, 1.0f, 1.0f, |
| 179 | 1.0f, 1.0f, 1.0f, |
Chia-I Wu | ae3b55d | 2015-04-22 14:56:17 +0800 | [diff] [blame] | 180 | -1.0f, 1.0f,-1.0f, |
Courtney Goeltzenleuchter | 6836939 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 181 | 1.0f, 1.0f, 1.0f, |
Mike Stroyan | 16b3d98 | 2015-03-19 14:29:04 -0600 | [diff] [blame] | 182 | 1.0f, 1.0f,-1.0f, |
Courtney Goeltzenleuchter | 6836939 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 183 | |
Chia-I Wu | ae3b55d | 2015-04-22 14:56:17 +0800 | [diff] [blame] | 184 | 1.0f, 1.0f,-1.0f, // +X side |
Courtney Goeltzenleuchter | 6836939 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 185 | 1.0f, 1.0f, 1.0f, |
| 186 | 1.0f,-1.0f, 1.0f, |
Chia-I Wu | ae3b55d | 2015-04-22 14:56:17 +0800 | [diff] [blame] | 187 | 1.0f,-1.0f, 1.0f, |
Courtney Goeltzenleuchter | 6836939 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 188 | 1.0f,-1.0f,-1.0f, |
| 189 | 1.0f, 1.0f,-1.0f, |
| 190 | |
Chia-I Wu | ae3b55d | 2015-04-22 14:56:17 +0800 | [diff] [blame] | 191 | -1.0f, 1.0f, 1.0f, // +Z side |
Courtney Goeltzenleuchter | 6836939 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 192 | -1.0f,-1.0f, 1.0f, |
Mike Stroyan | 16b3d98 | 2015-03-19 14:29:04 -0600 | [diff] [blame] | 193 | 1.0f, 1.0f, 1.0f, |
Chia-I Wu | ae3b55d | 2015-04-22 14:56:17 +0800 | [diff] [blame] | 194 | -1.0f,-1.0f, 1.0f, |
Courtney Goeltzenleuchter | 6836939 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 195 | 1.0f,-1.0f, 1.0f, |
| 196 | 1.0f, 1.0f, 1.0f, |
Courtney Goeltzenleuchter | 4984d15 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 197 | }; |
| 198 | |
Mark Lobodzinski | c4fffc7 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 199 | static const float g_uv_buffer_data[] = { |
Chia-I Wu | ae3b55d | 2015-04-22 14:56:17 +0800 | [diff] [blame] | 200 | 0.0f, 0.0f, // -X side |
| 201 | 1.0f, 0.0f, |
| 202 | 1.0f, 1.0f, |
| 203 | 1.0f, 1.0f, |
| 204 | 0.0f, 1.0f, |
| 205 | 0.0f, 0.0f, |
| 206 | |
| 207 | 1.0f, 0.0f, // -Z side |
| 208 | 0.0f, 1.0f, |
| 209 | 0.0f, 0.0f, |
| 210 | 1.0f, 0.0f, |
| 211 | 1.0f, 1.0f, |
| 212 | 0.0f, 1.0f, |
| 213 | |
| 214 | 1.0f, 1.0f, // -Y side |
| 215 | 1.0f, 0.0f, |
| 216 | 0.0f, 0.0f, |
| 217 | 1.0f, 1.0f, |
Courtney Goeltzenleuchter | 6836939 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 218 | 0.0f, 0.0f, |
| 219 | 0.0f, 1.0f, |
| 220 | |
Chia-I Wu | ae3b55d | 2015-04-22 14:56:17 +0800 | [diff] [blame] | 221 | 1.0f, 1.0f, // +Y side |
Courtney Goeltzenleuchter | 8879d3a | 2014-10-29 08:29:35 -0600 | [diff] [blame] | 222 | 0.0f, 1.0f, |
Chia-I Wu | ae3b55d | 2015-04-22 14:56:17 +0800 | [diff] [blame] | 223 | 0.0f, 0.0f, |
Mike Stroyan | 16b3d98 | 2015-03-19 14:29:04 -0600 | [diff] [blame] | 224 | 1.0f, 1.0f, |
Courtney Goeltzenleuchter | 6836939 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 225 | 0.0f, 0.0f, |
Mike Stroyan | 16b3d98 | 2015-03-19 14:29:04 -0600 | [diff] [blame] | 226 | 1.0f, 0.0f, |
Courtney Goeltzenleuchter | 6836939 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 227 | |
Chia-I Wu | ae3b55d | 2015-04-22 14:56:17 +0800 | [diff] [blame] | 228 | 1.0f, 1.0f, // +X side |
| 229 | 0.0f, 1.0f, |
| 230 | 0.0f, 0.0f, |
| 231 | 0.0f, 0.0f, |
Courtney Goeltzenleuchter | 6836939 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 232 | 1.0f, 0.0f, |
Mike Stroyan | 16b3d98 | 2015-03-19 14:29:04 -0600 | [diff] [blame] | 233 | 1.0f, 1.0f, |
Courtney Goeltzenleuchter | 6836939 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 234 | |
Chia-I Wu | ae3b55d | 2015-04-22 14:56:17 +0800 | [diff] [blame] | 235 | 0.0f, 1.0f, // +Z side |
| 236 | 0.0f, 0.0f, |
Courtney Goeltzenleuchter | 6836939 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 237 | 1.0f, 1.0f, |
Mike Stroyan | 16b3d98 | 2015-03-19 14:29:04 -0600 | [diff] [blame] | 238 | 0.0f, 0.0f, |
Chia-I Wu | ae3b55d | 2015-04-22 14:56:17 +0800 | [diff] [blame] | 239 | 1.0f, 0.0f, |
Courtney Goeltzenleuchter | 6836939 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 240 | 1.0f, 1.0f, |
Courtney Goeltzenleuchter | 4984d15 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 241 | }; |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 242 | // clang-format on |
Courtney Goeltzenleuchter | 4984d15 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 243 | |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 244 | void dumpMatrix(const char *note, mat4x4 MVP) { |
Courtney Goeltzenleuchter | 4984d15 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 245 | int i; |
| 246 | |
| 247 | printf("%s: \n", note); |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 248 | for (i = 0; i < 4; i++) { |
Courtney Goeltzenleuchter | 4984d15 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 249 | printf("%f, %f, %f, %f\n", MVP[i][0], MVP[i][1], MVP[i][2], MVP[i][3]); |
| 250 | } |
| 251 | printf("\n"); |
| 252 | fflush(stdout); |
| 253 | } |
| 254 | |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 255 | void dumpVec4(const char *note, vec4 vector) { |
Courtney Goeltzenleuchter | 4984d15 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 256 | printf("%s: \n", note); |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 257 | 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] | 258 | printf("\n"); |
| 259 | fflush(stdout); |
| 260 | } |
| 261 | |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 262 | VKAPI_ATTR VkBool32 VKAPI_CALL |
| 263 | dbgFunc(VkFlags msgFlags, VkDebugReportObjectTypeEXT objType, |
| 264 | uint64_t srcObject, size_t location, int32_t msgCode, |
| 265 | const char *pLayerPrefix, const char *pMsg, void *pUserData) { |
| 266 | char *message = (char *)malloc(strlen(pMsg) + 100); |
Tony Barbour | 3dddd5d | 2015-04-29 16:19:20 -0600 | [diff] [blame] | 267 | |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 268 | assert(message); |
Tony Barbour | 3dddd5d | 2015-04-29 16:19:20 -0600 | [diff] [blame] | 269 | |
Courtney Goeltzenleuchter | 0f060df | 2015-12-09 15:48:16 -0700 | [diff] [blame] | 270 | if (msgFlags & VK_DEBUG_REPORT_ERROR_BIT_EXT) { |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 271 | sprintf(message, "ERROR: [%s] Code %d : %s", pLayerPrefix, msgCode, |
| 272 | pMsg); |
Karl Schultz | 0218c00 | 2016-03-22 17:06:13 -0600 | [diff] [blame] | 273 | validation_error = 1; |
Mark Lobodzinski | cf9784e | 2016-02-11 09:26:16 -0700 | [diff] [blame] | 274 | } else if (msgFlags & VK_DEBUG_REPORT_WARNING_BIT_EXT) { |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 275 | // We know that we're submitting queues without fences, ignore this |
| 276 | // warning |
| 277 | if (strstr(pMsg, |
| 278 | "vkQueueSubmit parameter, VkFence fence, is null pointer")) { |
Courtney Goeltzenleuchter | ab32819 | 2015-09-04 13:52:24 -0600 | [diff] [blame] | 279 | return false; |
Tony Barbour | d70b42c | 2015-06-30 14:14:19 -0600 | [diff] [blame] | 280 | } |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 281 | sprintf(message, "WARNING: [%s] Code %d : %s", pLayerPrefix, msgCode, |
| 282 | pMsg); |
Karl Schultz | 0218c00 | 2016-03-22 17:06:13 -0600 | [diff] [blame] | 283 | validation_error = 1; |
Tony Barbour | 3dddd5d | 2015-04-29 16:19:20 -0600 | [diff] [blame] | 284 | } else { |
Karl Schultz | 0218c00 | 2016-03-22 17:06:13 -0600 | [diff] [blame] | 285 | validation_error = 1; |
Courtney Goeltzenleuchter | ab32819 | 2015-09-04 13:52:24 -0600 | [diff] [blame] | 286 | return false; |
Tony Barbour | 3dddd5d | 2015-04-29 16:19:20 -0600 | [diff] [blame] | 287 | } |
| 288 | |
| 289 | #ifdef _WIN32 |
| 290 | MessageBox(NULL, message, "Alert", MB_OK); |
| 291 | #else |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 292 | printf("%s\n", message); |
Tony Barbour | 3dddd5d | 2015-04-29 16:19:20 -0600 | [diff] [blame] | 293 | fflush(stdout); |
| 294 | #endif |
| 295 | free(message); |
Courtney Goeltzenleuchter | ab32819 | 2015-09-04 13:52:24 -0600 | [diff] [blame] | 296 | |
Courtney Goeltzenleuchter | 8cda44e | 2015-09-08 16:57:22 -0600 | [diff] [blame] | 297 | /* |
| 298 | * false indicates that layer should not bail-out of an |
| 299 | * API call that had validation failures. This may mean that the |
| 300 | * app dies inside the driver due to invalid parameter(s). |
| 301 | * That's what would happen without validation layers, so we'll |
| 302 | * keep that behavior here. |
| 303 | */ |
| 304 | return false; |
Tony Barbour | 3dddd5d | 2015-04-29 16:19:20 -0600 | [diff] [blame] | 305 | } |
| 306 | |
Karl Schultz | 0c3c151 | 2016-03-25 15:35:18 -0600 | [diff] [blame] | 307 | VKAPI_ATTR VkBool32 VKAPI_CALL |
| 308 | BreakCallback(VkFlags msgFlags, VkDebugReportObjectTypeEXT objType, |
| 309 | uint64_t srcObject, size_t location, int32_t msgCode, |
| 310 | const char *pLayerPrefix, const char *pMsg, |
| 311 | void *pUserData) { |
Courtney Goeltzenleuchter | af1951c | 2015-12-01 14:11:38 -0700 | [diff] [blame] | 312 | #ifndef WIN32 |
| 313 | raise(SIGTRAP); |
| 314 | #else |
| 315 | DebugBreak(); |
| 316 | #endif |
| 317 | |
| 318 | return false; |
| 319 | } |
| 320 | |
Ian Elliott | a078197 | 2015-08-21 15:09:33 -0600 | [diff] [blame] | 321 | typedef struct _SwapchainBuffers { |
Ian Elliott | aaae535 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 322 | VkImage image; |
Chia-I Wu | a8fe78a | 2015-10-27 18:04:07 +0800 | [diff] [blame] | 323 | VkCommandBuffer cmd; |
Courtney Goeltzenleuchter | 41866db | 2015-09-01 17:30:39 -0600 | [diff] [blame] | 324 | VkImageView view; |
Ian Elliott | a078197 | 2015-08-21 15:09:33 -0600 | [diff] [blame] | 325 | } SwapchainBuffers; |
Ian Elliott | aaae535 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 326 | |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 327 | struct demo { |
Ian Elliott | 639ca47 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 328 | #ifdef _WIN32 |
| 329 | #define APP_NAME_STR_LEN 80 |
| 330 | HINSTANCE connection; // hInstance - Windows Instance |
| 331 | char name[APP_NAME_STR_LEN]; // Name to put on the window/icon |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 332 | HWND window; // hWnd - window handle |
| 333 | #else // _WIN32 |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 334 | xcb_connection_t *connection; |
| 335 | xcb_screen_t *screen; |
Chia-I Wu | cbb564e | 2015-04-16 22:02:10 +0800 | [diff] [blame] | 336 | xcb_window_t window; |
| 337 | xcb_intern_atom_reply_t *atom_wm_delete_window; |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 338 | #endif // _WIN32 |
Ian Elliott | b08e0d9 | 2015-11-20 11:55:46 -0700 | [diff] [blame] | 339 | VkSurfaceKHR surface; |
Cody Northrop | 1fedb21 | 2015-05-28 11:27:16 -0600 | [diff] [blame] | 340 | bool prepared; |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 341 | bool use_staging_buffer; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 342 | |
Courtney Goeltzenleuchter | 78351a6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 343 | VkInstance inst; |
Tony Barbour | 72304ef | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 344 | VkPhysicalDevice gpu; |
Courtney Goeltzenleuchter | 78351a6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 345 | VkDevice device; |
| 346 | VkQueue queue; |
Courtney Goeltzenleuchter | ed667a5 | 2015-03-05 18:09:39 -0700 | [diff] [blame] | 347 | uint32_t graphics_queue_node_index; |
Tony Barbour | ecf1b2b | 2015-06-24 16:06:58 -0600 | [diff] [blame] | 348 | VkPhysicalDeviceProperties gpu_props; |
Cody Northrop | 4d3c11d | 2015-08-03 17:04:53 -0600 | [diff] [blame] | 349 | VkQueueFamilyProperties *queue_props; |
Mark Lobodzinski | eadf998 | 2015-07-02 16:49:40 -0600 | [diff] [blame] | 350 | VkPhysicalDeviceMemoryProperties memory_properties; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 351 | |
Tony Barbour | da2bad5 | 2016-01-22 14:36:40 -0700 | [diff] [blame] | 352 | uint32_t enabled_extension_count; |
| 353 | uint32_t enabled_layer_count; |
| 354 | char *extension_names[64]; |
| 355 | char *device_validation_layers[64]; |
| 356 | |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 357 | int width, height; |
Courtney Goeltzenleuchter | 78351a6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 358 | VkFormat format; |
Ian Elliott | a078197 | 2015-08-21 15:09:33 -0600 | [diff] [blame] | 359 | VkColorSpaceKHR color_space; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 360 | |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 361 | PFN_vkGetPhysicalDeviceSurfaceSupportKHR |
| 362 | fpGetPhysicalDeviceSurfaceSupportKHR; |
| 363 | PFN_vkGetPhysicalDeviceSurfaceCapabilitiesKHR |
| 364 | fpGetPhysicalDeviceSurfaceCapabilitiesKHR; |
| 365 | PFN_vkGetPhysicalDeviceSurfaceFormatsKHR |
| 366 | fpGetPhysicalDeviceSurfaceFormatsKHR; |
| 367 | PFN_vkGetPhysicalDeviceSurfacePresentModesKHR |
| 368 | fpGetPhysicalDeviceSurfacePresentModesKHR; |
Ian Elliott | a078197 | 2015-08-21 15:09:33 -0600 | [diff] [blame] | 369 | PFN_vkCreateSwapchainKHR fpCreateSwapchainKHR; |
| 370 | PFN_vkDestroySwapchainKHR fpDestroySwapchainKHR; |
| 371 | PFN_vkGetSwapchainImagesKHR fpGetSwapchainImagesKHR; |
| 372 | PFN_vkAcquireNextImageKHR fpAcquireNextImageKHR; |
| 373 | PFN_vkQueuePresentKHR fpQueuePresentKHR; |
Ian Elliott | a078197 | 2015-08-21 15:09:33 -0600 | [diff] [blame] | 374 | uint32_t swapchainImageCount; |
Ian Elliott | cf074d3 | 2015-10-16 18:02:43 -0600 | [diff] [blame] | 375 | VkSwapchainKHR swapchain; |
Ian Elliott | a078197 | 2015-08-21 15:09:33 -0600 | [diff] [blame] | 376 | SwapchainBuffers *buffers; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 377 | |
Chia-I Wu | a8fe78a | 2015-10-27 18:04:07 +0800 | [diff] [blame] | 378 | VkCommandPool cmd_pool; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 379 | |
| 380 | struct { |
Courtney Goeltzenleuchter | 78351a6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 381 | VkFormat format; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 382 | |
Courtney Goeltzenleuchter | 78351a6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 383 | VkImage image; |
Chia-I Wu | a8fe78a | 2015-10-27 18:04:07 +0800 | [diff] [blame] | 384 | VkMemoryAllocateInfo mem_alloc; |
Mark Lobodzinski | 2dcdfd7 | 2015-05-29 09:32:35 -0500 | [diff] [blame] | 385 | VkDeviceMemory mem; |
Courtney Goeltzenleuchter | 41866db | 2015-09-01 17:30:39 -0600 | [diff] [blame] | 386 | VkImageView view; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 387 | } depth; |
| 388 | |
Courtney Goeltzenleuchter | 752eb8e | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 389 | struct texture_object textures[DEMO_TEXTURE_COUNT]; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 390 | |
| 391 | struct { |
Courtney Goeltzenleuchter | 78351a6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 392 | VkBuffer buf; |
Chia-I Wu | a8fe78a | 2015-10-27 18:04:07 +0800 | [diff] [blame] | 393 | VkMemoryAllocateInfo mem_alloc; |
Mark Lobodzinski | 2dcdfd7 | 2015-05-29 09:32:35 -0500 | [diff] [blame] | 394 | VkDeviceMemory mem; |
Courtney Goeltzenleuchter | c3ad65e | 2015-10-23 13:38:14 -0600 | [diff] [blame] | 395 | VkDescriptorBufferInfo buffer_info; |
Courtney Goeltzenleuchter | fc3b953 | 2014-10-28 10:32:57 -0600 | [diff] [blame] | 396 | } uniform_data; |
| 397 | |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 398 | VkCommandBuffer cmd; // Buffer for initialization commands |
Mark Lobodzinski | 1cfc772 | 2015-04-17 14:11:39 -0500 | [diff] [blame] | 399 | VkPipelineLayout pipeline_layout; |
Courtney Goeltzenleuchter | 78351a6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 400 | VkDescriptorSetLayout desc_layout; |
Jon Ashburn | c4ab7af | 2015-07-09 15:02:25 -0600 | [diff] [blame] | 401 | VkPipelineCache pipelineCache; |
Chia-I Wu | f973e32 | 2015-07-08 13:34:24 +0800 | [diff] [blame] | 402 | VkRenderPass render_pass; |
Courtney Goeltzenleuchter | 78351a6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 403 | VkPipeline pipeline; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 404 | |
Courtney Goeltzenleuchter | fc3b953 | 2014-10-28 10:32:57 -0600 | [diff] [blame] | 405 | mat4x4 projection_matrix; |
| 406 | mat4x4 view_matrix; |
| 407 | mat4x4 model_matrix; |
| 408 | |
Mark Lobodzinski | c4fffc7 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 409 | float spin_angle; |
| 410 | float spin_increment; |
Courtney Goeltzenleuchter | 8879d3a | 2014-10-29 08:29:35 -0600 | [diff] [blame] | 411 | bool pause; |
| 412 | |
Tony Barbour | b6dba5c | 2015-07-21 09:04:41 -0600 | [diff] [blame] | 413 | VkShaderModule vert_shader_module; |
| 414 | VkShaderModule frag_shader_module; |
| 415 | |
Courtney Goeltzenleuchter | 78351a6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 416 | VkDescriptorPool desc_pool; |
| 417 | VkDescriptorSet desc_set; |
Chia-I Wu | 6a3c897 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 418 | |
Tony Barbour | d8e504f | 2015-10-08 14:26:24 -0600 | [diff] [blame] | 419 | VkFramebuffer *framebuffers; |
Chia-I Wu | f973e32 | 2015-07-08 13:34:24 +0800 | [diff] [blame] | 420 | |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 421 | bool quit; |
David Pinedo | 2bb7c93 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 422 | int32_t curFrame; |
| 423 | int32_t frameCount; |
Tony Barbour | 3dddd5d | 2015-04-29 16:19:20 -0600 | [diff] [blame] | 424 | bool validate; |
Courtney Goeltzenleuchter | 03f4b77 | 2015-07-22 11:03:51 -0600 | [diff] [blame] | 425 | bool use_break; |
Courtney Goeltzenleuchter | 0f060df | 2015-12-09 15:48:16 -0700 | [diff] [blame] | 426 | PFN_vkCreateDebugReportCallbackEXT CreateDebugReportCallback; |
| 427 | PFN_vkDestroyDebugReportCallbackEXT DestroyDebugReportCallback; |
| 428 | VkDebugReportCallbackEXT msg_callback; |
| 429 | PFN_vkDebugReportMessageEXT DebugReportMessage; |
Courtney Goeltzenleuchter | 922a0fa | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 430 | |
Mark Lobodzinski | c4fffc7 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 431 | uint32_t current_buffer; |
Courtney Goeltzenleuchter | fe95fca | 2015-07-15 17:40:20 -0600 | [diff] [blame] | 432 | uint32_t queue_count; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 433 | }; |
| 434 | |
Ian Elliott | cf074d3 | 2015-10-16 18:02:43 -0600 | [diff] [blame] | 435 | // Forward declaration: |
| 436 | static void demo_resize(struct demo *demo); |
| 437 | |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 438 | static bool memory_type_from_properties(struct demo *demo, uint32_t typeBits, |
| 439 | VkFlags requirements_mask, |
| 440 | uint32_t *typeIndex) { |
| 441 | // Search memtypes to find first index with those properties |
| 442 | for (uint32_t i = 0; i < 32; i++) { |
| 443 | if ((typeBits & 1) == 1) { |
| 444 | // Type is available, does it match user properties? |
| 445 | if ((demo->memory_properties.memoryTypes[i].propertyFlags & |
| 446 | requirements_mask) == requirements_mask) { |
| 447 | *typeIndex = i; |
| 448 | return true; |
| 449 | } |
| 450 | } |
| 451 | typeBits >>= 1; |
| 452 | } |
| 453 | // No memory types matched, return failure |
| 454 | return false; |
Mark Lobodzinski | eadf998 | 2015-07-02 16:49:40 -0600 | [diff] [blame] | 455 | } |
| 456 | |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 457 | static void demo_flush_init_cmd(struct demo *demo) { |
Tony Barbour | fdc2d35 | 2015-04-22 09:02:32 -0600 | [diff] [blame] | 458 | VkResult U_ASSERT_ONLY err; |
Courtney Goeltzenleuchter | 752eb8e | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 459 | |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 460 | if (demo->cmd == VK_NULL_HANDLE) |
Courtney Goeltzenleuchter | 752eb8e | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 461 | return; |
| 462 | |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 463 | err = vkEndCommandBuffer(demo->cmd); |
Courtney Goeltzenleuchter | 752eb8e | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 464 | assert(!err); |
| 465 | |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 466 | const VkCommandBuffer cmd_bufs[] = {demo->cmd}; |
Chia-I Wu | 1c4086a | 2015-10-26 20:04:44 +0800 | [diff] [blame] | 467 | VkFence nullFence = VK_NULL_HANDLE; |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 468 | VkSubmitInfo submit_info = {.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO, |
| 469 | .pNext = NULL, |
| 470 | .waitSemaphoreCount = 0, |
| 471 | .pWaitSemaphores = NULL, |
| 472 | .pWaitDstStageMask = NULL, |
| 473 | .commandBufferCount = 1, |
| 474 | .pCommandBuffers = cmd_bufs, |
| 475 | .signalSemaphoreCount = 0, |
| 476 | .pSignalSemaphores = NULL}; |
Courtney Goeltzenleuchter | 752eb8e | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 477 | |
Courtney Goeltzenleuchter | 5fa898d | 2015-10-20 18:04:07 -0600 | [diff] [blame] | 478 | err = vkQueueSubmit(demo->queue, 1, &submit_info, nullFence); |
Courtney Goeltzenleuchter | 752eb8e | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 479 | assert(!err); |
| 480 | |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 481 | err = vkQueueWaitIdle(demo->queue); |
Courtney Goeltzenleuchter | 752eb8e | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 482 | assert(!err); |
| 483 | |
Courtney Goeltzenleuchter | 014ded8 | 2015-10-23 14:21:05 -0600 | [diff] [blame] | 484 | vkFreeCommandBuffers(demo->device, demo->cmd_pool, 1, cmd_bufs); |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 485 | demo->cmd = VK_NULL_HANDLE; |
Courtney Goeltzenleuchter | 752eb8e | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 486 | } |
| 487 | |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 488 | static void demo_set_image_layout(struct demo *demo, VkImage image, |
| 489 | VkImageAspectFlags aspectMask, |
| 490 | VkImageLayout old_image_layout, |
Tobin Ehlis | f07d955 | 2016-02-11 17:49:23 -0700 | [diff] [blame] | 491 | VkImageLayout new_image_layout, |
| 492 | VkAccessFlagBits srcAccessMask) { |
Tony Barbour | fdc2d35 | 2015-04-22 09:02:32 -0600 | [diff] [blame] | 493 | VkResult U_ASSERT_ONLY err; |
Courtney Goeltzenleuchter | 752eb8e | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 494 | |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 495 | if (demo->cmd == VK_NULL_HANDLE) { |
Chia-I Wu | a8fe78a | 2015-10-27 18:04:07 +0800 | [diff] [blame] | 496 | const VkCommandBufferAllocateInfo cmd = { |
Chia-I Wu | f48700b | 2015-11-10 16:21:09 +0800 | [diff] [blame] | 497 | .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO, |
Courtney Goeltzenleuchter | 752eb8e | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 498 | .pNext = NULL, |
Chia-I Wu | a8fe78a | 2015-10-27 18:04:07 +0800 | [diff] [blame] | 499 | .commandPool = demo->cmd_pool, |
| 500 | .level = VK_COMMAND_BUFFER_LEVEL_PRIMARY, |
Jon Ashburn | 0bec67e | 2016-01-11 13:12:43 -0700 | [diff] [blame] | 501 | .commandBufferCount = 1, |
Courtney Goeltzenleuchter | 752eb8e | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 502 | }; |
| 503 | |
Chia-I Wu | a8fe78a | 2015-10-27 18:04:07 +0800 | [diff] [blame] | 504 | err = vkAllocateCommandBuffers(demo->device, &cmd, &demo->cmd); |
Courtney Goeltzenleuchter | 752eb8e | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 505 | assert(!err); |
| 506 | |
Jon Ashburn | 0bec67e | 2016-01-11 13:12:43 -0700 | [diff] [blame] | 507 | VkCommandBufferInheritanceInfo cmd_buf_hinfo = { |
| 508 | .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_INHERITANCE_INFO, |
Courtney Goeltzenleuchter | 752eb8e | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 509 | .pNext = NULL, |
Chia-I Wu | 1c4086a | 2015-10-26 20:04:44 +0800 | [diff] [blame] | 510 | .renderPass = VK_NULL_HANDLE, |
Cody Northrop | a0a178c | 2015-08-11 11:35:58 -0600 | [diff] [blame] | 511 | .subpass = 0, |
Chia-I Wu | 1c4086a | 2015-10-26 20:04:44 +0800 | [diff] [blame] | 512 | .framebuffer = VK_NULL_HANDLE, |
Chia-I Wu | 4ac4a8b | 2015-11-11 10:18:12 +0800 | [diff] [blame] | 513 | .occlusionQueryEnable = VK_FALSE, |
| 514 | .queryFlags = 0, |
| 515 | .pipelineStatistics = 0, |
Courtney Goeltzenleuchter | 752eb8e | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 516 | }; |
Jon Ashburn | 0bec67e | 2016-01-11 13:12:43 -0700 | [diff] [blame] | 517 | VkCommandBufferBeginInfo cmd_buf_info = { |
| 518 | .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO, |
| 519 | .pNext = NULL, |
| 520 | .flags = 0, |
| 521 | .pInheritanceInfo = &cmd_buf_hinfo, |
| 522 | }; |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 523 | err = vkBeginCommandBuffer(demo->cmd, &cmd_buf_info); |
Tony Barbour | cb59a5d | 2015-10-23 10:53:30 -0600 | [diff] [blame] | 524 | assert(!err); |
Courtney Goeltzenleuchter | 752eb8e | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 525 | } |
| 526 | |
Courtney Goeltzenleuchter | 78351a6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 527 | VkImageMemoryBarrier image_memory_barrier = { |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 528 | .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER, |
Courtney Goeltzenleuchter | 752eb8e | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 529 | .pNext = NULL, |
Tobin Ehlis | f07d955 | 2016-02-11 17:49:23 -0700 | [diff] [blame] | 530 | .srcAccessMask = srcAccessMask, |
Chia-I Wu | 1957462 | 2015-10-27 19:54:37 +0800 | [diff] [blame] | 531 | .dstAccessMask = 0, |
Courtney Goeltzenleuchter | 752eb8e | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 532 | .oldLayout = old_image_layout, |
| 533 | .newLayout = new_image_layout, |
| 534 | .image = image, |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 535 | .subresourceRange = {aspectMask, 0, 1, 0, 1}}; |
Courtney Goeltzenleuchter | 752eb8e | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 536 | |
Chia-I Wu | a8fe78a | 2015-10-27 18:04:07 +0800 | [diff] [blame] | 537 | if (new_image_layout == VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL) { |
Courtney Goeltzenleuchter | 752eb8e | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 538 | /* Make sure anything that was copying from this image has completed */ |
Chia-I Wu | 1957462 | 2015-10-27 19:54:37 +0800 | [diff] [blame] | 539 | image_memory_barrier.dstAccessMask = VK_ACCESS_TRANSFER_READ_BIT; |
Courtney Goeltzenleuchter | 752eb8e | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 540 | } |
| 541 | |
Mark Lobodzinski | 9e0be70 | 2015-11-04 13:49:48 -0700 | [diff] [blame] | 542 | if (new_image_layout == VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL) { |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 543 | image_memory_barrier.dstAccessMask = |
| 544 | VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT; |
Mark Lobodzinski | 9e0be70 | 2015-11-04 13:49:48 -0700 | [diff] [blame] | 545 | } |
| 546 | |
| 547 | if (new_image_layout == VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL) { |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 548 | image_memory_barrier.dstAccessMask = |
| 549 | VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT; |
Mark Lobodzinski | 9e0be70 | 2015-11-04 13:49:48 -0700 | [diff] [blame] | 550 | } |
| 551 | |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 552 | if (new_image_layout == VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL) { |
Courtney Goeltzenleuchter | 752eb8e | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 553 | /* Make sure any Copy or CPU writes to image are flushed */ |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 554 | image_memory_barrier.dstAccessMask = |
| 555 | VK_ACCESS_SHADER_READ_BIT | VK_ACCESS_INPUT_ATTACHMENT_READ_BIT; |
Courtney Goeltzenleuchter | 752eb8e | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 556 | } |
| 557 | |
Courtney Goeltzenleuchter | 78351a6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 558 | VkImageMemoryBarrier *pmemory_barrier = &image_memory_barrier; |
Courtney Goeltzenleuchter | 752eb8e | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 559 | |
Tony Barbour | 50bbca4 | 2015-06-29 16:20:35 -0600 | [diff] [blame] | 560 | VkPipelineStageFlags src_stages = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT; |
| 561 | VkPipelineStageFlags dest_stages = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT; |
Courtney Goeltzenleuchter | 752eb8e | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 562 | |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 563 | vkCmdPipelineBarrier(demo->cmd, src_stages, dest_stages, 0, 0, NULL, 0, |
| 564 | NULL, 1, pmemory_barrier); |
Courtney Goeltzenleuchter | 752eb8e | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 565 | } |
| 566 | |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 567 | static void demo_draw_build_cmd(struct demo *demo, VkCommandBuffer cmd_buf) { |
Jon Ashburn | 0bec67e | 2016-01-11 13:12:43 -0700 | [diff] [blame] | 568 | VkCommandBufferInheritanceInfo cmd_buf_hinfo = { |
| 569 | .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_INHERITANCE_INFO, |
Courtney Goeltzenleuchter | 53968d8 | 2015-04-03 15:25:24 -0600 | [diff] [blame] | 570 | .pNext = NULL, |
Chia-I Wu | 1c4086a | 2015-10-26 20:04:44 +0800 | [diff] [blame] | 571 | .renderPass = VK_NULL_HANDLE, |
Cody Northrop | a0a178c | 2015-08-11 11:35:58 -0600 | [diff] [blame] | 572 | .subpass = 0, |
Chia-I Wu | 1c4086a | 2015-10-26 20:04:44 +0800 | [diff] [blame] | 573 | .framebuffer = VK_NULL_HANDLE, |
Chia-I Wu | 4ac4a8b | 2015-11-11 10:18:12 +0800 | [diff] [blame] | 574 | .occlusionQueryEnable = VK_FALSE, |
| 575 | .queryFlags = 0, |
| 576 | .pipelineStatistics = 0, |
Jon Ashburn | 60ccfbe | 2014-12-31 17:08:35 -0700 | [diff] [blame] | 577 | }; |
Jon Ashburn | 0bec67e | 2016-01-11 13:12:43 -0700 | [diff] [blame] | 578 | const VkCommandBufferBeginInfo cmd_buf_info = { |
| 579 | .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO, |
| 580 | .pNext = NULL, |
| 581 | .flags = 0, |
| 582 | .pInheritanceInfo = &cmd_buf_hinfo, |
| 583 | }; |
Chia-I Wu | a74c5b2 | 2015-07-07 11:50:03 +0800 | [diff] [blame] | 584 | const VkClearValue clear_values[2] = { |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 585 | [0] = {.color.float32 = {0.2f, 0.2f, 0.2f, 0.2f}}, |
| 586 | [1] = {.depthStencil = {1.0f, 0}}, |
Chia-I Wu | a74c5b2 | 2015-07-07 11:50:03 +0800 | [diff] [blame] | 587 | }; |
| 588 | const VkRenderPassBeginInfo rp_begin = { |
| 589 | .sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO, |
| 590 | .pNext = NULL, |
Chia-I Wu | f973e32 | 2015-07-08 13:34:24 +0800 | [diff] [blame] | 591 | .renderPass = demo->render_pass, |
| 592 | .framebuffer = demo->framebuffers[demo->current_buffer], |
Chia-I Wu | a74c5b2 | 2015-07-07 11:50:03 +0800 | [diff] [blame] | 593 | .renderArea.offset.x = 0, |
| 594 | .renderArea.offset.y = 0, |
| 595 | .renderArea.extent.width = demo->width, |
| 596 | .renderArea.extent.height = demo->height, |
Cody Northrop | 372bbae | 2015-08-04 11:51:03 -0600 | [diff] [blame] | 597 | .clearValueCount = 2, |
| 598 | .pClearValues = clear_values, |
Jon Ashburn | 08f6eb9 | 2015-01-02 18:24:05 -0700 | [diff] [blame] | 599 | }; |
Chia-I Wu | f973e32 | 2015-07-08 13:34:24 +0800 | [diff] [blame] | 600 | VkResult U_ASSERT_ONLY err; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 601 | |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 602 | err = vkBeginCommandBuffer(cmd_buf, &cmd_buf_info); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 603 | assert(!err); |
| 604 | |
Chia-I Wu | f051d26 | 2015-10-27 19:25:11 +0800 | [diff] [blame] | 605 | vkCmdBeginRenderPass(cmd_buf, &rp_begin, VK_SUBPASS_CONTENTS_INLINE); |
Chia-I Wu | a74c5b2 | 2015-07-07 11:50:03 +0800 | [diff] [blame] | 606 | |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 607 | vkCmdBindPipeline(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS, demo->pipeline); |
| 608 | vkCmdBindDescriptorSets(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS, |
| 609 | demo->pipeline_layout, 0, 1, &demo->desc_set, 0, |
| 610 | NULL); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 611 | |
Courtney Goeltzenleuchter | 4cbf78b | 2015-09-17 15:06:17 -0600 | [diff] [blame] | 612 | VkViewport viewport; |
| 613 | memset(&viewport, 0, sizeof(viewport)); |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 614 | viewport.height = (float)demo->height; |
| 615 | viewport.width = (float)demo->width; |
| 616 | viewport.minDepth = (float)0.0f; |
| 617 | viewport.maxDepth = (float)1.0f; |
Jon Ashburn | 19255f8 | 2015-12-30 14:06:55 -0700 | [diff] [blame] | 618 | vkCmdSetViewport(cmd_buf, 0, 1, &viewport); |
Courtney Goeltzenleuchter | 4cbf78b | 2015-09-17 15:06:17 -0600 | [diff] [blame] | 619 | |
| 620 | VkRect2D scissor; |
| 621 | memset(&scissor, 0, sizeof(scissor)); |
| 622 | scissor.extent.width = demo->width; |
| 623 | scissor.extent.height = demo->height; |
| 624 | scissor.offset.x = 0; |
| 625 | scissor.offset.y = 0; |
Jon Ashburn | 19255f8 | 2015-12-30 14:06:55 -0700 | [diff] [blame] | 626 | vkCmdSetScissor(cmd_buf, 0, 1, &scissor); |
Courtney Goeltzenleuchter | 4cbf78b | 2015-09-17 15:06:17 -0600 | [diff] [blame] | 627 | |
Courtney Goeltzenleuchter | 332a367 | 2015-09-23 12:31:50 -0600 | [diff] [blame] | 628 | vkCmdDraw(cmd_buf, 12 * 3, 1, 0, 0); |
Chia-I Wu | 57b23b4 | 2015-06-26 15:34:39 +0800 | [diff] [blame] | 629 | vkCmdEndRenderPass(cmd_buf); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 630 | |
Tony Barbour | 15a4ef6 | 2015-10-21 10:14:48 -0600 | [diff] [blame] | 631 | VkImageMemoryBarrier prePresentBarrier = { |
| 632 | .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER, |
| 633 | .pNext = NULL, |
Chia-I Wu | 1957462 | 2015-10-27 19:54:37 +0800 | [diff] [blame] | 634 | .srcAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT, |
Tony Barbour | e5af539 | 2016-01-05 09:59:05 -0700 | [diff] [blame] | 635 | .dstAccessMask = VK_ACCESS_MEMORY_READ_BIT, |
Tony Barbour | 15a4ef6 | 2015-10-21 10:14:48 -0600 | [diff] [blame] | 636 | .oldLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, |
Ian Elliott | f4a4e44 | 2015-11-17 17:29:40 -0700 | [diff] [blame] | 637 | .newLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR, |
Tony Barbour | 15a4ef6 | 2015-10-21 10:14:48 -0600 | [diff] [blame] | 638 | .srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED, |
Chia-I Wu | a8fe78a | 2015-10-27 18:04:07 +0800 | [diff] [blame] | 639 | .dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED, |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 640 | .subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1}}; |
Tony Barbour | 15a4ef6 | 2015-10-21 10:14:48 -0600 | [diff] [blame] | 641 | |
| 642 | prePresentBarrier.image = demo->buffers[demo->current_buffer].image; |
| 643 | VkImageMemoryBarrier *pmemory_barrier = &prePresentBarrier; |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 644 | vkCmdPipelineBarrier(cmd_buf, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, |
| 645 | VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT, 0, 0, NULL, 0, |
| 646 | NULL, 1, pmemory_barrier); |
Tony Barbour | 15a4ef6 | 2015-10-21 10:14:48 -0600 | [diff] [blame] | 647 | |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 648 | err = vkEndCommandBuffer(cmd_buf); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 649 | assert(!err); |
| 650 | } |
| 651 | |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 652 | void demo_update_data_buffer(struct demo *demo) { |
Courtney Goeltzenleuchter | fc3b953 | 2014-10-28 10:32:57 -0600 | [diff] [blame] | 653 | mat4x4 MVP, Model, VP; |
| 654 | int matrixSize = sizeof(MVP); |
Mark Lobodzinski | c4fffc7 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 655 | uint8_t *pData; |
Tony Barbour | fdc2d35 | 2015-04-22 09:02:32 -0600 | [diff] [blame] | 656 | VkResult U_ASSERT_ONLY err; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 657 | |
Courtney Goeltzenleuchter | fc3b953 | 2014-10-28 10:32:57 -0600 | [diff] [blame] | 658 | mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 659 | |
Courtney Goeltzenleuchter | fc3b953 | 2014-10-28 10:32:57 -0600 | [diff] [blame] | 660 | // Rotate 22.5 degrees around the Y axis |
Courtney Goeltzenleuchter | 4984d15 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 661 | mat4x4_dup(Model, demo->model_matrix); |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 662 | mat4x4_rotate(demo->model_matrix, Model, 0.0f, 1.0f, 0.0f, |
| 663 | (float)degreesToRadians(demo->spin_angle)); |
Courtney Goeltzenleuchter | 4984d15 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 664 | mat4x4_mul(MVP, VP, demo->model_matrix); |
Courtney Goeltzenleuchter | fc3b953 | 2014-10-28 10:32:57 -0600 | [diff] [blame] | 665 | |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 666 | err = vkMapMemory(demo->device, demo->uniform_data.mem, 0, |
| 667 | demo->uniform_data.mem_alloc.allocationSize, 0, |
| 668 | (void **)&pData); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 669 | assert(!err); |
| 670 | |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 671 | memcpy(pData, (const void *)&MVP[0][0], matrixSize); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 672 | |
Mark Lobodzinski | e4c92a6 | 2015-09-07 13:59:43 -0600 | [diff] [blame] | 673 | vkUnmapMemory(demo->device, demo->uniform_data.mem); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 674 | } |
| 675 | |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 676 | static void demo_draw(struct demo *demo) { |
Tony Barbour | fdc2d35 | 2015-04-22 09:02:32 -0600 | [diff] [blame] | 677 | VkResult U_ASSERT_ONLY err; |
Ian Elliott | aaae535 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 678 | VkSemaphore presentCompleteSemaphore; |
| 679 | VkSemaphoreCreateInfo presentCompleteSemaphoreCreateInfo = { |
| 680 | .sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO, |
| 681 | .pNext = NULL, |
Mark Lobodzinski | e7ba7f1 | 2015-10-08 10:44:07 -0600 | [diff] [blame] | 682 | .flags = 0, |
Ian Elliott | aaae535 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 683 | }; |
Chia-I Wu | 1c4086a | 2015-10-26 20:04:44 +0800 | [diff] [blame] | 684 | VkFence nullFence = VK_NULL_HANDLE; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 685 | |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 686 | err = vkCreateSemaphore(demo->device, &presentCompleteSemaphoreCreateInfo, |
| 687 | NULL, &presentCompleteSemaphore); |
Ian Elliott | aaae535 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 688 | assert(!err); |
| 689 | |
| 690 | // Get the index of the next available swapchain image: |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 691 | err = demo->fpAcquireNextImageKHR(demo->device, demo->swapchain, UINT64_MAX, |
Ian Elliott | aaae535 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 692 | presentCompleteSemaphore, |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 693 | (VkFence)0, // TODO: Show use of fence |
Ian Elliott | aaae535 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 694 | &demo->current_buffer); |
Ian Elliott | cf074d3 | 2015-10-16 18:02:43 -0600 | [diff] [blame] | 695 | if (err == VK_ERROR_OUT_OF_DATE_KHR) { |
| 696 | // demo->swapchain is out of date (e.g. the window was resized) and |
| 697 | // must be recreated: |
| 698 | demo_resize(demo); |
| 699 | demo_draw(demo); |
Chia-I Wu | 6363606 | 2015-10-26 21:10:41 +0800 | [diff] [blame] | 700 | vkDestroySemaphore(demo->device, presentCompleteSemaphore, NULL); |
Ian Elliott | cf074d3 | 2015-10-16 18:02:43 -0600 | [diff] [blame] | 701 | return; |
| 702 | } else if (err == VK_SUBOPTIMAL_KHR) { |
| 703 | // demo->swapchain is not as optimal as it could be, but the platform's |
| 704 | // presentation engine will still present the image correctly. |
| 705 | } else { |
| 706 | assert(!err); |
| 707 | } |
Ian Elliott | aaae535 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 708 | |
Tony Barbour | 15a4ef6 | 2015-10-21 10:14:48 -0600 | [diff] [blame] | 709 | // Assume the command buffer has been run on current_buffer before so |
| 710 | // we need to set the image layout back to COLOR_ATTACHMENT_OPTIMAL |
| 711 | demo_set_image_layout(demo, demo->buffers[demo->current_buffer].image, |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 712 | VK_IMAGE_ASPECT_COLOR_BIT, |
| 713 | VK_IMAGE_LAYOUT_PRESENT_SRC_KHR, |
Tobin Ehlis | f07d955 | 2016-02-11 17:49:23 -0700 | [diff] [blame] | 714 | VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, |
| 715 | 0); |
Tony Barbour | 15a4ef6 | 2015-10-21 10:14:48 -0600 | [diff] [blame] | 716 | demo_flush_init_cmd(demo); |
| 717 | |
Courtney Goeltzenleuchter | 5fa898d | 2015-10-20 18:04:07 -0600 | [diff] [blame] | 718 | // Wait for the present complete semaphore to be signaled to ensure |
| 719 | // that the image won't be rendered to until the presentation |
| 720 | // engine has fully released ownership to the application, and it is |
| 721 | // okay to render to the image. |
| 722 | |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 723 | // FIXME/TODO: DEAL WITH VK_IMAGE_LAYOUT_PRESENT_SRC_KHR |
| 724 | VkPipelineStageFlags pipe_stage_flags = |
| 725 | VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT; |
| 726 | VkSubmitInfo submit_info = {.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO, |
| 727 | .pNext = NULL, |
| 728 | .waitSemaphoreCount = 1, |
| 729 | .pWaitSemaphores = &presentCompleteSemaphore, |
| 730 | .pWaitDstStageMask = &pipe_stage_flags, |
| 731 | .commandBufferCount = 1, |
| 732 | .pCommandBuffers = |
| 733 | &demo->buffers[demo->current_buffer].cmd, |
| 734 | .signalSemaphoreCount = 0, |
| 735 | .pSignalSemaphores = NULL}; |
Courtney Goeltzenleuchter | 5fa898d | 2015-10-20 18:04:07 -0600 | [diff] [blame] | 736 | |
| 737 | err = vkQueueSubmit(demo->queue, 1, &submit_info, nullFence); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 738 | assert(!err); |
| 739 | |
Ian Elliott | a078197 | 2015-08-21 15:09:33 -0600 | [diff] [blame] | 740 | VkPresentInfoKHR present = { |
| 741 | .sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR, |
Ian Elliott | aaae535 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 742 | .pNext = NULL, |
Ian Elliott | a078197 | 2015-08-21 15:09:33 -0600 | [diff] [blame] | 743 | .swapchainCount = 1, |
Ian Elliott | b08e0d9 | 2015-11-20 11:55:46 -0700 | [diff] [blame] | 744 | .pSwapchains = &demo->swapchain, |
| 745 | .pImageIndices = &demo->current_buffer, |
Ian Elliott | aaae535 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 746 | }; |
| 747 | |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 748 | // TBD/TODO: SHOULD THE "present" PARAMETER BE "const" IN THE HEADER? |
Ian Elliott | a078197 | 2015-08-21 15:09:33 -0600 | [diff] [blame] | 749 | err = demo->fpQueuePresentKHR(demo->queue, &present); |
Ian Elliott | cf074d3 | 2015-10-16 18:02:43 -0600 | [diff] [blame] | 750 | if (err == VK_ERROR_OUT_OF_DATE_KHR) { |
| 751 | // demo->swapchain is out of date (e.g. the window was resized) and |
| 752 | // must be recreated: |
| 753 | demo_resize(demo); |
| 754 | } else if (err == VK_SUBOPTIMAL_KHR) { |
| 755 | // demo->swapchain is not as optimal as it could be, but the platform's |
| 756 | // presentation engine will still present the image correctly. |
| 757 | } else { |
| 758 | assert(!err); |
| 759 | } |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 760 | |
Chia-I Wu | cbb564e | 2015-04-16 22:02:10 +0800 | [diff] [blame] | 761 | err = vkQueueWaitIdle(demo->queue); |
| 762 | assert(err == VK_SUCCESS); |
Mike Stroyan | 15fed6b | 2015-08-28 10:58:06 -0600 | [diff] [blame] | 763 | |
Chia-I Wu | 6363606 | 2015-10-26 21:10:41 +0800 | [diff] [blame] | 764 | vkDestroySemaphore(demo->device, presentCompleteSemaphore, NULL); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 765 | } |
| 766 | |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 767 | static void demo_prepare_buffers(struct demo *demo) { |
Ian Elliott | aaae535 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 768 | VkResult U_ASSERT_ONLY err; |
Ian Elliott | cf074d3 | 2015-10-16 18:02:43 -0600 | [diff] [blame] | 769 | VkSwapchainKHR oldSwapchain = demo->swapchain; |
Ian Elliott | aaae535 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 770 | |
Ian Elliott | b08e0d9 | 2015-11-20 11:55:46 -0700 | [diff] [blame] | 771 | // Check the surface capabilities and formats |
| 772 | VkSurfaceCapabilitiesKHR surfCapabilities; |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 773 | err = demo->fpGetPhysicalDeviceSurfaceCapabilitiesKHR( |
| 774 | demo->gpu, demo->surface, &surfCapabilities); |
Ian Elliott | aaae535 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 775 | assert(!err); |
| 776 | |
Ian Elliott | 8528eff | 2015-08-07 15:56:59 -0600 | [diff] [blame] | 777 | uint32_t presentModeCount; |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 778 | err = demo->fpGetPhysicalDeviceSurfacePresentModesKHR( |
| 779 | demo->gpu, demo->surface, &presentModeCount, NULL); |
Ian Elliott | aaae535 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 780 | assert(!err); |
Ian Elliott | a078197 | 2015-08-21 15:09:33 -0600 | [diff] [blame] | 781 | VkPresentModeKHR *presentModes = |
| 782 | (VkPresentModeKHR *)malloc(presentModeCount * sizeof(VkPresentModeKHR)); |
Ian Elliott | 8528eff | 2015-08-07 15:56:59 -0600 | [diff] [blame] | 783 | assert(presentModes); |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 784 | err = demo->fpGetPhysicalDeviceSurfacePresentModesKHR( |
| 785 | demo->gpu, demo->surface, &presentModeCount, presentModes); |
Ian Elliott | aaae535 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 786 | assert(!err); |
| 787 | |
Ian Elliott | a078197 | 2015-08-21 15:09:33 -0600 | [diff] [blame] | 788 | VkExtent2D swapchainExtent; |
Ian Elliott | aaae535 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 789 | // width and height are either both -1, or both not -1. |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 790 | if (surfCapabilities.currentExtent.width == (uint32_t)-1) { |
Ian Elliott | aaae535 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 791 | // If the surface size is undefined, the size is set to |
| 792 | // the size of the images requested. |
Ian Elliott | a078197 | 2015-08-21 15:09:33 -0600 | [diff] [blame] | 793 | swapchainExtent.width = demo->width; |
| 794 | swapchainExtent.height = demo->height; |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 795 | } else { |
Ian Elliott | aaae535 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 796 | // If the surface size is defined, the swap chain size must match |
Ian Elliott | b08e0d9 | 2015-11-20 11:55:46 -0700 | [diff] [blame] | 797 | swapchainExtent = surfCapabilities.currentExtent; |
| 798 | demo->width = surfCapabilities.currentExtent.width; |
| 799 | demo->height = surfCapabilities.currentExtent.height; |
Ian Elliott | aaae535 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 800 | } |
| 801 | |
| 802 | // If mailbox mode is available, use it, as is the lowest-latency non- |
Ian Elliott | 3ebce34 | 2015-07-27 13:53:11 -0600 | [diff] [blame] | 803 | // tearing mode. If not, try IMMEDIATE which will usually be available, |
| 804 | // and is fastest (though it tears). If not, fall back to FIFO which is |
| 805 | // always available. |
Ian Elliott | a078197 | 2015-08-21 15:09:33 -0600 | [diff] [blame] | 806 | VkPresentModeKHR swapchainPresentMode = VK_PRESENT_MODE_FIFO_KHR; |
Ian Elliott | aaae535 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 807 | for (size_t i = 0; i < presentModeCount; i++) { |
Ian Elliott | a078197 | 2015-08-21 15:09:33 -0600 | [diff] [blame] | 808 | if (presentModes[i] == VK_PRESENT_MODE_MAILBOX_KHR) { |
| 809 | swapchainPresentMode = VK_PRESENT_MODE_MAILBOX_KHR; |
Ian Elliott | aaae535 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 810 | break; |
| 811 | } |
Ian Elliott | a078197 | 2015-08-21 15:09:33 -0600 | [diff] [blame] | 812 | if ((swapchainPresentMode != VK_PRESENT_MODE_MAILBOX_KHR) && |
| 813 | (presentModes[i] == VK_PRESENT_MODE_IMMEDIATE_KHR)) { |
| 814 | swapchainPresentMode = VK_PRESENT_MODE_IMMEDIATE_KHR; |
Ian Elliott | 3ebce34 | 2015-07-27 13:53:11 -0600 | [diff] [blame] | 815 | } |
Ian Elliott | aaae535 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 816 | } |
| 817 | |
| 818 | // Determine the number of VkImage's to use in the swap chain (we desire to |
| 819 | // own only 1 image at a time, besides the images being displayed and |
| 820 | // queued for display): |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 821 | uint32_t desiredNumberOfSwapchainImages = |
| 822 | surfCapabilities.minImageCount + 1; |
Ian Elliott | b08e0d9 | 2015-11-20 11:55:46 -0700 | [diff] [blame] | 823 | if ((surfCapabilities.maxImageCount > 0) && |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 824 | (desiredNumberOfSwapchainImages > surfCapabilities.maxImageCount)) { |
Ian Elliott | aaae535 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 825 | // Application must settle for fewer images than desired: |
Ian Elliott | b08e0d9 | 2015-11-20 11:55:46 -0700 | [diff] [blame] | 826 | desiredNumberOfSwapchainImages = surfCapabilities.maxImageCount; |
Ian Elliott | aaae535 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 827 | } |
| 828 | |
Tony Barbour | 9fd6d35 | 2015-09-16 15:01:32 -0600 | [diff] [blame] | 829 | VkSurfaceTransformFlagsKHR preTransform; |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 830 | if (surfCapabilities.supportedTransforms & |
| 831 | VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR) { |
Ian Elliott | a7a731c | 2015-12-11 15:52:12 -0700 | [diff] [blame] | 832 | preTransform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR; |
Ian Elliott | aaae535 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 833 | } else { |
Ian Elliott | b08e0d9 | 2015-11-20 11:55:46 -0700 | [diff] [blame] | 834 | preTransform = surfCapabilities.currentTransform; |
Ian Elliott | aaae535 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 835 | } |
| 836 | |
Ian Elliott | cf074d3 | 2015-10-16 18:02:43 -0600 | [diff] [blame] | 837 | const VkSwapchainCreateInfoKHR swapchain = { |
Ian Elliott | 5b97eae | 2015-09-22 10:20:23 -0600 | [diff] [blame] | 838 | .sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR, |
Chia-I Wu | cbb564e | 2015-04-16 22:02:10 +0800 | [diff] [blame] | 839 | .pNext = NULL, |
Ian Elliott | b08e0d9 | 2015-11-20 11:55:46 -0700 | [diff] [blame] | 840 | .surface = demo->surface, |
Ian Elliott | a078197 | 2015-08-21 15:09:33 -0600 | [diff] [blame] | 841 | .minImageCount = desiredNumberOfSwapchainImages, |
Chia-I Wu | cbb564e | 2015-04-16 22:02:10 +0800 | [diff] [blame] | 842 | .imageFormat = demo->format, |
Ian Elliott | 8528eff | 2015-08-07 15:56:59 -0600 | [diff] [blame] | 843 | .imageColorSpace = demo->color_space, |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 844 | .imageExtent = |
| 845 | { |
| 846 | .width = swapchainExtent.width, .height = swapchainExtent.height, |
| 847 | }, |
Ian Elliott | b08e0d9 | 2015-11-20 11:55:46 -0700 | [diff] [blame] | 848 | .imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT, |
Ian Elliott | aaae535 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 849 | .preTransform = preTransform, |
Ian Elliott | 86f29a8 | 2015-12-28 15:25:33 -0700 | [diff] [blame] | 850 | .compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR, |
Ian Elliott | b08e0d9 | 2015-11-20 11:55:46 -0700 | [diff] [blame] | 851 | .imageArrayLayers = 1, |
| 852 | .imageSharingMode = VK_SHARING_MODE_EXCLUSIVE, |
| 853 | .queueFamilyIndexCount = 0, |
Ian Elliott | 8528eff | 2015-08-07 15:56:59 -0600 | [diff] [blame] | 854 | .pQueueFamilyIndices = NULL, |
Ian Elliott | a078197 | 2015-08-21 15:09:33 -0600 | [diff] [blame] | 855 | .presentMode = swapchainPresentMode, |
Ian Elliott | cf074d3 | 2015-10-16 18:02:43 -0600 | [diff] [blame] | 856 | .oldSwapchain = oldSwapchain, |
Ian Elliott | aaae535 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 857 | .clipped = true, |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 858 | }; |
Mark Lobodzinski | c4fffc7 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 859 | uint32_t i; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 860 | |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 861 | err = demo->fpCreateSwapchainKHR(demo->device, &swapchain, NULL, |
| 862 | &demo->swapchain); |
Chia-I Wu | cbb564e | 2015-04-16 22:02:10 +0800 | [diff] [blame] | 863 | assert(!err); |
| 864 | |
Ian Elliott | cf074d3 | 2015-10-16 18:02:43 -0600 | [diff] [blame] | 865 | // If we just re-created an existing swapchain, we should destroy the old |
| 866 | // swapchain at this point. |
| 867 | // Note: destroying the swapchain also cleans up all its associated |
| 868 | // presentable images once the platform is done with them. |
Chia-I Wu | 1c4086a | 2015-10-26 20:04:44 +0800 | [diff] [blame] | 869 | if (oldSwapchain != VK_NULL_HANDLE) { |
Ian Elliott | b08e0d9 | 2015-11-20 11:55:46 -0700 | [diff] [blame] | 870 | demo->fpDestroySwapchainKHR(demo->device, oldSwapchain, NULL); |
Ian Elliott | cf074d3 | 2015-10-16 18:02:43 -0600 | [diff] [blame] | 871 | } |
| 872 | |
| 873 | err = demo->fpGetSwapchainImagesKHR(demo->device, demo->swapchain, |
Ian Elliott | a078197 | 2015-08-21 15:09:33 -0600 | [diff] [blame] | 874 | &demo->swapchainImageCount, NULL); |
Ian Elliott | aaae535 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 875 | assert(!err); |
Chia-I Wu | cbb564e | 2015-04-16 22:02:10 +0800 | [diff] [blame] | 876 | |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 877 | VkImage *swapchainImages = |
| 878 | (VkImage *)malloc(demo->swapchainImageCount * sizeof(VkImage)); |
Ian Elliott | a078197 | 2015-08-21 15:09:33 -0600 | [diff] [blame] | 879 | assert(swapchainImages); |
Ian Elliott | cf074d3 | 2015-10-16 18:02:43 -0600 | [diff] [blame] | 880 | err = demo->fpGetSwapchainImagesKHR(demo->device, demo->swapchain, |
Ian Elliott | a078197 | 2015-08-21 15:09:33 -0600 | [diff] [blame] | 881 | &demo->swapchainImageCount, |
| 882 | swapchainImages); |
Ian Elliott | aaae535 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 883 | assert(!err); |
Ian Elliott | aaae535 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 884 | |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 885 | demo->buffers = (SwapchainBuffers *)malloc(sizeof(SwapchainBuffers) * |
| 886 | demo->swapchainImageCount); |
Ian Elliott | aaae535 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 887 | assert(demo->buffers); |
| 888 | |
Ian Elliott | a078197 | 2015-08-21 15:09:33 -0600 | [diff] [blame] | 889 | for (i = 0; i < demo->swapchainImageCount; i++) { |
Courtney Goeltzenleuchter | 41866db | 2015-09-01 17:30:39 -0600 | [diff] [blame] | 890 | VkImageViewCreateInfo color_image_view = { |
| 891 | .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO, |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 892 | .pNext = NULL, |
| 893 | .format = demo->format, |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 894 | .components = |
| 895 | { |
| 896 | .r = VK_COMPONENT_SWIZZLE_R, |
| 897 | .g = VK_COMPONENT_SWIZZLE_G, |
| 898 | .b = VK_COMPONENT_SWIZZLE_B, |
| 899 | .a = VK_COMPONENT_SWIZZLE_A, |
| 900 | }, |
| 901 | .subresourceRange = {.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT, |
| 902 | .baseMipLevel = 0, |
| 903 | .levelCount = 1, |
| 904 | .baseArrayLayer = 0, |
| 905 | .layerCount = 1}, |
Courtney Goeltzenleuchter | 41866db | 2015-09-01 17:30:39 -0600 | [diff] [blame] | 906 | .viewType = VK_IMAGE_VIEW_TYPE_2D, |
| 907 | .flags = 0, |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 908 | }; |
| 909 | |
Ian Elliott | a078197 | 2015-08-21 15:09:33 -0600 | [diff] [blame] | 910 | demo->buffers[i].image = swapchainImages[i]; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 911 | |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 912 | // Render loop will expect image to have been used before and in |
| 913 | // VK_IMAGE_LAYOUT_PRESENT_SRC_KHR |
| 914 | // layout and will change to COLOR_ATTACHMENT_OPTIMAL, so init the image |
| 915 | // to that state |
| 916 | demo_set_image_layout( |
| 917 | demo, demo->buffers[i].image, VK_IMAGE_ASPECT_COLOR_BIT, |
Tobin Ehlis | f07d955 | 2016-02-11 17:49:23 -0700 | [diff] [blame] | 918 | VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_PRESENT_SRC_KHR, |
| 919 | 0); |
Courtney Goeltzenleuchter | 752eb8e | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 920 | |
Courtney Goeltzenleuchter | 41866db | 2015-09-01 17:30:39 -0600 | [diff] [blame] | 921 | color_image_view.image = demo->buffers[i].image; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 922 | |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 923 | err = vkCreateImageView(demo->device, &color_image_view, NULL, |
| 924 | &demo->buffers[i].view); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 925 | assert(!err); |
| 926 | } |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 927 | |
Mark Young | 04fd3d8 | 2016-01-25 14:43:50 -0700 | [diff] [blame] | 928 | if (NULL != presentModes) { |
| 929 | free(presentModes); |
| 930 | } |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 931 | } |
| 932 | |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 933 | static void demo_prepare_depth(struct demo *demo) { |
Tony Barbour | 72304ef | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 934 | const VkFormat depth_format = VK_FORMAT_D16_UNORM; |
Courtney Goeltzenleuchter | 78351a6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 935 | const VkImageCreateInfo image = { |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 936 | .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO, |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 937 | .pNext = NULL, |
Tony Barbour | 72304ef | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 938 | .imageType = VK_IMAGE_TYPE_2D, |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 939 | .format = depth_format, |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 940 | .extent = {demo->width, demo->height, 1}, |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 941 | .mipLevels = 1, |
Courtney Goeltzenleuchter | dff021f | 2015-10-21 17:57:31 -0600 | [diff] [blame] | 942 | .arrayLayers = 1, |
Chia-I Wu | 3ede946 | 2015-10-31 00:31:16 +0800 | [diff] [blame] | 943 | .samples = VK_SAMPLE_COUNT_1_BIT, |
Tony Barbour | 72304ef | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 944 | .tiling = VK_IMAGE_TILING_OPTIMAL, |
Courtney Goeltzenleuchter | 4e368ee | 2015-09-10 14:14:11 -0600 | [diff] [blame] | 945 | .usage = VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT, |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 946 | .flags = 0, |
| 947 | }; |
Dominik Witczak | 2fe1c70 | 2015-09-22 18:25:33 +0200 | [diff] [blame] | 948 | |
Courtney Goeltzenleuchter | 41866db | 2015-09-01 17:30:39 -0600 | [diff] [blame] | 949 | VkImageViewCreateInfo view = { |
| 950 | .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO, |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 951 | .pNext = NULL, |
Chia-I Wu | 1c4086a | 2015-10-26 20:04:44 +0800 | [diff] [blame] | 952 | .image = VK_NULL_HANDLE, |
Courtney Goeltzenleuchter | 556ee32 | 2015-07-15 17:41:38 -0600 | [diff] [blame] | 953 | .format = depth_format, |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 954 | .subresourceRange = {.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT, |
| 955 | .baseMipLevel = 0, |
| 956 | .levelCount = 1, |
| 957 | .baseArrayLayer = 0, |
| 958 | .layerCount = 1}, |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 959 | .flags = 0, |
Courtney Goeltzenleuchter | 41866db | 2015-09-01 17:30:39 -0600 | [diff] [blame] | 960 | .viewType = VK_IMAGE_VIEW_TYPE_2D, |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 961 | }; |
Mike Stroyan | ebae832 | 2015-04-17 12:36:38 -0600 | [diff] [blame] | 962 | |
Mark Lobodzinski | 2dcdfd7 | 2015-05-29 09:32:35 -0500 | [diff] [blame] | 963 | VkMemoryRequirements mem_reqs; |
Tony Barbour | fdc2d35 | 2015-04-22 09:02:32 -0600 | [diff] [blame] | 964 | VkResult U_ASSERT_ONLY err; |
Courtney Goeltzenleuchter | 87a0ef0 | 2015-10-22 11:03:31 -0600 | [diff] [blame] | 965 | bool U_ASSERT_ONLY pass; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 966 | |
| 967 | demo->depth.format = depth_format; |
| 968 | |
| 969 | /* create image */ |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 970 | err = vkCreateImage(demo->device, &image, NULL, &demo->depth.image); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 971 | assert(!err); |
| 972 | |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 973 | vkGetImageMemoryRequirements(demo->device, demo->depth.image, &mem_reqs); |
Tony Barbour | cb59a5d | 2015-10-23 10:53:30 -0600 | [diff] [blame] | 974 | assert(!err); |
Mark Lobodzinski | eadf998 | 2015-07-02 16:49:40 -0600 | [diff] [blame] | 975 | |
Chia-I Wu | f48700b | 2015-11-10 16:21:09 +0800 | [diff] [blame] | 976 | demo->depth.mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO; |
Dominik Witczak | 2fe1c70 | 2015-09-22 18:25:33 +0200 | [diff] [blame] | 977 | demo->depth.mem_alloc.pNext = NULL; |
| 978 | demo->depth.mem_alloc.allocationSize = mem_reqs.size; |
| 979 | demo->depth.mem_alloc.memoryTypeIndex = 0; |
| 980 | |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 981 | pass = memory_type_from_properties(demo, mem_reqs.memoryTypeBits, |
| 982 | 0, /* No requirements */ |
| 983 | &demo->depth.mem_alloc.memoryTypeIndex); |
Courtney Goeltzenleuchter | 87a0ef0 | 2015-10-22 11:03:31 -0600 | [diff] [blame] | 984 | assert(pass); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 985 | |
Mark Lobodzinski | 2dcdfd7 | 2015-05-29 09:32:35 -0500 | [diff] [blame] | 986 | /* allocate memory */ |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 987 | err = vkAllocateMemory(demo->device, &demo->depth.mem_alloc, NULL, |
| 988 | &demo->depth.mem); |
Mark Lobodzinski | 2dcdfd7 | 2015-05-29 09:32:35 -0500 | [diff] [blame] | 989 | assert(!err); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 990 | |
Mark Lobodzinski | 2dcdfd7 | 2015-05-29 09:32:35 -0500 | [diff] [blame] | 991 | /* bind memory */ |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 992 | err = |
| 993 | vkBindImageMemory(demo->device, demo->depth.image, demo->depth.mem, 0); |
Mark Lobodzinski | 2dcdfd7 | 2015-05-29 09:32:35 -0500 | [diff] [blame] | 994 | assert(!err); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 995 | |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 996 | demo_set_image_layout(demo, demo->depth.image, VK_IMAGE_ASPECT_DEPTH_BIT, |
| 997 | VK_IMAGE_LAYOUT_UNDEFINED, |
Tobin Ehlis | f07d955 | 2016-02-11 17:49:23 -0700 | [diff] [blame] | 998 | VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL, |
| 999 | 0); |
Courtney Goeltzenleuchter | 752eb8e | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 1000 | |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1001 | /* create image view */ |
| 1002 | view.image = demo->depth.image; |
Chia-I Wu | 6363606 | 2015-10-26 21:10:41 +0800 | [diff] [blame] | 1003 | err = vkCreateImageView(demo->device, &view, NULL, &demo->depth.view); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1004 | assert(!err); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1005 | } |
| 1006 | |
Tony Barbour | 1a86d03 | 2015-09-21 15:17:33 -0600 | [diff] [blame] | 1007 | /* Load a ppm file into memory */ |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 1008 | bool loadTexture(const char *filename, uint8_t *rgba_data, |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1009 | VkSubresourceLayout *layout, int32_t *width, int32_t *height) { |
| 1010 | FILE *fPtr = fopen(filename, "rb"); |
Courtney Goeltzenleuchter | d3cdaaf | 2015-12-17 09:53:29 -0700 | [diff] [blame] | 1011 | char header[256], *cPtr, *tmp; |
Courtney Goeltzenleuchter | 6836939 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 1012 | |
Tony Barbour | 1a86d03 | 2015-09-21 15:17:33 -0600 | [diff] [blame] | 1013 | if (!fPtr) |
| 1014 | return false; |
Courtney Goeltzenleuchter | 6836939 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 1015 | |
Tony Barbour | 1a86d03 | 2015-09-21 15:17:33 -0600 | [diff] [blame] | 1016 | cPtr = fgets(header, 256, fPtr); // P6 |
Mike Stroyan | 9e9792a | 2015-10-28 11:15:46 -0600 | [diff] [blame] | 1017 | if (cPtr == NULL || strncmp(header, "P6\n", 3)) { |
| 1018 | fclose(fPtr); |
Tony Barbour | 1a86d03 | 2015-09-21 15:17:33 -0600 | [diff] [blame] | 1019 | return false; |
Mike Stroyan | 9e9792a | 2015-10-28 11:15:46 -0600 | [diff] [blame] | 1020 | } |
Courtney Goeltzenleuchter | 6836939 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 1021 | |
Tony Barbour | 1a86d03 | 2015-09-21 15:17:33 -0600 | [diff] [blame] | 1022 | do { |
| 1023 | cPtr = fgets(header, 256, fPtr); |
Mike Stroyan | 9e9792a | 2015-10-28 11:15:46 -0600 | [diff] [blame] | 1024 | if (cPtr == NULL) { |
| 1025 | fclose(fPtr); |
Tony Barbour | 1a86d03 | 2015-09-21 15:17:33 -0600 | [diff] [blame] | 1026 | return false; |
Mike Stroyan | 9e9792a | 2015-10-28 11:15:46 -0600 | [diff] [blame] | 1027 | } |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1028 | } while (!strncmp(header, "#", 1)); |
Courtney Goeltzenleuchter | 6836939 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 1029 | |
Tony Barbour | 1a86d03 | 2015-09-21 15:17:33 -0600 | [diff] [blame] | 1030 | sscanf(header, "%u %u", height, width); |
Mike Stroyan | 9e9792a | 2015-10-28 11:15:46 -0600 | [diff] [blame] | 1031 | if (rgba_data == NULL) { |
| 1032 | fclose(fPtr); |
Tony Barbour | 1a86d03 | 2015-09-21 15:17:33 -0600 | [diff] [blame] | 1033 | return true; |
Mike Stroyan | 9e9792a | 2015-10-28 11:15:46 -0600 | [diff] [blame] | 1034 | } |
Courtney Goeltzenleuchter | d3cdaaf | 2015-12-17 09:53:29 -0700 | [diff] [blame] | 1035 | tmp = fgets(header, 256, fPtr); // Format |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1036 | (void)tmp; |
Mike Stroyan | 9e9792a | 2015-10-28 11:15:46 -0600 | [diff] [blame] | 1037 | if (cPtr == NULL || strncmp(header, "255\n", 3)) { |
| 1038 | fclose(fPtr); |
Tony Barbour | 1a86d03 | 2015-09-21 15:17:33 -0600 | [diff] [blame] | 1039 | return false; |
Mike Stroyan | 9e9792a | 2015-10-28 11:15:46 -0600 | [diff] [blame] | 1040 | } |
Courtney Goeltzenleuchter | 6836939 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 1041 | |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1042 | for (int y = 0; y < *height; y++) { |
Tony Barbour | 1a86d03 | 2015-09-21 15:17:33 -0600 | [diff] [blame] | 1043 | uint8_t *rowPtr = rgba_data; |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1044 | for (int x = 0; x < *width; x++) { |
Courtney Goeltzenleuchter | d3cdaaf | 2015-12-17 09:53:29 -0700 | [diff] [blame] | 1045 | size_t s = fread(rowPtr, 3, 1, fPtr); |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1046 | (void)s; |
Tony Barbour | 1a86d03 | 2015-09-21 15:17:33 -0600 | [diff] [blame] | 1047 | rowPtr[3] = 255; /* Alpha of 1 */ |
| 1048 | rowPtr += 4; |
| 1049 | } |
| 1050 | rgba_data += layout->rowPitch; |
| 1051 | } |
| 1052 | fclose(fPtr); |
Mike Stroyan | 9e9792a | 2015-10-28 11:15:46 -0600 | [diff] [blame] | 1053 | return true; |
Courtney Goeltzenleuchter | 6836939 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 1054 | } |
Courtney Goeltzenleuchter | 6836939 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 1055 | |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1056 | static void demo_prepare_texture_image(struct demo *demo, const char *filename, |
Courtney Goeltzenleuchter | 752eb8e | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 1057 | struct texture_object *tex_obj, |
Courtney Goeltzenleuchter | 78351a6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 1058 | VkImageTiling tiling, |
Courtney Goeltzenleuchter | c56c36a | 2015-04-21 09:31:23 -0600 | [diff] [blame] | 1059 | VkImageUsageFlags usage, |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1060 | VkFlags required_props) { |
Mike Stroyan | 8b89e07 | 2015-06-15 14:21:03 -0600 | [diff] [blame] | 1061 | const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM; |
Mark Lobodzinski | c4fffc7 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 1062 | int32_t tex_width; |
| 1063 | int32_t tex_height; |
Tony Barbour | fdc2d35 | 2015-04-22 09:02:32 -0600 | [diff] [blame] | 1064 | VkResult U_ASSERT_ONLY err; |
Courtney Goeltzenleuchter | 87a0ef0 | 2015-10-22 11:03:31 -0600 | [diff] [blame] | 1065 | bool U_ASSERT_ONLY pass; |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 1066 | |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1067 | if (!loadTexture(filename, NULL, NULL, &tex_width, &tex_height)) { |
David Pinedo | 30bd71d | 2015-04-23 08:16:57 -0600 | [diff] [blame] | 1068 | printf("Failed to load textures\n"); |
| 1069 | fflush(stdout); |
| 1070 | exit(1); |
| 1071 | } |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 1072 | |
Courtney Goeltzenleuchter | 752eb8e | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 1073 | tex_obj->tex_width = tex_width; |
| 1074 | tex_obj->tex_height = tex_height; |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 1075 | |
Courtney Goeltzenleuchter | 78351a6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 1076 | const VkImageCreateInfo image_create_info = { |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1077 | .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO, |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 1078 | .pNext = NULL, |
Tony Barbour | 72304ef | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 1079 | .imageType = VK_IMAGE_TYPE_2D, |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 1080 | .format = tex_format, |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1081 | .extent = {tex_width, tex_height, 1}, |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 1082 | .mipLevels = 1, |
Courtney Goeltzenleuchter | dff021f | 2015-10-21 17:57:31 -0600 | [diff] [blame] | 1083 | .arrayLayers = 1, |
Chia-I Wu | 3ede946 | 2015-10-31 00:31:16 +0800 | [diff] [blame] | 1084 | .samples = VK_SAMPLE_COUNT_1_BIT, |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 1085 | .tiling = tiling, |
Courtney Goeltzenleuchter | c56c36a | 2015-04-21 09:31:23 -0600 | [diff] [blame] | 1086 | .usage = usage, |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 1087 | .flags = 0, |
Tobin Ehlis | f07d955 | 2016-02-11 17:49:23 -0700 | [diff] [blame] | 1088 | .initialLayout = VK_IMAGE_LAYOUT_PREINITIALIZED, |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 1089 | }; |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 1090 | |
Mark Lobodzinski | 2dcdfd7 | 2015-05-29 09:32:35 -0500 | [diff] [blame] | 1091 | VkMemoryRequirements mem_reqs; |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 1092 | |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1093 | err = |
| 1094 | vkCreateImage(demo->device, &image_create_info, NULL, &tex_obj->image); |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 1095 | assert(!err); |
| 1096 | |
Courtney Goeltzenleuchter | bd5c174 | 2015-10-20 16:40:38 -0600 | [diff] [blame] | 1097 | vkGetImageMemoryRequirements(demo->device, tex_obj->image, &mem_reqs); |
Piers Daniell | 735ee53 | 2015-02-23 16:23:13 -0700 | [diff] [blame] | 1098 | |
Chia-I Wu | f48700b | 2015-11-10 16:21:09 +0800 | [diff] [blame] | 1099 | tex_obj->mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO; |
Dominik Witczak | 2fe1c70 | 2015-09-22 18:25:33 +0200 | [diff] [blame] | 1100 | tex_obj->mem_alloc.pNext = NULL; |
| 1101 | tex_obj->mem_alloc.allocationSize = mem_reqs.size; |
| 1102 | tex_obj->mem_alloc.memoryTypeIndex = 0; |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 1103 | |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1104 | pass = memory_type_from_properties(demo, mem_reqs.memoryTypeBits, |
| 1105 | required_props, |
| 1106 | &tex_obj->mem_alloc.memoryTypeIndex); |
Courtney Goeltzenleuchter | 87a0ef0 | 2015-10-22 11:03:31 -0600 | [diff] [blame] | 1107 | assert(pass); |
Mark Lobodzinski | eadf998 | 2015-07-02 16:49:40 -0600 | [diff] [blame] | 1108 | |
Mark Lobodzinski | 2dcdfd7 | 2015-05-29 09:32:35 -0500 | [diff] [blame] | 1109 | /* allocate memory */ |
Chia-I Wu | a8fe78a | 2015-10-27 18:04:07 +0800 | [diff] [blame] | 1110 | err = vkAllocateMemory(demo->device, &tex_obj->mem_alloc, NULL, |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1111 | &(tex_obj->mem)); |
Mark Lobodzinski | 2dcdfd7 | 2015-05-29 09:32:35 -0500 | [diff] [blame] | 1112 | assert(!err); |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 1113 | |
Mark Lobodzinski | 2dcdfd7 | 2015-05-29 09:32:35 -0500 | [diff] [blame] | 1114 | /* bind memory */ |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1115 | err = vkBindImageMemory(demo->device, tex_obj->image, tex_obj->mem, 0); |
Mark Lobodzinski | 2dcdfd7 | 2015-05-29 09:32:35 -0500 | [diff] [blame] | 1116 | assert(!err); |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 1117 | |
Tony Barbour | 8a9f62a | 2015-10-15 12:42:56 -0600 | [diff] [blame] | 1118 | if (required_props & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) { |
Courtney Goeltzenleuchter | 78351a6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 1119 | const VkImageSubresource subres = { |
Chia-I Wu | a014143 | 2015-10-27 19:55:05 +0800 | [diff] [blame] | 1120 | .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT, |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 1121 | .mipLevel = 0, |
Courtney Goeltzenleuchter | fc465b8 | 2015-09-10 16:38:41 -0600 | [diff] [blame] | 1122 | .arrayLayer = 0, |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 1123 | }; |
Courtney Goeltzenleuchter | 78351a6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 1124 | VkSubresourceLayout layout; |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 1125 | void *data; |
| 1126 | |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1127 | vkGetImageSubresourceLayout(demo->device, tex_obj->image, &subres, |
| 1128 | &layout); |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 1129 | |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1130 | err = vkMapMemory(demo->device, tex_obj->mem, 0, |
| 1131 | tex_obj->mem_alloc.allocationSize, 0, &data); |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 1132 | assert(!err); |
| 1133 | |
| 1134 | if (!loadTexture(filename, data, &layout, &tex_width, &tex_height)) { |
| 1135 | fprintf(stderr, "Error loading texture: %s\n", filename); |
| 1136 | } |
| 1137 | |
Mark Lobodzinski | e4c92a6 | 2015-09-07 13:59:43 -0600 | [diff] [blame] | 1138 | vkUnmapMemory(demo->device, tex_obj->mem); |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 1139 | } |
Courtney Goeltzenleuchter | 752eb8e | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 1140 | |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1141 | tex_obj->imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL; |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1142 | demo_set_image_layout(demo, tex_obj->image, VK_IMAGE_ASPECT_COLOR_BIT, |
Tobin Ehlis | f07d955 | 2016-02-11 17:49:23 -0700 | [diff] [blame] | 1143 | VK_IMAGE_LAYOUT_PREINITIALIZED, tex_obj->imageLayout, |
| 1144 | VK_ACCESS_HOST_WRITE_BIT); |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1145 | /* setting the image layout does not reference the actual memory so no need |
| 1146 | * to add a mem ref */ |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 1147 | } |
| 1148 | |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1149 | static void demo_destroy_texture_image(struct demo *demo, |
| 1150 | struct texture_object *tex_objs) { |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 1151 | /* clean up staging resources */ |
Chia-I Wu | 6363606 | 2015-10-26 21:10:41 +0800 | [diff] [blame] | 1152 | vkFreeMemory(demo->device, tex_objs->mem, NULL); |
| 1153 | vkDestroyImage(demo->device, tex_objs->image, NULL); |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 1154 | } |
| 1155 | |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1156 | static void demo_prepare_textures(struct demo *demo) { |
Tony Barbour | 72304ef | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 1157 | const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM; |
Courtney Goeltzenleuchter | 78351a6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 1158 | VkFormatProperties props; |
Mark Lobodzinski | c4fffc7 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 1159 | uint32_t i; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1160 | |
Courtney Goeltzenleuchter | bd5c174 | 2015-10-20 16:40:38 -0600 | [diff] [blame] | 1161 | vkGetPhysicalDeviceFormatProperties(demo->gpu, tex_format, &props); |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 1162 | |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1163 | for (i = 0; i < DEMO_TEXTURE_COUNT; i++) { |
Courtney Goeltzenleuchter | bd5c174 | 2015-10-20 16:40:38 -0600 | [diff] [blame] | 1164 | VkResult U_ASSERT_ONLY err; |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 1165 | |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1166 | if ((props.linearTilingFeatures & |
| 1167 | VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) && |
| 1168 | !demo->use_staging_buffer) { |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 1169 | /* Device can texture using linear textures */ |
| 1170 | demo_prepare_texture_image(demo, tex_files[i], &demo->textures[i], |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1171 | VK_IMAGE_TILING_LINEAR, |
| 1172 | VK_IMAGE_USAGE_SAMPLED_BIT, |
| 1173 | VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT); |
| 1174 | } else if (props.optimalTilingFeatures & |
| 1175 | VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) { |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 1176 | /* Must use staging buffer to copy linear texture to optimized */ |
Courtney Goeltzenleuchter | 752eb8e | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 1177 | struct texture_object staging_texture; |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 1178 | |
| 1179 | memset(&staging_texture, 0, sizeof(staging_texture)); |
| 1180 | demo_prepare_texture_image(demo, tex_files[i], &staging_texture, |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1181 | VK_IMAGE_TILING_LINEAR, |
| 1182 | VK_IMAGE_USAGE_TRANSFER_SRC_BIT, |
| 1183 | VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT); |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 1184 | |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1185 | demo_prepare_texture_image( |
| 1186 | demo, tex_files[i], &demo->textures[i], VK_IMAGE_TILING_OPTIMAL, |
| 1187 | (VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT), |
| 1188 | VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT); |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 1189 | |
Courtney Goeltzenleuchter | 752eb8e | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 1190 | demo_set_image_layout(demo, staging_texture.image, |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1191 | VK_IMAGE_ASPECT_COLOR_BIT, |
| 1192 | staging_texture.imageLayout, |
Tobin Ehlis | f07d955 | 2016-02-11 17:49:23 -0700 | [diff] [blame] | 1193 | VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, |
| 1194 | 0); |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 1195 | |
Courtney Goeltzenleuchter | 752eb8e | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 1196 | demo_set_image_layout(demo, demo->textures[i].image, |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1197 | VK_IMAGE_ASPECT_COLOR_BIT, |
| 1198 | demo->textures[i].imageLayout, |
Tobin Ehlis | f07d955 | 2016-02-11 17:49:23 -0700 | [diff] [blame] | 1199 | VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, |
| 1200 | 0); |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 1201 | |
Courtney Goeltzenleuchter | 78351a6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 1202 | VkImageCopy copy_region = { |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1203 | .srcSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1}, |
| 1204 | .srcOffset = {0, 0, 0}, |
| 1205 | .dstSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1}, |
| 1206 | .dstOffset = {0, 0, 0}, |
| 1207 | .extent = {staging_texture.tex_width, |
| 1208 | staging_texture.tex_height, 1}, |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 1209 | }; |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1210 | vkCmdCopyImage( |
| 1211 | demo->cmd, staging_texture.image, |
| 1212 | VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, demo->textures[i].image, |
| 1213 | VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, ©_region); |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 1214 | |
Courtney Goeltzenleuchter | 752eb8e | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 1215 | demo_set_image_layout(demo, demo->textures[i].image, |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1216 | VK_IMAGE_ASPECT_COLOR_BIT, |
| 1217 | VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, |
Tobin Ehlis | f07d955 | 2016-02-11 17:49:23 -0700 | [diff] [blame] | 1218 | demo->textures[i].imageLayout, |
| 1219 | 0); |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 1220 | |
Courtney Goeltzenleuchter | 752eb8e | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 1221 | demo_flush_init_cmd(demo); |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 1222 | |
Courtney Goeltzenleuchter | f3aeb2b | 2015-04-21 09:30:03 -0600 | [diff] [blame] | 1223 | demo_destroy_texture_image(demo, &staging_texture); |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 1224 | } else { |
Mike Stroyan | 8b89e07 | 2015-06-15 14:21:03 -0600 | [diff] [blame] | 1225 | /* Can't support VK_FORMAT_R8G8B8A8_UNORM !? */ |
| 1226 | assert(!"No support for R8G8B8A8_UNORM as texture image format"); |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 1227 | } |
| 1228 | |
Courtney Goeltzenleuchter | 78351a6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 1229 | const VkSamplerCreateInfo sampler = { |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1230 | .sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO, |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1231 | .pNext = NULL, |
Chia-I Wu | 6ef8c3b | 2015-10-26 16:49:32 +0800 | [diff] [blame] | 1232 | .magFilter = VK_FILTER_NEAREST, |
| 1233 | .minFilter = VK_FILTER_NEAREST, |
Jon Ashburn | 0bec67e | 2016-01-11 13:12:43 -0700 | [diff] [blame] | 1234 | .mipmapMode = VK_SAMPLER_MIPMAP_MODE_NEAREST, |
Chia-I Wu | 6555f3f | 2015-10-26 17:32:47 +0800 | [diff] [blame] | 1235 | .addressModeU = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE, |
| 1236 | .addressModeV = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE, |
| 1237 | .addressModeW = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE, |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1238 | .mipLodBias = 0.0f, |
Jon Ashburn | 55e3fb0 | 2015-12-14 14:59:20 -0700 | [diff] [blame] | 1239 | .anisotropyEnable = VK_FALSE, |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 1240 | .maxAnisotropy = 1, |
Tony Barbour | 72304ef | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 1241 | .compareOp = VK_COMPARE_OP_NEVER, |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1242 | .minLod = 0.0f, |
| 1243 | .maxLod = 0.0f, |
Tony Barbour | cb530c7 | 2015-06-25 16:56:44 -0600 | [diff] [blame] | 1244 | .borderColor = VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE, |
Mark Lobodzinski | 644dc49 | 2015-09-01 15:42:56 -0600 | [diff] [blame] | 1245 | .unnormalizedCoordinates = VK_FALSE, |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1246 | }; |
Courtney Goeltzenleuchter | 6836939 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 1247 | |
Courtney Goeltzenleuchter | 78351a6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 1248 | VkImageViewCreateInfo view = { |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1249 | .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO, |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1250 | .pNext = NULL, |
Chia-I Wu | 1c4086a | 2015-10-26 20:04:44 +0800 | [diff] [blame] | 1251 | .image = VK_NULL_HANDLE, |
Tony Barbour | 72304ef | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 1252 | .viewType = VK_IMAGE_VIEW_TYPE_2D, |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 1253 | .format = tex_format, |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1254 | .components = |
| 1255 | { |
| 1256 | VK_COMPONENT_SWIZZLE_R, VK_COMPONENT_SWIZZLE_G, |
| 1257 | VK_COMPONENT_SWIZZLE_B, VK_COMPONENT_SWIZZLE_A, |
| 1258 | }, |
| 1259 | .subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1}, |
Courtney Goeltzenleuchter | 41866db | 2015-09-01 17:30:39 -0600 | [diff] [blame] | 1260 | .flags = 0, |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1261 | }; |
Jon Ashburn | b2a6665 | 2015-01-16 09:37:43 -0700 | [diff] [blame] | 1262 | |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1263 | /* create sampler */ |
Chia-I Wu | 6363606 | 2015-10-26 21:10:41 +0800 | [diff] [blame] | 1264 | err = vkCreateSampler(demo->device, &sampler, NULL, |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1265 | &demo->textures[i].sampler); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1266 | assert(!err); |
| 1267 | |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1268 | /* create image view */ |
| 1269 | view.image = demo->textures[i].image; |
Chia-I Wu | 6363606 | 2015-10-26 21:10:41 +0800 | [diff] [blame] | 1270 | err = vkCreateImageView(demo->device, &view, NULL, |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1271 | &demo->textures[i].view); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1272 | assert(!err); |
| 1273 | } |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1274 | } |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1275 | |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1276 | void demo_prepare_cube_data_buffer(struct demo *demo) { |
Courtney Goeltzenleuchter | 28c4d5f | 2015-04-14 18:48:46 -0600 | [diff] [blame] | 1277 | VkBufferCreateInfo buf_info; |
Mark Lobodzinski | 2dcdfd7 | 2015-05-29 09:32:35 -0500 | [diff] [blame] | 1278 | VkMemoryRequirements mem_reqs; |
Mark Lobodzinski | c4fffc7 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 1279 | uint8_t *pData; |
Courtney Goeltzenleuchter | 4984d15 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 1280 | int i; |
| 1281 | mat4x4 MVP, VP; |
Tony Barbour | fdc2d35 | 2015-04-22 09:02:32 -0600 | [diff] [blame] | 1282 | VkResult U_ASSERT_ONLY err; |
Courtney Goeltzenleuchter | 87a0ef0 | 2015-10-22 11:03:31 -0600 | [diff] [blame] | 1283 | bool U_ASSERT_ONLY pass; |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1284 | struct vktexcube_vs_uniform data; |
Courtney Goeltzenleuchter | 4984d15 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 1285 | |
Courtney Goeltzenleuchter | 4984d15 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 1286 | mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix); |
Courtney Goeltzenleuchter | 4984d15 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 1287 | mat4x4_mul(MVP, VP, demo->model_matrix); |
| 1288 | memcpy(data.mvp, MVP, sizeof(MVP)); |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1289 | // dumpMatrix("MVP", MVP); |
Courtney Goeltzenleuchter | 4984d15 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 1290 | |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1291 | for (i = 0; i < 12 * 3; i++) { |
| 1292 | data.position[i][0] = g_vertex_buffer_data[i * 3]; |
| 1293 | data.position[i][1] = g_vertex_buffer_data[i * 3 + 1]; |
| 1294 | data.position[i][2] = g_vertex_buffer_data[i * 3 + 2]; |
Courtney Goeltzenleuchter | 4984d15 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 1295 | data.position[i][3] = 1.0f; |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1296 | data.attr[i][0] = g_uv_buffer_data[2 * i]; |
| 1297 | data.attr[i][1] = g_uv_buffer_data[2 * i + 1]; |
Courtney Goeltzenleuchter | 4984d15 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 1298 | data.attr[i][2] = 0; |
| 1299 | data.attr[i][3] = 0; |
| 1300 | } |
| 1301 | |
Chia-I Wu | 8ea21c7 | 2015-01-01 07:55:04 +0800 | [diff] [blame] | 1302 | memset(&buf_info, 0, sizeof(buf_info)); |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1303 | buf_info.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO; |
Courtney Goeltzenleuchter | 42a078d | 2015-04-15 15:29:59 -0600 | [diff] [blame] | 1304 | buf_info.usage = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT; |
Cody Northrop | 9180730 | 2015-07-16 10:29:32 -0600 | [diff] [blame] | 1305 | buf_info.size = sizeof(data); |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1306 | err = |
| 1307 | vkCreateBuffer(demo->device, &buf_info, NULL, &demo->uniform_data.buf); |
Chia-I Wu | 8ea21c7 | 2015-01-01 07:55:04 +0800 | [diff] [blame] | 1308 | assert(!err); |
| 1309 | |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1310 | vkGetBufferMemoryRequirements(demo->device, demo->uniform_data.buf, |
| 1311 | &mem_reqs); |
Chia-I Wu | 8ea21c7 | 2015-01-01 07:55:04 +0800 | [diff] [blame] | 1312 | |
Chia-I Wu | f48700b | 2015-11-10 16:21:09 +0800 | [diff] [blame] | 1313 | demo->uniform_data.mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO; |
Dominik Witczak | 2fe1c70 | 2015-09-22 18:25:33 +0200 | [diff] [blame] | 1314 | demo->uniform_data.mem_alloc.pNext = NULL; |
| 1315 | demo->uniform_data.mem_alloc.allocationSize = mem_reqs.size; |
| 1316 | demo->uniform_data.mem_alloc.memoryTypeIndex = 0; |
| 1317 | |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1318 | pass = memory_type_from_properties( |
| 1319 | demo, mem_reqs.memoryTypeBits, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, |
| 1320 | &demo->uniform_data.mem_alloc.memoryTypeIndex); |
Courtney Goeltzenleuchter | 87a0ef0 | 2015-10-22 11:03:31 -0600 | [diff] [blame] | 1321 | assert(pass); |
Courtney Goeltzenleuchter | 4984d15 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 1322 | |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1323 | err = vkAllocateMemory(demo->device, &demo->uniform_data.mem_alloc, NULL, |
| 1324 | &(demo->uniform_data.mem)); |
Mark Lobodzinski | 2dcdfd7 | 2015-05-29 09:32:35 -0500 | [diff] [blame] | 1325 | assert(!err); |
Courtney Goeltzenleuchter | 4984d15 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 1326 | |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1327 | err = vkMapMemory(demo->device, demo->uniform_data.mem, 0, |
| 1328 | demo->uniform_data.mem_alloc.allocationSize, 0, |
| 1329 | (void **)&pData); |
Mark Lobodzinski | 2dcdfd7 | 2015-05-29 09:32:35 -0500 | [diff] [blame] | 1330 | assert(!err); |
Courtney Goeltzenleuchter | 4984d15 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 1331 | |
Mark Lobodzinski | 2dcdfd7 | 2015-05-29 09:32:35 -0500 | [diff] [blame] | 1332 | memcpy(pData, &data, sizeof data); |
Courtney Goeltzenleuchter | 4984d15 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 1333 | |
Mark Lobodzinski | e4c92a6 | 2015-09-07 13:59:43 -0600 | [diff] [blame] | 1334 | vkUnmapMemory(demo->device, demo->uniform_data.mem); |
Mark Lobodzinski | 2dcdfd7 | 2015-05-29 09:32:35 -0500 | [diff] [blame] | 1335 | |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1336 | err = vkBindBufferMemory(demo->device, demo->uniform_data.buf, |
| 1337 | demo->uniform_data.mem, 0); |
Mark Lobodzinski | 2dcdfd7 | 2015-05-29 09:32:35 -0500 | [diff] [blame] | 1338 | assert(!err); |
Chia-I Wu | 8ea21c7 | 2015-01-01 07:55:04 +0800 | [diff] [blame] | 1339 | |
Courtney Goeltzenleuchter | c3ad65e | 2015-10-23 13:38:14 -0600 | [diff] [blame] | 1340 | demo->uniform_data.buffer_info.buffer = demo->uniform_data.buf; |
| 1341 | demo->uniform_data.buffer_info.offset = 0; |
| 1342 | demo->uniform_data.buffer_info.range = sizeof(data); |
Courtney Goeltzenleuchter | 4984d15 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 1343 | } |
| 1344 | |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1345 | static void demo_prepare_descriptor_layout(struct demo *demo) { |
Courtney Goeltzenleuchter | 78351a6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 1346 | const VkDescriptorSetLayoutBinding layout_bindings[2] = { |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1347 | [0] = |
| 1348 | { |
| 1349 | .binding = 0, |
| 1350 | .descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, |
| 1351 | .descriptorCount = 1, |
| 1352 | .stageFlags = VK_SHADER_STAGE_VERTEX_BIT, |
| 1353 | .pImmutableSamplers = NULL, |
| 1354 | }, |
| 1355 | [1] = |
| 1356 | { |
| 1357 | .binding = 1, |
| 1358 | .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, |
| 1359 | .descriptorCount = DEMO_TEXTURE_COUNT, |
| 1360 | .stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT, |
| 1361 | .pImmutableSamplers = NULL, |
| 1362 | }, |
Chia-I Wu | a2aa863 | 2015-03-26 15:04:41 +0800 | [diff] [blame] | 1363 | }; |
Courtney Goeltzenleuchter | 78351a6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 1364 | const VkDescriptorSetLayoutCreateInfo descriptor_layout = { |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1365 | .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO, |
Chia-I Wu | 6a3c897 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 1366 | .pNext = NULL, |
Chia-I Wu | 3dfc134 | 2015-10-26 20:48:51 +0800 | [diff] [blame] | 1367 | .bindingCount = 2, |
Jon Ashburn | 238f343 | 2015-12-30 18:01:16 -0700 | [diff] [blame] | 1368 | .pBindings = layout_bindings, |
Chia-I Wu | 6a3c897 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 1369 | }; |
Tony Barbour | fdc2d35 | 2015-04-22 09:02:32 -0600 | [diff] [blame] | 1370 | VkResult U_ASSERT_ONLY err; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1371 | |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1372 | err = vkCreateDescriptorSetLayout(demo->device, &descriptor_layout, NULL, |
| 1373 | &demo->desc_layout); |
Chia-I Wu | b58c24a | 2015-03-26 15:27:55 +0800 | [diff] [blame] | 1374 | assert(!err); |
| 1375 | |
Mark Lobodzinski | 1cfc772 | 2015-04-17 14:11:39 -0500 | [diff] [blame] | 1376 | const VkPipelineLayoutCreateInfo pPipelineLayoutCreateInfo = { |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1377 | .sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO, |
| 1378 | .pNext = NULL, |
Chia-I Wu | 3dfc134 | 2015-10-26 20:48:51 +0800 | [diff] [blame] | 1379 | .setLayoutCount = 1, |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1380 | .pSetLayouts = &demo->desc_layout, |
Mark Lobodzinski | 1cfc772 | 2015-04-17 14:11:39 -0500 | [diff] [blame] | 1381 | }; |
| 1382 | |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1383 | err = vkCreatePipelineLayout(demo->device, &pPipelineLayoutCreateInfo, NULL, |
Mark Lobodzinski | 1cfc772 | 2015-04-17 14:11:39 -0500 | [diff] [blame] | 1384 | &demo->pipeline_layout); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1385 | assert(!err); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1386 | } |
| 1387 | |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1388 | static void demo_prepare_render_pass(struct demo *demo) { |
Chia-I Wu | a74c5b2 | 2015-07-07 11:50:03 +0800 | [diff] [blame] | 1389 | const VkAttachmentDescription attachments[2] = { |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1390 | [0] = |
| 1391 | { |
| 1392 | .format = demo->format, |
| 1393 | .samples = VK_SAMPLE_COUNT_1_BIT, |
| 1394 | .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR, |
| 1395 | .storeOp = VK_ATTACHMENT_STORE_OP_STORE, |
| 1396 | .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE, |
| 1397 | .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE, |
| 1398 | .initialLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, |
| 1399 | .finalLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, |
| 1400 | }, |
| 1401 | [1] = |
| 1402 | { |
| 1403 | .format = demo->depth.format, |
| 1404 | .samples = VK_SAMPLE_COUNT_1_BIT, |
| 1405 | .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR, |
| 1406 | .storeOp = VK_ATTACHMENT_STORE_OP_DONT_CARE, |
| 1407 | .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE, |
| 1408 | .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE, |
| 1409 | .initialLayout = |
| 1410 | VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL, |
| 1411 | .finalLayout = |
| 1412 | VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL, |
| 1413 | }, |
Chia-I Wu | f973e32 | 2015-07-08 13:34:24 +0800 | [diff] [blame] | 1414 | }; |
Chia-I Wu | a74c5b2 | 2015-07-07 11:50:03 +0800 | [diff] [blame] | 1415 | const VkAttachmentReference color_reference = { |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1416 | .attachment = 0, .layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, |
Chia-I Wu | a74c5b2 | 2015-07-07 11:50:03 +0800 | [diff] [blame] | 1417 | }; |
Chia-I Wu | 6555f3f | 2015-10-26 17:32:47 +0800 | [diff] [blame] | 1418 | const VkAttachmentReference depth_reference = { |
| 1419 | .attachment = 1, |
| 1420 | .layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL, |
| 1421 | }; |
Chia-I Wu | a74c5b2 | 2015-07-07 11:50:03 +0800 | [diff] [blame] | 1422 | const VkSubpassDescription subpass = { |
Chia-I Wu | a74c5b2 | 2015-07-07 11:50:03 +0800 | [diff] [blame] | 1423 | .pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS, |
| 1424 | .flags = 0, |
Chia-I Wu | 3dfc134 | 2015-10-26 20:48:51 +0800 | [diff] [blame] | 1425 | .inputAttachmentCount = 0, |
Cody Northrop | 945bb83 | 2015-08-04 11:16:41 -0600 | [diff] [blame] | 1426 | .pInputAttachments = NULL, |
Chia-I Wu | 3dfc134 | 2015-10-26 20:48:51 +0800 | [diff] [blame] | 1427 | .colorAttachmentCount = 1, |
Cody Northrop | 945bb83 | 2015-08-04 11:16:41 -0600 | [diff] [blame] | 1428 | .pColorAttachments = &color_reference, |
| 1429 | .pResolveAttachments = NULL, |
Chia-I Wu | 6555f3f | 2015-10-26 17:32:47 +0800 | [diff] [blame] | 1430 | .pDepthStencilAttachment = &depth_reference, |
Chia-I Wu | 3dfc134 | 2015-10-26 20:48:51 +0800 | [diff] [blame] | 1431 | .preserveAttachmentCount = 0, |
Cody Northrop | 945bb83 | 2015-08-04 11:16:41 -0600 | [diff] [blame] | 1432 | .pPreserveAttachments = NULL, |
Chia-I Wu | a74c5b2 | 2015-07-07 11:50:03 +0800 | [diff] [blame] | 1433 | }; |
Chia-I Wu | f973e32 | 2015-07-08 13:34:24 +0800 | [diff] [blame] | 1434 | const VkRenderPassCreateInfo rp_info = { |
| 1435 | .sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO, |
| 1436 | .pNext = NULL, |
Chia-I Wu | a74c5b2 | 2015-07-07 11:50:03 +0800 | [diff] [blame] | 1437 | .attachmentCount = 2, |
| 1438 | .pAttachments = attachments, |
| 1439 | .subpassCount = 1, |
| 1440 | .pSubpasses = &subpass, |
| 1441 | .dependencyCount = 0, |
| 1442 | .pDependencies = NULL, |
Chia-I Wu | f973e32 | 2015-07-08 13:34:24 +0800 | [diff] [blame] | 1443 | }; |
Chia-I Wu | a74c5b2 | 2015-07-07 11:50:03 +0800 | [diff] [blame] | 1444 | VkResult U_ASSERT_ONLY err; |
Chia-I Wu | f973e32 | 2015-07-08 13:34:24 +0800 | [diff] [blame] | 1445 | |
Chia-I Wu | 6363606 | 2015-10-26 21:10:41 +0800 | [diff] [blame] | 1446 | err = vkCreateRenderPass(demo->device, &rp_info, NULL, &demo->render_pass); |
Chia-I Wu | f973e32 | 2015-07-08 13:34:24 +0800 | [diff] [blame] | 1447 | assert(!err); |
| 1448 | } |
| 1449 | |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1450 | static VkShaderModule |
| 1451 | 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] | 1452 | VkShaderModule module; |
Courtney Goeltzenleuchter | a80a1a9 | 2015-06-24 18:24:19 -0600 | [diff] [blame] | 1453 | VkShaderModuleCreateInfo moduleCreateInfo; |
Courtney Goeltzenleuchter | d3cdaaf | 2015-12-17 09:53:29 -0700 | [diff] [blame] | 1454 | VkResult U_ASSERT_ONLY err; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1455 | |
Courtney Goeltzenleuchter | a80a1a9 | 2015-06-24 18:24:19 -0600 | [diff] [blame] | 1456 | moduleCreateInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO; |
| 1457 | moduleCreateInfo.pNext = NULL; |
| 1458 | |
Courtney Goeltzenleuchter | a95e305 | 2015-10-30 15:19:27 -0600 | [diff] [blame] | 1459 | moduleCreateInfo.codeSize = size; |
| 1460 | moduleCreateInfo.pCode = code; |
| 1461 | moduleCreateInfo.flags = 0; |
| 1462 | err = vkCreateShaderModule(demo->device, &moduleCreateInfo, NULL, &module); |
| 1463 | assert(!err); |
Chia-I Wu | 46176f1 | 2015-10-31 00:31:16 +0800 | [diff] [blame] | 1464 | |
| 1465 | return module; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1466 | } |
| 1467 | |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1468 | char *demo_read_spv(const char *filename, size_t *psize) { |
Courtney Goeltzenleuchter | 97db1c1 | 2014-10-30 15:14:16 -0600 | [diff] [blame] | 1469 | long int size; |
Tony Barbour | f992173 | 2015-04-22 11:36:22 -0600 | [diff] [blame] | 1470 | size_t U_ASSERT_ONLY retval; |
Courtney Goeltzenleuchter | 97db1c1 | 2014-10-30 15:14:16 -0600 | [diff] [blame] | 1471 | void *shader_code; |
| 1472 | |
| 1473 | FILE *fp = fopen(filename, "rb"); |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1474 | if (!fp) |
| 1475 | return NULL; |
Courtney Goeltzenleuchter | 97db1c1 | 2014-10-30 15:14:16 -0600 | [diff] [blame] | 1476 | |
| 1477 | fseek(fp, 0L, SEEK_END); |
| 1478 | size = ftell(fp); |
| 1479 | |
| 1480 | fseek(fp, 0L, SEEK_SET); |
| 1481 | |
| 1482 | shader_code = malloc(size); |
Tony Barbour | fdc2d35 | 2015-04-22 09:02:32 -0600 | [diff] [blame] | 1483 | retval = fread(shader_code, size, 1, fp); |
| 1484 | assert(retval == 1); |
Courtney Goeltzenleuchter | 97db1c1 | 2014-10-30 15:14:16 -0600 | [diff] [blame] | 1485 | |
| 1486 | *psize = size; |
| 1487 | |
Mike Stroyan | 9e9792a | 2015-10-28 11:15:46 -0600 | [diff] [blame] | 1488 | fclose(fp); |
Courtney Goeltzenleuchter | 97db1c1 | 2014-10-30 15:14:16 -0600 | [diff] [blame] | 1489 | return shader_code; |
| 1490 | } |
| 1491 | |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1492 | static VkShaderModule demo_prepare_vs(struct demo *demo) { |
Courtney Goeltzenleuchter | a95e305 | 2015-10-30 15:19:27 -0600 | [diff] [blame] | 1493 | void *vertShaderCode; |
| 1494 | size_t size; |
Courtney Goeltzenleuchter | 97db1c1 | 2014-10-30 15:14:16 -0600 | [diff] [blame] | 1495 | |
Courtney Goeltzenleuchter | a95e305 | 2015-10-30 15:19:27 -0600 | [diff] [blame] | 1496 | vertShaderCode = demo_read_spv("cube-vert.spv", &size); |
| 1497 | |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1498 | demo->vert_shader_module = |
| 1499 | demo_prepare_shader_module(demo, vertShaderCode, size); |
Courtney Goeltzenleuchter | fc3b953 | 2014-10-28 10:32:57 -0600 | [diff] [blame] | 1500 | |
Courtney Goeltzenleuchter | a95e305 | 2015-10-30 15:19:27 -0600 | [diff] [blame] | 1501 | free(vertShaderCode); |
Chia-I Wu | 46176f1 | 2015-10-31 00:31:16 +0800 | [diff] [blame] | 1502 | |
| 1503 | return demo->vert_shader_module; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1504 | } |
| 1505 | |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1506 | static VkShaderModule demo_prepare_fs(struct demo *demo) { |
Courtney Goeltzenleuchter | a95e305 | 2015-10-30 15:19:27 -0600 | [diff] [blame] | 1507 | void *fragShaderCode; |
| 1508 | size_t size; |
Courtney Goeltzenleuchter | 97db1c1 | 2014-10-30 15:14:16 -0600 | [diff] [blame] | 1509 | |
Courtney Goeltzenleuchter | a95e305 | 2015-10-30 15:19:27 -0600 | [diff] [blame] | 1510 | fragShaderCode = demo_read_spv("cube-frag.spv", &size); |
Courtney Goeltzenleuchter | 97db1c1 | 2014-10-30 15:14:16 -0600 | [diff] [blame] | 1511 | |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1512 | demo->frag_shader_module = |
| 1513 | demo_prepare_shader_module(demo, fragShaderCode, size); |
Courtney Goeltzenleuchter | fc3b953 | 2014-10-28 10:32:57 -0600 | [diff] [blame] | 1514 | |
Courtney Goeltzenleuchter | a95e305 | 2015-10-30 15:19:27 -0600 | [diff] [blame] | 1515 | free(fragShaderCode); |
Chia-I Wu | 46176f1 | 2015-10-31 00:31:16 +0800 | [diff] [blame] | 1516 | |
| 1517 | return demo->frag_shader_module; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1518 | } |
| 1519 | |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1520 | static void demo_prepare_pipeline(struct demo *demo) { |
Courtney Goeltzenleuchter | 78351a6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 1521 | VkGraphicsPipelineCreateInfo pipeline; |
Jon Ashburn | c4ab7af | 2015-07-09 15:02:25 -0600 | [diff] [blame] | 1522 | VkPipelineCacheCreateInfo pipelineCache; |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1523 | VkPipelineVertexInputStateCreateInfo vi; |
Tony Barbour | 194bf28 | 2015-07-10 15:29:03 -0600 | [diff] [blame] | 1524 | VkPipelineInputAssemblyStateCreateInfo ia; |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1525 | VkPipelineRasterizationStateCreateInfo rs; |
| 1526 | VkPipelineColorBlendStateCreateInfo cb; |
| 1527 | VkPipelineDepthStencilStateCreateInfo ds; |
| 1528 | VkPipelineViewportStateCreateInfo vp; |
| 1529 | VkPipelineMultisampleStateCreateInfo ms; |
| 1530 | VkDynamicState dynamicStateEnables[VK_DYNAMIC_STATE_RANGE_SIZE]; |
| 1531 | VkPipelineDynamicStateCreateInfo dynamicState; |
Tony Barbour | fdc2d35 | 2015-04-22 09:02:32 -0600 | [diff] [blame] | 1532 | VkResult U_ASSERT_ONLY err; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1533 | |
Piers Daniell | ba839d1 | 2015-09-29 13:01:09 -0600 | [diff] [blame] | 1534 | memset(dynamicStateEnables, 0, sizeof dynamicStateEnables); |
| 1535 | memset(&dynamicState, 0, sizeof dynamicState); |
| 1536 | dynamicState.sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO; |
| 1537 | dynamicState.pDynamicStates = dynamicStateEnables; |
| 1538 | |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1539 | memset(&pipeline, 0, sizeof(pipeline)); |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1540 | pipeline.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO; |
Mark Lobodzinski | 1cfc772 | 2015-04-17 14:11:39 -0500 | [diff] [blame] | 1541 | pipeline.layout = demo->pipeline_layout; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1542 | |
Jason Ekstrand | 515b7ae | 2015-10-15 17:15:25 -0700 | [diff] [blame] | 1543 | memset(&vi, 0, sizeof(vi)); |
| 1544 | vi.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO; |
| 1545 | |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1546 | memset(&ia, 0, sizeof(ia)); |
Tony Barbour | 194bf28 | 2015-07-10 15:29:03 -0600 | [diff] [blame] | 1547 | ia.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO; |
Tony Barbour | 72304ef | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 1548 | ia.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1549 | |
| 1550 | memset(&rs, 0, sizeof(rs)); |
Chia-I Wu | f051d26 | 2015-10-27 19:25:11 +0800 | [diff] [blame] | 1551 | rs.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO; |
| 1552 | rs.polygonMode = VK_POLYGON_MODE_FILL; |
Chia-I Wu | 6555f3f | 2015-10-26 17:32:47 +0800 | [diff] [blame] | 1553 | rs.cullMode = VK_CULL_MODE_BACK_BIT; |
Chia-I Wu | a8fe78a | 2015-10-27 18:04:07 +0800 | [diff] [blame] | 1554 | rs.frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE; |
Courtney Goeltzenleuchter | 0db2c91 | 2015-10-15 12:57:38 -0600 | [diff] [blame] | 1555 | rs.depthClampEnable = VK_FALSE; |
Cody Northrop | 709c174 | 2015-08-17 11:10:49 -0600 | [diff] [blame] | 1556 | rs.rasterizerDiscardEnable = VK_FALSE; |
| 1557 | rs.depthBiasEnable = VK_FALSE; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1558 | |
| 1559 | memset(&cb, 0, sizeof(cb)); |
Tony Barbour | 194bf28 | 2015-07-10 15:29:03 -0600 | [diff] [blame] | 1560 | cb.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO; |
| 1561 | VkPipelineColorBlendAttachmentState att_state[1]; |
Tony Barbour | 29645d0 | 2015-01-16 14:27:35 -0700 | [diff] [blame] | 1562 | memset(att_state, 0, sizeof(att_state)); |
Chia-I Wu | f051d26 | 2015-10-27 19:25:11 +0800 | [diff] [blame] | 1563 | att_state[0].colorWriteMask = 0xf; |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1564 | att_state[0].blendEnable = VK_FALSE; |
Tony Barbour | 29645d0 | 2015-01-16 14:27:35 -0700 | [diff] [blame] | 1565 | cb.attachmentCount = 1; |
| 1566 | cb.pAttachments = att_state; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1567 | |
Tony Barbour | 29645d0 | 2015-01-16 14:27:35 -0700 | [diff] [blame] | 1568 | memset(&vp, 0, sizeof(vp)); |
Tony Barbour | 194bf28 | 2015-07-10 15:29:03 -0600 | [diff] [blame] | 1569 | vp.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO; |
Tony Barbour | 72304ef | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 1570 | vp.viewportCount = 1; |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1571 | dynamicStateEnables[dynamicState.dynamicStateCount++] = |
| 1572 | VK_DYNAMIC_STATE_VIEWPORT; |
Piers Daniell | ba839d1 | 2015-09-29 13:01:09 -0600 | [diff] [blame] | 1573 | vp.scissorCount = 1; |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1574 | dynamicStateEnables[dynamicState.dynamicStateCount++] = |
| 1575 | VK_DYNAMIC_STATE_SCISSOR; |
Tony Barbour | 29645d0 | 2015-01-16 14:27:35 -0700 | [diff] [blame] | 1576 | |
| 1577 | memset(&ds, 0, sizeof(ds)); |
Tony Barbour | 194bf28 | 2015-07-10 15:29:03 -0600 | [diff] [blame] | 1578 | ds.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO; |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1579 | ds.depthTestEnable = VK_TRUE; |
| 1580 | ds.depthWriteEnable = VK_TRUE; |
Chia-I Wu | a8fe78a | 2015-10-27 18:04:07 +0800 | [diff] [blame] | 1581 | ds.depthCompareOp = VK_COMPARE_OP_LESS_OR_EQUAL; |
Cody Northrop | 5e4125d | 2015-08-26 10:01:32 -0600 | [diff] [blame] | 1582 | ds.depthBoundsTestEnable = VK_FALSE; |
Chia-I Wu | f051d26 | 2015-10-27 19:25:11 +0800 | [diff] [blame] | 1583 | ds.back.failOp = VK_STENCIL_OP_KEEP; |
| 1584 | ds.back.passOp = VK_STENCIL_OP_KEEP; |
| 1585 | ds.back.compareOp = VK_COMPARE_OP_ALWAYS; |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1586 | ds.stencilTestEnable = VK_FALSE; |
Tony Barbour | 29645d0 | 2015-01-16 14:27:35 -0700 | [diff] [blame] | 1587 | ds.front = ds.back; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1588 | |
Tony Barbour | 29645d0 | 2015-01-16 14:27:35 -0700 | [diff] [blame] | 1589 | memset(&ms, 0, sizeof(ms)); |
Tony Barbour | 194bf28 | 2015-07-10 15:29:03 -0600 | [diff] [blame] | 1590 | ms.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO; |
Cody Northrop | 011546b | 2015-08-04 14:34:54 -0600 | [diff] [blame] | 1591 | ms.pSampleMask = NULL; |
Chia-I Wu | 3ede946 | 2015-10-31 00:31:16 +0800 | [diff] [blame] | 1592 | ms.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1593 | |
Mark Lobodzinski | 521d776 | 2015-06-23 15:11:57 -0600 | [diff] [blame] | 1594 | // Two stages: vs and fs |
| 1595 | pipeline.stageCount = 2; |
| 1596 | VkPipelineShaderStageCreateInfo shaderStages[2]; |
| 1597 | memset(&shaderStages, 0, 2 * sizeof(VkPipelineShaderStageCreateInfo)); |
| 1598 | |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1599 | shaderStages[0].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO; |
| 1600 | shaderStages[0].stage = VK_SHADER_STAGE_VERTEX_BIT; |
Chia-I Wu | 46176f1 | 2015-10-31 00:31:16 +0800 | [diff] [blame] | 1601 | shaderStages[0].module = demo_prepare_vs(demo); |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1602 | shaderStages[0].pName = "main"; |
Mark Lobodzinski | 521d776 | 2015-06-23 15:11:57 -0600 | [diff] [blame] | 1603 | |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1604 | shaderStages[1].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO; |
| 1605 | shaderStages[1].stage = VK_SHADER_STAGE_FRAGMENT_BIT; |
Chia-I Wu | 46176f1 | 2015-10-31 00:31:16 +0800 | [diff] [blame] | 1606 | shaderStages[1].module = demo_prepare_fs(demo); |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1607 | shaderStages[1].pName = "main"; |
Mark Lobodzinski | 521d776 | 2015-06-23 15:11:57 -0600 | [diff] [blame] | 1608 | |
Jon Ashburn | c4ab7af | 2015-07-09 15:02:25 -0600 | [diff] [blame] | 1609 | memset(&pipelineCache, 0, sizeof(pipelineCache)); |
| 1610 | pipelineCache.sType = VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1611 | |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1612 | err = vkCreatePipelineCache(demo->device, &pipelineCache, NULL, |
| 1613 | &demo->pipelineCache); |
Jon Ashburn | c4ab7af | 2015-07-09 15:02:25 -0600 | [diff] [blame] | 1614 | assert(!err); |
Tony Barbour | 194bf28 | 2015-07-10 15:29:03 -0600 | [diff] [blame] | 1615 | |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1616 | pipeline.pVertexInputState = &vi; |
Tony Barbour | 194bf28 | 2015-07-10 15:29:03 -0600 | [diff] [blame] | 1617 | pipeline.pInputAssemblyState = &ia; |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1618 | pipeline.pRasterizationState = &rs; |
| 1619 | pipeline.pColorBlendState = &cb; |
| 1620 | pipeline.pMultisampleState = &ms; |
| 1621 | pipeline.pViewportState = &vp; |
| 1622 | pipeline.pDepthStencilState = &ds; |
| 1623 | pipeline.pStages = shaderStages; |
| 1624 | pipeline.renderPass = demo->render_pass; |
| 1625 | pipeline.pDynamicState = &dynamicState; |
Tony Barbour | 194bf28 | 2015-07-10 15:29:03 -0600 | [diff] [blame] | 1626 | |
Courtney Goeltzenleuchter | d311fd0 | 2015-08-13 16:55:04 -0600 | [diff] [blame] | 1627 | pipeline.renderPass = demo->render_pass; |
| 1628 | |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1629 | err = vkCreateGraphicsPipelines(demo->device, demo->pipelineCache, 1, |
| 1630 | &pipeline, NULL, &demo->pipeline); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1631 | assert(!err); |
| 1632 | |
Chia-I Wu | 6363606 | 2015-10-26 21:10:41 +0800 | [diff] [blame] | 1633 | vkDestroyShaderModule(demo->device, demo->frag_shader_module, NULL); |
| 1634 | vkDestroyShaderModule(demo->device, demo->vert_shader_module, NULL); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1635 | } |
| 1636 | |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1637 | static void demo_prepare_descriptor_pool(struct demo *demo) { |
Chia-I Wu | f051d26 | 2015-10-27 19:25:11 +0800 | [diff] [blame] | 1638 | const VkDescriptorPoolSize type_counts[2] = { |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1639 | [0] = |
| 1640 | { |
| 1641 | .type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, |
| 1642 | .descriptorCount = 1, |
| 1643 | }, |
| 1644 | [1] = |
| 1645 | { |
| 1646 | .type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, |
| 1647 | .descriptorCount = DEMO_TEXTURE_COUNT, |
| 1648 | }, |
Chia-I Wu | 6a3c897 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 1649 | }; |
Courtney Goeltzenleuchter | 78351a6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 1650 | const VkDescriptorPoolCreateInfo descriptor_pool = { |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1651 | .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO, |
Chia-I Wu | 6a3c897 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 1652 | .pNext = NULL, |
Courtney Goeltzenleuchter | cf92aa4 | 2015-09-16 16:12:45 -0600 | [diff] [blame] | 1653 | .maxSets = 1, |
Chia-I Wu | f051d26 | 2015-10-27 19:25:11 +0800 | [diff] [blame] | 1654 | .poolSizeCount = 2, |
| 1655 | .pPoolSizes = type_counts, |
Chia-I Wu | 6a3c897 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 1656 | }; |
Tony Barbour | fdc2d35 | 2015-04-22 09:02:32 -0600 | [diff] [blame] | 1657 | VkResult U_ASSERT_ONLY err; |
Chia-I Wu | 6a3c897 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 1658 | |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1659 | err = vkCreateDescriptorPool(demo->device, &descriptor_pool, NULL, |
| 1660 | &demo->desc_pool); |
Chia-I Wu | 6a3c897 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 1661 | assert(!err); |
| 1662 | } |
| 1663 | |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1664 | static void demo_prepare_descriptor_set(struct demo *demo) { |
Courtney Goeltzenleuchter | c3ad65e | 2015-10-23 13:38:14 -0600 | [diff] [blame] | 1665 | VkDescriptorImageInfo tex_descs[DEMO_TEXTURE_COUNT]; |
Chia-I Wu | ae721ba | 2015-05-25 16:27:55 +0800 | [diff] [blame] | 1666 | VkWriteDescriptorSet writes[2]; |
Tony Barbour | fdc2d35 | 2015-04-22 09:02:32 -0600 | [diff] [blame] | 1667 | VkResult U_ASSERT_ONLY err; |
Mark Lobodzinski | c4fffc7 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 1668 | uint32_t i; |
Chia-I Wu | 6a3c897 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 1669 | |
Chia-I Wu | a8fe78a | 2015-10-27 18:04:07 +0800 | [diff] [blame] | 1670 | VkDescriptorSetAllocateInfo alloc_info = { |
Chia-I Wu | f48700b | 2015-11-10 16:21:09 +0800 | [diff] [blame] | 1671 | .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO, |
Courtney Goeltzenleuchter | 014ded8 | 2015-10-23 14:21:05 -0600 | [diff] [blame] | 1672 | .pNext = NULL, |
| 1673 | .descriptorPool = demo->desc_pool, |
Jon Ashburn | 0bec67e | 2016-01-11 13:12:43 -0700 | [diff] [blame] | 1674 | .descriptorSetCount = 1, |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1675 | .pSetLayouts = &demo->desc_layout}; |
Chia-I Wu | a8fe78a | 2015-10-27 18:04:07 +0800 | [diff] [blame] | 1676 | err = vkAllocateDescriptorSets(demo->device, &alloc_info, &demo->desc_set); |
Cody Northrop | 1595469 | 2015-08-03 12:47:29 -0600 | [diff] [blame] | 1677 | assert(!err); |
Chia-I Wu | 6a3c897 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 1678 | |
Chia-I Wu | ae721ba | 2015-05-25 16:27:55 +0800 | [diff] [blame] | 1679 | memset(&tex_descs, 0, sizeof(tex_descs)); |
| 1680 | for (i = 0; i < DEMO_TEXTURE_COUNT; i++) { |
Courtney Goeltzenleuchter | c3ad65e | 2015-10-23 13:38:14 -0600 | [diff] [blame] | 1681 | tex_descs[i].sampler = demo->textures[i].sampler; |
| 1682 | tex_descs[i].imageView = demo->textures[i].view; |
| 1683 | tex_descs[i].imageLayout = VK_IMAGE_LAYOUT_GENERAL; |
Chia-I Wu | ae721ba | 2015-05-25 16:27:55 +0800 | [diff] [blame] | 1684 | } |
| 1685 | |
| 1686 | memset(&writes, 0, sizeof(writes)); |
| 1687 | |
| 1688 | writes[0].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET; |
Chia-I Wu | a8fe78a | 2015-10-27 18:04:07 +0800 | [diff] [blame] | 1689 | writes[0].dstSet = demo->desc_set; |
Chia-I Wu | 3dfc134 | 2015-10-26 20:48:51 +0800 | [diff] [blame] | 1690 | writes[0].descriptorCount = 1; |
Chia-I Wu | ae721ba | 2015-05-25 16:27:55 +0800 | [diff] [blame] | 1691 | writes[0].descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER; |
Courtney Goeltzenleuchter | c3ad65e | 2015-10-23 13:38:14 -0600 | [diff] [blame] | 1692 | writes[0].pBufferInfo = &demo->uniform_data.buffer_info; |
Chia-I Wu | ae721ba | 2015-05-25 16:27:55 +0800 | [diff] [blame] | 1693 | |
| 1694 | writes[1].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET; |
Chia-I Wu | a8fe78a | 2015-10-27 18:04:07 +0800 | [diff] [blame] | 1695 | writes[1].dstSet = demo->desc_set; |
| 1696 | writes[1].dstBinding = 1; |
Chia-I Wu | 3dfc134 | 2015-10-26 20:48:51 +0800 | [diff] [blame] | 1697 | writes[1].descriptorCount = DEMO_TEXTURE_COUNT; |
Chia-I Wu | ae721ba | 2015-05-25 16:27:55 +0800 | [diff] [blame] | 1698 | writes[1].descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER; |
Courtney Goeltzenleuchter | c3ad65e | 2015-10-23 13:38:14 -0600 | [diff] [blame] | 1699 | writes[1].pImageInfo = tex_descs; |
Chia-I Wu | ae721ba | 2015-05-25 16:27:55 +0800 | [diff] [blame] | 1700 | |
Mark Lobodzinski | e4c92a6 | 2015-09-07 13:59:43 -0600 | [diff] [blame] | 1701 | vkUpdateDescriptorSets(demo->device, 2, writes, 0, NULL); |
Chia-I Wu | 6a3c897 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 1702 | } |
| 1703 | |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1704 | static void demo_prepare_framebuffers(struct demo *demo) { |
Courtney Goeltzenleuchter | 41866db | 2015-09-01 17:30:39 -0600 | [diff] [blame] | 1705 | VkImageView attachments[2]; |
Cody Northrop | 2e11cad | 2015-08-04 10:47:08 -0600 | [diff] [blame] | 1706 | attachments[1] = demo->depth.view; |
| 1707 | |
Chia-I Wu | f973e32 | 2015-07-08 13:34:24 +0800 | [diff] [blame] | 1708 | const VkFramebufferCreateInfo fb_info = { |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1709 | .sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO, |
| 1710 | .pNext = NULL, |
| 1711 | .renderPass = demo->render_pass, |
| 1712 | .attachmentCount = 2, |
| 1713 | .pAttachments = attachments, |
| 1714 | .width = demo->width, |
| 1715 | .height = demo->height, |
| 1716 | .layers = 1, |
Chia-I Wu | f973e32 | 2015-07-08 13:34:24 +0800 | [diff] [blame] | 1717 | }; |
| 1718 | VkResult U_ASSERT_ONLY err; |
| 1719 | uint32_t i; |
| 1720 | |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1721 | demo->framebuffers = (VkFramebuffer *)malloc(demo->swapchainImageCount * |
| 1722 | sizeof(VkFramebuffer)); |
Tony Barbour | d8e504f | 2015-10-08 14:26:24 -0600 | [diff] [blame] | 1723 | assert(demo->framebuffers); |
| 1724 | |
| 1725 | for (i = 0; i < demo->swapchainImageCount; i++) { |
Cody Northrop | 2e11cad | 2015-08-04 10:47:08 -0600 | [diff] [blame] | 1726 | attachments[0] = demo->buffers[i].view; |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1727 | err = vkCreateFramebuffer(demo->device, &fb_info, NULL, |
| 1728 | &demo->framebuffers[i]); |
Chia-I Wu | f973e32 | 2015-07-08 13:34:24 +0800 | [diff] [blame] | 1729 | assert(!err); |
| 1730 | } |
| 1731 | } |
| 1732 | |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1733 | static void demo_prepare(struct demo *demo) { |
Cody Northrop | 91e221b | 2015-07-09 18:08:32 -0600 | [diff] [blame] | 1734 | VkResult U_ASSERT_ONLY err; |
| 1735 | |
Chia-I Wu | a8fe78a | 2015-10-27 18:04:07 +0800 | [diff] [blame] | 1736 | const VkCommandPoolCreateInfo cmd_pool_info = { |
| 1737 | .sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO, |
Cody Northrop | 91e221b | 2015-07-09 18:08:32 -0600 | [diff] [blame] | 1738 | .pNext = NULL, |
| 1739 | .queueFamilyIndex = demo->graphics_queue_node_index, |
| 1740 | .flags = 0, |
| 1741 | }; |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1742 | err = vkCreateCommandPool(demo->device, &cmd_pool_info, NULL, |
| 1743 | &demo->cmd_pool); |
Cody Northrop | 91e221b | 2015-07-09 18:08:32 -0600 | [diff] [blame] | 1744 | assert(!err); |
| 1745 | |
Chia-I Wu | a8fe78a | 2015-10-27 18:04:07 +0800 | [diff] [blame] | 1746 | const VkCommandBufferAllocateInfo cmd = { |
Chia-I Wu | f48700b | 2015-11-10 16:21:09 +0800 | [diff] [blame] | 1747 | .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO, |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1748 | .pNext = NULL, |
Chia-I Wu | a8fe78a | 2015-10-27 18:04:07 +0800 | [diff] [blame] | 1749 | .commandPool = demo->cmd_pool, |
| 1750 | .level = VK_COMMAND_BUFFER_LEVEL_PRIMARY, |
Jon Ashburn | 0bec67e | 2016-01-11 13:12:43 -0700 | [diff] [blame] | 1751 | .commandBufferCount = 1, |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1752 | }; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1753 | |
| 1754 | demo_prepare_buffers(demo); |
| 1755 | demo_prepare_depth(demo); |
| 1756 | demo_prepare_textures(demo); |
Courtney Goeltzenleuchter | 8879d3a | 2014-10-29 08:29:35 -0600 | [diff] [blame] | 1757 | demo_prepare_cube_data_buffer(demo); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1758 | |
Chia-I Wu | 6a3c897 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 1759 | demo_prepare_descriptor_layout(demo); |
Chia-I Wu | f973e32 | 2015-07-08 13:34:24 +0800 | [diff] [blame] | 1760 | demo_prepare_render_pass(demo); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1761 | demo_prepare_pipeline(demo); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1762 | |
Ian Elliott | a078197 | 2015-08-21 15:09:33 -0600 | [diff] [blame] | 1763 | for (uint32_t i = 0; i < demo->swapchainImageCount; i++) { |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1764 | err = |
| 1765 | vkAllocateCommandBuffers(demo->device, &cmd, &demo->buffers[i].cmd); |
Courtney Goeltzenleuchter | efc58ae | 2015-02-17 09:48:44 -0700 | [diff] [blame] | 1766 | assert(!err); |
| 1767 | } |
Chia-I Wu | 6a3c897 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 1768 | |
Chia-I Wu | 63ea926 | 2015-03-26 13:14:16 +0800 | [diff] [blame] | 1769 | demo_prepare_descriptor_pool(demo); |
Chia-I Wu | 6a3c897 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 1770 | demo_prepare_descriptor_set(demo); |
Courtney Goeltzenleuchter | efc58ae | 2015-02-17 09:48:44 -0700 | [diff] [blame] | 1771 | |
Chia-I Wu | f973e32 | 2015-07-08 13:34:24 +0800 | [diff] [blame] | 1772 | demo_prepare_framebuffers(demo); |
Courtney Goeltzenleuchter | efc58ae | 2015-02-17 09:48:44 -0700 | [diff] [blame] | 1773 | |
Ian Elliott | a078197 | 2015-08-21 15:09:33 -0600 | [diff] [blame] | 1774 | for (uint32_t i = 0; i < demo->swapchainImageCount; i++) { |
Courtney Goeltzenleuchter | efc58ae | 2015-02-17 09:48:44 -0700 | [diff] [blame] | 1775 | demo->current_buffer = i; |
| 1776 | demo_draw_build_cmd(demo, demo->buffers[i].cmd); |
| 1777 | } |
| 1778 | |
Courtney Goeltzenleuchter | 752eb8e | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 1779 | /* |
| 1780 | * Prepare functions above may generate pipeline commands |
| 1781 | * that need to be flushed before beginning the render loop. |
| 1782 | */ |
| 1783 | demo_flush_init_cmd(demo); |
| 1784 | |
Courtney Goeltzenleuchter | efc58ae | 2015-02-17 09:48:44 -0700 | [diff] [blame] | 1785 | demo->current_buffer = 0; |
Mike Stroyan | bb60b38 | 2015-10-28 09:46:57 -0600 | [diff] [blame] | 1786 | demo->prepared = true; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1787 | } |
| 1788 | |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1789 | static void demo_cleanup(struct demo *demo) { |
David Pinedo | 2bb7c93 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 1790 | uint32_t i; |
| 1791 | |
| 1792 | demo->prepared = false; |
| 1793 | |
Tony Barbour | d8e504f | 2015-10-08 14:26:24 -0600 | [diff] [blame] | 1794 | for (i = 0; i < demo->swapchainImageCount; i++) { |
Chia-I Wu | 6363606 | 2015-10-26 21:10:41 +0800 | [diff] [blame] | 1795 | vkDestroyFramebuffer(demo->device, demo->framebuffers[i], NULL); |
Tony Barbour | 155cce8 | 2015-07-03 10:33:54 -0600 | [diff] [blame] | 1796 | } |
Tony Barbour | d8e504f | 2015-10-08 14:26:24 -0600 | [diff] [blame] | 1797 | free(demo->framebuffers); |
Chia-I Wu | 6363606 | 2015-10-26 21:10:41 +0800 | [diff] [blame] | 1798 | vkDestroyDescriptorPool(demo->device, demo->desc_pool, NULL); |
Chia-I Wu | f973e32 | 2015-07-08 13:34:24 +0800 | [diff] [blame] | 1799 | |
Chia-I Wu | 6363606 | 2015-10-26 21:10:41 +0800 | [diff] [blame] | 1800 | vkDestroyPipeline(demo->device, demo->pipeline, NULL); |
| 1801 | vkDestroyPipelineCache(demo->device, demo->pipelineCache, NULL); |
| 1802 | vkDestroyRenderPass(demo->device, demo->render_pass, NULL); |
| 1803 | vkDestroyPipelineLayout(demo->device, demo->pipeline_layout, NULL); |
| 1804 | vkDestroyDescriptorSetLayout(demo->device, demo->desc_layout, NULL); |
David Pinedo | 2bb7c93 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 1805 | |
| 1806 | for (i = 0; i < DEMO_TEXTURE_COUNT; i++) { |
Chia-I Wu | 6363606 | 2015-10-26 21:10:41 +0800 | [diff] [blame] | 1807 | vkDestroyImageView(demo->device, demo->textures[i].view, NULL); |
| 1808 | vkDestroyImage(demo->device, demo->textures[i].image, NULL); |
| 1809 | vkFreeMemory(demo->device, demo->textures[i].mem, NULL); |
| 1810 | vkDestroySampler(demo->device, demo->textures[i].sampler, NULL); |
David Pinedo | 2bb7c93 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 1811 | } |
Ian Elliott | b08e0d9 | 2015-11-20 11:55:46 -0700 | [diff] [blame] | 1812 | demo->fpDestroySwapchainKHR(demo->device, demo->swapchain, NULL); |
David Pinedo | 2bb7c93 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 1813 | |
Chia-I Wu | 6363606 | 2015-10-26 21:10:41 +0800 | [diff] [blame] | 1814 | vkDestroyImageView(demo->device, demo->depth.view, NULL); |
| 1815 | vkDestroyImage(demo->device, demo->depth.image, NULL); |
| 1816 | vkFreeMemory(demo->device, demo->depth.mem, NULL); |
David Pinedo | 2bb7c93 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 1817 | |
Chia-I Wu | 6363606 | 2015-10-26 21:10:41 +0800 | [diff] [blame] | 1818 | vkDestroyBuffer(demo->device, demo->uniform_data.buf, NULL); |
| 1819 | vkFreeMemory(demo->device, demo->uniform_data.mem, NULL); |
David Pinedo | 2bb7c93 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 1820 | |
Ian Elliott | a078197 | 2015-08-21 15:09:33 -0600 | [diff] [blame] | 1821 | for (i = 0; i < demo->swapchainImageCount; i++) { |
Chia-I Wu | 6363606 | 2015-10-26 21:10:41 +0800 | [diff] [blame] | 1822 | vkDestroyImageView(demo->device, demo->buffers[i].view, NULL); |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1823 | vkFreeCommandBuffers(demo->device, demo->cmd_pool, 1, |
| 1824 | &demo->buffers[i].cmd); |
David Pinedo | 2bb7c93 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 1825 | } |
Ian Elliott | aaae535 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 1826 | free(demo->buffers); |
David Pinedo | 2bb7c93 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 1827 | |
Courtney Goeltzenleuchter | 874eb8d | 2015-09-24 17:16:48 -0600 | [diff] [blame] | 1828 | free(demo->queue_props); |
| 1829 | |
Chia-I Wu | 6363606 | 2015-10-26 21:10:41 +0800 | [diff] [blame] | 1830 | vkDestroyCommandPool(demo->device, demo->cmd_pool, NULL); |
| 1831 | vkDestroyDevice(demo->device, NULL); |
Tony Barbour | d70b42c | 2015-06-30 14:14:19 -0600 | [diff] [blame] | 1832 | if (demo->validate) { |
Courtney Goeltzenleuchter | 2dafae4 | 2015-11-30 12:13:14 -0700 | [diff] [blame] | 1833 | demo->DestroyDebugReportCallback(demo->inst, demo->msg_callback, NULL); |
Tony Barbour | d70b42c | 2015-06-30 14:14:19 -0600 | [diff] [blame] | 1834 | } |
Ian Elliott | b08e0d9 | 2015-11-20 11:55:46 -0700 | [diff] [blame] | 1835 | vkDestroySurfaceKHR(demo->inst, demo->surface, NULL); |
Chia-I Wu | 6363606 | 2015-10-26 21:10:41 +0800 | [diff] [blame] | 1836 | vkDestroyInstance(demo->inst, NULL); |
David Pinedo | 2bb7c93 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 1837 | |
| 1838 | #ifndef _WIN32 |
| 1839 | xcb_destroy_window(demo->connection, demo->window); |
| 1840 | xcb_disconnect(demo->connection); |
Courtney Goeltzenleuchter | 874eb8d | 2015-09-24 17:16:48 -0600 | [diff] [blame] | 1841 | free(demo->atom_wm_delete_window); |
David Pinedo | 2bb7c93 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 1842 | #endif // _WIN32 |
| 1843 | } |
| 1844 | |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1845 | static void demo_resize(struct demo *demo) { |
Ian Elliott | cf074d3 | 2015-10-16 18:02:43 -0600 | [diff] [blame] | 1846 | uint32_t i; |
| 1847 | |
Mike Stroyan | bb60b38 | 2015-10-28 09:46:57 -0600 | [diff] [blame] | 1848 | // Don't react to resize until after first initialization. |
| 1849 | if (!demo->prepared) { |
| 1850 | return; |
| 1851 | } |
Ian Elliott | cf074d3 | 2015-10-16 18:02:43 -0600 | [diff] [blame] | 1852 | // In order to properly resize the window, we must re-create the swapchain |
| 1853 | // AND redo the command buffers, etc. |
| 1854 | // |
| 1855 | // First, perform part of the demo_cleanup() function: |
| 1856 | demo->prepared = false; |
| 1857 | |
| 1858 | for (i = 0; i < demo->swapchainImageCount; i++) { |
Chia-I Wu | 6363606 | 2015-10-26 21:10:41 +0800 | [diff] [blame] | 1859 | vkDestroyFramebuffer(demo->device, demo->framebuffers[i], NULL); |
Ian Elliott | cf074d3 | 2015-10-16 18:02:43 -0600 | [diff] [blame] | 1860 | } |
| 1861 | free(demo->framebuffers); |
Chia-I Wu | 6363606 | 2015-10-26 21:10:41 +0800 | [diff] [blame] | 1862 | vkDestroyDescriptorPool(demo->device, demo->desc_pool, NULL); |
Ian Elliott | cf074d3 | 2015-10-16 18:02:43 -0600 | [diff] [blame] | 1863 | |
Chia-I Wu | 6363606 | 2015-10-26 21:10:41 +0800 | [diff] [blame] | 1864 | vkDestroyPipeline(demo->device, demo->pipeline, NULL); |
| 1865 | vkDestroyPipelineCache(demo->device, demo->pipelineCache, NULL); |
| 1866 | vkDestroyRenderPass(demo->device, demo->render_pass, NULL); |
| 1867 | vkDestroyPipelineLayout(demo->device, demo->pipeline_layout, NULL); |
| 1868 | vkDestroyDescriptorSetLayout(demo->device, demo->desc_layout, NULL); |
Ian Elliott | cf074d3 | 2015-10-16 18:02:43 -0600 | [diff] [blame] | 1869 | |
| 1870 | for (i = 0; i < DEMO_TEXTURE_COUNT; i++) { |
Chia-I Wu | 6363606 | 2015-10-26 21:10:41 +0800 | [diff] [blame] | 1871 | vkDestroyImageView(demo->device, demo->textures[i].view, NULL); |
| 1872 | vkDestroyImage(demo->device, demo->textures[i].image, NULL); |
| 1873 | vkFreeMemory(demo->device, demo->textures[i].mem, NULL); |
| 1874 | vkDestroySampler(demo->device, demo->textures[i].sampler, NULL); |
Ian Elliott | cf074d3 | 2015-10-16 18:02:43 -0600 | [diff] [blame] | 1875 | } |
Ian Elliott | cf074d3 | 2015-10-16 18:02:43 -0600 | [diff] [blame] | 1876 | |
Chia-I Wu | 6363606 | 2015-10-26 21:10:41 +0800 | [diff] [blame] | 1877 | vkDestroyImageView(demo->device, demo->depth.view, NULL); |
| 1878 | vkDestroyImage(demo->device, demo->depth.image, NULL); |
| 1879 | vkFreeMemory(demo->device, demo->depth.mem, NULL); |
Ian Elliott | cf074d3 | 2015-10-16 18:02:43 -0600 | [diff] [blame] | 1880 | |
Chia-I Wu | 6363606 | 2015-10-26 21:10:41 +0800 | [diff] [blame] | 1881 | vkDestroyBuffer(demo->device, demo->uniform_data.buf, NULL); |
| 1882 | vkFreeMemory(demo->device, demo->uniform_data.mem, NULL); |
Ian Elliott | cf074d3 | 2015-10-16 18:02:43 -0600 | [diff] [blame] | 1883 | |
| 1884 | for (i = 0; i < demo->swapchainImageCount; i++) { |
Chia-I Wu | 6363606 | 2015-10-26 21:10:41 +0800 | [diff] [blame] | 1885 | vkDestroyImageView(demo->device, demo->buffers[i].view, NULL); |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1886 | vkFreeCommandBuffers(demo->device, demo->cmd_pool, 1, |
| 1887 | &demo->buffers[i].cmd); |
Ian Elliott | cf074d3 | 2015-10-16 18:02:43 -0600 | [diff] [blame] | 1888 | } |
Chia-I Wu | 6363606 | 2015-10-26 21:10:41 +0800 | [diff] [blame] | 1889 | vkDestroyCommandPool(demo->device, demo->cmd_pool, NULL); |
Ian Elliott | cf074d3 | 2015-10-16 18:02:43 -0600 | [diff] [blame] | 1890 | free(demo->buffers); |
| 1891 | |
Ian Elliott | cf074d3 | 2015-10-16 18:02:43 -0600 | [diff] [blame] | 1892 | // Second, re-perform the demo_prepare() function, which will re-create the |
| 1893 | // swapchain: |
| 1894 | demo_prepare(demo); |
| 1895 | } |
| 1896 | |
David Pinedo | 2bb7c93 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 1897 | // On MS-Windows, make this a global, so it's available to WndProc() |
| 1898 | struct demo demo; |
| 1899 | |
Ian Elliott | 639ca47 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 1900 | #ifdef _WIN32 |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1901 | static void demo_run(struct demo *demo) { |
Courtney Goeltzenleuchter | b7e2270 | 2015-04-27 14:56:34 -0600 | [diff] [blame] | 1902 | if (!demo->prepared) |
| 1903 | return; |
Ian Elliott | 639ca47 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 1904 | // Wait for work to finish before updating MVP. |
| 1905 | vkDeviceWaitIdle(demo->device); |
| 1906 | demo_update_data_buffer(demo); |
| 1907 | |
| 1908 | demo_draw(demo); |
| 1909 | |
| 1910 | // Wait for work to finish before updating MVP. |
| 1911 | vkDeviceWaitIdle(demo->device); |
Ian Elliott | 639ca47 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 1912 | |
David Pinedo | 2bb7c93 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 1913 | demo->curFrame++; |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1914 | if (demo->frameCount != INT_MAX && demo->curFrame == demo->frameCount) { |
Karl Schultz | 8a66391 | 2016-03-24 18:43:41 -0600 | [diff] [blame] | 1915 | PostQuitMessage(validation_error); |
David Pinedo | 2bb7c93 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 1916 | } |
David Pinedo | 2bb7c93 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 1917 | } |
Ian Elliott | 639ca47 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 1918 | |
| 1919 | // MS-Windows event handling function: |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1920 | LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { |
| 1921 | switch (uMsg) { |
Ian Elliott | aaae535 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 1922 | case WM_CLOSE: |
Karl Schultz | 0218c00 | 2016-03-22 17:06:13 -0600 | [diff] [blame] | 1923 | PostQuitMessage(validation_error); |
Tony Barbour | 3dddd5d | 2015-04-29 16:19:20 -0600 | [diff] [blame] | 1924 | break; |
Ian Elliott | aaae535 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 1925 | case WM_PAINT: |
Ian Elliott | 639ca47 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 1926 | demo_run(&demo); |
Tony Barbour | c8a5989 | 2015-10-20 12:49:46 -0600 | [diff] [blame] | 1927 | break; |
Ian Elliott | 5ef45e9 | 2015-10-21 15:14:07 -0600 | [diff] [blame] | 1928 | case WM_SIZE: |
Piers Daniell | c0b6e43 | 2016-02-18 10:54:15 -0700 | [diff] [blame] | 1929 | // Resize the application to the new window size, except when |
| 1930 | // it was minimized. Vulkan doesn't support images or swapchains |
| 1931 | // with width=0 and height=0. |
| 1932 | if (wParam != SIZE_MINIMIZED) { |
| 1933 | demo.width = lParam & 0xffff; |
| 1934 | demo.height = lParam & 0xffff0000 >> 16; |
| 1935 | demo_resize(&demo); |
| 1936 | } |
Ian Elliott | 5ef45e9 | 2015-10-21 15:14:07 -0600 | [diff] [blame] | 1937 | break; |
Ian Elliott | 639ca47 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 1938 | default: |
| 1939 | break; |
| 1940 | } |
| 1941 | return (DefWindowProc(hWnd, uMsg, wParam, lParam)); |
| 1942 | } |
| 1943 | |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1944 | static void demo_create_window(struct demo *demo) { |
| 1945 | WNDCLASSEX win_class; |
Ian Elliott | 639ca47 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 1946 | |
| 1947 | // Initialize the window class structure: |
| 1948 | win_class.cbSize = sizeof(WNDCLASSEX); |
| 1949 | win_class.style = CS_HREDRAW | CS_VREDRAW; |
| 1950 | win_class.lpfnWndProc = WndProc; |
| 1951 | win_class.cbClsExtra = 0; |
| 1952 | win_class.cbWndExtra = 0; |
| 1953 | win_class.hInstance = demo->connection; // hInstance |
| 1954 | win_class.hIcon = LoadIcon(NULL, IDI_APPLICATION); |
| 1955 | win_class.hCursor = LoadCursor(NULL, IDC_ARROW); |
| 1956 | win_class.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); |
| 1957 | win_class.lpszMenuName = NULL; |
| 1958 | win_class.lpszClassName = demo->name; |
| 1959 | win_class.hIconSm = LoadIcon(NULL, IDI_WINLOGO); |
| 1960 | // Register window class: |
| 1961 | if (!RegisterClassEx(&win_class)) { |
| 1962 | // It didn't work, so try to give a useful error: |
| 1963 | printf("Unexpected error trying to start the application!\n"); |
| 1964 | fflush(stdout); |
| 1965 | exit(1); |
| 1966 | } |
| 1967 | // Create window with the registered class: |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1968 | RECT wr = {0, 0, demo->width, demo->height}; |
Mike Stroyan | b46779e | 2015-06-15 14:20:13 -0600 | [diff] [blame] | 1969 | AdjustWindowRect(&wr, WS_OVERLAPPEDWINDOW, FALSE); |
Ian Elliott | 639ca47 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 1970 | demo->window = CreateWindowEx(0, |
| 1971 | demo->name, // class name |
| 1972 | demo->name, // app name |
| 1973 | WS_OVERLAPPEDWINDOW | // window style |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1974 | WS_VISIBLE | WS_SYSMENU, |
| 1975 | 100, 100, // x/y coords |
| 1976 | wr.right - wr.left, // width |
| 1977 | wr.bottom - wr.top, // height |
| 1978 | NULL, // handle to parent |
| 1979 | NULL, // handle to menu |
| 1980 | demo->connection, // hInstance |
| 1981 | NULL); // no extra parameters |
Ian Elliott | 639ca47 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 1982 | if (!demo->window) { |
| 1983 | // It didn't work, so try to give a useful error: |
| 1984 | printf("Cannot create a window in which to draw!\n"); |
| 1985 | fflush(stdout); |
| 1986 | exit(1); |
| 1987 | } |
| 1988 | } |
| 1989 | #else // _WIN32 |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1990 | static void demo_handle_event(struct demo *demo, |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1991 | const xcb_generic_event_t *event) { |
Piers Daniell | 735ee53 | 2015-02-23 16:23:13 -0700 | [diff] [blame] | 1992 | uint8_t event_code = event->response_type & 0x7f; |
Courtney Goeltzenleuchter | 98cb2cb | 2014-11-06 14:27:52 -0700 | [diff] [blame] | 1993 | switch (event_code) { |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1994 | case XCB_EXPOSE: |
Courtney Goeltzenleuchter | c465d6c | 2014-11-18 11:28:09 -0700 | [diff] [blame] | 1995 | // TODO: Resize window |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1996 | break; |
Courtney Goeltzenleuchter | 98cb2cb | 2014-11-06 14:27:52 -0700 | [diff] [blame] | 1997 | case XCB_CLIENT_MESSAGE: |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 1998 | if ((*(xcb_client_message_event_t *)event).data.data32[0] == |
| 1999 | (*demo->atom_wm_delete_window).atom) { |
Courtney Goeltzenleuchter | 98cb2cb | 2014-11-06 14:27:52 -0700 | [diff] [blame] | 2000 | demo->quit = true; |
| 2001 | } |
| 2002 | break; |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 2003 | case XCB_KEY_RELEASE: { |
| 2004 | const xcb_key_release_event_t *key = |
| 2005 | (const xcb_key_release_event_t *)event; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 2006 | |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 2007 | switch (key->detail) { |
| 2008 | case 0x9: // Escape |
| 2009 | demo->quit = true; |
| 2010 | break; |
| 2011 | case 0x71: // left arrow key |
| 2012 | demo->spin_angle += demo->spin_increment; |
| 2013 | break; |
| 2014 | case 0x72: // right arrow key |
| 2015 | demo->spin_angle -= demo->spin_increment; |
| 2016 | break; |
| 2017 | case 0x41: |
| 2018 | demo->pause = !demo->pause; |
| 2019 | break; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 2020 | } |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 2021 | } break; |
| 2022 | case XCB_CONFIGURE_NOTIFY: { |
| 2023 | const xcb_configure_notify_event_t *cfg = |
| 2024 | (const xcb_configure_notify_event_t *)event; |
| 2025 | if ((demo->width != cfg->width) || (demo->height != cfg->height)) { |
Damien Leone | 745a0b4 | 2016-01-26 14:34:12 -0800 | [diff] [blame] | 2026 | demo->width = cfg->width; |
| 2027 | demo->height = cfg->height; |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 2028 | demo_resize(demo); |
Ian Elliott | cf074d3 | 2015-10-16 18:02:43 -0600 | [diff] [blame] | 2029 | } |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 2030 | } break; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 2031 | default: |
| 2032 | break; |
| 2033 | } |
| 2034 | } |
| 2035 | |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 2036 | static void demo_run(struct demo *demo) { |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 2037 | xcb_flush(demo->connection); |
| 2038 | |
| 2039 | while (!demo->quit) { |
| 2040 | xcb_generic_event_t *event; |
| 2041 | |
Courtney Goeltzenleuchter | c465d6c | 2014-11-18 11:28:09 -0700 | [diff] [blame] | 2042 | if (demo->pause) { |
| 2043 | event = xcb_wait_for_event(demo->connection); |
| 2044 | } else { |
| 2045 | event = xcb_poll_for_event(demo->connection); |
| 2046 | } |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 2047 | if (event) { |
| 2048 | demo_handle_event(demo, event); |
| 2049 | free(event); |
Courtney Goeltzenleuchter | 9808555 | 2014-11-10 11:13:13 -0700 | [diff] [blame] | 2050 | } |
Courtney Goeltzenleuchter | c465d6c | 2014-11-18 11:28:09 -0700 | [diff] [blame] | 2051 | |
| 2052 | // Wait for work to finish before updating MVP. |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 2053 | vkDeviceWaitIdle(demo->device); |
Courtney Goeltzenleuchter | c465d6c | 2014-11-18 11:28:09 -0700 | [diff] [blame] | 2054 | demo_update_data_buffer(demo); |
| 2055 | |
Courtney Goeltzenleuchter | 9808555 | 2014-11-10 11:13:13 -0700 | [diff] [blame] | 2056 | demo_draw(demo); |
Courtney Goeltzenleuchter | 1454f3c | 2014-11-18 11:28:09 -0700 | [diff] [blame] | 2057 | |
Courtney Goeltzenleuchter | 9808555 | 2014-11-10 11:13:13 -0700 | [diff] [blame] | 2058 | // Wait for work to finish before updating MVP. |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 2059 | vkDeviceWaitIdle(demo->device); |
David Pinedo | 2bb7c93 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 2060 | demo->curFrame++; |
Tony Barbour | 1a86d03 | 2015-09-21 15:17:33 -0600 | [diff] [blame] | 2061 | if (demo->frameCount != INT32_MAX && demo->curFrame == demo->frameCount) |
David Pinedo | 2bb7c93 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 2062 | demo->quit = true; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 2063 | } |
| 2064 | } |
| 2065 | |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 2066 | static void demo_create_window(struct demo *demo) { |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 2067 | uint32_t value_mask, value_list[32]; |
| 2068 | |
| 2069 | demo->window = xcb_generate_id(demo->connection); |
| 2070 | |
| 2071 | value_mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK; |
| 2072 | value_list[0] = demo->screen->black_pixel; |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 2073 | value_list[1] = XCB_EVENT_MASK_KEY_RELEASE | XCB_EVENT_MASK_EXPOSURE | |
Ian Elliott | cf074d3 | 2015-10-16 18:02:43 -0600 | [diff] [blame] | 2074 | XCB_EVENT_MASK_STRUCTURE_NOTIFY; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 2075 | |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 2076 | xcb_create_window(demo->connection, XCB_COPY_FROM_PARENT, demo->window, |
| 2077 | demo->screen->root, 0, 0, demo->width, demo->height, 0, |
| 2078 | XCB_WINDOW_CLASS_INPUT_OUTPUT, demo->screen->root_visual, |
| 2079 | value_mask, value_list); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 2080 | |
Courtney Goeltzenleuchter | 98cb2cb | 2014-11-06 14:27:52 -0700 | [diff] [blame] | 2081 | /* Magic code that will send notification when window is destroyed */ |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 2082 | xcb_intern_atom_cookie_t cookie = |
| 2083 | xcb_intern_atom(demo->connection, 1, 12, "WM_PROTOCOLS"); |
| 2084 | xcb_intern_atom_reply_t *reply = |
| 2085 | xcb_intern_atom_reply(demo->connection, cookie, 0); |
Courtney Goeltzenleuchter | 98cb2cb | 2014-11-06 14:27:52 -0700 | [diff] [blame] | 2086 | |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 2087 | xcb_intern_atom_cookie_t cookie2 = |
| 2088 | xcb_intern_atom(demo->connection, 0, 16, "WM_DELETE_WINDOW"); |
| 2089 | demo->atom_wm_delete_window = |
| 2090 | xcb_intern_atom_reply(demo->connection, cookie2, 0); |
Courtney Goeltzenleuchter | 98cb2cb | 2014-11-06 14:27:52 -0700 | [diff] [blame] | 2091 | |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 2092 | xcb_change_property(demo->connection, XCB_PROP_MODE_REPLACE, demo->window, |
| 2093 | (*reply).atom, 4, 32, 1, |
Courtney Goeltzenleuchter | 98cb2cb | 2014-11-06 14:27:52 -0700 | [diff] [blame] | 2094 | &(*demo->atom_wm_delete_window).atom); |
| 2095 | free(reply); |
| 2096 | |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 2097 | xcb_map_window(demo->connection, demo->window); |
David Pinedo | 2bb7c93 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 2098 | |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 2099 | // Force the x/y coordinates to 100,100 results are identical in consecutive |
| 2100 | // runs |
| 2101 | const uint32_t coords[] = {100, 100}; |
David Pinedo | 2bb7c93 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 2102 | xcb_configure_window(demo->connection, demo->window, |
| 2103 | XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y, coords); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 2104 | } |
Ian Elliott | 639ca47 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 2105 | #endif // _WIN32 |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 2106 | |
Courtney Goeltzenleuchter | 886f76c | 2015-07-06 17:46:11 -0600 | [diff] [blame] | 2107 | /* |
| 2108 | * Return 1 (true) if all layer names specified in check_names |
| 2109 | * can be found in given layer properties. |
| 2110 | */ |
Courtney Goeltzenleuchter | c88ce51 | 2015-07-09 11:44:38 -0600 | [diff] [blame] | 2111 | static VkBool32 demo_check_layers(uint32_t check_count, char **check_names, |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 2112 | uint32_t layer_count, |
| 2113 | VkLayerProperties *layers) { |
Courtney Goeltzenleuchter | 886f76c | 2015-07-06 17:46:11 -0600 | [diff] [blame] | 2114 | for (uint32_t i = 0; i < check_count; i++) { |
Courtney Goeltzenleuchter | c88ce51 | 2015-07-09 11:44:38 -0600 | [diff] [blame] | 2115 | VkBool32 found = 0; |
Courtney Goeltzenleuchter | 886f76c | 2015-07-06 17:46:11 -0600 | [diff] [blame] | 2116 | for (uint32_t j = 0; j < layer_count; j++) { |
| 2117 | if (!strcmp(check_names[i], layers[j].layerName)) { |
| 2118 | found = 1; |
Dustin Graves | 7c28735 | 2016-01-27 13:48:06 -0700 | [diff] [blame] | 2119 | break; |
Courtney Goeltzenleuchter | 886f76c | 2015-07-06 17:46:11 -0600 | [diff] [blame] | 2120 | } |
| 2121 | } |
| 2122 | if (!found) { |
| 2123 | fprintf(stderr, "Cannot find layer: %s\n", check_names[i]); |
| 2124 | return 0; |
| 2125 | } |
| 2126 | } |
| 2127 | return 1; |
| 2128 | } |
| 2129 | |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 2130 | static void demo_init_vk(struct demo *demo) { |
Tobin Ehlis | 99239dc | 2015-04-16 18:04:57 -0600 | [diff] [blame] | 2131 | VkResult err; |
Courtney Goeltzenleuchter | 922a0fa | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 2132 | uint32_t instance_extension_count = 0; |
Courtney Goeltzenleuchter | be10336 | 2015-06-29 15:39:26 -0600 | [diff] [blame] | 2133 | uint32_t instance_layer_count = 0; |
Tony Barbour | da2bad5 | 2016-01-22 14:36:40 -0700 | [diff] [blame] | 2134 | uint32_t device_validation_layer_count = 0; |
Karl Schultz | 7c50ad6 | 2016-04-01 12:07:03 -0600 | [diff] [blame] | 2135 | char **instance_validation_layers = NULL; |
| 2136 | demo->enabled_extension_count = 0; |
| 2137 | demo->enabled_layer_count = 0; |
Courtney Goeltzenleuchter | be10336 | 2015-06-29 15:39:26 -0600 | [diff] [blame] | 2138 | |
Karl Schultz | 7c50ad6 | 2016-04-01 12:07:03 -0600 | [diff] [blame] | 2139 | char *instance_validation_layers_alt1[] = { |
| 2140 | "VK_LAYER_LUNARG_standard_validation" |
| 2141 | }; |
| 2142 | |
| 2143 | char *instance_validation_layers_alt2[] = { |
Mark Lobodzinski | aaab5c0 | 2016-03-17 15:08:18 -0600 | [diff] [blame] | 2144 | "VK_LAYER_GOOGLE_threading", "VK_LAYER_LUNARG_parameter_validation", |
Karl Schultz | 635272d | 2016-03-07 17:54:07 -0700 | [diff] [blame] | 2145 | "VK_LAYER_LUNARG_device_limits", "VK_LAYER_LUNARG_object_tracker", |
Tobin Ehlis | 1398c71 | 2016-03-09 16:12:48 -0700 | [diff] [blame] | 2146 | "VK_LAYER_LUNARG_image", "VK_LAYER_LUNARG_core_validation", |
Karl Schultz | 7c50ad6 | 2016-04-01 12:07:03 -0600 | [diff] [blame] | 2147 | "VK_LAYER_LUNARG_swapchain", "VK_LAYER_GOOGLE_unique_objects" |
Courtney Goeltzenleuchter | 886f76c | 2015-07-06 17:46:11 -0600 | [diff] [blame] | 2148 | }; |
| 2149 | |
Courtney Goeltzenleuchter | be10336 | 2015-06-29 15:39:26 -0600 | [diff] [blame] | 2150 | /* Look for validation layers */ |
Courtney Goeltzenleuchter | c88ce51 | 2015-07-09 11:44:38 -0600 | [diff] [blame] | 2151 | VkBool32 validation_found = 0; |
Karl Schultz | 7c50ad6 | 2016-04-01 12:07:03 -0600 | [diff] [blame] | 2152 | if (demo->validate) { |
Tobin Ehlis | 99239dc | 2015-04-16 18:04:57 -0600 | [diff] [blame] | 2153 | |
Karl Schultz | 7c50ad6 | 2016-04-01 12:07:03 -0600 | [diff] [blame] | 2154 | err = vkEnumerateInstanceLayerProperties(&instance_layer_count, NULL); |
Dustin Graves | 7c28735 | 2016-01-27 13:48:06 -0700 | [diff] [blame] | 2155 | assert(!err); |
Courtney Goeltzenleuchter | 886f76c | 2015-07-06 17:46:11 -0600 | [diff] [blame] | 2156 | |
Karl Schultz | 7c50ad6 | 2016-04-01 12:07:03 -0600 | [diff] [blame] | 2157 | instance_validation_layers = instance_validation_layers_alt1; |
| 2158 | if (instance_layer_count > 0) { |
| 2159 | VkLayerProperties *instance_layers = |
| 2160 | malloc(sizeof (VkLayerProperties) * instance_layer_count); |
| 2161 | err = vkEnumerateInstanceLayerProperties(&instance_layer_count, |
| 2162 | instance_layers); |
| 2163 | assert(!err); |
Dustin Graves | 7c28735 | 2016-01-27 13:48:06 -0700 | [diff] [blame] | 2164 | |
Karl Schultz | 7c50ad6 | 2016-04-01 12:07:03 -0600 | [diff] [blame] | 2165 | |
| 2166 | validation_found = demo_check_layers( |
| 2167 | ARRAY_SIZE(instance_validation_layers_alt1), |
| 2168 | instance_validation_layers, instance_layer_count, |
| 2169 | instance_layers); |
| 2170 | if (validation_found) { |
| 2171 | demo->enabled_layer_count = ARRAY_SIZE(instance_validation_layers_alt1); |
| 2172 | demo->device_validation_layers[0] = "VK_LAYER_LUNARG_standard_validation"; |
| 2173 | device_validation_layer_count = 1; |
| 2174 | } else { |
| 2175 | // use alternative set of validation layers |
| 2176 | instance_validation_layers = instance_validation_layers_alt2; |
| 2177 | demo->enabled_layer_count = ARRAY_SIZE(instance_validation_layers_alt2); |
| 2178 | validation_found = demo_check_layers( |
| 2179 | ARRAY_SIZE(instance_validation_layers_alt2), |
| 2180 | instance_validation_layers, instance_layer_count, |
| 2181 | instance_layers); |
| 2182 | device_validation_layer_count = |
| 2183 | ARRAY_SIZE(instance_validation_layers_alt2); |
| 2184 | for (uint32_t i = 0; i < device_validation_layer_count; i++) { |
| 2185 | demo->device_validation_layers[i] = |
| 2186 | instance_validation_layers[i]; |
| 2187 | } |
| 2188 | } |
| 2189 | free(instance_layers); |
Courtney Goeltzenleuchter | be10336 | 2015-06-29 15:39:26 -0600 | [diff] [blame] | 2190 | } |
Dustin Graves | 7c28735 | 2016-01-27 13:48:06 -0700 | [diff] [blame] | 2191 | |
Karl Schultz | 7c50ad6 | 2016-04-01 12:07:03 -0600 | [diff] [blame] | 2192 | if (!validation_found) { |
| 2193 | ERR_EXIT("vkEnumerateInstanceLayerProperties failed to find" |
| 2194 | "required validation layer.\n\n" |
| 2195 | "Please look at the Getting Started guide for additional " |
| 2196 | "information.\n", |
| 2197 | "vkCreateInstance Failure"); |
| 2198 | } |
Dustin Graves | 7c28735 | 2016-01-27 13:48:06 -0700 | [diff] [blame] | 2199 | } |
| 2200 | |
| 2201 | /* Look for instance extensions */ |
| 2202 | VkBool32 surfaceExtFound = 0; |
| 2203 | VkBool32 platformSurfaceExtFound = 0; |
Karl Schultz | 7c50ad6 | 2016-04-01 12:07:03 -0600 | [diff] [blame] | 2204 | memset(demo->extension_names, 0, sizeof(demo->extension_names)); |
Dustin Graves | 7c28735 | 2016-01-27 13:48:06 -0700 | [diff] [blame] | 2205 | |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 2206 | err = vkEnumerateInstanceExtensionProperties( |
| 2207 | NULL, &instance_extension_count, NULL); |
Courtney Goeltzenleuchter | be10336 | 2015-06-29 15:39:26 -0600 | [diff] [blame] | 2208 | assert(!err); |
| 2209 | |
Dustin Graves | 7c28735 | 2016-01-27 13:48:06 -0700 | [diff] [blame] | 2210 | if (instance_extension_count > 0) { |
| 2211 | VkExtensionProperties *instance_extensions = |
| 2212 | malloc(sizeof(VkExtensionProperties) * instance_extension_count); |
| 2213 | err = vkEnumerateInstanceExtensionProperties( |
| 2214 | NULL, &instance_extension_count, instance_extensions); |
| 2215 | assert(!err); |
| 2216 | for (uint32_t i = 0; i < instance_extension_count; i++) { |
| 2217 | if (!strcmp(VK_KHR_SURFACE_EXTENSION_NAME, |
| 2218 | instance_extensions[i].extensionName)) { |
| 2219 | surfaceExtFound = 1; |
Karl Schultz | 7c50ad6 | 2016-04-01 12:07:03 -0600 | [diff] [blame] | 2220 | demo->extension_names[demo->enabled_extension_count++] = |
Dustin Graves | 7c28735 | 2016-01-27 13:48:06 -0700 | [diff] [blame] | 2221 | VK_KHR_SURFACE_EXTENSION_NAME; |
Courtney Goeltzenleuchter | be10336 | 2015-06-29 15:39:26 -0600 | [diff] [blame] | 2222 | } |
Dustin Graves | 7c28735 | 2016-01-27 13:48:06 -0700 | [diff] [blame] | 2223 | #ifdef _WIN32 |
| 2224 | if (!strcmp(VK_KHR_WIN32_SURFACE_EXTENSION_NAME, |
| 2225 | instance_extensions[i].extensionName)) { |
| 2226 | platformSurfaceExtFound = 1; |
Karl Schultz | 7c50ad6 | 2016-04-01 12:07:03 -0600 | [diff] [blame] | 2227 | demo->extension_names[demo->enabled_extension_count++] = |
Dustin Graves | 7c28735 | 2016-01-27 13:48:06 -0700 | [diff] [blame] | 2228 | VK_KHR_WIN32_SURFACE_EXTENSION_NAME; |
| 2229 | } |
| 2230 | #else // _WIN32 |
| 2231 | if (!strcmp(VK_KHR_XCB_SURFACE_EXTENSION_NAME, |
| 2232 | instance_extensions[i].extensionName)) { |
| 2233 | platformSurfaceExtFound = 1; |
Karl Schultz | 7c50ad6 | 2016-04-01 12:07:03 -0600 | [diff] [blame] | 2234 | demo->extension_names[demo->enabled_extension_count++] = |
Dustin Graves | 7c28735 | 2016-01-27 13:48:06 -0700 | [diff] [blame] | 2235 | VK_KHR_XCB_SURFACE_EXTENSION_NAME; |
| 2236 | } |
| 2237 | #endif // _WIN32 |
| 2238 | if (!strcmp(VK_EXT_DEBUG_REPORT_EXTENSION_NAME, |
| 2239 | instance_extensions[i].extensionName)) { |
| 2240 | if (demo->validate) { |
Karl Schultz | 7c50ad6 | 2016-04-01 12:07:03 -0600 | [diff] [blame] | 2241 | demo->extension_names[demo->enabled_extension_count++] = |
Dustin Graves | 7c28735 | 2016-01-27 13:48:06 -0700 | [diff] [blame] | 2242 | VK_EXT_DEBUG_REPORT_EXTENSION_NAME; |
| 2243 | } |
| 2244 | } |
Karl Schultz | 7c50ad6 | 2016-04-01 12:07:03 -0600 | [diff] [blame] | 2245 | assert(demo->enabled_extension_count < 64); |
Courtney Goeltzenleuchter | 922a0fa | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 2246 | } |
Dustin Graves | 7c28735 | 2016-01-27 13:48:06 -0700 | [diff] [blame] | 2247 | |
| 2248 | free(instance_extensions); |
Tobin Ehlis | 99239dc | 2015-04-16 18:04:57 -0600 | [diff] [blame] | 2249 | } |
Dustin Graves | 7c28735 | 2016-01-27 13:48:06 -0700 | [diff] [blame] | 2250 | |
Ian Elliott | d3f7dcc | 2015-11-20 13:08:34 -0700 | [diff] [blame] | 2251 | if (!surfaceExtFound) { |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 2252 | ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find " |
| 2253 | "the " VK_KHR_SURFACE_EXTENSION_NAME |
| 2254 | " extension.\n\nDo you have a compatible " |
Ian Elliott | 6515291 | 2015-04-28 13:22:33 -0600 | [diff] [blame] | 2255 | "Vulkan installable client driver (ICD) installed?\nPlease " |
| 2256 | "look at the Getting Started guide for additional " |
| 2257 | "information.\n", |
| 2258 | "vkCreateInstance Failure"); |
| 2259 | } |
Ian Elliott | d3f7dcc | 2015-11-20 13:08:34 -0700 | [diff] [blame] | 2260 | if (!platformSurfaceExtFound) { |
Ian Elliott | d3f7dcc | 2015-11-20 13:08:34 -0700 | [diff] [blame] | 2261 | #ifdef _WIN32 |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 2262 | ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find " |
| 2263 | "the " VK_KHR_WIN32_SURFACE_EXTENSION_NAME |
| 2264 | " extension.\n\nDo you have a compatible " |
Ian Elliott | d3f7dcc | 2015-11-20 13:08:34 -0700 | [diff] [blame] | 2265 | "Vulkan installable client driver (ICD) installed?\nPlease " |
| 2266 | "look at the Getting Started guide for additional " |
| 2267 | "information.\n", |
| 2268 | "vkCreateInstance Failure"); |
Mark Lobodzinski | 6f7a603 | 2015-11-24 12:04:15 -0700 | [diff] [blame] | 2269 | #else // _WIN32 |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 2270 | ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find " |
| 2271 | "the " VK_KHR_XCB_SURFACE_EXTENSION_NAME |
| 2272 | " extension.\n\nDo you have a compatible " |
Mark Lobodzinski | 6f7a603 | 2015-11-24 12:04:15 -0700 | [diff] [blame] | 2273 | "Vulkan installable client driver (ICD) installed?\nPlease " |
| 2274 | "look at the Getting Started guide for additional " |
| 2275 | "information.\n", |
| 2276 | "vkCreateInstance Failure"); |
| 2277 | #endif // _WIN32 |
Ian Elliott | d3f7dcc | 2015-11-20 13:08:34 -0700 | [diff] [blame] | 2278 | } |
Courtney Goeltzenleuchter | 78351a6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 2279 | const VkApplicationInfo app = { |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 2280 | .sType = VK_STRUCTURE_TYPE_APPLICATION_INFO, |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 2281 | .pNext = NULL, |
Chia-I Wu | a8fe78a | 2015-10-27 18:04:07 +0800 | [diff] [blame] | 2282 | .pApplicationName = APP_SHORT_NAME, |
| 2283 | .applicationVersion = 0, |
Ian Elliott | 44e33f7 | 2015-04-28 10:52:52 -0600 | [diff] [blame] | 2284 | .pEngineName = APP_SHORT_NAME, |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 2285 | .engineVersion = 0, |
Jon Ashburn | 7f4bc2c | 2016-03-22 13:57:46 -0600 | [diff] [blame] | 2286 | .apiVersion = VK_API_VERSION_1_0, |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 2287 | }; |
Tony Barbour | 3dddd5d | 2015-04-29 16:19:20 -0600 | [diff] [blame] | 2288 | VkInstanceCreateInfo inst_info = { |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 2289 | .sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO, |
Jon Ashburn | ab46b36 | 2015-04-04 14:52:07 -0600 | [diff] [blame] | 2290 | .pNext = NULL, |
Chia-I Wu | a8fe78a | 2015-10-27 18:04:07 +0800 | [diff] [blame] | 2291 | .pApplicationInfo = &app, |
Karl Schultz | 7c50ad6 | 2016-04-01 12:07:03 -0600 | [diff] [blame] | 2292 | .enabledLayerCount = demo->enabled_layer_count, |
| 2293 | .ppEnabledLayerNames = (const char *const *)instance_validation_layers, |
| 2294 | .enabledExtensionCount = demo->enabled_extension_count, |
| 2295 | .ppEnabledExtensionNames = (const char *const *)demo->extension_names, |
Jon Ashburn | ab46b36 | 2015-04-04 14:52:07 -0600 | [diff] [blame] | 2296 | }; |
Karl Schultz | cd9887d | 2016-03-25 14:25:16 -0600 | [diff] [blame] | 2297 | |
| 2298 | /* |
| 2299 | * This is info for a temp callback to use during CreateInstance. |
| 2300 | * After the instance is created, we use the instance-based |
| 2301 | * function to register the final callback. |
| 2302 | */ |
Ian Elliott | 5959bbd | 2016-03-25 09:07:19 -0600 | [diff] [blame] | 2303 | VkDebugReportCallbackCreateInfoEXT dbgCreateInfo; |
Ian Elliott | 5959bbd | 2016-03-25 09:07:19 -0600 | [diff] [blame] | 2304 | if (demo->validate) { |
Ian Elliott | 5959bbd | 2016-03-25 09:07:19 -0600 | [diff] [blame] | 2305 | dbgCreateInfo.sType = VK_STRUCTURE_TYPE_DEBUG_REPORT_CREATE_INFO_EXT; |
| 2306 | dbgCreateInfo.pNext = NULL; |
Karl Schultz | cd9887d | 2016-03-25 14:25:16 -0600 | [diff] [blame] | 2307 | dbgCreateInfo.pfnCallback = demo->use_break ? BreakCallback : dbgFunc; |
Ian Elliott | 5959bbd | 2016-03-25 09:07:19 -0600 | [diff] [blame] | 2308 | dbgCreateInfo.pUserData = NULL; |
| 2309 | dbgCreateInfo.flags = |
| 2310 | VK_DEBUG_REPORT_ERROR_BIT_EXT | VK_DEBUG_REPORT_WARNING_BIT_EXT; |
| 2311 | inst_info.pNext = &dbgCreateInfo; |
| 2312 | } |
Ian Elliott | 097d9f3 | 2015-04-28 11:35:02 -0600 | [diff] [blame] | 2313 | |
Mark Lobodzinski | c4fffc7 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 2314 | uint32_t gpu_count; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 2315 | |
Chia-I Wu | 6363606 | 2015-10-26 21:10:41 +0800 | [diff] [blame] | 2316 | err = vkCreateInstance(&inst_info, NULL, &demo->inst); |
Ian Elliott | 0726413 | 2015-04-28 11:35:02 -0600 | [diff] [blame] | 2317 | if (err == VK_ERROR_INCOMPATIBLE_DRIVER) { |
| 2318 | ERR_EXIT("Cannot find a compatible Vulkan installable client driver " |
Ian Elliott | 6515291 | 2015-04-28 13:22:33 -0600 | [diff] [blame] | 2319 | "(ICD).\n\nPlease look at the Getting Started guide for " |
Ian Elliott | 0726413 | 2015-04-28 11:35:02 -0600 | [diff] [blame] | 2320 | "additional information.\n", |
| 2321 | "vkCreateInstance Failure"); |
Courtney Goeltzenleuchter | 1e47e4b | 2015-09-14 18:01:17 -0600 | [diff] [blame] | 2322 | } else if (err == VK_ERROR_EXTENSION_NOT_PRESENT) { |
Tony Barbour | 3dddd5d | 2015-04-29 16:19:20 -0600 | [diff] [blame] | 2323 | ERR_EXIT("Cannot find a specified extension library" |
Dustin Graves | 7c28735 | 2016-01-27 13:48:06 -0700 | [diff] [blame] | 2324 | ".\nMake sure your layers path is set appropriately.\n", |
Tony Barbour | 3dddd5d | 2015-04-29 16:19:20 -0600 | [diff] [blame] | 2325 | "vkCreateInstance Failure"); |
Ian Elliott | 0726413 | 2015-04-28 11:35:02 -0600 | [diff] [blame] | 2326 | } else if (err) { |
Ian Elliott | 6515291 | 2015-04-28 13:22:33 -0600 | [diff] [blame] | 2327 | ERR_EXIT("vkCreateInstance failed.\n\nDo you have a compatible Vulkan " |
| 2328 | "installable client driver (ICD) installed?\nPlease look at " |
Ian Elliott | 0726413 | 2015-04-28 11:35:02 -0600 | [diff] [blame] | 2329 | "the Getting Started guide for additional information.\n", |
| 2330 | "vkCreateInstance Failure"); |
Ian Elliott | 3979e28 | 2015-04-03 15:24:55 -0600 | [diff] [blame] | 2331 | } |
Jon Ashburn | ab46b36 | 2015-04-04 14:52:07 -0600 | [diff] [blame] | 2332 | |
Tobin Ehlis | 8a724d8 | 2015-09-07 15:16:39 -0600 | [diff] [blame] | 2333 | /* Make initial call to query gpu_count, then second call for gpu info*/ |
| 2334 | err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, NULL); |
| 2335 | assert(!err && gpu_count > 0); |
Dustin Graves | 7c28735 | 2016-01-27 13:48:06 -0700 | [diff] [blame] | 2336 | |
| 2337 | if (gpu_count > 0) { |
| 2338 | VkPhysicalDevice *physical_devices = malloc(sizeof(VkPhysicalDevice) * gpu_count); |
| 2339 | err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, physical_devices); |
| 2340 | assert(!err); |
| 2341 | /* For cube demo we just grab the first physical device */ |
| 2342 | demo->gpu = physical_devices[0]; |
| 2343 | free(physical_devices); |
| 2344 | } else { |
| 2345 | ERR_EXIT("vkEnumeratePhysicalDevices reported zero accessible devices.\n\n" |
| 2346 | "Do you have a compatible Vulkan installable client driver (ICD) " |
| 2347 | "installed?\nPlease look at the Getting Started guide for " |
| 2348 | "additional information.\n", |
| 2349 | "vkEnumeratePhysicalDevices Failure"); |
| 2350 | } |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 2351 | |
Courtney Goeltzenleuchter | be10336 | 2015-06-29 15:39:26 -0600 | [diff] [blame] | 2352 | /* Look for validation layers */ |
| 2353 | validation_found = 0; |
Tony Barbour | da2bad5 | 2016-01-22 14:36:40 -0700 | [diff] [blame] | 2354 | demo->enabled_layer_count = 0; |
Courtney Goeltzenleuchter | be10336 | 2015-06-29 15:39:26 -0600 | [diff] [blame] | 2355 | uint32_t device_layer_count = 0; |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 2356 | err = |
| 2357 | vkEnumerateDeviceLayerProperties(demo->gpu, &device_layer_count, NULL); |
Courtney Goeltzenleuchter | be10336 | 2015-06-29 15:39:26 -0600 | [diff] [blame] | 2358 | assert(!err); |
| 2359 | |
Dustin Graves | 7c28735 | 2016-01-27 13:48:06 -0700 | [diff] [blame] | 2360 | if (device_layer_count > 0) { |
| 2361 | VkLayerProperties *device_layers = |
| 2362 | malloc(sizeof(VkLayerProperties) * device_layer_count); |
| 2363 | err = vkEnumerateDeviceLayerProperties(demo->gpu, &device_layer_count, |
| 2364 | device_layers); |
| 2365 | assert(!err); |
Courtney Goeltzenleuchter | 886f76c | 2015-07-06 17:46:11 -0600 | [diff] [blame] | 2366 | |
Dustin Graves | 7c28735 | 2016-01-27 13:48:06 -0700 | [diff] [blame] | 2367 | if (demo->validate) { |
| 2368 | validation_found = demo_check_layers(device_validation_layer_count, |
| 2369 | demo->device_validation_layers, |
| 2370 | device_layer_count, |
| 2371 | device_layers); |
| 2372 | demo->enabled_layer_count = device_validation_layer_count; |
Courtney Goeltzenleuchter | be10336 | 2015-06-29 15:39:26 -0600 | [diff] [blame] | 2373 | } |
Dustin Graves | 7c28735 | 2016-01-27 13:48:06 -0700 | [diff] [blame] | 2374 | |
| 2375 | free(device_layers); |
Courtney Goeltzenleuchter | be10336 | 2015-06-29 15:39:26 -0600 | [diff] [blame] | 2376 | } |
| 2377 | |
Dustin Graves | 7c28735 | 2016-01-27 13:48:06 -0700 | [diff] [blame] | 2378 | if (demo->validate && !validation_found) { |
| 2379 | ERR_EXIT("vkEnumerateDeviceLayerProperties failed to find" |
| 2380 | "a required validation layer.\n\n" |
| 2381 | "Please look at the Getting Started guide for additional " |
| 2382 | "information.\n", |
| 2383 | "vkCreateDevice Failure"); |
| 2384 | } |
| 2385 | |
| 2386 | /* Look for device extensions */ |
Courtney Goeltzenleuchter | be10336 | 2015-06-29 15:39:26 -0600 | [diff] [blame] | 2387 | uint32_t device_extension_count = 0; |
Dustin Graves | 7c28735 | 2016-01-27 13:48:06 -0700 | [diff] [blame] | 2388 | VkBool32 swapchainExtFound = 0; |
| 2389 | demo->enabled_extension_count = 0; |
Karl Schultz | 7c50ad6 | 2016-04-01 12:07:03 -0600 | [diff] [blame] | 2390 | memset(demo->extension_names, 0, sizeof(demo->extension_names)); |
Dustin Graves | 7c28735 | 2016-01-27 13:48:06 -0700 | [diff] [blame] | 2391 | |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 2392 | err = vkEnumerateDeviceExtensionProperties(demo->gpu, NULL, |
| 2393 | &device_extension_count, NULL); |
Courtney Goeltzenleuchter | be10336 | 2015-06-29 15:39:26 -0600 | [diff] [blame] | 2394 | assert(!err); |
| 2395 | |
Dustin Graves | 7c28735 | 2016-01-27 13:48:06 -0700 | [diff] [blame] | 2396 | if (device_extension_count > 0) { |
| 2397 | VkExtensionProperties *device_extensions = |
| 2398 | malloc(sizeof(VkExtensionProperties) * device_extension_count); |
| 2399 | err = vkEnumerateDeviceExtensionProperties( |
| 2400 | demo->gpu, NULL, &device_extension_count, device_extensions); |
| 2401 | assert(!err); |
Ian Elliott | aaae535 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 2402 | |
Dustin Graves | 7c28735 | 2016-01-27 13:48:06 -0700 | [diff] [blame] | 2403 | for (uint32_t i = 0; i < device_extension_count; i++) { |
| 2404 | if (!strcmp(VK_KHR_SWAPCHAIN_EXTENSION_NAME, |
| 2405 | device_extensions[i].extensionName)) { |
| 2406 | swapchainExtFound = 1; |
| 2407 | demo->extension_names[demo->enabled_extension_count++] = |
| 2408 | VK_KHR_SWAPCHAIN_EXTENSION_NAME; |
| 2409 | } |
| 2410 | assert(demo->enabled_extension_count < 64); |
Courtney Goeltzenleuchter | be10336 | 2015-06-29 15:39:26 -0600 | [diff] [blame] | 2411 | } |
Dustin Graves | 7c28735 | 2016-01-27 13:48:06 -0700 | [diff] [blame] | 2412 | |
| 2413 | free(device_extensions); |
Courtney Goeltzenleuchter | be10336 | 2015-06-29 15:39:26 -0600 | [diff] [blame] | 2414 | } |
Dustin Graves | 7c28735 | 2016-01-27 13:48:06 -0700 | [diff] [blame] | 2415 | |
Tony Barbour | cc69f45 | 2015-10-08 13:59:42 -0600 | [diff] [blame] | 2416 | if (!swapchainExtFound) { |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 2417 | ERR_EXIT("vkEnumerateDeviceExtensionProperties failed to find " |
| 2418 | "the " VK_KHR_SWAPCHAIN_EXTENSION_NAME |
| 2419 | " extension.\n\nDo you have a compatible " |
Courtney Goeltzenleuchter | be10336 | 2015-06-29 15:39:26 -0600 | [diff] [blame] | 2420 | "Vulkan installable client driver (ICD) installed?\nPlease " |
| 2421 | "look at the Getting Started guide for additional " |
| 2422 | "information.\n", |
| 2423 | "vkCreateInstance Failure"); |
| 2424 | } |
| 2425 | |
Tony Barbour | 3dddd5d | 2015-04-29 16:19:20 -0600 | [diff] [blame] | 2426 | if (demo->validate) { |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 2427 | demo->CreateDebugReportCallback = |
| 2428 | (PFN_vkCreateDebugReportCallbackEXT)vkGetInstanceProcAddr( |
| 2429 | demo->inst, "vkCreateDebugReportCallbackEXT"); |
| 2430 | demo->DestroyDebugReportCallback = |
| 2431 | (PFN_vkDestroyDebugReportCallbackEXT)vkGetInstanceProcAddr( |
| 2432 | demo->inst, "vkDestroyDebugReportCallbackEXT"); |
Courtney Goeltzenleuchter | 2dafae4 | 2015-11-30 12:13:14 -0700 | [diff] [blame] | 2433 | if (!demo->CreateDebugReportCallback) { |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 2434 | ERR_EXIT( |
| 2435 | "GetProcAddr: Unable to find vkCreateDebugReportCallbackEXT\n", |
| 2436 | "vkGetProcAddr Failure"); |
Courtney Goeltzenleuchter | 922a0fa | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 2437 | } |
Courtney Goeltzenleuchter | 2dafae4 | 2015-11-30 12:13:14 -0700 | [diff] [blame] | 2438 | if (!demo->DestroyDebugReportCallback) { |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 2439 | ERR_EXIT( |
| 2440 | "GetProcAddr: Unable to find vkDestroyDebugReportCallbackEXT\n", |
| 2441 | "vkGetProcAddr Failure"); |
Tony Barbour | d70b42c | 2015-06-30 14:14:19 -0600 | [diff] [blame] | 2442 | } |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 2443 | demo->DebugReportMessage = |
| 2444 | (PFN_vkDebugReportMessageEXT)vkGetInstanceProcAddr( |
| 2445 | demo->inst, "vkDebugReportMessageEXT"); |
Courtney Goeltzenleuchter | af1951c | 2015-12-01 14:11:38 -0700 | [diff] [blame] | 2446 | if (!demo->DebugReportMessage) { |
Courtney Goeltzenleuchter | 0f060df | 2015-12-09 15:48:16 -0700 | [diff] [blame] | 2447 | ERR_EXIT("GetProcAddr: Unable to find vkDebugReportMessageEXT\n", |
Courtney Goeltzenleuchter | af1951c | 2015-12-01 14:11:38 -0700 | [diff] [blame] | 2448 | "vkGetProcAddr Failure"); |
| 2449 | } |
Jon Ashburn | 9549c8e | 2015-12-15 08:47:46 -0700 | [diff] [blame] | 2450 | |
Courtney Goeltzenleuchter | 0f060df | 2015-12-09 15:48:16 -0700 | [diff] [blame] | 2451 | VkDebugReportCallbackCreateInfoEXT dbgCreateInfo; |
Karl Schultz | cd9887d | 2016-03-25 14:25:16 -0600 | [diff] [blame] | 2452 | PFN_vkDebugReportCallbackEXT callback; |
| 2453 | callback = demo->use_break ? BreakCallback : dbgFunc; |
Courtney Goeltzenleuchter | 0f060df | 2015-12-09 15:48:16 -0700 | [diff] [blame] | 2454 | dbgCreateInfo.sType = VK_STRUCTURE_TYPE_DEBUG_REPORT_CREATE_INFO_EXT; |
Courtney Goeltzenleuchter | 2dafae4 | 2015-11-30 12:13:14 -0700 | [diff] [blame] | 2455 | dbgCreateInfo.pNext = NULL; |
| 2456 | dbgCreateInfo.pfnCallback = callback; |
| 2457 | dbgCreateInfo.pUserData = NULL; |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 2458 | dbgCreateInfo.flags = |
Mark Lobodzinski | cf9784e | 2016-02-11 09:26:16 -0700 | [diff] [blame] | 2459 | VK_DEBUG_REPORT_ERROR_BIT_EXT | VK_DEBUG_REPORT_WARNING_BIT_EXT; |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 2460 | err = demo->CreateDebugReportCallback(demo->inst, &dbgCreateInfo, NULL, |
| 2461 | &demo->msg_callback); |
Courtney Goeltzenleuchter | 922a0fa | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 2462 | switch (err) { |
| 2463 | case VK_SUCCESS: |
| 2464 | break; |
Courtney Goeltzenleuchter | 922a0fa | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 2465 | case VK_ERROR_OUT_OF_HOST_MEMORY: |
Courtney Goeltzenleuchter | 2dafae4 | 2015-11-30 12:13:14 -0700 | [diff] [blame] | 2466 | ERR_EXIT("CreateDebugReportCallback: out of host memory\n", |
| 2467 | "CreateDebugReportCallback Failure"); |
Courtney Goeltzenleuchter | 922a0fa | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 2468 | break; |
| 2469 | default: |
Courtney Goeltzenleuchter | 2dafae4 | 2015-11-30 12:13:14 -0700 | [diff] [blame] | 2470 | ERR_EXIT("CreateDebugReportCallback: unknown failure\n", |
| 2471 | "CreateDebugReportCallback Failure"); |
Courtney Goeltzenleuchter | 922a0fa | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 2472 | break; |
| 2473 | } |
Tony Barbour | 3dddd5d | 2015-04-29 16:19:20 -0600 | [diff] [blame] | 2474 | } |
Courtney Goeltzenleuchter | bd5c174 | 2015-10-20 16:40:38 -0600 | [diff] [blame] | 2475 | vkGetPhysicalDeviceProperties(demo->gpu, &demo->gpu_props); |
Tobin Ehlis | 9626ba6 | 2015-09-22 13:52:37 -0600 | [diff] [blame] | 2476 | |
| 2477 | /* Call with NULL data to get count */ |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 2478 | vkGetPhysicalDeviceQueueFamilyProperties(demo->gpu, &demo->queue_count, |
| 2479 | NULL); |
Tobin Ehlis | 9626ba6 | 2015-09-22 13:52:37 -0600 | [diff] [blame] | 2480 | assert(demo->queue_count >= 1); |
| 2481 | |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 2482 | demo->queue_props = (VkQueueFamilyProperties *)malloc( |
| 2483 | demo->queue_count * sizeof(VkQueueFamilyProperties)); |
| 2484 | vkGetPhysicalDeviceQueueFamilyProperties(demo->gpu, &demo->queue_count, |
| 2485 | demo->queue_props); |
Tobin Ehlis | 9626ba6 | 2015-09-22 13:52:37 -0600 | [diff] [blame] | 2486 | // Find a queue that supports gfx |
| 2487 | uint32_t gfx_queue_idx = 0; |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 2488 | for (gfx_queue_idx = 0; gfx_queue_idx < demo->queue_count; |
| 2489 | gfx_queue_idx++) { |
Tobin Ehlis | 9626ba6 | 2015-09-22 13:52:37 -0600 | [diff] [blame] | 2490 | if (demo->queue_props[gfx_queue_idx].queueFlags & VK_QUEUE_GRAPHICS_BIT) |
| 2491 | break; |
| 2492 | } |
| 2493 | assert(gfx_queue_idx < demo->queue_count); |
Tobin Ehlis | bf3020e | 2015-09-24 15:18:22 -0600 | [diff] [blame] | 2494 | // Query fine-grained feature support for this device. |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 2495 | // If app has specific feature requirements it should check supported |
| 2496 | // features based on this query |
Tobin Ehlis | 8d03b7c | 2015-09-29 11:22:37 -0600 | [diff] [blame] | 2497 | VkPhysicalDeviceFeatures physDevFeatures; |
Courtney Goeltzenleuchter | bd5c174 | 2015-10-20 16:40:38 -0600 | [diff] [blame] | 2498 | vkGetPhysicalDeviceFeatures(demo->gpu, &physDevFeatures); |
Tobin Ehlis | bf3020e | 2015-09-24 15:18:22 -0600 | [diff] [blame] | 2499 | |
Tony Barbour | da2bad5 | 2016-01-22 14:36:40 -0700 | [diff] [blame] | 2500 | GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfaceSupportKHR); |
| 2501 | GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfaceCapabilitiesKHR); |
| 2502 | GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfaceFormatsKHR); |
| 2503 | GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfacePresentModesKHR); |
| 2504 | GET_INSTANCE_PROC_ADDR(demo->inst, GetSwapchainImagesKHR); |
| 2505 | } |
| 2506 | |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 2507 | static void demo_create_device(struct demo *demo) { |
Tony Barbour | da2bad5 | 2016-01-22 14:36:40 -0700 | [diff] [blame] | 2508 | VkResult U_ASSERT_ONLY err; |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 2509 | float queue_priorities[1] = {0.0}; |
Tobin Ehlis | 9626ba6 | 2015-09-22 13:52:37 -0600 | [diff] [blame] | 2510 | const VkDeviceQueueCreateInfo queue = { |
| 2511 | .sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO, |
| 2512 | .pNext = NULL, |
Tony Barbour | da2bad5 | 2016-01-22 14:36:40 -0700 | [diff] [blame] | 2513 | .queueFamilyIndex = demo->graphics_queue_node_index, |
Chia-I Wu | 8b49744 | 2015-11-06 06:42:02 +0800 | [diff] [blame] | 2514 | .queueCount = 1, |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 2515 | .pQueuePriorities = queue_priorities}; |
Tobin Ehlis | 9626ba6 | 2015-09-22 13:52:37 -0600 | [diff] [blame] | 2516 | |
| 2517 | VkDeviceCreateInfo device = { |
| 2518 | .sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO, |
| 2519 | .pNext = NULL, |
Chia-I Wu | 8b49744 | 2015-11-06 06:42:02 +0800 | [diff] [blame] | 2520 | .queueCreateInfoCount = 1, |
| 2521 | .pQueueCreateInfos = &queue, |
Tony Barbour | da2bad5 | 2016-01-22 14:36:40 -0700 | [diff] [blame] | 2522 | .enabledLayerCount = demo->enabled_layer_count, |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 2523 | .ppEnabledLayerNames = |
| 2524 | (const char *const *)((demo->validate) |
| 2525 | ? demo->device_validation_layers |
| 2526 | : NULL), |
Tony Barbour | da2bad5 | 2016-01-22 14:36:40 -0700 | [diff] [blame] | 2527 | .enabledExtensionCount = demo->enabled_extension_count, |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 2528 | .ppEnabledExtensionNames = (const char *const *)demo->extension_names, |
| 2529 | .pEnabledFeatures = |
| 2530 | NULL, // If specific features are required, pass them in here |
Tobin Ehlis | 9626ba6 | 2015-09-22 13:52:37 -0600 | [diff] [blame] | 2531 | }; |
Tony Barbour | 3dddd5d | 2015-04-29 16:19:20 -0600 | [diff] [blame] | 2532 | |
Chia-I Wu | 6363606 | 2015-10-26 21:10:41 +0800 | [diff] [blame] | 2533 | err = vkCreateDevice(demo->gpu, &device, NULL, &demo->device); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 2534 | assert(!err); |
Courtney Goeltzenleuchter | 3a35c82 | 2015-07-15 17:45:38 -0600 | [diff] [blame] | 2535 | } |
| 2536 | |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 2537 | static void demo_init_vk_swapchain(struct demo *demo) { |
Courtney Goeltzenleuchter | d3cdaaf | 2015-12-17 09:53:29 -0700 | [diff] [blame] | 2538 | VkResult U_ASSERT_ONLY err; |
Courtney Goeltzenleuchter | 3a35c82 | 2015-07-15 17:45:38 -0600 | [diff] [blame] | 2539 | uint32_t i; |
Courtney Goeltzenleuchter | ed667a5 | 2015-03-05 18:09:39 -0700 | [diff] [blame] | 2540 | |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 2541 | // Create a WSI surface for the window: |
Ian Elliott | aaae535 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 2542 | #ifdef _WIN32 |
Ian Elliott | a7a731c | 2015-12-11 15:52:12 -0700 | [diff] [blame] | 2543 | VkWin32SurfaceCreateInfoKHR createInfo; |
| 2544 | createInfo.sType = VK_STRUCTURE_TYPE_WIN32_SURFACE_CREATE_INFO_KHR; |
| 2545 | createInfo.pNext = NULL; |
| 2546 | createInfo.flags = 0; |
Jon Ashburn | b90f50d | 2015-12-24 17:08:01 -0700 | [diff] [blame] | 2547 | createInfo.hinstance = demo->connection; |
| 2548 | createInfo.hwnd = demo->window; |
Ian Elliott | a7a731c | 2015-12-11 15:52:12 -0700 | [diff] [blame] | 2549 | |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 2550 | err = |
| 2551 | vkCreateWin32SurfaceKHR(demo->inst, &createInfo, NULL, &demo->surface); |
Ian Elliott | b08e0d9 | 2015-11-20 11:55:46 -0700 | [diff] [blame] | 2552 | |
Ian Elliott | aaae535 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 2553 | #else // _WIN32 |
Ian Elliott | a7a731c | 2015-12-11 15:52:12 -0700 | [diff] [blame] | 2554 | VkXcbSurfaceCreateInfoKHR createInfo; |
| 2555 | createInfo.sType = VK_STRUCTURE_TYPE_XCB_SURFACE_CREATE_INFO_KHR; |
| 2556 | createInfo.pNext = NULL; |
| 2557 | createInfo.flags = 0; |
| 2558 | createInfo.connection = demo->connection; |
| 2559 | createInfo.window = demo->window; |
| 2560 | |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 2561 | err = vkCreateXcbSurfaceKHR(demo->inst, &createInfo, NULL, &demo->surface); |
Ian Elliott | aaae535 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 2562 | #endif // _WIN32 |
| 2563 | |
Tony Barbour | cc69f45 | 2015-10-08 13:59:42 -0600 | [diff] [blame] | 2564 | // Iterate over each queue to learn whether it supports presenting: |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 2565 | VkBool32 *supportsPresent = |
| 2566 | (VkBool32 *)malloc(demo->queue_count * sizeof(VkBool32)); |
Courtney Goeltzenleuchter | fe95fca | 2015-07-15 17:40:20 -0600 | [diff] [blame] | 2567 | for (i = 0; i < demo->queue_count; i++) { |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 2568 | demo->fpGetPhysicalDeviceSurfaceSupportKHR(demo->gpu, i, demo->surface, |
Ian Elliott | aaae535 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 2569 | &supportsPresent[i]); |
Courtney Goeltzenleuchter | ed667a5 | 2015-03-05 18:09:39 -0700 | [diff] [blame] | 2570 | } |
Ian Elliott | aaae535 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 2571 | |
| 2572 | // Search for a graphics and a present queue in the array of queue |
| 2573 | // families, try to find one that supports both |
| 2574 | uint32_t graphicsQueueNodeIndex = UINT32_MAX; |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 2575 | uint32_t presentQueueNodeIndex = UINT32_MAX; |
Courtney Goeltzenleuchter | fe95fca | 2015-07-15 17:40:20 -0600 | [diff] [blame] | 2576 | for (i = 0; i < demo->queue_count; i++) { |
Ian Elliott | aaae535 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 2577 | if ((demo->queue_props[i].queueFlags & VK_QUEUE_GRAPHICS_BIT) != 0) { |
| 2578 | if (graphicsQueueNodeIndex == UINT32_MAX) { |
| 2579 | graphicsQueueNodeIndex = i; |
| 2580 | } |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 2581 | |
Ian Elliott | aaae535 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 2582 | if (supportsPresent[i] == VK_TRUE) { |
| 2583 | graphicsQueueNodeIndex = i; |
| 2584 | presentQueueNodeIndex = i; |
| 2585 | break; |
| 2586 | } |
| 2587 | } |
| 2588 | } |
| 2589 | if (presentQueueNodeIndex == UINT32_MAX) { |
| 2590 | // If didn't find a queue that supports both graphics and present, then |
| 2591 | // find a separate present queue. |
Courtney Goeltzenleuchter | fe95fca | 2015-07-15 17:40:20 -0600 | [diff] [blame] | 2592 | for (uint32_t i = 0; i < demo->queue_count; ++i) { |
Ian Elliott | aaae535 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 2593 | if (supportsPresent[i] == VK_TRUE) { |
| 2594 | presentQueueNodeIndex = i; |
| 2595 | break; |
| 2596 | } |
| 2597 | } |
| 2598 | } |
| 2599 | free(supportsPresent); |
| 2600 | |
| 2601 | // Generate error if could not find both a graphics and a present queue |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 2602 | if (graphicsQueueNodeIndex == UINT32_MAX || |
| 2603 | presentQueueNodeIndex == UINT32_MAX) { |
Ian Elliott | aaae535 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 2604 | ERR_EXIT("Could not find a graphics and a present queue\n", |
Tony Barbour | cc69f45 | 2015-10-08 13:59:42 -0600 | [diff] [blame] | 2605 | "Swapchain Initialization Failure"); |
Ian Elliott | aaae535 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 2606 | } |
| 2607 | |
| 2608 | // TODO: Add support for separate queues, including presentation, |
Ian Elliott | f2ff802 | 2015-11-23 12:48:15 -0700 | [diff] [blame] | 2609 | // synchronization, and appropriate tracking for QueueSubmit. |
| 2610 | // NOTE: While it is possible for an application to use a separate graphics |
| 2611 | // and a present queues, this demo program assumes it is only using |
| 2612 | // one: |
Ian Elliott | aaae535 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 2613 | if (graphicsQueueNodeIndex != presentQueueNodeIndex) { |
| 2614 | ERR_EXIT("Could not find a common graphics and a present queue\n", |
Tony Barbour | cc69f45 | 2015-10-08 13:59:42 -0600 | [diff] [blame] | 2615 | "Swapchain Initialization Failure"); |
Ian Elliott | aaae535 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 2616 | } |
| 2617 | |
| 2618 | demo->graphics_queue_node_index = graphicsQueueNodeIndex; |
Courtney Goeltzenleuchter | ed667a5 | 2015-03-05 18:09:39 -0700 | [diff] [blame] | 2619 | |
Tony Barbour | da2bad5 | 2016-01-22 14:36:40 -0700 | [diff] [blame] | 2620 | demo_create_device(demo); |
| 2621 | |
| 2622 | GET_DEVICE_PROC_ADDR(demo->device, CreateSwapchainKHR); |
| 2623 | GET_DEVICE_PROC_ADDR(demo->device, DestroySwapchainKHR); |
| 2624 | GET_DEVICE_PROC_ADDR(demo->device, GetSwapchainImagesKHR); |
| 2625 | GET_DEVICE_PROC_ADDR(demo->device, AcquireNextImageKHR); |
| 2626 | GET_DEVICE_PROC_ADDR(demo->device, QueuePresentKHR); |
| 2627 | |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 2628 | vkGetDeviceQueue(demo->device, demo->graphics_queue_node_index, 0, |
| 2629 | &demo->queue); |
Tony Barbour | 3dddd5d | 2015-04-29 16:19:20 -0600 | [diff] [blame] | 2630 | |
Ian Elliott | aaae535 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 2631 | // Get the list of VkFormat's that are supported: |
Ian Elliott | 8528eff | 2015-08-07 15:56:59 -0600 | [diff] [blame] | 2632 | uint32_t formatCount; |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 2633 | err = demo->fpGetPhysicalDeviceSurfaceFormatsKHR(demo->gpu, demo->surface, |
Ian Elliott | f2ff802 | 2015-11-23 12:48:15 -0700 | [diff] [blame] | 2634 | &formatCount, NULL); |
Ian Elliott | aaae535 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 2635 | assert(!err); |
Ian Elliott | a078197 | 2015-08-21 15:09:33 -0600 | [diff] [blame] | 2636 | VkSurfaceFormatKHR *surfFormats = |
| 2637 | (VkSurfaceFormatKHR *)malloc(formatCount * sizeof(VkSurfaceFormatKHR)); |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 2638 | err = demo->fpGetPhysicalDeviceSurfaceFormatsKHR(demo->gpu, demo->surface, |
Ian Elliott | f2ff802 | 2015-11-23 12:48:15 -0700 | [diff] [blame] | 2639 | &formatCount, surfFormats); |
Ian Elliott | aaae535 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 2640 | assert(!err); |
| 2641 | // If the format list includes just one entry of VK_FORMAT_UNDEFINED, |
| 2642 | // the surface has no preferred format. Otherwise, at least one |
| 2643 | // supported format will be returned. |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 2644 | if (formatCount == 1 && surfFormats[0].format == VK_FORMAT_UNDEFINED) { |
Ian Elliott | aaae535 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 2645 | demo->format = VK_FORMAT_B8G8R8A8_UNORM; |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 2646 | } else { |
Ian Elliott | aaae535 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 2647 | assert(formatCount >= 1); |
| 2648 | demo->format = surfFormats[0].format; |
| 2649 | } |
Ian Elliott | 8528eff | 2015-08-07 15:56:59 -0600 | [diff] [blame] | 2650 | demo->color_space = surfFormats[0].colorSpace; |
Ian Elliott | e4602cd | 2015-04-21 16:41:02 -0600 | [diff] [blame] | 2651 | |
David Pinedo | 2bb7c93 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 2652 | demo->quit = false; |
| 2653 | demo->curFrame = 0; |
Mark Lobodzinski | eadf998 | 2015-07-02 16:49:40 -0600 | [diff] [blame] | 2654 | |
| 2655 | // Get Memory information and properties |
Courtney Goeltzenleuchter | bd5c174 | 2015-10-20 16:40:38 -0600 | [diff] [blame] | 2656 | vkGetPhysicalDeviceMemoryProperties(demo->gpu, &demo->memory_properties); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 2657 | } |
| 2658 | |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 2659 | static void demo_init_connection(struct demo *demo) { |
Ian Elliott | 639ca47 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 2660 | #ifndef _WIN32 |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 2661 | const xcb_setup_t *setup; |
| 2662 | xcb_screen_iterator_t iter; |
| 2663 | int scr; |
| 2664 | |
| 2665 | demo->connection = xcb_connect(NULL, &scr); |
Ian Elliott | 3979e28 | 2015-04-03 15:24:55 -0600 | [diff] [blame] | 2666 | if (demo->connection == NULL) { |
| 2667 | printf("Cannot find a compatible Vulkan installable client driver " |
| 2668 | "(ICD).\nExiting ...\n"); |
| 2669 | fflush(stdout); |
| 2670 | exit(1); |
| 2671 | } |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 2672 | |
| 2673 | setup = xcb_get_setup(demo->connection); |
| 2674 | iter = xcb_setup_roots_iterator(setup); |
| 2675 | while (scr-- > 0) |
| 2676 | xcb_screen_next(&iter); |
| 2677 | |
| 2678 | demo->screen = iter.data; |
Ian Elliott | 639ca47 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 2679 | #endif // _WIN32 |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 2680 | } |
| 2681 | |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 2682 | static void demo_init(struct demo *demo, int argc, char **argv) { |
Courtney Goeltzenleuchter | 8879d3a | 2014-10-29 08:29:35 -0600 | [diff] [blame] | 2683 | vec3 eye = {0.0f, 3.0f, 5.0f}; |
| 2684 | vec3 origin = {0, 0, 0}; |
Chia-I Wu | ae3b55d | 2015-04-22 14:56:17 +0800 | [diff] [blame] | 2685 | vec3 up = {0.0f, 1.0f, 0.0}; |
Courtney Goeltzenleuchter | 8879d3a | 2014-10-29 08:29:35 -0600 | [diff] [blame] | 2686 | |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 2687 | memset(demo, 0, sizeof(*demo)); |
Tony Barbour | 1a86d03 | 2015-09-21 15:17:33 -0600 | [diff] [blame] | 2688 | demo->frameCount = INT32_MAX; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 2689 | |
Piers Daniell | 735ee53 | 2015-02-23 16:23:13 -0700 | [diff] [blame] | 2690 | for (int i = 1; i < argc; i++) { |
Tony Barbour | 3dddd5d | 2015-04-29 16:19:20 -0600 | [diff] [blame] | 2691 | if (strcmp(argv[i], "--use_staging") == 0) { |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 2692 | demo->use_staging_buffer = true; |
Tony Barbour | 3dddd5d | 2015-04-29 16:19:20 -0600 | [diff] [blame] | 2693 | continue; |
Ian Elliott | 639ca47 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 2694 | } |
Courtney Goeltzenleuchter | 03f4b77 | 2015-07-22 11:03:51 -0600 | [diff] [blame] | 2695 | if (strcmp(argv[i], "--break") == 0) { |
| 2696 | demo->use_break = true; |
| 2697 | continue; |
| 2698 | } |
Tony Barbour | 3dddd5d | 2015-04-29 16:19:20 -0600 | [diff] [blame] | 2699 | if (strcmp(argv[i], "--validate") == 0) { |
| 2700 | demo->validate = true; |
| 2701 | continue; |
| 2702 | } |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 2703 | if (strcmp(argv[i], "--c") == 0 && demo->frameCount == INT32_MAX && |
| 2704 | i < argc - 1 && sscanf(argv[i + 1], "%d", &demo->frameCount) == 1 && |
| 2705 | demo->frameCount >= 0) { |
David Pinedo | 2bb7c93 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 2706 | i++; |
| 2707 | continue; |
| 2708 | } |
Tony Barbour | 3dddd5d | 2015-04-29 16:19:20 -0600 | [diff] [blame] | 2709 | |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 2710 | fprintf(stderr, "Usage:\n %s [--use_staging] [--validate] [--break] " |
| 2711 | "[--c <framecount>]\n", |
| 2712 | APP_SHORT_NAME); |
Ian Elliott | 639ca47 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 2713 | fflush(stderr); |
| 2714 | exit(1); |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 2715 | } |
| 2716 | |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 2717 | demo_init_connection(demo); |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 2718 | demo_init_vk(demo); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 2719 | |
Courtney Goeltzenleuchter | 8879d3a | 2014-10-29 08:29:35 -0600 | [diff] [blame] | 2720 | demo->width = 500; |
| 2721 | demo->height = 500; |
Courtney Goeltzenleuchter | 8879d3a | 2014-10-29 08:29:35 -0600 | [diff] [blame] | 2722 | |
| 2723 | demo->spin_angle = 0.01f; |
| 2724 | demo->spin_increment = 0.01f; |
| 2725 | demo->pause = false; |
| 2726 | |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 2727 | mat4x4_perspective(demo->projection_matrix, (float)degreesToRadians(45.0f), |
| 2728 | 1.0f, 0.1f, 100.0f); |
Courtney Goeltzenleuchter | 8879d3a | 2014-10-29 08:29:35 -0600 | [diff] [blame] | 2729 | mat4x4_look_at(demo->view_matrix, eye, origin, up); |
| 2730 | mat4x4_identity(demo->model_matrix); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 2731 | } |
| 2732 | |
Ian Elliott | 639ca47 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 2733 | #ifdef _WIN32 |
Mark Young | 418b9d1 | 2016-01-06 14:26:04 -0700 | [diff] [blame] | 2734 | // Include header required for parsing the command line options. |
| 2735 | #include <shellapi.h> |
Ian Elliott | f9cf78c | 2015-04-28 10:33:11 -0600 | [diff] [blame] | 2736 | |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 2737 | int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR pCmdLine, |
| 2738 | int nCmdShow) { |
| 2739 | MSG msg; // message |
| 2740 | bool done; // flag saying when app is complete |
Tony Barbour | 3dddd5d | 2015-04-29 16:19:20 -0600 | [diff] [blame] | 2741 | int argc; |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 2742 | char **argv; |
Ian Elliott | 639ca47 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 2743 | |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 2744 | // Use the CommandLine functions to get the command line arguments. |
| 2745 | // Unfortunately, Microsoft outputs |
| 2746 | // this information as wide characters for Unicode, and we simply want the |
| 2747 | // Ascii version to be compatible |
| 2748 | // with the non-Windows side. So, we have to convert the information to |
| 2749 | // Ascii character strings. |
| 2750 | LPWSTR *commandLineArgs = CommandLineToArgvW(GetCommandLineW(), &argc); |
| 2751 | if (NULL == commandLineArgs) { |
Mark Young | 418b9d1 | 2016-01-06 14:26:04 -0700 | [diff] [blame] | 2752 | argc = 0; |
| 2753 | } |
| 2754 | |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 2755 | if (argc > 0) { |
| 2756 | argv = (char **)malloc(sizeof(char *) * argc); |
| 2757 | if (argv == NULL) { |
Mark Young | 418b9d1 | 2016-01-06 14:26:04 -0700 | [diff] [blame] | 2758 | argc = 0; |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 2759 | } else { |
| 2760 | for (int iii = 0; iii < argc; iii++) { |
| 2761 | size_t wideCharLen = wcslen(commandLineArgs[iii]); |
Mark Young | 418b9d1 | 2016-01-06 14:26:04 -0700 | [diff] [blame] | 2762 | size_t numConverted = 0; |
| 2763 | |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 2764 | argv[iii] = (char *)malloc(sizeof(char) * (wideCharLen + 1)); |
| 2765 | if (argv[iii] != NULL) { |
| 2766 | wcstombs_s(&numConverted, argv[iii], wideCharLen + 1, |
| 2767 | commandLineArgs[iii], wideCharLen + 1); |
Mark Young | 418b9d1 | 2016-01-06 14:26:04 -0700 | [diff] [blame] | 2768 | } |
| 2769 | } |
| 2770 | } |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 2771 | } else { |
Mark Young | 418b9d1 | 2016-01-06 14:26:04 -0700 | [diff] [blame] | 2772 | argv = NULL; |
| 2773 | } |
Tony Barbour | 3dddd5d | 2015-04-29 16:19:20 -0600 | [diff] [blame] | 2774 | |
| 2775 | demo_init(&demo, argc, argv); |
Mark Young | 418b9d1 | 2016-01-06 14:26:04 -0700 | [diff] [blame] | 2776 | |
| 2777 | // 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] | 2778 | if (argc > 0 && argv != NULL) { |
| 2779 | for (int iii = 0; iii < argc; iii++) { |
| 2780 | if (argv[iii] != NULL) { |
Mark Young | 418b9d1 | 2016-01-06 14:26:04 -0700 | [diff] [blame] | 2781 | free(argv[iii]); |
| 2782 | } |
| 2783 | } |
| 2784 | free(argv); |
| 2785 | } |
| 2786 | |
Tony Barbour | 3dddd5d | 2015-04-29 16:19:20 -0600 | [diff] [blame] | 2787 | demo.connection = hInstance; |
| 2788 | strncpy(demo.name, "cube", APP_NAME_STR_LEN); |
Ian Elliott | 639ca47 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 2789 | demo_create_window(&demo); |
Tony Barbour | cc69f45 | 2015-10-08 13:59:42 -0600 | [diff] [blame] | 2790 | demo_init_vk_swapchain(&demo); |
Ian Elliott | 639ca47 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 2791 | |
| 2792 | demo_prepare(&demo); |
| 2793 | |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 2794 | done = false; // initialize loop condition variable |
Mark Young | 418b9d1 | 2016-01-06 14:26:04 -0700 | [diff] [blame] | 2795 | |
| 2796 | // main message loop |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 2797 | while (!done) { |
Ian Elliott | a748eaf | 2015-04-28 15:50:36 -0600 | [diff] [blame] | 2798 | PeekMessage(&msg, NULL, 0, 0, PM_REMOVE); |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 2799 | if (msg.message == WM_QUIT) // check for a quit message |
Ian Elliott | 639ca47 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 2800 | { |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 2801 | done = true; // if found, quit app |
| 2802 | } else { |
Ian Elliott | 639ca47 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 2803 | /* Translate and dispatch to event queue*/ |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 2804 | TranslateMessage(&msg); |
Ian Elliott | 639ca47 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 2805 | DispatchMessage(&msg); |
| 2806 | } |
Tony Barbour | c8a5989 | 2015-10-20 12:49:46 -0600 | [diff] [blame] | 2807 | RedrawWindow(demo.window, NULL, NULL, RDW_INTERNALPAINT); |
Ian Elliott | 639ca47 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 2808 | } |
| 2809 | |
| 2810 | demo_cleanup(&demo); |
| 2811 | |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 2812 | return (int)msg.wParam; |
Ian Elliott | 639ca47 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 2813 | } |
| 2814 | #else // _WIN32 |
Karl Schultz | e4dc75c | 2016-02-02 15:37:51 -0700 | [diff] [blame] | 2815 | int main(int argc, char **argv) { |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 2816 | struct demo demo; |
| 2817 | |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 2818 | demo_init(&demo, argc, argv); |
Chia-I Wu | cbb564e | 2015-04-16 22:02:10 +0800 | [diff] [blame] | 2819 | demo_create_window(&demo); |
Tony Barbour | cc69f45 | 2015-10-08 13:59:42 -0600 | [diff] [blame] | 2820 | demo_init_vk_swapchain(&demo); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 2821 | |
| 2822 | demo_prepare(&demo); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 2823 | demo_run(&demo); |
| 2824 | |
| 2825 | demo_cleanup(&demo); |
| 2826 | |
Karl Schultz | 0218c00 | 2016-03-22 17:06:13 -0600 | [diff] [blame] | 2827 | return validation_error; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 2828 | } |
Ian Elliott | 639ca47 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 2829 | #endif // _WIN32 |