blob: 9ceb253f881470e76059140b5a9e547642d75c43 [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
Tony Barbour22a30862015-04-22 09:02:32 -060053#if defined(NDEBUG) && defined(__GNUC__)
54#define U_ASSERT_ONLY __attribute__((unused))
55#else
56#define U_ASSERT_ONLY
57#endif
58
Ian Elliottcaa9f272015-04-28 11:35:02 -060059#ifdef _WIN32
60#define ERR_EXIT(err_msg, err_class) \
61 do { \
62 MessageBox(NULL, err_msg, err_class, MB_OK); \
63 exit(1); \
64 } while (0)
65
66// NOTE: If the following values (copied from "loader_platform.h") change, they
67// need to change here as well:
68#define LAYER_NAMES_ENV "VK_LAYER_NAMES"
69#define LAYER_NAMES_REGISTRY_VALUE "VK_LAYER_NAMES"
70
71#else // _WIN32
72
73#define ERR_EXIT(err_msg, err_class) \
74 do { \
75 printf(err_msg); \
76 fflush(stdout); \
77 exit(1); \
78 } while (0)
79#endif // _WIN32
80
Ian Elliott1b6de092015-06-22 15:07:49 -060081#define GET_DEVICE_PROC_ADDR(dev, entrypoint) \
82{ \
83 demo->fp##entrypoint = vkGetDeviceProcAddr(dev, "vk"#entrypoint); \
84 if (demo->fp##entrypoint == NULL) { \
85 ERR_EXIT("vkGetDeviceProcAddr failed to find vk"#entrypoint, \
86 "vkGetDeviceProcAddr Failure"); \
87 } \
88}
89
Courtney Goeltzenleuchter7e334982014-10-30 15:14:16 -060090/*
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -070091 * structure to track all objects related to a texture.
92 */
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -060093struct texture_object {
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -060094 VkSampler sampler;
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -070095
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -060096 VkImage image;
97 VkImageLayout imageLayout;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -060098
Mark Lobodzinski23182612015-05-29 09:32:35 -050099 VkDeviceMemory mem;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600100 VkImageView view;
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700101 int32_t tex_width, tex_height;
102};
103
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600104static char *tex_files[] = {
Courtney Goeltzenleuchter7e334982014-10-30 15:14:16 -0600105 "lunarg-logo-256x256-solid.png"
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600106};
107
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600108struct vkcube_vs_uniform {
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -0600109 // Must start with MVP
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600110 float mvp[4][4];
111 float position[12*3][4];
112 float color[12*3][4];
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -0600113};
114
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600115struct vktexcube_vs_uniform {
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -0600116 // Must start with MVP
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600117 float mvp[4][4];
118 float position[12*3][4];
119 float attr[12*3][4];
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -0600120};
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -0600121
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600122//--------------------------------------------------------------------------------------
123// Mesh and VertexFormat Data
124//--------------------------------------------------------------------------------------
125struct Vertex
126{
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600127 float posX, posY, posZ, posW; // Position data
128 float r, g, b, a; // Color
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600129};
130
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -0600131struct VertexPosTex
132{
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600133 float posX, posY, posZ, posW; // Position data
134 float u, v, s, t; // Texcoord
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -0600135};
136
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600137#define XYZ1(_x_, _y_, _z_) (_x_), (_y_), (_z_), 1.f
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -0600138#define UV(_u_, _v_) (_u_), (_v_), 0.f, 1.f
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600139
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600140static const float g_vertex_buffer_data[] = {
Chia-I Wuc3487c22015-04-22 14:56:17 +0800141 -1.0f,-1.0f,-1.0f, // -X side
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600142 -1.0f,-1.0f, 1.0f,
143 -1.0f, 1.0f, 1.0f,
Chia-I Wuc3487c22015-04-22 14:56:17 +0800144 -1.0f, 1.0f, 1.0f,
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600145 -1.0f, 1.0f,-1.0f,
146 -1.0f,-1.0f,-1.0f,
147
Chia-I Wuc3487c22015-04-22 14:56:17 +0800148 -1.0f,-1.0f,-1.0f, // -Z side
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600149 1.0f, 1.0f,-1.0f,
150 1.0f,-1.0f,-1.0f,
Chia-I Wuc3487c22015-04-22 14:56:17 +0800151 -1.0f,-1.0f,-1.0f,
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600152 -1.0f, 1.0f,-1.0f,
Mike Stroyanea3945c2015-03-19 14:29:04 -0600153 1.0f, 1.0f,-1.0f,
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600154
Chia-I Wuc3487c22015-04-22 14:56:17 +0800155 -1.0f,-1.0f,-1.0f, // -Y side
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600156 1.0f,-1.0f,-1.0f,
Mike Stroyanea3945c2015-03-19 14:29:04 -0600157 1.0f,-1.0f, 1.0f,
Chia-I Wuc3487c22015-04-22 14:56:17 +0800158 -1.0f,-1.0f,-1.0f,
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600159 1.0f,-1.0f, 1.0f,
Mike Stroyanea3945c2015-03-19 14:29:04 -0600160 -1.0f,-1.0f, 1.0f,
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600161
Chia-I Wuc3487c22015-04-22 14:56:17 +0800162 -1.0f, 1.0f,-1.0f, // +Y side
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600163 -1.0f, 1.0f, 1.0f,
164 1.0f, 1.0f, 1.0f,
Chia-I Wuc3487c22015-04-22 14:56:17 +0800165 -1.0f, 1.0f,-1.0f,
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600166 1.0f, 1.0f, 1.0f,
Mike Stroyanea3945c2015-03-19 14:29:04 -0600167 1.0f, 1.0f,-1.0f,
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600168
Chia-I Wuc3487c22015-04-22 14:56:17 +0800169 1.0f, 1.0f,-1.0f, // +X side
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600170 1.0f, 1.0f, 1.0f,
171 1.0f,-1.0f, 1.0f,
Chia-I Wuc3487c22015-04-22 14:56:17 +0800172 1.0f,-1.0f, 1.0f,
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600173 1.0f,-1.0f,-1.0f,
174 1.0f, 1.0f,-1.0f,
175
Chia-I Wuc3487c22015-04-22 14:56:17 +0800176 -1.0f, 1.0f, 1.0f, // +Z side
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600177 -1.0f,-1.0f, 1.0f,
Mike Stroyanea3945c2015-03-19 14:29:04 -0600178 1.0f, 1.0f, 1.0f,
Chia-I Wuc3487c22015-04-22 14:56:17 +0800179 -1.0f,-1.0f, 1.0f,
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600180 1.0f,-1.0f, 1.0f,
181 1.0f, 1.0f, 1.0f,
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -0600182};
183
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600184static const float g_uv_buffer_data[] = {
Chia-I Wuc3487c22015-04-22 14:56:17 +0800185 0.0f, 0.0f, // -X side
186 1.0f, 0.0f,
187 1.0f, 1.0f,
188 1.0f, 1.0f,
189 0.0f, 1.0f,
190 0.0f, 0.0f,
191
192 1.0f, 0.0f, // -Z side
193 0.0f, 1.0f,
194 0.0f, 0.0f,
195 1.0f, 0.0f,
196 1.0f, 1.0f,
197 0.0f, 1.0f,
198
199 1.0f, 1.0f, // -Y side
200 1.0f, 0.0f,
201 0.0f, 0.0f,
202 1.0f, 1.0f,
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600203 0.0f, 0.0f,
204 0.0f, 1.0f,
205
Chia-I Wuc3487c22015-04-22 14:56:17 +0800206 1.0f, 1.0f, // +Y side
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -0600207 0.0f, 1.0f,
Chia-I Wuc3487c22015-04-22 14:56:17 +0800208 0.0f, 0.0f,
Mike Stroyanea3945c2015-03-19 14:29:04 -0600209 1.0f, 1.0f,
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600210 0.0f, 0.0f,
Mike Stroyanea3945c2015-03-19 14:29:04 -0600211 1.0f, 0.0f,
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600212
Chia-I Wuc3487c22015-04-22 14:56:17 +0800213 1.0f, 1.0f, // +X side
214 0.0f, 1.0f,
215 0.0f, 0.0f,
216 0.0f, 0.0f,
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600217 1.0f, 0.0f,
Mike Stroyanea3945c2015-03-19 14:29:04 -0600218 1.0f, 1.0f,
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600219
Chia-I Wuc3487c22015-04-22 14:56:17 +0800220 0.0f, 1.0f, // +Z side
221 0.0f, 0.0f,
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600222 1.0f, 1.0f,
Mike Stroyanea3945c2015-03-19 14:29:04 -0600223 0.0f, 0.0f,
Chia-I Wuc3487c22015-04-22 14:56:17 +0800224 1.0f, 0.0f,
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600225 1.0f, 1.0f,
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -0600226};
227
228void dumpMatrix(const char *note, mat4x4 MVP)
229{
230 int i;
231
232 printf("%s: \n", note);
233 for (i=0; i<4; i++) {
234 printf("%f, %f, %f, %f\n", MVP[i][0], MVP[i][1], MVP[i][2], MVP[i][3]);
235 }
236 printf("\n");
237 fflush(stdout);
238}
239
240void dumpVec4(const char *note, vec4 vector)
241{
242 printf("%s: \n", note);
243 printf("%f, %f, %f, %f\n", vector[0], vector[1], vector[2], vector[3]);
244 printf("\n");
245 fflush(stdout);
246}
247
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -0600248void dbgFunc(
249 VkFlags msgFlags,
250 VkObjectType objType,
251 VkObject srcObject,
252 size_t location,
253 int32_t msgCode,
254 const char* pLayerPrefix,
255 const char* pMsg,
256 void* pUserData)
Tony Barbour5685ad72015-04-29 16:19:20 -0600257{
258 char *message = (char *) malloc(strlen(pMsg)+100);
259
260 assert (message);
261
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -0600262 if (msgFlags & VK_DBG_REPORT_ERROR_BIT) {
263 sprintf(message,"ERROR: [%s] Code %d : %s", pLayerPrefix, msgCode, pMsg);
264 } else if (msgFlags & VK_DBG_REPORT_WARN_BIT) {
Tony Barboura65ecc22015-06-30 14:14:19 -0600265 // We know that we're submitting queues without fences, ignore this warning
266 if (strstr(pMsg, "vkQueueSubmit parameter, VkFence fence, is null pointer")){
267 return;
268 }
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -0600269 sprintf(message,"WARNING: [%s] Code %d : %s", pLayerPrefix, msgCode, pMsg);
Tony Barbour5685ad72015-04-29 16:19:20 -0600270 } else {
271 return;
272 }
273
274#ifdef _WIN32
275 MessageBox(NULL, message, "Alert", MB_OK);
276#else
277 printf("%s\n",message);
278 fflush(stdout);
279#endif
280 free(message);
281}
282
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600283struct demo {
Ian Elliotte14e9f92015-04-16 15:23:05 -0600284#ifdef _WIN32
285#define APP_NAME_STR_LEN 80
286 HINSTANCE connection; // hInstance - Windows Instance
287 char name[APP_NAME_STR_LEN]; // Name to put on the window/icon
288 HWND window; // hWnd - window handle
289#else // _WIN32
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600290 xcb_connection_t *connection;
291 xcb_screen_t *screen;
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800292 xcb_window_t window;
293 xcb_intern_atom_reply_t *atom_wm_delete_window;
Ian Elliotte14e9f92015-04-16 15:23:05 -0600294#endif // _WIN32
Cody Northrop75db0322015-05-28 11:27:16 -0600295 bool prepared;
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700296 bool use_staging_buffer;
Cody Northrop75db0322015-05-28 11:27:16 -0600297 bool use_glsl;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600298
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600299 VkInstance inst;
Tony Barbour8205d902015-04-16 15:59:00 -0600300 VkPhysicalDevice gpu;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600301 VkDevice device;
302 VkQueue queue;
Courtney Goeltzenleuchterf3168062015-03-05 18:09:39 -0700303 uint32_t graphics_queue_node_index;
Tony Barbour426b9052015-06-24 16:06:58 -0600304 VkPhysicalDeviceProperties gpu_props;
Tony Barbour8205d902015-04-16 15:59:00 -0600305 VkPhysicalDeviceQueueProperties *queue_props;
Mark Lobodzinski72346292015-07-02 16:49:40 -0600306 VkPhysicalDeviceMemoryProperties memory_properties;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600307
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600308 VkFramebuffer framebuffer;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600309 int width, height;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600310 VkFormat format;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600311
Jon Ashburncedc15f2015-05-21 18:13:33 -0600312 PFN_vkCreateSwapChainWSI fpCreateSwapChainWSI;
313 PFN_vkDestroySwapChainWSI fpDestroySwapChainWSI;
314 PFN_vkGetSwapChainInfoWSI fpGetSwapChainInfoWSI;
315 PFN_vkQueuePresentWSI fpQueuePresentWSI;
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800316 VkSwapChainWSI swap_chain;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600317 struct {
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600318 VkImage image;
Tony Barbour8205d902015-04-16 15:59:00 -0600319 VkDeviceMemory mem;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600320 VkCmdBuffer cmd;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600321
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600322 VkColorAttachmentView view;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600323 } buffers[DEMO_BUFFER_COUNT];
324
325 struct {
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600326 VkFormat format;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600327
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600328 VkImage image;
Mark Lobodzinski23182612015-05-29 09:32:35 -0500329 VkDeviceMemory mem;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600330 VkDepthStencilView view;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600331 } depth;
332
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600333 struct texture_object textures[DEMO_TEXTURE_COUNT];
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600334
335 struct {
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600336 VkBuffer buf;
Mark Lobodzinski23182612015-05-29 09:32:35 -0500337 VkDeviceMemory mem;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600338 VkBufferView view;
Chia-I Wu8cd8ecd2015-05-25 16:27:55 +0800339 VkDescriptorInfo desc;
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -0600340 } uniform_data;
341
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600342 VkCmdBuffer cmd; // Buffer for initialization commands
Mark Lobodzinski556f7212015-04-17 14:11:39 -0500343 VkPipelineLayout pipeline_layout;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600344 VkDescriptorSetLayout desc_layout;
345 VkPipeline pipeline;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600346
Courtney Goeltzenleuchterfcf855f2015-04-10 16:24:50 -0600347 VkDynamicVpState viewport;
348 VkDynamicRsState raster;
349 VkDynamicCbState color_blend;
350 VkDynamicDsState depth_stencil;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600351
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -0600352 mat4x4 projection_matrix;
353 mat4x4 view_matrix;
354 mat4x4 model_matrix;
355
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600356 float spin_angle;
357 float spin_increment;
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -0600358 bool pause;
359
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600360 VkDescriptorPool desc_pool;
361 VkDescriptorSet desc_set;
Chia-I Wuf8385062015-01-04 16:27:24 +0800362
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600363 bool quit;
David Pinedoeeca2a22015-06-18 17:03:14 -0600364 int32_t curFrame;
365 int32_t frameCount;
Tony Barbour5685ad72015-04-29 16:19:20 -0600366 bool validate;
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -0600367 PFN_vkDbgCreateMsgCallback dbgCreateMsgCallback;
Tony Barboura65ecc22015-06-30 14:14:19 -0600368 PFN_vkDbgDestroyMsgCallback dbgDestroyMsgCallback;
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -0600369 VkDbgMsgCallback msg_callback;
370
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600371 uint32_t current_buffer;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600372};
373
Mark Lobodzinski72346292015-07-02 16:49:40 -0600374static VkResult memory_type_from_properties(struct demo *demo, uint32_t typeBits, VkFlags properties, uint32_t *typeIndex)
375{
376 // Search memtypes to find first index with those properties
377 for (uint32_t i = 0; i < 32; i++) {
378 if ((typeBits & 1) == 1) {
379 // Type is available, does it match user properties?
380 if ((demo->memory_properties.memoryTypes[i].propertyFlags & properties) == properties) {
381 *typeIndex = i;
382 return VK_SUCCESS;
383 }
384 }
385 typeBits >>= 1;
386 }
387 // No memory types matched, return failure
388 return VK_UNSUPPORTED;
389}
390
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600391static void demo_flush_init_cmd(struct demo *demo)
392{
Tony Barbour22a30862015-04-22 09:02:32 -0600393 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600394
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600395 if (demo->cmd == VK_NULL_HANDLE)
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600396 return;
397
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600398 err = vkEndCommandBuffer(demo->cmd);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600399 assert(!err);
400
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600401 const VkCmdBuffer cmd_bufs[] = { demo->cmd };
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600402
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600403 err = vkQueueSubmit(demo->queue, 1, cmd_bufs, VK_NULL_HANDLE);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600404 assert(!err);
405
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600406 err = vkQueueWaitIdle(demo->queue);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600407 assert(!err);
408
Mike Stroyan230e6252015-04-17 12:36:38 -0600409 vkDestroyObject(demo->device, VK_OBJECT_TYPE_COMMAND_BUFFER, demo->cmd);
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600410 demo->cmd = VK_NULL_HANDLE;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600411}
412
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600413static void demo_set_image_layout(
414 struct demo *demo,
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600415 VkImage image,
malnasse4b8ba4d2015-06-03 17:28:38 -0400416 VkImageAspect aspect,
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600417 VkImageLayout old_image_layout,
418 VkImageLayout new_image_layout)
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600419{
Tony Barbour22a30862015-04-22 09:02:32 -0600420 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600421
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600422 if (demo->cmd == VK_NULL_HANDLE) {
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600423 const VkCmdBufferCreateInfo cmd = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600424 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600425 .pNext = NULL,
426 .queueNodeIndex = demo->graphics_queue_node_index,
Chia-I Wu88eaa3b2015-06-26 15:34:39 +0800427 .level = VK_CMD_BUFFER_LEVEL_PRIMARY,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600428 .flags = 0,
429 };
430
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600431 err = vkCreateCommandBuffer(demo->device, &cmd, &demo->cmd);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600432 assert(!err);
433
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600434 VkCmdBufferBeginInfo cmd_buf_info = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600435 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600436 .pNext = NULL,
Tony Barbour8205d902015-04-16 15:59:00 -0600437 .flags = VK_CMD_BUFFER_OPTIMIZE_SMALL_BATCH_BIT |
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600438 VK_CMD_BUFFER_OPTIMIZE_ONE_TIME_SUBMIT_BIT,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600439 };
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600440 err = vkBeginCommandBuffer(demo->cmd, &cmd_buf_info);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600441 }
442
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600443 VkImageMemoryBarrier image_memory_barrier = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600444 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600445 .pNext = NULL,
446 .outputMask = 0,
447 .inputMask = 0,
448 .oldLayout = old_image_layout,
449 .newLayout = new_image_layout,
450 .image = image,
malnasse4b8ba4d2015-06-03 17:28:38 -0400451 .subresourceRange = { aspect, 0, 1, 0, 0 }
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600452 };
453
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600454 if (new_image_layout == VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL) {
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600455 /* Make sure anything that was copying from this image has completed */
Courtney Goeltzenleuchterad870812015-04-15 15:29:59 -0600456 image_memory_barrier.inputMask = VK_MEMORY_INPUT_TRANSFER_BIT;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600457 }
458
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600459 if (new_image_layout == VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL) {
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600460 /* Make sure any Copy or CPU writes to image are flushed */
Courtney Goeltzenleuchtera569a502015-04-29 17:16:21 -0600461 image_memory_barrier.outputMask = VK_MEMORY_OUTPUT_HOST_WRITE_BIT | VK_MEMORY_OUTPUT_TRANSFER_BIT;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600462 }
463
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600464 VkImageMemoryBarrier *pmemory_barrier = &image_memory_barrier;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600465
Tony Barbourc2e987e2015-06-29 16:20:35 -0600466 VkPipelineStageFlags src_stages = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
467 VkPipelineStageFlags dest_stages = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600468
Tony Barbourc2e987e2015-06-29 16:20:35 -0600469 vkCmdPipelineBarrier(demo->cmd, src_stages, dest_stages, false, 1, (const void **)&pmemory_barrier);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600470}
471
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600472static void demo_draw_build_cmd(struct demo *demo, VkCmdBuffer cmd_buf)
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600473{
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600474 const VkColorAttachmentBindInfo color_attachment = {
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600475 .view = demo->buffers[demo->current_buffer].view,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600476 .layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600477 };
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600478 const VkDepthStencilBindInfo depth_stencil = {
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600479 .view = demo->depth.view,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600480 .layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600481 };
Chris Forbese3105972015-06-24 14:34:53 +1200482 const VkClearColorValue clear_color = {
483 .f32 = { 0.2f, 0.2f, 0.2f, 0.2f },
Courtney Goeltzenleuchter9a1ded82015-04-03 16:35:32 -0600484 };
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600485 const float clear_depth = 1.0f;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600486 VkCmdBufferBeginInfo cmd_buf_info = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600487 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO,
Courtney Goeltzenleuchtere3b0f3a2015-04-03 15:25:24 -0600488 .pNext = NULL,
Tony Barbour8205d902015-04-16 15:59:00 -0600489 .flags = VK_CMD_BUFFER_OPTIMIZE_SMALL_BATCH_BIT |
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600490 VK_CMD_BUFFER_OPTIMIZE_ONE_TIME_SUBMIT_BIT,
Jon Ashburn53d27af2014-12-31 17:08:35 -0700491 };
Tony Barbour22a30862015-04-22 09:02:32 -0600492 VkResult U_ASSERT_ONLY err;
Chris Forbesfd8456c2015-06-17 11:36:12 +1200493 VkAttachmentLoadOp load_op = VK_ATTACHMENT_LOAD_OP_CLEAR;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600494 VkAttachmentStoreOp store_op = VK_ATTACHMENT_STORE_OP_DONT_CARE;
495 const VkFramebufferCreateInfo fb_info = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600496 .sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO,
Jon Ashburn3325d6b2015-01-02 18:24:05 -0700497 .pNext = NULL,
498 .colorAttachmentCount = 1,
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600499 .pColorAttachments = (VkColorAttachmentBindInfo*) &color_attachment,
500 .pDepthStencilAttachment = (VkDepthStencilBindInfo*) &depth_stencil,
Jon Ashburn3325d6b2015-01-02 18:24:05 -0700501 .sampleCount = 1,
Mark Lobodzinski71fcc2d2015-01-27 13:24:03 -0600502 .width = demo->width,
503 .height = demo->height,
504 .layers = 1,
Jon Ashburn3325d6b2015-01-02 18:24:05 -0700505 };
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600506 VkRenderPassCreateInfo rp_info;
507 VkRenderPassBegin rp_begin;
Jon Ashburn3325d6b2015-01-02 18:24:05 -0700508
Chia-I Wu88eaa3b2015-06-26 15:34:39 +0800509 rp_begin.contents = VK_RENDER_PASS_CONTENTS_INLINE;
510
Jon Ashburn3325d6b2015-01-02 18:24:05 -0700511 memset(&rp_info, 0 , sizeof(rp_info));
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600512 err = vkCreateFramebuffer(demo->device, &fb_info, &rp_begin.framebuffer);
Jon Ashburn3325d6b2015-01-02 18:24:05 -0700513 assert(!err);
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600514 rp_info.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO;
Jon Ashburn3325d6b2015-01-02 18:24:05 -0700515 rp_info.renderArea.extent.width = demo->width;
516 rp_info.renderArea.extent.height = demo->height;
Courtney Goeltzenleuchtere3b0f3a2015-04-03 15:25:24 -0600517 rp_info.colorAttachmentCount = fb_info.colorAttachmentCount;
518 rp_info.pColorFormats = &demo->format;
519 rp_info.pColorLayouts = &color_attachment.layout;
Jon Ashburn3325d6b2015-01-02 18:24:05 -0700520 rp_info.pColorLoadOps = &load_op;
521 rp_info.pColorStoreOps = &store_op;
Courtney Goeltzenleuchtere3b0f3a2015-04-03 15:25:24 -0600522 rp_info.pColorLoadClearValues = &clear_color;
Tony Barbour8205d902015-04-16 15:59:00 -0600523 rp_info.depthStencilFormat = VK_FORMAT_D16_UNORM;
Courtney Goeltzenleuchtere3b0f3a2015-04-03 15:25:24 -0600524 rp_info.depthStencilLayout = depth_stencil.layout;
Chris Forbesefc7a6f2015-06-22 18:47:28 +1200525 rp_info.depthLoadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
Courtney Goeltzenleuchtere3b0f3a2015-04-03 15:25:24 -0600526 rp_info.depthLoadClearValue = clear_depth;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600527 rp_info.depthStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
528 rp_info.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
Courtney Goeltzenleuchtere3b0f3a2015-04-03 15:25:24 -0600529 rp_info.stencilLoadClearValue = 0;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600530 rp_info.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
Tony Barboura65ecc22015-06-30 14:14:19 -0600531 rp_info.sampleCount = 1;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600532 err = vkCreateRenderPass(demo->device, &rp_info, &rp_begin.renderPass);
Jon Ashburn3325d6b2015-01-02 18:24:05 -0700533 assert(!err);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600534
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600535 err = vkBeginCommandBuffer(cmd_buf, &cmd_buf_info);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600536 assert(!err);
537
Tobin Ehlise4076782015-06-24 15:53:07 -0600538 vkCmdBeginRenderPass(cmd_buf, &rp_begin);
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600539 vkCmdBindPipeline(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600540 demo->pipeline);
Mark Lobodzinskia65c4632015-06-15 13:21:21 -0600541 vkCmdBindDescriptorSets(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS, demo->pipeline_layout,
Cody Northrop1a01b1d2015-04-16 13:41:56 -0600542 0, 1, &demo->desc_set, 0, NULL);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600543
Tony Barbour8205d902015-04-16 15:59:00 -0600544 vkCmdBindDynamicStateObject(cmd_buf, VK_STATE_BIND_POINT_VIEWPORT, demo->viewport);
545 vkCmdBindDynamicStateObject(cmd_buf, VK_STATE_BIND_POINT_RASTER, demo->raster);
546 vkCmdBindDynamicStateObject(cmd_buf, VK_STATE_BIND_POINT_COLOR_BLEND,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600547 demo->color_blend);
Tony Barbour8205d902015-04-16 15:59:00 -0600548 vkCmdBindDynamicStateObject(cmd_buf, VK_STATE_BIND_POINT_DEPTH_STENCIL,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600549 demo->depth_stencil);
550
Chris Forbesefc7a6f2015-06-22 18:47:28 +1200551 vkCmdBeginRenderPass(cmd_buf, &rp_begin);
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -0600552
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600553 vkCmdDraw(cmd_buf, 0, 12 * 3, 0, 1);
Chia-I Wu88eaa3b2015-06-26 15:34:39 +0800554 vkCmdEndRenderPass(cmd_buf);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600555
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600556 err = vkEndCommandBuffer(cmd_buf);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600557 assert(!err);
Courtney Goeltzenleuchter04fc2fb2015-02-25 17:53:18 -0700558
Mike Stroyan230e6252015-04-17 12:36:38 -0600559 vkDestroyObject(demo->device, VK_OBJECT_TYPE_RENDER_PASS, rp_begin.renderPass);
560 vkDestroyObject(demo->device, VK_OBJECT_TYPE_FRAMEBUFFER, rp_begin.framebuffer);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600561}
562
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -0600563
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -0600564void demo_update_data_buffer(struct demo *demo)
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600565{
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -0600566 mat4x4 MVP, Model, VP;
567 int matrixSize = sizeof(MVP);
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600568 uint8_t *pData;
Tony Barbour22a30862015-04-22 09:02:32 -0600569 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600570
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -0600571 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600572
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -0600573 // Rotate 22.5 degrees around the Y axis
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -0600574 mat4x4_dup(Model, demo->model_matrix);
Piers Daniell886be472015-02-23 16:23:13 -0700575 mat4x4_rotate(demo->model_matrix, Model, 0.0f, 1.0f, 0.0f, (float)degreesToRadians(demo->spin_angle));
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -0600576 mat4x4_mul(MVP, VP, demo->model_matrix);
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -0600577
Mark Lobodzinski23182612015-05-29 09:32:35 -0500578 err = vkMapMemory(demo->device, demo->uniform_data.mem, 0, 0, 0, (void **) &pData);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600579 assert(!err);
580
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -0600581 memcpy(pData, (const void*) &MVP[0][0], matrixSize);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600582
Mark Lobodzinski23182612015-05-29 09:32:35 -0500583 err = vkUnmapMemory(demo->device, demo->uniform_data.mem);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600584 assert(!err);
585}
586
587static void demo_draw(struct demo *demo)
588{
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800589 const VkPresentInfoWSI present = {
590 .sType = VK_STRUCTURE_TYPE_PRESENT_INFO_WSI,
591 .pNext = NULL,
592 .image = demo->buffers[demo->current_buffer].image,
593 .flipInterval = 0,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600594 };
Tony Barbour22a30862015-04-22 09:02:32 -0600595 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600596
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600597 err = vkQueueSubmit(demo->queue, 1, &demo->buffers[demo->current_buffer].cmd,
598 VK_NULL_HANDLE);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600599 assert(!err);
600
Jon Ashburncedc15f2015-05-21 18:13:33 -0600601 err = demo->fpQueuePresentWSI(demo->queue, &present);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600602 assert(!err);
603
604 demo->current_buffer = (demo->current_buffer + 1) % DEMO_BUFFER_COUNT;
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800605
606 err = vkQueueWaitIdle(demo->queue);
607 assert(err == VK_SUCCESS);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600608}
609
610static void demo_prepare_buffers(struct demo *demo)
611{
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800612 const VkSwapChainCreateInfoWSI swap_chain = {
613 .sType = VK_STRUCTURE_TYPE_SWAP_CHAIN_CREATE_INFO_WSI,
614 .pNext = NULL,
615 .pNativeWindowSystemHandle = demo->connection,
616 .pNativeWindowHandle = (void *) (intptr_t) demo->window,
Ian Elliott32536f92015-04-21 16:41:02 -0600617 .displayCount = 1,
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800618 .imageCount = DEMO_BUFFER_COUNT,
619 .imageFormat = demo->format,
620 .imageExtent = {
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600621 .width = demo->width,
622 .height = demo->height,
623 },
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800624 .imageArraySize = 1,
625 .imageUsageFlags = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600626 };
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800627 VkSwapChainImageInfoWSI images[DEMO_BUFFER_COUNT];
628 size_t images_size = sizeof(images);
Tony Barbour22a30862015-04-22 09:02:32 -0600629 VkResult U_ASSERT_ONLY err;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600630 uint32_t i;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600631
Jon Ashburncedc15f2015-05-21 18:13:33 -0600632 err = demo->fpCreateSwapChainWSI(demo->device, &swap_chain, &demo->swap_chain);
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800633 assert(!err);
634
Jon Ashburncedc15f2015-05-21 18:13:33 -0600635 err = demo->fpGetSwapChainInfoWSI(demo->swap_chain,
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800636 VK_SWAP_CHAIN_INFO_TYPE_PERSISTENT_IMAGES_WSI,
637 &images_size, images);
638 assert(!err && images_size == sizeof(images));
639
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600640 for (i = 0; i < DEMO_BUFFER_COUNT; i++) {
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600641 VkColorAttachmentViewCreateInfo color_attachment_view = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600642 .sType = VK_STRUCTURE_TYPE_COLOR_ATTACHMENT_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600643 .pNext = NULL,
644 .format = demo->format,
645 .mipLevel = 0,
646 .baseArraySlice = 0,
647 .arraySize = 1,
648 };
649
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800650 demo->buffers[i].image = images[i].image;
651 demo->buffers[i].mem = images[i].memory;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600652
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600653 demo_set_image_layout(demo, demo->buffers[i].image,
malnasse4b8ba4d2015-06-03 17:28:38 -0400654 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600655 VK_IMAGE_LAYOUT_UNDEFINED,
656 VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600657
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600658 color_attachment_view.image = demo->buffers[i].image;
659
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600660 err = vkCreateColorAttachmentView(demo->device,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600661 &color_attachment_view, &demo->buffers[i].view);
662 assert(!err);
663 }
664}
665
666static void demo_prepare_depth(struct demo *demo)
667{
Tony Barbour8205d902015-04-16 15:59:00 -0600668 const VkFormat depth_format = VK_FORMAT_D16_UNORM;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600669 const VkImageCreateInfo image = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600670 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600671 .pNext = NULL,
Tony Barbour8205d902015-04-16 15:59:00 -0600672 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600673 .format = depth_format,
674 .extent = { demo->width, demo->height, 1 },
675 .mipLevels = 1,
676 .arraySize = 1,
677 .samples = 1,
Tony Barbour8205d902015-04-16 15:59:00 -0600678 .tiling = VK_IMAGE_TILING_OPTIMAL,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600679 .usage = VK_IMAGE_USAGE_DEPTH_STENCIL_BIT,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600680 .flags = 0,
681 };
Courtney Goeltzenleuchterddcb6192015-04-14 18:48:46 -0600682 VkMemoryAllocInfo mem_alloc = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600683 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
Mark Lobodzinski97dcd042015-04-16 08:52:00 -0500684 .pNext = NULL,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600685 .allocationSize = 0,
Mark Lobodzinski72346292015-07-02 16:49:40 -0600686 .memoryTypeIndex = 0,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600687 };
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600688 VkDepthStencilViewCreateInfo view = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600689 .sType = VK_STRUCTURE_TYPE_DEPTH_STENCIL_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600690 .pNext = NULL,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600691 .image = VK_NULL_HANDLE,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600692 .mipLevel = 0,
693 .baseArraySlice = 0,
694 .arraySize = 1,
695 .flags = 0,
696 };
Mike Stroyan230e6252015-04-17 12:36:38 -0600697
Mark Lobodzinski23182612015-05-29 09:32:35 -0500698 VkMemoryRequirements mem_reqs;
Tony Barbour22a30862015-04-22 09:02:32 -0600699 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600700
701 demo->depth.format = depth_format;
702
703 /* create image */
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600704 err = vkCreateImage(demo->device, &image,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600705 &demo->depth.image);
706 assert(!err);
707
Tony Barbour426b9052015-06-24 16:06:58 -0600708 err = vkGetObjectMemoryRequirements(demo->device,
709 VK_OBJECT_TYPE_IMAGE, demo->depth.image, &mem_reqs);
Mark Lobodzinski72346292015-07-02 16:49:40 -0600710
711 mem_alloc.allocationSize = mem_reqs.size;
712 err = memory_type_from_properties(demo,
713 mem_reqs.memoryTypeBits,
714 VK_MEMORY_PROPERTY_DEVICE_ONLY,
715 &mem_alloc.memoryTypeIndex);
716 assert(!err);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600717
Mark Lobodzinski23182612015-05-29 09:32:35 -0500718 /* allocate memory */
719 err = vkAllocMemory(demo->device, &mem_alloc, &demo->depth.mem);
720 assert(!err);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600721
Mark Lobodzinski23182612015-05-29 09:32:35 -0500722 /* bind memory */
723 err = vkBindObjectMemory(demo->device,
724 VK_OBJECT_TYPE_IMAGE, demo->depth.image,
725 demo->depth.mem, 0);
726 assert(!err);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600727
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600728 demo_set_image_layout(demo, demo->depth.image,
malnasse4b8ba4d2015-06-03 17:28:38 -0400729 VK_IMAGE_ASPECT_DEPTH,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600730 VK_IMAGE_LAYOUT_UNDEFINED,
731 VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600732
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600733 /* create image view */
734 view.image = demo->depth.image;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600735 err = vkCreateDepthStencilView(demo->device, &view,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600736 &demo->depth.view);
737 assert(!err);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600738}
739
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600740/** loadTexture
Courtney Goeltzenleuchtercb67a322015-04-21 09:31:23 -0600741 * loads a png file into an memory object, using cstdio , libpng.
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600742 *
Courtney Goeltzenleuchtercb67a322015-04-21 09:31:23 -0600743 * \param demo : Needed to access VK calls
744 * \param filename : the png file to be loaded
745 * \param width : width of png, to be updated as a side effect of this function
746 * \param height : height of png, to be updated as a side effect of this function
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600747 *
Courtney Goeltzenleuchtercb67a322015-04-21 09:31:23 -0600748 * \return bool : an opengl texture id. true if successful?,
749 * should be validated by the client of this function.
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600750 *
751 * Source: http://en.wikibooks.org/wiki/OpenGL_Programming/Intermediate/Textures
752 * Modified to copy image to memory
753 *
754 */
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700755bool loadTexture(const char *filename, uint8_t *rgba_data,
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600756 VkSubresourceLayout *layout,
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600757 int32_t *width, int32_t *height)
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600758{
759 //header for testing if it is a png
760 png_byte header[8];
Tony Barboura938abb2015-04-22 11:36:22 -0600761 int is_png, bit_depth, color_type, rowbytes;
762 size_t retval;
Ian Elliott642f8922015-02-13 14:29:21 -0700763 png_uint_32 i, twidth, theight;
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600764 png_structp png_ptr;
765 png_infop info_ptr, end_info;
766 png_byte *image_data;
767 png_bytep *row_pointers;
768
769 //open file as binary
770 FILE *fp = fopen(filename, "rb");
771 if (!fp) {
772 return false;
773 }
774
775 //read the header
Tony Barbour22a30862015-04-22 09:02:32 -0600776 retval = fread(header, 1, 8, fp);
777 if (retval != 8) {
778 fclose(fp);
779 return false;
780 }
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600781
782 //test if png
783 is_png = !png_sig_cmp(header, 0, 8);
784 if (!is_png) {
785 fclose(fp);
786 return false;
787 }
788
789 //create png struct
790 png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL,
791 NULL, NULL);
792 if (!png_ptr) {
793 fclose(fp);
794 return (false);
795 }
796
797 //create png info struct
798 info_ptr = png_create_info_struct(png_ptr);
799 if (!info_ptr) {
800 png_destroy_read_struct(&png_ptr, (png_infopp) NULL, (png_infopp) NULL);
801 fclose(fp);
802 return (false);
803 }
804
805 //create png info struct
806 end_info = png_create_info_struct(png_ptr);
807 if (!end_info) {
808 png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp) NULL);
809 fclose(fp);
810 return (false);
811 }
812
813 //png error stuff, not sure libpng man suggests this.
814 if (setjmp(png_jmpbuf(png_ptr))) {
815 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
816 fclose(fp);
817 return (false);
818 }
819
820 //init png reading
821 png_init_io(png_ptr, fp);
822
823 //let libpng know you already read the first 8 bytes
824 png_set_sig_bytes(png_ptr, 8);
825
826 // read all the info up to the image data
827 png_read_info(png_ptr, info_ptr);
828
829 // get info about png
830 png_get_IHDR(png_ptr, info_ptr, &twidth, &theight, &bit_depth, &color_type,
831 NULL, NULL, NULL);
832
833 //update width and height based on png info
834 *width = twidth;
835 *height = theight;
836
837 // Require that incoming texture be 8bits per color component
838 // and 4 components (RGBA).
839 if (png_get_bit_depth(png_ptr, info_ptr) != 8 ||
840 png_get_channels(png_ptr, info_ptr) != 4) {
841 return false;
842 }
843
844 if (rgba_data == NULL) {
845 // If data pointer is null, we just want the width & height
846 // clean up memory and close stuff
847 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
848 fclose(fp);
849
850 return true;
851 }
852
853 // Update the png info struct.
854 png_read_update_info(png_ptr, info_ptr);
855
856 // Row size in bytes.
857 rowbytes = png_get_rowbytes(png_ptr, info_ptr);
858
859 // Allocate the image_data as a big block, to be given to opengl
860 image_data = (png_byte *)malloc(rowbytes * theight * sizeof(png_byte));
861 if (!image_data) {
862 //clean up memory and close stuff
863 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
864 fclose(fp);
865 return false;
866 }
867
868 // row_pointers is for pointing to image_data for reading the png with libpng
869 row_pointers = (png_bytep *)malloc(theight * sizeof(png_bytep));
870 if (!row_pointers) {
871 //clean up memory and close stuff
872 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
873 // delete[] image_data;
874 fclose(fp);
875 return false;
876 }
877 // set the individual row_pointers to point at the correct offsets of image_data
878 for (i = 0; i < theight; ++i)
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700879 row_pointers[theight - 1 - i] = rgba_data + i * layout->rowPitch;
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600880
881 // read the png into image_data through row_pointers
882 png_read_image(png_ptr, row_pointers);
883
884 // clean up memory and close stuff
885 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
886 free(row_pointers);
887 free(image_data);
888 fclose(fp);
889
890 return true;
891}
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600892
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700893static void demo_prepare_texture_image(struct demo *demo,
894 const char *filename,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600895 struct texture_object *tex_obj,
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600896 VkImageTiling tiling,
Courtney Goeltzenleuchtercb67a322015-04-21 09:31:23 -0600897 VkImageUsageFlags usage,
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600898 VkFlags mem_props)
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600899{
Mike Stroyande24a6f2015-06-15 14:21:03 -0600900 const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600901 int32_t tex_width;
902 int32_t tex_height;
Tony Barbour22a30862015-04-22 09:02:32 -0600903 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700904
David Pinedo61e77382015-04-23 08:16:57 -0600905 if (!loadTexture(filename, NULL, NULL, &tex_width, &tex_height))
906 {
907 printf("Failed to load textures\n");
908 fflush(stdout);
909 exit(1);
910 }
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700911
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600912 tex_obj->tex_width = tex_width;
913 tex_obj->tex_height = tex_height;
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700914
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600915 const VkImageCreateInfo image_create_info = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600916 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700917 .pNext = NULL,
Tony Barbour8205d902015-04-16 15:59:00 -0600918 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700919 .format = tex_format,
920 .extent = { tex_width, tex_height, 1 },
921 .mipLevels = 1,
922 .arraySize = 1,
923 .samples = 1,
924 .tiling = tiling,
Courtney Goeltzenleuchtercb67a322015-04-21 09:31:23 -0600925 .usage = usage,
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700926 .flags = 0,
927 };
Courtney Goeltzenleuchterddcb6192015-04-14 18:48:46 -0600928 VkMemoryAllocInfo mem_alloc = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600929 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
Mark Lobodzinski97dcd042015-04-16 08:52:00 -0500930 .pNext = NULL,
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700931 .allocationSize = 0,
Mark Lobodzinski72346292015-07-02 16:49:40 -0600932 .memoryTypeIndex = 0,
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700933 };
934
Mark Lobodzinski23182612015-05-29 09:32:35 -0500935 VkMemoryRequirements mem_reqs;
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700936
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600937 err = vkCreateImage(demo->device, &image_create_info,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600938 &tex_obj->image);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700939 assert(!err);
940
Tony Barbour426b9052015-06-24 16:06:58 -0600941 err = vkGetObjectMemoryRequirements(demo->device,
942 VK_OBJECT_TYPE_IMAGE, tex_obj->image, &mem_reqs);
943 assert(!err);
Piers Daniell886be472015-02-23 16:23:13 -0700944
Mark Lobodzinski23182612015-05-29 09:32:35 -0500945 mem_alloc.allocationSize = mem_reqs.size;
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700946
Mark Lobodzinski72346292015-07-02 16:49:40 -0600947 err = memory_type_from_properties(demo, mem_reqs.memoryTypeBits, mem_props, &mem_alloc.memoryTypeIndex);
948 assert(!err);
949
Mark Lobodzinski23182612015-05-29 09:32:35 -0500950 /* allocate memory */
951 err = vkAllocMemory(demo->device, &mem_alloc,
952 &(tex_obj->mem));
953 assert(!err);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700954
Mark Lobodzinski23182612015-05-29 09:32:35 -0500955 /* bind memory */
956 err = vkBindObjectMemory(demo->device,
957 VK_OBJECT_TYPE_IMAGE, tex_obj->image,
958 tex_obj->mem, 0);
959 assert(!err);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700960
Tony Barbour8205d902015-04-16 15:59:00 -0600961 if (mem_props & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) {
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600962 const VkImageSubresource subres = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600963 .aspect = VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700964 .mipLevel = 0,
965 .arraySlice = 0,
966 };
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600967 VkSubresourceLayout layout;
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700968 void *data;
969
Tony Barbour426b9052015-06-24 16:06:58 -0600970 err = vkGetImageSubresourceLayout(demo->device, tex_obj->image, &subres, &layout);
971 assert(!err);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700972
Mark Lobodzinski23182612015-05-29 09:32:35 -0500973 err = vkMapMemory(demo->device, tex_obj->mem, 0, 0, 0, &data);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700974 assert(!err);
975
976 if (!loadTexture(filename, data, &layout, &tex_width, &tex_height)) {
977 fprintf(stderr, "Error loading texture: %s\n", filename);
978 }
979
Mark Lobodzinski23182612015-05-29 09:32:35 -0500980 err = vkUnmapMemory(demo->device, tex_obj->mem);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700981 assert(!err);
982 }
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600983
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600984 tex_obj->imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600985 demo_set_image_layout(demo, tex_obj->image,
malnasse4b8ba4d2015-06-03 17:28:38 -0400986 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600987 VK_IMAGE_LAYOUT_UNDEFINED,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600988 tex_obj->imageLayout);
989 /* 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 -0700990}
991
Mark Lobodzinskicf26e072015-04-16 11:44:05 -0500992static void demo_destroy_texture_image(struct demo *demo, struct texture_object *tex_objs)
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700993{
994 /* clean up staging resources */
Mark Lobodzinski23182612015-05-29 09:32:35 -0500995 vkFreeMemory(demo->device, tex_objs->mem);
Mike Stroyan230e6252015-04-17 12:36:38 -0600996 vkDestroyObject(demo->device, VK_OBJECT_TYPE_IMAGE, tex_objs->image);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700997}
998
999static void demo_prepare_textures(struct demo *demo)
1000{
Tony Barbour8205d902015-04-16 15:59:00 -06001001 const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001002 VkFormatProperties props;
Tony Barbour22a30862015-04-22 09:02:32 -06001003 VkResult U_ASSERT_ONLY err;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001004 uint32_t i;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001005
Chris Forbesd7576302015-06-21 22:55:02 +12001006 err = vkGetPhysicalDeviceFormatInfo(demo->gpu, tex_format, &props);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001007 assert(!err);
1008
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001009 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001010
FslNopper434db6a2015-05-06 21:42:01 +02001011 if ((props.linearTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) && !demo->use_staging_buffer) {
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001012 /* Device can texture using linear textures */
1013 demo_prepare_texture_image(demo, tex_files[i], &demo->textures[i],
Courtney Goeltzenleuchtercb67a322015-04-21 09:31:23 -06001014 VK_IMAGE_TILING_LINEAR, VK_IMAGE_USAGE_SAMPLED_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
Tony Barbour8205d902015-04-16 15:59:00 -06001015 } else if (props.optimalTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) {
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001016 /* Must use staging buffer to copy linear texture to optimized */
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -06001017 struct texture_object staging_texture;
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001018
1019 memset(&staging_texture, 0, sizeof(staging_texture));
1020 demo_prepare_texture_image(demo, tex_files[i], &staging_texture,
Courtney Goeltzenleuchtercb67a322015-04-21 09:31:23 -06001021 VK_IMAGE_TILING_LINEAR, VK_IMAGE_USAGE_TRANSFER_SOURCE_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001022
1023 demo_prepare_texture_image(demo, tex_files[i], &demo->textures[i],
Courtney Goeltzenleuchtercb67a322015-04-21 09:31:23 -06001024 VK_IMAGE_TILING_OPTIMAL,
1025 (VK_IMAGE_USAGE_TRANSFER_DESTINATION_BIT | VK_IMAGE_USAGE_SAMPLED_BIT),
1026 VK_MEMORY_PROPERTY_DEVICE_ONLY);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001027
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -06001028 demo_set_image_layout(demo, staging_texture.image,
malnasse4b8ba4d2015-06-03 17:28:38 -04001029 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -06001030 staging_texture.imageLayout,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001031 VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001032
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -06001033 demo_set_image_layout(demo, demo->textures[i].image,
malnasse4b8ba4d2015-06-03 17:28:38 -04001034 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -06001035 demo->textures[i].imageLayout,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001036 VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001037
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001038 VkImageCopy copy_region = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001039 .srcSubresource = { VK_IMAGE_ASPECT_COLOR, 0, 0 },
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001040 .srcOffset = { 0, 0, 0 },
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001041 .destSubresource = { VK_IMAGE_ASPECT_COLOR, 0, 0 },
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001042 .destOffset = { 0, 0, 0 },
1043 .extent = { staging_texture.tex_width, staging_texture.tex_height, 1 },
1044 };
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001045 vkCmdCopyImage(demo->cmd,
1046 staging_texture.image, VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL,
1047 demo->textures[i].image, VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL,
Courtney Goeltzenleuchter51cbf302015-03-25 11:25:10 -06001048 1, &copy_region);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001049
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -06001050 demo_set_image_layout(demo, demo->textures[i].image,
malnasse4b8ba4d2015-06-03 17:28:38 -04001051 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001052 VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -06001053 demo->textures[i].imageLayout);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001054
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -06001055 demo_flush_init_cmd(demo);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001056
Courtney Goeltzenleuchter876629f2015-04-21 09:30:03 -06001057 demo_destroy_texture_image(demo, &staging_texture);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001058 } else {
Mike Stroyande24a6f2015-06-15 14:21:03 -06001059 /* Can't support VK_FORMAT_R8G8B8A8_UNORM !? */
1060 assert(!"No support for R8G8B8A8_UNORM as texture image format");
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001061 }
1062
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001063 const VkSamplerCreateInfo sampler = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001064 .sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001065 .pNext = NULL,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001066 .magFilter = VK_TEX_FILTER_NEAREST,
1067 .minFilter = VK_TEX_FILTER_NEAREST,
Tony Barbour8205d902015-04-16 15:59:00 -06001068 .mipMode = VK_TEX_MIPMAP_MODE_BASE,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001069 .addressU = VK_TEX_ADDRESS_CLAMP,
1070 .addressV = VK_TEX_ADDRESS_CLAMP,
1071 .addressW = VK_TEX_ADDRESS_CLAMP,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001072 .mipLodBias = 0.0f,
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001073 .maxAnisotropy = 1,
Tony Barbour8205d902015-04-16 15:59:00 -06001074 .compareOp = VK_COMPARE_OP_NEVER,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001075 .minLod = 0.0f,
1076 .maxLod = 0.0f,
Tony Barbour2c4e7c72015-06-25 16:56:44 -06001077 .borderColor = VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001078 };
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -06001079
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001080 VkImageViewCreateInfo view = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001081 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001082 .pNext = NULL,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001083 .image = VK_NULL_HANDLE,
Tony Barbour8205d902015-04-16 15:59:00 -06001084 .viewType = VK_IMAGE_VIEW_TYPE_2D,
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001085 .format = tex_format,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001086 .channels = { VK_CHANNEL_SWIZZLE_R,
1087 VK_CHANNEL_SWIZZLE_G,
1088 VK_CHANNEL_SWIZZLE_B,
1089 VK_CHANNEL_SWIZZLE_A, },
1090 .subresourceRange = { VK_IMAGE_ASPECT_COLOR, 0, 1, 0, 1 },
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001091 };
Jon Ashburna9ae3832015-01-16 09:37:43 -07001092
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001093 /* create sampler */
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001094 err = vkCreateSampler(demo->device, &sampler,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001095 &demo->textures[i].sampler);
1096 assert(!err);
1097
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001098 /* create image view */
1099 view.image = demo->textures[i].image;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001100 err = vkCreateImageView(demo->device, &view,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001101 &demo->textures[i].view);
1102 assert(!err);
1103 }
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001104}
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001105
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001106void demo_prepare_cube_data_buffer(struct demo *demo)
1107{
Courtney Goeltzenleuchterddcb6192015-04-14 18:48:46 -06001108 VkBufferCreateInfo buf_info;
1109 VkBufferViewCreateInfo view_info;
Courtney Goeltzenleuchterddcb6192015-04-14 18:48:46 -06001110 VkMemoryAllocInfo alloc_info = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001111 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
Mark Lobodzinski97dcd042015-04-16 08:52:00 -05001112 .pNext = NULL,
Jon Ashburnc6ae13d2015-01-19 15:00:26 -07001113 .allocationSize = 0,
Mark Lobodzinski72346292015-07-02 16:49:40 -06001114 .memoryTypeIndex = 0,
Jon Ashburnc6ae13d2015-01-19 15:00:26 -07001115 };
Mark Lobodzinski23182612015-05-29 09:32:35 -05001116 VkMemoryRequirements mem_reqs;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001117 size_t mem_reqs_size = sizeof(VkMemoryRequirements);
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001118 uint8_t *pData;
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001119 int i;
1120 mat4x4 MVP, VP;
Tony Barbour22a30862015-04-22 09:02:32 -06001121 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001122 struct vktexcube_vs_uniform data;
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001123
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001124 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001125 mat4x4_mul(MVP, VP, demo->model_matrix);
1126 memcpy(data.mvp, MVP, sizeof(MVP));
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -06001127// dumpMatrix("MVP", MVP);
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001128
1129 for (i=0; i<12*3; i++) {
1130 data.position[i][0] = g_vertex_buffer_data[i*3];
1131 data.position[i][1] = g_vertex_buffer_data[i*3+1];
1132 data.position[i][2] = g_vertex_buffer_data[i*3+2];
1133 data.position[i][3] = 1.0f;
1134 data.attr[i][0] = g_uv_buffer_data[2*i];
1135 data.attr[i][1] = g_uv_buffer_data[2*i + 1];
1136 data.attr[i][2] = 0;
1137 data.attr[i][3] = 0;
1138 }
1139
Chia-I Wu714df452015-01-01 07:55:04 +08001140 memset(&buf_info, 0, sizeof(buf_info));
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001141 buf_info.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
Courtney Goeltzenleuchterad870812015-04-15 15:29:59 -06001142 buf_info.usage = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001143 err = vkCreateBuffer(demo->device, &buf_info, &demo->uniform_data.buf);
Chia-I Wu714df452015-01-01 07:55:04 +08001144 assert(!err);
1145
Tony Barbour426b9052015-06-24 16:06:58 -06001146 err = vkGetObjectMemoryRequirements(demo->device,
1147 VK_OBJECT_TYPE_BUFFER, demo->uniform_data.buf, &mem_reqs);
Mark Lobodzinski23182612015-05-29 09:32:35 -05001148 assert(!err && mem_reqs_size == sizeof(mem_reqs));
Chia-I Wu714df452015-01-01 07:55:04 +08001149
Mark Lobodzinski23182612015-05-29 09:32:35 -05001150 alloc_info.allocationSize = mem_reqs.size;
Mark Lobodzinski72346292015-07-02 16:49:40 -06001151 err = memory_type_from_properties(demo,
1152 mem_reqs.memoryTypeBits,
1153 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT,
1154 &alloc_info.memoryTypeIndex);
1155 assert(!err);
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001156
Mark Lobodzinski23182612015-05-29 09:32:35 -05001157 err = vkAllocMemory(demo->device, &alloc_info, &(demo->uniform_data.mem));
1158 assert(!err);
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001159
Mark Lobodzinski23182612015-05-29 09:32:35 -05001160 err = vkMapMemory(demo->device, demo->uniform_data.mem, 0, 0, 0, (void **) &pData);
1161 assert(!err);
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001162
Mark Lobodzinski23182612015-05-29 09:32:35 -05001163 memcpy(pData, &data, sizeof data);
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001164
Mark Lobodzinski23182612015-05-29 09:32:35 -05001165 err = vkUnmapMemory(demo->device, demo->uniform_data.mem);
1166 assert(!err);
1167
1168 err = vkBindObjectMemory(demo->device,
1169 VK_OBJECT_TYPE_BUFFER, demo->uniform_data.buf,
1170 demo->uniform_data.mem, 0);
1171 assert(!err);
Chia-I Wu714df452015-01-01 07:55:04 +08001172
1173 memset(&view_info, 0, sizeof(view_info));
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001174 view_info.sType = VK_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO;
Chia-I Wu714df452015-01-01 07:55:04 +08001175 view_info.buffer = demo->uniform_data.buf;
Tony Barbour8205d902015-04-16 15:59:00 -06001176 view_info.viewType = VK_BUFFER_VIEW_TYPE_RAW;
Chia-I Wu714df452015-01-01 07:55:04 +08001177 view_info.offset = 0;
1178 view_info.range = sizeof(data);
1179
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001180 err = vkCreateBufferView(demo->device, &view_info, &demo->uniform_data.view);
Chia-I Wu714df452015-01-01 07:55:04 +08001181 assert(!err);
1182
Chia-I Wu8cd8ecd2015-05-25 16:27:55 +08001183 demo->uniform_data.desc.bufferView = demo->uniform_data.view;
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001184}
1185
Chia-I Wuf8385062015-01-04 16:27:24 +08001186static void demo_prepare_descriptor_layout(struct demo *demo)
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001187{
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001188 const VkDescriptorSetLayoutBinding layout_bindings[2] = {
Chia-I Wufc9d9132015-03-26 15:04:41 +08001189 [0] = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001190 .descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
Chia-I Wud3114a22015-05-25 16:22:52 +08001191 .arraySize = 1,
Tony Barbour8205d902015-04-16 15:59:00 -06001192 .stageFlags = VK_SHADER_STAGE_VERTEX_BIT,
Chia-I Wu310eece2015-03-27 12:56:09 +08001193 .pImmutableSamplers = NULL,
Chia-I Wufc9d9132015-03-26 15:04:41 +08001194 },
1195 [1] = {
Courtney Goeltzenleuchterad870812015-04-15 15:29:59 -06001196 .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
Chia-I Wud3114a22015-05-25 16:22:52 +08001197 .arraySize = DEMO_TEXTURE_COUNT,
Tony Barbour8205d902015-04-16 15:59:00 -06001198 .stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT,
Chia-I Wu310eece2015-03-27 12:56:09 +08001199 .pImmutableSamplers = NULL,
Chia-I Wufc9d9132015-03-26 15:04:41 +08001200 },
1201 };
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001202 const VkDescriptorSetLayoutCreateInfo descriptor_layout = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001203 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
Chia-I Wuf8385062015-01-04 16:27:24 +08001204 .pNext = NULL,
Chia-I Wufc9d9132015-03-26 15:04:41 +08001205 .count = 2,
1206 .pBinding = layout_bindings,
Chia-I Wuf8385062015-01-04 16:27:24 +08001207 };
Tony Barbour22a30862015-04-22 09:02:32 -06001208 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001209
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001210 err = vkCreateDescriptorSetLayout(demo->device,
Chia-I Wu7732cb22015-03-26 15:27:55 +08001211 &descriptor_layout, &demo->desc_layout);
1212 assert(!err);
1213
Mark Lobodzinski556f7212015-04-17 14:11:39 -05001214 const VkPipelineLayoutCreateInfo pPipelineLayoutCreateInfo = {
1215 .sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
1216 .pNext = NULL,
1217 .descriptorSetCount = 1,
1218 .pSetLayouts = &demo->desc_layout,
1219 };
1220
1221 err = vkCreatePipelineLayout(demo->device,
1222 &pPipelineLayoutCreateInfo,
1223 &demo->pipeline_layout);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001224 assert(!err);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001225}
1226
Tobin Ehlise796a1d2015-07-02 11:02:49 -06001227static VkShader demo_prepare_shader(struct demo* demo,
Tony Barbour8205d902015-04-16 15:59:00 -06001228 VkShaderStage stage,
Tobin Ehlise796a1d2015-07-02 11:02:49 -06001229 const void* code,
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001230 size_t size)
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001231{
Courtney Goeltzenleuchter0b29b0d2015-06-24 18:24:19 -06001232 VkShaderModuleCreateInfo moduleCreateInfo;
1233 VkShaderCreateInfo shaderCreateInfo;
1234 VkShaderModule shaderModule;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001235 VkShader shader;
1236 VkResult err;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001237
Courtney Goeltzenleuchter7e334982014-10-30 15:14:16 -06001238
Courtney Goeltzenleuchter0b29b0d2015-06-24 18:24:19 -06001239 moduleCreateInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
1240 moduleCreateInfo.pNext = NULL;
1241
1242 shaderCreateInfo.sType = VK_STRUCTURE_TYPE_SHADER_CREATE_INFO;
1243 shaderCreateInfo.pNext = NULL;
Tobin Ehlise796a1d2015-07-02 11:02:49 -06001244 shaderCreateInfo.pName = "main";
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001245
Cody Northrop75db0322015-05-28 11:27:16 -06001246 if (!demo->use_glsl) {
Courtney Goeltzenleuchter0b29b0d2015-06-24 18:24:19 -06001247 moduleCreateInfo.codeSize = size;
1248 moduleCreateInfo.pCode = code;
1249 moduleCreateInfo.flags = 0;
1250 err = vkCreateShaderModule(demo->device, &moduleCreateInfo, &shaderModule);
Cody Northrop75db0322015-05-28 11:27:16 -06001251 if (err) {
Courtney Goeltzenleuchter0b29b0d2015-06-24 18:24:19 -06001252 free((void *) moduleCreateInfo.pCode);
Cody Northrop75db0322015-05-28 11:27:16 -06001253 }
Courtney Goeltzenleuchter0b29b0d2015-06-24 18:24:19 -06001254
1255 shaderCreateInfo.flags = 0;
1256 shaderCreateInfo.module = shaderModule;
1257 err = vkCreateShader(demo->device, &shaderCreateInfo, &shader);
Cody Northrop75db0322015-05-28 11:27:16 -06001258 } else {
1259 // Create fake SPV structure to feed GLSL
1260 // to the driver "under the covers"
Courtney Goeltzenleuchter0b29b0d2015-06-24 18:24:19 -06001261 moduleCreateInfo.codeSize = 3 * sizeof(uint32_t) + size + 1;
1262 moduleCreateInfo.pCode = malloc(moduleCreateInfo.codeSize);
1263 moduleCreateInfo.flags = 0;
Cody Northrop75db0322015-05-28 11:27:16 -06001264
1265 /* try version 0 first: VkShaderStage followed by GLSL */
Courtney Goeltzenleuchter0b29b0d2015-06-24 18:24:19 -06001266 ((uint32_t *) moduleCreateInfo.pCode)[0] = ICD_SPV_MAGIC;
1267 ((uint32_t *) moduleCreateInfo.pCode)[1] = 0;
1268 ((uint32_t *) moduleCreateInfo.pCode)[2] = stage;
1269 memcpy(((uint32_t *) moduleCreateInfo.pCode + 3), code, size + 1);
Cody Northrop75db0322015-05-28 11:27:16 -06001270
Courtney Goeltzenleuchter0b29b0d2015-06-24 18:24:19 -06001271 err = vkCreateShaderModule(demo->device, &moduleCreateInfo, &shaderModule);
Cody Northrop75db0322015-05-28 11:27:16 -06001272 if (err) {
Courtney Goeltzenleuchter0b29b0d2015-06-24 18:24:19 -06001273 free((void *) moduleCreateInfo.pCode);
Cody Northrop75db0322015-05-28 11:27:16 -06001274 }
Courtney Goeltzenleuchter0b29b0d2015-06-24 18:24:19 -06001275
1276 shaderCreateInfo.flags = 0;
1277 shaderCreateInfo.module = shaderModule;
1278 err = vkCreateShader(demo->device, &shaderCreateInfo, &shader);
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -06001279 }
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001280 return shader;
1281}
1282
Cody Northropacfb0492015-03-17 15:55:58 -06001283char *demo_read_spv(const char *filename, size_t *psize)
Courtney Goeltzenleuchter7e334982014-10-30 15:14:16 -06001284{
1285 long int size;
Tony Barboura938abb2015-04-22 11:36:22 -06001286 size_t U_ASSERT_ONLY retval;
Courtney Goeltzenleuchter7e334982014-10-30 15:14:16 -06001287 void *shader_code;
1288
1289 FILE *fp = fopen(filename, "rb");
1290 if (!fp) return NULL;
1291
1292 fseek(fp, 0L, SEEK_END);
1293 size = ftell(fp);
1294
1295 fseek(fp, 0L, SEEK_SET);
1296
1297 shader_code = malloc(size);
Tony Barbour22a30862015-04-22 09:02:32 -06001298 retval = fread(shader_code, size, 1, fp);
1299 assert(retval == 1);
Courtney Goeltzenleuchter7e334982014-10-30 15:14:16 -06001300
1301 *psize = size;
1302
1303 return shader_code;
1304}
1305
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001306static VkShader demo_prepare_vs(struct demo *demo)
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001307{
Cody Northrop75db0322015-05-28 11:27:16 -06001308 if (!demo->use_glsl) {
1309 void *vertShaderCode;
1310 size_t size;
Courtney Goeltzenleuchter7e334982014-10-30 15:14:16 -06001311
Cody Northrop75db0322015-05-28 11:27:16 -06001312 vertShaderCode = demo_read_spv("cube-vert.spv", &size);
Courtney Goeltzenleuchter7e334982014-10-30 15:14:16 -06001313
Cody Northrop75db0322015-05-28 11:27:16 -06001314 return demo_prepare_shader(demo, VK_SHADER_STAGE_VERTEX,
1315 vertShaderCode, size);
1316 } else {
1317 static const char *vertShaderText =
1318 "#version 140\n"
1319 "#extension GL_ARB_separate_shader_objects : enable\n"
1320 "#extension GL_ARB_shading_language_420pack : enable\n"
1321 "\n"
1322 "layout(binding = 0) uniform buf {\n"
1323 " mat4 MVP;\n"
1324 " vec4 position[12*3];\n"
1325 " vec4 attr[12*3];\n"
1326 "} ubuf;\n"
1327 "\n"
1328 "layout (location = 0) out vec4 texcoord;\n"
1329 "\n"
1330 "void main() \n"
1331 "{\n"
1332 " texcoord = ubuf.attr[gl_VertexID];\n"
1333 " gl_Position = ubuf.MVP * ubuf.position[gl_VertexID];\n"
1334 "\n"
1335 " // GL->VK conventions\n"
1336 " gl_Position.y = -gl_Position.y;\n"
1337 " gl_Position.z = (gl_Position.z + gl_Position.w) / 2.0;\n"
1338 "}\n";
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -06001339
Cody Northrop75db0322015-05-28 11:27:16 -06001340 return demo_prepare_shader(demo, VK_SHADER_STAGE_VERTEX,
1341 (const void *) vertShaderText,
1342 strlen(vertShaderText));
1343 }
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001344}
1345
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001346static VkShader demo_prepare_fs(struct demo *demo)
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001347{
Cody Northrop75db0322015-05-28 11:27:16 -06001348 if (!demo->use_glsl) {
1349 void *fragShaderCode;
1350 size_t size;
Courtney Goeltzenleuchter7e334982014-10-30 15:14:16 -06001351
Cody Northrop75db0322015-05-28 11:27:16 -06001352 fragShaderCode = demo_read_spv("cube-frag.spv", &size);
Courtney Goeltzenleuchter7e334982014-10-30 15:14:16 -06001353
Cody Northrop75db0322015-05-28 11:27:16 -06001354 return demo_prepare_shader(demo, VK_SHADER_STAGE_FRAGMENT,
1355 fragShaderCode, size);
1356 } else {
1357 static const char *fragShaderText =
1358 "#version 140\n"
1359 "#extension GL_ARB_separate_shader_objects : enable\n"
1360 "#extension GL_ARB_shading_language_420pack : enable\n"
1361 "layout (binding = 1) uniform sampler2D tex;\n"
1362 "\n"
1363 "layout (location = 0) in vec4 texcoord;\n"
1364 "layout (location = 0) out vec4 uFragColor;\n"
1365 "void main() {\n"
1366 " uFragColor = texture(tex, texcoord.xy);\n"
1367 "}\n";
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -06001368
Cody Northrop75db0322015-05-28 11:27:16 -06001369 return demo_prepare_shader(demo, VK_SHADER_STAGE_FRAGMENT,
1370 (const void *) fragShaderText,
1371 strlen(fragShaderText));
1372 }
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001373}
1374
1375static void demo_prepare_pipeline(struct demo *demo)
1376{
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001377 VkGraphicsPipelineCreateInfo pipeline;
Mark Lobodzinski0e0fb5c2015-06-23 15:11:57 -06001378
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001379 VkPipelineIaStateCreateInfo ia;
1380 VkPipelineRsStateCreateInfo rs;
1381 VkPipelineCbStateCreateInfo cb;
1382 VkPipelineDsStateCreateInfo ds;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001383 VkPipelineVpStateCreateInfo vp;
1384 VkPipelineMsStateCreateInfo ms;
Tony Barbour22a30862015-04-22 09:02:32 -06001385 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001386
1387 memset(&pipeline, 0, sizeof(pipeline));
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001388 pipeline.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
Mark Lobodzinski556f7212015-04-17 14:11:39 -05001389 pipeline.layout = demo->pipeline_layout;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001390
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001391 memset(&ia, 0, sizeof(ia));
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001392 ia.sType = VK_STRUCTURE_TYPE_PIPELINE_IA_STATE_CREATE_INFO;
Tony Barbour8205d902015-04-16 15:59:00 -06001393 ia.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001394
1395 memset(&rs, 0, sizeof(rs));
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001396 rs.sType = VK_STRUCTURE_TYPE_PIPELINE_RS_STATE_CREATE_INFO;
Tony Barbour8205d902015-04-16 15:59:00 -06001397 rs.fillMode = VK_FILL_MODE_SOLID;
1398 rs.cullMode = VK_CULL_MODE_BACK;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001399 rs.frontFace = VK_FRONT_FACE_CCW;
Chia-I Wue2504cb2015-04-22 14:20:52 +08001400 rs.depthClipEnable = VK_TRUE;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001401
1402 memset(&cb, 0, sizeof(cb));
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001403 cb.sType = VK_STRUCTURE_TYPE_PIPELINE_CB_STATE_CREATE_INFO;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001404 VkPipelineCbAttachmentState att_state[1];
Tony Barbourfa6cac72015-01-16 14:27:35 -07001405 memset(att_state, 0, sizeof(att_state));
1406 att_state[0].format = demo->format;
1407 att_state[0].channelWriteMask = 0xf;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001408 att_state[0].blendEnable = VK_FALSE;
Tony Barbourfa6cac72015-01-16 14:27:35 -07001409 cb.attachmentCount = 1;
1410 cb.pAttachments = att_state;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001411
Tony Barbourfa6cac72015-01-16 14:27:35 -07001412 memset(&vp, 0, sizeof(vp));
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001413 vp.sType = VK_STRUCTURE_TYPE_PIPELINE_VP_STATE_CREATE_INFO;
Tony Barbour8205d902015-04-16 15:59:00 -06001414 vp.viewportCount = 1;
Tony Barbourfa6cac72015-01-16 14:27:35 -07001415
1416 memset(&ds, 0, sizeof(ds));
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001417 ds.sType = VK_STRUCTURE_TYPE_PIPELINE_DS_STATE_CREATE_INFO;
Tony Barbourfa6cac72015-01-16 14:27:35 -07001418 ds.format = demo->depth.format;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001419 ds.depthTestEnable = VK_TRUE;
1420 ds.depthWriteEnable = VK_TRUE;
Tony Barbour8205d902015-04-16 15:59:00 -06001421 ds.depthCompareOp = VK_COMPARE_OP_LESS_EQUAL;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001422 ds.depthBoundsEnable = VK_FALSE;
1423 ds.back.stencilFailOp = VK_STENCIL_OP_KEEP;
1424 ds.back.stencilPassOp = VK_STENCIL_OP_KEEP;
Tony Barbour8205d902015-04-16 15:59:00 -06001425 ds.back.stencilCompareOp = VK_COMPARE_OP_ALWAYS;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001426 ds.stencilTestEnable = VK_FALSE;
Tony Barbourfa6cac72015-01-16 14:27:35 -07001427 ds.front = ds.back;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001428
Tony Barbourfa6cac72015-01-16 14:27:35 -07001429 memset(&ms, 0, sizeof(ms));
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001430 ms.sType = VK_STRUCTURE_TYPE_PIPELINE_MS_STATE_CREATE_INFO;
Tony Barbourfa6cac72015-01-16 14:27:35 -07001431 ms.sampleMask = 1;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001432 ms.multisampleEnable = VK_FALSE;
Tony Barboure094edf2015-06-26 10:18:34 -06001433 ms.rasterSamples = 1;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001434
Mark Lobodzinski0e0fb5c2015-06-23 15:11:57 -06001435 // Two stages: vs and fs
1436 pipeline.stageCount = 2;
1437 VkPipelineShaderStageCreateInfo shaderStages[2];
1438 memset(&shaderStages, 0, 2 * sizeof(VkPipelineShaderStageCreateInfo));
1439
1440 shaderStages[0].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1441 shaderStages[0].stage = VK_SHADER_STAGE_VERTEX;
1442 shaderStages[0].shader = demo_prepare_vs(demo);
1443
1444 shaderStages[1].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1445 shaderStages[1].stage = VK_SHADER_STAGE_FRAGMENT;
1446 shaderStages[1].shader = demo_prepare_fs(demo);
1447
Mark Lobodzinskic1e21462015-06-30 10:18:36 -06001448 pipeline.pVertexInputState = NULL;
Mark Lobodzinski0e0fb5c2015-06-23 15:11:57 -06001449 pipeline.pIaState = &ia;
1450 pipeline.pRsState = &rs;
1451 pipeline.pCbState = &cb;
1452 pipeline.pMsState = &ms;
1453 pipeline.pVpState = &vp;
1454 pipeline.pDsState = &ds;
1455 pipeline.pStages = shaderStages;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001456
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001457 err = vkCreateGraphicsPipeline(demo->device, &pipeline, &demo->pipeline);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001458 assert(!err);
1459
Mark Lobodzinski0e0fb5c2015-06-23 15:11:57 -06001460 for (uint32_t i = 0; i < pipeline.stageCount; i++) {
1461 vkDestroyObject(demo->device, VK_OBJECT_TYPE_SHADER, shaderStages[i].shader);
1462 }
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001463}
1464
1465static void demo_prepare_dynamic_states(struct demo *demo)
1466{
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001467 VkDynamicVpStateCreateInfo viewport_create;
1468 VkDynamicRsStateCreateInfo raster;
1469 VkDynamicCbStateCreateInfo color_blend;
1470 VkDynamicDsStateCreateInfo depth_stencil;
Tony Barbour22a30862015-04-22 09:02:32 -06001471 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001472
Tony Barbourfa6cac72015-01-16 14:27:35 -07001473 memset(&viewport_create, 0, sizeof(viewport_create));
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001474 viewport_create.sType = VK_STRUCTURE_TYPE_DYNAMIC_VP_STATE_CREATE_INFO;
Courtney Goeltzenleuchterc6e32f92015-02-11 14:13:34 -07001475 viewport_create.viewportAndScissorCount = 1;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001476 VkViewport viewport;
Piers Daniell886be472015-02-23 16:23:13 -07001477 memset(&viewport, 0, sizeof(viewport));
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001478 viewport.height = (float) demo->height;
1479 viewport.width = (float) demo->width;
1480 viewport.minDepth = (float) 0.0f;
1481 viewport.maxDepth = (float) 1.0f;
Piers Daniell886be472015-02-23 16:23:13 -07001482 viewport_create.pViewports = &viewport;
Chris Forbes2951d7d2015-06-22 17:21:59 +12001483 VkRect2D scissor;
Piers Daniell886be472015-02-23 16:23:13 -07001484 memset(&scissor, 0, sizeof(scissor));
Courtney Goeltzenleuchterc6e32f92015-02-11 14:13:34 -07001485 scissor.extent.width = demo->width;
1486 scissor.extent.height = demo->height;
1487 scissor.offset.x = 0;
1488 scissor.offset.y = 0;
Courtney Goeltzenleuchterc6e32f92015-02-11 14:13:34 -07001489 viewport_create.pScissors = &scissor;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001490
1491 memset(&raster, 0, sizeof(raster));
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001492 raster.sType = VK_STRUCTURE_TYPE_DYNAMIC_RS_STATE_CREATE_INFO;
Piers Daniell886be472015-02-23 16:23:13 -07001493 raster.lineWidth = 1.0;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001494
1495 memset(&color_blend, 0, sizeof(color_blend));
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001496 color_blend.sType = VK_STRUCTURE_TYPE_DYNAMIC_CB_STATE_CREATE_INFO;
Piers Daniell886be472015-02-23 16:23:13 -07001497 color_blend.blendConst[0] = 1.0f;
1498 color_blend.blendConst[1] = 1.0f;
1499 color_blend.blendConst[2] = 1.0f;
1500 color_blend.blendConst[3] = 1.0f;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001501
1502 memset(&depth_stencil, 0, sizeof(depth_stencil));
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001503 depth_stencil.sType = VK_STRUCTURE_TYPE_DYNAMIC_DS_STATE_CREATE_INFO;
Mark Lobodzinski4405fbf2015-06-12 11:14:17 -06001504 depth_stencil.minDepthBounds = 0.0f;
1505 depth_stencil.maxDepthBounds = 1.0f;
Tony Barbourfa6cac72015-01-16 14:27:35 -07001506 depth_stencil.stencilBackRef = 0;
1507 depth_stencil.stencilFrontRef = 0;
1508 depth_stencil.stencilReadMask = 0xff;
1509 depth_stencil.stencilWriteMask = 0xff;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001510
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001511 err = vkCreateDynamicViewportState(demo->device, &viewport_create, &demo->viewport);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001512 assert(!err);
1513
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001514 err = vkCreateDynamicRasterState(demo->device, &raster, &demo->raster);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001515 assert(!err);
1516
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001517 err = vkCreateDynamicColorBlendState(demo->device,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001518 &color_blend, &demo->color_blend);
1519 assert(!err);
1520
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001521 err = vkCreateDynamicDepthStencilState(demo->device,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001522 &depth_stencil, &demo->depth_stencil);
1523 assert(!err);
1524}
1525
Chia-I Wu8d24b3b2015-03-26 13:14:16 +08001526static void demo_prepare_descriptor_pool(struct demo *demo)
Chia-I Wuf8385062015-01-04 16:27:24 +08001527{
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001528 const VkDescriptorTypeCount type_counts[2] = {
Chia-I Wuf8385062015-01-04 16:27:24 +08001529 [0] = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001530 .type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
Chia-I Wuf8385062015-01-04 16:27:24 +08001531 .count = 1,
1532 },
1533 [1] = {
Courtney Goeltzenleuchterad870812015-04-15 15:29:59 -06001534 .type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
Chia-I Wuf8385062015-01-04 16:27:24 +08001535 .count = DEMO_TEXTURE_COUNT,
1536 },
1537 };
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001538 const VkDescriptorPoolCreateInfo descriptor_pool = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001539 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO,
Chia-I Wuf8385062015-01-04 16:27:24 +08001540 .pNext = NULL,
1541 .count = 2,
1542 .pTypeCount = type_counts,
1543 };
Tony Barbour22a30862015-04-22 09:02:32 -06001544 VkResult U_ASSERT_ONLY err;
Chia-I Wuf8385062015-01-04 16:27:24 +08001545
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001546 err = vkCreateDescriptorPool(demo->device,
1547 VK_DESCRIPTOR_POOL_USAGE_ONE_SHOT, 1,
Chia-I Wu8d24b3b2015-03-26 13:14:16 +08001548 &descriptor_pool, &demo->desc_pool);
Chia-I Wuf8385062015-01-04 16:27:24 +08001549 assert(!err);
1550}
1551
1552static void demo_prepare_descriptor_set(struct demo *demo)
1553{
Chia-I Wu8cd8ecd2015-05-25 16:27:55 +08001554 VkDescriptorInfo tex_descs[DEMO_TEXTURE_COUNT];
1555 VkWriteDescriptorSet writes[2];
Tony Barbour22a30862015-04-22 09:02:32 -06001556 VkResult U_ASSERT_ONLY err;
Chia-I Wuf8385062015-01-04 16:27:24 +08001557 uint32_t count;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001558 uint32_t i;
Chia-I Wuf8385062015-01-04 16:27:24 +08001559
Mike Stroyan230e6252015-04-17 12:36:38 -06001560 err = vkAllocDescriptorSets(demo->device, demo->desc_pool,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001561 VK_DESCRIPTOR_SET_USAGE_STATIC,
Chia-I Wu6e68a892015-02-23 10:41:08 -07001562 1, &demo->desc_layout,
Chia-I Wuf8385062015-01-04 16:27:24 +08001563 &demo->desc_set, &count);
1564 assert(!err && count == 1);
1565
Chia-I Wu8cd8ecd2015-05-25 16:27:55 +08001566 memset(&tex_descs, 0, sizeof(tex_descs));
1567 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
1568 tex_descs[i].sampler = demo->textures[i].sampler;
1569 tex_descs[i].imageView = demo->textures[i].view;
1570 tex_descs[i].imageLayout = VK_IMAGE_LAYOUT_GENERAL;
1571 }
1572
1573 memset(&writes, 0, sizeof(writes));
1574
1575 writes[0].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
1576 writes[0].destSet = demo->desc_set;
1577 writes[0].count = 1;
1578 writes[0].descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
1579 writes[0].pDescriptors = &demo->uniform_data.desc;
1580
1581 writes[1].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
1582 writes[1].destSet = demo->desc_set;
1583 writes[1].destBinding = 1;
1584 writes[1].count = DEMO_TEXTURE_COUNT;
1585 writes[1].descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
1586 writes[1].pDescriptors = tex_descs;
1587
1588 err = vkUpdateDescriptorSets(demo->device, 2, writes, 0, NULL);
1589 assert(!err);
Chia-I Wuf8385062015-01-04 16:27:24 +08001590}
1591
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001592static void demo_prepare(struct demo *demo)
1593{
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001594 const VkCmdBufferCreateInfo cmd = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001595 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001596 .pNext = NULL,
Courtney Goeltzenleuchterf3168062015-03-05 18:09:39 -07001597 .queueNodeIndex = demo->graphics_queue_node_index,
Chia-I Wu88eaa3b2015-06-26 15:34:39 +08001598 .level = VK_CMD_BUFFER_LEVEL_PRIMARY,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001599 .flags = 0,
1600 };
Tony Barbour22a30862015-04-22 09:02:32 -06001601 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001602
1603 demo_prepare_buffers(demo);
1604 demo_prepare_depth(demo);
1605 demo_prepare_textures(demo);
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -06001606 demo_prepare_cube_data_buffer(demo);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001607
Chia-I Wuf8385062015-01-04 16:27:24 +08001608 demo_prepare_descriptor_layout(demo);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001609 demo_prepare_pipeline(demo);
1610 demo_prepare_dynamic_states(demo);
1611
Courtney Goeltzenleuchterbd3b3aa2015-02-17 09:48:44 -07001612 for (int i = 0; i < DEMO_BUFFER_COUNT; i++) {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001613 err = vkCreateCommandBuffer(demo->device, &cmd, &demo->buffers[i].cmd);
Courtney Goeltzenleuchterbd3b3aa2015-02-17 09:48:44 -07001614 assert(!err);
1615 }
Chia-I Wuf8385062015-01-04 16:27:24 +08001616
Chia-I Wu8d24b3b2015-03-26 13:14:16 +08001617 demo_prepare_descriptor_pool(demo);
Chia-I Wuf8385062015-01-04 16:27:24 +08001618 demo_prepare_descriptor_set(demo);
Courtney Goeltzenleuchterbd3b3aa2015-02-17 09:48:44 -07001619
1620
1621 for (int i = 0; i < DEMO_BUFFER_COUNT; i++) {
1622 demo->current_buffer = i;
1623 demo_draw_build_cmd(demo, demo->buffers[i].cmd);
1624 }
1625
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -06001626 /*
1627 * Prepare functions above may generate pipeline commands
1628 * that need to be flushed before beginning the render loop.
1629 */
1630 demo_flush_init_cmd(demo);
1631
Courtney Goeltzenleuchterbd3b3aa2015-02-17 09:48:44 -07001632 demo->current_buffer = 0;
Jon Ashburn8a399e92015-04-24 09:46:24 -07001633 demo->prepared = true;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001634}
1635
David Pinedoeeca2a22015-06-18 17:03:14 -06001636static void demo_cleanup(struct demo *demo)
1637{
1638 uint32_t i;
1639
1640 demo->prepared = false;
1641
1642 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DESCRIPTOR_SET, demo->desc_set);
1643 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DESCRIPTOR_POOL, demo->desc_pool);
1644
1645 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DYNAMIC_VP_STATE, demo->viewport);
1646 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DYNAMIC_RS_STATE, demo->raster);
1647 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DYNAMIC_CB_STATE, demo->color_blend);
1648 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DYNAMIC_DS_STATE, demo->depth_stencil);
1649
1650 vkDestroyObject(demo->device, VK_OBJECT_TYPE_PIPELINE, demo->pipeline);
1651 vkDestroyObject(demo->device, VK_OBJECT_TYPE_PIPELINE_LAYOUT, demo->pipeline_layout);
1652 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DESCRIPTOR_SET_LAYOUT, demo->desc_layout);
1653
1654 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
1655 vkDestroyObject(demo->device, VK_OBJECT_TYPE_IMAGE_VIEW, demo->textures[i].view);
1656 vkDestroyObject(demo->device, VK_OBJECT_TYPE_IMAGE, demo->textures[i].image);
1657 vkFreeMemory(demo->device, demo->textures[i].mem);
1658 vkDestroyObject(demo->device, VK_OBJECT_TYPE_SAMPLER, demo->textures[i].sampler);
1659 }
1660 demo->fpDestroySwapChainWSI(demo->swap_chain);
1661
1662 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DEPTH_STENCIL_VIEW, demo->depth.view);
1663 vkDestroyObject(demo->device, VK_OBJECT_TYPE_IMAGE, demo->depth.image);
1664 vkFreeMemory(demo->device, demo->depth.mem);
1665
1666 vkDestroyObject(demo->device, VK_OBJECT_TYPE_BUFFER_VIEW, demo->uniform_data.view);
1667 vkDestroyObject(demo->device, VK_OBJECT_TYPE_BUFFER, demo->uniform_data.buf);
1668 vkFreeMemory(demo->device, demo->uniform_data.mem);
1669
1670 for (i = 0; i < DEMO_BUFFER_COUNT; i++) {
1671 vkDestroyObject(demo->device, VK_OBJECT_TYPE_COLOR_ATTACHMENT_VIEW, demo->buffers[i].view);
1672 vkDestroyObject(demo->device, VK_OBJECT_TYPE_COMMAND_BUFFER, demo->buffers[i].cmd);
1673 }
1674
1675 vkDestroyDevice(demo->device);
Tony Barboura65ecc22015-06-30 14:14:19 -06001676 if (demo->validate) {
1677 demo->dbgDestroyMsgCallback(demo->inst, demo->msg_callback);
1678 }
David Pinedoeeca2a22015-06-18 17:03:14 -06001679 vkDestroyInstance(demo->inst);
1680
1681#ifndef _WIN32
1682 xcb_destroy_window(demo->connection, demo->window);
1683 xcb_disconnect(demo->connection);
1684#endif // _WIN32
1685}
1686
1687// On MS-Windows, make this a global, so it's available to WndProc()
1688struct demo demo;
1689
Ian Elliotte14e9f92015-04-16 15:23:05 -06001690#ifdef _WIN32
1691static void demo_run(struct demo *demo)
1692{
Courtney Goeltzenleuchter857542b2015-04-27 14:56:34 -06001693 if (!demo->prepared)
1694 return;
Ian Elliotte14e9f92015-04-16 15:23:05 -06001695 // Wait for work to finish before updating MVP.
1696 vkDeviceWaitIdle(demo->device);
1697 demo_update_data_buffer(demo);
1698
1699 demo_draw(demo);
1700
1701 // Wait for work to finish before updating MVP.
1702 vkDeviceWaitIdle(demo->device);
Ian Elliotte14e9f92015-04-16 15:23:05 -06001703
David Pinedoeeca2a22015-06-18 17:03:14 -06001704 demo->curFrame++;
1705
1706 if (demo->frameCount != INT_MAX && demo->curFrame == demo->frameCount)
1707 {
1708 demo->quit=true;
1709 demo_cleanup(demo);
1710 ExitProcess(0);
1711 }
1712
1713}
Ian Elliotte14e9f92015-04-16 15:23:05 -06001714
1715// MS-Windows event handling function:
1716LRESULT CALLBACK WndProc(HWND hWnd,
1717 UINT uMsg,
1718 WPARAM wParam,
1719 LPARAM lParam)
1720{
Ian Elliotte14e9f92015-04-16 15:23:05 -06001721 switch(uMsg)
1722 {
Ian Elliotte14e9f92015-04-16 15:23:05 -06001723 case WM_CLOSE:
1724 PostQuitMessage(0);
Tony Barbour5685ad72015-04-29 16:19:20 -06001725 break;
Ian Elliotte14e9f92015-04-16 15:23:05 -06001726 case WM_PAINT:
1727 demo_run(&demo);
1728 return 0;
1729 default:
1730 break;
1731 }
1732 return (DefWindowProc(hWnd, uMsg, wParam, lParam));
1733}
1734
1735static void demo_create_window(struct demo *demo)
1736{
1737 WNDCLASSEX win_class;
1738
1739 // Initialize the window class structure:
1740 win_class.cbSize = sizeof(WNDCLASSEX);
1741 win_class.style = CS_HREDRAW | CS_VREDRAW;
1742 win_class.lpfnWndProc = WndProc;
1743 win_class.cbClsExtra = 0;
1744 win_class.cbWndExtra = 0;
1745 win_class.hInstance = demo->connection; // hInstance
1746 win_class.hIcon = LoadIcon(NULL, IDI_APPLICATION);
1747 win_class.hCursor = LoadCursor(NULL, IDC_ARROW);
1748 win_class.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
1749 win_class.lpszMenuName = NULL;
1750 win_class.lpszClassName = demo->name;
1751 win_class.hIconSm = LoadIcon(NULL, IDI_WINLOGO);
1752 // Register window class:
1753 if (!RegisterClassEx(&win_class)) {
1754 // It didn't work, so try to give a useful error:
1755 printf("Unexpected error trying to start the application!\n");
1756 fflush(stdout);
1757 exit(1);
1758 }
1759 // Create window with the registered class:
Mike Stroyanf5856292015-06-15 14:20:13 -06001760 RECT wr = { 0, 0, demo->width, demo->height };
1761 AdjustWindowRect(&wr, WS_OVERLAPPEDWINDOW, FALSE);
Ian Elliotte14e9f92015-04-16 15:23:05 -06001762 demo->window = CreateWindowEx(0,
1763 demo->name, // class name
1764 demo->name, // app name
1765 WS_OVERLAPPEDWINDOW | // window style
1766 WS_VISIBLE |
1767 WS_SYSMENU,
1768 100,100, // x/y coords
Mike Stroyanf5856292015-06-15 14:20:13 -06001769 wr.right-wr.left, // width
1770 wr.bottom-wr.top, // height
Ian Elliotte14e9f92015-04-16 15:23:05 -06001771 NULL, // handle to parent
1772 NULL, // handle to menu
1773 demo->connection, // hInstance
1774 NULL); // no extra parameters
1775 if (!demo->window) {
1776 // It didn't work, so try to give a useful error:
1777 printf("Cannot create a window in which to draw!\n");
1778 fflush(stdout);
1779 exit(1);
1780 }
1781}
1782#else // _WIN32
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001783static void demo_handle_event(struct demo *demo,
1784 const xcb_generic_event_t *event)
1785{
Piers Daniell886be472015-02-23 16:23:13 -07001786 uint8_t event_code = event->response_type & 0x7f;
Courtney Goeltzenleuchterca21a212014-11-06 14:27:52 -07001787 switch (event_code) {
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001788 case XCB_EXPOSE:
Courtney Goeltzenleuchter54611482014-11-18 11:28:09 -07001789 // TODO: Resize window
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001790 break;
Courtney Goeltzenleuchterca21a212014-11-06 14:27:52 -07001791 case XCB_CLIENT_MESSAGE:
1792 if((*(xcb_client_message_event_t*)event).data.data32[0] ==
1793 (*demo->atom_wm_delete_window).atom) {
1794 demo->quit = true;
1795 }
1796 break;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001797 case XCB_KEY_RELEASE:
1798 {
1799 const xcb_key_release_event_t *key =
1800 (const xcb_key_release_event_t *) event;
1801
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -06001802 switch (key->detail) {
1803 case 0x9: // Escape
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001804 demo->quit = true;
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -06001805 break;
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001806 case 0x71: // left arrow key
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -06001807 demo->spin_angle += demo->spin_increment;
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001808 break;
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -06001809 case 0x72: // right arrow key
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -06001810 demo->spin_angle -= demo->spin_increment;
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -06001811 break;
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -06001812 case 0x41:
1813 demo->pause = !demo->pause;
Piers Daniell886be472015-02-23 16:23:13 -07001814 break;
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -06001815 }
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001816 }
1817 break;
1818 default:
1819 break;
1820 }
1821}
1822
1823static void demo_run(struct demo *demo)
1824{
1825 xcb_flush(demo->connection);
1826
1827 while (!demo->quit) {
1828 xcb_generic_event_t *event;
1829
Courtney Goeltzenleuchter54611482014-11-18 11:28:09 -07001830 if (demo->pause) {
1831 event = xcb_wait_for_event(demo->connection);
1832 } else {
1833 event = xcb_poll_for_event(demo->connection);
1834 }
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001835 if (event) {
1836 demo_handle_event(demo, event);
1837 free(event);
Courtney Goeltzenleuchterbb3e1312014-11-10 11:13:13 -07001838 }
Courtney Goeltzenleuchter54611482014-11-18 11:28:09 -07001839
1840 // Wait for work to finish before updating MVP.
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001841 vkDeviceWaitIdle(demo->device);
Courtney Goeltzenleuchter54611482014-11-18 11:28:09 -07001842 demo_update_data_buffer(demo);
1843
Courtney Goeltzenleuchterbb3e1312014-11-10 11:13:13 -07001844 demo_draw(demo);
Courtney Goeltzenleuchter21f89972014-11-18 11:28:09 -07001845
Courtney Goeltzenleuchterbb3e1312014-11-10 11:13:13 -07001846 // Wait for work to finish before updating MVP.
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001847 vkDeviceWaitIdle(demo->device);
David Pinedoeeca2a22015-06-18 17:03:14 -06001848 demo->curFrame++;
1849 if (demo->frameCount != INT_MAX && demo->curFrame == demo->frameCount)
1850 demo->quit = true;
1851
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001852 }
1853}
1854
1855static void demo_create_window(struct demo *demo)
1856{
1857 uint32_t value_mask, value_list[32];
1858
1859 demo->window = xcb_generate_id(demo->connection);
1860
1861 value_mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK;
1862 value_list[0] = demo->screen->black_pixel;
1863 value_list[1] = XCB_EVENT_MASK_KEY_RELEASE |
1864 XCB_EVENT_MASK_EXPOSURE;
1865
1866 xcb_create_window(demo->connection,
1867 XCB_COPY_FROM_PARENT,
1868 demo->window, demo->screen->root,
1869 0, 0, demo->width, demo->height, 0,
1870 XCB_WINDOW_CLASS_INPUT_OUTPUT,
1871 demo->screen->root_visual,
1872 value_mask, value_list);
1873
Courtney Goeltzenleuchterca21a212014-11-06 14:27:52 -07001874 /* Magic code that will send notification when window is destroyed */
1875 xcb_intern_atom_cookie_t cookie = xcb_intern_atom(demo->connection, 1, 12,
1876 "WM_PROTOCOLS");
1877 xcb_intern_atom_reply_t* reply = xcb_intern_atom_reply(demo->connection, cookie, 0);
1878
1879 xcb_intern_atom_cookie_t cookie2 = xcb_intern_atom(demo->connection, 0, 16, "WM_DELETE_WINDOW");
1880 demo->atom_wm_delete_window = xcb_intern_atom_reply(demo->connection, cookie2, 0);
1881
1882 xcb_change_property(demo->connection, XCB_PROP_MODE_REPLACE,
1883 demo->window, (*reply).atom, 4, 32, 1,
1884 &(*demo->atom_wm_delete_window).atom);
1885 free(reply);
1886
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001887 xcb_map_window(demo->connection, demo->window);
David Pinedoeeca2a22015-06-18 17:03:14 -06001888
1889 // Force the x/y coordinates to 100,100 results are identical in consecutive runs
1890 const uint32_t coords[] = {100, 100};
1891 xcb_configure_window(demo->connection, demo->window,
1892 XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y, coords);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001893}
Ian Elliotte14e9f92015-04-16 15:23:05 -06001894#endif // _WIN32
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001895
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001896static void demo_init_vk(struct demo *demo)
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001897{
Tobin Ehlis3536b442015-04-16 18:04:57 -06001898 VkResult err;
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001899 char *extension_names[64];
1900 char *layer_names[64];
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -06001901 VkExtensionProperties *instance_extensions;
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001902 VkLayerProperties *instance_layers;
1903 VkLayerProperties *device_layers;
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -06001904 uint32_t instance_extension_count = 0;
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001905 uint32_t instance_layer_count = 0;
1906 uint32_t enabled_extension_count = 0;
1907 uint32_t enabled_layer_count = 0;
1908
1909 /* Look for validation layers */
1910 bool32_t validation_found = 0;
1911 err = vkGetGlobalLayerProperties(&instance_layer_count, NULL);
Tobin Ehlis3536b442015-04-16 18:04:57 -06001912 assert(!err);
1913
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001914 memset(layer_names, 0, sizeof(layer_names));
1915 instance_layers = malloc(sizeof(VkLayerProperties) * instance_layer_count);
1916 err = vkGetGlobalLayerProperties(&instance_layer_count, instance_layers);
1917 assert(!err);
1918 for (uint32_t i = 0; i < instance_layer_count; i++) {
1919 if (!validation_found && demo->validate && !strcmp("Validation", instance_layers[i].layerName)) {
1920 layer_names[enabled_layer_count++] = "Validation";
1921 validation_found = 1;
1922 }
1923 assert(enabled_layer_count < 64);
1924 }
1925 if (demo->validate && !validation_found) {
1926 ERR_EXIT("vkGetGlobalLayerProperties failed to find any "
1927 "\"Validation\" layers.\n\n"
1928 "Please look at the Getting Started guide for additional "
1929 "information.\n",
1930 "vkCreateInstance Failure");
1931 }
1932
1933 err = vkGetGlobalExtensionProperties(NULL, &instance_extension_count, NULL);
1934 assert(!err);
1935
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -06001936 bool32_t WSIextFound = 0;
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001937 memset(extension_names, 0, sizeof(extension_names));
1938 instance_extensions = malloc(sizeof(VkExtensionProperties) * instance_extension_count);
1939 err = vkGetGlobalExtensionProperties(NULL, &instance_extension_count, instance_extensions);
1940 assert(!err);
1941 for (uint32_t i = 0; i < instance_extension_count; i++) {
1942 if (!strcmp(VK_WSI_LUNARG_EXTENSION_NAME, instance_extensions[i].extName)) {
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -06001943 WSIextFound = 1;
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001944 extension_names[enabled_extension_count++] = VK_WSI_LUNARG_EXTENSION_NAME;
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -06001945 }
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001946 if (!strcmp(DEBUG_REPORT_EXTENSION_NAME, instance_extensions[i].extName)) {
1947 if (demo->validate) {
1948 extension_names[enabled_extension_count++] = DEBUG_REPORT_EXTENSION_NAME;
1949 }
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -06001950 }
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001951 assert(enabled_extension_count < 64);
Tobin Ehlis3536b442015-04-16 18:04:57 -06001952 }
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -06001953 if (!WSIextFound) {
Tony Barbour426b9052015-06-24 16:06:58 -06001954 ERR_EXIT("vkGetGlobalExtensionProperties failed to find the "
Ian Elliott3b375cf2015-04-28 13:22:33 -06001955 "\"VK_WSI_LunarG\" extension.\n\nDo you have a compatible "
1956 "Vulkan installable client driver (ICD) installed?\nPlease "
1957 "look at the Getting Started guide for additional "
1958 "information.\n",
1959 "vkCreateInstance Failure");
1960 }
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001961 const VkApplicationInfo app = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001962 .sType = VK_STRUCTURE_TYPE_APPLICATION_INFO,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001963 .pNext = NULL,
Ian Elliott4e19ed02015-04-28 10:52:52 -06001964 .pAppName = APP_SHORT_NAME,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001965 .appVersion = 0,
Ian Elliott4e19ed02015-04-28 10:52:52 -06001966 .pEngineName = APP_SHORT_NAME,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001967 .engineVersion = 0,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001968 .apiVersion = VK_API_VERSION,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001969 };
Tony Barbour5685ad72015-04-29 16:19:20 -06001970 VkInstanceCreateInfo inst_info = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001971 .sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
Jon Ashburn29669a42015-04-04 14:52:07 -06001972 .pNext = NULL,
1973 .pAppInfo = &app,
1974 .pAllocCb = NULL,
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06001975 .layerCount = enabled_layer_count,
1976 .ppEnabledLayerNames = (const char *const*) layer_names,
1977 .extensionCount = enabled_extension_count,
1978 .ppEnabledExtensionNames = (const char *const*) extension_names,
Jon Ashburn29669a42015-04-04 14:52:07 -06001979 };
Courtney Goeltzenleuchterddcb6192015-04-14 18:48:46 -06001980 const VkDeviceQueueCreateInfo queue = {
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001981 .queueNodeIndex = 0,
1982 .queueCount = 1,
1983 };
Ian Elliottff6dab52015-04-28 11:35:02 -06001984
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001985 uint32_t gpu_count;
1986 uint32_t i;
Courtney Goeltzenleuchterf3168062015-03-05 18:09:39 -07001987 uint32_t queue_count;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001988
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001989 err = vkCreateInstance(&inst_info, &demo->inst);
Ian Elliottcaa9f272015-04-28 11:35:02 -06001990 if (err == VK_ERROR_INCOMPATIBLE_DRIVER) {
1991 ERR_EXIT("Cannot find a compatible Vulkan installable client driver "
Ian Elliott3b375cf2015-04-28 13:22:33 -06001992 "(ICD).\n\nPlease look at the Getting Started guide for "
Ian Elliottcaa9f272015-04-28 11:35:02 -06001993 "additional information.\n",
1994 "vkCreateInstance Failure");
Tony Barbour5685ad72015-04-29 16:19:20 -06001995 } else if (err == VK_ERROR_INVALID_EXTENSION) {
1996 ERR_EXIT("Cannot find a specified extension library"
1997 ".\nMake sure your layers path is set appropriately\n",
1998 "vkCreateInstance Failure");
Ian Elliottcaa9f272015-04-28 11:35:02 -06001999 } else if (err) {
Ian Elliott3b375cf2015-04-28 13:22:33 -06002000 ERR_EXIT("vkCreateInstance failed.\n\nDo you have a compatible Vulkan "
2001 "installable client driver (ICD) installed?\nPlease look at "
Ian Elliottcaa9f272015-04-28 11:35:02 -06002002 "the Getting Started guide for additional information.\n",
2003 "vkCreateInstance Failure");
Ian Elliottdfe55f72015-04-03 15:24:55 -06002004 }
Jon Ashburn29669a42015-04-04 14:52:07 -06002005
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002006 free(instance_layers);
2007 free(instance_extensions);
Tony Barbour5685ad72015-04-29 16:19:20 -06002008
Jon Ashburn07b309a2015-04-15 11:31:12 -06002009 gpu_count = 1;
2010 err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, &demo->gpu);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002011 assert(!err && gpu_count == 1);
2012
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002013 /* Look for validation layers */
2014 validation_found = 0;
2015 enabled_layer_count = 0;
2016 uint32_t device_layer_count = 0;
2017 err = vkGetPhysicalDeviceLayerProperties(demo->gpu, &device_layer_count, NULL);
2018 assert(!err);
2019
2020 memset(layer_names, 0, sizeof(layer_names));
2021 device_layers = malloc(sizeof(VkLayerProperties) * device_layer_count);
2022 err = vkGetPhysicalDeviceLayerProperties(demo->gpu, &device_layer_count, device_layers);
2023 assert(!err);
2024 for (uint32_t i = 0; i < device_layer_count; i++) {
2025 if (!validation_found && demo->validate &&
2026 !strcmp("Validation", device_layers[i].layerName)) {
2027 layer_names[enabled_layer_count++] = "Validation";
2028 validation_found = 1;
2029 }
2030 assert(enabled_layer_count < 64);
2031 }
2032 if (demo->validate && !validation_found) {
2033 ERR_EXIT("vkGetGlobalLayerProperties failed to find any "
2034 "\"Validation\" layers.\n\n"
2035 "Please look at the Getting Started guide for additional "
2036 "information.\n",
2037 "vkCreateInstance Failure");
2038 }
2039
2040 uint32_t device_extension_count = 0;
2041 VkExtensionProperties *device_extensions = NULL;
2042 err = vkGetPhysicalDeviceExtensionProperties(
2043 demo->gpu, NULL, &device_extension_count, NULL);
2044 assert(!err);
2045
2046 WSIextFound = 0;
2047 enabled_extension_count = 0;
2048 memset(extension_names, 0, sizeof(extension_names));
2049 device_extensions = malloc(sizeof(VkExtensionProperties) * device_extension_count);
2050 err = vkGetPhysicalDeviceExtensionProperties(
2051 demo->gpu, NULL, &device_extension_count, device_extensions);
2052 assert(!err);
2053 for (uint32_t i = 0; i < device_extension_count; i++) {
2054 if (!strcmp(VK_WSI_LUNARG_EXTENSION_NAME, device_extensions[i].extName)) {
2055 WSIextFound = 1;
2056 extension_names[enabled_extension_count++] = VK_WSI_LUNARG_EXTENSION_NAME;
2057 }
2058 assert(enabled_extension_count < 64);
2059 }
2060 if (!WSIextFound) {
2061 ERR_EXIT("vkGetGlobalExtensionProperties failed to find the "
2062 "\"VK_WSI_LunarG\" extension.\n\nDo you have a compatible "
2063 "Vulkan installable client driver (ICD) installed?\nPlease "
2064 "look at the Getting Started guide for additional "
2065 "information.\n",
2066 "vkCreateInstance Failure");
2067 }
2068
2069 VkDeviceCreateInfo device = {
2070 .sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
2071 .pNext = NULL,
2072 .queueRecordCount = 1,
2073 .pRequestedQueues = &queue,
2074 .layerCount = enabled_layer_count,
2075 .ppEnabledLayerNames = (const char*const*) layer_names,
2076 .extensionCount = enabled_extension_count,
2077 .ppEnabledExtensionNames = (const char *const*) extension_names,
2078 .flags = 0,
2079 };
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -06002080
Tony Barbour5685ad72015-04-29 16:19:20 -06002081 if (demo->validate) {
Tony Barboura65ecc22015-06-30 14:14:19 -06002082 demo->dbgCreateMsgCallback = vkGetInstanceProcAddr(demo->inst, "vkDbgCreateMsgCallback");
2083 demo->dbgDestroyMsgCallback = vkGetInstanceProcAddr(demo->inst, "vkDbgDestroyMsgCallback");
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -06002084 if (!demo->dbgCreateMsgCallback) {
2085 ERR_EXIT("GetProcAddr: Unable to find vkDbgCreateMsgCallback\n",
2086 "vkGetProcAddr Failure");
2087 }
Tony Barboura65ecc22015-06-30 14:14:19 -06002088 if (!demo->dbgDestroyMsgCallback) {
2089 ERR_EXIT("GetProcAddr: Unable to find vkDbgDestroyMsgCallback\n",
2090 "vkGetProcAddr Failure");
2091 }
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -06002092 err = demo->dbgCreateMsgCallback(
2093 demo->inst,
2094 VK_DBG_REPORT_ERROR_BIT | VK_DBG_REPORT_WARN_BIT,
2095 dbgFunc, NULL,
2096 &demo->msg_callback);
2097 switch (err) {
2098 case VK_SUCCESS:
2099 break;
2100 case VK_ERROR_INVALID_POINTER:
2101 ERR_EXIT("dbgCreateMsgCallback: Invalid pointer\n",
2102 "dbgCreateMsgCallback Failure");
2103 break;
2104 case VK_ERROR_OUT_OF_HOST_MEMORY:
2105 ERR_EXIT("dbgCreateMsgCallback: out of host memory\n",
2106 "dbgCreateMsgCallback Failure");
2107 break;
2108 default:
2109 ERR_EXIT("dbgCreateMsgCallback: unknown failure\n",
2110 "dbgCreateMsgCallback Failure");
2111 break;
2112 }
Tony Barbour5685ad72015-04-29 16:19:20 -06002113 }
2114
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06002115 err = vkCreateDevice(demo->gpu, &device, &demo->device);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002116 assert(!err);
2117
Courtney Goeltzenleuchter18061cd2015-06-29 15:39:26 -06002118 free(device_layers);
2119
Ian Elliott1b6de092015-06-22 15:07:49 -06002120 GET_DEVICE_PROC_ADDR(demo->device, CreateSwapChainWSI);
2121 GET_DEVICE_PROC_ADDR(demo->device, CreateSwapChainWSI);
2122 GET_DEVICE_PROC_ADDR(demo->device, DestroySwapChainWSI);
2123 GET_DEVICE_PROC_ADDR(demo->device, GetSwapChainInfoWSI);
2124 GET_DEVICE_PROC_ADDR(demo->device, QueuePresentWSI);
Jon Ashburncedc15f2015-05-21 18:13:33 -06002125
Tony Barbour426b9052015-06-24 16:06:58 -06002126 err = vkGetPhysicalDeviceProperties(demo->gpu, &demo->gpu_props);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -06002127 assert(!err);
2128
Tony Barbour426b9052015-06-24 16:06:58 -06002129 err = vkGetPhysicalDeviceQueueCount(demo->gpu, &queue_count);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -06002130 assert(!err);
2131
Tony Barbour426b9052015-06-24 16:06:58 -06002132 demo->queue_props = (VkPhysicalDeviceQueueProperties *) malloc(queue_count * sizeof(VkPhysicalDeviceQueueProperties));
2133 err = vkGetPhysicalDeviceQueueProperties(demo->gpu, queue_count, demo->queue_props);
Courtney Goeltzenleuchterf3168062015-03-05 18:09:39 -07002134 assert(!err);
Courtney Goeltzenleuchterf3168062015-03-05 18:09:39 -07002135 assert(queue_count >= 1);
2136
Mark Lobodzinskicf26e072015-04-16 11:44:05 -05002137 // Graphics queue and MemMgr queue can be separate.
2138 // TODO: Add support for separate queues, including synchronization,
Mark Lobodzinskifb9f5642015-05-11 17:21:15 -05002139 // and appropriate tracking for QueueSubmit
Courtney Goeltzenleuchterf3168062015-03-05 18:09:39 -07002140 for (i = 0; i < queue_count; i++) {
Mark Lobodzinskifb9f5642015-05-11 17:21:15 -05002141 if (demo->queue_props[i].queueFlags & VK_QUEUE_GRAPHICS_BIT)
Courtney Goeltzenleuchterf3168062015-03-05 18:09:39 -07002142 break;
2143 }
2144 assert(i < queue_count);
2145 demo->graphics_queue_node_index = i;
2146
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06002147 err = vkGetDeviceQueue(demo->device, demo->graphics_queue_node_index,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002148 0, &demo->queue);
2149 assert(!err);
Tony Barbour5685ad72015-04-29 16:19:20 -06002150
Jon Ashburnba4a1952015-06-16 12:44:51 -06002151 // for now hardcode format till get WSI support
2152 demo->format = VK_FORMAT_B8G8R8A8_UNORM;
Ian Elliott32536f92015-04-21 16:41:02 -06002153
David Pinedoeeca2a22015-06-18 17:03:14 -06002154 demo->quit = false;
2155 demo->curFrame = 0;
Mark Lobodzinski72346292015-07-02 16:49:40 -06002156
2157 // Get Memory information and properties
2158 err = vkGetPhysicalDeviceMemoryProperties(demo->gpu, &demo->memory_properties);
2159 assert(!err);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002160}
2161
2162static void demo_init_connection(struct demo *demo)
2163{
Ian Elliotte14e9f92015-04-16 15:23:05 -06002164#ifndef _WIN32
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002165 const xcb_setup_t *setup;
2166 xcb_screen_iterator_t iter;
2167 int scr;
2168
2169 demo->connection = xcb_connect(NULL, &scr);
Ian Elliottdfe55f72015-04-03 15:24:55 -06002170 if (demo->connection == NULL) {
2171 printf("Cannot find a compatible Vulkan installable client driver "
2172 "(ICD).\nExiting ...\n");
2173 fflush(stdout);
2174 exit(1);
2175 }
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002176
2177 setup = xcb_get_setup(demo->connection);
2178 iter = xcb_setup_roots_iterator(setup);
2179 while (scr-- > 0)
2180 xcb_screen_next(&iter);
2181
2182 demo->screen = iter.data;
Ian Elliotte14e9f92015-04-16 15:23:05 -06002183#endif // _WIN32
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002184}
2185
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07002186static void demo_init(struct demo *demo, int argc, char **argv)
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002187{
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -06002188 vec3 eye = {0.0f, 3.0f, 5.0f};
2189 vec3 origin = {0, 0, 0};
Chia-I Wuc3487c22015-04-22 14:56:17 +08002190 vec3 up = {0.0f, 1.0f, 0.0};
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -06002191
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002192 memset(demo, 0, sizeof(*demo));
David Pinedoeeca2a22015-06-18 17:03:14 -06002193 demo->frameCount = INT_MAX;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002194
Piers Daniell886be472015-02-23 16:23:13 -07002195 for (int i = 1; i < argc; i++) {
Tony Barbour5685ad72015-04-29 16:19:20 -06002196 if (strcmp(argv[i], "--use_staging") == 0) {
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07002197 demo->use_staging_buffer = true;
Tony Barbour5685ad72015-04-29 16:19:20 -06002198 continue;
Ian Elliotte14e9f92015-04-16 15:23:05 -06002199 }
Cody Northrop75db0322015-05-28 11:27:16 -06002200 if (strcmp(argv[i], "--use_glsl") == 0) {
2201 demo->use_glsl = true;
2202 continue;
2203 }
Tony Barbour5685ad72015-04-29 16:19:20 -06002204 if (strcmp(argv[i], "--validate") == 0) {
2205 demo->validate = true;
2206 continue;
2207 }
David Pinedoeeca2a22015-06-18 17:03:14 -06002208 if (strcmp(argv[i], "--c") == 0 &&
2209 demo->frameCount == INT_MAX &&
2210 i < argc-1 &&
2211 sscanf(argv[i+1],"%d", &demo->frameCount) == 1 &&
2212 demo->frameCount >= 0)
2213 {
2214 i++;
2215 continue;
2216 }
Tony Barbour5685ad72015-04-29 16:19:20 -06002217
David Pinedoeeca2a22015-06-18 17:03:14 -06002218 fprintf(stderr, "Usage:\n %s [--use_staging] [--validate] [--c <framecount>]\n", APP_SHORT_NAME);
Ian Elliotte14e9f92015-04-16 15:23:05 -06002219 fflush(stderr);
2220 exit(1);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07002221 }
2222
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002223 demo_init_connection(demo);
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06002224 demo_init_vk(demo);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002225
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -06002226 demo->width = 500;
2227 demo->height = 500;
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -06002228
2229 demo->spin_angle = 0.01f;
2230 demo->spin_increment = 0.01f;
2231 demo->pause = false;
2232
Piers Daniell886be472015-02-23 16:23:13 -07002233 mat4x4_perspective(demo->projection_matrix, (float)degreesToRadians(45.0f), 1.0f, 0.1f, 100.0f);
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -06002234 mat4x4_look_at(demo->view_matrix, eye, origin, up);
2235 mat4x4_identity(demo->model_matrix);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002236}
2237
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002238
Ian Elliotte14e9f92015-04-16 15:23:05 -06002239#ifdef _WIN32
Tony Barbour5685ad72015-04-29 16:19:20 -06002240extern int __getmainargs(
2241 int * _Argc,
2242 char *** _Argv,
2243 char *** _Env,
2244 int _DoWildCard,
2245 int * new_mode);
Ian Elliott7595eee2015-04-28 10:33:11 -06002246
Ian Elliott421107f2015-04-28 15:50:36 -06002247int WINAPI WinMain(HINSTANCE hInstance,
2248 HINSTANCE hPrevInstance,
2249 LPSTR pCmdLine,
2250 int nCmdShow)
Ian Elliotte14e9f92015-04-16 15:23:05 -06002251{
2252 MSG msg; // message
2253 bool done; // flag saying when app is complete
Tony Barbour5685ad72015-04-29 16:19:20 -06002254 int argc;
2255 char** argv;
2256 char** env;
2257 int new_mode = 0;
Ian Elliotte14e9f92015-04-16 15:23:05 -06002258
Tony Barbour5685ad72015-04-29 16:19:20 -06002259 __getmainargs(&argc,&argv,&env,0,&new_mode);
2260
2261 demo_init(&demo, argc, argv);
2262 demo.connection = hInstance;
2263 strncpy(demo.name, "cube", APP_NAME_STR_LEN);
Ian Elliotte14e9f92015-04-16 15:23:05 -06002264 demo_create_window(&demo);
2265
2266 demo_prepare(&demo);
2267
2268 done = false; //initialize loop condition variable
2269 /* main message loop*/
2270 while(!done)
2271 {
Ian Elliott421107f2015-04-28 15:50:36 -06002272 PeekMessage(&msg, NULL, 0, 0, PM_REMOVE);
Ian Elliotte14e9f92015-04-16 15:23:05 -06002273 if (msg.message == WM_QUIT) //check for a quit message
2274 {
2275 done = true; //if found, quit app
2276 }
2277 else
2278 {
2279 /* Translate and dispatch to event queue*/
2280 TranslateMessage(&msg);
2281 DispatchMessage(&msg);
2282 }
2283 }
2284
2285 demo_cleanup(&demo);
2286
Tony Barboura938abb2015-04-22 11:36:22 -06002287 return (int) msg.wParam;
Ian Elliotte14e9f92015-04-16 15:23:05 -06002288}
2289#else // _WIN32
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07002290int main(int argc, char **argv)
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002291{
2292 struct demo demo;
2293
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07002294 demo_init(&demo, argc, argv);
Chia-I Wu5b66aa52015-04-16 22:02:10 +08002295 demo_create_window(&demo);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002296
2297 demo_prepare(&demo);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002298 demo_run(&demo);
2299
2300 demo_cleanup(&demo);
2301
2302 return 0;
2303}
Ian Elliotte14e9f92015-04-16 15:23:05 -06002304#endif // _WIN32