blob: 3aa2128dfca2e9f7c4d54a4b238dba486a19b01b [file] [log] [blame]
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001#define _GNU_SOURCE
Courtney Goeltzenleuchter7be88a82014-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 Elliott639ca472015-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 Goeltzenleuchter7be88a82014-10-23 13:16:59 -060013#include <xcb/xcb.h>
Ian Elliott639ca472015-04-16 15:23:05 -060014#endif // _WIN32
15
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -060016#include <vulkan.h>
17#include <vkDbg.h>
Chia-I Wucbb564e2015-04-16 22:02:10 +080018#include <vk_wsi_lunarg.h>
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060019
Cody Northropfe3d8bc2015-03-17 14:54:35 -060020#include "icd-spv.h"
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060021
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -060022#include "linmath.h"
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -060023#include <png.h>
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -060024
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060025#define DEMO_BUFFER_COUNT 2
26#define DEMO_TEXTURE_COUNT 1
27
Tony Barbourfdc2d352015-04-22 09:02:32 -060028#if defined(NDEBUG) && defined(__GNUC__)
29#define U_ASSERT_ONLY __attribute__((unused))
30#else
31#define U_ASSERT_ONLY
32#endif
33
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -060034/*
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -070035 * structure to track all objects related to a texture.
36 */
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -060037struct texture_object {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -060038 VkSampler sampler;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -070039
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -060040 VkImage image;
41 VkImageLayout imageLayout;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -060042
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -070043 uint32_t num_mem;
Tony Barbour72304ef2015-04-16 15:59:00 -060044 VkDeviceMemory *mem;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -060045 VkImageView view;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -070046 int32_t tex_width, tex_height;
47};
48
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -060049static char *tex_files[] = {
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -060050 "lunarg-logo-256x256-solid.png"
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -060051};
52
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -060053struct vkcube_vs_uniform {
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -060054 // Must start with MVP
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -060055 float mvp[4][4];
56 float position[12*3][4];
57 float color[12*3][4];
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -060058};
59
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -060060struct vktexcube_vs_uniform {
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -060061 // Must start with MVP
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -060062 float mvp[4][4];
63 float position[12*3][4];
64 float attr[12*3][4];
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -060065};
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -060066
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060067//--------------------------------------------------------------------------------------
68// Mesh and VertexFormat Data
69//--------------------------------------------------------------------------------------
70struct Vertex
71{
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -060072 float posX, posY, posZ, posW; // Position data
73 float r, g, b, a; // Color
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060074};
75
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -060076struct VertexPosTex
77{
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -060078 float posX, posY, posZ, posW; // Position data
79 float u, v, s, t; // Texcoord
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -060080};
81
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060082#define XYZ1(_x_, _y_, _z_) (_x_), (_y_), (_z_), 1.f
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -060083#define UV(_u_, _v_) (_u_), (_v_), 0.f, 1.f
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060084
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -060085static const float g_vertex_buffer_data[] = {
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -060086 -1.0f,-1.0f,-1.0f, // Vertex 0
87 -1.0f,-1.0f, 1.0f,
88 -1.0f, 1.0f, 1.0f,
89
90 -1.0f, 1.0f, 1.0f, // Vertex 1
91 -1.0f, 1.0f,-1.0f,
92 -1.0f,-1.0f,-1.0f,
93
94 -1.0f,-1.0f,-1.0f, // Vertex 2
95 1.0f, 1.0f,-1.0f,
96 1.0f,-1.0f,-1.0f,
97
98 -1.0f,-1.0f,-1.0f, // Vertex 3
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -060099 -1.0f, 1.0f,-1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600100 1.0f, 1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600101
102 -1.0f,-1.0f,-1.0f, // Vertex 4
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600103 1.0f,-1.0f,-1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600104 1.0f,-1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600105
106 -1.0f,-1.0f,-1.0f, // Vertex 5
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600107 1.0f,-1.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600108 -1.0f,-1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600109
110 -1.0f, 1.0f,-1.0f, // Vertex 6
111 -1.0f, 1.0f, 1.0f,
112 1.0f, 1.0f, 1.0f,
113
114 -1.0f, 1.0f,-1.0f, // Vertex 7
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600115 1.0f, 1.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600116 1.0f, 1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600117
118 1.0f, 1.0f,-1.0f, // Vertex 8
119 1.0f, 1.0f, 1.0f,
120 1.0f,-1.0f, 1.0f,
121
122 1.0f,-1.0f, 1.0f, // Vertex 9
123 1.0f,-1.0f,-1.0f,
124 1.0f, 1.0f,-1.0f,
125
126 -1.0f, 1.0f, 1.0f, // Vertex 10
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600127 -1.0f,-1.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600128 1.0f, 1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600129
130 -1.0f,-1.0f, 1.0f, // Vertex 11
131 1.0f,-1.0f, 1.0f,
132 1.0f, 1.0f, 1.0f,
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600133};
134
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600135static const float g_uv_buffer_data[] = {
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600136 1.0f, 0.0f, // Vertex 0
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600137 0.0f, 0.0f,
138 0.0f, 1.0f,
139
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600140 0.0f, 1.0f, // Vertex 1
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600141 1.0f, 1.0f,
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600142 1.0f, 0.0f,
143
144// 0.0f, 1.0f, // Vertex 2
145// 1.0f, 0.0f,
146// 0.0f, 0.0f,
147
148// 0.0f, 1.0f, // Vertex 3
149// 1.0f, 0.0f,
150// 1.0f, 1.0f,
151
152 0.0f, 0.0f, // Vertex 2
153 1.0f, 1.0f,
154 1.0f, 0.0f,
155
156 0.0f, 0.0f, // Vertex 3
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600157 0.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600158 1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600159
160 0.0f, 1.0f, // Vertex 4
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600161 0.0f, 0.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600162 1.0f, 0.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600163
164 0.0f, 1.0f, // Vertex 5
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600165 1.0f, 0.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600166 1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600167
168 0.0f, 1.0f, // Vertex 6
169 1.0f, 1.0f,
170 1.0f, 0.0f,
171
172 0.0f, 1.0f, // Vertex 7
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600173 1.0f, 0.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600174 0.0f, 0.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600175
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600176 0.0f, 1.0f, // Vertex 8
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600177 1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600178 1.0f, 0.0f,
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600179
180 1.0f, 0.0f, // Vertex 9
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600181 0.0f, 0.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600182 0.0f, 1.0f,
183
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600184 1.0f, 1.0f, // Vertex 10
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600185 1.0f, 0.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600186 0.0f, 1.0f,
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600187
188 1.0f, 0.0f, // Vertex 11
189 0.0f, 0.0f,
190 0.0f, 1.0f,
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600191};
192
193void dumpMatrix(const char *note, mat4x4 MVP)
194{
195 int i;
196
197 printf("%s: \n", note);
198 for (i=0; i<4; i++) {
199 printf("%f, %f, %f, %f\n", MVP[i][0], MVP[i][1], MVP[i][2], MVP[i][3]);
200 }
201 printf("\n");
202 fflush(stdout);
203}
204
205void dumpVec4(const char *note, vec4 vector)
206{
207 printf("%s: \n", note);
208 printf("%f, %f, %f, %f\n", vector[0], vector[1], vector[2], vector[3]);
209 printf("\n");
210 fflush(stdout);
211}
212
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600213struct demo {
Ian Elliott639ca472015-04-16 15:23:05 -0600214#ifdef _WIN32
215#define APP_NAME_STR_LEN 80
216 HINSTANCE connection; // hInstance - Windows Instance
217 char name[APP_NAME_STR_LEN]; // Name to put on the window/icon
218 HWND window; // hWnd - window handle
219#else // _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600220 xcb_connection_t *connection;
221 xcb_screen_t *screen;
Chia-I Wucbb564e2015-04-16 22:02:10 +0800222 xcb_window_t window;
223 xcb_intern_atom_reply_t *atom_wm_delete_window;
Ian Elliott639ca472015-04-16 15:23:05 -0600224#endif // _WIN32
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700225 bool use_staging_buffer;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600226
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600227 VkInstance inst;
Tony Barbour72304ef2015-04-16 15:59:00 -0600228 VkPhysicalDevice gpu;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600229 VkDevice device;
230 VkQueue queue;
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -0700231 uint32_t graphics_queue_node_index;
Tony Barbour72304ef2015-04-16 15:59:00 -0600232 VkPhysicalDeviceProperties *gpu_props;
233 VkPhysicalDeviceQueueProperties *queue_props;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600234
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600235 VkFramebuffer framebuffer;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600236 int width, height;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600237 VkFormat format;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600238
Chia-I Wucbb564e2015-04-16 22:02:10 +0800239 VkSwapChainWSI swap_chain;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600240 struct {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600241 VkImage image;
Tony Barbour72304ef2015-04-16 15:59:00 -0600242 VkDeviceMemory mem;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600243 VkCmdBuffer cmd;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600244
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600245 VkColorAttachmentView view;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600246 } buffers[DEMO_BUFFER_COUNT];
247
248 struct {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600249 VkFormat format;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600250
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600251 VkImage image;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600252 uint32_t num_mem;
Tony Barbour72304ef2015-04-16 15:59:00 -0600253 VkDeviceMemory *mem;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600254 VkDepthStencilView view;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600255 } depth;
256
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600257 struct texture_object textures[DEMO_TEXTURE_COUNT];
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600258
259 struct {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600260 VkBuffer buf;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600261 uint32_t num_mem;
Tony Barbour72304ef2015-04-16 15:59:00 -0600262 VkDeviceMemory *mem;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600263 VkBufferView view;
264 VkBufferViewAttachInfo attach;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600265 } uniform_data;
266
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600267 VkCmdBuffer cmd; // Buffer for initialization commands
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -0500268 VkPipelineLayout pipeline_layout;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600269 VkDescriptorSetLayout desc_layout;
270 VkPipeline pipeline;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600271
Courtney Goeltzenleuchter5e6d1e92015-04-10 16:24:50 -0600272 VkDynamicVpState viewport;
273 VkDynamicRsState raster;
274 VkDynamicCbState color_blend;
275 VkDynamicDsState depth_stencil;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600276
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600277 mat4x4 projection_matrix;
278 mat4x4 view_matrix;
279 mat4x4 model_matrix;
280
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600281 float spin_angle;
282 float spin_increment;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600283 bool pause;
284
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600285 VkDescriptorPool desc_pool;
286 VkDescriptorSet desc_set;
Chia-I Wu6a3c8972015-01-04 16:27:24 +0800287
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600288 bool quit;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600289 uint32_t current_buffer;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600290};
291
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600292static void demo_flush_init_cmd(struct demo *demo)
293{
Tony Barbourfdc2d352015-04-22 09:02:32 -0600294 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600295
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600296 if (demo->cmd == VK_NULL_HANDLE)
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600297 return;
298
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600299 err = vkEndCommandBuffer(demo->cmd);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600300 assert(!err);
301
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600302 const VkCmdBuffer cmd_bufs[] = { demo->cmd };
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600303
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600304 err = vkQueueSubmit(demo->queue, 1, cmd_bufs, VK_NULL_HANDLE);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600305 assert(!err);
306
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600307 err = vkQueueWaitIdle(demo->queue);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600308 assert(!err);
309
Mike Stroyanebae8322015-04-17 12:36:38 -0600310 vkDestroyObject(demo->device, VK_OBJECT_TYPE_COMMAND_BUFFER, demo->cmd);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600311 demo->cmd = VK_NULL_HANDLE;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600312}
313
314static void demo_add_mem_refs(
315 struct demo *demo,
Tony Barbour72304ef2015-04-16 15:59:00 -0600316 int num_refs, VkDeviceMemory *mem)
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600317{
Courtney Goeltzenleuchter6b0caf12015-04-16 13:38:46 -0600318 vkQueueAddMemReferences(demo->queue, num_refs, mem);
Courtney Goeltzenleuchter1359b7a2015-04-02 16:25:42 -0600319}
320
321static void demo_remove_mem_refs(
322 struct demo *demo,
Tony Barbour72304ef2015-04-16 15:59:00 -0600323 int num_refs, VkDeviceMemory *mem)
Courtney Goeltzenleuchter1359b7a2015-04-02 16:25:42 -0600324{
Courtney Goeltzenleuchter6b0caf12015-04-16 13:38:46 -0600325 vkQueueRemoveMemReferences(demo->queue, num_refs, mem);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600326}
327
328static void demo_set_image_layout(
329 struct demo *demo,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600330 VkImage image,
331 VkImageLayout old_image_layout,
332 VkImageLayout new_image_layout)
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600333{
Tony Barbourfdc2d352015-04-22 09:02:32 -0600334 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600335
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600336 if (demo->cmd == VK_NULL_HANDLE) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600337 const VkCmdBufferCreateInfo cmd = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600338 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600339 .pNext = NULL,
340 .queueNodeIndex = demo->graphics_queue_node_index,
341 .flags = 0,
342 };
343
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600344 err = vkCreateCommandBuffer(demo->device, &cmd, &demo->cmd);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600345 assert(!err);
346
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600347 VkCmdBufferBeginInfo cmd_buf_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600348 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600349 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -0600350 .flags = VK_CMD_BUFFER_OPTIMIZE_SMALL_BATCH_BIT |
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600351 VK_CMD_BUFFER_OPTIMIZE_ONE_TIME_SUBMIT_BIT,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600352 };
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600353 err = vkBeginCommandBuffer(demo->cmd, &cmd_buf_info);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600354 }
355
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600356 VkImageMemoryBarrier image_memory_barrier = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600357 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600358 .pNext = NULL,
359 .outputMask = 0,
360 .inputMask = 0,
361 .oldLayout = old_image_layout,
362 .newLayout = new_image_layout,
363 .image = image,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600364 .subresourceRange = { VK_IMAGE_ASPECT_COLOR, 0, 1, 0, 0 }
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600365 };
366
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600367 if (new_image_layout == VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL) {
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600368 /* Make sure anything that was copying from this image has completed */
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -0600369 image_memory_barrier.inputMask = VK_MEMORY_INPUT_TRANSFER_BIT;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600370 }
371
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600372 if (new_image_layout == VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL) {
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600373 /* Make sure any Copy or CPU writes to image are flushed */
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -0600374 image_memory_barrier.outputMask = VK_MEMORY_OUTPUT_CPU_WRITE_BIT | VK_MEMORY_OUTPUT_TRANSFER_BIT;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600375 }
376
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600377 VkImageMemoryBarrier *pmemory_barrier = &image_memory_barrier;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600378
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600379 VkPipeEvent set_events[] = { VK_PIPE_EVENT_TOP_OF_PIPE };
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600380
Tony Barbour72304ef2015-04-16 15:59:00 -0600381 vkCmdPipelineBarrier(demo->cmd, VK_WAIT_EVENT_TOP_OF_PIPE, 1, set_events, 1, (const void **)&pmemory_barrier);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600382}
383
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600384static void demo_draw_build_cmd(struct demo *demo, VkCmdBuffer cmd_buf)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600385{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600386 const VkColorAttachmentBindInfo color_attachment = {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600387 .view = demo->buffers[demo->current_buffer].view,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600388 .layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600389 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600390 const VkDepthStencilBindInfo depth_stencil = {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600391 .view = demo->depth.view,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600392 .layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600393 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600394 const VkClearColor clear_color = {
Courtney Goeltzenleuchter374553c2015-04-03 16:35:32 -0600395 .color.floatColor = { 0.2f, 0.2f, 0.2f, 0.2f },
396 .useRawValue = false,
397 };
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600398 const float clear_depth = 1.0f;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600399 VkImageSubresourceRange clear_range;
400 VkCmdBufferBeginInfo cmd_buf_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600401 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO,
Courtney Goeltzenleuchter53968d82015-04-03 15:25:24 -0600402 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -0600403 .flags = VK_CMD_BUFFER_OPTIMIZE_SMALL_BATCH_BIT |
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600404 VK_CMD_BUFFER_OPTIMIZE_ONE_TIME_SUBMIT_BIT,
Jon Ashburn60ccfbe2014-12-31 17:08:35 -0700405 };
Tony Barbourfdc2d352015-04-22 09:02:32 -0600406 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600407 VkAttachmentLoadOp load_op = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
408 VkAttachmentStoreOp store_op = VK_ATTACHMENT_STORE_OP_DONT_CARE;
409 const VkFramebufferCreateInfo fb_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600410 .sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO,
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700411 .pNext = NULL,
412 .colorAttachmentCount = 1,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600413 .pColorAttachments = (VkColorAttachmentBindInfo*) &color_attachment,
414 .pDepthStencilAttachment = (VkDepthStencilBindInfo*) &depth_stencil,
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700415 .sampleCount = 1,
Mark Lobodzinskic06b7412015-01-27 13:24:03 -0600416 .width = demo->width,
417 .height = demo->height,
418 .layers = 1,
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700419 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600420 VkRenderPassCreateInfo rp_info;
421 VkRenderPassBegin rp_begin;
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700422
423 memset(&rp_info, 0 , sizeof(rp_info));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600424 err = vkCreateFramebuffer(demo->device, &fb_info, &rp_begin.framebuffer);
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700425 assert(!err);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600426 rp_info.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO;
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700427 rp_info.renderArea.extent.width = demo->width;
428 rp_info.renderArea.extent.height = demo->height;
Courtney Goeltzenleuchter53968d82015-04-03 15:25:24 -0600429 rp_info.colorAttachmentCount = fb_info.colorAttachmentCount;
430 rp_info.pColorFormats = &demo->format;
431 rp_info.pColorLayouts = &color_attachment.layout;
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700432 rp_info.pColorLoadOps = &load_op;
433 rp_info.pColorStoreOps = &store_op;
Courtney Goeltzenleuchter53968d82015-04-03 15:25:24 -0600434 rp_info.pColorLoadClearValues = &clear_color;
Tony Barbour72304ef2015-04-16 15:59:00 -0600435 rp_info.depthStencilFormat = VK_FORMAT_D16_UNORM;
Courtney Goeltzenleuchter53968d82015-04-03 15:25:24 -0600436 rp_info.depthStencilLayout = depth_stencil.layout;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600437 rp_info.depthLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
Courtney Goeltzenleuchter53968d82015-04-03 15:25:24 -0600438 rp_info.depthLoadClearValue = clear_depth;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600439 rp_info.depthStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
440 rp_info.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
Courtney Goeltzenleuchter53968d82015-04-03 15:25:24 -0600441 rp_info.stencilLoadClearValue = 0;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600442 rp_info.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
443 err = vkCreateRenderPass(demo->device, &rp_info, &rp_begin.renderPass);
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700444 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600445
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600446 err = vkBeginCommandBuffer(cmd_buf, &cmd_buf_info);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600447 assert(!err);
448
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600449 vkCmdBindPipeline(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600450 demo->pipeline);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600451 vkCmdBindDescriptorSets(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS,
Cody Northropfb5185a2015-04-16 13:41:56 -0600452 0, 1, &demo->desc_set, 0, NULL);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600453
Tony Barbour72304ef2015-04-16 15:59:00 -0600454 vkCmdBindDynamicStateObject(cmd_buf, VK_STATE_BIND_POINT_VIEWPORT, demo->viewport);
455 vkCmdBindDynamicStateObject(cmd_buf, VK_STATE_BIND_POINT_RASTER, demo->raster);
456 vkCmdBindDynamicStateObject(cmd_buf, VK_STATE_BIND_POINT_COLOR_BLEND,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600457 demo->color_blend);
Tony Barbour72304ef2015-04-16 15:59:00 -0600458 vkCmdBindDynamicStateObject(cmd_buf, VK_STATE_BIND_POINT_DEPTH_STENCIL,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600459 demo->depth_stencil);
460
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600461 vkCmdBeginRenderPass(cmd_buf, &rp_begin);
462 clear_range.aspect = VK_IMAGE_ASPECT_COLOR;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600463 clear_range.baseMipLevel = 0;
464 clear_range.mipLevels = 1;
465 clear_range.baseArraySlice = 0;
466 clear_range.arraySize = 1;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600467 vkCmdClearColorImage(cmd_buf,
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600468 demo->buffers[demo->current_buffer].image,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600469 VK_IMAGE_LAYOUT_CLEAR_OPTIMAL,
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600470 clear_color, 1, &clear_range);
471
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600472 clear_range.aspect = VK_IMAGE_ASPECT_DEPTH;
473 vkCmdClearDepthStencil(cmd_buf, demo->depth.image,
474 VK_IMAGE_LAYOUT_CLEAR_OPTIMAL,
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600475 clear_depth, 0, 1, &clear_range);
476
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600477 vkCmdDraw(cmd_buf, 0, 12 * 3, 0, 1);
478 vkCmdEndRenderPass(cmd_buf, rp_begin.renderPass);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600479
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600480 err = vkEndCommandBuffer(cmd_buf);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600481 assert(!err);
Courtney Goeltzenleuchter94901dc2015-02-25 17:53:18 -0700482
Mike Stroyanebae8322015-04-17 12:36:38 -0600483 vkDestroyObject(demo->device, VK_OBJECT_TYPE_RENDER_PASS, rp_begin.renderPass);
484 vkDestroyObject(demo->device, VK_OBJECT_TYPE_FRAMEBUFFER, rp_begin.framebuffer);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600485}
486
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600487
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600488void demo_update_data_buffer(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600489{
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600490 mat4x4 MVP, Model, VP;
491 int matrixSize = sizeof(MVP);
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600492 uint8_t *pData;
Tony Barbourfdc2d352015-04-22 09:02:32 -0600493 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600494
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600495 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600496
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600497 // Rotate 22.5 degrees around the Y axis
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600498 mat4x4_dup(Model, demo->model_matrix);
Piers Daniell735ee532015-02-23 16:23:13 -0700499 mat4x4_rotate(demo->model_matrix, Model, 0.0f, 1.0f, 0.0f, (float)degreesToRadians(demo->spin_angle));
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600500 mat4x4_mul(MVP, VP, demo->model_matrix);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600501
Jon Ashburnae7c21c2015-01-19 15:00:26 -0700502 assert(demo->uniform_data.num_mem == 1);
Mike Stroyanebae8322015-04-17 12:36:38 -0600503 err = vkMapMemory(demo->device, demo->uniform_data.mem[0], 0, 0, 0, (void **) &pData);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600504 assert(!err);
505
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600506 memcpy(pData, (const void*) &MVP[0][0], matrixSize);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600507
Mike Stroyanebae8322015-04-17 12:36:38 -0600508 err = vkUnmapMemory(demo->device, demo->uniform_data.mem[0]);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600509 assert(!err);
510}
511
512static void demo_draw(struct demo *demo)
513{
Chia-I Wucbb564e2015-04-16 22:02:10 +0800514 const VkPresentInfoWSI present = {
515 .sType = VK_STRUCTURE_TYPE_PRESENT_INFO_WSI,
516 .pNext = NULL,
517 .image = demo->buffers[demo->current_buffer].image,
518 .flipInterval = 0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600519 };
Tony Barbourfdc2d352015-04-22 09:02:32 -0600520 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600521
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600522 err = vkQueueSubmit(demo->queue, 1, &demo->buffers[demo->current_buffer].cmd,
523 VK_NULL_HANDLE);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600524 assert(!err);
525
Chia-I Wucbb564e2015-04-16 22:02:10 +0800526 err = vkQueuePresentWSI(demo->queue, &present);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600527 assert(!err);
528
529 demo->current_buffer = (demo->current_buffer + 1) % DEMO_BUFFER_COUNT;
Chia-I Wucbb564e2015-04-16 22:02:10 +0800530
531 err = vkQueueWaitIdle(demo->queue);
532 assert(err == VK_SUCCESS);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600533}
534
535static void demo_prepare_buffers(struct demo *demo)
536{
Chia-I Wucbb564e2015-04-16 22:02:10 +0800537 const VkSwapChainCreateInfoWSI swap_chain = {
538 .sType = VK_STRUCTURE_TYPE_SWAP_CHAIN_CREATE_INFO_WSI,
539 .pNext = NULL,
540 .pNativeWindowSystemHandle = demo->connection,
541 .pNativeWindowHandle = (void *) (intptr_t) demo->window,
542 .imageCount = DEMO_BUFFER_COUNT,
543 .imageFormat = demo->format,
544 .imageExtent = {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600545 .width = demo->width,
546 .height = demo->height,
547 },
Chia-I Wucbb564e2015-04-16 22:02:10 +0800548 .imageArraySize = 1,
549 .imageUsageFlags = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600550 };
Chia-I Wucbb564e2015-04-16 22:02:10 +0800551 VkSwapChainImageInfoWSI images[DEMO_BUFFER_COUNT];
552 size_t images_size = sizeof(images);
Tony Barbourfdc2d352015-04-22 09:02:32 -0600553 VkResult U_ASSERT_ONLY err;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600554 uint32_t i;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600555
Chia-I Wucbb564e2015-04-16 22:02:10 +0800556 err = vkCreateSwapChainWSI(demo->device, &swap_chain, &demo->swap_chain);
557 assert(!err);
558
559 err = vkGetSwapChainInfoWSI(demo->swap_chain,
560 VK_SWAP_CHAIN_INFO_TYPE_PERSISTENT_IMAGES_WSI,
561 &images_size, images);
562 assert(!err && images_size == sizeof(images));
563
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600564 for (i = 0; i < DEMO_BUFFER_COUNT; i++) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600565 VkColorAttachmentViewCreateInfo color_attachment_view = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600566 .sType = VK_STRUCTURE_TYPE_COLOR_ATTACHMENT_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600567 .pNext = NULL,
568 .format = demo->format,
569 .mipLevel = 0,
570 .baseArraySlice = 0,
571 .arraySize = 1,
572 };
573
Chia-I Wucbb564e2015-04-16 22:02:10 +0800574 demo->buffers[i].image = images[i].image;
575 demo->buffers[i].mem = images[i].memory;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600576
Courtney Goeltzenleuchter2c2fbbf2015-04-07 17:13:38 -0600577 demo_add_mem_refs(demo, 1, &demo->buffers[i].mem);
Courtney Goeltzenleuchter1359b7a2015-04-02 16:25:42 -0600578
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600579 demo_set_image_layout(demo, demo->buffers[i].image,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600580 VK_IMAGE_LAYOUT_UNDEFINED,
581 VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600582
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600583 color_attachment_view.image = demo->buffers[i].image;
584
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600585 err = vkCreateColorAttachmentView(demo->device,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600586 &color_attachment_view, &demo->buffers[i].view);
587 assert(!err);
588 }
589}
590
591static void demo_prepare_depth(struct demo *demo)
592{
Tony Barbour72304ef2015-04-16 15:59:00 -0600593 const VkFormat depth_format = VK_FORMAT_D16_UNORM;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600594 const VkImageCreateInfo image = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600595 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600596 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -0600597 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600598 .format = depth_format,
599 .extent = { demo->width, demo->height, 1 },
600 .mipLevels = 1,
601 .arraySize = 1,
602 .samples = 1,
Tony Barbour72304ef2015-04-16 15:59:00 -0600603 .tiling = VK_IMAGE_TILING_OPTIMAL,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600604 .usage = VK_IMAGE_USAGE_DEPTH_STENCIL_BIT,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600605 .flags = 0,
606 };
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -0600607 VkMemoryAllocInfo mem_alloc = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600608 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
Mark Lobodzinskia6907f72015-04-16 08:52:00 -0500609 .pNext = NULL,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600610 .allocationSize = 0,
Tony Barbour72304ef2015-04-16 15:59:00 -0600611 .memProps = VK_MEMORY_PROPERTY_DEVICE_ONLY,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600612 .memPriority = VK_MEMORY_PRIORITY_NORMAL,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600613 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600614 VkDepthStencilViewCreateInfo view = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600615 .sType = VK_STRUCTURE_TYPE_DEPTH_STENCIL_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600616 .pNext = NULL,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600617 .image = VK_NULL_HANDLE,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600618 .mipLevel = 0,
619 .baseArraySlice = 0,
620 .arraySize = 1,
621 .flags = 0,
622 };
Mike Stroyanebae8322015-04-17 12:36:38 -0600623
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600624 VkMemoryRequirements *mem_reqs;
625 size_t mem_reqs_size = sizeof(VkMemoryRequirements);
Tony Barbourfdc2d352015-04-22 09:02:32 -0600626 VkResult U_ASSERT_ONLY err;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600627 uint32_t num_allocations = 0;
628 size_t num_alloc_size = sizeof(num_allocations);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600629
630 demo->depth.format = depth_format;
631
632 /* create image */
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600633 err = vkCreateImage(demo->device, &image,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600634 &demo->depth.image);
635 assert(!err);
636
Mike Stroyanebae8322015-04-17 12:36:38 -0600637 err = vkGetObjectInfo(demo->device,
638 VK_OBJECT_TYPE_IMAGE, demo->depth.image,
639 VK_OBJECT_INFO_TYPE_MEMORY_ALLOCATION_COUNT,
640 &num_alloc_size, &num_allocations);
Jon Ashburnb2a66652015-01-16 09:37:43 -0700641 assert(!err && num_alloc_size == sizeof(num_allocations));
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600642 mem_reqs = malloc(num_allocations * sizeof(VkMemoryRequirements));
Tony Barbour72304ef2015-04-16 15:59:00 -0600643 demo->depth.mem = malloc(num_allocations * sizeof(VkDeviceMemory));
Jon Ashburnb2a66652015-01-16 09:37:43 -0700644 demo->depth.num_mem = num_allocations;
Mike Stroyanebae8322015-04-17 12:36:38 -0600645 err = vkGetObjectInfo(demo->device,
646 VK_OBJECT_TYPE_IMAGE, demo->depth.image,
Tony Barbour72304ef2015-04-16 15:59:00 -0600647 VK_OBJECT_INFO_TYPE_MEMORY_REQUIREMENTS,
Jon Ashburnb2a66652015-01-16 09:37:43 -0700648 &mem_reqs_size, mem_reqs);
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600649 assert(!err && mem_reqs_size == num_allocations * sizeof(VkMemoryRequirements));
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600650 for (uint32_t i = 0; i < num_allocations; i ++) {
Jon Ashburnb2a66652015-01-16 09:37:43 -0700651 mem_alloc.allocationSize = mem_reqs[i].size;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600652
Jon Ashburnb2a66652015-01-16 09:37:43 -0700653 /* allocate memory */
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600654 err = vkAllocMemory(demo->device, &mem_alloc,
Jon Ashburnb2a66652015-01-16 09:37:43 -0700655 &(demo->depth.mem[i]));
656 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600657
Jon Ashburnb2a66652015-01-16 09:37:43 -0700658 /* bind memory */
Mike Stroyanebae8322015-04-17 12:36:38 -0600659 err = vkQueueBindObjectMemory(demo->queue,
660 VK_OBJECT_TYPE_IMAGE, demo->depth.image,
661 i, demo->depth.mem[i], 0);
Jon Ashburnb2a66652015-01-16 09:37:43 -0700662 assert(!err);
663 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600664
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600665 demo_set_image_layout(demo, demo->depth.image,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600666 VK_IMAGE_LAYOUT_UNDEFINED,
667 VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600668
Courtney Goeltzenleuchter2c2fbbf2015-04-07 17:13:38 -0600669 demo_add_mem_refs(demo, demo->depth.num_mem, demo->depth.mem);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600670
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600671 /* create image view */
672 view.image = demo->depth.image;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600673 err = vkCreateDepthStencilView(demo->device, &view,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600674 &demo->depth.view);
675 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600676}
677
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600678/** loadTexture
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -0600679 * loads a png file into an memory object, using cstdio , libpng.
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600680 *
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -0600681 * \param demo : Needed to access VK calls
682 * \param filename : the png file to be loaded
683 * \param width : width of png, to be updated as a side effect of this function
684 * \param height : height of png, to be updated as a side effect of this function
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600685 *
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -0600686 * \return bool : an opengl texture id. true if successful?,
687 * should be validated by the client of this function.
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600688 *
689 * Source: http://en.wikibooks.org/wiki/OpenGL_Programming/Intermediate/Textures
690 * Modified to copy image to memory
691 *
692 */
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700693bool loadTexture(const char *filename, uint8_t *rgba_data,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600694 VkSubresourceLayout *layout,
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600695 int32_t *width, int32_t *height)
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600696{
697 //header for testing if it is a png
698 png_byte header[8];
Tony Barbourf9921732015-04-22 11:36:22 -0600699 int is_png, bit_depth, color_type, rowbytes;
700 size_t retval;
Ian Elliott1e42dff2015-02-13 14:29:21 -0700701 png_uint_32 i, twidth, theight;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600702 png_structp png_ptr;
703 png_infop info_ptr, end_info;
704 png_byte *image_data;
705 png_bytep *row_pointers;
706
707 //open file as binary
708 FILE *fp = fopen(filename, "rb");
709 if (!fp) {
710 return false;
711 }
712
713 //read the header
Tony Barbourfdc2d352015-04-22 09:02:32 -0600714 retval = fread(header, 1, 8, fp);
715 if (retval != 8) {
716 fclose(fp);
717 return false;
718 }
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600719
720 //test if png
721 is_png = !png_sig_cmp(header, 0, 8);
722 if (!is_png) {
723 fclose(fp);
724 return false;
725 }
726
727 //create png struct
728 png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL,
729 NULL, NULL);
730 if (!png_ptr) {
731 fclose(fp);
732 return (false);
733 }
734
735 //create png info struct
736 info_ptr = png_create_info_struct(png_ptr);
737 if (!info_ptr) {
738 png_destroy_read_struct(&png_ptr, (png_infopp) NULL, (png_infopp) NULL);
739 fclose(fp);
740 return (false);
741 }
742
743 //create png info struct
744 end_info = png_create_info_struct(png_ptr);
745 if (!end_info) {
746 png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp) NULL);
747 fclose(fp);
748 return (false);
749 }
750
751 //png error stuff, not sure libpng man suggests this.
752 if (setjmp(png_jmpbuf(png_ptr))) {
753 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
754 fclose(fp);
755 return (false);
756 }
757
758 //init png reading
759 png_init_io(png_ptr, fp);
760
761 //let libpng know you already read the first 8 bytes
762 png_set_sig_bytes(png_ptr, 8);
763
764 // read all the info up to the image data
765 png_read_info(png_ptr, info_ptr);
766
767 // get info about png
768 png_get_IHDR(png_ptr, info_ptr, &twidth, &theight, &bit_depth, &color_type,
769 NULL, NULL, NULL);
770
771 //update width and height based on png info
772 *width = twidth;
773 *height = theight;
774
775 // Require that incoming texture be 8bits per color component
776 // and 4 components (RGBA).
777 if (png_get_bit_depth(png_ptr, info_ptr) != 8 ||
778 png_get_channels(png_ptr, info_ptr) != 4) {
779 return false;
780 }
781
782 if (rgba_data == NULL) {
783 // If data pointer is null, we just want the width & height
784 // clean up memory and close stuff
785 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
786 fclose(fp);
787
788 return true;
789 }
790
791 // Update the png info struct.
792 png_read_update_info(png_ptr, info_ptr);
793
794 // Row size in bytes.
795 rowbytes = png_get_rowbytes(png_ptr, info_ptr);
796
797 // Allocate the image_data as a big block, to be given to opengl
798 image_data = (png_byte *)malloc(rowbytes * theight * sizeof(png_byte));
799 if (!image_data) {
800 //clean up memory and close stuff
801 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
802 fclose(fp);
803 return false;
804 }
805
806 // row_pointers is for pointing to image_data for reading the png with libpng
807 row_pointers = (png_bytep *)malloc(theight * sizeof(png_bytep));
808 if (!row_pointers) {
809 //clean up memory and close stuff
810 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
811 // delete[] image_data;
812 fclose(fp);
813 return false;
814 }
815 // set the individual row_pointers to point at the correct offsets of image_data
816 for (i = 0; i < theight; ++i)
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700817 row_pointers[theight - 1 - i] = rgba_data + i * layout->rowPitch;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600818
819 // read the png into image_data through row_pointers
820 png_read_image(png_ptr, row_pointers);
821
822 // clean up memory and close stuff
823 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
824 free(row_pointers);
825 free(image_data);
826 fclose(fp);
827
828 return true;
829}
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600830
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700831static void demo_prepare_texture_image(struct demo *demo,
832 const char *filename,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600833 struct texture_object *tex_obj,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600834 VkImageTiling tiling,
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -0600835 VkImageUsageFlags usage,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600836 VkFlags mem_props)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600837{
Tony Barbour72304ef2015-04-16 15:59:00 -0600838 const VkFormat tex_format = VK_FORMAT_B8G8R8A8_UNORM;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600839 int32_t tex_width;
840 int32_t tex_height;
Tony Barbourfdc2d352015-04-22 09:02:32 -0600841 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700842
843 err = loadTexture(filename, NULL, NULL, &tex_width, &tex_height);
844 assert(err);
845
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600846 tex_obj->tex_width = tex_width;
847 tex_obj->tex_height = tex_height;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700848
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600849 const VkImageCreateInfo image_create_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600850 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700851 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -0600852 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700853 .format = tex_format,
854 .extent = { tex_width, tex_height, 1 },
855 .mipLevels = 1,
856 .arraySize = 1,
857 .samples = 1,
858 .tiling = tiling,
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -0600859 .usage = usage,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700860 .flags = 0,
861 };
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -0600862 VkMemoryAllocInfo mem_alloc = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600863 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
Mark Lobodzinskia6907f72015-04-16 08:52:00 -0500864 .pNext = NULL,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700865 .allocationSize = 0,
866 .memProps = mem_props,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600867 .memPriority = VK_MEMORY_PRIORITY_NORMAL,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700868 };
869
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600870 VkMemoryRequirements *mem_reqs;
871 size_t mem_reqs_size = sizeof(VkMemoryRequirements);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700872 uint32_t num_allocations = 0;
873 size_t num_alloc_size = sizeof(num_allocations);
874
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600875 err = vkCreateImage(demo->device, &image_create_info,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600876 &tex_obj->image);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700877 assert(!err);
878
Mike Stroyanebae8322015-04-17 12:36:38 -0600879 err = vkGetObjectInfo(demo->device,
880 VK_OBJECT_TYPE_IMAGE, tex_obj->image,
Tony Barbour72304ef2015-04-16 15:59:00 -0600881 VK_OBJECT_INFO_TYPE_MEMORY_ALLOCATION_COUNT,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700882 &num_alloc_size, &num_allocations);
883 assert(!err && num_alloc_size == sizeof(num_allocations));
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600884 mem_reqs = malloc(num_allocations * sizeof(VkMemoryRequirements));
Tony Barbour72304ef2015-04-16 15:59:00 -0600885 tex_obj->mem = malloc(num_allocations * sizeof(VkDeviceMemory));
Mike Stroyanebae8322015-04-17 12:36:38 -0600886 err = vkGetObjectInfo(demo->device,
887 VK_OBJECT_TYPE_IMAGE, tex_obj->image,
Tony Barbour72304ef2015-04-16 15:59:00 -0600888 VK_OBJECT_INFO_TYPE_MEMORY_REQUIREMENTS,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700889 &mem_reqs_size, mem_reqs);
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600890 assert(!err && mem_reqs_size == num_allocations * sizeof(VkMemoryRequirements));
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700891 for (uint32_t j = 0; j < num_allocations; j ++) {
Piers Daniell735ee532015-02-23 16:23:13 -0700892 mem_alloc.allocationSize = mem_reqs[j].size;
893
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700894 /* allocate memory */
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600895 err = vkAllocMemory(demo->device, &mem_alloc,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600896 &(tex_obj->mem[j]));
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700897 assert(!err);
898
899 /* bind memory */
Mike Stroyanebae8322015-04-17 12:36:38 -0600900 err = vkQueueBindObjectMemory(demo->queue,
901 VK_OBJECT_TYPE_IMAGE, tex_obj->image,
902 j, tex_obj->mem[j], 0);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700903 assert(!err);
904 }
905 free(mem_reqs);
906 mem_reqs = NULL;
907
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600908 tex_obj->num_mem = num_allocations;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700909
Tony Barbour72304ef2015-04-16 15:59:00 -0600910 if (mem_props & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600911 const VkImageSubresource subres = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600912 .aspect = VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700913 .mipLevel = 0,
914 .arraySlice = 0,
915 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600916 VkSubresourceLayout layout;
917 size_t layout_size = sizeof(VkSubresourceLayout);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700918 void *data;
919
Mike Stroyanebae8322015-04-17 12:36:38 -0600920 err = vkGetImageSubresourceInfo(demo->device, tex_obj->image, &subres,
Tony Barbour72304ef2015-04-16 15:59:00 -0600921 VK_SUBRESOURCE_INFO_TYPE_LAYOUT,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700922 &layout_size, &layout);
923 assert(!err && layout_size == sizeof(layout));
924 /* Linear texture must be within a single memory object */
925 assert(num_allocations == 1);
926
Mike Stroyanebae8322015-04-17 12:36:38 -0600927 err = vkMapMemory(demo->device, tex_obj->mem[0], 0, 0, 0, &data);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700928 assert(!err);
929
930 if (!loadTexture(filename, data, &layout, &tex_width, &tex_height)) {
931 fprintf(stderr, "Error loading texture: %s\n", filename);
932 }
933
Mike Stroyanebae8322015-04-17 12:36:38 -0600934 err = vkUnmapMemory(demo->device, tex_obj->mem[0]);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700935 assert(!err);
936 }
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600937
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600938 tex_obj->imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600939 demo_set_image_layout(demo, tex_obj->image,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600940 VK_IMAGE_LAYOUT_UNDEFINED,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600941 tex_obj->imageLayout);
942 /* setting the image layout does not reference the actual memory so no need to add a mem ref */
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700943}
944
Mark Lobodzinskida9b1092015-04-16 11:44:05 -0500945static void demo_destroy_texture_image(struct demo *demo, struct texture_object *tex_objs)
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700946{
947 /* clean up staging resources */
948 for (uint32_t j = 0; j < tex_objs->num_mem; j ++) {
Mike Stroyanebae8322015-04-17 12:36:38 -0600949 vkQueueBindObjectMemory(demo->queue,
950 VK_OBJECT_TYPE_IMAGE, tex_objs->image, j, VK_NULL_HANDLE, 0);
951 vkFreeMemory(demo->device, tex_objs->mem[j]);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700952 }
953
954 free(tex_objs->mem);
Mike Stroyanebae8322015-04-17 12:36:38 -0600955 vkDestroyObject(demo->device, VK_OBJECT_TYPE_IMAGE, tex_objs->image);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700956}
957
958static void demo_prepare_textures(struct demo *demo)
959{
Tony Barbour72304ef2015-04-16 15:59:00 -0600960 const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600961 VkFormatProperties props;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700962 size_t size = sizeof(props);
Tony Barbourfdc2d352015-04-22 09:02:32 -0600963 VkResult U_ASSERT_ONLY err;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600964 uint32_t i;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600965
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600966 err = vkGetFormatInfo(demo->device, tex_format,
Tony Barbour72304ef2015-04-16 15:59:00 -0600967 VK_FORMAT_INFO_TYPE_PROPERTIES,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700968 &size, &props);
969 assert(!err);
970
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600971 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700972
Tony Barbour72304ef2015-04-16 15:59:00 -0600973 if (props.linearTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT && !demo->use_staging_buffer) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700974 /* Device can texture using linear textures */
975 demo_prepare_texture_image(demo, tex_files[i], &demo->textures[i],
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -0600976 VK_IMAGE_TILING_LINEAR, VK_IMAGE_USAGE_SAMPLED_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
Tony Barbour72304ef2015-04-16 15:59:00 -0600977 } else if (props.optimalTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700978 /* Must use staging buffer to copy linear texture to optimized */
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600979 struct texture_object staging_texture;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700980
981 memset(&staging_texture, 0, sizeof(staging_texture));
982 demo_prepare_texture_image(demo, tex_files[i], &staging_texture,
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -0600983 VK_IMAGE_TILING_LINEAR, VK_IMAGE_USAGE_TRANSFER_SOURCE_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700984
985 demo_prepare_texture_image(demo, tex_files[i], &demo->textures[i],
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -0600986 VK_IMAGE_TILING_OPTIMAL,
987 (VK_IMAGE_USAGE_TRANSFER_DESTINATION_BIT | VK_IMAGE_USAGE_SAMPLED_BIT),
988 VK_MEMORY_PROPERTY_DEVICE_ONLY);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700989
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600990 demo_set_image_layout(demo, staging_texture.image,
991 staging_texture.imageLayout,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600992 VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700993
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600994 demo_set_image_layout(demo, demo->textures[i].image,
995 demo->textures[i].imageLayout,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600996 VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700997
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600998 VkImageCopy copy_region = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600999 .srcSubresource = { VK_IMAGE_ASPECT_COLOR, 0, 0 },
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001000 .srcOffset = { 0, 0, 0 },
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001001 .destSubresource = { VK_IMAGE_ASPECT_COLOR, 0, 0 },
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001002 .destOffset = { 0, 0, 0 },
1003 .extent = { staging_texture.tex_width, staging_texture.tex_height, 1 },
1004 };
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001005 vkCmdCopyImage(demo->cmd,
1006 staging_texture.image, VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL,
1007 demo->textures[i].image, VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL,
Courtney Goeltzenleuchter8e89a312015-03-25 11:25:10 -06001008 1, &copy_region);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001009
Courtney Goeltzenleuchter2c2fbbf2015-04-07 17:13:38 -06001010 demo_add_mem_refs(demo, staging_texture.num_mem, staging_texture.mem);
1011 demo_add_mem_refs(demo, demo->textures[i].num_mem, demo->textures[i].mem);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001012
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001013 demo_set_image_layout(demo, demo->textures[i].image,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001014 VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001015 demo->textures[i].imageLayout);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001016
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001017 demo_flush_init_cmd(demo);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001018
Courtney Goeltzenleuchter1359b7a2015-04-02 16:25:42 -06001019 demo_remove_mem_refs(demo, staging_texture.num_mem, staging_texture.mem);
Courtney Goeltzenleuchterf3aeb2b2015-04-21 09:30:03 -06001020 demo_destroy_texture_image(demo, &staging_texture);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001021 } else {
Tony Barbour72304ef2015-04-16 15:59:00 -06001022 /* Can't support VK_FORMAT_B8G8R8A8_UNORM !? */
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001023 assert(!"No support for tB8G8R8A8_UNORM as texture image format");
1024 }
1025
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001026 const VkSamplerCreateInfo sampler = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001027 .sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001028 .pNext = NULL,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001029 .magFilter = VK_TEX_FILTER_NEAREST,
1030 .minFilter = VK_TEX_FILTER_NEAREST,
Tony Barbour72304ef2015-04-16 15:59:00 -06001031 .mipMode = VK_TEX_MIPMAP_MODE_BASE,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001032 .addressU = VK_TEX_ADDRESS_CLAMP,
1033 .addressV = VK_TEX_ADDRESS_CLAMP,
1034 .addressW = VK_TEX_ADDRESS_CLAMP,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001035 .mipLodBias = 0.0f,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001036 .maxAnisotropy = 1,
Tony Barbour72304ef2015-04-16 15:59:00 -06001037 .compareOp = VK_COMPARE_OP_NEVER,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001038 .minLod = 0.0f,
1039 .maxLod = 0.0f,
Tony Barbour72304ef2015-04-16 15:59:00 -06001040 .borderColor = VK_BORDER_COLOR_OPAQUE_WHITE,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001041 };
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001042
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001043 VkImageViewCreateInfo view = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001044 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001045 .pNext = NULL,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001046 .image = VK_NULL_HANDLE,
Tony Barbour72304ef2015-04-16 15:59:00 -06001047 .viewType = VK_IMAGE_VIEW_TYPE_2D,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001048 .format = tex_format,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001049 .channels = { VK_CHANNEL_SWIZZLE_R,
1050 VK_CHANNEL_SWIZZLE_G,
1051 VK_CHANNEL_SWIZZLE_B,
1052 VK_CHANNEL_SWIZZLE_A, },
1053 .subresourceRange = { VK_IMAGE_ASPECT_COLOR, 0, 1, 0, 1 },
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001054 .minLod = 0.0f,
1055 };
Jon Ashburnb2a66652015-01-16 09:37:43 -07001056
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001057 /* create sampler */
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001058 err = vkCreateSampler(demo->device, &sampler,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001059 &demo->textures[i].sampler);
1060 assert(!err);
1061
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001062 /* create image view */
1063 view.image = demo->textures[i].image;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001064 err = vkCreateImageView(demo->device, &view,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001065 &demo->textures[i].view);
1066 assert(!err);
1067 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001068}
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001069
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001070void demo_prepare_cube_data_buffer(struct demo *demo)
1071{
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -06001072 VkBufferCreateInfo buf_info;
1073 VkBufferViewCreateInfo view_info;
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -06001074 VkMemoryAllocInfo alloc_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001075 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
Mark Lobodzinskia6907f72015-04-16 08:52:00 -05001076 .pNext = NULL,
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001077 .allocationSize = 0,
Tony Barbour72304ef2015-04-16 15:59:00 -06001078 .memProps = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001079 .memPriority = VK_MEMORY_PRIORITY_NORMAL,
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001080 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001081 VkMemoryRequirements *mem_reqs;
1082 size_t mem_reqs_size = sizeof(VkMemoryRequirements);
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001083 uint32_t num_allocations = 0;
1084 size_t num_alloc_size = sizeof(num_allocations);
1085 uint8_t *pData;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001086 int i;
1087 mat4x4 MVP, VP;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001088 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001089 struct vktexcube_vs_uniform data;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001090
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001091 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001092 mat4x4_mul(MVP, VP, demo->model_matrix);
1093 memcpy(data.mvp, MVP, sizeof(MVP));
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001094// dumpMatrix("MVP", MVP);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001095
1096 for (i=0; i<12*3; i++) {
1097 data.position[i][0] = g_vertex_buffer_data[i*3];
1098 data.position[i][1] = g_vertex_buffer_data[i*3+1];
1099 data.position[i][2] = g_vertex_buffer_data[i*3+2];
1100 data.position[i][3] = 1.0f;
1101 data.attr[i][0] = g_uv_buffer_data[2*i];
1102 data.attr[i][1] = g_uv_buffer_data[2*i + 1];
1103 data.attr[i][2] = 0;
1104 data.attr[i][3] = 0;
1105 }
1106
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001107 memset(&buf_info, 0, sizeof(buf_info));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001108 buf_info.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001109 buf_info.size = sizeof(data);
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -06001110 buf_info.usage = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001111 err = vkCreateBuffer(demo->device, &buf_info, &demo->uniform_data.buf);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001112 assert(!err);
1113
Mike Stroyanebae8322015-04-17 12:36:38 -06001114 err = vkGetObjectInfo(demo->device,
1115 VK_OBJECT_TYPE_BUFFER, demo->uniform_data.buf,
Tony Barbour72304ef2015-04-16 15:59:00 -06001116 VK_OBJECT_INFO_TYPE_MEMORY_ALLOCATION_COUNT,
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001117 &num_alloc_size, &num_allocations);
1118 assert(!err && num_alloc_size == sizeof(num_allocations));
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001119 mem_reqs = malloc(num_allocations * sizeof(VkMemoryRequirements));
Tony Barbour72304ef2015-04-16 15:59:00 -06001120 demo->uniform_data.mem = malloc(num_allocations * sizeof(VkDeviceMemory));
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001121 demo->uniform_data.num_mem = num_allocations;
Mike Stroyanebae8322015-04-17 12:36:38 -06001122 err = vkGetObjectInfo(demo->device,
1123 VK_OBJECT_TYPE_BUFFER, demo->uniform_data.buf,
Tony Barbour72304ef2015-04-16 15:59:00 -06001124 VK_OBJECT_INFO_TYPE_MEMORY_REQUIREMENTS,
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001125 &mem_reqs_size, mem_reqs);
1126 assert(!err && mem_reqs_size == num_allocations * sizeof(*mem_reqs));
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001127 for (uint32_t i = 0; i < num_allocations; i ++) {
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001128 alloc_info.allocationSize = mem_reqs[i].size;
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001129
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001130 err = vkAllocMemory(demo->device, &alloc_info, &(demo->uniform_data.mem[i]));
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001131 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001132
Mike Stroyanebae8322015-04-17 12:36:38 -06001133 err = vkMapMemory(demo->device, demo->uniform_data.mem[i], 0, 0, 0, (void **) &pData);
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001134 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001135
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001136 memcpy(pData, &data, sizeof data);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001137
Mike Stroyanebae8322015-04-17 12:36:38 -06001138 err = vkUnmapMemory(demo->device, demo->uniform_data.mem[i]);
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001139 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001140
Mike Stroyanebae8322015-04-17 12:36:38 -06001141 err = vkQueueBindObjectMemory(demo->queue,
1142 VK_OBJECT_TYPE_BUFFER, demo->uniform_data.buf,
1143 i, demo->uniform_data.mem[i], 0);
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001144 assert(!err);
1145 }
Courtney Goeltzenleuchter2c2fbbf2015-04-07 17:13:38 -06001146 demo_add_mem_refs(demo, demo->uniform_data.num_mem, demo->uniform_data.mem);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001147
1148 memset(&view_info, 0, sizeof(view_info));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001149 view_info.sType = VK_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO;
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001150 view_info.buffer = demo->uniform_data.buf;
Tony Barbour72304ef2015-04-16 15:59:00 -06001151 view_info.viewType = VK_BUFFER_VIEW_TYPE_RAW;
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001152 view_info.offset = 0;
1153 view_info.range = sizeof(data);
1154
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001155 err = vkCreateBufferView(demo->device, &view_info, &demo->uniform_data.view);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001156 assert(!err);
1157
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001158 demo->uniform_data.attach.sType = VK_STRUCTURE_TYPE_BUFFER_VIEW_ATTACH_INFO;
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001159 demo->uniform_data.attach.view = demo->uniform_data.view;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001160}
1161
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001162static void demo_prepare_descriptor_layout(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001163{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001164 const VkDescriptorSetLayoutBinding layout_bindings[2] = {
Chia-I Wua2aa8632015-03-26 15:04:41 +08001165 [0] = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001166 .descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
Chia-I Wua2aa8632015-03-26 15:04:41 +08001167 .count = 1,
Tony Barbour72304ef2015-04-16 15:59:00 -06001168 .stageFlags = VK_SHADER_STAGE_VERTEX_BIT,
Chia-I Wu91e8e212015-03-27 12:56:09 +08001169 .pImmutableSamplers = NULL,
Chia-I Wua2aa8632015-03-26 15:04:41 +08001170 },
1171 [1] = {
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -06001172 .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
Chia-I Wua2aa8632015-03-26 15:04:41 +08001173 .count = DEMO_TEXTURE_COUNT,
Tony Barbour72304ef2015-04-16 15:59:00 -06001174 .stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT,
Chia-I Wu91e8e212015-03-27 12:56:09 +08001175 .pImmutableSamplers = NULL,
Chia-I Wua2aa8632015-03-26 15:04:41 +08001176 },
1177 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001178 const VkDescriptorSetLayoutCreateInfo descriptor_layout = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001179 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001180 .pNext = NULL,
Chia-I Wua2aa8632015-03-26 15:04:41 +08001181 .count = 2,
1182 .pBinding = layout_bindings,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001183 };
Tony Barbourfdc2d352015-04-22 09:02:32 -06001184 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001185
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001186 err = vkCreateDescriptorSetLayout(demo->device,
Chia-I Wub58c24a2015-03-26 15:27:55 +08001187 &descriptor_layout, &demo->desc_layout);
1188 assert(!err);
1189
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001190 const VkPipelineLayoutCreateInfo pPipelineLayoutCreateInfo = {
1191 .sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
1192 .pNext = NULL,
1193 .descriptorSetCount = 1,
1194 .pSetLayouts = &demo->desc_layout,
1195 };
1196
1197 err = vkCreatePipelineLayout(demo->device,
1198 &pPipelineLayoutCreateInfo,
1199 &demo->pipeline_layout);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001200 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001201}
1202
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001203static VkShader demo_prepare_shader(struct demo *demo,
Tony Barbour72304ef2015-04-16 15:59:00 -06001204 VkShaderStage stage,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001205 const void *code,
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001206 size_t size)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001207{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001208 VkShaderCreateInfo createInfo;
1209 VkShader shader;
1210 VkResult err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001211
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001212
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001213 createInfo.sType = VK_STRUCTURE_TYPE_SHADER_CREATE_INFO;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001214 createInfo.pNext = NULL;
1215
Cody Northrop8b12a1f2015-03-17 15:55:58 -06001216#ifdef EXTERNAL_SPV
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001217 createInfo.codeSize = size;
1218 createInfo.pCode = code;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001219 createInfo.flags = 0;
1220
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001221 err = vkCreateShader(demo->device, &createInfo, &shader);
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001222 if (err) {
1223 free((void *) createInfo.pCode);
1224 }
1225#else
Cody Northrop8b12a1f2015-03-17 15:55:58 -06001226 // Create fake SPV structure to feed GLSL
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001227 // to the driver "under the covers"
1228 createInfo.codeSize = 3 * sizeof(uint32_t) + size + 1;
1229 createInfo.pCode = malloc(createInfo.codeSize);
1230 createInfo.flags = 0;
1231
Tony Barbour72304ef2015-04-16 15:59:00 -06001232 /* try version 0 first: VkShaderStage followed by GLSL */
Cody Northrop8b12a1f2015-03-17 15:55:58 -06001233 ((uint32_t *) createInfo.pCode)[0] = ICD_SPV_MAGIC;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001234 ((uint32_t *) createInfo.pCode)[1] = 0;
1235 ((uint32_t *) createInfo.pCode)[2] = stage;
1236 memcpy(((uint32_t *) createInfo.pCode + 3), code, size + 1);
1237
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001238 err = vkCreateShader(demo->device, &createInfo, &shader);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001239 if (err) {
1240 free((void *) createInfo.pCode);
Tony Barbourf9921732015-04-22 11:36:22 -06001241 return (VkShader) VK_NULL_HANDLE;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001242 }
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001243#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001244
1245 return shader;
1246}
1247
Cody Northrop8b12a1f2015-03-17 15:55:58 -06001248char *demo_read_spv(const char *filename, size_t *psize)
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001249{
1250 long int size;
Tony Barbourf9921732015-04-22 11:36:22 -06001251 size_t U_ASSERT_ONLY retval;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001252 void *shader_code;
1253
1254 FILE *fp = fopen(filename, "rb");
1255 if (!fp) return NULL;
1256
1257 fseek(fp, 0L, SEEK_END);
1258 size = ftell(fp);
1259
1260 fseek(fp, 0L, SEEK_SET);
1261
1262 shader_code = malloc(size);
Tony Barbourfdc2d352015-04-22 09:02:32 -06001263 retval = fread(shader_code, size, 1, fp);
1264 assert(retval == 1);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001265
1266 *psize = size;
1267
1268 return shader_code;
1269}
1270
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001271static VkShader demo_prepare_vs(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001272{
Cody Northrop8b12a1f2015-03-17 15:55:58 -06001273#ifdef EXTERNAL_SPV
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001274 void *vertShaderCode;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001275 size_t size;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001276
Cody Northrop8b12a1f2015-03-17 15:55:58 -06001277 vertShaderCode = demo_read_spv("cube-vert.spv", &size);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001278
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001279 return demo_prepare_shader(demo, VK_SHADER_STAGE_VERTEX,
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001280 vertShaderCode, size);
1281#else
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001282 static const char *vertShaderText =
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001283 "#version 140\n"
1284 "#extension GL_ARB_separate_shader_objects : enable\n"
1285 "#extension GL_ARB_shading_language_420pack : enable\n"
1286 "\n"
1287 "layout(binding = 0) uniform buf {\n"
1288 " mat4 MVP;\n"
1289 " vec4 position[12*3];\n"
1290 " vec4 attr[12*3];\n"
1291 "} ubuf;\n"
1292 "\n"
1293 "layout (location = 0) out vec4 texcoord;\n"
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001294 "\n"
1295 "void main() \n"
1296 "{\n"
1297 " texcoord = ubuf.attr[gl_VertexID];\n"
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001298 " gl_Position = ubuf.MVP * ubuf.position[gl_VertexID];\n"
1299 "}\n";
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001300
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001301 return demo_prepare_shader(demo, VK_SHADER_STAGE_VERTEX,
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001302 (const void *) vertShaderText,
1303 strlen(vertShaderText));
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001304#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001305}
1306
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001307static VkShader demo_prepare_fs(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001308{
Cody Northrop8b12a1f2015-03-17 15:55:58 -06001309#ifdef EXTERNAL_SPV
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001310 void *fragShaderCode;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001311 size_t size;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001312
Cody Northrop8b12a1f2015-03-17 15:55:58 -06001313 fragShaderCode = demo_read_spv("cube-frag.spv", &size);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001314
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001315 return demo_prepare_shader(demo, VK_SHADER_STAGE_FRAGMENT,
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001316 fragShaderCode, size);
1317#else
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001318 static const char *fragShaderText =
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001319 "#version 140\n"
1320 "#extension GL_ARB_separate_shader_objects : enable\n"
1321 "#extension GL_ARB_shading_language_420pack : enable\n"
Courtney Goeltzenleuchter4d1acf12015-02-25 15:13:35 -07001322 "layout (binding = 1) uniform sampler2D tex;\n"
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001323 "\n"
1324 "layout (location = 0) in vec4 texcoord;\n"
1325 "void main() {\n"
1326 " gl_FragColor = texture(tex, texcoord.xy);\n"
1327 "}\n";
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001328
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001329 return demo_prepare_shader(demo, VK_SHADER_STAGE_FRAGMENT,
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001330 (const void *) fragShaderText,
1331 strlen(fragShaderText));
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001332#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001333}
1334
1335static void demo_prepare_pipeline(struct demo *demo)
1336{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001337 VkGraphicsPipelineCreateInfo pipeline;
1338 VkPipelineIaStateCreateInfo ia;
1339 VkPipelineRsStateCreateInfo rs;
1340 VkPipelineCbStateCreateInfo cb;
1341 VkPipelineDsStateCreateInfo ds;
1342 VkPipelineShaderStageCreateInfo vs;
1343 VkPipelineShaderStageCreateInfo fs;
1344 VkPipelineVpStateCreateInfo vp;
1345 VkPipelineMsStateCreateInfo ms;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001346 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001347
1348 memset(&pipeline, 0, sizeof(pipeline));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001349 pipeline.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001350 pipeline.layout = demo->pipeline_layout;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001351
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001352 memset(&ia, 0, sizeof(ia));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001353 ia.sType = VK_STRUCTURE_TYPE_PIPELINE_IA_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06001354 ia.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001355
1356 memset(&rs, 0, sizeof(rs));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001357 rs.sType = VK_STRUCTURE_TYPE_PIPELINE_RS_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06001358 rs.fillMode = VK_FILL_MODE_SOLID;
1359 rs.cullMode = VK_CULL_MODE_BACK;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001360 rs.frontFace = VK_FRONT_FACE_CCW;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001361
1362 memset(&cb, 0, sizeof(cb));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001363 cb.sType = VK_STRUCTURE_TYPE_PIPELINE_CB_STATE_CREATE_INFO;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001364 VkPipelineCbAttachmentState att_state[1];
Tony Barbour29645d02015-01-16 14:27:35 -07001365 memset(att_state, 0, sizeof(att_state));
1366 att_state[0].format = demo->format;
1367 att_state[0].channelWriteMask = 0xf;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001368 att_state[0].blendEnable = VK_FALSE;
Tony Barbour29645d02015-01-16 14:27:35 -07001369 cb.attachmentCount = 1;
1370 cb.pAttachments = att_state;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001371
Tony Barbour29645d02015-01-16 14:27:35 -07001372 memset(&vp, 0, sizeof(vp));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001373 vp.sType = VK_STRUCTURE_TYPE_PIPELINE_VP_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06001374 vp.viewportCount = 1;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001375 vp.clipOrigin = VK_COORDINATE_ORIGIN_LOWER_LEFT;
Tony Barbour29645d02015-01-16 14:27:35 -07001376
1377 memset(&ds, 0, sizeof(ds));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001378 ds.sType = VK_STRUCTURE_TYPE_PIPELINE_DS_STATE_CREATE_INFO;
Tony Barbour29645d02015-01-16 14:27:35 -07001379 ds.format = demo->depth.format;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001380 ds.depthTestEnable = VK_TRUE;
1381 ds.depthWriteEnable = VK_TRUE;
Tony Barbour72304ef2015-04-16 15:59:00 -06001382 ds.depthCompareOp = VK_COMPARE_OP_LESS_EQUAL;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001383 ds.depthBoundsEnable = VK_FALSE;
1384 ds.back.stencilFailOp = VK_STENCIL_OP_KEEP;
1385 ds.back.stencilPassOp = VK_STENCIL_OP_KEEP;
Tony Barbour72304ef2015-04-16 15:59:00 -06001386 ds.back.stencilCompareOp = VK_COMPARE_OP_ALWAYS;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001387 ds.stencilTestEnable = VK_FALSE;
Tony Barbour29645d02015-01-16 14:27:35 -07001388 ds.front = ds.back;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001389
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001390 memset(&vs, 0, sizeof(vs));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001391 vs.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1392 vs.shader.stage = VK_SHADER_STAGE_VERTEX;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001393 vs.shader.shader = demo_prepare_vs(demo);
Mike Stroyanebae8322015-04-17 12:36:38 -06001394 assert(vs.shader.shader != VK_NULL_HANDLE);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001395
1396 memset(&fs, 0, sizeof(fs));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001397 fs.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1398 fs.shader.stage = VK_SHADER_STAGE_FRAGMENT;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001399 fs.shader.shader = demo_prepare_fs(demo);
Mike Stroyanebae8322015-04-17 12:36:38 -06001400 assert(fs.shader.shader != VK_NULL_HANDLE);
Tony Barbour29645d02015-01-16 14:27:35 -07001401
1402 memset(&ms, 0, sizeof(ms));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001403 ms.sType = VK_STRUCTURE_TYPE_PIPELINE_MS_STATE_CREATE_INFO;
Tony Barbour29645d02015-01-16 14:27:35 -07001404 ms.sampleMask = 1;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001405 ms.multisampleEnable = VK_FALSE;
Tony Barbour29645d02015-01-16 14:27:35 -07001406 ms.samples = 1;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001407
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001408 pipeline.pNext = (const void *) &ia;
1409 ia.pNext = (const void *) &rs;
1410 rs.pNext = (const void *) &cb;
1411 cb.pNext = (const void *) &ms;
1412 ms.pNext = (const void *) &vp;
1413 vp.pNext = (const void *) &ds;
1414 ds.pNext = (const void *) &vs;
1415 vs.pNext = (const void *) &fs;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001416
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001417 err = vkCreateGraphicsPipeline(demo->device, &pipeline, &demo->pipeline);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001418 assert(!err);
1419
Mike Stroyanebae8322015-04-17 12:36:38 -06001420 vkDestroyObject(demo->device, VK_OBJECT_TYPE_SHADER, vs.shader.shader);
1421 vkDestroyObject(demo->device, VK_OBJECT_TYPE_SHADER, fs.shader.shader);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001422}
1423
1424static void demo_prepare_dynamic_states(struct demo *demo)
1425{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001426 VkDynamicVpStateCreateInfo viewport_create;
1427 VkDynamicRsStateCreateInfo raster;
1428 VkDynamicCbStateCreateInfo color_blend;
1429 VkDynamicDsStateCreateInfo depth_stencil;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001430 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001431
Tony Barbour29645d02015-01-16 14:27:35 -07001432 memset(&viewport_create, 0, sizeof(viewport_create));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001433 viewport_create.sType = VK_STRUCTURE_TYPE_DYNAMIC_VP_STATE_CREATE_INFO;
Courtney Goeltzenleuchter1710d8d2015-02-11 14:13:34 -07001434 viewport_create.viewportAndScissorCount = 1;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001435 VkViewport viewport;
Piers Daniell735ee532015-02-23 16:23:13 -07001436 memset(&viewport, 0, sizeof(viewport));
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001437 viewport.height = (float) demo->height;
1438 viewport.width = (float) demo->width;
1439 viewport.minDepth = (float) 0.0f;
1440 viewport.maxDepth = (float) 1.0f;
Piers Daniell735ee532015-02-23 16:23:13 -07001441 viewport_create.pViewports = &viewport;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001442 VkRect scissor;
Piers Daniell735ee532015-02-23 16:23:13 -07001443 memset(&scissor, 0, sizeof(scissor));
Courtney Goeltzenleuchter1710d8d2015-02-11 14:13:34 -07001444 scissor.extent.width = demo->width;
1445 scissor.extent.height = demo->height;
1446 scissor.offset.x = 0;
1447 scissor.offset.y = 0;
Courtney Goeltzenleuchter1710d8d2015-02-11 14:13:34 -07001448 viewport_create.pScissors = &scissor;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001449
1450 memset(&raster, 0, sizeof(raster));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001451 raster.sType = VK_STRUCTURE_TYPE_DYNAMIC_RS_STATE_CREATE_INFO;
Piers Daniell735ee532015-02-23 16:23:13 -07001452 raster.pointSize = 1.0;
1453 raster.lineWidth = 1.0;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001454
1455 memset(&color_blend, 0, sizeof(color_blend));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001456 color_blend.sType = VK_STRUCTURE_TYPE_DYNAMIC_CB_STATE_CREATE_INFO;
Piers Daniell735ee532015-02-23 16:23:13 -07001457 color_blend.blendConst[0] = 1.0f;
1458 color_blend.blendConst[1] = 1.0f;
1459 color_blend.blendConst[2] = 1.0f;
1460 color_blend.blendConst[3] = 1.0f;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001461
1462 memset(&depth_stencil, 0, sizeof(depth_stencil));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001463 depth_stencil.sType = VK_STRUCTURE_TYPE_DYNAMIC_DS_STATE_CREATE_INFO;
Piers Daniell735ee532015-02-23 16:23:13 -07001464 depth_stencil.minDepth = 0.0f;
1465 depth_stencil.maxDepth = 1.0f;
Tony Barbour29645d02015-01-16 14:27:35 -07001466 depth_stencil.stencilBackRef = 0;
1467 depth_stencil.stencilFrontRef = 0;
1468 depth_stencil.stencilReadMask = 0xff;
1469 depth_stencil.stencilWriteMask = 0xff;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001470
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001471 err = vkCreateDynamicViewportState(demo->device, &viewport_create, &demo->viewport);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001472 assert(!err);
1473
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001474 err = vkCreateDynamicRasterState(demo->device, &raster, &demo->raster);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001475 assert(!err);
1476
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001477 err = vkCreateDynamicColorBlendState(demo->device,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001478 &color_blend, &demo->color_blend);
1479 assert(!err);
1480
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001481 err = vkCreateDynamicDepthStencilState(demo->device,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001482 &depth_stencil, &demo->depth_stencil);
1483 assert(!err);
1484}
1485
Chia-I Wu63ea9262015-03-26 13:14:16 +08001486static void demo_prepare_descriptor_pool(struct demo *demo)
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001487{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001488 const VkDescriptorTypeCount type_counts[2] = {
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001489 [0] = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001490 .type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001491 .count = 1,
1492 },
1493 [1] = {
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -06001494 .type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001495 .count = DEMO_TEXTURE_COUNT,
1496 },
1497 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001498 const VkDescriptorPoolCreateInfo descriptor_pool = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001499 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001500 .pNext = NULL,
1501 .count = 2,
1502 .pTypeCount = type_counts,
1503 };
Tony Barbourfdc2d352015-04-22 09:02:32 -06001504 VkResult U_ASSERT_ONLY err;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001505
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001506 err = vkCreateDescriptorPool(demo->device,
1507 VK_DESCRIPTOR_POOL_USAGE_ONE_SHOT, 1,
Chia-I Wu63ea9262015-03-26 13:14:16 +08001508 &descriptor_pool, &demo->desc_pool);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001509 assert(!err);
1510}
1511
1512static void demo_prepare_descriptor_set(struct demo *demo)
1513{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001514 VkImageViewAttachInfo view_info[DEMO_TEXTURE_COUNT];
1515 VkSamplerImageViewInfo combined_info[DEMO_TEXTURE_COUNT];
1516 VkUpdateSamplerTextures update_fs;
1517 VkUpdateBuffers update_vs;
Chia-I Wub58c24a2015-03-26 15:27:55 +08001518 const void *update_array[2] = { &update_vs, &update_fs };
Tony Barbourfdc2d352015-04-22 09:02:32 -06001519 VkResult U_ASSERT_ONLY err;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001520 uint32_t count;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001521 uint32_t i;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001522
1523 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001524 view_info[i].sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_ATTACH_INFO;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001525 view_info[i].pNext = NULL;
1526 view_info[i].view = demo->textures[i].view,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001527 view_info[i].layout = VK_IMAGE_LAYOUT_GENERAL;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001528
Courtney Goeltzenleuchtereb4754f2015-04-09 11:43:10 -06001529 combined_info[i].sampler = demo->textures[i].sampler;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001530 combined_info[i].pImageView = &view_info[i];
1531 }
1532
1533 memset(&update_vs, 0, sizeof(update_vs));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001534 update_vs.sType = VK_STRUCTURE_TYPE_UPDATE_BUFFERS;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001535 update_vs.pNext = &update_fs;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001536 update_vs.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001537 update_vs.count = 1;
Chia-I Wub58c24a2015-03-26 15:27:55 +08001538 update_vs.pBufferViews = &demo->uniform_data.attach;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001539
1540 memset(&update_fs, 0, sizeof(update_fs));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001541 update_fs.sType = VK_STRUCTURE_TYPE_UPDATE_SAMPLER_TEXTURES;
Chia-I Wub58c24a2015-03-26 15:27:55 +08001542 update_fs.binding = 1;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001543 update_fs.count = DEMO_TEXTURE_COUNT;
1544 update_fs.pSamplerImageViews = combined_info;
1545
Mike Stroyanebae8322015-04-17 12:36:38 -06001546 err = vkAllocDescriptorSets(demo->device, demo->desc_pool,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001547 VK_DESCRIPTOR_SET_USAGE_STATIC,
Chia-I Wu87544e72015-02-23 10:41:08 -07001548 1, &demo->desc_layout,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001549 &demo->desc_set, &count);
1550 assert(!err && count == 1);
1551
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001552 vkBeginDescriptorPoolUpdate(demo->device,
1553 VK_DESCRIPTOR_UPDATE_MODE_FASTEST);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001554
Mike Stroyanebae8322015-04-17 12:36:38 -06001555 vkClearDescriptorSets(demo->device, demo->desc_pool, 1, &demo->desc_set);
1556 vkUpdateDescriptors(demo->device, demo->desc_set, 2, update_array);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001557
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001558 vkEndDescriptorPoolUpdate(demo->device, demo->buffers[demo->current_buffer].cmd);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001559}
1560
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001561static void demo_prepare(struct demo *demo)
1562{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001563 const VkCmdBufferCreateInfo cmd = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001564 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001565 .pNext = NULL,
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07001566 .queueNodeIndex = demo->graphics_queue_node_index,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001567 .flags = 0,
1568 };
Tony Barbourfdc2d352015-04-22 09:02:32 -06001569 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001570
1571 demo_prepare_buffers(demo);
1572 demo_prepare_depth(demo);
1573 demo_prepare_textures(demo);
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001574 demo_prepare_cube_data_buffer(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001575
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001576 demo_prepare_descriptor_layout(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001577 demo_prepare_pipeline(demo);
1578 demo_prepare_dynamic_states(demo);
1579
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001580 for (int i = 0; i < DEMO_BUFFER_COUNT; i++) {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001581 err = vkCreateCommandBuffer(demo->device, &cmd, &demo->buffers[i].cmd);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001582 assert(!err);
1583 }
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001584
Chia-I Wu63ea9262015-03-26 13:14:16 +08001585 demo_prepare_descriptor_pool(demo);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001586 demo_prepare_descriptor_set(demo);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001587
1588
1589 for (int i = 0; i < DEMO_BUFFER_COUNT; i++) {
1590 demo->current_buffer = i;
1591 demo_draw_build_cmd(demo, demo->buffers[i].cmd);
1592 }
1593
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001594 /*
1595 * Prepare functions above may generate pipeline commands
1596 * that need to be flushed before beginning the render loop.
1597 */
1598 demo_flush_init_cmd(demo);
1599
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001600 demo->current_buffer = 0;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001601}
1602
Ian Elliott639ca472015-04-16 15:23:05 -06001603#ifdef _WIN32
1604static void demo_run(struct demo *demo)
1605{
1606 // Wait for work to finish before updating MVP.
1607 vkDeviceWaitIdle(demo->device);
1608 demo_update_data_buffer(demo);
1609
1610 demo_draw(demo);
1611
1612 // Wait for work to finish before updating MVP.
1613 vkDeviceWaitIdle(demo->device);
1614}
1615
1616// On MS-Windows, make this a global, so it's available to WndProc()
1617struct demo demo;
1618
1619// MS-Windows event handling function:
1620LRESULT CALLBACK WndProc(HWND hWnd,
1621 UINT uMsg,
1622 WPARAM wParam,
1623 LPARAM lParam)
1624{
Ian Elliott639ca472015-04-16 15:23:05 -06001625 char tmp_str[] = "Test Vulkan Cube Program";
1626
1627 switch(uMsg)
1628 {
1629 case WM_CREATE:
1630 return 0;
1631 case WM_CLOSE:
1632 PostQuitMessage(0);
1633 return 0;
1634 case WM_PAINT:
1635 demo_run(&demo);
1636 return 0;
1637 default:
1638 break;
1639 }
1640 return (DefWindowProc(hWnd, uMsg, wParam, lParam));
1641}
1642
1643static void demo_create_window(struct demo *demo)
1644{
1645 WNDCLASSEX win_class;
1646
1647 // Initialize the window class structure:
1648 win_class.cbSize = sizeof(WNDCLASSEX);
1649 win_class.style = CS_HREDRAW | CS_VREDRAW;
1650 win_class.lpfnWndProc = WndProc;
1651 win_class.cbClsExtra = 0;
1652 win_class.cbWndExtra = 0;
1653 win_class.hInstance = demo->connection; // hInstance
1654 win_class.hIcon = LoadIcon(NULL, IDI_APPLICATION);
1655 win_class.hCursor = LoadCursor(NULL, IDC_ARROW);
1656 win_class.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
1657 win_class.lpszMenuName = NULL;
1658 win_class.lpszClassName = demo->name;
1659 win_class.hIconSm = LoadIcon(NULL, IDI_WINLOGO);
1660 // Register window class:
1661 if (!RegisterClassEx(&win_class)) {
1662 // It didn't work, so try to give a useful error:
1663 printf("Unexpected error trying to start the application!\n");
1664 fflush(stdout);
1665 exit(1);
1666 }
1667 // Create window with the registered class:
1668 demo->window = CreateWindowEx(0,
1669 demo->name, // class name
1670 demo->name, // app name
1671 WS_OVERLAPPEDWINDOW | // window style
1672 WS_VISIBLE |
1673 WS_SYSMENU,
1674 100,100, // x/y coords
1675 demo->width, // width
1676 demo->height, // height
1677 NULL, // handle to parent
1678 NULL, // handle to menu
1679 demo->connection, // hInstance
1680 NULL); // no extra parameters
1681 if (!demo->window) {
1682 // It didn't work, so try to give a useful error:
1683 printf("Cannot create a window in which to draw!\n");
1684 fflush(stdout);
1685 exit(1);
1686 }
1687}
1688#else // _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001689static void demo_handle_event(struct demo *demo,
1690 const xcb_generic_event_t *event)
1691{
Piers Daniell735ee532015-02-23 16:23:13 -07001692 uint8_t event_code = event->response_type & 0x7f;
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07001693 switch (event_code) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001694 case XCB_EXPOSE:
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07001695 // TODO: Resize window
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001696 break;
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07001697 case XCB_CLIENT_MESSAGE:
1698 if((*(xcb_client_message_event_t*)event).data.data32[0] ==
1699 (*demo->atom_wm_delete_window).atom) {
1700 demo->quit = true;
1701 }
1702 break;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001703 case XCB_KEY_RELEASE:
1704 {
1705 const xcb_key_release_event_t *key =
1706 (const xcb_key_release_event_t *) event;
1707
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001708 switch (key->detail) {
1709 case 0x9: // Escape
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001710 demo->quit = true;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001711 break;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001712 case 0x71: // left arrow key
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001713 demo->spin_angle += demo->spin_increment;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001714 break;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001715 case 0x72: // right arrow key
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001716 demo->spin_angle -= demo->spin_increment;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001717 break;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001718 case 0x41:
1719 demo->pause = !demo->pause;
Piers Daniell735ee532015-02-23 16:23:13 -07001720 break;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001721 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001722 }
1723 break;
1724 default:
1725 break;
1726 }
1727}
1728
1729static void demo_run(struct demo *demo)
1730{
1731 xcb_flush(demo->connection);
1732
1733 while (!demo->quit) {
1734 xcb_generic_event_t *event;
1735
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07001736 if (demo->pause) {
1737 event = xcb_wait_for_event(demo->connection);
1738 } else {
1739 event = xcb_poll_for_event(demo->connection);
1740 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001741 if (event) {
1742 demo_handle_event(demo, event);
1743 free(event);
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07001744 }
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07001745
1746 // Wait for work to finish before updating MVP.
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001747 vkDeviceWaitIdle(demo->device);
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07001748 demo_update_data_buffer(demo);
1749
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07001750 demo_draw(demo);
Courtney Goeltzenleuchter1454f3c2014-11-18 11:28:09 -07001751
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07001752 // Wait for work to finish before updating MVP.
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001753 vkDeviceWaitIdle(demo->device);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001754 }
1755}
1756
1757static void demo_create_window(struct demo *demo)
1758{
1759 uint32_t value_mask, value_list[32];
1760
1761 demo->window = xcb_generate_id(demo->connection);
1762
1763 value_mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK;
1764 value_list[0] = demo->screen->black_pixel;
1765 value_list[1] = XCB_EVENT_MASK_KEY_RELEASE |
1766 XCB_EVENT_MASK_EXPOSURE;
1767
1768 xcb_create_window(demo->connection,
1769 XCB_COPY_FROM_PARENT,
1770 demo->window, demo->screen->root,
1771 0, 0, demo->width, demo->height, 0,
1772 XCB_WINDOW_CLASS_INPUT_OUTPUT,
1773 demo->screen->root_visual,
1774 value_mask, value_list);
1775
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07001776 /* Magic code that will send notification when window is destroyed */
1777 xcb_intern_atom_cookie_t cookie = xcb_intern_atom(demo->connection, 1, 12,
1778 "WM_PROTOCOLS");
1779 xcb_intern_atom_reply_t* reply = xcb_intern_atom_reply(demo->connection, cookie, 0);
1780
1781 xcb_intern_atom_cookie_t cookie2 = xcb_intern_atom(demo->connection, 0, 16, "WM_DELETE_WINDOW");
1782 demo->atom_wm_delete_window = xcb_intern_atom_reply(demo->connection, cookie2, 0);
1783
1784 xcb_change_property(demo->connection, XCB_PROP_MODE_REPLACE,
1785 demo->window, (*reply).atom, 4, 32, 1,
1786 &(*demo->atom_wm_delete_window).atom);
1787 free(reply);
1788
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001789 xcb_map_window(demo->connection, demo->window);
1790}
Ian Elliott639ca472015-04-16 15:23:05 -06001791#endif // _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001792
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001793static void demo_init_vk(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001794{
Tobin Ehlis99239dc2015-04-16 18:04:57 -06001795 VkResult err;
1796 // Extensions to enable
1797 const char *ext_names[] = {
Chia-I Wucbb564e2015-04-16 22:02:10 +08001798 "VK_WSI_LunarG",
Tobin Ehlis99239dc2015-04-16 18:04:57 -06001799 };
1800 size_t extSize = sizeof(uint32_t);
1801 uint32_t extCount = 0;
1802 err = vkGetGlobalExtensionInfo(VK_EXTENSION_INFO_TYPE_COUNT, 0, &extSize, &extCount);
1803 assert(!err);
1804
1805 VkExtensionProperties extProp;
1806 extSize = sizeof(VkExtensionProperties);
Tony Barbourfdc2d352015-04-22 09:02:32 -06001807 bool32_t U_ASSERT_ONLY extFound = 0;
Tobin Ehlis99239dc2015-04-16 18:04:57 -06001808 for (uint32_t i = 0; i < extCount; i++) {
1809 err = vkGetGlobalExtensionInfo(VK_EXTENSION_INFO_TYPE_PROPERTIES, i, &extSize, &extProp);
1810 if (!strcmp(ext_names[0], extProp.extName))
1811 extFound = 1;
1812 }
1813 assert(extFound);
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001814 const VkApplicationInfo app = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001815 .sType = VK_STRUCTURE_TYPE_APPLICATION_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001816 .pNext = NULL,
Chia-I Wub2c81932014-12-27 15:16:07 +08001817 .pAppName = "cube",
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001818 .appVersion = 0,
Chia-I Wub2c81932014-12-27 15:16:07 +08001819 .pEngineName = "cube",
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001820 .engineVersion = 0,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001821 .apiVersion = VK_API_VERSION,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001822 };
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -06001823 const VkInstanceCreateInfo inst_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001824 .sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
Jon Ashburnab46b362015-04-04 14:52:07 -06001825 .pNext = NULL,
1826 .pAppInfo = &app,
1827 .pAllocCb = NULL,
Tobin Ehlis99239dc2015-04-16 18:04:57 -06001828 .extensionCount = 1,
1829 .ppEnabledExtensionNames = ext_names,
Jon Ashburnab46b362015-04-04 14:52:07 -06001830 };
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -06001831 const VkDeviceQueueCreateInfo queue = {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001832 .queueNodeIndex = 0,
1833 .queueCount = 1,
1834 };
Tobin Ehlis7537db92015-04-16 10:04:35 -06001835
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -06001836 const VkDeviceCreateInfo device = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001837 .sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001838 .pNext = NULL,
1839 .queueRecordCount = 1,
1840 .pRequestedQueues = &queue,
Tobin Ehlis99239dc2015-04-16 18:04:57 -06001841 .extensionCount = 1,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001842 .ppEnabledExtensionNames = ext_names,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001843 .flags = VK_DEVICE_CREATE_VALIDATION_BIT,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001844 };
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001845 uint32_t gpu_count;
1846 uint32_t i;
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07001847 size_t data_size;
1848 uint32_t queue_count;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001849
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001850 err = vkCreateInstance(&inst_info, &demo->inst);
Tony Barbour7c5603c2015-04-17 16:39:58 -06001851 if (err) {
1852#ifdef _WIN32
1853 MessageBox(NULL, "vkCreateInstance failed - do you have a Vulkan graphics driver installed?",
1854 "vkCreateInstance Failure", MB_OK);
1855#else
1856 printf("vkCreateInstance failed - Do you have a Vulkan graphics driver installed?"
1857 "(\nExiting ...\n");
Ian Elliott3979e282015-04-03 15:24:55 -06001858 fflush(stdout);
Tony Barbour7c5603c2015-04-17 16:39:58 -06001859#endif
Ian Elliott3979e282015-04-03 15:24:55 -06001860 exit(1);
Ian Elliott3979e282015-04-03 15:24:55 -06001861 }
Jon Ashburnab46b362015-04-04 14:52:07 -06001862
Jon Ashburn07c0c0c2015-04-15 11:31:12 -06001863 gpu_count = 1;
1864 err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, &demo->gpu);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001865 assert(!err && gpu_count == 1);
1866
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001867 err = vkCreateDevice(demo->gpu, &device, &demo->device);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001868 assert(!err);
1869
Tony Barbour72304ef2015-04-16 15:59:00 -06001870 err = vkGetPhysicalDeviceInfo(demo->gpu, VK_PHYSICAL_DEVICE_INFO_TYPE_PROPERTIES,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001871 &data_size, NULL);
1872 assert(!err);
1873
Tony Barbour72304ef2015-04-16 15:59:00 -06001874 demo->gpu_props = (VkPhysicalDeviceProperties *) malloc(data_size);
1875 err = vkGetPhysicalDeviceInfo(demo->gpu, VK_PHYSICAL_DEVICE_INFO_TYPE_PROPERTIES,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001876 &data_size, demo->gpu_props);
1877 assert(!err);
1878
Tony Barbour72304ef2015-04-16 15:59:00 -06001879 err = vkGetPhysicalDeviceInfo(demo->gpu, VK_PHYSICAL_DEVICE_INFO_TYPE_QUEUE_PROPERTIES,
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07001880 &data_size, NULL);
1881 assert(!err);
1882
Tony Barbour72304ef2015-04-16 15:59:00 -06001883 demo->queue_props = (VkPhysicalDeviceQueueProperties *) malloc(data_size);
1884 err = vkGetPhysicalDeviceInfo(demo->gpu, VK_PHYSICAL_DEVICE_INFO_TYPE_QUEUE_PROPERTIES,
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07001885 &data_size, demo->queue_props);
1886 assert(!err);
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001887 queue_count = (uint32_t)(data_size / sizeof(VkPhysicalDeviceQueueProperties));
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07001888 assert(queue_count >= 1);
1889
Mark Lobodzinskida9b1092015-04-16 11:44:05 -05001890 // Graphics queue and MemMgr queue can be separate.
1891 // TODO: Add support for separate queues, including synchronization,
1892 // and appropriate tracking for QueueSubmit and QueueBindObjectMemory
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07001893 for (i = 0; i < queue_count; i++) {
Mark Lobodzinskida9b1092015-04-16 11:44:05 -05001894 if ((demo->queue_props[i].queueFlags & VK_QUEUE_GRAPHICS_BIT) &&
1895 (demo->queue_props[i].queueFlags & VK_QUEUE_MEMMGR_BIT) )
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07001896 break;
1897 }
1898 assert(i < queue_count);
1899 demo->graphics_queue_node_index = i;
1900
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001901 err = vkGetDeviceQueue(demo->device, demo->graphics_queue_node_index,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001902 0, &demo->queue);
1903 assert(!err);
1904}
1905
1906static void demo_init_connection(struct demo *demo)
1907{
Ian Elliott639ca472015-04-16 15:23:05 -06001908#ifndef _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001909 const xcb_setup_t *setup;
1910 xcb_screen_iterator_t iter;
1911 int scr;
1912
1913 demo->connection = xcb_connect(NULL, &scr);
Ian Elliott3979e282015-04-03 15:24:55 -06001914 if (demo->connection == NULL) {
1915 printf("Cannot find a compatible Vulkan installable client driver "
1916 "(ICD).\nExiting ...\n");
1917 fflush(stdout);
1918 exit(1);
1919 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001920
1921 setup = xcb_get_setup(demo->connection);
1922 iter = xcb_setup_roots_iterator(setup);
1923 while (scr-- > 0)
1924 xcb_screen_next(&iter);
1925
1926 demo->screen = iter.data;
Ian Elliott639ca472015-04-16 15:23:05 -06001927#endif // _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001928}
1929
Ian Elliott639ca472015-04-16 15:23:05 -06001930#ifdef _WIN32
1931static void demo_init(struct demo *demo, HINSTANCE hInstance, LPSTR pCmdLine)
1932#else // _WIN32
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001933static void demo_init(struct demo *demo, int argc, char **argv)
Ian Elliott639ca472015-04-16 15:23:05 -06001934#endif // _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001935{
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001936 vec3 eye = {0.0f, 3.0f, 5.0f};
1937 vec3 origin = {0, 0, 0};
1938 vec3 up = {0.0f, -1.0f, 0.0};
Ian Elliott639ca472015-04-16 15:23:05 -06001939 bool argv_error = false;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001940
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001941 memset(demo, 0, sizeof(*demo));
1942
Ian Elliott639ca472015-04-16 15:23:05 -06001943#ifdef _WIN32
1944 demo->connection = hInstance;
1945 strncpy(demo->name, "cube", APP_NAME_STR_LEN);
1946
1947 if (strncmp(pCmdLine, "--use_staging", strlen("--use_staging")) == 0)
1948 demo->use_staging_buffer = true;
1949 else if (strlen(pCmdLine) != 0) {
1950 fprintf(stderr, "Do not recognize argument \"%s\".\n", pCmdLine);
1951 argv_error = true;
1952 }
1953#else // _WIN32
Piers Daniell735ee532015-02-23 16:23:13 -07001954 for (int i = 1; i < argc; i++) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001955 if (strncmp(argv[i], "--use_staging", strlen("--use_staging")) == 0)
1956 demo->use_staging_buffer = true;
Ian Elliott639ca472015-04-16 15:23:05 -06001957 else {
1958 fprintf(stderr, "Do not recognize argument \"%s\".\n", argv[i]);
1959 argv_error = true;
1960 }
1961 }
1962#endif // _WIN32
1963 if (argv_error) {
1964 fprintf(stderr, "Usage:\n cube [--use_staging]\n");
1965 fflush(stderr);
1966 exit(1);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001967 }
1968
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001969 demo_init_connection(demo);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001970 demo_init_vk(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001971
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001972 demo->width = 500;
1973 demo->height = 500;
Tony Barbour72304ef2015-04-16 15:59:00 -06001974 demo->format = VK_FORMAT_B8G8R8A8_UNORM;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001975
1976 demo->spin_angle = 0.01f;
1977 demo->spin_increment = 0.01f;
1978 demo->pause = false;
1979
Piers Daniell735ee532015-02-23 16:23:13 -07001980 mat4x4_perspective(demo->projection_matrix, (float)degreesToRadians(45.0f), 1.0f, 0.1f, 100.0f);
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001981 mat4x4_look_at(demo->view_matrix, eye, origin, up);
1982 mat4x4_identity(demo->model_matrix);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001983}
1984
1985static void demo_cleanup(struct demo *demo)
1986{
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001987 uint32_t i, j;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001988
Mike Stroyanebae8322015-04-17 12:36:38 -06001989 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DESCRIPTOR_SET, demo->desc_set);
1990 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DESCRIPTOR_POOL, demo->desc_pool);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001991
Mike Stroyanebae8322015-04-17 12:36:38 -06001992 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DYNAMIC_VP_STATE, demo->viewport);
1993 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DYNAMIC_RS_STATE, demo->raster);
1994 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DYNAMIC_CB_STATE, demo->color_blend);
1995 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DYNAMIC_DS_STATE, demo->depth_stencil);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001996
Mike Stroyanebae8322015-04-17 12:36:38 -06001997 vkDestroyObject(demo->device, VK_OBJECT_TYPE_PIPELINE, demo->pipeline);
1998 vkDestroyObject(demo->device, VK_OBJECT_TYPE_PIPELINE_LAYOUT, demo->pipeline_layout);
1999 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DESCRIPTOR_SET_LAYOUT, demo->desc_layout);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002000
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002001 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Mike Stroyanebae8322015-04-17 12:36:38 -06002002 vkDestroyObject(demo->device, VK_OBJECT_TYPE_IMAGE_VIEW, demo->textures[i].view);
2003 vkQueueBindObjectMemory(demo->queue, VK_OBJECT_TYPE_IMAGE, demo->textures[i].image, 0, VK_NULL_HANDLE, 0);
2004 vkDestroyObject(demo->device, VK_OBJECT_TYPE_IMAGE, demo->textures[i].image);
Courtney Goeltzenleuchter1359b7a2015-04-02 16:25:42 -06002005 demo_remove_mem_refs(demo, demo->textures[i].num_mem, demo->textures[i].mem);
Jon Ashburnb2a66652015-01-16 09:37:43 -07002006 for (j = 0; j < demo->textures[i].num_mem; j++)
Mike Stroyanebae8322015-04-17 12:36:38 -06002007 vkFreeMemory(demo->device, demo->textures[i].mem[j]);
2008 free(demo->textures[i].mem);
2009 vkDestroyObject(demo->device, VK_OBJECT_TYPE_SAMPLER, demo->textures[i].sampler);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002010 }
Chia-I Wucbb564e2015-04-16 22:02:10 +08002011 vkDestroySwapChainWSI(demo->swap_chain);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002012
Mike Stroyanebae8322015-04-17 12:36:38 -06002013 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DEPTH_STENCIL_VIEW, demo->depth.view);
2014 vkQueueBindObjectMemory(demo->queue, VK_OBJECT_TYPE_IMAGE, demo->depth.image, 0, VK_NULL_HANDLE, 0);
Courtney Goeltzenleuchter1359b7a2015-04-02 16:25:42 -06002015 demo_remove_mem_refs(demo, demo->depth.num_mem, demo->depth.mem);
Mike Stroyanebae8322015-04-17 12:36:38 -06002016 vkDestroyObject(demo->device, VK_OBJECT_TYPE_IMAGE, demo->depth.image);
Courtney Goeltzenleuchter1359b7a2015-04-02 16:25:42 -06002017 for (j = 0; j < demo->depth.num_mem; j++) {
Mike Stroyanebae8322015-04-17 12:36:38 -06002018 vkFreeMemory(demo->device, demo->depth.mem[j]);
Courtney Goeltzenleuchter1359b7a2015-04-02 16:25:42 -06002019 }
Mark Lobodzinski7b8fee62015-02-18 16:38:17 -06002020
Mike Stroyanebae8322015-04-17 12:36:38 -06002021 vkDestroyObject(demo->device, VK_OBJECT_TYPE_BUFFER_VIEW, demo->uniform_data.view);
2022 vkQueueBindObjectMemory(demo->queue, VK_OBJECT_TYPE_BUFFER, demo->uniform_data.buf, 0, VK_NULL_HANDLE, 0);
2023 vkDestroyObject(demo->device, VK_OBJECT_TYPE_BUFFER, demo->uniform_data.buf);
Courtney Goeltzenleuchter1359b7a2015-04-02 16:25:42 -06002024 demo_remove_mem_refs(demo, demo->uniform_data.num_mem, demo->uniform_data.mem);
Jon Ashburnae7c21c2015-01-19 15:00:26 -07002025 for (j = 0; j < demo->uniform_data.num_mem; j++)
Mike Stroyanebae8322015-04-17 12:36:38 -06002026 vkFreeMemory(demo->device, demo->uniform_data.mem[j]);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002027
2028 for (i = 0; i < DEMO_BUFFER_COUNT; i++) {
Mike Stroyanebae8322015-04-17 12:36:38 -06002029 vkDestroyObject(demo->device, VK_OBJECT_TYPE_COLOR_ATTACHMENT_VIEW, demo->buffers[i].view);
2030 vkDestroyObject(demo->device, VK_OBJECT_TYPE_COMMAND_BUFFER, demo->buffers[i].cmd);
Courtney Goeltzenleuchter1359b7a2015-04-02 16:25:42 -06002031 demo_remove_mem_refs(demo, 1, &demo->buffers[i].mem);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002032 }
2033
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002034 vkDestroyDevice(demo->device);
2035 vkDestroyInstance(demo->inst);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002036
Ian Elliott639ca472015-04-16 15:23:05 -06002037#ifndef _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002038 xcb_destroy_window(demo->connection, demo->window);
2039 xcb_disconnect(demo->connection);
Ian Elliott639ca472015-04-16 15:23:05 -06002040#endif // _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002041}
2042
Ian Elliott639ca472015-04-16 15:23:05 -06002043#ifdef _WIN32
2044int APIENTRY WinMain(HINSTANCE hInstance,
2045 HINSTANCE hPrevInstance,
2046 LPSTR pCmdLine,
2047 int nCmdShow)
2048{
2049 MSG msg; // message
2050 bool done; // flag saying when app is complete
2051
2052 demo_init(&demo, hInstance, pCmdLine);
2053 demo_create_window(&demo);
2054
2055 demo_prepare(&demo);
2056
2057 done = false; //initialize loop condition variable
2058 /* main message loop*/
2059 while(!done)
2060 {
Tony Barbourf9921732015-04-22 11:36:22 -06002061 PeekMessage(&msg,0,0,0,PM_REMOVE);
Ian Elliott639ca472015-04-16 15:23:05 -06002062 if (msg.message == WM_QUIT) //check for a quit message
2063 {
2064 done = true; //if found, quit app
2065 }
2066 else
2067 {
2068 /* Translate and dispatch to event queue*/
2069 TranslateMessage(&msg);
2070 DispatchMessage(&msg);
2071 }
2072 }
2073
2074 demo_cleanup(&demo);
2075
Tony Barbourf9921732015-04-22 11:36:22 -06002076 return (int) msg.wParam;
Ian Elliott639ca472015-04-16 15:23:05 -06002077}
2078#else // _WIN32
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002079int main(int argc, char **argv)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002080{
2081 struct demo demo;
2082
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002083 demo_init(&demo, argc, argv);
Chia-I Wucbb564e2015-04-16 22:02:10 +08002084 demo_create_window(&demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002085
2086 demo_prepare(&demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002087 demo_run(&demo);
2088
2089 demo_cleanup(&demo);
2090
2091 return 0;
2092}
Ian Elliott639ca472015-04-16 15:23:05 -06002093#endif // _WIN32