blob: 645de5e2ca3c1081dd63bf5f1ec7a0dbe5a15f45 [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
Ian Elliott44e33f72015-04-28 10:52:52 -060027#define APP_SHORT_NAME "cube"
28#define APP_LONG_NAME "The Vulkan Cube Demo Program"
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060029
Tony Barbourfdc2d352015-04-22 09:02:32 -060030#if defined(NDEBUG) && defined(__GNUC__)
31#define U_ASSERT_ONLY __attribute__((unused))
32#else
33#define U_ASSERT_ONLY
34#endif
35
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -060036/*
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -070037 * structure to track all objects related to a texture.
38 */
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -060039struct texture_object {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -060040 VkSampler sampler;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -070041
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -060042 VkImage image;
43 VkImageLayout imageLayout;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -060044
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -070045 uint32_t num_mem;
Tony Barbour72304ef2015-04-16 15:59:00 -060046 VkDeviceMemory *mem;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -060047 VkImageView view;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -070048 int32_t tex_width, tex_height;
49};
50
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -060051static char *tex_files[] = {
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -060052 "lunarg-logo-256x256-solid.png"
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -060053};
54
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -060055struct vkcube_vs_uniform {
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -060056 // Must start with MVP
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -060057 float mvp[4][4];
58 float position[12*3][4];
59 float color[12*3][4];
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -060060};
61
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -060062struct vktexcube_vs_uniform {
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -060063 // Must start with MVP
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -060064 float mvp[4][4];
65 float position[12*3][4];
66 float attr[12*3][4];
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -060067};
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -060068
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060069//--------------------------------------------------------------------------------------
70// Mesh and VertexFormat Data
71//--------------------------------------------------------------------------------------
72struct Vertex
73{
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -060074 float posX, posY, posZ, posW; // Position data
75 float r, g, b, a; // Color
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060076};
77
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -060078struct VertexPosTex
79{
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -060080 float posX, posY, posZ, posW; // Position data
81 float u, v, s, t; // Texcoord
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -060082};
83
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060084#define XYZ1(_x_, _y_, _z_) (_x_), (_y_), (_z_), 1.f
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -060085#define UV(_u_, _v_) (_u_), (_v_), 0.f, 1.f
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060086
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -060087static const float g_vertex_buffer_data[] = {
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -060088 -1.0f,-1.0f,-1.0f, // Vertex 0
89 -1.0f,-1.0f, 1.0f,
90 -1.0f, 1.0f, 1.0f,
91
92 -1.0f, 1.0f, 1.0f, // Vertex 1
93 -1.0f, 1.0f,-1.0f,
94 -1.0f,-1.0f,-1.0f,
95
96 -1.0f,-1.0f,-1.0f, // Vertex 2
97 1.0f, 1.0f,-1.0f,
98 1.0f,-1.0f,-1.0f,
99
100 -1.0f,-1.0f,-1.0f, // Vertex 3
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600101 -1.0f, 1.0f,-1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600102 1.0f, 1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600103
104 -1.0f,-1.0f,-1.0f, // Vertex 4
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600105 1.0f,-1.0f,-1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600106 1.0f,-1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600107
108 -1.0f,-1.0f,-1.0f, // Vertex 5
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600109 1.0f,-1.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600110 -1.0f,-1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600111
112 -1.0f, 1.0f,-1.0f, // Vertex 6
113 -1.0f, 1.0f, 1.0f,
114 1.0f, 1.0f, 1.0f,
115
116 -1.0f, 1.0f,-1.0f, // Vertex 7
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600117 1.0f, 1.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600118 1.0f, 1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600119
120 1.0f, 1.0f,-1.0f, // Vertex 8
121 1.0f, 1.0f, 1.0f,
122 1.0f,-1.0f, 1.0f,
123
124 1.0f,-1.0f, 1.0f, // Vertex 9
125 1.0f,-1.0f,-1.0f,
126 1.0f, 1.0f,-1.0f,
127
128 -1.0f, 1.0f, 1.0f, // Vertex 10
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600129 -1.0f,-1.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600130 1.0f, 1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600131
132 -1.0f,-1.0f, 1.0f, // Vertex 11
133 1.0f,-1.0f, 1.0f,
134 1.0f, 1.0f, 1.0f,
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600135};
136
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600137static const float g_uv_buffer_data[] = {
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600138 1.0f, 0.0f, // Vertex 0
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600139 0.0f, 0.0f,
140 0.0f, 1.0f,
141
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600142 0.0f, 1.0f, // Vertex 1
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600143 1.0f, 1.0f,
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600144 1.0f, 0.0f,
145
146// 0.0f, 1.0f, // Vertex 2
147// 1.0f, 0.0f,
148// 0.0f, 0.0f,
149
150// 0.0f, 1.0f, // Vertex 3
151// 1.0f, 0.0f,
152// 1.0f, 1.0f,
153
154 0.0f, 0.0f, // Vertex 2
155 1.0f, 1.0f,
156 1.0f, 0.0f,
157
158 0.0f, 0.0f, // Vertex 3
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600159 0.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600160 1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600161
162 0.0f, 1.0f, // Vertex 4
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600163 0.0f, 0.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600164 1.0f, 0.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600165
166 0.0f, 1.0f, // Vertex 5
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600167 1.0f, 0.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600168 1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600169
170 0.0f, 1.0f, // Vertex 6
171 1.0f, 1.0f,
172 1.0f, 0.0f,
173
174 0.0f, 1.0f, // Vertex 7
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600175 1.0f, 0.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600176 0.0f, 0.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600177
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600178 0.0f, 1.0f, // Vertex 8
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600179 1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600180 1.0f, 0.0f,
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600181
182 1.0f, 0.0f, // Vertex 9
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600183 0.0f, 0.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600184 0.0f, 1.0f,
185
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600186 1.0f, 1.0f, // Vertex 10
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600187 1.0f, 0.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600188 0.0f, 1.0f,
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600189
190 1.0f, 0.0f, // Vertex 11
191 0.0f, 0.0f,
192 0.0f, 1.0f,
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600193};
194
195void dumpMatrix(const char *note, mat4x4 MVP)
196{
197 int i;
198
199 printf("%s: \n", note);
200 for (i=0; i<4; i++) {
201 printf("%f, %f, %f, %f\n", MVP[i][0], MVP[i][1], MVP[i][2], MVP[i][3]);
202 }
203 printf("\n");
204 fflush(stdout);
205}
206
207void dumpVec4(const char *note, vec4 vector)
208{
209 printf("%s: \n", note);
210 printf("%f, %f, %f, %f\n", vector[0], vector[1], vector[2], vector[3]);
211 printf("\n");
212 fflush(stdout);
213}
214
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600215struct demo {
Ian Elliott639ca472015-04-16 15:23:05 -0600216#ifdef _WIN32
217#define APP_NAME_STR_LEN 80
218 HINSTANCE connection; // hInstance - Windows Instance
219 char name[APP_NAME_STR_LEN]; // Name to put on the window/icon
220 HWND window; // hWnd - window handle
221#else // _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600222 xcb_connection_t *connection;
223 xcb_screen_t *screen;
Chia-I Wucbb564e2015-04-16 22:02:10 +0800224 xcb_window_t window;
225 xcb_intern_atom_reply_t *atom_wm_delete_window;
Ian Elliott639ca472015-04-16 15:23:05 -0600226#endif // _WIN32
Jon Ashburn8d26c062015-04-24 09:46:24 -0700227 bool prepared;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700228 bool use_staging_buffer;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600229
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600230 VkInstance inst;
Tony Barbour72304ef2015-04-16 15:59:00 -0600231 VkPhysicalDevice gpu;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600232 VkDevice device;
233 VkQueue queue;
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -0700234 uint32_t graphics_queue_node_index;
Tony Barbour72304ef2015-04-16 15:59:00 -0600235 VkPhysicalDeviceProperties *gpu_props;
236 VkPhysicalDeviceQueueProperties *queue_props;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600237
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600238 VkFramebuffer framebuffer;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600239 int width, height;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600240 VkFormat format;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600241
Chia-I Wucbb564e2015-04-16 22:02:10 +0800242 VkSwapChainWSI swap_chain;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600243 struct {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600244 VkImage image;
Tony Barbour72304ef2015-04-16 15:59:00 -0600245 VkDeviceMemory mem;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600246 VkCmdBuffer cmd;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600247
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600248 VkColorAttachmentView view;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600249 } buffers[DEMO_BUFFER_COUNT];
250
251 struct {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600252 VkFormat format;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600253
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600254 VkImage image;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600255 uint32_t num_mem;
Tony Barbour72304ef2015-04-16 15:59:00 -0600256 VkDeviceMemory *mem;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600257 VkDepthStencilView view;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600258 } depth;
259
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600260 struct texture_object textures[DEMO_TEXTURE_COUNT];
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600261
262 struct {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600263 VkBuffer buf;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600264 uint32_t num_mem;
Tony Barbour72304ef2015-04-16 15:59:00 -0600265 VkDeviceMemory *mem;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600266 VkBufferView view;
267 VkBufferViewAttachInfo attach;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600268 } uniform_data;
269
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600270 VkCmdBuffer cmd; // Buffer for initialization commands
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -0500271 VkPipelineLayout pipeline_layout;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600272 VkDescriptorSetLayout desc_layout;
273 VkPipeline pipeline;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600274
Courtney Goeltzenleuchter5e6d1e92015-04-10 16:24:50 -0600275 VkDynamicVpState viewport;
276 VkDynamicRsState raster;
277 VkDynamicCbState color_blend;
278 VkDynamicDsState depth_stencil;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600279
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600280 mat4x4 projection_matrix;
281 mat4x4 view_matrix;
282 mat4x4 model_matrix;
283
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600284 float spin_angle;
285 float spin_increment;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600286 bool pause;
287
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600288 VkDescriptorPool desc_pool;
289 VkDescriptorSet desc_set;
Chia-I Wu6a3c8972015-01-04 16:27:24 +0800290
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600291 bool quit;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600292 uint32_t current_buffer;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600293};
294
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600295static void demo_flush_init_cmd(struct demo *demo)
296{
Tony Barbourfdc2d352015-04-22 09:02:32 -0600297 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600298
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600299 if (demo->cmd == VK_NULL_HANDLE)
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600300 return;
301
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600302 err = vkEndCommandBuffer(demo->cmd);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600303 assert(!err);
304
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600305 const VkCmdBuffer cmd_bufs[] = { demo->cmd };
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600306
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600307 err = vkQueueSubmit(demo->queue, 1, cmd_bufs, VK_NULL_HANDLE);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600308 assert(!err);
309
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600310 err = vkQueueWaitIdle(demo->queue);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600311 assert(!err);
312
Mike Stroyanebae8322015-04-17 12:36:38 -0600313 vkDestroyObject(demo->device, VK_OBJECT_TYPE_COMMAND_BUFFER, demo->cmd);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600314 demo->cmd = VK_NULL_HANDLE;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600315}
316
317static void demo_add_mem_refs(
318 struct demo *demo,
Tony Barbour72304ef2015-04-16 15:59:00 -0600319 int num_refs, VkDeviceMemory *mem)
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600320{
Courtney Goeltzenleuchter6b0caf12015-04-16 13:38:46 -0600321 vkQueueAddMemReferences(demo->queue, num_refs, mem);
Courtney Goeltzenleuchter1359b7a2015-04-02 16:25:42 -0600322}
323
324static void demo_remove_mem_refs(
325 struct demo *demo,
Tony Barbour72304ef2015-04-16 15:59:00 -0600326 int num_refs, VkDeviceMemory *mem)
Courtney Goeltzenleuchter1359b7a2015-04-02 16:25:42 -0600327{
Courtney Goeltzenleuchter6b0caf12015-04-16 13:38:46 -0600328 vkQueueRemoveMemReferences(demo->queue, num_refs, mem);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600329}
330
331static void demo_set_image_layout(
332 struct demo *demo,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600333 VkImage image,
334 VkImageLayout old_image_layout,
335 VkImageLayout new_image_layout)
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600336{
Tony Barbourfdc2d352015-04-22 09:02:32 -0600337 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600338
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600339 if (demo->cmd == VK_NULL_HANDLE) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600340 const VkCmdBufferCreateInfo cmd = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600341 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600342 .pNext = NULL,
343 .queueNodeIndex = demo->graphics_queue_node_index,
344 .flags = 0,
345 };
346
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600347 err = vkCreateCommandBuffer(demo->device, &cmd, &demo->cmd);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600348 assert(!err);
349
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600350 VkCmdBufferBeginInfo cmd_buf_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600351 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600352 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -0600353 .flags = VK_CMD_BUFFER_OPTIMIZE_SMALL_BATCH_BIT |
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600354 VK_CMD_BUFFER_OPTIMIZE_ONE_TIME_SUBMIT_BIT,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600355 };
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600356 err = vkBeginCommandBuffer(demo->cmd, &cmd_buf_info);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600357 }
358
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600359 VkImageMemoryBarrier image_memory_barrier = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600360 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600361 .pNext = NULL,
362 .outputMask = 0,
363 .inputMask = 0,
364 .oldLayout = old_image_layout,
365 .newLayout = new_image_layout,
366 .image = image,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600367 .subresourceRange = { VK_IMAGE_ASPECT_COLOR, 0, 1, 0, 0 }
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600368 };
369
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600370 if (new_image_layout == VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL) {
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600371 /* Make sure anything that was copying from this image has completed */
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -0600372 image_memory_barrier.inputMask = VK_MEMORY_INPUT_TRANSFER_BIT;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600373 }
374
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600375 if (new_image_layout == VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL) {
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600376 /* Make sure any Copy or CPU writes to image are flushed */
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -0600377 image_memory_barrier.outputMask = VK_MEMORY_OUTPUT_CPU_WRITE_BIT | VK_MEMORY_OUTPUT_TRANSFER_BIT;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600378 }
379
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600380 VkImageMemoryBarrier *pmemory_barrier = &image_memory_barrier;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600381
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600382 VkPipeEvent set_events[] = { VK_PIPE_EVENT_TOP_OF_PIPE };
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600383
Tony Barbour72304ef2015-04-16 15:59:00 -0600384 vkCmdPipelineBarrier(demo->cmd, VK_WAIT_EVENT_TOP_OF_PIPE, 1, set_events, 1, (const void **)&pmemory_barrier);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600385}
386
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600387static void demo_draw_build_cmd(struct demo *demo, VkCmdBuffer cmd_buf)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600388{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600389 const VkColorAttachmentBindInfo color_attachment = {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600390 .view = demo->buffers[demo->current_buffer].view,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600391 .layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600392 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600393 const VkDepthStencilBindInfo depth_stencil = {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600394 .view = demo->depth.view,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600395 .layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600396 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600397 const VkClearColor clear_color = {
Courtney Goeltzenleuchter374553c2015-04-03 16:35:32 -0600398 .color.floatColor = { 0.2f, 0.2f, 0.2f, 0.2f },
399 .useRawValue = false,
400 };
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600401 const float clear_depth = 1.0f;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600402 VkImageSubresourceRange clear_range;
403 VkCmdBufferBeginInfo cmd_buf_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600404 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO,
Courtney Goeltzenleuchter53968d82015-04-03 15:25:24 -0600405 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -0600406 .flags = VK_CMD_BUFFER_OPTIMIZE_SMALL_BATCH_BIT |
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600407 VK_CMD_BUFFER_OPTIMIZE_ONE_TIME_SUBMIT_BIT,
Jon Ashburn60ccfbe2014-12-31 17:08:35 -0700408 };
Tony Barbourfdc2d352015-04-22 09:02:32 -0600409 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600410 VkAttachmentLoadOp load_op = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
411 VkAttachmentStoreOp store_op = VK_ATTACHMENT_STORE_OP_DONT_CARE;
412 const VkFramebufferCreateInfo fb_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600413 .sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO,
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700414 .pNext = NULL,
415 .colorAttachmentCount = 1,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600416 .pColorAttachments = (VkColorAttachmentBindInfo*) &color_attachment,
417 .pDepthStencilAttachment = (VkDepthStencilBindInfo*) &depth_stencil,
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700418 .sampleCount = 1,
Mark Lobodzinskic06b7412015-01-27 13:24:03 -0600419 .width = demo->width,
420 .height = demo->height,
421 .layers = 1,
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700422 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600423 VkRenderPassCreateInfo rp_info;
424 VkRenderPassBegin rp_begin;
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700425
426 memset(&rp_info, 0 , sizeof(rp_info));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600427 err = vkCreateFramebuffer(demo->device, &fb_info, &rp_begin.framebuffer);
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700428 assert(!err);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600429 rp_info.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO;
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700430 rp_info.renderArea.extent.width = demo->width;
431 rp_info.renderArea.extent.height = demo->height;
Courtney Goeltzenleuchter53968d82015-04-03 15:25:24 -0600432 rp_info.colorAttachmentCount = fb_info.colorAttachmentCount;
433 rp_info.pColorFormats = &demo->format;
434 rp_info.pColorLayouts = &color_attachment.layout;
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700435 rp_info.pColorLoadOps = &load_op;
436 rp_info.pColorStoreOps = &store_op;
Courtney Goeltzenleuchter53968d82015-04-03 15:25:24 -0600437 rp_info.pColorLoadClearValues = &clear_color;
Tony Barbour72304ef2015-04-16 15:59:00 -0600438 rp_info.depthStencilFormat = VK_FORMAT_D16_UNORM;
Courtney Goeltzenleuchter53968d82015-04-03 15:25:24 -0600439 rp_info.depthStencilLayout = depth_stencil.layout;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600440 rp_info.depthLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
Courtney Goeltzenleuchter53968d82015-04-03 15:25:24 -0600441 rp_info.depthLoadClearValue = clear_depth;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600442 rp_info.depthStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
443 rp_info.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
Courtney Goeltzenleuchter53968d82015-04-03 15:25:24 -0600444 rp_info.stencilLoadClearValue = 0;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600445 rp_info.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
446 err = vkCreateRenderPass(demo->device, &rp_info, &rp_begin.renderPass);
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700447 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600448
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600449 err = vkBeginCommandBuffer(cmd_buf, &cmd_buf_info);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600450 assert(!err);
451
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600452 vkCmdBindPipeline(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600453 demo->pipeline);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600454 vkCmdBindDescriptorSets(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS,
Cody Northropfb5185a2015-04-16 13:41:56 -0600455 0, 1, &demo->desc_set, 0, NULL);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600456
Tony Barbour72304ef2015-04-16 15:59:00 -0600457 vkCmdBindDynamicStateObject(cmd_buf, VK_STATE_BIND_POINT_VIEWPORT, demo->viewport);
458 vkCmdBindDynamicStateObject(cmd_buf, VK_STATE_BIND_POINT_RASTER, demo->raster);
459 vkCmdBindDynamicStateObject(cmd_buf, VK_STATE_BIND_POINT_COLOR_BLEND,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600460 demo->color_blend);
Tony Barbour72304ef2015-04-16 15:59:00 -0600461 vkCmdBindDynamicStateObject(cmd_buf, VK_STATE_BIND_POINT_DEPTH_STENCIL,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600462 demo->depth_stencil);
463
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600464 vkCmdBeginRenderPass(cmd_buf, &rp_begin);
465 clear_range.aspect = VK_IMAGE_ASPECT_COLOR;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600466 clear_range.baseMipLevel = 0;
467 clear_range.mipLevels = 1;
468 clear_range.baseArraySlice = 0;
469 clear_range.arraySize = 1;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600470 vkCmdClearColorImage(cmd_buf,
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600471 demo->buffers[demo->current_buffer].image,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600472 VK_IMAGE_LAYOUT_CLEAR_OPTIMAL,
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600473 clear_color, 1, &clear_range);
474
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600475 clear_range.aspect = VK_IMAGE_ASPECT_DEPTH;
476 vkCmdClearDepthStencil(cmd_buf, demo->depth.image,
477 VK_IMAGE_LAYOUT_CLEAR_OPTIMAL,
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600478 clear_depth, 0, 1, &clear_range);
479
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600480 vkCmdDraw(cmd_buf, 0, 12 * 3, 0, 1);
481 vkCmdEndRenderPass(cmd_buf, rp_begin.renderPass);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600482
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600483 err = vkEndCommandBuffer(cmd_buf);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600484 assert(!err);
Courtney Goeltzenleuchter94901dc2015-02-25 17:53:18 -0700485
Mike Stroyanebae8322015-04-17 12:36:38 -0600486 vkDestroyObject(demo->device, VK_OBJECT_TYPE_RENDER_PASS, rp_begin.renderPass);
487 vkDestroyObject(demo->device, VK_OBJECT_TYPE_FRAMEBUFFER, rp_begin.framebuffer);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600488}
489
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600490
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600491void demo_update_data_buffer(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600492{
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600493 mat4x4 MVP, Model, VP;
494 int matrixSize = sizeof(MVP);
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600495 uint8_t *pData;
Tony Barbourfdc2d352015-04-22 09:02:32 -0600496 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600497
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600498 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600499
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600500 // Rotate 22.5 degrees around the Y axis
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600501 mat4x4_dup(Model, demo->model_matrix);
Piers Daniell735ee532015-02-23 16:23:13 -0700502 mat4x4_rotate(demo->model_matrix, Model, 0.0f, 1.0f, 0.0f, (float)degreesToRadians(demo->spin_angle));
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600503 mat4x4_mul(MVP, VP, demo->model_matrix);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600504
Jon Ashburnae7c21c2015-01-19 15:00:26 -0700505 assert(demo->uniform_data.num_mem == 1);
Mike Stroyanebae8322015-04-17 12:36:38 -0600506 err = vkMapMemory(demo->device, demo->uniform_data.mem[0], 0, 0, 0, (void **) &pData);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600507 assert(!err);
508
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600509 memcpy(pData, (const void*) &MVP[0][0], matrixSize);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600510
Mike Stroyanebae8322015-04-17 12:36:38 -0600511 err = vkUnmapMemory(demo->device, demo->uniform_data.mem[0]);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600512 assert(!err);
513}
514
515static void demo_draw(struct demo *demo)
516{
Chia-I Wucbb564e2015-04-16 22:02:10 +0800517 const VkPresentInfoWSI present = {
518 .sType = VK_STRUCTURE_TYPE_PRESENT_INFO_WSI,
519 .pNext = NULL,
520 .image = demo->buffers[demo->current_buffer].image,
521 .flipInterval = 0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600522 };
Tony Barbourfdc2d352015-04-22 09:02:32 -0600523 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600524
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600525 err = vkQueueSubmit(demo->queue, 1, &demo->buffers[demo->current_buffer].cmd,
526 VK_NULL_HANDLE);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600527 assert(!err);
528
Chia-I Wucbb564e2015-04-16 22:02:10 +0800529 err = vkQueuePresentWSI(demo->queue, &present);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600530 assert(!err);
531
532 demo->current_buffer = (demo->current_buffer + 1) % DEMO_BUFFER_COUNT;
Chia-I Wucbb564e2015-04-16 22:02:10 +0800533
534 err = vkQueueWaitIdle(demo->queue);
535 assert(err == VK_SUCCESS);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600536}
537
538static void demo_prepare_buffers(struct demo *demo)
539{
Chia-I Wucbb564e2015-04-16 22:02:10 +0800540 const VkSwapChainCreateInfoWSI swap_chain = {
541 .sType = VK_STRUCTURE_TYPE_SWAP_CHAIN_CREATE_INFO_WSI,
542 .pNext = NULL,
543 .pNativeWindowSystemHandle = demo->connection,
544 .pNativeWindowHandle = (void *) (intptr_t) demo->window,
545 .imageCount = DEMO_BUFFER_COUNT,
546 .imageFormat = demo->format,
547 .imageExtent = {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600548 .width = demo->width,
549 .height = demo->height,
550 },
Chia-I Wucbb564e2015-04-16 22:02:10 +0800551 .imageArraySize = 1,
552 .imageUsageFlags = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600553 };
Chia-I Wucbb564e2015-04-16 22:02:10 +0800554 VkSwapChainImageInfoWSI images[DEMO_BUFFER_COUNT];
555 size_t images_size = sizeof(images);
Tony Barbourfdc2d352015-04-22 09:02:32 -0600556 VkResult U_ASSERT_ONLY err;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600557 uint32_t i;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600558
Chia-I Wucbb564e2015-04-16 22:02:10 +0800559 err = vkCreateSwapChainWSI(demo->device, &swap_chain, &demo->swap_chain);
560 assert(!err);
561
562 err = vkGetSwapChainInfoWSI(demo->swap_chain,
563 VK_SWAP_CHAIN_INFO_TYPE_PERSISTENT_IMAGES_WSI,
564 &images_size, images);
565 assert(!err && images_size == sizeof(images));
566
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600567 for (i = 0; i < DEMO_BUFFER_COUNT; i++) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600568 VkColorAttachmentViewCreateInfo color_attachment_view = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600569 .sType = VK_STRUCTURE_TYPE_COLOR_ATTACHMENT_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600570 .pNext = NULL,
571 .format = demo->format,
572 .mipLevel = 0,
573 .baseArraySlice = 0,
574 .arraySize = 1,
575 };
576
Chia-I Wucbb564e2015-04-16 22:02:10 +0800577 demo->buffers[i].image = images[i].image;
578 demo->buffers[i].mem = images[i].memory;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600579
Courtney Goeltzenleuchter2c2fbbf2015-04-07 17:13:38 -0600580 demo_add_mem_refs(demo, 1, &demo->buffers[i].mem);
Courtney Goeltzenleuchter1359b7a2015-04-02 16:25:42 -0600581
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600582 demo_set_image_layout(demo, demo->buffers[i].image,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600583 VK_IMAGE_LAYOUT_UNDEFINED,
584 VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600585
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600586 color_attachment_view.image = demo->buffers[i].image;
587
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600588 err = vkCreateColorAttachmentView(demo->device,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600589 &color_attachment_view, &demo->buffers[i].view);
590 assert(!err);
591 }
592}
593
594static void demo_prepare_depth(struct demo *demo)
595{
Tony Barbour72304ef2015-04-16 15:59:00 -0600596 const VkFormat depth_format = VK_FORMAT_D16_UNORM;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600597 const VkImageCreateInfo image = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600598 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600599 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -0600600 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600601 .format = depth_format,
602 .extent = { demo->width, demo->height, 1 },
603 .mipLevels = 1,
604 .arraySize = 1,
605 .samples = 1,
Tony Barbour72304ef2015-04-16 15:59:00 -0600606 .tiling = VK_IMAGE_TILING_OPTIMAL,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600607 .usage = VK_IMAGE_USAGE_DEPTH_STENCIL_BIT,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600608 .flags = 0,
609 };
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -0600610 VkMemoryAllocInfo mem_alloc = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600611 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
Mark Lobodzinskia6907f72015-04-16 08:52:00 -0500612 .pNext = NULL,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600613 .allocationSize = 0,
Tony Barbour72304ef2015-04-16 15:59:00 -0600614 .memProps = VK_MEMORY_PROPERTY_DEVICE_ONLY,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600615 .memPriority = VK_MEMORY_PRIORITY_NORMAL,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600616 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600617 VkDepthStencilViewCreateInfo view = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600618 .sType = VK_STRUCTURE_TYPE_DEPTH_STENCIL_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600619 .pNext = NULL,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600620 .image = VK_NULL_HANDLE,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600621 .mipLevel = 0,
622 .baseArraySlice = 0,
623 .arraySize = 1,
624 .flags = 0,
625 };
Mike Stroyanebae8322015-04-17 12:36:38 -0600626
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600627 VkMemoryRequirements *mem_reqs;
628 size_t mem_reqs_size = sizeof(VkMemoryRequirements);
Tony Barbourfdc2d352015-04-22 09:02:32 -0600629 VkResult U_ASSERT_ONLY err;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600630 uint32_t num_allocations = 0;
631 size_t num_alloc_size = sizeof(num_allocations);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600632
633 demo->depth.format = depth_format;
634
635 /* create image */
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600636 err = vkCreateImage(demo->device, &image,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600637 &demo->depth.image);
638 assert(!err);
639
Mike Stroyanebae8322015-04-17 12:36:38 -0600640 err = vkGetObjectInfo(demo->device,
641 VK_OBJECT_TYPE_IMAGE, demo->depth.image,
642 VK_OBJECT_INFO_TYPE_MEMORY_ALLOCATION_COUNT,
643 &num_alloc_size, &num_allocations);
Jon Ashburnb2a66652015-01-16 09:37:43 -0700644 assert(!err && num_alloc_size == sizeof(num_allocations));
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600645 mem_reqs = malloc(num_allocations * sizeof(VkMemoryRequirements));
Tony Barbour72304ef2015-04-16 15:59:00 -0600646 demo->depth.mem = malloc(num_allocations * sizeof(VkDeviceMemory));
Jon Ashburnb2a66652015-01-16 09:37:43 -0700647 demo->depth.num_mem = num_allocations;
Mike Stroyanebae8322015-04-17 12:36:38 -0600648 err = vkGetObjectInfo(demo->device,
649 VK_OBJECT_TYPE_IMAGE, demo->depth.image,
Tony Barbour72304ef2015-04-16 15:59:00 -0600650 VK_OBJECT_INFO_TYPE_MEMORY_REQUIREMENTS,
Jon Ashburnb2a66652015-01-16 09:37:43 -0700651 &mem_reqs_size, mem_reqs);
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600652 assert(!err && mem_reqs_size == num_allocations * sizeof(VkMemoryRequirements));
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600653 for (uint32_t i = 0; i < num_allocations; i ++) {
Jon Ashburnb2a66652015-01-16 09:37:43 -0700654 mem_alloc.allocationSize = mem_reqs[i].size;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600655
Jon Ashburnb2a66652015-01-16 09:37:43 -0700656 /* allocate memory */
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600657 err = vkAllocMemory(demo->device, &mem_alloc,
Jon Ashburnb2a66652015-01-16 09:37:43 -0700658 &(demo->depth.mem[i]));
659 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600660
Jon Ashburnb2a66652015-01-16 09:37:43 -0700661 /* bind memory */
Mike Stroyanebae8322015-04-17 12:36:38 -0600662 err = vkQueueBindObjectMemory(demo->queue,
663 VK_OBJECT_TYPE_IMAGE, demo->depth.image,
664 i, demo->depth.mem[i], 0);
Jon Ashburnb2a66652015-01-16 09:37:43 -0700665 assert(!err);
666 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600667
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600668 demo_set_image_layout(demo, demo->depth.image,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600669 VK_IMAGE_LAYOUT_UNDEFINED,
670 VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600671
Courtney Goeltzenleuchter2c2fbbf2015-04-07 17:13:38 -0600672 demo_add_mem_refs(demo, demo->depth.num_mem, demo->depth.mem);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600673
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600674 /* create image view */
675 view.image = demo->depth.image;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600676 err = vkCreateDepthStencilView(demo->device, &view,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600677 &demo->depth.view);
678 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600679}
680
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600681/** loadTexture
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -0600682 * loads a png file into an memory object, using cstdio , libpng.
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600683 *
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -0600684 * \param demo : Needed to access VK calls
685 * \param filename : the png file to be loaded
686 * \param width : width of png, to be updated as a side effect of this function
687 * \param height : height of png, to be updated as a side effect of this function
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600688 *
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -0600689 * \return bool : an opengl texture id. true if successful?,
690 * should be validated by the client of this function.
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600691 *
692 * Source: http://en.wikibooks.org/wiki/OpenGL_Programming/Intermediate/Textures
693 * Modified to copy image to memory
694 *
695 */
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700696bool loadTexture(const char *filename, uint8_t *rgba_data,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600697 VkSubresourceLayout *layout,
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600698 int32_t *width, int32_t *height)
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600699{
700 //header for testing if it is a png
701 png_byte header[8];
Tony Barbourf9921732015-04-22 11:36:22 -0600702 int is_png, bit_depth, color_type, rowbytes;
703 size_t retval;
Ian Elliott1e42dff2015-02-13 14:29:21 -0700704 png_uint_32 i, twidth, theight;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600705 png_structp png_ptr;
706 png_infop info_ptr, end_info;
707 png_byte *image_data;
708 png_bytep *row_pointers;
709
710 //open file as binary
711 FILE *fp = fopen(filename, "rb");
712 if (!fp) {
713 return false;
714 }
715
716 //read the header
Tony Barbourfdc2d352015-04-22 09:02:32 -0600717 retval = fread(header, 1, 8, fp);
718 if (retval != 8) {
719 fclose(fp);
720 return false;
721 }
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600722
723 //test if png
724 is_png = !png_sig_cmp(header, 0, 8);
725 if (!is_png) {
726 fclose(fp);
727 return false;
728 }
729
730 //create png struct
731 png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL,
732 NULL, NULL);
733 if (!png_ptr) {
734 fclose(fp);
735 return (false);
736 }
737
738 //create png info struct
739 info_ptr = png_create_info_struct(png_ptr);
740 if (!info_ptr) {
741 png_destroy_read_struct(&png_ptr, (png_infopp) NULL, (png_infopp) NULL);
742 fclose(fp);
743 return (false);
744 }
745
746 //create png info struct
747 end_info = png_create_info_struct(png_ptr);
748 if (!end_info) {
749 png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp) NULL);
750 fclose(fp);
751 return (false);
752 }
753
754 //png error stuff, not sure libpng man suggests this.
755 if (setjmp(png_jmpbuf(png_ptr))) {
756 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
757 fclose(fp);
758 return (false);
759 }
760
761 //init png reading
762 png_init_io(png_ptr, fp);
763
764 //let libpng know you already read the first 8 bytes
765 png_set_sig_bytes(png_ptr, 8);
766
767 // read all the info up to the image data
768 png_read_info(png_ptr, info_ptr);
769
770 // get info about png
771 png_get_IHDR(png_ptr, info_ptr, &twidth, &theight, &bit_depth, &color_type,
772 NULL, NULL, NULL);
773
774 //update width and height based on png info
775 *width = twidth;
776 *height = theight;
777
778 // Require that incoming texture be 8bits per color component
779 // and 4 components (RGBA).
780 if (png_get_bit_depth(png_ptr, info_ptr) != 8 ||
781 png_get_channels(png_ptr, info_ptr) != 4) {
782 return false;
783 }
784
785 if (rgba_data == NULL) {
786 // If data pointer is null, we just want the width & height
787 // clean up memory and close stuff
788 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
789 fclose(fp);
790
791 return true;
792 }
793
794 // Update the png info struct.
795 png_read_update_info(png_ptr, info_ptr);
796
797 // Row size in bytes.
798 rowbytes = png_get_rowbytes(png_ptr, info_ptr);
799
800 // Allocate the image_data as a big block, to be given to opengl
801 image_data = (png_byte *)malloc(rowbytes * theight * sizeof(png_byte));
802 if (!image_data) {
803 //clean up memory and close stuff
804 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
805 fclose(fp);
806 return false;
807 }
808
809 // row_pointers is for pointing to image_data for reading the png with libpng
810 row_pointers = (png_bytep *)malloc(theight * sizeof(png_bytep));
811 if (!row_pointers) {
812 //clean up memory and close stuff
813 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
814 // delete[] image_data;
815 fclose(fp);
816 return false;
817 }
818 // set the individual row_pointers to point at the correct offsets of image_data
819 for (i = 0; i < theight; ++i)
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700820 row_pointers[theight - 1 - i] = rgba_data + i * layout->rowPitch;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600821
822 // read the png into image_data through row_pointers
823 png_read_image(png_ptr, row_pointers);
824
825 // clean up memory and close stuff
826 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
827 free(row_pointers);
828 free(image_data);
829 fclose(fp);
830
831 return true;
832}
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600833
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700834static void demo_prepare_texture_image(struct demo *demo,
835 const char *filename,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600836 struct texture_object *tex_obj,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600837 VkImageTiling tiling,
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -0600838 VkImageUsageFlags usage,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600839 VkFlags mem_props)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600840{
Tony Barbour72304ef2015-04-16 15:59:00 -0600841 const VkFormat tex_format = VK_FORMAT_B8G8R8A8_UNORM;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600842 int32_t tex_width;
843 int32_t tex_height;
Tony Barbourfdc2d352015-04-22 09:02:32 -0600844 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700845
David Pinedo30bd71d2015-04-23 08:16:57 -0600846 if (!loadTexture(filename, NULL, NULL, &tex_width, &tex_height))
847 {
848 printf("Failed to load textures\n");
849 fflush(stdout);
850 exit(1);
851 }
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700852
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600853 tex_obj->tex_width = tex_width;
854 tex_obj->tex_height = tex_height;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700855
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600856 const VkImageCreateInfo image_create_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600857 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700858 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -0600859 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700860 .format = tex_format,
861 .extent = { tex_width, tex_height, 1 },
862 .mipLevels = 1,
863 .arraySize = 1,
864 .samples = 1,
865 .tiling = tiling,
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -0600866 .usage = usage,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700867 .flags = 0,
868 };
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -0600869 VkMemoryAllocInfo mem_alloc = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600870 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
Mark Lobodzinskia6907f72015-04-16 08:52:00 -0500871 .pNext = NULL,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700872 .allocationSize = 0,
873 .memProps = mem_props,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600874 .memPriority = VK_MEMORY_PRIORITY_NORMAL,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700875 };
876
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600877 VkMemoryRequirements *mem_reqs;
878 size_t mem_reqs_size = sizeof(VkMemoryRequirements);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700879 uint32_t num_allocations = 0;
880 size_t num_alloc_size = sizeof(num_allocations);
881
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600882 err = vkCreateImage(demo->device, &image_create_info,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600883 &tex_obj->image);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700884 assert(!err);
885
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_ALLOCATION_COUNT,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700889 &num_alloc_size, &num_allocations);
890 assert(!err && num_alloc_size == sizeof(num_allocations));
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600891 mem_reqs = malloc(num_allocations * sizeof(VkMemoryRequirements));
Tony Barbour72304ef2015-04-16 15:59:00 -0600892 tex_obj->mem = malloc(num_allocations * sizeof(VkDeviceMemory));
Mike Stroyanebae8322015-04-17 12:36:38 -0600893 err = vkGetObjectInfo(demo->device,
894 VK_OBJECT_TYPE_IMAGE, tex_obj->image,
Tony Barbour72304ef2015-04-16 15:59:00 -0600895 VK_OBJECT_INFO_TYPE_MEMORY_REQUIREMENTS,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700896 &mem_reqs_size, mem_reqs);
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600897 assert(!err && mem_reqs_size == num_allocations * sizeof(VkMemoryRequirements));
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700898 for (uint32_t j = 0; j < num_allocations; j ++) {
Piers Daniell735ee532015-02-23 16:23:13 -0700899 mem_alloc.allocationSize = mem_reqs[j].size;
900
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700901 /* allocate memory */
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600902 err = vkAllocMemory(demo->device, &mem_alloc,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600903 &(tex_obj->mem[j]));
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700904 assert(!err);
905
906 /* bind memory */
Mike Stroyanebae8322015-04-17 12:36:38 -0600907 err = vkQueueBindObjectMemory(demo->queue,
908 VK_OBJECT_TYPE_IMAGE, tex_obj->image,
909 j, tex_obj->mem[j], 0);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700910 assert(!err);
911 }
912 free(mem_reqs);
913 mem_reqs = NULL;
914
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600915 tex_obj->num_mem = num_allocations;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700916
Tony Barbour72304ef2015-04-16 15:59:00 -0600917 if (mem_props & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600918 const VkImageSubresource subres = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600919 .aspect = VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700920 .mipLevel = 0,
921 .arraySlice = 0,
922 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600923 VkSubresourceLayout layout;
924 size_t layout_size = sizeof(VkSubresourceLayout);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700925 void *data;
926
Mike Stroyanebae8322015-04-17 12:36:38 -0600927 err = vkGetImageSubresourceInfo(demo->device, tex_obj->image, &subres,
Tony Barbour72304ef2015-04-16 15:59:00 -0600928 VK_SUBRESOURCE_INFO_TYPE_LAYOUT,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700929 &layout_size, &layout);
930 assert(!err && layout_size == sizeof(layout));
931 /* Linear texture must be within a single memory object */
932 assert(num_allocations == 1);
933
Mike Stroyanebae8322015-04-17 12:36:38 -0600934 err = vkMapMemory(demo->device, tex_obj->mem[0], 0, 0, 0, &data);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700935 assert(!err);
936
937 if (!loadTexture(filename, data, &layout, &tex_width, &tex_height)) {
938 fprintf(stderr, "Error loading texture: %s\n", filename);
939 }
940
Mike Stroyanebae8322015-04-17 12:36:38 -0600941 err = vkUnmapMemory(demo->device, tex_obj->mem[0]);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700942 assert(!err);
943 }
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600944
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600945 tex_obj->imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600946 demo_set_image_layout(demo, tex_obj->image,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600947 VK_IMAGE_LAYOUT_UNDEFINED,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600948 tex_obj->imageLayout);
949 /* 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 -0700950}
951
Mark Lobodzinskida9b1092015-04-16 11:44:05 -0500952static void demo_destroy_texture_image(struct demo *demo, struct texture_object *tex_objs)
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700953{
954 /* clean up staging resources */
955 for (uint32_t j = 0; j < tex_objs->num_mem; j ++) {
Mike Stroyanebae8322015-04-17 12:36:38 -0600956 vkQueueBindObjectMemory(demo->queue,
957 VK_OBJECT_TYPE_IMAGE, tex_objs->image, j, VK_NULL_HANDLE, 0);
958 vkFreeMemory(demo->device, tex_objs->mem[j]);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700959 }
960
961 free(tex_objs->mem);
Mike Stroyanebae8322015-04-17 12:36:38 -0600962 vkDestroyObject(demo->device, VK_OBJECT_TYPE_IMAGE, tex_objs->image);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700963}
964
965static void demo_prepare_textures(struct demo *demo)
966{
Tony Barbour72304ef2015-04-16 15:59:00 -0600967 const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600968 VkFormatProperties props;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700969 size_t size = sizeof(props);
Tony Barbourfdc2d352015-04-22 09:02:32 -0600970 VkResult U_ASSERT_ONLY err;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600971 uint32_t i;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600972
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600973 err = vkGetFormatInfo(demo->device, tex_format,
Tony Barbour72304ef2015-04-16 15:59:00 -0600974 VK_FORMAT_INFO_TYPE_PROPERTIES,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700975 &size, &props);
976 assert(!err);
977
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600978 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700979
Tony Barbour72304ef2015-04-16 15:59:00 -0600980 if (props.linearTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT && !demo->use_staging_buffer) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700981 /* Device can texture using linear textures */
982 demo_prepare_texture_image(demo, tex_files[i], &demo->textures[i],
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -0600983 VK_IMAGE_TILING_LINEAR, VK_IMAGE_USAGE_SAMPLED_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
Tony Barbour72304ef2015-04-16 15:59:00 -0600984 } else if (props.optimalTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700985 /* Must use staging buffer to copy linear texture to optimized */
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600986 struct texture_object staging_texture;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700987
988 memset(&staging_texture, 0, sizeof(staging_texture));
989 demo_prepare_texture_image(demo, tex_files[i], &staging_texture,
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -0600990 VK_IMAGE_TILING_LINEAR, VK_IMAGE_USAGE_TRANSFER_SOURCE_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700991
992 demo_prepare_texture_image(demo, tex_files[i], &demo->textures[i],
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -0600993 VK_IMAGE_TILING_OPTIMAL,
994 (VK_IMAGE_USAGE_TRANSFER_DESTINATION_BIT | VK_IMAGE_USAGE_SAMPLED_BIT),
995 VK_MEMORY_PROPERTY_DEVICE_ONLY);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700996
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600997 demo_set_image_layout(demo, staging_texture.image,
998 staging_texture.imageLayout,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600999 VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001000
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001001 demo_set_image_layout(demo, demo->textures[i].image,
1002 demo->textures[i].imageLayout,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001003 VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001004
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001005 VkImageCopy copy_region = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001006 .srcSubresource = { VK_IMAGE_ASPECT_COLOR, 0, 0 },
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001007 .srcOffset = { 0, 0, 0 },
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001008 .destSubresource = { VK_IMAGE_ASPECT_COLOR, 0, 0 },
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001009 .destOffset = { 0, 0, 0 },
1010 .extent = { staging_texture.tex_width, staging_texture.tex_height, 1 },
1011 };
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001012 vkCmdCopyImage(demo->cmd,
1013 staging_texture.image, VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL,
1014 demo->textures[i].image, VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL,
Courtney Goeltzenleuchter8e89a312015-03-25 11:25:10 -06001015 1, &copy_region);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001016
Courtney Goeltzenleuchter2c2fbbf2015-04-07 17:13:38 -06001017 demo_add_mem_refs(demo, staging_texture.num_mem, staging_texture.mem);
1018 demo_add_mem_refs(demo, demo->textures[i].num_mem, demo->textures[i].mem);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001019
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001020 demo_set_image_layout(demo, demo->textures[i].image,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001021 VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001022 demo->textures[i].imageLayout);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001023
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001024 demo_flush_init_cmd(demo);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001025
Courtney Goeltzenleuchter1359b7a2015-04-02 16:25:42 -06001026 demo_remove_mem_refs(demo, staging_texture.num_mem, staging_texture.mem);
Courtney Goeltzenleuchterf3aeb2b2015-04-21 09:30:03 -06001027 demo_destroy_texture_image(demo, &staging_texture);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001028 } else {
Tony Barbour72304ef2015-04-16 15:59:00 -06001029 /* Can't support VK_FORMAT_B8G8R8A8_UNORM !? */
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001030 assert(!"No support for tB8G8R8A8_UNORM as texture image format");
1031 }
1032
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001033 const VkSamplerCreateInfo sampler = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001034 .sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001035 .pNext = NULL,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001036 .magFilter = VK_TEX_FILTER_NEAREST,
1037 .minFilter = VK_TEX_FILTER_NEAREST,
Tony Barbour72304ef2015-04-16 15:59:00 -06001038 .mipMode = VK_TEX_MIPMAP_MODE_BASE,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001039 .addressU = VK_TEX_ADDRESS_CLAMP,
1040 .addressV = VK_TEX_ADDRESS_CLAMP,
1041 .addressW = VK_TEX_ADDRESS_CLAMP,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001042 .mipLodBias = 0.0f,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001043 .maxAnisotropy = 1,
Tony Barbour72304ef2015-04-16 15:59:00 -06001044 .compareOp = VK_COMPARE_OP_NEVER,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001045 .minLod = 0.0f,
1046 .maxLod = 0.0f,
Tony Barbour72304ef2015-04-16 15:59:00 -06001047 .borderColor = VK_BORDER_COLOR_OPAQUE_WHITE,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001048 };
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001049
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001050 VkImageViewCreateInfo view = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001051 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001052 .pNext = NULL,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001053 .image = VK_NULL_HANDLE,
Tony Barbour72304ef2015-04-16 15:59:00 -06001054 .viewType = VK_IMAGE_VIEW_TYPE_2D,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001055 .format = tex_format,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001056 .channels = { VK_CHANNEL_SWIZZLE_R,
1057 VK_CHANNEL_SWIZZLE_G,
1058 VK_CHANNEL_SWIZZLE_B,
1059 VK_CHANNEL_SWIZZLE_A, },
1060 .subresourceRange = { VK_IMAGE_ASPECT_COLOR, 0, 1, 0, 1 },
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001061 .minLod = 0.0f,
1062 };
Jon Ashburnb2a66652015-01-16 09:37:43 -07001063
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001064 /* create sampler */
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001065 err = vkCreateSampler(demo->device, &sampler,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001066 &demo->textures[i].sampler);
1067 assert(!err);
1068
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001069 /* create image view */
1070 view.image = demo->textures[i].image;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001071 err = vkCreateImageView(demo->device, &view,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001072 &demo->textures[i].view);
1073 assert(!err);
1074 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001075}
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001076
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001077void demo_prepare_cube_data_buffer(struct demo *demo)
1078{
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -06001079 VkBufferCreateInfo buf_info;
1080 VkBufferViewCreateInfo view_info;
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -06001081 VkMemoryAllocInfo alloc_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001082 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
Mark Lobodzinskia6907f72015-04-16 08:52:00 -05001083 .pNext = NULL,
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001084 .allocationSize = 0,
Tony Barbour72304ef2015-04-16 15:59:00 -06001085 .memProps = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001086 .memPriority = VK_MEMORY_PRIORITY_NORMAL,
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001087 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001088 VkMemoryRequirements *mem_reqs;
1089 size_t mem_reqs_size = sizeof(VkMemoryRequirements);
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001090 uint32_t num_allocations = 0;
1091 size_t num_alloc_size = sizeof(num_allocations);
1092 uint8_t *pData;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001093 int i;
1094 mat4x4 MVP, VP;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001095 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001096 struct vktexcube_vs_uniform data;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001097
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001098 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001099 mat4x4_mul(MVP, VP, demo->model_matrix);
1100 memcpy(data.mvp, MVP, sizeof(MVP));
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001101// dumpMatrix("MVP", MVP);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001102
1103 for (i=0; i<12*3; i++) {
1104 data.position[i][0] = g_vertex_buffer_data[i*3];
1105 data.position[i][1] = g_vertex_buffer_data[i*3+1];
1106 data.position[i][2] = g_vertex_buffer_data[i*3+2];
1107 data.position[i][3] = 1.0f;
1108 data.attr[i][0] = g_uv_buffer_data[2*i];
1109 data.attr[i][1] = g_uv_buffer_data[2*i + 1];
1110 data.attr[i][2] = 0;
1111 data.attr[i][3] = 0;
1112 }
1113
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001114 memset(&buf_info, 0, sizeof(buf_info));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001115 buf_info.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001116 buf_info.size = sizeof(data);
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -06001117 buf_info.usage = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001118 err = vkCreateBuffer(demo->device, &buf_info, &demo->uniform_data.buf);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001119 assert(!err);
1120
Mike Stroyanebae8322015-04-17 12:36:38 -06001121 err = vkGetObjectInfo(demo->device,
1122 VK_OBJECT_TYPE_BUFFER, demo->uniform_data.buf,
Tony Barbour72304ef2015-04-16 15:59:00 -06001123 VK_OBJECT_INFO_TYPE_MEMORY_ALLOCATION_COUNT,
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001124 &num_alloc_size, &num_allocations);
1125 assert(!err && num_alloc_size == sizeof(num_allocations));
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001126 mem_reqs = malloc(num_allocations * sizeof(VkMemoryRequirements));
Tony Barbour72304ef2015-04-16 15:59:00 -06001127 demo->uniform_data.mem = malloc(num_allocations * sizeof(VkDeviceMemory));
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001128 demo->uniform_data.num_mem = num_allocations;
Mike Stroyanebae8322015-04-17 12:36:38 -06001129 err = vkGetObjectInfo(demo->device,
1130 VK_OBJECT_TYPE_BUFFER, demo->uniform_data.buf,
Tony Barbour72304ef2015-04-16 15:59:00 -06001131 VK_OBJECT_INFO_TYPE_MEMORY_REQUIREMENTS,
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001132 &mem_reqs_size, mem_reqs);
1133 assert(!err && mem_reqs_size == num_allocations * sizeof(*mem_reqs));
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001134 for (uint32_t i = 0; i < num_allocations; i ++) {
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001135 alloc_info.allocationSize = mem_reqs[i].size;
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001136
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001137 err = vkAllocMemory(demo->device, &alloc_info, &(demo->uniform_data.mem[i]));
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001138 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001139
Mike Stroyanebae8322015-04-17 12:36:38 -06001140 err = vkMapMemory(demo->device, demo->uniform_data.mem[i], 0, 0, 0, (void **) &pData);
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001141 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001142
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001143 memcpy(pData, &data, sizeof data);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001144
Mike Stroyanebae8322015-04-17 12:36:38 -06001145 err = vkUnmapMemory(demo->device, demo->uniform_data.mem[i]);
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001146 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001147
Mike Stroyanebae8322015-04-17 12:36:38 -06001148 err = vkQueueBindObjectMemory(demo->queue,
1149 VK_OBJECT_TYPE_BUFFER, demo->uniform_data.buf,
1150 i, demo->uniform_data.mem[i], 0);
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001151 assert(!err);
1152 }
Courtney Goeltzenleuchter2c2fbbf2015-04-07 17:13:38 -06001153 demo_add_mem_refs(demo, demo->uniform_data.num_mem, demo->uniform_data.mem);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001154
1155 memset(&view_info, 0, sizeof(view_info));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001156 view_info.sType = VK_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO;
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001157 view_info.buffer = demo->uniform_data.buf;
Tony Barbour72304ef2015-04-16 15:59:00 -06001158 view_info.viewType = VK_BUFFER_VIEW_TYPE_RAW;
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001159 view_info.offset = 0;
1160 view_info.range = sizeof(data);
1161
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001162 err = vkCreateBufferView(demo->device, &view_info, &demo->uniform_data.view);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001163 assert(!err);
1164
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001165 demo->uniform_data.attach.sType = VK_STRUCTURE_TYPE_BUFFER_VIEW_ATTACH_INFO;
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001166 demo->uniform_data.attach.view = demo->uniform_data.view;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001167}
1168
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001169static void demo_prepare_descriptor_layout(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001170{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001171 const VkDescriptorSetLayoutBinding layout_bindings[2] = {
Chia-I Wua2aa8632015-03-26 15:04:41 +08001172 [0] = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001173 .descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
Chia-I Wua2aa8632015-03-26 15:04:41 +08001174 .count = 1,
Tony Barbour72304ef2015-04-16 15:59:00 -06001175 .stageFlags = VK_SHADER_STAGE_VERTEX_BIT,
Chia-I Wu91e8e212015-03-27 12:56:09 +08001176 .pImmutableSamplers = NULL,
Chia-I Wua2aa8632015-03-26 15:04:41 +08001177 },
1178 [1] = {
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -06001179 .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
Chia-I Wua2aa8632015-03-26 15:04:41 +08001180 .count = DEMO_TEXTURE_COUNT,
Tony Barbour72304ef2015-04-16 15:59:00 -06001181 .stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT,
Chia-I Wu91e8e212015-03-27 12:56:09 +08001182 .pImmutableSamplers = NULL,
Chia-I Wua2aa8632015-03-26 15:04:41 +08001183 },
1184 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001185 const VkDescriptorSetLayoutCreateInfo descriptor_layout = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001186 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001187 .pNext = NULL,
Chia-I Wua2aa8632015-03-26 15:04:41 +08001188 .count = 2,
1189 .pBinding = layout_bindings,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001190 };
Tony Barbourfdc2d352015-04-22 09:02:32 -06001191 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001192
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001193 err = vkCreateDescriptorSetLayout(demo->device,
Chia-I Wub58c24a2015-03-26 15:27:55 +08001194 &descriptor_layout, &demo->desc_layout);
1195 assert(!err);
1196
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001197 const VkPipelineLayoutCreateInfo pPipelineLayoutCreateInfo = {
1198 .sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
1199 .pNext = NULL,
1200 .descriptorSetCount = 1,
1201 .pSetLayouts = &demo->desc_layout,
1202 };
1203
1204 err = vkCreatePipelineLayout(demo->device,
1205 &pPipelineLayoutCreateInfo,
1206 &demo->pipeline_layout);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001207 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001208}
1209
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001210static VkShader demo_prepare_shader(struct demo *demo,
Tony Barbour72304ef2015-04-16 15:59:00 -06001211 VkShaderStage stage,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001212 const void *code,
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001213 size_t size)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001214{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001215 VkShaderCreateInfo createInfo;
1216 VkShader shader;
1217 VkResult err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001218
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001219
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001220 createInfo.sType = VK_STRUCTURE_TYPE_SHADER_CREATE_INFO;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001221 createInfo.pNext = NULL;
1222
Cody Northrop8b12a1f2015-03-17 15:55:58 -06001223#ifdef EXTERNAL_SPV
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001224 createInfo.codeSize = size;
1225 createInfo.pCode = code;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001226 createInfo.flags = 0;
1227
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001228 err = vkCreateShader(demo->device, &createInfo, &shader);
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001229 if (err) {
1230 free((void *) createInfo.pCode);
1231 }
1232#else
Cody Northrop8b12a1f2015-03-17 15:55:58 -06001233 // Create fake SPV structure to feed GLSL
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001234 // to the driver "under the covers"
1235 createInfo.codeSize = 3 * sizeof(uint32_t) + size + 1;
1236 createInfo.pCode = malloc(createInfo.codeSize);
1237 createInfo.flags = 0;
1238
Tony Barbour72304ef2015-04-16 15:59:00 -06001239 /* try version 0 first: VkShaderStage followed by GLSL */
Cody Northrop8b12a1f2015-03-17 15:55:58 -06001240 ((uint32_t *) createInfo.pCode)[0] = ICD_SPV_MAGIC;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001241 ((uint32_t *) createInfo.pCode)[1] = 0;
1242 ((uint32_t *) createInfo.pCode)[2] = stage;
1243 memcpy(((uint32_t *) createInfo.pCode + 3), code, size + 1);
1244
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001245 err = vkCreateShader(demo->device, &createInfo, &shader);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001246 if (err) {
1247 free((void *) createInfo.pCode);
Tony Barbourf9921732015-04-22 11:36:22 -06001248 return (VkShader) VK_NULL_HANDLE;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001249 }
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001250#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001251
1252 return shader;
1253}
1254
Cody Northrop8b12a1f2015-03-17 15:55:58 -06001255char *demo_read_spv(const char *filename, size_t *psize)
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001256{
1257 long int size;
Tony Barbourf9921732015-04-22 11:36:22 -06001258 size_t U_ASSERT_ONLY retval;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001259 void *shader_code;
1260
1261 FILE *fp = fopen(filename, "rb");
1262 if (!fp) return NULL;
1263
1264 fseek(fp, 0L, SEEK_END);
1265 size = ftell(fp);
1266
1267 fseek(fp, 0L, SEEK_SET);
1268
1269 shader_code = malloc(size);
Tony Barbourfdc2d352015-04-22 09:02:32 -06001270 retval = fread(shader_code, size, 1, fp);
1271 assert(retval == 1);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001272
1273 *psize = size;
1274
1275 return shader_code;
1276}
1277
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001278static VkShader demo_prepare_vs(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001279{
Cody Northrop8b12a1f2015-03-17 15:55:58 -06001280#ifdef EXTERNAL_SPV
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001281 void *vertShaderCode;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001282 size_t size;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001283
Cody Northrop8b12a1f2015-03-17 15:55:58 -06001284 vertShaderCode = demo_read_spv("cube-vert.spv", &size);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001285
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001286 return demo_prepare_shader(demo, VK_SHADER_STAGE_VERTEX,
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001287 vertShaderCode, size);
1288#else
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001289 static const char *vertShaderText =
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001290 "#version 140\n"
1291 "#extension GL_ARB_separate_shader_objects : enable\n"
1292 "#extension GL_ARB_shading_language_420pack : enable\n"
1293 "\n"
1294 "layout(binding = 0) uniform buf {\n"
1295 " mat4 MVP;\n"
1296 " vec4 position[12*3];\n"
1297 " vec4 attr[12*3];\n"
1298 "} ubuf;\n"
1299 "\n"
1300 "layout (location = 0) out vec4 texcoord;\n"
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001301 "\n"
1302 "void main() \n"
1303 "{\n"
1304 " texcoord = ubuf.attr[gl_VertexID];\n"
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001305 " gl_Position = ubuf.MVP * ubuf.position[gl_VertexID];\n"
1306 "}\n";
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001307
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001308 return demo_prepare_shader(demo, VK_SHADER_STAGE_VERTEX,
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001309 (const void *) vertShaderText,
1310 strlen(vertShaderText));
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001311#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001312}
1313
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001314static VkShader demo_prepare_fs(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001315{
Cody Northrop8b12a1f2015-03-17 15:55:58 -06001316#ifdef EXTERNAL_SPV
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001317 void *fragShaderCode;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001318 size_t size;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001319
Cody Northrop8b12a1f2015-03-17 15:55:58 -06001320 fragShaderCode = demo_read_spv("cube-frag.spv", &size);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001321
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001322 return demo_prepare_shader(demo, VK_SHADER_STAGE_FRAGMENT,
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001323 fragShaderCode, size);
1324#else
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001325 static const char *fragShaderText =
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001326 "#version 140\n"
1327 "#extension GL_ARB_separate_shader_objects : enable\n"
1328 "#extension GL_ARB_shading_language_420pack : enable\n"
Courtney Goeltzenleuchter4d1acf12015-02-25 15:13:35 -07001329 "layout (binding = 1) uniform sampler2D tex;\n"
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001330 "\n"
1331 "layout (location = 0) in vec4 texcoord;\n"
1332 "void main() {\n"
1333 " gl_FragColor = texture(tex, texcoord.xy);\n"
1334 "}\n";
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001335
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001336 return demo_prepare_shader(demo, VK_SHADER_STAGE_FRAGMENT,
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001337 (const void *) fragShaderText,
1338 strlen(fragShaderText));
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001339#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001340}
1341
1342static void demo_prepare_pipeline(struct demo *demo)
1343{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001344 VkGraphicsPipelineCreateInfo pipeline;
1345 VkPipelineIaStateCreateInfo ia;
1346 VkPipelineRsStateCreateInfo rs;
1347 VkPipelineCbStateCreateInfo cb;
1348 VkPipelineDsStateCreateInfo ds;
1349 VkPipelineShaderStageCreateInfo vs;
1350 VkPipelineShaderStageCreateInfo fs;
1351 VkPipelineVpStateCreateInfo vp;
1352 VkPipelineMsStateCreateInfo ms;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001353 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001354
1355 memset(&pipeline, 0, sizeof(pipeline));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001356 pipeline.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001357 pipeline.layout = demo->pipeline_layout;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001358
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001359 memset(&ia, 0, sizeof(ia));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001360 ia.sType = VK_STRUCTURE_TYPE_PIPELINE_IA_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06001361 ia.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001362
1363 memset(&rs, 0, sizeof(rs));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001364 rs.sType = VK_STRUCTURE_TYPE_PIPELINE_RS_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06001365 rs.fillMode = VK_FILL_MODE_SOLID;
1366 rs.cullMode = VK_CULL_MODE_BACK;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001367 rs.frontFace = VK_FRONT_FACE_CCW;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001368
1369 memset(&cb, 0, sizeof(cb));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001370 cb.sType = VK_STRUCTURE_TYPE_PIPELINE_CB_STATE_CREATE_INFO;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001371 VkPipelineCbAttachmentState att_state[1];
Tony Barbour29645d02015-01-16 14:27:35 -07001372 memset(att_state, 0, sizeof(att_state));
1373 att_state[0].format = demo->format;
1374 att_state[0].channelWriteMask = 0xf;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001375 att_state[0].blendEnable = VK_FALSE;
Tony Barbour29645d02015-01-16 14:27:35 -07001376 cb.attachmentCount = 1;
1377 cb.pAttachments = att_state;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001378
Tony Barbour29645d02015-01-16 14:27:35 -07001379 memset(&vp, 0, sizeof(vp));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001380 vp.sType = VK_STRUCTURE_TYPE_PIPELINE_VP_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06001381 vp.viewportCount = 1;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001382 vp.clipOrigin = VK_COORDINATE_ORIGIN_LOWER_LEFT;
Tony Barbour29645d02015-01-16 14:27:35 -07001383
1384 memset(&ds, 0, sizeof(ds));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001385 ds.sType = VK_STRUCTURE_TYPE_PIPELINE_DS_STATE_CREATE_INFO;
Tony Barbour29645d02015-01-16 14:27:35 -07001386 ds.format = demo->depth.format;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001387 ds.depthTestEnable = VK_TRUE;
1388 ds.depthWriteEnable = VK_TRUE;
Tony Barbour72304ef2015-04-16 15:59:00 -06001389 ds.depthCompareOp = VK_COMPARE_OP_LESS_EQUAL;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001390 ds.depthBoundsEnable = VK_FALSE;
1391 ds.back.stencilFailOp = VK_STENCIL_OP_KEEP;
1392 ds.back.stencilPassOp = VK_STENCIL_OP_KEEP;
Tony Barbour72304ef2015-04-16 15:59:00 -06001393 ds.back.stencilCompareOp = VK_COMPARE_OP_ALWAYS;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001394 ds.stencilTestEnable = VK_FALSE;
Tony Barbour29645d02015-01-16 14:27:35 -07001395 ds.front = ds.back;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001396
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001397 memset(&vs, 0, sizeof(vs));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001398 vs.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1399 vs.shader.stage = VK_SHADER_STAGE_VERTEX;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001400 vs.shader.shader = demo_prepare_vs(demo);
Mike Stroyanebae8322015-04-17 12:36:38 -06001401 assert(vs.shader.shader != VK_NULL_HANDLE);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001402
1403 memset(&fs, 0, sizeof(fs));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001404 fs.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1405 fs.shader.stage = VK_SHADER_STAGE_FRAGMENT;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001406 fs.shader.shader = demo_prepare_fs(demo);
Mike Stroyanebae8322015-04-17 12:36:38 -06001407 assert(fs.shader.shader != VK_NULL_HANDLE);
Tony Barbour29645d02015-01-16 14:27:35 -07001408
1409 memset(&ms, 0, sizeof(ms));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001410 ms.sType = VK_STRUCTURE_TYPE_PIPELINE_MS_STATE_CREATE_INFO;
Tony Barbour29645d02015-01-16 14:27:35 -07001411 ms.sampleMask = 1;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001412 ms.multisampleEnable = VK_FALSE;
Tony Barbour29645d02015-01-16 14:27:35 -07001413 ms.samples = 1;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001414
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001415 pipeline.pNext = (const void *) &ia;
1416 ia.pNext = (const void *) &rs;
1417 rs.pNext = (const void *) &cb;
1418 cb.pNext = (const void *) &ms;
1419 ms.pNext = (const void *) &vp;
1420 vp.pNext = (const void *) &ds;
1421 ds.pNext = (const void *) &vs;
1422 vs.pNext = (const void *) &fs;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001423
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001424 err = vkCreateGraphicsPipeline(demo->device, &pipeline, &demo->pipeline);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001425 assert(!err);
1426
Mike Stroyanebae8322015-04-17 12:36:38 -06001427 vkDestroyObject(demo->device, VK_OBJECT_TYPE_SHADER, vs.shader.shader);
1428 vkDestroyObject(demo->device, VK_OBJECT_TYPE_SHADER, fs.shader.shader);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001429}
1430
1431static void demo_prepare_dynamic_states(struct demo *demo)
1432{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001433 VkDynamicVpStateCreateInfo viewport_create;
1434 VkDynamicRsStateCreateInfo raster;
1435 VkDynamicCbStateCreateInfo color_blend;
1436 VkDynamicDsStateCreateInfo depth_stencil;
Tony Barbourfdc2d352015-04-22 09:02:32 -06001437 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001438
Tony Barbour29645d02015-01-16 14:27:35 -07001439 memset(&viewport_create, 0, sizeof(viewport_create));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001440 viewport_create.sType = VK_STRUCTURE_TYPE_DYNAMIC_VP_STATE_CREATE_INFO;
Courtney Goeltzenleuchter1710d8d2015-02-11 14:13:34 -07001441 viewport_create.viewportAndScissorCount = 1;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001442 VkViewport viewport;
Piers Daniell735ee532015-02-23 16:23:13 -07001443 memset(&viewport, 0, sizeof(viewport));
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001444 viewport.height = (float) demo->height;
1445 viewport.width = (float) demo->width;
1446 viewport.minDepth = (float) 0.0f;
1447 viewport.maxDepth = (float) 1.0f;
Piers Daniell735ee532015-02-23 16:23:13 -07001448 viewport_create.pViewports = &viewport;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001449 VkRect scissor;
Piers Daniell735ee532015-02-23 16:23:13 -07001450 memset(&scissor, 0, sizeof(scissor));
Courtney Goeltzenleuchter1710d8d2015-02-11 14:13:34 -07001451 scissor.extent.width = demo->width;
1452 scissor.extent.height = demo->height;
1453 scissor.offset.x = 0;
1454 scissor.offset.y = 0;
Courtney Goeltzenleuchter1710d8d2015-02-11 14:13:34 -07001455 viewport_create.pScissors = &scissor;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001456
1457 memset(&raster, 0, sizeof(raster));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001458 raster.sType = VK_STRUCTURE_TYPE_DYNAMIC_RS_STATE_CREATE_INFO;
Piers Daniell735ee532015-02-23 16:23:13 -07001459 raster.pointSize = 1.0;
1460 raster.lineWidth = 1.0;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001461
1462 memset(&color_blend, 0, sizeof(color_blend));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001463 color_blend.sType = VK_STRUCTURE_TYPE_DYNAMIC_CB_STATE_CREATE_INFO;
Piers Daniell735ee532015-02-23 16:23:13 -07001464 color_blend.blendConst[0] = 1.0f;
1465 color_blend.blendConst[1] = 1.0f;
1466 color_blend.blendConst[2] = 1.0f;
1467 color_blend.blendConst[3] = 1.0f;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001468
1469 memset(&depth_stencil, 0, sizeof(depth_stencil));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001470 depth_stencil.sType = VK_STRUCTURE_TYPE_DYNAMIC_DS_STATE_CREATE_INFO;
Piers Daniell735ee532015-02-23 16:23:13 -07001471 depth_stencil.minDepth = 0.0f;
1472 depth_stencil.maxDepth = 1.0f;
Tony Barbour29645d02015-01-16 14:27:35 -07001473 depth_stencil.stencilBackRef = 0;
1474 depth_stencil.stencilFrontRef = 0;
1475 depth_stencil.stencilReadMask = 0xff;
1476 depth_stencil.stencilWriteMask = 0xff;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001477
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001478 err = vkCreateDynamicViewportState(demo->device, &viewport_create, &demo->viewport);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001479 assert(!err);
1480
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001481 err = vkCreateDynamicRasterState(demo->device, &raster, &demo->raster);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001482 assert(!err);
1483
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001484 err = vkCreateDynamicColorBlendState(demo->device,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001485 &color_blend, &demo->color_blend);
1486 assert(!err);
1487
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001488 err = vkCreateDynamicDepthStencilState(demo->device,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001489 &depth_stencil, &demo->depth_stencil);
1490 assert(!err);
1491}
1492
Chia-I Wu63ea9262015-03-26 13:14:16 +08001493static void demo_prepare_descriptor_pool(struct demo *demo)
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001494{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001495 const VkDescriptorTypeCount type_counts[2] = {
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001496 [0] = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001497 .type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001498 .count = 1,
1499 },
1500 [1] = {
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -06001501 .type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001502 .count = DEMO_TEXTURE_COUNT,
1503 },
1504 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001505 const VkDescriptorPoolCreateInfo descriptor_pool = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001506 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001507 .pNext = NULL,
1508 .count = 2,
1509 .pTypeCount = type_counts,
1510 };
Tony Barbourfdc2d352015-04-22 09:02:32 -06001511 VkResult U_ASSERT_ONLY err;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001512
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001513 err = vkCreateDescriptorPool(demo->device,
1514 VK_DESCRIPTOR_POOL_USAGE_ONE_SHOT, 1,
Chia-I Wu63ea9262015-03-26 13:14:16 +08001515 &descriptor_pool, &demo->desc_pool);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001516 assert(!err);
1517}
1518
1519static void demo_prepare_descriptor_set(struct demo *demo)
1520{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001521 VkImageViewAttachInfo view_info[DEMO_TEXTURE_COUNT];
1522 VkSamplerImageViewInfo combined_info[DEMO_TEXTURE_COUNT];
1523 VkUpdateSamplerTextures update_fs;
1524 VkUpdateBuffers update_vs;
Chia-I Wub58c24a2015-03-26 15:27:55 +08001525 const void *update_array[2] = { &update_vs, &update_fs };
Tony Barbourfdc2d352015-04-22 09:02:32 -06001526 VkResult U_ASSERT_ONLY err;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001527 uint32_t count;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001528 uint32_t i;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001529
1530 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001531 view_info[i].sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_ATTACH_INFO;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001532 view_info[i].pNext = NULL;
1533 view_info[i].view = demo->textures[i].view,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001534 view_info[i].layout = VK_IMAGE_LAYOUT_GENERAL;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001535
Courtney Goeltzenleuchtereb4754f2015-04-09 11:43:10 -06001536 combined_info[i].sampler = demo->textures[i].sampler;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001537 combined_info[i].pImageView = &view_info[i];
1538 }
1539
1540 memset(&update_vs, 0, sizeof(update_vs));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001541 update_vs.sType = VK_STRUCTURE_TYPE_UPDATE_BUFFERS;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001542 update_vs.pNext = &update_fs;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001543 update_vs.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001544 update_vs.count = 1;
Chia-I Wub58c24a2015-03-26 15:27:55 +08001545 update_vs.pBufferViews = &demo->uniform_data.attach;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001546
1547 memset(&update_fs, 0, sizeof(update_fs));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001548 update_fs.sType = VK_STRUCTURE_TYPE_UPDATE_SAMPLER_TEXTURES;
Chia-I Wub58c24a2015-03-26 15:27:55 +08001549 update_fs.binding = 1;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001550 update_fs.count = DEMO_TEXTURE_COUNT;
1551 update_fs.pSamplerImageViews = combined_info;
1552
Mike Stroyanebae8322015-04-17 12:36:38 -06001553 err = vkAllocDescriptorSets(demo->device, demo->desc_pool,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001554 VK_DESCRIPTOR_SET_USAGE_STATIC,
Chia-I Wu87544e72015-02-23 10:41:08 -07001555 1, &demo->desc_layout,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001556 &demo->desc_set, &count);
1557 assert(!err && count == 1);
1558
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001559 vkBeginDescriptorPoolUpdate(demo->device,
1560 VK_DESCRIPTOR_UPDATE_MODE_FASTEST);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001561
Mike Stroyanebae8322015-04-17 12:36:38 -06001562 vkClearDescriptorSets(demo->device, demo->desc_pool, 1, &demo->desc_set);
1563 vkUpdateDescriptors(demo->device, demo->desc_set, 2, update_array);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001564
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001565 vkEndDescriptorPoolUpdate(demo->device, demo->buffers[demo->current_buffer].cmd);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001566}
1567
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001568static void demo_prepare(struct demo *demo)
1569{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001570 const VkCmdBufferCreateInfo cmd = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001571 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001572 .pNext = NULL,
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07001573 .queueNodeIndex = demo->graphics_queue_node_index,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001574 .flags = 0,
1575 };
Tony Barbourfdc2d352015-04-22 09:02:32 -06001576 VkResult U_ASSERT_ONLY err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001577
1578 demo_prepare_buffers(demo);
1579 demo_prepare_depth(demo);
1580 demo_prepare_textures(demo);
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001581 demo_prepare_cube_data_buffer(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001582
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001583 demo_prepare_descriptor_layout(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001584 demo_prepare_pipeline(demo);
1585 demo_prepare_dynamic_states(demo);
1586
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001587 for (int i = 0; i < DEMO_BUFFER_COUNT; i++) {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001588 err = vkCreateCommandBuffer(demo->device, &cmd, &demo->buffers[i].cmd);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001589 assert(!err);
1590 }
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001591
Chia-I Wu63ea9262015-03-26 13:14:16 +08001592 demo_prepare_descriptor_pool(demo);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001593 demo_prepare_descriptor_set(demo);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001594
1595
1596 for (int i = 0; i < DEMO_BUFFER_COUNT; i++) {
1597 demo->current_buffer = i;
1598 demo_draw_build_cmd(demo, demo->buffers[i].cmd);
1599 }
1600
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001601 /*
1602 * Prepare functions above may generate pipeline commands
1603 * that need to be flushed before beginning the render loop.
1604 */
1605 demo_flush_init_cmd(demo);
1606
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001607 demo->current_buffer = 0;
Jon Ashburn8d26c062015-04-24 09:46:24 -07001608 demo->prepared = true;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001609}
1610
Ian Elliott639ca472015-04-16 15:23:05 -06001611#ifdef _WIN32
1612static void demo_run(struct demo *demo)
1613{
Courtney Goeltzenleuchterb7e22702015-04-27 14:56:34 -06001614 if (!demo->prepared)
1615 return;
Ian Elliott639ca472015-04-16 15:23:05 -06001616 // Wait for work to finish before updating MVP.
1617 vkDeviceWaitIdle(demo->device);
1618 demo_update_data_buffer(demo);
1619
1620 demo_draw(demo);
1621
1622 // Wait for work to finish before updating MVP.
1623 vkDeviceWaitIdle(demo->device);
1624}
1625
1626// On MS-Windows, make this a global, so it's available to WndProc()
1627struct demo demo;
1628
1629// MS-Windows event handling function:
1630LRESULT CALLBACK WndProc(HWND hWnd,
1631 UINT uMsg,
1632 WPARAM wParam,
1633 LPARAM lParam)
1634{
Ian Elliott44e33f72015-04-28 10:52:52 -06001635<<<<<<< HEAD
Ian Elliott639ca472015-04-16 15:23:05 -06001636 char tmp_str[] = "Test Vulkan Cube Program";
Ian Elliott44e33f72015-04-28 10:52:52 -06001637=======
1638 PAINTSTRUCT paint_struct;
1639 HDC hDC; // Device context
1640 char tmp_str[] = APP_LONG_NAME;
1641>>>>>>> 7224550... demos: Use macros for the app name, and window name.
Ian Elliott639ca472015-04-16 15:23:05 -06001642
1643 switch(uMsg)
1644 {
1645 case WM_CREATE:
1646 return 0;
1647 case WM_CLOSE:
1648 PostQuitMessage(0);
1649 return 0;
1650 case WM_PAINT:
1651 demo_run(&demo);
1652 return 0;
1653 default:
1654 break;
1655 }
1656 return (DefWindowProc(hWnd, uMsg, wParam, lParam));
1657}
1658
1659static void demo_create_window(struct demo *demo)
1660{
1661 WNDCLASSEX win_class;
1662
1663 // Initialize the window class structure:
1664 win_class.cbSize = sizeof(WNDCLASSEX);
1665 win_class.style = CS_HREDRAW | CS_VREDRAW;
1666 win_class.lpfnWndProc = WndProc;
1667 win_class.cbClsExtra = 0;
1668 win_class.cbWndExtra = 0;
1669 win_class.hInstance = demo->connection; // hInstance
1670 win_class.hIcon = LoadIcon(NULL, IDI_APPLICATION);
1671 win_class.hCursor = LoadCursor(NULL, IDC_ARROW);
1672 win_class.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
1673 win_class.lpszMenuName = NULL;
1674 win_class.lpszClassName = demo->name;
1675 win_class.hIconSm = LoadIcon(NULL, IDI_WINLOGO);
1676 // Register window class:
1677 if (!RegisterClassEx(&win_class)) {
1678 // It didn't work, so try to give a useful error:
1679 printf("Unexpected error trying to start the application!\n");
1680 fflush(stdout);
1681 exit(1);
1682 }
1683 // Create window with the registered class:
1684 demo->window = CreateWindowEx(0,
1685 demo->name, // class name
1686 demo->name, // app name
1687 WS_OVERLAPPEDWINDOW | // window style
1688 WS_VISIBLE |
1689 WS_SYSMENU,
1690 100,100, // x/y coords
1691 demo->width, // width
1692 demo->height, // height
1693 NULL, // handle to parent
1694 NULL, // handle to menu
1695 demo->connection, // hInstance
1696 NULL); // no extra parameters
1697 if (!demo->window) {
1698 // It didn't work, so try to give a useful error:
1699 printf("Cannot create a window in which to draw!\n");
1700 fflush(stdout);
1701 exit(1);
1702 }
1703}
1704#else // _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001705static void demo_handle_event(struct demo *demo,
1706 const xcb_generic_event_t *event)
1707{
Piers Daniell735ee532015-02-23 16:23:13 -07001708 uint8_t event_code = event->response_type & 0x7f;
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07001709 switch (event_code) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001710 case XCB_EXPOSE:
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07001711 // TODO: Resize window
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001712 break;
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07001713 case XCB_CLIENT_MESSAGE:
1714 if((*(xcb_client_message_event_t*)event).data.data32[0] ==
1715 (*demo->atom_wm_delete_window).atom) {
1716 demo->quit = true;
1717 }
1718 break;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001719 case XCB_KEY_RELEASE:
1720 {
1721 const xcb_key_release_event_t *key =
1722 (const xcb_key_release_event_t *) event;
1723
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001724 switch (key->detail) {
1725 case 0x9: // Escape
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001726 demo->quit = true;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001727 break;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001728 case 0x71: // left arrow key
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001729 demo->spin_angle += demo->spin_increment;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001730 break;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001731 case 0x72: // right arrow key
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001732 demo->spin_angle -= demo->spin_increment;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001733 break;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001734 case 0x41:
1735 demo->pause = !demo->pause;
Piers Daniell735ee532015-02-23 16:23:13 -07001736 break;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001737 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001738 }
1739 break;
1740 default:
1741 break;
1742 }
1743}
1744
1745static void demo_run(struct demo *demo)
1746{
1747 xcb_flush(demo->connection);
1748
1749 while (!demo->quit) {
1750 xcb_generic_event_t *event;
1751
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07001752 if (demo->pause) {
1753 event = xcb_wait_for_event(demo->connection);
1754 } else {
1755 event = xcb_poll_for_event(demo->connection);
1756 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001757 if (event) {
1758 demo_handle_event(demo, event);
1759 free(event);
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07001760 }
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07001761
1762 // Wait for work to finish before updating MVP.
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001763 vkDeviceWaitIdle(demo->device);
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07001764 demo_update_data_buffer(demo);
1765
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07001766 demo_draw(demo);
Courtney Goeltzenleuchter1454f3c2014-11-18 11:28:09 -07001767
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07001768 // Wait for work to finish before updating MVP.
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001769 vkDeviceWaitIdle(demo->device);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001770 }
1771}
1772
1773static void demo_create_window(struct demo *demo)
1774{
1775 uint32_t value_mask, value_list[32];
1776
1777 demo->window = xcb_generate_id(demo->connection);
1778
1779 value_mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK;
1780 value_list[0] = demo->screen->black_pixel;
1781 value_list[1] = XCB_EVENT_MASK_KEY_RELEASE |
1782 XCB_EVENT_MASK_EXPOSURE;
1783
1784 xcb_create_window(demo->connection,
1785 XCB_COPY_FROM_PARENT,
1786 demo->window, demo->screen->root,
1787 0, 0, demo->width, demo->height, 0,
1788 XCB_WINDOW_CLASS_INPUT_OUTPUT,
1789 demo->screen->root_visual,
1790 value_mask, value_list);
1791
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07001792 /* Magic code that will send notification when window is destroyed */
1793 xcb_intern_atom_cookie_t cookie = xcb_intern_atom(demo->connection, 1, 12,
1794 "WM_PROTOCOLS");
1795 xcb_intern_atom_reply_t* reply = xcb_intern_atom_reply(demo->connection, cookie, 0);
1796
1797 xcb_intern_atom_cookie_t cookie2 = xcb_intern_atom(demo->connection, 0, 16, "WM_DELETE_WINDOW");
1798 demo->atom_wm_delete_window = xcb_intern_atom_reply(demo->connection, cookie2, 0);
1799
1800 xcb_change_property(demo->connection, XCB_PROP_MODE_REPLACE,
1801 demo->window, (*reply).atom, 4, 32, 1,
1802 &(*demo->atom_wm_delete_window).atom);
1803 free(reply);
1804
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001805 xcb_map_window(demo->connection, demo->window);
1806}
Ian Elliott639ca472015-04-16 15:23:05 -06001807#endif // _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001808
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001809static void demo_init_vk(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001810{
Tobin Ehlis99239dc2015-04-16 18:04:57 -06001811 VkResult err;
1812 // Extensions to enable
1813 const char *ext_names[] = {
Chia-I Wucbb564e2015-04-16 22:02:10 +08001814 "VK_WSI_LunarG",
Tobin Ehlis99239dc2015-04-16 18:04:57 -06001815 };
1816 size_t extSize = sizeof(uint32_t);
1817 uint32_t extCount = 0;
1818 err = vkGetGlobalExtensionInfo(VK_EXTENSION_INFO_TYPE_COUNT, 0, &extSize, &extCount);
1819 assert(!err);
1820
1821 VkExtensionProperties extProp;
1822 extSize = sizeof(VkExtensionProperties);
Tony Barbourfdc2d352015-04-22 09:02:32 -06001823 bool32_t U_ASSERT_ONLY extFound = 0;
Tobin Ehlis99239dc2015-04-16 18:04:57 -06001824 for (uint32_t i = 0; i < extCount; i++) {
1825 err = vkGetGlobalExtensionInfo(VK_EXTENSION_INFO_TYPE_PROPERTIES, i, &extSize, &extProp);
1826 if (!strcmp(ext_names[0], extProp.extName))
1827 extFound = 1;
1828 }
1829 assert(extFound);
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001830 const VkApplicationInfo app = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001831 .sType = VK_STRUCTURE_TYPE_APPLICATION_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001832 .pNext = NULL,
Ian Elliott44e33f72015-04-28 10:52:52 -06001833 .pAppName = APP_SHORT_NAME,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001834 .appVersion = 0,
Ian Elliott44e33f72015-04-28 10:52:52 -06001835 .pEngineName = APP_SHORT_NAME,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001836 .engineVersion = 0,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001837 .apiVersion = VK_API_VERSION,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001838 };
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -06001839 const VkInstanceCreateInfo inst_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001840 .sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
Jon Ashburnab46b362015-04-04 14:52:07 -06001841 .pNext = NULL,
1842 .pAppInfo = &app,
1843 .pAllocCb = NULL,
Tobin Ehlis99239dc2015-04-16 18:04:57 -06001844 .extensionCount = 1,
1845 .ppEnabledExtensionNames = ext_names,
Jon Ashburnab46b362015-04-04 14:52:07 -06001846 };
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -06001847 const VkDeviceQueueCreateInfo queue = {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001848 .queueNodeIndex = 0,
1849 .queueCount = 1,
1850 };
Tobin Ehlis7537db92015-04-16 10:04:35 -06001851
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -06001852 const VkDeviceCreateInfo device = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001853 .sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001854 .pNext = NULL,
1855 .queueRecordCount = 1,
1856 .pRequestedQueues = &queue,
Tobin Ehlis99239dc2015-04-16 18:04:57 -06001857 .extensionCount = 1,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001858 .ppEnabledExtensionNames = ext_names,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001859 .flags = VK_DEVICE_CREATE_VALIDATION_BIT,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001860 };
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001861 uint32_t gpu_count;
1862 uint32_t i;
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07001863 size_t data_size;
1864 uint32_t queue_count;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001865
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001866 err = vkCreateInstance(&inst_info, &demo->inst);
Tony Barbour7c5603c2015-04-17 16:39:58 -06001867 if (err) {
1868#ifdef _WIN32
1869 MessageBox(NULL, "vkCreateInstance failed - do you have a Vulkan graphics driver installed?",
1870 "vkCreateInstance Failure", MB_OK);
1871#else
1872 printf("vkCreateInstance failed - Do you have a Vulkan graphics driver installed?"
1873 "(\nExiting ...\n");
Ian Elliott3979e282015-04-03 15:24:55 -06001874 fflush(stdout);
Tony Barbour7c5603c2015-04-17 16:39:58 -06001875#endif
Ian Elliott3979e282015-04-03 15:24:55 -06001876 exit(1);
Ian Elliott3979e282015-04-03 15:24:55 -06001877 }
Jon Ashburnab46b362015-04-04 14:52:07 -06001878
Jon Ashburn07c0c0c2015-04-15 11:31:12 -06001879 gpu_count = 1;
1880 err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, &demo->gpu);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001881 assert(!err && gpu_count == 1);
1882
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001883 err = vkCreateDevice(demo->gpu, &device, &demo->device);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001884 assert(!err);
1885
Tony Barbour72304ef2015-04-16 15:59:00 -06001886 err = vkGetPhysicalDeviceInfo(demo->gpu, VK_PHYSICAL_DEVICE_INFO_TYPE_PROPERTIES,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001887 &data_size, NULL);
1888 assert(!err);
1889
Tony Barbour72304ef2015-04-16 15:59:00 -06001890 demo->gpu_props = (VkPhysicalDeviceProperties *) malloc(data_size);
1891 err = vkGetPhysicalDeviceInfo(demo->gpu, VK_PHYSICAL_DEVICE_INFO_TYPE_PROPERTIES,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001892 &data_size, demo->gpu_props);
1893 assert(!err);
1894
Tony Barbour72304ef2015-04-16 15:59:00 -06001895 err = vkGetPhysicalDeviceInfo(demo->gpu, VK_PHYSICAL_DEVICE_INFO_TYPE_QUEUE_PROPERTIES,
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07001896 &data_size, NULL);
1897 assert(!err);
1898
Tony Barbour72304ef2015-04-16 15:59:00 -06001899 demo->queue_props = (VkPhysicalDeviceQueueProperties *) malloc(data_size);
1900 err = vkGetPhysicalDeviceInfo(demo->gpu, VK_PHYSICAL_DEVICE_INFO_TYPE_QUEUE_PROPERTIES,
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07001901 &data_size, demo->queue_props);
1902 assert(!err);
Courtney Goeltzenleuchterc56c36a2015-04-21 09:31:23 -06001903 queue_count = (uint32_t)(data_size / sizeof(VkPhysicalDeviceQueueProperties));
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07001904 assert(queue_count >= 1);
1905
Mark Lobodzinskida9b1092015-04-16 11:44:05 -05001906 // Graphics queue and MemMgr queue can be separate.
1907 // TODO: Add support for separate queues, including synchronization,
1908 // and appropriate tracking for QueueSubmit and QueueBindObjectMemory
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07001909 for (i = 0; i < queue_count; i++) {
Mark Lobodzinskida9b1092015-04-16 11:44:05 -05001910 if ((demo->queue_props[i].queueFlags & VK_QUEUE_GRAPHICS_BIT) &&
1911 (demo->queue_props[i].queueFlags & VK_QUEUE_MEMMGR_BIT) )
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07001912 break;
1913 }
1914 assert(i < queue_count);
1915 demo->graphics_queue_node_index = i;
1916
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001917 err = vkGetDeviceQueue(demo->device, demo->graphics_queue_node_index,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001918 0, &demo->queue);
1919 assert(!err);
1920}
1921
1922static void demo_init_connection(struct demo *demo)
1923{
Ian Elliott639ca472015-04-16 15:23:05 -06001924#ifndef _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001925 const xcb_setup_t *setup;
1926 xcb_screen_iterator_t iter;
1927 int scr;
1928
1929 demo->connection = xcb_connect(NULL, &scr);
Ian Elliott3979e282015-04-03 15:24:55 -06001930 if (demo->connection == NULL) {
1931 printf("Cannot find a compatible Vulkan installable client driver "
1932 "(ICD).\nExiting ...\n");
1933 fflush(stdout);
1934 exit(1);
1935 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001936
1937 setup = xcb_get_setup(demo->connection);
1938 iter = xcb_setup_roots_iterator(setup);
1939 while (scr-- > 0)
1940 xcb_screen_next(&iter);
1941
1942 demo->screen = iter.data;
Ian Elliott639ca472015-04-16 15:23:05 -06001943#endif // _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001944}
1945
Ian Elliott639ca472015-04-16 15:23:05 -06001946#ifdef _WIN32
1947static void demo_init(struct demo *demo, HINSTANCE hInstance, LPSTR pCmdLine)
1948#else // _WIN32
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001949static void demo_init(struct demo *demo, int argc, char **argv)
Ian Elliott639ca472015-04-16 15:23:05 -06001950#endif // _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001951{
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001952 vec3 eye = {0.0f, 3.0f, 5.0f};
1953 vec3 origin = {0, 0, 0};
1954 vec3 up = {0.0f, -1.0f, 0.0};
Ian Elliott639ca472015-04-16 15:23:05 -06001955 bool argv_error = false;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001956
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001957 memset(demo, 0, sizeof(*demo));
1958
Ian Elliott639ca472015-04-16 15:23:05 -06001959#ifdef _WIN32
1960 demo->connection = hInstance;
Ian Elliott44e33f72015-04-28 10:52:52 -06001961 strncpy(demo->name, APP_SHORT_NAME, APP_NAME_STR_LEN);
Ian Elliott639ca472015-04-16 15:23:05 -06001962
1963 if (strncmp(pCmdLine, "--use_staging", strlen("--use_staging")) == 0)
1964 demo->use_staging_buffer = true;
1965 else if (strlen(pCmdLine) != 0) {
1966 fprintf(stderr, "Do not recognize argument \"%s\".\n", pCmdLine);
1967 argv_error = true;
1968 }
1969#else // _WIN32
Piers Daniell735ee532015-02-23 16:23:13 -07001970 for (int i = 1; i < argc; i++) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001971 if (strncmp(argv[i], "--use_staging", strlen("--use_staging")) == 0)
1972 demo->use_staging_buffer = true;
Ian Elliott639ca472015-04-16 15:23:05 -06001973 else {
1974 fprintf(stderr, "Do not recognize argument \"%s\".\n", argv[i]);
1975 argv_error = true;
1976 }
1977 }
1978#endif // _WIN32
1979 if (argv_error) {
Ian Elliott44e33f72015-04-28 10:52:52 -06001980 fprintf(stderr, "Usage:\n %s [--use_staging]\n", APP_SHORT_NAME);
Ian Elliott639ca472015-04-16 15:23:05 -06001981 fflush(stderr);
1982 exit(1);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001983 }
1984
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001985 demo_init_connection(demo);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001986 demo_init_vk(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001987
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001988 demo->width = 500;
1989 demo->height = 500;
Tony Barbour72304ef2015-04-16 15:59:00 -06001990 demo->format = VK_FORMAT_B8G8R8A8_UNORM;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001991
1992 demo->spin_angle = 0.01f;
1993 demo->spin_increment = 0.01f;
1994 demo->pause = false;
1995
Piers Daniell735ee532015-02-23 16:23:13 -07001996 mat4x4_perspective(demo->projection_matrix, (float)degreesToRadians(45.0f), 1.0f, 0.1f, 100.0f);
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001997 mat4x4_look_at(demo->view_matrix, eye, origin, up);
1998 mat4x4_identity(demo->model_matrix);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001999}
2000
2001static void demo_cleanup(struct demo *demo)
2002{
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06002003 uint32_t i, j;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002004
Courtney Goeltzenleuchterb7e22702015-04-27 14:56:34 -06002005 demo->prepared = false;
2006
Mike Stroyanebae8322015-04-17 12:36:38 -06002007 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DESCRIPTOR_SET, demo->desc_set);
2008 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DESCRIPTOR_POOL, demo->desc_pool);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08002009
Mike Stroyanebae8322015-04-17 12:36:38 -06002010 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DYNAMIC_VP_STATE, demo->viewport);
2011 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DYNAMIC_RS_STATE, demo->raster);
2012 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DYNAMIC_CB_STATE, demo->color_blend);
2013 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DYNAMIC_DS_STATE, demo->depth_stencil);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002014
Mike Stroyanebae8322015-04-17 12:36:38 -06002015 vkDestroyObject(demo->device, VK_OBJECT_TYPE_PIPELINE, demo->pipeline);
2016 vkDestroyObject(demo->device, VK_OBJECT_TYPE_PIPELINE_LAYOUT, demo->pipeline_layout);
2017 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DESCRIPTOR_SET_LAYOUT, demo->desc_layout);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002018
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002019 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Mike Stroyanebae8322015-04-17 12:36:38 -06002020 vkDestroyObject(demo->device, VK_OBJECT_TYPE_IMAGE_VIEW, demo->textures[i].view);
2021 vkQueueBindObjectMemory(demo->queue, VK_OBJECT_TYPE_IMAGE, demo->textures[i].image, 0, VK_NULL_HANDLE, 0);
2022 vkDestroyObject(demo->device, VK_OBJECT_TYPE_IMAGE, demo->textures[i].image);
Courtney Goeltzenleuchter1359b7a2015-04-02 16:25:42 -06002023 demo_remove_mem_refs(demo, demo->textures[i].num_mem, demo->textures[i].mem);
Jon Ashburnb2a66652015-01-16 09:37:43 -07002024 for (j = 0; j < demo->textures[i].num_mem; j++)
Mike Stroyanebae8322015-04-17 12:36:38 -06002025 vkFreeMemory(demo->device, demo->textures[i].mem[j]);
2026 free(demo->textures[i].mem);
2027 vkDestroyObject(demo->device, VK_OBJECT_TYPE_SAMPLER, demo->textures[i].sampler);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002028 }
Chia-I Wucbb564e2015-04-16 22:02:10 +08002029 vkDestroySwapChainWSI(demo->swap_chain);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002030
Mike Stroyanebae8322015-04-17 12:36:38 -06002031 vkDestroyObject(demo->device, VK_OBJECT_TYPE_DEPTH_STENCIL_VIEW, demo->depth.view);
2032 vkQueueBindObjectMemory(demo->queue, VK_OBJECT_TYPE_IMAGE, demo->depth.image, 0, VK_NULL_HANDLE, 0);
Courtney Goeltzenleuchter1359b7a2015-04-02 16:25:42 -06002033 demo_remove_mem_refs(demo, demo->depth.num_mem, demo->depth.mem);
Mike Stroyanebae8322015-04-17 12:36:38 -06002034 vkDestroyObject(demo->device, VK_OBJECT_TYPE_IMAGE, demo->depth.image);
Courtney Goeltzenleuchter1359b7a2015-04-02 16:25:42 -06002035 for (j = 0; j < demo->depth.num_mem; j++) {
Mike Stroyanebae8322015-04-17 12:36:38 -06002036 vkFreeMemory(demo->device, demo->depth.mem[j]);
Courtney Goeltzenleuchter1359b7a2015-04-02 16:25:42 -06002037 }
Mark Lobodzinski7b8fee62015-02-18 16:38:17 -06002038
Mike Stroyanebae8322015-04-17 12:36:38 -06002039 vkDestroyObject(demo->device, VK_OBJECT_TYPE_BUFFER_VIEW, demo->uniform_data.view);
2040 vkQueueBindObjectMemory(demo->queue, VK_OBJECT_TYPE_BUFFER, demo->uniform_data.buf, 0, VK_NULL_HANDLE, 0);
2041 vkDestroyObject(demo->device, VK_OBJECT_TYPE_BUFFER, demo->uniform_data.buf);
Courtney Goeltzenleuchter1359b7a2015-04-02 16:25:42 -06002042 demo_remove_mem_refs(demo, demo->uniform_data.num_mem, demo->uniform_data.mem);
Jon Ashburnae7c21c2015-01-19 15:00:26 -07002043 for (j = 0; j < demo->uniform_data.num_mem; j++)
Mike Stroyanebae8322015-04-17 12:36:38 -06002044 vkFreeMemory(demo->device, demo->uniform_data.mem[j]);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002045
2046 for (i = 0; i < DEMO_BUFFER_COUNT; i++) {
Mike Stroyanebae8322015-04-17 12:36:38 -06002047 vkDestroyObject(demo->device, VK_OBJECT_TYPE_COLOR_ATTACHMENT_VIEW, demo->buffers[i].view);
2048 vkDestroyObject(demo->device, VK_OBJECT_TYPE_COMMAND_BUFFER, demo->buffers[i].cmd);
Courtney Goeltzenleuchter1359b7a2015-04-02 16:25:42 -06002049 demo_remove_mem_refs(demo, 1, &demo->buffers[i].mem);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002050 }
2051
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002052 vkDestroyDevice(demo->device);
2053 vkDestroyInstance(demo->inst);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002054
Ian Elliott639ca472015-04-16 15:23:05 -06002055#ifndef _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002056 xcb_destroy_window(demo->connection, demo->window);
2057 xcb_disconnect(demo->connection);
Ian Elliott639ca472015-04-16 15:23:05 -06002058#endif // _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002059}
2060
Ian Elliott639ca472015-04-16 15:23:05 -06002061#ifdef _WIN32
Ian Elliottf9cf78c2015-04-28 10:33:11 -06002062
Ian Elliott639ca472015-04-16 15:23:05 -06002063int APIENTRY WinMain(HINSTANCE hInstance,
2064 HINSTANCE hPrevInstance,
2065 LPSTR pCmdLine,
2066 int nCmdShow)
2067{
2068 MSG msg; // message
2069 bool done; // flag saying when app is complete
2070
2071 demo_init(&demo, hInstance, pCmdLine);
2072 demo_create_window(&demo);
2073
2074 demo_prepare(&demo);
2075
2076 done = false; //initialize loop condition variable
2077 /* main message loop*/
2078 while(!done)
2079 {
Tony Barbourf9921732015-04-22 11:36:22 -06002080 PeekMessage(&msg,0,0,0,PM_REMOVE);
Ian Elliott639ca472015-04-16 15:23:05 -06002081 if (msg.message == WM_QUIT) //check for a quit message
2082 {
2083 done = true; //if found, quit app
2084 }
2085 else
2086 {
2087 /* Translate and dispatch to event queue*/
2088 TranslateMessage(&msg);
2089 DispatchMessage(&msg);
2090 }
2091 }
2092
2093 demo_cleanup(&demo);
2094
Tony Barbourf9921732015-04-22 11:36:22 -06002095 return (int) msg.wParam;
Ian Elliott639ca472015-04-16 15:23:05 -06002096}
2097#else // _WIN32
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002098int main(int argc, char **argv)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002099{
2100 struct demo demo;
2101
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002102 demo_init(&demo, argc, argv);
Chia-I Wucbb564e2015-04-16 22:02:10 +08002103 demo_create_window(&demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002104
2105 demo_prepare(&demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002106 demo_run(&demo);
2107
2108 demo_cleanup(&demo);
2109
2110 return 0;
2111}
Ian Elliott639ca472015-04-16 15:23:05 -06002112#endif // _WIN32