blob: a346400849e0c76ece677981aaaaf3c420342ae5 [file] [log] [blame]
Ian Elliotta748eaf2015-04-28 15:50:36 -06001/*
2 * Vulkan
3 *
4 * Copyright (C) 2014-2015 LunarG, Inc.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 */
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -060024#define _GNU_SOURCE
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060025#include <stdio.h>
26#include <stdlib.h>
27#include <string.h>
28#include <stdbool.h>
29#include <assert.h>
30
Ian Elliott639ca472015-04-16 15:23:05 -060031#ifdef _WIN32
32#pragma comment(linker, "/subsystem:windows")
33#include <windows.h>
34#define APP_NAME_STR_LEN 80
35#else // _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060036#include <xcb/xcb.h>
Ian Elliott639ca472015-04-16 15:23:05 -060037#endif // _WIN32
38
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -060039#include <vulkan.h>
Chia-I Wucbb564e2015-04-16 22:02:10 +080040#include <vk_wsi_lunarg.h>
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -060041#include "vk_debug_report_lunarg.h"
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060042
Cody Northropfe3d8bc2015-03-17 14:54:35 -060043#include "icd-spv.h"
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060044
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -060045#include "linmath.h"
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -060046#include <png.h>
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -060047
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060048#define DEMO_BUFFER_COUNT 2
49#define DEMO_TEXTURE_COUNT 1
Ian Elliott44e33f72015-04-28 10:52:52 -060050#define APP_SHORT_NAME "cube"
51#define APP_LONG_NAME "The Vulkan Cube Demo Program"
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060052
Tony Barbourfdc2d352015-04-22 09:02:32 -060053#if defined(NDEBUG) && defined(__GNUC__)
54#define U_ASSERT_ONLY __attribute__((unused))
55#else
56#define U_ASSERT_ONLY
57#endif
58
Ian Elliott07264132015-04-28 11:35:02 -060059#ifdef _WIN32
60#define ERR_EXIT(err_msg, err_class) \
61 do { \
62 MessageBox(NULL, err_msg, err_class, MB_OK); \
63 exit(1); \
64 } while (0)
65
66// NOTE: If the following values (copied from "loader_platform.h") change, they
67// need to change here as well:
68#define LAYER_NAMES_ENV "VK_LAYER_NAMES"
69#define LAYER_NAMES_REGISTRY_VALUE "VK_LAYER_NAMES"
70
71#else // _WIN32
72
73#define ERR_EXIT(err_msg, err_class) \
74 do { \
75 printf(err_msg); \
76 fflush(stdout); \
77 exit(1); \
78 } while (0)
79#endif // _WIN32
80
Ian Elliott673898b2015-06-22 15:07:49 -060081#define GET_DEVICE_PROC_ADDR(dev, entrypoint) \
82{ \
83 demo->fp##entrypoint = vkGetDeviceProcAddr(dev, "vk"#entrypoint); \
84 if (demo->fp##entrypoint == NULL) { \
85 ERR_EXIT("vkGetDeviceProcAddr failed to find vk"#entrypoint, \
86 "vkGetDeviceProcAddr Failure"); \
87 } \
88}
89
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -060090/*
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -070091 * structure to track all objects related to a texture.
92 */
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -060093struct texture_object {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -060094 VkSampler sampler;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -070095
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -060096 VkImage image;
97 VkImageLayout imageLayout;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -060098
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -050099 VkDeviceMemory mem;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600100 VkImageView view;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700101 int32_t tex_width, tex_height;
102};
103
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600104static char *tex_files[] = {
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -0600105 "lunarg-logo-256x256-solid.png"
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600106};
107
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600108struct vkcube_vs_uniform {
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600109 // Must start with MVP
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600110 float mvp[4][4];
111 float position[12*3][4];
112 float color[12*3][4];
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600113};
114
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600115struct vktexcube_vs_uniform {
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600116 // Must start with MVP
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600117 float mvp[4][4];
118 float position[12*3][4];
119 float attr[12*3][4];
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600120};
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600121
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600122//--------------------------------------------------------------------------------------
123// Mesh and VertexFormat Data
124//--------------------------------------------------------------------------------------
125struct Vertex
126{
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600127 float posX, posY, posZ, posW; // Position data
128 float r, g, b, a; // Color
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600129};
130
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600131struct VertexPosTex
132{
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600133 float posX, posY, posZ, posW; // Position data
134 float u, v, s, t; // Texcoord
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600135};
136
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600137#define XYZ1(_x_, _y_, _z_) (_x_), (_y_), (_z_), 1.f
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600138#define UV(_u_, _v_) (_u_), (_v_), 0.f, 1.f
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600139
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600140static const float g_vertex_buffer_data[] = {
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800141 -1.0f,-1.0f,-1.0f, // -X side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600142 -1.0f,-1.0f, 1.0f,
143 -1.0f, 1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800144 -1.0f, 1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600145 -1.0f, 1.0f,-1.0f,
146 -1.0f,-1.0f,-1.0f,
147
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800148 -1.0f,-1.0f,-1.0f, // -Z side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600149 1.0f, 1.0f,-1.0f,
150 1.0f,-1.0f,-1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800151 -1.0f,-1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600152 -1.0f, 1.0f,-1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600153 1.0f, 1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600154
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800155 -1.0f,-1.0f,-1.0f, // -Y side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600156 1.0f,-1.0f,-1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600157 1.0f,-1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800158 -1.0f,-1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600159 1.0f,-1.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600160 -1.0f,-1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600161
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800162 -1.0f, 1.0f,-1.0f, // +Y side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600163 -1.0f, 1.0f, 1.0f,
164 1.0f, 1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800165 -1.0f, 1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600166 1.0f, 1.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600167 1.0f, 1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600168
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800169 1.0f, 1.0f,-1.0f, // +X side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600170 1.0f, 1.0f, 1.0f,
171 1.0f,-1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800172 1.0f,-1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600173 1.0f,-1.0f,-1.0f,
174 1.0f, 1.0f,-1.0f,
175
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800176 -1.0f, 1.0f, 1.0f, // +Z side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600177 -1.0f,-1.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600178 1.0f, 1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800179 -1.0f,-1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600180 1.0f,-1.0f, 1.0f,
181 1.0f, 1.0f, 1.0f,
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600182};
183
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600184static const float g_uv_buffer_data[] = {
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800185 0.0f, 0.0f, // -X side
186 1.0f, 0.0f,
187 1.0f, 1.0f,
188 1.0f, 1.0f,
189 0.0f, 1.0f,
190 0.0f, 0.0f,
191
192 1.0f, 0.0f, // -Z side
193 0.0f, 1.0f,
194 0.0f, 0.0f,
195 1.0f, 0.0f,
196 1.0f, 1.0f,
197 0.0f, 1.0f,
198
199 1.0f, 1.0f, // -Y side
200 1.0f, 0.0f,
201 0.0f, 0.0f,
202 1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600203 0.0f, 0.0f,
204 0.0f, 1.0f,
205
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800206 1.0f, 1.0f, // +Y side
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600207 0.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800208 0.0f, 0.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600209 1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600210 0.0f, 0.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600211 1.0f, 0.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600212
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800213 1.0f, 1.0f, // +X side
214 0.0f, 1.0f,
215 0.0f, 0.0f,
216 0.0f, 0.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600217 1.0f, 0.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600218 1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600219
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800220 0.0f, 1.0f, // +Z side
221 0.0f, 0.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600222 1.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600223 0.0f, 0.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800224 1.0f, 0.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600225 1.0f, 1.0f,
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600226};
227
228void dumpMatrix(const char *note, mat4x4 MVP)
229{
230 int i;
231
232 printf("%s: \n", note);
233 for (i=0; i<4; i++) {
234 printf("%f, %f, %f, %f\n", MVP[i][0], MVP[i][1], MVP[i][2], MVP[i][3]);
235 }
236 printf("\n");
237 fflush(stdout);
238}
239
240void dumpVec4(const char *note, vec4 vector)
241{
242 printf("%s: \n", note);
243 printf("%f, %f, %f, %f\n", vector[0], vector[1], vector[2], vector[3]);
244 printf("\n");
245 fflush(stdout);
246}
247
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -0600248void dbgFunc(
249 VkFlags msgFlags,
250 VkObjectType objType,
251 VkObject srcObject,
252 size_t location,
253 int32_t msgCode,
254 const char* pLayerPrefix,
255 const char* pMsg,
256 void* pUserData)
Tony Barbour3dddd5d2015-04-29 16:19:20 -0600257{
258 char *message = (char *) malloc(strlen(pMsg)+100);
259
260 assert (message);
261
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -0600262 if (msgFlags & VK_DBG_REPORT_ERROR_BIT) {
263 sprintf(message,"ERROR: [%s] Code %d : %s", pLayerPrefix, msgCode, pMsg);
264 } else if (msgFlags & VK_DBG_REPORT_WARN_BIT) {
Tony Barbourd70b42c2015-06-30 14:14:19 -0600265 // We know that we're submitting queues without fences, ignore this warning
266 if (strstr(pMsg, "vkQueueSubmit parameter, VkFence fence, is null pointer")){
267 return;
268 }
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -0600269 sprintf(message,"WARNING: [%s] Code %d : %s", pLayerPrefix, msgCode, pMsg);
Tony Barbour3dddd5d2015-04-29 16:19:20 -0600270 } else {
271 return;
272 }
273
274#ifdef _WIN32
275 MessageBox(NULL, message, "Alert", MB_OK);
276#else
277 printf("%s\n",message);
278 fflush(stdout);
279#endif
280 free(message);
281}
282
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600283struct demo {
Ian Elliott639ca472015-04-16 15:23:05 -0600284#ifdef _WIN32
285#define APP_NAME_STR_LEN 80
286 HINSTANCE connection; // hInstance - Windows Instance
287 char name[APP_NAME_STR_LEN]; // Name to put on the window/icon
288 HWND window; // hWnd - window handle
289#else // _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600290 xcb_connection_t *connection;
291 xcb_screen_t *screen;
Chia-I Wucbb564e2015-04-16 22:02:10 +0800292 xcb_window_t window;
293 xcb_intern_atom_reply_t *atom_wm_delete_window;
Ian Elliott639ca472015-04-16 15:23:05 -0600294#endif // _WIN32
Cody Northrop1fedb212015-05-28 11:27:16 -0600295 bool prepared;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700296 bool use_staging_buffer;
Cody Northrop1fedb212015-05-28 11:27:16 -0600297 bool use_glsl;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600298
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600299 VkInstance inst;
Tony Barbour72304ef2015-04-16 15:59:00 -0600300 VkPhysicalDevice gpu;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600301 VkDevice device;
302 VkQueue queue;
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -0700303 uint32_t graphics_queue_node_index;
Tony Barbourecf1b2b2015-06-24 16:06:58 -0600304 VkPhysicalDeviceProperties gpu_props;
Tony Barbour72304ef2015-04-16 15:59:00 -0600305 VkPhysicalDeviceQueueProperties *queue_props;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600306 VkPhysicalDeviceMemoryProperties memory_properties;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600307
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600308 VkFramebuffer framebuffer;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600309 int width, height;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600310 VkFormat format;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600311
Jon Ashburn0b85d052015-05-21 18:13:33 -0600312 PFN_vkCreateSwapChainWSI fpCreateSwapChainWSI;
313 PFN_vkDestroySwapChainWSI fpDestroySwapChainWSI;
314 PFN_vkGetSwapChainInfoWSI fpGetSwapChainInfoWSI;
315 PFN_vkQueuePresentWSI fpQueuePresentWSI;
Chia-I Wucbb564e2015-04-16 22:02:10 +0800316 VkSwapChainWSI swap_chain;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600317 struct {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600318 VkImage image;
Tony Barbour72304ef2015-04-16 15:59:00 -0600319 VkDeviceMemory mem;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600320 VkCmdBuffer cmd;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600321
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600322 VkColorAttachmentView view;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600323 } buffers[DEMO_BUFFER_COUNT];
324
325 struct {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600326 VkFormat format;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600327
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600328 VkImage image;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500329 VkDeviceMemory mem;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600330 VkDepthStencilView view;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600331 } depth;
332
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600333 struct texture_object textures[DEMO_TEXTURE_COUNT];
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600334
335 struct {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600336 VkBuffer buf;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500337 VkDeviceMemory mem;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600338 VkBufferView view;
Chia-I Wuae721ba2015-05-25 16:27:55 +0800339 VkDescriptorInfo desc;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600340 } uniform_data;
341
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600342 VkCmdBuffer cmd; // Buffer for initialization commands
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -0500343 VkPipelineLayout pipeline_layout;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600344 VkDescriptorSetLayout desc_layout;
345 VkPipeline pipeline;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600346
Courtney Goeltzenleuchter5e6d1e92015-04-10 16:24:50 -0600347 VkDynamicVpState viewport;
348 VkDynamicRsState raster;
349 VkDynamicCbState color_blend;
350 VkDynamicDsState depth_stencil;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600351
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600352 mat4x4 projection_matrix;
353 mat4x4 view_matrix;
354 mat4x4 model_matrix;
355
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600356 float spin_angle;
357 float spin_increment;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600358 bool pause;
359
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600360 VkDescriptorPool desc_pool;
361 VkDescriptorSet desc_set;
Chia-I Wu6a3c8972015-01-04 16:27:24 +0800362
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600363 bool quit;
David Pinedo2bb7c932015-06-18 17:03:14 -0600364 int32_t curFrame;
365 int32_t frameCount;
Tony Barbour3dddd5d2015-04-29 16:19:20 -0600366 bool validate;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -0600367 PFN_vkDbgCreateMsgCallback dbgCreateMsgCallback;
Tony Barbourd70b42c2015-06-30 14:14:19 -0600368 PFN_vkDbgDestroyMsgCallback dbgDestroyMsgCallback;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -0600369 VkDbgMsgCallback msg_callback;
370
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600371 uint32_t current_buffer;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600372};
373
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600374static VkResult memory_type_from_properties(struct demo *demo, uint32_t typeBits, VkFlags properties, uint32_t *typeIndex)
375{
376 // Search memtypes to find first index with those properties
377 for (uint32_t i = 0; i < 32; i++) {
378 if ((typeBits & 1) == 1) {
379 // Type is available, does it match user properties?
380 if ((demo->memory_properties.memoryTypes[i].propertyFlags & properties) == properties) {
381 *typeIndex = i;
382 return VK_SUCCESS;
383 }
384 }
385 typeBits >>= 1;
386 }
387 // No memory types matched, return failure
388 return VK_UNSUPPORTED;
389}
390
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600391static void demo_flush_init_cmd(struct demo *demo)
392{
Tony Barbourfdc2d352015-04-22 09:02:32 -0600393 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600394
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600395 if (demo->cmd == VK_NULL_HANDLE)
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600396 return;
397
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600398 err = vkEndCommandBuffer(demo->cmd);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600399 assert(!err);
400
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600401 const VkCmdBuffer cmd_bufs[] = { demo->cmd };
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600402
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600403 err = vkQueueSubmit(demo->queue, 1, cmd_bufs, VK_NULL_HANDLE);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600404 assert(!err);
405
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600406 err = vkQueueWaitIdle(demo->queue);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600407 assert(!err);
408
Mike Stroyanebae8322015-04-17 12:36:38 -0600409 vkDestroyObject(demo->device, VK_OBJECT_TYPE_COMMAND_BUFFER, demo->cmd);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600410 demo->cmd = VK_NULL_HANDLE;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600411}
412
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600413static void demo_set_image_layout(
414 struct demo *demo,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600415 VkImage image,
malnasseeb25d3f2015-06-03 17:28:38 -0400416 VkImageAspect aspect,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600417 VkImageLayout old_image_layout,
418 VkImageLayout new_image_layout)
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600419{
Tony Barbourfdc2d352015-04-22 09:02:32 -0600420 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600421
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600422 if (demo->cmd == VK_NULL_HANDLE) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600423 const VkCmdBufferCreateInfo cmd = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600424 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600425 .pNext = NULL,
426 .queueNodeIndex = demo->graphics_queue_node_index,
Chia-I Wu57b23b42015-06-26 15:34:39 +0800427 .level = VK_CMD_BUFFER_LEVEL_PRIMARY,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600428 .flags = 0,
429 };
430
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600431 err = vkCreateCommandBuffer(demo->device, &cmd, &demo->cmd);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600432 assert(!err);
433
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600434 VkCmdBufferBeginInfo cmd_buf_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600435 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600436 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -0600437 .flags = VK_CMD_BUFFER_OPTIMIZE_SMALL_BATCH_BIT |
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600438 VK_CMD_BUFFER_OPTIMIZE_ONE_TIME_SUBMIT_BIT,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600439 };
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600440 err = vkBeginCommandBuffer(demo->cmd, &cmd_buf_info);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600441 }
442
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600443 VkImageMemoryBarrier image_memory_barrier = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600444 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600445 .pNext = NULL,
446 .outputMask = 0,
447 .inputMask = 0,
448 .oldLayout = old_image_layout,
449 .newLayout = new_image_layout,
450 .image = image,
malnasseeb25d3f2015-06-03 17:28:38 -0400451 .subresourceRange = { aspect, 0, 1, 0, 0 }
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600452 };
453
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600454 if (new_image_layout == VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL) {
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600455 /* Make sure anything that was copying from this image has completed */
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -0600456 image_memory_barrier.inputMask = VK_MEMORY_INPUT_TRANSFER_BIT;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600457 }
458
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600459 if (new_image_layout == VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL) {
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600460 /* Make sure any Copy or CPU writes to image are flushed */
Courtney Goeltzenleuchter2bda8332015-04-29 17:16:21 -0600461 image_memory_barrier.outputMask = VK_MEMORY_OUTPUT_HOST_WRITE_BIT | VK_MEMORY_OUTPUT_TRANSFER_BIT;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600462 }
463
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600464 VkImageMemoryBarrier *pmemory_barrier = &image_memory_barrier;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600465
Tony Barbour50bbca42015-06-29 16:20:35 -0600466 VkPipelineStageFlags src_stages = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
467 VkPipelineStageFlags dest_stages = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600468
Tony Barbour50bbca42015-06-29 16:20:35 -0600469 vkCmdPipelineBarrier(demo->cmd, src_stages, dest_stages, false, 1, (const void **)&pmemory_barrier);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600470}
471
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600472static void demo_draw_build_cmd(struct demo *demo, VkCmdBuffer cmd_buf)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600473{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600474 const VkColorAttachmentBindInfo color_attachment = {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600475 .view = demo->buffers[demo->current_buffer].view,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600476 .layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600477 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600478 const VkDepthStencilBindInfo depth_stencil = {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600479 .view = demo->depth.view,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600480 .layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600481 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600482 const VkClearColor clear_color = {
Courtney Goeltzenleuchter374553c2015-04-03 16:35:32 -0600483 .color.floatColor = { 0.2f, 0.2f, 0.2f, 0.2f },
484 .useRawValue = false,
485 };
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600486 const float clear_depth = 1.0f;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600487 VkImageSubresourceRange clear_range;
488 VkCmdBufferBeginInfo cmd_buf_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600489 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO,
Courtney Goeltzenleuchter53968d82015-04-03 15:25:24 -0600490 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -0600491 .flags = VK_CMD_BUFFER_OPTIMIZE_SMALL_BATCH_BIT |
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600492 VK_CMD_BUFFER_OPTIMIZE_ONE_TIME_SUBMIT_BIT,
Jon Ashburn60ccfbe2014-12-31 17:08:35 -0700493 };
Tony Barbourfdc2d352015-04-22 09:02:32 -0600494 VkResult U_ASSERT_ONLY err;
Chris Forbes40a71562015-06-17 11:36:12 +1200495 VkAttachmentLoadOp load_op = VK_ATTACHMENT_LOAD_OP_CLEAR;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600496 VkAttachmentStoreOp store_op = VK_ATTACHMENT_STORE_OP_DONT_CARE;
497 const VkFramebufferCreateInfo fb_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600498 .sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO,
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700499 .pNext = NULL,
500 .colorAttachmentCount = 1,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600501 .pColorAttachments = (VkColorAttachmentBindInfo*) &color_attachment,
502 .pDepthStencilAttachment = (VkDepthStencilBindInfo*) &depth_stencil,
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700503 .sampleCount = 1,
Mark Lobodzinskic06b7412015-01-27 13:24:03 -0600504 .width = demo->width,
505 .height = demo->height,
506 .layers = 1,
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700507 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600508 VkRenderPassCreateInfo rp_info;
509 VkRenderPassBegin rp_begin;
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700510
Chia-I Wu57b23b42015-06-26 15:34:39 +0800511 rp_begin.contents = VK_RENDER_PASS_CONTENTS_INLINE;
512
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700513 memset(&rp_info, 0 , sizeof(rp_info));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600514 err = vkCreateFramebuffer(demo->device, &fb_info, &rp_begin.framebuffer);
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700515 assert(!err);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600516 rp_info.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO;
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700517 rp_info.renderArea.extent.width = demo->width;
518 rp_info.renderArea.extent.height = demo->height;
Courtney Goeltzenleuchter53968d82015-04-03 15:25:24 -0600519 rp_info.colorAttachmentCount = fb_info.colorAttachmentCount;
520 rp_info.pColorFormats = &demo->format;
521 rp_info.pColorLayouts = &color_attachment.layout;
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700522 rp_info.pColorLoadOps = &load_op;
523 rp_info.pColorStoreOps = &store_op;
Courtney Goeltzenleuchter53968d82015-04-03 15:25:24 -0600524 rp_info.pColorLoadClearValues = &clear_color;
Tony Barbour72304ef2015-04-16 15:59:00 -0600525 rp_info.depthStencilFormat = VK_FORMAT_D16_UNORM;
Courtney Goeltzenleuchter53968d82015-04-03 15:25:24 -0600526 rp_info.depthStencilLayout = depth_stencil.layout;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600527 rp_info.depthLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
Courtney Goeltzenleuchter53968d82015-04-03 15:25:24 -0600528 rp_info.depthLoadClearValue = clear_depth;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600529 rp_info.depthStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
530 rp_info.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
Courtney Goeltzenleuchter53968d82015-04-03 15:25:24 -0600531 rp_info.stencilLoadClearValue = 0;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600532 rp_info.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
Tony Barbourd70b42c2015-06-30 14:14:19 -0600533 rp_info.sampleCount = 1;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600534 err = vkCreateRenderPass(demo->device, &rp_info, &rp_begin.renderPass);
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700535 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600536
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600537 err = vkBeginCommandBuffer(cmd_buf, &cmd_buf_info);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600538 assert(!err);
539
Tobin Ehlis080219d2015-06-24 15:53:07 -0600540 vkCmdBeginRenderPass(cmd_buf, &rp_begin);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600541 vkCmdBindPipeline(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600542 demo->pipeline);
Mark Lobodzinskic6e8b3d2015-06-15 13:21:21 -0600543 vkCmdBindDescriptorSets(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS, demo->pipeline_layout,
Cody Northropfb5185a2015-04-16 13:41:56 -0600544 0, 1, &demo->desc_set, 0, NULL);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600545
Tony Barbour72304ef2015-04-16 15:59:00 -0600546 vkCmdBindDynamicStateObject(cmd_buf, VK_STATE_BIND_POINT_VIEWPORT, demo->viewport);
547 vkCmdBindDynamicStateObject(cmd_buf, VK_STATE_BIND_POINT_RASTER, demo->raster);
548 vkCmdBindDynamicStateObject(cmd_buf, VK_STATE_BIND_POINT_COLOR_BLEND,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600549 demo->color_blend);
Tony Barbour72304ef2015-04-16 15:59:00 -0600550 vkCmdBindDynamicStateObject(cmd_buf, VK_STATE_BIND_POINT_DEPTH_STENCIL,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600551 demo->depth_stencil);
552
Chris Forbes40a71562015-06-17 11:36:12 +1200553 clear_range.aspect = VK_IMAGE_ASPECT_DEPTH;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600554 clear_range.baseMipLevel = 0;
555 clear_range.mipLevels = 1;
556 clear_range.baseArraySlice = 0;
557 clear_range.arraySize = 1;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600558
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600559 vkCmdClearDepthStencil(cmd_buf, demo->depth.image,
Mark Lobodzinskic61a7d42015-07-01 15:18:26 -0600560 VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600561 clear_depth, 0, 1, &clear_range);
562
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600563 vkCmdDraw(cmd_buf, 0, 12 * 3, 0, 1);
Chia-I Wu57b23b42015-06-26 15:34:39 +0800564 vkCmdEndRenderPass(cmd_buf);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600565
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600566 err = vkEndCommandBuffer(cmd_buf);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600567 assert(!err);
Courtney Goeltzenleuchter94901dc2015-02-25 17:53:18 -0700568
Mike Stroyanebae8322015-04-17 12:36:38 -0600569 vkDestroyObject(demo->device, VK_OBJECT_TYPE_RENDER_PASS, rp_begin.renderPass);
570 vkDestroyObject(demo->device, VK_OBJECT_TYPE_FRAMEBUFFER, rp_begin.framebuffer);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600571}
572
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600573
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600574void demo_update_data_buffer(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600575{
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600576 mat4x4 MVP, Model, VP;
577 int matrixSize = sizeof(MVP);
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600578 uint8_t *pData;
Tony Barbourfdc2d352015-04-22 09:02:32 -0600579 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600580
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600581 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600582
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600583 // Rotate 22.5 degrees around the Y axis
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600584 mat4x4_dup(Model, demo->model_matrix);
Piers Daniell735ee532015-02-23 16:23:13 -0700585 mat4x4_rotate(demo->model_matrix, Model, 0.0f, 1.0f, 0.0f, (float)degreesToRadians(demo->spin_angle));
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600586 mat4x4_mul(MVP, VP, demo->model_matrix);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600587
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500588 err = vkMapMemory(demo->device, demo->uniform_data.mem, 0, 0, 0, (void **) &pData);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600589 assert(!err);
590
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600591 memcpy(pData, (const void*) &MVP[0][0], matrixSize);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600592
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500593 err = vkUnmapMemory(demo->device, demo->uniform_data.mem);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600594 assert(!err);
595}
596
597static void demo_draw(struct demo *demo)
598{
Chia-I Wucbb564e2015-04-16 22:02:10 +0800599 const VkPresentInfoWSI present = {
600 .sType = VK_STRUCTURE_TYPE_PRESENT_INFO_WSI,
601 .pNext = NULL,
602 .image = demo->buffers[demo->current_buffer].image,
603 .flipInterval = 0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600604 };
Tony Barbourfdc2d352015-04-22 09:02:32 -0600605 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600606
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600607 err = vkQueueSubmit(demo->queue, 1, &demo->buffers[demo->current_buffer].cmd,
608 VK_NULL_HANDLE);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600609 assert(!err);
610
Jon Ashburn0b85d052015-05-21 18:13:33 -0600611 err = demo->fpQueuePresentWSI(demo->queue, &present);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600612 assert(!err);
613
614 demo->current_buffer = (demo->current_buffer + 1) % DEMO_BUFFER_COUNT;
Chia-I Wucbb564e2015-04-16 22:02:10 +0800615
616 err = vkQueueWaitIdle(demo->queue);
617 assert(err == VK_SUCCESS);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600618}
619
620static void demo_prepare_buffers(struct demo *demo)
621{
Chia-I Wucbb564e2015-04-16 22:02:10 +0800622 const VkSwapChainCreateInfoWSI swap_chain = {
623 .sType = VK_STRUCTURE_TYPE_SWAP_CHAIN_CREATE_INFO_WSI,
624 .pNext = NULL,
625 .pNativeWindowSystemHandle = demo->connection,
626 .pNativeWindowHandle = (void *) (intptr_t) demo->window,
Ian Elliotte4602cd2015-04-21 16:41:02 -0600627 .displayCount = 1,
Chia-I Wucbb564e2015-04-16 22:02:10 +0800628 .imageCount = DEMO_BUFFER_COUNT,
629 .imageFormat = demo->format,
630 .imageExtent = {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600631 .width = demo->width,
632 .height = demo->height,
633 },
Chia-I Wucbb564e2015-04-16 22:02:10 +0800634 .imageArraySize = 1,
635 .imageUsageFlags = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600636 };
Chia-I Wucbb564e2015-04-16 22:02:10 +0800637 VkSwapChainImageInfoWSI images[DEMO_BUFFER_COUNT];
638 size_t images_size = sizeof(images);
Tony Barbourfdc2d352015-04-22 09:02:32 -0600639 VkResult U_ASSERT_ONLY err;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600640 uint32_t i;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600641
Jon Ashburn0b85d052015-05-21 18:13:33 -0600642 err = demo->fpCreateSwapChainWSI(demo->device, &swap_chain, &demo->swap_chain);
Chia-I Wucbb564e2015-04-16 22:02:10 +0800643 assert(!err);
644
Jon Ashburn0b85d052015-05-21 18:13:33 -0600645 err = demo->fpGetSwapChainInfoWSI(demo->swap_chain,
Chia-I Wucbb564e2015-04-16 22:02:10 +0800646 VK_SWAP_CHAIN_INFO_TYPE_PERSISTENT_IMAGES_WSI,
647 &images_size, images);
648 assert(!err && images_size == sizeof(images));
649
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600650 for (i = 0; i < DEMO_BUFFER_COUNT; i++) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600651 VkColorAttachmentViewCreateInfo color_attachment_view = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600652 .sType = VK_STRUCTURE_TYPE_COLOR_ATTACHMENT_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600653 .pNext = NULL,
654 .format = demo->format,
655 .mipLevel = 0,
656 .baseArraySlice = 0,
657 .arraySize = 1,
658 };
659
Chia-I Wucbb564e2015-04-16 22:02:10 +0800660 demo->buffers[i].image = images[i].image;
661 demo->buffers[i].mem = images[i].memory;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600662
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600663 demo_set_image_layout(demo, demo->buffers[i].image,
malnasseeb25d3f2015-06-03 17:28:38 -0400664 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600665 VK_IMAGE_LAYOUT_UNDEFINED,
666 VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600667
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600668 color_attachment_view.image = demo->buffers[i].image;
669
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600670 err = vkCreateColorAttachmentView(demo->device,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600671 &color_attachment_view, &demo->buffers[i].view);
672 assert(!err);
673 }
674}
675
676static void demo_prepare_depth(struct demo *demo)
677{
Tony Barbour72304ef2015-04-16 15:59:00 -0600678 const VkFormat depth_format = VK_FORMAT_D16_UNORM;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600679 const VkImageCreateInfo image = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600680 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600681 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -0600682 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600683 .format = depth_format,
684 .extent = { demo->width, demo->height, 1 },
685 .mipLevels = 1,
686 .arraySize = 1,
687 .samples = 1,
Tony Barbour72304ef2015-04-16 15:59:00 -0600688 .tiling = VK_IMAGE_TILING_OPTIMAL,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600689 .usage = VK_IMAGE_USAGE_DEPTH_STENCIL_BIT,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600690 .flags = 0,
691 };
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -0600692 VkMemoryAllocInfo mem_alloc = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600693 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
Mark Lobodzinskia6907f72015-04-16 08:52:00 -0500694 .pNext = NULL,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600695 .allocationSize = 0,
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600696 .memoryTypeIndex = 0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600697 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600698 VkDepthStencilViewCreateInfo view = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600699 .sType = VK_STRUCTURE_TYPE_DEPTH_STENCIL_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600700 .pNext = NULL,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600701 .image = VK_NULL_HANDLE,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600702 .mipLevel = 0,
703 .baseArraySlice = 0,
704 .arraySize = 1,
705 .flags = 0,
706 };
Mike Stroyanebae8322015-04-17 12:36:38 -0600707
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500708 VkMemoryRequirements mem_reqs;
Tony Barbourfdc2d352015-04-22 09:02:32 -0600709 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600710
711 demo->depth.format = depth_format;
712
713 /* create image */
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600714 err = vkCreateImage(demo->device, &image,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600715 &demo->depth.image);
716 assert(!err);
717
Tony Barbourecf1b2b2015-06-24 16:06:58 -0600718 err = vkGetObjectMemoryRequirements(demo->device,
719 VK_OBJECT_TYPE_IMAGE, demo->depth.image, &mem_reqs);
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600720
721 mem_alloc.allocationSize = mem_reqs.size;
722 err = memory_type_from_properties(demo,
723 mem_reqs.memoryTypeBits,
724 VK_MEMORY_PROPERTY_DEVICE_ONLY,
725 &mem_alloc.memoryTypeIndex);
726 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600727
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500728 /* allocate memory */
729 err = vkAllocMemory(demo->device, &mem_alloc, &demo->depth.mem);
730 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600731
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500732 /* bind memory */
733 err = vkBindObjectMemory(demo->device,
734 VK_OBJECT_TYPE_IMAGE, demo->depth.image,
735 demo->depth.mem, 0);
736 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600737
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600738 demo_set_image_layout(demo, demo->depth.image,
malnasseeb25d3f2015-06-03 17:28:38 -0400739 VK_IMAGE_ASPECT_DEPTH,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600740 VK_IMAGE_LAYOUT_UNDEFINED,
741 VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600742
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600743 /* create image view */
744 view.image = demo->depth.image;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600745 err = vkCreateDepthStencilView(demo->device, &view,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600746 &demo->depth.view);
747 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600748}
749
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600750/** loadTexture
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -0600751 * loads a png file into an memory object, using cstdio , libpng.
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600752 *
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -0600753 * \param demo : Needed to access VK calls
754 * \param filename : the png file to be loaded
755 * \param width : width of png, to be updated as a side effect of this function
756 * \param height : height of png, to be updated as a side effect of this function
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600757 *
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -0600758 * \return bool : an opengl texture id. true if successful?,
759 * should be validated by the client of this function.
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600760 *
761 * Source: http://en.wikibooks.org/wiki/OpenGL_Programming/Intermediate/Textures
762 * Modified to copy image to memory
763 *
764 */
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700765bool loadTexture(const char *filename, uint8_t *rgba_data,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600766 VkSubresourceLayout *layout,
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600767 int32_t *width, int32_t *height)
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600768{
769 //header for testing if it is a png
770 png_byte header[8];
Tony Barbourf9921732015-04-22 11:36:22 -0600771 int is_png, bit_depth, color_type, rowbytes;
772 size_t retval;
Ian Elliott1e42dff2015-02-13 14:29:21 -0700773 png_uint_32 i, twidth, theight;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600774 png_structp png_ptr;
775 png_infop info_ptr, end_info;
776 png_byte *image_data;
777 png_bytep *row_pointers;
778
779 //open file as binary
780 FILE *fp = fopen(filename, "rb");
781 if (!fp) {
782 return false;
783 }
784
785 //read the header
Tony Barbourfdc2d352015-04-22 09:02:32 -0600786 retval = fread(header, 1, 8, fp);
787 if (retval != 8) {
788 fclose(fp);
789 return false;
790 }
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600791
792 //test if png
793 is_png = !png_sig_cmp(header, 0, 8);
794 if (!is_png) {
795 fclose(fp);
796 return false;
797 }
798
799 //create png struct
800 png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL,
801 NULL, NULL);
802 if (!png_ptr) {
803 fclose(fp);
804 return (false);
805 }
806
807 //create png info struct
808 info_ptr = png_create_info_struct(png_ptr);
809 if (!info_ptr) {
810 png_destroy_read_struct(&png_ptr, (png_infopp) NULL, (png_infopp) NULL);
811 fclose(fp);
812 return (false);
813 }
814
815 //create png info struct
816 end_info = png_create_info_struct(png_ptr);
817 if (!end_info) {
818 png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp) NULL);
819 fclose(fp);
820 return (false);
821 }
822
823 //png error stuff, not sure libpng man suggests this.
824 if (setjmp(png_jmpbuf(png_ptr))) {
825 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
826 fclose(fp);
827 return (false);
828 }
829
830 //init png reading
831 png_init_io(png_ptr, fp);
832
833 //let libpng know you already read the first 8 bytes
834 png_set_sig_bytes(png_ptr, 8);
835
836 // read all the info up to the image data
837 png_read_info(png_ptr, info_ptr);
838
839 // get info about png
840 png_get_IHDR(png_ptr, info_ptr, &twidth, &theight, &bit_depth, &color_type,
841 NULL, NULL, NULL);
842
843 //update width and height based on png info
844 *width = twidth;
845 *height = theight;
846
847 // Require that incoming texture be 8bits per color component
848 // and 4 components (RGBA).
849 if (png_get_bit_depth(png_ptr, info_ptr) != 8 ||
850 png_get_channels(png_ptr, info_ptr) != 4) {
851 return false;
852 }
853
854 if (rgba_data == NULL) {
855 // If data pointer is null, we just want the width & height
856 // clean up memory and close stuff
857 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
858 fclose(fp);
859
860 return true;
861 }
862
863 // Update the png info struct.
864 png_read_update_info(png_ptr, info_ptr);
865
866 // Row size in bytes.
867 rowbytes = png_get_rowbytes(png_ptr, info_ptr);
868
869 // Allocate the image_data as a big block, to be given to opengl
870 image_data = (png_byte *)malloc(rowbytes * theight * sizeof(png_byte));
871 if (!image_data) {
872 //clean up memory and close stuff
873 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
874 fclose(fp);
875 return false;
876 }
877
878 // row_pointers is for pointing to image_data for reading the png with libpng
879 row_pointers = (png_bytep *)malloc(theight * sizeof(png_bytep));
880 if (!row_pointers) {
881 //clean up memory and close stuff
882 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
883 // delete[] image_data;
884 fclose(fp);
885 return false;
886 }
887 // set the individual row_pointers to point at the correct offsets of image_data
888 for (i = 0; i < theight; ++i)
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700889 row_pointers[theight - 1 - i] = rgba_data + i * layout->rowPitch;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600890
891 // read the png into image_data through row_pointers
892 png_read_image(png_ptr, row_pointers);
893
894 // clean up memory and close stuff
895 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
896 free(row_pointers);
897 free(image_data);
898 fclose(fp);
899
900 return true;
901}
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600902
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700903static void demo_prepare_texture_image(struct demo *demo,
904 const char *filename,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600905 struct texture_object *tex_obj,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600906 VkImageTiling tiling,
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -0600907 VkImageUsageFlags usage,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600908 VkFlags mem_props)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600909{
Mike Stroyan8b89e072015-06-15 14:21:03 -0600910 const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600911 int32_t tex_width;
912 int32_t tex_height;
Tony Barbourfdc2d352015-04-22 09:02:32 -0600913 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700914
David Pinedo30bd71d2015-04-23 08:16:57 -0600915 if (!loadTexture(filename, NULL, NULL, &tex_width, &tex_height))
916 {
917 printf("Failed to load textures\n");
918 fflush(stdout);
919 exit(1);
920 }
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700921
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600922 tex_obj->tex_width = tex_width;
923 tex_obj->tex_height = tex_height;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700924
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600925 const VkImageCreateInfo image_create_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600926 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700927 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -0600928 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700929 .format = tex_format,
930 .extent = { tex_width, tex_height, 1 },
931 .mipLevels = 1,
932 .arraySize = 1,
933 .samples = 1,
934 .tiling = tiling,
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -0600935 .usage = usage,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700936 .flags = 0,
937 };
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -0600938 VkMemoryAllocInfo mem_alloc = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600939 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
Mark Lobodzinskia6907f72015-04-16 08:52:00 -0500940 .pNext = NULL,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700941 .allocationSize = 0,
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600942 .memoryTypeIndex = 0,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700943 };
944
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500945 VkMemoryRequirements mem_reqs;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700946
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600947 err = vkCreateImage(demo->device, &image_create_info,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600948 &tex_obj->image);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700949 assert(!err);
950
Tony Barbourecf1b2b2015-06-24 16:06:58 -0600951 err = vkGetObjectMemoryRequirements(demo->device,
952 VK_OBJECT_TYPE_IMAGE, tex_obj->image, &mem_reqs);
953 assert(!err);
Piers Daniell735ee532015-02-23 16:23:13 -0700954
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500955 mem_alloc.allocationSize = mem_reqs.size;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700956
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600957 err = memory_type_from_properties(demo, mem_reqs.memoryTypeBits, mem_props, &mem_alloc.memoryTypeIndex);
958 assert(!err);
959
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500960 /* allocate memory */
961 err = vkAllocMemory(demo->device, &mem_alloc,
962 &(tex_obj->mem));
963 assert(!err);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700964
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500965 /* bind memory */
966 err = vkBindObjectMemory(demo->device,
967 VK_OBJECT_TYPE_IMAGE, tex_obj->image,
968 tex_obj->mem, 0);
969 assert(!err);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700970
Tony Barbour72304ef2015-04-16 15:59:00 -0600971 if (mem_props & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600972 const VkImageSubresource subres = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600973 .aspect = VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700974 .mipLevel = 0,
975 .arraySlice = 0,
976 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600977 VkSubresourceLayout layout;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700978 void *data;
979
Tony Barbourecf1b2b2015-06-24 16:06:58 -0600980 err = vkGetImageSubresourceLayout(demo->device, tex_obj->image, &subres, &layout);
981 assert(!err);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700982
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500983 err = vkMapMemory(demo->device, tex_obj->mem, 0, 0, 0, &data);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700984 assert(!err);
985
986 if (!loadTexture(filename, data, &layout, &tex_width, &tex_height)) {
987 fprintf(stderr, "Error loading texture: %s\n", filename);
988 }
989
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500990 err = vkUnmapMemory(demo->device, tex_obj->mem);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700991 assert(!err);
992 }
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600993
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600994 tex_obj->imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600995 demo_set_image_layout(demo, tex_obj->image,
malnasseeb25d3f2015-06-03 17:28:38 -0400996 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600997 VK_IMAGE_LAYOUT_UNDEFINED,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600998 tex_obj->imageLayout);
999 /* 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 -07001000}
1001
Mark Lobodzinskida9b1092015-04-16 11:44:05 -05001002static void demo_destroy_texture_image(struct demo *demo, struct texture_object *tex_objs)
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001003{
1004 /* clean up staging resources */
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001005 vkFreeMemory(demo->device, tex_objs->mem);
Mike Stroyanebae8322015-04-17 12:36:38 -06001006 vkDestroyObject(demo->device, VK_OBJECT_TYPE_IMAGE, tex_objs->image);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001007}
1008
1009static void demo_prepare_textures(struct demo *demo)
1010{
Tony Barbour72304ef2015-04-16 15:59:00 -06001011 const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001012 VkFormatProperties props;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001013 VkResult U_ASSERT_ONLY err;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001014 uint32_t i;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001015
Chris Forbesf576a3e2015-06-21 22:55:02 +12001016 err = vkGetPhysicalDeviceFormatInfo(demo->gpu, tex_format, &props);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001017 assert(!err);
1018
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001019 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001020
FslNopper6a7e50e2015-05-06 21:42:01 +02001021 if ((props.linearTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) && !demo->use_staging_buffer) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001022 /* Device can texture using linear textures */
1023 demo_prepare_texture_image(demo, tex_files[i], &demo->textures[i],
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001024 VK_IMAGE_TILING_LINEAR, VK_IMAGE_USAGE_SAMPLED_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
Tony Barbour72304ef2015-04-16 15:59:00 -06001025 } else if (props.optimalTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001026 /* Must use staging buffer to copy linear texture to optimized */
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001027 struct texture_object staging_texture;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001028
1029 memset(&staging_texture, 0, sizeof(staging_texture));
1030 demo_prepare_texture_image(demo, tex_files[i], &staging_texture,
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001031 VK_IMAGE_TILING_LINEAR, VK_IMAGE_USAGE_TRANSFER_SOURCE_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001032
1033 demo_prepare_texture_image(demo, tex_files[i], &demo->textures[i],
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001034 VK_IMAGE_TILING_OPTIMAL,
1035 (VK_IMAGE_USAGE_TRANSFER_DESTINATION_BIT | VK_IMAGE_USAGE_SAMPLED_BIT),
1036 VK_MEMORY_PROPERTY_DEVICE_ONLY);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001037
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001038 demo_set_image_layout(demo, staging_texture.image,
malnasseeb25d3f2015-06-03 17:28:38 -04001039 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001040 staging_texture.imageLayout,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001041 VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001042
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001043 demo_set_image_layout(demo, demo->textures[i].image,
malnasseeb25d3f2015-06-03 17:28:38 -04001044 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001045 demo->textures[i].imageLayout,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001046 VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001047
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001048 VkImageCopy copy_region = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001049 .srcSubresource = { VK_IMAGE_ASPECT_COLOR, 0, 0 },
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001050 .srcOffset = { 0, 0, 0 },
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001051 .destSubresource = { VK_IMAGE_ASPECT_COLOR, 0, 0 },
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001052 .destOffset = { 0, 0, 0 },
1053 .extent = { staging_texture.tex_width, staging_texture.tex_height, 1 },
1054 };
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001055 vkCmdCopyImage(demo->cmd,
1056 staging_texture.image, VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL,
1057 demo->textures[i].image, VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL,
Courtney Goeltzenleuchter8e89a312015-03-25 11:25:10 -06001058 1, &copy_region);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001059
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001060 demo_set_image_layout(demo, demo->textures[i].image,
malnasseeb25d3f2015-06-03 17:28:38 -04001061 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001062 VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001063 demo->textures[i].imageLayout);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001064
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001065 demo_flush_init_cmd(demo);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001066
Courtney Goeltzenleuchterf3aeb2b2015-04-21 09:30:03 -06001067 demo_destroy_texture_image(demo, &staging_texture);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001068 } else {
Mike Stroyan8b89e072015-06-15 14:21:03 -06001069 /* Can't support VK_FORMAT_R8G8B8A8_UNORM !? */
1070 assert(!"No support for R8G8B8A8_UNORM as texture image format");
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001071 }
1072
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001073 const VkSamplerCreateInfo sampler = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001074 .sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001075 .pNext = NULL,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001076 .magFilter = VK_TEX_FILTER_NEAREST,
1077 .minFilter = VK_TEX_FILTER_NEAREST,
Tony Barbour72304ef2015-04-16 15:59:00 -06001078 .mipMode = VK_TEX_MIPMAP_MODE_BASE,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001079 .addressU = VK_TEX_ADDRESS_CLAMP,
1080 .addressV = VK_TEX_ADDRESS_CLAMP,
1081 .addressW = VK_TEX_ADDRESS_CLAMP,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001082 .mipLodBias = 0.0f,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001083 .maxAnisotropy = 1,
Tony Barbour72304ef2015-04-16 15:59:00 -06001084 .compareOp = VK_COMPARE_OP_NEVER,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001085 .minLod = 0.0f,
1086 .maxLod = 0.0f,
Tony Barbourcb530c72015-06-25 16:56:44 -06001087 .borderColor = VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001088 };
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001089
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001090 VkImageViewCreateInfo view = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001091 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001092 .pNext = NULL,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001093 .image = VK_NULL_HANDLE,
Tony Barbour72304ef2015-04-16 15:59:00 -06001094 .viewType = VK_IMAGE_VIEW_TYPE_2D,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001095 .format = tex_format,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001096 .channels = { VK_CHANNEL_SWIZZLE_R,
1097 VK_CHANNEL_SWIZZLE_G,
1098 VK_CHANNEL_SWIZZLE_B,
1099 VK_CHANNEL_SWIZZLE_A, },
1100 .subresourceRange = { VK_IMAGE_ASPECT_COLOR, 0, 1, 0, 1 },
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001101 };
Jon Ashburnb2a66652015-01-16 09:37:43 -07001102
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001103 /* create sampler */
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001104 err = vkCreateSampler(demo->device, &sampler,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001105 &demo->textures[i].sampler);
1106 assert(!err);
1107
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001108 /* create image view */
1109 view.image = demo->textures[i].image;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001110 err = vkCreateImageView(demo->device, &view,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001111 &demo->textures[i].view);
1112 assert(!err);
1113 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001114}
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001115
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001116void demo_prepare_cube_data_buffer(struct demo *demo)
1117{
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -06001118 VkBufferCreateInfo buf_info;
1119 VkBufferViewCreateInfo view_info;
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -06001120 VkMemoryAllocInfo alloc_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001121 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
Mark Lobodzinskia6907f72015-04-16 08:52:00 -05001122 .pNext = NULL,
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001123 .allocationSize = 0,
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06001124 .memoryTypeIndex = 0,
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001125 };
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001126 VkMemoryRequirements mem_reqs;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001127 size_t mem_reqs_size = sizeof(VkMemoryRequirements);
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001128 uint8_t *pData;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001129 int i;
1130 mat4x4 MVP, VP;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001131 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001132 struct vktexcube_vs_uniform data;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001133
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001134 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001135 mat4x4_mul(MVP, VP, demo->model_matrix);
1136 memcpy(data.mvp, MVP, sizeof(MVP));
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001137// dumpMatrix("MVP", MVP);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001138
1139 for (i=0; i<12*3; i++) {
1140 data.position[i][0] = g_vertex_buffer_data[i*3];
1141 data.position[i][1] = g_vertex_buffer_data[i*3+1];
1142 data.position[i][2] = g_vertex_buffer_data[i*3+2];
1143 data.position[i][3] = 1.0f;
1144 data.attr[i][0] = g_uv_buffer_data[2*i];
1145 data.attr[i][1] = g_uv_buffer_data[2*i + 1];
1146 data.attr[i][2] = 0;
1147 data.attr[i][3] = 0;
1148 }
1149
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001150 memset(&buf_info, 0, sizeof(buf_info));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001151 buf_info.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -06001152 buf_info.usage = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001153 err = vkCreateBuffer(demo->device, &buf_info, &demo->uniform_data.buf);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001154 assert(!err);
1155
Tony Barbourecf1b2b2015-06-24 16:06:58 -06001156 err = vkGetObjectMemoryRequirements(demo->device,
1157 VK_OBJECT_TYPE_BUFFER, demo->uniform_data.buf, &mem_reqs);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001158 assert(!err && mem_reqs_size == sizeof(mem_reqs));
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001159
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001160 alloc_info.allocationSize = mem_reqs.size;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06001161 err = memory_type_from_properties(demo,
1162 mem_reqs.memoryTypeBits,
1163 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT,
1164 &alloc_info.memoryTypeIndex);
1165 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001166
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001167 err = vkAllocMemory(demo->device, &alloc_info, &(demo->uniform_data.mem));
1168 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001169
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001170 err = vkMapMemory(demo->device, demo->uniform_data.mem, 0, 0, 0, (void **) &pData);
1171 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001172
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001173 memcpy(pData, &data, sizeof data);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001174
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001175 err = vkUnmapMemory(demo->device, demo->uniform_data.mem);
1176 assert(!err);
1177
1178 err = vkBindObjectMemory(demo->device,
1179 VK_OBJECT_TYPE_BUFFER, demo->uniform_data.buf,
1180 demo->uniform_data.mem, 0);
1181 assert(!err);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001182
1183 memset(&view_info, 0, sizeof(view_info));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001184 view_info.sType = VK_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO;
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001185 view_info.buffer = demo->uniform_data.buf;
Tony Barbour72304ef2015-04-16 15:59:00 -06001186 view_info.viewType = VK_BUFFER_VIEW_TYPE_RAW;
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001187 view_info.offset = 0;
1188 view_info.range = sizeof(data);
1189
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001190 err = vkCreateBufferView(demo->device, &view_info, &demo->uniform_data.view);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001191 assert(!err);
1192
Chia-I Wuae721ba2015-05-25 16:27:55 +08001193 demo->uniform_data.desc.bufferView = demo->uniform_data.view;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001194}
1195
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001196static void demo_prepare_descriptor_layout(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001197{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001198 const VkDescriptorSetLayoutBinding layout_bindings[2] = {
Chia-I Wua2aa8632015-03-26 15:04:41 +08001199 [0] = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001200 .descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
Chia-I Wu6d29a412015-05-25 16:22:52 +08001201 .arraySize = 1,
Tony Barbour72304ef2015-04-16 15:59:00 -06001202 .stageFlags = VK_SHADER_STAGE_VERTEX_BIT,
Chia-I Wu91e8e212015-03-27 12:56:09 +08001203 .pImmutableSamplers = NULL,
Chia-I Wua2aa8632015-03-26 15:04:41 +08001204 },
1205 [1] = {
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -06001206 .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
Chia-I Wu6d29a412015-05-25 16:22:52 +08001207 .arraySize = DEMO_TEXTURE_COUNT,
Tony Barbour72304ef2015-04-16 15:59:00 -06001208 .stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT,
Chia-I Wu91e8e212015-03-27 12:56:09 +08001209 .pImmutableSamplers = NULL,
Chia-I Wua2aa8632015-03-26 15:04:41 +08001210 },
1211 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001212 const VkDescriptorSetLayoutCreateInfo descriptor_layout = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001213 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001214 .pNext = NULL,
Chia-I Wua2aa8632015-03-26 15:04:41 +08001215 .count = 2,
1216 .pBinding = layout_bindings,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001217 };
Tony Barbourfdc2d352015-04-22 09:02:32 -06001218 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001219
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001220 err = vkCreateDescriptorSetLayout(demo->device,
Chia-I Wub58c24a2015-03-26 15:27:55 +08001221 &descriptor_layout, &demo->desc_layout);
1222 assert(!err);
1223
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001224 const VkPipelineLayoutCreateInfo pPipelineLayoutCreateInfo = {
1225 .sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
1226 .pNext = NULL,
1227 .descriptorSetCount = 1,
1228 .pSetLayouts = &demo->desc_layout,
1229 };
1230
1231 err = vkCreatePipelineLayout(demo->device,
1232 &pPipelineLayoutCreateInfo,
1233 &demo->pipeline_layout);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001234 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001235}
1236
Tobin Ehlisd1f17ac2015-07-02 11:02:49 -06001237static VkShader demo_prepare_shader(struct demo* demo,
Tony Barbour72304ef2015-04-16 15:59:00 -06001238 VkShaderStage stage,
Tobin Ehlisd1f17ac2015-07-02 11:02:49 -06001239 const void* code,
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001240 size_t size)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001241{
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001242 VkShaderModuleCreateInfo moduleCreateInfo;
1243 VkShaderCreateInfo shaderCreateInfo;
1244 VkShaderModule shaderModule;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001245 VkShader shader;
1246 VkResult err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001247
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001248
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001249 moduleCreateInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
1250 moduleCreateInfo.pNext = NULL;
1251
1252 shaderCreateInfo.sType = VK_STRUCTURE_TYPE_SHADER_CREATE_INFO;
1253 shaderCreateInfo.pNext = NULL;
Tobin Ehlisd1f17ac2015-07-02 11:02:49 -06001254 shaderCreateInfo.pName = "main";
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001255
Cody Northrop1fedb212015-05-28 11:27:16 -06001256 if (!demo->use_glsl) {
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001257 moduleCreateInfo.codeSize = size;
1258 moduleCreateInfo.pCode = code;
1259 moduleCreateInfo.flags = 0;
1260 err = vkCreateShaderModule(demo->device, &moduleCreateInfo, &shaderModule);
Cody Northrop1fedb212015-05-28 11:27:16 -06001261 if (err) {
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001262 free((void *) moduleCreateInfo.pCode);
Cody Northrop1fedb212015-05-28 11:27:16 -06001263 }
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001264
1265 shaderCreateInfo.flags = 0;
1266 shaderCreateInfo.module = shaderModule;
1267 err = vkCreateShader(demo->device, &shaderCreateInfo, &shader);
Cody Northrop1fedb212015-05-28 11:27:16 -06001268 } else {
1269 // Create fake SPV structure to feed GLSL
1270 // to the driver "under the covers"
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001271 moduleCreateInfo.codeSize = 3 * sizeof(uint32_t) + size + 1;
1272 moduleCreateInfo.pCode = malloc(moduleCreateInfo.codeSize);
1273 moduleCreateInfo.flags = 0;
Cody Northrop1fedb212015-05-28 11:27:16 -06001274
1275 /* try version 0 first: VkShaderStage followed by GLSL */
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001276 ((uint32_t *) moduleCreateInfo.pCode)[0] = ICD_SPV_MAGIC;
1277 ((uint32_t *) moduleCreateInfo.pCode)[1] = 0;
1278 ((uint32_t *) moduleCreateInfo.pCode)[2] = stage;
1279 memcpy(((uint32_t *) moduleCreateInfo.pCode + 3), code, size + 1);
Cody Northrop1fedb212015-05-28 11:27:16 -06001280
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001281 err = vkCreateShaderModule(demo->device, &moduleCreateInfo, &shaderModule);
Cody Northrop1fedb212015-05-28 11:27:16 -06001282 if (err) {
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001283 free((void *) moduleCreateInfo.pCode);
Cody Northrop1fedb212015-05-28 11:27:16 -06001284 }
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001285
1286 shaderCreateInfo.flags = 0;
1287 shaderCreateInfo.module = shaderModule;
1288 err = vkCreateShader(demo->device, &shaderCreateInfo, &shader);
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001289 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001290 return shader;
1291}
1292
Cody Northrop8b12a1f2015-03-17 15:55:58 -06001293char *demo_read_spv(const char *filename, size_t *psize)
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001294{
1295 long int size;
Tony Barbourf9921732015-04-22 11:36:22 -06001296 size_t U_ASSERT_ONLY retval;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001297 void *shader_code;
1298
1299 FILE *fp = fopen(filename, "rb");
1300 if (!fp) return NULL;
1301
1302 fseek(fp, 0L, SEEK_END);
1303 size = ftell(fp);
1304
1305 fseek(fp, 0L, SEEK_SET);
1306
1307 shader_code = malloc(size);
Tony Barbourfdc2d352015-04-22 09:02:32 -06001308 retval = fread(shader_code, size, 1, fp);
1309 assert(retval == 1);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001310
1311 *psize = size;
1312
1313 return shader_code;
1314}
1315
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001316static VkShader demo_prepare_vs(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001317{
Cody Northrop1fedb212015-05-28 11:27:16 -06001318 if (!demo->use_glsl) {
1319 void *vertShaderCode;
1320 size_t size;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001321
Cody Northrop1fedb212015-05-28 11:27:16 -06001322 vertShaderCode = demo_read_spv("cube-vert.spv", &size);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001323
Cody Northrop1fedb212015-05-28 11:27:16 -06001324 return demo_prepare_shader(demo, VK_SHADER_STAGE_VERTEX,
1325 vertShaderCode, size);
1326 } else {
1327 static const char *vertShaderText =
1328 "#version 140\n"
1329 "#extension GL_ARB_separate_shader_objects : enable\n"
1330 "#extension GL_ARB_shading_language_420pack : enable\n"
1331 "\n"
1332 "layout(binding = 0) uniform buf {\n"
1333 " mat4 MVP;\n"
1334 " vec4 position[12*3];\n"
1335 " vec4 attr[12*3];\n"
1336 "} ubuf;\n"
1337 "\n"
1338 "layout (location = 0) out vec4 texcoord;\n"
1339 "\n"
1340 "void main() \n"
1341 "{\n"
1342 " texcoord = ubuf.attr[gl_VertexID];\n"
1343 " gl_Position = ubuf.MVP * ubuf.position[gl_VertexID];\n"
1344 "\n"
1345 " // GL->VK conventions\n"
1346 " gl_Position.y = -gl_Position.y;\n"
1347 " gl_Position.z = (gl_Position.z + gl_Position.w) / 2.0;\n"
1348 "}\n";
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001349
Cody Northrop1fedb212015-05-28 11:27:16 -06001350 return demo_prepare_shader(demo, VK_SHADER_STAGE_VERTEX,
1351 (const void *) vertShaderText,
1352 strlen(vertShaderText));
1353 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001354}
1355
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001356static VkShader demo_prepare_fs(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001357{
Cody Northrop1fedb212015-05-28 11:27:16 -06001358 if (!demo->use_glsl) {
1359 void *fragShaderCode;
1360 size_t size;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001361
Cody Northrop1fedb212015-05-28 11:27:16 -06001362 fragShaderCode = demo_read_spv("cube-frag.spv", &size);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001363
Cody Northrop1fedb212015-05-28 11:27:16 -06001364 return demo_prepare_shader(demo, VK_SHADER_STAGE_FRAGMENT,
1365 fragShaderCode, size);
1366 } else {
1367 static const char *fragShaderText =
1368 "#version 140\n"
1369 "#extension GL_ARB_separate_shader_objects : enable\n"
1370 "#extension GL_ARB_shading_language_420pack : enable\n"
1371 "layout (binding = 1) uniform sampler2D tex;\n"
1372 "\n"
1373 "layout (location = 0) in vec4 texcoord;\n"
1374 "layout (location = 0) out vec4 uFragColor;\n"
1375 "void main() {\n"
1376 " uFragColor = texture(tex, texcoord.xy);\n"
1377 "}\n";
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001378
Cody Northrop1fedb212015-05-28 11:27:16 -06001379 return demo_prepare_shader(demo, VK_SHADER_STAGE_FRAGMENT,
1380 (const void *) fragShaderText,
1381 strlen(fragShaderText));
1382 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001383}
1384
1385static void demo_prepare_pipeline(struct demo *demo)
1386{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001387 VkGraphicsPipelineCreateInfo pipeline;
Mark Lobodzinski521d7762015-06-23 15:11:57 -06001388
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001389 VkPipelineIaStateCreateInfo ia;
1390 VkPipelineRsStateCreateInfo rs;
1391 VkPipelineCbStateCreateInfo cb;
1392 VkPipelineDsStateCreateInfo ds;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001393 VkPipelineVpStateCreateInfo vp;
1394 VkPipelineMsStateCreateInfo ms;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001395 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001396
1397 memset(&pipeline, 0, sizeof(pipeline));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001398 pipeline.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001399 pipeline.layout = demo->pipeline_layout;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001400
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001401 memset(&ia, 0, sizeof(ia));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001402 ia.sType = VK_STRUCTURE_TYPE_PIPELINE_IA_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06001403 ia.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001404
1405 memset(&rs, 0, sizeof(rs));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001406 rs.sType = VK_STRUCTURE_TYPE_PIPELINE_RS_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06001407 rs.fillMode = VK_FILL_MODE_SOLID;
1408 rs.cullMode = VK_CULL_MODE_BACK;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001409 rs.frontFace = VK_FRONT_FACE_CCW;
Chia-I Wubb67e6e2015-04-22 14:20:52 +08001410 rs.depthClipEnable = VK_TRUE;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001411
1412 memset(&cb, 0, sizeof(cb));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001413 cb.sType = VK_STRUCTURE_TYPE_PIPELINE_CB_STATE_CREATE_INFO;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001414 VkPipelineCbAttachmentState att_state[1];
Tony Barbour29645d02015-01-16 14:27:35 -07001415 memset(att_state, 0, sizeof(att_state));
1416 att_state[0].format = demo->format;
1417 att_state[0].channelWriteMask = 0xf;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001418 att_state[0].blendEnable = VK_FALSE;
Tony Barbour29645d02015-01-16 14:27:35 -07001419 cb.attachmentCount = 1;
1420 cb.pAttachments = att_state;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001421
Tony Barbour29645d02015-01-16 14:27:35 -07001422 memset(&vp, 0, sizeof(vp));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001423 vp.sType = VK_STRUCTURE_TYPE_PIPELINE_VP_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06001424 vp.viewportCount = 1;
Tony Barbour29645d02015-01-16 14:27:35 -07001425
1426 memset(&ds, 0, sizeof(ds));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001427 ds.sType = VK_STRUCTURE_TYPE_PIPELINE_DS_STATE_CREATE_INFO;
Tony Barbour29645d02015-01-16 14:27:35 -07001428 ds.format = demo->depth.format;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001429 ds.depthTestEnable = VK_TRUE;
1430 ds.depthWriteEnable = VK_TRUE;
Tony Barbour72304ef2015-04-16 15:59:00 -06001431 ds.depthCompareOp = VK_COMPARE_OP_LESS_EQUAL;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001432 ds.depthBoundsEnable = VK_FALSE;
1433 ds.back.stencilFailOp = VK_STENCIL_OP_KEEP;
1434 ds.back.stencilPassOp = VK_STENCIL_OP_KEEP;
Tony Barbour72304ef2015-04-16 15:59:00 -06001435 ds.back.stencilCompareOp = VK_COMPARE_OP_ALWAYS;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001436 ds.stencilTestEnable = VK_FALSE;
Tony Barbour29645d02015-01-16 14:27:35 -07001437 ds.front = ds.back;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001438
Tony Barbour29645d02015-01-16 14:27:35 -07001439 memset(&ms, 0, sizeof(ms));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001440 ms.sType = VK_STRUCTURE_TYPE_PIPELINE_MS_STATE_CREATE_INFO;
Tony Barbour29645d02015-01-16 14:27:35 -07001441 ms.sampleMask = 1;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001442 ms.multisampleEnable = VK_FALSE;
Tony Barbour3ca22502015-06-26 10:18:34 -06001443 ms.rasterSamples = 1;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001444
Mark Lobodzinski521d7762015-06-23 15:11:57 -06001445 // Two stages: vs and fs
1446 pipeline.stageCount = 2;
1447 VkPipelineShaderStageCreateInfo shaderStages[2];
1448 memset(&shaderStages, 0, 2 * sizeof(VkPipelineShaderStageCreateInfo));
1449
1450 shaderStages[0].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1451 shaderStages[0].stage = VK_SHADER_STAGE_VERTEX;
1452 shaderStages[0].shader = demo_prepare_vs(demo);
1453
1454 shaderStages[1].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1455 shaderStages[1].stage = VK_SHADER_STAGE_FRAGMENT;
1456 shaderStages[1].shader = demo_prepare_fs(demo);
1457
Mark Lobodzinski15169cf2015-06-30 10:18:36 -06001458 pipeline.pVertexInputState = NULL;
Mark Lobodzinski521d7762015-06-23 15:11:57 -06001459 pipeline.pIaState = &ia;
1460 pipeline.pRsState = &rs;
1461 pipeline.pCbState = &cb;
1462 pipeline.pMsState = &ms;
1463 pipeline.pVpState = &vp;
1464 pipeline.pDsState = &ds;
1465 pipeline.pStages = shaderStages;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001466
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001467 err = vkCreateGraphicsPipeline(demo->device, &pipeline, &demo->pipeline);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001468 assert(!err);
1469
Mark Lobodzinski521d7762015-06-23 15:11:57 -06001470 for (uint32_t i = 0; i < pipeline.stageCount; i++) {
1471 vkDestroyObject(demo->device, VK_OBJECT_TYPE_SHADER, shaderStages[i].shader);
1472 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001473}
1474
1475static void demo_prepare_dynamic_states(struct demo *demo)
1476{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001477 VkDynamicVpStateCreateInfo viewport_create;
1478 VkDynamicRsStateCreateInfo raster;
1479 VkDynamicCbStateCreateInfo color_blend;
1480 VkDynamicDsStateCreateInfo depth_stencil;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001481 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001482
Tony Barbour29645d02015-01-16 14:27:35 -07001483 memset(&viewport_create, 0, sizeof(viewport_create));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001484 viewport_create.sType = VK_STRUCTURE_TYPE_DYNAMIC_VP_STATE_CREATE_INFO;
Courtney Goeltzenleuchter1710d8d2015-02-11 14:13:34 -07001485 viewport_create.viewportAndScissorCount = 1;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001486 VkViewport viewport;
Piers Daniell735ee532015-02-23 16:23:13 -07001487 memset(&viewport, 0, sizeof(viewport));
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001488 viewport.height = (float) demo->height;
1489 viewport.width = (float) demo->width;
1490 viewport.minDepth = (float) 0.0f;
1491 viewport.maxDepth = (float) 1.0f;
Piers Daniell735ee532015-02-23 16:23:13 -07001492 viewport_create.pViewports = &viewport;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001493 VkRect scissor;
Piers Daniell735ee532015-02-23 16:23:13 -07001494 memset(&scissor, 0, sizeof(scissor));
Courtney Goeltzenleuchter1710d8d2015-02-11 14:13:34 -07001495 scissor.extent.width = demo->width;
1496 scissor.extent.height = demo->height;
1497 scissor.offset.x = 0;
1498 scissor.offset.y = 0;
Courtney Goeltzenleuchter1710d8d2015-02-11 14:13:34 -07001499 viewport_create.pScissors = &scissor;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001500
1501 memset(&raster, 0, sizeof(raster));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001502 raster.sType = VK_STRUCTURE_TYPE_DYNAMIC_RS_STATE_CREATE_INFO;
Piers Daniell735ee532015-02-23 16:23:13 -07001503 raster.lineWidth = 1.0;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001504
1505 memset(&color_blend, 0, sizeof(color_blend));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001506 color_blend.sType = VK_STRUCTURE_TYPE_DYNAMIC_CB_STATE_CREATE_INFO;
Piers Daniell735ee532015-02-23 16:23:13 -07001507 color_blend.blendConst[0] = 1.0f;
1508 color_blend.blendConst[1] = 1.0f;
1509 color_blend.blendConst[2] = 1.0f;
1510 color_blend.blendConst[3] = 1.0f;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001511
1512 memset(&depth_stencil, 0, sizeof(depth_stencil));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001513 depth_stencil.sType = VK_STRUCTURE_TYPE_DYNAMIC_DS_STATE_CREATE_INFO;
Mark Lobodzinski89d32b12015-06-12 11:14:17 -06001514 depth_stencil.minDepthBounds = 0.0f;
1515 depth_stencil.maxDepthBounds = 1.0f;
Tony Barbour29645d02015-01-16 14:27:35 -07001516 depth_stencil.stencilBackRef = 0;
1517 depth_stencil.stencilFrontRef = 0;
1518 depth_stencil.stencilReadMask = 0xff;
1519 depth_stencil.stencilWriteMask = 0xff;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001520
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001521 err = vkCreateDynamicViewportState(demo->device, &viewport_create, &demo->viewport);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001522 assert(!err);
1523
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001524 err = vkCreateDynamicRasterState(demo->device, &raster, &demo->raster);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001525 assert(!err);
1526
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001527 err = vkCreateDynamicColorBlendState(demo->device,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001528 &color_blend, &demo->color_blend);
1529 assert(!err);
1530
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001531 err = vkCreateDynamicDepthStencilState(demo->device,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001532 &depth_stencil, &demo->depth_stencil);
1533 assert(!err);
1534}
1535
Chia-I Wu63ea9262015-03-26 13:14:16 +08001536static void demo_prepare_descriptor_pool(struct demo *demo)
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001537{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001538 const VkDescriptorTypeCount type_counts[2] = {
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001539 [0] = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001540 .type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001541 .count = 1,
1542 },
1543 [1] = {
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -06001544 .type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001545 .count = DEMO_TEXTURE_COUNT,
1546 },
1547 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001548 const VkDescriptorPoolCreateInfo descriptor_pool = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001549 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001550 .pNext = NULL,
1551 .count = 2,
1552 .pTypeCount = type_counts,
1553 };
Tony Barbourfdc2d352015-04-22 09:02:32 -06001554 VkResult U_ASSERT_ONLY err;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001555
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001556 err = vkCreateDescriptorPool(demo->device,
1557 VK_DESCRIPTOR_POOL_USAGE_ONE_SHOT, 1,
Chia-I Wu63ea9262015-03-26 13:14:16 +08001558 &descriptor_pool, &demo->desc_pool);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001559 assert(!err);
1560}
1561
1562static void demo_prepare_descriptor_set(struct demo *demo)
1563{
Chia-I Wuae721ba2015-05-25 16:27:55 +08001564 VkDescriptorInfo tex_descs[DEMO_TEXTURE_COUNT];
1565 VkWriteDescriptorSet writes[2];
Tony Barbourfdc2d352015-04-22 09:02:32 -06001566 VkResult U_ASSERT_ONLY err;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001567 uint32_t count;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001568 uint32_t i;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001569
Mike Stroyanebae8322015-04-17 12:36:38 -06001570 err = vkAllocDescriptorSets(demo->device, demo->desc_pool,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001571 VK_DESCRIPTOR_SET_USAGE_STATIC,
Chia-I Wu87544e72015-02-23 10:41:08 -07001572 1, &demo->desc_layout,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001573 &demo->desc_set, &count);
1574 assert(!err && count == 1);
1575
Chia-I Wuae721ba2015-05-25 16:27:55 +08001576 memset(&tex_descs, 0, sizeof(tex_descs));
1577 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
1578 tex_descs[i].sampler = demo->textures[i].sampler;
1579 tex_descs[i].imageView = demo->textures[i].view;
1580 tex_descs[i].imageLayout = VK_IMAGE_LAYOUT_GENERAL;
1581 }
1582
1583 memset(&writes, 0, sizeof(writes));
1584
1585 writes[0].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
1586 writes[0].destSet = demo->desc_set;
1587 writes[0].count = 1;
1588 writes[0].descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
1589 writes[0].pDescriptors = &demo->uniform_data.desc;
1590
1591 writes[1].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
1592 writes[1].destSet = demo->desc_set;
1593 writes[1].destBinding = 1;
1594 writes[1].count = DEMO_TEXTURE_COUNT;
1595 writes[1].descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
1596 writes[1].pDescriptors = tex_descs;
1597
1598 err = vkUpdateDescriptorSets(demo->device, 2, writes, 0, NULL);
1599 assert(!err);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001600}
1601
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001602static void demo_prepare(struct demo *demo)
1603{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001604 const VkCmdBufferCreateInfo cmd = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001605 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001606 .pNext = NULL,
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07001607 .queueNodeIndex = demo->graphics_queue_node_index,
Chia-I Wu57b23b42015-06-26 15:34:39 +08001608 .level = VK_CMD_BUFFER_LEVEL_PRIMARY,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001609 .flags = 0,
1610 };
Tony Barbourfdc2d352015-04-22 09:02:32 -06001611 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001612
1613 demo_prepare_buffers(demo);
1614 demo_prepare_depth(demo);
1615 demo_prepare_textures(demo);
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001616 demo_prepare_cube_data_buffer(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001617
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001618 demo_prepare_descriptor_layout(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001619 demo_prepare_pipeline(demo);
1620 demo_prepare_dynamic_states(demo);
1621
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001622 for (int i = 0; i < DEMO_BUFFER_COUNT; i++) {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001623 err = vkCreateCommandBuffer(demo->device, &cmd, &demo->buffers[i].cmd);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001624 assert(!err);
1625 }
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001626
Chia-I Wu63ea9262015-03-26 13:14:16 +08001627 demo_prepare_descriptor_pool(demo);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001628 demo_prepare_descriptor_set(demo);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001629
1630
1631 for (int i = 0; i < DEMO_BUFFER_COUNT; i++) {
1632 demo->current_buffer = i;
1633 demo_draw_build_cmd(demo, demo->buffers[i].cmd);
1634 }
1635
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001636 /*
1637 * Prepare functions above may generate pipeline commands
1638 * that need to be flushed before beginning the render loop.
1639 */
1640 demo_flush_init_cmd(demo);
1641
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001642 demo->current_buffer = 0;
Jon Ashburn8d26c062015-04-24 09:46:24 -07001643 demo->prepared = true;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001644}
1645
David Pinedo2bb7c932015-06-18 17:03:14 -06001646static void demo_cleanup(struct demo *demo)
1647{
1648 uint32_t i;
1649
1650 demo->prepared = false;
1651
1652 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DESCRIPTOR_SET, demo->desc_set);
1653 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DESCRIPTOR_POOL, demo->desc_pool);
1654
1655 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DYNAMIC_VP_STATE, demo->viewport);
1656 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DYNAMIC_RS_STATE, demo->raster);
1657 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DYNAMIC_CB_STATE, demo->color_blend);
1658 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DYNAMIC_DS_STATE, demo->depth_stencil);
1659
1660 vkDestroyObject(demo->device, VK_OBJECT_TYPE_PIPELINE, demo->pipeline);
1661 vkDestroyObject(demo->device, VK_OBJECT_TYPE_PIPELINE_LAYOUT, demo->pipeline_layout);
1662 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DESCRIPTOR_SET_LAYOUT, demo->desc_layout);
1663
1664 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
1665 vkDestroyObject(demo->device, VK_OBJECT_TYPE_IMAGE_VIEW, demo->textures[i].view);
1666 vkDestroyObject(demo->device, VK_OBJECT_TYPE_IMAGE, demo->textures[i].image);
1667 vkFreeMemory(demo->device, demo->textures[i].mem);
1668 vkDestroyObject(demo->device, VK_OBJECT_TYPE_SAMPLER, demo->textures[i].sampler);
1669 }
1670 demo->fpDestroySwapChainWSI(demo->swap_chain);
1671
1672 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DEPTH_STENCIL_VIEW, demo->depth.view);
1673 vkDestroyObject(demo->device, VK_OBJECT_TYPE_IMAGE, demo->depth.image);
1674 vkFreeMemory(demo->device, demo->depth.mem);
1675
1676 vkDestroyObject(demo->device, VK_OBJECT_TYPE_BUFFER_VIEW, demo->uniform_data.view);
1677 vkDestroyObject(demo->device, VK_OBJECT_TYPE_BUFFER, demo->uniform_data.buf);
1678 vkFreeMemory(demo->device, demo->uniform_data.mem);
1679
1680 for (i = 0; i < DEMO_BUFFER_COUNT; i++) {
1681 vkDestroyObject(demo->device, VK_OBJECT_TYPE_COLOR_ATTACHMENT_VIEW, demo->buffers[i].view);
1682 vkDestroyObject(demo->device, VK_OBJECT_TYPE_COMMAND_BUFFER, demo->buffers[i].cmd);
1683 }
1684
1685 vkDestroyDevice(demo->device);
Tony Barbourd70b42c2015-06-30 14:14:19 -06001686 if (demo->validate) {
1687 demo->dbgDestroyMsgCallback(demo->inst, demo->msg_callback);
1688 }
David Pinedo2bb7c932015-06-18 17:03:14 -06001689 vkDestroyInstance(demo->inst);
1690
1691#ifndef _WIN32
1692 xcb_destroy_window(demo->connection, demo->window);
1693 xcb_disconnect(demo->connection);
1694#endif // _WIN32
1695}
1696
1697// On MS-Windows, make this a global, so it's available to WndProc()
1698struct demo demo;
1699
Ian Elliott639ca472015-04-16 15:23:05 -06001700#ifdef _WIN32
1701static void demo_run(struct demo *demo)
1702{
Courtney Goeltzenleuchterb7e22702015-04-27 14:56:34 -06001703 if (!demo->prepared)
1704 return;
Ian Elliott639ca472015-04-16 15:23:05 -06001705 // Wait for work to finish before updating MVP.
1706 vkDeviceWaitIdle(demo->device);
1707 demo_update_data_buffer(demo);
1708
1709 demo_draw(demo);
1710
1711 // Wait for work to finish before updating MVP.
1712 vkDeviceWaitIdle(demo->device);
Ian Elliott639ca472015-04-16 15:23:05 -06001713
David Pinedo2bb7c932015-06-18 17:03:14 -06001714 demo->curFrame++;
1715
1716 if (demo->frameCount != INT_MAX && demo->curFrame == demo->frameCount)
1717 {
1718 demo->quit=true;
1719 demo_cleanup(demo);
1720 ExitProcess(0);
1721 }
1722
1723}
Ian Elliott639ca472015-04-16 15:23:05 -06001724
1725// MS-Windows event handling function:
1726LRESULT CALLBACK WndProc(HWND hWnd,
1727 UINT uMsg,
1728 WPARAM wParam,
1729 LPARAM lParam)
1730{
Ian Elliott639ca472015-04-16 15:23:05 -06001731 switch(uMsg)
1732 {
Ian Elliott639ca472015-04-16 15:23:05 -06001733 case WM_CLOSE:
1734 PostQuitMessage(0);
Tony Barbour3dddd5d2015-04-29 16:19:20 -06001735 break;
Ian Elliott639ca472015-04-16 15:23:05 -06001736 case WM_PAINT:
1737 demo_run(&demo);
1738 return 0;
1739 default:
1740 break;
1741 }
1742 return (DefWindowProc(hWnd, uMsg, wParam, lParam));
1743}
1744
1745static void demo_create_window(struct demo *demo)
1746{
1747 WNDCLASSEX win_class;
1748
1749 // Initialize the window class structure:
1750 win_class.cbSize = sizeof(WNDCLASSEX);
1751 win_class.style = CS_HREDRAW | CS_VREDRAW;
1752 win_class.lpfnWndProc = WndProc;
1753 win_class.cbClsExtra = 0;
1754 win_class.cbWndExtra = 0;
1755 win_class.hInstance = demo->connection; // hInstance
1756 win_class.hIcon = LoadIcon(NULL, IDI_APPLICATION);
1757 win_class.hCursor = LoadCursor(NULL, IDC_ARROW);
1758 win_class.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
1759 win_class.lpszMenuName = NULL;
1760 win_class.lpszClassName = demo->name;
1761 win_class.hIconSm = LoadIcon(NULL, IDI_WINLOGO);
1762 // Register window class:
1763 if (!RegisterClassEx(&win_class)) {
1764 // It didn't work, so try to give a useful error:
1765 printf("Unexpected error trying to start the application!\n");
1766 fflush(stdout);
1767 exit(1);
1768 }
1769 // Create window with the registered class:
Mike Stroyanb46779e2015-06-15 14:20:13 -06001770 RECT wr = { 0, 0, demo->width, demo->height };
1771 AdjustWindowRect(&wr, WS_OVERLAPPEDWINDOW, FALSE);
Ian Elliott639ca472015-04-16 15:23:05 -06001772 demo->window = CreateWindowEx(0,
1773 demo->name, // class name
1774 demo->name, // app name
1775 WS_OVERLAPPEDWINDOW | // window style
1776 WS_VISIBLE |
1777 WS_SYSMENU,
1778 100,100, // x/y coords
Mike Stroyanb46779e2015-06-15 14:20:13 -06001779 wr.right-wr.left, // width
1780 wr.bottom-wr.top, // height
Ian Elliott639ca472015-04-16 15:23:05 -06001781 NULL, // handle to parent
1782 NULL, // handle to menu
1783 demo->connection, // hInstance
1784 NULL); // no extra parameters
1785 if (!demo->window) {
1786 // It didn't work, so try to give a useful error:
1787 printf("Cannot create a window in which to draw!\n");
1788 fflush(stdout);
1789 exit(1);
1790 }
1791}
1792#else // _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001793static void demo_handle_event(struct demo *demo,
1794 const xcb_generic_event_t *event)
1795{
Piers Daniell735ee532015-02-23 16:23:13 -07001796 uint8_t event_code = event->response_type & 0x7f;
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07001797 switch (event_code) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001798 case XCB_EXPOSE:
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07001799 // TODO: Resize window
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001800 break;
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07001801 case XCB_CLIENT_MESSAGE:
1802 if((*(xcb_client_message_event_t*)event).data.data32[0] ==
1803 (*demo->atom_wm_delete_window).atom) {
1804 demo->quit = true;
1805 }
1806 break;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001807 case XCB_KEY_RELEASE:
1808 {
1809 const xcb_key_release_event_t *key =
1810 (const xcb_key_release_event_t *) event;
1811
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001812 switch (key->detail) {
1813 case 0x9: // Escape
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001814 demo->quit = true;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001815 break;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001816 case 0x71: // left arrow key
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001817 demo->spin_angle += demo->spin_increment;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001818 break;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001819 case 0x72: // right arrow key
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001820 demo->spin_angle -= demo->spin_increment;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001821 break;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001822 case 0x41:
1823 demo->pause = !demo->pause;
Piers Daniell735ee532015-02-23 16:23:13 -07001824 break;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001825 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001826 }
1827 break;
1828 default:
1829 break;
1830 }
1831}
1832
1833static void demo_run(struct demo *demo)
1834{
1835 xcb_flush(demo->connection);
1836
1837 while (!demo->quit) {
1838 xcb_generic_event_t *event;
1839
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07001840 if (demo->pause) {
1841 event = xcb_wait_for_event(demo->connection);
1842 } else {
1843 event = xcb_poll_for_event(demo->connection);
1844 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001845 if (event) {
1846 demo_handle_event(demo, event);
1847 free(event);
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07001848 }
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07001849
1850 // Wait for work to finish before updating MVP.
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001851 vkDeviceWaitIdle(demo->device);
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07001852 demo_update_data_buffer(demo);
1853
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07001854 demo_draw(demo);
Courtney Goeltzenleuchter1454f3c2014-11-18 11:28:09 -07001855
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07001856 // Wait for work to finish before updating MVP.
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001857 vkDeviceWaitIdle(demo->device);
David Pinedo2bb7c932015-06-18 17:03:14 -06001858 demo->curFrame++;
1859 if (demo->frameCount != INT_MAX && demo->curFrame == demo->frameCount)
1860 demo->quit = true;
1861
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001862 }
1863}
1864
1865static void demo_create_window(struct demo *demo)
1866{
1867 uint32_t value_mask, value_list[32];
1868
1869 demo->window = xcb_generate_id(demo->connection);
1870
1871 value_mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK;
1872 value_list[0] = demo->screen->black_pixel;
1873 value_list[1] = XCB_EVENT_MASK_KEY_RELEASE |
1874 XCB_EVENT_MASK_EXPOSURE;
1875
1876 xcb_create_window(demo->connection,
1877 XCB_COPY_FROM_PARENT,
1878 demo->window, demo->screen->root,
1879 0, 0, demo->width, demo->height, 0,
1880 XCB_WINDOW_CLASS_INPUT_OUTPUT,
1881 demo->screen->root_visual,
1882 value_mask, value_list);
1883
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07001884 /* Magic code that will send notification when window is destroyed */
1885 xcb_intern_atom_cookie_t cookie = xcb_intern_atom(demo->connection, 1, 12,
1886 "WM_PROTOCOLS");
1887 xcb_intern_atom_reply_t* reply = xcb_intern_atom_reply(demo->connection, cookie, 0);
1888
1889 xcb_intern_atom_cookie_t cookie2 = xcb_intern_atom(demo->connection, 0, 16, "WM_DELETE_WINDOW");
1890 demo->atom_wm_delete_window = xcb_intern_atom_reply(demo->connection, cookie2, 0);
1891
1892 xcb_change_property(demo->connection, XCB_PROP_MODE_REPLACE,
1893 demo->window, (*reply).atom, 4, 32, 1,
1894 &(*demo->atom_wm_delete_window).atom);
1895 free(reply);
1896
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001897 xcb_map_window(demo->connection, demo->window);
David Pinedo2bb7c932015-06-18 17:03:14 -06001898
1899 // Force the x/y coordinates to 100,100 results are identical in consecutive runs
1900 const uint32_t coords[] = {100, 100};
1901 xcb_configure_window(demo->connection, demo->window,
1902 XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y, coords);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001903}
Ian Elliott639ca472015-04-16 15:23:05 -06001904#endif // _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001905
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001906static void demo_init_vk(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001907{
Tobin Ehlis99239dc2015-04-16 18:04:57 -06001908 VkResult err;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06001909 VkExtensionProperties *instance_extensions;
1910 uint32_t instance_extension_count = 0;
1911 VkExtensionProperties *device_extensions;
1912 uint32_t device_extension_count = 0;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06001913 uint32_t total_extension_count = 0;
Tony Barbourecf1b2b2015-06-24 16:06:58 -06001914 err = vkGetGlobalExtensionCount(&total_extension_count);
Tobin Ehlis99239dc2015-04-16 18:04:57 -06001915 assert(!err);
1916
1917 VkExtensionProperties extProp;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06001918 bool32_t WSIextFound = 0;
1919 instance_extensions = malloc(sizeof(VkExtensionProperties) * total_extension_count);
1920 device_extensions = malloc(sizeof(VkExtensionProperties) * total_extension_count);
1921 for (uint32_t i = 0; i < total_extension_count; i++) {
Tony Barbourecf1b2b2015-06-24 16:06:58 -06001922 err = vkGetGlobalExtensionProperties(i, &extProp);
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06001923 if (!strcmp("VK_WSI_LunarG", extProp.name)) {
1924 WSIextFound = 1;
1925 memcpy(&instance_extensions[instance_extension_count++], &extProp, sizeof(VkExtensionProperties));
1926 memcpy(&device_extensions[device_extension_count++], &extProp, sizeof(VkExtensionProperties));
1927 }
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06001928 if (!strcmp(DEBUG_REPORT_EXTENSION_NAME, extProp.name)) {
1929 memcpy(&instance_extensions[instance_extension_count++], &extProp, sizeof(VkExtensionProperties));
1930 }
Tony Barbourd70b42c2015-06-30 14:14:19 -06001931 if (demo->validate && !strcmp("Validation",extProp.name)) {
1932 memcpy(&instance_extensions[instance_extension_count++], &extProp, sizeof(VkExtensionProperties));
1933 memcpy(&device_extensions[device_extension_count++], &extProp, sizeof(VkExtensionProperties));
1934 }
Tobin Ehlis99239dc2015-04-16 18:04:57 -06001935 }
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06001936 if (!WSIextFound) {
Tony Barbourecf1b2b2015-06-24 16:06:58 -06001937 ERR_EXIT("vkGetGlobalExtensionProperties failed to find the "
Ian Elliott65152912015-04-28 13:22:33 -06001938 "\"VK_WSI_LunarG\" extension.\n\nDo you have a compatible "
1939 "Vulkan installable client driver (ICD) installed?\nPlease "
1940 "look at the Getting Started guide for additional "
1941 "information.\n",
1942 "vkCreateInstance Failure");
1943 }
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001944 const VkApplicationInfo app = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001945 .sType = VK_STRUCTURE_TYPE_APPLICATION_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001946 .pNext = NULL,
Ian Elliott44e33f72015-04-28 10:52:52 -06001947 .pAppName = APP_SHORT_NAME,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001948 .appVersion = 0,
Ian Elliott44e33f72015-04-28 10:52:52 -06001949 .pEngineName = APP_SHORT_NAME,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001950 .engineVersion = 0,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001951 .apiVersion = VK_API_VERSION,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001952 };
Tony Barbour3dddd5d2015-04-29 16:19:20 -06001953 VkInstanceCreateInfo inst_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001954 .sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
Jon Ashburnab46b362015-04-04 14:52:07 -06001955 .pNext = NULL,
1956 .pAppInfo = &app,
1957 .pAllocCb = NULL,
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06001958 .extensionCount = instance_extension_count,
1959 .pEnabledExtensions = instance_extensions,
Jon Ashburnab46b362015-04-04 14:52:07 -06001960 };
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -06001961 const VkDeviceQueueCreateInfo queue = {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001962 .queueNodeIndex = 0,
1963 .queueCount = 1,
1964 };
Ian Elliott097d9f32015-04-28 11:35:02 -06001965
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06001966 VkDeviceCreateInfo device = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001967 .sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001968 .pNext = NULL,
1969 .queueRecordCount = 1,
1970 .pRequestedQueues = &queue,
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06001971 .extensionCount = device_extension_count,
1972 .pEnabledExtensions = device_extensions,
Jon Ashburn7842d522015-05-18 09:06:15 -06001973 .flags = 0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001974 };
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001975 uint32_t gpu_count;
1976 uint32_t i;
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07001977 uint32_t queue_count;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001978
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001979 err = vkCreateInstance(&inst_info, &demo->inst);
Ian Elliott07264132015-04-28 11:35:02 -06001980 if (err == VK_ERROR_INCOMPATIBLE_DRIVER) {
1981 ERR_EXIT("Cannot find a compatible Vulkan installable client driver "
Ian Elliott65152912015-04-28 13:22:33 -06001982 "(ICD).\n\nPlease look at the Getting Started guide for "
Ian Elliott07264132015-04-28 11:35:02 -06001983 "additional information.\n",
1984 "vkCreateInstance Failure");
Tony Barbour3dddd5d2015-04-29 16:19:20 -06001985 } else if (err == VK_ERROR_INVALID_EXTENSION) {
1986 ERR_EXIT("Cannot find a specified extension library"
1987 ".\nMake sure your layers path is set appropriately\n",
1988 "vkCreateInstance Failure");
Ian Elliott07264132015-04-28 11:35:02 -06001989 } else if (err) {
Ian Elliott65152912015-04-28 13:22:33 -06001990 ERR_EXIT("vkCreateInstance failed.\n\nDo you have a compatible Vulkan "
1991 "installable client driver (ICD) installed?\nPlease look at "
Ian Elliott07264132015-04-28 11:35:02 -06001992 "the Getting Started guide for additional information.\n",
1993 "vkCreateInstance Failure");
Ian Elliott3979e282015-04-03 15:24:55 -06001994 }
Jon Ashburnab46b362015-04-04 14:52:07 -06001995
Tony Barbour3dddd5d2015-04-29 16:19:20 -06001996
Jon Ashburn07c0c0c2015-04-15 11:31:12 -06001997 gpu_count = 1;
1998 err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, &demo->gpu);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001999 assert(!err && gpu_count == 1);
2000
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002001
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002002 if (demo->validate) {
Tony Barbourd70b42c2015-06-30 14:14:19 -06002003 demo->dbgCreateMsgCallback = vkGetInstanceProcAddr(demo->inst, "vkDbgCreateMsgCallback");
2004 demo->dbgDestroyMsgCallback = vkGetInstanceProcAddr(demo->inst, "vkDbgDestroyMsgCallback");
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002005 if (!demo->dbgCreateMsgCallback) {
2006 ERR_EXIT("GetProcAddr: Unable to find vkDbgCreateMsgCallback\n",
2007 "vkGetProcAddr Failure");
2008 }
Tony Barbourd70b42c2015-06-30 14:14:19 -06002009 if (!demo->dbgDestroyMsgCallback) {
2010 ERR_EXIT("GetProcAddr: Unable to find vkDbgDestroyMsgCallback\n",
2011 "vkGetProcAddr Failure");
2012 }
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002013 err = demo->dbgCreateMsgCallback(
2014 demo->inst,
2015 VK_DBG_REPORT_ERROR_BIT | VK_DBG_REPORT_WARN_BIT,
2016 dbgFunc, NULL,
2017 &demo->msg_callback);
2018 switch (err) {
2019 case VK_SUCCESS:
2020 break;
2021 case VK_ERROR_INVALID_POINTER:
2022 ERR_EXIT("dbgCreateMsgCallback: Invalid pointer\n",
2023 "dbgCreateMsgCallback Failure");
2024 break;
2025 case VK_ERROR_OUT_OF_HOST_MEMORY:
2026 ERR_EXIT("dbgCreateMsgCallback: out of host memory\n",
2027 "dbgCreateMsgCallback Failure");
2028 break;
2029 default:
2030 ERR_EXIT("dbgCreateMsgCallback: unknown failure\n",
2031 "dbgCreateMsgCallback Failure");
2032 break;
2033 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002034 }
2035
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002036 err = vkCreateDevice(demo->gpu, &device, &demo->device);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002037 assert(!err);
2038
Ian Elliott673898b2015-06-22 15:07:49 -06002039 GET_DEVICE_PROC_ADDR(demo->device, CreateSwapChainWSI);
2040 GET_DEVICE_PROC_ADDR(demo->device, CreateSwapChainWSI);
2041 GET_DEVICE_PROC_ADDR(demo->device, DestroySwapChainWSI);
2042 GET_DEVICE_PROC_ADDR(demo->device, GetSwapChainInfoWSI);
2043 GET_DEVICE_PROC_ADDR(demo->device, QueuePresentWSI);
Jon Ashburn0b85d052015-05-21 18:13:33 -06002044
Tony Barbourecf1b2b2015-06-24 16:06:58 -06002045 err = vkGetPhysicalDeviceProperties(demo->gpu, &demo->gpu_props);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06002046 assert(!err);
2047
Tony Barbourecf1b2b2015-06-24 16:06:58 -06002048 err = vkGetPhysicalDeviceQueueCount(demo->gpu, &queue_count);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06002049 assert(!err);
2050
Tony Barbourecf1b2b2015-06-24 16:06:58 -06002051 demo->queue_props = (VkPhysicalDeviceQueueProperties *) malloc(queue_count * sizeof(VkPhysicalDeviceQueueProperties));
2052 err = vkGetPhysicalDeviceQueueProperties(demo->gpu, queue_count, demo->queue_props);
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07002053 assert(!err);
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07002054 assert(queue_count >= 1);
2055
Mark Lobodzinskida9b1092015-04-16 11:44:05 -05002056 // Graphics queue and MemMgr queue can be separate.
2057 // TODO: Add support for separate queues, including synchronization,
Mark Lobodzinski6baaec72015-05-11 17:21:15 -05002058 // and appropriate tracking for QueueSubmit
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07002059 for (i = 0; i < queue_count; i++) {
Mark Lobodzinski6baaec72015-05-11 17:21:15 -05002060 if (demo->queue_props[i].queueFlags & VK_QUEUE_GRAPHICS_BIT)
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07002061 break;
2062 }
2063 assert(i < queue_count);
2064 demo->graphics_queue_node_index = i;
2065
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002066 err = vkGetDeviceQueue(demo->device, demo->graphics_queue_node_index,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002067 0, &demo->queue);
2068 assert(!err);
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002069
Jon Ashburnd7130cb2015-06-16 12:44:51 -06002070 // for now hardcode format till get WSI support
2071 demo->format = VK_FORMAT_B8G8R8A8_UNORM;
Ian Elliotte4602cd2015-04-21 16:41:02 -06002072
David Pinedo2bb7c932015-06-18 17:03:14 -06002073 demo->quit = false;
2074 demo->curFrame = 0;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06002075
2076 // Get Memory information and properties
2077 err = vkGetPhysicalDeviceMemoryProperties(demo->gpu, &demo->memory_properties);
2078 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002079}
2080
2081static void demo_init_connection(struct demo *demo)
2082{
Ian Elliott639ca472015-04-16 15:23:05 -06002083#ifndef _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002084 const xcb_setup_t *setup;
2085 xcb_screen_iterator_t iter;
2086 int scr;
2087
2088 demo->connection = xcb_connect(NULL, &scr);
Ian Elliott3979e282015-04-03 15:24:55 -06002089 if (demo->connection == NULL) {
2090 printf("Cannot find a compatible Vulkan installable client driver "
2091 "(ICD).\nExiting ...\n");
2092 fflush(stdout);
2093 exit(1);
2094 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002095
2096 setup = xcb_get_setup(demo->connection);
2097 iter = xcb_setup_roots_iterator(setup);
2098 while (scr-- > 0)
2099 xcb_screen_next(&iter);
2100
2101 demo->screen = iter.data;
Ian Elliott639ca472015-04-16 15:23:05 -06002102#endif // _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002103}
2104
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002105static void demo_init(struct demo *demo, int argc, char **argv)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002106{
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002107 vec3 eye = {0.0f, 3.0f, 5.0f};
2108 vec3 origin = {0, 0, 0};
Chia-I Wuae3b55d2015-04-22 14:56:17 +08002109 vec3 up = {0.0f, 1.0f, 0.0};
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002110
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002111 memset(demo, 0, sizeof(*demo));
David Pinedo2bb7c932015-06-18 17:03:14 -06002112 demo->frameCount = INT_MAX;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002113
Piers Daniell735ee532015-02-23 16:23:13 -07002114 for (int i = 1; i < argc; i++) {
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002115 if (strcmp(argv[i], "--use_staging") == 0) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002116 demo->use_staging_buffer = true;
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002117 continue;
Ian Elliott639ca472015-04-16 15:23:05 -06002118 }
Cody Northrop1fedb212015-05-28 11:27:16 -06002119 if (strcmp(argv[i], "--use_glsl") == 0) {
2120 demo->use_glsl = true;
2121 continue;
2122 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002123 if (strcmp(argv[i], "--validate") == 0) {
2124 demo->validate = true;
2125 continue;
2126 }
David Pinedo2bb7c932015-06-18 17:03:14 -06002127 if (strcmp(argv[i], "--c") == 0 &&
2128 demo->frameCount == INT_MAX &&
2129 i < argc-1 &&
2130 sscanf(argv[i+1],"%d", &demo->frameCount) == 1 &&
2131 demo->frameCount >= 0)
2132 {
2133 i++;
2134 continue;
2135 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002136
David Pinedo2bb7c932015-06-18 17:03:14 -06002137 fprintf(stderr, "Usage:\n %s [--use_staging] [--validate] [--c <framecount>]\n", APP_SHORT_NAME);
Ian Elliott639ca472015-04-16 15:23:05 -06002138 fflush(stderr);
2139 exit(1);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002140 }
2141
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002142 demo_init_connection(demo);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002143 demo_init_vk(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002144
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002145 demo->width = 500;
2146 demo->height = 500;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002147
2148 demo->spin_angle = 0.01f;
2149 demo->spin_increment = 0.01f;
2150 demo->pause = false;
2151
Piers Daniell735ee532015-02-23 16:23:13 -07002152 mat4x4_perspective(demo->projection_matrix, (float)degreesToRadians(45.0f), 1.0f, 0.1f, 100.0f);
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002153 mat4x4_look_at(demo->view_matrix, eye, origin, up);
2154 mat4x4_identity(demo->model_matrix);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002155}
2156
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002157
Ian Elliott639ca472015-04-16 15:23:05 -06002158#ifdef _WIN32
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002159extern int __getmainargs(
2160 int * _Argc,
2161 char *** _Argv,
2162 char *** _Env,
2163 int _DoWildCard,
2164 int * new_mode);
Ian Elliottf9cf78c2015-04-28 10:33:11 -06002165
Ian Elliotta748eaf2015-04-28 15:50:36 -06002166int WINAPI WinMain(HINSTANCE hInstance,
2167 HINSTANCE hPrevInstance,
2168 LPSTR pCmdLine,
2169 int nCmdShow)
Ian Elliott639ca472015-04-16 15:23:05 -06002170{
2171 MSG msg; // message
2172 bool done; // flag saying when app is complete
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002173 int argc;
2174 char** argv;
2175 char** env;
2176 int new_mode = 0;
Ian Elliott639ca472015-04-16 15:23:05 -06002177
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002178 __getmainargs(&argc,&argv,&env,0,&new_mode);
2179
2180 demo_init(&demo, argc, argv);
2181 demo.connection = hInstance;
2182 strncpy(demo.name, "cube", APP_NAME_STR_LEN);
Ian Elliott639ca472015-04-16 15:23:05 -06002183 demo_create_window(&demo);
2184
2185 demo_prepare(&demo);
2186
2187 done = false; //initialize loop condition variable
2188 /* main message loop*/
2189 while(!done)
2190 {
Ian Elliotta748eaf2015-04-28 15:50:36 -06002191 PeekMessage(&msg, NULL, 0, 0, PM_REMOVE);
Ian Elliott639ca472015-04-16 15:23:05 -06002192 if (msg.message == WM_QUIT) //check for a quit message
2193 {
2194 done = true; //if found, quit app
2195 }
2196 else
2197 {
2198 /* Translate and dispatch to event queue*/
2199 TranslateMessage(&msg);
2200 DispatchMessage(&msg);
2201 }
2202 }
2203
2204 demo_cleanup(&demo);
2205
Tony Barbourf9921732015-04-22 11:36:22 -06002206 return (int) msg.wParam;
Ian Elliott639ca472015-04-16 15:23:05 -06002207}
2208#else // _WIN32
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002209int main(int argc, char **argv)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002210{
2211 struct demo demo;
2212
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002213 demo_init(&demo, argc, argv);
Chia-I Wucbb564e2015-04-16 22:02:10 +08002214 demo_create_window(&demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002215
2216 demo_prepare(&demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002217 demo_run(&demo);
2218
2219 demo_cleanup(&demo);
2220
2221 return 0;
2222}
Ian Elliott639ca472015-04-16 15:23:05 -06002223#endif // _WIN32