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" |
Courtney Goeltzenleuchter | 6836939 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 48 | #include <png.h> |
Courtney Goeltzenleuchter | fc3b953 | 2014-10-28 10:32:57 -0600 | [diff] [blame] | 49 | |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 50 | #define DEMO_BUFFER_COUNT 2 |
| 51 | #define DEMO_TEXTURE_COUNT 1 |
Ian Elliott | 44e33f7 | 2015-04-28 10:52:52 -0600 | [diff] [blame] | 52 | #define APP_SHORT_NAME "cube" |
| 53 | #define APP_LONG_NAME "The Vulkan Cube Demo Program" |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 54 | |
Courtney Goeltzenleuchter | 886f76c | 2015-07-06 17:46:11 -0600 | [diff] [blame] | 55 | #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) |
| 56 | |
Tony Barbour | fdc2d35 | 2015-04-22 09:02:32 -0600 | [diff] [blame] | 57 | #if defined(NDEBUG) && defined(__GNUC__) |
| 58 | #define U_ASSERT_ONLY __attribute__((unused)) |
| 59 | #else |
| 60 | #define U_ASSERT_ONLY |
| 61 | #endif |
| 62 | |
Ian Elliott | 0726413 | 2015-04-28 11:35:02 -0600 | [diff] [blame] | 63 | #ifdef _WIN32 |
| 64 | #define ERR_EXIT(err_msg, err_class) \ |
| 65 | do { \ |
| 66 | MessageBox(NULL, err_msg, err_class, MB_OK); \ |
| 67 | exit(1); \ |
| 68 | } while (0) |
| 69 | |
Ian Elliott | 0726413 | 2015-04-28 11:35:02 -0600 | [diff] [blame] | 70 | #else // _WIN32 |
| 71 | |
| 72 | #define ERR_EXIT(err_msg, err_class) \ |
| 73 | do { \ |
| 74 | printf(err_msg); \ |
| 75 | fflush(stdout); \ |
| 76 | exit(1); \ |
| 77 | } while (0) |
| 78 | #endif // _WIN32 |
| 79 | |
Ian Elliott | aaae535 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 80 | #define GET_INSTANCE_PROC_ADDR(inst, entrypoint) \ |
| 81 | { \ |
| 82 | demo->fp##entrypoint = (PFN_vk##entrypoint) vkGetInstanceProcAddr(inst, "vk"#entrypoint); \ |
| 83 | if (demo->fp##entrypoint == NULL) { \ |
| 84 | ERR_EXIT("vkGetInstanceProcAddr failed to find vk"#entrypoint, \ |
| 85 | "vkGetInstanceProcAddr Failure"); \ |
| 86 | } \ |
| 87 | } |
| 88 | |
Ian Elliott | 673898b | 2015-06-22 15:07:49 -0600 | [diff] [blame] | 89 | #define GET_DEVICE_PROC_ADDR(dev, entrypoint) \ |
| 90 | { \ |
Courtney Goeltzenleuchter | 4761063 | 2015-07-12 14:35:22 -0600 | [diff] [blame] | 91 | demo->fp##entrypoint = (PFN_vk##entrypoint) vkGetDeviceProcAddr(dev, "vk"#entrypoint); \ |
Ian Elliott | 673898b | 2015-06-22 15:07:49 -0600 | [diff] [blame] | 92 | if (demo->fp##entrypoint == NULL) { \ |
| 93 | ERR_EXIT("vkGetDeviceProcAddr failed to find vk"#entrypoint, \ |
| 94 | "vkGetDeviceProcAddr Failure"); \ |
| 95 | } \ |
| 96 | } |
| 97 | |
Courtney Goeltzenleuchter | 97db1c1 | 2014-10-30 15:14:16 -0600 | [diff] [blame] | 98 | /* |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 99 | * structure to track all objects related to a texture. |
| 100 | */ |
Courtney Goeltzenleuchter | 752eb8e | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 101 | struct texture_object { |
Courtney Goeltzenleuchter | 78351a6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 102 | VkSampler sampler; |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 103 | |
Courtney Goeltzenleuchter | 78351a6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 104 | VkImage image; |
| 105 | VkImageLayout imageLayout; |
Courtney Goeltzenleuchter | 752eb8e | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 106 | |
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[] = { |
Courtney Goeltzenleuchter | 97db1c1 | 2014-10-30 15:14:16 -0600 | [diff] [blame] | 113 | "lunarg-logo-256x256-solid.png" |
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; |
Mark Lobodzinski | 2dcdfd7 | 2015-05-29 09:32:35 -0500 | [diff] [blame] | 357 | VkDeviceMemory mem; |
Courtney Goeltzenleuchter | 41866db | 2015-09-01 17:30:39 -0600 | [diff] [blame] | 358 | VkImageView view; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 359 | } depth; |
| 360 | |
Courtney Goeltzenleuchter | 752eb8e | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 361 | struct texture_object textures[DEMO_TEXTURE_COUNT]; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 362 | |
| 363 | struct { |
Courtney Goeltzenleuchter | 78351a6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 364 | VkBuffer buf; |
Mark Lobodzinski | 2dcdfd7 | 2015-05-29 09:32:35 -0500 | [diff] [blame] | 365 | VkDeviceMemory mem; |
Courtney Goeltzenleuchter | 78351a6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 366 | VkBufferView view; |
Chia-I Wu | ae721ba | 2015-05-25 16:27:55 +0800 | [diff] [blame] | 367 | VkDescriptorInfo desc; |
Courtney Goeltzenleuchter | fc3b953 | 2014-10-28 10:32:57 -0600 | [diff] [blame] | 368 | } uniform_data; |
| 369 | |
Courtney Goeltzenleuchter | 78351a6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 370 | VkCmdBuffer cmd; // Buffer for initialization commands |
Mark Lobodzinski | 1cfc772 | 2015-04-17 14:11:39 -0500 | [diff] [blame] | 371 | VkPipelineLayout pipeline_layout; |
Courtney Goeltzenleuchter | 78351a6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 372 | VkDescriptorSetLayout desc_layout; |
Jon Ashburn | c4ab7af | 2015-07-09 15:02:25 -0600 | [diff] [blame] | 373 | VkPipelineCache pipelineCache; |
Chia-I Wu | f973e32 | 2015-07-08 13:34:24 +0800 | [diff] [blame] | 374 | VkRenderPass render_pass; |
Courtney Goeltzenleuchter | 78351a6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 375 | VkPipeline pipeline; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 376 | |
Cody Northrop | 5e4125d | 2015-08-26 10:01:32 -0600 | [diff] [blame] | 377 | VkDynamicViewportState dynamic_viewport; |
| 378 | VkDynamicLineWidthState dynamic_line_width; |
| 379 | VkDynamicDepthBiasState dynamic_depth_bias; |
| 380 | VkDynamicBlendState dynamic_blend; |
| 381 | VkDynamicDepthBoundsState dynamic_depth_bounds; |
Cody Northrop | 36a642f | 2015-08-18 15:21:16 -0600 | [diff] [blame] | 382 | VkDynamicStencilState dynamic_stencil; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 383 | |
Courtney Goeltzenleuchter | fc3b953 | 2014-10-28 10:32:57 -0600 | [diff] [blame] | 384 | mat4x4 projection_matrix; |
| 385 | mat4x4 view_matrix; |
| 386 | mat4x4 model_matrix; |
| 387 | |
Mark Lobodzinski | c4fffc7 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 388 | float spin_angle; |
| 389 | float spin_increment; |
Courtney Goeltzenleuchter | 8879d3a | 2014-10-29 08:29:35 -0600 | [diff] [blame] | 390 | bool pause; |
| 391 | |
Tony Barbour | b6dba5c | 2015-07-21 09:04:41 -0600 | [diff] [blame] | 392 | VkShaderModule vert_shader_module; |
| 393 | VkShaderModule frag_shader_module; |
| 394 | |
Courtney Goeltzenleuchter | 78351a6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 395 | VkDescriptorPool desc_pool; |
| 396 | VkDescriptorSet desc_set; |
Chia-I Wu | 6a3c897 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 397 | |
Chia-I Wu | f973e32 | 2015-07-08 13:34:24 +0800 | [diff] [blame] | 398 | VkFramebuffer framebuffers[DEMO_BUFFER_COUNT]; |
| 399 | |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 400 | bool quit; |
David Pinedo | 2bb7c93 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 401 | int32_t curFrame; |
| 402 | int32_t frameCount; |
Tony Barbour | 3dddd5d | 2015-04-29 16:19:20 -0600 | [diff] [blame] | 403 | bool validate; |
Courtney Goeltzenleuchter | 03f4b77 | 2015-07-22 11:03:51 -0600 | [diff] [blame] | 404 | bool use_break; |
Courtney Goeltzenleuchter | 922a0fa | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 405 | PFN_vkDbgCreateMsgCallback dbgCreateMsgCallback; |
Tony Barbour | d70b42c | 2015-06-30 14:14:19 -0600 | [diff] [blame] | 406 | PFN_vkDbgDestroyMsgCallback dbgDestroyMsgCallback; |
Courtney Goeltzenleuchter | 03f4b77 | 2015-07-22 11:03:51 -0600 | [diff] [blame] | 407 | PFN_vkDbgMsgCallback dbgBreakCallback; |
Courtney Goeltzenleuchter | 922a0fa | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 408 | VkDbgMsgCallback msg_callback; |
| 409 | |
Mark Lobodzinski | c4fffc7 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 410 | uint32_t current_buffer; |
Courtney Goeltzenleuchter | fe95fca | 2015-07-15 17:40:20 -0600 | [diff] [blame] | 411 | uint32_t queue_count; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 412 | }; |
| 413 | |
Mark Lobodzinski | eadf998 | 2015-07-02 16:49:40 -0600 | [diff] [blame] | 414 | static VkResult memory_type_from_properties(struct demo *demo, uint32_t typeBits, VkFlags properties, uint32_t *typeIndex) |
| 415 | { |
| 416 | // Search memtypes to find first index with those properties |
| 417 | for (uint32_t i = 0; i < 32; i++) { |
| 418 | if ((typeBits & 1) == 1) { |
| 419 | // Type is available, does it match user properties? |
| 420 | if ((demo->memory_properties.memoryTypes[i].propertyFlags & properties) == properties) { |
| 421 | *typeIndex = i; |
| 422 | return VK_SUCCESS; |
| 423 | } |
| 424 | } |
| 425 | typeBits >>= 1; |
| 426 | } |
| 427 | // No memory types matched, return failure |
| 428 | return VK_UNSUPPORTED; |
| 429 | } |
| 430 | |
Courtney Goeltzenleuchter | 752eb8e | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 431 | static void demo_flush_init_cmd(struct demo *demo) |
| 432 | { |
Tony Barbour | fdc2d35 | 2015-04-22 09:02:32 -0600 | [diff] [blame] | 433 | VkResult U_ASSERT_ONLY err; |
Courtney Goeltzenleuchter | 752eb8e | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 434 | |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 435 | if (demo->cmd == VK_NULL_HANDLE) |
Courtney Goeltzenleuchter | 752eb8e | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 436 | return; |
| 437 | |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 438 | err = vkEndCommandBuffer(demo->cmd); |
Courtney Goeltzenleuchter | 752eb8e | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 439 | assert(!err); |
| 440 | |
Courtney Goeltzenleuchter | 78351a6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 441 | const VkCmdBuffer cmd_bufs[] = { demo->cmd }; |
Tony Barbour | 155cce8 | 2015-07-03 10:33:54 -0600 | [diff] [blame] | 442 | VkFence nullFence = { VK_NULL_HANDLE }; |
Courtney Goeltzenleuchter | 752eb8e | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 443 | |
Tony Barbour | 155cce8 | 2015-07-03 10:33:54 -0600 | [diff] [blame] | 444 | err = vkQueueSubmit(demo->queue, 1, cmd_bufs, nullFence); |
Courtney Goeltzenleuchter | 752eb8e | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 445 | assert(!err); |
| 446 | |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 447 | err = vkQueueWaitIdle(demo->queue); |
Courtney Goeltzenleuchter | 752eb8e | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 448 | assert(!err); |
| 449 | |
Tony Barbour | 155cce8 | 2015-07-03 10:33:54 -0600 | [diff] [blame] | 450 | vkDestroyCommandBuffer(demo->device, demo->cmd); |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 451 | demo->cmd = VK_NULL_HANDLE; |
Courtney Goeltzenleuchter | 752eb8e | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 452 | } |
| 453 | |
Courtney Goeltzenleuchter | 752eb8e | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 454 | static void demo_set_image_layout( |
| 455 | struct demo *demo, |
Courtney Goeltzenleuchter | 78351a6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 456 | VkImage image, |
malnasse | eb25d3f | 2015-06-03 17:28:38 -0400 | [diff] [blame] | 457 | VkImageAspect aspect, |
Courtney Goeltzenleuchter | 78351a6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 458 | VkImageLayout old_image_layout, |
| 459 | VkImageLayout new_image_layout) |
Courtney Goeltzenleuchter | 752eb8e | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 460 | { |
Tony Barbour | fdc2d35 | 2015-04-22 09:02:32 -0600 | [diff] [blame] | 461 | VkResult U_ASSERT_ONLY err; |
Courtney Goeltzenleuchter | 752eb8e | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 462 | |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 463 | if (demo->cmd == VK_NULL_HANDLE) { |
Courtney Goeltzenleuchter | 78351a6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 464 | const VkCmdBufferCreateInfo cmd = { |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 465 | .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO, |
Courtney Goeltzenleuchter | 752eb8e | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 466 | .pNext = NULL, |
Cody Northrop | 91e221b | 2015-07-09 18:08:32 -0600 | [diff] [blame] | 467 | .cmdPool = demo->cmd_pool, |
Chia-I Wu | 57b23b4 | 2015-06-26 15:34:39 +0800 | [diff] [blame] | 468 | .level = VK_CMD_BUFFER_LEVEL_PRIMARY, |
Courtney Goeltzenleuchter | 752eb8e | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 469 | .flags = 0, |
| 470 | }; |
| 471 | |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 472 | err = vkCreateCommandBuffer(demo->device, &cmd, &demo->cmd); |
Courtney Goeltzenleuchter | 752eb8e | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 473 | assert(!err); |
| 474 | |
Courtney Goeltzenleuchter | 78351a6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 475 | VkCmdBufferBeginInfo cmd_buf_info = { |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 476 | .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO, |
Courtney Goeltzenleuchter | 752eb8e | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 477 | .pNext = NULL, |
Tony Barbour | 72304ef | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 478 | .flags = VK_CMD_BUFFER_OPTIMIZE_SMALL_BATCH_BIT | |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 479 | VK_CMD_BUFFER_OPTIMIZE_ONE_TIME_SUBMIT_BIT, |
Courtney Goeltzenleuchter | dc093ab | 2015-08-26 14:48:00 -0600 | [diff] [blame] | 480 | .renderPass = { VK_NULL_HANDLE }, |
Cody Northrop | a0a178c | 2015-08-11 11:35:58 -0600 | [diff] [blame] | 481 | .subpass = 0, |
Courtney Goeltzenleuchter | dc093ab | 2015-08-26 14:48:00 -0600 | [diff] [blame] | 482 | .framebuffer = { VK_NULL_HANDLE }, |
Courtney Goeltzenleuchter | 752eb8e | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 483 | }; |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 484 | err = vkBeginCommandBuffer(demo->cmd, &cmd_buf_info); |
Courtney Goeltzenleuchter | 752eb8e | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 485 | } |
| 486 | |
Courtney Goeltzenleuchter | 78351a6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 487 | VkImageMemoryBarrier image_memory_barrier = { |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 488 | .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER, |
Courtney Goeltzenleuchter | 752eb8e | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 489 | .pNext = NULL, |
| 490 | .outputMask = 0, |
| 491 | .inputMask = 0, |
| 492 | .oldLayout = old_image_layout, |
| 493 | .newLayout = new_image_layout, |
| 494 | .image = image, |
malnasse | eb25d3f | 2015-06-03 17:28:38 -0400 | [diff] [blame] | 495 | .subresourceRange = { aspect, 0, 1, 0, 0 } |
Courtney Goeltzenleuchter | 752eb8e | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 496 | }; |
| 497 | |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 498 | if (new_image_layout == VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL) { |
Courtney Goeltzenleuchter | 752eb8e | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 499 | /* Make sure anything that was copying from this image has completed */ |
Courtney Goeltzenleuchter | 42a078d | 2015-04-15 15:29:59 -0600 | [diff] [blame] | 500 | image_memory_barrier.inputMask = VK_MEMORY_INPUT_TRANSFER_BIT; |
Courtney Goeltzenleuchter | 752eb8e | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 501 | } |
| 502 | |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 503 | if (new_image_layout == VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL) { |
Courtney Goeltzenleuchter | 752eb8e | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 504 | /* Make sure any Copy or CPU writes to image are flushed */ |
Courtney Goeltzenleuchter | 2bda833 | 2015-04-29 17:16:21 -0600 | [diff] [blame] | 505 | 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] | 506 | } |
| 507 | |
Courtney Goeltzenleuchter | 78351a6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 508 | VkImageMemoryBarrier *pmemory_barrier = &image_memory_barrier; |
Courtney Goeltzenleuchter | 752eb8e | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 509 | |
Tony Barbour | 50bbca4 | 2015-06-29 16:20:35 -0600 | [diff] [blame] | 510 | VkPipelineStageFlags src_stages = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT; |
| 511 | VkPipelineStageFlags dest_stages = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT; |
Courtney Goeltzenleuchter | 752eb8e | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 512 | |
Courtney Goeltzenleuchter | 0cab2fb | 2015-07-12 13:07:46 -0600 | [diff] [blame] | 513 | 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] | 514 | } |
| 515 | |
Courtney Goeltzenleuchter | 78351a6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 516 | static void demo_draw_build_cmd(struct demo *demo, VkCmdBuffer cmd_buf) |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 517 | { |
Chia-I Wu | f973e32 | 2015-07-08 13:34:24 +0800 | [diff] [blame] | 518 | const VkCmdBufferBeginInfo cmd_buf_info = { |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 519 | .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO, |
Courtney Goeltzenleuchter | 53968d8 | 2015-04-03 15:25:24 -0600 | [diff] [blame] | 520 | .pNext = NULL, |
Courtney Goeltzenleuchter | a8df1c0 | 2015-07-28 08:59:17 -0600 | [diff] [blame] | 521 | .flags = VK_CMD_BUFFER_OPTIMIZE_SMALL_BATCH_BIT, |
Courtney Goeltzenleuchter | dc093ab | 2015-08-26 14:48:00 -0600 | [diff] [blame] | 522 | .renderPass = { VK_NULL_HANDLE }, |
Cody Northrop | a0a178c | 2015-08-11 11:35:58 -0600 | [diff] [blame] | 523 | .subpass = 0, |
Courtney Goeltzenleuchter | dc093ab | 2015-08-26 14:48:00 -0600 | [diff] [blame] | 524 | .framebuffer = { VK_NULL_HANDLE }, |
Jon Ashburn | 60ccfbe | 2014-12-31 17:08:35 -0700 | [diff] [blame] | 525 | }; |
Chia-I Wu | a74c5b2 | 2015-07-07 11:50:03 +0800 | [diff] [blame] | 526 | const VkClearValue clear_values[2] = { |
Cody Northrop | 5574592 | 2015-08-25 15:26:38 -0600 | [diff] [blame] | 527 | [0] = { .color.float32 = { 0.2f, 0.2f, 0.2f, 0.2f } }, |
| 528 | [1] = { .depthStencil = { 1.0f, 0 } }, |
Chia-I Wu | a74c5b2 | 2015-07-07 11:50:03 +0800 | [diff] [blame] | 529 | }; |
| 530 | const VkRenderPassBeginInfo rp_begin = { |
| 531 | .sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO, |
| 532 | .pNext = NULL, |
Chia-I Wu | f973e32 | 2015-07-08 13:34:24 +0800 | [diff] [blame] | 533 | .renderPass = demo->render_pass, |
| 534 | .framebuffer = demo->framebuffers[demo->current_buffer], |
Chia-I Wu | a74c5b2 | 2015-07-07 11:50:03 +0800 | [diff] [blame] | 535 | .renderArea.offset.x = 0, |
| 536 | .renderArea.offset.y = 0, |
| 537 | .renderArea.extent.width = demo->width, |
| 538 | .renderArea.extent.height = demo->height, |
Cody Northrop | 372bbae | 2015-08-04 11:51:03 -0600 | [diff] [blame] | 539 | .clearValueCount = 2, |
| 540 | .pClearValues = clear_values, |
Jon Ashburn | 08f6eb9 | 2015-01-02 18:24:05 -0700 | [diff] [blame] | 541 | }; |
Chia-I Wu | f973e32 | 2015-07-08 13:34:24 +0800 | [diff] [blame] | 542 | VkResult U_ASSERT_ONLY err; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 543 | |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 544 | err = vkBeginCommandBuffer(cmd_buf, &cmd_buf_info); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 545 | assert(!err); |
| 546 | |
Chia-I Wu | a74c5b2 | 2015-07-07 11:50:03 +0800 | [diff] [blame] | 547 | vkCmdBeginRenderPass(cmd_buf, &rp_begin, VK_RENDER_PASS_CONTENTS_INLINE); |
| 548 | |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 549 | vkCmdBindPipeline(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS, |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 550 | demo->pipeline); |
Mark Lobodzinski | c6e8b3d | 2015-06-15 13:21:21 -0600 | [diff] [blame] | 551 | vkCmdBindDescriptorSets(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS, demo->pipeline_layout, |
Cody Northrop | fb5185a | 2015-04-16 13:41:56 -0600 | [diff] [blame] | 552 | 0, 1, &demo->desc_set, 0, NULL); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 553 | |
Cody Northrop | 5e4125d | 2015-08-26 10:01:32 -0600 | [diff] [blame] | 554 | vkCmdBindDynamicViewportState(cmd_buf, demo->dynamic_viewport); |
| 555 | vkCmdBindDynamicLineWidthState(cmd_buf, demo->dynamic_line_width); |
| 556 | vkCmdBindDynamicDepthBiasState(cmd_buf, demo->dynamic_depth_bias); |
| 557 | vkCmdBindDynamicBlendState(cmd_buf, demo->dynamic_blend); |
| 558 | vkCmdBindDynamicDepthBoundsState(cmd_buf, demo->dynamic_depth_bounds); |
Cody Northrop | 36a642f | 2015-08-18 15:21:16 -0600 | [diff] [blame] | 559 | vkCmdBindDynamicStencilState(cmd_buf, demo->dynamic_stencil); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 560 | |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 561 | vkCmdDraw(cmd_buf, 0, 12 * 3, 0, 1); |
Chia-I Wu | 57b23b4 | 2015-06-26 15:34:39 +0800 | [diff] [blame] | 562 | vkCmdEndRenderPass(cmd_buf); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 563 | |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 564 | err = vkEndCommandBuffer(cmd_buf); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 565 | assert(!err); |
| 566 | } |
| 567 | |
Courtney Goeltzenleuchter | fc3b953 | 2014-10-28 10:32:57 -0600 | [diff] [blame] | 568 | |
Courtney Goeltzenleuchter | 8879d3a | 2014-10-29 08:29:35 -0600 | [diff] [blame] | 569 | void demo_update_data_buffer(struct demo *demo) |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 570 | { |
Courtney Goeltzenleuchter | fc3b953 | 2014-10-28 10:32:57 -0600 | [diff] [blame] | 571 | mat4x4 MVP, Model, VP; |
| 572 | int matrixSize = sizeof(MVP); |
Mark Lobodzinski | c4fffc7 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 573 | uint8_t *pData; |
Tony Barbour | fdc2d35 | 2015-04-22 09:02:32 -0600 | [diff] [blame] | 574 | VkResult U_ASSERT_ONLY err; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 575 | |
Courtney Goeltzenleuchter | fc3b953 | 2014-10-28 10:32:57 -0600 | [diff] [blame] | 576 | mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 577 | |
Courtney Goeltzenleuchter | fc3b953 | 2014-10-28 10:32:57 -0600 | [diff] [blame] | 578 | // Rotate 22.5 degrees around the Y axis |
Courtney Goeltzenleuchter | 4984d15 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 579 | mat4x4_dup(Model, demo->model_matrix); |
Piers Daniell | 735ee53 | 2015-02-23 16:23:13 -0700 | [diff] [blame] | 580 | 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] | 581 | mat4x4_mul(MVP, VP, demo->model_matrix); |
Courtney Goeltzenleuchter | fc3b953 | 2014-10-28 10:32:57 -0600 | [diff] [blame] | 582 | |
Mark Lobodzinski | 2dcdfd7 | 2015-05-29 09:32:35 -0500 | [diff] [blame] | 583 | err = vkMapMemory(demo->device, demo->uniform_data.mem, 0, 0, 0, (void **) &pData); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 584 | assert(!err); |
| 585 | |
Courtney Goeltzenleuchter | fc3b953 | 2014-10-28 10:32:57 -0600 | [diff] [blame] | 586 | memcpy(pData, (const void*) &MVP[0][0], matrixSize); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 587 | |
Mark Lobodzinski | e4c92a6 | 2015-09-07 13:59:43 -0600 | [diff] [blame] | 588 | vkUnmapMemory(demo->device, demo->uniform_data.mem); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 589 | } |
| 590 | |
| 591 | static void demo_draw(struct demo *demo) |
| 592 | { |
Tony Barbour | fdc2d35 | 2015-04-22 09:02:32 -0600 | [diff] [blame] | 593 | VkResult U_ASSERT_ONLY err; |
Ian Elliott | aaae535 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 594 | VkSemaphore presentCompleteSemaphore; |
| 595 | VkSemaphoreCreateInfo presentCompleteSemaphoreCreateInfo = { |
| 596 | .sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO, |
| 597 | .pNext = NULL, |
| 598 | .flags = VK_FENCE_CREATE_SIGNALED_BIT, |
| 599 | }; |
Tony Barbour | 155cce8 | 2015-07-03 10:33:54 -0600 | [diff] [blame] | 600 | VkFence nullFence = { VK_NULL_HANDLE }; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 601 | |
Ian Elliott | aaae535 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 602 | err = vkCreateSemaphore(demo->device, |
| 603 | &presentCompleteSemaphoreCreateInfo, |
| 604 | &presentCompleteSemaphore); |
| 605 | assert(!err); |
| 606 | |
| 607 | // Get the index of the next available swapchain image: |
Ian Elliott | a078197 | 2015-08-21 15:09:33 -0600 | [diff] [blame] | 608 | err = demo->fpAcquireNextImageKHR(demo->device, demo->swap_chain, |
Ian Elliott | aaae535 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 609 | UINT64_MAX, |
| 610 | presentCompleteSemaphore, |
| 611 | &demo->current_buffer); |
Ian Elliott | a078197 | 2015-08-21 15:09:33 -0600 | [diff] [blame] | 612 | // 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] | 613 | // return codes |
| 614 | assert(!err); |
| 615 | |
| 616 | // Wait for the present complete semaphore to be signaled to ensure |
| 617 | // that the image won't be rendered to until the presentation |
| 618 | // engine has fully released ownership to the application, and it is |
| 619 | // okay to render to the image. |
| 620 | vkQueueWaitSemaphore(demo->queue, presentCompleteSemaphore); |
| 621 | |
Ian Elliott | a078197 | 2015-08-21 15:09:33 -0600 | [diff] [blame] | 622 | // FIXME/TODO: DEAL WITH VK_IMAGE_LAYOUT_PRESENT_SOURCE_KHR |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 623 | err = vkQueueSubmit(demo->queue, 1, &demo->buffers[demo->current_buffer].cmd, |
Tony Barbour | 155cce8 | 2015-07-03 10:33:54 -0600 | [diff] [blame] | 624 | nullFence); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 625 | assert(!err); |
| 626 | |
Ian Elliott | a078197 | 2015-08-21 15:09:33 -0600 | [diff] [blame] | 627 | VkPresentInfoKHR present = { |
| 628 | .sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR, |
Ian Elliott | aaae535 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 629 | .pNext = NULL, |
Ian Elliott | a078197 | 2015-08-21 15:09:33 -0600 | [diff] [blame] | 630 | .swapchainCount = 1, |
| 631 | .swapchains = &demo->swap_chain, |
Ian Elliott | aaae535 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 632 | .imageIndices = &demo->current_buffer, |
| 633 | }; |
| 634 | |
| 635 | // TBD/TODO: SHOULD THE "present" PARAMETER BE "const" IN THE HEADER? |
Ian Elliott | a078197 | 2015-08-21 15:09:33 -0600 | [diff] [blame] | 636 | err = demo->fpQueuePresentKHR(demo->queue, &present); |
| 637 | // 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] | 638 | // return codes |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 639 | assert(!err); |
| 640 | |
Chia-I Wu | cbb564e | 2015-04-16 22:02:10 +0800 | [diff] [blame] | 641 | err = vkQueueWaitIdle(demo->queue); |
| 642 | assert(err == VK_SUCCESS); |
Mike Stroyan | 15fed6b | 2015-08-28 10:58:06 -0600 | [diff] [blame] | 643 | |
Mark Lobodzinski | e4c92a6 | 2015-09-07 13:59:43 -0600 | [diff] [blame] | 644 | vkDestroySemaphore(demo->device, presentCompleteSemaphore); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 645 | } |
| 646 | |
| 647 | static void demo_prepare_buffers(struct demo *demo) |
| 648 | { |
Ian Elliott | aaae535 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 649 | VkResult U_ASSERT_ONLY err; |
| 650 | |
| 651 | // Check the surface properties and formats |
Ian Elliott | a078197 | 2015-08-21 15:09:33 -0600 | [diff] [blame] | 652 | VkSurfacePropertiesKHR surfProperties; |
| 653 | err = demo->fpGetSurfacePropertiesKHR(demo->device, |
| 654 | (const VkSurfaceDescriptionKHR *)&demo->surface_description, |
Ian Elliott | 8528eff | 2015-08-07 15:56:59 -0600 | [diff] [blame] | 655 | &surfProperties); |
Ian Elliott | aaae535 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 656 | assert(!err); |
| 657 | |
Ian Elliott | 8528eff | 2015-08-07 15:56:59 -0600 | [diff] [blame] | 658 | uint32_t presentModeCount; |
Ian Elliott | a078197 | 2015-08-21 15:09:33 -0600 | [diff] [blame] | 659 | err = demo->fpGetSurfacePresentModesKHR(demo->device, |
| 660 | (const VkSurfaceDescriptionKHR *)&demo->surface_description, |
Ian Elliott | 8528eff | 2015-08-07 15:56:59 -0600 | [diff] [blame] | 661 | &presentModeCount, NULL); |
Ian Elliott | aaae535 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 662 | assert(!err); |
Ian Elliott | a078197 | 2015-08-21 15:09:33 -0600 | [diff] [blame] | 663 | VkPresentModeKHR *presentModes = |
| 664 | (VkPresentModeKHR *)malloc(presentModeCount * sizeof(VkPresentModeKHR)); |
Ian Elliott | 8528eff | 2015-08-07 15:56:59 -0600 | [diff] [blame] | 665 | assert(presentModes); |
Ian Elliott | a078197 | 2015-08-21 15:09:33 -0600 | [diff] [blame] | 666 | err = demo->fpGetSurfacePresentModesKHR(demo->device, |
| 667 | (const VkSurfaceDescriptionKHR *)&demo->surface_description, |
Ian Elliott | 8528eff | 2015-08-07 15:56:59 -0600 | [diff] [blame] | 668 | &presentModeCount, presentModes); |
Ian Elliott | aaae535 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 669 | assert(!err); |
| 670 | |
Ian Elliott | a078197 | 2015-08-21 15:09:33 -0600 | [diff] [blame] | 671 | VkExtent2D swapchainExtent; |
Ian Elliott | aaae535 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 672 | // width and height are either both -1, or both not -1. |
Ian Elliott | 8528eff | 2015-08-07 15:56:59 -0600 | [diff] [blame] | 673 | if (surfProperties.currentExtent.width == -1) |
Ian Elliott | aaae535 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 674 | { |
| 675 | // If the surface size is undefined, the size is set to |
| 676 | // the size of the images requested. |
Ian Elliott | a078197 | 2015-08-21 15:09:33 -0600 | [diff] [blame] | 677 | swapchainExtent.width = demo->width; |
| 678 | swapchainExtent.height = demo->height; |
Ian Elliott | aaae535 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 679 | } |
| 680 | else |
| 681 | { |
| 682 | // If the surface size is defined, the swap chain size must match |
Ian Elliott | a078197 | 2015-08-21 15:09:33 -0600 | [diff] [blame] | 683 | swapchainExtent = surfProperties.currentExtent; |
Ian Elliott | aaae535 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 684 | } |
| 685 | |
| 686 | // 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] | 687 | // tearing mode. If not, try IMMEDIATE which will usually be available, |
| 688 | // and is fastest (though it tears). If not, fall back to FIFO which is |
| 689 | // always available. |
Ian Elliott | a078197 | 2015-08-21 15:09:33 -0600 | [diff] [blame] | 690 | VkPresentModeKHR swapchainPresentMode = VK_PRESENT_MODE_FIFO_KHR; |
Ian Elliott | aaae535 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 691 | for (size_t i = 0; i < presentModeCount; i++) { |
Ian Elliott | a078197 | 2015-08-21 15:09:33 -0600 | [diff] [blame] | 692 | if (presentModes[i] == VK_PRESENT_MODE_MAILBOX_KHR) { |
| 693 | swapchainPresentMode = VK_PRESENT_MODE_MAILBOX_KHR; |
Ian Elliott | aaae535 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 694 | break; |
| 695 | } |
Ian Elliott | a078197 | 2015-08-21 15:09:33 -0600 | [diff] [blame] | 696 | if ((swapchainPresentMode != VK_PRESENT_MODE_MAILBOX_KHR) && |
| 697 | (presentModes[i] == VK_PRESENT_MODE_IMMEDIATE_KHR)) { |
| 698 | swapchainPresentMode = VK_PRESENT_MODE_IMMEDIATE_KHR; |
Ian Elliott | 3ebce34 | 2015-07-27 13:53:11 -0600 | [diff] [blame] | 699 | } |
Ian Elliott | aaae535 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 700 | } |
| 701 | |
Ian Elliott | 2d47da9 | 2015-07-13 12:20:56 -0600 | [diff] [blame] | 702 | #define WORK_AROUND_CODE |
| 703 | #ifdef WORK_AROUND_CODE |
Ian Elliott | 8528eff | 2015-08-07 15:56:59 -0600 | [diff] [blame] | 704 | // After the proper code was created, other parts of this demo were |
| 705 | // modified to only support DEMO_BUFFER_COUNT number of command buffers, |
| 706 | // images, etc. Live with that for now. |
| 707 | // 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] | 708 | // by vkCreateSwapchainKHR(). |
| 709 | uint32_t desiredNumberOfSwapchainImages = DEMO_BUFFER_COUNT; |
Ian Elliott | 2d47da9 | 2015-07-13 12:20:56 -0600 | [diff] [blame] | 710 | #else // WORK_AROUND_CODE |
Ian Elliott | aaae535 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 711 | // Determine the number of VkImage's to use in the swap chain (we desire to |
| 712 | // own only 1 image at a time, besides the images being displayed and |
| 713 | // queued for display): |
Ian Elliott | a078197 | 2015-08-21 15:09:33 -0600 | [diff] [blame] | 714 | uint32_t desiredNumberOfSwapchainImages = surfProperties.minImageCount + 1; |
Ian Elliott | 8528eff | 2015-08-07 15:56:59 -0600 | [diff] [blame] | 715 | if ((surfProperties.maxImageCount > 0) && |
Ian Elliott | a078197 | 2015-08-21 15:09:33 -0600 | [diff] [blame] | 716 | (desiredNumberOfSwapchainImages > surfProperties.maxImageCount)) |
Ian Elliott | aaae535 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 717 | { |
| 718 | // Application must settle for fewer images than desired: |
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 | } |
Ian Elliott | 2d47da9 | 2015-07-13 12:20:56 -0600 | [diff] [blame] | 721 | #endif // WORK_AROUND_CODE |
Ian Elliott | aaae535 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 722 | |
Ian Elliott | a078197 | 2015-08-21 15:09:33 -0600 | [diff] [blame] | 723 | VkSurfaceTransformFlagBitsKHR preTransform; |
| 724 | if (surfProperties.supportedTransforms & VK_SURFACE_TRANSFORM_NONE_BIT_KHR) { |
| 725 | preTransform = VK_SURFACE_TRANSFORM_NONE_KHR; |
Ian Elliott | aaae535 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 726 | } else { |
Ian Elliott | 8528eff | 2015-08-07 15:56:59 -0600 | [diff] [blame] | 727 | preTransform = surfProperties.currentTransform; |
Ian Elliott | aaae535 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 728 | } |
| 729 | |
Ian Elliott | a078197 | 2015-08-21 15:09:33 -0600 | [diff] [blame] | 730 | const VkSwapchainCreateInfoKHR swap_chain = { |
| 731 | .sType = VK_STRUCTURE_TYPE_SWAP_CHAIN_CREATE_INFO_KHR, |
Chia-I Wu | cbb564e | 2015-04-16 22:02:10 +0800 | [diff] [blame] | 732 | .pNext = NULL, |
Ian Elliott | a078197 | 2015-08-21 15:09:33 -0600 | [diff] [blame] | 733 | .pSurfaceDescription = (const VkSurfaceDescriptionKHR *)&demo->surface_description, |
| 734 | .minImageCount = desiredNumberOfSwapchainImages, |
Chia-I Wu | cbb564e | 2015-04-16 22:02:10 +0800 | [diff] [blame] | 735 | .imageFormat = demo->format, |
Ian Elliott | 8528eff | 2015-08-07 15:56:59 -0600 | [diff] [blame] | 736 | .imageColorSpace = demo->color_space, |
Chia-I Wu | cbb564e | 2015-04-16 22:02:10 +0800 | [diff] [blame] | 737 | .imageExtent = { |
Ian Elliott | a078197 | 2015-08-21 15:09:33 -0600 | [diff] [blame] | 738 | .width = swapchainExtent.width, |
| 739 | .height = swapchainExtent.height, |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 740 | }, |
Cody Northrop | 11b48d0 | 2015-08-28 16:22:48 -0600 | [diff] [blame] | 741 | .imageUsageFlags = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT, |
Ian Elliott | aaae535 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 742 | .preTransform = preTransform, |
Chia-I Wu | cbb564e | 2015-04-16 22:02:10 +0800 | [diff] [blame] | 743 | .imageArraySize = 1, |
Ian Elliott | 8528eff | 2015-08-07 15:56:59 -0600 | [diff] [blame] | 744 | .sharingMode = VK_SHARING_MODE_EXCLUSIVE, |
| 745 | .queueFamilyCount = 0, |
| 746 | .pQueueFamilyIndices = NULL, |
Ian Elliott | a078197 | 2015-08-21 15:09:33 -0600 | [diff] [blame] | 747 | .presentMode = swapchainPresentMode, |
| 748 | .oldSwapchain.handle = 0, |
Ian Elliott | aaae535 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 749 | .clipped = true, |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 750 | }; |
Mark Lobodzinski | c4fffc7 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 751 | uint32_t i; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 752 | |
Ian Elliott | a078197 | 2015-08-21 15:09:33 -0600 | [diff] [blame] | 753 | err = demo->fpCreateSwapchainKHR(demo->device, &swap_chain, &demo->swap_chain); |
Chia-I Wu | cbb564e | 2015-04-16 22:02:10 +0800 | [diff] [blame] | 754 | assert(!err); |
| 755 | |
Ian Elliott | a078197 | 2015-08-21 15:09:33 -0600 | [diff] [blame] | 756 | err = demo->fpGetSwapchainImagesKHR(demo->device, demo->swap_chain, |
| 757 | &demo->swapchainImageCount, NULL); |
Ian Elliott | aaae535 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 758 | assert(!err); |
Chia-I Wu | cbb564e | 2015-04-16 22:02:10 +0800 | [diff] [blame] | 759 | |
Ian Elliott | a078197 | 2015-08-21 15:09:33 -0600 | [diff] [blame] | 760 | VkImage* swapchainImages = |
| 761 | (VkImage*)malloc(demo->swapchainImageCount * sizeof(VkImage)); |
| 762 | assert(swapchainImages); |
| 763 | err = demo->fpGetSwapchainImagesKHR(demo->device, demo->swap_chain, |
| 764 | &demo->swapchainImageCount, |
| 765 | swapchainImages); |
Ian Elliott | aaae535 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 766 | assert(!err); |
Ian Elliott | 2d47da9 | 2015-07-13 12:20:56 -0600 | [diff] [blame] | 767 | #ifdef WORK_AROUND_CODE |
Ian Elliott | 8528eff | 2015-08-07 15:56:59 -0600 | [diff] [blame] | 768 | // After the proper code was created, other parts of this demo were |
| 769 | // modified to only support DEMO_BUFFER_COUNT number of command buffers, |
| 770 | // images, etc. Live with that for now. |
| 771 | // 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] | 772 | // by vkCreateSwapchainKHR(). |
| 773 | demo->swapchainImageCount = DEMO_BUFFER_COUNT; |
Ian Elliott | 2d47da9 | 2015-07-13 12:20:56 -0600 | [diff] [blame] | 774 | #endif // WORK_AROUND_CODE |
Ian Elliott | aaae535 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 775 | |
Ian Elliott | a078197 | 2015-08-21 15:09:33 -0600 | [diff] [blame] | 776 | demo->buffers = (SwapchainBuffers*)malloc(sizeof(SwapchainBuffers)*demo->swapchainImageCount); |
Ian Elliott | aaae535 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 777 | assert(demo->buffers); |
| 778 | |
Ian Elliott | a078197 | 2015-08-21 15:09:33 -0600 | [diff] [blame] | 779 | for (i = 0; i < demo->swapchainImageCount; i++) { |
Courtney Goeltzenleuchter | 41866db | 2015-09-01 17:30:39 -0600 | [diff] [blame] | 780 | VkImageViewCreateInfo color_image_view = { |
| 781 | .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO, |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 782 | .pNext = NULL, |
| 783 | .format = demo->format, |
Courtney Goeltzenleuchter | 41866db | 2015-09-01 17:30:39 -0600 | [diff] [blame] | 784 | .channels = { |
| 785 | .r = VK_CHANNEL_SWIZZLE_R, |
| 786 | .g = VK_CHANNEL_SWIZZLE_G, |
| 787 | .b = VK_CHANNEL_SWIZZLE_B, |
| 788 | .a = VK_CHANNEL_SWIZZLE_A, |
| 789 | }, |
| 790 | .subresourceRange = { |
| 791 | .aspect = VK_IMAGE_ASPECT_COLOR, |
| 792 | .baseMipLevel = 0, |
| 793 | .mipLevels = 1, |
| 794 | .baseArraySlice = 0, |
| 795 | .arraySize = 1 |
| 796 | }, |
| 797 | .viewType = VK_IMAGE_VIEW_TYPE_2D, |
| 798 | .flags = 0, |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 799 | }; |
| 800 | |
Ian Elliott | a078197 | 2015-08-21 15:09:33 -0600 | [diff] [blame] | 801 | demo->buffers[i].image = swapchainImages[i]; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 802 | |
Courtney Goeltzenleuchter | 752eb8e | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 803 | demo_set_image_layout(demo, demo->buffers[i].image, |
malnasse | eb25d3f | 2015-06-03 17:28:38 -0400 | [diff] [blame] | 804 | VK_IMAGE_ASPECT_COLOR, |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 805 | VK_IMAGE_LAYOUT_UNDEFINED, |
| 806 | VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL); |
Courtney Goeltzenleuchter | 752eb8e | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 807 | |
Courtney Goeltzenleuchter | 41866db | 2015-09-01 17:30:39 -0600 | [diff] [blame] | 808 | color_image_view.image = demo->buffers[i].image; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 809 | |
Courtney Goeltzenleuchter | 41866db | 2015-09-01 17:30:39 -0600 | [diff] [blame] | 810 | err = vkCreateImageView(demo->device, |
| 811 | &color_image_view, &demo->buffers[i].view); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 812 | assert(!err); |
| 813 | } |
| 814 | } |
| 815 | |
| 816 | static void demo_prepare_depth(struct demo *demo) |
| 817 | { |
Tony Barbour | 72304ef | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 818 | const VkFormat depth_format = VK_FORMAT_D16_UNORM; |
Courtney Goeltzenleuchter | 78351a6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 819 | const VkImageCreateInfo image = { |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 820 | .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO, |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 821 | .pNext = NULL, |
Tony Barbour | 72304ef | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 822 | .imageType = VK_IMAGE_TYPE_2D, |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 823 | .format = depth_format, |
| 824 | .extent = { demo->width, demo->height, 1 }, |
| 825 | .mipLevels = 1, |
| 826 | .arraySize = 1, |
| 827 | .samples = 1, |
Tony Barbour | 72304ef | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 828 | .tiling = VK_IMAGE_TILING_OPTIMAL, |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 829 | .usage = VK_IMAGE_USAGE_DEPTH_STENCIL_BIT, |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 830 | .flags = 0, |
| 831 | }; |
Courtney Goeltzenleuchter | 28c4d5f | 2015-04-14 18:48:46 -0600 | [diff] [blame] | 832 | VkMemoryAllocInfo mem_alloc = { |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 833 | .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO, |
Mark Lobodzinski | a6907f7 | 2015-04-16 08:52:00 -0500 | [diff] [blame] | 834 | .pNext = NULL, |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 835 | .allocationSize = 0, |
Mark Lobodzinski | eadf998 | 2015-07-02 16:49:40 -0600 | [diff] [blame] | 836 | .memoryTypeIndex = 0, |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 837 | }; |
Courtney Goeltzenleuchter | 41866db | 2015-09-01 17:30:39 -0600 | [diff] [blame] | 838 | VkImageViewCreateInfo view = { |
| 839 | .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO, |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 840 | .pNext = NULL, |
Tony Barbour | 155cce8 | 2015-07-03 10:33:54 -0600 | [diff] [blame] | 841 | .image.handle = VK_NULL_HANDLE, |
Courtney Goeltzenleuchter | 556ee32 | 2015-07-15 17:41:38 -0600 | [diff] [blame] | 842 | .format = depth_format, |
Courtney Goeltzenleuchter | 41866db | 2015-09-01 17:30:39 -0600 | [diff] [blame] | 843 | .subresourceRange = { |
Cody Northrop | f2241ba | 2015-09-03 16:09:28 -0600 | [diff] [blame] | 844 | .aspect = VK_IMAGE_ASPECT_DEPTH, |
Courtney Goeltzenleuchter | 41866db | 2015-09-01 17:30:39 -0600 | [diff] [blame] | 845 | .baseMipLevel = 0, |
| 846 | .mipLevels = 1, |
| 847 | .baseArraySlice = 0, |
| 848 | .arraySize = 1 |
| 849 | }, |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 850 | .flags = 0, |
Courtney Goeltzenleuchter | 41866db | 2015-09-01 17:30:39 -0600 | [diff] [blame] | 851 | .viewType = VK_IMAGE_VIEW_TYPE_2D, |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 852 | }; |
Mike Stroyan | ebae832 | 2015-04-17 12:36:38 -0600 | [diff] [blame] | 853 | |
Mark Lobodzinski | 2dcdfd7 | 2015-05-29 09:32:35 -0500 | [diff] [blame] | 854 | VkMemoryRequirements mem_reqs; |
Tony Barbour | fdc2d35 | 2015-04-22 09:02:32 -0600 | [diff] [blame] | 855 | VkResult U_ASSERT_ONLY err; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 856 | |
| 857 | demo->depth.format = depth_format; |
| 858 | |
| 859 | /* create image */ |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 860 | err = vkCreateImage(demo->device, &image, |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 861 | &demo->depth.image); |
| 862 | assert(!err); |
| 863 | |
Tony Barbour | 155cce8 | 2015-07-03 10:33:54 -0600 | [diff] [blame] | 864 | err = vkGetImageMemoryRequirements(demo->device, |
| 865 | demo->depth.image, &mem_reqs); |
Mark Lobodzinski | eadf998 | 2015-07-02 16:49:40 -0600 | [diff] [blame] | 866 | |
| 867 | mem_alloc.allocationSize = mem_reqs.size; |
| 868 | err = memory_type_from_properties(demo, |
| 869 | mem_reqs.memoryTypeBits, |
| 870 | VK_MEMORY_PROPERTY_DEVICE_ONLY, |
| 871 | &mem_alloc.memoryTypeIndex); |
| 872 | assert(!err); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 873 | |
Mark Lobodzinski | 2dcdfd7 | 2015-05-29 09:32:35 -0500 | [diff] [blame] | 874 | /* allocate memory */ |
| 875 | err = vkAllocMemory(demo->device, &mem_alloc, &demo->depth.mem); |
| 876 | assert(!err); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 877 | |
Mark Lobodzinski | 2dcdfd7 | 2015-05-29 09:32:35 -0500 | [diff] [blame] | 878 | /* bind memory */ |
Tony Barbour | 155cce8 | 2015-07-03 10:33:54 -0600 | [diff] [blame] | 879 | err = vkBindImageMemory(demo->device, demo->depth.image, |
Mark Lobodzinski | 2dcdfd7 | 2015-05-29 09:32:35 -0500 | [diff] [blame] | 880 | demo->depth.mem, 0); |
| 881 | assert(!err); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 882 | |
Courtney Goeltzenleuchter | 752eb8e | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 883 | demo_set_image_layout(demo, demo->depth.image, |
malnasse | eb25d3f | 2015-06-03 17:28:38 -0400 | [diff] [blame] | 884 | VK_IMAGE_ASPECT_DEPTH, |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 885 | VK_IMAGE_LAYOUT_UNDEFINED, |
| 886 | VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL); |
Courtney Goeltzenleuchter | 752eb8e | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 887 | |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 888 | /* create image view */ |
| 889 | view.image = demo->depth.image; |
Courtney Goeltzenleuchter | 41866db | 2015-09-01 17:30:39 -0600 | [diff] [blame] | 890 | err = vkCreateImageView(demo->device, &view, &demo->depth.view); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 891 | assert(!err); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 892 | } |
| 893 | |
Courtney Goeltzenleuchter | 6836939 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 894 | /** loadTexture |
Courtney Goeltzenleuchter | c56c36a | 2015-04-21 09:31:23 -0600 | [diff] [blame] | 895 | * loads a png file into an memory object, using cstdio , libpng. |
Courtney Goeltzenleuchter | 6836939 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 896 | * |
Courtney Goeltzenleuchter | c56c36a | 2015-04-21 09:31:23 -0600 | [diff] [blame] | 897 | * \param demo : Needed to access VK calls |
| 898 | * \param filename : the png file to be loaded |
| 899 | * \param width : width of png, to be updated as a side effect of this function |
| 900 | * \param height : height of png, to be updated as a side effect of this function |
Courtney Goeltzenleuchter | 6836939 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 901 | * |
Courtney Goeltzenleuchter | c56c36a | 2015-04-21 09:31:23 -0600 | [diff] [blame] | 902 | * \return bool : an opengl texture id. true if successful?, |
| 903 | * should be validated by the client of this function. |
Courtney Goeltzenleuchter | 6836939 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 904 | * |
| 905 | * Source: http://en.wikibooks.org/wiki/OpenGL_Programming/Intermediate/Textures |
| 906 | * Modified to copy image to memory |
| 907 | * |
| 908 | */ |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 909 | bool loadTexture(const char *filename, uint8_t *rgba_data, |
Courtney Goeltzenleuchter | 78351a6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 910 | VkSubresourceLayout *layout, |
Mark Lobodzinski | c4fffc7 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 911 | int32_t *width, int32_t *height) |
Courtney Goeltzenleuchter | 6836939 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 912 | { |
| 913 | //header for testing if it is a png |
| 914 | png_byte header[8]; |
Tony Barbour | f992173 | 2015-04-22 11:36:22 -0600 | [diff] [blame] | 915 | int is_png, bit_depth, color_type, rowbytes; |
| 916 | size_t retval; |
Ian Elliott | 1e42dff | 2015-02-13 14:29:21 -0700 | [diff] [blame] | 917 | png_uint_32 i, twidth, theight; |
Courtney Goeltzenleuchter | 6836939 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 918 | png_structp png_ptr; |
| 919 | png_infop info_ptr, end_info; |
| 920 | png_byte *image_data; |
| 921 | png_bytep *row_pointers; |
| 922 | |
| 923 | //open file as binary |
| 924 | FILE *fp = fopen(filename, "rb"); |
| 925 | if (!fp) { |
| 926 | return false; |
| 927 | } |
| 928 | |
| 929 | //read the header |
Tony Barbour | fdc2d35 | 2015-04-22 09:02:32 -0600 | [diff] [blame] | 930 | retval = fread(header, 1, 8, fp); |
| 931 | if (retval != 8) { |
| 932 | fclose(fp); |
| 933 | return false; |
| 934 | } |
Courtney Goeltzenleuchter | 6836939 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 935 | |
| 936 | //test if png |
| 937 | is_png = !png_sig_cmp(header, 0, 8); |
| 938 | if (!is_png) { |
| 939 | fclose(fp); |
| 940 | return false; |
| 941 | } |
| 942 | |
| 943 | //create png struct |
| 944 | png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, |
| 945 | NULL, NULL); |
| 946 | if (!png_ptr) { |
| 947 | fclose(fp); |
| 948 | return (false); |
| 949 | } |
| 950 | |
| 951 | //create png info struct |
| 952 | info_ptr = png_create_info_struct(png_ptr); |
| 953 | if (!info_ptr) { |
| 954 | png_destroy_read_struct(&png_ptr, (png_infopp) NULL, (png_infopp) NULL); |
| 955 | fclose(fp); |
| 956 | return (false); |
| 957 | } |
| 958 | |
| 959 | //create png info struct |
| 960 | end_info = png_create_info_struct(png_ptr); |
| 961 | if (!end_info) { |
| 962 | png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp) NULL); |
| 963 | fclose(fp); |
| 964 | return (false); |
| 965 | } |
| 966 | |
| 967 | //png error stuff, not sure libpng man suggests this. |
| 968 | if (setjmp(png_jmpbuf(png_ptr))) { |
| 969 | png_destroy_read_struct(&png_ptr, &info_ptr, &end_info); |
| 970 | fclose(fp); |
| 971 | return (false); |
| 972 | } |
| 973 | |
| 974 | //init png reading |
| 975 | png_init_io(png_ptr, fp); |
| 976 | |
| 977 | //let libpng know you already read the first 8 bytes |
| 978 | png_set_sig_bytes(png_ptr, 8); |
| 979 | |
| 980 | // read all the info up to the image data |
| 981 | png_read_info(png_ptr, info_ptr); |
| 982 | |
| 983 | // get info about png |
| 984 | png_get_IHDR(png_ptr, info_ptr, &twidth, &theight, &bit_depth, &color_type, |
| 985 | NULL, NULL, NULL); |
| 986 | |
| 987 | //update width and height based on png info |
| 988 | *width = twidth; |
| 989 | *height = theight; |
| 990 | |
| 991 | // Require that incoming texture be 8bits per color component |
| 992 | // and 4 components (RGBA). |
| 993 | if (png_get_bit_depth(png_ptr, info_ptr) != 8 || |
| 994 | png_get_channels(png_ptr, info_ptr) != 4) { |
| 995 | return false; |
| 996 | } |
| 997 | |
| 998 | if (rgba_data == NULL) { |
| 999 | // If data pointer is null, we just want the width & height |
| 1000 | // clean up memory and close stuff |
| 1001 | png_destroy_read_struct(&png_ptr, &info_ptr, &end_info); |
| 1002 | fclose(fp); |
| 1003 | |
| 1004 | return true; |
| 1005 | } |
| 1006 | |
| 1007 | // Update the png info struct. |
| 1008 | png_read_update_info(png_ptr, info_ptr); |
| 1009 | |
| 1010 | // Row size in bytes. |
| 1011 | rowbytes = png_get_rowbytes(png_ptr, info_ptr); |
| 1012 | |
| 1013 | // Allocate the image_data as a big block, to be given to opengl |
| 1014 | image_data = (png_byte *)malloc(rowbytes * theight * sizeof(png_byte)); |
| 1015 | if (!image_data) { |
| 1016 | //clean up memory and close stuff |
| 1017 | png_destroy_read_struct(&png_ptr, &info_ptr, &end_info); |
| 1018 | fclose(fp); |
| 1019 | return false; |
| 1020 | } |
| 1021 | |
| 1022 | // row_pointers is for pointing to image_data for reading the png with libpng |
| 1023 | row_pointers = (png_bytep *)malloc(theight * sizeof(png_bytep)); |
| 1024 | if (!row_pointers) { |
| 1025 | //clean up memory and close stuff |
| 1026 | png_destroy_read_struct(&png_ptr, &info_ptr, &end_info); |
| 1027 | // delete[] image_data; |
| 1028 | fclose(fp); |
| 1029 | return false; |
| 1030 | } |
| 1031 | // set the individual row_pointers to point at the correct offsets of image_data |
| 1032 | for (i = 0; i < theight; ++i) |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 1033 | row_pointers[theight - 1 - i] = rgba_data + i * layout->rowPitch; |
Courtney Goeltzenleuchter | 6836939 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 1034 | |
| 1035 | // read the png into image_data through row_pointers |
| 1036 | png_read_image(png_ptr, row_pointers); |
| 1037 | |
| 1038 | // clean up memory and close stuff |
| 1039 | png_destroy_read_struct(&png_ptr, &info_ptr, &end_info); |
| 1040 | free(row_pointers); |
| 1041 | free(image_data); |
| 1042 | fclose(fp); |
| 1043 | |
| 1044 | return true; |
| 1045 | } |
Courtney Goeltzenleuchter | 6836939 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 1046 | |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 1047 | static void demo_prepare_texture_image(struct demo *demo, |
| 1048 | const char *filename, |
Courtney Goeltzenleuchter | 752eb8e | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 1049 | struct texture_object *tex_obj, |
Courtney Goeltzenleuchter | 78351a6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 1050 | VkImageTiling tiling, |
Courtney Goeltzenleuchter | c56c36a | 2015-04-21 09:31:23 -0600 | [diff] [blame] | 1051 | VkImageUsageFlags usage, |
Courtney Goeltzenleuchter | 78351a6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 1052 | VkFlags mem_props) |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1053 | { |
Mike Stroyan | 8b89e07 | 2015-06-15 14:21:03 -0600 | [diff] [blame] | 1054 | const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM; |
Mark Lobodzinski | c4fffc7 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 1055 | int32_t tex_width; |
| 1056 | int32_t tex_height; |
Tony Barbour | fdc2d35 | 2015-04-22 09:02:32 -0600 | [diff] [blame] | 1057 | VkResult U_ASSERT_ONLY err; |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 1058 | |
David Pinedo | 30bd71d | 2015-04-23 08:16:57 -0600 | [diff] [blame] | 1059 | if (!loadTexture(filename, NULL, NULL, &tex_width, &tex_height)) |
| 1060 | { |
| 1061 | printf("Failed to load textures\n"); |
| 1062 | fflush(stdout); |
| 1063 | exit(1); |
| 1064 | } |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 1065 | |
Courtney Goeltzenleuchter | 752eb8e | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 1066 | tex_obj->tex_width = tex_width; |
| 1067 | tex_obj->tex_height = tex_height; |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 1068 | |
Courtney Goeltzenleuchter | 78351a6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 1069 | const VkImageCreateInfo image_create_info = { |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1070 | .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO, |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 1071 | .pNext = NULL, |
Tony Barbour | 72304ef | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 1072 | .imageType = VK_IMAGE_TYPE_2D, |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 1073 | .format = tex_format, |
| 1074 | .extent = { tex_width, tex_height, 1 }, |
| 1075 | .mipLevels = 1, |
| 1076 | .arraySize = 1, |
| 1077 | .samples = 1, |
| 1078 | .tiling = tiling, |
Courtney Goeltzenleuchter | c56c36a | 2015-04-21 09:31:23 -0600 | [diff] [blame] | 1079 | .usage = usage, |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 1080 | .flags = 0, |
| 1081 | }; |
Courtney Goeltzenleuchter | 28c4d5f | 2015-04-14 18:48:46 -0600 | [diff] [blame] | 1082 | VkMemoryAllocInfo mem_alloc = { |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1083 | .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO, |
Mark Lobodzinski | a6907f7 | 2015-04-16 08:52:00 -0500 | [diff] [blame] | 1084 | .pNext = NULL, |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 1085 | .allocationSize = 0, |
Mark Lobodzinski | eadf998 | 2015-07-02 16:49:40 -0600 | [diff] [blame] | 1086 | .memoryTypeIndex = 0, |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 1087 | }; |
| 1088 | |
Mark Lobodzinski | 2dcdfd7 | 2015-05-29 09:32:35 -0500 | [diff] [blame] | 1089 | VkMemoryRequirements mem_reqs; |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 1090 | |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1091 | err = vkCreateImage(demo->device, &image_create_info, |
Courtney Goeltzenleuchter | 752eb8e | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 1092 | &tex_obj->image); |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 1093 | assert(!err); |
| 1094 | |
Tony Barbour | 155cce8 | 2015-07-03 10:33:54 -0600 | [diff] [blame] | 1095 | err = vkGetImageMemoryRequirements(demo->device, tex_obj->image, &mem_reqs); |
Tony Barbour | ecf1b2b | 2015-06-24 16:06:58 -0600 | [diff] [blame] | 1096 | assert(!err); |
Piers Daniell | 735ee53 | 2015-02-23 16:23:13 -0700 | [diff] [blame] | 1097 | |
Mark Lobodzinski | 2dcdfd7 | 2015-05-29 09:32:35 -0500 | [diff] [blame] | 1098 | mem_alloc.allocationSize = mem_reqs.size; |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 1099 | |
Mark Lobodzinski | eadf998 | 2015-07-02 16:49:40 -0600 | [diff] [blame] | 1100 | err = memory_type_from_properties(demo, mem_reqs.memoryTypeBits, mem_props, &mem_alloc.memoryTypeIndex); |
| 1101 | assert(!err); |
| 1102 | |
Mark Lobodzinski | 2dcdfd7 | 2015-05-29 09:32:35 -0500 | [diff] [blame] | 1103 | /* allocate memory */ |
| 1104 | err = vkAllocMemory(demo->device, &mem_alloc, |
| 1105 | &(tex_obj->mem)); |
| 1106 | assert(!err); |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 1107 | |
Mark Lobodzinski | 2dcdfd7 | 2015-05-29 09:32:35 -0500 | [diff] [blame] | 1108 | /* bind memory */ |
Tony Barbour | 155cce8 | 2015-07-03 10:33:54 -0600 | [diff] [blame] | 1109 | err = vkBindImageMemory(demo->device, tex_obj->image, |
Mark Lobodzinski | 2dcdfd7 | 2015-05-29 09:32:35 -0500 | [diff] [blame] | 1110 | tex_obj->mem, 0); |
| 1111 | assert(!err); |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 1112 | |
Tony Barbour | 72304ef | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 1113 | if (mem_props & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) { |
Courtney Goeltzenleuchter | 78351a6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 1114 | const VkImageSubresource subres = { |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1115 | .aspect = VK_IMAGE_ASPECT_COLOR, |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 1116 | .mipLevel = 0, |
| 1117 | .arraySlice = 0, |
| 1118 | }; |
Courtney Goeltzenleuchter | 78351a6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 1119 | VkSubresourceLayout layout; |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 1120 | void *data; |
| 1121 | |
Tony Barbour | ecf1b2b | 2015-06-24 16:06:58 -0600 | [diff] [blame] | 1122 | err = vkGetImageSubresourceLayout(demo->device, tex_obj->image, &subres, &layout); |
| 1123 | assert(!err); |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 1124 | |
Mark Lobodzinski | 2dcdfd7 | 2015-05-29 09:32:35 -0500 | [diff] [blame] | 1125 | err = vkMapMemory(demo->device, tex_obj->mem, 0, 0, 0, &data); |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 1126 | assert(!err); |
| 1127 | |
| 1128 | if (!loadTexture(filename, data, &layout, &tex_width, &tex_height)) { |
| 1129 | fprintf(stderr, "Error loading texture: %s\n", filename); |
| 1130 | } |
| 1131 | |
Mark Lobodzinski | e4c92a6 | 2015-09-07 13:59:43 -0600 | [diff] [blame] | 1132 | vkUnmapMemory(demo->device, tex_obj->mem); |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 1133 | } |
Courtney Goeltzenleuchter | 752eb8e | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 1134 | |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1135 | tex_obj->imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL; |
Courtney Goeltzenleuchter | 752eb8e | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 1136 | demo_set_image_layout(demo, tex_obj->image, |
malnasse | eb25d3f | 2015-06-03 17:28:38 -0400 | [diff] [blame] | 1137 | VK_IMAGE_ASPECT_COLOR, |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1138 | VK_IMAGE_LAYOUT_UNDEFINED, |
Courtney Goeltzenleuchter | 752eb8e | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 1139 | tex_obj->imageLayout); |
| 1140 | /* 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] | 1141 | } |
| 1142 | |
Mark Lobodzinski | da9b109 | 2015-04-16 11:44:05 -0500 | [diff] [blame] | 1143 | 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] | 1144 | { |
| 1145 | /* clean up staging resources */ |
Mark Lobodzinski | 2dcdfd7 | 2015-05-29 09:32:35 -0500 | [diff] [blame] | 1146 | vkFreeMemory(demo->device, tex_objs->mem); |
Tony Barbour | 155cce8 | 2015-07-03 10:33:54 -0600 | [diff] [blame] | 1147 | vkDestroyImage(demo->device, tex_objs->image); |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 1148 | } |
| 1149 | |
| 1150 | static void demo_prepare_textures(struct demo *demo) |
| 1151 | { |
Tony Barbour | 72304ef | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 1152 | const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM; |
Courtney Goeltzenleuchter | 78351a6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 1153 | VkFormatProperties props; |
Tony Barbour | fdc2d35 | 2015-04-22 09:02:32 -0600 | [diff] [blame] | 1154 | VkResult U_ASSERT_ONLY err; |
Mark Lobodzinski | c4fffc7 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 1155 | uint32_t i; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1156 | |
Courtney Goeltzenleuchter | 4a593f1 | 2015-07-12 12:52:09 -0600 | [diff] [blame] | 1157 | err = vkGetPhysicalDeviceFormatProperties(demo->gpu, tex_format, &props); |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 1158 | assert(!err); |
| 1159 | |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1160 | for (i = 0; i < DEMO_TEXTURE_COUNT; i++) { |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 1161 | |
FslNopper | 6a7e50e | 2015-05-06 21:42:01 +0200 | [diff] [blame] | 1162 | 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] | 1163 | /* Device can texture using linear textures */ |
| 1164 | demo_prepare_texture_image(demo, tex_files[i], &demo->textures[i], |
Courtney Goeltzenleuchter | c56c36a | 2015-04-21 09:31:23 -0600 | [diff] [blame] | 1165 | 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] | 1166 | } else if (props.optimalTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) { |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 1167 | /* Must use staging buffer to copy linear texture to optimized */ |
Courtney Goeltzenleuchter | 752eb8e | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 1168 | struct texture_object staging_texture; |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 1169 | |
| 1170 | memset(&staging_texture, 0, sizeof(staging_texture)); |
| 1171 | demo_prepare_texture_image(demo, tex_files[i], &staging_texture, |
Courtney Goeltzenleuchter | c56c36a | 2015-04-21 09:31:23 -0600 | [diff] [blame] | 1172 | 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] | 1173 | |
| 1174 | demo_prepare_texture_image(demo, tex_files[i], &demo->textures[i], |
Courtney Goeltzenleuchter | c56c36a | 2015-04-21 09:31:23 -0600 | [diff] [blame] | 1175 | VK_IMAGE_TILING_OPTIMAL, |
| 1176 | (VK_IMAGE_USAGE_TRANSFER_DESTINATION_BIT | VK_IMAGE_USAGE_SAMPLED_BIT), |
| 1177 | VK_MEMORY_PROPERTY_DEVICE_ONLY); |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 1178 | |
Courtney Goeltzenleuchter | 752eb8e | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 1179 | demo_set_image_layout(demo, staging_texture.image, |
malnasse | eb25d3f | 2015-06-03 17:28:38 -0400 | [diff] [blame] | 1180 | VK_IMAGE_ASPECT_COLOR, |
Courtney Goeltzenleuchter | 752eb8e | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 1181 | staging_texture.imageLayout, |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1182 | VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL); |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 1183 | |
Courtney Goeltzenleuchter | 752eb8e | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 1184 | demo_set_image_layout(demo, demo->textures[i].image, |
malnasse | eb25d3f | 2015-06-03 17:28:38 -0400 | [diff] [blame] | 1185 | VK_IMAGE_ASPECT_COLOR, |
Courtney Goeltzenleuchter | 752eb8e | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 1186 | demo->textures[i].imageLayout, |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1187 | VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL); |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 1188 | |
Courtney Goeltzenleuchter | 78351a6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 1189 | VkImageCopy copy_region = { |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1190 | .srcSubresource = { VK_IMAGE_ASPECT_COLOR, 0, 0 }, |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 1191 | .srcOffset = { 0, 0, 0 }, |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1192 | .destSubresource = { VK_IMAGE_ASPECT_COLOR, 0, 0 }, |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 1193 | .destOffset = { 0, 0, 0 }, |
| 1194 | .extent = { staging_texture.tex_width, staging_texture.tex_height, 1 }, |
| 1195 | }; |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1196 | vkCmdCopyImage(demo->cmd, |
| 1197 | staging_texture.image, VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL, |
| 1198 | demo->textures[i].image, VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL, |
Courtney Goeltzenleuchter | 8e89a31 | 2015-03-25 11:25:10 -0600 | [diff] [blame] | 1199 | 1, ©_region); |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 1200 | |
Courtney Goeltzenleuchter | 752eb8e | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 1201 | demo_set_image_layout(demo, demo->textures[i].image, |
malnasse | eb25d3f | 2015-06-03 17:28:38 -0400 | [diff] [blame] | 1202 | VK_IMAGE_ASPECT_COLOR, |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1203 | VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL, |
Courtney Goeltzenleuchter | 752eb8e | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 1204 | demo->textures[i].imageLayout); |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 1205 | |
Courtney Goeltzenleuchter | 752eb8e | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 1206 | demo_flush_init_cmd(demo); |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 1207 | |
Courtney Goeltzenleuchter | f3aeb2b | 2015-04-21 09:30:03 -0600 | [diff] [blame] | 1208 | demo_destroy_texture_image(demo, &staging_texture); |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 1209 | } else { |
Mike Stroyan | 8b89e07 | 2015-06-15 14:21:03 -0600 | [diff] [blame] | 1210 | /* Can't support VK_FORMAT_R8G8B8A8_UNORM !? */ |
| 1211 | assert(!"No support for R8G8B8A8_UNORM as texture image format"); |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 1212 | } |
| 1213 | |
Courtney Goeltzenleuchter | 78351a6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 1214 | const VkSamplerCreateInfo sampler = { |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1215 | .sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO, |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1216 | .pNext = NULL, |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1217 | .magFilter = VK_TEX_FILTER_NEAREST, |
| 1218 | .minFilter = VK_TEX_FILTER_NEAREST, |
Tony Barbour | 72304ef | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 1219 | .mipMode = VK_TEX_MIPMAP_MODE_BASE, |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1220 | .addressU = VK_TEX_ADDRESS_CLAMP, |
| 1221 | .addressV = VK_TEX_ADDRESS_CLAMP, |
| 1222 | .addressW = VK_TEX_ADDRESS_CLAMP, |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1223 | .mipLodBias = 0.0f, |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 1224 | .maxAnisotropy = 1, |
Tony Barbour | 72304ef | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 1225 | .compareOp = VK_COMPARE_OP_NEVER, |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1226 | .minLod = 0.0f, |
| 1227 | .maxLod = 0.0f, |
Tony Barbour | cb530c7 | 2015-06-25 16:56:44 -0600 | [diff] [blame] | 1228 | .borderColor = VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE, |
Mark Lobodzinski | 644dc49 | 2015-09-01 15:42:56 -0600 | [diff] [blame] | 1229 | .unnormalizedCoordinates = VK_FALSE, |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1230 | }; |
Courtney Goeltzenleuchter | 6836939 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 1231 | |
Courtney Goeltzenleuchter | 78351a6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 1232 | VkImageViewCreateInfo view = { |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1233 | .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO, |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1234 | .pNext = NULL, |
Tony Barbour | 155cce8 | 2015-07-03 10:33:54 -0600 | [diff] [blame] | 1235 | .image.handle = VK_NULL_HANDLE, |
Tony Barbour | 72304ef | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 1236 | .viewType = VK_IMAGE_VIEW_TYPE_2D, |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 1237 | .format = tex_format, |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1238 | .channels = { VK_CHANNEL_SWIZZLE_R, |
| 1239 | VK_CHANNEL_SWIZZLE_G, |
| 1240 | VK_CHANNEL_SWIZZLE_B, |
| 1241 | VK_CHANNEL_SWIZZLE_A, }, |
| 1242 | .subresourceRange = { VK_IMAGE_ASPECT_COLOR, 0, 1, 0, 1 }, |
Courtney Goeltzenleuchter | 41866db | 2015-09-01 17:30:39 -0600 | [diff] [blame] | 1243 | .flags = 0, |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1244 | }; |
Jon Ashburn | b2a6665 | 2015-01-16 09:37:43 -0700 | [diff] [blame] | 1245 | |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1246 | /* create sampler */ |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1247 | err = vkCreateSampler(demo->device, &sampler, |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1248 | &demo->textures[i].sampler); |
| 1249 | assert(!err); |
| 1250 | |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1251 | /* create image view */ |
| 1252 | view.image = demo->textures[i].image; |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1253 | err = vkCreateImageView(demo->device, &view, |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1254 | &demo->textures[i].view); |
| 1255 | assert(!err); |
| 1256 | } |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1257 | } |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1258 | |
Courtney Goeltzenleuchter | 4984d15 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 1259 | void demo_prepare_cube_data_buffer(struct demo *demo) |
| 1260 | { |
Courtney Goeltzenleuchter | 28c4d5f | 2015-04-14 18:48:46 -0600 | [diff] [blame] | 1261 | VkBufferCreateInfo buf_info; |
| 1262 | VkBufferViewCreateInfo view_info; |
Courtney Goeltzenleuchter | 28c4d5f | 2015-04-14 18:48:46 -0600 | [diff] [blame] | 1263 | VkMemoryAllocInfo alloc_info = { |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1264 | .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO, |
Mark Lobodzinski | a6907f7 | 2015-04-16 08:52:00 -0500 | [diff] [blame] | 1265 | .pNext = NULL, |
Jon Ashburn | ae7c21c | 2015-01-19 15:00:26 -0700 | [diff] [blame] | 1266 | .allocationSize = 0, |
Mark Lobodzinski | eadf998 | 2015-07-02 16:49:40 -0600 | [diff] [blame] | 1267 | .memoryTypeIndex = 0, |
Jon Ashburn | ae7c21c | 2015-01-19 15:00:26 -0700 | [diff] [blame] | 1268 | }; |
Mark Lobodzinski | 2dcdfd7 | 2015-05-29 09:32:35 -0500 | [diff] [blame] | 1269 | VkMemoryRequirements mem_reqs; |
Mark Lobodzinski | c4fffc7 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 1270 | uint8_t *pData; |
Courtney Goeltzenleuchter | 4984d15 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 1271 | int i; |
| 1272 | mat4x4 MVP, VP; |
Tony Barbour | fdc2d35 | 2015-04-22 09:02:32 -0600 | [diff] [blame] | 1273 | VkResult U_ASSERT_ONLY err; |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1274 | struct vktexcube_vs_uniform data; |
Courtney Goeltzenleuchter | 4984d15 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 1275 | |
Courtney Goeltzenleuchter | 4984d15 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 1276 | mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix); |
Courtney Goeltzenleuchter | 4984d15 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 1277 | mat4x4_mul(MVP, VP, demo->model_matrix); |
| 1278 | memcpy(data.mvp, MVP, sizeof(MVP)); |
Courtney Goeltzenleuchter | 8879d3a | 2014-10-29 08:29:35 -0600 | [diff] [blame] | 1279 | // dumpMatrix("MVP", MVP); |
Courtney Goeltzenleuchter | 4984d15 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 1280 | |
| 1281 | for (i=0; i<12*3; i++) { |
| 1282 | data.position[i][0] = g_vertex_buffer_data[i*3]; |
| 1283 | data.position[i][1] = g_vertex_buffer_data[i*3+1]; |
| 1284 | data.position[i][2] = g_vertex_buffer_data[i*3+2]; |
| 1285 | data.position[i][3] = 1.0f; |
| 1286 | data.attr[i][0] = g_uv_buffer_data[2*i]; |
| 1287 | data.attr[i][1] = g_uv_buffer_data[2*i + 1]; |
| 1288 | data.attr[i][2] = 0; |
| 1289 | data.attr[i][3] = 0; |
| 1290 | } |
| 1291 | |
Chia-I Wu | 8ea21c7 | 2015-01-01 07:55:04 +0800 | [diff] [blame] | 1292 | memset(&buf_info, 0, sizeof(buf_info)); |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1293 | buf_info.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO; |
Courtney Goeltzenleuchter | 42a078d | 2015-04-15 15:29:59 -0600 | [diff] [blame] | 1294 | buf_info.usage = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT; |
Cody Northrop | 9180730 | 2015-07-16 10:29:32 -0600 | [diff] [blame] | 1295 | buf_info.size = sizeof(data); |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1296 | err = vkCreateBuffer(demo->device, &buf_info, &demo->uniform_data.buf); |
Chia-I Wu | 8ea21c7 | 2015-01-01 07:55:04 +0800 | [diff] [blame] | 1297 | assert(!err); |
| 1298 | |
Tony Barbour | 155cce8 | 2015-07-03 10:33:54 -0600 | [diff] [blame] | 1299 | err = vkGetBufferMemoryRequirements(demo->device, demo->uniform_data.buf, &mem_reqs); |
Courtney Goeltzenleuchter | 0a82c0b | 2015-07-15 11:17:08 -0600 | [diff] [blame] | 1300 | assert(!err); |
Chia-I Wu | 8ea21c7 | 2015-01-01 07:55:04 +0800 | [diff] [blame] | 1301 | |
Mark Lobodzinski | 2dcdfd7 | 2015-05-29 09:32:35 -0500 | [diff] [blame] | 1302 | alloc_info.allocationSize = mem_reqs.size; |
Mark Lobodzinski | eadf998 | 2015-07-02 16:49:40 -0600 | [diff] [blame] | 1303 | err = memory_type_from_properties(demo, |
| 1304 | mem_reqs.memoryTypeBits, |
| 1305 | VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, |
| 1306 | &alloc_info.memoryTypeIndex); |
| 1307 | assert(!err); |
Courtney Goeltzenleuchter | 4984d15 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 1308 | |
Mark Lobodzinski | 2dcdfd7 | 2015-05-29 09:32:35 -0500 | [diff] [blame] | 1309 | err = vkAllocMemory(demo->device, &alloc_info, &(demo->uniform_data.mem)); |
| 1310 | assert(!err); |
Courtney Goeltzenleuchter | 4984d15 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 1311 | |
Mark Lobodzinski | 2dcdfd7 | 2015-05-29 09:32:35 -0500 | [diff] [blame] | 1312 | err = vkMapMemory(demo->device, demo->uniform_data.mem, 0, 0, 0, (void **) &pData); |
| 1313 | assert(!err); |
Courtney Goeltzenleuchter | 4984d15 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 1314 | |
Mark Lobodzinski | 2dcdfd7 | 2015-05-29 09:32:35 -0500 | [diff] [blame] | 1315 | memcpy(pData, &data, sizeof data); |
Courtney Goeltzenleuchter | 4984d15 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 1316 | |
Mark Lobodzinski | e4c92a6 | 2015-09-07 13:59:43 -0600 | [diff] [blame] | 1317 | vkUnmapMemory(demo->device, demo->uniform_data.mem); |
Mark Lobodzinski | 2dcdfd7 | 2015-05-29 09:32:35 -0500 | [diff] [blame] | 1318 | |
Tony Barbour | 155cce8 | 2015-07-03 10:33:54 -0600 | [diff] [blame] | 1319 | err = vkBindBufferMemory(demo->device, |
| 1320 | demo->uniform_data.buf, |
Mark Lobodzinski | 2dcdfd7 | 2015-05-29 09:32:35 -0500 | [diff] [blame] | 1321 | demo->uniform_data.mem, 0); |
| 1322 | assert(!err); |
Chia-I Wu | 8ea21c7 | 2015-01-01 07:55:04 +0800 | [diff] [blame] | 1323 | |
| 1324 | memset(&view_info, 0, sizeof(view_info)); |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1325 | view_info.sType = VK_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO; |
Chia-I Wu | 8ea21c7 | 2015-01-01 07:55:04 +0800 | [diff] [blame] | 1326 | view_info.buffer = demo->uniform_data.buf; |
Tony Barbour | 72304ef | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 1327 | view_info.viewType = VK_BUFFER_VIEW_TYPE_RAW; |
Chia-I Wu | 8ea21c7 | 2015-01-01 07:55:04 +0800 | [diff] [blame] | 1328 | view_info.offset = 0; |
| 1329 | view_info.range = sizeof(data); |
| 1330 | |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1331 | err = vkCreateBufferView(demo->device, &view_info, &demo->uniform_data.view); |
Chia-I Wu | 8ea21c7 | 2015-01-01 07:55:04 +0800 | [diff] [blame] | 1332 | assert(!err); |
| 1333 | |
Chia-I Wu | ae721ba | 2015-05-25 16:27:55 +0800 | [diff] [blame] | 1334 | demo->uniform_data.desc.bufferView = demo->uniform_data.view; |
Courtney Goeltzenleuchter | 4984d15 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 1335 | } |
| 1336 | |
Chia-I Wu | 6a3c897 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 1337 | static void demo_prepare_descriptor_layout(struct demo *demo) |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1338 | { |
Courtney Goeltzenleuchter | 78351a6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 1339 | const VkDescriptorSetLayoutBinding layout_bindings[2] = { |
Chia-I Wu | a2aa863 | 2015-03-26 15:04:41 +0800 | [diff] [blame] | 1340 | [0] = { |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1341 | .descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, |
Chia-I Wu | 6d29a41 | 2015-05-25 16:22:52 +0800 | [diff] [blame] | 1342 | .arraySize = 1, |
Tony Barbour | 72304ef | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 1343 | .stageFlags = VK_SHADER_STAGE_VERTEX_BIT, |
Chia-I Wu | 91e8e21 | 2015-03-27 12:56:09 +0800 | [diff] [blame] | 1344 | .pImmutableSamplers = NULL, |
Chia-I Wu | a2aa863 | 2015-03-26 15:04:41 +0800 | [diff] [blame] | 1345 | }, |
| 1346 | [1] = { |
Courtney Goeltzenleuchter | 42a078d | 2015-04-15 15:29:59 -0600 | [diff] [blame] | 1347 | .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, |
Chia-I Wu | 6d29a41 | 2015-05-25 16:22:52 +0800 | [diff] [blame] | 1348 | .arraySize = DEMO_TEXTURE_COUNT, |
Tony Barbour | 72304ef | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 1349 | .stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT, |
Chia-I Wu | 91e8e21 | 2015-03-27 12:56:09 +0800 | [diff] [blame] | 1350 | .pImmutableSamplers = NULL, |
Chia-I Wu | a2aa863 | 2015-03-26 15:04:41 +0800 | [diff] [blame] | 1351 | }, |
| 1352 | }; |
Courtney Goeltzenleuchter | 78351a6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 1353 | const VkDescriptorSetLayoutCreateInfo descriptor_layout = { |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1354 | .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO, |
Chia-I Wu | 6a3c897 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 1355 | .pNext = NULL, |
Chia-I Wu | a2aa863 | 2015-03-26 15:04:41 +0800 | [diff] [blame] | 1356 | .count = 2, |
| 1357 | .pBinding = layout_bindings, |
Chia-I Wu | 6a3c897 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 1358 | }; |
Tony Barbour | fdc2d35 | 2015-04-22 09:02:32 -0600 | [diff] [blame] | 1359 | VkResult U_ASSERT_ONLY err; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1360 | |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1361 | err = vkCreateDescriptorSetLayout(demo->device, |
Chia-I Wu | b58c24a | 2015-03-26 15:27:55 +0800 | [diff] [blame] | 1362 | &descriptor_layout, &demo->desc_layout); |
| 1363 | assert(!err); |
| 1364 | |
Mark Lobodzinski | 1cfc772 | 2015-04-17 14:11:39 -0500 | [diff] [blame] | 1365 | const VkPipelineLayoutCreateInfo pPipelineLayoutCreateInfo = { |
| 1366 | .sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO, |
| 1367 | .pNext = NULL, |
| 1368 | .descriptorSetCount = 1, |
| 1369 | .pSetLayouts = &demo->desc_layout, |
| 1370 | }; |
| 1371 | |
| 1372 | err = vkCreatePipelineLayout(demo->device, |
| 1373 | &pPipelineLayoutCreateInfo, |
| 1374 | &demo->pipeline_layout); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1375 | assert(!err); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1376 | } |
| 1377 | |
Chia-I Wu | f973e32 | 2015-07-08 13:34:24 +0800 | [diff] [blame] | 1378 | static void demo_prepare_render_pass(struct demo *demo) |
| 1379 | { |
Chia-I Wu | a74c5b2 | 2015-07-07 11:50:03 +0800 | [diff] [blame] | 1380 | const VkAttachmentDescription attachments[2] = { |
| 1381 | [0] = { |
| 1382 | .sType = VK_STRUCTURE_TYPE_ATTACHMENT_DESCRIPTION, |
| 1383 | .pNext = NULL, |
| 1384 | .format = demo->format, |
| 1385 | .samples = 1, |
| 1386 | .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR, |
| 1387 | .storeOp = VK_ATTACHMENT_STORE_OP_STORE, |
| 1388 | .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE, |
| 1389 | .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE, |
| 1390 | .initialLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, |
| 1391 | .finalLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, |
| 1392 | }, |
| 1393 | [1] = { |
| 1394 | .sType = VK_STRUCTURE_TYPE_ATTACHMENT_DESCRIPTION, |
| 1395 | .pNext = NULL, |
| 1396 | .format = demo->depth.format, |
| 1397 | .samples = 1, |
| 1398 | .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR, |
| 1399 | .storeOp = VK_ATTACHMENT_STORE_OP_DONT_CARE, |
| 1400 | .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE, |
| 1401 | .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE, |
| 1402 | .initialLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL, |
| 1403 | .finalLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL, |
| 1404 | }, |
Chia-I Wu | f973e32 | 2015-07-08 13:34:24 +0800 | [diff] [blame] | 1405 | }; |
Chia-I Wu | a74c5b2 | 2015-07-07 11:50:03 +0800 | [diff] [blame] | 1406 | const VkAttachmentReference color_reference = { |
| 1407 | .attachment = 0, |
| 1408 | .layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, |
| 1409 | }; |
| 1410 | const VkSubpassDescription subpass = { |
| 1411 | .sType = VK_STRUCTURE_TYPE_SUBPASS_DESCRIPTION, |
| 1412 | .pNext = NULL, |
| 1413 | .pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS, |
| 1414 | .flags = 0, |
| 1415 | .inputCount = 0, |
Cody Northrop | 945bb83 | 2015-08-04 11:16:41 -0600 | [diff] [blame] | 1416 | .pInputAttachments = NULL, |
Chia-I Wu | a74c5b2 | 2015-07-07 11:50:03 +0800 | [diff] [blame] | 1417 | .colorCount = 1, |
Cody Northrop | 945bb83 | 2015-08-04 11:16:41 -0600 | [diff] [blame] | 1418 | .pColorAttachments = &color_reference, |
| 1419 | .pResolveAttachments = NULL, |
Chia-I Wu | a74c5b2 | 2015-07-07 11:50:03 +0800 | [diff] [blame] | 1420 | .depthStencilAttachment = { |
| 1421 | .attachment = 1, |
| 1422 | .layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL, |
| 1423 | }, |
| 1424 | .preserveCount = 0, |
Cody Northrop | 945bb83 | 2015-08-04 11:16:41 -0600 | [diff] [blame] | 1425 | .pPreserveAttachments = NULL, |
Chia-I Wu | a74c5b2 | 2015-07-07 11:50:03 +0800 | [diff] [blame] | 1426 | }; |
Chia-I Wu | f973e32 | 2015-07-08 13:34:24 +0800 | [diff] [blame] | 1427 | const VkRenderPassCreateInfo rp_info = { |
| 1428 | .sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO, |
| 1429 | .pNext = NULL, |
Chia-I Wu | a74c5b2 | 2015-07-07 11:50:03 +0800 | [diff] [blame] | 1430 | .attachmentCount = 2, |
| 1431 | .pAttachments = attachments, |
| 1432 | .subpassCount = 1, |
| 1433 | .pSubpasses = &subpass, |
| 1434 | .dependencyCount = 0, |
| 1435 | .pDependencies = NULL, |
Chia-I Wu | f973e32 | 2015-07-08 13:34:24 +0800 | [diff] [blame] | 1436 | }; |
Chia-I Wu | a74c5b2 | 2015-07-07 11:50:03 +0800 | [diff] [blame] | 1437 | VkResult U_ASSERT_ONLY err; |
Chia-I Wu | f973e32 | 2015-07-08 13:34:24 +0800 | [diff] [blame] | 1438 | |
| 1439 | err = vkCreateRenderPass(demo->device, &rp_info, &demo->render_pass); |
| 1440 | assert(!err); |
| 1441 | } |
| 1442 | |
Tobin Ehlis | d1f17ac | 2015-07-02 11:02:49 -0600 | [diff] [blame] | 1443 | static VkShader demo_prepare_shader(struct demo* demo, |
Tony Barbour | 72304ef | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 1444 | VkShaderStage stage, |
Tony Barbour | b6dba5c | 2015-07-21 09:04:41 -0600 | [diff] [blame] | 1445 | VkShaderModule* pShaderModule, |
Tobin Ehlis | d1f17ac | 2015-07-02 11:02:49 -0600 | [diff] [blame] | 1446 | const void* code, |
Mark Lobodzinski | c4fffc7 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 1447 | size_t size) |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1448 | { |
Courtney Goeltzenleuchter | a80a1a9 | 2015-06-24 18:24:19 -0600 | [diff] [blame] | 1449 | VkShaderModuleCreateInfo moduleCreateInfo; |
| 1450 | VkShaderCreateInfo shaderCreateInfo; |
Courtney Goeltzenleuchter | 78351a6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 1451 | VkShader shader; |
| 1452 | VkResult err; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1453 | |
Courtney Goeltzenleuchter | 97db1c1 | 2014-10-30 15:14:16 -0600 | [diff] [blame] | 1454 | |
Courtney Goeltzenleuchter | a80a1a9 | 2015-06-24 18:24:19 -0600 | [diff] [blame] | 1455 | moduleCreateInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO; |
| 1456 | moduleCreateInfo.pNext = NULL; |
| 1457 | |
| 1458 | shaderCreateInfo.sType = VK_STRUCTURE_TYPE_SHADER_CREATE_INFO; |
| 1459 | shaderCreateInfo.pNext = NULL; |
Tobin Ehlis | d1f17ac | 2015-07-02 11:02:49 -0600 | [diff] [blame] | 1460 | shaderCreateInfo.pName = "main"; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1461 | |
Cody Northrop | 1fedb21 | 2015-05-28 11:27:16 -0600 | [diff] [blame] | 1462 | if (!demo->use_glsl) { |
Courtney Goeltzenleuchter | a80a1a9 | 2015-06-24 18:24:19 -0600 | [diff] [blame] | 1463 | moduleCreateInfo.codeSize = size; |
| 1464 | moduleCreateInfo.pCode = code; |
| 1465 | moduleCreateInfo.flags = 0; |
Tony Barbour | b6dba5c | 2015-07-21 09:04:41 -0600 | [diff] [blame] | 1466 | err = vkCreateShaderModule(demo->device, &moduleCreateInfo, pShaderModule); |
Cody Northrop | 1fedb21 | 2015-05-28 11:27:16 -0600 | [diff] [blame] | 1467 | if (err) { |
Courtney Goeltzenleuchter | a80a1a9 | 2015-06-24 18:24:19 -0600 | [diff] [blame] | 1468 | free((void *) moduleCreateInfo.pCode); |
Cody Northrop | 1fedb21 | 2015-05-28 11:27:16 -0600 | [diff] [blame] | 1469 | } |
Courtney Goeltzenleuchter | a80a1a9 | 2015-06-24 18:24:19 -0600 | [diff] [blame] | 1470 | |
| 1471 | shaderCreateInfo.flags = 0; |
Tony Barbour | b6dba5c | 2015-07-21 09:04:41 -0600 | [diff] [blame] | 1472 | shaderCreateInfo.module = *pShaderModule; |
Piers Daniell | 8611f13 | 2015-07-16 09:35:35 -0600 | [diff] [blame] | 1473 | shaderCreateInfo.pName = "main"; |
Cody Northrop | a9e2194 | 2015-08-24 15:11:10 -0600 | [diff] [blame] | 1474 | shaderCreateInfo.stage = stage; |
Courtney Goeltzenleuchter | a80a1a9 | 2015-06-24 18:24:19 -0600 | [diff] [blame] | 1475 | err = vkCreateShader(demo->device, &shaderCreateInfo, &shader); |
Cody Northrop | 1fedb21 | 2015-05-28 11:27:16 -0600 | [diff] [blame] | 1476 | } else { |
| 1477 | // Create fake SPV structure to feed GLSL |
| 1478 | // to the driver "under the covers" |
Courtney Goeltzenleuchter | a80a1a9 | 2015-06-24 18:24:19 -0600 | [diff] [blame] | 1479 | moduleCreateInfo.codeSize = 3 * sizeof(uint32_t) + size + 1; |
| 1480 | moduleCreateInfo.pCode = malloc(moduleCreateInfo.codeSize); |
| 1481 | moduleCreateInfo.flags = 0; |
Cody Northrop | 1fedb21 | 2015-05-28 11:27:16 -0600 | [diff] [blame] | 1482 | |
| 1483 | /* try version 0 first: VkShaderStage followed by GLSL */ |
Courtney Goeltzenleuchter | a80a1a9 | 2015-06-24 18:24:19 -0600 | [diff] [blame] | 1484 | ((uint32_t *) moduleCreateInfo.pCode)[0] = ICD_SPV_MAGIC; |
| 1485 | ((uint32_t *) moduleCreateInfo.pCode)[1] = 0; |
| 1486 | ((uint32_t *) moduleCreateInfo.pCode)[2] = stage; |
| 1487 | memcpy(((uint32_t *) moduleCreateInfo.pCode + 3), code, size + 1); |
Cody Northrop | 1fedb21 | 2015-05-28 11:27:16 -0600 | [diff] [blame] | 1488 | |
Tony Barbour | b6dba5c | 2015-07-21 09:04:41 -0600 | [diff] [blame] | 1489 | err = vkCreateShaderModule(demo->device, &moduleCreateInfo, pShaderModule); |
Cody Northrop | 1fedb21 | 2015-05-28 11:27:16 -0600 | [diff] [blame] | 1490 | if (err) { |
Courtney Goeltzenleuchter | a80a1a9 | 2015-06-24 18:24:19 -0600 | [diff] [blame] | 1491 | free((void *) moduleCreateInfo.pCode); |
Cody Northrop | 1fedb21 | 2015-05-28 11:27:16 -0600 | [diff] [blame] | 1492 | } |
Courtney Goeltzenleuchter | a80a1a9 | 2015-06-24 18:24:19 -0600 | [diff] [blame] | 1493 | |
| 1494 | shaderCreateInfo.flags = 0; |
Tony Barbour | b6dba5c | 2015-07-21 09:04:41 -0600 | [diff] [blame] | 1495 | shaderCreateInfo.module = *pShaderModule; |
Piers Daniell | 8611f13 | 2015-07-16 09:35:35 -0600 | [diff] [blame] | 1496 | shaderCreateInfo.pName = "main"; |
Cody Northrop | a9e2194 | 2015-08-24 15:11:10 -0600 | [diff] [blame] | 1497 | shaderCreateInfo.stage = stage; |
Courtney Goeltzenleuchter | a80a1a9 | 2015-06-24 18:24:19 -0600 | [diff] [blame] | 1498 | err = vkCreateShader(demo->device, &shaderCreateInfo, &shader); |
Courtney Goeltzenleuchter | 6836939 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 1499 | } |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1500 | return shader; |
| 1501 | } |
| 1502 | |
Cody Northrop | 8b12a1f | 2015-03-17 15:55:58 -0600 | [diff] [blame] | 1503 | char *demo_read_spv(const char *filename, size_t *psize) |
Courtney Goeltzenleuchter | 97db1c1 | 2014-10-30 15:14:16 -0600 | [diff] [blame] | 1504 | { |
| 1505 | long int size; |
Tony Barbour | f992173 | 2015-04-22 11:36:22 -0600 | [diff] [blame] | 1506 | size_t U_ASSERT_ONLY retval; |
Courtney Goeltzenleuchter | 97db1c1 | 2014-10-30 15:14:16 -0600 | [diff] [blame] | 1507 | void *shader_code; |
| 1508 | |
| 1509 | FILE *fp = fopen(filename, "rb"); |
| 1510 | if (!fp) return NULL; |
| 1511 | |
| 1512 | fseek(fp, 0L, SEEK_END); |
| 1513 | size = ftell(fp); |
| 1514 | |
| 1515 | fseek(fp, 0L, SEEK_SET); |
| 1516 | |
| 1517 | shader_code = malloc(size); |
Tony Barbour | fdc2d35 | 2015-04-22 09:02:32 -0600 | [diff] [blame] | 1518 | retval = fread(shader_code, size, 1, fp); |
| 1519 | assert(retval == 1); |
Courtney Goeltzenleuchter | 97db1c1 | 2014-10-30 15:14:16 -0600 | [diff] [blame] | 1520 | |
| 1521 | *psize = size; |
| 1522 | |
| 1523 | return shader_code; |
| 1524 | } |
| 1525 | |
Courtney Goeltzenleuchter | 78351a6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 1526 | static VkShader demo_prepare_vs(struct demo *demo) |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1527 | { |
Cody Northrop | 1fedb21 | 2015-05-28 11:27:16 -0600 | [diff] [blame] | 1528 | if (!demo->use_glsl) { |
| 1529 | void *vertShaderCode; |
| 1530 | size_t size; |
Courtney Goeltzenleuchter | 97db1c1 | 2014-10-30 15:14:16 -0600 | [diff] [blame] | 1531 | |
Cody Northrop | 1fedb21 | 2015-05-28 11:27:16 -0600 | [diff] [blame] | 1532 | vertShaderCode = demo_read_spv("cube-vert.spv", &size); |
Courtney Goeltzenleuchter | 97db1c1 | 2014-10-30 15:14:16 -0600 | [diff] [blame] | 1533 | |
Tony Barbour | b6dba5c | 2015-07-21 09:04:41 -0600 | [diff] [blame] | 1534 | 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] | 1535 | vertShaderCode, size); |
| 1536 | } else { |
| 1537 | static const char *vertShaderText = |
| 1538 | "#version 140\n" |
| 1539 | "#extension GL_ARB_separate_shader_objects : enable\n" |
| 1540 | "#extension GL_ARB_shading_language_420pack : enable\n" |
| 1541 | "\n" |
| 1542 | "layout(binding = 0) uniform buf {\n" |
| 1543 | " mat4 MVP;\n" |
| 1544 | " vec4 position[12*3];\n" |
| 1545 | " vec4 attr[12*3];\n" |
| 1546 | "} ubuf;\n" |
| 1547 | "\n" |
| 1548 | "layout (location = 0) out vec4 texcoord;\n" |
| 1549 | "\n" |
| 1550 | "void main() \n" |
| 1551 | "{\n" |
| 1552 | " texcoord = ubuf.attr[gl_VertexID];\n" |
| 1553 | " gl_Position = ubuf.MVP * ubuf.position[gl_VertexID];\n" |
| 1554 | "\n" |
| 1555 | " // GL->VK conventions\n" |
| 1556 | " gl_Position.y = -gl_Position.y;\n" |
| 1557 | " gl_Position.z = (gl_Position.z + gl_Position.w) / 2.0;\n" |
| 1558 | "}\n"; |
Courtney Goeltzenleuchter | fc3b953 | 2014-10-28 10:32:57 -0600 | [diff] [blame] | 1559 | |
Tony Barbour | b6dba5c | 2015-07-21 09:04:41 -0600 | [diff] [blame] | 1560 | 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] | 1561 | (const void *) vertShaderText, |
| 1562 | strlen(vertShaderText)); |
| 1563 | } |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1564 | } |
| 1565 | |
Courtney Goeltzenleuchter | 78351a6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 1566 | static VkShader demo_prepare_fs(struct demo *demo) |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1567 | { |
Cody Northrop | 1fedb21 | 2015-05-28 11:27:16 -0600 | [diff] [blame] | 1568 | if (!demo->use_glsl) { |
| 1569 | void *fragShaderCode; |
| 1570 | size_t size; |
Courtney Goeltzenleuchter | 97db1c1 | 2014-10-30 15:14:16 -0600 | [diff] [blame] | 1571 | |
Cody Northrop | 1fedb21 | 2015-05-28 11:27:16 -0600 | [diff] [blame] | 1572 | fragShaderCode = demo_read_spv("cube-frag.spv", &size); |
Courtney Goeltzenleuchter | 97db1c1 | 2014-10-30 15:14:16 -0600 | [diff] [blame] | 1573 | |
Tony Barbour | b6dba5c | 2015-07-21 09:04:41 -0600 | [diff] [blame] | 1574 | 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] | 1575 | fragShaderCode, size); |
| 1576 | } else { |
| 1577 | static const char *fragShaderText = |
| 1578 | "#version 140\n" |
| 1579 | "#extension GL_ARB_separate_shader_objects : enable\n" |
| 1580 | "#extension GL_ARB_shading_language_420pack : enable\n" |
| 1581 | "layout (binding = 1) uniform sampler2D tex;\n" |
| 1582 | "\n" |
| 1583 | "layout (location = 0) in vec4 texcoord;\n" |
| 1584 | "layout (location = 0) out vec4 uFragColor;\n" |
| 1585 | "void main() {\n" |
| 1586 | " uFragColor = texture(tex, texcoord.xy);\n" |
| 1587 | "}\n"; |
Courtney Goeltzenleuchter | fc3b953 | 2014-10-28 10:32:57 -0600 | [diff] [blame] | 1588 | |
Tony Barbour | b6dba5c | 2015-07-21 09:04:41 -0600 | [diff] [blame] | 1589 | 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] | 1590 | (const void *) fragShaderText, |
| 1591 | strlen(fragShaderText)); |
| 1592 | } |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1593 | } |
| 1594 | |
| 1595 | static void demo_prepare_pipeline(struct demo *demo) |
| 1596 | { |
Courtney Goeltzenleuchter | 78351a6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 1597 | VkGraphicsPipelineCreateInfo pipeline; |
Jon Ashburn | c4ab7af | 2015-07-09 15:02:25 -0600 | [diff] [blame] | 1598 | VkPipelineCacheCreateInfo pipelineCache; |
Tony Barbour | 194bf28 | 2015-07-10 15:29:03 -0600 | [diff] [blame] | 1599 | VkPipelineInputAssemblyStateCreateInfo ia; |
| 1600 | VkPipelineRasterStateCreateInfo rs; |
| 1601 | VkPipelineColorBlendStateCreateInfo cb; |
| 1602 | VkPipelineDepthStencilStateCreateInfo ds; |
| 1603 | VkPipelineViewportStateCreateInfo vp; |
| 1604 | VkPipelineMultisampleStateCreateInfo ms; |
Tony Barbour | fdc2d35 | 2015-04-22 09:02:32 -0600 | [diff] [blame] | 1605 | VkResult U_ASSERT_ONLY err; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1606 | |
| 1607 | memset(&pipeline, 0, sizeof(pipeline)); |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1608 | pipeline.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO; |
Mark Lobodzinski | 1cfc772 | 2015-04-17 14:11:39 -0500 | [diff] [blame] | 1609 | pipeline.layout = demo->pipeline_layout; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1610 | |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1611 | memset(&ia, 0, sizeof(ia)); |
Tony Barbour | 194bf28 | 2015-07-10 15:29:03 -0600 | [diff] [blame] | 1612 | ia.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO; |
Tony Barbour | 72304ef | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 1613 | ia.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1614 | |
| 1615 | memset(&rs, 0, sizeof(rs)); |
Tony Barbour | 194bf28 | 2015-07-10 15:29:03 -0600 | [diff] [blame] | 1616 | rs.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTER_STATE_CREATE_INFO; |
Tony Barbour | 72304ef | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 1617 | rs.fillMode = VK_FILL_MODE_SOLID; |
| 1618 | rs.cullMode = VK_CULL_MODE_BACK; |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1619 | rs.frontFace = VK_FRONT_FACE_CCW; |
Chia-I Wu | bb67e6e | 2015-04-22 14:20:52 +0800 | [diff] [blame] | 1620 | rs.depthClipEnable = VK_TRUE; |
Cody Northrop | 709c174 | 2015-08-17 11:10:49 -0600 | [diff] [blame] | 1621 | rs.rasterizerDiscardEnable = VK_FALSE; |
| 1622 | rs.depthBiasEnable = VK_FALSE; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1623 | |
| 1624 | memset(&cb, 0, sizeof(cb)); |
Tony Barbour | 194bf28 | 2015-07-10 15:29:03 -0600 | [diff] [blame] | 1625 | cb.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO; |
| 1626 | VkPipelineColorBlendAttachmentState att_state[1]; |
Tony Barbour | 29645d0 | 2015-01-16 14:27:35 -0700 | [diff] [blame] | 1627 | memset(att_state, 0, sizeof(att_state)); |
Tony Barbour | 29645d0 | 2015-01-16 14:27:35 -0700 | [diff] [blame] | 1628 | att_state[0].channelWriteMask = 0xf; |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1629 | att_state[0].blendEnable = VK_FALSE; |
Tony Barbour | 29645d0 | 2015-01-16 14:27:35 -0700 | [diff] [blame] | 1630 | cb.attachmentCount = 1; |
| 1631 | cb.pAttachments = att_state; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1632 | |
Tony Barbour | 29645d0 | 2015-01-16 14:27:35 -0700 | [diff] [blame] | 1633 | memset(&vp, 0, sizeof(vp)); |
Tony Barbour | 194bf28 | 2015-07-10 15:29:03 -0600 | [diff] [blame] | 1634 | vp.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO; |
Tony Barbour | 72304ef | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 1635 | vp.viewportCount = 1; |
Tony Barbour | 29645d0 | 2015-01-16 14:27:35 -0700 | [diff] [blame] | 1636 | |
| 1637 | memset(&ds, 0, sizeof(ds)); |
Tony Barbour | 194bf28 | 2015-07-10 15:29:03 -0600 | [diff] [blame] | 1638 | ds.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO; |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1639 | ds.depthTestEnable = VK_TRUE; |
| 1640 | ds.depthWriteEnable = VK_TRUE; |
Tony Barbour | 72304ef | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 1641 | ds.depthCompareOp = VK_COMPARE_OP_LESS_EQUAL; |
Cody Northrop | 5e4125d | 2015-08-26 10:01:32 -0600 | [diff] [blame] | 1642 | ds.depthBoundsTestEnable = VK_FALSE; |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1643 | ds.back.stencilFailOp = VK_STENCIL_OP_KEEP; |
| 1644 | ds.back.stencilPassOp = VK_STENCIL_OP_KEEP; |
Tony Barbour | 72304ef | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 1645 | ds.back.stencilCompareOp = VK_COMPARE_OP_ALWAYS; |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1646 | ds.stencilTestEnable = VK_FALSE; |
Tony Barbour | 29645d0 | 2015-01-16 14:27:35 -0700 | [diff] [blame] | 1647 | ds.front = ds.back; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1648 | |
Tony Barbour | 29645d0 | 2015-01-16 14:27:35 -0700 | [diff] [blame] | 1649 | memset(&ms, 0, sizeof(ms)); |
Tony Barbour | 194bf28 | 2015-07-10 15:29:03 -0600 | [diff] [blame] | 1650 | ms.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO; |
Cody Northrop | 011546b | 2015-08-04 14:34:54 -0600 | [diff] [blame] | 1651 | ms.pSampleMask = NULL; |
Tony Barbour | 3ca2250 | 2015-06-26 10:18:34 -0600 | [diff] [blame] | 1652 | ms.rasterSamples = 1; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1653 | |
Mark Lobodzinski | 521d776 | 2015-06-23 15:11:57 -0600 | [diff] [blame] | 1654 | // Two stages: vs and fs |
| 1655 | pipeline.stageCount = 2; |
| 1656 | VkPipelineShaderStageCreateInfo shaderStages[2]; |
| 1657 | memset(&shaderStages, 0, 2 * sizeof(VkPipelineShaderStageCreateInfo)); |
| 1658 | |
| 1659 | shaderStages[0].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO; |
| 1660 | shaderStages[0].stage = VK_SHADER_STAGE_VERTEX; |
| 1661 | shaderStages[0].shader = demo_prepare_vs(demo); |
| 1662 | |
| 1663 | shaderStages[1].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO; |
| 1664 | shaderStages[1].stage = VK_SHADER_STAGE_FRAGMENT; |
| 1665 | shaderStages[1].shader = demo_prepare_fs(demo); |
| 1666 | |
Jon Ashburn | c4ab7af | 2015-07-09 15:02:25 -0600 | [diff] [blame] | 1667 | memset(&pipelineCache, 0, sizeof(pipelineCache)); |
| 1668 | pipelineCache.sType = VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1669 | |
Jon Ashburn | c4ab7af | 2015-07-09 15:02:25 -0600 | [diff] [blame] | 1670 | err = vkCreatePipelineCache(demo->device, &pipelineCache, &demo->pipelineCache); |
| 1671 | assert(!err); |
Tony Barbour | 194bf28 | 2015-07-10 15:29:03 -0600 | [diff] [blame] | 1672 | |
| 1673 | pipeline.pVertexInputState = NULL; |
| 1674 | pipeline.pInputAssemblyState = &ia; |
| 1675 | pipeline.pRasterState = &rs; |
| 1676 | pipeline.pColorBlendState = &cb; |
| 1677 | pipeline.pMultisampleState = &ms; |
| 1678 | pipeline.pViewportState = &vp; |
| 1679 | pipeline.pDepthStencilState = &ds; |
| 1680 | pipeline.pStages = shaderStages; |
Tony Barbour | 13ed322 | 2015-08-06 14:32:54 -0600 | [diff] [blame] | 1681 | pipeline.renderPass = demo->render_pass; |
Tony Barbour | 194bf28 | 2015-07-10 15:29:03 -0600 | [diff] [blame] | 1682 | |
Courtney Goeltzenleuchter | d311fd0 | 2015-08-13 16:55:04 -0600 | [diff] [blame] | 1683 | pipeline.renderPass = demo->render_pass; |
| 1684 | |
Jon Ashburn | c4ab7af | 2015-07-09 15:02:25 -0600 | [diff] [blame] | 1685 | err = vkCreateGraphicsPipelines(demo->device, demo->pipelineCache, 1, &pipeline, &demo->pipeline); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1686 | assert(!err); |
| 1687 | |
Mark Lobodzinski | 521d776 | 2015-06-23 15:11:57 -0600 | [diff] [blame] | 1688 | for (uint32_t i = 0; i < pipeline.stageCount; i++) { |
Tony Barbour | 155cce8 | 2015-07-03 10:33:54 -0600 | [diff] [blame] | 1689 | vkDestroyShader(demo->device, shaderStages[i].shader); |
Mark Lobodzinski | 521d776 | 2015-06-23 15:11:57 -0600 | [diff] [blame] | 1690 | } |
Tony Barbour | b6dba5c | 2015-07-21 09:04:41 -0600 | [diff] [blame] | 1691 | vkDestroyShaderModule(demo->device, demo->frag_shader_module); |
| 1692 | vkDestroyShaderModule(demo->device, demo->vert_shader_module); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1693 | } |
| 1694 | |
| 1695 | static void demo_prepare_dynamic_states(struct demo *demo) |
| 1696 | { |
Tony Barbour | 155cce8 | 2015-07-03 10:33:54 -0600 | [diff] [blame] | 1697 | VkDynamicViewportStateCreateInfo viewport_create; |
Cody Northrop | 5e4125d | 2015-08-26 10:01:32 -0600 | [diff] [blame] | 1698 | VkDynamicLineWidthStateCreateInfo line_width; |
| 1699 | VkDynamicDepthBiasStateCreateInfo depth_bias; |
| 1700 | VkDynamicBlendStateCreateInfo blend; |
| 1701 | VkDynamicDepthBoundsStateCreateInfo depth_bounds; |
Cody Northrop | 36a642f | 2015-08-18 15:21:16 -0600 | [diff] [blame] | 1702 | VkDynamicStencilStateCreateInfo stencil; |
Tony Barbour | fdc2d35 | 2015-04-22 09:02:32 -0600 | [diff] [blame] | 1703 | VkResult U_ASSERT_ONLY err; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1704 | |
Tony Barbour | 29645d0 | 2015-01-16 14:27:35 -0700 | [diff] [blame] | 1705 | memset(&viewport_create, 0, sizeof(viewport_create)); |
Tony Barbour | 155cce8 | 2015-07-03 10:33:54 -0600 | [diff] [blame] | 1706 | viewport_create.sType = VK_STRUCTURE_TYPE_DYNAMIC_VIEWPORT_STATE_CREATE_INFO; |
Courtney Goeltzenleuchter | 1710d8d | 2015-02-11 14:13:34 -0700 | [diff] [blame] | 1707 | viewport_create.viewportAndScissorCount = 1; |
Courtney Goeltzenleuchter | 78351a6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 1708 | VkViewport viewport; |
Piers Daniell | 735ee53 | 2015-02-23 16:23:13 -0700 | [diff] [blame] | 1709 | memset(&viewport, 0, sizeof(viewport)); |
Mark Lobodzinski | c4fffc7 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 1710 | viewport.height = (float) demo->height; |
| 1711 | viewport.width = (float) demo->width; |
| 1712 | viewport.minDepth = (float) 0.0f; |
| 1713 | viewport.maxDepth = (float) 1.0f; |
Piers Daniell | 735ee53 | 2015-02-23 16:23:13 -0700 | [diff] [blame] | 1714 | viewport_create.pViewports = &viewport; |
Chris Forbes | faa9173 | 2015-06-22 17:21:59 +1200 | [diff] [blame] | 1715 | VkRect2D scissor; |
Piers Daniell | 735ee53 | 2015-02-23 16:23:13 -0700 | [diff] [blame] | 1716 | memset(&scissor, 0, sizeof(scissor)); |
Courtney Goeltzenleuchter | 1710d8d | 2015-02-11 14:13:34 -0700 | [diff] [blame] | 1717 | scissor.extent.width = demo->width; |
| 1718 | scissor.extent.height = demo->height; |
| 1719 | scissor.offset.x = 0; |
| 1720 | scissor.offset.y = 0; |
Courtney Goeltzenleuchter | 1710d8d | 2015-02-11 14:13:34 -0700 | [diff] [blame] | 1721 | viewport_create.pScissors = &scissor; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1722 | |
Cody Northrop | 5e4125d | 2015-08-26 10:01:32 -0600 | [diff] [blame] | 1723 | memset(&line_width, 0, sizeof(line_width)); |
| 1724 | line_width.sType = VK_STRUCTURE_TYPE_DYNAMIC_LINE_WIDTH_STATE_CREATE_INFO; |
| 1725 | line_width.lineWidth = 1.0; |
Cody Northrop | 709c174 | 2015-08-17 11:10:49 -0600 | [diff] [blame] | 1726 | |
Cody Northrop | 5e4125d | 2015-08-26 10:01:32 -0600 | [diff] [blame] | 1727 | memset(&depth_bias, 0, sizeof(depth_bias)); |
| 1728 | depth_bias.sType = VK_STRUCTURE_TYPE_DYNAMIC_DEPTH_BIAS_STATE_CREATE_INFO; |
| 1729 | depth_bias.depthBias = 0.0f; |
| 1730 | depth_bias.depthBiasClamp = 0.0f; |
| 1731 | depth_bias.slopeScaledDepthBias = 0.0f; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1732 | |
Cody Northrop | 5e4125d | 2015-08-26 10:01:32 -0600 | [diff] [blame] | 1733 | memset(&blend, 0, sizeof(blend)); |
| 1734 | blend.sType = VK_STRUCTURE_TYPE_DYNAMIC_BLEND_STATE_CREATE_INFO; |
| 1735 | blend.blendConst[0] = 1.0f; |
| 1736 | blend.blendConst[1] = 1.0f; |
| 1737 | blend.blendConst[2] = 1.0f; |
| 1738 | blend.blendConst[3] = 1.0f; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1739 | |
Cody Northrop | 5e4125d | 2015-08-26 10:01:32 -0600 | [diff] [blame] | 1740 | memset(&depth_bounds, 0, sizeof(depth_bounds)); |
| 1741 | depth_bounds.sType = VK_STRUCTURE_TYPE_DYNAMIC_DEPTH_BOUNDS_STATE_CREATE_INFO; |
| 1742 | depth_bounds.minDepthBounds = 0.0f; |
| 1743 | depth_bounds.maxDepthBounds = 1.0f; |
Cody Northrop | 36a642f | 2015-08-18 15:21:16 -0600 | [diff] [blame] | 1744 | |
| 1745 | memset(&stencil, 0, sizeof(stencil)); |
| 1746 | stencil.sType = VK_STRUCTURE_TYPE_DYNAMIC_STENCIL_STATE_CREATE_INFO; |
| 1747 | stencil.stencilReference = 0; |
Cody Northrop | 5e4125d | 2015-08-26 10:01:32 -0600 | [diff] [blame] | 1748 | stencil.stencilCompareMask = 0xff; |
Cody Northrop | 36a642f | 2015-08-18 15:21:16 -0600 | [diff] [blame] | 1749 | stencil.stencilWriteMask = 0xff; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1750 | |
Cody Northrop | 5e4125d | 2015-08-26 10:01:32 -0600 | [diff] [blame] | 1751 | err = vkCreateDynamicViewportState(demo->device, &viewport_create, &demo->dynamic_viewport); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1752 | assert(!err); |
| 1753 | |
Cody Northrop | 5e4125d | 2015-08-26 10:01:32 -0600 | [diff] [blame] | 1754 | err = vkCreateDynamicLineWidthState(demo->device, &line_width, &demo->dynamic_line_width); |
Cody Northrop | 709c174 | 2015-08-17 11:10:49 -0600 | [diff] [blame] | 1755 | assert(!err); |
| 1756 | |
Cody Northrop | 5e4125d | 2015-08-26 10:01:32 -0600 | [diff] [blame] | 1757 | err = vkCreateDynamicDepthBiasState(demo->device, &depth_bias, &demo->dynamic_depth_bias); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1758 | assert(!err); |
| 1759 | |
Cody Northrop | 5e4125d | 2015-08-26 10:01:32 -0600 | [diff] [blame] | 1760 | err = vkCreateDynamicBlendState(demo->device, |
| 1761 | &blend, &demo->dynamic_blend); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1762 | assert(!err); |
| 1763 | |
Cody Northrop | 5e4125d | 2015-08-26 10:01:32 -0600 | [diff] [blame] | 1764 | err = vkCreateDynamicDepthBoundsState(demo->device, |
| 1765 | &depth_bounds, &demo->dynamic_depth_bounds); |
Cody Northrop | 36a642f | 2015-08-18 15:21:16 -0600 | [diff] [blame] | 1766 | assert(!err); |
| 1767 | |
| 1768 | err = vkCreateDynamicStencilState(demo->device, |
| 1769 | &stencil, &stencil, &demo->dynamic_stencil); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1770 | assert(!err); |
| 1771 | } |
| 1772 | |
Chia-I Wu | 63ea926 | 2015-03-26 13:14:16 +0800 | [diff] [blame] | 1773 | static void demo_prepare_descriptor_pool(struct demo *demo) |
Chia-I Wu | 6a3c897 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 1774 | { |
Courtney Goeltzenleuchter | 78351a6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 1775 | const VkDescriptorTypeCount type_counts[2] = { |
Chia-I Wu | 6a3c897 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 1776 | [0] = { |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1777 | .type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, |
Chia-I Wu | 6a3c897 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 1778 | .count = 1, |
| 1779 | }, |
| 1780 | [1] = { |
Courtney Goeltzenleuchter | 42a078d | 2015-04-15 15:29:59 -0600 | [diff] [blame] | 1781 | .type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, |
Chia-I Wu | 6a3c897 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 1782 | .count = DEMO_TEXTURE_COUNT, |
| 1783 | }, |
| 1784 | }; |
Courtney Goeltzenleuchter | 78351a6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 1785 | const VkDescriptorPoolCreateInfo descriptor_pool = { |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1786 | .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO, |
Chia-I Wu | 6a3c897 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 1787 | .pNext = NULL, |
| 1788 | .count = 2, |
| 1789 | .pTypeCount = type_counts, |
| 1790 | }; |
Tony Barbour | fdc2d35 | 2015-04-22 09:02:32 -0600 | [diff] [blame] | 1791 | VkResult U_ASSERT_ONLY err; |
Chia-I Wu | 6a3c897 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 1792 | |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1793 | err = vkCreateDescriptorPool(demo->device, |
| 1794 | VK_DESCRIPTOR_POOL_USAGE_ONE_SHOT, 1, |
Chia-I Wu | 63ea926 | 2015-03-26 13:14:16 +0800 | [diff] [blame] | 1795 | &descriptor_pool, &demo->desc_pool); |
Chia-I Wu | 6a3c897 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 1796 | assert(!err); |
| 1797 | } |
| 1798 | |
| 1799 | static void demo_prepare_descriptor_set(struct demo *demo) |
| 1800 | { |
Chia-I Wu | ae721ba | 2015-05-25 16:27:55 +0800 | [diff] [blame] | 1801 | VkDescriptorInfo tex_descs[DEMO_TEXTURE_COUNT]; |
| 1802 | VkWriteDescriptorSet writes[2]; |
Tony Barbour | fdc2d35 | 2015-04-22 09:02:32 -0600 | [diff] [blame] | 1803 | VkResult U_ASSERT_ONLY err; |
Mark Lobodzinski | c4fffc7 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 1804 | uint32_t i; |
Chia-I Wu | 6a3c897 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 1805 | |
Mike Stroyan | ebae832 | 2015-04-17 12:36:38 -0600 | [diff] [blame] | 1806 | err = vkAllocDescriptorSets(demo->device, demo->desc_pool, |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1807 | VK_DESCRIPTOR_SET_USAGE_STATIC, |
Chia-I Wu | 87544e7 | 2015-02-23 10:41:08 -0700 | [diff] [blame] | 1808 | 1, &demo->desc_layout, |
Cody Northrop | 1595469 | 2015-08-03 12:47:29 -0600 | [diff] [blame] | 1809 | &demo->desc_set); |
| 1810 | assert(!err); |
Chia-I Wu | 6a3c897 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 1811 | |
Chia-I Wu | ae721ba | 2015-05-25 16:27:55 +0800 | [diff] [blame] | 1812 | memset(&tex_descs, 0, sizeof(tex_descs)); |
| 1813 | for (i = 0; i < DEMO_TEXTURE_COUNT; i++) { |
| 1814 | tex_descs[i].sampler = demo->textures[i].sampler; |
| 1815 | tex_descs[i].imageView = demo->textures[i].view; |
| 1816 | tex_descs[i].imageLayout = VK_IMAGE_LAYOUT_GENERAL; |
| 1817 | } |
| 1818 | |
| 1819 | memset(&writes, 0, sizeof(writes)); |
| 1820 | |
| 1821 | writes[0].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET; |
| 1822 | writes[0].destSet = demo->desc_set; |
| 1823 | writes[0].count = 1; |
| 1824 | writes[0].descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER; |
| 1825 | writes[0].pDescriptors = &demo->uniform_data.desc; |
| 1826 | |
| 1827 | writes[1].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET; |
| 1828 | writes[1].destSet = demo->desc_set; |
| 1829 | writes[1].destBinding = 1; |
| 1830 | writes[1].count = DEMO_TEXTURE_COUNT; |
| 1831 | writes[1].descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER; |
| 1832 | writes[1].pDescriptors = tex_descs; |
| 1833 | |
Mark Lobodzinski | e4c92a6 | 2015-09-07 13:59:43 -0600 | [diff] [blame] | 1834 | vkUpdateDescriptorSets(demo->device, 2, writes, 0, NULL); |
Chia-I Wu | 6a3c897 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 1835 | } |
| 1836 | |
Chia-I Wu | f973e32 | 2015-07-08 13:34:24 +0800 | [diff] [blame] | 1837 | static void demo_prepare_framebuffers(struct demo *demo) |
| 1838 | { |
Courtney Goeltzenleuchter | 41866db | 2015-09-01 17:30:39 -0600 | [diff] [blame] | 1839 | VkImageView attachments[2]; |
Cody Northrop | 2e11cad | 2015-08-04 10:47:08 -0600 | [diff] [blame] | 1840 | attachments[1] = demo->depth.view; |
| 1841 | |
Chia-I Wu | f973e32 | 2015-07-08 13:34:24 +0800 | [diff] [blame] | 1842 | const VkFramebufferCreateInfo fb_info = { |
| 1843 | .sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO, |
| 1844 | .pNext = NULL, |
Chia-I Wu | a74c5b2 | 2015-07-07 11:50:03 +0800 | [diff] [blame] | 1845 | .renderPass = demo->render_pass, |
| 1846 | .attachmentCount = 2, |
| 1847 | .pAttachments = attachments, |
Chia-I Wu | f973e32 | 2015-07-08 13:34:24 +0800 | [diff] [blame] | 1848 | .width = demo->width, |
| 1849 | .height = demo->height, |
| 1850 | .layers = 1, |
| 1851 | }; |
| 1852 | VkResult U_ASSERT_ONLY err; |
| 1853 | uint32_t i; |
| 1854 | |
| 1855 | for (i = 0; i < DEMO_BUFFER_COUNT; i++) { |
Cody Northrop | 2e11cad | 2015-08-04 10:47:08 -0600 | [diff] [blame] | 1856 | attachments[0] = demo->buffers[i].view; |
Chia-I Wu | f973e32 | 2015-07-08 13:34:24 +0800 | [diff] [blame] | 1857 | err = vkCreateFramebuffer(demo->device, &fb_info, &demo->framebuffers[i]); |
| 1858 | assert(!err); |
| 1859 | } |
| 1860 | } |
| 1861 | |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1862 | static void demo_prepare(struct demo *demo) |
| 1863 | { |
Cody Northrop | 91e221b | 2015-07-09 18:08:32 -0600 | [diff] [blame] | 1864 | VkResult U_ASSERT_ONLY err; |
| 1865 | |
| 1866 | const VkCmdPoolCreateInfo cmd_pool_info = { |
| 1867 | .sType = VK_STRUCTURE_TYPE_CMD_POOL_CREATE_INFO, |
| 1868 | .pNext = NULL, |
| 1869 | .queueFamilyIndex = demo->graphics_queue_node_index, |
| 1870 | .flags = 0, |
| 1871 | }; |
| 1872 | err = vkCreateCommandPool(demo->device, &cmd_pool_info, &demo->cmd_pool); |
| 1873 | assert(!err); |
| 1874 | |
Courtney Goeltzenleuchter | 78351a6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 1875 | const VkCmdBufferCreateInfo cmd = { |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1876 | .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO, |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1877 | .pNext = NULL, |
Cody Northrop | 91e221b | 2015-07-09 18:08:32 -0600 | [diff] [blame] | 1878 | .cmdPool = demo->cmd_pool, |
Chia-I Wu | 57b23b4 | 2015-06-26 15:34:39 +0800 | [diff] [blame] | 1879 | .level = VK_CMD_BUFFER_LEVEL_PRIMARY, |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1880 | .flags = 0, |
| 1881 | }; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1882 | |
| 1883 | demo_prepare_buffers(demo); |
| 1884 | demo_prepare_depth(demo); |
| 1885 | demo_prepare_textures(demo); |
Courtney Goeltzenleuchter | 8879d3a | 2014-10-29 08:29:35 -0600 | [diff] [blame] | 1886 | demo_prepare_cube_data_buffer(demo); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1887 | |
Chia-I Wu | 6a3c897 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 1888 | demo_prepare_descriptor_layout(demo); |
Chia-I Wu | f973e32 | 2015-07-08 13:34:24 +0800 | [diff] [blame] | 1889 | demo_prepare_render_pass(demo); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1890 | demo_prepare_pipeline(demo); |
| 1891 | demo_prepare_dynamic_states(demo); |
| 1892 | |
Ian Elliott | a078197 | 2015-08-21 15:09:33 -0600 | [diff] [blame] | 1893 | for (uint32_t i = 0; i < demo->swapchainImageCount; i++) { |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1894 | err = vkCreateCommandBuffer(demo->device, &cmd, &demo->buffers[i].cmd); |
Courtney Goeltzenleuchter | efc58ae | 2015-02-17 09:48:44 -0700 | [diff] [blame] | 1895 | assert(!err); |
| 1896 | } |
Chia-I Wu | 6a3c897 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 1897 | |
Chia-I Wu | 63ea926 | 2015-03-26 13:14:16 +0800 | [diff] [blame] | 1898 | demo_prepare_descriptor_pool(demo); |
Chia-I Wu | 6a3c897 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 1899 | demo_prepare_descriptor_set(demo); |
Courtney Goeltzenleuchter | efc58ae | 2015-02-17 09:48:44 -0700 | [diff] [blame] | 1900 | |
Chia-I Wu | f973e32 | 2015-07-08 13:34:24 +0800 | [diff] [blame] | 1901 | demo_prepare_framebuffers(demo); |
Courtney Goeltzenleuchter | efc58ae | 2015-02-17 09:48:44 -0700 | [diff] [blame] | 1902 | |
Ian Elliott | a078197 | 2015-08-21 15:09:33 -0600 | [diff] [blame] | 1903 | for (uint32_t i = 0; i < demo->swapchainImageCount; i++) { |
Courtney Goeltzenleuchter | efc58ae | 2015-02-17 09:48:44 -0700 | [diff] [blame] | 1904 | demo->current_buffer = i; |
| 1905 | demo_draw_build_cmd(demo, demo->buffers[i].cmd); |
| 1906 | } |
| 1907 | |
Courtney Goeltzenleuchter | 752eb8e | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 1908 | /* |
| 1909 | * Prepare functions above may generate pipeline commands |
| 1910 | * that need to be flushed before beginning the render loop. |
| 1911 | */ |
| 1912 | demo_flush_init_cmd(demo); |
| 1913 | |
Courtney Goeltzenleuchter | efc58ae | 2015-02-17 09:48:44 -0700 | [diff] [blame] | 1914 | demo->current_buffer = 0; |
Jon Ashburn | 8d26c06 | 2015-04-24 09:46:24 -0700 | [diff] [blame] | 1915 | demo->prepared = true; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1916 | } |
| 1917 | |
David Pinedo | 2bb7c93 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 1918 | static void demo_cleanup(struct demo *demo) |
| 1919 | { |
| 1920 | uint32_t i; |
| 1921 | |
| 1922 | demo->prepared = false; |
| 1923 | |
Tony Barbour | 155cce8 | 2015-07-03 10:33:54 -0600 | [diff] [blame] | 1924 | for (i = 0; i < DEMO_BUFFER_COUNT; i++) { |
| 1925 | vkDestroyFramebuffer(demo->device, demo->framebuffers[i]); |
| 1926 | } |
Tony Barbour | 4efb01f | 2015-07-10 10:50:45 -0600 | [diff] [blame] | 1927 | vkFreeDescriptorSets(demo->device, demo->desc_pool, 1, &demo->desc_set); |
Tony Barbour | 155cce8 | 2015-07-03 10:33:54 -0600 | [diff] [blame] | 1928 | vkDestroyDescriptorPool(demo->device, demo->desc_pool); |
Chia-I Wu | f973e32 | 2015-07-08 13:34:24 +0800 | [diff] [blame] | 1929 | |
Cody Northrop | 5e4125d | 2015-08-26 10:01:32 -0600 | [diff] [blame] | 1930 | vkDestroyDynamicViewportState(demo->device, demo->dynamic_viewport); |
| 1931 | vkDestroyDynamicLineWidthState(demo->device, demo->dynamic_line_width); |
| 1932 | vkDestroyDynamicDepthBiasState(demo->device, demo->dynamic_depth_bias); |
| 1933 | vkDestroyDynamicBlendState(demo->device, demo->dynamic_blend); |
| 1934 | vkDestroyDynamicDepthBoundsState(demo->device, demo->dynamic_depth_bounds); |
Cody Northrop | 36a642f | 2015-08-18 15:21:16 -0600 | [diff] [blame] | 1935 | vkDestroyDynamicStencilState(demo->device, demo->dynamic_stencil); |
David Pinedo | 2bb7c93 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 1936 | |
Tony Barbour | 155cce8 | 2015-07-03 10:33:54 -0600 | [diff] [blame] | 1937 | vkDestroyPipeline(demo->device, demo->pipeline); |
Jon Ashburn | c4ab7af | 2015-07-09 15:02:25 -0600 | [diff] [blame] | 1938 | vkDestroyPipelineCache(demo->device, demo->pipelineCache); |
Tony Barbour | 155cce8 | 2015-07-03 10:33:54 -0600 | [diff] [blame] | 1939 | vkDestroyRenderPass(demo->device, demo->render_pass); |
| 1940 | vkDestroyPipelineLayout(demo->device, demo->pipeline_layout); |
| 1941 | vkDestroyDescriptorSetLayout(demo->device, demo->desc_layout); |
David Pinedo | 2bb7c93 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 1942 | |
| 1943 | for (i = 0; i < DEMO_TEXTURE_COUNT; i++) { |
Tony Barbour | 155cce8 | 2015-07-03 10:33:54 -0600 | [diff] [blame] | 1944 | vkDestroyImageView(demo->device, demo->textures[i].view); |
| 1945 | vkDestroyImage(demo->device, demo->textures[i].image); |
David Pinedo | 2bb7c93 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 1946 | vkFreeMemory(demo->device, demo->textures[i].mem); |
Tony Barbour | 155cce8 | 2015-07-03 10:33:54 -0600 | [diff] [blame] | 1947 | vkDestroySampler(demo->device, demo->textures[i].sampler); |
David Pinedo | 2bb7c93 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 1948 | } |
Ian Elliott | a078197 | 2015-08-21 15:09:33 -0600 | [diff] [blame] | 1949 | demo->fpDestroySwapchainKHR(demo->device, demo->swap_chain); |
David Pinedo | 2bb7c93 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 1950 | |
Courtney Goeltzenleuchter | 41866db | 2015-09-01 17:30:39 -0600 | [diff] [blame] | 1951 | vkDestroyImageView(demo->device, demo->depth.view); |
Tony Barbour | 155cce8 | 2015-07-03 10:33:54 -0600 | [diff] [blame] | 1952 | vkDestroyImage(demo->device, demo->depth.image); |
David Pinedo | 2bb7c93 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 1953 | vkFreeMemory(demo->device, demo->depth.mem); |
| 1954 | |
Tony Barbour | 155cce8 | 2015-07-03 10:33:54 -0600 | [diff] [blame] | 1955 | vkDestroyBufferView(demo->device, demo->uniform_data.view); |
| 1956 | vkDestroyBuffer(demo->device, demo->uniform_data.buf); |
David Pinedo | 2bb7c93 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 1957 | vkFreeMemory(demo->device, demo->uniform_data.mem); |
| 1958 | |
Ian Elliott | a078197 | 2015-08-21 15:09:33 -0600 | [diff] [blame] | 1959 | for (i = 0; i < demo->swapchainImageCount; i++) { |
Courtney Goeltzenleuchter | 41866db | 2015-09-01 17:30:39 -0600 | [diff] [blame] | 1960 | vkDestroyImageView(demo->device, demo->buffers[i].view); |
Tony Barbour | 155cce8 | 2015-07-03 10:33:54 -0600 | [diff] [blame] | 1961 | vkDestroyCommandBuffer(demo->device, demo->buffers[i].cmd); |
David Pinedo | 2bb7c93 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 1962 | } |
Ian Elliott | aaae535 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 1963 | free(demo->buffers); |
David Pinedo | 2bb7c93 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 1964 | |
Cody Northrop | 91e221b | 2015-07-09 18:08:32 -0600 | [diff] [blame] | 1965 | vkDestroyCommandPool(demo->device, demo->cmd_pool); |
David Pinedo | 2bb7c93 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 1966 | vkDestroyDevice(demo->device); |
Tony Barbour | d70b42c | 2015-06-30 14:14:19 -0600 | [diff] [blame] | 1967 | if (demo->validate) { |
| 1968 | demo->dbgDestroyMsgCallback(demo->inst, demo->msg_callback); |
| 1969 | } |
David Pinedo | 2bb7c93 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 1970 | vkDestroyInstance(demo->inst); |
| 1971 | |
| 1972 | #ifndef _WIN32 |
| 1973 | xcb_destroy_window(demo->connection, demo->window); |
| 1974 | xcb_disconnect(demo->connection); |
| 1975 | #endif // _WIN32 |
| 1976 | } |
| 1977 | |
| 1978 | // On MS-Windows, make this a global, so it's available to WndProc() |
| 1979 | struct demo demo; |
| 1980 | |
Ian Elliott | 639ca47 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 1981 | #ifdef _WIN32 |
| 1982 | static void demo_run(struct demo *demo) |
| 1983 | { |
Courtney Goeltzenleuchter | b7e2270 | 2015-04-27 14:56:34 -0600 | [diff] [blame] | 1984 | if (!demo->prepared) |
| 1985 | return; |
Ian Elliott | 639ca47 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 1986 | // Wait for work to finish before updating MVP. |
| 1987 | vkDeviceWaitIdle(demo->device); |
| 1988 | demo_update_data_buffer(demo); |
| 1989 | |
| 1990 | demo_draw(demo); |
| 1991 | |
| 1992 | // Wait for work to finish before updating MVP. |
| 1993 | vkDeviceWaitIdle(demo->device); |
Ian Elliott | 639ca47 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 1994 | |
David Pinedo | 2bb7c93 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 1995 | demo->curFrame++; |
| 1996 | |
| 1997 | if (demo->frameCount != INT_MAX && demo->curFrame == demo->frameCount) |
| 1998 | { |
| 1999 | demo->quit=true; |
| 2000 | demo_cleanup(demo); |
| 2001 | ExitProcess(0); |
| 2002 | } |
| 2003 | |
| 2004 | } |
Ian Elliott | 639ca47 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 2005 | |
| 2006 | // MS-Windows event handling function: |
| 2007 | LRESULT CALLBACK WndProc(HWND hWnd, |
| 2008 | UINT uMsg, |
| 2009 | WPARAM wParam, |
| 2010 | LPARAM lParam) |
| 2011 | { |
Ian Elliott | 639ca47 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 2012 | switch(uMsg) |
| 2013 | { |
Ian Elliott | aaae535 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 2014 | case WM_CLOSE: |
Ian Elliott | 639ca47 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 2015 | PostQuitMessage(0); |
Tony Barbour | 3dddd5d | 2015-04-29 16:19:20 -0600 | [diff] [blame] | 2016 | break; |
Ian Elliott | aaae535 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 2017 | case WM_PAINT: |
Ian Elliott | 639ca47 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 2018 | demo_run(&demo); |
| 2019 | return 0; |
| 2020 | default: |
| 2021 | break; |
| 2022 | } |
| 2023 | return (DefWindowProc(hWnd, uMsg, wParam, lParam)); |
| 2024 | } |
| 2025 | |
| 2026 | static void demo_create_window(struct demo *demo) |
| 2027 | { |
| 2028 | WNDCLASSEX win_class; |
| 2029 | |
| 2030 | // Initialize the window class structure: |
| 2031 | win_class.cbSize = sizeof(WNDCLASSEX); |
| 2032 | win_class.style = CS_HREDRAW | CS_VREDRAW; |
| 2033 | win_class.lpfnWndProc = WndProc; |
| 2034 | win_class.cbClsExtra = 0; |
| 2035 | win_class.cbWndExtra = 0; |
| 2036 | win_class.hInstance = demo->connection; // hInstance |
| 2037 | win_class.hIcon = LoadIcon(NULL, IDI_APPLICATION); |
| 2038 | win_class.hCursor = LoadCursor(NULL, IDC_ARROW); |
| 2039 | win_class.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); |
| 2040 | win_class.lpszMenuName = NULL; |
| 2041 | win_class.lpszClassName = demo->name; |
| 2042 | win_class.hIconSm = LoadIcon(NULL, IDI_WINLOGO); |
| 2043 | // Register window class: |
| 2044 | if (!RegisterClassEx(&win_class)) { |
| 2045 | // It didn't work, so try to give a useful error: |
| 2046 | printf("Unexpected error trying to start the application!\n"); |
| 2047 | fflush(stdout); |
| 2048 | exit(1); |
| 2049 | } |
| 2050 | // Create window with the registered class: |
Mike Stroyan | b46779e | 2015-06-15 14:20:13 -0600 | [diff] [blame] | 2051 | RECT wr = { 0, 0, demo->width, demo->height }; |
| 2052 | AdjustWindowRect(&wr, WS_OVERLAPPEDWINDOW, FALSE); |
Ian Elliott | 639ca47 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 2053 | demo->window = CreateWindowEx(0, |
| 2054 | demo->name, // class name |
| 2055 | demo->name, // app name |
| 2056 | WS_OVERLAPPEDWINDOW | // window style |
| 2057 | WS_VISIBLE | |
| 2058 | WS_SYSMENU, |
| 2059 | 100,100, // x/y coords |
Mike Stroyan | b46779e | 2015-06-15 14:20:13 -0600 | [diff] [blame] | 2060 | wr.right-wr.left, // width |
| 2061 | wr.bottom-wr.top, // height |
Ian Elliott | 639ca47 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 2062 | NULL, // handle to parent |
| 2063 | NULL, // handle to menu |
| 2064 | demo->connection, // hInstance |
| 2065 | NULL); // no extra parameters |
| 2066 | if (!demo->window) { |
| 2067 | // It didn't work, so try to give a useful error: |
| 2068 | printf("Cannot create a window in which to draw!\n"); |
| 2069 | fflush(stdout); |
| 2070 | exit(1); |
| 2071 | } |
| 2072 | } |
| 2073 | #else // _WIN32 |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 2074 | static void demo_handle_event(struct demo *demo, |
| 2075 | const xcb_generic_event_t *event) |
| 2076 | { |
Piers Daniell | 735ee53 | 2015-02-23 16:23:13 -0700 | [diff] [blame] | 2077 | uint8_t event_code = event->response_type & 0x7f; |
Courtney Goeltzenleuchter | 98cb2cb | 2014-11-06 14:27:52 -0700 | [diff] [blame] | 2078 | switch (event_code) { |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 2079 | case XCB_EXPOSE: |
Courtney Goeltzenleuchter | c465d6c | 2014-11-18 11:28:09 -0700 | [diff] [blame] | 2080 | // TODO: Resize window |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 2081 | break; |
Courtney Goeltzenleuchter | 98cb2cb | 2014-11-06 14:27:52 -0700 | [diff] [blame] | 2082 | case XCB_CLIENT_MESSAGE: |
| 2083 | if((*(xcb_client_message_event_t*)event).data.data32[0] == |
| 2084 | (*demo->atom_wm_delete_window).atom) { |
| 2085 | demo->quit = true; |
| 2086 | } |
| 2087 | break; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 2088 | case XCB_KEY_RELEASE: |
| 2089 | { |
| 2090 | const xcb_key_release_event_t *key = |
| 2091 | (const xcb_key_release_event_t *) event; |
| 2092 | |
Courtney Goeltzenleuchter | fc3b953 | 2014-10-28 10:32:57 -0600 | [diff] [blame] | 2093 | switch (key->detail) { |
| 2094 | case 0x9: // Escape |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 2095 | demo->quit = true; |
Courtney Goeltzenleuchter | fc3b953 | 2014-10-28 10:32:57 -0600 | [diff] [blame] | 2096 | break; |
Courtney Goeltzenleuchter | 4984d15 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 2097 | case 0x71: // left arrow key |
Courtney Goeltzenleuchter | 8879d3a | 2014-10-29 08:29:35 -0600 | [diff] [blame] | 2098 | demo->spin_angle += demo->spin_increment; |
Courtney Goeltzenleuchter | 4984d15 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 2099 | break; |
Courtney Goeltzenleuchter | fc3b953 | 2014-10-28 10:32:57 -0600 | [diff] [blame] | 2100 | case 0x72: // right arrow key |
Courtney Goeltzenleuchter | 8879d3a | 2014-10-29 08:29:35 -0600 | [diff] [blame] | 2101 | demo->spin_angle -= demo->spin_increment; |
Courtney Goeltzenleuchter | fc3b953 | 2014-10-28 10:32:57 -0600 | [diff] [blame] | 2102 | break; |
Courtney Goeltzenleuchter | 8879d3a | 2014-10-29 08:29:35 -0600 | [diff] [blame] | 2103 | case 0x41: |
| 2104 | demo->pause = !demo->pause; |
Piers Daniell | 735ee53 | 2015-02-23 16:23:13 -0700 | [diff] [blame] | 2105 | break; |
Courtney Goeltzenleuchter | fc3b953 | 2014-10-28 10:32:57 -0600 | [diff] [blame] | 2106 | } |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 2107 | } |
| 2108 | break; |
| 2109 | default: |
| 2110 | break; |
| 2111 | } |
| 2112 | } |
| 2113 | |
| 2114 | static void demo_run(struct demo *demo) |
| 2115 | { |
| 2116 | xcb_flush(demo->connection); |
| 2117 | |
| 2118 | while (!demo->quit) { |
| 2119 | xcb_generic_event_t *event; |
| 2120 | |
Courtney Goeltzenleuchter | c465d6c | 2014-11-18 11:28:09 -0700 | [diff] [blame] | 2121 | if (demo->pause) { |
| 2122 | event = xcb_wait_for_event(demo->connection); |
| 2123 | } else { |
| 2124 | event = xcb_poll_for_event(demo->connection); |
| 2125 | } |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 2126 | if (event) { |
| 2127 | demo_handle_event(demo, event); |
| 2128 | free(event); |
Courtney Goeltzenleuchter | 9808555 | 2014-11-10 11:13:13 -0700 | [diff] [blame] | 2129 | } |
Courtney Goeltzenleuchter | c465d6c | 2014-11-18 11:28:09 -0700 | [diff] [blame] | 2130 | |
| 2131 | // Wait for work to finish before updating MVP. |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 2132 | vkDeviceWaitIdle(demo->device); |
Courtney Goeltzenleuchter | c465d6c | 2014-11-18 11:28:09 -0700 | [diff] [blame] | 2133 | demo_update_data_buffer(demo); |
| 2134 | |
Courtney Goeltzenleuchter | 9808555 | 2014-11-10 11:13:13 -0700 | [diff] [blame] | 2135 | demo_draw(demo); |
Courtney Goeltzenleuchter | 1454f3c | 2014-11-18 11:28:09 -0700 | [diff] [blame] | 2136 | |
Courtney Goeltzenleuchter | 9808555 | 2014-11-10 11:13:13 -0700 | [diff] [blame] | 2137 | // Wait for work to finish before updating MVP. |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 2138 | vkDeviceWaitIdle(demo->device); |
David Pinedo | 2bb7c93 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 2139 | demo->curFrame++; |
| 2140 | if (demo->frameCount != INT_MAX && demo->curFrame == demo->frameCount) |
| 2141 | demo->quit = true; |
| 2142 | |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 2143 | } |
| 2144 | } |
| 2145 | |
| 2146 | static void demo_create_window(struct demo *demo) |
| 2147 | { |
| 2148 | uint32_t value_mask, value_list[32]; |
| 2149 | |
| 2150 | demo->window = xcb_generate_id(demo->connection); |
| 2151 | |
| 2152 | value_mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK; |
| 2153 | value_list[0] = demo->screen->black_pixel; |
| 2154 | value_list[1] = XCB_EVENT_MASK_KEY_RELEASE | |
| 2155 | XCB_EVENT_MASK_EXPOSURE; |
| 2156 | |
| 2157 | xcb_create_window(demo->connection, |
| 2158 | XCB_COPY_FROM_PARENT, |
| 2159 | demo->window, demo->screen->root, |
| 2160 | 0, 0, demo->width, demo->height, 0, |
| 2161 | XCB_WINDOW_CLASS_INPUT_OUTPUT, |
| 2162 | demo->screen->root_visual, |
| 2163 | value_mask, value_list); |
| 2164 | |
Courtney Goeltzenleuchter | 98cb2cb | 2014-11-06 14:27:52 -0700 | [diff] [blame] | 2165 | /* Magic code that will send notification when window is destroyed */ |
| 2166 | xcb_intern_atom_cookie_t cookie = xcb_intern_atom(demo->connection, 1, 12, |
| 2167 | "WM_PROTOCOLS"); |
| 2168 | xcb_intern_atom_reply_t* reply = xcb_intern_atom_reply(demo->connection, cookie, 0); |
| 2169 | |
| 2170 | xcb_intern_atom_cookie_t cookie2 = xcb_intern_atom(demo->connection, 0, 16, "WM_DELETE_WINDOW"); |
| 2171 | demo->atom_wm_delete_window = xcb_intern_atom_reply(demo->connection, cookie2, 0); |
| 2172 | |
| 2173 | xcb_change_property(demo->connection, XCB_PROP_MODE_REPLACE, |
| 2174 | demo->window, (*reply).atom, 4, 32, 1, |
| 2175 | &(*demo->atom_wm_delete_window).atom); |
| 2176 | free(reply); |
| 2177 | |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 2178 | xcb_map_window(demo->connection, demo->window); |
David Pinedo | 2bb7c93 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 2179 | |
| 2180 | // Force the x/y coordinates to 100,100 results are identical in consecutive runs |
| 2181 | const uint32_t coords[] = {100, 100}; |
| 2182 | xcb_configure_window(demo->connection, demo->window, |
| 2183 | XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y, coords); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 2184 | } |
Ian Elliott | 639ca47 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 2185 | #endif // _WIN32 |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 2186 | |
Courtney Goeltzenleuchter | 886f76c | 2015-07-06 17:46:11 -0600 | [diff] [blame] | 2187 | /* |
| 2188 | * Return 1 (true) if all layer names specified in check_names |
| 2189 | * can be found in given layer properties. |
| 2190 | */ |
Courtney Goeltzenleuchter | c88ce51 | 2015-07-09 11:44:38 -0600 | [diff] [blame] | 2191 | static VkBool32 demo_check_layers(uint32_t check_count, char **check_names, |
Courtney Goeltzenleuchter | 886f76c | 2015-07-06 17:46:11 -0600 | [diff] [blame] | 2192 | uint32_t layer_count, VkLayerProperties *layers) |
| 2193 | { |
| 2194 | for (uint32_t i = 0; i < check_count; i++) { |
Courtney Goeltzenleuchter | c88ce51 | 2015-07-09 11:44:38 -0600 | [diff] [blame] | 2195 | VkBool32 found = 0; |
Courtney Goeltzenleuchter | 886f76c | 2015-07-06 17:46:11 -0600 | [diff] [blame] | 2196 | for (uint32_t j = 0; j < layer_count; j++) { |
| 2197 | if (!strcmp(check_names[i], layers[j].layerName)) { |
| 2198 | found = 1; |
| 2199 | } |
| 2200 | } |
| 2201 | if (!found) { |
| 2202 | fprintf(stderr, "Cannot find layer: %s\n", check_names[i]); |
| 2203 | return 0; |
| 2204 | } |
| 2205 | } |
| 2206 | return 1; |
| 2207 | } |
| 2208 | |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 2209 | static void demo_init_vk(struct demo *demo) |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 2210 | { |
Tobin Ehlis | 99239dc | 2015-04-16 18:04:57 -0600 | [diff] [blame] | 2211 | VkResult err; |
Courtney Goeltzenleuchter | be10336 | 2015-06-29 15:39:26 -0600 | [diff] [blame] | 2212 | char *extension_names[64]; |
Courtney Goeltzenleuchter | 922a0fa | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 2213 | VkExtensionProperties *instance_extensions; |
Tobin Ehlis | 8a724d8 | 2015-09-07 15:16:39 -0600 | [diff] [blame] | 2214 | VkPhysicalDevice *physical_devices; |
Courtney Goeltzenleuchter | be10336 | 2015-06-29 15:39:26 -0600 | [diff] [blame] | 2215 | VkLayerProperties *instance_layers; |
| 2216 | VkLayerProperties *device_layers; |
Courtney Goeltzenleuchter | 922a0fa | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 2217 | uint32_t instance_extension_count = 0; |
Courtney Goeltzenleuchter | be10336 | 2015-06-29 15:39:26 -0600 | [diff] [blame] | 2218 | uint32_t instance_layer_count = 0; |
| 2219 | uint32_t enabled_extension_count = 0; |
| 2220 | uint32_t enabled_layer_count = 0; |
| 2221 | |
Courtney Goeltzenleuchter | 886f76c | 2015-07-06 17:46:11 -0600 | [diff] [blame] | 2222 | char *instance_validation_layers[] = { |
Tony Barbour | b6dba5c | 2015-07-21 09:04:41 -0600 | [diff] [blame] | 2223 | "Threading", |
Courtney Goeltzenleuchter | 886f76c | 2015-07-06 17:46:11 -0600 | [diff] [blame] | 2224 | "MemTracker", |
Tony Barbour | b6dba5c | 2015-07-21 09:04:41 -0600 | [diff] [blame] | 2225 | "ObjectTracker", |
| 2226 | "DrawState", |
| 2227 | "ParamChecker", |
| 2228 | "ShaderChecker", |
Courtney Goeltzenleuchter | 886f76c | 2015-07-06 17:46:11 -0600 | [diff] [blame] | 2229 | }; |
| 2230 | |
| 2231 | char *device_validation_layers[] = { |
Tony Barbour | b6dba5c | 2015-07-21 09:04:41 -0600 | [diff] [blame] | 2232 | "Threading", |
Courtney Goeltzenleuchter | 886f76c | 2015-07-06 17:46:11 -0600 | [diff] [blame] | 2233 | "MemTracker", |
Tony Barbour | b6dba5c | 2015-07-21 09:04:41 -0600 | [diff] [blame] | 2234 | "ObjectTracker", |
| 2235 | "DrawState", |
| 2236 | "ParamChecker", |
| 2237 | "ShaderChecker", |
Courtney Goeltzenleuchter | 886f76c | 2015-07-06 17:46:11 -0600 | [diff] [blame] | 2238 | }; |
| 2239 | |
Courtney Goeltzenleuchter | be10336 | 2015-06-29 15:39:26 -0600 | [diff] [blame] | 2240 | /* Look for validation layers */ |
Courtney Goeltzenleuchter | c88ce51 | 2015-07-09 11:44:38 -0600 | [diff] [blame] | 2241 | VkBool32 validation_found = 0; |
Courtney Goeltzenleuchter | be10336 | 2015-06-29 15:39:26 -0600 | [diff] [blame] | 2242 | err = vkGetGlobalLayerProperties(&instance_layer_count, NULL); |
Tobin Ehlis | 99239dc | 2015-04-16 18:04:57 -0600 | [diff] [blame] | 2243 | assert(!err); |
| 2244 | |
Courtney Goeltzenleuchter | be10336 | 2015-06-29 15:39:26 -0600 | [diff] [blame] | 2245 | instance_layers = malloc(sizeof(VkLayerProperties) * instance_layer_count); |
| 2246 | err = vkGetGlobalLayerProperties(&instance_layer_count, instance_layers); |
| 2247 | assert(!err); |
Courtney Goeltzenleuchter | 886f76c | 2015-07-06 17:46:11 -0600 | [diff] [blame] | 2248 | |
| 2249 | if (demo->validate) { |
| 2250 | validation_found = demo_check_layers(ARRAY_SIZE(instance_validation_layers), instance_validation_layers, |
| 2251 | instance_layer_count, instance_layers); |
| 2252 | if (!validation_found) { |
| 2253 | ERR_EXIT("vkGetGlobalLayerProperties failed to find" |
| 2254 | "required validation layer.\n\n" |
| 2255 | "Please look at the Getting Started guide for additional " |
| 2256 | "information.\n", |
| 2257 | "vkCreateInstance Failure"); |
Courtney Goeltzenleuchter | be10336 | 2015-06-29 15:39:26 -0600 | [diff] [blame] | 2258 | } |
Courtney Goeltzenleuchter | 886f76c | 2015-07-06 17:46:11 -0600 | [diff] [blame] | 2259 | enabled_layer_count = ARRAY_SIZE(instance_validation_layers); |
Courtney Goeltzenleuchter | be10336 | 2015-06-29 15:39:26 -0600 | [diff] [blame] | 2260 | } |
| 2261 | |
| 2262 | err = vkGetGlobalExtensionProperties(NULL, &instance_extension_count, NULL); |
| 2263 | assert(!err); |
| 2264 | |
Courtney Goeltzenleuchter | c88ce51 | 2015-07-09 11:44:38 -0600 | [diff] [blame] | 2265 | VkBool32 WSIextFound = 0; |
Courtney Goeltzenleuchter | be10336 | 2015-06-29 15:39:26 -0600 | [diff] [blame] | 2266 | memset(extension_names, 0, sizeof(extension_names)); |
| 2267 | instance_extensions = malloc(sizeof(VkExtensionProperties) * instance_extension_count); |
| 2268 | err = vkGetGlobalExtensionProperties(NULL, &instance_extension_count, instance_extensions); |
| 2269 | assert(!err); |
| 2270 | for (uint32_t i = 0; i < instance_extension_count; i++) { |
Ian Elliott | a078197 | 2015-08-21 15:09:33 -0600 | [diff] [blame] | 2271 | if (!strcmp("VK_EXT_KHR_swapchain", instance_extensions[i].extName)) { |
Courtney Goeltzenleuchter | 922a0fa | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 2272 | WSIextFound = 1; |
Ian Elliott | a078197 | 2015-08-21 15:09:33 -0600 | [diff] [blame] | 2273 | extension_names[enabled_extension_count++] = "VK_EXT_KHR_swapchain"; |
Courtney Goeltzenleuchter | 922a0fa | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 2274 | } |
Courtney Goeltzenleuchter | a59efa7 | 2015-07-30 11:32:46 -0600 | [diff] [blame] | 2275 | if (!strcmp(VK_DEBUG_REPORT_EXTENSION_NAME, instance_extensions[i].extName)) { |
Courtney Goeltzenleuchter | be10336 | 2015-06-29 15:39:26 -0600 | [diff] [blame] | 2276 | if (demo->validate) { |
Courtney Goeltzenleuchter | a59efa7 | 2015-07-30 11:32:46 -0600 | [diff] [blame] | 2277 | extension_names[enabled_extension_count++] = VK_DEBUG_REPORT_EXTENSION_NAME; |
Courtney Goeltzenleuchter | be10336 | 2015-06-29 15:39:26 -0600 | [diff] [blame] | 2278 | } |
Courtney Goeltzenleuchter | 922a0fa | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 2279 | } |
Courtney Goeltzenleuchter | be10336 | 2015-06-29 15:39:26 -0600 | [diff] [blame] | 2280 | assert(enabled_extension_count < 64); |
Tobin Ehlis | 99239dc | 2015-04-16 18:04:57 -0600 | [diff] [blame] | 2281 | } |
Courtney Goeltzenleuchter | 922a0fa | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 2282 | if (!WSIextFound) { |
Tony Barbour | ecf1b2b | 2015-06-24 16:06:58 -0600 | [diff] [blame] | 2283 | ERR_EXIT("vkGetGlobalExtensionProperties failed to find the " |
Ian Elliott | a078197 | 2015-08-21 15:09:33 -0600 | [diff] [blame] | 2284 | "\"VK_EXT_KHR_swapchain\" extension.\n\nDo you have a compatible " |
Ian Elliott | 6515291 | 2015-04-28 13:22:33 -0600 | [diff] [blame] | 2285 | "Vulkan installable client driver (ICD) installed?\nPlease " |
| 2286 | "look at the Getting Started guide for additional " |
| 2287 | "information.\n", |
| 2288 | "vkCreateInstance Failure"); |
| 2289 | } |
Courtney Goeltzenleuchter | 78351a6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 2290 | const VkApplicationInfo app = { |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 2291 | .sType = VK_STRUCTURE_TYPE_APPLICATION_INFO, |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 2292 | .pNext = NULL, |
Ian Elliott | 44e33f7 | 2015-04-28 10:52:52 -0600 | [diff] [blame] | 2293 | .pAppName = APP_SHORT_NAME, |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 2294 | .appVersion = 0, |
Ian Elliott | 44e33f7 | 2015-04-28 10:52:52 -0600 | [diff] [blame] | 2295 | .pEngineName = APP_SHORT_NAME, |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 2296 | .engineVersion = 0, |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 2297 | .apiVersion = VK_API_VERSION, |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 2298 | }; |
Tony Barbour | 3dddd5d | 2015-04-29 16:19:20 -0600 | [diff] [blame] | 2299 | VkInstanceCreateInfo inst_info = { |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 2300 | .sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO, |
Jon Ashburn | ab46b36 | 2015-04-04 14:52:07 -0600 | [diff] [blame] | 2301 | .pNext = NULL, |
| 2302 | .pAppInfo = &app, |
| 2303 | .pAllocCb = NULL, |
Courtney Goeltzenleuchter | be10336 | 2015-06-29 15:39:26 -0600 | [diff] [blame] | 2304 | .layerCount = enabled_layer_count, |
Courtney Goeltzenleuchter | 886f76c | 2015-07-06 17:46:11 -0600 | [diff] [blame] | 2305 | .ppEnabledLayerNames = (const char *const*) ((demo->validate) ? instance_validation_layers : NULL), |
Courtney Goeltzenleuchter | be10336 | 2015-06-29 15:39:26 -0600 | [diff] [blame] | 2306 | .extensionCount = enabled_extension_count, |
| 2307 | .ppEnabledExtensionNames = (const char *const*) extension_names, |
Jon Ashburn | ab46b36 | 2015-04-04 14:52:07 -0600 | [diff] [blame] | 2308 | }; |
Courtney Goeltzenleuchter | 28c4d5f | 2015-04-14 18:48:46 -0600 | [diff] [blame] | 2309 | const VkDeviceQueueCreateInfo queue = { |
Chris Forbes | c90f440 | 2015-07-11 19:11:39 +1200 | [diff] [blame] | 2310 | .queueFamilyIndex = 0, |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 2311 | .queueCount = 1, |
| 2312 | }; |
Ian Elliott | 097d9f3 | 2015-04-28 11:35:02 -0600 | [diff] [blame] | 2313 | |
Mark Lobodzinski | c4fffc7 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 2314 | uint32_t gpu_count; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 2315 | |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 2316 | err = vkCreateInstance(&inst_info, &demo->inst); |
Ian Elliott | 0726413 | 2015-04-28 11:35:02 -0600 | [diff] [blame] | 2317 | if (err == VK_ERROR_INCOMPATIBLE_DRIVER) { |
| 2318 | ERR_EXIT("Cannot find a compatible Vulkan installable client driver " |
Ian Elliott | 6515291 | 2015-04-28 13:22:33 -0600 | [diff] [blame] | 2319 | "(ICD).\n\nPlease look at the Getting Started guide for " |
Ian Elliott | 0726413 | 2015-04-28 11:35:02 -0600 | [diff] [blame] | 2320 | "additional information.\n", |
| 2321 | "vkCreateInstance Failure"); |
Tony Barbour | 3dddd5d | 2015-04-29 16:19:20 -0600 | [diff] [blame] | 2322 | } else if (err == VK_ERROR_INVALID_EXTENSION) { |
| 2323 | ERR_EXIT("Cannot find a specified extension library" |
| 2324 | ".\nMake sure your layers path is set appropriately\n", |
| 2325 | "vkCreateInstance Failure"); |
Ian Elliott | 0726413 | 2015-04-28 11:35:02 -0600 | [diff] [blame] | 2326 | } else if (err) { |
Ian Elliott | 6515291 | 2015-04-28 13:22:33 -0600 | [diff] [blame] | 2327 | ERR_EXIT("vkCreateInstance failed.\n\nDo you have a compatible Vulkan " |
| 2328 | "installable client driver (ICD) installed?\nPlease look at " |
Ian Elliott | 0726413 | 2015-04-28 11:35:02 -0600 | [diff] [blame] | 2329 | "the Getting Started guide for additional information.\n", |
| 2330 | "vkCreateInstance Failure"); |
Ian Elliott | 3979e28 | 2015-04-03 15:24:55 -0600 | [diff] [blame] | 2331 | } |
Jon Ashburn | ab46b36 | 2015-04-04 14:52:07 -0600 | [diff] [blame] | 2332 | |
Courtney Goeltzenleuchter | be10336 | 2015-06-29 15:39:26 -0600 | [diff] [blame] | 2333 | free(instance_layers); |
| 2334 | free(instance_extensions); |
Tony Barbour | 3dddd5d | 2015-04-29 16:19:20 -0600 | [diff] [blame] | 2335 | |
Tobin Ehlis | 8a724d8 | 2015-09-07 15:16:39 -0600 | [diff] [blame] | 2336 | /* Make initial call to query gpu_count, then second call for gpu info*/ |
| 2337 | err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, NULL); |
| 2338 | assert(!err && gpu_count > 0); |
| 2339 | physical_devices = malloc(sizeof(VkPhysicalDevice) * gpu_count); |
| 2340 | err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, physical_devices); |
| 2341 | assert(!err); |
| 2342 | /* For cube demo we just grab the first physical device */ |
| 2343 | demo->gpu = physical_devices[0]; |
| 2344 | free(physical_devices); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 2345 | |
Courtney Goeltzenleuchter | be10336 | 2015-06-29 15:39:26 -0600 | [diff] [blame] | 2346 | /* Look for validation layers */ |
| 2347 | validation_found = 0; |
| 2348 | enabled_layer_count = 0; |
| 2349 | uint32_t device_layer_count = 0; |
| 2350 | err = vkGetPhysicalDeviceLayerProperties(demo->gpu, &device_layer_count, NULL); |
| 2351 | assert(!err); |
| 2352 | |
Courtney Goeltzenleuchter | be10336 | 2015-06-29 15:39:26 -0600 | [diff] [blame] | 2353 | device_layers = malloc(sizeof(VkLayerProperties) * device_layer_count); |
| 2354 | err = vkGetPhysicalDeviceLayerProperties(demo->gpu, &device_layer_count, device_layers); |
| 2355 | assert(!err); |
Courtney Goeltzenleuchter | 886f76c | 2015-07-06 17:46:11 -0600 | [diff] [blame] | 2356 | |
| 2357 | if (demo->validate) { |
| 2358 | validation_found = demo_check_layers(ARRAY_SIZE(device_validation_layers), device_validation_layers, |
| 2359 | device_layer_count, device_layers); |
| 2360 | if (!validation_found) { |
| 2361 | ERR_EXIT("vkGetPhysicalDeviceLayerProperties failed to find" |
| 2362 | "a required validation layer.\n\n" |
| 2363 | "Please look at the Getting Started guide for additional " |
| 2364 | "information.\n", |
| 2365 | "vkCreateDevice Failure"); |
Courtney Goeltzenleuchter | be10336 | 2015-06-29 15:39:26 -0600 | [diff] [blame] | 2366 | } |
Courtney Goeltzenleuchter | 886f76c | 2015-07-06 17:46:11 -0600 | [diff] [blame] | 2367 | enabled_layer_count = ARRAY_SIZE(device_validation_layers); |
Courtney Goeltzenleuchter | be10336 | 2015-06-29 15:39:26 -0600 | [diff] [blame] | 2368 | } |
| 2369 | |
| 2370 | uint32_t device_extension_count = 0; |
| 2371 | VkExtensionProperties *device_extensions = NULL; |
| 2372 | err = vkGetPhysicalDeviceExtensionProperties( |
| 2373 | demo->gpu, NULL, &device_extension_count, NULL); |
| 2374 | assert(!err); |
| 2375 | |
| 2376 | WSIextFound = 0; |
| 2377 | enabled_extension_count = 0; |
| 2378 | memset(extension_names, 0, sizeof(extension_names)); |
| 2379 | device_extensions = malloc(sizeof(VkExtensionProperties) * device_extension_count); |
| 2380 | err = vkGetPhysicalDeviceExtensionProperties( |
| 2381 | demo->gpu, NULL, &device_extension_count, device_extensions); |
| 2382 | assert(!err); |
Ian Elliott | aaae535 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 2383 | |
Courtney Goeltzenleuchter | be10336 | 2015-06-29 15:39:26 -0600 | [diff] [blame] | 2384 | for (uint32_t i = 0; i < device_extension_count; i++) { |
Ian Elliott | a078197 | 2015-08-21 15:09:33 -0600 | [diff] [blame] | 2385 | if (!strcmp("VK_EXT_KHR_device_swapchain", device_extensions[i].extName)) { |
Courtney Goeltzenleuchter | be10336 | 2015-06-29 15:39:26 -0600 | [diff] [blame] | 2386 | WSIextFound = 1; |
Ian Elliott | a078197 | 2015-08-21 15:09:33 -0600 | [diff] [blame] | 2387 | extension_names[enabled_extension_count++] = "VK_EXT_KHR_device_swapchain"; |
Courtney Goeltzenleuchter | be10336 | 2015-06-29 15:39:26 -0600 | [diff] [blame] | 2388 | } |
| 2389 | assert(enabled_extension_count < 64); |
| 2390 | } |
| 2391 | if (!WSIextFound) { |
Ian Elliott | aaae535 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 2392 | ERR_EXIT("vkGetPhysicalDeviceExtensionProperties failed to find the " |
Ian Elliott | a078197 | 2015-08-21 15:09:33 -0600 | [diff] [blame] | 2393 | "\"VK_EXT_KHR_device_swapchain\" extension.\n\nDo you have a compatible " |
Courtney Goeltzenleuchter | be10336 | 2015-06-29 15:39:26 -0600 | [diff] [blame] | 2394 | "Vulkan installable client driver (ICD) installed?\nPlease " |
| 2395 | "look at the Getting Started guide for additional " |
| 2396 | "information.\n", |
| 2397 | "vkCreateInstance Failure"); |
| 2398 | } |
| 2399 | |
| 2400 | VkDeviceCreateInfo device = { |
| 2401 | .sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO, |
| 2402 | .pNext = NULL, |
| 2403 | .queueRecordCount = 1, |
| 2404 | .pRequestedQueues = &queue, |
| 2405 | .layerCount = enabled_layer_count, |
Courtney Goeltzenleuchter | 886f76c | 2015-07-06 17:46:11 -0600 | [diff] [blame] | 2406 | .ppEnabledLayerNames = (const char *const*) ((demo->validate) ? device_validation_layers : NULL), |
Courtney Goeltzenleuchter | be10336 | 2015-06-29 15:39:26 -0600 | [diff] [blame] | 2407 | .extensionCount = enabled_extension_count, |
| 2408 | .ppEnabledExtensionNames = (const char *const*) extension_names, |
Courtney Goeltzenleuchter | be10336 | 2015-06-29 15:39:26 -0600 | [diff] [blame] | 2409 | }; |
Courtney Goeltzenleuchter | 922a0fa | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 2410 | |
Tony Barbour | 3dddd5d | 2015-04-29 16:19:20 -0600 | [diff] [blame] | 2411 | if (demo->validate) { |
Courtney Goeltzenleuchter | 4761063 | 2015-07-12 14:35:22 -0600 | [diff] [blame] | 2412 | demo->dbgCreateMsgCallback = (PFN_vkDbgCreateMsgCallback) vkGetInstanceProcAddr(demo->inst, "vkDbgCreateMsgCallback"); |
| 2413 | demo->dbgDestroyMsgCallback = (PFN_vkDbgDestroyMsgCallback) vkGetInstanceProcAddr(demo->inst, "vkDbgDestroyMsgCallback"); |
Courtney Goeltzenleuchter | 922a0fa | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 2414 | if (!demo->dbgCreateMsgCallback) { |
| 2415 | ERR_EXIT("GetProcAddr: Unable to find vkDbgCreateMsgCallback\n", |
| 2416 | "vkGetProcAddr Failure"); |
| 2417 | } |
Tony Barbour | d70b42c | 2015-06-30 14:14:19 -0600 | [diff] [blame] | 2418 | if (!demo->dbgDestroyMsgCallback) { |
| 2419 | ERR_EXIT("GetProcAddr: Unable to find vkDbgDestroyMsgCallback\n", |
| 2420 | "vkGetProcAddr Failure"); |
| 2421 | } |
Courtney Goeltzenleuchter | 03f4b77 | 2015-07-22 11:03:51 -0600 | [diff] [blame] | 2422 | demo->dbgBreakCallback = (PFN_vkDbgMsgCallback) vkGetInstanceProcAddr(demo->inst, "vkDbgBreakCallback"); |
| 2423 | if (!demo->dbgBreakCallback) { |
| 2424 | ERR_EXIT("GetProcAddr: Unable to find vkDbgBreakCallback\n", |
| 2425 | "vkGetProcAddr Failure"); |
| 2426 | } |
| 2427 | |
| 2428 | PFN_vkDbgMsgCallback callback; |
| 2429 | |
| 2430 | if (!demo->use_break) { |
| 2431 | callback = dbgFunc; |
| 2432 | } else { |
| 2433 | callback = demo->dbgBreakCallback; |
| 2434 | } |
Courtney Goeltzenleuchter | 922a0fa | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 2435 | err = demo->dbgCreateMsgCallback( |
| 2436 | demo->inst, |
| 2437 | VK_DBG_REPORT_ERROR_BIT | VK_DBG_REPORT_WARN_BIT, |
Courtney Goeltzenleuchter | 03f4b77 | 2015-07-22 11:03:51 -0600 | [diff] [blame] | 2438 | callback, NULL, |
Courtney Goeltzenleuchter | 922a0fa | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 2439 | &demo->msg_callback); |
| 2440 | switch (err) { |
| 2441 | case VK_SUCCESS: |
| 2442 | break; |
Courtney Goeltzenleuchter | 922a0fa | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 2443 | case VK_ERROR_OUT_OF_HOST_MEMORY: |
| 2444 | ERR_EXIT("dbgCreateMsgCallback: out of host memory\n", |
| 2445 | "dbgCreateMsgCallback Failure"); |
| 2446 | break; |
| 2447 | default: |
| 2448 | ERR_EXIT("dbgCreateMsgCallback: unknown failure\n", |
| 2449 | "dbgCreateMsgCallback Failure"); |
| 2450 | break; |
| 2451 | } |
Tony Barbour | 3dddd5d | 2015-04-29 16:19:20 -0600 | [diff] [blame] | 2452 | } |
| 2453 | |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 2454 | err = vkCreateDevice(demo->gpu, &device, &demo->device); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 2455 | assert(!err); |
| 2456 | |
Courtney Goeltzenleuchter | be10336 | 2015-06-29 15:39:26 -0600 | [diff] [blame] | 2457 | free(device_layers); |
| 2458 | |
Ian Elliott | a078197 | 2015-08-21 15:09:33 -0600 | [diff] [blame] | 2459 | GET_INSTANCE_PROC_ADDR(demo->inst, GetPhysicalDeviceSurfaceSupportKHR); |
| 2460 | GET_DEVICE_PROC_ADDR(demo->device, GetSurfacePropertiesKHR); |
| 2461 | GET_DEVICE_PROC_ADDR(demo->device, GetSurfaceFormatsKHR); |
| 2462 | GET_DEVICE_PROC_ADDR(demo->device, GetSurfacePresentModesKHR); |
| 2463 | GET_DEVICE_PROC_ADDR(demo->device, CreateSwapchainKHR); |
| 2464 | GET_DEVICE_PROC_ADDR(demo->device, CreateSwapchainKHR); |
| 2465 | GET_DEVICE_PROC_ADDR(demo->device, DestroySwapchainKHR); |
| 2466 | GET_DEVICE_PROC_ADDR(demo->device, GetSwapchainImagesKHR); |
| 2467 | GET_DEVICE_PROC_ADDR(demo->device, AcquireNextImageKHR); |
| 2468 | GET_DEVICE_PROC_ADDR(demo->device, QueuePresentKHR); |
Jon Ashburn | 0b85d05 | 2015-05-21 18:13:33 -0600 | [diff] [blame] | 2469 | |
Tony Barbour | ecf1b2b | 2015-06-24 16:06:58 -0600 | [diff] [blame] | 2470 | err = vkGetPhysicalDeviceProperties(demo->gpu, &demo->gpu_props); |
Courtney Goeltzenleuchter | 752eb8e | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 2471 | assert(!err); |
| 2472 | |
Cody Northrop | 4d3c11d | 2015-08-03 17:04:53 -0600 | [diff] [blame] | 2473 | /* Call with NULL data to get count */ |
| 2474 | err = vkGetPhysicalDeviceQueueFamilyProperties(demo->gpu, &demo->queue_count, NULL); |
Courtney Goeltzenleuchter | 752eb8e | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 2475 | assert(!err); |
Courtney Goeltzenleuchter | fe95fca | 2015-07-15 17:40:20 -0600 | [diff] [blame] | 2476 | assert(demo->queue_count >= 1); |
Courtney Goeltzenleuchter | 752eb8e | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 2477 | |
Cody Northrop | 4d3c11d | 2015-08-03 17:04:53 -0600 | [diff] [blame] | 2478 | demo->queue_props = (VkQueueFamilyProperties *) malloc(demo->queue_count * sizeof(VkQueueFamilyProperties)); |
| 2479 | err = vkGetPhysicalDeviceQueueFamilyProperties(demo->gpu, &demo->queue_count, demo->queue_props); |
Courtney Goeltzenleuchter | ed667a5 | 2015-03-05 18:09:39 -0700 | [diff] [blame] | 2480 | assert(!err); |
Courtney Goeltzenleuchter | 3a35c82 | 2015-07-15 17:45:38 -0600 | [diff] [blame] | 2481 | assert(demo->queue_count >= 1); |
| 2482 | } |
| 2483 | |
| 2484 | static void demo_init_vk_wsi(struct demo *demo) |
| 2485 | { |
| 2486 | VkResult err; |
| 2487 | uint32_t i; |
Courtney Goeltzenleuchter | ed667a5 | 2015-03-05 18:09:39 -0700 | [diff] [blame] | 2488 | |
Ian Elliott | aaae535 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 2489 | // Construct the WSI surface description: |
Ian Elliott | a078197 | 2015-08-21 15:09:33 -0600 | [diff] [blame] | 2490 | demo->surface_description.sType = VK_STRUCTURE_TYPE_SURFACE_DESCRIPTION_WINDOW_KHR; |
Ian Elliott | aaae535 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 2491 | demo->surface_description.pNext = NULL; |
| 2492 | #ifdef _WIN32 |
Ian Elliott | a078197 | 2015-08-21 15:09:33 -0600 | [diff] [blame] | 2493 | demo->surface_description.platform = VK_PLATFORM_WIN32_KHR; |
Ian Elliott | aaae535 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 2494 | demo->surface_description.pPlatformHandle = demo->connection; |
| 2495 | demo->surface_description.pPlatformWindow = demo->window; |
| 2496 | #else // _WIN32 |
| 2497 | demo->platform_handle_xcb.connection = demo->connection; |
| 2498 | demo->platform_handle_xcb.root = demo->screen->root; |
Ian Elliott | a078197 | 2015-08-21 15:09:33 -0600 | [diff] [blame] | 2499 | demo->surface_description.platform = VK_PLATFORM_XCB_KHR; |
Ian Elliott | aaae535 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 2500 | demo->surface_description.pPlatformHandle = &demo->platform_handle_xcb; |
| 2501 | demo->surface_description.pPlatformWindow = &demo->window; |
| 2502 | #endif // _WIN32 |
| 2503 | |
| 2504 | // Iterate over each queue to learn whether it supports presenting to WSI: |
Courtney Goeltzenleuchter | fe95fca | 2015-07-15 17:40:20 -0600 | [diff] [blame] | 2505 | VkBool32* supportsPresent = (VkBool32 *)malloc(demo->queue_count * sizeof(VkBool32)); |
| 2506 | for (i = 0; i < demo->queue_count; i++) { |
Ian Elliott | a078197 | 2015-08-21 15:09:33 -0600 | [diff] [blame] | 2507 | demo->fpGetPhysicalDeviceSurfaceSupportKHR(demo->gpu, i, |
| 2508 | (VkSurfaceDescriptionKHR *) &demo->surface_description, |
Ian Elliott | aaae535 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 2509 | &supportsPresent[i]); |
Courtney Goeltzenleuchter | ed667a5 | 2015-03-05 18:09:39 -0700 | [diff] [blame] | 2510 | } |
Ian Elliott | aaae535 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 2511 | |
| 2512 | // Search for a graphics and a present queue in the array of queue |
| 2513 | // families, try to find one that supports both |
| 2514 | uint32_t graphicsQueueNodeIndex = UINT32_MAX; |
| 2515 | uint32_t presentQueueNodeIndex = UINT32_MAX; |
Courtney Goeltzenleuchter | fe95fca | 2015-07-15 17:40:20 -0600 | [diff] [blame] | 2516 | for (i = 0; i < demo->queue_count; i++) { |
Ian Elliott | aaae535 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 2517 | if ((demo->queue_props[i].queueFlags & VK_QUEUE_GRAPHICS_BIT) != 0) { |
| 2518 | if (graphicsQueueNodeIndex == UINT32_MAX) { |
| 2519 | graphicsQueueNodeIndex = i; |
| 2520 | } |
| 2521 | |
| 2522 | if (supportsPresent[i] == VK_TRUE) { |
| 2523 | graphicsQueueNodeIndex = i; |
| 2524 | presentQueueNodeIndex = i; |
| 2525 | break; |
| 2526 | } |
| 2527 | } |
| 2528 | } |
| 2529 | if (presentQueueNodeIndex == UINT32_MAX) { |
| 2530 | // If didn't find a queue that supports both graphics and present, then |
| 2531 | // find a separate present queue. |
Courtney Goeltzenleuchter | fe95fca | 2015-07-15 17:40:20 -0600 | [diff] [blame] | 2532 | for (uint32_t i = 0; i < demo->queue_count; ++i) { |
Ian Elliott | aaae535 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 2533 | if (supportsPresent[i] == VK_TRUE) { |
| 2534 | presentQueueNodeIndex = i; |
| 2535 | break; |
| 2536 | } |
| 2537 | } |
| 2538 | } |
| 2539 | free(supportsPresent); |
| 2540 | |
| 2541 | // Generate error if could not find both a graphics and a present queue |
| 2542 | if (graphicsQueueNodeIndex == UINT32_MAX || presentQueueNodeIndex == UINT32_MAX) { |
| 2543 | ERR_EXIT("Could not find a graphics and a present queue\n", |
| 2544 | "WSI Initialization Failure"); |
| 2545 | } |
| 2546 | |
| 2547 | // TODO: Add support for separate queues, including presentation, |
| 2548 | // synchronization, and appropriate tracking for QueueSubmit |
| 2549 | // While it is possible for an application to use a separate graphics and a |
| 2550 | // present queues, this demo program assumes it is only using one: |
| 2551 | if (graphicsQueueNodeIndex != presentQueueNodeIndex) { |
| 2552 | ERR_EXIT("Could not find a common graphics and a present queue\n", |
| 2553 | "WSI Initialization Failure"); |
| 2554 | } |
| 2555 | |
| 2556 | demo->graphics_queue_node_index = graphicsQueueNodeIndex; |
Courtney Goeltzenleuchter | ed667a5 | 2015-03-05 18:09:39 -0700 | [diff] [blame] | 2557 | |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 2558 | err = vkGetDeviceQueue(demo->device, demo->graphics_queue_node_index, |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 2559 | 0, &demo->queue); |
| 2560 | assert(!err); |
Tony Barbour | 3dddd5d | 2015-04-29 16:19:20 -0600 | [diff] [blame] | 2561 | |
Ian Elliott | aaae535 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 2562 | // Get the list of VkFormat's that are supported: |
Ian Elliott | 8528eff | 2015-08-07 15:56:59 -0600 | [diff] [blame] | 2563 | uint32_t formatCount; |
Ian Elliott | a078197 | 2015-08-21 15:09:33 -0600 | [diff] [blame] | 2564 | err = demo->fpGetSurfaceFormatsKHR(demo->device, |
| 2565 | (VkSurfaceDescriptionKHR *) &demo->surface_description, |
Ian Elliott | 8528eff | 2015-08-07 15:56:59 -0600 | [diff] [blame] | 2566 | &formatCount, NULL); |
Ian Elliott | aaae535 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 2567 | assert(!err); |
Ian Elliott | a078197 | 2015-08-21 15:09:33 -0600 | [diff] [blame] | 2568 | VkSurfaceFormatKHR *surfFormats = |
| 2569 | (VkSurfaceFormatKHR *)malloc(formatCount * sizeof(VkSurfaceFormatKHR)); |
| 2570 | err = demo->fpGetSurfaceFormatsKHR(demo->device, |
| 2571 | (VkSurfaceDescriptionKHR *) &demo->surface_description, |
Ian Elliott | 8528eff | 2015-08-07 15:56:59 -0600 | [diff] [blame] | 2572 | &formatCount, surfFormats); |
Ian Elliott | aaae535 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 2573 | assert(!err); |
| 2574 | // If the format list includes just one entry of VK_FORMAT_UNDEFINED, |
| 2575 | // the surface has no preferred format. Otherwise, at least one |
| 2576 | // supported format will be returned. |
Ian Elliott | aaae535 | 2015-07-06 14:27:58 -0600 | [diff] [blame] | 2577 | if (formatCount == 1 && surfFormats[0].format == VK_FORMAT_UNDEFINED) |
| 2578 | { |
| 2579 | demo->format = VK_FORMAT_B8G8R8A8_UNORM; |
| 2580 | } |
| 2581 | else |
| 2582 | { |
| 2583 | assert(formatCount >= 1); |
| 2584 | demo->format = surfFormats[0].format; |
| 2585 | } |
Ian Elliott | 8528eff | 2015-08-07 15:56:59 -0600 | [diff] [blame] | 2586 | demo->color_space = surfFormats[0].colorSpace; |
Ian Elliott | e4602cd | 2015-04-21 16:41:02 -0600 | [diff] [blame] | 2587 | |
David Pinedo | 2bb7c93 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 2588 | demo->quit = false; |
| 2589 | demo->curFrame = 0; |
Mark Lobodzinski | eadf998 | 2015-07-02 16:49:40 -0600 | [diff] [blame] | 2590 | |
| 2591 | // Get Memory information and properties |
| 2592 | err = vkGetPhysicalDeviceMemoryProperties(demo->gpu, &demo->memory_properties); |
| 2593 | assert(!err); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 2594 | } |
| 2595 | |
| 2596 | static void demo_init_connection(struct demo *demo) |
| 2597 | { |
Ian Elliott | 639ca47 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 2598 | #ifndef _WIN32 |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 2599 | const xcb_setup_t *setup; |
| 2600 | xcb_screen_iterator_t iter; |
| 2601 | int scr; |
| 2602 | |
| 2603 | demo->connection = xcb_connect(NULL, &scr); |
Ian Elliott | 3979e28 | 2015-04-03 15:24:55 -0600 | [diff] [blame] | 2604 | if (demo->connection == NULL) { |
| 2605 | printf("Cannot find a compatible Vulkan installable client driver " |
| 2606 | "(ICD).\nExiting ...\n"); |
| 2607 | fflush(stdout); |
| 2608 | exit(1); |
| 2609 | } |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 2610 | |
| 2611 | setup = xcb_get_setup(demo->connection); |
| 2612 | iter = xcb_setup_roots_iterator(setup); |
| 2613 | while (scr-- > 0) |
| 2614 | xcb_screen_next(&iter); |
| 2615 | |
| 2616 | demo->screen = iter.data; |
Ian Elliott | 639ca47 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 2617 | #endif // _WIN32 |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 2618 | } |
| 2619 | |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 2620 | static void demo_init(struct demo *demo, int argc, char **argv) |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 2621 | { |
Courtney Goeltzenleuchter | 8879d3a | 2014-10-29 08:29:35 -0600 | [diff] [blame] | 2622 | vec3 eye = {0.0f, 3.0f, 5.0f}; |
| 2623 | vec3 origin = {0, 0, 0}; |
Chia-I Wu | ae3b55d | 2015-04-22 14:56:17 +0800 | [diff] [blame] | 2624 | vec3 up = {0.0f, 1.0f, 0.0}; |
Courtney Goeltzenleuchter | 8879d3a | 2014-10-29 08:29:35 -0600 | [diff] [blame] | 2625 | |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 2626 | memset(demo, 0, sizeof(*demo)); |
David Pinedo | 2bb7c93 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 2627 | demo->frameCount = INT_MAX; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 2628 | |
Piers Daniell | 735ee53 | 2015-02-23 16:23:13 -0700 | [diff] [blame] | 2629 | for (int i = 1; i < argc; i++) { |
Tony Barbour | 3dddd5d | 2015-04-29 16:19:20 -0600 | [diff] [blame] | 2630 | if (strcmp(argv[i], "--use_staging") == 0) { |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 2631 | demo->use_staging_buffer = true; |
Tony Barbour | 3dddd5d | 2015-04-29 16:19:20 -0600 | [diff] [blame] | 2632 | continue; |
Ian Elliott | 639ca47 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 2633 | } |
Cody Northrop | 1fedb21 | 2015-05-28 11:27:16 -0600 | [diff] [blame] | 2634 | if (strcmp(argv[i], "--use_glsl") == 0) { |
| 2635 | demo->use_glsl = true; |
| 2636 | continue; |
| 2637 | } |
Courtney Goeltzenleuchter | 03f4b77 | 2015-07-22 11:03:51 -0600 | [diff] [blame] | 2638 | if (strcmp(argv[i], "--break") == 0) { |
| 2639 | demo->use_break = true; |
| 2640 | continue; |
| 2641 | } |
Tony Barbour | 3dddd5d | 2015-04-29 16:19:20 -0600 | [diff] [blame] | 2642 | if (strcmp(argv[i], "--validate") == 0) { |
| 2643 | demo->validate = true; |
| 2644 | continue; |
| 2645 | } |
David Pinedo | 2bb7c93 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 2646 | if (strcmp(argv[i], "--c") == 0 && |
| 2647 | demo->frameCount == INT_MAX && |
| 2648 | i < argc-1 && |
| 2649 | sscanf(argv[i+1],"%d", &demo->frameCount) == 1 && |
| 2650 | demo->frameCount >= 0) |
| 2651 | { |
| 2652 | i++; |
| 2653 | continue; |
| 2654 | } |
Tony Barbour | 3dddd5d | 2015-04-29 16:19:20 -0600 | [diff] [blame] | 2655 | |
Courtney Goeltzenleuchter | 03f4b77 | 2015-07-22 11:03:51 -0600 | [diff] [blame] | 2656 | 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] | 2657 | fflush(stderr); |
| 2658 | exit(1); |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 2659 | } |
| 2660 | |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 2661 | demo_init_connection(demo); |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 2662 | demo_init_vk(demo); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 2663 | |
Courtney Goeltzenleuchter | 8879d3a | 2014-10-29 08:29:35 -0600 | [diff] [blame] | 2664 | demo->width = 500; |
| 2665 | demo->height = 500; |
Courtney Goeltzenleuchter | 8879d3a | 2014-10-29 08:29:35 -0600 | [diff] [blame] | 2666 | |
| 2667 | demo->spin_angle = 0.01f; |
| 2668 | demo->spin_increment = 0.01f; |
| 2669 | demo->pause = false; |
| 2670 | |
Piers Daniell | 735ee53 | 2015-02-23 16:23:13 -0700 | [diff] [blame] | 2671 | 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] | 2672 | mat4x4_look_at(demo->view_matrix, eye, origin, up); |
| 2673 | mat4x4_identity(demo->model_matrix); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 2674 | } |
| 2675 | |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 2676 | |
Ian Elliott | 639ca47 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 2677 | #ifdef _WIN32 |
Tony Barbour | 3dddd5d | 2015-04-29 16:19:20 -0600 | [diff] [blame] | 2678 | extern int __getmainargs( |
| 2679 | int * _Argc, |
| 2680 | char *** _Argv, |
| 2681 | char *** _Env, |
| 2682 | int _DoWildCard, |
| 2683 | int * new_mode); |
Ian Elliott | f9cf78c | 2015-04-28 10:33:11 -0600 | [diff] [blame] | 2684 | |
Ian Elliott | a748eaf | 2015-04-28 15:50:36 -0600 | [diff] [blame] | 2685 | int WINAPI WinMain(HINSTANCE hInstance, |
| 2686 | HINSTANCE hPrevInstance, |
| 2687 | LPSTR pCmdLine, |
| 2688 | int nCmdShow) |
Ian Elliott | 639ca47 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 2689 | { |
| 2690 | MSG msg; // message |
| 2691 | bool done; // flag saying when app is complete |
Tony Barbour | 3dddd5d | 2015-04-29 16:19:20 -0600 | [diff] [blame] | 2692 | int argc; |
| 2693 | char** argv; |
| 2694 | char** env; |
| 2695 | int new_mode = 0; |
Ian Elliott | 639ca47 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 2696 | |
Tony Barbour | 3dddd5d | 2015-04-29 16:19:20 -0600 | [diff] [blame] | 2697 | __getmainargs(&argc,&argv,&env,0,&new_mode); |
| 2698 | |
| 2699 | demo_init(&demo, argc, argv); |
| 2700 | demo.connection = hInstance; |
| 2701 | strncpy(demo.name, "cube", APP_NAME_STR_LEN); |
Ian Elliott | 639ca47 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 2702 | demo_create_window(&demo); |
Courtney Goeltzenleuchter | 3a35c82 | 2015-07-15 17:45:38 -0600 | [diff] [blame] | 2703 | demo_init_vk_wsi(&demo); |
Ian Elliott | 639ca47 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 2704 | |
| 2705 | demo_prepare(&demo); |
| 2706 | |
| 2707 | done = false; //initialize loop condition variable |
| 2708 | /* main message loop*/ |
| 2709 | while(!done) |
| 2710 | { |
Ian Elliott | a748eaf | 2015-04-28 15:50:36 -0600 | [diff] [blame] | 2711 | PeekMessage(&msg, NULL, 0, 0, PM_REMOVE); |
Ian Elliott | 639ca47 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 2712 | if (msg.message == WM_QUIT) //check for a quit message |
| 2713 | { |
| 2714 | done = true; //if found, quit app |
| 2715 | } |
| 2716 | else |
| 2717 | { |
| 2718 | /* Translate and dispatch to event queue*/ |
| 2719 | TranslateMessage(&msg); |
| 2720 | DispatchMessage(&msg); |
| 2721 | } |
| 2722 | } |
| 2723 | |
| 2724 | demo_cleanup(&demo); |
| 2725 | |
Tony Barbour | f992173 | 2015-04-22 11:36:22 -0600 | [diff] [blame] | 2726 | return (int) msg.wParam; |
Ian Elliott | 639ca47 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 2727 | } |
| 2728 | #else // _WIN32 |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 2729 | int main(int argc, char **argv) |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 2730 | { |
| 2731 | struct demo demo; |
| 2732 | |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 2733 | demo_init(&demo, argc, argv); |
Chia-I Wu | cbb564e | 2015-04-16 22:02:10 +0800 | [diff] [blame] | 2734 | demo_create_window(&demo); |
Courtney Goeltzenleuchter | 3a35c82 | 2015-07-15 17:45:38 -0600 | [diff] [blame] | 2735 | demo_init_vk_wsi(&demo); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 2736 | |
| 2737 | demo_prepare(&demo); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 2738 | demo_run(&demo); |
| 2739 | |
| 2740 | demo_cleanup(&demo); |
| 2741 | |
| 2742 | return 0; |
| 2743 | } |
Ian Elliott | 639ca47 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 2744 | #endif // _WIN32 |