blob: 7fd57f19bb1a3f0e89bda91fcb9e0b35ef33f9e9 [file] [log] [blame]
Ian Elliotta748eaf2015-04-28 15:50:36 -06001/*
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 Goeltzenleuchter68369392014-10-29 15:59:49 -060024#define _GNU_SOURCE
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060025#include <stdio.h>
26#include <stdlib.h>
27#include <string.h>
28#include <stdbool.h>
29#include <assert.h>
30
Ian Elliott639ca472015-04-16 15:23:05 -060031#ifdef _WIN32
32#pragma comment(linker, "/subsystem:windows")
33#include <windows.h>
34#define APP_NAME_STR_LEN 80
35#else // _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060036#include <xcb/xcb.h>
Ian Elliott639ca472015-04-16 15:23:05 -060037#endif // _WIN32
38
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -060039#include <vulkan.h>
40#include <vkDbg.h>
Chia-I Wucbb564e2015-04-16 22:02:10 +080041#include <vk_wsi_lunarg.h>
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060042
Cody Northropfe3d8bc2015-03-17 14:54:35 -060043#include "icd-spv.h"
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060044
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -060045#include "linmath.h"
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -060046#include <png.h>
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -060047
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060048#define DEMO_BUFFER_COUNT 2
49#define DEMO_TEXTURE_COUNT 1
Ian Elliott44e33f72015-04-28 10:52:52 -060050#define APP_SHORT_NAME "cube"
51#define APP_LONG_NAME "The Vulkan Cube Demo Program"
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060052
Tony Barbourfdc2d352015-04-22 09:02:32 -060053#if defined(NDEBUG) && defined(__GNUC__)
54#define U_ASSERT_ONLY __attribute__((unused))
55#else
56#define U_ASSERT_ONLY
57#endif
58
Ian Elliott07264132015-04-28 11:35:02 -060059#ifdef _WIN32
60#define ERR_EXIT(err_msg, err_class) \
61 do { \
62 MessageBox(NULL, err_msg, err_class, MB_OK); \
63 exit(1); \
64 } while (0)
65
66// NOTE: If the following values (copied from "loader_platform.h") change, they
67// need to change here as well:
68#define LAYER_NAMES_ENV "VK_LAYER_NAMES"
69#define LAYER_NAMES_REGISTRY_VALUE "VK_LAYER_NAMES"
70
71#else // _WIN32
72
73#define ERR_EXIT(err_msg, err_class) \
74 do { \
75 printf(err_msg); \
76 fflush(stdout); \
77 exit(1); \
78 } while (0)
79#endif // _WIN32
80
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -060081/*
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -070082 * structure to track all objects related to a texture.
83 */
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -060084struct texture_object {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -060085 VkSampler sampler;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -070086
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -060087 VkImage image;
88 VkImageLayout imageLayout;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -060089
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -070090 uint32_t num_mem;
Tony Barbour72304ef2015-04-16 15:59:00 -060091 VkDeviceMemory *mem;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -060092 VkImageView view;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -070093 int32_t tex_width, tex_height;
94};
95
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -060096static char *tex_files[] = {
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -060097 "lunarg-logo-256x256-solid.png"
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -060098};
99
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600100struct vkcube_vs_uniform {
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600101 // Must start with MVP
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600102 float mvp[4][4];
103 float position[12*3][4];
104 float color[12*3][4];
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600105};
106
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600107struct vktexcube_vs_uniform {
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600108 // Must start with MVP
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600109 float mvp[4][4];
110 float position[12*3][4];
111 float attr[12*3][4];
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600112};
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600113
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600114//--------------------------------------------------------------------------------------
115// Mesh and VertexFormat Data
116//--------------------------------------------------------------------------------------
117struct Vertex
118{
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600119 float posX, posY, posZ, posW; // Position data
120 float r, g, b, a; // Color
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600121};
122
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600123struct VertexPosTex
124{
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600125 float posX, posY, posZ, posW; // Position data
126 float u, v, s, t; // Texcoord
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600127};
128
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600129#define XYZ1(_x_, _y_, _z_) (_x_), (_y_), (_z_), 1.f
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600130#define UV(_u_, _v_) (_u_), (_v_), 0.f, 1.f
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600131
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600132static const float g_vertex_buffer_data[] = {
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600133 -1.0f,-1.0f,-1.0f, // Vertex 0
134 -1.0f,-1.0f, 1.0f,
135 -1.0f, 1.0f, 1.0f,
136
137 -1.0f, 1.0f, 1.0f, // Vertex 1
138 -1.0f, 1.0f,-1.0f,
139 -1.0f,-1.0f,-1.0f,
140
141 -1.0f,-1.0f,-1.0f, // Vertex 2
142 1.0f, 1.0f,-1.0f,
143 1.0f,-1.0f,-1.0f,
144
145 -1.0f,-1.0f,-1.0f, // Vertex 3
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600146 -1.0f, 1.0f,-1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600147 1.0f, 1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600148
149 -1.0f,-1.0f,-1.0f, // Vertex 4
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600150 1.0f,-1.0f,-1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600151 1.0f,-1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600152
153 -1.0f,-1.0f,-1.0f, // Vertex 5
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600154 1.0f,-1.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600155 -1.0f,-1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600156
157 -1.0f, 1.0f,-1.0f, // Vertex 6
158 -1.0f, 1.0f, 1.0f,
159 1.0f, 1.0f, 1.0f,
160
161 -1.0f, 1.0f,-1.0f, // Vertex 7
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600162 1.0f, 1.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600163 1.0f, 1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600164
165 1.0f, 1.0f,-1.0f, // Vertex 8
166 1.0f, 1.0f, 1.0f,
167 1.0f,-1.0f, 1.0f,
168
169 1.0f,-1.0f, 1.0f, // Vertex 9
170 1.0f,-1.0f,-1.0f,
171 1.0f, 1.0f,-1.0f,
172
173 -1.0f, 1.0f, 1.0f, // Vertex 10
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600174 -1.0f,-1.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600175 1.0f, 1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600176
177 -1.0f,-1.0f, 1.0f, // Vertex 11
178 1.0f,-1.0f, 1.0f,
179 1.0f, 1.0f, 1.0f,
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600180};
181
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600182static const float g_uv_buffer_data[] = {
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600183 1.0f, 0.0f, // Vertex 0
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600184 0.0f, 0.0f,
185 0.0f, 1.0f,
186
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600187 0.0f, 1.0f, // Vertex 1
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600188 1.0f, 1.0f,
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600189 1.0f, 0.0f,
190
191// 0.0f, 1.0f, // Vertex 2
192// 1.0f, 0.0f,
193// 0.0f, 0.0f,
194
195// 0.0f, 1.0f, // Vertex 3
196// 1.0f, 0.0f,
197// 1.0f, 1.0f,
198
199 0.0f, 0.0f, // Vertex 2
200 1.0f, 1.0f,
201 1.0f, 0.0f,
202
203 0.0f, 0.0f, // Vertex 3
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600204 0.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600205 1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600206
207 0.0f, 1.0f, // Vertex 4
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600208 0.0f, 0.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600209 1.0f, 0.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600210
211 0.0f, 1.0f, // Vertex 5
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600212 1.0f, 0.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600213 1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600214
215 0.0f, 1.0f, // Vertex 6
216 1.0f, 1.0f,
217 1.0f, 0.0f,
218
219 0.0f, 1.0f, // Vertex 7
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600220 1.0f, 0.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600221 0.0f, 0.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600222
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600223 0.0f, 1.0f, // Vertex 8
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600224 1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600225 1.0f, 0.0f,
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600226
227 1.0f, 0.0f, // Vertex 9
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600228 0.0f, 0.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600229 0.0f, 1.0f,
230
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600231 1.0f, 1.0f, // Vertex 10
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600232 1.0f, 0.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600233 0.0f, 1.0f,
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600234
235 1.0f, 0.0f, // Vertex 11
236 0.0f, 0.0f,
237 0.0f, 1.0f,
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600238};
239
240void dumpMatrix(const char *note, mat4x4 MVP)
241{
242 int i;
243
244 printf("%s: \n", note);
245 for (i=0; i<4; i++) {
246 printf("%f, %f, %f, %f\n", MVP[i][0], MVP[i][1], MVP[i][2], MVP[i][3]);
247 }
248 printf("\n");
249 fflush(stdout);
250}
251
252void dumpVec4(const char *note, vec4 vector)
253{
254 printf("%s: \n", note);
255 printf("%f, %f, %f, %f\n", vector[0], vector[1], vector[2], vector[3]);
256 printf("\n");
257 fflush(stdout);
258}
259
Tony Barbour3dddd5d2015-04-29 16:19:20 -0600260void VKAPI dbgFunc(
261 VK_DBG_MSG_TYPE msgType,
262 VkValidationLevel validationLevel,
263 VkObject srcObject,
264 size_t location,
265 int32_t msgCode,
266 const char* pMsg,
267 void* pUserData)
268{
269 char *message = (char *) malloc(strlen(pMsg)+100);
270
271 assert (message);
272
273 if (msgType == VK_DBG_MSG_ERROR) {
274 sprintf(message,"ERROR: Code %d : %s", msgCode, pMsg);
275 } else if (msgType == VK_DBG_MSG_WARNING) {
276 sprintf(message,"WARNING: Code %d : %s", msgCode, pMsg);
277 } else {
278 return;
279 }
280
281#ifdef _WIN32
282 MessageBox(NULL, message, "Alert", MB_OK);
283#else
284 printf("%s\n",message);
285 fflush(stdout);
286#endif
287 free(message);
288}
289
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600290struct demo {
Ian Elliott639ca472015-04-16 15:23:05 -0600291#ifdef _WIN32
292#define APP_NAME_STR_LEN 80
293 HINSTANCE connection; // hInstance - Windows Instance
294 char name[APP_NAME_STR_LEN]; // Name to put on the window/icon
295 HWND window; // hWnd - window handle
296#else // _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600297 xcb_connection_t *connection;
298 xcb_screen_t *screen;
Chia-I Wucbb564e2015-04-16 22:02:10 +0800299 xcb_window_t window;
300 xcb_intern_atom_reply_t *atom_wm_delete_window;
Ian Elliott639ca472015-04-16 15:23:05 -0600301#endif // _WIN32
Jon Ashburn8d26c062015-04-24 09:46:24 -0700302 bool prepared;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700303 bool use_staging_buffer;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600304
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600305 VkInstance inst;
Tony Barbour72304ef2015-04-16 15:59:00 -0600306 VkPhysicalDevice gpu;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600307 VkDevice device;
308 VkQueue queue;
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -0700309 uint32_t graphics_queue_node_index;
Tony Barbour72304ef2015-04-16 15:59:00 -0600310 VkPhysicalDeviceProperties *gpu_props;
311 VkPhysicalDeviceQueueProperties *queue_props;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600312
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600313 VkFramebuffer framebuffer;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600314 int width, height;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600315 VkFormat format;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600316
Chia-I Wucbb564e2015-04-16 22:02:10 +0800317 VkSwapChainWSI swap_chain;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600318 struct {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600319 VkImage image;
Tony Barbour72304ef2015-04-16 15:59:00 -0600320 VkDeviceMemory mem;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600321 VkCmdBuffer cmd;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600322
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600323 VkColorAttachmentView view;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600324 } buffers[DEMO_BUFFER_COUNT];
325
326 struct {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600327 VkFormat format;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600328
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600329 VkImage image;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600330 uint32_t num_mem;
Tony Barbour72304ef2015-04-16 15:59:00 -0600331 VkDeviceMemory *mem;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600332 VkDepthStencilView view;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600333 } depth;
334
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600335 struct texture_object textures[DEMO_TEXTURE_COUNT];
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600336
337 struct {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600338 VkBuffer buf;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600339 uint32_t num_mem;
Tony Barbour72304ef2015-04-16 15:59:00 -0600340 VkDeviceMemory *mem;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600341 VkBufferView view;
342 VkBufferViewAttachInfo attach;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600343 } uniform_data;
344
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600345 VkCmdBuffer cmd; // Buffer for initialization commands
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -0500346 VkPipelineLayout pipeline_layout;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600347 VkDescriptorSetLayout desc_layout;
348 VkPipeline pipeline;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600349
Courtney Goeltzenleuchter5e6d1e92015-04-10 16:24:50 -0600350 VkDynamicVpState viewport;
351 VkDynamicRsState raster;
352 VkDynamicCbState color_blend;
353 VkDynamicDsState depth_stencil;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600354
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600355 mat4x4 projection_matrix;
356 mat4x4 view_matrix;
357 mat4x4 model_matrix;
358
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600359 float spin_angle;
360 float spin_increment;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600361 bool pause;
362
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600363 VkDescriptorPool desc_pool;
364 VkDescriptorSet desc_set;
Chia-I Wu6a3c8972015-01-04 16:27:24 +0800365
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600366 bool quit;
Tony Barbour3dddd5d2015-04-29 16:19:20 -0600367 bool validate;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600368 uint32_t current_buffer;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600369};
370
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600371static void demo_flush_init_cmd(struct demo *demo)
372{
Tony Barbourfdc2d352015-04-22 09:02:32 -0600373 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600374
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600375 if (demo->cmd == VK_NULL_HANDLE)
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600376 return;
377
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600378 err = vkEndCommandBuffer(demo->cmd);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600379 assert(!err);
380
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600381 const VkCmdBuffer cmd_bufs[] = { demo->cmd };
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600382
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600383 err = vkQueueSubmit(demo->queue, 1, cmd_bufs, VK_NULL_HANDLE);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600384 assert(!err);
385
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600386 err = vkQueueWaitIdle(demo->queue);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600387 assert(!err);
388
Mike Stroyanebae8322015-04-17 12:36:38 -0600389 vkDestroyObject(demo->device, VK_OBJECT_TYPE_COMMAND_BUFFER, demo->cmd);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600390 demo->cmd = VK_NULL_HANDLE;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600391}
392
393static void demo_add_mem_refs(
394 struct demo *demo,
Tony Barbour72304ef2015-04-16 15:59:00 -0600395 int num_refs, VkDeviceMemory *mem)
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600396{
Courtney Goeltzenleuchter6b0caf12015-04-16 13:38:46 -0600397 vkQueueAddMemReferences(demo->queue, num_refs, mem);
Courtney Goeltzenleuchter1359b7a2015-04-02 16:25:42 -0600398}
399
400static void demo_remove_mem_refs(
401 struct demo *demo,
Tony Barbour72304ef2015-04-16 15:59:00 -0600402 int num_refs, VkDeviceMemory *mem)
Courtney Goeltzenleuchter1359b7a2015-04-02 16:25:42 -0600403{
Courtney Goeltzenleuchter6b0caf12015-04-16 13:38:46 -0600404 vkQueueRemoveMemReferences(demo->queue, num_refs, mem);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600405}
406
407static void demo_set_image_layout(
408 struct demo *demo,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600409 VkImage image,
410 VkImageLayout old_image_layout,
411 VkImageLayout new_image_layout)
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600412{
Tony Barbourfdc2d352015-04-22 09:02:32 -0600413 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600414
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600415 if (demo->cmd == VK_NULL_HANDLE) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600416 const VkCmdBufferCreateInfo cmd = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600417 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600418 .pNext = NULL,
419 .queueNodeIndex = demo->graphics_queue_node_index,
420 .flags = 0,
421 };
422
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600423 err = vkCreateCommandBuffer(demo->device, &cmd, &demo->cmd);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600424 assert(!err);
425
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600426 VkCmdBufferBeginInfo cmd_buf_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600427 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600428 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -0600429 .flags = VK_CMD_BUFFER_OPTIMIZE_SMALL_BATCH_BIT |
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600430 VK_CMD_BUFFER_OPTIMIZE_ONE_TIME_SUBMIT_BIT,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600431 };
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600432 err = vkBeginCommandBuffer(demo->cmd, &cmd_buf_info);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600433 }
434
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600435 VkImageMemoryBarrier image_memory_barrier = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600436 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600437 .pNext = NULL,
438 .outputMask = 0,
439 .inputMask = 0,
440 .oldLayout = old_image_layout,
441 .newLayout = new_image_layout,
442 .image = image,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600443 .subresourceRange = { VK_IMAGE_ASPECT_COLOR, 0, 1, 0, 0 }
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600444 };
445
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600446 if (new_image_layout == VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL) {
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600447 /* Make sure anything that was copying from this image has completed */
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -0600448 image_memory_barrier.inputMask = VK_MEMORY_INPUT_TRANSFER_BIT;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600449 }
450
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600451 if (new_image_layout == VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL) {
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600452 /* Make sure any Copy or CPU writes to image are flushed */
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -0600453 image_memory_barrier.outputMask = VK_MEMORY_OUTPUT_CPU_WRITE_BIT | VK_MEMORY_OUTPUT_TRANSFER_BIT;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600454 }
455
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600456 VkImageMemoryBarrier *pmemory_barrier = &image_memory_barrier;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600457
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600458 VkPipeEvent set_events[] = { VK_PIPE_EVENT_TOP_OF_PIPE };
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600459
Tony Barbour72304ef2015-04-16 15:59:00 -0600460 vkCmdPipelineBarrier(demo->cmd, VK_WAIT_EVENT_TOP_OF_PIPE, 1, set_events, 1, (const void **)&pmemory_barrier);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600461}
462
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600463static void demo_draw_build_cmd(struct demo *demo, VkCmdBuffer cmd_buf)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600464{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600465 const VkColorAttachmentBindInfo color_attachment = {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600466 .view = demo->buffers[demo->current_buffer].view,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600467 .layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600468 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600469 const VkDepthStencilBindInfo depth_stencil = {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600470 .view = demo->depth.view,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600471 .layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600472 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600473 const VkClearColor clear_color = {
Courtney Goeltzenleuchter374553c2015-04-03 16:35:32 -0600474 .color.floatColor = { 0.2f, 0.2f, 0.2f, 0.2f },
475 .useRawValue = false,
476 };
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600477 const float clear_depth = 1.0f;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600478 VkImageSubresourceRange clear_range;
479 VkCmdBufferBeginInfo cmd_buf_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600480 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO,
Courtney Goeltzenleuchter53968d82015-04-03 15:25:24 -0600481 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -0600482 .flags = VK_CMD_BUFFER_OPTIMIZE_SMALL_BATCH_BIT |
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600483 VK_CMD_BUFFER_OPTIMIZE_ONE_TIME_SUBMIT_BIT,
Jon Ashburn60ccfbe2014-12-31 17:08:35 -0700484 };
Tony Barbourfdc2d352015-04-22 09:02:32 -0600485 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600486 VkAttachmentLoadOp load_op = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
487 VkAttachmentStoreOp store_op = VK_ATTACHMENT_STORE_OP_DONT_CARE;
488 const VkFramebufferCreateInfo fb_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600489 .sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO,
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700490 .pNext = NULL,
491 .colorAttachmentCount = 1,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600492 .pColorAttachments = (VkColorAttachmentBindInfo*) &color_attachment,
493 .pDepthStencilAttachment = (VkDepthStencilBindInfo*) &depth_stencil,
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700494 .sampleCount = 1,
Mark Lobodzinskic06b7412015-01-27 13:24:03 -0600495 .width = demo->width,
496 .height = demo->height,
497 .layers = 1,
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700498 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600499 VkRenderPassCreateInfo rp_info;
500 VkRenderPassBegin rp_begin;
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700501
502 memset(&rp_info, 0 , sizeof(rp_info));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600503 err = vkCreateFramebuffer(demo->device, &fb_info, &rp_begin.framebuffer);
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700504 assert(!err);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600505 rp_info.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO;
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700506 rp_info.renderArea.extent.width = demo->width;
507 rp_info.renderArea.extent.height = demo->height;
Courtney Goeltzenleuchter53968d82015-04-03 15:25:24 -0600508 rp_info.colorAttachmentCount = fb_info.colorAttachmentCount;
509 rp_info.pColorFormats = &demo->format;
510 rp_info.pColorLayouts = &color_attachment.layout;
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700511 rp_info.pColorLoadOps = &load_op;
512 rp_info.pColorStoreOps = &store_op;
Courtney Goeltzenleuchter53968d82015-04-03 15:25:24 -0600513 rp_info.pColorLoadClearValues = &clear_color;
Tony Barbour72304ef2015-04-16 15:59:00 -0600514 rp_info.depthStencilFormat = VK_FORMAT_D16_UNORM;
Courtney Goeltzenleuchter53968d82015-04-03 15:25:24 -0600515 rp_info.depthStencilLayout = depth_stencil.layout;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600516 rp_info.depthLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
Courtney Goeltzenleuchter53968d82015-04-03 15:25:24 -0600517 rp_info.depthLoadClearValue = clear_depth;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600518 rp_info.depthStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
519 rp_info.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
Courtney Goeltzenleuchter53968d82015-04-03 15:25:24 -0600520 rp_info.stencilLoadClearValue = 0;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600521 rp_info.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
522 err = vkCreateRenderPass(demo->device, &rp_info, &rp_begin.renderPass);
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700523 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600524
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600525 err = vkBeginCommandBuffer(cmd_buf, &cmd_buf_info);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600526 assert(!err);
527
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600528 vkCmdBindPipeline(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600529 demo->pipeline);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600530 vkCmdBindDescriptorSets(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS,
Cody Northropfb5185a2015-04-16 13:41:56 -0600531 0, 1, &demo->desc_set, 0, NULL);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600532
Tony Barbour72304ef2015-04-16 15:59:00 -0600533 vkCmdBindDynamicStateObject(cmd_buf, VK_STATE_BIND_POINT_VIEWPORT, demo->viewport);
534 vkCmdBindDynamicStateObject(cmd_buf, VK_STATE_BIND_POINT_RASTER, demo->raster);
535 vkCmdBindDynamicStateObject(cmd_buf, VK_STATE_BIND_POINT_COLOR_BLEND,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600536 demo->color_blend);
Tony Barbour72304ef2015-04-16 15:59:00 -0600537 vkCmdBindDynamicStateObject(cmd_buf, VK_STATE_BIND_POINT_DEPTH_STENCIL,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600538 demo->depth_stencil);
539
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600540 vkCmdBeginRenderPass(cmd_buf, &rp_begin);
541 clear_range.aspect = VK_IMAGE_ASPECT_COLOR;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600542 clear_range.baseMipLevel = 0;
543 clear_range.mipLevels = 1;
544 clear_range.baseArraySlice = 0;
545 clear_range.arraySize = 1;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600546 vkCmdClearColorImage(cmd_buf,
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600547 demo->buffers[demo->current_buffer].image,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600548 VK_IMAGE_LAYOUT_CLEAR_OPTIMAL,
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600549 clear_color, 1, &clear_range);
550
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600551 clear_range.aspect = VK_IMAGE_ASPECT_DEPTH;
552 vkCmdClearDepthStencil(cmd_buf, demo->depth.image,
553 VK_IMAGE_LAYOUT_CLEAR_OPTIMAL,
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600554 clear_depth, 0, 1, &clear_range);
555
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600556 vkCmdDraw(cmd_buf, 0, 12 * 3, 0, 1);
557 vkCmdEndRenderPass(cmd_buf, rp_begin.renderPass);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600558
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600559 err = vkEndCommandBuffer(cmd_buf);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600560 assert(!err);
Courtney Goeltzenleuchter94901dc2015-02-25 17:53:18 -0700561
Mike Stroyanebae8322015-04-17 12:36:38 -0600562 vkDestroyObject(demo->device, VK_OBJECT_TYPE_RENDER_PASS, rp_begin.renderPass);
563 vkDestroyObject(demo->device, VK_OBJECT_TYPE_FRAMEBUFFER, rp_begin.framebuffer);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600564}
565
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600566
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600567void demo_update_data_buffer(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600568{
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600569 mat4x4 MVP, Model, VP;
570 int matrixSize = sizeof(MVP);
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600571 uint8_t *pData;
Tony Barbourfdc2d352015-04-22 09:02:32 -0600572 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600573
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600574 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600575
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600576 // Rotate 22.5 degrees around the Y axis
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600577 mat4x4_dup(Model, demo->model_matrix);
Piers Daniell735ee532015-02-23 16:23:13 -0700578 mat4x4_rotate(demo->model_matrix, Model, 0.0f, 1.0f, 0.0f, (float)degreesToRadians(demo->spin_angle));
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600579 mat4x4_mul(MVP, VP, demo->model_matrix);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600580
Jon Ashburnae7c21c2015-01-19 15:00:26 -0700581 assert(demo->uniform_data.num_mem == 1);
Mike Stroyanebae8322015-04-17 12:36:38 -0600582 err = vkMapMemory(demo->device, demo->uniform_data.mem[0], 0, 0, 0, (void **) &pData);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600583 assert(!err);
584
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600585 memcpy(pData, (const void*) &MVP[0][0], matrixSize);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600586
Mike Stroyanebae8322015-04-17 12:36:38 -0600587 err = vkUnmapMemory(demo->device, demo->uniform_data.mem[0]);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600588 assert(!err);
589}
590
591static void demo_draw(struct demo *demo)
592{
Chia-I Wucbb564e2015-04-16 22:02:10 +0800593 const VkPresentInfoWSI present = {
594 .sType = VK_STRUCTURE_TYPE_PRESENT_INFO_WSI,
595 .pNext = NULL,
596 .image = demo->buffers[demo->current_buffer].image,
597 .flipInterval = 0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600598 };
Tony Barbourfdc2d352015-04-22 09:02:32 -0600599 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600600
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600601 err = vkQueueSubmit(demo->queue, 1, &demo->buffers[demo->current_buffer].cmd,
602 VK_NULL_HANDLE);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600603 assert(!err);
604
Chia-I Wucbb564e2015-04-16 22:02:10 +0800605 err = vkQueuePresentWSI(demo->queue, &present);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600606 assert(!err);
607
608 demo->current_buffer = (demo->current_buffer + 1) % DEMO_BUFFER_COUNT;
Chia-I Wucbb564e2015-04-16 22:02:10 +0800609
610 err = vkQueueWaitIdle(demo->queue);
611 assert(err == VK_SUCCESS);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600612}
613
614static void demo_prepare_buffers(struct demo *demo)
615{
Chia-I Wucbb564e2015-04-16 22:02:10 +0800616 const VkSwapChainCreateInfoWSI swap_chain = {
617 .sType = VK_STRUCTURE_TYPE_SWAP_CHAIN_CREATE_INFO_WSI,
618 .pNext = NULL,
619 .pNativeWindowSystemHandle = demo->connection,
620 .pNativeWindowHandle = (void *) (intptr_t) demo->window,
621 .imageCount = DEMO_BUFFER_COUNT,
622 .imageFormat = demo->format,
623 .imageExtent = {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600624 .width = demo->width,
625 .height = demo->height,
626 },
Chia-I Wucbb564e2015-04-16 22:02:10 +0800627 .imageArraySize = 1,
628 .imageUsageFlags = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600629 };
Chia-I Wucbb564e2015-04-16 22:02:10 +0800630 VkSwapChainImageInfoWSI images[DEMO_BUFFER_COUNT];
631 size_t images_size = sizeof(images);
Tony Barbourfdc2d352015-04-22 09:02:32 -0600632 VkResult U_ASSERT_ONLY err;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600633 uint32_t i;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600634
Chia-I Wucbb564e2015-04-16 22:02:10 +0800635 err = vkCreateSwapChainWSI(demo->device, &swap_chain, &demo->swap_chain);
636 assert(!err);
637
638 err = vkGetSwapChainInfoWSI(demo->swap_chain,
639 VK_SWAP_CHAIN_INFO_TYPE_PERSISTENT_IMAGES_WSI,
640 &images_size, images);
641 assert(!err && images_size == sizeof(images));
642
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600643 for (i = 0; i < DEMO_BUFFER_COUNT; i++) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600644 VkColorAttachmentViewCreateInfo color_attachment_view = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600645 .sType = VK_STRUCTURE_TYPE_COLOR_ATTACHMENT_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600646 .pNext = NULL,
647 .format = demo->format,
648 .mipLevel = 0,
649 .baseArraySlice = 0,
650 .arraySize = 1,
651 };
652
Chia-I Wucbb564e2015-04-16 22:02:10 +0800653 demo->buffers[i].image = images[i].image;
654 demo->buffers[i].mem = images[i].memory;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600655
Courtney Goeltzenleuchter2c2fbbf2015-04-07 17:13:38 -0600656 demo_add_mem_refs(demo, 1, &demo->buffers[i].mem);
Courtney Goeltzenleuchter1359b7a2015-04-02 16:25:42 -0600657
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600658 demo_set_image_layout(demo, demo->buffers[i].image,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600659 VK_IMAGE_LAYOUT_UNDEFINED,
660 VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600661
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600662 color_attachment_view.image = demo->buffers[i].image;
663
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600664 err = vkCreateColorAttachmentView(demo->device,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600665 &color_attachment_view, &demo->buffers[i].view);
666 assert(!err);
667 }
668}
669
670static void demo_prepare_depth(struct demo *demo)
671{
Tony Barbour72304ef2015-04-16 15:59:00 -0600672 const VkFormat depth_format = VK_FORMAT_D16_UNORM;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600673 const VkImageCreateInfo image = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600674 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600675 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -0600676 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600677 .format = depth_format,
678 .extent = { demo->width, demo->height, 1 },
679 .mipLevels = 1,
680 .arraySize = 1,
681 .samples = 1,
Tony Barbour72304ef2015-04-16 15:59:00 -0600682 .tiling = VK_IMAGE_TILING_OPTIMAL,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600683 .usage = VK_IMAGE_USAGE_DEPTH_STENCIL_BIT,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600684 .flags = 0,
685 };
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -0600686 VkMemoryAllocInfo mem_alloc = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600687 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
Mark Lobodzinskia6907f72015-04-16 08:52:00 -0500688 .pNext = NULL,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600689 .allocationSize = 0,
Tony Barbour72304ef2015-04-16 15:59:00 -0600690 .memProps = VK_MEMORY_PROPERTY_DEVICE_ONLY,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600691 .memPriority = VK_MEMORY_PRIORITY_NORMAL,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600692 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600693 VkDepthStencilViewCreateInfo view = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600694 .sType = VK_STRUCTURE_TYPE_DEPTH_STENCIL_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600695 .pNext = NULL,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600696 .image = VK_NULL_HANDLE,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600697 .mipLevel = 0,
698 .baseArraySlice = 0,
699 .arraySize = 1,
700 .flags = 0,
701 };
Mike Stroyanebae8322015-04-17 12:36:38 -0600702
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600703 VkMemoryRequirements *mem_reqs;
704 size_t mem_reqs_size = sizeof(VkMemoryRequirements);
Tony Barbourfdc2d352015-04-22 09:02:32 -0600705 VkResult U_ASSERT_ONLY err;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600706 uint32_t num_allocations = 0;
707 size_t num_alloc_size = sizeof(num_allocations);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600708
709 demo->depth.format = depth_format;
710
711 /* create image */
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600712 err = vkCreateImage(demo->device, &image,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600713 &demo->depth.image);
714 assert(!err);
715
Mike Stroyanebae8322015-04-17 12:36:38 -0600716 err = vkGetObjectInfo(demo->device,
717 VK_OBJECT_TYPE_IMAGE, demo->depth.image,
718 VK_OBJECT_INFO_TYPE_MEMORY_ALLOCATION_COUNT,
719 &num_alloc_size, &num_allocations);
Jon Ashburnb2a66652015-01-16 09:37:43 -0700720 assert(!err && num_alloc_size == sizeof(num_allocations));
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600721 mem_reqs = malloc(num_allocations * sizeof(VkMemoryRequirements));
Tony Barbour72304ef2015-04-16 15:59:00 -0600722 demo->depth.mem = malloc(num_allocations * sizeof(VkDeviceMemory));
Jon Ashburnb2a66652015-01-16 09:37:43 -0700723 demo->depth.num_mem = num_allocations;
Mike Stroyanebae8322015-04-17 12:36:38 -0600724 err = vkGetObjectInfo(demo->device,
725 VK_OBJECT_TYPE_IMAGE, demo->depth.image,
Tony Barbour72304ef2015-04-16 15:59:00 -0600726 VK_OBJECT_INFO_TYPE_MEMORY_REQUIREMENTS,
Jon Ashburnb2a66652015-01-16 09:37:43 -0700727 &mem_reqs_size, mem_reqs);
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600728 assert(!err && mem_reqs_size == num_allocations * sizeof(VkMemoryRequirements));
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600729 for (uint32_t i = 0; i < num_allocations; i ++) {
Jon Ashburnb2a66652015-01-16 09:37:43 -0700730 mem_alloc.allocationSize = mem_reqs[i].size;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600731
Jon Ashburnb2a66652015-01-16 09:37:43 -0700732 /* allocate memory */
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600733 err = vkAllocMemory(demo->device, &mem_alloc,
Jon Ashburnb2a66652015-01-16 09:37:43 -0700734 &(demo->depth.mem[i]));
735 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600736
Jon Ashburnb2a66652015-01-16 09:37:43 -0700737 /* bind memory */
Mike Stroyanebae8322015-04-17 12:36:38 -0600738 err = vkQueueBindObjectMemory(demo->queue,
739 VK_OBJECT_TYPE_IMAGE, demo->depth.image,
740 i, demo->depth.mem[i], 0);
Jon Ashburnb2a66652015-01-16 09:37:43 -0700741 assert(!err);
742 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600743
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600744 demo_set_image_layout(demo, demo->depth.image,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600745 VK_IMAGE_LAYOUT_UNDEFINED,
746 VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600747
Courtney Goeltzenleuchter2c2fbbf2015-04-07 17:13:38 -0600748 demo_add_mem_refs(demo, demo->depth.num_mem, demo->depth.mem);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600749
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600750 /* create image view */
751 view.image = demo->depth.image;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600752 err = vkCreateDepthStencilView(demo->device, &view,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600753 &demo->depth.view);
754 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600755}
756
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600757/** loadTexture
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -0600758 * loads a png file into an memory object, using cstdio , libpng.
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600759 *
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -0600760 * \param demo : Needed to access VK calls
761 * \param filename : the png file to be loaded
762 * \param width : width of png, to be updated as a side effect of this function
763 * \param height : height of png, to be updated as a side effect of this function
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600764 *
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -0600765 * \return bool : an opengl texture id. true if successful?,
766 * should be validated by the client of this function.
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600767 *
768 * Source: http://en.wikibooks.org/wiki/OpenGL_Programming/Intermediate/Textures
769 * Modified to copy image to memory
770 *
771 */
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700772bool loadTexture(const char *filename, uint8_t *rgba_data,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600773 VkSubresourceLayout *layout,
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600774 int32_t *width, int32_t *height)
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600775{
776 //header for testing if it is a png
777 png_byte header[8];
Tony Barbourf9921732015-04-22 11:36:22 -0600778 int is_png, bit_depth, color_type, rowbytes;
779 size_t retval;
Ian Elliott1e42dff2015-02-13 14:29:21 -0700780 png_uint_32 i, twidth, theight;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600781 png_structp png_ptr;
782 png_infop info_ptr, end_info;
783 png_byte *image_data;
784 png_bytep *row_pointers;
785
786 //open file as binary
787 FILE *fp = fopen(filename, "rb");
788 if (!fp) {
789 return false;
790 }
791
792 //read the header
Tony Barbourfdc2d352015-04-22 09:02:32 -0600793 retval = fread(header, 1, 8, fp);
794 if (retval != 8) {
795 fclose(fp);
796 return false;
797 }
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600798
799 //test if png
800 is_png = !png_sig_cmp(header, 0, 8);
801 if (!is_png) {
802 fclose(fp);
803 return false;
804 }
805
806 //create png struct
807 png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL,
808 NULL, NULL);
809 if (!png_ptr) {
810 fclose(fp);
811 return (false);
812 }
813
814 //create png info struct
815 info_ptr = png_create_info_struct(png_ptr);
816 if (!info_ptr) {
817 png_destroy_read_struct(&png_ptr, (png_infopp) NULL, (png_infopp) NULL);
818 fclose(fp);
819 return (false);
820 }
821
822 //create png info struct
823 end_info = png_create_info_struct(png_ptr);
824 if (!end_info) {
825 png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp) NULL);
826 fclose(fp);
827 return (false);
828 }
829
830 //png error stuff, not sure libpng man suggests this.
831 if (setjmp(png_jmpbuf(png_ptr))) {
832 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
833 fclose(fp);
834 return (false);
835 }
836
837 //init png reading
838 png_init_io(png_ptr, fp);
839
840 //let libpng know you already read the first 8 bytes
841 png_set_sig_bytes(png_ptr, 8);
842
843 // read all the info up to the image data
844 png_read_info(png_ptr, info_ptr);
845
846 // get info about png
847 png_get_IHDR(png_ptr, info_ptr, &twidth, &theight, &bit_depth, &color_type,
848 NULL, NULL, NULL);
849
850 //update width and height based on png info
851 *width = twidth;
852 *height = theight;
853
854 // Require that incoming texture be 8bits per color component
855 // and 4 components (RGBA).
856 if (png_get_bit_depth(png_ptr, info_ptr) != 8 ||
857 png_get_channels(png_ptr, info_ptr) != 4) {
858 return false;
859 }
860
861 if (rgba_data == NULL) {
862 // If data pointer is null, we just want the width & height
863 // clean up memory and close stuff
864 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
865 fclose(fp);
866
867 return true;
868 }
869
870 // Update the png info struct.
871 png_read_update_info(png_ptr, info_ptr);
872
873 // Row size in bytes.
874 rowbytes = png_get_rowbytes(png_ptr, info_ptr);
875
876 // Allocate the image_data as a big block, to be given to opengl
877 image_data = (png_byte *)malloc(rowbytes * theight * sizeof(png_byte));
878 if (!image_data) {
879 //clean up memory and close stuff
880 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
881 fclose(fp);
882 return false;
883 }
884
885 // row_pointers is for pointing to image_data for reading the png with libpng
886 row_pointers = (png_bytep *)malloc(theight * sizeof(png_bytep));
887 if (!row_pointers) {
888 //clean up memory and close stuff
889 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
890 // delete[] image_data;
891 fclose(fp);
892 return false;
893 }
894 // set the individual row_pointers to point at the correct offsets of image_data
895 for (i = 0; i < theight; ++i)
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700896 row_pointers[theight - 1 - i] = rgba_data + i * layout->rowPitch;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600897
898 // read the png into image_data through row_pointers
899 png_read_image(png_ptr, row_pointers);
900
901 // clean up memory and close stuff
902 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
903 free(row_pointers);
904 free(image_data);
905 fclose(fp);
906
907 return true;
908}
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600909
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700910static void demo_prepare_texture_image(struct demo *demo,
911 const char *filename,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600912 struct texture_object *tex_obj,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600913 VkImageTiling tiling,
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -0600914 VkImageUsageFlags usage,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600915 VkFlags mem_props)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600916{
Tony Barbour72304ef2015-04-16 15:59:00 -0600917 const VkFormat tex_format = VK_FORMAT_B8G8R8A8_UNORM;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600918 int32_t tex_width;
919 int32_t tex_height;
Tony Barbourfdc2d352015-04-22 09:02:32 -0600920 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700921
David Pinedo30bd71d2015-04-23 08:16:57 -0600922 if (!loadTexture(filename, NULL, NULL, &tex_width, &tex_height))
923 {
924 printf("Failed to load textures\n");
925 fflush(stdout);
926 exit(1);
927 }
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700928
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600929 tex_obj->tex_width = tex_width;
930 tex_obj->tex_height = tex_height;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700931
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600932 const VkImageCreateInfo image_create_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600933 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700934 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -0600935 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700936 .format = tex_format,
937 .extent = { tex_width, tex_height, 1 },
938 .mipLevels = 1,
939 .arraySize = 1,
940 .samples = 1,
941 .tiling = tiling,
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -0600942 .usage = usage,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700943 .flags = 0,
944 };
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -0600945 VkMemoryAllocInfo mem_alloc = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600946 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
Mark Lobodzinskia6907f72015-04-16 08:52:00 -0500947 .pNext = NULL,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700948 .allocationSize = 0,
949 .memProps = mem_props,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600950 .memPriority = VK_MEMORY_PRIORITY_NORMAL,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700951 };
952
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600953 VkMemoryRequirements *mem_reqs;
954 size_t mem_reqs_size = sizeof(VkMemoryRequirements);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700955 uint32_t num_allocations = 0;
956 size_t num_alloc_size = sizeof(num_allocations);
957
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600958 err = vkCreateImage(demo->device, &image_create_info,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600959 &tex_obj->image);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700960 assert(!err);
961
Mike Stroyanebae8322015-04-17 12:36:38 -0600962 err = vkGetObjectInfo(demo->device,
963 VK_OBJECT_TYPE_IMAGE, tex_obj->image,
Tony Barbour72304ef2015-04-16 15:59:00 -0600964 VK_OBJECT_INFO_TYPE_MEMORY_ALLOCATION_COUNT,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700965 &num_alloc_size, &num_allocations);
966 assert(!err && num_alloc_size == sizeof(num_allocations));
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600967 mem_reqs = malloc(num_allocations * sizeof(VkMemoryRequirements));
Tony Barbour72304ef2015-04-16 15:59:00 -0600968 tex_obj->mem = malloc(num_allocations * sizeof(VkDeviceMemory));
Mike Stroyanebae8322015-04-17 12:36:38 -0600969 err = vkGetObjectInfo(demo->device,
970 VK_OBJECT_TYPE_IMAGE, tex_obj->image,
Tony Barbour72304ef2015-04-16 15:59:00 -0600971 VK_OBJECT_INFO_TYPE_MEMORY_REQUIREMENTS,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700972 &mem_reqs_size, mem_reqs);
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600973 assert(!err && mem_reqs_size == num_allocations * sizeof(VkMemoryRequirements));
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700974 for (uint32_t j = 0; j < num_allocations; j ++) {
Piers Daniell735ee532015-02-23 16:23:13 -0700975 mem_alloc.allocationSize = mem_reqs[j].size;
976
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700977 /* allocate memory */
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600978 err = vkAllocMemory(demo->device, &mem_alloc,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600979 &(tex_obj->mem[j]));
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700980 assert(!err);
981
982 /* bind memory */
Mike Stroyanebae8322015-04-17 12:36:38 -0600983 err = vkQueueBindObjectMemory(demo->queue,
984 VK_OBJECT_TYPE_IMAGE, tex_obj->image,
985 j, tex_obj->mem[j], 0);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700986 assert(!err);
987 }
988 free(mem_reqs);
989 mem_reqs = NULL;
990
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600991 tex_obj->num_mem = num_allocations;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700992
Tony Barbour72304ef2015-04-16 15:59:00 -0600993 if (mem_props & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600994 const VkImageSubresource subres = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600995 .aspect = VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700996 .mipLevel = 0,
997 .arraySlice = 0,
998 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600999 VkSubresourceLayout layout;
1000 size_t layout_size = sizeof(VkSubresourceLayout);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001001 void *data;
1002
Mike Stroyanebae8322015-04-17 12:36:38 -06001003 err = vkGetImageSubresourceInfo(demo->device, tex_obj->image, &subres,
Tony Barbour72304ef2015-04-16 15:59:00 -06001004 VK_SUBRESOURCE_INFO_TYPE_LAYOUT,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001005 &layout_size, &layout);
1006 assert(!err && layout_size == sizeof(layout));
1007 /* Linear texture must be within a single memory object */
1008 assert(num_allocations == 1);
1009
Mike Stroyanebae8322015-04-17 12:36:38 -06001010 err = vkMapMemory(demo->device, tex_obj->mem[0], 0, 0, 0, &data);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001011 assert(!err);
1012
1013 if (!loadTexture(filename, data, &layout, &tex_width, &tex_height)) {
1014 fprintf(stderr, "Error loading texture: %s\n", filename);
1015 }
1016
Mike Stroyanebae8322015-04-17 12:36:38 -06001017 err = vkUnmapMemory(demo->device, tex_obj->mem[0]);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001018 assert(!err);
1019 }
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001020
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001021 tex_obj->imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001022 demo_set_image_layout(demo, tex_obj->image,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001023 VK_IMAGE_LAYOUT_UNDEFINED,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001024 tex_obj->imageLayout);
1025 /* setting the image layout does not reference the actual memory so no need to add a mem ref */
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001026}
1027
Mark Lobodzinskida9b1092015-04-16 11:44:05 -05001028static void demo_destroy_texture_image(struct demo *demo, struct texture_object *tex_objs)
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001029{
1030 /* clean up staging resources */
1031 for (uint32_t j = 0; j < tex_objs->num_mem; j ++) {
Mike Stroyanebae8322015-04-17 12:36:38 -06001032 vkQueueBindObjectMemory(demo->queue,
1033 VK_OBJECT_TYPE_IMAGE, tex_objs->image, j, VK_NULL_HANDLE, 0);
1034 vkFreeMemory(demo->device, tex_objs->mem[j]);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001035 }
1036
1037 free(tex_objs->mem);
Mike Stroyanebae8322015-04-17 12:36:38 -06001038 vkDestroyObject(demo->device, VK_OBJECT_TYPE_IMAGE, tex_objs->image);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001039}
1040
1041static void demo_prepare_textures(struct demo *demo)
1042{
Tony Barbour72304ef2015-04-16 15:59:00 -06001043 const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001044 VkFormatProperties props;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001045 size_t size = sizeof(props);
Tony Barbourfdc2d352015-04-22 09:02:32 -06001046 VkResult U_ASSERT_ONLY err;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001047 uint32_t i;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001048
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001049 err = vkGetFormatInfo(demo->device, tex_format,
Tony Barbour72304ef2015-04-16 15:59:00 -06001050 VK_FORMAT_INFO_TYPE_PROPERTIES,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001051 &size, &props);
1052 assert(!err);
1053
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001054 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001055
Tony Barbour72304ef2015-04-16 15:59:00 -06001056 if (props.linearTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT && !demo->use_staging_buffer) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001057 /* Device can texture using linear textures */
1058 demo_prepare_texture_image(demo, tex_files[i], &demo->textures[i],
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001059 VK_IMAGE_TILING_LINEAR, VK_IMAGE_USAGE_SAMPLED_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
Tony Barbour72304ef2015-04-16 15:59:00 -06001060 } else if (props.optimalTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001061 /* Must use staging buffer to copy linear texture to optimized */
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001062 struct texture_object staging_texture;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001063
1064 memset(&staging_texture, 0, sizeof(staging_texture));
1065 demo_prepare_texture_image(demo, tex_files[i], &staging_texture,
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001066 VK_IMAGE_TILING_LINEAR, VK_IMAGE_USAGE_TRANSFER_SOURCE_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001067
1068 demo_prepare_texture_image(demo, tex_files[i], &demo->textures[i],
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001069 VK_IMAGE_TILING_OPTIMAL,
1070 (VK_IMAGE_USAGE_TRANSFER_DESTINATION_BIT | VK_IMAGE_USAGE_SAMPLED_BIT),
1071 VK_MEMORY_PROPERTY_DEVICE_ONLY);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001072
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001073 demo_set_image_layout(demo, staging_texture.image,
1074 staging_texture.imageLayout,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001075 VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001076
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001077 demo_set_image_layout(demo, demo->textures[i].image,
1078 demo->textures[i].imageLayout,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001079 VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001080
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001081 VkImageCopy copy_region = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001082 .srcSubresource = { VK_IMAGE_ASPECT_COLOR, 0, 0 },
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001083 .srcOffset = { 0, 0, 0 },
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001084 .destSubresource = { VK_IMAGE_ASPECT_COLOR, 0, 0 },
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001085 .destOffset = { 0, 0, 0 },
1086 .extent = { staging_texture.tex_width, staging_texture.tex_height, 1 },
1087 };
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001088 vkCmdCopyImage(demo->cmd,
1089 staging_texture.image, VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL,
1090 demo->textures[i].image, VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL,
Courtney Goeltzenleuchter8e89a312015-03-25 11:25:10 -06001091 1, &copy_region);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001092
Courtney Goeltzenleuchter2c2fbbf2015-04-07 17:13:38 -06001093 demo_add_mem_refs(demo, staging_texture.num_mem, staging_texture.mem);
1094 demo_add_mem_refs(demo, demo->textures[i].num_mem, demo->textures[i].mem);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001095
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001096 demo_set_image_layout(demo, demo->textures[i].image,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001097 VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001098 demo->textures[i].imageLayout);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001099
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001100 demo_flush_init_cmd(demo);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001101
Courtney Goeltzenleuchter1359b7a2015-04-02 16:25:42 -06001102 demo_remove_mem_refs(demo, staging_texture.num_mem, staging_texture.mem);
Courtney Goeltzenleuchterf3aeb2b2015-04-21 09:30:03 -06001103 demo_destroy_texture_image(demo, &staging_texture);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001104 } else {
Tony Barbour72304ef2015-04-16 15:59:00 -06001105 /* Can't support VK_FORMAT_B8G8R8A8_UNORM !? */
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001106 assert(!"No support for tB8G8R8A8_UNORM as texture image format");
1107 }
1108
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001109 const VkSamplerCreateInfo sampler = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001110 .sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001111 .pNext = NULL,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001112 .magFilter = VK_TEX_FILTER_NEAREST,
1113 .minFilter = VK_TEX_FILTER_NEAREST,
Tony Barbour72304ef2015-04-16 15:59:00 -06001114 .mipMode = VK_TEX_MIPMAP_MODE_BASE,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001115 .addressU = VK_TEX_ADDRESS_CLAMP,
1116 .addressV = VK_TEX_ADDRESS_CLAMP,
1117 .addressW = VK_TEX_ADDRESS_CLAMP,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001118 .mipLodBias = 0.0f,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001119 .maxAnisotropy = 1,
Tony Barbour72304ef2015-04-16 15:59:00 -06001120 .compareOp = VK_COMPARE_OP_NEVER,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001121 .minLod = 0.0f,
1122 .maxLod = 0.0f,
Tony Barbour72304ef2015-04-16 15:59:00 -06001123 .borderColor = VK_BORDER_COLOR_OPAQUE_WHITE,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001124 };
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001125
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001126 VkImageViewCreateInfo view = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001127 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001128 .pNext = NULL,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001129 .image = VK_NULL_HANDLE,
Tony Barbour72304ef2015-04-16 15:59:00 -06001130 .viewType = VK_IMAGE_VIEW_TYPE_2D,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001131 .format = tex_format,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001132 .channels = { VK_CHANNEL_SWIZZLE_R,
1133 VK_CHANNEL_SWIZZLE_G,
1134 VK_CHANNEL_SWIZZLE_B,
1135 VK_CHANNEL_SWIZZLE_A, },
1136 .subresourceRange = { VK_IMAGE_ASPECT_COLOR, 0, 1, 0, 1 },
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001137 .minLod = 0.0f,
1138 };
Jon Ashburnb2a66652015-01-16 09:37:43 -07001139
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001140 /* create sampler */
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001141 err = vkCreateSampler(demo->device, &sampler,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001142 &demo->textures[i].sampler);
1143 assert(!err);
1144
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001145 /* create image view */
1146 view.image = demo->textures[i].image;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001147 err = vkCreateImageView(demo->device, &view,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001148 &demo->textures[i].view);
1149 assert(!err);
1150 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001151}
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001152
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001153void demo_prepare_cube_data_buffer(struct demo *demo)
1154{
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -06001155 VkBufferCreateInfo buf_info;
1156 VkBufferViewCreateInfo view_info;
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -06001157 VkMemoryAllocInfo alloc_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001158 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
Mark Lobodzinskia6907f72015-04-16 08:52:00 -05001159 .pNext = NULL,
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001160 .allocationSize = 0,
Tony Barbour72304ef2015-04-16 15:59:00 -06001161 .memProps = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001162 .memPriority = VK_MEMORY_PRIORITY_NORMAL,
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001163 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001164 VkMemoryRequirements *mem_reqs;
1165 size_t mem_reqs_size = sizeof(VkMemoryRequirements);
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001166 uint32_t num_allocations = 0;
1167 size_t num_alloc_size = sizeof(num_allocations);
1168 uint8_t *pData;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001169 int i;
1170 mat4x4 MVP, VP;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001171 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001172 struct vktexcube_vs_uniform data;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001173
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001174 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001175 mat4x4_mul(MVP, VP, demo->model_matrix);
1176 memcpy(data.mvp, MVP, sizeof(MVP));
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001177// dumpMatrix("MVP", MVP);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001178
1179 for (i=0; i<12*3; i++) {
1180 data.position[i][0] = g_vertex_buffer_data[i*3];
1181 data.position[i][1] = g_vertex_buffer_data[i*3+1];
1182 data.position[i][2] = g_vertex_buffer_data[i*3+2];
1183 data.position[i][3] = 1.0f;
1184 data.attr[i][0] = g_uv_buffer_data[2*i];
1185 data.attr[i][1] = g_uv_buffer_data[2*i + 1];
1186 data.attr[i][2] = 0;
1187 data.attr[i][3] = 0;
1188 }
1189
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001190 memset(&buf_info, 0, sizeof(buf_info));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001191 buf_info.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001192 buf_info.size = sizeof(data);
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -06001193 buf_info.usage = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001194 err = vkCreateBuffer(demo->device, &buf_info, &demo->uniform_data.buf);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001195 assert(!err);
1196
Mike Stroyanebae8322015-04-17 12:36:38 -06001197 err = vkGetObjectInfo(demo->device,
1198 VK_OBJECT_TYPE_BUFFER, demo->uniform_data.buf,
Tony Barbour72304ef2015-04-16 15:59:00 -06001199 VK_OBJECT_INFO_TYPE_MEMORY_ALLOCATION_COUNT,
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001200 &num_alloc_size, &num_allocations);
1201 assert(!err && num_alloc_size == sizeof(num_allocations));
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001202 mem_reqs = malloc(num_allocations * sizeof(VkMemoryRequirements));
Tony Barbour72304ef2015-04-16 15:59:00 -06001203 demo->uniform_data.mem = malloc(num_allocations * sizeof(VkDeviceMemory));
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001204 demo->uniform_data.num_mem = num_allocations;
Mike Stroyanebae8322015-04-17 12:36:38 -06001205 err = vkGetObjectInfo(demo->device,
1206 VK_OBJECT_TYPE_BUFFER, demo->uniform_data.buf,
Tony Barbour72304ef2015-04-16 15:59:00 -06001207 VK_OBJECT_INFO_TYPE_MEMORY_REQUIREMENTS,
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001208 &mem_reqs_size, mem_reqs);
1209 assert(!err && mem_reqs_size == num_allocations * sizeof(*mem_reqs));
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001210 for (uint32_t i = 0; i < num_allocations; i ++) {
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001211 alloc_info.allocationSize = mem_reqs[i].size;
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001212
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001213 err = vkAllocMemory(demo->device, &alloc_info, &(demo->uniform_data.mem[i]));
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001214 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001215
Mike Stroyanebae8322015-04-17 12:36:38 -06001216 err = vkMapMemory(demo->device, demo->uniform_data.mem[i], 0, 0, 0, (void **) &pData);
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001217 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001218
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001219 memcpy(pData, &data, sizeof data);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001220
Mike Stroyanebae8322015-04-17 12:36:38 -06001221 err = vkUnmapMemory(demo->device, demo->uniform_data.mem[i]);
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001222 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001223
Mike Stroyanebae8322015-04-17 12:36:38 -06001224 err = vkQueueBindObjectMemory(demo->queue,
1225 VK_OBJECT_TYPE_BUFFER, demo->uniform_data.buf,
1226 i, demo->uniform_data.mem[i], 0);
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001227 assert(!err);
1228 }
Courtney Goeltzenleuchter2c2fbbf2015-04-07 17:13:38 -06001229 demo_add_mem_refs(demo, demo->uniform_data.num_mem, demo->uniform_data.mem);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001230
1231 memset(&view_info, 0, sizeof(view_info));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001232 view_info.sType = VK_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO;
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001233 view_info.buffer = demo->uniform_data.buf;
Tony Barbour72304ef2015-04-16 15:59:00 -06001234 view_info.viewType = VK_BUFFER_VIEW_TYPE_RAW;
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001235 view_info.offset = 0;
1236 view_info.range = sizeof(data);
1237
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001238 err = vkCreateBufferView(demo->device, &view_info, &demo->uniform_data.view);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001239 assert(!err);
1240
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001241 demo->uniform_data.attach.sType = VK_STRUCTURE_TYPE_BUFFER_VIEW_ATTACH_INFO;
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001242 demo->uniform_data.attach.view = demo->uniform_data.view;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001243}
1244
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001245static void demo_prepare_descriptor_layout(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001246{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001247 const VkDescriptorSetLayoutBinding layout_bindings[2] = {
Chia-I Wua2aa8632015-03-26 15:04:41 +08001248 [0] = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001249 .descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
Chia-I Wua2aa8632015-03-26 15:04:41 +08001250 .count = 1,
Tony Barbour72304ef2015-04-16 15:59:00 -06001251 .stageFlags = VK_SHADER_STAGE_VERTEX_BIT,
Chia-I Wu91e8e212015-03-27 12:56:09 +08001252 .pImmutableSamplers = NULL,
Chia-I Wua2aa8632015-03-26 15:04:41 +08001253 },
1254 [1] = {
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -06001255 .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
Chia-I Wua2aa8632015-03-26 15:04:41 +08001256 .count = DEMO_TEXTURE_COUNT,
Tony Barbour72304ef2015-04-16 15:59:00 -06001257 .stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT,
Chia-I Wu91e8e212015-03-27 12:56:09 +08001258 .pImmutableSamplers = NULL,
Chia-I Wua2aa8632015-03-26 15:04:41 +08001259 },
1260 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001261 const VkDescriptorSetLayoutCreateInfo descriptor_layout = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001262 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001263 .pNext = NULL,
Chia-I Wua2aa8632015-03-26 15:04:41 +08001264 .count = 2,
1265 .pBinding = layout_bindings,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001266 };
Tony Barbourfdc2d352015-04-22 09:02:32 -06001267 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001268
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001269 err = vkCreateDescriptorSetLayout(demo->device,
Chia-I Wub58c24a2015-03-26 15:27:55 +08001270 &descriptor_layout, &demo->desc_layout);
1271 assert(!err);
1272
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001273 const VkPipelineLayoutCreateInfo pPipelineLayoutCreateInfo = {
1274 .sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
1275 .pNext = NULL,
1276 .descriptorSetCount = 1,
1277 .pSetLayouts = &demo->desc_layout,
1278 };
1279
1280 err = vkCreatePipelineLayout(demo->device,
1281 &pPipelineLayoutCreateInfo,
1282 &demo->pipeline_layout);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001283 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001284}
1285
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001286static VkShader demo_prepare_shader(struct demo *demo,
Tony Barbour72304ef2015-04-16 15:59:00 -06001287 VkShaderStage stage,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001288 const void *code,
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001289 size_t size)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001290{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001291 VkShaderCreateInfo createInfo;
1292 VkShader shader;
1293 VkResult err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001294
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001295
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001296 createInfo.sType = VK_STRUCTURE_TYPE_SHADER_CREATE_INFO;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001297 createInfo.pNext = NULL;
1298
Cody Northrop8b12a1f2015-03-17 15:55:58 -06001299#ifdef EXTERNAL_SPV
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001300 createInfo.codeSize = size;
1301 createInfo.pCode = code;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001302 createInfo.flags = 0;
1303
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001304 err = vkCreateShader(demo->device, &createInfo, &shader);
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001305 if (err) {
1306 free((void *) createInfo.pCode);
1307 }
1308#else
Cody Northrop8b12a1f2015-03-17 15:55:58 -06001309 // Create fake SPV structure to feed GLSL
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001310 // to the driver "under the covers"
1311 createInfo.codeSize = 3 * sizeof(uint32_t) + size + 1;
1312 createInfo.pCode = malloc(createInfo.codeSize);
1313 createInfo.flags = 0;
1314
Tony Barbour72304ef2015-04-16 15:59:00 -06001315 /* try version 0 first: VkShaderStage followed by GLSL */
Cody Northrop8b12a1f2015-03-17 15:55:58 -06001316 ((uint32_t *) createInfo.pCode)[0] = ICD_SPV_MAGIC;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001317 ((uint32_t *) createInfo.pCode)[1] = 0;
1318 ((uint32_t *) createInfo.pCode)[2] = stage;
1319 memcpy(((uint32_t *) createInfo.pCode + 3), code, size + 1);
1320
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001321 err = vkCreateShader(demo->device, &createInfo, &shader);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001322 if (err) {
1323 free((void *) createInfo.pCode);
Tony Barbourf9921732015-04-22 11:36:22 -06001324 return (VkShader) VK_NULL_HANDLE;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001325 }
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001326#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001327
1328 return shader;
1329}
1330
Cody Northrop8b12a1f2015-03-17 15:55:58 -06001331char *demo_read_spv(const char *filename, size_t *psize)
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001332{
1333 long int size;
Tony Barbourf9921732015-04-22 11:36:22 -06001334 size_t U_ASSERT_ONLY retval;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001335 void *shader_code;
1336
1337 FILE *fp = fopen(filename, "rb");
1338 if (!fp) return NULL;
1339
1340 fseek(fp, 0L, SEEK_END);
1341 size = ftell(fp);
1342
1343 fseek(fp, 0L, SEEK_SET);
1344
1345 shader_code = malloc(size);
Tony Barbourfdc2d352015-04-22 09:02:32 -06001346 retval = fread(shader_code, size, 1, fp);
1347 assert(retval == 1);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001348
1349 *psize = size;
1350
1351 return shader_code;
1352}
1353
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001354static VkShader demo_prepare_vs(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001355{
Cody Northrop8b12a1f2015-03-17 15:55:58 -06001356#ifdef EXTERNAL_SPV
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001357 void *vertShaderCode;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001358 size_t size;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001359
Cody Northrop8b12a1f2015-03-17 15:55:58 -06001360 vertShaderCode = demo_read_spv("cube-vert.spv", &size);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001361
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001362 return demo_prepare_shader(demo, VK_SHADER_STAGE_VERTEX,
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001363 vertShaderCode, size);
1364#else
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001365 static const char *vertShaderText =
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001366 "#version 140\n"
1367 "#extension GL_ARB_separate_shader_objects : enable\n"
1368 "#extension GL_ARB_shading_language_420pack : enable\n"
1369 "\n"
1370 "layout(binding = 0) uniform buf {\n"
1371 " mat4 MVP;\n"
1372 " vec4 position[12*3];\n"
1373 " vec4 attr[12*3];\n"
1374 "} ubuf;\n"
1375 "\n"
1376 "layout (location = 0) out vec4 texcoord;\n"
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001377 "\n"
1378 "void main() \n"
1379 "{\n"
1380 " texcoord = ubuf.attr[gl_VertexID];\n"
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001381 " gl_Position = ubuf.MVP * ubuf.position[gl_VertexID];\n"
1382 "}\n";
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001383
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001384 return demo_prepare_shader(demo, VK_SHADER_STAGE_VERTEX,
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001385 (const void *) vertShaderText,
1386 strlen(vertShaderText));
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001387#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001388}
1389
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001390static VkShader demo_prepare_fs(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001391{
Cody Northrop8b12a1f2015-03-17 15:55:58 -06001392#ifdef EXTERNAL_SPV
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001393 void *fragShaderCode;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001394 size_t size;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001395
Cody Northrop8b12a1f2015-03-17 15:55:58 -06001396 fragShaderCode = demo_read_spv("cube-frag.spv", &size);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001397
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001398 return demo_prepare_shader(demo, VK_SHADER_STAGE_FRAGMENT,
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001399 fragShaderCode, size);
1400#else
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001401 static const char *fragShaderText =
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001402 "#version 140\n"
1403 "#extension GL_ARB_separate_shader_objects : enable\n"
1404 "#extension GL_ARB_shading_language_420pack : enable\n"
Courtney Goeltzenleuchter4d1acf12015-02-25 15:13:35 -07001405 "layout (binding = 1) uniform sampler2D tex;\n"
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001406 "\n"
1407 "layout (location = 0) in vec4 texcoord;\n"
1408 "void main() {\n"
1409 " gl_FragColor = texture(tex, texcoord.xy);\n"
1410 "}\n";
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001411
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001412 return demo_prepare_shader(demo, VK_SHADER_STAGE_FRAGMENT,
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001413 (const void *) fragShaderText,
1414 strlen(fragShaderText));
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001415#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001416}
1417
1418static void demo_prepare_pipeline(struct demo *demo)
1419{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001420 VkGraphicsPipelineCreateInfo pipeline;
1421 VkPipelineIaStateCreateInfo ia;
1422 VkPipelineRsStateCreateInfo rs;
1423 VkPipelineCbStateCreateInfo cb;
1424 VkPipelineDsStateCreateInfo ds;
1425 VkPipelineShaderStageCreateInfo vs;
1426 VkPipelineShaderStageCreateInfo fs;
1427 VkPipelineVpStateCreateInfo vp;
1428 VkPipelineMsStateCreateInfo ms;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001429 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001430
1431 memset(&pipeline, 0, sizeof(pipeline));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001432 pipeline.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001433 pipeline.layout = demo->pipeline_layout;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001434
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001435 memset(&ia, 0, sizeof(ia));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001436 ia.sType = VK_STRUCTURE_TYPE_PIPELINE_IA_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06001437 ia.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001438
1439 memset(&rs, 0, sizeof(rs));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001440 rs.sType = VK_STRUCTURE_TYPE_PIPELINE_RS_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06001441 rs.fillMode = VK_FILL_MODE_SOLID;
1442 rs.cullMode = VK_CULL_MODE_BACK;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001443 rs.frontFace = VK_FRONT_FACE_CCW;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001444
1445 memset(&cb, 0, sizeof(cb));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001446 cb.sType = VK_STRUCTURE_TYPE_PIPELINE_CB_STATE_CREATE_INFO;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001447 VkPipelineCbAttachmentState att_state[1];
Tony Barbour29645d02015-01-16 14:27:35 -07001448 memset(att_state, 0, sizeof(att_state));
1449 att_state[0].format = demo->format;
1450 att_state[0].channelWriteMask = 0xf;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001451 att_state[0].blendEnable = VK_FALSE;
Tony Barbour29645d02015-01-16 14:27:35 -07001452 cb.attachmentCount = 1;
1453 cb.pAttachments = att_state;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001454
Tony Barbour29645d02015-01-16 14:27:35 -07001455 memset(&vp, 0, sizeof(vp));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001456 vp.sType = VK_STRUCTURE_TYPE_PIPELINE_VP_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06001457 vp.viewportCount = 1;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001458 vp.clipOrigin = VK_COORDINATE_ORIGIN_LOWER_LEFT;
Tony Barbour29645d02015-01-16 14:27:35 -07001459
1460 memset(&ds, 0, sizeof(ds));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001461 ds.sType = VK_STRUCTURE_TYPE_PIPELINE_DS_STATE_CREATE_INFO;
Tony Barbour29645d02015-01-16 14:27:35 -07001462 ds.format = demo->depth.format;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001463 ds.depthTestEnable = VK_TRUE;
1464 ds.depthWriteEnable = VK_TRUE;
Tony Barbour72304ef2015-04-16 15:59:00 -06001465 ds.depthCompareOp = VK_COMPARE_OP_LESS_EQUAL;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001466 ds.depthBoundsEnable = VK_FALSE;
1467 ds.back.stencilFailOp = VK_STENCIL_OP_KEEP;
1468 ds.back.stencilPassOp = VK_STENCIL_OP_KEEP;
Tony Barbour72304ef2015-04-16 15:59:00 -06001469 ds.back.stencilCompareOp = VK_COMPARE_OP_ALWAYS;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001470 ds.stencilTestEnable = VK_FALSE;
Tony Barbour29645d02015-01-16 14:27:35 -07001471 ds.front = ds.back;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001472
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001473 memset(&vs, 0, sizeof(vs));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001474 vs.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1475 vs.shader.stage = VK_SHADER_STAGE_VERTEX;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001476 vs.shader.shader = demo_prepare_vs(demo);
Mike Stroyanebae8322015-04-17 12:36:38 -06001477 assert(vs.shader.shader != VK_NULL_HANDLE);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001478
1479 memset(&fs, 0, sizeof(fs));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001480 fs.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1481 fs.shader.stage = VK_SHADER_STAGE_FRAGMENT;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001482 fs.shader.shader = demo_prepare_fs(demo);
Mike Stroyanebae8322015-04-17 12:36:38 -06001483 assert(fs.shader.shader != VK_NULL_HANDLE);
Tony Barbour29645d02015-01-16 14:27:35 -07001484
1485 memset(&ms, 0, sizeof(ms));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001486 ms.sType = VK_STRUCTURE_TYPE_PIPELINE_MS_STATE_CREATE_INFO;
Tony Barbour29645d02015-01-16 14:27:35 -07001487 ms.sampleMask = 1;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001488 ms.multisampleEnable = VK_FALSE;
Tony Barbour29645d02015-01-16 14:27:35 -07001489 ms.samples = 1;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001490
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001491 pipeline.pNext = (const void *) &ia;
1492 ia.pNext = (const void *) &rs;
1493 rs.pNext = (const void *) &cb;
1494 cb.pNext = (const void *) &ms;
1495 ms.pNext = (const void *) &vp;
1496 vp.pNext = (const void *) &ds;
1497 ds.pNext = (const void *) &vs;
1498 vs.pNext = (const void *) &fs;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001499
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001500 err = vkCreateGraphicsPipeline(demo->device, &pipeline, &demo->pipeline);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001501 assert(!err);
1502
Mike Stroyanebae8322015-04-17 12:36:38 -06001503 vkDestroyObject(demo->device, VK_OBJECT_TYPE_SHADER, vs.shader.shader);
1504 vkDestroyObject(demo->device, VK_OBJECT_TYPE_SHADER, fs.shader.shader);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001505}
1506
1507static void demo_prepare_dynamic_states(struct demo *demo)
1508{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001509 VkDynamicVpStateCreateInfo viewport_create;
1510 VkDynamicRsStateCreateInfo raster;
1511 VkDynamicCbStateCreateInfo color_blend;
1512 VkDynamicDsStateCreateInfo depth_stencil;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001513 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001514
Tony Barbour29645d02015-01-16 14:27:35 -07001515 memset(&viewport_create, 0, sizeof(viewport_create));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001516 viewport_create.sType = VK_STRUCTURE_TYPE_DYNAMIC_VP_STATE_CREATE_INFO;
Courtney Goeltzenleuchter1710d8d2015-02-11 14:13:34 -07001517 viewport_create.viewportAndScissorCount = 1;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001518 VkViewport viewport;
Piers Daniell735ee532015-02-23 16:23:13 -07001519 memset(&viewport, 0, sizeof(viewport));
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001520 viewport.height = (float) demo->height;
1521 viewport.width = (float) demo->width;
1522 viewport.minDepth = (float) 0.0f;
1523 viewport.maxDepth = (float) 1.0f;
Piers Daniell735ee532015-02-23 16:23:13 -07001524 viewport_create.pViewports = &viewport;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001525 VkRect scissor;
Piers Daniell735ee532015-02-23 16:23:13 -07001526 memset(&scissor, 0, sizeof(scissor));
Courtney Goeltzenleuchter1710d8d2015-02-11 14:13:34 -07001527 scissor.extent.width = demo->width;
1528 scissor.extent.height = demo->height;
1529 scissor.offset.x = 0;
1530 scissor.offset.y = 0;
Courtney Goeltzenleuchter1710d8d2015-02-11 14:13:34 -07001531 viewport_create.pScissors = &scissor;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001532
1533 memset(&raster, 0, sizeof(raster));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001534 raster.sType = VK_STRUCTURE_TYPE_DYNAMIC_RS_STATE_CREATE_INFO;
Piers Daniell735ee532015-02-23 16:23:13 -07001535 raster.pointSize = 1.0;
1536 raster.lineWidth = 1.0;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001537
1538 memset(&color_blend, 0, sizeof(color_blend));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001539 color_blend.sType = VK_STRUCTURE_TYPE_DYNAMIC_CB_STATE_CREATE_INFO;
Piers Daniell735ee532015-02-23 16:23:13 -07001540 color_blend.blendConst[0] = 1.0f;
1541 color_blend.blendConst[1] = 1.0f;
1542 color_blend.blendConst[2] = 1.0f;
1543 color_blend.blendConst[3] = 1.0f;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001544
1545 memset(&depth_stencil, 0, sizeof(depth_stencil));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001546 depth_stencil.sType = VK_STRUCTURE_TYPE_DYNAMIC_DS_STATE_CREATE_INFO;
Piers Daniell735ee532015-02-23 16:23:13 -07001547 depth_stencil.minDepth = 0.0f;
1548 depth_stencil.maxDepth = 1.0f;
Tony Barbour29645d02015-01-16 14:27:35 -07001549 depth_stencil.stencilBackRef = 0;
1550 depth_stencil.stencilFrontRef = 0;
1551 depth_stencil.stencilReadMask = 0xff;
1552 depth_stencil.stencilWriteMask = 0xff;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001553
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001554 err = vkCreateDynamicViewportState(demo->device, &viewport_create, &demo->viewport);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001555 assert(!err);
1556
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001557 err = vkCreateDynamicRasterState(demo->device, &raster, &demo->raster);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001558 assert(!err);
1559
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001560 err = vkCreateDynamicColorBlendState(demo->device,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001561 &color_blend, &demo->color_blend);
1562 assert(!err);
1563
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001564 err = vkCreateDynamicDepthStencilState(demo->device,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001565 &depth_stencil, &demo->depth_stencil);
1566 assert(!err);
1567}
1568
Chia-I Wu63ea9262015-03-26 13:14:16 +08001569static void demo_prepare_descriptor_pool(struct demo *demo)
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001570{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001571 const VkDescriptorTypeCount type_counts[2] = {
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001572 [0] = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001573 .type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001574 .count = 1,
1575 },
1576 [1] = {
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -06001577 .type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001578 .count = DEMO_TEXTURE_COUNT,
1579 },
1580 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001581 const VkDescriptorPoolCreateInfo descriptor_pool = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001582 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001583 .pNext = NULL,
1584 .count = 2,
1585 .pTypeCount = type_counts,
1586 };
Tony Barbourfdc2d352015-04-22 09:02:32 -06001587 VkResult U_ASSERT_ONLY err;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001588
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001589 err = vkCreateDescriptorPool(demo->device,
1590 VK_DESCRIPTOR_POOL_USAGE_ONE_SHOT, 1,
Chia-I Wu63ea9262015-03-26 13:14:16 +08001591 &descriptor_pool, &demo->desc_pool);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001592 assert(!err);
1593}
1594
1595static void demo_prepare_descriptor_set(struct demo *demo)
1596{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001597 VkImageViewAttachInfo view_info[DEMO_TEXTURE_COUNT];
1598 VkSamplerImageViewInfo combined_info[DEMO_TEXTURE_COUNT];
1599 VkUpdateSamplerTextures update_fs;
1600 VkUpdateBuffers update_vs;
Chia-I Wub58c24a2015-03-26 15:27:55 +08001601 const void *update_array[2] = { &update_vs, &update_fs };
Tony Barbourfdc2d352015-04-22 09:02:32 -06001602 VkResult U_ASSERT_ONLY err;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001603 uint32_t count;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001604 uint32_t i;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001605
1606 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001607 view_info[i].sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_ATTACH_INFO;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001608 view_info[i].pNext = NULL;
1609 view_info[i].view = demo->textures[i].view,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001610 view_info[i].layout = VK_IMAGE_LAYOUT_GENERAL;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001611
Courtney Goeltzenleuchtereb4754f2015-04-09 11:43:10 -06001612 combined_info[i].sampler = demo->textures[i].sampler;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001613 combined_info[i].pImageView = &view_info[i];
1614 }
1615
1616 memset(&update_vs, 0, sizeof(update_vs));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001617 update_vs.sType = VK_STRUCTURE_TYPE_UPDATE_BUFFERS;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001618 update_vs.pNext = &update_fs;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001619 update_vs.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001620 update_vs.count = 1;
Chia-I Wub58c24a2015-03-26 15:27:55 +08001621 update_vs.pBufferViews = &demo->uniform_data.attach;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001622
1623 memset(&update_fs, 0, sizeof(update_fs));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001624 update_fs.sType = VK_STRUCTURE_TYPE_UPDATE_SAMPLER_TEXTURES;
Chia-I Wub58c24a2015-03-26 15:27:55 +08001625 update_fs.binding = 1;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001626 update_fs.count = DEMO_TEXTURE_COUNT;
1627 update_fs.pSamplerImageViews = combined_info;
1628
Mike Stroyanebae8322015-04-17 12:36:38 -06001629 err = vkAllocDescriptorSets(demo->device, demo->desc_pool,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001630 VK_DESCRIPTOR_SET_USAGE_STATIC,
Chia-I Wu87544e72015-02-23 10:41:08 -07001631 1, &demo->desc_layout,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001632 &demo->desc_set, &count);
1633 assert(!err && count == 1);
1634
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001635 vkBeginDescriptorPoolUpdate(demo->device,
1636 VK_DESCRIPTOR_UPDATE_MODE_FASTEST);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001637
Mike Stroyanebae8322015-04-17 12:36:38 -06001638 vkClearDescriptorSets(demo->device, demo->desc_pool, 1, &demo->desc_set);
1639 vkUpdateDescriptors(demo->device, demo->desc_set, 2, update_array);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001640
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001641 vkEndDescriptorPoolUpdate(demo->device, demo->buffers[demo->current_buffer].cmd);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001642}
1643
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001644static void demo_prepare(struct demo *demo)
1645{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001646 const VkCmdBufferCreateInfo cmd = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001647 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001648 .pNext = NULL,
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07001649 .queueNodeIndex = demo->graphics_queue_node_index,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001650 .flags = 0,
1651 };
Tony Barbourfdc2d352015-04-22 09:02:32 -06001652 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001653
1654 demo_prepare_buffers(demo);
1655 demo_prepare_depth(demo);
1656 demo_prepare_textures(demo);
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001657 demo_prepare_cube_data_buffer(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001658
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001659 demo_prepare_descriptor_layout(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001660 demo_prepare_pipeline(demo);
1661 demo_prepare_dynamic_states(demo);
1662
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001663 for (int i = 0; i < DEMO_BUFFER_COUNT; i++) {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001664 err = vkCreateCommandBuffer(demo->device, &cmd, &demo->buffers[i].cmd);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001665 assert(!err);
1666 }
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001667
Chia-I Wu63ea9262015-03-26 13:14:16 +08001668 demo_prepare_descriptor_pool(demo);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001669 demo_prepare_descriptor_set(demo);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001670
1671
1672 for (int i = 0; i < DEMO_BUFFER_COUNT; i++) {
1673 demo->current_buffer = i;
1674 demo_draw_build_cmd(demo, demo->buffers[i].cmd);
1675 }
1676
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001677 /*
1678 * Prepare functions above may generate pipeline commands
1679 * that need to be flushed before beginning the render loop.
1680 */
1681 demo_flush_init_cmd(demo);
1682
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001683 demo->current_buffer = 0;
Jon Ashburn8d26c062015-04-24 09:46:24 -07001684 demo->prepared = true;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001685}
1686
Ian Elliott639ca472015-04-16 15:23:05 -06001687#ifdef _WIN32
1688static void demo_run(struct demo *demo)
1689{
Courtney Goeltzenleuchterb7e22702015-04-27 14:56:34 -06001690 if (!demo->prepared)
1691 return;
Ian Elliott639ca472015-04-16 15:23:05 -06001692 // Wait for work to finish before updating MVP.
1693 vkDeviceWaitIdle(demo->device);
1694 demo_update_data_buffer(demo);
1695
1696 demo_draw(demo);
1697
1698 // Wait for work to finish before updating MVP.
1699 vkDeviceWaitIdle(demo->device);
1700}
1701
1702// On MS-Windows, make this a global, so it's available to WndProc()
1703struct demo demo;
1704
1705// MS-Windows event handling function:
1706LRESULT CALLBACK WndProc(HWND hWnd,
1707 UINT uMsg,
1708 WPARAM wParam,
1709 LPARAM lParam)
1710{
Ian Elliott639ca472015-04-16 15:23:05 -06001711 switch(uMsg)
1712 {
Ian Elliott639ca472015-04-16 15:23:05 -06001713 case WM_CLOSE:
1714 PostQuitMessage(0);
Tony Barbour3dddd5d2015-04-29 16:19:20 -06001715 break;
Ian Elliott639ca472015-04-16 15:23:05 -06001716 case WM_PAINT:
1717 demo_run(&demo);
1718 return 0;
1719 default:
1720 break;
1721 }
1722 return (DefWindowProc(hWnd, uMsg, wParam, lParam));
1723}
1724
1725static void demo_create_window(struct demo *demo)
1726{
1727 WNDCLASSEX win_class;
1728
1729 // Initialize the window class structure:
1730 win_class.cbSize = sizeof(WNDCLASSEX);
1731 win_class.style = CS_HREDRAW | CS_VREDRAW;
1732 win_class.lpfnWndProc = WndProc;
1733 win_class.cbClsExtra = 0;
1734 win_class.cbWndExtra = 0;
1735 win_class.hInstance = demo->connection; // hInstance
1736 win_class.hIcon = LoadIcon(NULL, IDI_APPLICATION);
1737 win_class.hCursor = LoadCursor(NULL, IDC_ARROW);
1738 win_class.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
1739 win_class.lpszMenuName = NULL;
1740 win_class.lpszClassName = demo->name;
1741 win_class.hIconSm = LoadIcon(NULL, IDI_WINLOGO);
1742 // Register window class:
1743 if (!RegisterClassEx(&win_class)) {
1744 // It didn't work, so try to give a useful error:
1745 printf("Unexpected error trying to start the application!\n");
1746 fflush(stdout);
1747 exit(1);
1748 }
1749 // Create window with the registered class:
1750 demo->window = CreateWindowEx(0,
1751 demo->name, // class name
1752 demo->name, // app name
1753 WS_OVERLAPPEDWINDOW | // window style
1754 WS_VISIBLE |
1755 WS_SYSMENU,
1756 100,100, // x/y coords
1757 demo->width, // width
1758 demo->height, // height
1759 NULL, // handle to parent
1760 NULL, // handle to menu
1761 demo->connection, // hInstance
1762 NULL); // no extra parameters
1763 if (!demo->window) {
1764 // It didn't work, so try to give a useful error:
1765 printf("Cannot create a window in which to draw!\n");
1766 fflush(stdout);
1767 exit(1);
1768 }
1769}
1770#else // _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001771static void demo_handle_event(struct demo *demo,
1772 const xcb_generic_event_t *event)
1773{
Piers Daniell735ee532015-02-23 16:23:13 -07001774 uint8_t event_code = event->response_type & 0x7f;
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07001775 switch (event_code) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001776 case XCB_EXPOSE:
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07001777 // TODO: Resize window
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001778 break;
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07001779 case XCB_CLIENT_MESSAGE:
1780 if((*(xcb_client_message_event_t*)event).data.data32[0] ==
1781 (*demo->atom_wm_delete_window).atom) {
1782 demo->quit = true;
1783 }
1784 break;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001785 case XCB_KEY_RELEASE:
1786 {
1787 const xcb_key_release_event_t *key =
1788 (const xcb_key_release_event_t *) event;
1789
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001790 switch (key->detail) {
1791 case 0x9: // Escape
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001792 demo->quit = true;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001793 break;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001794 case 0x71: // left arrow key
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001795 demo->spin_angle += demo->spin_increment;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001796 break;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001797 case 0x72: // right arrow key
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001798 demo->spin_angle -= demo->spin_increment;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001799 break;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001800 case 0x41:
1801 demo->pause = !demo->pause;
Piers Daniell735ee532015-02-23 16:23:13 -07001802 break;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001803 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001804 }
1805 break;
1806 default:
1807 break;
1808 }
1809}
1810
1811static void demo_run(struct demo *demo)
1812{
1813 xcb_flush(demo->connection);
1814
1815 while (!demo->quit) {
1816 xcb_generic_event_t *event;
1817
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07001818 if (demo->pause) {
1819 event = xcb_wait_for_event(demo->connection);
1820 } else {
1821 event = xcb_poll_for_event(demo->connection);
1822 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001823 if (event) {
1824 demo_handle_event(demo, event);
1825 free(event);
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07001826 }
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07001827
1828 // Wait for work to finish before updating MVP.
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001829 vkDeviceWaitIdle(demo->device);
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07001830 demo_update_data_buffer(demo);
1831
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07001832 demo_draw(demo);
Courtney Goeltzenleuchter1454f3c2014-11-18 11:28:09 -07001833
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07001834 // Wait for work to finish before updating MVP.
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001835 vkDeviceWaitIdle(demo->device);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001836 }
1837}
1838
1839static void demo_create_window(struct demo *demo)
1840{
1841 uint32_t value_mask, value_list[32];
1842
1843 demo->window = xcb_generate_id(demo->connection);
1844
1845 value_mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK;
1846 value_list[0] = demo->screen->black_pixel;
1847 value_list[1] = XCB_EVENT_MASK_KEY_RELEASE |
1848 XCB_EVENT_MASK_EXPOSURE;
1849
1850 xcb_create_window(demo->connection,
1851 XCB_COPY_FROM_PARENT,
1852 demo->window, demo->screen->root,
1853 0, 0, demo->width, demo->height, 0,
1854 XCB_WINDOW_CLASS_INPUT_OUTPUT,
1855 demo->screen->root_visual,
1856 value_mask, value_list);
1857
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07001858 /* Magic code that will send notification when window is destroyed */
1859 xcb_intern_atom_cookie_t cookie = xcb_intern_atom(demo->connection, 1, 12,
1860 "WM_PROTOCOLS");
1861 xcb_intern_atom_reply_t* reply = xcb_intern_atom_reply(demo->connection, cookie, 0);
1862
1863 xcb_intern_atom_cookie_t cookie2 = xcb_intern_atom(demo->connection, 0, 16, "WM_DELETE_WINDOW");
1864 demo->atom_wm_delete_window = xcb_intern_atom_reply(demo->connection, cookie2, 0);
1865
1866 xcb_change_property(demo->connection, XCB_PROP_MODE_REPLACE,
1867 demo->window, (*reply).atom, 4, 32, 1,
1868 &(*demo->atom_wm_delete_window).atom);
1869 free(reply);
1870
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001871 xcb_map_window(demo->connection, demo->window);
1872}
Ian Elliott639ca472015-04-16 15:23:05 -06001873#endif // _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001874
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001875static void demo_init_vk(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001876{
Tobin Ehlis99239dc2015-04-16 18:04:57 -06001877 VkResult err;
1878 // Extensions to enable
1879 const char *ext_names[] = {
Chia-I Wucbb564e2015-04-16 22:02:10 +08001880 "VK_WSI_LunarG",
Tony Barbour3dddd5d2015-04-29 16:19:20 -06001881 "Validation"
Tobin Ehlis99239dc2015-04-16 18:04:57 -06001882 };
1883 size_t extSize = sizeof(uint32_t);
1884 uint32_t extCount = 0;
1885 err = vkGetGlobalExtensionInfo(VK_EXTENSION_INFO_TYPE_COUNT, 0, &extSize, &extCount);
1886 assert(!err);
1887
1888 VkExtensionProperties extProp;
1889 extSize = sizeof(VkExtensionProperties);
Tony Barbourfdc2d352015-04-22 09:02:32 -06001890 bool32_t U_ASSERT_ONLY extFound = 0;
Tobin Ehlis99239dc2015-04-16 18:04:57 -06001891 for (uint32_t i = 0; i < extCount; i++) {
1892 err = vkGetGlobalExtensionInfo(VK_EXTENSION_INFO_TYPE_PROPERTIES, i, &extSize, &extProp);
1893 if (!strcmp(ext_names[0], extProp.extName))
1894 extFound = 1;
1895 }
Ian Elliott65152912015-04-28 13:22:33 -06001896 if (!extFound) {
1897 ERR_EXIT("vkGetGlobalExtensionInfo failed to find the "
1898 "\"VK_WSI_LunarG\" extension.\n\nDo you have a compatible "
1899 "Vulkan installable client driver (ICD) installed?\nPlease "
1900 "look at the Getting Started guide for additional "
1901 "information.\n",
1902 "vkCreateInstance Failure");
1903 }
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001904 const VkApplicationInfo app = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001905 .sType = VK_STRUCTURE_TYPE_APPLICATION_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001906 .pNext = NULL,
Ian Elliott44e33f72015-04-28 10:52:52 -06001907 .pAppName = APP_SHORT_NAME,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001908 .appVersion = 0,
Ian Elliott44e33f72015-04-28 10:52:52 -06001909 .pEngineName = APP_SHORT_NAME,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001910 .engineVersion = 0,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001911 .apiVersion = VK_API_VERSION,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001912 };
Tony Barbour3dddd5d2015-04-29 16:19:20 -06001913 VkInstanceCreateInfo inst_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001914 .sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
Jon Ashburnab46b362015-04-04 14:52:07 -06001915 .pNext = NULL,
1916 .pAppInfo = &app,
1917 .pAllocCb = NULL,
Tobin Ehlis99239dc2015-04-16 18:04:57 -06001918 .extensionCount = 1,
1919 .ppEnabledExtensionNames = ext_names,
Jon Ashburnab46b362015-04-04 14:52:07 -06001920 };
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -06001921 const VkDeviceQueueCreateInfo queue = {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001922 .queueNodeIndex = 0,
1923 .queueCount = 1,
1924 };
Ian Elliott097d9f32015-04-28 11:35:02 -06001925
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -06001926 const VkDeviceCreateInfo device = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001927 .sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001928 .pNext = NULL,
1929 .queueRecordCount = 1,
1930 .pRequestedQueues = &queue,
Tobin Ehlis99239dc2015-04-16 18:04:57 -06001931 .extensionCount = 1,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001932 .ppEnabledExtensionNames = ext_names,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001933 .flags = VK_DEVICE_CREATE_VALIDATION_BIT,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001934 };
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001935 uint32_t gpu_count;
1936 uint32_t i;
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07001937 size_t data_size;
1938 uint32_t queue_count;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001939
Tony Barbour3dddd5d2015-04-29 16:19:20 -06001940 if (demo->validate) {
1941 inst_info.extensionCount = 2;
1942 }
1943
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001944 err = vkCreateInstance(&inst_info, &demo->inst);
Ian Elliott07264132015-04-28 11:35:02 -06001945 if (err == VK_ERROR_INCOMPATIBLE_DRIVER) {
1946 ERR_EXIT("Cannot find a compatible Vulkan installable client driver "
Ian Elliott65152912015-04-28 13:22:33 -06001947 "(ICD).\n\nPlease look at the Getting Started guide for "
Ian Elliott07264132015-04-28 11:35:02 -06001948 "additional information.\n",
1949 "vkCreateInstance Failure");
Tony Barbour3dddd5d2015-04-29 16:19:20 -06001950 } else if (err == VK_ERROR_INVALID_EXTENSION) {
1951 ERR_EXIT("Cannot find a specified extension library"
1952 ".\nMake sure your layers path is set appropriately\n",
1953 "vkCreateInstance Failure");
Ian Elliott07264132015-04-28 11:35:02 -06001954 } else if (err) {
Ian Elliott65152912015-04-28 13:22:33 -06001955 ERR_EXIT("vkCreateInstance failed.\n\nDo you have a compatible Vulkan "
1956 "installable client driver (ICD) installed?\nPlease look at "
Ian Elliott07264132015-04-28 11:35:02 -06001957 "the Getting Started guide for additional information.\n",
1958 "vkCreateInstance Failure");
Ian Elliott3979e282015-04-03 15:24:55 -06001959 }
Jon Ashburnab46b362015-04-04 14:52:07 -06001960
Tony Barbour3dddd5d2015-04-29 16:19:20 -06001961
Jon Ashburn07c0c0c2015-04-15 11:31:12 -06001962 gpu_count = 1;
1963 err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, &demo->gpu);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001964 assert(!err && gpu_count == 1);
1965
Tony Barbour3dddd5d2015-04-29 16:19:20 -06001966 if (demo->validate) {
1967 vkDbgRegisterMsgCallback(demo->inst, dbgFunc, NULL);
1968 }
1969
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001970 err = vkCreateDevice(demo->gpu, &device, &demo->device);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001971 assert(!err);
1972
Tony Barbour72304ef2015-04-16 15:59:00 -06001973 err = vkGetPhysicalDeviceInfo(demo->gpu, VK_PHYSICAL_DEVICE_INFO_TYPE_PROPERTIES,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001974 &data_size, NULL);
1975 assert(!err);
1976
Tony Barbour72304ef2015-04-16 15:59:00 -06001977 demo->gpu_props = (VkPhysicalDeviceProperties *) malloc(data_size);
1978 err = vkGetPhysicalDeviceInfo(demo->gpu, VK_PHYSICAL_DEVICE_INFO_TYPE_PROPERTIES,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001979 &data_size, demo->gpu_props);
1980 assert(!err);
1981
Tony Barbour72304ef2015-04-16 15:59:00 -06001982 err = vkGetPhysicalDeviceInfo(demo->gpu, VK_PHYSICAL_DEVICE_INFO_TYPE_QUEUE_PROPERTIES,
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07001983 &data_size, NULL);
1984 assert(!err);
1985
Tony Barbour72304ef2015-04-16 15:59:00 -06001986 demo->queue_props = (VkPhysicalDeviceQueueProperties *) malloc(data_size);
1987 err = vkGetPhysicalDeviceInfo(demo->gpu, VK_PHYSICAL_DEVICE_INFO_TYPE_QUEUE_PROPERTIES,
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07001988 &data_size, demo->queue_props);
1989 assert(!err);
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001990 queue_count = (uint32_t)(data_size / sizeof(VkPhysicalDeviceQueueProperties));
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07001991 assert(queue_count >= 1);
1992
Mark Lobodzinskida9b1092015-04-16 11:44:05 -05001993 // Graphics queue and MemMgr queue can be separate.
1994 // TODO: Add support for separate queues, including synchronization,
1995 // and appropriate tracking for QueueSubmit and QueueBindObjectMemory
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07001996 for (i = 0; i < queue_count; i++) {
Mark Lobodzinskida9b1092015-04-16 11:44:05 -05001997 if ((demo->queue_props[i].queueFlags & VK_QUEUE_GRAPHICS_BIT) &&
1998 (demo->queue_props[i].queueFlags & VK_QUEUE_MEMMGR_BIT) )
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07001999 break;
2000 }
2001 assert(i < queue_count);
2002 demo->graphics_queue_node_index = i;
2003
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002004 err = vkGetDeviceQueue(demo->device, demo->graphics_queue_node_index,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002005 0, &demo->queue);
2006 assert(!err);
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002007
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002008}
2009
2010static void demo_init_connection(struct demo *demo)
2011{
Ian Elliott639ca472015-04-16 15:23:05 -06002012#ifndef _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002013 const xcb_setup_t *setup;
2014 xcb_screen_iterator_t iter;
2015 int scr;
2016
2017 demo->connection = xcb_connect(NULL, &scr);
Ian Elliott3979e282015-04-03 15:24:55 -06002018 if (demo->connection == NULL) {
2019 printf("Cannot find a compatible Vulkan installable client driver "
2020 "(ICD).\nExiting ...\n");
2021 fflush(stdout);
2022 exit(1);
2023 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002024
2025 setup = xcb_get_setup(demo->connection);
2026 iter = xcb_setup_roots_iterator(setup);
2027 while (scr-- > 0)
2028 xcb_screen_next(&iter);
2029
2030 demo->screen = iter.data;
Ian Elliott639ca472015-04-16 15:23:05 -06002031#endif // _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002032}
2033
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002034static void demo_init(struct demo *demo, int argc, char **argv)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002035{
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002036 vec3 eye = {0.0f, 3.0f, 5.0f};
2037 vec3 origin = {0, 0, 0};
2038 vec3 up = {0.0f, -1.0f, 0.0};
2039
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002040 memset(demo, 0, sizeof(*demo));
2041
Piers Daniell735ee532015-02-23 16:23:13 -07002042 for (int i = 1; i < argc; i++) {
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002043 if (strcmp(argv[i], "--use_staging") == 0) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002044 demo->use_staging_buffer = true;
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002045 continue;
Ian Elliott639ca472015-04-16 15:23:05 -06002046 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002047 if (strcmp(argv[i], "--validate") == 0) {
2048 demo->validate = true;
2049 continue;
2050 }
2051
2052 fprintf(stderr, "Usage:\n %s [--use_staging] [--validate}\n", APP_SHORT_NAME);
Ian Elliott639ca472015-04-16 15:23:05 -06002053 fflush(stderr);
2054 exit(1);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002055 }
2056
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002057 demo_init_connection(demo);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002058 demo_init_vk(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002059
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002060 demo->width = 500;
2061 demo->height = 500;
Tony Barbour72304ef2015-04-16 15:59:00 -06002062 demo->format = VK_FORMAT_B8G8R8A8_UNORM;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002063
2064 demo->spin_angle = 0.01f;
2065 demo->spin_increment = 0.01f;
2066 demo->pause = false;
2067
Piers Daniell735ee532015-02-23 16:23:13 -07002068 mat4x4_perspective(demo->projection_matrix, (float)degreesToRadians(45.0f), 1.0f, 0.1f, 100.0f);
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002069 mat4x4_look_at(demo->view_matrix, eye, origin, up);
2070 mat4x4_identity(demo->model_matrix);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002071}
2072
2073static void demo_cleanup(struct demo *demo)
2074{
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06002075 uint32_t i, j;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002076
Courtney Goeltzenleuchterb7e22702015-04-27 14:56:34 -06002077 demo->prepared = false;
2078
Mike Stroyanebae8322015-04-17 12:36:38 -06002079 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DESCRIPTOR_SET, demo->desc_set);
2080 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DESCRIPTOR_POOL, demo->desc_pool);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08002081
Mike Stroyanebae8322015-04-17 12:36:38 -06002082 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DYNAMIC_VP_STATE, demo->viewport);
2083 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DYNAMIC_RS_STATE, demo->raster);
2084 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DYNAMIC_CB_STATE, demo->color_blend);
2085 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DYNAMIC_DS_STATE, demo->depth_stencil);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002086
Mike Stroyanebae8322015-04-17 12:36:38 -06002087 vkDestroyObject(demo->device, VK_OBJECT_TYPE_PIPELINE, demo->pipeline);
2088 vkDestroyObject(demo->device, VK_OBJECT_TYPE_PIPELINE_LAYOUT, demo->pipeline_layout);
2089 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DESCRIPTOR_SET_LAYOUT, demo->desc_layout);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002090
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002091 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Mike Stroyanebae8322015-04-17 12:36:38 -06002092 vkDestroyObject(demo->device, VK_OBJECT_TYPE_IMAGE_VIEW, demo->textures[i].view);
2093 vkQueueBindObjectMemory(demo->queue, VK_OBJECT_TYPE_IMAGE, demo->textures[i].image, 0, VK_NULL_HANDLE, 0);
2094 vkDestroyObject(demo->device, VK_OBJECT_TYPE_IMAGE, demo->textures[i].image);
Courtney Goeltzenleuchter1359b7a2015-04-02 16:25:42 -06002095 demo_remove_mem_refs(demo, demo->textures[i].num_mem, demo->textures[i].mem);
Jon Ashburnb2a66652015-01-16 09:37:43 -07002096 for (j = 0; j < demo->textures[i].num_mem; j++)
Mike Stroyanebae8322015-04-17 12:36:38 -06002097 vkFreeMemory(demo->device, demo->textures[i].mem[j]);
2098 free(demo->textures[i].mem);
2099 vkDestroyObject(demo->device, VK_OBJECT_TYPE_SAMPLER, demo->textures[i].sampler);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002100 }
Chia-I Wucbb564e2015-04-16 22:02:10 +08002101 vkDestroySwapChainWSI(demo->swap_chain);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002102
Mike Stroyanebae8322015-04-17 12:36:38 -06002103 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DEPTH_STENCIL_VIEW, demo->depth.view);
2104 vkQueueBindObjectMemory(demo->queue, VK_OBJECT_TYPE_IMAGE, demo->depth.image, 0, VK_NULL_HANDLE, 0);
Courtney Goeltzenleuchter1359b7a2015-04-02 16:25:42 -06002105 demo_remove_mem_refs(demo, demo->depth.num_mem, demo->depth.mem);
Mike Stroyanebae8322015-04-17 12:36:38 -06002106 vkDestroyObject(demo->device, VK_OBJECT_TYPE_IMAGE, demo->depth.image);
Courtney Goeltzenleuchter1359b7a2015-04-02 16:25:42 -06002107 for (j = 0; j < demo->depth.num_mem; j++) {
Mike Stroyanebae8322015-04-17 12:36:38 -06002108 vkFreeMemory(demo->device, demo->depth.mem[j]);
Courtney Goeltzenleuchter1359b7a2015-04-02 16:25:42 -06002109 }
Mark Lobodzinski7b8fee62015-02-18 16:38:17 -06002110
Mike Stroyanebae8322015-04-17 12:36:38 -06002111 vkDestroyObject(demo->device, VK_OBJECT_TYPE_BUFFER_VIEW, demo->uniform_data.view);
2112 vkQueueBindObjectMemory(demo->queue, VK_OBJECT_TYPE_BUFFER, demo->uniform_data.buf, 0, VK_NULL_HANDLE, 0);
2113 vkDestroyObject(demo->device, VK_OBJECT_TYPE_BUFFER, demo->uniform_data.buf);
Courtney Goeltzenleuchter1359b7a2015-04-02 16:25:42 -06002114 demo_remove_mem_refs(demo, demo->uniform_data.num_mem, demo->uniform_data.mem);
Jon Ashburnae7c21c2015-01-19 15:00:26 -07002115 for (j = 0; j < demo->uniform_data.num_mem; j++)
Mike Stroyanebae8322015-04-17 12:36:38 -06002116 vkFreeMemory(demo->device, demo->uniform_data.mem[j]);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002117
2118 for (i = 0; i < DEMO_BUFFER_COUNT; i++) {
Mike Stroyanebae8322015-04-17 12:36:38 -06002119 vkDestroyObject(demo->device, VK_OBJECT_TYPE_COLOR_ATTACHMENT_VIEW, demo->buffers[i].view);
2120 vkDestroyObject(demo->device, VK_OBJECT_TYPE_COMMAND_BUFFER, demo->buffers[i].cmd);
Courtney Goeltzenleuchter1359b7a2015-04-02 16:25:42 -06002121 demo_remove_mem_refs(demo, 1, &demo->buffers[i].mem);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002122 }
2123
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002124 vkDestroyDevice(demo->device);
2125 vkDestroyInstance(demo->inst);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002126
Ian Elliott639ca472015-04-16 15:23:05 -06002127#ifndef _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002128 xcb_destroy_window(demo->connection, demo->window);
2129 xcb_disconnect(demo->connection);
Ian Elliott639ca472015-04-16 15:23:05 -06002130#endif // _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002131}
2132
Ian Elliott639ca472015-04-16 15:23:05 -06002133#ifdef _WIN32
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002134extern int __getmainargs(
2135 int * _Argc,
2136 char *** _Argv,
2137 char *** _Env,
2138 int _DoWildCard,
2139 int * new_mode);
Ian Elliottf9cf78c2015-04-28 10:33:11 -06002140
Ian Elliotta748eaf2015-04-28 15:50:36 -06002141int WINAPI WinMain(HINSTANCE hInstance,
2142 HINSTANCE hPrevInstance,
2143 LPSTR pCmdLine,
2144 int nCmdShow)
Ian Elliott639ca472015-04-16 15:23:05 -06002145{
2146 MSG msg; // message
2147 bool done; // flag saying when app is complete
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002148 int argc;
2149 char** argv;
2150 char** env;
2151 int new_mode = 0;
Ian Elliott639ca472015-04-16 15:23:05 -06002152
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002153 __getmainargs(&argc,&argv,&env,0,&new_mode);
2154
2155 demo_init(&demo, argc, argv);
2156 demo.connection = hInstance;
2157 strncpy(demo.name, "cube", APP_NAME_STR_LEN);
Ian Elliott639ca472015-04-16 15:23:05 -06002158 demo_create_window(&demo);
2159
2160 demo_prepare(&demo);
2161
2162 done = false; //initialize loop condition variable
2163 /* main message loop*/
2164 while(!done)
2165 {
Ian Elliotta748eaf2015-04-28 15:50:36 -06002166 PeekMessage(&msg, NULL, 0, 0, PM_REMOVE);
Ian Elliott639ca472015-04-16 15:23:05 -06002167 if (msg.message == WM_QUIT) //check for a quit message
2168 {
2169 done = true; //if found, quit app
2170 }
2171 else
2172 {
2173 /* Translate and dispatch to event queue*/
2174 TranslateMessage(&msg);
2175 DispatchMessage(&msg);
2176 }
2177 }
2178
2179 demo_cleanup(&demo);
2180
Tony Barbourf9921732015-04-22 11:36:22 -06002181 return (int) msg.wParam;
Ian Elliott639ca472015-04-16 15:23:05 -06002182}
2183#else // _WIN32
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002184int main(int argc, char **argv)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002185{
2186 struct demo demo;
2187
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002188 demo_init(&demo, argc, argv);
Chia-I Wucbb564e2015-04-16 22:02:10 +08002189 demo_create_window(&demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002190
2191 demo_prepare(&demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002192 demo_run(&demo);
2193
2194 demo_cleanup(&demo);
2195
2196 return 0;
2197}
Ian Elliott639ca472015-04-16 15:23:05 -06002198#endif // _WIN32