blob: a34503041f9f6c59a361975345edb7dcdf9db787 [file] [log] [blame]
Ian Elliott421107f2015-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 Goeltzenleuchter17223602014-10-29 15:59:49 -060024#define _GNU_SOURCE
Courtney Goeltzenleuchter4e8246e2014-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 Elliotte14e9f92015-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 Goeltzenleuchter4e8246e2014-10-23 13:16:59 -060036#include <xcb/xcb.h>
Ian Elliotte14e9f92015-04-16 15:23:05 -060037#endif // _WIN32
38
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -060039#include <vulkan.h>
Chia-I Wu5b66aa52015-04-16 22:02:10 +080040#include <vk_wsi_lunarg.h>
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -060041#include "vk_debug_report_lunarg.h"
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -060042
Cody Northropd4e020a2015-03-17 14:54:35 -060043#include "icd-spv.h"
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -060044
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -060045#include "linmath.h"
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -060046#include <png.h>
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -060047
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -060048#define DEMO_BUFFER_COUNT 2
49#define DEMO_TEXTURE_COUNT 1
Ian Elliott4e19ed02015-04-28 10:52:52 -060050#define APP_SHORT_NAME "cube"
51#define APP_LONG_NAME "The Vulkan Cube Demo Program"
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -060052
Courtney Goeltzenleuchter8c15fc52015-07-06 17:46:11 -060053#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
54
Tony Barbour22a30862015-04-22 09:02:32 -060055#if defined(NDEBUG) && defined(__GNUC__)
56#define U_ASSERT_ONLY __attribute__((unused))
57#else
58#define U_ASSERT_ONLY
59#endif
60
Ian Elliottcaa9f272015-04-28 11:35:02 -060061#ifdef _WIN32
62#define ERR_EXIT(err_msg, err_class) \
63 do { \
64 MessageBox(NULL, err_msg, err_class, MB_OK); \
65 exit(1); \
66 } while (0)
67
68// NOTE: If the following values (copied from "loader_platform.h") change, they
69// need to change here as well:
70#define LAYER_NAMES_ENV "VK_LAYER_NAMES"
71#define LAYER_NAMES_REGISTRY_VALUE "VK_LAYER_NAMES"
72
73#else // _WIN32
74
75#define ERR_EXIT(err_msg, err_class) \
76 do { \
77 printf(err_msg); \
78 fflush(stdout); \
79 exit(1); \
80 } while (0)
81#endif // _WIN32
82
Ian Elliott1b6de092015-06-22 15:07:49 -060083#define GET_DEVICE_PROC_ADDR(dev, entrypoint) \
84{ \
Courtney Goeltzenleuchtera4c8c712015-07-12 14:35:22 -060085 demo->fp##entrypoint = (PFN_vk##entrypoint) vkGetDeviceProcAddr(dev, "vk"#entrypoint); \
Ian Elliott1b6de092015-06-22 15:07:49 -060086 if (demo->fp##entrypoint == NULL) { \
87 ERR_EXIT("vkGetDeviceProcAddr failed to find vk"#entrypoint, \
88 "vkGetDeviceProcAddr Failure"); \
89 } \
90}
91
Courtney Goeltzenleuchter7e334982014-10-30 15:14:16 -060092/*
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -070093 * structure to track all objects related to a texture.
94 */
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -060095struct texture_object {
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -060096 VkSampler sampler;
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -070097
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -060098 VkImage image;
99 VkImageLayout imageLayout;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600100
Mark Lobodzinski23182612015-05-29 09:32:35 -0500101 VkDeviceMemory mem;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600102 VkImageView view;
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700103 int32_t tex_width, tex_height;
104};
105
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600106static char *tex_files[] = {
Courtney Goeltzenleuchter7e334982014-10-30 15:14:16 -0600107 "lunarg-logo-256x256-solid.png"
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600108};
109
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600110struct vkcube_vs_uniform {
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -0600111 // Must start with MVP
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600112 float mvp[4][4];
113 float position[12*3][4];
114 float color[12*3][4];
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -0600115};
116
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600117struct vktexcube_vs_uniform {
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -0600118 // Must start with MVP
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600119 float mvp[4][4];
120 float position[12*3][4];
121 float attr[12*3][4];
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -0600122};
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -0600123
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600124//--------------------------------------------------------------------------------------
125// Mesh and VertexFormat Data
126//--------------------------------------------------------------------------------------
127struct Vertex
128{
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600129 float posX, posY, posZ, posW; // Position data
130 float r, g, b, a; // Color
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600131};
132
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -0600133struct VertexPosTex
134{
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600135 float posX, posY, posZ, posW; // Position data
136 float u, v, s, t; // Texcoord
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -0600137};
138
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600139#define XYZ1(_x_, _y_, _z_) (_x_), (_y_), (_z_), 1.f
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -0600140#define UV(_u_, _v_) (_u_), (_v_), 0.f, 1.f
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600141
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600142static const float g_vertex_buffer_data[] = {
Chia-I Wuc3487c22015-04-22 14:56:17 +0800143 -1.0f,-1.0f,-1.0f, // -X side
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600144 -1.0f,-1.0f, 1.0f,
145 -1.0f, 1.0f, 1.0f,
Chia-I Wuc3487c22015-04-22 14:56:17 +0800146 -1.0f, 1.0f, 1.0f,
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600147 -1.0f, 1.0f,-1.0f,
148 -1.0f,-1.0f,-1.0f,
149
Chia-I Wuc3487c22015-04-22 14:56:17 +0800150 -1.0f,-1.0f,-1.0f, // -Z side
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600151 1.0f, 1.0f,-1.0f,
152 1.0f,-1.0f,-1.0f,
Chia-I Wuc3487c22015-04-22 14:56:17 +0800153 -1.0f,-1.0f,-1.0f,
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600154 -1.0f, 1.0f,-1.0f,
Mike Stroyanea3945c2015-03-19 14:29:04 -0600155 1.0f, 1.0f,-1.0f,
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600156
Chia-I Wuc3487c22015-04-22 14:56:17 +0800157 -1.0f,-1.0f,-1.0f, // -Y side
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600158 1.0f,-1.0f,-1.0f,
Mike Stroyanea3945c2015-03-19 14:29:04 -0600159 1.0f,-1.0f, 1.0f,
Chia-I Wuc3487c22015-04-22 14:56:17 +0800160 -1.0f,-1.0f,-1.0f,
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600161 1.0f,-1.0f, 1.0f,
Mike Stroyanea3945c2015-03-19 14:29:04 -0600162 -1.0f,-1.0f, 1.0f,
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600163
Chia-I Wuc3487c22015-04-22 14:56:17 +0800164 -1.0f, 1.0f,-1.0f, // +Y side
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600165 -1.0f, 1.0f, 1.0f,
166 1.0f, 1.0f, 1.0f,
Chia-I Wuc3487c22015-04-22 14:56:17 +0800167 -1.0f, 1.0f,-1.0f,
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600168 1.0f, 1.0f, 1.0f,
Mike Stroyanea3945c2015-03-19 14:29:04 -0600169 1.0f, 1.0f,-1.0f,
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600170
Chia-I Wuc3487c22015-04-22 14:56:17 +0800171 1.0f, 1.0f,-1.0f, // +X side
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600172 1.0f, 1.0f, 1.0f,
173 1.0f,-1.0f, 1.0f,
Chia-I Wuc3487c22015-04-22 14:56:17 +0800174 1.0f,-1.0f, 1.0f,
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600175 1.0f,-1.0f,-1.0f,
176 1.0f, 1.0f,-1.0f,
177
Chia-I Wuc3487c22015-04-22 14:56:17 +0800178 -1.0f, 1.0f, 1.0f, // +Z side
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600179 -1.0f,-1.0f, 1.0f,
Mike Stroyanea3945c2015-03-19 14:29:04 -0600180 1.0f, 1.0f, 1.0f,
Chia-I Wuc3487c22015-04-22 14:56:17 +0800181 -1.0f,-1.0f, 1.0f,
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600182 1.0f,-1.0f, 1.0f,
183 1.0f, 1.0f, 1.0f,
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -0600184};
185
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600186static const float g_uv_buffer_data[] = {
Chia-I Wuc3487c22015-04-22 14:56:17 +0800187 0.0f, 0.0f, // -X side
188 1.0f, 0.0f,
189 1.0f, 1.0f,
190 1.0f, 1.0f,
191 0.0f, 1.0f,
192 0.0f, 0.0f,
193
194 1.0f, 0.0f, // -Z side
195 0.0f, 1.0f,
196 0.0f, 0.0f,
197 1.0f, 0.0f,
198 1.0f, 1.0f,
199 0.0f, 1.0f,
200
201 1.0f, 1.0f, // -Y side
202 1.0f, 0.0f,
203 0.0f, 0.0f,
204 1.0f, 1.0f,
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600205 0.0f, 0.0f,
206 0.0f, 1.0f,
207
Chia-I Wuc3487c22015-04-22 14:56:17 +0800208 1.0f, 1.0f, // +Y side
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -0600209 0.0f, 1.0f,
Chia-I Wuc3487c22015-04-22 14:56:17 +0800210 0.0f, 0.0f,
Mike Stroyanea3945c2015-03-19 14:29:04 -0600211 1.0f, 1.0f,
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600212 0.0f, 0.0f,
Mike Stroyanea3945c2015-03-19 14:29:04 -0600213 1.0f, 0.0f,
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600214
Chia-I Wuc3487c22015-04-22 14:56:17 +0800215 1.0f, 1.0f, // +X side
216 0.0f, 1.0f,
217 0.0f, 0.0f,
218 0.0f, 0.0f,
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600219 1.0f, 0.0f,
Mike Stroyanea3945c2015-03-19 14:29:04 -0600220 1.0f, 1.0f,
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600221
Chia-I Wuc3487c22015-04-22 14:56:17 +0800222 0.0f, 1.0f, // +Z side
223 0.0f, 0.0f,
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600224 1.0f, 1.0f,
Mike Stroyanea3945c2015-03-19 14:29:04 -0600225 0.0f, 0.0f,
Chia-I Wuc3487c22015-04-22 14:56:17 +0800226 1.0f, 0.0f,
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600227 1.0f, 1.0f,
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -0600228};
229
230void dumpMatrix(const char *note, mat4x4 MVP)
231{
232 int i;
233
234 printf("%s: \n", note);
235 for (i=0; i<4; i++) {
236 printf("%f, %f, %f, %f\n", MVP[i][0], MVP[i][1], MVP[i][2], MVP[i][3]);
237 }
238 printf("\n");
239 fflush(stdout);
240}
241
242void dumpVec4(const char *note, vec4 vector)
243{
244 printf("%s: \n", note);
245 printf("%f, %f, %f, %f\n", vector[0], vector[1], vector[2], vector[3]);
246 printf("\n");
247 fflush(stdout);
248}
249
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -0600250void dbgFunc(
Tony Barbourde4124d2015-07-03 10:33:54 -0600251 VkFlags msgFlags,
252 VkDbgObjectType objType,
253 uint64_t srcObject,
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -0600254 size_t location,
255 int32_t msgCode,
256 const char* pLayerPrefix,
257 const char* pMsg,
258 void* pUserData)
Tony Barbour5685ad72015-04-29 16:19:20 -0600259{
260 char *message = (char *) malloc(strlen(pMsg)+100);
261
262 assert (message);
263
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -0600264 if (msgFlags & VK_DBG_REPORT_ERROR_BIT) {
265 sprintf(message,"ERROR: [%s] Code %d : %s", pLayerPrefix, msgCode, pMsg);
266 } else if (msgFlags & VK_DBG_REPORT_WARN_BIT) {
Tony Barboura65ecc22015-06-30 14:14:19 -0600267 // We know that we're submitting queues without fences, ignore this warning
268 if (strstr(pMsg, "vkQueueSubmit parameter, VkFence fence, is null pointer")){
269 return;
270 }
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -0600271 sprintf(message,"WARNING: [%s] Code %d : %s", pLayerPrefix, msgCode, pMsg);
Tony Barbour5685ad72015-04-29 16:19:20 -0600272 } else {
273 return;
274 }
275
276#ifdef _WIN32
277 MessageBox(NULL, message, "Alert", MB_OK);
278#else
279 printf("%s\n",message);
280 fflush(stdout);
281#endif
282 free(message);
283}
284
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600285struct demo {
Ian Elliotte14e9f92015-04-16 15:23:05 -0600286#ifdef _WIN32
287#define APP_NAME_STR_LEN 80
288 HINSTANCE connection; // hInstance - Windows Instance
289 char name[APP_NAME_STR_LEN]; // Name to put on the window/icon
290 HWND window; // hWnd - window handle
291#else // _WIN32
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600292 xcb_connection_t *connection;
293 xcb_screen_t *screen;
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800294 xcb_window_t window;
295 xcb_intern_atom_reply_t *atom_wm_delete_window;
Ian Elliotte14e9f92015-04-16 15:23:05 -0600296#endif // _WIN32
Cody Northrop75db0322015-05-28 11:27:16 -0600297 bool prepared;
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700298 bool use_staging_buffer;
Cody Northrop75db0322015-05-28 11:27:16 -0600299 bool use_glsl;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600300
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600301 VkInstance inst;
Tony Barbour8205d902015-04-16 15:59:00 -0600302 VkPhysicalDevice gpu;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600303 VkDevice device;
304 VkQueue queue;
Courtney Goeltzenleuchterf3168062015-03-05 18:09:39 -0700305 uint32_t graphics_queue_node_index;
Tony Barbour426b9052015-06-24 16:06:58 -0600306 VkPhysicalDeviceProperties gpu_props;
Tony Barbour8205d902015-04-16 15:59:00 -0600307 VkPhysicalDeviceQueueProperties *queue_props;
Mark Lobodzinski72346292015-07-02 16:49:40 -0600308 VkPhysicalDeviceMemoryProperties memory_properties;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600309
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600310 VkFramebuffer framebuffer;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600311 int width, height;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600312 VkFormat format;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600313
Jon Ashburncedc15f2015-05-21 18:13:33 -0600314 PFN_vkCreateSwapChainWSI fpCreateSwapChainWSI;
315 PFN_vkDestroySwapChainWSI fpDestroySwapChainWSI;
316 PFN_vkGetSwapChainInfoWSI fpGetSwapChainInfoWSI;
317 PFN_vkQueuePresentWSI fpQueuePresentWSI;
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800318 VkSwapChainWSI swap_chain;
Cody Northrop18ea11b2015-07-09 18:08:32 -0600319 VkCmdPool cmd_pool;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600320 struct {
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600321 VkImage image;
Tony Barbour8205d902015-04-16 15:59:00 -0600322 VkDeviceMemory mem;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600323 VkCmdBuffer cmd;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600324
Chia-I Wuc278df82015-07-07 11:50:03 +0800325 VkAttachmentView view;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600326 } buffers[DEMO_BUFFER_COUNT];
327
328 struct {
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600329 VkFormat format;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600330
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600331 VkImage image;
Mark Lobodzinski23182612015-05-29 09:32:35 -0500332 VkDeviceMemory mem;
Chia-I Wuc278df82015-07-07 11:50:03 +0800333 VkAttachmentView view;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600334 } depth;
335
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600336 struct texture_object textures[DEMO_TEXTURE_COUNT];
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600337
338 struct {
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600339 VkBuffer buf;
Mark Lobodzinski23182612015-05-29 09:32:35 -0500340 VkDeviceMemory mem;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600341 VkBufferView view;
Chia-I Wu8cd8ecd2015-05-25 16:27:55 +0800342 VkDescriptorInfo desc;
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -0600343 } uniform_data;
344
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600345 VkCmdBuffer cmd; // Buffer for initialization commands
Mark Lobodzinski556f7212015-04-17 14:11:39 -0500346 VkPipelineLayout pipeline_layout;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600347 VkDescriptorSetLayout desc_layout;
Jon Ashburn0d60d272015-07-09 15:02:25 -0600348 VkPipelineCache pipelineCache;
Chia-I Wu76cd4222015-07-08 13:34:24 +0800349 VkRenderPass render_pass;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600350 VkPipeline pipeline;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600351
Tony Barbourde4124d2015-07-03 10:33:54 -0600352 VkDynamicViewportState viewport;
353 VkDynamicRasterState raster;
354 VkDynamicColorBlendState color_blend;
355 VkDynamicDepthStencilState depth_stencil;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600356
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -0600357 mat4x4 projection_matrix;
358 mat4x4 view_matrix;
359 mat4x4 model_matrix;
360
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600361 float spin_angle;
362 float spin_increment;
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -0600363 bool pause;
364
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600365 VkDescriptorPool desc_pool;
366 VkDescriptorSet desc_set;
Chia-I Wuf8385062015-01-04 16:27:24 +0800367
Chia-I Wu76cd4222015-07-08 13:34:24 +0800368 VkFramebuffer framebuffers[DEMO_BUFFER_COUNT];
369
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600370 bool quit;
David Pinedoeeca2a22015-06-18 17:03:14 -0600371 int32_t curFrame;
372 int32_t frameCount;
Tony Barbour5685ad72015-04-29 16:19:20 -0600373 bool validate;
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -0600374 PFN_vkDbgCreateMsgCallback dbgCreateMsgCallback;
Tony Barboura65ecc22015-06-30 14:14:19 -0600375 PFN_vkDbgDestroyMsgCallback dbgDestroyMsgCallback;
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -0600376 VkDbgMsgCallback msg_callback;
377
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600378 uint32_t current_buffer;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600379};
380
Mark Lobodzinski72346292015-07-02 16:49:40 -0600381static VkResult memory_type_from_properties(struct demo *demo, uint32_t typeBits, VkFlags properties, uint32_t *typeIndex)
382{
383 // Search memtypes to find first index with those properties
384 for (uint32_t i = 0; i < 32; i++) {
385 if ((typeBits & 1) == 1) {
386 // Type is available, does it match user properties?
387 if ((demo->memory_properties.memoryTypes[i].propertyFlags & properties) == properties) {
388 *typeIndex = i;
389 return VK_SUCCESS;
390 }
391 }
392 typeBits >>= 1;
393 }
394 // No memory types matched, return failure
395 return VK_UNSUPPORTED;
396}
397
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600398static void demo_flush_init_cmd(struct demo *demo)
399{
Tony Barbour22a30862015-04-22 09:02:32 -0600400 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600401
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600402 if (demo->cmd == VK_NULL_HANDLE)
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600403 return;
404
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600405 err = vkEndCommandBuffer(demo->cmd);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600406 assert(!err);
407
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600408 const VkCmdBuffer cmd_bufs[] = { demo->cmd };
Tony Barbourde4124d2015-07-03 10:33:54 -0600409 VkFence nullFence = { VK_NULL_HANDLE };
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600410
Tony Barbourde4124d2015-07-03 10:33:54 -0600411 err = vkQueueSubmit(demo->queue, 1, cmd_bufs, nullFence);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600412 assert(!err);
413
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600414 err = vkQueueWaitIdle(demo->queue);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600415 assert(!err);
416
Tony Barbourde4124d2015-07-03 10:33:54 -0600417 vkDestroyCommandBuffer(demo->device, demo->cmd);
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600418 demo->cmd = VK_NULL_HANDLE;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600419}
420
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600421static void demo_set_image_layout(
422 struct demo *demo,
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600423 VkImage image,
malnasse4b8ba4d2015-06-03 17:28:38 -0400424 VkImageAspect aspect,
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600425 VkImageLayout old_image_layout,
426 VkImageLayout new_image_layout)
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600427{
Tony Barbour22a30862015-04-22 09:02:32 -0600428 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600429
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600430 if (demo->cmd == VK_NULL_HANDLE) {
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600431 const VkCmdBufferCreateInfo cmd = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600432 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600433 .pNext = NULL,
Cody Northrop18ea11b2015-07-09 18:08:32 -0600434 .cmdPool = demo->cmd_pool,
Chia-I Wu88eaa3b2015-06-26 15:34:39 +0800435 .level = VK_CMD_BUFFER_LEVEL_PRIMARY,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600436 .flags = 0,
437 };
438
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600439 err = vkCreateCommandBuffer(demo->device, &cmd, &demo->cmd);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600440 assert(!err);
441
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600442 VkCmdBufferBeginInfo cmd_buf_info = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600443 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600444 .pNext = NULL,
Tony Barbour8205d902015-04-16 15:59:00 -0600445 .flags = VK_CMD_BUFFER_OPTIMIZE_SMALL_BATCH_BIT |
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600446 VK_CMD_BUFFER_OPTIMIZE_ONE_TIME_SUBMIT_BIT,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600447 };
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600448 err = vkBeginCommandBuffer(demo->cmd, &cmd_buf_info);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600449 }
450
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600451 VkImageMemoryBarrier image_memory_barrier = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600452 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600453 .pNext = NULL,
454 .outputMask = 0,
455 .inputMask = 0,
456 .oldLayout = old_image_layout,
457 .newLayout = new_image_layout,
458 .image = image,
malnasse4b8ba4d2015-06-03 17:28:38 -0400459 .subresourceRange = { aspect, 0, 1, 0, 0 }
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600460 };
461
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600462 if (new_image_layout == VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL) {
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600463 /* Make sure anything that was copying from this image has completed */
Courtney Goeltzenleuchterad870812015-04-15 15:29:59 -0600464 image_memory_barrier.inputMask = VK_MEMORY_INPUT_TRANSFER_BIT;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600465 }
466
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600467 if (new_image_layout == VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL) {
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600468 /* Make sure any Copy or CPU writes to image are flushed */
Courtney Goeltzenleuchtera569a502015-04-29 17:16:21 -0600469 image_memory_barrier.outputMask = VK_MEMORY_OUTPUT_HOST_WRITE_BIT | VK_MEMORY_OUTPUT_TRANSFER_BIT;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600470 }
471
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600472 VkImageMemoryBarrier *pmemory_barrier = &image_memory_barrier;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600473
Tony Barbourc2e987e2015-06-29 16:20:35 -0600474 VkPipelineStageFlags src_stages = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
475 VkPipelineStageFlags dest_stages = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600476
Courtney Goeltzenleuchter82b348f2015-07-12 13:07:46 -0600477 vkCmdPipelineBarrier(demo->cmd, src_stages, dest_stages, false, 1, (const void * const*)&pmemory_barrier);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600478}
479
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600480static void demo_draw_build_cmd(struct demo *demo, VkCmdBuffer cmd_buf)
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600481{
Chia-I Wu76cd4222015-07-08 13:34:24 +0800482 const VkCmdBufferBeginInfo cmd_buf_info = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600483 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO,
Courtney Goeltzenleuchtere3b0f3a2015-04-03 15:25:24 -0600484 .pNext = NULL,
Tony Barbour8205d902015-04-16 15:59:00 -0600485 .flags = VK_CMD_BUFFER_OPTIMIZE_SMALL_BATCH_BIT |
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600486 VK_CMD_BUFFER_OPTIMIZE_ONE_TIME_SUBMIT_BIT,
Jon Ashburn53d27af2014-12-31 17:08:35 -0700487 };
Chia-I Wuc278df82015-07-07 11:50:03 +0800488 const VkClearValue clear_values[2] = {
489 [0] = { .color.f32 = { 0.2f, 0.2f, 0.2f, 0.2f } },
490 [1] = { .ds = { 1.0f, 0 } },
491 };
492 const VkRenderPassBeginInfo rp_begin = {
493 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO,
494 .pNext = NULL,
Chia-I Wu76cd4222015-07-08 13:34:24 +0800495 .renderPass = demo->render_pass,
496 .framebuffer = demo->framebuffers[demo->current_buffer],
Chia-I Wuc278df82015-07-07 11:50:03 +0800497 .renderArea.offset.x = 0,
498 .renderArea.offset.y = 0,
499 .renderArea.extent.width = demo->width,
500 .renderArea.extent.height = demo->height,
501 .attachmentCount = 2,
502 .pAttachmentClearValues = clear_values,
Jon Ashburn3325d6b2015-01-02 18:24:05 -0700503 };
Chia-I Wu76cd4222015-07-08 13:34:24 +0800504 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600505
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600506 err = vkBeginCommandBuffer(cmd_buf, &cmd_buf_info);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600507 assert(!err);
508
Chia-I Wuc278df82015-07-07 11:50:03 +0800509 vkCmdBeginRenderPass(cmd_buf, &rp_begin, VK_RENDER_PASS_CONTENTS_INLINE);
510
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600511 vkCmdBindPipeline(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600512 demo->pipeline);
Mark Lobodzinskia65c4632015-06-15 13:21:21 -0600513 vkCmdBindDescriptorSets(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS, demo->pipeline_layout,
Cody Northrop1a01b1d2015-04-16 13:41:56 -0600514 0, 1, &demo->desc_set, 0, NULL);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600515
Tony Barbourde4124d2015-07-03 10:33:54 -0600516 vkCmdBindDynamicViewportState(cmd_buf, demo->viewport);
517 vkCmdBindDynamicRasterState(cmd_buf, demo->raster);
518 vkCmdBindDynamicColorBlendState(cmd_buf, demo->color_blend);
519 vkCmdBindDynamicDepthStencilState(cmd_buf, demo->depth_stencil);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600520
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600521 vkCmdDraw(cmd_buf, 0, 12 * 3, 0, 1);
Chia-I Wu88eaa3b2015-06-26 15:34:39 +0800522 vkCmdEndRenderPass(cmd_buf);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600523
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600524 err = vkEndCommandBuffer(cmd_buf);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600525 assert(!err);
526}
527
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -0600528
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -0600529void demo_update_data_buffer(struct demo *demo)
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600530{
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -0600531 mat4x4 MVP, Model, VP;
532 int matrixSize = sizeof(MVP);
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600533 uint8_t *pData;
Tony Barbour22a30862015-04-22 09:02:32 -0600534 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600535
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -0600536 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600537
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -0600538 // Rotate 22.5 degrees around the Y axis
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -0600539 mat4x4_dup(Model, demo->model_matrix);
Piers Daniell886be472015-02-23 16:23:13 -0700540 mat4x4_rotate(demo->model_matrix, Model, 0.0f, 1.0f, 0.0f, (float)degreesToRadians(demo->spin_angle));
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -0600541 mat4x4_mul(MVP, VP, demo->model_matrix);
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -0600542
Mark Lobodzinski23182612015-05-29 09:32:35 -0500543 err = vkMapMemory(demo->device, demo->uniform_data.mem, 0, 0, 0, (void **) &pData);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600544 assert(!err);
545
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -0600546 memcpy(pData, (const void*) &MVP[0][0], matrixSize);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600547
Mark Lobodzinski23182612015-05-29 09:32:35 -0500548 err = vkUnmapMemory(demo->device, demo->uniform_data.mem);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600549 assert(!err);
550}
551
552static void demo_draw(struct demo *demo)
553{
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800554 const VkPresentInfoWSI present = {
555 .sType = VK_STRUCTURE_TYPE_PRESENT_INFO_WSI,
556 .pNext = NULL,
557 .image = demo->buffers[demo->current_buffer].image,
558 .flipInterval = 0,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600559 };
Tony Barbour22a30862015-04-22 09:02:32 -0600560 VkResult U_ASSERT_ONLY err;
Tony Barbourde4124d2015-07-03 10:33:54 -0600561 VkFence nullFence = { VK_NULL_HANDLE };
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600562
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600563 err = vkQueueSubmit(demo->queue, 1, &demo->buffers[demo->current_buffer].cmd,
Tony Barbourde4124d2015-07-03 10:33:54 -0600564 nullFence);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600565 assert(!err);
566
Jon Ashburncedc15f2015-05-21 18:13:33 -0600567 err = demo->fpQueuePresentWSI(demo->queue, &present);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600568 assert(!err);
569
570 demo->current_buffer = (demo->current_buffer + 1) % DEMO_BUFFER_COUNT;
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800571
572 err = vkQueueWaitIdle(demo->queue);
573 assert(err == VK_SUCCESS);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600574}
575
576static void demo_prepare_buffers(struct demo *demo)
577{
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800578 const VkSwapChainCreateInfoWSI swap_chain = {
579 .sType = VK_STRUCTURE_TYPE_SWAP_CHAIN_CREATE_INFO_WSI,
580 .pNext = NULL,
581 .pNativeWindowSystemHandle = demo->connection,
582 .pNativeWindowHandle = (void *) (intptr_t) demo->window,
Ian Elliott32536f92015-04-21 16:41:02 -0600583 .displayCount = 1,
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800584 .imageCount = DEMO_BUFFER_COUNT,
585 .imageFormat = demo->format,
586 .imageExtent = {
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600587 .width = demo->width,
588 .height = demo->height,
589 },
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800590 .imageArraySize = 1,
591 .imageUsageFlags = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600592 };
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800593 VkSwapChainImageInfoWSI images[DEMO_BUFFER_COUNT];
594 size_t images_size = sizeof(images);
Tony Barbour22a30862015-04-22 09:02:32 -0600595 VkResult U_ASSERT_ONLY err;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600596 uint32_t i;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600597
Jon Ashburncedc15f2015-05-21 18:13:33 -0600598 err = demo->fpCreateSwapChainWSI(demo->device, &swap_chain, &demo->swap_chain);
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800599 assert(!err);
600
Jon Ashburncedc15f2015-05-21 18:13:33 -0600601 err = demo->fpGetSwapChainInfoWSI(demo->swap_chain,
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800602 VK_SWAP_CHAIN_INFO_TYPE_PERSISTENT_IMAGES_WSI,
603 &images_size, images);
604 assert(!err && images_size == sizeof(images));
605
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600606 for (i = 0; i < DEMO_BUFFER_COUNT; i++) {
Chia-I Wuc278df82015-07-07 11:50:03 +0800607 VkAttachmentViewCreateInfo color_attachment_view = {
608 .sType = VK_STRUCTURE_TYPE_ATTACHMENT_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600609 .pNext = NULL,
610 .format = demo->format,
611 .mipLevel = 0,
612 .baseArraySlice = 0,
613 .arraySize = 1,
614 };
615
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800616 demo->buffers[i].image = images[i].image;
617 demo->buffers[i].mem = images[i].memory;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600618
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600619 demo_set_image_layout(demo, demo->buffers[i].image,
malnasse4b8ba4d2015-06-03 17:28:38 -0400620 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600621 VK_IMAGE_LAYOUT_UNDEFINED,
622 VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600623
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600624 color_attachment_view.image = demo->buffers[i].image;
625
Chia-I Wuc278df82015-07-07 11:50:03 +0800626 err = vkCreateAttachmentView(demo->device,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600627 &color_attachment_view, &demo->buffers[i].view);
628 assert(!err);
629 }
630}
631
632static void demo_prepare_depth(struct demo *demo)
633{
Tony Barbour8205d902015-04-16 15:59:00 -0600634 const VkFormat depth_format = VK_FORMAT_D16_UNORM;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600635 const VkImageCreateInfo image = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600636 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600637 .pNext = NULL,
Tony Barbour8205d902015-04-16 15:59:00 -0600638 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600639 .format = depth_format,
640 .extent = { demo->width, demo->height, 1 },
641 .mipLevels = 1,
642 .arraySize = 1,
643 .samples = 1,
Tony Barbour8205d902015-04-16 15:59:00 -0600644 .tiling = VK_IMAGE_TILING_OPTIMAL,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600645 .usage = VK_IMAGE_USAGE_DEPTH_STENCIL_BIT,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600646 .flags = 0,
647 };
Courtney Goeltzenleuchterddcb6192015-04-14 18:48:46 -0600648 VkMemoryAllocInfo mem_alloc = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600649 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
Mark Lobodzinski97dcd042015-04-16 08:52:00 -0500650 .pNext = NULL,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600651 .allocationSize = 0,
Mark Lobodzinski72346292015-07-02 16:49:40 -0600652 .memoryTypeIndex = 0,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600653 };
Chia-I Wuc278df82015-07-07 11:50:03 +0800654 VkAttachmentViewCreateInfo view = {
655 .sType = VK_STRUCTURE_TYPE_ATTACHMENT_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600656 .pNext = NULL,
Tony Barbourde4124d2015-07-03 10:33:54 -0600657 .image.handle = VK_NULL_HANDLE,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600658 .mipLevel = 0,
659 .baseArraySlice = 0,
660 .arraySize = 1,
661 .flags = 0,
662 };
Mike Stroyan230e6252015-04-17 12:36:38 -0600663
Mark Lobodzinski23182612015-05-29 09:32:35 -0500664 VkMemoryRequirements mem_reqs;
Tony Barbour22a30862015-04-22 09:02:32 -0600665 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600666
667 demo->depth.format = depth_format;
668
669 /* create image */
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600670 err = vkCreateImage(demo->device, &image,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600671 &demo->depth.image);
672 assert(!err);
673
Tony Barbourde4124d2015-07-03 10:33:54 -0600674 err = vkGetImageMemoryRequirements(demo->device,
675 demo->depth.image, &mem_reqs);
Mark Lobodzinski72346292015-07-02 16:49:40 -0600676
677 mem_alloc.allocationSize = mem_reqs.size;
678 err = memory_type_from_properties(demo,
679 mem_reqs.memoryTypeBits,
680 VK_MEMORY_PROPERTY_DEVICE_ONLY,
681 &mem_alloc.memoryTypeIndex);
682 assert(!err);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600683
Mark Lobodzinski23182612015-05-29 09:32:35 -0500684 /* allocate memory */
685 err = vkAllocMemory(demo->device, &mem_alloc, &demo->depth.mem);
686 assert(!err);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600687
Mark Lobodzinski23182612015-05-29 09:32:35 -0500688 /* bind memory */
Tony Barbourde4124d2015-07-03 10:33:54 -0600689 err = vkBindImageMemory(demo->device, demo->depth.image,
Mark Lobodzinski23182612015-05-29 09:32:35 -0500690 demo->depth.mem, 0);
691 assert(!err);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600692
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600693 demo_set_image_layout(demo, demo->depth.image,
malnasse4b8ba4d2015-06-03 17:28:38 -0400694 VK_IMAGE_ASPECT_DEPTH,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600695 VK_IMAGE_LAYOUT_UNDEFINED,
696 VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600697
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600698 /* create image view */
699 view.image = demo->depth.image;
Chia-I Wuc278df82015-07-07 11:50:03 +0800700 err = vkCreateAttachmentView(demo->device, &view, &demo->depth.view);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600701 assert(!err);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600702}
703
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600704/** loadTexture
Courtney Goeltzenleuchtercb67a322015-04-21 09:31:23 -0600705 * loads a png file into an memory object, using cstdio , libpng.
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600706 *
Courtney Goeltzenleuchtercb67a322015-04-21 09:31:23 -0600707 * \param demo : Needed to access VK calls
708 * \param filename : the png file to be loaded
709 * \param width : width of png, to be updated as a side effect of this function
710 * \param height : height of png, to be updated as a side effect of this function
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600711 *
Courtney Goeltzenleuchtercb67a322015-04-21 09:31:23 -0600712 * \return bool : an opengl texture id. true if successful?,
713 * should be validated by the client of this function.
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600714 *
715 * Source: http://en.wikibooks.org/wiki/OpenGL_Programming/Intermediate/Textures
716 * Modified to copy image to memory
717 *
718 */
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700719bool loadTexture(const char *filename, uint8_t *rgba_data,
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600720 VkSubresourceLayout *layout,
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600721 int32_t *width, int32_t *height)
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600722{
723 //header for testing if it is a png
724 png_byte header[8];
Tony Barboura938abb2015-04-22 11:36:22 -0600725 int is_png, bit_depth, color_type, rowbytes;
726 size_t retval;
Ian Elliott642f8922015-02-13 14:29:21 -0700727 png_uint_32 i, twidth, theight;
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600728 png_structp png_ptr;
729 png_infop info_ptr, end_info;
730 png_byte *image_data;
731 png_bytep *row_pointers;
732
733 //open file as binary
734 FILE *fp = fopen(filename, "rb");
735 if (!fp) {
736 return false;
737 }
738
739 //read the header
Tony Barbour22a30862015-04-22 09:02:32 -0600740 retval = fread(header, 1, 8, fp);
741 if (retval != 8) {
742 fclose(fp);
743 return false;
744 }
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600745
746 //test if png
747 is_png = !png_sig_cmp(header, 0, 8);
748 if (!is_png) {
749 fclose(fp);
750 return false;
751 }
752
753 //create png struct
754 png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL,
755 NULL, NULL);
756 if (!png_ptr) {
757 fclose(fp);
758 return (false);
759 }
760
761 //create png info struct
762 info_ptr = png_create_info_struct(png_ptr);
763 if (!info_ptr) {
764 png_destroy_read_struct(&png_ptr, (png_infopp) NULL, (png_infopp) NULL);
765 fclose(fp);
766 return (false);
767 }
768
769 //create png info struct
770 end_info = png_create_info_struct(png_ptr);
771 if (!end_info) {
772 png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp) NULL);
773 fclose(fp);
774 return (false);
775 }
776
777 //png error stuff, not sure libpng man suggests this.
778 if (setjmp(png_jmpbuf(png_ptr))) {
779 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
780 fclose(fp);
781 return (false);
782 }
783
784 //init png reading
785 png_init_io(png_ptr, fp);
786
787 //let libpng know you already read the first 8 bytes
788 png_set_sig_bytes(png_ptr, 8);
789
790 // read all the info up to the image data
791 png_read_info(png_ptr, info_ptr);
792
793 // get info about png
794 png_get_IHDR(png_ptr, info_ptr, &twidth, &theight, &bit_depth, &color_type,
795 NULL, NULL, NULL);
796
797 //update width and height based on png info
798 *width = twidth;
799 *height = theight;
800
801 // Require that incoming texture be 8bits per color component
802 // and 4 components (RGBA).
803 if (png_get_bit_depth(png_ptr, info_ptr) != 8 ||
804 png_get_channels(png_ptr, info_ptr) != 4) {
805 return false;
806 }
807
808 if (rgba_data == NULL) {
809 // If data pointer is null, we just want the width & height
810 // clean up memory and close stuff
811 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
812 fclose(fp);
813
814 return true;
815 }
816
817 // Update the png info struct.
818 png_read_update_info(png_ptr, info_ptr);
819
820 // Row size in bytes.
821 rowbytes = png_get_rowbytes(png_ptr, info_ptr);
822
823 // Allocate the image_data as a big block, to be given to opengl
824 image_data = (png_byte *)malloc(rowbytes * theight * sizeof(png_byte));
825 if (!image_data) {
826 //clean up memory and close stuff
827 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
828 fclose(fp);
829 return false;
830 }
831
832 // row_pointers is for pointing to image_data for reading the png with libpng
833 row_pointers = (png_bytep *)malloc(theight * sizeof(png_bytep));
834 if (!row_pointers) {
835 //clean up memory and close stuff
836 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
837 // delete[] image_data;
838 fclose(fp);
839 return false;
840 }
841 // set the individual row_pointers to point at the correct offsets of image_data
842 for (i = 0; i < theight; ++i)
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700843 row_pointers[theight - 1 - i] = rgba_data + i * layout->rowPitch;
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600844
845 // read the png into image_data through row_pointers
846 png_read_image(png_ptr, row_pointers);
847
848 // clean up memory and close stuff
849 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
850 free(row_pointers);
851 free(image_data);
852 fclose(fp);
853
854 return true;
855}
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600856
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700857static void demo_prepare_texture_image(struct demo *demo,
858 const char *filename,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600859 struct texture_object *tex_obj,
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600860 VkImageTiling tiling,
Courtney Goeltzenleuchtercb67a322015-04-21 09:31:23 -0600861 VkImageUsageFlags usage,
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600862 VkFlags mem_props)
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600863{
Mike Stroyande24a6f2015-06-15 14:21:03 -0600864 const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600865 int32_t tex_width;
866 int32_t tex_height;
Tony Barbour22a30862015-04-22 09:02:32 -0600867 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700868
David Pinedo61e77382015-04-23 08:16:57 -0600869 if (!loadTexture(filename, NULL, NULL, &tex_width, &tex_height))
870 {
871 printf("Failed to load textures\n");
872 fflush(stdout);
873 exit(1);
874 }
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700875
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600876 tex_obj->tex_width = tex_width;
877 tex_obj->tex_height = tex_height;
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700878
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600879 const VkImageCreateInfo image_create_info = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600880 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700881 .pNext = NULL,
Tony Barbour8205d902015-04-16 15:59:00 -0600882 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700883 .format = tex_format,
884 .extent = { tex_width, tex_height, 1 },
885 .mipLevels = 1,
886 .arraySize = 1,
887 .samples = 1,
888 .tiling = tiling,
Courtney Goeltzenleuchtercb67a322015-04-21 09:31:23 -0600889 .usage = usage,
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700890 .flags = 0,
891 };
Courtney Goeltzenleuchterddcb6192015-04-14 18:48:46 -0600892 VkMemoryAllocInfo mem_alloc = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600893 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
Mark Lobodzinski97dcd042015-04-16 08:52:00 -0500894 .pNext = NULL,
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700895 .allocationSize = 0,
Mark Lobodzinski72346292015-07-02 16:49:40 -0600896 .memoryTypeIndex = 0,
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700897 };
898
Mark Lobodzinski23182612015-05-29 09:32:35 -0500899 VkMemoryRequirements mem_reqs;
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700900
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600901 err = vkCreateImage(demo->device, &image_create_info,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600902 &tex_obj->image);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700903 assert(!err);
904
Tony Barbourde4124d2015-07-03 10:33:54 -0600905 err = vkGetImageMemoryRequirements(demo->device, tex_obj->image, &mem_reqs);
Tony Barbour426b9052015-06-24 16:06:58 -0600906 assert(!err);
Piers Daniell886be472015-02-23 16:23:13 -0700907
Mark Lobodzinski23182612015-05-29 09:32:35 -0500908 mem_alloc.allocationSize = mem_reqs.size;
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700909
Mark Lobodzinski72346292015-07-02 16:49:40 -0600910 err = memory_type_from_properties(demo, mem_reqs.memoryTypeBits, mem_props, &mem_alloc.memoryTypeIndex);
911 assert(!err);
912
Mark Lobodzinski23182612015-05-29 09:32:35 -0500913 /* allocate memory */
914 err = vkAllocMemory(demo->device, &mem_alloc,
915 &(tex_obj->mem));
916 assert(!err);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700917
Mark Lobodzinski23182612015-05-29 09:32:35 -0500918 /* bind memory */
Tony Barbourde4124d2015-07-03 10:33:54 -0600919 err = vkBindImageMemory(demo->device, tex_obj->image,
Mark Lobodzinski23182612015-05-29 09:32:35 -0500920 tex_obj->mem, 0);
921 assert(!err);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700922
Tony Barbour8205d902015-04-16 15:59:00 -0600923 if (mem_props & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) {
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600924 const VkImageSubresource subres = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600925 .aspect = VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700926 .mipLevel = 0,
927 .arraySlice = 0,
928 };
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600929 VkSubresourceLayout layout;
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700930 void *data;
931
Tony Barbour426b9052015-06-24 16:06:58 -0600932 err = vkGetImageSubresourceLayout(demo->device, tex_obj->image, &subres, &layout);
933 assert(!err);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700934
Mark Lobodzinski23182612015-05-29 09:32:35 -0500935 err = vkMapMemory(demo->device, tex_obj->mem, 0, 0, 0, &data);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700936 assert(!err);
937
938 if (!loadTexture(filename, data, &layout, &tex_width, &tex_height)) {
939 fprintf(stderr, "Error loading texture: %s\n", filename);
940 }
941
Mark Lobodzinski23182612015-05-29 09:32:35 -0500942 err = vkUnmapMemory(demo->device, tex_obj->mem);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700943 assert(!err);
944 }
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600945
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600946 tex_obj->imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600947 demo_set_image_layout(demo, tex_obj->image,
malnasse4b8ba4d2015-06-03 17:28:38 -0400948 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600949 VK_IMAGE_LAYOUT_UNDEFINED,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600950 tex_obj->imageLayout);
951 /* setting the image layout does not reference the actual memory so no need to add a mem ref */
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700952}
953
Mark Lobodzinskicf26e072015-04-16 11:44:05 -0500954static void demo_destroy_texture_image(struct demo *demo, struct texture_object *tex_objs)
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700955{
956 /* clean up staging resources */
Mark Lobodzinski23182612015-05-29 09:32:35 -0500957 vkFreeMemory(demo->device, tex_objs->mem);
Tony Barbourde4124d2015-07-03 10:33:54 -0600958 vkDestroyImage(demo->device, tex_objs->image);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700959}
960
961static void demo_prepare_textures(struct demo *demo)
962{
Tony Barbour8205d902015-04-16 15:59:00 -0600963 const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600964 VkFormatProperties props;
Tony Barbour22a30862015-04-22 09:02:32 -0600965 VkResult U_ASSERT_ONLY err;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600966 uint32_t i;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600967
Courtney Goeltzenleuchter4da96aa2015-07-12 12:52:09 -0600968 err = vkGetPhysicalDeviceFormatProperties(demo->gpu, tex_format, &props);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700969 assert(!err);
970
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600971 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700972
FslNopper434db6a2015-05-06 21:42:01 +0200973 if ((props.linearTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) && !demo->use_staging_buffer) {
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700974 /* Device can texture using linear textures */
975 demo_prepare_texture_image(demo, tex_files[i], &demo->textures[i],
Courtney Goeltzenleuchtercb67a322015-04-21 09:31:23 -0600976 VK_IMAGE_TILING_LINEAR, VK_IMAGE_USAGE_SAMPLED_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
Tony Barbour8205d902015-04-16 15:59:00 -0600977 } else if (props.optimalTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) {
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700978 /* Must use staging buffer to copy linear texture to optimized */
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600979 struct texture_object staging_texture;
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700980
981 memset(&staging_texture, 0, sizeof(staging_texture));
982 demo_prepare_texture_image(demo, tex_files[i], &staging_texture,
Courtney Goeltzenleuchtercb67a322015-04-21 09:31:23 -0600983 VK_IMAGE_TILING_LINEAR, VK_IMAGE_USAGE_TRANSFER_SOURCE_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700984
985 demo_prepare_texture_image(demo, tex_files[i], &demo->textures[i],
Courtney Goeltzenleuchtercb67a322015-04-21 09:31:23 -0600986 VK_IMAGE_TILING_OPTIMAL,
987 (VK_IMAGE_USAGE_TRANSFER_DESTINATION_BIT | VK_IMAGE_USAGE_SAMPLED_BIT),
988 VK_MEMORY_PROPERTY_DEVICE_ONLY);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700989
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600990 demo_set_image_layout(demo, staging_texture.image,
malnasse4b8ba4d2015-06-03 17:28:38 -0400991 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600992 staging_texture.imageLayout,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600993 VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700994
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600995 demo_set_image_layout(demo, demo->textures[i].image,
malnasse4b8ba4d2015-06-03 17:28:38 -0400996 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600997 demo->textures[i].imageLayout,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600998 VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700999
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001000 VkImageCopy copy_region = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001001 .srcSubresource = { VK_IMAGE_ASPECT_COLOR, 0, 0 },
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001002 .srcOffset = { 0, 0, 0 },
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001003 .destSubresource = { VK_IMAGE_ASPECT_COLOR, 0, 0 },
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001004 .destOffset = { 0, 0, 0 },
1005 .extent = { staging_texture.tex_width, staging_texture.tex_height, 1 },
1006 };
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001007 vkCmdCopyImage(demo->cmd,
1008 staging_texture.image, VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL,
1009 demo->textures[i].image, VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL,
Courtney Goeltzenleuchter51cbf302015-03-25 11:25:10 -06001010 1, &copy_region);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001011
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -06001012 demo_set_image_layout(demo, demo->textures[i].image,
malnasse4b8ba4d2015-06-03 17:28:38 -04001013 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001014 VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -06001015 demo->textures[i].imageLayout);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001016
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -06001017 demo_flush_init_cmd(demo);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001018
Courtney Goeltzenleuchter876629f2015-04-21 09:30:03 -06001019 demo_destroy_texture_image(demo, &staging_texture);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001020 } else {
Mike Stroyande24a6f2015-06-15 14:21:03 -06001021 /* Can't support VK_FORMAT_R8G8B8A8_UNORM !? */
1022 assert(!"No support for R8G8B8A8_UNORM as texture image format");
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001023 }
1024
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001025 const VkSamplerCreateInfo sampler = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001026 .sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001027 .pNext = NULL,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001028 .magFilter = VK_TEX_FILTER_NEAREST,
1029 .minFilter = VK_TEX_FILTER_NEAREST,
Tony Barbour8205d902015-04-16 15:59:00 -06001030 .mipMode = VK_TEX_MIPMAP_MODE_BASE,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001031 .addressU = VK_TEX_ADDRESS_CLAMP,
1032 .addressV = VK_TEX_ADDRESS_CLAMP,
1033 .addressW = VK_TEX_ADDRESS_CLAMP,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001034 .mipLodBias = 0.0f,
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001035 .maxAnisotropy = 1,
Tony Barbour8205d902015-04-16 15:59:00 -06001036 .compareOp = VK_COMPARE_OP_NEVER,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001037 .minLod = 0.0f,
1038 .maxLod = 0.0f,
Tony Barbour2c4e7c72015-06-25 16:56:44 -06001039 .borderColor = VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001040 };
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -06001041
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001042 VkImageViewCreateInfo view = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001043 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001044 .pNext = NULL,
Tony Barbourde4124d2015-07-03 10:33:54 -06001045 .image.handle = VK_NULL_HANDLE,
Tony Barbour8205d902015-04-16 15:59:00 -06001046 .viewType = VK_IMAGE_VIEW_TYPE_2D,
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001047 .format = tex_format,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001048 .channels = { VK_CHANNEL_SWIZZLE_R,
1049 VK_CHANNEL_SWIZZLE_G,
1050 VK_CHANNEL_SWIZZLE_B,
1051 VK_CHANNEL_SWIZZLE_A, },
1052 .subresourceRange = { VK_IMAGE_ASPECT_COLOR, 0, 1, 0, 1 },
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001053 };
Jon Ashburna9ae3832015-01-16 09:37:43 -07001054
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001055 /* create sampler */
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001056 err = vkCreateSampler(demo->device, &sampler,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001057 &demo->textures[i].sampler);
1058 assert(!err);
1059
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001060 /* create image view */
1061 view.image = demo->textures[i].image;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001062 err = vkCreateImageView(demo->device, &view,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001063 &demo->textures[i].view);
1064 assert(!err);
1065 }
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001066}
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001067
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001068void demo_prepare_cube_data_buffer(struct demo *demo)
1069{
Courtney Goeltzenleuchterddcb6192015-04-14 18:48:46 -06001070 VkBufferCreateInfo buf_info;
1071 VkBufferViewCreateInfo view_info;
Courtney Goeltzenleuchterddcb6192015-04-14 18:48:46 -06001072 VkMemoryAllocInfo alloc_info = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001073 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
Mark Lobodzinski97dcd042015-04-16 08:52:00 -05001074 .pNext = NULL,
Jon Ashburnc6ae13d2015-01-19 15:00:26 -07001075 .allocationSize = 0,
Mark Lobodzinski72346292015-07-02 16:49:40 -06001076 .memoryTypeIndex = 0,
Jon Ashburnc6ae13d2015-01-19 15:00:26 -07001077 };
Mark Lobodzinski23182612015-05-29 09:32:35 -05001078 VkMemoryRequirements mem_reqs;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001079 size_t mem_reqs_size = sizeof(VkMemoryRequirements);
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001080 uint8_t *pData;
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001081 int i;
1082 mat4x4 MVP, VP;
Tony Barbour22a30862015-04-22 09:02:32 -06001083 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001084 struct vktexcube_vs_uniform data;
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001085
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001086 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001087 mat4x4_mul(MVP, VP, demo->model_matrix);
1088 memcpy(data.mvp, MVP, sizeof(MVP));
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -06001089// dumpMatrix("MVP", MVP);
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001090
1091 for (i=0; i<12*3; i++) {
1092 data.position[i][0] = g_vertex_buffer_data[i*3];
1093 data.position[i][1] = g_vertex_buffer_data[i*3+1];
1094 data.position[i][2] = g_vertex_buffer_data[i*3+2];
1095 data.position[i][3] = 1.0f;
1096 data.attr[i][0] = g_uv_buffer_data[2*i];
1097 data.attr[i][1] = g_uv_buffer_data[2*i + 1];
1098 data.attr[i][2] = 0;
1099 data.attr[i][3] = 0;
1100 }
1101
Chia-I Wu714df452015-01-01 07:55:04 +08001102 memset(&buf_info, 0, sizeof(buf_info));
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001103 buf_info.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
Courtney Goeltzenleuchterad870812015-04-15 15:29:59 -06001104 buf_info.usage = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001105 err = vkCreateBuffer(demo->device, &buf_info, &demo->uniform_data.buf);
Chia-I Wu714df452015-01-01 07:55:04 +08001106 assert(!err);
1107
Tony Barbourde4124d2015-07-03 10:33:54 -06001108 err = vkGetBufferMemoryRequirements(demo->device, demo->uniform_data.buf, &mem_reqs);
Mark Lobodzinski23182612015-05-29 09:32:35 -05001109 assert(!err && mem_reqs_size == sizeof(mem_reqs));
Chia-I Wu714df452015-01-01 07:55:04 +08001110
Mark Lobodzinski23182612015-05-29 09:32:35 -05001111 alloc_info.allocationSize = mem_reqs.size;
Mark Lobodzinski72346292015-07-02 16:49:40 -06001112 err = memory_type_from_properties(demo,
1113 mem_reqs.memoryTypeBits,
1114 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT,
1115 &alloc_info.memoryTypeIndex);
1116 assert(!err);
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001117
Mark Lobodzinski23182612015-05-29 09:32:35 -05001118 err = vkAllocMemory(demo->device, &alloc_info, &(demo->uniform_data.mem));
1119 assert(!err);
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001120
Mark Lobodzinski23182612015-05-29 09:32:35 -05001121 err = vkMapMemory(demo->device, demo->uniform_data.mem, 0, 0, 0, (void **) &pData);
1122 assert(!err);
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001123
Mark Lobodzinski23182612015-05-29 09:32:35 -05001124 memcpy(pData, &data, sizeof data);
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001125
Mark Lobodzinski23182612015-05-29 09:32:35 -05001126 err = vkUnmapMemory(demo->device, demo->uniform_data.mem);
1127 assert(!err);
1128
Tony Barbourde4124d2015-07-03 10:33:54 -06001129 err = vkBindBufferMemory(demo->device,
1130 demo->uniform_data.buf,
Mark Lobodzinski23182612015-05-29 09:32:35 -05001131 demo->uniform_data.mem, 0);
1132 assert(!err);
Chia-I Wu714df452015-01-01 07:55:04 +08001133
1134 memset(&view_info, 0, sizeof(view_info));
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001135 view_info.sType = VK_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO;
Chia-I Wu714df452015-01-01 07:55:04 +08001136 view_info.buffer = demo->uniform_data.buf;
Tony Barbour8205d902015-04-16 15:59:00 -06001137 view_info.viewType = VK_BUFFER_VIEW_TYPE_RAW;
Chia-I Wu714df452015-01-01 07:55:04 +08001138 view_info.offset = 0;
1139 view_info.range = sizeof(data);
1140
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001141 err = vkCreateBufferView(demo->device, &view_info, &demo->uniform_data.view);
Chia-I Wu714df452015-01-01 07:55:04 +08001142 assert(!err);
1143
Chia-I Wu8cd8ecd2015-05-25 16:27:55 +08001144 demo->uniform_data.desc.bufferView = demo->uniform_data.view;
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001145}
1146
Chia-I Wuf8385062015-01-04 16:27:24 +08001147static void demo_prepare_descriptor_layout(struct demo *demo)
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001148{
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001149 const VkDescriptorSetLayoutBinding layout_bindings[2] = {
Chia-I Wufc9d9132015-03-26 15:04:41 +08001150 [0] = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001151 .descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
Chia-I Wud3114a22015-05-25 16:22:52 +08001152 .arraySize = 1,
Tony Barbour8205d902015-04-16 15:59:00 -06001153 .stageFlags = VK_SHADER_STAGE_VERTEX_BIT,
Chia-I Wu310eece2015-03-27 12:56:09 +08001154 .pImmutableSamplers = NULL,
Chia-I Wufc9d9132015-03-26 15:04:41 +08001155 },
1156 [1] = {
Courtney Goeltzenleuchterad870812015-04-15 15:29:59 -06001157 .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
Chia-I Wud3114a22015-05-25 16:22:52 +08001158 .arraySize = DEMO_TEXTURE_COUNT,
Tony Barbour8205d902015-04-16 15:59:00 -06001159 .stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT,
Chia-I Wu310eece2015-03-27 12:56:09 +08001160 .pImmutableSamplers = NULL,
Chia-I Wufc9d9132015-03-26 15:04:41 +08001161 },
1162 };
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001163 const VkDescriptorSetLayoutCreateInfo descriptor_layout = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001164 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
Chia-I Wuf8385062015-01-04 16:27:24 +08001165 .pNext = NULL,
Chia-I Wufc9d9132015-03-26 15:04:41 +08001166 .count = 2,
1167 .pBinding = layout_bindings,
Chia-I Wuf8385062015-01-04 16:27:24 +08001168 };
Tony Barbour22a30862015-04-22 09:02:32 -06001169 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001170
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001171 err = vkCreateDescriptorSetLayout(demo->device,
Chia-I Wu7732cb22015-03-26 15:27:55 +08001172 &descriptor_layout, &demo->desc_layout);
1173 assert(!err);
1174
Mark Lobodzinski556f7212015-04-17 14:11:39 -05001175 const VkPipelineLayoutCreateInfo pPipelineLayoutCreateInfo = {
1176 .sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
1177 .pNext = NULL,
1178 .descriptorSetCount = 1,
1179 .pSetLayouts = &demo->desc_layout,
1180 };
1181
1182 err = vkCreatePipelineLayout(demo->device,
1183 &pPipelineLayoutCreateInfo,
1184 &demo->pipeline_layout);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001185 assert(!err);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001186}
1187
Chia-I Wu76cd4222015-07-08 13:34:24 +08001188static void demo_prepare_render_pass(struct demo *demo)
1189{
Chia-I Wuc278df82015-07-07 11:50:03 +08001190 const VkAttachmentDescription attachments[2] = {
1191 [0] = {
1192 .sType = VK_STRUCTURE_TYPE_ATTACHMENT_DESCRIPTION,
1193 .pNext = NULL,
1194 .format = demo->format,
1195 .samples = 1,
1196 .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
1197 .storeOp = VK_ATTACHMENT_STORE_OP_STORE,
1198 .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
1199 .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1200 .initialLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
1201 .finalLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
1202 },
1203 [1] = {
1204 .sType = VK_STRUCTURE_TYPE_ATTACHMENT_DESCRIPTION,
1205 .pNext = NULL,
1206 .format = demo->depth.format,
1207 .samples = 1,
1208 .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
1209 .storeOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1210 .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
1211 .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1212 .initialLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1213 .finalLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1214 },
Chia-I Wu76cd4222015-07-08 13:34:24 +08001215 };
Chia-I Wuc278df82015-07-07 11:50:03 +08001216 const VkAttachmentReference color_reference = {
1217 .attachment = 0,
1218 .layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
1219 };
1220 const VkSubpassDescription subpass = {
1221 .sType = VK_STRUCTURE_TYPE_SUBPASS_DESCRIPTION,
1222 .pNext = NULL,
1223 .pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS,
1224 .flags = 0,
1225 .inputCount = 0,
1226 .inputAttachments = NULL,
1227 .colorCount = 1,
1228 .colorAttachments = &color_reference,
1229 .resolveAttachments = NULL,
1230 .depthStencilAttachment = {
1231 .attachment = 1,
1232 .layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1233 },
1234 .preserveCount = 0,
1235 .preserveAttachments = NULL,
1236 };
Chia-I Wu76cd4222015-07-08 13:34:24 +08001237 const VkRenderPassCreateInfo rp_info = {
1238 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO,
1239 .pNext = NULL,
Chia-I Wuc278df82015-07-07 11:50:03 +08001240 .attachmentCount = 2,
1241 .pAttachments = attachments,
1242 .subpassCount = 1,
1243 .pSubpasses = &subpass,
1244 .dependencyCount = 0,
1245 .pDependencies = NULL,
Chia-I Wu76cd4222015-07-08 13:34:24 +08001246 };
Chia-I Wuc278df82015-07-07 11:50:03 +08001247 VkResult U_ASSERT_ONLY err;
Chia-I Wu76cd4222015-07-08 13:34:24 +08001248
1249 err = vkCreateRenderPass(demo->device, &rp_info, &demo->render_pass);
1250 assert(!err);
1251}
1252
Tobin Ehlise796a1d2015-07-02 11:02:49 -06001253static VkShader demo_prepare_shader(struct demo* demo,
Tony Barbour8205d902015-04-16 15:59:00 -06001254 VkShaderStage stage,
Tobin Ehlise796a1d2015-07-02 11:02:49 -06001255 const void* code,
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001256 size_t size)
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001257{
Courtney Goeltzenleuchter0b29b0d2015-06-24 18:24:19 -06001258 VkShaderModuleCreateInfo moduleCreateInfo;
1259 VkShaderCreateInfo shaderCreateInfo;
1260 VkShaderModule shaderModule;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001261 VkShader shader;
1262 VkResult err;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001263
Courtney Goeltzenleuchter7e334982014-10-30 15:14:16 -06001264
Courtney Goeltzenleuchter0b29b0d2015-06-24 18:24:19 -06001265 moduleCreateInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
1266 moduleCreateInfo.pNext = NULL;
1267
1268 shaderCreateInfo.sType = VK_STRUCTURE_TYPE_SHADER_CREATE_INFO;
1269 shaderCreateInfo.pNext = NULL;
Tobin Ehlise796a1d2015-07-02 11:02:49 -06001270 shaderCreateInfo.pName = "main";
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001271
Cody Northrop75db0322015-05-28 11:27:16 -06001272 if (!demo->use_glsl) {
Courtney Goeltzenleuchter0b29b0d2015-06-24 18:24:19 -06001273 moduleCreateInfo.codeSize = size;
1274 moduleCreateInfo.pCode = code;
1275 moduleCreateInfo.flags = 0;
1276 err = vkCreateShaderModule(demo->device, &moduleCreateInfo, &shaderModule);
Cody Northrop75db0322015-05-28 11:27:16 -06001277 if (err) {
Courtney Goeltzenleuchter0b29b0d2015-06-24 18:24:19 -06001278 free((void *) moduleCreateInfo.pCode);
Cody Northrop75db0322015-05-28 11:27:16 -06001279 }
Courtney Goeltzenleuchter0b29b0d2015-06-24 18:24:19 -06001280
1281 shaderCreateInfo.flags = 0;
1282 shaderCreateInfo.module = shaderModule;
1283 err = vkCreateShader(demo->device, &shaderCreateInfo, &shader);
Cody Northrop75db0322015-05-28 11:27:16 -06001284 } else {
1285 // Create fake SPV structure to feed GLSL
1286 // to the driver "under the covers"
Courtney Goeltzenleuchter0b29b0d2015-06-24 18:24:19 -06001287 moduleCreateInfo.codeSize = 3 * sizeof(uint32_t) + size + 1;
1288 moduleCreateInfo.pCode = malloc(moduleCreateInfo.codeSize);
1289 moduleCreateInfo.flags = 0;
Cody Northrop75db0322015-05-28 11:27:16 -06001290
1291 /* try version 0 first: VkShaderStage followed by GLSL */
Courtney Goeltzenleuchter0b29b0d2015-06-24 18:24:19 -06001292 ((uint32_t *) moduleCreateInfo.pCode)[0] = ICD_SPV_MAGIC;
1293 ((uint32_t *) moduleCreateInfo.pCode)[1] = 0;
1294 ((uint32_t *) moduleCreateInfo.pCode)[2] = stage;
1295 memcpy(((uint32_t *) moduleCreateInfo.pCode + 3), code, size + 1);
Cody Northrop75db0322015-05-28 11:27:16 -06001296
Courtney Goeltzenleuchter0b29b0d2015-06-24 18:24:19 -06001297 err = vkCreateShaderModule(demo->device, &moduleCreateInfo, &shaderModule);
Cody Northrop75db0322015-05-28 11:27:16 -06001298 if (err) {
Courtney Goeltzenleuchter0b29b0d2015-06-24 18:24:19 -06001299 free((void *) moduleCreateInfo.pCode);
Cody Northrop75db0322015-05-28 11:27:16 -06001300 }
Courtney Goeltzenleuchter0b29b0d2015-06-24 18:24:19 -06001301
1302 shaderCreateInfo.flags = 0;
1303 shaderCreateInfo.module = shaderModule;
1304 err = vkCreateShader(demo->device, &shaderCreateInfo, &shader);
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -06001305 }
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001306 return shader;
1307}
1308
Cody Northropacfb0492015-03-17 15:55:58 -06001309char *demo_read_spv(const char *filename, size_t *psize)
Courtney Goeltzenleuchter7e334982014-10-30 15:14:16 -06001310{
1311 long int size;
Tony Barboura938abb2015-04-22 11:36:22 -06001312 size_t U_ASSERT_ONLY retval;
Courtney Goeltzenleuchter7e334982014-10-30 15:14:16 -06001313 void *shader_code;
1314
1315 FILE *fp = fopen(filename, "rb");
1316 if (!fp) return NULL;
1317
1318 fseek(fp, 0L, SEEK_END);
1319 size = ftell(fp);
1320
1321 fseek(fp, 0L, SEEK_SET);
1322
1323 shader_code = malloc(size);
Tony Barbour22a30862015-04-22 09:02:32 -06001324 retval = fread(shader_code, size, 1, fp);
1325 assert(retval == 1);
Courtney Goeltzenleuchter7e334982014-10-30 15:14:16 -06001326
1327 *psize = size;
1328
1329 return shader_code;
1330}
1331
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001332static VkShader demo_prepare_vs(struct demo *demo)
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001333{
Cody Northrop75db0322015-05-28 11:27:16 -06001334 if (!demo->use_glsl) {
1335 void *vertShaderCode;
1336 size_t size;
Courtney Goeltzenleuchter7e334982014-10-30 15:14:16 -06001337
Cody Northrop75db0322015-05-28 11:27:16 -06001338 vertShaderCode = demo_read_spv("cube-vert.spv", &size);
Courtney Goeltzenleuchter7e334982014-10-30 15:14:16 -06001339
Cody Northrop75db0322015-05-28 11:27:16 -06001340 return demo_prepare_shader(demo, VK_SHADER_STAGE_VERTEX,
1341 vertShaderCode, size);
1342 } else {
1343 static const char *vertShaderText =
1344 "#version 140\n"
1345 "#extension GL_ARB_separate_shader_objects : enable\n"
1346 "#extension GL_ARB_shading_language_420pack : enable\n"
1347 "\n"
1348 "layout(binding = 0) uniform buf {\n"
1349 " mat4 MVP;\n"
1350 " vec4 position[12*3];\n"
1351 " vec4 attr[12*3];\n"
1352 "} ubuf;\n"
1353 "\n"
1354 "layout (location = 0) out vec4 texcoord;\n"
1355 "\n"
1356 "void main() \n"
1357 "{\n"
1358 " texcoord = ubuf.attr[gl_VertexID];\n"
1359 " gl_Position = ubuf.MVP * ubuf.position[gl_VertexID];\n"
1360 "\n"
1361 " // GL->VK conventions\n"
1362 " gl_Position.y = -gl_Position.y;\n"
1363 " gl_Position.z = (gl_Position.z + gl_Position.w) / 2.0;\n"
1364 "}\n";
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -06001365
Cody Northrop75db0322015-05-28 11:27:16 -06001366 return demo_prepare_shader(demo, VK_SHADER_STAGE_VERTEX,
1367 (const void *) vertShaderText,
1368 strlen(vertShaderText));
1369 }
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001370}
1371
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001372static VkShader demo_prepare_fs(struct demo *demo)
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001373{
Cody Northrop75db0322015-05-28 11:27:16 -06001374 if (!demo->use_glsl) {
1375 void *fragShaderCode;
1376 size_t size;
Courtney Goeltzenleuchter7e334982014-10-30 15:14:16 -06001377
Cody Northrop75db0322015-05-28 11:27:16 -06001378 fragShaderCode = demo_read_spv("cube-frag.spv", &size);
Courtney Goeltzenleuchter7e334982014-10-30 15:14:16 -06001379
Cody Northrop75db0322015-05-28 11:27:16 -06001380 return demo_prepare_shader(demo, VK_SHADER_STAGE_FRAGMENT,
1381 fragShaderCode, size);
1382 } else {
1383 static const char *fragShaderText =
1384 "#version 140\n"
1385 "#extension GL_ARB_separate_shader_objects : enable\n"
1386 "#extension GL_ARB_shading_language_420pack : enable\n"
1387 "layout (binding = 1) uniform sampler2D tex;\n"
1388 "\n"
1389 "layout (location = 0) in vec4 texcoord;\n"
1390 "layout (location = 0) out vec4 uFragColor;\n"
1391 "void main() {\n"
1392 " uFragColor = texture(tex, texcoord.xy);\n"
1393 "}\n";
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -06001394
Cody Northrop75db0322015-05-28 11:27:16 -06001395 return demo_prepare_shader(demo, VK_SHADER_STAGE_FRAGMENT,
1396 (const void *) fragShaderText,
1397 strlen(fragShaderText));
1398 }
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001399}
1400
1401static void demo_prepare_pipeline(struct demo *demo)
1402{
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001403 VkGraphicsPipelineCreateInfo pipeline;
Jon Ashburn0d60d272015-07-09 15:02:25 -06001404 VkPipelineCacheCreateInfo pipelineCache;
Tony Barboure307f582015-07-10 15:29:03 -06001405 VkPipelineInputAssemblyStateCreateInfo ia;
1406 VkPipelineRasterStateCreateInfo rs;
1407 VkPipelineColorBlendStateCreateInfo cb;
1408 VkPipelineDepthStencilStateCreateInfo ds;
1409 VkPipelineViewportStateCreateInfo vp;
1410 VkPipelineMultisampleStateCreateInfo ms;
Tony Barbour22a30862015-04-22 09:02:32 -06001411 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001412
1413 memset(&pipeline, 0, sizeof(pipeline));
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001414 pipeline.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
Mark Lobodzinski556f7212015-04-17 14:11:39 -05001415 pipeline.layout = demo->pipeline_layout;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001416
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001417 memset(&ia, 0, sizeof(ia));
Tony Barboure307f582015-07-10 15:29:03 -06001418 ia.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
Tony Barbour8205d902015-04-16 15:59:00 -06001419 ia.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001420
1421 memset(&rs, 0, sizeof(rs));
Tony Barboure307f582015-07-10 15:29:03 -06001422 rs.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTER_STATE_CREATE_INFO;
Tony Barbour8205d902015-04-16 15:59:00 -06001423 rs.fillMode = VK_FILL_MODE_SOLID;
1424 rs.cullMode = VK_CULL_MODE_BACK;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001425 rs.frontFace = VK_FRONT_FACE_CCW;
Chia-I Wue2504cb2015-04-22 14:20:52 +08001426 rs.depthClipEnable = VK_TRUE;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001427
1428 memset(&cb, 0, sizeof(cb));
Tony Barboure307f582015-07-10 15:29:03 -06001429 cb.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
1430 VkPipelineColorBlendAttachmentState att_state[1];
Tony Barbourfa6cac72015-01-16 14:27:35 -07001431 memset(att_state, 0, sizeof(att_state));
Tony Barbourfa6cac72015-01-16 14:27:35 -07001432 att_state[0].channelWriteMask = 0xf;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001433 att_state[0].blendEnable = VK_FALSE;
Tony Barbourfa6cac72015-01-16 14:27:35 -07001434 cb.attachmentCount = 1;
1435 cb.pAttachments = att_state;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001436
Tony Barbourfa6cac72015-01-16 14:27:35 -07001437 memset(&vp, 0, sizeof(vp));
Tony Barboure307f582015-07-10 15:29:03 -06001438 vp.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
Tony Barbour8205d902015-04-16 15:59:00 -06001439 vp.viewportCount = 1;
Tony Barbourfa6cac72015-01-16 14:27:35 -07001440
1441 memset(&ds, 0, sizeof(ds));
Tony Barboure307f582015-07-10 15:29:03 -06001442 ds.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001443 ds.depthTestEnable = VK_TRUE;
1444 ds.depthWriteEnable = VK_TRUE;
Tony Barbour8205d902015-04-16 15:59:00 -06001445 ds.depthCompareOp = VK_COMPARE_OP_LESS_EQUAL;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001446 ds.depthBoundsEnable = VK_FALSE;
1447 ds.back.stencilFailOp = VK_STENCIL_OP_KEEP;
1448 ds.back.stencilPassOp = VK_STENCIL_OP_KEEP;
Tony Barbour8205d902015-04-16 15:59:00 -06001449 ds.back.stencilCompareOp = VK_COMPARE_OP_ALWAYS;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001450 ds.stencilTestEnable = VK_FALSE;
Tony Barbourfa6cac72015-01-16 14:27:35 -07001451 ds.front = ds.back;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001452
Tony Barbourfa6cac72015-01-16 14:27:35 -07001453 memset(&ms, 0, sizeof(ms));
Tony Barboure307f582015-07-10 15:29:03 -06001454 ms.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
Tony Barbourfa6cac72015-01-16 14:27:35 -07001455 ms.sampleMask = 1;
Tony Barboure094edf2015-06-26 10:18:34 -06001456 ms.rasterSamples = 1;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001457
Mark Lobodzinski0e0fb5c2015-06-23 15:11:57 -06001458 // Two stages: vs and fs
1459 pipeline.stageCount = 2;
1460 VkPipelineShaderStageCreateInfo shaderStages[2];
1461 memset(&shaderStages, 0, 2 * sizeof(VkPipelineShaderStageCreateInfo));
1462
1463 shaderStages[0].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1464 shaderStages[0].stage = VK_SHADER_STAGE_VERTEX;
1465 shaderStages[0].shader = demo_prepare_vs(demo);
1466
1467 shaderStages[1].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1468 shaderStages[1].stage = VK_SHADER_STAGE_FRAGMENT;
1469 shaderStages[1].shader = demo_prepare_fs(demo);
1470
Jon Ashburn0d60d272015-07-09 15:02:25 -06001471 memset(&pipelineCache, 0, sizeof(pipelineCache));
1472 pipelineCache.sType = VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001473
Jon Ashburn0d60d272015-07-09 15:02:25 -06001474 err = vkCreatePipelineCache(demo->device, &pipelineCache, &demo->pipelineCache);
1475 assert(!err);
Tony Barboure307f582015-07-10 15:29:03 -06001476
1477 pipeline.pVertexInputState = NULL;
1478 pipeline.pInputAssemblyState = &ia;
1479 pipeline.pRasterState = &rs;
1480 pipeline.pColorBlendState = &cb;
1481 pipeline.pMultisampleState = &ms;
1482 pipeline.pViewportState = &vp;
1483 pipeline.pDepthStencilState = &ds;
1484 pipeline.pStages = shaderStages;
1485
Jon Ashburn0d60d272015-07-09 15:02:25 -06001486 err = vkCreateGraphicsPipelines(demo->device, demo->pipelineCache, 1, &pipeline, &demo->pipeline);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001487 assert(!err);
1488
Mark Lobodzinski0e0fb5c2015-06-23 15:11:57 -06001489 for (uint32_t i = 0; i < pipeline.stageCount; i++) {
Tony Barbourde4124d2015-07-03 10:33:54 -06001490 vkDestroyShader(demo->device, shaderStages[i].shader);
Mark Lobodzinski0e0fb5c2015-06-23 15:11:57 -06001491 }
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001492}
1493
1494static void demo_prepare_dynamic_states(struct demo *demo)
1495{
Tony Barbourde4124d2015-07-03 10:33:54 -06001496 VkDynamicViewportStateCreateInfo viewport_create;
1497 VkDynamicRasterStateCreateInfo raster;
1498 VkDynamicColorBlendStateCreateInfo color_blend;
1499 VkDynamicDepthStencilStateCreateInfo depth_stencil;
Tony Barbour22a30862015-04-22 09:02:32 -06001500 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001501
Tony Barbourfa6cac72015-01-16 14:27:35 -07001502 memset(&viewport_create, 0, sizeof(viewport_create));
Tony Barbourde4124d2015-07-03 10:33:54 -06001503 viewport_create.sType = VK_STRUCTURE_TYPE_DYNAMIC_VIEWPORT_STATE_CREATE_INFO;
Courtney Goeltzenleuchterc6e32f92015-02-11 14:13:34 -07001504 viewport_create.viewportAndScissorCount = 1;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001505 VkViewport viewport;
Piers Daniell886be472015-02-23 16:23:13 -07001506 memset(&viewport, 0, sizeof(viewport));
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001507 viewport.height = (float) demo->height;
1508 viewport.width = (float) demo->width;
1509 viewport.minDepth = (float) 0.0f;
1510 viewport.maxDepth = (float) 1.0f;
Piers Daniell886be472015-02-23 16:23:13 -07001511 viewport_create.pViewports = &viewport;
Chris Forbes2951d7d2015-06-22 17:21:59 +12001512 VkRect2D scissor;
Piers Daniell886be472015-02-23 16:23:13 -07001513 memset(&scissor, 0, sizeof(scissor));
Courtney Goeltzenleuchterc6e32f92015-02-11 14:13:34 -07001514 scissor.extent.width = demo->width;
1515 scissor.extent.height = demo->height;
1516 scissor.offset.x = 0;
1517 scissor.offset.y = 0;
Courtney Goeltzenleuchterc6e32f92015-02-11 14:13:34 -07001518 viewport_create.pScissors = &scissor;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001519
1520 memset(&raster, 0, sizeof(raster));
Tony Barbourde4124d2015-07-03 10:33:54 -06001521 raster.sType = VK_STRUCTURE_TYPE_DYNAMIC_RASTER_STATE_CREATE_INFO;
Piers Daniell886be472015-02-23 16:23:13 -07001522 raster.lineWidth = 1.0;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001523
1524 memset(&color_blend, 0, sizeof(color_blend));
Tony Barbourde4124d2015-07-03 10:33:54 -06001525 color_blend.sType = VK_STRUCTURE_TYPE_DYNAMIC_COLOR_BLEND_STATE_CREATE_INFO;
Piers Daniell886be472015-02-23 16:23:13 -07001526 color_blend.blendConst[0] = 1.0f;
1527 color_blend.blendConst[1] = 1.0f;
1528 color_blend.blendConst[2] = 1.0f;
1529 color_blend.blendConst[3] = 1.0f;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001530
1531 memset(&depth_stencil, 0, sizeof(depth_stencil));
Tony Barbourde4124d2015-07-03 10:33:54 -06001532 depth_stencil.sType = VK_STRUCTURE_TYPE_DYNAMIC_DEPTH_STENCIL_STATE_CREATE_INFO;
Mark Lobodzinski4405fbf2015-06-12 11:14:17 -06001533 depth_stencil.minDepthBounds = 0.0f;
1534 depth_stencil.maxDepthBounds = 1.0f;
Tony Barbourfa6cac72015-01-16 14:27:35 -07001535 depth_stencil.stencilBackRef = 0;
1536 depth_stencil.stencilFrontRef = 0;
1537 depth_stencil.stencilReadMask = 0xff;
1538 depth_stencil.stencilWriteMask = 0xff;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001539
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001540 err = vkCreateDynamicViewportState(demo->device, &viewport_create, &demo->viewport);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001541 assert(!err);
1542
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001543 err = vkCreateDynamicRasterState(demo->device, &raster, &demo->raster);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001544 assert(!err);
1545
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001546 err = vkCreateDynamicColorBlendState(demo->device,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001547 &color_blend, &demo->color_blend);
1548 assert(!err);
1549
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001550 err = vkCreateDynamicDepthStencilState(demo->device,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001551 &depth_stencil, &demo->depth_stencil);
1552 assert(!err);
1553}
1554
Chia-I Wu8d24b3b2015-03-26 13:14:16 +08001555static void demo_prepare_descriptor_pool(struct demo *demo)
Chia-I Wuf8385062015-01-04 16:27:24 +08001556{
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001557 const VkDescriptorTypeCount type_counts[2] = {
Chia-I Wuf8385062015-01-04 16:27:24 +08001558 [0] = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001559 .type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
Chia-I Wuf8385062015-01-04 16:27:24 +08001560 .count = 1,
1561 },
1562 [1] = {
Courtney Goeltzenleuchterad870812015-04-15 15:29:59 -06001563 .type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
Chia-I Wuf8385062015-01-04 16:27:24 +08001564 .count = DEMO_TEXTURE_COUNT,
1565 },
1566 };
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001567 const VkDescriptorPoolCreateInfo descriptor_pool = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001568 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO,
Chia-I Wuf8385062015-01-04 16:27:24 +08001569 .pNext = NULL,
1570 .count = 2,
1571 .pTypeCount = type_counts,
1572 };
Tony Barbour22a30862015-04-22 09:02:32 -06001573 VkResult U_ASSERT_ONLY err;
Chia-I Wuf8385062015-01-04 16:27:24 +08001574
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001575 err = vkCreateDescriptorPool(demo->device,
1576 VK_DESCRIPTOR_POOL_USAGE_ONE_SHOT, 1,
Chia-I Wu8d24b3b2015-03-26 13:14:16 +08001577 &descriptor_pool, &demo->desc_pool);
Chia-I Wuf8385062015-01-04 16:27:24 +08001578 assert(!err);
1579}
1580
1581static void demo_prepare_descriptor_set(struct demo *demo)
1582{
Chia-I Wu8cd8ecd2015-05-25 16:27:55 +08001583 VkDescriptorInfo tex_descs[DEMO_TEXTURE_COUNT];
1584 VkWriteDescriptorSet writes[2];
Tony Barbour22a30862015-04-22 09:02:32 -06001585 VkResult U_ASSERT_ONLY err;
Chia-I Wuf8385062015-01-04 16:27:24 +08001586 uint32_t count;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001587 uint32_t i;
Chia-I Wuf8385062015-01-04 16:27:24 +08001588
Mike Stroyan230e6252015-04-17 12:36:38 -06001589 err = vkAllocDescriptorSets(demo->device, demo->desc_pool,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001590 VK_DESCRIPTOR_SET_USAGE_STATIC,
Chia-I Wu6e68a892015-02-23 10:41:08 -07001591 1, &demo->desc_layout,
Chia-I Wuf8385062015-01-04 16:27:24 +08001592 &demo->desc_set, &count);
1593 assert(!err && count == 1);
1594
Chia-I Wu8cd8ecd2015-05-25 16:27:55 +08001595 memset(&tex_descs, 0, sizeof(tex_descs));
1596 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
1597 tex_descs[i].sampler = demo->textures[i].sampler;
1598 tex_descs[i].imageView = demo->textures[i].view;
1599 tex_descs[i].imageLayout = VK_IMAGE_LAYOUT_GENERAL;
1600 }
1601
1602 memset(&writes, 0, sizeof(writes));
1603
1604 writes[0].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
1605 writes[0].destSet = demo->desc_set;
1606 writes[0].count = 1;
1607 writes[0].descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
1608 writes[0].pDescriptors = &demo->uniform_data.desc;
1609
1610 writes[1].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
1611 writes[1].destSet = demo->desc_set;
1612 writes[1].destBinding = 1;
1613 writes[1].count = DEMO_TEXTURE_COUNT;
1614 writes[1].descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
1615 writes[1].pDescriptors = tex_descs;
1616
1617 err = vkUpdateDescriptorSets(demo->device, 2, writes, 0, NULL);
1618 assert(!err);
Chia-I Wuf8385062015-01-04 16:27:24 +08001619}
1620
Chia-I Wu76cd4222015-07-08 13:34:24 +08001621static void demo_prepare_framebuffers(struct demo *demo)
1622{
Chia-I Wuc278df82015-07-07 11:50:03 +08001623 VkAttachmentBindInfo attachments[2] = {
1624 [0] = {
Tobin Ehlis0b9c1952015-07-06 14:02:36 -06001625 .view.handle = VK_NULL_HANDLE,
Chia-I Wuc278df82015-07-07 11:50:03 +08001626 .layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
1627 },
1628 [1] = {
1629 .view = demo->depth.view,
1630 .layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1631 },
Chia-I Wu76cd4222015-07-08 13:34:24 +08001632 };
1633 const VkFramebufferCreateInfo fb_info = {
1634 .sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO,
1635 .pNext = NULL,
Chia-I Wuc278df82015-07-07 11:50:03 +08001636 .renderPass = demo->render_pass,
1637 .attachmentCount = 2,
1638 .pAttachments = attachments,
Chia-I Wu76cd4222015-07-08 13:34:24 +08001639 .width = demo->width,
1640 .height = demo->height,
1641 .layers = 1,
1642 };
1643 VkResult U_ASSERT_ONLY err;
1644 uint32_t i;
1645
1646 for (i = 0; i < DEMO_BUFFER_COUNT; i++) {
Chia-I Wuc278df82015-07-07 11:50:03 +08001647 attachments[0].view = demo->buffers[i].view;
Chia-I Wu76cd4222015-07-08 13:34:24 +08001648 err = vkCreateFramebuffer(demo->device, &fb_info, &demo->framebuffers[i]);
1649 assert(!err);
1650 }
1651}
1652
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001653static void demo_prepare(struct demo *demo)
1654{
Cody Northrop18ea11b2015-07-09 18:08:32 -06001655 VkResult U_ASSERT_ONLY err;
1656
1657 const VkCmdPoolCreateInfo cmd_pool_info = {
1658 .sType = VK_STRUCTURE_TYPE_CMD_POOL_CREATE_INFO,
1659 .pNext = NULL,
1660 .queueFamilyIndex = demo->graphics_queue_node_index,
1661 .flags = 0,
1662 };
1663 err = vkCreateCommandPool(demo->device, &cmd_pool_info, &demo->cmd_pool);
1664 assert(!err);
1665
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001666 const VkCmdBufferCreateInfo cmd = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001667 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001668 .pNext = NULL,
Cody Northrop18ea11b2015-07-09 18:08:32 -06001669 .cmdPool = demo->cmd_pool,
Chia-I Wu88eaa3b2015-06-26 15:34:39 +08001670 .level = VK_CMD_BUFFER_LEVEL_PRIMARY,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001671 .flags = 0,
1672 };
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001673
1674 demo_prepare_buffers(demo);
1675 demo_prepare_depth(demo);
1676 demo_prepare_textures(demo);
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -06001677 demo_prepare_cube_data_buffer(demo);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001678
Chia-I Wuf8385062015-01-04 16:27:24 +08001679 demo_prepare_descriptor_layout(demo);
Chia-I Wu76cd4222015-07-08 13:34:24 +08001680 demo_prepare_render_pass(demo);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001681 demo_prepare_pipeline(demo);
1682 demo_prepare_dynamic_states(demo);
1683
Courtney Goeltzenleuchterbd3b3aa2015-02-17 09:48:44 -07001684 for (int i = 0; i < DEMO_BUFFER_COUNT; i++) {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001685 err = vkCreateCommandBuffer(demo->device, &cmd, &demo->buffers[i].cmd);
Courtney Goeltzenleuchterbd3b3aa2015-02-17 09:48:44 -07001686 assert(!err);
1687 }
Chia-I Wuf8385062015-01-04 16:27:24 +08001688
Chia-I Wu8d24b3b2015-03-26 13:14:16 +08001689 demo_prepare_descriptor_pool(demo);
Chia-I Wuf8385062015-01-04 16:27:24 +08001690 demo_prepare_descriptor_set(demo);
Courtney Goeltzenleuchterbd3b3aa2015-02-17 09:48:44 -07001691
Chia-I Wu76cd4222015-07-08 13:34:24 +08001692 demo_prepare_framebuffers(demo);
Courtney Goeltzenleuchterbd3b3aa2015-02-17 09:48:44 -07001693
1694 for (int i = 0; i < DEMO_BUFFER_COUNT; i++) {
1695 demo->current_buffer = i;
1696 demo_draw_build_cmd(demo, demo->buffers[i].cmd);
1697 }
1698
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -06001699 /*
1700 * Prepare functions above may generate pipeline commands
1701 * that need to be flushed before beginning the render loop.
1702 */
1703 demo_flush_init_cmd(demo);
1704
Courtney Goeltzenleuchterbd3b3aa2015-02-17 09:48:44 -07001705 demo->current_buffer = 0;
Jon Ashburn8a399e92015-04-24 09:46:24 -07001706 demo->prepared = true;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001707}
1708
David Pinedoeeca2a22015-06-18 17:03:14 -06001709static void demo_cleanup(struct demo *demo)
1710{
1711 uint32_t i;
1712
1713 demo->prepared = false;
1714
Tony Barbourde4124d2015-07-03 10:33:54 -06001715 for (i = 0; i < DEMO_BUFFER_COUNT; i++) {
1716 vkDestroyFramebuffer(demo->device, demo->framebuffers[i]);
1717 }
Tony Barbourb857d312015-07-10 10:50:45 -06001718 vkFreeDescriptorSets(demo->device, demo->desc_pool, 1, &demo->desc_set);
Tony Barbourde4124d2015-07-03 10:33:54 -06001719 vkDestroyDescriptorPool(demo->device, demo->desc_pool);
Chia-I Wu76cd4222015-07-08 13:34:24 +08001720
Tony Barbourde4124d2015-07-03 10:33:54 -06001721 vkDestroyDynamicViewportState(demo->device, demo->viewport);
1722 vkDestroyDynamicRasterState(demo->device, demo->raster);
1723 vkDestroyDynamicColorBlendState(demo->device, demo->color_blend);
1724 vkDestroyDynamicDepthStencilState(demo->device, demo->depth_stencil);
David Pinedoeeca2a22015-06-18 17:03:14 -06001725
Tony Barbourde4124d2015-07-03 10:33:54 -06001726 vkDestroyPipeline(demo->device, demo->pipeline);
Jon Ashburn0d60d272015-07-09 15:02:25 -06001727 vkDestroyPipelineCache(demo->device, demo->pipelineCache);
Tony Barbourde4124d2015-07-03 10:33:54 -06001728 vkDestroyRenderPass(demo->device, demo->render_pass);
1729 vkDestroyPipelineLayout(demo->device, demo->pipeline_layout);
1730 vkDestroyDescriptorSetLayout(demo->device, demo->desc_layout);
David Pinedoeeca2a22015-06-18 17:03:14 -06001731
1732 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Tony Barbourde4124d2015-07-03 10:33:54 -06001733 vkDestroyImageView(demo->device, demo->textures[i].view);
1734 vkDestroyImage(demo->device, demo->textures[i].image);
David Pinedoeeca2a22015-06-18 17:03:14 -06001735 vkFreeMemory(demo->device, demo->textures[i].mem);
Tony Barbourde4124d2015-07-03 10:33:54 -06001736 vkDestroySampler(demo->device, demo->textures[i].sampler);
David Pinedoeeca2a22015-06-18 17:03:14 -06001737 }
1738 demo->fpDestroySwapChainWSI(demo->swap_chain);
1739
Tony Barbourde4124d2015-07-03 10:33:54 -06001740 vkDestroyAttachmentView(demo->device, demo->depth.view);
1741 vkDestroyImage(demo->device, demo->depth.image);
David Pinedoeeca2a22015-06-18 17:03:14 -06001742 vkFreeMemory(demo->device, demo->depth.mem);
1743
Tony Barbourde4124d2015-07-03 10:33:54 -06001744 vkDestroyBufferView(demo->device, demo->uniform_data.view);
1745 vkDestroyBuffer(demo->device, demo->uniform_data.buf);
David Pinedoeeca2a22015-06-18 17:03:14 -06001746 vkFreeMemory(demo->device, demo->uniform_data.mem);
1747
1748 for (i = 0; i < DEMO_BUFFER_COUNT; i++) {
Tony Barbourde4124d2015-07-03 10:33:54 -06001749 vkDestroyAttachmentView(demo->device, demo->buffers[i].view);
1750 vkDestroyCommandBuffer(demo->device, demo->buffers[i].cmd);
David Pinedoeeca2a22015-06-18 17:03:14 -06001751 }
1752
Cody Northrop18ea11b2015-07-09 18:08:32 -06001753 vkDestroyCommandPool(demo->device, demo->cmd_pool);
David Pinedoeeca2a22015-06-18 17:03:14 -06001754 vkDestroyDevice(demo->device);
Tony Barboura65ecc22015-06-30 14:14:19 -06001755 if (demo->validate) {
1756 demo->dbgDestroyMsgCallback(demo->inst, demo->msg_callback);
1757 }
David Pinedoeeca2a22015-06-18 17:03:14 -06001758 vkDestroyInstance(demo->inst);
1759
1760#ifndef _WIN32
1761 xcb_destroy_window(demo->connection, demo->window);
1762 xcb_disconnect(demo->connection);
1763#endif // _WIN32
1764}
1765
1766// On MS-Windows, make this a global, so it's available to WndProc()
1767struct demo demo;
1768
Ian Elliotte14e9f92015-04-16 15:23:05 -06001769#ifdef _WIN32
1770static void demo_run(struct demo *demo)
1771{
Courtney Goeltzenleuchter857542b2015-04-27 14:56:34 -06001772 if (!demo->prepared)
1773 return;
Ian Elliotte14e9f92015-04-16 15:23:05 -06001774 // Wait for work to finish before updating MVP.
1775 vkDeviceWaitIdle(demo->device);
1776 demo_update_data_buffer(demo);
1777
1778 demo_draw(demo);
1779
1780 // Wait for work to finish before updating MVP.
1781 vkDeviceWaitIdle(demo->device);
Ian Elliotte14e9f92015-04-16 15:23:05 -06001782
David Pinedoeeca2a22015-06-18 17:03:14 -06001783 demo->curFrame++;
1784
1785 if (demo->frameCount != INT_MAX && demo->curFrame == demo->frameCount)
1786 {
1787 demo->quit=true;
1788 demo_cleanup(demo);
1789 ExitProcess(0);
1790 }
1791
1792}
Ian Elliotte14e9f92015-04-16 15:23:05 -06001793
1794// MS-Windows event handling function:
1795LRESULT CALLBACK WndProc(HWND hWnd,
1796 UINT uMsg,
1797 WPARAM wParam,
1798 LPARAM lParam)
1799{
Ian Elliotte14e9f92015-04-16 15:23:05 -06001800 switch(uMsg)
1801 {
Ian Elliotte14e9f92015-04-16 15:23:05 -06001802 case WM_CLOSE:
1803 PostQuitMessage(0);
Tony Barbour5685ad72015-04-29 16:19:20 -06001804 break;
Ian Elliotte14e9f92015-04-16 15:23:05 -06001805 case WM_PAINT:
1806 demo_run(&demo);
1807 return 0;
1808 default:
1809 break;
1810 }
1811 return (DefWindowProc(hWnd, uMsg, wParam, lParam));
1812}
1813
1814static void demo_create_window(struct demo *demo)
1815{
1816 WNDCLASSEX win_class;
1817
1818 // Initialize the window class structure:
1819 win_class.cbSize = sizeof(WNDCLASSEX);
1820 win_class.style = CS_HREDRAW | CS_VREDRAW;
1821 win_class.lpfnWndProc = WndProc;
1822 win_class.cbClsExtra = 0;
1823 win_class.cbWndExtra = 0;
1824 win_class.hInstance = demo->connection; // hInstance
1825 win_class.hIcon = LoadIcon(NULL, IDI_APPLICATION);
1826 win_class.hCursor = LoadCursor(NULL, IDC_ARROW);
1827 win_class.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
1828 win_class.lpszMenuName = NULL;
1829 win_class.lpszClassName = demo->name;
1830 win_class.hIconSm = LoadIcon(NULL, IDI_WINLOGO);
1831 // Register window class:
1832 if (!RegisterClassEx(&win_class)) {
1833 // It didn't work, so try to give a useful error:
1834 printf("Unexpected error trying to start the application!\n");
1835 fflush(stdout);
1836 exit(1);
1837 }
1838 // Create window with the registered class:
Mike Stroyanf5856292015-06-15 14:20:13 -06001839 RECT wr = { 0, 0, demo->width, demo->height };
1840 AdjustWindowRect(&wr, WS_OVERLAPPEDWINDOW, FALSE);
Ian Elliotte14e9f92015-04-16 15:23:05 -06001841 demo->window = CreateWindowEx(0,
1842 demo->name, // class name
1843 demo->name, // app name
1844 WS_OVERLAPPEDWINDOW | // window style
1845 WS_VISIBLE |
1846 WS_SYSMENU,
1847 100,100, // x/y coords
Mike Stroyanf5856292015-06-15 14:20:13 -06001848 wr.right-wr.left, // width
1849 wr.bottom-wr.top, // height
Ian Elliotte14e9f92015-04-16 15:23:05 -06001850 NULL, // handle to parent
1851 NULL, // handle to menu
1852 demo->connection, // hInstance
1853 NULL); // no extra parameters
1854 if (!demo->window) {
1855 // It didn't work, so try to give a useful error:
1856 printf("Cannot create a window in which to draw!\n");
1857 fflush(stdout);
1858 exit(1);
1859 }
1860}
1861#else // _WIN32
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001862static void demo_handle_event(struct demo *demo,
1863 const xcb_generic_event_t *event)
1864{
Piers Daniell886be472015-02-23 16:23:13 -07001865 uint8_t event_code = event->response_type & 0x7f;
Courtney Goeltzenleuchterca21a212014-11-06 14:27:52 -07001866 switch (event_code) {
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001867 case XCB_EXPOSE:
Courtney Goeltzenleuchter54611482014-11-18 11:28:09 -07001868 // TODO: Resize window
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001869 break;
Courtney Goeltzenleuchterca21a212014-11-06 14:27:52 -07001870 case XCB_CLIENT_MESSAGE:
1871 if((*(xcb_client_message_event_t*)event).data.data32[0] ==
1872 (*demo->atom_wm_delete_window).atom) {
1873 demo->quit = true;
1874 }
1875 break;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001876 case XCB_KEY_RELEASE:
1877 {
1878 const xcb_key_release_event_t *key =
1879 (const xcb_key_release_event_t *) event;
1880
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -06001881 switch (key->detail) {
1882 case 0x9: // Escape
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001883 demo->quit = true;
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -06001884 break;
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001885 case 0x71: // left arrow key
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -06001886 demo->spin_angle += demo->spin_increment;
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001887 break;
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -06001888 case 0x72: // right arrow key
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -06001889 demo->spin_angle -= demo->spin_increment;
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -06001890 break;
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -06001891 case 0x41:
1892 demo->pause = !demo->pause;
Piers Daniell886be472015-02-23 16:23:13 -07001893 break;
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -06001894 }
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001895 }
1896 break;
1897 default:
1898 break;
1899 }
1900}
1901
1902static void demo_run(struct demo *demo)
1903{
1904 xcb_flush(demo->connection);
1905
1906 while (!demo->quit) {
1907 xcb_generic_event_t *event;
1908
Courtney Goeltzenleuchter54611482014-11-18 11:28:09 -07001909 if (demo->pause) {
1910 event = xcb_wait_for_event(demo->connection);
1911 } else {
1912 event = xcb_poll_for_event(demo->connection);
1913 }
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001914 if (event) {
1915 demo_handle_event(demo, event);
1916 free(event);
Courtney Goeltzenleuchterbb3e1312014-11-10 11:13:13 -07001917 }
Courtney Goeltzenleuchter54611482014-11-18 11:28:09 -07001918
1919 // Wait for work to finish before updating MVP.
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001920 vkDeviceWaitIdle(demo->device);
Courtney Goeltzenleuchter54611482014-11-18 11:28:09 -07001921 demo_update_data_buffer(demo);
1922
Courtney Goeltzenleuchterbb3e1312014-11-10 11:13:13 -07001923 demo_draw(demo);
Courtney Goeltzenleuchter21f89972014-11-18 11:28:09 -07001924
Courtney Goeltzenleuchterbb3e1312014-11-10 11:13:13 -07001925 // Wait for work to finish before updating MVP.
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001926 vkDeviceWaitIdle(demo->device);
David Pinedoeeca2a22015-06-18 17:03:14 -06001927 demo->curFrame++;
1928 if (demo->frameCount != INT_MAX && demo->curFrame == demo->frameCount)
1929 demo->quit = true;
1930
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001931 }
1932}
1933
1934static void demo_create_window(struct demo *demo)
1935{
1936 uint32_t value_mask, value_list[32];
1937
1938 demo->window = xcb_generate_id(demo->connection);
1939
1940 value_mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK;
1941 value_list[0] = demo->screen->black_pixel;
1942 value_list[1] = XCB_EVENT_MASK_KEY_RELEASE |
1943 XCB_EVENT_MASK_EXPOSURE;
1944
1945 xcb_create_window(demo->connection,
1946 XCB_COPY_FROM_PARENT,
1947 demo->window, demo->screen->root,
1948 0, 0, demo->width, demo->height, 0,
1949 XCB_WINDOW_CLASS_INPUT_OUTPUT,
1950 demo->screen->root_visual,
1951 value_mask, value_list);
1952
Courtney Goeltzenleuchterca21a212014-11-06 14:27:52 -07001953 /* Magic code that will send notification when window is destroyed */
1954 xcb_intern_atom_cookie_t cookie = xcb_intern_atom(demo->connection, 1, 12,
1955 "WM_PROTOCOLS");
1956 xcb_intern_atom_reply_t* reply = xcb_intern_atom_reply(demo->connection, cookie, 0);
1957
1958 xcb_intern_atom_cookie_t cookie2 = xcb_intern_atom(demo->connection, 0, 16, "WM_DELETE_WINDOW");
1959 demo->atom_wm_delete_window = xcb_intern_atom_reply(demo->connection, cookie2, 0);
1960
1961 xcb_change_property(demo->connection, XCB_PROP_MODE_REPLACE,
1962 demo->window, (*reply).atom, 4, 32, 1,
1963 &(*demo->atom_wm_delete_window).atom);
1964 free(reply);
1965
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001966 xcb_map_window(demo->connection, demo->window);
David Pinedoeeca2a22015-06-18 17:03:14 -06001967
1968 // Force the x/y coordinates to 100,100 results are identical in consecutive runs
1969 const uint32_t coords[] = {100, 100};
1970 xcb_configure_window(demo->connection, demo->window,
1971 XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y, coords);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001972}
Ian Elliotte14e9f92015-04-16 15:23:05 -06001973#endif // _WIN32
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001974
Courtney Goeltzenleuchter8c15fc52015-07-06 17:46:11 -06001975/*
1976 * Return 1 (true) if all layer names specified in check_names
1977 * can be found in given layer properties.
1978 */
Courtney Goeltzenleuchter1f41f542015-07-09 11:44:38 -06001979static VkBool32 demo_check_layers(uint32_t check_count, char **check_names,
Courtney Goeltzenleuchter8c15fc52015-07-06 17:46:11 -06001980 uint32_t layer_count, VkLayerProperties *layers)
1981{
1982 for (uint32_t i = 0; i < check_count; i++) {
Courtney Goeltzenleuchter1f41f542015-07-09 11:44:38 -06001983 VkBool32 found = 0;
Courtney Goeltzenleuchter8c15fc52015-07-06 17:46:11 -06001984 for (uint32_t j = 0; j < layer_count; j++) {
1985 if (!strcmp(check_names[i], layers[j].layerName)) {
1986 found = 1;
1987 }
1988 }
1989 if (!found) {
1990 fprintf(stderr, "Cannot find layer: %s\n", check_names[i]);
1991 return 0;
1992 }
1993 }
1994 return 1;
1995}
1996
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001997static void demo_init_vk(struct demo *demo)
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001998{
Tobin Ehlis3536b442015-04-16 18:04:57 -06001999 VkResult err;
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002000 char *extension_names[64];
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -06002001 VkExtensionProperties *instance_extensions;
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002002 VkLayerProperties *instance_layers;
2003 VkLayerProperties *device_layers;
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -06002004 uint32_t instance_extension_count = 0;
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002005 uint32_t instance_layer_count = 0;
2006 uint32_t enabled_extension_count = 0;
2007 uint32_t enabled_layer_count = 0;
2008
Courtney Goeltzenleuchter8c15fc52015-07-06 17:46:11 -06002009 char *instance_validation_layers[] = {
2010 "MemTracker",
2011 };
2012
2013 char *device_validation_layers[] = {
2014 "MemTracker",
2015 };
2016
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002017 /* Look for validation layers */
Courtney Goeltzenleuchter1f41f542015-07-09 11:44:38 -06002018 VkBool32 validation_found = 0;
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002019 err = vkGetGlobalLayerProperties(&instance_layer_count, NULL);
Tobin Ehlis3536b442015-04-16 18:04:57 -06002020 assert(!err);
2021
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002022 instance_layers = malloc(sizeof(VkLayerProperties) * instance_layer_count);
2023 err = vkGetGlobalLayerProperties(&instance_layer_count, instance_layers);
2024 assert(!err);
Courtney Goeltzenleuchter8c15fc52015-07-06 17:46:11 -06002025
2026 if (demo->validate) {
2027 validation_found = demo_check_layers(ARRAY_SIZE(instance_validation_layers), instance_validation_layers,
2028 instance_layer_count, instance_layers);
2029 if (!validation_found) {
2030 ERR_EXIT("vkGetGlobalLayerProperties failed to find"
2031 "required validation layer.\n\n"
2032 "Please look at the Getting Started guide for additional "
2033 "information.\n",
2034 "vkCreateInstance Failure");
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002035 }
Courtney Goeltzenleuchter8c15fc52015-07-06 17:46:11 -06002036 enabled_layer_count = ARRAY_SIZE(instance_validation_layers);
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002037 }
2038
2039 err = vkGetGlobalExtensionProperties(NULL, &instance_extension_count, NULL);
2040 assert(!err);
2041
Courtney Goeltzenleuchter1f41f542015-07-09 11:44:38 -06002042 VkBool32 WSIextFound = 0;
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002043 memset(extension_names, 0, sizeof(extension_names));
2044 instance_extensions = malloc(sizeof(VkExtensionProperties) * instance_extension_count);
2045 err = vkGetGlobalExtensionProperties(NULL, &instance_extension_count, instance_extensions);
2046 assert(!err);
2047 for (uint32_t i = 0; i < instance_extension_count; i++) {
2048 if (!strcmp(VK_WSI_LUNARG_EXTENSION_NAME, instance_extensions[i].extName)) {
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -06002049 WSIextFound = 1;
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002050 extension_names[enabled_extension_count++] = VK_WSI_LUNARG_EXTENSION_NAME;
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -06002051 }
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002052 if (!strcmp(DEBUG_REPORT_EXTENSION_NAME, instance_extensions[i].extName)) {
2053 if (demo->validate) {
2054 extension_names[enabled_extension_count++] = DEBUG_REPORT_EXTENSION_NAME;
2055 }
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -06002056 }
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002057 assert(enabled_extension_count < 64);
Tobin Ehlis3536b442015-04-16 18:04:57 -06002058 }
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -06002059 if (!WSIextFound) {
Tony Barbour426b9052015-06-24 16:06:58 -06002060 ERR_EXIT("vkGetGlobalExtensionProperties failed to find the "
Ian Elliott3b375cf2015-04-28 13:22:33 -06002061 "\"VK_WSI_LunarG\" extension.\n\nDo you have a compatible "
2062 "Vulkan installable client driver (ICD) installed?\nPlease "
2063 "look at the Getting Started guide for additional "
2064 "information.\n",
2065 "vkCreateInstance Failure");
2066 }
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06002067 const VkApplicationInfo app = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06002068 .sType = VK_STRUCTURE_TYPE_APPLICATION_INFO,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002069 .pNext = NULL,
Ian Elliott4e19ed02015-04-28 10:52:52 -06002070 .pAppName = APP_SHORT_NAME,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002071 .appVersion = 0,
Ian Elliott4e19ed02015-04-28 10:52:52 -06002072 .pEngineName = APP_SHORT_NAME,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002073 .engineVersion = 0,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06002074 .apiVersion = VK_API_VERSION,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002075 };
Tony Barbour5685ad72015-04-29 16:19:20 -06002076 VkInstanceCreateInfo inst_info = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06002077 .sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
Jon Ashburn29669a42015-04-04 14:52:07 -06002078 .pNext = NULL,
2079 .pAppInfo = &app,
2080 .pAllocCb = NULL,
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002081 .layerCount = enabled_layer_count,
Courtney Goeltzenleuchter8c15fc52015-07-06 17:46:11 -06002082 .ppEnabledLayerNames = (const char *const*) ((demo->validate) ? instance_validation_layers : NULL),
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002083 .extensionCount = enabled_extension_count,
2084 .ppEnabledExtensionNames = (const char *const*) extension_names,
Jon Ashburn29669a42015-04-04 14:52:07 -06002085 };
Courtney Goeltzenleuchterddcb6192015-04-14 18:48:46 -06002086 const VkDeviceQueueCreateInfo queue = {
Chris Forbesfa6d36e2015-07-11 19:11:39 +12002087 .queueFamilyIndex = 0,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002088 .queueCount = 1,
2089 };
Ian Elliottff6dab52015-04-28 11:35:02 -06002090
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06002091 uint32_t gpu_count;
2092 uint32_t i;
Courtney Goeltzenleuchterf3168062015-03-05 18:09:39 -07002093 uint32_t queue_count;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002094
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06002095 err = vkCreateInstance(&inst_info, &demo->inst);
Ian Elliottcaa9f272015-04-28 11:35:02 -06002096 if (err == VK_ERROR_INCOMPATIBLE_DRIVER) {
2097 ERR_EXIT("Cannot find a compatible Vulkan installable client driver "
Ian Elliott3b375cf2015-04-28 13:22:33 -06002098 "(ICD).\n\nPlease look at the Getting Started guide for "
Ian Elliottcaa9f272015-04-28 11:35:02 -06002099 "additional information.\n",
2100 "vkCreateInstance Failure");
Tony Barbour5685ad72015-04-29 16:19:20 -06002101 } else if (err == VK_ERROR_INVALID_EXTENSION) {
2102 ERR_EXIT("Cannot find a specified extension library"
2103 ".\nMake sure your layers path is set appropriately\n",
2104 "vkCreateInstance Failure");
Ian Elliottcaa9f272015-04-28 11:35:02 -06002105 } else if (err) {
Ian Elliott3b375cf2015-04-28 13:22:33 -06002106 ERR_EXIT("vkCreateInstance failed.\n\nDo you have a compatible Vulkan "
2107 "installable client driver (ICD) installed?\nPlease look at "
Ian Elliottcaa9f272015-04-28 11:35:02 -06002108 "the Getting Started guide for additional information.\n",
2109 "vkCreateInstance Failure");
Ian Elliottdfe55f72015-04-03 15:24:55 -06002110 }
Jon Ashburn29669a42015-04-04 14:52:07 -06002111
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002112 free(instance_layers);
2113 free(instance_extensions);
Tony Barbour5685ad72015-04-29 16:19:20 -06002114
Jon Ashburn07b309a2015-04-15 11:31:12 -06002115 gpu_count = 1;
2116 err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, &demo->gpu);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002117 assert(!err && gpu_count == 1);
2118
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002119 /* Look for validation layers */
2120 validation_found = 0;
2121 enabled_layer_count = 0;
2122 uint32_t device_layer_count = 0;
2123 err = vkGetPhysicalDeviceLayerProperties(demo->gpu, &device_layer_count, NULL);
2124 assert(!err);
2125
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002126 device_layers = malloc(sizeof(VkLayerProperties) * device_layer_count);
2127 err = vkGetPhysicalDeviceLayerProperties(demo->gpu, &device_layer_count, device_layers);
2128 assert(!err);
Courtney Goeltzenleuchter8c15fc52015-07-06 17:46:11 -06002129
2130 if (demo->validate) {
2131 validation_found = demo_check_layers(ARRAY_SIZE(device_validation_layers), device_validation_layers,
2132 device_layer_count, device_layers);
2133 if (!validation_found) {
2134 ERR_EXIT("vkGetPhysicalDeviceLayerProperties failed to find"
2135 "a required validation layer.\n\n"
2136 "Please look at the Getting Started guide for additional "
2137 "information.\n",
2138 "vkCreateDevice Failure");
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002139 }
Courtney Goeltzenleuchter8c15fc52015-07-06 17:46:11 -06002140 enabled_layer_count = ARRAY_SIZE(device_validation_layers);
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002141 }
2142
2143 uint32_t device_extension_count = 0;
2144 VkExtensionProperties *device_extensions = NULL;
2145 err = vkGetPhysicalDeviceExtensionProperties(
2146 demo->gpu, NULL, &device_extension_count, NULL);
2147 assert(!err);
2148
2149 WSIextFound = 0;
2150 enabled_extension_count = 0;
2151 memset(extension_names, 0, sizeof(extension_names));
2152 device_extensions = malloc(sizeof(VkExtensionProperties) * device_extension_count);
2153 err = vkGetPhysicalDeviceExtensionProperties(
2154 demo->gpu, NULL, &device_extension_count, device_extensions);
2155 assert(!err);
Courtney Goeltzenleuchter37d091e2015-07-05 22:08:51 -06002156#if 0
2157 /* Will need this check in future */
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002158 for (uint32_t i = 0; i < device_extension_count; i++) {
2159 if (!strcmp(VK_WSI_LUNARG_EXTENSION_NAME, device_extensions[i].extName)) {
2160 WSIextFound = 1;
2161 extension_names[enabled_extension_count++] = VK_WSI_LUNARG_EXTENSION_NAME;
2162 }
2163 assert(enabled_extension_count < 64);
2164 }
2165 if (!WSIextFound) {
2166 ERR_EXIT("vkGetGlobalExtensionProperties failed to find the "
2167 "\"VK_WSI_LunarG\" extension.\n\nDo you have a compatible "
2168 "Vulkan installable client driver (ICD) installed?\nPlease "
2169 "look at the Getting Started guide for additional "
2170 "information.\n",
2171 "vkCreateInstance Failure");
2172 }
Courtney Goeltzenleuchter37d091e2015-07-05 22:08:51 -06002173#endif
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002174
2175 VkDeviceCreateInfo device = {
2176 .sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
2177 .pNext = NULL,
2178 .queueRecordCount = 1,
2179 .pRequestedQueues = &queue,
2180 .layerCount = enabled_layer_count,
Courtney Goeltzenleuchter8c15fc52015-07-06 17:46:11 -06002181 .ppEnabledLayerNames = (const char *const*) ((demo->validate) ? device_validation_layers : NULL),
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002182 .extensionCount = enabled_extension_count,
2183 .ppEnabledExtensionNames = (const char *const*) extension_names,
2184 .flags = 0,
2185 };
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -06002186
Tony Barbour5685ad72015-04-29 16:19:20 -06002187 if (demo->validate) {
Courtney Goeltzenleuchtera4c8c712015-07-12 14:35:22 -06002188 demo->dbgCreateMsgCallback = (PFN_vkDbgCreateMsgCallback) vkGetInstanceProcAddr(demo->inst, "vkDbgCreateMsgCallback");
2189 demo->dbgDestroyMsgCallback = (PFN_vkDbgDestroyMsgCallback) vkGetInstanceProcAddr(demo->inst, "vkDbgDestroyMsgCallback");
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -06002190 if (!demo->dbgCreateMsgCallback) {
2191 ERR_EXIT("GetProcAddr: Unable to find vkDbgCreateMsgCallback\n",
2192 "vkGetProcAddr Failure");
2193 }
Tony Barboura65ecc22015-06-30 14:14:19 -06002194 if (!demo->dbgDestroyMsgCallback) {
2195 ERR_EXIT("GetProcAddr: Unable to find vkDbgDestroyMsgCallback\n",
2196 "vkGetProcAddr Failure");
2197 }
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -06002198 err = demo->dbgCreateMsgCallback(
2199 demo->inst,
2200 VK_DBG_REPORT_ERROR_BIT | VK_DBG_REPORT_WARN_BIT,
2201 dbgFunc, NULL,
2202 &demo->msg_callback);
2203 switch (err) {
2204 case VK_SUCCESS:
2205 break;
2206 case VK_ERROR_INVALID_POINTER:
2207 ERR_EXIT("dbgCreateMsgCallback: Invalid pointer\n",
2208 "dbgCreateMsgCallback Failure");
2209 break;
2210 case VK_ERROR_OUT_OF_HOST_MEMORY:
2211 ERR_EXIT("dbgCreateMsgCallback: out of host memory\n",
2212 "dbgCreateMsgCallback Failure");
2213 break;
2214 default:
2215 ERR_EXIT("dbgCreateMsgCallback: unknown failure\n",
2216 "dbgCreateMsgCallback Failure");
2217 break;
2218 }
Tony Barbour5685ad72015-04-29 16:19:20 -06002219 }
2220
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06002221 err = vkCreateDevice(demo->gpu, &device, &demo->device);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002222 assert(!err);
2223
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002224 free(device_layers);
2225
Ian Elliott1b6de092015-06-22 15:07:49 -06002226 GET_DEVICE_PROC_ADDR(demo->device, CreateSwapChainWSI);
2227 GET_DEVICE_PROC_ADDR(demo->device, CreateSwapChainWSI);
2228 GET_DEVICE_PROC_ADDR(demo->device, DestroySwapChainWSI);
2229 GET_DEVICE_PROC_ADDR(demo->device, GetSwapChainInfoWSI);
2230 GET_DEVICE_PROC_ADDR(demo->device, QueuePresentWSI);
Jon Ashburncedc15f2015-05-21 18:13:33 -06002231
Tony Barbour426b9052015-06-24 16:06:58 -06002232 err = vkGetPhysicalDeviceProperties(demo->gpu, &demo->gpu_props);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -06002233 assert(!err);
2234
Tony Barbour426b9052015-06-24 16:06:58 -06002235 err = vkGetPhysicalDeviceQueueCount(demo->gpu, &queue_count);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -06002236 assert(!err);
2237
Tony Barbour426b9052015-06-24 16:06:58 -06002238 demo->queue_props = (VkPhysicalDeviceQueueProperties *) malloc(queue_count * sizeof(VkPhysicalDeviceQueueProperties));
2239 err = vkGetPhysicalDeviceQueueProperties(demo->gpu, queue_count, demo->queue_props);
Courtney Goeltzenleuchterf3168062015-03-05 18:09:39 -07002240 assert(!err);
Courtney Goeltzenleuchterf3168062015-03-05 18:09:39 -07002241 assert(queue_count >= 1);
2242
Mark Lobodzinskicf26e072015-04-16 11:44:05 -05002243 // Graphics queue and MemMgr queue can be separate.
2244 // TODO: Add support for separate queues, including synchronization,
Mark Lobodzinskifb9f5642015-05-11 17:21:15 -05002245 // and appropriate tracking for QueueSubmit
Courtney Goeltzenleuchterf3168062015-03-05 18:09:39 -07002246 for (i = 0; i < queue_count; i++) {
Mark Lobodzinskifb9f5642015-05-11 17:21:15 -05002247 if (demo->queue_props[i].queueFlags & VK_QUEUE_GRAPHICS_BIT)
Courtney Goeltzenleuchterf3168062015-03-05 18:09:39 -07002248 break;
2249 }
2250 assert(i < queue_count);
2251 demo->graphics_queue_node_index = i;
2252
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06002253 err = vkGetDeviceQueue(demo->device, demo->graphics_queue_node_index,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002254 0, &demo->queue);
2255 assert(!err);
Tony Barbour5685ad72015-04-29 16:19:20 -06002256
Jon Ashburnba4a1952015-06-16 12:44:51 -06002257 // for now hardcode format till get WSI support
2258 demo->format = VK_FORMAT_B8G8R8A8_UNORM;
Ian Elliott32536f92015-04-21 16:41:02 -06002259
David Pinedoeeca2a22015-06-18 17:03:14 -06002260 demo->quit = false;
2261 demo->curFrame = 0;
Mark Lobodzinski72346292015-07-02 16:49:40 -06002262
2263 // Get Memory information and properties
2264 err = vkGetPhysicalDeviceMemoryProperties(demo->gpu, &demo->memory_properties);
2265 assert(!err);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002266}
2267
2268static void demo_init_connection(struct demo *demo)
2269{
Ian Elliotte14e9f92015-04-16 15:23:05 -06002270#ifndef _WIN32
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002271 const xcb_setup_t *setup;
2272 xcb_screen_iterator_t iter;
2273 int scr;
2274
2275 demo->connection = xcb_connect(NULL, &scr);
Ian Elliottdfe55f72015-04-03 15:24:55 -06002276 if (demo->connection == NULL) {
2277 printf("Cannot find a compatible Vulkan installable client driver "
2278 "(ICD).\nExiting ...\n");
2279 fflush(stdout);
2280 exit(1);
2281 }
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002282
2283 setup = xcb_get_setup(demo->connection);
2284 iter = xcb_setup_roots_iterator(setup);
2285 while (scr-- > 0)
2286 xcb_screen_next(&iter);
2287
2288 demo->screen = iter.data;
Ian Elliotte14e9f92015-04-16 15:23:05 -06002289#endif // _WIN32
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002290}
2291
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07002292static void demo_init(struct demo *demo, int argc, char **argv)
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002293{
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -06002294 vec3 eye = {0.0f, 3.0f, 5.0f};
2295 vec3 origin = {0, 0, 0};
Chia-I Wuc3487c22015-04-22 14:56:17 +08002296 vec3 up = {0.0f, 1.0f, 0.0};
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -06002297
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002298 memset(demo, 0, sizeof(*demo));
David Pinedoeeca2a22015-06-18 17:03:14 -06002299 demo->frameCount = INT_MAX;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002300
Piers Daniell886be472015-02-23 16:23:13 -07002301 for (int i = 1; i < argc; i++) {
Tony Barbour5685ad72015-04-29 16:19:20 -06002302 if (strcmp(argv[i], "--use_staging") == 0) {
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07002303 demo->use_staging_buffer = true;
Tony Barbour5685ad72015-04-29 16:19:20 -06002304 continue;
Ian Elliotte14e9f92015-04-16 15:23:05 -06002305 }
Cody Northrop75db0322015-05-28 11:27:16 -06002306 if (strcmp(argv[i], "--use_glsl") == 0) {
2307 demo->use_glsl = true;
2308 continue;
2309 }
Tony Barbour5685ad72015-04-29 16:19:20 -06002310 if (strcmp(argv[i], "--validate") == 0) {
2311 demo->validate = true;
2312 continue;
2313 }
David Pinedoeeca2a22015-06-18 17:03:14 -06002314 if (strcmp(argv[i], "--c") == 0 &&
2315 demo->frameCount == INT_MAX &&
2316 i < argc-1 &&
2317 sscanf(argv[i+1],"%d", &demo->frameCount) == 1 &&
2318 demo->frameCount >= 0)
2319 {
2320 i++;
2321 continue;
2322 }
Tony Barbour5685ad72015-04-29 16:19:20 -06002323
David Pinedoeeca2a22015-06-18 17:03:14 -06002324 fprintf(stderr, "Usage:\n %s [--use_staging] [--validate] [--c <framecount>]\n", APP_SHORT_NAME);
Ian Elliotte14e9f92015-04-16 15:23:05 -06002325 fflush(stderr);
2326 exit(1);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07002327 }
2328
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002329 demo_init_connection(demo);
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06002330 demo_init_vk(demo);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002331
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -06002332 demo->width = 500;
2333 demo->height = 500;
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -06002334
2335 demo->spin_angle = 0.01f;
2336 demo->spin_increment = 0.01f;
2337 demo->pause = false;
2338
Piers Daniell886be472015-02-23 16:23:13 -07002339 mat4x4_perspective(demo->projection_matrix, (float)degreesToRadians(45.0f), 1.0f, 0.1f, 100.0f);
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -06002340 mat4x4_look_at(demo->view_matrix, eye, origin, up);
2341 mat4x4_identity(demo->model_matrix);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002342}
2343
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002344
Ian Elliotte14e9f92015-04-16 15:23:05 -06002345#ifdef _WIN32
Tony Barbour5685ad72015-04-29 16:19:20 -06002346extern int __getmainargs(
2347 int * _Argc,
2348 char *** _Argv,
2349 char *** _Env,
2350 int _DoWildCard,
2351 int * new_mode);
Ian Elliott7595eee2015-04-28 10:33:11 -06002352
Ian Elliott421107f2015-04-28 15:50:36 -06002353int WINAPI WinMain(HINSTANCE hInstance,
2354 HINSTANCE hPrevInstance,
2355 LPSTR pCmdLine,
2356 int nCmdShow)
Ian Elliotte14e9f92015-04-16 15:23:05 -06002357{
2358 MSG msg; // message
2359 bool done; // flag saying when app is complete
Tony Barbour5685ad72015-04-29 16:19:20 -06002360 int argc;
2361 char** argv;
2362 char** env;
2363 int new_mode = 0;
Ian Elliotte14e9f92015-04-16 15:23:05 -06002364
Tony Barbour5685ad72015-04-29 16:19:20 -06002365 __getmainargs(&argc,&argv,&env,0,&new_mode);
2366
2367 demo_init(&demo, argc, argv);
2368 demo.connection = hInstance;
2369 strncpy(demo.name, "cube", APP_NAME_STR_LEN);
Ian Elliotte14e9f92015-04-16 15:23:05 -06002370 demo_create_window(&demo);
2371
2372 demo_prepare(&demo);
2373
2374 done = false; //initialize loop condition variable
2375 /* main message loop*/
2376 while(!done)
2377 {
Ian Elliott421107f2015-04-28 15:50:36 -06002378 PeekMessage(&msg, NULL, 0, 0, PM_REMOVE);
Ian Elliotte14e9f92015-04-16 15:23:05 -06002379 if (msg.message == WM_QUIT) //check for a quit message
2380 {
2381 done = true; //if found, quit app
2382 }
2383 else
2384 {
2385 /* Translate and dispatch to event queue*/
2386 TranslateMessage(&msg);
2387 DispatchMessage(&msg);
2388 }
2389 }
2390
2391 demo_cleanup(&demo);
2392
Tony Barboura938abb2015-04-22 11:36:22 -06002393 return (int) msg.wParam;
Ian Elliotte14e9f92015-04-16 15:23:05 -06002394}
2395#else // _WIN32
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07002396int main(int argc, char **argv)
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002397{
2398 struct demo demo;
2399
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07002400 demo_init(&demo, argc, argv);
Chia-I Wu5b66aa52015-04-16 22:02:10 +08002401 demo_create_window(&demo);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002402
2403 demo_prepare(&demo);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002404 demo_run(&demo);
2405
2406 demo_cleanup(&demo);
2407
2408 return 0;
2409}
Ian Elliotte14e9f92015-04-16 15:23:05 -06002410#endif // _WIN32