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