blob: ce36b06277dff11ca0db24e9f3641ae6493a5610 [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(
Tony Barbourde4124d2015-07-03 10:33:54 -0600251 VkFlags msgFlags,
252 VkDbgObjectType objType,
253 uint64_t srcObject,
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -0600254 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
Chia-I Wuc278df82015-07-07 11:50:03 +0800324 VkAttachmentView 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;
Chia-I Wuc278df82015-07-07 11:50:03 +0800332 VkAttachmentView 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;
Jon Ashburn0d60d272015-07-09 15:02:25 -0600347 VkPipelineCache pipelineCache;
Chia-I Wu76cd4222015-07-08 13:34:24 +0800348 VkRenderPass render_pass;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600349 VkPipeline pipeline;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600350
Tony Barbourde4124d2015-07-03 10:33:54 -0600351 VkDynamicViewportState viewport;
352 VkDynamicRasterState raster;
353 VkDynamicColorBlendState color_blend;
354 VkDynamicDepthStencilState depth_stencil;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600355
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -0600356 mat4x4 projection_matrix;
357 mat4x4 view_matrix;
358 mat4x4 model_matrix;
359
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600360 float spin_angle;
361 float spin_increment;
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -0600362 bool pause;
363
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600364 VkDescriptorPool desc_pool;
365 VkDescriptorSet desc_set;
Chia-I Wuf8385062015-01-04 16:27:24 +0800366
Chia-I Wu76cd4222015-07-08 13:34:24 +0800367 VkFramebuffer framebuffers[DEMO_BUFFER_COUNT];
368
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600369 bool quit;
David Pinedoeeca2a22015-06-18 17:03:14 -0600370 int32_t curFrame;
371 int32_t frameCount;
Tony Barbour5685ad72015-04-29 16:19:20 -0600372 bool validate;
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -0600373 PFN_vkDbgCreateMsgCallback dbgCreateMsgCallback;
Tony Barboura65ecc22015-06-30 14:14:19 -0600374 PFN_vkDbgDestroyMsgCallback dbgDestroyMsgCallback;
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -0600375 VkDbgMsgCallback msg_callback;
376
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600377 uint32_t current_buffer;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600378};
379
Mark Lobodzinski72346292015-07-02 16:49:40 -0600380static VkResult memory_type_from_properties(struct demo *demo, uint32_t typeBits, VkFlags properties, uint32_t *typeIndex)
381{
382 // Search memtypes to find first index with those properties
383 for (uint32_t i = 0; i < 32; i++) {
384 if ((typeBits & 1) == 1) {
385 // Type is available, does it match user properties?
386 if ((demo->memory_properties.memoryTypes[i].propertyFlags & properties) == properties) {
387 *typeIndex = i;
388 return VK_SUCCESS;
389 }
390 }
391 typeBits >>= 1;
392 }
393 // No memory types matched, return failure
394 return VK_UNSUPPORTED;
395}
396
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600397static void demo_flush_init_cmd(struct demo *demo)
398{
Tony Barbour22a30862015-04-22 09:02:32 -0600399 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600400
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600401 if (demo->cmd == VK_NULL_HANDLE)
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600402 return;
403
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600404 err = vkEndCommandBuffer(demo->cmd);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600405 assert(!err);
406
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600407 const VkCmdBuffer cmd_bufs[] = { demo->cmd };
Tony Barbourde4124d2015-07-03 10:33:54 -0600408 VkFence nullFence = { VK_NULL_HANDLE };
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600409
Tony Barbourde4124d2015-07-03 10:33:54 -0600410 err = vkQueueSubmit(demo->queue, 1, cmd_bufs, nullFence);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600411 assert(!err);
412
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600413 err = vkQueueWaitIdle(demo->queue);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600414 assert(!err);
415
Tony Barbourde4124d2015-07-03 10:33:54 -0600416 vkDestroyCommandBuffer(demo->device, demo->cmd);
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600417 demo->cmd = VK_NULL_HANDLE;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600418}
419
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600420static void demo_set_image_layout(
421 struct demo *demo,
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600422 VkImage image,
malnasse4b8ba4d2015-06-03 17:28:38 -0400423 VkImageAspect aspect,
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600424 VkImageLayout old_image_layout,
425 VkImageLayout new_image_layout)
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600426{
Tony Barbour22a30862015-04-22 09:02:32 -0600427 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600428
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600429 if (demo->cmd == VK_NULL_HANDLE) {
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600430 const VkCmdBufferCreateInfo cmd = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600431 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600432 .pNext = NULL,
433 .queueNodeIndex = demo->graphics_queue_node_index,
Chia-I Wu88eaa3b2015-06-26 15:34:39 +0800434 .level = VK_CMD_BUFFER_LEVEL_PRIMARY,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600435 .flags = 0,
436 };
437
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600438 err = vkCreateCommandBuffer(demo->device, &cmd, &demo->cmd);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600439 assert(!err);
440
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600441 VkCmdBufferBeginInfo cmd_buf_info = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600442 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600443 .pNext = NULL,
Tony Barbour8205d902015-04-16 15:59:00 -0600444 .flags = VK_CMD_BUFFER_OPTIMIZE_SMALL_BATCH_BIT |
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600445 VK_CMD_BUFFER_OPTIMIZE_ONE_TIME_SUBMIT_BIT,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600446 };
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600447 err = vkBeginCommandBuffer(demo->cmd, &cmd_buf_info);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600448 }
449
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600450 VkImageMemoryBarrier image_memory_barrier = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600451 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600452 .pNext = NULL,
453 .outputMask = 0,
454 .inputMask = 0,
455 .oldLayout = old_image_layout,
456 .newLayout = new_image_layout,
457 .image = image,
malnasse4b8ba4d2015-06-03 17:28:38 -0400458 .subresourceRange = { aspect, 0, 1, 0, 0 }
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600459 };
460
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600461 if (new_image_layout == VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL) {
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600462 /* Make sure anything that was copying from this image has completed */
Courtney Goeltzenleuchterad870812015-04-15 15:29:59 -0600463 image_memory_barrier.inputMask = VK_MEMORY_INPUT_TRANSFER_BIT;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600464 }
465
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600466 if (new_image_layout == VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL) {
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600467 /* Make sure any Copy or CPU writes to image are flushed */
Courtney Goeltzenleuchtera569a502015-04-29 17:16:21 -0600468 image_memory_barrier.outputMask = VK_MEMORY_OUTPUT_HOST_WRITE_BIT | VK_MEMORY_OUTPUT_TRANSFER_BIT;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600469 }
470
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600471 VkImageMemoryBarrier *pmemory_barrier = &image_memory_barrier;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600472
Tony Barbourc2e987e2015-06-29 16:20:35 -0600473 VkPipelineStageFlags src_stages = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
474 VkPipelineStageFlags dest_stages = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600475
Tony Barbourc2e987e2015-06-29 16:20:35 -0600476 vkCmdPipelineBarrier(demo->cmd, src_stages, dest_stages, false, 1, (const void **)&pmemory_barrier);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600477}
478
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600479static void demo_draw_build_cmd(struct demo *demo, VkCmdBuffer cmd_buf)
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600480{
Chia-I Wu76cd4222015-07-08 13:34:24 +0800481 const VkCmdBufferBeginInfo cmd_buf_info = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600482 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO,
Courtney Goeltzenleuchtere3b0f3a2015-04-03 15:25:24 -0600483 .pNext = NULL,
Tony Barbour8205d902015-04-16 15:59:00 -0600484 .flags = VK_CMD_BUFFER_OPTIMIZE_SMALL_BATCH_BIT |
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600485 VK_CMD_BUFFER_OPTIMIZE_ONE_TIME_SUBMIT_BIT,
Jon Ashburn53d27af2014-12-31 17:08:35 -0700486 };
Chia-I Wuc278df82015-07-07 11:50:03 +0800487 const VkClearValue clear_values[2] = {
488 [0] = { .color.f32 = { 0.2f, 0.2f, 0.2f, 0.2f } },
489 [1] = { .ds = { 1.0f, 0 } },
490 };
491 const VkRenderPassBeginInfo rp_begin = {
492 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO,
493 .pNext = NULL,
Chia-I Wu76cd4222015-07-08 13:34:24 +0800494 .renderPass = demo->render_pass,
495 .framebuffer = demo->framebuffers[demo->current_buffer],
Chia-I Wuc278df82015-07-07 11:50:03 +0800496 .renderArea.offset.x = 0,
497 .renderArea.offset.y = 0,
498 .renderArea.extent.width = demo->width,
499 .renderArea.extent.height = demo->height,
500 .attachmentCount = 2,
501 .pAttachmentClearValues = clear_values,
Jon Ashburn3325d6b2015-01-02 18:24:05 -0700502 };
Chia-I Wu76cd4222015-07-08 13:34:24 +0800503 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600504
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600505 err = vkBeginCommandBuffer(cmd_buf, &cmd_buf_info);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600506 assert(!err);
507
Chia-I Wuc278df82015-07-07 11:50:03 +0800508 vkCmdBeginRenderPass(cmd_buf, &rp_begin, VK_RENDER_PASS_CONTENTS_INLINE);
509
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600510 vkCmdBindPipeline(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600511 demo->pipeline);
Mark Lobodzinskia65c4632015-06-15 13:21:21 -0600512 vkCmdBindDescriptorSets(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS, demo->pipeline_layout,
Cody Northrop1a01b1d2015-04-16 13:41:56 -0600513 0, 1, &demo->desc_set, 0, NULL);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600514
Tony Barbourde4124d2015-07-03 10:33:54 -0600515 vkCmdBindDynamicViewportState(cmd_buf, demo->viewport);
516 vkCmdBindDynamicRasterState(cmd_buf, demo->raster);
517 vkCmdBindDynamicColorBlendState(cmd_buf, demo->color_blend);
518 vkCmdBindDynamicDepthStencilState(cmd_buf, demo->depth_stencil);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600519
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600520 vkCmdDraw(cmd_buf, 0, 12 * 3, 0, 1);
Chia-I Wu88eaa3b2015-06-26 15:34:39 +0800521 vkCmdEndRenderPass(cmd_buf);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600522
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600523 err = vkEndCommandBuffer(cmd_buf);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600524 assert(!err);
525}
526
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -0600527
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -0600528void demo_update_data_buffer(struct demo *demo)
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600529{
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -0600530 mat4x4 MVP, Model, VP;
531 int matrixSize = sizeof(MVP);
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600532 uint8_t *pData;
Tony Barbour22a30862015-04-22 09:02:32 -0600533 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600534
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -0600535 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600536
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -0600537 // Rotate 22.5 degrees around the Y axis
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -0600538 mat4x4_dup(Model, demo->model_matrix);
Piers Daniell886be472015-02-23 16:23:13 -0700539 mat4x4_rotate(demo->model_matrix, Model, 0.0f, 1.0f, 0.0f, (float)degreesToRadians(demo->spin_angle));
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -0600540 mat4x4_mul(MVP, VP, demo->model_matrix);
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -0600541
Mark Lobodzinski23182612015-05-29 09:32:35 -0500542 err = vkMapMemory(demo->device, demo->uniform_data.mem, 0, 0, 0, (void **) &pData);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600543 assert(!err);
544
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -0600545 memcpy(pData, (const void*) &MVP[0][0], matrixSize);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600546
Mark Lobodzinski23182612015-05-29 09:32:35 -0500547 err = vkUnmapMemory(demo->device, demo->uniform_data.mem);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600548 assert(!err);
549}
550
551static void demo_draw(struct demo *demo)
552{
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800553 const VkPresentInfoWSI present = {
554 .sType = VK_STRUCTURE_TYPE_PRESENT_INFO_WSI,
555 .pNext = NULL,
556 .image = demo->buffers[demo->current_buffer].image,
557 .flipInterval = 0,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600558 };
Tony Barbour22a30862015-04-22 09:02:32 -0600559 VkResult U_ASSERT_ONLY err;
Tony Barbourde4124d2015-07-03 10:33:54 -0600560 VkFence nullFence = { VK_NULL_HANDLE };
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600561
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600562 err = vkQueueSubmit(demo->queue, 1, &demo->buffers[demo->current_buffer].cmd,
Tony Barbourde4124d2015-07-03 10:33:54 -0600563 nullFence);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600564 assert(!err);
565
Jon Ashburncedc15f2015-05-21 18:13:33 -0600566 err = demo->fpQueuePresentWSI(demo->queue, &present);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600567 assert(!err);
568
569 demo->current_buffer = (demo->current_buffer + 1) % DEMO_BUFFER_COUNT;
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800570
571 err = vkQueueWaitIdle(demo->queue);
572 assert(err == VK_SUCCESS);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600573}
574
575static void demo_prepare_buffers(struct demo *demo)
576{
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800577 const VkSwapChainCreateInfoWSI swap_chain = {
578 .sType = VK_STRUCTURE_TYPE_SWAP_CHAIN_CREATE_INFO_WSI,
579 .pNext = NULL,
580 .pNativeWindowSystemHandle = demo->connection,
581 .pNativeWindowHandle = (void *) (intptr_t) demo->window,
Ian Elliott32536f92015-04-21 16:41:02 -0600582 .displayCount = 1,
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800583 .imageCount = DEMO_BUFFER_COUNT,
584 .imageFormat = demo->format,
585 .imageExtent = {
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600586 .width = demo->width,
587 .height = demo->height,
588 },
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800589 .imageArraySize = 1,
590 .imageUsageFlags = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600591 };
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800592 VkSwapChainImageInfoWSI images[DEMO_BUFFER_COUNT];
593 size_t images_size = sizeof(images);
Tony Barbour22a30862015-04-22 09:02:32 -0600594 VkResult U_ASSERT_ONLY err;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600595 uint32_t i;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600596
Jon Ashburncedc15f2015-05-21 18:13:33 -0600597 err = demo->fpCreateSwapChainWSI(demo->device, &swap_chain, &demo->swap_chain);
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800598 assert(!err);
599
Jon Ashburncedc15f2015-05-21 18:13:33 -0600600 err = demo->fpGetSwapChainInfoWSI(demo->swap_chain,
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800601 VK_SWAP_CHAIN_INFO_TYPE_PERSISTENT_IMAGES_WSI,
602 &images_size, images);
603 assert(!err && images_size == sizeof(images));
604
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600605 for (i = 0; i < DEMO_BUFFER_COUNT; i++) {
Chia-I Wuc278df82015-07-07 11:50:03 +0800606 VkAttachmentViewCreateInfo color_attachment_view = {
607 .sType = VK_STRUCTURE_TYPE_ATTACHMENT_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600608 .pNext = NULL,
609 .format = demo->format,
610 .mipLevel = 0,
611 .baseArraySlice = 0,
612 .arraySize = 1,
613 };
614
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800615 demo->buffers[i].image = images[i].image;
616 demo->buffers[i].mem = images[i].memory;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600617
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600618 demo_set_image_layout(demo, demo->buffers[i].image,
malnasse4b8ba4d2015-06-03 17:28:38 -0400619 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600620 VK_IMAGE_LAYOUT_UNDEFINED,
621 VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600622
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600623 color_attachment_view.image = demo->buffers[i].image;
624
Chia-I Wuc278df82015-07-07 11:50:03 +0800625 err = vkCreateAttachmentView(demo->device,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600626 &color_attachment_view, &demo->buffers[i].view);
627 assert(!err);
628 }
629}
630
631static void demo_prepare_depth(struct demo *demo)
632{
Tony Barbour8205d902015-04-16 15:59:00 -0600633 const VkFormat depth_format = VK_FORMAT_D16_UNORM;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600634 const VkImageCreateInfo image = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600635 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600636 .pNext = NULL,
Tony Barbour8205d902015-04-16 15:59:00 -0600637 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600638 .format = depth_format,
639 .extent = { demo->width, demo->height, 1 },
640 .mipLevels = 1,
641 .arraySize = 1,
642 .samples = 1,
Tony Barbour8205d902015-04-16 15:59:00 -0600643 .tiling = VK_IMAGE_TILING_OPTIMAL,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600644 .usage = VK_IMAGE_USAGE_DEPTH_STENCIL_BIT,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600645 .flags = 0,
646 };
Courtney Goeltzenleuchterddcb6192015-04-14 18:48:46 -0600647 VkMemoryAllocInfo mem_alloc = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600648 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
Mark Lobodzinski97dcd042015-04-16 08:52:00 -0500649 .pNext = NULL,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600650 .allocationSize = 0,
Mark Lobodzinski72346292015-07-02 16:49:40 -0600651 .memoryTypeIndex = 0,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600652 };
Chia-I Wuc278df82015-07-07 11:50:03 +0800653 VkAttachmentViewCreateInfo view = {
654 .sType = VK_STRUCTURE_TYPE_ATTACHMENT_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600655 .pNext = NULL,
Tony Barbourde4124d2015-07-03 10:33:54 -0600656 .image.handle = VK_NULL_HANDLE,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600657 .mipLevel = 0,
658 .baseArraySlice = 0,
659 .arraySize = 1,
660 .flags = 0,
661 };
Mike Stroyan230e6252015-04-17 12:36:38 -0600662
Mark Lobodzinski23182612015-05-29 09:32:35 -0500663 VkMemoryRequirements mem_reqs;
Tony Barbour22a30862015-04-22 09:02:32 -0600664 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600665
666 demo->depth.format = depth_format;
667
668 /* create image */
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600669 err = vkCreateImage(demo->device, &image,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600670 &demo->depth.image);
671 assert(!err);
672
Tony Barbourde4124d2015-07-03 10:33:54 -0600673 err = vkGetImageMemoryRequirements(demo->device,
674 demo->depth.image, &mem_reqs);
Mark Lobodzinski72346292015-07-02 16:49:40 -0600675
676 mem_alloc.allocationSize = mem_reqs.size;
677 err = memory_type_from_properties(demo,
678 mem_reqs.memoryTypeBits,
679 VK_MEMORY_PROPERTY_DEVICE_ONLY,
680 &mem_alloc.memoryTypeIndex);
681 assert(!err);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600682
Mark Lobodzinski23182612015-05-29 09:32:35 -0500683 /* allocate memory */
684 err = vkAllocMemory(demo->device, &mem_alloc, &demo->depth.mem);
685 assert(!err);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600686
Mark Lobodzinski23182612015-05-29 09:32:35 -0500687 /* bind memory */
Tony Barbourde4124d2015-07-03 10:33:54 -0600688 err = vkBindImageMemory(demo->device, demo->depth.image,
Mark Lobodzinski23182612015-05-29 09:32:35 -0500689 demo->depth.mem, 0);
690 assert(!err);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600691
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600692 demo_set_image_layout(demo, demo->depth.image,
malnasse4b8ba4d2015-06-03 17:28:38 -0400693 VK_IMAGE_ASPECT_DEPTH,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600694 VK_IMAGE_LAYOUT_UNDEFINED,
695 VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600696
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600697 /* create image view */
698 view.image = demo->depth.image;
Chia-I Wuc278df82015-07-07 11:50:03 +0800699 err = vkCreateAttachmentView(demo->device, &view, &demo->depth.view);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600700 assert(!err);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600701}
702
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600703/** loadTexture
Courtney Goeltzenleuchtercb67a322015-04-21 09:31:23 -0600704 * loads a png file into an memory object, using cstdio , libpng.
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600705 *
Courtney Goeltzenleuchtercb67a322015-04-21 09:31:23 -0600706 * \param demo : Needed to access VK calls
707 * \param filename : the png file to be loaded
708 * \param width : width of png, to be updated as a side effect of this function
709 * \param height : height of png, to be updated as a side effect of this function
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600710 *
Courtney Goeltzenleuchtercb67a322015-04-21 09:31:23 -0600711 * \return bool : an opengl texture id. true if successful?,
712 * should be validated by the client of this function.
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600713 *
714 * Source: http://en.wikibooks.org/wiki/OpenGL_Programming/Intermediate/Textures
715 * Modified to copy image to memory
716 *
717 */
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700718bool loadTexture(const char *filename, uint8_t *rgba_data,
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600719 VkSubresourceLayout *layout,
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600720 int32_t *width, int32_t *height)
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600721{
722 //header for testing if it is a png
723 png_byte header[8];
Tony Barboura938abb2015-04-22 11:36:22 -0600724 int is_png, bit_depth, color_type, rowbytes;
725 size_t retval;
Ian Elliott642f8922015-02-13 14:29:21 -0700726 png_uint_32 i, twidth, theight;
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600727 png_structp png_ptr;
728 png_infop info_ptr, end_info;
729 png_byte *image_data;
730 png_bytep *row_pointers;
731
732 //open file as binary
733 FILE *fp = fopen(filename, "rb");
734 if (!fp) {
735 return false;
736 }
737
738 //read the header
Tony Barbour22a30862015-04-22 09:02:32 -0600739 retval = fread(header, 1, 8, fp);
740 if (retval != 8) {
741 fclose(fp);
742 return false;
743 }
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600744
745 //test if png
746 is_png = !png_sig_cmp(header, 0, 8);
747 if (!is_png) {
748 fclose(fp);
749 return false;
750 }
751
752 //create png struct
753 png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL,
754 NULL, NULL);
755 if (!png_ptr) {
756 fclose(fp);
757 return (false);
758 }
759
760 //create png info struct
761 info_ptr = png_create_info_struct(png_ptr);
762 if (!info_ptr) {
763 png_destroy_read_struct(&png_ptr, (png_infopp) NULL, (png_infopp) NULL);
764 fclose(fp);
765 return (false);
766 }
767
768 //create png info struct
769 end_info = png_create_info_struct(png_ptr);
770 if (!end_info) {
771 png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp) NULL);
772 fclose(fp);
773 return (false);
774 }
775
776 //png error stuff, not sure libpng man suggests this.
777 if (setjmp(png_jmpbuf(png_ptr))) {
778 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
779 fclose(fp);
780 return (false);
781 }
782
783 //init png reading
784 png_init_io(png_ptr, fp);
785
786 //let libpng know you already read the first 8 bytes
787 png_set_sig_bytes(png_ptr, 8);
788
789 // read all the info up to the image data
790 png_read_info(png_ptr, info_ptr);
791
792 // get info about png
793 png_get_IHDR(png_ptr, info_ptr, &twidth, &theight, &bit_depth, &color_type,
794 NULL, NULL, NULL);
795
796 //update width and height based on png info
797 *width = twidth;
798 *height = theight;
799
800 // Require that incoming texture be 8bits per color component
801 // and 4 components (RGBA).
802 if (png_get_bit_depth(png_ptr, info_ptr) != 8 ||
803 png_get_channels(png_ptr, info_ptr) != 4) {
804 return false;
805 }
806
807 if (rgba_data == NULL) {
808 // If data pointer is null, we just want the width & height
809 // clean up memory and close stuff
810 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
811 fclose(fp);
812
813 return true;
814 }
815
816 // Update the png info struct.
817 png_read_update_info(png_ptr, info_ptr);
818
819 // Row size in bytes.
820 rowbytes = png_get_rowbytes(png_ptr, info_ptr);
821
822 // Allocate the image_data as a big block, to be given to opengl
823 image_data = (png_byte *)malloc(rowbytes * theight * sizeof(png_byte));
824 if (!image_data) {
825 //clean up memory and close stuff
826 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
827 fclose(fp);
828 return false;
829 }
830
831 // row_pointers is for pointing to image_data for reading the png with libpng
832 row_pointers = (png_bytep *)malloc(theight * sizeof(png_bytep));
833 if (!row_pointers) {
834 //clean up memory and close stuff
835 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
836 // delete[] image_data;
837 fclose(fp);
838 return false;
839 }
840 // set the individual row_pointers to point at the correct offsets of image_data
841 for (i = 0; i < theight; ++i)
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700842 row_pointers[theight - 1 - i] = rgba_data + i * layout->rowPitch;
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600843
844 // read the png into image_data through row_pointers
845 png_read_image(png_ptr, row_pointers);
846
847 // clean up memory and close stuff
848 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
849 free(row_pointers);
850 free(image_data);
851 fclose(fp);
852
853 return true;
854}
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600855
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700856static void demo_prepare_texture_image(struct demo *demo,
857 const char *filename,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600858 struct texture_object *tex_obj,
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600859 VkImageTiling tiling,
Courtney Goeltzenleuchtercb67a322015-04-21 09:31:23 -0600860 VkImageUsageFlags usage,
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600861 VkFlags mem_props)
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600862{
Mike Stroyande24a6f2015-06-15 14:21:03 -0600863 const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600864 int32_t tex_width;
865 int32_t tex_height;
Tony Barbour22a30862015-04-22 09:02:32 -0600866 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700867
David Pinedo61e77382015-04-23 08:16:57 -0600868 if (!loadTexture(filename, NULL, NULL, &tex_width, &tex_height))
869 {
870 printf("Failed to load textures\n");
871 fflush(stdout);
872 exit(1);
873 }
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700874
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600875 tex_obj->tex_width = tex_width;
876 tex_obj->tex_height = tex_height;
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700877
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600878 const VkImageCreateInfo image_create_info = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600879 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700880 .pNext = NULL,
Tony Barbour8205d902015-04-16 15:59:00 -0600881 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700882 .format = tex_format,
883 .extent = { tex_width, tex_height, 1 },
884 .mipLevels = 1,
885 .arraySize = 1,
886 .samples = 1,
887 .tiling = tiling,
Courtney Goeltzenleuchtercb67a322015-04-21 09:31:23 -0600888 .usage = usage,
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700889 .flags = 0,
890 };
Courtney Goeltzenleuchterddcb6192015-04-14 18:48:46 -0600891 VkMemoryAllocInfo mem_alloc = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600892 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
Mark Lobodzinski97dcd042015-04-16 08:52:00 -0500893 .pNext = NULL,
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700894 .allocationSize = 0,
Mark Lobodzinski72346292015-07-02 16:49:40 -0600895 .memoryTypeIndex = 0,
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700896 };
897
Mark Lobodzinski23182612015-05-29 09:32:35 -0500898 VkMemoryRequirements mem_reqs;
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700899
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600900 err = vkCreateImage(demo->device, &image_create_info,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600901 &tex_obj->image);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700902 assert(!err);
903
Tony Barbourde4124d2015-07-03 10:33:54 -0600904 err = vkGetImageMemoryRequirements(demo->device, tex_obj->image, &mem_reqs);
Tony Barbour426b9052015-06-24 16:06:58 -0600905 assert(!err);
Piers Daniell886be472015-02-23 16:23:13 -0700906
Mark Lobodzinski23182612015-05-29 09:32:35 -0500907 mem_alloc.allocationSize = mem_reqs.size;
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700908
Mark Lobodzinski72346292015-07-02 16:49:40 -0600909 err = memory_type_from_properties(demo, mem_reqs.memoryTypeBits, mem_props, &mem_alloc.memoryTypeIndex);
910 assert(!err);
911
Mark Lobodzinski23182612015-05-29 09:32:35 -0500912 /* allocate memory */
913 err = vkAllocMemory(demo->device, &mem_alloc,
914 &(tex_obj->mem));
915 assert(!err);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700916
Mark Lobodzinski23182612015-05-29 09:32:35 -0500917 /* bind memory */
Tony Barbourde4124d2015-07-03 10:33:54 -0600918 err = vkBindImageMemory(demo->device, tex_obj->image,
Mark Lobodzinski23182612015-05-29 09:32:35 -0500919 tex_obj->mem, 0);
920 assert(!err);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700921
Tony Barbour8205d902015-04-16 15:59:00 -0600922 if (mem_props & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) {
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600923 const VkImageSubresource subres = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600924 .aspect = VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700925 .mipLevel = 0,
926 .arraySlice = 0,
927 };
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600928 VkSubresourceLayout layout;
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700929 void *data;
930
Tony Barbour426b9052015-06-24 16:06:58 -0600931 err = vkGetImageSubresourceLayout(demo->device, tex_obj->image, &subres, &layout);
932 assert(!err);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700933
Mark Lobodzinski23182612015-05-29 09:32:35 -0500934 err = vkMapMemory(demo->device, tex_obj->mem, 0, 0, 0, &data);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700935 assert(!err);
936
937 if (!loadTexture(filename, data, &layout, &tex_width, &tex_height)) {
938 fprintf(stderr, "Error loading texture: %s\n", filename);
939 }
940
Mark Lobodzinski23182612015-05-29 09:32:35 -0500941 err = vkUnmapMemory(demo->device, tex_obj->mem);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700942 assert(!err);
943 }
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600944
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600945 tex_obj->imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600946 demo_set_image_layout(demo, tex_obj->image,
malnasse4b8ba4d2015-06-03 17:28:38 -0400947 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600948 VK_IMAGE_LAYOUT_UNDEFINED,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600949 tex_obj->imageLayout);
950 /* 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 -0700951}
952
Mark Lobodzinskicf26e072015-04-16 11:44:05 -0500953static void demo_destroy_texture_image(struct demo *demo, struct texture_object *tex_objs)
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700954{
955 /* clean up staging resources */
Mark Lobodzinski23182612015-05-29 09:32:35 -0500956 vkFreeMemory(demo->device, tex_objs->mem);
Tony Barbourde4124d2015-07-03 10:33:54 -0600957 vkDestroyImage(demo->device, tex_objs->image);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700958}
959
960static void demo_prepare_textures(struct demo *demo)
961{
Tony Barbour8205d902015-04-16 15:59:00 -0600962 const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600963 VkFormatProperties props;
Tony Barbour22a30862015-04-22 09:02:32 -0600964 VkResult U_ASSERT_ONLY err;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600965 uint32_t i;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600966
Chris Forbesd7576302015-06-21 22:55:02 +1200967 err = vkGetPhysicalDeviceFormatInfo(demo->gpu, tex_format, &props);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700968 assert(!err);
969
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600970 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700971
FslNopper434db6a2015-05-06 21:42:01 +0200972 if ((props.linearTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) && !demo->use_staging_buffer) {
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700973 /* Device can texture using linear textures */
974 demo_prepare_texture_image(demo, tex_files[i], &demo->textures[i],
Courtney Goeltzenleuchtercb67a322015-04-21 09:31:23 -0600975 VK_IMAGE_TILING_LINEAR, VK_IMAGE_USAGE_SAMPLED_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
Tony Barbour8205d902015-04-16 15:59:00 -0600976 } else if (props.optimalTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) {
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700977 /* Must use staging buffer to copy linear texture to optimized */
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600978 struct texture_object staging_texture;
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700979
980 memset(&staging_texture, 0, sizeof(staging_texture));
981 demo_prepare_texture_image(demo, tex_files[i], &staging_texture,
Courtney Goeltzenleuchtercb67a322015-04-21 09:31:23 -0600982 VK_IMAGE_TILING_LINEAR, VK_IMAGE_USAGE_TRANSFER_SOURCE_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700983
984 demo_prepare_texture_image(demo, tex_files[i], &demo->textures[i],
Courtney Goeltzenleuchtercb67a322015-04-21 09:31:23 -0600985 VK_IMAGE_TILING_OPTIMAL,
986 (VK_IMAGE_USAGE_TRANSFER_DESTINATION_BIT | VK_IMAGE_USAGE_SAMPLED_BIT),
987 VK_MEMORY_PROPERTY_DEVICE_ONLY);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700988
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600989 demo_set_image_layout(demo, staging_texture.image,
malnasse4b8ba4d2015-06-03 17:28:38 -0400990 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600991 staging_texture.imageLayout,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600992 VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700993
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600994 demo_set_image_layout(demo, demo->textures[i].image,
malnasse4b8ba4d2015-06-03 17:28:38 -0400995 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600996 demo->textures[i].imageLayout,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600997 VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700998
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600999 VkImageCopy copy_region = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001000 .srcSubresource = { VK_IMAGE_ASPECT_COLOR, 0, 0 },
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001001 .srcOffset = { 0, 0, 0 },
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001002 .destSubresource = { VK_IMAGE_ASPECT_COLOR, 0, 0 },
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001003 .destOffset = { 0, 0, 0 },
1004 .extent = { staging_texture.tex_width, staging_texture.tex_height, 1 },
1005 };
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001006 vkCmdCopyImage(demo->cmd,
1007 staging_texture.image, VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL,
1008 demo->textures[i].image, VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL,
Courtney Goeltzenleuchter51cbf302015-03-25 11:25:10 -06001009 1, &copy_region);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001010
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -06001011 demo_set_image_layout(demo, demo->textures[i].image,
malnasse4b8ba4d2015-06-03 17:28:38 -04001012 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001013 VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -06001014 demo->textures[i].imageLayout);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001015
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -06001016 demo_flush_init_cmd(demo);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001017
Courtney Goeltzenleuchter876629f2015-04-21 09:30:03 -06001018 demo_destroy_texture_image(demo, &staging_texture);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001019 } else {
Mike Stroyande24a6f2015-06-15 14:21:03 -06001020 /* Can't support VK_FORMAT_R8G8B8A8_UNORM !? */
1021 assert(!"No support for R8G8B8A8_UNORM as texture image format");
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001022 }
1023
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001024 const VkSamplerCreateInfo sampler = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001025 .sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001026 .pNext = NULL,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001027 .magFilter = VK_TEX_FILTER_NEAREST,
1028 .minFilter = VK_TEX_FILTER_NEAREST,
Tony Barbour8205d902015-04-16 15:59:00 -06001029 .mipMode = VK_TEX_MIPMAP_MODE_BASE,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001030 .addressU = VK_TEX_ADDRESS_CLAMP,
1031 .addressV = VK_TEX_ADDRESS_CLAMP,
1032 .addressW = VK_TEX_ADDRESS_CLAMP,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001033 .mipLodBias = 0.0f,
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001034 .maxAnisotropy = 1,
Tony Barbour8205d902015-04-16 15:59:00 -06001035 .compareOp = VK_COMPARE_OP_NEVER,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001036 .minLod = 0.0f,
1037 .maxLod = 0.0f,
Tony Barbour2c4e7c72015-06-25 16:56:44 -06001038 .borderColor = VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001039 };
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -06001040
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001041 VkImageViewCreateInfo view = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001042 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001043 .pNext = NULL,
Tony Barbourde4124d2015-07-03 10:33:54 -06001044 .image.handle = VK_NULL_HANDLE,
Tony Barbour8205d902015-04-16 15:59:00 -06001045 .viewType = VK_IMAGE_VIEW_TYPE_2D,
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001046 .format = tex_format,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001047 .channels = { VK_CHANNEL_SWIZZLE_R,
1048 VK_CHANNEL_SWIZZLE_G,
1049 VK_CHANNEL_SWIZZLE_B,
1050 VK_CHANNEL_SWIZZLE_A, },
1051 .subresourceRange = { VK_IMAGE_ASPECT_COLOR, 0, 1, 0, 1 },
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001052 };
Jon Ashburna9ae3832015-01-16 09:37:43 -07001053
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001054 /* create sampler */
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001055 err = vkCreateSampler(demo->device, &sampler,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001056 &demo->textures[i].sampler);
1057 assert(!err);
1058
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001059 /* create image view */
1060 view.image = demo->textures[i].image;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001061 err = vkCreateImageView(demo->device, &view,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001062 &demo->textures[i].view);
1063 assert(!err);
1064 }
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001065}
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001066
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001067void demo_prepare_cube_data_buffer(struct demo *demo)
1068{
Courtney Goeltzenleuchterddcb6192015-04-14 18:48:46 -06001069 VkBufferCreateInfo buf_info;
1070 VkBufferViewCreateInfo view_info;
Courtney Goeltzenleuchterddcb6192015-04-14 18:48:46 -06001071 VkMemoryAllocInfo alloc_info = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001072 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
Mark Lobodzinski97dcd042015-04-16 08:52:00 -05001073 .pNext = NULL,
Jon Ashburnc6ae13d2015-01-19 15:00:26 -07001074 .allocationSize = 0,
Mark Lobodzinski72346292015-07-02 16:49:40 -06001075 .memoryTypeIndex = 0,
Jon Ashburnc6ae13d2015-01-19 15:00:26 -07001076 };
Mark Lobodzinski23182612015-05-29 09:32:35 -05001077 VkMemoryRequirements mem_reqs;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001078 size_t mem_reqs_size = sizeof(VkMemoryRequirements);
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001079 uint8_t *pData;
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001080 int i;
1081 mat4x4 MVP, VP;
Tony Barbour22a30862015-04-22 09:02:32 -06001082 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001083 struct vktexcube_vs_uniform data;
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001084
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001085 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001086 mat4x4_mul(MVP, VP, demo->model_matrix);
1087 memcpy(data.mvp, MVP, sizeof(MVP));
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -06001088// dumpMatrix("MVP", MVP);
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001089
1090 for (i=0; i<12*3; i++) {
1091 data.position[i][0] = g_vertex_buffer_data[i*3];
1092 data.position[i][1] = g_vertex_buffer_data[i*3+1];
1093 data.position[i][2] = g_vertex_buffer_data[i*3+2];
1094 data.position[i][3] = 1.0f;
1095 data.attr[i][0] = g_uv_buffer_data[2*i];
1096 data.attr[i][1] = g_uv_buffer_data[2*i + 1];
1097 data.attr[i][2] = 0;
1098 data.attr[i][3] = 0;
1099 }
1100
Chia-I Wu714df452015-01-01 07:55:04 +08001101 memset(&buf_info, 0, sizeof(buf_info));
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001102 buf_info.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
Courtney Goeltzenleuchterad870812015-04-15 15:29:59 -06001103 buf_info.usage = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001104 err = vkCreateBuffer(demo->device, &buf_info, &demo->uniform_data.buf);
Chia-I Wu714df452015-01-01 07:55:04 +08001105 assert(!err);
1106
Tony Barbourde4124d2015-07-03 10:33:54 -06001107 err = vkGetBufferMemoryRequirements(demo->device, demo->uniform_data.buf, &mem_reqs);
Mark Lobodzinski23182612015-05-29 09:32:35 -05001108 assert(!err && mem_reqs_size == sizeof(mem_reqs));
Chia-I Wu714df452015-01-01 07:55:04 +08001109
Mark Lobodzinski23182612015-05-29 09:32:35 -05001110 alloc_info.allocationSize = mem_reqs.size;
Mark Lobodzinski72346292015-07-02 16:49:40 -06001111 err = memory_type_from_properties(demo,
1112 mem_reqs.memoryTypeBits,
1113 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT,
1114 &alloc_info.memoryTypeIndex);
1115 assert(!err);
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001116
Mark Lobodzinski23182612015-05-29 09:32:35 -05001117 err = vkAllocMemory(demo->device, &alloc_info, &(demo->uniform_data.mem));
1118 assert(!err);
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001119
Mark Lobodzinski23182612015-05-29 09:32:35 -05001120 err = vkMapMemory(demo->device, demo->uniform_data.mem, 0, 0, 0, (void **) &pData);
1121 assert(!err);
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001122
Mark Lobodzinski23182612015-05-29 09:32:35 -05001123 memcpy(pData, &data, sizeof data);
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001124
Mark Lobodzinski23182612015-05-29 09:32:35 -05001125 err = vkUnmapMemory(demo->device, demo->uniform_data.mem);
1126 assert(!err);
1127
Tony Barbourde4124d2015-07-03 10:33:54 -06001128 err = vkBindBufferMemory(demo->device,
1129 demo->uniform_data.buf,
Mark Lobodzinski23182612015-05-29 09:32:35 -05001130 demo->uniform_data.mem, 0);
1131 assert(!err);
Chia-I Wu714df452015-01-01 07:55:04 +08001132
1133 memset(&view_info, 0, sizeof(view_info));
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001134 view_info.sType = VK_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO;
Chia-I Wu714df452015-01-01 07:55:04 +08001135 view_info.buffer = demo->uniform_data.buf;
Tony Barbour8205d902015-04-16 15:59:00 -06001136 view_info.viewType = VK_BUFFER_VIEW_TYPE_RAW;
Chia-I Wu714df452015-01-01 07:55:04 +08001137 view_info.offset = 0;
1138 view_info.range = sizeof(data);
1139
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001140 err = vkCreateBufferView(demo->device, &view_info, &demo->uniform_data.view);
Chia-I Wu714df452015-01-01 07:55:04 +08001141 assert(!err);
1142
Chia-I Wu8cd8ecd2015-05-25 16:27:55 +08001143 demo->uniform_data.desc.bufferView = demo->uniform_data.view;
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001144}
1145
Chia-I Wuf8385062015-01-04 16:27:24 +08001146static void demo_prepare_descriptor_layout(struct demo *demo)
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001147{
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001148 const VkDescriptorSetLayoutBinding layout_bindings[2] = {
Chia-I Wufc9d9132015-03-26 15:04:41 +08001149 [0] = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001150 .descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
Chia-I Wud3114a22015-05-25 16:22:52 +08001151 .arraySize = 1,
Tony Barbour8205d902015-04-16 15:59:00 -06001152 .stageFlags = VK_SHADER_STAGE_VERTEX_BIT,
Chia-I Wu310eece2015-03-27 12:56:09 +08001153 .pImmutableSamplers = NULL,
Chia-I Wufc9d9132015-03-26 15:04:41 +08001154 },
1155 [1] = {
Courtney Goeltzenleuchterad870812015-04-15 15:29:59 -06001156 .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
Chia-I Wud3114a22015-05-25 16:22:52 +08001157 .arraySize = DEMO_TEXTURE_COUNT,
Tony Barbour8205d902015-04-16 15:59:00 -06001158 .stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT,
Chia-I Wu310eece2015-03-27 12:56:09 +08001159 .pImmutableSamplers = NULL,
Chia-I Wufc9d9132015-03-26 15:04:41 +08001160 },
1161 };
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001162 const VkDescriptorSetLayoutCreateInfo descriptor_layout = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001163 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
Chia-I Wuf8385062015-01-04 16:27:24 +08001164 .pNext = NULL,
Chia-I Wufc9d9132015-03-26 15:04:41 +08001165 .count = 2,
1166 .pBinding = layout_bindings,
Chia-I Wuf8385062015-01-04 16:27:24 +08001167 };
Tony Barbour22a30862015-04-22 09:02:32 -06001168 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001169
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001170 err = vkCreateDescriptorSetLayout(demo->device,
Chia-I Wu7732cb22015-03-26 15:27:55 +08001171 &descriptor_layout, &demo->desc_layout);
1172 assert(!err);
1173
Mark Lobodzinski556f7212015-04-17 14:11:39 -05001174 const VkPipelineLayoutCreateInfo pPipelineLayoutCreateInfo = {
1175 .sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
1176 .pNext = NULL,
1177 .descriptorSetCount = 1,
1178 .pSetLayouts = &demo->desc_layout,
1179 };
1180
1181 err = vkCreatePipelineLayout(demo->device,
1182 &pPipelineLayoutCreateInfo,
1183 &demo->pipeline_layout);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001184 assert(!err);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001185}
1186
Chia-I Wu76cd4222015-07-08 13:34:24 +08001187static void demo_prepare_render_pass(struct demo *demo)
1188{
Chia-I Wuc278df82015-07-07 11:50:03 +08001189 const VkAttachmentDescription attachments[2] = {
1190 [0] = {
1191 .sType = VK_STRUCTURE_TYPE_ATTACHMENT_DESCRIPTION,
1192 .pNext = NULL,
1193 .format = demo->format,
1194 .samples = 1,
1195 .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
1196 .storeOp = VK_ATTACHMENT_STORE_OP_STORE,
1197 .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
1198 .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1199 .initialLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
1200 .finalLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
1201 },
1202 [1] = {
1203 .sType = VK_STRUCTURE_TYPE_ATTACHMENT_DESCRIPTION,
1204 .pNext = NULL,
1205 .format = demo->depth.format,
1206 .samples = 1,
1207 .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
1208 .storeOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1209 .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
1210 .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1211 .initialLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1212 .finalLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1213 },
Chia-I Wu76cd4222015-07-08 13:34:24 +08001214 };
Chia-I Wuc278df82015-07-07 11:50:03 +08001215 const VkAttachmentReference color_reference = {
1216 .attachment = 0,
1217 .layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
1218 };
1219 const VkSubpassDescription subpass = {
1220 .sType = VK_STRUCTURE_TYPE_SUBPASS_DESCRIPTION,
1221 .pNext = NULL,
1222 .pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS,
1223 .flags = 0,
1224 .inputCount = 0,
1225 .inputAttachments = NULL,
1226 .colorCount = 1,
1227 .colorAttachments = &color_reference,
1228 .resolveAttachments = NULL,
1229 .depthStencilAttachment = {
1230 .attachment = 1,
1231 .layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1232 },
1233 .preserveCount = 0,
1234 .preserveAttachments = NULL,
1235 };
Chia-I Wu76cd4222015-07-08 13:34:24 +08001236 const VkRenderPassCreateInfo rp_info = {
1237 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO,
1238 .pNext = NULL,
Chia-I Wuc278df82015-07-07 11:50:03 +08001239 .attachmentCount = 2,
1240 .pAttachments = attachments,
1241 .subpassCount = 1,
1242 .pSubpasses = &subpass,
1243 .dependencyCount = 0,
1244 .pDependencies = NULL,
Chia-I Wu76cd4222015-07-08 13:34:24 +08001245 };
Chia-I Wuc278df82015-07-07 11:50:03 +08001246 VkResult U_ASSERT_ONLY err;
Chia-I Wu76cd4222015-07-08 13:34:24 +08001247
1248 err = vkCreateRenderPass(demo->device, &rp_info, &demo->render_pass);
1249 assert(!err);
1250}
1251
Tobin Ehlise796a1d2015-07-02 11:02:49 -06001252static VkShader demo_prepare_shader(struct demo* demo,
Tony Barbour8205d902015-04-16 15:59:00 -06001253 VkShaderStage stage,
Tobin Ehlise796a1d2015-07-02 11:02:49 -06001254 const void* code,
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001255 size_t size)
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001256{
Courtney Goeltzenleuchter0b29b0d2015-06-24 18:24:19 -06001257 VkShaderModuleCreateInfo moduleCreateInfo;
1258 VkShaderCreateInfo shaderCreateInfo;
1259 VkShaderModule shaderModule;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001260 VkShader shader;
1261 VkResult err;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001262
Courtney Goeltzenleuchter7e334982014-10-30 15:14:16 -06001263
Courtney Goeltzenleuchter0b29b0d2015-06-24 18:24:19 -06001264 moduleCreateInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
1265 moduleCreateInfo.pNext = NULL;
1266
1267 shaderCreateInfo.sType = VK_STRUCTURE_TYPE_SHADER_CREATE_INFO;
1268 shaderCreateInfo.pNext = NULL;
Tobin Ehlise796a1d2015-07-02 11:02:49 -06001269 shaderCreateInfo.pName = "main";
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001270
Cody Northrop75db0322015-05-28 11:27:16 -06001271 if (!demo->use_glsl) {
Courtney Goeltzenleuchter0b29b0d2015-06-24 18:24:19 -06001272 moduleCreateInfo.codeSize = size;
1273 moduleCreateInfo.pCode = code;
1274 moduleCreateInfo.flags = 0;
1275 err = vkCreateShaderModule(demo->device, &moduleCreateInfo, &shaderModule);
Cody Northrop75db0322015-05-28 11:27:16 -06001276 if (err) {
Courtney Goeltzenleuchter0b29b0d2015-06-24 18:24:19 -06001277 free((void *) moduleCreateInfo.pCode);
Cody Northrop75db0322015-05-28 11:27:16 -06001278 }
Courtney Goeltzenleuchter0b29b0d2015-06-24 18:24:19 -06001279
1280 shaderCreateInfo.flags = 0;
1281 shaderCreateInfo.module = shaderModule;
1282 err = vkCreateShader(demo->device, &shaderCreateInfo, &shader);
Cody Northrop75db0322015-05-28 11:27:16 -06001283 } else {
1284 // Create fake SPV structure to feed GLSL
1285 // to the driver "under the covers"
Courtney Goeltzenleuchter0b29b0d2015-06-24 18:24:19 -06001286 moduleCreateInfo.codeSize = 3 * sizeof(uint32_t) + size + 1;
1287 moduleCreateInfo.pCode = malloc(moduleCreateInfo.codeSize);
1288 moduleCreateInfo.flags = 0;
Cody Northrop75db0322015-05-28 11:27:16 -06001289
1290 /* try version 0 first: VkShaderStage followed by GLSL */
Courtney Goeltzenleuchter0b29b0d2015-06-24 18:24:19 -06001291 ((uint32_t *) moduleCreateInfo.pCode)[0] = ICD_SPV_MAGIC;
1292 ((uint32_t *) moduleCreateInfo.pCode)[1] = 0;
1293 ((uint32_t *) moduleCreateInfo.pCode)[2] = stage;
1294 memcpy(((uint32_t *) moduleCreateInfo.pCode + 3), code, size + 1);
Cody Northrop75db0322015-05-28 11:27:16 -06001295
Courtney Goeltzenleuchter0b29b0d2015-06-24 18:24:19 -06001296 err = vkCreateShaderModule(demo->device, &moduleCreateInfo, &shaderModule);
Cody Northrop75db0322015-05-28 11:27:16 -06001297 if (err) {
Courtney Goeltzenleuchter0b29b0d2015-06-24 18:24:19 -06001298 free((void *) moduleCreateInfo.pCode);
Cody Northrop75db0322015-05-28 11:27:16 -06001299 }
Courtney Goeltzenleuchter0b29b0d2015-06-24 18:24:19 -06001300
1301 shaderCreateInfo.flags = 0;
1302 shaderCreateInfo.module = shaderModule;
1303 err = vkCreateShader(demo->device, &shaderCreateInfo, &shader);
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -06001304 }
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001305 return shader;
1306}
1307
Cody Northropacfb0492015-03-17 15:55:58 -06001308char *demo_read_spv(const char *filename, size_t *psize)
Courtney Goeltzenleuchter7e334982014-10-30 15:14:16 -06001309{
1310 long int size;
Tony Barboura938abb2015-04-22 11:36:22 -06001311 size_t U_ASSERT_ONLY retval;
Courtney Goeltzenleuchter7e334982014-10-30 15:14:16 -06001312 void *shader_code;
1313
1314 FILE *fp = fopen(filename, "rb");
1315 if (!fp) return NULL;
1316
1317 fseek(fp, 0L, SEEK_END);
1318 size = ftell(fp);
1319
1320 fseek(fp, 0L, SEEK_SET);
1321
1322 shader_code = malloc(size);
Tony Barbour22a30862015-04-22 09:02:32 -06001323 retval = fread(shader_code, size, 1, fp);
1324 assert(retval == 1);
Courtney Goeltzenleuchter7e334982014-10-30 15:14:16 -06001325
1326 *psize = size;
1327
1328 return shader_code;
1329}
1330
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001331static VkShader demo_prepare_vs(struct demo *demo)
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001332{
Cody Northrop75db0322015-05-28 11:27:16 -06001333 if (!demo->use_glsl) {
1334 void *vertShaderCode;
1335 size_t size;
Courtney Goeltzenleuchter7e334982014-10-30 15:14:16 -06001336
Cody Northrop75db0322015-05-28 11:27:16 -06001337 vertShaderCode = demo_read_spv("cube-vert.spv", &size);
Courtney Goeltzenleuchter7e334982014-10-30 15:14:16 -06001338
Cody Northrop75db0322015-05-28 11:27:16 -06001339 return demo_prepare_shader(demo, VK_SHADER_STAGE_VERTEX,
1340 vertShaderCode, size);
1341 } else {
1342 static const char *vertShaderText =
1343 "#version 140\n"
1344 "#extension GL_ARB_separate_shader_objects : enable\n"
1345 "#extension GL_ARB_shading_language_420pack : enable\n"
1346 "\n"
1347 "layout(binding = 0) uniform buf {\n"
1348 " mat4 MVP;\n"
1349 " vec4 position[12*3];\n"
1350 " vec4 attr[12*3];\n"
1351 "} ubuf;\n"
1352 "\n"
1353 "layout (location = 0) out vec4 texcoord;\n"
1354 "\n"
1355 "void main() \n"
1356 "{\n"
1357 " texcoord = ubuf.attr[gl_VertexID];\n"
1358 " gl_Position = ubuf.MVP * ubuf.position[gl_VertexID];\n"
1359 "\n"
1360 " // GL->VK conventions\n"
1361 " gl_Position.y = -gl_Position.y;\n"
1362 " gl_Position.z = (gl_Position.z + gl_Position.w) / 2.0;\n"
1363 "}\n";
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -06001364
Cody Northrop75db0322015-05-28 11:27:16 -06001365 return demo_prepare_shader(demo, VK_SHADER_STAGE_VERTEX,
1366 (const void *) vertShaderText,
1367 strlen(vertShaderText));
1368 }
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001369}
1370
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001371static VkShader demo_prepare_fs(struct demo *demo)
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001372{
Cody Northrop75db0322015-05-28 11:27:16 -06001373 if (!demo->use_glsl) {
1374 void *fragShaderCode;
1375 size_t size;
Courtney Goeltzenleuchter7e334982014-10-30 15:14:16 -06001376
Cody Northrop75db0322015-05-28 11:27:16 -06001377 fragShaderCode = demo_read_spv("cube-frag.spv", &size);
Courtney Goeltzenleuchter7e334982014-10-30 15:14:16 -06001378
Cody Northrop75db0322015-05-28 11:27:16 -06001379 return demo_prepare_shader(demo, VK_SHADER_STAGE_FRAGMENT,
1380 fragShaderCode, size);
1381 } else {
1382 static const char *fragShaderText =
1383 "#version 140\n"
1384 "#extension GL_ARB_separate_shader_objects : enable\n"
1385 "#extension GL_ARB_shading_language_420pack : enable\n"
1386 "layout (binding = 1) uniform sampler2D tex;\n"
1387 "\n"
1388 "layout (location = 0) in vec4 texcoord;\n"
1389 "layout (location = 0) out vec4 uFragColor;\n"
1390 "void main() {\n"
1391 " uFragColor = texture(tex, texcoord.xy);\n"
1392 "}\n";
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -06001393
Cody Northrop75db0322015-05-28 11:27:16 -06001394 return demo_prepare_shader(demo, VK_SHADER_STAGE_FRAGMENT,
1395 (const void *) fragShaderText,
1396 strlen(fragShaderText));
1397 }
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001398}
1399
1400static void demo_prepare_pipeline(struct demo *demo)
1401{
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001402 VkGraphicsPipelineCreateInfo pipeline;
Jon Ashburn0d60d272015-07-09 15:02:25 -06001403 VkPipelineCacheCreateInfo pipelineCache;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001404 VkPipelineIaStateCreateInfo ia;
1405 VkPipelineRsStateCreateInfo rs;
1406 VkPipelineCbStateCreateInfo cb;
1407 VkPipelineDsStateCreateInfo ds;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001408 VkPipelineVpStateCreateInfo vp;
1409 VkPipelineMsStateCreateInfo ms;
Tony Barbour22a30862015-04-22 09:02:32 -06001410 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001411
1412 memset(&pipeline, 0, sizeof(pipeline));
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001413 pipeline.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
Mark Lobodzinski556f7212015-04-17 14:11:39 -05001414 pipeline.layout = demo->pipeline_layout;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001415
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001416 memset(&ia, 0, sizeof(ia));
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001417 ia.sType = VK_STRUCTURE_TYPE_PIPELINE_IA_STATE_CREATE_INFO;
Tony Barbour8205d902015-04-16 15:59:00 -06001418 ia.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001419
1420 memset(&rs, 0, sizeof(rs));
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001421 rs.sType = VK_STRUCTURE_TYPE_PIPELINE_RS_STATE_CREATE_INFO;
Tony Barbour8205d902015-04-16 15:59:00 -06001422 rs.fillMode = VK_FILL_MODE_SOLID;
1423 rs.cullMode = VK_CULL_MODE_BACK;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001424 rs.frontFace = VK_FRONT_FACE_CCW;
Chia-I Wue2504cb2015-04-22 14:20:52 +08001425 rs.depthClipEnable = VK_TRUE;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001426
1427 memset(&cb, 0, sizeof(cb));
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001428 cb.sType = VK_STRUCTURE_TYPE_PIPELINE_CB_STATE_CREATE_INFO;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001429 VkPipelineCbAttachmentState att_state[1];
Tony Barbourfa6cac72015-01-16 14:27:35 -07001430 memset(att_state, 0, sizeof(att_state));
Tony Barbourfa6cac72015-01-16 14:27:35 -07001431 att_state[0].channelWriteMask = 0xf;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001432 att_state[0].blendEnable = VK_FALSE;
Tony Barbourfa6cac72015-01-16 14:27:35 -07001433 cb.attachmentCount = 1;
1434 cb.pAttachments = att_state;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001435
Tony Barbourfa6cac72015-01-16 14:27:35 -07001436 memset(&vp, 0, sizeof(vp));
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001437 vp.sType = VK_STRUCTURE_TYPE_PIPELINE_VP_STATE_CREATE_INFO;
Tony Barbour8205d902015-04-16 15:59:00 -06001438 vp.viewportCount = 1;
Tony Barbourfa6cac72015-01-16 14:27:35 -07001439
1440 memset(&ds, 0, sizeof(ds));
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001441 ds.sType = VK_STRUCTURE_TYPE_PIPELINE_DS_STATE_CREATE_INFO;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001442 ds.depthTestEnable = VK_TRUE;
1443 ds.depthWriteEnable = VK_TRUE;
Tony Barbour8205d902015-04-16 15:59:00 -06001444 ds.depthCompareOp = VK_COMPARE_OP_LESS_EQUAL;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001445 ds.depthBoundsEnable = VK_FALSE;
1446 ds.back.stencilFailOp = VK_STENCIL_OP_KEEP;
1447 ds.back.stencilPassOp = VK_STENCIL_OP_KEEP;
Tony Barbour8205d902015-04-16 15:59:00 -06001448 ds.back.stencilCompareOp = VK_COMPARE_OP_ALWAYS;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001449 ds.stencilTestEnable = VK_FALSE;
Tony Barbourfa6cac72015-01-16 14:27:35 -07001450 ds.front = ds.back;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001451
Tony Barbourfa6cac72015-01-16 14:27:35 -07001452 memset(&ms, 0, sizeof(ms));
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001453 ms.sType = VK_STRUCTURE_TYPE_PIPELINE_MS_STATE_CREATE_INFO;
Tony Barbourfa6cac72015-01-16 14:27:35 -07001454 ms.sampleMask = 1;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001455 ms.multisampleEnable = VK_FALSE;
Tony Barboure094edf2015-06-26 10:18:34 -06001456 ms.rasterSamples = 1;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001457
Mark Lobodzinski0e0fb5c2015-06-23 15:11:57 -06001458 // Two stages: vs and fs
1459 pipeline.stageCount = 2;
1460 VkPipelineShaderStageCreateInfo shaderStages[2];
1461 memset(&shaderStages, 0, 2 * sizeof(VkPipelineShaderStageCreateInfo));
1462
1463 shaderStages[0].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1464 shaderStages[0].stage = VK_SHADER_STAGE_VERTEX;
1465 shaderStages[0].shader = demo_prepare_vs(demo);
1466
1467 shaderStages[1].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1468 shaderStages[1].stage = VK_SHADER_STAGE_FRAGMENT;
1469 shaderStages[1].shader = demo_prepare_fs(demo);
1470
Mark Lobodzinskic1e21462015-06-30 10:18:36 -06001471 pipeline.pVertexInputState = NULL;
Mark Lobodzinski0e0fb5c2015-06-23 15:11:57 -06001472 pipeline.pIaState = &ia;
1473 pipeline.pRsState = &rs;
1474 pipeline.pCbState = &cb;
1475 pipeline.pMsState = &ms;
1476 pipeline.pVpState = &vp;
1477 pipeline.pDsState = &ds;
1478 pipeline.pStages = shaderStages;
Chia-I Wuc278df82015-07-07 11:50:03 +08001479 pipeline.renderPass = demo->render_pass;
1480 pipeline.subpass = 0;
1481
Jon Ashburn0d60d272015-07-09 15:02:25 -06001482 memset(&pipelineCache, 0, sizeof(pipelineCache));
1483 pipelineCache.sType = VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001484
Jon Ashburn0d60d272015-07-09 15:02:25 -06001485 err = vkCreatePipelineCache(demo->device, &pipelineCache, &demo->pipelineCache);
1486 assert(!err);
1487 err = vkCreateGraphicsPipelines(demo->device, demo->pipelineCache, 1, &pipeline, &demo->pipeline);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001488 assert(!err);
1489
Mark Lobodzinski0e0fb5c2015-06-23 15:11:57 -06001490 for (uint32_t i = 0; i < pipeline.stageCount; i++) {
Tony Barbourde4124d2015-07-03 10:33:54 -06001491 vkDestroyShader(demo->device, shaderStages[i].shader);
Mark Lobodzinski0e0fb5c2015-06-23 15:11:57 -06001492 }
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001493}
1494
1495static void demo_prepare_dynamic_states(struct demo *demo)
1496{
Tony Barbourde4124d2015-07-03 10:33:54 -06001497 VkDynamicViewportStateCreateInfo viewport_create;
1498 VkDynamicRasterStateCreateInfo raster;
1499 VkDynamicColorBlendStateCreateInfo color_blend;
1500 VkDynamicDepthStencilStateCreateInfo depth_stencil;
Tony Barbour22a30862015-04-22 09:02:32 -06001501 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001502
Tony Barbourfa6cac72015-01-16 14:27:35 -07001503 memset(&viewport_create, 0, sizeof(viewport_create));
Tony Barbourde4124d2015-07-03 10:33:54 -06001504 viewport_create.sType = VK_STRUCTURE_TYPE_DYNAMIC_VIEWPORT_STATE_CREATE_INFO;
Courtney Goeltzenleuchterc6e32f92015-02-11 14:13:34 -07001505 viewport_create.viewportAndScissorCount = 1;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001506 VkViewport viewport;
Piers Daniell886be472015-02-23 16:23:13 -07001507 memset(&viewport, 0, sizeof(viewport));
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001508 viewport.height = (float) demo->height;
1509 viewport.width = (float) demo->width;
1510 viewport.minDepth = (float) 0.0f;
1511 viewport.maxDepth = (float) 1.0f;
Piers Daniell886be472015-02-23 16:23:13 -07001512 viewport_create.pViewports = &viewport;
Chris Forbes2951d7d2015-06-22 17:21:59 +12001513 VkRect2D scissor;
Piers Daniell886be472015-02-23 16:23:13 -07001514 memset(&scissor, 0, sizeof(scissor));
Courtney Goeltzenleuchterc6e32f92015-02-11 14:13:34 -07001515 scissor.extent.width = demo->width;
1516 scissor.extent.height = demo->height;
1517 scissor.offset.x = 0;
1518 scissor.offset.y = 0;
Courtney Goeltzenleuchterc6e32f92015-02-11 14:13:34 -07001519 viewport_create.pScissors = &scissor;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001520
1521 memset(&raster, 0, sizeof(raster));
Tony Barbourde4124d2015-07-03 10:33:54 -06001522 raster.sType = VK_STRUCTURE_TYPE_DYNAMIC_RASTER_STATE_CREATE_INFO;
Piers Daniell886be472015-02-23 16:23:13 -07001523 raster.lineWidth = 1.0;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001524
1525 memset(&color_blend, 0, sizeof(color_blend));
Tony Barbourde4124d2015-07-03 10:33:54 -06001526 color_blend.sType = VK_STRUCTURE_TYPE_DYNAMIC_COLOR_BLEND_STATE_CREATE_INFO;
Piers Daniell886be472015-02-23 16:23:13 -07001527 color_blend.blendConst[0] = 1.0f;
1528 color_blend.blendConst[1] = 1.0f;
1529 color_blend.blendConst[2] = 1.0f;
1530 color_blend.blendConst[3] = 1.0f;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001531
1532 memset(&depth_stencil, 0, sizeof(depth_stencil));
Tony Barbourde4124d2015-07-03 10:33:54 -06001533 depth_stencil.sType = VK_STRUCTURE_TYPE_DYNAMIC_DEPTH_STENCIL_STATE_CREATE_INFO;
Mark Lobodzinski4405fbf2015-06-12 11:14:17 -06001534 depth_stencil.minDepthBounds = 0.0f;
1535 depth_stencil.maxDepthBounds = 1.0f;
Tony Barbourfa6cac72015-01-16 14:27:35 -07001536 depth_stencil.stencilBackRef = 0;
1537 depth_stencil.stencilFrontRef = 0;
1538 depth_stencil.stencilReadMask = 0xff;
1539 depth_stencil.stencilWriteMask = 0xff;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001540
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001541 err = vkCreateDynamicViewportState(demo->device, &viewport_create, &demo->viewport);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001542 assert(!err);
1543
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001544 err = vkCreateDynamicRasterState(demo->device, &raster, &demo->raster);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001545 assert(!err);
1546
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001547 err = vkCreateDynamicColorBlendState(demo->device,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001548 &color_blend, &demo->color_blend);
1549 assert(!err);
1550
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001551 err = vkCreateDynamicDepthStencilState(demo->device,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001552 &depth_stencil, &demo->depth_stencil);
1553 assert(!err);
1554}
1555
Chia-I Wu8d24b3b2015-03-26 13:14:16 +08001556static void demo_prepare_descriptor_pool(struct demo *demo)
Chia-I Wuf8385062015-01-04 16:27:24 +08001557{
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001558 const VkDescriptorTypeCount type_counts[2] = {
Chia-I Wuf8385062015-01-04 16:27:24 +08001559 [0] = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001560 .type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
Chia-I Wuf8385062015-01-04 16:27:24 +08001561 .count = 1,
1562 },
1563 [1] = {
Courtney Goeltzenleuchterad870812015-04-15 15:29:59 -06001564 .type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
Chia-I Wuf8385062015-01-04 16:27:24 +08001565 .count = DEMO_TEXTURE_COUNT,
1566 },
1567 };
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001568 const VkDescriptorPoolCreateInfo descriptor_pool = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001569 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO,
Chia-I Wuf8385062015-01-04 16:27:24 +08001570 .pNext = NULL,
1571 .count = 2,
1572 .pTypeCount = type_counts,
1573 };
Tony Barbour22a30862015-04-22 09:02:32 -06001574 VkResult U_ASSERT_ONLY err;
Chia-I Wuf8385062015-01-04 16:27:24 +08001575
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001576 err = vkCreateDescriptorPool(demo->device,
1577 VK_DESCRIPTOR_POOL_USAGE_ONE_SHOT, 1,
Chia-I Wu8d24b3b2015-03-26 13:14:16 +08001578 &descriptor_pool, &demo->desc_pool);
Chia-I Wuf8385062015-01-04 16:27:24 +08001579 assert(!err);
1580}
1581
1582static void demo_prepare_descriptor_set(struct demo *demo)
1583{
Chia-I Wu8cd8ecd2015-05-25 16:27:55 +08001584 VkDescriptorInfo tex_descs[DEMO_TEXTURE_COUNT];
1585 VkWriteDescriptorSet writes[2];
Tony Barbour22a30862015-04-22 09:02:32 -06001586 VkResult U_ASSERT_ONLY err;
Chia-I Wuf8385062015-01-04 16:27:24 +08001587 uint32_t count;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001588 uint32_t i;
Chia-I Wuf8385062015-01-04 16:27:24 +08001589
Mike Stroyan230e6252015-04-17 12:36:38 -06001590 err = vkAllocDescriptorSets(demo->device, demo->desc_pool,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001591 VK_DESCRIPTOR_SET_USAGE_STATIC,
Chia-I Wu6e68a892015-02-23 10:41:08 -07001592 1, &demo->desc_layout,
Chia-I Wuf8385062015-01-04 16:27:24 +08001593 &demo->desc_set, &count);
1594 assert(!err && count == 1);
1595
Chia-I Wu8cd8ecd2015-05-25 16:27:55 +08001596 memset(&tex_descs, 0, sizeof(tex_descs));
1597 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
1598 tex_descs[i].sampler = demo->textures[i].sampler;
1599 tex_descs[i].imageView = demo->textures[i].view;
1600 tex_descs[i].imageLayout = VK_IMAGE_LAYOUT_GENERAL;
1601 }
1602
1603 memset(&writes, 0, sizeof(writes));
1604
1605 writes[0].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
1606 writes[0].destSet = demo->desc_set;
1607 writes[0].count = 1;
1608 writes[0].descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
1609 writes[0].pDescriptors = &demo->uniform_data.desc;
1610
1611 writes[1].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
1612 writes[1].destSet = demo->desc_set;
1613 writes[1].destBinding = 1;
1614 writes[1].count = DEMO_TEXTURE_COUNT;
1615 writes[1].descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
1616 writes[1].pDescriptors = tex_descs;
1617
1618 err = vkUpdateDescriptorSets(demo->device, 2, writes, 0, NULL);
1619 assert(!err);
Chia-I Wuf8385062015-01-04 16:27:24 +08001620}
1621
Chia-I Wu76cd4222015-07-08 13:34:24 +08001622static void demo_prepare_framebuffers(struct demo *demo)
1623{
Chia-I Wuc278df82015-07-07 11:50:03 +08001624 VkAttachmentBindInfo attachments[2] = {
1625 [0] = {
Tobin Ehlis0b9c1952015-07-06 14:02:36 -06001626 .view.handle = VK_NULL_HANDLE,
Chia-I Wuc278df82015-07-07 11:50:03 +08001627 .layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
1628 },
1629 [1] = {
1630 .view = demo->depth.view,
1631 .layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1632 },
Chia-I Wu76cd4222015-07-08 13:34:24 +08001633 };
1634 const VkFramebufferCreateInfo fb_info = {
1635 .sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO,
1636 .pNext = NULL,
Chia-I Wuc278df82015-07-07 11:50:03 +08001637 .renderPass = demo->render_pass,
1638 .attachmentCount = 2,
1639 .pAttachments = attachments,
Chia-I Wu76cd4222015-07-08 13:34:24 +08001640 .width = demo->width,
1641 .height = demo->height,
1642 .layers = 1,
1643 };
1644 VkResult U_ASSERT_ONLY err;
1645 uint32_t i;
1646
1647 for (i = 0; i < DEMO_BUFFER_COUNT; i++) {
Chia-I Wuc278df82015-07-07 11:50:03 +08001648 attachments[0].view = demo->buffers[i].view;
Chia-I Wu76cd4222015-07-08 13:34:24 +08001649 err = vkCreateFramebuffer(demo->device, &fb_info, &demo->framebuffers[i]);
1650 assert(!err);
1651 }
1652}
1653
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001654static void demo_prepare(struct demo *demo)
1655{
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001656 const VkCmdBufferCreateInfo cmd = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001657 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001658 .pNext = NULL,
Courtney Goeltzenleuchterf3168062015-03-05 18:09:39 -07001659 .queueNodeIndex = demo->graphics_queue_node_index,
Chia-I Wu88eaa3b2015-06-26 15:34:39 +08001660 .level = VK_CMD_BUFFER_LEVEL_PRIMARY,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001661 .flags = 0,
1662 };
Tony Barbour22a30862015-04-22 09:02:32 -06001663 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001664
1665 demo_prepare_buffers(demo);
1666 demo_prepare_depth(demo);
1667 demo_prepare_textures(demo);
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -06001668 demo_prepare_cube_data_buffer(demo);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001669
Chia-I Wuf8385062015-01-04 16:27:24 +08001670 demo_prepare_descriptor_layout(demo);
Chia-I Wu76cd4222015-07-08 13:34:24 +08001671 demo_prepare_render_pass(demo);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001672 demo_prepare_pipeline(demo);
1673 demo_prepare_dynamic_states(demo);
1674
Courtney Goeltzenleuchterbd3b3aa2015-02-17 09:48:44 -07001675 for (int i = 0; i < DEMO_BUFFER_COUNT; i++) {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001676 err = vkCreateCommandBuffer(demo->device, &cmd, &demo->buffers[i].cmd);
Courtney Goeltzenleuchterbd3b3aa2015-02-17 09:48:44 -07001677 assert(!err);
1678 }
Chia-I Wuf8385062015-01-04 16:27:24 +08001679
Chia-I Wu8d24b3b2015-03-26 13:14:16 +08001680 demo_prepare_descriptor_pool(demo);
Chia-I Wuf8385062015-01-04 16:27:24 +08001681 demo_prepare_descriptor_set(demo);
Courtney Goeltzenleuchterbd3b3aa2015-02-17 09:48:44 -07001682
Chia-I Wu76cd4222015-07-08 13:34:24 +08001683 demo_prepare_framebuffers(demo);
Courtney Goeltzenleuchterbd3b3aa2015-02-17 09:48:44 -07001684
1685 for (int i = 0; i < DEMO_BUFFER_COUNT; i++) {
1686 demo->current_buffer = i;
1687 demo_draw_build_cmd(demo, demo->buffers[i].cmd);
1688 }
1689
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -06001690 /*
1691 * Prepare functions above may generate pipeline commands
1692 * that need to be flushed before beginning the render loop.
1693 */
1694 demo_flush_init_cmd(demo);
1695
Courtney Goeltzenleuchterbd3b3aa2015-02-17 09:48:44 -07001696 demo->current_buffer = 0;
Jon Ashburn8a399e92015-04-24 09:46:24 -07001697 demo->prepared = true;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001698}
1699
David Pinedoeeca2a22015-06-18 17:03:14 -06001700static void demo_cleanup(struct demo *demo)
1701{
1702 uint32_t i;
1703
1704 demo->prepared = false;
1705
Tony Barbourde4124d2015-07-03 10:33:54 -06001706 for (i = 0; i < DEMO_BUFFER_COUNT; i++) {
1707 vkDestroyFramebuffer(demo->device, demo->framebuffers[i]);
1708 }
Tony Barbourb857d312015-07-10 10:50:45 -06001709 vkFreeDescriptorSets(demo->device, demo->desc_pool, 1, &demo->desc_set);
Tony Barbourde4124d2015-07-03 10:33:54 -06001710 vkDestroyDescriptorPool(demo->device, demo->desc_pool);
Chia-I Wu76cd4222015-07-08 13:34:24 +08001711
Tony Barbourde4124d2015-07-03 10:33:54 -06001712 vkDestroyDynamicViewportState(demo->device, demo->viewport);
1713 vkDestroyDynamicRasterState(demo->device, demo->raster);
1714 vkDestroyDynamicColorBlendState(demo->device, demo->color_blend);
1715 vkDestroyDynamicDepthStencilState(demo->device, demo->depth_stencil);
David Pinedoeeca2a22015-06-18 17:03:14 -06001716
Tony Barbourde4124d2015-07-03 10:33:54 -06001717 vkDestroyPipeline(demo->device, demo->pipeline);
Jon Ashburn0d60d272015-07-09 15:02:25 -06001718 vkDestroyPipelineCache(demo->device, demo->pipelineCache);
Tony Barbourde4124d2015-07-03 10:33:54 -06001719 vkDestroyRenderPass(demo->device, demo->render_pass);
1720 vkDestroyPipelineLayout(demo->device, demo->pipeline_layout);
1721 vkDestroyDescriptorSetLayout(demo->device, demo->desc_layout);
David Pinedoeeca2a22015-06-18 17:03:14 -06001722
1723 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Tony Barbourde4124d2015-07-03 10:33:54 -06001724 vkDestroyImageView(demo->device, demo->textures[i].view);
1725 vkDestroyImage(demo->device, demo->textures[i].image);
David Pinedoeeca2a22015-06-18 17:03:14 -06001726 vkFreeMemory(demo->device, demo->textures[i].mem);
Tony Barbourde4124d2015-07-03 10:33:54 -06001727 vkDestroySampler(demo->device, demo->textures[i].sampler);
David Pinedoeeca2a22015-06-18 17:03:14 -06001728 }
1729 demo->fpDestroySwapChainWSI(demo->swap_chain);
1730
Tony Barbourde4124d2015-07-03 10:33:54 -06001731 vkDestroyAttachmentView(demo->device, demo->depth.view);
1732 vkDestroyImage(demo->device, demo->depth.image);
David Pinedoeeca2a22015-06-18 17:03:14 -06001733 vkFreeMemory(demo->device, demo->depth.mem);
1734
Tony Barbourde4124d2015-07-03 10:33:54 -06001735 vkDestroyBufferView(demo->device, demo->uniform_data.view);
1736 vkDestroyBuffer(demo->device, demo->uniform_data.buf);
David Pinedoeeca2a22015-06-18 17:03:14 -06001737 vkFreeMemory(demo->device, demo->uniform_data.mem);
1738
1739 for (i = 0; i < DEMO_BUFFER_COUNT; i++) {
Tony Barbourde4124d2015-07-03 10:33:54 -06001740 vkDestroyAttachmentView(demo->device, demo->buffers[i].view);
1741 vkDestroyCommandBuffer(demo->device, demo->buffers[i].cmd);
David Pinedoeeca2a22015-06-18 17:03:14 -06001742 }
1743
1744 vkDestroyDevice(demo->device);
Tony Barboura65ecc22015-06-30 14:14:19 -06001745 if (demo->validate) {
1746 demo->dbgDestroyMsgCallback(demo->inst, demo->msg_callback);
1747 }
David Pinedoeeca2a22015-06-18 17:03:14 -06001748 vkDestroyInstance(demo->inst);
1749
1750#ifndef _WIN32
1751 xcb_destroy_window(demo->connection, demo->window);
1752 xcb_disconnect(demo->connection);
1753#endif // _WIN32
1754}
1755
1756// On MS-Windows, make this a global, so it's available to WndProc()
1757struct demo demo;
1758
Ian Elliotte14e9f92015-04-16 15:23:05 -06001759#ifdef _WIN32
1760static void demo_run(struct demo *demo)
1761{
Courtney Goeltzenleuchter857542b2015-04-27 14:56:34 -06001762 if (!demo->prepared)
1763 return;
Ian Elliotte14e9f92015-04-16 15:23:05 -06001764 // Wait for work to finish before updating MVP.
1765 vkDeviceWaitIdle(demo->device);
1766 demo_update_data_buffer(demo);
1767
1768 demo_draw(demo);
1769
1770 // Wait for work to finish before updating MVP.
1771 vkDeviceWaitIdle(demo->device);
Ian Elliotte14e9f92015-04-16 15:23:05 -06001772
David Pinedoeeca2a22015-06-18 17:03:14 -06001773 demo->curFrame++;
1774
1775 if (demo->frameCount != INT_MAX && demo->curFrame == demo->frameCount)
1776 {
1777 demo->quit=true;
1778 demo_cleanup(demo);
1779 ExitProcess(0);
1780 }
1781
1782}
Ian Elliotte14e9f92015-04-16 15:23:05 -06001783
1784// MS-Windows event handling function:
1785LRESULT CALLBACK WndProc(HWND hWnd,
1786 UINT uMsg,
1787 WPARAM wParam,
1788 LPARAM lParam)
1789{
Ian Elliotte14e9f92015-04-16 15:23:05 -06001790 switch(uMsg)
1791 {
Ian Elliotte14e9f92015-04-16 15:23:05 -06001792 case WM_CLOSE:
1793 PostQuitMessage(0);
Tony Barbour5685ad72015-04-29 16:19:20 -06001794 break;
Ian Elliotte14e9f92015-04-16 15:23:05 -06001795 case WM_PAINT:
1796 demo_run(&demo);
1797 return 0;
1798 default:
1799 break;
1800 }
1801 return (DefWindowProc(hWnd, uMsg, wParam, lParam));
1802}
1803
1804static void demo_create_window(struct demo *demo)
1805{
1806 WNDCLASSEX win_class;
1807
1808 // Initialize the window class structure:
1809 win_class.cbSize = sizeof(WNDCLASSEX);
1810 win_class.style = CS_HREDRAW | CS_VREDRAW;
1811 win_class.lpfnWndProc = WndProc;
1812 win_class.cbClsExtra = 0;
1813 win_class.cbWndExtra = 0;
1814 win_class.hInstance = demo->connection; // hInstance
1815 win_class.hIcon = LoadIcon(NULL, IDI_APPLICATION);
1816 win_class.hCursor = LoadCursor(NULL, IDC_ARROW);
1817 win_class.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
1818 win_class.lpszMenuName = NULL;
1819 win_class.lpszClassName = demo->name;
1820 win_class.hIconSm = LoadIcon(NULL, IDI_WINLOGO);
1821 // Register window class:
1822 if (!RegisterClassEx(&win_class)) {
1823 // It didn't work, so try to give a useful error:
1824 printf("Unexpected error trying to start the application!\n");
1825 fflush(stdout);
1826 exit(1);
1827 }
1828 // Create window with the registered class:
Mike Stroyanf5856292015-06-15 14:20:13 -06001829 RECT wr = { 0, 0, demo->width, demo->height };
1830 AdjustWindowRect(&wr, WS_OVERLAPPEDWINDOW, FALSE);
Ian Elliotte14e9f92015-04-16 15:23:05 -06001831 demo->window = CreateWindowEx(0,
1832 demo->name, // class name
1833 demo->name, // app name
1834 WS_OVERLAPPEDWINDOW | // window style
1835 WS_VISIBLE |
1836 WS_SYSMENU,
1837 100,100, // x/y coords
Mike Stroyanf5856292015-06-15 14:20:13 -06001838 wr.right-wr.left, // width
1839 wr.bottom-wr.top, // height
Ian Elliotte14e9f92015-04-16 15:23:05 -06001840 NULL, // handle to parent
1841 NULL, // handle to menu
1842 demo->connection, // hInstance
1843 NULL); // no extra parameters
1844 if (!demo->window) {
1845 // It didn't work, so try to give a useful error:
1846 printf("Cannot create a window in which to draw!\n");
1847 fflush(stdout);
1848 exit(1);
1849 }
1850}
1851#else // _WIN32
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001852static void demo_handle_event(struct demo *demo,
1853 const xcb_generic_event_t *event)
1854{
Piers Daniell886be472015-02-23 16:23:13 -07001855 uint8_t event_code = event->response_type & 0x7f;
Courtney Goeltzenleuchterca21a212014-11-06 14:27:52 -07001856 switch (event_code) {
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001857 case XCB_EXPOSE:
Courtney Goeltzenleuchter54611482014-11-18 11:28:09 -07001858 // TODO: Resize window
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001859 break;
Courtney Goeltzenleuchterca21a212014-11-06 14:27:52 -07001860 case XCB_CLIENT_MESSAGE:
1861 if((*(xcb_client_message_event_t*)event).data.data32[0] ==
1862 (*demo->atom_wm_delete_window).atom) {
1863 demo->quit = true;
1864 }
1865 break;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001866 case XCB_KEY_RELEASE:
1867 {
1868 const xcb_key_release_event_t *key =
1869 (const xcb_key_release_event_t *) event;
1870
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -06001871 switch (key->detail) {
1872 case 0x9: // Escape
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001873 demo->quit = true;
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -06001874 break;
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001875 case 0x71: // left arrow key
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -06001876 demo->spin_angle += demo->spin_increment;
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001877 break;
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -06001878 case 0x72: // right arrow key
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -06001879 demo->spin_angle -= demo->spin_increment;
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -06001880 break;
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -06001881 case 0x41:
1882 demo->pause = !demo->pause;
Piers Daniell886be472015-02-23 16:23:13 -07001883 break;
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -06001884 }
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001885 }
1886 break;
1887 default:
1888 break;
1889 }
1890}
1891
1892static void demo_run(struct demo *demo)
1893{
1894 xcb_flush(demo->connection);
1895
1896 while (!demo->quit) {
1897 xcb_generic_event_t *event;
1898
Courtney Goeltzenleuchter54611482014-11-18 11:28:09 -07001899 if (demo->pause) {
1900 event = xcb_wait_for_event(demo->connection);
1901 } else {
1902 event = xcb_poll_for_event(demo->connection);
1903 }
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001904 if (event) {
1905 demo_handle_event(demo, event);
1906 free(event);
Courtney Goeltzenleuchterbb3e1312014-11-10 11:13:13 -07001907 }
Courtney Goeltzenleuchter54611482014-11-18 11:28:09 -07001908
1909 // Wait for work to finish before updating MVP.
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001910 vkDeviceWaitIdle(demo->device);
Courtney Goeltzenleuchter54611482014-11-18 11:28:09 -07001911 demo_update_data_buffer(demo);
1912
Courtney Goeltzenleuchterbb3e1312014-11-10 11:13:13 -07001913 demo_draw(demo);
Courtney Goeltzenleuchter21f89972014-11-18 11:28:09 -07001914
Courtney Goeltzenleuchterbb3e1312014-11-10 11:13:13 -07001915 // Wait for work to finish before updating MVP.
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001916 vkDeviceWaitIdle(demo->device);
David Pinedoeeca2a22015-06-18 17:03:14 -06001917 demo->curFrame++;
1918 if (demo->frameCount != INT_MAX && demo->curFrame == demo->frameCount)
1919 demo->quit = true;
1920
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001921 }
1922}
1923
1924static void demo_create_window(struct demo *demo)
1925{
1926 uint32_t value_mask, value_list[32];
1927
1928 demo->window = xcb_generate_id(demo->connection);
1929
1930 value_mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK;
1931 value_list[0] = demo->screen->black_pixel;
1932 value_list[1] = XCB_EVENT_MASK_KEY_RELEASE |
1933 XCB_EVENT_MASK_EXPOSURE;
1934
1935 xcb_create_window(demo->connection,
1936 XCB_COPY_FROM_PARENT,
1937 demo->window, demo->screen->root,
1938 0, 0, demo->width, demo->height, 0,
1939 XCB_WINDOW_CLASS_INPUT_OUTPUT,
1940 demo->screen->root_visual,
1941 value_mask, value_list);
1942
Courtney Goeltzenleuchterca21a212014-11-06 14:27:52 -07001943 /* Magic code that will send notification when window is destroyed */
1944 xcb_intern_atom_cookie_t cookie = xcb_intern_atom(demo->connection, 1, 12,
1945 "WM_PROTOCOLS");
1946 xcb_intern_atom_reply_t* reply = xcb_intern_atom_reply(demo->connection, cookie, 0);
1947
1948 xcb_intern_atom_cookie_t cookie2 = xcb_intern_atom(demo->connection, 0, 16, "WM_DELETE_WINDOW");
1949 demo->atom_wm_delete_window = xcb_intern_atom_reply(demo->connection, cookie2, 0);
1950
1951 xcb_change_property(demo->connection, XCB_PROP_MODE_REPLACE,
1952 demo->window, (*reply).atom, 4, 32, 1,
1953 &(*demo->atom_wm_delete_window).atom);
1954 free(reply);
1955
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001956 xcb_map_window(demo->connection, demo->window);
David Pinedoeeca2a22015-06-18 17:03:14 -06001957
1958 // Force the x/y coordinates to 100,100 results are identical in consecutive runs
1959 const uint32_t coords[] = {100, 100};
1960 xcb_configure_window(demo->connection, demo->window,
1961 XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y, coords);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001962}
Ian Elliotte14e9f92015-04-16 15:23:05 -06001963#endif // _WIN32
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001964
Courtney Goeltzenleuchter8c15fc52015-07-06 17:46:11 -06001965/*
1966 * Return 1 (true) if all layer names specified in check_names
1967 * can be found in given layer properties.
1968 */
Courtney Goeltzenleuchter1f41f542015-07-09 11:44:38 -06001969static VkBool32 demo_check_layers(uint32_t check_count, char **check_names,
Courtney Goeltzenleuchter8c15fc52015-07-06 17:46:11 -06001970 uint32_t layer_count, VkLayerProperties *layers)
1971{
1972 for (uint32_t i = 0; i < check_count; i++) {
Courtney Goeltzenleuchter1f41f542015-07-09 11:44:38 -06001973 VkBool32 found = 0;
Courtney Goeltzenleuchter8c15fc52015-07-06 17:46:11 -06001974 for (uint32_t j = 0; j < layer_count; j++) {
1975 if (!strcmp(check_names[i], layers[j].layerName)) {
1976 found = 1;
1977 }
1978 }
1979 if (!found) {
1980 fprintf(stderr, "Cannot find layer: %s\n", check_names[i]);
1981 return 0;
1982 }
1983 }
1984 return 1;
1985}
1986
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001987static void demo_init_vk(struct demo *demo)
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001988{
Tobin Ehlis3536b442015-04-16 18:04:57 -06001989 VkResult err;
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001990 char *extension_names[64];
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -06001991 VkExtensionProperties *instance_extensions;
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001992 VkLayerProperties *instance_layers;
1993 VkLayerProperties *device_layers;
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -06001994 uint32_t instance_extension_count = 0;
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001995 uint32_t instance_layer_count = 0;
1996 uint32_t enabled_extension_count = 0;
1997 uint32_t enabled_layer_count = 0;
1998
Courtney Goeltzenleuchter8c15fc52015-07-06 17:46:11 -06001999 char *instance_validation_layers[] = {
2000 "MemTracker",
2001 };
2002
2003 char *device_validation_layers[] = {
2004 "MemTracker",
2005 };
2006
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002007 /* Look for validation layers */
Courtney Goeltzenleuchter1f41f542015-07-09 11:44:38 -06002008 VkBool32 validation_found = 0;
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002009 err = vkGetGlobalLayerProperties(&instance_layer_count, NULL);
Tobin Ehlis3536b442015-04-16 18:04:57 -06002010 assert(!err);
2011
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002012 instance_layers = malloc(sizeof(VkLayerProperties) * instance_layer_count);
2013 err = vkGetGlobalLayerProperties(&instance_layer_count, instance_layers);
2014 assert(!err);
Courtney Goeltzenleuchter8c15fc52015-07-06 17:46:11 -06002015
2016 if (demo->validate) {
2017 validation_found = demo_check_layers(ARRAY_SIZE(instance_validation_layers), instance_validation_layers,
2018 instance_layer_count, instance_layers);
2019 if (!validation_found) {
2020 ERR_EXIT("vkGetGlobalLayerProperties failed to find"
2021 "required validation layer.\n\n"
2022 "Please look at the Getting Started guide for additional "
2023 "information.\n",
2024 "vkCreateInstance Failure");
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002025 }
Courtney Goeltzenleuchter8c15fc52015-07-06 17:46:11 -06002026 enabled_layer_count = ARRAY_SIZE(instance_validation_layers);
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002027 }
2028
2029 err = vkGetGlobalExtensionProperties(NULL, &instance_extension_count, NULL);
2030 assert(!err);
2031
Courtney Goeltzenleuchter1f41f542015-07-09 11:44:38 -06002032 VkBool32 WSIextFound = 0;
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002033 memset(extension_names, 0, sizeof(extension_names));
2034 instance_extensions = malloc(sizeof(VkExtensionProperties) * instance_extension_count);
2035 err = vkGetGlobalExtensionProperties(NULL, &instance_extension_count, instance_extensions);
2036 assert(!err);
2037 for (uint32_t i = 0; i < instance_extension_count; i++) {
2038 if (!strcmp(VK_WSI_LUNARG_EXTENSION_NAME, instance_extensions[i].extName)) {
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -06002039 WSIextFound = 1;
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002040 extension_names[enabled_extension_count++] = VK_WSI_LUNARG_EXTENSION_NAME;
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -06002041 }
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002042 if (!strcmp(DEBUG_REPORT_EXTENSION_NAME, instance_extensions[i].extName)) {
2043 if (demo->validate) {
2044 extension_names[enabled_extension_count++] = DEBUG_REPORT_EXTENSION_NAME;
2045 }
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -06002046 }
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002047 assert(enabled_extension_count < 64);
Tobin Ehlis3536b442015-04-16 18:04:57 -06002048 }
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -06002049 if (!WSIextFound) {
Tony Barbour426b9052015-06-24 16:06:58 -06002050 ERR_EXIT("vkGetGlobalExtensionProperties failed to find the "
Ian Elliott3b375cf2015-04-28 13:22:33 -06002051 "\"VK_WSI_LunarG\" extension.\n\nDo you have a compatible "
2052 "Vulkan installable client driver (ICD) installed?\nPlease "
2053 "look at the Getting Started guide for additional "
2054 "information.\n",
2055 "vkCreateInstance Failure");
2056 }
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06002057 const VkApplicationInfo app = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06002058 .sType = VK_STRUCTURE_TYPE_APPLICATION_INFO,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002059 .pNext = NULL,
Ian Elliott4e19ed02015-04-28 10:52:52 -06002060 .pAppName = APP_SHORT_NAME,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002061 .appVersion = 0,
Ian Elliott4e19ed02015-04-28 10:52:52 -06002062 .pEngineName = APP_SHORT_NAME,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002063 .engineVersion = 0,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06002064 .apiVersion = VK_API_VERSION,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002065 };
Tony Barbour5685ad72015-04-29 16:19:20 -06002066 VkInstanceCreateInfo inst_info = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06002067 .sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
Jon Ashburn29669a42015-04-04 14:52:07 -06002068 .pNext = NULL,
2069 .pAppInfo = &app,
2070 .pAllocCb = NULL,
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002071 .layerCount = enabled_layer_count,
Courtney Goeltzenleuchter8c15fc52015-07-06 17:46:11 -06002072 .ppEnabledLayerNames = (const char *const*) ((demo->validate) ? instance_validation_layers : NULL),
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002073 .extensionCount = enabled_extension_count,
2074 .ppEnabledExtensionNames = (const char *const*) extension_names,
Jon Ashburn29669a42015-04-04 14:52:07 -06002075 };
Courtney Goeltzenleuchterddcb6192015-04-14 18:48:46 -06002076 const VkDeviceQueueCreateInfo queue = {
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002077 .queueNodeIndex = 0,
2078 .queueCount = 1,
2079 };
Ian Elliottff6dab52015-04-28 11:35:02 -06002080
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06002081 uint32_t gpu_count;
2082 uint32_t i;
Courtney Goeltzenleuchterf3168062015-03-05 18:09:39 -07002083 uint32_t queue_count;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002084
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06002085 err = vkCreateInstance(&inst_info, &demo->inst);
Ian Elliottcaa9f272015-04-28 11:35:02 -06002086 if (err == VK_ERROR_INCOMPATIBLE_DRIVER) {
2087 ERR_EXIT("Cannot find a compatible Vulkan installable client driver "
Ian Elliott3b375cf2015-04-28 13:22:33 -06002088 "(ICD).\n\nPlease look at the Getting Started guide for "
Ian Elliottcaa9f272015-04-28 11:35:02 -06002089 "additional information.\n",
2090 "vkCreateInstance Failure");
Tony Barbour5685ad72015-04-29 16:19:20 -06002091 } else if (err == VK_ERROR_INVALID_EXTENSION) {
2092 ERR_EXIT("Cannot find a specified extension library"
2093 ".\nMake sure your layers path is set appropriately\n",
2094 "vkCreateInstance Failure");
Ian Elliottcaa9f272015-04-28 11:35:02 -06002095 } else if (err) {
Ian Elliott3b375cf2015-04-28 13:22:33 -06002096 ERR_EXIT("vkCreateInstance failed.\n\nDo you have a compatible Vulkan "
2097 "installable client driver (ICD) installed?\nPlease look at "
Ian Elliottcaa9f272015-04-28 11:35:02 -06002098 "the Getting Started guide for additional information.\n",
2099 "vkCreateInstance Failure");
Ian Elliottdfe55f72015-04-03 15:24:55 -06002100 }
Jon Ashburn29669a42015-04-04 14:52:07 -06002101
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002102 free(instance_layers);
2103 free(instance_extensions);
Tony Barbour5685ad72015-04-29 16:19:20 -06002104
Jon Ashburn07b309a2015-04-15 11:31:12 -06002105 gpu_count = 1;
2106 err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, &demo->gpu);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002107 assert(!err && gpu_count == 1);
2108
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002109 /* Look for validation layers */
2110 validation_found = 0;
2111 enabled_layer_count = 0;
2112 uint32_t device_layer_count = 0;
2113 err = vkGetPhysicalDeviceLayerProperties(demo->gpu, &device_layer_count, NULL);
2114 assert(!err);
2115
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002116 device_layers = malloc(sizeof(VkLayerProperties) * device_layer_count);
2117 err = vkGetPhysicalDeviceLayerProperties(demo->gpu, &device_layer_count, device_layers);
2118 assert(!err);
Courtney Goeltzenleuchter8c15fc52015-07-06 17:46:11 -06002119
2120 if (demo->validate) {
2121 validation_found = demo_check_layers(ARRAY_SIZE(device_validation_layers), device_validation_layers,
2122 device_layer_count, device_layers);
2123 if (!validation_found) {
2124 ERR_EXIT("vkGetPhysicalDeviceLayerProperties failed to find"
2125 "a required validation layer.\n\n"
2126 "Please look at the Getting Started guide for additional "
2127 "information.\n",
2128 "vkCreateDevice Failure");
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002129 }
Courtney Goeltzenleuchter8c15fc52015-07-06 17:46:11 -06002130 enabled_layer_count = ARRAY_SIZE(device_validation_layers);
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002131 }
2132
2133 uint32_t device_extension_count = 0;
2134 VkExtensionProperties *device_extensions = NULL;
2135 err = vkGetPhysicalDeviceExtensionProperties(
2136 demo->gpu, NULL, &device_extension_count, NULL);
2137 assert(!err);
2138
2139 WSIextFound = 0;
2140 enabled_extension_count = 0;
2141 memset(extension_names, 0, sizeof(extension_names));
2142 device_extensions = malloc(sizeof(VkExtensionProperties) * device_extension_count);
2143 err = vkGetPhysicalDeviceExtensionProperties(
2144 demo->gpu, NULL, &device_extension_count, device_extensions);
2145 assert(!err);
Courtney Goeltzenleuchter37d091e2015-07-05 22:08:51 -06002146#if 0
2147 /* Will need this check in future */
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002148 for (uint32_t i = 0; i < device_extension_count; i++) {
2149 if (!strcmp(VK_WSI_LUNARG_EXTENSION_NAME, device_extensions[i].extName)) {
2150 WSIextFound = 1;
2151 extension_names[enabled_extension_count++] = VK_WSI_LUNARG_EXTENSION_NAME;
2152 }
2153 assert(enabled_extension_count < 64);
2154 }
2155 if (!WSIextFound) {
2156 ERR_EXIT("vkGetGlobalExtensionProperties failed to find the "
2157 "\"VK_WSI_LunarG\" extension.\n\nDo you have a compatible "
2158 "Vulkan installable client driver (ICD) installed?\nPlease "
2159 "look at the Getting Started guide for additional "
2160 "information.\n",
2161 "vkCreateInstance Failure");
2162 }
Courtney Goeltzenleuchter37d091e2015-07-05 22:08:51 -06002163#endif
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002164
2165 VkDeviceCreateInfo device = {
2166 .sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
2167 .pNext = NULL,
2168 .queueRecordCount = 1,
2169 .pRequestedQueues = &queue,
2170 .layerCount = enabled_layer_count,
Courtney Goeltzenleuchter8c15fc52015-07-06 17:46:11 -06002171 .ppEnabledLayerNames = (const char *const*) ((demo->validate) ? device_validation_layers : NULL),
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002172 .extensionCount = enabled_extension_count,
2173 .ppEnabledExtensionNames = (const char *const*) extension_names,
2174 .flags = 0,
2175 };
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -06002176
Tony Barbour5685ad72015-04-29 16:19:20 -06002177 if (demo->validate) {
Tony Barboura65ecc22015-06-30 14:14:19 -06002178 demo->dbgCreateMsgCallback = vkGetInstanceProcAddr(demo->inst, "vkDbgCreateMsgCallback");
2179 demo->dbgDestroyMsgCallback = vkGetInstanceProcAddr(demo->inst, "vkDbgDestroyMsgCallback");
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -06002180 if (!demo->dbgCreateMsgCallback) {
2181 ERR_EXIT("GetProcAddr: Unable to find vkDbgCreateMsgCallback\n",
2182 "vkGetProcAddr Failure");
2183 }
Tony Barboura65ecc22015-06-30 14:14:19 -06002184 if (!demo->dbgDestroyMsgCallback) {
2185 ERR_EXIT("GetProcAddr: Unable to find vkDbgDestroyMsgCallback\n",
2186 "vkGetProcAddr Failure");
2187 }
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -06002188 err = demo->dbgCreateMsgCallback(
2189 demo->inst,
2190 VK_DBG_REPORT_ERROR_BIT | VK_DBG_REPORT_WARN_BIT,
2191 dbgFunc, NULL,
2192 &demo->msg_callback);
2193 switch (err) {
2194 case VK_SUCCESS:
2195 break;
2196 case VK_ERROR_INVALID_POINTER:
2197 ERR_EXIT("dbgCreateMsgCallback: Invalid pointer\n",
2198 "dbgCreateMsgCallback Failure");
2199 break;
2200 case VK_ERROR_OUT_OF_HOST_MEMORY:
2201 ERR_EXIT("dbgCreateMsgCallback: out of host memory\n",
2202 "dbgCreateMsgCallback Failure");
2203 break;
2204 default:
2205 ERR_EXIT("dbgCreateMsgCallback: unknown failure\n",
2206 "dbgCreateMsgCallback Failure");
2207 break;
2208 }
Tony Barbour5685ad72015-04-29 16:19:20 -06002209 }
2210
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06002211 err = vkCreateDevice(demo->gpu, &device, &demo->device);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002212 assert(!err);
2213
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002214 free(device_layers);
2215
Ian Elliott1b6de092015-06-22 15:07:49 -06002216 GET_DEVICE_PROC_ADDR(demo->device, CreateSwapChainWSI);
2217 GET_DEVICE_PROC_ADDR(demo->device, CreateSwapChainWSI);
2218 GET_DEVICE_PROC_ADDR(demo->device, DestroySwapChainWSI);
2219 GET_DEVICE_PROC_ADDR(demo->device, GetSwapChainInfoWSI);
2220 GET_DEVICE_PROC_ADDR(demo->device, QueuePresentWSI);
Jon Ashburncedc15f2015-05-21 18:13:33 -06002221
Tony Barbour426b9052015-06-24 16:06:58 -06002222 err = vkGetPhysicalDeviceProperties(demo->gpu, &demo->gpu_props);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -06002223 assert(!err);
2224
Tony Barbour426b9052015-06-24 16:06:58 -06002225 err = vkGetPhysicalDeviceQueueCount(demo->gpu, &queue_count);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -06002226 assert(!err);
2227
Tony Barbour426b9052015-06-24 16:06:58 -06002228 demo->queue_props = (VkPhysicalDeviceQueueProperties *) malloc(queue_count * sizeof(VkPhysicalDeviceQueueProperties));
2229 err = vkGetPhysicalDeviceQueueProperties(demo->gpu, queue_count, demo->queue_props);
Courtney Goeltzenleuchterf3168062015-03-05 18:09:39 -07002230 assert(!err);
Courtney Goeltzenleuchterf3168062015-03-05 18:09:39 -07002231 assert(queue_count >= 1);
2232
Mark Lobodzinskicf26e072015-04-16 11:44:05 -05002233 // Graphics queue and MemMgr queue can be separate.
2234 // TODO: Add support for separate queues, including synchronization,
Mark Lobodzinskifb9f5642015-05-11 17:21:15 -05002235 // and appropriate tracking for QueueSubmit
Courtney Goeltzenleuchterf3168062015-03-05 18:09:39 -07002236 for (i = 0; i < queue_count; i++) {
Mark Lobodzinskifb9f5642015-05-11 17:21:15 -05002237 if (demo->queue_props[i].queueFlags & VK_QUEUE_GRAPHICS_BIT)
Courtney Goeltzenleuchterf3168062015-03-05 18:09:39 -07002238 break;
2239 }
2240 assert(i < queue_count);
2241 demo->graphics_queue_node_index = i;
2242
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06002243 err = vkGetDeviceQueue(demo->device, demo->graphics_queue_node_index,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002244 0, &demo->queue);
2245 assert(!err);
Tony Barbour5685ad72015-04-29 16:19:20 -06002246
Jon Ashburnba4a1952015-06-16 12:44:51 -06002247 // for now hardcode format till get WSI support
2248 demo->format = VK_FORMAT_B8G8R8A8_UNORM;
Ian Elliott32536f92015-04-21 16:41:02 -06002249
David Pinedoeeca2a22015-06-18 17:03:14 -06002250 demo->quit = false;
2251 demo->curFrame = 0;
Mark Lobodzinski72346292015-07-02 16:49:40 -06002252
2253 // Get Memory information and properties
2254 err = vkGetPhysicalDeviceMemoryProperties(demo->gpu, &demo->memory_properties);
2255 assert(!err);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002256}
2257
2258static void demo_init_connection(struct demo *demo)
2259{
Ian Elliotte14e9f92015-04-16 15:23:05 -06002260#ifndef _WIN32
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002261 const xcb_setup_t *setup;
2262 xcb_screen_iterator_t iter;
2263 int scr;
2264
2265 demo->connection = xcb_connect(NULL, &scr);
Ian Elliottdfe55f72015-04-03 15:24:55 -06002266 if (demo->connection == NULL) {
2267 printf("Cannot find a compatible Vulkan installable client driver "
2268 "(ICD).\nExiting ...\n");
2269 fflush(stdout);
2270 exit(1);
2271 }
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002272
2273 setup = xcb_get_setup(demo->connection);
2274 iter = xcb_setup_roots_iterator(setup);
2275 while (scr-- > 0)
2276 xcb_screen_next(&iter);
2277
2278 demo->screen = iter.data;
Ian Elliotte14e9f92015-04-16 15:23:05 -06002279#endif // _WIN32
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002280}
2281
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07002282static void demo_init(struct demo *demo, int argc, char **argv)
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002283{
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -06002284 vec3 eye = {0.0f, 3.0f, 5.0f};
2285 vec3 origin = {0, 0, 0};
Chia-I Wuc3487c22015-04-22 14:56:17 +08002286 vec3 up = {0.0f, 1.0f, 0.0};
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -06002287
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002288 memset(demo, 0, sizeof(*demo));
David Pinedoeeca2a22015-06-18 17:03:14 -06002289 demo->frameCount = INT_MAX;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002290
Piers Daniell886be472015-02-23 16:23:13 -07002291 for (int i = 1; i < argc; i++) {
Tony Barbour5685ad72015-04-29 16:19:20 -06002292 if (strcmp(argv[i], "--use_staging") == 0) {
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07002293 demo->use_staging_buffer = true;
Tony Barbour5685ad72015-04-29 16:19:20 -06002294 continue;
Ian Elliotte14e9f92015-04-16 15:23:05 -06002295 }
Cody Northrop75db0322015-05-28 11:27:16 -06002296 if (strcmp(argv[i], "--use_glsl") == 0) {
2297 demo->use_glsl = true;
2298 continue;
2299 }
Tony Barbour5685ad72015-04-29 16:19:20 -06002300 if (strcmp(argv[i], "--validate") == 0) {
2301 demo->validate = true;
2302 continue;
2303 }
David Pinedoeeca2a22015-06-18 17:03:14 -06002304 if (strcmp(argv[i], "--c") == 0 &&
2305 demo->frameCount == INT_MAX &&
2306 i < argc-1 &&
2307 sscanf(argv[i+1],"%d", &demo->frameCount) == 1 &&
2308 demo->frameCount >= 0)
2309 {
2310 i++;
2311 continue;
2312 }
Tony Barbour5685ad72015-04-29 16:19:20 -06002313
David Pinedoeeca2a22015-06-18 17:03:14 -06002314 fprintf(stderr, "Usage:\n %s [--use_staging] [--validate] [--c <framecount>]\n", APP_SHORT_NAME);
Ian Elliotte14e9f92015-04-16 15:23:05 -06002315 fflush(stderr);
2316 exit(1);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07002317 }
2318
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002319 demo_init_connection(demo);
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06002320 demo_init_vk(demo);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002321
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -06002322 demo->width = 500;
2323 demo->height = 500;
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -06002324
2325 demo->spin_angle = 0.01f;
2326 demo->spin_increment = 0.01f;
2327 demo->pause = false;
2328
Piers Daniell886be472015-02-23 16:23:13 -07002329 mat4x4_perspective(demo->projection_matrix, (float)degreesToRadians(45.0f), 1.0f, 0.1f, 100.0f);
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -06002330 mat4x4_look_at(demo->view_matrix, eye, origin, up);
2331 mat4x4_identity(demo->model_matrix);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002332}
2333
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002334
Ian Elliotte14e9f92015-04-16 15:23:05 -06002335#ifdef _WIN32
Tony Barbour5685ad72015-04-29 16:19:20 -06002336extern int __getmainargs(
2337 int * _Argc,
2338 char *** _Argv,
2339 char *** _Env,
2340 int _DoWildCard,
2341 int * new_mode);
Ian Elliott7595eee2015-04-28 10:33:11 -06002342
Ian Elliott421107f2015-04-28 15:50:36 -06002343int WINAPI WinMain(HINSTANCE hInstance,
2344 HINSTANCE hPrevInstance,
2345 LPSTR pCmdLine,
2346 int nCmdShow)
Ian Elliotte14e9f92015-04-16 15:23:05 -06002347{
2348 MSG msg; // message
2349 bool done; // flag saying when app is complete
Tony Barbour5685ad72015-04-29 16:19:20 -06002350 int argc;
2351 char** argv;
2352 char** env;
2353 int new_mode = 0;
Ian Elliotte14e9f92015-04-16 15:23:05 -06002354
Tony Barbour5685ad72015-04-29 16:19:20 -06002355 __getmainargs(&argc,&argv,&env,0,&new_mode);
2356
2357 demo_init(&demo, argc, argv);
2358 demo.connection = hInstance;
2359 strncpy(demo.name, "cube", APP_NAME_STR_LEN);
Ian Elliotte14e9f92015-04-16 15:23:05 -06002360 demo_create_window(&demo);
2361
2362 demo_prepare(&demo);
2363
2364 done = false; //initialize loop condition variable
2365 /* main message loop*/
2366 while(!done)
2367 {
Ian Elliott421107f2015-04-28 15:50:36 -06002368 PeekMessage(&msg, NULL, 0, 0, PM_REMOVE);
Ian Elliotte14e9f92015-04-16 15:23:05 -06002369 if (msg.message == WM_QUIT) //check for a quit message
2370 {
2371 done = true; //if found, quit app
2372 }
2373 else
2374 {
2375 /* Translate and dispatch to event queue*/
2376 TranslateMessage(&msg);
2377 DispatchMessage(&msg);
2378 }
2379 }
2380
2381 demo_cleanup(&demo);
2382
Tony Barboura938abb2015-04-22 11:36:22 -06002383 return (int) msg.wParam;
Ian Elliotte14e9f92015-04-16 15:23:05 -06002384}
2385#else // _WIN32
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07002386int main(int argc, char **argv)
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002387{
2388 struct demo demo;
2389
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07002390 demo_init(&demo, argc, argv);
Chia-I Wu5b66aa52015-04-16 22:02:10 +08002391 demo_create_window(&demo);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002392
2393 demo_prepare(&demo);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002394 demo_run(&demo);
2395
2396 demo_cleanup(&demo);
2397
2398 return 0;
2399}
Ian Elliotte14e9f92015-04-16 15:23:05 -06002400#endif // _WIN32