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