blob: 5fff204e6693654ed7aec74454bbb8f2675c213e [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[] = {
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600133 -1.0f,-1.0f,-1.0f, // Vertex 0
134 -1.0f,-1.0f, 1.0f,
135 -1.0f, 1.0f, 1.0f,
136
137 -1.0f, 1.0f, 1.0f, // Vertex 1
138 -1.0f, 1.0f,-1.0f,
139 -1.0f,-1.0f,-1.0f,
140
141 -1.0f,-1.0f,-1.0f, // Vertex 2
142 1.0f, 1.0f,-1.0f,
143 1.0f,-1.0f,-1.0f,
144
145 -1.0f,-1.0f,-1.0f, // Vertex 3
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600146 -1.0f, 1.0f,-1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600147 1.0f, 1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600148
149 -1.0f,-1.0f,-1.0f, // Vertex 4
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600150 1.0f,-1.0f,-1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600151 1.0f,-1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600152
153 -1.0f,-1.0f,-1.0f, // Vertex 5
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600154 1.0f,-1.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600155 -1.0f,-1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600156
157 -1.0f, 1.0f,-1.0f, // Vertex 6
158 -1.0f, 1.0f, 1.0f,
159 1.0f, 1.0f, 1.0f,
160
161 -1.0f, 1.0f,-1.0f, // Vertex 7
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600162 1.0f, 1.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600163 1.0f, 1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600164
165 1.0f, 1.0f,-1.0f, // Vertex 8
166 1.0f, 1.0f, 1.0f,
167 1.0f,-1.0f, 1.0f,
168
169 1.0f,-1.0f, 1.0f, // Vertex 9
170 1.0f,-1.0f,-1.0f,
171 1.0f, 1.0f,-1.0f,
172
173 -1.0f, 1.0f, 1.0f, // Vertex 10
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600174 -1.0f,-1.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600175 1.0f, 1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600176
177 -1.0f,-1.0f, 1.0f, // Vertex 11
178 1.0f,-1.0f, 1.0f,
179 1.0f, 1.0f, 1.0f,
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600180};
181
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600182static const float g_uv_buffer_data[] = {
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600183 1.0f, 0.0f, // Vertex 0
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600184 0.0f, 0.0f,
185 0.0f, 1.0f,
186
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600187 0.0f, 1.0f, // Vertex 1
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600188 1.0f, 1.0f,
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600189 1.0f, 0.0f,
190
191// 0.0f, 1.0f, // Vertex 2
192// 1.0f, 0.0f,
193// 0.0f, 0.0f,
194
195// 0.0f, 1.0f, // Vertex 3
196// 1.0f, 0.0f,
197// 1.0f, 1.0f,
198
199 0.0f, 0.0f, // Vertex 2
200 1.0f, 1.0f,
201 1.0f, 0.0f,
202
203 0.0f, 0.0f, // Vertex 3
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600204 0.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600205 1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600206
207 0.0f, 1.0f, // Vertex 4
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600208 0.0f, 0.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600209 1.0f, 0.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600210
211 0.0f, 1.0f, // Vertex 5
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600212 1.0f, 0.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600213 1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600214
215 0.0f, 1.0f, // Vertex 6
216 1.0f, 1.0f,
217 1.0f, 0.0f,
218
219 0.0f, 1.0f, // Vertex 7
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600220 1.0f, 0.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600221 0.0f, 0.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600222
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600223 0.0f, 1.0f, // Vertex 8
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600224 1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600225 1.0f, 0.0f,
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600226
227 1.0f, 0.0f, // Vertex 9
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600228 0.0f, 0.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600229 0.0f, 1.0f,
230
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600231 1.0f, 1.0f, // Vertex 10
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600232 1.0f, 0.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600233 0.0f, 1.0f,
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600234
235 1.0f, 0.0f, // Vertex 11
236 0.0f, 0.0f,
237 0.0f, 1.0f,
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600238};
239
240void dumpMatrix(const char *note, mat4x4 MVP)
241{
242 int i;
243
244 printf("%s: \n", note);
245 for (i=0; i<4; i++) {
246 printf("%f, %f, %f, %f\n", MVP[i][0], MVP[i][1], MVP[i][2], MVP[i][3]);
247 }
248 printf("\n");
249 fflush(stdout);
250}
251
252void dumpVec4(const char *note, vec4 vector)
253{
254 printf("%s: \n", note);
255 printf("%f, %f, %f, %f\n", vector[0], vector[1], vector[2], vector[3]);
256 printf("\n");
257 fflush(stdout);
258}
259
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600260struct demo {
Ian Elliott639ca472015-04-16 15:23:05 -0600261#ifdef _WIN32
262#define APP_NAME_STR_LEN 80
263 HINSTANCE connection; // hInstance - Windows Instance
264 char name[APP_NAME_STR_LEN]; // Name to put on the window/icon
265 HWND window; // hWnd - window handle
266#else // _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600267 xcb_connection_t *connection;
268 xcb_screen_t *screen;
Chia-I Wucbb564e2015-04-16 22:02:10 +0800269 xcb_window_t window;
270 xcb_intern_atom_reply_t *atom_wm_delete_window;
Ian Elliott639ca472015-04-16 15:23:05 -0600271#endif // _WIN32
Jon Ashburn8d26c062015-04-24 09:46:24 -0700272 bool prepared;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700273 bool use_staging_buffer;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600274
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600275 VkInstance inst;
Tony Barbour72304ef2015-04-16 15:59:00 -0600276 VkPhysicalDevice gpu;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600277 VkDevice device;
278 VkQueue queue;
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -0700279 uint32_t graphics_queue_node_index;
Tony Barbour72304ef2015-04-16 15:59:00 -0600280 VkPhysicalDeviceProperties *gpu_props;
281 VkPhysicalDeviceQueueProperties *queue_props;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600282
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600283 VkFramebuffer framebuffer;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600284 int width, height;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600285 VkFormat format;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600286
Chia-I Wucbb564e2015-04-16 22:02:10 +0800287 VkSwapChainWSI swap_chain;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600288 struct {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600289 VkImage image;
Tony Barbour72304ef2015-04-16 15:59:00 -0600290 VkDeviceMemory mem;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600291 VkCmdBuffer cmd;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600292
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600293 VkColorAttachmentView view;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600294 } buffers[DEMO_BUFFER_COUNT];
295
296 struct {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600297 VkFormat format;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600298
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600299 VkImage image;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600300 uint32_t num_mem;
Tony Barbour72304ef2015-04-16 15:59:00 -0600301 VkDeviceMemory *mem;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600302 VkDepthStencilView view;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600303 } depth;
304
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600305 struct texture_object textures[DEMO_TEXTURE_COUNT];
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600306
307 struct {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600308 VkBuffer buf;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600309 uint32_t num_mem;
Tony Barbour72304ef2015-04-16 15:59:00 -0600310 VkDeviceMemory *mem;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600311 VkBufferView view;
312 VkBufferViewAttachInfo attach;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600313 } uniform_data;
314
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600315 VkCmdBuffer cmd; // Buffer for initialization commands
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -0500316 VkPipelineLayout pipeline_layout;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600317 VkDescriptorSetLayout desc_layout;
318 VkPipeline pipeline;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600319
Courtney Goeltzenleuchter5e6d1e92015-04-10 16:24:50 -0600320 VkDynamicVpState viewport;
321 VkDynamicRsState raster;
322 VkDynamicCbState color_blend;
323 VkDynamicDsState depth_stencil;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600324
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600325 mat4x4 projection_matrix;
326 mat4x4 view_matrix;
327 mat4x4 model_matrix;
328
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600329 float spin_angle;
330 float spin_increment;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600331 bool pause;
332
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600333 VkDescriptorPool desc_pool;
334 VkDescriptorSet desc_set;
Chia-I Wu6a3c8972015-01-04 16:27:24 +0800335
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600336 bool quit;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600337 uint32_t current_buffer;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600338};
339
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600340static void demo_flush_init_cmd(struct demo *demo)
341{
Tony Barbourfdc2d352015-04-22 09:02:32 -0600342 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600343
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600344 if (demo->cmd == VK_NULL_HANDLE)
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600345 return;
346
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600347 err = vkEndCommandBuffer(demo->cmd);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600348 assert(!err);
349
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600350 const VkCmdBuffer cmd_bufs[] = { demo->cmd };
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600351
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600352 err = vkQueueSubmit(demo->queue, 1, cmd_bufs, VK_NULL_HANDLE);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600353 assert(!err);
354
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600355 err = vkQueueWaitIdle(demo->queue);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600356 assert(!err);
357
Mike Stroyanebae8322015-04-17 12:36:38 -0600358 vkDestroyObject(demo->device, VK_OBJECT_TYPE_COMMAND_BUFFER, demo->cmd);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600359 demo->cmd = VK_NULL_HANDLE;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600360}
361
362static void demo_add_mem_refs(
363 struct demo *demo,
Tony Barbour72304ef2015-04-16 15:59:00 -0600364 int num_refs, VkDeviceMemory *mem)
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600365{
Courtney Goeltzenleuchter6b0caf12015-04-16 13:38:46 -0600366 vkQueueAddMemReferences(demo->queue, num_refs, mem);
Courtney Goeltzenleuchter1359b7a2015-04-02 16:25:42 -0600367}
368
369static void demo_remove_mem_refs(
370 struct demo *demo,
Tony Barbour72304ef2015-04-16 15:59:00 -0600371 int num_refs, VkDeviceMemory *mem)
Courtney Goeltzenleuchter1359b7a2015-04-02 16:25:42 -0600372{
Courtney Goeltzenleuchter6b0caf12015-04-16 13:38:46 -0600373 vkQueueRemoveMemReferences(demo->queue, num_refs, mem);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600374}
375
376static void demo_set_image_layout(
377 struct demo *demo,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600378 VkImage image,
379 VkImageLayout old_image_layout,
380 VkImageLayout new_image_layout)
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600381{
Tony Barbourfdc2d352015-04-22 09:02:32 -0600382 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600383
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600384 if (demo->cmd == VK_NULL_HANDLE) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600385 const VkCmdBufferCreateInfo cmd = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600386 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600387 .pNext = NULL,
388 .queueNodeIndex = demo->graphics_queue_node_index,
389 .flags = 0,
390 };
391
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600392 err = vkCreateCommandBuffer(demo->device, &cmd, &demo->cmd);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600393 assert(!err);
394
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600395 VkCmdBufferBeginInfo cmd_buf_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600396 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600397 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -0600398 .flags = VK_CMD_BUFFER_OPTIMIZE_SMALL_BATCH_BIT |
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600399 VK_CMD_BUFFER_OPTIMIZE_ONE_TIME_SUBMIT_BIT,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600400 };
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600401 err = vkBeginCommandBuffer(demo->cmd, &cmd_buf_info);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600402 }
403
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600404 VkImageMemoryBarrier image_memory_barrier = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600405 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600406 .pNext = NULL,
407 .outputMask = 0,
408 .inputMask = 0,
409 .oldLayout = old_image_layout,
410 .newLayout = new_image_layout,
411 .image = image,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600412 .subresourceRange = { VK_IMAGE_ASPECT_COLOR, 0, 1, 0, 0 }
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600413 };
414
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600415 if (new_image_layout == VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL) {
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600416 /* Make sure anything that was copying from this image has completed */
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -0600417 image_memory_barrier.inputMask = VK_MEMORY_INPUT_TRANSFER_BIT;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600418 }
419
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600420 if (new_image_layout == VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL) {
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600421 /* Make sure any Copy or CPU writes to image are flushed */
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -0600422 image_memory_barrier.outputMask = VK_MEMORY_OUTPUT_CPU_WRITE_BIT | VK_MEMORY_OUTPUT_TRANSFER_BIT;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600423 }
424
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600425 VkImageMemoryBarrier *pmemory_barrier = &image_memory_barrier;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600426
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600427 VkPipeEvent set_events[] = { VK_PIPE_EVENT_TOP_OF_PIPE };
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600428
Tony Barbour72304ef2015-04-16 15:59:00 -0600429 vkCmdPipelineBarrier(demo->cmd, VK_WAIT_EVENT_TOP_OF_PIPE, 1, set_events, 1, (const void **)&pmemory_barrier);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600430}
431
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600432static void demo_draw_build_cmd(struct demo *demo, VkCmdBuffer cmd_buf)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600433{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600434 const VkColorAttachmentBindInfo color_attachment = {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600435 .view = demo->buffers[demo->current_buffer].view,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600436 .layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600437 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600438 const VkDepthStencilBindInfo depth_stencil = {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600439 .view = demo->depth.view,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600440 .layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600441 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600442 const VkClearColor clear_color = {
Courtney Goeltzenleuchter374553c2015-04-03 16:35:32 -0600443 .color.floatColor = { 0.2f, 0.2f, 0.2f, 0.2f },
444 .useRawValue = false,
445 };
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600446 const float clear_depth = 1.0f;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600447 VkImageSubresourceRange clear_range;
448 VkCmdBufferBeginInfo cmd_buf_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600449 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO,
Courtney Goeltzenleuchter53968d82015-04-03 15:25:24 -0600450 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -0600451 .flags = VK_CMD_BUFFER_OPTIMIZE_SMALL_BATCH_BIT |
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600452 VK_CMD_BUFFER_OPTIMIZE_ONE_TIME_SUBMIT_BIT,
Jon Ashburn60ccfbe2014-12-31 17:08:35 -0700453 };
Tony Barbourfdc2d352015-04-22 09:02:32 -0600454 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600455 VkAttachmentLoadOp load_op = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
456 VkAttachmentStoreOp store_op = VK_ATTACHMENT_STORE_OP_DONT_CARE;
457 const VkFramebufferCreateInfo fb_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600458 .sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO,
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700459 .pNext = NULL,
460 .colorAttachmentCount = 1,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600461 .pColorAttachments = (VkColorAttachmentBindInfo*) &color_attachment,
462 .pDepthStencilAttachment = (VkDepthStencilBindInfo*) &depth_stencil,
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700463 .sampleCount = 1,
Mark Lobodzinskic06b7412015-01-27 13:24:03 -0600464 .width = demo->width,
465 .height = demo->height,
466 .layers = 1,
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700467 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600468 VkRenderPassCreateInfo rp_info;
469 VkRenderPassBegin rp_begin;
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700470
471 memset(&rp_info, 0 , sizeof(rp_info));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600472 err = vkCreateFramebuffer(demo->device, &fb_info, &rp_begin.framebuffer);
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700473 assert(!err);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600474 rp_info.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO;
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700475 rp_info.renderArea.extent.width = demo->width;
476 rp_info.renderArea.extent.height = demo->height;
Courtney Goeltzenleuchter53968d82015-04-03 15:25:24 -0600477 rp_info.colorAttachmentCount = fb_info.colorAttachmentCount;
478 rp_info.pColorFormats = &demo->format;
479 rp_info.pColorLayouts = &color_attachment.layout;
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700480 rp_info.pColorLoadOps = &load_op;
481 rp_info.pColorStoreOps = &store_op;
Courtney Goeltzenleuchter53968d82015-04-03 15:25:24 -0600482 rp_info.pColorLoadClearValues = &clear_color;
Tony Barbour72304ef2015-04-16 15:59:00 -0600483 rp_info.depthStencilFormat = VK_FORMAT_D16_UNORM;
Courtney Goeltzenleuchter53968d82015-04-03 15:25:24 -0600484 rp_info.depthStencilLayout = depth_stencil.layout;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600485 rp_info.depthLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
Courtney Goeltzenleuchter53968d82015-04-03 15:25:24 -0600486 rp_info.depthLoadClearValue = clear_depth;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600487 rp_info.depthStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
488 rp_info.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
Courtney Goeltzenleuchter53968d82015-04-03 15:25:24 -0600489 rp_info.stencilLoadClearValue = 0;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600490 rp_info.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
491 err = vkCreateRenderPass(demo->device, &rp_info, &rp_begin.renderPass);
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700492 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600493
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600494 err = vkBeginCommandBuffer(cmd_buf, &cmd_buf_info);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600495 assert(!err);
496
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600497 vkCmdBindPipeline(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600498 demo->pipeline);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600499 vkCmdBindDescriptorSets(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS,
Cody Northropfb5185a2015-04-16 13:41:56 -0600500 0, 1, &demo->desc_set, 0, NULL);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600501
Tony Barbour72304ef2015-04-16 15:59:00 -0600502 vkCmdBindDynamicStateObject(cmd_buf, VK_STATE_BIND_POINT_VIEWPORT, demo->viewport);
503 vkCmdBindDynamicStateObject(cmd_buf, VK_STATE_BIND_POINT_RASTER, demo->raster);
504 vkCmdBindDynamicStateObject(cmd_buf, VK_STATE_BIND_POINT_COLOR_BLEND,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600505 demo->color_blend);
Tony Barbour72304ef2015-04-16 15:59:00 -0600506 vkCmdBindDynamicStateObject(cmd_buf, VK_STATE_BIND_POINT_DEPTH_STENCIL,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600507 demo->depth_stencil);
508
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600509 vkCmdBeginRenderPass(cmd_buf, &rp_begin);
510 clear_range.aspect = VK_IMAGE_ASPECT_COLOR;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600511 clear_range.baseMipLevel = 0;
512 clear_range.mipLevels = 1;
513 clear_range.baseArraySlice = 0;
514 clear_range.arraySize = 1;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600515 vkCmdClearColorImage(cmd_buf,
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600516 demo->buffers[demo->current_buffer].image,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600517 VK_IMAGE_LAYOUT_CLEAR_OPTIMAL,
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600518 clear_color, 1, &clear_range);
519
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600520 clear_range.aspect = VK_IMAGE_ASPECT_DEPTH;
521 vkCmdClearDepthStencil(cmd_buf, demo->depth.image,
522 VK_IMAGE_LAYOUT_CLEAR_OPTIMAL,
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600523 clear_depth, 0, 1, &clear_range);
524
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600525 vkCmdDraw(cmd_buf, 0, 12 * 3, 0, 1);
526 vkCmdEndRenderPass(cmd_buf, rp_begin.renderPass);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600527
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600528 err = vkEndCommandBuffer(cmd_buf);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600529 assert(!err);
Courtney Goeltzenleuchter94901dc2015-02-25 17:53:18 -0700530
Mike Stroyanebae8322015-04-17 12:36:38 -0600531 vkDestroyObject(demo->device, VK_OBJECT_TYPE_RENDER_PASS, rp_begin.renderPass);
532 vkDestroyObject(demo->device, VK_OBJECT_TYPE_FRAMEBUFFER, rp_begin.framebuffer);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600533}
534
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600535
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600536void demo_update_data_buffer(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600537{
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600538 mat4x4 MVP, Model, VP;
539 int matrixSize = sizeof(MVP);
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600540 uint8_t *pData;
Tony Barbourfdc2d352015-04-22 09:02:32 -0600541 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600542
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600543 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600544
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600545 // Rotate 22.5 degrees around the Y axis
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600546 mat4x4_dup(Model, demo->model_matrix);
Piers Daniell735ee532015-02-23 16:23:13 -0700547 mat4x4_rotate(demo->model_matrix, Model, 0.0f, 1.0f, 0.0f, (float)degreesToRadians(demo->spin_angle));
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600548 mat4x4_mul(MVP, VP, demo->model_matrix);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600549
Jon Ashburnae7c21c2015-01-19 15:00:26 -0700550 assert(demo->uniform_data.num_mem == 1);
Mike Stroyanebae8322015-04-17 12:36:38 -0600551 err = vkMapMemory(demo->device, demo->uniform_data.mem[0], 0, 0, 0, (void **) &pData);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600552 assert(!err);
553
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600554 memcpy(pData, (const void*) &MVP[0][0], matrixSize);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600555
Mike Stroyanebae8322015-04-17 12:36:38 -0600556 err = vkUnmapMemory(demo->device, demo->uniform_data.mem[0]);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600557 assert(!err);
558}
559
560static void demo_draw(struct demo *demo)
561{
Chia-I Wucbb564e2015-04-16 22:02:10 +0800562 const VkPresentInfoWSI present = {
563 .sType = VK_STRUCTURE_TYPE_PRESENT_INFO_WSI,
564 .pNext = NULL,
565 .image = demo->buffers[demo->current_buffer].image,
566 .flipInterval = 0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600567 };
Tony Barbourfdc2d352015-04-22 09:02:32 -0600568 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600569
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600570 err = vkQueueSubmit(demo->queue, 1, &demo->buffers[demo->current_buffer].cmd,
571 VK_NULL_HANDLE);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600572 assert(!err);
573
Chia-I Wucbb564e2015-04-16 22:02:10 +0800574 err = vkQueuePresentWSI(demo->queue, &present);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600575 assert(!err);
576
577 demo->current_buffer = (demo->current_buffer + 1) % DEMO_BUFFER_COUNT;
Chia-I Wucbb564e2015-04-16 22:02:10 +0800578
579 err = vkQueueWaitIdle(demo->queue);
580 assert(err == VK_SUCCESS);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600581}
582
583static void demo_prepare_buffers(struct demo *demo)
584{
Chia-I Wucbb564e2015-04-16 22:02:10 +0800585 const VkSwapChainCreateInfoWSI swap_chain = {
586 .sType = VK_STRUCTURE_TYPE_SWAP_CHAIN_CREATE_INFO_WSI,
587 .pNext = NULL,
588 .pNativeWindowSystemHandle = demo->connection,
589 .pNativeWindowHandle = (void *) (intptr_t) demo->window,
590 .imageCount = DEMO_BUFFER_COUNT,
591 .imageFormat = demo->format,
592 .imageExtent = {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600593 .width = demo->width,
594 .height = demo->height,
595 },
Chia-I Wucbb564e2015-04-16 22:02:10 +0800596 .imageArraySize = 1,
597 .imageUsageFlags = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600598 };
Chia-I Wucbb564e2015-04-16 22:02:10 +0800599 VkSwapChainImageInfoWSI images[DEMO_BUFFER_COUNT];
600 size_t images_size = sizeof(images);
Tony Barbourfdc2d352015-04-22 09:02:32 -0600601 VkResult U_ASSERT_ONLY err;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600602 uint32_t i;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600603
Chia-I Wucbb564e2015-04-16 22:02:10 +0800604 err = vkCreateSwapChainWSI(demo->device, &swap_chain, &demo->swap_chain);
605 assert(!err);
606
607 err = vkGetSwapChainInfoWSI(demo->swap_chain,
608 VK_SWAP_CHAIN_INFO_TYPE_PERSISTENT_IMAGES_WSI,
609 &images_size, images);
610 assert(!err && images_size == sizeof(images));
611
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600612 for (i = 0; i < DEMO_BUFFER_COUNT; i++) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600613 VkColorAttachmentViewCreateInfo color_attachment_view = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600614 .sType = VK_STRUCTURE_TYPE_COLOR_ATTACHMENT_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600615 .pNext = NULL,
616 .format = demo->format,
617 .mipLevel = 0,
618 .baseArraySlice = 0,
619 .arraySize = 1,
620 };
621
Chia-I Wucbb564e2015-04-16 22:02:10 +0800622 demo->buffers[i].image = images[i].image;
623 demo->buffers[i].mem = images[i].memory;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600624
Courtney Goeltzenleuchter2c2fbbf2015-04-07 17:13:38 -0600625 demo_add_mem_refs(demo, 1, &demo->buffers[i].mem);
Courtney Goeltzenleuchter1359b7a2015-04-02 16:25:42 -0600626
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600627 demo_set_image_layout(demo, demo->buffers[i].image,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600628 VK_IMAGE_LAYOUT_UNDEFINED,
629 VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600630
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600631 color_attachment_view.image = demo->buffers[i].image;
632
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600633 err = vkCreateColorAttachmentView(demo->device,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600634 &color_attachment_view, &demo->buffers[i].view);
635 assert(!err);
636 }
637}
638
639static void demo_prepare_depth(struct demo *demo)
640{
Tony Barbour72304ef2015-04-16 15:59:00 -0600641 const VkFormat depth_format = VK_FORMAT_D16_UNORM;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600642 const VkImageCreateInfo image = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600643 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600644 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -0600645 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600646 .format = depth_format,
647 .extent = { demo->width, demo->height, 1 },
648 .mipLevels = 1,
649 .arraySize = 1,
650 .samples = 1,
Tony Barbour72304ef2015-04-16 15:59:00 -0600651 .tiling = VK_IMAGE_TILING_OPTIMAL,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600652 .usage = VK_IMAGE_USAGE_DEPTH_STENCIL_BIT,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600653 .flags = 0,
654 };
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -0600655 VkMemoryAllocInfo mem_alloc = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600656 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
Mark Lobodzinskia6907f72015-04-16 08:52:00 -0500657 .pNext = NULL,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600658 .allocationSize = 0,
Tony Barbour72304ef2015-04-16 15:59:00 -0600659 .memProps = VK_MEMORY_PROPERTY_DEVICE_ONLY,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600660 .memPriority = VK_MEMORY_PRIORITY_NORMAL,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600661 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600662 VkDepthStencilViewCreateInfo view = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600663 .sType = VK_STRUCTURE_TYPE_DEPTH_STENCIL_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600664 .pNext = NULL,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600665 .image = VK_NULL_HANDLE,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600666 .mipLevel = 0,
667 .baseArraySlice = 0,
668 .arraySize = 1,
669 .flags = 0,
670 };
Mike Stroyanebae8322015-04-17 12:36:38 -0600671
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600672 VkMemoryRequirements *mem_reqs;
673 size_t mem_reqs_size = sizeof(VkMemoryRequirements);
Tony Barbourfdc2d352015-04-22 09:02:32 -0600674 VkResult U_ASSERT_ONLY err;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600675 uint32_t num_allocations = 0;
676 size_t num_alloc_size = sizeof(num_allocations);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600677
678 demo->depth.format = depth_format;
679
680 /* create image */
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600681 err = vkCreateImage(demo->device, &image,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600682 &demo->depth.image);
683 assert(!err);
684
Mike Stroyanebae8322015-04-17 12:36:38 -0600685 err = vkGetObjectInfo(demo->device,
686 VK_OBJECT_TYPE_IMAGE, demo->depth.image,
687 VK_OBJECT_INFO_TYPE_MEMORY_ALLOCATION_COUNT,
688 &num_alloc_size, &num_allocations);
Jon Ashburnb2a66652015-01-16 09:37:43 -0700689 assert(!err && num_alloc_size == sizeof(num_allocations));
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600690 mem_reqs = malloc(num_allocations * sizeof(VkMemoryRequirements));
Tony Barbour72304ef2015-04-16 15:59:00 -0600691 demo->depth.mem = malloc(num_allocations * sizeof(VkDeviceMemory));
Jon Ashburnb2a66652015-01-16 09:37:43 -0700692 demo->depth.num_mem = num_allocations;
Mike Stroyanebae8322015-04-17 12:36:38 -0600693 err = vkGetObjectInfo(demo->device,
694 VK_OBJECT_TYPE_IMAGE, demo->depth.image,
Tony Barbour72304ef2015-04-16 15:59:00 -0600695 VK_OBJECT_INFO_TYPE_MEMORY_REQUIREMENTS,
Jon Ashburnb2a66652015-01-16 09:37:43 -0700696 &mem_reqs_size, mem_reqs);
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600697 assert(!err && mem_reqs_size == num_allocations * sizeof(VkMemoryRequirements));
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600698 for (uint32_t i = 0; i < num_allocations; i ++) {
Jon Ashburnb2a66652015-01-16 09:37:43 -0700699 mem_alloc.allocationSize = mem_reqs[i].size;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600700
Jon Ashburnb2a66652015-01-16 09:37:43 -0700701 /* allocate memory */
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600702 err = vkAllocMemory(demo->device, &mem_alloc,
Jon Ashburnb2a66652015-01-16 09:37:43 -0700703 &(demo->depth.mem[i]));
704 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600705
Jon Ashburnb2a66652015-01-16 09:37:43 -0700706 /* bind memory */
Mike Stroyanebae8322015-04-17 12:36:38 -0600707 err = vkQueueBindObjectMemory(demo->queue,
708 VK_OBJECT_TYPE_IMAGE, demo->depth.image,
709 i, demo->depth.mem[i], 0);
Jon Ashburnb2a66652015-01-16 09:37:43 -0700710 assert(!err);
711 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600712
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600713 demo_set_image_layout(demo, demo->depth.image,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600714 VK_IMAGE_LAYOUT_UNDEFINED,
715 VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600716
Courtney Goeltzenleuchter2c2fbbf2015-04-07 17:13:38 -0600717 demo_add_mem_refs(demo, demo->depth.num_mem, demo->depth.mem);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600718
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600719 /* create image view */
720 view.image = demo->depth.image;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600721 err = vkCreateDepthStencilView(demo->device, &view,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600722 &demo->depth.view);
723 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600724}
725
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600726/** loadTexture
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -0600727 * loads a png file into an memory object, using cstdio , libpng.
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600728 *
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -0600729 * \param demo : Needed to access VK calls
730 * \param filename : the png file to be loaded
731 * \param width : width of png, to be updated as a side effect of this function
732 * \param height : height of png, to be updated as a side effect of this function
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600733 *
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -0600734 * \return bool : an opengl texture id. true if successful?,
735 * should be validated by the client of this function.
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600736 *
737 * Source: http://en.wikibooks.org/wiki/OpenGL_Programming/Intermediate/Textures
738 * Modified to copy image to memory
739 *
740 */
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700741bool loadTexture(const char *filename, uint8_t *rgba_data,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600742 VkSubresourceLayout *layout,
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600743 int32_t *width, int32_t *height)
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600744{
745 //header for testing if it is a png
746 png_byte header[8];
Tony Barbourf9921732015-04-22 11:36:22 -0600747 int is_png, bit_depth, color_type, rowbytes;
748 size_t retval;
Ian Elliott1e42dff2015-02-13 14:29:21 -0700749 png_uint_32 i, twidth, theight;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600750 png_structp png_ptr;
751 png_infop info_ptr, end_info;
752 png_byte *image_data;
753 png_bytep *row_pointers;
754
755 //open file as binary
756 FILE *fp = fopen(filename, "rb");
757 if (!fp) {
758 return false;
759 }
760
761 //read the header
Tony Barbourfdc2d352015-04-22 09:02:32 -0600762 retval = fread(header, 1, 8, fp);
763 if (retval != 8) {
764 fclose(fp);
765 return false;
766 }
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600767
768 //test if png
769 is_png = !png_sig_cmp(header, 0, 8);
770 if (!is_png) {
771 fclose(fp);
772 return false;
773 }
774
775 //create png struct
776 png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL,
777 NULL, NULL);
778 if (!png_ptr) {
779 fclose(fp);
780 return (false);
781 }
782
783 //create png info struct
784 info_ptr = png_create_info_struct(png_ptr);
785 if (!info_ptr) {
786 png_destroy_read_struct(&png_ptr, (png_infopp) NULL, (png_infopp) NULL);
787 fclose(fp);
788 return (false);
789 }
790
791 //create png info struct
792 end_info = png_create_info_struct(png_ptr);
793 if (!end_info) {
794 png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp) NULL);
795 fclose(fp);
796 return (false);
797 }
798
799 //png error stuff, not sure libpng man suggests this.
800 if (setjmp(png_jmpbuf(png_ptr))) {
801 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
802 fclose(fp);
803 return (false);
804 }
805
806 //init png reading
807 png_init_io(png_ptr, fp);
808
809 //let libpng know you already read the first 8 bytes
810 png_set_sig_bytes(png_ptr, 8);
811
812 // read all the info up to the image data
813 png_read_info(png_ptr, info_ptr);
814
815 // get info about png
816 png_get_IHDR(png_ptr, info_ptr, &twidth, &theight, &bit_depth, &color_type,
817 NULL, NULL, NULL);
818
819 //update width and height based on png info
820 *width = twidth;
821 *height = theight;
822
823 // Require that incoming texture be 8bits per color component
824 // and 4 components (RGBA).
825 if (png_get_bit_depth(png_ptr, info_ptr) != 8 ||
826 png_get_channels(png_ptr, info_ptr) != 4) {
827 return false;
828 }
829
830 if (rgba_data == NULL) {
831 // If data pointer is null, we just want the width & height
832 // clean up memory and close stuff
833 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
834 fclose(fp);
835
836 return true;
837 }
838
839 // Update the png info struct.
840 png_read_update_info(png_ptr, info_ptr);
841
842 // Row size in bytes.
843 rowbytes = png_get_rowbytes(png_ptr, info_ptr);
844
845 // Allocate the image_data as a big block, to be given to opengl
846 image_data = (png_byte *)malloc(rowbytes * theight * sizeof(png_byte));
847 if (!image_data) {
848 //clean up memory and close stuff
849 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
850 fclose(fp);
851 return false;
852 }
853
854 // row_pointers is for pointing to image_data for reading the png with libpng
855 row_pointers = (png_bytep *)malloc(theight * sizeof(png_bytep));
856 if (!row_pointers) {
857 //clean up memory and close stuff
858 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
859 // delete[] image_data;
860 fclose(fp);
861 return false;
862 }
863 // set the individual row_pointers to point at the correct offsets of image_data
864 for (i = 0; i < theight; ++i)
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700865 row_pointers[theight - 1 - i] = rgba_data + i * layout->rowPitch;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600866
867 // read the png into image_data through row_pointers
868 png_read_image(png_ptr, row_pointers);
869
870 // clean up memory and close stuff
871 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
872 free(row_pointers);
873 free(image_data);
874 fclose(fp);
875
876 return true;
877}
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600878
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700879static void demo_prepare_texture_image(struct demo *demo,
880 const char *filename,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600881 struct texture_object *tex_obj,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600882 VkImageTiling tiling,
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -0600883 VkImageUsageFlags usage,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600884 VkFlags mem_props)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600885{
Tony Barbour72304ef2015-04-16 15:59:00 -0600886 const VkFormat tex_format = VK_FORMAT_B8G8R8A8_UNORM;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600887 int32_t tex_width;
888 int32_t tex_height;
Tony Barbourfdc2d352015-04-22 09:02:32 -0600889 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700890
David Pinedo30bd71d2015-04-23 08:16:57 -0600891 if (!loadTexture(filename, NULL, NULL, &tex_width, &tex_height))
892 {
893 printf("Failed to load textures\n");
894 fflush(stdout);
895 exit(1);
896 }
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700897
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600898 tex_obj->tex_width = tex_width;
899 tex_obj->tex_height = tex_height;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700900
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600901 const VkImageCreateInfo image_create_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600902 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700903 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -0600904 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700905 .format = tex_format,
906 .extent = { tex_width, tex_height, 1 },
907 .mipLevels = 1,
908 .arraySize = 1,
909 .samples = 1,
910 .tiling = tiling,
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -0600911 .usage = usage,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700912 .flags = 0,
913 };
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -0600914 VkMemoryAllocInfo mem_alloc = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600915 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
Mark Lobodzinskia6907f72015-04-16 08:52:00 -0500916 .pNext = NULL,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700917 .allocationSize = 0,
918 .memProps = mem_props,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600919 .memPriority = VK_MEMORY_PRIORITY_NORMAL,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700920 };
921
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600922 VkMemoryRequirements *mem_reqs;
923 size_t mem_reqs_size = sizeof(VkMemoryRequirements);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700924 uint32_t num_allocations = 0;
925 size_t num_alloc_size = sizeof(num_allocations);
926
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600927 err = vkCreateImage(demo->device, &image_create_info,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600928 &tex_obj->image);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700929 assert(!err);
930
Mike Stroyanebae8322015-04-17 12:36:38 -0600931 err = vkGetObjectInfo(demo->device,
932 VK_OBJECT_TYPE_IMAGE, tex_obj->image,
Tony Barbour72304ef2015-04-16 15:59:00 -0600933 VK_OBJECT_INFO_TYPE_MEMORY_ALLOCATION_COUNT,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700934 &num_alloc_size, &num_allocations);
935 assert(!err && num_alloc_size == sizeof(num_allocations));
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600936 mem_reqs = malloc(num_allocations * sizeof(VkMemoryRequirements));
Tony Barbour72304ef2015-04-16 15:59:00 -0600937 tex_obj->mem = malloc(num_allocations * sizeof(VkDeviceMemory));
Mike Stroyanebae8322015-04-17 12:36:38 -0600938 err = vkGetObjectInfo(demo->device,
939 VK_OBJECT_TYPE_IMAGE, tex_obj->image,
Tony Barbour72304ef2015-04-16 15:59:00 -0600940 VK_OBJECT_INFO_TYPE_MEMORY_REQUIREMENTS,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700941 &mem_reqs_size, mem_reqs);
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600942 assert(!err && mem_reqs_size == num_allocations * sizeof(VkMemoryRequirements));
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700943 for (uint32_t j = 0; j < num_allocations; j ++) {
Piers Daniell735ee532015-02-23 16:23:13 -0700944 mem_alloc.allocationSize = mem_reqs[j].size;
945
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700946 /* allocate memory */
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600947 err = vkAllocMemory(demo->device, &mem_alloc,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600948 &(tex_obj->mem[j]));
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700949 assert(!err);
950
951 /* bind memory */
Mike Stroyanebae8322015-04-17 12:36:38 -0600952 err = vkQueueBindObjectMemory(demo->queue,
953 VK_OBJECT_TYPE_IMAGE, tex_obj->image,
954 j, tex_obj->mem[j], 0);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700955 assert(!err);
956 }
957 free(mem_reqs);
958 mem_reqs = NULL;
959
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600960 tex_obj->num_mem = num_allocations;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700961
Tony Barbour72304ef2015-04-16 15:59:00 -0600962 if (mem_props & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600963 const VkImageSubresource subres = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600964 .aspect = VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700965 .mipLevel = 0,
966 .arraySlice = 0,
967 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600968 VkSubresourceLayout layout;
969 size_t layout_size = sizeof(VkSubresourceLayout);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700970 void *data;
971
Mike Stroyanebae8322015-04-17 12:36:38 -0600972 err = vkGetImageSubresourceInfo(demo->device, tex_obj->image, &subres,
Tony Barbour72304ef2015-04-16 15:59:00 -0600973 VK_SUBRESOURCE_INFO_TYPE_LAYOUT,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700974 &layout_size, &layout);
975 assert(!err && layout_size == sizeof(layout));
976 /* Linear texture must be within a single memory object */
977 assert(num_allocations == 1);
978
Mike Stroyanebae8322015-04-17 12:36:38 -0600979 err = vkMapMemory(demo->device, tex_obj->mem[0], 0, 0, 0, &data);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700980 assert(!err);
981
982 if (!loadTexture(filename, data, &layout, &tex_width, &tex_height)) {
983 fprintf(stderr, "Error loading texture: %s\n", filename);
984 }
985
Mike Stroyanebae8322015-04-17 12:36:38 -0600986 err = vkUnmapMemory(demo->device, tex_obj->mem[0]);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700987 assert(!err);
988 }
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600989
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600990 tex_obj->imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600991 demo_set_image_layout(demo, tex_obj->image,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600992 VK_IMAGE_LAYOUT_UNDEFINED,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600993 tex_obj->imageLayout);
994 /* 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 -0700995}
996
Mark Lobodzinskida9b1092015-04-16 11:44:05 -0500997static void demo_destroy_texture_image(struct demo *demo, struct texture_object *tex_objs)
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700998{
999 /* clean up staging resources */
1000 for (uint32_t j = 0; j < tex_objs->num_mem; j ++) {
Mike Stroyanebae8322015-04-17 12:36:38 -06001001 vkQueueBindObjectMemory(demo->queue,
1002 VK_OBJECT_TYPE_IMAGE, tex_objs->image, j, VK_NULL_HANDLE, 0);
1003 vkFreeMemory(demo->device, tex_objs->mem[j]);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001004 }
1005
1006 free(tex_objs->mem);
Mike Stroyanebae8322015-04-17 12:36:38 -06001007 vkDestroyObject(demo->device, VK_OBJECT_TYPE_IMAGE, tex_objs->image);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001008}
1009
1010static void demo_prepare_textures(struct demo *demo)
1011{
Tony Barbour72304ef2015-04-16 15:59:00 -06001012 const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001013 VkFormatProperties props;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001014 size_t size = sizeof(props);
Tony Barbourfdc2d352015-04-22 09:02:32 -06001015 VkResult U_ASSERT_ONLY err;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001016 uint32_t i;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001017
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001018 err = vkGetFormatInfo(demo->device, tex_format,
Tony Barbour72304ef2015-04-16 15:59:00 -06001019 VK_FORMAT_INFO_TYPE_PROPERTIES,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001020 &size, &props);
1021 assert(!err);
1022
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001023 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001024
Tony Barbour72304ef2015-04-16 15:59:00 -06001025 if (props.linearTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT && !demo->use_staging_buffer) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001026 /* Device can texture using linear textures */
1027 demo_prepare_texture_image(demo, tex_files[i], &demo->textures[i],
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001028 VK_IMAGE_TILING_LINEAR, VK_IMAGE_USAGE_SAMPLED_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
Tony Barbour72304ef2015-04-16 15:59:00 -06001029 } else if (props.optimalTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001030 /* Must use staging buffer to copy linear texture to optimized */
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001031 struct texture_object staging_texture;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001032
1033 memset(&staging_texture, 0, sizeof(staging_texture));
1034 demo_prepare_texture_image(demo, tex_files[i], &staging_texture,
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001035 VK_IMAGE_TILING_LINEAR, VK_IMAGE_USAGE_TRANSFER_SOURCE_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001036
1037 demo_prepare_texture_image(demo, tex_files[i], &demo->textures[i],
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001038 VK_IMAGE_TILING_OPTIMAL,
1039 (VK_IMAGE_USAGE_TRANSFER_DESTINATION_BIT | VK_IMAGE_USAGE_SAMPLED_BIT),
1040 VK_MEMORY_PROPERTY_DEVICE_ONLY);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001041
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001042 demo_set_image_layout(demo, staging_texture.image,
1043 staging_texture.imageLayout,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001044 VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001045
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001046 demo_set_image_layout(demo, demo->textures[i].image,
1047 demo->textures[i].imageLayout,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001048 VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001049
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001050 VkImageCopy copy_region = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001051 .srcSubresource = { VK_IMAGE_ASPECT_COLOR, 0, 0 },
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001052 .srcOffset = { 0, 0, 0 },
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001053 .destSubresource = { VK_IMAGE_ASPECT_COLOR, 0, 0 },
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001054 .destOffset = { 0, 0, 0 },
1055 .extent = { staging_texture.tex_width, staging_texture.tex_height, 1 },
1056 };
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001057 vkCmdCopyImage(demo->cmd,
1058 staging_texture.image, VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL,
1059 demo->textures[i].image, VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL,
Courtney Goeltzenleuchter8e89a312015-03-25 11:25:10 -06001060 1, &copy_region);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001061
Courtney Goeltzenleuchter2c2fbbf2015-04-07 17:13:38 -06001062 demo_add_mem_refs(demo, staging_texture.num_mem, staging_texture.mem);
1063 demo_add_mem_refs(demo, demo->textures[i].num_mem, demo->textures[i].mem);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001064
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001065 demo_set_image_layout(demo, demo->textures[i].image,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001066 VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001067 demo->textures[i].imageLayout);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001068
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001069 demo_flush_init_cmd(demo);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001070
Courtney Goeltzenleuchter1359b7a2015-04-02 16:25:42 -06001071 demo_remove_mem_refs(demo, staging_texture.num_mem, staging_texture.mem);
Courtney Goeltzenleuchterf3aeb2b2015-04-21 09:30:03 -06001072 demo_destroy_texture_image(demo, &staging_texture);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001073 } else {
Tony Barbour72304ef2015-04-16 15:59:00 -06001074 /* Can't support VK_FORMAT_B8G8R8A8_UNORM !? */
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001075 assert(!"No support for tB8G8R8A8_UNORM as texture image format");
1076 }
1077
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001078 const VkSamplerCreateInfo sampler = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001079 .sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001080 .pNext = NULL,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001081 .magFilter = VK_TEX_FILTER_NEAREST,
1082 .minFilter = VK_TEX_FILTER_NEAREST,
Tony Barbour72304ef2015-04-16 15:59:00 -06001083 .mipMode = VK_TEX_MIPMAP_MODE_BASE,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001084 .addressU = VK_TEX_ADDRESS_CLAMP,
1085 .addressV = VK_TEX_ADDRESS_CLAMP,
1086 .addressW = VK_TEX_ADDRESS_CLAMP,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001087 .mipLodBias = 0.0f,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001088 .maxAnisotropy = 1,
Tony Barbour72304ef2015-04-16 15:59:00 -06001089 .compareOp = VK_COMPARE_OP_NEVER,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001090 .minLod = 0.0f,
1091 .maxLod = 0.0f,
Tony Barbour72304ef2015-04-16 15:59:00 -06001092 .borderColor = VK_BORDER_COLOR_OPAQUE_WHITE,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001093 };
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001094
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001095 VkImageViewCreateInfo view = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001096 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001097 .pNext = NULL,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001098 .image = VK_NULL_HANDLE,
Tony Barbour72304ef2015-04-16 15:59:00 -06001099 .viewType = VK_IMAGE_VIEW_TYPE_2D,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001100 .format = tex_format,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001101 .channels = { VK_CHANNEL_SWIZZLE_R,
1102 VK_CHANNEL_SWIZZLE_G,
1103 VK_CHANNEL_SWIZZLE_B,
1104 VK_CHANNEL_SWIZZLE_A, },
1105 .subresourceRange = { VK_IMAGE_ASPECT_COLOR, 0, 1, 0, 1 },
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001106 .minLod = 0.0f,
1107 };
Jon Ashburnb2a66652015-01-16 09:37:43 -07001108
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001109 /* create sampler */
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001110 err = vkCreateSampler(demo->device, &sampler,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001111 &demo->textures[i].sampler);
1112 assert(!err);
1113
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001114 /* create image view */
1115 view.image = demo->textures[i].image;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001116 err = vkCreateImageView(demo->device, &view,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001117 &demo->textures[i].view);
1118 assert(!err);
1119 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001120}
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001121
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001122void demo_prepare_cube_data_buffer(struct demo *demo)
1123{
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -06001124 VkBufferCreateInfo buf_info;
1125 VkBufferViewCreateInfo view_info;
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -06001126 VkMemoryAllocInfo alloc_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001127 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
Mark Lobodzinskia6907f72015-04-16 08:52:00 -05001128 .pNext = NULL,
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001129 .allocationSize = 0,
Tony Barbour72304ef2015-04-16 15:59:00 -06001130 .memProps = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001131 .memPriority = VK_MEMORY_PRIORITY_NORMAL,
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001132 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001133 VkMemoryRequirements *mem_reqs;
1134 size_t mem_reqs_size = sizeof(VkMemoryRequirements);
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001135 uint32_t num_allocations = 0;
1136 size_t num_alloc_size = sizeof(num_allocations);
1137 uint8_t *pData;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001138 int i;
1139 mat4x4 MVP, VP;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001140 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001141 struct vktexcube_vs_uniform data;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001142
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001143 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001144 mat4x4_mul(MVP, VP, demo->model_matrix);
1145 memcpy(data.mvp, MVP, sizeof(MVP));
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001146// dumpMatrix("MVP", MVP);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001147
1148 for (i=0; i<12*3; i++) {
1149 data.position[i][0] = g_vertex_buffer_data[i*3];
1150 data.position[i][1] = g_vertex_buffer_data[i*3+1];
1151 data.position[i][2] = g_vertex_buffer_data[i*3+2];
1152 data.position[i][3] = 1.0f;
1153 data.attr[i][0] = g_uv_buffer_data[2*i];
1154 data.attr[i][1] = g_uv_buffer_data[2*i + 1];
1155 data.attr[i][2] = 0;
1156 data.attr[i][3] = 0;
1157 }
1158
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001159 memset(&buf_info, 0, sizeof(buf_info));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001160 buf_info.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001161 buf_info.size = sizeof(data);
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -06001162 buf_info.usage = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001163 err = vkCreateBuffer(demo->device, &buf_info, &demo->uniform_data.buf);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001164 assert(!err);
1165
Mike Stroyanebae8322015-04-17 12:36:38 -06001166 err = vkGetObjectInfo(demo->device,
1167 VK_OBJECT_TYPE_BUFFER, demo->uniform_data.buf,
Tony Barbour72304ef2015-04-16 15:59:00 -06001168 VK_OBJECT_INFO_TYPE_MEMORY_ALLOCATION_COUNT,
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001169 &num_alloc_size, &num_allocations);
1170 assert(!err && num_alloc_size == sizeof(num_allocations));
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001171 mem_reqs = malloc(num_allocations * sizeof(VkMemoryRequirements));
Tony Barbour72304ef2015-04-16 15:59:00 -06001172 demo->uniform_data.mem = malloc(num_allocations * sizeof(VkDeviceMemory));
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001173 demo->uniform_data.num_mem = num_allocations;
Mike Stroyanebae8322015-04-17 12:36:38 -06001174 err = vkGetObjectInfo(demo->device,
1175 VK_OBJECT_TYPE_BUFFER, demo->uniform_data.buf,
Tony Barbour72304ef2015-04-16 15:59:00 -06001176 VK_OBJECT_INFO_TYPE_MEMORY_REQUIREMENTS,
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001177 &mem_reqs_size, mem_reqs);
1178 assert(!err && mem_reqs_size == num_allocations * sizeof(*mem_reqs));
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001179 for (uint32_t i = 0; i < num_allocations; i ++) {
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001180 alloc_info.allocationSize = mem_reqs[i].size;
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001181
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001182 err = vkAllocMemory(demo->device, &alloc_info, &(demo->uniform_data.mem[i]));
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001183 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001184
Mike Stroyanebae8322015-04-17 12:36:38 -06001185 err = vkMapMemory(demo->device, demo->uniform_data.mem[i], 0, 0, 0, (void **) &pData);
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001186 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001187
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001188 memcpy(pData, &data, sizeof data);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001189
Mike Stroyanebae8322015-04-17 12:36:38 -06001190 err = vkUnmapMemory(demo->device, demo->uniform_data.mem[i]);
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001191 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001192
Mike Stroyanebae8322015-04-17 12:36:38 -06001193 err = vkQueueBindObjectMemory(demo->queue,
1194 VK_OBJECT_TYPE_BUFFER, demo->uniform_data.buf,
1195 i, demo->uniform_data.mem[i], 0);
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001196 assert(!err);
1197 }
Courtney Goeltzenleuchter2c2fbbf2015-04-07 17:13:38 -06001198 demo_add_mem_refs(demo, demo->uniform_data.num_mem, demo->uniform_data.mem);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001199
1200 memset(&view_info, 0, sizeof(view_info));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001201 view_info.sType = VK_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO;
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001202 view_info.buffer = demo->uniform_data.buf;
Tony Barbour72304ef2015-04-16 15:59:00 -06001203 view_info.viewType = VK_BUFFER_VIEW_TYPE_RAW;
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001204 view_info.offset = 0;
1205 view_info.range = sizeof(data);
1206
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001207 err = vkCreateBufferView(demo->device, &view_info, &demo->uniform_data.view);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001208 assert(!err);
1209
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001210 demo->uniform_data.attach.sType = VK_STRUCTURE_TYPE_BUFFER_VIEW_ATTACH_INFO;
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001211 demo->uniform_data.attach.view = demo->uniform_data.view;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001212}
1213
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001214static void demo_prepare_descriptor_layout(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001215{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001216 const VkDescriptorSetLayoutBinding layout_bindings[2] = {
Chia-I Wua2aa8632015-03-26 15:04:41 +08001217 [0] = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001218 .descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
Chia-I Wua2aa8632015-03-26 15:04:41 +08001219 .count = 1,
Tony Barbour72304ef2015-04-16 15:59:00 -06001220 .stageFlags = VK_SHADER_STAGE_VERTEX_BIT,
Chia-I Wu91e8e212015-03-27 12:56:09 +08001221 .pImmutableSamplers = NULL,
Chia-I Wua2aa8632015-03-26 15:04:41 +08001222 },
1223 [1] = {
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -06001224 .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
Chia-I Wua2aa8632015-03-26 15:04:41 +08001225 .count = DEMO_TEXTURE_COUNT,
Tony Barbour72304ef2015-04-16 15:59:00 -06001226 .stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT,
Chia-I Wu91e8e212015-03-27 12:56:09 +08001227 .pImmutableSamplers = NULL,
Chia-I Wua2aa8632015-03-26 15:04:41 +08001228 },
1229 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001230 const VkDescriptorSetLayoutCreateInfo descriptor_layout = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001231 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001232 .pNext = NULL,
Chia-I Wua2aa8632015-03-26 15:04:41 +08001233 .count = 2,
1234 .pBinding = layout_bindings,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001235 };
Tony Barbourfdc2d352015-04-22 09:02:32 -06001236 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001237
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001238 err = vkCreateDescriptorSetLayout(demo->device,
Chia-I Wub58c24a2015-03-26 15:27:55 +08001239 &descriptor_layout, &demo->desc_layout);
1240 assert(!err);
1241
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001242 const VkPipelineLayoutCreateInfo pPipelineLayoutCreateInfo = {
1243 .sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
1244 .pNext = NULL,
1245 .descriptorSetCount = 1,
1246 .pSetLayouts = &demo->desc_layout,
1247 };
1248
1249 err = vkCreatePipelineLayout(demo->device,
1250 &pPipelineLayoutCreateInfo,
1251 &demo->pipeline_layout);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001252 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001253}
1254
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001255static VkShader demo_prepare_shader(struct demo *demo,
Tony Barbour72304ef2015-04-16 15:59:00 -06001256 VkShaderStage stage,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001257 const void *code,
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001258 size_t size)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001259{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001260 VkShaderCreateInfo createInfo;
1261 VkShader shader;
1262 VkResult err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001263
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001264
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001265 createInfo.sType = VK_STRUCTURE_TYPE_SHADER_CREATE_INFO;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001266 createInfo.pNext = NULL;
1267
Cody Northrop8b12a1f2015-03-17 15:55:58 -06001268#ifdef EXTERNAL_SPV
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001269 createInfo.codeSize = size;
1270 createInfo.pCode = code;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001271 createInfo.flags = 0;
1272
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001273 err = vkCreateShader(demo->device, &createInfo, &shader);
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001274 if (err) {
1275 free((void *) createInfo.pCode);
1276 }
1277#else
Cody Northrop8b12a1f2015-03-17 15:55:58 -06001278 // Create fake SPV structure to feed GLSL
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001279 // to the driver "under the covers"
1280 createInfo.codeSize = 3 * sizeof(uint32_t) + size + 1;
1281 createInfo.pCode = malloc(createInfo.codeSize);
1282 createInfo.flags = 0;
1283
Tony Barbour72304ef2015-04-16 15:59:00 -06001284 /* try version 0 first: VkShaderStage followed by GLSL */
Cody Northrop8b12a1f2015-03-17 15:55:58 -06001285 ((uint32_t *) createInfo.pCode)[0] = ICD_SPV_MAGIC;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001286 ((uint32_t *) createInfo.pCode)[1] = 0;
1287 ((uint32_t *) createInfo.pCode)[2] = stage;
1288 memcpy(((uint32_t *) createInfo.pCode + 3), code, size + 1);
1289
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001290 err = vkCreateShader(demo->device, &createInfo, &shader);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001291 if (err) {
1292 free((void *) createInfo.pCode);
Tony Barbourf9921732015-04-22 11:36:22 -06001293 return (VkShader) VK_NULL_HANDLE;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001294 }
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001295#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001296
1297 return shader;
1298}
1299
Cody Northrop8b12a1f2015-03-17 15:55:58 -06001300char *demo_read_spv(const char *filename, size_t *psize)
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001301{
1302 long int size;
Tony Barbourf9921732015-04-22 11:36:22 -06001303 size_t U_ASSERT_ONLY retval;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001304 void *shader_code;
1305
1306 FILE *fp = fopen(filename, "rb");
1307 if (!fp) return NULL;
1308
1309 fseek(fp, 0L, SEEK_END);
1310 size = ftell(fp);
1311
1312 fseek(fp, 0L, SEEK_SET);
1313
1314 shader_code = malloc(size);
Tony Barbourfdc2d352015-04-22 09:02:32 -06001315 retval = fread(shader_code, size, 1, fp);
1316 assert(retval == 1);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001317
1318 *psize = size;
1319
1320 return shader_code;
1321}
1322
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001323static VkShader demo_prepare_vs(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001324{
Cody Northrop8b12a1f2015-03-17 15:55:58 -06001325#ifdef EXTERNAL_SPV
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001326 void *vertShaderCode;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001327 size_t size;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001328
Cody Northrop8b12a1f2015-03-17 15:55:58 -06001329 vertShaderCode = demo_read_spv("cube-vert.spv", &size);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001330
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001331 return demo_prepare_shader(demo, VK_SHADER_STAGE_VERTEX,
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001332 vertShaderCode, size);
1333#else
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001334 static const char *vertShaderText =
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001335 "#version 140\n"
1336 "#extension GL_ARB_separate_shader_objects : enable\n"
1337 "#extension GL_ARB_shading_language_420pack : enable\n"
1338 "\n"
1339 "layout(binding = 0) uniform buf {\n"
1340 " mat4 MVP;\n"
1341 " vec4 position[12*3];\n"
1342 " vec4 attr[12*3];\n"
1343 "} ubuf;\n"
1344 "\n"
1345 "layout (location = 0) out vec4 texcoord;\n"
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001346 "\n"
1347 "void main() \n"
1348 "{\n"
1349 " texcoord = ubuf.attr[gl_VertexID];\n"
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001350 " gl_Position = ubuf.MVP * ubuf.position[gl_VertexID];\n"
1351 "}\n";
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001352
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001353 return demo_prepare_shader(demo, VK_SHADER_STAGE_VERTEX,
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001354 (const void *) vertShaderText,
1355 strlen(vertShaderText));
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001356#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001357}
1358
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001359static VkShader demo_prepare_fs(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001360{
Cody Northrop8b12a1f2015-03-17 15:55:58 -06001361#ifdef EXTERNAL_SPV
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001362 void *fragShaderCode;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001363 size_t size;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001364
Cody Northrop8b12a1f2015-03-17 15:55:58 -06001365 fragShaderCode = demo_read_spv("cube-frag.spv", &size);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001366
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001367 return demo_prepare_shader(demo, VK_SHADER_STAGE_FRAGMENT,
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001368 fragShaderCode, size);
1369#else
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001370 static const char *fragShaderText =
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001371 "#version 140\n"
1372 "#extension GL_ARB_separate_shader_objects : enable\n"
1373 "#extension GL_ARB_shading_language_420pack : enable\n"
Courtney Goeltzenleuchter4d1acf12015-02-25 15:13:35 -07001374 "layout (binding = 1) uniform sampler2D tex;\n"
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001375 "\n"
1376 "layout (location = 0) in vec4 texcoord;\n"
1377 "void main() {\n"
1378 " gl_FragColor = texture(tex, texcoord.xy);\n"
1379 "}\n";
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001380
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001381 return demo_prepare_shader(demo, VK_SHADER_STAGE_FRAGMENT,
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001382 (const void *) fragShaderText,
1383 strlen(fragShaderText));
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001384#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001385}
1386
1387static void demo_prepare_pipeline(struct demo *demo)
1388{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001389 VkGraphicsPipelineCreateInfo pipeline;
1390 VkPipelineIaStateCreateInfo ia;
1391 VkPipelineRsStateCreateInfo rs;
1392 VkPipelineCbStateCreateInfo cb;
1393 VkPipelineDsStateCreateInfo ds;
1394 VkPipelineShaderStageCreateInfo vs;
1395 VkPipelineShaderStageCreateInfo fs;
1396 VkPipelineVpStateCreateInfo vp;
1397 VkPipelineMsStateCreateInfo ms;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001398 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001399
1400 memset(&pipeline, 0, sizeof(pipeline));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001401 pipeline.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001402 pipeline.layout = demo->pipeline_layout;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001403
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001404 memset(&ia, 0, sizeof(ia));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001405 ia.sType = VK_STRUCTURE_TYPE_PIPELINE_IA_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06001406 ia.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001407
1408 memset(&rs, 0, sizeof(rs));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001409 rs.sType = VK_STRUCTURE_TYPE_PIPELINE_RS_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06001410 rs.fillMode = VK_FILL_MODE_SOLID;
1411 rs.cullMode = VK_CULL_MODE_BACK;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001412 rs.frontFace = VK_FRONT_FACE_CCW;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001413
1414 memset(&cb, 0, sizeof(cb));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001415 cb.sType = VK_STRUCTURE_TYPE_PIPELINE_CB_STATE_CREATE_INFO;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001416 VkPipelineCbAttachmentState att_state[1];
Tony Barbour29645d02015-01-16 14:27:35 -07001417 memset(att_state, 0, sizeof(att_state));
1418 att_state[0].format = demo->format;
1419 att_state[0].channelWriteMask = 0xf;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001420 att_state[0].blendEnable = VK_FALSE;
Tony Barbour29645d02015-01-16 14:27:35 -07001421 cb.attachmentCount = 1;
1422 cb.pAttachments = att_state;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001423
Tony Barbour29645d02015-01-16 14:27:35 -07001424 memset(&vp, 0, sizeof(vp));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001425 vp.sType = VK_STRUCTURE_TYPE_PIPELINE_VP_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06001426 vp.viewportCount = 1;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001427 vp.clipOrigin = VK_COORDINATE_ORIGIN_LOWER_LEFT;
Tony Barbour29645d02015-01-16 14:27:35 -07001428
1429 memset(&ds, 0, sizeof(ds));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001430 ds.sType = VK_STRUCTURE_TYPE_PIPELINE_DS_STATE_CREATE_INFO;
Tony Barbour29645d02015-01-16 14:27:35 -07001431 ds.format = demo->depth.format;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001432 ds.depthTestEnable = VK_TRUE;
1433 ds.depthWriteEnable = VK_TRUE;
Tony Barbour72304ef2015-04-16 15:59:00 -06001434 ds.depthCompareOp = VK_COMPARE_OP_LESS_EQUAL;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001435 ds.depthBoundsEnable = VK_FALSE;
1436 ds.back.stencilFailOp = VK_STENCIL_OP_KEEP;
1437 ds.back.stencilPassOp = VK_STENCIL_OP_KEEP;
Tony Barbour72304ef2015-04-16 15:59:00 -06001438 ds.back.stencilCompareOp = VK_COMPARE_OP_ALWAYS;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001439 ds.stencilTestEnable = VK_FALSE;
Tony Barbour29645d02015-01-16 14:27:35 -07001440 ds.front = ds.back;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001441
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001442 memset(&vs, 0, sizeof(vs));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001443 vs.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1444 vs.shader.stage = VK_SHADER_STAGE_VERTEX;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001445 vs.shader.shader = demo_prepare_vs(demo);
Mike Stroyanebae8322015-04-17 12:36:38 -06001446 assert(vs.shader.shader != VK_NULL_HANDLE);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001447
1448 memset(&fs, 0, sizeof(fs));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001449 fs.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1450 fs.shader.stage = VK_SHADER_STAGE_FRAGMENT;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001451 fs.shader.shader = demo_prepare_fs(demo);
Mike Stroyanebae8322015-04-17 12:36:38 -06001452 assert(fs.shader.shader != VK_NULL_HANDLE);
Tony Barbour29645d02015-01-16 14:27:35 -07001453
1454 memset(&ms, 0, sizeof(ms));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001455 ms.sType = VK_STRUCTURE_TYPE_PIPELINE_MS_STATE_CREATE_INFO;
Tony Barbour29645d02015-01-16 14:27:35 -07001456 ms.sampleMask = 1;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001457 ms.multisampleEnable = VK_FALSE;
Tony Barbour29645d02015-01-16 14:27:35 -07001458 ms.samples = 1;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001459
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001460 pipeline.pNext = (const void *) &ia;
1461 ia.pNext = (const void *) &rs;
1462 rs.pNext = (const void *) &cb;
1463 cb.pNext = (const void *) &ms;
1464 ms.pNext = (const void *) &vp;
1465 vp.pNext = (const void *) &ds;
1466 ds.pNext = (const void *) &vs;
1467 vs.pNext = (const void *) &fs;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001468
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001469 err = vkCreateGraphicsPipeline(demo->device, &pipeline, &demo->pipeline);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001470 assert(!err);
1471
Mike Stroyanebae8322015-04-17 12:36:38 -06001472 vkDestroyObject(demo->device, VK_OBJECT_TYPE_SHADER, vs.shader.shader);
1473 vkDestroyObject(demo->device, VK_OBJECT_TYPE_SHADER, fs.shader.shader);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001474}
1475
1476static void demo_prepare_dynamic_states(struct demo *demo)
1477{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001478 VkDynamicVpStateCreateInfo viewport_create;
1479 VkDynamicRsStateCreateInfo raster;
1480 VkDynamicCbStateCreateInfo color_blend;
1481 VkDynamicDsStateCreateInfo depth_stencil;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001482 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001483
Tony Barbour29645d02015-01-16 14:27:35 -07001484 memset(&viewport_create, 0, sizeof(viewport_create));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001485 viewport_create.sType = VK_STRUCTURE_TYPE_DYNAMIC_VP_STATE_CREATE_INFO;
Courtney Goeltzenleuchter1710d8d2015-02-11 14:13:34 -07001486 viewport_create.viewportAndScissorCount = 1;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001487 VkViewport viewport;
Piers Daniell735ee532015-02-23 16:23:13 -07001488 memset(&viewport, 0, sizeof(viewport));
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001489 viewport.height = (float) demo->height;
1490 viewport.width = (float) demo->width;
1491 viewport.minDepth = (float) 0.0f;
1492 viewport.maxDepth = (float) 1.0f;
Piers Daniell735ee532015-02-23 16:23:13 -07001493 viewport_create.pViewports = &viewport;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001494 VkRect scissor;
Piers Daniell735ee532015-02-23 16:23:13 -07001495 memset(&scissor, 0, sizeof(scissor));
Courtney Goeltzenleuchter1710d8d2015-02-11 14:13:34 -07001496 scissor.extent.width = demo->width;
1497 scissor.extent.height = demo->height;
1498 scissor.offset.x = 0;
1499 scissor.offset.y = 0;
Courtney Goeltzenleuchter1710d8d2015-02-11 14:13:34 -07001500 viewport_create.pScissors = &scissor;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001501
1502 memset(&raster, 0, sizeof(raster));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001503 raster.sType = VK_STRUCTURE_TYPE_DYNAMIC_RS_STATE_CREATE_INFO;
Piers Daniell735ee532015-02-23 16:23:13 -07001504 raster.pointSize = 1.0;
1505 raster.lineWidth = 1.0;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001506
1507 memset(&color_blend, 0, sizeof(color_blend));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001508 color_blend.sType = VK_STRUCTURE_TYPE_DYNAMIC_CB_STATE_CREATE_INFO;
Piers Daniell735ee532015-02-23 16:23:13 -07001509 color_blend.blendConst[0] = 1.0f;
1510 color_blend.blendConst[1] = 1.0f;
1511 color_blend.blendConst[2] = 1.0f;
1512 color_blend.blendConst[3] = 1.0f;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001513
1514 memset(&depth_stencil, 0, sizeof(depth_stencil));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001515 depth_stencil.sType = VK_STRUCTURE_TYPE_DYNAMIC_DS_STATE_CREATE_INFO;
Piers Daniell735ee532015-02-23 16:23:13 -07001516 depth_stencil.minDepth = 0.0f;
1517 depth_stencil.maxDepth = 1.0f;
Tony Barbour29645d02015-01-16 14:27:35 -07001518 depth_stencil.stencilBackRef = 0;
1519 depth_stencil.stencilFrontRef = 0;
1520 depth_stencil.stencilReadMask = 0xff;
1521 depth_stencil.stencilWriteMask = 0xff;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001522
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001523 err = vkCreateDynamicViewportState(demo->device, &viewport_create, &demo->viewport);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001524 assert(!err);
1525
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001526 err = vkCreateDynamicRasterState(demo->device, &raster, &demo->raster);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001527 assert(!err);
1528
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001529 err = vkCreateDynamicColorBlendState(demo->device,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001530 &color_blend, &demo->color_blend);
1531 assert(!err);
1532
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001533 err = vkCreateDynamicDepthStencilState(demo->device,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001534 &depth_stencil, &demo->depth_stencil);
1535 assert(!err);
1536}
1537
Chia-I Wu63ea9262015-03-26 13:14:16 +08001538static void demo_prepare_descriptor_pool(struct demo *demo)
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001539{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001540 const VkDescriptorTypeCount type_counts[2] = {
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001541 [0] = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001542 .type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001543 .count = 1,
1544 },
1545 [1] = {
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -06001546 .type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001547 .count = DEMO_TEXTURE_COUNT,
1548 },
1549 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001550 const VkDescriptorPoolCreateInfo descriptor_pool = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001551 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001552 .pNext = NULL,
1553 .count = 2,
1554 .pTypeCount = type_counts,
1555 };
Tony Barbourfdc2d352015-04-22 09:02:32 -06001556 VkResult U_ASSERT_ONLY err;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001557
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001558 err = vkCreateDescriptorPool(demo->device,
1559 VK_DESCRIPTOR_POOL_USAGE_ONE_SHOT, 1,
Chia-I Wu63ea9262015-03-26 13:14:16 +08001560 &descriptor_pool, &demo->desc_pool);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001561 assert(!err);
1562}
1563
1564static void demo_prepare_descriptor_set(struct demo *demo)
1565{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001566 VkImageViewAttachInfo view_info[DEMO_TEXTURE_COUNT];
1567 VkSamplerImageViewInfo combined_info[DEMO_TEXTURE_COUNT];
1568 VkUpdateSamplerTextures update_fs;
1569 VkUpdateBuffers update_vs;
Chia-I Wub58c24a2015-03-26 15:27:55 +08001570 const void *update_array[2] = { &update_vs, &update_fs };
Tony Barbourfdc2d352015-04-22 09:02:32 -06001571 VkResult U_ASSERT_ONLY err;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001572 uint32_t count;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001573 uint32_t i;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001574
1575 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001576 view_info[i].sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_ATTACH_INFO;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001577 view_info[i].pNext = NULL;
1578 view_info[i].view = demo->textures[i].view,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001579 view_info[i].layout = VK_IMAGE_LAYOUT_GENERAL;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001580
Courtney Goeltzenleuchtereb4754f2015-04-09 11:43:10 -06001581 combined_info[i].sampler = demo->textures[i].sampler;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001582 combined_info[i].pImageView = &view_info[i];
1583 }
1584
1585 memset(&update_vs, 0, sizeof(update_vs));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001586 update_vs.sType = VK_STRUCTURE_TYPE_UPDATE_BUFFERS;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001587 update_vs.pNext = &update_fs;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001588 update_vs.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001589 update_vs.count = 1;
Chia-I Wub58c24a2015-03-26 15:27:55 +08001590 update_vs.pBufferViews = &demo->uniform_data.attach;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001591
1592 memset(&update_fs, 0, sizeof(update_fs));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001593 update_fs.sType = VK_STRUCTURE_TYPE_UPDATE_SAMPLER_TEXTURES;
Chia-I Wub58c24a2015-03-26 15:27:55 +08001594 update_fs.binding = 1;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001595 update_fs.count = DEMO_TEXTURE_COUNT;
1596 update_fs.pSamplerImageViews = combined_info;
1597
Mike Stroyanebae8322015-04-17 12:36:38 -06001598 err = vkAllocDescriptorSets(demo->device, demo->desc_pool,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001599 VK_DESCRIPTOR_SET_USAGE_STATIC,
Chia-I Wu87544e72015-02-23 10:41:08 -07001600 1, &demo->desc_layout,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001601 &demo->desc_set, &count);
1602 assert(!err && count == 1);
1603
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001604 vkBeginDescriptorPoolUpdate(demo->device,
1605 VK_DESCRIPTOR_UPDATE_MODE_FASTEST);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001606
Mike Stroyanebae8322015-04-17 12:36:38 -06001607 vkClearDescriptorSets(demo->device, demo->desc_pool, 1, &demo->desc_set);
1608 vkUpdateDescriptors(demo->device, demo->desc_set, 2, update_array);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001609
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001610 vkEndDescriptorPoolUpdate(demo->device, demo->buffers[demo->current_buffer].cmd);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001611}
1612
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001613static void demo_prepare(struct demo *demo)
1614{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001615 const VkCmdBufferCreateInfo cmd = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001616 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001617 .pNext = NULL,
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07001618 .queueNodeIndex = demo->graphics_queue_node_index,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001619 .flags = 0,
1620 };
Tony Barbourfdc2d352015-04-22 09:02:32 -06001621 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001622
1623 demo_prepare_buffers(demo);
1624 demo_prepare_depth(demo);
1625 demo_prepare_textures(demo);
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001626 demo_prepare_cube_data_buffer(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001627
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001628 demo_prepare_descriptor_layout(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001629 demo_prepare_pipeline(demo);
1630 demo_prepare_dynamic_states(demo);
1631
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001632 for (int i = 0; i < DEMO_BUFFER_COUNT; i++) {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001633 err = vkCreateCommandBuffer(demo->device, &cmd, &demo->buffers[i].cmd);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001634 assert(!err);
1635 }
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001636
Chia-I Wu63ea9262015-03-26 13:14:16 +08001637 demo_prepare_descriptor_pool(demo);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001638 demo_prepare_descriptor_set(demo);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001639
1640
1641 for (int i = 0; i < DEMO_BUFFER_COUNT; i++) {
1642 demo->current_buffer = i;
1643 demo_draw_build_cmd(demo, demo->buffers[i].cmd);
1644 }
1645
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001646 /*
1647 * Prepare functions above may generate pipeline commands
1648 * that need to be flushed before beginning the render loop.
1649 */
1650 demo_flush_init_cmd(demo);
1651
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001652 demo->current_buffer = 0;
Jon Ashburn8d26c062015-04-24 09:46:24 -07001653 demo->prepared = true;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001654}
1655
Ian Elliott639ca472015-04-16 15:23:05 -06001656#ifdef _WIN32
1657static void demo_run(struct demo *demo)
1658{
Courtney Goeltzenleuchterb7e22702015-04-27 14:56:34 -06001659 if (!demo->prepared)
1660 return;
Ian Elliott639ca472015-04-16 15:23:05 -06001661 // Wait for work to finish before updating MVP.
1662 vkDeviceWaitIdle(demo->device);
1663 demo_update_data_buffer(demo);
1664
1665 demo_draw(demo);
1666
1667 // Wait for work to finish before updating MVP.
1668 vkDeviceWaitIdle(demo->device);
1669}
1670
1671// On MS-Windows, make this a global, so it's available to WndProc()
1672struct demo demo;
1673
1674// MS-Windows event handling function:
1675LRESULT CALLBACK WndProc(HWND hWnd,
1676 UINT uMsg,
1677 WPARAM wParam,
1678 LPARAM lParam)
1679{
Ian Elliott44e33f72015-04-28 10:52:52 -06001680 char tmp_str[] = APP_LONG_NAME;
Ian Elliott639ca472015-04-16 15:23:05 -06001681
1682 switch(uMsg)
1683 {
1684 case WM_CREATE:
1685 return 0;
1686 case WM_CLOSE:
1687 PostQuitMessage(0);
1688 return 0;
1689 case WM_PAINT:
1690 demo_run(&demo);
1691 return 0;
1692 default:
1693 break;
1694 }
1695 return (DefWindowProc(hWnd, uMsg, wParam, lParam));
1696}
1697
1698static void demo_create_window(struct demo *demo)
1699{
1700 WNDCLASSEX win_class;
1701
1702 // Initialize the window class structure:
1703 win_class.cbSize = sizeof(WNDCLASSEX);
1704 win_class.style = CS_HREDRAW | CS_VREDRAW;
1705 win_class.lpfnWndProc = WndProc;
1706 win_class.cbClsExtra = 0;
1707 win_class.cbWndExtra = 0;
1708 win_class.hInstance = demo->connection; // hInstance
1709 win_class.hIcon = LoadIcon(NULL, IDI_APPLICATION);
1710 win_class.hCursor = LoadCursor(NULL, IDC_ARROW);
1711 win_class.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
1712 win_class.lpszMenuName = NULL;
1713 win_class.lpszClassName = demo->name;
1714 win_class.hIconSm = LoadIcon(NULL, IDI_WINLOGO);
1715 // Register window class:
1716 if (!RegisterClassEx(&win_class)) {
1717 // It didn't work, so try to give a useful error:
1718 printf("Unexpected error trying to start the application!\n");
1719 fflush(stdout);
1720 exit(1);
1721 }
1722 // Create window with the registered class:
1723 demo->window = CreateWindowEx(0,
1724 demo->name, // class name
1725 demo->name, // app name
1726 WS_OVERLAPPEDWINDOW | // window style
1727 WS_VISIBLE |
1728 WS_SYSMENU,
1729 100,100, // x/y coords
1730 demo->width, // width
1731 demo->height, // height
1732 NULL, // handle to parent
1733 NULL, // handle to menu
1734 demo->connection, // hInstance
1735 NULL); // no extra parameters
1736 if (!demo->window) {
1737 // It didn't work, so try to give a useful error:
1738 printf("Cannot create a window in which to draw!\n");
1739 fflush(stdout);
1740 exit(1);
1741 }
1742}
1743#else // _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001744static void demo_handle_event(struct demo *demo,
1745 const xcb_generic_event_t *event)
1746{
Piers Daniell735ee532015-02-23 16:23:13 -07001747 uint8_t event_code = event->response_type & 0x7f;
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07001748 switch (event_code) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001749 case XCB_EXPOSE:
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07001750 // TODO: Resize window
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001751 break;
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07001752 case XCB_CLIENT_MESSAGE:
1753 if((*(xcb_client_message_event_t*)event).data.data32[0] ==
1754 (*demo->atom_wm_delete_window).atom) {
1755 demo->quit = true;
1756 }
1757 break;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001758 case XCB_KEY_RELEASE:
1759 {
1760 const xcb_key_release_event_t *key =
1761 (const xcb_key_release_event_t *) event;
1762
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001763 switch (key->detail) {
1764 case 0x9: // Escape
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001765 demo->quit = true;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001766 break;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001767 case 0x71: // left arrow key
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001768 demo->spin_angle += demo->spin_increment;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001769 break;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001770 case 0x72: // right arrow key
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001771 demo->spin_angle -= demo->spin_increment;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001772 break;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001773 case 0x41:
1774 demo->pause = !demo->pause;
Piers Daniell735ee532015-02-23 16:23:13 -07001775 break;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001776 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001777 }
1778 break;
1779 default:
1780 break;
1781 }
1782}
1783
1784static void demo_run(struct demo *demo)
1785{
1786 xcb_flush(demo->connection);
1787
1788 while (!demo->quit) {
1789 xcb_generic_event_t *event;
1790
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07001791 if (demo->pause) {
1792 event = xcb_wait_for_event(demo->connection);
1793 } else {
1794 event = xcb_poll_for_event(demo->connection);
1795 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001796 if (event) {
1797 demo_handle_event(demo, event);
1798 free(event);
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07001799 }
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07001800
1801 // Wait for work to finish before updating MVP.
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001802 vkDeviceWaitIdle(demo->device);
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07001803 demo_update_data_buffer(demo);
1804
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07001805 demo_draw(demo);
Courtney Goeltzenleuchter1454f3c2014-11-18 11:28:09 -07001806
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07001807 // Wait for work to finish before updating MVP.
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001808 vkDeviceWaitIdle(demo->device);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001809 }
1810}
1811
1812static void demo_create_window(struct demo *demo)
1813{
1814 uint32_t value_mask, value_list[32];
1815
1816 demo->window = xcb_generate_id(demo->connection);
1817
1818 value_mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK;
1819 value_list[0] = demo->screen->black_pixel;
1820 value_list[1] = XCB_EVENT_MASK_KEY_RELEASE |
1821 XCB_EVENT_MASK_EXPOSURE;
1822
1823 xcb_create_window(demo->connection,
1824 XCB_COPY_FROM_PARENT,
1825 demo->window, demo->screen->root,
1826 0, 0, demo->width, demo->height, 0,
1827 XCB_WINDOW_CLASS_INPUT_OUTPUT,
1828 demo->screen->root_visual,
1829 value_mask, value_list);
1830
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07001831 /* Magic code that will send notification when window is destroyed */
1832 xcb_intern_atom_cookie_t cookie = xcb_intern_atom(demo->connection, 1, 12,
1833 "WM_PROTOCOLS");
1834 xcb_intern_atom_reply_t* reply = xcb_intern_atom_reply(demo->connection, cookie, 0);
1835
1836 xcb_intern_atom_cookie_t cookie2 = xcb_intern_atom(demo->connection, 0, 16, "WM_DELETE_WINDOW");
1837 demo->atom_wm_delete_window = xcb_intern_atom_reply(demo->connection, cookie2, 0);
1838
1839 xcb_change_property(demo->connection, XCB_PROP_MODE_REPLACE,
1840 demo->window, (*reply).atom, 4, 32, 1,
1841 &(*demo->atom_wm_delete_window).atom);
1842 free(reply);
1843
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001844 xcb_map_window(demo->connection, demo->window);
1845}
Ian Elliott639ca472015-04-16 15:23:05 -06001846#endif // _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001847
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001848static void demo_init_vk(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001849{
Tobin Ehlis99239dc2015-04-16 18:04:57 -06001850 VkResult err;
1851 // Extensions to enable
1852 const char *ext_names[] = {
Chia-I Wucbb564e2015-04-16 22:02:10 +08001853 "VK_WSI_LunarG",
Tobin Ehlis99239dc2015-04-16 18:04:57 -06001854 };
1855 size_t extSize = sizeof(uint32_t);
1856 uint32_t extCount = 0;
1857 err = vkGetGlobalExtensionInfo(VK_EXTENSION_INFO_TYPE_COUNT, 0, &extSize, &extCount);
1858 assert(!err);
1859
1860 VkExtensionProperties extProp;
1861 extSize = sizeof(VkExtensionProperties);
Tony Barbourfdc2d352015-04-22 09:02:32 -06001862 bool32_t U_ASSERT_ONLY extFound = 0;
Tobin Ehlis99239dc2015-04-16 18:04:57 -06001863 for (uint32_t i = 0; i < extCount; i++) {
1864 err = vkGetGlobalExtensionInfo(VK_EXTENSION_INFO_TYPE_PROPERTIES, i, &extSize, &extProp);
1865 if (!strcmp(ext_names[0], extProp.extName))
1866 extFound = 1;
1867 }
Ian Elliott65152912015-04-28 13:22:33 -06001868 if (!extFound) {
1869 ERR_EXIT("vkGetGlobalExtensionInfo failed to find the "
1870 "\"VK_WSI_LunarG\" extension.\n\nDo you have a compatible "
1871 "Vulkan installable client driver (ICD) installed?\nPlease "
1872 "look at the Getting Started guide for additional "
1873 "information.\n",
1874 "vkCreateInstance Failure");
1875 }
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001876 const VkApplicationInfo app = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001877 .sType = VK_STRUCTURE_TYPE_APPLICATION_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001878 .pNext = NULL,
Ian Elliott44e33f72015-04-28 10:52:52 -06001879 .pAppName = APP_SHORT_NAME,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001880 .appVersion = 0,
Ian Elliott44e33f72015-04-28 10:52:52 -06001881 .pEngineName = APP_SHORT_NAME,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001882 .engineVersion = 0,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001883 .apiVersion = VK_API_VERSION,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001884 };
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -06001885 const VkInstanceCreateInfo inst_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001886 .sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
Jon Ashburnab46b362015-04-04 14:52:07 -06001887 .pNext = NULL,
1888 .pAppInfo = &app,
1889 .pAllocCb = NULL,
Tobin Ehlis99239dc2015-04-16 18:04:57 -06001890 .extensionCount = 1,
1891 .ppEnabledExtensionNames = ext_names,
Jon Ashburnab46b362015-04-04 14:52:07 -06001892 };
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -06001893 const VkDeviceQueueCreateInfo queue = {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001894 .queueNodeIndex = 0,
1895 .queueCount = 1,
1896 };
Ian Elliott097d9f32015-04-28 11:35:02 -06001897
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -06001898 const VkDeviceCreateInfo device = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001899 .sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001900 .pNext = NULL,
1901 .queueRecordCount = 1,
1902 .pRequestedQueues = &queue,
Tobin Ehlis99239dc2015-04-16 18:04:57 -06001903 .extensionCount = 1,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001904 .ppEnabledExtensionNames = ext_names,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001905 .flags = VK_DEVICE_CREATE_VALIDATION_BIT,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001906 };
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001907 uint32_t gpu_count;
1908 uint32_t i;
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07001909 size_t data_size;
1910 uint32_t queue_count;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001911
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001912 err = vkCreateInstance(&inst_info, &demo->inst);
Ian Elliott07264132015-04-28 11:35:02 -06001913 if (err == VK_ERROR_INCOMPATIBLE_DRIVER) {
1914 ERR_EXIT("Cannot find a compatible Vulkan installable client driver "
Ian Elliott65152912015-04-28 13:22:33 -06001915 "(ICD).\n\nPlease look at the Getting Started guide for "
Ian Elliott07264132015-04-28 11:35:02 -06001916 "additional information.\n",
1917 "vkCreateInstance Failure");
1918 } else if (err) {
Ian Elliott65152912015-04-28 13:22:33 -06001919 ERR_EXIT("vkCreateInstance failed.\n\nDo you have a compatible Vulkan "
1920 "installable client driver (ICD) installed?\nPlease look at "
Ian Elliott07264132015-04-28 11:35:02 -06001921 "the Getting Started guide for additional information.\n",
1922 "vkCreateInstance Failure");
Ian Elliott3979e282015-04-03 15:24:55 -06001923 }
Jon Ashburnab46b362015-04-04 14:52:07 -06001924
Jon Ashburn07c0c0c2015-04-15 11:31:12 -06001925 gpu_count = 1;
1926 err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, &demo->gpu);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001927 assert(!err && gpu_count == 1);
1928
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001929 err = vkCreateDevice(demo->gpu, &device, &demo->device);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001930 assert(!err);
1931
Tony Barbour72304ef2015-04-16 15:59:00 -06001932 err = vkGetPhysicalDeviceInfo(demo->gpu, VK_PHYSICAL_DEVICE_INFO_TYPE_PROPERTIES,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001933 &data_size, NULL);
1934 assert(!err);
1935
Tony Barbour72304ef2015-04-16 15:59:00 -06001936 demo->gpu_props = (VkPhysicalDeviceProperties *) malloc(data_size);
1937 err = vkGetPhysicalDeviceInfo(demo->gpu, VK_PHYSICAL_DEVICE_INFO_TYPE_PROPERTIES,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001938 &data_size, demo->gpu_props);
1939 assert(!err);
1940
Tony Barbour72304ef2015-04-16 15:59:00 -06001941 err = vkGetPhysicalDeviceInfo(demo->gpu, VK_PHYSICAL_DEVICE_INFO_TYPE_QUEUE_PROPERTIES,
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07001942 &data_size, NULL);
1943 assert(!err);
1944
Tony Barbour72304ef2015-04-16 15:59:00 -06001945 demo->queue_props = (VkPhysicalDeviceQueueProperties *) malloc(data_size);
1946 err = vkGetPhysicalDeviceInfo(demo->gpu, VK_PHYSICAL_DEVICE_INFO_TYPE_QUEUE_PROPERTIES,
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07001947 &data_size, demo->queue_props);
1948 assert(!err);
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001949 queue_count = (uint32_t)(data_size / sizeof(VkPhysicalDeviceQueueProperties));
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07001950 assert(queue_count >= 1);
1951
Mark Lobodzinskida9b1092015-04-16 11:44:05 -05001952 // Graphics queue and MemMgr queue can be separate.
1953 // TODO: Add support for separate queues, including synchronization,
1954 // and appropriate tracking for QueueSubmit and QueueBindObjectMemory
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07001955 for (i = 0; i < queue_count; i++) {
Mark Lobodzinskida9b1092015-04-16 11:44:05 -05001956 if ((demo->queue_props[i].queueFlags & VK_QUEUE_GRAPHICS_BIT) &&
1957 (demo->queue_props[i].queueFlags & VK_QUEUE_MEMMGR_BIT) )
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07001958 break;
1959 }
1960 assert(i < queue_count);
1961 demo->graphics_queue_node_index = i;
1962
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001963 err = vkGetDeviceQueue(demo->device, demo->graphics_queue_node_index,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001964 0, &demo->queue);
1965 assert(!err);
1966}
1967
1968static void demo_init_connection(struct demo *demo)
1969{
Ian Elliott639ca472015-04-16 15:23:05 -06001970#ifndef _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001971 const xcb_setup_t *setup;
1972 xcb_screen_iterator_t iter;
1973 int scr;
1974
1975 demo->connection = xcb_connect(NULL, &scr);
Ian Elliott3979e282015-04-03 15:24:55 -06001976 if (demo->connection == NULL) {
1977 printf("Cannot find a compatible Vulkan installable client driver "
1978 "(ICD).\nExiting ...\n");
1979 fflush(stdout);
1980 exit(1);
1981 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001982
1983 setup = xcb_get_setup(demo->connection);
1984 iter = xcb_setup_roots_iterator(setup);
1985 while (scr-- > 0)
1986 xcb_screen_next(&iter);
1987
1988 demo->screen = iter.data;
Ian Elliott639ca472015-04-16 15:23:05 -06001989#endif // _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001990}
1991
Ian Elliott639ca472015-04-16 15:23:05 -06001992#ifdef _WIN32
1993static void demo_init(struct demo *demo, HINSTANCE hInstance, LPSTR pCmdLine)
1994#else // _WIN32
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001995static void demo_init(struct demo *demo, int argc, char **argv)
Ian Elliott639ca472015-04-16 15:23:05 -06001996#endif // _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001997{
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001998 vec3 eye = {0.0f, 3.0f, 5.0f};
1999 vec3 origin = {0, 0, 0};
2000 vec3 up = {0.0f, -1.0f, 0.0};
Ian Elliott639ca472015-04-16 15:23:05 -06002001 bool argv_error = false;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002002
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002003 memset(demo, 0, sizeof(*demo));
2004
Ian Elliott639ca472015-04-16 15:23:05 -06002005#ifdef _WIN32
2006 demo->connection = hInstance;
Ian Elliott44e33f72015-04-28 10:52:52 -06002007 strncpy(demo->name, APP_SHORT_NAME, APP_NAME_STR_LEN);
Ian Elliott639ca472015-04-16 15:23:05 -06002008
2009 if (strncmp(pCmdLine, "--use_staging", strlen("--use_staging")) == 0)
2010 demo->use_staging_buffer = true;
2011 else if (strlen(pCmdLine) != 0) {
2012 fprintf(stderr, "Do not recognize argument \"%s\".\n", pCmdLine);
2013 argv_error = true;
2014 }
2015#else // _WIN32
Piers Daniell735ee532015-02-23 16:23:13 -07002016 for (int i = 1; i < argc; i++) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002017 if (strncmp(argv[i], "--use_staging", strlen("--use_staging")) == 0)
2018 demo->use_staging_buffer = true;
Ian Elliott639ca472015-04-16 15:23:05 -06002019 else {
2020 fprintf(stderr, "Do not recognize argument \"%s\".\n", argv[i]);
2021 argv_error = true;
2022 }
2023 }
2024#endif // _WIN32
2025 if (argv_error) {
Ian Elliott44e33f72015-04-28 10:52:52 -06002026 fprintf(stderr, "Usage:\n %s [--use_staging]\n", APP_SHORT_NAME);
Ian Elliott639ca472015-04-16 15:23:05 -06002027 fflush(stderr);
2028 exit(1);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002029 }
2030
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002031 demo_init_connection(demo);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002032 demo_init_vk(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002033
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002034 demo->width = 500;
2035 demo->height = 500;
Tony Barbour72304ef2015-04-16 15:59:00 -06002036 demo->format = VK_FORMAT_B8G8R8A8_UNORM;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002037
2038 demo->spin_angle = 0.01f;
2039 demo->spin_increment = 0.01f;
2040 demo->pause = false;
2041
Piers Daniell735ee532015-02-23 16:23:13 -07002042 mat4x4_perspective(demo->projection_matrix, (float)degreesToRadians(45.0f), 1.0f, 0.1f, 100.0f);
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002043 mat4x4_look_at(demo->view_matrix, eye, origin, up);
2044 mat4x4_identity(demo->model_matrix);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002045}
2046
2047static void demo_cleanup(struct demo *demo)
2048{
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06002049 uint32_t i, j;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002050
Courtney Goeltzenleuchterb7e22702015-04-27 14:56:34 -06002051 demo->prepared = false;
2052
Mike Stroyanebae8322015-04-17 12:36:38 -06002053 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DESCRIPTOR_SET, demo->desc_set);
2054 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DESCRIPTOR_POOL, demo->desc_pool);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08002055
Mike Stroyanebae8322015-04-17 12:36:38 -06002056 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DYNAMIC_VP_STATE, demo->viewport);
2057 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DYNAMIC_RS_STATE, demo->raster);
2058 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DYNAMIC_CB_STATE, demo->color_blend);
2059 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DYNAMIC_DS_STATE, demo->depth_stencil);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002060
Mike Stroyanebae8322015-04-17 12:36:38 -06002061 vkDestroyObject(demo->device, VK_OBJECT_TYPE_PIPELINE, demo->pipeline);
2062 vkDestroyObject(demo->device, VK_OBJECT_TYPE_PIPELINE_LAYOUT, demo->pipeline_layout);
2063 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DESCRIPTOR_SET_LAYOUT, demo->desc_layout);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002064
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002065 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Mike Stroyanebae8322015-04-17 12:36:38 -06002066 vkDestroyObject(demo->device, VK_OBJECT_TYPE_IMAGE_VIEW, demo->textures[i].view);
2067 vkQueueBindObjectMemory(demo->queue, VK_OBJECT_TYPE_IMAGE, demo->textures[i].image, 0, VK_NULL_HANDLE, 0);
2068 vkDestroyObject(demo->device, VK_OBJECT_TYPE_IMAGE, demo->textures[i].image);
Courtney Goeltzenleuchter1359b7a2015-04-02 16:25:42 -06002069 demo_remove_mem_refs(demo, demo->textures[i].num_mem, demo->textures[i].mem);
Jon Ashburnb2a66652015-01-16 09:37:43 -07002070 for (j = 0; j < demo->textures[i].num_mem; j++)
Mike Stroyanebae8322015-04-17 12:36:38 -06002071 vkFreeMemory(demo->device, demo->textures[i].mem[j]);
2072 free(demo->textures[i].mem);
2073 vkDestroyObject(demo->device, VK_OBJECT_TYPE_SAMPLER, demo->textures[i].sampler);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002074 }
Chia-I Wucbb564e2015-04-16 22:02:10 +08002075 vkDestroySwapChainWSI(demo->swap_chain);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002076
Mike Stroyanebae8322015-04-17 12:36:38 -06002077 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DEPTH_STENCIL_VIEW, demo->depth.view);
2078 vkQueueBindObjectMemory(demo->queue, VK_OBJECT_TYPE_IMAGE, demo->depth.image, 0, VK_NULL_HANDLE, 0);
Courtney Goeltzenleuchter1359b7a2015-04-02 16:25:42 -06002079 demo_remove_mem_refs(demo, demo->depth.num_mem, demo->depth.mem);
Mike Stroyanebae8322015-04-17 12:36:38 -06002080 vkDestroyObject(demo->device, VK_OBJECT_TYPE_IMAGE, demo->depth.image);
Courtney Goeltzenleuchter1359b7a2015-04-02 16:25:42 -06002081 for (j = 0; j < demo->depth.num_mem; j++) {
Mike Stroyanebae8322015-04-17 12:36:38 -06002082 vkFreeMemory(demo->device, demo->depth.mem[j]);
Courtney Goeltzenleuchter1359b7a2015-04-02 16:25:42 -06002083 }
Mark Lobodzinski7b8fee62015-02-18 16:38:17 -06002084
Mike Stroyanebae8322015-04-17 12:36:38 -06002085 vkDestroyObject(demo->device, VK_OBJECT_TYPE_BUFFER_VIEW, demo->uniform_data.view);
2086 vkQueueBindObjectMemory(demo->queue, VK_OBJECT_TYPE_BUFFER, demo->uniform_data.buf, 0, VK_NULL_HANDLE, 0);
2087 vkDestroyObject(demo->device, VK_OBJECT_TYPE_BUFFER, demo->uniform_data.buf);
Courtney Goeltzenleuchter1359b7a2015-04-02 16:25:42 -06002088 demo_remove_mem_refs(demo, demo->uniform_data.num_mem, demo->uniform_data.mem);
Jon Ashburnae7c21c2015-01-19 15:00:26 -07002089 for (j = 0; j < demo->uniform_data.num_mem; j++)
Mike Stroyanebae8322015-04-17 12:36:38 -06002090 vkFreeMemory(demo->device, demo->uniform_data.mem[j]);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002091
2092 for (i = 0; i < DEMO_BUFFER_COUNT; i++) {
Mike Stroyanebae8322015-04-17 12:36:38 -06002093 vkDestroyObject(demo->device, VK_OBJECT_TYPE_COLOR_ATTACHMENT_VIEW, demo->buffers[i].view);
2094 vkDestroyObject(demo->device, VK_OBJECT_TYPE_COMMAND_BUFFER, demo->buffers[i].cmd);
Courtney Goeltzenleuchter1359b7a2015-04-02 16:25:42 -06002095 demo_remove_mem_refs(demo, 1, &demo->buffers[i].mem);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002096 }
2097
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002098 vkDestroyDevice(demo->device);
2099 vkDestroyInstance(demo->inst);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002100
Ian Elliott639ca472015-04-16 15:23:05 -06002101#ifndef _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002102 xcb_destroy_window(demo->connection, demo->window);
2103 xcb_disconnect(demo->connection);
Ian Elliott639ca472015-04-16 15:23:05 -06002104#endif // _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002105}
2106
Ian Elliott639ca472015-04-16 15:23:05 -06002107#ifdef _WIN32
Ian Elliottf9cf78c2015-04-28 10:33:11 -06002108
Ian Elliotta748eaf2015-04-28 15:50:36 -06002109int WINAPI WinMain(HINSTANCE hInstance,
2110 HINSTANCE hPrevInstance,
2111 LPSTR pCmdLine,
2112 int nCmdShow)
Ian Elliott639ca472015-04-16 15:23:05 -06002113{
2114 MSG msg; // message
2115 bool done; // flag saying when app is complete
2116
2117 demo_init(&demo, hInstance, pCmdLine);
2118 demo_create_window(&demo);
2119
2120 demo_prepare(&demo);
2121
2122 done = false; //initialize loop condition variable
2123 /* main message loop*/
2124 while(!done)
2125 {
Ian Elliotta748eaf2015-04-28 15:50:36 -06002126 PeekMessage(&msg, NULL, 0, 0, PM_REMOVE);
Ian Elliott639ca472015-04-16 15:23:05 -06002127 if (msg.message == WM_QUIT) //check for a quit message
2128 {
2129 done = true; //if found, quit app
2130 }
2131 else
2132 {
2133 /* Translate and dispatch to event queue*/
2134 TranslateMessage(&msg);
2135 DispatchMessage(&msg);
2136 }
2137 }
2138
2139 demo_cleanup(&demo);
2140
Tony Barbourf9921732015-04-22 11:36:22 -06002141 return (int) msg.wParam;
Ian Elliott639ca472015-04-16 15:23:05 -06002142}
2143#else // _WIN32
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002144int main(int argc, char **argv)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002145{
2146 struct demo demo;
2147
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002148 demo_init(&demo, argc, argv);
Chia-I Wucbb564e2015-04-16 22:02:10 +08002149 demo_create_window(&demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002150
2151 demo_prepare(&demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002152 demo_run(&demo);
2153
2154 demo_cleanup(&demo);
2155
2156 return 0;
2157}
Ian Elliott639ca472015-04-16 15:23:05 -06002158#endif // _WIN32