blob: b126a7a0b1fda1846ddb0b7b846f9cc7696f23a1 [file] [log] [blame]
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -06001#define _GNU_SOURCE
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002#include <stdio.h>
3#include <stdlib.h>
4#include <string.h>
5#include <stdbool.h>
6#include <assert.h>
7
Ian Elliotte14e9f92015-04-16 15:23:05 -06008#ifdef _WIN32
9#pragma comment(linker, "/subsystem:windows")
10#include <windows.h>
11#define APP_NAME_STR_LEN 80
12#else // _WIN32
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -060013#include <xcb/xcb.h>
Ian Elliotte14e9f92015-04-16 15:23:05 -060014#endif // _WIN32
15
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -060016#include <vulkan.h>
17#include <vkDbg.h>
Chia-I Wu5b66aa52015-04-16 22:02:10 +080018#include <vk_wsi_lunarg.h>
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -060019
Cody Northropd4e020a2015-03-17 14:54:35 -060020#include "icd-spv.h"
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -060021
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -060022#include "linmath.h"
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -060023#include <png.h>
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -060024
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -060025#define DEMO_BUFFER_COUNT 2
26#define DEMO_TEXTURE_COUNT 1
Ian Elliott4e19ed02015-04-28 10:52:52 -060027#define APP_SHORT_NAME "cube"
28#define APP_LONG_NAME "The Vulkan Cube Demo Program"
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -060029
Tony Barbour22a30862015-04-22 09:02:32 -060030#if defined(NDEBUG) && defined(__GNUC__)
31#define U_ASSERT_ONLY __attribute__((unused))
32#else
33#define U_ASSERT_ONLY
34#endif
35
Ian Elliottcaa9f272015-04-28 11:35:02 -060036#ifdef _WIN32
37#define ERR_EXIT(err_msg, err_class) \
38 do { \
39 MessageBox(NULL, err_msg, err_class, MB_OK); \
40 exit(1); \
41 } while (0)
42
43// NOTE: If the following values (copied from "loader_platform.h") change, they
44// need to change here as well:
45#define LAYER_NAMES_ENV "VK_LAYER_NAMES"
46#define LAYER_NAMES_REGISTRY_VALUE "VK_LAYER_NAMES"
47
48#else // _WIN32
49
50#define ERR_EXIT(err_msg, err_class) \
51 do { \
52 printf(err_msg); \
53 fflush(stdout); \
54 exit(1); \
55 } while (0)
56#endif // _WIN32
57
Courtney Goeltzenleuchter7e334982014-10-30 15:14:16 -060058/*
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -070059 * structure to track all objects related to a texture.
60 */
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -060061struct texture_object {
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -060062 VkSampler sampler;
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -070063
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -060064 VkImage image;
65 VkImageLayout imageLayout;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -060066
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -070067 uint32_t num_mem;
Tony Barbour8205d902015-04-16 15:59:00 -060068 VkDeviceMemory *mem;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -060069 VkImageView view;
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -070070 int32_t tex_width, tex_height;
71};
72
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -060073static char *tex_files[] = {
Courtney Goeltzenleuchter7e334982014-10-30 15:14:16 -060074 "lunarg-logo-256x256-solid.png"
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -060075};
76
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -060077struct vkcube_vs_uniform {
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -060078 // Must start with MVP
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -060079 float mvp[4][4];
80 float position[12*3][4];
81 float color[12*3][4];
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -060082};
83
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -060084struct vktexcube_vs_uniform {
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -060085 // Must start with MVP
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -060086 float mvp[4][4];
87 float position[12*3][4];
88 float attr[12*3][4];
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -060089};
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -060090
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -060091//--------------------------------------------------------------------------------------
92// Mesh and VertexFormat Data
93//--------------------------------------------------------------------------------------
94struct Vertex
95{
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -060096 float posX, posY, posZ, posW; // Position data
97 float r, g, b, a; // Color
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -060098};
99
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -0600100struct VertexPosTex
101{
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600102 float posX, posY, posZ, posW; // Position data
103 float u, v, s, t; // Texcoord
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -0600104};
105
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600106#define XYZ1(_x_, _y_, _z_) (_x_), (_y_), (_z_), 1.f
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -0600107#define UV(_u_, _v_) (_u_), (_v_), 0.f, 1.f
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600108
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600109static const float g_vertex_buffer_data[] = {
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600110 -1.0f,-1.0f,-1.0f, // Vertex 0
111 -1.0f,-1.0f, 1.0f,
112 -1.0f, 1.0f, 1.0f,
113
114 -1.0f, 1.0f, 1.0f, // Vertex 1
115 -1.0f, 1.0f,-1.0f,
116 -1.0f,-1.0f,-1.0f,
117
118 -1.0f,-1.0f,-1.0f, // Vertex 2
119 1.0f, 1.0f,-1.0f,
120 1.0f,-1.0f,-1.0f,
121
122 -1.0f,-1.0f,-1.0f, // Vertex 3
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600123 -1.0f, 1.0f,-1.0f,
Mike Stroyanea3945c2015-03-19 14:29:04 -0600124 1.0f, 1.0f,-1.0f,
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600125
126 -1.0f,-1.0f,-1.0f, // Vertex 4
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600127 1.0f,-1.0f,-1.0f,
Mike Stroyanea3945c2015-03-19 14:29:04 -0600128 1.0f,-1.0f, 1.0f,
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600129
130 -1.0f,-1.0f,-1.0f, // Vertex 5
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600131 1.0f,-1.0f, 1.0f,
Mike Stroyanea3945c2015-03-19 14:29:04 -0600132 -1.0f,-1.0f, 1.0f,
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600133
134 -1.0f, 1.0f,-1.0f, // Vertex 6
135 -1.0f, 1.0f, 1.0f,
136 1.0f, 1.0f, 1.0f,
137
138 -1.0f, 1.0f,-1.0f, // Vertex 7
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600139 1.0f, 1.0f, 1.0f,
Mike Stroyanea3945c2015-03-19 14:29:04 -0600140 1.0f, 1.0f,-1.0f,
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600141
142 1.0f, 1.0f,-1.0f, // Vertex 8
143 1.0f, 1.0f, 1.0f,
144 1.0f,-1.0f, 1.0f,
145
146 1.0f,-1.0f, 1.0f, // Vertex 9
147 1.0f,-1.0f,-1.0f,
148 1.0f, 1.0f,-1.0f,
149
150 -1.0f, 1.0f, 1.0f, // Vertex 10
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600151 -1.0f,-1.0f, 1.0f,
Mike Stroyanea3945c2015-03-19 14:29:04 -0600152 1.0f, 1.0f, 1.0f,
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600153
154 -1.0f,-1.0f, 1.0f, // Vertex 11
155 1.0f,-1.0f, 1.0f,
156 1.0f, 1.0f, 1.0f,
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -0600157};
158
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600159static const float g_uv_buffer_data[] = {
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -0600160 1.0f, 0.0f, // Vertex 0
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600161 0.0f, 0.0f,
162 0.0f, 1.0f,
163
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -0600164 0.0f, 1.0f, // Vertex 1
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600165 1.0f, 1.0f,
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -0600166 1.0f, 0.0f,
167
168// 0.0f, 1.0f, // Vertex 2
169// 1.0f, 0.0f,
170// 0.0f, 0.0f,
171
172// 0.0f, 1.0f, // Vertex 3
173// 1.0f, 0.0f,
174// 1.0f, 1.0f,
175
176 0.0f, 0.0f, // Vertex 2
177 1.0f, 1.0f,
178 1.0f, 0.0f,
179
180 0.0f, 0.0f, // Vertex 3
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -0600181 0.0f, 1.0f,
Mike Stroyanea3945c2015-03-19 14:29:04 -0600182 1.0f, 1.0f,
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600183
184 0.0f, 1.0f, // Vertex 4
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600185 0.0f, 0.0f,
Mike Stroyanea3945c2015-03-19 14:29:04 -0600186 1.0f, 0.0f,
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600187
188 0.0f, 1.0f, // Vertex 5
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600189 1.0f, 0.0f,
Mike Stroyanea3945c2015-03-19 14:29:04 -0600190 1.0f, 1.0f,
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600191
192 0.0f, 1.0f, // Vertex 6
193 1.0f, 1.0f,
194 1.0f, 0.0f,
195
196 0.0f, 1.0f, // Vertex 7
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600197 1.0f, 0.0f,
Mike Stroyanea3945c2015-03-19 14:29:04 -0600198 0.0f, 0.0f,
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600199
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -0600200 0.0f, 1.0f, // Vertex 8
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600201 1.0f, 1.0f,
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600202 1.0f, 0.0f,
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -0600203
204 1.0f, 0.0f, // Vertex 9
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600205 0.0f, 0.0f,
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600206 0.0f, 1.0f,
207
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -0600208 1.0f, 1.0f, // Vertex 10
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600209 1.0f, 0.0f,
Mike Stroyanea3945c2015-03-19 14:29:04 -0600210 0.0f, 1.0f,
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -0600211
212 1.0f, 0.0f, // Vertex 11
213 0.0f, 0.0f,
214 0.0f, 1.0f,
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -0600215};
216
217void dumpMatrix(const char *note, mat4x4 MVP)
218{
219 int i;
220
221 printf("%s: \n", note);
222 for (i=0; i<4; i++) {
223 printf("%f, %f, %f, %f\n", MVP[i][0], MVP[i][1], MVP[i][2], MVP[i][3]);
224 }
225 printf("\n");
226 fflush(stdout);
227}
228
229void dumpVec4(const char *note, vec4 vector)
230{
231 printf("%s: \n", note);
232 printf("%f, %f, %f, %f\n", vector[0], vector[1], vector[2], vector[3]);
233 printf("\n");
234 fflush(stdout);
235}
236
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600237struct demo {
Ian Elliotte14e9f92015-04-16 15:23:05 -0600238#ifdef _WIN32
239#define APP_NAME_STR_LEN 80
240 HINSTANCE connection; // hInstance - Windows Instance
241 char name[APP_NAME_STR_LEN]; // Name to put on the window/icon
242 HWND window; // hWnd - window handle
243#else // _WIN32
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600244 xcb_connection_t *connection;
245 xcb_screen_t *screen;
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800246 xcb_window_t window;
247 xcb_intern_atom_reply_t *atom_wm_delete_window;
Ian Elliotte14e9f92015-04-16 15:23:05 -0600248#endif // _WIN32
Jon Ashburn8a399e92015-04-24 09:46:24 -0700249 bool prepared;
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700250 bool use_staging_buffer;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600251
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600252 VkInstance inst;
Tony Barbour8205d902015-04-16 15:59:00 -0600253 VkPhysicalDevice gpu;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600254 VkDevice device;
255 VkQueue queue;
Courtney Goeltzenleuchterf3168062015-03-05 18:09:39 -0700256 uint32_t graphics_queue_node_index;
Tony Barbour8205d902015-04-16 15:59:00 -0600257 VkPhysicalDeviceProperties *gpu_props;
258 VkPhysicalDeviceQueueProperties *queue_props;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600259
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600260 VkFramebuffer framebuffer;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600261 int width, height;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600262 VkFormat format;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600263
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800264 VkSwapChainWSI swap_chain;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600265 struct {
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600266 VkImage image;
Tony Barbour8205d902015-04-16 15:59:00 -0600267 VkDeviceMemory mem;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600268 VkCmdBuffer cmd;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600269
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600270 VkColorAttachmentView view;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600271 } buffers[DEMO_BUFFER_COUNT];
272
273 struct {
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600274 VkFormat format;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600275
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600276 VkImage image;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600277 uint32_t num_mem;
Tony Barbour8205d902015-04-16 15:59:00 -0600278 VkDeviceMemory *mem;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600279 VkDepthStencilView view;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600280 } depth;
281
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600282 struct texture_object textures[DEMO_TEXTURE_COUNT];
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600283
284 struct {
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600285 VkBuffer buf;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600286 uint32_t num_mem;
Tony Barbour8205d902015-04-16 15:59:00 -0600287 VkDeviceMemory *mem;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600288 VkBufferView view;
289 VkBufferViewAttachInfo attach;
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -0600290 } uniform_data;
291
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600292 VkCmdBuffer cmd; // Buffer for initialization commands
Mark Lobodzinski556f7212015-04-17 14:11:39 -0500293 VkPipelineLayout pipeline_layout;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600294 VkDescriptorSetLayout desc_layout;
295 VkPipeline pipeline;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600296
Courtney Goeltzenleuchterfcf855f2015-04-10 16:24:50 -0600297 VkDynamicVpState viewport;
298 VkDynamicRsState raster;
299 VkDynamicCbState color_blend;
300 VkDynamicDsState depth_stencil;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600301
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -0600302 mat4x4 projection_matrix;
303 mat4x4 view_matrix;
304 mat4x4 model_matrix;
305
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600306 float spin_angle;
307 float spin_increment;
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -0600308 bool pause;
309
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600310 VkDescriptorPool desc_pool;
311 VkDescriptorSet desc_set;
Chia-I Wuf8385062015-01-04 16:27:24 +0800312
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600313 bool quit;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600314 uint32_t current_buffer;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600315};
316
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600317static void demo_flush_init_cmd(struct demo *demo)
318{
Tony Barbour22a30862015-04-22 09:02:32 -0600319 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600320
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600321 if (demo->cmd == VK_NULL_HANDLE)
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600322 return;
323
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600324 err = vkEndCommandBuffer(demo->cmd);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600325 assert(!err);
326
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600327 const VkCmdBuffer cmd_bufs[] = { demo->cmd };
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600328
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600329 err = vkQueueSubmit(demo->queue, 1, cmd_bufs, VK_NULL_HANDLE);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600330 assert(!err);
331
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600332 err = vkQueueWaitIdle(demo->queue);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600333 assert(!err);
334
Mike Stroyan230e6252015-04-17 12:36:38 -0600335 vkDestroyObject(demo->device, VK_OBJECT_TYPE_COMMAND_BUFFER, demo->cmd);
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600336 demo->cmd = VK_NULL_HANDLE;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600337}
338
339static void demo_add_mem_refs(
340 struct demo *demo,
Tony Barbour8205d902015-04-16 15:59:00 -0600341 int num_refs, VkDeviceMemory *mem)
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600342{
Courtney Goeltzenleuchter46962942015-04-16 13:38:46 -0600343 vkQueueAddMemReferences(demo->queue, num_refs, mem);
Courtney Goeltzenleuchter75538942015-04-02 16:25:42 -0600344}
345
346static void demo_remove_mem_refs(
347 struct demo *demo,
Tony Barbour8205d902015-04-16 15:59:00 -0600348 int num_refs, VkDeviceMemory *mem)
Courtney Goeltzenleuchter75538942015-04-02 16:25:42 -0600349{
Courtney Goeltzenleuchter46962942015-04-16 13:38:46 -0600350 vkQueueRemoveMemReferences(demo->queue, num_refs, mem);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600351}
352
353static void demo_set_image_layout(
354 struct demo *demo,
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600355 VkImage image,
356 VkImageLayout old_image_layout,
357 VkImageLayout new_image_layout)
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600358{
Tony Barbour22a30862015-04-22 09:02:32 -0600359 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600360
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600361 if (demo->cmd == VK_NULL_HANDLE) {
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600362 const VkCmdBufferCreateInfo cmd = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600363 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600364 .pNext = NULL,
365 .queueNodeIndex = demo->graphics_queue_node_index,
366 .flags = 0,
367 };
368
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600369 err = vkCreateCommandBuffer(demo->device, &cmd, &demo->cmd);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600370 assert(!err);
371
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600372 VkCmdBufferBeginInfo cmd_buf_info = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600373 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600374 .pNext = NULL,
Tony Barbour8205d902015-04-16 15:59:00 -0600375 .flags = VK_CMD_BUFFER_OPTIMIZE_SMALL_BATCH_BIT |
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600376 VK_CMD_BUFFER_OPTIMIZE_ONE_TIME_SUBMIT_BIT,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600377 };
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600378 err = vkBeginCommandBuffer(demo->cmd, &cmd_buf_info);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600379 }
380
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600381 VkImageMemoryBarrier image_memory_barrier = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600382 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600383 .pNext = NULL,
384 .outputMask = 0,
385 .inputMask = 0,
386 .oldLayout = old_image_layout,
387 .newLayout = new_image_layout,
388 .image = image,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600389 .subresourceRange = { VK_IMAGE_ASPECT_COLOR, 0, 1, 0, 0 }
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600390 };
391
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600392 if (new_image_layout == VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL) {
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600393 /* Make sure anything that was copying from this image has completed */
Courtney Goeltzenleuchterad870812015-04-15 15:29:59 -0600394 image_memory_barrier.inputMask = VK_MEMORY_INPUT_TRANSFER_BIT;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600395 }
396
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600397 if (new_image_layout == VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL) {
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600398 /* Make sure any Copy or CPU writes to image are flushed */
Courtney Goeltzenleuchterad870812015-04-15 15:29:59 -0600399 image_memory_barrier.outputMask = VK_MEMORY_OUTPUT_CPU_WRITE_BIT | VK_MEMORY_OUTPUT_TRANSFER_BIT;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600400 }
401
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600402 VkImageMemoryBarrier *pmemory_barrier = &image_memory_barrier;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600403
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600404 VkPipeEvent set_events[] = { VK_PIPE_EVENT_TOP_OF_PIPE };
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600405
Tony Barbour8205d902015-04-16 15:59:00 -0600406 vkCmdPipelineBarrier(demo->cmd, VK_WAIT_EVENT_TOP_OF_PIPE, 1, set_events, 1, (const void **)&pmemory_barrier);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600407}
408
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600409static void demo_draw_build_cmd(struct demo *demo, VkCmdBuffer cmd_buf)
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600410{
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600411 const VkColorAttachmentBindInfo color_attachment = {
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600412 .view = demo->buffers[demo->current_buffer].view,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600413 .layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600414 };
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600415 const VkDepthStencilBindInfo depth_stencil = {
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600416 .view = demo->depth.view,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600417 .layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600418 };
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600419 const VkClearColor clear_color = {
Courtney Goeltzenleuchter9a1ded82015-04-03 16:35:32 -0600420 .color.floatColor = { 0.2f, 0.2f, 0.2f, 0.2f },
421 .useRawValue = false,
422 };
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600423 const float clear_depth = 1.0f;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600424 VkImageSubresourceRange clear_range;
425 VkCmdBufferBeginInfo cmd_buf_info = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600426 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO,
Courtney Goeltzenleuchtere3b0f3a2015-04-03 15:25:24 -0600427 .pNext = NULL,
Tony Barbour8205d902015-04-16 15:59:00 -0600428 .flags = VK_CMD_BUFFER_OPTIMIZE_SMALL_BATCH_BIT |
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600429 VK_CMD_BUFFER_OPTIMIZE_ONE_TIME_SUBMIT_BIT,
Jon Ashburn53d27af2014-12-31 17:08:35 -0700430 };
Tony Barbour22a30862015-04-22 09:02:32 -0600431 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600432 VkAttachmentLoadOp load_op = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
433 VkAttachmentStoreOp store_op = VK_ATTACHMENT_STORE_OP_DONT_CARE;
434 const VkFramebufferCreateInfo fb_info = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600435 .sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO,
Jon Ashburn3325d6b2015-01-02 18:24:05 -0700436 .pNext = NULL,
437 .colorAttachmentCount = 1,
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600438 .pColorAttachments = (VkColorAttachmentBindInfo*) &color_attachment,
439 .pDepthStencilAttachment = (VkDepthStencilBindInfo*) &depth_stencil,
Jon Ashburn3325d6b2015-01-02 18:24:05 -0700440 .sampleCount = 1,
Mark Lobodzinski71fcc2d2015-01-27 13:24:03 -0600441 .width = demo->width,
442 .height = demo->height,
443 .layers = 1,
Jon Ashburn3325d6b2015-01-02 18:24:05 -0700444 };
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600445 VkRenderPassCreateInfo rp_info;
446 VkRenderPassBegin rp_begin;
Jon Ashburn3325d6b2015-01-02 18:24:05 -0700447
448 memset(&rp_info, 0 , sizeof(rp_info));
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600449 err = vkCreateFramebuffer(demo->device, &fb_info, &rp_begin.framebuffer);
Jon Ashburn3325d6b2015-01-02 18:24:05 -0700450 assert(!err);
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600451 rp_info.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO;
Jon Ashburn3325d6b2015-01-02 18:24:05 -0700452 rp_info.renderArea.extent.width = demo->width;
453 rp_info.renderArea.extent.height = demo->height;
Courtney Goeltzenleuchtere3b0f3a2015-04-03 15:25:24 -0600454 rp_info.colorAttachmentCount = fb_info.colorAttachmentCount;
455 rp_info.pColorFormats = &demo->format;
456 rp_info.pColorLayouts = &color_attachment.layout;
Jon Ashburn3325d6b2015-01-02 18:24:05 -0700457 rp_info.pColorLoadOps = &load_op;
458 rp_info.pColorStoreOps = &store_op;
Courtney Goeltzenleuchtere3b0f3a2015-04-03 15:25:24 -0600459 rp_info.pColorLoadClearValues = &clear_color;
Tony Barbour8205d902015-04-16 15:59:00 -0600460 rp_info.depthStencilFormat = VK_FORMAT_D16_UNORM;
Courtney Goeltzenleuchtere3b0f3a2015-04-03 15:25:24 -0600461 rp_info.depthStencilLayout = depth_stencil.layout;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600462 rp_info.depthLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
Courtney Goeltzenleuchtere3b0f3a2015-04-03 15:25:24 -0600463 rp_info.depthLoadClearValue = clear_depth;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600464 rp_info.depthStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
465 rp_info.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
Courtney Goeltzenleuchtere3b0f3a2015-04-03 15:25:24 -0600466 rp_info.stencilLoadClearValue = 0;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600467 rp_info.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
468 err = vkCreateRenderPass(demo->device, &rp_info, &rp_begin.renderPass);
Jon Ashburn3325d6b2015-01-02 18:24:05 -0700469 assert(!err);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600470
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600471 err = vkBeginCommandBuffer(cmd_buf, &cmd_buf_info);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600472 assert(!err);
473
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600474 vkCmdBindPipeline(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600475 demo->pipeline);
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600476 vkCmdBindDescriptorSets(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS,
Cody Northrop1a01b1d2015-04-16 13:41:56 -0600477 0, 1, &demo->desc_set, 0, NULL);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600478
Tony Barbour8205d902015-04-16 15:59:00 -0600479 vkCmdBindDynamicStateObject(cmd_buf, VK_STATE_BIND_POINT_VIEWPORT, demo->viewport);
480 vkCmdBindDynamicStateObject(cmd_buf, VK_STATE_BIND_POINT_RASTER, demo->raster);
481 vkCmdBindDynamicStateObject(cmd_buf, VK_STATE_BIND_POINT_COLOR_BLEND,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600482 demo->color_blend);
Tony Barbour8205d902015-04-16 15:59:00 -0600483 vkCmdBindDynamicStateObject(cmd_buf, VK_STATE_BIND_POINT_DEPTH_STENCIL,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600484 demo->depth_stencil);
485
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600486 vkCmdBeginRenderPass(cmd_buf, &rp_begin);
487 clear_range.aspect = VK_IMAGE_ASPECT_COLOR;
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -0600488 clear_range.baseMipLevel = 0;
489 clear_range.mipLevels = 1;
490 clear_range.baseArraySlice = 0;
491 clear_range.arraySize = 1;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600492 vkCmdClearColorImage(cmd_buf,
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -0600493 demo->buffers[demo->current_buffer].image,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600494 VK_IMAGE_LAYOUT_CLEAR_OPTIMAL,
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -0600495 clear_color, 1, &clear_range);
496
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600497 clear_range.aspect = VK_IMAGE_ASPECT_DEPTH;
498 vkCmdClearDepthStencil(cmd_buf, demo->depth.image,
499 VK_IMAGE_LAYOUT_CLEAR_OPTIMAL,
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -0600500 clear_depth, 0, 1, &clear_range);
501
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600502 vkCmdDraw(cmd_buf, 0, 12 * 3, 0, 1);
503 vkCmdEndRenderPass(cmd_buf, rp_begin.renderPass);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600504
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600505 err = vkEndCommandBuffer(cmd_buf);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600506 assert(!err);
Courtney Goeltzenleuchter04fc2fb2015-02-25 17:53:18 -0700507
Mike Stroyan230e6252015-04-17 12:36:38 -0600508 vkDestroyObject(demo->device, VK_OBJECT_TYPE_RENDER_PASS, rp_begin.renderPass);
509 vkDestroyObject(demo->device, VK_OBJECT_TYPE_FRAMEBUFFER, rp_begin.framebuffer);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600510}
511
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -0600512
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -0600513void demo_update_data_buffer(struct demo *demo)
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600514{
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -0600515 mat4x4 MVP, Model, VP;
516 int matrixSize = sizeof(MVP);
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600517 uint8_t *pData;
Tony Barbour22a30862015-04-22 09:02:32 -0600518 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600519
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -0600520 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600521
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -0600522 // Rotate 22.5 degrees around the Y axis
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -0600523 mat4x4_dup(Model, demo->model_matrix);
Piers Daniell886be472015-02-23 16:23:13 -0700524 mat4x4_rotate(demo->model_matrix, Model, 0.0f, 1.0f, 0.0f, (float)degreesToRadians(demo->spin_angle));
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -0600525 mat4x4_mul(MVP, VP, demo->model_matrix);
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -0600526
Jon Ashburnc6ae13d2015-01-19 15:00:26 -0700527 assert(demo->uniform_data.num_mem == 1);
Mike Stroyan230e6252015-04-17 12:36:38 -0600528 err = vkMapMemory(demo->device, demo->uniform_data.mem[0], 0, 0, 0, (void **) &pData);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600529 assert(!err);
530
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -0600531 memcpy(pData, (const void*) &MVP[0][0], matrixSize);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600532
Mike Stroyan230e6252015-04-17 12:36:38 -0600533 err = vkUnmapMemory(demo->device, demo->uniform_data.mem[0]);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600534 assert(!err);
535}
536
537static void demo_draw(struct demo *demo)
538{
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800539 const VkPresentInfoWSI present = {
540 .sType = VK_STRUCTURE_TYPE_PRESENT_INFO_WSI,
541 .pNext = NULL,
542 .image = demo->buffers[demo->current_buffer].image,
543 .flipInterval = 0,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600544 };
Tony Barbour22a30862015-04-22 09:02:32 -0600545 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600546
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600547 err = vkQueueSubmit(demo->queue, 1, &demo->buffers[demo->current_buffer].cmd,
548 VK_NULL_HANDLE);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600549 assert(!err);
550
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800551 err = vkQueuePresentWSI(demo->queue, &present);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600552 assert(!err);
553
554 demo->current_buffer = (demo->current_buffer + 1) % DEMO_BUFFER_COUNT;
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800555
556 err = vkQueueWaitIdle(demo->queue);
557 assert(err == VK_SUCCESS);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600558}
559
560static void demo_prepare_buffers(struct demo *demo)
561{
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800562 const VkSwapChainCreateInfoWSI swap_chain = {
563 .sType = VK_STRUCTURE_TYPE_SWAP_CHAIN_CREATE_INFO_WSI,
564 .pNext = NULL,
565 .pNativeWindowSystemHandle = demo->connection,
566 .pNativeWindowHandle = (void *) (intptr_t) demo->window,
567 .imageCount = DEMO_BUFFER_COUNT,
568 .imageFormat = demo->format,
569 .imageExtent = {
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600570 .width = demo->width,
571 .height = demo->height,
572 },
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800573 .imageArraySize = 1,
574 .imageUsageFlags = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600575 };
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800576 VkSwapChainImageInfoWSI images[DEMO_BUFFER_COUNT];
577 size_t images_size = sizeof(images);
Tony Barbour22a30862015-04-22 09:02:32 -0600578 VkResult U_ASSERT_ONLY err;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600579 uint32_t i;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600580
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800581 err = vkCreateSwapChainWSI(demo->device, &swap_chain, &demo->swap_chain);
582 assert(!err);
583
584 err = vkGetSwapChainInfoWSI(demo->swap_chain,
585 VK_SWAP_CHAIN_INFO_TYPE_PERSISTENT_IMAGES_WSI,
586 &images_size, images);
587 assert(!err && images_size == sizeof(images));
588
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600589 for (i = 0; i < DEMO_BUFFER_COUNT; i++) {
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600590 VkColorAttachmentViewCreateInfo color_attachment_view = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600591 .sType = VK_STRUCTURE_TYPE_COLOR_ATTACHMENT_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600592 .pNext = NULL,
593 .format = demo->format,
594 .mipLevel = 0,
595 .baseArraySlice = 0,
596 .arraySize = 1,
597 };
598
Chia-I Wu5b66aa52015-04-16 22:02:10 +0800599 demo->buffers[i].image = images[i].image;
600 demo->buffers[i].mem = images[i].memory;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600601
Courtney Goeltzenleuchter8d49dbd2015-04-07 17:13:38 -0600602 demo_add_mem_refs(demo, 1, &demo->buffers[i].mem);
Courtney Goeltzenleuchter75538942015-04-02 16:25:42 -0600603
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600604 demo_set_image_layout(demo, demo->buffers[i].image,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600605 VK_IMAGE_LAYOUT_UNDEFINED,
606 VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600607
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600608 color_attachment_view.image = demo->buffers[i].image;
609
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600610 err = vkCreateColorAttachmentView(demo->device,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600611 &color_attachment_view, &demo->buffers[i].view);
612 assert(!err);
613 }
614}
615
616static void demo_prepare_depth(struct demo *demo)
617{
Tony Barbour8205d902015-04-16 15:59:00 -0600618 const VkFormat depth_format = VK_FORMAT_D16_UNORM;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600619 const VkImageCreateInfo image = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600620 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600621 .pNext = NULL,
Tony Barbour8205d902015-04-16 15:59:00 -0600622 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600623 .format = depth_format,
624 .extent = { demo->width, demo->height, 1 },
625 .mipLevels = 1,
626 .arraySize = 1,
627 .samples = 1,
Tony Barbour8205d902015-04-16 15:59:00 -0600628 .tiling = VK_IMAGE_TILING_OPTIMAL,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600629 .usage = VK_IMAGE_USAGE_DEPTH_STENCIL_BIT,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600630 .flags = 0,
631 };
Courtney Goeltzenleuchterddcb6192015-04-14 18:48:46 -0600632 VkMemoryAllocInfo mem_alloc = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600633 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
Mark Lobodzinski97dcd042015-04-16 08:52:00 -0500634 .pNext = NULL,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600635 .allocationSize = 0,
Tony Barbour8205d902015-04-16 15:59:00 -0600636 .memProps = VK_MEMORY_PROPERTY_DEVICE_ONLY,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600637 .memPriority = VK_MEMORY_PRIORITY_NORMAL,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600638 };
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600639 VkDepthStencilViewCreateInfo view = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600640 .sType = VK_STRUCTURE_TYPE_DEPTH_STENCIL_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600641 .pNext = NULL,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600642 .image = VK_NULL_HANDLE,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600643 .mipLevel = 0,
644 .baseArraySlice = 0,
645 .arraySize = 1,
646 .flags = 0,
647 };
Mike Stroyan230e6252015-04-17 12:36:38 -0600648
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600649 VkMemoryRequirements *mem_reqs;
650 size_t mem_reqs_size = sizeof(VkMemoryRequirements);
Tony Barbour22a30862015-04-22 09:02:32 -0600651 VkResult U_ASSERT_ONLY err;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600652 uint32_t num_allocations = 0;
653 size_t num_alloc_size = sizeof(num_allocations);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600654
655 demo->depth.format = depth_format;
656
657 /* create image */
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600658 err = vkCreateImage(demo->device, &image,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600659 &demo->depth.image);
660 assert(!err);
661
Mike Stroyan230e6252015-04-17 12:36:38 -0600662 err = vkGetObjectInfo(demo->device,
663 VK_OBJECT_TYPE_IMAGE, demo->depth.image,
664 VK_OBJECT_INFO_TYPE_MEMORY_ALLOCATION_COUNT,
665 &num_alloc_size, &num_allocations);
Jon Ashburna9ae3832015-01-16 09:37:43 -0700666 assert(!err && num_alloc_size == sizeof(num_allocations));
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600667 mem_reqs = malloc(num_allocations * sizeof(VkMemoryRequirements));
Tony Barbour8205d902015-04-16 15:59:00 -0600668 demo->depth.mem = malloc(num_allocations * sizeof(VkDeviceMemory));
Jon Ashburna9ae3832015-01-16 09:37:43 -0700669 demo->depth.num_mem = num_allocations;
Mike Stroyan230e6252015-04-17 12:36:38 -0600670 err = vkGetObjectInfo(demo->device,
671 VK_OBJECT_TYPE_IMAGE, demo->depth.image,
Tony Barbour8205d902015-04-16 15:59:00 -0600672 VK_OBJECT_INFO_TYPE_MEMORY_REQUIREMENTS,
Jon Ashburna9ae3832015-01-16 09:37:43 -0700673 &mem_reqs_size, mem_reqs);
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600674 assert(!err && mem_reqs_size == num_allocations * sizeof(VkMemoryRequirements));
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600675 for (uint32_t i = 0; i < num_allocations; i ++) {
Jon Ashburna9ae3832015-01-16 09:37:43 -0700676 mem_alloc.allocationSize = mem_reqs[i].size;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600677
Jon Ashburna9ae3832015-01-16 09:37:43 -0700678 /* allocate memory */
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600679 err = vkAllocMemory(demo->device, &mem_alloc,
Jon Ashburna9ae3832015-01-16 09:37:43 -0700680 &(demo->depth.mem[i]));
681 assert(!err);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600682
Jon Ashburna9ae3832015-01-16 09:37:43 -0700683 /* bind memory */
Mike Stroyan230e6252015-04-17 12:36:38 -0600684 err = vkQueueBindObjectMemory(demo->queue,
685 VK_OBJECT_TYPE_IMAGE, demo->depth.image,
686 i, demo->depth.mem[i], 0);
Jon Ashburna9ae3832015-01-16 09:37:43 -0700687 assert(!err);
688 }
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600689
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600690 demo_set_image_layout(demo, demo->depth.image,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600691 VK_IMAGE_LAYOUT_UNDEFINED,
692 VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600693
Courtney Goeltzenleuchter8d49dbd2015-04-07 17:13:38 -0600694 demo_add_mem_refs(demo, demo->depth.num_mem, demo->depth.mem);
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600695
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600696 /* create image view */
697 view.image = demo->depth.image;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600698 err = vkCreateDepthStencilView(demo->device, &view,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600699 &demo->depth.view);
700 assert(!err);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600701}
702
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600703/** loadTexture
Courtney Goeltzenleuchtercb67a322015-04-21 09:31:23 -0600704 * loads a png file into an memory object, using cstdio , libpng.
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600705 *
Courtney Goeltzenleuchtercb67a322015-04-21 09:31:23 -0600706 * \param demo : Needed to access VK calls
707 * \param filename : the png file to be loaded
708 * \param width : width of png, to be updated as a side effect of this function
709 * \param height : height of png, to be updated as a side effect of this function
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600710 *
Courtney Goeltzenleuchtercb67a322015-04-21 09:31:23 -0600711 * \return bool : an opengl texture id. true if successful?,
712 * should be validated by the client of this function.
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600713 *
714 * Source: http://en.wikibooks.org/wiki/OpenGL_Programming/Intermediate/Textures
715 * Modified to copy image to memory
716 *
717 */
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700718bool loadTexture(const char *filename, uint8_t *rgba_data,
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600719 VkSubresourceLayout *layout,
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600720 int32_t *width, int32_t *height)
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600721{
722 //header for testing if it is a png
723 png_byte header[8];
Tony Barboura938abb2015-04-22 11:36:22 -0600724 int is_png, bit_depth, color_type, rowbytes;
725 size_t retval;
Ian Elliott642f8922015-02-13 14:29:21 -0700726 png_uint_32 i, twidth, theight;
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600727 png_structp png_ptr;
728 png_infop info_ptr, end_info;
729 png_byte *image_data;
730 png_bytep *row_pointers;
731
732 //open file as binary
733 FILE *fp = fopen(filename, "rb");
734 if (!fp) {
735 return false;
736 }
737
738 //read the header
Tony Barbour22a30862015-04-22 09:02:32 -0600739 retval = fread(header, 1, 8, fp);
740 if (retval != 8) {
741 fclose(fp);
742 return false;
743 }
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600744
745 //test if png
746 is_png = !png_sig_cmp(header, 0, 8);
747 if (!is_png) {
748 fclose(fp);
749 return false;
750 }
751
752 //create png struct
753 png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL,
754 NULL, NULL);
755 if (!png_ptr) {
756 fclose(fp);
757 return (false);
758 }
759
760 //create png info struct
761 info_ptr = png_create_info_struct(png_ptr);
762 if (!info_ptr) {
763 png_destroy_read_struct(&png_ptr, (png_infopp) NULL, (png_infopp) NULL);
764 fclose(fp);
765 return (false);
766 }
767
768 //create png info struct
769 end_info = png_create_info_struct(png_ptr);
770 if (!end_info) {
771 png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp) NULL);
772 fclose(fp);
773 return (false);
774 }
775
776 //png error stuff, not sure libpng man suggests this.
777 if (setjmp(png_jmpbuf(png_ptr))) {
778 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
779 fclose(fp);
780 return (false);
781 }
782
783 //init png reading
784 png_init_io(png_ptr, fp);
785
786 //let libpng know you already read the first 8 bytes
787 png_set_sig_bytes(png_ptr, 8);
788
789 // read all the info up to the image data
790 png_read_info(png_ptr, info_ptr);
791
792 // get info about png
793 png_get_IHDR(png_ptr, info_ptr, &twidth, &theight, &bit_depth, &color_type,
794 NULL, NULL, NULL);
795
796 //update width and height based on png info
797 *width = twidth;
798 *height = theight;
799
800 // Require that incoming texture be 8bits per color component
801 // and 4 components (RGBA).
802 if (png_get_bit_depth(png_ptr, info_ptr) != 8 ||
803 png_get_channels(png_ptr, info_ptr) != 4) {
804 return false;
805 }
806
807 if (rgba_data == NULL) {
808 // If data pointer is null, we just want the width & height
809 // clean up memory and close stuff
810 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
811 fclose(fp);
812
813 return true;
814 }
815
816 // Update the png info struct.
817 png_read_update_info(png_ptr, info_ptr);
818
819 // Row size in bytes.
820 rowbytes = png_get_rowbytes(png_ptr, info_ptr);
821
822 // Allocate the image_data as a big block, to be given to opengl
823 image_data = (png_byte *)malloc(rowbytes * theight * sizeof(png_byte));
824 if (!image_data) {
825 //clean up memory and close stuff
826 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
827 fclose(fp);
828 return false;
829 }
830
831 // row_pointers is for pointing to image_data for reading the png with libpng
832 row_pointers = (png_bytep *)malloc(theight * sizeof(png_bytep));
833 if (!row_pointers) {
834 //clean up memory and close stuff
835 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
836 // delete[] image_data;
837 fclose(fp);
838 return false;
839 }
840 // set the individual row_pointers to point at the correct offsets of image_data
841 for (i = 0; i < theight; ++i)
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700842 row_pointers[theight - 1 - i] = rgba_data + i * layout->rowPitch;
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600843
844 // read the png into image_data through row_pointers
845 png_read_image(png_ptr, row_pointers);
846
847 // clean up memory and close stuff
848 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
849 free(row_pointers);
850 free(image_data);
851 fclose(fp);
852
853 return true;
854}
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -0600855
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700856static void demo_prepare_texture_image(struct demo *demo,
857 const char *filename,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600858 struct texture_object *tex_obj,
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600859 VkImageTiling tiling,
Courtney Goeltzenleuchtercb67a322015-04-21 09:31:23 -0600860 VkImageUsageFlags usage,
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600861 VkFlags mem_props)
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600862{
Tony Barbour8205d902015-04-16 15:59:00 -0600863 const VkFormat tex_format = VK_FORMAT_B8G8R8A8_UNORM;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600864 int32_t tex_width;
865 int32_t tex_height;
Tony Barbour22a30862015-04-22 09:02:32 -0600866 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700867
David Pinedo61e77382015-04-23 08:16:57 -0600868 if (!loadTexture(filename, NULL, NULL, &tex_width, &tex_height))
869 {
870 printf("Failed to load textures\n");
871 fflush(stdout);
872 exit(1);
873 }
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700874
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600875 tex_obj->tex_width = tex_width;
876 tex_obj->tex_height = tex_height;
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700877
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600878 const VkImageCreateInfo image_create_info = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600879 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700880 .pNext = NULL,
Tony Barbour8205d902015-04-16 15:59:00 -0600881 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700882 .format = tex_format,
883 .extent = { tex_width, tex_height, 1 },
884 .mipLevels = 1,
885 .arraySize = 1,
886 .samples = 1,
887 .tiling = tiling,
Courtney Goeltzenleuchtercb67a322015-04-21 09:31:23 -0600888 .usage = usage,
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700889 .flags = 0,
890 };
Courtney Goeltzenleuchterddcb6192015-04-14 18:48:46 -0600891 VkMemoryAllocInfo mem_alloc = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600892 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
Mark Lobodzinski97dcd042015-04-16 08:52:00 -0500893 .pNext = NULL,
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700894 .allocationSize = 0,
895 .memProps = mem_props,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600896 .memPriority = VK_MEMORY_PRIORITY_NORMAL,
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700897 };
898
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600899 VkMemoryRequirements *mem_reqs;
900 size_t mem_reqs_size = sizeof(VkMemoryRequirements);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700901 uint32_t num_allocations = 0;
902 size_t num_alloc_size = sizeof(num_allocations);
903
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600904 err = vkCreateImage(demo->device, &image_create_info,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600905 &tex_obj->image);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700906 assert(!err);
907
Mike Stroyan230e6252015-04-17 12:36:38 -0600908 err = vkGetObjectInfo(demo->device,
909 VK_OBJECT_TYPE_IMAGE, tex_obj->image,
Tony Barbour8205d902015-04-16 15:59:00 -0600910 VK_OBJECT_INFO_TYPE_MEMORY_ALLOCATION_COUNT,
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700911 &num_alloc_size, &num_allocations);
912 assert(!err && num_alloc_size == sizeof(num_allocations));
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600913 mem_reqs = malloc(num_allocations * sizeof(VkMemoryRequirements));
Tony Barbour8205d902015-04-16 15:59:00 -0600914 tex_obj->mem = malloc(num_allocations * sizeof(VkDeviceMemory));
Mike Stroyan230e6252015-04-17 12:36:38 -0600915 err = vkGetObjectInfo(demo->device,
916 VK_OBJECT_TYPE_IMAGE, tex_obj->image,
Tony Barbour8205d902015-04-16 15:59:00 -0600917 VK_OBJECT_INFO_TYPE_MEMORY_REQUIREMENTS,
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700918 &mem_reqs_size, mem_reqs);
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600919 assert(!err && mem_reqs_size == num_allocations * sizeof(VkMemoryRequirements));
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700920 for (uint32_t j = 0; j < num_allocations; j ++) {
Piers Daniell886be472015-02-23 16:23:13 -0700921 mem_alloc.allocationSize = mem_reqs[j].size;
922
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700923 /* allocate memory */
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600924 err = vkAllocMemory(demo->device, &mem_alloc,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600925 &(tex_obj->mem[j]));
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700926 assert(!err);
927
928 /* bind memory */
Mike Stroyan230e6252015-04-17 12:36:38 -0600929 err = vkQueueBindObjectMemory(demo->queue,
930 VK_OBJECT_TYPE_IMAGE, tex_obj->image,
931 j, tex_obj->mem[j], 0);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700932 assert(!err);
933 }
934 free(mem_reqs);
935 mem_reqs = NULL;
936
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600937 tex_obj->num_mem = num_allocations;
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700938
Tony Barbour8205d902015-04-16 15:59:00 -0600939 if (mem_props & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) {
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600940 const VkImageSubresource subres = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600941 .aspect = VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700942 .mipLevel = 0,
943 .arraySlice = 0,
944 };
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600945 VkSubresourceLayout layout;
946 size_t layout_size = sizeof(VkSubresourceLayout);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700947 void *data;
948
Mike Stroyan230e6252015-04-17 12:36:38 -0600949 err = vkGetImageSubresourceInfo(demo->device, tex_obj->image, &subres,
Tony Barbour8205d902015-04-16 15:59:00 -0600950 VK_SUBRESOURCE_INFO_TYPE_LAYOUT,
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700951 &layout_size, &layout);
952 assert(!err && layout_size == sizeof(layout));
953 /* Linear texture must be within a single memory object */
954 assert(num_allocations == 1);
955
Mike Stroyan230e6252015-04-17 12:36:38 -0600956 err = vkMapMemory(demo->device, tex_obj->mem[0], 0, 0, 0, &data);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700957 assert(!err);
958
959 if (!loadTexture(filename, data, &layout, &tex_width, &tex_height)) {
960 fprintf(stderr, "Error loading texture: %s\n", filename);
961 }
962
Mike Stroyan230e6252015-04-17 12:36:38 -0600963 err = vkUnmapMemory(demo->device, tex_obj->mem[0]);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700964 assert(!err);
965 }
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600966
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600967 tex_obj->imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600968 demo_set_image_layout(demo, tex_obj->image,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600969 VK_IMAGE_LAYOUT_UNDEFINED,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -0600970 tex_obj->imageLayout);
971 /* setting the image layout does not reference the actual memory so no need to add a mem ref */
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700972}
973
Mark Lobodzinskicf26e072015-04-16 11:44:05 -0500974static void demo_destroy_texture_image(struct demo *demo, struct texture_object *tex_objs)
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700975{
976 /* clean up staging resources */
977 for (uint32_t j = 0; j < tex_objs->num_mem; j ++) {
Mike Stroyan230e6252015-04-17 12:36:38 -0600978 vkQueueBindObjectMemory(demo->queue,
979 VK_OBJECT_TYPE_IMAGE, tex_objs->image, j, VK_NULL_HANDLE, 0);
980 vkFreeMemory(demo->device, tex_objs->mem[j]);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700981 }
982
983 free(tex_objs->mem);
Mike Stroyan230e6252015-04-17 12:36:38 -0600984 vkDestroyObject(demo->device, VK_OBJECT_TYPE_IMAGE, tex_objs->image);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700985}
986
987static void demo_prepare_textures(struct demo *demo)
988{
Tony Barbour8205d902015-04-16 15:59:00 -0600989 const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -0600990 VkFormatProperties props;
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700991 size_t size = sizeof(props);
Tony Barbour22a30862015-04-22 09:02:32 -0600992 VkResult U_ASSERT_ONLY err;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600993 uint32_t i;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -0600994
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -0600995 err = vkGetFormatInfo(demo->device, tex_format,
Tony Barbour8205d902015-04-16 15:59:00 -0600996 VK_FORMAT_INFO_TYPE_PROPERTIES,
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -0700997 &size, &props);
998 assert(!err);
999
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001000 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001001
Tony Barbour8205d902015-04-16 15:59:00 -06001002 if (props.linearTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT && !demo->use_staging_buffer) {
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001003 /* Device can texture using linear textures */
1004 demo_prepare_texture_image(demo, tex_files[i], &demo->textures[i],
Courtney Goeltzenleuchtercb67a322015-04-21 09:31:23 -06001005 VK_IMAGE_TILING_LINEAR, VK_IMAGE_USAGE_SAMPLED_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
Tony Barbour8205d902015-04-16 15:59:00 -06001006 } else if (props.optimalTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) {
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001007 /* Must use staging buffer to copy linear texture to optimized */
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -06001008 struct texture_object staging_texture;
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001009
1010 memset(&staging_texture, 0, sizeof(staging_texture));
1011 demo_prepare_texture_image(demo, tex_files[i], &staging_texture,
Courtney Goeltzenleuchtercb67a322015-04-21 09:31:23 -06001012 VK_IMAGE_TILING_LINEAR, VK_IMAGE_USAGE_TRANSFER_SOURCE_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001013
1014 demo_prepare_texture_image(demo, tex_files[i], &demo->textures[i],
Courtney Goeltzenleuchtercb67a322015-04-21 09:31:23 -06001015 VK_IMAGE_TILING_OPTIMAL,
1016 (VK_IMAGE_USAGE_TRANSFER_DESTINATION_BIT | VK_IMAGE_USAGE_SAMPLED_BIT),
1017 VK_MEMORY_PROPERTY_DEVICE_ONLY);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001018
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -06001019 demo_set_image_layout(demo, staging_texture.image,
1020 staging_texture.imageLayout,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001021 VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001022
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -06001023 demo_set_image_layout(demo, demo->textures[i].image,
1024 demo->textures[i].imageLayout,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001025 VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001026
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001027 VkImageCopy copy_region = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001028 .srcSubresource = { VK_IMAGE_ASPECT_COLOR, 0, 0 },
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001029 .srcOffset = { 0, 0, 0 },
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001030 .destSubresource = { VK_IMAGE_ASPECT_COLOR, 0, 0 },
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001031 .destOffset = { 0, 0, 0 },
1032 .extent = { staging_texture.tex_width, staging_texture.tex_height, 1 },
1033 };
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001034 vkCmdCopyImage(demo->cmd,
1035 staging_texture.image, VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL,
1036 demo->textures[i].image, VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL,
Courtney Goeltzenleuchter51cbf302015-03-25 11:25:10 -06001037 1, &copy_region);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001038
Courtney Goeltzenleuchter8d49dbd2015-04-07 17:13:38 -06001039 demo_add_mem_refs(demo, staging_texture.num_mem, staging_texture.mem);
1040 demo_add_mem_refs(demo, demo->textures[i].num_mem, demo->textures[i].mem);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001041
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -06001042 demo_set_image_layout(demo, demo->textures[i].image,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001043 VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -06001044 demo->textures[i].imageLayout);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001045
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -06001046 demo_flush_init_cmd(demo);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001047
Courtney Goeltzenleuchter75538942015-04-02 16:25:42 -06001048 demo_remove_mem_refs(demo, staging_texture.num_mem, staging_texture.mem);
Courtney Goeltzenleuchter876629f2015-04-21 09:30:03 -06001049 demo_destroy_texture_image(demo, &staging_texture);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001050 } else {
Tony Barbour8205d902015-04-16 15:59:00 -06001051 /* Can't support VK_FORMAT_B8G8R8A8_UNORM !? */
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001052 assert(!"No support for tB8G8R8A8_UNORM as texture image format");
1053 }
1054
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001055 const VkSamplerCreateInfo sampler = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001056 .sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001057 .pNext = NULL,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001058 .magFilter = VK_TEX_FILTER_NEAREST,
1059 .minFilter = VK_TEX_FILTER_NEAREST,
Tony Barbour8205d902015-04-16 15:59:00 -06001060 .mipMode = VK_TEX_MIPMAP_MODE_BASE,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001061 .addressU = VK_TEX_ADDRESS_CLAMP,
1062 .addressV = VK_TEX_ADDRESS_CLAMP,
1063 .addressW = VK_TEX_ADDRESS_CLAMP,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001064 .mipLodBias = 0.0f,
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001065 .maxAnisotropy = 1,
Tony Barbour8205d902015-04-16 15:59:00 -06001066 .compareOp = VK_COMPARE_OP_NEVER,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001067 .minLod = 0.0f,
1068 .maxLod = 0.0f,
Tony Barbour8205d902015-04-16 15:59:00 -06001069 .borderColor = VK_BORDER_COLOR_OPAQUE_WHITE,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001070 };
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -06001071
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001072 VkImageViewCreateInfo view = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001073 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001074 .pNext = NULL,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001075 .image = VK_NULL_HANDLE,
Tony Barbour8205d902015-04-16 15:59:00 -06001076 .viewType = VK_IMAGE_VIEW_TYPE_2D,
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001077 .format = tex_format,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001078 .channels = { VK_CHANNEL_SWIZZLE_R,
1079 VK_CHANNEL_SWIZZLE_G,
1080 VK_CHANNEL_SWIZZLE_B,
1081 VK_CHANNEL_SWIZZLE_A, },
1082 .subresourceRange = { VK_IMAGE_ASPECT_COLOR, 0, 1, 0, 1 },
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001083 .minLod = 0.0f,
1084 };
Jon Ashburna9ae3832015-01-16 09:37:43 -07001085
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001086 /* create sampler */
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001087 err = vkCreateSampler(demo->device, &sampler,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001088 &demo->textures[i].sampler);
1089 assert(!err);
1090
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001091 /* create image view */
1092 view.image = demo->textures[i].image;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001093 err = vkCreateImageView(demo->device, &view,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001094 &demo->textures[i].view);
1095 assert(!err);
1096 }
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001097}
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001098
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001099void demo_prepare_cube_data_buffer(struct demo *demo)
1100{
Courtney Goeltzenleuchterddcb6192015-04-14 18:48:46 -06001101 VkBufferCreateInfo buf_info;
1102 VkBufferViewCreateInfo view_info;
Courtney Goeltzenleuchterddcb6192015-04-14 18:48:46 -06001103 VkMemoryAllocInfo alloc_info = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001104 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
Mark Lobodzinski97dcd042015-04-16 08:52:00 -05001105 .pNext = NULL,
Jon Ashburnc6ae13d2015-01-19 15:00:26 -07001106 .allocationSize = 0,
Tony Barbour8205d902015-04-16 15:59:00 -06001107 .memProps = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001108 .memPriority = VK_MEMORY_PRIORITY_NORMAL,
Jon Ashburnc6ae13d2015-01-19 15:00:26 -07001109 };
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001110 VkMemoryRequirements *mem_reqs;
1111 size_t mem_reqs_size = sizeof(VkMemoryRequirements);
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001112 uint32_t num_allocations = 0;
1113 size_t num_alloc_size = sizeof(num_allocations);
1114 uint8_t *pData;
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001115 int i;
1116 mat4x4 MVP, VP;
Tony Barbour22a30862015-04-22 09:02:32 -06001117 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001118 struct vktexcube_vs_uniform data;
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001119
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001120 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001121 mat4x4_mul(MVP, VP, demo->model_matrix);
1122 memcpy(data.mvp, MVP, sizeof(MVP));
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -06001123// dumpMatrix("MVP", MVP);
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001124
1125 for (i=0; i<12*3; i++) {
1126 data.position[i][0] = g_vertex_buffer_data[i*3];
1127 data.position[i][1] = g_vertex_buffer_data[i*3+1];
1128 data.position[i][2] = g_vertex_buffer_data[i*3+2];
1129 data.position[i][3] = 1.0f;
1130 data.attr[i][0] = g_uv_buffer_data[2*i];
1131 data.attr[i][1] = g_uv_buffer_data[2*i + 1];
1132 data.attr[i][2] = 0;
1133 data.attr[i][3] = 0;
1134 }
1135
Chia-I Wu714df452015-01-01 07:55:04 +08001136 memset(&buf_info, 0, sizeof(buf_info));
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001137 buf_info.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
Chia-I Wu714df452015-01-01 07:55:04 +08001138 buf_info.size = sizeof(data);
Courtney Goeltzenleuchterad870812015-04-15 15:29:59 -06001139 buf_info.usage = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001140 err = vkCreateBuffer(demo->device, &buf_info, &demo->uniform_data.buf);
Chia-I Wu714df452015-01-01 07:55:04 +08001141 assert(!err);
1142
Mike Stroyan230e6252015-04-17 12:36:38 -06001143 err = vkGetObjectInfo(demo->device,
1144 VK_OBJECT_TYPE_BUFFER, demo->uniform_data.buf,
Tony Barbour8205d902015-04-16 15:59:00 -06001145 VK_OBJECT_INFO_TYPE_MEMORY_ALLOCATION_COUNT,
Jon Ashburnc6ae13d2015-01-19 15:00:26 -07001146 &num_alloc_size, &num_allocations);
1147 assert(!err && num_alloc_size == sizeof(num_allocations));
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001148 mem_reqs = malloc(num_allocations * sizeof(VkMemoryRequirements));
Tony Barbour8205d902015-04-16 15:59:00 -06001149 demo->uniform_data.mem = malloc(num_allocations * sizeof(VkDeviceMemory));
Jon Ashburnc6ae13d2015-01-19 15:00:26 -07001150 demo->uniform_data.num_mem = num_allocations;
Mike Stroyan230e6252015-04-17 12:36:38 -06001151 err = vkGetObjectInfo(demo->device,
1152 VK_OBJECT_TYPE_BUFFER, demo->uniform_data.buf,
Tony Barbour8205d902015-04-16 15:59:00 -06001153 VK_OBJECT_INFO_TYPE_MEMORY_REQUIREMENTS,
Jon Ashburnc6ae13d2015-01-19 15:00:26 -07001154 &mem_reqs_size, mem_reqs);
1155 assert(!err && mem_reqs_size == num_allocations * sizeof(*mem_reqs));
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001156 for (uint32_t i = 0; i < num_allocations; i ++) {
Jon Ashburnc6ae13d2015-01-19 15:00:26 -07001157 alloc_info.allocationSize = mem_reqs[i].size;
Chia-I Wu714df452015-01-01 07:55:04 +08001158
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001159 err = vkAllocMemory(demo->device, &alloc_info, &(demo->uniform_data.mem[i]));
Jon Ashburnc6ae13d2015-01-19 15:00:26 -07001160 assert(!err);
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001161
Mike Stroyan230e6252015-04-17 12:36:38 -06001162 err = vkMapMemory(demo->device, demo->uniform_data.mem[i], 0, 0, 0, (void **) &pData);
Jon Ashburnc6ae13d2015-01-19 15:00:26 -07001163 assert(!err);
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001164
Courtney Goeltzenleuchtercb67a322015-04-21 09:31:23 -06001165 memcpy(pData, &data, sizeof data);
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001166
Mike Stroyan230e6252015-04-17 12:36:38 -06001167 err = vkUnmapMemory(demo->device, demo->uniform_data.mem[i]);
Jon Ashburnc6ae13d2015-01-19 15:00:26 -07001168 assert(!err);
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001169
Mike Stroyan230e6252015-04-17 12:36:38 -06001170 err = vkQueueBindObjectMemory(demo->queue,
1171 VK_OBJECT_TYPE_BUFFER, demo->uniform_data.buf,
1172 i, demo->uniform_data.mem[i], 0);
Jon Ashburnc6ae13d2015-01-19 15:00:26 -07001173 assert(!err);
1174 }
Courtney Goeltzenleuchter8d49dbd2015-04-07 17:13:38 -06001175 demo_add_mem_refs(demo, demo->uniform_data.num_mem, demo->uniform_data.mem);
Chia-I Wu714df452015-01-01 07:55:04 +08001176
1177 memset(&view_info, 0, sizeof(view_info));
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001178 view_info.sType = VK_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO;
Chia-I Wu714df452015-01-01 07:55:04 +08001179 view_info.buffer = demo->uniform_data.buf;
Tony Barbour8205d902015-04-16 15:59:00 -06001180 view_info.viewType = VK_BUFFER_VIEW_TYPE_RAW;
Chia-I Wu714df452015-01-01 07:55:04 +08001181 view_info.offset = 0;
1182 view_info.range = sizeof(data);
1183
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001184 err = vkCreateBufferView(demo->device, &view_info, &demo->uniform_data.view);
Chia-I Wu714df452015-01-01 07:55:04 +08001185 assert(!err);
1186
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001187 demo->uniform_data.attach.sType = VK_STRUCTURE_TYPE_BUFFER_VIEW_ATTACH_INFO;
Chia-I Wu714df452015-01-01 07:55:04 +08001188 demo->uniform_data.attach.view = demo->uniform_data.view;
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001189}
1190
Chia-I Wuf8385062015-01-04 16:27:24 +08001191static void demo_prepare_descriptor_layout(struct demo *demo)
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001192{
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001193 const VkDescriptorSetLayoutBinding layout_bindings[2] = {
Chia-I Wufc9d9132015-03-26 15:04:41 +08001194 [0] = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001195 .descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
Chia-I Wufc9d9132015-03-26 15:04:41 +08001196 .count = 1,
Tony Barbour8205d902015-04-16 15:59:00 -06001197 .stageFlags = VK_SHADER_STAGE_VERTEX_BIT,
Chia-I Wu310eece2015-03-27 12:56:09 +08001198 .pImmutableSamplers = NULL,
Chia-I Wufc9d9132015-03-26 15:04:41 +08001199 },
1200 [1] = {
Courtney Goeltzenleuchterad870812015-04-15 15:29:59 -06001201 .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
Chia-I Wufc9d9132015-03-26 15:04:41 +08001202 .count = DEMO_TEXTURE_COUNT,
Tony Barbour8205d902015-04-16 15:59:00 -06001203 .stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT,
Chia-I Wu310eece2015-03-27 12:56:09 +08001204 .pImmutableSamplers = NULL,
Chia-I Wufc9d9132015-03-26 15:04:41 +08001205 },
1206 };
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001207 const VkDescriptorSetLayoutCreateInfo descriptor_layout = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001208 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
Chia-I Wuf8385062015-01-04 16:27:24 +08001209 .pNext = NULL,
Chia-I Wufc9d9132015-03-26 15:04:41 +08001210 .count = 2,
1211 .pBinding = layout_bindings,
Chia-I Wuf8385062015-01-04 16:27:24 +08001212 };
Tony Barbour22a30862015-04-22 09:02:32 -06001213 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001214
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001215 err = vkCreateDescriptorSetLayout(demo->device,
Chia-I Wu7732cb22015-03-26 15:27:55 +08001216 &descriptor_layout, &demo->desc_layout);
1217 assert(!err);
1218
Mark Lobodzinski556f7212015-04-17 14:11:39 -05001219 const VkPipelineLayoutCreateInfo pPipelineLayoutCreateInfo = {
1220 .sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
1221 .pNext = NULL,
1222 .descriptorSetCount = 1,
1223 .pSetLayouts = &demo->desc_layout,
1224 };
1225
1226 err = vkCreatePipelineLayout(demo->device,
1227 &pPipelineLayoutCreateInfo,
1228 &demo->pipeline_layout);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001229 assert(!err);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001230}
1231
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001232static VkShader demo_prepare_shader(struct demo *demo,
Tony Barbour8205d902015-04-16 15:59:00 -06001233 VkShaderStage stage,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001234 const void *code,
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001235 size_t size)
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001236{
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001237 VkShaderCreateInfo createInfo;
1238 VkShader shader;
1239 VkResult err;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001240
Courtney Goeltzenleuchter7e334982014-10-30 15:14:16 -06001241
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001242 createInfo.sType = VK_STRUCTURE_TYPE_SHADER_CREATE_INFO;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001243 createInfo.pNext = NULL;
1244
Cody Northropacfb0492015-03-17 15:55:58 -06001245#ifdef EXTERNAL_SPV
Courtney Goeltzenleuchter7e334982014-10-30 15:14:16 -06001246 createInfo.codeSize = size;
1247 createInfo.pCode = code;
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -06001248 createInfo.flags = 0;
1249
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001250 err = vkCreateShader(demo->device, &createInfo, &shader);
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -06001251 if (err) {
1252 free((void *) createInfo.pCode);
1253 }
1254#else
Cody Northropacfb0492015-03-17 15:55:58 -06001255 // Create fake SPV structure to feed GLSL
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001256 // to the driver "under the covers"
1257 createInfo.codeSize = 3 * sizeof(uint32_t) + size + 1;
1258 createInfo.pCode = malloc(createInfo.codeSize);
1259 createInfo.flags = 0;
1260
Tony Barbour8205d902015-04-16 15:59:00 -06001261 /* try version 0 first: VkShaderStage followed by GLSL */
Cody Northropacfb0492015-03-17 15:55:58 -06001262 ((uint32_t *) createInfo.pCode)[0] = ICD_SPV_MAGIC;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001263 ((uint32_t *) createInfo.pCode)[1] = 0;
1264 ((uint32_t *) createInfo.pCode)[2] = stage;
1265 memcpy(((uint32_t *) createInfo.pCode + 3), code, size + 1);
1266
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001267 err = vkCreateShader(demo->device, &createInfo, &shader);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001268 if (err) {
1269 free((void *) createInfo.pCode);
Tony Barboura938abb2015-04-22 11:36:22 -06001270 return (VkShader) VK_NULL_HANDLE;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001271 }
Courtney Goeltzenleuchter17223602014-10-29 15:59:49 -06001272#endif
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001273
1274 return shader;
1275}
1276
Cody Northropacfb0492015-03-17 15:55:58 -06001277char *demo_read_spv(const char *filename, size_t *psize)
Courtney Goeltzenleuchter7e334982014-10-30 15:14:16 -06001278{
1279 long int size;
Tony Barboura938abb2015-04-22 11:36:22 -06001280 size_t U_ASSERT_ONLY retval;
Courtney Goeltzenleuchter7e334982014-10-30 15:14:16 -06001281 void *shader_code;
1282
1283 FILE *fp = fopen(filename, "rb");
1284 if (!fp) return NULL;
1285
1286 fseek(fp, 0L, SEEK_END);
1287 size = ftell(fp);
1288
1289 fseek(fp, 0L, SEEK_SET);
1290
1291 shader_code = malloc(size);
Tony Barbour22a30862015-04-22 09:02:32 -06001292 retval = fread(shader_code, size, 1, fp);
1293 assert(retval == 1);
Courtney Goeltzenleuchter7e334982014-10-30 15:14:16 -06001294
1295 *psize = size;
1296
1297 return shader_code;
1298}
1299
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001300static VkShader demo_prepare_vs(struct demo *demo)
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001301{
Cody Northropacfb0492015-03-17 15:55:58 -06001302#ifdef EXTERNAL_SPV
Courtney Goeltzenleuchter7e334982014-10-30 15:14:16 -06001303 void *vertShaderCode;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001304 size_t size;
Courtney Goeltzenleuchter7e334982014-10-30 15:14:16 -06001305
Cody Northropacfb0492015-03-17 15:55:58 -06001306 vertShaderCode = demo_read_spv("cube-vert.spv", &size);
Courtney Goeltzenleuchter7e334982014-10-30 15:14:16 -06001307
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001308 return demo_prepare_shader(demo, VK_SHADER_STAGE_VERTEX,
Courtney Goeltzenleuchter7e334982014-10-30 15:14:16 -06001309 vertShaderCode, size);
1310#else
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -06001311 static const char *vertShaderText =
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001312 "#version 140\n"
1313 "#extension GL_ARB_separate_shader_objects : enable\n"
1314 "#extension GL_ARB_shading_language_420pack : enable\n"
1315 "\n"
1316 "layout(binding = 0) uniform buf {\n"
1317 " mat4 MVP;\n"
1318 " vec4 position[12*3];\n"
1319 " vec4 attr[12*3];\n"
1320 "} ubuf;\n"
1321 "\n"
1322 "layout (location = 0) out vec4 texcoord;\n"
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001323 "\n"
1324 "void main() \n"
1325 "{\n"
1326 " texcoord = ubuf.attr[gl_VertexID];\n"
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001327 " gl_Position = ubuf.MVP * ubuf.position[gl_VertexID];\n"
1328 "}\n";
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -06001329
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001330 return demo_prepare_shader(demo, VK_SHADER_STAGE_VERTEX,
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -06001331 (const void *) vertShaderText,
1332 strlen(vertShaderText));
Courtney Goeltzenleuchter7e334982014-10-30 15:14:16 -06001333#endif
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001334}
1335
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001336static VkShader demo_prepare_fs(struct demo *demo)
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001337{
Cody Northropacfb0492015-03-17 15:55:58 -06001338#ifdef EXTERNAL_SPV
Courtney Goeltzenleuchter7e334982014-10-30 15:14:16 -06001339 void *fragShaderCode;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001340 size_t size;
Courtney Goeltzenleuchter7e334982014-10-30 15:14:16 -06001341
Cody Northropacfb0492015-03-17 15:55:58 -06001342 fragShaderCode = demo_read_spv("cube-frag.spv", &size);
Courtney Goeltzenleuchter7e334982014-10-30 15:14:16 -06001343
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001344 return demo_prepare_shader(demo, VK_SHADER_STAGE_FRAGMENT,
Courtney Goeltzenleuchter7e334982014-10-30 15:14:16 -06001345 fragShaderCode, size);
1346#else
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -06001347 static const char *fragShaderText =
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001348 "#version 140\n"
1349 "#extension GL_ARB_separate_shader_objects : enable\n"
1350 "#extension GL_ARB_shading_language_420pack : enable\n"
Courtney Goeltzenleuchter988cc782015-02-25 15:13:35 -07001351 "layout (binding = 1) uniform sampler2D tex;\n"
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001352 "\n"
1353 "layout (location = 0) in vec4 texcoord;\n"
1354 "void main() {\n"
1355 " gl_FragColor = texture(tex, texcoord.xy);\n"
1356 "}\n";
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -06001357
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001358 return demo_prepare_shader(demo, VK_SHADER_STAGE_FRAGMENT,
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -06001359 (const void *) fragShaderText,
1360 strlen(fragShaderText));
Courtney Goeltzenleuchter7e334982014-10-30 15:14:16 -06001361#endif
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001362}
1363
1364static void demo_prepare_pipeline(struct demo *demo)
1365{
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001366 VkGraphicsPipelineCreateInfo pipeline;
1367 VkPipelineIaStateCreateInfo ia;
1368 VkPipelineRsStateCreateInfo rs;
1369 VkPipelineCbStateCreateInfo cb;
1370 VkPipelineDsStateCreateInfo ds;
1371 VkPipelineShaderStageCreateInfo vs;
1372 VkPipelineShaderStageCreateInfo fs;
1373 VkPipelineVpStateCreateInfo vp;
1374 VkPipelineMsStateCreateInfo ms;
Tony Barbour22a30862015-04-22 09:02:32 -06001375 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001376
1377 memset(&pipeline, 0, sizeof(pipeline));
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001378 pipeline.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
Mark Lobodzinski556f7212015-04-17 14:11:39 -05001379 pipeline.layout = demo->pipeline_layout;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001380
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001381 memset(&ia, 0, sizeof(ia));
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001382 ia.sType = VK_STRUCTURE_TYPE_PIPELINE_IA_STATE_CREATE_INFO;
Tony Barbour8205d902015-04-16 15:59:00 -06001383 ia.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001384
1385 memset(&rs, 0, sizeof(rs));
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001386 rs.sType = VK_STRUCTURE_TYPE_PIPELINE_RS_STATE_CREATE_INFO;
Tony Barbour8205d902015-04-16 15:59:00 -06001387 rs.fillMode = VK_FILL_MODE_SOLID;
1388 rs.cullMode = VK_CULL_MODE_BACK;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001389 rs.frontFace = VK_FRONT_FACE_CCW;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001390
1391 memset(&cb, 0, sizeof(cb));
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001392 cb.sType = VK_STRUCTURE_TYPE_PIPELINE_CB_STATE_CREATE_INFO;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001393 VkPipelineCbAttachmentState att_state[1];
Tony Barbourfa6cac72015-01-16 14:27:35 -07001394 memset(att_state, 0, sizeof(att_state));
1395 att_state[0].format = demo->format;
1396 att_state[0].channelWriteMask = 0xf;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001397 att_state[0].blendEnable = VK_FALSE;
Tony Barbourfa6cac72015-01-16 14:27:35 -07001398 cb.attachmentCount = 1;
1399 cb.pAttachments = att_state;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001400
Tony Barbourfa6cac72015-01-16 14:27:35 -07001401 memset(&vp, 0, sizeof(vp));
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001402 vp.sType = VK_STRUCTURE_TYPE_PIPELINE_VP_STATE_CREATE_INFO;
Tony Barbour8205d902015-04-16 15:59:00 -06001403 vp.viewportCount = 1;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001404 vp.clipOrigin = VK_COORDINATE_ORIGIN_LOWER_LEFT;
Tony Barbourfa6cac72015-01-16 14:27:35 -07001405
1406 memset(&ds, 0, sizeof(ds));
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001407 ds.sType = VK_STRUCTURE_TYPE_PIPELINE_DS_STATE_CREATE_INFO;
Tony Barbourfa6cac72015-01-16 14:27:35 -07001408 ds.format = demo->depth.format;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001409 ds.depthTestEnable = VK_TRUE;
1410 ds.depthWriteEnable = VK_TRUE;
Tony Barbour8205d902015-04-16 15:59:00 -06001411 ds.depthCompareOp = VK_COMPARE_OP_LESS_EQUAL;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001412 ds.depthBoundsEnable = VK_FALSE;
1413 ds.back.stencilFailOp = VK_STENCIL_OP_KEEP;
1414 ds.back.stencilPassOp = VK_STENCIL_OP_KEEP;
Tony Barbour8205d902015-04-16 15:59:00 -06001415 ds.back.stencilCompareOp = VK_COMPARE_OP_ALWAYS;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001416 ds.stencilTestEnable = VK_FALSE;
Tony Barbourfa6cac72015-01-16 14:27:35 -07001417 ds.front = ds.back;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001418
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001419 memset(&vs, 0, sizeof(vs));
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001420 vs.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1421 vs.shader.stage = VK_SHADER_STAGE_VERTEX;
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001422 vs.shader.shader = demo_prepare_vs(demo);
Mike Stroyan230e6252015-04-17 12:36:38 -06001423 assert(vs.shader.shader != VK_NULL_HANDLE);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001424
1425 memset(&fs, 0, sizeof(fs));
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001426 fs.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1427 fs.shader.stage = VK_SHADER_STAGE_FRAGMENT;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001428 fs.shader.shader = demo_prepare_fs(demo);
Mike Stroyan230e6252015-04-17 12:36:38 -06001429 assert(fs.shader.shader != VK_NULL_HANDLE);
Tony Barbourfa6cac72015-01-16 14:27:35 -07001430
1431 memset(&ms, 0, sizeof(ms));
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001432 ms.sType = VK_STRUCTURE_TYPE_PIPELINE_MS_STATE_CREATE_INFO;
Tony Barbourfa6cac72015-01-16 14:27:35 -07001433 ms.sampleMask = 1;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001434 ms.multisampleEnable = VK_FALSE;
Tony Barbourfa6cac72015-01-16 14:27:35 -07001435 ms.samples = 1;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001436
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001437 pipeline.pNext = (const void *) &ia;
1438 ia.pNext = (const void *) &rs;
1439 rs.pNext = (const void *) &cb;
1440 cb.pNext = (const void *) &ms;
1441 ms.pNext = (const void *) &vp;
1442 vp.pNext = (const void *) &ds;
1443 ds.pNext = (const void *) &vs;
1444 vs.pNext = (const void *) &fs;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001445
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001446 err = vkCreateGraphicsPipeline(demo->device, &pipeline, &demo->pipeline);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001447 assert(!err);
1448
Mike Stroyan230e6252015-04-17 12:36:38 -06001449 vkDestroyObject(demo->device, VK_OBJECT_TYPE_SHADER, vs.shader.shader);
1450 vkDestroyObject(demo->device, VK_OBJECT_TYPE_SHADER, fs.shader.shader);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001451}
1452
1453static void demo_prepare_dynamic_states(struct demo *demo)
1454{
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001455 VkDynamicVpStateCreateInfo viewport_create;
1456 VkDynamicRsStateCreateInfo raster;
1457 VkDynamicCbStateCreateInfo color_blend;
1458 VkDynamicDsStateCreateInfo depth_stencil;
Tony Barbour22a30862015-04-22 09:02:32 -06001459 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001460
Tony Barbourfa6cac72015-01-16 14:27:35 -07001461 memset(&viewport_create, 0, sizeof(viewport_create));
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001462 viewport_create.sType = VK_STRUCTURE_TYPE_DYNAMIC_VP_STATE_CREATE_INFO;
Courtney Goeltzenleuchterc6e32f92015-02-11 14:13:34 -07001463 viewport_create.viewportAndScissorCount = 1;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001464 VkViewport viewport;
Piers Daniell886be472015-02-23 16:23:13 -07001465 memset(&viewport, 0, sizeof(viewport));
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001466 viewport.height = (float) demo->height;
1467 viewport.width = (float) demo->width;
1468 viewport.minDepth = (float) 0.0f;
1469 viewport.maxDepth = (float) 1.0f;
Piers Daniell886be472015-02-23 16:23:13 -07001470 viewport_create.pViewports = &viewport;
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001471 VkRect scissor;
Piers Daniell886be472015-02-23 16:23:13 -07001472 memset(&scissor, 0, sizeof(scissor));
Courtney Goeltzenleuchterc6e32f92015-02-11 14:13:34 -07001473 scissor.extent.width = demo->width;
1474 scissor.extent.height = demo->height;
1475 scissor.offset.x = 0;
1476 scissor.offset.y = 0;
Courtney Goeltzenleuchterc6e32f92015-02-11 14:13:34 -07001477 viewport_create.pScissors = &scissor;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001478
1479 memset(&raster, 0, sizeof(raster));
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001480 raster.sType = VK_STRUCTURE_TYPE_DYNAMIC_RS_STATE_CREATE_INFO;
Piers Daniell886be472015-02-23 16:23:13 -07001481 raster.pointSize = 1.0;
1482 raster.lineWidth = 1.0;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001483
1484 memset(&color_blend, 0, sizeof(color_blend));
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001485 color_blend.sType = VK_STRUCTURE_TYPE_DYNAMIC_CB_STATE_CREATE_INFO;
Piers Daniell886be472015-02-23 16:23:13 -07001486 color_blend.blendConst[0] = 1.0f;
1487 color_blend.blendConst[1] = 1.0f;
1488 color_blend.blendConst[2] = 1.0f;
1489 color_blend.blendConst[3] = 1.0f;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001490
1491 memset(&depth_stencil, 0, sizeof(depth_stencil));
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001492 depth_stencil.sType = VK_STRUCTURE_TYPE_DYNAMIC_DS_STATE_CREATE_INFO;
Piers Daniell886be472015-02-23 16:23:13 -07001493 depth_stencil.minDepth = 0.0f;
1494 depth_stencil.maxDepth = 1.0f;
Tony Barbourfa6cac72015-01-16 14:27:35 -07001495 depth_stencil.stencilBackRef = 0;
1496 depth_stencil.stencilFrontRef = 0;
1497 depth_stencil.stencilReadMask = 0xff;
1498 depth_stencil.stencilWriteMask = 0xff;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001499
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001500 err = vkCreateDynamicViewportState(demo->device, &viewport_create, &demo->viewport);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001501 assert(!err);
1502
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001503 err = vkCreateDynamicRasterState(demo->device, &raster, &demo->raster);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001504 assert(!err);
1505
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001506 err = vkCreateDynamicColorBlendState(demo->device,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001507 &color_blend, &demo->color_blend);
1508 assert(!err);
1509
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001510 err = vkCreateDynamicDepthStencilState(demo->device,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001511 &depth_stencil, &demo->depth_stencil);
1512 assert(!err);
1513}
1514
Chia-I Wu8d24b3b2015-03-26 13:14:16 +08001515static void demo_prepare_descriptor_pool(struct demo *demo)
Chia-I Wuf8385062015-01-04 16:27:24 +08001516{
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001517 const VkDescriptorTypeCount type_counts[2] = {
Chia-I Wuf8385062015-01-04 16:27:24 +08001518 [0] = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001519 .type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
Chia-I Wuf8385062015-01-04 16:27:24 +08001520 .count = 1,
1521 },
1522 [1] = {
Courtney Goeltzenleuchterad870812015-04-15 15:29:59 -06001523 .type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
Chia-I Wuf8385062015-01-04 16:27:24 +08001524 .count = DEMO_TEXTURE_COUNT,
1525 },
1526 };
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001527 const VkDescriptorPoolCreateInfo descriptor_pool = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001528 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO,
Chia-I Wuf8385062015-01-04 16:27:24 +08001529 .pNext = NULL,
1530 .count = 2,
1531 .pTypeCount = type_counts,
1532 };
Tony Barbour22a30862015-04-22 09:02:32 -06001533 VkResult U_ASSERT_ONLY err;
Chia-I Wuf8385062015-01-04 16:27:24 +08001534
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001535 err = vkCreateDescriptorPool(demo->device,
1536 VK_DESCRIPTOR_POOL_USAGE_ONE_SHOT, 1,
Chia-I Wu8d24b3b2015-03-26 13:14:16 +08001537 &descriptor_pool, &demo->desc_pool);
Chia-I Wuf8385062015-01-04 16:27:24 +08001538 assert(!err);
1539}
1540
1541static void demo_prepare_descriptor_set(struct demo *demo)
1542{
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001543 VkImageViewAttachInfo view_info[DEMO_TEXTURE_COUNT];
1544 VkSamplerImageViewInfo combined_info[DEMO_TEXTURE_COUNT];
1545 VkUpdateSamplerTextures update_fs;
1546 VkUpdateBuffers update_vs;
Chia-I Wu7732cb22015-03-26 15:27:55 +08001547 const void *update_array[2] = { &update_vs, &update_fs };
Tony Barbour22a30862015-04-22 09:02:32 -06001548 VkResult U_ASSERT_ONLY err;
Chia-I Wuf8385062015-01-04 16:27:24 +08001549 uint32_t count;
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001550 uint32_t i;
Chia-I Wuf8385062015-01-04 16:27:24 +08001551
1552 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001553 view_info[i].sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_ATTACH_INFO;
Chia-I Wuf8385062015-01-04 16:27:24 +08001554 view_info[i].pNext = NULL;
1555 view_info[i].view = demo->textures[i].view,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001556 view_info[i].layout = VK_IMAGE_LAYOUT_GENERAL;
Chia-I Wuf8385062015-01-04 16:27:24 +08001557
Courtney Goeltzenleuchterd38dcc92015-04-09 11:43:10 -06001558 combined_info[i].sampler = demo->textures[i].sampler;
Chia-I Wuf8385062015-01-04 16:27:24 +08001559 combined_info[i].pImageView = &view_info[i];
1560 }
1561
1562 memset(&update_vs, 0, sizeof(update_vs));
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001563 update_vs.sType = VK_STRUCTURE_TYPE_UPDATE_BUFFERS;
Chia-I Wuf8385062015-01-04 16:27:24 +08001564 update_vs.pNext = &update_fs;
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001565 update_vs.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
Chia-I Wuf8385062015-01-04 16:27:24 +08001566 update_vs.count = 1;
Chia-I Wu7732cb22015-03-26 15:27:55 +08001567 update_vs.pBufferViews = &demo->uniform_data.attach;
Chia-I Wuf8385062015-01-04 16:27:24 +08001568
1569 memset(&update_fs, 0, sizeof(update_fs));
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001570 update_fs.sType = VK_STRUCTURE_TYPE_UPDATE_SAMPLER_TEXTURES;
Chia-I Wu7732cb22015-03-26 15:27:55 +08001571 update_fs.binding = 1;
Chia-I Wuf8385062015-01-04 16:27:24 +08001572 update_fs.count = DEMO_TEXTURE_COUNT;
1573 update_fs.pSamplerImageViews = combined_info;
1574
Mike Stroyan230e6252015-04-17 12:36:38 -06001575 err = vkAllocDescriptorSets(demo->device, demo->desc_pool,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001576 VK_DESCRIPTOR_SET_USAGE_STATIC,
Chia-I Wu6e68a892015-02-23 10:41:08 -07001577 1, &demo->desc_layout,
Chia-I Wuf8385062015-01-04 16:27:24 +08001578 &demo->desc_set, &count);
1579 assert(!err && count == 1);
1580
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001581 vkBeginDescriptorPoolUpdate(demo->device,
1582 VK_DESCRIPTOR_UPDATE_MODE_FASTEST);
Chia-I Wuf8385062015-01-04 16:27:24 +08001583
Mike Stroyan230e6252015-04-17 12:36:38 -06001584 vkClearDescriptorSets(demo->device, demo->desc_pool, 1, &demo->desc_set);
1585 vkUpdateDescriptors(demo->device, demo->desc_set, 2, update_array);
Chia-I Wuf8385062015-01-04 16:27:24 +08001586
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001587 vkEndDescriptorPoolUpdate(demo->device, demo->buffers[demo->current_buffer].cmd);
Chia-I Wuf8385062015-01-04 16:27:24 +08001588}
1589
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001590static void demo_prepare(struct demo *demo)
1591{
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001592 const VkCmdBufferCreateInfo cmd = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001593 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001594 .pNext = NULL,
Courtney Goeltzenleuchterf3168062015-03-05 18:09:39 -07001595 .queueNodeIndex = demo->graphics_queue_node_index,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001596 .flags = 0,
1597 };
Tony Barbour22a30862015-04-22 09:02:32 -06001598 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001599
1600 demo_prepare_buffers(demo);
1601 demo_prepare_depth(demo);
1602 demo_prepare_textures(demo);
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -06001603 demo_prepare_cube_data_buffer(demo);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001604
Chia-I Wuf8385062015-01-04 16:27:24 +08001605 demo_prepare_descriptor_layout(demo);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001606 demo_prepare_pipeline(demo);
1607 demo_prepare_dynamic_states(demo);
1608
Courtney Goeltzenleuchterbd3b3aa2015-02-17 09:48:44 -07001609 for (int i = 0; i < DEMO_BUFFER_COUNT; i++) {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001610 err = vkCreateCommandBuffer(demo->device, &cmd, &demo->buffers[i].cmd);
Courtney Goeltzenleuchterbd3b3aa2015-02-17 09:48:44 -07001611 assert(!err);
1612 }
Chia-I Wuf8385062015-01-04 16:27:24 +08001613
Chia-I Wu8d24b3b2015-03-26 13:14:16 +08001614 demo_prepare_descriptor_pool(demo);
Chia-I Wuf8385062015-01-04 16:27:24 +08001615 demo_prepare_descriptor_set(demo);
Courtney Goeltzenleuchterbd3b3aa2015-02-17 09:48:44 -07001616
1617
1618 for (int i = 0; i < DEMO_BUFFER_COUNT; i++) {
1619 demo->current_buffer = i;
1620 demo_draw_build_cmd(demo, demo->buffers[i].cmd);
1621 }
1622
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -06001623 /*
1624 * Prepare functions above may generate pipeline commands
1625 * that need to be flushed before beginning the render loop.
1626 */
1627 demo_flush_init_cmd(demo);
1628
Courtney Goeltzenleuchterbd3b3aa2015-02-17 09:48:44 -07001629 demo->current_buffer = 0;
Jon Ashburn8a399e92015-04-24 09:46:24 -07001630 demo->prepared = true;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001631}
1632
Ian Elliotte14e9f92015-04-16 15:23:05 -06001633#ifdef _WIN32
1634static void demo_run(struct demo *demo)
1635{
Courtney Goeltzenleuchter857542b2015-04-27 14:56:34 -06001636 if (!demo->prepared)
1637 return;
Ian Elliotte14e9f92015-04-16 15:23:05 -06001638 // Wait for work to finish before updating MVP.
1639 vkDeviceWaitIdle(demo->device);
1640 demo_update_data_buffer(demo);
1641
1642 demo_draw(demo);
1643
1644 // Wait for work to finish before updating MVP.
1645 vkDeviceWaitIdle(demo->device);
1646}
1647
1648// On MS-Windows, make this a global, so it's available to WndProc()
1649struct demo demo;
1650
1651// MS-Windows event handling function:
1652LRESULT CALLBACK WndProc(HWND hWnd,
1653 UINT uMsg,
1654 WPARAM wParam,
1655 LPARAM lParam)
1656{
Ian Elliott4e19ed02015-04-28 10:52:52 -06001657 PAINTSTRUCT paint_struct;
1658 HDC hDC; // Device context
1659 char tmp_str[] = APP_LONG_NAME;
Ian Elliotte14e9f92015-04-16 15:23:05 -06001660
1661 switch(uMsg)
1662 {
1663 case WM_CREATE:
1664 return 0;
1665 case WM_CLOSE:
1666 PostQuitMessage(0);
1667 return 0;
1668 case WM_PAINT:
1669 demo_run(&demo);
1670 return 0;
1671 default:
1672 break;
1673 }
1674 return (DefWindowProc(hWnd, uMsg, wParam, lParam));
1675}
1676
1677static void demo_create_window(struct demo *demo)
1678{
1679 WNDCLASSEX win_class;
1680
1681 // Initialize the window class structure:
1682 win_class.cbSize = sizeof(WNDCLASSEX);
1683 win_class.style = CS_HREDRAW | CS_VREDRAW;
1684 win_class.lpfnWndProc = WndProc;
1685 win_class.cbClsExtra = 0;
1686 win_class.cbWndExtra = 0;
1687 win_class.hInstance = demo->connection; // hInstance
1688 win_class.hIcon = LoadIcon(NULL, IDI_APPLICATION);
1689 win_class.hCursor = LoadCursor(NULL, IDC_ARROW);
1690 win_class.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
1691 win_class.lpszMenuName = NULL;
1692 win_class.lpszClassName = demo->name;
1693 win_class.hIconSm = LoadIcon(NULL, IDI_WINLOGO);
1694 // Register window class:
1695 if (!RegisterClassEx(&win_class)) {
1696 // It didn't work, so try to give a useful error:
1697 printf("Unexpected error trying to start the application!\n");
1698 fflush(stdout);
1699 exit(1);
1700 }
1701 // Create window with the registered class:
1702 demo->window = CreateWindowEx(0,
1703 demo->name, // class name
1704 demo->name, // app name
1705 WS_OVERLAPPEDWINDOW | // window style
1706 WS_VISIBLE |
1707 WS_SYSMENU,
1708 100,100, // x/y coords
1709 demo->width, // width
1710 demo->height, // height
1711 NULL, // handle to parent
1712 NULL, // handle to menu
1713 demo->connection, // hInstance
1714 NULL); // no extra parameters
1715 if (!demo->window) {
1716 // It didn't work, so try to give a useful error:
1717 printf("Cannot create a window in which to draw!\n");
1718 fflush(stdout);
1719 exit(1);
1720 }
1721}
1722#else // _WIN32
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001723static void demo_handle_event(struct demo *demo,
1724 const xcb_generic_event_t *event)
1725{
Piers Daniell886be472015-02-23 16:23:13 -07001726 uint8_t event_code = event->response_type & 0x7f;
Courtney Goeltzenleuchterca21a212014-11-06 14:27:52 -07001727 switch (event_code) {
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001728 case XCB_EXPOSE:
Courtney Goeltzenleuchter54611482014-11-18 11:28:09 -07001729 // TODO: Resize window
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001730 break;
Courtney Goeltzenleuchterca21a212014-11-06 14:27:52 -07001731 case XCB_CLIENT_MESSAGE:
1732 if((*(xcb_client_message_event_t*)event).data.data32[0] ==
1733 (*demo->atom_wm_delete_window).atom) {
1734 demo->quit = true;
1735 }
1736 break;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001737 case XCB_KEY_RELEASE:
1738 {
1739 const xcb_key_release_event_t *key =
1740 (const xcb_key_release_event_t *) event;
1741
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -06001742 switch (key->detail) {
1743 case 0x9: // Escape
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001744 demo->quit = true;
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -06001745 break;
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001746 case 0x71: // left arrow key
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -06001747 demo->spin_angle += demo->spin_increment;
Courtney Goeltzenleuchtere3342402014-10-28 14:50:30 -06001748 break;
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -06001749 case 0x72: // right arrow key
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -06001750 demo->spin_angle -= demo->spin_increment;
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -06001751 break;
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -06001752 case 0x41:
1753 demo->pause = !demo->pause;
Piers Daniell886be472015-02-23 16:23:13 -07001754 break;
Courtney Goeltzenleuchter6f88d4c2014-10-28 10:32:57 -06001755 }
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001756 }
1757 break;
1758 default:
1759 break;
1760 }
1761}
1762
1763static void demo_run(struct demo *demo)
1764{
1765 xcb_flush(demo->connection);
1766
1767 while (!demo->quit) {
1768 xcb_generic_event_t *event;
1769
Courtney Goeltzenleuchter54611482014-11-18 11:28:09 -07001770 if (demo->pause) {
1771 event = xcb_wait_for_event(demo->connection);
1772 } else {
1773 event = xcb_poll_for_event(demo->connection);
1774 }
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001775 if (event) {
1776 demo_handle_event(demo, event);
1777 free(event);
Courtney Goeltzenleuchterbb3e1312014-11-10 11:13:13 -07001778 }
Courtney Goeltzenleuchter54611482014-11-18 11:28:09 -07001779
1780 // Wait for work to finish before updating MVP.
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001781 vkDeviceWaitIdle(demo->device);
Courtney Goeltzenleuchter54611482014-11-18 11:28:09 -07001782 demo_update_data_buffer(demo);
1783
Courtney Goeltzenleuchterbb3e1312014-11-10 11:13:13 -07001784 demo_draw(demo);
Courtney Goeltzenleuchter21f89972014-11-18 11:28:09 -07001785
Courtney Goeltzenleuchterbb3e1312014-11-10 11:13:13 -07001786 // Wait for work to finish before updating MVP.
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001787 vkDeviceWaitIdle(demo->device);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001788 }
1789}
1790
1791static void demo_create_window(struct demo *demo)
1792{
1793 uint32_t value_mask, value_list[32];
1794
1795 demo->window = xcb_generate_id(demo->connection);
1796
1797 value_mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK;
1798 value_list[0] = demo->screen->black_pixel;
1799 value_list[1] = XCB_EVENT_MASK_KEY_RELEASE |
1800 XCB_EVENT_MASK_EXPOSURE;
1801
1802 xcb_create_window(demo->connection,
1803 XCB_COPY_FROM_PARENT,
1804 demo->window, demo->screen->root,
1805 0, 0, demo->width, demo->height, 0,
1806 XCB_WINDOW_CLASS_INPUT_OUTPUT,
1807 demo->screen->root_visual,
1808 value_mask, value_list);
1809
Courtney Goeltzenleuchterca21a212014-11-06 14:27:52 -07001810 /* Magic code that will send notification when window is destroyed */
1811 xcb_intern_atom_cookie_t cookie = xcb_intern_atom(demo->connection, 1, 12,
1812 "WM_PROTOCOLS");
1813 xcb_intern_atom_reply_t* reply = xcb_intern_atom_reply(demo->connection, cookie, 0);
1814
1815 xcb_intern_atom_cookie_t cookie2 = xcb_intern_atom(demo->connection, 0, 16, "WM_DELETE_WINDOW");
1816 demo->atom_wm_delete_window = xcb_intern_atom_reply(demo->connection, cookie2, 0);
1817
1818 xcb_change_property(demo->connection, XCB_PROP_MODE_REPLACE,
1819 demo->window, (*reply).atom, 4, 32, 1,
1820 &(*demo->atom_wm_delete_window).atom);
1821 free(reply);
1822
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001823 xcb_map_window(demo->connection, demo->window);
1824}
Ian Elliotte14e9f92015-04-16 15:23:05 -06001825#endif // _WIN32
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001826
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001827static void demo_init_vk(struct demo *demo)
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001828{
Tobin Ehlis3536b442015-04-16 18:04:57 -06001829 VkResult err;
1830 // Extensions to enable
1831 const char *ext_names[] = {
Chia-I Wu5b66aa52015-04-16 22:02:10 +08001832 "VK_WSI_LunarG",
Tobin Ehlis3536b442015-04-16 18:04:57 -06001833 };
1834 size_t extSize = sizeof(uint32_t);
1835 uint32_t extCount = 0;
1836 err = vkGetGlobalExtensionInfo(VK_EXTENSION_INFO_TYPE_COUNT, 0, &extSize, &extCount);
1837 assert(!err);
1838
1839 VkExtensionProperties extProp;
1840 extSize = sizeof(VkExtensionProperties);
Tony Barbour22a30862015-04-22 09:02:32 -06001841 bool32_t U_ASSERT_ONLY extFound = 0;
Tobin Ehlis3536b442015-04-16 18:04:57 -06001842 for (uint32_t i = 0; i < extCount; i++) {
1843 err = vkGetGlobalExtensionInfo(VK_EXTENSION_INFO_TYPE_PROPERTIES, i, &extSize, &extProp);
1844 if (!strcmp(ext_names[0], extProp.extName))
1845 extFound = 1;
1846 }
1847 assert(extFound);
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -06001848 const VkApplicationInfo app = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001849 .sType = VK_STRUCTURE_TYPE_APPLICATION_INFO,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001850 .pNext = NULL,
Ian Elliott4e19ed02015-04-28 10:52:52 -06001851 .pAppName = APP_SHORT_NAME,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001852 .appVersion = 0,
Ian Elliott4e19ed02015-04-28 10:52:52 -06001853 .pEngineName = APP_SHORT_NAME,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001854 .engineVersion = 0,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001855 .apiVersion = VK_API_VERSION,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001856 };
Courtney Goeltzenleuchterddcb6192015-04-14 18:48:46 -06001857 const VkInstanceCreateInfo inst_info = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001858 .sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
Jon Ashburn29669a42015-04-04 14:52:07 -06001859 .pNext = NULL,
1860 .pAppInfo = &app,
1861 .pAllocCb = NULL,
Tobin Ehlis3536b442015-04-16 18:04:57 -06001862 .extensionCount = 1,
1863 .ppEnabledExtensionNames = ext_names,
Jon Ashburn29669a42015-04-04 14:52:07 -06001864 };
Courtney Goeltzenleuchterddcb6192015-04-14 18:48:46 -06001865 const VkDeviceQueueCreateInfo queue = {
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001866 .queueNodeIndex = 0,
1867 .queueCount = 1,
1868 };
Tobin Ehlisbbc18b52015-04-16 10:04:35 -06001869
Courtney Goeltzenleuchterddcb6192015-04-14 18:48:46 -06001870 const VkDeviceCreateInfo device = {
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001871 .sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001872 .pNext = NULL,
1873 .queueRecordCount = 1,
1874 .pRequestedQueues = &queue,
Tobin Ehlis3536b442015-04-16 18:04:57 -06001875 .extensionCount = 1,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001876 .ppEnabledExtensionNames = ext_names,
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001877 .flags = VK_DEVICE_CREATE_VALIDATION_BIT,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001878 };
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001879 uint32_t gpu_count;
1880 uint32_t i;
Courtney Goeltzenleuchterf3168062015-03-05 18:09:39 -07001881 size_t data_size;
1882 uint32_t queue_count;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001883
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001884 err = vkCreateInstance(&inst_info, &demo->inst);
Ian Elliottcaa9f272015-04-28 11:35:02 -06001885 if (err == VK_ERROR_INCOMPATIBLE_DRIVER) {
1886 ERR_EXIT("Cannot find a compatible Vulkan installable client driver "
1887 "(ICD).\nPlease look at the Getting Started guide for "
1888 "additional information.\n",
1889 "vkCreateInstance Failure");
1890 } else if (err) {
1891 ERR_EXIT("vkCreateInstance failed. Do you have a compatible Vulkan "
1892 "installable client driver (ICD) installed. Please look at "
1893 "the Getting Started guide for additional information.\n",
1894 "vkCreateInstance Failure");
Ian Elliottdfe55f72015-04-03 15:24:55 -06001895 }
Jon Ashburn29669a42015-04-04 14:52:07 -06001896
Jon Ashburn07b309a2015-04-15 11:31:12 -06001897 gpu_count = 1;
1898 err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, &demo->gpu);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001899 assert(!err && gpu_count == 1);
1900
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001901 err = vkCreateDevice(demo->gpu, &device, &demo->device);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001902 assert(!err);
1903
Tony Barbour8205d902015-04-16 15:59:00 -06001904 err = vkGetPhysicalDeviceInfo(demo->gpu, VK_PHYSICAL_DEVICE_INFO_TYPE_PROPERTIES,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -06001905 &data_size, NULL);
1906 assert(!err);
1907
Tony Barbour8205d902015-04-16 15:59:00 -06001908 demo->gpu_props = (VkPhysicalDeviceProperties *) malloc(data_size);
1909 err = vkGetPhysicalDeviceInfo(demo->gpu, VK_PHYSICAL_DEVICE_INFO_TYPE_PROPERTIES,
Courtney Goeltzenleuchterbfccf412015-03-25 13:36:41 -06001910 &data_size, demo->gpu_props);
1911 assert(!err);
1912
Tony Barbour8205d902015-04-16 15:59:00 -06001913 err = vkGetPhysicalDeviceInfo(demo->gpu, VK_PHYSICAL_DEVICE_INFO_TYPE_QUEUE_PROPERTIES,
Courtney Goeltzenleuchterf3168062015-03-05 18:09:39 -07001914 &data_size, NULL);
1915 assert(!err);
1916
Tony Barbour8205d902015-04-16 15:59:00 -06001917 demo->queue_props = (VkPhysicalDeviceQueueProperties *) malloc(data_size);
1918 err = vkGetPhysicalDeviceInfo(demo->gpu, VK_PHYSICAL_DEVICE_INFO_TYPE_QUEUE_PROPERTIES,
Courtney Goeltzenleuchterf3168062015-03-05 18:09:39 -07001919 &data_size, demo->queue_props);
1920 assert(!err);
Courtney Goeltzenleuchtercb67a322015-04-21 09:31:23 -06001921 queue_count = (uint32_t)(data_size / sizeof(VkPhysicalDeviceQueueProperties));
Courtney Goeltzenleuchterf3168062015-03-05 18:09:39 -07001922 assert(queue_count >= 1);
1923
Mark Lobodzinskicf26e072015-04-16 11:44:05 -05001924 // Graphics queue and MemMgr queue can be separate.
1925 // TODO: Add support for separate queues, including synchronization,
1926 // and appropriate tracking for QueueSubmit and QueueBindObjectMemory
Courtney Goeltzenleuchterf3168062015-03-05 18:09:39 -07001927 for (i = 0; i < queue_count; i++) {
Mark Lobodzinskicf26e072015-04-16 11:44:05 -05001928 if ((demo->queue_props[i].queueFlags & VK_QUEUE_GRAPHICS_BIT) &&
1929 (demo->queue_props[i].queueFlags & VK_QUEUE_MEMMGR_BIT) )
Courtney Goeltzenleuchterf3168062015-03-05 18:09:39 -07001930 break;
1931 }
1932 assert(i < queue_count);
1933 demo->graphics_queue_node_index = i;
1934
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06001935 err = vkGetDeviceQueue(demo->device, demo->graphics_queue_node_index,
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001936 0, &demo->queue);
1937 assert(!err);
1938}
1939
1940static void demo_init_connection(struct demo *demo)
1941{
Ian Elliotte14e9f92015-04-16 15:23:05 -06001942#ifndef _WIN32
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001943 const xcb_setup_t *setup;
1944 xcb_screen_iterator_t iter;
1945 int scr;
1946
1947 demo->connection = xcb_connect(NULL, &scr);
Ian Elliottdfe55f72015-04-03 15:24:55 -06001948 if (demo->connection == NULL) {
1949 printf("Cannot find a compatible Vulkan installable client driver "
1950 "(ICD).\nExiting ...\n");
1951 fflush(stdout);
1952 exit(1);
1953 }
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001954
1955 setup = xcb_get_setup(demo->connection);
1956 iter = xcb_setup_roots_iterator(setup);
1957 while (scr-- > 0)
1958 xcb_screen_next(&iter);
1959
1960 demo->screen = iter.data;
Ian Elliotte14e9f92015-04-16 15:23:05 -06001961#endif // _WIN32
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001962}
1963
Ian Elliotte14e9f92015-04-16 15:23:05 -06001964#ifdef _WIN32
1965static void demo_init(struct demo *demo, HINSTANCE hInstance, LPSTR pCmdLine)
1966#else // _WIN32
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001967static void demo_init(struct demo *demo, int argc, char **argv)
Ian Elliotte14e9f92015-04-16 15:23:05 -06001968#endif // _WIN32
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001969{
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -06001970 vec3 eye = {0.0f, 3.0f, 5.0f};
1971 vec3 origin = {0, 0, 0};
1972 vec3 up = {0.0f, -1.0f, 0.0};
Ian Elliotte14e9f92015-04-16 15:23:05 -06001973 bool argv_error = false;
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -06001974
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06001975 memset(demo, 0, sizeof(*demo));
1976
Ian Elliotte14e9f92015-04-16 15:23:05 -06001977#ifdef _WIN32
1978 demo->connection = hInstance;
Ian Elliott4e19ed02015-04-28 10:52:52 -06001979 strncpy(demo->name, APP_SHORT_NAME, APP_NAME_STR_LEN);
Ian Elliotte14e9f92015-04-16 15:23:05 -06001980
1981 if (strncmp(pCmdLine, "--use_staging", strlen("--use_staging")) == 0)
1982 demo->use_staging_buffer = true;
1983 else if (strlen(pCmdLine) != 0) {
1984 fprintf(stderr, "Do not recognize argument \"%s\".\n", pCmdLine);
1985 argv_error = true;
1986 }
1987#else // _WIN32
Piers Daniell886be472015-02-23 16:23:13 -07001988 for (int i = 1; i < argc; i++) {
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07001989 if (strncmp(argv[i], "--use_staging", strlen("--use_staging")) == 0)
1990 demo->use_staging_buffer = true;
Ian Elliotte14e9f92015-04-16 15:23:05 -06001991 else {
1992 fprintf(stderr, "Do not recognize argument \"%s\".\n", argv[i]);
1993 argv_error = true;
1994 }
1995 }
1996#endif // _WIN32
1997 if (argv_error) {
Ian Elliott4e19ed02015-04-28 10:52:52 -06001998 fprintf(stderr, "Usage:\n %s [--use_staging]\n", APP_SHORT_NAME);
Ian Elliotte14e9f92015-04-16 15:23:05 -06001999 fflush(stderr);
2000 exit(1);
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07002001 }
2002
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002003 demo_init_connection(demo);
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06002004 demo_init_vk(demo);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002005
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -06002006 demo->width = 500;
2007 demo->height = 500;
Tony Barbour8205d902015-04-16 15:59:00 -06002008 demo->format = VK_FORMAT_B8G8R8A8_UNORM;
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -06002009
2010 demo->spin_angle = 0.01f;
2011 demo->spin_increment = 0.01f;
2012 demo->pause = false;
2013
Piers Daniell886be472015-02-23 16:23:13 -07002014 mat4x4_perspective(demo->projection_matrix, (float)degreesToRadians(45.0f), 1.0f, 0.1f, 100.0f);
Courtney Goeltzenleuchter3eeff432014-10-29 08:29:35 -06002015 mat4x4_look_at(demo->view_matrix, eye, origin, up);
2016 mat4x4_identity(demo->model_matrix);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002017}
2018
2019static void demo_cleanup(struct demo *demo)
2020{
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06002021 uint32_t i, j;
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002022
Courtney Goeltzenleuchter857542b2015-04-27 14:56:34 -06002023 demo->prepared = false;
2024
Mike Stroyan230e6252015-04-17 12:36:38 -06002025 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DESCRIPTOR_SET, demo->desc_set);
2026 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DESCRIPTOR_POOL, demo->desc_pool);
Chia-I Wuf8385062015-01-04 16:27:24 +08002027
Mike Stroyan230e6252015-04-17 12:36:38 -06002028 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DYNAMIC_VP_STATE, demo->viewport);
2029 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DYNAMIC_RS_STATE, demo->raster);
2030 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DYNAMIC_CB_STATE, demo->color_blend);
2031 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DYNAMIC_DS_STATE, demo->depth_stencil);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002032
Mike Stroyan230e6252015-04-17 12:36:38 -06002033 vkDestroyObject(demo->device, VK_OBJECT_TYPE_PIPELINE, demo->pipeline);
2034 vkDestroyObject(demo->device, VK_OBJECT_TYPE_PIPELINE_LAYOUT, demo->pipeline_layout);
2035 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DESCRIPTOR_SET_LAYOUT, demo->desc_layout);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002036
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002037 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Mike Stroyan230e6252015-04-17 12:36:38 -06002038 vkDestroyObject(demo->device, VK_OBJECT_TYPE_IMAGE_VIEW, demo->textures[i].view);
2039 vkQueueBindObjectMemory(demo->queue, VK_OBJECT_TYPE_IMAGE, demo->textures[i].image, 0, VK_NULL_HANDLE, 0);
2040 vkDestroyObject(demo->device, VK_OBJECT_TYPE_IMAGE, demo->textures[i].image);
Courtney Goeltzenleuchter75538942015-04-02 16:25:42 -06002041 demo_remove_mem_refs(demo, demo->textures[i].num_mem, demo->textures[i].mem);
Jon Ashburna9ae3832015-01-16 09:37:43 -07002042 for (j = 0; j < demo->textures[i].num_mem; j++)
Mike Stroyan230e6252015-04-17 12:36:38 -06002043 vkFreeMemory(demo->device, demo->textures[i].mem[j]);
2044 free(demo->textures[i].mem);
2045 vkDestroyObject(demo->device, VK_OBJECT_TYPE_SAMPLER, demo->textures[i].sampler);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002046 }
Chia-I Wu5b66aa52015-04-16 22:02:10 +08002047 vkDestroySwapChainWSI(demo->swap_chain);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002048
Mike Stroyan230e6252015-04-17 12:36:38 -06002049 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DEPTH_STENCIL_VIEW, demo->depth.view);
2050 vkQueueBindObjectMemory(demo->queue, VK_OBJECT_TYPE_IMAGE, demo->depth.image, 0, VK_NULL_HANDLE, 0);
Courtney Goeltzenleuchter75538942015-04-02 16:25:42 -06002051 demo_remove_mem_refs(demo, demo->depth.num_mem, demo->depth.mem);
Mike Stroyan230e6252015-04-17 12:36:38 -06002052 vkDestroyObject(demo->device, VK_OBJECT_TYPE_IMAGE, demo->depth.image);
Courtney Goeltzenleuchter75538942015-04-02 16:25:42 -06002053 for (j = 0; j < demo->depth.num_mem; j++) {
Mike Stroyan230e6252015-04-17 12:36:38 -06002054 vkFreeMemory(demo->device, demo->depth.mem[j]);
Courtney Goeltzenleuchter75538942015-04-02 16:25:42 -06002055 }
Mark Lobodzinski15427102015-02-18 16:38:17 -06002056
Mike Stroyan230e6252015-04-17 12:36:38 -06002057 vkDestroyObject(demo->device, VK_OBJECT_TYPE_BUFFER_VIEW, demo->uniform_data.view);
2058 vkQueueBindObjectMemory(demo->queue, VK_OBJECT_TYPE_BUFFER, demo->uniform_data.buf, 0, VK_NULL_HANDLE, 0);
2059 vkDestroyObject(demo->device, VK_OBJECT_TYPE_BUFFER, demo->uniform_data.buf);
Courtney Goeltzenleuchter75538942015-04-02 16:25:42 -06002060 demo_remove_mem_refs(demo, demo->uniform_data.num_mem, demo->uniform_data.mem);
Jon Ashburnc6ae13d2015-01-19 15:00:26 -07002061 for (j = 0; j < demo->uniform_data.num_mem; j++)
Mike Stroyan230e6252015-04-17 12:36:38 -06002062 vkFreeMemory(demo->device, demo->uniform_data.mem[j]);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002063
2064 for (i = 0; i < DEMO_BUFFER_COUNT; i++) {
Mike Stroyan230e6252015-04-17 12:36:38 -06002065 vkDestroyObject(demo->device, VK_OBJECT_TYPE_COLOR_ATTACHMENT_VIEW, demo->buffers[i].view);
2066 vkDestroyObject(demo->device, VK_OBJECT_TYPE_COMMAND_BUFFER, demo->buffers[i].cmd);
Courtney Goeltzenleuchter75538942015-04-02 16:25:42 -06002067 demo_remove_mem_refs(demo, 1, &demo->buffers[i].mem);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002068 }
2069
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -06002070 vkDestroyDevice(demo->device);
2071 vkDestroyInstance(demo->inst);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002072
Ian Elliotte14e9f92015-04-16 15:23:05 -06002073#ifndef _WIN32
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002074 xcb_destroy_window(demo->connection, demo->window);
2075 xcb_disconnect(demo->connection);
Ian Elliotte14e9f92015-04-16 15:23:05 -06002076#endif // _WIN32
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002077}
2078
Ian Elliotte14e9f92015-04-16 15:23:05 -06002079#ifdef _WIN32
Ian Elliott7595eee2015-04-28 10:33:11 -06002080
Ian Elliotte14e9f92015-04-16 15:23:05 -06002081int APIENTRY WinMain(HINSTANCE hInstance,
2082 HINSTANCE hPrevInstance,
2083 LPSTR pCmdLine,
2084 int nCmdShow)
2085{
2086 MSG msg; // message
2087 bool done; // flag saying when app is complete
2088
2089 demo_init(&demo, hInstance, pCmdLine);
2090 demo_create_window(&demo);
2091
2092 demo_prepare(&demo);
2093
2094 done = false; //initialize loop condition variable
2095 /* main message loop*/
2096 while(!done)
2097 {
Tony Barboura938abb2015-04-22 11:36:22 -06002098 PeekMessage(&msg,0,0,0,PM_REMOVE);
Ian Elliotte14e9f92015-04-16 15:23:05 -06002099 if (msg.message == WM_QUIT) //check for a quit message
2100 {
2101 done = true; //if found, quit app
2102 }
2103 else
2104 {
2105 /* Translate and dispatch to event queue*/
2106 TranslateMessage(&msg);
2107 DispatchMessage(&msg);
2108 }
2109 }
2110
2111 demo_cleanup(&demo);
2112
Tony Barboura938abb2015-04-22 11:36:22 -06002113 return (int) msg.wParam;
Ian Elliotte14e9f92015-04-16 15:23:05 -06002114}
2115#else // _WIN32
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07002116int main(int argc, char **argv)
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002117{
2118 struct demo demo;
2119
Courtney Goeltzenleuchter40a8b0a2015-02-17 12:54:31 -07002120 demo_init(&demo, argc, argv);
Chia-I Wu5b66aa52015-04-16 22:02:10 +08002121 demo_create_window(&demo);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002122
2123 demo_prepare(&demo);
Courtney Goeltzenleuchter4e8246e2014-10-23 13:16:59 -06002124 demo_run(&demo);
2125
2126 demo_cleanup(&demo);
2127
2128 return 0;
2129}
Ian Elliotte14e9f92015-04-16 15:23:05 -06002130#endif // _WIN32