blob: 33a9389c4f26592ae161f0d7ff231a571369c572 [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
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -050090 VkDeviceMemory mem;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -060091 VkImageView view;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -070092 int32_t tex_width, tex_height;
93};
94
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -060095static char *tex_files[] = {
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -060096 "lunarg-logo-256x256-solid.png"
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -060097};
98
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -060099struct vkcube_vs_uniform {
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600100 // Must start with MVP
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600101 float mvp[4][4];
102 float position[12*3][4];
103 float color[12*3][4];
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600104};
105
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600106struct vktexcube_vs_uniform {
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600107 // Must start with MVP
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600108 float mvp[4][4];
109 float position[12*3][4];
110 float attr[12*3][4];
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600111};
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600112
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600113//--------------------------------------------------------------------------------------
114// Mesh and VertexFormat Data
115//--------------------------------------------------------------------------------------
116struct Vertex
117{
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600118 float posX, posY, posZ, posW; // Position data
119 float r, g, b, a; // Color
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600120};
121
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600122struct VertexPosTex
123{
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600124 float posX, posY, posZ, posW; // Position data
125 float u, v, s, t; // Texcoord
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600126};
127
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600128#define XYZ1(_x_, _y_, _z_) (_x_), (_y_), (_z_), 1.f
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600129#define UV(_u_, _v_) (_u_), (_v_), 0.f, 1.f
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600130
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600131static const float g_vertex_buffer_data[] = {
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800132 -1.0f,-1.0f,-1.0f, // -X side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600133 -1.0f,-1.0f, 1.0f,
134 -1.0f, 1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800135 -1.0f, 1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600136 -1.0f, 1.0f,-1.0f,
137 -1.0f,-1.0f,-1.0f,
138
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800139 -1.0f,-1.0f,-1.0f, // -Z side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600140 1.0f, 1.0f,-1.0f,
141 1.0f,-1.0f,-1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800142 -1.0f,-1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600143 -1.0f, 1.0f,-1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600144 1.0f, 1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600145
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800146 -1.0f,-1.0f,-1.0f, // -Y side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600147 1.0f,-1.0f,-1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600148 1.0f,-1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800149 -1.0f,-1.0f,-1.0f,
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
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800153 -1.0f, 1.0f,-1.0f, // +Y side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600154 -1.0f, 1.0f, 1.0f,
155 1.0f, 1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800156 -1.0f, 1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600157 1.0f, 1.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600158 1.0f, 1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600159
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800160 1.0f, 1.0f,-1.0f, // +X side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600161 1.0f, 1.0f, 1.0f,
162 1.0f,-1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800163 1.0f,-1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600164 1.0f,-1.0f,-1.0f,
165 1.0f, 1.0f,-1.0f,
166
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800167 -1.0f, 1.0f, 1.0f, // +Z side
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600168 -1.0f,-1.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600169 1.0f, 1.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800170 -1.0f,-1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600171 1.0f,-1.0f, 1.0f,
172 1.0f, 1.0f, 1.0f,
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600173};
174
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600175static const float g_uv_buffer_data[] = {
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800176 0.0f, 0.0f, // -X side
177 1.0f, 0.0f,
178 1.0f, 1.0f,
179 1.0f, 1.0f,
180 0.0f, 1.0f,
181 0.0f, 0.0f,
182
183 1.0f, 0.0f, // -Z side
184 0.0f, 1.0f,
185 0.0f, 0.0f,
186 1.0f, 0.0f,
187 1.0f, 1.0f,
188 0.0f, 1.0f,
189
190 1.0f, 1.0f, // -Y side
191 1.0f, 0.0f,
192 0.0f, 0.0f,
193 1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600194 0.0f, 0.0f,
195 0.0f, 1.0f,
196
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800197 1.0f, 1.0f, // +Y side
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600198 0.0f, 1.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800199 0.0f, 0.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600200 1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600201 0.0f, 0.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600202 1.0f, 0.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600203
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800204 1.0f, 1.0f, // +X side
205 0.0f, 1.0f,
206 0.0f, 0.0f,
207 0.0f, 0.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600208 1.0f, 0.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600209 1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600210
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800211 0.0f, 1.0f, // +Z side
212 0.0f, 0.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600213 1.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600214 0.0f, 0.0f,
Chia-I Wuae3b55d2015-04-22 14:56:17 +0800215 1.0f, 0.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600216 1.0f, 1.0f,
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600217};
218
219void dumpMatrix(const char *note, mat4x4 MVP)
220{
221 int i;
222
223 printf("%s: \n", note);
224 for (i=0; i<4; i++) {
225 printf("%f, %f, %f, %f\n", MVP[i][0], MVP[i][1], MVP[i][2], MVP[i][3]);
226 }
227 printf("\n");
228 fflush(stdout);
229}
230
231void dumpVec4(const char *note, vec4 vector)
232{
233 printf("%s: \n", note);
234 printf("%f, %f, %f, %f\n", vector[0], vector[1], vector[2], vector[3]);
235 printf("\n");
236 fflush(stdout);
237}
238
Tony Barbour3dddd5d2015-04-29 16:19:20 -0600239void VKAPI dbgFunc(
240 VK_DBG_MSG_TYPE msgType,
241 VkValidationLevel validationLevel,
242 VkObject srcObject,
243 size_t location,
244 int32_t msgCode,
245 const char* pMsg,
246 void* pUserData)
247{
248 char *message = (char *) malloc(strlen(pMsg)+100);
249
250 assert (message);
251
252 if (msgType == VK_DBG_MSG_ERROR) {
253 sprintf(message,"ERROR: Code %d : %s", msgCode, pMsg);
254 } else if (msgType == VK_DBG_MSG_WARNING) {
255 sprintf(message,"WARNING: Code %d : %s", msgCode, pMsg);
256 } else {
257 return;
258 }
259
260#ifdef _WIN32
261 MessageBox(NULL, message, "Alert", MB_OK);
262#else
263 printf("%s\n",message);
264 fflush(stdout);
265#endif
266 free(message);
267}
268
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600269struct demo {
Ian Elliott639ca472015-04-16 15:23:05 -0600270#ifdef _WIN32
271#define APP_NAME_STR_LEN 80
272 HINSTANCE connection; // hInstance - Windows Instance
273 char name[APP_NAME_STR_LEN]; // Name to put on the window/icon
274 HWND window; // hWnd - window handle
275#else // _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600276 xcb_connection_t *connection;
277 xcb_screen_t *screen;
Chia-I Wucbb564e2015-04-16 22:02:10 +0800278 xcb_window_t window;
279 xcb_intern_atom_reply_t *atom_wm_delete_window;
Ian Elliott639ca472015-04-16 15:23:05 -0600280#endif // _WIN32
Jon Ashburn8d26c062015-04-24 09:46:24 -0700281 bool prepared;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700282 bool use_staging_buffer;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600283
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600284 VkInstance inst;
Tony Barbour72304ef2015-04-16 15:59:00 -0600285 VkPhysicalDevice gpu;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600286 VkDevice device;
287 VkQueue queue;
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -0700288 uint32_t graphics_queue_node_index;
Tony Barbour72304ef2015-04-16 15:59:00 -0600289 VkPhysicalDeviceProperties *gpu_props;
290 VkPhysicalDeviceQueueProperties *queue_props;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600291
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600292 VkFramebuffer framebuffer;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600293 int width, height;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600294 VkFormat format;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600295
Ian Elliotte4602cd2015-04-21 16:41:02 -0600296 VkDisplayPropertiesWSI *display_props;
297 int num_displays;
Chia-I Wucbb564e2015-04-16 22:02:10 +0800298 VkSwapChainWSI swap_chain;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600299 struct {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600300 VkImage image;
Tony Barbour72304ef2015-04-16 15:59:00 -0600301 VkDeviceMemory mem;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600302 VkCmdBuffer cmd;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600303
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600304 VkColorAttachmentView view;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600305 } buffers[DEMO_BUFFER_COUNT];
306
307 struct {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600308 VkFormat format;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600309
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600310 VkImage image;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500311 VkDeviceMemory mem;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600312 VkDepthStencilView view;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600313 } depth;
314
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600315 struct texture_object textures[DEMO_TEXTURE_COUNT];
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600316
317 struct {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600318 VkBuffer buf;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500319 VkDeviceMemory mem;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600320 VkBufferView view;
Chia-I Wuae721ba2015-05-25 16:27:55 +0800321 VkDescriptorInfo desc;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600322 } uniform_data;
323
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600324 VkCmdBuffer cmd; // Buffer for initialization commands
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -0500325 VkPipelineLayout pipeline_layout;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600326 VkDescriptorSetLayout desc_layout;
327 VkPipeline pipeline;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600328
Courtney Goeltzenleuchter5e6d1e92015-04-10 16:24:50 -0600329 VkDynamicVpState viewport;
330 VkDynamicRsState raster;
331 VkDynamicCbState color_blend;
332 VkDynamicDsState depth_stencil;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600333
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600334 mat4x4 projection_matrix;
335 mat4x4 view_matrix;
336 mat4x4 model_matrix;
337
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600338 float spin_angle;
339 float spin_increment;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600340 bool pause;
341
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600342 VkDescriptorPool desc_pool;
343 VkDescriptorSet desc_set;
Chia-I Wu6a3c8972015-01-04 16:27:24 +0800344
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600345 bool quit;
Tony Barbour3dddd5d2015-04-29 16:19:20 -0600346 bool validate;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600347 uint32_t current_buffer;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600348};
349
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600350static void demo_flush_init_cmd(struct demo *demo)
351{
Tony Barbourfdc2d352015-04-22 09:02:32 -0600352 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600353
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600354 if (demo->cmd == VK_NULL_HANDLE)
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600355 return;
356
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600357 err = vkEndCommandBuffer(demo->cmd);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600358 assert(!err);
359
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600360 const VkCmdBuffer cmd_bufs[] = { demo->cmd };
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600361
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600362 err = vkQueueSubmit(demo->queue, 1, cmd_bufs, VK_NULL_HANDLE);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600363 assert(!err);
364
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600365 err = vkQueueWaitIdle(demo->queue);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600366 assert(!err);
367
Mike Stroyanebae8322015-04-17 12:36:38 -0600368 vkDestroyObject(demo->device, VK_OBJECT_TYPE_COMMAND_BUFFER, demo->cmd);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600369 demo->cmd = VK_NULL_HANDLE;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600370}
371
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600372static void demo_set_image_layout(
373 struct demo *demo,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600374 VkImage image,
malnasseeb25d3f2015-06-03 17:28:38 -0400375 VkImageAspect aspect,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600376 VkImageLayout old_image_layout,
377 VkImageLayout new_image_layout)
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600378{
Tony Barbourfdc2d352015-04-22 09:02:32 -0600379 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600380
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600381 if (demo->cmd == VK_NULL_HANDLE) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600382 const VkCmdBufferCreateInfo cmd = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600383 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600384 .pNext = NULL,
385 .queueNodeIndex = demo->graphics_queue_node_index,
386 .flags = 0,
387 };
388
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600389 err = vkCreateCommandBuffer(demo->device, &cmd, &demo->cmd);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600390 assert(!err);
391
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600392 VkCmdBufferBeginInfo cmd_buf_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600393 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600394 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -0600395 .flags = VK_CMD_BUFFER_OPTIMIZE_SMALL_BATCH_BIT |
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600396 VK_CMD_BUFFER_OPTIMIZE_ONE_TIME_SUBMIT_BIT,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600397 };
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600398 err = vkBeginCommandBuffer(demo->cmd, &cmd_buf_info);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600399 }
400
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600401 VkImageMemoryBarrier image_memory_barrier = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600402 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600403 .pNext = NULL,
404 .outputMask = 0,
405 .inputMask = 0,
406 .oldLayout = old_image_layout,
407 .newLayout = new_image_layout,
408 .image = image,
malnasseeb25d3f2015-06-03 17:28:38 -0400409 .subresourceRange = { aspect, 0, 1, 0, 0 }
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600410 };
411
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600412 if (new_image_layout == VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL) {
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600413 /* Make sure anything that was copying from this image has completed */
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -0600414 image_memory_barrier.inputMask = VK_MEMORY_INPUT_TRANSFER_BIT;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600415 }
416
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600417 if (new_image_layout == VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL) {
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600418 /* Make sure any Copy or CPU writes to image are flushed */
Courtney Goeltzenleuchter2bda8332015-04-29 17:16:21 -0600419 image_memory_barrier.outputMask = VK_MEMORY_OUTPUT_HOST_WRITE_BIT | VK_MEMORY_OUTPUT_TRANSFER_BIT;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600420 }
421
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600422 VkImageMemoryBarrier *pmemory_barrier = &image_memory_barrier;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600423
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600424 VkPipeEvent set_events[] = { VK_PIPE_EVENT_TOP_OF_PIPE };
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600425
Tony Barbour72304ef2015-04-16 15:59:00 -0600426 vkCmdPipelineBarrier(demo->cmd, VK_WAIT_EVENT_TOP_OF_PIPE, 1, set_events, 1, (const void **)&pmemory_barrier);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600427}
428
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600429static void demo_draw_build_cmd(struct demo *demo, VkCmdBuffer cmd_buf)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600430{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600431 const VkColorAttachmentBindInfo color_attachment = {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600432 .view = demo->buffers[demo->current_buffer].view,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600433 .layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600434 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600435 const VkDepthStencilBindInfo depth_stencil = {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600436 .view = demo->depth.view,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600437 .layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600438 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600439 const VkClearColor clear_color = {
Courtney Goeltzenleuchter374553c2015-04-03 16:35:32 -0600440 .color.floatColor = { 0.2f, 0.2f, 0.2f, 0.2f },
441 .useRawValue = false,
442 };
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600443 const float clear_depth = 1.0f;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600444 VkImageSubresourceRange clear_range;
445 VkCmdBufferBeginInfo cmd_buf_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600446 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO,
Courtney Goeltzenleuchter53968d82015-04-03 15:25:24 -0600447 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -0600448 .flags = VK_CMD_BUFFER_OPTIMIZE_SMALL_BATCH_BIT |
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600449 VK_CMD_BUFFER_OPTIMIZE_ONE_TIME_SUBMIT_BIT,
Jon Ashburn60ccfbe2014-12-31 17:08:35 -0700450 };
Tony Barbourfdc2d352015-04-22 09:02:32 -0600451 VkResult U_ASSERT_ONLY err;
Chris Forbes40a71562015-06-17 11:36:12 +1200452 VkAttachmentLoadOp load_op = VK_ATTACHMENT_LOAD_OP_CLEAR;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600453 VkAttachmentStoreOp store_op = VK_ATTACHMENT_STORE_OP_DONT_CARE;
454 const VkFramebufferCreateInfo fb_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600455 .sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO,
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700456 .pNext = NULL,
457 .colorAttachmentCount = 1,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600458 .pColorAttachments = (VkColorAttachmentBindInfo*) &color_attachment,
459 .pDepthStencilAttachment = (VkDepthStencilBindInfo*) &depth_stencil,
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700460 .sampleCount = 1,
Mark Lobodzinskic06b7412015-01-27 13:24:03 -0600461 .width = demo->width,
462 .height = demo->height,
463 .layers = 1,
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700464 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600465 VkRenderPassCreateInfo rp_info;
466 VkRenderPassBegin rp_begin;
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700467
468 memset(&rp_info, 0 , sizeof(rp_info));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600469 err = vkCreateFramebuffer(demo->device, &fb_info, &rp_begin.framebuffer);
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700470 assert(!err);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600471 rp_info.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO;
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700472 rp_info.renderArea.extent.width = demo->width;
473 rp_info.renderArea.extent.height = demo->height;
Courtney Goeltzenleuchter53968d82015-04-03 15:25:24 -0600474 rp_info.colorAttachmentCount = fb_info.colorAttachmentCount;
475 rp_info.pColorFormats = &demo->format;
476 rp_info.pColorLayouts = &color_attachment.layout;
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700477 rp_info.pColorLoadOps = &load_op;
478 rp_info.pColorStoreOps = &store_op;
Courtney Goeltzenleuchter53968d82015-04-03 15:25:24 -0600479 rp_info.pColorLoadClearValues = &clear_color;
Tony Barbour72304ef2015-04-16 15:59:00 -0600480 rp_info.depthStencilFormat = VK_FORMAT_D16_UNORM;
Courtney Goeltzenleuchter53968d82015-04-03 15:25:24 -0600481 rp_info.depthStencilLayout = depth_stencil.layout;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600482 rp_info.depthLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
Courtney Goeltzenleuchter53968d82015-04-03 15:25:24 -0600483 rp_info.depthLoadClearValue = clear_depth;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600484 rp_info.depthStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
485 rp_info.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
Courtney Goeltzenleuchter53968d82015-04-03 15:25:24 -0600486 rp_info.stencilLoadClearValue = 0;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600487 rp_info.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
488 err = vkCreateRenderPass(demo->device, &rp_info, &rp_begin.renderPass);
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700489 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600490
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600491 err = vkBeginCommandBuffer(cmd_buf, &cmd_buf_info);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600492 assert(!err);
493
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600494 vkCmdBindPipeline(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600495 demo->pipeline);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600496 vkCmdBindDescriptorSets(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS,
Cody Northropfb5185a2015-04-16 13:41:56 -0600497 0, 1, &demo->desc_set, 0, NULL);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600498
Tony Barbour72304ef2015-04-16 15:59:00 -0600499 vkCmdBindDynamicStateObject(cmd_buf, VK_STATE_BIND_POINT_VIEWPORT, demo->viewport);
500 vkCmdBindDynamicStateObject(cmd_buf, VK_STATE_BIND_POINT_RASTER, demo->raster);
501 vkCmdBindDynamicStateObject(cmd_buf, VK_STATE_BIND_POINT_COLOR_BLEND,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600502 demo->color_blend);
Tony Barbour72304ef2015-04-16 15:59:00 -0600503 vkCmdBindDynamicStateObject(cmd_buf, VK_STATE_BIND_POINT_DEPTH_STENCIL,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600504 demo->depth_stencil);
505
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600506 vkCmdBeginRenderPass(cmd_buf, &rp_begin);
Chris Forbes40a71562015-06-17 11:36:12 +1200507 clear_range.aspect = VK_IMAGE_ASPECT_DEPTH;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600508 clear_range.baseMipLevel = 0;
509 clear_range.mipLevels = 1;
510 clear_range.baseArraySlice = 0;
511 clear_range.arraySize = 1;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600512
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600513 vkCmdClearDepthStencil(cmd_buf, demo->depth.image,
514 VK_IMAGE_LAYOUT_CLEAR_OPTIMAL,
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600515 clear_depth, 0, 1, &clear_range);
516
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600517 vkCmdDraw(cmd_buf, 0, 12 * 3, 0, 1);
518 vkCmdEndRenderPass(cmd_buf, rp_begin.renderPass);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600519
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600520 err = vkEndCommandBuffer(cmd_buf);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600521 assert(!err);
Courtney Goeltzenleuchter94901dc2015-02-25 17:53:18 -0700522
Mike Stroyanebae8322015-04-17 12:36:38 -0600523 vkDestroyObject(demo->device, VK_OBJECT_TYPE_RENDER_PASS, rp_begin.renderPass);
524 vkDestroyObject(demo->device, VK_OBJECT_TYPE_FRAMEBUFFER, rp_begin.framebuffer);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600525}
526
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600527
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600528void demo_update_data_buffer(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600529{
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600530 mat4x4 MVP, Model, VP;
531 int matrixSize = sizeof(MVP);
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600532 uint8_t *pData;
Tony Barbourfdc2d352015-04-22 09:02:32 -0600533 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600534
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600535 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600536
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600537 // Rotate 22.5 degrees around the Y axis
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600538 mat4x4_dup(Model, demo->model_matrix);
Piers Daniell735ee532015-02-23 16:23:13 -0700539 mat4x4_rotate(demo->model_matrix, Model, 0.0f, 1.0f, 0.0f, (float)degreesToRadians(demo->spin_angle));
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600540 mat4x4_mul(MVP, VP, demo->model_matrix);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600541
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500542 err = vkMapMemory(demo->device, demo->uniform_data.mem, 0, 0, 0, (void **) &pData);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600543 assert(!err);
544
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600545 memcpy(pData, (const void*) &MVP[0][0], matrixSize);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600546
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500547 err = vkUnmapMemory(demo->device, demo->uniform_data.mem);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600548 assert(!err);
549}
550
551static void demo_draw(struct demo *demo)
552{
Chia-I Wucbb564e2015-04-16 22:02:10 +0800553 const VkPresentInfoWSI present = {
554 .sType = VK_STRUCTURE_TYPE_PRESENT_INFO_WSI,
555 .pNext = NULL,
556 .image = demo->buffers[demo->current_buffer].image,
557 .flipInterval = 0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600558 };
Tony Barbourfdc2d352015-04-22 09:02:32 -0600559 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600560
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600561 err = vkQueueSubmit(demo->queue, 1, &demo->buffers[demo->current_buffer].cmd,
562 VK_NULL_HANDLE);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600563 assert(!err);
564
Chia-I Wucbb564e2015-04-16 22:02:10 +0800565 err = vkQueuePresentWSI(demo->queue, &present);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600566 assert(!err);
567
568 demo->current_buffer = (demo->current_buffer + 1) % DEMO_BUFFER_COUNT;
Chia-I Wucbb564e2015-04-16 22:02:10 +0800569
570 err = vkQueueWaitIdle(demo->queue);
571 assert(err == VK_SUCCESS);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600572}
573
574static void demo_prepare_buffers(struct demo *demo)
575{
Chia-I Wucbb564e2015-04-16 22:02:10 +0800576 const VkSwapChainCreateInfoWSI swap_chain = {
577 .sType = VK_STRUCTURE_TYPE_SWAP_CHAIN_CREATE_INFO_WSI,
578 .pNext = NULL,
579 .pNativeWindowSystemHandle = demo->connection,
580 .pNativeWindowHandle = (void *) (intptr_t) demo->window,
Ian Elliotte4602cd2015-04-21 16:41:02 -0600581 .displayCount = 1,
Chia-I Wucbb564e2015-04-16 22:02:10 +0800582 .imageCount = DEMO_BUFFER_COUNT,
583 .imageFormat = demo->format,
584 .imageExtent = {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600585 .width = demo->width,
586 .height = demo->height,
587 },
Chia-I Wucbb564e2015-04-16 22:02:10 +0800588 .imageArraySize = 1,
589 .imageUsageFlags = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600590 };
Chia-I Wucbb564e2015-04-16 22:02:10 +0800591 VkSwapChainImageInfoWSI images[DEMO_BUFFER_COUNT];
592 size_t images_size = sizeof(images);
Tony Barbourfdc2d352015-04-22 09:02:32 -0600593 VkResult U_ASSERT_ONLY err;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600594 uint32_t i;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600595
Chia-I Wucbb564e2015-04-16 22:02:10 +0800596 err = vkCreateSwapChainWSI(demo->device, &swap_chain, &demo->swap_chain);
597 assert(!err);
598
599 err = vkGetSwapChainInfoWSI(demo->swap_chain,
600 VK_SWAP_CHAIN_INFO_TYPE_PERSISTENT_IMAGES_WSI,
601 &images_size, images);
602 assert(!err && images_size == sizeof(images));
603
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600604 for (i = 0; i < DEMO_BUFFER_COUNT; i++) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600605 VkColorAttachmentViewCreateInfo color_attachment_view = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600606 .sType = VK_STRUCTURE_TYPE_COLOR_ATTACHMENT_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600607 .pNext = NULL,
608 .format = demo->format,
609 .mipLevel = 0,
610 .baseArraySlice = 0,
611 .arraySize = 1,
612 };
613
Chia-I Wucbb564e2015-04-16 22:02:10 +0800614 demo->buffers[i].image = images[i].image;
615 demo->buffers[i].mem = images[i].memory;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600616
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600617 demo_set_image_layout(demo, demo->buffers[i].image,
malnasseeb25d3f2015-06-03 17:28:38 -0400618 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600619 VK_IMAGE_LAYOUT_UNDEFINED,
620 VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600621
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600622 color_attachment_view.image = demo->buffers[i].image;
623
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600624 err = vkCreateColorAttachmentView(demo->device,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600625 &color_attachment_view, &demo->buffers[i].view);
626 assert(!err);
627 }
628}
629
630static void demo_prepare_depth(struct demo *demo)
631{
Tony Barbour72304ef2015-04-16 15:59:00 -0600632 const VkFormat depth_format = VK_FORMAT_D16_UNORM;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600633 const VkImageCreateInfo image = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600634 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600635 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -0600636 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600637 .format = depth_format,
638 .extent = { demo->width, demo->height, 1 },
639 .mipLevels = 1,
640 .arraySize = 1,
641 .samples = 1,
Tony Barbour72304ef2015-04-16 15:59:00 -0600642 .tiling = VK_IMAGE_TILING_OPTIMAL,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600643 .usage = VK_IMAGE_USAGE_DEPTH_STENCIL_BIT,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600644 .flags = 0,
645 };
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -0600646 VkMemoryAllocInfo mem_alloc = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600647 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
Mark Lobodzinskia6907f72015-04-16 08:52:00 -0500648 .pNext = NULL,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600649 .allocationSize = 0,
Tony Barbour72304ef2015-04-16 15:59:00 -0600650 .memProps = VK_MEMORY_PROPERTY_DEVICE_ONLY,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600651 .memPriority = VK_MEMORY_PRIORITY_NORMAL,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600652 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600653 VkDepthStencilViewCreateInfo view = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600654 .sType = VK_STRUCTURE_TYPE_DEPTH_STENCIL_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600655 .pNext = NULL,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600656 .image = VK_NULL_HANDLE,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600657 .mipLevel = 0,
658 .baseArraySlice = 0,
659 .arraySize = 1,
660 .flags = 0,
661 };
Mike Stroyanebae8322015-04-17 12:36:38 -0600662
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500663 VkMemoryRequirements mem_reqs;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600664 size_t mem_reqs_size = sizeof(VkMemoryRequirements);
Tony Barbourfdc2d352015-04-22 09:02:32 -0600665 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600666
667 demo->depth.format = depth_format;
668
669 /* create image */
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600670 err = vkCreateImage(demo->device, &image,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600671 &demo->depth.image);
672 assert(!err);
673
Mike Stroyanebae8322015-04-17 12:36:38 -0600674 err = vkGetObjectInfo(demo->device,
675 VK_OBJECT_TYPE_IMAGE, demo->depth.image,
Tony Barbour72304ef2015-04-16 15:59:00 -0600676 VK_OBJECT_INFO_TYPE_MEMORY_REQUIREMENTS,
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500677 &mem_reqs_size, &mem_reqs);
678 mem_alloc.allocationSize = mem_reqs.size;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600679
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500680 /* allocate memory */
681 err = vkAllocMemory(demo->device, &mem_alloc, &demo->depth.mem);
682 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600683
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500684 /* bind memory */
685 err = vkBindObjectMemory(demo->device,
686 VK_OBJECT_TYPE_IMAGE, demo->depth.image,
687 demo->depth.mem, 0);
688 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600689
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600690 demo_set_image_layout(demo, demo->depth.image,
malnasseeb25d3f2015-06-03 17:28:38 -0400691 VK_IMAGE_ASPECT_DEPTH,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600692 VK_IMAGE_LAYOUT_UNDEFINED,
693 VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600694
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600695 /* create image view */
696 view.image = demo->depth.image;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600697 err = vkCreateDepthStencilView(demo->device, &view,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600698 &demo->depth.view);
699 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600700}
701
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600702/** loadTexture
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -0600703 * loads a png file into an memory object, using cstdio , libpng.
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600704 *
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -0600705 * \param demo : Needed to access VK calls
706 * \param filename : the png file to be loaded
707 * \param width : width of png, to be updated as a side effect of this function
708 * \param height : height of png, to be updated as a side effect of this function
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600709 *
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -0600710 * \return bool : an opengl texture id. true if successful?,
711 * should be validated by the client of this function.
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600712 *
713 * Source: http://en.wikibooks.org/wiki/OpenGL_Programming/Intermediate/Textures
714 * Modified to copy image to memory
715 *
716 */
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700717bool loadTexture(const char *filename, uint8_t *rgba_data,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600718 VkSubresourceLayout *layout,
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600719 int32_t *width, int32_t *height)
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600720{
721 //header for testing if it is a png
722 png_byte header[8];
Tony Barbourf9921732015-04-22 11:36:22 -0600723 int is_png, bit_depth, color_type, rowbytes;
724 size_t retval;
Ian Elliott1e42dff2015-02-13 14:29:21 -0700725 png_uint_32 i, twidth, theight;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600726 png_structp png_ptr;
727 png_infop info_ptr, end_info;
728 png_byte *image_data;
729 png_bytep *row_pointers;
730
731 //open file as binary
732 FILE *fp = fopen(filename, "rb");
733 if (!fp) {
734 return false;
735 }
736
737 //read the header
Tony Barbourfdc2d352015-04-22 09:02:32 -0600738 retval = fread(header, 1, 8, fp);
739 if (retval != 8) {
740 fclose(fp);
741 return false;
742 }
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600743
744 //test if png
745 is_png = !png_sig_cmp(header, 0, 8);
746 if (!is_png) {
747 fclose(fp);
748 return false;
749 }
750
751 //create png struct
752 png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL,
753 NULL, NULL);
754 if (!png_ptr) {
755 fclose(fp);
756 return (false);
757 }
758
759 //create png info struct
760 info_ptr = png_create_info_struct(png_ptr);
761 if (!info_ptr) {
762 png_destroy_read_struct(&png_ptr, (png_infopp) NULL, (png_infopp) NULL);
763 fclose(fp);
764 return (false);
765 }
766
767 //create png info struct
768 end_info = png_create_info_struct(png_ptr);
769 if (!end_info) {
770 png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp) NULL);
771 fclose(fp);
772 return (false);
773 }
774
775 //png error stuff, not sure libpng man suggests this.
776 if (setjmp(png_jmpbuf(png_ptr))) {
777 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
778 fclose(fp);
779 return (false);
780 }
781
782 //init png reading
783 png_init_io(png_ptr, fp);
784
785 //let libpng know you already read the first 8 bytes
786 png_set_sig_bytes(png_ptr, 8);
787
788 // read all the info up to the image data
789 png_read_info(png_ptr, info_ptr);
790
791 // get info about png
792 png_get_IHDR(png_ptr, info_ptr, &twidth, &theight, &bit_depth, &color_type,
793 NULL, NULL, NULL);
794
795 //update width and height based on png info
796 *width = twidth;
797 *height = theight;
798
799 // Require that incoming texture be 8bits per color component
800 // and 4 components (RGBA).
801 if (png_get_bit_depth(png_ptr, info_ptr) != 8 ||
802 png_get_channels(png_ptr, info_ptr) != 4) {
803 return false;
804 }
805
806 if (rgba_data == NULL) {
807 // If data pointer is null, we just want the width & height
808 // clean up memory and close stuff
809 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
810 fclose(fp);
811
812 return true;
813 }
814
815 // Update the png info struct.
816 png_read_update_info(png_ptr, info_ptr);
817
818 // Row size in bytes.
819 rowbytes = png_get_rowbytes(png_ptr, info_ptr);
820
821 // Allocate the image_data as a big block, to be given to opengl
822 image_data = (png_byte *)malloc(rowbytes * theight * sizeof(png_byte));
823 if (!image_data) {
824 //clean up memory and close stuff
825 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
826 fclose(fp);
827 return false;
828 }
829
830 // row_pointers is for pointing to image_data for reading the png with libpng
831 row_pointers = (png_bytep *)malloc(theight * sizeof(png_bytep));
832 if (!row_pointers) {
833 //clean up memory and close stuff
834 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
835 // delete[] image_data;
836 fclose(fp);
837 return false;
838 }
839 // set the individual row_pointers to point at the correct offsets of image_data
840 for (i = 0; i < theight; ++i)
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700841 row_pointers[theight - 1 - i] = rgba_data + i * layout->rowPitch;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600842
843 // read the png into image_data through row_pointers
844 png_read_image(png_ptr, row_pointers);
845
846 // clean up memory and close stuff
847 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
848 free(row_pointers);
849 free(image_data);
850 fclose(fp);
851
852 return true;
853}
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600854
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700855static void demo_prepare_texture_image(struct demo *demo,
856 const char *filename,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600857 struct texture_object *tex_obj,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600858 VkImageTiling tiling,
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -0600859 VkImageUsageFlags usage,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600860 VkFlags mem_props)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600861{
Mike Stroyan8b89e072015-06-15 14:21:03 -0600862 const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600863 int32_t tex_width;
864 int32_t tex_height;
Tony Barbourfdc2d352015-04-22 09:02:32 -0600865 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700866
David Pinedo30bd71d2015-04-23 08:16:57 -0600867 if (!loadTexture(filename, NULL, NULL, &tex_width, &tex_height))
868 {
869 printf("Failed to load textures\n");
870 fflush(stdout);
871 exit(1);
872 }
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700873
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600874 tex_obj->tex_width = tex_width;
875 tex_obj->tex_height = tex_height;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700876
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600877 const VkImageCreateInfo image_create_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600878 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700879 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -0600880 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700881 .format = tex_format,
882 .extent = { tex_width, tex_height, 1 },
883 .mipLevels = 1,
884 .arraySize = 1,
885 .samples = 1,
886 .tiling = tiling,
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -0600887 .usage = usage,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700888 .flags = 0,
889 };
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -0600890 VkMemoryAllocInfo mem_alloc = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600891 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
Mark Lobodzinskia6907f72015-04-16 08:52:00 -0500892 .pNext = NULL,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700893 .allocationSize = 0,
894 .memProps = mem_props,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600895 .memPriority = VK_MEMORY_PRIORITY_NORMAL,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700896 };
897
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500898 VkMemoryRequirements mem_reqs;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600899 size_t mem_reqs_size = sizeof(VkMemoryRequirements);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700900
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600901 err = vkCreateImage(demo->device, &image_create_info,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600902 &tex_obj->image);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700903 assert(!err);
904
Mike Stroyanebae8322015-04-17 12:36:38 -0600905 err = vkGetObjectInfo(demo->device,
906 VK_OBJECT_TYPE_IMAGE, tex_obj->image,
Tony Barbour72304ef2015-04-16 15:59:00 -0600907 VK_OBJECT_INFO_TYPE_MEMORY_REQUIREMENTS,
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500908 &mem_reqs_size, &mem_reqs);
909 assert(!err && mem_reqs_size == sizeof(VkMemoryRequirements));
Piers Daniell735ee532015-02-23 16:23:13 -0700910
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500911 mem_alloc.allocationSize = mem_reqs.size;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700912
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500913 /* allocate memory */
914 err = vkAllocMemory(demo->device, &mem_alloc,
915 &(tex_obj->mem));
916 assert(!err);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700917
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500918 /* bind memory */
919 err = vkBindObjectMemory(demo->device,
920 VK_OBJECT_TYPE_IMAGE, tex_obj->image,
921 tex_obj->mem, 0);
922 assert(!err);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700923
Tony Barbour72304ef2015-04-16 15:59:00 -0600924 if (mem_props & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600925 const VkImageSubresource subres = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600926 .aspect = VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700927 .mipLevel = 0,
928 .arraySlice = 0,
929 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600930 VkSubresourceLayout layout;
931 size_t layout_size = sizeof(VkSubresourceLayout);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700932 void *data;
933
Mike Stroyanebae8322015-04-17 12:36:38 -0600934 err = vkGetImageSubresourceInfo(demo->device, tex_obj->image, &subres,
Tony Barbour72304ef2015-04-16 15:59:00 -0600935 VK_SUBRESOURCE_INFO_TYPE_LAYOUT,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700936 &layout_size, &layout);
937 assert(!err && layout_size == sizeof(layout));
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700938
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500939 err = vkMapMemory(demo->device, tex_obj->mem, 0, 0, 0, &data);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700940 assert(!err);
941
942 if (!loadTexture(filename, data, &layout, &tex_width, &tex_height)) {
943 fprintf(stderr, "Error loading texture: %s\n", filename);
944 }
945
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500946 err = vkUnmapMemory(demo->device, tex_obj->mem);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700947 assert(!err);
948 }
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600949
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600950 tex_obj->imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600951 demo_set_image_layout(demo, tex_obj->image,
malnasseeb25d3f2015-06-03 17:28:38 -0400952 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600953 VK_IMAGE_LAYOUT_UNDEFINED,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600954 tex_obj->imageLayout);
955 /* 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 -0700956}
957
Mark Lobodzinskida9b1092015-04-16 11:44:05 -0500958static void demo_destroy_texture_image(struct demo *demo, struct texture_object *tex_objs)
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700959{
960 /* clean up staging resources */
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500961 vkFreeMemory(demo->device, tex_objs->mem);
Mike Stroyanebae8322015-04-17 12:36:38 -0600962 vkDestroyObject(demo->device, VK_OBJECT_TYPE_IMAGE, tex_objs->image);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700963}
964
965static void demo_prepare_textures(struct demo *demo)
966{
Tony Barbour72304ef2015-04-16 15:59:00 -0600967 const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600968 VkFormatProperties props;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700969 size_t size = sizeof(props);
Tony Barbourfdc2d352015-04-22 09:02:32 -0600970 VkResult U_ASSERT_ONLY err;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600971 uint32_t i;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600972
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600973 err = vkGetFormatInfo(demo->device, tex_format,
Tony Barbour72304ef2015-04-16 15:59:00 -0600974 VK_FORMAT_INFO_TYPE_PROPERTIES,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700975 &size, &props);
976 assert(!err);
977
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600978 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700979
FslNopper6a7e50e2015-05-06 21:42:01 +0200980 if ((props.linearTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) && !demo->use_staging_buffer) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700981 /* Device can texture using linear textures */
982 demo_prepare_texture_image(demo, tex_files[i], &demo->textures[i],
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -0600983 VK_IMAGE_TILING_LINEAR, VK_IMAGE_USAGE_SAMPLED_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
Tony Barbour72304ef2015-04-16 15:59:00 -0600984 } else if (props.optimalTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700985 /* Must use staging buffer to copy linear texture to optimized */
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600986 struct texture_object staging_texture;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700987
988 memset(&staging_texture, 0, sizeof(staging_texture));
989 demo_prepare_texture_image(demo, tex_files[i], &staging_texture,
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -0600990 VK_IMAGE_TILING_LINEAR, VK_IMAGE_USAGE_TRANSFER_SOURCE_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700991
992 demo_prepare_texture_image(demo, tex_files[i], &demo->textures[i],
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -0600993 VK_IMAGE_TILING_OPTIMAL,
994 (VK_IMAGE_USAGE_TRANSFER_DESTINATION_BIT | VK_IMAGE_USAGE_SAMPLED_BIT),
995 VK_MEMORY_PROPERTY_DEVICE_ONLY);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700996
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600997 demo_set_image_layout(demo, staging_texture.image,
malnasseeb25d3f2015-06-03 17:28:38 -0400998 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600999 staging_texture.imageLayout,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001000 VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001001
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001002 demo_set_image_layout(demo, demo->textures[i].image,
malnasseeb25d3f2015-06-03 17:28:38 -04001003 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001004 demo->textures[i].imageLayout,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001005 VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001006
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001007 VkImageCopy copy_region = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001008 .srcSubresource = { VK_IMAGE_ASPECT_COLOR, 0, 0 },
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001009 .srcOffset = { 0, 0, 0 },
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001010 .destSubresource = { VK_IMAGE_ASPECT_COLOR, 0, 0 },
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001011 .destOffset = { 0, 0, 0 },
1012 .extent = { staging_texture.tex_width, staging_texture.tex_height, 1 },
1013 };
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001014 vkCmdCopyImage(demo->cmd,
1015 staging_texture.image, VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL,
1016 demo->textures[i].image, VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL,
Courtney Goeltzenleuchter8e89a312015-03-25 11:25:10 -06001017 1, &copy_region);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001018
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001019 demo_set_image_layout(demo, demo->textures[i].image,
malnasseeb25d3f2015-06-03 17:28:38 -04001020 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001021 VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001022 demo->textures[i].imageLayout);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001023
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001024 demo_flush_init_cmd(demo);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001025
Courtney Goeltzenleuchterf3aeb2b2015-04-21 09:30:03 -06001026 demo_destroy_texture_image(demo, &staging_texture);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001027 } else {
Mike Stroyan8b89e072015-06-15 14:21:03 -06001028 /* Can't support VK_FORMAT_R8G8B8A8_UNORM !? */
1029 assert(!"No support for R8G8B8A8_UNORM as texture image format");
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001030 }
1031
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001032 const VkSamplerCreateInfo sampler = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001033 .sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001034 .pNext = NULL,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001035 .magFilter = VK_TEX_FILTER_NEAREST,
1036 .minFilter = VK_TEX_FILTER_NEAREST,
Tony Barbour72304ef2015-04-16 15:59:00 -06001037 .mipMode = VK_TEX_MIPMAP_MODE_BASE,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001038 .addressU = VK_TEX_ADDRESS_CLAMP,
1039 .addressV = VK_TEX_ADDRESS_CLAMP,
1040 .addressW = VK_TEX_ADDRESS_CLAMP,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001041 .mipLodBias = 0.0f,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001042 .maxAnisotropy = 1,
Tony Barbour72304ef2015-04-16 15:59:00 -06001043 .compareOp = VK_COMPARE_OP_NEVER,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001044 .minLod = 0.0f,
1045 .maxLod = 0.0f,
Tony Barbour72304ef2015-04-16 15:59:00 -06001046 .borderColor = VK_BORDER_COLOR_OPAQUE_WHITE,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001047 };
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001048
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001049 VkImageViewCreateInfo view = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001050 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001051 .pNext = NULL,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001052 .image = VK_NULL_HANDLE,
Tony Barbour72304ef2015-04-16 15:59:00 -06001053 .viewType = VK_IMAGE_VIEW_TYPE_2D,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001054 .format = tex_format,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001055 .channels = { VK_CHANNEL_SWIZZLE_R,
1056 VK_CHANNEL_SWIZZLE_G,
1057 VK_CHANNEL_SWIZZLE_B,
1058 VK_CHANNEL_SWIZZLE_A, },
1059 .subresourceRange = { VK_IMAGE_ASPECT_COLOR, 0, 1, 0, 1 },
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001060 .minLod = 0.0f,
1061 };
Jon Ashburnb2a66652015-01-16 09:37:43 -07001062
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001063 /* create sampler */
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001064 err = vkCreateSampler(demo->device, &sampler,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001065 &demo->textures[i].sampler);
1066 assert(!err);
1067
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001068 /* create image view */
1069 view.image = demo->textures[i].image;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001070 err = vkCreateImageView(demo->device, &view,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001071 &demo->textures[i].view);
1072 assert(!err);
1073 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001074}
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001075
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001076void demo_prepare_cube_data_buffer(struct demo *demo)
1077{
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -06001078 VkBufferCreateInfo buf_info;
1079 VkBufferViewCreateInfo view_info;
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -06001080 VkMemoryAllocInfo alloc_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001081 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
Mark Lobodzinskia6907f72015-04-16 08:52:00 -05001082 .pNext = NULL,
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001083 .allocationSize = 0,
Tony Barbour72304ef2015-04-16 15:59:00 -06001084 .memProps = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001085 .memPriority = VK_MEMORY_PRIORITY_NORMAL,
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001086 };
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001087 VkMemoryRequirements mem_reqs;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001088 size_t mem_reqs_size = sizeof(VkMemoryRequirements);
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001089 uint8_t *pData;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001090 int i;
1091 mat4x4 MVP, VP;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001092 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001093 struct vktexcube_vs_uniform data;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001094
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001095 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001096 mat4x4_mul(MVP, VP, demo->model_matrix);
1097 memcpy(data.mvp, MVP, sizeof(MVP));
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001098// dumpMatrix("MVP", MVP);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001099
1100 for (i=0; i<12*3; i++) {
1101 data.position[i][0] = g_vertex_buffer_data[i*3];
1102 data.position[i][1] = g_vertex_buffer_data[i*3+1];
1103 data.position[i][2] = g_vertex_buffer_data[i*3+2];
1104 data.position[i][3] = 1.0f;
1105 data.attr[i][0] = g_uv_buffer_data[2*i];
1106 data.attr[i][1] = g_uv_buffer_data[2*i + 1];
1107 data.attr[i][2] = 0;
1108 data.attr[i][3] = 0;
1109 }
1110
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001111 memset(&buf_info, 0, sizeof(buf_info));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001112 buf_info.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001113 buf_info.size = sizeof(data);
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -06001114 buf_info.usage = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001115 err = vkCreateBuffer(demo->device, &buf_info, &demo->uniform_data.buf);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001116 assert(!err);
1117
Mike Stroyanebae8322015-04-17 12:36:38 -06001118 err = vkGetObjectInfo(demo->device,
Mike Stroyanebae8322015-04-17 12:36:38 -06001119 VK_OBJECT_TYPE_BUFFER, demo->uniform_data.buf,
Tony Barbour72304ef2015-04-16 15:59:00 -06001120 VK_OBJECT_INFO_TYPE_MEMORY_REQUIREMENTS,
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001121 &mem_reqs_size, &mem_reqs);
1122 assert(!err && mem_reqs_size == sizeof(mem_reqs));
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001123
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001124 alloc_info.allocationSize = mem_reqs.size;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001125
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001126 err = vkAllocMemory(demo->device, &alloc_info, &(demo->uniform_data.mem));
1127 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001128
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001129 err = vkMapMemory(demo->device, demo->uniform_data.mem, 0, 0, 0, (void **) &pData);
1130 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001131
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001132 memcpy(pData, &data, sizeof data);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001133
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001134 err = vkUnmapMemory(demo->device, demo->uniform_data.mem);
1135 assert(!err);
1136
1137 err = vkBindObjectMemory(demo->device,
1138 VK_OBJECT_TYPE_BUFFER, demo->uniform_data.buf,
1139 demo->uniform_data.mem, 0);
1140 assert(!err);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001141
1142 memset(&view_info, 0, sizeof(view_info));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001143 view_info.sType = VK_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO;
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001144 view_info.buffer = demo->uniform_data.buf;
Tony Barbour72304ef2015-04-16 15:59:00 -06001145 view_info.viewType = VK_BUFFER_VIEW_TYPE_RAW;
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001146 view_info.offset = 0;
1147 view_info.range = sizeof(data);
1148
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001149 err = vkCreateBufferView(demo->device, &view_info, &demo->uniform_data.view);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001150 assert(!err);
1151
Chia-I Wuae721ba2015-05-25 16:27:55 +08001152 demo->uniform_data.desc.bufferView = demo->uniform_data.view;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001153}
1154
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001155static void demo_prepare_descriptor_layout(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001156{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001157 const VkDescriptorSetLayoutBinding layout_bindings[2] = {
Chia-I Wua2aa8632015-03-26 15:04:41 +08001158 [0] = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001159 .descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
Chia-I Wu6d29a412015-05-25 16:22:52 +08001160 .arraySize = 1,
Tony Barbour72304ef2015-04-16 15:59:00 -06001161 .stageFlags = VK_SHADER_STAGE_VERTEX_BIT,
Chia-I Wu91e8e212015-03-27 12:56:09 +08001162 .pImmutableSamplers = NULL,
Chia-I Wua2aa8632015-03-26 15:04:41 +08001163 },
1164 [1] = {
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -06001165 .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
Chia-I Wu6d29a412015-05-25 16:22:52 +08001166 .arraySize = DEMO_TEXTURE_COUNT,
Tony Barbour72304ef2015-04-16 15:59:00 -06001167 .stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT,
Chia-I Wu91e8e212015-03-27 12:56:09 +08001168 .pImmutableSamplers = NULL,
Chia-I Wua2aa8632015-03-26 15:04:41 +08001169 },
1170 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001171 const VkDescriptorSetLayoutCreateInfo descriptor_layout = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001172 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001173 .pNext = NULL,
Chia-I Wua2aa8632015-03-26 15:04:41 +08001174 .count = 2,
1175 .pBinding = layout_bindings,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001176 };
Tony Barbourfdc2d352015-04-22 09:02:32 -06001177 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001178
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001179 err = vkCreateDescriptorSetLayout(demo->device,
Chia-I Wub58c24a2015-03-26 15:27:55 +08001180 &descriptor_layout, &demo->desc_layout);
1181 assert(!err);
1182
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001183 const VkPipelineLayoutCreateInfo pPipelineLayoutCreateInfo = {
1184 .sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
1185 .pNext = NULL,
1186 .descriptorSetCount = 1,
1187 .pSetLayouts = &demo->desc_layout,
1188 };
1189
1190 err = vkCreatePipelineLayout(demo->device,
1191 &pPipelineLayoutCreateInfo,
1192 &demo->pipeline_layout);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001193 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001194}
1195
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001196static VkShader demo_prepare_shader(struct demo *demo,
Tony Barbour72304ef2015-04-16 15:59:00 -06001197 VkShaderStage stage,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001198 const void *code,
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001199 size_t size)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001200{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001201 VkShaderCreateInfo createInfo;
1202 VkShader shader;
1203 VkResult err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001204
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001205
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001206 createInfo.sType = VK_STRUCTURE_TYPE_SHADER_CREATE_INFO;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001207 createInfo.pNext = NULL;
1208
Cody Northrop8b12a1f2015-03-17 15:55:58 -06001209#ifdef EXTERNAL_SPV
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001210 createInfo.codeSize = size;
1211 createInfo.pCode = code;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001212 createInfo.flags = 0;
1213
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001214 err = vkCreateShader(demo->device, &createInfo, &shader);
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001215 if (err) {
1216 free((void *) createInfo.pCode);
1217 }
1218#else
Cody Northrop8b12a1f2015-03-17 15:55:58 -06001219 // Create fake SPV structure to feed GLSL
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001220 // to the driver "under the covers"
1221 createInfo.codeSize = 3 * sizeof(uint32_t) + size + 1;
1222 createInfo.pCode = malloc(createInfo.codeSize);
1223 createInfo.flags = 0;
1224
Tony Barbour72304ef2015-04-16 15:59:00 -06001225 /* try version 0 first: VkShaderStage followed by GLSL */
Cody Northrop8b12a1f2015-03-17 15:55:58 -06001226 ((uint32_t *) createInfo.pCode)[0] = ICD_SPV_MAGIC;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001227 ((uint32_t *) createInfo.pCode)[1] = 0;
1228 ((uint32_t *) createInfo.pCode)[2] = stage;
1229 memcpy(((uint32_t *) createInfo.pCode + 3), code, size + 1);
1230
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001231 err = vkCreateShader(demo->device, &createInfo, &shader);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001232 if (err) {
1233 free((void *) createInfo.pCode);
Tony Barbourf9921732015-04-22 11:36:22 -06001234 return (VkShader) VK_NULL_HANDLE;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001235 }
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001236#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001237
1238 return shader;
1239}
1240
Cody Northrop8b12a1f2015-03-17 15:55:58 -06001241char *demo_read_spv(const char *filename, size_t *psize)
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001242{
1243 long int size;
Tony Barbourf9921732015-04-22 11:36:22 -06001244 size_t U_ASSERT_ONLY retval;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001245 void *shader_code;
1246
1247 FILE *fp = fopen(filename, "rb");
1248 if (!fp) return NULL;
1249
1250 fseek(fp, 0L, SEEK_END);
1251 size = ftell(fp);
1252
1253 fseek(fp, 0L, SEEK_SET);
1254
1255 shader_code = malloc(size);
Tony Barbourfdc2d352015-04-22 09:02:32 -06001256 retval = fread(shader_code, size, 1, fp);
1257 assert(retval == 1);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001258
1259 *psize = size;
1260
1261 return shader_code;
1262}
1263
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001264static VkShader demo_prepare_vs(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001265{
Cody Northrop8b12a1f2015-03-17 15:55:58 -06001266#ifdef EXTERNAL_SPV
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001267 void *vertShaderCode;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001268 size_t size;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001269
Cody Northrop8b12a1f2015-03-17 15:55:58 -06001270 vertShaderCode = demo_read_spv("cube-vert.spv", &size);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001271
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001272 return demo_prepare_shader(demo, VK_SHADER_STAGE_VERTEX,
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001273 vertShaderCode, size);
1274#else
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001275 static const char *vertShaderText =
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001276 "#version 140\n"
1277 "#extension GL_ARB_separate_shader_objects : enable\n"
1278 "#extension GL_ARB_shading_language_420pack : enable\n"
1279 "\n"
1280 "layout(binding = 0) uniform buf {\n"
1281 " mat4 MVP;\n"
1282 " vec4 position[12*3];\n"
1283 " vec4 attr[12*3];\n"
1284 "} ubuf;\n"
1285 "\n"
1286 "layout (location = 0) out vec4 texcoord;\n"
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001287 "\n"
1288 "void main() \n"
1289 "{\n"
1290 " texcoord = ubuf.attr[gl_VertexID];\n"
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001291 " gl_Position = ubuf.MVP * ubuf.position[gl_VertexID];\n"
Chia-I Wuae3b55d2015-04-22 14:56:17 +08001292 "\n"
1293 " // GL->VK conventions\n"
1294 " gl_Position.y = -gl_Position.y;\n"
Chia-I Wubb67e6e2015-04-22 14:20:52 +08001295 " gl_Position.z = (gl_Position.z + gl_Position.w) / 2.0;\n"
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001296 "}\n";
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001297
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001298 return demo_prepare_shader(demo, VK_SHADER_STAGE_VERTEX,
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001299 (const void *) vertShaderText,
1300 strlen(vertShaderText));
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001301#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001302}
1303
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001304static VkShader demo_prepare_fs(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001305{
Cody Northrop8b12a1f2015-03-17 15:55:58 -06001306#ifdef EXTERNAL_SPV
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001307 void *fragShaderCode;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001308 size_t size;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001309
Cody Northrop8b12a1f2015-03-17 15:55:58 -06001310 fragShaderCode = demo_read_spv("cube-frag.spv", &size);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001311
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001312 return demo_prepare_shader(demo, VK_SHADER_STAGE_FRAGMENT,
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001313 fragShaderCode, size);
1314#else
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001315 static const char *fragShaderText =
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001316 "#version 140\n"
1317 "#extension GL_ARB_separate_shader_objects : enable\n"
1318 "#extension GL_ARB_shading_language_420pack : enable\n"
Courtney Goeltzenleuchter4d1acf12015-02-25 15:13:35 -07001319 "layout (binding = 1) uniform sampler2D tex;\n"
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001320 "\n"
1321 "layout (location = 0) in vec4 texcoord;\n"
1322 "void main() {\n"
1323 " gl_FragColor = texture(tex, texcoord.xy);\n"
1324 "}\n";
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001325
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001326 return demo_prepare_shader(demo, VK_SHADER_STAGE_FRAGMENT,
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001327 (const void *) fragShaderText,
1328 strlen(fragShaderText));
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001329#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001330}
1331
1332static void demo_prepare_pipeline(struct demo *demo)
1333{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001334 VkGraphicsPipelineCreateInfo pipeline;
1335 VkPipelineIaStateCreateInfo ia;
1336 VkPipelineRsStateCreateInfo rs;
1337 VkPipelineCbStateCreateInfo cb;
1338 VkPipelineDsStateCreateInfo ds;
1339 VkPipelineShaderStageCreateInfo vs;
1340 VkPipelineShaderStageCreateInfo fs;
1341 VkPipelineVpStateCreateInfo vp;
1342 VkPipelineMsStateCreateInfo ms;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001343 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001344
1345 memset(&pipeline, 0, sizeof(pipeline));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001346 pipeline.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001347 pipeline.layout = demo->pipeline_layout;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001348
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001349 memset(&ia, 0, sizeof(ia));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001350 ia.sType = VK_STRUCTURE_TYPE_PIPELINE_IA_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06001351 ia.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001352
1353 memset(&rs, 0, sizeof(rs));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001354 rs.sType = VK_STRUCTURE_TYPE_PIPELINE_RS_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06001355 rs.fillMode = VK_FILL_MODE_SOLID;
1356 rs.cullMode = VK_CULL_MODE_BACK;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001357 rs.frontFace = VK_FRONT_FACE_CCW;
Chia-I Wubb67e6e2015-04-22 14:20:52 +08001358 rs.depthClipEnable = VK_TRUE;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001359
1360 memset(&cb, 0, sizeof(cb));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001361 cb.sType = VK_STRUCTURE_TYPE_PIPELINE_CB_STATE_CREATE_INFO;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001362 VkPipelineCbAttachmentState att_state[1];
Tony Barbour29645d02015-01-16 14:27:35 -07001363 memset(att_state, 0, sizeof(att_state));
1364 att_state[0].format = demo->format;
1365 att_state[0].channelWriteMask = 0xf;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001366 att_state[0].blendEnable = VK_FALSE;
Tony Barbour29645d02015-01-16 14:27:35 -07001367 cb.attachmentCount = 1;
1368 cb.pAttachments = att_state;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001369
Tony Barbour29645d02015-01-16 14:27:35 -07001370 memset(&vp, 0, sizeof(vp));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001371 vp.sType = VK_STRUCTURE_TYPE_PIPELINE_VP_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06001372 vp.viewportCount = 1;
Tony Barbour29645d02015-01-16 14:27:35 -07001373
1374 memset(&ds, 0, sizeof(ds));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001375 ds.sType = VK_STRUCTURE_TYPE_PIPELINE_DS_STATE_CREATE_INFO;
Tony Barbour29645d02015-01-16 14:27:35 -07001376 ds.format = demo->depth.format;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001377 ds.depthTestEnable = VK_TRUE;
1378 ds.depthWriteEnable = VK_TRUE;
Tony Barbour72304ef2015-04-16 15:59:00 -06001379 ds.depthCompareOp = VK_COMPARE_OP_LESS_EQUAL;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001380 ds.depthBoundsEnable = VK_FALSE;
1381 ds.back.stencilFailOp = VK_STENCIL_OP_KEEP;
1382 ds.back.stencilPassOp = VK_STENCIL_OP_KEEP;
Tony Barbour72304ef2015-04-16 15:59:00 -06001383 ds.back.stencilCompareOp = VK_COMPARE_OP_ALWAYS;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001384 ds.stencilTestEnable = VK_FALSE;
Tony Barbour29645d02015-01-16 14:27:35 -07001385 ds.front = ds.back;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001386
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001387 memset(&vs, 0, sizeof(vs));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001388 vs.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1389 vs.shader.stage = VK_SHADER_STAGE_VERTEX;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001390 vs.shader.shader = demo_prepare_vs(demo);
Mike Stroyanebae8322015-04-17 12:36:38 -06001391 assert(vs.shader.shader != VK_NULL_HANDLE);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001392
1393 memset(&fs, 0, sizeof(fs));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001394 fs.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1395 fs.shader.stage = VK_SHADER_STAGE_FRAGMENT;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001396 fs.shader.shader = demo_prepare_fs(demo);
Mike Stroyanebae8322015-04-17 12:36:38 -06001397 assert(fs.shader.shader != VK_NULL_HANDLE);
Tony Barbour29645d02015-01-16 14:27:35 -07001398
1399 memset(&ms, 0, sizeof(ms));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001400 ms.sType = VK_STRUCTURE_TYPE_PIPELINE_MS_STATE_CREATE_INFO;
Tony Barbour29645d02015-01-16 14:27:35 -07001401 ms.sampleMask = 1;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001402 ms.multisampleEnable = VK_FALSE;
Tony Barbour29645d02015-01-16 14:27:35 -07001403 ms.samples = 1;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001404
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001405 pipeline.pNext = (const void *) &ia;
1406 ia.pNext = (const void *) &rs;
1407 rs.pNext = (const void *) &cb;
1408 cb.pNext = (const void *) &ms;
1409 ms.pNext = (const void *) &vp;
1410 vp.pNext = (const void *) &ds;
1411 ds.pNext = (const void *) &vs;
1412 vs.pNext = (const void *) &fs;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001413
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001414 err = vkCreateGraphicsPipeline(demo->device, &pipeline, &demo->pipeline);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001415 assert(!err);
1416
Mike Stroyanebae8322015-04-17 12:36:38 -06001417 vkDestroyObject(demo->device, VK_OBJECT_TYPE_SHADER, vs.shader.shader);
1418 vkDestroyObject(demo->device, VK_OBJECT_TYPE_SHADER, fs.shader.shader);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001419}
1420
1421static void demo_prepare_dynamic_states(struct demo *demo)
1422{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001423 VkDynamicVpStateCreateInfo viewport_create;
1424 VkDynamicRsStateCreateInfo raster;
1425 VkDynamicCbStateCreateInfo color_blend;
1426 VkDynamicDsStateCreateInfo depth_stencil;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001427 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001428
Tony Barbour29645d02015-01-16 14:27:35 -07001429 memset(&viewport_create, 0, sizeof(viewport_create));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001430 viewport_create.sType = VK_STRUCTURE_TYPE_DYNAMIC_VP_STATE_CREATE_INFO;
Courtney Goeltzenleuchter1710d8d2015-02-11 14:13:34 -07001431 viewport_create.viewportAndScissorCount = 1;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001432 VkViewport viewport;
Piers Daniell735ee532015-02-23 16:23:13 -07001433 memset(&viewport, 0, sizeof(viewport));
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001434 viewport.height = (float) demo->height;
1435 viewport.width = (float) demo->width;
1436 viewport.minDepth = (float) 0.0f;
1437 viewport.maxDepth = (float) 1.0f;
Piers Daniell735ee532015-02-23 16:23:13 -07001438 viewport_create.pViewports = &viewport;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001439 VkRect scissor;
Piers Daniell735ee532015-02-23 16:23:13 -07001440 memset(&scissor, 0, sizeof(scissor));
Courtney Goeltzenleuchter1710d8d2015-02-11 14:13:34 -07001441 scissor.extent.width = demo->width;
1442 scissor.extent.height = demo->height;
1443 scissor.offset.x = 0;
1444 scissor.offset.y = 0;
Courtney Goeltzenleuchter1710d8d2015-02-11 14:13:34 -07001445 viewport_create.pScissors = &scissor;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001446
1447 memset(&raster, 0, sizeof(raster));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001448 raster.sType = VK_STRUCTURE_TYPE_DYNAMIC_RS_STATE_CREATE_INFO;
Piers Daniell735ee532015-02-23 16:23:13 -07001449 raster.pointSize = 1.0;
1450 raster.lineWidth = 1.0;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001451
1452 memset(&color_blend, 0, sizeof(color_blend));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001453 color_blend.sType = VK_STRUCTURE_TYPE_DYNAMIC_CB_STATE_CREATE_INFO;
Piers Daniell735ee532015-02-23 16:23:13 -07001454 color_blend.blendConst[0] = 1.0f;
1455 color_blend.blendConst[1] = 1.0f;
1456 color_blend.blendConst[2] = 1.0f;
1457 color_blend.blendConst[3] = 1.0f;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001458
1459 memset(&depth_stencil, 0, sizeof(depth_stencil));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001460 depth_stencil.sType = VK_STRUCTURE_TYPE_DYNAMIC_DS_STATE_CREATE_INFO;
Piers Daniell735ee532015-02-23 16:23:13 -07001461 depth_stencil.minDepth = 0.0f;
1462 depth_stencil.maxDepth = 1.0f;
Tony Barbour29645d02015-01-16 14:27:35 -07001463 depth_stencil.stencilBackRef = 0;
1464 depth_stencil.stencilFrontRef = 0;
1465 depth_stencil.stencilReadMask = 0xff;
1466 depth_stencil.stencilWriteMask = 0xff;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001467
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001468 err = vkCreateDynamicViewportState(demo->device, &viewport_create, &demo->viewport);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001469 assert(!err);
1470
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001471 err = vkCreateDynamicRasterState(demo->device, &raster, &demo->raster);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001472 assert(!err);
1473
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001474 err = vkCreateDynamicColorBlendState(demo->device,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001475 &color_blend, &demo->color_blend);
1476 assert(!err);
1477
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001478 err = vkCreateDynamicDepthStencilState(demo->device,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001479 &depth_stencil, &demo->depth_stencil);
1480 assert(!err);
1481}
1482
Chia-I Wu63ea9262015-03-26 13:14:16 +08001483static void demo_prepare_descriptor_pool(struct demo *demo)
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001484{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001485 const VkDescriptorTypeCount type_counts[2] = {
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001486 [0] = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001487 .type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001488 .count = 1,
1489 },
1490 [1] = {
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -06001491 .type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001492 .count = DEMO_TEXTURE_COUNT,
1493 },
1494 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001495 const VkDescriptorPoolCreateInfo descriptor_pool = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001496 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001497 .pNext = NULL,
1498 .count = 2,
1499 .pTypeCount = type_counts,
1500 };
Tony Barbourfdc2d352015-04-22 09:02:32 -06001501 VkResult U_ASSERT_ONLY err;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001502
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001503 err = vkCreateDescriptorPool(demo->device,
1504 VK_DESCRIPTOR_POOL_USAGE_ONE_SHOT, 1,
Chia-I Wu63ea9262015-03-26 13:14:16 +08001505 &descriptor_pool, &demo->desc_pool);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001506 assert(!err);
1507}
1508
1509static void demo_prepare_descriptor_set(struct demo *demo)
1510{
Chia-I Wuae721ba2015-05-25 16:27:55 +08001511 VkDescriptorInfo tex_descs[DEMO_TEXTURE_COUNT];
1512 VkWriteDescriptorSet writes[2];
Tony Barbourfdc2d352015-04-22 09:02:32 -06001513 VkResult U_ASSERT_ONLY err;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001514 uint32_t count;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001515 uint32_t i;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001516
Mike Stroyanebae8322015-04-17 12:36:38 -06001517 err = vkAllocDescriptorSets(demo->device, demo->desc_pool,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001518 VK_DESCRIPTOR_SET_USAGE_STATIC,
Chia-I Wu87544e72015-02-23 10:41:08 -07001519 1, &demo->desc_layout,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001520 &demo->desc_set, &count);
1521 assert(!err && count == 1);
1522
Mike Stroyanebae8322015-04-17 12:36:38 -06001523 vkClearDescriptorSets(demo->device, demo->desc_pool, 1, &demo->desc_set);
Chia-I Wuae721ba2015-05-25 16:27:55 +08001524
1525 memset(&tex_descs, 0, sizeof(tex_descs));
1526 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
1527 tex_descs[i].sampler = demo->textures[i].sampler;
1528 tex_descs[i].imageView = demo->textures[i].view;
1529 tex_descs[i].imageLayout = VK_IMAGE_LAYOUT_GENERAL;
1530 }
1531
1532 memset(&writes, 0, sizeof(writes));
1533
1534 writes[0].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
1535 writes[0].destSet = demo->desc_set;
1536 writes[0].count = 1;
1537 writes[0].descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
1538 writes[0].pDescriptors = &demo->uniform_data.desc;
1539
1540 writes[1].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
1541 writes[1].destSet = demo->desc_set;
1542 writes[1].destBinding = 1;
1543 writes[1].count = DEMO_TEXTURE_COUNT;
1544 writes[1].descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
1545 writes[1].pDescriptors = tex_descs;
1546
1547 err = vkUpdateDescriptorSets(demo->device, 2, writes, 0, NULL);
1548 assert(!err);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001549}
1550
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001551static void demo_prepare(struct demo *demo)
1552{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001553 const VkCmdBufferCreateInfo cmd = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001554 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001555 .pNext = NULL,
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07001556 .queueNodeIndex = demo->graphics_queue_node_index,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001557 .flags = 0,
1558 };
Tony Barbourfdc2d352015-04-22 09:02:32 -06001559 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001560
1561 demo_prepare_buffers(demo);
1562 demo_prepare_depth(demo);
1563 demo_prepare_textures(demo);
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001564 demo_prepare_cube_data_buffer(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001565
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001566 demo_prepare_descriptor_layout(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001567 demo_prepare_pipeline(demo);
1568 demo_prepare_dynamic_states(demo);
1569
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001570 for (int i = 0; i < DEMO_BUFFER_COUNT; i++) {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001571 err = vkCreateCommandBuffer(demo->device, &cmd, &demo->buffers[i].cmd);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001572 assert(!err);
1573 }
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001574
Chia-I Wu63ea9262015-03-26 13:14:16 +08001575 demo_prepare_descriptor_pool(demo);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001576 demo_prepare_descriptor_set(demo);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001577
1578
1579 for (int i = 0; i < DEMO_BUFFER_COUNT; i++) {
1580 demo->current_buffer = i;
1581 demo_draw_build_cmd(demo, demo->buffers[i].cmd);
1582 }
1583
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001584 /*
1585 * Prepare functions above may generate pipeline commands
1586 * that need to be flushed before beginning the render loop.
1587 */
1588 demo_flush_init_cmd(demo);
1589
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001590 demo->current_buffer = 0;
Jon Ashburn8d26c062015-04-24 09:46:24 -07001591 demo->prepared = true;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001592}
1593
Ian Elliott639ca472015-04-16 15:23:05 -06001594#ifdef _WIN32
1595static void demo_run(struct demo *demo)
1596{
Courtney Goeltzenleuchterb7e22702015-04-27 14:56:34 -06001597 if (!demo->prepared)
1598 return;
Ian Elliott639ca472015-04-16 15:23:05 -06001599 // Wait for work to finish before updating MVP.
1600 vkDeviceWaitIdle(demo->device);
1601 demo_update_data_buffer(demo);
1602
1603 demo_draw(demo);
1604
1605 // Wait for work to finish before updating MVP.
1606 vkDeviceWaitIdle(demo->device);
1607}
1608
1609// On MS-Windows, make this a global, so it's available to WndProc()
1610struct demo demo;
1611
1612// MS-Windows event handling function:
1613LRESULT CALLBACK WndProc(HWND hWnd,
1614 UINT uMsg,
1615 WPARAM wParam,
1616 LPARAM lParam)
1617{
Ian Elliott639ca472015-04-16 15:23:05 -06001618 switch(uMsg)
1619 {
Ian Elliott639ca472015-04-16 15:23:05 -06001620 case WM_CLOSE:
1621 PostQuitMessage(0);
Tony Barbour3dddd5d2015-04-29 16:19:20 -06001622 break;
Ian Elliott639ca472015-04-16 15:23:05 -06001623 case WM_PAINT:
1624 demo_run(&demo);
1625 return 0;
1626 default:
1627 break;
1628 }
1629 return (DefWindowProc(hWnd, uMsg, wParam, lParam));
1630}
1631
1632static void demo_create_window(struct demo *demo)
1633{
1634 WNDCLASSEX win_class;
1635
1636 // Initialize the window class structure:
1637 win_class.cbSize = sizeof(WNDCLASSEX);
1638 win_class.style = CS_HREDRAW | CS_VREDRAW;
1639 win_class.lpfnWndProc = WndProc;
1640 win_class.cbClsExtra = 0;
1641 win_class.cbWndExtra = 0;
1642 win_class.hInstance = demo->connection; // hInstance
1643 win_class.hIcon = LoadIcon(NULL, IDI_APPLICATION);
1644 win_class.hCursor = LoadCursor(NULL, IDC_ARROW);
1645 win_class.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
1646 win_class.lpszMenuName = NULL;
1647 win_class.lpszClassName = demo->name;
1648 win_class.hIconSm = LoadIcon(NULL, IDI_WINLOGO);
1649 // Register window class:
1650 if (!RegisterClassEx(&win_class)) {
1651 // It didn't work, so try to give a useful error:
1652 printf("Unexpected error trying to start the application!\n");
1653 fflush(stdout);
1654 exit(1);
1655 }
1656 // Create window with the registered class:
Mike Stroyanb46779e2015-06-15 14:20:13 -06001657 RECT wr = { 0, 0, demo->width, demo->height };
1658 AdjustWindowRect(&wr, WS_OVERLAPPEDWINDOW, FALSE);
Ian Elliott639ca472015-04-16 15:23:05 -06001659 demo->window = CreateWindowEx(0,
1660 demo->name, // class name
1661 demo->name, // app name
1662 WS_OVERLAPPEDWINDOW | // window style
1663 WS_VISIBLE |
1664 WS_SYSMENU,
1665 100,100, // x/y coords
Mike Stroyanb46779e2015-06-15 14:20:13 -06001666 wr.right-wr.left, // width
1667 wr.bottom-wr.top, // height
Ian Elliott639ca472015-04-16 15:23:05 -06001668 NULL, // handle to parent
1669 NULL, // handle to menu
1670 demo->connection, // hInstance
1671 NULL); // no extra parameters
1672 if (!demo->window) {
1673 // It didn't work, so try to give a useful error:
1674 printf("Cannot create a window in which to draw!\n");
1675 fflush(stdout);
1676 exit(1);
1677 }
1678}
1679#else // _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001680static void demo_handle_event(struct demo *demo,
1681 const xcb_generic_event_t *event)
1682{
Piers Daniell735ee532015-02-23 16:23:13 -07001683 uint8_t event_code = event->response_type & 0x7f;
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07001684 switch (event_code) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001685 case XCB_EXPOSE:
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07001686 // TODO: Resize window
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001687 break;
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07001688 case XCB_CLIENT_MESSAGE:
1689 if((*(xcb_client_message_event_t*)event).data.data32[0] ==
1690 (*demo->atom_wm_delete_window).atom) {
1691 demo->quit = true;
1692 }
1693 break;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001694 case XCB_KEY_RELEASE:
1695 {
1696 const xcb_key_release_event_t *key =
1697 (const xcb_key_release_event_t *) event;
1698
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001699 switch (key->detail) {
1700 case 0x9: // Escape
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001701 demo->quit = true;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001702 break;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001703 case 0x71: // left arrow key
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001704 demo->spin_angle += demo->spin_increment;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001705 break;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001706 case 0x72: // right arrow key
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001707 demo->spin_angle -= demo->spin_increment;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001708 break;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001709 case 0x41:
1710 demo->pause = !demo->pause;
Piers Daniell735ee532015-02-23 16:23:13 -07001711 break;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001712 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001713 }
1714 break;
1715 default:
1716 break;
1717 }
1718}
1719
1720static void demo_run(struct demo *demo)
1721{
1722 xcb_flush(demo->connection);
1723
1724 while (!demo->quit) {
1725 xcb_generic_event_t *event;
1726
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07001727 if (demo->pause) {
1728 event = xcb_wait_for_event(demo->connection);
1729 } else {
1730 event = xcb_poll_for_event(demo->connection);
1731 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001732 if (event) {
1733 demo_handle_event(demo, event);
1734 free(event);
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07001735 }
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07001736
1737 // Wait for work to finish before updating MVP.
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001738 vkDeviceWaitIdle(demo->device);
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07001739 demo_update_data_buffer(demo);
1740
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07001741 demo_draw(demo);
Courtney Goeltzenleuchter1454f3c2014-11-18 11:28:09 -07001742
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07001743 // Wait for work to finish before updating MVP.
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001744 vkDeviceWaitIdle(demo->device);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001745 }
1746}
1747
1748static void demo_create_window(struct demo *demo)
1749{
1750 uint32_t value_mask, value_list[32];
1751
1752 demo->window = xcb_generate_id(demo->connection);
1753
1754 value_mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK;
1755 value_list[0] = demo->screen->black_pixel;
1756 value_list[1] = XCB_EVENT_MASK_KEY_RELEASE |
1757 XCB_EVENT_MASK_EXPOSURE;
1758
1759 xcb_create_window(demo->connection,
1760 XCB_COPY_FROM_PARENT,
1761 demo->window, demo->screen->root,
1762 0, 0, demo->width, demo->height, 0,
1763 XCB_WINDOW_CLASS_INPUT_OUTPUT,
1764 demo->screen->root_visual,
1765 value_mask, value_list);
1766
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07001767 /* Magic code that will send notification when window is destroyed */
1768 xcb_intern_atom_cookie_t cookie = xcb_intern_atom(demo->connection, 1, 12,
1769 "WM_PROTOCOLS");
1770 xcb_intern_atom_reply_t* reply = xcb_intern_atom_reply(demo->connection, cookie, 0);
1771
1772 xcb_intern_atom_cookie_t cookie2 = xcb_intern_atom(demo->connection, 0, 16, "WM_DELETE_WINDOW");
1773 demo->atom_wm_delete_window = xcb_intern_atom_reply(demo->connection, cookie2, 0);
1774
1775 xcb_change_property(demo->connection, XCB_PROP_MODE_REPLACE,
1776 demo->window, (*reply).atom, 4, 32, 1,
1777 &(*demo->atom_wm_delete_window).atom);
1778 free(reply);
1779
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001780 xcb_map_window(demo->connection, demo->window);
1781}
Ian Elliott639ca472015-04-16 15:23:05 -06001782#endif // _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001783
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001784static void demo_init_vk(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001785{
Tobin Ehlis99239dc2015-04-16 18:04:57 -06001786 VkResult err;
1787 // Extensions to enable
1788 const char *ext_names[] = {
Chia-I Wucbb564e2015-04-16 22:02:10 +08001789 "VK_WSI_LunarG",
Tony Barbour3dddd5d2015-04-29 16:19:20 -06001790 "Validation"
Tobin Ehlis99239dc2015-04-16 18:04:57 -06001791 };
1792 size_t extSize = sizeof(uint32_t);
1793 uint32_t extCount = 0;
1794 err = vkGetGlobalExtensionInfo(VK_EXTENSION_INFO_TYPE_COUNT, 0, &extSize, &extCount);
1795 assert(!err);
1796
1797 VkExtensionProperties extProp;
1798 extSize = sizeof(VkExtensionProperties);
Tony Barbourfdc2d352015-04-22 09:02:32 -06001799 bool32_t U_ASSERT_ONLY extFound = 0;
Tobin Ehlis99239dc2015-04-16 18:04:57 -06001800 for (uint32_t i = 0; i < extCount; i++) {
1801 err = vkGetGlobalExtensionInfo(VK_EXTENSION_INFO_TYPE_PROPERTIES, i, &extSize, &extProp);
1802 if (!strcmp(ext_names[0], extProp.extName))
1803 extFound = 1;
1804 }
Ian Elliott65152912015-04-28 13:22:33 -06001805 if (!extFound) {
1806 ERR_EXIT("vkGetGlobalExtensionInfo failed to find the "
1807 "\"VK_WSI_LunarG\" extension.\n\nDo you have a compatible "
1808 "Vulkan installable client driver (ICD) installed?\nPlease "
1809 "look at the Getting Started guide for additional "
1810 "information.\n",
1811 "vkCreateInstance Failure");
1812 }
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001813 const VkApplicationInfo app = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001814 .sType = VK_STRUCTURE_TYPE_APPLICATION_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001815 .pNext = NULL,
Ian Elliott44e33f72015-04-28 10:52:52 -06001816 .pAppName = APP_SHORT_NAME,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001817 .appVersion = 0,
Ian Elliott44e33f72015-04-28 10:52:52 -06001818 .pEngineName = APP_SHORT_NAME,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001819 .engineVersion = 0,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001820 .apiVersion = VK_API_VERSION,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001821 };
Tony Barbour3dddd5d2015-04-29 16:19:20 -06001822 VkInstanceCreateInfo inst_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001823 .sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
Jon Ashburnab46b362015-04-04 14:52:07 -06001824 .pNext = NULL,
1825 .pAppInfo = &app,
1826 .pAllocCb = NULL,
Tobin Ehlis99239dc2015-04-16 18:04:57 -06001827 .extensionCount = 1,
1828 .ppEnabledExtensionNames = ext_names,
Jon Ashburnab46b362015-04-04 14:52:07 -06001829 };
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -06001830 const VkDeviceQueueCreateInfo queue = {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001831 .queueNodeIndex = 0,
1832 .queueCount = 1,
1833 };
Ian Elliott097d9f32015-04-28 11:35:02 -06001834
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -06001835 const VkDeviceCreateInfo device = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001836 .sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001837 .pNext = NULL,
1838 .queueRecordCount = 1,
1839 .pRequestedQueues = &queue,
Tobin Ehlis99239dc2015-04-16 18:04:57 -06001840 .extensionCount = 1,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001841 .ppEnabledExtensionNames = ext_names,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001842 .flags = VK_DEVICE_CREATE_VALIDATION_BIT,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001843 };
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001844 uint32_t gpu_count;
1845 uint32_t i;
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07001846 size_t data_size;
1847 uint32_t queue_count;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001848
Tony Barbour3dddd5d2015-04-29 16:19:20 -06001849 if (demo->validate) {
1850 inst_info.extensionCount = 2;
1851 }
1852
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001853 err = vkCreateInstance(&inst_info, &demo->inst);
Ian Elliott07264132015-04-28 11:35:02 -06001854 if (err == VK_ERROR_INCOMPATIBLE_DRIVER) {
1855 ERR_EXIT("Cannot find a compatible Vulkan installable client driver "
Ian Elliott65152912015-04-28 13:22:33 -06001856 "(ICD).\n\nPlease look at the Getting Started guide for "
Ian Elliott07264132015-04-28 11:35:02 -06001857 "additional information.\n",
1858 "vkCreateInstance Failure");
Tony Barbour3dddd5d2015-04-29 16:19:20 -06001859 } else if (err == VK_ERROR_INVALID_EXTENSION) {
1860 ERR_EXIT("Cannot find a specified extension library"
1861 ".\nMake sure your layers path is set appropriately\n",
1862 "vkCreateInstance Failure");
Ian Elliott07264132015-04-28 11:35:02 -06001863 } else if (err) {
Ian Elliott65152912015-04-28 13:22:33 -06001864 ERR_EXIT("vkCreateInstance failed.\n\nDo you have a compatible Vulkan "
1865 "installable client driver (ICD) installed?\nPlease look at "
Ian Elliott07264132015-04-28 11:35:02 -06001866 "the Getting Started guide for additional information.\n",
1867 "vkCreateInstance Failure");
Ian Elliott3979e282015-04-03 15:24:55 -06001868 }
Jon Ashburnab46b362015-04-04 14:52:07 -06001869
Tony Barbour3dddd5d2015-04-29 16:19:20 -06001870
Jon Ashburn07c0c0c2015-04-15 11:31:12 -06001871 gpu_count = 1;
1872 err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, &demo->gpu);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001873 assert(!err && gpu_count == 1);
1874
Tony Barbour3dddd5d2015-04-29 16:19:20 -06001875 if (demo->validate) {
1876 vkDbgRegisterMsgCallback(demo->inst, dbgFunc, NULL);
1877 }
1878
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001879 err = vkCreateDevice(demo->gpu, &device, &demo->device);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001880 assert(!err);
1881
Tony Barbour72304ef2015-04-16 15:59:00 -06001882 err = vkGetPhysicalDeviceInfo(demo->gpu, VK_PHYSICAL_DEVICE_INFO_TYPE_PROPERTIES,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001883 &data_size, NULL);
1884 assert(!err);
1885
Tony Barbour72304ef2015-04-16 15:59:00 -06001886 demo->gpu_props = (VkPhysicalDeviceProperties *) malloc(data_size);
1887 err = vkGetPhysicalDeviceInfo(demo->gpu, VK_PHYSICAL_DEVICE_INFO_TYPE_PROPERTIES,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001888 &data_size, demo->gpu_props);
1889 assert(!err);
1890
Tony Barbour72304ef2015-04-16 15:59:00 -06001891 err = vkGetPhysicalDeviceInfo(demo->gpu, VK_PHYSICAL_DEVICE_INFO_TYPE_QUEUE_PROPERTIES,
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07001892 &data_size, NULL);
1893 assert(!err);
1894
Tony Barbour72304ef2015-04-16 15:59:00 -06001895 demo->queue_props = (VkPhysicalDeviceQueueProperties *) malloc(data_size);
1896 err = vkGetPhysicalDeviceInfo(demo->gpu, VK_PHYSICAL_DEVICE_INFO_TYPE_QUEUE_PROPERTIES,
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07001897 &data_size, demo->queue_props);
1898 assert(!err);
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001899 queue_count = (uint32_t)(data_size / sizeof(VkPhysicalDeviceQueueProperties));
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07001900 assert(queue_count >= 1);
1901
Mark Lobodzinskida9b1092015-04-16 11:44:05 -05001902 // Graphics queue and MemMgr queue can be separate.
1903 // TODO: Add support for separate queues, including synchronization,
Mark Lobodzinski6baaec72015-05-11 17:21:15 -05001904 // and appropriate tracking for QueueSubmit
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07001905 for (i = 0; i < queue_count; i++) {
Mark Lobodzinski6baaec72015-05-11 17:21:15 -05001906 if (demo->queue_props[i].queueFlags & VK_QUEUE_GRAPHICS_BIT)
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07001907 break;
1908 }
1909 assert(i < queue_count);
1910 demo->graphics_queue_node_index = i;
1911
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001912 err = vkGetDeviceQueue(demo->device, demo->graphics_queue_node_index,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001913 0, &demo->queue);
1914 assert(!err);
Tony Barbour3dddd5d2015-04-29 16:19:20 -06001915
Ian Elliotte4602cd2015-04-21 16:41:02 -06001916
1917 // Get the VkDisplayWSI's associated with this physical device:
1918 VkDisplayWSI display;
1919 err = vkGetPhysicalDeviceInfo(demo->gpu, VK_PHYSICAL_DEVICE_INFO_TYPE_DISPLAY_PROPERTIES_WSI,
1920 &data_size, NULL);
Ian Elliottd414a8f2015-05-15 17:52:29 -06001921 if (err != VK_SUCCESS) {
1922 printf("The Vulkan installable client driver (ICD) does not support "
1923 "querying\nfor the swap-chain image format. Therefore, am "
1924 "hardcoding this\nformat to VK_FORMAT_B8G8R8A8_UNORM.\n");
1925 fflush(stdout);
1926 demo->format = VK_FORMAT_B8G8R8A8_UNORM;
1927 return;
1928 }
Ian Elliotte4602cd2015-04-21 16:41:02 -06001929 demo->display_props = (VkDisplayPropertiesWSI *) malloc(data_size);
1930 err = vkGetPhysicalDeviceInfo(demo->gpu, VK_PHYSICAL_DEVICE_INFO_TYPE_DISPLAY_PROPERTIES_WSI,
1931 &data_size, demo->display_props);
1932 assert(!err);
1933 demo->num_displays = data_size / sizeof(VkDisplayPropertiesWSI);
1934 // For now, simply use the first display (TODO: Enhance this for the
1935 // future):
1936 display = demo->display_props[0].display;
1937
1938 // Get a VkFormat to use with the VkDisplayWSI we are using:
1939 err = vkGetDisplayInfoWSI(display, VK_DISPLAY_INFO_TYPE_FORMAT_PROPERTIES_WSI,
1940 &data_size, NULL);
1941 VkDisplayFormatPropertiesWSI* display_format_props =
1942 (VkDisplayFormatPropertiesWSI*) malloc(data_size);
1943 err = vkGetDisplayInfoWSI(display, VK_DISPLAY_INFO_TYPE_FORMAT_PROPERTIES_WSI,
1944 &data_size, display_format_props);
1945 // For now, simply use the first VkFormat (TODO: Enhance this for the
1946 // future):
1947 demo->format = display_format_props[0].swapChainFormat;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001948}
1949
1950static void demo_init_connection(struct demo *demo)
1951{
Ian Elliott639ca472015-04-16 15:23:05 -06001952#ifndef _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001953 const xcb_setup_t *setup;
1954 xcb_screen_iterator_t iter;
1955 int scr;
1956
1957 demo->connection = xcb_connect(NULL, &scr);
Ian Elliott3979e282015-04-03 15:24:55 -06001958 if (demo->connection == NULL) {
1959 printf("Cannot find a compatible Vulkan installable client driver "
1960 "(ICD).\nExiting ...\n");
1961 fflush(stdout);
1962 exit(1);
1963 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001964
1965 setup = xcb_get_setup(demo->connection);
1966 iter = xcb_setup_roots_iterator(setup);
1967 while (scr-- > 0)
1968 xcb_screen_next(&iter);
1969
1970 demo->screen = iter.data;
Ian Elliott639ca472015-04-16 15:23:05 -06001971#endif // _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001972}
1973
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001974static void demo_init(struct demo *demo, int argc, char **argv)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001975{
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001976 vec3 eye = {0.0f, 3.0f, 5.0f};
1977 vec3 origin = {0, 0, 0};
Chia-I Wuae3b55d2015-04-22 14:56:17 +08001978 vec3 up = {0.0f, 1.0f, 0.0};
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001979
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001980 memset(demo, 0, sizeof(*demo));
1981
Piers Daniell735ee532015-02-23 16:23:13 -07001982 for (int i = 1; i < argc; i++) {
Tony Barbour3dddd5d2015-04-29 16:19:20 -06001983 if (strcmp(argv[i], "--use_staging") == 0) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001984 demo->use_staging_buffer = true;
Tony Barbour3dddd5d2015-04-29 16:19:20 -06001985 continue;
Ian Elliott639ca472015-04-16 15:23:05 -06001986 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06001987 if (strcmp(argv[i], "--validate") == 0) {
1988 demo->validate = true;
1989 continue;
1990 }
1991
1992 fprintf(stderr, "Usage:\n %s [--use_staging] [--validate}\n", APP_SHORT_NAME);
Ian Elliott639ca472015-04-16 15:23:05 -06001993 fflush(stderr);
1994 exit(1);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001995 }
1996
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001997 demo_init_connection(demo);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001998 demo_init_vk(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001999
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002000 demo->width = 500;
2001 demo->height = 500;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002002
2003 demo->spin_angle = 0.01f;
2004 demo->spin_increment = 0.01f;
2005 demo->pause = false;
2006
Piers Daniell735ee532015-02-23 16:23:13 -07002007 mat4x4_perspective(demo->projection_matrix, (float)degreesToRadians(45.0f), 1.0f, 0.1f, 100.0f);
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002008 mat4x4_look_at(demo->view_matrix, eye, origin, up);
2009 mat4x4_identity(demo->model_matrix);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002010}
2011
2012static void demo_cleanup(struct demo *demo)
2013{
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05002014 uint32_t i;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002015
Courtney Goeltzenleuchterb7e22702015-04-27 14:56:34 -06002016 demo->prepared = false;
2017
Mike Stroyanebae8322015-04-17 12:36:38 -06002018 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DESCRIPTOR_SET, demo->desc_set);
2019 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DESCRIPTOR_POOL, demo->desc_pool);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08002020
Mike Stroyanebae8322015-04-17 12:36:38 -06002021 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DYNAMIC_VP_STATE, demo->viewport);
2022 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DYNAMIC_RS_STATE, demo->raster);
2023 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DYNAMIC_CB_STATE, demo->color_blend);
2024 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DYNAMIC_DS_STATE, demo->depth_stencil);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002025
Mike Stroyanebae8322015-04-17 12:36:38 -06002026 vkDestroyObject(demo->device, VK_OBJECT_TYPE_PIPELINE, demo->pipeline);
2027 vkDestroyObject(demo->device, VK_OBJECT_TYPE_PIPELINE_LAYOUT, demo->pipeline_layout);
2028 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DESCRIPTOR_SET_LAYOUT, demo->desc_layout);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002029
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002030 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Mike Stroyanebae8322015-04-17 12:36:38 -06002031 vkDestroyObject(demo->device, VK_OBJECT_TYPE_IMAGE_VIEW, demo->textures[i].view);
Mike Stroyanebae8322015-04-17 12:36:38 -06002032 vkDestroyObject(demo->device, VK_OBJECT_TYPE_IMAGE, demo->textures[i].image);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05002033 vkFreeMemory(demo->device, demo->textures[i].mem);
Mike Stroyanebae8322015-04-17 12:36:38 -06002034 vkDestroyObject(demo->device, VK_OBJECT_TYPE_SAMPLER, demo->textures[i].sampler);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002035 }
Chia-I Wucbb564e2015-04-16 22:02:10 +08002036 vkDestroySwapChainWSI(demo->swap_chain);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002037
Mike Stroyanebae8322015-04-17 12:36:38 -06002038 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DEPTH_STENCIL_VIEW, demo->depth.view);
Mike Stroyanebae8322015-04-17 12:36:38 -06002039 vkDestroyObject(demo->device, VK_OBJECT_TYPE_IMAGE, demo->depth.image);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05002040 vkFreeMemory(demo->device, demo->depth.mem);
Mark Lobodzinski7b8fee62015-02-18 16:38:17 -06002041
Mike Stroyanebae8322015-04-17 12:36:38 -06002042 vkDestroyObject(demo->device, VK_OBJECT_TYPE_BUFFER_VIEW, demo->uniform_data.view);
Mike Stroyanebae8322015-04-17 12:36:38 -06002043 vkDestroyObject(demo->device, VK_OBJECT_TYPE_BUFFER, demo->uniform_data.buf);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05002044 vkFreeMemory(demo->device, demo->uniform_data.mem);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002045
2046 for (i = 0; i < DEMO_BUFFER_COUNT; i++) {
Mike Stroyanebae8322015-04-17 12:36:38 -06002047 vkDestroyObject(demo->device, VK_OBJECT_TYPE_COLOR_ATTACHMENT_VIEW, demo->buffers[i].view);
2048 vkDestroyObject(demo->device, VK_OBJECT_TYPE_COMMAND_BUFFER, demo->buffers[i].cmd);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002049 }
2050
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002051 vkDestroyDevice(demo->device);
2052 vkDestroyInstance(demo->inst);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002053
Ian Elliott639ca472015-04-16 15:23:05 -06002054#ifndef _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002055 xcb_destroy_window(demo->connection, demo->window);
2056 xcb_disconnect(demo->connection);
Ian Elliott639ca472015-04-16 15:23:05 -06002057#endif // _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002058}
2059
Ian Elliott639ca472015-04-16 15:23:05 -06002060#ifdef _WIN32
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002061extern int __getmainargs(
2062 int * _Argc,
2063 char *** _Argv,
2064 char *** _Env,
2065 int _DoWildCard,
2066 int * new_mode);
Ian Elliottf9cf78c2015-04-28 10:33:11 -06002067
Ian Elliotta748eaf2015-04-28 15:50:36 -06002068int WINAPI WinMain(HINSTANCE hInstance,
2069 HINSTANCE hPrevInstance,
2070 LPSTR pCmdLine,
2071 int nCmdShow)
Ian Elliott639ca472015-04-16 15:23:05 -06002072{
2073 MSG msg; // message
2074 bool done; // flag saying when app is complete
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002075 int argc;
2076 char** argv;
2077 char** env;
2078 int new_mode = 0;
Ian Elliott639ca472015-04-16 15:23:05 -06002079
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002080 __getmainargs(&argc,&argv,&env,0,&new_mode);
2081
2082 demo_init(&demo, argc, argv);
2083 demo.connection = hInstance;
2084 strncpy(demo.name, "cube", APP_NAME_STR_LEN);
Ian Elliott639ca472015-04-16 15:23:05 -06002085 demo_create_window(&demo);
2086
2087 demo_prepare(&demo);
2088
2089 done = false; //initialize loop condition variable
2090 /* main message loop*/
2091 while(!done)
2092 {
Ian Elliotta748eaf2015-04-28 15:50:36 -06002093 PeekMessage(&msg, NULL, 0, 0, PM_REMOVE);
Ian Elliott639ca472015-04-16 15:23:05 -06002094 if (msg.message == WM_QUIT) //check for a quit message
2095 {
2096 done = true; //if found, quit app
2097 }
2098 else
2099 {
2100 /* Translate and dispatch to event queue*/
2101 TranslateMessage(&msg);
2102 DispatchMessage(&msg);
2103 }
2104 }
2105
2106 demo_cleanup(&demo);
2107
Tony Barbourf9921732015-04-22 11:36:22 -06002108 return (int) msg.wParam;
Ian Elliott639ca472015-04-16 15:23:05 -06002109}
2110#else // _WIN32
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002111int main(int argc, char **argv)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002112{
2113 struct demo demo;
2114
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002115 demo_init(&demo, argc, argv);
Chia-I Wucbb564e2015-04-16 22:02:10 +08002116 demo_create_window(&demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002117
2118 demo_prepare(&demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002119 demo_run(&demo);
2120
2121 demo_cleanup(&demo);
2122
2123 return 0;
2124}
Ian Elliott639ca472015-04-16 15:23:05 -06002125#endif // _WIN32