blob: 5a303e3c5cb708ff721b28acb02723048dccbe93 [file] [log] [blame]
Ian Elliotta748eaf2015-04-28 15:50:36 -06001/*
2 * Vulkan
3 *
4 * Copyright (C) 2014-2015 LunarG, Inc.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 */
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -060024#define _GNU_SOURCE
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060025#include <stdio.h>
26#include <stdlib.h>
27#include <string.h>
28#include <stdbool.h>
29#include <assert.h>
30
Ian Elliott639ca472015-04-16 15:23:05 -060031#ifdef _WIN32
32#pragma comment(linker, "/subsystem:windows")
33#include <windows.h>
34#define APP_NAME_STR_LEN 80
35#else // _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060036#include <xcb/xcb.h>
Ian Elliott639ca472015-04-16 15:23:05 -060037#endif // _WIN32
38
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -060039#include <vulkan.h>
Chia-I Wucbb564e2015-04-16 22:02:10 +080040#include <vk_wsi_lunarg.h>
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -060041#include "vk_debug_report_lunarg.h"
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060042
Cody Northropfe3d8bc2015-03-17 14:54:35 -060043#include "icd-spv.h"
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060044
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -060045#include "linmath.h"
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -060046#include <png.h>
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -060047
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060048#define DEMO_BUFFER_COUNT 2
49#define DEMO_TEXTURE_COUNT 1
Ian Elliott44e33f72015-04-28 10:52:52 -060050#define APP_SHORT_NAME "cube"
51#define APP_LONG_NAME "The Vulkan Cube Demo Program"
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060052
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -060053#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
54
Tony Barbourfdc2d352015-04-22 09:02:32 -060055#if defined(NDEBUG) && defined(__GNUC__)
56#define U_ASSERT_ONLY __attribute__((unused))
57#else
58#define U_ASSERT_ONLY
59#endif
60
Ian Elliott07264132015-04-28 11:35:02 -060061#ifdef _WIN32
62#define ERR_EXIT(err_msg, err_class) \
63 do { \
64 MessageBox(NULL, err_msg, err_class, MB_OK); \
65 exit(1); \
66 } while (0)
67
68// NOTE: If the following values (copied from "loader_platform.h") change, they
69// need to change here as well:
70#define LAYER_NAMES_ENV "VK_LAYER_NAMES"
71#define LAYER_NAMES_REGISTRY_VALUE "VK_LAYER_NAMES"
72
73#else // _WIN32
74
75#define ERR_EXIT(err_msg, err_class) \
76 do { \
77 printf(err_msg); \
78 fflush(stdout); \
79 exit(1); \
80 } while (0)
81#endif // _WIN32
82
Ian Elliott673898b2015-06-22 15:07:49 -060083#define GET_DEVICE_PROC_ADDR(dev, entrypoint) \
84{ \
85 demo->fp##entrypoint = vkGetDeviceProcAddr(dev, "vk"#entrypoint); \
86 if (demo->fp##entrypoint == NULL) { \
87 ERR_EXIT("vkGetDeviceProcAddr failed to find vk"#entrypoint, \
88 "vkGetDeviceProcAddr Failure"); \
89 } \
90}
91
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -060092/*
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -070093 * structure to track all objects related to a texture.
94 */
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -060095struct texture_object {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -060096 VkSampler sampler;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -070097
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -060098 VkImage image;
99 VkImageLayout imageLayout;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600100
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500101 VkDeviceMemory mem;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600102 VkImageView view;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700103 int32_t tex_width, tex_height;
104};
105
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600106static char *tex_files[] = {
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -0600107 "lunarg-logo-256x256-solid.png"
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600108};
109
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600110struct vkcube_vs_uniform {
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600111 // Must start with MVP
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600112 float mvp[4][4];
113 float position[12*3][4];
114 float color[12*3][4];
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600115};
116
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600117struct vktexcube_vs_uniform {
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600118 // Must start with MVP
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600119 float mvp[4][4];
120 float position[12*3][4];
121 float attr[12*3][4];
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600122};
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600123
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600124//--------------------------------------------------------------------------------------
125// Mesh and VertexFormat Data
126//--------------------------------------------------------------------------------------
127struct Vertex
128{
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600129 float posX, posY, posZ, posW; // Position data
130 float r, g, b, a; // Color
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600131};
132
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600133struct VertexPosTex
134{
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600135 float posX, posY, posZ, posW; // Position data
136 float u, v, s, t; // Texcoord
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600137};
138
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600139#define XYZ1(_x_, _y_, _z_) (_x_), (_y_), (_z_), 1.f
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600140#define UV(_u_, _v_) (_u_), (_v_), 0.f, 1.f
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600141
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600142static const float g_vertex_buffer_data[] = {
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800143 -1.0f,-1.0f,-1.0f, // -X side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600144 -1.0f,-1.0f, 1.0f,
145 -1.0f, 1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800146 -1.0f, 1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600147 -1.0f, 1.0f,-1.0f,
148 -1.0f,-1.0f,-1.0f,
149
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800150 -1.0f,-1.0f,-1.0f, // -Z side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600151 1.0f, 1.0f,-1.0f,
152 1.0f,-1.0f,-1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800153 -1.0f,-1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600154 -1.0f, 1.0f,-1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600155 1.0f, 1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600156
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800157 -1.0f,-1.0f,-1.0f, // -Y side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600158 1.0f,-1.0f,-1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600159 1.0f,-1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800160 -1.0f,-1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600161 1.0f,-1.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600162 -1.0f,-1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600163
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800164 -1.0f, 1.0f,-1.0f, // +Y side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600165 -1.0f, 1.0f, 1.0f,
166 1.0f, 1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800167 -1.0f, 1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600168 1.0f, 1.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600169 1.0f, 1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600170
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800171 1.0f, 1.0f,-1.0f, // +X side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600172 1.0f, 1.0f, 1.0f,
173 1.0f,-1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800174 1.0f,-1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600175 1.0f,-1.0f,-1.0f,
176 1.0f, 1.0f,-1.0f,
177
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800178 -1.0f, 1.0f, 1.0f, // +Z side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600179 -1.0f,-1.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600180 1.0f, 1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800181 -1.0f,-1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600182 1.0f,-1.0f, 1.0f,
183 1.0f, 1.0f, 1.0f,
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600184};
185
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600186static const float g_uv_buffer_data[] = {
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800187 0.0f, 0.0f, // -X side
188 1.0f, 0.0f,
189 1.0f, 1.0f,
190 1.0f, 1.0f,
191 0.0f, 1.0f,
192 0.0f, 0.0f,
193
194 1.0f, 0.0f, // -Z side
195 0.0f, 1.0f,
196 0.0f, 0.0f,
197 1.0f, 0.0f,
198 1.0f, 1.0f,
199 0.0f, 1.0f,
200
201 1.0f, 1.0f, // -Y side
202 1.0f, 0.0f,
203 0.0f, 0.0f,
204 1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600205 0.0f, 0.0f,
206 0.0f, 1.0f,
207
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800208 1.0f, 1.0f, // +Y side
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600209 0.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800210 0.0f, 0.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600211 1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600212 0.0f, 0.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600213 1.0f, 0.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600214
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800215 1.0f, 1.0f, // +X side
216 0.0f, 1.0f,
217 0.0f, 0.0f,
218 0.0f, 0.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600219 1.0f, 0.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600220 1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600221
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800222 0.0f, 1.0f, // +Z side
223 0.0f, 0.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600224 1.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600225 0.0f, 0.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800226 1.0f, 0.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600227 1.0f, 1.0f,
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600228};
229
230void dumpMatrix(const char *note, mat4x4 MVP)
231{
232 int i;
233
234 printf("%s: \n", note);
235 for (i=0; i<4; i++) {
236 printf("%f, %f, %f, %f\n", MVP[i][0], MVP[i][1], MVP[i][2], MVP[i][3]);
237 }
238 printf("\n");
239 fflush(stdout);
240}
241
242void dumpVec4(const char *note, vec4 vector)
243{
244 printf("%s: \n", note);
245 printf("%f, %f, %f, %f\n", vector[0], vector[1], vector[2], vector[3]);
246 printf("\n");
247 fflush(stdout);
248}
249
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -0600250void dbgFunc(
251 VkFlags msgFlags,
252 VkObjectType objType,
253 VkObject srcObject,
254 size_t location,
255 int32_t msgCode,
256 const char* pLayerPrefix,
257 const char* pMsg,
258 void* pUserData)
Tony Barbour3dddd5d2015-04-29 16:19:20 -0600259{
260 char *message = (char *) malloc(strlen(pMsg)+100);
261
262 assert (message);
263
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -0600264 if (msgFlags & VK_DBG_REPORT_ERROR_BIT) {
265 sprintf(message,"ERROR: [%s] Code %d : %s", pLayerPrefix, msgCode, pMsg);
266 } else if (msgFlags & VK_DBG_REPORT_WARN_BIT) {
Tony Barbourd70b42c2015-06-30 14:14:19 -0600267 // We know that we're submitting queues without fences, ignore this warning
268 if (strstr(pMsg, "vkQueueSubmit parameter, VkFence fence, is null pointer")){
269 return;
270 }
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -0600271 sprintf(message,"WARNING: [%s] Code %d : %s", pLayerPrefix, msgCode, pMsg);
Tony Barbour3dddd5d2015-04-29 16:19:20 -0600272 } else {
273 return;
274 }
275
276#ifdef _WIN32
277 MessageBox(NULL, message, "Alert", MB_OK);
278#else
279 printf("%s\n",message);
280 fflush(stdout);
281#endif
282 free(message);
283}
284
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600285struct demo {
Ian Elliott639ca472015-04-16 15:23:05 -0600286#ifdef _WIN32
287#define APP_NAME_STR_LEN 80
288 HINSTANCE connection; // hInstance - Windows Instance
289 char name[APP_NAME_STR_LEN]; // Name to put on the window/icon
290 HWND window; // hWnd - window handle
291#else // _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600292 xcb_connection_t *connection;
293 xcb_screen_t *screen;
Chia-I Wucbb564e2015-04-16 22:02:10 +0800294 xcb_window_t window;
295 xcb_intern_atom_reply_t *atom_wm_delete_window;
Ian Elliott639ca472015-04-16 15:23:05 -0600296#endif // _WIN32
Cody Northrop1fedb212015-05-28 11:27:16 -0600297 bool prepared;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700298 bool use_staging_buffer;
Cody Northrop1fedb212015-05-28 11:27:16 -0600299 bool use_glsl;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600300
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600301 VkInstance inst;
Tony Barbour72304ef2015-04-16 15:59:00 -0600302 VkPhysicalDevice gpu;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600303 VkDevice device;
304 VkQueue queue;
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -0700305 uint32_t graphics_queue_node_index;
Tony Barbourecf1b2b2015-06-24 16:06:58 -0600306 VkPhysicalDeviceProperties gpu_props;
Tony Barbour72304ef2015-04-16 15:59:00 -0600307 VkPhysicalDeviceQueueProperties *queue_props;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600308 VkPhysicalDeviceMemoryProperties memory_properties;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600309
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600310 VkFramebuffer framebuffer;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600311 int width, height;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600312 VkFormat format;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600313
Jon Ashburn0b85d052015-05-21 18:13:33 -0600314 PFN_vkCreateSwapChainWSI fpCreateSwapChainWSI;
315 PFN_vkDestroySwapChainWSI fpDestroySwapChainWSI;
316 PFN_vkGetSwapChainInfoWSI fpGetSwapChainInfoWSI;
317 PFN_vkQueuePresentWSI fpQueuePresentWSI;
Chia-I Wucbb564e2015-04-16 22:02:10 +0800318 VkSwapChainWSI swap_chain;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600319 struct {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600320 VkImage image;
Tony Barbour72304ef2015-04-16 15:59:00 -0600321 VkDeviceMemory mem;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600322 VkCmdBuffer cmd;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600323
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600324 VkColorAttachmentView view;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600325 } buffers[DEMO_BUFFER_COUNT];
326
327 struct {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600328 VkFormat format;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600329
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600330 VkImage image;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500331 VkDeviceMemory mem;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600332 VkDepthStencilView view;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600333 } depth;
334
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600335 struct texture_object textures[DEMO_TEXTURE_COUNT];
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600336
337 struct {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600338 VkBuffer buf;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500339 VkDeviceMemory mem;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600340 VkBufferView view;
Chia-I Wuae721ba2015-05-25 16:27:55 +0800341 VkDescriptorInfo desc;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600342 } uniform_data;
343
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600344 VkCmdBuffer cmd; // Buffer for initialization commands
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -0500345 VkPipelineLayout pipeline_layout;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600346 VkDescriptorSetLayout desc_layout;
Jon Ashburnc4ab7af2015-07-09 15:02:25 -0600347 VkPipelineCache pipelineCache;
Chia-I Wuf973e322015-07-08 13:34:24 +0800348 VkRenderPass render_pass;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600349 VkPipeline pipeline;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600350
Courtney Goeltzenleuchter5e6d1e92015-04-10 16:24:50 -0600351 VkDynamicVpState viewport;
352 VkDynamicRsState raster;
353 VkDynamicCbState color_blend;
354 VkDynamicDsState depth_stencil;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600355
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600356 mat4x4 projection_matrix;
357 mat4x4 view_matrix;
358 mat4x4 model_matrix;
359
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600360 float spin_angle;
361 float spin_increment;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600362 bool pause;
363
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600364 VkDescriptorPool desc_pool;
365 VkDescriptorSet desc_set;
Chia-I Wu6a3c8972015-01-04 16:27:24 +0800366
Chia-I Wuf973e322015-07-08 13:34:24 +0800367 VkFramebuffer framebuffers[DEMO_BUFFER_COUNT];
368
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600369 bool quit;
David Pinedo2bb7c932015-06-18 17:03:14 -0600370 int32_t curFrame;
371 int32_t frameCount;
Tony Barbour3dddd5d2015-04-29 16:19:20 -0600372 bool validate;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -0600373 PFN_vkDbgCreateMsgCallback dbgCreateMsgCallback;
Tony Barbourd70b42c2015-06-30 14:14:19 -0600374 PFN_vkDbgDestroyMsgCallback dbgDestroyMsgCallback;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -0600375 VkDbgMsgCallback msg_callback;
376
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600377 uint32_t current_buffer;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600378};
379
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600380static VkResult memory_type_from_properties(struct demo *demo, uint32_t typeBits, VkFlags properties, uint32_t *typeIndex)
381{
382 // Search memtypes to find first index with those properties
383 for (uint32_t i = 0; i < 32; i++) {
384 if ((typeBits & 1) == 1) {
385 // Type is available, does it match user properties?
386 if ((demo->memory_properties.memoryTypes[i].propertyFlags & properties) == properties) {
387 *typeIndex = i;
388 return VK_SUCCESS;
389 }
390 }
391 typeBits >>= 1;
392 }
393 // No memory types matched, return failure
394 return VK_UNSUPPORTED;
395}
396
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600397static void demo_flush_init_cmd(struct demo *demo)
398{
Tony Barbourfdc2d352015-04-22 09:02:32 -0600399 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600400
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600401 if (demo->cmd == VK_NULL_HANDLE)
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600402 return;
403
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600404 err = vkEndCommandBuffer(demo->cmd);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600405 assert(!err);
406
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600407 const VkCmdBuffer cmd_bufs[] = { demo->cmd };
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600408
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600409 err = vkQueueSubmit(demo->queue, 1, cmd_bufs, VK_NULL_HANDLE);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600410 assert(!err);
411
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600412 err = vkQueueWaitIdle(demo->queue);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600413 assert(!err);
414
Mike Stroyanebae8322015-04-17 12:36:38 -0600415 vkDestroyObject(demo->device, VK_OBJECT_TYPE_COMMAND_BUFFER, demo->cmd);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600416 demo->cmd = VK_NULL_HANDLE;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600417}
418
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600419static void demo_set_image_layout(
420 struct demo *demo,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600421 VkImage image,
malnasseeb25d3f2015-06-03 17:28:38 -0400422 VkImageAspect aspect,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600423 VkImageLayout old_image_layout,
424 VkImageLayout new_image_layout)
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600425{
Tony Barbourfdc2d352015-04-22 09:02:32 -0600426 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600427
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600428 if (demo->cmd == VK_NULL_HANDLE) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600429 const VkCmdBufferCreateInfo cmd = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600430 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600431 .pNext = NULL,
432 .queueNodeIndex = demo->graphics_queue_node_index,
Chia-I Wu57b23b42015-06-26 15:34:39 +0800433 .level = VK_CMD_BUFFER_LEVEL_PRIMARY,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600434 .flags = 0,
435 };
436
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600437 err = vkCreateCommandBuffer(demo->device, &cmd, &demo->cmd);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600438 assert(!err);
439
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600440 VkCmdBufferBeginInfo cmd_buf_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600441 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600442 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -0600443 .flags = VK_CMD_BUFFER_OPTIMIZE_SMALL_BATCH_BIT |
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600444 VK_CMD_BUFFER_OPTIMIZE_ONE_TIME_SUBMIT_BIT,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600445 };
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600446 err = vkBeginCommandBuffer(demo->cmd, &cmd_buf_info);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600447 }
448
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600449 VkImageMemoryBarrier image_memory_barrier = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600450 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600451 .pNext = NULL,
452 .outputMask = 0,
453 .inputMask = 0,
454 .oldLayout = old_image_layout,
455 .newLayout = new_image_layout,
456 .image = image,
malnasseeb25d3f2015-06-03 17:28:38 -0400457 .subresourceRange = { aspect, 0, 1, 0, 0 }
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600458 };
459
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600460 if (new_image_layout == VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL) {
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600461 /* Make sure anything that was copying from this image has completed */
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -0600462 image_memory_barrier.inputMask = VK_MEMORY_INPUT_TRANSFER_BIT;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600463 }
464
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600465 if (new_image_layout == VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL) {
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600466 /* Make sure any Copy or CPU writes to image are flushed */
Courtney Goeltzenleuchter2bda8332015-04-29 17:16:21 -0600467 image_memory_barrier.outputMask = VK_MEMORY_OUTPUT_HOST_WRITE_BIT | VK_MEMORY_OUTPUT_TRANSFER_BIT;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600468 }
469
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600470 VkImageMemoryBarrier *pmemory_barrier = &image_memory_barrier;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600471
Tony Barbour50bbca42015-06-29 16:20:35 -0600472 VkPipelineStageFlags src_stages = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
473 VkPipelineStageFlags dest_stages = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600474
Tony Barbour50bbca42015-06-29 16:20:35 -0600475 vkCmdPipelineBarrier(demo->cmd, src_stages, dest_stages, false, 1, (const void **)&pmemory_barrier);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600476}
477
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600478static void demo_draw_build_cmd(struct demo *demo, VkCmdBuffer cmd_buf)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600479{
Chia-I Wuf973e322015-07-08 13:34:24 +0800480 const VkCmdBufferBeginInfo cmd_buf_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600481 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO,
Courtney Goeltzenleuchter53968d82015-04-03 15:25:24 -0600482 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -0600483 .flags = VK_CMD_BUFFER_OPTIMIZE_SMALL_BATCH_BIT |
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600484 VK_CMD_BUFFER_OPTIMIZE_ONE_TIME_SUBMIT_BIT,
Jon Ashburn60ccfbe2014-12-31 17:08:35 -0700485 };
Chia-I Wuf973e322015-07-08 13:34:24 +0800486 const VkRenderPassBegin rp_begin = {
487 .renderPass = demo->render_pass,
488 .framebuffer = demo->framebuffers[demo->current_buffer],
489 .contents = VK_RENDER_PASS_CONTENTS_INLINE,
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700490 };
Chia-I Wuf973e322015-07-08 13:34:24 +0800491 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600492
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600493 err = vkBeginCommandBuffer(cmd_buf, &cmd_buf_info);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600494 assert(!err);
495
Tobin Ehlis080219d2015-06-24 15:53:07 -0600496 vkCmdBeginRenderPass(cmd_buf, &rp_begin);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600497 vkCmdBindPipeline(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600498 demo->pipeline);
Mark Lobodzinskic6e8b3d2015-06-15 13:21:21 -0600499 vkCmdBindDescriptorSets(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS, demo->pipeline_layout,
Cody Northropfb5185a2015-04-16 13:41:56 -0600500 0, 1, &demo->desc_set, 0, NULL);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600501
Tony Barbour72304ef2015-04-16 15:59:00 -0600502 vkCmdBindDynamicStateObject(cmd_buf, VK_STATE_BIND_POINT_VIEWPORT, demo->viewport);
503 vkCmdBindDynamicStateObject(cmd_buf, VK_STATE_BIND_POINT_RASTER, demo->raster);
504 vkCmdBindDynamicStateObject(cmd_buf, VK_STATE_BIND_POINT_COLOR_BLEND,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600505 demo->color_blend);
Tony Barbour72304ef2015-04-16 15:59:00 -0600506 vkCmdBindDynamicStateObject(cmd_buf, VK_STATE_BIND_POINT_DEPTH_STENCIL,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600507 demo->depth_stencil);
508
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600509 vkCmdDraw(cmd_buf, 0, 12 * 3, 0, 1);
Chia-I Wu57b23b42015-06-26 15:34:39 +0800510 vkCmdEndRenderPass(cmd_buf);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600511
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600512 err = vkEndCommandBuffer(cmd_buf);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600513 assert(!err);
514}
515
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600516
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600517void demo_update_data_buffer(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600518{
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600519 mat4x4 MVP, Model, VP;
520 int matrixSize = sizeof(MVP);
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600521 uint8_t *pData;
Tony Barbourfdc2d352015-04-22 09:02:32 -0600522 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600523
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600524 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600525
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600526 // Rotate 22.5 degrees around the Y axis
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600527 mat4x4_dup(Model, demo->model_matrix);
Piers Daniell735ee532015-02-23 16:23:13 -0700528 mat4x4_rotate(demo->model_matrix, Model, 0.0f, 1.0f, 0.0f, (float)degreesToRadians(demo->spin_angle));
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600529 mat4x4_mul(MVP, VP, demo->model_matrix);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600530
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500531 err = vkMapMemory(demo->device, demo->uniform_data.mem, 0, 0, 0, (void **) &pData);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600532 assert(!err);
533
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600534 memcpy(pData, (const void*) &MVP[0][0], matrixSize);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600535
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500536 err = vkUnmapMemory(demo->device, demo->uniform_data.mem);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600537 assert(!err);
538}
539
540static void demo_draw(struct demo *demo)
541{
Chia-I Wucbb564e2015-04-16 22:02:10 +0800542 const VkPresentInfoWSI present = {
543 .sType = VK_STRUCTURE_TYPE_PRESENT_INFO_WSI,
544 .pNext = NULL,
545 .image = demo->buffers[demo->current_buffer].image,
546 .flipInterval = 0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600547 };
Tony Barbourfdc2d352015-04-22 09:02:32 -0600548 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600549
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600550 err = vkQueueSubmit(demo->queue, 1, &demo->buffers[demo->current_buffer].cmd,
551 VK_NULL_HANDLE);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600552 assert(!err);
553
Jon Ashburn0b85d052015-05-21 18:13:33 -0600554 err = demo->fpQueuePresentWSI(demo->queue, &present);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600555 assert(!err);
556
557 demo->current_buffer = (demo->current_buffer + 1) % DEMO_BUFFER_COUNT;
Chia-I Wucbb564e2015-04-16 22:02:10 +0800558
559 err = vkQueueWaitIdle(demo->queue);
560 assert(err == VK_SUCCESS);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600561}
562
563static void demo_prepare_buffers(struct demo *demo)
564{
Chia-I Wucbb564e2015-04-16 22:02:10 +0800565 const VkSwapChainCreateInfoWSI swap_chain = {
566 .sType = VK_STRUCTURE_TYPE_SWAP_CHAIN_CREATE_INFO_WSI,
567 .pNext = NULL,
568 .pNativeWindowSystemHandle = demo->connection,
569 .pNativeWindowHandle = (void *) (intptr_t) demo->window,
Ian Elliotte4602cd2015-04-21 16:41:02 -0600570 .displayCount = 1,
Chia-I Wucbb564e2015-04-16 22:02:10 +0800571 .imageCount = DEMO_BUFFER_COUNT,
572 .imageFormat = demo->format,
573 .imageExtent = {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600574 .width = demo->width,
575 .height = demo->height,
576 },
Chia-I Wucbb564e2015-04-16 22:02:10 +0800577 .imageArraySize = 1,
578 .imageUsageFlags = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600579 };
Chia-I Wucbb564e2015-04-16 22:02:10 +0800580 VkSwapChainImageInfoWSI images[DEMO_BUFFER_COUNT];
581 size_t images_size = sizeof(images);
Tony Barbourfdc2d352015-04-22 09:02:32 -0600582 VkResult U_ASSERT_ONLY err;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600583 uint32_t i;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600584
Jon Ashburn0b85d052015-05-21 18:13:33 -0600585 err = demo->fpCreateSwapChainWSI(demo->device, &swap_chain, &demo->swap_chain);
Chia-I Wucbb564e2015-04-16 22:02:10 +0800586 assert(!err);
587
Jon Ashburn0b85d052015-05-21 18:13:33 -0600588 err = demo->fpGetSwapChainInfoWSI(demo->swap_chain,
Chia-I Wucbb564e2015-04-16 22:02:10 +0800589 VK_SWAP_CHAIN_INFO_TYPE_PERSISTENT_IMAGES_WSI,
590 &images_size, images);
591 assert(!err && images_size == sizeof(images));
592
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600593 for (i = 0; i < DEMO_BUFFER_COUNT; i++) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600594 VkColorAttachmentViewCreateInfo color_attachment_view = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600595 .sType = VK_STRUCTURE_TYPE_COLOR_ATTACHMENT_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600596 .pNext = NULL,
597 .format = demo->format,
598 .mipLevel = 0,
599 .baseArraySlice = 0,
600 .arraySize = 1,
601 };
602
Chia-I Wucbb564e2015-04-16 22:02:10 +0800603 demo->buffers[i].image = images[i].image;
604 demo->buffers[i].mem = images[i].memory;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600605
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600606 demo_set_image_layout(demo, demo->buffers[i].image,
malnasseeb25d3f2015-06-03 17:28:38 -0400607 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600608 VK_IMAGE_LAYOUT_UNDEFINED,
609 VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600610
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600611 color_attachment_view.image = demo->buffers[i].image;
612
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600613 err = vkCreateColorAttachmentView(demo->device,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600614 &color_attachment_view, &demo->buffers[i].view);
615 assert(!err);
616 }
617}
618
619static void demo_prepare_depth(struct demo *demo)
620{
Tony Barbour72304ef2015-04-16 15:59:00 -0600621 const VkFormat depth_format = VK_FORMAT_D16_UNORM;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600622 const VkImageCreateInfo image = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600623 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600624 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -0600625 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600626 .format = depth_format,
627 .extent = { demo->width, demo->height, 1 },
628 .mipLevels = 1,
629 .arraySize = 1,
630 .samples = 1,
Tony Barbour72304ef2015-04-16 15:59:00 -0600631 .tiling = VK_IMAGE_TILING_OPTIMAL,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600632 .usage = VK_IMAGE_USAGE_DEPTH_STENCIL_BIT,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600633 .flags = 0,
634 };
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -0600635 VkMemoryAllocInfo mem_alloc = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600636 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
Mark Lobodzinskia6907f72015-04-16 08:52:00 -0500637 .pNext = NULL,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600638 .allocationSize = 0,
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600639 .memoryTypeIndex = 0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600640 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600641 VkDepthStencilViewCreateInfo view = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600642 .sType = VK_STRUCTURE_TYPE_DEPTH_STENCIL_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600643 .pNext = NULL,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600644 .image = VK_NULL_HANDLE,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600645 .mipLevel = 0,
646 .baseArraySlice = 0,
647 .arraySize = 1,
648 .flags = 0,
649 };
Mike Stroyanebae8322015-04-17 12:36:38 -0600650
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500651 VkMemoryRequirements mem_reqs;
Tony Barbourfdc2d352015-04-22 09:02:32 -0600652 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600653
654 demo->depth.format = depth_format;
655
656 /* create image */
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600657 err = vkCreateImage(demo->device, &image,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600658 &demo->depth.image);
659 assert(!err);
660
Tony Barbourecf1b2b2015-06-24 16:06:58 -0600661 err = vkGetObjectMemoryRequirements(demo->device,
662 VK_OBJECT_TYPE_IMAGE, demo->depth.image, &mem_reqs);
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600663
664 mem_alloc.allocationSize = mem_reqs.size;
665 err = memory_type_from_properties(demo,
666 mem_reqs.memoryTypeBits,
667 VK_MEMORY_PROPERTY_DEVICE_ONLY,
668 &mem_alloc.memoryTypeIndex);
669 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600670
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500671 /* allocate memory */
672 err = vkAllocMemory(demo->device, &mem_alloc, &demo->depth.mem);
673 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600674
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500675 /* bind memory */
676 err = vkBindObjectMemory(demo->device,
677 VK_OBJECT_TYPE_IMAGE, demo->depth.image,
678 demo->depth.mem, 0);
679 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600680
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600681 demo_set_image_layout(demo, demo->depth.image,
malnasseeb25d3f2015-06-03 17:28:38 -0400682 VK_IMAGE_ASPECT_DEPTH,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600683 VK_IMAGE_LAYOUT_UNDEFINED,
684 VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600685
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600686 /* create image view */
687 view.image = demo->depth.image;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600688 err = vkCreateDepthStencilView(demo->device, &view,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600689 &demo->depth.view);
690 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600691}
692
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600693/** loadTexture
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -0600694 * loads a png file into an memory object, using cstdio , libpng.
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600695 *
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -0600696 * \param demo : Needed to access VK calls
697 * \param filename : the png file to be loaded
698 * \param width : width of png, to be updated as a side effect of this function
699 * \param height : height of png, to be updated as a side effect of this function
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600700 *
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -0600701 * \return bool : an opengl texture id. true if successful?,
702 * should be validated by the client of this function.
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600703 *
704 * Source: http://en.wikibooks.org/wiki/OpenGL_Programming/Intermediate/Textures
705 * Modified to copy image to memory
706 *
707 */
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700708bool loadTexture(const char *filename, uint8_t *rgba_data,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600709 VkSubresourceLayout *layout,
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600710 int32_t *width, int32_t *height)
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600711{
712 //header for testing if it is a png
713 png_byte header[8];
Tony Barbourf9921732015-04-22 11:36:22 -0600714 int is_png, bit_depth, color_type, rowbytes;
715 size_t retval;
Ian Elliott1e42dff2015-02-13 14:29:21 -0700716 png_uint_32 i, twidth, theight;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600717 png_structp png_ptr;
718 png_infop info_ptr, end_info;
719 png_byte *image_data;
720 png_bytep *row_pointers;
721
722 //open file as binary
723 FILE *fp = fopen(filename, "rb");
724 if (!fp) {
725 return false;
726 }
727
728 //read the header
Tony Barbourfdc2d352015-04-22 09:02:32 -0600729 retval = fread(header, 1, 8, fp);
730 if (retval != 8) {
731 fclose(fp);
732 return false;
733 }
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600734
735 //test if png
736 is_png = !png_sig_cmp(header, 0, 8);
737 if (!is_png) {
738 fclose(fp);
739 return false;
740 }
741
742 //create png struct
743 png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL,
744 NULL, NULL);
745 if (!png_ptr) {
746 fclose(fp);
747 return (false);
748 }
749
750 //create png info struct
751 info_ptr = png_create_info_struct(png_ptr);
752 if (!info_ptr) {
753 png_destroy_read_struct(&png_ptr, (png_infopp) NULL, (png_infopp) NULL);
754 fclose(fp);
755 return (false);
756 }
757
758 //create png info struct
759 end_info = png_create_info_struct(png_ptr);
760 if (!end_info) {
761 png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp) NULL);
762 fclose(fp);
763 return (false);
764 }
765
766 //png error stuff, not sure libpng man suggests this.
767 if (setjmp(png_jmpbuf(png_ptr))) {
768 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
769 fclose(fp);
770 return (false);
771 }
772
773 //init png reading
774 png_init_io(png_ptr, fp);
775
776 //let libpng know you already read the first 8 bytes
777 png_set_sig_bytes(png_ptr, 8);
778
779 // read all the info up to the image data
780 png_read_info(png_ptr, info_ptr);
781
782 // get info about png
783 png_get_IHDR(png_ptr, info_ptr, &twidth, &theight, &bit_depth, &color_type,
784 NULL, NULL, NULL);
785
786 //update width and height based on png info
787 *width = twidth;
788 *height = theight;
789
790 // Require that incoming texture be 8bits per color component
791 // and 4 components (RGBA).
792 if (png_get_bit_depth(png_ptr, info_ptr) != 8 ||
793 png_get_channels(png_ptr, info_ptr) != 4) {
794 return false;
795 }
796
797 if (rgba_data == NULL) {
798 // If data pointer is null, we just want the width & height
799 // clean up memory and close stuff
800 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
801 fclose(fp);
802
803 return true;
804 }
805
806 // Update the png info struct.
807 png_read_update_info(png_ptr, info_ptr);
808
809 // Row size in bytes.
810 rowbytes = png_get_rowbytes(png_ptr, info_ptr);
811
812 // Allocate the image_data as a big block, to be given to opengl
813 image_data = (png_byte *)malloc(rowbytes * theight * sizeof(png_byte));
814 if (!image_data) {
815 //clean up memory and close stuff
816 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
817 fclose(fp);
818 return false;
819 }
820
821 // row_pointers is for pointing to image_data for reading the png with libpng
822 row_pointers = (png_bytep *)malloc(theight * sizeof(png_bytep));
823 if (!row_pointers) {
824 //clean up memory and close stuff
825 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
826 // delete[] image_data;
827 fclose(fp);
828 return false;
829 }
830 // set the individual row_pointers to point at the correct offsets of image_data
831 for (i = 0; i < theight; ++i)
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700832 row_pointers[theight - 1 - i] = rgba_data + i * layout->rowPitch;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600833
834 // read the png into image_data through row_pointers
835 png_read_image(png_ptr, row_pointers);
836
837 // clean up memory and close stuff
838 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
839 free(row_pointers);
840 free(image_data);
841 fclose(fp);
842
843 return true;
844}
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600845
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700846static void demo_prepare_texture_image(struct demo *demo,
847 const char *filename,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600848 struct texture_object *tex_obj,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600849 VkImageTiling tiling,
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -0600850 VkImageUsageFlags usage,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600851 VkFlags mem_props)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600852{
Mike Stroyan8b89e072015-06-15 14:21:03 -0600853 const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600854 int32_t tex_width;
855 int32_t tex_height;
Tony Barbourfdc2d352015-04-22 09:02:32 -0600856 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700857
David Pinedo30bd71d2015-04-23 08:16:57 -0600858 if (!loadTexture(filename, NULL, NULL, &tex_width, &tex_height))
859 {
860 printf("Failed to load textures\n");
861 fflush(stdout);
862 exit(1);
863 }
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700864
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600865 tex_obj->tex_width = tex_width;
866 tex_obj->tex_height = tex_height;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700867
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600868 const VkImageCreateInfo image_create_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600869 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700870 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -0600871 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700872 .format = tex_format,
873 .extent = { tex_width, tex_height, 1 },
874 .mipLevels = 1,
875 .arraySize = 1,
876 .samples = 1,
877 .tiling = tiling,
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -0600878 .usage = usage,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700879 .flags = 0,
880 };
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -0600881 VkMemoryAllocInfo mem_alloc = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600882 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
Mark Lobodzinskia6907f72015-04-16 08:52:00 -0500883 .pNext = NULL,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700884 .allocationSize = 0,
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600885 .memoryTypeIndex = 0,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700886 };
887
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500888 VkMemoryRequirements mem_reqs;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700889
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600890 err = vkCreateImage(demo->device, &image_create_info,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600891 &tex_obj->image);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700892 assert(!err);
893
Tony Barbourecf1b2b2015-06-24 16:06:58 -0600894 err = vkGetObjectMemoryRequirements(demo->device,
895 VK_OBJECT_TYPE_IMAGE, tex_obj->image, &mem_reqs);
896 assert(!err);
Piers Daniell735ee532015-02-23 16:23:13 -0700897
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500898 mem_alloc.allocationSize = mem_reqs.size;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700899
Mark Lobodzinskieadf9982015-07-02 16:49:40 -0600900 err = memory_type_from_properties(demo, mem_reqs.memoryTypeBits, mem_props, &mem_alloc.memoryTypeIndex);
901 assert(!err);
902
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500903 /* allocate memory */
904 err = vkAllocMemory(demo->device, &mem_alloc,
905 &(tex_obj->mem));
906 assert(!err);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700907
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500908 /* bind memory */
909 err = vkBindObjectMemory(demo->device,
910 VK_OBJECT_TYPE_IMAGE, tex_obj->image,
911 tex_obj->mem, 0);
912 assert(!err);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700913
Tony Barbour72304ef2015-04-16 15:59:00 -0600914 if (mem_props & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600915 const VkImageSubresource subres = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600916 .aspect = VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700917 .mipLevel = 0,
918 .arraySlice = 0,
919 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600920 VkSubresourceLayout layout;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700921 void *data;
922
Tony Barbourecf1b2b2015-06-24 16:06:58 -0600923 err = vkGetImageSubresourceLayout(demo->device, tex_obj->image, &subres, &layout);
924 assert(!err);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700925
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500926 err = vkMapMemory(demo->device, tex_obj->mem, 0, 0, 0, &data);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700927 assert(!err);
928
929 if (!loadTexture(filename, data, &layout, &tex_width, &tex_height)) {
930 fprintf(stderr, "Error loading texture: %s\n", filename);
931 }
932
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500933 err = vkUnmapMemory(demo->device, tex_obj->mem);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700934 assert(!err);
935 }
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600936
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600937 tex_obj->imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600938 demo_set_image_layout(demo, tex_obj->image,
malnasseeb25d3f2015-06-03 17:28:38 -0400939 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600940 VK_IMAGE_LAYOUT_UNDEFINED,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600941 tex_obj->imageLayout);
942 /* setting the image layout does not reference the actual memory so no need to add a mem ref */
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700943}
944
Mark Lobodzinskida9b1092015-04-16 11:44:05 -0500945static void demo_destroy_texture_image(struct demo *demo, struct texture_object *tex_objs)
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700946{
947 /* clean up staging resources */
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500948 vkFreeMemory(demo->device, tex_objs->mem);
Mike Stroyanebae8322015-04-17 12:36:38 -0600949 vkDestroyObject(demo->device, VK_OBJECT_TYPE_IMAGE, tex_objs->image);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700950}
951
952static void demo_prepare_textures(struct demo *demo)
953{
Tony Barbour72304ef2015-04-16 15:59:00 -0600954 const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600955 VkFormatProperties props;
Tony Barbourfdc2d352015-04-22 09:02:32 -0600956 VkResult U_ASSERT_ONLY err;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600957 uint32_t i;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600958
Chris Forbesf576a3e2015-06-21 22:55:02 +1200959 err = vkGetPhysicalDeviceFormatInfo(demo->gpu, tex_format, &props);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700960 assert(!err);
961
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600962 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700963
FslNopper6a7e50e2015-05-06 21:42:01 +0200964 if ((props.linearTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) && !demo->use_staging_buffer) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700965 /* Device can texture using linear textures */
966 demo_prepare_texture_image(demo, tex_files[i], &demo->textures[i],
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -0600967 VK_IMAGE_TILING_LINEAR, VK_IMAGE_USAGE_SAMPLED_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
Tony Barbour72304ef2015-04-16 15:59:00 -0600968 } else if (props.optimalTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700969 /* Must use staging buffer to copy linear texture to optimized */
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600970 struct texture_object staging_texture;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700971
972 memset(&staging_texture, 0, sizeof(staging_texture));
973 demo_prepare_texture_image(demo, tex_files[i], &staging_texture,
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -0600974 VK_IMAGE_TILING_LINEAR, VK_IMAGE_USAGE_TRANSFER_SOURCE_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700975
976 demo_prepare_texture_image(demo, tex_files[i], &demo->textures[i],
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -0600977 VK_IMAGE_TILING_OPTIMAL,
978 (VK_IMAGE_USAGE_TRANSFER_DESTINATION_BIT | VK_IMAGE_USAGE_SAMPLED_BIT),
979 VK_MEMORY_PROPERTY_DEVICE_ONLY);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700980
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600981 demo_set_image_layout(demo, staging_texture.image,
malnasseeb25d3f2015-06-03 17:28:38 -0400982 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600983 staging_texture.imageLayout,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600984 VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700985
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600986 demo_set_image_layout(demo, demo->textures[i].image,
malnasseeb25d3f2015-06-03 17:28:38 -0400987 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600988 demo->textures[i].imageLayout,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600989 VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700990
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600991 VkImageCopy copy_region = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600992 .srcSubresource = { VK_IMAGE_ASPECT_COLOR, 0, 0 },
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700993 .srcOffset = { 0, 0, 0 },
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600994 .destSubresource = { VK_IMAGE_ASPECT_COLOR, 0, 0 },
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700995 .destOffset = { 0, 0, 0 },
996 .extent = { staging_texture.tex_width, staging_texture.tex_height, 1 },
997 };
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600998 vkCmdCopyImage(demo->cmd,
999 staging_texture.image, VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL,
1000 demo->textures[i].image, VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL,
Courtney Goeltzenleuchter8e89a312015-03-25 11:25:10 -06001001 1, &copy_region);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001002
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001003 demo_set_image_layout(demo, demo->textures[i].image,
malnasseeb25d3f2015-06-03 17:28:38 -04001004 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001005 VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001006 demo->textures[i].imageLayout);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001007
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001008 demo_flush_init_cmd(demo);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001009
Courtney Goeltzenleuchterf3aeb2b2015-04-21 09:30:03 -06001010 demo_destroy_texture_image(demo, &staging_texture);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001011 } else {
Mike Stroyan8b89e072015-06-15 14:21:03 -06001012 /* Can't support VK_FORMAT_R8G8B8A8_UNORM !? */
1013 assert(!"No support for R8G8B8A8_UNORM as texture image format");
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001014 }
1015
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001016 const VkSamplerCreateInfo sampler = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001017 .sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001018 .pNext = NULL,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001019 .magFilter = VK_TEX_FILTER_NEAREST,
1020 .minFilter = VK_TEX_FILTER_NEAREST,
Tony Barbour72304ef2015-04-16 15:59:00 -06001021 .mipMode = VK_TEX_MIPMAP_MODE_BASE,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001022 .addressU = VK_TEX_ADDRESS_CLAMP,
1023 .addressV = VK_TEX_ADDRESS_CLAMP,
1024 .addressW = VK_TEX_ADDRESS_CLAMP,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001025 .mipLodBias = 0.0f,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001026 .maxAnisotropy = 1,
Tony Barbour72304ef2015-04-16 15:59:00 -06001027 .compareOp = VK_COMPARE_OP_NEVER,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001028 .minLod = 0.0f,
1029 .maxLod = 0.0f,
Tony Barbourcb530c72015-06-25 16:56:44 -06001030 .borderColor = VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001031 };
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001032
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001033 VkImageViewCreateInfo view = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001034 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001035 .pNext = NULL,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001036 .image = VK_NULL_HANDLE,
Tony Barbour72304ef2015-04-16 15:59:00 -06001037 .viewType = VK_IMAGE_VIEW_TYPE_2D,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001038 .format = tex_format,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001039 .channels = { VK_CHANNEL_SWIZZLE_R,
1040 VK_CHANNEL_SWIZZLE_G,
1041 VK_CHANNEL_SWIZZLE_B,
1042 VK_CHANNEL_SWIZZLE_A, },
1043 .subresourceRange = { VK_IMAGE_ASPECT_COLOR, 0, 1, 0, 1 },
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001044 };
Jon Ashburnb2a66652015-01-16 09:37:43 -07001045
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001046 /* create sampler */
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001047 err = vkCreateSampler(demo->device, &sampler,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001048 &demo->textures[i].sampler);
1049 assert(!err);
1050
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001051 /* create image view */
1052 view.image = demo->textures[i].image;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001053 err = vkCreateImageView(demo->device, &view,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001054 &demo->textures[i].view);
1055 assert(!err);
1056 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001057}
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001058
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001059void demo_prepare_cube_data_buffer(struct demo *demo)
1060{
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -06001061 VkBufferCreateInfo buf_info;
1062 VkBufferViewCreateInfo view_info;
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -06001063 VkMemoryAllocInfo alloc_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001064 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
Mark Lobodzinskia6907f72015-04-16 08:52:00 -05001065 .pNext = NULL,
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001066 .allocationSize = 0,
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06001067 .memoryTypeIndex = 0,
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001068 };
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001069 VkMemoryRequirements mem_reqs;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001070 size_t mem_reqs_size = sizeof(VkMemoryRequirements);
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001071 uint8_t *pData;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001072 int i;
1073 mat4x4 MVP, VP;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001074 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001075 struct vktexcube_vs_uniform data;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001076
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001077 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001078 mat4x4_mul(MVP, VP, demo->model_matrix);
1079 memcpy(data.mvp, MVP, sizeof(MVP));
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001080// dumpMatrix("MVP", MVP);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001081
1082 for (i=0; i<12*3; i++) {
1083 data.position[i][0] = g_vertex_buffer_data[i*3];
1084 data.position[i][1] = g_vertex_buffer_data[i*3+1];
1085 data.position[i][2] = g_vertex_buffer_data[i*3+2];
1086 data.position[i][3] = 1.0f;
1087 data.attr[i][0] = g_uv_buffer_data[2*i];
1088 data.attr[i][1] = g_uv_buffer_data[2*i + 1];
1089 data.attr[i][2] = 0;
1090 data.attr[i][3] = 0;
1091 }
1092
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001093 memset(&buf_info, 0, sizeof(buf_info));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001094 buf_info.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -06001095 buf_info.usage = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001096 err = vkCreateBuffer(demo->device, &buf_info, &demo->uniform_data.buf);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001097 assert(!err);
1098
Tony Barbourecf1b2b2015-06-24 16:06:58 -06001099 err = vkGetObjectMemoryRequirements(demo->device,
1100 VK_OBJECT_TYPE_BUFFER, demo->uniform_data.buf, &mem_reqs);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001101 assert(!err && mem_reqs_size == sizeof(mem_reqs));
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001102
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001103 alloc_info.allocationSize = mem_reqs.size;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06001104 err = memory_type_from_properties(demo,
1105 mem_reqs.memoryTypeBits,
1106 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT,
1107 &alloc_info.memoryTypeIndex);
1108 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001109
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001110 err = vkAllocMemory(demo->device, &alloc_info, &(demo->uniform_data.mem));
1111 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001112
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001113 err = vkMapMemory(demo->device, demo->uniform_data.mem, 0, 0, 0, (void **) &pData);
1114 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001115
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001116 memcpy(pData, &data, sizeof data);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001117
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001118 err = vkUnmapMemory(demo->device, demo->uniform_data.mem);
1119 assert(!err);
1120
1121 err = vkBindObjectMemory(demo->device,
1122 VK_OBJECT_TYPE_BUFFER, demo->uniform_data.buf,
1123 demo->uniform_data.mem, 0);
1124 assert(!err);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001125
1126 memset(&view_info, 0, sizeof(view_info));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001127 view_info.sType = VK_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO;
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001128 view_info.buffer = demo->uniform_data.buf;
Tony Barbour72304ef2015-04-16 15:59:00 -06001129 view_info.viewType = VK_BUFFER_VIEW_TYPE_RAW;
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001130 view_info.offset = 0;
1131 view_info.range = sizeof(data);
1132
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001133 err = vkCreateBufferView(demo->device, &view_info, &demo->uniform_data.view);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001134 assert(!err);
1135
Chia-I Wuae721ba2015-05-25 16:27:55 +08001136 demo->uniform_data.desc.bufferView = demo->uniform_data.view;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001137}
1138
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001139static void demo_prepare_descriptor_layout(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001140{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001141 const VkDescriptorSetLayoutBinding layout_bindings[2] = {
Chia-I Wua2aa8632015-03-26 15:04:41 +08001142 [0] = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001143 .descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
Chia-I Wu6d29a412015-05-25 16:22:52 +08001144 .arraySize = 1,
Tony Barbour72304ef2015-04-16 15:59:00 -06001145 .stageFlags = VK_SHADER_STAGE_VERTEX_BIT,
Chia-I Wu91e8e212015-03-27 12:56:09 +08001146 .pImmutableSamplers = NULL,
Chia-I Wua2aa8632015-03-26 15:04:41 +08001147 },
1148 [1] = {
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -06001149 .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
Chia-I Wu6d29a412015-05-25 16:22:52 +08001150 .arraySize = DEMO_TEXTURE_COUNT,
Tony Barbour72304ef2015-04-16 15:59:00 -06001151 .stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT,
Chia-I Wu91e8e212015-03-27 12:56:09 +08001152 .pImmutableSamplers = NULL,
Chia-I Wua2aa8632015-03-26 15:04:41 +08001153 },
1154 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001155 const VkDescriptorSetLayoutCreateInfo descriptor_layout = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001156 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001157 .pNext = NULL,
Chia-I Wua2aa8632015-03-26 15:04:41 +08001158 .count = 2,
1159 .pBinding = layout_bindings,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001160 };
Tony Barbourfdc2d352015-04-22 09:02:32 -06001161 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001162
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001163 err = vkCreateDescriptorSetLayout(demo->device,
Chia-I Wub58c24a2015-03-26 15:27:55 +08001164 &descriptor_layout, &demo->desc_layout);
1165 assert(!err);
1166
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001167 const VkPipelineLayoutCreateInfo pPipelineLayoutCreateInfo = {
1168 .sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
1169 .pNext = NULL,
1170 .descriptorSetCount = 1,
1171 .pSetLayouts = &demo->desc_layout,
1172 };
1173
1174 err = vkCreatePipelineLayout(demo->device,
1175 &pPipelineLayoutCreateInfo,
1176 &demo->pipeline_layout);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001177 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001178}
1179
Chia-I Wuf973e322015-07-08 13:34:24 +08001180static void demo_prepare_render_pass(struct demo *demo)
1181{
1182 const VkClearColorValue clear_color = {
1183 .f32 = { 0.2f, 0.2f, 0.2f, 0.2f },
1184 };
1185 const float clear_depth = 1.0f;
1186 VkResult U_ASSERT_ONLY err;
1187 const VkImageLayout color_layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
1188 const VkAttachmentLoadOp color_load_op = VK_ATTACHMENT_LOAD_OP_CLEAR;
1189 const VkAttachmentStoreOp color_store_op = VK_ATTACHMENT_STORE_OP_DONT_CARE;
1190 const VkRenderPassCreateInfo rp_info = {
1191 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO,
1192 .pNext = NULL,
1193 .renderArea.offset.x = 0,
1194 .renderArea.offset.y = 0,
1195 .renderArea.extent.width = demo->width,
1196 .renderArea.extent.height = demo->height,
1197 .colorAttachmentCount = 1,
1198 .extent.width = demo->width,
1199 .extent.height = demo->height,
1200 .sampleCount = 1,
1201 .layers = 1,
1202 .pColorFormats = &demo->format,
1203 .pColorLayouts = &color_layout,
1204 .pColorLoadOps = &color_load_op,
1205 .pColorStoreOps = &color_store_op,
1206 .pColorLoadClearValues = &clear_color,
1207 .depthStencilFormat = demo->depth.format,
1208 .depthStencilLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1209 .depthLoadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
1210 .depthLoadClearValue = clear_depth,
1211 .depthStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1212 .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
1213 .stencilLoadClearValue = 0,
1214 .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
1215 };
1216
1217 err = vkCreateRenderPass(demo->device, &rp_info, &demo->render_pass);
1218 assert(!err);
1219}
1220
Tobin Ehlisd1f17ac2015-07-02 11:02:49 -06001221static VkShader demo_prepare_shader(struct demo* demo,
Tony Barbour72304ef2015-04-16 15:59:00 -06001222 VkShaderStage stage,
Tobin Ehlisd1f17ac2015-07-02 11:02:49 -06001223 const void* code,
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001224 size_t size)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001225{
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001226 VkShaderModuleCreateInfo moduleCreateInfo;
1227 VkShaderCreateInfo shaderCreateInfo;
1228 VkShaderModule shaderModule;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001229 VkShader shader;
1230 VkResult err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001231
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001232
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001233 moduleCreateInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
1234 moduleCreateInfo.pNext = NULL;
1235
1236 shaderCreateInfo.sType = VK_STRUCTURE_TYPE_SHADER_CREATE_INFO;
1237 shaderCreateInfo.pNext = NULL;
Tobin Ehlisd1f17ac2015-07-02 11:02:49 -06001238 shaderCreateInfo.pName = "main";
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001239
Cody Northrop1fedb212015-05-28 11:27:16 -06001240 if (!demo->use_glsl) {
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001241 moduleCreateInfo.codeSize = size;
1242 moduleCreateInfo.pCode = code;
1243 moduleCreateInfo.flags = 0;
1244 err = vkCreateShaderModule(demo->device, &moduleCreateInfo, &shaderModule);
Cody Northrop1fedb212015-05-28 11:27:16 -06001245 if (err) {
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001246 free((void *) moduleCreateInfo.pCode);
Cody Northrop1fedb212015-05-28 11:27:16 -06001247 }
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001248
1249 shaderCreateInfo.flags = 0;
1250 shaderCreateInfo.module = shaderModule;
1251 err = vkCreateShader(demo->device, &shaderCreateInfo, &shader);
Cody Northrop1fedb212015-05-28 11:27:16 -06001252 } else {
1253 // Create fake SPV structure to feed GLSL
1254 // to the driver "under the covers"
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001255 moduleCreateInfo.codeSize = 3 * sizeof(uint32_t) + size + 1;
1256 moduleCreateInfo.pCode = malloc(moduleCreateInfo.codeSize);
1257 moduleCreateInfo.flags = 0;
Cody Northrop1fedb212015-05-28 11:27:16 -06001258
1259 /* try version 0 first: VkShaderStage followed by GLSL */
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001260 ((uint32_t *) moduleCreateInfo.pCode)[0] = ICD_SPV_MAGIC;
1261 ((uint32_t *) moduleCreateInfo.pCode)[1] = 0;
1262 ((uint32_t *) moduleCreateInfo.pCode)[2] = stage;
1263 memcpy(((uint32_t *) moduleCreateInfo.pCode + 3), code, size + 1);
Cody Northrop1fedb212015-05-28 11:27:16 -06001264
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001265 err = vkCreateShaderModule(demo->device, &moduleCreateInfo, &shaderModule);
Cody Northrop1fedb212015-05-28 11:27:16 -06001266 if (err) {
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001267 free((void *) moduleCreateInfo.pCode);
Cody Northrop1fedb212015-05-28 11:27:16 -06001268 }
Courtney Goeltzenleuchtera80a1a92015-06-24 18:24:19 -06001269
1270 shaderCreateInfo.flags = 0;
1271 shaderCreateInfo.module = shaderModule;
1272 err = vkCreateShader(demo->device, &shaderCreateInfo, &shader);
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001273 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001274 return shader;
1275}
1276
Cody Northrop8b12a1f2015-03-17 15:55:58 -06001277char *demo_read_spv(const char *filename, size_t *psize)
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001278{
1279 long int size;
Tony Barbourf9921732015-04-22 11:36:22 -06001280 size_t U_ASSERT_ONLY retval;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001281 void *shader_code;
1282
1283 FILE *fp = fopen(filename, "rb");
1284 if (!fp) return NULL;
1285
1286 fseek(fp, 0L, SEEK_END);
1287 size = ftell(fp);
1288
1289 fseek(fp, 0L, SEEK_SET);
1290
1291 shader_code = malloc(size);
Tony Barbourfdc2d352015-04-22 09:02:32 -06001292 retval = fread(shader_code, size, 1, fp);
1293 assert(retval == 1);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001294
1295 *psize = size;
1296
1297 return shader_code;
1298}
1299
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001300static VkShader demo_prepare_vs(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001301{
Cody Northrop1fedb212015-05-28 11:27:16 -06001302 if (!demo->use_glsl) {
1303 void *vertShaderCode;
1304 size_t size;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001305
Cody Northrop1fedb212015-05-28 11:27:16 -06001306 vertShaderCode = demo_read_spv("cube-vert.spv", &size);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001307
Cody Northrop1fedb212015-05-28 11:27:16 -06001308 return demo_prepare_shader(demo, VK_SHADER_STAGE_VERTEX,
1309 vertShaderCode, size);
1310 } else {
1311 static const char *vertShaderText =
1312 "#version 140\n"
1313 "#extension GL_ARB_separate_shader_objects : enable\n"
1314 "#extension GL_ARB_shading_language_420pack : enable\n"
1315 "\n"
1316 "layout(binding = 0) uniform buf {\n"
1317 " mat4 MVP;\n"
1318 " vec4 position[12*3];\n"
1319 " vec4 attr[12*3];\n"
1320 "} ubuf;\n"
1321 "\n"
1322 "layout (location = 0) out vec4 texcoord;\n"
1323 "\n"
1324 "void main() \n"
1325 "{\n"
1326 " texcoord = ubuf.attr[gl_VertexID];\n"
1327 " gl_Position = ubuf.MVP * ubuf.position[gl_VertexID];\n"
1328 "\n"
1329 " // GL->VK conventions\n"
1330 " gl_Position.y = -gl_Position.y;\n"
1331 " gl_Position.z = (gl_Position.z + gl_Position.w) / 2.0;\n"
1332 "}\n";
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001333
Cody Northrop1fedb212015-05-28 11:27:16 -06001334 return demo_prepare_shader(demo, VK_SHADER_STAGE_VERTEX,
1335 (const void *) vertShaderText,
1336 strlen(vertShaderText));
1337 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001338}
1339
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001340static VkShader demo_prepare_fs(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001341{
Cody Northrop1fedb212015-05-28 11:27:16 -06001342 if (!demo->use_glsl) {
1343 void *fragShaderCode;
1344 size_t size;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001345
Cody Northrop1fedb212015-05-28 11:27:16 -06001346 fragShaderCode = demo_read_spv("cube-frag.spv", &size);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001347
Cody Northrop1fedb212015-05-28 11:27:16 -06001348 return demo_prepare_shader(demo, VK_SHADER_STAGE_FRAGMENT,
1349 fragShaderCode, size);
1350 } else {
1351 static const char *fragShaderText =
1352 "#version 140\n"
1353 "#extension GL_ARB_separate_shader_objects : enable\n"
1354 "#extension GL_ARB_shading_language_420pack : enable\n"
1355 "layout (binding = 1) uniform sampler2D tex;\n"
1356 "\n"
1357 "layout (location = 0) in vec4 texcoord;\n"
1358 "layout (location = 0) out vec4 uFragColor;\n"
1359 "void main() {\n"
1360 " uFragColor = texture(tex, texcoord.xy);\n"
1361 "}\n";
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001362
Cody Northrop1fedb212015-05-28 11:27:16 -06001363 return demo_prepare_shader(demo, VK_SHADER_STAGE_FRAGMENT,
1364 (const void *) fragShaderText,
1365 strlen(fragShaderText));
1366 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001367}
1368
1369static void demo_prepare_pipeline(struct demo *demo)
1370{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001371 VkGraphicsPipelineCreateInfo pipeline;
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001372 VkPipelineCacheCreateInfo pipelineCache;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001373 VkPipelineIaStateCreateInfo ia;
1374 VkPipelineRsStateCreateInfo rs;
1375 VkPipelineCbStateCreateInfo cb;
1376 VkPipelineDsStateCreateInfo ds;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001377 VkPipelineVpStateCreateInfo vp;
1378 VkPipelineMsStateCreateInfo ms;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001379 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001380
1381 memset(&pipeline, 0, sizeof(pipeline));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001382 pipeline.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001383 pipeline.layout = demo->pipeline_layout;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001384
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001385 memset(&ia, 0, sizeof(ia));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001386 ia.sType = VK_STRUCTURE_TYPE_PIPELINE_IA_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06001387 ia.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001388
1389 memset(&rs, 0, sizeof(rs));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001390 rs.sType = VK_STRUCTURE_TYPE_PIPELINE_RS_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06001391 rs.fillMode = VK_FILL_MODE_SOLID;
1392 rs.cullMode = VK_CULL_MODE_BACK;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001393 rs.frontFace = VK_FRONT_FACE_CCW;
Chia-I Wubb67e6e2015-04-22 14:20:52 +08001394 rs.depthClipEnable = VK_TRUE;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001395
1396 memset(&cb, 0, sizeof(cb));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001397 cb.sType = VK_STRUCTURE_TYPE_PIPELINE_CB_STATE_CREATE_INFO;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001398 VkPipelineCbAttachmentState att_state[1];
Tony Barbour29645d02015-01-16 14:27:35 -07001399 memset(att_state, 0, sizeof(att_state));
1400 att_state[0].format = demo->format;
1401 att_state[0].channelWriteMask = 0xf;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001402 att_state[0].blendEnable = VK_FALSE;
Tony Barbour29645d02015-01-16 14:27:35 -07001403 cb.attachmentCount = 1;
1404 cb.pAttachments = att_state;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001405
Tony Barbour29645d02015-01-16 14:27:35 -07001406 memset(&vp, 0, sizeof(vp));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001407 vp.sType = VK_STRUCTURE_TYPE_PIPELINE_VP_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06001408 vp.viewportCount = 1;
Tony Barbour29645d02015-01-16 14:27:35 -07001409
1410 memset(&ds, 0, sizeof(ds));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001411 ds.sType = VK_STRUCTURE_TYPE_PIPELINE_DS_STATE_CREATE_INFO;
Tony Barbour29645d02015-01-16 14:27:35 -07001412 ds.format = demo->depth.format;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001413 ds.depthTestEnable = VK_TRUE;
1414 ds.depthWriteEnable = VK_TRUE;
Tony Barbour72304ef2015-04-16 15:59:00 -06001415 ds.depthCompareOp = VK_COMPARE_OP_LESS_EQUAL;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001416 ds.depthBoundsEnable = VK_FALSE;
1417 ds.back.stencilFailOp = VK_STENCIL_OP_KEEP;
1418 ds.back.stencilPassOp = VK_STENCIL_OP_KEEP;
Tony Barbour72304ef2015-04-16 15:59:00 -06001419 ds.back.stencilCompareOp = VK_COMPARE_OP_ALWAYS;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001420 ds.stencilTestEnable = VK_FALSE;
Tony Barbour29645d02015-01-16 14:27:35 -07001421 ds.front = ds.back;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001422
Tony Barbour29645d02015-01-16 14:27:35 -07001423 memset(&ms, 0, sizeof(ms));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001424 ms.sType = VK_STRUCTURE_TYPE_PIPELINE_MS_STATE_CREATE_INFO;
Tony Barbour29645d02015-01-16 14:27:35 -07001425 ms.sampleMask = 1;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001426 ms.multisampleEnable = VK_FALSE;
Tony Barbour3ca22502015-06-26 10:18:34 -06001427 ms.rasterSamples = 1;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001428
Mark Lobodzinski521d7762015-06-23 15:11:57 -06001429 // Two stages: vs and fs
1430 pipeline.stageCount = 2;
1431 VkPipelineShaderStageCreateInfo shaderStages[2];
1432 memset(&shaderStages, 0, 2 * sizeof(VkPipelineShaderStageCreateInfo));
1433
1434 shaderStages[0].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1435 shaderStages[0].stage = VK_SHADER_STAGE_VERTEX;
1436 shaderStages[0].shader = demo_prepare_vs(demo);
1437
1438 shaderStages[1].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1439 shaderStages[1].stage = VK_SHADER_STAGE_FRAGMENT;
1440 shaderStages[1].shader = demo_prepare_fs(demo);
1441
Mark Lobodzinski15169cf2015-06-30 10:18:36 -06001442 pipeline.pVertexInputState = NULL;
Mark Lobodzinski521d7762015-06-23 15:11:57 -06001443 pipeline.pIaState = &ia;
1444 pipeline.pRsState = &rs;
1445 pipeline.pCbState = &cb;
1446 pipeline.pMsState = &ms;
1447 pipeline.pVpState = &vp;
1448 pipeline.pDsState = &ds;
1449 pipeline.pStages = shaderStages;
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001450 memset(&pipelineCache, 0, sizeof(pipelineCache));
1451 pipelineCache.sType = VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001452
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001453 err = vkCreatePipelineCache(demo->device, &pipelineCache, &demo->pipelineCache);
1454 assert(!err);
1455 err = vkCreateGraphicsPipelines(demo->device, demo->pipelineCache, 1, &pipeline, &demo->pipeline);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001456 assert(!err);
1457
Mark Lobodzinski521d7762015-06-23 15:11:57 -06001458 for (uint32_t i = 0; i < pipeline.stageCount; i++) {
1459 vkDestroyObject(demo->device, VK_OBJECT_TYPE_SHADER, shaderStages[i].shader);
1460 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001461}
1462
1463static void demo_prepare_dynamic_states(struct demo *demo)
1464{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001465 VkDynamicVpStateCreateInfo viewport_create;
1466 VkDynamicRsStateCreateInfo raster;
1467 VkDynamicCbStateCreateInfo color_blend;
1468 VkDynamicDsStateCreateInfo depth_stencil;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001469 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001470
Tony Barbour29645d02015-01-16 14:27:35 -07001471 memset(&viewport_create, 0, sizeof(viewport_create));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001472 viewport_create.sType = VK_STRUCTURE_TYPE_DYNAMIC_VP_STATE_CREATE_INFO;
Courtney Goeltzenleuchter1710d8d2015-02-11 14:13:34 -07001473 viewport_create.viewportAndScissorCount = 1;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001474 VkViewport viewport;
Piers Daniell735ee532015-02-23 16:23:13 -07001475 memset(&viewport, 0, sizeof(viewport));
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001476 viewport.height = (float) demo->height;
1477 viewport.width = (float) demo->width;
1478 viewport.minDepth = (float) 0.0f;
1479 viewport.maxDepth = (float) 1.0f;
Piers Daniell735ee532015-02-23 16:23:13 -07001480 viewport_create.pViewports = &viewport;
Chris Forbesfaa91732015-06-22 17:21:59 +12001481 VkRect2D scissor;
Piers Daniell735ee532015-02-23 16:23:13 -07001482 memset(&scissor, 0, sizeof(scissor));
Courtney Goeltzenleuchter1710d8d2015-02-11 14:13:34 -07001483 scissor.extent.width = demo->width;
1484 scissor.extent.height = demo->height;
1485 scissor.offset.x = 0;
1486 scissor.offset.y = 0;
Courtney Goeltzenleuchter1710d8d2015-02-11 14:13:34 -07001487 viewport_create.pScissors = &scissor;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001488
1489 memset(&raster, 0, sizeof(raster));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001490 raster.sType = VK_STRUCTURE_TYPE_DYNAMIC_RS_STATE_CREATE_INFO;
Piers Daniell735ee532015-02-23 16:23:13 -07001491 raster.lineWidth = 1.0;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001492
1493 memset(&color_blend, 0, sizeof(color_blend));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001494 color_blend.sType = VK_STRUCTURE_TYPE_DYNAMIC_CB_STATE_CREATE_INFO;
Piers Daniell735ee532015-02-23 16:23:13 -07001495 color_blend.blendConst[0] = 1.0f;
1496 color_blend.blendConst[1] = 1.0f;
1497 color_blend.blendConst[2] = 1.0f;
1498 color_blend.blendConst[3] = 1.0f;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001499
1500 memset(&depth_stencil, 0, sizeof(depth_stencil));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001501 depth_stencil.sType = VK_STRUCTURE_TYPE_DYNAMIC_DS_STATE_CREATE_INFO;
Mark Lobodzinski89d32b12015-06-12 11:14:17 -06001502 depth_stencil.minDepthBounds = 0.0f;
1503 depth_stencil.maxDepthBounds = 1.0f;
Tony Barbour29645d02015-01-16 14:27:35 -07001504 depth_stencil.stencilBackRef = 0;
1505 depth_stencil.stencilFrontRef = 0;
1506 depth_stencil.stencilReadMask = 0xff;
1507 depth_stencil.stencilWriteMask = 0xff;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001508
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001509 err = vkCreateDynamicViewportState(demo->device, &viewport_create, &demo->viewport);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001510 assert(!err);
1511
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001512 err = vkCreateDynamicRasterState(demo->device, &raster, &demo->raster);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001513 assert(!err);
1514
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001515 err = vkCreateDynamicColorBlendState(demo->device,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001516 &color_blend, &demo->color_blend);
1517 assert(!err);
1518
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001519 err = vkCreateDynamicDepthStencilState(demo->device,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001520 &depth_stencil, &demo->depth_stencil);
1521 assert(!err);
1522}
1523
Chia-I Wu63ea9262015-03-26 13:14:16 +08001524static void demo_prepare_descriptor_pool(struct demo *demo)
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001525{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001526 const VkDescriptorTypeCount type_counts[2] = {
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001527 [0] = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001528 .type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001529 .count = 1,
1530 },
1531 [1] = {
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -06001532 .type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001533 .count = DEMO_TEXTURE_COUNT,
1534 },
1535 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001536 const VkDescriptorPoolCreateInfo descriptor_pool = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001537 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001538 .pNext = NULL,
1539 .count = 2,
1540 .pTypeCount = type_counts,
1541 };
Tony Barbourfdc2d352015-04-22 09:02:32 -06001542 VkResult U_ASSERT_ONLY err;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001543
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001544 err = vkCreateDescriptorPool(demo->device,
1545 VK_DESCRIPTOR_POOL_USAGE_ONE_SHOT, 1,
Chia-I Wu63ea9262015-03-26 13:14:16 +08001546 &descriptor_pool, &demo->desc_pool);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001547 assert(!err);
1548}
1549
1550static void demo_prepare_descriptor_set(struct demo *demo)
1551{
Chia-I Wuae721ba2015-05-25 16:27:55 +08001552 VkDescriptorInfo tex_descs[DEMO_TEXTURE_COUNT];
1553 VkWriteDescriptorSet writes[2];
Tony Barbourfdc2d352015-04-22 09:02:32 -06001554 VkResult U_ASSERT_ONLY err;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001555 uint32_t count;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001556 uint32_t i;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001557
Mike Stroyanebae8322015-04-17 12:36:38 -06001558 err = vkAllocDescriptorSets(demo->device, demo->desc_pool,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001559 VK_DESCRIPTOR_SET_USAGE_STATIC,
Chia-I Wu87544e72015-02-23 10:41:08 -07001560 1, &demo->desc_layout,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001561 &demo->desc_set, &count);
1562 assert(!err && count == 1);
1563
Chia-I Wuae721ba2015-05-25 16:27:55 +08001564 memset(&tex_descs, 0, sizeof(tex_descs));
1565 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
1566 tex_descs[i].sampler = demo->textures[i].sampler;
1567 tex_descs[i].imageView = demo->textures[i].view;
1568 tex_descs[i].imageLayout = VK_IMAGE_LAYOUT_GENERAL;
1569 }
1570
1571 memset(&writes, 0, sizeof(writes));
1572
1573 writes[0].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
1574 writes[0].destSet = demo->desc_set;
1575 writes[0].count = 1;
1576 writes[0].descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
1577 writes[0].pDescriptors = &demo->uniform_data.desc;
1578
1579 writes[1].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
1580 writes[1].destSet = demo->desc_set;
1581 writes[1].destBinding = 1;
1582 writes[1].count = DEMO_TEXTURE_COUNT;
1583 writes[1].descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
1584 writes[1].pDescriptors = tex_descs;
1585
1586 err = vkUpdateDescriptorSets(demo->device, 2, writes, 0, NULL);
1587 assert(!err);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001588}
1589
Chia-I Wuf973e322015-07-08 13:34:24 +08001590static void demo_prepare_framebuffers(struct demo *demo)
1591{
1592 VkColorAttachmentBindInfo color_attachment = {
1593 .view = VK_NULL_HANDLE,
1594 .layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
1595 };
1596 const VkDepthStencilBindInfo depth_stencil = {
1597 .view = demo->depth.view,
1598 .layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1599 };
1600 const VkFramebufferCreateInfo fb_info = {
1601 .sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO,
1602 .pNext = NULL,
1603 .colorAttachmentCount = 1,
1604 .pColorAttachments = &color_attachment,
1605 .pDepthStencilAttachment = &depth_stencil,
1606 .sampleCount = 1,
1607 .width = demo->width,
1608 .height = demo->height,
1609 .layers = 1,
1610 };
1611 VkResult U_ASSERT_ONLY err;
1612 uint32_t i;
1613
1614 for (i = 0; i < DEMO_BUFFER_COUNT; i++) {
1615 color_attachment.view = demo->buffers[i].view;
1616 err = vkCreateFramebuffer(demo->device, &fb_info, &demo->framebuffers[i]);
1617 assert(!err);
1618 }
1619}
1620
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001621static void demo_prepare(struct demo *demo)
1622{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001623 const VkCmdBufferCreateInfo cmd = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001624 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001625 .pNext = NULL,
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07001626 .queueNodeIndex = demo->graphics_queue_node_index,
Chia-I Wu57b23b42015-06-26 15:34:39 +08001627 .level = VK_CMD_BUFFER_LEVEL_PRIMARY,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001628 .flags = 0,
1629 };
Tony Barbourfdc2d352015-04-22 09:02:32 -06001630 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001631
1632 demo_prepare_buffers(demo);
1633 demo_prepare_depth(demo);
1634 demo_prepare_textures(demo);
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001635 demo_prepare_cube_data_buffer(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001636
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001637 demo_prepare_descriptor_layout(demo);
Chia-I Wuf973e322015-07-08 13:34:24 +08001638 demo_prepare_render_pass(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001639 demo_prepare_pipeline(demo);
1640 demo_prepare_dynamic_states(demo);
1641
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001642 for (int i = 0; i < DEMO_BUFFER_COUNT; i++) {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001643 err = vkCreateCommandBuffer(demo->device, &cmd, &demo->buffers[i].cmd);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001644 assert(!err);
1645 }
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001646
Chia-I Wu63ea9262015-03-26 13:14:16 +08001647 demo_prepare_descriptor_pool(demo);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001648 demo_prepare_descriptor_set(demo);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001649
Chia-I Wuf973e322015-07-08 13:34:24 +08001650 demo_prepare_framebuffers(demo);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001651
1652 for (int i = 0; i < DEMO_BUFFER_COUNT; i++) {
1653 demo->current_buffer = i;
1654 demo_draw_build_cmd(demo, demo->buffers[i].cmd);
1655 }
1656
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001657 /*
1658 * Prepare functions above may generate pipeline commands
1659 * that need to be flushed before beginning the render loop.
1660 */
1661 demo_flush_init_cmd(demo);
1662
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001663 demo->current_buffer = 0;
Jon Ashburn8d26c062015-04-24 09:46:24 -07001664 demo->prepared = true;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001665}
1666
David Pinedo2bb7c932015-06-18 17:03:14 -06001667static void demo_cleanup(struct demo *demo)
1668{
1669 uint32_t i;
1670
1671 demo->prepared = false;
1672
Chia-I Wuf973e322015-07-08 13:34:24 +08001673 for (i = 0; i < DEMO_BUFFER_COUNT; i++)
1674 vkDestroyObject(demo->device, VK_OBJECT_TYPE_FRAMEBUFFER, demo->framebuffers[i]);
1675
David Pinedo2bb7c932015-06-18 17:03:14 -06001676 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DESCRIPTOR_SET, demo->desc_set);
1677 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DESCRIPTOR_POOL, demo->desc_pool);
1678
1679 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DYNAMIC_VP_STATE, demo->viewport);
1680 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DYNAMIC_RS_STATE, demo->raster);
1681 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DYNAMIC_CB_STATE, demo->color_blend);
1682 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DYNAMIC_DS_STATE, demo->depth_stencil);
1683
1684 vkDestroyObject(demo->device, VK_OBJECT_TYPE_PIPELINE, demo->pipeline);
Jon Ashburnc4ab7af2015-07-09 15:02:25 -06001685 vkDestroyPipelineCache(demo->device, demo->pipelineCache);
Chia-I Wuf973e322015-07-08 13:34:24 +08001686 vkDestroyObject(demo->device, VK_OBJECT_TYPE_RENDER_PASS, demo->render_pass);
David Pinedo2bb7c932015-06-18 17:03:14 -06001687 vkDestroyObject(demo->device, VK_OBJECT_TYPE_PIPELINE_LAYOUT, demo->pipeline_layout);
1688 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DESCRIPTOR_SET_LAYOUT, demo->desc_layout);
1689
1690 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
1691 vkDestroyObject(demo->device, VK_OBJECT_TYPE_IMAGE_VIEW, demo->textures[i].view);
1692 vkDestroyObject(demo->device, VK_OBJECT_TYPE_IMAGE, demo->textures[i].image);
1693 vkFreeMemory(demo->device, demo->textures[i].mem);
1694 vkDestroyObject(demo->device, VK_OBJECT_TYPE_SAMPLER, demo->textures[i].sampler);
1695 }
1696 demo->fpDestroySwapChainWSI(demo->swap_chain);
1697
1698 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DEPTH_STENCIL_VIEW, demo->depth.view);
1699 vkDestroyObject(demo->device, VK_OBJECT_TYPE_IMAGE, demo->depth.image);
1700 vkFreeMemory(demo->device, demo->depth.mem);
1701
1702 vkDestroyObject(demo->device, VK_OBJECT_TYPE_BUFFER_VIEW, demo->uniform_data.view);
1703 vkDestroyObject(demo->device, VK_OBJECT_TYPE_BUFFER, demo->uniform_data.buf);
1704 vkFreeMemory(demo->device, demo->uniform_data.mem);
1705
1706 for (i = 0; i < DEMO_BUFFER_COUNT; i++) {
1707 vkDestroyObject(demo->device, VK_OBJECT_TYPE_COLOR_ATTACHMENT_VIEW, demo->buffers[i].view);
1708 vkDestroyObject(demo->device, VK_OBJECT_TYPE_COMMAND_BUFFER, demo->buffers[i].cmd);
1709 }
1710
1711 vkDestroyDevice(demo->device);
Tony Barbourd70b42c2015-06-30 14:14:19 -06001712 if (demo->validate) {
1713 demo->dbgDestroyMsgCallback(demo->inst, demo->msg_callback);
1714 }
David Pinedo2bb7c932015-06-18 17:03:14 -06001715 vkDestroyInstance(demo->inst);
1716
1717#ifndef _WIN32
1718 xcb_destroy_window(demo->connection, demo->window);
1719 xcb_disconnect(demo->connection);
1720#endif // _WIN32
1721}
1722
1723// On MS-Windows, make this a global, so it's available to WndProc()
1724struct demo demo;
1725
Ian Elliott639ca472015-04-16 15:23:05 -06001726#ifdef _WIN32
1727static void demo_run(struct demo *demo)
1728{
Courtney Goeltzenleuchterb7e22702015-04-27 14:56:34 -06001729 if (!demo->prepared)
1730 return;
Ian Elliott639ca472015-04-16 15:23:05 -06001731 // Wait for work to finish before updating MVP.
1732 vkDeviceWaitIdle(demo->device);
1733 demo_update_data_buffer(demo);
1734
1735 demo_draw(demo);
1736
1737 // Wait for work to finish before updating MVP.
1738 vkDeviceWaitIdle(demo->device);
Ian Elliott639ca472015-04-16 15:23:05 -06001739
David Pinedo2bb7c932015-06-18 17:03:14 -06001740 demo->curFrame++;
1741
1742 if (demo->frameCount != INT_MAX && demo->curFrame == demo->frameCount)
1743 {
1744 demo->quit=true;
1745 demo_cleanup(demo);
1746 ExitProcess(0);
1747 }
1748
1749}
Ian Elliott639ca472015-04-16 15:23:05 -06001750
1751// MS-Windows event handling function:
1752LRESULT CALLBACK WndProc(HWND hWnd,
1753 UINT uMsg,
1754 WPARAM wParam,
1755 LPARAM lParam)
1756{
Ian Elliott639ca472015-04-16 15:23:05 -06001757 switch(uMsg)
1758 {
Ian Elliott639ca472015-04-16 15:23:05 -06001759 case WM_CLOSE:
1760 PostQuitMessage(0);
Tony Barbour3dddd5d2015-04-29 16:19:20 -06001761 break;
Ian Elliott639ca472015-04-16 15:23:05 -06001762 case WM_PAINT:
1763 demo_run(&demo);
1764 return 0;
1765 default:
1766 break;
1767 }
1768 return (DefWindowProc(hWnd, uMsg, wParam, lParam));
1769}
1770
1771static void demo_create_window(struct demo *demo)
1772{
1773 WNDCLASSEX win_class;
1774
1775 // Initialize the window class structure:
1776 win_class.cbSize = sizeof(WNDCLASSEX);
1777 win_class.style = CS_HREDRAW | CS_VREDRAW;
1778 win_class.lpfnWndProc = WndProc;
1779 win_class.cbClsExtra = 0;
1780 win_class.cbWndExtra = 0;
1781 win_class.hInstance = demo->connection; // hInstance
1782 win_class.hIcon = LoadIcon(NULL, IDI_APPLICATION);
1783 win_class.hCursor = LoadCursor(NULL, IDC_ARROW);
1784 win_class.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
1785 win_class.lpszMenuName = NULL;
1786 win_class.lpszClassName = demo->name;
1787 win_class.hIconSm = LoadIcon(NULL, IDI_WINLOGO);
1788 // Register window class:
1789 if (!RegisterClassEx(&win_class)) {
1790 // It didn't work, so try to give a useful error:
1791 printf("Unexpected error trying to start the application!\n");
1792 fflush(stdout);
1793 exit(1);
1794 }
1795 // Create window with the registered class:
Mike Stroyanb46779e2015-06-15 14:20:13 -06001796 RECT wr = { 0, 0, demo->width, demo->height };
1797 AdjustWindowRect(&wr, WS_OVERLAPPEDWINDOW, FALSE);
Ian Elliott639ca472015-04-16 15:23:05 -06001798 demo->window = CreateWindowEx(0,
1799 demo->name, // class name
1800 demo->name, // app name
1801 WS_OVERLAPPEDWINDOW | // window style
1802 WS_VISIBLE |
1803 WS_SYSMENU,
1804 100,100, // x/y coords
Mike Stroyanb46779e2015-06-15 14:20:13 -06001805 wr.right-wr.left, // width
1806 wr.bottom-wr.top, // height
Ian Elliott639ca472015-04-16 15:23:05 -06001807 NULL, // handle to parent
1808 NULL, // handle to menu
1809 demo->connection, // hInstance
1810 NULL); // no extra parameters
1811 if (!demo->window) {
1812 // It didn't work, so try to give a useful error:
1813 printf("Cannot create a window in which to draw!\n");
1814 fflush(stdout);
1815 exit(1);
1816 }
1817}
1818#else // _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001819static void demo_handle_event(struct demo *demo,
1820 const xcb_generic_event_t *event)
1821{
Piers Daniell735ee532015-02-23 16:23:13 -07001822 uint8_t event_code = event->response_type & 0x7f;
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07001823 switch (event_code) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001824 case XCB_EXPOSE:
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07001825 // TODO: Resize window
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001826 break;
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07001827 case XCB_CLIENT_MESSAGE:
1828 if((*(xcb_client_message_event_t*)event).data.data32[0] ==
1829 (*demo->atom_wm_delete_window).atom) {
1830 demo->quit = true;
1831 }
1832 break;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001833 case XCB_KEY_RELEASE:
1834 {
1835 const xcb_key_release_event_t *key =
1836 (const xcb_key_release_event_t *) event;
1837
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001838 switch (key->detail) {
1839 case 0x9: // Escape
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001840 demo->quit = true;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001841 break;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001842 case 0x71: // left arrow key
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001843 demo->spin_angle += demo->spin_increment;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001844 break;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001845 case 0x72: // right arrow key
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001846 demo->spin_angle -= demo->spin_increment;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001847 break;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001848 case 0x41:
1849 demo->pause = !demo->pause;
Piers Daniell735ee532015-02-23 16:23:13 -07001850 break;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001851 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001852 }
1853 break;
1854 default:
1855 break;
1856 }
1857}
1858
1859static void demo_run(struct demo *demo)
1860{
1861 xcb_flush(demo->connection);
1862
1863 while (!demo->quit) {
1864 xcb_generic_event_t *event;
1865
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07001866 if (demo->pause) {
1867 event = xcb_wait_for_event(demo->connection);
1868 } else {
1869 event = xcb_poll_for_event(demo->connection);
1870 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001871 if (event) {
1872 demo_handle_event(demo, event);
1873 free(event);
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07001874 }
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07001875
1876 // Wait for work to finish before updating MVP.
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001877 vkDeviceWaitIdle(demo->device);
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07001878 demo_update_data_buffer(demo);
1879
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07001880 demo_draw(demo);
Courtney Goeltzenleuchter1454f3c2014-11-18 11:28:09 -07001881
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07001882 // Wait for work to finish before updating MVP.
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001883 vkDeviceWaitIdle(demo->device);
David Pinedo2bb7c932015-06-18 17:03:14 -06001884 demo->curFrame++;
1885 if (demo->frameCount != INT_MAX && demo->curFrame == demo->frameCount)
1886 demo->quit = true;
1887
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001888 }
1889}
1890
1891static void demo_create_window(struct demo *demo)
1892{
1893 uint32_t value_mask, value_list[32];
1894
1895 demo->window = xcb_generate_id(demo->connection);
1896
1897 value_mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK;
1898 value_list[0] = demo->screen->black_pixel;
1899 value_list[1] = XCB_EVENT_MASK_KEY_RELEASE |
1900 XCB_EVENT_MASK_EXPOSURE;
1901
1902 xcb_create_window(demo->connection,
1903 XCB_COPY_FROM_PARENT,
1904 demo->window, demo->screen->root,
1905 0, 0, demo->width, demo->height, 0,
1906 XCB_WINDOW_CLASS_INPUT_OUTPUT,
1907 demo->screen->root_visual,
1908 value_mask, value_list);
1909
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07001910 /* Magic code that will send notification when window is destroyed */
1911 xcb_intern_atom_cookie_t cookie = xcb_intern_atom(demo->connection, 1, 12,
1912 "WM_PROTOCOLS");
1913 xcb_intern_atom_reply_t* reply = xcb_intern_atom_reply(demo->connection, cookie, 0);
1914
1915 xcb_intern_atom_cookie_t cookie2 = xcb_intern_atom(demo->connection, 0, 16, "WM_DELETE_WINDOW");
1916 demo->atom_wm_delete_window = xcb_intern_atom_reply(demo->connection, cookie2, 0);
1917
1918 xcb_change_property(demo->connection, XCB_PROP_MODE_REPLACE,
1919 demo->window, (*reply).atom, 4, 32, 1,
1920 &(*demo->atom_wm_delete_window).atom);
1921 free(reply);
1922
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001923 xcb_map_window(demo->connection, demo->window);
David Pinedo2bb7c932015-06-18 17:03:14 -06001924
1925 // Force the x/y coordinates to 100,100 results are identical in consecutive runs
1926 const uint32_t coords[] = {100, 100};
1927 xcb_configure_window(demo->connection, demo->window,
1928 XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y, coords);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001929}
Ian Elliott639ca472015-04-16 15:23:05 -06001930#endif // _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001931
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06001932/*
1933 * Return 1 (true) if all layer names specified in check_names
1934 * can be found in given layer properties.
1935 */
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06001936static VkBool32 demo_check_layers(uint32_t check_count, char **check_names,
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06001937 uint32_t layer_count, VkLayerProperties *layers)
1938{
1939 for (uint32_t i = 0; i < check_count; i++) {
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06001940 VkBool32 found = 0;
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06001941 for (uint32_t j = 0; j < layer_count; j++) {
1942 if (!strcmp(check_names[i], layers[j].layerName)) {
1943 found = 1;
1944 }
1945 }
1946 if (!found) {
1947 fprintf(stderr, "Cannot find layer: %s\n", check_names[i]);
1948 return 0;
1949 }
1950 }
1951 return 1;
1952}
1953
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001954static void demo_init_vk(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001955{
Tobin Ehlis99239dc2015-04-16 18:04:57 -06001956 VkResult err;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06001957 char *extension_names[64];
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06001958 VkExtensionProperties *instance_extensions;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06001959 VkLayerProperties *instance_layers;
1960 VkLayerProperties *device_layers;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06001961 uint32_t instance_extension_count = 0;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06001962 uint32_t instance_layer_count = 0;
1963 uint32_t enabled_extension_count = 0;
1964 uint32_t enabled_layer_count = 0;
1965
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06001966 char *instance_validation_layers[] = {
1967 "MemTracker",
1968 };
1969
1970 char *device_validation_layers[] = {
1971 "MemTracker",
1972 };
1973
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06001974 /* Look for validation layers */
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06001975 VkBool32 validation_found = 0;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06001976 err = vkGetGlobalLayerProperties(&instance_layer_count, NULL);
Tobin Ehlis99239dc2015-04-16 18:04:57 -06001977 assert(!err);
1978
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06001979 instance_layers = malloc(sizeof(VkLayerProperties) * instance_layer_count);
1980 err = vkGetGlobalLayerProperties(&instance_layer_count, instance_layers);
1981 assert(!err);
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06001982
1983 if (demo->validate) {
1984 validation_found = demo_check_layers(ARRAY_SIZE(instance_validation_layers), instance_validation_layers,
1985 instance_layer_count, instance_layers);
1986 if (!validation_found) {
1987 ERR_EXIT("vkGetGlobalLayerProperties failed to find"
1988 "required validation layer.\n\n"
1989 "Please look at the Getting Started guide for additional "
1990 "information.\n",
1991 "vkCreateInstance Failure");
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06001992 }
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06001993 enabled_layer_count = ARRAY_SIZE(instance_validation_layers);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06001994 }
1995
1996 err = vkGetGlobalExtensionProperties(NULL, &instance_extension_count, NULL);
1997 assert(!err);
1998
Courtney Goeltzenleuchterc88ce512015-07-09 11:44:38 -06001999 VkBool32 WSIextFound = 0;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002000 memset(extension_names, 0, sizeof(extension_names));
2001 instance_extensions = malloc(sizeof(VkExtensionProperties) * instance_extension_count);
2002 err = vkGetGlobalExtensionProperties(NULL, &instance_extension_count, instance_extensions);
2003 assert(!err);
2004 for (uint32_t i = 0; i < instance_extension_count; i++) {
2005 if (!strcmp(VK_WSI_LUNARG_EXTENSION_NAME, instance_extensions[i].extName)) {
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002006 WSIextFound = 1;
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002007 extension_names[enabled_extension_count++] = VK_WSI_LUNARG_EXTENSION_NAME;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002008 }
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002009 if (!strcmp(DEBUG_REPORT_EXTENSION_NAME, instance_extensions[i].extName)) {
2010 if (demo->validate) {
2011 extension_names[enabled_extension_count++] = DEBUG_REPORT_EXTENSION_NAME;
2012 }
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002013 }
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002014 assert(enabled_extension_count < 64);
Tobin Ehlis99239dc2015-04-16 18:04:57 -06002015 }
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002016 if (!WSIextFound) {
Tony Barbourecf1b2b2015-06-24 16:06:58 -06002017 ERR_EXIT("vkGetGlobalExtensionProperties failed to find the "
Ian Elliott65152912015-04-28 13:22:33 -06002018 "\"VK_WSI_LunarG\" extension.\n\nDo you have a compatible "
2019 "Vulkan installable client driver (ICD) installed?\nPlease "
2020 "look at the Getting Started guide for additional "
2021 "information.\n",
2022 "vkCreateInstance Failure");
2023 }
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06002024 const VkApplicationInfo app = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002025 .sType = VK_STRUCTURE_TYPE_APPLICATION_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002026 .pNext = NULL,
Ian Elliott44e33f72015-04-28 10:52:52 -06002027 .pAppName = APP_SHORT_NAME,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002028 .appVersion = 0,
Ian Elliott44e33f72015-04-28 10:52:52 -06002029 .pEngineName = APP_SHORT_NAME,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002030 .engineVersion = 0,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002031 .apiVersion = VK_API_VERSION,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002032 };
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002033 VkInstanceCreateInfo inst_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002034 .sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
Jon Ashburnab46b362015-04-04 14:52:07 -06002035 .pNext = NULL,
2036 .pAppInfo = &app,
2037 .pAllocCb = NULL,
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002038 .layerCount = enabled_layer_count,
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002039 .ppEnabledLayerNames = (const char *const*) ((demo->validate) ? instance_validation_layers : NULL),
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002040 .extensionCount = enabled_extension_count,
2041 .ppEnabledExtensionNames = (const char *const*) extension_names,
Jon Ashburnab46b362015-04-04 14:52:07 -06002042 };
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -06002043 const VkDeviceQueueCreateInfo queue = {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002044 .queueNodeIndex = 0,
2045 .queueCount = 1,
2046 };
Ian Elliott097d9f32015-04-28 11:35:02 -06002047
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06002048 uint32_t gpu_count;
2049 uint32_t i;
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07002050 uint32_t queue_count;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002051
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002052 err = vkCreateInstance(&inst_info, &demo->inst);
Ian Elliott07264132015-04-28 11:35:02 -06002053 if (err == VK_ERROR_INCOMPATIBLE_DRIVER) {
2054 ERR_EXIT("Cannot find a compatible Vulkan installable client driver "
Ian Elliott65152912015-04-28 13:22:33 -06002055 "(ICD).\n\nPlease look at the Getting Started guide for "
Ian Elliott07264132015-04-28 11:35:02 -06002056 "additional information.\n",
2057 "vkCreateInstance Failure");
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002058 } else if (err == VK_ERROR_INVALID_EXTENSION) {
2059 ERR_EXIT("Cannot find a specified extension library"
2060 ".\nMake sure your layers path is set appropriately\n",
2061 "vkCreateInstance Failure");
Ian Elliott07264132015-04-28 11:35:02 -06002062 } else if (err) {
Ian Elliott65152912015-04-28 13:22:33 -06002063 ERR_EXIT("vkCreateInstance failed.\n\nDo you have a compatible Vulkan "
2064 "installable client driver (ICD) installed?\nPlease look at "
Ian Elliott07264132015-04-28 11:35:02 -06002065 "the Getting Started guide for additional information.\n",
2066 "vkCreateInstance Failure");
Ian Elliott3979e282015-04-03 15:24:55 -06002067 }
Jon Ashburnab46b362015-04-04 14:52:07 -06002068
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002069 free(instance_layers);
2070 free(instance_extensions);
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002071
Jon Ashburn07c0c0c2015-04-15 11:31:12 -06002072 gpu_count = 1;
2073 err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, &demo->gpu);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002074 assert(!err && gpu_count == 1);
2075
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002076 /* Look for validation layers */
2077 validation_found = 0;
2078 enabled_layer_count = 0;
2079 uint32_t device_layer_count = 0;
2080 err = vkGetPhysicalDeviceLayerProperties(demo->gpu, &device_layer_count, NULL);
2081 assert(!err);
2082
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002083 device_layers = malloc(sizeof(VkLayerProperties) * device_layer_count);
2084 err = vkGetPhysicalDeviceLayerProperties(demo->gpu, &device_layer_count, device_layers);
2085 assert(!err);
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002086
2087 if (demo->validate) {
2088 validation_found = demo_check_layers(ARRAY_SIZE(device_validation_layers), device_validation_layers,
2089 device_layer_count, device_layers);
2090 if (!validation_found) {
2091 ERR_EXIT("vkGetPhysicalDeviceLayerProperties failed to find"
2092 "a required validation layer.\n\n"
2093 "Please look at the Getting Started guide for additional "
2094 "information.\n",
2095 "vkCreateDevice Failure");
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002096 }
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002097 enabled_layer_count = ARRAY_SIZE(device_validation_layers);
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002098 }
2099
2100 uint32_t device_extension_count = 0;
2101 VkExtensionProperties *device_extensions = NULL;
2102 err = vkGetPhysicalDeviceExtensionProperties(
2103 demo->gpu, NULL, &device_extension_count, NULL);
2104 assert(!err);
2105
2106 WSIextFound = 0;
2107 enabled_extension_count = 0;
2108 memset(extension_names, 0, sizeof(extension_names));
2109 device_extensions = malloc(sizeof(VkExtensionProperties) * device_extension_count);
2110 err = vkGetPhysicalDeviceExtensionProperties(
2111 demo->gpu, NULL, &device_extension_count, device_extensions);
2112 assert(!err);
Courtney Goeltzenleuchterff6e23a2015-07-05 22:08:51 -06002113#if 0
2114 /* Will need this check in future */
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002115 for (uint32_t i = 0; i < device_extension_count; i++) {
2116 if (!strcmp(VK_WSI_LUNARG_EXTENSION_NAME, device_extensions[i].extName)) {
2117 WSIextFound = 1;
2118 extension_names[enabled_extension_count++] = VK_WSI_LUNARG_EXTENSION_NAME;
2119 }
2120 assert(enabled_extension_count < 64);
2121 }
2122 if (!WSIextFound) {
2123 ERR_EXIT("vkGetGlobalExtensionProperties failed to find the "
2124 "\"VK_WSI_LunarG\" extension.\n\nDo you have a compatible "
2125 "Vulkan installable client driver (ICD) installed?\nPlease "
2126 "look at the Getting Started guide for additional "
2127 "information.\n",
2128 "vkCreateInstance Failure");
2129 }
Courtney Goeltzenleuchterff6e23a2015-07-05 22:08:51 -06002130#endif
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002131
2132 VkDeviceCreateInfo device = {
2133 .sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
2134 .pNext = NULL,
2135 .queueRecordCount = 1,
2136 .pRequestedQueues = &queue,
2137 .layerCount = enabled_layer_count,
Courtney Goeltzenleuchter886f76c2015-07-06 17:46:11 -06002138 .ppEnabledLayerNames = (const char *const*) ((demo->validate) ? device_validation_layers : NULL),
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002139 .extensionCount = enabled_extension_count,
2140 .ppEnabledExtensionNames = (const char *const*) extension_names,
2141 .flags = 0,
2142 };
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002143
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002144 if (demo->validate) {
Tony Barbourd70b42c2015-06-30 14:14:19 -06002145 demo->dbgCreateMsgCallback = vkGetInstanceProcAddr(demo->inst, "vkDbgCreateMsgCallback");
2146 demo->dbgDestroyMsgCallback = vkGetInstanceProcAddr(demo->inst, "vkDbgDestroyMsgCallback");
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002147 if (!demo->dbgCreateMsgCallback) {
2148 ERR_EXIT("GetProcAddr: Unable to find vkDbgCreateMsgCallback\n",
2149 "vkGetProcAddr Failure");
2150 }
Tony Barbourd70b42c2015-06-30 14:14:19 -06002151 if (!demo->dbgDestroyMsgCallback) {
2152 ERR_EXIT("GetProcAddr: Unable to find vkDbgDestroyMsgCallback\n",
2153 "vkGetProcAddr Failure");
2154 }
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06002155 err = demo->dbgCreateMsgCallback(
2156 demo->inst,
2157 VK_DBG_REPORT_ERROR_BIT | VK_DBG_REPORT_WARN_BIT,
2158 dbgFunc, NULL,
2159 &demo->msg_callback);
2160 switch (err) {
2161 case VK_SUCCESS:
2162 break;
2163 case VK_ERROR_INVALID_POINTER:
2164 ERR_EXIT("dbgCreateMsgCallback: Invalid pointer\n",
2165 "dbgCreateMsgCallback Failure");
2166 break;
2167 case VK_ERROR_OUT_OF_HOST_MEMORY:
2168 ERR_EXIT("dbgCreateMsgCallback: out of host memory\n",
2169 "dbgCreateMsgCallback Failure");
2170 break;
2171 default:
2172 ERR_EXIT("dbgCreateMsgCallback: unknown failure\n",
2173 "dbgCreateMsgCallback Failure");
2174 break;
2175 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002176 }
2177
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002178 err = vkCreateDevice(demo->gpu, &device, &demo->device);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002179 assert(!err);
2180
Courtney Goeltzenleuchterbe103362015-06-29 15:39:26 -06002181 free(device_layers);
2182
Ian Elliott673898b2015-06-22 15:07:49 -06002183 GET_DEVICE_PROC_ADDR(demo->device, CreateSwapChainWSI);
2184 GET_DEVICE_PROC_ADDR(demo->device, CreateSwapChainWSI);
2185 GET_DEVICE_PROC_ADDR(demo->device, DestroySwapChainWSI);
2186 GET_DEVICE_PROC_ADDR(demo->device, GetSwapChainInfoWSI);
2187 GET_DEVICE_PROC_ADDR(demo->device, QueuePresentWSI);
Jon Ashburn0b85d052015-05-21 18:13:33 -06002188
Tony Barbourecf1b2b2015-06-24 16:06:58 -06002189 err = vkGetPhysicalDeviceProperties(demo->gpu, &demo->gpu_props);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06002190 assert(!err);
2191
Tony Barbourecf1b2b2015-06-24 16:06:58 -06002192 err = vkGetPhysicalDeviceQueueCount(demo->gpu, &queue_count);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06002193 assert(!err);
2194
Tony Barbourecf1b2b2015-06-24 16:06:58 -06002195 demo->queue_props = (VkPhysicalDeviceQueueProperties *) malloc(queue_count * sizeof(VkPhysicalDeviceQueueProperties));
2196 err = vkGetPhysicalDeviceQueueProperties(demo->gpu, queue_count, demo->queue_props);
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07002197 assert(!err);
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07002198 assert(queue_count >= 1);
2199
Mark Lobodzinskida9b1092015-04-16 11:44:05 -05002200 // Graphics queue and MemMgr queue can be separate.
2201 // TODO: Add support for separate queues, including synchronization,
Mark Lobodzinski6baaec72015-05-11 17:21:15 -05002202 // and appropriate tracking for QueueSubmit
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07002203 for (i = 0; i < queue_count; i++) {
Mark Lobodzinski6baaec72015-05-11 17:21:15 -05002204 if (demo->queue_props[i].queueFlags & VK_QUEUE_GRAPHICS_BIT)
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07002205 break;
2206 }
2207 assert(i < queue_count);
2208 demo->graphics_queue_node_index = i;
2209
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002210 err = vkGetDeviceQueue(demo->device, demo->graphics_queue_node_index,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002211 0, &demo->queue);
2212 assert(!err);
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002213
Jon Ashburnd7130cb2015-06-16 12:44:51 -06002214 // for now hardcode format till get WSI support
2215 demo->format = VK_FORMAT_B8G8R8A8_UNORM;
Ian Elliotte4602cd2015-04-21 16:41:02 -06002216
David Pinedo2bb7c932015-06-18 17:03:14 -06002217 demo->quit = false;
2218 demo->curFrame = 0;
Mark Lobodzinskieadf9982015-07-02 16:49:40 -06002219
2220 // Get Memory information and properties
2221 err = vkGetPhysicalDeviceMemoryProperties(demo->gpu, &demo->memory_properties);
2222 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002223}
2224
2225static void demo_init_connection(struct demo *demo)
2226{
Ian Elliott639ca472015-04-16 15:23:05 -06002227#ifndef _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002228 const xcb_setup_t *setup;
2229 xcb_screen_iterator_t iter;
2230 int scr;
2231
2232 demo->connection = xcb_connect(NULL, &scr);
Ian Elliott3979e282015-04-03 15:24:55 -06002233 if (demo->connection == NULL) {
2234 printf("Cannot find a compatible Vulkan installable client driver "
2235 "(ICD).\nExiting ...\n");
2236 fflush(stdout);
2237 exit(1);
2238 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002239
2240 setup = xcb_get_setup(demo->connection);
2241 iter = xcb_setup_roots_iterator(setup);
2242 while (scr-- > 0)
2243 xcb_screen_next(&iter);
2244
2245 demo->screen = iter.data;
Ian Elliott639ca472015-04-16 15:23:05 -06002246#endif // _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002247}
2248
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002249static void demo_init(struct demo *demo, int argc, char **argv)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002250{
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002251 vec3 eye = {0.0f, 3.0f, 5.0f};
2252 vec3 origin = {0, 0, 0};
Chia-I Wuae3b55d2015-04-22 14:56:17 +08002253 vec3 up = {0.0f, 1.0f, 0.0};
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002254
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002255 memset(demo, 0, sizeof(*demo));
David Pinedo2bb7c932015-06-18 17:03:14 -06002256 demo->frameCount = INT_MAX;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002257
Piers Daniell735ee532015-02-23 16:23:13 -07002258 for (int i = 1; i < argc; i++) {
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002259 if (strcmp(argv[i], "--use_staging") == 0) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002260 demo->use_staging_buffer = true;
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002261 continue;
Ian Elliott639ca472015-04-16 15:23:05 -06002262 }
Cody Northrop1fedb212015-05-28 11:27:16 -06002263 if (strcmp(argv[i], "--use_glsl") == 0) {
2264 demo->use_glsl = true;
2265 continue;
2266 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002267 if (strcmp(argv[i], "--validate") == 0) {
2268 demo->validate = true;
2269 continue;
2270 }
David Pinedo2bb7c932015-06-18 17:03:14 -06002271 if (strcmp(argv[i], "--c") == 0 &&
2272 demo->frameCount == INT_MAX &&
2273 i < argc-1 &&
2274 sscanf(argv[i+1],"%d", &demo->frameCount) == 1 &&
2275 demo->frameCount >= 0)
2276 {
2277 i++;
2278 continue;
2279 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002280
David Pinedo2bb7c932015-06-18 17:03:14 -06002281 fprintf(stderr, "Usage:\n %s [--use_staging] [--validate] [--c <framecount>]\n", APP_SHORT_NAME);
Ian Elliott639ca472015-04-16 15:23:05 -06002282 fflush(stderr);
2283 exit(1);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002284 }
2285
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002286 demo_init_connection(demo);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002287 demo_init_vk(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002288
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002289 demo->width = 500;
2290 demo->height = 500;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002291
2292 demo->spin_angle = 0.01f;
2293 demo->spin_increment = 0.01f;
2294 demo->pause = false;
2295
Piers Daniell735ee532015-02-23 16:23:13 -07002296 mat4x4_perspective(demo->projection_matrix, (float)degreesToRadians(45.0f), 1.0f, 0.1f, 100.0f);
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002297 mat4x4_look_at(demo->view_matrix, eye, origin, up);
2298 mat4x4_identity(demo->model_matrix);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002299}
2300
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002301
Ian Elliott639ca472015-04-16 15:23:05 -06002302#ifdef _WIN32
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002303extern int __getmainargs(
2304 int * _Argc,
2305 char *** _Argv,
2306 char *** _Env,
2307 int _DoWildCard,
2308 int * new_mode);
Ian Elliottf9cf78c2015-04-28 10:33:11 -06002309
Ian Elliotta748eaf2015-04-28 15:50:36 -06002310int WINAPI WinMain(HINSTANCE hInstance,
2311 HINSTANCE hPrevInstance,
2312 LPSTR pCmdLine,
2313 int nCmdShow)
Ian Elliott639ca472015-04-16 15:23:05 -06002314{
2315 MSG msg; // message
2316 bool done; // flag saying when app is complete
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002317 int argc;
2318 char** argv;
2319 char** env;
2320 int new_mode = 0;
Ian Elliott639ca472015-04-16 15:23:05 -06002321
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002322 __getmainargs(&argc,&argv,&env,0,&new_mode);
2323
2324 demo_init(&demo, argc, argv);
2325 demo.connection = hInstance;
2326 strncpy(demo.name, "cube", APP_NAME_STR_LEN);
Ian Elliott639ca472015-04-16 15:23:05 -06002327 demo_create_window(&demo);
2328
2329 demo_prepare(&demo);
2330
2331 done = false; //initialize loop condition variable
2332 /* main message loop*/
2333 while(!done)
2334 {
Ian Elliotta748eaf2015-04-28 15:50:36 -06002335 PeekMessage(&msg, NULL, 0, 0, PM_REMOVE);
Ian Elliott639ca472015-04-16 15:23:05 -06002336 if (msg.message == WM_QUIT) //check for a quit message
2337 {
2338 done = true; //if found, quit app
2339 }
2340 else
2341 {
2342 /* Translate and dispatch to event queue*/
2343 TranslateMessage(&msg);
2344 DispatchMessage(&msg);
2345 }
2346 }
2347
2348 demo_cleanup(&demo);
2349
Tony Barbourf9921732015-04-22 11:36:22 -06002350 return (int) msg.wParam;
Ian Elliott639ca472015-04-16 15:23:05 -06002351}
2352#else // _WIN32
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002353int main(int argc, char **argv)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002354{
2355 struct demo demo;
2356
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002357 demo_init(&demo, argc, argv);
Chia-I Wucbb564e2015-04-16 22:02:10 +08002358 demo_create_window(&demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002359
2360 demo_prepare(&demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002361 demo_run(&demo);
2362
2363 demo_cleanup(&demo);
2364
2365 return 0;
2366}
Ian Elliott639ca472015-04-16 15:23:05 -06002367#endif // _WIN32