blob: 796a14003a034ba5ab79395cfd4fc9ca39bfd896 [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>
40#include <vkDbg.h>
Chia-I Wucbb564e2015-04-16 22:02:10 +080041#include <vk_wsi_lunarg.h>
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060042
Cody Northropfe3d8bc2015-03-17 14:54:35 -060043#include "icd-spv.h"
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060044
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -060045#include "linmath.h"
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -060046#include <png.h>
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -060047
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060048#define DEMO_BUFFER_COUNT 2
49#define DEMO_TEXTURE_COUNT 1
Ian Elliott44e33f72015-04-28 10:52:52 -060050#define APP_SHORT_NAME "cube"
51#define APP_LONG_NAME "The Vulkan Cube Demo Program"
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060052
Tony Barbourfdc2d352015-04-22 09:02:32 -060053#if defined(NDEBUG) && defined(__GNUC__)
54#define U_ASSERT_ONLY __attribute__((unused))
55#else
56#define U_ASSERT_ONLY
57#endif
58
Ian Elliott07264132015-04-28 11:35:02 -060059#ifdef _WIN32
60#define ERR_EXIT(err_msg, err_class) \
61 do { \
62 MessageBox(NULL, err_msg, err_class, MB_OK); \
63 exit(1); \
64 } while (0)
65
66// NOTE: If the following values (copied from "loader_platform.h") change, they
67// need to change here as well:
68#define LAYER_NAMES_ENV "VK_LAYER_NAMES"
69#define LAYER_NAMES_REGISTRY_VALUE "VK_LAYER_NAMES"
70
71#else // _WIN32
72
73#define ERR_EXIT(err_msg, err_class) \
74 do { \
75 printf(err_msg); \
76 fflush(stdout); \
77 exit(1); \
78 } while (0)
79#endif // _WIN32
80
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -060081/*
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -070082 * structure to track all objects related to a texture.
83 */
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -060084struct texture_object {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -060085 VkSampler sampler;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -070086
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -060087 VkImage image;
88 VkImageLayout imageLayout;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -060089
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -070090 uint32_t num_mem;
Tony Barbour72304ef2015-04-16 15:59:00 -060091 VkDeviceMemory *mem;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -060092 VkImageView view;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -070093 int32_t tex_width, tex_height;
94};
95
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -060096static char *tex_files[] = {
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -060097 "lunarg-logo-256x256-solid.png"
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -060098};
99
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600100struct vkcube_vs_uniform {
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600101 // Must start with MVP
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600102 float mvp[4][4];
103 float position[12*3][4];
104 float color[12*3][4];
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600105};
106
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600107struct vktexcube_vs_uniform {
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600108 // Must start with MVP
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600109 float mvp[4][4];
110 float position[12*3][4];
111 float attr[12*3][4];
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600112};
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600113
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600114//--------------------------------------------------------------------------------------
115// Mesh and VertexFormat Data
116//--------------------------------------------------------------------------------------
117struct Vertex
118{
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600119 float posX, posY, posZ, posW; // Position data
120 float r, g, b, a; // Color
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600121};
122
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600123struct VertexPosTex
124{
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600125 float posX, posY, posZ, posW; // Position data
126 float u, v, s, t; // Texcoord
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600127};
128
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600129#define XYZ1(_x_, _y_, _z_) (_x_), (_y_), (_z_), 1.f
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600130#define UV(_u_, _v_) (_u_), (_v_), 0.f, 1.f
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600131
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600132static const float g_vertex_buffer_data[] = {
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800133 -1.0f,-1.0f,-1.0f, // -X side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600134 -1.0f,-1.0f, 1.0f,
135 -1.0f, 1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800136 -1.0f, 1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600137 -1.0f, 1.0f,-1.0f,
138 -1.0f,-1.0f,-1.0f,
139
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800140 -1.0f,-1.0f,-1.0f, // -Z side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600141 1.0f, 1.0f,-1.0f,
142 1.0f,-1.0f,-1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800143 -1.0f,-1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600144 -1.0f, 1.0f,-1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600145 1.0f, 1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600146
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800147 -1.0f,-1.0f,-1.0f, // -Y side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600148 1.0f,-1.0f,-1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600149 1.0f,-1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800150 -1.0f,-1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600151 1.0f,-1.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600152 -1.0f,-1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600153
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800154 -1.0f, 1.0f,-1.0f, // +Y side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600155 -1.0f, 1.0f, 1.0f,
156 1.0f, 1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800157 -1.0f, 1.0f,-1.0f,
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,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600160
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800161 1.0f, 1.0f,-1.0f, // +X side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600162 1.0f, 1.0f, 1.0f,
163 1.0f,-1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800164 1.0f,-1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600165 1.0f,-1.0f,-1.0f,
166 1.0f, 1.0f,-1.0f,
167
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800168 -1.0f, 1.0f, 1.0f, // +Z side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600169 -1.0f,-1.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600170 1.0f, 1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800171 -1.0f,-1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600172 1.0f,-1.0f, 1.0f,
173 1.0f, 1.0f, 1.0f,
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600174};
175
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600176static const float g_uv_buffer_data[] = {
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800177 0.0f, 0.0f, // -X side
178 1.0f, 0.0f,
179 1.0f, 1.0f,
180 1.0f, 1.0f,
181 0.0f, 1.0f,
182 0.0f, 0.0f,
183
184 1.0f, 0.0f, // -Z side
185 0.0f, 1.0f,
186 0.0f, 0.0f,
187 1.0f, 0.0f,
188 1.0f, 1.0f,
189 0.0f, 1.0f,
190
191 1.0f, 1.0f, // -Y side
192 1.0f, 0.0f,
193 0.0f, 0.0f,
194 1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600195 0.0f, 0.0f,
196 0.0f, 1.0f,
197
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800198 1.0f, 1.0f, // +Y side
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600199 0.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800200 0.0f, 0.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600201 1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600202 0.0f, 0.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600203 1.0f, 0.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600204
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800205 1.0f, 1.0f, // +X side
206 0.0f, 1.0f,
207 0.0f, 0.0f,
208 0.0f, 0.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600209 1.0f, 0.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600210 1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600211
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800212 0.0f, 1.0f, // +Z side
213 0.0f, 0.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600214 1.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600215 0.0f, 0.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800216 1.0f, 0.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600217 1.0f, 1.0f,
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600218};
219
220void dumpMatrix(const char *note, mat4x4 MVP)
221{
222 int i;
223
224 printf("%s: \n", note);
225 for (i=0; i<4; i++) {
226 printf("%f, %f, %f, %f\n", MVP[i][0], MVP[i][1], MVP[i][2], MVP[i][3]);
227 }
228 printf("\n");
229 fflush(stdout);
230}
231
232void dumpVec4(const char *note, vec4 vector)
233{
234 printf("%s: \n", note);
235 printf("%f, %f, %f, %f\n", vector[0], vector[1], vector[2], vector[3]);
236 printf("\n");
237 fflush(stdout);
238}
239
Tony Barbour3dddd5d2015-04-29 16:19:20 -0600240void VKAPI dbgFunc(
241 VK_DBG_MSG_TYPE msgType,
242 VkValidationLevel validationLevel,
243 VkObject srcObject,
244 size_t location,
245 int32_t msgCode,
246 const char* pMsg,
247 void* pUserData)
248{
249 char *message = (char *) malloc(strlen(pMsg)+100);
250
251 assert (message);
252
253 if (msgType == VK_DBG_MSG_ERROR) {
254 sprintf(message,"ERROR: Code %d : %s", msgCode, pMsg);
255 } else if (msgType == VK_DBG_MSG_WARNING) {
256 sprintf(message,"WARNING: Code %d : %s", msgCode, pMsg);
257 } else {
258 return;
259 }
260
261#ifdef _WIN32
262 MessageBox(NULL, message, "Alert", MB_OK);
263#else
264 printf("%s\n",message);
265 fflush(stdout);
266#endif
267 free(message);
268}
269
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600270struct demo {
Ian Elliott639ca472015-04-16 15:23:05 -0600271#ifdef _WIN32
272#define APP_NAME_STR_LEN 80
273 HINSTANCE connection; // hInstance - Windows Instance
274 char name[APP_NAME_STR_LEN]; // Name to put on the window/icon
275 HWND window; // hWnd - window handle
276#else // _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600277 xcb_connection_t *connection;
278 xcb_screen_t *screen;
Chia-I Wucbb564e2015-04-16 22:02:10 +0800279 xcb_window_t window;
280 xcb_intern_atom_reply_t *atom_wm_delete_window;
Ian Elliott639ca472015-04-16 15:23:05 -0600281#endif // _WIN32
Jon Ashburn8d26c062015-04-24 09:46:24 -0700282 bool prepared;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700283 bool use_staging_buffer;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600284
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600285 VkInstance inst;
Tony Barbour72304ef2015-04-16 15:59:00 -0600286 VkPhysicalDevice gpu;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600287 VkDevice device;
288 VkQueue queue;
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -0700289 uint32_t graphics_queue_node_index;
Tony Barbour72304ef2015-04-16 15:59:00 -0600290 VkPhysicalDeviceProperties *gpu_props;
291 VkPhysicalDeviceQueueProperties *queue_props;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600292
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600293 VkFramebuffer framebuffer;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600294 int width, height;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600295 VkFormat format;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600296
Chia-I Wucbb564e2015-04-16 22:02:10 +0800297 VkSwapChainWSI swap_chain;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600298 struct {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600299 VkImage image;
Tony Barbour72304ef2015-04-16 15:59:00 -0600300 VkDeviceMemory mem;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600301 VkCmdBuffer cmd;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600302
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600303 VkColorAttachmentView view;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600304 } buffers[DEMO_BUFFER_COUNT];
305
306 struct {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600307 VkFormat format;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600308
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600309 VkImage image;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600310 uint32_t num_mem;
Tony Barbour72304ef2015-04-16 15:59:00 -0600311 VkDeviceMemory *mem;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600312 VkDepthStencilView view;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600313 } depth;
314
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600315 struct texture_object textures[DEMO_TEXTURE_COUNT];
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600316
317 struct {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600318 VkBuffer buf;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600319 uint32_t num_mem;
Tony Barbour72304ef2015-04-16 15:59:00 -0600320 VkDeviceMemory *mem;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600321 VkBufferView view;
322 VkBufferViewAttachInfo attach;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600323 } uniform_data;
324
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600325 VkCmdBuffer cmd; // Buffer for initialization commands
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -0500326 VkPipelineLayout pipeline_layout;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600327 VkDescriptorSetLayout desc_layout;
328 VkPipeline pipeline;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600329
Courtney Goeltzenleuchter5e6d1e92015-04-10 16:24:50 -0600330 VkDynamicVpState viewport;
331 VkDynamicRsState raster;
332 VkDynamicCbState color_blend;
333 VkDynamicDsState depth_stencil;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600334
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600335 mat4x4 projection_matrix;
336 mat4x4 view_matrix;
337 mat4x4 model_matrix;
338
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600339 float spin_angle;
340 float spin_increment;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600341 bool pause;
342
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600343 VkDescriptorPool desc_pool;
344 VkDescriptorSet desc_set;
Chia-I Wu6a3c8972015-01-04 16:27:24 +0800345
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600346 bool quit;
Tony Barbour3dddd5d2015-04-29 16:19:20 -0600347 bool validate;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600348 uint32_t current_buffer;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600349};
350
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600351static void demo_flush_init_cmd(struct demo *demo)
352{
Tony Barbourfdc2d352015-04-22 09:02:32 -0600353 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600354
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600355 if (demo->cmd == VK_NULL_HANDLE)
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600356 return;
357
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600358 err = vkEndCommandBuffer(demo->cmd);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600359 assert(!err);
360
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600361 const VkCmdBuffer cmd_bufs[] = { demo->cmd };
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600362
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600363 err = vkQueueSubmit(demo->queue, 1, cmd_bufs, VK_NULL_HANDLE);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600364 assert(!err);
365
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600366 err = vkQueueWaitIdle(demo->queue);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600367 assert(!err);
368
Mike Stroyanebae8322015-04-17 12:36:38 -0600369 vkDestroyObject(demo->device, VK_OBJECT_TYPE_COMMAND_BUFFER, demo->cmd);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600370 demo->cmd = VK_NULL_HANDLE;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600371}
372
373static void demo_add_mem_refs(
374 struct demo *demo,
Tony Barbour72304ef2015-04-16 15:59:00 -0600375 int num_refs, VkDeviceMemory *mem)
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600376{
Courtney Goeltzenleuchter6b0caf12015-04-16 13:38:46 -0600377 vkQueueAddMemReferences(demo->queue, num_refs, mem);
Courtney Goeltzenleuchter1359b7a2015-04-02 16:25:42 -0600378}
379
380static void demo_remove_mem_refs(
381 struct demo *demo,
Tony Barbour72304ef2015-04-16 15:59:00 -0600382 int num_refs, VkDeviceMemory *mem)
Courtney Goeltzenleuchter1359b7a2015-04-02 16:25:42 -0600383{
Courtney Goeltzenleuchter6b0caf12015-04-16 13:38:46 -0600384 vkQueueRemoveMemReferences(demo->queue, num_refs, mem);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600385}
386
387static void demo_set_image_layout(
388 struct demo *demo,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600389 VkImage image,
390 VkImageLayout old_image_layout,
391 VkImageLayout new_image_layout)
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600392{
Tony Barbourfdc2d352015-04-22 09:02:32 -0600393 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600394
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600395 if (demo->cmd == VK_NULL_HANDLE) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600396 const VkCmdBufferCreateInfo cmd = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600397 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600398 .pNext = NULL,
399 .queueNodeIndex = demo->graphics_queue_node_index,
400 .flags = 0,
401 };
402
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600403 err = vkCreateCommandBuffer(demo->device, &cmd, &demo->cmd);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600404 assert(!err);
405
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600406 VkCmdBufferBeginInfo cmd_buf_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600407 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600408 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -0600409 .flags = VK_CMD_BUFFER_OPTIMIZE_SMALL_BATCH_BIT |
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600410 VK_CMD_BUFFER_OPTIMIZE_ONE_TIME_SUBMIT_BIT,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600411 };
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600412 err = vkBeginCommandBuffer(demo->cmd, &cmd_buf_info);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600413 }
414
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600415 VkImageMemoryBarrier image_memory_barrier = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600416 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600417 .pNext = NULL,
418 .outputMask = 0,
419 .inputMask = 0,
420 .oldLayout = old_image_layout,
421 .newLayout = new_image_layout,
422 .image = image,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600423 .subresourceRange = { VK_IMAGE_ASPECT_COLOR, 0, 1, 0, 0 }
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600424 };
425
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600426 if (new_image_layout == VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL) {
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600427 /* Make sure anything that was copying from this image has completed */
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -0600428 image_memory_barrier.inputMask = VK_MEMORY_INPUT_TRANSFER_BIT;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600429 }
430
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600431 if (new_image_layout == VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL) {
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600432 /* Make sure any Copy or CPU writes to image are flushed */
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -0600433 image_memory_barrier.outputMask = VK_MEMORY_OUTPUT_CPU_WRITE_BIT | VK_MEMORY_OUTPUT_TRANSFER_BIT;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600434 }
435
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600436 VkImageMemoryBarrier *pmemory_barrier = &image_memory_barrier;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600437
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600438 VkPipeEvent set_events[] = { VK_PIPE_EVENT_TOP_OF_PIPE };
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600439
Tony Barbour72304ef2015-04-16 15:59:00 -0600440 vkCmdPipelineBarrier(demo->cmd, VK_WAIT_EVENT_TOP_OF_PIPE, 1, set_events, 1, (const void **)&pmemory_barrier);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600441}
442
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600443static void demo_draw_build_cmd(struct demo *demo, VkCmdBuffer cmd_buf)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600444{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600445 const VkColorAttachmentBindInfo color_attachment = {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600446 .view = demo->buffers[demo->current_buffer].view,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600447 .layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600448 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600449 const VkDepthStencilBindInfo depth_stencil = {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600450 .view = demo->depth.view,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600451 .layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600452 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600453 const VkClearColor clear_color = {
Courtney Goeltzenleuchter374553c2015-04-03 16:35:32 -0600454 .color.floatColor = { 0.2f, 0.2f, 0.2f, 0.2f },
455 .useRawValue = false,
456 };
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600457 const float clear_depth = 1.0f;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600458 VkImageSubresourceRange clear_range;
459 VkCmdBufferBeginInfo cmd_buf_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600460 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO,
Courtney Goeltzenleuchter53968d82015-04-03 15:25:24 -0600461 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -0600462 .flags = VK_CMD_BUFFER_OPTIMIZE_SMALL_BATCH_BIT |
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600463 VK_CMD_BUFFER_OPTIMIZE_ONE_TIME_SUBMIT_BIT,
Jon Ashburn60ccfbe2014-12-31 17:08:35 -0700464 };
Tony Barbourfdc2d352015-04-22 09:02:32 -0600465 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600466 VkAttachmentLoadOp load_op = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
467 VkAttachmentStoreOp store_op = VK_ATTACHMENT_STORE_OP_DONT_CARE;
468 const VkFramebufferCreateInfo fb_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600469 .sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO,
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700470 .pNext = NULL,
471 .colorAttachmentCount = 1,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600472 .pColorAttachments = (VkColorAttachmentBindInfo*) &color_attachment,
473 .pDepthStencilAttachment = (VkDepthStencilBindInfo*) &depth_stencil,
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700474 .sampleCount = 1,
Mark Lobodzinskic06b7412015-01-27 13:24:03 -0600475 .width = demo->width,
476 .height = demo->height,
477 .layers = 1,
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700478 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600479 VkRenderPassCreateInfo rp_info;
480 VkRenderPassBegin rp_begin;
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700481
482 memset(&rp_info, 0 , sizeof(rp_info));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600483 err = vkCreateFramebuffer(demo->device, &fb_info, &rp_begin.framebuffer);
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700484 assert(!err);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600485 rp_info.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO;
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700486 rp_info.renderArea.extent.width = demo->width;
487 rp_info.renderArea.extent.height = demo->height;
Courtney Goeltzenleuchter53968d82015-04-03 15:25:24 -0600488 rp_info.colorAttachmentCount = fb_info.colorAttachmentCount;
489 rp_info.pColorFormats = &demo->format;
490 rp_info.pColorLayouts = &color_attachment.layout;
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700491 rp_info.pColorLoadOps = &load_op;
492 rp_info.pColorStoreOps = &store_op;
Courtney Goeltzenleuchter53968d82015-04-03 15:25:24 -0600493 rp_info.pColorLoadClearValues = &clear_color;
Tony Barbour72304ef2015-04-16 15:59:00 -0600494 rp_info.depthStencilFormat = VK_FORMAT_D16_UNORM;
Courtney Goeltzenleuchter53968d82015-04-03 15:25:24 -0600495 rp_info.depthStencilLayout = depth_stencil.layout;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600496 rp_info.depthLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
Courtney Goeltzenleuchter53968d82015-04-03 15:25:24 -0600497 rp_info.depthLoadClearValue = clear_depth;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600498 rp_info.depthStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
499 rp_info.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
Courtney Goeltzenleuchter53968d82015-04-03 15:25:24 -0600500 rp_info.stencilLoadClearValue = 0;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600501 rp_info.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
502 err = vkCreateRenderPass(demo->device, &rp_info, &rp_begin.renderPass);
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700503 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600504
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600505 err = vkBeginCommandBuffer(cmd_buf, &cmd_buf_info);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600506 assert(!err);
507
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600508 vkCmdBindPipeline(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600509 demo->pipeline);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600510 vkCmdBindDescriptorSets(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS,
Cody Northropfb5185a2015-04-16 13:41:56 -0600511 0, 1, &demo->desc_set, 0, NULL);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600512
Tony Barbour72304ef2015-04-16 15:59:00 -0600513 vkCmdBindDynamicStateObject(cmd_buf, VK_STATE_BIND_POINT_VIEWPORT, demo->viewport);
514 vkCmdBindDynamicStateObject(cmd_buf, VK_STATE_BIND_POINT_RASTER, demo->raster);
515 vkCmdBindDynamicStateObject(cmd_buf, VK_STATE_BIND_POINT_COLOR_BLEND,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600516 demo->color_blend);
Tony Barbour72304ef2015-04-16 15:59:00 -0600517 vkCmdBindDynamicStateObject(cmd_buf, VK_STATE_BIND_POINT_DEPTH_STENCIL,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600518 demo->depth_stencil);
519
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600520 vkCmdBeginRenderPass(cmd_buf, &rp_begin);
521 clear_range.aspect = VK_IMAGE_ASPECT_COLOR;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600522 clear_range.baseMipLevel = 0;
523 clear_range.mipLevels = 1;
524 clear_range.baseArraySlice = 0;
525 clear_range.arraySize = 1;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600526 vkCmdClearColorImage(cmd_buf,
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600527 demo->buffers[demo->current_buffer].image,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600528 VK_IMAGE_LAYOUT_CLEAR_OPTIMAL,
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600529 clear_color, 1, &clear_range);
530
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600531 clear_range.aspect = VK_IMAGE_ASPECT_DEPTH;
532 vkCmdClearDepthStencil(cmd_buf, demo->depth.image,
533 VK_IMAGE_LAYOUT_CLEAR_OPTIMAL,
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600534 clear_depth, 0, 1, &clear_range);
535
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600536 vkCmdDraw(cmd_buf, 0, 12 * 3, 0, 1);
537 vkCmdEndRenderPass(cmd_buf, rp_begin.renderPass);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600538
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600539 err = vkEndCommandBuffer(cmd_buf);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600540 assert(!err);
Courtney Goeltzenleuchter94901dc2015-02-25 17:53:18 -0700541
Mike Stroyanebae8322015-04-17 12:36:38 -0600542 vkDestroyObject(demo->device, VK_OBJECT_TYPE_RENDER_PASS, rp_begin.renderPass);
543 vkDestroyObject(demo->device, VK_OBJECT_TYPE_FRAMEBUFFER, rp_begin.framebuffer);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600544}
545
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600546
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600547void demo_update_data_buffer(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600548{
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600549 mat4x4 MVP, Model, VP;
550 int matrixSize = sizeof(MVP);
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600551 uint8_t *pData;
Tony Barbourfdc2d352015-04-22 09:02:32 -0600552 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600553
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600554 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600555
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600556 // Rotate 22.5 degrees around the Y axis
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600557 mat4x4_dup(Model, demo->model_matrix);
Piers Daniell735ee532015-02-23 16:23:13 -0700558 mat4x4_rotate(demo->model_matrix, Model, 0.0f, 1.0f, 0.0f, (float)degreesToRadians(demo->spin_angle));
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600559 mat4x4_mul(MVP, VP, demo->model_matrix);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600560
Jon Ashburnae7c21c2015-01-19 15:00:26 -0700561 assert(demo->uniform_data.num_mem == 1);
Mike Stroyanebae8322015-04-17 12:36:38 -0600562 err = vkMapMemory(demo->device, demo->uniform_data.mem[0], 0, 0, 0, (void **) &pData);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600563 assert(!err);
564
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600565 memcpy(pData, (const void*) &MVP[0][0], matrixSize);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600566
Mike Stroyanebae8322015-04-17 12:36:38 -0600567 err = vkUnmapMemory(demo->device, demo->uniform_data.mem[0]);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600568 assert(!err);
569}
570
571static void demo_draw(struct demo *demo)
572{
Chia-I Wucbb564e2015-04-16 22:02:10 +0800573 const VkPresentInfoWSI present = {
574 .sType = VK_STRUCTURE_TYPE_PRESENT_INFO_WSI,
575 .pNext = NULL,
576 .image = demo->buffers[demo->current_buffer].image,
577 .flipInterval = 0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600578 };
Tony Barbourfdc2d352015-04-22 09:02:32 -0600579 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600580
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600581 err = vkQueueSubmit(demo->queue, 1, &demo->buffers[demo->current_buffer].cmd,
582 VK_NULL_HANDLE);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600583 assert(!err);
584
Chia-I Wucbb564e2015-04-16 22:02:10 +0800585 err = vkQueuePresentWSI(demo->queue, &present);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600586 assert(!err);
587
588 demo->current_buffer = (demo->current_buffer + 1) % DEMO_BUFFER_COUNT;
Chia-I Wucbb564e2015-04-16 22:02:10 +0800589
590 err = vkQueueWaitIdle(demo->queue);
591 assert(err == VK_SUCCESS);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600592}
593
594static void demo_prepare_buffers(struct demo *demo)
595{
Chia-I Wucbb564e2015-04-16 22:02:10 +0800596 const VkSwapChainCreateInfoWSI swap_chain = {
597 .sType = VK_STRUCTURE_TYPE_SWAP_CHAIN_CREATE_INFO_WSI,
598 .pNext = NULL,
599 .pNativeWindowSystemHandle = demo->connection,
600 .pNativeWindowHandle = (void *) (intptr_t) demo->window,
601 .imageCount = DEMO_BUFFER_COUNT,
602 .imageFormat = demo->format,
603 .imageExtent = {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600604 .width = demo->width,
605 .height = demo->height,
606 },
Chia-I Wucbb564e2015-04-16 22:02:10 +0800607 .imageArraySize = 1,
608 .imageUsageFlags = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600609 };
Chia-I Wucbb564e2015-04-16 22:02:10 +0800610 VkSwapChainImageInfoWSI images[DEMO_BUFFER_COUNT];
611 size_t images_size = sizeof(images);
Tony Barbourfdc2d352015-04-22 09:02:32 -0600612 VkResult U_ASSERT_ONLY err;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600613 uint32_t i;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600614
Chia-I Wucbb564e2015-04-16 22:02:10 +0800615 err = vkCreateSwapChainWSI(demo->device, &swap_chain, &demo->swap_chain);
616 assert(!err);
617
618 err = vkGetSwapChainInfoWSI(demo->swap_chain,
619 VK_SWAP_CHAIN_INFO_TYPE_PERSISTENT_IMAGES_WSI,
620 &images_size, images);
621 assert(!err && images_size == sizeof(images));
622
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600623 for (i = 0; i < DEMO_BUFFER_COUNT; i++) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600624 VkColorAttachmentViewCreateInfo color_attachment_view = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600625 .sType = VK_STRUCTURE_TYPE_COLOR_ATTACHMENT_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600626 .pNext = NULL,
627 .format = demo->format,
628 .mipLevel = 0,
629 .baseArraySlice = 0,
630 .arraySize = 1,
631 };
632
Chia-I Wucbb564e2015-04-16 22:02:10 +0800633 demo->buffers[i].image = images[i].image;
634 demo->buffers[i].mem = images[i].memory;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600635
Courtney Goeltzenleuchter2c2fbbf2015-04-07 17:13:38 -0600636 demo_add_mem_refs(demo, 1, &demo->buffers[i].mem);
Courtney Goeltzenleuchter1359b7a2015-04-02 16:25:42 -0600637
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600638 demo_set_image_layout(demo, demo->buffers[i].image,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600639 VK_IMAGE_LAYOUT_UNDEFINED,
640 VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600641
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600642 color_attachment_view.image = demo->buffers[i].image;
643
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600644 err = vkCreateColorAttachmentView(demo->device,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600645 &color_attachment_view, &demo->buffers[i].view);
646 assert(!err);
647 }
648}
649
650static void demo_prepare_depth(struct demo *demo)
651{
Tony Barbour72304ef2015-04-16 15:59:00 -0600652 const VkFormat depth_format = VK_FORMAT_D16_UNORM;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600653 const VkImageCreateInfo image = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600654 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600655 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -0600656 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600657 .format = depth_format,
658 .extent = { demo->width, demo->height, 1 },
659 .mipLevels = 1,
660 .arraySize = 1,
661 .samples = 1,
Tony Barbour72304ef2015-04-16 15:59:00 -0600662 .tiling = VK_IMAGE_TILING_OPTIMAL,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600663 .usage = VK_IMAGE_USAGE_DEPTH_STENCIL_BIT,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600664 .flags = 0,
665 };
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -0600666 VkMemoryAllocInfo mem_alloc = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600667 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
Mark Lobodzinskia6907f72015-04-16 08:52:00 -0500668 .pNext = NULL,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600669 .allocationSize = 0,
Tony Barbour72304ef2015-04-16 15:59:00 -0600670 .memProps = VK_MEMORY_PROPERTY_DEVICE_ONLY,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600671 .memPriority = VK_MEMORY_PRIORITY_NORMAL,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600672 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600673 VkDepthStencilViewCreateInfo view = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600674 .sType = VK_STRUCTURE_TYPE_DEPTH_STENCIL_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600675 .pNext = NULL,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600676 .image = VK_NULL_HANDLE,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600677 .mipLevel = 0,
678 .baseArraySlice = 0,
679 .arraySize = 1,
680 .flags = 0,
681 };
Mike Stroyanebae8322015-04-17 12:36:38 -0600682
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600683 VkMemoryRequirements *mem_reqs;
684 size_t mem_reqs_size = sizeof(VkMemoryRequirements);
Tony Barbourfdc2d352015-04-22 09:02:32 -0600685 VkResult U_ASSERT_ONLY err;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600686 uint32_t num_allocations = 0;
687 size_t num_alloc_size = sizeof(num_allocations);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600688
689 demo->depth.format = depth_format;
690
691 /* create image */
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600692 err = vkCreateImage(demo->device, &image,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600693 &demo->depth.image);
694 assert(!err);
695
Mike Stroyanebae8322015-04-17 12:36:38 -0600696 err = vkGetObjectInfo(demo->device,
697 VK_OBJECT_TYPE_IMAGE, demo->depth.image,
698 VK_OBJECT_INFO_TYPE_MEMORY_ALLOCATION_COUNT,
699 &num_alloc_size, &num_allocations);
Jon Ashburnb2a66652015-01-16 09:37:43 -0700700 assert(!err && num_alloc_size == sizeof(num_allocations));
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600701 mem_reqs = malloc(num_allocations * sizeof(VkMemoryRequirements));
Tony Barbour72304ef2015-04-16 15:59:00 -0600702 demo->depth.mem = malloc(num_allocations * sizeof(VkDeviceMemory));
Jon Ashburnb2a66652015-01-16 09:37:43 -0700703 demo->depth.num_mem = num_allocations;
Mike Stroyanebae8322015-04-17 12:36:38 -0600704 err = vkGetObjectInfo(demo->device,
705 VK_OBJECT_TYPE_IMAGE, demo->depth.image,
Tony Barbour72304ef2015-04-16 15:59:00 -0600706 VK_OBJECT_INFO_TYPE_MEMORY_REQUIREMENTS,
Jon Ashburnb2a66652015-01-16 09:37:43 -0700707 &mem_reqs_size, mem_reqs);
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600708 assert(!err && mem_reqs_size == num_allocations * sizeof(VkMemoryRequirements));
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600709 for (uint32_t i = 0; i < num_allocations; i ++) {
Jon Ashburnb2a66652015-01-16 09:37:43 -0700710 mem_alloc.allocationSize = mem_reqs[i].size;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600711
Jon Ashburnb2a66652015-01-16 09:37:43 -0700712 /* allocate memory */
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600713 err = vkAllocMemory(demo->device, &mem_alloc,
Jon Ashburnb2a66652015-01-16 09:37:43 -0700714 &(demo->depth.mem[i]));
715 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600716
Jon Ashburnb2a66652015-01-16 09:37:43 -0700717 /* bind memory */
Mike Stroyanebae8322015-04-17 12:36:38 -0600718 err = vkQueueBindObjectMemory(demo->queue,
719 VK_OBJECT_TYPE_IMAGE, demo->depth.image,
720 i, demo->depth.mem[i], 0);
Jon Ashburnb2a66652015-01-16 09:37:43 -0700721 assert(!err);
722 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600723
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600724 demo_set_image_layout(demo, demo->depth.image,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600725 VK_IMAGE_LAYOUT_UNDEFINED,
726 VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600727
Courtney Goeltzenleuchter2c2fbbf2015-04-07 17:13:38 -0600728 demo_add_mem_refs(demo, demo->depth.num_mem, demo->depth.mem);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600729
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600730 /* create image view */
731 view.image = demo->depth.image;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600732 err = vkCreateDepthStencilView(demo->device, &view,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600733 &demo->depth.view);
734 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600735}
736
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600737/** loadTexture
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -0600738 * loads a png file into an memory object, using cstdio , libpng.
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600739 *
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -0600740 * \param demo : Needed to access VK calls
741 * \param filename : the png file to be loaded
742 * \param width : width of png, to be updated as a side effect of this function
743 * \param height : height of png, to be updated as a side effect of this function
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600744 *
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -0600745 * \return bool : an opengl texture id. true if successful?,
746 * should be validated by the client of this function.
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600747 *
748 * Source: http://en.wikibooks.org/wiki/OpenGL_Programming/Intermediate/Textures
749 * Modified to copy image to memory
750 *
751 */
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700752bool loadTexture(const char *filename, uint8_t *rgba_data,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600753 VkSubresourceLayout *layout,
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600754 int32_t *width, int32_t *height)
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600755{
756 //header for testing if it is a png
757 png_byte header[8];
Tony Barbourf9921732015-04-22 11:36:22 -0600758 int is_png, bit_depth, color_type, rowbytes;
759 size_t retval;
Ian Elliott1e42dff2015-02-13 14:29:21 -0700760 png_uint_32 i, twidth, theight;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600761 png_structp png_ptr;
762 png_infop info_ptr, end_info;
763 png_byte *image_data;
764 png_bytep *row_pointers;
765
766 //open file as binary
767 FILE *fp = fopen(filename, "rb");
768 if (!fp) {
769 return false;
770 }
771
772 //read the header
Tony Barbourfdc2d352015-04-22 09:02:32 -0600773 retval = fread(header, 1, 8, fp);
774 if (retval != 8) {
775 fclose(fp);
776 return false;
777 }
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600778
779 //test if png
780 is_png = !png_sig_cmp(header, 0, 8);
781 if (!is_png) {
782 fclose(fp);
783 return false;
784 }
785
786 //create png struct
787 png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL,
788 NULL, NULL);
789 if (!png_ptr) {
790 fclose(fp);
791 return (false);
792 }
793
794 //create png info struct
795 info_ptr = png_create_info_struct(png_ptr);
796 if (!info_ptr) {
797 png_destroy_read_struct(&png_ptr, (png_infopp) NULL, (png_infopp) NULL);
798 fclose(fp);
799 return (false);
800 }
801
802 //create png info struct
803 end_info = png_create_info_struct(png_ptr);
804 if (!end_info) {
805 png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp) NULL);
806 fclose(fp);
807 return (false);
808 }
809
810 //png error stuff, not sure libpng man suggests this.
811 if (setjmp(png_jmpbuf(png_ptr))) {
812 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
813 fclose(fp);
814 return (false);
815 }
816
817 //init png reading
818 png_init_io(png_ptr, fp);
819
820 //let libpng know you already read the first 8 bytes
821 png_set_sig_bytes(png_ptr, 8);
822
823 // read all the info up to the image data
824 png_read_info(png_ptr, info_ptr);
825
826 // get info about png
827 png_get_IHDR(png_ptr, info_ptr, &twidth, &theight, &bit_depth, &color_type,
828 NULL, NULL, NULL);
829
830 //update width and height based on png info
831 *width = twidth;
832 *height = theight;
833
834 // Require that incoming texture be 8bits per color component
835 // and 4 components (RGBA).
836 if (png_get_bit_depth(png_ptr, info_ptr) != 8 ||
837 png_get_channels(png_ptr, info_ptr) != 4) {
838 return false;
839 }
840
841 if (rgba_data == NULL) {
842 // If data pointer is null, we just want the width & height
843 // clean up memory and close stuff
844 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
845 fclose(fp);
846
847 return true;
848 }
849
850 // Update the png info struct.
851 png_read_update_info(png_ptr, info_ptr);
852
853 // Row size in bytes.
854 rowbytes = png_get_rowbytes(png_ptr, info_ptr);
855
856 // Allocate the image_data as a big block, to be given to opengl
857 image_data = (png_byte *)malloc(rowbytes * theight * sizeof(png_byte));
858 if (!image_data) {
859 //clean up memory and close stuff
860 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
861 fclose(fp);
862 return false;
863 }
864
865 // row_pointers is for pointing to image_data for reading the png with libpng
866 row_pointers = (png_bytep *)malloc(theight * sizeof(png_bytep));
867 if (!row_pointers) {
868 //clean up memory and close stuff
869 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
870 // delete[] image_data;
871 fclose(fp);
872 return false;
873 }
874 // set the individual row_pointers to point at the correct offsets of image_data
875 for (i = 0; i < theight; ++i)
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700876 row_pointers[theight - 1 - i] = rgba_data + i * layout->rowPitch;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600877
878 // read the png into image_data through row_pointers
879 png_read_image(png_ptr, row_pointers);
880
881 // clean up memory and close stuff
882 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
883 free(row_pointers);
884 free(image_data);
885 fclose(fp);
886
887 return true;
888}
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600889
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700890static void demo_prepare_texture_image(struct demo *demo,
891 const char *filename,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600892 struct texture_object *tex_obj,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600893 VkImageTiling tiling,
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -0600894 VkImageUsageFlags usage,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600895 VkFlags mem_props)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600896{
Tony Barbour72304ef2015-04-16 15:59:00 -0600897 const VkFormat tex_format = VK_FORMAT_B8G8R8A8_UNORM;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600898 int32_t tex_width;
899 int32_t tex_height;
Tony Barbourfdc2d352015-04-22 09:02:32 -0600900 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700901
David Pinedo30bd71d2015-04-23 08:16:57 -0600902 if (!loadTexture(filename, NULL, NULL, &tex_width, &tex_height))
903 {
904 printf("Failed to load textures\n");
905 fflush(stdout);
906 exit(1);
907 }
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700908
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600909 tex_obj->tex_width = tex_width;
910 tex_obj->tex_height = tex_height;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700911
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600912 const VkImageCreateInfo image_create_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600913 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700914 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -0600915 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700916 .format = tex_format,
917 .extent = { tex_width, tex_height, 1 },
918 .mipLevels = 1,
919 .arraySize = 1,
920 .samples = 1,
921 .tiling = tiling,
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -0600922 .usage = usage,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700923 .flags = 0,
924 };
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -0600925 VkMemoryAllocInfo mem_alloc = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600926 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
Mark Lobodzinskia6907f72015-04-16 08:52:00 -0500927 .pNext = NULL,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700928 .allocationSize = 0,
929 .memProps = mem_props,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600930 .memPriority = VK_MEMORY_PRIORITY_NORMAL,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700931 };
932
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600933 VkMemoryRequirements *mem_reqs;
934 size_t mem_reqs_size = sizeof(VkMemoryRequirements);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700935 uint32_t num_allocations = 0;
936 size_t num_alloc_size = sizeof(num_allocations);
937
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600938 err = vkCreateImage(demo->device, &image_create_info,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600939 &tex_obj->image);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700940 assert(!err);
941
Mike Stroyanebae8322015-04-17 12:36:38 -0600942 err = vkGetObjectInfo(demo->device,
943 VK_OBJECT_TYPE_IMAGE, tex_obj->image,
Tony Barbour72304ef2015-04-16 15:59:00 -0600944 VK_OBJECT_INFO_TYPE_MEMORY_ALLOCATION_COUNT,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700945 &num_alloc_size, &num_allocations);
946 assert(!err && num_alloc_size == sizeof(num_allocations));
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600947 mem_reqs = malloc(num_allocations * sizeof(VkMemoryRequirements));
Tony Barbour72304ef2015-04-16 15:59:00 -0600948 tex_obj->mem = malloc(num_allocations * sizeof(VkDeviceMemory));
Mike Stroyanebae8322015-04-17 12:36:38 -0600949 err = vkGetObjectInfo(demo->device,
950 VK_OBJECT_TYPE_IMAGE, tex_obj->image,
Tony Barbour72304ef2015-04-16 15:59:00 -0600951 VK_OBJECT_INFO_TYPE_MEMORY_REQUIREMENTS,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700952 &mem_reqs_size, mem_reqs);
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600953 assert(!err && mem_reqs_size == num_allocations * sizeof(VkMemoryRequirements));
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700954 for (uint32_t j = 0; j < num_allocations; j ++) {
Piers Daniell735ee532015-02-23 16:23:13 -0700955 mem_alloc.allocationSize = mem_reqs[j].size;
956
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700957 /* allocate memory */
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600958 err = vkAllocMemory(demo->device, &mem_alloc,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600959 &(tex_obj->mem[j]));
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700960 assert(!err);
961
962 /* bind memory */
Mike Stroyanebae8322015-04-17 12:36:38 -0600963 err = vkQueueBindObjectMemory(demo->queue,
964 VK_OBJECT_TYPE_IMAGE, tex_obj->image,
965 j, tex_obj->mem[j], 0);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700966 assert(!err);
967 }
968 free(mem_reqs);
969 mem_reqs = NULL;
970
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600971 tex_obj->num_mem = num_allocations;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700972
Tony Barbour72304ef2015-04-16 15:59:00 -0600973 if (mem_props & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600974 const VkImageSubresource subres = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600975 .aspect = VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700976 .mipLevel = 0,
977 .arraySlice = 0,
978 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600979 VkSubresourceLayout layout;
980 size_t layout_size = sizeof(VkSubresourceLayout);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700981 void *data;
982
Mike Stroyanebae8322015-04-17 12:36:38 -0600983 err = vkGetImageSubresourceInfo(demo->device, tex_obj->image, &subres,
Tony Barbour72304ef2015-04-16 15:59:00 -0600984 VK_SUBRESOURCE_INFO_TYPE_LAYOUT,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700985 &layout_size, &layout);
986 assert(!err && layout_size == sizeof(layout));
987 /* Linear texture must be within a single memory object */
988 assert(num_allocations == 1);
989
Mike Stroyanebae8322015-04-17 12:36:38 -0600990 err = vkMapMemory(demo->device, tex_obj->mem[0], 0, 0, 0, &data);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700991 assert(!err);
992
993 if (!loadTexture(filename, data, &layout, &tex_width, &tex_height)) {
994 fprintf(stderr, "Error loading texture: %s\n", filename);
995 }
996
Mike Stroyanebae8322015-04-17 12:36:38 -0600997 err = vkUnmapMemory(demo->device, tex_obj->mem[0]);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700998 assert(!err);
999 }
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001000
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001001 tex_obj->imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001002 demo_set_image_layout(demo, tex_obj->image,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001003 VK_IMAGE_LAYOUT_UNDEFINED,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001004 tex_obj->imageLayout);
1005 /* 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 -07001006}
1007
Mark Lobodzinskida9b1092015-04-16 11:44:05 -05001008static void demo_destroy_texture_image(struct demo *demo, struct texture_object *tex_objs)
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001009{
1010 /* clean up staging resources */
1011 for (uint32_t j = 0; j < tex_objs->num_mem; j ++) {
Mike Stroyanebae8322015-04-17 12:36:38 -06001012 vkQueueBindObjectMemory(demo->queue,
1013 VK_OBJECT_TYPE_IMAGE, tex_objs->image, j, VK_NULL_HANDLE, 0);
1014 vkFreeMemory(demo->device, tex_objs->mem[j]);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001015 }
1016
1017 free(tex_objs->mem);
Mike Stroyanebae8322015-04-17 12:36:38 -06001018 vkDestroyObject(demo->device, VK_OBJECT_TYPE_IMAGE, tex_objs->image);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001019}
1020
1021static void demo_prepare_textures(struct demo *demo)
1022{
Tony Barbour72304ef2015-04-16 15:59:00 -06001023 const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001024 VkFormatProperties props;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001025 size_t size = sizeof(props);
Tony Barbourfdc2d352015-04-22 09:02:32 -06001026 VkResult U_ASSERT_ONLY err;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001027 uint32_t i;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001028
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001029 err = vkGetFormatInfo(demo->device, tex_format,
Tony Barbour72304ef2015-04-16 15:59:00 -06001030 VK_FORMAT_INFO_TYPE_PROPERTIES,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001031 &size, &props);
1032 assert(!err);
1033
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001034 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001035
Tony Barbour72304ef2015-04-16 15:59:00 -06001036 if (props.linearTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT && !demo->use_staging_buffer) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001037 /* Device can texture using linear textures */
1038 demo_prepare_texture_image(demo, tex_files[i], &demo->textures[i],
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001039 VK_IMAGE_TILING_LINEAR, VK_IMAGE_USAGE_SAMPLED_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
Tony Barbour72304ef2015-04-16 15:59:00 -06001040 } else if (props.optimalTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001041 /* Must use staging buffer to copy linear texture to optimized */
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001042 struct texture_object staging_texture;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001043
1044 memset(&staging_texture, 0, sizeof(staging_texture));
1045 demo_prepare_texture_image(demo, tex_files[i], &staging_texture,
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001046 VK_IMAGE_TILING_LINEAR, VK_IMAGE_USAGE_TRANSFER_SOURCE_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001047
1048 demo_prepare_texture_image(demo, tex_files[i], &demo->textures[i],
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001049 VK_IMAGE_TILING_OPTIMAL,
1050 (VK_IMAGE_USAGE_TRANSFER_DESTINATION_BIT | VK_IMAGE_USAGE_SAMPLED_BIT),
1051 VK_MEMORY_PROPERTY_DEVICE_ONLY);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001052
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001053 demo_set_image_layout(demo, staging_texture.image,
1054 staging_texture.imageLayout,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001055 VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001056
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001057 demo_set_image_layout(demo, demo->textures[i].image,
1058 demo->textures[i].imageLayout,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001059 VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001060
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001061 VkImageCopy copy_region = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001062 .srcSubresource = { VK_IMAGE_ASPECT_COLOR, 0, 0 },
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001063 .srcOffset = { 0, 0, 0 },
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001064 .destSubresource = { VK_IMAGE_ASPECT_COLOR, 0, 0 },
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001065 .destOffset = { 0, 0, 0 },
1066 .extent = { staging_texture.tex_width, staging_texture.tex_height, 1 },
1067 };
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001068 vkCmdCopyImage(demo->cmd,
1069 staging_texture.image, VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL,
1070 demo->textures[i].image, VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL,
Courtney Goeltzenleuchter8e89a312015-03-25 11:25:10 -06001071 1, &copy_region);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001072
Courtney Goeltzenleuchter2c2fbbf2015-04-07 17:13:38 -06001073 demo_add_mem_refs(demo, staging_texture.num_mem, staging_texture.mem);
1074 demo_add_mem_refs(demo, demo->textures[i].num_mem, demo->textures[i].mem);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001075
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001076 demo_set_image_layout(demo, demo->textures[i].image,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001077 VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001078 demo->textures[i].imageLayout);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001079
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001080 demo_flush_init_cmd(demo);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001081
Courtney Goeltzenleuchter1359b7a2015-04-02 16:25:42 -06001082 demo_remove_mem_refs(demo, staging_texture.num_mem, staging_texture.mem);
Courtney Goeltzenleuchterf3aeb2b2015-04-21 09:30:03 -06001083 demo_destroy_texture_image(demo, &staging_texture);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001084 } else {
Tony Barbour72304ef2015-04-16 15:59:00 -06001085 /* Can't support VK_FORMAT_B8G8R8A8_UNORM !? */
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001086 assert(!"No support for tB8G8R8A8_UNORM as texture image format");
1087 }
1088
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001089 const VkSamplerCreateInfo sampler = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001090 .sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001091 .pNext = NULL,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001092 .magFilter = VK_TEX_FILTER_NEAREST,
1093 .minFilter = VK_TEX_FILTER_NEAREST,
Tony Barbour72304ef2015-04-16 15:59:00 -06001094 .mipMode = VK_TEX_MIPMAP_MODE_BASE,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001095 .addressU = VK_TEX_ADDRESS_CLAMP,
1096 .addressV = VK_TEX_ADDRESS_CLAMP,
1097 .addressW = VK_TEX_ADDRESS_CLAMP,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001098 .mipLodBias = 0.0f,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001099 .maxAnisotropy = 1,
Tony Barbour72304ef2015-04-16 15:59:00 -06001100 .compareOp = VK_COMPARE_OP_NEVER,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001101 .minLod = 0.0f,
1102 .maxLod = 0.0f,
Tony Barbour72304ef2015-04-16 15:59:00 -06001103 .borderColor = VK_BORDER_COLOR_OPAQUE_WHITE,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001104 };
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001105
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001106 VkImageViewCreateInfo view = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001107 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001108 .pNext = NULL,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001109 .image = VK_NULL_HANDLE,
Tony Barbour72304ef2015-04-16 15:59:00 -06001110 .viewType = VK_IMAGE_VIEW_TYPE_2D,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001111 .format = tex_format,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001112 .channels = { VK_CHANNEL_SWIZZLE_R,
1113 VK_CHANNEL_SWIZZLE_G,
1114 VK_CHANNEL_SWIZZLE_B,
1115 VK_CHANNEL_SWIZZLE_A, },
1116 .subresourceRange = { VK_IMAGE_ASPECT_COLOR, 0, 1, 0, 1 },
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001117 .minLod = 0.0f,
1118 };
Jon Ashburnb2a66652015-01-16 09:37:43 -07001119
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001120 /* create sampler */
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001121 err = vkCreateSampler(demo->device, &sampler,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001122 &demo->textures[i].sampler);
1123 assert(!err);
1124
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001125 /* create image view */
1126 view.image = demo->textures[i].image;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001127 err = vkCreateImageView(demo->device, &view,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001128 &demo->textures[i].view);
1129 assert(!err);
1130 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001131}
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001132
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001133void demo_prepare_cube_data_buffer(struct demo *demo)
1134{
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -06001135 VkBufferCreateInfo buf_info;
1136 VkBufferViewCreateInfo view_info;
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -06001137 VkMemoryAllocInfo alloc_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001138 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
Mark Lobodzinskia6907f72015-04-16 08:52:00 -05001139 .pNext = NULL,
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001140 .allocationSize = 0,
Tony Barbour72304ef2015-04-16 15:59:00 -06001141 .memProps = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001142 .memPriority = VK_MEMORY_PRIORITY_NORMAL,
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001143 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001144 VkMemoryRequirements *mem_reqs;
1145 size_t mem_reqs_size = sizeof(VkMemoryRequirements);
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001146 uint32_t num_allocations = 0;
1147 size_t num_alloc_size = sizeof(num_allocations);
1148 uint8_t *pData;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001149 int i;
1150 mat4x4 MVP, VP;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001151 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001152 struct vktexcube_vs_uniform data;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001153
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001154 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001155 mat4x4_mul(MVP, VP, demo->model_matrix);
1156 memcpy(data.mvp, MVP, sizeof(MVP));
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001157// dumpMatrix("MVP", MVP);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001158
1159 for (i=0; i<12*3; i++) {
1160 data.position[i][0] = g_vertex_buffer_data[i*3];
1161 data.position[i][1] = g_vertex_buffer_data[i*3+1];
1162 data.position[i][2] = g_vertex_buffer_data[i*3+2];
1163 data.position[i][3] = 1.0f;
1164 data.attr[i][0] = g_uv_buffer_data[2*i];
1165 data.attr[i][1] = g_uv_buffer_data[2*i + 1];
1166 data.attr[i][2] = 0;
1167 data.attr[i][3] = 0;
1168 }
1169
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001170 memset(&buf_info, 0, sizeof(buf_info));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001171 buf_info.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001172 buf_info.size = sizeof(data);
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -06001173 buf_info.usage = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001174 err = vkCreateBuffer(demo->device, &buf_info, &demo->uniform_data.buf);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001175 assert(!err);
1176
Mike Stroyanebae8322015-04-17 12:36:38 -06001177 err = vkGetObjectInfo(demo->device,
1178 VK_OBJECT_TYPE_BUFFER, demo->uniform_data.buf,
Tony Barbour72304ef2015-04-16 15:59:00 -06001179 VK_OBJECT_INFO_TYPE_MEMORY_ALLOCATION_COUNT,
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001180 &num_alloc_size, &num_allocations);
1181 assert(!err && num_alloc_size == sizeof(num_allocations));
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001182 mem_reqs = malloc(num_allocations * sizeof(VkMemoryRequirements));
Tony Barbour72304ef2015-04-16 15:59:00 -06001183 demo->uniform_data.mem = malloc(num_allocations * sizeof(VkDeviceMemory));
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001184 demo->uniform_data.num_mem = num_allocations;
Mike Stroyanebae8322015-04-17 12:36:38 -06001185 err = vkGetObjectInfo(demo->device,
1186 VK_OBJECT_TYPE_BUFFER, demo->uniform_data.buf,
Tony Barbour72304ef2015-04-16 15:59:00 -06001187 VK_OBJECT_INFO_TYPE_MEMORY_REQUIREMENTS,
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001188 &mem_reqs_size, mem_reqs);
1189 assert(!err && mem_reqs_size == num_allocations * sizeof(*mem_reqs));
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001190 for (uint32_t i = 0; i < num_allocations; i ++) {
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001191 alloc_info.allocationSize = mem_reqs[i].size;
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001192
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001193 err = vkAllocMemory(demo->device, &alloc_info, &(demo->uniform_data.mem[i]));
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001194 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001195
Mike Stroyanebae8322015-04-17 12:36:38 -06001196 err = vkMapMemory(demo->device, demo->uniform_data.mem[i], 0, 0, 0, (void **) &pData);
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001197 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001198
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001199 memcpy(pData, &data, sizeof data);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001200
Mike Stroyanebae8322015-04-17 12:36:38 -06001201 err = vkUnmapMemory(demo->device, demo->uniform_data.mem[i]);
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001202 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001203
Mike Stroyanebae8322015-04-17 12:36:38 -06001204 err = vkQueueBindObjectMemory(demo->queue,
1205 VK_OBJECT_TYPE_BUFFER, demo->uniform_data.buf,
1206 i, demo->uniform_data.mem[i], 0);
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001207 assert(!err);
1208 }
Courtney Goeltzenleuchter2c2fbbf2015-04-07 17:13:38 -06001209 demo_add_mem_refs(demo, demo->uniform_data.num_mem, demo->uniform_data.mem);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001210
1211 memset(&view_info, 0, sizeof(view_info));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001212 view_info.sType = VK_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO;
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001213 view_info.buffer = demo->uniform_data.buf;
Tony Barbour72304ef2015-04-16 15:59:00 -06001214 view_info.viewType = VK_BUFFER_VIEW_TYPE_RAW;
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001215 view_info.offset = 0;
1216 view_info.range = sizeof(data);
1217
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001218 err = vkCreateBufferView(demo->device, &view_info, &demo->uniform_data.view);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001219 assert(!err);
1220
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001221 demo->uniform_data.attach.sType = VK_STRUCTURE_TYPE_BUFFER_VIEW_ATTACH_INFO;
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001222 demo->uniform_data.attach.view = demo->uniform_data.view;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001223}
1224
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001225static void demo_prepare_descriptor_layout(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001226{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001227 const VkDescriptorSetLayoutBinding layout_bindings[2] = {
Chia-I Wua2aa8632015-03-26 15:04:41 +08001228 [0] = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001229 .descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
Chia-I Wua2aa8632015-03-26 15:04:41 +08001230 .count = 1,
Tony Barbour72304ef2015-04-16 15:59:00 -06001231 .stageFlags = VK_SHADER_STAGE_VERTEX_BIT,
Chia-I Wu91e8e212015-03-27 12:56:09 +08001232 .pImmutableSamplers = NULL,
Chia-I Wua2aa8632015-03-26 15:04:41 +08001233 },
1234 [1] = {
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -06001235 .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
Chia-I Wua2aa8632015-03-26 15:04:41 +08001236 .count = DEMO_TEXTURE_COUNT,
Tony Barbour72304ef2015-04-16 15:59:00 -06001237 .stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT,
Chia-I Wu91e8e212015-03-27 12:56:09 +08001238 .pImmutableSamplers = NULL,
Chia-I Wua2aa8632015-03-26 15:04:41 +08001239 },
1240 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001241 const VkDescriptorSetLayoutCreateInfo descriptor_layout = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001242 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001243 .pNext = NULL,
Chia-I Wua2aa8632015-03-26 15:04:41 +08001244 .count = 2,
1245 .pBinding = layout_bindings,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001246 };
Tony Barbourfdc2d352015-04-22 09:02:32 -06001247 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001248
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001249 err = vkCreateDescriptorSetLayout(demo->device,
Chia-I Wub58c24a2015-03-26 15:27:55 +08001250 &descriptor_layout, &demo->desc_layout);
1251 assert(!err);
1252
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001253 const VkPipelineLayoutCreateInfo pPipelineLayoutCreateInfo = {
1254 .sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
1255 .pNext = NULL,
1256 .descriptorSetCount = 1,
1257 .pSetLayouts = &demo->desc_layout,
1258 };
1259
1260 err = vkCreatePipelineLayout(demo->device,
1261 &pPipelineLayoutCreateInfo,
1262 &demo->pipeline_layout);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001263 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001264}
1265
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001266static VkShader demo_prepare_shader(struct demo *demo,
Tony Barbour72304ef2015-04-16 15:59:00 -06001267 VkShaderStage stage,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001268 const void *code,
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001269 size_t size)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001270{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001271 VkShaderCreateInfo createInfo;
1272 VkShader shader;
1273 VkResult err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001274
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001275
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001276 createInfo.sType = VK_STRUCTURE_TYPE_SHADER_CREATE_INFO;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001277 createInfo.pNext = NULL;
1278
Cody Northrop8b12a1f2015-03-17 15:55:58 -06001279#ifdef EXTERNAL_SPV
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001280 createInfo.codeSize = size;
1281 createInfo.pCode = code;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001282 createInfo.flags = 0;
1283
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001284 err = vkCreateShader(demo->device, &createInfo, &shader);
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001285 if (err) {
1286 free((void *) createInfo.pCode);
1287 }
1288#else
Cody Northrop8b12a1f2015-03-17 15:55:58 -06001289 // Create fake SPV structure to feed GLSL
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001290 // to the driver "under the covers"
1291 createInfo.codeSize = 3 * sizeof(uint32_t) + size + 1;
1292 createInfo.pCode = malloc(createInfo.codeSize);
1293 createInfo.flags = 0;
1294
Tony Barbour72304ef2015-04-16 15:59:00 -06001295 /* try version 0 first: VkShaderStage followed by GLSL */
Cody Northrop8b12a1f2015-03-17 15:55:58 -06001296 ((uint32_t *) createInfo.pCode)[0] = ICD_SPV_MAGIC;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001297 ((uint32_t *) createInfo.pCode)[1] = 0;
1298 ((uint32_t *) createInfo.pCode)[2] = stage;
1299 memcpy(((uint32_t *) createInfo.pCode + 3), code, size + 1);
1300
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001301 err = vkCreateShader(demo->device, &createInfo, &shader);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001302 if (err) {
1303 free((void *) createInfo.pCode);
Tony Barbourf9921732015-04-22 11:36:22 -06001304 return (VkShader) VK_NULL_HANDLE;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001305 }
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001306#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001307
1308 return shader;
1309}
1310
Cody Northrop8b12a1f2015-03-17 15:55:58 -06001311char *demo_read_spv(const char *filename, size_t *psize)
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001312{
1313 long int size;
Tony Barbourf9921732015-04-22 11:36:22 -06001314 size_t U_ASSERT_ONLY retval;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001315 void *shader_code;
1316
1317 FILE *fp = fopen(filename, "rb");
1318 if (!fp) return NULL;
1319
1320 fseek(fp, 0L, SEEK_END);
1321 size = ftell(fp);
1322
1323 fseek(fp, 0L, SEEK_SET);
1324
1325 shader_code = malloc(size);
Tony Barbourfdc2d352015-04-22 09:02:32 -06001326 retval = fread(shader_code, size, 1, fp);
1327 assert(retval == 1);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001328
1329 *psize = size;
1330
1331 return shader_code;
1332}
1333
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001334static VkShader demo_prepare_vs(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001335{
Cody Northrop8b12a1f2015-03-17 15:55:58 -06001336#ifdef EXTERNAL_SPV
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001337 void *vertShaderCode;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001338 size_t size;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001339
Cody Northrop8b12a1f2015-03-17 15:55:58 -06001340 vertShaderCode = demo_read_spv("cube-vert.spv", &size);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001341
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001342 return demo_prepare_shader(demo, VK_SHADER_STAGE_VERTEX,
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001343 vertShaderCode, size);
1344#else
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001345 static const char *vertShaderText =
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001346 "#version 140\n"
1347 "#extension GL_ARB_separate_shader_objects : enable\n"
1348 "#extension GL_ARB_shading_language_420pack : enable\n"
1349 "\n"
1350 "layout(binding = 0) uniform buf {\n"
1351 " mat4 MVP;\n"
1352 " vec4 position[12*3];\n"
1353 " vec4 attr[12*3];\n"
1354 "} ubuf;\n"
1355 "\n"
1356 "layout (location = 0) out vec4 texcoord;\n"
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001357 "\n"
1358 "void main() \n"
1359 "{\n"
1360 " texcoord = ubuf.attr[gl_VertexID];\n"
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001361 " gl_Position = ubuf.MVP * ubuf.position[gl_VertexID];\n"
Chia-I Wuae3b55d2015-04-22 14:56:17 +08001362 "\n"
1363 " // GL->VK conventions\n"
1364 " gl_Position.y = -gl_Position.y;\n"
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001365 "}\n";
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001366
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001367 return demo_prepare_shader(demo, VK_SHADER_STAGE_VERTEX,
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001368 (const void *) vertShaderText,
1369 strlen(vertShaderText));
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001370#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001371}
1372
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001373static VkShader demo_prepare_fs(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001374{
Cody Northrop8b12a1f2015-03-17 15:55:58 -06001375#ifdef EXTERNAL_SPV
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001376 void *fragShaderCode;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001377 size_t size;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001378
Cody Northrop8b12a1f2015-03-17 15:55:58 -06001379 fragShaderCode = demo_read_spv("cube-frag.spv", &size);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001380
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001381 return demo_prepare_shader(demo, VK_SHADER_STAGE_FRAGMENT,
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001382 fragShaderCode, size);
1383#else
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001384 static const char *fragShaderText =
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001385 "#version 140\n"
1386 "#extension GL_ARB_separate_shader_objects : enable\n"
1387 "#extension GL_ARB_shading_language_420pack : enable\n"
Courtney Goeltzenleuchter4d1acf12015-02-25 15:13:35 -07001388 "layout (binding = 1) uniform sampler2D tex;\n"
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001389 "\n"
1390 "layout (location = 0) in vec4 texcoord;\n"
1391 "void main() {\n"
1392 " gl_FragColor = texture(tex, texcoord.xy);\n"
1393 "}\n";
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001394
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001395 return demo_prepare_shader(demo, VK_SHADER_STAGE_FRAGMENT,
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001396 (const void *) fragShaderText,
1397 strlen(fragShaderText));
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001398#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001399}
1400
1401static void demo_prepare_pipeline(struct demo *demo)
1402{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001403 VkGraphicsPipelineCreateInfo pipeline;
1404 VkPipelineIaStateCreateInfo ia;
1405 VkPipelineRsStateCreateInfo rs;
1406 VkPipelineCbStateCreateInfo cb;
1407 VkPipelineDsStateCreateInfo ds;
1408 VkPipelineShaderStageCreateInfo vs;
1409 VkPipelineShaderStageCreateInfo fs;
1410 VkPipelineVpStateCreateInfo vp;
1411 VkPipelineMsStateCreateInfo ms;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001412 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001413
1414 memset(&pipeline, 0, sizeof(pipeline));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001415 pipeline.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001416 pipeline.layout = demo->pipeline_layout;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001417
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001418 memset(&ia, 0, sizeof(ia));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001419 ia.sType = VK_STRUCTURE_TYPE_PIPELINE_IA_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06001420 ia.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001421
1422 memset(&rs, 0, sizeof(rs));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001423 rs.sType = VK_STRUCTURE_TYPE_PIPELINE_RS_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06001424 rs.fillMode = VK_FILL_MODE_SOLID;
1425 rs.cullMode = VK_CULL_MODE_BACK;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001426 rs.frontFace = VK_FRONT_FACE_CCW;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001427
1428 memset(&cb, 0, sizeof(cb));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001429 cb.sType = VK_STRUCTURE_TYPE_PIPELINE_CB_STATE_CREATE_INFO;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001430 VkPipelineCbAttachmentState att_state[1];
Tony Barbour29645d02015-01-16 14:27:35 -07001431 memset(att_state, 0, sizeof(att_state));
1432 att_state[0].format = demo->format;
1433 att_state[0].channelWriteMask = 0xf;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001434 att_state[0].blendEnable = VK_FALSE;
Tony Barbour29645d02015-01-16 14:27:35 -07001435 cb.attachmentCount = 1;
1436 cb.pAttachments = att_state;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001437
Tony Barbour29645d02015-01-16 14:27:35 -07001438 memset(&vp, 0, sizeof(vp));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001439 vp.sType = VK_STRUCTURE_TYPE_PIPELINE_VP_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06001440 vp.viewportCount = 1;
Tony Barbour29645d02015-01-16 14:27:35 -07001441
1442 memset(&ds, 0, sizeof(ds));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001443 ds.sType = VK_STRUCTURE_TYPE_PIPELINE_DS_STATE_CREATE_INFO;
Tony Barbour29645d02015-01-16 14:27:35 -07001444 ds.format = demo->depth.format;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001445 ds.depthTestEnable = VK_TRUE;
1446 ds.depthWriteEnable = VK_TRUE;
Tony Barbour72304ef2015-04-16 15:59:00 -06001447 ds.depthCompareOp = VK_COMPARE_OP_LESS_EQUAL;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001448 ds.depthBoundsEnable = VK_FALSE;
1449 ds.back.stencilFailOp = VK_STENCIL_OP_KEEP;
1450 ds.back.stencilPassOp = VK_STENCIL_OP_KEEP;
Tony Barbour72304ef2015-04-16 15:59:00 -06001451 ds.back.stencilCompareOp = VK_COMPARE_OP_ALWAYS;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001452 ds.stencilTestEnable = VK_FALSE;
Tony Barbour29645d02015-01-16 14:27:35 -07001453 ds.front = ds.back;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001454
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001455 memset(&vs, 0, sizeof(vs));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001456 vs.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1457 vs.shader.stage = VK_SHADER_STAGE_VERTEX;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001458 vs.shader.shader = demo_prepare_vs(demo);
Mike Stroyanebae8322015-04-17 12:36:38 -06001459 assert(vs.shader.shader != VK_NULL_HANDLE);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001460
1461 memset(&fs, 0, sizeof(fs));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001462 fs.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1463 fs.shader.stage = VK_SHADER_STAGE_FRAGMENT;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001464 fs.shader.shader = demo_prepare_fs(demo);
Mike Stroyanebae8322015-04-17 12:36:38 -06001465 assert(fs.shader.shader != VK_NULL_HANDLE);
Tony Barbour29645d02015-01-16 14:27:35 -07001466
1467 memset(&ms, 0, sizeof(ms));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001468 ms.sType = VK_STRUCTURE_TYPE_PIPELINE_MS_STATE_CREATE_INFO;
Tony Barbour29645d02015-01-16 14:27:35 -07001469 ms.sampleMask = 1;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001470 ms.multisampleEnable = VK_FALSE;
Tony Barbour29645d02015-01-16 14:27:35 -07001471 ms.samples = 1;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001472
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001473 pipeline.pNext = (const void *) &ia;
1474 ia.pNext = (const void *) &rs;
1475 rs.pNext = (const void *) &cb;
1476 cb.pNext = (const void *) &ms;
1477 ms.pNext = (const void *) &vp;
1478 vp.pNext = (const void *) &ds;
1479 ds.pNext = (const void *) &vs;
1480 vs.pNext = (const void *) &fs;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001481
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001482 err = vkCreateGraphicsPipeline(demo->device, &pipeline, &demo->pipeline);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001483 assert(!err);
1484
Mike Stroyanebae8322015-04-17 12:36:38 -06001485 vkDestroyObject(demo->device, VK_OBJECT_TYPE_SHADER, vs.shader.shader);
1486 vkDestroyObject(demo->device, VK_OBJECT_TYPE_SHADER, fs.shader.shader);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001487}
1488
1489static void demo_prepare_dynamic_states(struct demo *demo)
1490{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001491 VkDynamicVpStateCreateInfo viewport_create;
1492 VkDynamicRsStateCreateInfo raster;
1493 VkDynamicCbStateCreateInfo color_blend;
1494 VkDynamicDsStateCreateInfo depth_stencil;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001495 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001496
Tony Barbour29645d02015-01-16 14:27:35 -07001497 memset(&viewport_create, 0, sizeof(viewport_create));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001498 viewport_create.sType = VK_STRUCTURE_TYPE_DYNAMIC_VP_STATE_CREATE_INFO;
Courtney Goeltzenleuchter1710d8d2015-02-11 14:13:34 -07001499 viewport_create.viewportAndScissorCount = 1;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001500 VkViewport viewport;
Piers Daniell735ee532015-02-23 16:23:13 -07001501 memset(&viewport, 0, sizeof(viewport));
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001502 viewport.height = (float) demo->height;
1503 viewport.width = (float) demo->width;
1504 viewport.minDepth = (float) 0.0f;
1505 viewport.maxDepth = (float) 1.0f;
Piers Daniell735ee532015-02-23 16:23:13 -07001506 viewport_create.pViewports = &viewport;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001507 VkRect scissor;
Piers Daniell735ee532015-02-23 16:23:13 -07001508 memset(&scissor, 0, sizeof(scissor));
Courtney Goeltzenleuchter1710d8d2015-02-11 14:13:34 -07001509 scissor.extent.width = demo->width;
1510 scissor.extent.height = demo->height;
1511 scissor.offset.x = 0;
1512 scissor.offset.y = 0;
Courtney Goeltzenleuchter1710d8d2015-02-11 14:13:34 -07001513 viewport_create.pScissors = &scissor;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001514
1515 memset(&raster, 0, sizeof(raster));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001516 raster.sType = VK_STRUCTURE_TYPE_DYNAMIC_RS_STATE_CREATE_INFO;
Piers Daniell735ee532015-02-23 16:23:13 -07001517 raster.pointSize = 1.0;
1518 raster.lineWidth = 1.0;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001519
1520 memset(&color_blend, 0, sizeof(color_blend));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001521 color_blend.sType = VK_STRUCTURE_TYPE_DYNAMIC_CB_STATE_CREATE_INFO;
Piers Daniell735ee532015-02-23 16:23:13 -07001522 color_blend.blendConst[0] = 1.0f;
1523 color_blend.blendConst[1] = 1.0f;
1524 color_blend.blendConst[2] = 1.0f;
1525 color_blend.blendConst[3] = 1.0f;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001526
1527 memset(&depth_stencil, 0, sizeof(depth_stencil));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001528 depth_stencil.sType = VK_STRUCTURE_TYPE_DYNAMIC_DS_STATE_CREATE_INFO;
Piers Daniell735ee532015-02-23 16:23:13 -07001529 depth_stencil.minDepth = 0.0f;
1530 depth_stencil.maxDepth = 1.0f;
Tony Barbour29645d02015-01-16 14:27:35 -07001531 depth_stencil.stencilBackRef = 0;
1532 depth_stencil.stencilFrontRef = 0;
1533 depth_stencil.stencilReadMask = 0xff;
1534 depth_stencil.stencilWriteMask = 0xff;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001535
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001536 err = vkCreateDynamicViewportState(demo->device, &viewport_create, &demo->viewport);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001537 assert(!err);
1538
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001539 err = vkCreateDynamicRasterState(demo->device, &raster, &demo->raster);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001540 assert(!err);
1541
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001542 err = vkCreateDynamicColorBlendState(demo->device,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001543 &color_blend, &demo->color_blend);
1544 assert(!err);
1545
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001546 err = vkCreateDynamicDepthStencilState(demo->device,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001547 &depth_stencil, &demo->depth_stencil);
1548 assert(!err);
1549}
1550
Chia-I Wu63ea9262015-03-26 13:14:16 +08001551static void demo_prepare_descriptor_pool(struct demo *demo)
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001552{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001553 const VkDescriptorTypeCount type_counts[2] = {
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001554 [0] = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001555 .type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001556 .count = 1,
1557 },
1558 [1] = {
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -06001559 .type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001560 .count = DEMO_TEXTURE_COUNT,
1561 },
1562 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001563 const VkDescriptorPoolCreateInfo descriptor_pool = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001564 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001565 .pNext = NULL,
1566 .count = 2,
1567 .pTypeCount = type_counts,
1568 };
Tony Barbourfdc2d352015-04-22 09:02:32 -06001569 VkResult U_ASSERT_ONLY err;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001570
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001571 err = vkCreateDescriptorPool(demo->device,
1572 VK_DESCRIPTOR_POOL_USAGE_ONE_SHOT, 1,
Chia-I Wu63ea9262015-03-26 13:14:16 +08001573 &descriptor_pool, &demo->desc_pool);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001574 assert(!err);
1575}
1576
1577static void demo_prepare_descriptor_set(struct demo *demo)
1578{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001579 VkImageViewAttachInfo view_info[DEMO_TEXTURE_COUNT];
1580 VkSamplerImageViewInfo combined_info[DEMO_TEXTURE_COUNT];
1581 VkUpdateSamplerTextures update_fs;
1582 VkUpdateBuffers update_vs;
Chia-I Wub58c24a2015-03-26 15:27:55 +08001583 const void *update_array[2] = { &update_vs, &update_fs };
Tony Barbourfdc2d352015-04-22 09:02:32 -06001584 VkResult U_ASSERT_ONLY err;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001585 uint32_t count;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001586 uint32_t i;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001587
1588 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001589 view_info[i].sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_ATTACH_INFO;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001590 view_info[i].pNext = NULL;
1591 view_info[i].view = demo->textures[i].view,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001592 view_info[i].layout = VK_IMAGE_LAYOUT_GENERAL;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001593
Courtney Goeltzenleuchtereb4754f2015-04-09 11:43:10 -06001594 combined_info[i].sampler = demo->textures[i].sampler;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001595 combined_info[i].pImageView = &view_info[i];
1596 }
1597
1598 memset(&update_vs, 0, sizeof(update_vs));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001599 update_vs.sType = VK_STRUCTURE_TYPE_UPDATE_BUFFERS;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001600 update_vs.pNext = &update_fs;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001601 update_vs.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001602 update_vs.count = 1;
Chia-I Wub58c24a2015-03-26 15:27:55 +08001603 update_vs.pBufferViews = &demo->uniform_data.attach;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001604
1605 memset(&update_fs, 0, sizeof(update_fs));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001606 update_fs.sType = VK_STRUCTURE_TYPE_UPDATE_SAMPLER_TEXTURES;
Chia-I Wub58c24a2015-03-26 15:27:55 +08001607 update_fs.binding = 1;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001608 update_fs.count = DEMO_TEXTURE_COUNT;
1609 update_fs.pSamplerImageViews = combined_info;
1610
Mike Stroyanebae8322015-04-17 12:36:38 -06001611 err = vkAllocDescriptorSets(demo->device, demo->desc_pool,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001612 VK_DESCRIPTOR_SET_USAGE_STATIC,
Chia-I Wu87544e72015-02-23 10:41:08 -07001613 1, &demo->desc_layout,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001614 &demo->desc_set, &count);
1615 assert(!err && count == 1);
1616
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001617 vkBeginDescriptorPoolUpdate(demo->device,
1618 VK_DESCRIPTOR_UPDATE_MODE_FASTEST);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001619
Mike Stroyanebae8322015-04-17 12:36:38 -06001620 vkClearDescriptorSets(demo->device, demo->desc_pool, 1, &demo->desc_set);
1621 vkUpdateDescriptors(demo->device, demo->desc_set, 2, update_array);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001622
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001623 vkEndDescriptorPoolUpdate(demo->device, demo->buffers[demo->current_buffer].cmd);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001624}
1625
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001626static void demo_prepare(struct demo *demo)
1627{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001628 const VkCmdBufferCreateInfo cmd = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001629 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001630 .pNext = NULL,
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07001631 .queueNodeIndex = demo->graphics_queue_node_index,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001632 .flags = 0,
1633 };
Tony Barbourfdc2d352015-04-22 09:02:32 -06001634 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001635
1636 demo_prepare_buffers(demo);
1637 demo_prepare_depth(demo);
1638 demo_prepare_textures(demo);
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001639 demo_prepare_cube_data_buffer(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001640
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001641 demo_prepare_descriptor_layout(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001642 demo_prepare_pipeline(demo);
1643 demo_prepare_dynamic_states(demo);
1644
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001645 for (int i = 0; i < DEMO_BUFFER_COUNT; i++) {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001646 err = vkCreateCommandBuffer(demo->device, &cmd, &demo->buffers[i].cmd);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001647 assert(!err);
1648 }
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001649
Chia-I Wu63ea9262015-03-26 13:14:16 +08001650 demo_prepare_descriptor_pool(demo);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001651 demo_prepare_descriptor_set(demo);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001652
1653
1654 for (int i = 0; i < DEMO_BUFFER_COUNT; i++) {
1655 demo->current_buffer = i;
1656 demo_draw_build_cmd(demo, demo->buffers[i].cmd);
1657 }
1658
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001659 /*
1660 * Prepare functions above may generate pipeline commands
1661 * that need to be flushed before beginning the render loop.
1662 */
1663 demo_flush_init_cmd(demo);
1664
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001665 demo->current_buffer = 0;
Jon Ashburn8d26c062015-04-24 09:46:24 -07001666 demo->prepared = true;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001667}
1668
Ian Elliott639ca472015-04-16 15:23:05 -06001669#ifdef _WIN32
1670static void demo_run(struct demo *demo)
1671{
Courtney Goeltzenleuchterb7e22702015-04-27 14:56:34 -06001672 if (!demo->prepared)
1673 return;
Ian Elliott639ca472015-04-16 15:23:05 -06001674 // Wait for work to finish before updating MVP.
1675 vkDeviceWaitIdle(demo->device);
1676 demo_update_data_buffer(demo);
1677
1678 demo_draw(demo);
1679
1680 // Wait for work to finish before updating MVP.
1681 vkDeviceWaitIdle(demo->device);
1682}
1683
1684// On MS-Windows, make this a global, so it's available to WndProc()
1685struct demo demo;
1686
1687// MS-Windows event handling function:
1688LRESULT CALLBACK WndProc(HWND hWnd,
1689 UINT uMsg,
1690 WPARAM wParam,
1691 LPARAM lParam)
1692{
Ian Elliott639ca472015-04-16 15:23:05 -06001693 switch(uMsg)
1694 {
Ian Elliott639ca472015-04-16 15:23:05 -06001695 case WM_CLOSE:
1696 PostQuitMessage(0);
Tony Barbour3dddd5d2015-04-29 16:19:20 -06001697 break;
Ian Elliott639ca472015-04-16 15:23:05 -06001698 case WM_PAINT:
1699 demo_run(&demo);
1700 return 0;
1701 default:
1702 break;
1703 }
1704 return (DefWindowProc(hWnd, uMsg, wParam, lParam));
1705}
1706
1707static void demo_create_window(struct demo *demo)
1708{
1709 WNDCLASSEX win_class;
1710
1711 // Initialize the window class structure:
1712 win_class.cbSize = sizeof(WNDCLASSEX);
1713 win_class.style = CS_HREDRAW | CS_VREDRAW;
1714 win_class.lpfnWndProc = WndProc;
1715 win_class.cbClsExtra = 0;
1716 win_class.cbWndExtra = 0;
1717 win_class.hInstance = demo->connection; // hInstance
1718 win_class.hIcon = LoadIcon(NULL, IDI_APPLICATION);
1719 win_class.hCursor = LoadCursor(NULL, IDC_ARROW);
1720 win_class.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
1721 win_class.lpszMenuName = NULL;
1722 win_class.lpszClassName = demo->name;
1723 win_class.hIconSm = LoadIcon(NULL, IDI_WINLOGO);
1724 // Register window class:
1725 if (!RegisterClassEx(&win_class)) {
1726 // It didn't work, so try to give a useful error:
1727 printf("Unexpected error trying to start the application!\n");
1728 fflush(stdout);
1729 exit(1);
1730 }
1731 // Create window with the registered class:
1732 demo->window = CreateWindowEx(0,
1733 demo->name, // class name
1734 demo->name, // app name
1735 WS_OVERLAPPEDWINDOW | // window style
1736 WS_VISIBLE |
1737 WS_SYSMENU,
1738 100,100, // x/y coords
1739 demo->width, // width
1740 demo->height, // height
1741 NULL, // handle to parent
1742 NULL, // handle to menu
1743 demo->connection, // hInstance
1744 NULL); // no extra parameters
1745 if (!demo->window) {
1746 // It didn't work, so try to give a useful error:
1747 printf("Cannot create a window in which to draw!\n");
1748 fflush(stdout);
1749 exit(1);
1750 }
1751}
1752#else // _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001753static void demo_handle_event(struct demo *demo,
1754 const xcb_generic_event_t *event)
1755{
Piers Daniell735ee532015-02-23 16:23:13 -07001756 uint8_t event_code = event->response_type & 0x7f;
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07001757 switch (event_code) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001758 case XCB_EXPOSE:
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07001759 // TODO: Resize window
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001760 break;
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07001761 case XCB_CLIENT_MESSAGE:
1762 if((*(xcb_client_message_event_t*)event).data.data32[0] ==
1763 (*demo->atom_wm_delete_window).atom) {
1764 demo->quit = true;
1765 }
1766 break;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001767 case XCB_KEY_RELEASE:
1768 {
1769 const xcb_key_release_event_t *key =
1770 (const xcb_key_release_event_t *) event;
1771
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001772 switch (key->detail) {
1773 case 0x9: // Escape
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001774 demo->quit = true;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001775 break;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001776 case 0x71: // left arrow key
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001777 demo->spin_angle += demo->spin_increment;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001778 break;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001779 case 0x72: // right arrow key
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001780 demo->spin_angle -= demo->spin_increment;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001781 break;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001782 case 0x41:
1783 demo->pause = !demo->pause;
Piers Daniell735ee532015-02-23 16:23:13 -07001784 break;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001785 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001786 }
1787 break;
1788 default:
1789 break;
1790 }
1791}
1792
1793static void demo_run(struct demo *demo)
1794{
1795 xcb_flush(demo->connection);
1796
1797 while (!demo->quit) {
1798 xcb_generic_event_t *event;
1799
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07001800 if (demo->pause) {
1801 event = xcb_wait_for_event(demo->connection);
1802 } else {
1803 event = xcb_poll_for_event(demo->connection);
1804 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001805 if (event) {
1806 demo_handle_event(demo, event);
1807 free(event);
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07001808 }
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07001809
1810 // Wait for work to finish before updating MVP.
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001811 vkDeviceWaitIdle(demo->device);
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07001812 demo_update_data_buffer(demo);
1813
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07001814 demo_draw(demo);
Courtney Goeltzenleuchter1454f3c2014-11-18 11:28:09 -07001815
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07001816 // Wait for work to finish before updating MVP.
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001817 vkDeviceWaitIdle(demo->device);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001818 }
1819}
1820
1821static void demo_create_window(struct demo *demo)
1822{
1823 uint32_t value_mask, value_list[32];
1824
1825 demo->window = xcb_generate_id(demo->connection);
1826
1827 value_mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK;
1828 value_list[0] = demo->screen->black_pixel;
1829 value_list[1] = XCB_EVENT_MASK_KEY_RELEASE |
1830 XCB_EVENT_MASK_EXPOSURE;
1831
1832 xcb_create_window(demo->connection,
1833 XCB_COPY_FROM_PARENT,
1834 demo->window, demo->screen->root,
1835 0, 0, demo->width, demo->height, 0,
1836 XCB_WINDOW_CLASS_INPUT_OUTPUT,
1837 demo->screen->root_visual,
1838 value_mask, value_list);
1839
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07001840 /* Magic code that will send notification when window is destroyed */
1841 xcb_intern_atom_cookie_t cookie = xcb_intern_atom(demo->connection, 1, 12,
1842 "WM_PROTOCOLS");
1843 xcb_intern_atom_reply_t* reply = xcb_intern_atom_reply(demo->connection, cookie, 0);
1844
1845 xcb_intern_atom_cookie_t cookie2 = xcb_intern_atom(demo->connection, 0, 16, "WM_DELETE_WINDOW");
1846 demo->atom_wm_delete_window = xcb_intern_atom_reply(demo->connection, cookie2, 0);
1847
1848 xcb_change_property(demo->connection, XCB_PROP_MODE_REPLACE,
1849 demo->window, (*reply).atom, 4, 32, 1,
1850 &(*demo->atom_wm_delete_window).atom);
1851 free(reply);
1852
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001853 xcb_map_window(demo->connection, demo->window);
1854}
Ian Elliott639ca472015-04-16 15:23:05 -06001855#endif // _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001856
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001857static void demo_init_vk(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001858{
Tobin Ehlis99239dc2015-04-16 18:04:57 -06001859 VkResult err;
1860 // Extensions to enable
1861 const char *ext_names[] = {
Chia-I Wucbb564e2015-04-16 22:02:10 +08001862 "VK_WSI_LunarG",
Tony Barbour3dddd5d2015-04-29 16:19:20 -06001863 "Validation"
Tobin Ehlis99239dc2015-04-16 18:04:57 -06001864 };
1865 size_t extSize = sizeof(uint32_t);
1866 uint32_t extCount = 0;
1867 err = vkGetGlobalExtensionInfo(VK_EXTENSION_INFO_TYPE_COUNT, 0, &extSize, &extCount);
1868 assert(!err);
1869
1870 VkExtensionProperties extProp;
1871 extSize = sizeof(VkExtensionProperties);
Tony Barbourfdc2d352015-04-22 09:02:32 -06001872 bool32_t U_ASSERT_ONLY extFound = 0;
Tobin Ehlis99239dc2015-04-16 18:04:57 -06001873 for (uint32_t i = 0; i < extCount; i++) {
1874 err = vkGetGlobalExtensionInfo(VK_EXTENSION_INFO_TYPE_PROPERTIES, i, &extSize, &extProp);
1875 if (!strcmp(ext_names[0], extProp.extName))
1876 extFound = 1;
1877 }
Ian Elliott65152912015-04-28 13:22:33 -06001878 if (!extFound) {
1879 ERR_EXIT("vkGetGlobalExtensionInfo failed to find the "
1880 "\"VK_WSI_LunarG\" extension.\n\nDo you have a compatible "
1881 "Vulkan installable client driver (ICD) installed?\nPlease "
1882 "look at the Getting Started guide for additional "
1883 "information.\n",
1884 "vkCreateInstance Failure");
1885 }
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001886 const VkApplicationInfo app = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001887 .sType = VK_STRUCTURE_TYPE_APPLICATION_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001888 .pNext = NULL,
Ian Elliott44e33f72015-04-28 10:52:52 -06001889 .pAppName = APP_SHORT_NAME,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001890 .appVersion = 0,
Ian Elliott44e33f72015-04-28 10:52:52 -06001891 .pEngineName = APP_SHORT_NAME,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001892 .engineVersion = 0,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001893 .apiVersion = VK_API_VERSION,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001894 };
Tony Barbour3dddd5d2015-04-29 16:19:20 -06001895 VkInstanceCreateInfo inst_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001896 .sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
Jon Ashburnab46b362015-04-04 14:52:07 -06001897 .pNext = NULL,
1898 .pAppInfo = &app,
1899 .pAllocCb = NULL,
Tobin Ehlis99239dc2015-04-16 18:04:57 -06001900 .extensionCount = 1,
1901 .ppEnabledExtensionNames = ext_names,
Jon Ashburnab46b362015-04-04 14:52:07 -06001902 };
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -06001903 const VkDeviceQueueCreateInfo queue = {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001904 .queueNodeIndex = 0,
1905 .queueCount = 1,
1906 };
Ian Elliott097d9f32015-04-28 11:35:02 -06001907
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -06001908 const VkDeviceCreateInfo device = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001909 .sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001910 .pNext = NULL,
1911 .queueRecordCount = 1,
1912 .pRequestedQueues = &queue,
Tobin Ehlis99239dc2015-04-16 18:04:57 -06001913 .extensionCount = 1,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001914 .ppEnabledExtensionNames = ext_names,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001915 .flags = VK_DEVICE_CREATE_VALIDATION_BIT,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001916 };
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001917 uint32_t gpu_count;
1918 uint32_t i;
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07001919 size_t data_size;
1920 uint32_t queue_count;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001921
Tony Barbour3dddd5d2015-04-29 16:19:20 -06001922 if (demo->validate) {
1923 inst_info.extensionCount = 2;
1924 }
1925
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001926 err = vkCreateInstance(&inst_info, &demo->inst);
Ian Elliott07264132015-04-28 11:35:02 -06001927 if (err == VK_ERROR_INCOMPATIBLE_DRIVER) {
1928 ERR_EXIT("Cannot find a compatible Vulkan installable client driver "
Ian Elliott65152912015-04-28 13:22:33 -06001929 "(ICD).\n\nPlease look at the Getting Started guide for "
Ian Elliott07264132015-04-28 11:35:02 -06001930 "additional information.\n",
1931 "vkCreateInstance Failure");
Tony Barbour3dddd5d2015-04-29 16:19:20 -06001932 } else if (err == VK_ERROR_INVALID_EXTENSION) {
1933 ERR_EXIT("Cannot find a specified extension library"
1934 ".\nMake sure your layers path is set appropriately\n",
1935 "vkCreateInstance Failure");
Ian Elliott07264132015-04-28 11:35:02 -06001936 } else if (err) {
Ian Elliott65152912015-04-28 13:22:33 -06001937 ERR_EXIT("vkCreateInstance failed.\n\nDo you have a compatible Vulkan "
1938 "installable client driver (ICD) installed?\nPlease look at "
Ian Elliott07264132015-04-28 11:35:02 -06001939 "the Getting Started guide for additional information.\n",
1940 "vkCreateInstance Failure");
Ian Elliott3979e282015-04-03 15:24:55 -06001941 }
Jon Ashburnab46b362015-04-04 14:52:07 -06001942
Tony Barbour3dddd5d2015-04-29 16:19:20 -06001943
Jon Ashburn07c0c0c2015-04-15 11:31:12 -06001944 gpu_count = 1;
1945 err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, &demo->gpu);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001946 assert(!err && gpu_count == 1);
1947
Tony Barbour3dddd5d2015-04-29 16:19:20 -06001948 if (demo->validate) {
1949 vkDbgRegisterMsgCallback(demo->inst, dbgFunc, NULL);
1950 }
1951
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001952 err = vkCreateDevice(demo->gpu, &device, &demo->device);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001953 assert(!err);
1954
Tony Barbour72304ef2015-04-16 15:59:00 -06001955 err = vkGetPhysicalDeviceInfo(demo->gpu, VK_PHYSICAL_DEVICE_INFO_TYPE_PROPERTIES,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001956 &data_size, NULL);
1957 assert(!err);
1958
Tony Barbour72304ef2015-04-16 15:59:00 -06001959 demo->gpu_props = (VkPhysicalDeviceProperties *) malloc(data_size);
1960 err = vkGetPhysicalDeviceInfo(demo->gpu, VK_PHYSICAL_DEVICE_INFO_TYPE_PROPERTIES,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001961 &data_size, demo->gpu_props);
1962 assert(!err);
1963
Tony Barbour72304ef2015-04-16 15:59:00 -06001964 err = vkGetPhysicalDeviceInfo(demo->gpu, VK_PHYSICAL_DEVICE_INFO_TYPE_QUEUE_PROPERTIES,
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07001965 &data_size, NULL);
1966 assert(!err);
1967
Tony Barbour72304ef2015-04-16 15:59:00 -06001968 demo->queue_props = (VkPhysicalDeviceQueueProperties *) malloc(data_size);
1969 err = vkGetPhysicalDeviceInfo(demo->gpu, VK_PHYSICAL_DEVICE_INFO_TYPE_QUEUE_PROPERTIES,
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07001970 &data_size, demo->queue_props);
1971 assert(!err);
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001972 queue_count = (uint32_t)(data_size / sizeof(VkPhysicalDeviceQueueProperties));
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07001973 assert(queue_count >= 1);
1974
Mark Lobodzinskida9b1092015-04-16 11:44:05 -05001975 // Graphics queue and MemMgr queue can be separate.
1976 // TODO: Add support for separate queues, including synchronization,
1977 // and appropriate tracking for QueueSubmit and QueueBindObjectMemory
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07001978 for (i = 0; i < queue_count; i++) {
Mark Lobodzinskida9b1092015-04-16 11:44:05 -05001979 if ((demo->queue_props[i].queueFlags & VK_QUEUE_GRAPHICS_BIT) &&
1980 (demo->queue_props[i].queueFlags & VK_QUEUE_MEMMGR_BIT) )
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07001981 break;
1982 }
1983 assert(i < queue_count);
1984 demo->graphics_queue_node_index = i;
1985
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001986 err = vkGetDeviceQueue(demo->device, demo->graphics_queue_node_index,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001987 0, &demo->queue);
1988 assert(!err);
Tony Barbour3dddd5d2015-04-29 16:19:20 -06001989
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001990}
1991
1992static void demo_init_connection(struct demo *demo)
1993{
Ian Elliott639ca472015-04-16 15:23:05 -06001994#ifndef _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001995 const xcb_setup_t *setup;
1996 xcb_screen_iterator_t iter;
1997 int scr;
1998
1999 demo->connection = xcb_connect(NULL, &scr);
Ian Elliott3979e282015-04-03 15:24:55 -06002000 if (demo->connection == NULL) {
2001 printf("Cannot find a compatible Vulkan installable client driver "
2002 "(ICD).\nExiting ...\n");
2003 fflush(stdout);
2004 exit(1);
2005 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002006
2007 setup = xcb_get_setup(demo->connection);
2008 iter = xcb_setup_roots_iterator(setup);
2009 while (scr-- > 0)
2010 xcb_screen_next(&iter);
2011
2012 demo->screen = iter.data;
Ian Elliott639ca472015-04-16 15:23:05 -06002013#endif // _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002014}
2015
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002016static void demo_init(struct demo *demo, int argc, char **argv)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002017{
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002018 vec3 eye = {0.0f, 3.0f, 5.0f};
2019 vec3 origin = {0, 0, 0};
Chia-I Wuae3b55d2015-04-22 14:56:17 +08002020 vec3 up = {0.0f, 1.0f, 0.0};
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002021
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002022 memset(demo, 0, sizeof(*demo));
2023
Piers Daniell735ee532015-02-23 16:23:13 -07002024 for (int i = 1; i < argc; i++) {
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002025 if (strcmp(argv[i], "--use_staging") == 0) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002026 demo->use_staging_buffer = true;
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002027 continue;
Ian Elliott639ca472015-04-16 15:23:05 -06002028 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002029 if (strcmp(argv[i], "--validate") == 0) {
2030 demo->validate = true;
2031 continue;
2032 }
2033
2034 fprintf(stderr, "Usage:\n %s [--use_staging] [--validate}\n", APP_SHORT_NAME);
Ian Elliott639ca472015-04-16 15:23:05 -06002035 fflush(stderr);
2036 exit(1);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002037 }
2038
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002039 demo_init_connection(demo);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002040 demo_init_vk(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002041
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002042 demo->width = 500;
2043 demo->height = 500;
Tony Barbour72304ef2015-04-16 15:59:00 -06002044 demo->format = VK_FORMAT_B8G8R8A8_UNORM;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002045
2046 demo->spin_angle = 0.01f;
2047 demo->spin_increment = 0.01f;
2048 demo->pause = false;
2049
Piers Daniell735ee532015-02-23 16:23:13 -07002050 mat4x4_perspective(demo->projection_matrix, (float)degreesToRadians(45.0f), 1.0f, 0.1f, 100.0f);
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002051 mat4x4_look_at(demo->view_matrix, eye, origin, up);
2052 mat4x4_identity(demo->model_matrix);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002053}
2054
2055static void demo_cleanup(struct demo *demo)
2056{
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06002057 uint32_t i, j;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002058
Courtney Goeltzenleuchterb7e22702015-04-27 14:56:34 -06002059 demo->prepared = false;
2060
Mike Stroyanebae8322015-04-17 12:36:38 -06002061 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DESCRIPTOR_SET, demo->desc_set);
2062 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DESCRIPTOR_POOL, demo->desc_pool);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08002063
Mike Stroyanebae8322015-04-17 12:36:38 -06002064 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DYNAMIC_VP_STATE, demo->viewport);
2065 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DYNAMIC_RS_STATE, demo->raster);
2066 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DYNAMIC_CB_STATE, demo->color_blend);
2067 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DYNAMIC_DS_STATE, demo->depth_stencil);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002068
Mike Stroyanebae8322015-04-17 12:36:38 -06002069 vkDestroyObject(demo->device, VK_OBJECT_TYPE_PIPELINE, demo->pipeline);
2070 vkDestroyObject(demo->device, VK_OBJECT_TYPE_PIPELINE_LAYOUT, demo->pipeline_layout);
2071 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DESCRIPTOR_SET_LAYOUT, demo->desc_layout);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002072
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002073 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Mike Stroyanebae8322015-04-17 12:36:38 -06002074 vkDestroyObject(demo->device, VK_OBJECT_TYPE_IMAGE_VIEW, demo->textures[i].view);
2075 vkQueueBindObjectMemory(demo->queue, VK_OBJECT_TYPE_IMAGE, demo->textures[i].image, 0, VK_NULL_HANDLE, 0);
2076 vkDestroyObject(demo->device, VK_OBJECT_TYPE_IMAGE, demo->textures[i].image);
Courtney Goeltzenleuchter1359b7a2015-04-02 16:25:42 -06002077 demo_remove_mem_refs(demo, demo->textures[i].num_mem, demo->textures[i].mem);
Jon Ashburnb2a66652015-01-16 09:37:43 -07002078 for (j = 0; j < demo->textures[i].num_mem; j++)
Mike Stroyanebae8322015-04-17 12:36:38 -06002079 vkFreeMemory(demo->device, demo->textures[i].mem[j]);
2080 free(demo->textures[i].mem);
2081 vkDestroyObject(demo->device, VK_OBJECT_TYPE_SAMPLER, demo->textures[i].sampler);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002082 }
Chia-I Wucbb564e2015-04-16 22:02:10 +08002083 vkDestroySwapChainWSI(demo->swap_chain);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002084
Mike Stroyanebae8322015-04-17 12:36:38 -06002085 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DEPTH_STENCIL_VIEW, demo->depth.view);
2086 vkQueueBindObjectMemory(demo->queue, VK_OBJECT_TYPE_IMAGE, demo->depth.image, 0, VK_NULL_HANDLE, 0);
Courtney Goeltzenleuchter1359b7a2015-04-02 16:25:42 -06002087 demo_remove_mem_refs(demo, demo->depth.num_mem, demo->depth.mem);
Mike Stroyanebae8322015-04-17 12:36:38 -06002088 vkDestroyObject(demo->device, VK_OBJECT_TYPE_IMAGE, demo->depth.image);
Courtney Goeltzenleuchter1359b7a2015-04-02 16:25:42 -06002089 for (j = 0; j < demo->depth.num_mem; j++) {
Mike Stroyanebae8322015-04-17 12:36:38 -06002090 vkFreeMemory(demo->device, demo->depth.mem[j]);
Courtney Goeltzenleuchter1359b7a2015-04-02 16:25:42 -06002091 }
Mark Lobodzinski7b8fee62015-02-18 16:38:17 -06002092
Mike Stroyanebae8322015-04-17 12:36:38 -06002093 vkDestroyObject(demo->device, VK_OBJECT_TYPE_BUFFER_VIEW, demo->uniform_data.view);
2094 vkQueueBindObjectMemory(demo->queue, VK_OBJECT_TYPE_BUFFER, demo->uniform_data.buf, 0, VK_NULL_HANDLE, 0);
2095 vkDestroyObject(demo->device, VK_OBJECT_TYPE_BUFFER, demo->uniform_data.buf);
Courtney Goeltzenleuchter1359b7a2015-04-02 16:25:42 -06002096 demo_remove_mem_refs(demo, demo->uniform_data.num_mem, demo->uniform_data.mem);
Jon Ashburnae7c21c2015-01-19 15:00:26 -07002097 for (j = 0; j < demo->uniform_data.num_mem; j++)
Mike Stroyanebae8322015-04-17 12:36:38 -06002098 vkFreeMemory(demo->device, demo->uniform_data.mem[j]);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002099
2100 for (i = 0; i < DEMO_BUFFER_COUNT; i++) {
Mike Stroyanebae8322015-04-17 12:36:38 -06002101 vkDestroyObject(demo->device, VK_OBJECT_TYPE_COLOR_ATTACHMENT_VIEW, demo->buffers[i].view);
2102 vkDestroyObject(demo->device, VK_OBJECT_TYPE_COMMAND_BUFFER, demo->buffers[i].cmd);
Courtney Goeltzenleuchter1359b7a2015-04-02 16:25:42 -06002103 demo_remove_mem_refs(demo, 1, &demo->buffers[i].mem);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002104 }
2105
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002106 vkDestroyDevice(demo->device);
2107 vkDestroyInstance(demo->inst);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002108
Ian Elliott639ca472015-04-16 15:23:05 -06002109#ifndef _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002110 xcb_destroy_window(demo->connection, demo->window);
2111 xcb_disconnect(demo->connection);
Ian Elliott639ca472015-04-16 15:23:05 -06002112#endif // _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002113}
2114
Ian Elliott639ca472015-04-16 15:23:05 -06002115#ifdef _WIN32
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002116extern int __getmainargs(
2117 int * _Argc,
2118 char *** _Argv,
2119 char *** _Env,
2120 int _DoWildCard,
2121 int * new_mode);
Ian Elliottf9cf78c2015-04-28 10:33:11 -06002122
Ian Elliotta748eaf2015-04-28 15:50:36 -06002123int WINAPI WinMain(HINSTANCE hInstance,
2124 HINSTANCE hPrevInstance,
2125 LPSTR pCmdLine,
2126 int nCmdShow)
Ian Elliott639ca472015-04-16 15:23:05 -06002127{
2128 MSG msg; // message
2129 bool done; // flag saying when app is complete
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002130 int argc;
2131 char** argv;
2132 char** env;
2133 int new_mode = 0;
Ian Elliott639ca472015-04-16 15:23:05 -06002134
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002135 __getmainargs(&argc,&argv,&env,0,&new_mode);
2136
2137 demo_init(&demo, argc, argv);
2138 demo.connection = hInstance;
2139 strncpy(demo.name, "cube", APP_NAME_STR_LEN);
Ian Elliott639ca472015-04-16 15:23:05 -06002140 demo_create_window(&demo);
2141
2142 demo_prepare(&demo);
2143
2144 done = false; //initialize loop condition variable
2145 /* main message loop*/
2146 while(!done)
2147 {
Ian Elliotta748eaf2015-04-28 15:50:36 -06002148 PeekMessage(&msg, NULL, 0, 0, PM_REMOVE);
Ian Elliott639ca472015-04-16 15:23:05 -06002149 if (msg.message == WM_QUIT) //check for a quit message
2150 {
2151 done = true; //if found, quit app
2152 }
2153 else
2154 {
2155 /* Translate and dispatch to event queue*/
2156 TranslateMessage(&msg);
2157 DispatchMessage(&msg);
2158 }
2159 }
2160
2161 demo_cleanup(&demo);
2162
Tony Barbourf9921732015-04-22 11:36:22 -06002163 return (int) msg.wParam;
Ian Elliott639ca472015-04-16 15:23:05 -06002164}
2165#else // _WIN32
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002166int main(int argc, char **argv)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002167{
2168 struct demo demo;
2169
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002170 demo_init(&demo, argc, argv);
Chia-I Wucbb564e2015-04-16 22:02:10 +08002171 demo_create_window(&demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002172
2173 demo_prepare(&demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002174 demo_run(&demo);
2175
2176 demo_cleanup(&demo);
2177
2178 return 0;
2179}
Ian Elliott639ca472015-04-16 15:23:05 -06002180#endif // _WIN32