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