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