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