blob: 8ddeb19ffe1b7874f42293f0fa38e0213a4b4b0e [file] [log] [blame]
Ian Elliotta748eaf2015-04-28 15:50:36 -06001/*
2 * Vulkan
3 *
4 * Copyright (C) 2014-2015 LunarG, Inc.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 */
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -060024#define _GNU_SOURCE
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060025#include <stdio.h>
26#include <stdlib.h>
27#include <string.h>
28#include <stdbool.h>
29#include <assert.h>
30
Ian Elliott639ca472015-04-16 15:23:05 -060031#ifdef _WIN32
32#pragma comment(linker, "/subsystem:windows")
33#include <windows.h>
34#define APP_NAME_STR_LEN 80
35#else // _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060036#include <xcb/xcb.h>
Ian Elliott639ca472015-04-16 15:23:05 -060037#endif // _WIN32
38
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -060039#include <vulkan.h>
Chia-I Wucbb564e2015-04-16 22:02:10 +080040#include <vk_wsi_lunarg.h>
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -060041#include "vk_debug_report_lunarg.h"
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060042
Cody Northropfe3d8bc2015-03-17 14:54:35 -060043#include "icd-spv.h"
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060044
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -060045#include "linmath.h"
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -060046#include <png.h>
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -060047
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060048#define DEMO_BUFFER_COUNT 2
49#define DEMO_TEXTURE_COUNT 1
Ian Elliott44e33f72015-04-28 10:52:52 -060050#define APP_SHORT_NAME "cube"
51#define APP_LONG_NAME "The Vulkan Cube Demo Program"
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060052
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
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -0600239void dbgFunc(
240 VkFlags msgFlags,
241 VkObjectType objType,
242 VkObject srcObject,
243 size_t location,
244 int32_t msgCode,
245 const char* pLayerPrefix,
246 const char* pMsg,
247 void* pUserData)
Tony Barbour3dddd5d2015-04-29 16:19:20 -0600248{
249 char *message = (char *) malloc(strlen(pMsg)+100);
250
251 assert (message);
252
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -0600253 if (msgFlags & VK_DBG_REPORT_ERROR_BIT) {
254 sprintf(message,"ERROR: [%s] Code %d : %s", pLayerPrefix, msgCode, pMsg);
255 } else if (msgFlags & VK_DBG_REPORT_WARN_BIT) {
256 sprintf(message,"WARNING: [%s] Code %d : %s", pLayerPrefix, msgCode, pMsg);
Tony Barbour3dddd5d2015-04-29 16:19:20 -0600257 } else {
258 return;
259 }
260
261#ifdef _WIN32
262 MessageBox(NULL, message, "Alert", MB_OK);
263#else
264 printf("%s\n",message);
265 fflush(stdout);
266#endif
267 free(message);
268}
269
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600270struct demo {
Ian Elliott639ca472015-04-16 15:23:05 -0600271#ifdef _WIN32
272#define APP_NAME_STR_LEN 80
273 HINSTANCE connection; // hInstance - Windows Instance
274 char name[APP_NAME_STR_LEN]; // Name to put on the window/icon
275 HWND window; // hWnd - window handle
276#else // _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600277 xcb_connection_t *connection;
278 xcb_screen_t *screen;
Chia-I Wucbb564e2015-04-16 22:02:10 +0800279 xcb_window_t window;
280 xcb_intern_atom_reply_t *atom_wm_delete_window;
Ian Elliott639ca472015-04-16 15:23:05 -0600281#endif // _WIN32
Cody Northrop1fedb212015-05-28 11:27:16 -0600282 bool prepared;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700283 bool use_staging_buffer;
Cody Northrop1fedb212015-05-28 11:27:16 -0600284 bool use_glsl;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600285
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600286 VkInstance inst;
Tony Barbour72304ef2015-04-16 15:59:00 -0600287 VkPhysicalDevice gpu;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600288 VkDevice device;
289 VkQueue queue;
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -0700290 uint32_t graphics_queue_node_index;
Tony Barbour72304ef2015-04-16 15:59:00 -0600291 VkPhysicalDeviceProperties *gpu_props;
292 VkPhysicalDeviceQueueProperties *queue_props;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600293
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600294 VkFramebuffer framebuffer;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600295 int width, height;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600296 VkFormat format;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600297
Jon Ashburn0b85d052015-05-21 18:13:33 -0600298 PFN_vkCreateSwapChainWSI fpCreateSwapChainWSI;
299 PFN_vkDestroySwapChainWSI fpDestroySwapChainWSI;
300 PFN_vkGetSwapChainInfoWSI fpGetSwapChainInfoWSI;
301 PFN_vkQueuePresentWSI fpQueuePresentWSI;
Chia-I Wucbb564e2015-04-16 22:02:10 +0800302 VkSwapChainWSI swap_chain;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600303 struct {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600304 VkImage image;
Tony Barbour72304ef2015-04-16 15:59:00 -0600305 VkDeviceMemory mem;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600306 VkCmdBuffer cmd;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600307
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600308 VkColorAttachmentView view;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600309 } buffers[DEMO_BUFFER_COUNT];
310
311 struct {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600312 VkFormat format;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600313
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600314 VkImage image;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500315 VkDeviceMemory mem;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600316 VkDepthStencilView view;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600317 } depth;
318
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600319 struct texture_object textures[DEMO_TEXTURE_COUNT];
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600320
321 struct {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600322 VkBuffer buf;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500323 VkDeviceMemory mem;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600324 VkBufferView view;
Chia-I Wuae721ba2015-05-25 16:27:55 +0800325 VkDescriptorInfo desc;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600326 } uniform_data;
327
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600328 VkCmdBuffer cmd; // Buffer for initialization commands
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -0500329 VkPipelineLayout pipeline_layout;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600330 VkDescriptorSetLayout desc_layout;
331 VkPipeline pipeline;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600332
Courtney Goeltzenleuchter5e6d1e92015-04-10 16:24:50 -0600333 VkDynamicVpState viewport;
334 VkDynamicRsState raster;
335 VkDynamicCbState color_blend;
336 VkDynamicDsState depth_stencil;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600337
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600338 mat4x4 projection_matrix;
339 mat4x4 view_matrix;
340 mat4x4 model_matrix;
341
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600342 float spin_angle;
343 float spin_increment;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600344 bool pause;
345
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600346 VkDescriptorPool desc_pool;
347 VkDescriptorSet desc_set;
Chia-I Wu6a3c8972015-01-04 16:27:24 +0800348
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600349 bool quit;
Tony Barbour3dddd5d2015-04-29 16:19:20 -0600350 bool validate;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -0600351 PFN_vkDbgCreateMsgCallback dbgCreateMsgCallback;
352 VkDbgMsgCallback msg_callback;
353
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600354 uint32_t current_buffer;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600355};
356
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600357static void demo_flush_init_cmd(struct demo *demo)
358{
Tony Barbourfdc2d352015-04-22 09:02:32 -0600359 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600360
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600361 if (demo->cmd == VK_NULL_HANDLE)
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600362 return;
363
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600364 err = vkEndCommandBuffer(demo->cmd);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600365 assert(!err);
366
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600367 const VkCmdBuffer cmd_bufs[] = { demo->cmd };
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600368
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600369 err = vkQueueSubmit(demo->queue, 1, cmd_bufs, VK_NULL_HANDLE);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600370 assert(!err);
371
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600372 err = vkQueueWaitIdle(demo->queue);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600373 assert(!err);
374
Mike Stroyanebae8322015-04-17 12:36:38 -0600375 vkDestroyObject(demo->device, VK_OBJECT_TYPE_COMMAND_BUFFER, demo->cmd);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600376 demo->cmd = VK_NULL_HANDLE;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600377}
378
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600379static void demo_set_image_layout(
380 struct demo *demo,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600381 VkImage image,
malnasseeb25d3f2015-06-03 17:28:38 -0400382 VkImageAspect aspect,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600383 VkImageLayout old_image_layout,
384 VkImageLayout new_image_layout)
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600385{
Tony Barbourfdc2d352015-04-22 09:02:32 -0600386 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600387
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600388 if (demo->cmd == VK_NULL_HANDLE) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600389 const VkCmdBufferCreateInfo cmd = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600390 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600391 .pNext = NULL,
392 .queueNodeIndex = demo->graphics_queue_node_index,
393 .flags = 0,
394 };
395
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600396 err = vkCreateCommandBuffer(demo->device, &cmd, &demo->cmd);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600397 assert(!err);
398
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600399 VkCmdBufferBeginInfo cmd_buf_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600400 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600401 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -0600402 .flags = VK_CMD_BUFFER_OPTIMIZE_SMALL_BATCH_BIT |
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600403 VK_CMD_BUFFER_OPTIMIZE_ONE_TIME_SUBMIT_BIT,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600404 };
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600405 err = vkBeginCommandBuffer(demo->cmd, &cmd_buf_info);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600406 }
407
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600408 VkImageMemoryBarrier image_memory_barrier = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600409 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600410 .pNext = NULL,
411 .outputMask = 0,
412 .inputMask = 0,
413 .oldLayout = old_image_layout,
414 .newLayout = new_image_layout,
415 .image = image,
malnasseeb25d3f2015-06-03 17:28:38 -0400416 .subresourceRange = { aspect, 0, 1, 0, 0 }
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600417 };
418
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600419 if (new_image_layout == VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL) {
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600420 /* Make sure anything that was copying from this image has completed */
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -0600421 image_memory_barrier.inputMask = VK_MEMORY_INPUT_TRANSFER_BIT;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600422 }
423
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600424 if (new_image_layout == VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL) {
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600425 /* Make sure any Copy or CPU writes to image are flushed */
Courtney Goeltzenleuchter2bda8332015-04-29 17:16:21 -0600426 image_memory_barrier.outputMask = VK_MEMORY_OUTPUT_HOST_WRITE_BIT | VK_MEMORY_OUTPUT_TRANSFER_BIT;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600427 }
428
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600429 VkImageMemoryBarrier *pmemory_barrier = &image_memory_barrier;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600430
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600431 VkPipeEvent set_events[] = { VK_PIPE_EVENT_TOP_OF_PIPE };
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600432
Tony Barbour72304ef2015-04-16 15:59:00 -0600433 vkCmdPipelineBarrier(demo->cmd, VK_WAIT_EVENT_TOP_OF_PIPE, 1, set_events, 1, (const void **)&pmemory_barrier);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600434}
435
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600436static void demo_draw_build_cmd(struct demo *demo, VkCmdBuffer cmd_buf)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600437{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600438 const VkColorAttachmentBindInfo color_attachment = {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600439 .view = demo->buffers[demo->current_buffer].view,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600440 .layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600441 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600442 const VkDepthStencilBindInfo depth_stencil = {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600443 .view = demo->depth.view,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600444 .layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600445 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600446 const VkClearColor clear_color = {
Courtney Goeltzenleuchter374553c2015-04-03 16:35:32 -0600447 .color.floatColor = { 0.2f, 0.2f, 0.2f, 0.2f },
448 .useRawValue = false,
449 };
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600450 const float clear_depth = 1.0f;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600451 VkImageSubresourceRange clear_range;
452 VkCmdBufferBeginInfo cmd_buf_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600453 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO,
Courtney Goeltzenleuchter53968d82015-04-03 15:25:24 -0600454 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -0600455 .flags = VK_CMD_BUFFER_OPTIMIZE_SMALL_BATCH_BIT |
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600456 VK_CMD_BUFFER_OPTIMIZE_ONE_TIME_SUBMIT_BIT,
Jon Ashburn60ccfbe2014-12-31 17:08:35 -0700457 };
Tony Barbourfdc2d352015-04-22 09:02:32 -0600458 VkResult U_ASSERT_ONLY err;
Chris Forbes40a71562015-06-17 11:36:12 +1200459 VkAttachmentLoadOp load_op = VK_ATTACHMENT_LOAD_OP_CLEAR;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600460 VkAttachmentStoreOp store_op = VK_ATTACHMENT_STORE_OP_DONT_CARE;
461 const VkFramebufferCreateInfo fb_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600462 .sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO,
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700463 .pNext = NULL,
464 .colorAttachmentCount = 1,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600465 .pColorAttachments = (VkColorAttachmentBindInfo*) &color_attachment,
466 .pDepthStencilAttachment = (VkDepthStencilBindInfo*) &depth_stencil,
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700467 .sampleCount = 1,
Mark Lobodzinskic06b7412015-01-27 13:24:03 -0600468 .width = demo->width,
469 .height = demo->height,
470 .layers = 1,
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700471 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600472 VkRenderPassCreateInfo rp_info;
473 VkRenderPassBegin rp_begin;
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700474
475 memset(&rp_info, 0 , sizeof(rp_info));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600476 err = vkCreateFramebuffer(demo->device, &fb_info, &rp_begin.framebuffer);
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700477 assert(!err);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600478 rp_info.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO;
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700479 rp_info.renderArea.extent.width = demo->width;
480 rp_info.renderArea.extent.height = demo->height;
Courtney Goeltzenleuchter53968d82015-04-03 15:25:24 -0600481 rp_info.colorAttachmentCount = fb_info.colorAttachmentCount;
482 rp_info.pColorFormats = &demo->format;
483 rp_info.pColorLayouts = &color_attachment.layout;
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700484 rp_info.pColorLoadOps = &load_op;
485 rp_info.pColorStoreOps = &store_op;
Courtney Goeltzenleuchter53968d82015-04-03 15:25:24 -0600486 rp_info.pColorLoadClearValues = &clear_color;
Tony Barbour72304ef2015-04-16 15:59:00 -0600487 rp_info.depthStencilFormat = VK_FORMAT_D16_UNORM;
Courtney Goeltzenleuchter53968d82015-04-03 15:25:24 -0600488 rp_info.depthStencilLayout = depth_stencil.layout;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600489 rp_info.depthLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
Courtney Goeltzenleuchter53968d82015-04-03 15:25:24 -0600490 rp_info.depthLoadClearValue = clear_depth;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600491 rp_info.depthStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
492 rp_info.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
Courtney Goeltzenleuchter53968d82015-04-03 15:25:24 -0600493 rp_info.stencilLoadClearValue = 0;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600494 rp_info.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
495 err = vkCreateRenderPass(demo->device, &rp_info, &rp_begin.renderPass);
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700496 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600497
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600498 err = vkBeginCommandBuffer(cmd_buf, &cmd_buf_info);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600499 assert(!err);
500
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600501 vkCmdBindPipeline(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600502 demo->pipeline);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600503 vkCmdBindDescriptorSets(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS,
Cody Northropfb5185a2015-04-16 13:41:56 -0600504 0, 1, &demo->desc_set, 0, NULL);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600505
Tony Barbour72304ef2015-04-16 15:59:00 -0600506 vkCmdBindDynamicStateObject(cmd_buf, VK_STATE_BIND_POINT_VIEWPORT, demo->viewport);
507 vkCmdBindDynamicStateObject(cmd_buf, VK_STATE_BIND_POINT_RASTER, demo->raster);
508 vkCmdBindDynamicStateObject(cmd_buf, VK_STATE_BIND_POINT_COLOR_BLEND,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600509 demo->color_blend);
Tony Barbour72304ef2015-04-16 15:59:00 -0600510 vkCmdBindDynamicStateObject(cmd_buf, VK_STATE_BIND_POINT_DEPTH_STENCIL,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600511 demo->depth_stencil);
512
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600513 vkCmdBeginRenderPass(cmd_buf, &rp_begin);
Chris Forbes40a71562015-06-17 11:36:12 +1200514 clear_range.aspect = VK_IMAGE_ASPECT_DEPTH;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600515 clear_range.baseMipLevel = 0;
516 clear_range.mipLevels = 1;
517 clear_range.baseArraySlice = 0;
518 clear_range.arraySize = 1;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600519
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600520 vkCmdClearDepthStencil(cmd_buf, demo->depth.image,
521 VK_IMAGE_LAYOUT_CLEAR_OPTIMAL,
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600522 clear_depth, 0, 1, &clear_range);
523
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600524 vkCmdDraw(cmd_buf, 0, 12 * 3, 0, 1);
525 vkCmdEndRenderPass(cmd_buf, rp_begin.renderPass);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600526
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600527 err = vkEndCommandBuffer(cmd_buf);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600528 assert(!err);
Courtney Goeltzenleuchter94901dc2015-02-25 17:53:18 -0700529
Mike Stroyanebae8322015-04-17 12:36:38 -0600530 vkDestroyObject(demo->device, VK_OBJECT_TYPE_RENDER_PASS, rp_begin.renderPass);
531 vkDestroyObject(demo->device, VK_OBJECT_TYPE_FRAMEBUFFER, rp_begin.framebuffer);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600532}
533
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600534
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600535void demo_update_data_buffer(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600536{
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600537 mat4x4 MVP, Model, VP;
538 int matrixSize = sizeof(MVP);
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600539 uint8_t *pData;
Tony Barbourfdc2d352015-04-22 09:02:32 -0600540 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600541
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600542 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600543
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600544 // Rotate 22.5 degrees around the Y axis
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600545 mat4x4_dup(Model, demo->model_matrix);
Piers Daniell735ee532015-02-23 16:23:13 -0700546 mat4x4_rotate(demo->model_matrix, Model, 0.0f, 1.0f, 0.0f, (float)degreesToRadians(demo->spin_angle));
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600547 mat4x4_mul(MVP, VP, demo->model_matrix);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600548
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500549 err = vkMapMemory(demo->device, demo->uniform_data.mem, 0, 0, 0, (void **) &pData);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600550 assert(!err);
551
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600552 memcpy(pData, (const void*) &MVP[0][0], matrixSize);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600553
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500554 err = vkUnmapMemory(demo->device, demo->uniform_data.mem);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600555 assert(!err);
556}
557
558static void demo_draw(struct demo *demo)
559{
Chia-I Wucbb564e2015-04-16 22:02:10 +0800560 const VkPresentInfoWSI present = {
561 .sType = VK_STRUCTURE_TYPE_PRESENT_INFO_WSI,
562 .pNext = NULL,
563 .image = demo->buffers[demo->current_buffer].image,
564 .flipInterval = 0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600565 };
Tony Barbourfdc2d352015-04-22 09:02:32 -0600566 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600567
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600568 err = vkQueueSubmit(demo->queue, 1, &demo->buffers[demo->current_buffer].cmd,
569 VK_NULL_HANDLE);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600570 assert(!err);
571
Jon Ashburn0b85d052015-05-21 18:13:33 -0600572 err = demo->fpQueuePresentWSI(demo->queue, &present);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600573 assert(!err);
574
575 demo->current_buffer = (demo->current_buffer + 1) % DEMO_BUFFER_COUNT;
Chia-I Wucbb564e2015-04-16 22:02:10 +0800576
577 err = vkQueueWaitIdle(demo->queue);
578 assert(err == VK_SUCCESS);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600579}
580
581static void demo_prepare_buffers(struct demo *demo)
582{
Chia-I Wucbb564e2015-04-16 22:02:10 +0800583 const VkSwapChainCreateInfoWSI swap_chain = {
584 .sType = VK_STRUCTURE_TYPE_SWAP_CHAIN_CREATE_INFO_WSI,
585 .pNext = NULL,
586 .pNativeWindowSystemHandle = demo->connection,
587 .pNativeWindowHandle = (void *) (intptr_t) demo->window,
Ian Elliotte4602cd2015-04-21 16:41:02 -0600588 .displayCount = 1,
Chia-I Wucbb564e2015-04-16 22:02:10 +0800589 .imageCount = DEMO_BUFFER_COUNT,
590 .imageFormat = demo->format,
591 .imageExtent = {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600592 .width = demo->width,
593 .height = demo->height,
594 },
Chia-I Wucbb564e2015-04-16 22:02:10 +0800595 .imageArraySize = 1,
596 .imageUsageFlags = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600597 };
Chia-I Wucbb564e2015-04-16 22:02:10 +0800598 VkSwapChainImageInfoWSI images[DEMO_BUFFER_COUNT];
599 size_t images_size = sizeof(images);
Tony Barbourfdc2d352015-04-22 09:02:32 -0600600 VkResult U_ASSERT_ONLY err;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600601 uint32_t i;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600602
Jon Ashburn0b85d052015-05-21 18:13:33 -0600603 err = demo->fpCreateSwapChainWSI(demo->device, &swap_chain, &demo->swap_chain);
Chia-I Wucbb564e2015-04-16 22:02:10 +0800604 assert(!err);
605
Jon Ashburn0b85d052015-05-21 18:13:33 -0600606 err = demo->fpGetSwapChainInfoWSI(demo->swap_chain,
Chia-I Wucbb564e2015-04-16 22:02:10 +0800607 VK_SWAP_CHAIN_INFO_TYPE_PERSISTENT_IMAGES_WSI,
608 &images_size, images);
609 assert(!err && images_size == sizeof(images));
610
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600611 for (i = 0; i < DEMO_BUFFER_COUNT; i++) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600612 VkColorAttachmentViewCreateInfo color_attachment_view = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600613 .sType = VK_STRUCTURE_TYPE_COLOR_ATTACHMENT_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600614 .pNext = NULL,
615 .format = demo->format,
616 .mipLevel = 0,
617 .baseArraySlice = 0,
618 .arraySize = 1,
619 };
620
Chia-I Wucbb564e2015-04-16 22:02:10 +0800621 demo->buffers[i].image = images[i].image;
622 demo->buffers[i].mem = images[i].memory;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600623
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600624 demo_set_image_layout(demo, demo->buffers[i].image,
malnasseeb25d3f2015-06-03 17:28:38 -0400625 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600626 VK_IMAGE_LAYOUT_UNDEFINED,
627 VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600628
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600629 color_attachment_view.image = demo->buffers[i].image;
630
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600631 err = vkCreateColorAttachmentView(demo->device,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600632 &color_attachment_view, &demo->buffers[i].view);
633 assert(!err);
634 }
635}
636
637static void demo_prepare_depth(struct demo *demo)
638{
Tony Barbour72304ef2015-04-16 15:59:00 -0600639 const VkFormat depth_format = VK_FORMAT_D16_UNORM;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600640 const VkImageCreateInfo image = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600641 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600642 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -0600643 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600644 .format = depth_format,
645 .extent = { demo->width, demo->height, 1 },
646 .mipLevels = 1,
647 .arraySize = 1,
648 .samples = 1,
Tony Barbour72304ef2015-04-16 15:59:00 -0600649 .tiling = VK_IMAGE_TILING_OPTIMAL,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600650 .usage = VK_IMAGE_USAGE_DEPTH_STENCIL_BIT,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600651 .flags = 0,
652 };
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -0600653 VkMemoryAllocInfo mem_alloc = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600654 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
Mark Lobodzinskia6907f72015-04-16 08:52:00 -0500655 .pNext = NULL,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600656 .allocationSize = 0,
Tony Barbour72304ef2015-04-16 15:59:00 -0600657 .memProps = VK_MEMORY_PROPERTY_DEVICE_ONLY,
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 Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700901 };
902
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500903 VkMemoryRequirements mem_reqs;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600904 size_t mem_reqs_size = sizeof(VkMemoryRequirements);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700905
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600906 err = vkCreateImage(demo->device, &image_create_info,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600907 &tex_obj->image);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700908 assert(!err);
909
Mike Stroyanebae8322015-04-17 12:36:38 -0600910 err = vkGetObjectInfo(demo->device,
911 VK_OBJECT_TYPE_IMAGE, tex_obj->image,
Tony Barbour72304ef2015-04-16 15:59:00 -0600912 VK_OBJECT_INFO_TYPE_MEMORY_REQUIREMENTS,
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500913 &mem_reqs_size, &mem_reqs);
914 assert(!err && mem_reqs_size == sizeof(VkMemoryRequirements));
Piers Daniell735ee532015-02-23 16:23:13 -0700915
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500916 mem_alloc.allocationSize = mem_reqs.size;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700917
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500918 /* allocate memory */
919 err = vkAllocMemory(demo->device, &mem_alloc,
920 &(tex_obj->mem));
921 assert(!err);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700922
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500923 /* bind memory */
924 err = vkBindObjectMemory(demo->device,
925 VK_OBJECT_TYPE_IMAGE, tex_obj->image,
926 tex_obj->mem, 0);
927 assert(!err);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700928
Tony Barbour72304ef2015-04-16 15:59:00 -0600929 if (mem_props & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600930 const VkImageSubresource subres = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600931 .aspect = VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700932 .mipLevel = 0,
933 .arraySlice = 0,
934 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600935 VkSubresourceLayout layout;
936 size_t layout_size = sizeof(VkSubresourceLayout);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700937 void *data;
938
Mike Stroyanebae8322015-04-17 12:36:38 -0600939 err = vkGetImageSubresourceInfo(demo->device, tex_obj->image, &subres,
Tony Barbour72304ef2015-04-16 15:59:00 -0600940 VK_SUBRESOURCE_INFO_TYPE_LAYOUT,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700941 &layout_size, &layout);
942 assert(!err && layout_size == sizeof(layout));
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700943
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500944 err = vkMapMemory(demo->device, tex_obj->mem, 0, 0, 0, &data);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700945 assert(!err);
946
947 if (!loadTexture(filename, data, &layout, &tex_width, &tex_height)) {
948 fprintf(stderr, "Error loading texture: %s\n", filename);
949 }
950
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500951 err = vkUnmapMemory(demo->device, tex_obj->mem);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700952 assert(!err);
953 }
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600954
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600955 tex_obj->imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600956 demo_set_image_layout(demo, tex_obj->image,
malnasseeb25d3f2015-06-03 17:28:38 -0400957 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600958 VK_IMAGE_LAYOUT_UNDEFINED,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600959 tex_obj->imageLayout);
960 /* 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 -0700961}
962
Mark Lobodzinskida9b1092015-04-16 11:44:05 -0500963static void demo_destroy_texture_image(struct demo *demo, struct texture_object *tex_objs)
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700964{
965 /* clean up staging resources */
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500966 vkFreeMemory(demo->device, tex_objs->mem);
Mike Stroyanebae8322015-04-17 12:36:38 -0600967 vkDestroyObject(demo->device, VK_OBJECT_TYPE_IMAGE, tex_objs->image);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700968}
969
970static void demo_prepare_textures(struct demo *demo)
971{
Tony Barbour72304ef2015-04-16 15:59:00 -0600972 const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600973 VkFormatProperties props;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700974 size_t size = sizeof(props);
Tony Barbourfdc2d352015-04-22 09:02:32 -0600975 VkResult U_ASSERT_ONLY err;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600976 uint32_t i;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600977
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600978 err = vkGetFormatInfo(demo->device, tex_format,
Tony Barbour72304ef2015-04-16 15:59:00 -0600979 VK_FORMAT_INFO_TYPE_PROPERTIES,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700980 &size, &props);
981 assert(!err);
982
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600983 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700984
FslNopper6a7e50e2015-05-06 21:42:01 +0200985 if ((props.linearTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) && !demo->use_staging_buffer) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700986 /* Device can texture using linear textures */
987 demo_prepare_texture_image(demo, tex_files[i], &demo->textures[i],
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -0600988 VK_IMAGE_TILING_LINEAR, VK_IMAGE_USAGE_SAMPLED_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
Tony Barbour72304ef2015-04-16 15:59:00 -0600989 } else if (props.optimalTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700990 /* Must use staging buffer to copy linear texture to optimized */
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600991 struct texture_object staging_texture;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700992
993 memset(&staging_texture, 0, sizeof(staging_texture));
994 demo_prepare_texture_image(demo, tex_files[i], &staging_texture,
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -0600995 VK_IMAGE_TILING_LINEAR, VK_IMAGE_USAGE_TRANSFER_SOURCE_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700996
997 demo_prepare_texture_image(demo, tex_files[i], &demo->textures[i],
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -0600998 VK_IMAGE_TILING_OPTIMAL,
999 (VK_IMAGE_USAGE_TRANSFER_DESTINATION_BIT | VK_IMAGE_USAGE_SAMPLED_BIT),
1000 VK_MEMORY_PROPERTY_DEVICE_ONLY);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001001
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001002 demo_set_image_layout(demo, staging_texture.image,
malnasseeb25d3f2015-06-03 17:28:38 -04001003 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001004 staging_texture.imageLayout,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001005 VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001006
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001007 demo_set_image_layout(demo, demo->textures[i].image,
malnasseeb25d3f2015-06-03 17:28:38 -04001008 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001009 demo->textures[i].imageLayout,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001010 VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001011
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001012 VkImageCopy copy_region = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001013 .srcSubresource = { VK_IMAGE_ASPECT_COLOR, 0, 0 },
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001014 .srcOffset = { 0, 0, 0 },
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001015 .destSubresource = { VK_IMAGE_ASPECT_COLOR, 0, 0 },
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001016 .destOffset = { 0, 0, 0 },
1017 .extent = { staging_texture.tex_width, staging_texture.tex_height, 1 },
1018 };
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001019 vkCmdCopyImage(demo->cmd,
1020 staging_texture.image, VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL,
1021 demo->textures[i].image, VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL,
Courtney Goeltzenleuchter8e89a312015-03-25 11:25:10 -06001022 1, &copy_region);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001023
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001024 demo_set_image_layout(demo, demo->textures[i].image,
malnasseeb25d3f2015-06-03 17:28:38 -04001025 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001026 VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001027 demo->textures[i].imageLayout);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001028
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001029 demo_flush_init_cmd(demo);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001030
Courtney Goeltzenleuchterf3aeb2b2015-04-21 09:30:03 -06001031 demo_destroy_texture_image(demo, &staging_texture);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001032 } else {
Mike Stroyan8b89e072015-06-15 14:21:03 -06001033 /* Can't support VK_FORMAT_R8G8B8A8_UNORM !? */
1034 assert(!"No support for R8G8B8A8_UNORM as texture image format");
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001035 }
1036
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001037 const VkSamplerCreateInfo sampler = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001038 .sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001039 .pNext = NULL,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001040 .magFilter = VK_TEX_FILTER_NEAREST,
1041 .minFilter = VK_TEX_FILTER_NEAREST,
Tony Barbour72304ef2015-04-16 15:59:00 -06001042 .mipMode = VK_TEX_MIPMAP_MODE_BASE,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001043 .addressU = VK_TEX_ADDRESS_CLAMP,
1044 .addressV = VK_TEX_ADDRESS_CLAMP,
1045 .addressW = VK_TEX_ADDRESS_CLAMP,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001046 .mipLodBias = 0.0f,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001047 .maxAnisotropy = 1,
Tony Barbour72304ef2015-04-16 15:59:00 -06001048 .compareOp = VK_COMPARE_OP_NEVER,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001049 .minLod = 0.0f,
1050 .maxLod = 0.0f,
Tony Barbour72304ef2015-04-16 15:59:00 -06001051 .borderColor = VK_BORDER_COLOR_OPAQUE_WHITE,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001052 };
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001053
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001054 VkImageViewCreateInfo view = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001055 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001056 .pNext = NULL,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001057 .image = VK_NULL_HANDLE,
Tony Barbour72304ef2015-04-16 15:59:00 -06001058 .viewType = VK_IMAGE_VIEW_TYPE_2D,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001059 .format = tex_format,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001060 .channels = { VK_CHANNEL_SWIZZLE_R,
1061 VK_CHANNEL_SWIZZLE_G,
1062 VK_CHANNEL_SWIZZLE_B,
1063 VK_CHANNEL_SWIZZLE_A, },
1064 .subresourceRange = { VK_IMAGE_ASPECT_COLOR, 0, 1, 0, 1 },
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001065 .minLod = 0.0f,
1066 };
Jon Ashburnb2a66652015-01-16 09:37:43 -07001067
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001068 /* create sampler */
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001069 err = vkCreateSampler(demo->device, &sampler,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001070 &demo->textures[i].sampler);
1071 assert(!err);
1072
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001073 /* create image view */
1074 view.image = demo->textures[i].image;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001075 err = vkCreateImageView(demo->device, &view,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001076 &demo->textures[i].view);
1077 assert(!err);
1078 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001079}
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001080
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001081void demo_prepare_cube_data_buffer(struct demo *demo)
1082{
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -06001083 VkBufferCreateInfo buf_info;
1084 VkBufferViewCreateInfo view_info;
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -06001085 VkMemoryAllocInfo alloc_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001086 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
Mark Lobodzinskia6907f72015-04-16 08:52:00 -05001087 .pNext = NULL,
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001088 .allocationSize = 0,
Tony Barbour72304ef2015-04-16 15:59:00 -06001089 .memProps = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT,
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001090 };
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001091 VkMemoryRequirements mem_reqs;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001092 size_t mem_reqs_size = sizeof(VkMemoryRequirements);
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001093 uint8_t *pData;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001094 int i;
1095 mat4x4 MVP, VP;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001096 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001097 struct vktexcube_vs_uniform data;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001098
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001099 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001100 mat4x4_mul(MVP, VP, demo->model_matrix);
1101 memcpy(data.mvp, MVP, sizeof(MVP));
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001102// dumpMatrix("MVP", MVP);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001103
1104 for (i=0; i<12*3; i++) {
1105 data.position[i][0] = g_vertex_buffer_data[i*3];
1106 data.position[i][1] = g_vertex_buffer_data[i*3+1];
1107 data.position[i][2] = g_vertex_buffer_data[i*3+2];
1108 data.position[i][3] = 1.0f;
1109 data.attr[i][0] = g_uv_buffer_data[2*i];
1110 data.attr[i][1] = g_uv_buffer_data[2*i + 1];
1111 data.attr[i][2] = 0;
1112 data.attr[i][3] = 0;
1113 }
1114
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001115 memset(&buf_info, 0, sizeof(buf_info));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001116 buf_info.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001117 buf_info.size = sizeof(data);
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -06001118 buf_info.usage = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001119 err = vkCreateBuffer(demo->device, &buf_info, &demo->uniform_data.buf);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001120 assert(!err);
1121
Mike Stroyanebae8322015-04-17 12:36:38 -06001122 err = vkGetObjectInfo(demo->device,
Mike Stroyanebae8322015-04-17 12:36:38 -06001123 VK_OBJECT_TYPE_BUFFER, demo->uniform_data.buf,
Tony Barbour72304ef2015-04-16 15:59:00 -06001124 VK_OBJECT_INFO_TYPE_MEMORY_REQUIREMENTS,
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001125 &mem_reqs_size, &mem_reqs);
1126 assert(!err && mem_reqs_size == sizeof(mem_reqs));
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001127
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001128 alloc_info.allocationSize = mem_reqs.size;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001129
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001130 err = vkAllocMemory(demo->device, &alloc_info, &(demo->uniform_data.mem));
1131 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001132
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001133 err = vkMapMemory(demo->device, demo->uniform_data.mem, 0, 0, 0, (void **) &pData);
1134 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001135
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001136 memcpy(pData, &data, sizeof data);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001137
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001138 err = vkUnmapMemory(demo->device, demo->uniform_data.mem);
1139 assert(!err);
1140
1141 err = vkBindObjectMemory(demo->device,
1142 VK_OBJECT_TYPE_BUFFER, demo->uniform_data.buf,
1143 demo->uniform_data.mem, 0);
1144 assert(!err);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001145
1146 memset(&view_info, 0, sizeof(view_info));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001147 view_info.sType = VK_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO;
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001148 view_info.buffer = demo->uniform_data.buf;
Tony Barbour72304ef2015-04-16 15:59:00 -06001149 view_info.viewType = VK_BUFFER_VIEW_TYPE_RAW;
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001150 view_info.offset = 0;
1151 view_info.range = sizeof(data);
1152
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001153 err = vkCreateBufferView(demo->device, &view_info, &demo->uniform_data.view);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001154 assert(!err);
1155
Chia-I Wuae721ba2015-05-25 16:27:55 +08001156 demo->uniform_data.desc.bufferView = demo->uniform_data.view;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001157}
1158
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001159static void demo_prepare_descriptor_layout(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001160{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001161 const VkDescriptorSetLayoutBinding layout_bindings[2] = {
Chia-I Wua2aa8632015-03-26 15:04:41 +08001162 [0] = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001163 .descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
Chia-I Wu6d29a412015-05-25 16:22:52 +08001164 .arraySize = 1,
Tony Barbour72304ef2015-04-16 15:59:00 -06001165 .stageFlags = VK_SHADER_STAGE_VERTEX_BIT,
Chia-I Wu91e8e212015-03-27 12:56:09 +08001166 .pImmutableSamplers = NULL,
Chia-I Wua2aa8632015-03-26 15:04:41 +08001167 },
1168 [1] = {
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -06001169 .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
Chia-I Wu6d29a412015-05-25 16:22:52 +08001170 .arraySize = DEMO_TEXTURE_COUNT,
Tony Barbour72304ef2015-04-16 15:59:00 -06001171 .stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT,
Chia-I Wu91e8e212015-03-27 12:56:09 +08001172 .pImmutableSamplers = NULL,
Chia-I Wua2aa8632015-03-26 15:04:41 +08001173 },
1174 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001175 const VkDescriptorSetLayoutCreateInfo descriptor_layout = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001176 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001177 .pNext = NULL,
Chia-I Wua2aa8632015-03-26 15:04:41 +08001178 .count = 2,
1179 .pBinding = layout_bindings,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001180 };
Tony Barbourfdc2d352015-04-22 09:02:32 -06001181 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001182
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001183 err = vkCreateDescriptorSetLayout(demo->device,
Chia-I Wub58c24a2015-03-26 15:27:55 +08001184 &descriptor_layout, &demo->desc_layout);
1185 assert(!err);
1186
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001187 const VkPipelineLayoutCreateInfo pPipelineLayoutCreateInfo = {
1188 .sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
1189 .pNext = NULL,
1190 .descriptorSetCount = 1,
1191 .pSetLayouts = &demo->desc_layout,
1192 };
1193
1194 err = vkCreatePipelineLayout(demo->device,
1195 &pPipelineLayoutCreateInfo,
1196 &demo->pipeline_layout);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001197 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001198}
1199
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001200static VkShader demo_prepare_shader(struct demo *demo,
Tony Barbour72304ef2015-04-16 15:59:00 -06001201 VkShaderStage stage,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001202 const void *code,
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001203 size_t size)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001204{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001205 VkShaderCreateInfo createInfo;
1206 VkShader shader;
1207 VkResult err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001208
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001209
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001210 createInfo.sType = VK_STRUCTURE_TYPE_SHADER_CREATE_INFO;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001211 createInfo.pNext = NULL;
1212
Cody Northrop1fedb212015-05-28 11:27:16 -06001213 if (!demo->use_glsl) {
1214 createInfo.codeSize = size;
1215 createInfo.pCode = code;
1216 createInfo.flags = 0;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001217
Cody Northrop1fedb212015-05-28 11:27:16 -06001218 err = vkCreateShader(demo->device, &createInfo, &shader);
1219 if (err) {
1220 free((void *) createInfo.pCode);
1221 }
1222 } else {
1223 // Create fake SPV structure to feed GLSL
1224 // to the driver "under the covers"
1225 createInfo.codeSize = 3 * sizeof(uint32_t) + size + 1;
1226 createInfo.pCode = malloc(createInfo.codeSize);
1227 createInfo.flags = 0;
1228
1229 /* try version 0 first: VkShaderStage followed by GLSL */
1230 ((uint32_t *) createInfo.pCode)[0] = ICD_SPV_MAGIC;
1231 ((uint32_t *) createInfo.pCode)[1] = 0;
1232 ((uint32_t *) createInfo.pCode)[2] = stage;
1233 memcpy(((uint32_t *) createInfo.pCode + 3), code, size + 1);
1234
1235 err = vkCreateShader(demo->device, &createInfo, &shader);
1236 if (err) {
1237 free((void *) createInfo.pCode);
1238 return (VkShader) VK_NULL_HANDLE;
1239 }
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001240 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001241 return shader;
1242}
1243
Cody Northrop8b12a1f2015-03-17 15:55:58 -06001244char *demo_read_spv(const char *filename, size_t *psize)
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001245{
1246 long int size;
Tony Barbourf9921732015-04-22 11:36:22 -06001247 size_t U_ASSERT_ONLY retval;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001248 void *shader_code;
1249
1250 FILE *fp = fopen(filename, "rb");
1251 if (!fp) return NULL;
1252
1253 fseek(fp, 0L, SEEK_END);
1254 size = ftell(fp);
1255
1256 fseek(fp, 0L, SEEK_SET);
1257
1258 shader_code = malloc(size);
Tony Barbourfdc2d352015-04-22 09:02:32 -06001259 retval = fread(shader_code, size, 1, fp);
1260 assert(retval == 1);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001261
1262 *psize = size;
1263
1264 return shader_code;
1265}
1266
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001267static VkShader demo_prepare_vs(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001268{
Cody Northrop1fedb212015-05-28 11:27:16 -06001269 if (!demo->use_glsl) {
1270 void *vertShaderCode;
1271 size_t size;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001272
Cody Northrop1fedb212015-05-28 11:27:16 -06001273 vertShaderCode = demo_read_spv("cube-vert.spv", &size);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001274
Cody Northrop1fedb212015-05-28 11:27:16 -06001275 return demo_prepare_shader(demo, VK_SHADER_STAGE_VERTEX,
1276 vertShaderCode, size);
1277 } else {
1278 static const char *vertShaderText =
1279 "#version 140\n"
1280 "#extension GL_ARB_separate_shader_objects : enable\n"
1281 "#extension GL_ARB_shading_language_420pack : enable\n"
1282 "\n"
1283 "layout(binding = 0) uniform buf {\n"
1284 " mat4 MVP;\n"
1285 " vec4 position[12*3];\n"
1286 " vec4 attr[12*3];\n"
1287 "} ubuf;\n"
1288 "\n"
1289 "layout (location = 0) out vec4 texcoord;\n"
1290 "\n"
1291 "void main() \n"
1292 "{\n"
1293 " texcoord = ubuf.attr[gl_VertexID];\n"
1294 " gl_Position = ubuf.MVP * ubuf.position[gl_VertexID];\n"
1295 "\n"
1296 " // GL->VK conventions\n"
1297 " gl_Position.y = -gl_Position.y;\n"
1298 " gl_Position.z = (gl_Position.z + gl_Position.w) / 2.0;\n"
1299 "}\n";
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001300
Cody Northrop1fedb212015-05-28 11:27:16 -06001301 return demo_prepare_shader(demo, VK_SHADER_STAGE_VERTEX,
1302 (const void *) vertShaderText,
1303 strlen(vertShaderText));
1304 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001305}
1306
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001307static VkShader demo_prepare_fs(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001308{
Cody Northrop1fedb212015-05-28 11:27:16 -06001309 if (!demo->use_glsl) {
1310 void *fragShaderCode;
1311 size_t size;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001312
Cody Northrop1fedb212015-05-28 11:27:16 -06001313 fragShaderCode = demo_read_spv("cube-frag.spv", &size);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001314
Cody Northrop1fedb212015-05-28 11:27:16 -06001315 return demo_prepare_shader(demo, VK_SHADER_STAGE_FRAGMENT,
1316 fragShaderCode, size);
1317 } else {
1318 static const char *fragShaderText =
1319 "#version 140\n"
1320 "#extension GL_ARB_separate_shader_objects : enable\n"
1321 "#extension GL_ARB_shading_language_420pack : enable\n"
1322 "layout (binding = 1) uniform sampler2D tex;\n"
1323 "\n"
1324 "layout (location = 0) in vec4 texcoord;\n"
1325 "layout (location = 0) out vec4 uFragColor;\n"
1326 "void main() {\n"
1327 " uFragColor = texture(tex, texcoord.xy);\n"
1328 "}\n";
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001329
Cody Northrop1fedb212015-05-28 11:27:16 -06001330 return demo_prepare_shader(demo, VK_SHADER_STAGE_FRAGMENT,
1331 (const void *) fragShaderText,
1332 strlen(fragShaderText));
1333 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001334}
1335
1336static void demo_prepare_pipeline(struct demo *demo)
1337{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001338 VkGraphicsPipelineCreateInfo pipeline;
1339 VkPipelineIaStateCreateInfo ia;
1340 VkPipelineRsStateCreateInfo rs;
1341 VkPipelineCbStateCreateInfo cb;
1342 VkPipelineDsStateCreateInfo ds;
1343 VkPipelineShaderStageCreateInfo vs;
1344 VkPipelineShaderStageCreateInfo fs;
1345 VkPipelineVpStateCreateInfo vp;
1346 VkPipelineMsStateCreateInfo ms;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001347 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001348
1349 memset(&pipeline, 0, sizeof(pipeline));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001350 pipeline.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001351 pipeline.layout = demo->pipeline_layout;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001352
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001353 memset(&ia, 0, sizeof(ia));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001354 ia.sType = VK_STRUCTURE_TYPE_PIPELINE_IA_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06001355 ia.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001356
1357 memset(&rs, 0, sizeof(rs));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001358 rs.sType = VK_STRUCTURE_TYPE_PIPELINE_RS_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06001359 rs.fillMode = VK_FILL_MODE_SOLID;
1360 rs.cullMode = VK_CULL_MODE_BACK;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001361 rs.frontFace = VK_FRONT_FACE_CCW;
Chia-I Wubb67e6e2015-04-22 14:20:52 +08001362 rs.depthClipEnable = VK_TRUE;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001363
1364 memset(&cb, 0, sizeof(cb));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001365 cb.sType = VK_STRUCTURE_TYPE_PIPELINE_CB_STATE_CREATE_INFO;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001366 VkPipelineCbAttachmentState att_state[1];
Tony Barbour29645d02015-01-16 14:27:35 -07001367 memset(att_state, 0, sizeof(att_state));
1368 att_state[0].format = demo->format;
1369 att_state[0].channelWriteMask = 0xf;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001370 att_state[0].blendEnable = VK_FALSE;
Tony Barbour29645d02015-01-16 14:27:35 -07001371 cb.attachmentCount = 1;
1372 cb.pAttachments = att_state;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001373
Tony Barbour29645d02015-01-16 14:27:35 -07001374 memset(&vp, 0, sizeof(vp));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001375 vp.sType = VK_STRUCTURE_TYPE_PIPELINE_VP_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06001376 vp.viewportCount = 1;
Tony Barbour29645d02015-01-16 14:27:35 -07001377
1378 memset(&ds, 0, sizeof(ds));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001379 ds.sType = VK_STRUCTURE_TYPE_PIPELINE_DS_STATE_CREATE_INFO;
Tony Barbour29645d02015-01-16 14:27:35 -07001380 ds.format = demo->depth.format;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001381 ds.depthTestEnable = VK_TRUE;
1382 ds.depthWriteEnable = VK_TRUE;
Tony Barbour72304ef2015-04-16 15:59:00 -06001383 ds.depthCompareOp = VK_COMPARE_OP_LESS_EQUAL;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001384 ds.depthBoundsEnable = VK_FALSE;
1385 ds.back.stencilFailOp = VK_STENCIL_OP_KEEP;
1386 ds.back.stencilPassOp = VK_STENCIL_OP_KEEP;
Tony Barbour72304ef2015-04-16 15:59:00 -06001387 ds.back.stencilCompareOp = VK_COMPARE_OP_ALWAYS;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001388 ds.stencilTestEnable = VK_FALSE;
Tony Barbour29645d02015-01-16 14:27:35 -07001389 ds.front = ds.back;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001390
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001391 memset(&vs, 0, sizeof(vs));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001392 vs.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1393 vs.shader.stage = VK_SHADER_STAGE_VERTEX;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001394 vs.shader.shader = demo_prepare_vs(demo);
Mike Stroyanebae8322015-04-17 12:36:38 -06001395 assert(vs.shader.shader != VK_NULL_HANDLE);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001396
1397 memset(&fs, 0, sizeof(fs));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001398 fs.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1399 fs.shader.stage = VK_SHADER_STAGE_FRAGMENT;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001400 fs.shader.shader = demo_prepare_fs(demo);
Mike Stroyanebae8322015-04-17 12:36:38 -06001401 assert(fs.shader.shader != VK_NULL_HANDLE);
Tony Barbour29645d02015-01-16 14:27:35 -07001402
1403 memset(&ms, 0, sizeof(ms));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001404 ms.sType = VK_STRUCTURE_TYPE_PIPELINE_MS_STATE_CREATE_INFO;
Tony Barbour29645d02015-01-16 14:27:35 -07001405 ms.sampleMask = 1;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001406 ms.multisampleEnable = VK_FALSE;
Tony Barbour29645d02015-01-16 14:27:35 -07001407 ms.samples = 1;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001408
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001409 pipeline.pNext = (const void *) &ia;
1410 ia.pNext = (const void *) &rs;
1411 rs.pNext = (const void *) &cb;
1412 cb.pNext = (const void *) &ms;
1413 ms.pNext = (const void *) &vp;
1414 vp.pNext = (const void *) &ds;
1415 ds.pNext = (const void *) &vs;
1416 vs.pNext = (const void *) &fs;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001417
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001418 err = vkCreateGraphicsPipeline(demo->device, &pipeline, &demo->pipeline);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001419 assert(!err);
1420
Mike Stroyanebae8322015-04-17 12:36:38 -06001421 vkDestroyObject(demo->device, VK_OBJECT_TYPE_SHADER, vs.shader.shader);
1422 vkDestroyObject(demo->device, VK_OBJECT_TYPE_SHADER, fs.shader.shader);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001423}
1424
1425static void demo_prepare_dynamic_states(struct demo *demo)
1426{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001427 VkDynamicVpStateCreateInfo viewport_create;
1428 VkDynamicRsStateCreateInfo raster;
1429 VkDynamicCbStateCreateInfo color_blend;
1430 VkDynamicDsStateCreateInfo depth_stencil;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001431 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001432
Tony Barbour29645d02015-01-16 14:27:35 -07001433 memset(&viewport_create, 0, sizeof(viewport_create));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001434 viewport_create.sType = VK_STRUCTURE_TYPE_DYNAMIC_VP_STATE_CREATE_INFO;
Courtney Goeltzenleuchter1710d8d2015-02-11 14:13:34 -07001435 viewport_create.viewportAndScissorCount = 1;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001436 VkViewport viewport;
Piers Daniell735ee532015-02-23 16:23:13 -07001437 memset(&viewport, 0, sizeof(viewport));
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001438 viewport.height = (float) demo->height;
1439 viewport.width = (float) demo->width;
1440 viewport.minDepth = (float) 0.0f;
1441 viewport.maxDepth = (float) 1.0f;
Piers Daniell735ee532015-02-23 16:23:13 -07001442 viewport_create.pViewports = &viewport;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001443 VkRect scissor;
Piers Daniell735ee532015-02-23 16:23:13 -07001444 memset(&scissor, 0, sizeof(scissor));
Courtney Goeltzenleuchter1710d8d2015-02-11 14:13:34 -07001445 scissor.extent.width = demo->width;
1446 scissor.extent.height = demo->height;
1447 scissor.offset.x = 0;
1448 scissor.offset.y = 0;
Courtney Goeltzenleuchter1710d8d2015-02-11 14:13:34 -07001449 viewport_create.pScissors = &scissor;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001450
1451 memset(&raster, 0, sizeof(raster));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001452 raster.sType = VK_STRUCTURE_TYPE_DYNAMIC_RS_STATE_CREATE_INFO;
Piers Daniell735ee532015-02-23 16:23:13 -07001453 raster.lineWidth = 1.0;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001454
1455 memset(&color_blend, 0, sizeof(color_blend));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001456 color_blend.sType = VK_STRUCTURE_TYPE_DYNAMIC_CB_STATE_CREATE_INFO;
Piers Daniell735ee532015-02-23 16:23:13 -07001457 color_blend.blendConst[0] = 1.0f;
1458 color_blend.blendConst[1] = 1.0f;
1459 color_blend.blendConst[2] = 1.0f;
1460 color_blend.blendConst[3] = 1.0f;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001461
1462 memset(&depth_stencil, 0, sizeof(depth_stencil));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001463 depth_stencil.sType = VK_STRUCTURE_TYPE_DYNAMIC_DS_STATE_CREATE_INFO;
Mark Lobodzinski89d32b12015-06-12 11:14:17 -06001464 depth_stencil.minDepthBounds = 0.0f;
1465 depth_stencil.maxDepthBounds = 1.0f;
Tony Barbour29645d02015-01-16 14:27:35 -07001466 depth_stencil.stencilBackRef = 0;
1467 depth_stencil.stencilFrontRef = 0;
1468 depth_stencil.stencilReadMask = 0xff;
1469 depth_stencil.stencilWriteMask = 0xff;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001470
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001471 err = vkCreateDynamicViewportState(demo->device, &viewport_create, &demo->viewport);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001472 assert(!err);
1473
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001474 err = vkCreateDynamicRasterState(demo->device, &raster, &demo->raster);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001475 assert(!err);
1476
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001477 err = vkCreateDynamicColorBlendState(demo->device,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001478 &color_blend, &demo->color_blend);
1479 assert(!err);
1480
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001481 err = vkCreateDynamicDepthStencilState(demo->device,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001482 &depth_stencil, &demo->depth_stencil);
1483 assert(!err);
1484}
1485
Chia-I Wu63ea9262015-03-26 13:14:16 +08001486static void demo_prepare_descriptor_pool(struct demo *demo)
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001487{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001488 const VkDescriptorTypeCount type_counts[2] = {
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001489 [0] = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001490 .type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001491 .count = 1,
1492 },
1493 [1] = {
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -06001494 .type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001495 .count = DEMO_TEXTURE_COUNT,
1496 },
1497 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001498 const VkDescriptorPoolCreateInfo descriptor_pool = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001499 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001500 .pNext = NULL,
1501 .count = 2,
1502 .pTypeCount = type_counts,
1503 };
Tony Barbourfdc2d352015-04-22 09:02:32 -06001504 VkResult U_ASSERT_ONLY err;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001505
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001506 err = vkCreateDescriptorPool(demo->device,
1507 VK_DESCRIPTOR_POOL_USAGE_ONE_SHOT, 1,
Chia-I Wu63ea9262015-03-26 13:14:16 +08001508 &descriptor_pool, &demo->desc_pool);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001509 assert(!err);
1510}
1511
1512static void demo_prepare_descriptor_set(struct demo *demo)
1513{
Chia-I Wuae721ba2015-05-25 16:27:55 +08001514 VkDescriptorInfo tex_descs[DEMO_TEXTURE_COUNT];
1515 VkWriteDescriptorSet writes[2];
Tony Barbourfdc2d352015-04-22 09:02:32 -06001516 VkResult U_ASSERT_ONLY err;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001517 uint32_t count;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001518 uint32_t i;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001519
Mike Stroyanebae8322015-04-17 12:36:38 -06001520 err = vkAllocDescriptorSets(demo->device, demo->desc_pool,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001521 VK_DESCRIPTOR_SET_USAGE_STATIC,
Chia-I Wu87544e72015-02-23 10:41:08 -07001522 1, &demo->desc_layout,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001523 &demo->desc_set, &count);
1524 assert(!err && count == 1);
1525
Chia-I Wuae721ba2015-05-25 16:27:55 +08001526 memset(&tex_descs, 0, sizeof(tex_descs));
1527 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
1528 tex_descs[i].sampler = demo->textures[i].sampler;
1529 tex_descs[i].imageView = demo->textures[i].view;
1530 tex_descs[i].imageLayout = VK_IMAGE_LAYOUT_GENERAL;
1531 }
1532
1533 memset(&writes, 0, sizeof(writes));
1534
1535 writes[0].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
1536 writes[0].destSet = demo->desc_set;
1537 writes[0].count = 1;
1538 writes[0].descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
1539 writes[0].pDescriptors = &demo->uniform_data.desc;
1540
1541 writes[1].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
1542 writes[1].destSet = demo->desc_set;
1543 writes[1].destBinding = 1;
1544 writes[1].count = DEMO_TEXTURE_COUNT;
1545 writes[1].descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
1546 writes[1].pDescriptors = tex_descs;
1547
1548 err = vkUpdateDescriptorSets(demo->device, 2, writes, 0, NULL);
1549 assert(!err);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001550}
1551
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001552static void demo_prepare(struct demo *demo)
1553{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001554 const VkCmdBufferCreateInfo cmd = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001555 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001556 .pNext = NULL,
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07001557 .queueNodeIndex = demo->graphics_queue_node_index,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001558 .flags = 0,
1559 };
Tony Barbourfdc2d352015-04-22 09:02:32 -06001560 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001561
1562 demo_prepare_buffers(demo);
1563 demo_prepare_depth(demo);
1564 demo_prepare_textures(demo);
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001565 demo_prepare_cube_data_buffer(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001566
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001567 demo_prepare_descriptor_layout(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001568 demo_prepare_pipeline(demo);
1569 demo_prepare_dynamic_states(demo);
1570
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001571 for (int i = 0; i < DEMO_BUFFER_COUNT; i++) {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001572 err = vkCreateCommandBuffer(demo->device, &cmd, &demo->buffers[i].cmd);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001573 assert(!err);
1574 }
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001575
Chia-I Wu63ea9262015-03-26 13:14:16 +08001576 demo_prepare_descriptor_pool(demo);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001577 demo_prepare_descriptor_set(demo);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001578
1579
1580 for (int i = 0; i < DEMO_BUFFER_COUNT; i++) {
1581 demo->current_buffer = i;
1582 demo_draw_build_cmd(demo, demo->buffers[i].cmd);
1583 }
1584
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001585 /*
1586 * Prepare functions above may generate pipeline commands
1587 * that need to be flushed before beginning the render loop.
1588 */
1589 demo_flush_init_cmd(demo);
1590
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001591 demo->current_buffer = 0;
Jon Ashburn8d26c062015-04-24 09:46:24 -07001592 demo->prepared = true;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001593}
1594
Ian Elliott639ca472015-04-16 15:23:05 -06001595#ifdef _WIN32
1596static void demo_run(struct demo *demo)
1597{
Courtney Goeltzenleuchterb7e22702015-04-27 14:56:34 -06001598 if (!demo->prepared)
1599 return;
Ian Elliott639ca472015-04-16 15:23:05 -06001600 // Wait for work to finish before updating MVP.
1601 vkDeviceWaitIdle(demo->device);
1602 demo_update_data_buffer(demo);
1603
1604 demo_draw(demo);
1605
1606 // Wait for work to finish before updating MVP.
1607 vkDeviceWaitIdle(demo->device);
1608}
1609
1610// On MS-Windows, make this a global, so it's available to WndProc()
1611struct demo demo;
1612
1613// MS-Windows event handling function:
1614LRESULT CALLBACK WndProc(HWND hWnd,
1615 UINT uMsg,
1616 WPARAM wParam,
1617 LPARAM lParam)
1618{
Ian Elliott639ca472015-04-16 15:23:05 -06001619 switch(uMsg)
1620 {
Ian Elliott639ca472015-04-16 15:23:05 -06001621 case WM_CLOSE:
1622 PostQuitMessage(0);
Tony Barbour3dddd5d2015-04-29 16:19:20 -06001623 break;
Ian Elliott639ca472015-04-16 15:23:05 -06001624 case WM_PAINT:
1625 demo_run(&demo);
1626 return 0;
1627 default:
1628 break;
1629 }
1630 return (DefWindowProc(hWnd, uMsg, wParam, lParam));
1631}
1632
1633static void demo_create_window(struct demo *demo)
1634{
1635 WNDCLASSEX win_class;
1636
1637 // Initialize the window class structure:
1638 win_class.cbSize = sizeof(WNDCLASSEX);
1639 win_class.style = CS_HREDRAW | CS_VREDRAW;
1640 win_class.lpfnWndProc = WndProc;
1641 win_class.cbClsExtra = 0;
1642 win_class.cbWndExtra = 0;
1643 win_class.hInstance = demo->connection; // hInstance
1644 win_class.hIcon = LoadIcon(NULL, IDI_APPLICATION);
1645 win_class.hCursor = LoadCursor(NULL, IDC_ARROW);
1646 win_class.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
1647 win_class.lpszMenuName = NULL;
1648 win_class.lpszClassName = demo->name;
1649 win_class.hIconSm = LoadIcon(NULL, IDI_WINLOGO);
1650 // Register window class:
1651 if (!RegisterClassEx(&win_class)) {
1652 // It didn't work, so try to give a useful error:
1653 printf("Unexpected error trying to start the application!\n");
1654 fflush(stdout);
1655 exit(1);
1656 }
1657 // Create window with the registered class:
Mike Stroyanb46779e2015-06-15 14:20:13 -06001658 RECT wr = { 0, 0, demo->width, demo->height };
1659 AdjustWindowRect(&wr, WS_OVERLAPPEDWINDOW, FALSE);
Ian Elliott639ca472015-04-16 15:23:05 -06001660 demo->window = CreateWindowEx(0,
1661 demo->name, // class name
1662 demo->name, // app name
1663 WS_OVERLAPPEDWINDOW | // window style
1664 WS_VISIBLE |
1665 WS_SYSMENU,
1666 100,100, // x/y coords
Mike Stroyanb46779e2015-06-15 14:20:13 -06001667 wr.right-wr.left, // width
1668 wr.bottom-wr.top, // height
Ian Elliott639ca472015-04-16 15:23:05 -06001669 NULL, // handle to parent
1670 NULL, // handle to menu
1671 demo->connection, // hInstance
1672 NULL); // no extra parameters
1673 if (!demo->window) {
1674 // It didn't work, so try to give a useful error:
1675 printf("Cannot create a window in which to draw!\n");
1676 fflush(stdout);
1677 exit(1);
1678 }
1679}
1680#else // _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001681static void demo_handle_event(struct demo *demo,
1682 const xcb_generic_event_t *event)
1683{
Piers Daniell735ee532015-02-23 16:23:13 -07001684 uint8_t event_code = event->response_type & 0x7f;
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07001685 switch (event_code) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001686 case XCB_EXPOSE:
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07001687 // TODO: Resize window
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001688 break;
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07001689 case XCB_CLIENT_MESSAGE:
1690 if((*(xcb_client_message_event_t*)event).data.data32[0] ==
1691 (*demo->atom_wm_delete_window).atom) {
1692 demo->quit = true;
1693 }
1694 break;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001695 case XCB_KEY_RELEASE:
1696 {
1697 const xcb_key_release_event_t *key =
1698 (const xcb_key_release_event_t *) event;
1699
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001700 switch (key->detail) {
1701 case 0x9: // Escape
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001702 demo->quit = true;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001703 break;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001704 case 0x71: // left arrow key
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001705 demo->spin_angle += demo->spin_increment;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001706 break;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001707 case 0x72: // right arrow key
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001708 demo->spin_angle -= demo->spin_increment;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001709 break;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001710 case 0x41:
1711 demo->pause = !demo->pause;
Piers Daniell735ee532015-02-23 16:23:13 -07001712 break;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001713 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001714 }
1715 break;
1716 default:
1717 break;
1718 }
1719}
1720
1721static void demo_run(struct demo *demo)
1722{
1723 xcb_flush(demo->connection);
1724
1725 while (!demo->quit) {
1726 xcb_generic_event_t *event;
1727
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07001728 if (demo->pause) {
1729 event = xcb_wait_for_event(demo->connection);
1730 } else {
1731 event = xcb_poll_for_event(demo->connection);
1732 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001733 if (event) {
1734 demo_handle_event(demo, event);
1735 free(event);
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07001736 }
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07001737
1738 // Wait for work to finish before updating MVP.
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001739 vkDeviceWaitIdle(demo->device);
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07001740 demo_update_data_buffer(demo);
1741
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07001742 demo_draw(demo);
Courtney Goeltzenleuchter1454f3c2014-11-18 11:28:09 -07001743
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07001744 // Wait for work to finish before updating MVP.
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001745 vkDeviceWaitIdle(demo->device);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001746 }
1747}
1748
1749static void demo_create_window(struct demo *demo)
1750{
1751 uint32_t value_mask, value_list[32];
1752
1753 demo->window = xcb_generate_id(demo->connection);
1754
1755 value_mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK;
1756 value_list[0] = demo->screen->black_pixel;
1757 value_list[1] = XCB_EVENT_MASK_KEY_RELEASE |
1758 XCB_EVENT_MASK_EXPOSURE;
1759
1760 xcb_create_window(demo->connection,
1761 XCB_COPY_FROM_PARENT,
1762 demo->window, demo->screen->root,
1763 0, 0, demo->width, demo->height, 0,
1764 XCB_WINDOW_CLASS_INPUT_OUTPUT,
1765 demo->screen->root_visual,
1766 value_mask, value_list);
1767
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07001768 /* Magic code that will send notification when window is destroyed */
1769 xcb_intern_atom_cookie_t cookie = xcb_intern_atom(demo->connection, 1, 12,
1770 "WM_PROTOCOLS");
1771 xcb_intern_atom_reply_t* reply = xcb_intern_atom_reply(demo->connection, cookie, 0);
1772
1773 xcb_intern_atom_cookie_t cookie2 = xcb_intern_atom(demo->connection, 0, 16, "WM_DELETE_WINDOW");
1774 demo->atom_wm_delete_window = xcb_intern_atom_reply(demo->connection, cookie2, 0);
1775
1776 xcb_change_property(demo->connection, XCB_PROP_MODE_REPLACE,
1777 demo->window, (*reply).atom, 4, 32, 1,
1778 &(*demo->atom_wm_delete_window).atom);
1779 free(reply);
1780
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001781 xcb_map_window(demo->connection, demo->window);
1782}
Ian Elliott639ca472015-04-16 15:23:05 -06001783#endif // _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001784
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001785static void demo_init_vk(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001786{
Tobin Ehlis99239dc2015-04-16 18:04:57 -06001787 VkResult err;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06001788 VkExtensionProperties *instance_extensions;
1789 uint32_t instance_extension_count = 0;
1790 VkExtensionProperties *device_extensions;
1791 uint32_t device_extension_count = 0;
Tobin Ehlis99239dc2015-04-16 18:04:57 -06001792 size_t extSize = sizeof(uint32_t);
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06001793 uint32_t total_extension_count = 0;
1794 err = vkGetGlobalExtensionInfo(VK_EXTENSION_INFO_TYPE_COUNT, 0, &extSize, &total_extension_count);
Tobin Ehlis99239dc2015-04-16 18:04:57 -06001795 assert(!err);
1796
1797 VkExtensionProperties extProp;
1798 extSize = sizeof(VkExtensionProperties);
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06001799 bool32_t WSIextFound = 0;
1800 instance_extensions = malloc(sizeof(VkExtensionProperties) * total_extension_count);
1801 device_extensions = malloc(sizeof(VkExtensionProperties) * total_extension_count);
1802 for (uint32_t i = 0; i < total_extension_count; i++) {
Tobin Ehlis99239dc2015-04-16 18:04:57 -06001803 err = vkGetGlobalExtensionInfo(VK_EXTENSION_INFO_TYPE_PROPERTIES, i, &extSize, &extProp);
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06001804 if (!strcmp("VK_WSI_LunarG", extProp.name)) {
1805 WSIextFound = 1;
1806 memcpy(&instance_extensions[instance_extension_count++], &extProp, sizeof(VkExtensionProperties));
1807 memcpy(&device_extensions[device_extension_count++], &extProp, sizeof(VkExtensionProperties));
1808 }
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06001809 if (!strcmp(DEBUG_REPORT_EXTENSION_NAME, extProp.name)) {
1810 memcpy(&instance_extensions[instance_extension_count++], &extProp, sizeof(VkExtensionProperties));
1811 }
Tobin Ehlis99239dc2015-04-16 18:04:57 -06001812 }
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06001813 if (!WSIextFound) {
Ian Elliott65152912015-04-28 13:22:33 -06001814 ERR_EXIT("vkGetGlobalExtensionInfo failed to find the "
1815 "\"VK_WSI_LunarG\" extension.\n\nDo you have a compatible "
1816 "Vulkan installable client driver (ICD) installed?\nPlease "
1817 "look at the Getting Started guide for additional "
1818 "information.\n",
1819 "vkCreateInstance Failure");
1820 }
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001821 const VkApplicationInfo app = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001822 .sType = VK_STRUCTURE_TYPE_APPLICATION_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001823 .pNext = NULL,
Ian Elliott44e33f72015-04-28 10:52:52 -06001824 .pAppName = APP_SHORT_NAME,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001825 .appVersion = 0,
Ian Elliott44e33f72015-04-28 10:52:52 -06001826 .pEngineName = APP_SHORT_NAME,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001827 .engineVersion = 0,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001828 .apiVersion = VK_API_VERSION,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001829 };
Tony Barbour3dddd5d2015-04-29 16:19:20 -06001830 VkInstanceCreateInfo inst_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001831 .sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
Jon Ashburnab46b362015-04-04 14:52:07 -06001832 .pNext = NULL,
1833 .pAppInfo = &app,
1834 .pAllocCb = NULL,
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06001835 .extensionCount = instance_extension_count,
1836 .pEnabledExtensions = instance_extensions,
Jon Ashburnab46b362015-04-04 14:52:07 -06001837 };
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -06001838 const VkDeviceQueueCreateInfo queue = {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001839 .queueNodeIndex = 0,
1840 .queueCount = 1,
1841 };
Ian Elliott097d9f32015-04-28 11:35:02 -06001842
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06001843 VkDeviceCreateInfo device = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001844 .sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001845 .pNext = NULL,
1846 .queueRecordCount = 1,
1847 .pRequestedQueues = &queue,
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06001848 .extensionCount = device_extension_count,
1849 .pEnabledExtensions = device_extensions,
Jon Ashburn7842d522015-05-18 09:06:15 -06001850 .flags = 0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001851 };
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001852 uint32_t gpu_count;
1853 uint32_t i;
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07001854 size_t data_size;
1855 uint32_t queue_count;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001856
Tony Barbour3dddd5d2015-04-29 16:19:20 -06001857 if (demo->validate) {
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06001858 inst_info.extensionCount = 3;
1859 device.extensionCount = 3;
Tony Barbour3dddd5d2015-04-29 16:19:20 -06001860 }
1861
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001862 err = vkCreateInstance(&inst_info, &demo->inst);
Ian Elliott07264132015-04-28 11:35:02 -06001863 if (err == VK_ERROR_INCOMPATIBLE_DRIVER) {
1864 ERR_EXIT("Cannot find a compatible Vulkan installable client driver "
Ian Elliott65152912015-04-28 13:22:33 -06001865 "(ICD).\n\nPlease look at the Getting Started guide for "
Ian Elliott07264132015-04-28 11:35:02 -06001866 "additional information.\n",
1867 "vkCreateInstance Failure");
Tony Barbour3dddd5d2015-04-29 16:19:20 -06001868 } else if (err == VK_ERROR_INVALID_EXTENSION) {
1869 ERR_EXIT("Cannot find a specified extension library"
1870 ".\nMake sure your layers path is set appropriately\n",
1871 "vkCreateInstance Failure");
Ian Elliott07264132015-04-28 11:35:02 -06001872 } else if (err) {
Ian Elliott65152912015-04-28 13:22:33 -06001873 ERR_EXIT("vkCreateInstance failed.\n\nDo you have a compatible Vulkan "
1874 "installable client driver (ICD) installed?\nPlease look at "
Ian Elliott07264132015-04-28 11:35:02 -06001875 "the Getting Started guide for additional information.\n",
1876 "vkCreateInstance Failure");
Ian Elliott3979e282015-04-03 15:24:55 -06001877 }
Jon Ashburnab46b362015-04-04 14:52:07 -06001878
Tony Barbour3dddd5d2015-04-29 16:19:20 -06001879
Jon Ashburn07c0c0c2015-04-15 11:31:12 -06001880 gpu_count = 1;
1881 err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, &demo->gpu);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001882 assert(!err && gpu_count == 1);
1883
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06001884
Tony Barbour3dddd5d2015-04-29 16:19:20 -06001885 if (demo->validate) {
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06001886 demo->dbgCreateMsgCallback = vkGetInstanceProcAddr((VkPhysicalDevice) NULL, "vkDbgCreateMsgCallback");
1887 if (!demo->dbgCreateMsgCallback) {
1888 ERR_EXIT("GetProcAddr: Unable to find vkDbgCreateMsgCallback\n",
1889 "vkGetProcAddr Failure");
1890 }
1891 err = demo->dbgCreateMsgCallback(
1892 demo->inst,
1893 VK_DBG_REPORT_ERROR_BIT | VK_DBG_REPORT_WARN_BIT,
1894 dbgFunc, NULL,
1895 &demo->msg_callback);
1896 switch (err) {
1897 case VK_SUCCESS:
1898 break;
1899 case VK_ERROR_INVALID_POINTER:
1900 ERR_EXIT("dbgCreateMsgCallback: Invalid pointer\n",
1901 "dbgCreateMsgCallback Failure");
1902 break;
1903 case VK_ERROR_OUT_OF_HOST_MEMORY:
1904 ERR_EXIT("dbgCreateMsgCallback: out of host memory\n",
1905 "dbgCreateMsgCallback Failure");
1906 break;
1907 default:
1908 ERR_EXIT("dbgCreateMsgCallback: unknown failure\n",
1909 "dbgCreateMsgCallback Failure");
1910 break;
1911 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06001912 }
1913
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001914 err = vkCreateDevice(demo->gpu, &device, &demo->device);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001915 assert(!err);
1916
Jon Ashburn0b85d052015-05-21 18:13:33 -06001917 demo->fpCreateSwapChainWSI = vkGetDeviceProcAddr(demo->device, "vkCreateSwapChainWSI");
1918 if (demo->fpCreateSwapChainWSI == NULL)
1919 ERR_EXIT("vkGetDeviceProcAddr failed to find vkCreateSwapChainWSI",
1920 "vkGetDeviceProcAddr Failure");
1921 demo->fpDestroySwapChainWSI = vkGetDeviceProcAddr(demo->device, "vkDestroySwapChainWSI");
1922 if (demo->fpDestroySwapChainWSI == NULL)
1923 ERR_EXIT("vkGetDeviceProcAddr failed to find vkDestroySwapChainWSI",
1924 "vkGetDeviceProcAddr Failure");
1925 demo->fpGetSwapChainInfoWSI = vkGetDeviceProcAddr(demo->device, "vkGetSwapChainInfoWSI");
1926 if (demo->fpGetSwapChainInfoWSI == NULL)
1927 ERR_EXIT("vkGetDeviceProcAddr failed to find vkGetSwapChainInfoWSI",
1928 "vkGetDeviceProcAddr Failure");
1929 demo->fpQueuePresentWSI = vkGetDeviceProcAddr(demo->device, "vkQueuePresentWSI");
1930 if (demo->fpQueuePresentWSI == NULL)
1931 ERR_EXIT("vkGetDeviceProcAddr failed to find vkQueuePresentWSI",
1932 "vkGetDeviceProcAddr Failure");
1933
Tony Barbour72304ef2015-04-16 15:59:00 -06001934 err = vkGetPhysicalDeviceInfo(demo->gpu, VK_PHYSICAL_DEVICE_INFO_TYPE_PROPERTIES,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001935 &data_size, NULL);
1936 assert(!err);
1937
Tony Barbour72304ef2015-04-16 15:59:00 -06001938 demo->gpu_props = (VkPhysicalDeviceProperties *) malloc(data_size);
1939 err = vkGetPhysicalDeviceInfo(demo->gpu, VK_PHYSICAL_DEVICE_INFO_TYPE_PROPERTIES,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001940 &data_size, demo->gpu_props);
1941 assert(!err);
1942
Tony Barbour72304ef2015-04-16 15:59:00 -06001943 err = vkGetPhysicalDeviceInfo(demo->gpu, VK_PHYSICAL_DEVICE_INFO_TYPE_QUEUE_PROPERTIES,
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07001944 &data_size, NULL);
1945 assert(!err);
1946
Tony Barbour72304ef2015-04-16 15:59:00 -06001947 demo->queue_props = (VkPhysicalDeviceQueueProperties *) malloc(data_size);
1948 err = vkGetPhysicalDeviceInfo(demo->gpu, VK_PHYSICAL_DEVICE_INFO_TYPE_QUEUE_PROPERTIES,
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07001949 &data_size, demo->queue_props);
1950 assert(!err);
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001951 queue_count = (uint32_t)(data_size / sizeof(VkPhysicalDeviceQueueProperties));
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07001952 assert(queue_count >= 1);
1953
Mark Lobodzinskida9b1092015-04-16 11:44:05 -05001954 // Graphics queue and MemMgr queue can be separate.
1955 // TODO: Add support for separate queues, including synchronization,
Mark Lobodzinski6baaec72015-05-11 17:21:15 -05001956 // and appropriate tracking for QueueSubmit
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07001957 for (i = 0; i < queue_count; i++) {
Mark Lobodzinski6baaec72015-05-11 17:21:15 -05001958 if (demo->queue_props[i].queueFlags & VK_QUEUE_GRAPHICS_BIT)
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07001959 break;
1960 }
1961 assert(i < queue_count);
1962 demo->graphics_queue_node_index = i;
1963
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001964 err = vkGetDeviceQueue(demo->device, demo->graphics_queue_node_index,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001965 0, &demo->queue);
1966 assert(!err);
Tony Barbour3dddd5d2015-04-29 16:19:20 -06001967
Jon Ashburnd7130cb2015-06-16 12:44:51 -06001968 // for now hardcode format till get WSI support
1969 demo->format = VK_FORMAT_B8G8R8A8_UNORM;
Ian Elliotte4602cd2015-04-21 16:41:02 -06001970
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001971}
1972
1973static void demo_init_connection(struct demo *demo)
1974{
Ian Elliott639ca472015-04-16 15:23:05 -06001975#ifndef _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001976 const xcb_setup_t *setup;
1977 xcb_screen_iterator_t iter;
1978 int scr;
1979
1980 demo->connection = xcb_connect(NULL, &scr);
Ian Elliott3979e282015-04-03 15:24:55 -06001981 if (demo->connection == NULL) {
1982 printf("Cannot find a compatible Vulkan installable client driver "
1983 "(ICD).\nExiting ...\n");
1984 fflush(stdout);
1985 exit(1);
1986 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001987
1988 setup = xcb_get_setup(demo->connection);
1989 iter = xcb_setup_roots_iterator(setup);
1990 while (scr-- > 0)
1991 xcb_screen_next(&iter);
1992
1993 demo->screen = iter.data;
Ian Elliott639ca472015-04-16 15:23:05 -06001994#endif // _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001995}
1996
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001997static void demo_init(struct demo *demo, int argc, char **argv)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001998{
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001999 vec3 eye = {0.0f, 3.0f, 5.0f};
2000 vec3 origin = {0, 0, 0};
Chia-I Wuae3b55d2015-04-22 14:56:17 +08002001 vec3 up = {0.0f, 1.0f, 0.0};
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002002
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002003 memset(demo, 0, sizeof(*demo));
2004
Piers Daniell735ee532015-02-23 16:23:13 -07002005 for (int i = 1; i < argc; i++) {
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002006 if (strcmp(argv[i], "--use_staging") == 0) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002007 demo->use_staging_buffer = true;
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002008 continue;
Ian Elliott639ca472015-04-16 15:23:05 -06002009 }
Cody Northrop1fedb212015-05-28 11:27:16 -06002010 if (strcmp(argv[i], "--use_glsl") == 0) {
2011 demo->use_glsl = true;
2012 continue;
2013 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002014 if (strcmp(argv[i], "--validate") == 0) {
2015 demo->validate = true;
2016 continue;
2017 }
2018
2019 fprintf(stderr, "Usage:\n %s [--use_staging] [--validate}\n", APP_SHORT_NAME);
Ian Elliott639ca472015-04-16 15:23:05 -06002020 fflush(stderr);
2021 exit(1);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002022 }
2023
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002024 demo_init_connection(demo);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002025 demo_init_vk(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002026
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002027 demo->width = 500;
2028 demo->height = 500;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002029
2030 demo->spin_angle = 0.01f;
2031 demo->spin_increment = 0.01f;
2032 demo->pause = false;
2033
Piers Daniell735ee532015-02-23 16:23:13 -07002034 mat4x4_perspective(demo->projection_matrix, (float)degreesToRadians(45.0f), 1.0f, 0.1f, 100.0f);
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002035 mat4x4_look_at(demo->view_matrix, eye, origin, up);
2036 mat4x4_identity(demo->model_matrix);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002037}
2038
2039static void demo_cleanup(struct demo *demo)
2040{
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05002041 uint32_t i;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002042
Courtney Goeltzenleuchterb7e22702015-04-27 14:56:34 -06002043 demo->prepared = false;
2044
Mike Stroyanebae8322015-04-17 12:36:38 -06002045 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DESCRIPTOR_SET, demo->desc_set);
2046 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DESCRIPTOR_POOL, demo->desc_pool);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08002047
Mike Stroyanebae8322015-04-17 12:36:38 -06002048 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DYNAMIC_VP_STATE, demo->viewport);
2049 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DYNAMIC_RS_STATE, demo->raster);
2050 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DYNAMIC_CB_STATE, demo->color_blend);
2051 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DYNAMIC_DS_STATE, demo->depth_stencil);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002052
Mike Stroyanebae8322015-04-17 12:36:38 -06002053 vkDestroyObject(demo->device, VK_OBJECT_TYPE_PIPELINE, demo->pipeline);
2054 vkDestroyObject(demo->device, VK_OBJECT_TYPE_PIPELINE_LAYOUT, demo->pipeline_layout);
2055 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DESCRIPTOR_SET_LAYOUT, demo->desc_layout);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002056
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002057 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Mike Stroyanebae8322015-04-17 12:36:38 -06002058 vkDestroyObject(demo->device, VK_OBJECT_TYPE_IMAGE_VIEW, demo->textures[i].view);
Mike Stroyanebae8322015-04-17 12:36:38 -06002059 vkDestroyObject(demo->device, VK_OBJECT_TYPE_IMAGE, demo->textures[i].image);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05002060 vkFreeMemory(demo->device, demo->textures[i].mem);
Mike Stroyanebae8322015-04-17 12:36:38 -06002061 vkDestroyObject(demo->device, VK_OBJECT_TYPE_SAMPLER, demo->textures[i].sampler);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002062 }
Jon Ashburn0b85d052015-05-21 18:13:33 -06002063 demo->fpDestroySwapChainWSI(demo->swap_chain);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002064
Mike Stroyanebae8322015-04-17 12:36:38 -06002065 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DEPTH_STENCIL_VIEW, demo->depth.view);
Mike Stroyanebae8322015-04-17 12:36:38 -06002066 vkDestroyObject(demo->device, VK_OBJECT_TYPE_IMAGE, demo->depth.image);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05002067 vkFreeMemory(demo->device, demo->depth.mem);
Mark Lobodzinski7b8fee62015-02-18 16:38:17 -06002068
Mike Stroyanebae8322015-04-17 12:36:38 -06002069 vkDestroyObject(demo->device, VK_OBJECT_TYPE_BUFFER_VIEW, demo->uniform_data.view);
Mike Stroyanebae8322015-04-17 12:36:38 -06002070 vkDestroyObject(demo->device, VK_OBJECT_TYPE_BUFFER, demo->uniform_data.buf);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05002071 vkFreeMemory(demo->device, demo->uniform_data.mem);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002072
2073 for (i = 0; i < DEMO_BUFFER_COUNT; i++) {
Mike Stroyanebae8322015-04-17 12:36:38 -06002074 vkDestroyObject(demo->device, VK_OBJECT_TYPE_COLOR_ATTACHMENT_VIEW, demo->buffers[i].view);
2075 vkDestroyObject(demo->device, VK_OBJECT_TYPE_COMMAND_BUFFER, demo->buffers[i].cmd);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002076 }
2077
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002078 vkDestroyDevice(demo->device);
2079 vkDestroyInstance(demo->inst);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002080
Ian Elliott639ca472015-04-16 15:23:05 -06002081#ifndef _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002082 xcb_destroy_window(demo->connection, demo->window);
2083 xcb_disconnect(demo->connection);
Ian Elliott639ca472015-04-16 15:23:05 -06002084#endif // _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002085}
2086
Ian Elliott639ca472015-04-16 15:23:05 -06002087#ifdef _WIN32
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002088extern int __getmainargs(
2089 int * _Argc,
2090 char *** _Argv,
2091 char *** _Env,
2092 int _DoWildCard,
2093 int * new_mode);
Ian Elliottf9cf78c2015-04-28 10:33:11 -06002094
Ian Elliotta748eaf2015-04-28 15:50:36 -06002095int WINAPI WinMain(HINSTANCE hInstance,
2096 HINSTANCE hPrevInstance,
2097 LPSTR pCmdLine,
2098 int nCmdShow)
Ian Elliott639ca472015-04-16 15:23:05 -06002099{
2100 MSG msg; // message
2101 bool done; // flag saying when app is complete
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002102 int argc;
2103 char** argv;
2104 char** env;
2105 int new_mode = 0;
Ian Elliott639ca472015-04-16 15:23:05 -06002106
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002107 __getmainargs(&argc,&argv,&env,0,&new_mode);
2108
2109 demo_init(&demo, argc, argv);
2110 demo.connection = hInstance;
2111 strncpy(demo.name, "cube", APP_NAME_STR_LEN);
Ian Elliott639ca472015-04-16 15:23:05 -06002112 demo_create_window(&demo);
2113
2114 demo_prepare(&demo);
2115
2116 done = false; //initialize loop condition variable
2117 /* main message loop*/
2118 while(!done)
2119 {
Ian Elliotta748eaf2015-04-28 15:50:36 -06002120 PeekMessage(&msg, NULL, 0, 0, PM_REMOVE);
Ian Elliott639ca472015-04-16 15:23:05 -06002121 if (msg.message == WM_QUIT) //check for a quit message
2122 {
2123 done = true; //if found, quit app
2124 }
2125 else
2126 {
2127 /* Translate and dispatch to event queue*/
2128 TranslateMessage(&msg);
2129 DispatchMessage(&msg);
2130 }
2131 }
2132
2133 demo_cleanup(&demo);
2134
Tony Barbourf9921732015-04-22 11:36:22 -06002135 return (int) msg.wParam;
Ian Elliott639ca472015-04-16 15:23:05 -06002136}
2137#else // _WIN32
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002138int main(int argc, char **argv)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002139{
2140 struct demo demo;
2141
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002142 demo_init(&demo, argc, argv);
Chia-I Wucbb564e2015-04-16 22:02:10 +08002143 demo_create_window(&demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002144
2145 demo_prepare(&demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002146 demo_run(&demo);
2147
2148 demo_cleanup(&demo);
2149
2150 return 0;
2151}
Ian Elliott639ca472015-04-16 15:23:05 -06002152#endif // _WIN32