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