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