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