blob: 3a4c41797b0e9a6e72125942375fc86cc080a0c2 [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) {
265 sprintf(message,"WARNING: [%s] Code %d : %s", pLayerPrefix, msgCode, pMsg);
Tony Barbour3dddd5d2015-04-29 16:19:20 -0600266 } else {
267 return;
268 }
269
270#ifdef _WIN32
271 MessageBox(NULL, message, "Alert", MB_OK);
272#else
273 printf("%s\n",message);
274 fflush(stdout);
275#endif
276 free(message);
277}
278
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600279struct demo {
Ian Elliott639ca472015-04-16 15:23:05 -0600280#ifdef _WIN32
281#define APP_NAME_STR_LEN 80
282 HINSTANCE connection; // hInstance - Windows Instance
283 char name[APP_NAME_STR_LEN]; // Name to put on the window/icon
284 HWND window; // hWnd - window handle
285#else // _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600286 xcb_connection_t *connection;
287 xcb_screen_t *screen;
Chia-I Wucbb564e2015-04-16 22:02:10 +0800288 xcb_window_t window;
289 xcb_intern_atom_reply_t *atom_wm_delete_window;
Ian Elliott639ca472015-04-16 15:23:05 -0600290#endif // _WIN32
Cody Northrop1fedb212015-05-28 11:27:16 -0600291 bool prepared;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700292 bool use_staging_buffer;
Cody Northrop1fedb212015-05-28 11:27:16 -0600293 bool use_glsl;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600294
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600295 VkInstance inst;
Tony Barbour72304ef2015-04-16 15:59:00 -0600296 VkPhysicalDevice gpu;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600297 VkDevice device;
298 VkQueue queue;
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -0700299 uint32_t graphics_queue_node_index;
Tony Barbour72304ef2015-04-16 15:59:00 -0600300 VkPhysicalDeviceProperties *gpu_props;
301 VkPhysicalDeviceQueueProperties *queue_props;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600302
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600303 VkFramebuffer framebuffer;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600304 int width, height;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600305 VkFormat format;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600306
Jon Ashburn0b85d052015-05-21 18:13:33 -0600307 PFN_vkCreateSwapChainWSI fpCreateSwapChainWSI;
308 PFN_vkDestroySwapChainWSI fpDestroySwapChainWSI;
309 PFN_vkGetSwapChainInfoWSI fpGetSwapChainInfoWSI;
310 PFN_vkQueuePresentWSI fpQueuePresentWSI;
Chia-I Wucbb564e2015-04-16 22:02:10 +0800311 VkSwapChainWSI swap_chain;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600312 struct {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600313 VkImage image;
Tony Barbour72304ef2015-04-16 15:59:00 -0600314 VkDeviceMemory mem;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600315 VkCmdBuffer cmd;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600316
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600317 VkColorAttachmentView view;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600318 } buffers[DEMO_BUFFER_COUNT];
319
320 struct {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600321 VkFormat format;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600322
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600323 VkImage image;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500324 VkDeviceMemory mem;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600325 VkDepthStencilView view;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600326 } depth;
327
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600328 struct texture_object textures[DEMO_TEXTURE_COUNT];
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600329
330 struct {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600331 VkBuffer buf;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500332 VkDeviceMemory mem;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600333 VkBufferView view;
Chia-I Wuae721ba2015-05-25 16:27:55 +0800334 VkDescriptorInfo desc;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600335 } uniform_data;
336
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600337 VkCmdBuffer cmd; // Buffer for initialization commands
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -0500338 VkPipelineLayout pipeline_layout;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600339 VkDescriptorSetLayout desc_layout;
340 VkPipeline pipeline;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600341
Courtney Goeltzenleuchter5e6d1e92015-04-10 16:24:50 -0600342 VkDynamicVpState viewport;
343 VkDynamicRsState raster;
344 VkDynamicCbState color_blend;
345 VkDynamicDsState depth_stencil;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600346
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600347 mat4x4 projection_matrix;
348 mat4x4 view_matrix;
349 mat4x4 model_matrix;
350
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600351 float spin_angle;
352 float spin_increment;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600353 bool pause;
354
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600355 VkDescriptorPool desc_pool;
356 VkDescriptorSet desc_set;
Chia-I Wu6a3c8972015-01-04 16:27:24 +0800357
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600358 bool quit;
David Pinedo2bb7c932015-06-18 17:03:14 -0600359 int32_t curFrame;
360 int32_t frameCount;
Tony Barbour3dddd5d2015-04-29 16:19:20 -0600361 bool validate;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -0600362 PFN_vkDbgCreateMsgCallback dbgCreateMsgCallback;
363 VkDbgMsgCallback msg_callback;
364
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600365 uint32_t current_buffer;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600366};
367
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600368static void demo_flush_init_cmd(struct demo *demo)
369{
Tony Barbourfdc2d352015-04-22 09:02:32 -0600370 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600371
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600372 if (demo->cmd == VK_NULL_HANDLE)
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600373 return;
374
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600375 err = vkEndCommandBuffer(demo->cmd);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600376 assert(!err);
377
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600378 const VkCmdBuffer cmd_bufs[] = { demo->cmd };
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600379
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600380 err = vkQueueSubmit(demo->queue, 1, cmd_bufs, VK_NULL_HANDLE);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600381 assert(!err);
382
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600383 err = vkQueueWaitIdle(demo->queue);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600384 assert(!err);
385
Mike Stroyanebae8322015-04-17 12:36:38 -0600386 vkDestroyObject(demo->device, VK_OBJECT_TYPE_COMMAND_BUFFER, demo->cmd);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600387 demo->cmd = VK_NULL_HANDLE;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600388}
389
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600390static void demo_set_image_layout(
391 struct demo *demo,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600392 VkImage image,
malnasseeb25d3f2015-06-03 17:28:38 -0400393 VkImageAspect aspect,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600394 VkImageLayout old_image_layout,
395 VkImageLayout new_image_layout)
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600396{
Tony Barbourfdc2d352015-04-22 09:02:32 -0600397 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600398
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600399 if (demo->cmd == VK_NULL_HANDLE) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600400 const VkCmdBufferCreateInfo cmd = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600401 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600402 .pNext = NULL,
403 .queueNodeIndex = demo->graphics_queue_node_index,
404 .flags = 0,
405 };
406
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600407 err = vkCreateCommandBuffer(demo->device, &cmd, &demo->cmd);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600408 assert(!err);
409
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600410 VkCmdBufferBeginInfo cmd_buf_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600411 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600412 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -0600413 .flags = VK_CMD_BUFFER_OPTIMIZE_SMALL_BATCH_BIT |
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600414 VK_CMD_BUFFER_OPTIMIZE_ONE_TIME_SUBMIT_BIT,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600415 };
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600416 err = vkBeginCommandBuffer(demo->cmd, &cmd_buf_info);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600417 }
418
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600419 VkImageMemoryBarrier image_memory_barrier = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600420 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600421 .pNext = NULL,
422 .outputMask = 0,
423 .inputMask = 0,
424 .oldLayout = old_image_layout,
425 .newLayout = new_image_layout,
426 .image = image,
malnasseeb25d3f2015-06-03 17:28:38 -0400427 .subresourceRange = { aspect, 0, 1, 0, 0 }
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600428 };
429
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600430 if (new_image_layout == VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL) {
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600431 /* Make sure anything that was copying from this image has completed */
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -0600432 image_memory_barrier.inputMask = VK_MEMORY_INPUT_TRANSFER_BIT;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600433 }
434
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600435 if (new_image_layout == VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL) {
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600436 /* Make sure any Copy or CPU writes to image are flushed */
Courtney Goeltzenleuchter2bda8332015-04-29 17:16:21 -0600437 image_memory_barrier.outputMask = VK_MEMORY_OUTPUT_HOST_WRITE_BIT | VK_MEMORY_OUTPUT_TRANSFER_BIT;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600438 }
439
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600440 VkImageMemoryBarrier *pmemory_barrier = &image_memory_barrier;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600441
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600442 VkPipeEvent set_events[] = { VK_PIPE_EVENT_TOP_OF_PIPE };
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600443
Tony Barbour72304ef2015-04-16 15:59:00 -0600444 vkCmdPipelineBarrier(demo->cmd, VK_WAIT_EVENT_TOP_OF_PIPE, 1, set_events, 1, (const void **)&pmemory_barrier);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600445}
446
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600447static void demo_draw_build_cmd(struct demo *demo, VkCmdBuffer cmd_buf)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600448{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600449 const VkColorAttachmentBindInfo color_attachment = {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600450 .view = demo->buffers[demo->current_buffer].view,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600451 .layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600452 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600453 const VkDepthStencilBindInfo depth_stencil = {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600454 .view = demo->depth.view,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600455 .layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600456 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600457 const VkClearColor clear_color = {
Courtney Goeltzenleuchter374553c2015-04-03 16:35:32 -0600458 .color.floatColor = { 0.2f, 0.2f, 0.2f, 0.2f },
459 .useRawValue = false,
460 };
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600461 const float clear_depth = 1.0f;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600462 VkImageSubresourceRange clear_range;
463 VkCmdBufferBeginInfo cmd_buf_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600464 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO,
Courtney Goeltzenleuchter53968d82015-04-03 15:25:24 -0600465 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -0600466 .flags = VK_CMD_BUFFER_OPTIMIZE_SMALL_BATCH_BIT |
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600467 VK_CMD_BUFFER_OPTIMIZE_ONE_TIME_SUBMIT_BIT,
Jon Ashburn60ccfbe2014-12-31 17:08:35 -0700468 };
Tony Barbourfdc2d352015-04-22 09:02:32 -0600469 VkResult U_ASSERT_ONLY err;
Chris Forbes40a71562015-06-17 11:36:12 +1200470 VkAttachmentLoadOp load_op = VK_ATTACHMENT_LOAD_OP_CLEAR;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600471 VkAttachmentStoreOp store_op = VK_ATTACHMENT_STORE_OP_DONT_CARE;
472 const VkFramebufferCreateInfo fb_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600473 .sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO,
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700474 .pNext = NULL,
475 .colorAttachmentCount = 1,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600476 .pColorAttachments = (VkColorAttachmentBindInfo*) &color_attachment,
477 .pDepthStencilAttachment = (VkDepthStencilBindInfo*) &depth_stencil,
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700478 .sampleCount = 1,
Mark Lobodzinskic06b7412015-01-27 13:24:03 -0600479 .width = demo->width,
480 .height = demo->height,
481 .layers = 1,
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700482 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600483 VkRenderPassCreateInfo rp_info;
484 VkRenderPassBegin rp_begin;
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700485
486 memset(&rp_info, 0 , sizeof(rp_info));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600487 err = vkCreateFramebuffer(demo->device, &fb_info, &rp_begin.framebuffer);
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700488 assert(!err);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600489 rp_info.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO;
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700490 rp_info.renderArea.extent.width = demo->width;
491 rp_info.renderArea.extent.height = demo->height;
Courtney Goeltzenleuchter53968d82015-04-03 15:25:24 -0600492 rp_info.colorAttachmentCount = fb_info.colorAttachmentCount;
493 rp_info.pColorFormats = &demo->format;
494 rp_info.pColorLayouts = &color_attachment.layout;
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700495 rp_info.pColorLoadOps = &load_op;
496 rp_info.pColorStoreOps = &store_op;
Courtney Goeltzenleuchter53968d82015-04-03 15:25:24 -0600497 rp_info.pColorLoadClearValues = &clear_color;
Tony Barbour72304ef2015-04-16 15:59:00 -0600498 rp_info.depthStencilFormat = VK_FORMAT_D16_UNORM;
Courtney Goeltzenleuchter53968d82015-04-03 15:25:24 -0600499 rp_info.depthStencilLayout = depth_stencil.layout;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600500 rp_info.depthLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
Courtney Goeltzenleuchter53968d82015-04-03 15:25:24 -0600501 rp_info.depthLoadClearValue = clear_depth;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600502 rp_info.depthStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
503 rp_info.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
Courtney Goeltzenleuchter53968d82015-04-03 15:25:24 -0600504 rp_info.stencilLoadClearValue = 0;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600505 rp_info.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
506 err = vkCreateRenderPass(demo->device, &rp_info, &rp_begin.renderPass);
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700507 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600508
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600509 err = vkBeginCommandBuffer(cmd_buf, &cmd_buf_info);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600510 assert(!err);
511
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600512 vkCmdBindPipeline(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600513 demo->pipeline);
Mark Lobodzinskic6e8b3d2015-06-15 13:21:21 -0600514 vkCmdBindDescriptorSets(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS, demo->pipeline_layout,
Cody Northropfb5185a2015-04-16 13:41:56 -0600515 0, 1, &demo->desc_set, 0, NULL);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600516
Tony Barbour72304ef2015-04-16 15:59:00 -0600517 vkCmdBindDynamicStateObject(cmd_buf, VK_STATE_BIND_POINT_VIEWPORT, demo->viewport);
518 vkCmdBindDynamicStateObject(cmd_buf, VK_STATE_BIND_POINT_RASTER, demo->raster);
519 vkCmdBindDynamicStateObject(cmd_buf, VK_STATE_BIND_POINT_COLOR_BLEND,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600520 demo->color_blend);
Tony Barbour72304ef2015-04-16 15:59:00 -0600521 vkCmdBindDynamicStateObject(cmd_buf, VK_STATE_BIND_POINT_DEPTH_STENCIL,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600522 demo->depth_stencil);
523
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600524 vkCmdBeginRenderPass(cmd_buf, &rp_begin);
Chris Forbes40a71562015-06-17 11:36:12 +1200525 clear_range.aspect = VK_IMAGE_ASPECT_DEPTH;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600526 clear_range.baseMipLevel = 0;
527 clear_range.mipLevels = 1;
528 clear_range.baseArraySlice = 0;
529 clear_range.arraySize = 1;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600530
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600531 vkCmdClearDepthStencil(cmd_buf, demo->depth.image,
532 VK_IMAGE_LAYOUT_CLEAR_OPTIMAL,
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600533 clear_depth, 0, 1, &clear_range);
534
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600535 vkCmdDraw(cmd_buf, 0, 12 * 3, 0, 1);
536 vkCmdEndRenderPass(cmd_buf, rp_begin.renderPass);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600537
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600538 err = vkEndCommandBuffer(cmd_buf);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600539 assert(!err);
Courtney Goeltzenleuchter94901dc2015-02-25 17:53:18 -0700540
Mike Stroyanebae8322015-04-17 12:36:38 -0600541 vkDestroyObject(demo->device, VK_OBJECT_TYPE_RENDER_PASS, rp_begin.renderPass);
542 vkDestroyObject(demo->device, VK_OBJECT_TYPE_FRAMEBUFFER, rp_begin.framebuffer);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600543}
544
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600545
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600546void demo_update_data_buffer(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600547{
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600548 mat4x4 MVP, Model, VP;
549 int matrixSize = sizeof(MVP);
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600550 uint8_t *pData;
Tony Barbourfdc2d352015-04-22 09:02:32 -0600551 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600552
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600553 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600554
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600555 // Rotate 22.5 degrees around the Y axis
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600556 mat4x4_dup(Model, demo->model_matrix);
Piers Daniell735ee532015-02-23 16:23:13 -0700557 mat4x4_rotate(demo->model_matrix, Model, 0.0f, 1.0f, 0.0f, (float)degreesToRadians(demo->spin_angle));
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600558 mat4x4_mul(MVP, VP, demo->model_matrix);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600559
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500560 err = vkMapMemory(demo->device, demo->uniform_data.mem, 0, 0, 0, (void **) &pData);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600561 assert(!err);
562
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600563 memcpy(pData, (const void*) &MVP[0][0], matrixSize);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600564
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500565 err = vkUnmapMemory(demo->device, demo->uniform_data.mem);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600566 assert(!err);
567}
568
569static void demo_draw(struct demo *demo)
570{
Chia-I Wucbb564e2015-04-16 22:02:10 +0800571 const VkPresentInfoWSI present = {
572 .sType = VK_STRUCTURE_TYPE_PRESENT_INFO_WSI,
573 .pNext = NULL,
574 .image = demo->buffers[demo->current_buffer].image,
575 .flipInterval = 0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600576 };
Tony Barbourfdc2d352015-04-22 09:02:32 -0600577 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600578
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600579 err = vkQueueSubmit(demo->queue, 1, &demo->buffers[demo->current_buffer].cmd,
580 VK_NULL_HANDLE);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600581 assert(!err);
582
Jon Ashburn0b85d052015-05-21 18:13:33 -0600583 err = demo->fpQueuePresentWSI(demo->queue, &present);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600584 assert(!err);
585
586 demo->current_buffer = (demo->current_buffer + 1) % DEMO_BUFFER_COUNT;
Chia-I Wucbb564e2015-04-16 22:02:10 +0800587
588 err = vkQueueWaitIdle(demo->queue);
589 assert(err == VK_SUCCESS);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600590}
591
592static void demo_prepare_buffers(struct demo *demo)
593{
Chia-I Wucbb564e2015-04-16 22:02:10 +0800594 const VkSwapChainCreateInfoWSI swap_chain = {
595 .sType = VK_STRUCTURE_TYPE_SWAP_CHAIN_CREATE_INFO_WSI,
596 .pNext = NULL,
597 .pNativeWindowSystemHandle = demo->connection,
598 .pNativeWindowHandle = (void *) (intptr_t) demo->window,
Ian Elliotte4602cd2015-04-21 16:41:02 -0600599 .displayCount = 1,
Chia-I Wucbb564e2015-04-16 22:02:10 +0800600 .imageCount = DEMO_BUFFER_COUNT,
601 .imageFormat = demo->format,
602 .imageExtent = {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600603 .width = demo->width,
604 .height = demo->height,
605 },
Chia-I Wucbb564e2015-04-16 22:02:10 +0800606 .imageArraySize = 1,
607 .imageUsageFlags = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600608 };
Chia-I Wucbb564e2015-04-16 22:02:10 +0800609 VkSwapChainImageInfoWSI images[DEMO_BUFFER_COUNT];
610 size_t images_size = sizeof(images);
Tony Barbourfdc2d352015-04-22 09:02:32 -0600611 VkResult U_ASSERT_ONLY err;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600612 uint32_t i;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600613
Jon Ashburn0b85d052015-05-21 18:13:33 -0600614 err = demo->fpCreateSwapChainWSI(demo->device, &swap_chain, &demo->swap_chain);
Chia-I Wucbb564e2015-04-16 22:02:10 +0800615 assert(!err);
616
Jon Ashburn0b85d052015-05-21 18:13:33 -0600617 err = demo->fpGetSwapChainInfoWSI(demo->swap_chain,
Chia-I Wucbb564e2015-04-16 22:02:10 +0800618 VK_SWAP_CHAIN_INFO_TYPE_PERSISTENT_IMAGES_WSI,
619 &images_size, images);
620 assert(!err && images_size == sizeof(images));
621
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600622 for (i = 0; i < DEMO_BUFFER_COUNT; i++) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600623 VkColorAttachmentViewCreateInfo color_attachment_view = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600624 .sType = VK_STRUCTURE_TYPE_COLOR_ATTACHMENT_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600625 .pNext = NULL,
626 .format = demo->format,
627 .mipLevel = 0,
628 .baseArraySlice = 0,
629 .arraySize = 1,
630 };
631
Chia-I Wucbb564e2015-04-16 22:02:10 +0800632 demo->buffers[i].image = images[i].image;
633 demo->buffers[i].mem = images[i].memory;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600634
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600635 demo_set_image_layout(demo, demo->buffers[i].image,
malnasseeb25d3f2015-06-03 17:28:38 -0400636 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600637 VK_IMAGE_LAYOUT_UNDEFINED,
638 VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600639
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600640 color_attachment_view.image = demo->buffers[i].image;
641
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600642 err = vkCreateColorAttachmentView(demo->device,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600643 &color_attachment_view, &demo->buffers[i].view);
644 assert(!err);
645 }
646}
647
648static void demo_prepare_depth(struct demo *demo)
649{
Tony Barbour72304ef2015-04-16 15:59:00 -0600650 const VkFormat depth_format = VK_FORMAT_D16_UNORM;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600651 const VkImageCreateInfo image = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600652 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600653 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -0600654 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600655 .format = depth_format,
656 .extent = { demo->width, demo->height, 1 },
657 .mipLevels = 1,
658 .arraySize = 1,
659 .samples = 1,
Tony Barbour72304ef2015-04-16 15:59:00 -0600660 .tiling = VK_IMAGE_TILING_OPTIMAL,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600661 .usage = VK_IMAGE_USAGE_DEPTH_STENCIL_BIT,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600662 .flags = 0,
663 };
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -0600664 VkMemoryAllocInfo mem_alloc = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600665 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
Mark Lobodzinskia6907f72015-04-16 08:52:00 -0500666 .pNext = NULL,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600667 .allocationSize = 0,
Tony Barbour72304ef2015-04-16 15:59:00 -0600668 .memProps = VK_MEMORY_PROPERTY_DEVICE_ONLY,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600669 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600670 VkDepthStencilViewCreateInfo view = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600671 .sType = VK_STRUCTURE_TYPE_DEPTH_STENCIL_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600672 .pNext = NULL,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600673 .image = VK_NULL_HANDLE,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600674 .mipLevel = 0,
675 .baseArraySlice = 0,
676 .arraySize = 1,
677 .flags = 0,
678 };
Mike Stroyanebae8322015-04-17 12:36:38 -0600679
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500680 VkMemoryRequirements mem_reqs;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600681 size_t mem_reqs_size = sizeof(VkMemoryRequirements);
Tony Barbourfdc2d352015-04-22 09:02:32 -0600682 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600683
684 demo->depth.format = depth_format;
685
686 /* create image */
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600687 err = vkCreateImage(demo->device, &image,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600688 &demo->depth.image);
689 assert(!err);
690
Mike Stroyanebae8322015-04-17 12:36:38 -0600691 err = vkGetObjectInfo(demo->device,
692 VK_OBJECT_TYPE_IMAGE, demo->depth.image,
Tony Barbour72304ef2015-04-16 15:59:00 -0600693 VK_OBJECT_INFO_TYPE_MEMORY_REQUIREMENTS,
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500694 &mem_reqs_size, &mem_reqs);
695 mem_alloc.allocationSize = mem_reqs.size;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600696
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500697 /* allocate memory */
698 err = vkAllocMemory(demo->device, &mem_alloc, &demo->depth.mem);
699 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600700
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500701 /* bind memory */
702 err = vkBindObjectMemory(demo->device,
703 VK_OBJECT_TYPE_IMAGE, demo->depth.image,
704 demo->depth.mem, 0);
705 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600706
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600707 demo_set_image_layout(demo, demo->depth.image,
malnasseeb25d3f2015-06-03 17:28:38 -0400708 VK_IMAGE_ASPECT_DEPTH,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600709 VK_IMAGE_LAYOUT_UNDEFINED,
710 VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600711
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600712 /* create image view */
713 view.image = demo->depth.image;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600714 err = vkCreateDepthStencilView(demo->device, &view,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600715 &demo->depth.view);
716 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600717}
718
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600719/** loadTexture
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -0600720 * loads a png file into an memory object, using cstdio , libpng.
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600721 *
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -0600722 * \param demo : Needed to access VK calls
723 * \param filename : the png file to be loaded
724 * \param width : width of png, to be updated as a side effect of this function
725 * \param height : height of png, to be updated as a side effect of this function
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600726 *
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -0600727 * \return bool : an opengl texture id. true if successful?,
728 * should be validated by the client of this function.
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600729 *
730 * Source: http://en.wikibooks.org/wiki/OpenGL_Programming/Intermediate/Textures
731 * Modified to copy image to memory
732 *
733 */
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700734bool loadTexture(const char *filename, uint8_t *rgba_data,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600735 VkSubresourceLayout *layout,
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600736 int32_t *width, int32_t *height)
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600737{
738 //header for testing if it is a png
739 png_byte header[8];
Tony Barbourf9921732015-04-22 11:36:22 -0600740 int is_png, bit_depth, color_type, rowbytes;
741 size_t retval;
Ian Elliott1e42dff2015-02-13 14:29:21 -0700742 png_uint_32 i, twidth, theight;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600743 png_structp png_ptr;
744 png_infop info_ptr, end_info;
745 png_byte *image_data;
746 png_bytep *row_pointers;
747
748 //open file as binary
749 FILE *fp = fopen(filename, "rb");
750 if (!fp) {
751 return false;
752 }
753
754 //read the header
Tony Barbourfdc2d352015-04-22 09:02:32 -0600755 retval = fread(header, 1, 8, fp);
756 if (retval != 8) {
757 fclose(fp);
758 return false;
759 }
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600760
761 //test if png
762 is_png = !png_sig_cmp(header, 0, 8);
763 if (!is_png) {
764 fclose(fp);
765 return false;
766 }
767
768 //create png struct
769 png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL,
770 NULL, NULL);
771 if (!png_ptr) {
772 fclose(fp);
773 return (false);
774 }
775
776 //create png info struct
777 info_ptr = png_create_info_struct(png_ptr);
778 if (!info_ptr) {
779 png_destroy_read_struct(&png_ptr, (png_infopp) NULL, (png_infopp) NULL);
780 fclose(fp);
781 return (false);
782 }
783
784 //create png info struct
785 end_info = png_create_info_struct(png_ptr);
786 if (!end_info) {
787 png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp) NULL);
788 fclose(fp);
789 return (false);
790 }
791
792 //png error stuff, not sure libpng man suggests this.
793 if (setjmp(png_jmpbuf(png_ptr))) {
794 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
795 fclose(fp);
796 return (false);
797 }
798
799 //init png reading
800 png_init_io(png_ptr, fp);
801
802 //let libpng know you already read the first 8 bytes
803 png_set_sig_bytes(png_ptr, 8);
804
805 // read all the info up to the image data
806 png_read_info(png_ptr, info_ptr);
807
808 // get info about png
809 png_get_IHDR(png_ptr, info_ptr, &twidth, &theight, &bit_depth, &color_type,
810 NULL, NULL, NULL);
811
812 //update width and height based on png info
813 *width = twidth;
814 *height = theight;
815
816 // Require that incoming texture be 8bits per color component
817 // and 4 components (RGBA).
818 if (png_get_bit_depth(png_ptr, info_ptr) != 8 ||
819 png_get_channels(png_ptr, info_ptr) != 4) {
820 return false;
821 }
822
823 if (rgba_data == NULL) {
824 // If data pointer is null, we just want the width & height
825 // clean up memory and close stuff
826 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
827 fclose(fp);
828
829 return true;
830 }
831
832 // Update the png info struct.
833 png_read_update_info(png_ptr, info_ptr);
834
835 // Row size in bytes.
836 rowbytes = png_get_rowbytes(png_ptr, info_ptr);
837
838 // Allocate the image_data as a big block, to be given to opengl
839 image_data = (png_byte *)malloc(rowbytes * theight * sizeof(png_byte));
840 if (!image_data) {
841 //clean up memory and close stuff
842 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
843 fclose(fp);
844 return false;
845 }
846
847 // row_pointers is for pointing to image_data for reading the png with libpng
848 row_pointers = (png_bytep *)malloc(theight * sizeof(png_bytep));
849 if (!row_pointers) {
850 //clean up memory and close stuff
851 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
852 // delete[] image_data;
853 fclose(fp);
854 return false;
855 }
856 // set the individual row_pointers to point at the correct offsets of image_data
857 for (i = 0; i < theight; ++i)
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700858 row_pointers[theight - 1 - i] = rgba_data + i * layout->rowPitch;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600859
860 // read the png into image_data through row_pointers
861 png_read_image(png_ptr, row_pointers);
862
863 // clean up memory and close stuff
864 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
865 free(row_pointers);
866 free(image_data);
867 fclose(fp);
868
869 return true;
870}
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600871
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700872static void demo_prepare_texture_image(struct demo *demo,
873 const char *filename,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600874 struct texture_object *tex_obj,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600875 VkImageTiling tiling,
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -0600876 VkImageUsageFlags usage,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600877 VkFlags mem_props)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600878{
Mike Stroyan8b89e072015-06-15 14:21:03 -0600879 const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600880 int32_t tex_width;
881 int32_t tex_height;
Tony Barbourfdc2d352015-04-22 09:02:32 -0600882 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700883
David Pinedo30bd71d2015-04-23 08:16:57 -0600884 if (!loadTexture(filename, NULL, NULL, &tex_width, &tex_height))
885 {
886 printf("Failed to load textures\n");
887 fflush(stdout);
888 exit(1);
889 }
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700890
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600891 tex_obj->tex_width = tex_width;
892 tex_obj->tex_height = tex_height;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700893
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600894 const VkImageCreateInfo image_create_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600895 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700896 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -0600897 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700898 .format = tex_format,
899 .extent = { tex_width, tex_height, 1 },
900 .mipLevels = 1,
901 .arraySize = 1,
902 .samples = 1,
903 .tiling = tiling,
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -0600904 .usage = usage,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700905 .flags = 0,
906 };
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -0600907 VkMemoryAllocInfo mem_alloc = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600908 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
Mark Lobodzinskia6907f72015-04-16 08:52:00 -0500909 .pNext = NULL,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700910 .allocationSize = 0,
911 .memProps = mem_props,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700912 };
913
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500914 VkMemoryRequirements mem_reqs;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600915 size_t mem_reqs_size = sizeof(VkMemoryRequirements);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700916
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600917 err = vkCreateImage(demo->device, &image_create_info,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600918 &tex_obj->image);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700919 assert(!err);
920
Mike Stroyanebae8322015-04-17 12:36:38 -0600921 err = vkGetObjectInfo(demo->device,
922 VK_OBJECT_TYPE_IMAGE, tex_obj->image,
Tony Barbour72304ef2015-04-16 15:59:00 -0600923 VK_OBJECT_INFO_TYPE_MEMORY_REQUIREMENTS,
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500924 &mem_reqs_size, &mem_reqs);
925 assert(!err && mem_reqs_size == sizeof(VkMemoryRequirements));
Piers Daniell735ee532015-02-23 16:23:13 -0700926
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500927 mem_alloc.allocationSize = mem_reqs.size;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700928
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500929 /* allocate memory */
930 err = vkAllocMemory(demo->device, &mem_alloc,
931 &(tex_obj->mem));
932 assert(!err);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700933
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500934 /* bind memory */
935 err = vkBindObjectMemory(demo->device,
936 VK_OBJECT_TYPE_IMAGE, tex_obj->image,
937 tex_obj->mem, 0);
938 assert(!err);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700939
Tony Barbour72304ef2015-04-16 15:59:00 -0600940 if (mem_props & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600941 const VkImageSubresource subres = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600942 .aspect = VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700943 .mipLevel = 0,
944 .arraySlice = 0,
945 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600946 VkSubresourceLayout layout;
947 size_t layout_size = sizeof(VkSubresourceLayout);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700948 void *data;
949
Mike Stroyanebae8322015-04-17 12:36:38 -0600950 err = vkGetImageSubresourceInfo(demo->device, tex_obj->image, &subres,
Tony Barbour72304ef2015-04-16 15:59:00 -0600951 VK_SUBRESOURCE_INFO_TYPE_LAYOUT,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700952 &layout_size, &layout);
953 assert(!err && layout_size == sizeof(layout));
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700954
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500955 err = vkMapMemory(demo->device, tex_obj->mem, 0, 0, 0, &data);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700956 assert(!err);
957
958 if (!loadTexture(filename, data, &layout, &tex_width, &tex_height)) {
959 fprintf(stderr, "Error loading texture: %s\n", filename);
960 }
961
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500962 err = vkUnmapMemory(demo->device, tex_obj->mem);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700963 assert(!err);
964 }
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600965
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600966 tex_obj->imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600967 demo_set_image_layout(demo, tex_obj->image,
malnasseeb25d3f2015-06-03 17:28:38 -0400968 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600969 VK_IMAGE_LAYOUT_UNDEFINED,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600970 tex_obj->imageLayout);
971 /* 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 -0700972}
973
Mark Lobodzinskida9b1092015-04-16 11:44:05 -0500974static void demo_destroy_texture_image(struct demo *demo, struct texture_object *tex_objs)
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700975{
976 /* clean up staging resources */
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500977 vkFreeMemory(demo->device, tex_objs->mem);
Mike Stroyanebae8322015-04-17 12:36:38 -0600978 vkDestroyObject(demo->device, VK_OBJECT_TYPE_IMAGE, tex_objs->image);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700979}
980
981static void demo_prepare_textures(struct demo *demo)
982{
Tony Barbour72304ef2015-04-16 15:59:00 -0600983 const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600984 VkFormatProperties props;
Tony Barbourfdc2d352015-04-22 09:02:32 -0600985 VkResult U_ASSERT_ONLY err;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600986 uint32_t i;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600987
Chris Forbesf576a3e2015-06-21 22:55:02 +1200988 err = vkGetPhysicalDeviceFormatInfo(demo->gpu, tex_format, &props);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700989 assert(!err);
990
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600991 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700992
FslNopper6a7e50e2015-05-06 21:42:01 +0200993 if ((props.linearTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) && !demo->use_staging_buffer) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700994 /* Device can texture using linear textures */
995 demo_prepare_texture_image(demo, tex_files[i], &demo->textures[i],
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -0600996 VK_IMAGE_TILING_LINEAR, VK_IMAGE_USAGE_SAMPLED_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
Tony Barbour72304ef2015-04-16 15:59:00 -0600997 } else if (props.optimalTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700998 /* Must use staging buffer to copy linear texture to optimized */
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600999 struct texture_object staging_texture;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001000
1001 memset(&staging_texture, 0, sizeof(staging_texture));
1002 demo_prepare_texture_image(demo, tex_files[i], &staging_texture,
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001003 VK_IMAGE_TILING_LINEAR, VK_IMAGE_USAGE_TRANSFER_SOURCE_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001004
1005 demo_prepare_texture_image(demo, tex_files[i], &demo->textures[i],
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001006 VK_IMAGE_TILING_OPTIMAL,
1007 (VK_IMAGE_USAGE_TRANSFER_DESTINATION_BIT | VK_IMAGE_USAGE_SAMPLED_BIT),
1008 VK_MEMORY_PROPERTY_DEVICE_ONLY);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001009
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001010 demo_set_image_layout(demo, staging_texture.image,
malnasseeb25d3f2015-06-03 17:28:38 -04001011 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001012 staging_texture.imageLayout,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001013 VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001014
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001015 demo_set_image_layout(demo, demo->textures[i].image,
malnasseeb25d3f2015-06-03 17:28:38 -04001016 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001017 demo->textures[i].imageLayout,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001018 VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001019
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001020 VkImageCopy copy_region = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001021 .srcSubresource = { VK_IMAGE_ASPECT_COLOR, 0, 0 },
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001022 .srcOffset = { 0, 0, 0 },
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001023 .destSubresource = { VK_IMAGE_ASPECT_COLOR, 0, 0 },
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001024 .destOffset = { 0, 0, 0 },
1025 .extent = { staging_texture.tex_width, staging_texture.tex_height, 1 },
1026 };
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001027 vkCmdCopyImage(demo->cmd,
1028 staging_texture.image, VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL,
1029 demo->textures[i].image, VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL,
Courtney Goeltzenleuchter8e89a312015-03-25 11:25:10 -06001030 1, &copy_region);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001031
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001032 demo_set_image_layout(demo, demo->textures[i].image,
malnasseeb25d3f2015-06-03 17:28:38 -04001033 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001034 VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001035 demo->textures[i].imageLayout);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001036
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001037 demo_flush_init_cmd(demo);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001038
Courtney Goeltzenleuchterf3aeb2b2015-04-21 09:30:03 -06001039 demo_destroy_texture_image(demo, &staging_texture);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001040 } else {
Mike Stroyan8b89e072015-06-15 14:21:03 -06001041 /* Can't support VK_FORMAT_R8G8B8A8_UNORM !? */
1042 assert(!"No support for R8G8B8A8_UNORM as texture image format");
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001043 }
1044
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001045 const VkSamplerCreateInfo sampler = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001046 .sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001047 .pNext = NULL,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001048 .magFilter = VK_TEX_FILTER_NEAREST,
1049 .minFilter = VK_TEX_FILTER_NEAREST,
Tony Barbour72304ef2015-04-16 15:59:00 -06001050 .mipMode = VK_TEX_MIPMAP_MODE_BASE,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001051 .addressU = VK_TEX_ADDRESS_CLAMP,
1052 .addressV = VK_TEX_ADDRESS_CLAMP,
1053 .addressW = VK_TEX_ADDRESS_CLAMP,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001054 .mipLodBias = 0.0f,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001055 .maxAnisotropy = 1,
Tony Barbour72304ef2015-04-16 15:59:00 -06001056 .compareOp = VK_COMPARE_OP_NEVER,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001057 .minLod = 0.0f,
1058 .maxLod = 0.0f,
Tony Barbour72304ef2015-04-16 15:59:00 -06001059 .borderColor = VK_BORDER_COLOR_OPAQUE_WHITE,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001060 };
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001061
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001062 VkImageViewCreateInfo view = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001063 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001064 .pNext = NULL,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001065 .image = VK_NULL_HANDLE,
Tony Barbour72304ef2015-04-16 15:59:00 -06001066 .viewType = VK_IMAGE_VIEW_TYPE_2D,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001067 .format = tex_format,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001068 .channels = { VK_CHANNEL_SWIZZLE_R,
1069 VK_CHANNEL_SWIZZLE_G,
1070 VK_CHANNEL_SWIZZLE_B,
1071 VK_CHANNEL_SWIZZLE_A, },
1072 .subresourceRange = { VK_IMAGE_ASPECT_COLOR, 0, 1, 0, 1 },
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001073 .minLod = 0.0f,
1074 };
Jon Ashburnb2a66652015-01-16 09:37:43 -07001075
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001076 /* create sampler */
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001077 err = vkCreateSampler(demo->device, &sampler,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001078 &demo->textures[i].sampler);
1079 assert(!err);
1080
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001081 /* create image view */
1082 view.image = demo->textures[i].image;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001083 err = vkCreateImageView(demo->device, &view,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001084 &demo->textures[i].view);
1085 assert(!err);
1086 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001087}
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001088
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001089void demo_prepare_cube_data_buffer(struct demo *demo)
1090{
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -06001091 VkBufferCreateInfo buf_info;
1092 VkBufferViewCreateInfo view_info;
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -06001093 VkMemoryAllocInfo alloc_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001094 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
Mark Lobodzinskia6907f72015-04-16 08:52:00 -05001095 .pNext = NULL,
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001096 .allocationSize = 0,
Tony Barbour72304ef2015-04-16 15:59:00 -06001097 .memProps = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT,
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001098 };
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001099 VkMemoryRequirements mem_reqs;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001100 size_t mem_reqs_size = sizeof(VkMemoryRequirements);
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001101 uint8_t *pData;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001102 int i;
1103 mat4x4 MVP, VP;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001104 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001105 struct vktexcube_vs_uniform data;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001106
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001107 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001108 mat4x4_mul(MVP, VP, demo->model_matrix);
1109 memcpy(data.mvp, MVP, sizeof(MVP));
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001110// dumpMatrix("MVP", MVP);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001111
1112 for (i=0; i<12*3; i++) {
1113 data.position[i][0] = g_vertex_buffer_data[i*3];
1114 data.position[i][1] = g_vertex_buffer_data[i*3+1];
1115 data.position[i][2] = g_vertex_buffer_data[i*3+2];
1116 data.position[i][3] = 1.0f;
1117 data.attr[i][0] = g_uv_buffer_data[2*i];
1118 data.attr[i][1] = g_uv_buffer_data[2*i + 1];
1119 data.attr[i][2] = 0;
1120 data.attr[i][3] = 0;
1121 }
1122
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001123 memset(&buf_info, 0, sizeof(buf_info));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001124 buf_info.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001125 buf_info.size = sizeof(data);
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -06001126 buf_info.usage = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001127 err = vkCreateBuffer(demo->device, &buf_info, &demo->uniform_data.buf);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001128 assert(!err);
1129
Mike Stroyanebae8322015-04-17 12:36:38 -06001130 err = vkGetObjectInfo(demo->device,
Mike Stroyanebae8322015-04-17 12:36:38 -06001131 VK_OBJECT_TYPE_BUFFER, demo->uniform_data.buf,
Tony Barbour72304ef2015-04-16 15:59:00 -06001132 VK_OBJECT_INFO_TYPE_MEMORY_REQUIREMENTS,
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001133 &mem_reqs_size, &mem_reqs);
1134 assert(!err && mem_reqs_size == sizeof(mem_reqs));
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001135
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001136 alloc_info.allocationSize = mem_reqs.size;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001137
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001138 err = vkAllocMemory(demo->device, &alloc_info, &(demo->uniform_data.mem));
1139 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001140
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001141 err = vkMapMemory(demo->device, demo->uniform_data.mem, 0, 0, 0, (void **) &pData);
1142 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001143
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001144 memcpy(pData, &data, sizeof data);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001145
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001146 err = vkUnmapMemory(demo->device, demo->uniform_data.mem);
1147 assert(!err);
1148
1149 err = vkBindObjectMemory(demo->device,
1150 VK_OBJECT_TYPE_BUFFER, demo->uniform_data.buf,
1151 demo->uniform_data.mem, 0);
1152 assert(!err);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001153
1154 memset(&view_info, 0, sizeof(view_info));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001155 view_info.sType = VK_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO;
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001156 view_info.buffer = demo->uniform_data.buf;
Tony Barbour72304ef2015-04-16 15:59:00 -06001157 view_info.viewType = VK_BUFFER_VIEW_TYPE_RAW;
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001158 view_info.offset = 0;
1159 view_info.range = sizeof(data);
1160
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001161 err = vkCreateBufferView(demo->device, &view_info, &demo->uniform_data.view);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001162 assert(!err);
1163
Chia-I Wuae721ba2015-05-25 16:27:55 +08001164 demo->uniform_data.desc.bufferView = demo->uniform_data.view;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001165}
1166
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001167static void demo_prepare_descriptor_layout(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001168{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001169 const VkDescriptorSetLayoutBinding layout_bindings[2] = {
Chia-I Wua2aa8632015-03-26 15:04:41 +08001170 [0] = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001171 .descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
Chia-I Wu6d29a412015-05-25 16:22:52 +08001172 .arraySize = 1,
Tony Barbour72304ef2015-04-16 15:59:00 -06001173 .stageFlags = VK_SHADER_STAGE_VERTEX_BIT,
Chia-I Wu91e8e212015-03-27 12:56:09 +08001174 .pImmutableSamplers = NULL,
Chia-I Wua2aa8632015-03-26 15:04:41 +08001175 },
1176 [1] = {
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -06001177 .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
Chia-I Wu6d29a412015-05-25 16:22:52 +08001178 .arraySize = DEMO_TEXTURE_COUNT,
Tony Barbour72304ef2015-04-16 15:59:00 -06001179 .stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT,
Chia-I Wu91e8e212015-03-27 12:56:09 +08001180 .pImmutableSamplers = NULL,
Chia-I Wua2aa8632015-03-26 15:04:41 +08001181 },
1182 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001183 const VkDescriptorSetLayoutCreateInfo descriptor_layout = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001184 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001185 .pNext = NULL,
Chia-I Wua2aa8632015-03-26 15:04:41 +08001186 .count = 2,
1187 .pBinding = layout_bindings,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001188 };
Tony Barbourfdc2d352015-04-22 09:02:32 -06001189 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001190
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001191 err = vkCreateDescriptorSetLayout(demo->device,
Chia-I Wub58c24a2015-03-26 15:27:55 +08001192 &descriptor_layout, &demo->desc_layout);
1193 assert(!err);
1194
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001195 const VkPipelineLayoutCreateInfo pPipelineLayoutCreateInfo = {
1196 .sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
1197 .pNext = NULL,
1198 .descriptorSetCount = 1,
1199 .pSetLayouts = &demo->desc_layout,
1200 };
1201
1202 err = vkCreatePipelineLayout(demo->device,
1203 &pPipelineLayoutCreateInfo,
1204 &demo->pipeline_layout);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001205 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001206}
1207
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001208static VkShader demo_prepare_shader(struct demo *demo,
Tony Barbour72304ef2015-04-16 15:59:00 -06001209 VkShaderStage stage,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001210 const void *code,
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001211 size_t size)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001212{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001213 VkShaderCreateInfo createInfo;
1214 VkShader shader;
1215 VkResult err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001216
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001217
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001218 createInfo.sType = VK_STRUCTURE_TYPE_SHADER_CREATE_INFO;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001219 createInfo.pNext = NULL;
1220
Cody Northrop1fedb212015-05-28 11:27:16 -06001221 if (!demo->use_glsl) {
1222 createInfo.codeSize = size;
1223 createInfo.pCode = code;
1224 createInfo.flags = 0;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001225
Cody Northrop1fedb212015-05-28 11:27:16 -06001226 err = vkCreateShader(demo->device, &createInfo, &shader);
1227 if (err) {
1228 free((void *) createInfo.pCode);
1229 }
1230 } else {
1231 // Create fake SPV structure to feed GLSL
1232 // to the driver "under the covers"
1233 createInfo.codeSize = 3 * sizeof(uint32_t) + size + 1;
1234 createInfo.pCode = malloc(createInfo.codeSize);
1235 createInfo.flags = 0;
1236
1237 /* try version 0 first: VkShaderStage followed by GLSL */
1238 ((uint32_t *) createInfo.pCode)[0] = ICD_SPV_MAGIC;
1239 ((uint32_t *) createInfo.pCode)[1] = 0;
1240 ((uint32_t *) createInfo.pCode)[2] = stage;
1241 memcpy(((uint32_t *) createInfo.pCode + 3), code, size + 1);
1242
1243 err = vkCreateShader(demo->device, &createInfo, &shader);
1244 if (err) {
1245 free((void *) createInfo.pCode);
1246 return (VkShader) VK_NULL_HANDLE;
1247 }
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001248 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001249 return shader;
1250}
1251
Cody Northrop8b12a1f2015-03-17 15:55:58 -06001252char *demo_read_spv(const char *filename, size_t *psize)
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001253{
1254 long int size;
Tony Barbourf9921732015-04-22 11:36:22 -06001255 size_t U_ASSERT_ONLY retval;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001256 void *shader_code;
1257
1258 FILE *fp = fopen(filename, "rb");
1259 if (!fp) return NULL;
1260
1261 fseek(fp, 0L, SEEK_END);
1262 size = ftell(fp);
1263
1264 fseek(fp, 0L, SEEK_SET);
1265
1266 shader_code = malloc(size);
Tony Barbourfdc2d352015-04-22 09:02:32 -06001267 retval = fread(shader_code, size, 1, fp);
1268 assert(retval == 1);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001269
1270 *psize = size;
1271
1272 return shader_code;
1273}
1274
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001275static VkShader demo_prepare_vs(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001276{
Cody Northrop1fedb212015-05-28 11:27:16 -06001277 if (!demo->use_glsl) {
1278 void *vertShaderCode;
1279 size_t size;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001280
Cody Northrop1fedb212015-05-28 11:27:16 -06001281 vertShaderCode = demo_read_spv("cube-vert.spv", &size);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001282
Cody Northrop1fedb212015-05-28 11:27:16 -06001283 return demo_prepare_shader(demo, VK_SHADER_STAGE_VERTEX,
1284 vertShaderCode, size);
1285 } else {
1286 static const char *vertShaderText =
1287 "#version 140\n"
1288 "#extension GL_ARB_separate_shader_objects : enable\n"
1289 "#extension GL_ARB_shading_language_420pack : enable\n"
1290 "\n"
1291 "layout(binding = 0) uniform buf {\n"
1292 " mat4 MVP;\n"
1293 " vec4 position[12*3];\n"
1294 " vec4 attr[12*3];\n"
1295 "} ubuf;\n"
1296 "\n"
1297 "layout (location = 0) out vec4 texcoord;\n"
1298 "\n"
1299 "void main() \n"
1300 "{\n"
1301 " texcoord = ubuf.attr[gl_VertexID];\n"
1302 " gl_Position = ubuf.MVP * ubuf.position[gl_VertexID];\n"
1303 "\n"
1304 " // GL->VK conventions\n"
1305 " gl_Position.y = -gl_Position.y;\n"
1306 " gl_Position.z = (gl_Position.z + gl_Position.w) / 2.0;\n"
1307 "}\n";
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001308
Cody Northrop1fedb212015-05-28 11:27:16 -06001309 return demo_prepare_shader(demo, VK_SHADER_STAGE_VERTEX,
1310 (const void *) vertShaderText,
1311 strlen(vertShaderText));
1312 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001313}
1314
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001315static VkShader demo_prepare_fs(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001316{
Cody Northrop1fedb212015-05-28 11:27:16 -06001317 if (!demo->use_glsl) {
1318 void *fragShaderCode;
1319 size_t size;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001320
Cody Northrop1fedb212015-05-28 11:27:16 -06001321 fragShaderCode = demo_read_spv("cube-frag.spv", &size);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001322
Cody Northrop1fedb212015-05-28 11:27:16 -06001323 return demo_prepare_shader(demo, VK_SHADER_STAGE_FRAGMENT,
1324 fragShaderCode, size);
1325 } else {
1326 static const char *fragShaderText =
1327 "#version 140\n"
1328 "#extension GL_ARB_separate_shader_objects : enable\n"
1329 "#extension GL_ARB_shading_language_420pack : enable\n"
1330 "layout (binding = 1) uniform sampler2D tex;\n"
1331 "\n"
1332 "layout (location = 0) in vec4 texcoord;\n"
1333 "layout (location = 0) out vec4 uFragColor;\n"
1334 "void main() {\n"
1335 " uFragColor = texture(tex, texcoord.xy);\n"
1336 "}\n";
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001337
Cody Northrop1fedb212015-05-28 11:27:16 -06001338 return demo_prepare_shader(demo, VK_SHADER_STAGE_FRAGMENT,
1339 (const void *) fragShaderText,
1340 strlen(fragShaderText));
1341 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001342}
1343
1344static void demo_prepare_pipeline(struct demo *demo)
1345{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001346 VkGraphicsPipelineCreateInfo pipeline;
Mark Lobodzinski521d7762015-06-23 15:11:57 -06001347
1348 VkPipelineVertexInputStateCreateInfo vi;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001349 VkPipelineIaStateCreateInfo ia;
1350 VkPipelineRsStateCreateInfo rs;
1351 VkPipelineCbStateCreateInfo cb;
1352 VkPipelineDsStateCreateInfo ds;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001353 VkPipelineVpStateCreateInfo vp;
1354 VkPipelineMsStateCreateInfo ms;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001355 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001356
Mark Lobodzinski521d7762015-06-23 15:11:57 -06001357 memset(&vi, 0, sizeof(vi));
1358
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001359 memset(&pipeline, 0, sizeof(pipeline));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001360 pipeline.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001361 pipeline.layout = demo->pipeline_layout;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001362
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001363 memset(&ia, 0, sizeof(ia));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001364 ia.sType = VK_STRUCTURE_TYPE_PIPELINE_IA_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06001365 ia.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001366
1367 memset(&rs, 0, sizeof(rs));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001368 rs.sType = VK_STRUCTURE_TYPE_PIPELINE_RS_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06001369 rs.fillMode = VK_FILL_MODE_SOLID;
1370 rs.cullMode = VK_CULL_MODE_BACK;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001371 rs.frontFace = VK_FRONT_FACE_CCW;
Chia-I Wubb67e6e2015-04-22 14:20:52 +08001372 rs.depthClipEnable = VK_TRUE;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001373
1374 memset(&cb, 0, sizeof(cb));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001375 cb.sType = VK_STRUCTURE_TYPE_PIPELINE_CB_STATE_CREATE_INFO;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001376 VkPipelineCbAttachmentState att_state[1];
Tony Barbour29645d02015-01-16 14:27:35 -07001377 memset(att_state, 0, sizeof(att_state));
1378 att_state[0].format = demo->format;
1379 att_state[0].channelWriteMask = 0xf;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001380 att_state[0].blendEnable = VK_FALSE;
Tony Barbour29645d02015-01-16 14:27:35 -07001381 cb.attachmentCount = 1;
1382 cb.pAttachments = att_state;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001383
Tony Barbour29645d02015-01-16 14:27:35 -07001384 memset(&vp, 0, sizeof(vp));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001385 vp.sType = VK_STRUCTURE_TYPE_PIPELINE_VP_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06001386 vp.viewportCount = 1;
Tony Barbour29645d02015-01-16 14:27:35 -07001387
1388 memset(&ds, 0, sizeof(ds));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001389 ds.sType = VK_STRUCTURE_TYPE_PIPELINE_DS_STATE_CREATE_INFO;
Tony Barbour29645d02015-01-16 14:27:35 -07001390 ds.format = demo->depth.format;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001391 ds.depthTestEnable = VK_TRUE;
1392 ds.depthWriteEnable = VK_TRUE;
Tony Barbour72304ef2015-04-16 15:59:00 -06001393 ds.depthCompareOp = VK_COMPARE_OP_LESS_EQUAL;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001394 ds.depthBoundsEnable = VK_FALSE;
1395 ds.back.stencilFailOp = VK_STENCIL_OP_KEEP;
1396 ds.back.stencilPassOp = VK_STENCIL_OP_KEEP;
Tony Barbour72304ef2015-04-16 15:59:00 -06001397 ds.back.stencilCompareOp = VK_COMPARE_OP_ALWAYS;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001398 ds.stencilTestEnable = VK_FALSE;
Tony Barbour29645d02015-01-16 14:27:35 -07001399 ds.front = ds.back;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001400
Tony Barbour29645d02015-01-16 14:27:35 -07001401 memset(&ms, 0, sizeof(ms));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001402 ms.sType = VK_STRUCTURE_TYPE_PIPELINE_MS_STATE_CREATE_INFO;
Tony Barbour29645d02015-01-16 14:27:35 -07001403 ms.sampleMask = 1;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001404 ms.multisampleEnable = VK_FALSE;
Tony Barbour29645d02015-01-16 14:27:35 -07001405 ms.samples = 1;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001406
Mark Lobodzinski521d7762015-06-23 15:11:57 -06001407 // Two stages: vs and fs
1408 pipeline.stageCount = 2;
1409 VkPipelineShaderStageCreateInfo shaderStages[2];
1410 memset(&shaderStages, 0, 2 * sizeof(VkPipelineShaderStageCreateInfo));
1411
1412 shaderStages[0].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1413 shaderStages[0].stage = VK_SHADER_STAGE_VERTEX;
1414 shaderStages[0].shader = demo_prepare_vs(demo);
1415
1416 shaderStages[1].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1417 shaderStages[1].stage = VK_SHADER_STAGE_FRAGMENT;
1418 shaderStages[1].shader = demo_prepare_fs(demo);
1419
1420 pipeline.pVertexInputState = &vi;
1421 pipeline.pIaState = &ia;
1422 pipeline.pRsState = &rs;
1423 pipeline.pCbState = &cb;
1424 pipeline.pMsState = &ms;
1425 pipeline.pVpState = &vp;
1426 pipeline.pDsState = &ds;
1427 pipeline.pStages = shaderStages;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001428
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001429 err = vkCreateGraphicsPipeline(demo->device, &pipeline, &demo->pipeline);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001430 assert(!err);
1431
Mark Lobodzinski521d7762015-06-23 15:11:57 -06001432 for (uint32_t i = 0; i < pipeline.stageCount; i++) {
1433 vkDestroyObject(demo->device, VK_OBJECT_TYPE_SHADER, shaderStages[i].shader);
1434 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001435}
1436
1437static void demo_prepare_dynamic_states(struct demo *demo)
1438{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001439 VkDynamicVpStateCreateInfo viewport_create;
1440 VkDynamicRsStateCreateInfo raster;
1441 VkDynamicCbStateCreateInfo color_blend;
1442 VkDynamicDsStateCreateInfo depth_stencil;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001443 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001444
Tony Barbour29645d02015-01-16 14:27:35 -07001445 memset(&viewport_create, 0, sizeof(viewport_create));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001446 viewport_create.sType = VK_STRUCTURE_TYPE_DYNAMIC_VP_STATE_CREATE_INFO;
Courtney Goeltzenleuchter1710d8d2015-02-11 14:13:34 -07001447 viewport_create.viewportAndScissorCount = 1;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001448 VkViewport viewport;
Piers Daniell735ee532015-02-23 16:23:13 -07001449 memset(&viewport, 0, sizeof(viewport));
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001450 viewport.height = (float) demo->height;
1451 viewport.width = (float) demo->width;
1452 viewport.minDepth = (float) 0.0f;
1453 viewport.maxDepth = (float) 1.0f;
Piers Daniell735ee532015-02-23 16:23:13 -07001454 viewport_create.pViewports = &viewport;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001455 VkRect scissor;
Piers Daniell735ee532015-02-23 16:23:13 -07001456 memset(&scissor, 0, sizeof(scissor));
Courtney Goeltzenleuchter1710d8d2015-02-11 14:13:34 -07001457 scissor.extent.width = demo->width;
1458 scissor.extent.height = demo->height;
1459 scissor.offset.x = 0;
1460 scissor.offset.y = 0;
Courtney Goeltzenleuchter1710d8d2015-02-11 14:13:34 -07001461 viewport_create.pScissors = &scissor;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001462
1463 memset(&raster, 0, sizeof(raster));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001464 raster.sType = VK_STRUCTURE_TYPE_DYNAMIC_RS_STATE_CREATE_INFO;
Piers Daniell735ee532015-02-23 16:23:13 -07001465 raster.lineWidth = 1.0;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001466
1467 memset(&color_blend, 0, sizeof(color_blend));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001468 color_blend.sType = VK_STRUCTURE_TYPE_DYNAMIC_CB_STATE_CREATE_INFO;
Piers Daniell735ee532015-02-23 16:23:13 -07001469 color_blend.blendConst[0] = 1.0f;
1470 color_blend.blendConst[1] = 1.0f;
1471 color_blend.blendConst[2] = 1.0f;
1472 color_blend.blendConst[3] = 1.0f;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001473
1474 memset(&depth_stencil, 0, sizeof(depth_stencil));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001475 depth_stencil.sType = VK_STRUCTURE_TYPE_DYNAMIC_DS_STATE_CREATE_INFO;
Mark Lobodzinski89d32b12015-06-12 11:14:17 -06001476 depth_stencil.minDepthBounds = 0.0f;
1477 depth_stencil.maxDepthBounds = 1.0f;
Tony Barbour29645d02015-01-16 14:27:35 -07001478 depth_stencil.stencilBackRef = 0;
1479 depth_stencil.stencilFrontRef = 0;
1480 depth_stencil.stencilReadMask = 0xff;
1481 depth_stencil.stencilWriteMask = 0xff;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001482
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001483 err = vkCreateDynamicViewportState(demo->device, &viewport_create, &demo->viewport);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001484 assert(!err);
1485
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001486 err = vkCreateDynamicRasterState(demo->device, &raster, &demo->raster);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001487 assert(!err);
1488
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001489 err = vkCreateDynamicColorBlendState(demo->device,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001490 &color_blend, &demo->color_blend);
1491 assert(!err);
1492
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001493 err = vkCreateDynamicDepthStencilState(demo->device,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001494 &depth_stencil, &demo->depth_stencil);
1495 assert(!err);
1496}
1497
Chia-I Wu63ea9262015-03-26 13:14:16 +08001498static void demo_prepare_descriptor_pool(struct demo *demo)
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001499{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001500 const VkDescriptorTypeCount type_counts[2] = {
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001501 [0] = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001502 .type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001503 .count = 1,
1504 },
1505 [1] = {
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -06001506 .type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001507 .count = DEMO_TEXTURE_COUNT,
1508 },
1509 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001510 const VkDescriptorPoolCreateInfo descriptor_pool = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001511 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001512 .pNext = NULL,
1513 .count = 2,
1514 .pTypeCount = type_counts,
1515 };
Tony Barbourfdc2d352015-04-22 09:02:32 -06001516 VkResult U_ASSERT_ONLY err;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001517
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001518 err = vkCreateDescriptorPool(demo->device,
1519 VK_DESCRIPTOR_POOL_USAGE_ONE_SHOT, 1,
Chia-I Wu63ea9262015-03-26 13:14:16 +08001520 &descriptor_pool, &demo->desc_pool);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001521 assert(!err);
1522}
1523
1524static void demo_prepare_descriptor_set(struct demo *demo)
1525{
Chia-I Wuae721ba2015-05-25 16:27:55 +08001526 VkDescriptorInfo tex_descs[DEMO_TEXTURE_COUNT];
1527 VkWriteDescriptorSet writes[2];
Tony Barbourfdc2d352015-04-22 09:02:32 -06001528 VkResult U_ASSERT_ONLY err;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001529 uint32_t count;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001530 uint32_t i;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001531
Mike Stroyanebae8322015-04-17 12:36:38 -06001532 err = vkAllocDescriptorSets(demo->device, demo->desc_pool,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001533 VK_DESCRIPTOR_SET_USAGE_STATIC,
Chia-I Wu87544e72015-02-23 10:41:08 -07001534 1, &demo->desc_layout,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001535 &demo->desc_set, &count);
1536 assert(!err && count == 1);
1537
Chia-I Wuae721ba2015-05-25 16:27:55 +08001538 memset(&tex_descs, 0, sizeof(tex_descs));
1539 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
1540 tex_descs[i].sampler = demo->textures[i].sampler;
1541 tex_descs[i].imageView = demo->textures[i].view;
1542 tex_descs[i].imageLayout = VK_IMAGE_LAYOUT_GENERAL;
1543 }
1544
1545 memset(&writes, 0, sizeof(writes));
1546
1547 writes[0].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
1548 writes[0].destSet = demo->desc_set;
1549 writes[0].count = 1;
1550 writes[0].descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
1551 writes[0].pDescriptors = &demo->uniform_data.desc;
1552
1553 writes[1].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
1554 writes[1].destSet = demo->desc_set;
1555 writes[1].destBinding = 1;
1556 writes[1].count = DEMO_TEXTURE_COUNT;
1557 writes[1].descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
1558 writes[1].pDescriptors = tex_descs;
1559
1560 err = vkUpdateDescriptorSets(demo->device, 2, writes, 0, NULL);
1561 assert(!err);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001562}
1563
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001564static void demo_prepare(struct demo *demo)
1565{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001566 const VkCmdBufferCreateInfo cmd = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001567 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001568 .pNext = NULL,
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07001569 .queueNodeIndex = demo->graphics_queue_node_index,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001570 .flags = 0,
1571 };
Tony Barbourfdc2d352015-04-22 09:02:32 -06001572 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001573
1574 demo_prepare_buffers(demo);
1575 demo_prepare_depth(demo);
1576 demo_prepare_textures(demo);
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001577 demo_prepare_cube_data_buffer(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001578
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001579 demo_prepare_descriptor_layout(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001580 demo_prepare_pipeline(demo);
1581 demo_prepare_dynamic_states(demo);
1582
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001583 for (int i = 0; i < DEMO_BUFFER_COUNT; i++) {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001584 err = vkCreateCommandBuffer(demo->device, &cmd, &demo->buffers[i].cmd);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001585 assert(!err);
1586 }
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001587
Chia-I Wu63ea9262015-03-26 13:14:16 +08001588 demo_prepare_descriptor_pool(demo);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001589 demo_prepare_descriptor_set(demo);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001590
1591
1592 for (int i = 0; i < DEMO_BUFFER_COUNT; i++) {
1593 demo->current_buffer = i;
1594 demo_draw_build_cmd(demo, demo->buffers[i].cmd);
1595 }
1596
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001597 /*
1598 * Prepare functions above may generate pipeline commands
1599 * that need to be flushed before beginning the render loop.
1600 */
1601 demo_flush_init_cmd(demo);
1602
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001603 demo->current_buffer = 0;
Jon Ashburn8d26c062015-04-24 09:46:24 -07001604 demo->prepared = true;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001605}
1606
David Pinedo2bb7c932015-06-18 17:03:14 -06001607static void demo_cleanup(struct demo *demo)
1608{
1609 uint32_t i;
1610
1611 demo->prepared = false;
1612
1613 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DESCRIPTOR_SET, demo->desc_set);
1614 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DESCRIPTOR_POOL, demo->desc_pool);
1615
1616 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DYNAMIC_VP_STATE, demo->viewport);
1617 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DYNAMIC_RS_STATE, demo->raster);
1618 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DYNAMIC_CB_STATE, demo->color_blend);
1619 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DYNAMIC_DS_STATE, demo->depth_stencil);
1620
1621 vkDestroyObject(demo->device, VK_OBJECT_TYPE_PIPELINE, demo->pipeline);
1622 vkDestroyObject(demo->device, VK_OBJECT_TYPE_PIPELINE_LAYOUT, demo->pipeline_layout);
1623 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DESCRIPTOR_SET_LAYOUT, demo->desc_layout);
1624
1625 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
1626 vkDestroyObject(demo->device, VK_OBJECT_TYPE_IMAGE_VIEW, demo->textures[i].view);
1627 vkDestroyObject(demo->device, VK_OBJECT_TYPE_IMAGE, demo->textures[i].image);
1628 vkFreeMemory(demo->device, demo->textures[i].mem);
1629 vkDestroyObject(demo->device, VK_OBJECT_TYPE_SAMPLER, demo->textures[i].sampler);
1630 }
1631 demo->fpDestroySwapChainWSI(demo->swap_chain);
1632
1633 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DEPTH_STENCIL_VIEW, demo->depth.view);
1634 vkDestroyObject(demo->device, VK_OBJECT_TYPE_IMAGE, demo->depth.image);
1635 vkFreeMemory(demo->device, demo->depth.mem);
1636
1637 vkDestroyObject(demo->device, VK_OBJECT_TYPE_BUFFER_VIEW, demo->uniform_data.view);
1638 vkDestroyObject(demo->device, VK_OBJECT_TYPE_BUFFER, demo->uniform_data.buf);
1639 vkFreeMemory(demo->device, demo->uniform_data.mem);
1640
1641 for (i = 0; i < DEMO_BUFFER_COUNT; i++) {
1642 vkDestroyObject(demo->device, VK_OBJECT_TYPE_COLOR_ATTACHMENT_VIEW, demo->buffers[i].view);
1643 vkDestroyObject(demo->device, VK_OBJECT_TYPE_COMMAND_BUFFER, demo->buffers[i].cmd);
1644 }
1645
1646 vkDestroyDevice(demo->device);
1647 vkDestroyInstance(demo->inst);
1648
1649#ifndef _WIN32
1650 xcb_destroy_window(demo->connection, demo->window);
1651 xcb_disconnect(demo->connection);
1652#endif // _WIN32
1653}
1654
1655// On MS-Windows, make this a global, so it's available to WndProc()
1656struct demo demo;
1657
Ian Elliott639ca472015-04-16 15:23:05 -06001658#ifdef _WIN32
1659static void demo_run(struct demo *demo)
1660{
Courtney Goeltzenleuchterb7e22702015-04-27 14:56:34 -06001661 if (!demo->prepared)
1662 return;
Ian Elliott639ca472015-04-16 15:23:05 -06001663 // Wait for work to finish before updating MVP.
1664 vkDeviceWaitIdle(demo->device);
1665 demo_update_data_buffer(demo);
1666
1667 demo_draw(demo);
1668
1669 // Wait for work to finish before updating MVP.
1670 vkDeviceWaitIdle(demo->device);
Ian Elliott639ca472015-04-16 15:23:05 -06001671
David Pinedo2bb7c932015-06-18 17:03:14 -06001672 demo->curFrame++;
1673
1674 if (demo->frameCount != INT_MAX && demo->curFrame == demo->frameCount)
1675 {
1676 demo->quit=true;
1677 demo_cleanup(demo);
1678 ExitProcess(0);
1679 }
1680
1681}
Ian Elliott639ca472015-04-16 15:23:05 -06001682
1683// MS-Windows event handling function:
1684LRESULT CALLBACK WndProc(HWND hWnd,
1685 UINT uMsg,
1686 WPARAM wParam,
1687 LPARAM lParam)
1688{
Ian Elliott639ca472015-04-16 15:23:05 -06001689 switch(uMsg)
1690 {
Ian Elliott639ca472015-04-16 15:23:05 -06001691 case WM_CLOSE:
1692 PostQuitMessage(0);
Tony Barbour3dddd5d2015-04-29 16:19:20 -06001693 break;
Ian Elliott639ca472015-04-16 15:23:05 -06001694 case WM_PAINT:
1695 demo_run(&demo);
1696 return 0;
1697 default:
1698 break;
1699 }
1700 return (DefWindowProc(hWnd, uMsg, wParam, lParam));
1701}
1702
1703static void demo_create_window(struct demo *demo)
1704{
1705 WNDCLASSEX win_class;
1706
1707 // Initialize the window class structure:
1708 win_class.cbSize = sizeof(WNDCLASSEX);
1709 win_class.style = CS_HREDRAW | CS_VREDRAW;
1710 win_class.lpfnWndProc = WndProc;
1711 win_class.cbClsExtra = 0;
1712 win_class.cbWndExtra = 0;
1713 win_class.hInstance = demo->connection; // hInstance
1714 win_class.hIcon = LoadIcon(NULL, IDI_APPLICATION);
1715 win_class.hCursor = LoadCursor(NULL, IDC_ARROW);
1716 win_class.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
1717 win_class.lpszMenuName = NULL;
1718 win_class.lpszClassName = demo->name;
1719 win_class.hIconSm = LoadIcon(NULL, IDI_WINLOGO);
1720 // Register window class:
1721 if (!RegisterClassEx(&win_class)) {
1722 // It didn't work, so try to give a useful error:
1723 printf("Unexpected error trying to start the application!\n");
1724 fflush(stdout);
1725 exit(1);
1726 }
1727 // Create window with the registered class:
Mike Stroyanb46779e2015-06-15 14:20:13 -06001728 RECT wr = { 0, 0, demo->width, demo->height };
1729 AdjustWindowRect(&wr, WS_OVERLAPPEDWINDOW, FALSE);
Ian Elliott639ca472015-04-16 15:23:05 -06001730 demo->window = CreateWindowEx(0,
1731 demo->name, // class name
1732 demo->name, // app name
1733 WS_OVERLAPPEDWINDOW | // window style
1734 WS_VISIBLE |
1735 WS_SYSMENU,
1736 100,100, // x/y coords
Mike Stroyanb46779e2015-06-15 14:20:13 -06001737 wr.right-wr.left, // width
1738 wr.bottom-wr.top, // height
Ian Elliott639ca472015-04-16 15:23:05 -06001739 NULL, // handle to parent
1740 NULL, // handle to menu
1741 demo->connection, // hInstance
1742 NULL); // no extra parameters
1743 if (!demo->window) {
1744 // It didn't work, so try to give a useful error:
1745 printf("Cannot create a window in which to draw!\n");
1746 fflush(stdout);
1747 exit(1);
1748 }
1749}
1750#else // _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001751static void demo_handle_event(struct demo *demo,
1752 const xcb_generic_event_t *event)
1753{
Piers Daniell735ee532015-02-23 16:23:13 -07001754 uint8_t event_code = event->response_type & 0x7f;
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07001755 switch (event_code) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001756 case XCB_EXPOSE:
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07001757 // TODO: Resize window
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001758 break;
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07001759 case XCB_CLIENT_MESSAGE:
1760 if((*(xcb_client_message_event_t*)event).data.data32[0] ==
1761 (*demo->atom_wm_delete_window).atom) {
1762 demo->quit = true;
1763 }
1764 break;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001765 case XCB_KEY_RELEASE:
1766 {
1767 const xcb_key_release_event_t *key =
1768 (const xcb_key_release_event_t *) event;
1769
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001770 switch (key->detail) {
1771 case 0x9: // Escape
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001772 demo->quit = true;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001773 break;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001774 case 0x71: // left arrow key
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001775 demo->spin_angle += demo->spin_increment;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001776 break;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001777 case 0x72: // right arrow key
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001778 demo->spin_angle -= demo->spin_increment;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001779 break;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001780 case 0x41:
1781 demo->pause = !demo->pause;
Piers Daniell735ee532015-02-23 16:23:13 -07001782 break;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001783 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001784 }
1785 break;
1786 default:
1787 break;
1788 }
1789}
1790
1791static void demo_run(struct demo *demo)
1792{
1793 xcb_flush(demo->connection);
1794
1795 while (!demo->quit) {
1796 xcb_generic_event_t *event;
1797
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07001798 if (demo->pause) {
1799 event = xcb_wait_for_event(demo->connection);
1800 } else {
1801 event = xcb_poll_for_event(demo->connection);
1802 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001803 if (event) {
1804 demo_handle_event(demo, event);
1805 free(event);
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07001806 }
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07001807
1808 // Wait for work to finish before updating MVP.
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001809 vkDeviceWaitIdle(demo->device);
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07001810 demo_update_data_buffer(demo);
1811
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07001812 demo_draw(demo);
Courtney Goeltzenleuchter1454f3c2014-11-18 11:28:09 -07001813
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07001814 // Wait for work to finish before updating MVP.
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001815 vkDeviceWaitIdle(demo->device);
David Pinedo2bb7c932015-06-18 17:03:14 -06001816 demo->curFrame++;
1817 if (demo->frameCount != INT_MAX && demo->curFrame == demo->frameCount)
1818 demo->quit = true;
1819
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001820 }
1821}
1822
1823static void demo_create_window(struct demo *demo)
1824{
1825 uint32_t value_mask, value_list[32];
1826
1827 demo->window = xcb_generate_id(demo->connection);
1828
1829 value_mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK;
1830 value_list[0] = demo->screen->black_pixel;
1831 value_list[1] = XCB_EVENT_MASK_KEY_RELEASE |
1832 XCB_EVENT_MASK_EXPOSURE;
1833
1834 xcb_create_window(demo->connection,
1835 XCB_COPY_FROM_PARENT,
1836 demo->window, demo->screen->root,
1837 0, 0, demo->width, demo->height, 0,
1838 XCB_WINDOW_CLASS_INPUT_OUTPUT,
1839 demo->screen->root_visual,
1840 value_mask, value_list);
1841
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07001842 /* Magic code that will send notification when window is destroyed */
1843 xcb_intern_atom_cookie_t cookie = xcb_intern_atom(demo->connection, 1, 12,
1844 "WM_PROTOCOLS");
1845 xcb_intern_atom_reply_t* reply = xcb_intern_atom_reply(demo->connection, cookie, 0);
1846
1847 xcb_intern_atom_cookie_t cookie2 = xcb_intern_atom(demo->connection, 0, 16, "WM_DELETE_WINDOW");
1848 demo->atom_wm_delete_window = xcb_intern_atom_reply(demo->connection, cookie2, 0);
1849
1850 xcb_change_property(demo->connection, XCB_PROP_MODE_REPLACE,
1851 demo->window, (*reply).atom, 4, 32, 1,
1852 &(*demo->atom_wm_delete_window).atom);
1853 free(reply);
1854
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001855 xcb_map_window(demo->connection, demo->window);
David Pinedo2bb7c932015-06-18 17:03:14 -06001856
1857 // Force the x/y coordinates to 100,100 results are identical in consecutive runs
1858 const uint32_t coords[] = {100, 100};
1859 xcb_configure_window(demo->connection, demo->window,
1860 XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y, coords);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001861}
Ian Elliott639ca472015-04-16 15:23:05 -06001862#endif // _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001863
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001864static void demo_init_vk(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001865{
Tobin Ehlis99239dc2015-04-16 18:04:57 -06001866 VkResult err;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06001867 VkExtensionProperties *instance_extensions;
1868 uint32_t instance_extension_count = 0;
1869 VkExtensionProperties *device_extensions;
1870 uint32_t device_extension_count = 0;
Tobin Ehlis99239dc2015-04-16 18:04:57 -06001871 size_t extSize = sizeof(uint32_t);
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06001872 uint32_t total_extension_count = 0;
1873 err = vkGetGlobalExtensionInfo(VK_EXTENSION_INFO_TYPE_COUNT, 0, &extSize, &total_extension_count);
Tobin Ehlis99239dc2015-04-16 18:04:57 -06001874 assert(!err);
1875
1876 VkExtensionProperties extProp;
1877 extSize = sizeof(VkExtensionProperties);
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06001878 bool32_t WSIextFound = 0;
1879 instance_extensions = malloc(sizeof(VkExtensionProperties) * total_extension_count);
1880 device_extensions = malloc(sizeof(VkExtensionProperties) * total_extension_count);
1881 for (uint32_t i = 0; i < total_extension_count; i++) {
Tobin Ehlis99239dc2015-04-16 18:04:57 -06001882 err = vkGetGlobalExtensionInfo(VK_EXTENSION_INFO_TYPE_PROPERTIES, i, &extSize, &extProp);
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06001883 if (!strcmp("VK_WSI_LunarG", extProp.name)) {
1884 WSIextFound = 1;
1885 memcpy(&instance_extensions[instance_extension_count++], &extProp, sizeof(VkExtensionProperties));
1886 memcpy(&device_extensions[device_extension_count++], &extProp, sizeof(VkExtensionProperties));
1887 }
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06001888 if (!strcmp(DEBUG_REPORT_EXTENSION_NAME, extProp.name)) {
1889 memcpy(&instance_extensions[instance_extension_count++], &extProp, sizeof(VkExtensionProperties));
1890 }
Tobin Ehlis99239dc2015-04-16 18:04:57 -06001891 }
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06001892 if (!WSIextFound) {
Ian Elliott65152912015-04-28 13:22:33 -06001893 ERR_EXIT("vkGetGlobalExtensionInfo failed to find the "
1894 "\"VK_WSI_LunarG\" extension.\n\nDo you have a compatible "
1895 "Vulkan installable client driver (ICD) installed?\nPlease "
1896 "look at the Getting Started guide for additional "
1897 "information.\n",
1898 "vkCreateInstance Failure");
1899 }
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001900 const VkApplicationInfo app = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001901 .sType = VK_STRUCTURE_TYPE_APPLICATION_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001902 .pNext = NULL,
Ian Elliott44e33f72015-04-28 10:52:52 -06001903 .pAppName = APP_SHORT_NAME,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001904 .appVersion = 0,
Ian Elliott44e33f72015-04-28 10:52:52 -06001905 .pEngineName = APP_SHORT_NAME,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001906 .engineVersion = 0,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001907 .apiVersion = VK_API_VERSION,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001908 };
Tony Barbour3dddd5d2015-04-29 16:19:20 -06001909 VkInstanceCreateInfo inst_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001910 .sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
Jon Ashburnab46b362015-04-04 14:52:07 -06001911 .pNext = NULL,
1912 .pAppInfo = &app,
1913 .pAllocCb = NULL,
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06001914 .extensionCount = instance_extension_count,
1915 .pEnabledExtensions = instance_extensions,
Jon Ashburnab46b362015-04-04 14:52:07 -06001916 };
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -06001917 const VkDeviceQueueCreateInfo queue = {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001918 .queueNodeIndex = 0,
1919 .queueCount = 1,
1920 };
Ian Elliott097d9f32015-04-28 11:35:02 -06001921
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06001922 VkDeviceCreateInfo device = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001923 .sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001924 .pNext = NULL,
1925 .queueRecordCount = 1,
1926 .pRequestedQueues = &queue,
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06001927 .extensionCount = device_extension_count,
1928 .pEnabledExtensions = device_extensions,
Jon Ashburn7842d522015-05-18 09:06:15 -06001929 .flags = 0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001930 };
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001931 uint32_t gpu_count;
1932 uint32_t i;
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07001933 size_t data_size;
1934 uint32_t queue_count;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001935
Tony Barbour3dddd5d2015-04-29 16:19:20 -06001936 if (demo->validate) {
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06001937 inst_info.extensionCount = 3;
1938 device.extensionCount = 3;
Tony Barbour3dddd5d2015-04-29 16:19:20 -06001939 }
1940
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001941 err = vkCreateInstance(&inst_info, &demo->inst);
Ian Elliott07264132015-04-28 11:35:02 -06001942 if (err == VK_ERROR_INCOMPATIBLE_DRIVER) {
1943 ERR_EXIT("Cannot find a compatible Vulkan installable client driver "
Ian Elliott65152912015-04-28 13:22:33 -06001944 "(ICD).\n\nPlease look at the Getting Started guide for "
Ian Elliott07264132015-04-28 11:35:02 -06001945 "additional information.\n",
1946 "vkCreateInstance Failure");
Tony Barbour3dddd5d2015-04-29 16:19:20 -06001947 } else if (err == VK_ERROR_INVALID_EXTENSION) {
1948 ERR_EXIT("Cannot find a specified extension library"
1949 ".\nMake sure your layers path is set appropriately\n",
1950 "vkCreateInstance Failure");
Ian Elliott07264132015-04-28 11:35:02 -06001951 } else if (err) {
Ian Elliott65152912015-04-28 13:22:33 -06001952 ERR_EXIT("vkCreateInstance failed.\n\nDo you have a compatible Vulkan "
1953 "installable client driver (ICD) installed?\nPlease look at "
Ian Elliott07264132015-04-28 11:35:02 -06001954 "the Getting Started guide for additional information.\n",
1955 "vkCreateInstance Failure");
Ian Elliott3979e282015-04-03 15:24:55 -06001956 }
Jon Ashburnab46b362015-04-04 14:52:07 -06001957
Tony Barbour3dddd5d2015-04-29 16:19:20 -06001958
Jon Ashburn07c0c0c2015-04-15 11:31:12 -06001959 gpu_count = 1;
1960 err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, &demo->gpu);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001961 assert(!err && gpu_count == 1);
1962
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06001963
Tony Barbour3dddd5d2015-04-29 16:19:20 -06001964 if (demo->validate) {
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06001965 demo->dbgCreateMsgCallback = vkGetInstanceProcAddr((VkPhysicalDevice) NULL, "vkDbgCreateMsgCallback");
1966 if (!demo->dbgCreateMsgCallback) {
1967 ERR_EXIT("GetProcAddr: Unable to find vkDbgCreateMsgCallback\n",
1968 "vkGetProcAddr Failure");
1969 }
1970 err = demo->dbgCreateMsgCallback(
1971 demo->inst,
1972 VK_DBG_REPORT_ERROR_BIT | VK_DBG_REPORT_WARN_BIT,
1973 dbgFunc, NULL,
1974 &demo->msg_callback);
1975 switch (err) {
1976 case VK_SUCCESS:
1977 break;
1978 case VK_ERROR_INVALID_POINTER:
1979 ERR_EXIT("dbgCreateMsgCallback: Invalid pointer\n",
1980 "dbgCreateMsgCallback Failure");
1981 break;
1982 case VK_ERROR_OUT_OF_HOST_MEMORY:
1983 ERR_EXIT("dbgCreateMsgCallback: out of host memory\n",
1984 "dbgCreateMsgCallback Failure");
1985 break;
1986 default:
1987 ERR_EXIT("dbgCreateMsgCallback: unknown failure\n",
1988 "dbgCreateMsgCallback Failure");
1989 break;
1990 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06001991 }
1992
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001993 err = vkCreateDevice(demo->gpu, &device, &demo->device);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001994 assert(!err);
1995
Ian Elliott673898b2015-06-22 15:07:49 -06001996 GET_DEVICE_PROC_ADDR(demo->device, CreateSwapChainWSI);
1997 GET_DEVICE_PROC_ADDR(demo->device, CreateSwapChainWSI);
1998 GET_DEVICE_PROC_ADDR(demo->device, DestroySwapChainWSI);
1999 GET_DEVICE_PROC_ADDR(demo->device, GetSwapChainInfoWSI);
2000 GET_DEVICE_PROC_ADDR(demo->device, QueuePresentWSI);
Jon Ashburn0b85d052015-05-21 18:13:33 -06002001
Tony Barbour72304ef2015-04-16 15:59:00 -06002002 err = vkGetPhysicalDeviceInfo(demo->gpu, VK_PHYSICAL_DEVICE_INFO_TYPE_PROPERTIES,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06002003 &data_size, NULL);
2004 assert(!err);
2005
Tony Barbour72304ef2015-04-16 15:59:00 -06002006 demo->gpu_props = (VkPhysicalDeviceProperties *) malloc(data_size);
2007 err = vkGetPhysicalDeviceInfo(demo->gpu, VK_PHYSICAL_DEVICE_INFO_TYPE_PROPERTIES,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06002008 &data_size, demo->gpu_props);
2009 assert(!err);
2010
Tony Barbour72304ef2015-04-16 15:59:00 -06002011 err = vkGetPhysicalDeviceInfo(demo->gpu, VK_PHYSICAL_DEVICE_INFO_TYPE_QUEUE_PROPERTIES,
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07002012 &data_size, NULL);
2013 assert(!err);
2014
Tony Barbour72304ef2015-04-16 15:59:00 -06002015 demo->queue_props = (VkPhysicalDeviceQueueProperties *) malloc(data_size);
2016 err = vkGetPhysicalDeviceInfo(demo->gpu, VK_PHYSICAL_DEVICE_INFO_TYPE_QUEUE_PROPERTIES,
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07002017 &data_size, demo->queue_props);
2018 assert(!err);
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06002019 queue_count = (uint32_t)(data_size / sizeof(VkPhysicalDeviceQueueProperties));
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07002020 assert(queue_count >= 1);
2021
Mark Lobodzinskida9b1092015-04-16 11:44:05 -05002022 // Graphics queue and MemMgr queue can be separate.
2023 // TODO: Add support for separate queues, including synchronization,
Mark Lobodzinski6baaec72015-05-11 17:21:15 -05002024 // and appropriate tracking for QueueSubmit
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07002025 for (i = 0; i < queue_count; i++) {
Mark Lobodzinski6baaec72015-05-11 17:21:15 -05002026 if (demo->queue_props[i].queueFlags & VK_QUEUE_GRAPHICS_BIT)
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07002027 break;
2028 }
2029 assert(i < queue_count);
2030 demo->graphics_queue_node_index = i;
2031
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002032 err = vkGetDeviceQueue(demo->device, demo->graphics_queue_node_index,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002033 0, &demo->queue);
2034 assert(!err);
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002035
Jon Ashburnd7130cb2015-06-16 12:44:51 -06002036 // for now hardcode format till get WSI support
2037 demo->format = VK_FORMAT_B8G8R8A8_UNORM;
Ian Elliotte4602cd2015-04-21 16:41:02 -06002038
David Pinedo2bb7c932015-06-18 17:03:14 -06002039 demo->quit = false;
2040 demo->curFrame = 0;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002041}
2042
2043static void demo_init_connection(struct demo *demo)
2044{
Ian Elliott639ca472015-04-16 15:23:05 -06002045#ifndef _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002046 const xcb_setup_t *setup;
2047 xcb_screen_iterator_t iter;
2048 int scr;
2049
2050 demo->connection = xcb_connect(NULL, &scr);
Ian Elliott3979e282015-04-03 15:24:55 -06002051 if (demo->connection == NULL) {
2052 printf("Cannot find a compatible Vulkan installable client driver "
2053 "(ICD).\nExiting ...\n");
2054 fflush(stdout);
2055 exit(1);
2056 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002057
2058 setup = xcb_get_setup(demo->connection);
2059 iter = xcb_setup_roots_iterator(setup);
2060 while (scr-- > 0)
2061 xcb_screen_next(&iter);
2062
2063 demo->screen = iter.data;
Ian Elliott639ca472015-04-16 15:23:05 -06002064#endif // _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002065}
2066
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002067static void demo_init(struct demo *demo, int argc, char **argv)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002068{
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002069 vec3 eye = {0.0f, 3.0f, 5.0f};
2070 vec3 origin = {0, 0, 0};
Chia-I Wuae3b55d2015-04-22 14:56:17 +08002071 vec3 up = {0.0f, 1.0f, 0.0};
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002072
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002073 memset(demo, 0, sizeof(*demo));
David Pinedo2bb7c932015-06-18 17:03:14 -06002074 demo->frameCount = INT_MAX;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002075
Piers Daniell735ee532015-02-23 16:23:13 -07002076 for (int i = 1; i < argc; i++) {
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002077 if (strcmp(argv[i], "--use_staging") == 0) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002078 demo->use_staging_buffer = true;
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002079 continue;
Ian Elliott639ca472015-04-16 15:23:05 -06002080 }
Cody Northrop1fedb212015-05-28 11:27:16 -06002081 if (strcmp(argv[i], "--use_glsl") == 0) {
2082 demo->use_glsl = true;
2083 continue;
2084 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002085 if (strcmp(argv[i], "--validate") == 0) {
2086 demo->validate = true;
2087 continue;
2088 }
David Pinedo2bb7c932015-06-18 17:03:14 -06002089 if (strcmp(argv[i], "--c") == 0 &&
2090 demo->frameCount == INT_MAX &&
2091 i < argc-1 &&
2092 sscanf(argv[i+1],"%d", &demo->frameCount) == 1 &&
2093 demo->frameCount >= 0)
2094 {
2095 i++;
2096 continue;
2097 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002098
David Pinedo2bb7c932015-06-18 17:03:14 -06002099 fprintf(stderr, "Usage:\n %s [--use_staging] [--validate] [--c <framecount>]\n", APP_SHORT_NAME);
Ian Elliott639ca472015-04-16 15:23:05 -06002100 fflush(stderr);
2101 exit(1);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002102 }
2103
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002104 demo_init_connection(demo);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002105 demo_init_vk(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002106
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002107 demo->width = 500;
2108 demo->height = 500;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002109
2110 demo->spin_angle = 0.01f;
2111 demo->spin_increment = 0.01f;
2112 demo->pause = false;
2113
Piers Daniell735ee532015-02-23 16:23:13 -07002114 mat4x4_perspective(demo->projection_matrix, (float)degreesToRadians(45.0f), 1.0f, 0.1f, 100.0f);
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002115 mat4x4_look_at(demo->view_matrix, eye, origin, up);
2116 mat4x4_identity(demo->model_matrix);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002117}
2118
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002119
Ian Elliott639ca472015-04-16 15:23:05 -06002120#ifdef _WIN32
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002121extern int __getmainargs(
2122 int * _Argc,
2123 char *** _Argv,
2124 char *** _Env,
2125 int _DoWildCard,
2126 int * new_mode);
Ian Elliottf9cf78c2015-04-28 10:33:11 -06002127
Ian Elliotta748eaf2015-04-28 15:50:36 -06002128int WINAPI WinMain(HINSTANCE hInstance,
2129 HINSTANCE hPrevInstance,
2130 LPSTR pCmdLine,
2131 int nCmdShow)
Ian Elliott639ca472015-04-16 15:23:05 -06002132{
2133 MSG msg; // message
2134 bool done; // flag saying when app is complete
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002135 int argc;
2136 char** argv;
2137 char** env;
2138 int new_mode = 0;
Ian Elliott639ca472015-04-16 15:23:05 -06002139
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002140 __getmainargs(&argc,&argv,&env,0,&new_mode);
2141
2142 demo_init(&demo, argc, argv);
2143 demo.connection = hInstance;
2144 strncpy(demo.name, "cube", APP_NAME_STR_LEN);
Ian Elliott639ca472015-04-16 15:23:05 -06002145 demo_create_window(&demo);
2146
2147 demo_prepare(&demo);
2148
2149 done = false; //initialize loop condition variable
2150 /* main message loop*/
2151 while(!done)
2152 {
Ian Elliotta748eaf2015-04-28 15:50:36 -06002153 PeekMessage(&msg, NULL, 0, 0, PM_REMOVE);
Ian Elliott639ca472015-04-16 15:23:05 -06002154 if (msg.message == WM_QUIT) //check for a quit message
2155 {
2156 done = true; //if found, quit app
2157 }
2158 else
2159 {
2160 /* Translate and dispatch to event queue*/
2161 TranslateMessage(&msg);
2162 DispatchMessage(&msg);
2163 }
2164 }
2165
2166 demo_cleanup(&demo);
2167
Tony Barbourf9921732015-04-22 11:36:22 -06002168 return (int) msg.wParam;
Ian Elliott639ca472015-04-16 15:23:05 -06002169}
2170#else // _WIN32
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002171int main(int argc, char **argv)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002172{
2173 struct demo demo;
2174
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002175 demo_init(&demo, argc, argv);
Chia-I Wucbb564e2015-04-16 22:02:10 +08002176 demo_create_window(&demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002177
2178 demo_prepare(&demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002179 demo_run(&demo);
2180
2181 demo_cleanup(&demo);
2182
2183 return 0;
2184}
Ian Elliott639ca472015-04-16 15:23:05 -06002185#endif // _WIN32