Courtney Goeltzenleuchter | 6836939 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 1 | #define _GNU_SOURCE |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 2 | #include <stdio.h> |
| 3 | #include <stdlib.h> |
| 4 | #include <string.h> |
| 5 | #include <stdbool.h> |
| 6 | #include <assert.h> |
| 7 | |
| 8 | #include <xcb/xcb.h> |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 9 | #include <vulkan.h> |
| 10 | #include <vkDbg.h> |
| 11 | #include <vkWsiX11Ext.h> |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 12 | |
Cody Northrop | fe3d8bc | 2015-03-17 14:54:35 -0600 | [diff] [blame] | 13 | #include "icd-spv.h" |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 14 | |
Courtney Goeltzenleuchter | fc3b953 | 2014-10-28 10:32:57 -0600 | [diff] [blame] | 15 | #include "linmath.h" |
Courtney Goeltzenleuchter | 6836939 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 16 | #include <png.h> |
Courtney Goeltzenleuchter | fc3b953 | 2014-10-28 10:32:57 -0600 | [diff] [blame] | 17 | |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 18 | #define DEMO_BUFFER_COUNT 2 |
| 19 | #define DEMO_TEXTURE_COUNT 1 |
| 20 | |
Courtney Goeltzenleuchter | 97db1c1 | 2014-10-30 15:14:16 -0600 | [diff] [blame] | 21 | /* |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 22 | * structure to track all objects related to a texture. |
| 23 | */ |
Courtney Goeltzenleuchter | 752eb8e | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 24 | struct texture_object { |
Courtney Goeltzenleuchter | 78351a6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 25 | VkSampler sampler; |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 26 | |
Courtney Goeltzenleuchter | 78351a6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 27 | VkImage image; |
| 28 | VkImageLayout imageLayout; |
Courtney Goeltzenleuchter | 752eb8e | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 29 | |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 30 | uint32_t num_mem; |
Tony Barbour | 72304ef | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 31 | VkDeviceMemory *mem; |
Courtney Goeltzenleuchter | 78351a6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 32 | VkImageView view; |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 33 | int32_t tex_width, tex_height; |
| 34 | }; |
| 35 | |
Courtney Goeltzenleuchter | 6836939 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 36 | static char *tex_files[] = { |
Courtney Goeltzenleuchter | 97db1c1 | 2014-10-30 15:14:16 -0600 | [diff] [blame] | 37 | "lunarg-logo-256x256-solid.png" |
Courtney Goeltzenleuchter | 6836939 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 38 | }; |
| 39 | |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 40 | struct vkcube_vs_uniform { |
Courtney Goeltzenleuchter | 4984d15 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 41 | // Must start with MVP |
Mark Lobodzinski | c4fffc7 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 42 | float mvp[4][4]; |
| 43 | float position[12*3][4]; |
| 44 | float color[12*3][4]; |
Courtney Goeltzenleuchter | 4984d15 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 45 | }; |
| 46 | |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 47 | struct vktexcube_vs_uniform { |
Courtney Goeltzenleuchter | 4984d15 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 48 | // Must start with MVP |
Mark Lobodzinski | c4fffc7 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 49 | float mvp[4][4]; |
| 50 | float position[12*3][4]; |
| 51 | float attr[12*3][4]; |
Courtney Goeltzenleuchter | 4984d15 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 52 | }; |
Courtney Goeltzenleuchter | fc3b953 | 2014-10-28 10:32:57 -0600 | [diff] [blame] | 53 | |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 54 | //-------------------------------------------------------------------------------------- |
| 55 | // Mesh and VertexFormat Data |
| 56 | //-------------------------------------------------------------------------------------- |
| 57 | struct Vertex |
| 58 | { |
Mark Lobodzinski | c4fffc7 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 59 | float posX, posY, posZ, posW; // Position data |
| 60 | float r, g, b, a; // Color |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 61 | }; |
| 62 | |
Courtney Goeltzenleuchter | 4984d15 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 63 | struct VertexPosTex |
| 64 | { |
Mark Lobodzinski | c4fffc7 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 65 | float posX, posY, posZ, posW; // Position data |
| 66 | float u, v, s, t; // Texcoord |
Courtney Goeltzenleuchter | 4984d15 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 67 | }; |
| 68 | |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 69 | #define XYZ1(_x_, _y_, _z_) (_x_), (_y_), (_z_), 1.f |
Courtney Goeltzenleuchter | 4984d15 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 70 | #define UV(_u_, _v_) (_u_), (_v_), 0.f, 1.f |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 71 | |
Mark Lobodzinski | c4fffc7 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 72 | static const float g_vertex_buffer_data[] = { |
Courtney Goeltzenleuchter | 6836939 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 73 | -1.0f,-1.0f,-1.0f, // Vertex 0 |
| 74 | -1.0f,-1.0f, 1.0f, |
| 75 | -1.0f, 1.0f, 1.0f, |
| 76 | |
| 77 | -1.0f, 1.0f, 1.0f, // Vertex 1 |
| 78 | -1.0f, 1.0f,-1.0f, |
| 79 | -1.0f,-1.0f,-1.0f, |
| 80 | |
| 81 | -1.0f,-1.0f,-1.0f, // Vertex 2 |
| 82 | 1.0f, 1.0f,-1.0f, |
| 83 | 1.0f,-1.0f,-1.0f, |
| 84 | |
| 85 | -1.0f,-1.0f,-1.0f, // Vertex 3 |
Courtney Goeltzenleuchter | 6836939 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 86 | -1.0f, 1.0f,-1.0f, |
Mike Stroyan | 16b3d98 | 2015-03-19 14:29:04 -0600 | [diff] [blame] | 87 | 1.0f, 1.0f,-1.0f, |
Courtney Goeltzenleuchter | 6836939 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 88 | |
| 89 | -1.0f,-1.0f,-1.0f, // Vertex 4 |
Courtney Goeltzenleuchter | 6836939 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 90 | 1.0f,-1.0f,-1.0f, |
Mike Stroyan | 16b3d98 | 2015-03-19 14:29:04 -0600 | [diff] [blame] | 91 | 1.0f,-1.0f, 1.0f, |
Courtney Goeltzenleuchter | 6836939 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 92 | |
| 93 | -1.0f,-1.0f,-1.0f, // Vertex 5 |
Courtney Goeltzenleuchter | 6836939 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 94 | 1.0f,-1.0f, 1.0f, |
Mike Stroyan | 16b3d98 | 2015-03-19 14:29:04 -0600 | [diff] [blame] | 95 | -1.0f,-1.0f, 1.0f, |
Courtney Goeltzenleuchter | 6836939 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 96 | |
| 97 | -1.0f, 1.0f,-1.0f, // Vertex 6 |
| 98 | -1.0f, 1.0f, 1.0f, |
| 99 | 1.0f, 1.0f, 1.0f, |
| 100 | |
| 101 | -1.0f, 1.0f,-1.0f, // Vertex 7 |
Courtney Goeltzenleuchter | 6836939 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 102 | 1.0f, 1.0f, 1.0f, |
Mike Stroyan | 16b3d98 | 2015-03-19 14:29:04 -0600 | [diff] [blame] | 103 | 1.0f, 1.0f,-1.0f, |
Courtney Goeltzenleuchter | 6836939 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 104 | |
| 105 | 1.0f, 1.0f,-1.0f, // Vertex 8 |
| 106 | 1.0f, 1.0f, 1.0f, |
| 107 | 1.0f,-1.0f, 1.0f, |
| 108 | |
| 109 | 1.0f,-1.0f, 1.0f, // Vertex 9 |
| 110 | 1.0f,-1.0f,-1.0f, |
| 111 | 1.0f, 1.0f,-1.0f, |
| 112 | |
| 113 | -1.0f, 1.0f, 1.0f, // Vertex 10 |
Courtney Goeltzenleuchter | 6836939 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 114 | -1.0f,-1.0f, 1.0f, |
Mike Stroyan | 16b3d98 | 2015-03-19 14:29:04 -0600 | [diff] [blame] | 115 | 1.0f, 1.0f, 1.0f, |
Courtney Goeltzenleuchter | 6836939 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 116 | |
| 117 | -1.0f,-1.0f, 1.0f, // Vertex 11 |
| 118 | 1.0f,-1.0f, 1.0f, |
| 119 | 1.0f, 1.0f, 1.0f, |
Courtney Goeltzenleuchter | 4984d15 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 120 | }; |
| 121 | |
Mark Lobodzinski | c4fffc7 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 122 | static const float g_uv_buffer_data[] = { |
Courtney Goeltzenleuchter | 8879d3a | 2014-10-29 08:29:35 -0600 | [diff] [blame] | 123 | 1.0f, 0.0f, // Vertex 0 |
Courtney Goeltzenleuchter | 6836939 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 124 | 0.0f, 0.0f, |
| 125 | 0.0f, 1.0f, |
| 126 | |
Courtney Goeltzenleuchter | 8879d3a | 2014-10-29 08:29:35 -0600 | [diff] [blame] | 127 | 0.0f, 1.0f, // Vertex 1 |
Courtney Goeltzenleuchter | 6836939 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 128 | 1.0f, 1.0f, |
Courtney Goeltzenleuchter | 8879d3a | 2014-10-29 08:29:35 -0600 | [diff] [blame] | 129 | 1.0f, 0.0f, |
| 130 | |
| 131 | // 0.0f, 1.0f, // Vertex 2 |
| 132 | // 1.0f, 0.0f, |
| 133 | // 0.0f, 0.0f, |
| 134 | |
| 135 | // 0.0f, 1.0f, // Vertex 3 |
| 136 | // 1.0f, 0.0f, |
| 137 | // 1.0f, 1.0f, |
| 138 | |
| 139 | 0.0f, 0.0f, // Vertex 2 |
| 140 | 1.0f, 1.0f, |
| 141 | 1.0f, 0.0f, |
| 142 | |
| 143 | 0.0f, 0.0f, // Vertex 3 |
Courtney Goeltzenleuchter | 8879d3a | 2014-10-29 08:29:35 -0600 | [diff] [blame] | 144 | 0.0f, 1.0f, |
Mike Stroyan | 16b3d98 | 2015-03-19 14:29:04 -0600 | [diff] [blame] | 145 | 1.0f, 1.0f, |
Courtney Goeltzenleuchter | 6836939 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 146 | |
| 147 | 0.0f, 1.0f, // Vertex 4 |
Courtney Goeltzenleuchter | 6836939 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 148 | 0.0f, 0.0f, |
Mike Stroyan | 16b3d98 | 2015-03-19 14:29:04 -0600 | [diff] [blame] | 149 | 1.0f, 0.0f, |
Courtney Goeltzenleuchter | 6836939 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 150 | |
| 151 | 0.0f, 1.0f, // Vertex 5 |
Courtney Goeltzenleuchter | 6836939 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 152 | 1.0f, 0.0f, |
Mike Stroyan | 16b3d98 | 2015-03-19 14:29:04 -0600 | [diff] [blame] | 153 | 1.0f, 1.0f, |
Courtney Goeltzenleuchter | 6836939 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 154 | |
| 155 | 0.0f, 1.0f, // Vertex 6 |
| 156 | 1.0f, 1.0f, |
| 157 | 1.0f, 0.0f, |
| 158 | |
| 159 | 0.0f, 1.0f, // Vertex 7 |
Courtney Goeltzenleuchter | 6836939 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 160 | 1.0f, 0.0f, |
Mike Stroyan | 16b3d98 | 2015-03-19 14:29:04 -0600 | [diff] [blame] | 161 | 0.0f, 0.0f, |
Courtney Goeltzenleuchter | 6836939 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 162 | |
Courtney Goeltzenleuchter | 8879d3a | 2014-10-29 08:29:35 -0600 | [diff] [blame] | 163 | 0.0f, 1.0f, // Vertex 8 |
Courtney Goeltzenleuchter | 6836939 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 164 | 1.0f, 1.0f, |
Courtney Goeltzenleuchter | 6836939 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 165 | 1.0f, 0.0f, |
Courtney Goeltzenleuchter | 8879d3a | 2014-10-29 08:29:35 -0600 | [diff] [blame] | 166 | |
| 167 | 1.0f, 0.0f, // Vertex 9 |
Courtney Goeltzenleuchter | 6836939 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 168 | 0.0f, 0.0f, |
Courtney Goeltzenleuchter | 6836939 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 169 | 0.0f, 1.0f, |
| 170 | |
Courtney Goeltzenleuchter | 8879d3a | 2014-10-29 08:29:35 -0600 | [diff] [blame] | 171 | 1.0f, 1.0f, // Vertex 10 |
Courtney Goeltzenleuchter | 6836939 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 172 | 1.0f, 0.0f, |
Mike Stroyan | 16b3d98 | 2015-03-19 14:29:04 -0600 | [diff] [blame] | 173 | 0.0f, 1.0f, |
Courtney Goeltzenleuchter | 8879d3a | 2014-10-29 08:29:35 -0600 | [diff] [blame] | 174 | |
| 175 | 1.0f, 0.0f, // Vertex 11 |
| 176 | 0.0f, 0.0f, |
| 177 | 0.0f, 1.0f, |
Courtney Goeltzenleuchter | 4984d15 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 178 | }; |
| 179 | |
| 180 | void dumpMatrix(const char *note, mat4x4 MVP) |
| 181 | { |
| 182 | int i; |
| 183 | |
| 184 | printf("%s: \n", note); |
| 185 | for (i=0; i<4; i++) { |
| 186 | printf("%f, %f, %f, %f\n", MVP[i][0], MVP[i][1], MVP[i][2], MVP[i][3]); |
| 187 | } |
| 188 | printf("\n"); |
| 189 | fflush(stdout); |
| 190 | } |
| 191 | |
| 192 | void dumpVec4(const char *note, vec4 vector) |
| 193 | { |
| 194 | printf("%s: \n", note); |
| 195 | printf("%f, %f, %f, %f\n", vector[0], vector[1], vector[2], vector[3]); |
| 196 | printf("\n"); |
| 197 | fflush(stdout); |
| 198 | } |
| 199 | |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 200 | struct demo { |
| 201 | xcb_connection_t *connection; |
| 202 | xcb_screen_t *screen; |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 203 | bool use_staging_buffer; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 204 | |
Courtney Goeltzenleuchter | 78351a6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 205 | VkInstance inst; |
Tony Barbour | 72304ef | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 206 | VkPhysicalDevice gpu; |
Courtney Goeltzenleuchter | 78351a6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 207 | VkDevice device; |
| 208 | VkQueue queue; |
Courtney Goeltzenleuchter | ed667a5 | 2015-03-05 18:09:39 -0700 | [diff] [blame] | 209 | uint32_t graphics_queue_node_index; |
Tony Barbour | 72304ef | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 210 | VkPhysicalDeviceProperties *gpu_props; |
| 211 | VkPhysicalDeviceQueueProperties *queue_props; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 212 | |
Courtney Goeltzenleuchter | 78351a6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 213 | VkFramebuffer framebuffer; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 214 | int width, height; |
Courtney Goeltzenleuchter | 78351a6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 215 | VkFormat format; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 216 | |
| 217 | struct { |
Courtney Goeltzenleuchter | 78351a6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 218 | VkImage image; |
Tony Barbour | 72304ef | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 219 | VkDeviceMemory mem; |
Courtney Goeltzenleuchter | 78351a6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 220 | VkCmdBuffer cmd; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 221 | |
Courtney Goeltzenleuchter | 78351a6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 222 | VkColorAttachmentView view; |
| 223 | VkFence fence; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 224 | } buffers[DEMO_BUFFER_COUNT]; |
| 225 | |
| 226 | struct { |
Courtney Goeltzenleuchter | 78351a6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 227 | VkFormat format; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 228 | |
Courtney Goeltzenleuchter | 78351a6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 229 | VkImage image; |
Mark Lobodzinski | c4fffc7 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 230 | uint32_t num_mem; |
Tony Barbour | 72304ef | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 231 | VkDeviceMemory *mem; |
Courtney Goeltzenleuchter | 78351a6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 232 | VkDepthStencilView view; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 233 | } depth; |
| 234 | |
Courtney Goeltzenleuchter | 752eb8e | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 235 | struct texture_object textures[DEMO_TEXTURE_COUNT]; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 236 | |
| 237 | struct { |
Courtney Goeltzenleuchter | 78351a6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 238 | VkBuffer buf; |
Mark Lobodzinski | c4fffc7 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 239 | uint32_t num_mem; |
Tony Barbour | 72304ef | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 240 | VkDeviceMemory *mem; |
Courtney Goeltzenleuchter | 78351a6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 241 | VkBufferView view; |
| 242 | VkBufferViewAttachInfo attach; |
Courtney Goeltzenleuchter | fc3b953 | 2014-10-28 10:32:57 -0600 | [diff] [blame] | 243 | } uniform_data; |
| 244 | |
Courtney Goeltzenleuchter | 78351a6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 245 | VkCmdBuffer cmd; // Buffer for initialization commands |
| 246 | VkDescriptorSetLayoutChain desc_layout_chain; |
| 247 | VkDescriptorSetLayout desc_layout; |
| 248 | VkPipeline pipeline; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 249 | |
Courtney Goeltzenleuchter | 5e6d1e9 | 2015-04-10 16:24:50 -0600 | [diff] [blame] | 250 | VkDynamicVpState viewport; |
| 251 | VkDynamicRsState raster; |
| 252 | VkDynamicCbState color_blend; |
| 253 | VkDynamicDsState depth_stencil; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 254 | |
Courtney Goeltzenleuchter | fc3b953 | 2014-10-28 10:32:57 -0600 | [diff] [blame] | 255 | mat4x4 projection_matrix; |
| 256 | mat4x4 view_matrix; |
| 257 | mat4x4 model_matrix; |
| 258 | |
Mark Lobodzinski | c4fffc7 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 259 | float spin_angle; |
| 260 | float spin_increment; |
Courtney Goeltzenleuchter | 8879d3a | 2014-10-29 08:29:35 -0600 | [diff] [blame] | 261 | bool pause; |
| 262 | |
Courtney Goeltzenleuchter | 78351a6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 263 | VkDescriptorPool desc_pool; |
| 264 | VkDescriptorSet desc_set; |
Chia-I Wu | 6a3c897 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 265 | |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 266 | xcb_window_t window; |
Courtney Goeltzenleuchter | 98cb2cb | 2014-11-06 14:27:52 -0700 | [diff] [blame] | 267 | xcb_intern_atom_reply_t *atom_wm_delete_window; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 268 | |
| 269 | bool quit; |
Mark Lobodzinski | c4fffc7 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 270 | uint32_t current_buffer; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 271 | }; |
| 272 | |
Courtney Goeltzenleuchter | 752eb8e | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 273 | static void demo_flush_init_cmd(struct demo *demo) |
| 274 | { |
Courtney Goeltzenleuchter | 78351a6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 275 | VkResult err; |
Courtney Goeltzenleuchter | 752eb8e | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 276 | |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 277 | if (demo->cmd == VK_NULL_HANDLE) |
Courtney Goeltzenleuchter | 752eb8e | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 278 | return; |
| 279 | |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 280 | err = vkEndCommandBuffer(demo->cmd); |
Courtney Goeltzenleuchter | 752eb8e | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 281 | assert(!err); |
| 282 | |
Courtney Goeltzenleuchter | 78351a6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 283 | const VkCmdBuffer cmd_bufs[] = { demo->cmd }; |
Courtney Goeltzenleuchter | 752eb8e | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 284 | |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 285 | err = vkQueueSubmit(demo->queue, 1, cmd_bufs, VK_NULL_HANDLE); |
Courtney Goeltzenleuchter | 752eb8e | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 286 | assert(!err); |
| 287 | |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 288 | err = vkQueueWaitIdle(demo->queue); |
Courtney Goeltzenleuchter | 752eb8e | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 289 | assert(!err); |
| 290 | |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 291 | vkDestroyObject(demo->cmd); |
| 292 | demo->cmd = VK_NULL_HANDLE; |
Courtney Goeltzenleuchter | 752eb8e | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | static void demo_add_mem_refs( |
| 296 | struct demo *demo, |
Tony Barbour | 72304ef | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 297 | int num_refs, VkDeviceMemory *mem) |
Courtney Goeltzenleuchter | 752eb8e | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 298 | { |
Courtney Goeltzenleuchter | 6b0caf1 | 2015-04-16 13:38:46 -0600 | [diff] [blame] | 299 | vkQueueAddMemReferences(demo->queue, num_refs, mem); |
Courtney Goeltzenleuchter | 1359b7a | 2015-04-02 16:25:42 -0600 | [diff] [blame] | 300 | } |
| 301 | |
| 302 | static void demo_remove_mem_refs( |
| 303 | struct demo *demo, |
Tony Barbour | 72304ef | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 304 | int num_refs, VkDeviceMemory *mem) |
Courtney Goeltzenleuchter | 1359b7a | 2015-04-02 16:25:42 -0600 | [diff] [blame] | 305 | { |
Courtney Goeltzenleuchter | 6b0caf1 | 2015-04-16 13:38:46 -0600 | [diff] [blame] | 306 | vkQueueRemoveMemReferences(demo->queue, num_refs, mem); |
Courtney Goeltzenleuchter | 752eb8e | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | static void demo_set_image_layout( |
| 310 | struct demo *demo, |
Courtney Goeltzenleuchter | 78351a6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 311 | VkImage image, |
| 312 | VkImageLayout old_image_layout, |
| 313 | VkImageLayout new_image_layout) |
Courtney Goeltzenleuchter | 752eb8e | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 314 | { |
Courtney Goeltzenleuchter | 78351a6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 315 | VkResult err; |
Courtney Goeltzenleuchter | 752eb8e | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 316 | |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 317 | if (demo->cmd == VK_NULL_HANDLE) { |
Courtney Goeltzenleuchter | 78351a6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 318 | const VkCmdBufferCreateInfo cmd = { |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 319 | .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO, |
Courtney Goeltzenleuchter | 752eb8e | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 320 | .pNext = NULL, |
| 321 | .queueNodeIndex = demo->graphics_queue_node_index, |
| 322 | .flags = 0, |
| 323 | }; |
| 324 | |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 325 | err = vkCreateCommandBuffer(demo->device, &cmd, &demo->cmd); |
Courtney Goeltzenleuchter | 752eb8e | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 326 | assert(!err); |
| 327 | |
Courtney Goeltzenleuchter | 78351a6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 328 | VkCmdBufferBeginInfo cmd_buf_info = { |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 329 | .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO, |
Courtney Goeltzenleuchter | 752eb8e | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 330 | .pNext = NULL, |
Tony Barbour | 72304ef | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 331 | .flags = VK_CMD_BUFFER_OPTIMIZE_SMALL_BATCH_BIT | |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 332 | VK_CMD_BUFFER_OPTIMIZE_ONE_TIME_SUBMIT_BIT, |
Courtney Goeltzenleuchter | 752eb8e | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 333 | }; |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 334 | err = vkBeginCommandBuffer(demo->cmd, &cmd_buf_info); |
Courtney Goeltzenleuchter | 752eb8e | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 335 | } |
| 336 | |
Courtney Goeltzenleuchter | 78351a6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 337 | VkImageMemoryBarrier image_memory_barrier = { |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 338 | .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER, |
Courtney Goeltzenleuchter | 752eb8e | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 339 | .pNext = NULL, |
| 340 | .outputMask = 0, |
| 341 | .inputMask = 0, |
| 342 | .oldLayout = old_image_layout, |
| 343 | .newLayout = new_image_layout, |
| 344 | .image = image, |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 345 | .subresourceRange = { VK_IMAGE_ASPECT_COLOR, 0, 1, 0, 0 } |
Courtney Goeltzenleuchter | 752eb8e | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 346 | }; |
| 347 | |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 348 | if (new_image_layout == VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL) { |
Courtney Goeltzenleuchter | 752eb8e | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 349 | /* Make sure anything that was copying from this image has completed */ |
Courtney Goeltzenleuchter | 42a078d | 2015-04-15 15:29:59 -0600 | [diff] [blame] | 350 | image_memory_barrier.inputMask = VK_MEMORY_INPUT_TRANSFER_BIT; |
Courtney Goeltzenleuchter | 752eb8e | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 351 | } |
| 352 | |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 353 | if (new_image_layout == VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL) { |
Courtney Goeltzenleuchter | 752eb8e | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 354 | /* Make sure any Copy or CPU writes to image are flushed */ |
Courtney Goeltzenleuchter | 42a078d | 2015-04-15 15:29:59 -0600 | [diff] [blame] | 355 | image_memory_barrier.outputMask = VK_MEMORY_OUTPUT_CPU_WRITE_BIT | VK_MEMORY_OUTPUT_TRANSFER_BIT; |
Courtney Goeltzenleuchter | 752eb8e | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 356 | } |
| 357 | |
Courtney Goeltzenleuchter | 78351a6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 358 | VkImageMemoryBarrier *pmemory_barrier = &image_memory_barrier; |
Courtney Goeltzenleuchter | 752eb8e | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 359 | |
Courtney Goeltzenleuchter | 78351a6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 360 | VkPipeEvent set_events[] = { VK_PIPE_EVENT_TOP_OF_PIPE }; |
Courtney Goeltzenleuchter | 752eb8e | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 361 | |
Tony Barbour | 72304ef | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 362 | vkCmdPipelineBarrier(demo->cmd, VK_WAIT_EVENT_TOP_OF_PIPE, 1, set_events, 1, (const void **)&pmemory_barrier); |
Courtney Goeltzenleuchter | 752eb8e | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 363 | } |
| 364 | |
Courtney Goeltzenleuchter | 78351a6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 365 | static void demo_draw_build_cmd(struct demo *demo, VkCmdBuffer cmd_buf) |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 366 | { |
Courtney Goeltzenleuchter | 78351a6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 367 | const VkColorAttachmentBindInfo color_attachment = { |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 368 | .view = demo->buffers[demo->current_buffer].view, |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 369 | .layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 370 | }; |
Courtney Goeltzenleuchter | 78351a6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 371 | const VkDepthStencilBindInfo depth_stencil = { |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 372 | .view = demo->depth.view, |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 373 | .layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL, |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 374 | }; |
Courtney Goeltzenleuchter | 78351a6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 375 | const VkClearColor clear_color = { |
Courtney Goeltzenleuchter | 374553c | 2015-04-03 16:35:32 -0600 | [diff] [blame] | 376 | .color.floatColor = { 0.2f, 0.2f, 0.2f, 0.2f }, |
| 377 | .useRawValue = false, |
| 378 | }; |
Mark Lobodzinski | c4fffc7 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 379 | const float clear_depth = 1.0f; |
Courtney Goeltzenleuchter | 78351a6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 380 | VkImageSubresourceRange clear_range; |
| 381 | VkCmdBufferBeginInfo cmd_buf_info = { |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 382 | .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO, |
Courtney Goeltzenleuchter | 53968d8 | 2015-04-03 15:25:24 -0600 | [diff] [blame] | 383 | .pNext = NULL, |
Tony Barbour | 72304ef | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 384 | .flags = VK_CMD_BUFFER_OPTIMIZE_SMALL_BATCH_BIT | |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 385 | VK_CMD_BUFFER_OPTIMIZE_ONE_TIME_SUBMIT_BIT, |
Jon Ashburn | 60ccfbe | 2014-12-31 17:08:35 -0700 | [diff] [blame] | 386 | }; |
Courtney Goeltzenleuchter | 78351a6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 387 | VkResult err; |
| 388 | VkAttachmentLoadOp load_op = VK_ATTACHMENT_LOAD_OP_DONT_CARE; |
| 389 | VkAttachmentStoreOp store_op = VK_ATTACHMENT_STORE_OP_DONT_CARE; |
| 390 | const VkFramebufferCreateInfo fb_info = { |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 391 | .sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO, |
Jon Ashburn | 08f6eb9 | 2015-01-02 18:24:05 -0700 | [diff] [blame] | 392 | .pNext = NULL, |
| 393 | .colorAttachmentCount = 1, |
Courtney Goeltzenleuchter | 78351a6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 394 | .pColorAttachments = (VkColorAttachmentBindInfo*) &color_attachment, |
| 395 | .pDepthStencilAttachment = (VkDepthStencilBindInfo*) &depth_stencil, |
Jon Ashburn | 08f6eb9 | 2015-01-02 18:24:05 -0700 | [diff] [blame] | 396 | .sampleCount = 1, |
Mark Lobodzinski | c06b741 | 2015-01-27 13:24:03 -0600 | [diff] [blame] | 397 | .width = demo->width, |
| 398 | .height = demo->height, |
| 399 | .layers = 1, |
Jon Ashburn | 08f6eb9 | 2015-01-02 18:24:05 -0700 | [diff] [blame] | 400 | }; |
Courtney Goeltzenleuchter | 78351a6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 401 | VkRenderPassCreateInfo rp_info; |
| 402 | VkRenderPassBegin rp_begin; |
Jon Ashburn | 08f6eb9 | 2015-01-02 18:24:05 -0700 | [diff] [blame] | 403 | |
| 404 | memset(&rp_info, 0 , sizeof(rp_info)); |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 405 | err = vkCreateFramebuffer(demo->device, &fb_info, &rp_begin.framebuffer); |
Jon Ashburn | 08f6eb9 | 2015-01-02 18:24:05 -0700 | [diff] [blame] | 406 | assert(!err); |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 407 | rp_info.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO; |
Jon Ashburn | 08f6eb9 | 2015-01-02 18:24:05 -0700 | [diff] [blame] | 408 | rp_info.renderArea.extent.width = demo->width; |
| 409 | rp_info.renderArea.extent.height = demo->height; |
Courtney Goeltzenleuchter | 53968d8 | 2015-04-03 15:25:24 -0600 | [diff] [blame] | 410 | rp_info.colorAttachmentCount = fb_info.colorAttachmentCount; |
| 411 | rp_info.pColorFormats = &demo->format; |
| 412 | rp_info.pColorLayouts = &color_attachment.layout; |
Jon Ashburn | 08f6eb9 | 2015-01-02 18:24:05 -0700 | [diff] [blame] | 413 | rp_info.pColorLoadOps = &load_op; |
| 414 | rp_info.pColorStoreOps = &store_op; |
Courtney Goeltzenleuchter | 53968d8 | 2015-04-03 15:25:24 -0600 | [diff] [blame] | 415 | rp_info.pColorLoadClearValues = &clear_color; |
Tony Barbour | 72304ef | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 416 | rp_info.depthStencilFormat = VK_FORMAT_D16_UNORM; |
Courtney Goeltzenleuchter | 53968d8 | 2015-04-03 15:25:24 -0600 | [diff] [blame] | 417 | rp_info.depthStencilLayout = depth_stencil.layout; |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 418 | rp_info.depthLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE; |
Courtney Goeltzenleuchter | 53968d8 | 2015-04-03 15:25:24 -0600 | [diff] [blame] | 419 | rp_info.depthLoadClearValue = clear_depth; |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 420 | rp_info.depthStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE; |
| 421 | rp_info.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE; |
Courtney Goeltzenleuchter | 53968d8 | 2015-04-03 15:25:24 -0600 | [diff] [blame] | 422 | rp_info.stencilLoadClearValue = 0; |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 423 | rp_info.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE; |
| 424 | err = vkCreateRenderPass(demo->device, &rp_info, &rp_begin.renderPass); |
Jon Ashburn | 08f6eb9 | 2015-01-02 18:24:05 -0700 | [diff] [blame] | 425 | assert(!err); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 426 | |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 427 | err = vkBeginCommandBuffer(cmd_buf, &cmd_buf_info); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 428 | assert(!err); |
| 429 | |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 430 | vkCmdBindPipeline(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS, |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 431 | demo->pipeline); |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 432 | vkCmdBindDescriptorSets(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS, |
Chia-I Wu | 6521e9e | 2015-04-17 02:00:54 +0800 | [diff] [blame^] | 433 | 0, 1, &demo->desc_set, NULL); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 434 | |
Tony Barbour | 72304ef | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 435 | vkCmdBindDynamicStateObject(cmd_buf, VK_STATE_BIND_POINT_VIEWPORT, demo->viewport); |
| 436 | vkCmdBindDynamicStateObject(cmd_buf, VK_STATE_BIND_POINT_RASTER, demo->raster); |
| 437 | vkCmdBindDynamicStateObject(cmd_buf, VK_STATE_BIND_POINT_COLOR_BLEND, |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 438 | demo->color_blend); |
Tony Barbour | 72304ef | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 439 | vkCmdBindDynamicStateObject(cmd_buf, VK_STATE_BIND_POINT_DEPTH_STENCIL, |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 440 | demo->depth_stencil); |
| 441 | |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 442 | vkCmdBeginRenderPass(cmd_buf, &rp_begin); |
| 443 | clear_range.aspect = VK_IMAGE_ASPECT_COLOR; |
Courtney Goeltzenleuchter | fc3b953 | 2014-10-28 10:32:57 -0600 | [diff] [blame] | 444 | clear_range.baseMipLevel = 0; |
| 445 | clear_range.mipLevels = 1; |
| 446 | clear_range.baseArraySlice = 0; |
| 447 | clear_range.arraySize = 1; |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 448 | vkCmdClearColorImage(cmd_buf, |
Courtney Goeltzenleuchter | fc3b953 | 2014-10-28 10:32:57 -0600 | [diff] [blame] | 449 | demo->buffers[demo->current_buffer].image, |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 450 | VK_IMAGE_LAYOUT_CLEAR_OPTIMAL, |
Courtney Goeltzenleuchter | fc3b953 | 2014-10-28 10:32:57 -0600 | [diff] [blame] | 451 | clear_color, 1, &clear_range); |
| 452 | |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 453 | clear_range.aspect = VK_IMAGE_ASPECT_DEPTH; |
| 454 | vkCmdClearDepthStencil(cmd_buf, demo->depth.image, |
| 455 | VK_IMAGE_LAYOUT_CLEAR_OPTIMAL, |
Courtney Goeltzenleuchter | fc3b953 | 2014-10-28 10:32:57 -0600 | [diff] [blame] | 456 | clear_depth, 0, 1, &clear_range); |
| 457 | |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 458 | vkCmdDraw(cmd_buf, 0, 12 * 3, 0, 1); |
| 459 | vkCmdEndRenderPass(cmd_buf, rp_begin.renderPass); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 460 | |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 461 | err = vkEndCommandBuffer(cmd_buf); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 462 | assert(!err); |
Courtney Goeltzenleuchter | 94901dc | 2015-02-25 17:53:18 -0700 | [diff] [blame] | 463 | |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 464 | vkDestroyObject(rp_begin.renderPass); |
| 465 | vkDestroyObject(rp_begin.framebuffer); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 466 | } |
| 467 | |
Courtney Goeltzenleuchter | fc3b953 | 2014-10-28 10:32:57 -0600 | [diff] [blame] | 468 | |
Courtney Goeltzenleuchter | 8879d3a | 2014-10-29 08:29:35 -0600 | [diff] [blame] | 469 | void demo_update_data_buffer(struct demo *demo) |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 470 | { |
Courtney Goeltzenleuchter | fc3b953 | 2014-10-28 10:32:57 -0600 | [diff] [blame] | 471 | mat4x4 MVP, Model, VP; |
| 472 | int matrixSize = sizeof(MVP); |
Mark Lobodzinski | c4fffc7 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 473 | uint8_t *pData; |
Courtney Goeltzenleuchter | 78351a6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 474 | VkResult err; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 475 | |
Courtney Goeltzenleuchter | fc3b953 | 2014-10-28 10:32:57 -0600 | [diff] [blame] | 476 | mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 477 | |
Courtney Goeltzenleuchter | fc3b953 | 2014-10-28 10:32:57 -0600 | [diff] [blame] | 478 | // Rotate 22.5 degrees around the Y axis |
Courtney Goeltzenleuchter | 4984d15 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 479 | mat4x4_dup(Model, demo->model_matrix); |
Piers Daniell | 735ee53 | 2015-02-23 16:23:13 -0700 | [diff] [blame] | 480 | mat4x4_rotate(demo->model_matrix, Model, 0.0f, 1.0f, 0.0f, (float)degreesToRadians(demo->spin_angle)); |
Courtney Goeltzenleuchter | 4984d15 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 481 | mat4x4_mul(MVP, VP, demo->model_matrix); |
Courtney Goeltzenleuchter | fc3b953 | 2014-10-28 10:32:57 -0600 | [diff] [blame] | 482 | |
Jon Ashburn | ae7c21c | 2015-01-19 15:00:26 -0700 | [diff] [blame] | 483 | assert(demo->uniform_data.num_mem == 1); |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 484 | err = vkMapMemory(demo->uniform_data.mem[0], 0, (void **) &pData); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 485 | assert(!err); |
| 486 | |
Courtney Goeltzenleuchter | fc3b953 | 2014-10-28 10:32:57 -0600 | [diff] [blame] | 487 | memcpy(pData, (const void*) &MVP[0][0], matrixSize); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 488 | |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 489 | err = vkUnmapMemory(demo->uniform_data.mem[0]); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 490 | assert(!err); |
| 491 | } |
| 492 | |
| 493 | static void demo_draw(struct demo *demo) |
| 494 | { |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 495 | const VK_WSI_X11_PRESENT_INFO present = { |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 496 | .destWindow = demo->window, |
| 497 | .srcImage = demo->buffers[demo->current_buffer].image, |
Chia-I Wu | bb57f64 | 2014-11-07 14:30:34 +0800 | [diff] [blame] | 498 | .async = true, |
| 499 | .flip = false, |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 500 | }; |
Courtney Goeltzenleuchter | 78351a6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 501 | VkFence fence = demo->buffers[demo->current_buffer].fence; |
| 502 | VkResult err; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 503 | |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 504 | err = vkWaitForFences(demo->device, 1, &fence, VK_TRUE, ~((uint64_t) 0)); |
| 505 | assert(err == VK_SUCCESS || err == VK_ERROR_UNAVAILABLE); |
Chia-I Wu | bb57f64 | 2014-11-07 14:30:34 +0800 | [diff] [blame] | 506 | |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 507 | err = vkQueueSubmit(demo->queue, 1, &demo->buffers[demo->current_buffer].cmd, |
| 508 | VK_NULL_HANDLE); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 509 | assert(!err); |
| 510 | |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 511 | err = vkWsiX11QueuePresent(demo->queue, &present, fence); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 512 | assert(!err); |
| 513 | |
| 514 | demo->current_buffer = (demo->current_buffer + 1) % DEMO_BUFFER_COUNT; |
| 515 | } |
| 516 | |
| 517 | static void demo_prepare_buffers(struct demo *demo) |
| 518 | { |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 519 | const VK_WSI_X11_PRESENTABLE_IMAGE_CREATE_INFO presentable_image = { |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 520 | .format = demo->format, |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 521 | .usage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT, |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 522 | .extent = { |
| 523 | .width = demo->width, |
| 524 | .height = demo->height, |
| 525 | }, |
| 526 | .flags = 0, |
| 527 | }; |
Courtney Goeltzenleuchter | 78351a6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 528 | const VkFenceCreateInfo fence = { |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 529 | .sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO, |
Chia-I Wu | bb57f64 | 2014-11-07 14:30:34 +0800 | [diff] [blame] | 530 | .pNext = NULL, |
| 531 | .flags = 0, |
| 532 | }; |
Courtney Goeltzenleuchter | 78351a6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 533 | VkResult err; |
Mark Lobodzinski | c4fffc7 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 534 | uint32_t i; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 535 | |
| 536 | for (i = 0; i < DEMO_BUFFER_COUNT; i++) { |
Courtney Goeltzenleuchter | 78351a6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 537 | VkColorAttachmentViewCreateInfo color_attachment_view = { |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 538 | .sType = VK_STRUCTURE_TYPE_COLOR_ATTACHMENT_VIEW_CREATE_INFO, |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 539 | .pNext = NULL, |
| 540 | .format = demo->format, |
| 541 | .mipLevel = 0, |
| 542 | .baseArraySlice = 0, |
| 543 | .arraySize = 1, |
| 544 | }; |
| 545 | |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 546 | err = vkWsiX11CreatePresentableImage(demo->device, &presentable_image, |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 547 | &demo->buffers[i].image, &demo->buffers[i].mem); |
| 548 | assert(!err); |
| 549 | |
Courtney Goeltzenleuchter | 2c2fbbf | 2015-04-07 17:13:38 -0600 | [diff] [blame] | 550 | demo_add_mem_refs(demo, 1, &demo->buffers[i].mem); |
Courtney Goeltzenleuchter | 1359b7a | 2015-04-02 16:25:42 -0600 | [diff] [blame] | 551 | |
Courtney Goeltzenleuchter | 752eb8e | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 552 | demo_set_image_layout(demo, demo->buffers[i].image, |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 553 | VK_IMAGE_LAYOUT_UNDEFINED, |
| 554 | VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL); |
Courtney Goeltzenleuchter | 752eb8e | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 555 | |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 556 | color_attachment_view.image = demo->buffers[i].image; |
| 557 | |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 558 | err = vkCreateColorAttachmentView(demo->device, |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 559 | &color_attachment_view, &demo->buffers[i].view); |
| 560 | assert(!err); |
Chia-I Wu | bb57f64 | 2014-11-07 14:30:34 +0800 | [diff] [blame] | 561 | |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 562 | err = vkCreateFence(demo->device, |
Chia-I Wu | bb57f64 | 2014-11-07 14:30:34 +0800 | [diff] [blame] | 563 | &fence, &demo->buffers[i].fence); |
| 564 | assert(!err); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 565 | } |
| 566 | } |
| 567 | |
| 568 | static void demo_prepare_depth(struct demo *demo) |
| 569 | { |
Tony Barbour | 72304ef | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 570 | const VkFormat depth_format = VK_FORMAT_D16_UNORM; |
Courtney Goeltzenleuchter | 78351a6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 571 | const VkImageCreateInfo image = { |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 572 | .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO, |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 573 | .pNext = NULL, |
Tony Barbour | 72304ef | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 574 | .imageType = VK_IMAGE_TYPE_2D, |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 575 | .format = depth_format, |
| 576 | .extent = { demo->width, demo->height, 1 }, |
| 577 | .mipLevels = 1, |
| 578 | .arraySize = 1, |
| 579 | .samples = 1, |
Tony Barbour | 72304ef | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 580 | .tiling = VK_IMAGE_TILING_OPTIMAL, |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 581 | .usage = VK_IMAGE_USAGE_DEPTH_STENCIL_BIT, |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 582 | .flags = 0, |
| 583 | }; |
Courtney Goeltzenleuchter | 28c4d5f | 2015-04-14 18:48:46 -0600 | [diff] [blame] | 584 | VkMemoryAllocInfo mem_alloc = { |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 585 | .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO, |
Mark Lobodzinski | a6907f7 | 2015-04-16 08:52:00 -0500 | [diff] [blame] | 586 | .pNext = NULL, |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 587 | .allocationSize = 0, |
Tony Barbour | 72304ef | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 588 | .memProps = VK_MEMORY_PROPERTY_DEVICE_ONLY, |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 589 | .memPriority = VK_MEMORY_PRIORITY_NORMAL, |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 590 | }; |
Courtney Goeltzenleuchter | 78351a6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 591 | VkDepthStencilViewCreateInfo view = { |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 592 | .sType = VK_STRUCTURE_TYPE_DEPTH_STENCIL_VIEW_CREATE_INFO, |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 593 | .pNext = NULL, |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 594 | .image = VK_NULL_HANDLE, |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 595 | .mipLevel = 0, |
| 596 | .baseArraySlice = 0, |
| 597 | .arraySize = 1, |
| 598 | .flags = 0, |
| 599 | }; |
Courtney Goeltzenleuchter | 78351a6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 600 | VkMemoryRequirements *mem_reqs; |
| 601 | size_t mem_reqs_size = sizeof(VkMemoryRequirements); |
Courtney Goeltzenleuchter | 78351a6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 602 | VkResult err; |
Mark Lobodzinski | c4fffc7 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 603 | uint32_t num_allocations = 0; |
| 604 | size_t num_alloc_size = sizeof(num_allocations); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 605 | |
| 606 | demo->depth.format = depth_format; |
| 607 | |
| 608 | /* create image */ |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 609 | err = vkCreateImage(demo->device, &image, |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 610 | &demo->depth.image); |
| 611 | assert(!err); |
| 612 | |
Tony Barbour | 72304ef | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 613 | err = vkGetObjectInfo(demo->depth.image, VK_OBJECT_INFO_TYPE_MEMORY_ALLOCATION_COUNT, &num_alloc_size, &num_allocations); |
Jon Ashburn | b2a6665 | 2015-01-16 09:37:43 -0700 | [diff] [blame] | 614 | assert(!err && num_alloc_size == sizeof(num_allocations)); |
Courtney Goeltzenleuchter | 78351a6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 615 | mem_reqs = malloc(num_allocations * sizeof(VkMemoryRequirements)); |
Tony Barbour | 72304ef | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 616 | demo->depth.mem = malloc(num_allocations * sizeof(VkDeviceMemory)); |
Jon Ashburn | b2a6665 | 2015-01-16 09:37:43 -0700 | [diff] [blame] | 617 | demo->depth.num_mem = num_allocations; |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 618 | err = vkGetObjectInfo(demo->depth.image, |
Tony Barbour | 72304ef | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 619 | VK_OBJECT_INFO_TYPE_MEMORY_REQUIREMENTS, |
Jon Ashburn | b2a6665 | 2015-01-16 09:37:43 -0700 | [diff] [blame] | 620 | &mem_reqs_size, mem_reqs); |
Courtney Goeltzenleuchter | 78351a6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 621 | assert(!err && mem_reqs_size == num_allocations * sizeof(VkMemoryRequirements)); |
Mark Lobodzinski | c4fffc7 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 622 | for (uint32_t i = 0; i < num_allocations; i ++) { |
Jon Ashburn | b2a6665 | 2015-01-16 09:37:43 -0700 | [diff] [blame] | 623 | mem_alloc.allocationSize = mem_reqs[i].size; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 624 | |
Jon Ashburn | b2a6665 | 2015-01-16 09:37:43 -0700 | [diff] [blame] | 625 | /* allocate memory */ |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 626 | err = vkAllocMemory(demo->device, &mem_alloc, |
Jon Ashburn | b2a6665 | 2015-01-16 09:37:43 -0700 | [diff] [blame] | 627 | &(demo->depth.mem[i])); |
| 628 | assert(!err); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 629 | |
Jon Ashburn | b2a6665 | 2015-01-16 09:37:43 -0700 | [diff] [blame] | 630 | /* bind memory */ |
Mark Lobodzinski | da9b109 | 2015-04-16 11:44:05 -0500 | [diff] [blame] | 631 | err = vkQueueBindObjectMemory(demo->queue, demo->depth.image, i, |
Jon Ashburn | b2a6665 | 2015-01-16 09:37:43 -0700 | [diff] [blame] | 632 | demo->depth.mem[i], 0); |
| 633 | assert(!err); |
| 634 | } |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 635 | |
Courtney Goeltzenleuchter | 752eb8e | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 636 | demo_set_image_layout(demo, demo->depth.image, |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 637 | VK_IMAGE_LAYOUT_UNDEFINED, |
| 638 | VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL); |
Courtney Goeltzenleuchter | 752eb8e | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 639 | |
Courtney Goeltzenleuchter | 2c2fbbf | 2015-04-07 17:13:38 -0600 | [diff] [blame] | 640 | demo_add_mem_refs(demo, demo->depth.num_mem, demo->depth.mem); |
Courtney Goeltzenleuchter | 752eb8e | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 641 | |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 642 | /* create image view */ |
| 643 | view.image = demo->depth.image; |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 644 | err = vkCreateDepthStencilView(demo->device, &view, |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 645 | &demo->depth.view); |
| 646 | assert(!err); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 647 | } |
| 648 | |
Courtney Goeltzenleuchter | 6836939 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 649 | /** loadTexture |
| 650 | * loads a png file into an memory object, using cstdio , libpng. |
| 651 | * |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 652 | * \param demo : Needed to access VK calls |
Courtney Goeltzenleuchter | 6836939 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 653 | * \param filename : the png file to be loaded |
| 654 | * \param width : width of png, to be updated as a side effect of this function |
| 655 | * \param height : height of png, to be updated as a side effect of this function |
| 656 | * |
| 657 | * \return bool : an opengl texture id. true if successful?, |
| 658 | * should be validated by the client of this function. |
| 659 | * |
| 660 | * Source: http://en.wikibooks.org/wiki/OpenGL_Programming/Intermediate/Textures |
| 661 | * Modified to copy image to memory |
| 662 | * |
| 663 | */ |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 664 | bool loadTexture(const char *filename, uint8_t *rgba_data, |
Courtney Goeltzenleuchter | 78351a6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 665 | VkSubresourceLayout *layout, |
Mark Lobodzinski | c4fffc7 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 666 | int32_t *width, int32_t *height) |
Courtney Goeltzenleuchter | 6836939 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 667 | { |
| 668 | //header for testing if it is a png |
| 669 | png_byte header[8]; |
Ian Elliott | 1e42dff | 2015-02-13 14:29:21 -0700 | [diff] [blame] | 670 | int is_png, bit_depth, color_type,rowbytes; |
| 671 | png_uint_32 i, twidth, theight; |
Courtney Goeltzenleuchter | 6836939 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 672 | png_structp png_ptr; |
| 673 | png_infop info_ptr, end_info; |
| 674 | png_byte *image_data; |
| 675 | png_bytep *row_pointers; |
| 676 | |
| 677 | //open file as binary |
| 678 | FILE *fp = fopen(filename, "rb"); |
| 679 | if (!fp) { |
| 680 | return false; |
| 681 | } |
| 682 | |
| 683 | //read the header |
| 684 | fread(header, 1, 8, fp); |
| 685 | |
| 686 | //test if png |
| 687 | is_png = !png_sig_cmp(header, 0, 8); |
| 688 | if (!is_png) { |
| 689 | fclose(fp); |
| 690 | return false; |
| 691 | } |
| 692 | |
| 693 | //create png struct |
| 694 | png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, |
| 695 | NULL, NULL); |
| 696 | if (!png_ptr) { |
| 697 | fclose(fp); |
| 698 | return (false); |
| 699 | } |
| 700 | |
| 701 | //create png info struct |
| 702 | info_ptr = png_create_info_struct(png_ptr); |
| 703 | if (!info_ptr) { |
| 704 | png_destroy_read_struct(&png_ptr, (png_infopp) NULL, (png_infopp) NULL); |
| 705 | fclose(fp); |
| 706 | return (false); |
| 707 | } |
| 708 | |
| 709 | //create png info struct |
| 710 | end_info = png_create_info_struct(png_ptr); |
| 711 | if (!end_info) { |
| 712 | png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp) NULL); |
| 713 | fclose(fp); |
| 714 | return (false); |
| 715 | } |
| 716 | |
| 717 | //png error stuff, not sure libpng man suggests this. |
| 718 | if (setjmp(png_jmpbuf(png_ptr))) { |
| 719 | png_destroy_read_struct(&png_ptr, &info_ptr, &end_info); |
| 720 | fclose(fp); |
| 721 | return (false); |
| 722 | } |
| 723 | |
| 724 | //init png reading |
| 725 | png_init_io(png_ptr, fp); |
| 726 | |
| 727 | //let libpng know you already read the first 8 bytes |
| 728 | png_set_sig_bytes(png_ptr, 8); |
| 729 | |
| 730 | // read all the info up to the image data |
| 731 | png_read_info(png_ptr, info_ptr); |
| 732 | |
| 733 | // get info about png |
| 734 | png_get_IHDR(png_ptr, info_ptr, &twidth, &theight, &bit_depth, &color_type, |
| 735 | NULL, NULL, NULL); |
| 736 | |
| 737 | //update width and height based on png info |
| 738 | *width = twidth; |
| 739 | *height = theight; |
| 740 | |
| 741 | // Require that incoming texture be 8bits per color component |
| 742 | // and 4 components (RGBA). |
| 743 | if (png_get_bit_depth(png_ptr, info_ptr) != 8 || |
| 744 | png_get_channels(png_ptr, info_ptr) != 4) { |
| 745 | return false; |
| 746 | } |
| 747 | |
| 748 | if (rgba_data == NULL) { |
| 749 | // If data pointer is null, we just want the width & height |
| 750 | // clean up memory and close stuff |
| 751 | png_destroy_read_struct(&png_ptr, &info_ptr, &end_info); |
| 752 | fclose(fp); |
| 753 | |
| 754 | return true; |
| 755 | } |
| 756 | |
| 757 | // Update the png info struct. |
| 758 | png_read_update_info(png_ptr, info_ptr); |
| 759 | |
| 760 | // Row size in bytes. |
| 761 | rowbytes = png_get_rowbytes(png_ptr, info_ptr); |
| 762 | |
| 763 | // Allocate the image_data as a big block, to be given to opengl |
| 764 | image_data = (png_byte *)malloc(rowbytes * theight * sizeof(png_byte)); |
| 765 | if (!image_data) { |
| 766 | //clean up memory and close stuff |
| 767 | png_destroy_read_struct(&png_ptr, &info_ptr, &end_info); |
| 768 | fclose(fp); |
| 769 | return false; |
| 770 | } |
| 771 | |
| 772 | // row_pointers is for pointing to image_data for reading the png with libpng |
| 773 | row_pointers = (png_bytep *)malloc(theight * sizeof(png_bytep)); |
| 774 | if (!row_pointers) { |
| 775 | //clean up memory and close stuff |
| 776 | png_destroy_read_struct(&png_ptr, &info_ptr, &end_info); |
| 777 | // delete[] image_data; |
| 778 | fclose(fp); |
| 779 | return false; |
| 780 | } |
| 781 | // set the individual row_pointers to point at the correct offsets of image_data |
| 782 | for (i = 0; i < theight; ++i) |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 783 | row_pointers[theight - 1 - i] = rgba_data + i * layout->rowPitch; |
Courtney Goeltzenleuchter | 6836939 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 784 | |
| 785 | // read the png into image_data through row_pointers |
| 786 | png_read_image(png_ptr, row_pointers); |
| 787 | |
| 788 | // clean up memory and close stuff |
| 789 | png_destroy_read_struct(&png_ptr, &info_ptr, &end_info); |
| 790 | free(row_pointers); |
| 791 | free(image_data); |
| 792 | fclose(fp); |
| 793 | |
| 794 | return true; |
| 795 | } |
Courtney Goeltzenleuchter | 6836939 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 796 | |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 797 | static void demo_prepare_texture_image(struct demo *demo, |
| 798 | const char *filename, |
Courtney Goeltzenleuchter | 752eb8e | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 799 | struct texture_object *tex_obj, |
Courtney Goeltzenleuchter | 78351a6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 800 | VkImageTiling tiling, |
| 801 | VkFlags mem_props) |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 802 | { |
Tony Barbour | 72304ef | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 803 | const VkFormat tex_format = VK_FORMAT_B8G8R8A8_UNORM; |
Mark Lobodzinski | c4fffc7 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 804 | int32_t tex_width; |
| 805 | int32_t tex_height; |
Courtney Goeltzenleuchter | 78351a6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 806 | VkResult err; |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 807 | |
| 808 | err = loadTexture(filename, NULL, NULL, &tex_width, &tex_height); |
| 809 | assert(err); |
| 810 | |
Courtney Goeltzenleuchter | 752eb8e | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 811 | tex_obj->tex_width = tex_width; |
| 812 | tex_obj->tex_height = tex_height; |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 813 | |
Courtney Goeltzenleuchter | 78351a6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 814 | const VkImageCreateInfo image_create_info = { |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 815 | .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO, |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 816 | .pNext = NULL, |
Tony Barbour | 72304ef | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 817 | .imageType = VK_IMAGE_TYPE_2D, |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 818 | .format = tex_format, |
| 819 | .extent = { tex_width, tex_height, 1 }, |
| 820 | .mipLevels = 1, |
| 821 | .arraySize = 1, |
| 822 | .samples = 1, |
| 823 | .tiling = tiling, |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 824 | .usage = VK_IMAGE_USAGE_TRANSFER_SOURCE_BIT, |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 825 | .flags = 0, |
| 826 | }; |
Courtney Goeltzenleuchter | 28c4d5f | 2015-04-14 18:48:46 -0600 | [diff] [blame] | 827 | VkMemoryAllocInfo mem_alloc = { |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 828 | .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO, |
Mark Lobodzinski | a6907f7 | 2015-04-16 08:52:00 -0500 | [diff] [blame] | 829 | .pNext = NULL, |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 830 | .allocationSize = 0, |
| 831 | .memProps = mem_props, |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 832 | .memPriority = VK_MEMORY_PRIORITY_NORMAL, |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 833 | }; |
| 834 | |
Courtney Goeltzenleuchter | 78351a6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 835 | VkMemoryRequirements *mem_reqs; |
| 836 | size_t mem_reqs_size = sizeof(VkMemoryRequirements); |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 837 | uint32_t num_allocations = 0; |
| 838 | size_t num_alloc_size = sizeof(num_allocations); |
| 839 | |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 840 | err = vkCreateImage(demo->device, &image_create_info, |
Courtney Goeltzenleuchter | 752eb8e | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 841 | &tex_obj->image); |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 842 | assert(!err); |
| 843 | |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 844 | err = vkGetObjectInfo(tex_obj->image, |
Tony Barbour | 72304ef | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 845 | VK_OBJECT_INFO_TYPE_MEMORY_ALLOCATION_COUNT, |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 846 | &num_alloc_size, &num_allocations); |
| 847 | assert(!err && num_alloc_size == sizeof(num_allocations)); |
Courtney Goeltzenleuchter | 78351a6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 848 | mem_reqs = malloc(num_allocations * sizeof(VkMemoryRequirements)); |
Tony Barbour | 72304ef | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 849 | tex_obj->mem = malloc(num_allocations * sizeof(VkDeviceMemory)); |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 850 | err = vkGetObjectInfo(tex_obj->image, |
Tony Barbour | 72304ef | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 851 | VK_OBJECT_INFO_TYPE_MEMORY_REQUIREMENTS, |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 852 | &mem_reqs_size, mem_reqs); |
Courtney Goeltzenleuchter | 78351a6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 853 | assert(!err && mem_reqs_size == num_allocations * sizeof(VkMemoryRequirements)); |
Tony Barbour | 72304ef | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 854 | mem_alloc.memProps = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT; |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 855 | for (uint32_t j = 0; j < num_allocations; j ++) { |
Piers Daniell | 735ee53 | 2015-02-23 16:23:13 -0700 | [diff] [blame] | 856 | mem_alloc.allocationSize = mem_reqs[j].size; |
| 857 | |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 858 | /* allocate memory */ |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 859 | err = vkAllocMemory(demo->device, &mem_alloc, |
Courtney Goeltzenleuchter | 752eb8e | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 860 | &(tex_obj->mem[j])); |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 861 | assert(!err); |
| 862 | |
| 863 | /* bind memory */ |
Mark Lobodzinski | da9b109 | 2015-04-16 11:44:05 -0500 | [diff] [blame] | 864 | err = vkQueueBindObjectMemory(demo->queue, tex_obj->image, j, tex_obj->mem[j], 0); |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 865 | assert(!err); |
| 866 | } |
| 867 | free(mem_reqs); |
| 868 | mem_reqs = NULL; |
| 869 | |
Courtney Goeltzenleuchter | 752eb8e | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 870 | tex_obj->num_mem = num_allocations; |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 871 | |
Tony Barbour | 72304ef | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 872 | if (mem_props & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) { |
Courtney Goeltzenleuchter | 78351a6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 873 | const VkImageSubresource subres = { |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 874 | .aspect = VK_IMAGE_ASPECT_COLOR, |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 875 | .mipLevel = 0, |
| 876 | .arraySlice = 0, |
| 877 | }; |
Courtney Goeltzenleuchter | 78351a6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 878 | VkSubresourceLayout layout; |
| 879 | size_t layout_size = sizeof(VkSubresourceLayout); |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 880 | void *data; |
| 881 | |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 882 | err = vkGetImageSubresourceInfo(tex_obj->image, &subres, |
Tony Barbour | 72304ef | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 883 | VK_SUBRESOURCE_INFO_TYPE_LAYOUT, |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 884 | &layout_size, &layout); |
| 885 | assert(!err && layout_size == sizeof(layout)); |
| 886 | /* Linear texture must be within a single memory object */ |
| 887 | assert(num_allocations == 1); |
| 888 | |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 889 | err = vkMapMemory(tex_obj->mem[0], 0, &data); |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 890 | assert(!err); |
| 891 | |
| 892 | if (!loadTexture(filename, data, &layout, &tex_width, &tex_height)) { |
| 893 | fprintf(stderr, "Error loading texture: %s\n", filename); |
| 894 | } |
| 895 | |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 896 | err = vkUnmapMemory(tex_obj->mem[0]); |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 897 | assert(!err); |
| 898 | } |
Courtney Goeltzenleuchter | 752eb8e | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 899 | |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 900 | tex_obj->imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL; |
Courtney Goeltzenleuchter | 752eb8e | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 901 | demo_set_image_layout(demo, tex_obj->image, |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 902 | VK_IMAGE_LAYOUT_UNDEFINED, |
Courtney Goeltzenleuchter | 752eb8e | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 903 | tex_obj->imageLayout); |
| 904 | /* setting the image layout does not reference the actual memory so no need to add a mem ref */ |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 905 | } |
| 906 | |
Mark Lobodzinski | da9b109 | 2015-04-16 11:44:05 -0500 | [diff] [blame] | 907 | static void demo_destroy_texture_image(struct demo *demo, struct texture_object *tex_objs) |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 908 | { |
| 909 | /* clean up staging resources */ |
| 910 | for (uint32_t j = 0; j < tex_objs->num_mem; j ++) { |
Mark Lobodzinski | da9b109 | 2015-04-16 11:44:05 -0500 | [diff] [blame] | 911 | vkQueueBindObjectMemory(demo->queue, tex_objs->image, j, VK_NULL_HANDLE, 0); |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 912 | vkFreeMemory(tex_objs->mem[j]); |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 913 | } |
| 914 | |
| 915 | free(tex_objs->mem); |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 916 | vkDestroyObject(tex_objs->image); |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 917 | } |
| 918 | |
| 919 | static void demo_prepare_textures(struct demo *demo) |
| 920 | { |
Tony Barbour | 72304ef | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 921 | const VkFormat tex_format = VK_FORMAT_R8G8B8A8_UNORM; |
Courtney Goeltzenleuchter | 78351a6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 922 | VkFormatProperties props; |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 923 | size_t size = sizeof(props); |
Courtney Goeltzenleuchter | 78351a6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 924 | VkResult err; |
Mark Lobodzinski | c4fffc7 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 925 | uint32_t i; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 926 | |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 927 | err = vkGetFormatInfo(demo->device, tex_format, |
Tony Barbour | 72304ef | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 928 | VK_FORMAT_INFO_TYPE_PROPERTIES, |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 929 | &size, &props); |
| 930 | assert(!err); |
| 931 | |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 932 | for (i = 0; i < DEMO_TEXTURE_COUNT; i++) { |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 933 | |
Tony Barbour | 72304ef | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 934 | if (props.linearTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT && !demo->use_staging_buffer) { |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 935 | /* Device can texture using linear textures */ |
| 936 | demo_prepare_texture_image(demo, tex_files[i], &demo->textures[i], |
Tony Barbour | 72304ef | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 937 | VK_IMAGE_TILING_LINEAR, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT); |
| 938 | } else if (props.optimalTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT) { |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 939 | /* Must use staging buffer to copy linear texture to optimized */ |
Courtney Goeltzenleuchter | 752eb8e | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 940 | struct texture_object staging_texture; |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 941 | |
| 942 | memset(&staging_texture, 0, sizeof(staging_texture)); |
| 943 | demo_prepare_texture_image(demo, tex_files[i], &staging_texture, |
Tony Barbour | 72304ef | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 944 | VK_IMAGE_TILING_LINEAR, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT); |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 945 | |
| 946 | demo_prepare_texture_image(demo, tex_files[i], &demo->textures[i], |
Tony Barbour | 72304ef | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 947 | VK_IMAGE_TILING_OPTIMAL, VK_MEMORY_PROPERTY_DEVICE_ONLY); |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 948 | |
Courtney Goeltzenleuchter | 752eb8e | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 949 | demo_set_image_layout(demo, staging_texture.image, |
| 950 | staging_texture.imageLayout, |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 951 | VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL); |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 952 | |
Courtney Goeltzenleuchter | 752eb8e | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 953 | demo_set_image_layout(demo, demo->textures[i].image, |
| 954 | demo->textures[i].imageLayout, |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 955 | VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL); |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 956 | |
Courtney Goeltzenleuchter | 78351a6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 957 | VkImageCopy copy_region = { |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 958 | .srcSubresource = { VK_IMAGE_ASPECT_COLOR, 0, 0 }, |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 959 | .srcOffset = { 0, 0, 0 }, |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 960 | .destSubresource = { VK_IMAGE_ASPECT_COLOR, 0, 0 }, |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 961 | .destOffset = { 0, 0, 0 }, |
| 962 | .extent = { staging_texture.tex_width, staging_texture.tex_height, 1 }, |
| 963 | }; |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 964 | vkCmdCopyImage(demo->cmd, |
| 965 | staging_texture.image, VK_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL, |
| 966 | demo->textures[i].image, VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL, |
Courtney Goeltzenleuchter | 8e89a31 | 2015-03-25 11:25:10 -0600 | [diff] [blame] | 967 | 1, ©_region); |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 968 | |
Courtney Goeltzenleuchter | 2c2fbbf | 2015-04-07 17:13:38 -0600 | [diff] [blame] | 969 | demo_add_mem_refs(demo, staging_texture.num_mem, staging_texture.mem); |
| 970 | demo_add_mem_refs(demo, demo->textures[i].num_mem, demo->textures[i].mem); |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 971 | |
Courtney Goeltzenleuchter | 752eb8e | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 972 | demo_set_image_layout(demo, demo->textures[i].image, |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 973 | VK_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL, |
Courtney Goeltzenleuchter | 752eb8e | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 974 | demo->textures[i].imageLayout); |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 975 | |
Courtney Goeltzenleuchter | 752eb8e | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 976 | demo_flush_init_cmd(demo); |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 977 | |
Mark Lobodzinski | da9b109 | 2015-04-16 11:44:05 -0500 | [diff] [blame] | 978 | demo_destroy_texture_image(demo, &staging_texture); |
Courtney Goeltzenleuchter | 1359b7a | 2015-04-02 16:25:42 -0600 | [diff] [blame] | 979 | demo_remove_mem_refs(demo, staging_texture.num_mem, staging_texture.mem); |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 980 | } else { |
Tony Barbour | 72304ef | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 981 | /* Can't support VK_FORMAT_B8G8R8A8_UNORM !? */ |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 982 | assert(!"No support for tB8G8R8A8_UNORM as texture image format"); |
| 983 | } |
| 984 | |
Courtney Goeltzenleuchter | 78351a6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 985 | const VkSamplerCreateInfo sampler = { |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 986 | .sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO, |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 987 | .pNext = NULL, |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 988 | .magFilter = VK_TEX_FILTER_NEAREST, |
| 989 | .minFilter = VK_TEX_FILTER_NEAREST, |
Tony Barbour | 72304ef | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 990 | .mipMode = VK_TEX_MIPMAP_MODE_BASE, |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 991 | .addressU = VK_TEX_ADDRESS_CLAMP, |
| 992 | .addressV = VK_TEX_ADDRESS_CLAMP, |
| 993 | .addressW = VK_TEX_ADDRESS_CLAMP, |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 994 | .mipLodBias = 0.0f, |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 995 | .maxAnisotropy = 1, |
Tony Barbour | 72304ef | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 996 | .compareOp = VK_COMPARE_OP_NEVER, |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 997 | .minLod = 0.0f, |
| 998 | .maxLod = 0.0f, |
Tony Barbour | 72304ef | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 999 | .borderColor = VK_BORDER_COLOR_OPAQUE_WHITE, |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1000 | }; |
Courtney Goeltzenleuchter | 6836939 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 1001 | |
Courtney Goeltzenleuchter | 78351a6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 1002 | VkImageViewCreateInfo view = { |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1003 | .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO, |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1004 | .pNext = NULL, |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1005 | .image = VK_NULL_HANDLE, |
Tony Barbour | 72304ef | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 1006 | .viewType = VK_IMAGE_VIEW_TYPE_2D, |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 1007 | .format = tex_format, |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1008 | .channels = { VK_CHANNEL_SWIZZLE_R, |
| 1009 | VK_CHANNEL_SWIZZLE_G, |
| 1010 | VK_CHANNEL_SWIZZLE_B, |
| 1011 | VK_CHANNEL_SWIZZLE_A, }, |
| 1012 | .subresourceRange = { VK_IMAGE_ASPECT_COLOR, 0, 1, 0, 1 }, |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1013 | .minLod = 0.0f, |
| 1014 | }; |
Jon Ashburn | b2a6665 | 2015-01-16 09:37:43 -0700 | [diff] [blame] | 1015 | |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1016 | /* create sampler */ |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1017 | err = vkCreateSampler(demo->device, &sampler, |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1018 | &demo->textures[i].sampler); |
| 1019 | assert(!err); |
| 1020 | |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1021 | /* create image view */ |
| 1022 | view.image = demo->textures[i].image; |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1023 | err = vkCreateImageView(demo->device, &view, |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1024 | &demo->textures[i].view); |
| 1025 | assert(!err); |
| 1026 | } |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1027 | } |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1028 | |
Courtney Goeltzenleuchter | 4984d15 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 1029 | void demo_prepare_cube_data_buffer(struct demo *demo) |
| 1030 | { |
Courtney Goeltzenleuchter | 28c4d5f | 2015-04-14 18:48:46 -0600 | [diff] [blame] | 1031 | VkBufferCreateInfo buf_info; |
| 1032 | VkBufferViewCreateInfo view_info; |
Courtney Goeltzenleuchter | 28c4d5f | 2015-04-14 18:48:46 -0600 | [diff] [blame] | 1033 | VkMemoryAllocInfo alloc_info = { |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1034 | .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOC_INFO, |
Mark Lobodzinski | a6907f7 | 2015-04-16 08:52:00 -0500 | [diff] [blame] | 1035 | .pNext = NULL, |
Jon Ashburn | ae7c21c | 2015-01-19 15:00:26 -0700 | [diff] [blame] | 1036 | .allocationSize = 0, |
Tony Barbour | 72304ef | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 1037 | .memProps = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1038 | .memPriority = VK_MEMORY_PRIORITY_NORMAL, |
Jon Ashburn | ae7c21c | 2015-01-19 15:00:26 -0700 | [diff] [blame] | 1039 | }; |
Courtney Goeltzenleuchter | 78351a6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 1040 | VkMemoryRequirements *mem_reqs; |
| 1041 | size_t mem_reqs_size = sizeof(VkMemoryRequirements); |
Mark Lobodzinski | c4fffc7 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 1042 | uint32_t num_allocations = 0; |
| 1043 | size_t num_alloc_size = sizeof(num_allocations); |
| 1044 | uint8_t *pData; |
Courtney Goeltzenleuchter | 4984d15 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 1045 | int i; |
| 1046 | mat4x4 MVP, VP; |
Courtney Goeltzenleuchter | 78351a6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 1047 | VkResult err; |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1048 | struct vktexcube_vs_uniform data; |
Courtney Goeltzenleuchter | 4984d15 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 1049 | |
Courtney Goeltzenleuchter | 4984d15 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 1050 | mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix); |
Courtney Goeltzenleuchter | 4984d15 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 1051 | mat4x4_mul(MVP, VP, demo->model_matrix); |
| 1052 | memcpy(data.mvp, MVP, sizeof(MVP)); |
Courtney Goeltzenleuchter | 8879d3a | 2014-10-29 08:29:35 -0600 | [diff] [blame] | 1053 | // dumpMatrix("MVP", MVP); |
Courtney Goeltzenleuchter | 4984d15 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 1054 | |
| 1055 | for (i=0; i<12*3; i++) { |
| 1056 | data.position[i][0] = g_vertex_buffer_data[i*3]; |
| 1057 | data.position[i][1] = g_vertex_buffer_data[i*3+1]; |
| 1058 | data.position[i][2] = g_vertex_buffer_data[i*3+2]; |
| 1059 | data.position[i][3] = 1.0f; |
| 1060 | data.attr[i][0] = g_uv_buffer_data[2*i]; |
| 1061 | data.attr[i][1] = g_uv_buffer_data[2*i + 1]; |
| 1062 | data.attr[i][2] = 0; |
| 1063 | data.attr[i][3] = 0; |
| 1064 | } |
| 1065 | |
Chia-I Wu | 8ea21c7 | 2015-01-01 07:55:04 +0800 | [diff] [blame] | 1066 | memset(&buf_info, 0, sizeof(buf_info)); |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1067 | buf_info.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO; |
Chia-I Wu | 8ea21c7 | 2015-01-01 07:55:04 +0800 | [diff] [blame] | 1068 | buf_info.size = sizeof(data); |
Courtney Goeltzenleuchter | 42a078d | 2015-04-15 15:29:59 -0600 | [diff] [blame] | 1069 | buf_info.usage = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT; |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1070 | err = vkCreateBuffer(demo->device, &buf_info, &demo->uniform_data.buf); |
Chia-I Wu | 8ea21c7 | 2015-01-01 07:55:04 +0800 | [diff] [blame] | 1071 | assert(!err); |
| 1072 | |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1073 | err = vkGetObjectInfo(demo->uniform_data.buf, |
Tony Barbour | 72304ef | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 1074 | VK_OBJECT_INFO_TYPE_MEMORY_ALLOCATION_COUNT, |
Jon Ashburn | ae7c21c | 2015-01-19 15:00:26 -0700 | [diff] [blame] | 1075 | &num_alloc_size, &num_allocations); |
| 1076 | assert(!err && num_alloc_size == sizeof(num_allocations)); |
Courtney Goeltzenleuchter | 78351a6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 1077 | mem_reqs = malloc(num_allocations * sizeof(VkMemoryRequirements)); |
Tony Barbour | 72304ef | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 1078 | demo->uniform_data.mem = malloc(num_allocations * sizeof(VkDeviceMemory)); |
Jon Ashburn | ae7c21c | 2015-01-19 15:00:26 -0700 | [diff] [blame] | 1079 | demo->uniform_data.num_mem = num_allocations; |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1080 | err = vkGetObjectInfo(demo->uniform_data.buf, |
Tony Barbour | 72304ef | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 1081 | VK_OBJECT_INFO_TYPE_MEMORY_REQUIREMENTS, |
Jon Ashburn | ae7c21c | 2015-01-19 15:00:26 -0700 | [diff] [blame] | 1082 | &mem_reqs_size, mem_reqs); |
| 1083 | assert(!err && mem_reqs_size == num_allocations * sizeof(*mem_reqs)); |
Mark Lobodzinski | c4fffc7 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 1084 | for (uint32_t i = 0; i < num_allocations; i ++) { |
Jon Ashburn | ae7c21c | 2015-01-19 15:00:26 -0700 | [diff] [blame] | 1085 | alloc_info.allocationSize = mem_reqs[i].size; |
Chia-I Wu | 8ea21c7 | 2015-01-01 07:55:04 +0800 | [diff] [blame] | 1086 | |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1087 | err = vkAllocMemory(demo->device, &alloc_info, &(demo->uniform_data.mem[i])); |
Jon Ashburn | ae7c21c | 2015-01-19 15:00:26 -0700 | [diff] [blame] | 1088 | assert(!err); |
Courtney Goeltzenleuchter | 4984d15 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 1089 | |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1090 | err = vkMapMemory(demo->uniform_data.mem[i], 0, (void **) &pData); |
Jon Ashburn | ae7c21c | 2015-01-19 15:00:26 -0700 | [diff] [blame] | 1091 | assert(!err); |
Courtney Goeltzenleuchter | 4984d15 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 1092 | |
Piers Daniell | 735ee53 | 2015-02-23 16:23:13 -0700 | [diff] [blame] | 1093 | memcpy(pData, &data, (size_t)alloc_info.allocationSize); |
Courtney Goeltzenleuchter | 4984d15 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 1094 | |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1095 | err = vkUnmapMemory(demo->uniform_data.mem[i]); |
Jon Ashburn | ae7c21c | 2015-01-19 15:00:26 -0700 | [diff] [blame] | 1096 | assert(!err); |
Courtney Goeltzenleuchter | 4984d15 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 1097 | |
Mark Lobodzinski | da9b109 | 2015-04-16 11:44:05 -0500 | [diff] [blame] | 1098 | err = vkQueueBindObjectMemory(demo->queue, demo->uniform_data.buf, i, |
Jon Ashburn | ae7c21c | 2015-01-19 15:00:26 -0700 | [diff] [blame] | 1099 | demo->uniform_data.mem[i], 0); |
| 1100 | assert(!err); |
| 1101 | } |
Courtney Goeltzenleuchter | 2c2fbbf | 2015-04-07 17:13:38 -0600 | [diff] [blame] | 1102 | demo_add_mem_refs(demo, demo->uniform_data.num_mem, demo->uniform_data.mem); |
Chia-I Wu | 8ea21c7 | 2015-01-01 07:55:04 +0800 | [diff] [blame] | 1103 | |
| 1104 | memset(&view_info, 0, sizeof(view_info)); |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1105 | view_info.sType = VK_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO; |
Chia-I Wu | 8ea21c7 | 2015-01-01 07:55:04 +0800 | [diff] [blame] | 1106 | view_info.buffer = demo->uniform_data.buf; |
Tony Barbour | 72304ef | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 1107 | view_info.viewType = VK_BUFFER_VIEW_TYPE_RAW; |
Chia-I Wu | 8ea21c7 | 2015-01-01 07:55:04 +0800 | [diff] [blame] | 1108 | view_info.offset = 0; |
| 1109 | view_info.range = sizeof(data); |
| 1110 | |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1111 | err = vkCreateBufferView(demo->device, &view_info, &demo->uniform_data.view); |
Chia-I Wu | 8ea21c7 | 2015-01-01 07:55:04 +0800 | [diff] [blame] | 1112 | assert(!err); |
| 1113 | |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1114 | demo->uniform_data.attach.sType = VK_STRUCTURE_TYPE_BUFFER_VIEW_ATTACH_INFO; |
Chia-I Wu | 8ea21c7 | 2015-01-01 07:55:04 +0800 | [diff] [blame] | 1115 | demo->uniform_data.attach.view = demo->uniform_data.view; |
Courtney Goeltzenleuchter | 4984d15 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 1116 | } |
| 1117 | |
Chia-I Wu | 6a3c897 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 1118 | static void demo_prepare_descriptor_layout(struct demo *demo) |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1119 | { |
Courtney Goeltzenleuchter | 78351a6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 1120 | const VkDescriptorSetLayoutBinding layout_bindings[2] = { |
Chia-I Wu | a2aa863 | 2015-03-26 15:04:41 +0800 | [diff] [blame] | 1121 | [0] = { |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1122 | .descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, |
Chia-I Wu | a2aa863 | 2015-03-26 15:04:41 +0800 | [diff] [blame] | 1123 | .count = 1, |
Tony Barbour | 72304ef | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 1124 | .stageFlags = VK_SHADER_STAGE_VERTEX_BIT, |
Chia-I Wu | 91e8e21 | 2015-03-27 12:56:09 +0800 | [diff] [blame] | 1125 | .pImmutableSamplers = NULL, |
Chia-I Wu | a2aa863 | 2015-03-26 15:04:41 +0800 | [diff] [blame] | 1126 | }, |
| 1127 | [1] = { |
Courtney Goeltzenleuchter | 42a078d | 2015-04-15 15:29:59 -0600 | [diff] [blame] | 1128 | .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, |
Chia-I Wu | a2aa863 | 2015-03-26 15:04:41 +0800 | [diff] [blame] | 1129 | .count = DEMO_TEXTURE_COUNT, |
Tony Barbour | 72304ef | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 1130 | .stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT, |
Chia-I Wu | 91e8e21 | 2015-03-27 12:56:09 +0800 | [diff] [blame] | 1131 | .pImmutableSamplers = NULL, |
Chia-I Wu | a2aa863 | 2015-03-26 15:04:41 +0800 | [diff] [blame] | 1132 | }, |
| 1133 | }; |
Courtney Goeltzenleuchter | 78351a6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 1134 | const VkDescriptorSetLayoutCreateInfo descriptor_layout = { |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1135 | .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO, |
Chia-I Wu | 6a3c897 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 1136 | .pNext = NULL, |
Chia-I Wu | a2aa863 | 2015-03-26 15:04:41 +0800 | [diff] [blame] | 1137 | .count = 2, |
| 1138 | .pBinding = layout_bindings, |
Chia-I Wu | 6a3c897 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 1139 | }; |
Courtney Goeltzenleuchter | 78351a6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 1140 | VkResult err; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1141 | |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1142 | err = vkCreateDescriptorSetLayout(demo->device, |
Chia-I Wu | b58c24a | 2015-03-26 15:27:55 +0800 | [diff] [blame] | 1143 | &descriptor_layout, &demo->desc_layout); |
| 1144 | assert(!err); |
| 1145 | |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1146 | err = vkCreateDescriptorSetLayoutChain(demo->device, |
Chia-I Wu | b58c24a | 2015-03-26 15:27:55 +0800 | [diff] [blame] | 1147 | 1, &demo->desc_layout, &demo->desc_layout_chain); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1148 | assert(!err); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1149 | } |
| 1150 | |
Courtney Goeltzenleuchter | 78351a6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 1151 | static VkShader demo_prepare_shader(struct demo *demo, |
Tony Barbour | 72304ef | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 1152 | VkShaderStage stage, |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1153 | const void *code, |
Mark Lobodzinski | c4fffc7 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 1154 | size_t size) |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1155 | { |
Courtney Goeltzenleuchter | 78351a6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 1156 | VkShaderCreateInfo createInfo; |
| 1157 | VkShader shader; |
| 1158 | VkResult err; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1159 | |
Courtney Goeltzenleuchter | 97db1c1 | 2014-10-30 15:14:16 -0600 | [diff] [blame] | 1160 | |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1161 | createInfo.sType = VK_STRUCTURE_TYPE_SHADER_CREATE_INFO; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1162 | createInfo.pNext = NULL; |
| 1163 | |
Cody Northrop | 8b12a1f | 2015-03-17 15:55:58 -0600 | [diff] [blame] | 1164 | #ifdef EXTERNAL_SPV |
Courtney Goeltzenleuchter | 97db1c1 | 2014-10-30 15:14:16 -0600 | [diff] [blame] | 1165 | createInfo.codeSize = size; |
| 1166 | createInfo.pCode = code; |
Courtney Goeltzenleuchter | 6836939 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 1167 | createInfo.flags = 0; |
| 1168 | |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1169 | err = vkCreateShader(demo->device, &createInfo, &shader); |
Courtney Goeltzenleuchter | 6836939 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 1170 | if (err) { |
| 1171 | free((void *) createInfo.pCode); |
| 1172 | } |
| 1173 | #else |
Cody Northrop | 8b12a1f | 2015-03-17 15:55:58 -0600 | [diff] [blame] | 1174 | // Create fake SPV structure to feed GLSL |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1175 | // to the driver "under the covers" |
| 1176 | createInfo.codeSize = 3 * sizeof(uint32_t) + size + 1; |
| 1177 | createInfo.pCode = malloc(createInfo.codeSize); |
| 1178 | createInfo.flags = 0; |
| 1179 | |
Tony Barbour | 72304ef | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 1180 | /* try version 0 first: VkShaderStage followed by GLSL */ |
Cody Northrop | 8b12a1f | 2015-03-17 15:55:58 -0600 | [diff] [blame] | 1181 | ((uint32_t *) createInfo.pCode)[0] = ICD_SPV_MAGIC; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1182 | ((uint32_t *) createInfo.pCode)[1] = 0; |
| 1183 | ((uint32_t *) createInfo.pCode)[2] = stage; |
| 1184 | memcpy(((uint32_t *) createInfo.pCode + 3), code, size + 1); |
| 1185 | |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1186 | err = vkCreateShader(demo->device, &createInfo, &shader); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1187 | if (err) { |
| 1188 | free((void *) createInfo.pCode); |
Courtney Goeltzenleuchter | fc3b953 | 2014-10-28 10:32:57 -0600 | [diff] [blame] | 1189 | return NULL; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1190 | } |
Courtney Goeltzenleuchter | 6836939 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 1191 | #endif |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1192 | |
| 1193 | return shader; |
| 1194 | } |
| 1195 | |
Cody Northrop | 8b12a1f | 2015-03-17 15:55:58 -0600 | [diff] [blame] | 1196 | char *demo_read_spv(const char *filename, size_t *psize) |
Courtney Goeltzenleuchter | 97db1c1 | 2014-10-30 15:14:16 -0600 | [diff] [blame] | 1197 | { |
| 1198 | long int size; |
| 1199 | void *shader_code; |
| 1200 | |
| 1201 | FILE *fp = fopen(filename, "rb"); |
| 1202 | if (!fp) return NULL; |
| 1203 | |
| 1204 | fseek(fp, 0L, SEEK_END); |
| 1205 | size = ftell(fp); |
| 1206 | |
| 1207 | fseek(fp, 0L, SEEK_SET); |
| 1208 | |
| 1209 | shader_code = malloc(size); |
| 1210 | fread(shader_code, size, 1, fp); |
| 1211 | |
| 1212 | *psize = size; |
| 1213 | |
| 1214 | return shader_code; |
| 1215 | } |
| 1216 | |
Courtney Goeltzenleuchter | 78351a6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 1217 | static VkShader demo_prepare_vs(struct demo *demo) |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1218 | { |
Cody Northrop | 8b12a1f | 2015-03-17 15:55:58 -0600 | [diff] [blame] | 1219 | #ifdef EXTERNAL_SPV |
Courtney Goeltzenleuchter | 97db1c1 | 2014-10-30 15:14:16 -0600 | [diff] [blame] | 1220 | void *vertShaderCode; |
Mark Lobodzinski | c4fffc7 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 1221 | size_t size; |
Courtney Goeltzenleuchter | 97db1c1 | 2014-10-30 15:14:16 -0600 | [diff] [blame] | 1222 | |
Cody Northrop | 8b12a1f | 2015-03-17 15:55:58 -0600 | [diff] [blame] | 1223 | vertShaderCode = demo_read_spv("cube-vert.spv", &size); |
Courtney Goeltzenleuchter | 97db1c1 | 2014-10-30 15:14:16 -0600 | [diff] [blame] | 1224 | |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1225 | return demo_prepare_shader(demo, VK_SHADER_STAGE_VERTEX, |
Courtney Goeltzenleuchter | 97db1c1 | 2014-10-30 15:14:16 -0600 | [diff] [blame] | 1226 | vertShaderCode, size); |
| 1227 | #else |
Courtney Goeltzenleuchter | 8879d3a | 2014-10-29 08:29:35 -0600 | [diff] [blame] | 1228 | static const char *vertShaderText = |
Courtney Goeltzenleuchter | 4984d15 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 1229 | "#version 140\n" |
| 1230 | "#extension GL_ARB_separate_shader_objects : enable\n" |
| 1231 | "#extension GL_ARB_shading_language_420pack : enable\n" |
| 1232 | "\n" |
| 1233 | "layout(binding = 0) uniform buf {\n" |
| 1234 | " mat4 MVP;\n" |
| 1235 | " vec4 position[12*3];\n" |
| 1236 | " vec4 attr[12*3];\n" |
| 1237 | "} ubuf;\n" |
| 1238 | "\n" |
| 1239 | "layout (location = 0) out vec4 texcoord;\n" |
Courtney Goeltzenleuchter | 4984d15 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 1240 | "\n" |
| 1241 | "void main() \n" |
| 1242 | "{\n" |
| 1243 | " texcoord = ubuf.attr[gl_VertexID];\n" |
Courtney Goeltzenleuchter | 4984d15 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 1244 | " gl_Position = ubuf.MVP * ubuf.position[gl_VertexID];\n" |
| 1245 | "}\n"; |
Courtney Goeltzenleuchter | fc3b953 | 2014-10-28 10:32:57 -0600 | [diff] [blame] | 1246 | |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1247 | return demo_prepare_shader(demo, VK_SHADER_STAGE_VERTEX, |
Courtney Goeltzenleuchter | 8879d3a | 2014-10-29 08:29:35 -0600 | [diff] [blame] | 1248 | (const void *) vertShaderText, |
| 1249 | strlen(vertShaderText)); |
Courtney Goeltzenleuchter | 97db1c1 | 2014-10-30 15:14:16 -0600 | [diff] [blame] | 1250 | #endif |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1251 | } |
| 1252 | |
Courtney Goeltzenleuchter | 78351a6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 1253 | static VkShader demo_prepare_fs(struct demo *demo) |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1254 | { |
Cody Northrop | 8b12a1f | 2015-03-17 15:55:58 -0600 | [diff] [blame] | 1255 | #ifdef EXTERNAL_SPV |
Courtney Goeltzenleuchter | 97db1c1 | 2014-10-30 15:14:16 -0600 | [diff] [blame] | 1256 | void *fragShaderCode; |
Mark Lobodzinski | c4fffc7 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 1257 | size_t size; |
Courtney Goeltzenleuchter | 97db1c1 | 2014-10-30 15:14:16 -0600 | [diff] [blame] | 1258 | |
Cody Northrop | 8b12a1f | 2015-03-17 15:55:58 -0600 | [diff] [blame] | 1259 | fragShaderCode = demo_read_spv("cube-frag.spv", &size); |
Courtney Goeltzenleuchter | 97db1c1 | 2014-10-30 15:14:16 -0600 | [diff] [blame] | 1260 | |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1261 | return demo_prepare_shader(demo, VK_SHADER_STAGE_FRAGMENT, |
Courtney Goeltzenleuchter | 97db1c1 | 2014-10-30 15:14:16 -0600 | [diff] [blame] | 1262 | fragShaderCode, size); |
| 1263 | #else |
Courtney Goeltzenleuchter | 8879d3a | 2014-10-29 08:29:35 -0600 | [diff] [blame] | 1264 | static const char *fragShaderText = |
Courtney Goeltzenleuchter | 4984d15 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 1265 | "#version 140\n" |
| 1266 | "#extension GL_ARB_separate_shader_objects : enable\n" |
| 1267 | "#extension GL_ARB_shading_language_420pack : enable\n" |
Courtney Goeltzenleuchter | 4d1acf1 | 2015-02-25 15:13:35 -0700 | [diff] [blame] | 1268 | "layout (binding = 1) uniform sampler2D tex;\n" |
Courtney Goeltzenleuchter | 4984d15 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 1269 | "\n" |
| 1270 | "layout (location = 0) in vec4 texcoord;\n" |
| 1271 | "void main() {\n" |
| 1272 | " gl_FragColor = texture(tex, texcoord.xy);\n" |
| 1273 | "}\n"; |
Courtney Goeltzenleuchter | fc3b953 | 2014-10-28 10:32:57 -0600 | [diff] [blame] | 1274 | |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1275 | return demo_prepare_shader(demo, VK_SHADER_STAGE_FRAGMENT, |
Courtney Goeltzenleuchter | 8879d3a | 2014-10-29 08:29:35 -0600 | [diff] [blame] | 1276 | (const void *) fragShaderText, |
| 1277 | strlen(fragShaderText)); |
Courtney Goeltzenleuchter | 97db1c1 | 2014-10-30 15:14:16 -0600 | [diff] [blame] | 1278 | #endif |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1279 | } |
| 1280 | |
| 1281 | static void demo_prepare_pipeline(struct demo *demo) |
| 1282 | { |
Courtney Goeltzenleuchter | 78351a6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 1283 | VkGraphicsPipelineCreateInfo pipeline; |
| 1284 | VkPipelineIaStateCreateInfo ia; |
| 1285 | VkPipelineRsStateCreateInfo rs; |
| 1286 | VkPipelineCbStateCreateInfo cb; |
| 1287 | VkPipelineDsStateCreateInfo ds; |
| 1288 | VkPipelineShaderStageCreateInfo vs; |
| 1289 | VkPipelineShaderStageCreateInfo fs; |
| 1290 | VkPipelineVpStateCreateInfo vp; |
| 1291 | VkPipelineMsStateCreateInfo ms; |
| 1292 | VkResult err; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1293 | |
| 1294 | memset(&pipeline, 0, sizeof(pipeline)); |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1295 | pipeline.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO; |
Chia-I Wu | b58c24a | 2015-03-26 15:27:55 +0800 | [diff] [blame] | 1296 | pipeline.pSetLayoutChain = demo->desc_layout_chain; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1297 | |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1298 | memset(&ia, 0, sizeof(ia)); |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1299 | ia.sType = VK_STRUCTURE_TYPE_PIPELINE_IA_STATE_CREATE_INFO; |
Tony Barbour | 72304ef | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 1300 | ia.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1301 | |
| 1302 | memset(&rs, 0, sizeof(rs)); |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1303 | rs.sType = VK_STRUCTURE_TYPE_PIPELINE_RS_STATE_CREATE_INFO; |
Tony Barbour | 72304ef | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 1304 | rs.fillMode = VK_FILL_MODE_SOLID; |
| 1305 | rs.cullMode = VK_CULL_MODE_BACK; |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1306 | rs.frontFace = VK_FRONT_FACE_CCW; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1307 | |
| 1308 | memset(&cb, 0, sizeof(cb)); |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1309 | cb.sType = VK_STRUCTURE_TYPE_PIPELINE_CB_STATE_CREATE_INFO; |
Courtney Goeltzenleuchter | 78351a6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 1310 | VkPipelineCbAttachmentState att_state[1]; |
Tony Barbour | 29645d0 | 2015-01-16 14:27:35 -0700 | [diff] [blame] | 1311 | memset(att_state, 0, sizeof(att_state)); |
| 1312 | att_state[0].format = demo->format; |
| 1313 | att_state[0].channelWriteMask = 0xf; |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1314 | att_state[0].blendEnable = VK_FALSE; |
Tony Barbour | 29645d0 | 2015-01-16 14:27:35 -0700 | [diff] [blame] | 1315 | cb.attachmentCount = 1; |
| 1316 | cb.pAttachments = att_state; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1317 | |
Tony Barbour | 29645d0 | 2015-01-16 14:27:35 -0700 | [diff] [blame] | 1318 | memset(&vp, 0, sizeof(vp)); |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1319 | vp.sType = VK_STRUCTURE_TYPE_PIPELINE_VP_STATE_CREATE_INFO; |
Tony Barbour | 72304ef | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 1320 | vp.viewportCount = 1; |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1321 | vp.clipOrigin = VK_COORDINATE_ORIGIN_LOWER_LEFT; |
Tony Barbour | 29645d0 | 2015-01-16 14:27:35 -0700 | [diff] [blame] | 1322 | |
| 1323 | memset(&ds, 0, sizeof(ds)); |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1324 | ds.sType = VK_STRUCTURE_TYPE_PIPELINE_DS_STATE_CREATE_INFO; |
Tony Barbour | 29645d0 | 2015-01-16 14:27:35 -0700 | [diff] [blame] | 1325 | ds.format = demo->depth.format; |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1326 | ds.depthTestEnable = VK_TRUE; |
| 1327 | ds.depthWriteEnable = VK_TRUE; |
Tony Barbour | 72304ef | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 1328 | ds.depthCompareOp = VK_COMPARE_OP_LESS_EQUAL; |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1329 | ds.depthBoundsEnable = VK_FALSE; |
| 1330 | ds.back.stencilFailOp = VK_STENCIL_OP_KEEP; |
| 1331 | ds.back.stencilPassOp = VK_STENCIL_OP_KEEP; |
Tony Barbour | 72304ef | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 1332 | ds.back.stencilCompareOp = VK_COMPARE_OP_ALWAYS; |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1333 | ds.stencilTestEnable = VK_FALSE; |
Tony Barbour | 29645d0 | 2015-01-16 14:27:35 -0700 | [diff] [blame] | 1334 | ds.front = ds.back; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1335 | |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1336 | memset(&vs, 0, sizeof(vs)); |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1337 | vs.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO; |
| 1338 | vs.shader.stage = VK_SHADER_STAGE_VERTEX; |
Courtney Goeltzenleuchter | 4984d15 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 1339 | vs.shader.shader = demo_prepare_vs(demo); |
Courtney Goeltzenleuchter | fc3b953 | 2014-10-28 10:32:57 -0600 | [diff] [blame] | 1340 | assert(vs.shader.shader != NULL); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1341 | |
| 1342 | memset(&fs, 0, sizeof(fs)); |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1343 | fs.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO; |
| 1344 | fs.shader.stage = VK_SHADER_STAGE_FRAGMENT; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1345 | fs.shader.shader = demo_prepare_fs(demo); |
Courtney Goeltzenleuchter | fc3b953 | 2014-10-28 10:32:57 -0600 | [diff] [blame] | 1346 | assert(fs.shader.shader != NULL); |
Tony Barbour | 29645d0 | 2015-01-16 14:27:35 -0700 | [diff] [blame] | 1347 | |
| 1348 | memset(&ms, 0, sizeof(ms)); |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1349 | ms.sType = VK_STRUCTURE_TYPE_PIPELINE_MS_STATE_CREATE_INFO; |
Tony Barbour | 29645d0 | 2015-01-16 14:27:35 -0700 | [diff] [blame] | 1350 | ms.sampleMask = 1; |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1351 | ms.multisampleEnable = VK_FALSE; |
Tony Barbour | 29645d0 | 2015-01-16 14:27:35 -0700 | [diff] [blame] | 1352 | ms.samples = 1; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1353 | |
Mark Lobodzinski | c4fffc7 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 1354 | pipeline.pNext = (const void *) &ia; |
| 1355 | ia.pNext = (const void *) &rs; |
| 1356 | rs.pNext = (const void *) &cb; |
| 1357 | cb.pNext = (const void *) &ms; |
| 1358 | ms.pNext = (const void *) &vp; |
| 1359 | vp.pNext = (const void *) &ds; |
| 1360 | ds.pNext = (const void *) &vs; |
| 1361 | vs.pNext = (const void *) &fs; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1362 | |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1363 | err = vkCreateGraphicsPipeline(demo->device, &pipeline, &demo->pipeline); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1364 | assert(!err); |
| 1365 | |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1366 | vkDestroyObject(vs.shader.shader); |
| 1367 | vkDestroyObject(fs.shader.shader); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1368 | } |
| 1369 | |
| 1370 | static void demo_prepare_dynamic_states(struct demo *demo) |
| 1371 | { |
Courtney Goeltzenleuchter | 78351a6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 1372 | VkDynamicVpStateCreateInfo viewport_create; |
| 1373 | VkDynamicRsStateCreateInfo raster; |
| 1374 | VkDynamicCbStateCreateInfo color_blend; |
| 1375 | VkDynamicDsStateCreateInfo depth_stencil; |
| 1376 | VkResult err; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1377 | |
Tony Barbour | 29645d0 | 2015-01-16 14:27:35 -0700 | [diff] [blame] | 1378 | memset(&viewport_create, 0, sizeof(viewport_create)); |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1379 | viewport_create.sType = VK_STRUCTURE_TYPE_DYNAMIC_VP_STATE_CREATE_INFO; |
Courtney Goeltzenleuchter | 1710d8d | 2015-02-11 14:13:34 -0700 | [diff] [blame] | 1380 | viewport_create.viewportAndScissorCount = 1; |
Courtney Goeltzenleuchter | 78351a6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 1381 | VkViewport viewport; |
Piers Daniell | 735ee53 | 2015-02-23 16:23:13 -0700 | [diff] [blame] | 1382 | memset(&viewport, 0, sizeof(viewport)); |
Mark Lobodzinski | c4fffc7 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 1383 | viewport.height = (float) demo->height; |
| 1384 | viewport.width = (float) demo->width; |
| 1385 | viewport.minDepth = (float) 0.0f; |
| 1386 | viewport.maxDepth = (float) 1.0f; |
Piers Daniell | 735ee53 | 2015-02-23 16:23:13 -0700 | [diff] [blame] | 1387 | viewport_create.pViewports = &viewport; |
Courtney Goeltzenleuchter | 78351a6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 1388 | VkRect scissor; |
Piers Daniell | 735ee53 | 2015-02-23 16:23:13 -0700 | [diff] [blame] | 1389 | memset(&scissor, 0, sizeof(scissor)); |
Courtney Goeltzenleuchter | 1710d8d | 2015-02-11 14:13:34 -0700 | [diff] [blame] | 1390 | scissor.extent.width = demo->width; |
| 1391 | scissor.extent.height = demo->height; |
| 1392 | scissor.offset.x = 0; |
| 1393 | scissor.offset.y = 0; |
Courtney Goeltzenleuchter | 1710d8d | 2015-02-11 14:13:34 -0700 | [diff] [blame] | 1394 | viewport_create.pScissors = &scissor; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1395 | |
| 1396 | memset(&raster, 0, sizeof(raster)); |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1397 | raster.sType = VK_STRUCTURE_TYPE_DYNAMIC_RS_STATE_CREATE_INFO; |
Piers Daniell | 735ee53 | 2015-02-23 16:23:13 -0700 | [diff] [blame] | 1398 | raster.pointSize = 1.0; |
| 1399 | raster.lineWidth = 1.0; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1400 | |
| 1401 | memset(&color_blend, 0, sizeof(color_blend)); |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1402 | color_blend.sType = VK_STRUCTURE_TYPE_DYNAMIC_CB_STATE_CREATE_INFO; |
Piers Daniell | 735ee53 | 2015-02-23 16:23:13 -0700 | [diff] [blame] | 1403 | color_blend.blendConst[0] = 1.0f; |
| 1404 | color_blend.blendConst[1] = 1.0f; |
| 1405 | color_blend.blendConst[2] = 1.0f; |
| 1406 | color_blend.blendConst[3] = 1.0f; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1407 | |
| 1408 | memset(&depth_stencil, 0, sizeof(depth_stencil)); |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1409 | depth_stencil.sType = VK_STRUCTURE_TYPE_DYNAMIC_DS_STATE_CREATE_INFO; |
Piers Daniell | 735ee53 | 2015-02-23 16:23:13 -0700 | [diff] [blame] | 1410 | depth_stencil.minDepth = 0.0f; |
| 1411 | depth_stencil.maxDepth = 1.0f; |
Tony Barbour | 29645d0 | 2015-01-16 14:27:35 -0700 | [diff] [blame] | 1412 | depth_stencil.stencilBackRef = 0; |
| 1413 | depth_stencil.stencilFrontRef = 0; |
| 1414 | depth_stencil.stencilReadMask = 0xff; |
| 1415 | depth_stencil.stencilWriteMask = 0xff; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1416 | |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1417 | err = vkCreateDynamicViewportState(demo->device, &viewport_create, &demo->viewport); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1418 | assert(!err); |
| 1419 | |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1420 | err = vkCreateDynamicRasterState(demo->device, &raster, &demo->raster); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1421 | assert(!err); |
| 1422 | |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1423 | err = vkCreateDynamicColorBlendState(demo->device, |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1424 | &color_blend, &demo->color_blend); |
| 1425 | assert(!err); |
| 1426 | |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1427 | err = vkCreateDynamicDepthStencilState(demo->device, |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1428 | &depth_stencil, &demo->depth_stencil); |
| 1429 | assert(!err); |
| 1430 | } |
| 1431 | |
Chia-I Wu | 63ea926 | 2015-03-26 13:14:16 +0800 | [diff] [blame] | 1432 | static void demo_prepare_descriptor_pool(struct demo *demo) |
Chia-I Wu | 6a3c897 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 1433 | { |
Courtney Goeltzenleuchter | 78351a6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 1434 | const VkDescriptorTypeCount type_counts[2] = { |
Chia-I Wu | 6a3c897 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 1435 | [0] = { |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1436 | .type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, |
Chia-I Wu | 6a3c897 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 1437 | .count = 1, |
| 1438 | }, |
| 1439 | [1] = { |
Courtney Goeltzenleuchter | 42a078d | 2015-04-15 15:29:59 -0600 | [diff] [blame] | 1440 | .type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, |
Chia-I Wu | 6a3c897 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 1441 | .count = DEMO_TEXTURE_COUNT, |
| 1442 | }, |
| 1443 | }; |
Courtney Goeltzenleuchter | 78351a6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 1444 | const VkDescriptorPoolCreateInfo descriptor_pool = { |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1445 | .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO, |
Chia-I Wu | 6a3c897 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 1446 | .pNext = NULL, |
| 1447 | .count = 2, |
| 1448 | .pTypeCount = type_counts, |
| 1449 | }; |
Courtney Goeltzenleuchter | 78351a6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 1450 | VkResult err; |
Chia-I Wu | 6a3c897 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 1451 | |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1452 | err = vkCreateDescriptorPool(demo->device, |
| 1453 | VK_DESCRIPTOR_POOL_USAGE_ONE_SHOT, 1, |
Chia-I Wu | 63ea926 | 2015-03-26 13:14:16 +0800 | [diff] [blame] | 1454 | &descriptor_pool, &demo->desc_pool); |
Chia-I Wu | 6a3c897 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 1455 | assert(!err); |
| 1456 | } |
| 1457 | |
| 1458 | static void demo_prepare_descriptor_set(struct demo *demo) |
| 1459 | { |
Courtney Goeltzenleuchter | 78351a6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 1460 | VkImageViewAttachInfo view_info[DEMO_TEXTURE_COUNT]; |
| 1461 | VkSamplerImageViewInfo combined_info[DEMO_TEXTURE_COUNT]; |
| 1462 | VkUpdateSamplerTextures update_fs; |
| 1463 | VkUpdateBuffers update_vs; |
Chia-I Wu | b58c24a | 2015-03-26 15:27:55 +0800 | [diff] [blame] | 1464 | const void *update_array[2] = { &update_vs, &update_fs }; |
Courtney Goeltzenleuchter | 78351a6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 1465 | VkResult err; |
Chia-I Wu | 6a3c897 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 1466 | uint32_t count; |
Mark Lobodzinski | c4fffc7 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 1467 | uint32_t i; |
Chia-I Wu | 6a3c897 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 1468 | |
| 1469 | for (i = 0; i < DEMO_TEXTURE_COUNT; i++) { |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1470 | view_info[i].sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_ATTACH_INFO; |
Chia-I Wu | 6a3c897 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 1471 | view_info[i].pNext = NULL; |
| 1472 | view_info[i].view = demo->textures[i].view, |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1473 | view_info[i].layout = VK_IMAGE_LAYOUT_GENERAL; |
Chia-I Wu | 6a3c897 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 1474 | |
Courtney Goeltzenleuchter | eb4754f | 2015-04-09 11:43:10 -0600 | [diff] [blame] | 1475 | combined_info[i].sampler = demo->textures[i].sampler; |
Chia-I Wu | 6a3c897 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 1476 | combined_info[i].pImageView = &view_info[i]; |
| 1477 | } |
| 1478 | |
| 1479 | memset(&update_vs, 0, sizeof(update_vs)); |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1480 | update_vs.sType = VK_STRUCTURE_TYPE_UPDATE_BUFFERS; |
Chia-I Wu | 6a3c897 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 1481 | update_vs.pNext = &update_fs; |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1482 | update_vs.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER; |
Chia-I Wu | 6a3c897 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 1483 | update_vs.count = 1; |
Chia-I Wu | b58c24a | 2015-03-26 15:27:55 +0800 | [diff] [blame] | 1484 | update_vs.pBufferViews = &demo->uniform_data.attach; |
Chia-I Wu | 6a3c897 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 1485 | |
| 1486 | memset(&update_fs, 0, sizeof(update_fs)); |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1487 | update_fs.sType = VK_STRUCTURE_TYPE_UPDATE_SAMPLER_TEXTURES; |
Chia-I Wu | b58c24a | 2015-03-26 15:27:55 +0800 | [diff] [blame] | 1488 | update_fs.binding = 1; |
Chia-I Wu | 6a3c897 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 1489 | update_fs.count = DEMO_TEXTURE_COUNT; |
| 1490 | update_fs.pSamplerImageViews = combined_info; |
| 1491 | |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1492 | err = vkAllocDescriptorSets(demo->desc_pool, |
| 1493 | VK_DESCRIPTOR_SET_USAGE_STATIC, |
Chia-I Wu | 87544e7 | 2015-02-23 10:41:08 -0700 | [diff] [blame] | 1494 | 1, &demo->desc_layout, |
Chia-I Wu | 6a3c897 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 1495 | &demo->desc_set, &count); |
| 1496 | assert(!err && count == 1); |
| 1497 | |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1498 | vkBeginDescriptorPoolUpdate(demo->device, |
| 1499 | VK_DESCRIPTOR_UPDATE_MODE_FASTEST); |
Chia-I Wu | 6a3c897 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 1500 | |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1501 | vkClearDescriptorSets(demo->desc_pool, 1, &demo->desc_set); |
| 1502 | vkUpdateDescriptors(demo->desc_set, 2, update_array); |
Chia-I Wu | 6a3c897 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 1503 | |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1504 | vkEndDescriptorPoolUpdate(demo->device, demo->buffers[demo->current_buffer].cmd); |
Chia-I Wu | 6a3c897 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 1505 | } |
| 1506 | |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1507 | static void demo_prepare(struct demo *demo) |
| 1508 | { |
Courtney Goeltzenleuchter | 78351a6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 1509 | const VkCmdBufferCreateInfo cmd = { |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1510 | .sType = VK_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO, |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1511 | .pNext = NULL, |
Courtney Goeltzenleuchter | ed667a5 | 2015-03-05 18:09:39 -0700 | [diff] [blame] | 1512 | .queueNodeIndex = demo->graphics_queue_node_index, |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1513 | .flags = 0, |
| 1514 | }; |
Courtney Goeltzenleuchter | 78351a6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 1515 | VkResult err; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1516 | |
| 1517 | demo_prepare_buffers(demo); |
| 1518 | demo_prepare_depth(demo); |
| 1519 | demo_prepare_textures(demo); |
Courtney Goeltzenleuchter | 8879d3a | 2014-10-29 08:29:35 -0600 | [diff] [blame] | 1520 | demo_prepare_cube_data_buffer(demo); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1521 | |
Chia-I Wu | 6a3c897 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 1522 | demo_prepare_descriptor_layout(demo); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1523 | demo_prepare_pipeline(demo); |
| 1524 | demo_prepare_dynamic_states(demo); |
| 1525 | |
Courtney Goeltzenleuchter | efc58ae | 2015-02-17 09:48:44 -0700 | [diff] [blame] | 1526 | for (int i = 0; i < DEMO_BUFFER_COUNT; i++) { |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1527 | err = vkCreateCommandBuffer(demo->device, &cmd, &demo->buffers[i].cmd); |
Courtney Goeltzenleuchter | efc58ae | 2015-02-17 09:48:44 -0700 | [diff] [blame] | 1528 | assert(!err); |
| 1529 | } |
Chia-I Wu | 6a3c897 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 1530 | |
Chia-I Wu | 63ea926 | 2015-03-26 13:14:16 +0800 | [diff] [blame] | 1531 | demo_prepare_descriptor_pool(demo); |
Chia-I Wu | 6a3c897 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 1532 | demo_prepare_descriptor_set(demo); |
Courtney Goeltzenleuchter | efc58ae | 2015-02-17 09:48:44 -0700 | [diff] [blame] | 1533 | |
| 1534 | |
| 1535 | for (int i = 0; i < DEMO_BUFFER_COUNT; i++) { |
| 1536 | demo->current_buffer = i; |
| 1537 | demo_draw_build_cmd(demo, demo->buffers[i].cmd); |
| 1538 | } |
| 1539 | |
Courtney Goeltzenleuchter | 752eb8e | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 1540 | /* |
| 1541 | * Prepare functions above may generate pipeline commands |
| 1542 | * that need to be flushed before beginning the render loop. |
| 1543 | */ |
| 1544 | demo_flush_init_cmd(demo); |
| 1545 | |
Courtney Goeltzenleuchter | efc58ae | 2015-02-17 09:48:44 -0700 | [diff] [blame] | 1546 | demo->current_buffer = 0; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1547 | } |
| 1548 | |
| 1549 | static void demo_handle_event(struct demo *demo, |
| 1550 | const xcb_generic_event_t *event) |
| 1551 | { |
Piers Daniell | 735ee53 | 2015-02-23 16:23:13 -0700 | [diff] [blame] | 1552 | uint8_t event_code = event->response_type & 0x7f; |
Courtney Goeltzenleuchter | 98cb2cb | 2014-11-06 14:27:52 -0700 | [diff] [blame] | 1553 | switch (event_code) { |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1554 | case XCB_EXPOSE: |
Courtney Goeltzenleuchter | c465d6c | 2014-11-18 11:28:09 -0700 | [diff] [blame] | 1555 | // TODO: Resize window |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1556 | break; |
Courtney Goeltzenleuchter | 98cb2cb | 2014-11-06 14:27:52 -0700 | [diff] [blame] | 1557 | case XCB_CLIENT_MESSAGE: |
| 1558 | if((*(xcb_client_message_event_t*)event).data.data32[0] == |
| 1559 | (*demo->atom_wm_delete_window).atom) { |
| 1560 | demo->quit = true; |
| 1561 | } |
| 1562 | break; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1563 | case XCB_KEY_RELEASE: |
| 1564 | { |
| 1565 | const xcb_key_release_event_t *key = |
| 1566 | (const xcb_key_release_event_t *) event; |
| 1567 | |
Courtney Goeltzenleuchter | fc3b953 | 2014-10-28 10:32:57 -0600 | [diff] [blame] | 1568 | switch (key->detail) { |
| 1569 | case 0x9: // Escape |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1570 | demo->quit = true; |
Courtney Goeltzenleuchter | fc3b953 | 2014-10-28 10:32:57 -0600 | [diff] [blame] | 1571 | break; |
Courtney Goeltzenleuchter | 4984d15 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 1572 | case 0x71: // left arrow key |
Courtney Goeltzenleuchter | 8879d3a | 2014-10-29 08:29:35 -0600 | [diff] [blame] | 1573 | demo->spin_angle += demo->spin_increment; |
Courtney Goeltzenleuchter | 4984d15 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 1574 | break; |
Courtney Goeltzenleuchter | fc3b953 | 2014-10-28 10:32:57 -0600 | [diff] [blame] | 1575 | case 0x72: // right arrow key |
Courtney Goeltzenleuchter | 8879d3a | 2014-10-29 08:29:35 -0600 | [diff] [blame] | 1576 | demo->spin_angle -= demo->spin_increment; |
Courtney Goeltzenleuchter | fc3b953 | 2014-10-28 10:32:57 -0600 | [diff] [blame] | 1577 | break; |
Courtney Goeltzenleuchter | 8879d3a | 2014-10-29 08:29:35 -0600 | [diff] [blame] | 1578 | case 0x41: |
| 1579 | demo->pause = !demo->pause; |
Piers Daniell | 735ee53 | 2015-02-23 16:23:13 -0700 | [diff] [blame] | 1580 | break; |
Courtney Goeltzenleuchter | fc3b953 | 2014-10-28 10:32:57 -0600 | [diff] [blame] | 1581 | } |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1582 | } |
| 1583 | break; |
| 1584 | default: |
| 1585 | break; |
| 1586 | } |
| 1587 | } |
| 1588 | |
| 1589 | static void demo_run(struct demo *demo) |
| 1590 | { |
| 1591 | xcb_flush(demo->connection); |
| 1592 | |
| 1593 | while (!demo->quit) { |
| 1594 | xcb_generic_event_t *event; |
| 1595 | |
Courtney Goeltzenleuchter | c465d6c | 2014-11-18 11:28:09 -0700 | [diff] [blame] | 1596 | if (demo->pause) { |
| 1597 | event = xcb_wait_for_event(demo->connection); |
| 1598 | } else { |
| 1599 | event = xcb_poll_for_event(demo->connection); |
| 1600 | } |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1601 | if (event) { |
| 1602 | demo_handle_event(demo, event); |
| 1603 | free(event); |
Courtney Goeltzenleuchter | 9808555 | 2014-11-10 11:13:13 -0700 | [diff] [blame] | 1604 | } |
Courtney Goeltzenleuchter | c465d6c | 2014-11-18 11:28:09 -0700 | [diff] [blame] | 1605 | |
| 1606 | // Wait for work to finish before updating MVP. |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1607 | vkDeviceWaitIdle(demo->device); |
Courtney Goeltzenleuchter | c465d6c | 2014-11-18 11:28:09 -0700 | [diff] [blame] | 1608 | demo_update_data_buffer(demo); |
| 1609 | |
Courtney Goeltzenleuchter | 9808555 | 2014-11-10 11:13:13 -0700 | [diff] [blame] | 1610 | demo_draw(demo); |
Courtney Goeltzenleuchter | 1454f3c | 2014-11-18 11:28:09 -0700 | [diff] [blame] | 1611 | |
Courtney Goeltzenleuchter | 9808555 | 2014-11-10 11:13:13 -0700 | [diff] [blame] | 1612 | // Wait for work to finish before updating MVP. |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1613 | vkDeviceWaitIdle(demo->device); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1614 | } |
| 1615 | } |
| 1616 | |
| 1617 | static void demo_create_window(struct demo *demo) |
| 1618 | { |
| 1619 | uint32_t value_mask, value_list[32]; |
| 1620 | |
| 1621 | demo->window = xcb_generate_id(demo->connection); |
| 1622 | |
| 1623 | value_mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK; |
| 1624 | value_list[0] = demo->screen->black_pixel; |
| 1625 | value_list[1] = XCB_EVENT_MASK_KEY_RELEASE | |
| 1626 | XCB_EVENT_MASK_EXPOSURE; |
| 1627 | |
| 1628 | xcb_create_window(demo->connection, |
| 1629 | XCB_COPY_FROM_PARENT, |
| 1630 | demo->window, demo->screen->root, |
| 1631 | 0, 0, demo->width, demo->height, 0, |
| 1632 | XCB_WINDOW_CLASS_INPUT_OUTPUT, |
| 1633 | demo->screen->root_visual, |
| 1634 | value_mask, value_list); |
| 1635 | |
Courtney Goeltzenleuchter | 98cb2cb | 2014-11-06 14:27:52 -0700 | [diff] [blame] | 1636 | /* Magic code that will send notification when window is destroyed */ |
| 1637 | xcb_intern_atom_cookie_t cookie = xcb_intern_atom(demo->connection, 1, 12, |
| 1638 | "WM_PROTOCOLS"); |
| 1639 | xcb_intern_atom_reply_t* reply = xcb_intern_atom_reply(demo->connection, cookie, 0); |
| 1640 | |
| 1641 | xcb_intern_atom_cookie_t cookie2 = xcb_intern_atom(demo->connection, 0, 16, "WM_DELETE_WINDOW"); |
| 1642 | demo->atom_wm_delete_window = xcb_intern_atom_reply(demo->connection, cookie2, 0); |
| 1643 | |
| 1644 | xcb_change_property(demo->connection, XCB_PROP_MODE_REPLACE, |
| 1645 | demo->window, (*reply).atom, 4, 32, 1, |
| 1646 | &(*demo->atom_wm_delete_window).atom); |
| 1647 | free(reply); |
| 1648 | |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1649 | xcb_map_window(demo->connection, demo->window); |
| 1650 | } |
| 1651 | |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1652 | static void demo_init_vk(struct demo *demo) |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1653 | { |
Tobin Ehlis | 7537db9 | 2015-04-16 10:04:35 -0600 | [diff] [blame] | 1654 | // TODO : Should query count w/ GetGlobalExtensionInfo, then enable via CreateInstance |
Courtney Goeltzenleuchter | 78351a6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 1655 | const VkApplicationInfo app = { |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1656 | .sType = VK_STRUCTURE_TYPE_APPLICATION_INFO, |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1657 | .pNext = NULL, |
Chia-I Wu | b2c8193 | 2014-12-27 15:16:07 +0800 | [diff] [blame] | 1658 | .pAppName = "cube", |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1659 | .appVersion = 0, |
Chia-I Wu | b2c8193 | 2014-12-27 15:16:07 +0800 | [diff] [blame] | 1660 | .pEngineName = "cube", |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1661 | .engineVersion = 0, |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1662 | .apiVersion = VK_API_VERSION, |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1663 | }; |
Courtney Goeltzenleuchter | 28c4d5f | 2015-04-14 18:48:46 -0600 | [diff] [blame] | 1664 | const VkInstanceCreateInfo inst_info = { |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1665 | .sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO, |
Jon Ashburn | ab46b36 | 2015-04-04 14:52:07 -0600 | [diff] [blame] | 1666 | .pNext = NULL, |
| 1667 | .pAppInfo = &app, |
| 1668 | .pAllocCb = NULL, |
| 1669 | .extensionCount = 0, |
| 1670 | .ppEnabledExtensionNames = NULL, |
| 1671 | }; |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1672 | const VK_WSI_X11_CONNECTION_INFO connection = { |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1673 | .pConnection = demo->connection, |
| 1674 | .root = demo->screen->root, |
| 1675 | .provider = 0, |
| 1676 | }; |
Courtney Goeltzenleuchter | 28c4d5f | 2015-04-14 18:48:46 -0600 | [diff] [blame] | 1677 | const VkDeviceQueueCreateInfo queue = { |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1678 | .queueNodeIndex = 0, |
| 1679 | .queueCount = 1, |
| 1680 | }; |
Tobin Ehlis | 7537db9 | 2015-04-16 10:04:35 -0600 | [diff] [blame] | 1681 | |
Mark Lobodzinski | c4fffc7 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 1682 | const char *ext_names[] = { |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1683 | "VK_WSI_X11", |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1684 | }; |
Tobin Ehlis | 7537db9 | 2015-04-16 10:04:35 -0600 | [diff] [blame] | 1685 | |
Courtney Goeltzenleuchter | 28c4d5f | 2015-04-14 18:48:46 -0600 | [diff] [blame] | 1686 | const VkDeviceCreateInfo device = { |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1687 | .sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO, |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1688 | .pNext = NULL, |
| 1689 | .queueRecordCount = 1, |
| 1690 | .pRequestedQueues = &queue, |
Tobin Ehlis | 7537db9 | 2015-04-16 10:04:35 -0600 | [diff] [blame] | 1691 | .extensionCount = 1, // TODO : Should query count w/ GetGlobalExtensionInfo |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1692 | .ppEnabledExtensionNames = ext_names, |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1693 | .flags = VK_DEVICE_CREATE_VALIDATION_BIT, |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1694 | }; |
Courtney Goeltzenleuchter | 78351a6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 1695 | VkResult err; |
Mark Lobodzinski | c4fffc7 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 1696 | uint32_t gpu_count; |
| 1697 | uint32_t i; |
Courtney Goeltzenleuchter | ed667a5 | 2015-03-05 18:09:39 -0700 | [diff] [blame] | 1698 | size_t data_size; |
| 1699 | uint32_t queue_count; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1700 | |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1701 | err = vkCreateInstance(&inst_info, &demo->inst); |
| 1702 | if (err == VK_ERROR_INCOMPATIBLE_DRIVER) { |
Ian Elliott | 3979e28 | 2015-04-03 15:24:55 -0600 | [diff] [blame] | 1703 | printf("Cannot find a compatible Vulkan installable client driver " |
| 1704 | "(ICD).\nExiting ...\n"); |
| 1705 | fflush(stdout); |
| 1706 | exit(1); |
| 1707 | } else { |
| 1708 | assert(!err); |
| 1709 | } |
Jon Ashburn | ab46b36 | 2015-04-04 14:52:07 -0600 | [diff] [blame] | 1710 | |
Jon Ashburn | 07c0c0c | 2015-04-15 11:31:12 -0600 | [diff] [blame] | 1711 | gpu_count = 1; |
| 1712 | err = vkEnumeratePhysicalDevices(demo->inst, &gpu_count, &demo->gpu); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1713 | assert(!err && gpu_count == 1); |
| 1714 | |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1715 | err = vkWsiX11AssociateConnection(demo->gpu, &connection); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1716 | assert(!err); |
| 1717 | |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1718 | err = vkCreateDevice(demo->gpu, &device, &demo->device); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1719 | assert(!err); |
| 1720 | |
Tony Barbour | 72304ef | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 1721 | err = vkGetPhysicalDeviceInfo(demo->gpu, VK_PHYSICAL_DEVICE_INFO_TYPE_PROPERTIES, |
Courtney Goeltzenleuchter | 752eb8e | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 1722 | &data_size, NULL); |
| 1723 | assert(!err); |
| 1724 | |
Tony Barbour | 72304ef | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 1725 | demo->gpu_props = (VkPhysicalDeviceProperties *) malloc(data_size); |
| 1726 | err = vkGetPhysicalDeviceInfo(demo->gpu, VK_PHYSICAL_DEVICE_INFO_TYPE_PROPERTIES, |
Courtney Goeltzenleuchter | 752eb8e | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 1727 | &data_size, demo->gpu_props); |
| 1728 | assert(!err); |
| 1729 | |
Tony Barbour | 72304ef | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 1730 | err = vkGetPhysicalDeviceInfo(demo->gpu, VK_PHYSICAL_DEVICE_INFO_TYPE_QUEUE_PROPERTIES, |
Courtney Goeltzenleuchter | ed667a5 | 2015-03-05 18:09:39 -0700 | [diff] [blame] | 1731 | &data_size, NULL); |
| 1732 | assert(!err); |
| 1733 | |
Tony Barbour | 72304ef | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 1734 | demo->queue_props = (VkPhysicalDeviceQueueProperties *) malloc(data_size); |
| 1735 | err = vkGetPhysicalDeviceInfo(demo->gpu, VK_PHYSICAL_DEVICE_INFO_TYPE_QUEUE_PROPERTIES, |
Courtney Goeltzenleuchter | ed667a5 | 2015-03-05 18:09:39 -0700 | [diff] [blame] | 1736 | &data_size, demo->queue_props); |
| 1737 | assert(!err); |
Tony Barbour | 72304ef | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 1738 | queue_count = (uint32_t)(data_size / sizeof(VkPhysicalDeviceQueueProperties)); |
Courtney Goeltzenleuchter | ed667a5 | 2015-03-05 18:09:39 -0700 | [diff] [blame] | 1739 | assert(queue_count >= 1); |
| 1740 | |
Mark Lobodzinski | da9b109 | 2015-04-16 11:44:05 -0500 | [diff] [blame] | 1741 | // Graphics queue and MemMgr queue can be separate. |
| 1742 | // TODO: Add support for separate queues, including synchronization, |
| 1743 | // and appropriate tracking for QueueSubmit and QueueBindObjectMemory |
Courtney Goeltzenleuchter | ed667a5 | 2015-03-05 18:09:39 -0700 | [diff] [blame] | 1744 | for (i = 0; i < queue_count; i++) { |
Mark Lobodzinski | da9b109 | 2015-04-16 11:44:05 -0500 | [diff] [blame] | 1745 | if ((demo->queue_props[i].queueFlags & VK_QUEUE_GRAPHICS_BIT) && |
| 1746 | (demo->queue_props[i].queueFlags & VK_QUEUE_MEMMGR_BIT) ) |
Courtney Goeltzenleuchter | ed667a5 | 2015-03-05 18:09:39 -0700 | [diff] [blame] | 1747 | break; |
| 1748 | } |
| 1749 | assert(i < queue_count); |
| 1750 | demo->graphics_queue_node_index = i; |
| 1751 | |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1752 | err = vkGetDeviceQueue(demo->device, demo->graphics_queue_node_index, |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1753 | 0, &demo->queue); |
| 1754 | assert(!err); |
| 1755 | } |
| 1756 | |
| 1757 | static void demo_init_connection(struct demo *demo) |
| 1758 | { |
| 1759 | const xcb_setup_t *setup; |
| 1760 | xcb_screen_iterator_t iter; |
| 1761 | int scr; |
| 1762 | |
| 1763 | demo->connection = xcb_connect(NULL, &scr); |
Ian Elliott | 3979e28 | 2015-04-03 15:24:55 -0600 | [diff] [blame] | 1764 | if (demo->connection == NULL) { |
| 1765 | printf("Cannot find a compatible Vulkan installable client driver " |
| 1766 | "(ICD).\nExiting ...\n"); |
| 1767 | fflush(stdout); |
| 1768 | exit(1); |
| 1769 | } |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1770 | |
| 1771 | setup = xcb_get_setup(demo->connection); |
| 1772 | iter = xcb_setup_roots_iterator(setup); |
| 1773 | while (scr-- > 0) |
| 1774 | xcb_screen_next(&iter); |
| 1775 | |
| 1776 | demo->screen = iter.data; |
| 1777 | } |
| 1778 | |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 1779 | static void demo_init(struct demo *demo, int argc, char **argv) |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1780 | { |
Courtney Goeltzenleuchter | 8879d3a | 2014-10-29 08:29:35 -0600 | [diff] [blame] | 1781 | vec3 eye = {0.0f, 3.0f, 5.0f}; |
| 1782 | vec3 origin = {0, 0, 0}; |
| 1783 | vec3 up = {0.0f, -1.0f, 0.0}; |
| 1784 | |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1785 | memset(demo, 0, sizeof(*demo)); |
| 1786 | |
Piers Daniell | 735ee53 | 2015-02-23 16:23:13 -0700 | [diff] [blame] | 1787 | for (int i = 1; i < argc; i++) { |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 1788 | if (strncmp(argv[i], "--use_staging", strlen("--use_staging")) == 0) |
| 1789 | demo->use_staging_buffer = true; |
| 1790 | } |
| 1791 | |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1792 | demo_init_connection(demo); |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1793 | demo_init_vk(demo); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1794 | |
Courtney Goeltzenleuchter | 8879d3a | 2014-10-29 08:29:35 -0600 | [diff] [blame] | 1795 | demo->width = 500; |
| 1796 | demo->height = 500; |
Tony Barbour | 72304ef | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 1797 | demo->format = VK_FORMAT_B8G8R8A8_UNORM; |
Courtney Goeltzenleuchter | 8879d3a | 2014-10-29 08:29:35 -0600 | [diff] [blame] | 1798 | |
| 1799 | demo->spin_angle = 0.01f; |
| 1800 | demo->spin_increment = 0.01f; |
| 1801 | demo->pause = false; |
| 1802 | |
Piers Daniell | 735ee53 | 2015-02-23 16:23:13 -0700 | [diff] [blame] | 1803 | mat4x4_perspective(demo->projection_matrix, (float)degreesToRadians(45.0f), 1.0f, 0.1f, 100.0f); |
Courtney Goeltzenleuchter | 8879d3a | 2014-10-29 08:29:35 -0600 | [diff] [blame] | 1804 | mat4x4_look_at(demo->view_matrix, eye, origin, up); |
| 1805 | mat4x4_identity(demo->model_matrix); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1806 | } |
| 1807 | |
| 1808 | static void demo_cleanup(struct demo *demo) |
| 1809 | { |
Mark Lobodzinski | c4fffc7 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 1810 | uint32_t i, j; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1811 | |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1812 | vkDestroyObject(demo->desc_set); |
| 1813 | vkDestroyObject(demo->desc_pool); |
Chia-I Wu | 6a3c897 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 1814 | |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1815 | vkDestroyObject(demo->viewport); |
| 1816 | vkDestroyObject(demo->raster); |
| 1817 | vkDestroyObject(demo->color_blend); |
| 1818 | vkDestroyObject(demo->depth_stencil); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1819 | |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1820 | vkDestroyObject(demo->pipeline); |
| 1821 | vkDestroyObject(demo->desc_layout_chain); |
| 1822 | vkDestroyObject(demo->desc_layout); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1823 | |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1824 | for (i = 0; i < DEMO_TEXTURE_COUNT; i++) { |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1825 | vkDestroyObject(demo->textures[i].view); |
Mark Lobodzinski | da9b109 | 2015-04-16 11:44:05 -0500 | [diff] [blame] | 1826 | vkQueueBindObjectMemory(demo->queue, demo->textures[i].image, 0, VK_NULL_HANDLE, 0); |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1827 | vkDestroyObject(demo->textures[i].image); |
Courtney Goeltzenleuchter | 1359b7a | 2015-04-02 16:25:42 -0600 | [diff] [blame] | 1828 | demo_remove_mem_refs(demo, demo->textures[i].num_mem, demo->textures[i].mem); |
Jon Ashburn | b2a6665 | 2015-01-16 09:37:43 -0700 | [diff] [blame] | 1829 | for (j = 0; j < demo->textures[i].num_mem; j++) |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1830 | vkFreeMemory(demo->textures[i].mem[j]); |
| 1831 | vkDestroyObject(demo->textures[i].sampler); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1832 | } |
| 1833 | |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1834 | vkDestroyObject(demo->depth.view); |
Mark Lobodzinski | da9b109 | 2015-04-16 11:44:05 -0500 | [diff] [blame] | 1835 | vkQueueBindObjectMemory(demo->queue, demo->depth.image, 0, VK_NULL_HANDLE, 0); |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1836 | vkDestroyObject(demo->depth.image); |
Courtney Goeltzenleuchter | 1359b7a | 2015-04-02 16:25:42 -0600 | [diff] [blame] | 1837 | demo_remove_mem_refs(demo, demo->depth.num_mem, demo->depth.mem); |
| 1838 | for (j = 0; j < demo->depth.num_mem; j++) { |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1839 | vkFreeMemory(demo->depth.mem[j]); |
Courtney Goeltzenleuchter | 1359b7a | 2015-04-02 16:25:42 -0600 | [diff] [blame] | 1840 | } |
Mark Lobodzinski | 7b8fee6 | 2015-02-18 16:38:17 -0600 | [diff] [blame] | 1841 | |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1842 | vkDestroyObject(demo->uniform_data.view); |
Mark Lobodzinski | da9b109 | 2015-04-16 11:44:05 -0500 | [diff] [blame] | 1843 | vkQueueBindObjectMemory(demo->queue, demo->uniform_data.buf, 0, VK_NULL_HANDLE, 0); |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1844 | vkDestroyObject(demo->uniform_data.buf); |
Courtney Goeltzenleuchter | 1359b7a | 2015-04-02 16:25:42 -0600 | [diff] [blame] | 1845 | demo_remove_mem_refs(demo, demo->uniform_data.num_mem, demo->uniform_data.mem); |
Jon Ashburn | ae7c21c | 2015-01-19 15:00:26 -0700 | [diff] [blame] | 1846 | for (j = 0; j < demo->uniform_data.num_mem; j++) |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1847 | vkFreeMemory(demo->uniform_data.mem[j]); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1848 | |
| 1849 | for (i = 0; i < DEMO_BUFFER_COUNT; i++) { |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1850 | vkDestroyObject(demo->buffers[i].fence); |
| 1851 | vkDestroyObject(demo->buffers[i].view); |
| 1852 | vkDestroyObject(demo->buffers[i].image); |
| 1853 | vkDestroyObject(demo->buffers[i].cmd); |
Courtney Goeltzenleuchter | 1359b7a | 2015-04-02 16:25:42 -0600 | [diff] [blame] | 1854 | demo_remove_mem_refs(demo, 1, &demo->buffers[i].mem); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1855 | } |
| 1856 | |
Courtney Goeltzenleuchter | a4131c4 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1857 | vkDestroyDevice(demo->device); |
| 1858 | vkDestroyInstance(demo->inst); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1859 | |
| 1860 | xcb_destroy_window(demo->connection, demo->window); |
| 1861 | xcb_disconnect(demo->connection); |
| 1862 | } |
| 1863 | |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 1864 | int main(int argc, char **argv) |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1865 | { |
| 1866 | struct demo demo; |
| 1867 | |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 1868 | demo_init(&demo, argc, argv); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1869 | |
| 1870 | demo_prepare(&demo); |
| 1871 | demo_create_window(&demo); |
| 1872 | demo_run(&demo); |
| 1873 | |
| 1874 | demo_cleanup(&demo); |
| 1875 | |
| 1876 | return 0; |
| 1877 | } |