blob: 1ffaea14d8530b5e8571c3b6bbc99ac664e304c5 [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
Tony Barbour50bbca42015-06-29 16:20:35 -0600447 VkPipelineStageFlags src_stages = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
448 VkPipelineStageFlags dest_stages = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600449
Tony Barbour50bbca42015-06-29 16:20:35 -0600450 vkCmdPipelineBarrier(demo->cmd, src_stages, dest_stages, false, 1, (const void **)&pmemory_barrier);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600451}
452
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600453static void demo_draw_build_cmd(struct demo *demo, VkCmdBuffer cmd_buf)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600454{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600455 const VkColorAttachmentBindInfo color_attachment = {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600456 .view = demo->buffers[demo->current_buffer].view,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600457 .layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600458 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600459 const VkDepthStencilBindInfo depth_stencil = {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600460 .view = demo->depth.view,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600461 .layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600462 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600463 const VkClearColor clear_color = {
Courtney Goeltzenleuchter374553c2015-04-03 16:35:32 -0600464 .color.floatColor = { 0.2f, 0.2f, 0.2f, 0.2f },
465 .useRawValue = false,
466 };
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600467 const float clear_depth = 1.0f;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600468 VkImageSubresourceRange clear_range;
469 VkCmdBufferBeginInfo cmd_buf_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600470 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO,
Courtney Goeltzenleuchter53968d82015-04-03 15:25:24 -0600471 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -0600472 .flags = VK_CMD_BUFFER_OPTIMIZE_SMALL_BATCH_BIT |
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600473 VK_CMD_BUFFER_OPTIMIZE_ONE_TIME_SUBMIT_BIT,
Jon Ashburn60ccfbe2014-12-31 17:08:35 -0700474 };
Tony Barbourfdc2d352015-04-22 09:02:32 -0600475 VkResult U_ASSERT_ONLY err;
Chris Forbes40a71562015-06-17 11:36:12 +1200476 VkAttachmentLoadOp load_op = VK_ATTACHMENT_LOAD_OP_CLEAR;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600477 VkAttachmentStoreOp store_op = VK_ATTACHMENT_STORE_OP_DONT_CARE;
478 const VkFramebufferCreateInfo fb_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600479 .sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO,
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700480 .pNext = NULL,
481 .colorAttachmentCount = 1,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600482 .pColorAttachments = (VkColorAttachmentBindInfo*) &color_attachment,
483 .pDepthStencilAttachment = (VkDepthStencilBindInfo*) &depth_stencil,
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700484 .sampleCount = 1,
Mark Lobodzinskic06b7412015-01-27 13:24:03 -0600485 .width = demo->width,
486 .height = demo->height,
487 .layers = 1,
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700488 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600489 VkRenderPassCreateInfo rp_info;
490 VkRenderPassBegin rp_begin;
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700491
492 memset(&rp_info, 0 , sizeof(rp_info));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600493 err = vkCreateFramebuffer(demo->device, &fb_info, &rp_begin.framebuffer);
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700494 assert(!err);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600495 rp_info.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO;
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700496 rp_info.renderArea.extent.width = demo->width;
497 rp_info.renderArea.extent.height = demo->height;
Courtney Goeltzenleuchter53968d82015-04-03 15:25:24 -0600498 rp_info.colorAttachmentCount = fb_info.colorAttachmentCount;
499 rp_info.pColorFormats = &demo->format;
500 rp_info.pColorLayouts = &color_attachment.layout;
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700501 rp_info.pColorLoadOps = &load_op;
502 rp_info.pColorStoreOps = &store_op;
Courtney Goeltzenleuchter53968d82015-04-03 15:25:24 -0600503 rp_info.pColorLoadClearValues = &clear_color;
Tony Barbour72304ef2015-04-16 15:59:00 -0600504 rp_info.depthStencilFormat = VK_FORMAT_D16_UNORM;
Courtney Goeltzenleuchter53968d82015-04-03 15:25:24 -0600505 rp_info.depthStencilLayout = depth_stencil.layout;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600506 rp_info.depthLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
Courtney Goeltzenleuchter53968d82015-04-03 15:25:24 -0600507 rp_info.depthLoadClearValue = clear_depth;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600508 rp_info.depthStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
509 rp_info.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
Courtney Goeltzenleuchter53968d82015-04-03 15:25:24 -0600510 rp_info.stencilLoadClearValue = 0;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600511 rp_info.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
Tony Barbourd70b42c2015-06-30 14:14:19 -0600512 rp_info.sampleCount = 1;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600513 err = vkCreateRenderPass(demo->device, &rp_info, &rp_begin.renderPass);
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700514 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600515
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600516 err = vkBeginCommandBuffer(cmd_buf, &cmd_buf_info);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600517 assert(!err);
518
Tobin Ehlis080219d2015-06-24 15:53:07 -0600519 vkCmdBeginRenderPass(cmd_buf, &rp_begin);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600520 vkCmdBindPipeline(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600521 demo->pipeline);
Mark Lobodzinskic6e8b3d2015-06-15 13:21:21 -0600522 vkCmdBindDescriptorSets(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS, demo->pipeline_layout,
Cody Northropfb5185a2015-04-16 13:41:56 -0600523 0, 1, &demo->desc_set, 0, NULL);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600524
Tony Barbour72304ef2015-04-16 15:59:00 -0600525 vkCmdBindDynamicStateObject(cmd_buf, VK_STATE_BIND_POINT_VIEWPORT, demo->viewport);
526 vkCmdBindDynamicStateObject(cmd_buf, VK_STATE_BIND_POINT_RASTER, demo->raster);
527 vkCmdBindDynamicStateObject(cmd_buf, VK_STATE_BIND_POINT_COLOR_BLEND,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600528 demo->color_blend);
Tony Barbour72304ef2015-04-16 15:59:00 -0600529 vkCmdBindDynamicStateObject(cmd_buf, VK_STATE_BIND_POINT_DEPTH_STENCIL,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600530 demo->depth_stencil);
531
Chris Forbes40a71562015-06-17 11:36:12 +1200532 clear_range.aspect = VK_IMAGE_ASPECT_DEPTH;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600533 clear_range.baseMipLevel = 0;
534 clear_range.mipLevels = 1;
535 clear_range.baseArraySlice = 0;
536 clear_range.arraySize = 1;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600537
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600538 vkCmdClearDepthStencil(cmd_buf, demo->depth.image,
539 VK_IMAGE_LAYOUT_CLEAR_OPTIMAL,
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600540 clear_depth, 0, 1, &clear_range);
541
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600542 vkCmdDraw(cmd_buf, 0, 12 * 3, 0, 1);
543 vkCmdEndRenderPass(cmd_buf, rp_begin.renderPass);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600544
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600545 err = vkEndCommandBuffer(cmd_buf);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600546 assert(!err);
Courtney Goeltzenleuchter94901dc2015-02-25 17:53:18 -0700547
Mike Stroyanebae8322015-04-17 12:36:38 -0600548 vkDestroyObject(demo->device, VK_OBJECT_TYPE_RENDER_PASS, rp_begin.renderPass);
549 vkDestroyObject(demo->device, VK_OBJECT_TYPE_FRAMEBUFFER, rp_begin.framebuffer);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600550}
551
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600552
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600553void demo_update_data_buffer(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600554{
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600555 mat4x4 MVP, Model, VP;
556 int matrixSize = sizeof(MVP);
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600557 uint8_t *pData;
Tony Barbourfdc2d352015-04-22 09:02:32 -0600558 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600559
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600560 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600561
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600562 // Rotate 22.5 degrees around the Y axis
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600563 mat4x4_dup(Model, demo->model_matrix);
Piers Daniell735ee532015-02-23 16:23:13 -0700564 mat4x4_rotate(demo->model_matrix, Model, 0.0f, 1.0f, 0.0f, (float)degreesToRadians(demo->spin_angle));
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600565 mat4x4_mul(MVP, VP, demo->model_matrix);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600566
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500567 err = vkMapMemory(demo->device, demo->uniform_data.mem, 0, 0, 0, (void **) &pData);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600568 assert(!err);
569
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600570 memcpy(pData, (const void*) &MVP[0][0], matrixSize);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600571
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500572 err = vkUnmapMemory(demo->device, demo->uniform_data.mem);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600573 assert(!err);
574}
575
576static void demo_draw(struct demo *demo)
577{
Chia-I Wucbb564e2015-04-16 22:02:10 +0800578 const VkPresentInfoWSI present = {
579 .sType = VK_STRUCTURE_TYPE_PRESENT_INFO_WSI,
580 .pNext = NULL,
581 .image = demo->buffers[demo->current_buffer].image,
582 .flipInterval = 0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600583 };
Tony Barbourfdc2d352015-04-22 09:02:32 -0600584 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600585
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600586 err = vkQueueSubmit(demo->queue, 1, &demo->buffers[demo->current_buffer].cmd,
587 VK_NULL_HANDLE);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600588 assert(!err);
589
Jon Ashburn0b85d052015-05-21 18:13:33 -0600590 err = demo->fpQueuePresentWSI(demo->queue, &present);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600591 assert(!err);
592
593 demo->current_buffer = (demo->current_buffer + 1) % DEMO_BUFFER_COUNT;
Chia-I Wucbb564e2015-04-16 22:02:10 +0800594
595 err = vkQueueWaitIdle(demo->queue);
596 assert(err == VK_SUCCESS);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600597}
598
599static void demo_prepare_buffers(struct demo *demo)
600{
Chia-I Wucbb564e2015-04-16 22:02:10 +0800601 const VkSwapChainCreateInfoWSI swap_chain = {
602 .sType = VK_STRUCTURE_TYPE_SWAP_CHAIN_CREATE_INFO_WSI,
603 .pNext = NULL,
604 .pNativeWindowSystemHandle = demo->connection,
605 .pNativeWindowHandle = (void *) (intptr_t) demo->window,
Ian Elliotte4602cd2015-04-21 16:41:02 -0600606 .displayCount = 1,
Chia-I Wucbb564e2015-04-16 22:02:10 +0800607 .imageCount = DEMO_BUFFER_COUNT,
608 .imageFormat = demo->format,
609 .imageExtent = {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600610 .width = demo->width,
611 .height = demo->height,
612 },
Chia-I Wucbb564e2015-04-16 22:02:10 +0800613 .imageArraySize = 1,
614 .imageUsageFlags = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600615 };
Chia-I Wucbb564e2015-04-16 22:02:10 +0800616 VkSwapChainImageInfoWSI images[DEMO_BUFFER_COUNT];
617 size_t images_size = sizeof(images);
Tony Barbourfdc2d352015-04-22 09:02:32 -0600618 VkResult U_ASSERT_ONLY err;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600619 uint32_t i;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600620
Jon Ashburn0b85d052015-05-21 18:13:33 -0600621 err = demo->fpCreateSwapChainWSI(demo->device, &swap_chain, &demo->swap_chain);
Chia-I Wucbb564e2015-04-16 22:02:10 +0800622 assert(!err);
623
Jon Ashburn0b85d052015-05-21 18:13:33 -0600624 err = demo->fpGetSwapChainInfoWSI(demo->swap_chain,
Chia-I Wucbb564e2015-04-16 22:02:10 +0800625 VK_SWAP_CHAIN_INFO_TYPE_PERSISTENT_IMAGES_WSI,
626 &images_size, images);
627 assert(!err && images_size == sizeof(images));
628
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600629 for (i = 0; i < DEMO_BUFFER_COUNT; i++) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600630 VkColorAttachmentViewCreateInfo color_attachment_view = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600631 .sType = VK_STRUCTURE_TYPE_COLOR_ATTACHMENT_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600632 .pNext = NULL,
633 .format = demo->format,
634 .mipLevel = 0,
635 .baseArraySlice = 0,
636 .arraySize = 1,
637 };
638
Chia-I Wucbb564e2015-04-16 22:02:10 +0800639 demo->buffers[i].image = images[i].image;
640 demo->buffers[i].mem = images[i].memory;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600641
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600642 demo_set_image_layout(demo, demo->buffers[i].image,
malnasseeb25d3f2015-06-03 17:28:38 -0400643 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600644 VK_IMAGE_LAYOUT_UNDEFINED,
645 VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600646
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600647 color_attachment_view.image = demo->buffers[i].image;
648
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600649 err = vkCreateColorAttachmentView(demo->device,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600650 &color_attachment_view, &demo->buffers[i].view);
651 assert(!err);
652 }
653}
654
655static void demo_prepare_depth(struct demo *demo)
656{
Tony Barbour72304ef2015-04-16 15:59:00 -0600657 const VkFormat depth_format = VK_FORMAT_D16_UNORM;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600658 const VkImageCreateInfo image = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600659 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600660 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -0600661 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600662 .format = depth_format,
663 .extent = { demo->width, demo->height, 1 },
664 .mipLevels = 1,
665 .arraySize = 1,
666 .samples = 1,
Tony Barbour72304ef2015-04-16 15:59:00 -0600667 .tiling = VK_IMAGE_TILING_OPTIMAL,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600668 .usage = VK_IMAGE_USAGE_DEPTH_STENCIL_BIT,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600669 .flags = 0,
670 };
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -0600671 VkMemoryAllocInfo mem_alloc = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600672 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
Mark Lobodzinskia6907f72015-04-16 08:52:00 -0500673 .pNext = NULL,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600674 .allocationSize = 0,
Tony Barbour72304ef2015-04-16 15:59:00 -0600675 .memProps = VK_MEMORY_PROPERTY_DEVICE_ONLY,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600676 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600677 VkDepthStencilViewCreateInfo view = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600678 .sType = VK_STRUCTURE_TYPE_DEPTH_STENCIL_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600679 .pNext = NULL,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600680 .image = VK_NULL_HANDLE,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600681 .mipLevel = 0,
682 .baseArraySlice = 0,
683 .arraySize = 1,
684 .flags = 0,
685 };
Mike Stroyanebae8322015-04-17 12:36:38 -0600686
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500687 VkMemoryRequirements mem_reqs;
Tony Barbourfdc2d352015-04-22 09:02:32 -0600688 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600689
690 demo->depth.format = depth_format;
691
692 /* create image */
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600693 err = vkCreateImage(demo->device, &image,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600694 &demo->depth.image);
695 assert(!err);
696
Tony Barbourecf1b2b2015-06-24 16:06:58 -0600697 err = vkGetObjectMemoryRequirements(demo->device,
698 VK_OBJECT_TYPE_IMAGE, demo->depth.image, &mem_reqs);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500699 mem_alloc.allocationSize = mem_reqs.size;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600700
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500701 /* allocate memory */
702 err = vkAllocMemory(demo->device, &mem_alloc, &demo->depth.mem);
703 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600704
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500705 /* bind memory */
706 err = vkBindObjectMemory(demo->device,
707 VK_OBJECT_TYPE_IMAGE, demo->depth.image,
708 demo->depth.mem, 0);
709 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600710
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600711 demo_set_image_layout(demo, demo->depth.image,
malnasseeb25d3f2015-06-03 17:28:38 -0400712 VK_IMAGE_ASPECT_DEPTH,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600713 VK_IMAGE_LAYOUT_UNDEFINED,
714 VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600715
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600716 /* create image view */
717 view.image = demo->depth.image;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600718 err = vkCreateDepthStencilView(demo->device, &view,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600719 &demo->depth.view);
720 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600721}
722
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600723/** loadTexture
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -0600724 * loads a png file into an memory object, using cstdio , libpng.
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600725 *
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -0600726 * \param demo : Needed to access VK calls
727 * \param filename : the png file to be loaded
728 * \param width : width of png, to be updated as a side effect of this function
729 * \param height : height of png, to be updated as a side effect of this function
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600730 *
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -0600731 * \return bool : an opengl texture id. true if successful?,
732 * should be validated by the client of this function.
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600733 *
734 * Source: http://en.wikibooks.org/wiki/OpenGL_Programming/Intermediate/Textures
735 * Modified to copy image to memory
736 *
737 */
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700738bool loadTexture(const char *filename, uint8_t *rgba_data,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600739 VkSubresourceLayout *layout,
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600740 int32_t *width, int32_t *height)
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600741{
742 //header for testing if it is a png
743 png_byte header[8];
Tony Barbourf9921732015-04-22 11:36:22 -0600744 int is_png, bit_depth, color_type, rowbytes;
745 size_t retval;
Ian Elliott1e42dff2015-02-13 14:29:21 -0700746 png_uint_32 i, twidth, theight;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600747 png_structp png_ptr;
748 png_infop info_ptr, end_info;
749 png_byte *image_data;
750 png_bytep *row_pointers;
751
752 //open file as binary
753 FILE *fp = fopen(filename, "rb");
754 if (!fp) {
755 return false;
756 }
757
758 //read the header
Tony Barbourfdc2d352015-04-22 09:02:32 -0600759 retval = fread(header, 1, 8, fp);
760 if (retval != 8) {
761 fclose(fp);
762 return false;
763 }
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600764
765 //test if png
766 is_png = !png_sig_cmp(header, 0, 8);
767 if (!is_png) {
768 fclose(fp);
769 return false;
770 }
771
772 //create png struct
773 png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL,
774 NULL, NULL);
775 if (!png_ptr) {
776 fclose(fp);
777 return (false);
778 }
779
780 //create png info struct
781 info_ptr = png_create_info_struct(png_ptr);
782 if (!info_ptr) {
783 png_destroy_read_struct(&png_ptr, (png_infopp) NULL, (png_infopp) NULL);
784 fclose(fp);
785 return (false);
786 }
787
788 //create png info struct
789 end_info = png_create_info_struct(png_ptr);
790 if (!end_info) {
791 png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp) NULL);
792 fclose(fp);
793 return (false);
794 }
795
796 //png error stuff, not sure libpng man suggests this.
797 if (setjmp(png_jmpbuf(png_ptr))) {
798 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
799 fclose(fp);
800 return (false);
801 }
802
803 //init png reading
804 png_init_io(png_ptr, fp);
805
806 //let libpng know you already read the first 8 bytes
807 png_set_sig_bytes(png_ptr, 8);
808
809 // read all the info up to the image data
810 png_read_info(png_ptr, info_ptr);
811
812 // get info about png
813 png_get_IHDR(png_ptr, info_ptr, &twidth, &theight, &bit_depth, &color_type,
814 NULL, NULL, NULL);
815
816 //update width and height based on png info
817 *width = twidth;
818 *height = theight;
819
820 // Require that incoming texture be 8bits per color component
821 // and 4 components (RGBA).
822 if (png_get_bit_depth(png_ptr, info_ptr) != 8 ||
823 png_get_channels(png_ptr, info_ptr) != 4) {
824 return false;
825 }
826
827 if (rgba_data == NULL) {
828 // If data pointer is null, we just want the width & height
829 // clean up memory and close stuff
830 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
831 fclose(fp);
832
833 return true;
834 }
835
836 // Update the png info struct.
837 png_read_update_info(png_ptr, info_ptr);
838
839 // Row size in bytes.
840 rowbytes = png_get_rowbytes(png_ptr, info_ptr);
841
842 // Allocate the image_data as a big block, to be given to opengl
843 image_data = (png_byte *)malloc(rowbytes * theight * sizeof(png_byte));
844 if (!image_data) {
845 //clean up memory and close stuff
846 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
847 fclose(fp);
848 return false;
849 }
850
851 // row_pointers is for pointing to image_data for reading the png with libpng
852 row_pointers = (png_bytep *)malloc(theight * sizeof(png_bytep));
853 if (!row_pointers) {
854 //clean up memory and close stuff
855 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
856 // delete[] image_data;
857 fclose(fp);
858 return false;
859 }
860 // set the individual row_pointers to point at the correct offsets of image_data
861 for (i = 0; i < theight; ++i)
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700862 row_pointers[theight - 1 - i] = rgba_data + i * layout->rowPitch;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600863
864 // read the png into image_data through row_pointers
865 png_read_image(png_ptr, row_pointers);
866
867 // clean up memory and close stuff
868 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
869 free(row_pointers);
870 free(image_data);
871 fclose(fp);
872
873 return true;
874}
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600875
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700876static void demo_prepare_texture_image(struct demo *demo,
877 const char *filename,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600878 struct texture_object *tex_obj,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600879 VkImageTiling tiling,
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -0600880 VkImageUsageFlags usage,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600881 VkFlags mem_props)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600882{
Mike Stroyan8b89e072015-06-15 14:21:03 -0600883 const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600884 int32_t tex_width;
885 int32_t tex_height;
Tony Barbourfdc2d352015-04-22 09:02:32 -0600886 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700887
David Pinedo30bd71d2015-04-23 08:16:57 -0600888 if (!loadTexture(filename, NULL, NULL, &tex_width, &tex_height))
889 {
890 printf("Failed to load textures\n");
891 fflush(stdout);
892 exit(1);
893 }
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700894
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600895 tex_obj->tex_width = tex_width;
896 tex_obj->tex_height = tex_height;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700897
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600898 const VkImageCreateInfo image_create_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600899 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700900 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -0600901 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700902 .format = tex_format,
903 .extent = { tex_width, tex_height, 1 },
904 .mipLevels = 1,
905 .arraySize = 1,
906 .samples = 1,
907 .tiling = tiling,
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -0600908 .usage = usage,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700909 .flags = 0,
910 };
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -0600911 VkMemoryAllocInfo mem_alloc = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600912 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
Mark Lobodzinskia6907f72015-04-16 08:52:00 -0500913 .pNext = NULL,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700914 .allocationSize = 0,
915 .memProps = mem_props,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700916 };
917
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500918 VkMemoryRequirements mem_reqs;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700919
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600920 err = vkCreateImage(demo->device, &image_create_info,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600921 &tex_obj->image);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700922 assert(!err);
923
Tony Barbourecf1b2b2015-06-24 16:06:58 -0600924 err = vkGetObjectMemoryRequirements(demo->device,
925 VK_OBJECT_TYPE_IMAGE, tex_obj->image, &mem_reqs);
926 assert(!err);
Piers Daniell735ee532015-02-23 16:23:13 -0700927
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500928 mem_alloc.allocationSize = mem_reqs.size;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700929
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500930 /* allocate memory */
931 err = vkAllocMemory(demo->device, &mem_alloc,
932 &(tex_obj->mem));
933 assert(!err);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700934
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500935 /* bind memory */
936 err = vkBindObjectMemory(demo->device,
937 VK_OBJECT_TYPE_IMAGE, tex_obj->image,
938 tex_obj->mem, 0);
939 assert(!err);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700940
Tony Barbour72304ef2015-04-16 15:59:00 -0600941 if (mem_props & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600942 const VkImageSubresource subres = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600943 .aspect = VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700944 .mipLevel = 0,
945 .arraySlice = 0,
946 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600947 VkSubresourceLayout layout;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700948 void *data;
949
Tony Barbourecf1b2b2015-06-24 16:06:58 -0600950 err = vkGetImageSubresourceLayout(demo->device, tex_obj->image, &subres, &layout);
951 assert(!err);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700952
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500953 err = vkMapMemory(demo->device, tex_obj->mem, 0, 0, 0, &data);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700954 assert(!err);
955
956 if (!loadTexture(filename, data, &layout, &tex_width, &tex_height)) {
957 fprintf(stderr, "Error loading texture: %s\n", filename);
958 }
959
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500960 err = vkUnmapMemory(demo->device, tex_obj->mem);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700961 assert(!err);
962 }
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600963
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600964 tex_obj->imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600965 demo_set_image_layout(demo, tex_obj->image,
malnasseeb25d3f2015-06-03 17:28:38 -0400966 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600967 VK_IMAGE_LAYOUT_UNDEFINED,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600968 tex_obj->imageLayout);
969 /* 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 -0700970}
971
Mark Lobodzinskida9b1092015-04-16 11:44:05 -0500972static void demo_destroy_texture_image(struct demo *demo, struct texture_object *tex_objs)
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700973{
974 /* clean up staging resources */
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500975 vkFreeMemory(demo->device, tex_objs->mem);
Mike Stroyanebae8322015-04-17 12:36:38 -0600976 vkDestroyObject(demo->device, VK_OBJECT_TYPE_IMAGE, tex_objs->image);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700977}
978
979static void demo_prepare_textures(struct demo *demo)
980{
Tony Barbour72304ef2015-04-16 15:59:00 -0600981 const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600982 VkFormatProperties props;
Tony Barbourfdc2d352015-04-22 09:02:32 -0600983 VkResult U_ASSERT_ONLY err;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600984 uint32_t i;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600985
Chris Forbesf576a3e2015-06-21 22:55:02 +1200986 err = vkGetPhysicalDeviceFormatInfo(demo->gpu, tex_format, &props);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700987 assert(!err);
988
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600989 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700990
FslNopper6a7e50e2015-05-06 21:42:01 +0200991 if ((props.linearTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) && !demo->use_staging_buffer) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700992 /* Device can texture using linear textures */
993 demo_prepare_texture_image(demo, tex_files[i], &demo->textures[i],
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -0600994 VK_IMAGE_TILING_LINEAR, VK_IMAGE_USAGE_SAMPLED_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
Tony Barbour72304ef2015-04-16 15:59:00 -0600995 } else if (props.optimalTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700996 /* Must use staging buffer to copy linear texture to optimized */
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600997 struct texture_object staging_texture;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700998
999 memset(&staging_texture, 0, sizeof(staging_texture));
1000 demo_prepare_texture_image(demo, tex_files[i], &staging_texture,
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001001 VK_IMAGE_TILING_LINEAR, VK_IMAGE_USAGE_TRANSFER_SOURCE_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001002
1003 demo_prepare_texture_image(demo, tex_files[i], &demo->textures[i],
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001004 VK_IMAGE_TILING_OPTIMAL,
1005 (VK_IMAGE_USAGE_TRANSFER_DESTINATION_BIT | VK_IMAGE_USAGE_SAMPLED_BIT),
1006 VK_MEMORY_PROPERTY_DEVICE_ONLY);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001007
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001008 demo_set_image_layout(demo, staging_texture.image,
malnasseeb25d3f2015-06-03 17:28:38 -04001009 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001010 staging_texture.imageLayout,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001011 VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001012
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001013 demo_set_image_layout(demo, demo->textures[i].image,
malnasseeb25d3f2015-06-03 17:28:38 -04001014 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001015 demo->textures[i].imageLayout,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001016 VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001017
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001018 VkImageCopy copy_region = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001019 .srcSubresource = { VK_IMAGE_ASPECT_COLOR, 0, 0 },
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001020 .srcOffset = { 0, 0, 0 },
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001021 .destSubresource = { VK_IMAGE_ASPECT_COLOR, 0, 0 },
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001022 .destOffset = { 0, 0, 0 },
1023 .extent = { staging_texture.tex_width, staging_texture.tex_height, 1 },
1024 };
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001025 vkCmdCopyImage(demo->cmd,
1026 staging_texture.image, VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL,
1027 demo->textures[i].image, VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL,
Courtney Goeltzenleuchter8e89a312015-03-25 11:25:10 -06001028 1, &copy_region);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001029
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001030 demo_set_image_layout(demo, demo->textures[i].image,
malnasseeb25d3f2015-06-03 17:28:38 -04001031 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001032 VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001033 demo->textures[i].imageLayout);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001034
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001035 demo_flush_init_cmd(demo);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001036
Courtney Goeltzenleuchterf3aeb2b2015-04-21 09:30:03 -06001037 demo_destroy_texture_image(demo, &staging_texture);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001038 } else {
Mike Stroyan8b89e072015-06-15 14:21:03 -06001039 /* Can't support VK_FORMAT_R8G8B8A8_UNORM !? */
1040 assert(!"No support for R8G8B8A8_UNORM as texture image format");
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001041 }
1042
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001043 const VkSamplerCreateInfo sampler = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001044 .sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001045 .pNext = NULL,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001046 .magFilter = VK_TEX_FILTER_NEAREST,
1047 .minFilter = VK_TEX_FILTER_NEAREST,
Tony Barbour72304ef2015-04-16 15:59:00 -06001048 .mipMode = VK_TEX_MIPMAP_MODE_BASE,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001049 .addressU = VK_TEX_ADDRESS_CLAMP,
1050 .addressV = VK_TEX_ADDRESS_CLAMP,
1051 .addressW = VK_TEX_ADDRESS_CLAMP,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001052 .mipLodBias = 0.0f,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001053 .maxAnisotropy = 1,
Tony Barbour72304ef2015-04-16 15:59:00 -06001054 .compareOp = VK_COMPARE_OP_NEVER,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001055 .minLod = 0.0f,
1056 .maxLod = 0.0f,
Tony Barbourcb530c72015-06-25 16:56:44 -06001057 .borderColor = VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001058 };
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001059
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001060 VkImageViewCreateInfo view = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001061 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001062 .pNext = NULL,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001063 .image = VK_NULL_HANDLE,
Tony Barbour72304ef2015-04-16 15:59:00 -06001064 .viewType = VK_IMAGE_VIEW_TYPE_2D,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001065 .format = tex_format,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001066 .channels = { VK_CHANNEL_SWIZZLE_R,
1067 VK_CHANNEL_SWIZZLE_G,
1068 VK_CHANNEL_SWIZZLE_B,
1069 VK_CHANNEL_SWIZZLE_A, },
1070 .subresourceRange = { VK_IMAGE_ASPECT_COLOR, 0, 1, 0, 1 },
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001071 };
Jon Ashburnb2a66652015-01-16 09:37:43 -07001072
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001073 /* create sampler */
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001074 err = vkCreateSampler(demo->device, &sampler,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001075 &demo->textures[i].sampler);
1076 assert(!err);
1077
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001078 /* create image view */
1079 view.image = demo->textures[i].image;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001080 err = vkCreateImageView(demo->device, &view,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001081 &demo->textures[i].view);
1082 assert(!err);
1083 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001084}
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001085
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001086void demo_prepare_cube_data_buffer(struct demo *demo)
1087{
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -06001088 VkBufferCreateInfo buf_info;
1089 VkBufferViewCreateInfo view_info;
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -06001090 VkMemoryAllocInfo alloc_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001091 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
Mark Lobodzinskia6907f72015-04-16 08:52:00 -05001092 .pNext = NULL,
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001093 .allocationSize = 0,
Tony Barbour72304ef2015-04-16 15:59:00 -06001094 .memProps = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT,
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001095 };
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001096 VkMemoryRequirements mem_reqs;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001097 size_t mem_reqs_size = sizeof(VkMemoryRequirements);
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001098 uint8_t *pData;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001099 int i;
1100 mat4x4 MVP, VP;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001101 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001102 struct vktexcube_vs_uniform data;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001103
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001104 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001105 mat4x4_mul(MVP, VP, demo->model_matrix);
1106 memcpy(data.mvp, MVP, sizeof(MVP));
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001107// dumpMatrix("MVP", MVP);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001108
1109 for (i=0; i<12*3; i++) {
1110 data.position[i][0] = g_vertex_buffer_data[i*3];
1111 data.position[i][1] = g_vertex_buffer_data[i*3+1];
1112 data.position[i][2] = g_vertex_buffer_data[i*3+2];
1113 data.position[i][3] = 1.0f;
1114 data.attr[i][0] = g_uv_buffer_data[2*i];
1115 data.attr[i][1] = g_uv_buffer_data[2*i + 1];
1116 data.attr[i][2] = 0;
1117 data.attr[i][3] = 0;
1118 }
1119
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001120 memset(&buf_info, 0, sizeof(buf_info));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001121 buf_info.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -06001122 buf_info.usage = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001123 err = vkCreateBuffer(demo->device, &buf_info, &demo->uniform_data.buf);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001124 assert(!err);
1125
Tony Barbourecf1b2b2015-06-24 16:06:58 -06001126 err = vkGetObjectMemoryRequirements(demo->device,
1127 VK_OBJECT_TYPE_BUFFER, demo->uniform_data.buf, &mem_reqs);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001128 assert(!err && mem_reqs_size == sizeof(mem_reqs));
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001129
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001130 alloc_info.allocationSize = mem_reqs.size;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001131
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001132 err = vkAllocMemory(demo->device, &alloc_info, &(demo->uniform_data.mem));
1133 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001134
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001135 err = vkMapMemory(demo->device, demo->uniform_data.mem, 0, 0, 0, (void **) &pData);
1136 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001137
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001138 memcpy(pData, &data, sizeof data);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001139
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001140 err = vkUnmapMemory(demo->device, demo->uniform_data.mem);
1141 assert(!err);
1142
1143 err = vkBindObjectMemory(demo->device,
1144 VK_OBJECT_TYPE_BUFFER, demo->uniform_data.buf,
1145 demo->uniform_data.mem, 0);
1146 assert(!err);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001147
1148 memset(&view_info, 0, sizeof(view_info));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001149 view_info.sType = VK_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO;
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001150 view_info.buffer = demo->uniform_data.buf;
Tony Barbour72304ef2015-04-16 15:59:00 -06001151 view_info.viewType = VK_BUFFER_VIEW_TYPE_RAW;
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001152 view_info.offset = 0;
1153 view_info.range = sizeof(data);
1154
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001155 err = vkCreateBufferView(demo->device, &view_info, &demo->uniform_data.view);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001156 assert(!err);
1157
Chia-I Wuae721ba2015-05-25 16:27:55 +08001158 demo->uniform_data.desc.bufferView = demo->uniform_data.view;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001159}
1160
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001161static void demo_prepare_descriptor_layout(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001162{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001163 const VkDescriptorSetLayoutBinding layout_bindings[2] = {
Chia-I Wua2aa8632015-03-26 15:04:41 +08001164 [0] = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001165 .descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
Chia-I Wu6d29a412015-05-25 16:22:52 +08001166 .arraySize = 1,
Tony Barbour72304ef2015-04-16 15:59:00 -06001167 .stageFlags = VK_SHADER_STAGE_VERTEX_BIT,
Chia-I Wu91e8e212015-03-27 12:56:09 +08001168 .pImmutableSamplers = NULL,
Chia-I Wua2aa8632015-03-26 15:04:41 +08001169 },
1170 [1] = {
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -06001171 .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
Chia-I Wu6d29a412015-05-25 16:22:52 +08001172 .arraySize = DEMO_TEXTURE_COUNT,
Tony Barbour72304ef2015-04-16 15:59:00 -06001173 .stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT,
Chia-I Wu91e8e212015-03-27 12:56:09 +08001174 .pImmutableSamplers = NULL,
Chia-I Wua2aa8632015-03-26 15:04:41 +08001175 },
1176 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001177 const VkDescriptorSetLayoutCreateInfo descriptor_layout = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001178 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001179 .pNext = NULL,
Chia-I Wua2aa8632015-03-26 15:04:41 +08001180 .count = 2,
1181 .pBinding = layout_bindings,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001182 };
Tony Barbourfdc2d352015-04-22 09:02:32 -06001183 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001184
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001185 err = vkCreateDescriptorSetLayout(demo->device,
Chia-I Wub58c24a2015-03-26 15:27:55 +08001186 &descriptor_layout, &demo->desc_layout);
1187 assert(!err);
1188
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001189 const VkPipelineLayoutCreateInfo pPipelineLayoutCreateInfo = {
1190 .sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
1191 .pNext = NULL,
1192 .descriptorSetCount = 1,
1193 .pSetLayouts = &demo->desc_layout,
1194 };
1195
1196 err = vkCreatePipelineLayout(demo->device,
1197 &pPipelineLayoutCreateInfo,
1198 &demo->pipeline_layout);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001199 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001200}
1201
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001202static VkShader demo_prepare_shader(struct demo *demo,
Tony Barbour72304ef2015-04-16 15:59:00 -06001203 VkShaderStage stage,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001204 const void *code,
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001205 size_t size)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001206{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001207 VkShaderCreateInfo createInfo;
1208 VkShader shader;
1209 VkResult err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001210
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001211
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001212 createInfo.sType = VK_STRUCTURE_TYPE_SHADER_CREATE_INFO;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001213 createInfo.pNext = NULL;
1214
Cody Northrop1fedb212015-05-28 11:27:16 -06001215 if (!demo->use_glsl) {
1216 createInfo.codeSize = size;
1217 createInfo.pCode = code;
1218 createInfo.flags = 0;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001219
Cody Northrop1fedb212015-05-28 11:27:16 -06001220 err = vkCreateShader(demo->device, &createInfo, &shader);
1221 if (err) {
1222 free((void *) createInfo.pCode);
1223 }
1224 } else {
1225 // Create fake SPV structure to feed GLSL
1226 // to the driver "under the covers"
1227 createInfo.codeSize = 3 * sizeof(uint32_t) + size + 1;
1228 createInfo.pCode = malloc(createInfo.codeSize);
1229 createInfo.flags = 0;
1230
1231 /* try version 0 first: VkShaderStage followed by GLSL */
1232 ((uint32_t *) createInfo.pCode)[0] = ICD_SPV_MAGIC;
1233 ((uint32_t *) createInfo.pCode)[1] = 0;
1234 ((uint32_t *) createInfo.pCode)[2] = stage;
1235 memcpy(((uint32_t *) createInfo.pCode + 3), code, size + 1);
1236
1237 err = vkCreateShader(demo->device, &createInfo, &shader);
1238 if (err) {
1239 free((void *) createInfo.pCode);
1240 return (VkShader) VK_NULL_HANDLE;
1241 }
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001242 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001243 return shader;
1244}
1245
Cody Northrop8b12a1f2015-03-17 15:55:58 -06001246char *demo_read_spv(const char *filename, size_t *psize)
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001247{
1248 long int size;
Tony Barbourf9921732015-04-22 11:36:22 -06001249 size_t U_ASSERT_ONLY retval;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001250 void *shader_code;
1251
1252 FILE *fp = fopen(filename, "rb");
1253 if (!fp) return NULL;
1254
1255 fseek(fp, 0L, SEEK_END);
1256 size = ftell(fp);
1257
1258 fseek(fp, 0L, SEEK_SET);
1259
1260 shader_code = malloc(size);
Tony Barbourfdc2d352015-04-22 09:02:32 -06001261 retval = fread(shader_code, size, 1, fp);
1262 assert(retval == 1);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001263
1264 *psize = size;
1265
1266 return shader_code;
1267}
1268
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001269static VkShader demo_prepare_vs(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001270{
Cody Northrop1fedb212015-05-28 11:27:16 -06001271 if (!demo->use_glsl) {
1272 void *vertShaderCode;
1273 size_t size;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001274
Cody Northrop1fedb212015-05-28 11:27:16 -06001275 vertShaderCode = demo_read_spv("cube-vert.spv", &size);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001276
Cody Northrop1fedb212015-05-28 11:27:16 -06001277 return demo_prepare_shader(demo, VK_SHADER_STAGE_VERTEX,
1278 vertShaderCode, size);
1279 } else {
1280 static const char *vertShaderText =
1281 "#version 140\n"
1282 "#extension GL_ARB_separate_shader_objects : enable\n"
1283 "#extension GL_ARB_shading_language_420pack : enable\n"
1284 "\n"
1285 "layout(binding = 0) uniform buf {\n"
1286 " mat4 MVP;\n"
1287 " vec4 position[12*3];\n"
1288 " vec4 attr[12*3];\n"
1289 "} ubuf;\n"
1290 "\n"
1291 "layout (location = 0) out vec4 texcoord;\n"
1292 "\n"
1293 "void main() \n"
1294 "{\n"
1295 " texcoord = ubuf.attr[gl_VertexID];\n"
1296 " gl_Position = ubuf.MVP * ubuf.position[gl_VertexID];\n"
1297 "\n"
1298 " // GL->VK conventions\n"
1299 " gl_Position.y = -gl_Position.y;\n"
1300 " gl_Position.z = (gl_Position.z + gl_Position.w) / 2.0;\n"
1301 "}\n";
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001302
Cody Northrop1fedb212015-05-28 11:27:16 -06001303 return demo_prepare_shader(demo, VK_SHADER_STAGE_VERTEX,
1304 (const void *) vertShaderText,
1305 strlen(vertShaderText));
1306 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001307}
1308
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001309static VkShader demo_prepare_fs(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001310{
Cody Northrop1fedb212015-05-28 11:27:16 -06001311 if (!demo->use_glsl) {
1312 void *fragShaderCode;
1313 size_t size;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001314
Cody Northrop1fedb212015-05-28 11:27:16 -06001315 fragShaderCode = demo_read_spv("cube-frag.spv", &size);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001316
Cody Northrop1fedb212015-05-28 11:27:16 -06001317 return demo_prepare_shader(demo, VK_SHADER_STAGE_FRAGMENT,
1318 fragShaderCode, size);
1319 } else {
1320 static const char *fragShaderText =
1321 "#version 140\n"
1322 "#extension GL_ARB_separate_shader_objects : enable\n"
1323 "#extension GL_ARB_shading_language_420pack : enable\n"
1324 "layout (binding = 1) uniform sampler2D tex;\n"
1325 "\n"
1326 "layout (location = 0) in vec4 texcoord;\n"
1327 "layout (location = 0) out vec4 uFragColor;\n"
1328 "void main() {\n"
1329 " uFragColor = texture(tex, texcoord.xy);\n"
1330 "}\n";
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001331
Cody Northrop1fedb212015-05-28 11:27:16 -06001332 return demo_prepare_shader(demo, VK_SHADER_STAGE_FRAGMENT,
1333 (const void *) fragShaderText,
1334 strlen(fragShaderText));
1335 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001336}
1337
1338static void demo_prepare_pipeline(struct demo *demo)
1339{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001340 VkGraphicsPipelineCreateInfo pipeline;
Mark Lobodzinski521d7762015-06-23 15:11:57 -06001341
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001342 VkPipelineIaStateCreateInfo ia;
1343 VkPipelineRsStateCreateInfo rs;
1344 VkPipelineCbStateCreateInfo cb;
1345 VkPipelineDsStateCreateInfo ds;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001346 VkPipelineVpStateCreateInfo vp;
1347 VkPipelineMsStateCreateInfo ms;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001348 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001349
1350 memset(&pipeline, 0, sizeof(pipeline));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001351 pipeline.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001352 pipeline.layout = demo->pipeline_layout;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001353
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001354 memset(&ia, 0, sizeof(ia));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001355 ia.sType = VK_STRUCTURE_TYPE_PIPELINE_IA_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06001356 ia.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001357
1358 memset(&rs, 0, sizeof(rs));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001359 rs.sType = VK_STRUCTURE_TYPE_PIPELINE_RS_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06001360 rs.fillMode = VK_FILL_MODE_SOLID;
1361 rs.cullMode = VK_CULL_MODE_BACK;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001362 rs.frontFace = VK_FRONT_FACE_CCW;
Chia-I Wubb67e6e2015-04-22 14:20:52 +08001363 rs.depthClipEnable = VK_TRUE;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001364
1365 memset(&cb, 0, sizeof(cb));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001366 cb.sType = VK_STRUCTURE_TYPE_PIPELINE_CB_STATE_CREATE_INFO;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001367 VkPipelineCbAttachmentState att_state[1];
Tony Barbour29645d02015-01-16 14:27:35 -07001368 memset(att_state, 0, sizeof(att_state));
1369 att_state[0].format = demo->format;
1370 att_state[0].channelWriteMask = 0xf;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001371 att_state[0].blendEnable = VK_FALSE;
Tony Barbour29645d02015-01-16 14:27:35 -07001372 cb.attachmentCount = 1;
1373 cb.pAttachments = att_state;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001374
Tony Barbour29645d02015-01-16 14:27:35 -07001375 memset(&vp, 0, sizeof(vp));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001376 vp.sType = VK_STRUCTURE_TYPE_PIPELINE_VP_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06001377 vp.viewportCount = 1;
Tony Barbour29645d02015-01-16 14:27:35 -07001378
1379 memset(&ds, 0, sizeof(ds));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001380 ds.sType = VK_STRUCTURE_TYPE_PIPELINE_DS_STATE_CREATE_INFO;
Tony Barbour29645d02015-01-16 14:27:35 -07001381 ds.format = demo->depth.format;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001382 ds.depthTestEnable = VK_TRUE;
1383 ds.depthWriteEnable = VK_TRUE;
Tony Barbour72304ef2015-04-16 15:59:00 -06001384 ds.depthCompareOp = VK_COMPARE_OP_LESS_EQUAL;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001385 ds.depthBoundsEnable = VK_FALSE;
1386 ds.back.stencilFailOp = VK_STENCIL_OP_KEEP;
1387 ds.back.stencilPassOp = VK_STENCIL_OP_KEEP;
Tony Barbour72304ef2015-04-16 15:59:00 -06001388 ds.back.stencilCompareOp = VK_COMPARE_OP_ALWAYS;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001389 ds.stencilTestEnable = VK_FALSE;
Tony Barbour29645d02015-01-16 14:27:35 -07001390 ds.front = ds.back;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001391
Tony Barbour29645d02015-01-16 14:27:35 -07001392 memset(&ms, 0, sizeof(ms));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001393 ms.sType = VK_STRUCTURE_TYPE_PIPELINE_MS_STATE_CREATE_INFO;
Tony Barbour29645d02015-01-16 14:27:35 -07001394 ms.sampleMask = 1;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001395 ms.multisampleEnable = VK_FALSE;
Tony Barbour3ca22502015-06-26 10:18:34 -06001396 ms.rasterSamples = 1;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001397
Mark Lobodzinski521d7762015-06-23 15:11:57 -06001398 // Two stages: vs and fs
1399 pipeline.stageCount = 2;
1400 VkPipelineShaderStageCreateInfo shaderStages[2];
1401 memset(&shaderStages, 0, 2 * sizeof(VkPipelineShaderStageCreateInfo));
1402
1403 shaderStages[0].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1404 shaderStages[0].stage = VK_SHADER_STAGE_VERTEX;
1405 shaderStages[0].shader = demo_prepare_vs(demo);
1406
1407 shaderStages[1].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1408 shaderStages[1].stage = VK_SHADER_STAGE_FRAGMENT;
1409 shaderStages[1].shader = demo_prepare_fs(demo);
1410
Mark Lobodzinski15169cf2015-06-30 10:18:36 -06001411 pipeline.pVertexInputState = NULL;
Mark Lobodzinski521d7762015-06-23 15:11:57 -06001412 pipeline.pIaState = &ia;
1413 pipeline.pRsState = &rs;
1414 pipeline.pCbState = &cb;
1415 pipeline.pMsState = &ms;
1416 pipeline.pVpState = &vp;
1417 pipeline.pDsState = &ds;
1418 pipeline.pStages = shaderStages;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001419
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001420 err = vkCreateGraphicsPipeline(demo->device, &pipeline, &demo->pipeline);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001421 assert(!err);
1422
Mark Lobodzinski521d7762015-06-23 15:11:57 -06001423 for (uint32_t i = 0; i < pipeline.stageCount; i++) {
1424 vkDestroyObject(demo->device, VK_OBJECT_TYPE_SHADER, shaderStages[i].shader);
1425 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001426}
1427
1428static void demo_prepare_dynamic_states(struct demo *demo)
1429{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001430 VkDynamicVpStateCreateInfo viewport_create;
1431 VkDynamicRsStateCreateInfo raster;
1432 VkDynamicCbStateCreateInfo color_blend;
1433 VkDynamicDsStateCreateInfo depth_stencil;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001434 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001435
Tony Barbour29645d02015-01-16 14:27:35 -07001436 memset(&viewport_create, 0, sizeof(viewport_create));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001437 viewport_create.sType = VK_STRUCTURE_TYPE_DYNAMIC_VP_STATE_CREATE_INFO;
Courtney Goeltzenleuchter1710d8d2015-02-11 14:13:34 -07001438 viewport_create.viewportAndScissorCount = 1;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001439 VkViewport viewport;
Piers Daniell735ee532015-02-23 16:23:13 -07001440 memset(&viewport, 0, sizeof(viewport));
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001441 viewport.height = (float) demo->height;
1442 viewport.width = (float) demo->width;
1443 viewport.minDepth = (float) 0.0f;
1444 viewport.maxDepth = (float) 1.0f;
Piers Daniell735ee532015-02-23 16:23:13 -07001445 viewport_create.pViewports = &viewport;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001446 VkRect scissor;
Piers Daniell735ee532015-02-23 16:23:13 -07001447 memset(&scissor, 0, sizeof(scissor));
Courtney Goeltzenleuchter1710d8d2015-02-11 14:13:34 -07001448 scissor.extent.width = demo->width;
1449 scissor.extent.height = demo->height;
1450 scissor.offset.x = 0;
1451 scissor.offset.y = 0;
Courtney Goeltzenleuchter1710d8d2015-02-11 14:13:34 -07001452 viewport_create.pScissors = &scissor;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001453
1454 memset(&raster, 0, sizeof(raster));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001455 raster.sType = VK_STRUCTURE_TYPE_DYNAMIC_RS_STATE_CREATE_INFO;
Piers Daniell735ee532015-02-23 16:23:13 -07001456 raster.lineWidth = 1.0;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001457
1458 memset(&color_blend, 0, sizeof(color_blend));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001459 color_blend.sType = VK_STRUCTURE_TYPE_DYNAMIC_CB_STATE_CREATE_INFO;
Piers Daniell735ee532015-02-23 16:23:13 -07001460 color_blend.blendConst[0] = 1.0f;
1461 color_blend.blendConst[1] = 1.0f;
1462 color_blend.blendConst[2] = 1.0f;
1463 color_blend.blendConst[3] = 1.0f;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001464
1465 memset(&depth_stencil, 0, sizeof(depth_stencil));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001466 depth_stencil.sType = VK_STRUCTURE_TYPE_DYNAMIC_DS_STATE_CREATE_INFO;
Mark Lobodzinski89d32b12015-06-12 11:14:17 -06001467 depth_stencil.minDepthBounds = 0.0f;
1468 depth_stencil.maxDepthBounds = 1.0f;
Tony Barbour29645d02015-01-16 14:27:35 -07001469 depth_stencil.stencilBackRef = 0;
1470 depth_stencil.stencilFrontRef = 0;
1471 depth_stencil.stencilReadMask = 0xff;
1472 depth_stencil.stencilWriteMask = 0xff;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001473
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001474 err = vkCreateDynamicViewportState(demo->device, &viewport_create, &demo->viewport);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001475 assert(!err);
1476
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001477 err = vkCreateDynamicRasterState(demo->device, &raster, &demo->raster);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001478 assert(!err);
1479
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001480 err = vkCreateDynamicColorBlendState(demo->device,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001481 &color_blend, &demo->color_blend);
1482 assert(!err);
1483
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001484 err = vkCreateDynamicDepthStencilState(demo->device,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001485 &depth_stencil, &demo->depth_stencil);
1486 assert(!err);
1487}
1488
Chia-I Wu63ea9262015-03-26 13:14:16 +08001489static void demo_prepare_descriptor_pool(struct demo *demo)
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001490{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001491 const VkDescriptorTypeCount type_counts[2] = {
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001492 [0] = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001493 .type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001494 .count = 1,
1495 },
1496 [1] = {
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -06001497 .type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001498 .count = DEMO_TEXTURE_COUNT,
1499 },
1500 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001501 const VkDescriptorPoolCreateInfo descriptor_pool = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001502 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001503 .pNext = NULL,
1504 .count = 2,
1505 .pTypeCount = type_counts,
1506 };
Tony Barbourfdc2d352015-04-22 09:02:32 -06001507 VkResult U_ASSERT_ONLY err;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001508
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001509 err = vkCreateDescriptorPool(demo->device,
1510 VK_DESCRIPTOR_POOL_USAGE_ONE_SHOT, 1,
Chia-I Wu63ea9262015-03-26 13:14:16 +08001511 &descriptor_pool, &demo->desc_pool);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001512 assert(!err);
1513}
1514
1515static void demo_prepare_descriptor_set(struct demo *demo)
1516{
Chia-I Wuae721ba2015-05-25 16:27:55 +08001517 VkDescriptorInfo tex_descs[DEMO_TEXTURE_COUNT];
1518 VkWriteDescriptorSet writes[2];
Tony Barbourfdc2d352015-04-22 09:02:32 -06001519 VkResult U_ASSERT_ONLY err;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001520 uint32_t count;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001521 uint32_t i;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001522
Mike Stroyanebae8322015-04-17 12:36:38 -06001523 err = vkAllocDescriptorSets(demo->device, demo->desc_pool,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001524 VK_DESCRIPTOR_SET_USAGE_STATIC,
Chia-I Wu87544e72015-02-23 10:41:08 -07001525 1, &demo->desc_layout,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001526 &demo->desc_set, &count);
1527 assert(!err && count == 1);
1528
Chia-I Wuae721ba2015-05-25 16:27:55 +08001529 memset(&tex_descs, 0, sizeof(tex_descs));
1530 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
1531 tex_descs[i].sampler = demo->textures[i].sampler;
1532 tex_descs[i].imageView = demo->textures[i].view;
1533 tex_descs[i].imageLayout = VK_IMAGE_LAYOUT_GENERAL;
1534 }
1535
1536 memset(&writes, 0, sizeof(writes));
1537
1538 writes[0].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
1539 writes[0].destSet = demo->desc_set;
1540 writes[0].count = 1;
1541 writes[0].descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
1542 writes[0].pDescriptors = &demo->uniform_data.desc;
1543
1544 writes[1].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
1545 writes[1].destSet = demo->desc_set;
1546 writes[1].destBinding = 1;
1547 writes[1].count = DEMO_TEXTURE_COUNT;
1548 writes[1].descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
1549 writes[1].pDescriptors = tex_descs;
1550
1551 err = vkUpdateDescriptorSets(demo->device, 2, writes, 0, NULL);
1552 assert(!err);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001553}
1554
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001555static void demo_prepare(struct demo *demo)
1556{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001557 const VkCmdBufferCreateInfo cmd = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001558 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001559 .pNext = NULL,
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07001560 .queueNodeIndex = demo->graphics_queue_node_index,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001561 .flags = 0,
1562 };
Tony Barbourfdc2d352015-04-22 09:02:32 -06001563 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001564
1565 demo_prepare_buffers(demo);
1566 demo_prepare_depth(demo);
1567 demo_prepare_textures(demo);
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001568 demo_prepare_cube_data_buffer(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001569
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001570 demo_prepare_descriptor_layout(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001571 demo_prepare_pipeline(demo);
1572 demo_prepare_dynamic_states(demo);
1573
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001574 for (int i = 0; i < DEMO_BUFFER_COUNT; i++) {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001575 err = vkCreateCommandBuffer(demo->device, &cmd, &demo->buffers[i].cmd);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001576 assert(!err);
1577 }
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001578
Chia-I Wu63ea9262015-03-26 13:14:16 +08001579 demo_prepare_descriptor_pool(demo);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001580 demo_prepare_descriptor_set(demo);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001581
1582
1583 for (int i = 0; i < DEMO_BUFFER_COUNT; i++) {
1584 demo->current_buffer = i;
1585 demo_draw_build_cmd(demo, demo->buffers[i].cmd);
1586 }
1587
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001588 /*
1589 * Prepare functions above may generate pipeline commands
1590 * that need to be flushed before beginning the render loop.
1591 */
1592 demo_flush_init_cmd(demo);
1593
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001594 demo->current_buffer = 0;
Jon Ashburn8d26c062015-04-24 09:46:24 -07001595 demo->prepared = true;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001596}
1597
David Pinedo2bb7c932015-06-18 17:03:14 -06001598static void demo_cleanup(struct demo *demo)
1599{
1600 uint32_t i;
1601
1602 demo->prepared = false;
1603
1604 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DESCRIPTOR_SET, demo->desc_set);
1605 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DESCRIPTOR_POOL, demo->desc_pool);
1606
1607 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DYNAMIC_VP_STATE, demo->viewport);
1608 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DYNAMIC_RS_STATE, demo->raster);
1609 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DYNAMIC_CB_STATE, demo->color_blend);
1610 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DYNAMIC_DS_STATE, demo->depth_stencil);
1611
1612 vkDestroyObject(demo->device, VK_OBJECT_TYPE_PIPELINE, demo->pipeline);
1613 vkDestroyObject(demo->device, VK_OBJECT_TYPE_PIPELINE_LAYOUT, demo->pipeline_layout);
1614 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DESCRIPTOR_SET_LAYOUT, demo->desc_layout);
1615
1616 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
1617 vkDestroyObject(demo->device, VK_OBJECT_TYPE_IMAGE_VIEW, demo->textures[i].view);
1618 vkDestroyObject(demo->device, VK_OBJECT_TYPE_IMAGE, demo->textures[i].image);
1619 vkFreeMemory(demo->device, demo->textures[i].mem);
1620 vkDestroyObject(demo->device, VK_OBJECT_TYPE_SAMPLER, demo->textures[i].sampler);
1621 }
1622 demo->fpDestroySwapChainWSI(demo->swap_chain);
1623
1624 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DEPTH_STENCIL_VIEW, demo->depth.view);
1625 vkDestroyObject(demo->device, VK_OBJECT_TYPE_IMAGE, demo->depth.image);
1626 vkFreeMemory(demo->device, demo->depth.mem);
1627
1628 vkDestroyObject(demo->device, VK_OBJECT_TYPE_BUFFER_VIEW, demo->uniform_data.view);
1629 vkDestroyObject(demo->device, VK_OBJECT_TYPE_BUFFER, demo->uniform_data.buf);
1630 vkFreeMemory(demo->device, demo->uniform_data.mem);
1631
1632 for (i = 0; i < DEMO_BUFFER_COUNT; i++) {
1633 vkDestroyObject(demo->device, VK_OBJECT_TYPE_COLOR_ATTACHMENT_VIEW, demo->buffers[i].view);
1634 vkDestroyObject(demo->device, VK_OBJECT_TYPE_COMMAND_BUFFER, demo->buffers[i].cmd);
1635 }
1636
1637 vkDestroyDevice(demo->device);
Tony Barbourd70b42c2015-06-30 14:14:19 -06001638 if (demo->validate) {
1639 demo->dbgDestroyMsgCallback(demo->inst, demo->msg_callback);
1640 }
David Pinedo2bb7c932015-06-18 17:03:14 -06001641 vkDestroyInstance(demo->inst);
1642
1643#ifndef _WIN32
1644 xcb_destroy_window(demo->connection, demo->window);
1645 xcb_disconnect(demo->connection);
1646#endif // _WIN32
1647}
1648
1649// On MS-Windows, make this a global, so it's available to WndProc()
1650struct demo demo;
1651
Ian Elliott639ca472015-04-16 15:23:05 -06001652#ifdef _WIN32
1653static void demo_run(struct demo *demo)
1654{
Courtney Goeltzenleuchterb7e22702015-04-27 14:56:34 -06001655 if (!demo->prepared)
1656 return;
Ian Elliott639ca472015-04-16 15:23:05 -06001657 // Wait for work to finish before updating MVP.
1658 vkDeviceWaitIdle(demo->device);
1659 demo_update_data_buffer(demo);
1660
1661 demo_draw(demo);
1662
1663 // Wait for work to finish before updating MVP.
1664 vkDeviceWaitIdle(demo->device);
Ian Elliott639ca472015-04-16 15:23:05 -06001665
David Pinedo2bb7c932015-06-18 17:03:14 -06001666 demo->curFrame++;
1667
1668 if (demo->frameCount != INT_MAX && demo->curFrame == demo->frameCount)
1669 {
1670 demo->quit=true;
1671 demo_cleanup(demo);
1672 ExitProcess(0);
1673 }
1674
1675}
Ian Elliott639ca472015-04-16 15:23:05 -06001676
1677// MS-Windows event handling function:
1678LRESULT CALLBACK WndProc(HWND hWnd,
1679 UINT uMsg,
1680 WPARAM wParam,
1681 LPARAM lParam)
1682{
Ian Elliott639ca472015-04-16 15:23:05 -06001683 switch(uMsg)
1684 {
Ian Elliott639ca472015-04-16 15:23:05 -06001685 case WM_CLOSE:
1686 PostQuitMessage(0);
Tony Barbour3dddd5d2015-04-29 16:19:20 -06001687 break;
Ian Elliott639ca472015-04-16 15:23:05 -06001688 case WM_PAINT:
1689 demo_run(&demo);
1690 return 0;
1691 default:
1692 break;
1693 }
1694 return (DefWindowProc(hWnd, uMsg, wParam, lParam));
1695}
1696
1697static void demo_create_window(struct demo *demo)
1698{
1699 WNDCLASSEX win_class;
1700
1701 // Initialize the window class structure:
1702 win_class.cbSize = sizeof(WNDCLASSEX);
1703 win_class.style = CS_HREDRAW | CS_VREDRAW;
1704 win_class.lpfnWndProc = WndProc;
1705 win_class.cbClsExtra = 0;
1706 win_class.cbWndExtra = 0;
1707 win_class.hInstance = demo->connection; // hInstance
1708 win_class.hIcon = LoadIcon(NULL, IDI_APPLICATION);
1709 win_class.hCursor = LoadCursor(NULL, IDC_ARROW);
1710 win_class.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
1711 win_class.lpszMenuName = NULL;
1712 win_class.lpszClassName = demo->name;
1713 win_class.hIconSm = LoadIcon(NULL, IDI_WINLOGO);
1714 // Register window class:
1715 if (!RegisterClassEx(&win_class)) {
1716 // It didn't work, so try to give a useful error:
1717 printf("Unexpected error trying to start the application!\n");
1718 fflush(stdout);
1719 exit(1);
1720 }
1721 // Create window with the registered class:
Mike Stroyanb46779e2015-06-15 14:20:13 -06001722 RECT wr = { 0, 0, demo->width, demo->height };
1723 AdjustWindowRect(&wr, WS_OVERLAPPEDWINDOW, FALSE);
Ian Elliott639ca472015-04-16 15:23:05 -06001724 demo->window = CreateWindowEx(0,
1725 demo->name, // class name
1726 demo->name, // app name
1727 WS_OVERLAPPEDWINDOW | // window style
1728 WS_VISIBLE |
1729 WS_SYSMENU,
1730 100,100, // x/y coords
Mike Stroyanb46779e2015-06-15 14:20:13 -06001731 wr.right-wr.left, // width
1732 wr.bottom-wr.top, // height
Ian Elliott639ca472015-04-16 15:23:05 -06001733 NULL, // handle to parent
1734 NULL, // handle to menu
1735 demo->connection, // hInstance
1736 NULL); // no extra parameters
1737 if (!demo->window) {
1738 // It didn't work, so try to give a useful error:
1739 printf("Cannot create a window in which to draw!\n");
1740 fflush(stdout);
1741 exit(1);
1742 }
1743}
1744#else // _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001745static void demo_handle_event(struct demo *demo,
1746 const xcb_generic_event_t *event)
1747{
Piers Daniell735ee532015-02-23 16:23:13 -07001748 uint8_t event_code = event->response_type & 0x7f;
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07001749 switch (event_code) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001750 case XCB_EXPOSE:
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07001751 // TODO: Resize window
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001752 break;
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07001753 case XCB_CLIENT_MESSAGE:
1754 if((*(xcb_client_message_event_t*)event).data.data32[0] ==
1755 (*demo->atom_wm_delete_window).atom) {
1756 demo->quit = true;
1757 }
1758 break;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001759 case XCB_KEY_RELEASE:
1760 {
1761 const xcb_key_release_event_t *key =
1762 (const xcb_key_release_event_t *) event;
1763
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001764 switch (key->detail) {
1765 case 0x9: // Escape
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001766 demo->quit = true;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001767 break;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001768 case 0x71: // left arrow key
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001769 demo->spin_angle += demo->spin_increment;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001770 break;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001771 case 0x72: // right arrow key
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001772 demo->spin_angle -= demo->spin_increment;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001773 break;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001774 case 0x41:
1775 demo->pause = !demo->pause;
Piers Daniell735ee532015-02-23 16:23:13 -07001776 break;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001777 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001778 }
1779 break;
1780 default:
1781 break;
1782 }
1783}
1784
1785static void demo_run(struct demo *demo)
1786{
1787 xcb_flush(demo->connection);
1788
1789 while (!demo->quit) {
1790 xcb_generic_event_t *event;
1791
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07001792 if (demo->pause) {
1793 event = xcb_wait_for_event(demo->connection);
1794 } else {
1795 event = xcb_poll_for_event(demo->connection);
1796 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001797 if (event) {
1798 demo_handle_event(demo, event);
1799 free(event);
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07001800 }
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07001801
1802 // Wait for work to finish before updating MVP.
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001803 vkDeviceWaitIdle(demo->device);
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07001804 demo_update_data_buffer(demo);
1805
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07001806 demo_draw(demo);
Courtney Goeltzenleuchter1454f3c2014-11-18 11:28:09 -07001807
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07001808 // Wait for work to finish before updating MVP.
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001809 vkDeviceWaitIdle(demo->device);
David Pinedo2bb7c932015-06-18 17:03:14 -06001810 demo->curFrame++;
1811 if (demo->frameCount != INT_MAX && demo->curFrame == demo->frameCount)
1812 demo->quit = true;
1813
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001814 }
1815}
1816
1817static void demo_create_window(struct demo *demo)
1818{
1819 uint32_t value_mask, value_list[32];
1820
1821 demo->window = xcb_generate_id(demo->connection);
1822
1823 value_mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK;
1824 value_list[0] = demo->screen->black_pixel;
1825 value_list[1] = XCB_EVENT_MASK_KEY_RELEASE |
1826 XCB_EVENT_MASK_EXPOSURE;
1827
1828 xcb_create_window(demo->connection,
1829 XCB_COPY_FROM_PARENT,
1830 demo->window, demo->screen->root,
1831 0, 0, demo->width, demo->height, 0,
1832 XCB_WINDOW_CLASS_INPUT_OUTPUT,
1833 demo->screen->root_visual,
1834 value_mask, value_list);
1835
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07001836 /* Magic code that will send notification when window is destroyed */
1837 xcb_intern_atom_cookie_t cookie = xcb_intern_atom(demo->connection, 1, 12,
1838 "WM_PROTOCOLS");
1839 xcb_intern_atom_reply_t* reply = xcb_intern_atom_reply(demo->connection, cookie, 0);
1840
1841 xcb_intern_atom_cookie_t cookie2 = xcb_intern_atom(demo->connection, 0, 16, "WM_DELETE_WINDOW");
1842 demo->atom_wm_delete_window = xcb_intern_atom_reply(demo->connection, cookie2, 0);
1843
1844 xcb_change_property(demo->connection, XCB_PROP_MODE_REPLACE,
1845 demo->window, (*reply).atom, 4, 32, 1,
1846 &(*demo->atom_wm_delete_window).atom);
1847 free(reply);
1848
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001849 xcb_map_window(demo->connection, demo->window);
David Pinedo2bb7c932015-06-18 17:03:14 -06001850
1851 // Force the x/y coordinates to 100,100 results are identical in consecutive runs
1852 const uint32_t coords[] = {100, 100};
1853 xcb_configure_window(demo->connection, demo->window,
1854 XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y, coords);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001855}
Ian Elliott639ca472015-04-16 15:23:05 -06001856#endif // _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001857
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001858static void demo_init_vk(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001859{
Tobin Ehlis99239dc2015-04-16 18:04:57 -06001860 VkResult err;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06001861 VkExtensionProperties *instance_extensions;
1862 uint32_t instance_extension_count = 0;
1863 VkExtensionProperties *device_extensions;
1864 uint32_t device_extension_count = 0;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06001865 uint32_t total_extension_count = 0;
Tony Barbourecf1b2b2015-06-24 16:06:58 -06001866 err = vkGetGlobalExtensionCount(&total_extension_count);
Tobin Ehlis99239dc2015-04-16 18:04:57 -06001867 assert(!err);
1868
1869 VkExtensionProperties extProp;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06001870 bool32_t WSIextFound = 0;
1871 instance_extensions = malloc(sizeof(VkExtensionProperties) * total_extension_count);
1872 device_extensions = malloc(sizeof(VkExtensionProperties) * total_extension_count);
1873 for (uint32_t i = 0; i < total_extension_count; i++) {
Tony Barbourecf1b2b2015-06-24 16:06:58 -06001874 err = vkGetGlobalExtensionProperties(i, &extProp);
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06001875 if (!strcmp("VK_WSI_LunarG", extProp.name)) {
1876 WSIextFound = 1;
1877 memcpy(&instance_extensions[instance_extension_count++], &extProp, sizeof(VkExtensionProperties));
1878 memcpy(&device_extensions[device_extension_count++], &extProp, sizeof(VkExtensionProperties));
1879 }
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06001880 if (!strcmp(DEBUG_REPORT_EXTENSION_NAME, extProp.name)) {
1881 memcpy(&instance_extensions[instance_extension_count++], &extProp, sizeof(VkExtensionProperties));
1882 }
Tony Barbourd70b42c2015-06-30 14:14:19 -06001883 if (demo->validate && !strcmp("Validation",extProp.name)) {
1884 memcpy(&instance_extensions[instance_extension_count++], &extProp, sizeof(VkExtensionProperties));
1885 memcpy(&device_extensions[device_extension_count++], &extProp, sizeof(VkExtensionProperties));
1886 }
Tobin Ehlis99239dc2015-04-16 18:04:57 -06001887 }
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06001888 if (!WSIextFound) {
Tony Barbourecf1b2b2015-06-24 16:06:58 -06001889 ERR_EXIT("vkGetGlobalExtensionProperties failed to find the "
Ian Elliott65152912015-04-28 13:22:33 -06001890 "\"VK_WSI_LunarG\" extension.\n\nDo you have a compatible "
1891 "Vulkan installable client driver (ICD) installed?\nPlease "
1892 "look at the Getting Started guide for additional "
1893 "information.\n",
1894 "vkCreateInstance Failure");
1895 }
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001896 const VkApplicationInfo app = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001897 .sType = VK_STRUCTURE_TYPE_APPLICATION_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001898 .pNext = NULL,
Ian Elliott44e33f72015-04-28 10:52:52 -06001899 .pAppName = APP_SHORT_NAME,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001900 .appVersion = 0,
Ian Elliott44e33f72015-04-28 10:52:52 -06001901 .pEngineName = APP_SHORT_NAME,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001902 .engineVersion = 0,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001903 .apiVersion = VK_API_VERSION,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001904 };
Tony Barbour3dddd5d2015-04-29 16:19:20 -06001905 VkInstanceCreateInfo inst_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001906 .sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
Jon Ashburnab46b362015-04-04 14:52:07 -06001907 .pNext = NULL,
1908 .pAppInfo = &app,
1909 .pAllocCb = NULL,
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06001910 .extensionCount = instance_extension_count,
1911 .pEnabledExtensions = instance_extensions,
Jon Ashburnab46b362015-04-04 14:52:07 -06001912 };
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -06001913 const VkDeviceQueueCreateInfo queue = {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001914 .queueNodeIndex = 0,
1915 .queueCount = 1,
1916 };
Ian Elliott097d9f32015-04-28 11:35:02 -06001917
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06001918 VkDeviceCreateInfo device = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001919 .sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001920 .pNext = NULL,
1921 .queueRecordCount = 1,
1922 .pRequestedQueues = &queue,
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06001923 .extensionCount = device_extension_count,
1924 .pEnabledExtensions = device_extensions,
Jon Ashburn7842d522015-05-18 09:06:15 -06001925 .flags = 0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001926 };
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001927 uint32_t gpu_count;
1928 uint32_t i;
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07001929 uint32_t queue_count;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001930
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001931 err = vkCreateInstance(&inst_info, &demo->inst);
Ian Elliott07264132015-04-28 11:35:02 -06001932 if (err == VK_ERROR_INCOMPATIBLE_DRIVER) {
1933 ERR_EXIT("Cannot find a compatible Vulkan installable client driver "
Ian Elliott65152912015-04-28 13:22:33 -06001934 "(ICD).\n\nPlease look at the Getting Started guide for "
Ian Elliott07264132015-04-28 11:35:02 -06001935 "additional information.\n",
1936 "vkCreateInstance Failure");
Tony Barbour3dddd5d2015-04-29 16:19:20 -06001937 } else if (err == VK_ERROR_INVALID_EXTENSION) {
1938 ERR_EXIT("Cannot find a specified extension library"
1939 ".\nMake sure your layers path is set appropriately\n",
1940 "vkCreateInstance Failure");
Ian Elliott07264132015-04-28 11:35:02 -06001941 } else if (err) {
Ian Elliott65152912015-04-28 13:22:33 -06001942 ERR_EXIT("vkCreateInstance failed.\n\nDo you have a compatible Vulkan "
1943 "installable client driver (ICD) installed?\nPlease look at "
Ian Elliott07264132015-04-28 11:35:02 -06001944 "the Getting Started guide for additional information.\n",
1945 "vkCreateInstance Failure");
Ian Elliott3979e282015-04-03 15:24:55 -06001946 }
Jon Ashburnab46b362015-04-04 14:52:07 -06001947
Tony Barbour3dddd5d2015-04-29 16:19:20 -06001948
Jon Ashburn07c0c0c2015-04-15 11:31:12 -06001949 gpu_count = 1;
1950 err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, &demo->gpu);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001951 assert(!err && gpu_count == 1);
1952
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06001953
Tony Barbour3dddd5d2015-04-29 16:19:20 -06001954 if (demo->validate) {
Tony Barbourd70b42c2015-06-30 14:14:19 -06001955 demo->dbgCreateMsgCallback = vkGetInstanceProcAddr(demo->inst, "vkDbgCreateMsgCallback");
1956 demo->dbgDestroyMsgCallback = vkGetInstanceProcAddr(demo->inst, "vkDbgDestroyMsgCallback");
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06001957 if (!demo->dbgCreateMsgCallback) {
1958 ERR_EXIT("GetProcAddr: Unable to find vkDbgCreateMsgCallback\n",
1959 "vkGetProcAddr Failure");
1960 }
Tony Barbourd70b42c2015-06-30 14:14:19 -06001961 if (!demo->dbgDestroyMsgCallback) {
1962 ERR_EXIT("GetProcAddr: Unable to find vkDbgDestroyMsgCallback\n",
1963 "vkGetProcAddr Failure");
1964 }
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06001965 err = demo->dbgCreateMsgCallback(
1966 demo->inst,
1967 VK_DBG_REPORT_ERROR_BIT | VK_DBG_REPORT_WARN_BIT,
1968 dbgFunc, NULL,
1969 &demo->msg_callback);
1970 switch (err) {
1971 case VK_SUCCESS:
1972 break;
1973 case VK_ERROR_INVALID_POINTER:
1974 ERR_EXIT("dbgCreateMsgCallback: Invalid pointer\n",
1975 "dbgCreateMsgCallback Failure");
1976 break;
1977 case VK_ERROR_OUT_OF_HOST_MEMORY:
1978 ERR_EXIT("dbgCreateMsgCallback: out of host memory\n",
1979 "dbgCreateMsgCallback Failure");
1980 break;
1981 default:
1982 ERR_EXIT("dbgCreateMsgCallback: unknown failure\n",
1983 "dbgCreateMsgCallback Failure");
1984 break;
1985 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06001986 }
1987
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001988 err = vkCreateDevice(demo->gpu, &device, &demo->device);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001989 assert(!err);
1990
Ian Elliott673898b2015-06-22 15:07:49 -06001991 GET_DEVICE_PROC_ADDR(demo->device, CreateSwapChainWSI);
1992 GET_DEVICE_PROC_ADDR(demo->device, CreateSwapChainWSI);
1993 GET_DEVICE_PROC_ADDR(demo->device, DestroySwapChainWSI);
1994 GET_DEVICE_PROC_ADDR(demo->device, GetSwapChainInfoWSI);
1995 GET_DEVICE_PROC_ADDR(demo->device, QueuePresentWSI);
Jon Ashburn0b85d052015-05-21 18:13:33 -06001996
Tony Barbourecf1b2b2015-06-24 16:06:58 -06001997 err = vkGetPhysicalDeviceProperties(demo->gpu, &demo->gpu_props);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001998 assert(!err);
1999
Tony Barbourecf1b2b2015-06-24 16:06:58 -06002000 err = vkGetPhysicalDeviceQueueCount(demo->gpu, &queue_count);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06002001 assert(!err);
2002
Tony Barbourecf1b2b2015-06-24 16:06:58 -06002003 demo->queue_props = (VkPhysicalDeviceQueueProperties *) malloc(queue_count * sizeof(VkPhysicalDeviceQueueProperties));
2004 err = vkGetPhysicalDeviceQueueProperties(demo->gpu, queue_count, demo->queue_props);
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07002005 assert(!err);
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07002006 assert(queue_count >= 1);
2007
Mark Lobodzinskida9b1092015-04-16 11:44:05 -05002008 // Graphics queue and MemMgr queue can be separate.
2009 // TODO: Add support for separate queues, including synchronization,
Mark Lobodzinski6baaec72015-05-11 17:21:15 -05002010 // and appropriate tracking for QueueSubmit
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07002011 for (i = 0; i < queue_count; i++) {
Mark Lobodzinski6baaec72015-05-11 17:21:15 -05002012 if (demo->queue_props[i].queueFlags & VK_QUEUE_GRAPHICS_BIT)
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07002013 break;
2014 }
2015 assert(i < queue_count);
2016 demo->graphics_queue_node_index = i;
2017
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002018 err = vkGetDeviceQueue(demo->device, demo->graphics_queue_node_index,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002019 0, &demo->queue);
2020 assert(!err);
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002021
Jon Ashburnd7130cb2015-06-16 12:44:51 -06002022 // for now hardcode format till get WSI support
2023 demo->format = VK_FORMAT_B8G8R8A8_UNORM;
Ian Elliotte4602cd2015-04-21 16:41:02 -06002024
David Pinedo2bb7c932015-06-18 17:03:14 -06002025 demo->quit = false;
2026 demo->curFrame = 0;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002027}
2028
2029static void demo_init_connection(struct demo *demo)
2030{
Ian Elliott639ca472015-04-16 15:23:05 -06002031#ifndef _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002032 const xcb_setup_t *setup;
2033 xcb_screen_iterator_t iter;
2034 int scr;
2035
2036 demo->connection = xcb_connect(NULL, &scr);
Ian Elliott3979e282015-04-03 15:24:55 -06002037 if (demo->connection == NULL) {
2038 printf("Cannot find a compatible Vulkan installable client driver "
2039 "(ICD).\nExiting ...\n");
2040 fflush(stdout);
2041 exit(1);
2042 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002043
2044 setup = xcb_get_setup(demo->connection);
2045 iter = xcb_setup_roots_iterator(setup);
2046 while (scr-- > 0)
2047 xcb_screen_next(&iter);
2048
2049 demo->screen = iter.data;
Ian Elliott639ca472015-04-16 15:23:05 -06002050#endif // _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002051}
2052
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002053static void demo_init(struct demo *demo, int argc, char **argv)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002054{
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002055 vec3 eye = {0.0f, 3.0f, 5.0f};
2056 vec3 origin = {0, 0, 0};
Chia-I Wuae3b55d2015-04-22 14:56:17 +08002057 vec3 up = {0.0f, 1.0f, 0.0};
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002058
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002059 memset(demo, 0, sizeof(*demo));
David Pinedo2bb7c932015-06-18 17:03:14 -06002060 demo->frameCount = INT_MAX;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002061
Piers Daniell735ee532015-02-23 16:23:13 -07002062 for (int i = 1; i < argc; i++) {
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002063 if (strcmp(argv[i], "--use_staging") == 0) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002064 demo->use_staging_buffer = true;
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002065 continue;
Ian Elliott639ca472015-04-16 15:23:05 -06002066 }
Cody Northrop1fedb212015-05-28 11:27:16 -06002067 if (strcmp(argv[i], "--use_glsl") == 0) {
2068 demo->use_glsl = true;
2069 continue;
2070 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002071 if (strcmp(argv[i], "--validate") == 0) {
2072 demo->validate = true;
2073 continue;
2074 }
David Pinedo2bb7c932015-06-18 17:03:14 -06002075 if (strcmp(argv[i], "--c") == 0 &&
2076 demo->frameCount == INT_MAX &&
2077 i < argc-1 &&
2078 sscanf(argv[i+1],"%d", &demo->frameCount) == 1 &&
2079 demo->frameCount >= 0)
2080 {
2081 i++;
2082 continue;
2083 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002084
David Pinedo2bb7c932015-06-18 17:03:14 -06002085 fprintf(stderr, "Usage:\n %s [--use_staging] [--validate] [--c <framecount>]\n", APP_SHORT_NAME);
Ian Elliott639ca472015-04-16 15:23:05 -06002086 fflush(stderr);
2087 exit(1);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002088 }
2089
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002090 demo_init_connection(demo);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002091 demo_init_vk(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002092
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002093 demo->width = 500;
2094 demo->height = 500;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002095
2096 demo->spin_angle = 0.01f;
2097 demo->spin_increment = 0.01f;
2098 demo->pause = false;
2099
Piers Daniell735ee532015-02-23 16:23:13 -07002100 mat4x4_perspective(demo->projection_matrix, (float)degreesToRadians(45.0f), 1.0f, 0.1f, 100.0f);
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002101 mat4x4_look_at(demo->view_matrix, eye, origin, up);
2102 mat4x4_identity(demo->model_matrix);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002103}
2104
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002105
Ian Elliott639ca472015-04-16 15:23:05 -06002106#ifdef _WIN32
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002107extern int __getmainargs(
2108 int * _Argc,
2109 char *** _Argv,
2110 char *** _Env,
2111 int _DoWildCard,
2112 int * new_mode);
Ian Elliottf9cf78c2015-04-28 10:33:11 -06002113
Ian Elliotta748eaf2015-04-28 15:50:36 -06002114int WINAPI WinMain(HINSTANCE hInstance,
2115 HINSTANCE hPrevInstance,
2116 LPSTR pCmdLine,
2117 int nCmdShow)
Ian Elliott639ca472015-04-16 15:23:05 -06002118{
2119 MSG msg; // message
2120 bool done; // flag saying when app is complete
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002121 int argc;
2122 char** argv;
2123 char** env;
2124 int new_mode = 0;
Ian Elliott639ca472015-04-16 15:23:05 -06002125
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002126 __getmainargs(&argc,&argv,&env,0,&new_mode);
2127
2128 demo_init(&demo, argc, argv);
2129 demo.connection = hInstance;
2130 strncpy(demo.name, "cube", APP_NAME_STR_LEN);
Ian Elliott639ca472015-04-16 15:23:05 -06002131 demo_create_window(&demo);
2132
2133 demo_prepare(&demo);
2134
2135 done = false; //initialize loop condition variable
2136 /* main message loop*/
2137 while(!done)
2138 {
Ian Elliotta748eaf2015-04-28 15:50:36 -06002139 PeekMessage(&msg, NULL, 0, 0, PM_REMOVE);
Ian Elliott639ca472015-04-16 15:23:05 -06002140 if (msg.message == WM_QUIT) //check for a quit message
2141 {
2142 done = true; //if found, quit app
2143 }
2144 else
2145 {
2146 /* Translate and dispatch to event queue*/
2147 TranslateMessage(&msg);
2148 DispatchMessage(&msg);
2149 }
2150 }
2151
2152 demo_cleanup(&demo);
2153
Tony Barbourf9921732015-04-22 11:36:22 -06002154 return (int) msg.wParam;
Ian Elliott639ca472015-04-16 15:23:05 -06002155}
2156#else // _WIN32
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002157int main(int argc, char **argv)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002158{
2159 struct demo demo;
2160
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002161 demo_init(&demo, argc, argv);
Chia-I Wucbb564e2015-04-16 22:02:10 +08002162 demo_create_window(&demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002163
2164 demo_prepare(&demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002165 demo_run(&demo);
2166
2167 demo_cleanup(&demo);
2168
2169 return 0;
2170}
Ian Elliott639ca472015-04-16 15:23:05 -06002171#endif // _WIN32