blob: 9db21d9610d02741e1a2201ff3331f02c4876a76 [file] [log] [blame]
Ian Elliotta748eaf2015-04-28 15:50:36 -06001/*
2 * Vulkan
3 *
4 * Copyright (C) 2014-2015 LunarG, Inc.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 */
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -060024#define _GNU_SOURCE
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060025#include <stdio.h>
26#include <stdlib.h>
27#include <string.h>
28#include <stdbool.h>
29#include <assert.h>
30
Ian Elliott639ca472015-04-16 15:23:05 -060031#ifdef _WIN32
32#pragma comment(linker, "/subsystem:windows")
33#include <windows.h>
34#define APP_NAME_STR_LEN 80
35#else // _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060036#include <xcb/xcb.h>
Ian Elliott639ca472015-04-16 15:23:05 -060037#endif // _WIN32
38
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -060039#include <vulkan.h>
Chia-I Wucbb564e2015-04-16 22:02:10 +080040#include <vk_wsi_lunarg.h>
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -060041#include "vk_debug_report_lunarg.h"
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060042
Cody Northropfe3d8bc2015-03-17 14:54:35 -060043#include "icd-spv.h"
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060044
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -060045#include "linmath.h"
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -060046#include <png.h>
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -060047
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060048#define DEMO_BUFFER_COUNT 2
49#define DEMO_TEXTURE_COUNT 1
Ian Elliott44e33f72015-04-28 10:52:52 -060050#define APP_SHORT_NAME "cube"
51#define APP_LONG_NAME "The Vulkan Cube Demo Program"
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060052
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -060053#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
54
Tony Barbourfdc2d352015-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 Elliott07264132015-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 Elliott673898b2015-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 Goeltzenleuchter97db1c12014-10-30 15:14:16 -060092/*
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -070093 * structure to track all objects related to a texture.
94 */
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -060095struct texture_object {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -060096 VkSampler sampler;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -070097
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -060098 VkImage image;
99 VkImageLayout imageLayout;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600100
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500101 VkDeviceMemory mem;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600102 VkImageView view;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700103 int32_t tex_width, tex_height;
104};
105
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600106static char *tex_files[] = {
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -0600107 "lunarg-logo-256x256-solid.png"
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600108};
109
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600110struct vkcube_vs_uniform {
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600111 // Must start with MVP
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600112 float mvp[4][4];
113 float position[12*3][4];
114 float color[12*3][4];
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600115};
116
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600117struct vktexcube_vs_uniform {
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600118 // Must start with MVP
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600119 float mvp[4][4];
120 float position[12*3][4];
121 float attr[12*3][4];
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600122};
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600123
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600124//--------------------------------------------------------------------------------------
125// Mesh and VertexFormat Data
126//--------------------------------------------------------------------------------------
127struct Vertex
128{
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600129 float posX, posY, posZ, posW; // Position data
130 float r, g, b, a; // Color
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600131};
132
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600133struct VertexPosTex
134{
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600135 float posX, posY, posZ, posW; // Position data
136 float u, v, s, t; // Texcoord
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600137};
138
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600139#define XYZ1(_x_, _y_, _z_) (_x_), (_y_), (_z_), 1.f
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600140#define UV(_u_, _v_) (_u_), (_v_), 0.f, 1.f
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600141
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600142static const float g_vertex_buffer_data[] = {
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800143 -1.0f,-1.0f,-1.0f, // -X side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600144 -1.0f,-1.0f, 1.0f,
145 -1.0f, 1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800146 -1.0f, 1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600147 -1.0f, 1.0f,-1.0f,
148 -1.0f,-1.0f,-1.0f,
149
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800150 -1.0f,-1.0f,-1.0f, // -Z side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600151 1.0f, 1.0f,-1.0f,
152 1.0f,-1.0f,-1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800153 -1.0f,-1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600154 -1.0f, 1.0f,-1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600155 1.0f, 1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600156
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800157 -1.0f,-1.0f,-1.0f, // -Y side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600158 1.0f,-1.0f,-1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600159 1.0f,-1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800160 -1.0f,-1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600161 1.0f,-1.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600162 -1.0f,-1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600163
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800164 -1.0f, 1.0f,-1.0f, // +Y side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600165 -1.0f, 1.0f, 1.0f,
166 1.0f, 1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800167 -1.0f, 1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600168 1.0f, 1.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600169 1.0f, 1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600170
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800171 1.0f, 1.0f,-1.0f, // +X side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600172 1.0f, 1.0f, 1.0f,
173 1.0f,-1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800174 1.0f,-1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600175 1.0f,-1.0f,-1.0f,
176 1.0f, 1.0f,-1.0f,
177
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800178 -1.0f, 1.0f, 1.0f, // +Z side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600179 -1.0f,-1.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600180 1.0f, 1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800181 -1.0f,-1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600182 1.0f,-1.0f, 1.0f,
183 1.0f, 1.0f, 1.0f,
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600184};
185
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600186static const float g_uv_buffer_data[] = {
Chia-I Wuae3b55d2015-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 Goeltzenleuchter68369392014-10-29 15:59:49 -0600205 0.0f, 0.0f,
206 0.0f, 1.0f,
207
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800208 1.0f, 1.0f, // +Y side
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600209 0.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800210 0.0f, 0.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600211 1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600212 0.0f, 0.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600213 1.0f, 0.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600214
Chia-I Wuae3b55d2015-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 Goeltzenleuchter68369392014-10-29 15:59:49 -0600219 1.0f, 0.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600220 1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600221
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800222 0.0f, 1.0f, // +Z side
223 0.0f, 0.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600224 1.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600225 0.0f, 0.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800226 1.0f, 0.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600227 1.0f, 1.0f,
Courtney Goeltzenleuchter4984d152014-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 Goeltzenleuchter922a0fa2015-06-10 17:39:03 -0600250void dbgFunc(
251 VkFlags msgFlags,
252 VkObjectType objType,
253 VkObject srcObject,
254 size_t location,
255 int32_t msgCode,
256 const char* pLayerPrefix,
257 const char* pMsg,
258 void* pUserData)
Tony Barbour3dddd5d2015-04-29 16:19:20 -0600259{
260 char *message = (char *) malloc(strlen(pMsg)+100);
261
262 assert (message);
263
Courtney Goeltzenleuchter922a0fa2015-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 Barbourd70b42c2015-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 Goeltzenleuchter922a0fa2015-06-10 17:39:03 -0600271 sprintf(message,"WARNING: [%s] Code %d : %s", pLayerPrefix, msgCode, pMsg);
Tony Barbour3dddd5d2015-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 Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600285struct demo {
Ian Elliott639ca472015-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 Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600292 xcb_connection_t *connection;
293 xcb_screen_t *screen;
Chia-I Wucbb564e2015-04-16 22:02:10 +0800294 xcb_window_t window;
295 xcb_intern_atom_reply_t *atom_wm_delete_window;
Ian Elliott639ca472015-04-16 15:23:05 -0600296#endif // _WIN32
Cody Northrop1fedb212015-05-28 11:27:16 -0600297 bool prepared;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700298 bool use_staging_buffer;
Cody Northrop1fedb212015-05-28 11:27:16 -0600299 bool use_glsl;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600300
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600301 VkInstance inst;
Tony Barbour72304ef2015-04-16 15:59:00 -0600302 VkPhysicalDevice gpu;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600303 VkDevice device;
304 VkQueue queue;
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -0700305 uint32_t graphics_queue_node_index;
Tony Barbourecf1b2b2015-06-24 16:06:58 -0600306 VkPhysicalDeviceProperties gpu_props;
Tony Barbour72304ef2015-04-16 15:59:00 -0600307 VkPhysicalDeviceQueueProperties *queue_props;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600308 VkPhysicalDeviceMemoryProperties memory_properties;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600309
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600310 VkFramebuffer framebuffer;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600311 int width, height;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600312 VkFormat format;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600313
Jon Ashburn0b85d052015-05-21 18:13:33 -0600314 PFN_vkCreateSwapChainWSI fpCreateSwapChainWSI;
315 PFN_vkDestroySwapChainWSI fpDestroySwapChainWSI;
316 PFN_vkGetSwapChainInfoWSI fpGetSwapChainInfoWSI;
317 PFN_vkQueuePresentWSI fpQueuePresentWSI;
Chia-I Wucbb564e2015-04-16 22:02:10 +0800318 VkSwapChainWSI swap_chain;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600319 struct {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600320 VkImage image;
Tony Barbour72304ef2015-04-16 15:59:00 -0600321 VkDeviceMemory mem;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600322 VkCmdBuffer cmd;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600323
Chia-I Wua74c5b22015-07-07 11:50:03 +0800324 VkAttachmentView view;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600325 } buffers[DEMO_BUFFER_COUNT];
326
327 struct {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600328 VkFormat format;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600329
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600330 VkImage image;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500331 VkDeviceMemory mem;
Chia-I Wua74c5b22015-07-07 11:50:03 +0800332 VkAttachmentView view;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600333 } depth;
334
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600335 struct texture_object textures[DEMO_TEXTURE_COUNT];
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600336
337 struct {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600338 VkBuffer buf;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500339 VkDeviceMemory mem;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600340 VkBufferView view;
Chia-I Wuae721ba2015-05-25 16:27:55 +0800341 VkDescriptorInfo desc;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600342 } uniform_data;
343
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600344 VkCmdBuffer cmd; // Buffer for initialization commands
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -0500345 VkPipelineLayout pipeline_layout;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600346 VkDescriptorSetLayout desc_layout;
Jon Ashburnc4ab7af2015-07-09 15:02:25 -0600347 VkPipelineCache pipelineCache;
Chia-I Wuf973e322015-07-08 13:34:24 +0800348 VkRenderPass render_pass;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600349 VkPipeline pipeline;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600350
Courtney Goeltzenleuchter5e6d1e92015-04-10 16:24:50 -0600351 VkDynamicVpState viewport;
352 VkDynamicRsState raster;
353 VkDynamicCbState color_blend;
354 VkDynamicDsState depth_stencil;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600355
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600356 mat4x4 projection_matrix;
357 mat4x4 view_matrix;
358 mat4x4 model_matrix;
359
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600360 float spin_angle;
361 float spin_increment;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600362 bool pause;
363
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600364 VkDescriptorPool desc_pool;
365 VkDescriptorSet desc_set;
Chia-I Wu6a3c8972015-01-04 16:27:24 +0800366
Chia-I Wuf973e322015-07-08 13:34:24 +0800367 VkFramebuffer framebuffers[DEMO_BUFFER_COUNT];
368
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600369 bool quit;
David Pinedo2bb7c932015-06-18 17:03:14 -0600370 int32_t curFrame;
371 int32_t frameCount;
Tony Barbour3dddd5d2015-04-29 16:19:20 -0600372 bool validate;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -0600373 PFN_vkDbgCreateMsgCallback dbgCreateMsgCallback;
Tony Barbourd70b42c2015-06-30 14:14:19 -0600374 PFN_vkDbgDestroyMsgCallback dbgDestroyMsgCallback;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -0600375 VkDbgMsgCallback msg_callback;
376
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600377 uint32_t current_buffer;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600378};
379
Mark Lobodzinskieadf9982015-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 Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600397static void demo_flush_init_cmd(struct demo *demo)
398{
Tony Barbourfdc2d352015-04-22 09:02:32 -0600399 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600400
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600401 if (demo->cmd == VK_NULL_HANDLE)
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600402 return;
403
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600404 err = vkEndCommandBuffer(demo->cmd);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600405 assert(!err);
406
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600407 const VkCmdBuffer cmd_bufs[] = { demo->cmd };
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600408
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600409 err = vkQueueSubmit(demo->queue, 1, cmd_bufs, VK_NULL_HANDLE);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600410 assert(!err);
411
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600412 err = vkQueueWaitIdle(demo->queue);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600413 assert(!err);
414
Mike Stroyanebae8322015-04-17 12:36:38 -0600415 vkDestroyObject(demo->device, VK_OBJECT_TYPE_COMMAND_BUFFER, demo->cmd);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600416 demo->cmd = VK_NULL_HANDLE;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600417}
418
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600419static void demo_set_image_layout(
420 struct demo *demo,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600421 VkImage image,
malnasseeb25d3f2015-06-03 17:28:38 -0400422 VkImageAspect aspect,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600423 VkImageLayout old_image_layout,
424 VkImageLayout new_image_layout)
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600425{
Tony Barbourfdc2d352015-04-22 09:02:32 -0600426 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600427
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600428 if (demo->cmd == VK_NULL_HANDLE) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600429 const VkCmdBufferCreateInfo cmd = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600430 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600431 .pNext = NULL,
432 .queueNodeIndex = demo->graphics_queue_node_index,
Chia-I Wu57b23b42015-06-26 15:34:39 +0800433 .level = VK_CMD_BUFFER_LEVEL_PRIMARY,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600434 .flags = 0,
435 };
436
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600437 err = vkCreateCommandBuffer(demo->device, &cmd, &demo->cmd);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600438 assert(!err);
439
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600440 VkCmdBufferBeginInfo cmd_buf_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600441 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600442 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -0600443 .flags = VK_CMD_BUFFER_OPTIMIZE_SMALL_BATCH_BIT |
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600444 VK_CMD_BUFFER_OPTIMIZE_ONE_TIME_SUBMIT_BIT,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600445 };
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600446 err = vkBeginCommandBuffer(demo->cmd, &cmd_buf_info);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600447 }
448
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600449 VkImageMemoryBarrier image_memory_barrier = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600450 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600451 .pNext = NULL,
452 .outputMask = 0,
453 .inputMask = 0,
454 .oldLayout = old_image_layout,
455 .newLayout = new_image_layout,
456 .image = image,
malnasseeb25d3f2015-06-03 17:28:38 -0400457 .subresourceRange = { aspect, 0, 1, 0, 0 }
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600458 };
459
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600460 if (new_image_layout == VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL) {
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600461 /* Make sure anything that was copying from this image has completed */
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -0600462 image_memory_barrier.inputMask = VK_MEMORY_INPUT_TRANSFER_BIT;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600463 }
464
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600465 if (new_image_layout == VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL) {
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600466 /* Make sure any Copy or CPU writes to image are flushed */
Courtney Goeltzenleuchter2bda8332015-04-29 17:16:21 -0600467 image_memory_barrier.outputMask = VK_MEMORY_OUTPUT_HOST_WRITE_BIT | VK_MEMORY_OUTPUT_TRANSFER_BIT;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600468 }
469
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600470 VkImageMemoryBarrier *pmemory_barrier = &image_memory_barrier;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600471
Tony Barbour50bbca42015-06-29 16:20:35 -0600472 VkPipelineStageFlags src_stages = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
473 VkPipelineStageFlags dest_stages = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600474
Tony Barbour50bbca42015-06-29 16:20:35 -0600475 vkCmdPipelineBarrier(demo->cmd, src_stages, dest_stages, false, 1, (const void **)&pmemory_barrier);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600476}
477
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600478static void demo_draw_build_cmd(struct demo *demo, VkCmdBuffer cmd_buf)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600479{
Chia-I Wuf973e322015-07-08 13:34:24 +0800480 const VkCmdBufferBeginInfo cmd_buf_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600481 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO,
Courtney Goeltzenleuchter53968d82015-04-03 15:25:24 -0600482 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -0600483 .flags = VK_CMD_BUFFER_OPTIMIZE_SMALL_BATCH_BIT |
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600484 VK_CMD_BUFFER_OPTIMIZE_ONE_TIME_SUBMIT_BIT,
Jon Ashburn60ccfbe2014-12-31 17:08:35 -0700485 };
Chia-I Wua74c5b22015-07-07 11:50:03 +0800486 const VkClearValue clear_values[2] = {
487 [0] = { .color.f32 = { 0.2f, 0.2f, 0.2f, 0.2f } },
488 [1] = { .ds = { 1.0f, 0 } },
489 };
490 const VkRenderPassBeginInfo rp_begin = {
491 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO,
492 .pNext = NULL,
Chia-I Wuf973e322015-07-08 13:34:24 +0800493 .renderPass = demo->render_pass,
494 .framebuffer = demo->framebuffers[demo->current_buffer],
Chia-I Wua74c5b22015-07-07 11:50:03 +0800495 .renderArea.offset.x = 0,
496 .renderArea.offset.y = 0,
497 .renderArea.extent.width = demo->width,
498 .renderArea.extent.height = demo->height,
499 .attachmentCount = 2,
500 .pAttachmentClearValues = clear_values,
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700501 };
Chia-I Wuf973e322015-07-08 13:34:24 +0800502 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600503
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600504 err = vkBeginCommandBuffer(cmd_buf, &cmd_buf_info);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600505 assert(!err);
506
Chia-I Wua74c5b22015-07-07 11:50:03 +0800507 vkCmdBeginRenderPass(cmd_buf, &rp_begin, VK_RENDER_PASS_CONTENTS_INLINE);
508
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600509 vkCmdBindPipeline(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600510 demo->pipeline);
Mark Lobodzinskic6e8b3d2015-06-15 13:21:21 -0600511 vkCmdBindDescriptorSets(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS, demo->pipeline_layout,
Cody Northropfb5185a2015-04-16 13:41:56 -0600512 0, 1, &demo->desc_set, 0, NULL);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600513
Tony Barbour72304ef2015-04-16 15:59:00 -0600514 vkCmdBindDynamicStateObject(cmd_buf, VK_STATE_BIND_POINT_VIEWPORT, demo->viewport);
515 vkCmdBindDynamicStateObject(cmd_buf, VK_STATE_BIND_POINT_RASTER, demo->raster);
516 vkCmdBindDynamicStateObject(cmd_buf, VK_STATE_BIND_POINT_COLOR_BLEND,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600517 demo->color_blend);
Tony Barbour72304ef2015-04-16 15:59:00 -0600518 vkCmdBindDynamicStateObject(cmd_buf, VK_STATE_BIND_POINT_DEPTH_STENCIL,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600519 demo->depth_stencil);
520
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600521 vkCmdDraw(cmd_buf, 0, 12 * 3, 0, 1);
Chia-I Wu57b23b42015-06-26 15:34:39 +0800522 vkCmdEndRenderPass(cmd_buf);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600523
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600524 err = vkEndCommandBuffer(cmd_buf);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600525 assert(!err);
526}
527
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600528
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600529void demo_update_data_buffer(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600530{
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600531 mat4x4 MVP, Model, VP;
532 int matrixSize = sizeof(MVP);
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600533 uint8_t *pData;
Tony Barbourfdc2d352015-04-22 09:02:32 -0600534 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600535
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600536 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600537
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600538 // Rotate 22.5 degrees around the Y axis
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600539 mat4x4_dup(Model, demo->model_matrix);
Piers Daniell735ee532015-02-23 16:23:13 -0700540 mat4x4_rotate(demo->model_matrix, Model, 0.0f, 1.0f, 0.0f, (float)degreesToRadians(demo->spin_angle));
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600541 mat4x4_mul(MVP, VP, demo->model_matrix);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600542
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500543 err = vkMapMemory(demo->device, demo->uniform_data.mem, 0, 0, 0, (void **) &pData);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600544 assert(!err);
545
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600546 memcpy(pData, (const void*) &MVP[0][0], matrixSize);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600547
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500548 err = vkUnmapMemory(demo->device, demo->uniform_data.mem);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600549 assert(!err);
550}
551
552static void demo_draw(struct demo *demo)
553{
Chia-I Wucbb564e2015-04-16 22:02:10 +0800554 const VkPresentInfoWSI present = {
555 .sType = VK_STRUCTURE_TYPE_PRESENT_INFO_WSI,
556 .pNext = NULL,
557 .image = demo->buffers[demo->current_buffer].image,
558 .flipInterval = 0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600559 };
Tony Barbourfdc2d352015-04-22 09:02:32 -0600560 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600561
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600562 err = vkQueueSubmit(demo->queue, 1, &demo->buffers[demo->current_buffer].cmd,
563 VK_NULL_HANDLE);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600564 assert(!err);
565
Jon Ashburn0b85d052015-05-21 18:13:33 -0600566 err = demo->fpQueuePresentWSI(demo->queue, &present);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600567 assert(!err);
568
569 demo->current_buffer = (demo->current_buffer + 1) % DEMO_BUFFER_COUNT;
Chia-I Wucbb564e2015-04-16 22:02:10 +0800570
571 err = vkQueueWaitIdle(demo->queue);
572 assert(err == VK_SUCCESS);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600573}
574
575static void demo_prepare_buffers(struct demo *demo)
576{
Chia-I Wucbb564e2015-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 Elliotte4602cd2015-04-21 16:41:02 -0600582 .displayCount = 1,
Chia-I Wucbb564e2015-04-16 22:02:10 +0800583 .imageCount = DEMO_BUFFER_COUNT,
584 .imageFormat = demo->format,
585 .imageExtent = {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600586 .width = demo->width,
587 .height = demo->height,
588 },
Chia-I Wucbb564e2015-04-16 22:02:10 +0800589 .imageArraySize = 1,
590 .imageUsageFlags = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600591 };
Chia-I Wucbb564e2015-04-16 22:02:10 +0800592 VkSwapChainImageInfoWSI images[DEMO_BUFFER_COUNT];
593 size_t images_size = sizeof(images);
Tony Barbourfdc2d352015-04-22 09:02:32 -0600594 VkResult U_ASSERT_ONLY err;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600595 uint32_t i;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600596
Jon Ashburn0b85d052015-05-21 18:13:33 -0600597 err = demo->fpCreateSwapChainWSI(demo->device, &swap_chain, &demo->swap_chain);
Chia-I Wucbb564e2015-04-16 22:02:10 +0800598 assert(!err);
599
Jon Ashburn0b85d052015-05-21 18:13:33 -0600600 err = demo->fpGetSwapChainInfoWSI(demo->swap_chain,
Chia-I Wucbb564e2015-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 Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600605 for (i = 0; i < DEMO_BUFFER_COUNT; i++) {
Chia-I Wua74c5b22015-07-07 11:50:03 +0800606 VkAttachmentViewCreateInfo color_attachment_view = {
607 .sType = VK_STRUCTURE_TYPE_ATTACHMENT_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-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 Wucbb564e2015-04-16 22:02:10 +0800615 demo->buffers[i].image = images[i].image;
616 demo->buffers[i].mem = images[i].memory;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600617
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600618 demo_set_image_layout(demo, demo->buffers[i].image,
malnasseeb25d3f2015-06-03 17:28:38 -0400619 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600620 VK_IMAGE_LAYOUT_UNDEFINED,
621 VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600622
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600623 color_attachment_view.image = demo->buffers[i].image;
624
Chia-I Wua74c5b22015-07-07 11:50:03 +0800625 err = vkCreateAttachmentView(demo->device,
Courtney Goeltzenleuchter7be88a82014-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 Barbour72304ef2015-04-16 15:59:00 -0600633 const VkFormat depth_format = VK_FORMAT_D16_UNORM;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600634 const VkImageCreateInfo image = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600635 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600636 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -0600637 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchter7be88a82014-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 Barbour72304ef2015-04-16 15:59:00 -0600643 .tiling = VK_IMAGE_TILING_OPTIMAL,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600644 .usage = VK_IMAGE_USAGE_DEPTH_STENCIL_BIT,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600645 .flags = 0,
646 };
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -0600647 VkMemoryAllocInfo mem_alloc = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600648 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
Mark Lobodzinskia6907f72015-04-16 08:52:00 -0500649 .pNext = NULL,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600650 .allocationSize = 0,
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600651 .memoryTypeIndex = 0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600652 };
Chia-I Wua74c5b22015-07-07 11:50:03 +0800653 VkAttachmentViewCreateInfo view = {
654 .sType = VK_STRUCTURE_TYPE_ATTACHMENT_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600655 .pNext = NULL,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600656 .image = VK_NULL_HANDLE,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600657 .mipLevel = 0,
658 .baseArraySlice = 0,
659 .arraySize = 1,
660 .flags = 0,
661 };
Mike Stroyanebae8322015-04-17 12:36:38 -0600662
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500663 VkMemoryRequirements mem_reqs;
Tony Barbourfdc2d352015-04-22 09:02:32 -0600664 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600665
666 demo->depth.format = depth_format;
667
668 /* create image */
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600669 err = vkCreateImage(demo->device, &image,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600670 &demo->depth.image);
671 assert(!err);
672
Tony Barbourecf1b2b2015-06-24 16:06:58 -0600673 err = vkGetObjectMemoryRequirements(demo->device,
674 VK_OBJECT_TYPE_IMAGE, demo->depth.image, &mem_reqs);
Mark Lobodzinskieadf9982015-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 Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600682
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500683 /* allocate memory */
684 err = vkAllocMemory(demo->device, &mem_alloc, &demo->depth.mem);
685 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600686
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500687 /* bind memory */
688 err = vkBindObjectMemory(demo->device,
689 VK_OBJECT_TYPE_IMAGE, demo->depth.image,
690 demo->depth.mem, 0);
691 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600692
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600693 demo_set_image_layout(demo, demo->depth.image,
malnasseeb25d3f2015-06-03 17:28:38 -0400694 VK_IMAGE_ASPECT_DEPTH,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600695 VK_IMAGE_LAYOUT_UNDEFINED,
696 VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600697
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600698 /* create image view */
699 view.image = demo->depth.image;
Chia-I Wua74c5b22015-07-07 11:50:03 +0800700 err = vkCreateAttachmentView(demo->device, &view, &demo->depth.view);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600701 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600702}
703
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600704/** loadTexture
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -0600705 * loads a png file into an memory object, using cstdio , libpng.
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600706 *
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -0600707 * \param demo : Needed to access VK calls
708 * \param filename : the png file to be loaded
709 * \param width : width of png, to be updated as a side effect of this function
710 * \param height : height of png, to be updated as a side effect of this function
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600711 *
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -0600712 * \return bool : an opengl texture id. true if successful?,
713 * should be validated by the client of this function.
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600714 *
715 * Source: http://en.wikibooks.org/wiki/OpenGL_Programming/Intermediate/Textures
716 * Modified to copy image to memory
717 *
718 */
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700719bool loadTexture(const char *filename, uint8_t *rgba_data,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600720 VkSubresourceLayout *layout,
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600721 int32_t *width, int32_t *height)
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600722{
723 //header for testing if it is a png
724 png_byte header[8];
Tony Barbourf9921732015-04-22 11:36:22 -0600725 int is_png, bit_depth, color_type, rowbytes;
726 size_t retval;
Ian Elliott1e42dff2015-02-13 14:29:21 -0700727 png_uint_32 i, twidth, theight;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600728 png_structp png_ptr;
729 png_infop info_ptr, end_info;
730 png_byte *image_data;
731 png_bytep *row_pointers;
732
733 //open file as binary
734 FILE *fp = fopen(filename, "rb");
735 if (!fp) {
736 return false;
737 }
738
739 //read the header
Tony Barbourfdc2d352015-04-22 09:02:32 -0600740 retval = fread(header, 1, 8, fp);
741 if (retval != 8) {
742 fclose(fp);
743 return false;
744 }
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600745
746 //test if png
747 is_png = !png_sig_cmp(header, 0, 8);
748 if (!is_png) {
749 fclose(fp);
750 return false;
751 }
752
753 //create png struct
754 png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL,
755 NULL, NULL);
756 if (!png_ptr) {
757 fclose(fp);
758 return (false);
759 }
760
761 //create png info struct
762 info_ptr = png_create_info_struct(png_ptr);
763 if (!info_ptr) {
764 png_destroy_read_struct(&png_ptr, (png_infopp) NULL, (png_infopp) NULL);
765 fclose(fp);
766 return (false);
767 }
768
769 //create png info struct
770 end_info = png_create_info_struct(png_ptr);
771 if (!end_info) {
772 png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp) NULL);
773 fclose(fp);
774 return (false);
775 }
776
777 //png error stuff, not sure libpng man suggests this.
778 if (setjmp(png_jmpbuf(png_ptr))) {
779 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
780 fclose(fp);
781 return (false);
782 }
783
784 //init png reading
785 png_init_io(png_ptr, fp);
786
787 //let libpng know you already read the first 8 bytes
788 png_set_sig_bytes(png_ptr, 8);
789
790 // read all the info up to the image data
791 png_read_info(png_ptr, info_ptr);
792
793 // get info about png
794 png_get_IHDR(png_ptr, info_ptr, &twidth, &theight, &bit_depth, &color_type,
795 NULL, NULL, NULL);
796
797 //update width and height based on png info
798 *width = twidth;
799 *height = theight;
800
801 // Require that incoming texture be 8bits per color component
802 // and 4 components (RGBA).
803 if (png_get_bit_depth(png_ptr, info_ptr) != 8 ||
804 png_get_channels(png_ptr, info_ptr) != 4) {
805 return false;
806 }
807
808 if (rgba_data == NULL) {
809 // If data pointer is null, we just want the width & height
810 // clean up memory and close stuff
811 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
812 fclose(fp);
813
814 return true;
815 }
816
817 // Update the png info struct.
818 png_read_update_info(png_ptr, info_ptr);
819
820 // Row size in bytes.
821 rowbytes = png_get_rowbytes(png_ptr, info_ptr);
822
823 // Allocate the image_data as a big block, to be given to opengl
824 image_data = (png_byte *)malloc(rowbytes * theight * sizeof(png_byte));
825 if (!image_data) {
826 //clean up memory and close stuff
827 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
828 fclose(fp);
829 return false;
830 }
831
832 // row_pointers is for pointing to image_data for reading the png with libpng
833 row_pointers = (png_bytep *)malloc(theight * sizeof(png_bytep));
834 if (!row_pointers) {
835 //clean up memory and close stuff
836 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
837 // delete[] image_data;
838 fclose(fp);
839 return false;
840 }
841 // set the individual row_pointers to point at the correct offsets of image_data
842 for (i = 0; i < theight; ++i)
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700843 row_pointers[theight - 1 - i] = rgba_data + i * layout->rowPitch;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600844
845 // read the png into image_data through row_pointers
846 png_read_image(png_ptr, row_pointers);
847
848 // clean up memory and close stuff
849 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
850 free(row_pointers);
851 free(image_data);
852 fclose(fp);
853
854 return true;
855}
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600856
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700857static void demo_prepare_texture_image(struct demo *demo,
858 const char *filename,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600859 struct texture_object *tex_obj,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600860 VkImageTiling tiling,
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -0600861 VkImageUsageFlags usage,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600862 VkFlags mem_props)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600863{
Mike Stroyan8b89e072015-06-15 14:21:03 -0600864 const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600865 int32_t tex_width;
866 int32_t tex_height;
Tony Barbourfdc2d352015-04-22 09:02:32 -0600867 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700868
David Pinedo30bd71d2015-04-23 08:16:57 -0600869 if (!loadTexture(filename, NULL, NULL, &tex_width, &tex_height))
870 {
871 printf("Failed to load textures\n");
872 fflush(stdout);
873 exit(1);
874 }
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700875
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600876 tex_obj->tex_width = tex_width;
877 tex_obj->tex_height = tex_height;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700878
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600879 const VkImageCreateInfo image_create_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600880 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700881 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -0600882 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700883 .format = tex_format,
884 .extent = { tex_width, tex_height, 1 },
885 .mipLevels = 1,
886 .arraySize = 1,
887 .samples = 1,
888 .tiling = tiling,
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -0600889 .usage = usage,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700890 .flags = 0,
891 };
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -0600892 VkMemoryAllocInfo mem_alloc = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600893 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
Mark Lobodzinskia6907f72015-04-16 08:52:00 -0500894 .pNext = NULL,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700895 .allocationSize = 0,
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600896 .memoryTypeIndex = 0,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700897 };
898
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500899 VkMemoryRequirements mem_reqs;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700900
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600901 err = vkCreateImage(demo->device, &image_create_info,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600902 &tex_obj->image);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700903 assert(!err);
904
Tony Barbourecf1b2b2015-06-24 16:06:58 -0600905 err = vkGetObjectMemoryRequirements(demo->device,
906 VK_OBJECT_TYPE_IMAGE, tex_obj->image, &mem_reqs);
907 assert(!err);
Piers Daniell735ee532015-02-23 16:23:13 -0700908
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500909 mem_alloc.allocationSize = mem_reqs.size;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700910
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600911 err = memory_type_from_properties(demo, mem_reqs.memoryTypeBits, mem_props, &mem_alloc.memoryTypeIndex);
912 assert(!err);
913
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500914 /* allocate memory */
915 err = vkAllocMemory(demo->device, &mem_alloc,
916 &(tex_obj->mem));
917 assert(!err);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700918
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500919 /* bind memory */
920 err = vkBindObjectMemory(demo->device,
921 VK_OBJECT_TYPE_IMAGE, tex_obj->image,
922 tex_obj->mem, 0);
923 assert(!err);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700924
Tony Barbour72304ef2015-04-16 15:59:00 -0600925 if (mem_props & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600926 const VkImageSubresource subres = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600927 .aspect = VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700928 .mipLevel = 0,
929 .arraySlice = 0,
930 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600931 VkSubresourceLayout layout;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700932 void *data;
933
Tony Barbourecf1b2b2015-06-24 16:06:58 -0600934 err = vkGetImageSubresourceLayout(demo->device, tex_obj->image, &subres, &layout);
935 assert(!err);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700936
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500937 err = vkMapMemory(demo->device, tex_obj->mem, 0, 0, 0, &data);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700938 assert(!err);
939
940 if (!loadTexture(filename, data, &layout, &tex_width, &tex_height)) {
941 fprintf(stderr, "Error loading texture: %s\n", filename);
942 }
943
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500944 err = vkUnmapMemory(demo->device, tex_obj->mem);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700945 assert(!err);
946 }
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600947
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600948 tex_obj->imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600949 demo_set_image_layout(demo, tex_obj->image,
malnasseeb25d3f2015-06-03 17:28:38 -0400950 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600951 VK_IMAGE_LAYOUT_UNDEFINED,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600952 tex_obj->imageLayout);
953 /* setting the image layout does not reference the actual memory so no need to add a mem ref */
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700954}
955
Mark Lobodzinskida9b1092015-04-16 11:44:05 -0500956static void demo_destroy_texture_image(struct demo *demo, struct texture_object *tex_objs)
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700957{
958 /* clean up staging resources */
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500959 vkFreeMemory(demo->device, tex_objs->mem);
Mike Stroyanebae8322015-04-17 12:36:38 -0600960 vkDestroyObject(demo->device, VK_OBJECT_TYPE_IMAGE, tex_objs->image);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700961}
962
963static void demo_prepare_textures(struct demo *demo)
964{
Tony Barbour72304ef2015-04-16 15:59:00 -0600965 const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600966 VkFormatProperties props;
Tony Barbourfdc2d352015-04-22 09:02:32 -0600967 VkResult U_ASSERT_ONLY err;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600968 uint32_t i;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600969
Chris Forbesf576a3e2015-06-21 22:55:02 +1200970 err = vkGetPhysicalDeviceFormatInfo(demo->gpu, tex_format, &props);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700971 assert(!err);
972
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600973 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700974
FslNopper6a7e50e2015-05-06 21:42:01 +0200975 if ((props.linearTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) && !demo->use_staging_buffer) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700976 /* Device can texture using linear textures */
977 demo_prepare_texture_image(demo, tex_files[i], &demo->textures[i],
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -0600978 VK_IMAGE_TILING_LINEAR, VK_IMAGE_USAGE_SAMPLED_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
Tony Barbour72304ef2015-04-16 15:59:00 -0600979 } else if (props.optimalTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700980 /* Must use staging buffer to copy linear texture to optimized */
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600981 struct texture_object staging_texture;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700982
983 memset(&staging_texture, 0, sizeof(staging_texture));
984 demo_prepare_texture_image(demo, tex_files[i], &staging_texture,
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -0600985 VK_IMAGE_TILING_LINEAR, VK_IMAGE_USAGE_TRANSFER_SOURCE_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700986
987 demo_prepare_texture_image(demo, tex_files[i], &demo->textures[i],
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -0600988 VK_IMAGE_TILING_OPTIMAL,
989 (VK_IMAGE_USAGE_TRANSFER_DESTINATION_BIT | VK_IMAGE_USAGE_SAMPLED_BIT),
990 VK_MEMORY_PROPERTY_DEVICE_ONLY);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700991
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600992 demo_set_image_layout(demo, staging_texture.image,
malnasseeb25d3f2015-06-03 17:28:38 -0400993 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600994 staging_texture.imageLayout,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600995 VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700996
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600997 demo_set_image_layout(demo, demo->textures[i].image,
malnasseeb25d3f2015-06-03 17:28:38 -0400998 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600999 demo->textures[i].imageLayout,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001000 VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001001
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001002 VkImageCopy copy_region = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001003 .srcSubresource = { VK_IMAGE_ASPECT_COLOR, 0, 0 },
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001004 .srcOffset = { 0, 0, 0 },
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001005 .destSubresource = { VK_IMAGE_ASPECT_COLOR, 0, 0 },
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001006 .destOffset = { 0, 0, 0 },
1007 .extent = { staging_texture.tex_width, staging_texture.tex_height, 1 },
1008 };
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001009 vkCmdCopyImage(demo->cmd,
1010 staging_texture.image, VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL,
1011 demo->textures[i].image, VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL,
Courtney Goeltzenleuchter8e89a312015-03-25 11:25:10 -06001012 1, &copy_region);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001013
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001014 demo_set_image_layout(demo, demo->textures[i].image,
malnasseeb25d3f2015-06-03 17:28:38 -04001015 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001016 VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001017 demo->textures[i].imageLayout);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001018
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001019 demo_flush_init_cmd(demo);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001020
Courtney Goeltzenleuchterf3aeb2b2015-04-21 09:30:03 -06001021 demo_destroy_texture_image(demo, &staging_texture);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001022 } else {
Mike Stroyan8b89e072015-06-15 14:21:03 -06001023 /* Can't support VK_FORMAT_R8G8B8A8_UNORM !? */
1024 assert(!"No support for R8G8B8A8_UNORM as texture image format");
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001025 }
1026
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001027 const VkSamplerCreateInfo sampler = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001028 .sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001029 .pNext = NULL,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001030 .magFilter = VK_TEX_FILTER_NEAREST,
1031 .minFilter = VK_TEX_FILTER_NEAREST,
Tony Barbour72304ef2015-04-16 15:59:00 -06001032 .mipMode = VK_TEX_MIPMAP_MODE_BASE,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001033 .addressU = VK_TEX_ADDRESS_CLAMP,
1034 .addressV = VK_TEX_ADDRESS_CLAMP,
1035 .addressW = VK_TEX_ADDRESS_CLAMP,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001036 .mipLodBias = 0.0f,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001037 .maxAnisotropy = 1,
Tony Barbour72304ef2015-04-16 15:59:00 -06001038 .compareOp = VK_COMPARE_OP_NEVER,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001039 .minLod = 0.0f,
1040 .maxLod = 0.0f,
Tony Barbourcb530c72015-06-25 16:56:44 -06001041 .borderColor = VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001042 };
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001043
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001044 VkImageViewCreateInfo view = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001045 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001046 .pNext = NULL,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001047 .image = VK_NULL_HANDLE,
Tony Barbour72304ef2015-04-16 15:59:00 -06001048 .viewType = VK_IMAGE_VIEW_TYPE_2D,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001049 .format = tex_format,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001050 .channels = { VK_CHANNEL_SWIZZLE_R,
1051 VK_CHANNEL_SWIZZLE_G,
1052 VK_CHANNEL_SWIZZLE_B,
1053 VK_CHANNEL_SWIZZLE_A, },
1054 .subresourceRange = { VK_IMAGE_ASPECT_COLOR, 0, 1, 0, 1 },
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001055 };
Jon Ashburnb2a66652015-01-16 09:37:43 -07001056
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001057 /* create sampler */
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001058 err = vkCreateSampler(demo->device, &sampler,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001059 &demo->textures[i].sampler);
1060 assert(!err);
1061
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001062 /* create image view */
1063 view.image = demo->textures[i].image;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001064 err = vkCreateImageView(demo->device, &view,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001065 &demo->textures[i].view);
1066 assert(!err);
1067 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001068}
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001069
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001070void demo_prepare_cube_data_buffer(struct demo *demo)
1071{
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -06001072 VkBufferCreateInfo buf_info;
1073 VkBufferViewCreateInfo view_info;
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -06001074 VkMemoryAllocInfo alloc_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001075 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
Mark Lobodzinskia6907f72015-04-16 08:52:00 -05001076 .pNext = NULL,
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001077 .allocationSize = 0,
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06001078 .memoryTypeIndex = 0,
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001079 };
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001080 VkMemoryRequirements mem_reqs;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001081 size_t mem_reqs_size = sizeof(VkMemoryRequirements);
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001082 uint8_t *pData;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001083 int i;
1084 mat4x4 MVP, VP;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001085 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001086 struct vktexcube_vs_uniform data;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001087
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001088 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001089 mat4x4_mul(MVP, VP, demo->model_matrix);
1090 memcpy(data.mvp, MVP, sizeof(MVP));
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001091// dumpMatrix("MVP", MVP);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001092
1093 for (i=0; i<12*3; i++) {
1094 data.position[i][0] = g_vertex_buffer_data[i*3];
1095 data.position[i][1] = g_vertex_buffer_data[i*3+1];
1096 data.position[i][2] = g_vertex_buffer_data[i*3+2];
1097 data.position[i][3] = 1.0f;
1098 data.attr[i][0] = g_uv_buffer_data[2*i];
1099 data.attr[i][1] = g_uv_buffer_data[2*i + 1];
1100 data.attr[i][2] = 0;
1101 data.attr[i][3] = 0;
1102 }
1103
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001104 memset(&buf_info, 0, sizeof(buf_info));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001105 buf_info.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -06001106 buf_info.usage = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001107 err = vkCreateBuffer(demo->device, &buf_info, &demo->uniform_data.buf);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001108 assert(!err);
1109
Tony Barbourecf1b2b2015-06-24 16:06:58 -06001110 err = vkGetObjectMemoryRequirements(demo->device,
1111 VK_OBJECT_TYPE_BUFFER, demo->uniform_data.buf, &mem_reqs);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001112 assert(!err && mem_reqs_size == sizeof(mem_reqs));
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001113
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001114 alloc_info.allocationSize = mem_reqs.size;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06001115 err = memory_type_from_properties(demo,
1116 mem_reqs.memoryTypeBits,
1117 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT,
1118 &alloc_info.memoryTypeIndex);
1119 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001120
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001121 err = vkAllocMemory(demo->device, &alloc_info, &(demo->uniform_data.mem));
1122 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001123
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001124 err = vkMapMemory(demo->device, demo->uniform_data.mem, 0, 0, 0, (void **) &pData);
1125 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001126
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001127 memcpy(pData, &data, sizeof data);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001128
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001129 err = vkUnmapMemory(demo->device, demo->uniform_data.mem);
1130 assert(!err);
1131
1132 err = vkBindObjectMemory(demo->device,
1133 VK_OBJECT_TYPE_BUFFER, demo->uniform_data.buf,
1134 demo->uniform_data.mem, 0);
1135 assert(!err);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001136
1137 memset(&view_info, 0, sizeof(view_info));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001138 view_info.sType = VK_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO;
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001139 view_info.buffer = demo->uniform_data.buf;
Tony Barbour72304ef2015-04-16 15:59:00 -06001140 view_info.viewType = VK_BUFFER_VIEW_TYPE_RAW;
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001141 view_info.offset = 0;
1142 view_info.range = sizeof(data);
1143
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001144 err = vkCreateBufferView(demo->device, &view_info, &demo->uniform_data.view);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001145 assert(!err);
1146
Chia-I Wuae721ba2015-05-25 16:27:55 +08001147 demo->uniform_data.desc.bufferView = demo->uniform_data.view;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001148}
1149
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001150static void demo_prepare_descriptor_layout(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001151{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001152 const VkDescriptorSetLayoutBinding layout_bindings[2] = {
Chia-I Wua2aa8632015-03-26 15:04:41 +08001153 [0] = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001154 .descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
Chia-I Wu6d29a412015-05-25 16:22:52 +08001155 .arraySize = 1,
Tony Barbour72304ef2015-04-16 15:59:00 -06001156 .stageFlags = VK_SHADER_STAGE_VERTEX_BIT,
Chia-I Wu91e8e212015-03-27 12:56:09 +08001157 .pImmutableSamplers = NULL,
Chia-I Wua2aa8632015-03-26 15:04:41 +08001158 },
1159 [1] = {
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -06001160 .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
Chia-I Wu6d29a412015-05-25 16:22:52 +08001161 .arraySize = DEMO_TEXTURE_COUNT,
Tony Barbour72304ef2015-04-16 15:59:00 -06001162 .stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT,
Chia-I Wu91e8e212015-03-27 12:56:09 +08001163 .pImmutableSamplers = NULL,
Chia-I Wua2aa8632015-03-26 15:04:41 +08001164 },
1165 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001166 const VkDescriptorSetLayoutCreateInfo descriptor_layout = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001167 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001168 .pNext = NULL,
Chia-I Wua2aa8632015-03-26 15:04:41 +08001169 .count = 2,
1170 .pBinding = layout_bindings,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001171 };
Tony Barbourfdc2d352015-04-22 09:02:32 -06001172 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001173
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001174 err = vkCreateDescriptorSetLayout(demo->device,
Chia-I Wub58c24a2015-03-26 15:27:55 +08001175 &descriptor_layout, &demo->desc_layout);
1176 assert(!err);
1177
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001178 const VkPipelineLayoutCreateInfo pPipelineLayoutCreateInfo = {
1179 .sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
1180 .pNext = NULL,
1181 .descriptorSetCount = 1,
1182 .pSetLayouts = &demo->desc_layout,
1183 };
1184
1185 err = vkCreatePipelineLayout(demo->device,
1186 &pPipelineLayoutCreateInfo,
1187 &demo->pipeline_layout);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001188 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001189}
1190
Chia-I Wuf973e322015-07-08 13:34:24 +08001191static void demo_prepare_render_pass(struct demo *demo)
1192{
Chia-I Wua74c5b22015-07-07 11:50:03 +08001193 const VkAttachmentDescription attachments[2] = {
1194 [0] = {
1195 .sType = VK_STRUCTURE_TYPE_ATTACHMENT_DESCRIPTION,
1196 .pNext = NULL,
1197 .format = demo->format,
1198 .samples = 1,
1199 .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
1200 .storeOp = VK_ATTACHMENT_STORE_OP_STORE,
1201 .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
1202 .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1203 .initialLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
1204 .finalLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
1205 },
1206 [1] = {
1207 .sType = VK_STRUCTURE_TYPE_ATTACHMENT_DESCRIPTION,
1208 .pNext = NULL,
1209 .format = demo->depth.format,
1210 .samples = 1,
1211 .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
1212 .storeOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1213 .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
1214 .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1215 .initialLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1216 .finalLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1217 },
Chia-I Wuf973e322015-07-08 13:34:24 +08001218 };
Chia-I Wua74c5b22015-07-07 11:50:03 +08001219 const VkAttachmentReference color_reference = {
1220 .attachment = 0,
1221 .layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
1222 };
1223 const VkSubpassDescription subpass = {
1224 .sType = VK_STRUCTURE_TYPE_SUBPASS_DESCRIPTION,
1225 .pNext = NULL,
1226 .pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS,
1227 .flags = 0,
1228 .inputCount = 0,
1229 .inputAttachments = NULL,
1230 .colorCount = 1,
1231 .colorAttachments = &color_reference,
1232 .resolveAttachments = NULL,
1233 .depthStencilAttachment = {
1234 .attachment = 1,
1235 .layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1236 },
1237 .preserveCount = 0,
1238 .preserveAttachments = NULL,
1239 };
Chia-I Wuf973e322015-07-08 13:34:24 +08001240 const VkRenderPassCreateInfo rp_info = {
1241 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO,
1242 .pNext = NULL,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001243 .attachmentCount = 2,
1244 .pAttachments = attachments,
1245 .subpassCount = 1,
1246 .pSubpasses = &subpass,
1247 .dependencyCount = 0,
1248 .pDependencies = NULL,
Chia-I Wuf973e322015-07-08 13:34:24 +08001249 };
Chia-I Wua74c5b22015-07-07 11:50:03 +08001250 VkResult U_ASSERT_ONLY err;
Chia-I Wuf973e322015-07-08 13:34:24 +08001251
1252 err = vkCreateRenderPass(demo->device, &rp_info, &demo->render_pass);
1253 assert(!err);
1254}
1255
Tobin Ehlisd1f17ac2015-07-02 11:02:49 -06001256static VkShader demo_prepare_shader(struct demo* demo,
Tony Barbour72304ef2015-04-16 15:59:00 -06001257 VkShaderStage stage,
Tobin Ehlisd1f17ac2015-07-02 11:02:49 -06001258 const void* code,
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001259 size_t size)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001260{
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001261 VkShaderModuleCreateInfo moduleCreateInfo;
1262 VkShaderCreateInfo shaderCreateInfo;
1263 VkShaderModule shaderModule;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001264 VkShader shader;
1265 VkResult err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001266
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001267
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001268 moduleCreateInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
1269 moduleCreateInfo.pNext = NULL;
1270
1271 shaderCreateInfo.sType = VK_STRUCTURE_TYPE_SHADER_CREATE_INFO;
1272 shaderCreateInfo.pNext = NULL;
Tobin Ehlisd1f17ac2015-07-02 11:02:49 -06001273 shaderCreateInfo.pName = "main";
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001274
Cody Northrop1fedb212015-05-28 11:27:16 -06001275 if (!demo->use_glsl) {
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001276 moduleCreateInfo.codeSize = size;
1277 moduleCreateInfo.pCode = code;
1278 moduleCreateInfo.flags = 0;
1279 err = vkCreateShaderModule(demo->device, &moduleCreateInfo, &shaderModule);
Cody Northrop1fedb212015-05-28 11:27:16 -06001280 if (err) {
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001281 free((void *) moduleCreateInfo.pCode);
Cody Northrop1fedb212015-05-28 11:27:16 -06001282 }
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001283
1284 shaderCreateInfo.flags = 0;
1285 shaderCreateInfo.module = shaderModule;
1286 err = vkCreateShader(demo->device, &shaderCreateInfo, &shader);
Cody Northrop1fedb212015-05-28 11:27:16 -06001287 } else {
1288 // Create fake SPV structure to feed GLSL
1289 // to the driver "under the covers"
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001290 moduleCreateInfo.codeSize = 3 * sizeof(uint32_t) + size + 1;
1291 moduleCreateInfo.pCode = malloc(moduleCreateInfo.codeSize);
1292 moduleCreateInfo.flags = 0;
Cody Northrop1fedb212015-05-28 11:27:16 -06001293
1294 /* try version 0 first: VkShaderStage followed by GLSL */
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001295 ((uint32_t *) moduleCreateInfo.pCode)[0] = ICD_SPV_MAGIC;
1296 ((uint32_t *) moduleCreateInfo.pCode)[1] = 0;
1297 ((uint32_t *) moduleCreateInfo.pCode)[2] = stage;
1298 memcpy(((uint32_t *) moduleCreateInfo.pCode + 3), code, size + 1);
Cody Northrop1fedb212015-05-28 11:27:16 -06001299
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001300 err = vkCreateShaderModule(demo->device, &moduleCreateInfo, &shaderModule);
Cody Northrop1fedb212015-05-28 11:27:16 -06001301 if (err) {
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001302 free((void *) moduleCreateInfo.pCode);
Cody Northrop1fedb212015-05-28 11:27:16 -06001303 }
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001304
1305 shaderCreateInfo.flags = 0;
1306 shaderCreateInfo.module = shaderModule;
1307 err = vkCreateShader(demo->device, &shaderCreateInfo, &shader);
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001308 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001309 return shader;
1310}
1311
Cody Northrop8b12a1f2015-03-17 15:55:58 -06001312char *demo_read_spv(const char *filename, size_t *psize)
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001313{
1314 long int size;
Tony Barbourf9921732015-04-22 11:36:22 -06001315 size_t U_ASSERT_ONLY retval;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001316 void *shader_code;
1317
1318 FILE *fp = fopen(filename, "rb");
1319 if (!fp) return NULL;
1320
1321 fseek(fp, 0L, SEEK_END);
1322 size = ftell(fp);
1323
1324 fseek(fp, 0L, SEEK_SET);
1325
1326 shader_code = malloc(size);
Tony Barbourfdc2d352015-04-22 09:02:32 -06001327 retval = fread(shader_code, size, 1, fp);
1328 assert(retval == 1);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001329
1330 *psize = size;
1331
1332 return shader_code;
1333}
1334
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001335static VkShader demo_prepare_vs(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001336{
Cody Northrop1fedb212015-05-28 11:27:16 -06001337 if (!demo->use_glsl) {
1338 void *vertShaderCode;
1339 size_t size;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001340
Cody Northrop1fedb212015-05-28 11:27:16 -06001341 vertShaderCode = demo_read_spv("cube-vert.spv", &size);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001342
Cody Northrop1fedb212015-05-28 11:27:16 -06001343 return demo_prepare_shader(demo, VK_SHADER_STAGE_VERTEX,
1344 vertShaderCode, size);
1345 } else {
1346 static const char *vertShaderText =
1347 "#version 140\n"
1348 "#extension GL_ARB_separate_shader_objects : enable\n"
1349 "#extension GL_ARB_shading_language_420pack : enable\n"
1350 "\n"
1351 "layout(binding = 0) uniform buf {\n"
1352 " mat4 MVP;\n"
1353 " vec4 position[12*3];\n"
1354 " vec4 attr[12*3];\n"
1355 "} ubuf;\n"
1356 "\n"
1357 "layout (location = 0) out vec4 texcoord;\n"
1358 "\n"
1359 "void main() \n"
1360 "{\n"
1361 " texcoord = ubuf.attr[gl_VertexID];\n"
1362 " gl_Position = ubuf.MVP * ubuf.position[gl_VertexID];\n"
1363 "\n"
1364 " // GL->VK conventions\n"
1365 " gl_Position.y = -gl_Position.y;\n"
1366 " gl_Position.z = (gl_Position.z + gl_Position.w) / 2.0;\n"
1367 "}\n";
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001368
Cody Northrop1fedb212015-05-28 11:27:16 -06001369 return demo_prepare_shader(demo, VK_SHADER_STAGE_VERTEX,
1370 (const void *) vertShaderText,
1371 strlen(vertShaderText));
1372 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001373}
1374
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001375static VkShader demo_prepare_fs(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001376{
Cody Northrop1fedb212015-05-28 11:27:16 -06001377 if (!demo->use_glsl) {
1378 void *fragShaderCode;
1379 size_t size;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001380
Cody Northrop1fedb212015-05-28 11:27:16 -06001381 fragShaderCode = demo_read_spv("cube-frag.spv", &size);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001382
Cody Northrop1fedb212015-05-28 11:27:16 -06001383 return demo_prepare_shader(demo, VK_SHADER_STAGE_FRAGMENT,
1384 fragShaderCode, size);
1385 } else {
1386 static const char *fragShaderText =
1387 "#version 140\n"
1388 "#extension GL_ARB_separate_shader_objects : enable\n"
1389 "#extension GL_ARB_shading_language_420pack : enable\n"
1390 "layout (binding = 1) uniform sampler2D tex;\n"
1391 "\n"
1392 "layout (location = 0) in vec4 texcoord;\n"
1393 "layout (location = 0) out vec4 uFragColor;\n"
1394 "void main() {\n"
1395 " uFragColor = texture(tex, texcoord.xy);\n"
1396 "}\n";
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001397
Cody Northrop1fedb212015-05-28 11:27:16 -06001398 return demo_prepare_shader(demo, VK_SHADER_STAGE_FRAGMENT,
1399 (const void *) fragShaderText,
1400 strlen(fragShaderText));
1401 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001402}
1403
1404static void demo_prepare_pipeline(struct demo *demo)
1405{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001406 VkGraphicsPipelineCreateInfo pipeline;
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001407 VkPipelineCacheCreateInfo pipelineCache;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001408 VkPipelineIaStateCreateInfo ia;
1409 VkPipelineRsStateCreateInfo rs;
1410 VkPipelineCbStateCreateInfo cb;
1411 VkPipelineDsStateCreateInfo ds;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001412 VkPipelineVpStateCreateInfo vp;
1413 VkPipelineMsStateCreateInfo ms;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001414 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001415
1416 memset(&pipeline, 0, sizeof(pipeline));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001417 pipeline.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001418 pipeline.layout = demo->pipeline_layout;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001419
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001420 memset(&ia, 0, sizeof(ia));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001421 ia.sType = VK_STRUCTURE_TYPE_PIPELINE_IA_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06001422 ia.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001423
1424 memset(&rs, 0, sizeof(rs));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001425 rs.sType = VK_STRUCTURE_TYPE_PIPELINE_RS_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06001426 rs.fillMode = VK_FILL_MODE_SOLID;
1427 rs.cullMode = VK_CULL_MODE_BACK;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001428 rs.frontFace = VK_FRONT_FACE_CCW;
Chia-I Wubb67e6e2015-04-22 14:20:52 +08001429 rs.depthClipEnable = VK_TRUE;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001430
1431 memset(&cb, 0, sizeof(cb));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001432 cb.sType = VK_STRUCTURE_TYPE_PIPELINE_CB_STATE_CREATE_INFO;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001433 VkPipelineCbAttachmentState att_state[1];
Tony Barbour29645d02015-01-16 14:27:35 -07001434 memset(att_state, 0, sizeof(att_state));
Tony Barbour29645d02015-01-16 14:27:35 -07001435 att_state[0].channelWriteMask = 0xf;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001436 att_state[0].blendEnable = VK_FALSE;
Tony Barbour29645d02015-01-16 14:27:35 -07001437 cb.attachmentCount = 1;
1438 cb.pAttachments = att_state;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001439
Tony Barbour29645d02015-01-16 14:27:35 -07001440 memset(&vp, 0, sizeof(vp));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001441 vp.sType = VK_STRUCTURE_TYPE_PIPELINE_VP_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06001442 vp.viewportCount = 1;
Tony Barbour29645d02015-01-16 14:27:35 -07001443
1444 memset(&ds, 0, sizeof(ds));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001445 ds.sType = VK_STRUCTURE_TYPE_PIPELINE_DS_STATE_CREATE_INFO;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001446 ds.depthTestEnable = VK_TRUE;
1447 ds.depthWriteEnable = VK_TRUE;
Tony Barbour72304ef2015-04-16 15:59:00 -06001448 ds.depthCompareOp = VK_COMPARE_OP_LESS_EQUAL;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001449 ds.depthBoundsEnable = VK_FALSE;
1450 ds.back.stencilFailOp = VK_STENCIL_OP_KEEP;
1451 ds.back.stencilPassOp = VK_STENCIL_OP_KEEP;
Tony Barbour72304ef2015-04-16 15:59:00 -06001452 ds.back.stencilCompareOp = VK_COMPARE_OP_ALWAYS;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001453 ds.stencilTestEnable = VK_FALSE;
Tony Barbour29645d02015-01-16 14:27:35 -07001454 ds.front = ds.back;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001455
Tony Barbour29645d02015-01-16 14:27:35 -07001456 memset(&ms, 0, sizeof(ms));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001457 ms.sType = VK_STRUCTURE_TYPE_PIPELINE_MS_STATE_CREATE_INFO;
Tony Barbour29645d02015-01-16 14:27:35 -07001458 ms.sampleMask = 1;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001459 ms.multisampleEnable = VK_FALSE;
Tony Barbour3ca22502015-06-26 10:18:34 -06001460 ms.rasterSamples = 1;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001461
Mark Lobodzinski521d7762015-06-23 15:11:57 -06001462 // Two stages: vs and fs
1463 pipeline.stageCount = 2;
1464 VkPipelineShaderStageCreateInfo shaderStages[2];
1465 memset(&shaderStages, 0, 2 * sizeof(VkPipelineShaderStageCreateInfo));
1466
1467 shaderStages[0].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1468 shaderStages[0].stage = VK_SHADER_STAGE_VERTEX;
1469 shaderStages[0].shader = demo_prepare_vs(demo);
1470
1471 shaderStages[1].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1472 shaderStages[1].stage = VK_SHADER_STAGE_FRAGMENT;
1473 shaderStages[1].shader = demo_prepare_fs(demo);
1474
Mark Lobodzinski15169cf2015-06-30 10:18:36 -06001475 pipeline.pVertexInputState = NULL;
Mark Lobodzinski521d7762015-06-23 15:11:57 -06001476 pipeline.pIaState = &ia;
1477 pipeline.pRsState = &rs;
1478 pipeline.pCbState = &cb;
1479 pipeline.pMsState = &ms;
1480 pipeline.pVpState = &vp;
1481 pipeline.pDsState = &ds;
1482 pipeline.pStages = shaderStages;
Chia-I Wua74c5b22015-07-07 11:50:03 +08001483 pipeline.renderPass = demo->render_pass;
1484 pipeline.subpass = 0;
1485
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001486 memset(&pipelineCache, 0, sizeof(pipelineCache));
1487 pipelineCache.sType = VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001488
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001489 err = vkCreatePipelineCache(demo->device, &pipelineCache, &demo->pipelineCache);
1490 assert(!err);
1491 err = vkCreateGraphicsPipelines(demo->device, demo->pipelineCache, 1, &pipeline, &demo->pipeline);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001492 assert(!err);
1493
Mark Lobodzinski521d7762015-06-23 15:11:57 -06001494 for (uint32_t i = 0; i < pipeline.stageCount; i++) {
1495 vkDestroyObject(demo->device, VK_OBJECT_TYPE_SHADER, shaderStages[i].shader);
1496 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001497}
1498
1499static void demo_prepare_dynamic_states(struct demo *demo)
1500{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001501 VkDynamicVpStateCreateInfo viewport_create;
1502 VkDynamicRsStateCreateInfo raster;
1503 VkDynamicCbStateCreateInfo color_blend;
1504 VkDynamicDsStateCreateInfo depth_stencil;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001505 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001506
Tony Barbour29645d02015-01-16 14:27:35 -07001507 memset(&viewport_create, 0, sizeof(viewport_create));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001508 viewport_create.sType = VK_STRUCTURE_TYPE_DYNAMIC_VP_STATE_CREATE_INFO;
Courtney Goeltzenleuchter1710d8d2015-02-11 14:13:34 -07001509 viewport_create.viewportAndScissorCount = 1;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001510 VkViewport viewport;
Piers Daniell735ee532015-02-23 16:23:13 -07001511 memset(&viewport, 0, sizeof(viewport));
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001512 viewport.height = (float) demo->height;
1513 viewport.width = (float) demo->width;
1514 viewport.minDepth = (float) 0.0f;
1515 viewport.maxDepth = (float) 1.0f;
Piers Daniell735ee532015-02-23 16:23:13 -07001516 viewport_create.pViewports = &viewport;
Chris Forbesfaa91732015-06-22 17:21:59 +12001517 VkRect2D scissor;
Piers Daniell735ee532015-02-23 16:23:13 -07001518 memset(&scissor, 0, sizeof(scissor));
Courtney Goeltzenleuchter1710d8d2015-02-11 14:13:34 -07001519 scissor.extent.width = demo->width;
1520 scissor.extent.height = demo->height;
1521 scissor.offset.x = 0;
1522 scissor.offset.y = 0;
Courtney Goeltzenleuchter1710d8d2015-02-11 14:13:34 -07001523 viewport_create.pScissors = &scissor;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001524
1525 memset(&raster, 0, sizeof(raster));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001526 raster.sType = VK_STRUCTURE_TYPE_DYNAMIC_RS_STATE_CREATE_INFO;
Piers Daniell735ee532015-02-23 16:23:13 -07001527 raster.lineWidth = 1.0;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001528
1529 memset(&color_blend, 0, sizeof(color_blend));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001530 color_blend.sType = VK_STRUCTURE_TYPE_DYNAMIC_CB_STATE_CREATE_INFO;
Piers Daniell735ee532015-02-23 16:23:13 -07001531 color_blend.blendConst[0] = 1.0f;
1532 color_blend.blendConst[1] = 1.0f;
1533 color_blend.blendConst[2] = 1.0f;
1534 color_blend.blendConst[3] = 1.0f;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001535
1536 memset(&depth_stencil, 0, sizeof(depth_stencil));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001537 depth_stencil.sType = VK_STRUCTURE_TYPE_DYNAMIC_DS_STATE_CREATE_INFO;
Mark Lobodzinski89d32b12015-06-12 11:14:17 -06001538 depth_stencil.minDepthBounds = 0.0f;
1539 depth_stencil.maxDepthBounds = 1.0f;
Tony Barbour29645d02015-01-16 14:27:35 -07001540 depth_stencil.stencilBackRef = 0;
1541 depth_stencil.stencilFrontRef = 0;
1542 depth_stencil.stencilReadMask = 0xff;
1543 depth_stencil.stencilWriteMask = 0xff;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001544
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001545 err = vkCreateDynamicViewportState(demo->device, &viewport_create, &demo->viewport);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001546 assert(!err);
1547
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001548 err = vkCreateDynamicRasterState(demo->device, &raster, &demo->raster);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001549 assert(!err);
1550
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001551 err = vkCreateDynamicColorBlendState(demo->device,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001552 &color_blend, &demo->color_blend);
1553 assert(!err);
1554
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001555 err = vkCreateDynamicDepthStencilState(demo->device,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001556 &depth_stencil, &demo->depth_stencil);
1557 assert(!err);
1558}
1559
Chia-I Wu63ea9262015-03-26 13:14:16 +08001560static void demo_prepare_descriptor_pool(struct demo *demo)
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001561{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001562 const VkDescriptorTypeCount type_counts[2] = {
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001563 [0] = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001564 .type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001565 .count = 1,
1566 },
1567 [1] = {
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -06001568 .type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001569 .count = DEMO_TEXTURE_COUNT,
1570 },
1571 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001572 const VkDescriptorPoolCreateInfo descriptor_pool = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001573 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001574 .pNext = NULL,
1575 .count = 2,
1576 .pTypeCount = type_counts,
1577 };
Tony Barbourfdc2d352015-04-22 09:02:32 -06001578 VkResult U_ASSERT_ONLY err;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001579
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001580 err = vkCreateDescriptorPool(demo->device,
1581 VK_DESCRIPTOR_POOL_USAGE_ONE_SHOT, 1,
Chia-I Wu63ea9262015-03-26 13:14:16 +08001582 &descriptor_pool, &demo->desc_pool);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001583 assert(!err);
1584}
1585
1586static void demo_prepare_descriptor_set(struct demo *demo)
1587{
Chia-I Wuae721ba2015-05-25 16:27:55 +08001588 VkDescriptorInfo tex_descs[DEMO_TEXTURE_COUNT];
1589 VkWriteDescriptorSet writes[2];
Tony Barbourfdc2d352015-04-22 09:02:32 -06001590 VkResult U_ASSERT_ONLY err;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001591 uint32_t count;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001592 uint32_t i;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001593
Mike Stroyanebae8322015-04-17 12:36:38 -06001594 err = vkAllocDescriptorSets(demo->device, demo->desc_pool,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001595 VK_DESCRIPTOR_SET_USAGE_STATIC,
Chia-I Wu87544e72015-02-23 10:41:08 -07001596 1, &demo->desc_layout,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001597 &demo->desc_set, &count);
1598 assert(!err && count == 1);
1599
Chia-I Wuae721ba2015-05-25 16:27:55 +08001600 memset(&tex_descs, 0, sizeof(tex_descs));
1601 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
1602 tex_descs[i].sampler = demo->textures[i].sampler;
1603 tex_descs[i].imageView = demo->textures[i].view;
1604 tex_descs[i].imageLayout = VK_IMAGE_LAYOUT_GENERAL;
1605 }
1606
1607 memset(&writes, 0, sizeof(writes));
1608
1609 writes[0].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
1610 writes[0].destSet = demo->desc_set;
1611 writes[0].count = 1;
1612 writes[0].descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
1613 writes[0].pDescriptors = &demo->uniform_data.desc;
1614
1615 writes[1].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
1616 writes[1].destSet = demo->desc_set;
1617 writes[1].destBinding = 1;
1618 writes[1].count = DEMO_TEXTURE_COUNT;
1619 writes[1].descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
1620 writes[1].pDescriptors = tex_descs;
1621
1622 err = vkUpdateDescriptorSets(demo->device, 2, writes, 0, NULL);
1623 assert(!err);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001624}
1625
Chia-I Wuf973e322015-07-08 13:34:24 +08001626static void demo_prepare_framebuffers(struct demo *demo)
1627{
Chia-I Wua74c5b22015-07-07 11:50:03 +08001628 VkAttachmentBindInfo attachments[2] = {
1629 [0] = {
1630 .view = VK_NULL_HANDLE,
1631 .layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
1632 },
1633 [1] = {
1634 .view = demo->depth.view,
1635 .layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1636 },
Chia-I Wuf973e322015-07-08 13:34:24 +08001637 };
1638 const VkFramebufferCreateInfo fb_info = {
1639 .sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO,
1640 .pNext = NULL,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001641 .renderPass = demo->render_pass,
1642 .attachmentCount = 2,
1643 .pAttachments = attachments,
Chia-I Wuf973e322015-07-08 13:34:24 +08001644 .width = demo->width,
1645 .height = demo->height,
1646 .layers = 1,
1647 };
1648 VkResult U_ASSERT_ONLY err;
1649 uint32_t i;
1650
1651 for (i = 0; i < DEMO_BUFFER_COUNT; i++) {
Chia-I Wua74c5b22015-07-07 11:50:03 +08001652 attachments[0].view = demo->buffers[i].view;
Chia-I Wuf973e322015-07-08 13:34:24 +08001653 err = vkCreateFramebuffer(demo->device, &fb_info, &demo->framebuffers[i]);
1654 assert(!err);
1655 }
1656}
1657
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001658static void demo_prepare(struct demo *demo)
1659{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001660 const VkCmdBufferCreateInfo cmd = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001661 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001662 .pNext = NULL,
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07001663 .queueNodeIndex = demo->graphics_queue_node_index,
Chia-I Wu57b23b42015-06-26 15:34:39 +08001664 .level = VK_CMD_BUFFER_LEVEL_PRIMARY,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001665 .flags = 0,
1666 };
Tony Barbourfdc2d352015-04-22 09:02:32 -06001667 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001668
1669 demo_prepare_buffers(demo);
1670 demo_prepare_depth(demo);
1671 demo_prepare_textures(demo);
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001672 demo_prepare_cube_data_buffer(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001673
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001674 demo_prepare_descriptor_layout(demo);
Chia-I Wuf973e322015-07-08 13:34:24 +08001675 demo_prepare_render_pass(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001676 demo_prepare_pipeline(demo);
1677 demo_prepare_dynamic_states(demo);
1678
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001679 for (int i = 0; i < DEMO_BUFFER_COUNT; i++) {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001680 err = vkCreateCommandBuffer(demo->device, &cmd, &demo->buffers[i].cmd);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001681 assert(!err);
1682 }
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001683
Chia-I Wu63ea9262015-03-26 13:14:16 +08001684 demo_prepare_descriptor_pool(demo);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001685 demo_prepare_descriptor_set(demo);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001686
Chia-I Wuf973e322015-07-08 13:34:24 +08001687 demo_prepare_framebuffers(demo);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001688
1689 for (int i = 0; i < DEMO_BUFFER_COUNT; i++) {
1690 demo->current_buffer = i;
1691 demo_draw_build_cmd(demo, demo->buffers[i].cmd);
1692 }
1693
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001694 /*
1695 * Prepare functions above may generate pipeline commands
1696 * that need to be flushed before beginning the render loop.
1697 */
1698 demo_flush_init_cmd(demo);
1699
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001700 demo->current_buffer = 0;
Jon Ashburn8d26c062015-04-24 09:46:24 -07001701 demo->prepared = true;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001702}
1703
David Pinedo2bb7c932015-06-18 17:03:14 -06001704static void demo_cleanup(struct demo *demo)
1705{
1706 uint32_t i;
1707
1708 demo->prepared = false;
1709
Chia-I Wuf973e322015-07-08 13:34:24 +08001710 for (i = 0; i < DEMO_BUFFER_COUNT; i++)
1711 vkDestroyObject(demo->device, VK_OBJECT_TYPE_FRAMEBUFFER, demo->framebuffers[i]);
1712
David Pinedo2bb7c932015-06-18 17:03:14 -06001713 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DESCRIPTOR_SET, demo->desc_set);
1714 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DESCRIPTOR_POOL, demo->desc_pool);
1715
1716 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DYNAMIC_VP_STATE, demo->viewport);
1717 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DYNAMIC_RS_STATE, demo->raster);
1718 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DYNAMIC_CB_STATE, demo->color_blend);
1719 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DYNAMIC_DS_STATE, demo->depth_stencil);
1720
1721 vkDestroyObject(demo->device, VK_OBJECT_TYPE_PIPELINE, demo->pipeline);
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001722 vkDestroyPipelineCache(demo->device, demo->pipelineCache);
Chia-I Wuf973e322015-07-08 13:34:24 +08001723 vkDestroyObject(demo->device, VK_OBJECT_TYPE_RENDER_PASS, demo->render_pass);
David Pinedo2bb7c932015-06-18 17:03:14 -06001724 vkDestroyObject(demo->device, VK_OBJECT_TYPE_PIPELINE_LAYOUT, demo->pipeline_layout);
1725 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DESCRIPTOR_SET_LAYOUT, demo->desc_layout);
1726
1727 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
1728 vkDestroyObject(demo->device, VK_OBJECT_TYPE_IMAGE_VIEW, demo->textures[i].view);
1729 vkDestroyObject(demo->device, VK_OBJECT_TYPE_IMAGE, demo->textures[i].image);
1730 vkFreeMemory(demo->device, demo->textures[i].mem);
1731 vkDestroyObject(demo->device, VK_OBJECT_TYPE_SAMPLER, demo->textures[i].sampler);
1732 }
1733 demo->fpDestroySwapChainWSI(demo->swap_chain);
1734
Chia-I Wua74c5b22015-07-07 11:50:03 +08001735 vkDestroyObject(demo->device, VK_OBJECT_TYPE_ATTACHMENT_VIEW, demo->depth.view);
David Pinedo2bb7c932015-06-18 17:03:14 -06001736 vkDestroyObject(demo->device, VK_OBJECT_TYPE_IMAGE, demo->depth.image);
1737 vkFreeMemory(demo->device, demo->depth.mem);
1738
1739 vkDestroyObject(demo->device, VK_OBJECT_TYPE_BUFFER_VIEW, demo->uniform_data.view);
1740 vkDestroyObject(demo->device, VK_OBJECT_TYPE_BUFFER, demo->uniform_data.buf);
1741 vkFreeMemory(demo->device, demo->uniform_data.mem);
1742
1743 for (i = 0; i < DEMO_BUFFER_COUNT; i++) {
Chia-I Wua74c5b22015-07-07 11:50:03 +08001744 vkDestroyObject(demo->device, VK_OBJECT_TYPE_ATTACHMENT_VIEW, demo->buffers[i].view);
David Pinedo2bb7c932015-06-18 17:03:14 -06001745 vkDestroyObject(demo->device, VK_OBJECT_TYPE_COMMAND_BUFFER, demo->buffers[i].cmd);
1746 }
1747
1748 vkDestroyDevice(demo->device);
Tony Barbourd70b42c2015-06-30 14:14:19 -06001749 if (demo->validate) {
1750 demo->dbgDestroyMsgCallback(demo->inst, demo->msg_callback);
1751 }
David Pinedo2bb7c932015-06-18 17:03:14 -06001752 vkDestroyInstance(demo->inst);
1753
1754#ifndef _WIN32
1755 xcb_destroy_window(demo->connection, demo->window);
1756 xcb_disconnect(demo->connection);
1757#endif // _WIN32
1758}
1759
1760// On MS-Windows, make this a global, so it's available to WndProc()
1761struct demo demo;
1762
Ian Elliott639ca472015-04-16 15:23:05 -06001763#ifdef _WIN32
1764static void demo_run(struct demo *demo)
1765{
Courtney Goeltzenleuchterb7e22702015-04-27 14:56:34 -06001766 if (!demo->prepared)
1767 return;
Ian Elliott639ca472015-04-16 15:23:05 -06001768 // Wait for work to finish before updating MVP.
1769 vkDeviceWaitIdle(demo->device);
1770 demo_update_data_buffer(demo);
1771
1772 demo_draw(demo);
1773
1774 // Wait for work to finish before updating MVP.
1775 vkDeviceWaitIdle(demo->device);
Ian Elliott639ca472015-04-16 15:23:05 -06001776
David Pinedo2bb7c932015-06-18 17:03:14 -06001777 demo->curFrame++;
1778
1779 if (demo->frameCount != INT_MAX && demo->curFrame == demo->frameCount)
1780 {
1781 demo->quit=true;
1782 demo_cleanup(demo);
1783 ExitProcess(0);
1784 }
1785
1786}
Ian Elliott639ca472015-04-16 15:23:05 -06001787
1788// MS-Windows event handling function:
1789LRESULT CALLBACK WndProc(HWND hWnd,
1790 UINT uMsg,
1791 WPARAM wParam,
1792 LPARAM lParam)
1793{
Ian Elliott639ca472015-04-16 15:23:05 -06001794 switch(uMsg)
1795 {
Ian Elliott639ca472015-04-16 15:23:05 -06001796 case WM_CLOSE:
1797 PostQuitMessage(0);
Tony Barbour3dddd5d2015-04-29 16:19:20 -06001798 break;
Ian Elliott639ca472015-04-16 15:23:05 -06001799 case WM_PAINT:
1800 demo_run(&demo);
1801 return 0;
1802 default:
1803 break;
1804 }
1805 return (DefWindowProc(hWnd, uMsg, wParam, lParam));
1806}
1807
1808static void demo_create_window(struct demo *demo)
1809{
1810 WNDCLASSEX win_class;
1811
1812 // Initialize the window class structure:
1813 win_class.cbSize = sizeof(WNDCLASSEX);
1814 win_class.style = CS_HREDRAW | CS_VREDRAW;
1815 win_class.lpfnWndProc = WndProc;
1816 win_class.cbClsExtra = 0;
1817 win_class.cbWndExtra = 0;
1818 win_class.hInstance = demo->connection; // hInstance
1819 win_class.hIcon = LoadIcon(NULL, IDI_APPLICATION);
1820 win_class.hCursor = LoadCursor(NULL, IDC_ARROW);
1821 win_class.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
1822 win_class.lpszMenuName = NULL;
1823 win_class.lpszClassName = demo->name;
1824 win_class.hIconSm = LoadIcon(NULL, IDI_WINLOGO);
1825 // Register window class:
1826 if (!RegisterClassEx(&win_class)) {
1827 // It didn't work, so try to give a useful error:
1828 printf("Unexpected error trying to start the application!\n");
1829 fflush(stdout);
1830 exit(1);
1831 }
1832 // Create window with the registered class:
Mike Stroyanb46779e2015-06-15 14:20:13 -06001833 RECT wr = { 0, 0, demo->width, demo->height };
1834 AdjustWindowRect(&wr, WS_OVERLAPPEDWINDOW, FALSE);
Ian Elliott639ca472015-04-16 15:23:05 -06001835 demo->window = CreateWindowEx(0,
1836 demo->name, // class name
1837 demo->name, // app name
1838 WS_OVERLAPPEDWINDOW | // window style
1839 WS_VISIBLE |
1840 WS_SYSMENU,
1841 100,100, // x/y coords
Mike Stroyanb46779e2015-06-15 14:20:13 -06001842 wr.right-wr.left, // width
1843 wr.bottom-wr.top, // height
Ian Elliott639ca472015-04-16 15:23:05 -06001844 NULL, // handle to parent
1845 NULL, // handle to menu
1846 demo->connection, // hInstance
1847 NULL); // no extra parameters
1848 if (!demo->window) {
1849 // It didn't work, so try to give a useful error:
1850 printf("Cannot create a window in which to draw!\n");
1851 fflush(stdout);
1852 exit(1);
1853 }
1854}
1855#else // _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001856static void demo_handle_event(struct demo *demo,
1857 const xcb_generic_event_t *event)
1858{
Piers Daniell735ee532015-02-23 16:23:13 -07001859 uint8_t event_code = event->response_type & 0x7f;
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07001860 switch (event_code) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001861 case XCB_EXPOSE:
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07001862 // TODO: Resize window
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001863 break;
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07001864 case XCB_CLIENT_MESSAGE:
1865 if((*(xcb_client_message_event_t*)event).data.data32[0] ==
1866 (*demo->atom_wm_delete_window).atom) {
1867 demo->quit = true;
1868 }
1869 break;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001870 case XCB_KEY_RELEASE:
1871 {
1872 const xcb_key_release_event_t *key =
1873 (const xcb_key_release_event_t *) event;
1874
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001875 switch (key->detail) {
1876 case 0x9: // Escape
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001877 demo->quit = true;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001878 break;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001879 case 0x71: // left arrow key
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001880 demo->spin_angle += demo->spin_increment;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001881 break;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001882 case 0x72: // right arrow key
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001883 demo->spin_angle -= demo->spin_increment;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001884 break;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001885 case 0x41:
1886 demo->pause = !demo->pause;
Piers Daniell735ee532015-02-23 16:23:13 -07001887 break;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001888 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001889 }
1890 break;
1891 default:
1892 break;
1893 }
1894}
1895
1896static void demo_run(struct demo *demo)
1897{
1898 xcb_flush(demo->connection);
1899
1900 while (!demo->quit) {
1901 xcb_generic_event_t *event;
1902
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07001903 if (demo->pause) {
1904 event = xcb_wait_for_event(demo->connection);
1905 } else {
1906 event = xcb_poll_for_event(demo->connection);
1907 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001908 if (event) {
1909 demo_handle_event(demo, event);
1910 free(event);
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07001911 }
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07001912
1913 // Wait for work to finish before updating MVP.
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001914 vkDeviceWaitIdle(demo->device);
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07001915 demo_update_data_buffer(demo);
1916
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07001917 demo_draw(demo);
Courtney Goeltzenleuchter1454f3c2014-11-18 11:28:09 -07001918
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07001919 // Wait for work to finish before updating MVP.
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001920 vkDeviceWaitIdle(demo->device);
David Pinedo2bb7c932015-06-18 17:03:14 -06001921 demo->curFrame++;
1922 if (demo->frameCount != INT_MAX && demo->curFrame == demo->frameCount)
1923 demo->quit = true;
1924
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001925 }
1926}
1927
1928static void demo_create_window(struct demo *demo)
1929{
1930 uint32_t value_mask, value_list[32];
1931
1932 demo->window = xcb_generate_id(demo->connection);
1933
1934 value_mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK;
1935 value_list[0] = demo->screen->black_pixel;
1936 value_list[1] = XCB_EVENT_MASK_KEY_RELEASE |
1937 XCB_EVENT_MASK_EXPOSURE;
1938
1939 xcb_create_window(demo->connection,
1940 XCB_COPY_FROM_PARENT,
1941 demo->window, demo->screen->root,
1942 0, 0, demo->width, demo->height, 0,
1943 XCB_WINDOW_CLASS_INPUT_OUTPUT,
1944 demo->screen->root_visual,
1945 value_mask, value_list);
1946
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07001947 /* Magic code that will send notification when window is destroyed */
1948 xcb_intern_atom_cookie_t cookie = xcb_intern_atom(demo->connection, 1, 12,
1949 "WM_PROTOCOLS");
1950 xcb_intern_atom_reply_t* reply = xcb_intern_atom_reply(demo->connection, cookie, 0);
1951
1952 xcb_intern_atom_cookie_t cookie2 = xcb_intern_atom(demo->connection, 0, 16, "WM_DELETE_WINDOW");
1953 demo->atom_wm_delete_window = xcb_intern_atom_reply(demo->connection, cookie2, 0);
1954
1955 xcb_change_property(demo->connection, XCB_PROP_MODE_REPLACE,
1956 demo->window, (*reply).atom, 4, 32, 1,
1957 &(*demo->atom_wm_delete_window).atom);
1958 free(reply);
1959
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001960 xcb_map_window(demo->connection, demo->window);
David Pinedo2bb7c932015-06-18 17:03:14 -06001961
1962 // Force the x/y coordinates to 100,100 results are identical in consecutive runs
1963 const uint32_t coords[] = {100, 100};
1964 xcb_configure_window(demo->connection, demo->window,
1965 XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y, coords);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001966}
Ian Elliott639ca472015-04-16 15:23:05 -06001967#endif // _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001968
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06001969/*
1970 * Return 1 (true) if all layer names specified in check_names
1971 * can be found in given layer properties.
1972 */
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06001973static VkBool32 demo_check_layers(uint32_t check_count, char **check_names,
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06001974 uint32_t layer_count, VkLayerProperties *layers)
1975{
1976 for (uint32_t i = 0; i < check_count; i++) {
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06001977 VkBool32 found = 0;
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06001978 for (uint32_t j = 0; j < layer_count; j++) {
1979 if (!strcmp(check_names[i], layers[j].layerName)) {
1980 found = 1;
1981 }
1982 }
1983 if (!found) {
1984 fprintf(stderr, "Cannot find layer: %s\n", check_names[i]);
1985 return 0;
1986 }
1987 }
1988 return 1;
1989}
1990
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001991static void demo_init_vk(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001992{
Tobin Ehlis99239dc2015-04-16 18:04:57 -06001993 VkResult err;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06001994 char *extension_names[64];
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06001995 VkExtensionProperties *instance_extensions;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06001996 VkLayerProperties *instance_layers;
1997 VkLayerProperties *device_layers;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06001998 uint32_t instance_extension_count = 0;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06001999 uint32_t instance_layer_count = 0;
2000 uint32_t enabled_extension_count = 0;
2001 uint32_t enabled_layer_count = 0;
2002
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002003 char *instance_validation_layers[] = {
2004 "MemTracker",
2005 };
2006
2007 char *device_validation_layers[] = {
2008 "MemTracker",
2009 };
2010
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002011 /* Look for validation layers */
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06002012 VkBool32 validation_found = 0;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002013 err = vkGetGlobalLayerProperties(&instance_layer_count, NULL);
Tobin Ehlis99239dc2015-04-16 18:04:57 -06002014 assert(!err);
2015
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002016 instance_layers = malloc(sizeof(VkLayerProperties) * instance_layer_count);
2017 err = vkGetGlobalLayerProperties(&instance_layer_count, instance_layers);
2018 assert(!err);
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002019
2020 if (demo->validate) {
2021 validation_found = demo_check_layers(ARRAY_SIZE(instance_validation_layers), instance_validation_layers,
2022 instance_layer_count, instance_layers);
2023 if (!validation_found) {
2024 ERR_EXIT("vkGetGlobalLayerProperties failed to find"
2025 "required validation layer.\n\n"
2026 "Please look at the Getting Started guide for additional "
2027 "information.\n",
2028 "vkCreateInstance Failure");
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002029 }
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002030 enabled_layer_count = ARRAY_SIZE(instance_validation_layers);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002031 }
2032
2033 err = vkGetGlobalExtensionProperties(NULL, &instance_extension_count, NULL);
2034 assert(!err);
2035
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06002036 VkBool32 WSIextFound = 0;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002037 memset(extension_names, 0, sizeof(extension_names));
2038 instance_extensions = malloc(sizeof(VkExtensionProperties) * instance_extension_count);
2039 err = vkGetGlobalExtensionProperties(NULL, &instance_extension_count, instance_extensions);
2040 assert(!err);
2041 for (uint32_t i = 0; i < instance_extension_count; i++) {
2042 if (!strcmp(VK_WSI_LUNARG_EXTENSION_NAME, instance_extensions[i].extName)) {
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002043 WSIextFound = 1;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002044 extension_names[enabled_extension_count++] = VK_WSI_LUNARG_EXTENSION_NAME;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002045 }
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002046 if (!strcmp(DEBUG_REPORT_EXTENSION_NAME, instance_extensions[i].extName)) {
2047 if (demo->validate) {
2048 extension_names[enabled_extension_count++] = DEBUG_REPORT_EXTENSION_NAME;
2049 }
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002050 }
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002051 assert(enabled_extension_count < 64);
Tobin Ehlis99239dc2015-04-16 18:04:57 -06002052 }
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002053 if (!WSIextFound) {
Tony Barbourecf1b2b2015-06-24 16:06:58 -06002054 ERR_EXIT("vkGetGlobalExtensionProperties failed to find the "
Ian Elliott65152912015-04-28 13:22:33 -06002055 "\"VK_WSI_LunarG\" extension.\n\nDo you have a compatible "
2056 "Vulkan installable client driver (ICD) installed?\nPlease "
2057 "look at the Getting Started guide for additional "
2058 "information.\n",
2059 "vkCreateInstance Failure");
2060 }
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06002061 const VkApplicationInfo app = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002062 .sType = VK_STRUCTURE_TYPE_APPLICATION_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002063 .pNext = NULL,
Ian Elliott44e33f72015-04-28 10:52:52 -06002064 .pAppName = APP_SHORT_NAME,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002065 .appVersion = 0,
Ian Elliott44e33f72015-04-28 10:52:52 -06002066 .pEngineName = APP_SHORT_NAME,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002067 .engineVersion = 0,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002068 .apiVersion = VK_API_VERSION,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002069 };
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002070 VkInstanceCreateInfo inst_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002071 .sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
Jon Ashburnab46b362015-04-04 14:52:07 -06002072 .pNext = NULL,
2073 .pAppInfo = &app,
2074 .pAllocCb = NULL,
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002075 .layerCount = enabled_layer_count,
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002076 .ppEnabledLayerNames = (const char *const*) ((demo->validate) ? instance_validation_layers : NULL),
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002077 .extensionCount = enabled_extension_count,
2078 .ppEnabledExtensionNames = (const char *const*) extension_names,
Jon Ashburnab46b362015-04-04 14:52:07 -06002079 };
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -06002080 const VkDeviceQueueCreateInfo queue = {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002081 .queueNodeIndex = 0,
2082 .queueCount = 1,
2083 };
Ian Elliott097d9f32015-04-28 11:35:02 -06002084
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06002085 uint32_t gpu_count;
2086 uint32_t i;
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07002087 uint32_t queue_count;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002088
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002089 err = vkCreateInstance(&inst_info, &demo->inst);
Ian Elliott07264132015-04-28 11:35:02 -06002090 if (err == VK_ERROR_INCOMPATIBLE_DRIVER) {
2091 ERR_EXIT("Cannot find a compatible Vulkan installable client driver "
Ian Elliott65152912015-04-28 13:22:33 -06002092 "(ICD).\n\nPlease look at the Getting Started guide for "
Ian Elliott07264132015-04-28 11:35:02 -06002093 "additional information.\n",
2094 "vkCreateInstance Failure");
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002095 } else if (err == VK_ERROR_INVALID_EXTENSION) {
2096 ERR_EXIT("Cannot find a specified extension library"
2097 ".\nMake sure your layers path is set appropriately\n",
2098 "vkCreateInstance Failure");
Ian Elliott07264132015-04-28 11:35:02 -06002099 } else if (err) {
Ian Elliott65152912015-04-28 13:22:33 -06002100 ERR_EXIT("vkCreateInstance failed.\n\nDo you have a compatible Vulkan "
2101 "installable client driver (ICD) installed?\nPlease look at "
Ian Elliott07264132015-04-28 11:35:02 -06002102 "the Getting Started guide for additional information.\n",
2103 "vkCreateInstance Failure");
Ian Elliott3979e282015-04-03 15:24:55 -06002104 }
Jon Ashburnab46b362015-04-04 14:52:07 -06002105
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002106 free(instance_layers);
2107 free(instance_extensions);
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002108
Jon Ashburn07c0c0c2015-04-15 11:31:12 -06002109 gpu_count = 1;
2110 err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, &demo->gpu);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002111 assert(!err && gpu_count == 1);
2112
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002113 /* Look for validation layers */
2114 validation_found = 0;
2115 enabled_layer_count = 0;
2116 uint32_t device_layer_count = 0;
2117 err = vkGetPhysicalDeviceLayerProperties(demo->gpu, &device_layer_count, NULL);
2118 assert(!err);
2119
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002120 device_layers = malloc(sizeof(VkLayerProperties) * device_layer_count);
2121 err = vkGetPhysicalDeviceLayerProperties(demo->gpu, &device_layer_count, device_layers);
2122 assert(!err);
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002123
2124 if (demo->validate) {
2125 validation_found = demo_check_layers(ARRAY_SIZE(device_validation_layers), device_validation_layers,
2126 device_layer_count, device_layers);
2127 if (!validation_found) {
2128 ERR_EXIT("vkGetPhysicalDeviceLayerProperties failed to find"
2129 "a required validation layer.\n\n"
2130 "Please look at the Getting Started guide for additional "
2131 "information.\n",
2132 "vkCreateDevice Failure");
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002133 }
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002134 enabled_layer_count = ARRAY_SIZE(device_validation_layers);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002135 }
2136
2137 uint32_t device_extension_count = 0;
2138 VkExtensionProperties *device_extensions = NULL;
2139 err = vkGetPhysicalDeviceExtensionProperties(
2140 demo->gpu, NULL, &device_extension_count, NULL);
2141 assert(!err);
2142
2143 WSIextFound = 0;
2144 enabled_extension_count = 0;
2145 memset(extension_names, 0, sizeof(extension_names));
2146 device_extensions = malloc(sizeof(VkExtensionProperties) * device_extension_count);
2147 err = vkGetPhysicalDeviceExtensionProperties(
2148 demo->gpu, NULL, &device_extension_count, device_extensions);
2149 assert(!err);
Courtney Goeltzenleuchterff6e23a2015-07-05 22:08:51 -06002150#if 0
2151 /* Will need this check in future */
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002152 for (uint32_t i = 0; i < device_extension_count; i++) {
2153 if (!strcmp(VK_WSI_LUNARG_EXTENSION_NAME, device_extensions[i].extName)) {
2154 WSIextFound = 1;
2155 extension_names[enabled_extension_count++] = VK_WSI_LUNARG_EXTENSION_NAME;
2156 }
2157 assert(enabled_extension_count < 64);
2158 }
2159 if (!WSIextFound) {
2160 ERR_EXIT("vkGetGlobalExtensionProperties failed to find the "
2161 "\"VK_WSI_LunarG\" extension.\n\nDo you have a compatible "
2162 "Vulkan installable client driver (ICD) installed?\nPlease "
2163 "look at the Getting Started guide for additional "
2164 "information.\n",
2165 "vkCreateInstance Failure");
2166 }
Courtney Goeltzenleuchterff6e23a2015-07-05 22:08:51 -06002167#endif
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002168
2169 VkDeviceCreateInfo device = {
2170 .sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
2171 .pNext = NULL,
2172 .queueRecordCount = 1,
2173 .pRequestedQueues = &queue,
2174 .layerCount = enabled_layer_count,
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002175 .ppEnabledLayerNames = (const char *const*) ((demo->validate) ? device_validation_layers : NULL),
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002176 .extensionCount = enabled_extension_count,
2177 .ppEnabledExtensionNames = (const char *const*) extension_names,
2178 .flags = 0,
2179 };
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002180
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002181 if (demo->validate) {
Tony Barbourd70b42c2015-06-30 14:14:19 -06002182 demo->dbgCreateMsgCallback = vkGetInstanceProcAddr(demo->inst, "vkDbgCreateMsgCallback");
2183 demo->dbgDestroyMsgCallback = vkGetInstanceProcAddr(demo->inst, "vkDbgDestroyMsgCallback");
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002184 if (!demo->dbgCreateMsgCallback) {
2185 ERR_EXIT("GetProcAddr: Unable to find vkDbgCreateMsgCallback\n",
2186 "vkGetProcAddr Failure");
2187 }
Tony Barbourd70b42c2015-06-30 14:14:19 -06002188 if (!demo->dbgDestroyMsgCallback) {
2189 ERR_EXIT("GetProcAddr: Unable to find vkDbgDestroyMsgCallback\n",
2190 "vkGetProcAddr Failure");
2191 }
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002192 err = demo->dbgCreateMsgCallback(
2193 demo->inst,
2194 VK_DBG_REPORT_ERROR_BIT | VK_DBG_REPORT_WARN_BIT,
2195 dbgFunc, NULL,
2196 &demo->msg_callback);
2197 switch (err) {
2198 case VK_SUCCESS:
2199 break;
2200 case VK_ERROR_INVALID_POINTER:
2201 ERR_EXIT("dbgCreateMsgCallback: Invalid pointer\n",
2202 "dbgCreateMsgCallback Failure");
2203 break;
2204 case VK_ERROR_OUT_OF_HOST_MEMORY:
2205 ERR_EXIT("dbgCreateMsgCallback: out of host memory\n",
2206 "dbgCreateMsgCallback Failure");
2207 break;
2208 default:
2209 ERR_EXIT("dbgCreateMsgCallback: unknown failure\n",
2210 "dbgCreateMsgCallback Failure");
2211 break;
2212 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002213 }
2214
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002215 err = vkCreateDevice(demo->gpu, &device, &demo->device);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002216 assert(!err);
2217
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002218 free(device_layers);
2219
Ian Elliott673898b2015-06-22 15:07:49 -06002220 GET_DEVICE_PROC_ADDR(demo->device, CreateSwapChainWSI);
2221 GET_DEVICE_PROC_ADDR(demo->device, CreateSwapChainWSI);
2222 GET_DEVICE_PROC_ADDR(demo->device, DestroySwapChainWSI);
2223 GET_DEVICE_PROC_ADDR(demo->device, GetSwapChainInfoWSI);
2224 GET_DEVICE_PROC_ADDR(demo->device, QueuePresentWSI);
Jon Ashburn0b85d052015-05-21 18:13:33 -06002225
Tony Barbourecf1b2b2015-06-24 16:06:58 -06002226 err = vkGetPhysicalDeviceProperties(demo->gpu, &demo->gpu_props);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06002227 assert(!err);
2228
Tony Barbourecf1b2b2015-06-24 16:06:58 -06002229 err = vkGetPhysicalDeviceQueueCount(demo->gpu, &queue_count);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06002230 assert(!err);
2231
Tony Barbourecf1b2b2015-06-24 16:06:58 -06002232 demo->queue_props = (VkPhysicalDeviceQueueProperties *) malloc(queue_count * sizeof(VkPhysicalDeviceQueueProperties));
2233 err = vkGetPhysicalDeviceQueueProperties(demo->gpu, queue_count, demo->queue_props);
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07002234 assert(!err);
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07002235 assert(queue_count >= 1);
2236
Mark Lobodzinskida9b1092015-04-16 11:44:05 -05002237 // Graphics queue and MemMgr queue can be separate.
2238 // TODO: Add support for separate queues, including synchronization,
Mark Lobodzinski6baaec72015-05-11 17:21:15 -05002239 // and appropriate tracking for QueueSubmit
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07002240 for (i = 0; i < queue_count; i++) {
Mark Lobodzinski6baaec72015-05-11 17:21:15 -05002241 if (demo->queue_props[i].queueFlags & VK_QUEUE_GRAPHICS_BIT)
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07002242 break;
2243 }
2244 assert(i < queue_count);
2245 demo->graphics_queue_node_index = i;
2246
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002247 err = vkGetDeviceQueue(demo->device, demo->graphics_queue_node_index,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002248 0, &demo->queue);
2249 assert(!err);
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002250
Jon Ashburnd7130cb2015-06-16 12:44:51 -06002251 // for now hardcode format till get WSI support
2252 demo->format = VK_FORMAT_B8G8R8A8_UNORM;
Ian Elliotte4602cd2015-04-21 16:41:02 -06002253
David Pinedo2bb7c932015-06-18 17:03:14 -06002254 demo->quit = false;
2255 demo->curFrame = 0;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06002256
2257 // Get Memory information and properties
2258 err = vkGetPhysicalDeviceMemoryProperties(demo->gpu, &demo->memory_properties);
2259 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002260}
2261
2262static void demo_init_connection(struct demo *demo)
2263{
Ian Elliott639ca472015-04-16 15:23:05 -06002264#ifndef _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002265 const xcb_setup_t *setup;
2266 xcb_screen_iterator_t iter;
2267 int scr;
2268
2269 demo->connection = xcb_connect(NULL, &scr);
Ian Elliott3979e282015-04-03 15:24:55 -06002270 if (demo->connection == NULL) {
2271 printf("Cannot find a compatible Vulkan installable client driver "
2272 "(ICD).\nExiting ...\n");
2273 fflush(stdout);
2274 exit(1);
2275 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002276
2277 setup = xcb_get_setup(demo->connection);
2278 iter = xcb_setup_roots_iterator(setup);
2279 while (scr-- > 0)
2280 xcb_screen_next(&iter);
2281
2282 demo->screen = iter.data;
Ian Elliott639ca472015-04-16 15:23:05 -06002283#endif // _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002284}
2285
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002286static void demo_init(struct demo *demo, int argc, char **argv)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002287{
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002288 vec3 eye = {0.0f, 3.0f, 5.0f};
2289 vec3 origin = {0, 0, 0};
Chia-I Wuae3b55d2015-04-22 14:56:17 +08002290 vec3 up = {0.0f, 1.0f, 0.0};
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002291
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002292 memset(demo, 0, sizeof(*demo));
David Pinedo2bb7c932015-06-18 17:03:14 -06002293 demo->frameCount = INT_MAX;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002294
Piers Daniell735ee532015-02-23 16:23:13 -07002295 for (int i = 1; i < argc; i++) {
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002296 if (strcmp(argv[i], "--use_staging") == 0) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002297 demo->use_staging_buffer = true;
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002298 continue;
Ian Elliott639ca472015-04-16 15:23:05 -06002299 }
Cody Northrop1fedb212015-05-28 11:27:16 -06002300 if (strcmp(argv[i], "--use_glsl") == 0) {
2301 demo->use_glsl = true;
2302 continue;
2303 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002304 if (strcmp(argv[i], "--validate") == 0) {
2305 demo->validate = true;
2306 continue;
2307 }
David Pinedo2bb7c932015-06-18 17:03:14 -06002308 if (strcmp(argv[i], "--c") == 0 &&
2309 demo->frameCount == INT_MAX &&
2310 i < argc-1 &&
2311 sscanf(argv[i+1],"%d", &demo->frameCount) == 1 &&
2312 demo->frameCount >= 0)
2313 {
2314 i++;
2315 continue;
2316 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002317
David Pinedo2bb7c932015-06-18 17:03:14 -06002318 fprintf(stderr, "Usage:\n %s [--use_staging] [--validate] [--c <framecount>]\n", APP_SHORT_NAME);
Ian Elliott639ca472015-04-16 15:23:05 -06002319 fflush(stderr);
2320 exit(1);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002321 }
2322
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002323 demo_init_connection(demo);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002324 demo_init_vk(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002325
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002326 demo->width = 500;
2327 demo->height = 500;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002328
2329 demo->spin_angle = 0.01f;
2330 demo->spin_increment = 0.01f;
2331 demo->pause = false;
2332
Piers Daniell735ee532015-02-23 16:23:13 -07002333 mat4x4_perspective(demo->projection_matrix, (float)degreesToRadians(45.0f), 1.0f, 0.1f, 100.0f);
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002334 mat4x4_look_at(demo->view_matrix, eye, origin, up);
2335 mat4x4_identity(demo->model_matrix);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002336}
2337
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002338
Ian Elliott639ca472015-04-16 15:23:05 -06002339#ifdef _WIN32
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002340extern int __getmainargs(
2341 int * _Argc,
2342 char *** _Argv,
2343 char *** _Env,
2344 int _DoWildCard,
2345 int * new_mode);
Ian Elliottf9cf78c2015-04-28 10:33:11 -06002346
Ian Elliotta748eaf2015-04-28 15:50:36 -06002347int WINAPI WinMain(HINSTANCE hInstance,
2348 HINSTANCE hPrevInstance,
2349 LPSTR pCmdLine,
2350 int nCmdShow)
Ian Elliott639ca472015-04-16 15:23:05 -06002351{
2352 MSG msg; // message
2353 bool done; // flag saying when app is complete
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002354 int argc;
2355 char** argv;
2356 char** env;
2357 int new_mode = 0;
Ian Elliott639ca472015-04-16 15:23:05 -06002358
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002359 __getmainargs(&argc,&argv,&env,0,&new_mode);
2360
2361 demo_init(&demo, argc, argv);
2362 demo.connection = hInstance;
2363 strncpy(demo.name, "cube", APP_NAME_STR_LEN);
Ian Elliott639ca472015-04-16 15:23:05 -06002364 demo_create_window(&demo);
2365
2366 demo_prepare(&demo);
2367
2368 done = false; //initialize loop condition variable
2369 /* main message loop*/
2370 while(!done)
2371 {
Ian Elliotta748eaf2015-04-28 15:50:36 -06002372 PeekMessage(&msg, NULL, 0, 0, PM_REMOVE);
Ian Elliott639ca472015-04-16 15:23:05 -06002373 if (msg.message == WM_QUIT) //check for a quit message
2374 {
2375 done = true; //if found, quit app
2376 }
2377 else
2378 {
2379 /* Translate and dispatch to event queue*/
2380 TranslateMessage(&msg);
2381 DispatchMessage(&msg);
2382 }
2383 }
2384
2385 demo_cleanup(&demo);
2386
Tony Barbourf9921732015-04-22 11:36:22 -06002387 return (int) msg.wParam;
Ian Elliott639ca472015-04-16 15:23:05 -06002388}
2389#else // _WIN32
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002390int main(int argc, char **argv)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002391{
2392 struct demo demo;
2393
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002394 demo_init(&demo, argc, argv);
Chia-I Wucbb564e2015-04-16 22:02:10 +08002395 demo_create_window(&demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002396
2397 demo_prepare(&demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002398 demo_run(&demo);
2399
2400 demo_cleanup(&demo);
2401
2402 return 0;
2403}
Ian Elliott639ca472015-04-16 15:23:05 -06002404#endif // _WIN32