blob: a91d81ecd3d27b378428f412fc9481290c20a1f3 [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
Cody Northrop1fedb212015-05-28 11:27:16 -0600281 bool prepared;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700282 bool use_staging_buffer;
Cody Northrop1fedb212015-05-28 11:27:16 -0600283 bool use_glsl;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600284
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600285 VkInstance inst;
Tony Barbour72304ef2015-04-16 15:59:00 -0600286 VkPhysicalDevice gpu;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600287 VkDevice device;
288 VkQueue queue;
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -0700289 uint32_t graphics_queue_node_index;
Tony Barbour72304ef2015-04-16 15:59:00 -0600290 VkPhysicalDeviceProperties *gpu_props;
291 VkPhysicalDeviceQueueProperties *queue_props;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600292
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600293 VkFramebuffer framebuffer;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600294 int width, height;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600295 VkFormat format;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600296
Ian Elliotte4602cd2015-04-21 16:41:02 -0600297 VkDisplayPropertiesWSI *display_props;
298 int num_displays;
Courtney Goeltzenleuchter8f0767f2015-06-10 17:33:55 -0600299 PFN_vkGetDisplayInfoWSI fpGetDisplayInfoWSI;
Jon Ashburn0b85d052015-05-21 18:13:33 -0600300 PFN_vkCreateSwapChainWSI fpCreateSwapChainWSI;
301 PFN_vkDestroySwapChainWSI fpDestroySwapChainWSI;
302 PFN_vkGetSwapChainInfoWSI fpGetSwapChainInfoWSI;
303 PFN_vkQueuePresentWSI fpQueuePresentWSI;
Chia-I Wucbb564e2015-04-16 22:02:10 +0800304 VkSwapChainWSI swap_chain;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600305 struct {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600306 VkImage image;
Tony Barbour72304ef2015-04-16 15:59:00 -0600307 VkDeviceMemory mem;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600308 VkCmdBuffer cmd;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600309
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600310 VkColorAttachmentView view;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600311 } buffers[DEMO_BUFFER_COUNT];
312
313 struct {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600314 VkFormat format;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600315
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600316 VkImage image;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500317 VkDeviceMemory mem;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600318 VkDepthStencilView view;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600319 } depth;
320
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600321 struct texture_object textures[DEMO_TEXTURE_COUNT];
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600322
323 struct {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600324 VkBuffer buf;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500325 VkDeviceMemory mem;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600326 VkBufferView view;
Chia-I Wuae721ba2015-05-25 16:27:55 +0800327 VkDescriptorInfo desc;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600328 } uniform_data;
329
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600330 VkCmdBuffer cmd; // Buffer for initialization commands
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -0500331 VkPipelineLayout pipeline_layout;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600332 VkDescriptorSetLayout desc_layout;
333 VkPipeline pipeline;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600334
Courtney Goeltzenleuchter5e6d1e92015-04-10 16:24:50 -0600335 VkDynamicVpState viewport;
336 VkDynamicRsState raster;
337 VkDynamicCbState color_blend;
338 VkDynamicDsState depth_stencil;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600339
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600340 mat4x4 projection_matrix;
341 mat4x4 view_matrix;
342 mat4x4 model_matrix;
343
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600344 float spin_angle;
345 float spin_increment;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600346 bool pause;
347
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600348 VkDescriptorPool desc_pool;
349 VkDescriptorSet desc_set;
Chia-I Wu6a3c8972015-01-04 16:27:24 +0800350
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600351 bool quit;
Tony Barbour3dddd5d2015-04-29 16:19:20 -0600352 bool validate;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600353 uint32_t current_buffer;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600354};
355
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600356static void demo_flush_init_cmd(struct demo *demo)
357{
Tony Barbourfdc2d352015-04-22 09:02:32 -0600358 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600359
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600360 if (demo->cmd == VK_NULL_HANDLE)
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600361 return;
362
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600363 err = vkEndCommandBuffer(demo->cmd);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600364 assert(!err);
365
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600366 const VkCmdBuffer cmd_bufs[] = { demo->cmd };
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600367
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600368 err = vkQueueSubmit(demo->queue, 1, cmd_bufs, VK_NULL_HANDLE);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600369 assert(!err);
370
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600371 err = vkQueueWaitIdle(demo->queue);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600372 assert(!err);
373
Mike Stroyanebae8322015-04-17 12:36:38 -0600374 vkDestroyObject(demo->device, VK_OBJECT_TYPE_COMMAND_BUFFER, demo->cmd);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600375 demo->cmd = VK_NULL_HANDLE;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600376}
377
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600378static void demo_set_image_layout(
379 struct demo *demo,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600380 VkImage image,
malnasseeb25d3f2015-06-03 17:28:38 -0400381 VkImageAspect aspect,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600382 VkImageLayout old_image_layout,
383 VkImageLayout new_image_layout)
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600384{
Tony Barbourfdc2d352015-04-22 09:02:32 -0600385 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600386
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600387 if (demo->cmd == VK_NULL_HANDLE) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600388 const VkCmdBufferCreateInfo cmd = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600389 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600390 .pNext = NULL,
391 .queueNodeIndex = demo->graphics_queue_node_index,
392 .flags = 0,
393 };
394
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600395 err = vkCreateCommandBuffer(demo->device, &cmd, &demo->cmd);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600396 assert(!err);
397
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600398 VkCmdBufferBeginInfo cmd_buf_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600399 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600400 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -0600401 .flags = VK_CMD_BUFFER_OPTIMIZE_SMALL_BATCH_BIT |
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600402 VK_CMD_BUFFER_OPTIMIZE_ONE_TIME_SUBMIT_BIT,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600403 };
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600404 err = vkBeginCommandBuffer(demo->cmd, &cmd_buf_info);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600405 }
406
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600407 VkImageMemoryBarrier image_memory_barrier = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600408 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600409 .pNext = NULL,
410 .outputMask = 0,
411 .inputMask = 0,
412 .oldLayout = old_image_layout,
413 .newLayout = new_image_layout,
414 .image = image,
malnasseeb25d3f2015-06-03 17:28:38 -0400415 .subresourceRange = { aspect, 0, 1, 0, 0 }
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600416 };
417
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600418 if (new_image_layout == VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL) {
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600419 /* Make sure anything that was copying from this image has completed */
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -0600420 image_memory_barrier.inputMask = VK_MEMORY_INPUT_TRANSFER_BIT;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600421 }
422
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600423 if (new_image_layout == VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL) {
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600424 /* Make sure any Copy or CPU writes to image are flushed */
Courtney Goeltzenleuchter2bda8332015-04-29 17:16:21 -0600425 image_memory_barrier.outputMask = VK_MEMORY_OUTPUT_HOST_WRITE_BIT | VK_MEMORY_OUTPUT_TRANSFER_BIT;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600426 }
427
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600428 VkImageMemoryBarrier *pmemory_barrier = &image_memory_barrier;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600429
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600430 VkPipeEvent set_events[] = { VK_PIPE_EVENT_TOP_OF_PIPE };
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600431
Tony Barbour72304ef2015-04-16 15:59:00 -0600432 vkCmdPipelineBarrier(demo->cmd, VK_WAIT_EVENT_TOP_OF_PIPE, 1, set_events, 1, (const void **)&pmemory_barrier);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600433}
434
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600435static void demo_draw_build_cmd(struct demo *demo, VkCmdBuffer cmd_buf)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600436{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600437 const VkColorAttachmentBindInfo color_attachment = {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600438 .view = demo->buffers[demo->current_buffer].view,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600439 .layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600440 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600441 const VkDepthStencilBindInfo depth_stencil = {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600442 .view = demo->depth.view,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600443 .layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600444 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600445 const VkClearColor clear_color = {
Courtney Goeltzenleuchter374553c2015-04-03 16:35:32 -0600446 .color.floatColor = { 0.2f, 0.2f, 0.2f, 0.2f },
447 .useRawValue = false,
448 };
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600449 const float clear_depth = 1.0f;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600450 VkImageSubresourceRange clear_range;
451 VkCmdBufferBeginInfo cmd_buf_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600452 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO,
Courtney Goeltzenleuchter53968d82015-04-03 15:25:24 -0600453 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -0600454 .flags = VK_CMD_BUFFER_OPTIMIZE_SMALL_BATCH_BIT |
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600455 VK_CMD_BUFFER_OPTIMIZE_ONE_TIME_SUBMIT_BIT,
Jon Ashburn60ccfbe2014-12-31 17:08:35 -0700456 };
Tony Barbourfdc2d352015-04-22 09:02:32 -0600457 VkResult U_ASSERT_ONLY err;
Chris Forbes40a71562015-06-17 11:36:12 +1200458 VkAttachmentLoadOp load_op = VK_ATTACHMENT_LOAD_OP_CLEAR;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600459 VkAttachmentStoreOp store_op = VK_ATTACHMENT_STORE_OP_DONT_CARE;
460 const VkFramebufferCreateInfo fb_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600461 .sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO,
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700462 .pNext = NULL,
463 .colorAttachmentCount = 1,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600464 .pColorAttachments = (VkColorAttachmentBindInfo*) &color_attachment,
465 .pDepthStencilAttachment = (VkDepthStencilBindInfo*) &depth_stencil,
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700466 .sampleCount = 1,
Mark Lobodzinskic06b7412015-01-27 13:24:03 -0600467 .width = demo->width,
468 .height = demo->height,
469 .layers = 1,
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700470 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600471 VkRenderPassCreateInfo rp_info;
472 VkRenderPassBegin rp_begin;
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700473
474 memset(&rp_info, 0 , sizeof(rp_info));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600475 err = vkCreateFramebuffer(demo->device, &fb_info, &rp_begin.framebuffer);
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700476 assert(!err);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600477 rp_info.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO;
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700478 rp_info.renderArea.extent.width = demo->width;
479 rp_info.renderArea.extent.height = demo->height;
Courtney Goeltzenleuchter53968d82015-04-03 15:25:24 -0600480 rp_info.colorAttachmentCount = fb_info.colorAttachmentCount;
481 rp_info.pColorFormats = &demo->format;
482 rp_info.pColorLayouts = &color_attachment.layout;
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700483 rp_info.pColorLoadOps = &load_op;
484 rp_info.pColorStoreOps = &store_op;
Courtney Goeltzenleuchter53968d82015-04-03 15:25:24 -0600485 rp_info.pColorLoadClearValues = &clear_color;
Tony Barbour72304ef2015-04-16 15:59:00 -0600486 rp_info.depthStencilFormat = VK_FORMAT_D16_UNORM;
Courtney Goeltzenleuchter53968d82015-04-03 15:25:24 -0600487 rp_info.depthStencilLayout = depth_stencil.layout;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600488 rp_info.depthLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
Courtney Goeltzenleuchter53968d82015-04-03 15:25:24 -0600489 rp_info.depthLoadClearValue = clear_depth;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600490 rp_info.depthStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
491 rp_info.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
Courtney Goeltzenleuchter53968d82015-04-03 15:25:24 -0600492 rp_info.stencilLoadClearValue = 0;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600493 rp_info.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
494 err = vkCreateRenderPass(demo->device, &rp_info, &rp_begin.renderPass);
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700495 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600496
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600497 err = vkBeginCommandBuffer(cmd_buf, &cmd_buf_info);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600498 assert(!err);
499
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600500 vkCmdBindPipeline(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600501 demo->pipeline);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600502 vkCmdBindDescriptorSets(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS,
Cody Northropfb5185a2015-04-16 13:41:56 -0600503 0, 1, &demo->desc_set, 0, NULL);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600504
Tony Barbour72304ef2015-04-16 15:59:00 -0600505 vkCmdBindDynamicStateObject(cmd_buf, VK_STATE_BIND_POINT_VIEWPORT, demo->viewport);
506 vkCmdBindDynamicStateObject(cmd_buf, VK_STATE_BIND_POINT_RASTER, demo->raster);
507 vkCmdBindDynamicStateObject(cmd_buf, VK_STATE_BIND_POINT_COLOR_BLEND,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600508 demo->color_blend);
Tony Barbour72304ef2015-04-16 15:59:00 -0600509 vkCmdBindDynamicStateObject(cmd_buf, VK_STATE_BIND_POINT_DEPTH_STENCIL,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600510 demo->depth_stencil);
511
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600512 vkCmdBeginRenderPass(cmd_buf, &rp_begin);
Chris Forbes40a71562015-06-17 11:36:12 +1200513 clear_range.aspect = VK_IMAGE_ASPECT_DEPTH;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600514 clear_range.baseMipLevel = 0;
515 clear_range.mipLevels = 1;
516 clear_range.baseArraySlice = 0;
517 clear_range.arraySize = 1;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600518
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600519 vkCmdClearDepthStencil(cmd_buf, demo->depth.image,
520 VK_IMAGE_LAYOUT_CLEAR_OPTIMAL,
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600521 clear_depth, 0, 1, &clear_range);
522
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600523 vkCmdDraw(cmd_buf, 0, 12 * 3, 0, 1);
524 vkCmdEndRenderPass(cmd_buf, rp_begin.renderPass);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600525
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600526 err = vkEndCommandBuffer(cmd_buf);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600527 assert(!err);
Courtney Goeltzenleuchter94901dc2015-02-25 17:53:18 -0700528
Mike Stroyanebae8322015-04-17 12:36:38 -0600529 vkDestroyObject(demo->device, VK_OBJECT_TYPE_RENDER_PASS, rp_begin.renderPass);
530 vkDestroyObject(demo->device, VK_OBJECT_TYPE_FRAMEBUFFER, rp_begin.framebuffer);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600531}
532
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600533
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600534void demo_update_data_buffer(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600535{
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600536 mat4x4 MVP, Model, VP;
537 int matrixSize = sizeof(MVP);
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600538 uint8_t *pData;
Tony Barbourfdc2d352015-04-22 09:02:32 -0600539 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600540
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600541 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600542
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600543 // Rotate 22.5 degrees around the Y axis
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600544 mat4x4_dup(Model, demo->model_matrix);
Piers Daniell735ee532015-02-23 16:23:13 -0700545 mat4x4_rotate(demo->model_matrix, Model, 0.0f, 1.0f, 0.0f, (float)degreesToRadians(demo->spin_angle));
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600546 mat4x4_mul(MVP, VP, demo->model_matrix);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600547
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500548 err = vkMapMemory(demo->device, demo->uniform_data.mem, 0, 0, 0, (void **) &pData);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600549 assert(!err);
550
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600551 memcpy(pData, (const void*) &MVP[0][0], matrixSize);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600552
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500553 err = vkUnmapMemory(demo->device, demo->uniform_data.mem);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600554 assert(!err);
555}
556
557static void demo_draw(struct demo *demo)
558{
Chia-I Wucbb564e2015-04-16 22:02:10 +0800559 const VkPresentInfoWSI present = {
560 .sType = VK_STRUCTURE_TYPE_PRESENT_INFO_WSI,
561 .pNext = NULL,
562 .image = demo->buffers[demo->current_buffer].image,
563 .flipInterval = 0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600564 };
Tony Barbourfdc2d352015-04-22 09:02:32 -0600565 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600566
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600567 err = vkQueueSubmit(demo->queue, 1, &demo->buffers[demo->current_buffer].cmd,
568 VK_NULL_HANDLE);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600569 assert(!err);
570
Jon Ashburn0b85d052015-05-21 18:13:33 -0600571 err = demo->fpQueuePresentWSI(demo->queue, &present);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600572 assert(!err);
573
574 demo->current_buffer = (demo->current_buffer + 1) % DEMO_BUFFER_COUNT;
Chia-I Wucbb564e2015-04-16 22:02:10 +0800575
576 err = vkQueueWaitIdle(demo->queue);
577 assert(err == VK_SUCCESS);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600578}
579
580static void demo_prepare_buffers(struct demo *demo)
581{
Chia-I Wucbb564e2015-04-16 22:02:10 +0800582 const VkSwapChainCreateInfoWSI swap_chain = {
583 .sType = VK_STRUCTURE_TYPE_SWAP_CHAIN_CREATE_INFO_WSI,
584 .pNext = NULL,
585 .pNativeWindowSystemHandle = demo->connection,
586 .pNativeWindowHandle = (void *) (intptr_t) demo->window,
Ian Elliotte4602cd2015-04-21 16:41:02 -0600587 .displayCount = 1,
Chia-I Wucbb564e2015-04-16 22:02:10 +0800588 .imageCount = DEMO_BUFFER_COUNT,
589 .imageFormat = demo->format,
590 .imageExtent = {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600591 .width = demo->width,
592 .height = demo->height,
593 },
Chia-I Wucbb564e2015-04-16 22:02:10 +0800594 .imageArraySize = 1,
595 .imageUsageFlags = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600596 };
Chia-I Wucbb564e2015-04-16 22:02:10 +0800597 VkSwapChainImageInfoWSI images[DEMO_BUFFER_COUNT];
598 size_t images_size = sizeof(images);
Tony Barbourfdc2d352015-04-22 09:02:32 -0600599 VkResult U_ASSERT_ONLY err;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600600 uint32_t i;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600601
Jon Ashburn0b85d052015-05-21 18:13:33 -0600602 err = demo->fpCreateSwapChainWSI(demo->device, &swap_chain, &demo->swap_chain);
Chia-I Wucbb564e2015-04-16 22:02:10 +0800603 assert(!err);
604
Jon Ashburn0b85d052015-05-21 18:13:33 -0600605 err = demo->fpGetSwapChainInfoWSI(demo->swap_chain,
Chia-I Wucbb564e2015-04-16 22:02:10 +0800606 VK_SWAP_CHAIN_INFO_TYPE_PERSISTENT_IMAGES_WSI,
607 &images_size, images);
608 assert(!err && images_size == sizeof(images));
609
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600610 for (i = 0; i < DEMO_BUFFER_COUNT; i++) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600611 VkColorAttachmentViewCreateInfo color_attachment_view = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600612 .sType = VK_STRUCTURE_TYPE_COLOR_ATTACHMENT_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600613 .pNext = NULL,
614 .format = demo->format,
615 .mipLevel = 0,
616 .baseArraySlice = 0,
617 .arraySize = 1,
618 };
619
Chia-I Wucbb564e2015-04-16 22:02:10 +0800620 demo->buffers[i].image = images[i].image;
621 demo->buffers[i].mem = images[i].memory;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600622
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600623 demo_set_image_layout(demo, demo->buffers[i].image,
malnasseeb25d3f2015-06-03 17:28:38 -0400624 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600625 VK_IMAGE_LAYOUT_UNDEFINED,
626 VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600627
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600628 color_attachment_view.image = demo->buffers[i].image;
629
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600630 err = vkCreateColorAttachmentView(demo->device,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600631 &color_attachment_view, &demo->buffers[i].view);
632 assert(!err);
633 }
634}
635
636static void demo_prepare_depth(struct demo *demo)
637{
Tony Barbour72304ef2015-04-16 15:59:00 -0600638 const VkFormat depth_format = VK_FORMAT_D16_UNORM;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600639 const VkImageCreateInfo image = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600640 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600641 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -0600642 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600643 .format = depth_format,
644 .extent = { demo->width, demo->height, 1 },
645 .mipLevels = 1,
646 .arraySize = 1,
647 .samples = 1,
Tony Barbour72304ef2015-04-16 15:59:00 -0600648 .tiling = VK_IMAGE_TILING_OPTIMAL,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600649 .usage = VK_IMAGE_USAGE_DEPTH_STENCIL_BIT,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600650 .flags = 0,
651 };
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -0600652 VkMemoryAllocInfo mem_alloc = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600653 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
Mark Lobodzinskia6907f72015-04-16 08:52:00 -0500654 .pNext = NULL,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600655 .allocationSize = 0,
Tony Barbour72304ef2015-04-16 15:59:00 -0600656 .memProps = VK_MEMORY_PROPERTY_DEVICE_ONLY,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600657 .memPriority = VK_MEMORY_PRIORITY_NORMAL,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600658 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600659 VkDepthStencilViewCreateInfo view = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600660 .sType = VK_STRUCTURE_TYPE_DEPTH_STENCIL_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600661 .pNext = NULL,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600662 .image = VK_NULL_HANDLE,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600663 .mipLevel = 0,
664 .baseArraySlice = 0,
665 .arraySize = 1,
666 .flags = 0,
667 };
Mike Stroyanebae8322015-04-17 12:36:38 -0600668
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500669 VkMemoryRequirements mem_reqs;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600670 size_t mem_reqs_size = sizeof(VkMemoryRequirements);
Tony Barbourfdc2d352015-04-22 09:02:32 -0600671 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600672
673 demo->depth.format = depth_format;
674
675 /* create image */
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600676 err = vkCreateImage(demo->device, &image,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600677 &demo->depth.image);
678 assert(!err);
679
Mike Stroyanebae8322015-04-17 12:36:38 -0600680 err = vkGetObjectInfo(demo->device,
681 VK_OBJECT_TYPE_IMAGE, demo->depth.image,
Tony Barbour72304ef2015-04-16 15:59:00 -0600682 VK_OBJECT_INFO_TYPE_MEMORY_REQUIREMENTS,
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500683 &mem_reqs_size, &mem_reqs);
684 mem_alloc.allocationSize = mem_reqs.size;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600685
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500686 /* allocate memory */
687 err = vkAllocMemory(demo->device, &mem_alloc, &demo->depth.mem);
688 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600689
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500690 /* bind memory */
691 err = vkBindObjectMemory(demo->device,
692 VK_OBJECT_TYPE_IMAGE, demo->depth.image,
693 demo->depth.mem, 0);
694 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600695
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600696 demo_set_image_layout(demo, demo->depth.image,
malnasseeb25d3f2015-06-03 17:28:38 -0400697 VK_IMAGE_ASPECT_DEPTH,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600698 VK_IMAGE_LAYOUT_UNDEFINED,
699 VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600700
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600701 /* create image view */
702 view.image = demo->depth.image;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600703 err = vkCreateDepthStencilView(demo->device, &view,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600704 &demo->depth.view);
705 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600706}
707
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600708/** loadTexture
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -0600709 * loads a png file into an memory object, using cstdio , libpng.
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600710 *
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -0600711 * \param demo : Needed to access VK calls
712 * \param filename : the png file to be loaded
713 * \param width : width of png, to be updated as a side effect of this function
714 * \param height : height of png, to be updated as a side effect of this function
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600715 *
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -0600716 * \return bool : an opengl texture id. true if successful?,
717 * should be validated by the client of this function.
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600718 *
719 * Source: http://en.wikibooks.org/wiki/OpenGL_Programming/Intermediate/Textures
720 * Modified to copy image to memory
721 *
722 */
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700723bool loadTexture(const char *filename, uint8_t *rgba_data,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600724 VkSubresourceLayout *layout,
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600725 int32_t *width, int32_t *height)
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600726{
727 //header for testing if it is a png
728 png_byte header[8];
Tony Barbourf9921732015-04-22 11:36:22 -0600729 int is_png, bit_depth, color_type, rowbytes;
730 size_t retval;
Ian Elliott1e42dff2015-02-13 14:29:21 -0700731 png_uint_32 i, twidth, theight;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600732 png_structp png_ptr;
733 png_infop info_ptr, end_info;
734 png_byte *image_data;
735 png_bytep *row_pointers;
736
737 //open file as binary
738 FILE *fp = fopen(filename, "rb");
739 if (!fp) {
740 return false;
741 }
742
743 //read the header
Tony Barbourfdc2d352015-04-22 09:02:32 -0600744 retval = fread(header, 1, 8, fp);
745 if (retval != 8) {
746 fclose(fp);
747 return false;
748 }
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600749
750 //test if png
751 is_png = !png_sig_cmp(header, 0, 8);
752 if (!is_png) {
753 fclose(fp);
754 return false;
755 }
756
757 //create png struct
758 png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL,
759 NULL, NULL);
760 if (!png_ptr) {
761 fclose(fp);
762 return (false);
763 }
764
765 //create png info struct
766 info_ptr = png_create_info_struct(png_ptr);
767 if (!info_ptr) {
768 png_destroy_read_struct(&png_ptr, (png_infopp) NULL, (png_infopp) NULL);
769 fclose(fp);
770 return (false);
771 }
772
773 //create png info struct
774 end_info = png_create_info_struct(png_ptr);
775 if (!end_info) {
776 png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp) NULL);
777 fclose(fp);
778 return (false);
779 }
780
781 //png error stuff, not sure libpng man suggests this.
782 if (setjmp(png_jmpbuf(png_ptr))) {
783 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
784 fclose(fp);
785 return (false);
786 }
787
788 //init png reading
789 png_init_io(png_ptr, fp);
790
791 //let libpng know you already read the first 8 bytes
792 png_set_sig_bytes(png_ptr, 8);
793
794 // read all the info up to the image data
795 png_read_info(png_ptr, info_ptr);
796
797 // get info about png
798 png_get_IHDR(png_ptr, info_ptr, &twidth, &theight, &bit_depth, &color_type,
799 NULL, NULL, NULL);
800
801 //update width and height based on png info
802 *width = twidth;
803 *height = theight;
804
805 // Require that incoming texture be 8bits per color component
806 // and 4 components (RGBA).
807 if (png_get_bit_depth(png_ptr, info_ptr) != 8 ||
808 png_get_channels(png_ptr, info_ptr) != 4) {
809 return false;
810 }
811
812 if (rgba_data == NULL) {
813 // If data pointer is null, we just want the width & height
814 // clean up memory and close stuff
815 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
816 fclose(fp);
817
818 return true;
819 }
820
821 // Update the png info struct.
822 png_read_update_info(png_ptr, info_ptr);
823
824 // Row size in bytes.
825 rowbytes = png_get_rowbytes(png_ptr, info_ptr);
826
827 // Allocate the image_data as a big block, to be given to opengl
828 image_data = (png_byte *)malloc(rowbytes * theight * sizeof(png_byte));
829 if (!image_data) {
830 //clean up memory and close stuff
831 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
832 fclose(fp);
833 return false;
834 }
835
836 // row_pointers is for pointing to image_data for reading the png with libpng
837 row_pointers = (png_bytep *)malloc(theight * sizeof(png_bytep));
838 if (!row_pointers) {
839 //clean up memory and close stuff
840 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
841 // delete[] image_data;
842 fclose(fp);
843 return false;
844 }
845 // set the individual row_pointers to point at the correct offsets of image_data
846 for (i = 0; i < theight; ++i)
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700847 row_pointers[theight - 1 - i] = rgba_data + i * layout->rowPitch;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600848
849 // read the png into image_data through row_pointers
850 png_read_image(png_ptr, row_pointers);
851
852 // clean up memory and close stuff
853 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
854 free(row_pointers);
855 free(image_data);
856 fclose(fp);
857
858 return true;
859}
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600860
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700861static void demo_prepare_texture_image(struct demo *demo,
862 const char *filename,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600863 struct texture_object *tex_obj,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600864 VkImageTiling tiling,
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -0600865 VkImageUsageFlags usage,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600866 VkFlags mem_props)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600867{
Mike Stroyan8b89e072015-06-15 14:21:03 -0600868 const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600869 int32_t tex_width;
870 int32_t tex_height;
Tony Barbourfdc2d352015-04-22 09:02:32 -0600871 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700872
David Pinedo30bd71d2015-04-23 08:16:57 -0600873 if (!loadTexture(filename, NULL, NULL, &tex_width, &tex_height))
874 {
875 printf("Failed to load textures\n");
876 fflush(stdout);
877 exit(1);
878 }
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700879
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600880 tex_obj->tex_width = tex_width;
881 tex_obj->tex_height = tex_height;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700882
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600883 const VkImageCreateInfo image_create_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600884 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700885 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -0600886 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700887 .format = tex_format,
888 .extent = { tex_width, tex_height, 1 },
889 .mipLevels = 1,
890 .arraySize = 1,
891 .samples = 1,
892 .tiling = tiling,
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -0600893 .usage = usage,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700894 .flags = 0,
895 };
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -0600896 VkMemoryAllocInfo mem_alloc = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600897 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
Mark Lobodzinskia6907f72015-04-16 08:52:00 -0500898 .pNext = NULL,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700899 .allocationSize = 0,
900 .memProps = mem_props,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600901 .memPriority = VK_MEMORY_PRIORITY_NORMAL,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700902 };
903
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500904 VkMemoryRequirements mem_reqs;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600905 size_t mem_reqs_size = sizeof(VkMemoryRequirements);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700906
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600907 err = vkCreateImage(demo->device, &image_create_info,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600908 &tex_obj->image);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700909 assert(!err);
910
Mike Stroyanebae8322015-04-17 12:36:38 -0600911 err = vkGetObjectInfo(demo->device,
912 VK_OBJECT_TYPE_IMAGE, tex_obj->image,
Tony Barbour72304ef2015-04-16 15:59:00 -0600913 VK_OBJECT_INFO_TYPE_MEMORY_REQUIREMENTS,
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500914 &mem_reqs_size, &mem_reqs);
915 assert(!err && mem_reqs_size == sizeof(VkMemoryRequirements));
Piers Daniell735ee532015-02-23 16:23:13 -0700916
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500917 mem_alloc.allocationSize = mem_reqs.size;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700918
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500919 /* allocate memory */
920 err = vkAllocMemory(demo->device, &mem_alloc,
921 &(tex_obj->mem));
922 assert(!err);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700923
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500924 /* bind memory */
925 err = vkBindObjectMemory(demo->device,
926 VK_OBJECT_TYPE_IMAGE, tex_obj->image,
927 tex_obj->mem, 0);
928 assert(!err);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700929
Tony Barbour72304ef2015-04-16 15:59:00 -0600930 if (mem_props & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600931 const VkImageSubresource subres = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600932 .aspect = VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700933 .mipLevel = 0,
934 .arraySlice = 0,
935 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600936 VkSubresourceLayout layout;
937 size_t layout_size = sizeof(VkSubresourceLayout);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700938 void *data;
939
Mike Stroyanebae8322015-04-17 12:36:38 -0600940 err = vkGetImageSubresourceInfo(demo->device, tex_obj->image, &subres,
Tony Barbour72304ef2015-04-16 15:59:00 -0600941 VK_SUBRESOURCE_INFO_TYPE_LAYOUT,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700942 &layout_size, &layout);
943 assert(!err && layout_size == sizeof(layout));
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700944
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500945 err = vkMapMemory(demo->device, tex_obj->mem, 0, 0, 0, &data);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700946 assert(!err);
947
948 if (!loadTexture(filename, data, &layout, &tex_width, &tex_height)) {
949 fprintf(stderr, "Error loading texture: %s\n", filename);
950 }
951
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500952 err = vkUnmapMemory(demo->device, tex_obj->mem);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700953 assert(!err);
954 }
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600955
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600956 tex_obj->imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600957 demo_set_image_layout(demo, tex_obj->image,
malnasseeb25d3f2015-06-03 17:28:38 -0400958 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600959 VK_IMAGE_LAYOUT_UNDEFINED,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600960 tex_obj->imageLayout);
961 /* 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 -0700962}
963
Mark Lobodzinskida9b1092015-04-16 11:44:05 -0500964static void demo_destroy_texture_image(struct demo *demo, struct texture_object *tex_objs)
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700965{
966 /* clean up staging resources */
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500967 vkFreeMemory(demo->device, tex_objs->mem);
Mike Stroyanebae8322015-04-17 12:36:38 -0600968 vkDestroyObject(demo->device, VK_OBJECT_TYPE_IMAGE, tex_objs->image);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700969}
970
971static void demo_prepare_textures(struct demo *demo)
972{
Tony Barbour72304ef2015-04-16 15:59:00 -0600973 const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600974 VkFormatProperties props;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700975 size_t size = sizeof(props);
Tony Barbourfdc2d352015-04-22 09:02:32 -0600976 VkResult U_ASSERT_ONLY err;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600977 uint32_t i;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600978
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600979 err = vkGetFormatInfo(demo->device, tex_format,
Tony Barbour72304ef2015-04-16 15:59:00 -0600980 VK_FORMAT_INFO_TYPE_PROPERTIES,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700981 &size, &props);
982 assert(!err);
983
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600984 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700985
FslNopper6a7e50e2015-05-06 21:42:01 +0200986 if ((props.linearTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) && !demo->use_staging_buffer) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700987 /* Device can texture using linear textures */
988 demo_prepare_texture_image(demo, tex_files[i], &demo->textures[i],
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -0600989 VK_IMAGE_TILING_LINEAR, VK_IMAGE_USAGE_SAMPLED_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
Tony Barbour72304ef2015-04-16 15:59:00 -0600990 } else if (props.optimalTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700991 /* Must use staging buffer to copy linear texture to optimized */
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600992 struct texture_object staging_texture;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700993
994 memset(&staging_texture, 0, sizeof(staging_texture));
995 demo_prepare_texture_image(demo, tex_files[i], &staging_texture,
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -0600996 VK_IMAGE_TILING_LINEAR, VK_IMAGE_USAGE_TRANSFER_SOURCE_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700997
998 demo_prepare_texture_image(demo, tex_files[i], &demo->textures[i],
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -0600999 VK_IMAGE_TILING_OPTIMAL,
1000 (VK_IMAGE_USAGE_TRANSFER_DESTINATION_BIT | VK_IMAGE_USAGE_SAMPLED_BIT),
1001 VK_MEMORY_PROPERTY_DEVICE_ONLY);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001002
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001003 demo_set_image_layout(demo, staging_texture.image,
malnasseeb25d3f2015-06-03 17:28:38 -04001004 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001005 staging_texture.imageLayout,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001006 VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001007
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001008 demo_set_image_layout(demo, demo->textures[i].image,
malnasseeb25d3f2015-06-03 17:28:38 -04001009 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001010 demo->textures[i].imageLayout,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001011 VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001012
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001013 VkImageCopy copy_region = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001014 .srcSubresource = { VK_IMAGE_ASPECT_COLOR, 0, 0 },
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001015 .srcOffset = { 0, 0, 0 },
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001016 .destSubresource = { VK_IMAGE_ASPECT_COLOR, 0, 0 },
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001017 .destOffset = { 0, 0, 0 },
1018 .extent = { staging_texture.tex_width, staging_texture.tex_height, 1 },
1019 };
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001020 vkCmdCopyImage(demo->cmd,
1021 staging_texture.image, VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL,
1022 demo->textures[i].image, VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL,
Courtney Goeltzenleuchter8e89a312015-03-25 11:25:10 -06001023 1, &copy_region);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001024
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001025 demo_set_image_layout(demo, demo->textures[i].image,
malnasseeb25d3f2015-06-03 17:28:38 -04001026 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001027 VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001028 demo->textures[i].imageLayout);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001029
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001030 demo_flush_init_cmd(demo);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001031
Courtney Goeltzenleuchterf3aeb2b2015-04-21 09:30:03 -06001032 demo_destroy_texture_image(demo, &staging_texture);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001033 } else {
Mike Stroyan8b89e072015-06-15 14:21:03 -06001034 /* Can't support VK_FORMAT_R8G8B8A8_UNORM !? */
1035 assert(!"No support for R8G8B8A8_UNORM as texture image format");
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001036 }
1037
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001038 const VkSamplerCreateInfo sampler = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001039 .sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001040 .pNext = NULL,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001041 .magFilter = VK_TEX_FILTER_NEAREST,
1042 .minFilter = VK_TEX_FILTER_NEAREST,
Tony Barbour72304ef2015-04-16 15:59:00 -06001043 .mipMode = VK_TEX_MIPMAP_MODE_BASE,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001044 .addressU = VK_TEX_ADDRESS_CLAMP,
1045 .addressV = VK_TEX_ADDRESS_CLAMP,
1046 .addressW = VK_TEX_ADDRESS_CLAMP,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001047 .mipLodBias = 0.0f,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001048 .maxAnisotropy = 1,
Tony Barbour72304ef2015-04-16 15:59:00 -06001049 .compareOp = VK_COMPARE_OP_NEVER,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001050 .minLod = 0.0f,
1051 .maxLod = 0.0f,
Tony Barbour72304ef2015-04-16 15:59:00 -06001052 .borderColor = VK_BORDER_COLOR_OPAQUE_WHITE,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001053 };
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001054
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001055 VkImageViewCreateInfo view = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001056 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001057 .pNext = NULL,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001058 .image = VK_NULL_HANDLE,
Tony Barbour72304ef2015-04-16 15:59:00 -06001059 .viewType = VK_IMAGE_VIEW_TYPE_2D,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001060 .format = tex_format,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001061 .channels = { VK_CHANNEL_SWIZZLE_R,
1062 VK_CHANNEL_SWIZZLE_G,
1063 VK_CHANNEL_SWIZZLE_B,
1064 VK_CHANNEL_SWIZZLE_A, },
1065 .subresourceRange = { VK_IMAGE_ASPECT_COLOR, 0, 1, 0, 1 },
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001066 .minLod = 0.0f,
1067 };
Jon Ashburnb2a66652015-01-16 09:37:43 -07001068
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001069 /* create sampler */
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001070 err = vkCreateSampler(demo->device, &sampler,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001071 &demo->textures[i].sampler);
1072 assert(!err);
1073
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001074 /* create image view */
1075 view.image = demo->textures[i].image;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001076 err = vkCreateImageView(demo->device, &view,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001077 &demo->textures[i].view);
1078 assert(!err);
1079 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001080}
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001081
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001082void demo_prepare_cube_data_buffer(struct demo *demo)
1083{
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -06001084 VkBufferCreateInfo buf_info;
1085 VkBufferViewCreateInfo view_info;
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -06001086 VkMemoryAllocInfo alloc_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001087 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
Mark Lobodzinskia6907f72015-04-16 08:52:00 -05001088 .pNext = NULL,
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001089 .allocationSize = 0,
Tony Barbour72304ef2015-04-16 15:59:00 -06001090 .memProps = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001091 .memPriority = VK_MEMORY_PRIORITY_NORMAL,
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001092 };
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001093 VkMemoryRequirements mem_reqs;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001094 size_t mem_reqs_size = sizeof(VkMemoryRequirements);
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001095 uint8_t *pData;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001096 int i;
1097 mat4x4 MVP, VP;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001098 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001099 struct vktexcube_vs_uniform data;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001100
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001101 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001102 mat4x4_mul(MVP, VP, demo->model_matrix);
1103 memcpy(data.mvp, MVP, sizeof(MVP));
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001104// dumpMatrix("MVP", MVP);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001105
1106 for (i=0; i<12*3; i++) {
1107 data.position[i][0] = g_vertex_buffer_data[i*3];
1108 data.position[i][1] = g_vertex_buffer_data[i*3+1];
1109 data.position[i][2] = g_vertex_buffer_data[i*3+2];
1110 data.position[i][3] = 1.0f;
1111 data.attr[i][0] = g_uv_buffer_data[2*i];
1112 data.attr[i][1] = g_uv_buffer_data[2*i + 1];
1113 data.attr[i][2] = 0;
1114 data.attr[i][3] = 0;
1115 }
1116
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001117 memset(&buf_info, 0, sizeof(buf_info));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001118 buf_info.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001119 buf_info.size = sizeof(data);
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -06001120 buf_info.usage = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001121 err = vkCreateBuffer(demo->device, &buf_info, &demo->uniform_data.buf);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001122 assert(!err);
1123
Mike Stroyanebae8322015-04-17 12:36:38 -06001124 err = vkGetObjectInfo(demo->device,
Mike Stroyanebae8322015-04-17 12:36:38 -06001125 VK_OBJECT_TYPE_BUFFER, demo->uniform_data.buf,
Tony Barbour72304ef2015-04-16 15:59:00 -06001126 VK_OBJECT_INFO_TYPE_MEMORY_REQUIREMENTS,
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001127 &mem_reqs_size, &mem_reqs);
1128 assert(!err && mem_reqs_size == sizeof(mem_reqs));
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001129
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001130 alloc_info.allocationSize = mem_reqs.size;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001131
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001132 err = vkAllocMemory(demo->device, &alloc_info, &(demo->uniform_data.mem));
1133 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001134
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001135 err = vkMapMemory(demo->device, demo->uniform_data.mem, 0, 0, 0, (void **) &pData);
1136 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001137
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001138 memcpy(pData, &data, sizeof data);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001139
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001140 err = vkUnmapMemory(demo->device, demo->uniform_data.mem);
1141 assert(!err);
1142
1143 err = vkBindObjectMemory(demo->device,
1144 VK_OBJECT_TYPE_BUFFER, demo->uniform_data.buf,
1145 demo->uniform_data.mem, 0);
1146 assert(!err);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001147
1148 memset(&view_info, 0, sizeof(view_info));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001149 view_info.sType = VK_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO;
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001150 view_info.buffer = demo->uniform_data.buf;
Tony Barbour72304ef2015-04-16 15:59:00 -06001151 view_info.viewType = VK_BUFFER_VIEW_TYPE_RAW;
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001152 view_info.offset = 0;
1153 view_info.range = sizeof(data);
1154
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001155 err = vkCreateBufferView(demo->device, &view_info, &demo->uniform_data.view);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001156 assert(!err);
1157
Chia-I Wuae721ba2015-05-25 16:27:55 +08001158 demo->uniform_data.desc.bufferView = demo->uniform_data.view;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001159}
1160
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001161static void demo_prepare_descriptor_layout(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001162{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001163 const VkDescriptorSetLayoutBinding layout_bindings[2] = {
Chia-I Wua2aa8632015-03-26 15:04:41 +08001164 [0] = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001165 .descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
Chia-I Wu6d29a412015-05-25 16:22:52 +08001166 .arraySize = 1,
Tony Barbour72304ef2015-04-16 15:59:00 -06001167 .stageFlags = VK_SHADER_STAGE_VERTEX_BIT,
Chia-I Wu91e8e212015-03-27 12:56:09 +08001168 .pImmutableSamplers = NULL,
Chia-I Wua2aa8632015-03-26 15:04:41 +08001169 },
1170 [1] = {
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -06001171 .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
Chia-I Wu6d29a412015-05-25 16:22:52 +08001172 .arraySize = DEMO_TEXTURE_COUNT,
Tony Barbour72304ef2015-04-16 15:59:00 -06001173 .stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT,
Chia-I Wu91e8e212015-03-27 12:56:09 +08001174 .pImmutableSamplers = NULL,
Chia-I Wua2aa8632015-03-26 15:04:41 +08001175 },
1176 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001177 const VkDescriptorSetLayoutCreateInfo descriptor_layout = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001178 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001179 .pNext = NULL,
Chia-I Wua2aa8632015-03-26 15:04:41 +08001180 .count = 2,
1181 .pBinding = layout_bindings,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001182 };
Tony Barbourfdc2d352015-04-22 09:02:32 -06001183 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001184
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001185 err = vkCreateDescriptorSetLayout(demo->device,
Chia-I Wub58c24a2015-03-26 15:27:55 +08001186 &descriptor_layout, &demo->desc_layout);
1187 assert(!err);
1188
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001189 const VkPipelineLayoutCreateInfo pPipelineLayoutCreateInfo = {
1190 .sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
1191 .pNext = NULL,
1192 .descriptorSetCount = 1,
1193 .pSetLayouts = &demo->desc_layout,
1194 };
1195
1196 err = vkCreatePipelineLayout(demo->device,
1197 &pPipelineLayoutCreateInfo,
1198 &demo->pipeline_layout);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001199 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001200}
1201
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001202static VkShader demo_prepare_shader(struct demo *demo,
Tony Barbour72304ef2015-04-16 15:59:00 -06001203 VkShaderStage stage,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001204 const void *code,
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001205 size_t size)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001206{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001207 VkShaderCreateInfo createInfo;
1208 VkShader shader;
1209 VkResult err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001210
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001211
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001212 createInfo.sType = VK_STRUCTURE_TYPE_SHADER_CREATE_INFO;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001213 createInfo.pNext = NULL;
1214
Cody Northrop1fedb212015-05-28 11:27:16 -06001215 if (!demo->use_glsl) {
1216 createInfo.codeSize = size;
1217 createInfo.pCode = code;
1218 createInfo.flags = 0;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001219
Cody Northrop1fedb212015-05-28 11:27:16 -06001220 err = vkCreateShader(demo->device, &createInfo, &shader);
1221 if (err) {
1222 free((void *) createInfo.pCode);
1223 }
1224 } else {
1225 // Create fake SPV structure to feed GLSL
1226 // to the driver "under the covers"
1227 createInfo.codeSize = 3 * sizeof(uint32_t) + size + 1;
1228 createInfo.pCode = malloc(createInfo.codeSize);
1229 createInfo.flags = 0;
1230
1231 /* try version 0 first: VkShaderStage followed by GLSL */
1232 ((uint32_t *) createInfo.pCode)[0] = ICD_SPV_MAGIC;
1233 ((uint32_t *) createInfo.pCode)[1] = 0;
1234 ((uint32_t *) createInfo.pCode)[2] = stage;
1235 memcpy(((uint32_t *) createInfo.pCode + 3), code, size + 1);
1236
1237 err = vkCreateShader(demo->device, &createInfo, &shader);
1238 if (err) {
1239 free((void *) createInfo.pCode);
1240 return (VkShader) VK_NULL_HANDLE;
1241 }
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001242 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001243 return shader;
1244}
1245
Cody Northrop8b12a1f2015-03-17 15:55:58 -06001246char *demo_read_spv(const char *filename, size_t *psize)
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001247{
1248 long int size;
Tony Barbourf9921732015-04-22 11:36:22 -06001249 size_t U_ASSERT_ONLY retval;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001250 void *shader_code;
1251
1252 FILE *fp = fopen(filename, "rb");
1253 if (!fp) return NULL;
1254
1255 fseek(fp, 0L, SEEK_END);
1256 size = ftell(fp);
1257
1258 fseek(fp, 0L, SEEK_SET);
1259
1260 shader_code = malloc(size);
Tony Barbourfdc2d352015-04-22 09:02:32 -06001261 retval = fread(shader_code, size, 1, fp);
1262 assert(retval == 1);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001263
1264 *psize = size;
1265
1266 return shader_code;
1267}
1268
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001269static VkShader demo_prepare_vs(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001270{
Cody Northrop1fedb212015-05-28 11:27:16 -06001271 if (!demo->use_glsl) {
1272 void *vertShaderCode;
1273 size_t size;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001274
Cody Northrop1fedb212015-05-28 11:27:16 -06001275 vertShaderCode = demo_read_spv("cube-vert.spv", &size);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001276
Cody Northrop1fedb212015-05-28 11:27:16 -06001277 return demo_prepare_shader(demo, VK_SHADER_STAGE_VERTEX,
1278 vertShaderCode, size);
1279 } else {
1280 static const char *vertShaderText =
1281 "#version 140\n"
1282 "#extension GL_ARB_separate_shader_objects : enable\n"
1283 "#extension GL_ARB_shading_language_420pack : enable\n"
1284 "\n"
1285 "layout(binding = 0) uniform buf {\n"
1286 " mat4 MVP;\n"
1287 " vec4 position[12*3];\n"
1288 " vec4 attr[12*3];\n"
1289 "} ubuf;\n"
1290 "\n"
1291 "layout (location = 0) out vec4 texcoord;\n"
1292 "\n"
1293 "void main() \n"
1294 "{\n"
1295 " texcoord = ubuf.attr[gl_VertexID];\n"
1296 " gl_Position = ubuf.MVP * ubuf.position[gl_VertexID];\n"
1297 "\n"
1298 " // GL->VK conventions\n"
1299 " gl_Position.y = -gl_Position.y;\n"
1300 " gl_Position.z = (gl_Position.z + gl_Position.w) / 2.0;\n"
1301 "}\n";
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001302
Cody Northrop1fedb212015-05-28 11:27:16 -06001303 return demo_prepare_shader(demo, VK_SHADER_STAGE_VERTEX,
1304 (const void *) vertShaderText,
1305 strlen(vertShaderText));
1306 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001307}
1308
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001309static VkShader demo_prepare_fs(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001310{
Cody Northrop1fedb212015-05-28 11:27:16 -06001311 if (!demo->use_glsl) {
1312 void *fragShaderCode;
1313 size_t size;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001314
Cody Northrop1fedb212015-05-28 11:27:16 -06001315 fragShaderCode = demo_read_spv("cube-frag.spv", &size);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001316
Cody Northrop1fedb212015-05-28 11:27:16 -06001317 return demo_prepare_shader(demo, VK_SHADER_STAGE_FRAGMENT,
1318 fragShaderCode, size);
1319 } else {
1320 static const char *fragShaderText =
1321 "#version 140\n"
1322 "#extension GL_ARB_separate_shader_objects : enable\n"
1323 "#extension GL_ARB_shading_language_420pack : enable\n"
1324 "layout (binding = 1) uniform sampler2D tex;\n"
1325 "\n"
1326 "layout (location = 0) in vec4 texcoord;\n"
1327 "layout (location = 0) out vec4 uFragColor;\n"
1328 "void main() {\n"
1329 " uFragColor = texture(tex, texcoord.xy);\n"
1330 "}\n";
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001331
Cody Northrop1fedb212015-05-28 11:27:16 -06001332 return demo_prepare_shader(demo, VK_SHADER_STAGE_FRAGMENT,
1333 (const void *) fragShaderText,
1334 strlen(fragShaderText));
1335 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001336}
1337
1338static void demo_prepare_pipeline(struct demo *demo)
1339{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001340 VkGraphicsPipelineCreateInfo pipeline;
1341 VkPipelineIaStateCreateInfo ia;
1342 VkPipelineRsStateCreateInfo rs;
1343 VkPipelineCbStateCreateInfo cb;
1344 VkPipelineDsStateCreateInfo ds;
1345 VkPipelineShaderStageCreateInfo vs;
1346 VkPipelineShaderStageCreateInfo fs;
1347 VkPipelineVpStateCreateInfo vp;
1348 VkPipelineMsStateCreateInfo ms;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001349 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001350
1351 memset(&pipeline, 0, sizeof(pipeline));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001352 pipeline.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001353 pipeline.layout = demo->pipeline_layout;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001354
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001355 memset(&ia, 0, sizeof(ia));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001356 ia.sType = VK_STRUCTURE_TYPE_PIPELINE_IA_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06001357 ia.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001358
1359 memset(&rs, 0, sizeof(rs));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001360 rs.sType = VK_STRUCTURE_TYPE_PIPELINE_RS_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06001361 rs.fillMode = VK_FILL_MODE_SOLID;
1362 rs.cullMode = VK_CULL_MODE_BACK;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001363 rs.frontFace = VK_FRONT_FACE_CCW;
Chia-I Wubb67e6e2015-04-22 14:20:52 +08001364 rs.depthClipEnable = VK_TRUE;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001365
1366 memset(&cb, 0, sizeof(cb));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001367 cb.sType = VK_STRUCTURE_TYPE_PIPELINE_CB_STATE_CREATE_INFO;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001368 VkPipelineCbAttachmentState att_state[1];
Tony Barbour29645d02015-01-16 14:27:35 -07001369 memset(att_state, 0, sizeof(att_state));
1370 att_state[0].format = demo->format;
1371 att_state[0].channelWriteMask = 0xf;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001372 att_state[0].blendEnable = VK_FALSE;
Tony Barbour29645d02015-01-16 14:27:35 -07001373 cb.attachmentCount = 1;
1374 cb.pAttachments = att_state;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001375
Tony Barbour29645d02015-01-16 14:27:35 -07001376 memset(&vp, 0, sizeof(vp));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001377 vp.sType = VK_STRUCTURE_TYPE_PIPELINE_VP_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06001378 vp.viewportCount = 1;
Tony Barbour29645d02015-01-16 14:27:35 -07001379
1380 memset(&ds, 0, sizeof(ds));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001381 ds.sType = VK_STRUCTURE_TYPE_PIPELINE_DS_STATE_CREATE_INFO;
Tony Barbour29645d02015-01-16 14:27:35 -07001382 ds.format = demo->depth.format;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001383 ds.depthTestEnable = VK_TRUE;
1384 ds.depthWriteEnable = VK_TRUE;
Tony Barbour72304ef2015-04-16 15:59:00 -06001385 ds.depthCompareOp = VK_COMPARE_OP_LESS_EQUAL;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001386 ds.depthBoundsEnable = VK_FALSE;
1387 ds.back.stencilFailOp = VK_STENCIL_OP_KEEP;
1388 ds.back.stencilPassOp = VK_STENCIL_OP_KEEP;
Tony Barbour72304ef2015-04-16 15:59:00 -06001389 ds.back.stencilCompareOp = VK_COMPARE_OP_ALWAYS;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001390 ds.stencilTestEnable = VK_FALSE;
Tony Barbour29645d02015-01-16 14:27:35 -07001391 ds.front = ds.back;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001392
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001393 memset(&vs, 0, sizeof(vs));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001394 vs.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1395 vs.shader.stage = VK_SHADER_STAGE_VERTEX;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001396 vs.shader.shader = demo_prepare_vs(demo);
Mike Stroyanebae8322015-04-17 12:36:38 -06001397 assert(vs.shader.shader != VK_NULL_HANDLE);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001398
1399 memset(&fs, 0, sizeof(fs));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001400 fs.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1401 fs.shader.stage = VK_SHADER_STAGE_FRAGMENT;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001402 fs.shader.shader = demo_prepare_fs(demo);
Mike Stroyanebae8322015-04-17 12:36:38 -06001403 assert(fs.shader.shader != VK_NULL_HANDLE);
Tony Barbour29645d02015-01-16 14:27:35 -07001404
1405 memset(&ms, 0, sizeof(ms));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001406 ms.sType = VK_STRUCTURE_TYPE_PIPELINE_MS_STATE_CREATE_INFO;
Tony Barbour29645d02015-01-16 14:27:35 -07001407 ms.sampleMask = 1;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001408 ms.multisampleEnable = VK_FALSE;
Tony Barbour29645d02015-01-16 14:27:35 -07001409 ms.samples = 1;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001410
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001411 pipeline.pNext = (const void *) &ia;
1412 ia.pNext = (const void *) &rs;
1413 rs.pNext = (const void *) &cb;
1414 cb.pNext = (const void *) &ms;
1415 ms.pNext = (const void *) &vp;
1416 vp.pNext = (const void *) &ds;
1417 ds.pNext = (const void *) &vs;
1418 vs.pNext = (const void *) &fs;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001419
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001420 err = vkCreateGraphicsPipeline(demo->device, &pipeline, &demo->pipeline);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001421 assert(!err);
1422
Mike Stroyanebae8322015-04-17 12:36:38 -06001423 vkDestroyObject(demo->device, VK_OBJECT_TYPE_SHADER, vs.shader.shader);
1424 vkDestroyObject(demo->device, VK_OBJECT_TYPE_SHADER, fs.shader.shader);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001425}
1426
1427static void demo_prepare_dynamic_states(struct demo *demo)
1428{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001429 VkDynamicVpStateCreateInfo viewport_create;
1430 VkDynamicRsStateCreateInfo raster;
1431 VkDynamicCbStateCreateInfo color_blend;
1432 VkDynamicDsStateCreateInfo depth_stencil;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001433 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001434
Tony Barbour29645d02015-01-16 14:27:35 -07001435 memset(&viewport_create, 0, sizeof(viewport_create));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001436 viewport_create.sType = VK_STRUCTURE_TYPE_DYNAMIC_VP_STATE_CREATE_INFO;
Courtney Goeltzenleuchter1710d8d2015-02-11 14:13:34 -07001437 viewport_create.viewportAndScissorCount = 1;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001438 VkViewport viewport;
Piers Daniell735ee532015-02-23 16:23:13 -07001439 memset(&viewport, 0, sizeof(viewport));
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001440 viewport.height = (float) demo->height;
1441 viewport.width = (float) demo->width;
1442 viewport.minDepth = (float) 0.0f;
1443 viewport.maxDepth = (float) 1.0f;
Piers Daniell735ee532015-02-23 16:23:13 -07001444 viewport_create.pViewports = &viewport;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001445 VkRect scissor;
Piers Daniell735ee532015-02-23 16:23:13 -07001446 memset(&scissor, 0, sizeof(scissor));
Courtney Goeltzenleuchter1710d8d2015-02-11 14:13:34 -07001447 scissor.extent.width = demo->width;
1448 scissor.extent.height = demo->height;
1449 scissor.offset.x = 0;
1450 scissor.offset.y = 0;
Courtney Goeltzenleuchter1710d8d2015-02-11 14:13:34 -07001451 viewport_create.pScissors = &scissor;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001452
1453 memset(&raster, 0, sizeof(raster));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001454 raster.sType = VK_STRUCTURE_TYPE_DYNAMIC_RS_STATE_CREATE_INFO;
Piers Daniell735ee532015-02-23 16:23:13 -07001455 raster.pointSize = 1.0;
1456 raster.lineWidth = 1.0;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001457
1458 memset(&color_blend, 0, sizeof(color_blend));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001459 color_blend.sType = VK_STRUCTURE_TYPE_DYNAMIC_CB_STATE_CREATE_INFO;
Piers Daniell735ee532015-02-23 16:23:13 -07001460 color_blend.blendConst[0] = 1.0f;
1461 color_blend.blendConst[1] = 1.0f;
1462 color_blend.blendConst[2] = 1.0f;
1463 color_blend.blendConst[3] = 1.0f;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001464
1465 memset(&depth_stencil, 0, sizeof(depth_stencil));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001466 depth_stencil.sType = VK_STRUCTURE_TYPE_DYNAMIC_DS_STATE_CREATE_INFO;
Piers Daniell735ee532015-02-23 16:23:13 -07001467 depth_stencil.minDepth = 0.0f;
1468 depth_stencil.maxDepth = 1.0f;
Tony Barbour29645d02015-01-16 14:27:35 -07001469 depth_stencil.stencilBackRef = 0;
1470 depth_stencil.stencilFrontRef = 0;
1471 depth_stencil.stencilReadMask = 0xff;
1472 depth_stencil.stencilWriteMask = 0xff;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001473
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001474 err = vkCreateDynamicViewportState(demo->device, &viewport_create, &demo->viewport);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001475 assert(!err);
1476
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001477 err = vkCreateDynamicRasterState(demo->device, &raster, &demo->raster);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001478 assert(!err);
1479
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001480 err = vkCreateDynamicColorBlendState(demo->device,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001481 &color_blend, &demo->color_blend);
1482 assert(!err);
1483
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001484 err = vkCreateDynamicDepthStencilState(demo->device,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001485 &depth_stencil, &demo->depth_stencil);
1486 assert(!err);
1487}
1488
Chia-I Wu63ea9262015-03-26 13:14:16 +08001489static void demo_prepare_descriptor_pool(struct demo *demo)
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001490{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001491 const VkDescriptorTypeCount type_counts[2] = {
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001492 [0] = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001493 .type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001494 .count = 1,
1495 },
1496 [1] = {
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -06001497 .type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001498 .count = DEMO_TEXTURE_COUNT,
1499 },
1500 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001501 const VkDescriptorPoolCreateInfo descriptor_pool = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001502 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001503 .pNext = NULL,
1504 .count = 2,
1505 .pTypeCount = type_counts,
1506 };
Tony Barbourfdc2d352015-04-22 09:02:32 -06001507 VkResult U_ASSERT_ONLY err;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001508
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001509 err = vkCreateDescriptorPool(demo->device,
1510 VK_DESCRIPTOR_POOL_USAGE_ONE_SHOT, 1,
Chia-I Wu63ea9262015-03-26 13:14:16 +08001511 &descriptor_pool, &demo->desc_pool);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001512 assert(!err);
1513}
1514
1515static void demo_prepare_descriptor_set(struct demo *demo)
1516{
Chia-I Wuae721ba2015-05-25 16:27:55 +08001517 VkDescriptorInfo tex_descs[DEMO_TEXTURE_COUNT];
1518 VkWriteDescriptorSet writes[2];
Tony Barbourfdc2d352015-04-22 09:02:32 -06001519 VkResult U_ASSERT_ONLY err;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001520 uint32_t count;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001521 uint32_t i;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001522
Mike Stroyanebae8322015-04-17 12:36:38 -06001523 err = vkAllocDescriptorSets(demo->device, demo->desc_pool,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001524 VK_DESCRIPTOR_SET_USAGE_STATIC,
Chia-I Wu87544e72015-02-23 10:41:08 -07001525 1, &demo->desc_layout,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001526 &demo->desc_set, &count);
1527 assert(!err && count == 1);
1528
Mike Stroyanebae8322015-04-17 12:36:38 -06001529 vkClearDescriptorSets(demo->device, demo->desc_pool, 1, &demo->desc_set);
Chia-I Wuae721ba2015-05-25 16:27:55 +08001530
1531 memset(&tex_descs, 0, sizeof(tex_descs));
1532 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
1533 tex_descs[i].sampler = demo->textures[i].sampler;
1534 tex_descs[i].imageView = demo->textures[i].view;
1535 tex_descs[i].imageLayout = VK_IMAGE_LAYOUT_GENERAL;
1536 }
1537
1538 memset(&writes, 0, sizeof(writes));
1539
1540 writes[0].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
1541 writes[0].destSet = demo->desc_set;
1542 writes[0].count = 1;
1543 writes[0].descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
1544 writes[0].pDescriptors = &demo->uniform_data.desc;
1545
1546 writes[1].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
1547 writes[1].destSet = demo->desc_set;
1548 writes[1].destBinding = 1;
1549 writes[1].count = DEMO_TEXTURE_COUNT;
1550 writes[1].descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
1551 writes[1].pDescriptors = tex_descs;
1552
1553 err = vkUpdateDescriptorSets(demo->device, 2, writes, 0, NULL);
1554 assert(!err);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001555}
1556
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001557static void demo_prepare(struct demo *demo)
1558{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001559 const VkCmdBufferCreateInfo cmd = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001560 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001561 .pNext = NULL,
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07001562 .queueNodeIndex = demo->graphics_queue_node_index,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001563 .flags = 0,
1564 };
Tony Barbourfdc2d352015-04-22 09:02:32 -06001565 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001566
1567 demo_prepare_buffers(demo);
1568 demo_prepare_depth(demo);
1569 demo_prepare_textures(demo);
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001570 demo_prepare_cube_data_buffer(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001571
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001572 demo_prepare_descriptor_layout(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001573 demo_prepare_pipeline(demo);
1574 demo_prepare_dynamic_states(demo);
1575
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001576 for (int i = 0; i < DEMO_BUFFER_COUNT; i++) {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001577 err = vkCreateCommandBuffer(demo->device, &cmd, &demo->buffers[i].cmd);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001578 assert(!err);
1579 }
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001580
Chia-I Wu63ea9262015-03-26 13:14:16 +08001581 demo_prepare_descriptor_pool(demo);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001582 demo_prepare_descriptor_set(demo);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001583
1584
1585 for (int i = 0; i < DEMO_BUFFER_COUNT; i++) {
1586 demo->current_buffer = i;
1587 demo_draw_build_cmd(demo, demo->buffers[i].cmd);
1588 }
1589
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001590 /*
1591 * Prepare functions above may generate pipeline commands
1592 * that need to be flushed before beginning the render loop.
1593 */
1594 demo_flush_init_cmd(demo);
1595
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001596 demo->current_buffer = 0;
Jon Ashburn8d26c062015-04-24 09:46:24 -07001597 demo->prepared = true;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001598}
1599
Ian Elliott639ca472015-04-16 15:23:05 -06001600#ifdef _WIN32
1601static void demo_run(struct demo *demo)
1602{
Courtney Goeltzenleuchterb7e22702015-04-27 14:56:34 -06001603 if (!demo->prepared)
1604 return;
Ian Elliott639ca472015-04-16 15:23:05 -06001605 // Wait for work to finish before updating MVP.
1606 vkDeviceWaitIdle(demo->device);
1607 demo_update_data_buffer(demo);
1608
1609 demo_draw(demo);
1610
1611 // Wait for work to finish before updating MVP.
1612 vkDeviceWaitIdle(demo->device);
1613}
1614
1615// On MS-Windows, make this a global, so it's available to WndProc()
1616struct demo demo;
1617
1618// MS-Windows event handling function:
1619LRESULT CALLBACK WndProc(HWND hWnd,
1620 UINT uMsg,
1621 WPARAM wParam,
1622 LPARAM lParam)
1623{
Ian Elliott639ca472015-04-16 15:23:05 -06001624 switch(uMsg)
1625 {
Ian Elliott639ca472015-04-16 15:23:05 -06001626 case WM_CLOSE:
1627 PostQuitMessage(0);
Tony Barbour3dddd5d2015-04-29 16:19:20 -06001628 break;
Ian Elliott639ca472015-04-16 15:23:05 -06001629 case WM_PAINT:
1630 demo_run(&demo);
1631 return 0;
1632 default:
1633 break;
1634 }
1635 return (DefWindowProc(hWnd, uMsg, wParam, lParam));
1636}
1637
1638static void demo_create_window(struct demo *demo)
1639{
1640 WNDCLASSEX win_class;
1641
1642 // Initialize the window class structure:
1643 win_class.cbSize = sizeof(WNDCLASSEX);
1644 win_class.style = CS_HREDRAW | CS_VREDRAW;
1645 win_class.lpfnWndProc = WndProc;
1646 win_class.cbClsExtra = 0;
1647 win_class.cbWndExtra = 0;
1648 win_class.hInstance = demo->connection; // hInstance
1649 win_class.hIcon = LoadIcon(NULL, IDI_APPLICATION);
1650 win_class.hCursor = LoadCursor(NULL, IDC_ARROW);
1651 win_class.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
1652 win_class.lpszMenuName = NULL;
1653 win_class.lpszClassName = demo->name;
1654 win_class.hIconSm = LoadIcon(NULL, IDI_WINLOGO);
1655 // Register window class:
1656 if (!RegisterClassEx(&win_class)) {
1657 // It didn't work, so try to give a useful error:
1658 printf("Unexpected error trying to start the application!\n");
1659 fflush(stdout);
1660 exit(1);
1661 }
1662 // Create window with the registered class:
Mike Stroyanb46779e2015-06-15 14:20:13 -06001663 RECT wr = { 0, 0, demo->width, demo->height };
1664 AdjustWindowRect(&wr, WS_OVERLAPPEDWINDOW, FALSE);
Ian Elliott639ca472015-04-16 15:23:05 -06001665 demo->window = CreateWindowEx(0,
1666 demo->name, // class name
1667 demo->name, // app name
1668 WS_OVERLAPPEDWINDOW | // window style
1669 WS_VISIBLE |
1670 WS_SYSMENU,
1671 100,100, // x/y coords
Mike Stroyanb46779e2015-06-15 14:20:13 -06001672 wr.right-wr.left, // width
1673 wr.bottom-wr.top, // height
Ian Elliott639ca472015-04-16 15:23:05 -06001674 NULL, // handle to parent
1675 NULL, // handle to menu
1676 demo->connection, // hInstance
1677 NULL); // no extra parameters
1678 if (!demo->window) {
1679 // It didn't work, so try to give a useful error:
1680 printf("Cannot create a window in which to draw!\n");
1681 fflush(stdout);
1682 exit(1);
1683 }
1684}
1685#else // _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001686static void demo_handle_event(struct demo *demo,
1687 const xcb_generic_event_t *event)
1688{
Piers Daniell735ee532015-02-23 16:23:13 -07001689 uint8_t event_code = event->response_type & 0x7f;
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07001690 switch (event_code) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001691 case XCB_EXPOSE:
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07001692 // TODO: Resize window
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001693 break;
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07001694 case XCB_CLIENT_MESSAGE:
1695 if((*(xcb_client_message_event_t*)event).data.data32[0] ==
1696 (*demo->atom_wm_delete_window).atom) {
1697 demo->quit = true;
1698 }
1699 break;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001700 case XCB_KEY_RELEASE:
1701 {
1702 const xcb_key_release_event_t *key =
1703 (const xcb_key_release_event_t *) event;
1704
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001705 switch (key->detail) {
1706 case 0x9: // Escape
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001707 demo->quit = true;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001708 break;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001709 case 0x71: // left arrow key
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001710 demo->spin_angle += demo->spin_increment;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001711 break;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001712 case 0x72: // right arrow key
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001713 demo->spin_angle -= demo->spin_increment;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001714 break;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001715 case 0x41:
1716 demo->pause = !demo->pause;
Piers Daniell735ee532015-02-23 16:23:13 -07001717 break;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001718 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001719 }
1720 break;
1721 default:
1722 break;
1723 }
1724}
1725
1726static void demo_run(struct demo *demo)
1727{
1728 xcb_flush(demo->connection);
1729
1730 while (!demo->quit) {
1731 xcb_generic_event_t *event;
1732
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07001733 if (demo->pause) {
1734 event = xcb_wait_for_event(demo->connection);
1735 } else {
1736 event = xcb_poll_for_event(demo->connection);
1737 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001738 if (event) {
1739 demo_handle_event(demo, event);
1740 free(event);
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07001741 }
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07001742
1743 // Wait for work to finish before updating MVP.
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001744 vkDeviceWaitIdle(demo->device);
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07001745 demo_update_data_buffer(demo);
1746
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07001747 demo_draw(demo);
Courtney Goeltzenleuchter1454f3c2014-11-18 11:28:09 -07001748
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07001749 // Wait for work to finish before updating MVP.
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001750 vkDeviceWaitIdle(demo->device);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001751 }
1752}
1753
1754static void demo_create_window(struct demo *demo)
1755{
1756 uint32_t value_mask, value_list[32];
1757
1758 demo->window = xcb_generate_id(demo->connection);
1759
1760 value_mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK;
1761 value_list[0] = demo->screen->black_pixel;
1762 value_list[1] = XCB_EVENT_MASK_KEY_RELEASE |
1763 XCB_EVENT_MASK_EXPOSURE;
1764
1765 xcb_create_window(demo->connection,
1766 XCB_COPY_FROM_PARENT,
1767 demo->window, demo->screen->root,
1768 0, 0, demo->width, demo->height, 0,
1769 XCB_WINDOW_CLASS_INPUT_OUTPUT,
1770 demo->screen->root_visual,
1771 value_mask, value_list);
1772
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07001773 /* Magic code that will send notification when window is destroyed */
1774 xcb_intern_atom_cookie_t cookie = xcb_intern_atom(demo->connection, 1, 12,
1775 "WM_PROTOCOLS");
1776 xcb_intern_atom_reply_t* reply = xcb_intern_atom_reply(demo->connection, cookie, 0);
1777
1778 xcb_intern_atom_cookie_t cookie2 = xcb_intern_atom(demo->connection, 0, 16, "WM_DELETE_WINDOW");
1779 demo->atom_wm_delete_window = xcb_intern_atom_reply(demo->connection, cookie2, 0);
1780
1781 xcb_change_property(demo->connection, XCB_PROP_MODE_REPLACE,
1782 demo->window, (*reply).atom, 4, 32, 1,
1783 &(*demo->atom_wm_delete_window).atom);
1784 free(reply);
1785
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001786 xcb_map_window(demo->connection, demo->window);
1787}
Ian Elliott639ca472015-04-16 15:23:05 -06001788#endif // _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001789
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001790static void demo_init_vk(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001791{
Tobin Ehlis99239dc2015-04-16 18:04:57 -06001792 VkResult err;
1793 // Extensions to enable
1794 const char *ext_names[] = {
Chia-I Wucbb564e2015-04-16 22:02:10 +08001795 "VK_WSI_LunarG",
Tony Barbour3dddd5d2015-04-29 16:19:20 -06001796 "Validation"
Tobin Ehlis99239dc2015-04-16 18:04:57 -06001797 };
1798 size_t extSize = sizeof(uint32_t);
1799 uint32_t extCount = 0;
1800 err = vkGetGlobalExtensionInfo(VK_EXTENSION_INFO_TYPE_COUNT, 0, &extSize, &extCount);
1801 assert(!err);
1802
1803 VkExtensionProperties extProp;
1804 extSize = sizeof(VkExtensionProperties);
Tony Barbourfdc2d352015-04-22 09:02:32 -06001805 bool32_t U_ASSERT_ONLY extFound = 0;
Tobin Ehlis99239dc2015-04-16 18:04:57 -06001806 for (uint32_t i = 0; i < extCount; i++) {
1807 err = vkGetGlobalExtensionInfo(VK_EXTENSION_INFO_TYPE_PROPERTIES, i, &extSize, &extProp);
1808 if (!strcmp(ext_names[0], extProp.extName))
1809 extFound = 1;
1810 }
Ian Elliott65152912015-04-28 13:22:33 -06001811 if (!extFound) {
1812 ERR_EXIT("vkGetGlobalExtensionInfo failed to find the "
1813 "\"VK_WSI_LunarG\" extension.\n\nDo you have a compatible "
1814 "Vulkan installable client driver (ICD) installed?\nPlease "
1815 "look at the Getting Started guide for additional "
1816 "information.\n",
1817 "vkCreateInstance Failure");
1818 }
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001819 const VkApplicationInfo app = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001820 .sType = VK_STRUCTURE_TYPE_APPLICATION_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001821 .pNext = NULL,
Ian Elliott44e33f72015-04-28 10:52:52 -06001822 .pAppName = APP_SHORT_NAME,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001823 .appVersion = 0,
Ian Elliott44e33f72015-04-28 10:52:52 -06001824 .pEngineName = APP_SHORT_NAME,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001825 .engineVersion = 0,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001826 .apiVersion = VK_API_VERSION,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001827 };
Tony Barbour3dddd5d2015-04-29 16:19:20 -06001828 VkInstanceCreateInfo inst_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001829 .sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
Jon Ashburnab46b362015-04-04 14:52:07 -06001830 .pNext = NULL,
1831 .pAppInfo = &app,
1832 .pAllocCb = NULL,
Tobin Ehlis99239dc2015-04-16 18:04:57 -06001833 .extensionCount = 1,
1834 .ppEnabledExtensionNames = ext_names,
Jon Ashburnab46b362015-04-04 14:52:07 -06001835 };
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -06001836 const VkDeviceQueueCreateInfo queue = {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001837 .queueNodeIndex = 0,
1838 .queueCount = 1,
1839 };
Ian Elliott097d9f32015-04-28 11:35:02 -06001840
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -06001841 const VkDeviceCreateInfo device = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001842 .sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001843 .pNext = NULL,
1844 .queueRecordCount = 1,
1845 .pRequestedQueues = &queue,
Tobin Ehlis99239dc2015-04-16 18:04:57 -06001846 .extensionCount = 1,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001847 .ppEnabledExtensionNames = ext_names,
Jon Ashburn7842d522015-05-18 09:06:15 -06001848 .flags = 0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001849 };
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001850 uint32_t gpu_count;
1851 uint32_t i;
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07001852 size_t data_size;
1853 uint32_t queue_count;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001854
Tony Barbour3dddd5d2015-04-29 16:19:20 -06001855 if (demo->validate) {
1856 inst_info.extensionCount = 2;
1857 }
1858
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001859 err = vkCreateInstance(&inst_info, &demo->inst);
Ian Elliott07264132015-04-28 11:35:02 -06001860 if (err == VK_ERROR_INCOMPATIBLE_DRIVER) {
1861 ERR_EXIT("Cannot find a compatible Vulkan installable client driver "
Ian Elliott65152912015-04-28 13:22:33 -06001862 "(ICD).\n\nPlease look at the Getting Started guide for "
Ian Elliott07264132015-04-28 11:35:02 -06001863 "additional information.\n",
1864 "vkCreateInstance Failure");
Tony Barbour3dddd5d2015-04-29 16:19:20 -06001865 } else if (err == VK_ERROR_INVALID_EXTENSION) {
1866 ERR_EXIT("Cannot find a specified extension library"
1867 ".\nMake sure your layers path is set appropriately\n",
1868 "vkCreateInstance Failure");
Ian Elliott07264132015-04-28 11:35:02 -06001869 } else if (err) {
Ian Elliott65152912015-04-28 13:22:33 -06001870 ERR_EXIT("vkCreateInstance failed.\n\nDo you have a compatible Vulkan "
1871 "installable client driver (ICD) installed?\nPlease look at "
Ian Elliott07264132015-04-28 11:35:02 -06001872 "the Getting Started guide for additional information.\n",
1873 "vkCreateInstance Failure");
Ian Elliott3979e282015-04-03 15:24:55 -06001874 }
Jon Ashburnab46b362015-04-04 14:52:07 -06001875
Tony Barbour3dddd5d2015-04-29 16:19:20 -06001876
Jon Ashburn07c0c0c2015-04-15 11:31:12 -06001877 gpu_count = 1;
1878 err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, &demo->gpu);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001879 assert(!err && gpu_count == 1);
1880
Tony Barbour3dddd5d2015-04-29 16:19:20 -06001881 if (demo->validate) {
1882 vkDbgRegisterMsgCallback(demo->inst, dbgFunc, NULL);
1883 }
1884
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001885 err = vkCreateDevice(demo->gpu, &device, &demo->device);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001886 assert(!err);
1887
Courtney Goeltzenleuchter8f0767f2015-06-10 17:33:55 -06001888 demo->fpGetDisplayInfoWSI = vkGetDeviceProcAddr(demo->device, "vkGetDisplayInfoWSI");
1889 if (demo->fpGetDisplayInfoWSI == NULL)
1890 ERR_EXIT("vkGetDeviceProcAddr failed to find vkGetDisplayInfoWSI",
1891 "vkGetDeviceProcAddr Failure");
Jon Ashburn0b85d052015-05-21 18:13:33 -06001892 demo->fpCreateSwapChainWSI = vkGetDeviceProcAddr(demo->device, "vkCreateSwapChainWSI");
1893 if (demo->fpCreateSwapChainWSI == NULL)
1894 ERR_EXIT("vkGetDeviceProcAddr failed to find vkCreateSwapChainWSI",
1895 "vkGetDeviceProcAddr Failure");
1896 demo->fpDestroySwapChainWSI = vkGetDeviceProcAddr(demo->device, "vkDestroySwapChainWSI");
1897 if (demo->fpDestroySwapChainWSI == NULL)
1898 ERR_EXIT("vkGetDeviceProcAddr failed to find vkDestroySwapChainWSI",
1899 "vkGetDeviceProcAddr Failure");
1900 demo->fpGetSwapChainInfoWSI = vkGetDeviceProcAddr(demo->device, "vkGetSwapChainInfoWSI");
1901 if (demo->fpGetSwapChainInfoWSI == NULL)
1902 ERR_EXIT("vkGetDeviceProcAddr failed to find vkGetSwapChainInfoWSI",
1903 "vkGetDeviceProcAddr Failure");
1904 demo->fpQueuePresentWSI = vkGetDeviceProcAddr(demo->device, "vkQueuePresentWSI");
1905 if (demo->fpQueuePresentWSI == NULL)
1906 ERR_EXIT("vkGetDeviceProcAddr failed to find vkQueuePresentWSI",
1907 "vkGetDeviceProcAddr Failure");
1908
Tony Barbour72304ef2015-04-16 15:59:00 -06001909 err = vkGetPhysicalDeviceInfo(demo->gpu, VK_PHYSICAL_DEVICE_INFO_TYPE_PROPERTIES,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001910 &data_size, NULL);
1911 assert(!err);
1912
Tony Barbour72304ef2015-04-16 15:59:00 -06001913 demo->gpu_props = (VkPhysicalDeviceProperties *) malloc(data_size);
1914 err = vkGetPhysicalDeviceInfo(demo->gpu, VK_PHYSICAL_DEVICE_INFO_TYPE_PROPERTIES,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001915 &data_size, demo->gpu_props);
1916 assert(!err);
1917
Tony Barbour72304ef2015-04-16 15:59:00 -06001918 err = vkGetPhysicalDeviceInfo(demo->gpu, VK_PHYSICAL_DEVICE_INFO_TYPE_QUEUE_PROPERTIES,
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07001919 &data_size, NULL);
1920 assert(!err);
1921
Tony Barbour72304ef2015-04-16 15:59:00 -06001922 demo->queue_props = (VkPhysicalDeviceQueueProperties *) malloc(data_size);
1923 err = vkGetPhysicalDeviceInfo(demo->gpu, VK_PHYSICAL_DEVICE_INFO_TYPE_QUEUE_PROPERTIES,
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07001924 &data_size, demo->queue_props);
1925 assert(!err);
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001926 queue_count = (uint32_t)(data_size / sizeof(VkPhysicalDeviceQueueProperties));
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07001927 assert(queue_count >= 1);
1928
Mark Lobodzinskida9b1092015-04-16 11:44:05 -05001929 // Graphics queue and MemMgr queue can be separate.
1930 // TODO: Add support for separate queues, including synchronization,
Mark Lobodzinski6baaec72015-05-11 17:21:15 -05001931 // and appropriate tracking for QueueSubmit
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07001932 for (i = 0; i < queue_count; i++) {
Mark Lobodzinski6baaec72015-05-11 17:21:15 -05001933 if (demo->queue_props[i].queueFlags & VK_QUEUE_GRAPHICS_BIT)
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07001934 break;
1935 }
1936 assert(i < queue_count);
1937 demo->graphics_queue_node_index = i;
1938
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001939 err = vkGetDeviceQueue(demo->device, demo->graphics_queue_node_index,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001940 0, &demo->queue);
1941 assert(!err);
Tony Barbour3dddd5d2015-04-29 16:19:20 -06001942
Ian Elliotte4602cd2015-04-21 16:41:02 -06001943
1944 // Get the VkDisplayWSI's associated with this physical device:
1945 VkDisplayWSI display;
1946 err = vkGetPhysicalDeviceInfo(demo->gpu, VK_PHYSICAL_DEVICE_INFO_TYPE_DISPLAY_PROPERTIES_WSI,
1947 &data_size, NULL);
Ian Elliottd414a8f2015-05-15 17:52:29 -06001948 if (err != VK_SUCCESS) {
1949 printf("The Vulkan installable client driver (ICD) does not support "
1950 "querying\nfor the swap-chain image format. Therefore, am "
1951 "hardcoding this\nformat to VK_FORMAT_B8G8R8A8_UNORM.\n");
1952 fflush(stdout);
1953 demo->format = VK_FORMAT_B8G8R8A8_UNORM;
1954 return;
1955 }
Ian Elliotte4602cd2015-04-21 16:41:02 -06001956 demo->display_props = (VkDisplayPropertiesWSI *) malloc(data_size);
1957 err = vkGetPhysicalDeviceInfo(demo->gpu, VK_PHYSICAL_DEVICE_INFO_TYPE_DISPLAY_PROPERTIES_WSI,
1958 &data_size, demo->display_props);
1959 assert(!err);
1960 demo->num_displays = data_size / sizeof(VkDisplayPropertiesWSI);
1961 // For now, simply use the first display (TODO: Enhance this for the
1962 // future):
1963 display = demo->display_props[0].display;
1964
1965 // Get a VkFormat to use with the VkDisplayWSI we are using:
Courtney Goeltzenleuchter8f0767f2015-06-10 17:33:55 -06001966 err = demo->fpGetDisplayInfoWSI(display, VK_DISPLAY_INFO_TYPE_FORMAT_PROPERTIES_WSI,
Ian Elliotte4602cd2015-04-21 16:41:02 -06001967 &data_size, NULL);
1968 VkDisplayFormatPropertiesWSI* display_format_props =
1969 (VkDisplayFormatPropertiesWSI*) malloc(data_size);
Courtney Goeltzenleuchter8f0767f2015-06-10 17:33:55 -06001970 err = demo->fpGetDisplayInfoWSI(display, VK_DISPLAY_INFO_TYPE_FORMAT_PROPERTIES_WSI,
Ian Elliotte4602cd2015-04-21 16:41:02 -06001971 &data_size, display_format_props);
1972 // For now, simply use the first VkFormat (TODO: Enhance this for the
1973 // future):
1974 demo->format = display_format_props[0].swapChainFormat;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001975}
1976
1977static void demo_init_connection(struct demo *demo)
1978{
Ian Elliott639ca472015-04-16 15:23:05 -06001979#ifndef _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001980 const xcb_setup_t *setup;
1981 xcb_screen_iterator_t iter;
1982 int scr;
1983
1984 demo->connection = xcb_connect(NULL, &scr);
Ian Elliott3979e282015-04-03 15:24:55 -06001985 if (demo->connection == NULL) {
1986 printf("Cannot find a compatible Vulkan installable client driver "
1987 "(ICD).\nExiting ...\n");
1988 fflush(stdout);
1989 exit(1);
1990 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001991
1992 setup = xcb_get_setup(demo->connection);
1993 iter = xcb_setup_roots_iterator(setup);
1994 while (scr-- > 0)
1995 xcb_screen_next(&iter);
1996
1997 demo->screen = iter.data;
Ian Elliott639ca472015-04-16 15:23:05 -06001998#endif // _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001999}
2000
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002001static void demo_init(struct demo *demo, int argc, char **argv)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002002{
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002003 vec3 eye = {0.0f, 3.0f, 5.0f};
2004 vec3 origin = {0, 0, 0};
Chia-I Wuae3b55d2015-04-22 14:56:17 +08002005 vec3 up = {0.0f, 1.0f, 0.0};
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002006
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002007 memset(demo, 0, sizeof(*demo));
2008
Piers Daniell735ee532015-02-23 16:23:13 -07002009 for (int i = 1; i < argc; i++) {
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002010 if (strcmp(argv[i], "--use_staging") == 0) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002011 demo->use_staging_buffer = true;
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002012 continue;
Ian Elliott639ca472015-04-16 15:23:05 -06002013 }
Cody Northrop1fedb212015-05-28 11:27:16 -06002014 if (strcmp(argv[i], "--use_glsl") == 0) {
2015 demo->use_glsl = true;
2016 continue;
2017 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002018 if (strcmp(argv[i], "--validate") == 0) {
2019 demo->validate = true;
2020 continue;
2021 }
2022
2023 fprintf(stderr, "Usage:\n %s [--use_staging] [--validate}\n", APP_SHORT_NAME);
Ian Elliott639ca472015-04-16 15:23:05 -06002024 fflush(stderr);
2025 exit(1);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002026 }
2027
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002028 demo_init_connection(demo);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002029 demo_init_vk(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002030
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002031 demo->width = 500;
2032 demo->height = 500;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002033
2034 demo->spin_angle = 0.01f;
2035 demo->spin_increment = 0.01f;
2036 demo->pause = false;
2037
Piers Daniell735ee532015-02-23 16:23:13 -07002038 mat4x4_perspective(demo->projection_matrix, (float)degreesToRadians(45.0f), 1.0f, 0.1f, 100.0f);
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002039 mat4x4_look_at(demo->view_matrix, eye, origin, up);
2040 mat4x4_identity(demo->model_matrix);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002041}
2042
2043static void demo_cleanup(struct demo *demo)
2044{
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05002045 uint32_t i;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002046
Courtney Goeltzenleuchterb7e22702015-04-27 14:56:34 -06002047 demo->prepared = false;
2048
Mike Stroyanebae8322015-04-17 12:36:38 -06002049 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DESCRIPTOR_SET, demo->desc_set);
2050 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DESCRIPTOR_POOL, demo->desc_pool);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08002051
Mike Stroyanebae8322015-04-17 12:36:38 -06002052 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DYNAMIC_VP_STATE, demo->viewport);
2053 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DYNAMIC_RS_STATE, demo->raster);
2054 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DYNAMIC_CB_STATE, demo->color_blend);
2055 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DYNAMIC_DS_STATE, demo->depth_stencil);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002056
Mike Stroyanebae8322015-04-17 12:36:38 -06002057 vkDestroyObject(demo->device, VK_OBJECT_TYPE_PIPELINE, demo->pipeline);
2058 vkDestroyObject(demo->device, VK_OBJECT_TYPE_PIPELINE_LAYOUT, demo->pipeline_layout);
2059 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DESCRIPTOR_SET_LAYOUT, demo->desc_layout);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002060
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002061 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Mike Stroyanebae8322015-04-17 12:36:38 -06002062 vkDestroyObject(demo->device, VK_OBJECT_TYPE_IMAGE_VIEW, demo->textures[i].view);
Mike Stroyanebae8322015-04-17 12:36:38 -06002063 vkDestroyObject(demo->device, VK_OBJECT_TYPE_IMAGE, demo->textures[i].image);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05002064 vkFreeMemory(demo->device, demo->textures[i].mem);
Mike Stroyanebae8322015-04-17 12:36:38 -06002065 vkDestroyObject(demo->device, VK_OBJECT_TYPE_SAMPLER, demo->textures[i].sampler);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002066 }
Jon Ashburn0b85d052015-05-21 18:13:33 -06002067 demo->fpDestroySwapChainWSI(demo->swap_chain);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002068
Mike Stroyanebae8322015-04-17 12:36:38 -06002069 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DEPTH_STENCIL_VIEW, demo->depth.view);
Mike Stroyanebae8322015-04-17 12:36:38 -06002070 vkDestroyObject(demo->device, VK_OBJECT_TYPE_IMAGE, demo->depth.image);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05002071 vkFreeMemory(demo->device, demo->depth.mem);
Mark Lobodzinski7b8fee62015-02-18 16:38:17 -06002072
Mike Stroyanebae8322015-04-17 12:36:38 -06002073 vkDestroyObject(demo->device, VK_OBJECT_TYPE_BUFFER_VIEW, demo->uniform_data.view);
Mike Stroyanebae8322015-04-17 12:36:38 -06002074 vkDestroyObject(demo->device, VK_OBJECT_TYPE_BUFFER, demo->uniform_data.buf);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05002075 vkFreeMemory(demo->device, demo->uniform_data.mem);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002076
2077 for (i = 0; i < DEMO_BUFFER_COUNT; i++) {
Mike Stroyanebae8322015-04-17 12:36:38 -06002078 vkDestroyObject(demo->device, VK_OBJECT_TYPE_COLOR_ATTACHMENT_VIEW, demo->buffers[i].view);
2079 vkDestroyObject(demo->device, VK_OBJECT_TYPE_COMMAND_BUFFER, demo->buffers[i].cmd);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002080 }
2081
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002082 vkDestroyDevice(demo->device);
2083 vkDestroyInstance(demo->inst);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002084
Ian Elliott639ca472015-04-16 15:23:05 -06002085#ifndef _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002086 xcb_destroy_window(demo->connection, demo->window);
2087 xcb_disconnect(demo->connection);
Ian Elliott639ca472015-04-16 15:23:05 -06002088#endif // _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002089}
2090
Ian Elliott639ca472015-04-16 15:23:05 -06002091#ifdef _WIN32
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002092extern int __getmainargs(
2093 int * _Argc,
2094 char *** _Argv,
2095 char *** _Env,
2096 int _DoWildCard,
2097 int * new_mode);
Ian Elliottf9cf78c2015-04-28 10:33:11 -06002098
Ian Elliotta748eaf2015-04-28 15:50:36 -06002099int WINAPI WinMain(HINSTANCE hInstance,
2100 HINSTANCE hPrevInstance,
2101 LPSTR pCmdLine,
2102 int nCmdShow)
Ian Elliott639ca472015-04-16 15:23:05 -06002103{
2104 MSG msg; // message
2105 bool done; // flag saying when app is complete
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002106 int argc;
2107 char** argv;
2108 char** env;
2109 int new_mode = 0;
Ian Elliott639ca472015-04-16 15:23:05 -06002110
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002111 __getmainargs(&argc,&argv,&env,0,&new_mode);
2112
2113 demo_init(&demo, argc, argv);
2114 demo.connection = hInstance;
2115 strncpy(demo.name, "cube", APP_NAME_STR_LEN);
Ian Elliott639ca472015-04-16 15:23:05 -06002116 demo_create_window(&demo);
2117
2118 demo_prepare(&demo);
2119
2120 done = false; //initialize loop condition variable
2121 /* main message loop*/
2122 while(!done)
2123 {
Ian Elliotta748eaf2015-04-28 15:50:36 -06002124 PeekMessage(&msg, NULL, 0, 0, PM_REMOVE);
Ian Elliott639ca472015-04-16 15:23:05 -06002125 if (msg.message == WM_QUIT) //check for a quit message
2126 {
2127 done = true; //if found, quit app
2128 }
2129 else
2130 {
2131 /* Translate and dispatch to event queue*/
2132 TranslateMessage(&msg);
2133 DispatchMessage(&msg);
2134 }
2135 }
2136
2137 demo_cleanup(&demo);
2138
Tony Barbourf9921732015-04-22 11:36:22 -06002139 return (int) msg.wParam;
Ian Elliott639ca472015-04-16 15:23:05 -06002140}
2141#else // _WIN32
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002142int main(int argc, char **argv)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002143{
2144 struct demo demo;
2145
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002146 demo_init(&demo, argc, argv);
Chia-I Wucbb564e2015-04-16 22:02:10 +08002147 demo_create_window(&demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002148
2149 demo_prepare(&demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002150 demo_run(&demo);
2151
2152 demo_cleanup(&demo);
2153
2154 return 0;
2155}
Ian Elliott639ca472015-04-16 15:23:05 -06002156#endif // _WIN32