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