blob: f6c1aca1595b051062fc348fbbc82638c85e18ce [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(
Tony Barbour155cce82015-07-03 10:33:54 -0600251 VkFlags msgFlags,
252 VkDbgObjectType objType,
253 uint64_t srcObject,
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -0600254 size_t location,
255 int32_t msgCode,
256 const char* pLayerPrefix,
257 const char* pMsg,
258 void* pUserData)
Tony 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;
Cody Northrop91e221b2015-07-09 18:08:32 -0600319 VkCmdPool cmd_pool;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600320 struct {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600321 VkImage image;
Tony Barbour72304ef2015-04-16 15:59:00 -0600322 VkDeviceMemory mem;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600323 VkCmdBuffer cmd;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600324
Chia-I Wua74c5b22015-07-07 11:50:03 +0800325 VkAttachmentView view;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600326 } buffers[DEMO_BUFFER_COUNT];
327
328 struct {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600329 VkFormat format;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600330
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600331 VkImage image;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500332 VkDeviceMemory mem;
Chia-I Wua74c5b22015-07-07 11:50:03 +0800333 VkAttachmentView view;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600334 } depth;
335
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600336 struct texture_object textures[DEMO_TEXTURE_COUNT];
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600337
338 struct {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600339 VkBuffer buf;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500340 VkDeviceMemory mem;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600341 VkBufferView view;
Chia-I Wuae721ba2015-05-25 16:27:55 +0800342 VkDescriptorInfo desc;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600343 } uniform_data;
344
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600345 VkCmdBuffer cmd; // Buffer for initialization commands
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -0500346 VkPipelineLayout pipeline_layout;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600347 VkDescriptorSetLayout desc_layout;
Jon Ashburnc4ab7af2015-07-09 15:02:25 -0600348 VkPipelineCache pipelineCache;
Chia-I Wuf973e322015-07-08 13:34:24 +0800349 VkRenderPass render_pass;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600350 VkPipeline pipeline;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600351
Tony Barbour155cce82015-07-03 10:33:54 -0600352 VkDynamicViewportState viewport;
353 VkDynamicRasterState raster;
354 VkDynamicColorBlendState color_blend;
355 VkDynamicDepthStencilState depth_stencil;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600356
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600357 mat4x4 projection_matrix;
358 mat4x4 view_matrix;
359 mat4x4 model_matrix;
360
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600361 float spin_angle;
362 float spin_increment;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600363 bool pause;
364
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600365 VkDescriptorPool desc_pool;
366 VkDescriptorSet desc_set;
Chia-I Wu6a3c8972015-01-04 16:27:24 +0800367
Chia-I Wuf973e322015-07-08 13:34:24 +0800368 VkFramebuffer framebuffers[DEMO_BUFFER_COUNT];
369
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600370 bool quit;
David Pinedo2bb7c932015-06-18 17:03:14 -0600371 int32_t curFrame;
372 int32_t frameCount;
Tony Barbour3dddd5d2015-04-29 16:19:20 -0600373 bool validate;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -0600374 PFN_vkDbgCreateMsgCallback dbgCreateMsgCallback;
Tony Barbourd70b42c2015-06-30 14:14:19 -0600375 PFN_vkDbgDestroyMsgCallback dbgDestroyMsgCallback;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -0600376 VkDbgMsgCallback msg_callback;
377
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600378 uint32_t current_buffer;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600379};
380
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600381static VkResult memory_type_from_properties(struct demo *demo, uint32_t typeBits, VkFlags properties, uint32_t *typeIndex)
382{
383 // Search memtypes to find first index with those properties
384 for (uint32_t i = 0; i < 32; i++) {
385 if ((typeBits & 1) == 1) {
386 // Type is available, does it match user properties?
387 if ((demo->memory_properties.memoryTypes[i].propertyFlags & properties) == properties) {
388 *typeIndex = i;
389 return VK_SUCCESS;
390 }
391 }
392 typeBits >>= 1;
393 }
394 // No memory types matched, return failure
395 return VK_UNSUPPORTED;
396}
397
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600398static void demo_flush_init_cmd(struct demo *demo)
399{
Tony Barbourfdc2d352015-04-22 09:02:32 -0600400 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600401
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600402 if (demo->cmd == VK_NULL_HANDLE)
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600403 return;
404
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600405 err = vkEndCommandBuffer(demo->cmd);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600406 assert(!err);
407
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600408 const VkCmdBuffer cmd_bufs[] = { demo->cmd };
Tony Barbour155cce82015-07-03 10:33:54 -0600409 VkFence nullFence = { VK_NULL_HANDLE };
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600410
Tony Barbour155cce82015-07-03 10:33:54 -0600411 err = vkQueueSubmit(demo->queue, 1, cmd_bufs, nullFence);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600412 assert(!err);
413
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600414 err = vkQueueWaitIdle(demo->queue);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600415 assert(!err);
416
Tony Barbour155cce82015-07-03 10:33:54 -0600417 vkDestroyCommandBuffer(demo->device, demo->cmd);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600418 demo->cmd = VK_NULL_HANDLE;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600419}
420
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600421static void demo_set_image_layout(
422 struct demo *demo,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600423 VkImage image,
malnasseeb25d3f2015-06-03 17:28:38 -0400424 VkImageAspect aspect,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600425 VkImageLayout old_image_layout,
426 VkImageLayout new_image_layout)
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600427{
Tony Barbourfdc2d352015-04-22 09:02:32 -0600428 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600429
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600430 if (demo->cmd == VK_NULL_HANDLE) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600431 const VkCmdBufferCreateInfo cmd = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600432 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600433 .pNext = NULL,
Cody Northrop91e221b2015-07-09 18:08:32 -0600434 .cmdPool = demo->cmd_pool,
Chia-I Wu57b23b42015-06-26 15:34:39 +0800435 .level = VK_CMD_BUFFER_LEVEL_PRIMARY,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600436 .flags = 0,
437 };
438
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600439 err = vkCreateCommandBuffer(demo->device, &cmd, &demo->cmd);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600440 assert(!err);
441
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600442 VkCmdBufferBeginInfo cmd_buf_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600443 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600444 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -0600445 .flags = VK_CMD_BUFFER_OPTIMIZE_SMALL_BATCH_BIT |
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600446 VK_CMD_BUFFER_OPTIMIZE_ONE_TIME_SUBMIT_BIT,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600447 };
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600448 err = vkBeginCommandBuffer(demo->cmd, &cmd_buf_info);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600449 }
450
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600451 VkImageMemoryBarrier image_memory_barrier = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600452 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600453 .pNext = NULL,
454 .outputMask = 0,
455 .inputMask = 0,
456 .oldLayout = old_image_layout,
457 .newLayout = new_image_layout,
458 .image = image,
malnasseeb25d3f2015-06-03 17:28:38 -0400459 .subresourceRange = { aspect, 0, 1, 0, 0 }
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600460 };
461
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600462 if (new_image_layout == VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL) {
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600463 /* Make sure anything that was copying from this image has completed */
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -0600464 image_memory_barrier.inputMask = VK_MEMORY_INPUT_TRANSFER_BIT;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600465 }
466
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600467 if (new_image_layout == VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL) {
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600468 /* Make sure any Copy or CPU writes to image are flushed */
Courtney Goeltzenleuchter2bda8332015-04-29 17:16:21 -0600469 image_memory_barrier.outputMask = VK_MEMORY_OUTPUT_HOST_WRITE_BIT | VK_MEMORY_OUTPUT_TRANSFER_BIT;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600470 }
471
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600472 VkImageMemoryBarrier *pmemory_barrier = &image_memory_barrier;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600473
Tony Barbour50bbca42015-06-29 16:20:35 -0600474 VkPipelineStageFlags src_stages = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
475 VkPipelineStageFlags dest_stages = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600476
Tony Barbour50bbca42015-06-29 16:20:35 -0600477 vkCmdPipelineBarrier(demo->cmd, src_stages, dest_stages, false, 1, (const void **)&pmemory_barrier);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600478}
479
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600480static void demo_draw_build_cmd(struct demo *demo, VkCmdBuffer cmd_buf)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600481{
Chia-I Wuf973e322015-07-08 13:34:24 +0800482 const VkCmdBufferBeginInfo cmd_buf_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600483 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO,
Courtney Goeltzenleuchter53968d82015-04-03 15:25:24 -0600484 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -0600485 .flags = VK_CMD_BUFFER_OPTIMIZE_SMALL_BATCH_BIT |
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600486 VK_CMD_BUFFER_OPTIMIZE_ONE_TIME_SUBMIT_BIT,
Jon Ashburn60ccfbe2014-12-31 17:08:35 -0700487 };
Chia-I Wua74c5b22015-07-07 11:50:03 +0800488 const VkClearValue clear_values[2] = {
489 [0] = { .color.f32 = { 0.2f, 0.2f, 0.2f, 0.2f } },
490 [1] = { .ds = { 1.0f, 0 } },
491 };
492 const VkRenderPassBeginInfo rp_begin = {
493 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO,
494 .pNext = NULL,
Chia-I Wuf973e322015-07-08 13:34:24 +0800495 .renderPass = demo->render_pass,
496 .framebuffer = demo->framebuffers[demo->current_buffer],
Chia-I Wua74c5b22015-07-07 11:50:03 +0800497 .renderArea.offset.x = 0,
498 .renderArea.offset.y = 0,
499 .renderArea.extent.width = demo->width,
500 .renderArea.extent.height = demo->height,
501 .attachmentCount = 2,
502 .pAttachmentClearValues = clear_values,
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700503 };
Chia-I Wuf973e322015-07-08 13:34:24 +0800504 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600505
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600506 err = vkBeginCommandBuffer(cmd_buf, &cmd_buf_info);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600507 assert(!err);
508
Chia-I Wua74c5b22015-07-07 11:50:03 +0800509 vkCmdBeginRenderPass(cmd_buf, &rp_begin, VK_RENDER_PASS_CONTENTS_INLINE);
510
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600511 vkCmdBindPipeline(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600512 demo->pipeline);
Mark Lobodzinskic6e8b3d2015-06-15 13:21:21 -0600513 vkCmdBindDescriptorSets(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS, demo->pipeline_layout,
Cody Northropfb5185a2015-04-16 13:41:56 -0600514 0, 1, &demo->desc_set, 0, NULL);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600515
Tony Barbour155cce82015-07-03 10:33:54 -0600516 vkCmdBindDynamicViewportState(cmd_buf, demo->viewport);
517 vkCmdBindDynamicRasterState(cmd_buf, demo->raster);
518 vkCmdBindDynamicColorBlendState(cmd_buf, demo->color_blend);
519 vkCmdBindDynamicDepthStencilState(cmd_buf, demo->depth_stencil);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600520
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600521 vkCmdDraw(cmd_buf, 0, 12 * 3, 0, 1);
Chia-I Wu57b23b42015-06-26 15:34:39 +0800522 vkCmdEndRenderPass(cmd_buf);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600523
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600524 err = vkEndCommandBuffer(cmd_buf);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600525 assert(!err);
526}
527
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600528
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600529void demo_update_data_buffer(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600530{
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600531 mat4x4 MVP, Model, VP;
532 int matrixSize = sizeof(MVP);
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600533 uint8_t *pData;
Tony Barbourfdc2d352015-04-22 09:02:32 -0600534 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600535
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600536 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600537
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600538 // Rotate 22.5 degrees around the Y axis
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600539 mat4x4_dup(Model, demo->model_matrix);
Piers Daniell735ee532015-02-23 16:23:13 -0700540 mat4x4_rotate(demo->model_matrix, Model, 0.0f, 1.0f, 0.0f, (float)degreesToRadians(demo->spin_angle));
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600541 mat4x4_mul(MVP, VP, demo->model_matrix);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600542
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500543 err = vkMapMemory(demo->device, demo->uniform_data.mem, 0, 0, 0, (void **) &pData);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600544 assert(!err);
545
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600546 memcpy(pData, (const void*) &MVP[0][0], matrixSize);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600547
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500548 err = vkUnmapMemory(demo->device, demo->uniform_data.mem);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600549 assert(!err);
550}
551
552static void demo_draw(struct demo *demo)
553{
Chia-I Wucbb564e2015-04-16 22:02:10 +0800554 const VkPresentInfoWSI present = {
555 .sType = VK_STRUCTURE_TYPE_PRESENT_INFO_WSI,
556 .pNext = NULL,
557 .image = demo->buffers[demo->current_buffer].image,
558 .flipInterval = 0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600559 };
Tony Barbourfdc2d352015-04-22 09:02:32 -0600560 VkResult U_ASSERT_ONLY err;
Tony Barbour155cce82015-07-03 10:33:54 -0600561 VkFence nullFence = { VK_NULL_HANDLE };
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600562
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600563 err = vkQueueSubmit(demo->queue, 1, &demo->buffers[demo->current_buffer].cmd,
Tony Barbour155cce82015-07-03 10:33:54 -0600564 nullFence);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600565 assert(!err);
566
Jon Ashburn0b85d052015-05-21 18:13:33 -0600567 err = demo->fpQueuePresentWSI(demo->queue, &present);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600568 assert(!err);
569
570 demo->current_buffer = (demo->current_buffer + 1) % DEMO_BUFFER_COUNT;
Chia-I Wucbb564e2015-04-16 22:02:10 +0800571
572 err = vkQueueWaitIdle(demo->queue);
573 assert(err == VK_SUCCESS);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600574}
575
576static void demo_prepare_buffers(struct demo *demo)
577{
Chia-I Wucbb564e2015-04-16 22:02:10 +0800578 const VkSwapChainCreateInfoWSI swap_chain = {
579 .sType = VK_STRUCTURE_TYPE_SWAP_CHAIN_CREATE_INFO_WSI,
580 .pNext = NULL,
581 .pNativeWindowSystemHandle = demo->connection,
582 .pNativeWindowHandle = (void *) (intptr_t) demo->window,
Ian Elliotte4602cd2015-04-21 16:41:02 -0600583 .displayCount = 1,
Chia-I Wucbb564e2015-04-16 22:02:10 +0800584 .imageCount = DEMO_BUFFER_COUNT,
585 .imageFormat = demo->format,
586 .imageExtent = {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600587 .width = demo->width,
588 .height = demo->height,
589 },
Chia-I Wucbb564e2015-04-16 22:02:10 +0800590 .imageArraySize = 1,
591 .imageUsageFlags = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600592 };
Chia-I Wucbb564e2015-04-16 22:02:10 +0800593 VkSwapChainImageInfoWSI images[DEMO_BUFFER_COUNT];
594 size_t images_size = sizeof(images);
Tony Barbourfdc2d352015-04-22 09:02:32 -0600595 VkResult U_ASSERT_ONLY err;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600596 uint32_t i;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600597
Jon Ashburn0b85d052015-05-21 18:13:33 -0600598 err = demo->fpCreateSwapChainWSI(demo->device, &swap_chain, &demo->swap_chain);
Chia-I Wucbb564e2015-04-16 22:02:10 +0800599 assert(!err);
600
Jon Ashburn0b85d052015-05-21 18:13:33 -0600601 err = demo->fpGetSwapChainInfoWSI(demo->swap_chain,
Chia-I Wucbb564e2015-04-16 22:02:10 +0800602 VK_SWAP_CHAIN_INFO_TYPE_PERSISTENT_IMAGES_WSI,
603 &images_size, images);
604 assert(!err && images_size == sizeof(images));
605
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600606 for (i = 0; i < DEMO_BUFFER_COUNT; i++) {
Chia-I Wua74c5b22015-07-07 11:50:03 +0800607 VkAttachmentViewCreateInfo color_attachment_view = {
608 .sType = VK_STRUCTURE_TYPE_ATTACHMENT_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600609 .pNext = NULL,
610 .format = demo->format,
611 .mipLevel = 0,
612 .baseArraySlice = 0,
613 .arraySize = 1,
614 };
615
Chia-I Wucbb564e2015-04-16 22:02:10 +0800616 demo->buffers[i].image = images[i].image;
617 demo->buffers[i].mem = images[i].memory;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600618
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600619 demo_set_image_layout(demo, demo->buffers[i].image,
malnasseeb25d3f2015-06-03 17:28:38 -0400620 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600621 VK_IMAGE_LAYOUT_UNDEFINED,
622 VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600623
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600624 color_attachment_view.image = demo->buffers[i].image;
625
Chia-I Wua74c5b22015-07-07 11:50:03 +0800626 err = vkCreateAttachmentView(demo->device,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600627 &color_attachment_view, &demo->buffers[i].view);
628 assert(!err);
629 }
630}
631
632static void demo_prepare_depth(struct demo *demo)
633{
Tony Barbour72304ef2015-04-16 15:59:00 -0600634 const VkFormat depth_format = VK_FORMAT_D16_UNORM;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600635 const VkImageCreateInfo image = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600636 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600637 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -0600638 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600639 .format = depth_format,
640 .extent = { demo->width, demo->height, 1 },
641 .mipLevels = 1,
642 .arraySize = 1,
643 .samples = 1,
Tony Barbour72304ef2015-04-16 15:59:00 -0600644 .tiling = VK_IMAGE_TILING_OPTIMAL,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600645 .usage = VK_IMAGE_USAGE_DEPTH_STENCIL_BIT,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600646 .flags = 0,
647 };
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -0600648 VkMemoryAllocInfo mem_alloc = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600649 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
Mark Lobodzinskia6907f72015-04-16 08:52:00 -0500650 .pNext = NULL,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600651 .allocationSize = 0,
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600652 .memoryTypeIndex = 0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600653 };
Chia-I Wua74c5b22015-07-07 11:50:03 +0800654 VkAttachmentViewCreateInfo view = {
655 .sType = VK_STRUCTURE_TYPE_ATTACHMENT_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600656 .pNext = NULL,
Tony Barbour155cce82015-07-03 10:33:54 -0600657 .image.handle = VK_NULL_HANDLE,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600658 .mipLevel = 0,
659 .baseArraySlice = 0,
660 .arraySize = 1,
661 .flags = 0,
662 };
Mike Stroyanebae8322015-04-17 12:36:38 -0600663
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500664 VkMemoryRequirements mem_reqs;
Tony Barbourfdc2d352015-04-22 09:02:32 -0600665 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600666
667 demo->depth.format = depth_format;
668
669 /* create image */
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600670 err = vkCreateImage(demo->device, &image,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600671 &demo->depth.image);
672 assert(!err);
673
Tony Barbour155cce82015-07-03 10:33:54 -0600674 err = vkGetImageMemoryRequirements(demo->device,
675 demo->depth.image, &mem_reqs);
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600676
677 mem_alloc.allocationSize = mem_reqs.size;
678 err = memory_type_from_properties(demo,
679 mem_reqs.memoryTypeBits,
680 VK_MEMORY_PROPERTY_DEVICE_ONLY,
681 &mem_alloc.memoryTypeIndex);
682 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600683
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500684 /* allocate memory */
685 err = vkAllocMemory(demo->device, &mem_alloc, &demo->depth.mem);
686 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600687
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500688 /* bind memory */
Tony Barbour155cce82015-07-03 10:33:54 -0600689 err = vkBindImageMemory(demo->device, demo->depth.image,
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500690 demo->depth.mem, 0);
691 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600692
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600693 demo_set_image_layout(demo, demo->depth.image,
malnasseeb25d3f2015-06-03 17:28:38 -0400694 VK_IMAGE_ASPECT_DEPTH,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600695 VK_IMAGE_LAYOUT_UNDEFINED,
696 VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600697
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600698 /* create image view */
699 view.image = demo->depth.image;
Chia-I Wua74c5b22015-07-07 11:50:03 +0800700 err = vkCreateAttachmentView(demo->device, &view, &demo->depth.view);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600701 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600702}
703
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600704/** loadTexture
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -0600705 * loads a png file into an memory object, using cstdio , libpng.
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600706 *
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -0600707 * \param demo : Needed to access VK calls
708 * \param filename : the png file to be loaded
709 * \param width : width of png, to be updated as a side effect of this function
710 * \param height : height of png, to be updated as a side effect of this function
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600711 *
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -0600712 * \return bool : an opengl texture id. true if successful?,
713 * should be validated by the client of this function.
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600714 *
715 * Source: http://en.wikibooks.org/wiki/OpenGL_Programming/Intermediate/Textures
716 * Modified to copy image to memory
717 *
718 */
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700719bool loadTexture(const char *filename, uint8_t *rgba_data,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600720 VkSubresourceLayout *layout,
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600721 int32_t *width, int32_t *height)
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600722{
723 //header for testing if it is a png
724 png_byte header[8];
Tony Barbourf9921732015-04-22 11:36:22 -0600725 int is_png, bit_depth, color_type, rowbytes;
726 size_t retval;
Ian Elliott1e42dff2015-02-13 14:29:21 -0700727 png_uint_32 i, twidth, theight;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600728 png_structp png_ptr;
729 png_infop info_ptr, end_info;
730 png_byte *image_data;
731 png_bytep *row_pointers;
732
733 //open file as binary
734 FILE *fp = fopen(filename, "rb");
735 if (!fp) {
736 return false;
737 }
738
739 //read the header
Tony Barbourfdc2d352015-04-22 09:02:32 -0600740 retval = fread(header, 1, 8, fp);
741 if (retval != 8) {
742 fclose(fp);
743 return false;
744 }
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600745
746 //test if png
747 is_png = !png_sig_cmp(header, 0, 8);
748 if (!is_png) {
749 fclose(fp);
750 return false;
751 }
752
753 //create png struct
754 png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL,
755 NULL, NULL);
756 if (!png_ptr) {
757 fclose(fp);
758 return (false);
759 }
760
761 //create png info struct
762 info_ptr = png_create_info_struct(png_ptr);
763 if (!info_ptr) {
764 png_destroy_read_struct(&png_ptr, (png_infopp) NULL, (png_infopp) NULL);
765 fclose(fp);
766 return (false);
767 }
768
769 //create png info struct
770 end_info = png_create_info_struct(png_ptr);
771 if (!end_info) {
772 png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp) NULL);
773 fclose(fp);
774 return (false);
775 }
776
777 //png error stuff, not sure libpng man suggests this.
778 if (setjmp(png_jmpbuf(png_ptr))) {
779 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
780 fclose(fp);
781 return (false);
782 }
783
784 //init png reading
785 png_init_io(png_ptr, fp);
786
787 //let libpng know you already read the first 8 bytes
788 png_set_sig_bytes(png_ptr, 8);
789
790 // read all the info up to the image data
791 png_read_info(png_ptr, info_ptr);
792
793 // get info about png
794 png_get_IHDR(png_ptr, info_ptr, &twidth, &theight, &bit_depth, &color_type,
795 NULL, NULL, NULL);
796
797 //update width and height based on png info
798 *width = twidth;
799 *height = theight;
800
801 // Require that incoming texture be 8bits per color component
802 // and 4 components (RGBA).
803 if (png_get_bit_depth(png_ptr, info_ptr) != 8 ||
804 png_get_channels(png_ptr, info_ptr) != 4) {
805 return false;
806 }
807
808 if (rgba_data == NULL) {
809 // If data pointer is null, we just want the width & height
810 // clean up memory and close stuff
811 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
812 fclose(fp);
813
814 return true;
815 }
816
817 // Update the png info struct.
818 png_read_update_info(png_ptr, info_ptr);
819
820 // Row size in bytes.
821 rowbytes = png_get_rowbytes(png_ptr, info_ptr);
822
823 // Allocate the image_data as a big block, to be given to opengl
824 image_data = (png_byte *)malloc(rowbytes * theight * sizeof(png_byte));
825 if (!image_data) {
826 //clean up memory and close stuff
827 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
828 fclose(fp);
829 return false;
830 }
831
832 // row_pointers is for pointing to image_data for reading the png with libpng
833 row_pointers = (png_bytep *)malloc(theight * sizeof(png_bytep));
834 if (!row_pointers) {
835 //clean up memory and close stuff
836 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
837 // delete[] image_data;
838 fclose(fp);
839 return false;
840 }
841 // set the individual row_pointers to point at the correct offsets of image_data
842 for (i = 0; i < theight; ++i)
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700843 row_pointers[theight - 1 - i] = rgba_data + i * layout->rowPitch;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600844
845 // read the png into image_data through row_pointers
846 png_read_image(png_ptr, row_pointers);
847
848 // clean up memory and close stuff
849 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
850 free(row_pointers);
851 free(image_data);
852 fclose(fp);
853
854 return true;
855}
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600856
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700857static void demo_prepare_texture_image(struct demo *demo,
858 const char *filename,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600859 struct texture_object *tex_obj,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600860 VkImageTiling tiling,
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -0600861 VkImageUsageFlags usage,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600862 VkFlags mem_props)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600863{
Mike Stroyan8b89e072015-06-15 14:21:03 -0600864 const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600865 int32_t tex_width;
866 int32_t tex_height;
Tony Barbourfdc2d352015-04-22 09:02:32 -0600867 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700868
David Pinedo30bd71d2015-04-23 08:16:57 -0600869 if (!loadTexture(filename, NULL, NULL, &tex_width, &tex_height))
870 {
871 printf("Failed to load textures\n");
872 fflush(stdout);
873 exit(1);
874 }
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700875
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600876 tex_obj->tex_width = tex_width;
877 tex_obj->tex_height = tex_height;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700878
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600879 const VkImageCreateInfo image_create_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600880 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700881 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -0600882 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700883 .format = tex_format,
884 .extent = { tex_width, tex_height, 1 },
885 .mipLevels = 1,
886 .arraySize = 1,
887 .samples = 1,
888 .tiling = tiling,
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -0600889 .usage = usage,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700890 .flags = 0,
891 };
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -0600892 VkMemoryAllocInfo mem_alloc = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600893 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
Mark Lobodzinskia6907f72015-04-16 08:52:00 -0500894 .pNext = NULL,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700895 .allocationSize = 0,
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600896 .memoryTypeIndex = 0,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700897 };
898
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500899 VkMemoryRequirements mem_reqs;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700900
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600901 err = vkCreateImage(demo->device, &image_create_info,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600902 &tex_obj->image);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700903 assert(!err);
904
Tony Barbour155cce82015-07-03 10:33:54 -0600905 err = vkGetImageMemoryRequirements(demo->device, tex_obj->image, &mem_reqs);
Tony Barbourecf1b2b2015-06-24 16:06:58 -0600906 assert(!err);
Piers Daniell735ee532015-02-23 16:23:13 -0700907
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500908 mem_alloc.allocationSize = mem_reqs.size;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700909
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600910 err = memory_type_from_properties(demo, mem_reqs.memoryTypeBits, mem_props, &mem_alloc.memoryTypeIndex);
911 assert(!err);
912
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500913 /* allocate memory */
914 err = vkAllocMemory(demo->device, &mem_alloc,
915 &(tex_obj->mem));
916 assert(!err);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700917
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500918 /* bind memory */
Tony Barbour155cce82015-07-03 10:33:54 -0600919 err = vkBindImageMemory(demo->device, tex_obj->image,
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500920 tex_obj->mem, 0);
921 assert(!err);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700922
Tony Barbour72304ef2015-04-16 15:59:00 -0600923 if (mem_props & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600924 const VkImageSubresource subres = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600925 .aspect = VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700926 .mipLevel = 0,
927 .arraySlice = 0,
928 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600929 VkSubresourceLayout layout;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700930 void *data;
931
Tony Barbourecf1b2b2015-06-24 16:06:58 -0600932 err = vkGetImageSubresourceLayout(demo->device, tex_obj->image, &subres, &layout);
933 assert(!err);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700934
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500935 err = vkMapMemory(demo->device, tex_obj->mem, 0, 0, 0, &data);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700936 assert(!err);
937
938 if (!loadTexture(filename, data, &layout, &tex_width, &tex_height)) {
939 fprintf(stderr, "Error loading texture: %s\n", filename);
940 }
941
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500942 err = vkUnmapMemory(demo->device, tex_obj->mem);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700943 assert(!err);
944 }
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600945
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600946 tex_obj->imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600947 demo_set_image_layout(demo, tex_obj->image,
malnasseeb25d3f2015-06-03 17:28:38 -0400948 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600949 VK_IMAGE_LAYOUT_UNDEFINED,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600950 tex_obj->imageLayout);
951 /* 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 -0700952}
953
Mark Lobodzinskida9b1092015-04-16 11:44:05 -0500954static void demo_destroy_texture_image(struct demo *demo, struct texture_object *tex_objs)
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700955{
956 /* clean up staging resources */
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500957 vkFreeMemory(demo->device, tex_objs->mem);
Tony Barbour155cce82015-07-03 10:33:54 -0600958 vkDestroyImage(demo->device, tex_objs->image);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700959}
960
961static void demo_prepare_textures(struct demo *demo)
962{
Tony Barbour72304ef2015-04-16 15:59:00 -0600963 const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600964 VkFormatProperties props;
Tony Barbourfdc2d352015-04-22 09:02:32 -0600965 VkResult U_ASSERT_ONLY err;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600966 uint32_t i;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600967
Chris Forbesf576a3e2015-06-21 22:55:02 +1200968 err = vkGetPhysicalDeviceFormatInfo(demo->gpu, tex_format, &props);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700969 assert(!err);
970
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600971 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700972
FslNopper6a7e50e2015-05-06 21:42:01 +0200973 if ((props.linearTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) && !demo->use_staging_buffer) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700974 /* Device can texture using linear textures */
975 demo_prepare_texture_image(demo, tex_files[i], &demo->textures[i],
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -0600976 VK_IMAGE_TILING_LINEAR, VK_IMAGE_USAGE_SAMPLED_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
Tony Barbour72304ef2015-04-16 15:59:00 -0600977 } else if (props.optimalTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700978 /* Must use staging buffer to copy linear texture to optimized */
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600979 struct texture_object staging_texture;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700980
981 memset(&staging_texture, 0, sizeof(staging_texture));
982 demo_prepare_texture_image(demo, tex_files[i], &staging_texture,
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -0600983 VK_IMAGE_TILING_LINEAR, VK_IMAGE_USAGE_TRANSFER_SOURCE_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700984
985 demo_prepare_texture_image(demo, tex_files[i], &demo->textures[i],
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -0600986 VK_IMAGE_TILING_OPTIMAL,
987 (VK_IMAGE_USAGE_TRANSFER_DESTINATION_BIT | VK_IMAGE_USAGE_SAMPLED_BIT),
988 VK_MEMORY_PROPERTY_DEVICE_ONLY);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700989
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600990 demo_set_image_layout(demo, staging_texture.image,
malnasseeb25d3f2015-06-03 17:28:38 -0400991 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600992 staging_texture.imageLayout,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600993 VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700994
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600995 demo_set_image_layout(demo, demo->textures[i].image,
malnasseeb25d3f2015-06-03 17:28:38 -0400996 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600997 demo->textures[i].imageLayout,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600998 VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700999
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001000 VkImageCopy copy_region = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001001 .srcSubresource = { VK_IMAGE_ASPECT_COLOR, 0, 0 },
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001002 .srcOffset = { 0, 0, 0 },
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001003 .destSubresource = { VK_IMAGE_ASPECT_COLOR, 0, 0 },
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001004 .destOffset = { 0, 0, 0 },
1005 .extent = { staging_texture.tex_width, staging_texture.tex_height, 1 },
1006 };
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001007 vkCmdCopyImage(demo->cmd,
1008 staging_texture.image, VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL,
1009 demo->textures[i].image, VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL,
Courtney Goeltzenleuchter8e89a312015-03-25 11:25:10 -06001010 1, &copy_region);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001011
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001012 demo_set_image_layout(demo, demo->textures[i].image,
malnasseeb25d3f2015-06-03 17:28:38 -04001013 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001014 VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001015 demo->textures[i].imageLayout);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001016
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001017 demo_flush_init_cmd(demo);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001018
Courtney Goeltzenleuchterf3aeb2b2015-04-21 09:30:03 -06001019 demo_destroy_texture_image(demo, &staging_texture);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001020 } else {
Mike Stroyan8b89e072015-06-15 14:21:03 -06001021 /* Can't support VK_FORMAT_R8G8B8A8_UNORM !? */
1022 assert(!"No support for R8G8B8A8_UNORM as texture image format");
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001023 }
1024
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001025 const VkSamplerCreateInfo sampler = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001026 .sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001027 .pNext = NULL,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001028 .magFilter = VK_TEX_FILTER_NEAREST,
1029 .minFilter = VK_TEX_FILTER_NEAREST,
Tony Barbour72304ef2015-04-16 15:59:00 -06001030 .mipMode = VK_TEX_MIPMAP_MODE_BASE,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001031 .addressU = VK_TEX_ADDRESS_CLAMP,
1032 .addressV = VK_TEX_ADDRESS_CLAMP,
1033 .addressW = VK_TEX_ADDRESS_CLAMP,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001034 .mipLodBias = 0.0f,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001035 .maxAnisotropy = 1,
Tony Barbour72304ef2015-04-16 15:59:00 -06001036 .compareOp = VK_COMPARE_OP_NEVER,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001037 .minLod = 0.0f,
1038 .maxLod = 0.0f,
Tony Barbourcb530c72015-06-25 16:56:44 -06001039 .borderColor = VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001040 };
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001041
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001042 VkImageViewCreateInfo view = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001043 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001044 .pNext = NULL,
Tony Barbour155cce82015-07-03 10:33:54 -06001045 .image.handle = VK_NULL_HANDLE,
Tony Barbour72304ef2015-04-16 15:59:00 -06001046 .viewType = VK_IMAGE_VIEW_TYPE_2D,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001047 .format = tex_format,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001048 .channels = { VK_CHANNEL_SWIZZLE_R,
1049 VK_CHANNEL_SWIZZLE_G,
1050 VK_CHANNEL_SWIZZLE_B,
1051 VK_CHANNEL_SWIZZLE_A, },
1052 .subresourceRange = { VK_IMAGE_ASPECT_COLOR, 0, 1, 0, 1 },
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001053 };
Jon Ashburnb2a66652015-01-16 09:37:43 -07001054
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001055 /* create sampler */
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001056 err = vkCreateSampler(demo->device, &sampler,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001057 &demo->textures[i].sampler);
1058 assert(!err);
1059
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001060 /* create image view */
1061 view.image = demo->textures[i].image;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001062 err = vkCreateImageView(demo->device, &view,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001063 &demo->textures[i].view);
1064 assert(!err);
1065 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001066}
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001067
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001068void demo_prepare_cube_data_buffer(struct demo *demo)
1069{
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -06001070 VkBufferCreateInfo buf_info;
1071 VkBufferViewCreateInfo view_info;
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -06001072 VkMemoryAllocInfo alloc_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001073 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
Mark Lobodzinskia6907f72015-04-16 08:52:00 -05001074 .pNext = NULL,
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001075 .allocationSize = 0,
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06001076 .memoryTypeIndex = 0,
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001077 };
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001078 VkMemoryRequirements mem_reqs;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001079 size_t mem_reqs_size = sizeof(VkMemoryRequirements);
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001080 uint8_t *pData;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001081 int i;
1082 mat4x4 MVP, VP;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001083 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001084 struct vktexcube_vs_uniform data;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001085
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001086 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001087 mat4x4_mul(MVP, VP, demo->model_matrix);
1088 memcpy(data.mvp, MVP, sizeof(MVP));
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001089// dumpMatrix("MVP", MVP);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001090
1091 for (i=0; i<12*3; i++) {
1092 data.position[i][0] = g_vertex_buffer_data[i*3];
1093 data.position[i][1] = g_vertex_buffer_data[i*3+1];
1094 data.position[i][2] = g_vertex_buffer_data[i*3+2];
1095 data.position[i][3] = 1.0f;
1096 data.attr[i][0] = g_uv_buffer_data[2*i];
1097 data.attr[i][1] = g_uv_buffer_data[2*i + 1];
1098 data.attr[i][2] = 0;
1099 data.attr[i][3] = 0;
1100 }
1101
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001102 memset(&buf_info, 0, sizeof(buf_info));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001103 buf_info.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -06001104 buf_info.usage = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001105 err = vkCreateBuffer(demo->device, &buf_info, &demo->uniform_data.buf);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001106 assert(!err);
1107
Tony Barbour155cce82015-07-03 10:33:54 -06001108 err = vkGetBufferMemoryRequirements(demo->device, demo->uniform_data.buf, &mem_reqs);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001109 assert(!err && mem_reqs_size == sizeof(mem_reqs));
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001110
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001111 alloc_info.allocationSize = mem_reqs.size;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06001112 err = memory_type_from_properties(demo,
1113 mem_reqs.memoryTypeBits,
1114 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT,
1115 &alloc_info.memoryTypeIndex);
1116 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001117
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001118 err = vkAllocMemory(demo->device, &alloc_info, &(demo->uniform_data.mem));
1119 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001120
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001121 err = vkMapMemory(demo->device, demo->uniform_data.mem, 0, 0, 0, (void **) &pData);
1122 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001123
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001124 memcpy(pData, &data, sizeof data);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001125
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001126 err = vkUnmapMemory(demo->device, demo->uniform_data.mem);
1127 assert(!err);
1128
Tony Barbour155cce82015-07-03 10:33:54 -06001129 err = vkBindBufferMemory(demo->device,
1130 demo->uniform_data.buf,
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001131 demo->uniform_data.mem, 0);
1132 assert(!err);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001133
1134 memset(&view_info, 0, sizeof(view_info));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001135 view_info.sType = VK_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO;
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001136 view_info.buffer = demo->uniform_data.buf;
Tony Barbour72304ef2015-04-16 15:59:00 -06001137 view_info.viewType = VK_BUFFER_VIEW_TYPE_RAW;
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001138 view_info.offset = 0;
1139 view_info.range = sizeof(data);
1140
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001141 err = vkCreateBufferView(demo->device, &view_info, &demo->uniform_data.view);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001142 assert(!err);
1143
Chia-I Wuae721ba2015-05-25 16:27:55 +08001144 demo->uniform_data.desc.bufferView = demo->uniform_data.view;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001145}
1146
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001147static void demo_prepare_descriptor_layout(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001148{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001149 const VkDescriptorSetLayoutBinding layout_bindings[2] = {
Chia-I Wua2aa8632015-03-26 15:04:41 +08001150 [0] = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001151 .descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
Chia-I Wu6d29a412015-05-25 16:22:52 +08001152 .arraySize = 1,
Tony Barbour72304ef2015-04-16 15:59:00 -06001153 .stageFlags = VK_SHADER_STAGE_VERTEX_BIT,
Chia-I Wu91e8e212015-03-27 12:56:09 +08001154 .pImmutableSamplers = NULL,
Chia-I Wua2aa8632015-03-26 15:04:41 +08001155 },
1156 [1] = {
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -06001157 .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
Chia-I Wu6d29a412015-05-25 16:22:52 +08001158 .arraySize = DEMO_TEXTURE_COUNT,
Tony Barbour72304ef2015-04-16 15:59:00 -06001159 .stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT,
Chia-I Wu91e8e212015-03-27 12:56:09 +08001160 .pImmutableSamplers = NULL,
Chia-I Wua2aa8632015-03-26 15:04:41 +08001161 },
1162 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001163 const VkDescriptorSetLayoutCreateInfo descriptor_layout = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001164 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001165 .pNext = NULL,
Chia-I Wua2aa8632015-03-26 15:04:41 +08001166 .count = 2,
1167 .pBinding = layout_bindings,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001168 };
Tony Barbourfdc2d352015-04-22 09:02:32 -06001169 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001170
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001171 err = vkCreateDescriptorSetLayout(demo->device,
Chia-I Wub58c24a2015-03-26 15:27:55 +08001172 &descriptor_layout, &demo->desc_layout);
1173 assert(!err);
1174
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001175 const VkPipelineLayoutCreateInfo pPipelineLayoutCreateInfo = {
1176 .sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
1177 .pNext = NULL,
1178 .descriptorSetCount = 1,
1179 .pSetLayouts = &demo->desc_layout,
1180 };
1181
1182 err = vkCreatePipelineLayout(demo->device,
1183 &pPipelineLayoutCreateInfo,
1184 &demo->pipeline_layout);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001185 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001186}
1187
Chia-I Wuf973e322015-07-08 13:34:24 +08001188static void demo_prepare_render_pass(struct demo *demo)
1189{
Chia-I Wua74c5b22015-07-07 11:50:03 +08001190 const VkAttachmentDescription attachments[2] = {
1191 [0] = {
1192 .sType = VK_STRUCTURE_TYPE_ATTACHMENT_DESCRIPTION,
1193 .pNext = NULL,
1194 .format = demo->format,
1195 .samples = 1,
1196 .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
1197 .storeOp = VK_ATTACHMENT_STORE_OP_STORE,
1198 .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
1199 .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1200 .initialLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
1201 .finalLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
1202 },
1203 [1] = {
1204 .sType = VK_STRUCTURE_TYPE_ATTACHMENT_DESCRIPTION,
1205 .pNext = NULL,
1206 .format = demo->depth.format,
1207 .samples = 1,
1208 .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
1209 .storeOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1210 .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
1211 .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1212 .initialLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1213 .finalLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1214 },
Chia-I Wuf973e322015-07-08 13:34:24 +08001215 };
Chia-I Wua74c5b22015-07-07 11:50:03 +08001216 const VkAttachmentReference color_reference = {
1217 .attachment = 0,
1218 .layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
1219 };
1220 const VkSubpassDescription subpass = {
1221 .sType = VK_STRUCTURE_TYPE_SUBPASS_DESCRIPTION,
1222 .pNext = NULL,
1223 .pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS,
1224 .flags = 0,
1225 .inputCount = 0,
1226 .inputAttachments = NULL,
1227 .colorCount = 1,
1228 .colorAttachments = &color_reference,
1229 .resolveAttachments = NULL,
1230 .depthStencilAttachment = {
1231 .attachment = 1,
1232 .layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1233 },
1234 .preserveCount = 0,
1235 .preserveAttachments = NULL,
1236 };
Chia-I Wuf973e322015-07-08 13:34:24 +08001237 const VkRenderPassCreateInfo rp_info = {
1238 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO,
1239 .pNext = NULL,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001240 .attachmentCount = 2,
1241 .pAttachments = attachments,
1242 .subpassCount = 1,
1243 .pSubpasses = &subpass,
1244 .dependencyCount = 0,
1245 .pDependencies = NULL,
Chia-I Wuf973e322015-07-08 13:34:24 +08001246 };
Chia-I Wua74c5b22015-07-07 11:50:03 +08001247 VkResult U_ASSERT_ONLY err;
Chia-I Wuf973e322015-07-08 13:34:24 +08001248
1249 err = vkCreateRenderPass(demo->device, &rp_info, &demo->render_pass);
1250 assert(!err);
1251}
1252
Tobin Ehlisd1f17ac2015-07-02 11:02:49 -06001253static VkShader demo_prepare_shader(struct demo* demo,
Tony Barbour72304ef2015-04-16 15:59:00 -06001254 VkShaderStage stage,
Tobin Ehlisd1f17ac2015-07-02 11:02:49 -06001255 const void* code,
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001256 size_t size)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001257{
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001258 VkShaderModuleCreateInfo moduleCreateInfo;
1259 VkShaderCreateInfo shaderCreateInfo;
1260 VkShaderModule shaderModule;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001261 VkShader shader;
1262 VkResult err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001263
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001264
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001265 moduleCreateInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
1266 moduleCreateInfo.pNext = NULL;
1267
1268 shaderCreateInfo.sType = VK_STRUCTURE_TYPE_SHADER_CREATE_INFO;
1269 shaderCreateInfo.pNext = NULL;
Tobin Ehlisd1f17ac2015-07-02 11:02:49 -06001270 shaderCreateInfo.pName = "main";
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001271
Cody Northrop1fedb212015-05-28 11:27:16 -06001272 if (!demo->use_glsl) {
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001273 moduleCreateInfo.codeSize = size;
1274 moduleCreateInfo.pCode = code;
1275 moduleCreateInfo.flags = 0;
1276 err = vkCreateShaderModule(demo->device, &moduleCreateInfo, &shaderModule);
Cody Northrop1fedb212015-05-28 11:27:16 -06001277 if (err) {
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001278 free((void *) moduleCreateInfo.pCode);
Cody Northrop1fedb212015-05-28 11:27:16 -06001279 }
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001280
1281 shaderCreateInfo.flags = 0;
1282 shaderCreateInfo.module = shaderModule;
1283 err = vkCreateShader(demo->device, &shaderCreateInfo, &shader);
Cody Northrop1fedb212015-05-28 11:27:16 -06001284 } else {
1285 // Create fake SPV structure to feed GLSL
1286 // to the driver "under the covers"
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001287 moduleCreateInfo.codeSize = 3 * sizeof(uint32_t) + size + 1;
1288 moduleCreateInfo.pCode = malloc(moduleCreateInfo.codeSize);
1289 moduleCreateInfo.flags = 0;
Cody Northrop1fedb212015-05-28 11:27:16 -06001290
1291 /* try version 0 first: VkShaderStage followed by GLSL */
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001292 ((uint32_t *) moduleCreateInfo.pCode)[0] = ICD_SPV_MAGIC;
1293 ((uint32_t *) moduleCreateInfo.pCode)[1] = 0;
1294 ((uint32_t *) moduleCreateInfo.pCode)[2] = stage;
1295 memcpy(((uint32_t *) moduleCreateInfo.pCode + 3), code, size + 1);
Cody Northrop1fedb212015-05-28 11:27:16 -06001296
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001297 err = vkCreateShaderModule(demo->device, &moduleCreateInfo, &shaderModule);
Cody Northrop1fedb212015-05-28 11:27:16 -06001298 if (err) {
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001299 free((void *) moduleCreateInfo.pCode);
Cody Northrop1fedb212015-05-28 11:27:16 -06001300 }
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001301
1302 shaderCreateInfo.flags = 0;
1303 shaderCreateInfo.module = shaderModule;
1304 err = vkCreateShader(demo->device, &shaderCreateInfo, &shader);
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001305 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001306 return shader;
1307}
1308
Cody Northrop8b12a1f2015-03-17 15:55:58 -06001309char *demo_read_spv(const char *filename, size_t *psize)
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001310{
1311 long int size;
Tony Barbourf9921732015-04-22 11:36:22 -06001312 size_t U_ASSERT_ONLY retval;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001313 void *shader_code;
1314
1315 FILE *fp = fopen(filename, "rb");
1316 if (!fp) return NULL;
1317
1318 fseek(fp, 0L, SEEK_END);
1319 size = ftell(fp);
1320
1321 fseek(fp, 0L, SEEK_SET);
1322
1323 shader_code = malloc(size);
Tony Barbourfdc2d352015-04-22 09:02:32 -06001324 retval = fread(shader_code, size, 1, fp);
1325 assert(retval == 1);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001326
1327 *psize = size;
1328
1329 return shader_code;
1330}
1331
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001332static VkShader demo_prepare_vs(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001333{
Cody Northrop1fedb212015-05-28 11:27:16 -06001334 if (!demo->use_glsl) {
1335 void *vertShaderCode;
1336 size_t size;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001337
Cody Northrop1fedb212015-05-28 11:27:16 -06001338 vertShaderCode = demo_read_spv("cube-vert.spv", &size);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001339
Cody Northrop1fedb212015-05-28 11:27:16 -06001340 return demo_prepare_shader(demo, VK_SHADER_STAGE_VERTEX,
1341 vertShaderCode, size);
1342 } else {
1343 static const char *vertShaderText =
1344 "#version 140\n"
1345 "#extension GL_ARB_separate_shader_objects : enable\n"
1346 "#extension GL_ARB_shading_language_420pack : enable\n"
1347 "\n"
1348 "layout(binding = 0) uniform buf {\n"
1349 " mat4 MVP;\n"
1350 " vec4 position[12*3];\n"
1351 " vec4 attr[12*3];\n"
1352 "} ubuf;\n"
1353 "\n"
1354 "layout (location = 0) out vec4 texcoord;\n"
1355 "\n"
1356 "void main() \n"
1357 "{\n"
1358 " texcoord = ubuf.attr[gl_VertexID];\n"
1359 " gl_Position = ubuf.MVP * ubuf.position[gl_VertexID];\n"
1360 "\n"
1361 " // GL->VK conventions\n"
1362 " gl_Position.y = -gl_Position.y;\n"
1363 " gl_Position.z = (gl_Position.z + gl_Position.w) / 2.0;\n"
1364 "}\n";
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001365
Cody Northrop1fedb212015-05-28 11:27:16 -06001366 return demo_prepare_shader(demo, VK_SHADER_STAGE_VERTEX,
1367 (const void *) vertShaderText,
1368 strlen(vertShaderText));
1369 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001370}
1371
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001372static VkShader demo_prepare_fs(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001373{
Cody Northrop1fedb212015-05-28 11:27:16 -06001374 if (!demo->use_glsl) {
1375 void *fragShaderCode;
1376 size_t size;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001377
Cody Northrop1fedb212015-05-28 11:27:16 -06001378 fragShaderCode = demo_read_spv("cube-frag.spv", &size);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001379
Cody Northrop1fedb212015-05-28 11:27:16 -06001380 return demo_prepare_shader(demo, VK_SHADER_STAGE_FRAGMENT,
1381 fragShaderCode, size);
1382 } else {
1383 static const char *fragShaderText =
1384 "#version 140\n"
1385 "#extension GL_ARB_separate_shader_objects : enable\n"
1386 "#extension GL_ARB_shading_language_420pack : enable\n"
1387 "layout (binding = 1) uniform sampler2D tex;\n"
1388 "\n"
1389 "layout (location = 0) in vec4 texcoord;\n"
1390 "layout (location = 0) out vec4 uFragColor;\n"
1391 "void main() {\n"
1392 " uFragColor = texture(tex, texcoord.xy);\n"
1393 "}\n";
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001394
Cody Northrop1fedb212015-05-28 11:27:16 -06001395 return demo_prepare_shader(demo, VK_SHADER_STAGE_FRAGMENT,
1396 (const void *) fragShaderText,
1397 strlen(fragShaderText));
1398 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001399}
1400
1401static void demo_prepare_pipeline(struct demo *demo)
1402{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001403 VkGraphicsPipelineCreateInfo pipeline;
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001404 VkPipelineCacheCreateInfo pipelineCache;
Tony Barbour194bf282015-07-10 15:29:03 -06001405 VkPipelineInputAssemblyStateCreateInfo ia;
1406 VkPipelineRasterStateCreateInfo rs;
1407 VkPipelineColorBlendStateCreateInfo cb;
1408 VkPipelineDepthStencilStateCreateInfo ds;
1409 VkPipelineViewportStateCreateInfo vp;
1410 VkPipelineMultisampleStateCreateInfo ms;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001411 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001412
1413 memset(&pipeline, 0, sizeof(pipeline));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001414 pipeline.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001415 pipeline.layout = demo->pipeline_layout;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001416
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001417 memset(&ia, 0, sizeof(ia));
Tony Barbour194bf282015-07-10 15:29:03 -06001418 ia.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06001419 ia.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001420
1421 memset(&rs, 0, sizeof(rs));
Tony Barbour194bf282015-07-10 15:29:03 -06001422 rs.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTER_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06001423 rs.fillMode = VK_FILL_MODE_SOLID;
1424 rs.cullMode = VK_CULL_MODE_BACK;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001425 rs.frontFace = VK_FRONT_FACE_CCW;
Chia-I Wubb67e6e2015-04-22 14:20:52 +08001426 rs.depthClipEnable = VK_TRUE;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001427
1428 memset(&cb, 0, sizeof(cb));
Tony Barbour194bf282015-07-10 15:29:03 -06001429 cb.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
1430 VkPipelineColorBlendAttachmentState att_state[1];
Tony Barbour29645d02015-01-16 14:27:35 -07001431 memset(att_state, 0, sizeof(att_state));
Tony Barbour29645d02015-01-16 14:27:35 -07001432 att_state[0].channelWriteMask = 0xf;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001433 att_state[0].blendEnable = VK_FALSE;
Tony Barbour29645d02015-01-16 14:27:35 -07001434 cb.attachmentCount = 1;
1435 cb.pAttachments = att_state;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001436
Tony Barbour29645d02015-01-16 14:27:35 -07001437 memset(&vp, 0, sizeof(vp));
Tony Barbour194bf282015-07-10 15:29:03 -06001438 vp.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06001439 vp.viewportCount = 1;
Tony Barbour29645d02015-01-16 14:27:35 -07001440
1441 memset(&ds, 0, sizeof(ds));
Tony Barbour194bf282015-07-10 15:29:03 -06001442 ds.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001443 ds.depthTestEnable = VK_TRUE;
1444 ds.depthWriteEnable = VK_TRUE;
Tony Barbour72304ef2015-04-16 15:59:00 -06001445 ds.depthCompareOp = VK_COMPARE_OP_LESS_EQUAL;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001446 ds.depthBoundsEnable = VK_FALSE;
1447 ds.back.stencilFailOp = VK_STENCIL_OP_KEEP;
1448 ds.back.stencilPassOp = VK_STENCIL_OP_KEEP;
Tony Barbour72304ef2015-04-16 15:59:00 -06001449 ds.back.stencilCompareOp = VK_COMPARE_OP_ALWAYS;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001450 ds.stencilTestEnable = VK_FALSE;
Tony Barbour29645d02015-01-16 14:27:35 -07001451 ds.front = ds.back;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001452
Tony Barbour29645d02015-01-16 14:27:35 -07001453 memset(&ms, 0, sizeof(ms));
Tony Barbour194bf282015-07-10 15:29:03 -06001454 ms.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
Tony Barbour29645d02015-01-16 14:27:35 -07001455 ms.sampleMask = 1;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001456 ms.multisampleEnable = VK_FALSE;
Tony Barbour3ca22502015-06-26 10:18:34 -06001457 ms.rasterSamples = 1;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001458
Mark Lobodzinski521d7762015-06-23 15:11:57 -06001459 // Two stages: vs and fs
1460 pipeline.stageCount = 2;
1461 VkPipelineShaderStageCreateInfo shaderStages[2];
1462 memset(&shaderStages, 0, 2 * sizeof(VkPipelineShaderStageCreateInfo));
1463
1464 shaderStages[0].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1465 shaderStages[0].stage = VK_SHADER_STAGE_VERTEX;
1466 shaderStages[0].shader = demo_prepare_vs(demo);
1467
1468 shaderStages[1].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1469 shaderStages[1].stage = VK_SHADER_STAGE_FRAGMENT;
1470 shaderStages[1].shader = demo_prepare_fs(demo);
1471
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001472 memset(&pipelineCache, 0, sizeof(pipelineCache));
1473 pipelineCache.sType = VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001474
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001475 err = vkCreatePipelineCache(demo->device, &pipelineCache, &demo->pipelineCache);
1476 assert(!err);
Tony Barbour194bf282015-07-10 15:29:03 -06001477
1478 pipeline.pVertexInputState = NULL;
1479 pipeline.pInputAssemblyState = &ia;
1480 pipeline.pRasterState = &rs;
1481 pipeline.pColorBlendState = &cb;
1482 pipeline.pMultisampleState = &ms;
1483 pipeline.pViewportState = &vp;
1484 pipeline.pDepthStencilState = &ds;
1485 pipeline.pStages = shaderStages;
1486
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001487 err = vkCreateGraphicsPipelines(demo->device, demo->pipelineCache, 1, &pipeline, &demo->pipeline);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001488 assert(!err);
1489
Mark Lobodzinski521d7762015-06-23 15:11:57 -06001490 for (uint32_t i = 0; i < pipeline.stageCount; i++) {
Tony Barbour155cce82015-07-03 10:33:54 -06001491 vkDestroyShader(demo->device, shaderStages[i].shader);
Mark Lobodzinski521d7762015-06-23 15:11:57 -06001492 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001493}
1494
1495static void demo_prepare_dynamic_states(struct demo *demo)
1496{
Tony Barbour155cce82015-07-03 10:33:54 -06001497 VkDynamicViewportStateCreateInfo viewport_create;
1498 VkDynamicRasterStateCreateInfo raster;
1499 VkDynamicColorBlendStateCreateInfo color_blend;
1500 VkDynamicDepthStencilStateCreateInfo depth_stencil;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001501 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001502
Tony Barbour29645d02015-01-16 14:27:35 -07001503 memset(&viewport_create, 0, sizeof(viewport_create));
Tony Barbour155cce82015-07-03 10:33:54 -06001504 viewport_create.sType = VK_STRUCTURE_TYPE_DYNAMIC_VIEWPORT_STATE_CREATE_INFO;
Courtney Goeltzenleuchter1710d8d2015-02-11 14:13:34 -07001505 viewport_create.viewportAndScissorCount = 1;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001506 VkViewport viewport;
Piers Daniell735ee532015-02-23 16:23:13 -07001507 memset(&viewport, 0, sizeof(viewport));
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001508 viewport.height = (float) demo->height;
1509 viewport.width = (float) demo->width;
1510 viewport.minDepth = (float) 0.0f;
1511 viewport.maxDepth = (float) 1.0f;
Piers Daniell735ee532015-02-23 16:23:13 -07001512 viewport_create.pViewports = &viewport;
Chris Forbesfaa91732015-06-22 17:21:59 +12001513 VkRect2D scissor;
Piers Daniell735ee532015-02-23 16:23:13 -07001514 memset(&scissor, 0, sizeof(scissor));
Courtney Goeltzenleuchter1710d8d2015-02-11 14:13:34 -07001515 scissor.extent.width = demo->width;
1516 scissor.extent.height = demo->height;
1517 scissor.offset.x = 0;
1518 scissor.offset.y = 0;
Courtney Goeltzenleuchter1710d8d2015-02-11 14:13:34 -07001519 viewport_create.pScissors = &scissor;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001520
1521 memset(&raster, 0, sizeof(raster));
Tony Barbour155cce82015-07-03 10:33:54 -06001522 raster.sType = VK_STRUCTURE_TYPE_DYNAMIC_RASTER_STATE_CREATE_INFO;
Piers Daniell735ee532015-02-23 16:23:13 -07001523 raster.lineWidth = 1.0;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001524
1525 memset(&color_blend, 0, sizeof(color_blend));
Tony Barbour155cce82015-07-03 10:33:54 -06001526 color_blend.sType = VK_STRUCTURE_TYPE_DYNAMIC_COLOR_BLEND_STATE_CREATE_INFO;
Piers Daniell735ee532015-02-23 16:23:13 -07001527 color_blend.blendConst[0] = 1.0f;
1528 color_blend.blendConst[1] = 1.0f;
1529 color_blend.blendConst[2] = 1.0f;
1530 color_blend.blendConst[3] = 1.0f;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001531
1532 memset(&depth_stencil, 0, sizeof(depth_stencil));
Tony Barbour155cce82015-07-03 10:33:54 -06001533 depth_stencil.sType = VK_STRUCTURE_TYPE_DYNAMIC_DEPTH_STENCIL_STATE_CREATE_INFO;
Mark Lobodzinski89d32b12015-06-12 11:14:17 -06001534 depth_stencil.minDepthBounds = 0.0f;
1535 depth_stencil.maxDepthBounds = 1.0f;
Tony Barbour29645d02015-01-16 14:27:35 -07001536 depth_stencil.stencilBackRef = 0;
1537 depth_stencil.stencilFrontRef = 0;
1538 depth_stencil.stencilReadMask = 0xff;
1539 depth_stencil.stencilWriteMask = 0xff;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001540
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001541 err = vkCreateDynamicViewportState(demo->device, &viewport_create, &demo->viewport);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001542 assert(!err);
1543
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001544 err = vkCreateDynamicRasterState(demo->device, &raster, &demo->raster);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001545 assert(!err);
1546
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001547 err = vkCreateDynamicColorBlendState(demo->device,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001548 &color_blend, &demo->color_blend);
1549 assert(!err);
1550
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001551 err = vkCreateDynamicDepthStencilState(demo->device,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001552 &depth_stencil, &demo->depth_stencil);
1553 assert(!err);
1554}
1555
Chia-I Wu63ea9262015-03-26 13:14:16 +08001556static void demo_prepare_descriptor_pool(struct demo *demo)
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001557{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001558 const VkDescriptorTypeCount type_counts[2] = {
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001559 [0] = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001560 .type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001561 .count = 1,
1562 },
1563 [1] = {
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -06001564 .type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001565 .count = DEMO_TEXTURE_COUNT,
1566 },
1567 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001568 const VkDescriptorPoolCreateInfo descriptor_pool = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001569 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001570 .pNext = NULL,
1571 .count = 2,
1572 .pTypeCount = type_counts,
1573 };
Tony Barbourfdc2d352015-04-22 09:02:32 -06001574 VkResult U_ASSERT_ONLY err;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001575
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001576 err = vkCreateDescriptorPool(demo->device,
1577 VK_DESCRIPTOR_POOL_USAGE_ONE_SHOT, 1,
Chia-I Wu63ea9262015-03-26 13:14:16 +08001578 &descriptor_pool, &demo->desc_pool);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001579 assert(!err);
1580}
1581
1582static void demo_prepare_descriptor_set(struct demo *demo)
1583{
Chia-I Wuae721ba2015-05-25 16:27:55 +08001584 VkDescriptorInfo tex_descs[DEMO_TEXTURE_COUNT];
1585 VkWriteDescriptorSet writes[2];
Tony Barbourfdc2d352015-04-22 09:02:32 -06001586 VkResult U_ASSERT_ONLY err;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001587 uint32_t count;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001588 uint32_t i;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001589
Mike Stroyanebae8322015-04-17 12:36:38 -06001590 err = vkAllocDescriptorSets(demo->device, demo->desc_pool,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001591 VK_DESCRIPTOR_SET_USAGE_STATIC,
Chia-I Wu87544e72015-02-23 10:41:08 -07001592 1, &demo->desc_layout,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001593 &demo->desc_set, &count);
1594 assert(!err && count == 1);
1595
Chia-I Wuae721ba2015-05-25 16:27:55 +08001596 memset(&tex_descs, 0, sizeof(tex_descs));
1597 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
1598 tex_descs[i].sampler = demo->textures[i].sampler;
1599 tex_descs[i].imageView = demo->textures[i].view;
1600 tex_descs[i].imageLayout = VK_IMAGE_LAYOUT_GENERAL;
1601 }
1602
1603 memset(&writes, 0, sizeof(writes));
1604
1605 writes[0].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
1606 writes[0].destSet = demo->desc_set;
1607 writes[0].count = 1;
1608 writes[0].descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
1609 writes[0].pDescriptors = &demo->uniform_data.desc;
1610
1611 writes[1].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
1612 writes[1].destSet = demo->desc_set;
1613 writes[1].destBinding = 1;
1614 writes[1].count = DEMO_TEXTURE_COUNT;
1615 writes[1].descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
1616 writes[1].pDescriptors = tex_descs;
1617
1618 err = vkUpdateDescriptorSets(demo->device, 2, writes, 0, NULL);
1619 assert(!err);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001620}
1621
Chia-I Wuf973e322015-07-08 13:34:24 +08001622static void demo_prepare_framebuffers(struct demo *demo)
1623{
Chia-I Wua74c5b22015-07-07 11:50:03 +08001624 VkAttachmentBindInfo attachments[2] = {
1625 [0] = {
Tobin Ehlisd415ac32015-07-06 14:02:36 -06001626 .view.handle = VK_NULL_HANDLE,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001627 .layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
1628 },
1629 [1] = {
1630 .view = demo->depth.view,
1631 .layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1632 },
Chia-I Wuf973e322015-07-08 13:34:24 +08001633 };
1634 const VkFramebufferCreateInfo fb_info = {
1635 .sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO,
1636 .pNext = NULL,
Chia-I Wua74c5b22015-07-07 11:50:03 +08001637 .renderPass = demo->render_pass,
1638 .attachmentCount = 2,
1639 .pAttachments = attachments,
Chia-I Wuf973e322015-07-08 13:34:24 +08001640 .width = demo->width,
1641 .height = demo->height,
1642 .layers = 1,
1643 };
1644 VkResult U_ASSERT_ONLY err;
1645 uint32_t i;
1646
1647 for (i = 0; i < DEMO_BUFFER_COUNT; i++) {
Chia-I Wua74c5b22015-07-07 11:50:03 +08001648 attachments[0].view = demo->buffers[i].view;
Chia-I Wuf973e322015-07-08 13:34:24 +08001649 err = vkCreateFramebuffer(demo->device, &fb_info, &demo->framebuffers[i]);
1650 assert(!err);
1651 }
1652}
1653
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001654static void demo_prepare(struct demo *demo)
1655{
Cody Northrop91e221b2015-07-09 18:08:32 -06001656 VkResult U_ASSERT_ONLY err;
1657
1658 const VkCmdPoolCreateInfo cmd_pool_info = {
1659 .sType = VK_STRUCTURE_TYPE_CMD_POOL_CREATE_INFO,
1660 .pNext = NULL,
1661 .queueFamilyIndex = demo->graphics_queue_node_index,
1662 .flags = 0,
1663 };
1664 err = vkCreateCommandPool(demo->device, &cmd_pool_info, &demo->cmd_pool);
1665 assert(!err);
1666
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001667 const VkCmdBufferCreateInfo cmd = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001668 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001669 .pNext = NULL,
Cody Northrop91e221b2015-07-09 18:08:32 -06001670 .cmdPool = demo->cmd_pool,
Chia-I Wu57b23b42015-06-26 15:34:39 +08001671 .level = VK_CMD_BUFFER_LEVEL_PRIMARY,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001672 .flags = 0,
1673 };
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001674
1675 demo_prepare_buffers(demo);
1676 demo_prepare_depth(demo);
1677 demo_prepare_textures(demo);
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001678 demo_prepare_cube_data_buffer(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001679
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001680 demo_prepare_descriptor_layout(demo);
Chia-I Wuf973e322015-07-08 13:34:24 +08001681 demo_prepare_render_pass(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001682 demo_prepare_pipeline(demo);
1683 demo_prepare_dynamic_states(demo);
1684
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001685 for (int i = 0; i < DEMO_BUFFER_COUNT; i++) {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001686 err = vkCreateCommandBuffer(demo->device, &cmd, &demo->buffers[i].cmd);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001687 assert(!err);
1688 }
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001689
Chia-I Wu63ea9262015-03-26 13:14:16 +08001690 demo_prepare_descriptor_pool(demo);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001691 demo_prepare_descriptor_set(demo);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001692
Chia-I Wuf973e322015-07-08 13:34:24 +08001693 demo_prepare_framebuffers(demo);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001694
1695 for (int i = 0; i < DEMO_BUFFER_COUNT; i++) {
1696 demo->current_buffer = i;
1697 demo_draw_build_cmd(demo, demo->buffers[i].cmd);
1698 }
1699
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001700 /*
1701 * Prepare functions above may generate pipeline commands
1702 * that need to be flushed before beginning the render loop.
1703 */
1704 demo_flush_init_cmd(demo);
1705
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001706 demo->current_buffer = 0;
Jon Ashburn8d26c062015-04-24 09:46:24 -07001707 demo->prepared = true;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001708}
1709
David Pinedo2bb7c932015-06-18 17:03:14 -06001710static void demo_cleanup(struct demo *demo)
1711{
1712 uint32_t i;
1713
1714 demo->prepared = false;
1715
Tony Barbour155cce82015-07-03 10:33:54 -06001716 for (i = 0; i < DEMO_BUFFER_COUNT; i++) {
1717 vkDestroyFramebuffer(demo->device, demo->framebuffers[i]);
1718 }
Tony Barbour4efb01f2015-07-10 10:50:45 -06001719 vkFreeDescriptorSets(demo->device, demo->desc_pool, 1, &demo->desc_set);
Tony Barbour155cce82015-07-03 10:33:54 -06001720 vkDestroyDescriptorPool(demo->device, demo->desc_pool);
Chia-I Wuf973e322015-07-08 13:34:24 +08001721
Tony Barbour155cce82015-07-03 10:33:54 -06001722 vkDestroyDynamicViewportState(demo->device, demo->viewport);
1723 vkDestroyDynamicRasterState(demo->device, demo->raster);
1724 vkDestroyDynamicColorBlendState(demo->device, demo->color_blend);
1725 vkDestroyDynamicDepthStencilState(demo->device, demo->depth_stencil);
David Pinedo2bb7c932015-06-18 17:03:14 -06001726
Tony Barbour155cce82015-07-03 10:33:54 -06001727 vkDestroyPipeline(demo->device, demo->pipeline);
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001728 vkDestroyPipelineCache(demo->device, demo->pipelineCache);
Tony Barbour155cce82015-07-03 10:33:54 -06001729 vkDestroyRenderPass(demo->device, demo->render_pass);
1730 vkDestroyPipelineLayout(demo->device, demo->pipeline_layout);
1731 vkDestroyDescriptorSetLayout(demo->device, demo->desc_layout);
David Pinedo2bb7c932015-06-18 17:03:14 -06001732
1733 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Tony Barbour155cce82015-07-03 10:33:54 -06001734 vkDestroyImageView(demo->device, demo->textures[i].view);
1735 vkDestroyImage(demo->device, demo->textures[i].image);
David Pinedo2bb7c932015-06-18 17:03:14 -06001736 vkFreeMemory(demo->device, demo->textures[i].mem);
Tony Barbour155cce82015-07-03 10:33:54 -06001737 vkDestroySampler(demo->device, demo->textures[i].sampler);
David Pinedo2bb7c932015-06-18 17:03:14 -06001738 }
1739 demo->fpDestroySwapChainWSI(demo->swap_chain);
1740
Tony Barbour155cce82015-07-03 10:33:54 -06001741 vkDestroyAttachmentView(demo->device, demo->depth.view);
1742 vkDestroyImage(demo->device, demo->depth.image);
David Pinedo2bb7c932015-06-18 17:03:14 -06001743 vkFreeMemory(demo->device, demo->depth.mem);
1744
Tony Barbour155cce82015-07-03 10:33:54 -06001745 vkDestroyBufferView(demo->device, demo->uniform_data.view);
1746 vkDestroyBuffer(demo->device, demo->uniform_data.buf);
David Pinedo2bb7c932015-06-18 17:03:14 -06001747 vkFreeMemory(demo->device, demo->uniform_data.mem);
1748
1749 for (i = 0; i < DEMO_BUFFER_COUNT; i++) {
Tony Barbour155cce82015-07-03 10:33:54 -06001750 vkDestroyAttachmentView(demo->device, demo->buffers[i].view);
1751 vkDestroyCommandBuffer(demo->device, demo->buffers[i].cmd);
David Pinedo2bb7c932015-06-18 17:03:14 -06001752 }
1753
Cody Northrop91e221b2015-07-09 18:08:32 -06001754 vkDestroyCommandPool(demo->device, demo->cmd_pool);
David Pinedo2bb7c932015-06-18 17:03:14 -06001755 vkDestroyDevice(demo->device);
Tony Barbourd70b42c2015-06-30 14:14:19 -06001756 if (demo->validate) {
1757 demo->dbgDestroyMsgCallback(demo->inst, demo->msg_callback);
1758 }
David Pinedo2bb7c932015-06-18 17:03:14 -06001759 vkDestroyInstance(demo->inst);
1760
1761#ifndef _WIN32
1762 xcb_destroy_window(demo->connection, demo->window);
1763 xcb_disconnect(demo->connection);
1764#endif // _WIN32
1765}
1766
1767// On MS-Windows, make this a global, so it's available to WndProc()
1768struct demo demo;
1769
Ian Elliott639ca472015-04-16 15:23:05 -06001770#ifdef _WIN32
1771static void demo_run(struct demo *demo)
1772{
Courtney Goeltzenleuchterb7e22702015-04-27 14:56:34 -06001773 if (!demo->prepared)
1774 return;
Ian Elliott639ca472015-04-16 15:23:05 -06001775 // Wait for work to finish before updating MVP.
1776 vkDeviceWaitIdle(demo->device);
1777 demo_update_data_buffer(demo);
1778
1779 demo_draw(demo);
1780
1781 // Wait for work to finish before updating MVP.
1782 vkDeviceWaitIdle(demo->device);
Ian Elliott639ca472015-04-16 15:23:05 -06001783
David Pinedo2bb7c932015-06-18 17:03:14 -06001784 demo->curFrame++;
1785
1786 if (demo->frameCount != INT_MAX && demo->curFrame == demo->frameCount)
1787 {
1788 demo->quit=true;
1789 demo_cleanup(demo);
1790 ExitProcess(0);
1791 }
1792
1793}
Ian Elliott639ca472015-04-16 15:23:05 -06001794
1795// MS-Windows event handling function:
1796LRESULT CALLBACK WndProc(HWND hWnd,
1797 UINT uMsg,
1798 WPARAM wParam,
1799 LPARAM lParam)
1800{
Ian Elliott639ca472015-04-16 15:23:05 -06001801 switch(uMsg)
1802 {
Ian Elliott639ca472015-04-16 15:23:05 -06001803 case WM_CLOSE:
1804 PostQuitMessage(0);
Tony Barbour3dddd5d2015-04-29 16:19:20 -06001805 break;
Ian Elliott639ca472015-04-16 15:23:05 -06001806 case WM_PAINT:
1807 demo_run(&demo);
1808 return 0;
1809 default:
1810 break;
1811 }
1812 return (DefWindowProc(hWnd, uMsg, wParam, lParam));
1813}
1814
1815static void demo_create_window(struct demo *demo)
1816{
1817 WNDCLASSEX win_class;
1818
1819 // Initialize the window class structure:
1820 win_class.cbSize = sizeof(WNDCLASSEX);
1821 win_class.style = CS_HREDRAW | CS_VREDRAW;
1822 win_class.lpfnWndProc = WndProc;
1823 win_class.cbClsExtra = 0;
1824 win_class.cbWndExtra = 0;
1825 win_class.hInstance = demo->connection; // hInstance
1826 win_class.hIcon = LoadIcon(NULL, IDI_APPLICATION);
1827 win_class.hCursor = LoadCursor(NULL, IDC_ARROW);
1828 win_class.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
1829 win_class.lpszMenuName = NULL;
1830 win_class.lpszClassName = demo->name;
1831 win_class.hIconSm = LoadIcon(NULL, IDI_WINLOGO);
1832 // Register window class:
1833 if (!RegisterClassEx(&win_class)) {
1834 // It didn't work, so try to give a useful error:
1835 printf("Unexpected error trying to start the application!\n");
1836 fflush(stdout);
1837 exit(1);
1838 }
1839 // Create window with the registered class:
Mike Stroyanb46779e2015-06-15 14:20:13 -06001840 RECT wr = { 0, 0, demo->width, demo->height };
1841 AdjustWindowRect(&wr, WS_OVERLAPPEDWINDOW, FALSE);
Ian Elliott639ca472015-04-16 15:23:05 -06001842 demo->window = CreateWindowEx(0,
1843 demo->name, // class name
1844 demo->name, // app name
1845 WS_OVERLAPPEDWINDOW | // window style
1846 WS_VISIBLE |
1847 WS_SYSMENU,
1848 100,100, // x/y coords
Mike Stroyanb46779e2015-06-15 14:20:13 -06001849 wr.right-wr.left, // width
1850 wr.bottom-wr.top, // height
Ian Elliott639ca472015-04-16 15:23:05 -06001851 NULL, // handle to parent
1852 NULL, // handle to menu
1853 demo->connection, // hInstance
1854 NULL); // no extra parameters
1855 if (!demo->window) {
1856 // It didn't work, so try to give a useful error:
1857 printf("Cannot create a window in which to draw!\n");
1858 fflush(stdout);
1859 exit(1);
1860 }
1861}
1862#else // _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001863static void demo_handle_event(struct demo *demo,
1864 const xcb_generic_event_t *event)
1865{
Piers Daniell735ee532015-02-23 16:23:13 -07001866 uint8_t event_code = event->response_type & 0x7f;
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07001867 switch (event_code) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001868 case XCB_EXPOSE:
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07001869 // TODO: Resize window
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001870 break;
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07001871 case XCB_CLIENT_MESSAGE:
1872 if((*(xcb_client_message_event_t*)event).data.data32[0] ==
1873 (*demo->atom_wm_delete_window).atom) {
1874 demo->quit = true;
1875 }
1876 break;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001877 case XCB_KEY_RELEASE:
1878 {
1879 const xcb_key_release_event_t *key =
1880 (const xcb_key_release_event_t *) event;
1881
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001882 switch (key->detail) {
1883 case 0x9: // Escape
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001884 demo->quit = true;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001885 break;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001886 case 0x71: // left arrow key
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001887 demo->spin_angle += demo->spin_increment;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001888 break;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001889 case 0x72: // right arrow key
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001890 demo->spin_angle -= demo->spin_increment;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001891 break;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001892 case 0x41:
1893 demo->pause = !demo->pause;
Piers Daniell735ee532015-02-23 16:23:13 -07001894 break;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001895 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001896 }
1897 break;
1898 default:
1899 break;
1900 }
1901}
1902
1903static void demo_run(struct demo *demo)
1904{
1905 xcb_flush(demo->connection);
1906
1907 while (!demo->quit) {
1908 xcb_generic_event_t *event;
1909
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07001910 if (demo->pause) {
1911 event = xcb_wait_for_event(demo->connection);
1912 } else {
1913 event = xcb_poll_for_event(demo->connection);
1914 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001915 if (event) {
1916 demo_handle_event(demo, event);
1917 free(event);
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07001918 }
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07001919
1920 // Wait for work to finish before updating MVP.
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001921 vkDeviceWaitIdle(demo->device);
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07001922 demo_update_data_buffer(demo);
1923
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07001924 demo_draw(demo);
Courtney Goeltzenleuchter1454f3c2014-11-18 11:28:09 -07001925
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07001926 // Wait for work to finish before updating MVP.
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001927 vkDeviceWaitIdle(demo->device);
David Pinedo2bb7c932015-06-18 17:03:14 -06001928 demo->curFrame++;
1929 if (demo->frameCount != INT_MAX && demo->curFrame == demo->frameCount)
1930 demo->quit = true;
1931
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001932 }
1933}
1934
1935static void demo_create_window(struct demo *demo)
1936{
1937 uint32_t value_mask, value_list[32];
1938
1939 demo->window = xcb_generate_id(demo->connection);
1940
1941 value_mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK;
1942 value_list[0] = demo->screen->black_pixel;
1943 value_list[1] = XCB_EVENT_MASK_KEY_RELEASE |
1944 XCB_EVENT_MASK_EXPOSURE;
1945
1946 xcb_create_window(demo->connection,
1947 XCB_COPY_FROM_PARENT,
1948 demo->window, demo->screen->root,
1949 0, 0, demo->width, demo->height, 0,
1950 XCB_WINDOW_CLASS_INPUT_OUTPUT,
1951 demo->screen->root_visual,
1952 value_mask, value_list);
1953
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07001954 /* Magic code that will send notification when window is destroyed */
1955 xcb_intern_atom_cookie_t cookie = xcb_intern_atom(demo->connection, 1, 12,
1956 "WM_PROTOCOLS");
1957 xcb_intern_atom_reply_t* reply = xcb_intern_atom_reply(demo->connection, cookie, 0);
1958
1959 xcb_intern_atom_cookie_t cookie2 = xcb_intern_atom(demo->connection, 0, 16, "WM_DELETE_WINDOW");
1960 demo->atom_wm_delete_window = xcb_intern_atom_reply(demo->connection, cookie2, 0);
1961
1962 xcb_change_property(demo->connection, XCB_PROP_MODE_REPLACE,
1963 demo->window, (*reply).atom, 4, 32, 1,
1964 &(*demo->atom_wm_delete_window).atom);
1965 free(reply);
1966
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001967 xcb_map_window(demo->connection, demo->window);
David Pinedo2bb7c932015-06-18 17:03:14 -06001968
1969 // Force the x/y coordinates to 100,100 results are identical in consecutive runs
1970 const uint32_t coords[] = {100, 100};
1971 xcb_configure_window(demo->connection, demo->window,
1972 XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y, coords);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001973}
Ian Elliott639ca472015-04-16 15:23:05 -06001974#endif // _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001975
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06001976/*
1977 * Return 1 (true) if all layer names specified in check_names
1978 * can be found in given layer properties.
1979 */
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06001980static VkBool32 demo_check_layers(uint32_t check_count, char **check_names,
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06001981 uint32_t layer_count, VkLayerProperties *layers)
1982{
1983 for (uint32_t i = 0; i < check_count; i++) {
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06001984 VkBool32 found = 0;
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06001985 for (uint32_t j = 0; j < layer_count; j++) {
1986 if (!strcmp(check_names[i], layers[j].layerName)) {
1987 found = 1;
1988 }
1989 }
1990 if (!found) {
1991 fprintf(stderr, "Cannot find layer: %s\n", check_names[i]);
1992 return 0;
1993 }
1994 }
1995 return 1;
1996}
1997
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001998static void demo_init_vk(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001999{
Tobin Ehlis99239dc2015-04-16 18:04:57 -06002000 VkResult err;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002001 char *extension_names[64];
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002002 VkExtensionProperties *instance_extensions;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002003 VkLayerProperties *instance_layers;
2004 VkLayerProperties *device_layers;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002005 uint32_t instance_extension_count = 0;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002006 uint32_t instance_layer_count = 0;
2007 uint32_t enabled_extension_count = 0;
2008 uint32_t enabled_layer_count = 0;
2009
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002010 char *instance_validation_layers[] = {
2011 "MemTracker",
2012 };
2013
2014 char *device_validation_layers[] = {
2015 "MemTracker",
2016 };
2017
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002018 /* Look for validation layers */
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06002019 VkBool32 validation_found = 0;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002020 err = vkGetGlobalLayerProperties(&instance_layer_count, NULL);
Tobin Ehlis99239dc2015-04-16 18:04:57 -06002021 assert(!err);
2022
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002023 instance_layers = malloc(sizeof(VkLayerProperties) * instance_layer_count);
2024 err = vkGetGlobalLayerProperties(&instance_layer_count, instance_layers);
2025 assert(!err);
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002026
2027 if (demo->validate) {
2028 validation_found = demo_check_layers(ARRAY_SIZE(instance_validation_layers), instance_validation_layers,
2029 instance_layer_count, instance_layers);
2030 if (!validation_found) {
2031 ERR_EXIT("vkGetGlobalLayerProperties failed to find"
2032 "required validation layer.\n\n"
2033 "Please look at the Getting Started guide for additional "
2034 "information.\n",
2035 "vkCreateInstance Failure");
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002036 }
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002037 enabled_layer_count = ARRAY_SIZE(instance_validation_layers);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002038 }
2039
2040 err = vkGetGlobalExtensionProperties(NULL, &instance_extension_count, NULL);
2041 assert(!err);
2042
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06002043 VkBool32 WSIextFound = 0;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002044 memset(extension_names, 0, sizeof(extension_names));
2045 instance_extensions = malloc(sizeof(VkExtensionProperties) * instance_extension_count);
2046 err = vkGetGlobalExtensionProperties(NULL, &instance_extension_count, instance_extensions);
2047 assert(!err);
2048 for (uint32_t i = 0; i < instance_extension_count; i++) {
2049 if (!strcmp(VK_WSI_LUNARG_EXTENSION_NAME, instance_extensions[i].extName)) {
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002050 WSIextFound = 1;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002051 extension_names[enabled_extension_count++] = VK_WSI_LUNARG_EXTENSION_NAME;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002052 }
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002053 if (!strcmp(DEBUG_REPORT_EXTENSION_NAME, instance_extensions[i].extName)) {
2054 if (demo->validate) {
2055 extension_names[enabled_extension_count++] = DEBUG_REPORT_EXTENSION_NAME;
2056 }
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002057 }
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002058 assert(enabled_extension_count < 64);
Tobin Ehlis99239dc2015-04-16 18:04:57 -06002059 }
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002060 if (!WSIextFound) {
Tony Barbourecf1b2b2015-06-24 16:06:58 -06002061 ERR_EXIT("vkGetGlobalExtensionProperties failed to find the "
Ian Elliott65152912015-04-28 13:22:33 -06002062 "\"VK_WSI_LunarG\" extension.\n\nDo you have a compatible "
2063 "Vulkan installable client driver (ICD) installed?\nPlease "
2064 "look at the Getting Started guide for additional "
2065 "information.\n",
2066 "vkCreateInstance Failure");
2067 }
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06002068 const VkApplicationInfo app = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002069 .sType = VK_STRUCTURE_TYPE_APPLICATION_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002070 .pNext = NULL,
Ian Elliott44e33f72015-04-28 10:52:52 -06002071 .pAppName = APP_SHORT_NAME,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002072 .appVersion = 0,
Ian Elliott44e33f72015-04-28 10:52:52 -06002073 .pEngineName = APP_SHORT_NAME,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002074 .engineVersion = 0,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002075 .apiVersion = VK_API_VERSION,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002076 };
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002077 VkInstanceCreateInfo inst_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002078 .sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
Jon Ashburnab46b362015-04-04 14:52:07 -06002079 .pNext = NULL,
2080 .pAppInfo = &app,
2081 .pAllocCb = NULL,
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002082 .layerCount = enabled_layer_count,
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002083 .ppEnabledLayerNames = (const char *const*) ((demo->validate) ? instance_validation_layers : NULL),
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002084 .extensionCount = enabled_extension_count,
2085 .ppEnabledExtensionNames = (const char *const*) extension_names,
Jon Ashburnab46b362015-04-04 14:52:07 -06002086 };
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -06002087 const VkDeviceQueueCreateInfo queue = {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002088 .queueNodeIndex = 0,
2089 .queueCount = 1,
2090 };
Ian Elliott097d9f32015-04-28 11:35:02 -06002091
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06002092 uint32_t gpu_count;
2093 uint32_t i;
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07002094 uint32_t queue_count;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002095
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002096 err = vkCreateInstance(&inst_info, &demo->inst);
Ian Elliott07264132015-04-28 11:35:02 -06002097 if (err == VK_ERROR_INCOMPATIBLE_DRIVER) {
2098 ERR_EXIT("Cannot find a compatible Vulkan installable client driver "
Ian Elliott65152912015-04-28 13:22:33 -06002099 "(ICD).\n\nPlease look at the Getting Started guide for "
Ian Elliott07264132015-04-28 11:35:02 -06002100 "additional information.\n",
2101 "vkCreateInstance Failure");
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002102 } else if (err == VK_ERROR_INVALID_EXTENSION) {
2103 ERR_EXIT("Cannot find a specified extension library"
2104 ".\nMake sure your layers path is set appropriately\n",
2105 "vkCreateInstance Failure");
Ian Elliott07264132015-04-28 11:35:02 -06002106 } else if (err) {
Ian Elliott65152912015-04-28 13:22:33 -06002107 ERR_EXIT("vkCreateInstance failed.\n\nDo you have a compatible Vulkan "
2108 "installable client driver (ICD) installed?\nPlease look at "
Ian Elliott07264132015-04-28 11:35:02 -06002109 "the Getting Started guide for additional information.\n",
2110 "vkCreateInstance Failure");
Ian Elliott3979e282015-04-03 15:24:55 -06002111 }
Jon Ashburnab46b362015-04-04 14:52:07 -06002112
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002113 free(instance_layers);
2114 free(instance_extensions);
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002115
Jon Ashburn07c0c0c2015-04-15 11:31:12 -06002116 gpu_count = 1;
2117 err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, &demo->gpu);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002118 assert(!err && gpu_count == 1);
2119
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002120 /* Look for validation layers */
2121 validation_found = 0;
2122 enabled_layer_count = 0;
2123 uint32_t device_layer_count = 0;
2124 err = vkGetPhysicalDeviceLayerProperties(demo->gpu, &device_layer_count, NULL);
2125 assert(!err);
2126
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002127 device_layers = malloc(sizeof(VkLayerProperties) * device_layer_count);
2128 err = vkGetPhysicalDeviceLayerProperties(demo->gpu, &device_layer_count, device_layers);
2129 assert(!err);
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002130
2131 if (demo->validate) {
2132 validation_found = demo_check_layers(ARRAY_SIZE(device_validation_layers), device_validation_layers,
2133 device_layer_count, device_layers);
2134 if (!validation_found) {
2135 ERR_EXIT("vkGetPhysicalDeviceLayerProperties failed to find"
2136 "a required validation layer.\n\n"
2137 "Please look at the Getting Started guide for additional "
2138 "information.\n",
2139 "vkCreateDevice Failure");
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002140 }
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002141 enabled_layer_count = ARRAY_SIZE(device_validation_layers);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002142 }
2143
2144 uint32_t device_extension_count = 0;
2145 VkExtensionProperties *device_extensions = NULL;
2146 err = vkGetPhysicalDeviceExtensionProperties(
2147 demo->gpu, NULL, &device_extension_count, NULL);
2148 assert(!err);
2149
2150 WSIextFound = 0;
2151 enabled_extension_count = 0;
2152 memset(extension_names, 0, sizeof(extension_names));
2153 device_extensions = malloc(sizeof(VkExtensionProperties) * device_extension_count);
2154 err = vkGetPhysicalDeviceExtensionProperties(
2155 demo->gpu, NULL, &device_extension_count, device_extensions);
2156 assert(!err);
Courtney Goeltzenleuchterff6e23a2015-07-05 22:08:51 -06002157#if 0
2158 /* Will need this check in future */
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002159 for (uint32_t i = 0; i < device_extension_count; i++) {
2160 if (!strcmp(VK_WSI_LUNARG_EXTENSION_NAME, device_extensions[i].extName)) {
2161 WSIextFound = 1;
2162 extension_names[enabled_extension_count++] = VK_WSI_LUNARG_EXTENSION_NAME;
2163 }
2164 assert(enabled_extension_count < 64);
2165 }
2166 if (!WSIextFound) {
2167 ERR_EXIT("vkGetGlobalExtensionProperties failed to find the "
2168 "\"VK_WSI_LunarG\" extension.\n\nDo you have a compatible "
2169 "Vulkan installable client driver (ICD) installed?\nPlease "
2170 "look at the Getting Started guide for additional "
2171 "information.\n",
2172 "vkCreateInstance Failure");
2173 }
Courtney Goeltzenleuchterff6e23a2015-07-05 22:08:51 -06002174#endif
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002175
2176 VkDeviceCreateInfo device = {
2177 .sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
2178 .pNext = NULL,
2179 .queueRecordCount = 1,
2180 .pRequestedQueues = &queue,
2181 .layerCount = enabled_layer_count,
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002182 .ppEnabledLayerNames = (const char *const*) ((demo->validate) ? device_validation_layers : NULL),
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002183 .extensionCount = enabled_extension_count,
2184 .ppEnabledExtensionNames = (const char *const*) extension_names,
2185 .flags = 0,
2186 };
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002187
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002188 if (demo->validate) {
Tony Barbourd70b42c2015-06-30 14:14:19 -06002189 demo->dbgCreateMsgCallback = vkGetInstanceProcAddr(demo->inst, "vkDbgCreateMsgCallback");
2190 demo->dbgDestroyMsgCallback = vkGetInstanceProcAddr(demo->inst, "vkDbgDestroyMsgCallback");
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002191 if (!demo->dbgCreateMsgCallback) {
2192 ERR_EXIT("GetProcAddr: Unable to find vkDbgCreateMsgCallback\n",
2193 "vkGetProcAddr Failure");
2194 }
Tony Barbourd70b42c2015-06-30 14:14:19 -06002195 if (!demo->dbgDestroyMsgCallback) {
2196 ERR_EXIT("GetProcAddr: Unable to find vkDbgDestroyMsgCallback\n",
2197 "vkGetProcAddr Failure");
2198 }
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002199 err = demo->dbgCreateMsgCallback(
2200 demo->inst,
2201 VK_DBG_REPORT_ERROR_BIT | VK_DBG_REPORT_WARN_BIT,
2202 dbgFunc, NULL,
2203 &demo->msg_callback);
2204 switch (err) {
2205 case VK_SUCCESS:
2206 break;
2207 case VK_ERROR_INVALID_POINTER:
2208 ERR_EXIT("dbgCreateMsgCallback: Invalid pointer\n",
2209 "dbgCreateMsgCallback Failure");
2210 break;
2211 case VK_ERROR_OUT_OF_HOST_MEMORY:
2212 ERR_EXIT("dbgCreateMsgCallback: out of host memory\n",
2213 "dbgCreateMsgCallback Failure");
2214 break;
2215 default:
2216 ERR_EXIT("dbgCreateMsgCallback: unknown failure\n",
2217 "dbgCreateMsgCallback Failure");
2218 break;
2219 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002220 }
2221
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002222 err = vkCreateDevice(demo->gpu, &device, &demo->device);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002223 assert(!err);
2224
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002225 free(device_layers);
2226
Ian Elliott673898b2015-06-22 15:07:49 -06002227 GET_DEVICE_PROC_ADDR(demo->device, CreateSwapChainWSI);
2228 GET_DEVICE_PROC_ADDR(demo->device, CreateSwapChainWSI);
2229 GET_DEVICE_PROC_ADDR(demo->device, DestroySwapChainWSI);
2230 GET_DEVICE_PROC_ADDR(demo->device, GetSwapChainInfoWSI);
2231 GET_DEVICE_PROC_ADDR(demo->device, QueuePresentWSI);
Jon Ashburn0b85d052015-05-21 18:13:33 -06002232
Tony Barbourecf1b2b2015-06-24 16:06:58 -06002233 err = vkGetPhysicalDeviceProperties(demo->gpu, &demo->gpu_props);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06002234 assert(!err);
2235
Tony Barbourecf1b2b2015-06-24 16:06:58 -06002236 err = vkGetPhysicalDeviceQueueCount(demo->gpu, &queue_count);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06002237 assert(!err);
2238
Tony Barbourecf1b2b2015-06-24 16:06:58 -06002239 demo->queue_props = (VkPhysicalDeviceQueueProperties *) malloc(queue_count * sizeof(VkPhysicalDeviceQueueProperties));
2240 err = vkGetPhysicalDeviceQueueProperties(demo->gpu, queue_count, demo->queue_props);
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07002241 assert(!err);
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07002242 assert(queue_count >= 1);
2243
Mark Lobodzinskida9b1092015-04-16 11:44:05 -05002244 // Graphics queue and MemMgr queue can be separate.
2245 // TODO: Add support for separate queues, including synchronization,
Mark Lobodzinski6baaec72015-05-11 17:21:15 -05002246 // and appropriate tracking for QueueSubmit
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07002247 for (i = 0; i < queue_count; i++) {
Mark Lobodzinski6baaec72015-05-11 17:21:15 -05002248 if (demo->queue_props[i].queueFlags & VK_QUEUE_GRAPHICS_BIT)
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07002249 break;
2250 }
2251 assert(i < queue_count);
2252 demo->graphics_queue_node_index = i;
2253
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002254 err = vkGetDeviceQueue(demo->device, demo->graphics_queue_node_index,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002255 0, &demo->queue);
2256 assert(!err);
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002257
Jon Ashburnd7130cb2015-06-16 12:44:51 -06002258 // for now hardcode format till get WSI support
2259 demo->format = VK_FORMAT_B8G8R8A8_UNORM;
Ian Elliotte4602cd2015-04-21 16:41:02 -06002260
David Pinedo2bb7c932015-06-18 17:03:14 -06002261 demo->quit = false;
2262 demo->curFrame = 0;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06002263
2264 // Get Memory information and properties
2265 err = vkGetPhysicalDeviceMemoryProperties(demo->gpu, &demo->memory_properties);
2266 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002267}
2268
2269static void demo_init_connection(struct demo *demo)
2270{
Ian Elliott639ca472015-04-16 15:23:05 -06002271#ifndef _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002272 const xcb_setup_t *setup;
2273 xcb_screen_iterator_t iter;
2274 int scr;
2275
2276 demo->connection = xcb_connect(NULL, &scr);
Ian Elliott3979e282015-04-03 15:24:55 -06002277 if (demo->connection == NULL) {
2278 printf("Cannot find a compatible Vulkan installable client driver "
2279 "(ICD).\nExiting ...\n");
2280 fflush(stdout);
2281 exit(1);
2282 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002283
2284 setup = xcb_get_setup(demo->connection);
2285 iter = xcb_setup_roots_iterator(setup);
2286 while (scr-- > 0)
2287 xcb_screen_next(&iter);
2288
2289 demo->screen = iter.data;
Ian Elliott639ca472015-04-16 15:23:05 -06002290#endif // _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002291}
2292
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002293static void demo_init(struct demo *demo, int argc, char **argv)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002294{
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002295 vec3 eye = {0.0f, 3.0f, 5.0f};
2296 vec3 origin = {0, 0, 0};
Chia-I Wuae3b55d2015-04-22 14:56:17 +08002297 vec3 up = {0.0f, 1.0f, 0.0};
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002298
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002299 memset(demo, 0, sizeof(*demo));
David Pinedo2bb7c932015-06-18 17:03:14 -06002300 demo->frameCount = INT_MAX;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002301
Piers Daniell735ee532015-02-23 16:23:13 -07002302 for (int i = 1; i < argc; i++) {
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002303 if (strcmp(argv[i], "--use_staging") == 0) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002304 demo->use_staging_buffer = true;
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002305 continue;
Ian Elliott639ca472015-04-16 15:23:05 -06002306 }
Cody Northrop1fedb212015-05-28 11:27:16 -06002307 if (strcmp(argv[i], "--use_glsl") == 0) {
2308 demo->use_glsl = true;
2309 continue;
2310 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002311 if (strcmp(argv[i], "--validate") == 0) {
2312 demo->validate = true;
2313 continue;
2314 }
David Pinedo2bb7c932015-06-18 17:03:14 -06002315 if (strcmp(argv[i], "--c") == 0 &&
2316 demo->frameCount == INT_MAX &&
2317 i < argc-1 &&
2318 sscanf(argv[i+1],"%d", &demo->frameCount) == 1 &&
2319 demo->frameCount >= 0)
2320 {
2321 i++;
2322 continue;
2323 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002324
David Pinedo2bb7c932015-06-18 17:03:14 -06002325 fprintf(stderr, "Usage:\n %s [--use_staging] [--validate] [--c <framecount>]\n", APP_SHORT_NAME);
Ian Elliott639ca472015-04-16 15:23:05 -06002326 fflush(stderr);
2327 exit(1);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002328 }
2329
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002330 demo_init_connection(demo);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002331 demo_init_vk(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002332
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002333 demo->width = 500;
2334 demo->height = 500;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002335
2336 demo->spin_angle = 0.01f;
2337 demo->spin_increment = 0.01f;
2338 demo->pause = false;
2339
Piers Daniell735ee532015-02-23 16:23:13 -07002340 mat4x4_perspective(demo->projection_matrix, (float)degreesToRadians(45.0f), 1.0f, 0.1f, 100.0f);
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002341 mat4x4_look_at(demo->view_matrix, eye, origin, up);
2342 mat4x4_identity(demo->model_matrix);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002343}
2344
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002345
Ian Elliott639ca472015-04-16 15:23:05 -06002346#ifdef _WIN32
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002347extern int __getmainargs(
2348 int * _Argc,
2349 char *** _Argv,
2350 char *** _Env,
2351 int _DoWildCard,
2352 int * new_mode);
Ian Elliottf9cf78c2015-04-28 10:33:11 -06002353
Ian Elliotta748eaf2015-04-28 15:50:36 -06002354int WINAPI WinMain(HINSTANCE hInstance,
2355 HINSTANCE hPrevInstance,
2356 LPSTR pCmdLine,
2357 int nCmdShow)
Ian Elliott639ca472015-04-16 15:23:05 -06002358{
2359 MSG msg; // message
2360 bool done; // flag saying when app is complete
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002361 int argc;
2362 char** argv;
2363 char** env;
2364 int new_mode = 0;
Ian Elliott639ca472015-04-16 15:23:05 -06002365
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002366 __getmainargs(&argc,&argv,&env,0,&new_mode);
2367
2368 demo_init(&demo, argc, argv);
2369 demo.connection = hInstance;
2370 strncpy(demo.name, "cube", APP_NAME_STR_LEN);
Ian Elliott639ca472015-04-16 15:23:05 -06002371 demo_create_window(&demo);
2372
2373 demo_prepare(&demo);
2374
2375 done = false; //initialize loop condition variable
2376 /* main message loop*/
2377 while(!done)
2378 {
Ian Elliotta748eaf2015-04-28 15:50:36 -06002379 PeekMessage(&msg, NULL, 0, 0, PM_REMOVE);
Ian Elliott639ca472015-04-16 15:23:05 -06002380 if (msg.message == WM_QUIT) //check for a quit message
2381 {
2382 done = true; //if found, quit app
2383 }
2384 else
2385 {
2386 /* Translate and dispatch to event queue*/
2387 TranslateMessage(&msg);
2388 DispatchMessage(&msg);
2389 }
2390 }
2391
2392 demo_cleanup(&demo);
2393
Tony Barbourf9921732015-04-22 11:36:22 -06002394 return (int) msg.wParam;
Ian Elliott639ca472015-04-16 15:23:05 -06002395}
2396#else // _WIN32
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002397int main(int argc, char **argv)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002398{
2399 struct demo demo;
2400
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002401 demo_init(&demo, argc, argv);
Chia-I Wucbb564e2015-04-16 22:02:10 +08002402 demo_create_window(&demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002403
2404 demo_prepare(&demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002405 demo_run(&demo);
2406
2407 demo_cleanup(&demo);
2408
2409 return 0;
2410}
Ian Elliott639ca472015-04-16 15:23:05 -06002411#endif // _WIN32