blob: abd0f600a1e1aecd50334f4a72baed4753e4c985 [file] [log] [blame]
Ian Elliott421107f2015-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 Goeltzenleuchter17223602014-10-29 15:59:49 -060024#define _GNU_SOURCE
Courtney Goeltzenleuchter4e8246e2014-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 Elliotte14e9f92015-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 Goeltzenleuchter4e8246e2014-10-23 13:16:59 -060036#include <xcb/xcb.h>
Ian Elliotte14e9f92015-04-16 15:23:05 -060037#endif // _WIN32
38
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -060039#include <vulkan.h>
Chia-I Wu5b66aa52015-04-16 22:02:10 +080040#include <vk_wsi_lunarg.h>
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -060041#include "vk_debug_report_lunarg.h"
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -060042
Cody Northropd4e020a2015-03-17 14:54:35 -060043#include "icd-spv.h"
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -060044
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -060045#include "linmath.h"
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -060046#include <png.h>
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -060047
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -060048#define DEMO_BUFFER_COUNT 2
49#define DEMO_TEXTURE_COUNT 1
Ian Elliott4e19ed02015-04-28 10:52:52 -060050#define APP_SHORT_NAME "cube"
51#define APP_LONG_NAME "The Vulkan Cube Demo Program"
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -060052
Courtney Goeltzenleuchter8c15fc52015-07-06 17:46:11 -060053#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
54
Tony Barbour22a30862015-04-22 09:02:32 -060055#if defined(NDEBUG) && defined(__GNUC__)
56#define U_ASSERT_ONLY __attribute__((unused))
57#else
58#define U_ASSERT_ONLY
59#endif
60
Ian Elliottcaa9f272015-04-28 11:35:02 -060061#ifdef _WIN32
62#define ERR_EXIT(err_msg, err_class) \
63 do { \
64 MessageBox(NULL, err_msg, err_class, MB_OK); \
65 exit(1); \
66 } while (0)
67
68// NOTE: If the following values (copied from "loader_platform.h") change, they
69// need to change here as well:
70#define LAYER_NAMES_ENV "VK_LAYER_NAMES"
71#define LAYER_NAMES_REGISTRY_VALUE "VK_LAYER_NAMES"
72
73#else // _WIN32
74
75#define ERR_EXIT(err_msg, err_class) \
76 do { \
77 printf(err_msg); \
78 fflush(stdout); \
79 exit(1); \
80 } while (0)
81#endif // _WIN32
82
Ian Elliott1b6de092015-06-22 15:07:49 -060083#define GET_DEVICE_PROC_ADDR(dev, entrypoint) \
84{ \
85 demo->fp##entrypoint = vkGetDeviceProcAddr(dev, "vk"#entrypoint); \
86 if (demo->fp##entrypoint == NULL) { \
87 ERR_EXIT("vkGetDeviceProcAddr failed to find vk"#entrypoint, \
88 "vkGetDeviceProcAddr Failure"); \
89 } \
90}
91
Courtney Goeltzenleuchter7e334982014-10-30 15:14:16 -060092/*
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -070093 * structure to track all objects related to a texture.
94 */
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -060095struct texture_object {
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -060096 VkSampler sampler;
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -070097
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -060098 VkImage image;
99 VkImageLayout imageLayout;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600100
Mark Lobodzinski23182612015-05-29 09:32:35 -0500101 VkDeviceMemory mem;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600102 VkImageView view;
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700103 int32_t tex_width, tex_height;
104};
105
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600106static char *tex_files[] = {
Courtney Goeltzenleuchter7e334982014-10-30 15:14:16 -0600107 "lunarg-logo-256x256-solid.png"
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600108};
109
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600110struct vkcube_vs_uniform {
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -0600111 // Must start with MVP
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600112 float mvp[4][4];
113 float position[12*3][4];
114 float color[12*3][4];
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -0600115};
116
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600117struct vktexcube_vs_uniform {
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -0600118 // Must start with MVP
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600119 float mvp[4][4];
120 float position[12*3][4];
121 float attr[12*3][4];
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -0600122};
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -0600123
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600124//--------------------------------------------------------------------------------------
125// Mesh and VertexFormat Data
126//--------------------------------------------------------------------------------------
127struct Vertex
128{
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600129 float posX, posY, posZ, posW; // Position data
130 float r, g, b, a; // Color
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600131};
132
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -0600133struct VertexPosTex
134{
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600135 float posX, posY, posZ, posW; // Position data
136 float u, v, s, t; // Texcoord
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -0600137};
138
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600139#define XYZ1(_x_, _y_, _z_) (_x_), (_y_), (_z_), 1.f
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -0600140#define UV(_u_, _v_) (_u_), (_v_), 0.f, 1.f
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600141
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600142static const float g_vertex_buffer_data[] = {
Chia-I Wuc3487c22015-04-22 14:56:17 +0800143 -1.0f,-1.0f,-1.0f, // -X side
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600144 -1.0f,-1.0f, 1.0f,
145 -1.0f, 1.0f, 1.0f,
Chia-I Wuc3487c22015-04-22 14:56:17 +0800146 -1.0f, 1.0f, 1.0f,
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600147 -1.0f, 1.0f,-1.0f,
148 -1.0f,-1.0f,-1.0f,
149
Chia-I Wuc3487c22015-04-22 14:56:17 +0800150 -1.0f,-1.0f,-1.0f, // -Z side
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600151 1.0f, 1.0f,-1.0f,
152 1.0f,-1.0f,-1.0f,
Chia-I Wuc3487c22015-04-22 14:56:17 +0800153 -1.0f,-1.0f,-1.0f,
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600154 -1.0f, 1.0f,-1.0f,
Mike Stroyanea3945c2015-03-19 14:29:04 -0600155 1.0f, 1.0f,-1.0f,
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600156
Chia-I Wuc3487c22015-04-22 14:56:17 +0800157 -1.0f,-1.0f,-1.0f, // -Y side
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600158 1.0f,-1.0f,-1.0f,
Mike Stroyanea3945c2015-03-19 14:29:04 -0600159 1.0f,-1.0f, 1.0f,
Chia-I Wuc3487c22015-04-22 14:56:17 +0800160 -1.0f,-1.0f,-1.0f,
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600161 1.0f,-1.0f, 1.0f,
Mike Stroyanea3945c2015-03-19 14:29:04 -0600162 -1.0f,-1.0f, 1.0f,
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600163
Chia-I Wuc3487c22015-04-22 14:56:17 +0800164 -1.0f, 1.0f,-1.0f, // +Y side
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600165 -1.0f, 1.0f, 1.0f,
166 1.0f, 1.0f, 1.0f,
Chia-I Wuc3487c22015-04-22 14:56:17 +0800167 -1.0f, 1.0f,-1.0f,
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600168 1.0f, 1.0f, 1.0f,
Mike Stroyanea3945c2015-03-19 14:29:04 -0600169 1.0f, 1.0f,-1.0f,
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600170
Chia-I Wuc3487c22015-04-22 14:56:17 +0800171 1.0f, 1.0f,-1.0f, // +X side
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600172 1.0f, 1.0f, 1.0f,
173 1.0f,-1.0f, 1.0f,
Chia-I Wuc3487c22015-04-22 14:56:17 +0800174 1.0f,-1.0f, 1.0f,
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600175 1.0f,-1.0f,-1.0f,
176 1.0f, 1.0f,-1.0f,
177
Chia-I Wuc3487c22015-04-22 14:56:17 +0800178 -1.0f, 1.0f, 1.0f, // +Z side
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600179 -1.0f,-1.0f, 1.0f,
Mike Stroyanea3945c2015-03-19 14:29:04 -0600180 1.0f, 1.0f, 1.0f,
Chia-I Wuc3487c22015-04-22 14:56:17 +0800181 -1.0f,-1.0f, 1.0f,
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600182 1.0f,-1.0f, 1.0f,
183 1.0f, 1.0f, 1.0f,
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -0600184};
185
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600186static const float g_uv_buffer_data[] = {
Chia-I Wuc3487c22015-04-22 14:56:17 +0800187 0.0f, 0.0f, // -X side
188 1.0f, 0.0f,
189 1.0f, 1.0f,
190 1.0f, 1.0f,
191 0.0f, 1.0f,
192 0.0f, 0.0f,
193
194 1.0f, 0.0f, // -Z side
195 0.0f, 1.0f,
196 0.0f, 0.0f,
197 1.0f, 0.0f,
198 1.0f, 1.0f,
199 0.0f, 1.0f,
200
201 1.0f, 1.0f, // -Y side
202 1.0f, 0.0f,
203 0.0f, 0.0f,
204 1.0f, 1.0f,
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600205 0.0f, 0.0f,
206 0.0f, 1.0f,
207
Chia-I Wuc3487c22015-04-22 14:56:17 +0800208 1.0f, 1.0f, // +Y side
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -0600209 0.0f, 1.0f,
Chia-I Wuc3487c22015-04-22 14:56:17 +0800210 0.0f, 0.0f,
Mike Stroyanea3945c2015-03-19 14:29:04 -0600211 1.0f, 1.0f,
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600212 0.0f, 0.0f,
Mike Stroyanea3945c2015-03-19 14:29:04 -0600213 1.0f, 0.0f,
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600214
Chia-I Wuc3487c22015-04-22 14:56:17 +0800215 1.0f, 1.0f, // +X side
216 0.0f, 1.0f,
217 0.0f, 0.0f,
218 0.0f, 0.0f,
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600219 1.0f, 0.0f,
Mike Stroyanea3945c2015-03-19 14:29:04 -0600220 1.0f, 1.0f,
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600221
Chia-I Wuc3487c22015-04-22 14:56:17 +0800222 0.0f, 1.0f, // +Z side
223 0.0f, 0.0f,
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600224 1.0f, 1.0f,
Mike Stroyanea3945c2015-03-19 14:29:04 -0600225 0.0f, 0.0f,
Chia-I Wuc3487c22015-04-22 14:56:17 +0800226 1.0f, 0.0f,
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600227 1.0f, 1.0f,
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -0600228};
229
230void dumpMatrix(const char *note, mat4x4 MVP)
231{
232 int i;
233
234 printf("%s: \n", note);
235 for (i=0; i<4; i++) {
236 printf("%f, %f, %f, %f\n", MVP[i][0], MVP[i][1], MVP[i][2], MVP[i][3]);
237 }
238 printf("\n");
239 fflush(stdout);
240}
241
242void dumpVec4(const char *note, vec4 vector)
243{
244 printf("%s: \n", note);
245 printf("%f, %f, %f, %f\n", vector[0], vector[1], vector[2], vector[3]);
246 printf("\n");
247 fflush(stdout);
248}
249
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -0600250void dbgFunc(
251 VkFlags msgFlags,
252 VkObjectType objType,
253 VkObject srcObject,
254 size_t location,
255 int32_t msgCode,
256 const char* pLayerPrefix,
257 const char* pMsg,
258 void* pUserData)
Tony Barbour5685ad72015-04-29 16:19:20 -0600259{
260 char *message = (char *) malloc(strlen(pMsg)+100);
261
262 assert (message);
263
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -0600264 if (msgFlags & VK_DBG_REPORT_ERROR_BIT) {
265 sprintf(message,"ERROR: [%s] Code %d : %s", pLayerPrefix, msgCode, pMsg);
266 } else if (msgFlags & VK_DBG_REPORT_WARN_BIT) {
Tony Barboura65ecc22015-06-30 14:14:19 -0600267 // We know that we're submitting queues without fences, ignore this warning
268 if (strstr(pMsg, "vkQueueSubmit parameter, VkFence fence, is null pointer")){
269 return;
270 }
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -0600271 sprintf(message,"WARNING: [%s] Code %d : %s", pLayerPrefix, msgCode, pMsg);
Tony Barbour5685ad72015-04-29 16:19:20 -0600272 } else {
273 return;
274 }
275
276#ifdef _WIN32
277 MessageBox(NULL, message, "Alert", MB_OK);
278#else
279 printf("%s\n",message);
280 fflush(stdout);
281#endif
282 free(message);
283}
284
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600285struct demo {
Ian Elliotte14e9f92015-04-16 15:23:05 -0600286#ifdef _WIN32
287#define APP_NAME_STR_LEN 80
288 HINSTANCE connection; // hInstance - Windows Instance
289 char name[APP_NAME_STR_LEN]; // Name to put on the window/icon
290 HWND window; // hWnd - window handle
291#else // _WIN32
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600292 xcb_connection_t *connection;
293 xcb_screen_t *screen;
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800294 xcb_window_t window;
295 xcb_intern_atom_reply_t *atom_wm_delete_window;
Ian Elliotte14e9f92015-04-16 15:23:05 -0600296#endif // _WIN32
Cody Northrop75db0322015-05-28 11:27:16 -0600297 bool prepared;
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700298 bool use_staging_buffer;
Cody Northrop75db0322015-05-28 11:27:16 -0600299 bool use_glsl;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600300
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600301 VkInstance inst;
Tony Barbour8205d902015-04-16 15:59:00 -0600302 VkPhysicalDevice gpu;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600303 VkDevice device;
304 VkQueue queue;
Courtney Goeltzenleuchterf3168062015-03-05 18:09:39 -0700305 uint32_t graphics_queue_node_index;
Tony Barbour426b9052015-06-24 16:06:58 -0600306 VkPhysicalDeviceProperties gpu_props;
Tony Barbour8205d902015-04-16 15:59:00 -0600307 VkPhysicalDeviceQueueProperties *queue_props;
Mark Lobodzinski72346292015-07-02 16:49:40 -0600308 VkPhysicalDeviceMemoryProperties memory_properties;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600309
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600310 VkFramebuffer framebuffer;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600311 int width, height;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600312 VkFormat format;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600313
Jon Ashburncedc15f2015-05-21 18:13:33 -0600314 PFN_vkCreateSwapChainWSI fpCreateSwapChainWSI;
315 PFN_vkDestroySwapChainWSI fpDestroySwapChainWSI;
316 PFN_vkGetSwapChainInfoWSI fpGetSwapChainInfoWSI;
317 PFN_vkQueuePresentWSI fpQueuePresentWSI;
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800318 VkSwapChainWSI swap_chain;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600319 struct {
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600320 VkImage image;
Tony Barbour8205d902015-04-16 15:59:00 -0600321 VkDeviceMemory mem;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600322 VkCmdBuffer cmd;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600323
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600324 VkColorAttachmentView view;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600325 } buffers[DEMO_BUFFER_COUNT];
326
327 struct {
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600328 VkFormat format;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600329
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600330 VkImage image;
Mark Lobodzinski23182612015-05-29 09:32:35 -0500331 VkDeviceMemory mem;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600332 VkDepthStencilView view;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600333 } depth;
334
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600335 struct texture_object textures[DEMO_TEXTURE_COUNT];
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600336
337 struct {
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600338 VkBuffer buf;
Mark Lobodzinski23182612015-05-29 09:32:35 -0500339 VkDeviceMemory mem;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600340 VkBufferView view;
Chia-I Wu8cd8ecd2015-05-25 16:27:55 +0800341 VkDescriptorInfo desc;
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -0600342 } uniform_data;
343
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600344 VkCmdBuffer cmd; // Buffer for initialization commands
Mark Lobodzinski556f7212015-04-17 14:11:39 -0500345 VkPipelineLayout pipeline_layout;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600346 VkDescriptorSetLayout desc_layout;
347 VkPipeline pipeline;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600348
Courtney Goeltzenleuchterfcf855f2015-04-10 16:24:50 -0600349 VkDynamicVpState viewport;
350 VkDynamicRsState raster;
351 VkDynamicCbState color_blend;
352 VkDynamicDsState depth_stencil;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600353
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -0600354 mat4x4 projection_matrix;
355 mat4x4 view_matrix;
356 mat4x4 model_matrix;
357
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600358 float spin_angle;
359 float spin_increment;
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -0600360 bool pause;
361
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600362 VkDescriptorPool desc_pool;
363 VkDescriptorSet desc_set;
Chia-I Wuf8385062015-01-04 16:27:24 +0800364
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600365 bool quit;
David Pinedoeeca2a22015-06-18 17:03:14 -0600366 int32_t curFrame;
367 int32_t frameCount;
Tony Barbour5685ad72015-04-29 16:19:20 -0600368 bool validate;
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -0600369 PFN_vkDbgCreateMsgCallback dbgCreateMsgCallback;
Tony Barboura65ecc22015-06-30 14:14:19 -0600370 PFN_vkDbgDestroyMsgCallback dbgDestroyMsgCallback;
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -0600371 VkDbgMsgCallback msg_callback;
372
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600373 uint32_t current_buffer;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600374};
375
Mark Lobodzinski72346292015-07-02 16:49:40 -0600376static VkResult memory_type_from_properties(struct demo *demo, uint32_t typeBits, VkFlags properties, uint32_t *typeIndex)
377{
378 // Search memtypes to find first index with those properties
379 for (uint32_t i = 0; i < 32; i++) {
380 if ((typeBits & 1) == 1) {
381 // Type is available, does it match user properties?
382 if ((demo->memory_properties.memoryTypes[i].propertyFlags & properties) == properties) {
383 *typeIndex = i;
384 return VK_SUCCESS;
385 }
386 }
387 typeBits >>= 1;
388 }
389 // No memory types matched, return failure
390 return VK_UNSUPPORTED;
391}
392
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600393static void demo_flush_init_cmd(struct demo *demo)
394{
Tony Barbour22a30862015-04-22 09:02:32 -0600395 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600396
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600397 if (demo->cmd == VK_NULL_HANDLE)
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600398 return;
399
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600400 err = vkEndCommandBuffer(demo->cmd);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600401 assert(!err);
402
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600403 const VkCmdBuffer cmd_bufs[] = { demo->cmd };
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600404
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600405 err = vkQueueSubmit(demo->queue, 1, cmd_bufs, VK_NULL_HANDLE);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600406 assert(!err);
407
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600408 err = vkQueueWaitIdle(demo->queue);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600409 assert(!err);
410
Mike Stroyan230e6252015-04-17 12:36:38 -0600411 vkDestroyObject(demo->device, VK_OBJECT_TYPE_COMMAND_BUFFER, demo->cmd);
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600412 demo->cmd = VK_NULL_HANDLE;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600413}
414
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600415static void demo_set_image_layout(
416 struct demo *demo,
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600417 VkImage image,
malnasse4b8ba4d2015-06-03 17:28:38 -0400418 VkImageAspect aspect,
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600419 VkImageLayout old_image_layout,
420 VkImageLayout new_image_layout)
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600421{
Tony Barbour22a30862015-04-22 09:02:32 -0600422 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600423
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600424 if (demo->cmd == VK_NULL_HANDLE) {
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600425 const VkCmdBufferCreateInfo cmd = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600426 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600427 .pNext = NULL,
428 .queueNodeIndex = demo->graphics_queue_node_index,
Chia-I Wu88eaa3b2015-06-26 15:34:39 +0800429 .level = VK_CMD_BUFFER_LEVEL_PRIMARY,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600430 .flags = 0,
431 };
432
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600433 err = vkCreateCommandBuffer(demo->device, &cmd, &demo->cmd);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600434 assert(!err);
435
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600436 VkCmdBufferBeginInfo cmd_buf_info = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600437 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600438 .pNext = NULL,
Tony Barbour8205d902015-04-16 15:59:00 -0600439 .flags = VK_CMD_BUFFER_OPTIMIZE_SMALL_BATCH_BIT |
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600440 VK_CMD_BUFFER_OPTIMIZE_ONE_TIME_SUBMIT_BIT,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600441 };
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600442 err = vkBeginCommandBuffer(demo->cmd, &cmd_buf_info);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600443 }
444
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600445 VkImageMemoryBarrier image_memory_barrier = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600446 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600447 .pNext = NULL,
448 .outputMask = 0,
449 .inputMask = 0,
450 .oldLayout = old_image_layout,
451 .newLayout = new_image_layout,
452 .image = image,
malnasse4b8ba4d2015-06-03 17:28:38 -0400453 .subresourceRange = { aspect, 0, 1, 0, 0 }
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600454 };
455
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600456 if (new_image_layout == VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL) {
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600457 /* Make sure anything that was copying from this image has completed */
Courtney Goeltzenleuchterad870812015-04-15 15:29:59 -0600458 image_memory_barrier.inputMask = VK_MEMORY_INPUT_TRANSFER_BIT;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600459 }
460
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600461 if (new_image_layout == VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL) {
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600462 /* Make sure any Copy or CPU writes to image are flushed */
Courtney Goeltzenleuchtera569a502015-04-29 17:16:21 -0600463 image_memory_barrier.outputMask = VK_MEMORY_OUTPUT_HOST_WRITE_BIT | VK_MEMORY_OUTPUT_TRANSFER_BIT;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600464 }
465
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600466 VkImageMemoryBarrier *pmemory_barrier = &image_memory_barrier;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600467
Tony Barbourc2e987e2015-06-29 16:20:35 -0600468 VkPipelineStageFlags src_stages = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
469 VkPipelineStageFlags dest_stages = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600470
Tony Barbourc2e987e2015-06-29 16:20:35 -0600471 vkCmdPipelineBarrier(demo->cmd, src_stages, dest_stages, false, 1, (const void **)&pmemory_barrier);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600472}
473
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600474static void demo_draw_build_cmd(struct demo *demo, VkCmdBuffer cmd_buf)
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600475{
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600476 const VkColorAttachmentBindInfo color_attachment = {
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600477 .view = demo->buffers[demo->current_buffer].view,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600478 .layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600479 };
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600480 const VkDepthStencilBindInfo depth_stencil = {
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600481 .view = demo->depth.view,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600482 .layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600483 };
Chris Forbese3105972015-06-24 14:34:53 +1200484 const VkClearColorValue clear_color = {
485 .f32 = { 0.2f, 0.2f, 0.2f, 0.2f },
Courtney Goeltzenleuchter9a1ded82015-04-03 16:35:32 -0600486 };
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600487 const float clear_depth = 1.0f;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600488 VkCmdBufferBeginInfo cmd_buf_info = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600489 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO,
Courtney Goeltzenleuchtere3b0f3a2015-04-03 15:25:24 -0600490 .pNext = NULL,
Tony Barbour8205d902015-04-16 15:59:00 -0600491 .flags = VK_CMD_BUFFER_OPTIMIZE_SMALL_BATCH_BIT |
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600492 VK_CMD_BUFFER_OPTIMIZE_ONE_TIME_SUBMIT_BIT,
Jon Ashburn53d27af2014-12-31 17:08:35 -0700493 };
Tony Barbour22a30862015-04-22 09:02:32 -0600494 VkResult U_ASSERT_ONLY err;
Chris Forbesfd8456c2015-06-17 11:36:12 +1200495 VkAttachmentLoadOp load_op = VK_ATTACHMENT_LOAD_OP_CLEAR;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600496 VkAttachmentStoreOp store_op = VK_ATTACHMENT_STORE_OP_DONT_CARE;
497 const VkFramebufferCreateInfo fb_info = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600498 .sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO,
Jon Ashburn3325d6b2015-01-02 18:24:05 -0700499 .pNext = NULL,
500 .colorAttachmentCount = 1,
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600501 .pColorAttachments = (VkColorAttachmentBindInfo*) &color_attachment,
502 .pDepthStencilAttachment = (VkDepthStencilBindInfo*) &depth_stencil,
Jon Ashburn3325d6b2015-01-02 18:24:05 -0700503 .sampleCount = 1,
Mark Lobodzinski71fcc2d2015-01-27 13:24:03 -0600504 .width = demo->width,
505 .height = demo->height,
506 .layers = 1,
Jon Ashburn3325d6b2015-01-02 18:24:05 -0700507 };
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600508 VkRenderPassCreateInfo rp_info;
509 VkRenderPassBegin rp_begin;
Jon Ashburn3325d6b2015-01-02 18:24:05 -0700510
Chia-I Wu88eaa3b2015-06-26 15:34:39 +0800511 rp_begin.contents = VK_RENDER_PASS_CONTENTS_INLINE;
512
Jon Ashburn3325d6b2015-01-02 18:24:05 -0700513 memset(&rp_info, 0 , sizeof(rp_info));
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600514 err = vkCreateFramebuffer(demo->device, &fb_info, &rp_begin.framebuffer);
Jon Ashburn3325d6b2015-01-02 18:24:05 -0700515 assert(!err);
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600516 rp_info.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO;
Jon Ashburn3325d6b2015-01-02 18:24:05 -0700517 rp_info.renderArea.extent.width = demo->width;
518 rp_info.renderArea.extent.height = demo->height;
Courtney Goeltzenleuchtere3b0f3a2015-04-03 15:25:24 -0600519 rp_info.colorAttachmentCount = fb_info.colorAttachmentCount;
520 rp_info.pColorFormats = &demo->format;
521 rp_info.pColorLayouts = &color_attachment.layout;
Jon Ashburn3325d6b2015-01-02 18:24:05 -0700522 rp_info.pColorLoadOps = &load_op;
523 rp_info.pColorStoreOps = &store_op;
Courtney Goeltzenleuchtere3b0f3a2015-04-03 15:25:24 -0600524 rp_info.pColorLoadClearValues = &clear_color;
Tony Barbour8205d902015-04-16 15:59:00 -0600525 rp_info.depthStencilFormat = VK_FORMAT_D16_UNORM;
Courtney Goeltzenleuchtere3b0f3a2015-04-03 15:25:24 -0600526 rp_info.depthStencilLayout = depth_stencil.layout;
Chris Forbesefc7a6f2015-06-22 18:47:28 +1200527 rp_info.depthLoadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
Courtney Goeltzenleuchtere3b0f3a2015-04-03 15:25:24 -0600528 rp_info.depthLoadClearValue = clear_depth;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600529 rp_info.depthStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
530 rp_info.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
Courtney Goeltzenleuchtere3b0f3a2015-04-03 15:25:24 -0600531 rp_info.stencilLoadClearValue = 0;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600532 rp_info.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
Tony Barboura65ecc22015-06-30 14:14:19 -0600533 rp_info.sampleCount = 1;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600534 err = vkCreateRenderPass(demo->device, &rp_info, &rp_begin.renderPass);
Jon Ashburn3325d6b2015-01-02 18:24:05 -0700535 assert(!err);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600536
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600537 err = vkBeginCommandBuffer(cmd_buf, &cmd_buf_info);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600538 assert(!err);
539
Tobin Ehlise4076782015-06-24 15:53:07 -0600540 vkCmdBeginRenderPass(cmd_buf, &rp_begin);
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600541 vkCmdBindPipeline(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600542 demo->pipeline);
Mark Lobodzinskia65c4632015-06-15 13:21:21 -0600543 vkCmdBindDescriptorSets(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS, demo->pipeline_layout,
Cody Northrop1a01b1d2015-04-16 13:41:56 -0600544 0, 1, &demo->desc_set, 0, NULL);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600545
Tony Barbour8205d902015-04-16 15:59:00 -0600546 vkCmdBindDynamicStateObject(cmd_buf, VK_STATE_BIND_POINT_VIEWPORT, demo->viewport);
547 vkCmdBindDynamicStateObject(cmd_buf, VK_STATE_BIND_POINT_RASTER, demo->raster);
548 vkCmdBindDynamicStateObject(cmd_buf, VK_STATE_BIND_POINT_COLOR_BLEND,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600549 demo->color_blend);
Tony Barbour8205d902015-04-16 15:59:00 -0600550 vkCmdBindDynamicStateObject(cmd_buf, VK_STATE_BIND_POINT_DEPTH_STENCIL,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600551 demo->depth_stencil);
552
Chris Forbesefc7a6f2015-06-22 18:47:28 +1200553 vkCmdBeginRenderPass(cmd_buf, &rp_begin);
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -0600554
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600555 vkCmdDraw(cmd_buf, 0, 12 * 3, 0, 1);
Chia-I Wu88eaa3b2015-06-26 15:34:39 +0800556 vkCmdEndRenderPass(cmd_buf);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600557
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600558 err = vkEndCommandBuffer(cmd_buf);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600559 assert(!err);
Courtney Goeltzenleuchter04fc2fb2015-02-25 17:53:18 -0700560
Mike Stroyan230e6252015-04-17 12:36:38 -0600561 vkDestroyObject(demo->device, VK_OBJECT_TYPE_RENDER_PASS, rp_begin.renderPass);
562 vkDestroyObject(demo->device, VK_OBJECT_TYPE_FRAMEBUFFER, rp_begin.framebuffer);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600563}
564
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -0600565
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -0600566void demo_update_data_buffer(struct demo *demo)
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600567{
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -0600568 mat4x4 MVP, Model, VP;
569 int matrixSize = sizeof(MVP);
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600570 uint8_t *pData;
Tony Barbour22a30862015-04-22 09:02:32 -0600571 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600572
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -0600573 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600574
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -0600575 // Rotate 22.5 degrees around the Y axis
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -0600576 mat4x4_dup(Model, demo->model_matrix);
Piers Daniell886be472015-02-23 16:23:13 -0700577 mat4x4_rotate(demo->model_matrix, Model, 0.0f, 1.0f, 0.0f, (float)degreesToRadians(demo->spin_angle));
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -0600578 mat4x4_mul(MVP, VP, demo->model_matrix);
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -0600579
Mark Lobodzinski23182612015-05-29 09:32:35 -0500580 err = vkMapMemory(demo->device, demo->uniform_data.mem, 0, 0, 0, (void **) &pData);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600581 assert(!err);
582
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -0600583 memcpy(pData, (const void*) &MVP[0][0], matrixSize);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600584
Mark Lobodzinski23182612015-05-29 09:32:35 -0500585 err = vkUnmapMemory(demo->device, demo->uniform_data.mem);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600586 assert(!err);
587}
588
589static void demo_draw(struct demo *demo)
590{
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800591 const VkPresentInfoWSI present = {
592 .sType = VK_STRUCTURE_TYPE_PRESENT_INFO_WSI,
593 .pNext = NULL,
594 .image = demo->buffers[demo->current_buffer].image,
595 .flipInterval = 0,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600596 };
Tony Barbour22a30862015-04-22 09:02:32 -0600597 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600598
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600599 err = vkQueueSubmit(demo->queue, 1, &demo->buffers[demo->current_buffer].cmd,
600 VK_NULL_HANDLE);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600601 assert(!err);
602
Jon Ashburncedc15f2015-05-21 18:13:33 -0600603 err = demo->fpQueuePresentWSI(demo->queue, &present);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600604 assert(!err);
605
606 demo->current_buffer = (demo->current_buffer + 1) % DEMO_BUFFER_COUNT;
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800607
608 err = vkQueueWaitIdle(demo->queue);
609 assert(err == VK_SUCCESS);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600610}
611
612static void demo_prepare_buffers(struct demo *demo)
613{
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800614 const VkSwapChainCreateInfoWSI swap_chain = {
615 .sType = VK_STRUCTURE_TYPE_SWAP_CHAIN_CREATE_INFO_WSI,
616 .pNext = NULL,
617 .pNativeWindowSystemHandle = demo->connection,
618 .pNativeWindowHandle = (void *) (intptr_t) demo->window,
Ian Elliott32536f92015-04-21 16:41:02 -0600619 .displayCount = 1,
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800620 .imageCount = DEMO_BUFFER_COUNT,
621 .imageFormat = demo->format,
622 .imageExtent = {
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600623 .width = demo->width,
624 .height = demo->height,
625 },
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800626 .imageArraySize = 1,
627 .imageUsageFlags = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600628 };
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800629 VkSwapChainImageInfoWSI images[DEMO_BUFFER_COUNT];
630 size_t images_size = sizeof(images);
Tony Barbour22a30862015-04-22 09:02:32 -0600631 VkResult U_ASSERT_ONLY err;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600632 uint32_t i;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600633
Jon Ashburncedc15f2015-05-21 18:13:33 -0600634 err = demo->fpCreateSwapChainWSI(demo->device, &swap_chain, &demo->swap_chain);
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800635 assert(!err);
636
Jon Ashburncedc15f2015-05-21 18:13:33 -0600637 err = demo->fpGetSwapChainInfoWSI(demo->swap_chain,
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800638 VK_SWAP_CHAIN_INFO_TYPE_PERSISTENT_IMAGES_WSI,
639 &images_size, images);
640 assert(!err && images_size == sizeof(images));
641
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600642 for (i = 0; i < DEMO_BUFFER_COUNT; i++) {
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600643 VkColorAttachmentViewCreateInfo color_attachment_view = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600644 .sType = VK_STRUCTURE_TYPE_COLOR_ATTACHMENT_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600645 .pNext = NULL,
646 .format = demo->format,
647 .mipLevel = 0,
648 .baseArraySlice = 0,
649 .arraySize = 1,
650 };
651
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800652 demo->buffers[i].image = images[i].image;
653 demo->buffers[i].mem = images[i].memory;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600654
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600655 demo_set_image_layout(demo, demo->buffers[i].image,
malnasse4b8ba4d2015-06-03 17:28:38 -0400656 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600657 VK_IMAGE_LAYOUT_UNDEFINED,
658 VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600659
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600660 color_attachment_view.image = demo->buffers[i].image;
661
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600662 err = vkCreateColorAttachmentView(demo->device,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600663 &color_attachment_view, &demo->buffers[i].view);
664 assert(!err);
665 }
666}
667
668static void demo_prepare_depth(struct demo *demo)
669{
Tony Barbour8205d902015-04-16 15:59:00 -0600670 const VkFormat depth_format = VK_FORMAT_D16_UNORM;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600671 const VkImageCreateInfo image = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600672 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600673 .pNext = NULL,
Tony Barbour8205d902015-04-16 15:59:00 -0600674 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600675 .format = depth_format,
676 .extent = { demo->width, demo->height, 1 },
677 .mipLevels = 1,
678 .arraySize = 1,
679 .samples = 1,
Tony Barbour8205d902015-04-16 15:59:00 -0600680 .tiling = VK_IMAGE_TILING_OPTIMAL,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600681 .usage = VK_IMAGE_USAGE_DEPTH_STENCIL_BIT,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600682 .flags = 0,
683 };
Courtney Goeltzenleuchterddcb6192015-04-14 18:48:46 -0600684 VkMemoryAllocInfo mem_alloc = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600685 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
Mark Lobodzinski97dcd042015-04-16 08:52:00 -0500686 .pNext = NULL,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600687 .allocationSize = 0,
Mark Lobodzinski72346292015-07-02 16:49:40 -0600688 .memoryTypeIndex = 0,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600689 };
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600690 VkDepthStencilViewCreateInfo view = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600691 .sType = VK_STRUCTURE_TYPE_DEPTH_STENCIL_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600692 .pNext = NULL,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600693 .image = VK_NULL_HANDLE,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600694 .mipLevel = 0,
695 .baseArraySlice = 0,
696 .arraySize = 1,
697 .flags = 0,
698 };
Mike Stroyan230e6252015-04-17 12:36:38 -0600699
Mark Lobodzinski23182612015-05-29 09:32:35 -0500700 VkMemoryRequirements mem_reqs;
Tony Barbour22a30862015-04-22 09:02:32 -0600701 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600702
703 demo->depth.format = depth_format;
704
705 /* create image */
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600706 err = vkCreateImage(demo->device, &image,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600707 &demo->depth.image);
708 assert(!err);
709
Tony Barbour426b9052015-06-24 16:06:58 -0600710 err = vkGetObjectMemoryRequirements(demo->device,
711 VK_OBJECT_TYPE_IMAGE, demo->depth.image, &mem_reqs);
Mark Lobodzinski72346292015-07-02 16:49:40 -0600712
713 mem_alloc.allocationSize = mem_reqs.size;
714 err = memory_type_from_properties(demo,
715 mem_reqs.memoryTypeBits,
716 VK_MEMORY_PROPERTY_DEVICE_ONLY,
717 &mem_alloc.memoryTypeIndex);
718 assert(!err);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600719
Mark Lobodzinski23182612015-05-29 09:32:35 -0500720 /* allocate memory */
721 err = vkAllocMemory(demo->device, &mem_alloc, &demo->depth.mem);
722 assert(!err);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600723
Mark Lobodzinski23182612015-05-29 09:32:35 -0500724 /* bind memory */
725 err = vkBindObjectMemory(demo->device,
726 VK_OBJECT_TYPE_IMAGE, demo->depth.image,
727 demo->depth.mem, 0);
728 assert(!err);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600729
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600730 demo_set_image_layout(demo, demo->depth.image,
malnasse4b8ba4d2015-06-03 17:28:38 -0400731 VK_IMAGE_ASPECT_DEPTH,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600732 VK_IMAGE_LAYOUT_UNDEFINED,
733 VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600734
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600735 /* create image view */
736 view.image = demo->depth.image;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600737 err = vkCreateDepthStencilView(demo->device, &view,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600738 &demo->depth.view);
739 assert(!err);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600740}
741
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600742/** loadTexture
Courtney Goeltzenleuchtercb67a322015-04-21 09:31:23 -0600743 * loads a png file into an memory object, using cstdio , libpng.
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600744 *
Courtney Goeltzenleuchtercb67a322015-04-21 09:31:23 -0600745 * \param demo : Needed to access VK calls
746 * \param filename : the png file to be loaded
747 * \param width : width of png, to be updated as a side effect of this function
748 * \param height : height of png, to be updated as a side effect of this function
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600749 *
Courtney Goeltzenleuchtercb67a322015-04-21 09:31:23 -0600750 * \return bool : an opengl texture id. true if successful?,
751 * should be validated by the client of this function.
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600752 *
753 * Source: http://en.wikibooks.org/wiki/OpenGL_Programming/Intermediate/Textures
754 * Modified to copy image to memory
755 *
756 */
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700757bool loadTexture(const char *filename, uint8_t *rgba_data,
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600758 VkSubresourceLayout *layout,
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600759 int32_t *width, int32_t *height)
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600760{
761 //header for testing if it is a png
762 png_byte header[8];
Tony Barboura938abb2015-04-22 11:36:22 -0600763 int is_png, bit_depth, color_type, rowbytes;
764 size_t retval;
Ian Elliott642f8922015-02-13 14:29:21 -0700765 png_uint_32 i, twidth, theight;
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600766 png_structp png_ptr;
767 png_infop info_ptr, end_info;
768 png_byte *image_data;
769 png_bytep *row_pointers;
770
771 //open file as binary
772 FILE *fp = fopen(filename, "rb");
773 if (!fp) {
774 return false;
775 }
776
777 //read the header
Tony Barbour22a30862015-04-22 09:02:32 -0600778 retval = fread(header, 1, 8, fp);
779 if (retval != 8) {
780 fclose(fp);
781 return false;
782 }
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600783
784 //test if png
785 is_png = !png_sig_cmp(header, 0, 8);
786 if (!is_png) {
787 fclose(fp);
788 return false;
789 }
790
791 //create png struct
792 png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL,
793 NULL, NULL);
794 if (!png_ptr) {
795 fclose(fp);
796 return (false);
797 }
798
799 //create png info struct
800 info_ptr = png_create_info_struct(png_ptr);
801 if (!info_ptr) {
802 png_destroy_read_struct(&png_ptr, (png_infopp) NULL, (png_infopp) NULL);
803 fclose(fp);
804 return (false);
805 }
806
807 //create png info struct
808 end_info = png_create_info_struct(png_ptr);
809 if (!end_info) {
810 png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp) NULL);
811 fclose(fp);
812 return (false);
813 }
814
815 //png error stuff, not sure libpng man suggests this.
816 if (setjmp(png_jmpbuf(png_ptr))) {
817 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
818 fclose(fp);
819 return (false);
820 }
821
822 //init png reading
823 png_init_io(png_ptr, fp);
824
825 //let libpng know you already read the first 8 bytes
826 png_set_sig_bytes(png_ptr, 8);
827
828 // read all the info up to the image data
829 png_read_info(png_ptr, info_ptr);
830
831 // get info about png
832 png_get_IHDR(png_ptr, info_ptr, &twidth, &theight, &bit_depth, &color_type,
833 NULL, NULL, NULL);
834
835 //update width and height based on png info
836 *width = twidth;
837 *height = theight;
838
839 // Require that incoming texture be 8bits per color component
840 // and 4 components (RGBA).
841 if (png_get_bit_depth(png_ptr, info_ptr) != 8 ||
842 png_get_channels(png_ptr, info_ptr) != 4) {
843 return false;
844 }
845
846 if (rgba_data == NULL) {
847 // If data pointer is null, we just want the width & height
848 // clean up memory and close stuff
849 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
850 fclose(fp);
851
852 return true;
853 }
854
855 // Update the png info struct.
856 png_read_update_info(png_ptr, info_ptr);
857
858 // Row size in bytes.
859 rowbytes = png_get_rowbytes(png_ptr, info_ptr);
860
861 // Allocate the image_data as a big block, to be given to opengl
862 image_data = (png_byte *)malloc(rowbytes * theight * sizeof(png_byte));
863 if (!image_data) {
864 //clean up memory and close stuff
865 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
866 fclose(fp);
867 return false;
868 }
869
870 // row_pointers is for pointing to image_data for reading the png with libpng
871 row_pointers = (png_bytep *)malloc(theight * sizeof(png_bytep));
872 if (!row_pointers) {
873 //clean up memory and close stuff
874 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
875 // delete[] image_data;
876 fclose(fp);
877 return false;
878 }
879 // set the individual row_pointers to point at the correct offsets of image_data
880 for (i = 0; i < theight; ++i)
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700881 row_pointers[theight - 1 - i] = rgba_data + i * layout->rowPitch;
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600882
883 // read the png into image_data through row_pointers
884 png_read_image(png_ptr, row_pointers);
885
886 // clean up memory and close stuff
887 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
888 free(row_pointers);
889 free(image_data);
890 fclose(fp);
891
892 return true;
893}
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600894
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700895static void demo_prepare_texture_image(struct demo *demo,
896 const char *filename,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600897 struct texture_object *tex_obj,
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600898 VkImageTiling tiling,
Courtney Goeltzenleuchtercb67a322015-04-21 09:31:23 -0600899 VkImageUsageFlags usage,
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600900 VkFlags mem_props)
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600901{
Mike Stroyande24a6f2015-06-15 14:21:03 -0600902 const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600903 int32_t tex_width;
904 int32_t tex_height;
Tony Barbour22a30862015-04-22 09:02:32 -0600905 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700906
David Pinedo61e77382015-04-23 08:16:57 -0600907 if (!loadTexture(filename, NULL, NULL, &tex_width, &tex_height))
908 {
909 printf("Failed to load textures\n");
910 fflush(stdout);
911 exit(1);
912 }
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700913
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600914 tex_obj->tex_width = tex_width;
915 tex_obj->tex_height = tex_height;
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700916
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600917 const VkImageCreateInfo image_create_info = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600918 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700919 .pNext = NULL,
Tony Barbour8205d902015-04-16 15:59:00 -0600920 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700921 .format = tex_format,
922 .extent = { tex_width, tex_height, 1 },
923 .mipLevels = 1,
924 .arraySize = 1,
925 .samples = 1,
926 .tiling = tiling,
Courtney Goeltzenleuchtercb67a322015-04-21 09:31:23 -0600927 .usage = usage,
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700928 .flags = 0,
929 };
Courtney Goeltzenleuchterddcb6192015-04-14 18:48:46 -0600930 VkMemoryAllocInfo mem_alloc = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600931 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
Mark Lobodzinski97dcd042015-04-16 08:52:00 -0500932 .pNext = NULL,
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700933 .allocationSize = 0,
Mark Lobodzinski72346292015-07-02 16:49:40 -0600934 .memoryTypeIndex = 0,
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700935 };
936
Mark Lobodzinski23182612015-05-29 09:32:35 -0500937 VkMemoryRequirements mem_reqs;
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700938
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600939 err = vkCreateImage(demo->device, &image_create_info,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600940 &tex_obj->image);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700941 assert(!err);
942
Tony Barbour426b9052015-06-24 16:06:58 -0600943 err = vkGetObjectMemoryRequirements(demo->device,
944 VK_OBJECT_TYPE_IMAGE, tex_obj->image, &mem_reqs);
945 assert(!err);
Piers Daniell886be472015-02-23 16:23:13 -0700946
Mark Lobodzinski23182612015-05-29 09:32:35 -0500947 mem_alloc.allocationSize = mem_reqs.size;
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700948
Mark Lobodzinski72346292015-07-02 16:49:40 -0600949 err = memory_type_from_properties(demo, mem_reqs.memoryTypeBits, mem_props, &mem_alloc.memoryTypeIndex);
950 assert(!err);
951
Mark Lobodzinski23182612015-05-29 09:32:35 -0500952 /* allocate memory */
953 err = vkAllocMemory(demo->device, &mem_alloc,
954 &(tex_obj->mem));
955 assert(!err);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700956
Mark Lobodzinski23182612015-05-29 09:32:35 -0500957 /* bind memory */
958 err = vkBindObjectMemory(demo->device,
959 VK_OBJECT_TYPE_IMAGE, tex_obj->image,
960 tex_obj->mem, 0);
961 assert(!err);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700962
Tony Barbour8205d902015-04-16 15:59:00 -0600963 if (mem_props & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) {
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600964 const VkImageSubresource subres = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600965 .aspect = VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700966 .mipLevel = 0,
967 .arraySlice = 0,
968 };
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600969 VkSubresourceLayout layout;
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700970 void *data;
971
Tony Barbour426b9052015-06-24 16:06:58 -0600972 err = vkGetImageSubresourceLayout(demo->device, tex_obj->image, &subres, &layout);
973 assert(!err);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700974
Mark Lobodzinski23182612015-05-29 09:32:35 -0500975 err = vkMapMemory(demo->device, tex_obj->mem, 0, 0, 0, &data);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700976 assert(!err);
977
978 if (!loadTexture(filename, data, &layout, &tex_width, &tex_height)) {
979 fprintf(stderr, "Error loading texture: %s\n", filename);
980 }
981
Mark Lobodzinski23182612015-05-29 09:32:35 -0500982 err = vkUnmapMemory(demo->device, tex_obj->mem);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700983 assert(!err);
984 }
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600985
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600986 tex_obj->imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600987 demo_set_image_layout(demo, tex_obj->image,
malnasse4b8ba4d2015-06-03 17:28:38 -0400988 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600989 VK_IMAGE_LAYOUT_UNDEFINED,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600990 tex_obj->imageLayout);
991 /* setting the image layout does not reference the actual memory so no need to add a mem ref */
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700992}
993
Mark Lobodzinskicf26e072015-04-16 11:44:05 -0500994static void demo_destroy_texture_image(struct demo *demo, struct texture_object *tex_objs)
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700995{
996 /* clean up staging resources */
Mark Lobodzinski23182612015-05-29 09:32:35 -0500997 vkFreeMemory(demo->device, tex_objs->mem);
Mike Stroyan230e6252015-04-17 12:36:38 -0600998 vkDestroyObject(demo->device, VK_OBJECT_TYPE_IMAGE, tex_objs->image);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700999}
1000
1001static void demo_prepare_textures(struct demo *demo)
1002{
Tony Barbour8205d902015-04-16 15:59:00 -06001003 const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001004 VkFormatProperties props;
Tony Barbour22a30862015-04-22 09:02:32 -06001005 VkResult U_ASSERT_ONLY err;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001006 uint32_t i;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001007
Chris Forbesd7576302015-06-21 22:55:02 +12001008 err = vkGetPhysicalDeviceFormatInfo(demo->gpu, tex_format, &props);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001009 assert(!err);
1010
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001011 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001012
FslNopper434db6a2015-05-06 21:42:01 +02001013 if ((props.linearTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) && !demo->use_staging_buffer) {
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001014 /* Device can texture using linear textures */
1015 demo_prepare_texture_image(demo, tex_files[i], &demo->textures[i],
Courtney Goeltzenleuchtercb67a322015-04-21 09:31:23 -06001016 VK_IMAGE_TILING_LINEAR, VK_IMAGE_USAGE_SAMPLED_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
Tony Barbour8205d902015-04-16 15:59:00 -06001017 } else if (props.optimalTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) {
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001018 /* Must use staging buffer to copy linear texture to optimized */
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -06001019 struct texture_object staging_texture;
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001020
1021 memset(&staging_texture, 0, sizeof(staging_texture));
1022 demo_prepare_texture_image(demo, tex_files[i], &staging_texture,
Courtney Goeltzenleuchtercb67a322015-04-21 09:31:23 -06001023 VK_IMAGE_TILING_LINEAR, VK_IMAGE_USAGE_TRANSFER_SOURCE_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001024
1025 demo_prepare_texture_image(demo, tex_files[i], &demo->textures[i],
Courtney Goeltzenleuchtercb67a322015-04-21 09:31:23 -06001026 VK_IMAGE_TILING_OPTIMAL,
1027 (VK_IMAGE_USAGE_TRANSFER_DESTINATION_BIT | VK_IMAGE_USAGE_SAMPLED_BIT),
1028 VK_MEMORY_PROPERTY_DEVICE_ONLY);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001029
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -06001030 demo_set_image_layout(demo, staging_texture.image,
malnasse4b8ba4d2015-06-03 17:28:38 -04001031 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -06001032 staging_texture.imageLayout,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001033 VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001034
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -06001035 demo_set_image_layout(demo, demo->textures[i].image,
malnasse4b8ba4d2015-06-03 17:28:38 -04001036 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -06001037 demo->textures[i].imageLayout,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001038 VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001039
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001040 VkImageCopy copy_region = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001041 .srcSubresource = { VK_IMAGE_ASPECT_COLOR, 0, 0 },
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001042 .srcOffset = { 0, 0, 0 },
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001043 .destSubresource = { VK_IMAGE_ASPECT_COLOR, 0, 0 },
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001044 .destOffset = { 0, 0, 0 },
1045 .extent = { staging_texture.tex_width, staging_texture.tex_height, 1 },
1046 };
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001047 vkCmdCopyImage(demo->cmd,
1048 staging_texture.image, VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL,
1049 demo->textures[i].image, VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL,
Courtney Goeltzenleuchter51cbf302015-03-25 11:25:10 -06001050 1, &copy_region);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001051
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -06001052 demo_set_image_layout(demo, demo->textures[i].image,
malnasse4b8ba4d2015-06-03 17:28:38 -04001053 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001054 VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -06001055 demo->textures[i].imageLayout);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001056
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -06001057 demo_flush_init_cmd(demo);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001058
Courtney Goeltzenleuchter876629f2015-04-21 09:30:03 -06001059 demo_destroy_texture_image(demo, &staging_texture);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001060 } else {
Mike Stroyande24a6f2015-06-15 14:21:03 -06001061 /* Can't support VK_FORMAT_R8G8B8A8_UNORM !? */
1062 assert(!"No support for R8G8B8A8_UNORM as texture image format");
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001063 }
1064
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001065 const VkSamplerCreateInfo sampler = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001066 .sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001067 .pNext = NULL,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001068 .magFilter = VK_TEX_FILTER_NEAREST,
1069 .minFilter = VK_TEX_FILTER_NEAREST,
Tony Barbour8205d902015-04-16 15:59:00 -06001070 .mipMode = VK_TEX_MIPMAP_MODE_BASE,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001071 .addressU = VK_TEX_ADDRESS_CLAMP,
1072 .addressV = VK_TEX_ADDRESS_CLAMP,
1073 .addressW = VK_TEX_ADDRESS_CLAMP,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001074 .mipLodBias = 0.0f,
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001075 .maxAnisotropy = 1,
Tony Barbour8205d902015-04-16 15:59:00 -06001076 .compareOp = VK_COMPARE_OP_NEVER,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001077 .minLod = 0.0f,
1078 .maxLod = 0.0f,
Tony Barbour2c4e7c72015-06-25 16:56:44 -06001079 .borderColor = VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001080 };
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -06001081
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001082 VkImageViewCreateInfo view = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001083 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001084 .pNext = NULL,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001085 .image = VK_NULL_HANDLE,
Tony Barbour8205d902015-04-16 15:59:00 -06001086 .viewType = VK_IMAGE_VIEW_TYPE_2D,
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001087 .format = tex_format,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001088 .channels = { VK_CHANNEL_SWIZZLE_R,
1089 VK_CHANNEL_SWIZZLE_G,
1090 VK_CHANNEL_SWIZZLE_B,
1091 VK_CHANNEL_SWIZZLE_A, },
1092 .subresourceRange = { VK_IMAGE_ASPECT_COLOR, 0, 1, 0, 1 },
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001093 };
Jon Ashburna9ae3832015-01-16 09:37:43 -07001094
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001095 /* create sampler */
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001096 err = vkCreateSampler(demo->device, &sampler,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001097 &demo->textures[i].sampler);
1098 assert(!err);
1099
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001100 /* create image view */
1101 view.image = demo->textures[i].image;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001102 err = vkCreateImageView(demo->device, &view,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001103 &demo->textures[i].view);
1104 assert(!err);
1105 }
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001106}
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001107
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001108void demo_prepare_cube_data_buffer(struct demo *demo)
1109{
Courtney Goeltzenleuchterddcb6192015-04-14 18:48:46 -06001110 VkBufferCreateInfo buf_info;
1111 VkBufferViewCreateInfo view_info;
Courtney Goeltzenleuchterddcb6192015-04-14 18:48:46 -06001112 VkMemoryAllocInfo alloc_info = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001113 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
Mark Lobodzinski97dcd042015-04-16 08:52:00 -05001114 .pNext = NULL,
Jon Ashburnc6ae13d2015-01-19 15:00:26 -07001115 .allocationSize = 0,
Mark Lobodzinski72346292015-07-02 16:49:40 -06001116 .memoryTypeIndex = 0,
Jon Ashburnc6ae13d2015-01-19 15:00:26 -07001117 };
Mark Lobodzinski23182612015-05-29 09:32:35 -05001118 VkMemoryRequirements mem_reqs;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001119 size_t mem_reqs_size = sizeof(VkMemoryRequirements);
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001120 uint8_t *pData;
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001121 int i;
1122 mat4x4 MVP, VP;
Tony Barbour22a30862015-04-22 09:02:32 -06001123 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001124 struct vktexcube_vs_uniform data;
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001125
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001126 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001127 mat4x4_mul(MVP, VP, demo->model_matrix);
1128 memcpy(data.mvp, MVP, sizeof(MVP));
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -06001129// dumpMatrix("MVP", MVP);
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001130
1131 for (i=0; i<12*3; i++) {
1132 data.position[i][0] = g_vertex_buffer_data[i*3];
1133 data.position[i][1] = g_vertex_buffer_data[i*3+1];
1134 data.position[i][2] = g_vertex_buffer_data[i*3+2];
1135 data.position[i][3] = 1.0f;
1136 data.attr[i][0] = g_uv_buffer_data[2*i];
1137 data.attr[i][1] = g_uv_buffer_data[2*i + 1];
1138 data.attr[i][2] = 0;
1139 data.attr[i][3] = 0;
1140 }
1141
Chia-I Wu714df452015-01-01 07:55:04 +08001142 memset(&buf_info, 0, sizeof(buf_info));
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001143 buf_info.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
Courtney Goeltzenleuchterad870812015-04-15 15:29:59 -06001144 buf_info.usage = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001145 err = vkCreateBuffer(demo->device, &buf_info, &demo->uniform_data.buf);
Chia-I Wu714df452015-01-01 07:55:04 +08001146 assert(!err);
1147
Tony Barbour426b9052015-06-24 16:06:58 -06001148 err = vkGetObjectMemoryRequirements(demo->device,
1149 VK_OBJECT_TYPE_BUFFER, demo->uniform_data.buf, &mem_reqs);
Mark Lobodzinski23182612015-05-29 09:32:35 -05001150 assert(!err && mem_reqs_size == sizeof(mem_reqs));
Chia-I Wu714df452015-01-01 07:55:04 +08001151
Mark Lobodzinski23182612015-05-29 09:32:35 -05001152 alloc_info.allocationSize = mem_reqs.size;
Mark Lobodzinski72346292015-07-02 16:49:40 -06001153 err = memory_type_from_properties(demo,
1154 mem_reqs.memoryTypeBits,
1155 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT,
1156 &alloc_info.memoryTypeIndex);
1157 assert(!err);
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001158
Mark Lobodzinski23182612015-05-29 09:32:35 -05001159 err = vkAllocMemory(demo->device, &alloc_info, &(demo->uniform_data.mem));
1160 assert(!err);
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001161
Mark Lobodzinski23182612015-05-29 09:32:35 -05001162 err = vkMapMemory(demo->device, demo->uniform_data.mem, 0, 0, 0, (void **) &pData);
1163 assert(!err);
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001164
Mark Lobodzinski23182612015-05-29 09:32:35 -05001165 memcpy(pData, &data, sizeof data);
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001166
Mark Lobodzinski23182612015-05-29 09:32:35 -05001167 err = vkUnmapMemory(demo->device, demo->uniform_data.mem);
1168 assert(!err);
1169
1170 err = vkBindObjectMemory(demo->device,
1171 VK_OBJECT_TYPE_BUFFER, demo->uniform_data.buf,
1172 demo->uniform_data.mem, 0);
1173 assert(!err);
Chia-I Wu714df452015-01-01 07:55:04 +08001174
1175 memset(&view_info, 0, sizeof(view_info));
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001176 view_info.sType = VK_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO;
Chia-I Wu714df452015-01-01 07:55:04 +08001177 view_info.buffer = demo->uniform_data.buf;
Tony Barbour8205d902015-04-16 15:59:00 -06001178 view_info.viewType = VK_BUFFER_VIEW_TYPE_RAW;
Chia-I Wu714df452015-01-01 07:55:04 +08001179 view_info.offset = 0;
1180 view_info.range = sizeof(data);
1181
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001182 err = vkCreateBufferView(demo->device, &view_info, &demo->uniform_data.view);
Chia-I Wu714df452015-01-01 07:55:04 +08001183 assert(!err);
1184
Chia-I Wu8cd8ecd2015-05-25 16:27:55 +08001185 demo->uniform_data.desc.bufferView = demo->uniform_data.view;
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001186}
1187
Chia-I Wuf8385062015-01-04 16:27:24 +08001188static void demo_prepare_descriptor_layout(struct demo *demo)
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001189{
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001190 const VkDescriptorSetLayoutBinding layout_bindings[2] = {
Chia-I Wufc9d9132015-03-26 15:04:41 +08001191 [0] = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001192 .descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
Chia-I Wud3114a22015-05-25 16:22:52 +08001193 .arraySize = 1,
Tony Barbour8205d902015-04-16 15:59:00 -06001194 .stageFlags = VK_SHADER_STAGE_VERTEX_BIT,
Chia-I Wu310eece2015-03-27 12:56:09 +08001195 .pImmutableSamplers = NULL,
Chia-I Wufc9d9132015-03-26 15:04:41 +08001196 },
1197 [1] = {
Courtney Goeltzenleuchterad870812015-04-15 15:29:59 -06001198 .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
Chia-I Wud3114a22015-05-25 16:22:52 +08001199 .arraySize = DEMO_TEXTURE_COUNT,
Tony Barbour8205d902015-04-16 15:59:00 -06001200 .stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT,
Chia-I Wu310eece2015-03-27 12:56:09 +08001201 .pImmutableSamplers = NULL,
Chia-I Wufc9d9132015-03-26 15:04:41 +08001202 },
1203 };
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001204 const VkDescriptorSetLayoutCreateInfo descriptor_layout = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001205 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
Chia-I Wuf8385062015-01-04 16:27:24 +08001206 .pNext = NULL,
Chia-I Wufc9d9132015-03-26 15:04:41 +08001207 .count = 2,
1208 .pBinding = layout_bindings,
Chia-I Wuf8385062015-01-04 16:27:24 +08001209 };
Tony Barbour22a30862015-04-22 09:02:32 -06001210 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001211
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001212 err = vkCreateDescriptorSetLayout(demo->device,
Chia-I Wu7732cb22015-03-26 15:27:55 +08001213 &descriptor_layout, &demo->desc_layout);
1214 assert(!err);
1215
Mark Lobodzinski556f7212015-04-17 14:11:39 -05001216 const VkPipelineLayoutCreateInfo pPipelineLayoutCreateInfo = {
1217 .sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
1218 .pNext = NULL,
1219 .descriptorSetCount = 1,
1220 .pSetLayouts = &demo->desc_layout,
1221 };
1222
1223 err = vkCreatePipelineLayout(demo->device,
1224 &pPipelineLayoutCreateInfo,
1225 &demo->pipeline_layout);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001226 assert(!err);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001227}
1228
Tobin Ehlise796a1d2015-07-02 11:02:49 -06001229static VkShader demo_prepare_shader(struct demo* demo,
Tony Barbour8205d902015-04-16 15:59:00 -06001230 VkShaderStage stage,
Tobin Ehlise796a1d2015-07-02 11:02:49 -06001231 const void* code,
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001232 size_t size)
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001233{
Courtney Goeltzenleuchter0b29b0d2015-06-24 18:24:19 -06001234 VkShaderModuleCreateInfo moduleCreateInfo;
1235 VkShaderCreateInfo shaderCreateInfo;
1236 VkShaderModule shaderModule;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001237 VkShader shader;
1238 VkResult err;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001239
Courtney Goeltzenleuchter7e334982014-10-30 15:14:16 -06001240
Courtney Goeltzenleuchter0b29b0d2015-06-24 18:24:19 -06001241 moduleCreateInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
1242 moduleCreateInfo.pNext = NULL;
1243
1244 shaderCreateInfo.sType = VK_STRUCTURE_TYPE_SHADER_CREATE_INFO;
1245 shaderCreateInfo.pNext = NULL;
Tobin Ehlise796a1d2015-07-02 11:02:49 -06001246 shaderCreateInfo.pName = "main";
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001247
Cody Northrop75db0322015-05-28 11:27:16 -06001248 if (!demo->use_glsl) {
Courtney Goeltzenleuchter0b29b0d2015-06-24 18:24:19 -06001249 moduleCreateInfo.codeSize = size;
1250 moduleCreateInfo.pCode = code;
1251 moduleCreateInfo.flags = 0;
1252 err = vkCreateShaderModule(demo->device, &moduleCreateInfo, &shaderModule);
Cody Northrop75db0322015-05-28 11:27:16 -06001253 if (err) {
Courtney Goeltzenleuchter0b29b0d2015-06-24 18:24:19 -06001254 free((void *) moduleCreateInfo.pCode);
Cody Northrop75db0322015-05-28 11:27:16 -06001255 }
Courtney Goeltzenleuchter0b29b0d2015-06-24 18:24:19 -06001256
1257 shaderCreateInfo.flags = 0;
1258 shaderCreateInfo.module = shaderModule;
1259 err = vkCreateShader(demo->device, &shaderCreateInfo, &shader);
Cody Northrop75db0322015-05-28 11:27:16 -06001260 } else {
1261 // Create fake SPV structure to feed GLSL
1262 // to the driver "under the covers"
Courtney Goeltzenleuchter0b29b0d2015-06-24 18:24:19 -06001263 moduleCreateInfo.codeSize = 3 * sizeof(uint32_t) + size + 1;
1264 moduleCreateInfo.pCode = malloc(moduleCreateInfo.codeSize);
1265 moduleCreateInfo.flags = 0;
Cody Northrop75db0322015-05-28 11:27:16 -06001266
1267 /* try version 0 first: VkShaderStage followed by GLSL */
Courtney Goeltzenleuchter0b29b0d2015-06-24 18:24:19 -06001268 ((uint32_t *) moduleCreateInfo.pCode)[0] = ICD_SPV_MAGIC;
1269 ((uint32_t *) moduleCreateInfo.pCode)[1] = 0;
1270 ((uint32_t *) moduleCreateInfo.pCode)[2] = stage;
1271 memcpy(((uint32_t *) moduleCreateInfo.pCode + 3), code, size + 1);
Cody Northrop75db0322015-05-28 11:27:16 -06001272
Courtney Goeltzenleuchter0b29b0d2015-06-24 18:24:19 -06001273 err = vkCreateShaderModule(demo->device, &moduleCreateInfo, &shaderModule);
Cody Northrop75db0322015-05-28 11:27:16 -06001274 if (err) {
Courtney Goeltzenleuchter0b29b0d2015-06-24 18:24:19 -06001275 free((void *) moduleCreateInfo.pCode);
Cody Northrop75db0322015-05-28 11:27:16 -06001276 }
Courtney Goeltzenleuchter0b29b0d2015-06-24 18:24:19 -06001277
1278 shaderCreateInfo.flags = 0;
1279 shaderCreateInfo.module = shaderModule;
1280 err = vkCreateShader(demo->device, &shaderCreateInfo, &shader);
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -06001281 }
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001282 return shader;
1283}
1284
Cody Northropacfb0492015-03-17 15:55:58 -06001285char *demo_read_spv(const char *filename, size_t *psize)
Courtney Goeltzenleuchter7e334982014-10-30 15:14:16 -06001286{
1287 long int size;
Tony Barboura938abb2015-04-22 11:36:22 -06001288 size_t U_ASSERT_ONLY retval;
Courtney Goeltzenleuchter7e334982014-10-30 15:14:16 -06001289 void *shader_code;
1290
1291 FILE *fp = fopen(filename, "rb");
1292 if (!fp) return NULL;
1293
1294 fseek(fp, 0L, SEEK_END);
1295 size = ftell(fp);
1296
1297 fseek(fp, 0L, SEEK_SET);
1298
1299 shader_code = malloc(size);
Tony Barbour22a30862015-04-22 09:02:32 -06001300 retval = fread(shader_code, size, 1, fp);
1301 assert(retval == 1);
Courtney Goeltzenleuchter7e334982014-10-30 15:14:16 -06001302
1303 *psize = size;
1304
1305 return shader_code;
1306}
1307
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001308static VkShader demo_prepare_vs(struct demo *demo)
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001309{
Cody Northrop75db0322015-05-28 11:27:16 -06001310 if (!demo->use_glsl) {
1311 void *vertShaderCode;
1312 size_t size;
Courtney Goeltzenleuchter7e334982014-10-30 15:14:16 -06001313
Cody Northrop75db0322015-05-28 11:27:16 -06001314 vertShaderCode = demo_read_spv("cube-vert.spv", &size);
Courtney Goeltzenleuchter7e334982014-10-30 15:14:16 -06001315
Cody Northrop75db0322015-05-28 11:27:16 -06001316 return demo_prepare_shader(demo, VK_SHADER_STAGE_VERTEX,
1317 vertShaderCode, size);
1318 } else {
1319 static const char *vertShaderText =
1320 "#version 140\n"
1321 "#extension GL_ARB_separate_shader_objects : enable\n"
1322 "#extension GL_ARB_shading_language_420pack : enable\n"
1323 "\n"
1324 "layout(binding = 0) uniform buf {\n"
1325 " mat4 MVP;\n"
1326 " vec4 position[12*3];\n"
1327 " vec4 attr[12*3];\n"
1328 "} ubuf;\n"
1329 "\n"
1330 "layout (location = 0) out vec4 texcoord;\n"
1331 "\n"
1332 "void main() \n"
1333 "{\n"
1334 " texcoord = ubuf.attr[gl_VertexID];\n"
1335 " gl_Position = ubuf.MVP * ubuf.position[gl_VertexID];\n"
1336 "\n"
1337 " // GL->VK conventions\n"
1338 " gl_Position.y = -gl_Position.y;\n"
1339 " gl_Position.z = (gl_Position.z + gl_Position.w) / 2.0;\n"
1340 "}\n";
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -06001341
Cody Northrop75db0322015-05-28 11:27:16 -06001342 return demo_prepare_shader(demo, VK_SHADER_STAGE_VERTEX,
1343 (const void *) vertShaderText,
1344 strlen(vertShaderText));
1345 }
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001346}
1347
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001348static VkShader demo_prepare_fs(struct demo *demo)
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001349{
Cody Northrop75db0322015-05-28 11:27:16 -06001350 if (!demo->use_glsl) {
1351 void *fragShaderCode;
1352 size_t size;
Courtney Goeltzenleuchter7e334982014-10-30 15:14:16 -06001353
Cody Northrop75db0322015-05-28 11:27:16 -06001354 fragShaderCode = demo_read_spv("cube-frag.spv", &size);
Courtney Goeltzenleuchter7e334982014-10-30 15:14:16 -06001355
Cody Northrop75db0322015-05-28 11:27:16 -06001356 return demo_prepare_shader(demo, VK_SHADER_STAGE_FRAGMENT,
1357 fragShaderCode, size);
1358 } else {
1359 static const char *fragShaderText =
1360 "#version 140\n"
1361 "#extension GL_ARB_separate_shader_objects : enable\n"
1362 "#extension GL_ARB_shading_language_420pack : enable\n"
1363 "layout (binding = 1) uniform sampler2D tex;\n"
1364 "\n"
1365 "layout (location = 0) in vec4 texcoord;\n"
1366 "layout (location = 0) out vec4 uFragColor;\n"
1367 "void main() {\n"
1368 " uFragColor = texture(tex, texcoord.xy);\n"
1369 "}\n";
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -06001370
Cody Northrop75db0322015-05-28 11:27:16 -06001371 return demo_prepare_shader(demo, VK_SHADER_STAGE_FRAGMENT,
1372 (const void *) fragShaderText,
1373 strlen(fragShaderText));
1374 }
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001375}
1376
1377static void demo_prepare_pipeline(struct demo *demo)
1378{
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001379 VkGraphicsPipelineCreateInfo pipeline;
Mark Lobodzinski0e0fb5c2015-06-23 15:11:57 -06001380
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001381 VkPipelineIaStateCreateInfo ia;
1382 VkPipelineRsStateCreateInfo rs;
1383 VkPipelineCbStateCreateInfo cb;
1384 VkPipelineDsStateCreateInfo ds;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001385 VkPipelineVpStateCreateInfo vp;
1386 VkPipelineMsStateCreateInfo ms;
Tony Barbour22a30862015-04-22 09:02:32 -06001387 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001388
1389 memset(&pipeline, 0, sizeof(pipeline));
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001390 pipeline.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
Mark Lobodzinski556f7212015-04-17 14:11:39 -05001391 pipeline.layout = demo->pipeline_layout;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001392
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001393 memset(&ia, 0, sizeof(ia));
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001394 ia.sType = VK_STRUCTURE_TYPE_PIPELINE_IA_STATE_CREATE_INFO;
Tony Barbour8205d902015-04-16 15:59:00 -06001395 ia.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001396
1397 memset(&rs, 0, sizeof(rs));
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001398 rs.sType = VK_STRUCTURE_TYPE_PIPELINE_RS_STATE_CREATE_INFO;
Tony Barbour8205d902015-04-16 15:59:00 -06001399 rs.fillMode = VK_FILL_MODE_SOLID;
1400 rs.cullMode = VK_CULL_MODE_BACK;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001401 rs.frontFace = VK_FRONT_FACE_CCW;
Chia-I Wue2504cb2015-04-22 14:20:52 +08001402 rs.depthClipEnable = VK_TRUE;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001403
1404 memset(&cb, 0, sizeof(cb));
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001405 cb.sType = VK_STRUCTURE_TYPE_PIPELINE_CB_STATE_CREATE_INFO;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001406 VkPipelineCbAttachmentState att_state[1];
Tony Barbourfa6cac72015-01-16 14:27:35 -07001407 memset(att_state, 0, sizeof(att_state));
1408 att_state[0].format = demo->format;
1409 att_state[0].channelWriteMask = 0xf;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001410 att_state[0].blendEnable = VK_FALSE;
Tony Barbourfa6cac72015-01-16 14:27:35 -07001411 cb.attachmentCount = 1;
1412 cb.pAttachments = att_state;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001413
Tony Barbourfa6cac72015-01-16 14:27:35 -07001414 memset(&vp, 0, sizeof(vp));
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001415 vp.sType = VK_STRUCTURE_TYPE_PIPELINE_VP_STATE_CREATE_INFO;
Tony Barbour8205d902015-04-16 15:59:00 -06001416 vp.viewportCount = 1;
Tony Barbourfa6cac72015-01-16 14:27:35 -07001417
1418 memset(&ds, 0, sizeof(ds));
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001419 ds.sType = VK_STRUCTURE_TYPE_PIPELINE_DS_STATE_CREATE_INFO;
Tony Barbourfa6cac72015-01-16 14:27:35 -07001420 ds.format = demo->depth.format;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001421 ds.depthTestEnable = VK_TRUE;
1422 ds.depthWriteEnable = VK_TRUE;
Tony Barbour8205d902015-04-16 15:59:00 -06001423 ds.depthCompareOp = VK_COMPARE_OP_LESS_EQUAL;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001424 ds.depthBoundsEnable = VK_FALSE;
1425 ds.back.stencilFailOp = VK_STENCIL_OP_KEEP;
1426 ds.back.stencilPassOp = VK_STENCIL_OP_KEEP;
Tony Barbour8205d902015-04-16 15:59:00 -06001427 ds.back.stencilCompareOp = VK_COMPARE_OP_ALWAYS;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001428 ds.stencilTestEnable = VK_FALSE;
Tony Barbourfa6cac72015-01-16 14:27:35 -07001429 ds.front = ds.back;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001430
Tony Barbourfa6cac72015-01-16 14:27:35 -07001431 memset(&ms, 0, sizeof(ms));
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001432 ms.sType = VK_STRUCTURE_TYPE_PIPELINE_MS_STATE_CREATE_INFO;
Tony Barbourfa6cac72015-01-16 14:27:35 -07001433 ms.sampleMask = 1;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001434 ms.multisampleEnable = VK_FALSE;
Tony Barboure094edf2015-06-26 10:18:34 -06001435 ms.rasterSamples = 1;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001436
Mark Lobodzinski0e0fb5c2015-06-23 15:11:57 -06001437 // Two stages: vs and fs
1438 pipeline.stageCount = 2;
1439 VkPipelineShaderStageCreateInfo shaderStages[2];
1440 memset(&shaderStages, 0, 2 * sizeof(VkPipelineShaderStageCreateInfo));
1441
1442 shaderStages[0].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1443 shaderStages[0].stage = VK_SHADER_STAGE_VERTEX;
1444 shaderStages[0].shader = demo_prepare_vs(demo);
1445
1446 shaderStages[1].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1447 shaderStages[1].stage = VK_SHADER_STAGE_FRAGMENT;
1448 shaderStages[1].shader = demo_prepare_fs(demo);
1449
Mark Lobodzinskic1e21462015-06-30 10:18:36 -06001450 pipeline.pVertexInputState = NULL;
Mark Lobodzinski0e0fb5c2015-06-23 15:11:57 -06001451 pipeline.pIaState = &ia;
1452 pipeline.pRsState = &rs;
1453 pipeline.pCbState = &cb;
1454 pipeline.pMsState = &ms;
1455 pipeline.pVpState = &vp;
1456 pipeline.pDsState = &ds;
1457 pipeline.pStages = shaderStages;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001458
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001459 err = vkCreateGraphicsPipeline(demo->device, &pipeline, &demo->pipeline);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001460 assert(!err);
1461
Mark Lobodzinski0e0fb5c2015-06-23 15:11:57 -06001462 for (uint32_t i = 0; i < pipeline.stageCount; i++) {
1463 vkDestroyObject(demo->device, VK_OBJECT_TYPE_SHADER, shaderStages[i].shader);
1464 }
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001465}
1466
1467static void demo_prepare_dynamic_states(struct demo *demo)
1468{
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001469 VkDynamicVpStateCreateInfo viewport_create;
1470 VkDynamicRsStateCreateInfo raster;
1471 VkDynamicCbStateCreateInfo color_blend;
1472 VkDynamicDsStateCreateInfo depth_stencil;
Tony Barbour22a30862015-04-22 09:02:32 -06001473 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001474
Tony Barbourfa6cac72015-01-16 14:27:35 -07001475 memset(&viewport_create, 0, sizeof(viewport_create));
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001476 viewport_create.sType = VK_STRUCTURE_TYPE_DYNAMIC_VP_STATE_CREATE_INFO;
Courtney Goeltzenleuchterc6e32f92015-02-11 14:13:34 -07001477 viewport_create.viewportAndScissorCount = 1;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001478 VkViewport viewport;
Piers Daniell886be472015-02-23 16:23:13 -07001479 memset(&viewport, 0, sizeof(viewport));
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001480 viewport.height = (float) demo->height;
1481 viewport.width = (float) demo->width;
1482 viewport.minDepth = (float) 0.0f;
1483 viewport.maxDepth = (float) 1.0f;
Piers Daniell886be472015-02-23 16:23:13 -07001484 viewport_create.pViewports = &viewport;
Chris Forbes2951d7d2015-06-22 17:21:59 +12001485 VkRect2D scissor;
Piers Daniell886be472015-02-23 16:23:13 -07001486 memset(&scissor, 0, sizeof(scissor));
Courtney Goeltzenleuchterc6e32f92015-02-11 14:13:34 -07001487 scissor.extent.width = demo->width;
1488 scissor.extent.height = demo->height;
1489 scissor.offset.x = 0;
1490 scissor.offset.y = 0;
Courtney Goeltzenleuchterc6e32f92015-02-11 14:13:34 -07001491 viewport_create.pScissors = &scissor;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001492
1493 memset(&raster, 0, sizeof(raster));
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001494 raster.sType = VK_STRUCTURE_TYPE_DYNAMIC_RS_STATE_CREATE_INFO;
Piers Daniell886be472015-02-23 16:23:13 -07001495 raster.lineWidth = 1.0;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001496
1497 memset(&color_blend, 0, sizeof(color_blend));
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001498 color_blend.sType = VK_STRUCTURE_TYPE_DYNAMIC_CB_STATE_CREATE_INFO;
Piers Daniell886be472015-02-23 16:23:13 -07001499 color_blend.blendConst[0] = 1.0f;
1500 color_blend.blendConst[1] = 1.0f;
1501 color_blend.blendConst[2] = 1.0f;
1502 color_blend.blendConst[3] = 1.0f;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001503
1504 memset(&depth_stencil, 0, sizeof(depth_stencil));
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001505 depth_stencil.sType = VK_STRUCTURE_TYPE_DYNAMIC_DS_STATE_CREATE_INFO;
Mark Lobodzinski4405fbf2015-06-12 11:14:17 -06001506 depth_stencil.minDepthBounds = 0.0f;
1507 depth_stencil.maxDepthBounds = 1.0f;
Tony Barbourfa6cac72015-01-16 14:27:35 -07001508 depth_stencil.stencilBackRef = 0;
1509 depth_stencil.stencilFrontRef = 0;
1510 depth_stencil.stencilReadMask = 0xff;
1511 depth_stencil.stencilWriteMask = 0xff;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001512
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001513 err = vkCreateDynamicViewportState(demo->device, &viewport_create, &demo->viewport);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001514 assert(!err);
1515
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001516 err = vkCreateDynamicRasterState(demo->device, &raster, &demo->raster);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001517 assert(!err);
1518
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001519 err = vkCreateDynamicColorBlendState(demo->device,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001520 &color_blend, &demo->color_blend);
1521 assert(!err);
1522
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001523 err = vkCreateDynamicDepthStencilState(demo->device,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001524 &depth_stencil, &demo->depth_stencil);
1525 assert(!err);
1526}
1527
Chia-I Wu8d24b3b2015-03-26 13:14:16 +08001528static void demo_prepare_descriptor_pool(struct demo *demo)
Chia-I Wuf8385062015-01-04 16:27:24 +08001529{
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001530 const VkDescriptorTypeCount type_counts[2] = {
Chia-I Wuf8385062015-01-04 16:27:24 +08001531 [0] = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001532 .type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
Chia-I Wuf8385062015-01-04 16:27:24 +08001533 .count = 1,
1534 },
1535 [1] = {
Courtney Goeltzenleuchterad870812015-04-15 15:29:59 -06001536 .type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
Chia-I Wuf8385062015-01-04 16:27:24 +08001537 .count = DEMO_TEXTURE_COUNT,
1538 },
1539 };
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001540 const VkDescriptorPoolCreateInfo descriptor_pool = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001541 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO,
Chia-I Wuf8385062015-01-04 16:27:24 +08001542 .pNext = NULL,
1543 .count = 2,
1544 .pTypeCount = type_counts,
1545 };
Tony Barbour22a30862015-04-22 09:02:32 -06001546 VkResult U_ASSERT_ONLY err;
Chia-I Wuf8385062015-01-04 16:27:24 +08001547
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001548 err = vkCreateDescriptorPool(demo->device,
1549 VK_DESCRIPTOR_POOL_USAGE_ONE_SHOT, 1,
Chia-I Wu8d24b3b2015-03-26 13:14:16 +08001550 &descriptor_pool, &demo->desc_pool);
Chia-I Wuf8385062015-01-04 16:27:24 +08001551 assert(!err);
1552}
1553
1554static void demo_prepare_descriptor_set(struct demo *demo)
1555{
Chia-I Wu8cd8ecd2015-05-25 16:27:55 +08001556 VkDescriptorInfo tex_descs[DEMO_TEXTURE_COUNT];
1557 VkWriteDescriptorSet writes[2];
Tony Barbour22a30862015-04-22 09:02:32 -06001558 VkResult U_ASSERT_ONLY err;
Chia-I Wuf8385062015-01-04 16:27:24 +08001559 uint32_t count;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001560 uint32_t i;
Chia-I Wuf8385062015-01-04 16:27:24 +08001561
Mike Stroyan230e6252015-04-17 12:36:38 -06001562 err = vkAllocDescriptorSets(demo->device, demo->desc_pool,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001563 VK_DESCRIPTOR_SET_USAGE_STATIC,
Chia-I Wu6e68a892015-02-23 10:41:08 -07001564 1, &demo->desc_layout,
Chia-I Wuf8385062015-01-04 16:27:24 +08001565 &demo->desc_set, &count);
1566 assert(!err && count == 1);
1567
Chia-I Wu8cd8ecd2015-05-25 16:27:55 +08001568 memset(&tex_descs, 0, sizeof(tex_descs));
1569 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
1570 tex_descs[i].sampler = demo->textures[i].sampler;
1571 tex_descs[i].imageView = demo->textures[i].view;
1572 tex_descs[i].imageLayout = VK_IMAGE_LAYOUT_GENERAL;
1573 }
1574
1575 memset(&writes, 0, sizeof(writes));
1576
1577 writes[0].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
1578 writes[0].destSet = demo->desc_set;
1579 writes[0].count = 1;
1580 writes[0].descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
1581 writes[0].pDescriptors = &demo->uniform_data.desc;
1582
1583 writes[1].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
1584 writes[1].destSet = demo->desc_set;
1585 writes[1].destBinding = 1;
1586 writes[1].count = DEMO_TEXTURE_COUNT;
1587 writes[1].descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
1588 writes[1].pDescriptors = tex_descs;
1589
1590 err = vkUpdateDescriptorSets(demo->device, 2, writes, 0, NULL);
1591 assert(!err);
Chia-I Wuf8385062015-01-04 16:27:24 +08001592}
1593
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001594static void demo_prepare(struct demo *demo)
1595{
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001596 const VkCmdBufferCreateInfo cmd = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001597 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001598 .pNext = NULL,
Courtney Goeltzenleuchterf3168062015-03-05 18:09:39 -07001599 .queueNodeIndex = demo->graphics_queue_node_index,
Chia-I Wu88eaa3b2015-06-26 15:34:39 +08001600 .level = VK_CMD_BUFFER_LEVEL_PRIMARY,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001601 .flags = 0,
1602 };
Tony Barbour22a30862015-04-22 09:02:32 -06001603 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001604
1605 demo_prepare_buffers(demo);
1606 demo_prepare_depth(demo);
1607 demo_prepare_textures(demo);
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -06001608 demo_prepare_cube_data_buffer(demo);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001609
Chia-I Wuf8385062015-01-04 16:27:24 +08001610 demo_prepare_descriptor_layout(demo);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001611 demo_prepare_pipeline(demo);
1612 demo_prepare_dynamic_states(demo);
1613
Courtney Goeltzenleuchterbd3b3aa2015-02-17 09:48:44 -07001614 for (int i = 0; i < DEMO_BUFFER_COUNT; i++) {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001615 err = vkCreateCommandBuffer(demo->device, &cmd, &demo->buffers[i].cmd);
Courtney Goeltzenleuchterbd3b3aa2015-02-17 09:48:44 -07001616 assert(!err);
1617 }
Chia-I Wuf8385062015-01-04 16:27:24 +08001618
Chia-I Wu8d24b3b2015-03-26 13:14:16 +08001619 demo_prepare_descriptor_pool(demo);
Chia-I Wuf8385062015-01-04 16:27:24 +08001620 demo_prepare_descriptor_set(demo);
Courtney Goeltzenleuchterbd3b3aa2015-02-17 09:48:44 -07001621
1622
1623 for (int i = 0; i < DEMO_BUFFER_COUNT; i++) {
1624 demo->current_buffer = i;
1625 demo_draw_build_cmd(demo, demo->buffers[i].cmd);
1626 }
1627
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -06001628 /*
1629 * Prepare functions above may generate pipeline commands
1630 * that need to be flushed before beginning the render loop.
1631 */
1632 demo_flush_init_cmd(demo);
1633
Courtney Goeltzenleuchterbd3b3aa2015-02-17 09:48:44 -07001634 demo->current_buffer = 0;
Jon Ashburn8a399e92015-04-24 09:46:24 -07001635 demo->prepared = true;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001636}
1637
David Pinedoeeca2a22015-06-18 17:03:14 -06001638static void demo_cleanup(struct demo *demo)
1639{
1640 uint32_t i;
1641
1642 demo->prepared = false;
1643
1644 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DESCRIPTOR_SET, demo->desc_set);
1645 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DESCRIPTOR_POOL, demo->desc_pool);
1646
1647 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DYNAMIC_VP_STATE, demo->viewport);
1648 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DYNAMIC_RS_STATE, demo->raster);
1649 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DYNAMIC_CB_STATE, demo->color_blend);
1650 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DYNAMIC_DS_STATE, demo->depth_stencil);
1651
1652 vkDestroyObject(demo->device, VK_OBJECT_TYPE_PIPELINE, demo->pipeline);
1653 vkDestroyObject(demo->device, VK_OBJECT_TYPE_PIPELINE_LAYOUT, demo->pipeline_layout);
1654 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DESCRIPTOR_SET_LAYOUT, demo->desc_layout);
1655
1656 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
1657 vkDestroyObject(demo->device, VK_OBJECT_TYPE_IMAGE_VIEW, demo->textures[i].view);
1658 vkDestroyObject(demo->device, VK_OBJECT_TYPE_IMAGE, demo->textures[i].image);
1659 vkFreeMemory(demo->device, demo->textures[i].mem);
1660 vkDestroyObject(demo->device, VK_OBJECT_TYPE_SAMPLER, demo->textures[i].sampler);
1661 }
1662 demo->fpDestroySwapChainWSI(demo->swap_chain);
1663
1664 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DEPTH_STENCIL_VIEW, demo->depth.view);
1665 vkDestroyObject(demo->device, VK_OBJECT_TYPE_IMAGE, demo->depth.image);
1666 vkFreeMemory(demo->device, demo->depth.mem);
1667
1668 vkDestroyObject(demo->device, VK_OBJECT_TYPE_BUFFER_VIEW, demo->uniform_data.view);
1669 vkDestroyObject(demo->device, VK_OBJECT_TYPE_BUFFER, demo->uniform_data.buf);
1670 vkFreeMemory(demo->device, demo->uniform_data.mem);
1671
1672 for (i = 0; i < DEMO_BUFFER_COUNT; i++) {
1673 vkDestroyObject(demo->device, VK_OBJECT_TYPE_COLOR_ATTACHMENT_VIEW, demo->buffers[i].view);
1674 vkDestroyObject(demo->device, VK_OBJECT_TYPE_COMMAND_BUFFER, demo->buffers[i].cmd);
1675 }
1676
1677 vkDestroyDevice(demo->device);
Tony Barboura65ecc22015-06-30 14:14:19 -06001678 if (demo->validate) {
1679 demo->dbgDestroyMsgCallback(demo->inst, demo->msg_callback);
1680 }
David Pinedoeeca2a22015-06-18 17:03:14 -06001681 vkDestroyInstance(demo->inst);
1682
1683#ifndef _WIN32
1684 xcb_destroy_window(demo->connection, demo->window);
1685 xcb_disconnect(demo->connection);
1686#endif // _WIN32
1687}
1688
1689// On MS-Windows, make this a global, so it's available to WndProc()
1690struct demo demo;
1691
Ian Elliotte14e9f92015-04-16 15:23:05 -06001692#ifdef _WIN32
1693static void demo_run(struct demo *demo)
1694{
Courtney Goeltzenleuchter857542b2015-04-27 14:56:34 -06001695 if (!demo->prepared)
1696 return;
Ian Elliotte14e9f92015-04-16 15:23:05 -06001697 // Wait for work to finish before updating MVP.
1698 vkDeviceWaitIdle(demo->device);
1699 demo_update_data_buffer(demo);
1700
1701 demo_draw(demo);
1702
1703 // Wait for work to finish before updating MVP.
1704 vkDeviceWaitIdle(demo->device);
Ian Elliotte14e9f92015-04-16 15:23:05 -06001705
David Pinedoeeca2a22015-06-18 17:03:14 -06001706 demo->curFrame++;
1707
1708 if (demo->frameCount != INT_MAX && demo->curFrame == demo->frameCount)
1709 {
1710 demo->quit=true;
1711 demo_cleanup(demo);
1712 ExitProcess(0);
1713 }
1714
1715}
Ian Elliotte14e9f92015-04-16 15:23:05 -06001716
1717// MS-Windows event handling function:
1718LRESULT CALLBACK WndProc(HWND hWnd,
1719 UINT uMsg,
1720 WPARAM wParam,
1721 LPARAM lParam)
1722{
Ian Elliotte14e9f92015-04-16 15:23:05 -06001723 switch(uMsg)
1724 {
Ian Elliotte14e9f92015-04-16 15:23:05 -06001725 case WM_CLOSE:
1726 PostQuitMessage(0);
Tony Barbour5685ad72015-04-29 16:19:20 -06001727 break;
Ian Elliotte14e9f92015-04-16 15:23:05 -06001728 case WM_PAINT:
1729 demo_run(&demo);
1730 return 0;
1731 default:
1732 break;
1733 }
1734 return (DefWindowProc(hWnd, uMsg, wParam, lParam));
1735}
1736
1737static void demo_create_window(struct demo *demo)
1738{
1739 WNDCLASSEX win_class;
1740
1741 // Initialize the window class structure:
1742 win_class.cbSize = sizeof(WNDCLASSEX);
1743 win_class.style = CS_HREDRAW | CS_VREDRAW;
1744 win_class.lpfnWndProc = WndProc;
1745 win_class.cbClsExtra = 0;
1746 win_class.cbWndExtra = 0;
1747 win_class.hInstance = demo->connection; // hInstance
1748 win_class.hIcon = LoadIcon(NULL, IDI_APPLICATION);
1749 win_class.hCursor = LoadCursor(NULL, IDC_ARROW);
1750 win_class.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
1751 win_class.lpszMenuName = NULL;
1752 win_class.lpszClassName = demo->name;
1753 win_class.hIconSm = LoadIcon(NULL, IDI_WINLOGO);
1754 // Register window class:
1755 if (!RegisterClassEx(&win_class)) {
1756 // It didn't work, so try to give a useful error:
1757 printf("Unexpected error trying to start the application!\n");
1758 fflush(stdout);
1759 exit(1);
1760 }
1761 // Create window with the registered class:
Mike Stroyanf5856292015-06-15 14:20:13 -06001762 RECT wr = { 0, 0, demo->width, demo->height };
1763 AdjustWindowRect(&wr, WS_OVERLAPPEDWINDOW, FALSE);
Ian Elliotte14e9f92015-04-16 15:23:05 -06001764 demo->window = CreateWindowEx(0,
1765 demo->name, // class name
1766 demo->name, // app name
1767 WS_OVERLAPPEDWINDOW | // window style
1768 WS_VISIBLE |
1769 WS_SYSMENU,
1770 100,100, // x/y coords
Mike Stroyanf5856292015-06-15 14:20:13 -06001771 wr.right-wr.left, // width
1772 wr.bottom-wr.top, // height
Ian Elliotte14e9f92015-04-16 15:23:05 -06001773 NULL, // handle to parent
1774 NULL, // handle to menu
1775 demo->connection, // hInstance
1776 NULL); // no extra parameters
1777 if (!demo->window) {
1778 // It didn't work, so try to give a useful error:
1779 printf("Cannot create a window in which to draw!\n");
1780 fflush(stdout);
1781 exit(1);
1782 }
1783}
1784#else // _WIN32
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001785static void demo_handle_event(struct demo *demo,
1786 const xcb_generic_event_t *event)
1787{
Piers Daniell886be472015-02-23 16:23:13 -07001788 uint8_t event_code = event->response_type & 0x7f;
Courtney Goeltzenleuchterca21a212014-11-06 14:27:52 -07001789 switch (event_code) {
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001790 case XCB_EXPOSE:
Courtney Goeltzenleuchter54611482014-11-18 11:28:09 -07001791 // TODO: Resize window
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001792 break;
Courtney Goeltzenleuchterca21a212014-11-06 14:27:52 -07001793 case XCB_CLIENT_MESSAGE:
1794 if((*(xcb_client_message_event_t*)event).data.data32[0] ==
1795 (*demo->atom_wm_delete_window).atom) {
1796 demo->quit = true;
1797 }
1798 break;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001799 case XCB_KEY_RELEASE:
1800 {
1801 const xcb_key_release_event_t *key =
1802 (const xcb_key_release_event_t *) event;
1803
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -06001804 switch (key->detail) {
1805 case 0x9: // Escape
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001806 demo->quit = true;
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -06001807 break;
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001808 case 0x71: // left arrow key
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -06001809 demo->spin_angle += demo->spin_increment;
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001810 break;
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -06001811 case 0x72: // right arrow key
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -06001812 demo->spin_angle -= demo->spin_increment;
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -06001813 break;
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -06001814 case 0x41:
1815 demo->pause = !demo->pause;
Piers Daniell886be472015-02-23 16:23:13 -07001816 break;
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -06001817 }
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001818 }
1819 break;
1820 default:
1821 break;
1822 }
1823}
1824
1825static void demo_run(struct demo *demo)
1826{
1827 xcb_flush(demo->connection);
1828
1829 while (!demo->quit) {
1830 xcb_generic_event_t *event;
1831
Courtney Goeltzenleuchter54611482014-11-18 11:28:09 -07001832 if (demo->pause) {
1833 event = xcb_wait_for_event(demo->connection);
1834 } else {
1835 event = xcb_poll_for_event(demo->connection);
1836 }
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001837 if (event) {
1838 demo_handle_event(demo, event);
1839 free(event);
Courtney Goeltzenleuchterbb3e1312014-11-10 11:13:13 -07001840 }
Courtney Goeltzenleuchter54611482014-11-18 11:28:09 -07001841
1842 // Wait for work to finish before updating MVP.
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001843 vkDeviceWaitIdle(demo->device);
Courtney Goeltzenleuchter54611482014-11-18 11:28:09 -07001844 demo_update_data_buffer(demo);
1845
Courtney Goeltzenleuchterbb3e1312014-11-10 11:13:13 -07001846 demo_draw(demo);
Courtney Goeltzenleuchter21f89972014-11-18 11:28:09 -07001847
Courtney Goeltzenleuchterbb3e1312014-11-10 11:13:13 -07001848 // Wait for work to finish before updating MVP.
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001849 vkDeviceWaitIdle(demo->device);
David Pinedoeeca2a22015-06-18 17:03:14 -06001850 demo->curFrame++;
1851 if (demo->frameCount != INT_MAX && demo->curFrame == demo->frameCount)
1852 demo->quit = true;
1853
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001854 }
1855}
1856
1857static void demo_create_window(struct demo *demo)
1858{
1859 uint32_t value_mask, value_list[32];
1860
1861 demo->window = xcb_generate_id(demo->connection);
1862
1863 value_mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK;
1864 value_list[0] = demo->screen->black_pixel;
1865 value_list[1] = XCB_EVENT_MASK_KEY_RELEASE |
1866 XCB_EVENT_MASK_EXPOSURE;
1867
1868 xcb_create_window(demo->connection,
1869 XCB_COPY_FROM_PARENT,
1870 demo->window, demo->screen->root,
1871 0, 0, demo->width, demo->height, 0,
1872 XCB_WINDOW_CLASS_INPUT_OUTPUT,
1873 demo->screen->root_visual,
1874 value_mask, value_list);
1875
Courtney Goeltzenleuchterca21a212014-11-06 14:27:52 -07001876 /* Magic code that will send notification when window is destroyed */
1877 xcb_intern_atom_cookie_t cookie = xcb_intern_atom(demo->connection, 1, 12,
1878 "WM_PROTOCOLS");
1879 xcb_intern_atom_reply_t* reply = xcb_intern_atom_reply(demo->connection, cookie, 0);
1880
1881 xcb_intern_atom_cookie_t cookie2 = xcb_intern_atom(demo->connection, 0, 16, "WM_DELETE_WINDOW");
1882 demo->atom_wm_delete_window = xcb_intern_atom_reply(demo->connection, cookie2, 0);
1883
1884 xcb_change_property(demo->connection, XCB_PROP_MODE_REPLACE,
1885 demo->window, (*reply).atom, 4, 32, 1,
1886 &(*demo->atom_wm_delete_window).atom);
1887 free(reply);
1888
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001889 xcb_map_window(demo->connection, demo->window);
David Pinedoeeca2a22015-06-18 17:03:14 -06001890
1891 // Force the x/y coordinates to 100,100 results are identical in consecutive runs
1892 const uint32_t coords[] = {100, 100};
1893 xcb_configure_window(demo->connection, demo->window,
1894 XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y, coords);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001895}
Ian Elliotte14e9f92015-04-16 15:23:05 -06001896#endif // _WIN32
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001897
Courtney Goeltzenleuchter8c15fc52015-07-06 17:46:11 -06001898/*
1899 * Return 1 (true) if all layer names specified in check_names
1900 * can be found in given layer properties.
1901 */
1902static bool32_t demo_check_layers(uint32_t check_count, char **check_names,
1903 uint32_t layer_count, VkLayerProperties *layers)
1904{
1905 for (uint32_t i = 0; i < check_count; i++) {
1906 bool32_t found = 0;
1907 for (uint32_t j = 0; j < layer_count; j++) {
1908 if (!strcmp(check_names[i], layers[j].layerName)) {
1909 found = 1;
1910 }
1911 }
1912 if (!found) {
1913 fprintf(stderr, "Cannot find layer: %s\n", check_names[i]);
1914 return 0;
1915 }
1916 }
1917 return 1;
1918}
1919
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001920static void demo_init_vk(struct demo *demo)
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001921{
Tobin Ehlis3536b442015-04-16 18:04:57 -06001922 VkResult err;
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001923 char *extension_names[64];
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -06001924 VkExtensionProperties *instance_extensions;
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001925 VkLayerProperties *instance_layers;
1926 VkLayerProperties *device_layers;
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -06001927 uint32_t instance_extension_count = 0;
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001928 uint32_t instance_layer_count = 0;
1929 uint32_t enabled_extension_count = 0;
1930 uint32_t enabled_layer_count = 0;
1931
Courtney Goeltzenleuchter8c15fc52015-07-06 17:46:11 -06001932 char *instance_validation_layers[] = {
1933 "MemTracker",
1934 };
1935
1936 char *device_validation_layers[] = {
1937 "MemTracker",
1938 };
1939
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001940 /* Look for validation layers */
1941 bool32_t validation_found = 0;
1942 err = vkGetGlobalLayerProperties(&instance_layer_count, NULL);
Tobin Ehlis3536b442015-04-16 18:04:57 -06001943 assert(!err);
1944
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001945 instance_layers = malloc(sizeof(VkLayerProperties) * instance_layer_count);
1946 err = vkGetGlobalLayerProperties(&instance_layer_count, instance_layers);
1947 assert(!err);
Courtney Goeltzenleuchter8c15fc52015-07-06 17:46:11 -06001948
1949 if (demo->validate) {
1950 validation_found = demo_check_layers(ARRAY_SIZE(instance_validation_layers), instance_validation_layers,
1951 instance_layer_count, instance_layers);
1952 if (!validation_found) {
1953 ERR_EXIT("vkGetGlobalLayerProperties failed to find"
1954 "required validation layer.\n\n"
1955 "Please look at the Getting Started guide for additional "
1956 "information.\n",
1957 "vkCreateInstance Failure");
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001958 }
Courtney Goeltzenleuchter8c15fc52015-07-06 17:46:11 -06001959 enabled_layer_count = ARRAY_SIZE(instance_validation_layers);
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001960 }
1961
1962 err = vkGetGlobalExtensionProperties(NULL, &instance_extension_count, NULL);
1963 assert(!err);
1964
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -06001965 bool32_t WSIextFound = 0;
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001966 memset(extension_names, 0, sizeof(extension_names));
1967 instance_extensions = malloc(sizeof(VkExtensionProperties) * instance_extension_count);
1968 err = vkGetGlobalExtensionProperties(NULL, &instance_extension_count, instance_extensions);
1969 assert(!err);
1970 for (uint32_t i = 0; i < instance_extension_count; i++) {
1971 if (!strcmp(VK_WSI_LUNARG_EXTENSION_NAME, instance_extensions[i].extName)) {
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -06001972 WSIextFound = 1;
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001973 extension_names[enabled_extension_count++] = VK_WSI_LUNARG_EXTENSION_NAME;
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -06001974 }
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001975 if (!strcmp(DEBUG_REPORT_EXTENSION_NAME, instance_extensions[i].extName)) {
1976 if (demo->validate) {
1977 extension_names[enabled_extension_count++] = DEBUG_REPORT_EXTENSION_NAME;
1978 }
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -06001979 }
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001980 assert(enabled_extension_count < 64);
Tobin Ehlis3536b442015-04-16 18:04:57 -06001981 }
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -06001982 if (!WSIextFound) {
Tony Barbour426b9052015-06-24 16:06:58 -06001983 ERR_EXIT("vkGetGlobalExtensionProperties failed to find the "
Ian Elliott3b375cf2015-04-28 13:22:33 -06001984 "\"VK_WSI_LunarG\" extension.\n\nDo you have a compatible "
1985 "Vulkan installable client driver (ICD) installed?\nPlease "
1986 "look at the Getting Started guide for additional "
1987 "information.\n",
1988 "vkCreateInstance Failure");
1989 }
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001990 const VkApplicationInfo app = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001991 .sType = VK_STRUCTURE_TYPE_APPLICATION_INFO,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001992 .pNext = NULL,
Ian Elliott4e19ed02015-04-28 10:52:52 -06001993 .pAppName = APP_SHORT_NAME,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001994 .appVersion = 0,
Ian Elliott4e19ed02015-04-28 10:52:52 -06001995 .pEngineName = APP_SHORT_NAME,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001996 .engineVersion = 0,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001997 .apiVersion = VK_API_VERSION,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001998 };
Tony Barbour5685ad72015-04-29 16:19:20 -06001999 VkInstanceCreateInfo inst_info = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06002000 .sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
Jon Ashburn29669a42015-04-04 14:52:07 -06002001 .pNext = NULL,
2002 .pAppInfo = &app,
2003 .pAllocCb = NULL,
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002004 .layerCount = enabled_layer_count,
Courtney Goeltzenleuchter8c15fc52015-07-06 17:46:11 -06002005 .ppEnabledLayerNames = (const char *const*) ((demo->validate) ? instance_validation_layers : NULL),
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002006 .extensionCount = enabled_extension_count,
2007 .ppEnabledExtensionNames = (const char *const*) extension_names,
Jon Ashburn29669a42015-04-04 14:52:07 -06002008 };
Courtney Goeltzenleuchterddcb6192015-04-14 18:48:46 -06002009 const VkDeviceQueueCreateInfo queue = {
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002010 .queueNodeIndex = 0,
2011 .queueCount = 1,
2012 };
Ian Elliottff6dab52015-04-28 11:35:02 -06002013
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06002014 uint32_t gpu_count;
2015 uint32_t i;
Courtney Goeltzenleuchterf3168062015-03-05 18:09:39 -07002016 uint32_t queue_count;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002017
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06002018 err = vkCreateInstance(&inst_info, &demo->inst);
Ian Elliottcaa9f272015-04-28 11:35:02 -06002019 if (err == VK_ERROR_INCOMPATIBLE_DRIVER) {
2020 ERR_EXIT("Cannot find a compatible Vulkan installable client driver "
Ian Elliott3b375cf2015-04-28 13:22:33 -06002021 "(ICD).\n\nPlease look at the Getting Started guide for "
Ian Elliottcaa9f272015-04-28 11:35:02 -06002022 "additional information.\n",
2023 "vkCreateInstance Failure");
Tony Barbour5685ad72015-04-29 16:19:20 -06002024 } else if (err == VK_ERROR_INVALID_EXTENSION) {
2025 ERR_EXIT("Cannot find a specified extension library"
2026 ".\nMake sure your layers path is set appropriately\n",
2027 "vkCreateInstance Failure");
Ian Elliottcaa9f272015-04-28 11:35:02 -06002028 } else if (err) {
Ian Elliott3b375cf2015-04-28 13:22:33 -06002029 ERR_EXIT("vkCreateInstance failed.\n\nDo you have a compatible Vulkan "
2030 "installable client driver (ICD) installed?\nPlease look at "
Ian Elliottcaa9f272015-04-28 11:35:02 -06002031 "the Getting Started guide for additional information.\n",
2032 "vkCreateInstance Failure");
Ian Elliottdfe55f72015-04-03 15:24:55 -06002033 }
Jon Ashburn29669a42015-04-04 14:52:07 -06002034
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002035 free(instance_layers);
2036 free(instance_extensions);
Tony Barbour5685ad72015-04-29 16:19:20 -06002037
Jon Ashburn07b309a2015-04-15 11:31:12 -06002038 gpu_count = 1;
2039 err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, &demo->gpu);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002040 assert(!err && gpu_count == 1);
2041
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002042 /* Look for validation layers */
2043 validation_found = 0;
2044 enabled_layer_count = 0;
2045 uint32_t device_layer_count = 0;
2046 err = vkGetPhysicalDeviceLayerProperties(demo->gpu, &device_layer_count, NULL);
2047 assert(!err);
2048
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002049 device_layers = malloc(sizeof(VkLayerProperties) * device_layer_count);
2050 err = vkGetPhysicalDeviceLayerProperties(demo->gpu, &device_layer_count, device_layers);
2051 assert(!err);
Courtney Goeltzenleuchter8c15fc52015-07-06 17:46:11 -06002052
2053 if (demo->validate) {
2054 validation_found = demo_check_layers(ARRAY_SIZE(device_validation_layers), device_validation_layers,
2055 device_layer_count, device_layers);
2056 if (!validation_found) {
2057 ERR_EXIT("vkGetPhysicalDeviceLayerProperties failed to find"
2058 "a required validation layer.\n\n"
2059 "Please look at the Getting Started guide for additional "
2060 "information.\n",
2061 "vkCreateDevice Failure");
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002062 }
Courtney Goeltzenleuchter8c15fc52015-07-06 17:46:11 -06002063 enabled_layer_count = ARRAY_SIZE(device_validation_layers);
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002064 }
2065
2066 uint32_t device_extension_count = 0;
2067 VkExtensionProperties *device_extensions = NULL;
2068 err = vkGetPhysicalDeviceExtensionProperties(
2069 demo->gpu, NULL, &device_extension_count, NULL);
2070 assert(!err);
2071
2072 WSIextFound = 0;
2073 enabled_extension_count = 0;
2074 memset(extension_names, 0, sizeof(extension_names));
2075 device_extensions = malloc(sizeof(VkExtensionProperties) * device_extension_count);
2076 err = vkGetPhysicalDeviceExtensionProperties(
2077 demo->gpu, NULL, &device_extension_count, device_extensions);
2078 assert(!err);
Courtney Goeltzenleuchter37d091e2015-07-05 22:08:51 -06002079#if 0
2080 /* Will need this check in future */
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002081 for (uint32_t i = 0; i < device_extension_count; i++) {
2082 if (!strcmp(VK_WSI_LUNARG_EXTENSION_NAME, device_extensions[i].extName)) {
2083 WSIextFound = 1;
2084 extension_names[enabled_extension_count++] = VK_WSI_LUNARG_EXTENSION_NAME;
2085 }
2086 assert(enabled_extension_count < 64);
2087 }
2088 if (!WSIextFound) {
2089 ERR_EXIT("vkGetGlobalExtensionProperties failed to find the "
2090 "\"VK_WSI_LunarG\" extension.\n\nDo you have a compatible "
2091 "Vulkan installable client driver (ICD) installed?\nPlease "
2092 "look at the Getting Started guide for additional "
2093 "information.\n",
2094 "vkCreateInstance Failure");
2095 }
Courtney Goeltzenleuchter37d091e2015-07-05 22:08:51 -06002096#endif
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002097
2098 VkDeviceCreateInfo device = {
2099 .sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
2100 .pNext = NULL,
2101 .queueRecordCount = 1,
2102 .pRequestedQueues = &queue,
2103 .layerCount = enabled_layer_count,
Courtney Goeltzenleuchter8c15fc52015-07-06 17:46:11 -06002104 .ppEnabledLayerNames = (const char *const*) ((demo->validate) ? device_validation_layers : NULL),
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002105 .extensionCount = enabled_extension_count,
2106 .ppEnabledExtensionNames = (const char *const*) extension_names,
2107 .flags = 0,
2108 };
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -06002109
Tony Barbour5685ad72015-04-29 16:19:20 -06002110 if (demo->validate) {
Tony Barboura65ecc22015-06-30 14:14:19 -06002111 demo->dbgCreateMsgCallback = vkGetInstanceProcAddr(demo->inst, "vkDbgCreateMsgCallback");
2112 demo->dbgDestroyMsgCallback = vkGetInstanceProcAddr(demo->inst, "vkDbgDestroyMsgCallback");
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -06002113 if (!demo->dbgCreateMsgCallback) {
2114 ERR_EXIT("GetProcAddr: Unable to find vkDbgCreateMsgCallback\n",
2115 "vkGetProcAddr Failure");
2116 }
Tony Barboura65ecc22015-06-30 14:14:19 -06002117 if (!demo->dbgDestroyMsgCallback) {
2118 ERR_EXIT("GetProcAddr: Unable to find vkDbgDestroyMsgCallback\n",
2119 "vkGetProcAddr Failure");
2120 }
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -06002121 err = demo->dbgCreateMsgCallback(
2122 demo->inst,
2123 VK_DBG_REPORT_ERROR_BIT | VK_DBG_REPORT_WARN_BIT,
2124 dbgFunc, NULL,
2125 &demo->msg_callback);
2126 switch (err) {
2127 case VK_SUCCESS:
2128 break;
2129 case VK_ERROR_INVALID_POINTER:
2130 ERR_EXIT("dbgCreateMsgCallback: Invalid pointer\n",
2131 "dbgCreateMsgCallback Failure");
2132 break;
2133 case VK_ERROR_OUT_OF_HOST_MEMORY:
2134 ERR_EXIT("dbgCreateMsgCallback: out of host memory\n",
2135 "dbgCreateMsgCallback Failure");
2136 break;
2137 default:
2138 ERR_EXIT("dbgCreateMsgCallback: unknown failure\n",
2139 "dbgCreateMsgCallback Failure");
2140 break;
2141 }
Tony Barbour5685ad72015-04-29 16:19:20 -06002142 }
2143
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06002144 err = vkCreateDevice(demo->gpu, &device, &demo->device);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002145 assert(!err);
2146
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002147 free(device_layers);
2148
Ian Elliott1b6de092015-06-22 15:07:49 -06002149 GET_DEVICE_PROC_ADDR(demo->device, CreateSwapChainWSI);
2150 GET_DEVICE_PROC_ADDR(demo->device, CreateSwapChainWSI);
2151 GET_DEVICE_PROC_ADDR(demo->device, DestroySwapChainWSI);
2152 GET_DEVICE_PROC_ADDR(demo->device, GetSwapChainInfoWSI);
2153 GET_DEVICE_PROC_ADDR(demo->device, QueuePresentWSI);
Jon Ashburncedc15f2015-05-21 18:13:33 -06002154
Tony Barbour426b9052015-06-24 16:06:58 -06002155 err = vkGetPhysicalDeviceProperties(demo->gpu, &demo->gpu_props);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -06002156 assert(!err);
2157
Tony Barbour426b9052015-06-24 16:06:58 -06002158 err = vkGetPhysicalDeviceQueueCount(demo->gpu, &queue_count);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -06002159 assert(!err);
2160
Tony Barbour426b9052015-06-24 16:06:58 -06002161 demo->queue_props = (VkPhysicalDeviceQueueProperties *) malloc(queue_count * sizeof(VkPhysicalDeviceQueueProperties));
2162 err = vkGetPhysicalDeviceQueueProperties(demo->gpu, queue_count, demo->queue_props);
Courtney Goeltzenleuchterf3168062015-03-05 18:09:39 -07002163 assert(!err);
Courtney Goeltzenleuchterf3168062015-03-05 18:09:39 -07002164 assert(queue_count >= 1);
2165
Mark Lobodzinskicf26e072015-04-16 11:44:05 -05002166 // Graphics queue and MemMgr queue can be separate.
2167 // TODO: Add support for separate queues, including synchronization,
Mark Lobodzinskifb9f5642015-05-11 17:21:15 -05002168 // and appropriate tracking for QueueSubmit
Courtney Goeltzenleuchterf3168062015-03-05 18:09:39 -07002169 for (i = 0; i < queue_count; i++) {
Mark Lobodzinskifb9f5642015-05-11 17:21:15 -05002170 if (demo->queue_props[i].queueFlags & VK_QUEUE_GRAPHICS_BIT)
Courtney Goeltzenleuchterf3168062015-03-05 18:09:39 -07002171 break;
2172 }
2173 assert(i < queue_count);
2174 demo->graphics_queue_node_index = i;
2175
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06002176 err = vkGetDeviceQueue(demo->device, demo->graphics_queue_node_index,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002177 0, &demo->queue);
2178 assert(!err);
Tony Barbour5685ad72015-04-29 16:19:20 -06002179
Jon Ashburnba4a1952015-06-16 12:44:51 -06002180 // for now hardcode format till get WSI support
2181 demo->format = VK_FORMAT_B8G8R8A8_UNORM;
Ian Elliott32536f92015-04-21 16:41:02 -06002182
David Pinedoeeca2a22015-06-18 17:03:14 -06002183 demo->quit = false;
2184 demo->curFrame = 0;
Mark Lobodzinski72346292015-07-02 16:49:40 -06002185
2186 // Get Memory information and properties
2187 err = vkGetPhysicalDeviceMemoryProperties(demo->gpu, &demo->memory_properties);
2188 assert(!err);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002189}
2190
2191static void demo_init_connection(struct demo *demo)
2192{
Ian Elliotte14e9f92015-04-16 15:23:05 -06002193#ifndef _WIN32
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002194 const xcb_setup_t *setup;
2195 xcb_screen_iterator_t iter;
2196 int scr;
2197
2198 demo->connection = xcb_connect(NULL, &scr);
Ian Elliottdfe55f72015-04-03 15:24:55 -06002199 if (demo->connection == NULL) {
2200 printf("Cannot find a compatible Vulkan installable client driver "
2201 "(ICD).\nExiting ...\n");
2202 fflush(stdout);
2203 exit(1);
2204 }
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002205
2206 setup = xcb_get_setup(demo->connection);
2207 iter = xcb_setup_roots_iterator(setup);
2208 while (scr-- > 0)
2209 xcb_screen_next(&iter);
2210
2211 demo->screen = iter.data;
Ian Elliotte14e9f92015-04-16 15:23:05 -06002212#endif // _WIN32
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002213}
2214
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07002215static void demo_init(struct demo *demo, int argc, char **argv)
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002216{
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -06002217 vec3 eye = {0.0f, 3.0f, 5.0f};
2218 vec3 origin = {0, 0, 0};
Chia-I Wuc3487c22015-04-22 14:56:17 +08002219 vec3 up = {0.0f, 1.0f, 0.0};
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -06002220
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002221 memset(demo, 0, sizeof(*demo));
David Pinedoeeca2a22015-06-18 17:03:14 -06002222 demo->frameCount = INT_MAX;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002223
Piers Daniell886be472015-02-23 16:23:13 -07002224 for (int i = 1; i < argc; i++) {
Tony Barbour5685ad72015-04-29 16:19:20 -06002225 if (strcmp(argv[i], "--use_staging") == 0) {
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07002226 demo->use_staging_buffer = true;
Tony Barbour5685ad72015-04-29 16:19:20 -06002227 continue;
Ian Elliotte14e9f92015-04-16 15:23:05 -06002228 }
Cody Northrop75db0322015-05-28 11:27:16 -06002229 if (strcmp(argv[i], "--use_glsl") == 0) {
2230 demo->use_glsl = true;
2231 continue;
2232 }
Tony Barbour5685ad72015-04-29 16:19:20 -06002233 if (strcmp(argv[i], "--validate") == 0) {
2234 demo->validate = true;
2235 continue;
2236 }
David Pinedoeeca2a22015-06-18 17:03:14 -06002237 if (strcmp(argv[i], "--c") == 0 &&
2238 demo->frameCount == INT_MAX &&
2239 i < argc-1 &&
2240 sscanf(argv[i+1],"%d", &demo->frameCount) == 1 &&
2241 demo->frameCount >= 0)
2242 {
2243 i++;
2244 continue;
2245 }
Tony Barbour5685ad72015-04-29 16:19:20 -06002246
David Pinedoeeca2a22015-06-18 17:03:14 -06002247 fprintf(stderr, "Usage:\n %s [--use_staging] [--validate] [--c <framecount>]\n", APP_SHORT_NAME);
Ian Elliotte14e9f92015-04-16 15:23:05 -06002248 fflush(stderr);
2249 exit(1);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07002250 }
2251
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002252 demo_init_connection(demo);
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06002253 demo_init_vk(demo);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002254
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -06002255 demo->width = 500;
2256 demo->height = 500;
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -06002257
2258 demo->spin_angle = 0.01f;
2259 demo->spin_increment = 0.01f;
2260 demo->pause = false;
2261
Piers Daniell886be472015-02-23 16:23:13 -07002262 mat4x4_perspective(demo->projection_matrix, (float)degreesToRadians(45.0f), 1.0f, 0.1f, 100.0f);
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -06002263 mat4x4_look_at(demo->view_matrix, eye, origin, up);
2264 mat4x4_identity(demo->model_matrix);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002265}
2266
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002267
Ian Elliotte14e9f92015-04-16 15:23:05 -06002268#ifdef _WIN32
Tony Barbour5685ad72015-04-29 16:19:20 -06002269extern int __getmainargs(
2270 int * _Argc,
2271 char *** _Argv,
2272 char *** _Env,
2273 int _DoWildCard,
2274 int * new_mode);
Ian Elliott7595eee2015-04-28 10:33:11 -06002275
Ian Elliott421107f2015-04-28 15:50:36 -06002276int WINAPI WinMain(HINSTANCE hInstance,
2277 HINSTANCE hPrevInstance,
2278 LPSTR pCmdLine,
2279 int nCmdShow)
Ian Elliotte14e9f92015-04-16 15:23:05 -06002280{
2281 MSG msg; // message
2282 bool done; // flag saying when app is complete
Tony Barbour5685ad72015-04-29 16:19:20 -06002283 int argc;
2284 char** argv;
2285 char** env;
2286 int new_mode = 0;
Ian Elliotte14e9f92015-04-16 15:23:05 -06002287
Tony Barbour5685ad72015-04-29 16:19:20 -06002288 __getmainargs(&argc,&argv,&env,0,&new_mode);
2289
2290 demo_init(&demo, argc, argv);
2291 demo.connection = hInstance;
2292 strncpy(demo.name, "cube", APP_NAME_STR_LEN);
Ian Elliotte14e9f92015-04-16 15:23:05 -06002293 demo_create_window(&demo);
2294
2295 demo_prepare(&demo);
2296
2297 done = false; //initialize loop condition variable
2298 /* main message loop*/
2299 while(!done)
2300 {
Ian Elliott421107f2015-04-28 15:50:36 -06002301 PeekMessage(&msg, NULL, 0, 0, PM_REMOVE);
Ian Elliotte14e9f92015-04-16 15:23:05 -06002302 if (msg.message == WM_QUIT) //check for a quit message
2303 {
2304 done = true; //if found, quit app
2305 }
2306 else
2307 {
2308 /* Translate and dispatch to event queue*/
2309 TranslateMessage(&msg);
2310 DispatchMessage(&msg);
2311 }
2312 }
2313
2314 demo_cleanup(&demo);
2315
Tony Barboura938abb2015-04-22 11:36:22 -06002316 return (int) msg.wParam;
Ian Elliotte14e9f92015-04-16 15:23:05 -06002317}
2318#else // _WIN32
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07002319int main(int argc, char **argv)
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002320{
2321 struct demo demo;
2322
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07002323 demo_init(&demo, argc, argv);
Chia-I Wu5b66aa52015-04-16 22:02:10 +08002324 demo_create_window(&demo);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002325
2326 demo_prepare(&demo);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002327 demo_run(&demo);
2328
2329 demo_cleanup(&demo);
2330
2331 return 0;
2332}
Ian Elliotte14e9f92015-04-16 15:23:05 -06002333#endif // _WIN32