blob: 765734bcb6abce4651e0a66010c449edc2e3d6c2 [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
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600324 VkColorAttachmentView 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;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600332 VkDepthStencilView view;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600333 } depth;
334
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600335 struct texture_object textures[DEMO_TEXTURE_COUNT];
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600336
337 struct {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600338 VkBuffer buf;
Mark 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;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600348 VkPipeline pipeline;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600349
Courtney Goeltzenleuchter5e6d1e92015-04-10 16:24:50 -0600350 VkDynamicVpState viewport;
351 VkDynamicRsState raster;
352 VkDynamicCbState color_blend;
353 VkDynamicDsState depth_stencil;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600354
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600355 mat4x4 projection_matrix;
356 mat4x4 view_matrix;
357 mat4x4 model_matrix;
358
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600359 float spin_angle;
360 float spin_increment;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600361 bool pause;
362
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600363 VkDescriptorPool desc_pool;
364 VkDescriptorSet desc_set;
Chia-I Wu6a3c8972015-01-04 16:27:24 +0800365
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600366 bool quit;
David Pinedo2bb7c932015-06-18 17:03:14 -0600367 int32_t curFrame;
368 int32_t frameCount;
Tony Barbour3dddd5d2015-04-29 16:19:20 -0600369 bool validate;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -0600370 PFN_vkDbgCreateMsgCallback dbgCreateMsgCallback;
Tony Barbourd70b42c2015-06-30 14:14:19 -0600371 PFN_vkDbgDestroyMsgCallback dbgDestroyMsgCallback;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -0600372 VkDbgMsgCallback msg_callback;
373
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600374 uint32_t current_buffer;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600375};
376
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600377static VkResult memory_type_from_properties(struct demo *demo, uint32_t typeBits, VkFlags properties, uint32_t *typeIndex)
378{
379 // Search memtypes to find first index with those properties
380 for (uint32_t i = 0; i < 32; i++) {
381 if ((typeBits & 1) == 1) {
382 // Type is available, does it match user properties?
383 if ((demo->memory_properties.memoryTypes[i].propertyFlags & properties) == properties) {
384 *typeIndex = i;
385 return VK_SUCCESS;
386 }
387 }
388 typeBits >>= 1;
389 }
390 // No memory types matched, return failure
391 return VK_UNSUPPORTED;
392}
393
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600394static void demo_flush_init_cmd(struct demo *demo)
395{
Tony Barbourfdc2d352015-04-22 09:02:32 -0600396 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600397
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600398 if (demo->cmd == VK_NULL_HANDLE)
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600399 return;
400
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600401 err = vkEndCommandBuffer(demo->cmd);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600402 assert(!err);
403
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600404 const VkCmdBuffer cmd_bufs[] = { demo->cmd };
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600405
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600406 err = vkQueueSubmit(demo->queue, 1, cmd_bufs, VK_NULL_HANDLE);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600407 assert(!err);
408
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600409 err = vkQueueWaitIdle(demo->queue);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600410 assert(!err);
411
Mike Stroyanebae8322015-04-17 12:36:38 -0600412 vkDestroyObject(demo->device, VK_OBJECT_TYPE_COMMAND_BUFFER, demo->cmd);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600413 demo->cmd = VK_NULL_HANDLE;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600414}
415
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600416static void demo_set_image_layout(
417 struct demo *demo,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600418 VkImage image,
malnasseeb25d3f2015-06-03 17:28:38 -0400419 VkImageAspect aspect,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600420 VkImageLayout old_image_layout,
421 VkImageLayout new_image_layout)
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600422{
Tony Barbourfdc2d352015-04-22 09:02:32 -0600423 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600424
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600425 if (demo->cmd == VK_NULL_HANDLE) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600426 const VkCmdBufferCreateInfo cmd = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600427 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600428 .pNext = NULL,
429 .queueNodeIndex = demo->graphics_queue_node_index,
Chia-I Wu57b23b42015-06-26 15:34:39 +0800430 .level = VK_CMD_BUFFER_LEVEL_PRIMARY,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600431 .flags = 0,
432 };
433
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600434 err = vkCreateCommandBuffer(demo->device, &cmd, &demo->cmd);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600435 assert(!err);
436
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600437 VkCmdBufferBeginInfo cmd_buf_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600438 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600439 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -0600440 .flags = VK_CMD_BUFFER_OPTIMIZE_SMALL_BATCH_BIT |
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600441 VK_CMD_BUFFER_OPTIMIZE_ONE_TIME_SUBMIT_BIT,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600442 };
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600443 err = vkBeginCommandBuffer(demo->cmd, &cmd_buf_info);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600444 }
445
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600446 VkImageMemoryBarrier image_memory_barrier = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600447 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600448 .pNext = NULL,
449 .outputMask = 0,
450 .inputMask = 0,
451 .oldLayout = old_image_layout,
452 .newLayout = new_image_layout,
453 .image = image,
malnasseeb25d3f2015-06-03 17:28:38 -0400454 .subresourceRange = { aspect, 0, 1, 0, 0 }
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600455 };
456
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600457 if (new_image_layout == VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL) {
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600458 /* Make sure anything that was copying from this image has completed */
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -0600459 image_memory_barrier.inputMask = VK_MEMORY_INPUT_TRANSFER_BIT;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600460 }
461
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600462 if (new_image_layout == VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL) {
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600463 /* Make sure any Copy or CPU writes to image are flushed */
Courtney Goeltzenleuchter2bda8332015-04-29 17:16:21 -0600464 image_memory_barrier.outputMask = VK_MEMORY_OUTPUT_HOST_WRITE_BIT | VK_MEMORY_OUTPUT_TRANSFER_BIT;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600465 }
466
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600467 VkImageMemoryBarrier *pmemory_barrier = &image_memory_barrier;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600468
Tony Barbour50bbca42015-06-29 16:20:35 -0600469 VkPipelineStageFlags src_stages = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
470 VkPipelineStageFlags dest_stages = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600471
Tony Barbour50bbca42015-06-29 16:20:35 -0600472 vkCmdPipelineBarrier(demo->cmd, src_stages, dest_stages, false, 1, (const void **)&pmemory_barrier);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600473}
474
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600475static void demo_draw_build_cmd(struct demo *demo, VkCmdBuffer cmd_buf)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600476{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600477 const VkColorAttachmentBindInfo color_attachment = {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600478 .view = demo->buffers[demo->current_buffer].view,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600479 .layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600480 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600481 const VkDepthStencilBindInfo depth_stencil = {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600482 .view = demo->depth.view,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600483 .layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600484 };
Chris Forbes5cf96082015-06-24 14:34:53 +1200485 const VkClearColorValue clear_color = {
486 .f32 = { 0.2f, 0.2f, 0.2f, 0.2f },
Courtney Goeltzenleuchter374553c2015-04-03 16:35:32 -0600487 };
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600488 const float clear_depth = 1.0f;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600489 VkCmdBufferBeginInfo cmd_buf_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600490 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO,
Courtney Goeltzenleuchter53968d82015-04-03 15:25:24 -0600491 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -0600492 .flags = VK_CMD_BUFFER_OPTIMIZE_SMALL_BATCH_BIT |
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600493 VK_CMD_BUFFER_OPTIMIZE_ONE_TIME_SUBMIT_BIT,
Jon Ashburn60ccfbe2014-12-31 17:08:35 -0700494 };
Tony Barbourfdc2d352015-04-22 09:02:32 -0600495 VkResult U_ASSERT_ONLY err;
Chris Forbes40a71562015-06-17 11:36:12 +1200496 VkAttachmentLoadOp load_op = VK_ATTACHMENT_LOAD_OP_CLEAR;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600497 VkAttachmentStoreOp store_op = VK_ATTACHMENT_STORE_OP_DONT_CARE;
498 const VkFramebufferCreateInfo fb_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600499 .sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO,
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700500 .pNext = NULL,
501 .colorAttachmentCount = 1,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600502 .pColorAttachments = (VkColorAttachmentBindInfo*) &color_attachment,
503 .pDepthStencilAttachment = (VkDepthStencilBindInfo*) &depth_stencil,
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700504 .sampleCount = 1,
Mark Lobodzinskic06b7412015-01-27 13:24:03 -0600505 .width = demo->width,
506 .height = demo->height,
507 .layers = 1,
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700508 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600509 VkRenderPassCreateInfo rp_info;
510 VkRenderPassBegin rp_begin;
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700511
Chia-I Wu57b23b42015-06-26 15:34:39 +0800512 rp_begin.contents = VK_RENDER_PASS_CONTENTS_INLINE;
513
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700514 memset(&rp_info, 0 , sizeof(rp_info));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600515 err = vkCreateFramebuffer(demo->device, &fb_info, &rp_begin.framebuffer);
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700516 assert(!err);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600517 rp_info.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO;
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700518 rp_info.renderArea.extent.width = demo->width;
519 rp_info.renderArea.extent.height = demo->height;
Courtney Goeltzenleuchter53968d82015-04-03 15:25:24 -0600520 rp_info.colorAttachmentCount = fb_info.colorAttachmentCount;
521 rp_info.pColorFormats = &demo->format;
522 rp_info.pColorLayouts = &color_attachment.layout;
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700523 rp_info.pColorLoadOps = &load_op;
524 rp_info.pColorStoreOps = &store_op;
Courtney Goeltzenleuchter53968d82015-04-03 15:25:24 -0600525 rp_info.pColorLoadClearValues = &clear_color;
Tony Barbour72304ef2015-04-16 15:59:00 -0600526 rp_info.depthStencilFormat = VK_FORMAT_D16_UNORM;
Courtney Goeltzenleuchter53968d82015-04-03 15:25:24 -0600527 rp_info.depthStencilLayout = depth_stencil.layout;
Chris Forbesa62b8a12015-06-22 18:47:28 +1200528 rp_info.depthLoadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
Courtney Goeltzenleuchter53968d82015-04-03 15:25:24 -0600529 rp_info.depthLoadClearValue = clear_depth;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600530 rp_info.depthStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
531 rp_info.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
Courtney Goeltzenleuchter53968d82015-04-03 15:25:24 -0600532 rp_info.stencilLoadClearValue = 0;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600533 rp_info.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
Tony Barbourd70b42c2015-06-30 14:14:19 -0600534 rp_info.sampleCount = 1;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600535 err = vkCreateRenderPass(demo->device, &rp_info, &rp_begin.renderPass);
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700536 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600537
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600538 err = vkBeginCommandBuffer(cmd_buf, &cmd_buf_info);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600539 assert(!err);
540
Tobin Ehlis080219d2015-06-24 15:53:07 -0600541 vkCmdBeginRenderPass(cmd_buf, &rp_begin);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600542 vkCmdBindPipeline(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600543 demo->pipeline);
Mark Lobodzinskic6e8b3d2015-06-15 13:21:21 -0600544 vkCmdBindDescriptorSets(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS, demo->pipeline_layout,
Cody Northropfb5185a2015-04-16 13:41:56 -0600545 0, 1, &demo->desc_set, 0, NULL);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600546
Tony Barbour72304ef2015-04-16 15:59:00 -0600547 vkCmdBindDynamicStateObject(cmd_buf, VK_STATE_BIND_POINT_VIEWPORT, demo->viewport);
548 vkCmdBindDynamicStateObject(cmd_buf, VK_STATE_BIND_POINT_RASTER, demo->raster);
549 vkCmdBindDynamicStateObject(cmd_buf, VK_STATE_BIND_POINT_COLOR_BLEND,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600550 demo->color_blend);
Tony Barbour72304ef2015-04-16 15:59:00 -0600551 vkCmdBindDynamicStateObject(cmd_buf, VK_STATE_BIND_POINT_DEPTH_STENCIL,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600552 demo->depth_stencil);
553
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600554 vkCmdDraw(cmd_buf, 0, 12 * 3, 0, 1);
Chia-I Wu57b23b42015-06-26 15:34:39 +0800555 vkCmdEndRenderPass(cmd_buf);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600556
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600557 err = vkEndCommandBuffer(cmd_buf);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600558 assert(!err);
Courtney Goeltzenleuchter94901dc2015-02-25 17:53:18 -0700559
Mike Stroyanebae8322015-04-17 12:36:38 -0600560 vkDestroyObject(demo->device, VK_OBJECT_TYPE_RENDER_PASS, rp_begin.renderPass);
561 vkDestroyObject(demo->device, VK_OBJECT_TYPE_FRAMEBUFFER, rp_begin.framebuffer);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600562}
563
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600564
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600565void demo_update_data_buffer(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600566{
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600567 mat4x4 MVP, Model, VP;
568 int matrixSize = sizeof(MVP);
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600569 uint8_t *pData;
Tony Barbourfdc2d352015-04-22 09:02:32 -0600570 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600571
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600572 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600573
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600574 // Rotate 22.5 degrees around the Y axis
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600575 mat4x4_dup(Model, demo->model_matrix);
Piers Daniell735ee532015-02-23 16:23:13 -0700576 mat4x4_rotate(demo->model_matrix, Model, 0.0f, 1.0f, 0.0f, (float)degreesToRadians(demo->spin_angle));
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600577 mat4x4_mul(MVP, VP, demo->model_matrix);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600578
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500579 err = vkMapMemory(demo->device, demo->uniform_data.mem, 0, 0, 0, (void **) &pData);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600580 assert(!err);
581
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600582 memcpy(pData, (const void*) &MVP[0][0], matrixSize);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600583
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500584 err = vkUnmapMemory(demo->device, demo->uniform_data.mem);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600585 assert(!err);
586}
587
588static void demo_draw(struct demo *demo)
589{
Chia-I Wucbb564e2015-04-16 22:02:10 +0800590 const VkPresentInfoWSI present = {
591 .sType = VK_STRUCTURE_TYPE_PRESENT_INFO_WSI,
592 .pNext = NULL,
593 .image = demo->buffers[demo->current_buffer].image,
594 .flipInterval = 0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600595 };
Tony Barbourfdc2d352015-04-22 09:02:32 -0600596 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600597
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600598 err = vkQueueSubmit(demo->queue, 1, &demo->buffers[demo->current_buffer].cmd,
599 VK_NULL_HANDLE);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600600 assert(!err);
601
Jon Ashburn0b85d052015-05-21 18:13:33 -0600602 err = demo->fpQueuePresentWSI(demo->queue, &present);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600603 assert(!err);
604
605 demo->current_buffer = (demo->current_buffer + 1) % DEMO_BUFFER_COUNT;
Chia-I Wucbb564e2015-04-16 22:02:10 +0800606
607 err = vkQueueWaitIdle(demo->queue);
608 assert(err == VK_SUCCESS);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600609}
610
611static void demo_prepare_buffers(struct demo *demo)
612{
Chia-I Wucbb564e2015-04-16 22:02:10 +0800613 const VkSwapChainCreateInfoWSI swap_chain = {
614 .sType = VK_STRUCTURE_TYPE_SWAP_CHAIN_CREATE_INFO_WSI,
615 .pNext = NULL,
616 .pNativeWindowSystemHandle = demo->connection,
617 .pNativeWindowHandle = (void *) (intptr_t) demo->window,
Ian Elliotte4602cd2015-04-21 16:41:02 -0600618 .displayCount = 1,
Chia-I Wucbb564e2015-04-16 22:02:10 +0800619 .imageCount = DEMO_BUFFER_COUNT,
620 .imageFormat = demo->format,
621 .imageExtent = {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600622 .width = demo->width,
623 .height = demo->height,
624 },
Chia-I Wucbb564e2015-04-16 22:02:10 +0800625 .imageArraySize = 1,
626 .imageUsageFlags = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600627 };
Chia-I Wucbb564e2015-04-16 22:02:10 +0800628 VkSwapChainImageInfoWSI images[DEMO_BUFFER_COUNT];
629 size_t images_size = sizeof(images);
Tony Barbourfdc2d352015-04-22 09:02:32 -0600630 VkResult U_ASSERT_ONLY err;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600631 uint32_t i;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600632
Jon Ashburn0b85d052015-05-21 18:13:33 -0600633 err = demo->fpCreateSwapChainWSI(demo->device, &swap_chain, &demo->swap_chain);
Chia-I Wucbb564e2015-04-16 22:02:10 +0800634 assert(!err);
635
Jon Ashburn0b85d052015-05-21 18:13:33 -0600636 err = demo->fpGetSwapChainInfoWSI(demo->swap_chain,
Chia-I Wucbb564e2015-04-16 22:02:10 +0800637 VK_SWAP_CHAIN_INFO_TYPE_PERSISTENT_IMAGES_WSI,
638 &images_size, images);
639 assert(!err && images_size == sizeof(images));
640
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600641 for (i = 0; i < DEMO_BUFFER_COUNT; i++) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600642 VkColorAttachmentViewCreateInfo color_attachment_view = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600643 .sType = VK_STRUCTURE_TYPE_COLOR_ATTACHMENT_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600644 .pNext = NULL,
645 .format = demo->format,
646 .mipLevel = 0,
647 .baseArraySlice = 0,
648 .arraySize = 1,
649 };
650
Chia-I Wucbb564e2015-04-16 22:02:10 +0800651 demo->buffers[i].image = images[i].image;
652 demo->buffers[i].mem = images[i].memory;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600653
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600654 demo_set_image_layout(demo, demo->buffers[i].image,
malnasseeb25d3f2015-06-03 17:28:38 -0400655 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600656 VK_IMAGE_LAYOUT_UNDEFINED,
657 VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600658
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600659 color_attachment_view.image = demo->buffers[i].image;
660
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600661 err = vkCreateColorAttachmentView(demo->device,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600662 &color_attachment_view, &demo->buffers[i].view);
663 assert(!err);
664 }
665}
666
667static void demo_prepare_depth(struct demo *demo)
668{
Tony Barbour72304ef2015-04-16 15:59:00 -0600669 const VkFormat depth_format = VK_FORMAT_D16_UNORM;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600670 const VkImageCreateInfo image = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600671 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600672 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -0600673 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600674 .format = depth_format,
675 .extent = { demo->width, demo->height, 1 },
676 .mipLevels = 1,
677 .arraySize = 1,
678 .samples = 1,
Tony Barbour72304ef2015-04-16 15:59:00 -0600679 .tiling = VK_IMAGE_TILING_OPTIMAL,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600680 .usage = VK_IMAGE_USAGE_DEPTH_STENCIL_BIT,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600681 .flags = 0,
682 };
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -0600683 VkMemoryAllocInfo mem_alloc = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600684 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
Mark Lobodzinskia6907f72015-04-16 08:52:00 -0500685 .pNext = NULL,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600686 .allocationSize = 0,
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600687 .memoryTypeIndex = 0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600688 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600689 VkDepthStencilViewCreateInfo view = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600690 .sType = VK_STRUCTURE_TYPE_DEPTH_STENCIL_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600691 .pNext = NULL,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600692 .image = VK_NULL_HANDLE,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600693 .mipLevel = 0,
694 .baseArraySlice = 0,
695 .arraySize = 1,
696 .flags = 0,
697 };
Mike Stroyanebae8322015-04-17 12:36:38 -0600698
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500699 VkMemoryRequirements mem_reqs;
Tony Barbourfdc2d352015-04-22 09:02:32 -0600700 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600701
702 demo->depth.format = depth_format;
703
704 /* create image */
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600705 err = vkCreateImage(demo->device, &image,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600706 &demo->depth.image);
707 assert(!err);
708
Tony Barbourecf1b2b2015-06-24 16:06:58 -0600709 err = vkGetObjectMemoryRequirements(demo->device,
710 VK_OBJECT_TYPE_IMAGE, demo->depth.image, &mem_reqs);
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600711
712 mem_alloc.allocationSize = mem_reqs.size;
713 err = memory_type_from_properties(demo,
714 mem_reqs.memoryTypeBits,
715 VK_MEMORY_PROPERTY_DEVICE_ONLY,
716 &mem_alloc.memoryTypeIndex);
717 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600718
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500719 /* allocate memory */
720 err = vkAllocMemory(demo->device, &mem_alloc, &demo->depth.mem);
721 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600722
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500723 /* bind memory */
724 err = vkBindObjectMemory(demo->device,
725 VK_OBJECT_TYPE_IMAGE, demo->depth.image,
726 demo->depth.mem, 0);
727 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600728
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600729 demo_set_image_layout(demo, demo->depth.image,
malnasseeb25d3f2015-06-03 17:28:38 -0400730 VK_IMAGE_ASPECT_DEPTH,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600731 VK_IMAGE_LAYOUT_UNDEFINED,
732 VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600733
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600734 /* create image view */
735 view.image = demo->depth.image;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600736 err = vkCreateDepthStencilView(demo->device, &view,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600737 &demo->depth.view);
738 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600739}
740
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600741/** loadTexture
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -0600742 * loads a png file into an memory object, using cstdio , libpng.
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600743 *
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -0600744 * \param demo : Needed to access VK calls
745 * \param filename : the png file to be loaded
746 * \param width : width of png, to be updated as a side effect of this function
747 * \param height : height of png, to be updated as a side effect of this function
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600748 *
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -0600749 * \return bool : an opengl texture id. true if successful?,
750 * should be validated by the client of this function.
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600751 *
752 * Source: http://en.wikibooks.org/wiki/OpenGL_Programming/Intermediate/Textures
753 * Modified to copy image to memory
754 *
755 */
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700756bool loadTexture(const char *filename, uint8_t *rgba_data,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600757 VkSubresourceLayout *layout,
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600758 int32_t *width, int32_t *height)
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600759{
760 //header for testing if it is a png
761 png_byte header[8];
Tony Barbourf9921732015-04-22 11:36:22 -0600762 int is_png, bit_depth, color_type, rowbytes;
763 size_t retval;
Ian Elliott1e42dff2015-02-13 14:29:21 -0700764 png_uint_32 i, twidth, theight;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600765 png_structp png_ptr;
766 png_infop info_ptr, end_info;
767 png_byte *image_data;
768 png_bytep *row_pointers;
769
770 //open file as binary
771 FILE *fp = fopen(filename, "rb");
772 if (!fp) {
773 return false;
774 }
775
776 //read the header
Tony Barbourfdc2d352015-04-22 09:02:32 -0600777 retval = fread(header, 1, 8, fp);
778 if (retval != 8) {
779 fclose(fp);
780 return false;
781 }
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600782
783 //test if png
784 is_png = !png_sig_cmp(header, 0, 8);
785 if (!is_png) {
786 fclose(fp);
787 return false;
788 }
789
790 //create png struct
791 png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL,
792 NULL, NULL);
793 if (!png_ptr) {
794 fclose(fp);
795 return (false);
796 }
797
798 //create png info struct
799 info_ptr = png_create_info_struct(png_ptr);
800 if (!info_ptr) {
801 png_destroy_read_struct(&png_ptr, (png_infopp) NULL, (png_infopp) NULL);
802 fclose(fp);
803 return (false);
804 }
805
806 //create png info struct
807 end_info = png_create_info_struct(png_ptr);
808 if (!end_info) {
809 png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp) NULL);
810 fclose(fp);
811 return (false);
812 }
813
814 //png error stuff, not sure libpng man suggests this.
815 if (setjmp(png_jmpbuf(png_ptr))) {
816 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
817 fclose(fp);
818 return (false);
819 }
820
821 //init png reading
822 png_init_io(png_ptr, fp);
823
824 //let libpng know you already read the first 8 bytes
825 png_set_sig_bytes(png_ptr, 8);
826
827 // read all the info up to the image data
828 png_read_info(png_ptr, info_ptr);
829
830 // get info about png
831 png_get_IHDR(png_ptr, info_ptr, &twidth, &theight, &bit_depth, &color_type,
832 NULL, NULL, NULL);
833
834 //update width and height based on png info
835 *width = twidth;
836 *height = theight;
837
838 // Require that incoming texture be 8bits per color component
839 // and 4 components (RGBA).
840 if (png_get_bit_depth(png_ptr, info_ptr) != 8 ||
841 png_get_channels(png_ptr, info_ptr) != 4) {
842 return false;
843 }
844
845 if (rgba_data == NULL) {
846 // If data pointer is null, we just want the width & height
847 // clean up memory and close stuff
848 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
849 fclose(fp);
850
851 return true;
852 }
853
854 // Update the png info struct.
855 png_read_update_info(png_ptr, info_ptr);
856
857 // Row size in bytes.
858 rowbytes = png_get_rowbytes(png_ptr, info_ptr);
859
860 // Allocate the image_data as a big block, to be given to opengl
861 image_data = (png_byte *)malloc(rowbytes * theight * sizeof(png_byte));
862 if (!image_data) {
863 //clean up memory and close stuff
864 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
865 fclose(fp);
866 return false;
867 }
868
869 // row_pointers is for pointing to image_data for reading the png with libpng
870 row_pointers = (png_bytep *)malloc(theight * sizeof(png_bytep));
871 if (!row_pointers) {
872 //clean up memory and close stuff
873 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
874 // delete[] image_data;
875 fclose(fp);
876 return false;
877 }
878 // set the individual row_pointers to point at the correct offsets of image_data
879 for (i = 0; i < theight; ++i)
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700880 row_pointers[theight - 1 - i] = rgba_data + i * layout->rowPitch;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600881
882 // read the png into image_data through row_pointers
883 png_read_image(png_ptr, row_pointers);
884
885 // clean up memory and close stuff
886 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
887 free(row_pointers);
888 free(image_data);
889 fclose(fp);
890
891 return true;
892}
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600893
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700894static void demo_prepare_texture_image(struct demo *demo,
895 const char *filename,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600896 struct texture_object *tex_obj,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600897 VkImageTiling tiling,
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -0600898 VkImageUsageFlags usage,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600899 VkFlags mem_props)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600900{
Mike Stroyan8b89e072015-06-15 14:21:03 -0600901 const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600902 int32_t tex_width;
903 int32_t tex_height;
Tony Barbourfdc2d352015-04-22 09:02:32 -0600904 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700905
David Pinedo30bd71d2015-04-23 08:16:57 -0600906 if (!loadTexture(filename, NULL, NULL, &tex_width, &tex_height))
907 {
908 printf("Failed to load textures\n");
909 fflush(stdout);
910 exit(1);
911 }
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700912
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600913 tex_obj->tex_width = tex_width;
914 tex_obj->tex_height = tex_height;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700915
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600916 const VkImageCreateInfo image_create_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600917 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700918 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -0600919 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700920 .format = tex_format,
921 .extent = { tex_width, tex_height, 1 },
922 .mipLevels = 1,
923 .arraySize = 1,
924 .samples = 1,
925 .tiling = tiling,
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -0600926 .usage = usage,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700927 .flags = 0,
928 };
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -0600929 VkMemoryAllocInfo mem_alloc = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600930 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
Mark Lobodzinskia6907f72015-04-16 08:52:00 -0500931 .pNext = NULL,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700932 .allocationSize = 0,
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600933 .memoryTypeIndex = 0,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700934 };
935
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500936 VkMemoryRequirements mem_reqs;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700937
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600938 err = vkCreateImage(demo->device, &image_create_info,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600939 &tex_obj->image);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700940 assert(!err);
941
Tony Barbourecf1b2b2015-06-24 16:06:58 -0600942 err = vkGetObjectMemoryRequirements(demo->device,
943 VK_OBJECT_TYPE_IMAGE, tex_obj->image, &mem_reqs);
944 assert(!err);
Piers Daniell735ee532015-02-23 16:23:13 -0700945
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500946 mem_alloc.allocationSize = mem_reqs.size;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700947
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600948 err = memory_type_from_properties(demo, mem_reqs.memoryTypeBits, mem_props, &mem_alloc.memoryTypeIndex);
949 assert(!err);
950
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500951 /* allocate memory */
952 err = vkAllocMemory(demo->device, &mem_alloc,
953 &(tex_obj->mem));
954 assert(!err);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700955
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500956 /* bind memory */
957 err = vkBindObjectMemory(demo->device,
958 VK_OBJECT_TYPE_IMAGE, tex_obj->image,
959 tex_obj->mem, 0);
960 assert(!err);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700961
Tony Barbour72304ef2015-04-16 15:59:00 -0600962 if (mem_props & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600963 const VkImageSubresource subres = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600964 .aspect = VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700965 .mipLevel = 0,
966 .arraySlice = 0,
967 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600968 VkSubresourceLayout layout;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700969 void *data;
970
Tony Barbourecf1b2b2015-06-24 16:06:58 -0600971 err = vkGetImageSubresourceLayout(demo->device, tex_obj->image, &subres, &layout);
972 assert(!err);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700973
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500974 err = vkMapMemory(demo->device, tex_obj->mem, 0, 0, 0, &data);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700975 assert(!err);
976
977 if (!loadTexture(filename, data, &layout, &tex_width, &tex_height)) {
978 fprintf(stderr, "Error loading texture: %s\n", filename);
979 }
980
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500981 err = vkUnmapMemory(demo->device, tex_obj->mem);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700982 assert(!err);
983 }
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600984
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600985 tex_obj->imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600986 demo_set_image_layout(demo, tex_obj->image,
malnasseeb25d3f2015-06-03 17:28:38 -0400987 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600988 VK_IMAGE_LAYOUT_UNDEFINED,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600989 tex_obj->imageLayout);
990 /* 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 -0700991}
992
Mark Lobodzinskida9b1092015-04-16 11:44:05 -0500993static void demo_destroy_texture_image(struct demo *demo, struct texture_object *tex_objs)
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700994{
995 /* clean up staging resources */
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500996 vkFreeMemory(demo->device, tex_objs->mem);
Mike Stroyanebae8322015-04-17 12:36:38 -0600997 vkDestroyObject(demo->device, VK_OBJECT_TYPE_IMAGE, tex_objs->image);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700998}
999
1000static void demo_prepare_textures(struct demo *demo)
1001{
Tony Barbour72304ef2015-04-16 15:59:00 -06001002 const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001003 VkFormatProperties props;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001004 VkResult U_ASSERT_ONLY err;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001005 uint32_t i;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001006
Chris Forbesf576a3e2015-06-21 22:55:02 +12001007 err = vkGetPhysicalDeviceFormatInfo(demo->gpu, tex_format, &props);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001008 assert(!err);
1009
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001010 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001011
FslNopper6a7e50e2015-05-06 21:42:01 +02001012 if ((props.linearTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) && !demo->use_staging_buffer) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001013 /* Device can texture using linear textures */
1014 demo_prepare_texture_image(demo, tex_files[i], &demo->textures[i],
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001015 VK_IMAGE_TILING_LINEAR, VK_IMAGE_USAGE_SAMPLED_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
Tony Barbour72304ef2015-04-16 15:59:00 -06001016 } else if (props.optimalTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001017 /* Must use staging buffer to copy linear texture to optimized */
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001018 struct texture_object staging_texture;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001019
1020 memset(&staging_texture, 0, sizeof(staging_texture));
1021 demo_prepare_texture_image(demo, tex_files[i], &staging_texture,
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001022 VK_IMAGE_TILING_LINEAR, VK_IMAGE_USAGE_TRANSFER_SOURCE_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001023
1024 demo_prepare_texture_image(demo, tex_files[i], &demo->textures[i],
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001025 VK_IMAGE_TILING_OPTIMAL,
1026 (VK_IMAGE_USAGE_TRANSFER_DESTINATION_BIT | VK_IMAGE_USAGE_SAMPLED_BIT),
1027 VK_MEMORY_PROPERTY_DEVICE_ONLY);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001028
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001029 demo_set_image_layout(demo, staging_texture.image,
malnasseeb25d3f2015-06-03 17:28:38 -04001030 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001031 staging_texture.imageLayout,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001032 VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001033
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001034 demo_set_image_layout(demo, demo->textures[i].image,
malnasseeb25d3f2015-06-03 17:28:38 -04001035 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001036 demo->textures[i].imageLayout,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001037 VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001038
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001039 VkImageCopy copy_region = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001040 .srcSubresource = { VK_IMAGE_ASPECT_COLOR, 0, 0 },
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001041 .srcOffset = { 0, 0, 0 },
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001042 .destSubresource = { VK_IMAGE_ASPECT_COLOR, 0, 0 },
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001043 .destOffset = { 0, 0, 0 },
1044 .extent = { staging_texture.tex_width, staging_texture.tex_height, 1 },
1045 };
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001046 vkCmdCopyImage(demo->cmd,
1047 staging_texture.image, VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL,
1048 demo->textures[i].image, VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL,
Courtney Goeltzenleuchter8e89a312015-03-25 11:25:10 -06001049 1, &copy_region);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001050
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001051 demo_set_image_layout(demo, demo->textures[i].image,
malnasseeb25d3f2015-06-03 17:28:38 -04001052 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001053 VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001054 demo->textures[i].imageLayout);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001055
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001056 demo_flush_init_cmd(demo);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001057
Courtney Goeltzenleuchterf3aeb2b2015-04-21 09:30:03 -06001058 demo_destroy_texture_image(demo, &staging_texture);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001059 } else {
Mike Stroyan8b89e072015-06-15 14:21:03 -06001060 /* Can't support VK_FORMAT_R8G8B8A8_UNORM !? */
1061 assert(!"No support for R8G8B8A8_UNORM as texture image format");
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001062 }
1063
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001064 const VkSamplerCreateInfo sampler = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001065 .sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001066 .pNext = NULL,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001067 .magFilter = VK_TEX_FILTER_NEAREST,
1068 .minFilter = VK_TEX_FILTER_NEAREST,
Tony Barbour72304ef2015-04-16 15:59:00 -06001069 .mipMode = VK_TEX_MIPMAP_MODE_BASE,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001070 .addressU = VK_TEX_ADDRESS_CLAMP,
1071 .addressV = VK_TEX_ADDRESS_CLAMP,
1072 .addressW = VK_TEX_ADDRESS_CLAMP,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001073 .mipLodBias = 0.0f,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001074 .maxAnisotropy = 1,
Tony Barbour72304ef2015-04-16 15:59:00 -06001075 .compareOp = VK_COMPARE_OP_NEVER,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001076 .minLod = 0.0f,
1077 .maxLod = 0.0f,
Tony Barbourcb530c72015-06-25 16:56:44 -06001078 .borderColor = VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001079 };
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001080
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001081 VkImageViewCreateInfo view = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001082 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001083 .pNext = NULL,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001084 .image = VK_NULL_HANDLE,
Tony Barbour72304ef2015-04-16 15:59:00 -06001085 .viewType = VK_IMAGE_VIEW_TYPE_2D,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001086 .format = tex_format,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001087 .channels = { VK_CHANNEL_SWIZZLE_R,
1088 VK_CHANNEL_SWIZZLE_G,
1089 VK_CHANNEL_SWIZZLE_B,
1090 VK_CHANNEL_SWIZZLE_A, },
1091 .subresourceRange = { VK_IMAGE_ASPECT_COLOR, 0, 1, 0, 1 },
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001092 };
Jon Ashburnb2a66652015-01-16 09:37:43 -07001093
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001094 /* create sampler */
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001095 err = vkCreateSampler(demo->device, &sampler,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001096 &demo->textures[i].sampler);
1097 assert(!err);
1098
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001099 /* create image view */
1100 view.image = demo->textures[i].image;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001101 err = vkCreateImageView(demo->device, &view,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001102 &demo->textures[i].view);
1103 assert(!err);
1104 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001105}
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001106
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001107void demo_prepare_cube_data_buffer(struct demo *demo)
1108{
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -06001109 VkBufferCreateInfo buf_info;
1110 VkBufferViewCreateInfo view_info;
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -06001111 VkMemoryAllocInfo alloc_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001112 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
Mark Lobodzinskia6907f72015-04-16 08:52:00 -05001113 .pNext = NULL,
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001114 .allocationSize = 0,
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06001115 .memoryTypeIndex = 0,
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001116 };
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001117 VkMemoryRequirements mem_reqs;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001118 size_t mem_reqs_size = sizeof(VkMemoryRequirements);
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001119 uint8_t *pData;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001120 int i;
1121 mat4x4 MVP, VP;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001122 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001123 struct vktexcube_vs_uniform data;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001124
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001125 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001126 mat4x4_mul(MVP, VP, demo->model_matrix);
1127 memcpy(data.mvp, MVP, sizeof(MVP));
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001128// dumpMatrix("MVP", MVP);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001129
1130 for (i=0; i<12*3; i++) {
1131 data.position[i][0] = g_vertex_buffer_data[i*3];
1132 data.position[i][1] = g_vertex_buffer_data[i*3+1];
1133 data.position[i][2] = g_vertex_buffer_data[i*3+2];
1134 data.position[i][3] = 1.0f;
1135 data.attr[i][0] = g_uv_buffer_data[2*i];
1136 data.attr[i][1] = g_uv_buffer_data[2*i + 1];
1137 data.attr[i][2] = 0;
1138 data.attr[i][3] = 0;
1139 }
1140
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001141 memset(&buf_info, 0, sizeof(buf_info));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001142 buf_info.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -06001143 buf_info.usage = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001144 err = vkCreateBuffer(demo->device, &buf_info, &demo->uniform_data.buf);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001145 assert(!err);
1146
Tony Barbourecf1b2b2015-06-24 16:06:58 -06001147 err = vkGetObjectMemoryRequirements(demo->device,
1148 VK_OBJECT_TYPE_BUFFER, demo->uniform_data.buf, &mem_reqs);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001149 assert(!err && mem_reqs_size == sizeof(mem_reqs));
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001150
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001151 alloc_info.allocationSize = mem_reqs.size;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06001152 err = memory_type_from_properties(demo,
1153 mem_reqs.memoryTypeBits,
1154 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT,
1155 &alloc_info.memoryTypeIndex);
1156 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001157
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001158 err = vkAllocMemory(demo->device, &alloc_info, &(demo->uniform_data.mem));
1159 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001160
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001161 err = vkMapMemory(demo->device, demo->uniform_data.mem, 0, 0, 0, (void **) &pData);
1162 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001163
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001164 memcpy(pData, &data, sizeof data);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001165
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001166 err = vkUnmapMemory(demo->device, demo->uniform_data.mem);
1167 assert(!err);
1168
1169 err = vkBindObjectMemory(demo->device,
1170 VK_OBJECT_TYPE_BUFFER, demo->uniform_data.buf,
1171 demo->uniform_data.mem, 0);
1172 assert(!err);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001173
1174 memset(&view_info, 0, sizeof(view_info));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001175 view_info.sType = VK_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO;
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001176 view_info.buffer = demo->uniform_data.buf;
Tony Barbour72304ef2015-04-16 15:59:00 -06001177 view_info.viewType = VK_BUFFER_VIEW_TYPE_RAW;
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001178 view_info.offset = 0;
1179 view_info.range = sizeof(data);
1180
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001181 err = vkCreateBufferView(demo->device, &view_info, &demo->uniform_data.view);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001182 assert(!err);
1183
Chia-I Wuae721ba2015-05-25 16:27:55 +08001184 demo->uniform_data.desc.bufferView = demo->uniform_data.view;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001185}
1186
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001187static void demo_prepare_descriptor_layout(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001188{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001189 const VkDescriptorSetLayoutBinding layout_bindings[2] = {
Chia-I Wua2aa8632015-03-26 15:04:41 +08001190 [0] = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001191 .descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
Chia-I Wu6d29a412015-05-25 16:22:52 +08001192 .arraySize = 1,
Tony Barbour72304ef2015-04-16 15:59:00 -06001193 .stageFlags = VK_SHADER_STAGE_VERTEX_BIT,
Chia-I Wu91e8e212015-03-27 12:56:09 +08001194 .pImmutableSamplers = NULL,
Chia-I Wua2aa8632015-03-26 15:04:41 +08001195 },
1196 [1] = {
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -06001197 .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
Chia-I Wu6d29a412015-05-25 16:22:52 +08001198 .arraySize = DEMO_TEXTURE_COUNT,
Tony Barbour72304ef2015-04-16 15:59:00 -06001199 .stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT,
Chia-I Wu91e8e212015-03-27 12:56:09 +08001200 .pImmutableSamplers = NULL,
Chia-I Wua2aa8632015-03-26 15:04:41 +08001201 },
1202 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001203 const VkDescriptorSetLayoutCreateInfo descriptor_layout = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001204 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001205 .pNext = NULL,
Chia-I Wua2aa8632015-03-26 15:04:41 +08001206 .count = 2,
1207 .pBinding = layout_bindings,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001208 };
Tony Barbourfdc2d352015-04-22 09:02:32 -06001209 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001210
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001211 err = vkCreateDescriptorSetLayout(demo->device,
Chia-I Wub58c24a2015-03-26 15:27:55 +08001212 &descriptor_layout, &demo->desc_layout);
1213 assert(!err);
1214
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001215 const VkPipelineLayoutCreateInfo pPipelineLayoutCreateInfo = {
1216 .sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
1217 .pNext = NULL,
1218 .descriptorSetCount = 1,
1219 .pSetLayouts = &demo->desc_layout,
1220 };
1221
1222 err = vkCreatePipelineLayout(demo->device,
1223 &pPipelineLayoutCreateInfo,
1224 &demo->pipeline_layout);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001225 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001226}
1227
Tobin Ehlisd1f17ac2015-07-02 11:02:49 -06001228static VkShader demo_prepare_shader(struct demo* demo,
Tony Barbour72304ef2015-04-16 15:59:00 -06001229 VkShaderStage stage,
Tobin Ehlisd1f17ac2015-07-02 11:02:49 -06001230 const void* code,
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001231 size_t size)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001232{
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001233 VkShaderModuleCreateInfo moduleCreateInfo;
1234 VkShaderCreateInfo shaderCreateInfo;
1235 VkShaderModule shaderModule;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001236 VkShader shader;
1237 VkResult err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001238
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001239
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001240 moduleCreateInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
1241 moduleCreateInfo.pNext = NULL;
1242
1243 shaderCreateInfo.sType = VK_STRUCTURE_TYPE_SHADER_CREATE_INFO;
1244 shaderCreateInfo.pNext = NULL;
Tobin Ehlisd1f17ac2015-07-02 11:02:49 -06001245 shaderCreateInfo.pName = "main";
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001246
Cody Northrop1fedb212015-05-28 11:27:16 -06001247 if (!demo->use_glsl) {
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001248 moduleCreateInfo.codeSize = size;
1249 moduleCreateInfo.pCode = code;
1250 moduleCreateInfo.flags = 0;
1251 err = vkCreateShaderModule(demo->device, &moduleCreateInfo, &shaderModule);
Cody Northrop1fedb212015-05-28 11:27:16 -06001252 if (err) {
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001253 free((void *) moduleCreateInfo.pCode);
Cody Northrop1fedb212015-05-28 11:27:16 -06001254 }
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001255
1256 shaderCreateInfo.flags = 0;
1257 shaderCreateInfo.module = shaderModule;
1258 err = vkCreateShader(demo->device, &shaderCreateInfo, &shader);
Cody Northrop1fedb212015-05-28 11:27:16 -06001259 } else {
1260 // Create fake SPV structure to feed GLSL
1261 // to the driver "under the covers"
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001262 moduleCreateInfo.codeSize = 3 * sizeof(uint32_t) + size + 1;
1263 moduleCreateInfo.pCode = malloc(moduleCreateInfo.codeSize);
1264 moduleCreateInfo.flags = 0;
Cody Northrop1fedb212015-05-28 11:27:16 -06001265
1266 /* try version 0 first: VkShaderStage followed by GLSL */
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001267 ((uint32_t *) moduleCreateInfo.pCode)[0] = ICD_SPV_MAGIC;
1268 ((uint32_t *) moduleCreateInfo.pCode)[1] = 0;
1269 ((uint32_t *) moduleCreateInfo.pCode)[2] = stage;
1270 memcpy(((uint32_t *) moduleCreateInfo.pCode + 3), code, size + 1);
Cody Northrop1fedb212015-05-28 11:27:16 -06001271
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001272 err = vkCreateShaderModule(demo->device, &moduleCreateInfo, &shaderModule);
Cody Northrop1fedb212015-05-28 11:27:16 -06001273 if (err) {
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001274 free((void *) moduleCreateInfo.pCode);
Cody Northrop1fedb212015-05-28 11:27:16 -06001275 }
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001276
1277 shaderCreateInfo.flags = 0;
1278 shaderCreateInfo.module = shaderModule;
1279 err = vkCreateShader(demo->device, &shaderCreateInfo, &shader);
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001280 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001281 return shader;
1282}
1283
Cody Northrop8b12a1f2015-03-17 15:55:58 -06001284char *demo_read_spv(const char *filename, size_t *psize)
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001285{
1286 long int size;
Tony Barbourf9921732015-04-22 11:36:22 -06001287 size_t U_ASSERT_ONLY retval;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001288 void *shader_code;
1289
1290 FILE *fp = fopen(filename, "rb");
1291 if (!fp) return NULL;
1292
1293 fseek(fp, 0L, SEEK_END);
1294 size = ftell(fp);
1295
1296 fseek(fp, 0L, SEEK_SET);
1297
1298 shader_code = malloc(size);
Tony Barbourfdc2d352015-04-22 09:02:32 -06001299 retval = fread(shader_code, size, 1, fp);
1300 assert(retval == 1);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001301
1302 *psize = size;
1303
1304 return shader_code;
1305}
1306
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001307static VkShader demo_prepare_vs(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001308{
Cody Northrop1fedb212015-05-28 11:27:16 -06001309 if (!demo->use_glsl) {
1310 void *vertShaderCode;
1311 size_t size;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001312
Cody Northrop1fedb212015-05-28 11:27:16 -06001313 vertShaderCode = demo_read_spv("cube-vert.spv", &size);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001314
Cody Northrop1fedb212015-05-28 11:27:16 -06001315 return demo_prepare_shader(demo, VK_SHADER_STAGE_VERTEX,
1316 vertShaderCode, size);
1317 } else {
1318 static const char *vertShaderText =
1319 "#version 140\n"
1320 "#extension GL_ARB_separate_shader_objects : enable\n"
1321 "#extension GL_ARB_shading_language_420pack : enable\n"
1322 "\n"
1323 "layout(binding = 0) uniform buf {\n"
1324 " mat4 MVP;\n"
1325 " vec4 position[12*3];\n"
1326 " vec4 attr[12*3];\n"
1327 "} ubuf;\n"
1328 "\n"
1329 "layout (location = 0) out vec4 texcoord;\n"
1330 "\n"
1331 "void main() \n"
1332 "{\n"
1333 " texcoord = ubuf.attr[gl_VertexID];\n"
1334 " gl_Position = ubuf.MVP * ubuf.position[gl_VertexID];\n"
1335 "\n"
1336 " // GL->VK conventions\n"
1337 " gl_Position.y = -gl_Position.y;\n"
1338 " gl_Position.z = (gl_Position.z + gl_Position.w) / 2.0;\n"
1339 "}\n";
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001340
Cody Northrop1fedb212015-05-28 11:27:16 -06001341 return demo_prepare_shader(demo, VK_SHADER_STAGE_VERTEX,
1342 (const void *) vertShaderText,
1343 strlen(vertShaderText));
1344 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001345}
1346
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001347static VkShader demo_prepare_fs(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001348{
Cody Northrop1fedb212015-05-28 11:27:16 -06001349 if (!demo->use_glsl) {
1350 void *fragShaderCode;
1351 size_t size;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001352
Cody Northrop1fedb212015-05-28 11:27:16 -06001353 fragShaderCode = demo_read_spv("cube-frag.spv", &size);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001354
Cody Northrop1fedb212015-05-28 11:27:16 -06001355 return demo_prepare_shader(demo, VK_SHADER_STAGE_FRAGMENT,
1356 fragShaderCode, size);
1357 } else {
1358 static const char *fragShaderText =
1359 "#version 140\n"
1360 "#extension GL_ARB_separate_shader_objects : enable\n"
1361 "#extension GL_ARB_shading_language_420pack : enable\n"
1362 "layout (binding = 1) uniform sampler2D tex;\n"
1363 "\n"
1364 "layout (location = 0) in vec4 texcoord;\n"
1365 "layout (location = 0) out vec4 uFragColor;\n"
1366 "void main() {\n"
1367 " uFragColor = texture(tex, texcoord.xy);\n"
1368 "}\n";
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001369
Cody Northrop1fedb212015-05-28 11:27:16 -06001370 return demo_prepare_shader(demo, VK_SHADER_STAGE_FRAGMENT,
1371 (const void *) fragShaderText,
1372 strlen(fragShaderText));
1373 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001374}
1375
1376static void demo_prepare_pipeline(struct demo *demo)
1377{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001378 VkGraphicsPipelineCreateInfo pipeline;
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001379 VkPipelineCacheCreateInfo pipelineCache;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001380 VkPipelineIaStateCreateInfo ia;
1381 VkPipelineRsStateCreateInfo rs;
1382 VkPipelineCbStateCreateInfo cb;
1383 VkPipelineDsStateCreateInfo ds;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001384 VkPipelineVpStateCreateInfo vp;
1385 VkPipelineMsStateCreateInfo ms;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001386 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001387
1388 memset(&pipeline, 0, sizeof(pipeline));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001389 pipeline.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001390 pipeline.layout = demo->pipeline_layout;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001391
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001392 memset(&ia, 0, sizeof(ia));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001393 ia.sType = VK_STRUCTURE_TYPE_PIPELINE_IA_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06001394 ia.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001395
1396 memset(&rs, 0, sizeof(rs));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001397 rs.sType = VK_STRUCTURE_TYPE_PIPELINE_RS_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06001398 rs.fillMode = VK_FILL_MODE_SOLID;
1399 rs.cullMode = VK_CULL_MODE_BACK;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001400 rs.frontFace = VK_FRONT_FACE_CCW;
Chia-I Wubb67e6e2015-04-22 14:20:52 +08001401 rs.depthClipEnable = VK_TRUE;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001402
1403 memset(&cb, 0, sizeof(cb));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001404 cb.sType = VK_STRUCTURE_TYPE_PIPELINE_CB_STATE_CREATE_INFO;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001405 VkPipelineCbAttachmentState att_state[1];
Tony Barbour29645d02015-01-16 14:27:35 -07001406 memset(att_state, 0, sizeof(att_state));
1407 att_state[0].format = demo->format;
1408 att_state[0].channelWriteMask = 0xf;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001409 att_state[0].blendEnable = VK_FALSE;
Tony Barbour29645d02015-01-16 14:27:35 -07001410 cb.attachmentCount = 1;
1411 cb.pAttachments = att_state;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001412
Tony Barbour29645d02015-01-16 14:27:35 -07001413 memset(&vp, 0, sizeof(vp));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001414 vp.sType = VK_STRUCTURE_TYPE_PIPELINE_VP_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06001415 vp.viewportCount = 1;
Tony Barbour29645d02015-01-16 14:27:35 -07001416
1417 memset(&ds, 0, sizeof(ds));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001418 ds.sType = VK_STRUCTURE_TYPE_PIPELINE_DS_STATE_CREATE_INFO;
Tony Barbour29645d02015-01-16 14:27:35 -07001419 ds.format = demo->depth.format;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001420 ds.depthTestEnable = VK_TRUE;
1421 ds.depthWriteEnable = VK_TRUE;
Tony Barbour72304ef2015-04-16 15:59:00 -06001422 ds.depthCompareOp = VK_COMPARE_OP_LESS_EQUAL;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001423 ds.depthBoundsEnable = VK_FALSE;
1424 ds.back.stencilFailOp = VK_STENCIL_OP_KEEP;
1425 ds.back.stencilPassOp = VK_STENCIL_OP_KEEP;
Tony Barbour72304ef2015-04-16 15:59:00 -06001426 ds.back.stencilCompareOp = VK_COMPARE_OP_ALWAYS;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001427 ds.stencilTestEnable = VK_FALSE;
Tony Barbour29645d02015-01-16 14:27:35 -07001428 ds.front = ds.back;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001429
Tony Barbour29645d02015-01-16 14:27:35 -07001430 memset(&ms, 0, sizeof(ms));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001431 ms.sType = VK_STRUCTURE_TYPE_PIPELINE_MS_STATE_CREATE_INFO;
Tony Barbour29645d02015-01-16 14:27:35 -07001432 ms.sampleMask = 1;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001433 ms.multisampleEnable = VK_FALSE;
Tony Barbour3ca22502015-06-26 10:18:34 -06001434 ms.rasterSamples = 1;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001435
Mark Lobodzinski521d7762015-06-23 15:11:57 -06001436 // Two stages: vs and fs
1437 pipeline.stageCount = 2;
1438 VkPipelineShaderStageCreateInfo shaderStages[2];
1439 memset(&shaderStages, 0, 2 * sizeof(VkPipelineShaderStageCreateInfo));
1440
1441 shaderStages[0].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1442 shaderStages[0].stage = VK_SHADER_STAGE_VERTEX;
1443 shaderStages[0].shader = demo_prepare_vs(demo);
1444
1445 shaderStages[1].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1446 shaderStages[1].stage = VK_SHADER_STAGE_FRAGMENT;
1447 shaderStages[1].shader = demo_prepare_fs(demo);
1448
Mark Lobodzinski15169cf2015-06-30 10:18:36 -06001449 pipeline.pVertexInputState = NULL;
Mark Lobodzinski521d7762015-06-23 15:11:57 -06001450 pipeline.pIaState = &ia;
1451 pipeline.pRsState = &rs;
1452 pipeline.pCbState = &cb;
1453 pipeline.pMsState = &ms;
1454 pipeline.pVpState = &vp;
1455 pipeline.pDsState = &ds;
1456 pipeline.pStages = shaderStages;
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001457 memset(&pipelineCache, 0, sizeof(pipelineCache));
1458 pipelineCache.sType = VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001459
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001460 err = vkCreatePipelineCache(demo->device, &pipelineCache, &demo->pipelineCache);
1461 assert(!err);
1462 err = vkCreateGraphicsPipelines(demo->device, demo->pipelineCache, 1, &pipeline, &demo->pipeline);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001463 assert(!err);
1464
Mark Lobodzinski521d7762015-06-23 15:11:57 -06001465 for (uint32_t i = 0; i < pipeline.stageCount; i++) {
1466 vkDestroyObject(demo->device, VK_OBJECT_TYPE_SHADER, shaderStages[i].shader);
1467 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001468}
1469
1470static void demo_prepare_dynamic_states(struct demo *demo)
1471{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001472 VkDynamicVpStateCreateInfo viewport_create;
1473 VkDynamicRsStateCreateInfo raster;
1474 VkDynamicCbStateCreateInfo color_blend;
1475 VkDynamicDsStateCreateInfo depth_stencil;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001476 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001477
Tony Barbour29645d02015-01-16 14:27:35 -07001478 memset(&viewport_create, 0, sizeof(viewport_create));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001479 viewport_create.sType = VK_STRUCTURE_TYPE_DYNAMIC_VP_STATE_CREATE_INFO;
Courtney Goeltzenleuchter1710d8d2015-02-11 14:13:34 -07001480 viewport_create.viewportAndScissorCount = 1;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001481 VkViewport viewport;
Piers Daniell735ee532015-02-23 16:23:13 -07001482 memset(&viewport, 0, sizeof(viewport));
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001483 viewport.height = (float) demo->height;
1484 viewport.width = (float) demo->width;
1485 viewport.minDepth = (float) 0.0f;
1486 viewport.maxDepth = (float) 1.0f;
Piers Daniell735ee532015-02-23 16:23:13 -07001487 viewport_create.pViewports = &viewport;
Chris Forbesfaa91732015-06-22 17:21:59 +12001488 VkRect2D scissor;
Piers Daniell735ee532015-02-23 16:23:13 -07001489 memset(&scissor, 0, sizeof(scissor));
Courtney Goeltzenleuchter1710d8d2015-02-11 14:13:34 -07001490 scissor.extent.width = demo->width;
1491 scissor.extent.height = demo->height;
1492 scissor.offset.x = 0;
1493 scissor.offset.y = 0;
Courtney Goeltzenleuchter1710d8d2015-02-11 14:13:34 -07001494 viewport_create.pScissors = &scissor;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001495
1496 memset(&raster, 0, sizeof(raster));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001497 raster.sType = VK_STRUCTURE_TYPE_DYNAMIC_RS_STATE_CREATE_INFO;
Piers Daniell735ee532015-02-23 16:23:13 -07001498 raster.lineWidth = 1.0;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001499
1500 memset(&color_blend, 0, sizeof(color_blend));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001501 color_blend.sType = VK_STRUCTURE_TYPE_DYNAMIC_CB_STATE_CREATE_INFO;
Piers Daniell735ee532015-02-23 16:23:13 -07001502 color_blend.blendConst[0] = 1.0f;
1503 color_blend.blendConst[1] = 1.0f;
1504 color_blend.blendConst[2] = 1.0f;
1505 color_blend.blendConst[3] = 1.0f;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001506
1507 memset(&depth_stencil, 0, sizeof(depth_stencil));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001508 depth_stencil.sType = VK_STRUCTURE_TYPE_DYNAMIC_DS_STATE_CREATE_INFO;
Mark Lobodzinski89d32b12015-06-12 11:14:17 -06001509 depth_stencil.minDepthBounds = 0.0f;
1510 depth_stencil.maxDepthBounds = 1.0f;
Tony Barbour29645d02015-01-16 14:27:35 -07001511 depth_stencil.stencilBackRef = 0;
1512 depth_stencil.stencilFrontRef = 0;
1513 depth_stencil.stencilReadMask = 0xff;
1514 depth_stencil.stencilWriteMask = 0xff;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001515
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001516 err = vkCreateDynamicViewportState(demo->device, &viewport_create, &demo->viewport);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001517 assert(!err);
1518
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001519 err = vkCreateDynamicRasterState(demo->device, &raster, &demo->raster);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001520 assert(!err);
1521
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001522 err = vkCreateDynamicColorBlendState(demo->device,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001523 &color_blend, &demo->color_blend);
1524 assert(!err);
1525
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001526 err = vkCreateDynamicDepthStencilState(demo->device,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001527 &depth_stencil, &demo->depth_stencil);
1528 assert(!err);
1529}
1530
Chia-I Wu63ea9262015-03-26 13:14:16 +08001531static void demo_prepare_descriptor_pool(struct demo *demo)
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001532{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001533 const VkDescriptorTypeCount type_counts[2] = {
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001534 [0] = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001535 .type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001536 .count = 1,
1537 },
1538 [1] = {
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -06001539 .type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001540 .count = DEMO_TEXTURE_COUNT,
1541 },
1542 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001543 const VkDescriptorPoolCreateInfo descriptor_pool = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001544 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001545 .pNext = NULL,
1546 .count = 2,
1547 .pTypeCount = type_counts,
1548 };
Tony Barbourfdc2d352015-04-22 09:02:32 -06001549 VkResult U_ASSERT_ONLY err;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001550
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001551 err = vkCreateDescriptorPool(demo->device,
1552 VK_DESCRIPTOR_POOL_USAGE_ONE_SHOT, 1,
Chia-I Wu63ea9262015-03-26 13:14:16 +08001553 &descriptor_pool, &demo->desc_pool);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001554 assert(!err);
1555}
1556
1557static void demo_prepare_descriptor_set(struct demo *demo)
1558{
Chia-I Wuae721ba2015-05-25 16:27:55 +08001559 VkDescriptorInfo tex_descs[DEMO_TEXTURE_COUNT];
1560 VkWriteDescriptorSet writes[2];
Tony Barbourfdc2d352015-04-22 09:02:32 -06001561 VkResult U_ASSERT_ONLY err;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001562 uint32_t count;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001563 uint32_t i;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001564
Mike Stroyanebae8322015-04-17 12:36:38 -06001565 err = vkAllocDescriptorSets(demo->device, demo->desc_pool,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001566 VK_DESCRIPTOR_SET_USAGE_STATIC,
Chia-I Wu87544e72015-02-23 10:41:08 -07001567 1, &demo->desc_layout,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001568 &demo->desc_set, &count);
1569 assert(!err && count == 1);
1570
Chia-I Wuae721ba2015-05-25 16:27:55 +08001571 memset(&tex_descs, 0, sizeof(tex_descs));
1572 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
1573 tex_descs[i].sampler = demo->textures[i].sampler;
1574 tex_descs[i].imageView = demo->textures[i].view;
1575 tex_descs[i].imageLayout = VK_IMAGE_LAYOUT_GENERAL;
1576 }
1577
1578 memset(&writes, 0, sizeof(writes));
1579
1580 writes[0].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
1581 writes[0].destSet = demo->desc_set;
1582 writes[0].count = 1;
1583 writes[0].descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
1584 writes[0].pDescriptors = &demo->uniform_data.desc;
1585
1586 writes[1].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
1587 writes[1].destSet = demo->desc_set;
1588 writes[1].destBinding = 1;
1589 writes[1].count = DEMO_TEXTURE_COUNT;
1590 writes[1].descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
1591 writes[1].pDescriptors = tex_descs;
1592
1593 err = vkUpdateDescriptorSets(demo->device, 2, writes, 0, NULL);
1594 assert(!err);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001595}
1596
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001597static void demo_prepare(struct demo *demo)
1598{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001599 const VkCmdBufferCreateInfo cmd = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001600 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001601 .pNext = NULL,
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07001602 .queueNodeIndex = demo->graphics_queue_node_index,
Chia-I Wu57b23b42015-06-26 15:34:39 +08001603 .level = VK_CMD_BUFFER_LEVEL_PRIMARY,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001604 .flags = 0,
1605 };
Tony Barbourfdc2d352015-04-22 09:02:32 -06001606 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001607
1608 demo_prepare_buffers(demo);
1609 demo_prepare_depth(demo);
1610 demo_prepare_textures(demo);
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001611 demo_prepare_cube_data_buffer(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001612
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001613 demo_prepare_descriptor_layout(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001614 demo_prepare_pipeline(demo);
1615 demo_prepare_dynamic_states(demo);
1616
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001617 for (int i = 0; i < DEMO_BUFFER_COUNT; i++) {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001618 err = vkCreateCommandBuffer(demo->device, &cmd, &demo->buffers[i].cmd);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001619 assert(!err);
1620 }
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001621
Chia-I Wu63ea9262015-03-26 13:14:16 +08001622 demo_prepare_descriptor_pool(demo);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001623 demo_prepare_descriptor_set(demo);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001624
1625
1626 for (int i = 0; i < DEMO_BUFFER_COUNT; i++) {
1627 demo->current_buffer = i;
1628 demo_draw_build_cmd(demo, demo->buffers[i].cmd);
1629 }
1630
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001631 /*
1632 * Prepare functions above may generate pipeline commands
1633 * that need to be flushed before beginning the render loop.
1634 */
1635 demo_flush_init_cmd(demo);
1636
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001637 demo->current_buffer = 0;
Jon Ashburn8d26c062015-04-24 09:46:24 -07001638 demo->prepared = true;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001639}
1640
David Pinedo2bb7c932015-06-18 17:03:14 -06001641static void demo_cleanup(struct demo *demo)
1642{
1643 uint32_t i;
1644
1645 demo->prepared = false;
1646
1647 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DESCRIPTOR_SET, demo->desc_set);
1648 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DESCRIPTOR_POOL, demo->desc_pool);
1649
1650 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DYNAMIC_VP_STATE, demo->viewport);
1651 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DYNAMIC_RS_STATE, demo->raster);
1652 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DYNAMIC_CB_STATE, demo->color_blend);
1653 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DYNAMIC_DS_STATE, demo->depth_stencil);
1654
1655 vkDestroyObject(demo->device, VK_OBJECT_TYPE_PIPELINE, demo->pipeline);
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001656 vkDestroyPipelineCache(demo->device, demo->pipelineCache);
David Pinedo2bb7c932015-06-18 17:03:14 -06001657 vkDestroyObject(demo->device, VK_OBJECT_TYPE_PIPELINE_LAYOUT, demo->pipeline_layout);
1658 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DESCRIPTOR_SET_LAYOUT, demo->desc_layout);
1659
1660 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
1661 vkDestroyObject(demo->device, VK_OBJECT_TYPE_IMAGE_VIEW, demo->textures[i].view);
1662 vkDestroyObject(demo->device, VK_OBJECT_TYPE_IMAGE, demo->textures[i].image);
1663 vkFreeMemory(demo->device, demo->textures[i].mem);
1664 vkDestroyObject(demo->device, VK_OBJECT_TYPE_SAMPLER, demo->textures[i].sampler);
1665 }
1666 demo->fpDestroySwapChainWSI(demo->swap_chain);
1667
1668 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DEPTH_STENCIL_VIEW, demo->depth.view);
1669 vkDestroyObject(demo->device, VK_OBJECT_TYPE_IMAGE, demo->depth.image);
1670 vkFreeMemory(demo->device, demo->depth.mem);
1671
1672 vkDestroyObject(demo->device, VK_OBJECT_TYPE_BUFFER_VIEW, demo->uniform_data.view);
1673 vkDestroyObject(demo->device, VK_OBJECT_TYPE_BUFFER, demo->uniform_data.buf);
1674 vkFreeMemory(demo->device, demo->uniform_data.mem);
1675
1676 for (i = 0; i < DEMO_BUFFER_COUNT; i++) {
1677 vkDestroyObject(demo->device, VK_OBJECT_TYPE_COLOR_ATTACHMENT_VIEW, demo->buffers[i].view);
1678 vkDestroyObject(demo->device, VK_OBJECT_TYPE_COMMAND_BUFFER, demo->buffers[i].cmd);
1679 }
1680
1681 vkDestroyDevice(demo->device);
Tony Barbourd70b42c2015-06-30 14:14:19 -06001682 if (demo->validate) {
1683 demo->dbgDestroyMsgCallback(demo->inst, demo->msg_callback);
1684 }
David Pinedo2bb7c932015-06-18 17:03:14 -06001685 vkDestroyInstance(demo->inst);
1686
1687#ifndef _WIN32
1688 xcb_destroy_window(demo->connection, demo->window);
1689 xcb_disconnect(demo->connection);
1690#endif // _WIN32
1691}
1692
1693// On MS-Windows, make this a global, so it's available to WndProc()
1694struct demo demo;
1695
Ian Elliott639ca472015-04-16 15:23:05 -06001696#ifdef _WIN32
1697static void demo_run(struct demo *demo)
1698{
Courtney Goeltzenleuchterb7e22702015-04-27 14:56:34 -06001699 if (!demo->prepared)
1700 return;
Ian Elliott639ca472015-04-16 15:23:05 -06001701 // Wait for work to finish before updating MVP.
1702 vkDeviceWaitIdle(demo->device);
1703 demo_update_data_buffer(demo);
1704
1705 demo_draw(demo);
1706
1707 // Wait for work to finish before updating MVP.
1708 vkDeviceWaitIdle(demo->device);
Ian Elliott639ca472015-04-16 15:23:05 -06001709
David Pinedo2bb7c932015-06-18 17:03:14 -06001710 demo->curFrame++;
1711
1712 if (demo->frameCount != INT_MAX && demo->curFrame == demo->frameCount)
1713 {
1714 demo->quit=true;
1715 demo_cleanup(demo);
1716 ExitProcess(0);
1717 }
1718
1719}
Ian Elliott639ca472015-04-16 15:23:05 -06001720
1721// MS-Windows event handling function:
1722LRESULT CALLBACK WndProc(HWND hWnd,
1723 UINT uMsg,
1724 WPARAM wParam,
1725 LPARAM lParam)
1726{
Ian Elliott639ca472015-04-16 15:23:05 -06001727 switch(uMsg)
1728 {
Ian Elliott639ca472015-04-16 15:23:05 -06001729 case WM_CLOSE:
1730 PostQuitMessage(0);
Tony Barbour3dddd5d2015-04-29 16:19:20 -06001731 break;
Ian Elliott639ca472015-04-16 15:23:05 -06001732 case WM_PAINT:
1733 demo_run(&demo);
1734 return 0;
1735 default:
1736 break;
1737 }
1738 return (DefWindowProc(hWnd, uMsg, wParam, lParam));
1739}
1740
1741static void demo_create_window(struct demo *demo)
1742{
1743 WNDCLASSEX win_class;
1744
1745 // Initialize the window class structure:
1746 win_class.cbSize = sizeof(WNDCLASSEX);
1747 win_class.style = CS_HREDRAW | CS_VREDRAW;
1748 win_class.lpfnWndProc = WndProc;
1749 win_class.cbClsExtra = 0;
1750 win_class.cbWndExtra = 0;
1751 win_class.hInstance = demo->connection; // hInstance
1752 win_class.hIcon = LoadIcon(NULL, IDI_APPLICATION);
1753 win_class.hCursor = LoadCursor(NULL, IDC_ARROW);
1754 win_class.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
1755 win_class.lpszMenuName = NULL;
1756 win_class.lpszClassName = demo->name;
1757 win_class.hIconSm = LoadIcon(NULL, IDI_WINLOGO);
1758 // Register window class:
1759 if (!RegisterClassEx(&win_class)) {
1760 // It didn't work, so try to give a useful error:
1761 printf("Unexpected error trying to start the application!\n");
1762 fflush(stdout);
1763 exit(1);
1764 }
1765 // Create window with the registered class:
Mike Stroyanb46779e2015-06-15 14:20:13 -06001766 RECT wr = { 0, 0, demo->width, demo->height };
1767 AdjustWindowRect(&wr, WS_OVERLAPPEDWINDOW, FALSE);
Ian Elliott639ca472015-04-16 15:23:05 -06001768 demo->window = CreateWindowEx(0,
1769 demo->name, // class name
1770 demo->name, // app name
1771 WS_OVERLAPPEDWINDOW | // window style
1772 WS_VISIBLE |
1773 WS_SYSMENU,
1774 100,100, // x/y coords
Mike Stroyanb46779e2015-06-15 14:20:13 -06001775 wr.right-wr.left, // width
1776 wr.bottom-wr.top, // height
Ian Elliott639ca472015-04-16 15:23:05 -06001777 NULL, // handle to parent
1778 NULL, // handle to menu
1779 demo->connection, // hInstance
1780 NULL); // no extra parameters
1781 if (!demo->window) {
1782 // It didn't work, so try to give a useful error:
1783 printf("Cannot create a window in which to draw!\n");
1784 fflush(stdout);
1785 exit(1);
1786 }
1787}
1788#else // _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001789static void demo_handle_event(struct demo *demo,
1790 const xcb_generic_event_t *event)
1791{
Piers Daniell735ee532015-02-23 16:23:13 -07001792 uint8_t event_code = event->response_type & 0x7f;
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07001793 switch (event_code) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001794 case XCB_EXPOSE:
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07001795 // TODO: Resize window
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001796 break;
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07001797 case XCB_CLIENT_MESSAGE:
1798 if((*(xcb_client_message_event_t*)event).data.data32[0] ==
1799 (*demo->atom_wm_delete_window).atom) {
1800 demo->quit = true;
1801 }
1802 break;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001803 case XCB_KEY_RELEASE:
1804 {
1805 const xcb_key_release_event_t *key =
1806 (const xcb_key_release_event_t *) event;
1807
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001808 switch (key->detail) {
1809 case 0x9: // Escape
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001810 demo->quit = true;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001811 break;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001812 case 0x71: // left arrow key
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001813 demo->spin_angle += demo->spin_increment;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001814 break;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001815 case 0x72: // right arrow key
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001816 demo->spin_angle -= demo->spin_increment;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001817 break;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001818 case 0x41:
1819 demo->pause = !demo->pause;
Piers Daniell735ee532015-02-23 16:23:13 -07001820 break;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001821 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001822 }
1823 break;
1824 default:
1825 break;
1826 }
1827}
1828
1829static void demo_run(struct demo *demo)
1830{
1831 xcb_flush(demo->connection);
1832
1833 while (!demo->quit) {
1834 xcb_generic_event_t *event;
1835
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07001836 if (demo->pause) {
1837 event = xcb_wait_for_event(demo->connection);
1838 } else {
1839 event = xcb_poll_for_event(demo->connection);
1840 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001841 if (event) {
1842 demo_handle_event(demo, event);
1843 free(event);
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07001844 }
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07001845
1846 // Wait for work to finish before updating MVP.
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001847 vkDeviceWaitIdle(demo->device);
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07001848 demo_update_data_buffer(demo);
1849
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07001850 demo_draw(demo);
Courtney Goeltzenleuchter1454f3c2014-11-18 11:28:09 -07001851
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07001852 // Wait for work to finish before updating MVP.
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001853 vkDeviceWaitIdle(demo->device);
David Pinedo2bb7c932015-06-18 17:03:14 -06001854 demo->curFrame++;
1855 if (demo->frameCount != INT_MAX && demo->curFrame == demo->frameCount)
1856 demo->quit = true;
1857
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001858 }
1859}
1860
1861static void demo_create_window(struct demo *demo)
1862{
1863 uint32_t value_mask, value_list[32];
1864
1865 demo->window = xcb_generate_id(demo->connection);
1866
1867 value_mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK;
1868 value_list[0] = demo->screen->black_pixel;
1869 value_list[1] = XCB_EVENT_MASK_KEY_RELEASE |
1870 XCB_EVENT_MASK_EXPOSURE;
1871
1872 xcb_create_window(demo->connection,
1873 XCB_COPY_FROM_PARENT,
1874 demo->window, demo->screen->root,
1875 0, 0, demo->width, demo->height, 0,
1876 XCB_WINDOW_CLASS_INPUT_OUTPUT,
1877 demo->screen->root_visual,
1878 value_mask, value_list);
1879
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07001880 /* Magic code that will send notification when window is destroyed */
1881 xcb_intern_atom_cookie_t cookie = xcb_intern_atom(demo->connection, 1, 12,
1882 "WM_PROTOCOLS");
1883 xcb_intern_atom_reply_t* reply = xcb_intern_atom_reply(demo->connection, cookie, 0);
1884
1885 xcb_intern_atom_cookie_t cookie2 = xcb_intern_atom(demo->connection, 0, 16, "WM_DELETE_WINDOW");
1886 demo->atom_wm_delete_window = xcb_intern_atom_reply(demo->connection, cookie2, 0);
1887
1888 xcb_change_property(demo->connection, XCB_PROP_MODE_REPLACE,
1889 demo->window, (*reply).atom, 4, 32, 1,
1890 &(*demo->atom_wm_delete_window).atom);
1891 free(reply);
1892
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001893 xcb_map_window(demo->connection, demo->window);
David Pinedo2bb7c932015-06-18 17:03:14 -06001894
1895 // Force the x/y coordinates to 100,100 results are identical in consecutive runs
1896 const uint32_t coords[] = {100, 100};
1897 xcb_configure_window(demo->connection, demo->window,
1898 XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y, coords);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001899}
Ian Elliott639ca472015-04-16 15:23:05 -06001900#endif // _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001901
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06001902/*
1903 * Return 1 (true) if all layer names specified in check_names
1904 * can be found in given layer properties.
1905 */
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06001906static VkBool32 demo_check_layers(uint32_t check_count, char **check_names,
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06001907 uint32_t layer_count, VkLayerProperties *layers)
1908{
1909 for (uint32_t i = 0; i < check_count; i++) {
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06001910 VkBool32 found = 0;
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06001911 for (uint32_t j = 0; j < layer_count; j++) {
1912 if (!strcmp(check_names[i], layers[j].layerName)) {
1913 found = 1;
1914 }
1915 }
1916 if (!found) {
1917 fprintf(stderr, "Cannot find layer: %s\n", check_names[i]);
1918 return 0;
1919 }
1920 }
1921 return 1;
1922}
1923
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001924static void demo_init_vk(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001925{
Tobin Ehlis99239dc2015-04-16 18:04:57 -06001926 VkResult err;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06001927 char *extension_names[64];
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06001928 VkExtensionProperties *instance_extensions;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06001929 VkLayerProperties *instance_layers;
1930 VkLayerProperties *device_layers;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06001931 uint32_t instance_extension_count = 0;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06001932 uint32_t instance_layer_count = 0;
1933 uint32_t enabled_extension_count = 0;
1934 uint32_t enabled_layer_count = 0;
1935
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06001936 char *instance_validation_layers[] = {
1937 "MemTracker",
1938 };
1939
1940 char *device_validation_layers[] = {
1941 "MemTracker",
1942 };
1943
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06001944 /* Look for validation layers */
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06001945 VkBool32 validation_found = 0;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06001946 err = vkGetGlobalLayerProperties(&instance_layer_count, NULL);
Tobin Ehlis99239dc2015-04-16 18:04:57 -06001947 assert(!err);
1948
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06001949 instance_layers = malloc(sizeof(VkLayerProperties) * instance_layer_count);
1950 err = vkGetGlobalLayerProperties(&instance_layer_count, instance_layers);
1951 assert(!err);
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06001952
1953 if (demo->validate) {
1954 validation_found = demo_check_layers(ARRAY_SIZE(instance_validation_layers), instance_validation_layers,
1955 instance_layer_count, instance_layers);
1956 if (!validation_found) {
1957 ERR_EXIT("vkGetGlobalLayerProperties failed to find"
1958 "required validation layer.\n\n"
1959 "Please look at the Getting Started guide for additional "
1960 "information.\n",
1961 "vkCreateInstance Failure");
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06001962 }
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06001963 enabled_layer_count = ARRAY_SIZE(instance_validation_layers);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06001964 }
1965
1966 err = vkGetGlobalExtensionProperties(NULL, &instance_extension_count, NULL);
1967 assert(!err);
1968
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06001969 VkBool32 WSIextFound = 0;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06001970 memset(extension_names, 0, sizeof(extension_names));
1971 instance_extensions = malloc(sizeof(VkExtensionProperties) * instance_extension_count);
1972 err = vkGetGlobalExtensionProperties(NULL, &instance_extension_count, instance_extensions);
1973 assert(!err);
1974 for (uint32_t i = 0; i < instance_extension_count; i++) {
1975 if (!strcmp(VK_WSI_LUNARG_EXTENSION_NAME, instance_extensions[i].extName)) {
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06001976 WSIextFound = 1;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06001977 extension_names[enabled_extension_count++] = VK_WSI_LUNARG_EXTENSION_NAME;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06001978 }
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06001979 if (!strcmp(DEBUG_REPORT_EXTENSION_NAME, instance_extensions[i].extName)) {
1980 if (demo->validate) {
1981 extension_names[enabled_extension_count++] = DEBUG_REPORT_EXTENSION_NAME;
1982 }
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06001983 }
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06001984 assert(enabled_extension_count < 64);
Tobin Ehlis99239dc2015-04-16 18:04:57 -06001985 }
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06001986 if (!WSIextFound) {
Tony Barbourecf1b2b2015-06-24 16:06:58 -06001987 ERR_EXIT("vkGetGlobalExtensionProperties failed to find the "
Ian Elliott65152912015-04-28 13:22:33 -06001988 "\"VK_WSI_LunarG\" extension.\n\nDo you have a compatible "
1989 "Vulkan installable client driver (ICD) installed?\nPlease "
1990 "look at the Getting Started guide for additional "
1991 "information.\n",
1992 "vkCreateInstance Failure");
1993 }
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001994 const VkApplicationInfo app = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001995 .sType = VK_STRUCTURE_TYPE_APPLICATION_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001996 .pNext = NULL,
Ian Elliott44e33f72015-04-28 10:52:52 -06001997 .pAppName = APP_SHORT_NAME,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001998 .appVersion = 0,
Ian Elliott44e33f72015-04-28 10:52:52 -06001999 .pEngineName = APP_SHORT_NAME,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002000 .engineVersion = 0,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002001 .apiVersion = VK_API_VERSION,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002002 };
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002003 VkInstanceCreateInfo inst_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002004 .sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
Jon Ashburnab46b362015-04-04 14:52:07 -06002005 .pNext = NULL,
2006 .pAppInfo = &app,
2007 .pAllocCb = NULL,
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002008 .layerCount = enabled_layer_count,
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002009 .ppEnabledLayerNames = (const char *const*) ((demo->validate) ? instance_validation_layers : NULL),
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002010 .extensionCount = enabled_extension_count,
2011 .ppEnabledExtensionNames = (const char *const*) extension_names,
Jon Ashburnab46b362015-04-04 14:52:07 -06002012 };
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -06002013 const VkDeviceQueueCreateInfo queue = {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002014 .queueNodeIndex = 0,
2015 .queueCount = 1,
2016 };
Ian Elliott097d9f32015-04-28 11:35:02 -06002017
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06002018 uint32_t gpu_count;
2019 uint32_t i;
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07002020 uint32_t queue_count;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002021
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002022 err = vkCreateInstance(&inst_info, &demo->inst);
Ian Elliott07264132015-04-28 11:35:02 -06002023 if (err == VK_ERROR_INCOMPATIBLE_DRIVER) {
2024 ERR_EXIT("Cannot find a compatible Vulkan installable client driver "
Ian Elliott65152912015-04-28 13:22:33 -06002025 "(ICD).\n\nPlease look at the Getting Started guide for "
Ian Elliott07264132015-04-28 11:35:02 -06002026 "additional information.\n",
2027 "vkCreateInstance Failure");
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002028 } else if (err == VK_ERROR_INVALID_EXTENSION) {
2029 ERR_EXIT("Cannot find a specified extension library"
2030 ".\nMake sure your layers path is set appropriately\n",
2031 "vkCreateInstance Failure");
Ian Elliott07264132015-04-28 11:35:02 -06002032 } else if (err) {
Ian Elliott65152912015-04-28 13:22:33 -06002033 ERR_EXIT("vkCreateInstance failed.\n\nDo you have a compatible Vulkan "
2034 "installable client driver (ICD) installed?\nPlease look at "
Ian Elliott07264132015-04-28 11:35:02 -06002035 "the Getting Started guide for additional information.\n",
2036 "vkCreateInstance Failure");
Ian Elliott3979e282015-04-03 15:24:55 -06002037 }
Jon Ashburnab46b362015-04-04 14:52:07 -06002038
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002039 free(instance_layers);
2040 free(instance_extensions);
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002041
Jon Ashburn07c0c0c2015-04-15 11:31:12 -06002042 gpu_count = 1;
2043 err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, &demo->gpu);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002044 assert(!err && gpu_count == 1);
2045
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002046 /* Look for validation layers */
2047 validation_found = 0;
2048 enabled_layer_count = 0;
2049 uint32_t device_layer_count = 0;
2050 err = vkGetPhysicalDeviceLayerProperties(demo->gpu, &device_layer_count, NULL);
2051 assert(!err);
2052
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002053 device_layers = malloc(sizeof(VkLayerProperties) * device_layer_count);
2054 err = vkGetPhysicalDeviceLayerProperties(demo->gpu, &device_layer_count, device_layers);
2055 assert(!err);
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002056
2057 if (demo->validate) {
2058 validation_found = demo_check_layers(ARRAY_SIZE(device_validation_layers), device_validation_layers,
2059 device_layer_count, device_layers);
2060 if (!validation_found) {
2061 ERR_EXIT("vkGetPhysicalDeviceLayerProperties failed to find"
2062 "a required validation layer.\n\n"
2063 "Please look at the Getting Started guide for additional "
2064 "information.\n",
2065 "vkCreateDevice Failure");
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002066 }
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002067 enabled_layer_count = ARRAY_SIZE(device_validation_layers);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002068 }
2069
2070 uint32_t device_extension_count = 0;
2071 VkExtensionProperties *device_extensions = NULL;
2072 err = vkGetPhysicalDeviceExtensionProperties(
2073 demo->gpu, NULL, &device_extension_count, NULL);
2074 assert(!err);
2075
2076 WSIextFound = 0;
2077 enabled_extension_count = 0;
2078 memset(extension_names, 0, sizeof(extension_names));
2079 device_extensions = malloc(sizeof(VkExtensionProperties) * device_extension_count);
2080 err = vkGetPhysicalDeviceExtensionProperties(
2081 demo->gpu, NULL, &device_extension_count, device_extensions);
2082 assert(!err);
Courtney Goeltzenleuchterff6e23a2015-07-05 22:08:51 -06002083#if 0
2084 /* Will need this check in future */
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002085 for (uint32_t i = 0; i < device_extension_count; i++) {
2086 if (!strcmp(VK_WSI_LUNARG_EXTENSION_NAME, device_extensions[i].extName)) {
2087 WSIextFound = 1;
2088 extension_names[enabled_extension_count++] = VK_WSI_LUNARG_EXTENSION_NAME;
2089 }
2090 assert(enabled_extension_count < 64);
2091 }
2092 if (!WSIextFound) {
2093 ERR_EXIT("vkGetGlobalExtensionProperties failed to find the "
2094 "\"VK_WSI_LunarG\" extension.\n\nDo you have a compatible "
2095 "Vulkan installable client driver (ICD) installed?\nPlease "
2096 "look at the Getting Started guide for additional "
2097 "information.\n",
2098 "vkCreateInstance Failure");
2099 }
Courtney Goeltzenleuchterff6e23a2015-07-05 22:08:51 -06002100#endif
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002101
2102 VkDeviceCreateInfo device = {
2103 .sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
2104 .pNext = NULL,
2105 .queueRecordCount = 1,
2106 .pRequestedQueues = &queue,
2107 .layerCount = enabled_layer_count,
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002108 .ppEnabledLayerNames = (const char *const*) ((demo->validate) ? device_validation_layers : NULL),
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002109 .extensionCount = enabled_extension_count,
2110 .ppEnabledExtensionNames = (const char *const*) extension_names,
2111 .flags = 0,
2112 };
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002113
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002114 if (demo->validate) {
Tony Barbourd70b42c2015-06-30 14:14:19 -06002115 demo->dbgCreateMsgCallback = vkGetInstanceProcAddr(demo->inst, "vkDbgCreateMsgCallback");
2116 demo->dbgDestroyMsgCallback = vkGetInstanceProcAddr(demo->inst, "vkDbgDestroyMsgCallback");
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002117 if (!demo->dbgCreateMsgCallback) {
2118 ERR_EXIT("GetProcAddr: Unable to find vkDbgCreateMsgCallback\n",
2119 "vkGetProcAddr Failure");
2120 }
Tony Barbourd70b42c2015-06-30 14:14:19 -06002121 if (!demo->dbgDestroyMsgCallback) {
2122 ERR_EXIT("GetProcAddr: Unable to find vkDbgDestroyMsgCallback\n",
2123 "vkGetProcAddr Failure");
2124 }
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002125 err = demo->dbgCreateMsgCallback(
2126 demo->inst,
2127 VK_DBG_REPORT_ERROR_BIT | VK_DBG_REPORT_WARN_BIT,
2128 dbgFunc, NULL,
2129 &demo->msg_callback);
2130 switch (err) {
2131 case VK_SUCCESS:
2132 break;
2133 case VK_ERROR_INVALID_POINTER:
2134 ERR_EXIT("dbgCreateMsgCallback: Invalid pointer\n",
2135 "dbgCreateMsgCallback Failure");
2136 break;
2137 case VK_ERROR_OUT_OF_HOST_MEMORY:
2138 ERR_EXIT("dbgCreateMsgCallback: out of host memory\n",
2139 "dbgCreateMsgCallback Failure");
2140 break;
2141 default:
2142 ERR_EXIT("dbgCreateMsgCallback: unknown failure\n",
2143 "dbgCreateMsgCallback Failure");
2144 break;
2145 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002146 }
2147
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002148 err = vkCreateDevice(demo->gpu, &device, &demo->device);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002149 assert(!err);
2150
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002151 free(device_layers);
2152
Ian Elliott673898b2015-06-22 15:07:49 -06002153 GET_DEVICE_PROC_ADDR(demo->device, CreateSwapChainWSI);
2154 GET_DEVICE_PROC_ADDR(demo->device, CreateSwapChainWSI);
2155 GET_DEVICE_PROC_ADDR(demo->device, DestroySwapChainWSI);
2156 GET_DEVICE_PROC_ADDR(demo->device, GetSwapChainInfoWSI);
2157 GET_DEVICE_PROC_ADDR(demo->device, QueuePresentWSI);
Jon Ashburn0b85d052015-05-21 18:13:33 -06002158
Tony Barbourecf1b2b2015-06-24 16:06:58 -06002159 err = vkGetPhysicalDeviceProperties(demo->gpu, &demo->gpu_props);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06002160 assert(!err);
2161
Tony Barbourecf1b2b2015-06-24 16:06:58 -06002162 err = vkGetPhysicalDeviceQueueCount(demo->gpu, &queue_count);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06002163 assert(!err);
2164
Tony Barbourecf1b2b2015-06-24 16:06:58 -06002165 demo->queue_props = (VkPhysicalDeviceQueueProperties *) malloc(queue_count * sizeof(VkPhysicalDeviceQueueProperties));
2166 err = vkGetPhysicalDeviceQueueProperties(demo->gpu, queue_count, demo->queue_props);
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07002167 assert(!err);
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07002168 assert(queue_count >= 1);
2169
Mark Lobodzinskida9b1092015-04-16 11:44:05 -05002170 // Graphics queue and MemMgr queue can be separate.
2171 // TODO: Add support for separate queues, including synchronization,
Mark Lobodzinski6baaec72015-05-11 17:21:15 -05002172 // and appropriate tracking for QueueSubmit
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07002173 for (i = 0; i < queue_count; i++) {
Mark Lobodzinski6baaec72015-05-11 17:21:15 -05002174 if (demo->queue_props[i].queueFlags & VK_QUEUE_GRAPHICS_BIT)
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07002175 break;
2176 }
2177 assert(i < queue_count);
2178 demo->graphics_queue_node_index = i;
2179
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002180 err = vkGetDeviceQueue(demo->device, demo->graphics_queue_node_index,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002181 0, &demo->queue);
2182 assert(!err);
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002183
Jon Ashburnd7130cb2015-06-16 12:44:51 -06002184 // for now hardcode format till get WSI support
2185 demo->format = VK_FORMAT_B8G8R8A8_UNORM;
Ian Elliotte4602cd2015-04-21 16:41:02 -06002186
David Pinedo2bb7c932015-06-18 17:03:14 -06002187 demo->quit = false;
2188 demo->curFrame = 0;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06002189
2190 // Get Memory information and properties
2191 err = vkGetPhysicalDeviceMemoryProperties(demo->gpu, &demo->memory_properties);
2192 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002193}
2194
2195static void demo_init_connection(struct demo *demo)
2196{
Ian Elliott639ca472015-04-16 15:23:05 -06002197#ifndef _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002198 const xcb_setup_t *setup;
2199 xcb_screen_iterator_t iter;
2200 int scr;
2201
2202 demo->connection = xcb_connect(NULL, &scr);
Ian Elliott3979e282015-04-03 15:24:55 -06002203 if (demo->connection == NULL) {
2204 printf("Cannot find a compatible Vulkan installable client driver "
2205 "(ICD).\nExiting ...\n");
2206 fflush(stdout);
2207 exit(1);
2208 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002209
2210 setup = xcb_get_setup(demo->connection);
2211 iter = xcb_setup_roots_iterator(setup);
2212 while (scr-- > 0)
2213 xcb_screen_next(&iter);
2214
2215 demo->screen = iter.data;
Ian Elliott639ca472015-04-16 15:23:05 -06002216#endif // _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002217}
2218
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002219static void demo_init(struct demo *demo, int argc, char **argv)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002220{
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002221 vec3 eye = {0.0f, 3.0f, 5.0f};
2222 vec3 origin = {0, 0, 0};
Chia-I Wuae3b55d2015-04-22 14:56:17 +08002223 vec3 up = {0.0f, 1.0f, 0.0};
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002224
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002225 memset(demo, 0, sizeof(*demo));
David Pinedo2bb7c932015-06-18 17:03:14 -06002226 demo->frameCount = INT_MAX;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002227
Piers Daniell735ee532015-02-23 16:23:13 -07002228 for (int i = 1; i < argc; i++) {
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002229 if (strcmp(argv[i], "--use_staging") == 0) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002230 demo->use_staging_buffer = true;
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002231 continue;
Ian Elliott639ca472015-04-16 15:23:05 -06002232 }
Cody Northrop1fedb212015-05-28 11:27:16 -06002233 if (strcmp(argv[i], "--use_glsl") == 0) {
2234 demo->use_glsl = true;
2235 continue;
2236 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002237 if (strcmp(argv[i], "--validate") == 0) {
2238 demo->validate = true;
2239 continue;
2240 }
David Pinedo2bb7c932015-06-18 17:03:14 -06002241 if (strcmp(argv[i], "--c") == 0 &&
2242 demo->frameCount == INT_MAX &&
2243 i < argc-1 &&
2244 sscanf(argv[i+1],"%d", &demo->frameCount) == 1 &&
2245 demo->frameCount >= 0)
2246 {
2247 i++;
2248 continue;
2249 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002250
David Pinedo2bb7c932015-06-18 17:03:14 -06002251 fprintf(stderr, "Usage:\n %s [--use_staging] [--validate] [--c <framecount>]\n", APP_SHORT_NAME);
Ian Elliott639ca472015-04-16 15:23:05 -06002252 fflush(stderr);
2253 exit(1);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002254 }
2255
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002256 demo_init_connection(demo);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002257 demo_init_vk(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002258
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002259 demo->width = 500;
2260 demo->height = 500;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002261
2262 demo->spin_angle = 0.01f;
2263 demo->spin_increment = 0.01f;
2264 demo->pause = false;
2265
Piers Daniell735ee532015-02-23 16:23:13 -07002266 mat4x4_perspective(demo->projection_matrix, (float)degreesToRadians(45.0f), 1.0f, 0.1f, 100.0f);
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002267 mat4x4_look_at(demo->view_matrix, eye, origin, up);
2268 mat4x4_identity(demo->model_matrix);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002269}
2270
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002271
Ian Elliott639ca472015-04-16 15:23:05 -06002272#ifdef _WIN32
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002273extern int __getmainargs(
2274 int * _Argc,
2275 char *** _Argv,
2276 char *** _Env,
2277 int _DoWildCard,
2278 int * new_mode);
Ian Elliottf9cf78c2015-04-28 10:33:11 -06002279
Ian Elliotta748eaf2015-04-28 15:50:36 -06002280int WINAPI WinMain(HINSTANCE hInstance,
2281 HINSTANCE hPrevInstance,
2282 LPSTR pCmdLine,
2283 int nCmdShow)
Ian Elliott639ca472015-04-16 15:23:05 -06002284{
2285 MSG msg; // message
2286 bool done; // flag saying when app is complete
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002287 int argc;
2288 char** argv;
2289 char** env;
2290 int new_mode = 0;
Ian Elliott639ca472015-04-16 15:23:05 -06002291
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002292 __getmainargs(&argc,&argv,&env,0,&new_mode);
2293
2294 demo_init(&demo, argc, argv);
2295 demo.connection = hInstance;
2296 strncpy(demo.name, "cube", APP_NAME_STR_LEN);
Ian Elliott639ca472015-04-16 15:23:05 -06002297 demo_create_window(&demo);
2298
2299 demo_prepare(&demo);
2300
2301 done = false; //initialize loop condition variable
2302 /* main message loop*/
2303 while(!done)
2304 {
Ian Elliotta748eaf2015-04-28 15:50:36 -06002305 PeekMessage(&msg, NULL, 0, 0, PM_REMOVE);
Ian Elliott639ca472015-04-16 15:23:05 -06002306 if (msg.message == WM_QUIT) //check for a quit message
2307 {
2308 done = true; //if found, quit app
2309 }
2310 else
2311 {
2312 /* Translate and dispatch to event queue*/
2313 TranslateMessage(&msg);
2314 DispatchMessage(&msg);
2315 }
2316 }
2317
2318 demo_cleanup(&demo);
2319
Tony Barbourf9921732015-04-22 11:36:22 -06002320 return (int) msg.wParam;
Ian Elliott639ca472015-04-16 15:23:05 -06002321}
2322#else // _WIN32
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002323int main(int argc, char **argv)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002324{
2325 struct demo demo;
2326
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002327 demo_init(&demo, argc, argv);
Chia-I Wucbb564e2015-04-16 22:02:10 +08002328 demo_create_window(&demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002329
2330 demo_prepare(&demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002331 demo_run(&demo);
2332
2333 demo_cleanup(&demo);
2334
2335 return 0;
2336}
Ian Elliott639ca472015-04-16 15:23:05 -06002337#endif // _WIN32