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