Ian Elliott | 421107f | 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 | 1722360 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 24 | #define _GNU_SOURCE |
Courtney Goeltzenleuchter | 4e8246e | 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 | e14e9f9 | 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 | 4e8246e | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 36 | #include <xcb/xcb.h> |
Ian Elliott | e14e9f9 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 37 | #endif // _WIN32 |
| 38 | |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 39 | #include <vulkan.h> |
Chia-I Wu | 5b66aa5 | 2015-04-16 22:02:10 +0800 | [diff] [blame] | 40 | #include <vk_wsi_lunarg.h> |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 41 | #include "vk_debug_report_lunarg.h" |
Courtney Goeltzenleuchter | 4e8246e | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 42 | |
Cody Northrop | d4e020a | 2015-03-17 14:54:35 -0600 | [diff] [blame] | 43 | #include "icd-spv.h" |
Courtney Goeltzenleuchter | 4e8246e | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 44 | |
Courtney Goeltzenleuchter | 6f88d4c | 2014-10-28 10:32:57 -0600 | [diff] [blame] | 45 | #include "linmath.h" |
Courtney Goeltzenleuchter | 1722360 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 46 | #include <png.h> |
Courtney Goeltzenleuchter | 6f88d4c | 2014-10-28 10:32:57 -0600 | [diff] [blame] | 47 | |
Courtney Goeltzenleuchter | 4e8246e | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 48 | #define DEMO_BUFFER_COUNT 2 |
| 49 | #define DEMO_TEXTURE_COUNT 1 |
Ian Elliott | 4e19ed0 | 2015-04-28 10:52:52 -0600 | [diff] [blame] | 50 | #define APP_SHORT_NAME "cube" |
| 51 | #define APP_LONG_NAME "The Vulkan Cube Demo Program" |
Courtney Goeltzenleuchter | 4e8246e | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 52 | |
Courtney Goeltzenleuchter | 8c15fc5 | 2015-07-06 17:46:11 -0600 | [diff] [blame^] | 53 | #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) |
| 54 | |
Tony Barbour | 22a3086 | 2015-04-22 09:02:32 -0600 | [diff] [blame] | 55 | #if defined(NDEBUG) && defined(__GNUC__) |
| 56 | #define U_ASSERT_ONLY __attribute__((unused)) |
| 57 | #else |
| 58 | #define U_ASSERT_ONLY |
| 59 | #endif |
| 60 | |
Ian Elliott | caa9f27 | 2015-04-28 11:35:02 -0600 | [diff] [blame] | 61 | #ifdef _WIN32 |
| 62 | #define ERR_EXIT(err_msg, err_class) \ |
| 63 | do { \ |
| 64 | MessageBox(NULL, err_msg, err_class, MB_OK); \ |
| 65 | exit(1); \ |
| 66 | } while (0) |
| 67 | |
| 68 | // NOTE: If the following values (copied from "loader_platform.h") change, they |
| 69 | // need to change here as well: |
| 70 | #define LAYER_NAMES_ENV "VK_LAYER_NAMES" |
| 71 | #define LAYER_NAMES_REGISTRY_VALUE "VK_LAYER_NAMES" |
| 72 | |
| 73 | #else // _WIN32 |
| 74 | |
| 75 | #define ERR_EXIT(err_msg, err_class) \ |
| 76 | do { \ |
| 77 | printf(err_msg); \ |
| 78 | fflush(stdout); \ |
| 79 | exit(1); \ |
| 80 | } while (0) |
| 81 | #endif // _WIN32 |
| 82 | |
Ian Elliott | 1b6de09 | 2015-06-22 15:07:49 -0600 | [diff] [blame] | 83 | #define GET_DEVICE_PROC_ADDR(dev, entrypoint) \ |
| 84 | { \ |
| 85 | demo->fp##entrypoint = vkGetDeviceProcAddr(dev, "vk"#entrypoint); \ |
| 86 | if (demo->fp##entrypoint == NULL) { \ |
| 87 | ERR_EXIT("vkGetDeviceProcAddr failed to find vk"#entrypoint, \ |
| 88 | "vkGetDeviceProcAddr Failure"); \ |
| 89 | } \ |
| 90 | } |
| 91 | |
Courtney Goeltzenleuchter | 7e33498 | 2014-10-30 15:14:16 -0600 | [diff] [blame] | 92 | /* |
Courtney Goeltzenleuchter | 40a8b0a | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 93 | * structure to track all objects related to a texture. |
| 94 | */ |
Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 95 | struct texture_object { |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 96 | VkSampler sampler; |
Courtney Goeltzenleuchter | 40a8b0a | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 97 | |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 98 | VkImage image; |
| 99 | VkImageLayout imageLayout; |
Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 100 | |
Mark Lobodzinski | 2318261 | 2015-05-29 09:32:35 -0500 | [diff] [blame] | 101 | VkDeviceMemory mem; |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 102 | VkImageView view; |
Courtney Goeltzenleuchter | 40a8b0a | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 103 | int32_t tex_width, tex_height; |
| 104 | }; |
| 105 | |
Courtney Goeltzenleuchter | 1722360 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 106 | static char *tex_files[] = { |
Courtney Goeltzenleuchter | 7e33498 | 2014-10-30 15:14:16 -0600 | [diff] [blame] | 107 | "lunarg-logo-256x256-solid.png" |
Courtney Goeltzenleuchter | 1722360 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 108 | }; |
| 109 | |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 110 | struct vkcube_vs_uniform { |
Courtney Goeltzenleuchter | e334240 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 111 | // Must start with MVP |
Mark Lobodzinski | e2d07a5 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 112 | float mvp[4][4]; |
| 113 | float position[12*3][4]; |
| 114 | float color[12*3][4]; |
Courtney Goeltzenleuchter | e334240 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 115 | }; |
| 116 | |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 117 | struct vktexcube_vs_uniform { |
Courtney Goeltzenleuchter | e334240 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 118 | // Must start with MVP |
Mark Lobodzinski | e2d07a5 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 119 | float mvp[4][4]; |
| 120 | float position[12*3][4]; |
| 121 | float attr[12*3][4]; |
Courtney Goeltzenleuchter | e334240 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 122 | }; |
Courtney Goeltzenleuchter | 6f88d4c | 2014-10-28 10:32:57 -0600 | [diff] [blame] | 123 | |
Courtney Goeltzenleuchter | 4e8246e | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 124 | //-------------------------------------------------------------------------------------- |
| 125 | // Mesh and VertexFormat Data |
| 126 | //-------------------------------------------------------------------------------------- |
| 127 | struct Vertex |
| 128 | { |
Mark Lobodzinski | e2d07a5 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 129 | float posX, posY, posZ, posW; // Position data |
| 130 | float r, g, b, a; // Color |
Courtney Goeltzenleuchter | 4e8246e | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 131 | }; |
| 132 | |
Courtney Goeltzenleuchter | e334240 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 133 | struct VertexPosTex |
| 134 | { |
Mark Lobodzinski | e2d07a5 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 135 | float posX, posY, posZ, posW; // Position data |
| 136 | float u, v, s, t; // Texcoord |
Courtney Goeltzenleuchter | e334240 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 137 | }; |
| 138 | |
Courtney Goeltzenleuchter | 4e8246e | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 139 | #define XYZ1(_x_, _y_, _z_) (_x_), (_y_), (_z_), 1.f |
Courtney Goeltzenleuchter | e334240 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 140 | #define UV(_u_, _v_) (_u_), (_v_), 0.f, 1.f |
Courtney Goeltzenleuchter | 4e8246e | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 141 | |
Mark Lobodzinski | e2d07a5 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 142 | static const float g_vertex_buffer_data[] = { |
Chia-I Wu | c3487c2 | 2015-04-22 14:56:17 +0800 | [diff] [blame] | 143 | -1.0f,-1.0f,-1.0f, // -X side |
Courtney Goeltzenleuchter | 1722360 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 144 | -1.0f,-1.0f, 1.0f, |
| 145 | -1.0f, 1.0f, 1.0f, |
Chia-I Wu | c3487c2 | 2015-04-22 14:56:17 +0800 | [diff] [blame] | 146 | -1.0f, 1.0f, 1.0f, |
Courtney Goeltzenleuchter | 1722360 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 147 | -1.0f, 1.0f,-1.0f, |
| 148 | -1.0f,-1.0f,-1.0f, |
| 149 | |
Chia-I Wu | c3487c2 | 2015-04-22 14:56:17 +0800 | [diff] [blame] | 150 | -1.0f,-1.0f,-1.0f, // -Z side |
Courtney Goeltzenleuchter | 1722360 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 151 | 1.0f, 1.0f,-1.0f, |
| 152 | 1.0f,-1.0f,-1.0f, |
Chia-I Wu | c3487c2 | 2015-04-22 14:56:17 +0800 | [diff] [blame] | 153 | -1.0f,-1.0f,-1.0f, |
Courtney Goeltzenleuchter | 1722360 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 154 | -1.0f, 1.0f,-1.0f, |
Mike Stroyan | ea3945c | 2015-03-19 14:29:04 -0600 | [diff] [blame] | 155 | 1.0f, 1.0f,-1.0f, |
Courtney Goeltzenleuchter | 1722360 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 156 | |
Chia-I Wu | c3487c2 | 2015-04-22 14:56:17 +0800 | [diff] [blame] | 157 | -1.0f,-1.0f,-1.0f, // -Y side |
Courtney Goeltzenleuchter | 1722360 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 158 | 1.0f,-1.0f,-1.0f, |
Mike Stroyan | ea3945c | 2015-03-19 14:29:04 -0600 | [diff] [blame] | 159 | 1.0f,-1.0f, 1.0f, |
Chia-I Wu | c3487c2 | 2015-04-22 14:56:17 +0800 | [diff] [blame] | 160 | -1.0f,-1.0f,-1.0f, |
Courtney Goeltzenleuchter | 1722360 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 161 | 1.0f,-1.0f, 1.0f, |
Mike Stroyan | ea3945c | 2015-03-19 14:29:04 -0600 | [diff] [blame] | 162 | -1.0f,-1.0f, 1.0f, |
Courtney Goeltzenleuchter | 1722360 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 163 | |
Chia-I Wu | c3487c2 | 2015-04-22 14:56:17 +0800 | [diff] [blame] | 164 | -1.0f, 1.0f,-1.0f, // +Y side |
Courtney Goeltzenleuchter | 1722360 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 165 | -1.0f, 1.0f, 1.0f, |
| 166 | 1.0f, 1.0f, 1.0f, |
Chia-I Wu | c3487c2 | 2015-04-22 14:56:17 +0800 | [diff] [blame] | 167 | -1.0f, 1.0f,-1.0f, |
Courtney Goeltzenleuchter | 1722360 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 168 | 1.0f, 1.0f, 1.0f, |
Mike Stroyan | ea3945c | 2015-03-19 14:29:04 -0600 | [diff] [blame] | 169 | 1.0f, 1.0f,-1.0f, |
Courtney Goeltzenleuchter | 1722360 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 170 | |
Chia-I Wu | c3487c2 | 2015-04-22 14:56:17 +0800 | [diff] [blame] | 171 | 1.0f, 1.0f,-1.0f, // +X side |
Courtney Goeltzenleuchter | 1722360 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 172 | 1.0f, 1.0f, 1.0f, |
| 173 | 1.0f,-1.0f, 1.0f, |
Chia-I Wu | c3487c2 | 2015-04-22 14:56:17 +0800 | [diff] [blame] | 174 | 1.0f,-1.0f, 1.0f, |
Courtney Goeltzenleuchter | 1722360 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 175 | 1.0f,-1.0f,-1.0f, |
| 176 | 1.0f, 1.0f,-1.0f, |
| 177 | |
Chia-I Wu | c3487c2 | 2015-04-22 14:56:17 +0800 | [diff] [blame] | 178 | -1.0f, 1.0f, 1.0f, // +Z side |
Courtney Goeltzenleuchter | 1722360 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 179 | -1.0f,-1.0f, 1.0f, |
Mike Stroyan | ea3945c | 2015-03-19 14:29:04 -0600 | [diff] [blame] | 180 | 1.0f, 1.0f, 1.0f, |
Chia-I Wu | c3487c2 | 2015-04-22 14:56:17 +0800 | [diff] [blame] | 181 | -1.0f,-1.0f, 1.0f, |
Courtney Goeltzenleuchter | 1722360 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 182 | 1.0f,-1.0f, 1.0f, |
| 183 | 1.0f, 1.0f, 1.0f, |
Courtney Goeltzenleuchter | e334240 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 184 | }; |
| 185 | |
Mark Lobodzinski | e2d07a5 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 186 | static const float g_uv_buffer_data[] = { |
Chia-I Wu | c3487c2 | 2015-04-22 14:56:17 +0800 | [diff] [blame] | 187 | 0.0f, 0.0f, // -X side |
| 188 | 1.0f, 0.0f, |
| 189 | 1.0f, 1.0f, |
| 190 | 1.0f, 1.0f, |
| 191 | 0.0f, 1.0f, |
| 192 | 0.0f, 0.0f, |
| 193 | |
| 194 | 1.0f, 0.0f, // -Z side |
| 195 | 0.0f, 1.0f, |
| 196 | 0.0f, 0.0f, |
| 197 | 1.0f, 0.0f, |
| 198 | 1.0f, 1.0f, |
| 199 | 0.0f, 1.0f, |
| 200 | |
| 201 | 1.0f, 1.0f, // -Y side |
| 202 | 1.0f, 0.0f, |
| 203 | 0.0f, 0.0f, |
| 204 | 1.0f, 1.0f, |
Courtney Goeltzenleuchter | 1722360 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 205 | 0.0f, 0.0f, |
| 206 | 0.0f, 1.0f, |
| 207 | |
Chia-I Wu | c3487c2 | 2015-04-22 14:56:17 +0800 | [diff] [blame] | 208 | 1.0f, 1.0f, // +Y side |
Courtney Goeltzenleuchter | 3eeff43 | 2014-10-29 08:29:35 -0600 | [diff] [blame] | 209 | 0.0f, 1.0f, |
Chia-I Wu | c3487c2 | 2015-04-22 14:56:17 +0800 | [diff] [blame] | 210 | 0.0f, 0.0f, |
Mike Stroyan | ea3945c | 2015-03-19 14:29:04 -0600 | [diff] [blame] | 211 | 1.0f, 1.0f, |
Courtney Goeltzenleuchter | 1722360 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 212 | 0.0f, 0.0f, |
Mike Stroyan | ea3945c | 2015-03-19 14:29:04 -0600 | [diff] [blame] | 213 | 1.0f, 0.0f, |
Courtney Goeltzenleuchter | 1722360 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 214 | |
Chia-I Wu | c3487c2 | 2015-04-22 14:56:17 +0800 | [diff] [blame] | 215 | 1.0f, 1.0f, // +X side |
| 216 | 0.0f, 1.0f, |
| 217 | 0.0f, 0.0f, |
| 218 | 0.0f, 0.0f, |
Courtney Goeltzenleuchter | 1722360 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 219 | 1.0f, 0.0f, |
Mike Stroyan | ea3945c | 2015-03-19 14:29:04 -0600 | [diff] [blame] | 220 | 1.0f, 1.0f, |
Courtney Goeltzenleuchter | 1722360 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 221 | |
Chia-I Wu | c3487c2 | 2015-04-22 14:56:17 +0800 | [diff] [blame] | 222 | 0.0f, 1.0f, // +Z side |
| 223 | 0.0f, 0.0f, |
Courtney Goeltzenleuchter | 1722360 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 224 | 1.0f, 1.0f, |
Mike Stroyan | ea3945c | 2015-03-19 14:29:04 -0600 | [diff] [blame] | 225 | 0.0f, 0.0f, |
Chia-I Wu | c3487c2 | 2015-04-22 14:56:17 +0800 | [diff] [blame] | 226 | 1.0f, 0.0f, |
Courtney Goeltzenleuchter | 1722360 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 227 | 1.0f, 1.0f, |
Courtney Goeltzenleuchter | e334240 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 228 | }; |
| 229 | |
| 230 | void dumpMatrix(const char *note, mat4x4 MVP) |
| 231 | { |
| 232 | int i; |
| 233 | |
| 234 | printf("%s: \n", note); |
| 235 | for (i=0; i<4; i++) { |
| 236 | printf("%f, %f, %f, %f\n", MVP[i][0], MVP[i][1], MVP[i][2], MVP[i][3]); |
| 237 | } |
| 238 | printf("\n"); |
| 239 | fflush(stdout); |
| 240 | } |
| 241 | |
| 242 | void dumpVec4(const char *note, vec4 vector) |
| 243 | { |
| 244 | printf("%s: \n", note); |
| 245 | printf("%f, %f, %f, %f\n", vector[0], vector[1], vector[2], vector[3]); |
| 246 | printf("\n"); |
| 247 | fflush(stdout); |
| 248 | } |
| 249 | |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 250 | void dbgFunc( |
| 251 | VkFlags msgFlags, |
| 252 | VkObjectType objType, |
| 253 | VkObject srcObject, |
| 254 | size_t location, |
| 255 | int32_t msgCode, |
| 256 | const char* pLayerPrefix, |
| 257 | const char* pMsg, |
| 258 | void* pUserData) |
Tony Barbour | 5685ad7 | 2015-04-29 16:19:20 -0600 | [diff] [blame] | 259 | { |
| 260 | char *message = (char *) malloc(strlen(pMsg)+100); |
| 261 | |
| 262 | assert (message); |
| 263 | |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 264 | if (msgFlags & VK_DBG_REPORT_ERROR_BIT) { |
| 265 | sprintf(message,"ERROR: [%s] Code %d : %s", pLayerPrefix, msgCode, pMsg); |
| 266 | } else if (msgFlags & VK_DBG_REPORT_WARN_BIT) { |
Tony Barbour | a65ecc2 | 2015-06-30 14:14:19 -0600 | [diff] [blame] | 267 | // We know that we're submitting queues without fences, ignore this warning |
| 268 | if (strstr(pMsg, "vkQueueSubmit parameter, VkFence fence, is null pointer")){ |
| 269 | return; |
| 270 | } |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 271 | sprintf(message,"WARNING: [%s] Code %d : %s", pLayerPrefix, msgCode, pMsg); |
Tony Barbour | 5685ad7 | 2015-04-29 16:19:20 -0600 | [diff] [blame] | 272 | } else { |
| 273 | return; |
| 274 | } |
| 275 | |
| 276 | #ifdef _WIN32 |
| 277 | MessageBox(NULL, message, "Alert", MB_OK); |
| 278 | #else |
| 279 | printf("%s\n",message); |
| 280 | fflush(stdout); |
| 281 | #endif |
| 282 | free(message); |
| 283 | } |
| 284 | |
Courtney Goeltzenleuchter | 4e8246e | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 285 | struct demo { |
Ian Elliott | e14e9f9 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 286 | #ifdef _WIN32 |
| 287 | #define APP_NAME_STR_LEN 80 |
| 288 | HINSTANCE connection; // hInstance - Windows Instance |
| 289 | char name[APP_NAME_STR_LEN]; // Name to put on the window/icon |
| 290 | HWND window; // hWnd - window handle |
| 291 | #else // _WIN32 |
Courtney Goeltzenleuchter | 4e8246e | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 292 | xcb_connection_t *connection; |
| 293 | xcb_screen_t *screen; |
Chia-I Wu | 5b66aa5 | 2015-04-16 22:02:10 +0800 | [diff] [blame] | 294 | xcb_window_t window; |
| 295 | xcb_intern_atom_reply_t *atom_wm_delete_window; |
Ian Elliott | e14e9f9 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 296 | #endif // _WIN32 |
Cody Northrop | 75db032 | 2015-05-28 11:27:16 -0600 | [diff] [blame] | 297 | bool prepared; |
Courtney Goeltzenleuchter | 40a8b0a | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 298 | bool use_staging_buffer; |
Cody Northrop | 75db032 | 2015-05-28 11:27:16 -0600 | [diff] [blame] | 299 | bool use_glsl; |
Courtney Goeltzenleuchter | 4e8246e | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 300 | |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 301 | VkInstance inst; |
Tony Barbour | 8205d90 | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 302 | VkPhysicalDevice gpu; |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 303 | VkDevice device; |
| 304 | VkQueue queue; |
Courtney Goeltzenleuchter | f316806 | 2015-03-05 18:09:39 -0700 | [diff] [blame] | 305 | uint32_t graphics_queue_node_index; |
Tony Barbour | 426b905 | 2015-06-24 16:06:58 -0600 | [diff] [blame] | 306 | VkPhysicalDeviceProperties gpu_props; |
Tony Barbour | 8205d90 | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 307 | VkPhysicalDeviceQueueProperties *queue_props; |
Mark Lobodzinski | 7234629 | 2015-07-02 16:49:40 -0600 | [diff] [blame] | 308 | VkPhysicalDeviceMemoryProperties memory_properties; |
Courtney Goeltzenleuchter | 4e8246e | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 309 | |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 310 | VkFramebuffer framebuffer; |
Courtney Goeltzenleuchter | 4e8246e | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 311 | int width, height; |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 312 | VkFormat format; |
Courtney Goeltzenleuchter | 4e8246e | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 313 | |
Jon Ashburn | cedc15f | 2015-05-21 18:13:33 -0600 | [diff] [blame] | 314 | PFN_vkCreateSwapChainWSI fpCreateSwapChainWSI; |
| 315 | PFN_vkDestroySwapChainWSI fpDestroySwapChainWSI; |
| 316 | PFN_vkGetSwapChainInfoWSI fpGetSwapChainInfoWSI; |
| 317 | PFN_vkQueuePresentWSI fpQueuePresentWSI; |
Chia-I Wu | 5b66aa5 | 2015-04-16 22:02:10 +0800 | [diff] [blame] | 318 | VkSwapChainWSI swap_chain; |
Courtney Goeltzenleuchter | 4e8246e | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 319 | struct { |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 320 | VkImage image; |
Tony Barbour | 8205d90 | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 321 | VkDeviceMemory mem; |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 322 | VkCmdBuffer cmd; |
Courtney Goeltzenleuchter | 4e8246e | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 323 | |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 324 | VkColorAttachmentView view; |
Courtney Goeltzenleuchter | 4e8246e | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 325 | } buffers[DEMO_BUFFER_COUNT]; |
| 326 | |
| 327 | struct { |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 328 | VkFormat format; |
Courtney Goeltzenleuchter | 4e8246e | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 329 | |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 330 | VkImage image; |
Mark Lobodzinski | 2318261 | 2015-05-29 09:32:35 -0500 | [diff] [blame] | 331 | VkDeviceMemory mem; |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 332 | VkDepthStencilView view; |
Courtney Goeltzenleuchter | 4e8246e | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 333 | } depth; |
| 334 | |
Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 335 | struct texture_object textures[DEMO_TEXTURE_COUNT]; |
Courtney Goeltzenleuchter | 4e8246e | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 336 | |
| 337 | struct { |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 338 | VkBuffer buf; |
Mark Lobodzinski | 2318261 | 2015-05-29 09:32:35 -0500 | [diff] [blame] | 339 | VkDeviceMemory mem; |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 340 | VkBufferView view; |
Chia-I Wu | 8cd8ecd | 2015-05-25 16:27:55 +0800 | [diff] [blame] | 341 | VkDescriptorInfo desc; |
Courtney Goeltzenleuchter | 6f88d4c | 2014-10-28 10:32:57 -0600 | [diff] [blame] | 342 | } uniform_data; |
| 343 | |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 344 | VkCmdBuffer cmd; // Buffer for initialization commands |
Mark Lobodzinski | 556f721 | 2015-04-17 14:11:39 -0500 | [diff] [blame] | 345 | VkPipelineLayout pipeline_layout; |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 346 | VkDescriptorSetLayout desc_layout; |
| 347 | VkPipeline pipeline; |
Courtney Goeltzenleuchter | 4e8246e | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 348 | |
Courtney Goeltzenleuchter | fcf855f | 2015-04-10 16:24:50 -0600 | [diff] [blame] | 349 | VkDynamicVpState viewport; |
| 350 | VkDynamicRsState raster; |
| 351 | VkDynamicCbState color_blend; |
| 352 | VkDynamicDsState depth_stencil; |
Courtney Goeltzenleuchter | 4e8246e | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 353 | |
Courtney Goeltzenleuchter | 6f88d4c | 2014-10-28 10:32:57 -0600 | [diff] [blame] | 354 | mat4x4 projection_matrix; |
| 355 | mat4x4 view_matrix; |
| 356 | mat4x4 model_matrix; |
| 357 | |
Mark Lobodzinski | e2d07a5 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 358 | float spin_angle; |
| 359 | float spin_increment; |
Courtney Goeltzenleuchter | 3eeff43 | 2014-10-29 08:29:35 -0600 | [diff] [blame] | 360 | bool pause; |
| 361 | |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 362 | VkDescriptorPool desc_pool; |
| 363 | VkDescriptorSet desc_set; |
Chia-I Wu | f838506 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 364 | |
Courtney Goeltzenleuchter | 4e8246e | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 365 | bool quit; |
David Pinedo | eeca2a2 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 366 | int32_t curFrame; |
| 367 | int32_t frameCount; |
Tony Barbour | 5685ad7 | 2015-04-29 16:19:20 -0600 | [diff] [blame] | 368 | bool validate; |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 369 | PFN_vkDbgCreateMsgCallback dbgCreateMsgCallback; |
Tony Barbour | a65ecc2 | 2015-06-30 14:14:19 -0600 | [diff] [blame] | 370 | PFN_vkDbgDestroyMsgCallback dbgDestroyMsgCallback; |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 371 | VkDbgMsgCallback msg_callback; |
| 372 | |
Mark Lobodzinski | e2d07a5 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 373 | uint32_t current_buffer; |
Courtney Goeltzenleuchter | 4e8246e | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 374 | }; |
| 375 | |
Mark Lobodzinski | 7234629 | 2015-07-02 16:49:40 -0600 | [diff] [blame] | 376 | static VkResult memory_type_from_properties(struct demo *demo, uint32_t typeBits, VkFlags properties, uint32_t *typeIndex) |
| 377 | { |
| 378 | // Search memtypes to find first index with those properties |
| 379 | for (uint32_t i = 0; i < 32; i++) { |
| 380 | if ((typeBits & 1) == 1) { |
| 381 | // Type is available, does it match user properties? |
| 382 | if ((demo->memory_properties.memoryTypes[i].propertyFlags & properties) == properties) { |
| 383 | *typeIndex = i; |
| 384 | return VK_SUCCESS; |
| 385 | } |
| 386 | } |
| 387 | typeBits >>= 1; |
| 388 | } |
| 389 | // No memory types matched, return failure |
| 390 | return VK_UNSUPPORTED; |
| 391 | } |
| 392 | |
Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 393 | static void demo_flush_init_cmd(struct demo *demo) |
| 394 | { |
Tony Barbour | 22a3086 | 2015-04-22 09:02:32 -0600 | [diff] [blame] | 395 | VkResult U_ASSERT_ONLY err; |
Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 396 | |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 397 | if (demo->cmd == VK_NULL_HANDLE) |
Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 398 | return; |
| 399 | |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 400 | err = vkEndCommandBuffer(demo->cmd); |
Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 401 | assert(!err); |
| 402 | |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 403 | const VkCmdBuffer cmd_bufs[] = { demo->cmd }; |
Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 404 | |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 405 | err = vkQueueSubmit(demo->queue, 1, cmd_bufs, VK_NULL_HANDLE); |
Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 406 | assert(!err); |
| 407 | |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 408 | err = vkQueueWaitIdle(demo->queue); |
Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 409 | assert(!err); |
| 410 | |
Mike Stroyan | 230e625 | 2015-04-17 12:36:38 -0600 | [diff] [blame] | 411 | vkDestroyObject(demo->device, VK_OBJECT_TYPE_COMMAND_BUFFER, demo->cmd); |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 412 | demo->cmd = VK_NULL_HANDLE; |
Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 413 | } |
| 414 | |
Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 415 | static void demo_set_image_layout( |
| 416 | struct demo *demo, |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 417 | VkImage image, |
malnasse | 4b8ba4d | 2015-06-03 17:28:38 -0400 | [diff] [blame] | 418 | VkImageAspect aspect, |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 419 | VkImageLayout old_image_layout, |
| 420 | VkImageLayout new_image_layout) |
Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 421 | { |
Tony Barbour | 22a3086 | 2015-04-22 09:02:32 -0600 | [diff] [blame] | 422 | VkResult U_ASSERT_ONLY err; |
Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 423 | |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 424 | if (demo->cmd == VK_NULL_HANDLE) { |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 425 | const VkCmdBufferCreateInfo cmd = { |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 426 | .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO, |
Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 427 | .pNext = NULL, |
| 428 | .queueNodeIndex = demo->graphics_queue_node_index, |
Chia-I Wu | 88eaa3b | 2015-06-26 15:34:39 +0800 | [diff] [blame] | 429 | .level = VK_CMD_BUFFER_LEVEL_PRIMARY, |
Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 430 | .flags = 0, |
| 431 | }; |
| 432 | |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 433 | err = vkCreateCommandBuffer(demo->device, &cmd, &demo->cmd); |
Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 434 | assert(!err); |
| 435 | |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 436 | VkCmdBufferBeginInfo cmd_buf_info = { |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 437 | .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO, |
Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 438 | .pNext = NULL, |
Tony Barbour | 8205d90 | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 439 | .flags = VK_CMD_BUFFER_OPTIMIZE_SMALL_BATCH_BIT | |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 440 | VK_CMD_BUFFER_OPTIMIZE_ONE_TIME_SUBMIT_BIT, |
Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 441 | }; |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 442 | err = vkBeginCommandBuffer(demo->cmd, &cmd_buf_info); |
Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 443 | } |
| 444 | |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 445 | VkImageMemoryBarrier image_memory_barrier = { |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 446 | .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER, |
Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 447 | .pNext = NULL, |
| 448 | .outputMask = 0, |
| 449 | .inputMask = 0, |
| 450 | .oldLayout = old_image_layout, |
| 451 | .newLayout = new_image_layout, |
| 452 | .image = image, |
malnasse | 4b8ba4d | 2015-06-03 17:28:38 -0400 | [diff] [blame] | 453 | .subresourceRange = { aspect, 0, 1, 0, 0 } |
Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 454 | }; |
| 455 | |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 456 | if (new_image_layout == VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL) { |
Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 457 | /* Make sure anything that was copying from this image has completed */ |
Courtney Goeltzenleuchter | ad87081 | 2015-04-15 15:29:59 -0600 | [diff] [blame] | 458 | image_memory_barrier.inputMask = VK_MEMORY_INPUT_TRANSFER_BIT; |
Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 459 | } |
| 460 | |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 461 | if (new_image_layout == VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL) { |
Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 462 | /* Make sure any Copy or CPU writes to image are flushed */ |
Courtney Goeltzenleuchter | a569a50 | 2015-04-29 17:16:21 -0600 | [diff] [blame] | 463 | image_memory_barrier.outputMask = VK_MEMORY_OUTPUT_HOST_WRITE_BIT | VK_MEMORY_OUTPUT_TRANSFER_BIT; |
Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 464 | } |
| 465 | |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 466 | VkImageMemoryBarrier *pmemory_barrier = &image_memory_barrier; |
Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 467 | |
Tony Barbour | c2e987e | 2015-06-29 16:20:35 -0600 | [diff] [blame] | 468 | VkPipelineStageFlags src_stages = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT; |
| 469 | VkPipelineStageFlags dest_stages = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT; |
Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 470 | |
Tony Barbour | c2e987e | 2015-06-29 16:20:35 -0600 | [diff] [blame] | 471 | vkCmdPipelineBarrier(demo->cmd, src_stages, dest_stages, false, 1, (const void **)&pmemory_barrier); |
Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 472 | } |
| 473 | |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 474 | static void demo_draw_build_cmd(struct demo *demo, VkCmdBuffer cmd_buf) |
Courtney Goeltzenleuchter | 4e8246e | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 475 | { |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 476 | const VkColorAttachmentBindInfo color_attachment = { |
Courtney Goeltzenleuchter | 4e8246e | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 477 | .view = demo->buffers[demo->current_buffer].view, |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 478 | .layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, |
Courtney Goeltzenleuchter | 4e8246e | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 479 | }; |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 480 | const VkDepthStencilBindInfo depth_stencil = { |
Courtney Goeltzenleuchter | 4e8246e | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 481 | .view = demo->depth.view, |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 482 | .layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL, |
Courtney Goeltzenleuchter | 4e8246e | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 483 | }; |
Chris Forbes | e310597 | 2015-06-24 14:34:53 +1200 | [diff] [blame] | 484 | const VkClearColorValue clear_color = { |
| 485 | .f32 = { 0.2f, 0.2f, 0.2f, 0.2f }, |
Courtney Goeltzenleuchter | 9a1ded8 | 2015-04-03 16:35:32 -0600 | [diff] [blame] | 486 | }; |
Mark Lobodzinski | e2d07a5 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 487 | const float clear_depth = 1.0f; |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 488 | VkCmdBufferBeginInfo cmd_buf_info = { |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 489 | .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO, |
Courtney Goeltzenleuchter | e3b0f3a | 2015-04-03 15:25:24 -0600 | [diff] [blame] | 490 | .pNext = NULL, |
Tony Barbour | 8205d90 | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 491 | .flags = VK_CMD_BUFFER_OPTIMIZE_SMALL_BATCH_BIT | |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 492 | VK_CMD_BUFFER_OPTIMIZE_ONE_TIME_SUBMIT_BIT, |
Jon Ashburn | 53d27af | 2014-12-31 17:08:35 -0700 | [diff] [blame] | 493 | }; |
Tony Barbour | 22a3086 | 2015-04-22 09:02:32 -0600 | [diff] [blame] | 494 | VkResult U_ASSERT_ONLY err; |
Chris Forbes | fd8456c | 2015-06-17 11:36:12 +1200 | [diff] [blame] | 495 | VkAttachmentLoadOp load_op = VK_ATTACHMENT_LOAD_OP_CLEAR; |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 496 | VkAttachmentStoreOp store_op = VK_ATTACHMENT_STORE_OP_DONT_CARE; |
| 497 | const VkFramebufferCreateInfo fb_info = { |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 498 | .sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO, |
Jon Ashburn | 3325d6b | 2015-01-02 18:24:05 -0700 | [diff] [blame] | 499 | .pNext = NULL, |
| 500 | .colorAttachmentCount = 1, |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 501 | .pColorAttachments = (VkColorAttachmentBindInfo*) &color_attachment, |
| 502 | .pDepthStencilAttachment = (VkDepthStencilBindInfo*) &depth_stencil, |
Jon Ashburn | 3325d6b | 2015-01-02 18:24:05 -0700 | [diff] [blame] | 503 | .sampleCount = 1, |
Mark Lobodzinski | 71fcc2d | 2015-01-27 13:24:03 -0600 | [diff] [blame] | 504 | .width = demo->width, |
| 505 | .height = demo->height, |
| 506 | .layers = 1, |
Jon Ashburn | 3325d6b | 2015-01-02 18:24:05 -0700 | [diff] [blame] | 507 | }; |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 508 | VkRenderPassCreateInfo rp_info; |
| 509 | VkRenderPassBegin rp_begin; |
Jon Ashburn | 3325d6b | 2015-01-02 18:24:05 -0700 | [diff] [blame] | 510 | |
Chia-I Wu | 88eaa3b | 2015-06-26 15:34:39 +0800 | [diff] [blame] | 511 | rp_begin.contents = VK_RENDER_PASS_CONTENTS_INLINE; |
| 512 | |
Jon Ashburn | 3325d6b | 2015-01-02 18:24:05 -0700 | [diff] [blame] | 513 | memset(&rp_info, 0 , sizeof(rp_info)); |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 514 | err = vkCreateFramebuffer(demo->device, &fb_info, &rp_begin.framebuffer); |
Jon Ashburn | 3325d6b | 2015-01-02 18:24:05 -0700 | [diff] [blame] | 515 | assert(!err); |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 516 | rp_info.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO; |
Jon Ashburn | 3325d6b | 2015-01-02 18:24:05 -0700 | [diff] [blame] | 517 | rp_info.renderArea.extent.width = demo->width; |
| 518 | rp_info.renderArea.extent.height = demo->height; |
Courtney Goeltzenleuchter | e3b0f3a | 2015-04-03 15:25:24 -0600 | [diff] [blame] | 519 | rp_info.colorAttachmentCount = fb_info.colorAttachmentCount; |
| 520 | rp_info.pColorFormats = &demo->format; |
| 521 | rp_info.pColorLayouts = &color_attachment.layout; |
Jon Ashburn | 3325d6b | 2015-01-02 18:24:05 -0700 | [diff] [blame] | 522 | rp_info.pColorLoadOps = &load_op; |
| 523 | rp_info.pColorStoreOps = &store_op; |
Courtney Goeltzenleuchter | e3b0f3a | 2015-04-03 15:25:24 -0600 | [diff] [blame] | 524 | rp_info.pColorLoadClearValues = &clear_color; |
Tony Barbour | 8205d90 | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 525 | rp_info.depthStencilFormat = VK_FORMAT_D16_UNORM; |
Courtney Goeltzenleuchter | e3b0f3a | 2015-04-03 15:25:24 -0600 | [diff] [blame] | 526 | rp_info.depthStencilLayout = depth_stencil.layout; |
Chris Forbes | efc7a6f | 2015-06-22 18:47:28 +1200 | [diff] [blame] | 527 | rp_info.depthLoadOp = VK_ATTACHMENT_LOAD_OP_CLEAR; |
Courtney Goeltzenleuchter | e3b0f3a | 2015-04-03 15:25:24 -0600 | [diff] [blame] | 528 | rp_info.depthLoadClearValue = clear_depth; |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 529 | rp_info.depthStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE; |
| 530 | rp_info.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE; |
Courtney Goeltzenleuchter | e3b0f3a | 2015-04-03 15:25:24 -0600 | [diff] [blame] | 531 | rp_info.stencilLoadClearValue = 0; |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 532 | rp_info.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE; |
Tony Barbour | a65ecc2 | 2015-06-30 14:14:19 -0600 | [diff] [blame] | 533 | rp_info.sampleCount = 1; |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 534 | err = vkCreateRenderPass(demo->device, &rp_info, &rp_begin.renderPass); |
Jon Ashburn | 3325d6b | 2015-01-02 18:24:05 -0700 | [diff] [blame] | 535 | assert(!err); |
Courtney Goeltzenleuchter | 4e8246e | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 536 | |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 537 | err = vkBeginCommandBuffer(cmd_buf, &cmd_buf_info); |
Courtney Goeltzenleuchter | 4e8246e | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 538 | assert(!err); |
| 539 | |
Tobin Ehlis | e407678 | 2015-06-24 15:53:07 -0600 | [diff] [blame] | 540 | vkCmdBeginRenderPass(cmd_buf, &rp_begin); |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 541 | vkCmdBindPipeline(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS, |
Courtney Goeltzenleuchter | 4e8246e | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 542 | demo->pipeline); |
Mark Lobodzinski | a65c463 | 2015-06-15 13:21:21 -0600 | [diff] [blame] | 543 | vkCmdBindDescriptorSets(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS, demo->pipeline_layout, |
Cody Northrop | 1a01b1d | 2015-04-16 13:41:56 -0600 | [diff] [blame] | 544 | 0, 1, &demo->desc_set, 0, NULL); |
Courtney Goeltzenleuchter | 4e8246e | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 545 | |
Tony Barbour | 8205d90 | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 546 | vkCmdBindDynamicStateObject(cmd_buf, VK_STATE_BIND_POINT_VIEWPORT, demo->viewport); |
| 547 | vkCmdBindDynamicStateObject(cmd_buf, VK_STATE_BIND_POINT_RASTER, demo->raster); |
| 548 | vkCmdBindDynamicStateObject(cmd_buf, VK_STATE_BIND_POINT_COLOR_BLEND, |
Courtney Goeltzenleuchter | 4e8246e | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 549 | demo->color_blend); |
Tony Barbour | 8205d90 | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 550 | vkCmdBindDynamicStateObject(cmd_buf, VK_STATE_BIND_POINT_DEPTH_STENCIL, |
Courtney Goeltzenleuchter | 4e8246e | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 551 | demo->depth_stencil); |
| 552 | |
Chris Forbes | efc7a6f | 2015-06-22 18:47:28 +1200 | [diff] [blame] | 553 | vkCmdBeginRenderPass(cmd_buf, &rp_begin); |
Courtney Goeltzenleuchter | 6f88d4c | 2014-10-28 10:32:57 -0600 | [diff] [blame] | 554 | |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 555 | vkCmdDraw(cmd_buf, 0, 12 * 3, 0, 1); |
Chia-I Wu | 88eaa3b | 2015-06-26 15:34:39 +0800 | [diff] [blame] | 556 | vkCmdEndRenderPass(cmd_buf); |
Courtney Goeltzenleuchter | 4e8246e | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 557 | |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 558 | err = vkEndCommandBuffer(cmd_buf); |
Courtney Goeltzenleuchter | 4e8246e | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 559 | assert(!err); |
Courtney Goeltzenleuchter | 04fc2fb | 2015-02-25 17:53:18 -0700 | [diff] [blame] | 560 | |
Mike Stroyan | 230e625 | 2015-04-17 12:36:38 -0600 | [diff] [blame] | 561 | vkDestroyObject(demo->device, VK_OBJECT_TYPE_RENDER_PASS, rp_begin.renderPass); |
| 562 | vkDestroyObject(demo->device, VK_OBJECT_TYPE_FRAMEBUFFER, rp_begin.framebuffer); |
Courtney Goeltzenleuchter | 4e8246e | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 563 | } |
| 564 | |
Courtney Goeltzenleuchter | 6f88d4c | 2014-10-28 10:32:57 -0600 | [diff] [blame] | 565 | |
Courtney Goeltzenleuchter | 3eeff43 | 2014-10-29 08:29:35 -0600 | [diff] [blame] | 566 | void demo_update_data_buffer(struct demo *demo) |
Courtney Goeltzenleuchter | 4e8246e | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 567 | { |
Courtney Goeltzenleuchter | 6f88d4c | 2014-10-28 10:32:57 -0600 | [diff] [blame] | 568 | mat4x4 MVP, Model, VP; |
| 569 | int matrixSize = sizeof(MVP); |
Mark Lobodzinski | e2d07a5 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 570 | uint8_t *pData; |
Tony Barbour | 22a3086 | 2015-04-22 09:02:32 -0600 | [diff] [blame] | 571 | VkResult U_ASSERT_ONLY err; |
Courtney Goeltzenleuchter | 4e8246e | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 572 | |
Courtney Goeltzenleuchter | 6f88d4c | 2014-10-28 10:32:57 -0600 | [diff] [blame] | 573 | mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix); |
Courtney Goeltzenleuchter | 4e8246e | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 574 | |
Courtney Goeltzenleuchter | 6f88d4c | 2014-10-28 10:32:57 -0600 | [diff] [blame] | 575 | // Rotate 22.5 degrees around the Y axis |
Courtney Goeltzenleuchter | e334240 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 576 | mat4x4_dup(Model, demo->model_matrix); |
Piers Daniell | 886be47 | 2015-02-23 16:23:13 -0700 | [diff] [blame] | 577 | mat4x4_rotate(demo->model_matrix, Model, 0.0f, 1.0f, 0.0f, (float)degreesToRadians(demo->spin_angle)); |
Courtney Goeltzenleuchter | e334240 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 578 | mat4x4_mul(MVP, VP, demo->model_matrix); |
Courtney Goeltzenleuchter | 6f88d4c | 2014-10-28 10:32:57 -0600 | [diff] [blame] | 579 | |
Mark Lobodzinski | 2318261 | 2015-05-29 09:32:35 -0500 | [diff] [blame] | 580 | err = vkMapMemory(demo->device, demo->uniform_data.mem, 0, 0, 0, (void **) &pData); |
Courtney Goeltzenleuchter | 4e8246e | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 581 | assert(!err); |
| 582 | |
Courtney Goeltzenleuchter | 6f88d4c | 2014-10-28 10:32:57 -0600 | [diff] [blame] | 583 | memcpy(pData, (const void*) &MVP[0][0], matrixSize); |
Courtney Goeltzenleuchter | 4e8246e | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 584 | |
Mark Lobodzinski | 2318261 | 2015-05-29 09:32:35 -0500 | [diff] [blame] | 585 | err = vkUnmapMemory(demo->device, demo->uniform_data.mem); |
Courtney Goeltzenleuchter | 4e8246e | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 586 | assert(!err); |
| 587 | } |
| 588 | |
| 589 | static void demo_draw(struct demo *demo) |
| 590 | { |
Chia-I Wu | 5b66aa5 | 2015-04-16 22:02:10 +0800 | [diff] [blame] | 591 | const VkPresentInfoWSI present = { |
| 592 | .sType = VK_STRUCTURE_TYPE_PRESENT_INFO_WSI, |
| 593 | .pNext = NULL, |
| 594 | .image = demo->buffers[demo->current_buffer].image, |
| 595 | .flipInterval = 0, |
Courtney Goeltzenleuchter | 4e8246e | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 596 | }; |
Tony Barbour | 22a3086 | 2015-04-22 09:02:32 -0600 | [diff] [blame] | 597 | VkResult U_ASSERT_ONLY err; |
Courtney Goeltzenleuchter | 4e8246e | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 598 | |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 599 | err = vkQueueSubmit(demo->queue, 1, &demo->buffers[demo->current_buffer].cmd, |
| 600 | VK_NULL_HANDLE); |
Courtney Goeltzenleuchter | 4e8246e | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 601 | assert(!err); |
| 602 | |
Jon Ashburn | cedc15f | 2015-05-21 18:13:33 -0600 | [diff] [blame] | 603 | err = demo->fpQueuePresentWSI(demo->queue, &present); |
Courtney Goeltzenleuchter | 4e8246e | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 604 | assert(!err); |
| 605 | |
| 606 | demo->current_buffer = (demo->current_buffer + 1) % DEMO_BUFFER_COUNT; |
Chia-I Wu | 5b66aa5 | 2015-04-16 22:02:10 +0800 | [diff] [blame] | 607 | |
| 608 | err = vkQueueWaitIdle(demo->queue); |
| 609 | assert(err == VK_SUCCESS); |
Courtney Goeltzenleuchter | 4e8246e | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 610 | } |
| 611 | |
| 612 | static void demo_prepare_buffers(struct demo *demo) |
| 613 | { |
Chia-I Wu | 5b66aa5 | 2015-04-16 22:02:10 +0800 | [diff] [blame] | 614 | const VkSwapChainCreateInfoWSI swap_chain = { |
| 615 | .sType = VK_STRUCTURE_TYPE_SWAP_CHAIN_CREATE_INFO_WSI, |
| 616 | .pNext = NULL, |
| 617 | .pNativeWindowSystemHandle = demo->connection, |
| 618 | .pNativeWindowHandle = (void *) (intptr_t) demo->window, |
Ian Elliott | 32536f9 | 2015-04-21 16:41:02 -0600 | [diff] [blame] | 619 | .displayCount = 1, |
Chia-I Wu | 5b66aa5 | 2015-04-16 22:02:10 +0800 | [diff] [blame] | 620 | .imageCount = DEMO_BUFFER_COUNT, |
| 621 | .imageFormat = demo->format, |
| 622 | .imageExtent = { |
Courtney Goeltzenleuchter | 4e8246e | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 623 | .width = demo->width, |
| 624 | .height = demo->height, |
| 625 | }, |
Chia-I Wu | 5b66aa5 | 2015-04-16 22:02:10 +0800 | [diff] [blame] | 626 | .imageArraySize = 1, |
| 627 | .imageUsageFlags = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT, |
Courtney Goeltzenleuchter | 4e8246e | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 628 | }; |
Chia-I Wu | 5b66aa5 | 2015-04-16 22:02:10 +0800 | [diff] [blame] | 629 | VkSwapChainImageInfoWSI images[DEMO_BUFFER_COUNT]; |
| 630 | size_t images_size = sizeof(images); |
Tony Barbour | 22a3086 | 2015-04-22 09:02:32 -0600 | [diff] [blame] | 631 | VkResult U_ASSERT_ONLY err; |
Mark Lobodzinski | e2d07a5 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 632 | uint32_t i; |
Courtney Goeltzenleuchter | 4e8246e | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 633 | |
Jon Ashburn | cedc15f | 2015-05-21 18:13:33 -0600 | [diff] [blame] | 634 | err = demo->fpCreateSwapChainWSI(demo->device, &swap_chain, &demo->swap_chain); |
Chia-I Wu | 5b66aa5 | 2015-04-16 22:02:10 +0800 | [diff] [blame] | 635 | assert(!err); |
| 636 | |
Jon Ashburn | cedc15f | 2015-05-21 18:13:33 -0600 | [diff] [blame] | 637 | err = demo->fpGetSwapChainInfoWSI(demo->swap_chain, |
Chia-I Wu | 5b66aa5 | 2015-04-16 22:02:10 +0800 | [diff] [blame] | 638 | VK_SWAP_CHAIN_INFO_TYPE_PERSISTENT_IMAGES_WSI, |
| 639 | &images_size, images); |
| 640 | assert(!err && images_size == sizeof(images)); |
| 641 | |
Courtney Goeltzenleuchter | 4e8246e | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 642 | for (i = 0; i < DEMO_BUFFER_COUNT; i++) { |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 643 | VkColorAttachmentViewCreateInfo color_attachment_view = { |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 644 | .sType = VK_STRUCTURE_TYPE_COLOR_ATTACHMENT_VIEW_CREATE_INFO, |
Courtney Goeltzenleuchter | 4e8246e | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 645 | .pNext = NULL, |
| 646 | .format = demo->format, |
| 647 | .mipLevel = 0, |
| 648 | .baseArraySlice = 0, |
| 649 | .arraySize = 1, |
| 650 | }; |
| 651 | |
Chia-I Wu | 5b66aa5 | 2015-04-16 22:02:10 +0800 | [diff] [blame] | 652 | demo->buffers[i].image = images[i].image; |
| 653 | demo->buffers[i].mem = images[i].memory; |
Courtney Goeltzenleuchter | 4e8246e | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 654 | |
Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 655 | demo_set_image_layout(demo, demo->buffers[i].image, |
malnasse | 4b8ba4d | 2015-06-03 17:28:38 -0400 | [diff] [blame] | 656 | VK_IMAGE_ASPECT_COLOR, |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 657 | VK_IMAGE_LAYOUT_UNDEFINED, |
| 658 | VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL); |
Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 659 | |
Courtney Goeltzenleuchter | 4e8246e | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 660 | color_attachment_view.image = demo->buffers[i].image; |
| 661 | |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 662 | err = vkCreateColorAttachmentView(demo->device, |
Courtney Goeltzenleuchter | 4e8246e | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 663 | &color_attachment_view, &demo->buffers[i].view); |
| 664 | assert(!err); |
| 665 | } |
| 666 | } |
| 667 | |
| 668 | static void demo_prepare_depth(struct demo *demo) |
| 669 | { |
Tony Barbour | 8205d90 | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 670 | const VkFormat depth_format = VK_FORMAT_D16_UNORM; |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 671 | const VkImageCreateInfo image = { |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 672 | .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO, |
Courtney Goeltzenleuchter | 4e8246e | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 673 | .pNext = NULL, |
Tony Barbour | 8205d90 | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 674 | .imageType = VK_IMAGE_TYPE_2D, |
Courtney Goeltzenleuchter | 4e8246e | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 675 | .format = depth_format, |
| 676 | .extent = { demo->width, demo->height, 1 }, |
| 677 | .mipLevels = 1, |
| 678 | .arraySize = 1, |
| 679 | .samples = 1, |
Tony Barbour | 8205d90 | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 680 | .tiling = VK_IMAGE_TILING_OPTIMAL, |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 681 | .usage = VK_IMAGE_USAGE_DEPTH_STENCIL_BIT, |
Courtney Goeltzenleuchter | 4e8246e | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 682 | .flags = 0, |
| 683 | }; |
Courtney Goeltzenleuchter | ddcb619 | 2015-04-14 18:48:46 -0600 | [diff] [blame] | 684 | VkMemoryAllocInfo mem_alloc = { |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 685 | .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO, |
Mark Lobodzinski | 97dcd04 | 2015-04-16 08:52:00 -0500 | [diff] [blame] | 686 | .pNext = NULL, |
Courtney Goeltzenleuchter | 4e8246e | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 687 | .allocationSize = 0, |
Mark Lobodzinski | 7234629 | 2015-07-02 16:49:40 -0600 | [diff] [blame] | 688 | .memoryTypeIndex = 0, |
Courtney Goeltzenleuchter | 4e8246e | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 689 | }; |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 690 | VkDepthStencilViewCreateInfo view = { |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 691 | .sType = VK_STRUCTURE_TYPE_DEPTH_STENCIL_VIEW_CREATE_INFO, |
Courtney Goeltzenleuchter | 4e8246e | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 692 | .pNext = NULL, |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 693 | .image = VK_NULL_HANDLE, |
Courtney Goeltzenleuchter | 4e8246e | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 694 | .mipLevel = 0, |
| 695 | .baseArraySlice = 0, |
| 696 | .arraySize = 1, |
| 697 | .flags = 0, |
| 698 | }; |
Mike Stroyan | 230e625 | 2015-04-17 12:36:38 -0600 | [diff] [blame] | 699 | |
Mark Lobodzinski | 2318261 | 2015-05-29 09:32:35 -0500 | [diff] [blame] | 700 | VkMemoryRequirements mem_reqs; |
Tony Barbour | 22a3086 | 2015-04-22 09:02:32 -0600 | [diff] [blame] | 701 | VkResult U_ASSERT_ONLY err; |
Courtney Goeltzenleuchter | 4e8246e | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 702 | |
| 703 | demo->depth.format = depth_format; |
| 704 | |
| 705 | /* create image */ |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 706 | err = vkCreateImage(demo->device, &image, |
Courtney Goeltzenleuchter | 4e8246e | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 707 | &demo->depth.image); |
| 708 | assert(!err); |
| 709 | |
Tony Barbour | 426b905 | 2015-06-24 16:06:58 -0600 | [diff] [blame] | 710 | err = vkGetObjectMemoryRequirements(demo->device, |
| 711 | VK_OBJECT_TYPE_IMAGE, demo->depth.image, &mem_reqs); |
Mark Lobodzinski | 7234629 | 2015-07-02 16:49:40 -0600 | [diff] [blame] | 712 | |
| 713 | mem_alloc.allocationSize = mem_reqs.size; |
| 714 | err = memory_type_from_properties(demo, |
| 715 | mem_reqs.memoryTypeBits, |
| 716 | VK_MEMORY_PROPERTY_DEVICE_ONLY, |
| 717 | &mem_alloc.memoryTypeIndex); |
| 718 | assert(!err); |
Courtney Goeltzenleuchter | 4e8246e | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 719 | |
Mark Lobodzinski | 2318261 | 2015-05-29 09:32:35 -0500 | [diff] [blame] | 720 | /* allocate memory */ |
| 721 | err = vkAllocMemory(demo->device, &mem_alloc, &demo->depth.mem); |
| 722 | assert(!err); |
Courtney Goeltzenleuchter | 4e8246e | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 723 | |
Mark Lobodzinski | 2318261 | 2015-05-29 09:32:35 -0500 | [diff] [blame] | 724 | /* bind memory */ |
| 725 | err = vkBindObjectMemory(demo->device, |
| 726 | VK_OBJECT_TYPE_IMAGE, demo->depth.image, |
| 727 | demo->depth.mem, 0); |
| 728 | assert(!err); |
Courtney Goeltzenleuchter | 4e8246e | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 729 | |
Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 730 | demo_set_image_layout(demo, demo->depth.image, |
malnasse | 4b8ba4d | 2015-06-03 17:28:38 -0400 | [diff] [blame] | 731 | VK_IMAGE_ASPECT_DEPTH, |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 732 | VK_IMAGE_LAYOUT_UNDEFINED, |
| 733 | VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL); |
Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 734 | |
Courtney Goeltzenleuchter | 4e8246e | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 735 | /* create image view */ |
| 736 | view.image = demo->depth.image; |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 737 | err = vkCreateDepthStencilView(demo->device, &view, |
Courtney Goeltzenleuchter | 4e8246e | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 738 | &demo->depth.view); |
| 739 | assert(!err); |
Courtney Goeltzenleuchter | 4e8246e | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 740 | } |
| 741 | |
Courtney Goeltzenleuchter | 1722360 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 742 | /** loadTexture |
Courtney Goeltzenleuchter | cb67a32 | 2015-04-21 09:31:23 -0600 | [diff] [blame] | 743 | * loads a png file into an memory object, using cstdio , libpng. |
Courtney Goeltzenleuchter | 1722360 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 744 | * |
Courtney Goeltzenleuchter | cb67a32 | 2015-04-21 09:31:23 -0600 | [diff] [blame] | 745 | * \param demo : Needed to access VK calls |
| 746 | * \param filename : the png file to be loaded |
| 747 | * \param width : width of png, to be updated as a side effect of this function |
| 748 | * \param height : height of png, to be updated as a side effect of this function |
Courtney Goeltzenleuchter | 1722360 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 749 | * |
Courtney Goeltzenleuchter | cb67a32 | 2015-04-21 09:31:23 -0600 | [diff] [blame] | 750 | * \return bool : an opengl texture id. true if successful?, |
| 751 | * should be validated by the client of this function. |
Courtney Goeltzenleuchter | 1722360 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 752 | * |
| 753 | * Source: http://en.wikibooks.org/wiki/OpenGL_Programming/Intermediate/Textures |
| 754 | * Modified to copy image to memory |
| 755 | * |
| 756 | */ |
Courtney Goeltzenleuchter | 40a8b0a | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 757 | bool loadTexture(const char *filename, uint8_t *rgba_data, |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 758 | VkSubresourceLayout *layout, |
Mark Lobodzinski | e2d07a5 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 759 | int32_t *width, int32_t *height) |
Courtney Goeltzenleuchter | 1722360 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 760 | { |
| 761 | //header for testing if it is a png |
| 762 | png_byte header[8]; |
Tony Barbour | a938abb | 2015-04-22 11:36:22 -0600 | [diff] [blame] | 763 | int is_png, bit_depth, color_type, rowbytes; |
| 764 | size_t retval; |
Ian Elliott | 642f892 | 2015-02-13 14:29:21 -0700 | [diff] [blame] | 765 | png_uint_32 i, twidth, theight; |
Courtney Goeltzenleuchter | 1722360 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 766 | png_structp png_ptr; |
| 767 | png_infop info_ptr, end_info; |
| 768 | png_byte *image_data; |
| 769 | png_bytep *row_pointers; |
| 770 | |
| 771 | //open file as binary |
| 772 | FILE *fp = fopen(filename, "rb"); |
| 773 | if (!fp) { |
| 774 | return false; |
| 775 | } |
| 776 | |
| 777 | //read the header |
Tony Barbour | 22a3086 | 2015-04-22 09:02:32 -0600 | [diff] [blame] | 778 | retval = fread(header, 1, 8, fp); |
| 779 | if (retval != 8) { |
| 780 | fclose(fp); |
| 781 | return false; |
| 782 | } |
Courtney Goeltzenleuchter | 1722360 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 783 | |
| 784 | //test if png |
| 785 | is_png = !png_sig_cmp(header, 0, 8); |
| 786 | if (!is_png) { |
| 787 | fclose(fp); |
| 788 | return false; |
| 789 | } |
| 790 | |
| 791 | //create png struct |
| 792 | png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, |
| 793 | NULL, NULL); |
| 794 | if (!png_ptr) { |
| 795 | fclose(fp); |
| 796 | return (false); |
| 797 | } |
| 798 | |
| 799 | //create png info struct |
| 800 | info_ptr = png_create_info_struct(png_ptr); |
| 801 | if (!info_ptr) { |
| 802 | png_destroy_read_struct(&png_ptr, (png_infopp) NULL, (png_infopp) NULL); |
| 803 | fclose(fp); |
| 804 | return (false); |
| 805 | } |
| 806 | |
| 807 | //create png info struct |
| 808 | end_info = png_create_info_struct(png_ptr); |
| 809 | if (!end_info) { |
| 810 | png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp) NULL); |
| 811 | fclose(fp); |
| 812 | return (false); |
| 813 | } |
| 814 | |
| 815 | //png error stuff, not sure libpng man suggests this. |
| 816 | if (setjmp(png_jmpbuf(png_ptr))) { |
| 817 | png_destroy_read_struct(&png_ptr, &info_ptr, &end_info); |
| 818 | fclose(fp); |
| 819 | return (false); |
| 820 | } |
| 821 | |
| 822 | //init png reading |
| 823 | png_init_io(png_ptr, fp); |
| 824 | |
| 825 | //let libpng know you already read the first 8 bytes |
| 826 | png_set_sig_bytes(png_ptr, 8); |
| 827 | |
| 828 | // read all the info up to the image data |
| 829 | png_read_info(png_ptr, info_ptr); |
| 830 | |
| 831 | // get info about png |
| 832 | png_get_IHDR(png_ptr, info_ptr, &twidth, &theight, &bit_depth, &color_type, |
| 833 | NULL, NULL, NULL); |
| 834 | |
| 835 | //update width and height based on png info |
| 836 | *width = twidth; |
| 837 | *height = theight; |
| 838 | |
| 839 | // Require that incoming texture be 8bits per color component |
| 840 | // and 4 components (RGBA). |
| 841 | if (png_get_bit_depth(png_ptr, info_ptr) != 8 || |
| 842 | png_get_channels(png_ptr, info_ptr) != 4) { |
| 843 | return false; |
| 844 | } |
| 845 | |
| 846 | if (rgba_data == NULL) { |
| 847 | // If data pointer is null, we just want the width & height |
| 848 | // clean up memory and close stuff |
| 849 | png_destroy_read_struct(&png_ptr, &info_ptr, &end_info); |
| 850 | fclose(fp); |
| 851 | |
| 852 | return true; |
| 853 | } |
| 854 | |
| 855 | // Update the png info struct. |
| 856 | png_read_update_info(png_ptr, info_ptr); |
| 857 | |
| 858 | // Row size in bytes. |
| 859 | rowbytes = png_get_rowbytes(png_ptr, info_ptr); |
| 860 | |
| 861 | // Allocate the image_data as a big block, to be given to opengl |
| 862 | image_data = (png_byte *)malloc(rowbytes * theight * sizeof(png_byte)); |
| 863 | if (!image_data) { |
| 864 | //clean up memory and close stuff |
| 865 | png_destroy_read_struct(&png_ptr, &info_ptr, &end_info); |
| 866 | fclose(fp); |
| 867 | return false; |
| 868 | } |
| 869 | |
| 870 | // row_pointers is for pointing to image_data for reading the png with libpng |
| 871 | row_pointers = (png_bytep *)malloc(theight * sizeof(png_bytep)); |
| 872 | if (!row_pointers) { |
| 873 | //clean up memory and close stuff |
| 874 | png_destroy_read_struct(&png_ptr, &info_ptr, &end_info); |
| 875 | // delete[] image_data; |
| 876 | fclose(fp); |
| 877 | return false; |
| 878 | } |
| 879 | // set the individual row_pointers to point at the correct offsets of image_data |
| 880 | for (i = 0; i < theight; ++i) |
Courtney Goeltzenleuchter | 40a8b0a | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 881 | row_pointers[theight - 1 - i] = rgba_data + i * layout->rowPitch; |
Courtney Goeltzenleuchter | 1722360 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 882 | |
| 883 | // read the png into image_data through row_pointers |
| 884 | png_read_image(png_ptr, row_pointers); |
| 885 | |
| 886 | // clean up memory and close stuff |
| 887 | png_destroy_read_struct(&png_ptr, &info_ptr, &end_info); |
| 888 | free(row_pointers); |
| 889 | free(image_data); |
| 890 | fclose(fp); |
| 891 | |
| 892 | return true; |
| 893 | } |
Courtney Goeltzenleuchter | 1722360 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 894 | |
Courtney Goeltzenleuchter | 40a8b0a | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 895 | static void demo_prepare_texture_image(struct demo *demo, |
| 896 | const char *filename, |
Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 897 | struct texture_object *tex_obj, |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 898 | VkImageTiling tiling, |
Courtney Goeltzenleuchter | cb67a32 | 2015-04-21 09:31:23 -0600 | [diff] [blame] | 899 | VkImageUsageFlags usage, |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 900 | VkFlags mem_props) |
Courtney Goeltzenleuchter | 4e8246e | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 901 | { |
Mike Stroyan | de24a6f | 2015-06-15 14:21:03 -0600 | [diff] [blame] | 902 | const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM; |
Mark Lobodzinski | e2d07a5 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 903 | int32_t tex_width; |
| 904 | int32_t tex_height; |
Tony Barbour | 22a3086 | 2015-04-22 09:02:32 -0600 | [diff] [blame] | 905 | VkResult U_ASSERT_ONLY err; |
Courtney Goeltzenleuchter | 40a8b0a | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 906 | |
David Pinedo | 61e7738 | 2015-04-23 08:16:57 -0600 | [diff] [blame] | 907 | if (!loadTexture(filename, NULL, NULL, &tex_width, &tex_height)) |
| 908 | { |
| 909 | printf("Failed to load textures\n"); |
| 910 | fflush(stdout); |
| 911 | exit(1); |
| 912 | } |
Courtney Goeltzenleuchter | 40a8b0a | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 913 | |
Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 914 | tex_obj->tex_width = tex_width; |
| 915 | tex_obj->tex_height = tex_height; |
Courtney Goeltzenleuchter | 40a8b0a | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 916 | |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 917 | const VkImageCreateInfo image_create_info = { |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 918 | .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO, |
Courtney Goeltzenleuchter | 40a8b0a | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 919 | .pNext = NULL, |
Tony Barbour | 8205d90 | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 920 | .imageType = VK_IMAGE_TYPE_2D, |
Courtney Goeltzenleuchter | 40a8b0a | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 921 | .format = tex_format, |
| 922 | .extent = { tex_width, tex_height, 1 }, |
| 923 | .mipLevels = 1, |
| 924 | .arraySize = 1, |
| 925 | .samples = 1, |
| 926 | .tiling = tiling, |
Courtney Goeltzenleuchter | cb67a32 | 2015-04-21 09:31:23 -0600 | [diff] [blame] | 927 | .usage = usage, |
Courtney Goeltzenleuchter | 40a8b0a | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 928 | .flags = 0, |
| 929 | }; |
Courtney Goeltzenleuchter | ddcb619 | 2015-04-14 18:48:46 -0600 | [diff] [blame] | 930 | VkMemoryAllocInfo mem_alloc = { |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 931 | .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO, |
Mark Lobodzinski | 97dcd04 | 2015-04-16 08:52:00 -0500 | [diff] [blame] | 932 | .pNext = NULL, |
Courtney Goeltzenleuchter | 40a8b0a | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 933 | .allocationSize = 0, |
Mark Lobodzinski | 7234629 | 2015-07-02 16:49:40 -0600 | [diff] [blame] | 934 | .memoryTypeIndex = 0, |
Courtney Goeltzenleuchter | 40a8b0a | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 935 | }; |
| 936 | |
Mark Lobodzinski | 2318261 | 2015-05-29 09:32:35 -0500 | [diff] [blame] | 937 | VkMemoryRequirements mem_reqs; |
Courtney Goeltzenleuchter | 40a8b0a | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 938 | |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 939 | err = vkCreateImage(demo->device, &image_create_info, |
Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 940 | &tex_obj->image); |
Courtney Goeltzenleuchter | 40a8b0a | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 941 | assert(!err); |
| 942 | |
Tony Barbour | 426b905 | 2015-06-24 16:06:58 -0600 | [diff] [blame] | 943 | err = vkGetObjectMemoryRequirements(demo->device, |
| 944 | VK_OBJECT_TYPE_IMAGE, tex_obj->image, &mem_reqs); |
| 945 | assert(!err); |
Piers Daniell | 886be47 | 2015-02-23 16:23:13 -0700 | [diff] [blame] | 946 | |
Mark Lobodzinski | 2318261 | 2015-05-29 09:32:35 -0500 | [diff] [blame] | 947 | mem_alloc.allocationSize = mem_reqs.size; |
Courtney Goeltzenleuchter | 40a8b0a | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 948 | |
Mark Lobodzinski | 7234629 | 2015-07-02 16:49:40 -0600 | [diff] [blame] | 949 | err = memory_type_from_properties(demo, mem_reqs.memoryTypeBits, mem_props, &mem_alloc.memoryTypeIndex); |
| 950 | assert(!err); |
| 951 | |
Mark Lobodzinski | 2318261 | 2015-05-29 09:32:35 -0500 | [diff] [blame] | 952 | /* allocate memory */ |
| 953 | err = vkAllocMemory(demo->device, &mem_alloc, |
| 954 | &(tex_obj->mem)); |
| 955 | assert(!err); |
Courtney Goeltzenleuchter | 40a8b0a | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 956 | |
Mark Lobodzinski | 2318261 | 2015-05-29 09:32:35 -0500 | [diff] [blame] | 957 | /* bind memory */ |
| 958 | err = vkBindObjectMemory(demo->device, |
| 959 | VK_OBJECT_TYPE_IMAGE, tex_obj->image, |
| 960 | tex_obj->mem, 0); |
| 961 | assert(!err); |
Courtney Goeltzenleuchter | 40a8b0a | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 962 | |
Tony Barbour | 8205d90 | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 963 | if (mem_props & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) { |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 964 | const VkImageSubresource subres = { |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 965 | .aspect = VK_IMAGE_ASPECT_COLOR, |
Courtney Goeltzenleuchter | 40a8b0a | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 966 | .mipLevel = 0, |
| 967 | .arraySlice = 0, |
| 968 | }; |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 969 | VkSubresourceLayout layout; |
Courtney Goeltzenleuchter | 40a8b0a | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 970 | void *data; |
| 971 | |
Tony Barbour | 426b905 | 2015-06-24 16:06:58 -0600 | [diff] [blame] | 972 | err = vkGetImageSubresourceLayout(demo->device, tex_obj->image, &subres, &layout); |
| 973 | assert(!err); |
Courtney Goeltzenleuchter | 40a8b0a | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 974 | |
Mark Lobodzinski | 2318261 | 2015-05-29 09:32:35 -0500 | [diff] [blame] | 975 | err = vkMapMemory(demo->device, tex_obj->mem, 0, 0, 0, &data); |
Courtney Goeltzenleuchter | 40a8b0a | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 976 | assert(!err); |
| 977 | |
| 978 | if (!loadTexture(filename, data, &layout, &tex_width, &tex_height)) { |
| 979 | fprintf(stderr, "Error loading texture: %s\n", filename); |
| 980 | } |
| 981 | |
Mark Lobodzinski | 2318261 | 2015-05-29 09:32:35 -0500 | [diff] [blame] | 982 | err = vkUnmapMemory(demo->device, tex_obj->mem); |
Courtney Goeltzenleuchter | 40a8b0a | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 983 | assert(!err); |
| 984 | } |
Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 985 | |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 986 | tex_obj->imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL; |
Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 987 | demo_set_image_layout(demo, tex_obj->image, |
malnasse | 4b8ba4d | 2015-06-03 17:28:38 -0400 | [diff] [blame] | 988 | VK_IMAGE_ASPECT_COLOR, |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 989 | VK_IMAGE_LAYOUT_UNDEFINED, |
Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 990 | tex_obj->imageLayout); |
| 991 | /* setting the image layout does not reference the actual memory so no need to add a mem ref */ |
Courtney Goeltzenleuchter | 40a8b0a | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 992 | } |
| 993 | |
Mark Lobodzinski | cf26e07 | 2015-04-16 11:44:05 -0500 | [diff] [blame] | 994 | static void demo_destroy_texture_image(struct demo *demo, struct texture_object *tex_objs) |
Courtney Goeltzenleuchter | 40a8b0a | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 995 | { |
| 996 | /* clean up staging resources */ |
Mark Lobodzinski | 2318261 | 2015-05-29 09:32:35 -0500 | [diff] [blame] | 997 | vkFreeMemory(demo->device, tex_objs->mem); |
Mike Stroyan | 230e625 | 2015-04-17 12:36:38 -0600 | [diff] [blame] | 998 | vkDestroyObject(demo->device, VK_OBJECT_TYPE_IMAGE, tex_objs->image); |
Courtney Goeltzenleuchter | 40a8b0a | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 999 | } |
| 1000 | |
| 1001 | static void demo_prepare_textures(struct demo *demo) |
| 1002 | { |
Tony Barbour | 8205d90 | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 1003 | const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM; |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 1004 | VkFormatProperties props; |
Tony Barbour | 22a3086 | 2015-04-22 09:02:32 -0600 | [diff] [blame] | 1005 | VkResult U_ASSERT_ONLY err; |
Mark Lobodzinski | e2d07a5 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 1006 | uint32_t i; |
Courtney Goeltzenleuchter | 4e8246e | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1007 | |
Chris Forbes | d757630 | 2015-06-21 22:55:02 +1200 | [diff] [blame] | 1008 | err = vkGetPhysicalDeviceFormatInfo(demo->gpu, tex_format, &props); |
Courtney Goeltzenleuchter | 40a8b0a | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 1009 | assert(!err); |
| 1010 | |
Courtney Goeltzenleuchter | 4e8246e | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1011 | for (i = 0; i < DEMO_TEXTURE_COUNT; i++) { |
Courtney Goeltzenleuchter | 40a8b0a | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 1012 | |
FslNopper | 434db6a | 2015-05-06 21:42:01 +0200 | [diff] [blame] | 1013 | if ((props.linearTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) && !demo->use_staging_buffer) { |
Courtney Goeltzenleuchter | 40a8b0a | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 1014 | /* Device can texture using linear textures */ |
| 1015 | demo_prepare_texture_image(demo, tex_files[i], &demo->textures[i], |
Courtney Goeltzenleuchter | cb67a32 | 2015-04-21 09:31:23 -0600 | [diff] [blame] | 1016 | VK_IMAGE_TILING_LINEAR, VK_IMAGE_USAGE_SAMPLED_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT); |
Tony Barbour | 8205d90 | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 1017 | } else if (props.optimalTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) { |
Courtney Goeltzenleuchter | 40a8b0a | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 1018 | /* Must use staging buffer to copy linear texture to optimized */ |
Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 1019 | struct texture_object staging_texture; |
Courtney Goeltzenleuchter | 40a8b0a | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 1020 | |
| 1021 | memset(&staging_texture, 0, sizeof(staging_texture)); |
| 1022 | demo_prepare_texture_image(demo, tex_files[i], &staging_texture, |
Courtney Goeltzenleuchter | cb67a32 | 2015-04-21 09:31:23 -0600 | [diff] [blame] | 1023 | VK_IMAGE_TILING_LINEAR, VK_IMAGE_USAGE_TRANSFER_SOURCE_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT); |
Courtney Goeltzenleuchter | 40a8b0a | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 1024 | |
| 1025 | demo_prepare_texture_image(demo, tex_files[i], &demo->textures[i], |
Courtney Goeltzenleuchter | cb67a32 | 2015-04-21 09:31:23 -0600 | [diff] [blame] | 1026 | VK_IMAGE_TILING_OPTIMAL, |
| 1027 | (VK_IMAGE_USAGE_TRANSFER_DESTINATION_BIT | VK_IMAGE_USAGE_SAMPLED_BIT), |
| 1028 | VK_MEMORY_PROPERTY_DEVICE_ONLY); |
Courtney Goeltzenleuchter | 40a8b0a | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 1029 | |
Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 1030 | demo_set_image_layout(demo, staging_texture.image, |
malnasse | 4b8ba4d | 2015-06-03 17:28:38 -0400 | [diff] [blame] | 1031 | VK_IMAGE_ASPECT_COLOR, |
Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 1032 | staging_texture.imageLayout, |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1033 | VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL); |
Courtney Goeltzenleuchter | 40a8b0a | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 1034 | |
Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 1035 | demo_set_image_layout(demo, demo->textures[i].image, |
malnasse | 4b8ba4d | 2015-06-03 17:28:38 -0400 | [diff] [blame] | 1036 | VK_IMAGE_ASPECT_COLOR, |
Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 1037 | demo->textures[i].imageLayout, |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1038 | VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL); |
Courtney Goeltzenleuchter | 40a8b0a | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 1039 | |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 1040 | VkImageCopy copy_region = { |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1041 | .srcSubresource = { VK_IMAGE_ASPECT_COLOR, 0, 0 }, |
Courtney Goeltzenleuchter | 40a8b0a | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 1042 | .srcOffset = { 0, 0, 0 }, |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1043 | .destSubresource = { VK_IMAGE_ASPECT_COLOR, 0, 0 }, |
Courtney Goeltzenleuchter | 40a8b0a | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 1044 | .destOffset = { 0, 0, 0 }, |
| 1045 | .extent = { staging_texture.tex_width, staging_texture.tex_height, 1 }, |
| 1046 | }; |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1047 | vkCmdCopyImage(demo->cmd, |
| 1048 | staging_texture.image, VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL, |
| 1049 | demo->textures[i].image, VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL, |
Courtney Goeltzenleuchter | 51cbf30 | 2015-03-25 11:25:10 -0600 | [diff] [blame] | 1050 | 1, ©_region); |
Courtney Goeltzenleuchter | 40a8b0a | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 1051 | |
Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 1052 | demo_set_image_layout(demo, demo->textures[i].image, |
malnasse | 4b8ba4d | 2015-06-03 17:28:38 -0400 | [diff] [blame] | 1053 | VK_IMAGE_ASPECT_COLOR, |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1054 | VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL, |
Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 1055 | demo->textures[i].imageLayout); |
Courtney Goeltzenleuchter | 40a8b0a | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 1056 | |
Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 1057 | demo_flush_init_cmd(demo); |
Courtney Goeltzenleuchter | 40a8b0a | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 1058 | |
Courtney Goeltzenleuchter | 876629f | 2015-04-21 09:30:03 -0600 | [diff] [blame] | 1059 | demo_destroy_texture_image(demo, &staging_texture); |
Courtney Goeltzenleuchter | 40a8b0a | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 1060 | } else { |
Mike Stroyan | de24a6f | 2015-06-15 14:21:03 -0600 | [diff] [blame] | 1061 | /* Can't support VK_FORMAT_R8G8B8A8_UNORM !? */ |
| 1062 | assert(!"No support for R8G8B8A8_UNORM as texture image format"); |
Courtney Goeltzenleuchter | 40a8b0a | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 1063 | } |
| 1064 | |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 1065 | const VkSamplerCreateInfo sampler = { |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1066 | .sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO, |
Courtney Goeltzenleuchter | 4e8246e | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1067 | .pNext = NULL, |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1068 | .magFilter = VK_TEX_FILTER_NEAREST, |
| 1069 | .minFilter = VK_TEX_FILTER_NEAREST, |
Tony Barbour | 8205d90 | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 1070 | .mipMode = VK_TEX_MIPMAP_MODE_BASE, |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1071 | .addressU = VK_TEX_ADDRESS_CLAMP, |
| 1072 | .addressV = VK_TEX_ADDRESS_CLAMP, |
| 1073 | .addressW = VK_TEX_ADDRESS_CLAMP, |
Courtney Goeltzenleuchter | 4e8246e | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1074 | .mipLodBias = 0.0f, |
Courtney Goeltzenleuchter | 40a8b0a | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 1075 | .maxAnisotropy = 1, |
Tony Barbour | 8205d90 | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 1076 | .compareOp = VK_COMPARE_OP_NEVER, |
Courtney Goeltzenleuchter | 4e8246e | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1077 | .minLod = 0.0f, |
| 1078 | .maxLod = 0.0f, |
Tony Barbour | 2c4e7c7 | 2015-06-25 16:56:44 -0600 | [diff] [blame] | 1079 | .borderColor = VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE, |
Courtney Goeltzenleuchter | 4e8246e | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1080 | }; |
Courtney Goeltzenleuchter | 1722360 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 1081 | |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 1082 | VkImageViewCreateInfo view = { |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1083 | .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO, |
Courtney Goeltzenleuchter | 4e8246e | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1084 | .pNext = NULL, |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1085 | .image = VK_NULL_HANDLE, |
Tony Barbour | 8205d90 | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 1086 | .viewType = VK_IMAGE_VIEW_TYPE_2D, |
Courtney Goeltzenleuchter | 40a8b0a | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 1087 | .format = tex_format, |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1088 | .channels = { VK_CHANNEL_SWIZZLE_R, |
| 1089 | VK_CHANNEL_SWIZZLE_G, |
| 1090 | VK_CHANNEL_SWIZZLE_B, |
| 1091 | VK_CHANNEL_SWIZZLE_A, }, |
| 1092 | .subresourceRange = { VK_IMAGE_ASPECT_COLOR, 0, 1, 0, 1 }, |
Courtney Goeltzenleuchter | 4e8246e | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1093 | }; |
Jon Ashburn | a9ae383 | 2015-01-16 09:37:43 -0700 | [diff] [blame] | 1094 | |
Courtney Goeltzenleuchter | 4e8246e | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1095 | /* create sampler */ |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1096 | err = vkCreateSampler(demo->device, &sampler, |
Courtney Goeltzenleuchter | 4e8246e | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1097 | &demo->textures[i].sampler); |
| 1098 | assert(!err); |
| 1099 | |
Courtney Goeltzenleuchter | 4e8246e | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1100 | /* create image view */ |
| 1101 | view.image = demo->textures[i].image; |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1102 | err = vkCreateImageView(demo->device, &view, |
Courtney Goeltzenleuchter | 4e8246e | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1103 | &demo->textures[i].view); |
| 1104 | assert(!err); |
| 1105 | } |
Courtney Goeltzenleuchter | 4e8246e | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1106 | } |
Courtney Goeltzenleuchter | 4e8246e | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1107 | |
Courtney Goeltzenleuchter | e334240 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 1108 | void demo_prepare_cube_data_buffer(struct demo *demo) |
| 1109 | { |
Courtney Goeltzenleuchter | ddcb619 | 2015-04-14 18:48:46 -0600 | [diff] [blame] | 1110 | VkBufferCreateInfo buf_info; |
| 1111 | VkBufferViewCreateInfo view_info; |
Courtney Goeltzenleuchter | ddcb619 | 2015-04-14 18:48:46 -0600 | [diff] [blame] | 1112 | VkMemoryAllocInfo alloc_info = { |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1113 | .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO, |
Mark Lobodzinski | 97dcd04 | 2015-04-16 08:52:00 -0500 | [diff] [blame] | 1114 | .pNext = NULL, |
Jon Ashburn | c6ae13d | 2015-01-19 15:00:26 -0700 | [diff] [blame] | 1115 | .allocationSize = 0, |
Mark Lobodzinski | 7234629 | 2015-07-02 16:49:40 -0600 | [diff] [blame] | 1116 | .memoryTypeIndex = 0, |
Jon Ashburn | c6ae13d | 2015-01-19 15:00:26 -0700 | [diff] [blame] | 1117 | }; |
Mark Lobodzinski | 2318261 | 2015-05-29 09:32:35 -0500 | [diff] [blame] | 1118 | VkMemoryRequirements mem_reqs; |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 1119 | size_t mem_reqs_size = sizeof(VkMemoryRequirements); |
Mark Lobodzinski | e2d07a5 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 1120 | uint8_t *pData; |
Courtney Goeltzenleuchter | e334240 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 1121 | int i; |
| 1122 | mat4x4 MVP, VP; |
Tony Barbour | 22a3086 | 2015-04-22 09:02:32 -0600 | [diff] [blame] | 1123 | VkResult U_ASSERT_ONLY err; |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1124 | struct vktexcube_vs_uniform data; |
Courtney Goeltzenleuchter | e334240 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 1125 | |
Courtney Goeltzenleuchter | e334240 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 1126 | mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix); |
Courtney Goeltzenleuchter | e334240 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 1127 | mat4x4_mul(MVP, VP, demo->model_matrix); |
| 1128 | memcpy(data.mvp, MVP, sizeof(MVP)); |
Courtney Goeltzenleuchter | 3eeff43 | 2014-10-29 08:29:35 -0600 | [diff] [blame] | 1129 | // dumpMatrix("MVP", MVP); |
Courtney Goeltzenleuchter | e334240 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 1130 | |
| 1131 | for (i=0; i<12*3; i++) { |
| 1132 | data.position[i][0] = g_vertex_buffer_data[i*3]; |
| 1133 | data.position[i][1] = g_vertex_buffer_data[i*3+1]; |
| 1134 | data.position[i][2] = g_vertex_buffer_data[i*3+2]; |
| 1135 | data.position[i][3] = 1.0f; |
| 1136 | data.attr[i][0] = g_uv_buffer_data[2*i]; |
| 1137 | data.attr[i][1] = g_uv_buffer_data[2*i + 1]; |
| 1138 | data.attr[i][2] = 0; |
| 1139 | data.attr[i][3] = 0; |
| 1140 | } |
| 1141 | |
Chia-I Wu | 714df45 | 2015-01-01 07:55:04 +0800 | [diff] [blame] | 1142 | memset(&buf_info, 0, sizeof(buf_info)); |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1143 | buf_info.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO; |
Courtney Goeltzenleuchter | ad87081 | 2015-04-15 15:29:59 -0600 | [diff] [blame] | 1144 | buf_info.usage = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT; |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1145 | err = vkCreateBuffer(demo->device, &buf_info, &demo->uniform_data.buf); |
Chia-I Wu | 714df45 | 2015-01-01 07:55:04 +0800 | [diff] [blame] | 1146 | assert(!err); |
| 1147 | |
Tony Barbour | 426b905 | 2015-06-24 16:06:58 -0600 | [diff] [blame] | 1148 | err = vkGetObjectMemoryRequirements(demo->device, |
| 1149 | VK_OBJECT_TYPE_BUFFER, demo->uniform_data.buf, &mem_reqs); |
Mark Lobodzinski | 2318261 | 2015-05-29 09:32:35 -0500 | [diff] [blame] | 1150 | assert(!err && mem_reqs_size == sizeof(mem_reqs)); |
Chia-I Wu | 714df45 | 2015-01-01 07:55:04 +0800 | [diff] [blame] | 1151 | |
Mark Lobodzinski | 2318261 | 2015-05-29 09:32:35 -0500 | [diff] [blame] | 1152 | alloc_info.allocationSize = mem_reqs.size; |
Mark Lobodzinski | 7234629 | 2015-07-02 16:49:40 -0600 | [diff] [blame] | 1153 | err = memory_type_from_properties(demo, |
| 1154 | mem_reqs.memoryTypeBits, |
| 1155 | VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, |
| 1156 | &alloc_info.memoryTypeIndex); |
| 1157 | assert(!err); |
Courtney Goeltzenleuchter | e334240 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 1158 | |
Mark Lobodzinski | 2318261 | 2015-05-29 09:32:35 -0500 | [diff] [blame] | 1159 | err = vkAllocMemory(demo->device, &alloc_info, &(demo->uniform_data.mem)); |
| 1160 | assert(!err); |
Courtney Goeltzenleuchter | e334240 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 1161 | |
Mark Lobodzinski | 2318261 | 2015-05-29 09:32:35 -0500 | [diff] [blame] | 1162 | err = vkMapMemory(demo->device, demo->uniform_data.mem, 0, 0, 0, (void **) &pData); |
| 1163 | assert(!err); |
Courtney Goeltzenleuchter | e334240 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 1164 | |
Mark Lobodzinski | 2318261 | 2015-05-29 09:32:35 -0500 | [diff] [blame] | 1165 | memcpy(pData, &data, sizeof data); |
Courtney Goeltzenleuchter | e334240 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 1166 | |
Mark Lobodzinski | 2318261 | 2015-05-29 09:32:35 -0500 | [diff] [blame] | 1167 | err = vkUnmapMemory(demo->device, demo->uniform_data.mem); |
| 1168 | assert(!err); |
| 1169 | |
| 1170 | err = vkBindObjectMemory(demo->device, |
| 1171 | VK_OBJECT_TYPE_BUFFER, demo->uniform_data.buf, |
| 1172 | demo->uniform_data.mem, 0); |
| 1173 | assert(!err); |
Chia-I Wu | 714df45 | 2015-01-01 07:55:04 +0800 | [diff] [blame] | 1174 | |
| 1175 | memset(&view_info, 0, sizeof(view_info)); |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1176 | view_info.sType = VK_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO; |
Chia-I Wu | 714df45 | 2015-01-01 07:55:04 +0800 | [diff] [blame] | 1177 | view_info.buffer = demo->uniform_data.buf; |
Tony Barbour | 8205d90 | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 1178 | view_info.viewType = VK_BUFFER_VIEW_TYPE_RAW; |
Chia-I Wu | 714df45 | 2015-01-01 07:55:04 +0800 | [diff] [blame] | 1179 | view_info.offset = 0; |
| 1180 | view_info.range = sizeof(data); |
| 1181 | |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1182 | err = vkCreateBufferView(demo->device, &view_info, &demo->uniform_data.view); |
Chia-I Wu | 714df45 | 2015-01-01 07:55:04 +0800 | [diff] [blame] | 1183 | assert(!err); |
| 1184 | |
Chia-I Wu | 8cd8ecd | 2015-05-25 16:27:55 +0800 | [diff] [blame] | 1185 | demo->uniform_data.desc.bufferView = demo->uniform_data.view; |
Courtney Goeltzenleuchter | e334240 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 1186 | } |
| 1187 | |
Chia-I Wu | f838506 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 1188 | static void demo_prepare_descriptor_layout(struct demo *demo) |
Courtney Goeltzenleuchter | 4e8246e | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1189 | { |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 1190 | const VkDescriptorSetLayoutBinding layout_bindings[2] = { |
Chia-I Wu | fc9d913 | 2015-03-26 15:04:41 +0800 | [diff] [blame] | 1191 | [0] = { |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1192 | .descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, |
Chia-I Wu | d3114a2 | 2015-05-25 16:22:52 +0800 | [diff] [blame] | 1193 | .arraySize = 1, |
Tony Barbour | 8205d90 | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 1194 | .stageFlags = VK_SHADER_STAGE_VERTEX_BIT, |
Chia-I Wu | 310eece | 2015-03-27 12:56:09 +0800 | [diff] [blame] | 1195 | .pImmutableSamplers = NULL, |
Chia-I Wu | fc9d913 | 2015-03-26 15:04:41 +0800 | [diff] [blame] | 1196 | }, |
| 1197 | [1] = { |
Courtney Goeltzenleuchter | ad87081 | 2015-04-15 15:29:59 -0600 | [diff] [blame] | 1198 | .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, |
Chia-I Wu | d3114a2 | 2015-05-25 16:22:52 +0800 | [diff] [blame] | 1199 | .arraySize = DEMO_TEXTURE_COUNT, |
Tony Barbour | 8205d90 | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 1200 | .stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT, |
Chia-I Wu | 310eece | 2015-03-27 12:56:09 +0800 | [diff] [blame] | 1201 | .pImmutableSamplers = NULL, |
Chia-I Wu | fc9d913 | 2015-03-26 15:04:41 +0800 | [diff] [blame] | 1202 | }, |
| 1203 | }; |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 1204 | const VkDescriptorSetLayoutCreateInfo descriptor_layout = { |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1205 | .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO, |
Chia-I Wu | f838506 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 1206 | .pNext = NULL, |
Chia-I Wu | fc9d913 | 2015-03-26 15:04:41 +0800 | [diff] [blame] | 1207 | .count = 2, |
| 1208 | .pBinding = layout_bindings, |
Chia-I Wu | f838506 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 1209 | }; |
Tony Barbour | 22a3086 | 2015-04-22 09:02:32 -0600 | [diff] [blame] | 1210 | VkResult U_ASSERT_ONLY err; |
Courtney Goeltzenleuchter | 4e8246e | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1211 | |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1212 | err = vkCreateDescriptorSetLayout(demo->device, |
Chia-I Wu | 7732cb2 | 2015-03-26 15:27:55 +0800 | [diff] [blame] | 1213 | &descriptor_layout, &demo->desc_layout); |
| 1214 | assert(!err); |
| 1215 | |
Mark Lobodzinski | 556f721 | 2015-04-17 14:11:39 -0500 | [diff] [blame] | 1216 | const VkPipelineLayoutCreateInfo pPipelineLayoutCreateInfo = { |
| 1217 | .sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO, |
| 1218 | .pNext = NULL, |
| 1219 | .descriptorSetCount = 1, |
| 1220 | .pSetLayouts = &demo->desc_layout, |
| 1221 | }; |
| 1222 | |
| 1223 | err = vkCreatePipelineLayout(demo->device, |
| 1224 | &pPipelineLayoutCreateInfo, |
| 1225 | &demo->pipeline_layout); |
Courtney Goeltzenleuchter | 4e8246e | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1226 | assert(!err); |
Courtney Goeltzenleuchter | 4e8246e | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1227 | } |
| 1228 | |
Tobin Ehlis | e796a1d | 2015-07-02 11:02:49 -0600 | [diff] [blame] | 1229 | static VkShader demo_prepare_shader(struct demo* demo, |
Tony Barbour | 8205d90 | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 1230 | VkShaderStage stage, |
Tobin Ehlis | e796a1d | 2015-07-02 11:02:49 -0600 | [diff] [blame] | 1231 | const void* code, |
Mark Lobodzinski | e2d07a5 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 1232 | size_t size) |
Courtney Goeltzenleuchter | 4e8246e | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1233 | { |
Courtney Goeltzenleuchter | 0b29b0d | 2015-06-24 18:24:19 -0600 | [diff] [blame] | 1234 | VkShaderModuleCreateInfo moduleCreateInfo; |
| 1235 | VkShaderCreateInfo shaderCreateInfo; |
| 1236 | VkShaderModule shaderModule; |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 1237 | VkShader shader; |
| 1238 | VkResult err; |
Courtney Goeltzenleuchter | 4e8246e | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1239 | |
Courtney Goeltzenleuchter | 7e33498 | 2014-10-30 15:14:16 -0600 | [diff] [blame] | 1240 | |
Courtney Goeltzenleuchter | 0b29b0d | 2015-06-24 18:24:19 -0600 | [diff] [blame] | 1241 | moduleCreateInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO; |
| 1242 | moduleCreateInfo.pNext = NULL; |
| 1243 | |
| 1244 | shaderCreateInfo.sType = VK_STRUCTURE_TYPE_SHADER_CREATE_INFO; |
| 1245 | shaderCreateInfo.pNext = NULL; |
Tobin Ehlis | e796a1d | 2015-07-02 11:02:49 -0600 | [diff] [blame] | 1246 | shaderCreateInfo.pName = "main"; |
Courtney Goeltzenleuchter | 4e8246e | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1247 | |
Cody Northrop | 75db032 | 2015-05-28 11:27:16 -0600 | [diff] [blame] | 1248 | if (!demo->use_glsl) { |
Courtney Goeltzenleuchter | 0b29b0d | 2015-06-24 18:24:19 -0600 | [diff] [blame] | 1249 | moduleCreateInfo.codeSize = size; |
| 1250 | moduleCreateInfo.pCode = code; |
| 1251 | moduleCreateInfo.flags = 0; |
| 1252 | err = vkCreateShaderModule(demo->device, &moduleCreateInfo, &shaderModule); |
Cody Northrop | 75db032 | 2015-05-28 11:27:16 -0600 | [diff] [blame] | 1253 | if (err) { |
Courtney Goeltzenleuchter | 0b29b0d | 2015-06-24 18:24:19 -0600 | [diff] [blame] | 1254 | free((void *) moduleCreateInfo.pCode); |
Cody Northrop | 75db032 | 2015-05-28 11:27:16 -0600 | [diff] [blame] | 1255 | } |
Courtney Goeltzenleuchter | 0b29b0d | 2015-06-24 18:24:19 -0600 | [diff] [blame] | 1256 | |
| 1257 | shaderCreateInfo.flags = 0; |
| 1258 | shaderCreateInfo.module = shaderModule; |
| 1259 | err = vkCreateShader(demo->device, &shaderCreateInfo, &shader); |
Cody Northrop | 75db032 | 2015-05-28 11:27:16 -0600 | [diff] [blame] | 1260 | } else { |
| 1261 | // Create fake SPV structure to feed GLSL |
| 1262 | // to the driver "under the covers" |
Courtney Goeltzenleuchter | 0b29b0d | 2015-06-24 18:24:19 -0600 | [diff] [blame] | 1263 | moduleCreateInfo.codeSize = 3 * sizeof(uint32_t) + size + 1; |
| 1264 | moduleCreateInfo.pCode = malloc(moduleCreateInfo.codeSize); |
| 1265 | moduleCreateInfo.flags = 0; |
Cody Northrop | 75db032 | 2015-05-28 11:27:16 -0600 | [diff] [blame] | 1266 | |
| 1267 | /* try version 0 first: VkShaderStage followed by GLSL */ |
Courtney Goeltzenleuchter | 0b29b0d | 2015-06-24 18:24:19 -0600 | [diff] [blame] | 1268 | ((uint32_t *) moduleCreateInfo.pCode)[0] = ICD_SPV_MAGIC; |
| 1269 | ((uint32_t *) moduleCreateInfo.pCode)[1] = 0; |
| 1270 | ((uint32_t *) moduleCreateInfo.pCode)[2] = stage; |
| 1271 | memcpy(((uint32_t *) moduleCreateInfo.pCode + 3), code, size + 1); |
Cody Northrop | 75db032 | 2015-05-28 11:27:16 -0600 | [diff] [blame] | 1272 | |
Courtney Goeltzenleuchter | 0b29b0d | 2015-06-24 18:24:19 -0600 | [diff] [blame] | 1273 | err = vkCreateShaderModule(demo->device, &moduleCreateInfo, &shaderModule); |
Cody Northrop | 75db032 | 2015-05-28 11:27:16 -0600 | [diff] [blame] | 1274 | if (err) { |
Courtney Goeltzenleuchter | 0b29b0d | 2015-06-24 18:24:19 -0600 | [diff] [blame] | 1275 | free((void *) moduleCreateInfo.pCode); |
Cody Northrop | 75db032 | 2015-05-28 11:27:16 -0600 | [diff] [blame] | 1276 | } |
Courtney Goeltzenleuchter | 0b29b0d | 2015-06-24 18:24:19 -0600 | [diff] [blame] | 1277 | |
| 1278 | shaderCreateInfo.flags = 0; |
| 1279 | shaderCreateInfo.module = shaderModule; |
| 1280 | err = vkCreateShader(demo->device, &shaderCreateInfo, &shader); |
Courtney Goeltzenleuchter | 1722360 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 1281 | } |
Courtney Goeltzenleuchter | 4e8246e | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1282 | return shader; |
| 1283 | } |
| 1284 | |
Cody Northrop | acfb049 | 2015-03-17 15:55:58 -0600 | [diff] [blame] | 1285 | char *demo_read_spv(const char *filename, size_t *psize) |
Courtney Goeltzenleuchter | 7e33498 | 2014-10-30 15:14:16 -0600 | [diff] [blame] | 1286 | { |
| 1287 | long int size; |
Tony Barbour | a938abb | 2015-04-22 11:36:22 -0600 | [diff] [blame] | 1288 | size_t U_ASSERT_ONLY retval; |
Courtney Goeltzenleuchter | 7e33498 | 2014-10-30 15:14:16 -0600 | [diff] [blame] | 1289 | void *shader_code; |
| 1290 | |
| 1291 | FILE *fp = fopen(filename, "rb"); |
| 1292 | if (!fp) return NULL; |
| 1293 | |
| 1294 | fseek(fp, 0L, SEEK_END); |
| 1295 | size = ftell(fp); |
| 1296 | |
| 1297 | fseek(fp, 0L, SEEK_SET); |
| 1298 | |
| 1299 | shader_code = malloc(size); |
Tony Barbour | 22a3086 | 2015-04-22 09:02:32 -0600 | [diff] [blame] | 1300 | retval = fread(shader_code, size, 1, fp); |
| 1301 | assert(retval == 1); |
Courtney Goeltzenleuchter | 7e33498 | 2014-10-30 15:14:16 -0600 | [diff] [blame] | 1302 | |
| 1303 | *psize = size; |
| 1304 | |
| 1305 | return shader_code; |
| 1306 | } |
| 1307 | |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 1308 | static VkShader demo_prepare_vs(struct demo *demo) |
Courtney Goeltzenleuchter | 4e8246e | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1309 | { |
Cody Northrop | 75db032 | 2015-05-28 11:27:16 -0600 | [diff] [blame] | 1310 | if (!demo->use_glsl) { |
| 1311 | void *vertShaderCode; |
| 1312 | size_t size; |
Courtney Goeltzenleuchter | 7e33498 | 2014-10-30 15:14:16 -0600 | [diff] [blame] | 1313 | |
Cody Northrop | 75db032 | 2015-05-28 11:27:16 -0600 | [diff] [blame] | 1314 | vertShaderCode = demo_read_spv("cube-vert.spv", &size); |
Courtney Goeltzenleuchter | 7e33498 | 2014-10-30 15:14:16 -0600 | [diff] [blame] | 1315 | |
Cody Northrop | 75db032 | 2015-05-28 11:27:16 -0600 | [diff] [blame] | 1316 | return demo_prepare_shader(demo, VK_SHADER_STAGE_VERTEX, |
| 1317 | vertShaderCode, size); |
| 1318 | } else { |
| 1319 | static const char *vertShaderText = |
| 1320 | "#version 140\n" |
| 1321 | "#extension GL_ARB_separate_shader_objects : enable\n" |
| 1322 | "#extension GL_ARB_shading_language_420pack : enable\n" |
| 1323 | "\n" |
| 1324 | "layout(binding = 0) uniform buf {\n" |
| 1325 | " mat4 MVP;\n" |
| 1326 | " vec4 position[12*3];\n" |
| 1327 | " vec4 attr[12*3];\n" |
| 1328 | "} ubuf;\n" |
| 1329 | "\n" |
| 1330 | "layout (location = 0) out vec4 texcoord;\n" |
| 1331 | "\n" |
| 1332 | "void main() \n" |
| 1333 | "{\n" |
| 1334 | " texcoord = ubuf.attr[gl_VertexID];\n" |
| 1335 | " gl_Position = ubuf.MVP * ubuf.position[gl_VertexID];\n" |
| 1336 | "\n" |
| 1337 | " // GL->VK conventions\n" |
| 1338 | " gl_Position.y = -gl_Position.y;\n" |
| 1339 | " gl_Position.z = (gl_Position.z + gl_Position.w) / 2.0;\n" |
| 1340 | "}\n"; |
Courtney Goeltzenleuchter | 6f88d4c | 2014-10-28 10:32:57 -0600 | [diff] [blame] | 1341 | |
Cody Northrop | 75db032 | 2015-05-28 11:27:16 -0600 | [diff] [blame] | 1342 | return demo_prepare_shader(demo, VK_SHADER_STAGE_VERTEX, |
| 1343 | (const void *) vertShaderText, |
| 1344 | strlen(vertShaderText)); |
| 1345 | } |
Courtney Goeltzenleuchter | 4e8246e | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1346 | } |
| 1347 | |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 1348 | static VkShader demo_prepare_fs(struct demo *demo) |
Courtney Goeltzenleuchter | 4e8246e | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1349 | { |
Cody Northrop | 75db032 | 2015-05-28 11:27:16 -0600 | [diff] [blame] | 1350 | if (!demo->use_glsl) { |
| 1351 | void *fragShaderCode; |
| 1352 | size_t size; |
Courtney Goeltzenleuchter | 7e33498 | 2014-10-30 15:14:16 -0600 | [diff] [blame] | 1353 | |
Cody Northrop | 75db032 | 2015-05-28 11:27:16 -0600 | [diff] [blame] | 1354 | fragShaderCode = demo_read_spv("cube-frag.spv", &size); |
Courtney Goeltzenleuchter | 7e33498 | 2014-10-30 15:14:16 -0600 | [diff] [blame] | 1355 | |
Cody Northrop | 75db032 | 2015-05-28 11:27:16 -0600 | [diff] [blame] | 1356 | return demo_prepare_shader(demo, VK_SHADER_STAGE_FRAGMENT, |
| 1357 | fragShaderCode, size); |
| 1358 | } else { |
| 1359 | static const char *fragShaderText = |
| 1360 | "#version 140\n" |
| 1361 | "#extension GL_ARB_separate_shader_objects : enable\n" |
| 1362 | "#extension GL_ARB_shading_language_420pack : enable\n" |
| 1363 | "layout (binding = 1) uniform sampler2D tex;\n" |
| 1364 | "\n" |
| 1365 | "layout (location = 0) in vec4 texcoord;\n" |
| 1366 | "layout (location = 0) out vec4 uFragColor;\n" |
| 1367 | "void main() {\n" |
| 1368 | " uFragColor = texture(tex, texcoord.xy);\n" |
| 1369 | "}\n"; |
Courtney Goeltzenleuchter | 6f88d4c | 2014-10-28 10:32:57 -0600 | [diff] [blame] | 1370 | |
Cody Northrop | 75db032 | 2015-05-28 11:27:16 -0600 | [diff] [blame] | 1371 | return demo_prepare_shader(demo, VK_SHADER_STAGE_FRAGMENT, |
| 1372 | (const void *) fragShaderText, |
| 1373 | strlen(fragShaderText)); |
| 1374 | } |
Courtney Goeltzenleuchter | 4e8246e | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1375 | } |
| 1376 | |
| 1377 | static void demo_prepare_pipeline(struct demo *demo) |
| 1378 | { |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 1379 | VkGraphicsPipelineCreateInfo pipeline; |
Mark Lobodzinski | 0e0fb5c | 2015-06-23 15:11:57 -0600 | [diff] [blame] | 1380 | |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 1381 | VkPipelineIaStateCreateInfo ia; |
| 1382 | VkPipelineRsStateCreateInfo rs; |
| 1383 | VkPipelineCbStateCreateInfo cb; |
| 1384 | VkPipelineDsStateCreateInfo ds; |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 1385 | VkPipelineVpStateCreateInfo vp; |
| 1386 | VkPipelineMsStateCreateInfo ms; |
Tony Barbour | 22a3086 | 2015-04-22 09:02:32 -0600 | [diff] [blame] | 1387 | VkResult U_ASSERT_ONLY err; |
Courtney Goeltzenleuchter | 4e8246e | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1388 | |
| 1389 | memset(&pipeline, 0, sizeof(pipeline)); |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1390 | pipeline.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO; |
Mark Lobodzinski | 556f721 | 2015-04-17 14:11:39 -0500 | [diff] [blame] | 1391 | pipeline.layout = demo->pipeline_layout; |
Courtney Goeltzenleuchter | 4e8246e | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1392 | |
Courtney Goeltzenleuchter | 4e8246e | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1393 | memset(&ia, 0, sizeof(ia)); |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1394 | ia.sType = VK_STRUCTURE_TYPE_PIPELINE_IA_STATE_CREATE_INFO; |
Tony Barbour | 8205d90 | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 1395 | ia.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST; |
Courtney Goeltzenleuchter | 4e8246e | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1396 | |
| 1397 | memset(&rs, 0, sizeof(rs)); |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1398 | rs.sType = VK_STRUCTURE_TYPE_PIPELINE_RS_STATE_CREATE_INFO; |
Tony Barbour | 8205d90 | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 1399 | rs.fillMode = VK_FILL_MODE_SOLID; |
| 1400 | rs.cullMode = VK_CULL_MODE_BACK; |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1401 | rs.frontFace = VK_FRONT_FACE_CCW; |
Chia-I Wu | e2504cb | 2015-04-22 14:20:52 +0800 | [diff] [blame] | 1402 | rs.depthClipEnable = VK_TRUE; |
Courtney Goeltzenleuchter | 4e8246e | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1403 | |
| 1404 | memset(&cb, 0, sizeof(cb)); |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1405 | cb.sType = VK_STRUCTURE_TYPE_PIPELINE_CB_STATE_CREATE_INFO; |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 1406 | VkPipelineCbAttachmentState att_state[1]; |
Tony Barbour | fa6cac7 | 2015-01-16 14:27:35 -0700 | [diff] [blame] | 1407 | memset(att_state, 0, sizeof(att_state)); |
| 1408 | att_state[0].format = demo->format; |
| 1409 | att_state[0].channelWriteMask = 0xf; |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1410 | att_state[0].blendEnable = VK_FALSE; |
Tony Barbour | fa6cac7 | 2015-01-16 14:27:35 -0700 | [diff] [blame] | 1411 | cb.attachmentCount = 1; |
| 1412 | cb.pAttachments = att_state; |
Courtney Goeltzenleuchter | 4e8246e | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1413 | |
Tony Barbour | fa6cac7 | 2015-01-16 14:27:35 -0700 | [diff] [blame] | 1414 | memset(&vp, 0, sizeof(vp)); |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1415 | vp.sType = VK_STRUCTURE_TYPE_PIPELINE_VP_STATE_CREATE_INFO; |
Tony Barbour | 8205d90 | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 1416 | vp.viewportCount = 1; |
Tony Barbour | fa6cac7 | 2015-01-16 14:27:35 -0700 | [diff] [blame] | 1417 | |
| 1418 | memset(&ds, 0, sizeof(ds)); |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1419 | ds.sType = VK_STRUCTURE_TYPE_PIPELINE_DS_STATE_CREATE_INFO; |
Tony Barbour | fa6cac7 | 2015-01-16 14:27:35 -0700 | [diff] [blame] | 1420 | ds.format = demo->depth.format; |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1421 | ds.depthTestEnable = VK_TRUE; |
| 1422 | ds.depthWriteEnable = VK_TRUE; |
Tony Barbour | 8205d90 | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 1423 | ds.depthCompareOp = VK_COMPARE_OP_LESS_EQUAL; |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1424 | ds.depthBoundsEnable = VK_FALSE; |
| 1425 | ds.back.stencilFailOp = VK_STENCIL_OP_KEEP; |
| 1426 | ds.back.stencilPassOp = VK_STENCIL_OP_KEEP; |
Tony Barbour | 8205d90 | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 1427 | ds.back.stencilCompareOp = VK_COMPARE_OP_ALWAYS; |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1428 | ds.stencilTestEnable = VK_FALSE; |
Tony Barbour | fa6cac7 | 2015-01-16 14:27:35 -0700 | [diff] [blame] | 1429 | ds.front = ds.back; |
Courtney Goeltzenleuchter | 4e8246e | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1430 | |
Tony Barbour | fa6cac7 | 2015-01-16 14:27:35 -0700 | [diff] [blame] | 1431 | memset(&ms, 0, sizeof(ms)); |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1432 | ms.sType = VK_STRUCTURE_TYPE_PIPELINE_MS_STATE_CREATE_INFO; |
Tony Barbour | fa6cac7 | 2015-01-16 14:27:35 -0700 | [diff] [blame] | 1433 | ms.sampleMask = 1; |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1434 | ms.multisampleEnable = VK_FALSE; |
Tony Barbour | e094edf | 2015-06-26 10:18:34 -0600 | [diff] [blame] | 1435 | ms.rasterSamples = 1; |
Courtney Goeltzenleuchter | 4e8246e | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1436 | |
Mark Lobodzinski | 0e0fb5c | 2015-06-23 15:11:57 -0600 | [diff] [blame] | 1437 | // Two stages: vs and fs |
| 1438 | pipeline.stageCount = 2; |
| 1439 | VkPipelineShaderStageCreateInfo shaderStages[2]; |
| 1440 | memset(&shaderStages, 0, 2 * sizeof(VkPipelineShaderStageCreateInfo)); |
| 1441 | |
| 1442 | shaderStages[0].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO; |
| 1443 | shaderStages[0].stage = VK_SHADER_STAGE_VERTEX; |
| 1444 | shaderStages[0].shader = demo_prepare_vs(demo); |
| 1445 | |
| 1446 | shaderStages[1].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO; |
| 1447 | shaderStages[1].stage = VK_SHADER_STAGE_FRAGMENT; |
| 1448 | shaderStages[1].shader = demo_prepare_fs(demo); |
| 1449 | |
Mark Lobodzinski | c1e2146 | 2015-06-30 10:18:36 -0600 | [diff] [blame] | 1450 | pipeline.pVertexInputState = NULL; |
Mark Lobodzinski | 0e0fb5c | 2015-06-23 15:11:57 -0600 | [diff] [blame] | 1451 | pipeline.pIaState = &ia; |
| 1452 | pipeline.pRsState = &rs; |
| 1453 | pipeline.pCbState = &cb; |
| 1454 | pipeline.pMsState = &ms; |
| 1455 | pipeline.pVpState = &vp; |
| 1456 | pipeline.pDsState = &ds; |
| 1457 | pipeline.pStages = shaderStages; |
Courtney Goeltzenleuchter | 4e8246e | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1458 | |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1459 | err = vkCreateGraphicsPipeline(demo->device, &pipeline, &demo->pipeline); |
Courtney Goeltzenleuchter | 4e8246e | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1460 | assert(!err); |
| 1461 | |
Mark Lobodzinski | 0e0fb5c | 2015-06-23 15:11:57 -0600 | [diff] [blame] | 1462 | for (uint32_t i = 0; i < pipeline.stageCount; i++) { |
| 1463 | vkDestroyObject(demo->device, VK_OBJECT_TYPE_SHADER, shaderStages[i].shader); |
| 1464 | } |
Courtney Goeltzenleuchter | 4e8246e | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1465 | } |
| 1466 | |
| 1467 | static void demo_prepare_dynamic_states(struct demo *demo) |
| 1468 | { |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 1469 | VkDynamicVpStateCreateInfo viewport_create; |
| 1470 | VkDynamicRsStateCreateInfo raster; |
| 1471 | VkDynamicCbStateCreateInfo color_blend; |
| 1472 | VkDynamicDsStateCreateInfo depth_stencil; |
Tony Barbour | 22a3086 | 2015-04-22 09:02:32 -0600 | [diff] [blame] | 1473 | VkResult U_ASSERT_ONLY err; |
Courtney Goeltzenleuchter | 4e8246e | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1474 | |
Tony Barbour | fa6cac7 | 2015-01-16 14:27:35 -0700 | [diff] [blame] | 1475 | memset(&viewport_create, 0, sizeof(viewport_create)); |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1476 | viewport_create.sType = VK_STRUCTURE_TYPE_DYNAMIC_VP_STATE_CREATE_INFO; |
Courtney Goeltzenleuchter | c6e32f9 | 2015-02-11 14:13:34 -0700 | [diff] [blame] | 1477 | viewport_create.viewportAndScissorCount = 1; |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 1478 | VkViewport viewport; |
Piers Daniell | 886be47 | 2015-02-23 16:23:13 -0700 | [diff] [blame] | 1479 | memset(&viewport, 0, sizeof(viewport)); |
Mark Lobodzinski | e2d07a5 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 1480 | viewport.height = (float) demo->height; |
| 1481 | viewport.width = (float) demo->width; |
| 1482 | viewport.minDepth = (float) 0.0f; |
| 1483 | viewport.maxDepth = (float) 1.0f; |
Piers Daniell | 886be47 | 2015-02-23 16:23:13 -0700 | [diff] [blame] | 1484 | viewport_create.pViewports = &viewport; |
Chris Forbes | 2951d7d | 2015-06-22 17:21:59 +1200 | [diff] [blame] | 1485 | VkRect2D scissor; |
Piers Daniell | 886be47 | 2015-02-23 16:23:13 -0700 | [diff] [blame] | 1486 | memset(&scissor, 0, sizeof(scissor)); |
Courtney Goeltzenleuchter | c6e32f9 | 2015-02-11 14:13:34 -0700 | [diff] [blame] | 1487 | scissor.extent.width = demo->width; |
| 1488 | scissor.extent.height = demo->height; |
| 1489 | scissor.offset.x = 0; |
| 1490 | scissor.offset.y = 0; |
Courtney Goeltzenleuchter | c6e32f9 | 2015-02-11 14:13:34 -0700 | [diff] [blame] | 1491 | viewport_create.pScissors = &scissor; |
Courtney Goeltzenleuchter | 4e8246e | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1492 | |
| 1493 | memset(&raster, 0, sizeof(raster)); |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1494 | raster.sType = VK_STRUCTURE_TYPE_DYNAMIC_RS_STATE_CREATE_INFO; |
Piers Daniell | 886be47 | 2015-02-23 16:23:13 -0700 | [diff] [blame] | 1495 | raster.lineWidth = 1.0; |
Courtney Goeltzenleuchter | 4e8246e | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1496 | |
| 1497 | memset(&color_blend, 0, sizeof(color_blend)); |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1498 | color_blend.sType = VK_STRUCTURE_TYPE_DYNAMIC_CB_STATE_CREATE_INFO; |
Piers Daniell | 886be47 | 2015-02-23 16:23:13 -0700 | [diff] [blame] | 1499 | color_blend.blendConst[0] = 1.0f; |
| 1500 | color_blend.blendConst[1] = 1.0f; |
| 1501 | color_blend.blendConst[2] = 1.0f; |
| 1502 | color_blend.blendConst[3] = 1.0f; |
Courtney Goeltzenleuchter | 4e8246e | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1503 | |
| 1504 | memset(&depth_stencil, 0, sizeof(depth_stencil)); |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1505 | depth_stencil.sType = VK_STRUCTURE_TYPE_DYNAMIC_DS_STATE_CREATE_INFO; |
Mark Lobodzinski | 4405fbf | 2015-06-12 11:14:17 -0600 | [diff] [blame] | 1506 | depth_stencil.minDepthBounds = 0.0f; |
| 1507 | depth_stencil.maxDepthBounds = 1.0f; |
Tony Barbour | fa6cac7 | 2015-01-16 14:27:35 -0700 | [diff] [blame] | 1508 | depth_stencil.stencilBackRef = 0; |
| 1509 | depth_stencil.stencilFrontRef = 0; |
| 1510 | depth_stencil.stencilReadMask = 0xff; |
| 1511 | depth_stencil.stencilWriteMask = 0xff; |
Courtney Goeltzenleuchter | 4e8246e | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1512 | |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1513 | err = vkCreateDynamicViewportState(demo->device, &viewport_create, &demo->viewport); |
Courtney Goeltzenleuchter | 4e8246e | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1514 | assert(!err); |
| 1515 | |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1516 | err = vkCreateDynamicRasterState(demo->device, &raster, &demo->raster); |
Courtney Goeltzenleuchter | 4e8246e | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1517 | assert(!err); |
| 1518 | |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1519 | err = vkCreateDynamicColorBlendState(demo->device, |
Courtney Goeltzenleuchter | 4e8246e | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1520 | &color_blend, &demo->color_blend); |
| 1521 | assert(!err); |
| 1522 | |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1523 | err = vkCreateDynamicDepthStencilState(demo->device, |
Courtney Goeltzenleuchter | 4e8246e | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1524 | &depth_stencil, &demo->depth_stencil); |
| 1525 | assert(!err); |
| 1526 | } |
| 1527 | |
Chia-I Wu | 8d24b3b | 2015-03-26 13:14:16 +0800 | [diff] [blame] | 1528 | static void demo_prepare_descriptor_pool(struct demo *demo) |
Chia-I Wu | f838506 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 1529 | { |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 1530 | const VkDescriptorTypeCount type_counts[2] = { |
Chia-I Wu | f838506 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 1531 | [0] = { |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1532 | .type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, |
Chia-I Wu | f838506 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 1533 | .count = 1, |
| 1534 | }, |
| 1535 | [1] = { |
Courtney Goeltzenleuchter | ad87081 | 2015-04-15 15:29:59 -0600 | [diff] [blame] | 1536 | .type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, |
Chia-I Wu | f838506 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 1537 | .count = DEMO_TEXTURE_COUNT, |
| 1538 | }, |
| 1539 | }; |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 1540 | const VkDescriptorPoolCreateInfo descriptor_pool = { |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1541 | .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO, |
Chia-I Wu | f838506 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 1542 | .pNext = NULL, |
| 1543 | .count = 2, |
| 1544 | .pTypeCount = type_counts, |
| 1545 | }; |
Tony Barbour | 22a3086 | 2015-04-22 09:02:32 -0600 | [diff] [blame] | 1546 | VkResult U_ASSERT_ONLY err; |
Chia-I Wu | f838506 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 1547 | |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1548 | err = vkCreateDescriptorPool(demo->device, |
| 1549 | VK_DESCRIPTOR_POOL_USAGE_ONE_SHOT, 1, |
Chia-I Wu | 8d24b3b | 2015-03-26 13:14:16 +0800 | [diff] [blame] | 1550 | &descriptor_pool, &demo->desc_pool); |
Chia-I Wu | f838506 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 1551 | assert(!err); |
| 1552 | } |
| 1553 | |
| 1554 | static void demo_prepare_descriptor_set(struct demo *demo) |
| 1555 | { |
Chia-I Wu | 8cd8ecd | 2015-05-25 16:27:55 +0800 | [diff] [blame] | 1556 | VkDescriptorInfo tex_descs[DEMO_TEXTURE_COUNT]; |
| 1557 | VkWriteDescriptorSet writes[2]; |
Tony Barbour | 22a3086 | 2015-04-22 09:02:32 -0600 | [diff] [blame] | 1558 | VkResult U_ASSERT_ONLY err; |
Chia-I Wu | f838506 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 1559 | uint32_t count; |
Mark Lobodzinski | e2d07a5 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 1560 | uint32_t i; |
Chia-I Wu | f838506 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 1561 | |
Mike Stroyan | 230e625 | 2015-04-17 12:36:38 -0600 | [diff] [blame] | 1562 | err = vkAllocDescriptorSets(demo->device, demo->desc_pool, |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1563 | VK_DESCRIPTOR_SET_USAGE_STATIC, |
Chia-I Wu | 6e68a89 | 2015-02-23 10:41:08 -0700 | [diff] [blame] | 1564 | 1, &demo->desc_layout, |
Chia-I Wu | f838506 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 1565 | &demo->desc_set, &count); |
| 1566 | assert(!err && count == 1); |
| 1567 | |
Chia-I Wu | 8cd8ecd | 2015-05-25 16:27:55 +0800 | [diff] [blame] | 1568 | memset(&tex_descs, 0, sizeof(tex_descs)); |
| 1569 | for (i = 0; i < DEMO_TEXTURE_COUNT; i++) { |
| 1570 | tex_descs[i].sampler = demo->textures[i].sampler; |
| 1571 | tex_descs[i].imageView = demo->textures[i].view; |
| 1572 | tex_descs[i].imageLayout = VK_IMAGE_LAYOUT_GENERAL; |
| 1573 | } |
| 1574 | |
| 1575 | memset(&writes, 0, sizeof(writes)); |
| 1576 | |
| 1577 | writes[0].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET; |
| 1578 | writes[0].destSet = demo->desc_set; |
| 1579 | writes[0].count = 1; |
| 1580 | writes[0].descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER; |
| 1581 | writes[0].pDescriptors = &demo->uniform_data.desc; |
| 1582 | |
| 1583 | writes[1].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET; |
| 1584 | writes[1].destSet = demo->desc_set; |
| 1585 | writes[1].destBinding = 1; |
| 1586 | writes[1].count = DEMO_TEXTURE_COUNT; |
| 1587 | writes[1].descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER; |
| 1588 | writes[1].pDescriptors = tex_descs; |
| 1589 | |
| 1590 | err = vkUpdateDescriptorSets(demo->device, 2, writes, 0, NULL); |
| 1591 | assert(!err); |
Chia-I Wu | f838506 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 1592 | } |
| 1593 | |
Courtney Goeltzenleuchter | 4e8246e | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1594 | static void demo_prepare(struct demo *demo) |
| 1595 | { |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 1596 | const VkCmdBufferCreateInfo cmd = { |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1597 | .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO, |
Courtney Goeltzenleuchter | 4e8246e | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1598 | .pNext = NULL, |
Courtney Goeltzenleuchter | f316806 | 2015-03-05 18:09:39 -0700 | [diff] [blame] | 1599 | .queueNodeIndex = demo->graphics_queue_node_index, |
Chia-I Wu | 88eaa3b | 2015-06-26 15:34:39 +0800 | [diff] [blame] | 1600 | .level = VK_CMD_BUFFER_LEVEL_PRIMARY, |
Courtney Goeltzenleuchter | 4e8246e | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1601 | .flags = 0, |
| 1602 | }; |
Tony Barbour | 22a3086 | 2015-04-22 09:02:32 -0600 | [diff] [blame] | 1603 | VkResult U_ASSERT_ONLY err; |
Courtney Goeltzenleuchter | 4e8246e | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1604 | |
| 1605 | demo_prepare_buffers(demo); |
| 1606 | demo_prepare_depth(demo); |
| 1607 | demo_prepare_textures(demo); |
Courtney Goeltzenleuchter | 3eeff43 | 2014-10-29 08:29:35 -0600 | [diff] [blame] | 1608 | demo_prepare_cube_data_buffer(demo); |
Courtney Goeltzenleuchter | 4e8246e | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1609 | |
Chia-I Wu | f838506 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 1610 | demo_prepare_descriptor_layout(demo); |
Courtney Goeltzenleuchter | 4e8246e | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1611 | demo_prepare_pipeline(demo); |
| 1612 | demo_prepare_dynamic_states(demo); |
| 1613 | |
Courtney Goeltzenleuchter | bd3b3aa | 2015-02-17 09:48:44 -0700 | [diff] [blame] | 1614 | for (int i = 0; i < DEMO_BUFFER_COUNT; i++) { |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1615 | err = vkCreateCommandBuffer(demo->device, &cmd, &demo->buffers[i].cmd); |
Courtney Goeltzenleuchter | bd3b3aa | 2015-02-17 09:48:44 -0700 | [diff] [blame] | 1616 | assert(!err); |
| 1617 | } |
Chia-I Wu | f838506 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 1618 | |
Chia-I Wu | 8d24b3b | 2015-03-26 13:14:16 +0800 | [diff] [blame] | 1619 | demo_prepare_descriptor_pool(demo); |
Chia-I Wu | f838506 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 1620 | demo_prepare_descriptor_set(demo); |
Courtney Goeltzenleuchter | bd3b3aa | 2015-02-17 09:48:44 -0700 | [diff] [blame] | 1621 | |
| 1622 | |
| 1623 | for (int i = 0; i < DEMO_BUFFER_COUNT; i++) { |
| 1624 | demo->current_buffer = i; |
| 1625 | demo_draw_build_cmd(demo, demo->buffers[i].cmd); |
| 1626 | } |
| 1627 | |
Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 1628 | /* |
| 1629 | * Prepare functions above may generate pipeline commands |
| 1630 | * that need to be flushed before beginning the render loop. |
| 1631 | */ |
| 1632 | demo_flush_init_cmd(demo); |
| 1633 | |
Courtney Goeltzenleuchter | bd3b3aa | 2015-02-17 09:48:44 -0700 | [diff] [blame] | 1634 | demo->current_buffer = 0; |
Jon Ashburn | 8a399e9 | 2015-04-24 09:46:24 -0700 | [diff] [blame] | 1635 | demo->prepared = true; |
Courtney Goeltzenleuchter | 4e8246e | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1636 | } |
| 1637 | |
David Pinedo | eeca2a2 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 1638 | static void demo_cleanup(struct demo *demo) |
| 1639 | { |
| 1640 | uint32_t i; |
| 1641 | |
| 1642 | demo->prepared = false; |
| 1643 | |
| 1644 | vkDestroyObject(demo->device, VK_OBJECT_TYPE_DESCRIPTOR_SET, demo->desc_set); |
| 1645 | vkDestroyObject(demo->device, VK_OBJECT_TYPE_DESCRIPTOR_POOL, demo->desc_pool); |
| 1646 | |
| 1647 | vkDestroyObject(demo->device, VK_OBJECT_TYPE_DYNAMIC_VP_STATE, demo->viewport); |
| 1648 | vkDestroyObject(demo->device, VK_OBJECT_TYPE_DYNAMIC_RS_STATE, demo->raster); |
| 1649 | vkDestroyObject(demo->device, VK_OBJECT_TYPE_DYNAMIC_CB_STATE, demo->color_blend); |
| 1650 | vkDestroyObject(demo->device, VK_OBJECT_TYPE_DYNAMIC_DS_STATE, demo->depth_stencil); |
| 1651 | |
| 1652 | vkDestroyObject(demo->device, VK_OBJECT_TYPE_PIPELINE, demo->pipeline); |
| 1653 | vkDestroyObject(demo->device, VK_OBJECT_TYPE_PIPELINE_LAYOUT, demo->pipeline_layout); |
| 1654 | vkDestroyObject(demo->device, VK_OBJECT_TYPE_DESCRIPTOR_SET_LAYOUT, demo->desc_layout); |
| 1655 | |
| 1656 | for (i = 0; i < DEMO_TEXTURE_COUNT; i++) { |
| 1657 | vkDestroyObject(demo->device, VK_OBJECT_TYPE_IMAGE_VIEW, demo->textures[i].view); |
| 1658 | vkDestroyObject(demo->device, VK_OBJECT_TYPE_IMAGE, demo->textures[i].image); |
| 1659 | vkFreeMemory(demo->device, demo->textures[i].mem); |
| 1660 | vkDestroyObject(demo->device, VK_OBJECT_TYPE_SAMPLER, demo->textures[i].sampler); |
| 1661 | } |
| 1662 | demo->fpDestroySwapChainWSI(demo->swap_chain); |
| 1663 | |
| 1664 | vkDestroyObject(demo->device, VK_OBJECT_TYPE_DEPTH_STENCIL_VIEW, demo->depth.view); |
| 1665 | vkDestroyObject(demo->device, VK_OBJECT_TYPE_IMAGE, demo->depth.image); |
| 1666 | vkFreeMemory(demo->device, demo->depth.mem); |
| 1667 | |
| 1668 | vkDestroyObject(demo->device, VK_OBJECT_TYPE_BUFFER_VIEW, demo->uniform_data.view); |
| 1669 | vkDestroyObject(demo->device, VK_OBJECT_TYPE_BUFFER, demo->uniform_data.buf); |
| 1670 | vkFreeMemory(demo->device, demo->uniform_data.mem); |
| 1671 | |
| 1672 | for (i = 0; i < DEMO_BUFFER_COUNT; i++) { |
| 1673 | vkDestroyObject(demo->device, VK_OBJECT_TYPE_COLOR_ATTACHMENT_VIEW, demo->buffers[i].view); |
| 1674 | vkDestroyObject(demo->device, VK_OBJECT_TYPE_COMMAND_BUFFER, demo->buffers[i].cmd); |
| 1675 | } |
| 1676 | |
| 1677 | vkDestroyDevice(demo->device); |
Tony Barbour | a65ecc2 | 2015-06-30 14:14:19 -0600 | [diff] [blame] | 1678 | if (demo->validate) { |
| 1679 | demo->dbgDestroyMsgCallback(demo->inst, demo->msg_callback); |
| 1680 | } |
David Pinedo | eeca2a2 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 1681 | vkDestroyInstance(demo->inst); |
| 1682 | |
| 1683 | #ifndef _WIN32 |
| 1684 | xcb_destroy_window(demo->connection, demo->window); |
| 1685 | xcb_disconnect(demo->connection); |
| 1686 | #endif // _WIN32 |
| 1687 | } |
| 1688 | |
| 1689 | // On MS-Windows, make this a global, so it's available to WndProc() |
| 1690 | struct demo demo; |
| 1691 | |
Ian Elliott | e14e9f9 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 1692 | #ifdef _WIN32 |
| 1693 | static void demo_run(struct demo *demo) |
| 1694 | { |
Courtney Goeltzenleuchter | 857542b | 2015-04-27 14:56:34 -0600 | [diff] [blame] | 1695 | if (!demo->prepared) |
| 1696 | return; |
Ian Elliott | e14e9f9 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 1697 | // Wait for work to finish before updating MVP. |
| 1698 | vkDeviceWaitIdle(demo->device); |
| 1699 | demo_update_data_buffer(demo); |
| 1700 | |
| 1701 | demo_draw(demo); |
| 1702 | |
| 1703 | // Wait for work to finish before updating MVP. |
| 1704 | vkDeviceWaitIdle(demo->device); |
Ian Elliott | e14e9f9 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 1705 | |
David Pinedo | eeca2a2 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 1706 | demo->curFrame++; |
| 1707 | |
| 1708 | if (demo->frameCount != INT_MAX && demo->curFrame == demo->frameCount) |
| 1709 | { |
| 1710 | demo->quit=true; |
| 1711 | demo_cleanup(demo); |
| 1712 | ExitProcess(0); |
| 1713 | } |
| 1714 | |
| 1715 | } |
Ian Elliott | e14e9f9 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 1716 | |
| 1717 | // MS-Windows event handling function: |
| 1718 | LRESULT CALLBACK WndProc(HWND hWnd, |
| 1719 | UINT uMsg, |
| 1720 | WPARAM wParam, |
| 1721 | LPARAM lParam) |
| 1722 | { |
Ian Elliott | e14e9f9 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 1723 | switch(uMsg) |
| 1724 | { |
Ian Elliott | e14e9f9 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 1725 | case WM_CLOSE: |
| 1726 | PostQuitMessage(0); |
Tony Barbour | 5685ad7 | 2015-04-29 16:19:20 -0600 | [diff] [blame] | 1727 | break; |
Ian Elliott | e14e9f9 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 1728 | case WM_PAINT: |
| 1729 | demo_run(&demo); |
| 1730 | return 0; |
| 1731 | default: |
| 1732 | break; |
| 1733 | } |
| 1734 | return (DefWindowProc(hWnd, uMsg, wParam, lParam)); |
| 1735 | } |
| 1736 | |
| 1737 | static void demo_create_window(struct demo *demo) |
| 1738 | { |
| 1739 | WNDCLASSEX win_class; |
| 1740 | |
| 1741 | // Initialize the window class structure: |
| 1742 | win_class.cbSize = sizeof(WNDCLASSEX); |
| 1743 | win_class.style = CS_HREDRAW | CS_VREDRAW; |
| 1744 | win_class.lpfnWndProc = WndProc; |
| 1745 | win_class.cbClsExtra = 0; |
| 1746 | win_class.cbWndExtra = 0; |
| 1747 | win_class.hInstance = demo->connection; // hInstance |
| 1748 | win_class.hIcon = LoadIcon(NULL, IDI_APPLICATION); |
| 1749 | win_class.hCursor = LoadCursor(NULL, IDC_ARROW); |
| 1750 | win_class.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); |
| 1751 | win_class.lpszMenuName = NULL; |
| 1752 | win_class.lpszClassName = demo->name; |
| 1753 | win_class.hIconSm = LoadIcon(NULL, IDI_WINLOGO); |
| 1754 | // Register window class: |
| 1755 | if (!RegisterClassEx(&win_class)) { |
| 1756 | // It didn't work, so try to give a useful error: |
| 1757 | printf("Unexpected error trying to start the application!\n"); |
| 1758 | fflush(stdout); |
| 1759 | exit(1); |
| 1760 | } |
| 1761 | // Create window with the registered class: |
Mike Stroyan | f585629 | 2015-06-15 14:20:13 -0600 | [diff] [blame] | 1762 | RECT wr = { 0, 0, demo->width, demo->height }; |
| 1763 | AdjustWindowRect(&wr, WS_OVERLAPPEDWINDOW, FALSE); |
Ian Elliott | e14e9f9 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 1764 | demo->window = CreateWindowEx(0, |
| 1765 | demo->name, // class name |
| 1766 | demo->name, // app name |
| 1767 | WS_OVERLAPPEDWINDOW | // window style |
| 1768 | WS_VISIBLE | |
| 1769 | WS_SYSMENU, |
| 1770 | 100,100, // x/y coords |
Mike Stroyan | f585629 | 2015-06-15 14:20:13 -0600 | [diff] [blame] | 1771 | wr.right-wr.left, // width |
| 1772 | wr.bottom-wr.top, // height |
Ian Elliott | e14e9f9 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 1773 | NULL, // handle to parent |
| 1774 | NULL, // handle to menu |
| 1775 | demo->connection, // hInstance |
| 1776 | NULL); // no extra parameters |
| 1777 | if (!demo->window) { |
| 1778 | // It didn't work, so try to give a useful error: |
| 1779 | printf("Cannot create a window in which to draw!\n"); |
| 1780 | fflush(stdout); |
| 1781 | exit(1); |
| 1782 | } |
| 1783 | } |
| 1784 | #else // _WIN32 |
Courtney Goeltzenleuchter | 4e8246e | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1785 | static void demo_handle_event(struct demo *demo, |
| 1786 | const xcb_generic_event_t *event) |
| 1787 | { |
Piers Daniell | 886be47 | 2015-02-23 16:23:13 -0700 | [diff] [blame] | 1788 | uint8_t event_code = event->response_type & 0x7f; |
Courtney Goeltzenleuchter | ca21a21 | 2014-11-06 14:27:52 -0700 | [diff] [blame] | 1789 | switch (event_code) { |
Courtney Goeltzenleuchter | 4e8246e | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1790 | case XCB_EXPOSE: |
Courtney Goeltzenleuchter | 5461148 | 2014-11-18 11:28:09 -0700 | [diff] [blame] | 1791 | // TODO: Resize window |
Courtney Goeltzenleuchter | 4e8246e | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1792 | break; |
Courtney Goeltzenleuchter | ca21a21 | 2014-11-06 14:27:52 -0700 | [diff] [blame] | 1793 | case XCB_CLIENT_MESSAGE: |
| 1794 | if((*(xcb_client_message_event_t*)event).data.data32[0] == |
| 1795 | (*demo->atom_wm_delete_window).atom) { |
| 1796 | demo->quit = true; |
| 1797 | } |
| 1798 | break; |
Courtney Goeltzenleuchter | 4e8246e | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1799 | case XCB_KEY_RELEASE: |
| 1800 | { |
| 1801 | const xcb_key_release_event_t *key = |
| 1802 | (const xcb_key_release_event_t *) event; |
| 1803 | |
Courtney Goeltzenleuchter | 6f88d4c | 2014-10-28 10:32:57 -0600 | [diff] [blame] | 1804 | switch (key->detail) { |
| 1805 | case 0x9: // Escape |
Courtney Goeltzenleuchter | 4e8246e | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1806 | demo->quit = true; |
Courtney Goeltzenleuchter | 6f88d4c | 2014-10-28 10:32:57 -0600 | [diff] [blame] | 1807 | break; |
Courtney Goeltzenleuchter | e334240 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 1808 | case 0x71: // left arrow key |
Courtney Goeltzenleuchter | 3eeff43 | 2014-10-29 08:29:35 -0600 | [diff] [blame] | 1809 | demo->spin_angle += demo->spin_increment; |
Courtney Goeltzenleuchter | e334240 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 1810 | break; |
Courtney Goeltzenleuchter | 6f88d4c | 2014-10-28 10:32:57 -0600 | [diff] [blame] | 1811 | case 0x72: // right arrow key |
Courtney Goeltzenleuchter | 3eeff43 | 2014-10-29 08:29:35 -0600 | [diff] [blame] | 1812 | demo->spin_angle -= demo->spin_increment; |
Courtney Goeltzenleuchter | 6f88d4c | 2014-10-28 10:32:57 -0600 | [diff] [blame] | 1813 | break; |
Courtney Goeltzenleuchter | 3eeff43 | 2014-10-29 08:29:35 -0600 | [diff] [blame] | 1814 | case 0x41: |
| 1815 | demo->pause = !demo->pause; |
Piers Daniell | 886be47 | 2015-02-23 16:23:13 -0700 | [diff] [blame] | 1816 | break; |
Courtney Goeltzenleuchter | 6f88d4c | 2014-10-28 10:32:57 -0600 | [diff] [blame] | 1817 | } |
Courtney Goeltzenleuchter | 4e8246e | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1818 | } |
| 1819 | break; |
| 1820 | default: |
| 1821 | break; |
| 1822 | } |
| 1823 | } |
| 1824 | |
| 1825 | static void demo_run(struct demo *demo) |
| 1826 | { |
| 1827 | xcb_flush(demo->connection); |
| 1828 | |
| 1829 | while (!demo->quit) { |
| 1830 | xcb_generic_event_t *event; |
| 1831 | |
Courtney Goeltzenleuchter | 5461148 | 2014-11-18 11:28:09 -0700 | [diff] [blame] | 1832 | if (demo->pause) { |
| 1833 | event = xcb_wait_for_event(demo->connection); |
| 1834 | } else { |
| 1835 | event = xcb_poll_for_event(demo->connection); |
| 1836 | } |
Courtney Goeltzenleuchter | 4e8246e | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1837 | if (event) { |
| 1838 | demo_handle_event(demo, event); |
| 1839 | free(event); |
Courtney Goeltzenleuchter | bb3e131 | 2014-11-10 11:13:13 -0700 | [diff] [blame] | 1840 | } |
Courtney Goeltzenleuchter | 5461148 | 2014-11-18 11:28:09 -0700 | [diff] [blame] | 1841 | |
| 1842 | // Wait for work to finish before updating MVP. |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1843 | vkDeviceWaitIdle(demo->device); |
Courtney Goeltzenleuchter | 5461148 | 2014-11-18 11:28:09 -0700 | [diff] [blame] | 1844 | demo_update_data_buffer(demo); |
| 1845 | |
Courtney Goeltzenleuchter | bb3e131 | 2014-11-10 11:13:13 -0700 | [diff] [blame] | 1846 | demo_draw(demo); |
Courtney Goeltzenleuchter | 21f8997 | 2014-11-18 11:28:09 -0700 | [diff] [blame] | 1847 | |
Courtney Goeltzenleuchter | bb3e131 | 2014-11-10 11:13:13 -0700 | [diff] [blame] | 1848 | // Wait for work to finish before updating MVP. |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1849 | vkDeviceWaitIdle(demo->device); |
David Pinedo | eeca2a2 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 1850 | demo->curFrame++; |
| 1851 | if (demo->frameCount != INT_MAX && demo->curFrame == demo->frameCount) |
| 1852 | demo->quit = true; |
| 1853 | |
Courtney Goeltzenleuchter | 4e8246e | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1854 | } |
| 1855 | } |
| 1856 | |
| 1857 | static void demo_create_window(struct demo *demo) |
| 1858 | { |
| 1859 | uint32_t value_mask, value_list[32]; |
| 1860 | |
| 1861 | demo->window = xcb_generate_id(demo->connection); |
| 1862 | |
| 1863 | value_mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK; |
| 1864 | value_list[0] = demo->screen->black_pixel; |
| 1865 | value_list[1] = XCB_EVENT_MASK_KEY_RELEASE | |
| 1866 | XCB_EVENT_MASK_EXPOSURE; |
| 1867 | |
| 1868 | xcb_create_window(demo->connection, |
| 1869 | XCB_COPY_FROM_PARENT, |
| 1870 | demo->window, demo->screen->root, |
| 1871 | 0, 0, demo->width, demo->height, 0, |
| 1872 | XCB_WINDOW_CLASS_INPUT_OUTPUT, |
| 1873 | demo->screen->root_visual, |
| 1874 | value_mask, value_list); |
| 1875 | |
Courtney Goeltzenleuchter | ca21a21 | 2014-11-06 14:27:52 -0700 | [diff] [blame] | 1876 | /* Magic code that will send notification when window is destroyed */ |
| 1877 | xcb_intern_atom_cookie_t cookie = xcb_intern_atom(demo->connection, 1, 12, |
| 1878 | "WM_PROTOCOLS"); |
| 1879 | xcb_intern_atom_reply_t* reply = xcb_intern_atom_reply(demo->connection, cookie, 0); |
| 1880 | |
| 1881 | xcb_intern_atom_cookie_t cookie2 = xcb_intern_atom(demo->connection, 0, 16, "WM_DELETE_WINDOW"); |
| 1882 | demo->atom_wm_delete_window = xcb_intern_atom_reply(demo->connection, cookie2, 0); |
| 1883 | |
| 1884 | xcb_change_property(demo->connection, XCB_PROP_MODE_REPLACE, |
| 1885 | demo->window, (*reply).atom, 4, 32, 1, |
| 1886 | &(*demo->atom_wm_delete_window).atom); |
| 1887 | free(reply); |
| 1888 | |
Courtney Goeltzenleuchter | 4e8246e | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1889 | xcb_map_window(demo->connection, demo->window); |
David Pinedo | eeca2a2 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 1890 | |
| 1891 | // Force the x/y coordinates to 100,100 results are identical in consecutive runs |
| 1892 | const uint32_t coords[] = {100, 100}; |
| 1893 | xcb_configure_window(demo->connection, demo->window, |
| 1894 | XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y, coords); |
Courtney Goeltzenleuchter | 4e8246e | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1895 | } |
Ian Elliott | e14e9f9 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 1896 | #endif // _WIN32 |
Courtney Goeltzenleuchter | 4e8246e | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1897 | |
Courtney Goeltzenleuchter | 8c15fc5 | 2015-07-06 17:46:11 -0600 | [diff] [blame^] | 1898 | /* |
| 1899 | * Return 1 (true) if all layer names specified in check_names |
| 1900 | * can be found in given layer properties. |
| 1901 | */ |
| 1902 | static bool32_t demo_check_layers(uint32_t check_count, char **check_names, |
| 1903 | uint32_t layer_count, VkLayerProperties *layers) |
| 1904 | { |
| 1905 | for (uint32_t i = 0; i < check_count; i++) { |
| 1906 | bool32_t found = 0; |
| 1907 | for (uint32_t j = 0; j < layer_count; j++) { |
| 1908 | if (!strcmp(check_names[i], layers[j].layerName)) { |
| 1909 | found = 1; |
| 1910 | } |
| 1911 | } |
| 1912 | if (!found) { |
| 1913 | fprintf(stderr, "Cannot find layer: %s\n", check_names[i]); |
| 1914 | return 0; |
| 1915 | } |
| 1916 | } |
| 1917 | return 1; |
| 1918 | } |
| 1919 | |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1920 | static void demo_init_vk(struct demo *demo) |
Courtney Goeltzenleuchter | 4e8246e | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1921 | { |
Tobin Ehlis | 3536b44 | 2015-04-16 18:04:57 -0600 | [diff] [blame] | 1922 | VkResult err; |
Courtney Goeltzenleuchter | 18061cd | 2015-06-29 15:39:26 -0600 | [diff] [blame] | 1923 | char *extension_names[64]; |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 1924 | VkExtensionProperties *instance_extensions; |
Courtney Goeltzenleuchter | 18061cd | 2015-06-29 15:39:26 -0600 | [diff] [blame] | 1925 | VkLayerProperties *instance_layers; |
| 1926 | VkLayerProperties *device_layers; |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 1927 | uint32_t instance_extension_count = 0; |
Courtney Goeltzenleuchter | 18061cd | 2015-06-29 15:39:26 -0600 | [diff] [blame] | 1928 | uint32_t instance_layer_count = 0; |
| 1929 | uint32_t enabled_extension_count = 0; |
| 1930 | uint32_t enabled_layer_count = 0; |
| 1931 | |
Courtney Goeltzenleuchter | 8c15fc5 | 2015-07-06 17:46:11 -0600 | [diff] [blame^] | 1932 | char *instance_validation_layers[] = { |
| 1933 | "MemTracker", |
| 1934 | }; |
| 1935 | |
| 1936 | char *device_validation_layers[] = { |
| 1937 | "MemTracker", |
| 1938 | }; |
| 1939 | |
Courtney Goeltzenleuchter | 18061cd | 2015-06-29 15:39:26 -0600 | [diff] [blame] | 1940 | /* Look for validation layers */ |
| 1941 | bool32_t validation_found = 0; |
| 1942 | err = vkGetGlobalLayerProperties(&instance_layer_count, NULL); |
Tobin Ehlis | 3536b44 | 2015-04-16 18:04:57 -0600 | [diff] [blame] | 1943 | assert(!err); |
| 1944 | |
Courtney Goeltzenleuchter | 18061cd | 2015-06-29 15:39:26 -0600 | [diff] [blame] | 1945 | instance_layers = malloc(sizeof(VkLayerProperties) * instance_layer_count); |
| 1946 | err = vkGetGlobalLayerProperties(&instance_layer_count, instance_layers); |
| 1947 | assert(!err); |
Courtney Goeltzenleuchter | 8c15fc5 | 2015-07-06 17:46:11 -0600 | [diff] [blame^] | 1948 | |
| 1949 | if (demo->validate) { |
| 1950 | validation_found = demo_check_layers(ARRAY_SIZE(instance_validation_layers), instance_validation_layers, |
| 1951 | instance_layer_count, instance_layers); |
| 1952 | if (!validation_found) { |
| 1953 | ERR_EXIT("vkGetGlobalLayerProperties failed to find" |
| 1954 | "required validation layer.\n\n" |
| 1955 | "Please look at the Getting Started guide for additional " |
| 1956 | "information.\n", |
| 1957 | "vkCreateInstance Failure"); |
Courtney Goeltzenleuchter | 18061cd | 2015-06-29 15:39:26 -0600 | [diff] [blame] | 1958 | } |
Courtney Goeltzenleuchter | 8c15fc5 | 2015-07-06 17:46:11 -0600 | [diff] [blame^] | 1959 | enabled_layer_count = ARRAY_SIZE(instance_validation_layers); |
Courtney Goeltzenleuchter | 18061cd | 2015-06-29 15:39:26 -0600 | [diff] [blame] | 1960 | } |
| 1961 | |
| 1962 | err = vkGetGlobalExtensionProperties(NULL, &instance_extension_count, NULL); |
| 1963 | assert(!err); |
| 1964 | |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 1965 | bool32_t WSIextFound = 0; |
Courtney Goeltzenleuchter | 18061cd | 2015-06-29 15:39:26 -0600 | [diff] [blame] | 1966 | memset(extension_names, 0, sizeof(extension_names)); |
| 1967 | instance_extensions = malloc(sizeof(VkExtensionProperties) * instance_extension_count); |
| 1968 | err = vkGetGlobalExtensionProperties(NULL, &instance_extension_count, instance_extensions); |
| 1969 | assert(!err); |
| 1970 | for (uint32_t i = 0; i < instance_extension_count; i++) { |
| 1971 | if (!strcmp(VK_WSI_LUNARG_EXTENSION_NAME, instance_extensions[i].extName)) { |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 1972 | WSIextFound = 1; |
Courtney Goeltzenleuchter | 18061cd | 2015-06-29 15:39:26 -0600 | [diff] [blame] | 1973 | extension_names[enabled_extension_count++] = VK_WSI_LUNARG_EXTENSION_NAME; |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 1974 | } |
Courtney Goeltzenleuchter | 18061cd | 2015-06-29 15:39:26 -0600 | [diff] [blame] | 1975 | if (!strcmp(DEBUG_REPORT_EXTENSION_NAME, instance_extensions[i].extName)) { |
| 1976 | if (demo->validate) { |
| 1977 | extension_names[enabled_extension_count++] = DEBUG_REPORT_EXTENSION_NAME; |
| 1978 | } |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 1979 | } |
Courtney Goeltzenleuchter | 18061cd | 2015-06-29 15:39:26 -0600 | [diff] [blame] | 1980 | assert(enabled_extension_count < 64); |
Tobin Ehlis | 3536b44 | 2015-04-16 18:04:57 -0600 | [diff] [blame] | 1981 | } |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 1982 | if (!WSIextFound) { |
Tony Barbour | 426b905 | 2015-06-24 16:06:58 -0600 | [diff] [blame] | 1983 | ERR_EXIT("vkGetGlobalExtensionProperties failed to find the " |
Ian Elliott | 3b375cf | 2015-04-28 13:22:33 -0600 | [diff] [blame] | 1984 | "\"VK_WSI_LunarG\" extension.\n\nDo you have a compatible " |
| 1985 | "Vulkan installable client driver (ICD) installed?\nPlease " |
| 1986 | "look at the Getting Started guide for additional " |
| 1987 | "information.\n", |
| 1988 | "vkCreateInstance Failure"); |
| 1989 | } |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 1990 | const VkApplicationInfo app = { |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1991 | .sType = VK_STRUCTURE_TYPE_APPLICATION_INFO, |
Courtney Goeltzenleuchter | 4e8246e | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1992 | .pNext = NULL, |
Ian Elliott | 4e19ed0 | 2015-04-28 10:52:52 -0600 | [diff] [blame] | 1993 | .pAppName = APP_SHORT_NAME, |
Courtney Goeltzenleuchter | 4e8246e | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1994 | .appVersion = 0, |
Ian Elliott | 4e19ed0 | 2015-04-28 10:52:52 -0600 | [diff] [blame] | 1995 | .pEngineName = APP_SHORT_NAME, |
Courtney Goeltzenleuchter | 4e8246e | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1996 | .engineVersion = 0, |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1997 | .apiVersion = VK_API_VERSION, |
Courtney Goeltzenleuchter | 4e8246e | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1998 | }; |
Tony Barbour | 5685ad7 | 2015-04-29 16:19:20 -0600 | [diff] [blame] | 1999 | VkInstanceCreateInfo inst_info = { |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 2000 | .sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO, |
Jon Ashburn | 29669a4 | 2015-04-04 14:52:07 -0600 | [diff] [blame] | 2001 | .pNext = NULL, |
| 2002 | .pAppInfo = &app, |
| 2003 | .pAllocCb = NULL, |
Courtney Goeltzenleuchter | 18061cd | 2015-06-29 15:39:26 -0600 | [diff] [blame] | 2004 | .layerCount = enabled_layer_count, |
Courtney Goeltzenleuchter | 8c15fc5 | 2015-07-06 17:46:11 -0600 | [diff] [blame^] | 2005 | .ppEnabledLayerNames = (const char *const*) ((demo->validate) ? instance_validation_layers : NULL), |
Courtney Goeltzenleuchter | 18061cd | 2015-06-29 15:39:26 -0600 | [diff] [blame] | 2006 | .extensionCount = enabled_extension_count, |
| 2007 | .ppEnabledExtensionNames = (const char *const*) extension_names, |
Jon Ashburn | 29669a4 | 2015-04-04 14:52:07 -0600 | [diff] [blame] | 2008 | }; |
Courtney Goeltzenleuchter | ddcb619 | 2015-04-14 18:48:46 -0600 | [diff] [blame] | 2009 | const VkDeviceQueueCreateInfo queue = { |
Courtney Goeltzenleuchter | 4e8246e | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 2010 | .queueNodeIndex = 0, |
| 2011 | .queueCount = 1, |
| 2012 | }; |
Ian Elliott | ff6dab5 | 2015-04-28 11:35:02 -0600 | [diff] [blame] | 2013 | |
Mark Lobodzinski | e2d07a5 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 2014 | uint32_t gpu_count; |
| 2015 | uint32_t i; |
Courtney Goeltzenleuchter | f316806 | 2015-03-05 18:09:39 -0700 | [diff] [blame] | 2016 | uint32_t queue_count; |
Courtney Goeltzenleuchter | 4e8246e | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 2017 | |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 2018 | err = vkCreateInstance(&inst_info, &demo->inst); |
Ian Elliott | caa9f27 | 2015-04-28 11:35:02 -0600 | [diff] [blame] | 2019 | if (err == VK_ERROR_INCOMPATIBLE_DRIVER) { |
| 2020 | ERR_EXIT("Cannot find a compatible Vulkan installable client driver " |
Ian Elliott | 3b375cf | 2015-04-28 13:22:33 -0600 | [diff] [blame] | 2021 | "(ICD).\n\nPlease look at the Getting Started guide for " |
Ian Elliott | caa9f27 | 2015-04-28 11:35:02 -0600 | [diff] [blame] | 2022 | "additional information.\n", |
| 2023 | "vkCreateInstance Failure"); |
Tony Barbour | 5685ad7 | 2015-04-29 16:19:20 -0600 | [diff] [blame] | 2024 | } else if (err == VK_ERROR_INVALID_EXTENSION) { |
| 2025 | ERR_EXIT("Cannot find a specified extension library" |
| 2026 | ".\nMake sure your layers path is set appropriately\n", |
| 2027 | "vkCreateInstance Failure"); |
Ian Elliott | caa9f27 | 2015-04-28 11:35:02 -0600 | [diff] [blame] | 2028 | } else if (err) { |
Ian Elliott | 3b375cf | 2015-04-28 13:22:33 -0600 | [diff] [blame] | 2029 | ERR_EXIT("vkCreateInstance failed.\n\nDo you have a compatible Vulkan " |
| 2030 | "installable client driver (ICD) installed?\nPlease look at " |
Ian Elliott | caa9f27 | 2015-04-28 11:35:02 -0600 | [diff] [blame] | 2031 | "the Getting Started guide for additional information.\n", |
| 2032 | "vkCreateInstance Failure"); |
Ian Elliott | dfe55f7 | 2015-04-03 15:24:55 -0600 | [diff] [blame] | 2033 | } |
Jon Ashburn | 29669a4 | 2015-04-04 14:52:07 -0600 | [diff] [blame] | 2034 | |
Courtney Goeltzenleuchter | 18061cd | 2015-06-29 15:39:26 -0600 | [diff] [blame] | 2035 | free(instance_layers); |
| 2036 | free(instance_extensions); |
Tony Barbour | 5685ad7 | 2015-04-29 16:19:20 -0600 | [diff] [blame] | 2037 | |
Jon Ashburn | 07b309a | 2015-04-15 11:31:12 -0600 | [diff] [blame] | 2038 | gpu_count = 1; |
| 2039 | err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, &demo->gpu); |
Courtney Goeltzenleuchter | 4e8246e | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 2040 | assert(!err && gpu_count == 1); |
| 2041 | |
Courtney Goeltzenleuchter | 18061cd | 2015-06-29 15:39:26 -0600 | [diff] [blame] | 2042 | /* Look for validation layers */ |
| 2043 | validation_found = 0; |
| 2044 | enabled_layer_count = 0; |
| 2045 | uint32_t device_layer_count = 0; |
| 2046 | err = vkGetPhysicalDeviceLayerProperties(demo->gpu, &device_layer_count, NULL); |
| 2047 | assert(!err); |
| 2048 | |
Courtney Goeltzenleuchter | 18061cd | 2015-06-29 15:39:26 -0600 | [diff] [blame] | 2049 | device_layers = malloc(sizeof(VkLayerProperties) * device_layer_count); |
| 2050 | err = vkGetPhysicalDeviceLayerProperties(demo->gpu, &device_layer_count, device_layers); |
| 2051 | assert(!err); |
Courtney Goeltzenleuchter | 8c15fc5 | 2015-07-06 17:46:11 -0600 | [diff] [blame^] | 2052 | |
| 2053 | if (demo->validate) { |
| 2054 | validation_found = demo_check_layers(ARRAY_SIZE(device_validation_layers), device_validation_layers, |
| 2055 | device_layer_count, device_layers); |
| 2056 | if (!validation_found) { |
| 2057 | ERR_EXIT("vkGetPhysicalDeviceLayerProperties failed to find" |
| 2058 | "a required validation layer.\n\n" |
| 2059 | "Please look at the Getting Started guide for additional " |
| 2060 | "information.\n", |
| 2061 | "vkCreateDevice Failure"); |
Courtney Goeltzenleuchter | 18061cd | 2015-06-29 15:39:26 -0600 | [diff] [blame] | 2062 | } |
Courtney Goeltzenleuchter | 8c15fc5 | 2015-07-06 17:46:11 -0600 | [diff] [blame^] | 2063 | enabled_layer_count = ARRAY_SIZE(device_validation_layers); |
Courtney Goeltzenleuchter | 18061cd | 2015-06-29 15:39:26 -0600 | [diff] [blame] | 2064 | } |
| 2065 | |
| 2066 | uint32_t device_extension_count = 0; |
| 2067 | VkExtensionProperties *device_extensions = NULL; |
| 2068 | err = vkGetPhysicalDeviceExtensionProperties( |
| 2069 | demo->gpu, NULL, &device_extension_count, NULL); |
| 2070 | assert(!err); |
| 2071 | |
| 2072 | WSIextFound = 0; |
| 2073 | enabled_extension_count = 0; |
| 2074 | memset(extension_names, 0, sizeof(extension_names)); |
| 2075 | device_extensions = malloc(sizeof(VkExtensionProperties) * device_extension_count); |
| 2076 | err = vkGetPhysicalDeviceExtensionProperties( |
| 2077 | demo->gpu, NULL, &device_extension_count, device_extensions); |
| 2078 | assert(!err); |
Courtney Goeltzenleuchter | 37d091e | 2015-07-05 22:08:51 -0600 | [diff] [blame] | 2079 | #if 0 |
| 2080 | /* Will need this check in future */ |
Courtney Goeltzenleuchter | 18061cd | 2015-06-29 15:39:26 -0600 | [diff] [blame] | 2081 | for (uint32_t i = 0; i < device_extension_count; i++) { |
| 2082 | if (!strcmp(VK_WSI_LUNARG_EXTENSION_NAME, device_extensions[i].extName)) { |
| 2083 | WSIextFound = 1; |
| 2084 | extension_names[enabled_extension_count++] = VK_WSI_LUNARG_EXTENSION_NAME; |
| 2085 | } |
| 2086 | assert(enabled_extension_count < 64); |
| 2087 | } |
| 2088 | if (!WSIextFound) { |
| 2089 | ERR_EXIT("vkGetGlobalExtensionProperties failed to find the " |
| 2090 | "\"VK_WSI_LunarG\" extension.\n\nDo you have a compatible " |
| 2091 | "Vulkan installable client driver (ICD) installed?\nPlease " |
| 2092 | "look at the Getting Started guide for additional " |
| 2093 | "information.\n", |
| 2094 | "vkCreateInstance Failure"); |
| 2095 | } |
Courtney Goeltzenleuchter | 37d091e | 2015-07-05 22:08:51 -0600 | [diff] [blame] | 2096 | #endif |
Courtney Goeltzenleuchter | 18061cd | 2015-06-29 15:39:26 -0600 | [diff] [blame] | 2097 | |
| 2098 | VkDeviceCreateInfo device = { |
| 2099 | .sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO, |
| 2100 | .pNext = NULL, |
| 2101 | .queueRecordCount = 1, |
| 2102 | .pRequestedQueues = &queue, |
| 2103 | .layerCount = enabled_layer_count, |
Courtney Goeltzenleuchter | 8c15fc5 | 2015-07-06 17:46:11 -0600 | [diff] [blame^] | 2104 | .ppEnabledLayerNames = (const char *const*) ((demo->validate) ? device_validation_layers : NULL), |
Courtney Goeltzenleuchter | 18061cd | 2015-06-29 15:39:26 -0600 | [diff] [blame] | 2105 | .extensionCount = enabled_extension_count, |
| 2106 | .ppEnabledExtensionNames = (const char *const*) extension_names, |
| 2107 | .flags = 0, |
| 2108 | }; |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 2109 | |
Tony Barbour | 5685ad7 | 2015-04-29 16:19:20 -0600 | [diff] [blame] | 2110 | if (demo->validate) { |
Tony Barbour | a65ecc2 | 2015-06-30 14:14:19 -0600 | [diff] [blame] | 2111 | demo->dbgCreateMsgCallback = vkGetInstanceProcAddr(demo->inst, "vkDbgCreateMsgCallback"); |
| 2112 | demo->dbgDestroyMsgCallback = vkGetInstanceProcAddr(demo->inst, "vkDbgDestroyMsgCallback"); |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 2113 | if (!demo->dbgCreateMsgCallback) { |
| 2114 | ERR_EXIT("GetProcAddr: Unable to find vkDbgCreateMsgCallback\n", |
| 2115 | "vkGetProcAddr Failure"); |
| 2116 | } |
Tony Barbour | a65ecc2 | 2015-06-30 14:14:19 -0600 | [diff] [blame] | 2117 | if (!demo->dbgDestroyMsgCallback) { |
| 2118 | ERR_EXIT("GetProcAddr: Unable to find vkDbgDestroyMsgCallback\n", |
| 2119 | "vkGetProcAddr Failure"); |
| 2120 | } |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 2121 | err = demo->dbgCreateMsgCallback( |
| 2122 | demo->inst, |
| 2123 | VK_DBG_REPORT_ERROR_BIT | VK_DBG_REPORT_WARN_BIT, |
| 2124 | dbgFunc, NULL, |
| 2125 | &demo->msg_callback); |
| 2126 | switch (err) { |
| 2127 | case VK_SUCCESS: |
| 2128 | break; |
| 2129 | case VK_ERROR_INVALID_POINTER: |
| 2130 | ERR_EXIT("dbgCreateMsgCallback: Invalid pointer\n", |
| 2131 | "dbgCreateMsgCallback Failure"); |
| 2132 | break; |
| 2133 | case VK_ERROR_OUT_OF_HOST_MEMORY: |
| 2134 | ERR_EXIT("dbgCreateMsgCallback: out of host memory\n", |
| 2135 | "dbgCreateMsgCallback Failure"); |
| 2136 | break; |
| 2137 | default: |
| 2138 | ERR_EXIT("dbgCreateMsgCallback: unknown failure\n", |
| 2139 | "dbgCreateMsgCallback Failure"); |
| 2140 | break; |
| 2141 | } |
Tony Barbour | 5685ad7 | 2015-04-29 16:19:20 -0600 | [diff] [blame] | 2142 | } |
| 2143 | |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 2144 | err = vkCreateDevice(demo->gpu, &device, &demo->device); |
Courtney Goeltzenleuchter | 4e8246e | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 2145 | assert(!err); |
| 2146 | |
Courtney Goeltzenleuchter | 18061cd | 2015-06-29 15:39:26 -0600 | [diff] [blame] | 2147 | free(device_layers); |
| 2148 | |
Ian Elliott | 1b6de09 | 2015-06-22 15:07:49 -0600 | [diff] [blame] | 2149 | GET_DEVICE_PROC_ADDR(demo->device, CreateSwapChainWSI); |
| 2150 | GET_DEVICE_PROC_ADDR(demo->device, CreateSwapChainWSI); |
| 2151 | GET_DEVICE_PROC_ADDR(demo->device, DestroySwapChainWSI); |
| 2152 | GET_DEVICE_PROC_ADDR(demo->device, GetSwapChainInfoWSI); |
| 2153 | GET_DEVICE_PROC_ADDR(demo->device, QueuePresentWSI); |
Jon Ashburn | cedc15f | 2015-05-21 18:13:33 -0600 | [diff] [blame] | 2154 | |
Tony Barbour | 426b905 | 2015-06-24 16:06:58 -0600 | [diff] [blame] | 2155 | err = vkGetPhysicalDeviceProperties(demo->gpu, &demo->gpu_props); |
Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 2156 | assert(!err); |
| 2157 | |
Tony Barbour | 426b905 | 2015-06-24 16:06:58 -0600 | [diff] [blame] | 2158 | err = vkGetPhysicalDeviceQueueCount(demo->gpu, &queue_count); |
Courtney Goeltzenleuchter | bfccf41 | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 2159 | assert(!err); |
| 2160 | |
Tony Barbour | 426b905 | 2015-06-24 16:06:58 -0600 | [diff] [blame] | 2161 | demo->queue_props = (VkPhysicalDeviceQueueProperties *) malloc(queue_count * sizeof(VkPhysicalDeviceQueueProperties)); |
| 2162 | err = vkGetPhysicalDeviceQueueProperties(demo->gpu, queue_count, demo->queue_props); |
Courtney Goeltzenleuchter | f316806 | 2015-03-05 18:09:39 -0700 | [diff] [blame] | 2163 | assert(!err); |
Courtney Goeltzenleuchter | f316806 | 2015-03-05 18:09:39 -0700 | [diff] [blame] | 2164 | assert(queue_count >= 1); |
| 2165 | |
Mark Lobodzinski | cf26e07 | 2015-04-16 11:44:05 -0500 | [diff] [blame] | 2166 | // Graphics queue and MemMgr queue can be separate. |
| 2167 | // TODO: Add support for separate queues, including synchronization, |
Mark Lobodzinski | fb9f564 | 2015-05-11 17:21:15 -0500 | [diff] [blame] | 2168 | // and appropriate tracking for QueueSubmit |
Courtney Goeltzenleuchter | f316806 | 2015-03-05 18:09:39 -0700 | [diff] [blame] | 2169 | for (i = 0; i < queue_count; i++) { |
Mark Lobodzinski | fb9f564 | 2015-05-11 17:21:15 -0500 | [diff] [blame] | 2170 | if (demo->queue_props[i].queueFlags & VK_QUEUE_GRAPHICS_BIT) |
Courtney Goeltzenleuchter | f316806 | 2015-03-05 18:09:39 -0700 | [diff] [blame] | 2171 | break; |
| 2172 | } |
| 2173 | assert(i < queue_count); |
| 2174 | demo->graphics_queue_node_index = i; |
| 2175 | |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 2176 | err = vkGetDeviceQueue(demo->device, demo->graphics_queue_node_index, |
Courtney Goeltzenleuchter | 4e8246e | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 2177 | 0, &demo->queue); |
| 2178 | assert(!err); |
Tony Barbour | 5685ad7 | 2015-04-29 16:19:20 -0600 | [diff] [blame] | 2179 | |
Jon Ashburn | ba4a195 | 2015-06-16 12:44:51 -0600 | [diff] [blame] | 2180 | // for now hardcode format till get WSI support |
| 2181 | demo->format = VK_FORMAT_B8G8R8A8_UNORM; |
Ian Elliott | 32536f9 | 2015-04-21 16:41:02 -0600 | [diff] [blame] | 2182 | |
David Pinedo | eeca2a2 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 2183 | demo->quit = false; |
| 2184 | demo->curFrame = 0; |
Mark Lobodzinski | 7234629 | 2015-07-02 16:49:40 -0600 | [diff] [blame] | 2185 | |
| 2186 | // Get Memory information and properties |
| 2187 | err = vkGetPhysicalDeviceMemoryProperties(demo->gpu, &demo->memory_properties); |
| 2188 | assert(!err); |
Courtney Goeltzenleuchter | 4e8246e | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 2189 | } |
| 2190 | |
| 2191 | static void demo_init_connection(struct demo *demo) |
| 2192 | { |
Ian Elliott | e14e9f9 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 2193 | #ifndef _WIN32 |
Courtney Goeltzenleuchter | 4e8246e | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 2194 | const xcb_setup_t *setup; |
| 2195 | xcb_screen_iterator_t iter; |
| 2196 | int scr; |
| 2197 | |
| 2198 | demo->connection = xcb_connect(NULL, &scr); |
Ian Elliott | dfe55f7 | 2015-04-03 15:24:55 -0600 | [diff] [blame] | 2199 | if (demo->connection == NULL) { |
| 2200 | printf("Cannot find a compatible Vulkan installable client driver " |
| 2201 | "(ICD).\nExiting ...\n"); |
| 2202 | fflush(stdout); |
| 2203 | exit(1); |
| 2204 | } |
Courtney Goeltzenleuchter | 4e8246e | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 2205 | |
| 2206 | setup = xcb_get_setup(demo->connection); |
| 2207 | iter = xcb_setup_roots_iterator(setup); |
| 2208 | while (scr-- > 0) |
| 2209 | xcb_screen_next(&iter); |
| 2210 | |
| 2211 | demo->screen = iter.data; |
Ian Elliott | e14e9f9 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 2212 | #endif // _WIN32 |
Courtney Goeltzenleuchter | 4e8246e | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 2213 | } |
| 2214 | |
Courtney Goeltzenleuchter | 40a8b0a | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 2215 | static void demo_init(struct demo *demo, int argc, char **argv) |
Courtney Goeltzenleuchter | 4e8246e | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 2216 | { |
Courtney Goeltzenleuchter | 3eeff43 | 2014-10-29 08:29:35 -0600 | [diff] [blame] | 2217 | vec3 eye = {0.0f, 3.0f, 5.0f}; |
| 2218 | vec3 origin = {0, 0, 0}; |
Chia-I Wu | c3487c2 | 2015-04-22 14:56:17 +0800 | [diff] [blame] | 2219 | vec3 up = {0.0f, 1.0f, 0.0}; |
Courtney Goeltzenleuchter | 3eeff43 | 2014-10-29 08:29:35 -0600 | [diff] [blame] | 2220 | |
Courtney Goeltzenleuchter | 4e8246e | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 2221 | memset(demo, 0, sizeof(*demo)); |
David Pinedo | eeca2a2 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 2222 | demo->frameCount = INT_MAX; |
Courtney Goeltzenleuchter | 4e8246e | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 2223 | |
Piers Daniell | 886be47 | 2015-02-23 16:23:13 -0700 | [diff] [blame] | 2224 | for (int i = 1; i < argc; i++) { |
Tony Barbour | 5685ad7 | 2015-04-29 16:19:20 -0600 | [diff] [blame] | 2225 | if (strcmp(argv[i], "--use_staging") == 0) { |
Courtney Goeltzenleuchter | 40a8b0a | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 2226 | demo->use_staging_buffer = true; |
Tony Barbour | 5685ad7 | 2015-04-29 16:19:20 -0600 | [diff] [blame] | 2227 | continue; |
Ian Elliott | e14e9f9 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 2228 | } |
Cody Northrop | 75db032 | 2015-05-28 11:27:16 -0600 | [diff] [blame] | 2229 | if (strcmp(argv[i], "--use_glsl") == 0) { |
| 2230 | demo->use_glsl = true; |
| 2231 | continue; |
| 2232 | } |
Tony Barbour | 5685ad7 | 2015-04-29 16:19:20 -0600 | [diff] [blame] | 2233 | if (strcmp(argv[i], "--validate") == 0) { |
| 2234 | demo->validate = true; |
| 2235 | continue; |
| 2236 | } |
David Pinedo | eeca2a2 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 2237 | if (strcmp(argv[i], "--c") == 0 && |
| 2238 | demo->frameCount == INT_MAX && |
| 2239 | i < argc-1 && |
| 2240 | sscanf(argv[i+1],"%d", &demo->frameCount) == 1 && |
| 2241 | demo->frameCount >= 0) |
| 2242 | { |
| 2243 | i++; |
| 2244 | continue; |
| 2245 | } |
Tony Barbour | 5685ad7 | 2015-04-29 16:19:20 -0600 | [diff] [blame] | 2246 | |
David Pinedo | eeca2a2 | 2015-06-18 17:03:14 -0600 | [diff] [blame] | 2247 | fprintf(stderr, "Usage:\n %s [--use_staging] [--validate] [--c <framecount>]\n", APP_SHORT_NAME); |
Ian Elliott | e14e9f9 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 2248 | fflush(stderr); |
| 2249 | exit(1); |
Courtney Goeltzenleuchter | 40a8b0a | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 2250 | } |
| 2251 | |
Courtney Goeltzenleuchter | 4e8246e | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 2252 | demo_init_connection(demo); |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 2253 | demo_init_vk(demo); |
Courtney Goeltzenleuchter | 4e8246e | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 2254 | |
Courtney Goeltzenleuchter | 3eeff43 | 2014-10-29 08:29:35 -0600 | [diff] [blame] | 2255 | demo->width = 500; |
| 2256 | demo->height = 500; |
Courtney Goeltzenleuchter | 3eeff43 | 2014-10-29 08:29:35 -0600 | [diff] [blame] | 2257 | |
| 2258 | demo->spin_angle = 0.01f; |
| 2259 | demo->spin_increment = 0.01f; |
| 2260 | demo->pause = false; |
| 2261 | |
Piers Daniell | 886be47 | 2015-02-23 16:23:13 -0700 | [diff] [blame] | 2262 | mat4x4_perspective(demo->projection_matrix, (float)degreesToRadians(45.0f), 1.0f, 0.1f, 100.0f); |
Courtney Goeltzenleuchter | 3eeff43 | 2014-10-29 08:29:35 -0600 | [diff] [blame] | 2263 | mat4x4_look_at(demo->view_matrix, eye, origin, up); |
| 2264 | mat4x4_identity(demo->model_matrix); |
Courtney Goeltzenleuchter | 4e8246e | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 2265 | } |
| 2266 | |
Courtney Goeltzenleuchter | 4e8246e | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 2267 | |
Ian Elliott | e14e9f9 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 2268 | #ifdef _WIN32 |
Tony Barbour | 5685ad7 | 2015-04-29 16:19:20 -0600 | [diff] [blame] | 2269 | extern int __getmainargs( |
| 2270 | int * _Argc, |
| 2271 | char *** _Argv, |
| 2272 | char *** _Env, |
| 2273 | int _DoWildCard, |
| 2274 | int * new_mode); |
Ian Elliott | 7595eee | 2015-04-28 10:33:11 -0600 | [diff] [blame] | 2275 | |
Ian Elliott | 421107f | 2015-04-28 15:50:36 -0600 | [diff] [blame] | 2276 | int WINAPI WinMain(HINSTANCE hInstance, |
| 2277 | HINSTANCE hPrevInstance, |
| 2278 | LPSTR pCmdLine, |
| 2279 | int nCmdShow) |
Ian Elliott | e14e9f9 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 2280 | { |
| 2281 | MSG msg; // message |
| 2282 | bool done; // flag saying when app is complete |
Tony Barbour | 5685ad7 | 2015-04-29 16:19:20 -0600 | [diff] [blame] | 2283 | int argc; |
| 2284 | char** argv; |
| 2285 | char** env; |
| 2286 | int new_mode = 0; |
Ian Elliott | e14e9f9 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 2287 | |
Tony Barbour | 5685ad7 | 2015-04-29 16:19:20 -0600 | [diff] [blame] | 2288 | __getmainargs(&argc,&argv,&env,0,&new_mode); |
| 2289 | |
| 2290 | demo_init(&demo, argc, argv); |
| 2291 | demo.connection = hInstance; |
| 2292 | strncpy(demo.name, "cube", APP_NAME_STR_LEN); |
Ian Elliott | e14e9f9 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 2293 | demo_create_window(&demo); |
| 2294 | |
| 2295 | demo_prepare(&demo); |
| 2296 | |
| 2297 | done = false; //initialize loop condition variable |
| 2298 | /* main message loop*/ |
| 2299 | while(!done) |
| 2300 | { |
Ian Elliott | 421107f | 2015-04-28 15:50:36 -0600 | [diff] [blame] | 2301 | PeekMessage(&msg, NULL, 0, 0, PM_REMOVE); |
Ian Elliott | e14e9f9 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 2302 | if (msg.message == WM_QUIT) //check for a quit message |
| 2303 | { |
| 2304 | done = true; //if found, quit app |
| 2305 | } |
| 2306 | else |
| 2307 | { |
| 2308 | /* Translate and dispatch to event queue*/ |
| 2309 | TranslateMessage(&msg); |
| 2310 | DispatchMessage(&msg); |
| 2311 | } |
| 2312 | } |
| 2313 | |
| 2314 | demo_cleanup(&demo); |
| 2315 | |
Tony Barbour | a938abb | 2015-04-22 11:36:22 -0600 | [diff] [blame] | 2316 | return (int) msg.wParam; |
Ian Elliott | e14e9f9 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 2317 | } |
| 2318 | #else // _WIN32 |
Courtney Goeltzenleuchter | 40a8b0a | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 2319 | int main(int argc, char **argv) |
Courtney Goeltzenleuchter | 4e8246e | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 2320 | { |
| 2321 | struct demo demo; |
| 2322 | |
Courtney Goeltzenleuchter | 40a8b0a | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 2323 | demo_init(&demo, argc, argv); |
Chia-I Wu | 5b66aa5 | 2015-04-16 22:02:10 +0800 | [diff] [blame] | 2324 | demo_create_window(&demo); |
Courtney Goeltzenleuchter | 4e8246e | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 2325 | |
| 2326 | demo_prepare(&demo); |
Courtney Goeltzenleuchter | 4e8246e | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 2327 | demo_run(&demo); |
| 2328 | |
| 2329 | demo_cleanup(&demo); |
| 2330 | |
| 2331 | return 0; |
| 2332 | } |
Ian Elliott | e14e9f9 | 2015-04-16 15:23:05 -0600 | [diff] [blame] | 2333 | #endif // _WIN32 |