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