blob: c93cb3756d8a3d57953480528dac4868306c5f3d [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
Ian Elliotte4602cd2015-04-21 16:41:02 -0600298 VkDisplayPropertiesWSI *display_props;
299 int num_displays;
Courtney Goeltzenleuchter8f0767f2015-06-10 17:33:55 -0600300 PFN_vkGetDisplayInfoWSI fpGetDisplayInfoWSI;
Jon Ashburn0b85d052015-05-21 18:13:33 -0600301 PFN_vkCreateSwapChainWSI fpCreateSwapChainWSI;
302 PFN_vkDestroySwapChainWSI fpDestroySwapChainWSI;
303 PFN_vkGetSwapChainInfoWSI fpGetSwapChainInfoWSI;
304 PFN_vkQueuePresentWSI fpQueuePresentWSI;
Chia-I Wucbb564e2015-04-16 22:02:10 +0800305 VkSwapChainWSI swap_chain;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600306 struct {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600307 VkImage image;
Tony Barbour72304ef2015-04-16 15:59:00 -0600308 VkDeviceMemory mem;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600309 VkCmdBuffer cmd;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600310
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600311 VkColorAttachmentView view;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600312 } buffers[DEMO_BUFFER_COUNT];
313
314 struct {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600315 VkFormat format;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600316
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600317 VkImage image;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500318 VkDeviceMemory mem;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600319 VkDepthStencilView view;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600320 } depth;
321
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600322 struct texture_object textures[DEMO_TEXTURE_COUNT];
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600323
324 struct {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600325 VkBuffer buf;
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500326 VkDeviceMemory mem;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600327 VkBufferView view;
Chia-I Wuae721ba2015-05-25 16:27:55 +0800328 VkDescriptorInfo desc;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600329 } uniform_data;
330
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600331 VkCmdBuffer cmd; // Buffer for initialization commands
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -0500332 VkPipelineLayout pipeline_layout;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600333 VkDescriptorSetLayout desc_layout;
334 VkPipeline pipeline;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600335
Courtney Goeltzenleuchter5e6d1e92015-04-10 16:24:50 -0600336 VkDynamicVpState viewport;
337 VkDynamicRsState raster;
338 VkDynamicCbState color_blend;
339 VkDynamicDsState depth_stencil;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600340
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600341 mat4x4 projection_matrix;
342 mat4x4 view_matrix;
343 mat4x4 model_matrix;
344
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600345 float spin_angle;
346 float spin_increment;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600347 bool pause;
348
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600349 VkDescriptorPool desc_pool;
350 VkDescriptorSet desc_set;
Chia-I Wu6a3c8972015-01-04 16:27:24 +0800351
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600352 bool quit;
Tony Barbour3dddd5d2015-04-29 16:19:20 -0600353 bool validate;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -0600354 PFN_vkDbgCreateMsgCallback dbgCreateMsgCallback;
355 VkDbgMsgCallback msg_callback;
356
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600357 uint32_t current_buffer;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600358};
359
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600360static void demo_flush_init_cmd(struct demo *demo)
361{
Tony Barbourfdc2d352015-04-22 09:02:32 -0600362 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600363
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600364 if (demo->cmd == VK_NULL_HANDLE)
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600365 return;
366
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600367 err = vkEndCommandBuffer(demo->cmd);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600368 assert(!err);
369
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600370 const VkCmdBuffer cmd_bufs[] = { demo->cmd };
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600371
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600372 err = vkQueueSubmit(demo->queue, 1, cmd_bufs, VK_NULL_HANDLE);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600373 assert(!err);
374
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600375 err = vkQueueWaitIdle(demo->queue);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600376 assert(!err);
377
Mike Stroyanebae8322015-04-17 12:36:38 -0600378 vkDestroyObject(demo->device, VK_OBJECT_TYPE_COMMAND_BUFFER, demo->cmd);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600379 demo->cmd = VK_NULL_HANDLE;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600380}
381
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600382static void demo_set_image_layout(
383 struct demo *demo,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600384 VkImage image,
malnasseeb25d3f2015-06-03 17:28:38 -0400385 VkImageAspect aspect,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600386 VkImageLayout old_image_layout,
387 VkImageLayout new_image_layout)
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600388{
Tony Barbourfdc2d352015-04-22 09:02:32 -0600389 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600390
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600391 if (demo->cmd == VK_NULL_HANDLE) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600392 const VkCmdBufferCreateInfo cmd = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600393 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600394 .pNext = NULL,
395 .queueNodeIndex = demo->graphics_queue_node_index,
396 .flags = 0,
397 };
398
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600399 err = vkCreateCommandBuffer(demo->device, &cmd, &demo->cmd);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600400 assert(!err);
401
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600402 VkCmdBufferBeginInfo cmd_buf_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600403 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600404 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -0600405 .flags = VK_CMD_BUFFER_OPTIMIZE_SMALL_BATCH_BIT |
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600406 VK_CMD_BUFFER_OPTIMIZE_ONE_TIME_SUBMIT_BIT,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600407 };
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600408 err = vkBeginCommandBuffer(demo->cmd, &cmd_buf_info);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600409 }
410
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600411 VkImageMemoryBarrier image_memory_barrier = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600412 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600413 .pNext = NULL,
414 .outputMask = 0,
415 .inputMask = 0,
416 .oldLayout = old_image_layout,
417 .newLayout = new_image_layout,
418 .image = image,
malnasseeb25d3f2015-06-03 17:28:38 -0400419 .subresourceRange = { aspect, 0, 1, 0, 0 }
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600420 };
421
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600422 if (new_image_layout == VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL) {
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600423 /* Make sure anything that was copying from this image has completed */
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -0600424 image_memory_barrier.inputMask = VK_MEMORY_INPUT_TRANSFER_BIT;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600425 }
426
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600427 if (new_image_layout == VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL) {
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600428 /* Make sure any Copy or CPU writes to image are flushed */
Courtney Goeltzenleuchter2bda8332015-04-29 17:16:21 -0600429 image_memory_barrier.outputMask = VK_MEMORY_OUTPUT_HOST_WRITE_BIT | VK_MEMORY_OUTPUT_TRANSFER_BIT;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600430 }
431
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600432 VkImageMemoryBarrier *pmemory_barrier = &image_memory_barrier;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600433
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600434 VkPipeEvent set_events[] = { VK_PIPE_EVENT_TOP_OF_PIPE };
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600435
Tony Barbour72304ef2015-04-16 15:59:00 -0600436 vkCmdPipelineBarrier(demo->cmd, VK_WAIT_EVENT_TOP_OF_PIPE, 1, set_events, 1, (const void **)&pmemory_barrier);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600437}
438
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600439static void demo_draw_build_cmd(struct demo *demo, VkCmdBuffer cmd_buf)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600440{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600441 const VkColorAttachmentBindInfo color_attachment = {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600442 .view = demo->buffers[demo->current_buffer].view,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600443 .layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600444 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600445 const VkDepthStencilBindInfo depth_stencil = {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600446 .view = demo->depth.view,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600447 .layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600448 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600449 const VkClearColor clear_color = {
Courtney Goeltzenleuchter374553c2015-04-03 16:35:32 -0600450 .color.floatColor = { 0.2f, 0.2f, 0.2f, 0.2f },
451 .useRawValue = false,
452 };
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600453 const float clear_depth = 1.0f;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600454 VkImageSubresourceRange clear_range;
455 VkCmdBufferBeginInfo cmd_buf_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600456 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO,
Courtney Goeltzenleuchter53968d82015-04-03 15:25:24 -0600457 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -0600458 .flags = VK_CMD_BUFFER_OPTIMIZE_SMALL_BATCH_BIT |
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600459 VK_CMD_BUFFER_OPTIMIZE_ONE_TIME_SUBMIT_BIT,
Jon Ashburn60ccfbe2014-12-31 17:08:35 -0700460 };
Tony Barbourfdc2d352015-04-22 09:02:32 -0600461 VkResult U_ASSERT_ONLY err;
Chris Forbes40a71562015-06-17 11:36:12 +1200462 VkAttachmentLoadOp load_op = VK_ATTACHMENT_LOAD_OP_CLEAR;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600463 VkAttachmentStoreOp store_op = VK_ATTACHMENT_STORE_OP_DONT_CARE;
464 const VkFramebufferCreateInfo fb_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600465 .sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO,
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700466 .pNext = NULL,
467 .colorAttachmentCount = 1,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600468 .pColorAttachments = (VkColorAttachmentBindInfo*) &color_attachment,
469 .pDepthStencilAttachment = (VkDepthStencilBindInfo*) &depth_stencil,
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700470 .sampleCount = 1,
Mark Lobodzinskic06b7412015-01-27 13:24:03 -0600471 .width = demo->width,
472 .height = demo->height,
473 .layers = 1,
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700474 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600475 VkRenderPassCreateInfo rp_info;
476 VkRenderPassBegin rp_begin;
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700477
478 memset(&rp_info, 0 , sizeof(rp_info));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600479 err = vkCreateFramebuffer(demo->device, &fb_info, &rp_begin.framebuffer);
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700480 assert(!err);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600481 rp_info.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO;
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700482 rp_info.renderArea.extent.width = demo->width;
483 rp_info.renderArea.extent.height = demo->height;
Courtney Goeltzenleuchter53968d82015-04-03 15:25:24 -0600484 rp_info.colorAttachmentCount = fb_info.colorAttachmentCount;
485 rp_info.pColorFormats = &demo->format;
486 rp_info.pColorLayouts = &color_attachment.layout;
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700487 rp_info.pColorLoadOps = &load_op;
488 rp_info.pColorStoreOps = &store_op;
Courtney Goeltzenleuchter53968d82015-04-03 15:25:24 -0600489 rp_info.pColorLoadClearValues = &clear_color;
Tony Barbour72304ef2015-04-16 15:59:00 -0600490 rp_info.depthStencilFormat = VK_FORMAT_D16_UNORM;
Courtney Goeltzenleuchter53968d82015-04-03 15:25:24 -0600491 rp_info.depthStencilLayout = depth_stencil.layout;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600492 rp_info.depthLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
Courtney Goeltzenleuchter53968d82015-04-03 15:25:24 -0600493 rp_info.depthLoadClearValue = clear_depth;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600494 rp_info.depthStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
495 rp_info.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
Courtney Goeltzenleuchter53968d82015-04-03 15:25:24 -0600496 rp_info.stencilLoadClearValue = 0;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600497 rp_info.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
498 err = vkCreateRenderPass(demo->device, &rp_info, &rp_begin.renderPass);
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700499 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600500
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600501 err = vkBeginCommandBuffer(cmd_buf, &cmd_buf_info);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600502 assert(!err);
503
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600504 vkCmdBindPipeline(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600505 demo->pipeline);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600506 vkCmdBindDescriptorSets(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS,
Cody Northropfb5185a2015-04-16 13:41:56 -0600507 0, 1, &demo->desc_set, 0, NULL);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600508
Tony Barbour72304ef2015-04-16 15:59:00 -0600509 vkCmdBindDynamicStateObject(cmd_buf, VK_STATE_BIND_POINT_VIEWPORT, demo->viewport);
510 vkCmdBindDynamicStateObject(cmd_buf, VK_STATE_BIND_POINT_RASTER, demo->raster);
511 vkCmdBindDynamicStateObject(cmd_buf, VK_STATE_BIND_POINT_COLOR_BLEND,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600512 demo->color_blend);
Tony Barbour72304ef2015-04-16 15:59:00 -0600513 vkCmdBindDynamicStateObject(cmd_buf, VK_STATE_BIND_POINT_DEPTH_STENCIL,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600514 demo->depth_stencil);
515
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600516 vkCmdBeginRenderPass(cmd_buf, &rp_begin);
Chris Forbes40a71562015-06-17 11:36:12 +1200517 clear_range.aspect = VK_IMAGE_ASPECT_DEPTH;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600518 clear_range.baseMipLevel = 0;
519 clear_range.mipLevels = 1;
520 clear_range.baseArraySlice = 0;
521 clear_range.arraySize = 1;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600522
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600523 vkCmdClearDepthStencil(cmd_buf, demo->depth.image,
524 VK_IMAGE_LAYOUT_CLEAR_OPTIMAL,
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600525 clear_depth, 0, 1, &clear_range);
526
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600527 vkCmdDraw(cmd_buf, 0, 12 * 3, 0, 1);
528 vkCmdEndRenderPass(cmd_buf, rp_begin.renderPass);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600529
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600530 err = vkEndCommandBuffer(cmd_buf);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600531 assert(!err);
Courtney Goeltzenleuchter94901dc2015-02-25 17:53:18 -0700532
Mike Stroyanebae8322015-04-17 12:36:38 -0600533 vkDestroyObject(demo->device, VK_OBJECT_TYPE_RENDER_PASS, rp_begin.renderPass);
534 vkDestroyObject(demo->device, VK_OBJECT_TYPE_FRAMEBUFFER, rp_begin.framebuffer);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600535}
536
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600537
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600538void demo_update_data_buffer(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600539{
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600540 mat4x4 MVP, Model, VP;
541 int matrixSize = sizeof(MVP);
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600542 uint8_t *pData;
Tony Barbourfdc2d352015-04-22 09:02:32 -0600543 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600544
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600545 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600546
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600547 // Rotate 22.5 degrees around the Y axis
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600548 mat4x4_dup(Model, demo->model_matrix);
Piers Daniell735ee532015-02-23 16:23:13 -0700549 mat4x4_rotate(demo->model_matrix, Model, 0.0f, 1.0f, 0.0f, (float)degreesToRadians(demo->spin_angle));
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600550 mat4x4_mul(MVP, VP, demo->model_matrix);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600551
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500552 err = vkMapMemory(demo->device, demo->uniform_data.mem, 0, 0, 0, (void **) &pData);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600553 assert(!err);
554
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600555 memcpy(pData, (const void*) &MVP[0][0], matrixSize);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600556
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500557 err = vkUnmapMemory(demo->device, demo->uniform_data.mem);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600558 assert(!err);
559}
560
561static void demo_draw(struct demo *demo)
562{
Chia-I Wucbb564e2015-04-16 22:02:10 +0800563 const VkPresentInfoWSI present = {
564 .sType = VK_STRUCTURE_TYPE_PRESENT_INFO_WSI,
565 .pNext = NULL,
566 .image = demo->buffers[demo->current_buffer].image,
567 .flipInterval = 0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600568 };
Tony Barbourfdc2d352015-04-22 09:02:32 -0600569 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600570
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600571 err = vkQueueSubmit(demo->queue, 1, &demo->buffers[demo->current_buffer].cmd,
572 VK_NULL_HANDLE);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600573 assert(!err);
574
Jon Ashburn0b85d052015-05-21 18:13:33 -0600575 err = demo->fpQueuePresentWSI(demo->queue, &present);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600576 assert(!err);
577
578 demo->current_buffer = (demo->current_buffer + 1) % DEMO_BUFFER_COUNT;
Chia-I Wucbb564e2015-04-16 22:02:10 +0800579
580 err = vkQueueWaitIdle(demo->queue);
581 assert(err == VK_SUCCESS);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600582}
583
584static void demo_prepare_buffers(struct demo *demo)
585{
Chia-I Wucbb564e2015-04-16 22:02:10 +0800586 const VkSwapChainCreateInfoWSI swap_chain = {
587 .sType = VK_STRUCTURE_TYPE_SWAP_CHAIN_CREATE_INFO_WSI,
588 .pNext = NULL,
589 .pNativeWindowSystemHandle = demo->connection,
590 .pNativeWindowHandle = (void *) (intptr_t) demo->window,
Ian Elliotte4602cd2015-04-21 16:41:02 -0600591 .displayCount = 1,
Chia-I Wucbb564e2015-04-16 22:02:10 +0800592 .imageCount = DEMO_BUFFER_COUNT,
593 .imageFormat = demo->format,
594 .imageExtent = {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600595 .width = demo->width,
596 .height = demo->height,
597 },
Chia-I Wucbb564e2015-04-16 22:02:10 +0800598 .imageArraySize = 1,
599 .imageUsageFlags = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600600 };
Chia-I Wucbb564e2015-04-16 22:02:10 +0800601 VkSwapChainImageInfoWSI images[DEMO_BUFFER_COUNT];
602 size_t images_size = sizeof(images);
Tony Barbourfdc2d352015-04-22 09:02:32 -0600603 VkResult U_ASSERT_ONLY err;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600604 uint32_t i;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600605
Jon Ashburn0b85d052015-05-21 18:13:33 -0600606 err = demo->fpCreateSwapChainWSI(demo->device, &swap_chain, &demo->swap_chain);
Chia-I Wucbb564e2015-04-16 22:02:10 +0800607 assert(!err);
608
Jon Ashburn0b85d052015-05-21 18:13:33 -0600609 err = demo->fpGetSwapChainInfoWSI(demo->swap_chain,
Chia-I Wucbb564e2015-04-16 22:02:10 +0800610 VK_SWAP_CHAIN_INFO_TYPE_PERSISTENT_IMAGES_WSI,
611 &images_size, images);
612 assert(!err && images_size == sizeof(images));
613
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600614 for (i = 0; i < DEMO_BUFFER_COUNT; i++) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600615 VkColorAttachmentViewCreateInfo color_attachment_view = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600616 .sType = VK_STRUCTURE_TYPE_COLOR_ATTACHMENT_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600617 .pNext = NULL,
618 .format = demo->format,
619 .mipLevel = 0,
620 .baseArraySlice = 0,
621 .arraySize = 1,
622 };
623
Chia-I Wucbb564e2015-04-16 22:02:10 +0800624 demo->buffers[i].image = images[i].image;
625 demo->buffers[i].mem = images[i].memory;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600626
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600627 demo_set_image_layout(demo, demo->buffers[i].image,
malnasseeb25d3f2015-06-03 17:28:38 -0400628 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600629 VK_IMAGE_LAYOUT_UNDEFINED,
630 VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600631
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600632 color_attachment_view.image = demo->buffers[i].image;
633
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600634 err = vkCreateColorAttachmentView(demo->device,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600635 &color_attachment_view, &demo->buffers[i].view);
636 assert(!err);
637 }
638}
639
640static void demo_prepare_depth(struct demo *demo)
641{
Tony Barbour72304ef2015-04-16 15:59:00 -0600642 const VkFormat depth_format = VK_FORMAT_D16_UNORM;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600643 const VkImageCreateInfo image = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600644 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600645 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -0600646 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600647 .format = depth_format,
648 .extent = { demo->width, demo->height, 1 },
649 .mipLevels = 1,
650 .arraySize = 1,
651 .samples = 1,
Tony Barbour72304ef2015-04-16 15:59:00 -0600652 .tiling = VK_IMAGE_TILING_OPTIMAL,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600653 .usage = VK_IMAGE_USAGE_DEPTH_STENCIL_BIT,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600654 .flags = 0,
655 };
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -0600656 VkMemoryAllocInfo mem_alloc = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600657 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
Mark Lobodzinskia6907f72015-04-16 08:52:00 -0500658 .pNext = NULL,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600659 .allocationSize = 0,
Tony Barbour72304ef2015-04-16 15:59:00 -0600660 .memProps = VK_MEMORY_PROPERTY_DEVICE_ONLY,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600661 .memPriority = VK_MEMORY_PRIORITY_NORMAL,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600662 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600663 VkDepthStencilViewCreateInfo view = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600664 .sType = VK_STRUCTURE_TYPE_DEPTH_STENCIL_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600665 .pNext = NULL,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600666 .image = VK_NULL_HANDLE,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600667 .mipLevel = 0,
668 .baseArraySlice = 0,
669 .arraySize = 1,
670 .flags = 0,
671 };
Mike Stroyanebae8322015-04-17 12:36:38 -0600672
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500673 VkMemoryRequirements mem_reqs;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600674 size_t mem_reqs_size = sizeof(VkMemoryRequirements);
Tony Barbourfdc2d352015-04-22 09:02:32 -0600675 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600676
677 demo->depth.format = depth_format;
678
679 /* create image */
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600680 err = vkCreateImage(demo->device, &image,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600681 &demo->depth.image);
682 assert(!err);
683
Mike Stroyanebae8322015-04-17 12:36:38 -0600684 err = vkGetObjectInfo(demo->device,
685 VK_OBJECT_TYPE_IMAGE, demo->depth.image,
Tony Barbour72304ef2015-04-16 15:59:00 -0600686 VK_OBJECT_INFO_TYPE_MEMORY_REQUIREMENTS,
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500687 &mem_reqs_size, &mem_reqs);
688 mem_alloc.allocationSize = mem_reqs.size;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600689
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500690 /* allocate memory */
691 err = vkAllocMemory(demo->device, &mem_alloc, &demo->depth.mem);
692 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600693
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500694 /* bind memory */
695 err = vkBindObjectMemory(demo->device,
696 VK_OBJECT_TYPE_IMAGE, demo->depth.image,
697 demo->depth.mem, 0);
698 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600699
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600700 demo_set_image_layout(demo, demo->depth.image,
malnasseeb25d3f2015-06-03 17:28:38 -0400701 VK_IMAGE_ASPECT_DEPTH,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600702 VK_IMAGE_LAYOUT_UNDEFINED,
703 VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600704
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600705 /* create image view */
706 view.image = demo->depth.image;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600707 err = vkCreateDepthStencilView(demo->device, &view,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600708 &demo->depth.view);
709 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600710}
711
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600712/** loadTexture
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -0600713 * loads a png file into an memory object, using cstdio , libpng.
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600714 *
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -0600715 * \param demo : Needed to access VK calls
716 * \param filename : the png file to be loaded
717 * \param width : width of png, to be updated as a side effect of this function
718 * \param height : height of png, to be updated as a side effect of this function
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600719 *
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -0600720 * \return bool : an opengl texture id. true if successful?,
721 * should be validated by the client of this function.
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600722 *
723 * Source: http://en.wikibooks.org/wiki/OpenGL_Programming/Intermediate/Textures
724 * Modified to copy image to memory
725 *
726 */
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700727bool loadTexture(const char *filename, uint8_t *rgba_data,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600728 VkSubresourceLayout *layout,
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600729 int32_t *width, int32_t *height)
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600730{
731 //header for testing if it is a png
732 png_byte header[8];
Tony Barbourf9921732015-04-22 11:36:22 -0600733 int is_png, bit_depth, color_type, rowbytes;
734 size_t retval;
Ian Elliott1e42dff2015-02-13 14:29:21 -0700735 png_uint_32 i, twidth, theight;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600736 png_structp png_ptr;
737 png_infop info_ptr, end_info;
738 png_byte *image_data;
739 png_bytep *row_pointers;
740
741 //open file as binary
742 FILE *fp = fopen(filename, "rb");
743 if (!fp) {
744 return false;
745 }
746
747 //read the header
Tony Barbourfdc2d352015-04-22 09:02:32 -0600748 retval = fread(header, 1, 8, fp);
749 if (retval != 8) {
750 fclose(fp);
751 return false;
752 }
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600753
754 //test if png
755 is_png = !png_sig_cmp(header, 0, 8);
756 if (!is_png) {
757 fclose(fp);
758 return false;
759 }
760
761 //create png struct
762 png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL,
763 NULL, NULL);
764 if (!png_ptr) {
765 fclose(fp);
766 return (false);
767 }
768
769 //create png info struct
770 info_ptr = png_create_info_struct(png_ptr);
771 if (!info_ptr) {
772 png_destroy_read_struct(&png_ptr, (png_infopp) NULL, (png_infopp) NULL);
773 fclose(fp);
774 return (false);
775 }
776
777 //create png info struct
778 end_info = png_create_info_struct(png_ptr);
779 if (!end_info) {
780 png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp) NULL);
781 fclose(fp);
782 return (false);
783 }
784
785 //png error stuff, not sure libpng man suggests this.
786 if (setjmp(png_jmpbuf(png_ptr))) {
787 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
788 fclose(fp);
789 return (false);
790 }
791
792 //init png reading
793 png_init_io(png_ptr, fp);
794
795 //let libpng know you already read the first 8 bytes
796 png_set_sig_bytes(png_ptr, 8);
797
798 // read all the info up to the image data
799 png_read_info(png_ptr, info_ptr);
800
801 // get info about png
802 png_get_IHDR(png_ptr, info_ptr, &twidth, &theight, &bit_depth, &color_type,
803 NULL, NULL, NULL);
804
805 //update width and height based on png info
806 *width = twidth;
807 *height = theight;
808
809 // Require that incoming texture be 8bits per color component
810 // and 4 components (RGBA).
811 if (png_get_bit_depth(png_ptr, info_ptr) != 8 ||
812 png_get_channels(png_ptr, info_ptr) != 4) {
813 return false;
814 }
815
816 if (rgba_data == NULL) {
817 // If data pointer is null, we just want the width & height
818 // clean up memory and close stuff
819 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
820 fclose(fp);
821
822 return true;
823 }
824
825 // Update the png info struct.
826 png_read_update_info(png_ptr, info_ptr);
827
828 // Row size in bytes.
829 rowbytes = png_get_rowbytes(png_ptr, info_ptr);
830
831 // Allocate the image_data as a big block, to be given to opengl
832 image_data = (png_byte *)malloc(rowbytes * theight * sizeof(png_byte));
833 if (!image_data) {
834 //clean up memory and close stuff
835 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
836 fclose(fp);
837 return false;
838 }
839
840 // row_pointers is for pointing to image_data for reading the png with libpng
841 row_pointers = (png_bytep *)malloc(theight * sizeof(png_bytep));
842 if (!row_pointers) {
843 //clean up memory and close stuff
844 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
845 // delete[] image_data;
846 fclose(fp);
847 return false;
848 }
849 // set the individual row_pointers to point at the correct offsets of image_data
850 for (i = 0; i < theight; ++i)
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700851 row_pointers[theight - 1 - i] = rgba_data + i * layout->rowPitch;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600852
853 // read the png into image_data through row_pointers
854 png_read_image(png_ptr, row_pointers);
855
856 // clean up memory and close stuff
857 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
858 free(row_pointers);
859 free(image_data);
860 fclose(fp);
861
862 return true;
863}
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600864
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700865static void demo_prepare_texture_image(struct demo *demo,
866 const char *filename,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600867 struct texture_object *tex_obj,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600868 VkImageTiling tiling,
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -0600869 VkImageUsageFlags usage,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600870 VkFlags mem_props)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600871{
Mike Stroyan8b89e072015-06-15 14:21:03 -0600872 const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600873 int32_t tex_width;
874 int32_t tex_height;
Tony Barbourfdc2d352015-04-22 09:02:32 -0600875 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700876
David Pinedo30bd71d2015-04-23 08:16:57 -0600877 if (!loadTexture(filename, NULL, NULL, &tex_width, &tex_height))
878 {
879 printf("Failed to load textures\n");
880 fflush(stdout);
881 exit(1);
882 }
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700883
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600884 tex_obj->tex_width = tex_width;
885 tex_obj->tex_height = tex_height;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700886
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600887 const VkImageCreateInfo image_create_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600888 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700889 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -0600890 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700891 .format = tex_format,
892 .extent = { tex_width, tex_height, 1 },
893 .mipLevels = 1,
894 .arraySize = 1,
895 .samples = 1,
896 .tiling = tiling,
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -0600897 .usage = usage,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700898 .flags = 0,
899 };
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -0600900 VkMemoryAllocInfo mem_alloc = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600901 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
Mark Lobodzinskia6907f72015-04-16 08:52:00 -0500902 .pNext = NULL,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700903 .allocationSize = 0,
904 .memProps = mem_props,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600905 .memPriority = VK_MEMORY_PRIORITY_NORMAL,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700906 };
907
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500908 VkMemoryRequirements mem_reqs;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600909 size_t mem_reqs_size = sizeof(VkMemoryRequirements);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700910
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600911 err = vkCreateImage(demo->device, &image_create_info,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600912 &tex_obj->image);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700913 assert(!err);
914
Mike Stroyanebae8322015-04-17 12:36:38 -0600915 err = vkGetObjectInfo(demo->device,
916 VK_OBJECT_TYPE_IMAGE, tex_obj->image,
Tony Barbour72304ef2015-04-16 15:59:00 -0600917 VK_OBJECT_INFO_TYPE_MEMORY_REQUIREMENTS,
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500918 &mem_reqs_size, &mem_reqs);
919 assert(!err && mem_reqs_size == sizeof(VkMemoryRequirements));
Piers Daniell735ee532015-02-23 16:23:13 -0700920
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500921 mem_alloc.allocationSize = mem_reqs.size;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700922
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500923 /* allocate memory */
924 err = vkAllocMemory(demo->device, &mem_alloc,
925 &(tex_obj->mem));
926 assert(!err);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700927
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500928 /* bind memory */
929 err = vkBindObjectMemory(demo->device,
930 VK_OBJECT_TYPE_IMAGE, tex_obj->image,
931 tex_obj->mem, 0);
932 assert(!err);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700933
Tony Barbour72304ef2015-04-16 15:59:00 -0600934 if (mem_props & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600935 const VkImageSubresource subres = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600936 .aspect = VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700937 .mipLevel = 0,
938 .arraySlice = 0,
939 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600940 VkSubresourceLayout layout;
941 size_t layout_size = sizeof(VkSubresourceLayout);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700942 void *data;
943
Mike Stroyanebae8322015-04-17 12:36:38 -0600944 err = vkGetImageSubresourceInfo(demo->device, tex_obj->image, &subres,
Tony Barbour72304ef2015-04-16 15:59:00 -0600945 VK_SUBRESOURCE_INFO_TYPE_LAYOUT,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700946 &layout_size, &layout);
947 assert(!err && layout_size == sizeof(layout));
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700948
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500949 err = vkMapMemory(demo->device, tex_obj->mem, 0, 0, 0, &data);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700950 assert(!err);
951
952 if (!loadTexture(filename, data, &layout, &tex_width, &tex_height)) {
953 fprintf(stderr, "Error loading texture: %s\n", filename);
954 }
955
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500956 err = vkUnmapMemory(demo->device, tex_obj->mem);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700957 assert(!err);
958 }
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600959
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600960 tex_obj->imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600961 demo_set_image_layout(demo, tex_obj->image,
malnasseeb25d3f2015-06-03 17:28:38 -0400962 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600963 VK_IMAGE_LAYOUT_UNDEFINED,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600964 tex_obj->imageLayout);
965 /* 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 -0700966}
967
Mark Lobodzinskida9b1092015-04-16 11:44:05 -0500968static void demo_destroy_texture_image(struct demo *demo, struct texture_object *tex_objs)
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700969{
970 /* clean up staging resources */
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -0500971 vkFreeMemory(demo->device, tex_objs->mem);
Mike Stroyanebae8322015-04-17 12:36:38 -0600972 vkDestroyObject(demo->device, VK_OBJECT_TYPE_IMAGE, tex_objs->image);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700973}
974
975static void demo_prepare_textures(struct demo *demo)
976{
Tony Barbour72304ef2015-04-16 15:59:00 -0600977 const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600978 VkFormatProperties props;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700979 size_t size = sizeof(props);
Tony Barbourfdc2d352015-04-22 09:02:32 -0600980 VkResult U_ASSERT_ONLY err;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600981 uint32_t i;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600982
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600983 err = vkGetFormatInfo(demo->device, tex_format,
Tony Barbour72304ef2015-04-16 15:59:00 -0600984 VK_FORMAT_INFO_TYPE_PROPERTIES,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700985 &size, &props);
986 assert(!err);
987
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600988 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700989
FslNopper6a7e50e2015-05-06 21:42:01 +0200990 if ((props.linearTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) && !demo->use_staging_buffer) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700991 /* Device can texture using linear textures */
992 demo_prepare_texture_image(demo, tex_files[i], &demo->textures[i],
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -0600993 VK_IMAGE_TILING_LINEAR, VK_IMAGE_USAGE_SAMPLED_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
Tony Barbour72304ef2015-04-16 15:59:00 -0600994 } else if (props.optimalTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700995 /* Must use staging buffer to copy linear texture to optimized */
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600996 struct texture_object staging_texture;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700997
998 memset(&staging_texture, 0, sizeof(staging_texture));
999 demo_prepare_texture_image(demo, tex_files[i], &staging_texture,
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001000 VK_IMAGE_TILING_LINEAR, VK_IMAGE_USAGE_TRANSFER_SOURCE_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001001
1002 demo_prepare_texture_image(demo, tex_files[i], &demo->textures[i],
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001003 VK_IMAGE_TILING_OPTIMAL,
1004 (VK_IMAGE_USAGE_TRANSFER_DESTINATION_BIT | VK_IMAGE_USAGE_SAMPLED_BIT),
1005 VK_MEMORY_PROPERTY_DEVICE_ONLY);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001006
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001007 demo_set_image_layout(demo, staging_texture.image,
malnasseeb25d3f2015-06-03 17:28:38 -04001008 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001009 staging_texture.imageLayout,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001010 VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001011
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001012 demo_set_image_layout(demo, demo->textures[i].image,
malnasseeb25d3f2015-06-03 17:28:38 -04001013 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001014 demo->textures[i].imageLayout,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001015 VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001016
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001017 VkImageCopy copy_region = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001018 .srcSubresource = { VK_IMAGE_ASPECT_COLOR, 0, 0 },
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001019 .srcOffset = { 0, 0, 0 },
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001020 .destSubresource = { VK_IMAGE_ASPECT_COLOR, 0, 0 },
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001021 .destOffset = { 0, 0, 0 },
1022 .extent = { staging_texture.tex_width, staging_texture.tex_height, 1 },
1023 };
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001024 vkCmdCopyImage(demo->cmd,
1025 staging_texture.image, VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL,
1026 demo->textures[i].image, VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL,
Courtney Goeltzenleuchter8e89a312015-03-25 11:25:10 -06001027 1, &copy_region);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001028
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001029 demo_set_image_layout(demo, demo->textures[i].image,
malnasseeb25d3f2015-06-03 17:28:38 -04001030 VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001031 VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001032 demo->textures[i].imageLayout);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001033
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001034 demo_flush_init_cmd(demo);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001035
Courtney Goeltzenleuchterf3aeb2b2015-04-21 09:30:03 -06001036 demo_destroy_texture_image(demo, &staging_texture);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001037 } else {
Mike Stroyan8b89e072015-06-15 14:21:03 -06001038 /* Can't support VK_FORMAT_R8G8B8A8_UNORM !? */
1039 assert(!"No support for R8G8B8A8_UNORM as texture image format");
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001040 }
1041
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001042 const VkSamplerCreateInfo sampler = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001043 .sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001044 .pNext = NULL,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001045 .magFilter = VK_TEX_FILTER_NEAREST,
1046 .minFilter = VK_TEX_FILTER_NEAREST,
Tony Barbour72304ef2015-04-16 15:59:00 -06001047 .mipMode = VK_TEX_MIPMAP_MODE_BASE,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001048 .addressU = VK_TEX_ADDRESS_CLAMP,
1049 .addressV = VK_TEX_ADDRESS_CLAMP,
1050 .addressW = VK_TEX_ADDRESS_CLAMP,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001051 .mipLodBias = 0.0f,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001052 .maxAnisotropy = 1,
Tony Barbour72304ef2015-04-16 15:59:00 -06001053 .compareOp = VK_COMPARE_OP_NEVER,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001054 .minLod = 0.0f,
1055 .maxLod = 0.0f,
Tony Barbour72304ef2015-04-16 15:59:00 -06001056 .borderColor = VK_BORDER_COLOR_OPAQUE_WHITE,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001057 };
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001058
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001059 VkImageViewCreateInfo view = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001060 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001061 .pNext = NULL,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001062 .image = VK_NULL_HANDLE,
Tony Barbour72304ef2015-04-16 15:59:00 -06001063 .viewType = VK_IMAGE_VIEW_TYPE_2D,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001064 .format = tex_format,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001065 .channels = { VK_CHANNEL_SWIZZLE_R,
1066 VK_CHANNEL_SWIZZLE_G,
1067 VK_CHANNEL_SWIZZLE_B,
1068 VK_CHANNEL_SWIZZLE_A, },
1069 .subresourceRange = { VK_IMAGE_ASPECT_COLOR, 0, 1, 0, 1 },
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001070 .minLod = 0.0f,
1071 };
Jon Ashburnb2a66652015-01-16 09:37:43 -07001072
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001073 /* create sampler */
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001074 err = vkCreateSampler(demo->device, &sampler,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001075 &demo->textures[i].sampler);
1076 assert(!err);
1077
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001078 /* create image view */
1079 view.image = demo->textures[i].image;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001080 err = vkCreateImageView(demo->device, &view,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001081 &demo->textures[i].view);
1082 assert(!err);
1083 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001084}
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001085
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001086void demo_prepare_cube_data_buffer(struct demo *demo)
1087{
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -06001088 VkBufferCreateInfo buf_info;
1089 VkBufferViewCreateInfo view_info;
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -06001090 VkMemoryAllocInfo alloc_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001091 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
Mark Lobodzinskia6907f72015-04-16 08:52:00 -05001092 .pNext = NULL,
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001093 .allocationSize = 0,
Tony Barbour72304ef2015-04-16 15:59:00 -06001094 .memProps = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001095 .memPriority = VK_MEMORY_PRIORITY_NORMAL,
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001096 };
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001097 VkMemoryRequirements mem_reqs;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001098 size_t mem_reqs_size = sizeof(VkMemoryRequirements);
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001099 uint8_t *pData;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001100 int i;
1101 mat4x4 MVP, VP;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001102 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001103 struct vktexcube_vs_uniform data;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001104
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001105 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001106 mat4x4_mul(MVP, VP, demo->model_matrix);
1107 memcpy(data.mvp, MVP, sizeof(MVP));
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001108// dumpMatrix("MVP", MVP);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001109
1110 for (i=0; i<12*3; i++) {
1111 data.position[i][0] = g_vertex_buffer_data[i*3];
1112 data.position[i][1] = g_vertex_buffer_data[i*3+1];
1113 data.position[i][2] = g_vertex_buffer_data[i*3+2];
1114 data.position[i][3] = 1.0f;
1115 data.attr[i][0] = g_uv_buffer_data[2*i];
1116 data.attr[i][1] = g_uv_buffer_data[2*i + 1];
1117 data.attr[i][2] = 0;
1118 data.attr[i][3] = 0;
1119 }
1120
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001121 memset(&buf_info, 0, sizeof(buf_info));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001122 buf_info.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001123 buf_info.size = sizeof(data);
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -06001124 buf_info.usage = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001125 err = vkCreateBuffer(demo->device, &buf_info, &demo->uniform_data.buf);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001126 assert(!err);
1127
Mike Stroyanebae8322015-04-17 12:36:38 -06001128 err = vkGetObjectInfo(demo->device,
Mike Stroyanebae8322015-04-17 12:36:38 -06001129 VK_OBJECT_TYPE_BUFFER, demo->uniform_data.buf,
Tony Barbour72304ef2015-04-16 15:59:00 -06001130 VK_OBJECT_INFO_TYPE_MEMORY_REQUIREMENTS,
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001131 &mem_reqs_size, &mem_reqs);
1132 assert(!err && mem_reqs_size == sizeof(mem_reqs));
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001133
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001134 alloc_info.allocationSize = mem_reqs.size;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001135
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001136 err = vkAllocMemory(demo->device, &alloc_info, &(demo->uniform_data.mem));
1137 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001138
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001139 err = vkMapMemory(demo->device, demo->uniform_data.mem, 0, 0, 0, (void **) &pData);
1140 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001141
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001142 memcpy(pData, &data, sizeof data);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001143
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05001144 err = vkUnmapMemory(demo->device, demo->uniform_data.mem);
1145 assert(!err);
1146
1147 err = vkBindObjectMemory(demo->device,
1148 VK_OBJECT_TYPE_BUFFER, demo->uniform_data.buf,
1149 demo->uniform_data.mem, 0);
1150 assert(!err);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001151
1152 memset(&view_info, 0, sizeof(view_info));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001153 view_info.sType = VK_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO;
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001154 view_info.buffer = demo->uniform_data.buf;
Tony Barbour72304ef2015-04-16 15:59:00 -06001155 view_info.viewType = VK_BUFFER_VIEW_TYPE_RAW;
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001156 view_info.offset = 0;
1157 view_info.range = sizeof(data);
1158
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001159 err = vkCreateBufferView(demo->device, &view_info, &demo->uniform_data.view);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001160 assert(!err);
1161
Chia-I Wuae721ba2015-05-25 16:27:55 +08001162 demo->uniform_data.desc.bufferView = demo->uniform_data.view;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001163}
1164
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001165static void demo_prepare_descriptor_layout(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001166{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001167 const VkDescriptorSetLayoutBinding layout_bindings[2] = {
Chia-I Wua2aa8632015-03-26 15:04:41 +08001168 [0] = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001169 .descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
Chia-I Wu6d29a412015-05-25 16:22:52 +08001170 .arraySize = 1,
Tony Barbour72304ef2015-04-16 15:59:00 -06001171 .stageFlags = VK_SHADER_STAGE_VERTEX_BIT,
Chia-I Wu91e8e212015-03-27 12:56:09 +08001172 .pImmutableSamplers = NULL,
Chia-I Wua2aa8632015-03-26 15:04:41 +08001173 },
1174 [1] = {
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -06001175 .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
Chia-I Wu6d29a412015-05-25 16:22:52 +08001176 .arraySize = DEMO_TEXTURE_COUNT,
Tony Barbour72304ef2015-04-16 15:59:00 -06001177 .stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT,
Chia-I Wu91e8e212015-03-27 12:56:09 +08001178 .pImmutableSamplers = NULL,
Chia-I Wua2aa8632015-03-26 15:04:41 +08001179 },
1180 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001181 const VkDescriptorSetLayoutCreateInfo descriptor_layout = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001182 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001183 .pNext = NULL,
Chia-I Wua2aa8632015-03-26 15:04:41 +08001184 .count = 2,
1185 .pBinding = layout_bindings,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001186 };
Tony Barbourfdc2d352015-04-22 09:02:32 -06001187 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001188
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001189 err = vkCreateDescriptorSetLayout(demo->device,
Chia-I Wub58c24a2015-03-26 15:27:55 +08001190 &descriptor_layout, &demo->desc_layout);
1191 assert(!err);
1192
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001193 const VkPipelineLayoutCreateInfo pPipelineLayoutCreateInfo = {
1194 .sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
1195 .pNext = NULL,
1196 .descriptorSetCount = 1,
1197 .pSetLayouts = &demo->desc_layout,
1198 };
1199
1200 err = vkCreatePipelineLayout(demo->device,
1201 &pPipelineLayoutCreateInfo,
1202 &demo->pipeline_layout);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001203 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001204}
1205
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001206static VkShader demo_prepare_shader(struct demo *demo,
Tony Barbour72304ef2015-04-16 15:59:00 -06001207 VkShaderStage stage,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001208 const void *code,
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001209 size_t size)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001210{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001211 VkShaderCreateInfo createInfo;
1212 VkShader shader;
1213 VkResult err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001214
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001215
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001216 createInfo.sType = VK_STRUCTURE_TYPE_SHADER_CREATE_INFO;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001217 createInfo.pNext = NULL;
1218
Cody Northrop1fedb212015-05-28 11:27:16 -06001219 if (!demo->use_glsl) {
1220 createInfo.codeSize = size;
1221 createInfo.pCode = code;
1222 createInfo.flags = 0;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001223
Cody Northrop1fedb212015-05-28 11:27:16 -06001224 err = vkCreateShader(demo->device, &createInfo, &shader);
1225 if (err) {
1226 free((void *) createInfo.pCode);
1227 }
1228 } else {
1229 // Create fake SPV structure to feed GLSL
1230 // to the driver "under the covers"
1231 createInfo.codeSize = 3 * sizeof(uint32_t) + size + 1;
1232 createInfo.pCode = malloc(createInfo.codeSize);
1233 createInfo.flags = 0;
1234
1235 /* try version 0 first: VkShaderStage followed by GLSL */
1236 ((uint32_t *) createInfo.pCode)[0] = ICD_SPV_MAGIC;
1237 ((uint32_t *) createInfo.pCode)[1] = 0;
1238 ((uint32_t *) createInfo.pCode)[2] = stage;
1239 memcpy(((uint32_t *) createInfo.pCode + 3), code, size + 1);
1240
1241 err = vkCreateShader(demo->device, &createInfo, &shader);
1242 if (err) {
1243 free((void *) createInfo.pCode);
1244 return (VkShader) VK_NULL_HANDLE;
1245 }
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001246 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001247 return shader;
1248}
1249
Cody Northrop8b12a1f2015-03-17 15:55:58 -06001250char *demo_read_spv(const char *filename, size_t *psize)
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001251{
1252 long int size;
Tony Barbourf9921732015-04-22 11:36:22 -06001253 size_t U_ASSERT_ONLY retval;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001254 void *shader_code;
1255
1256 FILE *fp = fopen(filename, "rb");
1257 if (!fp) return NULL;
1258
1259 fseek(fp, 0L, SEEK_END);
1260 size = ftell(fp);
1261
1262 fseek(fp, 0L, SEEK_SET);
1263
1264 shader_code = malloc(size);
Tony Barbourfdc2d352015-04-22 09:02:32 -06001265 retval = fread(shader_code, size, 1, fp);
1266 assert(retval == 1);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001267
1268 *psize = size;
1269
1270 return shader_code;
1271}
1272
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001273static VkShader demo_prepare_vs(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001274{
Cody Northrop1fedb212015-05-28 11:27:16 -06001275 if (!demo->use_glsl) {
1276 void *vertShaderCode;
1277 size_t size;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001278
Cody Northrop1fedb212015-05-28 11:27:16 -06001279 vertShaderCode = demo_read_spv("cube-vert.spv", &size);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001280
Cody Northrop1fedb212015-05-28 11:27:16 -06001281 return demo_prepare_shader(demo, VK_SHADER_STAGE_VERTEX,
1282 vertShaderCode, size);
1283 } else {
1284 static const char *vertShaderText =
1285 "#version 140\n"
1286 "#extension GL_ARB_separate_shader_objects : enable\n"
1287 "#extension GL_ARB_shading_language_420pack : enable\n"
1288 "\n"
1289 "layout(binding = 0) uniform buf {\n"
1290 " mat4 MVP;\n"
1291 " vec4 position[12*3];\n"
1292 " vec4 attr[12*3];\n"
1293 "} ubuf;\n"
1294 "\n"
1295 "layout (location = 0) out vec4 texcoord;\n"
1296 "\n"
1297 "void main() \n"
1298 "{\n"
1299 " texcoord = ubuf.attr[gl_VertexID];\n"
1300 " gl_Position = ubuf.MVP * ubuf.position[gl_VertexID];\n"
1301 "\n"
1302 " // GL->VK conventions\n"
1303 " gl_Position.y = -gl_Position.y;\n"
1304 " gl_Position.z = (gl_Position.z + gl_Position.w) / 2.0;\n"
1305 "}\n";
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001306
Cody Northrop1fedb212015-05-28 11:27:16 -06001307 return demo_prepare_shader(demo, VK_SHADER_STAGE_VERTEX,
1308 (const void *) vertShaderText,
1309 strlen(vertShaderText));
1310 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001311}
1312
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001313static VkShader demo_prepare_fs(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001314{
Cody Northrop1fedb212015-05-28 11:27:16 -06001315 if (!demo->use_glsl) {
1316 void *fragShaderCode;
1317 size_t size;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001318
Cody Northrop1fedb212015-05-28 11:27:16 -06001319 fragShaderCode = demo_read_spv("cube-frag.spv", &size);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001320
Cody Northrop1fedb212015-05-28 11:27:16 -06001321 return demo_prepare_shader(demo, VK_SHADER_STAGE_FRAGMENT,
1322 fragShaderCode, size);
1323 } else {
1324 static const char *fragShaderText =
1325 "#version 140\n"
1326 "#extension GL_ARB_separate_shader_objects : enable\n"
1327 "#extension GL_ARB_shading_language_420pack : enable\n"
1328 "layout (binding = 1) uniform sampler2D tex;\n"
1329 "\n"
1330 "layout (location = 0) in vec4 texcoord;\n"
1331 "layout (location = 0) out vec4 uFragColor;\n"
1332 "void main() {\n"
1333 " uFragColor = texture(tex, texcoord.xy);\n"
1334 "}\n";
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001335
Cody Northrop1fedb212015-05-28 11:27:16 -06001336 return demo_prepare_shader(demo, VK_SHADER_STAGE_FRAGMENT,
1337 (const void *) fragShaderText,
1338 strlen(fragShaderText));
1339 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001340}
1341
1342static void demo_prepare_pipeline(struct demo *demo)
1343{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001344 VkGraphicsPipelineCreateInfo pipeline;
1345 VkPipelineIaStateCreateInfo ia;
1346 VkPipelineRsStateCreateInfo rs;
1347 VkPipelineCbStateCreateInfo cb;
1348 VkPipelineDsStateCreateInfo ds;
1349 VkPipelineShaderStageCreateInfo vs;
1350 VkPipelineShaderStageCreateInfo fs;
1351 VkPipelineVpStateCreateInfo vp;
1352 VkPipelineMsStateCreateInfo ms;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001353 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001354
1355 memset(&pipeline, 0, sizeof(pipeline));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001356 pipeline.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001357 pipeline.layout = demo->pipeline_layout;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001358
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001359 memset(&ia, 0, sizeof(ia));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001360 ia.sType = VK_STRUCTURE_TYPE_PIPELINE_IA_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06001361 ia.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001362
1363 memset(&rs, 0, sizeof(rs));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001364 rs.sType = VK_STRUCTURE_TYPE_PIPELINE_RS_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06001365 rs.fillMode = VK_FILL_MODE_SOLID;
1366 rs.cullMode = VK_CULL_MODE_BACK;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001367 rs.frontFace = VK_FRONT_FACE_CCW;
Chia-I Wubb67e6e2015-04-22 14:20:52 +08001368 rs.depthClipEnable = VK_TRUE;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001369
1370 memset(&cb, 0, sizeof(cb));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001371 cb.sType = VK_STRUCTURE_TYPE_PIPELINE_CB_STATE_CREATE_INFO;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001372 VkPipelineCbAttachmentState att_state[1];
Tony Barbour29645d02015-01-16 14:27:35 -07001373 memset(att_state, 0, sizeof(att_state));
1374 att_state[0].format = demo->format;
1375 att_state[0].channelWriteMask = 0xf;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001376 att_state[0].blendEnable = VK_FALSE;
Tony Barbour29645d02015-01-16 14:27:35 -07001377 cb.attachmentCount = 1;
1378 cb.pAttachments = att_state;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001379
Tony Barbour29645d02015-01-16 14:27:35 -07001380 memset(&vp, 0, sizeof(vp));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001381 vp.sType = VK_STRUCTURE_TYPE_PIPELINE_VP_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06001382 vp.viewportCount = 1;
Tony Barbour29645d02015-01-16 14:27:35 -07001383
1384 memset(&ds, 0, sizeof(ds));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001385 ds.sType = VK_STRUCTURE_TYPE_PIPELINE_DS_STATE_CREATE_INFO;
Tony Barbour29645d02015-01-16 14:27:35 -07001386 ds.format = demo->depth.format;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001387 ds.depthTestEnable = VK_TRUE;
1388 ds.depthWriteEnable = VK_TRUE;
Tony Barbour72304ef2015-04-16 15:59:00 -06001389 ds.depthCompareOp = VK_COMPARE_OP_LESS_EQUAL;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001390 ds.depthBoundsEnable = VK_FALSE;
1391 ds.back.stencilFailOp = VK_STENCIL_OP_KEEP;
1392 ds.back.stencilPassOp = VK_STENCIL_OP_KEEP;
Tony Barbour72304ef2015-04-16 15:59:00 -06001393 ds.back.stencilCompareOp = VK_COMPARE_OP_ALWAYS;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001394 ds.stencilTestEnable = VK_FALSE;
Tony Barbour29645d02015-01-16 14:27:35 -07001395 ds.front = ds.back;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001396
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001397 memset(&vs, 0, sizeof(vs));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001398 vs.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1399 vs.shader.stage = VK_SHADER_STAGE_VERTEX;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001400 vs.shader.shader = demo_prepare_vs(demo);
Mike Stroyanebae8322015-04-17 12:36:38 -06001401 assert(vs.shader.shader != VK_NULL_HANDLE);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001402
1403 memset(&fs, 0, sizeof(fs));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001404 fs.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1405 fs.shader.stage = VK_SHADER_STAGE_FRAGMENT;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001406 fs.shader.shader = demo_prepare_fs(demo);
Mike Stroyanebae8322015-04-17 12:36:38 -06001407 assert(fs.shader.shader != VK_NULL_HANDLE);
Tony Barbour29645d02015-01-16 14:27:35 -07001408
1409 memset(&ms, 0, sizeof(ms));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001410 ms.sType = VK_STRUCTURE_TYPE_PIPELINE_MS_STATE_CREATE_INFO;
Tony Barbour29645d02015-01-16 14:27:35 -07001411 ms.sampleMask = 1;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001412 ms.multisampleEnable = VK_FALSE;
Tony Barbour29645d02015-01-16 14:27:35 -07001413 ms.samples = 1;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001414
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001415 pipeline.pNext = (const void *) &ia;
1416 ia.pNext = (const void *) &rs;
1417 rs.pNext = (const void *) &cb;
1418 cb.pNext = (const void *) &ms;
1419 ms.pNext = (const void *) &vp;
1420 vp.pNext = (const void *) &ds;
1421 ds.pNext = (const void *) &vs;
1422 vs.pNext = (const void *) &fs;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001423
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001424 err = vkCreateGraphicsPipeline(demo->device, &pipeline, &demo->pipeline);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001425 assert(!err);
1426
Mike Stroyanebae8322015-04-17 12:36:38 -06001427 vkDestroyObject(demo->device, VK_OBJECT_TYPE_SHADER, vs.shader.shader);
1428 vkDestroyObject(demo->device, VK_OBJECT_TYPE_SHADER, fs.shader.shader);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001429}
1430
1431static void demo_prepare_dynamic_states(struct demo *demo)
1432{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001433 VkDynamicVpStateCreateInfo viewport_create;
1434 VkDynamicRsStateCreateInfo raster;
1435 VkDynamicCbStateCreateInfo color_blend;
1436 VkDynamicDsStateCreateInfo depth_stencil;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001437 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001438
Tony Barbour29645d02015-01-16 14:27:35 -07001439 memset(&viewport_create, 0, sizeof(viewport_create));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001440 viewport_create.sType = VK_STRUCTURE_TYPE_DYNAMIC_VP_STATE_CREATE_INFO;
Courtney Goeltzenleuchter1710d8d2015-02-11 14:13:34 -07001441 viewport_create.viewportAndScissorCount = 1;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001442 VkViewport viewport;
Piers Daniell735ee532015-02-23 16:23:13 -07001443 memset(&viewport, 0, sizeof(viewport));
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001444 viewport.height = (float) demo->height;
1445 viewport.width = (float) demo->width;
1446 viewport.minDepth = (float) 0.0f;
1447 viewport.maxDepth = (float) 1.0f;
Piers Daniell735ee532015-02-23 16:23:13 -07001448 viewport_create.pViewports = &viewport;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001449 VkRect scissor;
Piers Daniell735ee532015-02-23 16:23:13 -07001450 memset(&scissor, 0, sizeof(scissor));
Courtney Goeltzenleuchter1710d8d2015-02-11 14:13:34 -07001451 scissor.extent.width = demo->width;
1452 scissor.extent.height = demo->height;
1453 scissor.offset.x = 0;
1454 scissor.offset.y = 0;
Courtney Goeltzenleuchter1710d8d2015-02-11 14:13:34 -07001455 viewport_create.pScissors = &scissor;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001456
1457 memset(&raster, 0, sizeof(raster));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001458 raster.sType = VK_STRUCTURE_TYPE_DYNAMIC_RS_STATE_CREATE_INFO;
Piers Daniell735ee532015-02-23 16:23:13 -07001459 raster.pointSize = 1.0;
1460 raster.lineWidth = 1.0;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001461
1462 memset(&color_blend, 0, sizeof(color_blend));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001463 color_blend.sType = VK_STRUCTURE_TYPE_DYNAMIC_CB_STATE_CREATE_INFO;
Piers Daniell735ee532015-02-23 16:23:13 -07001464 color_blend.blendConst[0] = 1.0f;
1465 color_blend.blendConst[1] = 1.0f;
1466 color_blend.blendConst[2] = 1.0f;
1467 color_blend.blendConst[3] = 1.0f;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001468
1469 memset(&depth_stencil, 0, sizeof(depth_stencil));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001470 depth_stencil.sType = VK_STRUCTURE_TYPE_DYNAMIC_DS_STATE_CREATE_INFO;
Piers Daniell735ee532015-02-23 16:23:13 -07001471 depth_stencil.minDepth = 0.0f;
1472 depth_stencil.maxDepth = 1.0f;
Tony Barbour29645d02015-01-16 14:27:35 -07001473 depth_stencil.stencilBackRef = 0;
1474 depth_stencil.stencilFrontRef = 0;
1475 depth_stencil.stencilReadMask = 0xff;
1476 depth_stencil.stencilWriteMask = 0xff;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001477
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001478 err = vkCreateDynamicViewportState(demo->device, &viewport_create, &demo->viewport);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001479 assert(!err);
1480
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001481 err = vkCreateDynamicRasterState(demo->device, &raster, &demo->raster);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001482 assert(!err);
1483
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001484 err = vkCreateDynamicColorBlendState(demo->device,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001485 &color_blend, &demo->color_blend);
1486 assert(!err);
1487
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001488 err = vkCreateDynamicDepthStencilState(demo->device,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001489 &depth_stencil, &demo->depth_stencil);
1490 assert(!err);
1491}
1492
Chia-I Wu63ea9262015-03-26 13:14:16 +08001493static void demo_prepare_descriptor_pool(struct demo *demo)
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001494{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001495 const VkDescriptorTypeCount type_counts[2] = {
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001496 [0] = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001497 .type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001498 .count = 1,
1499 },
1500 [1] = {
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -06001501 .type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001502 .count = DEMO_TEXTURE_COUNT,
1503 },
1504 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001505 const VkDescriptorPoolCreateInfo descriptor_pool = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001506 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001507 .pNext = NULL,
1508 .count = 2,
1509 .pTypeCount = type_counts,
1510 };
Tony Barbourfdc2d352015-04-22 09:02:32 -06001511 VkResult U_ASSERT_ONLY err;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001512
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001513 err = vkCreateDescriptorPool(demo->device,
1514 VK_DESCRIPTOR_POOL_USAGE_ONE_SHOT, 1,
Chia-I Wu63ea9262015-03-26 13:14:16 +08001515 &descriptor_pool, &demo->desc_pool);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001516 assert(!err);
1517}
1518
1519static void demo_prepare_descriptor_set(struct demo *demo)
1520{
Chia-I Wuae721ba2015-05-25 16:27:55 +08001521 VkDescriptorInfo tex_descs[DEMO_TEXTURE_COUNT];
1522 VkWriteDescriptorSet writes[2];
Tony Barbourfdc2d352015-04-22 09:02:32 -06001523 VkResult U_ASSERT_ONLY err;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001524 uint32_t count;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001525 uint32_t i;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001526
Mike Stroyanebae8322015-04-17 12:36:38 -06001527 err = vkAllocDescriptorSets(demo->device, demo->desc_pool,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001528 VK_DESCRIPTOR_SET_USAGE_STATIC,
Chia-I Wu87544e72015-02-23 10:41:08 -07001529 1, &demo->desc_layout,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001530 &demo->desc_set, &count);
1531 assert(!err && count == 1);
1532
Mike Stroyanebae8322015-04-17 12:36:38 -06001533 vkClearDescriptorSets(demo->device, demo->desc_pool, 1, &demo->desc_set);
Chia-I Wuae721ba2015-05-25 16:27:55 +08001534
1535 memset(&tex_descs, 0, sizeof(tex_descs));
1536 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
1537 tex_descs[i].sampler = demo->textures[i].sampler;
1538 tex_descs[i].imageView = demo->textures[i].view;
1539 tex_descs[i].imageLayout = VK_IMAGE_LAYOUT_GENERAL;
1540 }
1541
1542 memset(&writes, 0, sizeof(writes));
1543
1544 writes[0].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
1545 writes[0].destSet = demo->desc_set;
1546 writes[0].count = 1;
1547 writes[0].descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
1548 writes[0].pDescriptors = &demo->uniform_data.desc;
1549
1550 writes[1].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
1551 writes[1].destSet = demo->desc_set;
1552 writes[1].destBinding = 1;
1553 writes[1].count = DEMO_TEXTURE_COUNT;
1554 writes[1].descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
1555 writes[1].pDescriptors = tex_descs;
1556
1557 err = vkUpdateDescriptorSets(demo->device, 2, writes, 0, NULL);
1558 assert(!err);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001559}
1560
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001561static void demo_prepare(struct demo *demo)
1562{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001563 const VkCmdBufferCreateInfo cmd = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001564 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001565 .pNext = NULL,
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07001566 .queueNodeIndex = demo->graphics_queue_node_index,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001567 .flags = 0,
1568 };
Tony Barbourfdc2d352015-04-22 09:02:32 -06001569 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001570
1571 demo_prepare_buffers(demo);
1572 demo_prepare_depth(demo);
1573 demo_prepare_textures(demo);
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001574 demo_prepare_cube_data_buffer(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001575
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001576 demo_prepare_descriptor_layout(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001577 demo_prepare_pipeline(demo);
1578 demo_prepare_dynamic_states(demo);
1579
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001580 for (int i = 0; i < DEMO_BUFFER_COUNT; i++) {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001581 err = vkCreateCommandBuffer(demo->device, &cmd, &demo->buffers[i].cmd);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001582 assert(!err);
1583 }
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001584
Chia-I Wu63ea9262015-03-26 13:14:16 +08001585 demo_prepare_descriptor_pool(demo);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001586 demo_prepare_descriptor_set(demo);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001587
1588
1589 for (int i = 0; i < DEMO_BUFFER_COUNT; i++) {
1590 demo->current_buffer = i;
1591 demo_draw_build_cmd(demo, demo->buffers[i].cmd);
1592 }
1593
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001594 /*
1595 * Prepare functions above may generate pipeline commands
1596 * that need to be flushed before beginning the render loop.
1597 */
1598 demo_flush_init_cmd(demo);
1599
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001600 demo->current_buffer = 0;
Jon Ashburn8d26c062015-04-24 09:46:24 -07001601 demo->prepared = true;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001602}
1603
Ian Elliott639ca472015-04-16 15:23:05 -06001604#ifdef _WIN32
1605static void demo_run(struct demo *demo)
1606{
Courtney Goeltzenleuchterb7e22702015-04-27 14:56:34 -06001607 if (!demo->prepared)
1608 return;
Ian Elliott639ca472015-04-16 15:23:05 -06001609 // Wait for work to finish before updating MVP.
1610 vkDeviceWaitIdle(demo->device);
1611 demo_update_data_buffer(demo);
1612
1613 demo_draw(demo);
1614
1615 // Wait for work to finish before updating MVP.
1616 vkDeviceWaitIdle(demo->device);
1617}
1618
1619// On MS-Windows, make this a global, so it's available to WndProc()
1620struct demo demo;
1621
1622// MS-Windows event handling function:
1623LRESULT CALLBACK WndProc(HWND hWnd,
1624 UINT uMsg,
1625 WPARAM wParam,
1626 LPARAM lParam)
1627{
Ian Elliott639ca472015-04-16 15:23:05 -06001628 switch(uMsg)
1629 {
Ian Elliott639ca472015-04-16 15:23:05 -06001630 case WM_CLOSE:
1631 PostQuitMessage(0);
Tony Barbour3dddd5d2015-04-29 16:19:20 -06001632 break;
Ian Elliott639ca472015-04-16 15:23:05 -06001633 case WM_PAINT:
1634 demo_run(&demo);
1635 return 0;
1636 default:
1637 break;
1638 }
1639 return (DefWindowProc(hWnd, uMsg, wParam, lParam));
1640}
1641
1642static void demo_create_window(struct demo *demo)
1643{
1644 WNDCLASSEX win_class;
1645
1646 // Initialize the window class structure:
1647 win_class.cbSize = sizeof(WNDCLASSEX);
1648 win_class.style = CS_HREDRAW | CS_VREDRAW;
1649 win_class.lpfnWndProc = WndProc;
1650 win_class.cbClsExtra = 0;
1651 win_class.cbWndExtra = 0;
1652 win_class.hInstance = demo->connection; // hInstance
1653 win_class.hIcon = LoadIcon(NULL, IDI_APPLICATION);
1654 win_class.hCursor = LoadCursor(NULL, IDC_ARROW);
1655 win_class.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
1656 win_class.lpszMenuName = NULL;
1657 win_class.lpszClassName = demo->name;
1658 win_class.hIconSm = LoadIcon(NULL, IDI_WINLOGO);
1659 // Register window class:
1660 if (!RegisterClassEx(&win_class)) {
1661 // It didn't work, so try to give a useful error:
1662 printf("Unexpected error trying to start the application!\n");
1663 fflush(stdout);
1664 exit(1);
1665 }
1666 // Create window with the registered class:
Mike Stroyanb46779e2015-06-15 14:20:13 -06001667 RECT wr = { 0, 0, demo->width, demo->height };
1668 AdjustWindowRect(&wr, WS_OVERLAPPEDWINDOW, FALSE);
Ian Elliott639ca472015-04-16 15:23:05 -06001669 demo->window = CreateWindowEx(0,
1670 demo->name, // class name
1671 demo->name, // app name
1672 WS_OVERLAPPEDWINDOW | // window style
1673 WS_VISIBLE |
1674 WS_SYSMENU,
1675 100,100, // x/y coords
Mike Stroyanb46779e2015-06-15 14:20:13 -06001676 wr.right-wr.left, // width
1677 wr.bottom-wr.top, // height
Ian Elliott639ca472015-04-16 15:23:05 -06001678 NULL, // handle to parent
1679 NULL, // handle to menu
1680 demo->connection, // hInstance
1681 NULL); // no extra parameters
1682 if (!demo->window) {
1683 // It didn't work, so try to give a useful error:
1684 printf("Cannot create a window in which to draw!\n");
1685 fflush(stdout);
1686 exit(1);
1687 }
1688}
1689#else // _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001690static void demo_handle_event(struct demo *demo,
1691 const xcb_generic_event_t *event)
1692{
Piers Daniell735ee532015-02-23 16:23:13 -07001693 uint8_t event_code = event->response_type & 0x7f;
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07001694 switch (event_code) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001695 case XCB_EXPOSE:
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07001696 // TODO: Resize window
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001697 break;
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07001698 case XCB_CLIENT_MESSAGE:
1699 if((*(xcb_client_message_event_t*)event).data.data32[0] ==
1700 (*demo->atom_wm_delete_window).atom) {
1701 demo->quit = true;
1702 }
1703 break;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001704 case XCB_KEY_RELEASE:
1705 {
1706 const xcb_key_release_event_t *key =
1707 (const xcb_key_release_event_t *) event;
1708
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001709 switch (key->detail) {
1710 case 0x9: // Escape
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001711 demo->quit = true;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001712 break;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001713 case 0x71: // left arrow key
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001714 demo->spin_angle += demo->spin_increment;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001715 break;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001716 case 0x72: // right arrow key
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001717 demo->spin_angle -= demo->spin_increment;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001718 break;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001719 case 0x41:
1720 demo->pause = !demo->pause;
Piers Daniell735ee532015-02-23 16:23:13 -07001721 break;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001722 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001723 }
1724 break;
1725 default:
1726 break;
1727 }
1728}
1729
1730static void demo_run(struct demo *demo)
1731{
1732 xcb_flush(demo->connection);
1733
1734 while (!demo->quit) {
1735 xcb_generic_event_t *event;
1736
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07001737 if (demo->pause) {
1738 event = xcb_wait_for_event(demo->connection);
1739 } else {
1740 event = xcb_poll_for_event(demo->connection);
1741 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001742 if (event) {
1743 demo_handle_event(demo, event);
1744 free(event);
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07001745 }
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07001746
1747 // Wait for work to finish before updating MVP.
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001748 vkDeviceWaitIdle(demo->device);
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07001749 demo_update_data_buffer(demo);
1750
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07001751 demo_draw(demo);
Courtney Goeltzenleuchter1454f3c2014-11-18 11:28:09 -07001752
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07001753 // Wait for work to finish before updating MVP.
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001754 vkDeviceWaitIdle(demo->device);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001755 }
1756}
1757
1758static void demo_create_window(struct demo *demo)
1759{
1760 uint32_t value_mask, value_list[32];
1761
1762 demo->window = xcb_generate_id(demo->connection);
1763
1764 value_mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK;
1765 value_list[0] = demo->screen->black_pixel;
1766 value_list[1] = XCB_EVENT_MASK_KEY_RELEASE |
1767 XCB_EVENT_MASK_EXPOSURE;
1768
1769 xcb_create_window(demo->connection,
1770 XCB_COPY_FROM_PARENT,
1771 demo->window, demo->screen->root,
1772 0, 0, demo->width, demo->height, 0,
1773 XCB_WINDOW_CLASS_INPUT_OUTPUT,
1774 demo->screen->root_visual,
1775 value_mask, value_list);
1776
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07001777 /* Magic code that will send notification when window is destroyed */
1778 xcb_intern_atom_cookie_t cookie = xcb_intern_atom(demo->connection, 1, 12,
1779 "WM_PROTOCOLS");
1780 xcb_intern_atom_reply_t* reply = xcb_intern_atom_reply(demo->connection, cookie, 0);
1781
1782 xcb_intern_atom_cookie_t cookie2 = xcb_intern_atom(demo->connection, 0, 16, "WM_DELETE_WINDOW");
1783 demo->atom_wm_delete_window = xcb_intern_atom_reply(demo->connection, cookie2, 0);
1784
1785 xcb_change_property(demo->connection, XCB_PROP_MODE_REPLACE,
1786 demo->window, (*reply).atom, 4, 32, 1,
1787 &(*demo->atom_wm_delete_window).atom);
1788 free(reply);
1789
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001790 xcb_map_window(demo->connection, demo->window);
1791}
Ian Elliott639ca472015-04-16 15:23:05 -06001792#endif // _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001793
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001794static void demo_init_vk(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001795{
Tobin Ehlis99239dc2015-04-16 18:04:57 -06001796 VkResult err;
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06001797 VkExtensionProperties *instance_extensions;
1798 uint32_t instance_extension_count = 0;
1799 VkExtensionProperties *device_extensions;
1800 uint32_t device_extension_count = 0;
Tobin Ehlis99239dc2015-04-16 18:04:57 -06001801 size_t extSize = sizeof(uint32_t);
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06001802 uint32_t total_extension_count = 0;
1803 err = vkGetGlobalExtensionInfo(VK_EXTENSION_INFO_TYPE_COUNT, 0, &extSize, &total_extension_count);
Tobin Ehlis99239dc2015-04-16 18:04:57 -06001804 assert(!err);
1805
1806 VkExtensionProperties extProp;
1807 extSize = sizeof(VkExtensionProperties);
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06001808 bool32_t WSIextFound = 0;
1809 instance_extensions = malloc(sizeof(VkExtensionProperties) * total_extension_count);
1810 device_extensions = malloc(sizeof(VkExtensionProperties) * total_extension_count);
1811 for (uint32_t i = 0; i < total_extension_count; i++) {
Tobin Ehlis99239dc2015-04-16 18:04:57 -06001812 err = vkGetGlobalExtensionInfo(VK_EXTENSION_INFO_TYPE_PROPERTIES, i, &extSize, &extProp);
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06001813 if (!strcmp("VK_WSI_LunarG", extProp.name)) {
1814 WSIextFound = 1;
1815 memcpy(&instance_extensions[instance_extension_count++], &extProp, sizeof(VkExtensionProperties));
1816 memcpy(&device_extensions[device_extension_count++], &extProp, sizeof(VkExtensionProperties));
1817 }
1818 if (!strcmp("Validation", extProp.name)) {
1819 memcpy(&instance_extensions[instance_extension_count++], &extProp, sizeof(VkExtensionProperties));
1820 memcpy(&device_extensions[device_extension_count++], &extProp, sizeof(VkExtensionProperties));
1821 }
1822 if (!strcmp(DEBUG_REPORT_EXTENSION_NAME, extProp.name)) {
1823 memcpy(&instance_extensions[instance_extension_count++], &extProp, sizeof(VkExtensionProperties));
1824 }
Tobin Ehlis99239dc2015-04-16 18:04:57 -06001825 }
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06001826 if (!WSIextFound) {
Ian Elliott65152912015-04-28 13:22:33 -06001827 ERR_EXIT("vkGetGlobalExtensionInfo failed to find the "
1828 "\"VK_WSI_LunarG\" extension.\n\nDo you have a compatible "
1829 "Vulkan installable client driver (ICD) installed?\nPlease "
1830 "look at the Getting Started guide for additional "
1831 "information.\n",
1832 "vkCreateInstance Failure");
1833 }
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001834 const VkApplicationInfo app = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001835 .sType = VK_STRUCTURE_TYPE_APPLICATION_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001836 .pNext = NULL,
Ian Elliott44e33f72015-04-28 10:52:52 -06001837 .pAppName = APP_SHORT_NAME,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001838 .appVersion = 0,
Ian Elliott44e33f72015-04-28 10:52:52 -06001839 .pEngineName = APP_SHORT_NAME,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001840 .engineVersion = 0,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001841 .apiVersion = VK_API_VERSION,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001842 };
Tony Barbour3dddd5d2015-04-29 16:19:20 -06001843 VkInstanceCreateInfo inst_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001844 .sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
Jon Ashburnab46b362015-04-04 14:52:07 -06001845 .pNext = NULL,
1846 .pAppInfo = &app,
1847 .pAllocCb = NULL,
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06001848 .extensionCount = instance_extension_count,
1849 .pEnabledExtensions = instance_extensions,
Jon Ashburnab46b362015-04-04 14:52:07 -06001850 };
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -06001851 const VkDeviceQueueCreateInfo queue = {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001852 .queueNodeIndex = 0,
1853 .queueCount = 1,
1854 };
Ian Elliott097d9f32015-04-28 11:35:02 -06001855
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06001856 VkDeviceCreateInfo device = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001857 .sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001858 .pNext = NULL,
1859 .queueRecordCount = 1,
1860 .pRequestedQueues = &queue,
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06001861 .extensionCount = device_extension_count,
1862 .pEnabledExtensions = device_extensions,
Jon Ashburn7842d522015-05-18 09:06:15 -06001863 .flags = 0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001864 };
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001865 uint32_t gpu_count;
1866 uint32_t i;
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07001867 size_t data_size;
1868 uint32_t queue_count;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001869
Tony Barbour3dddd5d2015-04-29 16:19:20 -06001870 if (demo->validate) {
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06001871 inst_info.extensionCount = 3;
1872 device.extensionCount = 3;
Tony Barbour3dddd5d2015-04-29 16:19:20 -06001873 }
1874
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001875 err = vkCreateInstance(&inst_info, &demo->inst);
Ian Elliott07264132015-04-28 11:35:02 -06001876 if (err == VK_ERROR_INCOMPATIBLE_DRIVER) {
1877 ERR_EXIT("Cannot find a compatible Vulkan installable client driver "
Ian Elliott65152912015-04-28 13:22:33 -06001878 "(ICD).\n\nPlease look at the Getting Started guide for "
Ian Elliott07264132015-04-28 11:35:02 -06001879 "additional information.\n",
1880 "vkCreateInstance Failure");
Tony Barbour3dddd5d2015-04-29 16:19:20 -06001881 } else if (err == VK_ERROR_INVALID_EXTENSION) {
1882 ERR_EXIT("Cannot find a specified extension library"
1883 ".\nMake sure your layers path is set appropriately\n",
1884 "vkCreateInstance Failure");
Ian Elliott07264132015-04-28 11:35:02 -06001885 } else if (err) {
Ian Elliott65152912015-04-28 13:22:33 -06001886 ERR_EXIT("vkCreateInstance failed.\n\nDo you have a compatible Vulkan "
1887 "installable client driver (ICD) installed?\nPlease look at "
Ian Elliott07264132015-04-28 11:35:02 -06001888 "the Getting Started guide for additional information.\n",
1889 "vkCreateInstance Failure");
Ian Elliott3979e282015-04-03 15:24:55 -06001890 }
Jon Ashburnab46b362015-04-04 14:52:07 -06001891
Tony Barbour3dddd5d2015-04-29 16:19:20 -06001892
Jon Ashburn07c0c0c2015-04-15 11:31:12 -06001893 gpu_count = 1;
1894 err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, &demo->gpu);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001895 assert(!err && gpu_count == 1);
1896
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06001897
Tony Barbour3dddd5d2015-04-29 16:19:20 -06001898 if (demo->validate) {
Courtney Goeltzenleuchter922a0fa2015-06-10 17:39:03 -06001899 demo->dbgCreateMsgCallback = vkGetInstanceProcAddr((VkPhysicalDevice) NULL, "vkDbgCreateMsgCallback");
1900 if (!demo->dbgCreateMsgCallback) {
1901 ERR_EXIT("GetProcAddr: Unable to find vkDbgCreateMsgCallback\n",
1902 "vkGetProcAddr Failure");
1903 }
1904 err = demo->dbgCreateMsgCallback(
1905 demo->inst,
1906 VK_DBG_REPORT_ERROR_BIT | VK_DBG_REPORT_WARN_BIT,
1907 dbgFunc, NULL,
1908 &demo->msg_callback);
1909 switch (err) {
1910 case VK_SUCCESS:
1911 break;
1912 case VK_ERROR_INVALID_POINTER:
1913 ERR_EXIT("dbgCreateMsgCallback: Invalid pointer\n",
1914 "dbgCreateMsgCallback Failure");
1915 break;
1916 case VK_ERROR_OUT_OF_HOST_MEMORY:
1917 ERR_EXIT("dbgCreateMsgCallback: out of host memory\n",
1918 "dbgCreateMsgCallback Failure");
1919 break;
1920 default:
1921 ERR_EXIT("dbgCreateMsgCallback: unknown failure\n",
1922 "dbgCreateMsgCallback Failure");
1923 break;
1924 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06001925 }
1926
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001927 err = vkCreateDevice(demo->gpu, &device, &demo->device);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001928 assert(!err);
1929
Courtney Goeltzenleuchter8f0767f2015-06-10 17:33:55 -06001930 demo->fpGetDisplayInfoWSI = vkGetDeviceProcAddr(demo->device, "vkGetDisplayInfoWSI");
1931 if (demo->fpGetDisplayInfoWSI == NULL)
1932 ERR_EXIT("vkGetDeviceProcAddr failed to find vkGetDisplayInfoWSI",
1933 "vkGetDeviceProcAddr Failure");
Jon Ashburn0b85d052015-05-21 18:13:33 -06001934 demo->fpCreateSwapChainWSI = vkGetDeviceProcAddr(demo->device, "vkCreateSwapChainWSI");
1935 if (demo->fpCreateSwapChainWSI == NULL)
1936 ERR_EXIT("vkGetDeviceProcAddr failed to find vkCreateSwapChainWSI",
1937 "vkGetDeviceProcAddr Failure");
1938 demo->fpDestroySwapChainWSI = vkGetDeviceProcAddr(demo->device, "vkDestroySwapChainWSI");
1939 if (demo->fpDestroySwapChainWSI == NULL)
1940 ERR_EXIT("vkGetDeviceProcAddr failed to find vkDestroySwapChainWSI",
1941 "vkGetDeviceProcAddr Failure");
1942 demo->fpGetSwapChainInfoWSI = vkGetDeviceProcAddr(demo->device, "vkGetSwapChainInfoWSI");
1943 if (demo->fpGetSwapChainInfoWSI == NULL)
1944 ERR_EXIT("vkGetDeviceProcAddr failed to find vkGetSwapChainInfoWSI",
1945 "vkGetDeviceProcAddr Failure");
1946 demo->fpQueuePresentWSI = vkGetDeviceProcAddr(demo->device, "vkQueuePresentWSI");
1947 if (demo->fpQueuePresentWSI == NULL)
1948 ERR_EXIT("vkGetDeviceProcAddr failed to find vkQueuePresentWSI",
1949 "vkGetDeviceProcAddr Failure");
1950
Tony Barbour72304ef2015-04-16 15:59:00 -06001951 err = vkGetPhysicalDeviceInfo(demo->gpu, VK_PHYSICAL_DEVICE_INFO_TYPE_PROPERTIES,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001952 &data_size, NULL);
1953 assert(!err);
1954
Tony Barbour72304ef2015-04-16 15:59:00 -06001955 demo->gpu_props = (VkPhysicalDeviceProperties *) malloc(data_size);
1956 err = vkGetPhysicalDeviceInfo(demo->gpu, VK_PHYSICAL_DEVICE_INFO_TYPE_PROPERTIES,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001957 &data_size, demo->gpu_props);
1958 assert(!err);
1959
Tony Barbour72304ef2015-04-16 15:59:00 -06001960 err = vkGetPhysicalDeviceInfo(demo->gpu, VK_PHYSICAL_DEVICE_INFO_TYPE_QUEUE_PROPERTIES,
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07001961 &data_size, NULL);
1962 assert(!err);
1963
Tony Barbour72304ef2015-04-16 15:59:00 -06001964 demo->queue_props = (VkPhysicalDeviceQueueProperties *) malloc(data_size);
1965 err = vkGetPhysicalDeviceInfo(demo->gpu, VK_PHYSICAL_DEVICE_INFO_TYPE_QUEUE_PROPERTIES,
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07001966 &data_size, demo->queue_props);
1967 assert(!err);
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001968 queue_count = (uint32_t)(data_size / sizeof(VkPhysicalDeviceQueueProperties));
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07001969 assert(queue_count >= 1);
1970
Mark Lobodzinskida9b1092015-04-16 11:44:05 -05001971 // Graphics queue and MemMgr queue can be separate.
1972 // TODO: Add support for separate queues, including synchronization,
Mark Lobodzinski6baaec72015-05-11 17:21:15 -05001973 // and appropriate tracking for QueueSubmit
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07001974 for (i = 0; i < queue_count; i++) {
Mark Lobodzinski6baaec72015-05-11 17:21:15 -05001975 if (demo->queue_props[i].queueFlags & VK_QUEUE_GRAPHICS_BIT)
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07001976 break;
1977 }
1978 assert(i < queue_count);
1979 demo->graphics_queue_node_index = i;
1980
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001981 err = vkGetDeviceQueue(demo->device, demo->graphics_queue_node_index,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001982 0, &demo->queue);
1983 assert(!err);
Tony Barbour3dddd5d2015-04-29 16:19:20 -06001984
Ian Elliotte4602cd2015-04-21 16:41:02 -06001985
1986 // Get the VkDisplayWSI's associated with this physical device:
1987 VkDisplayWSI display;
1988 err = vkGetPhysicalDeviceInfo(demo->gpu, VK_PHYSICAL_DEVICE_INFO_TYPE_DISPLAY_PROPERTIES_WSI,
1989 &data_size, NULL);
Ian Elliottd414a8f2015-05-15 17:52:29 -06001990 if (err != VK_SUCCESS) {
1991 printf("The Vulkan installable client driver (ICD) does not support "
1992 "querying\nfor the swap-chain image format. Therefore, am "
1993 "hardcoding this\nformat to VK_FORMAT_B8G8R8A8_UNORM.\n");
1994 fflush(stdout);
1995 demo->format = VK_FORMAT_B8G8R8A8_UNORM;
1996 return;
1997 }
Ian Elliotte4602cd2015-04-21 16:41:02 -06001998 demo->display_props = (VkDisplayPropertiesWSI *) malloc(data_size);
1999 err = vkGetPhysicalDeviceInfo(demo->gpu, VK_PHYSICAL_DEVICE_INFO_TYPE_DISPLAY_PROPERTIES_WSI,
2000 &data_size, demo->display_props);
2001 assert(!err);
2002 demo->num_displays = data_size / sizeof(VkDisplayPropertiesWSI);
2003 // For now, simply use the first display (TODO: Enhance this for the
2004 // future):
2005 display = demo->display_props[0].display;
2006
2007 // Get a VkFormat to use with the VkDisplayWSI we are using:
Courtney Goeltzenleuchter8f0767f2015-06-10 17:33:55 -06002008 err = demo->fpGetDisplayInfoWSI(display, VK_DISPLAY_INFO_TYPE_FORMAT_PROPERTIES_WSI,
Ian Elliotte4602cd2015-04-21 16:41:02 -06002009 &data_size, NULL);
2010 VkDisplayFormatPropertiesWSI* display_format_props =
2011 (VkDisplayFormatPropertiesWSI*) malloc(data_size);
Courtney Goeltzenleuchter8f0767f2015-06-10 17:33:55 -06002012 err = demo->fpGetDisplayInfoWSI(display, VK_DISPLAY_INFO_TYPE_FORMAT_PROPERTIES_WSI,
Ian Elliotte4602cd2015-04-21 16:41:02 -06002013 &data_size, display_format_props);
2014 // For now, simply use the first VkFormat (TODO: Enhance this for the
2015 // future):
2016 demo->format = display_format_props[0].swapChainFormat;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002017}
2018
2019static void demo_init_connection(struct demo *demo)
2020{
Ian Elliott639ca472015-04-16 15:23:05 -06002021#ifndef _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002022 const xcb_setup_t *setup;
2023 xcb_screen_iterator_t iter;
2024 int scr;
2025
2026 demo->connection = xcb_connect(NULL, &scr);
Ian Elliott3979e282015-04-03 15:24:55 -06002027 if (demo->connection == NULL) {
2028 printf("Cannot find a compatible Vulkan installable client driver "
2029 "(ICD).\nExiting ...\n");
2030 fflush(stdout);
2031 exit(1);
2032 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002033
2034 setup = xcb_get_setup(demo->connection);
2035 iter = xcb_setup_roots_iterator(setup);
2036 while (scr-- > 0)
2037 xcb_screen_next(&iter);
2038
2039 demo->screen = iter.data;
Ian Elliott639ca472015-04-16 15:23:05 -06002040#endif // _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002041}
2042
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002043static void demo_init(struct demo *demo, int argc, char **argv)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002044{
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002045 vec3 eye = {0.0f, 3.0f, 5.0f};
2046 vec3 origin = {0, 0, 0};
Chia-I Wuae3b55d2015-04-22 14:56:17 +08002047 vec3 up = {0.0f, 1.0f, 0.0};
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002048
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002049 memset(demo, 0, sizeof(*demo));
2050
Piers Daniell735ee532015-02-23 16:23:13 -07002051 for (int i = 1; i < argc; i++) {
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002052 if (strcmp(argv[i], "--use_staging") == 0) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002053 demo->use_staging_buffer = true;
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002054 continue;
Ian Elliott639ca472015-04-16 15:23:05 -06002055 }
Cody Northrop1fedb212015-05-28 11:27:16 -06002056 if (strcmp(argv[i], "--use_glsl") == 0) {
2057 demo->use_glsl = true;
2058 continue;
2059 }
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002060 if (strcmp(argv[i], "--validate") == 0) {
2061 demo->validate = true;
2062 continue;
2063 }
2064
2065 fprintf(stderr, "Usage:\n %s [--use_staging] [--validate}\n", APP_SHORT_NAME);
Ian Elliott639ca472015-04-16 15:23:05 -06002066 fflush(stderr);
2067 exit(1);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002068 }
2069
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002070 demo_init_connection(demo);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002071 demo_init_vk(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002072
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002073 demo->width = 500;
2074 demo->height = 500;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002075
2076 demo->spin_angle = 0.01f;
2077 demo->spin_increment = 0.01f;
2078 demo->pause = false;
2079
Piers Daniell735ee532015-02-23 16:23:13 -07002080 mat4x4_perspective(demo->projection_matrix, (float)degreesToRadians(45.0f), 1.0f, 0.1f, 100.0f);
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06002081 mat4x4_look_at(demo->view_matrix, eye, origin, up);
2082 mat4x4_identity(demo->model_matrix);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002083}
2084
2085static void demo_cleanup(struct demo *demo)
2086{
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05002087 uint32_t i;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002088
Courtney Goeltzenleuchterb7e22702015-04-27 14:56:34 -06002089 demo->prepared = false;
2090
Mike Stroyanebae8322015-04-17 12:36:38 -06002091 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DESCRIPTOR_SET, demo->desc_set);
2092 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DESCRIPTOR_POOL, demo->desc_pool);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08002093
Mike Stroyanebae8322015-04-17 12:36:38 -06002094 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DYNAMIC_VP_STATE, demo->viewport);
2095 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DYNAMIC_RS_STATE, demo->raster);
2096 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DYNAMIC_CB_STATE, demo->color_blend);
2097 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DYNAMIC_DS_STATE, demo->depth_stencil);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002098
Mike Stroyanebae8322015-04-17 12:36:38 -06002099 vkDestroyObject(demo->device, VK_OBJECT_TYPE_PIPELINE, demo->pipeline);
2100 vkDestroyObject(demo->device, VK_OBJECT_TYPE_PIPELINE_LAYOUT, demo->pipeline_layout);
2101 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DESCRIPTOR_SET_LAYOUT, demo->desc_layout);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002102
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002103 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Mike Stroyanebae8322015-04-17 12:36:38 -06002104 vkDestroyObject(demo->device, VK_OBJECT_TYPE_IMAGE_VIEW, demo->textures[i].view);
Mike Stroyanebae8322015-04-17 12:36:38 -06002105 vkDestroyObject(demo->device, VK_OBJECT_TYPE_IMAGE, demo->textures[i].image);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05002106 vkFreeMemory(demo->device, demo->textures[i].mem);
Mike Stroyanebae8322015-04-17 12:36:38 -06002107 vkDestroyObject(demo->device, VK_OBJECT_TYPE_SAMPLER, demo->textures[i].sampler);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002108 }
Jon Ashburn0b85d052015-05-21 18:13:33 -06002109 demo->fpDestroySwapChainWSI(demo->swap_chain);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002110
Mike Stroyanebae8322015-04-17 12:36:38 -06002111 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DEPTH_STENCIL_VIEW, demo->depth.view);
Mike Stroyanebae8322015-04-17 12:36:38 -06002112 vkDestroyObject(demo->device, VK_OBJECT_TYPE_IMAGE, demo->depth.image);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05002113 vkFreeMemory(demo->device, demo->depth.mem);
Mark Lobodzinski7b8fee62015-02-18 16:38:17 -06002114
Mike Stroyanebae8322015-04-17 12:36:38 -06002115 vkDestroyObject(demo->device, VK_OBJECT_TYPE_BUFFER_VIEW, demo->uniform_data.view);
Mike Stroyanebae8322015-04-17 12:36:38 -06002116 vkDestroyObject(demo->device, VK_OBJECT_TYPE_BUFFER, demo->uniform_data.buf);
Mark Lobodzinski2dcdfd72015-05-29 09:32:35 -05002117 vkFreeMemory(demo->device, demo->uniform_data.mem);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002118
2119 for (i = 0; i < DEMO_BUFFER_COUNT; i++) {
Mike Stroyanebae8322015-04-17 12:36:38 -06002120 vkDestroyObject(demo->device, VK_OBJECT_TYPE_COLOR_ATTACHMENT_VIEW, demo->buffers[i].view);
2121 vkDestroyObject(demo->device, VK_OBJECT_TYPE_COMMAND_BUFFER, demo->buffers[i].cmd);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002122 }
2123
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002124 vkDestroyDevice(demo->device);
2125 vkDestroyInstance(demo->inst);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002126
Ian Elliott639ca472015-04-16 15:23:05 -06002127#ifndef _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002128 xcb_destroy_window(demo->connection, demo->window);
2129 xcb_disconnect(demo->connection);
Ian Elliott639ca472015-04-16 15:23:05 -06002130#endif // _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002131}
2132
Ian Elliott639ca472015-04-16 15:23:05 -06002133#ifdef _WIN32
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002134extern int __getmainargs(
2135 int * _Argc,
2136 char *** _Argv,
2137 char *** _Env,
2138 int _DoWildCard,
2139 int * new_mode);
Ian Elliottf9cf78c2015-04-28 10:33:11 -06002140
Ian Elliotta748eaf2015-04-28 15:50:36 -06002141int WINAPI WinMain(HINSTANCE hInstance,
2142 HINSTANCE hPrevInstance,
2143 LPSTR pCmdLine,
2144 int nCmdShow)
Ian Elliott639ca472015-04-16 15:23:05 -06002145{
2146 MSG msg; // message
2147 bool done; // flag saying when app is complete
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002148 int argc;
2149 char** argv;
2150 char** env;
2151 int new_mode = 0;
Ian Elliott639ca472015-04-16 15:23:05 -06002152
Tony Barbour3dddd5d2015-04-29 16:19:20 -06002153 __getmainargs(&argc,&argv,&env,0,&new_mode);
2154
2155 demo_init(&demo, argc, argv);
2156 demo.connection = hInstance;
2157 strncpy(demo.name, "cube", APP_NAME_STR_LEN);
Ian Elliott639ca472015-04-16 15:23:05 -06002158 demo_create_window(&demo);
2159
2160 demo_prepare(&demo);
2161
2162 done = false; //initialize loop condition variable
2163 /* main message loop*/
2164 while(!done)
2165 {
Ian Elliotta748eaf2015-04-28 15:50:36 -06002166 PeekMessage(&msg, NULL, 0, 0, PM_REMOVE);
Ian Elliott639ca472015-04-16 15:23:05 -06002167 if (msg.message == WM_QUIT) //check for a quit message
2168 {
2169 done = true; //if found, quit app
2170 }
2171 else
2172 {
2173 /* Translate and dispatch to event queue*/
2174 TranslateMessage(&msg);
2175 DispatchMessage(&msg);
2176 }
2177 }
2178
2179 demo_cleanup(&demo);
2180
Tony Barbourf9921732015-04-22 11:36:22 -06002181 return (int) msg.wParam;
Ian Elliott639ca472015-04-16 15:23:05 -06002182}
2183#else // _WIN32
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002184int main(int argc, char **argv)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002185{
2186 struct demo demo;
2187
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002188 demo_init(&demo, argc, argv);
Chia-I Wucbb564e2015-04-16 22:02:10 +08002189 demo_create_window(&demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002190
2191 demo_prepare(&demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002192 demo_run(&demo);
2193
2194 demo_cleanup(&demo);
2195
2196 return 0;
2197}
Ian Elliott639ca472015-04-16 15:23:05 -06002198#endif // _WIN32