blob: 15505ee6a85c9d26bd332c33a30c3e908f3242b9 [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
Tony Barbourfdc2d352015-04-22 09:02:32 -060053#if defined(NDEBUG) && defined(__GNUC__)
54#define U_ASSERT_ONLY __attribute__((unused))
55#else
56#define U_ASSERT_ONLY
57#endif
58
Ian Elliott07264132015-04-28 11:35:02 -060059#ifdef _WIN32
60#define ERR_EXIT(err_msg, err_class) \
61 do { \
62 MessageBox(NULL, err_msg, err_class, MB_OK); \
63 exit(1); \
64 } while (0)
65
66// NOTE: If the following values (copied from "loader_platform.h") change, they
67// need to change here as well:
68#define LAYER_NAMES_ENV "VK_LAYER_NAMES"
69#define LAYER_NAMES_REGISTRY_VALUE "VK_LAYER_NAMES"
70
71#else // _WIN32
72
73#define ERR_EXIT(err_msg, err_class) \
74 do { \
75 printf(err_msg); \
76 fflush(stdout); \
77 exit(1); \
78 } while (0)
79#endif // _WIN32
80
Ian Elliott673898b2015-06-22 15:07:49 -060081#define GET_DEVICE_PROC_ADDR(dev, entrypoint) \
82{ \
83 demo->fp##entrypoint = vkGetDeviceProcAddr(dev, "vk"#entrypoint); \
84 if (demo->fp##entrypoint == NULL) { \
85 ERR_EXIT("vkGetDeviceProcAddr failed to find vk"#entrypoint, \
86 "vkGetDeviceProcAddr Failure"); \
87 } \
88}
89
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -060090/*
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -070091 * structure to track all objects related to a texture.
92 */
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -060093struct texture_object {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -060094 VkSampler sampler;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -070095
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -060096 VkImage image;
97 VkImageLayout imageLayout;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -060098
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -050099 VkDeviceMemory mem;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600100 VkImageView view;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700101 int32_t tex_width, tex_height;
102};
103
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600104static char *tex_files[] = {
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -0600105 "lunarg-logo-256x256-solid.png"
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600106};
107
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600108struct vkcube_vs_uniform {
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600109 // Must start with MVP
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600110 float mvp[4][4];
111 float position[12*3][4];
112 float color[12*3][4];
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600113};
114
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600115struct vktexcube_vs_uniform {
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600116 // Must start with MVP
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600117 float mvp[4][4];
118 float position[12*3][4];
119 float attr[12*3][4];
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600120};
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600121
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600122//--------------------------------------------------------------------------------------
123// Mesh and VertexFormat Data
124//--------------------------------------------------------------------------------------
125struct Vertex
126{
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600127 float posX, posY, posZ, posW; // Position data
128 float r, g, b, a; // Color
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600129};
130
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600131struct VertexPosTex
132{
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600133 float posX, posY, posZ, posW; // Position data
134 float u, v, s, t; // Texcoord
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600135};
136
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600137#define XYZ1(_x_, _y_, _z_) (_x_), (_y_), (_z_), 1.f
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600138#define UV(_u_, _v_) (_u_), (_v_), 0.f, 1.f
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600139
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600140static const float g_vertex_buffer_data[] = {
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800141 -1.0f,-1.0f,-1.0f, // -X side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600142 -1.0f,-1.0f, 1.0f,
143 -1.0f, 1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800144 -1.0f, 1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600145 -1.0f, 1.0f,-1.0f,
146 -1.0f,-1.0f,-1.0f,
147
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800148 -1.0f,-1.0f,-1.0f, // -Z side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600149 1.0f, 1.0f,-1.0f,
150 1.0f,-1.0f,-1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800151 -1.0f,-1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600152 -1.0f, 1.0f,-1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600153 1.0f, 1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600154
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800155 -1.0f,-1.0f,-1.0f, // -Y side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600156 1.0f,-1.0f,-1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600157 1.0f,-1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800158 -1.0f,-1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600159 1.0f,-1.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600160 -1.0f,-1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600161
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800162 -1.0f, 1.0f,-1.0f, // +Y side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600163 -1.0f, 1.0f, 1.0f,
164 1.0f, 1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800165 -1.0f, 1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600166 1.0f, 1.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600167 1.0f, 1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600168
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800169 1.0f, 1.0f,-1.0f, // +X side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600170 1.0f, 1.0f, 1.0f,
171 1.0f,-1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800172 1.0f,-1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600173 1.0f,-1.0f,-1.0f,
174 1.0f, 1.0f,-1.0f,
175
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800176 -1.0f, 1.0f, 1.0f, // +Z side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600177 -1.0f,-1.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600178 1.0f, 1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800179 -1.0f,-1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600180 1.0f,-1.0f, 1.0f,
181 1.0f, 1.0f, 1.0f,
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600182};
183
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600184static const float g_uv_buffer_data[] = {
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800185 0.0f, 0.0f, // -X side
186 1.0f, 0.0f,
187 1.0f, 1.0f,
188 1.0f, 1.0f,
189 0.0f, 1.0f,
190 0.0f, 0.0f,
191
192 1.0f, 0.0f, // -Z side
193 0.0f, 1.0f,
194 0.0f, 0.0f,
195 1.0f, 0.0f,
196 1.0f, 1.0f,
197 0.0f, 1.0f,
198
199 1.0f, 1.0f, // -Y side
200 1.0f, 0.0f,
201 0.0f, 0.0f,
202 1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600203 0.0f, 0.0f,
204 0.0f, 1.0f,
205
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800206 1.0f, 1.0f, // +Y side
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600207 0.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800208 0.0f, 0.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600209 1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600210 0.0f, 0.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600211 1.0f, 0.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600212
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800213 1.0f, 1.0f, // +X side
214 0.0f, 1.0f,
215 0.0f, 0.0f,
216 0.0f, 0.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600217 1.0f, 0.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600218 1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600219
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800220 0.0f, 1.0f, // +Z side
221 0.0f, 0.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600222 1.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600223 0.0f, 0.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800224 1.0f, 0.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600225 1.0f, 1.0f,
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600226};
227
228void dumpMatrix(const char *note, mat4x4 MVP)
229{
230 int i;
231
232 printf("%s: \n", note);
233 for (i=0; i<4; i++) {
234 printf("%f, %f, %f, %f\n", MVP[i][0], MVP[i][1], MVP[i][2], MVP[i][3]);
235 }
236 printf("\n");
237 fflush(stdout);
238}
239
240void dumpVec4(const char *note, vec4 vector)
241{
242 printf("%s: \n", note);
243 printf("%f, %f, %f, %f\n", vector[0], vector[1], vector[2], vector[3]);
244 printf("\n");
245 fflush(stdout);
246}
247
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -0600248void dbgFunc(
249 VkFlags msgFlags,
250 VkObjectType objType,
251 VkObject srcObject,
252 size_t location,
253 int32_t msgCode,
254 const char* pLayerPrefix,
255 const char* pMsg,
256 void* pUserData)
Tony Barbour3dddd5d2015-04-29 16:19:20 -0600257{
258 char *message = (char *) malloc(strlen(pMsg)+100);
259
260 assert (message);
261
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -0600262 if (msgFlags & VK_DBG_REPORT_ERROR_BIT) {
263 sprintf(message,"ERROR: [%s] Code %d : %s", pLayerPrefix, msgCode, pMsg);
264 } else if (msgFlags & VK_DBG_REPORT_WARN_BIT) {
Tony Barbourd70b42c2015-06-30 14:14:19 -0600265 // We know that we're submitting queues without fences, ignore this warning
266 if (strstr(pMsg, "vkQueueSubmit parameter, VkFence fence, is null pointer")){
267 return;
268 }
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -0600269 sprintf(message,"WARNING: [%s] Code %d : %s", pLayerPrefix, msgCode, pMsg);
Tony Barbour3dddd5d2015-04-29 16:19:20 -0600270 } else {
271 return;
272 }
273
274#ifdef _WIN32
275 MessageBox(NULL, message, "Alert", MB_OK);
276#else
277 printf("%s\n",message);
278 fflush(stdout);
279#endif
280 free(message);
281}
282
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600283struct demo {
Ian Elliott639ca472015-04-16 15:23:05 -0600284#ifdef _WIN32
285#define APP_NAME_STR_LEN 80
286 HINSTANCE connection; // hInstance - Windows Instance
287 char name[APP_NAME_STR_LEN]; // Name to put on the window/icon
288 HWND window; // hWnd - window handle
289#else // _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600290 xcb_connection_t *connection;
291 xcb_screen_t *screen;
Chia-I Wucbb564e2015-04-16 22:02:10 +0800292 xcb_window_t window;
293 xcb_intern_atom_reply_t *atom_wm_delete_window;
Ian Elliott639ca472015-04-16 15:23:05 -0600294#endif // _WIN32
Cody Northrop1fedb212015-05-28 11:27:16 -0600295 bool prepared;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700296 bool use_staging_buffer;
Cody Northrop1fedb212015-05-28 11:27:16 -0600297 bool use_glsl;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600298
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600299 VkInstance inst;
Tony Barbour72304ef2015-04-16 15:59:00 -0600300 VkPhysicalDevice gpu;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600301 VkDevice device;
302 VkQueue queue;
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -0700303 uint32_t graphics_queue_node_index;
Tony Barbourecf1b2b2015-06-24 16:06:58 -0600304 VkPhysicalDeviceProperties gpu_props;
Tony Barbour72304ef2015-04-16 15:59:00 -0600305 VkPhysicalDeviceQueueProperties *queue_props;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600306 VkPhysicalDeviceMemoryProperties memory_properties;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600307
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600308 VkFramebuffer framebuffer;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600309 int width, height;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600310 VkFormat format;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600311
Jon Ashburn0b85d052015-05-21 18:13:33 -0600312 PFN_vkCreateSwapChainWSI fpCreateSwapChainWSI;
313 PFN_vkDestroySwapChainWSI fpDestroySwapChainWSI;
314 PFN_vkGetSwapChainInfoWSI fpGetSwapChainInfoWSI;
315 PFN_vkQueuePresentWSI fpQueuePresentWSI;
Chia-I Wucbb564e2015-04-16 22:02:10 +0800316 VkSwapChainWSI swap_chain;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600317 struct {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600318 VkImage image;
Tony Barbour72304ef2015-04-16 15:59:00 -0600319 VkDeviceMemory mem;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600320 VkCmdBuffer cmd;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600321
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600322 VkColorAttachmentView view;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600323 } buffers[DEMO_BUFFER_COUNT];
324
325 struct {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600326 VkFormat format;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600327
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600328 VkImage image;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500329 VkDeviceMemory mem;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600330 VkDepthStencilView view;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600331 } depth;
332
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600333 struct texture_object textures[DEMO_TEXTURE_COUNT];
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600334
335 struct {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600336 VkBuffer buf;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500337 VkDeviceMemory mem;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600338 VkBufferView view;
Chia-I Wuae721ba2015-05-25 16:27:55 +0800339 VkDescriptorInfo desc;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600340 } uniform_data;
341
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600342 VkCmdBuffer cmd; // Buffer for initialization commands
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -0500343 VkPipelineLayout pipeline_layout;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600344 VkDescriptorSetLayout desc_layout;
345 VkPipeline pipeline;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600346
Courtney Goeltzenleuchter5e6d1e92015-04-10 16:24:50 -0600347 VkDynamicVpState viewport;
348 VkDynamicRsState raster;
349 VkDynamicCbState color_blend;
350 VkDynamicDsState depth_stencil;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600351
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600352 mat4x4 projection_matrix;
353 mat4x4 view_matrix;
354 mat4x4 model_matrix;
355
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600356 float spin_angle;
357 float spin_increment;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600358 bool pause;
359
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600360 VkDescriptorPool desc_pool;
361 VkDescriptorSet desc_set;
Chia-I Wu6a3c8972015-01-04 16:27:24 +0800362
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600363 bool quit;
David Pinedo2bb7c932015-06-18 17:03:14 -0600364 int32_t curFrame;
365 int32_t frameCount;
Tony Barbour3dddd5d2015-04-29 16:19:20 -0600366 bool validate;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -0600367 PFN_vkDbgCreateMsgCallback dbgCreateMsgCallback;
Tony Barbourd70b42c2015-06-30 14:14:19 -0600368 PFN_vkDbgDestroyMsgCallback dbgDestroyMsgCallback;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -0600369 VkDbgMsgCallback msg_callback;
370
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600371 uint32_t current_buffer;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600372};
373
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600374static VkResult memory_type_from_properties(struct demo *demo, uint32_t typeBits, VkFlags properties, uint32_t *typeIndex)
375{
376 // Search memtypes to find first index with those properties
377 for (uint32_t i = 0; i < 32; i++) {
378 if ((typeBits & 1) == 1) {
379 // Type is available, does it match user properties?
380 if ((demo->memory_properties.memoryTypes[i].propertyFlags & properties) == properties) {
381 *typeIndex = i;
382 return VK_SUCCESS;
383 }
384 }
385 typeBits >>= 1;
386 }
387 // No memory types matched, return failure
388 return VK_UNSUPPORTED;
389}
390
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600391static void demo_flush_init_cmd(struct demo *demo)
392{
Tony Barbourfdc2d352015-04-22 09:02:32 -0600393 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600394
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600395 if (demo->cmd == VK_NULL_HANDLE)
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600396 return;
397
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600398 err = vkEndCommandBuffer(demo->cmd);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600399 assert(!err);
400
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600401 const VkCmdBuffer cmd_bufs[] = { demo->cmd };
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600402
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600403 err = vkQueueSubmit(demo->queue, 1, cmd_bufs, VK_NULL_HANDLE);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600404 assert(!err);
405
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600406 err = vkQueueWaitIdle(demo->queue);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600407 assert(!err);
408
Mike Stroyanebae8322015-04-17 12:36:38 -0600409 vkDestroyObject(demo->device, VK_OBJECT_TYPE_COMMAND_BUFFER, demo->cmd);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600410 demo->cmd = VK_NULL_HANDLE;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600411}
412
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600413static void demo_set_image_layout(
414 struct demo *demo,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600415 VkImage image,
malnasseeb25d3f2015-06-03 17:28:38 -0400416 VkImageAspect aspect,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600417 VkImageLayout old_image_layout,
418 VkImageLayout new_image_layout)
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600419{
Tony Barbourfdc2d352015-04-22 09:02:32 -0600420 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600421
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600422 if (demo->cmd == VK_NULL_HANDLE) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600423 const VkCmdBufferCreateInfo cmd = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600424 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600425 .pNext = NULL,
426 .queueNodeIndex = demo->graphics_queue_node_index,
427 .flags = 0,
428 };
429
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600430 err = vkCreateCommandBuffer(demo->device, &cmd, &demo->cmd);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600431 assert(!err);
432
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600433 VkCmdBufferBeginInfo cmd_buf_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600434 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600435 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -0600436 .flags = VK_CMD_BUFFER_OPTIMIZE_SMALL_BATCH_BIT |
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600437 VK_CMD_BUFFER_OPTIMIZE_ONE_TIME_SUBMIT_BIT,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600438 };
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600439 err = vkBeginCommandBuffer(demo->cmd, &cmd_buf_info);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600440 }
441
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600442 VkImageMemoryBarrier image_memory_barrier = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600443 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600444 .pNext = NULL,
445 .outputMask = 0,
446 .inputMask = 0,
447 .oldLayout = old_image_layout,
448 .newLayout = new_image_layout,
449 .image = image,
malnasseeb25d3f2015-06-03 17:28:38 -0400450 .subresourceRange = { aspect, 0, 1, 0, 0 }
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600451 };
452
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600453 if (new_image_layout == VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL) {
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600454 /* Make sure anything that was copying from this image has completed */
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -0600455 image_memory_barrier.inputMask = VK_MEMORY_INPUT_TRANSFER_BIT;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600456 }
457
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600458 if (new_image_layout == VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL) {
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600459 /* Make sure any Copy or CPU writes to image are flushed */
Courtney Goeltzenleuchter2bda8332015-04-29 17:16:21 -0600460 image_memory_barrier.outputMask = VK_MEMORY_OUTPUT_HOST_WRITE_BIT | VK_MEMORY_OUTPUT_TRANSFER_BIT;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600461 }
462
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600463 VkImageMemoryBarrier *pmemory_barrier = &image_memory_barrier;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600464
Tony Barbour50bbca42015-06-29 16:20:35 -0600465 VkPipelineStageFlags src_stages = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
466 VkPipelineStageFlags dest_stages = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600467
Tony Barbour50bbca42015-06-29 16:20:35 -0600468 vkCmdPipelineBarrier(demo->cmd, src_stages, dest_stages, false, 1, (const void **)&pmemory_barrier);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600469}
470
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600471static void demo_draw_build_cmd(struct demo *demo, VkCmdBuffer cmd_buf)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600472{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600473 const VkColorAttachmentBindInfo color_attachment = {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600474 .view = demo->buffers[demo->current_buffer].view,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600475 .layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600476 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600477 const VkDepthStencilBindInfo depth_stencil = {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600478 .view = demo->depth.view,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600479 .layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600480 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600481 const VkClearColor clear_color = {
Courtney Goeltzenleuchter374553c2015-04-03 16:35:32 -0600482 .color.floatColor = { 0.2f, 0.2f, 0.2f, 0.2f },
483 .useRawValue = false,
484 };
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600485 const float clear_depth = 1.0f;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600486 VkImageSubresourceRange clear_range;
487 VkCmdBufferBeginInfo cmd_buf_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600488 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO,
Courtney Goeltzenleuchter53968d82015-04-03 15:25:24 -0600489 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -0600490 .flags = VK_CMD_BUFFER_OPTIMIZE_SMALL_BATCH_BIT |
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600491 VK_CMD_BUFFER_OPTIMIZE_ONE_TIME_SUBMIT_BIT,
Jon Ashburn60ccfbe2014-12-31 17:08:35 -0700492 };
Tony Barbourfdc2d352015-04-22 09:02:32 -0600493 VkResult U_ASSERT_ONLY err;
Chris Forbes40a71562015-06-17 11:36:12 +1200494 VkAttachmentLoadOp load_op = VK_ATTACHMENT_LOAD_OP_CLEAR;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600495 VkAttachmentStoreOp store_op = VK_ATTACHMENT_STORE_OP_DONT_CARE;
496 const VkFramebufferCreateInfo fb_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600497 .sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO,
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700498 .pNext = NULL,
499 .colorAttachmentCount = 1,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600500 .pColorAttachments = (VkColorAttachmentBindInfo*) &color_attachment,
501 .pDepthStencilAttachment = (VkDepthStencilBindInfo*) &depth_stencil,
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700502 .sampleCount = 1,
Mark Lobodzinskic06b7412015-01-27 13:24:03 -0600503 .width = demo->width,
504 .height = demo->height,
505 .layers = 1,
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700506 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600507 VkRenderPassCreateInfo rp_info;
508 VkRenderPassBegin rp_begin;
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700509
510 memset(&rp_info, 0 , sizeof(rp_info));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600511 err = vkCreateFramebuffer(demo->device, &fb_info, &rp_begin.framebuffer);
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700512 assert(!err);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600513 rp_info.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO;
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700514 rp_info.renderArea.extent.width = demo->width;
515 rp_info.renderArea.extent.height = demo->height;
Courtney Goeltzenleuchter53968d82015-04-03 15:25:24 -0600516 rp_info.colorAttachmentCount = fb_info.colorAttachmentCount;
517 rp_info.pColorFormats = &demo->format;
518 rp_info.pColorLayouts = &color_attachment.layout;
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700519 rp_info.pColorLoadOps = &load_op;
520 rp_info.pColorStoreOps = &store_op;
Courtney Goeltzenleuchter53968d82015-04-03 15:25:24 -0600521 rp_info.pColorLoadClearValues = &clear_color;
Tony Barbour72304ef2015-04-16 15:59:00 -0600522 rp_info.depthStencilFormat = VK_FORMAT_D16_UNORM;
Courtney Goeltzenleuchter53968d82015-04-03 15:25:24 -0600523 rp_info.depthStencilLayout = depth_stencil.layout;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600524 rp_info.depthLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
Courtney Goeltzenleuchter53968d82015-04-03 15:25:24 -0600525 rp_info.depthLoadClearValue = clear_depth;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600526 rp_info.depthStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
527 rp_info.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
Courtney Goeltzenleuchter53968d82015-04-03 15:25:24 -0600528 rp_info.stencilLoadClearValue = 0;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600529 rp_info.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
Tony Barbourd70b42c2015-06-30 14:14:19 -0600530 rp_info.sampleCount = 1;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600531 err = vkCreateRenderPass(demo->device, &rp_info, &rp_begin.renderPass);
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700532 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600533
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600534 err = vkBeginCommandBuffer(cmd_buf, &cmd_buf_info);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600535 assert(!err);
536
Tobin Ehlis080219d2015-06-24 15:53:07 -0600537 vkCmdBeginRenderPass(cmd_buf, &rp_begin);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600538 vkCmdBindPipeline(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600539 demo->pipeline);
Mark Lobodzinskic6e8b3d2015-06-15 13:21:21 -0600540 vkCmdBindDescriptorSets(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS, demo->pipeline_layout,
Cody Northropfb5185a2015-04-16 13:41:56 -0600541 0, 1, &demo->desc_set, 0, NULL);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600542
Tony Barbour72304ef2015-04-16 15:59:00 -0600543 vkCmdBindDynamicStateObject(cmd_buf, VK_STATE_BIND_POINT_VIEWPORT, demo->viewport);
544 vkCmdBindDynamicStateObject(cmd_buf, VK_STATE_BIND_POINT_RASTER, demo->raster);
545 vkCmdBindDynamicStateObject(cmd_buf, VK_STATE_BIND_POINT_COLOR_BLEND,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600546 demo->color_blend);
Tony Barbour72304ef2015-04-16 15:59:00 -0600547 vkCmdBindDynamicStateObject(cmd_buf, VK_STATE_BIND_POINT_DEPTH_STENCIL,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600548 demo->depth_stencil);
549
Chris Forbes40a71562015-06-17 11:36:12 +1200550 clear_range.aspect = VK_IMAGE_ASPECT_DEPTH;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600551 clear_range.baseMipLevel = 0;
552 clear_range.mipLevels = 1;
553 clear_range.baseArraySlice = 0;
554 clear_range.arraySize = 1;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600555
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600556 vkCmdClearDepthStencil(cmd_buf, demo->depth.image,
557 VK_IMAGE_LAYOUT_CLEAR_OPTIMAL,
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600558 clear_depth, 0, 1, &clear_range);
559
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600560 vkCmdDraw(cmd_buf, 0, 12 * 3, 0, 1);
561 vkCmdEndRenderPass(cmd_buf, rp_begin.renderPass);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600562
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600563 err = vkEndCommandBuffer(cmd_buf);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600564 assert(!err);
Courtney Goeltzenleuchter94901dc2015-02-25 17:53:18 -0700565
Mike Stroyanebae8322015-04-17 12:36:38 -0600566 vkDestroyObject(demo->device, VK_OBJECT_TYPE_RENDER_PASS, rp_begin.renderPass);
567 vkDestroyObject(demo->device, VK_OBJECT_TYPE_FRAMEBUFFER, rp_begin.framebuffer);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600568}
569
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600570
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600571void demo_update_data_buffer(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600572{
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600573 mat4x4 MVP, Model, VP;
574 int matrixSize = sizeof(MVP);
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600575 uint8_t *pData;
Tony Barbourfdc2d352015-04-22 09:02:32 -0600576 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600577
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600578 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600579
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600580 // Rotate 22.5 degrees around the Y axis
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600581 mat4x4_dup(Model, demo->model_matrix);
Piers Daniell735ee532015-02-23 16:23:13 -0700582 mat4x4_rotate(demo->model_matrix, Model, 0.0f, 1.0f, 0.0f, (float)degreesToRadians(demo->spin_angle));
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600583 mat4x4_mul(MVP, VP, demo->model_matrix);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600584
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500585 err = vkMapMemory(demo->device, demo->uniform_data.mem, 0, 0, 0, (void **) &pData);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600586 assert(!err);
587
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600588 memcpy(pData, (const void*) &MVP[0][0], matrixSize);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600589
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500590 err = vkUnmapMemory(demo->device, demo->uniform_data.mem);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600591 assert(!err);
592}
593
594static void demo_draw(struct demo *demo)
595{
Chia-I Wucbb564e2015-04-16 22:02:10 +0800596 const VkPresentInfoWSI present = {
597 .sType = VK_STRUCTURE_TYPE_PRESENT_INFO_WSI,
598 .pNext = NULL,
599 .image = demo->buffers[demo->current_buffer].image,
600 .flipInterval = 0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600601 };
Tony Barbourfdc2d352015-04-22 09:02:32 -0600602 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600603
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600604 err = vkQueueSubmit(demo->queue, 1, &demo->buffers[demo->current_buffer].cmd,
605 VK_NULL_HANDLE);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600606 assert(!err);
607
Jon Ashburn0b85d052015-05-21 18:13:33 -0600608 err = demo->fpQueuePresentWSI(demo->queue, &present);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600609 assert(!err);
610
611 demo->current_buffer = (demo->current_buffer + 1) % DEMO_BUFFER_COUNT;
Chia-I Wucbb564e2015-04-16 22:02:10 +0800612
613 err = vkQueueWaitIdle(demo->queue);
614 assert(err == VK_SUCCESS);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600615}
616
617static void demo_prepare_buffers(struct demo *demo)
618{
Chia-I Wucbb564e2015-04-16 22:02:10 +0800619 const VkSwapChainCreateInfoWSI swap_chain = {
620 .sType = VK_STRUCTURE_TYPE_SWAP_CHAIN_CREATE_INFO_WSI,
621 .pNext = NULL,
622 .pNativeWindowSystemHandle = demo->connection,
623 .pNativeWindowHandle = (void *) (intptr_t) demo->window,
Ian Elliotte4602cd2015-04-21 16:41:02 -0600624 .displayCount = 1,
Chia-I Wucbb564e2015-04-16 22:02:10 +0800625 .imageCount = DEMO_BUFFER_COUNT,
626 .imageFormat = demo->format,
627 .imageExtent = {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600628 .width = demo->width,
629 .height = demo->height,
630 },
Chia-I Wucbb564e2015-04-16 22:02:10 +0800631 .imageArraySize = 1,
632 .imageUsageFlags = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600633 };
Chia-I Wucbb564e2015-04-16 22:02:10 +0800634 VkSwapChainImageInfoWSI images[DEMO_BUFFER_COUNT];
635 size_t images_size = sizeof(images);
Tony Barbourfdc2d352015-04-22 09:02:32 -0600636 VkResult U_ASSERT_ONLY err;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600637 uint32_t i;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600638
Jon Ashburn0b85d052015-05-21 18:13:33 -0600639 err = demo->fpCreateSwapChainWSI(demo->device, &swap_chain, &demo->swap_chain);
Chia-I Wucbb564e2015-04-16 22:02:10 +0800640 assert(!err);
641
Jon Ashburn0b85d052015-05-21 18:13:33 -0600642 err = demo->fpGetSwapChainInfoWSI(demo->swap_chain,
Chia-I Wucbb564e2015-04-16 22:02:10 +0800643 VK_SWAP_CHAIN_INFO_TYPE_PERSISTENT_IMAGES_WSI,
644 &images_size, images);
645 assert(!err && images_size == sizeof(images));
646
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600647 for (i = 0; i < DEMO_BUFFER_COUNT; i++) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600648 VkColorAttachmentViewCreateInfo color_attachment_view = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600649 .sType = VK_STRUCTURE_TYPE_COLOR_ATTACHMENT_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600650 .pNext = NULL,
651 .format = demo->format,
652 .mipLevel = 0,
653 .baseArraySlice = 0,
654 .arraySize = 1,
655 };
656
Chia-I Wucbb564e2015-04-16 22:02:10 +0800657 demo->buffers[i].image = images[i].image;
658 demo->buffers[i].mem = images[i].memory;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600659
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600660 demo_set_image_layout(demo, demo->buffers[i].image,
malnasseeb25d3f2015-06-03 17:28:38 -0400661 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600662 VK_IMAGE_LAYOUT_UNDEFINED,
663 VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600664
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600665 color_attachment_view.image = demo->buffers[i].image;
666
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600667 err = vkCreateColorAttachmentView(demo->device,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600668 &color_attachment_view, &demo->buffers[i].view);
669 assert(!err);
670 }
671}
672
673static void demo_prepare_depth(struct demo *demo)
674{
Tony Barbour72304ef2015-04-16 15:59:00 -0600675 const VkFormat depth_format = VK_FORMAT_D16_UNORM;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600676 const VkImageCreateInfo image = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600677 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600678 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -0600679 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600680 .format = depth_format,
681 .extent = { demo->width, demo->height, 1 },
682 .mipLevels = 1,
683 .arraySize = 1,
684 .samples = 1,
Tony Barbour72304ef2015-04-16 15:59:00 -0600685 .tiling = VK_IMAGE_TILING_OPTIMAL,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600686 .usage = VK_IMAGE_USAGE_DEPTH_STENCIL_BIT,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600687 .flags = 0,
688 };
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -0600689 VkMemoryAllocInfo mem_alloc = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600690 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
Mark Lobodzinskia6907f72015-04-16 08:52:00 -0500691 .pNext = NULL,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600692 .allocationSize = 0,
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600693 .memoryTypeIndex = 0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600694 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600695 VkDepthStencilViewCreateInfo view = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600696 .sType = VK_STRUCTURE_TYPE_DEPTH_STENCIL_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600697 .pNext = NULL,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600698 .image = VK_NULL_HANDLE,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600699 .mipLevel = 0,
700 .baseArraySlice = 0,
701 .arraySize = 1,
702 .flags = 0,
703 };
Mike Stroyanebae8322015-04-17 12:36:38 -0600704
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500705 VkMemoryRequirements mem_reqs;
Tony Barbourfdc2d352015-04-22 09:02:32 -0600706 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600707
708 demo->depth.format = depth_format;
709
710 /* create image */
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600711 err = vkCreateImage(demo->device, &image,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600712 &demo->depth.image);
713 assert(!err);
714
Tony Barbourecf1b2b2015-06-24 16:06:58 -0600715 err = vkGetObjectMemoryRequirements(demo->device,
716 VK_OBJECT_TYPE_IMAGE, demo->depth.image, &mem_reqs);
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600717
718 mem_alloc.allocationSize = mem_reqs.size;
719 err = memory_type_from_properties(demo,
720 mem_reqs.memoryTypeBits,
721 VK_MEMORY_PROPERTY_DEVICE_ONLY,
722 &mem_alloc.memoryTypeIndex);
723 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600724
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500725 /* allocate memory */
726 err = vkAllocMemory(demo->device, &mem_alloc, &demo->depth.mem);
727 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600728
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500729 /* bind memory */
730 err = vkBindObjectMemory(demo->device,
731 VK_OBJECT_TYPE_IMAGE, demo->depth.image,
732 demo->depth.mem, 0);
733 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600734
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600735 demo_set_image_layout(demo, demo->depth.image,
malnasseeb25d3f2015-06-03 17:28:38 -0400736 VK_IMAGE_ASPECT_DEPTH,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600737 VK_IMAGE_LAYOUT_UNDEFINED,
738 VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600739
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600740 /* create image view */
741 view.image = demo->depth.image;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600742 err = vkCreateDepthStencilView(demo->device, &view,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600743 &demo->depth.view);
744 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600745}
746
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600747/** loadTexture
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -0600748 * loads a png file into an memory object, using cstdio , libpng.
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600749 *
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -0600750 * \param demo : Needed to access VK calls
751 * \param filename : the png file to be loaded
752 * \param width : width of png, to be updated as a side effect of this function
753 * \param height : height of png, to be updated as a side effect of this function
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600754 *
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -0600755 * \return bool : an opengl texture id. true if successful?,
756 * should be validated by the client of this function.
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600757 *
758 * Source: http://en.wikibooks.org/wiki/OpenGL_Programming/Intermediate/Textures
759 * Modified to copy image to memory
760 *
761 */
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700762bool loadTexture(const char *filename, uint8_t *rgba_data,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600763 VkSubresourceLayout *layout,
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600764 int32_t *width, int32_t *height)
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600765{
766 //header for testing if it is a png
767 png_byte header[8];
Tony Barbourf9921732015-04-22 11:36:22 -0600768 int is_png, bit_depth, color_type, rowbytes;
769 size_t retval;
Ian Elliott1e42dff2015-02-13 14:29:21 -0700770 png_uint_32 i, twidth, theight;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600771 png_structp png_ptr;
772 png_infop info_ptr, end_info;
773 png_byte *image_data;
774 png_bytep *row_pointers;
775
776 //open file as binary
777 FILE *fp = fopen(filename, "rb");
778 if (!fp) {
779 return false;
780 }
781
782 //read the header
Tony Barbourfdc2d352015-04-22 09:02:32 -0600783 retval = fread(header, 1, 8, fp);
784 if (retval != 8) {
785 fclose(fp);
786 return false;
787 }
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600788
789 //test if png
790 is_png = !png_sig_cmp(header, 0, 8);
791 if (!is_png) {
792 fclose(fp);
793 return false;
794 }
795
796 //create png struct
797 png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL,
798 NULL, NULL);
799 if (!png_ptr) {
800 fclose(fp);
801 return (false);
802 }
803
804 //create png info struct
805 info_ptr = png_create_info_struct(png_ptr);
806 if (!info_ptr) {
807 png_destroy_read_struct(&png_ptr, (png_infopp) NULL, (png_infopp) NULL);
808 fclose(fp);
809 return (false);
810 }
811
812 //create png info struct
813 end_info = png_create_info_struct(png_ptr);
814 if (!end_info) {
815 png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp) NULL);
816 fclose(fp);
817 return (false);
818 }
819
820 //png error stuff, not sure libpng man suggests this.
821 if (setjmp(png_jmpbuf(png_ptr))) {
822 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
823 fclose(fp);
824 return (false);
825 }
826
827 //init png reading
828 png_init_io(png_ptr, fp);
829
830 //let libpng know you already read the first 8 bytes
831 png_set_sig_bytes(png_ptr, 8);
832
833 // read all the info up to the image data
834 png_read_info(png_ptr, info_ptr);
835
836 // get info about png
837 png_get_IHDR(png_ptr, info_ptr, &twidth, &theight, &bit_depth, &color_type,
838 NULL, NULL, NULL);
839
840 //update width and height based on png info
841 *width = twidth;
842 *height = theight;
843
844 // Require that incoming texture be 8bits per color component
845 // and 4 components (RGBA).
846 if (png_get_bit_depth(png_ptr, info_ptr) != 8 ||
847 png_get_channels(png_ptr, info_ptr) != 4) {
848 return false;
849 }
850
851 if (rgba_data == NULL) {
852 // If data pointer is null, we just want the width & height
853 // clean up memory and close stuff
854 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
855 fclose(fp);
856
857 return true;
858 }
859
860 // Update the png info struct.
861 png_read_update_info(png_ptr, info_ptr);
862
863 // Row size in bytes.
864 rowbytes = png_get_rowbytes(png_ptr, info_ptr);
865
866 // Allocate the image_data as a big block, to be given to opengl
867 image_data = (png_byte *)malloc(rowbytes * theight * sizeof(png_byte));
868 if (!image_data) {
869 //clean up memory and close stuff
870 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
871 fclose(fp);
872 return false;
873 }
874
875 // row_pointers is for pointing to image_data for reading the png with libpng
876 row_pointers = (png_bytep *)malloc(theight * sizeof(png_bytep));
877 if (!row_pointers) {
878 //clean up memory and close stuff
879 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
880 // delete[] image_data;
881 fclose(fp);
882 return false;
883 }
884 // set the individual row_pointers to point at the correct offsets of image_data
885 for (i = 0; i < theight; ++i)
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700886 row_pointers[theight - 1 - i] = rgba_data + i * layout->rowPitch;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600887
888 // read the png into image_data through row_pointers
889 png_read_image(png_ptr, row_pointers);
890
891 // clean up memory and close stuff
892 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
893 free(row_pointers);
894 free(image_data);
895 fclose(fp);
896
897 return true;
898}
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600899
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700900static void demo_prepare_texture_image(struct demo *demo,
901 const char *filename,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600902 struct texture_object *tex_obj,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600903 VkImageTiling tiling,
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -0600904 VkImageUsageFlags usage,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600905 VkFlags mem_props)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600906{
Mike Stroyan8b89e072015-06-15 14:21:03 -0600907 const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600908 int32_t tex_width;
909 int32_t tex_height;
Tony Barbourfdc2d352015-04-22 09:02:32 -0600910 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700911
David Pinedo30bd71d2015-04-23 08:16:57 -0600912 if (!loadTexture(filename, NULL, NULL, &tex_width, &tex_height))
913 {
914 printf("Failed to load textures\n");
915 fflush(stdout);
916 exit(1);
917 }
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700918
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600919 tex_obj->tex_width = tex_width;
920 tex_obj->tex_height = tex_height;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700921
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600922 const VkImageCreateInfo image_create_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600923 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700924 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -0600925 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700926 .format = tex_format,
927 .extent = { tex_width, tex_height, 1 },
928 .mipLevels = 1,
929 .arraySize = 1,
930 .samples = 1,
931 .tiling = tiling,
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -0600932 .usage = usage,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700933 .flags = 0,
934 };
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -0600935 VkMemoryAllocInfo mem_alloc = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600936 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
Mark Lobodzinskia6907f72015-04-16 08:52:00 -0500937 .pNext = NULL,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700938 .allocationSize = 0,
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600939 .memoryTypeIndex = 0,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700940 };
941
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500942 VkMemoryRequirements mem_reqs;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700943
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600944 err = vkCreateImage(demo->device, &image_create_info,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600945 &tex_obj->image);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700946 assert(!err);
947
Tony Barbourecf1b2b2015-06-24 16:06:58 -0600948 err = vkGetObjectMemoryRequirements(demo->device,
949 VK_OBJECT_TYPE_IMAGE, tex_obj->image, &mem_reqs);
950 assert(!err);
Piers Daniell735ee532015-02-23 16:23:13 -0700951
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500952 mem_alloc.allocationSize = mem_reqs.size;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700953
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600954 err = memory_type_from_properties(demo, mem_reqs.memoryTypeBits, mem_props, &mem_alloc.memoryTypeIndex);
955 assert(!err);
956
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500957 /* allocate memory */
958 err = vkAllocMemory(demo->device, &mem_alloc,
959 &(tex_obj->mem));
960 assert(!err);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700961
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500962 /* bind memory */
963 err = vkBindObjectMemory(demo->device,
964 VK_OBJECT_TYPE_IMAGE, tex_obj->image,
965 tex_obj->mem, 0);
966 assert(!err);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700967
Tony Barbour72304ef2015-04-16 15:59:00 -0600968 if (mem_props & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600969 const VkImageSubresource subres = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600970 .aspect = VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700971 .mipLevel = 0,
972 .arraySlice = 0,
973 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600974 VkSubresourceLayout layout;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700975 void *data;
976
Tony Barbourecf1b2b2015-06-24 16:06:58 -0600977 err = vkGetImageSubresourceLayout(demo->device, tex_obj->image, &subres, &layout);
978 assert(!err);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700979
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500980 err = vkMapMemory(demo->device, tex_obj->mem, 0, 0, 0, &data);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700981 assert(!err);
982
983 if (!loadTexture(filename, data, &layout, &tex_width, &tex_height)) {
984 fprintf(stderr, "Error loading texture: %s\n", filename);
985 }
986
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500987 err = vkUnmapMemory(demo->device, tex_obj->mem);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700988 assert(!err);
989 }
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600990
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600991 tex_obj->imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600992 demo_set_image_layout(demo, tex_obj->image,
malnasseeb25d3f2015-06-03 17:28:38 -0400993 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600994 VK_IMAGE_LAYOUT_UNDEFINED,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600995 tex_obj->imageLayout);
996 /* 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 -0700997}
998
Mark Lobodzinskida9b1092015-04-16 11:44:05 -0500999static void demo_destroy_texture_image(struct demo *demo, struct texture_object *tex_objs)
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001000{
1001 /* clean up staging resources */
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001002 vkFreeMemory(demo->device, tex_objs->mem);
Mike Stroyanebae8322015-04-17 12:36:38 -06001003 vkDestroyObject(demo->device, VK_OBJECT_TYPE_IMAGE, tex_objs->image);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001004}
1005
1006static void demo_prepare_textures(struct demo *demo)
1007{
Tony Barbour72304ef2015-04-16 15:59:00 -06001008 const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001009 VkFormatProperties props;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001010 VkResult U_ASSERT_ONLY err;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001011 uint32_t i;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001012
Chris Forbesf576a3e2015-06-21 22:55:02 +12001013 err = vkGetPhysicalDeviceFormatInfo(demo->gpu, tex_format, &props);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001014 assert(!err);
1015
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001016 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001017
FslNopper6a7e50e2015-05-06 21:42:01 +02001018 if ((props.linearTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) && !demo->use_staging_buffer) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001019 /* Device can texture using linear textures */
1020 demo_prepare_texture_image(demo, tex_files[i], &demo->textures[i],
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001021 VK_IMAGE_TILING_LINEAR, VK_IMAGE_USAGE_SAMPLED_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
Tony Barbour72304ef2015-04-16 15:59:00 -06001022 } else if (props.optimalTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001023 /* Must use staging buffer to copy linear texture to optimized */
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001024 struct texture_object staging_texture;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001025
1026 memset(&staging_texture, 0, sizeof(staging_texture));
1027 demo_prepare_texture_image(demo, tex_files[i], &staging_texture,
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001028 VK_IMAGE_TILING_LINEAR, VK_IMAGE_USAGE_TRANSFER_SOURCE_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001029
1030 demo_prepare_texture_image(demo, tex_files[i], &demo->textures[i],
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001031 VK_IMAGE_TILING_OPTIMAL,
1032 (VK_IMAGE_USAGE_TRANSFER_DESTINATION_BIT | VK_IMAGE_USAGE_SAMPLED_BIT),
1033 VK_MEMORY_PROPERTY_DEVICE_ONLY);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001034
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001035 demo_set_image_layout(demo, staging_texture.image,
malnasseeb25d3f2015-06-03 17:28:38 -04001036 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001037 staging_texture.imageLayout,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001038 VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001039
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001040 demo_set_image_layout(demo, demo->textures[i].image,
malnasseeb25d3f2015-06-03 17:28:38 -04001041 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001042 demo->textures[i].imageLayout,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001043 VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001044
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001045 VkImageCopy copy_region = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001046 .srcSubresource = { VK_IMAGE_ASPECT_COLOR, 0, 0 },
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001047 .srcOffset = { 0, 0, 0 },
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001048 .destSubresource = { VK_IMAGE_ASPECT_COLOR, 0, 0 },
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001049 .destOffset = { 0, 0, 0 },
1050 .extent = { staging_texture.tex_width, staging_texture.tex_height, 1 },
1051 };
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001052 vkCmdCopyImage(demo->cmd,
1053 staging_texture.image, VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL,
1054 demo->textures[i].image, VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL,
Courtney Goeltzenleuchter8e89a312015-03-25 11:25:10 -06001055 1, &copy_region);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001056
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001057 demo_set_image_layout(demo, demo->textures[i].image,
malnasseeb25d3f2015-06-03 17:28:38 -04001058 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001059 VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001060 demo->textures[i].imageLayout);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001061
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001062 demo_flush_init_cmd(demo);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001063
Courtney Goeltzenleuchterf3aeb2b2015-04-21 09:30:03 -06001064 demo_destroy_texture_image(demo, &staging_texture);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001065 } else {
Mike Stroyan8b89e072015-06-15 14:21:03 -06001066 /* Can't support VK_FORMAT_R8G8B8A8_UNORM !? */
1067 assert(!"No support for R8G8B8A8_UNORM as texture image format");
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001068 }
1069
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001070 const VkSamplerCreateInfo sampler = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001071 .sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001072 .pNext = NULL,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001073 .magFilter = VK_TEX_FILTER_NEAREST,
1074 .minFilter = VK_TEX_FILTER_NEAREST,
Tony Barbour72304ef2015-04-16 15:59:00 -06001075 .mipMode = VK_TEX_MIPMAP_MODE_BASE,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001076 .addressU = VK_TEX_ADDRESS_CLAMP,
1077 .addressV = VK_TEX_ADDRESS_CLAMP,
1078 .addressW = VK_TEX_ADDRESS_CLAMP,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001079 .mipLodBias = 0.0f,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001080 .maxAnisotropy = 1,
Tony Barbour72304ef2015-04-16 15:59:00 -06001081 .compareOp = VK_COMPARE_OP_NEVER,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001082 .minLod = 0.0f,
1083 .maxLod = 0.0f,
Tony Barbourcb530c72015-06-25 16:56:44 -06001084 .borderColor = VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001085 };
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001086
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001087 VkImageViewCreateInfo view = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001088 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001089 .pNext = NULL,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001090 .image = VK_NULL_HANDLE,
Tony Barbour72304ef2015-04-16 15:59:00 -06001091 .viewType = VK_IMAGE_VIEW_TYPE_2D,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001092 .format = tex_format,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001093 .channels = { VK_CHANNEL_SWIZZLE_R,
1094 VK_CHANNEL_SWIZZLE_G,
1095 VK_CHANNEL_SWIZZLE_B,
1096 VK_CHANNEL_SWIZZLE_A, },
1097 .subresourceRange = { VK_IMAGE_ASPECT_COLOR, 0, 1, 0, 1 },
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001098 };
Jon Ashburnb2a66652015-01-16 09:37:43 -07001099
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001100 /* create sampler */
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001101 err = vkCreateSampler(demo->device, &sampler,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001102 &demo->textures[i].sampler);
1103 assert(!err);
1104
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001105 /* create image view */
1106 view.image = demo->textures[i].image;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001107 err = vkCreateImageView(demo->device, &view,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001108 &demo->textures[i].view);
1109 assert(!err);
1110 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001111}
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001112
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001113void demo_prepare_cube_data_buffer(struct demo *demo)
1114{
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -06001115 VkBufferCreateInfo buf_info;
1116 VkBufferViewCreateInfo view_info;
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -06001117 VkMemoryAllocInfo alloc_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001118 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
Mark Lobodzinskia6907f72015-04-16 08:52:00 -05001119 .pNext = NULL,
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001120 .allocationSize = 0,
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06001121 .memoryTypeIndex = 0,
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001122 };
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001123 VkMemoryRequirements mem_reqs;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001124 size_t mem_reqs_size = sizeof(VkMemoryRequirements);
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001125 uint8_t *pData;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001126 int i;
1127 mat4x4 MVP, VP;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001128 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001129 struct vktexcube_vs_uniform data;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001130
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001131 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001132 mat4x4_mul(MVP, VP, demo->model_matrix);
1133 memcpy(data.mvp, MVP, sizeof(MVP));
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001134// dumpMatrix("MVP", MVP);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001135
1136 for (i=0; i<12*3; i++) {
1137 data.position[i][0] = g_vertex_buffer_data[i*3];
1138 data.position[i][1] = g_vertex_buffer_data[i*3+1];
1139 data.position[i][2] = g_vertex_buffer_data[i*3+2];
1140 data.position[i][3] = 1.0f;
1141 data.attr[i][0] = g_uv_buffer_data[2*i];
1142 data.attr[i][1] = g_uv_buffer_data[2*i + 1];
1143 data.attr[i][2] = 0;
1144 data.attr[i][3] = 0;
1145 }
1146
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001147 memset(&buf_info, 0, sizeof(buf_info));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001148 buf_info.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -06001149 buf_info.usage = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001150 err = vkCreateBuffer(demo->device, &buf_info, &demo->uniform_data.buf);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001151 assert(!err);
1152
Tony Barbourecf1b2b2015-06-24 16:06:58 -06001153 err = vkGetObjectMemoryRequirements(demo->device,
1154 VK_OBJECT_TYPE_BUFFER, demo->uniform_data.buf, &mem_reqs);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001155 assert(!err && mem_reqs_size == sizeof(mem_reqs));
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001156
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001157 alloc_info.allocationSize = mem_reqs.size;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06001158 err = memory_type_from_properties(demo,
1159 mem_reqs.memoryTypeBits,
1160 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT,
1161 &alloc_info.memoryTypeIndex);
1162 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001163
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001164 err = vkAllocMemory(demo->device, &alloc_info, &(demo->uniform_data.mem));
1165 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001166
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001167 err = vkMapMemory(demo->device, demo->uniform_data.mem, 0, 0, 0, (void **) &pData);
1168 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001169
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001170 memcpy(pData, &data, sizeof data);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001171
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001172 err = vkUnmapMemory(demo->device, demo->uniform_data.mem);
1173 assert(!err);
1174
1175 err = vkBindObjectMemory(demo->device,
1176 VK_OBJECT_TYPE_BUFFER, demo->uniform_data.buf,
1177 demo->uniform_data.mem, 0);
1178 assert(!err);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001179
1180 memset(&view_info, 0, sizeof(view_info));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001181 view_info.sType = VK_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO;
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001182 view_info.buffer = demo->uniform_data.buf;
Tony Barbour72304ef2015-04-16 15:59:00 -06001183 view_info.viewType = VK_BUFFER_VIEW_TYPE_RAW;
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001184 view_info.offset = 0;
1185 view_info.range = sizeof(data);
1186
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001187 err = vkCreateBufferView(demo->device, &view_info, &demo->uniform_data.view);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001188 assert(!err);
1189
Chia-I Wuae721ba2015-05-25 16:27:55 +08001190 demo->uniform_data.desc.bufferView = demo->uniform_data.view;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001191}
1192
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001193static void demo_prepare_descriptor_layout(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001194{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001195 const VkDescriptorSetLayoutBinding layout_bindings[2] = {
Chia-I Wua2aa8632015-03-26 15:04:41 +08001196 [0] = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001197 .descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
Chia-I Wu6d29a412015-05-25 16:22:52 +08001198 .arraySize = 1,
Tony Barbour72304ef2015-04-16 15:59:00 -06001199 .stageFlags = VK_SHADER_STAGE_VERTEX_BIT,
Chia-I Wu91e8e212015-03-27 12:56:09 +08001200 .pImmutableSamplers = NULL,
Chia-I Wua2aa8632015-03-26 15:04:41 +08001201 },
1202 [1] = {
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -06001203 .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
Chia-I Wu6d29a412015-05-25 16:22:52 +08001204 .arraySize = DEMO_TEXTURE_COUNT,
Tony Barbour72304ef2015-04-16 15:59:00 -06001205 .stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT,
Chia-I Wu91e8e212015-03-27 12:56:09 +08001206 .pImmutableSamplers = NULL,
Chia-I Wua2aa8632015-03-26 15:04:41 +08001207 },
1208 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001209 const VkDescriptorSetLayoutCreateInfo descriptor_layout = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001210 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001211 .pNext = NULL,
Chia-I Wua2aa8632015-03-26 15:04:41 +08001212 .count = 2,
1213 .pBinding = layout_bindings,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001214 };
Tony Barbourfdc2d352015-04-22 09:02:32 -06001215 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001216
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001217 err = vkCreateDescriptorSetLayout(demo->device,
Chia-I Wub58c24a2015-03-26 15:27:55 +08001218 &descriptor_layout, &demo->desc_layout);
1219 assert(!err);
1220
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001221 const VkPipelineLayoutCreateInfo pPipelineLayoutCreateInfo = {
1222 .sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
1223 .pNext = NULL,
1224 .descriptorSetCount = 1,
1225 .pSetLayouts = &demo->desc_layout,
1226 };
1227
1228 err = vkCreatePipelineLayout(demo->device,
1229 &pPipelineLayoutCreateInfo,
1230 &demo->pipeline_layout);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001231 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001232}
1233
Tobin Ehlisd1f17ac2015-07-02 11:02:49 -06001234static VkShader demo_prepare_shader(struct demo* demo,
Tony Barbour72304ef2015-04-16 15:59:00 -06001235 VkShaderStage stage,
Tobin Ehlisd1f17ac2015-07-02 11:02:49 -06001236 const void* code,
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001237 size_t size)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001238{
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001239 VkShaderModuleCreateInfo moduleCreateInfo;
1240 VkShaderCreateInfo shaderCreateInfo;
1241 VkShaderModule shaderModule;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001242 VkShader shader;
1243 VkResult err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001244
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001245
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001246 moduleCreateInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
1247 moduleCreateInfo.pNext = NULL;
1248
1249 shaderCreateInfo.sType = VK_STRUCTURE_TYPE_SHADER_CREATE_INFO;
1250 shaderCreateInfo.pNext = NULL;
Tobin Ehlisd1f17ac2015-07-02 11:02:49 -06001251 shaderCreateInfo.pName = "main";
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001252
Cody Northrop1fedb212015-05-28 11:27:16 -06001253 if (!demo->use_glsl) {
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001254 moduleCreateInfo.codeSize = size;
1255 moduleCreateInfo.pCode = code;
1256 moduleCreateInfo.flags = 0;
1257 err = vkCreateShaderModule(demo->device, &moduleCreateInfo, &shaderModule);
Cody Northrop1fedb212015-05-28 11:27:16 -06001258 if (err) {
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001259 free((void *) moduleCreateInfo.pCode);
Cody Northrop1fedb212015-05-28 11:27:16 -06001260 }
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001261
1262 shaderCreateInfo.flags = 0;
1263 shaderCreateInfo.module = shaderModule;
1264 err = vkCreateShader(demo->device, &shaderCreateInfo, &shader);
Cody Northrop1fedb212015-05-28 11:27:16 -06001265 } else {
1266 // Create fake SPV structure to feed GLSL
1267 // to the driver "under the covers"
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001268 moduleCreateInfo.codeSize = 3 * sizeof(uint32_t) + size + 1;
1269 moduleCreateInfo.pCode = malloc(moduleCreateInfo.codeSize);
1270 moduleCreateInfo.flags = 0;
Cody Northrop1fedb212015-05-28 11:27:16 -06001271
1272 /* try version 0 first: VkShaderStage followed by GLSL */
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001273 ((uint32_t *) moduleCreateInfo.pCode)[0] = ICD_SPV_MAGIC;
1274 ((uint32_t *) moduleCreateInfo.pCode)[1] = 0;
1275 ((uint32_t *) moduleCreateInfo.pCode)[2] = stage;
1276 memcpy(((uint32_t *) moduleCreateInfo.pCode + 3), code, size + 1);
Cody Northrop1fedb212015-05-28 11:27:16 -06001277
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001278 err = vkCreateShaderModule(demo->device, &moduleCreateInfo, &shaderModule);
Cody Northrop1fedb212015-05-28 11:27:16 -06001279 if (err) {
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001280 free((void *) moduleCreateInfo.pCode);
Cody Northrop1fedb212015-05-28 11:27:16 -06001281 }
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001282
1283 shaderCreateInfo.flags = 0;
1284 shaderCreateInfo.module = shaderModule;
1285 err = vkCreateShader(demo->device, &shaderCreateInfo, &shader);
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001286 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001287 return shader;
1288}
1289
Cody Northrop8b12a1f2015-03-17 15:55:58 -06001290char *demo_read_spv(const char *filename, size_t *psize)
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001291{
1292 long int size;
Tony Barbourf9921732015-04-22 11:36:22 -06001293 size_t U_ASSERT_ONLY retval;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001294 void *shader_code;
1295
1296 FILE *fp = fopen(filename, "rb");
1297 if (!fp) return NULL;
1298
1299 fseek(fp, 0L, SEEK_END);
1300 size = ftell(fp);
1301
1302 fseek(fp, 0L, SEEK_SET);
1303
1304 shader_code = malloc(size);
Tony Barbourfdc2d352015-04-22 09:02:32 -06001305 retval = fread(shader_code, size, 1, fp);
1306 assert(retval == 1);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001307
1308 *psize = size;
1309
1310 return shader_code;
1311}
1312
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001313static VkShader demo_prepare_vs(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001314{
Cody Northrop1fedb212015-05-28 11:27:16 -06001315 if (!demo->use_glsl) {
1316 void *vertShaderCode;
1317 size_t size;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001318
Cody Northrop1fedb212015-05-28 11:27:16 -06001319 vertShaderCode = demo_read_spv("cube-vert.spv", &size);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001320
Cody Northrop1fedb212015-05-28 11:27:16 -06001321 return demo_prepare_shader(demo, VK_SHADER_STAGE_VERTEX,
1322 vertShaderCode, size);
1323 } else {
1324 static const char *vertShaderText =
1325 "#version 140\n"
1326 "#extension GL_ARB_separate_shader_objects : enable\n"
1327 "#extension GL_ARB_shading_language_420pack : enable\n"
1328 "\n"
1329 "layout(binding = 0) uniform buf {\n"
1330 " mat4 MVP;\n"
1331 " vec4 position[12*3];\n"
1332 " vec4 attr[12*3];\n"
1333 "} ubuf;\n"
1334 "\n"
1335 "layout (location = 0) out vec4 texcoord;\n"
1336 "\n"
1337 "void main() \n"
1338 "{\n"
1339 " texcoord = ubuf.attr[gl_VertexID];\n"
1340 " gl_Position = ubuf.MVP * ubuf.position[gl_VertexID];\n"
1341 "\n"
1342 " // GL->VK conventions\n"
1343 " gl_Position.y = -gl_Position.y;\n"
1344 " gl_Position.z = (gl_Position.z + gl_Position.w) / 2.0;\n"
1345 "}\n";
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001346
Cody Northrop1fedb212015-05-28 11:27:16 -06001347 return demo_prepare_shader(demo, VK_SHADER_STAGE_VERTEX,
1348 (const void *) vertShaderText,
1349 strlen(vertShaderText));
1350 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001351}
1352
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001353static VkShader demo_prepare_fs(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001354{
Cody Northrop1fedb212015-05-28 11:27:16 -06001355 if (!demo->use_glsl) {
1356 void *fragShaderCode;
1357 size_t size;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001358
Cody Northrop1fedb212015-05-28 11:27:16 -06001359 fragShaderCode = demo_read_spv("cube-frag.spv", &size);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001360
Cody Northrop1fedb212015-05-28 11:27:16 -06001361 return demo_prepare_shader(demo, VK_SHADER_STAGE_FRAGMENT,
1362 fragShaderCode, size);
1363 } else {
1364 static const char *fragShaderText =
1365 "#version 140\n"
1366 "#extension GL_ARB_separate_shader_objects : enable\n"
1367 "#extension GL_ARB_shading_language_420pack : enable\n"
1368 "layout (binding = 1) uniform sampler2D tex;\n"
1369 "\n"
1370 "layout (location = 0) in vec4 texcoord;\n"
1371 "layout (location = 0) out vec4 uFragColor;\n"
1372 "void main() {\n"
1373 " uFragColor = texture(tex, texcoord.xy);\n"
1374 "}\n";
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001375
Cody Northrop1fedb212015-05-28 11:27:16 -06001376 return demo_prepare_shader(demo, VK_SHADER_STAGE_FRAGMENT,
1377 (const void *) fragShaderText,
1378 strlen(fragShaderText));
1379 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001380}
1381
1382static void demo_prepare_pipeline(struct demo *demo)
1383{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001384 VkGraphicsPipelineCreateInfo pipeline;
Mark Lobodzinski521d7762015-06-23 15:11:57 -06001385
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001386 VkPipelineIaStateCreateInfo ia;
1387 VkPipelineRsStateCreateInfo rs;
1388 VkPipelineCbStateCreateInfo cb;
1389 VkPipelineDsStateCreateInfo ds;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001390 VkPipelineVpStateCreateInfo vp;
1391 VkPipelineMsStateCreateInfo ms;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001392 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001393
1394 memset(&pipeline, 0, sizeof(pipeline));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001395 pipeline.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001396 pipeline.layout = demo->pipeline_layout;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001397
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001398 memset(&ia, 0, sizeof(ia));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001399 ia.sType = VK_STRUCTURE_TYPE_PIPELINE_IA_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06001400 ia.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001401
1402 memset(&rs, 0, sizeof(rs));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001403 rs.sType = VK_STRUCTURE_TYPE_PIPELINE_RS_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06001404 rs.fillMode = VK_FILL_MODE_SOLID;
1405 rs.cullMode = VK_CULL_MODE_BACK;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001406 rs.frontFace = VK_FRONT_FACE_CCW;
Chia-I Wubb67e6e2015-04-22 14:20:52 +08001407 rs.depthClipEnable = VK_TRUE;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001408
1409 memset(&cb, 0, sizeof(cb));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001410 cb.sType = VK_STRUCTURE_TYPE_PIPELINE_CB_STATE_CREATE_INFO;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001411 VkPipelineCbAttachmentState att_state[1];
Tony Barbour29645d02015-01-16 14:27:35 -07001412 memset(att_state, 0, sizeof(att_state));
1413 att_state[0].format = demo->format;
1414 att_state[0].channelWriteMask = 0xf;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001415 att_state[0].blendEnable = VK_FALSE;
Tony Barbour29645d02015-01-16 14:27:35 -07001416 cb.attachmentCount = 1;
1417 cb.pAttachments = att_state;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001418
Tony Barbour29645d02015-01-16 14:27:35 -07001419 memset(&vp, 0, sizeof(vp));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001420 vp.sType = VK_STRUCTURE_TYPE_PIPELINE_VP_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06001421 vp.viewportCount = 1;
Tony Barbour29645d02015-01-16 14:27:35 -07001422
1423 memset(&ds, 0, sizeof(ds));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001424 ds.sType = VK_STRUCTURE_TYPE_PIPELINE_DS_STATE_CREATE_INFO;
Tony Barbour29645d02015-01-16 14:27:35 -07001425 ds.format = demo->depth.format;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001426 ds.depthTestEnable = VK_TRUE;
1427 ds.depthWriteEnable = VK_TRUE;
Tony Barbour72304ef2015-04-16 15:59:00 -06001428 ds.depthCompareOp = VK_COMPARE_OP_LESS_EQUAL;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001429 ds.depthBoundsEnable = VK_FALSE;
1430 ds.back.stencilFailOp = VK_STENCIL_OP_KEEP;
1431 ds.back.stencilPassOp = VK_STENCIL_OP_KEEP;
Tony Barbour72304ef2015-04-16 15:59:00 -06001432 ds.back.stencilCompareOp = VK_COMPARE_OP_ALWAYS;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001433 ds.stencilTestEnable = VK_FALSE;
Tony Barbour29645d02015-01-16 14:27:35 -07001434 ds.front = ds.back;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001435
Tony Barbour29645d02015-01-16 14:27:35 -07001436 memset(&ms, 0, sizeof(ms));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001437 ms.sType = VK_STRUCTURE_TYPE_PIPELINE_MS_STATE_CREATE_INFO;
Tony Barbour29645d02015-01-16 14:27:35 -07001438 ms.sampleMask = 1;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001439 ms.multisampleEnable = VK_FALSE;
Tony Barbour3ca22502015-06-26 10:18:34 -06001440 ms.rasterSamples = 1;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001441
Mark Lobodzinski521d7762015-06-23 15:11:57 -06001442 // Two stages: vs and fs
1443 pipeline.stageCount = 2;
1444 VkPipelineShaderStageCreateInfo shaderStages[2];
1445 memset(&shaderStages, 0, 2 * sizeof(VkPipelineShaderStageCreateInfo));
1446
1447 shaderStages[0].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1448 shaderStages[0].stage = VK_SHADER_STAGE_VERTEX;
1449 shaderStages[0].shader = demo_prepare_vs(demo);
1450
1451 shaderStages[1].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1452 shaderStages[1].stage = VK_SHADER_STAGE_FRAGMENT;
1453 shaderStages[1].shader = demo_prepare_fs(demo);
1454
Mark Lobodzinski15169cf2015-06-30 10:18:36 -06001455 pipeline.pVertexInputState = NULL;
Mark Lobodzinski521d7762015-06-23 15:11:57 -06001456 pipeline.pIaState = &ia;
1457 pipeline.pRsState = &rs;
1458 pipeline.pCbState = &cb;
1459 pipeline.pMsState = &ms;
1460 pipeline.pVpState = &vp;
1461 pipeline.pDsState = &ds;
1462 pipeline.pStages = shaderStages;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001463
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001464 err = vkCreateGraphicsPipeline(demo->device, &pipeline, &demo->pipeline);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001465 assert(!err);
1466
Mark Lobodzinski521d7762015-06-23 15:11:57 -06001467 for (uint32_t i = 0; i < pipeline.stageCount; i++) {
1468 vkDestroyObject(demo->device, VK_OBJECT_TYPE_SHADER, shaderStages[i].shader);
1469 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001470}
1471
1472static void demo_prepare_dynamic_states(struct demo *demo)
1473{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001474 VkDynamicVpStateCreateInfo viewport_create;
1475 VkDynamicRsStateCreateInfo raster;
1476 VkDynamicCbStateCreateInfo color_blend;
1477 VkDynamicDsStateCreateInfo depth_stencil;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001478 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001479
Tony Barbour29645d02015-01-16 14:27:35 -07001480 memset(&viewport_create, 0, sizeof(viewport_create));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001481 viewport_create.sType = VK_STRUCTURE_TYPE_DYNAMIC_VP_STATE_CREATE_INFO;
Courtney Goeltzenleuchter1710d8d2015-02-11 14:13:34 -07001482 viewport_create.viewportAndScissorCount = 1;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001483 VkViewport viewport;
Piers Daniell735ee532015-02-23 16:23:13 -07001484 memset(&viewport, 0, sizeof(viewport));
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001485 viewport.height = (float) demo->height;
1486 viewport.width = (float) demo->width;
1487 viewport.minDepth = (float) 0.0f;
1488 viewport.maxDepth = (float) 1.0f;
Piers Daniell735ee532015-02-23 16:23:13 -07001489 viewport_create.pViewports = &viewport;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001490 VkRect scissor;
Piers Daniell735ee532015-02-23 16:23:13 -07001491 memset(&scissor, 0, sizeof(scissor));
Courtney Goeltzenleuchter1710d8d2015-02-11 14:13:34 -07001492 scissor.extent.width = demo->width;
1493 scissor.extent.height = demo->height;
1494 scissor.offset.x = 0;
1495 scissor.offset.y = 0;
Courtney Goeltzenleuchter1710d8d2015-02-11 14:13:34 -07001496 viewport_create.pScissors = &scissor;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001497
1498 memset(&raster, 0, sizeof(raster));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001499 raster.sType = VK_STRUCTURE_TYPE_DYNAMIC_RS_STATE_CREATE_INFO;
Piers Daniell735ee532015-02-23 16:23:13 -07001500 raster.lineWidth = 1.0;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001501
1502 memset(&color_blend, 0, sizeof(color_blend));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001503 color_blend.sType = VK_STRUCTURE_TYPE_DYNAMIC_CB_STATE_CREATE_INFO;
Piers Daniell735ee532015-02-23 16:23:13 -07001504 color_blend.blendConst[0] = 1.0f;
1505 color_blend.blendConst[1] = 1.0f;
1506 color_blend.blendConst[2] = 1.0f;
1507 color_blend.blendConst[3] = 1.0f;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001508
1509 memset(&depth_stencil, 0, sizeof(depth_stencil));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001510 depth_stencil.sType = VK_STRUCTURE_TYPE_DYNAMIC_DS_STATE_CREATE_INFO;
Mark Lobodzinski89d32b12015-06-12 11:14:17 -06001511 depth_stencil.minDepthBounds = 0.0f;
1512 depth_stencil.maxDepthBounds = 1.0f;
Tony Barbour29645d02015-01-16 14:27:35 -07001513 depth_stencil.stencilBackRef = 0;
1514 depth_stencil.stencilFrontRef = 0;
1515 depth_stencil.stencilReadMask = 0xff;
1516 depth_stencil.stencilWriteMask = 0xff;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001517
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001518 err = vkCreateDynamicViewportState(demo->device, &viewport_create, &demo->viewport);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001519 assert(!err);
1520
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001521 err = vkCreateDynamicRasterState(demo->device, &raster, &demo->raster);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001522 assert(!err);
1523
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001524 err = vkCreateDynamicColorBlendState(demo->device,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001525 &color_blend, &demo->color_blend);
1526 assert(!err);
1527
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001528 err = vkCreateDynamicDepthStencilState(demo->device,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001529 &depth_stencil, &demo->depth_stencil);
1530 assert(!err);
1531}
1532
Chia-I Wu63ea9262015-03-26 13:14:16 +08001533static void demo_prepare_descriptor_pool(struct demo *demo)
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001534{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001535 const VkDescriptorTypeCount type_counts[2] = {
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001536 [0] = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001537 .type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001538 .count = 1,
1539 },
1540 [1] = {
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -06001541 .type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001542 .count = DEMO_TEXTURE_COUNT,
1543 },
1544 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001545 const VkDescriptorPoolCreateInfo descriptor_pool = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001546 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001547 .pNext = NULL,
1548 .count = 2,
1549 .pTypeCount = type_counts,
1550 };
Tony Barbourfdc2d352015-04-22 09:02:32 -06001551 VkResult U_ASSERT_ONLY err;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001552
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001553 err = vkCreateDescriptorPool(demo->device,
1554 VK_DESCRIPTOR_POOL_USAGE_ONE_SHOT, 1,
Chia-I Wu63ea9262015-03-26 13:14:16 +08001555 &descriptor_pool, &demo->desc_pool);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001556 assert(!err);
1557}
1558
1559static void demo_prepare_descriptor_set(struct demo *demo)
1560{
Chia-I Wuae721ba2015-05-25 16:27:55 +08001561 VkDescriptorInfo tex_descs[DEMO_TEXTURE_COUNT];
1562 VkWriteDescriptorSet writes[2];
Tony Barbourfdc2d352015-04-22 09:02:32 -06001563 VkResult U_ASSERT_ONLY err;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001564 uint32_t count;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001565 uint32_t i;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001566
Mike Stroyanebae8322015-04-17 12:36:38 -06001567 err = vkAllocDescriptorSets(demo->device, demo->desc_pool,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001568 VK_DESCRIPTOR_SET_USAGE_STATIC,
Chia-I Wu87544e72015-02-23 10:41:08 -07001569 1, &demo->desc_layout,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001570 &demo->desc_set, &count);
1571 assert(!err && count == 1);
1572
Chia-I Wuae721ba2015-05-25 16:27:55 +08001573 memset(&tex_descs, 0, sizeof(tex_descs));
1574 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
1575 tex_descs[i].sampler = demo->textures[i].sampler;
1576 tex_descs[i].imageView = demo->textures[i].view;
1577 tex_descs[i].imageLayout = VK_IMAGE_LAYOUT_GENERAL;
1578 }
1579
1580 memset(&writes, 0, sizeof(writes));
1581
1582 writes[0].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
1583 writes[0].destSet = demo->desc_set;
1584 writes[0].count = 1;
1585 writes[0].descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
1586 writes[0].pDescriptors = &demo->uniform_data.desc;
1587
1588 writes[1].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
1589 writes[1].destSet = demo->desc_set;
1590 writes[1].destBinding = 1;
1591 writes[1].count = DEMO_TEXTURE_COUNT;
1592 writes[1].descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
1593 writes[1].pDescriptors = tex_descs;
1594
1595 err = vkUpdateDescriptorSets(demo->device, 2, writes, 0, NULL);
1596 assert(!err);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001597}
1598
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001599static void demo_prepare(struct demo *demo)
1600{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001601 const VkCmdBufferCreateInfo cmd = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001602 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001603 .pNext = NULL,
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07001604 .queueNodeIndex = demo->graphics_queue_node_index,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001605 .flags = 0,
1606 };
Tony Barbourfdc2d352015-04-22 09:02:32 -06001607 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001608
1609 demo_prepare_buffers(demo);
1610 demo_prepare_depth(demo);
1611 demo_prepare_textures(demo);
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001612 demo_prepare_cube_data_buffer(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001613
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001614 demo_prepare_descriptor_layout(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001615 demo_prepare_pipeline(demo);
1616 demo_prepare_dynamic_states(demo);
1617
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001618 for (int i = 0; i < DEMO_BUFFER_COUNT; i++) {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001619 err = vkCreateCommandBuffer(demo->device, &cmd, &demo->buffers[i].cmd);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001620 assert(!err);
1621 }
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001622
Chia-I Wu63ea9262015-03-26 13:14:16 +08001623 demo_prepare_descriptor_pool(demo);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001624 demo_prepare_descriptor_set(demo);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001625
1626
1627 for (int i = 0; i < DEMO_BUFFER_COUNT; i++) {
1628 demo->current_buffer = i;
1629 demo_draw_build_cmd(demo, demo->buffers[i].cmd);
1630 }
1631
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001632 /*
1633 * Prepare functions above may generate pipeline commands
1634 * that need to be flushed before beginning the render loop.
1635 */
1636 demo_flush_init_cmd(demo);
1637
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001638 demo->current_buffer = 0;
Jon Ashburn8d26c062015-04-24 09:46:24 -07001639 demo->prepared = true;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001640}
1641
David Pinedo2bb7c932015-06-18 17:03:14 -06001642static void demo_cleanup(struct demo *demo)
1643{
1644 uint32_t i;
1645
1646 demo->prepared = false;
1647
1648 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DESCRIPTOR_SET, demo->desc_set);
1649 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DESCRIPTOR_POOL, demo->desc_pool);
1650
1651 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DYNAMIC_VP_STATE, demo->viewport);
1652 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DYNAMIC_RS_STATE, demo->raster);
1653 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DYNAMIC_CB_STATE, demo->color_blend);
1654 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DYNAMIC_DS_STATE, demo->depth_stencil);
1655
1656 vkDestroyObject(demo->device, VK_OBJECT_TYPE_PIPELINE, demo->pipeline);
1657 vkDestroyObject(demo->device, VK_OBJECT_TYPE_PIPELINE_LAYOUT, demo->pipeline_layout);
1658 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DESCRIPTOR_SET_LAYOUT, demo->desc_layout);
1659
1660 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
1661 vkDestroyObject(demo->device, VK_OBJECT_TYPE_IMAGE_VIEW, demo->textures[i].view);
1662 vkDestroyObject(demo->device, VK_OBJECT_TYPE_IMAGE, demo->textures[i].image);
1663 vkFreeMemory(demo->device, demo->textures[i].mem);
1664 vkDestroyObject(demo->device, VK_OBJECT_TYPE_SAMPLER, demo->textures[i].sampler);
1665 }
1666 demo->fpDestroySwapChainWSI(demo->swap_chain);
1667
1668 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DEPTH_STENCIL_VIEW, demo->depth.view);
1669 vkDestroyObject(demo->device, VK_OBJECT_TYPE_IMAGE, demo->depth.image);
1670 vkFreeMemory(demo->device, demo->depth.mem);
1671
1672 vkDestroyObject(demo->device, VK_OBJECT_TYPE_BUFFER_VIEW, demo->uniform_data.view);
1673 vkDestroyObject(demo->device, VK_OBJECT_TYPE_BUFFER, demo->uniform_data.buf);
1674 vkFreeMemory(demo->device, demo->uniform_data.mem);
1675
1676 for (i = 0; i < DEMO_BUFFER_COUNT; i++) {
1677 vkDestroyObject(demo->device, VK_OBJECT_TYPE_COLOR_ATTACHMENT_VIEW, demo->buffers[i].view);
1678 vkDestroyObject(demo->device, VK_OBJECT_TYPE_COMMAND_BUFFER, demo->buffers[i].cmd);
1679 }
1680
1681 vkDestroyDevice(demo->device);
Tony Barbourd70b42c2015-06-30 14:14:19 -06001682 if (demo->validate) {
1683 demo->dbgDestroyMsgCallback(demo->inst, demo->msg_callback);
1684 }
David Pinedo2bb7c932015-06-18 17:03:14 -06001685 vkDestroyInstance(demo->inst);
1686
1687#ifndef _WIN32
1688 xcb_destroy_window(demo->connection, demo->window);
1689 xcb_disconnect(demo->connection);
1690#endif // _WIN32
1691}
1692
1693// On MS-Windows, make this a global, so it's available to WndProc()
1694struct demo demo;
1695
Ian Elliott639ca472015-04-16 15:23:05 -06001696#ifdef _WIN32
1697static void demo_run(struct demo *demo)
1698{
Courtney Goeltzenleuchterb7e22702015-04-27 14:56:34 -06001699 if (!demo->prepared)
1700 return;
Ian Elliott639ca472015-04-16 15:23:05 -06001701 // Wait for work to finish before updating MVP.
1702 vkDeviceWaitIdle(demo->device);
1703 demo_update_data_buffer(demo);
1704
1705 demo_draw(demo);
1706
1707 // Wait for work to finish before updating MVP.
1708 vkDeviceWaitIdle(demo->device);
Ian Elliott639ca472015-04-16 15:23:05 -06001709
David Pinedo2bb7c932015-06-18 17:03:14 -06001710 demo->curFrame++;
1711
1712 if (demo->frameCount != INT_MAX && demo->curFrame == demo->frameCount)
1713 {
1714 demo->quit=true;
1715 demo_cleanup(demo);
1716 ExitProcess(0);
1717 }
1718
1719}
Ian Elliott639ca472015-04-16 15:23:05 -06001720
1721// MS-Windows event handling function:
1722LRESULT CALLBACK WndProc(HWND hWnd,
1723 UINT uMsg,
1724 WPARAM wParam,
1725 LPARAM lParam)
1726{
Ian Elliott639ca472015-04-16 15:23:05 -06001727 switch(uMsg)
1728 {
Ian Elliott639ca472015-04-16 15:23:05 -06001729 case WM_CLOSE:
1730 PostQuitMessage(0);
Tony Barbour3dddd5d2015-04-29 16:19:20 -06001731 break;
Ian Elliott639ca472015-04-16 15:23:05 -06001732 case WM_PAINT:
1733 demo_run(&demo);
1734 return 0;
1735 default:
1736 break;
1737 }
1738 return (DefWindowProc(hWnd, uMsg, wParam, lParam));
1739}
1740
1741static void demo_create_window(struct demo *demo)
1742{
1743 WNDCLASSEX win_class;
1744
1745 // Initialize the window class structure:
1746 win_class.cbSize = sizeof(WNDCLASSEX);
1747 win_class.style = CS_HREDRAW | CS_VREDRAW;
1748 win_class.lpfnWndProc = WndProc;
1749 win_class.cbClsExtra = 0;
1750 win_class.cbWndExtra = 0;
1751 win_class.hInstance = demo->connection; // hInstance
1752 win_class.hIcon = LoadIcon(NULL, IDI_APPLICATION);
1753 win_class.hCursor = LoadCursor(NULL, IDC_ARROW);
1754 win_class.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
1755 win_class.lpszMenuName = NULL;
1756 win_class.lpszClassName = demo->name;
1757 win_class.hIconSm = LoadIcon(NULL, IDI_WINLOGO);
1758 // Register window class:
1759 if (!RegisterClassEx(&win_class)) {
1760 // It didn't work, so try to give a useful error:
1761 printf("Unexpected error trying to start the application!\n");
1762 fflush(stdout);
1763 exit(1);
1764 }
1765 // Create window with the registered class:
Mike Stroyanb46779e2015-06-15 14:20:13 -06001766 RECT wr = { 0, 0, demo->width, demo->height };
1767 AdjustWindowRect(&wr, WS_OVERLAPPEDWINDOW, FALSE);
Ian Elliott639ca472015-04-16 15:23:05 -06001768 demo->window = CreateWindowEx(0,
1769 demo->name, // class name
1770 demo->name, // app name
1771 WS_OVERLAPPEDWINDOW | // window style
1772 WS_VISIBLE |
1773 WS_SYSMENU,
1774 100,100, // x/y coords
Mike Stroyanb46779e2015-06-15 14:20:13 -06001775 wr.right-wr.left, // width
1776 wr.bottom-wr.top, // height
Ian Elliott639ca472015-04-16 15:23:05 -06001777 NULL, // handle to parent
1778 NULL, // handle to menu
1779 demo->connection, // hInstance
1780 NULL); // no extra parameters
1781 if (!demo->window) {
1782 // It didn't work, so try to give a useful error:
1783 printf("Cannot create a window in which to draw!\n");
1784 fflush(stdout);
1785 exit(1);
1786 }
1787}
1788#else // _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001789static void demo_handle_event(struct demo *demo,
1790 const xcb_generic_event_t *event)
1791{
Piers Daniell735ee532015-02-23 16:23:13 -07001792 uint8_t event_code = event->response_type & 0x7f;
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07001793 switch (event_code) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001794 case XCB_EXPOSE:
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07001795 // TODO: Resize window
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001796 break;
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07001797 case XCB_CLIENT_MESSAGE:
1798 if((*(xcb_client_message_event_t*)event).data.data32[0] ==
1799 (*demo->atom_wm_delete_window).atom) {
1800 demo->quit = true;
1801 }
1802 break;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001803 case XCB_KEY_RELEASE:
1804 {
1805 const xcb_key_release_event_t *key =
1806 (const xcb_key_release_event_t *) event;
1807
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001808 switch (key->detail) {
1809 case 0x9: // Escape
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001810 demo->quit = true;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001811 break;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001812 case 0x71: // left arrow key
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001813 demo->spin_angle += demo->spin_increment;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001814 break;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001815 case 0x72: // right arrow key
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001816 demo->spin_angle -= demo->spin_increment;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001817 break;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001818 case 0x41:
1819 demo->pause = !demo->pause;
Piers Daniell735ee532015-02-23 16:23:13 -07001820 break;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001821 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001822 }
1823 break;
1824 default:
1825 break;
1826 }
1827}
1828
1829static void demo_run(struct demo *demo)
1830{
1831 xcb_flush(demo->connection);
1832
1833 while (!demo->quit) {
1834 xcb_generic_event_t *event;
1835
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07001836 if (demo->pause) {
1837 event = xcb_wait_for_event(demo->connection);
1838 } else {
1839 event = xcb_poll_for_event(demo->connection);
1840 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001841 if (event) {
1842 demo_handle_event(demo, event);
1843 free(event);
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07001844 }
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07001845
1846 // Wait for work to finish before updating MVP.
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001847 vkDeviceWaitIdle(demo->device);
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07001848 demo_update_data_buffer(demo);
1849
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07001850 demo_draw(demo);
Courtney Goeltzenleuchter1454f3c2014-11-18 11:28:09 -07001851
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07001852 // Wait for work to finish before updating MVP.
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001853 vkDeviceWaitIdle(demo->device);
David Pinedo2bb7c932015-06-18 17:03:14 -06001854 demo->curFrame++;
1855 if (demo->frameCount != INT_MAX && demo->curFrame == demo->frameCount)
1856 demo->quit = true;
1857
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001858 }
1859}
1860
1861static void demo_create_window(struct demo *demo)
1862{
1863 uint32_t value_mask, value_list[32];
1864
1865 demo->window = xcb_generate_id(demo->connection);
1866
1867 value_mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK;
1868 value_list[0] = demo->screen->black_pixel;
1869 value_list[1] = XCB_EVENT_MASK_KEY_RELEASE |
1870 XCB_EVENT_MASK_EXPOSURE;
1871
1872 xcb_create_window(demo->connection,
1873 XCB_COPY_FROM_PARENT,
1874 demo->window, demo->screen->root,
1875 0, 0, demo->width, demo->height, 0,
1876 XCB_WINDOW_CLASS_INPUT_OUTPUT,
1877 demo->screen->root_visual,
1878 value_mask, value_list);
1879
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07001880 /* Magic code that will send notification when window is destroyed */
1881 xcb_intern_atom_cookie_t cookie = xcb_intern_atom(demo->connection, 1, 12,
1882 "WM_PROTOCOLS");
1883 xcb_intern_atom_reply_t* reply = xcb_intern_atom_reply(demo->connection, cookie, 0);
1884
1885 xcb_intern_atom_cookie_t cookie2 = xcb_intern_atom(demo->connection, 0, 16, "WM_DELETE_WINDOW");
1886 demo->atom_wm_delete_window = xcb_intern_atom_reply(demo->connection, cookie2, 0);
1887
1888 xcb_change_property(demo->connection, XCB_PROP_MODE_REPLACE,
1889 demo->window, (*reply).atom, 4, 32, 1,
1890 &(*demo->atom_wm_delete_window).atom);
1891 free(reply);
1892
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001893 xcb_map_window(demo->connection, demo->window);
David Pinedo2bb7c932015-06-18 17:03:14 -06001894
1895 // Force the x/y coordinates to 100,100 results are identical in consecutive runs
1896 const uint32_t coords[] = {100, 100};
1897 xcb_configure_window(demo->connection, demo->window,
1898 XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y, coords);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001899}
Ian Elliott639ca472015-04-16 15:23:05 -06001900#endif // _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001901
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001902static void demo_init_vk(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001903{
Tobin Ehlis99239dc2015-04-16 18:04:57 -06001904 VkResult err;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06001905 VkExtensionProperties *instance_extensions;
1906 uint32_t instance_extension_count = 0;
1907 VkExtensionProperties *device_extensions;
1908 uint32_t device_extension_count = 0;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06001909 uint32_t total_extension_count = 0;
Tony Barbourecf1b2b2015-06-24 16:06:58 -06001910 err = vkGetGlobalExtensionCount(&total_extension_count);
Tobin Ehlis99239dc2015-04-16 18:04:57 -06001911 assert(!err);
1912
1913 VkExtensionProperties extProp;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06001914 bool32_t WSIextFound = 0;
1915 instance_extensions = malloc(sizeof(VkExtensionProperties) * total_extension_count);
1916 device_extensions = malloc(sizeof(VkExtensionProperties) * total_extension_count);
1917 for (uint32_t i = 0; i < total_extension_count; i++) {
Tony Barbourecf1b2b2015-06-24 16:06:58 -06001918 err = vkGetGlobalExtensionProperties(i, &extProp);
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06001919 if (!strcmp("VK_WSI_LunarG", extProp.name)) {
1920 WSIextFound = 1;
1921 memcpy(&instance_extensions[instance_extension_count++], &extProp, sizeof(VkExtensionProperties));
1922 memcpy(&device_extensions[device_extension_count++], &extProp, sizeof(VkExtensionProperties));
1923 }
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06001924 if (!strcmp(DEBUG_REPORT_EXTENSION_NAME, extProp.name)) {
1925 memcpy(&instance_extensions[instance_extension_count++], &extProp, sizeof(VkExtensionProperties));
1926 }
Tony Barbourd70b42c2015-06-30 14:14:19 -06001927 if (demo->validate && !strcmp("Validation",extProp.name)) {
1928 memcpy(&instance_extensions[instance_extension_count++], &extProp, sizeof(VkExtensionProperties));
1929 memcpy(&device_extensions[device_extension_count++], &extProp, sizeof(VkExtensionProperties));
1930 }
Tobin Ehlis99239dc2015-04-16 18:04:57 -06001931 }
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06001932 if (!WSIextFound) {
Tony Barbourecf1b2b2015-06-24 16:06:58 -06001933 ERR_EXIT("vkGetGlobalExtensionProperties failed to find the "
Ian Elliott65152912015-04-28 13:22:33 -06001934 "\"VK_WSI_LunarG\" extension.\n\nDo you have a compatible "
1935 "Vulkan installable client driver (ICD) installed?\nPlease "
1936 "look at the Getting Started guide for additional "
1937 "information.\n",
1938 "vkCreateInstance Failure");
1939 }
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001940 const VkApplicationInfo app = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001941 .sType = VK_STRUCTURE_TYPE_APPLICATION_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001942 .pNext = NULL,
Ian Elliott44e33f72015-04-28 10:52:52 -06001943 .pAppName = APP_SHORT_NAME,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001944 .appVersion = 0,
Ian Elliott44e33f72015-04-28 10:52:52 -06001945 .pEngineName = APP_SHORT_NAME,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001946 .engineVersion = 0,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001947 .apiVersion = VK_API_VERSION,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001948 };
Tony Barbour3dddd5d2015-04-29 16:19:20 -06001949 VkInstanceCreateInfo inst_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001950 .sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
Jon Ashburnab46b362015-04-04 14:52:07 -06001951 .pNext = NULL,
1952 .pAppInfo = &app,
1953 .pAllocCb = NULL,
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06001954 .extensionCount = instance_extension_count,
1955 .pEnabledExtensions = instance_extensions,
Jon Ashburnab46b362015-04-04 14:52:07 -06001956 };
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -06001957 const VkDeviceQueueCreateInfo queue = {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001958 .queueNodeIndex = 0,
1959 .queueCount = 1,
1960 };
Ian Elliott097d9f32015-04-28 11:35:02 -06001961
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06001962 VkDeviceCreateInfo device = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001963 .sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001964 .pNext = NULL,
1965 .queueRecordCount = 1,
1966 .pRequestedQueues = &queue,
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06001967 .extensionCount = device_extension_count,
1968 .pEnabledExtensions = device_extensions,
Jon Ashburn7842d522015-05-18 09:06:15 -06001969 .flags = 0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001970 };
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001971 uint32_t gpu_count;
1972 uint32_t i;
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07001973 uint32_t queue_count;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001974
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001975 err = vkCreateInstance(&inst_info, &demo->inst);
Ian Elliott07264132015-04-28 11:35:02 -06001976 if (err == VK_ERROR_INCOMPATIBLE_DRIVER) {
1977 ERR_EXIT("Cannot find a compatible Vulkan installable client driver "
Ian Elliott65152912015-04-28 13:22:33 -06001978 "(ICD).\n\nPlease look at the Getting Started guide for "
Ian Elliott07264132015-04-28 11:35:02 -06001979 "additional information.\n",
1980 "vkCreateInstance Failure");
Tony Barbour3dddd5d2015-04-29 16:19:20 -06001981 } else if (err == VK_ERROR_INVALID_EXTENSION) {
1982 ERR_EXIT("Cannot find a specified extension library"
1983 ".\nMake sure your layers path is set appropriately\n",
1984 "vkCreateInstance Failure");
Ian Elliott07264132015-04-28 11:35:02 -06001985 } else if (err) {
Ian Elliott65152912015-04-28 13:22:33 -06001986 ERR_EXIT("vkCreateInstance failed.\n\nDo you have a compatible Vulkan "
1987 "installable client driver (ICD) installed?\nPlease look at "
Ian Elliott07264132015-04-28 11:35:02 -06001988 "the Getting Started guide for additional information.\n",
1989 "vkCreateInstance Failure");
Ian Elliott3979e282015-04-03 15:24:55 -06001990 }
Jon Ashburnab46b362015-04-04 14:52:07 -06001991
Tony Barbour3dddd5d2015-04-29 16:19:20 -06001992
Jon Ashburn07c0c0c2015-04-15 11:31:12 -06001993 gpu_count = 1;
1994 err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, &demo->gpu);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001995 assert(!err && gpu_count == 1);
1996
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06001997
Tony Barbour3dddd5d2015-04-29 16:19:20 -06001998 if (demo->validate) {
Tony Barbourd70b42c2015-06-30 14:14:19 -06001999 demo->dbgCreateMsgCallback = vkGetInstanceProcAddr(demo->inst, "vkDbgCreateMsgCallback");
2000 demo->dbgDestroyMsgCallback = vkGetInstanceProcAddr(demo->inst, "vkDbgDestroyMsgCallback");
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002001 if (!demo->dbgCreateMsgCallback) {
2002 ERR_EXIT("GetProcAddr: Unable to find vkDbgCreateMsgCallback\n",
2003 "vkGetProcAddr Failure");
2004 }
Tony Barbourd70b42c2015-06-30 14:14:19 -06002005 if (!demo->dbgDestroyMsgCallback) {
2006 ERR_EXIT("GetProcAddr: Unable to find vkDbgDestroyMsgCallback\n",
2007 "vkGetProcAddr Failure");
2008 }
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002009 err = demo->dbgCreateMsgCallback(
2010 demo->inst,
2011 VK_DBG_REPORT_ERROR_BIT | VK_DBG_REPORT_WARN_BIT,
2012 dbgFunc, NULL,
2013 &demo->msg_callback);
2014 switch (err) {
2015 case VK_SUCCESS:
2016 break;
2017 case VK_ERROR_INVALID_POINTER:
2018 ERR_EXIT("dbgCreateMsgCallback: Invalid pointer\n",
2019 "dbgCreateMsgCallback Failure");
2020 break;
2021 case VK_ERROR_OUT_OF_HOST_MEMORY:
2022 ERR_EXIT("dbgCreateMsgCallback: out of host memory\n",
2023 "dbgCreateMsgCallback Failure");
2024 break;
2025 default:
2026 ERR_EXIT("dbgCreateMsgCallback: unknown failure\n",
2027 "dbgCreateMsgCallback Failure");
2028 break;
2029 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002030 }
2031
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002032 err = vkCreateDevice(demo->gpu, &device, &demo->device);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002033 assert(!err);
2034
Ian Elliott673898b2015-06-22 15:07:49 -06002035 GET_DEVICE_PROC_ADDR(demo->device, CreateSwapChainWSI);
2036 GET_DEVICE_PROC_ADDR(demo->device, CreateSwapChainWSI);
2037 GET_DEVICE_PROC_ADDR(demo->device, DestroySwapChainWSI);
2038 GET_DEVICE_PROC_ADDR(demo->device, GetSwapChainInfoWSI);
2039 GET_DEVICE_PROC_ADDR(demo->device, QueuePresentWSI);
Jon Ashburn0b85d052015-05-21 18:13:33 -06002040
Tony Barbourecf1b2b2015-06-24 16:06:58 -06002041 err = vkGetPhysicalDeviceProperties(demo->gpu, &demo->gpu_props);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06002042 assert(!err);
2043
Tony Barbourecf1b2b2015-06-24 16:06:58 -06002044 err = vkGetPhysicalDeviceQueueCount(demo->gpu, &queue_count);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06002045 assert(!err);
2046
Tony Barbourecf1b2b2015-06-24 16:06:58 -06002047 demo->queue_props = (VkPhysicalDeviceQueueProperties *) malloc(queue_count * sizeof(VkPhysicalDeviceQueueProperties));
2048 err = vkGetPhysicalDeviceQueueProperties(demo->gpu, queue_count, demo->queue_props);
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07002049 assert(!err);
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07002050 assert(queue_count >= 1);
2051
Mark Lobodzinskida9b1092015-04-16 11:44:05 -05002052 // Graphics queue and MemMgr queue can be separate.
2053 // TODO: Add support for separate queues, including synchronization,
Mark Lobodzinski6baaec72015-05-11 17:21:15 -05002054 // and appropriate tracking for QueueSubmit
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07002055 for (i = 0; i < queue_count; i++) {
Mark Lobodzinski6baaec72015-05-11 17:21:15 -05002056 if (demo->queue_props[i].queueFlags & VK_QUEUE_GRAPHICS_BIT)
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07002057 break;
2058 }
2059 assert(i < queue_count);
2060 demo->graphics_queue_node_index = i;
2061
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002062 err = vkGetDeviceQueue(demo->device, demo->graphics_queue_node_index,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002063 0, &demo->queue);
2064 assert(!err);
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002065
Jon Ashburnd7130cb2015-06-16 12:44:51 -06002066 // for now hardcode format till get WSI support
2067 demo->format = VK_FORMAT_B8G8R8A8_UNORM;
Ian Elliotte4602cd2015-04-21 16:41:02 -06002068
David Pinedo2bb7c932015-06-18 17:03:14 -06002069 demo->quit = false;
2070 demo->curFrame = 0;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06002071
2072 // Get Memory information and properties
2073 err = vkGetPhysicalDeviceMemoryProperties(demo->gpu, &demo->memory_properties);
2074 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002075}
2076
2077static void demo_init_connection(struct demo *demo)
2078{
Ian Elliott639ca472015-04-16 15:23:05 -06002079#ifndef _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002080 const xcb_setup_t *setup;
2081 xcb_screen_iterator_t iter;
2082 int scr;
2083
2084 demo->connection = xcb_connect(NULL, &scr);
Ian Elliott3979e282015-04-03 15:24:55 -06002085 if (demo->connection == NULL) {
2086 printf("Cannot find a compatible Vulkan installable client driver "
2087 "(ICD).\nExiting ...\n");
2088 fflush(stdout);
2089 exit(1);
2090 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002091
2092 setup = xcb_get_setup(demo->connection);
2093 iter = xcb_setup_roots_iterator(setup);
2094 while (scr-- > 0)
2095 xcb_screen_next(&iter);
2096
2097 demo->screen = iter.data;
Ian Elliott639ca472015-04-16 15:23:05 -06002098#endif // _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002099}
2100
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002101static void demo_init(struct demo *demo, int argc, char **argv)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002102{
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002103 vec3 eye = {0.0f, 3.0f, 5.0f};
2104 vec3 origin = {0, 0, 0};
Chia-I Wuae3b55d2015-04-22 14:56:17 +08002105 vec3 up = {0.0f, 1.0f, 0.0};
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002106
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002107 memset(demo, 0, sizeof(*demo));
David Pinedo2bb7c932015-06-18 17:03:14 -06002108 demo->frameCount = INT_MAX;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002109
Piers Daniell735ee532015-02-23 16:23:13 -07002110 for (int i = 1; i < argc; i++) {
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002111 if (strcmp(argv[i], "--use_staging") == 0) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002112 demo->use_staging_buffer = true;
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002113 continue;
Ian Elliott639ca472015-04-16 15:23:05 -06002114 }
Cody Northrop1fedb212015-05-28 11:27:16 -06002115 if (strcmp(argv[i], "--use_glsl") == 0) {
2116 demo->use_glsl = true;
2117 continue;
2118 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002119 if (strcmp(argv[i], "--validate") == 0) {
2120 demo->validate = true;
2121 continue;
2122 }
David Pinedo2bb7c932015-06-18 17:03:14 -06002123 if (strcmp(argv[i], "--c") == 0 &&
2124 demo->frameCount == INT_MAX &&
2125 i < argc-1 &&
2126 sscanf(argv[i+1],"%d", &demo->frameCount) == 1 &&
2127 demo->frameCount >= 0)
2128 {
2129 i++;
2130 continue;
2131 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002132
David Pinedo2bb7c932015-06-18 17:03:14 -06002133 fprintf(stderr, "Usage:\n %s [--use_staging] [--validate] [--c <framecount>]\n", APP_SHORT_NAME);
Ian Elliott639ca472015-04-16 15:23:05 -06002134 fflush(stderr);
2135 exit(1);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002136 }
2137
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002138 demo_init_connection(demo);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002139 demo_init_vk(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002140
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002141 demo->width = 500;
2142 demo->height = 500;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002143
2144 demo->spin_angle = 0.01f;
2145 demo->spin_increment = 0.01f;
2146 demo->pause = false;
2147
Piers Daniell735ee532015-02-23 16:23:13 -07002148 mat4x4_perspective(demo->projection_matrix, (float)degreesToRadians(45.0f), 1.0f, 0.1f, 100.0f);
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002149 mat4x4_look_at(demo->view_matrix, eye, origin, up);
2150 mat4x4_identity(demo->model_matrix);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002151}
2152
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002153
Ian Elliott639ca472015-04-16 15:23:05 -06002154#ifdef _WIN32
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002155extern int __getmainargs(
2156 int * _Argc,
2157 char *** _Argv,
2158 char *** _Env,
2159 int _DoWildCard,
2160 int * new_mode);
Ian Elliottf9cf78c2015-04-28 10:33:11 -06002161
Ian Elliotta748eaf2015-04-28 15:50:36 -06002162int WINAPI WinMain(HINSTANCE hInstance,
2163 HINSTANCE hPrevInstance,
2164 LPSTR pCmdLine,
2165 int nCmdShow)
Ian Elliott639ca472015-04-16 15:23:05 -06002166{
2167 MSG msg; // message
2168 bool done; // flag saying when app is complete
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002169 int argc;
2170 char** argv;
2171 char** env;
2172 int new_mode = 0;
Ian Elliott639ca472015-04-16 15:23:05 -06002173
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002174 __getmainargs(&argc,&argv,&env,0,&new_mode);
2175
2176 demo_init(&demo, argc, argv);
2177 demo.connection = hInstance;
2178 strncpy(demo.name, "cube", APP_NAME_STR_LEN);
Ian Elliott639ca472015-04-16 15:23:05 -06002179 demo_create_window(&demo);
2180
2181 demo_prepare(&demo);
2182
2183 done = false; //initialize loop condition variable
2184 /* main message loop*/
2185 while(!done)
2186 {
Ian Elliotta748eaf2015-04-28 15:50:36 -06002187 PeekMessage(&msg, NULL, 0, 0, PM_REMOVE);
Ian Elliott639ca472015-04-16 15:23:05 -06002188 if (msg.message == WM_QUIT) //check for a quit message
2189 {
2190 done = true; //if found, quit app
2191 }
2192 else
2193 {
2194 /* Translate and dispatch to event queue*/
2195 TranslateMessage(&msg);
2196 DispatchMessage(&msg);
2197 }
2198 }
2199
2200 demo_cleanup(&demo);
2201
Tony Barbourf9921732015-04-22 11:36:22 -06002202 return (int) msg.wParam;
Ian Elliott639ca472015-04-16 15:23:05 -06002203}
2204#else // _WIN32
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002205int main(int argc, char **argv)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002206{
2207 struct demo demo;
2208
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002209 demo_init(&demo, argc, argv);
Chia-I Wucbb564e2015-04-16 22:02:10 +08002210 demo_create_window(&demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002211
2212 demo_prepare(&demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002213 demo_run(&demo);
2214
2215 demo_cleanup(&demo);
2216
2217 return 0;
2218}
Ian Elliott639ca472015-04-16 15:23:05 -06002219#endif // _WIN32