blob: 09d9ed80506fc6fb07c3bb8dfe782b26152f8e47 [file] [log] [blame]
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001#define _GNU_SOURCE
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002#include <stdio.h>
3#include <stdlib.h>
4#include <string.h>
5#include <stdbool.h>
6#include <assert.h>
7
Ian Elliott639ca472015-04-16 15:23:05 -06008#ifdef _WIN32
9#pragma comment(linker, "/subsystem:windows")
10#include <windows.h>
11#define APP_NAME_STR_LEN 80
12#else // _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060013#include <xcb/xcb.h>
Ian Elliott639ca472015-04-16 15:23:05 -060014#endif // _WIN32
15
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -060016#include <vulkan.h>
17#include <vkDbg.h>
Chia-I Wucbb564e2015-04-16 22:02:10 +080018#include <vk_wsi_lunarg.h>
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060019
Cody Northropfe3d8bc2015-03-17 14:54:35 -060020#include "icd-spv.h"
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060021
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -060022#include "linmath.h"
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -060023#include <png.h>
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -060024
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060025#define DEMO_BUFFER_COUNT 2
26#define DEMO_TEXTURE_COUNT 1
27
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -060028/*
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -070029 * structure to track all objects related to a texture.
30 */
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -060031struct texture_object {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -060032 VkSampler sampler;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -070033
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -060034 VkImage image;
35 VkImageLayout imageLayout;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -060036
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -070037 uint32_t num_mem;
Tony Barbour72304ef2015-04-16 15:59:00 -060038 VkDeviceMemory *mem;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -060039 VkImageView view;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -070040 int32_t tex_width, tex_height;
41};
42
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -060043static char *tex_files[] = {
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -060044 "lunarg-logo-256x256-solid.png"
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -060045};
46
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -060047struct vkcube_vs_uniform {
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -060048 // Must start with MVP
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -060049 float mvp[4][4];
50 float position[12*3][4];
51 float color[12*3][4];
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -060052};
53
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -060054struct vktexcube_vs_uniform {
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -060055 // Must start with MVP
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -060056 float mvp[4][4];
57 float position[12*3][4];
58 float attr[12*3][4];
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -060059};
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -060060
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060061//--------------------------------------------------------------------------------------
62// Mesh and VertexFormat Data
63//--------------------------------------------------------------------------------------
64struct Vertex
65{
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -060066 float posX, posY, posZ, posW; // Position data
67 float r, g, b, a; // Color
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060068};
69
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -060070struct VertexPosTex
71{
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -060072 float posX, posY, posZ, posW; // Position data
73 float u, v, s, t; // Texcoord
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -060074};
75
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060076#define XYZ1(_x_, _y_, _z_) (_x_), (_y_), (_z_), 1.f
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -060077#define UV(_u_, _v_) (_u_), (_v_), 0.f, 1.f
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060078
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -060079static const float g_vertex_buffer_data[] = {
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -060080 -1.0f,-1.0f,-1.0f, // Vertex 0
81 -1.0f,-1.0f, 1.0f,
82 -1.0f, 1.0f, 1.0f,
83
84 -1.0f, 1.0f, 1.0f, // Vertex 1
85 -1.0f, 1.0f,-1.0f,
86 -1.0f,-1.0f,-1.0f,
87
88 -1.0f,-1.0f,-1.0f, // Vertex 2
89 1.0f, 1.0f,-1.0f,
90 1.0f,-1.0f,-1.0f,
91
92 -1.0f,-1.0f,-1.0f, // Vertex 3
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -060093 -1.0f, 1.0f,-1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -060094 1.0f, 1.0f,-1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -060095
96 -1.0f,-1.0f,-1.0f, // Vertex 4
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -060097 1.0f,-1.0f,-1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -060098 1.0f,-1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -060099
100 -1.0f,-1.0f,-1.0f, // Vertex 5
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 6
105 -1.0f, 1.0f, 1.0f,
106 1.0f, 1.0f, 1.0f,
107
108 -1.0f, 1.0f,-1.0f, // Vertex 7
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 8
113 1.0f, 1.0f, 1.0f,
114 1.0f,-1.0f, 1.0f,
115
116 1.0f,-1.0f, 1.0f, // Vertex 9
117 1.0f,-1.0f,-1.0f,
118 1.0f, 1.0f,-1.0f,
119
120 -1.0f, 1.0f, 1.0f, // Vertex 10
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600121 -1.0f,-1.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600122 1.0f, 1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600123
124 -1.0f,-1.0f, 1.0f, // Vertex 11
125 1.0f,-1.0f, 1.0f,
126 1.0f, 1.0f, 1.0f,
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600127};
128
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600129static const float g_uv_buffer_data[] = {
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600130 1.0f, 0.0f, // Vertex 0
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600131 0.0f, 0.0f,
132 0.0f, 1.0f,
133
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600134 0.0f, 1.0f, // Vertex 1
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600135 1.0f, 1.0f,
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600136 1.0f, 0.0f,
137
138// 0.0f, 1.0f, // Vertex 2
139// 1.0f, 0.0f,
140// 0.0f, 0.0f,
141
142// 0.0f, 1.0f, // Vertex 3
143// 1.0f, 0.0f,
144// 1.0f, 1.0f,
145
146 0.0f, 0.0f, // Vertex 2
147 1.0f, 1.0f,
148 1.0f, 0.0f,
149
150 0.0f, 0.0f, // Vertex 3
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600151 0.0f, 1.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600152 1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600153
154 0.0f, 1.0f, // Vertex 4
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600155 0.0f, 0.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600156 1.0f, 0.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600157
158 0.0f, 1.0f, // Vertex 5
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600159 1.0f, 0.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 6
163 1.0f, 1.0f,
164 1.0f, 0.0f,
165
166 0.0f, 1.0f, // Vertex 7
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600167 1.0f, 0.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600168 0.0f, 0.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600169
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600170 0.0f, 1.0f, // Vertex 8
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600171 1.0f, 1.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600172 1.0f, 0.0f,
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600173
174 1.0f, 0.0f, // Vertex 9
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600175 0.0f, 0.0f,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600176 0.0f, 1.0f,
177
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600178 1.0f, 1.0f, // Vertex 10
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600179 1.0f, 0.0f,
Mike Stroyan16b3d982015-03-19 14:29:04 -0600180 0.0f, 1.0f,
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600181
182 1.0f, 0.0f, // Vertex 11
183 0.0f, 0.0f,
184 0.0f, 1.0f,
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600185};
186
187void dumpMatrix(const char *note, mat4x4 MVP)
188{
189 int i;
190
191 printf("%s: \n", note);
192 for (i=0; i<4; i++) {
193 printf("%f, %f, %f, %f\n", MVP[i][0], MVP[i][1], MVP[i][2], MVP[i][3]);
194 }
195 printf("\n");
196 fflush(stdout);
197}
198
199void dumpVec4(const char *note, vec4 vector)
200{
201 printf("%s: \n", note);
202 printf("%f, %f, %f, %f\n", vector[0], vector[1], vector[2], vector[3]);
203 printf("\n");
204 fflush(stdout);
205}
206
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600207struct demo {
Ian Elliott639ca472015-04-16 15:23:05 -0600208#ifdef _WIN32
209#define APP_NAME_STR_LEN 80
210 HINSTANCE connection; // hInstance - Windows Instance
211 char name[APP_NAME_STR_LEN]; // Name to put on the window/icon
212 HWND window; // hWnd - window handle
213#else // _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600214 xcb_connection_t *connection;
215 xcb_screen_t *screen;
Chia-I Wucbb564e2015-04-16 22:02:10 +0800216 xcb_window_t window;
217 xcb_intern_atom_reply_t *atom_wm_delete_window;
Ian Elliott639ca472015-04-16 15:23:05 -0600218#endif // _WIN32
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700219 bool use_staging_buffer;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600220
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600221 VkInstance inst;
Tony Barbour72304ef2015-04-16 15:59:00 -0600222 VkPhysicalDevice gpu;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600223 VkDevice device;
224 VkQueue queue;
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -0700225 uint32_t graphics_queue_node_index;
Tony Barbour72304ef2015-04-16 15:59:00 -0600226 VkPhysicalDeviceProperties *gpu_props;
227 VkPhysicalDeviceQueueProperties *queue_props;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600228
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600229 VkFramebuffer framebuffer;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600230 int width, height;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600231 VkFormat format;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600232
Chia-I Wucbb564e2015-04-16 22:02:10 +0800233 VkSwapChainWSI swap_chain;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600234 struct {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600235 VkImage image;
Tony Barbour72304ef2015-04-16 15:59:00 -0600236 VkDeviceMemory mem;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600237 VkCmdBuffer cmd;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600238
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600239 VkColorAttachmentView view;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600240 } buffers[DEMO_BUFFER_COUNT];
241
242 struct {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600243 VkFormat format;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600244
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600245 VkImage image;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600246 uint32_t num_mem;
Tony Barbour72304ef2015-04-16 15:59:00 -0600247 VkDeviceMemory *mem;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600248 VkDepthStencilView view;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600249 } depth;
250
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600251 struct texture_object textures[DEMO_TEXTURE_COUNT];
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600252
253 struct {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600254 VkBuffer buf;
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 VkBufferView view;
258 VkBufferViewAttachInfo attach;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600259 } uniform_data;
260
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600261 VkCmdBuffer cmd; // Buffer for initialization commands
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -0500262 VkPipelineLayout pipeline_layout;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600263 VkDescriptorSetLayout desc_layout;
264 VkPipeline pipeline;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600265
Courtney Goeltzenleuchter5e6d1e92015-04-10 16:24:50 -0600266 VkDynamicVpState viewport;
267 VkDynamicRsState raster;
268 VkDynamicCbState color_blend;
269 VkDynamicDsState depth_stencil;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600270
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600271 mat4x4 projection_matrix;
272 mat4x4 view_matrix;
273 mat4x4 model_matrix;
274
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600275 float spin_angle;
276 float spin_increment;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600277 bool pause;
278
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600279 VkDescriptorPool desc_pool;
280 VkDescriptorSet desc_set;
Chia-I Wu6a3c8972015-01-04 16:27:24 +0800281
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600282 bool quit;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600283 uint32_t current_buffer;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600284};
285
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600286static void demo_flush_init_cmd(struct demo *demo)
287{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600288 VkResult err;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600289
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600290 if (demo->cmd == VK_NULL_HANDLE)
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600291 return;
292
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600293 err = vkEndCommandBuffer(demo->cmd);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600294 assert(!err);
295
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600296 const VkCmdBuffer cmd_bufs[] = { demo->cmd };
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600297
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600298 err = vkQueueSubmit(demo->queue, 1, cmd_bufs, VK_NULL_HANDLE);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600299 assert(!err);
300
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600301 err = vkQueueWaitIdle(demo->queue);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600302 assert(!err);
303
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600304 vkDestroyObject(demo->cmd);
305 demo->cmd = VK_NULL_HANDLE;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600306}
307
308static void demo_add_mem_refs(
309 struct demo *demo,
Tony Barbour72304ef2015-04-16 15:59:00 -0600310 int num_refs, VkDeviceMemory *mem)
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600311{
Courtney Goeltzenleuchter6b0caf12015-04-16 13:38:46 -0600312 vkQueueAddMemReferences(demo->queue, num_refs, mem);
Courtney Goeltzenleuchter1359b7a2015-04-02 16:25:42 -0600313}
314
315static void demo_remove_mem_refs(
316 struct demo *demo,
Tony Barbour72304ef2015-04-16 15:59:00 -0600317 int num_refs, VkDeviceMemory *mem)
Courtney Goeltzenleuchter1359b7a2015-04-02 16:25:42 -0600318{
Courtney Goeltzenleuchter6b0caf12015-04-16 13:38:46 -0600319 vkQueueRemoveMemReferences(demo->queue, num_refs, mem);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600320}
321
322static void demo_set_image_layout(
323 struct demo *demo,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600324 VkImage image,
325 VkImageLayout old_image_layout,
326 VkImageLayout new_image_layout)
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600327{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600328 VkResult err;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600329
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600330 if (demo->cmd == VK_NULL_HANDLE) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600331 const VkCmdBufferCreateInfo cmd = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600332 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600333 .pNext = NULL,
334 .queueNodeIndex = demo->graphics_queue_node_index,
335 .flags = 0,
336 };
337
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600338 err = vkCreateCommandBuffer(demo->device, &cmd, &demo->cmd);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600339 assert(!err);
340
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600341 VkCmdBufferBeginInfo cmd_buf_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600342 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600343 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -0600344 .flags = VK_CMD_BUFFER_OPTIMIZE_SMALL_BATCH_BIT |
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600345 VK_CMD_BUFFER_OPTIMIZE_ONE_TIME_SUBMIT_BIT,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600346 };
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600347 err = vkBeginCommandBuffer(demo->cmd, &cmd_buf_info);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600348 }
349
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600350 VkImageMemoryBarrier image_memory_barrier = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600351 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600352 .pNext = NULL,
353 .outputMask = 0,
354 .inputMask = 0,
355 .oldLayout = old_image_layout,
356 .newLayout = new_image_layout,
357 .image = image,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600358 .subresourceRange = { VK_IMAGE_ASPECT_COLOR, 0, 1, 0, 0 }
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600359 };
360
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600361 if (new_image_layout == VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL) {
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600362 /* Make sure anything that was copying from this image has completed */
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -0600363 image_memory_barrier.inputMask = VK_MEMORY_INPUT_TRANSFER_BIT;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600364 }
365
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600366 if (new_image_layout == VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL) {
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600367 /* Make sure any Copy or CPU writes to image are flushed */
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -0600368 image_memory_barrier.outputMask = VK_MEMORY_OUTPUT_CPU_WRITE_BIT | VK_MEMORY_OUTPUT_TRANSFER_BIT;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600369 }
370
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600371 VkImageMemoryBarrier *pmemory_barrier = &image_memory_barrier;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600372
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600373 VkPipeEvent set_events[] = { VK_PIPE_EVENT_TOP_OF_PIPE };
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600374
Tony Barbour72304ef2015-04-16 15:59:00 -0600375 vkCmdPipelineBarrier(demo->cmd, VK_WAIT_EVENT_TOP_OF_PIPE, 1, set_events, 1, (const void **)&pmemory_barrier);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600376}
377
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600378static void demo_draw_build_cmd(struct demo *demo, VkCmdBuffer cmd_buf)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600379{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600380 const VkColorAttachmentBindInfo color_attachment = {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600381 .view = demo->buffers[demo->current_buffer].view,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600382 .layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600383 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600384 const VkDepthStencilBindInfo depth_stencil = {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600385 .view = demo->depth.view,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600386 .layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600387 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600388 const VkClearColor clear_color = {
Courtney Goeltzenleuchter374553c2015-04-03 16:35:32 -0600389 .color.floatColor = { 0.2f, 0.2f, 0.2f, 0.2f },
390 .useRawValue = false,
391 };
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600392 const float clear_depth = 1.0f;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600393 VkImageSubresourceRange clear_range;
394 VkCmdBufferBeginInfo cmd_buf_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600395 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO,
Courtney Goeltzenleuchter53968d82015-04-03 15:25:24 -0600396 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -0600397 .flags = VK_CMD_BUFFER_OPTIMIZE_SMALL_BATCH_BIT |
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600398 VK_CMD_BUFFER_OPTIMIZE_ONE_TIME_SUBMIT_BIT,
Jon Ashburn60ccfbe2014-12-31 17:08:35 -0700399 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600400 VkResult err;
401 VkAttachmentLoadOp load_op = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
402 VkAttachmentStoreOp store_op = VK_ATTACHMENT_STORE_OP_DONT_CARE;
403 const VkFramebufferCreateInfo fb_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600404 .sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO,
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700405 .pNext = NULL,
406 .colorAttachmentCount = 1,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600407 .pColorAttachments = (VkColorAttachmentBindInfo*) &color_attachment,
408 .pDepthStencilAttachment = (VkDepthStencilBindInfo*) &depth_stencil,
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700409 .sampleCount = 1,
Mark Lobodzinskic06b7412015-01-27 13:24:03 -0600410 .width = demo->width,
411 .height = demo->height,
412 .layers = 1,
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700413 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600414 VkRenderPassCreateInfo rp_info;
415 VkRenderPassBegin rp_begin;
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700416
417 memset(&rp_info, 0 , sizeof(rp_info));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600418 err = vkCreateFramebuffer(demo->device, &fb_info, &rp_begin.framebuffer);
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700419 assert(!err);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600420 rp_info.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO;
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700421 rp_info.renderArea.extent.width = demo->width;
422 rp_info.renderArea.extent.height = demo->height;
Courtney Goeltzenleuchter53968d82015-04-03 15:25:24 -0600423 rp_info.colorAttachmentCount = fb_info.colorAttachmentCount;
424 rp_info.pColorFormats = &demo->format;
425 rp_info.pColorLayouts = &color_attachment.layout;
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700426 rp_info.pColorLoadOps = &load_op;
427 rp_info.pColorStoreOps = &store_op;
Courtney Goeltzenleuchter53968d82015-04-03 15:25:24 -0600428 rp_info.pColorLoadClearValues = &clear_color;
Tony Barbour72304ef2015-04-16 15:59:00 -0600429 rp_info.depthStencilFormat = VK_FORMAT_D16_UNORM;
Courtney Goeltzenleuchter53968d82015-04-03 15:25:24 -0600430 rp_info.depthStencilLayout = depth_stencil.layout;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600431 rp_info.depthLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
Courtney Goeltzenleuchter53968d82015-04-03 15:25:24 -0600432 rp_info.depthLoadClearValue = clear_depth;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600433 rp_info.depthStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
434 rp_info.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
Courtney Goeltzenleuchter53968d82015-04-03 15:25:24 -0600435 rp_info.stencilLoadClearValue = 0;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600436 rp_info.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
437 err = vkCreateRenderPass(demo->device, &rp_info, &rp_begin.renderPass);
Jon Ashburn08f6eb92015-01-02 18:24:05 -0700438 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600439
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600440 err = vkBeginCommandBuffer(cmd_buf, &cmd_buf_info);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600441 assert(!err);
442
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600443 vkCmdBindPipeline(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600444 demo->pipeline);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600445 vkCmdBindDescriptorSets(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS,
Cody Northropfb5185a2015-04-16 13:41:56 -0600446 0, 1, &demo->desc_set, 0, NULL);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600447
Tony Barbour72304ef2015-04-16 15:59:00 -0600448 vkCmdBindDynamicStateObject(cmd_buf, VK_STATE_BIND_POINT_VIEWPORT, demo->viewport);
449 vkCmdBindDynamicStateObject(cmd_buf, VK_STATE_BIND_POINT_RASTER, demo->raster);
450 vkCmdBindDynamicStateObject(cmd_buf, VK_STATE_BIND_POINT_COLOR_BLEND,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600451 demo->color_blend);
Tony Barbour72304ef2015-04-16 15:59:00 -0600452 vkCmdBindDynamicStateObject(cmd_buf, VK_STATE_BIND_POINT_DEPTH_STENCIL,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600453 demo->depth_stencil);
454
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600455 vkCmdBeginRenderPass(cmd_buf, &rp_begin);
456 clear_range.aspect = VK_IMAGE_ASPECT_COLOR;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600457 clear_range.baseMipLevel = 0;
458 clear_range.mipLevels = 1;
459 clear_range.baseArraySlice = 0;
460 clear_range.arraySize = 1;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600461 vkCmdClearColorImage(cmd_buf,
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600462 demo->buffers[demo->current_buffer].image,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600463 VK_IMAGE_LAYOUT_CLEAR_OPTIMAL,
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600464 clear_color, 1, &clear_range);
465
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600466 clear_range.aspect = VK_IMAGE_ASPECT_DEPTH;
467 vkCmdClearDepthStencil(cmd_buf, demo->depth.image,
468 VK_IMAGE_LAYOUT_CLEAR_OPTIMAL,
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600469 clear_depth, 0, 1, &clear_range);
470
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600471 vkCmdDraw(cmd_buf, 0, 12 * 3, 0, 1);
472 vkCmdEndRenderPass(cmd_buf, rp_begin.renderPass);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600473
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600474 err = vkEndCommandBuffer(cmd_buf);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600475 assert(!err);
Courtney Goeltzenleuchter94901dc2015-02-25 17:53:18 -0700476
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600477 vkDestroyObject(rp_begin.renderPass);
478 vkDestroyObject(rp_begin.framebuffer);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600479}
480
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600481
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -0600482void demo_update_data_buffer(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600483{
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600484 mat4x4 MVP, Model, VP;
485 int matrixSize = sizeof(MVP);
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600486 uint8_t *pData;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600487 VkResult err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600488
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600489 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600490
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600491 // Rotate 22.5 degrees around the Y axis
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600492 mat4x4_dup(Model, demo->model_matrix);
Piers Daniell735ee532015-02-23 16:23:13 -0700493 mat4x4_rotate(demo->model_matrix, Model, 0.0f, 1.0f, 0.0f, (float)degreesToRadians(demo->spin_angle));
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600494 mat4x4_mul(MVP, VP, demo->model_matrix);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600495
Jon Ashburnae7c21c2015-01-19 15:00:26 -0700496 assert(demo->uniform_data.num_mem == 1);
Tony Barbour3213c752015-04-16 19:09:28 -0600497 err = vkMapMemory(demo->uniform_data.mem[0], 0, 0, 0, (void **) &pData);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600498 assert(!err);
499
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600500 memcpy(pData, (const void*) &MVP[0][0], matrixSize);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600501
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600502 err = vkUnmapMemory(demo->uniform_data.mem[0]);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600503 assert(!err);
504}
505
506static void demo_draw(struct demo *demo)
507{
Chia-I Wucbb564e2015-04-16 22:02:10 +0800508 const VkPresentInfoWSI present = {
509 .sType = VK_STRUCTURE_TYPE_PRESENT_INFO_WSI,
510 .pNext = NULL,
511 .image = demo->buffers[demo->current_buffer].image,
512 .flipInterval = 0,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600513 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600514 VkResult err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600515
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600516 err = vkQueueSubmit(demo->queue, 1, &demo->buffers[demo->current_buffer].cmd,
517 VK_NULL_HANDLE);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600518 assert(!err);
519
Chia-I Wucbb564e2015-04-16 22:02:10 +0800520 err = vkQueuePresentWSI(demo->queue, &present);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600521 assert(!err);
522
523 demo->current_buffer = (demo->current_buffer + 1) % DEMO_BUFFER_COUNT;
Chia-I Wucbb564e2015-04-16 22:02:10 +0800524
525 err = vkQueueWaitIdle(demo->queue);
526 assert(err == VK_SUCCESS);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600527}
528
529static void demo_prepare_buffers(struct demo *demo)
530{
Chia-I Wucbb564e2015-04-16 22:02:10 +0800531 const VkSwapChainCreateInfoWSI swap_chain = {
532 .sType = VK_STRUCTURE_TYPE_SWAP_CHAIN_CREATE_INFO_WSI,
533 .pNext = NULL,
534 .pNativeWindowSystemHandle = demo->connection,
535 .pNativeWindowHandle = (void *) (intptr_t) demo->window,
536 .imageCount = DEMO_BUFFER_COUNT,
537 .imageFormat = demo->format,
538 .imageExtent = {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600539 .width = demo->width,
540 .height = demo->height,
541 },
Chia-I Wucbb564e2015-04-16 22:02:10 +0800542 .imageArraySize = 1,
543 .imageUsageFlags = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600544 };
Chia-I Wucbb564e2015-04-16 22:02:10 +0800545 VkSwapChainImageInfoWSI images[DEMO_BUFFER_COUNT];
546 size_t images_size = sizeof(images);
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600547 VkResult err;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600548 uint32_t i;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600549
Chia-I Wucbb564e2015-04-16 22:02:10 +0800550 err = vkCreateSwapChainWSI(demo->device, &swap_chain, &demo->swap_chain);
551 assert(!err);
552
553 err = vkGetSwapChainInfoWSI(demo->swap_chain,
554 VK_SWAP_CHAIN_INFO_TYPE_PERSISTENT_IMAGES_WSI,
555 &images_size, images);
556 assert(!err && images_size == sizeof(images));
557
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600558 for (i = 0; i < DEMO_BUFFER_COUNT; i++) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600559 VkColorAttachmentViewCreateInfo color_attachment_view = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600560 .sType = VK_STRUCTURE_TYPE_COLOR_ATTACHMENT_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600561 .pNext = NULL,
562 .format = demo->format,
563 .mipLevel = 0,
564 .baseArraySlice = 0,
565 .arraySize = 1,
566 };
567
Chia-I Wucbb564e2015-04-16 22:02:10 +0800568 demo->buffers[i].image = images[i].image;
569 demo->buffers[i].mem = images[i].memory;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600570
Courtney Goeltzenleuchter2c2fbbf2015-04-07 17:13:38 -0600571 demo_add_mem_refs(demo, 1, &demo->buffers[i].mem);
Courtney Goeltzenleuchter1359b7a2015-04-02 16:25:42 -0600572
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600573 demo_set_image_layout(demo, demo->buffers[i].image,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600574 VK_IMAGE_LAYOUT_UNDEFINED,
575 VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600576
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600577 color_attachment_view.image = demo->buffers[i].image;
578
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600579 err = vkCreateColorAttachmentView(demo->device,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600580 &color_attachment_view, &demo->buffers[i].view);
581 assert(!err);
582 }
583}
584
585static void demo_prepare_depth(struct demo *demo)
586{
Tony Barbour72304ef2015-04-16 15:59:00 -0600587 const VkFormat depth_format = VK_FORMAT_D16_UNORM;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600588 const VkImageCreateInfo image = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600589 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600590 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -0600591 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600592 .format = depth_format,
593 .extent = { demo->width, demo->height, 1 },
594 .mipLevels = 1,
595 .arraySize = 1,
596 .samples = 1,
Tony Barbour72304ef2015-04-16 15:59:00 -0600597 .tiling = VK_IMAGE_TILING_OPTIMAL,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600598 .usage = VK_IMAGE_USAGE_DEPTH_STENCIL_BIT,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600599 .flags = 0,
600 };
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -0600601 VkMemoryAllocInfo mem_alloc = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600602 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
Mark Lobodzinskia6907f72015-04-16 08:52:00 -0500603 .pNext = NULL,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600604 .allocationSize = 0,
Tony Barbour72304ef2015-04-16 15:59:00 -0600605 .memProps = VK_MEMORY_PROPERTY_DEVICE_ONLY,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600606 .memPriority = VK_MEMORY_PRIORITY_NORMAL,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600607 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600608 VkDepthStencilViewCreateInfo view = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600609 .sType = VK_STRUCTURE_TYPE_DEPTH_STENCIL_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600610 .pNext = NULL,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600611 .image = VK_NULL_HANDLE,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600612 .mipLevel = 0,
613 .baseArraySlice = 0,
614 .arraySize = 1,
615 .flags = 0,
616 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600617 VkMemoryRequirements *mem_reqs;
618 size_t mem_reqs_size = sizeof(VkMemoryRequirements);
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600619 VkResult err;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600620 uint32_t num_allocations = 0;
621 size_t num_alloc_size = sizeof(num_allocations);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600622
623 demo->depth.format = depth_format;
624
625 /* create image */
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600626 err = vkCreateImage(demo->device, &image,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600627 &demo->depth.image);
628 assert(!err);
629
Tony Barbour72304ef2015-04-16 15:59:00 -0600630 err = vkGetObjectInfo(demo->depth.image, VK_OBJECT_INFO_TYPE_MEMORY_ALLOCATION_COUNT, &num_alloc_size, &num_allocations);
Jon Ashburnb2a66652015-01-16 09:37:43 -0700631 assert(!err && num_alloc_size == sizeof(num_allocations));
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600632 mem_reqs = malloc(num_allocations * sizeof(VkMemoryRequirements));
Tony Barbour72304ef2015-04-16 15:59:00 -0600633 demo->depth.mem = malloc(num_allocations * sizeof(VkDeviceMemory));
Jon Ashburnb2a66652015-01-16 09:37:43 -0700634 demo->depth.num_mem = num_allocations;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600635 err = vkGetObjectInfo(demo->depth.image,
Tony Barbour72304ef2015-04-16 15:59:00 -0600636 VK_OBJECT_INFO_TYPE_MEMORY_REQUIREMENTS,
Jon Ashburnb2a66652015-01-16 09:37:43 -0700637 &mem_reqs_size, mem_reqs);
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600638 assert(!err && mem_reqs_size == num_allocations * sizeof(VkMemoryRequirements));
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600639 for (uint32_t i = 0; i < num_allocations; i ++) {
Jon Ashburnb2a66652015-01-16 09:37:43 -0700640 mem_alloc.allocationSize = mem_reqs[i].size;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600641
Jon Ashburnb2a66652015-01-16 09:37:43 -0700642 /* allocate memory */
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600643 err = vkAllocMemory(demo->device, &mem_alloc,
Jon Ashburnb2a66652015-01-16 09:37:43 -0700644 &(demo->depth.mem[i]));
645 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600646
Jon Ashburnb2a66652015-01-16 09:37:43 -0700647 /* bind memory */
Mark Lobodzinskida9b1092015-04-16 11:44:05 -0500648 err = vkQueueBindObjectMemory(demo->queue, demo->depth.image, i,
Jon Ashburnb2a66652015-01-16 09:37:43 -0700649 demo->depth.mem[i], 0);
650 assert(!err);
651 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600652
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600653 demo_set_image_layout(demo, demo->depth.image,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600654 VK_IMAGE_LAYOUT_UNDEFINED,
655 VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600656
Courtney Goeltzenleuchter2c2fbbf2015-04-07 17:13:38 -0600657 demo_add_mem_refs(demo, demo->depth.num_mem, demo->depth.mem);
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600658
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600659 /* create image view */
660 view.image = demo->depth.image;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600661 err = vkCreateDepthStencilView(demo->device, &view,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600662 &demo->depth.view);
663 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600664}
665
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600666/** loadTexture
667 * loads a png file into an memory object, using cstdio , libpng.
668 *
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600669 * \param demo : Needed to access VK calls
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600670 * \param filename : the png file to be loaded
671 * \param width : width of png, to be updated as a side effect of this function
672 * \param height : height of png, to be updated as a side effect of this function
673 *
674 * \return bool : an opengl texture id. true if successful?,
675 * should be validated by the client of this function.
676 *
677 * Source: http://en.wikibooks.org/wiki/OpenGL_Programming/Intermediate/Textures
678 * Modified to copy image to memory
679 *
680 */
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700681bool loadTexture(const char *filename, uint8_t *rgba_data,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600682 VkSubresourceLayout *layout,
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600683 int32_t *width, int32_t *height)
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600684{
685 //header for testing if it is a png
686 png_byte header[8];
Ian Elliott1e42dff2015-02-13 14:29:21 -0700687 int is_png, bit_depth, color_type,rowbytes;
688 png_uint_32 i, twidth, theight;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600689 png_structp png_ptr;
690 png_infop info_ptr, end_info;
691 png_byte *image_data;
692 png_bytep *row_pointers;
693
694 //open file as binary
695 FILE *fp = fopen(filename, "rb");
696 if (!fp) {
697 return false;
698 }
699
700 //read the header
701 fread(header, 1, 8, fp);
702
703 //test if png
704 is_png = !png_sig_cmp(header, 0, 8);
705 if (!is_png) {
706 fclose(fp);
707 return false;
708 }
709
710 //create png struct
711 png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL,
712 NULL, NULL);
713 if (!png_ptr) {
714 fclose(fp);
715 return (false);
716 }
717
718 //create png info struct
719 info_ptr = png_create_info_struct(png_ptr);
720 if (!info_ptr) {
721 png_destroy_read_struct(&png_ptr, (png_infopp) NULL, (png_infopp) NULL);
722 fclose(fp);
723 return (false);
724 }
725
726 //create png info struct
727 end_info = png_create_info_struct(png_ptr);
728 if (!end_info) {
729 png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp) NULL);
730 fclose(fp);
731 return (false);
732 }
733
734 //png error stuff, not sure libpng man suggests this.
735 if (setjmp(png_jmpbuf(png_ptr))) {
736 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
737 fclose(fp);
738 return (false);
739 }
740
741 //init png reading
742 png_init_io(png_ptr, fp);
743
744 //let libpng know you already read the first 8 bytes
745 png_set_sig_bytes(png_ptr, 8);
746
747 // read all the info up to the image data
748 png_read_info(png_ptr, info_ptr);
749
750 // get info about png
751 png_get_IHDR(png_ptr, info_ptr, &twidth, &theight, &bit_depth, &color_type,
752 NULL, NULL, NULL);
753
754 //update width and height based on png info
755 *width = twidth;
756 *height = theight;
757
758 // Require that incoming texture be 8bits per color component
759 // and 4 components (RGBA).
760 if (png_get_bit_depth(png_ptr, info_ptr) != 8 ||
761 png_get_channels(png_ptr, info_ptr) != 4) {
762 return false;
763 }
764
765 if (rgba_data == NULL) {
766 // If data pointer is null, we just want the width & height
767 // clean up memory and close stuff
768 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
769 fclose(fp);
770
771 return true;
772 }
773
774 // Update the png info struct.
775 png_read_update_info(png_ptr, info_ptr);
776
777 // Row size in bytes.
778 rowbytes = png_get_rowbytes(png_ptr, info_ptr);
779
780 // Allocate the image_data as a big block, to be given to opengl
781 image_data = (png_byte *)malloc(rowbytes * theight * sizeof(png_byte));
782 if (!image_data) {
783 //clean up memory and close stuff
784 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
785 fclose(fp);
786 return false;
787 }
788
789 // row_pointers is for pointing to image_data for reading the png with libpng
790 row_pointers = (png_bytep *)malloc(theight * sizeof(png_bytep));
791 if (!row_pointers) {
792 //clean up memory and close stuff
793 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
794 // delete[] image_data;
795 fclose(fp);
796 return false;
797 }
798 // set the individual row_pointers to point at the correct offsets of image_data
799 for (i = 0; i < theight; ++i)
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700800 row_pointers[theight - 1 - i] = rgba_data + i * layout->rowPitch;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600801
802 // read the png into image_data through row_pointers
803 png_read_image(png_ptr, row_pointers);
804
805 // clean up memory and close stuff
806 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
807 free(row_pointers);
808 free(image_data);
809 fclose(fp);
810
811 return true;
812}
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600813
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700814static void demo_prepare_texture_image(struct demo *demo,
815 const char *filename,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600816 struct texture_object *tex_obj,
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600817 VkImageTiling tiling,
818 VkFlags mem_props)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600819{
Tony Barbour72304ef2015-04-16 15:59:00 -0600820 const VkFormat tex_format = VK_FORMAT_B8G8R8A8_UNORM;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600821 int32_t tex_width;
822 int32_t tex_height;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600823 VkResult err;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700824
825 err = loadTexture(filename, NULL, NULL, &tex_width, &tex_height);
826 assert(err);
827
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600828 tex_obj->tex_width = tex_width;
829 tex_obj->tex_height = tex_height;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700830
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600831 const VkImageCreateInfo image_create_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600832 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700833 .pNext = NULL,
Tony Barbour72304ef2015-04-16 15:59:00 -0600834 .imageType = VK_IMAGE_TYPE_2D,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700835 .format = tex_format,
836 .extent = { tex_width, tex_height, 1 },
837 .mipLevels = 1,
838 .arraySize = 1,
839 .samples = 1,
840 .tiling = tiling,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600841 .usage = VK_IMAGE_USAGE_TRANSFER_SOURCE_BIT,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700842 .flags = 0,
843 };
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -0600844 VkMemoryAllocInfo mem_alloc = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600845 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
Mark Lobodzinskia6907f72015-04-16 08:52:00 -0500846 .pNext = NULL,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700847 .allocationSize = 0,
848 .memProps = mem_props,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600849 .memPriority = VK_MEMORY_PRIORITY_NORMAL,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700850 };
851
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600852 VkMemoryRequirements *mem_reqs;
853 size_t mem_reqs_size = sizeof(VkMemoryRequirements);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700854 uint32_t num_allocations = 0;
855 size_t num_alloc_size = sizeof(num_allocations);
856
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600857 err = vkCreateImage(demo->device, &image_create_info,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600858 &tex_obj->image);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700859 assert(!err);
860
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600861 err = vkGetObjectInfo(tex_obj->image,
Tony Barbour72304ef2015-04-16 15:59:00 -0600862 VK_OBJECT_INFO_TYPE_MEMORY_ALLOCATION_COUNT,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700863 &num_alloc_size, &num_allocations);
864 assert(!err && num_alloc_size == sizeof(num_allocations));
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600865 mem_reqs = malloc(num_allocations * sizeof(VkMemoryRequirements));
Tony Barbour72304ef2015-04-16 15:59:00 -0600866 tex_obj->mem = malloc(num_allocations * sizeof(VkDeviceMemory));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600867 err = vkGetObjectInfo(tex_obj->image,
Tony Barbour72304ef2015-04-16 15:59:00 -0600868 VK_OBJECT_INFO_TYPE_MEMORY_REQUIREMENTS,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700869 &mem_reqs_size, mem_reqs);
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600870 assert(!err && mem_reqs_size == num_allocations * sizeof(VkMemoryRequirements));
Tony Barbour72304ef2015-04-16 15:59:00 -0600871 mem_alloc.memProps = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700872 for (uint32_t j = 0; j < num_allocations; j ++) {
Piers Daniell735ee532015-02-23 16:23:13 -0700873 mem_alloc.allocationSize = mem_reqs[j].size;
874
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700875 /* allocate memory */
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600876 err = vkAllocMemory(demo->device, &mem_alloc,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600877 &(tex_obj->mem[j]));
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700878 assert(!err);
879
880 /* bind memory */
Mark Lobodzinskida9b1092015-04-16 11:44:05 -0500881 err = vkQueueBindObjectMemory(demo->queue, tex_obj->image, j, tex_obj->mem[j], 0);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700882 assert(!err);
883 }
884 free(mem_reqs);
885 mem_reqs = NULL;
886
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600887 tex_obj->num_mem = num_allocations;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700888
Tony Barbour72304ef2015-04-16 15:59:00 -0600889 if (mem_props & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) {
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600890 const VkImageSubresource subres = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600891 .aspect = VK_IMAGE_ASPECT_COLOR,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700892 .mipLevel = 0,
893 .arraySlice = 0,
894 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600895 VkSubresourceLayout layout;
896 size_t layout_size = sizeof(VkSubresourceLayout);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700897 void *data;
898
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600899 err = vkGetImageSubresourceInfo(tex_obj->image, &subres,
Tony Barbour72304ef2015-04-16 15:59:00 -0600900 VK_SUBRESOURCE_INFO_TYPE_LAYOUT,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700901 &layout_size, &layout);
902 assert(!err && layout_size == sizeof(layout));
903 /* Linear texture must be within a single memory object */
904 assert(num_allocations == 1);
905
Tony Barbour3213c752015-04-16 19:09:28 -0600906 err = vkMapMemory(tex_obj->mem[0], 0, 0, 0, &data);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700907 assert(!err);
908
909 if (!loadTexture(filename, data, &layout, &tex_width, &tex_height)) {
910 fprintf(stderr, "Error loading texture: %s\n", filename);
911 }
912
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600913 err = vkUnmapMemory(tex_obj->mem[0]);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700914 assert(!err);
915 }
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600916
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600917 tex_obj->imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600918 demo_set_image_layout(demo, tex_obj->image,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600919 VK_IMAGE_LAYOUT_UNDEFINED,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600920 tex_obj->imageLayout);
921 /* 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 -0700922}
923
Mark Lobodzinskida9b1092015-04-16 11:44:05 -0500924static void demo_destroy_texture_image(struct demo *demo, struct texture_object *tex_objs)
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700925{
926 /* clean up staging resources */
927 for (uint32_t j = 0; j < tex_objs->num_mem; j ++) {
Mark Lobodzinskida9b1092015-04-16 11:44:05 -0500928 vkQueueBindObjectMemory(demo->queue, tex_objs->image, j, VK_NULL_HANDLE, 0);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600929 vkFreeMemory(tex_objs->mem[j]);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700930 }
931
932 free(tex_objs->mem);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600933 vkDestroyObject(tex_objs->image);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700934}
935
936static void demo_prepare_textures(struct demo *demo)
937{
Tony Barbour72304ef2015-04-16 15:59:00 -0600938 const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600939 VkFormatProperties props;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700940 size_t size = sizeof(props);
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600941 VkResult err;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -0600942 uint32_t i;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600943
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600944 err = vkGetFormatInfo(demo->device, tex_format,
Tony Barbour72304ef2015-04-16 15:59:00 -0600945 VK_FORMAT_INFO_TYPE_PROPERTIES,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700946 &size, &props);
947 assert(!err);
948
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600949 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700950
Tony Barbour72304ef2015-04-16 15:59:00 -0600951 if (props.linearTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT && !demo->use_staging_buffer) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700952 /* Device can texture using linear textures */
953 demo_prepare_texture_image(demo, tex_files[i], &demo->textures[i],
Tony Barbour72304ef2015-04-16 15:59:00 -0600954 VK_IMAGE_TILING_LINEAR, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
955 } else if (props.optimalTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700956 /* Must use staging buffer to copy linear texture to optimized */
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600957 struct texture_object staging_texture;
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700958
959 memset(&staging_texture, 0, sizeof(staging_texture));
960 demo_prepare_texture_image(demo, tex_files[i], &staging_texture,
Tony Barbour72304ef2015-04-16 15:59:00 -0600961 VK_IMAGE_TILING_LINEAR, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700962
963 demo_prepare_texture_image(demo, tex_files[i], &demo->textures[i],
Tony Barbour72304ef2015-04-16 15:59:00 -0600964 VK_IMAGE_TILING_OPTIMAL, VK_MEMORY_PROPERTY_DEVICE_ONLY);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700965
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600966 demo_set_image_layout(demo, staging_texture.image,
967 staging_texture.imageLayout,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600968 VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700969
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600970 demo_set_image_layout(demo, demo->textures[i].image,
971 demo->textures[i].imageLayout,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600972 VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700973
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -0600974 VkImageCopy copy_region = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600975 .srcSubresource = { VK_IMAGE_ASPECT_COLOR, 0, 0 },
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700976 .srcOffset = { 0, 0, 0 },
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600977 .destSubresource = { VK_IMAGE_ASPECT_COLOR, 0, 0 },
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700978 .destOffset = { 0, 0, 0 },
979 .extent = { staging_texture.tex_width, staging_texture.tex_height, 1 },
980 };
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600981 vkCmdCopyImage(demo->cmd,
982 staging_texture.image, VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL,
983 demo->textures[i].image, VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL,
Courtney Goeltzenleuchter8e89a312015-03-25 11:25:10 -0600984 1, &copy_region);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700985
Courtney Goeltzenleuchter2c2fbbf2015-04-07 17:13:38 -0600986 demo_add_mem_refs(demo, staging_texture.num_mem, staging_texture.mem);
987 demo_add_mem_refs(demo, demo->textures[i].num_mem, demo->textures[i].mem);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700988
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600989 demo_set_image_layout(demo, demo->textures[i].image,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -0600990 VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600991 demo->textures[i].imageLayout);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700992
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -0600993 demo_flush_init_cmd(demo);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700994
Mark Lobodzinskida9b1092015-04-16 11:44:05 -0500995 demo_destroy_texture_image(demo, &staging_texture);
Courtney Goeltzenleuchter1359b7a2015-04-02 16:25:42 -0600996 demo_remove_mem_refs(demo, staging_texture.num_mem, staging_texture.mem);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700997 } else {
Tony Barbour72304ef2015-04-16 15:59:00 -0600998 /* Can't support VK_FORMAT_B8G8R8A8_UNORM !? */
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -0700999 assert(!"No support for tB8G8R8A8_UNORM as texture image format");
1000 }
1001
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001002 const VkSamplerCreateInfo sampler = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001003 .sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001004 .pNext = NULL,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001005 .magFilter = VK_TEX_FILTER_NEAREST,
1006 .minFilter = VK_TEX_FILTER_NEAREST,
Tony Barbour72304ef2015-04-16 15:59:00 -06001007 .mipMode = VK_TEX_MIPMAP_MODE_BASE,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001008 .addressU = VK_TEX_ADDRESS_CLAMP,
1009 .addressV = VK_TEX_ADDRESS_CLAMP,
1010 .addressW = VK_TEX_ADDRESS_CLAMP,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001011 .mipLodBias = 0.0f,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001012 .maxAnisotropy = 1,
Tony Barbour72304ef2015-04-16 15:59:00 -06001013 .compareOp = VK_COMPARE_OP_NEVER,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001014 .minLod = 0.0f,
1015 .maxLod = 0.0f,
Tony Barbour72304ef2015-04-16 15:59:00 -06001016 .borderColor = VK_BORDER_COLOR_OPAQUE_WHITE,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001017 };
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001018
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001019 VkImageViewCreateInfo view = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001020 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001021 .pNext = NULL,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001022 .image = VK_NULL_HANDLE,
Tony Barbour72304ef2015-04-16 15:59:00 -06001023 .viewType = VK_IMAGE_VIEW_TYPE_2D,
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001024 .format = tex_format,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001025 .channels = { VK_CHANNEL_SWIZZLE_R,
1026 VK_CHANNEL_SWIZZLE_G,
1027 VK_CHANNEL_SWIZZLE_B,
1028 VK_CHANNEL_SWIZZLE_A, },
1029 .subresourceRange = { VK_IMAGE_ASPECT_COLOR, 0, 1, 0, 1 },
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001030 .minLod = 0.0f,
1031 };
Jon Ashburnb2a66652015-01-16 09:37:43 -07001032
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001033 /* create sampler */
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001034 err = vkCreateSampler(demo->device, &sampler,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001035 &demo->textures[i].sampler);
1036 assert(!err);
1037
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001038 /* create image view */
1039 view.image = demo->textures[i].image;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001040 err = vkCreateImageView(demo->device, &view,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001041 &demo->textures[i].view);
1042 assert(!err);
1043 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001044}
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001045
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001046void demo_prepare_cube_data_buffer(struct demo *demo)
1047{
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -06001048 VkBufferCreateInfo buf_info;
1049 VkBufferViewCreateInfo view_info;
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -06001050 VkMemoryAllocInfo alloc_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001051 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
Mark Lobodzinskia6907f72015-04-16 08:52:00 -05001052 .pNext = NULL,
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001053 .allocationSize = 0,
Tony Barbour72304ef2015-04-16 15:59:00 -06001054 .memProps = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001055 .memPriority = VK_MEMORY_PRIORITY_NORMAL,
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001056 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001057 VkMemoryRequirements *mem_reqs;
1058 size_t mem_reqs_size = sizeof(VkMemoryRequirements);
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001059 uint32_t num_allocations = 0;
1060 size_t num_alloc_size = sizeof(num_allocations);
1061 uint8_t *pData;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001062 int i;
1063 mat4x4 MVP, VP;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001064 VkResult err;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001065 struct vktexcube_vs_uniform data;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001066
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001067 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001068 mat4x4_mul(MVP, VP, demo->model_matrix);
1069 memcpy(data.mvp, MVP, sizeof(MVP));
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001070// dumpMatrix("MVP", MVP);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001071
1072 for (i=0; i<12*3; i++) {
1073 data.position[i][0] = g_vertex_buffer_data[i*3];
1074 data.position[i][1] = g_vertex_buffer_data[i*3+1];
1075 data.position[i][2] = g_vertex_buffer_data[i*3+2];
1076 data.position[i][3] = 1.0f;
1077 data.attr[i][0] = g_uv_buffer_data[2*i];
1078 data.attr[i][1] = g_uv_buffer_data[2*i + 1];
1079 data.attr[i][2] = 0;
1080 data.attr[i][3] = 0;
1081 }
1082
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001083 memset(&buf_info, 0, sizeof(buf_info));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001084 buf_info.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001085 buf_info.size = sizeof(data);
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -06001086 buf_info.usage = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001087 err = vkCreateBuffer(demo->device, &buf_info, &demo->uniform_data.buf);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001088 assert(!err);
1089
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001090 err = vkGetObjectInfo(demo->uniform_data.buf,
Tony Barbour72304ef2015-04-16 15:59:00 -06001091 VK_OBJECT_INFO_TYPE_MEMORY_ALLOCATION_COUNT,
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001092 &num_alloc_size, &num_allocations);
1093 assert(!err && num_alloc_size == sizeof(num_allocations));
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001094 mem_reqs = malloc(num_allocations * sizeof(VkMemoryRequirements));
Tony Barbour72304ef2015-04-16 15:59:00 -06001095 demo->uniform_data.mem = malloc(num_allocations * sizeof(VkDeviceMemory));
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001096 demo->uniform_data.num_mem = num_allocations;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001097 err = vkGetObjectInfo(demo->uniform_data.buf,
Tony Barbour72304ef2015-04-16 15:59:00 -06001098 VK_OBJECT_INFO_TYPE_MEMORY_REQUIREMENTS,
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001099 &mem_reqs_size, mem_reqs);
1100 assert(!err && mem_reqs_size == num_allocations * sizeof(*mem_reqs));
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001101 for (uint32_t i = 0; i < num_allocations; i ++) {
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001102 alloc_info.allocationSize = mem_reqs[i].size;
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001103
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001104 err = vkAllocMemory(demo->device, &alloc_info, &(demo->uniform_data.mem[i]));
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001105 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001106
Tony Barbour3213c752015-04-16 19:09:28 -06001107 err = vkMapMemory(demo->uniform_data.mem[i], 0, 0, 0, (void **) &pData);
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001108 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001109
Piers Daniell735ee532015-02-23 16:23:13 -07001110 memcpy(pData, &data, (size_t)alloc_info.allocationSize);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001111
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001112 err = vkUnmapMemory(demo->uniform_data.mem[i]);
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001113 assert(!err);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001114
Mark Lobodzinskida9b1092015-04-16 11:44:05 -05001115 err = vkQueueBindObjectMemory(demo->queue, demo->uniform_data.buf, i,
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001116 demo->uniform_data.mem[i], 0);
1117 assert(!err);
1118 }
Courtney Goeltzenleuchter2c2fbbf2015-04-07 17:13:38 -06001119 demo_add_mem_refs(demo, demo->uniform_data.num_mem, demo->uniform_data.mem);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001120
1121 memset(&view_info, 0, sizeof(view_info));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001122 view_info.sType = VK_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO;
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001123 view_info.buffer = demo->uniform_data.buf;
Tony Barbour72304ef2015-04-16 15:59:00 -06001124 view_info.viewType = VK_BUFFER_VIEW_TYPE_RAW;
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001125 view_info.offset = 0;
1126 view_info.range = sizeof(data);
1127
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001128 err = vkCreateBufferView(demo->device, &view_info, &demo->uniform_data.view);
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001129 assert(!err);
1130
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001131 demo->uniform_data.attach.sType = VK_STRUCTURE_TYPE_BUFFER_VIEW_ATTACH_INFO;
Chia-I Wu8ea21c72015-01-01 07:55:04 +08001132 demo->uniform_data.attach.view = demo->uniform_data.view;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001133}
1134
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001135static void demo_prepare_descriptor_layout(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001136{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001137 const VkDescriptorSetLayoutBinding layout_bindings[2] = {
Chia-I Wua2aa8632015-03-26 15:04:41 +08001138 [0] = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001139 .descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
Chia-I Wua2aa8632015-03-26 15:04:41 +08001140 .count = 1,
Tony Barbour72304ef2015-04-16 15:59:00 -06001141 .stageFlags = VK_SHADER_STAGE_VERTEX_BIT,
Chia-I Wu91e8e212015-03-27 12:56:09 +08001142 .pImmutableSamplers = NULL,
Chia-I Wua2aa8632015-03-26 15:04:41 +08001143 },
1144 [1] = {
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -06001145 .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
Chia-I Wua2aa8632015-03-26 15:04:41 +08001146 .count = DEMO_TEXTURE_COUNT,
Tony Barbour72304ef2015-04-16 15:59:00 -06001147 .stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT,
Chia-I Wu91e8e212015-03-27 12:56:09 +08001148 .pImmutableSamplers = NULL,
Chia-I Wua2aa8632015-03-26 15:04:41 +08001149 },
1150 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001151 const VkDescriptorSetLayoutCreateInfo descriptor_layout = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001152 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001153 .pNext = NULL,
Chia-I Wua2aa8632015-03-26 15:04:41 +08001154 .count = 2,
1155 .pBinding = layout_bindings,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001156 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001157 VkResult err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001158
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001159 err = vkCreateDescriptorSetLayout(demo->device,
Chia-I Wub58c24a2015-03-26 15:27:55 +08001160 &descriptor_layout, &demo->desc_layout);
1161 assert(!err);
1162
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001163 const VkPipelineLayoutCreateInfo pPipelineLayoutCreateInfo = {
1164 .sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
1165 .pNext = NULL,
1166 .descriptorSetCount = 1,
1167 .pSetLayouts = &demo->desc_layout,
1168 };
1169
1170 err = vkCreatePipelineLayout(demo->device,
1171 &pPipelineLayoutCreateInfo,
1172 &demo->pipeline_layout);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001173 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001174}
1175
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001176static VkShader demo_prepare_shader(struct demo *demo,
Tony Barbour72304ef2015-04-16 15:59:00 -06001177 VkShaderStage stage,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001178 const void *code,
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001179 size_t size)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001180{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001181 VkShaderCreateInfo createInfo;
1182 VkShader shader;
1183 VkResult err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001184
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001185
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001186 createInfo.sType = VK_STRUCTURE_TYPE_SHADER_CREATE_INFO;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001187 createInfo.pNext = NULL;
1188
Cody Northrop8b12a1f2015-03-17 15:55:58 -06001189#ifdef EXTERNAL_SPV
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001190 createInfo.codeSize = size;
1191 createInfo.pCode = code;
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001192 createInfo.flags = 0;
1193
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001194 err = vkCreateShader(demo->device, &createInfo, &shader);
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001195 if (err) {
1196 free((void *) createInfo.pCode);
1197 }
1198#else
Cody Northrop8b12a1f2015-03-17 15:55:58 -06001199 // Create fake SPV structure to feed GLSL
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001200 // to the driver "under the covers"
1201 createInfo.codeSize = 3 * sizeof(uint32_t) + size + 1;
1202 createInfo.pCode = malloc(createInfo.codeSize);
1203 createInfo.flags = 0;
1204
Tony Barbour72304ef2015-04-16 15:59:00 -06001205 /* try version 0 first: VkShaderStage followed by GLSL */
Cody Northrop8b12a1f2015-03-17 15:55:58 -06001206 ((uint32_t *) createInfo.pCode)[0] = ICD_SPV_MAGIC;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001207 ((uint32_t *) createInfo.pCode)[1] = 0;
1208 ((uint32_t *) createInfo.pCode)[2] = stage;
1209 memcpy(((uint32_t *) createInfo.pCode + 3), code, size + 1);
1210
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001211 err = vkCreateShader(demo->device, &createInfo, &shader);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001212 if (err) {
1213 free((void *) createInfo.pCode);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001214 return NULL;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001215 }
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001216#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001217
1218 return shader;
1219}
1220
Cody Northrop8b12a1f2015-03-17 15:55:58 -06001221char *demo_read_spv(const char *filename, size_t *psize)
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001222{
1223 long int size;
1224 void *shader_code;
1225
1226 FILE *fp = fopen(filename, "rb");
1227 if (!fp) return NULL;
1228
1229 fseek(fp, 0L, SEEK_END);
1230 size = ftell(fp);
1231
1232 fseek(fp, 0L, SEEK_SET);
1233
1234 shader_code = malloc(size);
1235 fread(shader_code, size, 1, fp);
1236
1237 *psize = size;
1238
1239 return shader_code;
1240}
1241
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001242static VkShader demo_prepare_vs(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001243{
Cody Northrop8b12a1f2015-03-17 15:55:58 -06001244#ifdef EXTERNAL_SPV
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001245 void *vertShaderCode;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001246 size_t size;
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001247
Cody Northrop8b12a1f2015-03-17 15:55:58 -06001248 vertShaderCode = demo_read_spv("cube-vert.spv", &size);
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001249
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001250 return demo_prepare_shader(demo, VK_SHADER_STAGE_VERTEX,
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001251 vertShaderCode, size);
1252#else
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001253 static const char *vertShaderText =
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001254 "#version 140\n"
1255 "#extension GL_ARB_separate_shader_objects : enable\n"
1256 "#extension GL_ARB_shading_language_420pack : enable\n"
1257 "\n"
1258 "layout(binding = 0) uniform buf {\n"
1259 " mat4 MVP;\n"
1260 " vec4 position[12*3];\n"
1261 " vec4 attr[12*3];\n"
1262 "} ubuf;\n"
1263 "\n"
1264 "layout (location = 0) out vec4 texcoord;\n"
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001265 "\n"
1266 "void main() \n"
1267 "{\n"
1268 " texcoord = ubuf.attr[gl_VertexID];\n"
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001269 " gl_Position = ubuf.MVP * ubuf.position[gl_VertexID];\n"
1270 "}\n";
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001271
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001272 return demo_prepare_shader(demo, VK_SHADER_STAGE_VERTEX,
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001273 (const void *) vertShaderText,
1274 strlen(vertShaderText));
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001275#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001276}
1277
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001278static VkShader demo_prepare_fs(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 *fragShaderCode;
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 fragShaderCode = demo_read_spv("cube-frag.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_FRAGMENT,
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001287 fragShaderCode, size);
1288#else
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001289 static const char *fragShaderText =
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"
Courtney Goeltzenleuchter4d1acf12015-02-25 15:13:35 -07001293 "layout (binding = 1) uniform sampler2D tex;\n"
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001294 "\n"
1295 "layout (location = 0) in vec4 texcoord;\n"
1296 "void main() {\n"
1297 " gl_FragColor = texture(tex, texcoord.xy);\n"
1298 "}\n";
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001299
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001300 return demo_prepare_shader(demo, VK_SHADER_STAGE_FRAGMENT,
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001301 (const void *) fragShaderText,
1302 strlen(fragShaderText));
Courtney Goeltzenleuchter97db1c12014-10-30 15:14:16 -06001303#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001304}
1305
1306static void demo_prepare_pipeline(struct demo *demo)
1307{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001308 VkGraphicsPipelineCreateInfo pipeline;
1309 VkPipelineIaStateCreateInfo ia;
1310 VkPipelineRsStateCreateInfo rs;
1311 VkPipelineCbStateCreateInfo cb;
1312 VkPipelineDsStateCreateInfo ds;
1313 VkPipelineShaderStageCreateInfo vs;
1314 VkPipelineShaderStageCreateInfo fs;
1315 VkPipelineVpStateCreateInfo vp;
1316 VkPipelineMsStateCreateInfo ms;
1317 VkResult err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001318
1319 memset(&pipeline, 0, sizeof(pipeline));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001320 pipeline.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001321 pipeline.layout = demo->pipeline_layout;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001322
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001323 memset(&ia, 0, sizeof(ia));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001324 ia.sType = VK_STRUCTURE_TYPE_PIPELINE_IA_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06001325 ia.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001326
1327 memset(&rs, 0, sizeof(rs));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001328 rs.sType = VK_STRUCTURE_TYPE_PIPELINE_RS_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06001329 rs.fillMode = VK_FILL_MODE_SOLID;
1330 rs.cullMode = VK_CULL_MODE_BACK;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001331 rs.frontFace = VK_FRONT_FACE_CCW;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001332
1333 memset(&cb, 0, sizeof(cb));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001334 cb.sType = VK_STRUCTURE_TYPE_PIPELINE_CB_STATE_CREATE_INFO;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001335 VkPipelineCbAttachmentState att_state[1];
Tony Barbour29645d02015-01-16 14:27:35 -07001336 memset(att_state, 0, sizeof(att_state));
1337 att_state[0].format = demo->format;
1338 att_state[0].channelWriteMask = 0xf;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001339 att_state[0].blendEnable = VK_FALSE;
Tony Barbour29645d02015-01-16 14:27:35 -07001340 cb.attachmentCount = 1;
1341 cb.pAttachments = att_state;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001342
Tony Barbour29645d02015-01-16 14:27:35 -07001343 memset(&vp, 0, sizeof(vp));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001344 vp.sType = VK_STRUCTURE_TYPE_PIPELINE_VP_STATE_CREATE_INFO;
Tony Barbour72304ef2015-04-16 15:59:00 -06001345 vp.viewportCount = 1;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001346 vp.clipOrigin = VK_COORDINATE_ORIGIN_LOWER_LEFT;
Tony Barbour29645d02015-01-16 14:27:35 -07001347
1348 memset(&ds, 0, sizeof(ds));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001349 ds.sType = VK_STRUCTURE_TYPE_PIPELINE_DS_STATE_CREATE_INFO;
Tony Barbour29645d02015-01-16 14:27:35 -07001350 ds.format = demo->depth.format;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001351 ds.depthTestEnable = VK_TRUE;
1352 ds.depthWriteEnable = VK_TRUE;
Tony Barbour72304ef2015-04-16 15:59:00 -06001353 ds.depthCompareOp = VK_COMPARE_OP_LESS_EQUAL;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001354 ds.depthBoundsEnable = VK_FALSE;
1355 ds.back.stencilFailOp = VK_STENCIL_OP_KEEP;
1356 ds.back.stencilPassOp = VK_STENCIL_OP_KEEP;
Tony Barbour72304ef2015-04-16 15:59:00 -06001357 ds.back.stencilCompareOp = VK_COMPARE_OP_ALWAYS;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001358 ds.stencilTestEnable = VK_FALSE;
Tony Barbour29645d02015-01-16 14:27:35 -07001359 ds.front = ds.back;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001360
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001361 memset(&vs, 0, sizeof(vs));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001362 vs.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1363 vs.shader.stage = VK_SHADER_STAGE_VERTEX;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001364 vs.shader.shader = demo_prepare_vs(demo);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001365 assert(vs.shader.shader != NULL);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001366
1367 memset(&fs, 0, sizeof(fs));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001368 fs.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1369 fs.shader.stage = VK_SHADER_STAGE_FRAGMENT;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001370 fs.shader.shader = demo_prepare_fs(demo);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001371 assert(fs.shader.shader != NULL);
Tony Barbour29645d02015-01-16 14:27:35 -07001372
1373 memset(&ms, 0, sizeof(ms));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001374 ms.sType = VK_STRUCTURE_TYPE_PIPELINE_MS_STATE_CREATE_INFO;
Tony Barbour29645d02015-01-16 14:27:35 -07001375 ms.sampleMask = 1;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001376 ms.multisampleEnable = VK_FALSE;
Tony Barbour29645d02015-01-16 14:27:35 -07001377 ms.samples = 1;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001378
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001379 pipeline.pNext = (const void *) &ia;
1380 ia.pNext = (const void *) &rs;
1381 rs.pNext = (const void *) &cb;
1382 cb.pNext = (const void *) &ms;
1383 ms.pNext = (const void *) &vp;
1384 vp.pNext = (const void *) &ds;
1385 ds.pNext = (const void *) &vs;
1386 vs.pNext = (const void *) &fs;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001387
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001388 err = vkCreateGraphicsPipeline(demo->device, &pipeline, &demo->pipeline);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001389 assert(!err);
1390
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001391 vkDestroyObject(vs.shader.shader);
1392 vkDestroyObject(fs.shader.shader);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001393}
1394
1395static void demo_prepare_dynamic_states(struct demo *demo)
1396{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001397 VkDynamicVpStateCreateInfo viewport_create;
1398 VkDynamicRsStateCreateInfo raster;
1399 VkDynamicCbStateCreateInfo color_blend;
1400 VkDynamicDsStateCreateInfo depth_stencil;
1401 VkResult err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001402
Tony Barbour29645d02015-01-16 14:27:35 -07001403 memset(&viewport_create, 0, sizeof(viewport_create));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001404 viewport_create.sType = VK_STRUCTURE_TYPE_DYNAMIC_VP_STATE_CREATE_INFO;
Courtney Goeltzenleuchter1710d8d2015-02-11 14:13:34 -07001405 viewport_create.viewportAndScissorCount = 1;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001406 VkViewport viewport;
Piers Daniell735ee532015-02-23 16:23:13 -07001407 memset(&viewport, 0, sizeof(viewport));
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001408 viewport.height = (float) demo->height;
1409 viewport.width = (float) demo->width;
1410 viewport.minDepth = (float) 0.0f;
1411 viewport.maxDepth = (float) 1.0f;
Piers Daniell735ee532015-02-23 16:23:13 -07001412 viewport_create.pViewports = &viewport;
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001413 VkRect scissor;
Piers Daniell735ee532015-02-23 16:23:13 -07001414 memset(&scissor, 0, sizeof(scissor));
Courtney Goeltzenleuchter1710d8d2015-02-11 14:13:34 -07001415 scissor.extent.width = demo->width;
1416 scissor.extent.height = demo->height;
1417 scissor.offset.x = 0;
1418 scissor.offset.y = 0;
Courtney Goeltzenleuchter1710d8d2015-02-11 14:13:34 -07001419 viewport_create.pScissors = &scissor;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001420
1421 memset(&raster, 0, sizeof(raster));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001422 raster.sType = VK_STRUCTURE_TYPE_DYNAMIC_RS_STATE_CREATE_INFO;
Piers Daniell735ee532015-02-23 16:23:13 -07001423 raster.pointSize = 1.0;
1424 raster.lineWidth = 1.0;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001425
1426 memset(&color_blend, 0, sizeof(color_blend));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001427 color_blend.sType = VK_STRUCTURE_TYPE_DYNAMIC_CB_STATE_CREATE_INFO;
Piers Daniell735ee532015-02-23 16:23:13 -07001428 color_blend.blendConst[0] = 1.0f;
1429 color_blend.blendConst[1] = 1.0f;
1430 color_blend.blendConst[2] = 1.0f;
1431 color_blend.blendConst[3] = 1.0f;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001432
1433 memset(&depth_stencil, 0, sizeof(depth_stencil));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001434 depth_stencil.sType = VK_STRUCTURE_TYPE_DYNAMIC_DS_STATE_CREATE_INFO;
Piers Daniell735ee532015-02-23 16:23:13 -07001435 depth_stencil.minDepth = 0.0f;
1436 depth_stencil.maxDepth = 1.0f;
Tony Barbour29645d02015-01-16 14:27:35 -07001437 depth_stencil.stencilBackRef = 0;
1438 depth_stencil.stencilFrontRef = 0;
1439 depth_stencil.stencilReadMask = 0xff;
1440 depth_stencil.stencilWriteMask = 0xff;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001441
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001442 err = vkCreateDynamicViewportState(demo->device, &viewport_create, &demo->viewport);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001443 assert(!err);
1444
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001445 err = vkCreateDynamicRasterState(demo->device, &raster, &demo->raster);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001446 assert(!err);
1447
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001448 err = vkCreateDynamicColorBlendState(demo->device,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001449 &color_blend, &demo->color_blend);
1450 assert(!err);
1451
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001452 err = vkCreateDynamicDepthStencilState(demo->device,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001453 &depth_stencil, &demo->depth_stencil);
1454 assert(!err);
1455}
1456
Chia-I Wu63ea9262015-03-26 13:14:16 +08001457static void demo_prepare_descriptor_pool(struct demo *demo)
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001458{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001459 const VkDescriptorTypeCount type_counts[2] = {
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001460 [0] = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001461 .type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001462 .count = 1,
1463 },
1464 [1] = {
Courtney Goeltzenleuchter42a078d2015-04-15 15:29:59 -06001465 .type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001466 .count = DEMO_TEXTURE_COUNT,
1467 },
1468 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001469 const VkDescriptorPoolCreateInfo descriptor_pool = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001470 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001471 .pNext = NULL,
1472 .count = 2,
1473 .pTypeCount = type_counts,
1474 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001475 VkResult err;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001476
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001477 err = vkCreateDescriptorPool(demo->device,
1478 VK_DESCRIPTOR_POOL_USAGE_ONE_SHOT, 1,
Chia-I Wu63ea9262015-03-26 13:14:16 +08001479 &descriptor_pool, &demo->desc_pool);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001480 assert(!err);
1481}
1482
1483static void demo_prepare_descriptor_set(struct demo *demo)
1484{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001485 VkImageViewAttachInfo view_info[DEMO_TEXTURE_COUNT];
1486 VkSamplerImageViewInfo combined_info[DEMO_TEXTURE_COUNT];
1487 VkUpdateSamplerTextures update_fs;
1488 VkUpdateBuffers update_vs;
Chia-I Wub58c24a2015-03-26 15:27:55 +08001489 const void *update_array[2] = { &update_vs, &update_fs };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001490 VkResult err;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001491 uint32_t count;
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001492 uint32_t i;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001493
1494 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001495 view_info[i].sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_ATTACH_INFO;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001496 view_info[i].pNext = NULL;
1497 view_info[i].view = demo->textures[i].view,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001498 view_info[i].layout = VK_IMAGE_LAYOUT_GENERAL;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001499
Courtney Goeltzenleuchtereb4754f2015-04-09 11:43:10 -06001500 combined_info[i].sampler = demo->textures[i].sampler;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001501 combined_info[i].pImageView = &view_info[i];
1502 }
1503
1504 memset(&update_vs, 0, sizeof(update_vs));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001505 update_vs.sType = VK_STRUCTURE_TYPE_UPDATE_BUFFERS;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001506 update_vs.pNext = &update_fs;
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001507 update_vs.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001508 update_vs.count = 1;
Chia-I Wub58c24a2015-03-26 15:27:55 +08001509 update_vs.pBufferViews = &demo->uniform_data.attach;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001510
1511 memset(&update_fs, 0, sizeof(update_fs));
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001512 update_fs.sType = VK_STRUCTURE_TYPE_UPDATE_SAMPLER_TEXTURES;
Chia-I Wub58c24a2015-03-26 15:27:55 +08001513 update_fs.binding = 1;
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001514 update_fs.count = DEMO_TEXTURE_COUNT;
1515 update_fs.pSamplerImageViews = combined_info;
1516
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001517 err = vkAllocDescriptorSets(demo->desc_pool,
1518 VK_DESCRIPTOR_SET_USAGE_STATIC,
Chia-I Wu87544e72015-02-23 10:41:08 -07001519 1, &demo->desc_layout,
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001520 &demo->desc_set, &count);
1521 assert(!err && count == 1);
1522
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001523 vkBeginDescriptorPoolUpdate(demo->device,
1524 VK_DESCRIPTOR_UPDATE_MODE_FASTEST);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001525
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001526 vkClearDescriptorSets(demo->desc_pool, 1, &demo->desc_set);
1527 vkUpdateDescriptors(demo->desc_set, 2, update_array);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001528
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001529 vkEndDescriptorPoolUpdate(demo->device, demo->buffers[demo->current_buffer].cmd);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001530}
1531
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001532static void demo_prepare(struct demo *demo)
1533{
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001534 const VkCmdBufferCreateInfo cmd = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001535 .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001536 .pNext = NULL,
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07001537 .queueNodeIndex = demo->graphics_queue_node_index,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001538 .flags = 0,
1539 };
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001540 VkResult err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001541
1542 demo_prepare_buffers(demo);
1543 demo_prepare_depth(demo);
1544 demo_prepare_textures(demo);
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001545 demo_prepare_cube_data_buffer(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001546
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001547 demo_prepare_descriptor_layout(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001548 demo_prepare_pipeline(demo);
1549 demo_prepare_dynamic_states(demo);
1550
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001551 for (int i = 0; i < DEMO_BUFFER_COUNT; i++) {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001552 err = vkCreateCommandBuffer(demo->device, &cmd, &demo->buffers[i].cmd);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001553 assert(!err);
1554 }
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001555
Chia-I Wu63ea9262015-03-26 13:14:16 +08001556 demo_prepare_descriptor_pool(demo);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001557 demo_prepare_descriptor_set(demo);
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001558
1559
1560 for (int i = 0; i < DEMO_BUFFER_COUNT; i++) {
1561 demo->current_buffer = i;
1562 demo_draw_build_cmd(demo, demo->buffers[i].cmd);
1563 }
1564
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001565 /*
1566 * Prepare functions above may generate pipeline commands
1567 * that need to be flushed before beginning the render loop.
1568 */
1569 demo_flush_init_cmd(demo);
1570
Courtney Goeltzenleuchterefc58ae2015-02-17 09:48:44 -07001571 demo->current_buffer = 0;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001572}
1573
Ian Elliott639ca472015-04-16 15:23:05 -06001574#ifdef _WIN32
1575static void demo_run(struct demo *demo)
1576{
1577 // Wait for work to finish before updating MVP.
1578 vkDeviceWaitIdle(demo->device);
1579 demo_update_data_buffer(demo);
1580
1581 demo_draw(demo);
1582
1583 // Wait for work to finish before updating MVP.
1584 vkDeviceWaitIdle(demo->device);
1585}
1586
1587// On MS-Windows, make this a global, so it's available to WndProc()
1588struct demo demo;
1589
1590// MS-Windows event handling function:
1591LRESULT CALLBACK WndProc(HWND hWnd,
1592 UINT uMsg,
1593 WPARAM wParam,
1594 LPARAM lParam)
1595{
1596 PAINTSTRUCT paint_struct;
1597 HDC hDC; // Device context
1598 char tmp_str[] = "Test Vulkan Cube Program";
1599
1600 switch(uMsg)
1601 {
1602 case WM_CREATE:
1603 return 0;
1604 case WM_CLOSE:
1605 PostQuitMessage(0);
1606 return 0;
1607 case WM_PAINT:
1608 demo_run(&demo);
1609 return 0;
1610 default:
1611 break;
1612 }
1613 return (DefWindowProc(hWnd, uMsg, wParam, lParam));
1614}
1615
1616static void demo_create_window(struct demo *demo)
1617{
1618 WNDCLASSEX win_class;
1619
1620 // Initialize the window class structure:
1621 win_class.cbSize = sizeof(WNDCLASSEX);
1622 win_class.style = CS_HREDRAW | CS_VREDRAW;
1623 win_class.lpfnWndProc = WndProc;
1624 win_class.cbClsExtra = 0;
1625 win_class.cbWndExtra = 0;
1626 win_class.hInstance = demo->connection; // hInstance
1627 win_class.hIcon = LoadIcon(NULL, IDI_APPLICATION);
1628 win_class.hCursor = LoadCursor(NULL, IDC_ARROW);
1629 win_class.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
1630 win_class.lpszMenuName = NULL;
1631 win_class.lpszClassName = demo->name;
1632 win_class.hIconSm = LoadIcon(NULL, IDI_WINLOGO);
1633 // Register window class:
1634 if (!RegisterClassEx(&win_class)) {
1635 // It didn't work, so try to give a useful error:
1636 printf("Unexpected error trying to start the application!\n");
1637 fflush(stdout);
1638 exit(1);
1639 }
1640 // Create window with the registered class:
1641 demo->window = CreateWindowEx(0,
1642 demo->name, // class name
1643 demo->name, // app name
1644 WS_OVERLAPPEDWINDOW | // window style
1645 WS_VISIBLE |
1646 WS_SYSMENU,
1647 100,100, // x/y coords
1648 demo->width, // width
1649 demo->height, // height
1650 NULL, // handle to parent
1651 NULL, // handle to menu
1652 demo->connection, // hInstance
1653 NULL); // no extra parameters
1654 if (!demo->window) {
1655 // It didn't work, so try to give a useful error:
1656 printf("Cannot create a window in which to draw!\n");
1657 fflush(stdout);
1658 exit(1);
1659 }
1660}
1661#else // _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001662static void demo_handle_event(struct demo *demo,
1663 const xcb_generic_event_t *event)
1664{
Piers Daniell735ee532015-02-23 16:23:13 -07001665 uint8_t event_code = event->response_type & 0x7f;
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07001666 switch (event_code) {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001667 case XCB_EXPOSE:
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07001668 // TODO: Resize window
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001669 break;
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07001670 case XCB_CLIENT_MESSAGE:
1671 if((*(xcb_client_message_event_t*)event).data.data32[0] ==
1672 (*demo->atom_wm_delete_window).atom) {
1673 demo->quit = true;
1674 }
1675 break;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001676 case XCB_KEY_RELEASE:
1677 {
1678 const xcb_key_release_event_t *key =
1679 (const xcb_key_release_event_t *) event;
1680
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001681 switch (key->detail) {
1682 case 0x9: // Escape
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001683 demo->quit = true;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001684 break;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001685 case 0x71: // left arrow key
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001686 demo->spin_angle += demo->spin_increment;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001687 break;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001688 case 0x72: // right arrow key
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001689 demo->spin_angle -= demo->spin_increment;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001690 break;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001691 case 0x41:
1692 demo->pause = !demo->pause;
Piers Daniell735ee532015-02-23 16:23:13 -07001693 break;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001694 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001695 }
1696 break;
1697 default:
1698 break;
1699 }
1700}
1701
1702static void demo_run(struct demo *demo)
1703{
1704 xcb_flush(demo->connection);
1705
1706 while (!demo->quit) {
1707 xcb_generic_event_t *event;
1708
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07001709 if (demo->pause) {
1710 event = xcb_wait_for_event(demo->connection);
1711 } else {
1712 event = xcb_poll_for_event(demo->connection);
1713 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001714 if (event) {
1715 demo_handle_event(demo, event);
1716 free(event);
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07001717 }
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07001718
1719 // Wait for work to finish before updating MVP.
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001720 vkDeviceWaitIdle(demo->device);
Courtney Goeltzenleuchterc465d6c2014-11-18 11:28:09 -07001721 demo_update_data_buffer(demo);
1722
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07001723 demo_draw(demo);
Courtney Goeltzenleuchter1454f3c2014-11-18 11:28:09 -07001724
Courtney Goeltzenleuchter98085552014-11-10 11:13:13 -07001725 // Wait for work to finish before updating MVP.
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001726 vkDeviceWaitIdle(demo->device);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001727 }
1728}
1729
1730static void demo_create_window(struct demo *demo)
1731{
1732 uint32_t value_mask, value_list[32];
1733
1734 demo->window = xcb_generate_id(demo->connection);
1735
1736 value_mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK;
1737 value_list[0] = demo->screen->black_pixel;
1738 value_list[1] = XCB_EVENT_MASK_KEY_RELEASE |
1739 XCB_EVENT_MASK_EXPOSURE;
1740
1741 xcb_create_window(demo->connection,
1742 XCB_COPY_FROM_PARENT,
1743 demo->window, demo->screen->root,
1744 0, 0, demo->width, demo->height, 0,
1745 XCB_WINDOW_CLASS_INPUT_OUTPUT,
1746 demo->screen->root_visual,
1747 value_mask, value_list);
1748
Courtney Goeltzenleuchter98cb2cb2014-11-06 14:27:52 -07001749 /* Magic code that will send notification when window is destroyed */
1750 xcb_intern_atom_cookie_t cookie = xcb_intern_atom(demo->connection, 1, 12,
1751 "WM_PROTOCOLS");
1752 xcb_intern_atom_reply_t* reply = xcb_intern_atom_reply(demo->connection, cookie, 0);
1753
1754 xcb_intern_atom_cookie_t cookie2 = xcb_intern_atom(demo->connection, 0, 16, "WM_DELETE_WINDOW");
1755 demo->atom_wm_delete_window = xcb_intern_atom_reply(demo->connection, cookie2, 0);
1756
1757 xcb_change_property(demo->connection, XCB_PROP_MODE_REPLACE,
1758 demo->window, (*reply).atom, 4, 32, 1,
1759 &(*demo->atom_wm_delete_window).atom);
1760 free(reply);
1761
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001762 xcb_map_window(demo->connection, demo->window);
1763}
Ian Elliott639ca472015-04-16 15:23:05 -06001764#endif // _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001765
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001766static void demo_init_vk(struct demo *demo)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001767{
Tobin Ehlis99239dc2015-04-16 18:04:57 -06001768 VkResult err;
1769 // Extensions to enable
1770 const char *ext_names[] = {
Chia-I Wucbb564e2015-04-16 22:02:10 +08001771 "VK_WSI_LunarG",
Tobin Ehlis99239dc2015-04-16 18:04:57 -06001772 };
1773 size_t extSize = sizeof(uint32_t);
1774 uint32_t extCount = 0;
1775 err = vkGetGlobalExtensionInfo(VK_EXTENSION_INFO_TYPE_COUNT, 0, &extSize, &extCount);
1776 assert(!err);
1777
1778 VkExtensionProperties extProp;
1779 extSize = sizeof(VkExtensionProperties);
1780 bool32_t extFound = 0;
1781 for (uint32_t i = 0; i < extCount; i++) {
1782 err = vkGetGlobalExtensionInfo(VK_EXTENSION_INFO_TYPE_PROPERTIES, i, &extSize, &extProp);
1783 if (!strcmp(ext_names[0], extProp.extName))
1784 extFound = 1;
1785 }
1786 assert(extFound);
Courtney Goeltzenleuchter78351a62015-04-10 08:34:15 -06001787 const VkApplicationInfo app = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001788 .sType = VK_STRUCTURE_TYPE_APPLICATION_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001789 .pNext = NULL,
Chia-I Wub2c81932014-12-27 15:16:07 +08001790 .pAppName = "cube",
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001791 .appVersion = 0,
Chia-I Wub2c81932014-12-27 15:16:07 +08001792 .pEngineName = "cube",
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001793 .engineVersion = 0,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001794 .apiVersion = VK_API_VERSION,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001795 };
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -06001796 const VkInstanceCreateInfo inst_info = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001797 .sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
Jon Ashburnab46b362015-04-04 14:52:07 -06001798 .pNext = NULL,
1799 .pAppInfo = &app,
1800 .pAllocCb = NULL,
Tobin Ehlis99239dc2015-04-16 18:04:57 -06001801 .extensionCount = 1,
1802 .ppEnabledExtensionNames = ext_names,
Jon Ashburnab46b362015-04-04 14:52:07 -06001803 };
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -06001804 const VkDeviceQueueCreateInfo queue = {
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001805 .queueNodeIndex = 0,
1806 .queueCount = 1,
1807 };
Tobin Ehlis7537db92015-04-16 10:04:35 -06001808
Courtney Goeltzenleuchter28c4d5f2015-04-14 18:48:46 -06001809 const VkDeviceCreateInfo device = {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001810 .sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001811 .pNext = NULL,
1812 .queueRecordCount = 1,
1813 .pRequestedQueues = &queue,
Tobin Ehlis99239dc2015-04-16 18:04:57 -06001814 .extensionCount = 1,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001815 .ppEnabledExtensionNames = ext_names,
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001816 .flags = VK_DEVICE_CREATE_VALIDATION_BIT,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001817 };
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001818 uint32_t gpu_count;
1819 uint32_t i;
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07001820 size_t data_size;
1821 uint32_t queue_count;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001822
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001823 err = vkCreateInstance(&inst_info, &demo->inst);
1824 if (err == VK_ERROR_INCOMPATIBLE_DRIVER) {
Ian Elliott3979e282015-04-03 15:24:55 -06001825 printf("Cannot find a compatible Vulkan installable client driver "
1826 "(ICD).\nExiting ...\n");
1827 fflush(stdout);
1828 exit(1);
1829 } else {
1830 assert(!err);
1831 }
Jon Ashburnab46b362015-04-04 14:52:07 -06001832
Jon Ashburn07c0c0c2015-04-15 11:31:12 -06001833 gpu_count = 1;
1834 err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, &demo->gpu);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001835 assert(!err && gpu_count == 1);
1836
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001837 err = vkCreateDevice(demo->gpu, &device, &demo->device);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001838 assert(!err);
1839
Tony Barbour72304ef2015-04-16 15:59:00 -06001840 err = vkGetPhysicalDeviceInfo(demo->gpu, VK_PHYSICAL_DEVICE_INFO_TYPE_PROPERTIES,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001841 &data_size, NULL);
1842 assert(!err);
1843
Tony Barbour72304ef2015-04-16 15:59:00 -06001844 demo->gpu_props = (VkPhysicalDeviceProperties *) malloc(data_size);
1845 err = vkGetPhysicalDeviceInfo(demo->gpu, VK_PHYSICAL_DEVICE_INFO_TYPE_PROPERTIES,
Courtney Goeltzenleuchter752eb8e2015-03-25 13:36:41 -06001846 &data_size, demo->gpu_props);
1847 assert(!err);
1848
Tony Barbour72304ef2015-04-16 15:59:00 -06001849 err = vkGetPhysicalDeviceInfo(demo->gpu, VK_PHYSICAL_DEVICE_INFO_TYPE_QUEUE_PROPERTIES,
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07001850 &data_size, NULL);
1851 assert(!err);
1852
Tony Barbour72304ef2015-04-16 15:59:00 -06001853 demo->queue_props = (VkPhysicalDeviceQueueProperties *) malloc(data_size);
1854 err = vkGetPhysicalDeviceInfo(demo->gpu, VK_PHYSICAL_DEVICE_INFO_TYPE_QUEUE_PROPERTIES,
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07001855 &data_size, demo->queue_props);
1856 assert(!err);
Tony Barbour72304ef2015-04-16 15:59:00 -06001857 queue_count = (uint32_t)(data_size / sizeof(VkPhysicalDeviceQueueProperties));
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07001858 assert(queue_count >= 1);
1859
Mark Lobodzinskida9b1092015-04-16 11:44:05 -05001860 // Graphics queue and MemMgr queue can be separate.
1861 // TODO: Add support for separate queues, including synchronization,
1862 // and appropriate tracking for QueueSubmit and QueueBindObjectMemory
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07001863 for (i = 0; i < queue_count; i++) {
Mark Lobodzinskida9b1092015-04-16 11:44:05 -05001864 if ((demo->queue_props[i].queueFlags & VK_QUEUE_GRAPHICS_BIT) &&
1865 (demo->queue_props[i].queueFlags & VK_QUEUE_MEMMGR_BIT) )
Courtney Goeltzenleuchtered667a52015-03-05 18:09:39 -07001866 break;
1867 }
1868 assert(i < queue_count);
1869 demo->graphics_queue_node_index = i;
1870
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001871 err = vkGetDeviceQueue(demo->device, demo->graphics_queue_node_index,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001872 0, &demo->queue);
1873 assert(!err);
1874}
1875
1876static void demo_init_connection(struct demo *demo)
1877{
Ian Elliott639ca472015-04-16 15:23:05 -06001878#ifndef _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001879 const xcb_setup_t *setup;
1880 xcb_screen_iterator_t iter;
1881 int scr;
1882
1883 demo->connection = xcb_connect(NULL, &scr);
Ian Elliott3979e282015-04-03 15:24:55 -06001884 if (demo->connection == NULL) {
1885 printf("Cannot find a compatible Vulkan installable client driver "
1886 "(ICD).\nExiting ...\n");
1887 fflush(stdout);
1888 exit(1);
1889 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001890
1891 setup = xcb_get_setup(demo->connection);
1892 iter = xcb_setup_roots_iterator(setup);
1893 while (scr-- > 0)
1894 xcb_screen_next(&iter);
1895
1896 demo->screen = iter.data;
Ian Elliott639ca472015-04-16 15:23:05 -06001897#endif // _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001898}
1899
Ian Elliott639ca472015-04-16 15:23:05 -06001900#ifdef _WIN32
1901static void demo_init(struct demo *demo, HINSTANCE hInstance, LPSTR pCmdLine)
1902#else // _WIN32
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001903static void demo_init(struct demo *demo, int argc, char **argv)
Ian Elliott639ca472015-04-16 15:23:05 -06001904#endif // _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001905{
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001906 vec3 eye = {0.0f, 3.0f, 5.0f};
1907 vec3 origin = {0, 0, 0};
1908 vec3 up = {0.0f, -1.0f, 0.0};
Ian Elliott639ca472015-04-16 15:23:05 -06001909 bool argv_error = false;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001910
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001911 memset(demo, 0, sizeof(*demo));
1912
Ian Elliott639ca472015-04-16 15:23:05 -06001913#ifdef _WIN32
1914 demo->connection = hInstance;
1915 strncpy(demo->name, "cube", APP_NAME_STR_LEN);
1916
1917 if (strncmp(pCmdLine, "--use_staging", strlen("--use_staging")) == 0)
1918 demo->use_staging_buffer = true;
1919 else if (strlen(pCmdLine) != 0) {
1920 fprintf(stderr, "Do not recognize argument \"%s\".\n", pCmdLine);
1921 argv_error = true;
1922 }
1923#else // _WIN32
Piers Daniell735ee532015-02-23 16:23:13 -07001924 for (int i = 1; i < argc; i++) {
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001925 if (strncmp(argv[i], "--use_staging", strlen("--use_staging")) == 0)
1926 demo->use_staging_buffer = true;
Ian Elliott639ca472015-04-16 15:23:05 -06001927 else {
1928 fprintf(stderr, "Do not recognize argument \"%s\".\n", argv[i]);
1929 argv_error = true;
1930 }
1931 }
1932#endif // _WIN32
1933 if (argv_error) {
1934 fprintf(stderr, "Usage:\n cube [--use_staging]\n");
1935 fflush(stderr);
1936 exit(1);
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07001937 }
1938
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001939 demo_init_connection(demo);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001940 demo_init_vk(demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001941
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001942 demo->width = 500;
1943 demo->height = 500;
Tony Barbour72304ef2015-04-16 15:59:00 -06001944 demo->format = VK_FORMAT_B8G8R8A8_UNORM;
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001945
1946 demo->spin_angle = 0.01f;
1947 demo->spin_increment = 0.01f;
1948 demo->pause = false;
1949
Piers Daniell735ee532015-02-23 16:23:13 -07001950 mat4x4_perspective(demo->projection_matrix, (float)degreesToRadians(45.0f), 1.0f, 0.1f, 100.0f);
Courtney Goeltzenleuchter8879d3a2014-10-29 08:29:35 -06001951 mat4x4_look_at(demo->view_matrix, eye, origin, up);
1952 mat4x4_identity(demo->model_matrix);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001953}
1954
1955static void demo_cleanup(struct demo *demo)
1956{
Mark Lobodzinskic4fffc72015-01-29 08:55:56 -06001957 uint32_t i, j;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001958
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001959 vkDestroyObject(demo->desc_set);
1960 vkDestroyObject(demo->desc_pool);
Chia-I Wu6a3c8972015-01-04 16:27:24 +08001961
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001962 vkDestroyObject(demo->viewport);
1963 vkDestroyObject(demo->raster);
1964 vkDestroyObject(demo->color_blend);
1965 vkDestroyObject(demo->depth_stencil);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001966
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001967 vkDestroyObject(demo->pipeline);
Mark Lobodzinski1cfc7722015-04-17 14:11:39 -05001968 vkDestroyObject(demo->pipeline_layout);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001969 vkDestroyObject(demo->desc_layout);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001970
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001971 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001972 vkDestroyObject(demo->textures[i].view);
Mark Lobodzinskida9b1092015-04-16 11:44:05 -05001973 vkQueueBindObjectMemory(demo->queue, demo->textures[i].image, 0, VK_NULL_HANDLE, 0);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001974 vkDestroyObject(demo->textures[i].image);
Courtney Goeltzenleuchter1359b7a2015-04-02 16:25:42 -06001975 demo_remove_mem_refs(demo, demo->textures[i].num_mem, demo->textures[i].mem);
Jon Ashburnb2a66652015-01-16 09:37:43 -07001976 for (j = 0; j < demo->textures[i].num_mem; j++)
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001977 vkFreeMemory(demo->textures[i].mem[j]);
1978 vkDestroyObject(demo->textures[i].sampler);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001979 }
Chia-I Wucbb564e2015-04-16 22:02:10 +08001980 vkDestroySwapChainWSI(demo->swap_chain);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001981
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001982 vkDestroyObject(demo->depth.view);
Mark Lobodzinskida9b1092015-04-16 11:44:05 -05001983 vkQueueBindObjectMemory(demo->queue, demo->depth.image, 0, VK_NULL_HANDLE, 0);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001984 vkDestroyObject(demo->depth.image);
Courtney Goeltzenleuchter1359b7a2015-04-02 16:25:42 -06001985 demo_remove_mem_refs(demo, demo->depth.num_mem, demo->depth.mem);
1986 for (j = 0; j < demo->depth.num_mem; j++) {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001987 vkFreeMemory(demo->depth.mem[j]);
Courtney Goeltzenleuchter1359b7a2015-04-02 16:25:42 -06001988 }
Mark Lobodzinski7b8fee62015-02-18 16:38:17 -06001989
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001990 vkDestroyObject(demo->uniform_data.view);
Mark Lobodzinskida9b1092015-04-16 11:44:05 -05001991 vkQueueBindObjectMemory(demo->queue, demo->uniform_data.buf, 0, VK_NULL_HANDLE, 0);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001992 vkDestroyObject(demo->uniform_data.buf);
Courtney Goeltzenleuchter1359b7a2015-04-02 16:25:42 -06001993 demo_remove_mem_refs(demo, demo->uniform_data.num_mem, demo->uniform_data.mem);
Jon Ashburnae7c21c2015-01-19 15:00:26 -07001994 for (j = 0; j < demo->uniform_data.num_mem; j++)
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001995 vkFreeMemory(demo->uniform_data.mem[j]);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001996
1997 for (i = 0; i < DEMO_BUFFER_COUNT; i++) {
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001998 vkDestroyObject(demo->buffers[i].view);
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06001999 vkDestroyObject(demo->buffers[i].cmd);
Courtney Goeltzenleuchter1359b7a2015-04-02 16:25:42 -06002000 demo_remove_mem_refs(demo, 1, &demo->buffers[i].mem);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002001 }
2002
Courtney Goeltzenleuchtera4131c42015-04-08 15:36:08 -06002003 vkDestroyDevice(demo->device);
2004 vkDestroyInstance(demo->inst);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002005
Ian Elliott639ca472015-04-16 15:23:05 -06002006#ifndef _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002007 xcb_destroy_window(demo->connection, demo->window);
2008 xcb_disconnect(demo->connection);
Ian Elliott639ca472015-04-16 15:23:05 -06002009#endif // _WIN32
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002010}
2011
Ian Elliott639ca472015-04-16 15:23:05 -06002012#ifdef _WIN32
2013int APIENTRY WinMain(HINSTANCE hInstance,
2014 HINSTANCE hPrevInstance,
2015 LPSTR pCmdLine,
2016 int nCmdShow)
2017{
2018 MSG msg; // message
2019 bool done; // flag saying when app is complete
2020
2021 demo_init(&demo, hInstance, pCmdLine);
2022 demo_create_window(&demo);
2023
2024 demo_prepare(&demo);
2025
2026 done = false; //initialize loop condition variable
2027 /* main message loop*/
2028 while(!done)
2029 {
2030 PeekMessage(&msg,NULL,NULL,NULL,PM_REMOVE);
2031 if (msg.message == WM_QUIT) //check for a quit message
2032 {
2033 done = true; //if found, quit app
2034 }
2035 else
2036 {
2037 /* Translate and dispatch to event queue*/
2038 TranslateMessage(&msg);
2039 DispatchMessage(&msg);
2040 }
2041 }
2042
2043 demo_cleanup(&demo);
2044
2045 return msg.wParam;
2046}
2047#else // _WIN32
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002048int main(int argc, char **argv)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002049{
2050 struct demo demo;
2051
Courtney Goeltzenleuchterb4fc0072015-02-17 12:54:31 -07002052 demo_init(&demo, argc, argv);
Chia-I Wucbb564e2015-04-16 22:02:10 +08002053 demo_create_window(&demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002054
2055 demo_prepare(&demo);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002056 demo_run(&demo);
2057
2058 demo_cleanup(&demo);
2059
2060 return 0;
2061}
Ian Elliott639ca472015-04-16 15:23:05 -06002062#endif // _WIN32