blob: 71b44fdaec80aa40cd1dedf65871fbf7a9203c50 [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;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600306
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600307 VkFramebuffer framebuffer;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600308 int width, height;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600309 VkFormat format;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600310
Jon Ashburn0b85d052015-05-21 18:13:33 -0600311 PFN_vkCreateSwapChainWSI fpCreateSwapChainWSI;
312 PFN_vkDestroySwapChainWSI fpDestroySwapChainWSI;
313 PFN_vkGetSwapChainInfoWSI fpGetSwapChainInfoWSI;
314 PFN_vkQueuePresentWSI fpQueuePresentWSI;
Chia-I Wucbb564e2015-04-16 22:02:10 +0800315 VkSwapChainWSI swap_chain;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600316 struct {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600317 VkImage image;
Tony Barbour72304ef2015-04-16 15:59:00 -0600318 VkDeviceMemory mem;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600319 VkCmdBuffer cmd;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600320
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600321 VkColorAttachmentView view;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600322 } buffers[DEMO_BUFFER_COUNT];
323
324 struct {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600325 VkFormat format;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600326
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600327 VkImage image;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500328 VkDeviceMemory mem;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600329 VkDepthStencilView view;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600330 } depth;
331
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600332 struct texture_object textures[DEMO_TEXTURE_COUNT];
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600333
334 struct {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600335 VkBuffer buf;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500336 VkDeviceMemory mem;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600337 VkBufferView view;
Chia-I Wuae721ba2015-05-25 16:27:55 +0800338 VkDescriptorInfo desc;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600339 } uniform_data;
340
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600341 VkCmdBuffer cmd; // Buffer for initialization commands
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -0500342 VkPipelineLayout pipeline_layout;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600343 VkDescriptorSetLayout desc_layout;
344 VkPipeline pipeline;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600345
Courtney Goeltzenleuchter5e6d1e92015-04-10 16:24:50 -0600346 VkDynamicVpState viewport;
347 VkDynamicRsState raster;
348 VkDynamicCbState color_blend;
349 VkDynamicDsState depth_stencil;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600350
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600351 mat4x4 projection_matrix;
352 mat4x4 view_matrix;
353 mat4x4 model_matrix;
354
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600355 float spin_angle;
356 float spin_increment;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600357 bool pause;
358
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600359 VkDescriptorPool desc_pool;
360 VkDescriptorSet desc_set;
Chia-I Wu6a3c8972015-01-04 16:27:24 +0800361
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600362 bool quit;
David Pinedo2bb7c932015-06-18 17:03:14 -0600363 int32_t curFrame;
364 int32_t frameCount;
Tony Barbour3dddd5d2015-04-29 16:19:20 -0600365 bool validate;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -0600366 PFN_vkDbgCreateMsgCallback dbgCreateMsgCallback;
Tony Barbourd70b42c2015-06-30 14:14:19 -0600367 PFN_vkDbgDestroyMsgCallback dbgDestroyMsgCallback;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -0600368 VkDbgMsgCallback msg_callback;
369
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600370 uint32_t current_buffer;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600371};
372
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600373static void demo_flush_init_cmd(struct demo *demo)
374{
Tony Barbourfdc2d352015-04-22 09:02:32 -0600375 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600376
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600377 if (demo->cmd == VK_NULL_HANDLE)
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600378 return;
379
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600380 err = vkEndCommandBuffer(demo->cmd);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600381 assert(!err);
382
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600383 const VkCmdBuffer cmd_bufs[] = { demo->cmd };
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600384
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600385 err = vkQueueSubmit(demo->queue, 1, cmd_bufs, VK_NULL_HANDLE);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600386 assert(!err);
387
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600388 err = vkQueueWaitIdle(demo->queue);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600389 assert(!err);
390
Mike Stroyanebae8322015-04-17 12:36:38 -0600391 vkDestroyObject(demo->device, VK_OBJECT_TYPE_COMMAND_BUFFER, demo->cmd);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600392 demo->cmd = VK_NULL_HANDLE;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600393}
394
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600395static void demo_set_image_layout(
396 struct demo *demo,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600397 VkImage image,
malnasseeb25d3f2015-06-03 17:28:38 -0400398 VkImageAspect aspect,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600399 VkImageLayout old_image_layout,
400 VkImageLayout new_image_layout)
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600401{
Tony Barbourfdc2d352015-04-22 09:02:32 -0600402 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600403
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600404 if (demo->cmd == VK_NULL_HANDLE) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600405 const VkCmdBufferCreateInfo cmd = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600406 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600407 .pNext = NULL,
408 .queueNodeIndex = demo->graphics_queue_node_index,
409 .flags = 0,
410 };
411
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600412 err = vkCreateCommandBuffer(demo->device, &cmd, &demo->cmd);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600413 assert(!err);
414
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600415 VkCmdBufferBeginInfo cmd_buf_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600416 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600417 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -0600418 .flags = VK_CMD_BUFFER_OPTIMIZE_SMALL_BATCH_BIT |
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600419 VK_CMD_BUFFER_OPTIMIZE_ONE_TIME_SUBMIT_BIT,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600420 };
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600421 err = vkBeginCommandBuffer(demo->cmd, &cmd_buf_info);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600422 }
423
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600424 VkImageMemoryBarrier image_memory_barrier = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600425 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600426 .pNext = NULL,
427 .outputMask = 0,
428 .inputMask = 0,
429 .oldLayout = old_image_layout,
430 .newLayout = new_image_layout,
431 .image = image,
malnasseeb25d3f2015-06-03 17:28:38 -0400432 .subresourceRange = { aspect, 0, 1, 0, 0 }
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600433 };
434
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600435 if (new_image_layout == VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL) {
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600436 /* Make sure anything that was copying from this image has completed */
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -0600437 image_memory_barrier.inputMask = VK_MEMORY_INPUT_TRANSFER_BIT;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600438 }
439
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600440 if (new_image_layout == VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL) {
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600441 /* Make sure any Copy or CPU writes to image are flushed */
Courtney Goeltzenleuchter2bda8332015-04-29 17:16:21 -0600442 image_memory_barrier.outputMask = VK_MEMORY_OUTPUT_HOST_WRITE_BIT | VK_MEMORY_OUTPUT_TRANSFER_BIT;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600443 }
444
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600445 VkImageMemoryBarrier *pmemory_barrier = &image_memory_barrier;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600446
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600447 VkPipeEvent set_events[] = { VK_PIPE_EVENT_TOP_OF_PIPE };
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600448
Tony Barbour72304ef2015-04-16 15:59:00 -0600449 vkCmdPipelineBarrier(demo->cmd, VK_WAIT_EVENT_TOP_OF_PIPE, 1, set_events, 1, (const void **)&pmemory_barrier);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600450}
451
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600452static void demo_draw_build_cmd(struct demo *demo, VkCmdBuffer cmd_buf)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600453{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600454 const VkColorAttachmentBindInfo color_attachment = {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600455 .view = demo->buffers[demo->current_buffer].view,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600456 .layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600457 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600458 const VkDepthStencilBindInfo depth_stencil = {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600459 .view = demo->depth.view,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600460 .layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600461 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600462 const VkClearColor clear_color = {
Courtney Goeltzenleuchter374553c2015-04-03 16:35:32 -0600463 .color.floatColor = { 0.2f, 0.2f, 0.2f, 0.2f },
464 .useRawValue = false,
465 };
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600466 const float clear_depth = 1.0f;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600467 VkImageSubresourceRange clear_range;
468 VkCmdBufferBeginInfo cmd_buf_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600469 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO,
Courtney Goeltzenleuchter53968d82015-04-03 15:25:24 -0600470 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -0600471 .flags = VK_CMD_BUFFER_OPTIMIZE_SMALL_BATCH_BIT |
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600472 VK_CMD_BUFFER_OPTIMIZE_ONE_TIME_SUBMIT_BIT,
Jon Ashburn60ccfbe2014-12-31 17:08:35 -0700473 };
Tony Barbourfdc2d352015-04-22 09:02:32 -0600474 VkResult U_ASSERT_ONLY err;
Chris Forbes40a71562015-06-17 11:36:12 +1200475 VkAttachmentLoadOp load_op = VK_ATTACHMENT_LOAD_OP_CLEAR;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600476 VkAttachmentStoreOp store_op = VK_ATTACHMENT_STORE_OP_DONT_CARE;
477 const VkFramebufferCreateInfo fb_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600478 .sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO,
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700479 .pNext = NULL,
480 .colorAttachmentCount = 1,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600481 .pColorAttachments = (VkColorAttachmentBindInfo*) &color_attachment,
482 .pDepthStencilAttachment = (VkDepthStencilBindInfo*) &depth_stencil,
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700483 .sampleCount = 1,
Mark Lobodzinskic06b7412015-01-27 13:24:03 -0600484 .width = demo->width,
485 .height = demo->height,
486 .layers = 1,
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700487 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600488 VkRenderPassCreateInfo rp_info;
489 VkRenderPassBegin rp_begin;
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700490
491 memset(&rp_info, 0 , sizeof(rp_info));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600492 err = vkCreateFramebuffer(demo->device, &fb_info, &rp_begin.framebuffer);
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700493 assert(!err);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600494 rp_info.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO;
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700495 rp_info.renderArea.extent.width = demo->width;
496 rp_info.renderArea.extent.height = demo->height;
Courtney Goeltzenleuchter53968d82015-04-03 15:25:24 -0600497 rp_info.colorAttachmentCount = fb_info.colorAttachmentCount;
498 rp_info.pColorFormats = &demo->format;
499 rp_info.pColorLayouts = &color_attachment.layout;
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700500 rp_info.pColorLoadOps = &load_op;
501 rp_info.pColorStoreOps = &store_op;
Courtney Goeltzenleuchter53968d82015-04-03 15:25:24 -0600502 rp_info.pColorLoadClearValues = &clear_color;
Tony Barbour72304ef2015-04-16 15:59:00 -0600503 rp_info.depthStencilFormat = VK_FORMAT_D16_UNORM;
Courtney Goeltzenleuchter53968d82015-04-03 15:25:24 -0600504 rp_info.depthStencilLayout = depth_stencil.layout;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600505 rp_info.depthLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
Courtney Goeltzenleuchter53968d82015-04-03 15:25:24 -0600506 rp_info.depthLoadClearValue = clear_depth;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600507 rp_info.depthStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
508 rp_info.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
Courtney Goeltzenleuchter53968d82015-04-03 15:25:24 -0600509 rp_info.stencilLoadClearValue = 0;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600510 rp_info.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
Tony Barbourd70b42c2015-06-30 14:14:19 -0600511 rp_info.sampleCount = 1;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600512 err = vkCreateRenderPass(demo->device, &rp_info, &rp_begin.renderPass);
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700513 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600514
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600515 err = vkBeginCommandBuffer(cmd_buf, &cmd_buf_info);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600516 assert(!err);
517
Tobin Ehlis080219d2015-06-24 15:53:07 -0600518 vkCmdBeginRenderPass(cmd_buf, &rp_begin);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600519 vkCmdBindPipeline(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600520 demo->pipeline);
Mark Lobodzinskic6e8b3d2015-06-15 13:21:21 -0600521 vkCmdBindDescriptorSets(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS, demo->pipeline_layout,
Cody Northropfb5185a2015-04-16 13:41:56 -0600522 0, 1, &demo->desc_set, 0, NULL);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600523
Tony Barbour72304ef2015-04-16 15:59:00 -0600524 vkCmdBindDynamicStateObject(cmd_buf, VK_STATE_BIND_POINT_VIEWPORT, demo->viewport);
525 vkCmdBindDynamicStateObject(cmd_buf, VK_STATE_BIND_POINT_RASTER, demo->raster);
526 vkCmdBindDynamicStateObject(cmd_buf, VK_STATE_BIND_POINT_COLOR_BLEND,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600527 demo->color_blend);
Tony Barbour72304ef2015-04-16 15:59:00 -0600528 vkCmdBindDynamicStateObject(cmd_buf, VK_STATE_BIND_POINT_DEPTH_STENCIL,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600529 demo->depth_stencil);
530
Chris Forbes40a71562015-06-17 11:36:12 +1200531 clear_range.aspect = VK_IMAGE_ASPECT_DEPTH;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600532 clear_range.baseMipLevel = 0;
533 clear_range.mipLevels = 1;
534 clear_range.baseArraySlice = 0;
535 clear_range.arraySize = 1;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600536
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600537 vkCmdClearDepthStencil(cmd_buf, demo->depth.image,
538 VK_IMAGE_LAYOUT_CLEAR_OPTIMAL,
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600539 clear_depth, 0, 1, &clear_range);
540
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600541 vkCmdDraw(cmd_buf, 0, 12 * 3, 0, 1);
542 vkCmdEndRenderPass(cmd_buf, rp_begin.renderPass);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600543
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600544 err = vkEndCommandBuffer(cmd_buf);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600545 assert(!err);
Courtney Goeltzenleuchter94901dc2015-02-25 17:53:18 -0700546
Mike Stroyanebae8322015-04-17 12:36:38 -0600547 vkDestroyObject(demo->device, VK_OBJECT_TYPE_RENDER_PASS, rp_begin.renderPass);
548 vkDestroyObject(demo->device, VK_OBJECT_TYPE_FRAMEBUFFER, rp_begin.framebuffer);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600549}
550
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600551
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600552void demo_update_data_buffer(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600553{
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600554 mat4x4 MVP, Model, VP;
555 int matrixSize = sizeof(MVP);
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600556 uint8_t *pData;
Tony Barbourfdc2d352015-04-22 09:02:32 -0600557 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600558
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600559 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600560
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600561 // Rotate 22.5 degrees around the Y axis
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600562 mat4x4_dup(Model, demo->model_matrix);
Piers Daniell735ee532015-02-23 16:23:13 -0700563 mat4x4_rotate(demo->model_matrix, Model, 0.0f, 1.0f, 0.0f, (float)degreesToRadians(demo->spin_angle));
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600564 mat4x4_mul(MVP, VP, demo->model_matrix);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600565
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500566 err = vkMapMemory(demo->device, demo->uniform_data.mem, 0, 0, 0, (void **) &pData);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600567 assert(!err);
568
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600569 memcpy(pData, (const void*) &MVP[0][0], matrixSize);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600570
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500571 err = vkUnmapMemory(demo->device, demo->uniform_data.mem);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600572 assert(!err);
573}
574
575static void demo_draw(struct demo *demo)
576{
Chia-I Wucbb564e2015-04-16 22:02:10 +0800577 const VkPresentInfoWSI present = {
578 .sType = VK_STRUCTURE_TYPE_PRESENT_INFO_WSI,
579 .pNext = NULL,
580 .image = demo->buffers[demo->current_buffer].image,
581 .flipInterval = 0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600582 };
Tony Barbourfdc2d352015-04-22 09:02:32 -0600583 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600584
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600585 err = vkQueueSubmit(demo->queue, 1, &demo->buffers[demo->current_buffer].cmd,
586 VK_NULL_HANDLE);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600587 assert(!err);
588
Jon Ashburn0b85d052015-05-21 18:13:33 -0600589 err = demo->fpQueuePresentWSI(demo->queue, &present);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600590 assert(!err);
591
592 demo->current_buffer = (demo->current_buffer + 1) % DEMO_BUFFER_COUNT;
Chia-I Wucbb564e2015-04-16 22:02:10 +0800593
594 err = vkQueueWaitIdle(demo->queue);
595 assert(err == VK_SUCCESS);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600596}
597
598static void demo_prepare_buffers(struct demo *demo)
599{
Chia-I Wucbb564e2015-04-16 22:02:10 +0800600 const VkSwapChainCreateInfoWSI swap_chain = {
601 .sType = VK_STRUCTURE_TYPE_SWAP_CHAIN_CREATE_INFO_WSI,
602 .pNext = NULL,
603 .pNativeWindowSystemHandle = demo->connection,
604 .pNativeWindowHandle = (void *) (intptr_t) demo->window,
Ian Elliotte4602cd2015-04-21 16:41:02 -0600605 .displayCount = 1,
Chia-I Wucbb564e2015-04-16 22:02:10 +0800606 .imageCount = DEMO_BUFFER_COUNT,
607 .imageFormat = demo->format,
608 .imageExtent = {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600609 .width = demo->width,
610 .height = demo->height,
611 },
Chia-I Wucbb564e2015-04-16 22:02:10 +0800612 .imageArraySize = 1,
613 .imageUsageFlags = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600614 };
Chia-I Wucbb564e2015-04-16 22:02:10 +0800615 VkSwapChainImageInfoWSI images[DEMO_BUFFER_COUNT];
616 size_t images_size = sizeof(images);
Tony Barbourfdc2d352015-04-22 09:02:32 -0600617 VkResult U_ASSERT_ONLY err;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600618 uint32_t i;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600619
Jon Ashburn0b85d052015-05-21 18:13:33 -0600620 err = demo->fpCreateSwapChainWSI(demo->device, &swap_chain, &demo->swap_chain);
Chia-I Wucbb564e2015-04-16 22:02:10 +0800621 assert(!err);
622
Jon Ashburn0b85d052015-05-21 18:13:33 -0600623 err = demo->fpGetSwapChainInfoWSI(demo->swap_chain,
Chia-I Wucbb564e2015-04-16 22:02:10 +0800624 VK_SWAP_CHAIN_INFO_TYPE_PERSISTENT_IMAGES_WSI,
625 &images_size, images);
626 assert(!err && images_size == sizeof(images));
627
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600628 for (i = 0; i < DEMO_BUFFER_COUNT; i++) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600629 VkColorAttachmentViewCreateInfo color_attachment_view = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600630 .sType = VK_STRUCTURE_TYPE_COLOR_ATTACHMENT_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600631 .pNext = NULL,
632 .format = demo->format,
633 .mipLevel = 0,
634 .baseArraySlice = 0,
635 .arraySize = 1,
636 };
637
Chia-I Wucbb564e2015-04-16 22:02:10 +0800638 demo->buffers[i].image = images[i].image;
639 demo->buffers[i].mem = images[i].memory;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600640
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600641 demo_set_image_layout(demo, demo->buffers[i].image,
malnasseeb25d3f2015-06-03 17:28:38 -0400642 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600643 VK_IMAGE_LAYOUT_UNDEFINED,
644 VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600645
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600646 color_attachment_view.image = demo->buffers[i].image;
647
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600648 err = vkCreateColorAttachmentView(demo->device,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600649 &color_attachment_view, &demo->buffers[i].view);
650 assert(!err);
651 }
652}
653
654static void demo_prepare_depth(struct demo *demo)
655{
Tony Barbour72304ef2015-04-16 15:59:00 -0600656 const VkFormat depth_format = VK_FORMAT_D16_UNORM;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600657 const VkImageCreateInfo image = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600658 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600659 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -0600660 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600661 .format = depth_format,
662 .extent = { demo->width, demo->height, 1 },
663 .mipLevels = 1,
664 .arraySize = 1,
665 .samples = 1,
Tony Barbour72304ef2015-04-16 15:59:00 -0600666 .tiling = VK_IMAGE_TILING_OPTIMAL,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600667 .usage = VK_IMAGE_USAGE_DEPTH_STENCIL_BIT,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600668 .flags = 0,
669 };
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -0600670 VkMemoryAllocInfo mem_alloc = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600671 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
Mark Lobodzinskia6907f72015-04-16 08:52:00 -0500672 .pNext = NULL,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600673 .allocationSize = 0,
Tony Barbour72304ef2015-04-16 15:59:00 -0600674 .memProps = VK_MEMORY_PROPERTY_DEVICE_ONLY,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600675 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600676 VkDepthStencilViewCreateInfo view = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600677 .sType = VK_STRUCTURE_TYPE_DEPTH_STENCIL_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600678 .pNext = NULL,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600679 .image = VK_NULL_HANDLE,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600680 .mipLevel = 0,
681 .baseArraySlice = 0,
682 .arraySize = 1,
683 .flags = 0,
684 };
Mike Stroyanebae8322015-04-17 12:36:38 -0600685
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500686 VkMemoryRequirements mem_reqs;
Tony Barbourfdc2d352015-04-22 09:02:32 -0600687 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600688
689 demo->depth.format = depth_format;
690
691 /* create image */
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600692 err = vkCreateImage(demo->device, &image,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600693 &demo->depth.image);
694 assert(!err);
695
Tony Barbourecf1b2b2015-06-24 16:06:58 -0600696 err = vkGetObjectMemoryRequirements(demo->device,
697 VK_OBJECT_TYPE_IMAGE, demo->depth.image, &mem_reqs);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500698 mem_alloc.allocationSize = mem_reqs.size;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600699
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500700 /* allocate memory */
701 err = vkAllocMemory(demo->device, &mem_alloc, &demo->depth.mem);
702 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600703
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500704 /* bind memory */
705 err = vkBindObjectMemory(demo->device,
706 VK_OBJECT_TYPE_IMAGE, demo->depth.image,
707 demo->depth.mem, 0);
708 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600709
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600710 demo_set_image_layout(demo, demo->depth.image,
malnasseeb25d3f2015-06-03 17:28:38 -0400711 VK_IMAGE_ASPECT_DEPTH,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600712 VK_IMAGE_LAYOUT_UNDEFINED,
713 VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600714
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600715 /* create image view */
716 view.image = demo->depth.image;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600717 err = vkCreateDepthStencilView(demo->device, &view,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600718 &demo->depth.view);
719 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600720}
721
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600722/** loadTexture
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -0600723 * loads a png file into an memory object, using cstdio , libpng.
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600724 *
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -0600725 * \param demo : Needed to access VK calls
726 * \param filename : the png file to be loaded
727 * \param width : width of png, to be updated as a side effect of this function
728 * \param height : height of png, to be updated as a side effect of this function
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600729 *
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -0600730 * \return bool : an opengl texture id. true if successful?,
731 * should be validated by the client of this function.
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600732 *
733 * Source: http://en.wikibooks.org/wiki/OpenGL_Programming/Intermediate/Textures
734 * Modified to copy image to memory
735 *
736 */
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700737bool loadTexture(const char *filename, uint8_t *rgba_data,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600738 VkSubresourceLayout *layout,
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600739 int32_t *width, int32_t *height)
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600740{
741 //header for testing if it is a png
742 png_byte header[8];
Tony Barbourf9921732015-04-22 11:36:22 -0600743 int is_png, bit_depth, color_type, rowbytes;
744 size_t retval;
Ian Elliott1e42dff2015-02-13 14:29:21 -0700745 png_uint_32 i, twidth, theight;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600746 png_structp png_ptr;
747 png_infop info_ptr, end_info;
748 png_byte *image_data;
749 png_bytep *row_pointers;
750
751 //open file as binary
752 FILE *fp = fopen(filename, "rb");
753 if (!fp) {
754 return false;
755 }
756
757 //read the header
Tony Barbourfdc2d352015-04-22 09:02:32 -0600758 retval = fread(header, 1, 8, fp);
759 if (retval != 8) {
760 fclose(fp);
761 return false;
762 }
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600763
764 //test if png
765 is_png = !png_sig_cmp(header, 0, 8);
766 if (!is_png) {
767 fclose(fp);
768 return false;
769 }
770
771 //create png struct
772 png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL,
773 NULL, NULL);
774 if (!png_ptr) {
775 fclose(fp);
776 return (false);
777 }
778
779 //create png info struct
780 info_ptr = png_create_info_struct(png_ptr);
781 if (!info_ptr) {
782 png_destroy_read_struct(&png_ptr, (png_infopp) NULL, (png_infopp) NULL);
783 fclose(fp);
784 return (false);
785 }
786
787 //create png info struct
788 end_info = png_create_info_struct(png_ptr);
789 if (!end_info) {
790 png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp) NULL);
791 fclose(fp);
792 return (false);
793 }
794
795 //png error stuff, not sure libpng man suggests this.
796 if (setjmp(png_jmpbuf(png_ptr))) {
797 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
798 fclose(fp);
799 return (false);
800 }
801
802 //init png reading
803 png_init_io(png_ptr, fp);
804
805 //let libpng know you already read the first 8 bytes
806 png_set_sig_bytes(png_ptr, 8);
807
808 // read all the info up to the image data
809 png_read_info(png_ptr, info_ptr);
810
811 // get info about png
812 png_get_IHDR(png_ptr, info_ptr, &twidth, &theight, &bit_depth, &color_type,
813 NULL, NULL, NULL);
814
815 //update width and height based on png info
816 *width = twidth;
817 *height = theight;
818
819 // Require that incoming texture be 8bits per color component
820 // and 4 components (RGBA).
821 if (png_get_bit_depth(png_ptr, info_ptr) != 8 ||
822 png_get_channels(png_ptr, info_ptr) != 4) {
823 return false;
824 }
825
826 if (rgba_data == NULL) {
827 // If data pointer is null, we just want the width & height
828 // clean up memory and close stuff
829 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
830 fclose(fp);
831
832 return true;
833 }
834
835 // Update the png info struct.
836 png_read_update_info(png_ptr, info_ptr);
837
838 // Row size in bytes.
839 rowbytes = png_get_rowbytes(png_ptr, info_ptr);
840
841 // Allocate the image_data as a big block, to be given to opengl
842 image_data = (png_byte *)malloc(rowbytes * theight * sizeof(png_byte));
843 if (!image_data) {
844 //clean up memory and close stuff
845 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
846 fclose(fp);
847 return false;
848 }
849
850 // row_pointers is for pointing to image_data for reading the png with libpng
851 row_pointers = (png_bytep *)malloc(theight * sizeof(png_bytep));
852 if (!row_pointers) {
853 //clean up memory and close stuff
854 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
855 // delete[] image_data;
856 fclose(fp);
857 return false;
858 }
859 // set the individual row_pointers to point at the correct offsets of image_data
860 for (i = 0; i < theight; ++i)
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700861 row_pointers[theight - 1 - i] = rgba_data + i * layout->rowPitch;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600862
863 // read the png into image_data through row_pointers
864 png_read_image(png_ptr, row_pointers);
865
866 // clean up memory and close stuff
867 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
868 free(row_pointers);
869 free(image_data);
870 fclose(fp);
871
872 return true;
873}
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600874
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700875static void demo_prepare_texture_image(struct demo *demo,
876 const char *filename,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600877 struct texture_object *tex_obj,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600878 VkImageTiling tiling,
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -0600879 VkImageUsageFlags usage,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600880 VkFlags mem_props)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600881{
Mike Stroyan8b89e072015-06-15 14:21:03 -0600882 const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600883 int32_t tex_width;
884 int32_t tex_height;
Tony Barbourfdc2d352015-04-22 09:02:32 -0600885 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700886
David Pinedo30bd71d2015-04-23 08:16:57 -0600887 if (!loadTexture(filename, NULL, NULL, &tex_width, &tex_height))
888 {
889 printf("Failed to load textures\n");
890 fflush(stdout);
891 exit(1);
892 }
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700893
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600894 tex_obj->tex_width = tex_width;
895 tex_obj->tex_height = tex_height;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700896
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600897 const VkImageCreateInfo image_create_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600898 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700899 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -0600900 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700901 .format = tex_format,
902 .extent = { tex_width, tex_height, 1 },
903 .mipLevels = 1,
904 .arraySize = 1,
905 .samples = 1,
906 .tiling = tiling,
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -0600907 .usage = usage,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700908 .flags = 0,
909 };
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -0600910 VkMemoryAllocInfo mem_alloc = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600911 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
Mark Lobodzinskia6907f72015-04-16 08:52:00 -0500912 .pNext = NULL,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700913 .allocationSize = 0,
914 .memProps = mem_props,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700915 };
916
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500917 VkMemoryRequirements mem_reqs;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700918
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600919 err = vkCreateImage(demo->device, &image_create_info,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600920 &tex_obj->image);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700921 assert(!err);
922
Tony Barbourecf1b2b2015-06-24 16:06:58 -0600923 err = vkGetObjectMemoryRequirements(demo->device,
924 VK_OBJECT_TYPE_IMAGE, tex_obj->image, &mem_reqs);
925 assert(!err);
Piers Daniell735ee532015-02-23 16:23:13 -0700926
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500927 mem_alloc.allocationSize = mem_reqs.size;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700928
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500929 /* allocate memory */
930 err = vkAllocMemory(demo->device, &mem_alloc,
931 &(tex_obj->mem));
932 assert(!err);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700933
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500934 /* bind memory */
935 err = vkBindObjectMemory(demo->device,
936 VK_OBJECT_TYPE_IMAGE, tex_obj->image,
937 tex_obj->mem, 0);
938 assert(!err);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700939
Tony Barbour72304ef2015-04-16 15:59:00 -0600940 if (mem_props & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600941 const VkImageSubresource subres = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600942 .aspect = VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700943 .mipLevel = 0,
944 .arraySlice = 0,
945 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600946 VkSubresourceLayout layout;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700947 void *data;
948
Tony Barbourecf1b2b2015-06-24 16:06:58 -0600949 err = vkGetImageSubresourceLayout(demo->device, tex_obj->image, &subres, &layout);
950 assert(!err);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700951
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500952 err = vkMapMemory(demo->device, tex_obj->mem, 0, 0, 0, &data);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700953 assert(!err);
954
955 if (!loadTexture(filename, data, &layout, &tex_width, &tex_height)) {
956 fprintf(stderr, "Error loading texture: %s\n", filename);
957 }
958
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500959 err = vkUnmapMemory(demo->device, tex_obj->mem);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700960 assert(!err);
961 }
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600962
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600963 tex_obj->imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600964 demo_set_image_layout(demo, tex_obj->image,
malnasseeb25d3f2015-06-03 17:28:38 -0400965 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600966 VK_IMAGE_LAYOUT_UNDEFINED,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600967 tex_obj->imageLayout);
968 /* 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 -0700969}
970
Mark Lobodzinskida9b1092015-04-16 11:44:05 -0500971static void demo_destroy_texture_image(struct demo *demo, struct texture_object *tex_objs)
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700972{
973 /* clean up staging resources */
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500974 vkFreeMemory(demo->device, tex_objs->mem);
Mike Stroyanebae8322015-04-17 12:36:38 -0600975 vkDestroyObject(demo->device, VK_OBJECT_TYPE_IMAGE, tex_objs->image);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700976}
977
978static void demo_prepare_textures(struct demo *demo)
979{
Tony Barbour72304ef2015-04-16 15:59:00 -0600980 const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600981 VkFormatProperties props;
Tony Barbourfdc2d352015-04-22 09:02:32 -0600982 VkResult U_ASSERT_ONLY err;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600983 uint32_t i;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600984
Chris Forbesf576a3e2015-06-21 22:55:02 +1200985 err = vkGetPhysicalDeviceFormatInfo(demo->gpu, tex_format, &props);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700986 assert(!err);
987
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600988 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700989
FslNopper6a7e50e2015-05-06 21:42:01 +0200990 if ((props.linearTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) && !demo->use_staging_buffer) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700991 /* Device can texture using linear textures */
992 demo_prepare_texture_image(demo, tex_files[i], &demo->textures[i],
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -0600993 VK_IMAGE_TILING_LINEAR, VK_IMAGE_USAGE_SAMPLED_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
Tony Barbour72304ef2015-04-16 15:59:00 -0600994 } else if (props.optimalTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700995 /* Must use staging buffer to copy linear texture to optimized */
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600996 struct texture_object staging_texture;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700997
998 memset(&staging_texture, 0, sizeof(staging_texture));
999 demo_prepare_texture_image(demo, tex_files[i], &staging_texture,
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001000 VK_IMAGE_TILING_LINEAR, VK_IMAGE_USAGE_TRANSFER_SOURCE_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001001
1002 demo_prepare_texture_image(demo, tex_files[i], &demo->textures[i],
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001003 VK_IMAGE_TILING_OPTIMAL,
1004 (VK_IMAGE_USAGE_TRANSFER_DESTINATION_BIT | VK_IMAGE_USAGE_SAMPLED_BIT),
1005 VK_MEMORY_PROPERTY_DEVICE_ONLY);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001006
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001007 demo_set_image_layout(demo, staging_texture.image,
malnasseeb25d3f2015-06-03 17:28:38 -04001008 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001009 staging_texture.imageLayout,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001010 VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001011
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001012 demo_set_image_layout(demo, demo->textures[i].image,
malnasseeb25d3f2015-06-03 17:28:38 -04001013 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001014 demo->textures[i].imageLayout,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001015 VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001016
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001017 VkImageCopy copy_region = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001018 .srcSubresource = { VK_IMAGE_ASPECT_COLOR, 0, 0 },
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001019 .srcOffset = { 0, 0, 0 },
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001020 .destSubresource = { VK_IMAGE_ASPECT_COLOR, 0, 0 },
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001021 .destOffset = { 0, 0, 0 },
1022 .extent = { staging_texture.tex_width, staging_texture.tex_height, 1 },
1023 };
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001024 vkCmdCopyImage(demo->cmd,
1025 staging_texture.image, VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL,
1026 demo->textures[i].image, VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL,
Courtney Goeltzenleuchter8e89a312015-03-25 11:25:10 -06001027 1, &copy_region);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001028
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001029 demo_set_image_layout(demo, demo->textures[i].image,
malnasseeb25d3f2015-06-03 17:28:38 -04001030 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001031 VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001032 demo->textures[i].imageLayout);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001033
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001034 demo_flush_init_cmd(demo);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001035
Courtney Goeltzenleuchterf3aeb2b2015-04-21 09:30:03 -06001036 demo_destroy_texture_image(demo, &staging_texture);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001037 } else {
Mike Stroyan8b89e072015-06-15 14:21:03 -06001038 /* Can't support VK_FORMAT_R8G8B8A8_UNORM !? */
1039 assert(!"No support for R8G8B8A8_UNORM as texture image format");
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001040 }
1041
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001042 const VkSamplerCreateInfo sampler = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001043 .sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001044 .pNext = NULL,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001045 .magFilter = VK_TEX_FILTER_NEAREST,
1046 .minFilter = VK_TEX_FILTER_NEAREST,
Tony Barbour72304ef2015-04-16 15:59:00 -06001047 .mipMode = VK_TEX_MIPMAP_MODE_BASE,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001048 .addressU = VK_TEX_ADDRESS_CLAMP,
1049 .addressV = VK_TEX_ADDRESS_CLAMP,
1050 .addressW = VK_TEX_ADDRESS_CLAMP,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001051 .mipLodBias = 0.0f,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001052 .maxAnisotropy = 1,
Tony Barbour72304ef2015-04-16 15:59:00 -06001053 .compareOp = VK_COMPARE_OP_NEVER,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001054 .minLod = 0.0f,
1055 .maxLod = 0.0f,
Tony Barbourcb530c72015-06-25 16:56:44 -06001056 .borderColor = VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001057 };
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001058
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001059 VkImageViewCreateInfo view = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001060 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001061 .pNext = NULL,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001062 .image = VK_NULL_HANDLE,
Tony Barbour72304ef2015-04-16 15:59:00 -06001063 .viewType = VK_IMAGE_VIEW_TYPE_2D,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001064 .format = tex_format,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001065 .channels = { VK_CHANNEL_SWIZZLE_R,
1066 VK_CHANNEL_SWIZZLE_G,
1067 VK_CHANNEL_SWIZZLE_B,
1068 VK_CHANNEL_SWIZZLE_A, },
1069 .subresourceRange = { VK_IMAGE_ASPECT_COLOR, 0, 1, 0, 1 },
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001070 };
Jon Ashburnb2a66652015-01-16 09:37:43 -07001071
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001072 /* create sampler */
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001073 err = vkCreateSampler(demo->device, &sampler,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001074 &demo->textures[i].sampler);
1075 assert(!err);
1076
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001077 /* create image view */
1078 view.image = demo->textures[i].image;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001079 err = vkCreateImageView(demo->device, &view,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001080 &demo->textures[i].view);
1081 assert(!err);
1082 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001083}
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001084
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001085void demo_prepare_cube_data_buffer(struct demo *demo)
1086{
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -06001087 VkBufferCreateInfo buf_info;
1088 VkBufferViewCreateInfo view_info;
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -06001089 VkMemoryAllocInfo alloc_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001090 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
Mark Lobodzinskia6907f72015-04-16 08:52:00 -05001091 .pNext = NULL,
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001092 .allocationSize = 0,
Tony Barbour72304ef2015-04-16 15:59:00 -06001093 .memProps = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT,
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001094 };
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001095 VkMemoryRequirements mem_reqs;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001096 size_t mem_reqs_size = sizeof(VkMemoryRequirements);
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001097 uint8_t *pData;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001098 int i;
1099 mat4x4 MVP, VP;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001100 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001101 struct vktexcube_vs_uniform data;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001102
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001103 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001104 mat4x4_mul(MVP, VP, demo->model_matrix);
1105 memcpy(data.mvp, MVP, sizeof(MVP));
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001106// dumpMatrix("MVP", MVP);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001107
1108 for (i=0; i<12*3; i++) {
1109 data.position[i][0] = g_vertex_buffer_data[i*3];
1110 data.position[i][1] = g_vertex_buffer_data[i*3+1];
1111 data.position[i][2] = g_vertex_buffer_data[i*3+2];
1112 data.position[i][3] = 1.0f;
1113 data.attr[i][0] = g_uv_buffer_data[2*i];
1114 data.attr[i][1] = g_uv_buffer_data[2*i + 1];
1115 data.attr[i][2] = 0;
1116 data.attr[i][3] = 0;
1117 }
1118
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001119 memset(&buf_info, 0, sizeof(buf_info));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001120 buf_info.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -06001121 buf_info.usage = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001122 err = vkCreateBuffer(demo->device, &buf_info, &demo->uniform_data.buf);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001123 assert(!err);
1124
Tony Barbourecf1b2b2015-06-24 16:06:58 -06001125 err = vkGetObjectMemoryRequirements(demo->device,
1126 VK_OBJECT_TYPE_BUFFER, demo->uniform_data.buf, &mem_reqs);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001127 assert(!err && mem_reqs_size == sizeof(mem_reqs));
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001128
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001129 alloc_info.allocationSize = mem_reqs.size;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001130
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001131 err = vkAllocMemory(demo->device, &alloc_info, &(demo->uniform_data.mem));
1132 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001133
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001134 err = vkMapMemory(demo->device, demo->uniform_data.mem, 0, 0, 0, (void **) &pData);
1135 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001136
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001137 memcpy(pData, &data, sizeof data);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001138
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001139 err = vkUnmapMemory(demo->device, demo->uniform_data.mem);
1140 assert(!err);
1141
1142 err = vkBindObjectMemory(demo->device,
1143 VK_OBJECT_TYPE_BUFFER, demo->uniform_data.buf,
1144 demo->uniform_data.mem, 0);
1145 assert(!err);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001146
1147 memset(&view_info, 0, sizeof(view_info));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001148 view_info.sType = VK_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO;
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001149 view_info.buffer = demo->uniform_data.buf;
Tony Barbour72304ef2015-04-16 15:59:00 -06001150 view_info.viewType = VK_BUFFER_VIEW_TYPE_RAW;
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001151 view_info.offset = 0;
1152 view_info.range = sizeof(data);
1153
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001154 err = vkCreateBufferView(demo->device, &view_info, &demo->uniform_data.view);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001155 assert(!err);
1156
Chia-I Wuae721ba2015-05-25 16:27:55 +08001157 demo->uniform_data.desc.bufferView = demo->uniform_data.view;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001158}
1159
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001160static void demo_prepare_descriptor_layout(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001161{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001162 const VkDescriptorSetLayoutBinding layout_bindings[2] = {
Chia-I Wua2aa8632015-03-26 15:04:41 +08001163 [0] = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001164 .descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
Chia-I Wu6d29a412015-05-25 16:22:52 +08001165 .arraySize = 1,
Tony Barbour72304ef2015-04-16 15:59:00 -06001166 .stageFlags = VK_SHADER_STAGE_VERTEX_BIT,
Chia-I Wu91e8e212015-03-27 12:56:09 +08001167 .pImmutableSamplers = NULL,
Chia-I Wua2aa8632015-03-26 15:04:41 +08001168 },
1169 [1] = {
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -06001170 .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
Chia-I Wu6d29a412015-05-25 16:22:52 +08001171 .arraySize = DEMO_TEXTURE_COUNT,
Tony Barbour72304ef2015-04-16 15:59:00 -06001172 .stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT,
Chia-I Wu91e8e212015-03-27 12:56:09 +08001173 .pImmutableSamplers = NULL,
Chia-I Wua2aa8632015-03-26 15:04:41 +08001174 },
1175 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001176 const VkDescriptorSetLayoutCreateInfo descriptor_layout = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001177 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001178 .pNext = NULL,
Chia-I Wua2aa8632015-03-26 15:04:41 +08001179 .count = 2,
1180 .pBinding = layout_bindings,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001181 };
Tony Barbourfdc2d352015-04-22 09:02:32 -06001182 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001183
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001184 err = vkCreateDescriptorSetLayout(demo->device,
Chia-I Wub58c24a2015-03-26 15:27:55 +08001185 &descriptor_layout, &demo->desc_layout);
1186 assert(!err);
1187
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001188 const VkPipelineLayoutCreateInfo pPipelineLayoutCreateInfo = {
1189 .sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
1190 .pNext = NULL,
1191 .descriptorSetCount = 1,
1192 .pSetLayouts = &demo->desc_layout,
1193 };
1194
1195 err = vkCreatePipelineLayout(demo->device,
1196 &pPipelineLayoutCreateInfo,
1197 &demo->pipeline_layout);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001198 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001199}
1200
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001201static VkShader demo_prepare_shader(struct demo *demo,
Tony Barbour72304ef2015-04-16 15:59:00 -06001202 VkShaderStage stage,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001203 const void *code,
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001204 size_t size)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001205{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001206 VkShaderCreateInfo createInfo;
1207 VkShader shader;
1208 VkResult err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001209
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001210
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001211 createInfo.sType = VK_STRUCTURE_TYPE_SHADER_CREATE_INFO;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001212 createInfo.pNext = NULL;
1213
Cody Northrop1fedb212015-05-28 11:27:16 -06001214 if (!demo->use_glsl) {
1215 createInfo.codeSize = size;
1216 createInfo.pCode = code;
1217 createInfo.flags = 0;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001218
Cody Northrop1fedb212015-05-28 11:27:16 -06001219 err = vkCreateShader(demo->device, &createInfo, &shader);
1220 if (err) {
1221 free((void *) createInfo.pCode);
1222 }
1223 } else {
1224 // Create fake SPV structure to feed GLSL
1225 // to the driver "under the covers"
1226 createInfo.codeSize = 3 * sizeof(uint32_t) + size + 1;
1227 createInfo.pCode = malloc(createInfo.codeSize);
1228 createInfo.flags = 0;
1229
1230 /* try version 0 first: VkShaderStage followed by GLSL */
1231 ((uint32_t *) createInfo.pCode)[0] = ICD_SPV_MAGIC;
1232 ((uint32_t *) createInfo.pCode)[1] = 0;
1233 ((uint32_t *) createInfo.pCode)[2] = stage;
1234 memcpy(((uint32_t *) createInfo.pCode + 3), code, size + 1);
1235
1236 err = vkCreateShader(demo->device, &createInfo, &shader);
1237 if (err) {
1238 free((void *) createInfo.pCode);
1239 return (VkShader) VK_NULL_HANDLE;
1240 }
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001241 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001242 return shader;
1243}
1244
Cody Northrop8b12a1f2015-03-17 15:55:58 -06001245char *demo_read_spv(const char *filename, size_t *psize)
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001246{
1247 long int size;
Tony Barbourf9921732015-04-22 11:36:22 -06001248 size_t U_ASSERT_ONLY retval;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001249 void *shader_code;
1250
1251 FILE *fp = fopen(filename, "rb");
1252 if (!fp) return NULL;
1253
1254 fseek(fp, 0L, SEEK_END);
1255 size = ftell(fp);
1256
1257 fseek(fp, 0L, SEEK_SET);
1258
1259 shader_code = malloc(size);
Tony Barbourfdc2d352015-04-22 09:02:32 -06001260 retval = fread(shader_code, size, 1, fp);
1261 assert(retval == 1);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001262
1263 *psize = size;
1264
1265 return shader_code;
1266}
1267
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001268static VkShader demo_prepare_vs(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001269{
Cody Northrop1fedb212015-05-28 11:27:16 -06001270 if (!demo->use_glsl) {
1271 void *vertShaderCode;
1272 size_t size;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001273
Cody Northrop1fedb212015-05-28 11:27:16 -06001274 vertShaderCode = demo_read_spv("cube-vert.spv", &size);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001275
Cody Northrop1fedb212015-05-28 11:27:16 -06001276 return demo_prepare_shader(demo, VK_SHADER_STAGE_VERTEX,
1277 vertShaderCode, size);
1278 } else {
1279 static const char *vertShaderText =
1280 "#version 140\n"
1281 "#extension GL_ARB_separate_shader_objects : enable\n"
1282 "#extension GL_ARB_shading_language_420pack : enable\n"
1283 "\n"
1284 "layout(binding = 0) uniform buf {\n"
1285 " mat4 MVP;\n"
1286 " vec4 position[12*3];\n"
1287 " vec4 attr[12*3];\n"
1288 "} ubuf;\n"
1289 "\n"
1290 "layout (location = 0) out vec4 texcoord;\n"
1291 "\n"
1292 "void main() \n"
1293 "{\n"
1294 " texcoord = ubuf.attr[gl_VertexID];\n"
1295 " gl_Position = ubuf.MVP * ubuf.position[gl_VertexID];\n"
1296 "\n"
1297 " // GL->VK conventions\n"
1298 " gl_Position.y = -gl_Position.y;\n"
1299 " gl_Position.z = (gl_Position.z + gl_Position.w) / 2.0;\n"
1300 "}\n";
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001301
Cody Northrop1fedb212015-05-28 11:27:16 -06001302 return demo_prepare_shader(demo, VK_SHADER_STAGE_VERTEX,
1303 (const void *) vertShaderText,
1304 strlen(vertShaderText));
1305 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001306}
1307
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001308static VkShader demo_prepare_fs(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001309{
Cody Northrop1fedb212015-05-28 11:27:16 -06001310 if (!demo->use_glsl) {
1311 void *fragShaderCode;
1312 size_t size;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001313
Cody Northrop1fedb212015-05-28 11:27:16 -06001314 fragShaderCode = demo_read_spv("cube-frag.spv", &size);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001315
Cody Northrop1fedb212015-05-28 11:27:16 -06001316 return demo_prepare_shader(demo, VK_SHADER_STAGE_FRAGMENT,
1317 fragShaderCode, size);
1318 } else {
1319 static const char *fragShaderText =
1320 "#version 140\n"
1321 "#extension GL_ARB_separate_shader_objects : enable\n"
1322 "#extension GL_ARB_shading_language_420pack : enable\n"
1323 "layout (binding = 1) uniform sampler2D tex;\n"
1324 "\n"
1325 "layout (location = 0) in vec4 texcoord;\n"
1326 "layout (location = 0) out vec4 uFragColor;\n"
1327 "void main() {\n"
1328 " uFragColor = texture(tex, texcoord.xy);\n"
1329 "}\n";
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001330
Cody Northrop1fedb212015-05-28 11:27:16 -06001331 return demo_prepare_shader(demo, VK_SHADER_STAGE_FRAGMENT,
1332 (const void *) fragShaderText,
1333 strlen(fragShaderText));
1334 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001335}
1336
1337static void demo_prepare_pipeline(struct demo *demo)
1338{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001339 VkGraphicsPipelineCreateInfo pipeline;
Mark Lobodzinski521d7762015-06-23 15:11:57 -06001340
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001341 VkPipelineIaStateCreateInfo ia;
1342 VkPipelineRsStateCreateInfo rs;
1343 VkPipelineCbStateCreateInfo cb;
1344 VkPipelineDsStateCreateInfo ds;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001345 VkPipelineVpStateCreateInfo vp;
1346 VkPipelineMsStateCreateInfo ms;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001347 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001348
1349 memset(&pipeline, 0, sizeof(pipeline));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001350 pipeline.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001351 pipeline.layout = demo->pipeline_layout;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001352
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001353 memset(&ia, 0, sizeof(ia));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001354 ia.sType = VK_STRUCTURE_TYPE_PIPELINE_IA_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06001355 ia.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001356
1357 memset(&rs, 0, sizeof(rs));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001358 rs.sType = VK_STRUCTURE_TYPE_PIPELINE_RS_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06001359 rs.fillMode = VK_FILL_MODE_SOLID;
1360 rs.cullMode = VK_CULL_MODE_BACK;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001361 rs.frontFace = VK_FRONT_FACE_CCW;
Chia-I Wubb67e6e2015-04-22 14:20:52 +08001362 rs.depthClipEnable = VK_TRUE;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001363
1364 memset(&cb, 0, sizeof(cb));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001365 cb.sType = VK_STRUCTURE_TYPE_PIPELINE_CB_STATE_CREATE_INFO;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001366 VkPipelineCbAttachmentState att_state[1];
Tony Barbour29645d02015-01-16 14:27:35 -07001367 memset(att_state, 0, sizeof(att_state));
1368 att_state[0].format = demo->format;
1369 att_state[0].channelWriteMask = 0xf;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001370 att_state[0].blendEnable = VK_FALSE;
Tony Barbour29645d02015-01-16 14:27:35 -07001371 cb.attachmentCount = 1;
1372 cb.pAttachments = att_state;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001373
Tony Barbour29645d02015-01-16 14:27:35 -07001374 memset(&vp, 0, sizeof(vp));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001375 vp.sType = VK_STRUCTURE_TYPE_PIPELINE_VP_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06001376 vp.viewportCount = 1;
Tony Barbour29645d02015-01-16 14:27:35 -07001377
1378 memset(&ds, 0, sizeof(ds));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001379 ds.sType = VK_STRUCTURE_TYPE_PIPELINE_DS_STATE_CREATE_INFO;
Tony Barbour29645d02015-01-16 14:27:35 -07001380 ds.format = demo->depth.format;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001381 ds.depthTestEnable = VK_TRUE;
1382 ds.depthWriteEnable = VK_TRUE;
Tony Barbour72304ef2015-04-16 15:59:00 -06001383 ds.depthCompareOp = VK_COMPARE_OP_LESS_EQUAL;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001384 ds.depthBoundsEnable = VK_FALSE;
1385 ds.back.stencilFailOp = VK_STENCIL_OP_KEEP;
1386 ds.back.stencilPassOp = VK_STENCIL_OP_KEEP;
Tony Barbour72304ef2015-04-16 15:59:00 -06001387 ds.back.stencilCompareOp = VK_COMPARE_OP_ALWAYS;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001388 ds.stencilTestEnable = VK_FALSE;
Tony Barbour29645d02015-01-16 14:27:35 -07001389 ds.front = ds.back;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001390
Tony Barbour29645d02015-01-16 14:27:35 -07001391 memset(&ms, 0, sizeof(ms));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001392 ms.sType = VK_STRUCTURE_TYPE_PIPELINE_MS_STATE_CREATE_INFO;
Tony Barbour29645d02015-01-16 14:27:35 -07001393 ms.sampleMask = 1;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001394 ms.multisampleEnable = VK_FALSE;
Tony Barbour3ca22502015-06-26 10:18:34 -06001395 ms.rasterSamples = 1;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001396
Mark Lobodzinski521d7762015-06-23 15:11:57 -06001397 // Two stages: vs and fs
1398 pipeline.stageCount = 2;
1399 VkPipelineShaderStageCreateInfo shaderStages[2];
1400 memset(&shaderStages, 0, 2 * sizeof(VkPipelineShaderStageCreateInfo));
1401
1402 shaderStages[0].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1403 shaderStages[0].stage = VK_SHADER_STAGE_VERTEX;
1404 shaderStages[0].shader = demo_prepare_vs(demo);
1405
1406 shaderStages[1].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1407 shaderStages[1].stage = VK_SHADER_STAGE_FRAGMENT;
1408 shaderStages[1].shader = demo_prepare_fs(demo);
1409
Mark Lobodzinski15169cf2015-06-30 10:18:36 -06001410 pipeline.pVertexInputState = NULL;
Mark Lobodzinski521d7762015-06-23 15:11:57 -06001411 pipeline.pIaState = &ia;
1412 pipeline.pRsState = &rs;
1413 pipeline.pCbState = &cb;
1414 pipeline.pMsState = &ms;
1415 pipeline.pVpState = &vp;
1416 pipeline.pDsState = &ds;
1417 pipeline.pStages = shaderStages;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001418
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001419 err = vkCreateGraphicsPipeline(demo->device, &pipeline, &demo->pipeline);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001420 assert(!err);
1421
Mark Lobodzinski521d7762015-06-23 15:11:57 -06001422 for (uint32_t i = 0; i < pipeline.stageCount; i++) {
1423 vkDestroyObject(demo->device, VK_OBJECT_TYPE_SHADER, shaderStages[i].shader);
1424 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001425}
1426
1427static void demo_prepare_dynamic_states(struct demo *demo)
1428{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001429 VkDynamicVpStateCreateInfo viewport_create;
1430 VkDynamicRsStateCreateInfo raster;
1431 VkDynamicCbStateCreateInfo color_blend;
1432 VkDynamicDsStateCreateInfo depth_stencil;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001433 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001434
Tony Barbour29645d02015-01-16 14:27:35 -07001435 memset(&viewport_create, 0, sizeof(viewport_create));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001436 viewport_create.sType = VK_STRUCTURE_TYPE_DYNAMIC_VP_STATE_CREATE_INFO;
Courtney Goeltzenleuchter1710d8d2015-02-11 14:13:34 -07001437 viewport_create.viewportAndScissorCount = 1;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001438 VkViewport viewport;
Piers Daniell735ee532015-02-23 16:23:13 -07001439 memset(&viewport, 0, sizeof(viewport));
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001440 viewport.height = (float) demo->height;
1441 viewport.width = (float) demo->width;
1442 viewport.minDepth = (float) 0.0f;
1443 viewport.maxDepth = (float) 1.0f;
Piers Daniell735ee532015-02-23 16:23:13 -07001444 viewport_create.pViewports = &viewport;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001445 VkRect scissor;
Piers Daniell735ee532015-02-23 16:23:13 -07001446 memset(&scissor, 0, sizeof(scissor));
Courtney Goeltzenleuchter1710d8d2015-02-11 14:13:34 -07001447 scissor.extent.width = demo->width;
1448 scissor.extent.height = demo->height;
1449 scissor.offset.x = 0;
1450 scissor.offset.y = 0;
Courtney Goeltzenleuchter1710d8d2015-02-11 14:13:34 -07001451 viewport_create.pScissors = &scissor;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001452
1453 memset(&raster, 0, sizeof(raster));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001454 raster.sType = VK_STRUCTURE_TYPE_DYNAMIC_RS_STATE_CREATE_INFO;
Piers Daniell735ee532015-02-23 16:23:13 -07001455 raster.lineWidth = 1.0;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001456
1457 memset(&color_blend, 0, sizeof(color_blend));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001458 color_blend.sType = VK_STRUCTURE_TYPE_DYNAMIC_CB_STATE_CREATE_INFO;
Piers Daniell735ee532015-02-23 16:23:13 -07001459 color_blend.blendConst[0] = 1.0f;
1460 color_blend.blendConst[1] = 1.0f;
1461 color_blend.blendConst[2] = 1.0f;
1462 color_blend.blendConst[3] = 1.0f;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001463
1464 memset(&depth_stencil, 0, sizeof(depth_stencil));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001465 depth_stencil.sType = VK_STRUCTURE_TYPE_DYNAMIC_DS_STATE_CREATE_INFO;
Mark Lobodzinski89d32b12015-06-12 11:14:17 -06001466 depth_stencil.minDepthBounds = 0.0f;
1467 depth_stencil.maxDepthBounds = 1.0f;
Tony Barbour29645d02015-01-16 14:27:35 -07001468 depth_stencil.stencilBackRef = 0;
1469 depth_stencil.stencilFrontRef = 0;
1470 depth_stencil.stencilReadMask = 0xff;
1471 depth_stencil.stencilWriteMask = 0xff;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001472
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001473 err = vkCreateDynamicViewportState(demo->device, &viewport_create, &demo->viewport);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001474 assert(!err);
1475
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001476 err = vkCreateDynamicRasterState(demo->device, &raster, &demo->raster);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001477 assert(!err);
1478
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001479 err = vkCreateDynamicColorBlendState(demo->device,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001480 &color_blend, &demo->color_blend);
1481 assert(!err);
1482
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001483 err = vkCreateDynamicDepthStencilState(demo->device,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001484 &depth_stencil, &demo->depth_stencil);
1485 assert(!err);
1486}
1487
Chia-I Wu63ea9262015-03-26 13:14:16 +08001488static void demo_prepare_descriptor_pool(struct demo *demo)
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001489{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001490 const VkDescriptorTypeCount type_counts[2] = {
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001491 [0] = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001492 .type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001493 .count = 1,
1494 },
1495 [1] = {
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -06001496 .type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001497 .count = DEMO_TEXTURE_COUNT,
1498 },
1499 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001500 const VkDescriptorPoolCreateInfo descriptor_pool = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001501 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001502 .pNext = NULL,
1503 .count = 2,
1504 .pTypeCount = type_counts,
1505 };
Tony Barbourfdc2d352015-04-22 09:02:32 -06001506 VkResult U_ASSERT_ONLY err;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001507
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001508 err = vkCreateDescriptorPool(demo->device,
1509 VK_DESCRIPTOR_POOL_USAGE_ONE_SHOT, 1,
Chia-I Wu63ea9262015-03-26 13:14:16 +08001510 &descriptor_pool, &demo->desc_pool);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001511 assert(!err);
1512}
1513
1514static void demo_prepare_descriptor_set(struct demo *demo)
1515{
Chia-I Wuae721ba2015-05-25 16:27:55 +08001516 VkDescriptorInfo tex_descs[DEMO_TEXTURE_COUNT];
1517 VkWriteDescriptorSet writes[2];
Tony Barbourfdc2d352015-04-22 09:02:32 -06001518 VkResult U_ASSERT_ONLY err;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001519 uint32_t count;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001520 uint32_t i;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001521
Mike Stroyanebae8322015-04-17 12:36:38 -06001522 err = vkAllocDescriptorSets(demo->device, demo->desc_pool,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001523 VK_DESCRIPTOR_SET_USAGE_STATIC,
Chia-I Wu87544e72015-02-23 10:41:08 -07001524 1, &demo->desc_layout,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001525 &demo->desc_set, &count);
1526 assert(!err && count == 1);
1527
Chia-I Wuae721ba2015-05-25 16:27:55 +08001528 memset(&tex_descs, 0, sizeof(tex_descs));
1529 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
1530 tex_descs[i].sampler = demo->textures[i].sampler;
1531 tex_descs[i].imageView = demo->textures[i].view;
1532 tex_descs[i].imageLayout = VK_IMAGE_LAYOUT_GENERAL;
1533 }
1534
1535 memset(&writes, 0, sizeof(writes));
1536
1537 writes[0].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
1538 writes[0].destSet = demo->desc_set;
1539 writes[0].count = 1;
1540 writes[0].descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
1541 writes[0].pDescriptors = &demo->uniform_data.desc;
1542
1543 writes[1].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
1544 writes[1].destSet = demo->desc_set;
1545 writes[1].destBinding = 1;
1546 writes[1].count = DEMO_TEXTURE_COUNT;
1547 writes[1].descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
1548 writes[1].pDescriptors = tex_descs;
1549
1550 err = vkUpdateDescriptorSets(demo->device, 2, writes, 0, NULL);
1551 assert(!err);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001552}
1553
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001554static void demo_prepare(struct demo *demo)
1555{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001556 const VkCmdBufferCreateInfo cmd = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001557 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001558 .pNext = NULL,
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07001559 .queueNodeIndex = demo->graphics_queue_node_index,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001560 .flags = 0,
1561 };
Tony Barbourfdc2d352015-04-22 09:02:32 -06001562 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001563
1564 demo_prepare_buffers(demo);
1565 demo_prepare_depth(demo);
1566 demo_prepare_textures(demo);
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001567 demo_prepare_cube_data_buffer(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001568
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001569 demo_prepare_descriptor_layout(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001570 demo_prepare_pipeline(demo);
1571 demo_prepare_dynamic_states(demo);
1572
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001573 for (int i = 0; i < DEMO_BUFFER_COUNT; i++) {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001574 err = vkCreateCommandBuffer(demo->device, &cmd, &demo->buffers[i].cmd);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001575 assert(!err);
1576 }
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001577
Chia-I Wu63ea9262015-03-26 13:14:16 +08001578 demo_prepare_descriptor_pool(demo);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001579 demo_prepare_descriptor_set(demo);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001580
1581
1582 for (int i = 0; i < DEMO_BUFFER_COUNT; i++) {
1583 demo->current_buffer = i;
1584 demo_draw_build_cmd(demo, demo->buffers[i].cmd);
1585 }
1586
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001587 /*
1588 * Prepare functions above may generate pipeline commands
1589 * that need to be flushed before beginning the render loop.
1590 */
1591 demo_flush_init_cmd(demo);
1592
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001593 demo->current_buffer = 0;
Jon Ashburn8d26c062015-04-24 09:46:24 -07001594 demo->prepared = true;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001595}
1596
David Pinedo2bb7c932015-06-18 17:03:14 -06001597static void demo_cleanup(struct demo *demo)
1598{
1599 uint32_t i;
1600
1601 demo->prepared = false;
1602
1603 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DESCRIPTOR_SET, demo->desc_set);
1604 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DESCRIPTOR_POOL, demo->desc_pool);
1605
1606 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DYNAMIC_VP_STATE, demo->viewport);
1607 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DYNAMIC_RS_STATE, demo->raster);
1608 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DYNAMIC_CB_STATE, demo->color_blend);
1609 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DYNAMIC_DS_STATE, demo->depth_stencil);
1610
1611 vkDestroyObject(demo->device, VK_OBJECT_TYPE_PIPELINE, demo->pipeline);
1612 vkDestroyObject(demo->device, VK_OBJECT_TYPE_PIPELINE_LAYOUT, demo->pipeline_layout);
1613 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DESCRIPTOR_SET_LAYOUT, demo->desc_layout);
1614
1615 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
1616 vkDestroyObject(demo->device, VK_OBJECT_TYPE_IMAGE_VIEW, demo->textures[i].view);
1617 vkDestroyObject(demo->device, VK_OBJECT_TYPE_IMAGE, demo->textures[i].image);
1618 vkFreeMemory(demo->device, demo->textures[i].mem);
1619 vkDestroyObject(demo->device, VK_OBJECT_TYPE_SAMPLER, demo->textures[i].sampler);
1620 }
1621 demo->fpDestroySwapChainWSI(demo->swap_chain);
1622
1623 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DEPTH_STENCIL_VIEW, demo->depth.view);
1624 vkDestroyObject(demo->device, VK_OBJECT_TYPE_IMAGE, demo->depth.image);
1625 vkFreeMemory(demo->device, demo->depth.mem);
1626
1627 vkDestroyObject(demo->device, VK_OBJECT_TYPE_BUFFER_VIEW, demo->uniform_data.view);
1628 vkDestroyObject(demo->device, VK_OBJECT_TYPE_BUFFER, demo->uniform_data.buf);
1629 vkFreeMemory(demo->device, demo->uniform_data.mem);
1630
1631 for (i = 0; i < DEMO_BUFFER_COUNT; i++) {
1632 vkDestroyObject(demo->device, VK_OBJECT_TYPE_COLOR_ATTACHMENT_VIEW, demo->buffers[i].view);
1633 vkDestroyObject(demo->device, VK_OBJECT_TYPE_COMMAND_BUFFER, demo->buffers[i].cmd);
1634 }
1635
1636 vkDestroyDevice(demo->device);
Tony Barbourd70b42c2015-06-30 14:14:19 -06001637 if (demo->validate) {
1638 demo->dbgDestroyMsgCallback(demo->inst, demo->msg_callback);
1639 }
David Pinedo2bb7c932015-06-18 17:03:14 -06001640 vkDestroyInstance(demo->inst);
1641
1642#ifndef _WIN32
1643 xcb_destroy_window(demo->connection, demo->window);
1644 xcb_disconnect(demo->connection);
1645#endif // _WIN32
1646}
1647
1648// On MS-Windows, make this a global, so it's available to WndProc()
1649struct demo demo;
1650
Ian Elliott639ca472015-04-16 15:23:05 -06001651#ifdef _WIN32
1652static void demo_run(struct demo *demo)
1653{
Courtney Goeltzenleuchterb7e22702015-04-27 14:56:34 -06001654 if (!demo->prepared)
1655 return;
Ian Elliott639ca472015-04-16 15:23:05 -06001656 // Wait for work to finish before updating MVP.
1657 vkDeviceWaitIdle(demo->device);
1658 demo_update_data_buffer(demo);
1659
1660 demo_draw(demo);
1661
1662 // Wait for work to finish before updating MVP.
1663 vkDeviceWaitIdle(demo->device);
Ian Elliott639ca472015-04-16 15:23:05 -06001664
David Pinedo2bb7c932015-06-18 17:03:14 -06001665 demo->curFrame++;
1666
1667 if (demo->frameCount != INT_MAX && demo->curFrame == demo->frameCount)
1668 {
1669 demo->quit=true;
1670 demo_cleanup(demo);
1671 ExitProcess(0);
1672 }
1673
1674}
Ian Elliott639ca472015-04-16 15:23:05 -06001675
1676// MS-Windows event handling function:
1677LRESULT CALLBACK WndProc(HWND hWnd,
1678 UINT uMsg,
1679 WPARAM wParam,
1680 LPARAM lParam)
1681{
Ian Elliott639ca472015-04-16 15:23:05 -06001682 switch(uMsg)
1683 {
Ian Elliott639ca472015-04-16 15:23:05 -06001684 case WM_CLOSE:
1685 PostQuitMessage(0);
Tony Barbour3dddd5d2015-04-29 16:19:20 -06001686 break;
Ian Elliott639ca472015-04-16 15:23:05 -06001687 case WM_PAINT:
1688 demo_run(&demo);
1689 return 0;
1690 default:
1691 break;
1692 }
1693 return (DefWindowProc(hWnd, uMsg, wParam, lParam));
1694}
1695
1696static void demo_create_window(struct demo *demo)
1697{
1698 WNDCLASSEX win_class;
1699
1700 // Initialize the window class structure:
1701 win_class.cbSize = sizeof(WNDCLASSEX);
1702 win_class.style = CS_HREDRAW | CS_VREDRAW;
1703 win_class.lpfnWndProc = WndProc;
1704 win_class.cbClsExtra = 0;
1705 win_class.cbWndExtra = 0;
1706 win_class.hInstance = demo->connection; // hInstance
1707 win_class.hIcon = LoadIcon(NULL, IDI_APPLICATION);
1708 win_class.hCursor = LoadCursor(NULL, IDC_ARROW);
1709 win_class.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
1710 win_class.lpszMenuName = NULL;
1711 win_class.lpszClassName = demo->name;
1712 win_class.hIconSm = LoadIcon(NULL, IDI_WINLOGO);
1713 // Register window class:
1714 if (!RegisterClassEx(&win_class)) {
1715 // It didn't work, so try to give a useful error:
1716 printf("Unexpected error trying to start the application!\n");
1717 fflush(stdout);
1718 exit(1);
1719 }
1720 // Create window with the registered class:
Mike Stroyanb46779e2015-06-15 14:20:13 -06001721 RECT wr = { 0, 0, demo->width, demo->height };
1722 AdjustWindowRect(&wr, WS_OVERLAPPEDWINDOW, FALSE);
Ian Elliott639ca472015-04-16 15:23:05 -06001723 demo->window = CreateWindowEx(0,
1724 demo->name, // class name
1725 demo->name, // app name
1726 WS_OVERLAPPEDWINDOW | // window style
1727 WS_VISIBLE |
1728 WS_SYSMENU,
1729 100,100, // x/y coords
Mike Stroyanb46779e2015-06-15 14:20:13 -06001730 wr.right-wr.left, // width
1731 wr.bottom-wr.top, // height
Ian Elliott639ca472015-04-16 15:23:05 -06001732 NULL, // handle to parent
1733 NULL, // handle to menu
1734 demo->connection, // hInstance
1735 NULL); // no extra parameters
1736 if (!demo->window) {
1737 // It didn't work, so try to give a useful error:
1738 printf("Cannot create a window in which to draw!\n");
1739 fflush(stdout);
1740 exit(1);
1741 }
1742}
1743#else // _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001744static void demo_handle_event(struct demo *demo,
1745 const xcb_generic_event_t *event)
1746{
Piers Daniell735ee532015-02-23 16:23:13 -07001747 uint8_t event_code = event->response_type & 0x7f;
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07001748 switch (event_code) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001749 case XCB_EXPOSE:
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07001750 // TODO: Resize window
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001751 break;
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07001752 case XCB_CLIENT_MESSAGE:
1753 if((*(xcb_client_message_event_t*)event).data.data32[0] ==
1754 (*demo->atom_wm_delete_window).atom) {
1755 demo->quit = true;
1756 }
1757 break;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001758 case XCB_KEY_RELEASE:
1759 {
1760 const xcb_key_release_event_t *key =
1761 (const xcb_key_release_event_t *) event;
1762
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001763 switch (key->detail) {
1764 case 0x9: // Escape
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001765 demo->quit = true;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001766 break;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001767 case 0x71: // left arrow key
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001768 demo->spin_angle += demo->spin_increment;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001769 break;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001770 case 0x72: // right arrow key
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001771 demo->spin_angle -= demo->spin_increment;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001772 break;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001773 case 0x41:
1774 demo->pause = !demo->pause;
Piers Daniell735ee532015-02-23 16:23:13 -07001775 break;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001776 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001777 }
1778 break;
1779 default:
1780 break;
1781 }
1782}
1783
1784static void demo_run(struct demo *demo)
1785{
1786 xcb_flush(demo->connection);
1787
1788 while (!demo->quit) {
1789 xcb_generic_event_t *event;
1790
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07001791 if (demo->pause) {
1792 event = xcb_wait_for_event(demo->connection);
1793 } else {
1794 event = xcb_poll_for_event(demo->connection);
1795 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001796 if (event) {
1797 demo_handle_event(demo, event);
1798 free(event);
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07001799 }
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07001800
1801 // Wait for work to finish before updating MVP.
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001802 vkDeviceWaitIdle(demo->device);
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07001803 demo_update_data_buffer(demo);
1804
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07001805 demo_draw(demo);
Courtney Goeltzenleuchter1454f3c2014-11-18 11:28:09 -07001806
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07001807 // Wait for work to finish before updating MVP.
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001808 vkDeviceWaitIdle(demo->device);
David Pinedo2bb7c932015-06-18 17:03:14 -06001809 demo->curFrame++;
1810 if (demo->frameCount != INT_MAX && demo->curFrame == demo->frameCount)
1811 demo->quit = true;
1812
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001813 }
1814}
1815
1816static void demo_create_window(struct demo *demo)
1817{
1818 uint32_t value_mask, value_list[32];
1819
1820 demo->window = xcb_generate_id(demo->connection);
1821
1822 value_mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK;
1823 value_list[0] = demo->screen->black_pixel;
1824 value_list[1] = XCB_EVENT_MASK_KEY_RELEASE |
1825 XCB_EVENT_MASK_EXPOSURE;
1826
1827 xcb_create_window(demo->connection,
1828 XCB_COPY_FROM_PARENT,
1829 demo->window, demo->screen->root,
1830 0, 0, demo->width, demo->height, 0,
1831 XCB_WINDOW_CLASS_INPUT_OUTPUT,
1832 demo->screen->root_visual,
1833 value_mask, value_list);
1834
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07001835 /* Magic code that will send notification when window is destroyed */
1836 xcb_intern_atom_cookie_t cookie = xcb_intern_atom(demo->connection, 1, 12,
1837 "WM_PROTOCOLS");
1838 xcb_intern_atom_reply_t* reply = xcb_intern_atom_reply(demo->connection, cookie, 0);
1839
1840 xcb_intern_atom_cookie_t cookie2 = xcb_intern_atom(demo->connection, 0, 16, "WM_DELETE_WINDOW");
1841 demo->atom_wm_delete_window = xcb_intern_atom_reply(demo->connection, cookie2, 0);
1842
1843 xcb_change_property(demo->connection, XCB_PROP_MODE_REPLACE,
1844 demo->window, (*reply).atom, 4, 32, 1,
1845 &(*demo->atom_wm_delete_window).atom);
1846 free(reply);
1847
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001848 xcb_map_window(demo->connection, demo->window);
David Pinedo2bb7c932015-06-18 17:03:14 -06001849
1850 // Force the x/y coordinates to 100,100 results are identical in consecutive runs
1851 const uint32_t coords[] = {100, 100};
1852 xcb_configure_window(demo->connection, demo->window,
1853 XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y, coords);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001854}
Ian Elliott639ca472015-04-16 15:23:05 -06001855#endif // _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001856
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001857static void demo_init_vk(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001858{
Tobin Ehlis99239dc2015-04-16 18:04:57 -06001859 VkResult err;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06001860 VkExtensionProperties *instance_extensions;
1861 uint32_t instance_extension_count = 0;
1862 VkExtensionProperties *device_extensions;
1863 uint32_t device_extension_count = 0;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06001864 uint32_t total_extension_count = 0;
Tony Barbourecf1b2b2015-06-24 16:06:58 -06001865 err = vkGetGlobalExtensionCount(&total_extension_count);
Tobin Ehlis99239dc2015-04-16 18:04:57 -06001866 assert(!err);
1867
1868 VkExtensionProperties extProp;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06001869 bool32_t WSIextFound = 0;
1870 instance_extensions = malloc(sizeof(VkExtensionProperties) * total_extension_count);
1871 device_extensions = malloc(sizeof(VkExtensionProperties) * total_extension_count);
1872 for (uint32_t i = 0; i < total_extension_count; i++) {
Tony Barbourecf1b2b2015-06-24 16:06:58 -06001873 err = vkGetGlobalExtensionProperties(i, &extProp);
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06001874 if (!strcmp("VK_WSI_LunarG", extProp.name)) {
1875 WSIextFound = 1;
1876 memcpy(&instance_extensions[instance_extension_count++], &extProp, sizeof(VkExtensionProperties));
1877 memcpy(&device_extensions[device_extension_count++], &extProp, sizeof(VkExtensionProperties));
1878 }
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06001879 if (!strcmp(DEBUG_REPORT_EXTENSION_NAME, extProp.name)) {
1880 memcpy(&instance_extensions[instance_extension_count++], &extProp, sizeof(VkExtensionProperties));
1881 }
Tony Barbourd70b42c2015-06-30 14:14:19 -06001882 if (demo->validate && !strcmp("Validation",extProp.name)) {
1883 memcpy(&instance_extensions[instance_extension_count++], &extProp, sizeof(VkExtensionProperties));
1884 memcpy(&device_extensions[device_extension_count++], &extProp, sizeof(VkExtensionProperties));
1885 }
Tobin Ehlis99239dc2015-04-16 18:04:57 -06001886 }
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06001887 if (!WSIextFound) {
Tony Barbourecf1b2b2015-06-24 16:06:58 -06001888 ERR_EXIT("vkGetGlobalExtensionProperties failed to find the "
Ian Elliott65152912015-04-28 13:22:33 -06001889 "\"VK_WSI_LunarG\" extension.\n\nDo you have a compatible "
1890 "Vulkan installable client driver (ICD) installed?\nPlease "
1891 "look at the Getting Started guide for additional "
1892 "information.\n",
1893 "vkCreateInstance Failure");
1894 }
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001895 const VkApplicationInfo app = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001896 .sType = VK_STRUCTURE_TYPE_APPLICATION_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001897 .pNext = NULL,
Ian Elliott44e33f72015-04-28 10:52:52 -06001898 .pAppName = APP_SHORT_NAME,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001899 .appVersion = 0,
Ian Elliott44e33f72015-04-28 10:52:52 -06001900 .pEngineName = APP_SHORT_NAME,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001901 .engineVersion = 0,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001902 .apiVersion = VK_API_VERSION,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001903 };
Tony Barbour3dddd5d2015-04-29 16:19:20 -06001904 VkInstanceCreateInfo inst_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001905 .sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
Jon Ashburnab46b362015-04-04 14:52:07 -06001906 .pNext = NULL,
1907 .pAppInfo = &app,
1908 .pAllocCb = NULL,
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06001909 .extensionCount = instance_extension_count,
1910 .pEnabledExtensions = instance_extensions,
Jon Ashburnab46b362015-04-04 14:52:07 -06001911 };
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -06001912 const VkDeviceQueueCreateInfo queue = {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001913 .queueNodeIndex = 0,
1914 .queueCount = 1,
1915 };
Ian Elliott097d9f32015-04-28 11:35:02 -06001916
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06001917 VkDeviceCreateInfo device = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001918 .sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001919 .pNext = NULL,
1920 .queueRecordCount = 1,
1921 .pRequestedQueues = &queue,
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06001922 .extensionCount = device_extension_count,
1923 .pEnabledExtensions = device_extensions,
Jon Ashburn7842d522015-05-18 09:06:15 -06001924 .flags = 0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001925 };
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001926 uint32_t gpu_count;
1927 uint32_t i;
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07001928 uint32_t queue_count;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001929
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001930 err = vkCreateInstance(&inst_info, &demo->inst);
Ian Elliott07264132015-04-28 11:35:02 -06001931 if (err == VK_ERROR_INCOMPATIBLE_DRIVER) {
1932 ERR_EXIT("Cannot find a compatible Vulkan installable client driver "
Ian Elliott65152912015-04-28 13:22:33 -06001933 "(ICD).\n\nPlease look at the Getting Started guide for "
Ian Elliott07264132015-04-28 11:35:02 -06001934 "additional information.\n",
1935 "vkCreateInstance Failure");
Tony Barbour3dddd5d2015-04-29 16:19:20 -06001936 } else if (err == VK_ERROR_INVALID_EXTENSION) {
1937 ERR_EXIT("Cannot find a specified extension library"
1938 ".\nMake sure your layers path is set appropriately\n",
1939 "vkCreateInstance Failure");
Ian Elliott07264132015-04-28 11:35:02 -06001940 } else if (err) {
Ian Elliott65152912015-04-28 13:22:33 -06001941 ERR_EXIT("vkCreateInstance failed.\n\nDo you have a compatible Vulkan "
1942 "installable client driver (ICD) installed?\nPlease look at "
Ian Elliott07264132015-04-28 11:35:02 -06001943 "the Getting Started guide for additional information.\n",
1944 "vkCreateInstance Failure");
Ian Elliott3979e282015-04-03 15:24:55 -06001945 }
Jon Ashburnab46b362015-04-04 14:52:07 -06001946
Tony Barbour3dddd5d2015-04-29 16:19:20 -06001947
Jon Ashburn07c0c0c2015-04-15 11:31:12 -06001948 gpu_count = 1;
1949 err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, &demo->gpu);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001950 assert(!err && gpu_count == 1);
1951
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06001952
Tony Barbour3dddd5d2015-04-29 16:19:20 -06001953 if (demo->validate) {
Tony Barbourd70b42c2015-06-30 14:14:19 -06001954 demo->dbgCreateMsgCallback = vkGetInstanceProcAddr(demo->inst, "vkDbgCreateMsgCallback");
1955 demo->dbgDestroyMsgCallback = vkGetInstanceProcAddr(demo->inst, "vkDbgDestroyMsgCallback");
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06001956 if (!demo->dbgCreateMsgCallback) {
1957 ERR_EXIT("GetProcAddr: Unable to find vkDbgCreateMsgCallback\n",
1958 "vkGetProcAddr Failure");
1959 }
Tony Barbourd70b42c2015-06-30 14:14:19 -06001960 if (!demo->dbgDestroyMsgCallback) {
1961 ERR_EXIT("GetProcAddr: Unable to find vkDbgDestroyMsgCallback\n",
1962 "vkGetProcAddr Failure");
1963 }
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06001964 err = demo->dbgCreateMsgCallback(
1965 demo->inst,
1966 VK_DBG_REPORT_ERROR_BIT | VK_DBG_REPORT_WARN_BIT,
1967 dbgFunc, NULL,
1968 &demo->msg_callback);
1969 switch (err) {
1970 case VK_SUCCESS:
1971 break;
1972 case VK_ERROR_INVALID_POINTER:
1973 ERR_EXIT("dbgCreateMsgCallback: Invalid pointer\n",
1974 "dbgCreateMsgCallback Failure");
1975 break;
1976 case VK_ERROR_OUT_OF_HOST_MEMORY:
1977 ERR_EXIT("dbgCreateMsgCallback: out of host memory\n",
1978 "dbgCreateMsgCallback Failure");
1979 break;
1980 default:
1981 ERR_EXIT("dbgCreateMsgCallback: unknown failure\n",
1982 "dbgCreateMsgCallback Failure");
1983 break;
1984 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06001985 }
1986
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001987 err = vkCreateDevice(demo->gpu, &device, &demo->device);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001988 assert(!err);
1989
Ian Elliott673898b2015-06-22 15:07:49 -06001990 GET_DEVICE_PROC_ADDR(demo->device, CreateSwapChainWSI);
1991 GET_DEVICE_PROC_ADDR(demo->device, CreateSwapChainWSI);
1992 GET_DEVICE_PROC_ADDR(demo->device, DestroySwapChainWSI);
1993 GET_DEVICE_PROC_ADDR(demo->device, GetSwapChainInfoWSI);
1994 GET_DEVICE_PROC_ADDR(demo->device, QueuePresentWSI);
Jon Ashburn0b85d052015-05-21 18:13:33 -06001995
Tony Barbourecf1b2b2015-06-24 16:06:58 -06001996 err = vkGetPhysicalDeviceProperties(demo->gpu, &demo->gpu_props);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001997 assert(!err);
1998
Tony Barbourecf1b2b2015-06-24 16:06:58 -06001999 err = vkGetPhysicalDeviceQueueCount(demo->gpu, &queue_count);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06002000 assert(!err);
2001
Tony Barbourecf1b2b2015-06-24 16:06:58 -06002002 demo->queue_props = (VkPhysicalDeviceQueueProperties *) malloc(queue_count * sizeof(VkPhysicalDeviceQueueProperties));
2003 err = vkGetPhysicalDeviceQueueProperties(demo->gpu, queue_count, demo->queue_props);
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07002004 assert(!err);
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07002005 assert(queue_count >= 1);
2006
Mark Lobodzinskida9b1092015-04-16 11:44:05 -05002007 // Graphics queue and MemMgr queue can be separate.
2008 // TODO: Add support for separate queues, including synchronization,
Mark Lobodzinski6baaec72015-05-11 17:21:15 -05002009 // and appropriate tracking for QueueSubmit
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07002010 for (i = 0; i < queue_count; i++) {
Mark Lobodzinski6baaec72015-05-11 17:21:15 -05002011 if (demo->queue_props[i].queueFlags & VK_QUEUE_GRAPHICS_BIT)
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07002012 break;
2013 }
2014 assert(i < queue_count);
2015 demo->graphics_queue_node_index = i;
2016
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002017 err = vkGetDeviceQueue(demo->device, demo->graphics_queue_node_index,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002018 0, &demo->queue);
2019 assert(!err);
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002020
Jon Ashburnd7130cb2015-06-16 12:44:51 -06002021 // for now hardcode format till get WSI support
2022 demo->format = VK_FORMAT_B8G8R8A8_UNORM;
Ian Elliotte4602cd2015-04-21 16:41:02 -06002023
David Pinedo2bb7c932015-06-18 17:03:14 -06002024 demo->quit = false;
2025 demo->curFrame = 0;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002026}
2027
2028static void demo_init_connection(struct demo *demo)
2029{
Ian Elliott639ca472015-04-16 15:23:05 -06002030#ifndef _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002031 const xcb_setup_t *setup;
2032 xcb_screen_iterator_t iter;
2033 int scr;
2034
2035 demo->connection = xcb_connect(NULL, &scr);
Ian Elliott3979e282015-04-03 15:24:55 -06002036 if (demo->connection == NULL) {
2037 printf("Cannot find a compatible Vulkan installable client driver "
2038 "(ICD).\nExiting ...\n");
2039 fflush(stdout);
2040 exit(1);
2041 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002042
2043 setup = xcb_get_setup(demo->connection);
2044 iter = xcb_setup_roots_iterator(setup);
2045 while (scr-- > 0)
2046 xcb_screen_next(&iter);
2047
2048 demo->screen = iter.data;
Ian Elliott639ca472015-04-16 15:23:05 -06002049#endif // _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002050}
2051
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002052static void demo_init(struct demo *demo, int argc, char **argv)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002053{
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002054 vec3 eye = {0.0f, 3.0f, 5.0f};
2055 vec3 origin = {0, 0, 0};
Chia-I Wuae3b55d2015-04-22 14:56:17 +08002056 vec3 up = {0.0f, 1.0f, 0.0};
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002057
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002058 memset(demo, 0, sizeof(*demo));
David Pinedo2bb7c932015-06-18 17:03:14 -06002059 demo->frameCount = INT_MAX;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002060
Piers Daniell735ee532015-02-23 16:23:13 -07002061 for (int i = 1; i < argc; i++) {
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002062 if (strcmp(argv[i], "--use_staging") == 0) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002063 demo->use_staging_buffer = true;
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002064 continue;
Ian Elliott639ca472015-04-16 15:23:05 -06002065 }
Cody Northrop1fedb212015-05-28 11:27:16 -06002066 if (strcmp(argv[i], "--use_glsl") == 0) {
2067 demo->use_glsl = true;
2068 continue;
2069 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002070 if (strcmp(argv[i], "--validate") == 0) {
2071 demo->validate = true;
2072 continue;
2073 }
David Pinedo2bb7c932015-06-18 17:03:14 -06002074 if (strcmp(argv[i], "--c") == 0 &&
2075 demo->frameCount == INT_MAX &&
2076 i < argc-1 &&
2077 sscanf(argv[i+1],"%d", &demo->frameCount) == 1 &&
2078 demo->frameCount >= 0)
2079 {
2080 i++;
2081 continue;
2082 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002083
David Pinedo2bb7c932015-06-18 17:03:14 -06002084 fprintf(stderr, "Usage:\n %s [--use_staging] [--validate] [--c <framecount>]\n", APP_SHORT_NAME);
Ian Elliott639ca472015-04-16 15:23:05 -06002085 fflush(stderr);
2086 exit(1);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002087 }
2088
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002089 demo_init_connection(demo);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002090 demo_init_vk(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002091
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002092 demo->width = 500;
2093 demo->height = 500;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002094
2095 demo->spin_angle = 0.01f;
2096 demo->spin_increment = 0.01f;
2097 demo->pause = false;
2098
Piers Daniell735ee532015-02-23 16:23:13 -07002099 mat4x4_perspective(demo->projection_matrix, (float)degreesToRadians(45.0f), 1.0f, 0.1f, 100.0f);
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002100 mat4x4_look_at(demo->view_matrix, eye, origin, up);
2101 mat4x4_identity(demo->model_matrix);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002102}
2103
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002104
Ian Elliott639ca472015-04-16 15:23:05 -06002105#ifdef _WIN32
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002106extern int __getmainargs(
2107 int * _Argc,
2108 char *** _Argv,
2109 char *** _Env,
2110 int _DoWildCard,
2111 int * new_mode);
Ian Elliottf9cf78c2015-04-28 10:33:11 -06002112
Ian Elliotta748eaf2015-04-28 15:50:36 -06002113int WINAPI WinMain(HINSTANCE hInstance,
2114 HINSTANCE hPrevInstance,
2115 LPSTR pCmdLine,
2116 int nCmdShow)
Ian Elliott639ca472015-04-16 15:23:05 -06002117{
2118 MSG msg; // message
2119 bool done; // flag saying when app is complete
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002120 int argc;
2121 char** argv;
2122 char** env;
2123 int new_mode = 0;
Ian Elliott639ca472015-04-16 15:23:05 -06002124
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002125 __getmainargs(&argc,&argv,&env,0,&new_mode);
2126
2127 demo_init(&demo, argc, argv);
2128 demo.connection = hInstance;
2129 strncpy(demo.name, "cube", APP_NAME_STR_LEN);
Ian Elliott639ca472015-04-16 15:23:05 -06002130 demo_create_window(&demo);
2131
2132 demo_prepare(&demo);
2133
2134 done = false; //initialize loop condition variable
2135 /* main message loop*/
2136 while(!done)
2137 {
Ian Elliotta748eaf2015-04-28 15:50:36 -06002138 PeekMessage(&msg, NULL, 0, 0, PM_REMOVE);
Ian Elliott639ca472015-04-16 15:23:05 -06002139 if (msg.message == WM_QUIT) //check for a quit message
2140 {
2141 done = true; //if found, quit app
2142 }
2143 else
2144 {
2145 /* Translate and dispatch to event queue*/
2146 TranslateMessage(&msg);
2147 DispatchMessage(&msg);
2148 }
2149 }
2150
2151 demo_cleanup(&demo);
2152
Tony Barbourf9921732015-04-22 11:36:22 -06002153 return (int) msg.wParam;
Ian Elliott639ca472015-04-16 15:23:05 -06002154}
2155#else // _WIN32
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002156int main(int argc, char **argv)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002157{
2158 struct demo demo;
2159
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002160 demo_init(&demo, argc, argv);
Chia-I Wucbb564e2015-04-16 22:02:10 +08002161 demo_create_window(&demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002162
2163 demo_prepare(&demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002164 demo_run(&demo);
2165
2166 demo_cleanup(&demo);
2167
2168 return 0;
2169}
Ian Elliott639ca472015-04-16 15:23:05 -06002170#endif // _WIN32