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> |
| 9 | #include <xgl.h> |
| 10 | #include <xglDbg.h> |
| 11 | #include <xglWsiX11Ext.h> |
| 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 | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 25 | XGL_SAMPLER sampler; |
| 26 | |
| 27 | XGL_IMAGE image; |
Courtney Goeltzenleuchter | 752eb8e | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 28 | XGL_IMAGE_LAYOUT imageLayout; |
| 29 | |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 30 | uint32_t num_mem; |
| 31 | XGL_GPU_MEMORY *mem; |
| 32 | XGL_IMAGE_VIEW view; |
| 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 | 4984d15 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 40 | struct xglcube_vs_uniform { |
| 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 | |
| 47 | struct xgltexcube_vs_uniform { |
| 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 | |
Jon Ashburn | 93cfc43 | 2015-01-29 15:47:01 -0700 | [diff] [blame] | 205 | XGL_INSTANCE inst; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 206 | XGL_PHYSICAL_GPU gpu; |
| 207 | XGL_DEVICE device; |
| 208 | XGL_QUEUE queue; |
Courtney Goeltzenleuchter | ed667a5 | 2015-03-05 18:09:39 -0700 | [diff] [blame] | 209 | uint32_t graphics_queue_node_index; |
Courtney Goeltzenleuchter | 752eb8e | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 210 | XGL_PHYSICAL_GPU_PROPERTIES *gpu_props; |
Courtney Goeltzenleuchter | ed667a5 | 2015-03-05 18:09:39 -0700 | [diff] [blame] | 211 | XGL_PHYSICAL_GPU_QUEUE_PROPERTIES *queue_props; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 212 | |
Courtney Goeltzenleuchter | 53968d8 | 2015-04-03 15:25:24 -0600 | [diff] [blame] | 213 | XGL_FRAMEBUFFER framebuffer; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 214 | int width, height; |
| 215 | XGL_FORMAT format; |
| 216 | |
| 217 | struct { |
| 218 | XGL_IMAGE image; |
| 219 | XGL_GPU_MEMORY mem; |
Courtney Goeltzenleuchter | efc58ae | 2015-02-17 09:48:44 -0700 | [diff] [blame] | 220 | XGL_CMD_BUFFER cmd; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 221 | |
| 222 | XGL_COLOR_ATTACHMENT_VIEW view; |
Chia-I Wu | bb57f64 | 2014-11-07 14:30:34 +0800 | [diff] [blame] | 223 | XGL_FENCE fence; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 224 | } buffers[DEMO_BUFFER_COUNT]; |
| 225 | |
| 226 | struct { |
| 227 | XGL_FORMAT format; |
| 228 | |
| 229 | XGL_IMAGE image; |
Mark Lobodzinski | c4fffc7 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 230 | uint32_t num_mem; |
Jon Ashburn | b2a6665 | 2015-01-16 09:37:43 -0700 | [diff] [blame] | 231 | XGL_GPU_MEMORY *mem; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 232 | XGL_DEPTH_STENCIL_VIEW view; |
| 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 { |
Chia-I Wu | 8ea21c7 | 2015-01-01 07:55:04 +0800 | [diff] [blame] | 238 | XGL_BUFFER buf; |
Mark Lobodzinski | c4fffc7 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 239 | uint32_t num_mem; |
Jon Ashburn | ae7c21c | 2015-01-19 15:00:26 -0700 | [diff] [blame] | 240 | XGL_GPU_MEMORY *mem; |
Chia-I Wu | 8ea21c7 | 2015-01-01 07:55:04 +0800 | [diff] [blame] | 241 | XGL_BUFFER_VIEW view; |
| 242 | XGL_BUFFER_VIEW_ATTACH_INFO attach; |
Courtney Goeltzenleuchter | fc3b953 | 2014-10-28 10:32:57 -0600 | [diff] [blame] | 243 | } uniform_data; |
| 244 | |
Courtney Goeltzenleuchter | 752eb8e | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 245 | XGL_CMD_BUFFER cmd; // Buffer for initialization commands |
Chia-I Wu | b58c24a | 2015-03-26 15:27:55 +0800 | [diff] [blame] | 246 | XGL_DESCRIPTOR_SET_LAYOUT_CHAIN desc_layout_chain; |
Chia-I Wu | 87544e7 | 2015-02-23 10:41:08 -0700 | [diff] [blame] | 247 | XGL_DESCRIPTOR_SET_LAYOUT desc_layout; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 248 | XGL_PIPELINE pipeline; |
| 249 | |
Tony Barbour | 29645d0 | 2015-01-16 14:27:35 -0700 | [diff] [blame] | 250 | XGL_DYNAMIC_VP_STATE_OBJECT viewport; |
| 251 | XGL_DYNAMIC_RS_STATE_OBJECT raster; |
| 252 | XGL_DYNAMIC_CB_STATE_OBJECT color_blend; |
| 253 | XGL_DYNAMIC_DS_STATE_OBJECT 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 | |
Chia-I Wu | 63ea926 | 2015-03-26 13:14:16 +0800 | [diff] [blame] | 263 | XGL_DESCRIPTOR_POOL desc_pool; |
Chia-I Wu | 6a3c897 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 264 | XGL_DESCRIPTOR_SET desc_set; |
| 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 | { |
| 275 | XGL_RESULT err; |
| 276 | |
| 277 | if (demo->cmd == XGL_NULL_HANDLE) |
| 278 | return; |
| 279 | |
| 280 | err = xglEndCommandBuffer(demo->cmd); |
| 281 | assert(!err); |
| 282 | |
| 283 | const XGL_CMD_BUFFER cmd_bufs[] = { demo->cmd }; |
| 284 | |
Courtney Goeltzenleuchter | 2c2fbbf | 2015-04-07 17:13:38 -0600 | [diff] [blame] | 285 | err = xglQueueSubmit(demo->queue, 1, cmd_bufs, XGL_NULL_HANDLE); |
Courtney Goeltzenleuchter | 752eb8e | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 286 | assert(!err); |
| 287 | |
| 288 | err = xglQueueWaitIdle(demo->queue); |
| 289 | assert(!err); |
| 290 | |
| 291 | xglDestroyObject(demo->cmd); |
| 292 | demo->cmd = XGL_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, |
Courtney Goeltzenleuchter | 752eb8e | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 297 | int num_refs, XGL_GPU_MEMORY *mem) |
| 298 | { |
| 299 | for (int i = 0; i < num_refs; i++) { |
Courtney Goeltzenleuchter | 1359b7a | 2015-04-02 16:25:42 -0600 | [diff] [blame] | 300 | xglQueueAddMemReference(demo->queue, mem[i]); |
| 301 | } |
| 302 | } |
| 303 | |
| 304 | static void demo_remove_mem_refs( |
| 305 | struct demo *demo, |
| 306 | int num_refs, XGL_GPU_MEMORY *mem) |
| 307 | { |
| 308 | for (int i = 0; i < num_refs; i++) { |
| 309 | xglQueueRemoveMemReference(demo->queue, mem[i]); |
Courtney Goeltzenleuchter | 752eb8e | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 310 | } |
| 311 | } |
| 312 | |
| 313 | static void demo_set_image_layout( |
| 314 | struct demo *demo, |
| 315 | XGL_IMAGE image, |
| 316 | XGL_IMAGE_LAYOUT old_image_layout, |
| 317 | XGL_IMAGE_LAYOUT new_image_layout) |
| 318 | { |
| 319 | XGL_RESULT err; |
| 320 | |
| 321 | if (demo->cmd == XGL_NULL_HANDLE) { |
| 322 | const XGL_CMD_BUFFER_CREATE_INFO cmd = { |
| 323 | .sType = XGL_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO, |
| 324 | .pNext = NULL, |
| 325 | .queueNodeIndex = demo->graphics_queue_node_index, |
| 326 | .flags = 0, |
| 327 | }; |
| 328 | |
| 329 | err = xglCreateCommandBuffer(demo->device, &cmd, &demo->cmd); |
| 330 | assert(!err); |
| 331 | |
| 332 | XGL_CMD_BUFFER_BEGIN_INFO cmd_buf_info = { |
| 333 | .sType = XGL_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO, |
| 334 | .pNext = NULL, |
| 335 | .flags = XGL_CMD_BUFFER_OPTIMIZE_GPU_SMALL_BATCH_BIT | |
| 336 | XGL_CMD_BUFFER_OPTIMIZE_ONE_TIME_SUBMIT_BIT, |
| 337 | }; |
| 338 | err = xglBeginCommandBuffer(demo->cmd, &cmd_buf_info); |
| 339 | } |
| 340 | |
| 341 | XGL_IMAGE_MEMORY_BARRIER image_memory_barrier = { |
| 342 | .sType = XGL_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER, |
| 343 | .pNext = NULL, |
| 344 | .outputMask = 0, |
| 345 | .inputMask = 0, |
| 346 | .oldLayout = old_image_layout, |
| 347 | .newLayout = new_image_layout, |
| 348 | .image = image, |
| 349 | .subresourceRange = { XGL_IMAGE_ASPECT_COLOR, 0, 1, 0, 0 } |
| 350 | }; |
| 351 | |
| 352 | if (new_image_layout == XGL_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL) { |
| 353 | /* Make sure anything that was copying from this image has completed */ |
| 354 | image_memory_barrier.inputMask = XGL_MEMORY_INPUT_COPY_BIT; |
| 355 | } |
| 356 | |
| 357 | if (new_image_layout == XGL_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL) { |
| 358 | /* Make sure any Copy or CPU writes to image are flushed */ |
| 359 | image_memory_barrier.outputMask = XGL_MEMORY_OUTPUT_COPY_BIT | XGL_MEMORY_OUTPUT_CPU_WRITE_BIT; |
| 360 | } |
| 361 | |
| 362 | XGL_IMAGE_MEMORY_BARRIER *pmemory_barrier = &image_memory_barrier; |
| 363 | |
| 364 | XGL_PIPE_EVENT set_events[] = { XGL_PIPE_EVENT_TOP_OF_PIPE }; |
| 365 | |
| 366 | XGL_PIPELINE_BARRIER pipeline_barrier; |
| 367 | pipeline_barrier.sType = XGL_STRUCTURE_TYPE_PIPELINE_BARRIER; |
| 368 | pipeline_barrier.pNext = NULL; |
| 369 | pipeline_barrier.eventCount = 1; |
| 370 | pipeline_barrier.pEvents = set_events; |
| 371 | pipeline_barrier.waitEvent = XGL_WAIT_EVENT_TOP_OF_PIPE; |
| 372 | pipeline_barrier.memBarrierCount = 1; |
| 373 | pipeline_barrier.ppMemBarriers = (const void **)&pmemory_barrier; |
| 374 | |
| 375 | xglCmdPipelineBarrier(demo->cmd, &pipeline_barrier); |
| 376 | } |
| 377 | |
Courtney Goeltzenleuchter | efc58ae | 2015-02-17 09:48:44 -0700 | [diff] [blame] | 378 | static void demo_draw_build_cmd(struct demo *demo, XGL_CMD_BUFFER cmd_buf) |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 379 | { |
| 380 | const XGL_COLOR_ATTACHMENT_BIND_INFO color_attachment = { |
| 381 | .view = demo->buffers[demo->current_buffer].view, |
Mike Stroyan | 3bd2d89 | 2014-12-04 11:08:39 +0000 | [diff] [blame] | 382 | .layout = XGL_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 383 | }; |
| 384 | const XGL_DEPTH_STENCIL_BIND_INFO depth_stencil = { |
| 385 | .view = demo->depth.view, |
Mike Stroyan | 3bd2d89 | 2014-12-04 11:08:39 +0000 | [diff] [blame] | 386 | .layout = XGL_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL, |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 387 | }; |
Courtney Goeltzenleuchter | 374553c | 2015-04-03 16:35:32 -0600 | [diff] [blame] | 388 | const XGL_CLEAR_COLOR clear_color = { |
| 389 | .color.floatColor = { 0.2f, 0.2f, 0.2f, 0.2f }, |
| 390 | .useRawValue = false, |
| 391 | }; |
Mark Lobodzinski | c4fffc7 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 392 | const float clear_depth = 1.0f; |
Courtney Goeltzenleuchter | fc3b953 | 2014-10-28 10:32:57 -0600 | [diff] [blame] | 393 | XGL_IMAGE_SUBRESOURCE_RANGE clear_range; |
Jon Ashburn | 60ccfbe | 2014-12-31 17:08:35 -0700 | [diff] [blame] | 394 | XGL_CMD_BUFFER_BEGIN_INFO cmd_buf_info = { |
| 395 | .sType = XGL_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO, |
Courtney Goeltzenleuchter | 53968d8 | 2015-04-03 15:25:24 -0600 | [diff] [blame] | 396 | .pNext = NULL, |
Jon Ashburn | 60ccfbe | 2014-12-31 17:08:35 -0700 | [diff] [blame] | 397 | .flags = XGL_CMD_BUFFER_OPTIMIZE_GPU_SMALL_BATCH_BIT | |
| 398 | XGL_CMD_BUFFER_OPTIMIZE_ONE_TIME_SUBMIT_BIT, |
| 399 | }; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 400 | XGL_RESULT err; |
Jon Ashburn | 08f6eb9 | 2015-01-02 18:24:05 -0700 | [diff] [blame] | 401 | XGL_ATTACHMENT_LOAD_OP load_op = XGL_ATTACHMENT_LOAD_OP_DONT_CARE; |
| 402 | XGL_ATTACHMENT_STORE_OP store_op = XGL_ATTACHMENT_STORE_OP_DONT_CARE; |
| 403 | const XGL_FRAMEBUFFER_CREATE_INFO fb_info = { |
| 404 | .sType = XGL_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO, |
| 405 | .pNext = NULL, |
| 406 | .colorAttachmentCount = 1, |
| 407 | .pColorAttachments = (XGL_COLOR_ATTACHMENT_BIND_INFO*) &color_attachment, |
| 408 | .pDepthStencilAttachment = (XGL_DEPTH_STENCIL_BIND_INFO*) &depth_stencil, |
| 409 | .sampleCount = 1, |
Mark Lobodzinski | c06b741 | 2015-01-27 13:24:03 -0600 | [diff] [blame] | 410 | .width = demo->width, |
| 411 | .height = demo->height, |
| 412 | .layers = 1, |
Jon Ashburn | 08f6eb9 | 2015-01-02 18:24:05 -0700 | [diff] [blame] | 413 | }; |
| 414 | XGL_RENDER_PASS_CREATE_INFO rp_info; |
Courtney Goeltzenleuchter | 53968d8 | 2015-04-03 15:25:24 -0600 | [diff] [blame] | 415 | XGL_RENDER_PASS_BEGIN rp_begin; |
Jon Ashburn | 08f6eb9 | 2015-01-02 18:24:05 -0700 | [diff] [blame] | 416 | |
| 417 | memset(&rp_info, 0 , sizeof(rp_info)); |
Courtney Goeltzenleuchter | 53968d8 | 2015-04-03 15:25:24 -0600 | [diff] [blame] | 418 | err = xglCreateFramebuffer(demo->device, &fb_info, &rp_begin.framebuffer); |
Jon Ashburn | 08f6eb9 | 2015-01-02 18:24:05 -0700 | [diff] [blame] | 419 | assert(!err); |
| 420 | rp_info.sType = XGL_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO; |
| 421 | rp_info.renderArea.extent.width = demo->width; |
| 422 | rp_info.renderArea.extent.height = demo->height; |
Courtney Goeltzenleuchter | 53968d8 | 2015-04-03 15:25:24 -0600 | [diff] [blame] | 423 | rp_info.colorAttachmentCount = fb_info.colorAttachmentCount; |
| 424 | rp_info.pColorFormats = &demo->format; |
| 425 | rp_info.pColorLayouts = &color_attachment.layout; |
Jon Ashburn | 08f6eb9 | 2015-01-02 18:24:05 -0700 | [diff] [blame] | 426 | rp_info.pColorLoadOps = &load_op; |
| 427 | rp_info.pColorStoreOps = &store_op; |
Courtney Goeltzenleuchter | 53968d8 | 2015-04-03 15:25:24 -0600 | [diff] [blame] | 428 | rp_info.pColorLoadClearValues = &clear_color; |
| 429 | rp_info.depthStencilFormat = XGL_FMT_D16_UNORM; |
| 430 | rp_info.depthStencilLayout = depth_stencil.layout; |
Jon Ashburn | 08f6eb9 | 2015-01-02 18:24:05 -0700 | [diff] [blame] | 431 | rp_info.depthLoadOp = XGL_ATTACHMENT_LOAD_OP_DONT_CARE; |
Courtney Goeltzenleuchter | 53968d8 | 2015-04-03 15:25:24 -0600 | [diff] [blame] | 432 | rp_info.depthLoadClearValue = clear_depth; |
Jon Ashburn | 08f6eb9 | 2015-01-02 18:24:05 -0700 | [diff] [blame] | 433 | rp_info.depthStoreOp = XGL_ATTACHMENT_STORE_OP_DONT_CARE; |
| 434 | rp_info.stencilLoadOp = XGL_ATTACHMENT_LOAD_OP_DONT_CARE; |
Courtney Goeltzenleuchter | 53968d8 | 2015-04-03 15:25:24 -0600 | [diff] [blame] | 435 | rp_info.stencilLoadClearValue = 0; |
Jon Ashburn | 08f6eb9 | 2015-01-02 18:24:05 -0700 | [diff] [blame] | 436 | rp_info.stencilStoreOp = XGL_ATTACHMENT_STORE_OP_DONT_CARE; |
Courtney Goeltzenleuchter | 53968d8 | 2015-04-03 15:25:24 -0600 | [diff] [blame] | 437 | err = xglCreateRenderPass(demo->device, &rp_info, &rp_begin.renderPass); |
Jon Ashburn | 08f6eb9 | 2015-01-02 18:24:05 -0700 | [diff] [blame] | 438 | assert(!err); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 439 | |
Courtney Goeltzenleuchter | efc58ae | 2015-02-17 09:48:44 -0700 | [diff] [blame] | 440 | err = xglBeginCommandBuffer(cmd_buf, &cmd_buf_info); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 441 | assert(!err); |
| 442 | |
Courtney Goeltzenleuchter | efc58ae | 2015-02-17 09:48:44 -0700 | [diff] [blame] | 443 | xglCmdBindPipeline(cmd_buf, XGL_PIPELINE_BIND_POINT_GRAPHICS, |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 444 | demo->pipeline); |
Chia-I Wu | a28bdde | 2015-03-28 15:23:55 +0800 | [diff] [blame] | 445 | xglCmdBindDescriptorSets(cmd_buf, XGL_PIPELINE_BIND_POINT_GRAPHICS, |
| 446 | demo->desc_layout_chain, 0, 1, &demo->desc_set, NULL); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 447 | |
Courtney Goeltzenleuchter | efc58ae | 2015-02-17 09:48:44 -0700 | [diff] [blame] | 448 | xglCmdBindDynamicStateObject(cmd_buf, XGL_STATE_BIND_VIEWPORT, demo->viewport); |
| 449 | xglCmdBindDynamicStateObject(cmd_buf, XGL_STATE_BIND_RASTER, demo->raster); |
| 450 | xglCmdBindDynamicStateObject(cmd_buf, XGL_STATE_BIND_COLOR_BLEND, |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 451 | demo->color_blend); |
Courtney Goeltzenleuchter | efc58ae | 2015-02-17 09:48:44 -0700 | [diff] [blame] | 452 | xglCmdBindDynamicStateObject(cmd_buf, XGL_STATE_BIND_DEPTH_STENCIL, |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 453 | demo->depth_stencil); |
| 454 | |
Courtney Goeltzenleuchter | 53968d8 | 2015-04-03 15:25:24 -0600 | [diff] [blame] | 455 | xglCmdBeginRenderPass(cmd_buf, &rp_begin); |
Courtney Goeltzenleuchter | fc3b953 | 2014-10-28 10:32:57 -0600 | [diff] [blame] | 456 | clear_range.aspect = XGL_IMAGE_ASPECT_COLOR; |
| 457 | clear_range.baseMipLevel = 0; |
| 458 | clear_range.mipLevels = 1; |
| 459 | clear_range.baseArraySlice = 0; |
| 460 | clear_range.arraySize = 1; |
Courtney Goeltzenleuchter | efc58ae | 2015-02-17 09:48:44 -0700 | [diff] [blame] | 461 | xglCmdClearColorImage(cmd_buf, |
Courtney Goeltzenleuchter | fc3b953 | 2014-10-28 10:32:57 -0600 | [diff] [blame] | 462 | demo->buffers[demo->current_buffer].image, |
Courtney Goeltzenleuchter | 8e89a31 | 2015-03-25 11:25:10 -0600 | [diff] [blame] | 463 | XGL_IMAGE_LAYOUT_CLEAR_OPTIMAL, |
Courtney Goeltzenleuchter | fc3b953 | 2014-10-28 10:32:57 -0600 | [diff] [blame] | 464 | clear_color, 1, &clear_range); |
| 465 | |
| 466 | clear_range.aspect = XGL_IMAGE_ASPECT_DEPTH; |
Courtney Goeltzenleuchter | efc58ae | 2015-02-17 09:48:44 -0700 | [diff] [blame] | 467 | xglCmdClearDepthStencil(cmd_buf, demo->depth.image, |
Courtney Goeltzenleuchter | 8e89a31 | 2015-03-25 11:25:10 -0600 | [diff] [blame] | 468 | XGL_IMAGE_LAYOUT_CLEAR_OPTIMAL, |
Courtney Goeltzenleuchter | fc3b953 | 2014-10-28 10:32:57 -0600 | [diff] [blame] | 469 | clear_depth, 0, 1, &clear_range); |
| 470 | |
Courtney Goeltzenleuchter | efc58ae | 2015-02-17 09:48:44 -0700 | [diff] [blame] | 471 | xglCmdDraw(cmd_buf, 0, 12 * 3, 0, 1); |
Courtney Goeltzenleuchter | 53968d8 | 2015-04-03 15:25:24 -0600 | [diff] [blame] | 472 | xglCmdEndRenderPass(cmd_buf, rp_begin.renderPass); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 473 | |
Courtney Goeltzenleuchter | efc58ae | 2015-02-17 09:48:44 -0700 | [diff] [blame] | 474 | err = xglEndCommandBuffer(cmd_buf); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 475 | assert(!err); |
Courtney Goeltzenleuchter | 94901dc | 2015-02-25 17:53:18 -0700 | [diff] [blame] | 476 | |
Courtney Goeltzenleuchter | 53968d8 | 2015-04-03 15:25:24 -0600 | [diff] [blame] | 477 | xglDestroyObject(rp_begin.renderPass); |
| 478 | xglDestroyObject(rp_begin.framebuffer); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 479 | } |
| 480 | |
Courtney Goeltzenleuchter | fc3b953 | 2014-10-28 10:32:57 -0600 | [diff] [blame] | 481 | |
Courtney Goeltzenleuchter | 8879d3a | 2014-10-29 08:29:35 -0600 | [diff] [blame] | 482 | void demo_update_data_buffer(struct demo *demo) |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 483 | { |
Courtney Goeltzenleuchter | fc3b953 | 2014-10-28 10:32:57 -0600 | [diff] [blame] | 484 | mat4x4 MVP, Model, VP; |
| 485 | int matrixSize = sizeof(MVP); |
Mark Lobodzinski | c4fffc7 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 486 | uint8_t *pData; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 487 | XGL_RESULT err; |
| 488 | |
Courtney Goeltzenleuchter | fc3b953 | 2014-10-28 10:32:57 -0600 | [diff] [blame] | 489 | mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 490 | |
Courtney Goeltzenleuchter | fc3b953 | 2014-10-28 10:32:57 -0600 | [diff] [blame] | 491 | // Rotate 22.5 degrees around the Y axis |
Courtney Goeltzenleuchter | 4984d15 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 492 | mat4x4_dup(Model, demo->model_matrix); |
Piers Daniell | 735ee53 | 2015-02-23 16:23:13 -0700 | [diff] [blame] | 493 | 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] | 494 | mat4x4_mul(MVP, VP, demo->model_matrix); |
Courtney Goeltzenleuchter | fc3b953 | 2014-10-28 10:32:57 -0600 | [diff] [blame] | 495 | |
Jon Ashburn | ae7c21c | 2015-01-19 15:00:26 -0700 | [diff] [blame] | 496 | assert(demo->uniform_data.num_mem == 1); |
Mark Lobodzinski | c4fffc7 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 497 | err = xglMapMemory(demo->uniform_data.mem[0], 0, (void **) &pData); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 498 | assert(!err); |
| 499 | |
Courtney Goeltzenleuchter | fc3b953 | 2014-10-28 10:32:57 -0600 | [diff] [blame] | 500 | memcpy(pData, (const void*) &MVP[0][0], matrixSize); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 501 | |
Jon Ashburn | ae7c21c | 2015-01-19 15:00:26 -0700 | [diff] [blame] | 502 | err = xglUnmapMemory(demo->uniform_data.mem[0]); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 503 | assert(!err); |
| 504 | } |
| 505 | |
| 506 | static void demo_draw(struct demo *demo) |
| 507 | { |
| 508 | const XGL_WSI_X11_PRESENT_INFO present = { |
| 509 | .destWindow = demo->window, |
| 510 | .srcImage = demo->buffers[demo->current_buffer].image, |
Chia-I Wu | bb57f64 | 2014-11-07 14:30:34 +0800 | [diff] [blame] | 511 | .async = true, |
| 512 | .flip = false, |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 513 | }; |
Chia-I Wu | bb57f64 | 2014-11-07 14:30:34 +0800 | [diff] [blame] | 514 | XGL_FENCE fence = demo->buffers[demo->current_buffer].fence; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 515 | XGL_RESULT err; |
| 516 | |
Mark Lobodzinski | c4fffc7 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 517 | err = xglWaitForFences(demo->device, 1, &fence, XGL_TRUE, ~((uint64_t) 0)); |
Chia-I Wu | bb57f64 | 2014-11-07 14:30:34 +0800 | [diff] [blame] | 518 | assert(err == XGL_SUCCESS || err == XGL_ERROR_UNAVAILABLE); |
| 519 | |
Courtney Goeltzenleuchter | efc58ae | 2015-02-17 09:48:44 -0700 | [diff] [blame] | 520 | err = xglQueueSubmit(demo->queue, 1, &demo->buffers[demo->current_buffer].cmd, |
Courtney Goeltzenleuchter | 2c2fbbf | 2015-04-07 17:13:38 -0600 | [diff] [blame] | 521 | XGL_NULL_HANDLE); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 522 | assert(!err); |
| 523 | |
Chia-I Wu | bb57f64 | 2014-11-07 14:30:34 +0800 | [diff] [blame] | 524 | err = xglWsiX11QueuePresent(demo->queue, &present, fence); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 525 | assert(!err); |
| 526 | |
| 527 | demo->current_buffer = (demo->current_buffer + 1) % DEMO_BUFFER_COUNT; |
| 528 | } |
| 529 | |
| 530 | static void demo_prepare_buffers(struct demo *demo) |
| 531 | { |
| 532 | const XGL_WSI_X11_PRESENTABLE_IMAGE_CREATE_INFO presentable_image = { |
| 533 | .format = demo->format, |
| 534 | .usage = XGL_IMAGE_USAGE_COLOR_ATTACHMENT_BIT, |
| 535 | .extent = { |
| 536 | .width = demo->width, |
| 537 | .height = demo->height, |
| 538 | }, |
| 539 | .flags = 0, |
| 540 | }; |
Chia-I Wu | bb57f64 | 2014-11-07 14:30:34 +0800 | [diff] [blame] | 541 | const XGL_FENCE_CREATE_INFO fence = { |
| 542 | .sType = XGL_STRUCTURE_TYPE_FENCE_CREATE_INFO, |
| 543 | .pNext = NULL, |
| 544 | .flags = 0, |
| 545 | }; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 546 | XGL_RESULT err; |
Mark Lobodzinski | c4fffc7 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 547 | uint32_t i; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 548 | |
| 549 | for (i = 0; i < DEMO_BUFFER_COUNT; i++) { |
| 550 | XGL_COLOR_ATTACHMENT_VIEW_CREATE_INFO color_attachment_view = { |
| 551 | .sType = XGL_STRUCTURE_TYPE_COLOR_ATTACHMENT_VIEW_CREATE_INFO, |
| 552 | .pNext = NULL, |
| 553 | .format = demo->format, |
| 554 | .mipLevel = 0, |
| 555 | .baseArraySlice = 0, |
| 556 | .arraySize = 1, |
| 557 | }; |
| 558 | |
| 559 | err = xglWsiX11CreatePresentableImage(demo->device, &presentable_image, |
| 560 | &demo->buffers[i].image, &demo->buffers[i].mem); |
| 561 | assert(!err); |
| 562 | |
Courtney Goeltzenleuchter | 2c2fbbf | 2015-04-07 17:13:38 -0600 | [diff] [blame] | 563 | demo_add_mem_refs(demo, 1, &demo->buffers[i].mem); |
Courtney Goeltzenleuchter | 1359b7a | 2015-04-02 16:25:42 -0600 | [diff] [blame] | 564 | |
Courtney Goeltzenleuchter | 752eb8e | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 565 | demo_set_image_layout(demo, demo->buffers[i].image, |
| 566 | XGL_IMAGE_LAYOUT_UNDEFINED, |
| 567 | XGL_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL); |
| 568 | |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 569 | color_attachment_view.image = demo->buffers[i].image; |
| 570 | |
| 571 | err = xglCreateColorAttachmentView(demo->device, |
| 572 | &color_attachment_view, &demo->buffers[i].view); |
| 573 | assert(!err); |
Chia-I Wu | bb57f64 | 2014-11-07 14:30:34 +0800 | [diff] [blame] | 574 | |
| 575 | err = xglCreateFence(demo->device, |
| 576 | &fence, &demo->buffers[i].fence); |
| 577 | assert(!err); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 578 | } |
| 579 | } |
| 580 | |
| 581 | static void demo_prepare_depth(struct demo *demo) |
| 582 | { |
Jeremy Hayes | 9958ea8 | 2015-01-23 08:51:43 -0700 | [diff] [blame] | 583 | const XGL_FORMAT depth_format = XGL_FMT_D16_UNORM; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 584 | const XGL_IMAGE_CREATE_INFO image = { |
| 585 | .sType = XGL_STRUCTURE_TYPE_IMAGE_CREATE_INFO, |
| 586 | .pNext = NULL, |
| 587 | .imageType = XGL_IMAGE_2D, |
| 588 | .format = depth_format, |
| 589 | .extent = { demo->width, demo->height, 1 }, |
| 590 | .mipLevels = 1, |
| 591 | .arraySize = 1, |
| 592 | .samples = 1, |
| 593 | .tiling = XGL_OPTIMAL_TILING, |
| 594 | .usage = XGL_IMAGE_USAGE_DEPTH_STENCIL_BIT, |
| 595 | .flags = 0, |
| 596 | }; |
Jon Ashburn | ae7c21c | 2015-01-19 15:00:26 -0700 | [diff] [blame] | 597 | XGL_MEMORY_ALLOC_IMAGE_INFO img_alloc = { |
| 598 | .sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_IMAGE_INFO, |
| 599 | .pNext = NULL, |
| 600 | }; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 601 | XGL_MEMORY_ALLOC_INFO mem_alloc = { |
| 602 | .sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_INFO, |
Jon Ashburn | ae7c21c | 2015-01-19 15:00:26 -0700 | [diff] [blame] | 603 | .pNext = &img_alloc, |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 604 | .allocationSize = 0, |
Jon Ashburn | c483a8c | 2015-01-20 13:55:32 -0700 | [diff] [blame] | 605 | .memProps = XGL_MEMORY_PROPERTY_GPU_ONLY, |
Jon Ashburn | bdee4e8 | 2015-01-20 15:06:59 -0700 | [diff] [blame] | 606 | .memType = XGL_MEMORY_TYPE_IMAGE, |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 607 | .memPriority = XGL_MEMORY_PRIORITY_NORMAL, |
| 608 | }; |
| 609 | XGL_DEPTH_STENCIL_VIEW_CREATE_INFO view = { |
| 610 | .sType = XGL_STRUCTURE_TYPE_DEPTH_STENCIL_VIEW_CREATE_INFO, |
| 611 | .pNext = NULL, |
| 612 | .image = XGL_NULL_HANDLE, |
| 613 | .mipLevel = 0, |
| 614 | .baseArraySlice = 0, |
| 615 | .arraySize = 1, |
| 616 | .flags = 0, |
| 617 | }; |
Jon Ashburn | b2a6665 | 2015-01-16 09:37:43 -0700 | [diff] [blame] | 618 | XGL_MEMORY_REQUIREMENTS *mem_reqs; |
Mark Lobodzinski | c4fffc7 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 619 | size_t mem_reqs_size = sizeof(XGL_MEMORY_REQUIREMENTS); |
Jon Ashburn | ae7c21c | 2015-01-19 15:00:26 -0700 | [diff] [blame] | 620 | XGL_IMAGE_MEMORY_REQUIREMENTS img_reqs; |
Mark Lobodzinski | c4fffc7 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 621 | size_t img_reqs_size = sizeof(XGL_IMAGE_MEMORY_REQUIREMENTS); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 622 | XGL_RESULT err; |
Mark Lobodzinski | c4fffc7 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 623 | uint32_t num_allocations = 0; |
| 624 | size_t num_alloc_size = sizeof(num_allocations); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 625 | |
| 626 | demo->depth.format = depth_format; |
| 627 | |
| 628 | /* create image */ |
| 629 | err = xglCreateImage(demo->device, &image, |
| 630 | &demo->depth.image); |
| 631 | assert(!err); |
| 632 | |
Jon Ashburn | b2a6665 | 2015-01-16 09:37:43 -0700 | [diff] [blame] | 633 | err = xglGetObjectInfo(demo->depth.image, XGL_INFO_TYPE_MEMORY_ALLOCATION_COUNT, &num_alloc_size, &num_allocations); |
| 634 | assert(!err && num_alloc_size == sizeof(num_allocations)); |
| 635 | mem_reqs = malloc(num_allocations * sizeof(XGL_MEMORY_REQUIREMENTS)); |
| 636 | demo->depth.mem = malloc(num_allocations * sizeof(XGL_GPU_MEMORY)); |
| 637 | demo->depth.num_mem = num_allocations; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 638 | err = xglGetObjectInfo(demo->depth.image, |
Jon Ashburn | b2a6665 | 2015-01-16 09:37:43 -0700 | [diff] [blame] | 639 | XGL_INFO_TYPE_MEMORY_REQUIREMENTS, |
| 640 | &mem_reqs_size, mem_reqs); |
| 641 | assert(!err && mem_reqs_size == num_allocations * sizeof(XGL_MEMORY_REQUIREMENTS)); |
Jon Ashburn | ae7c21c | 2015-01-19 15:00:26 -0700 | [diff] [blame] | 642 | err = xglGetObjectInfo(demo->depth.image, |
| 643 | XGL_INFO_TYPE_IMAGE_MEMORY_REQUIREMENTS, |
| 644 | &img_reqs_size, &img_reqs); |
| 645 | assert(!err && img_reqs_size == sizeof(XGL_IMAGE_MEMORY_REQUIREMENTS)); |
| 646 | img_alloc.usage = img_reqs.usage; |
| 647 | img_alloc.formatClass = img_reqs.formatClass; |
| 648 | img_alloc.samples = img_reqs.samples; |
Mark Lobodzinski | c4fffc7 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 649 | for (uint32_t i = 0; i < num_allocations; i ++) { |
Jon Ashburn | b2a6665 | 2015-01-16 09:37:43 -0700 | [diff] [blame] | 650 | mem_alloc.allocationSize = mem_reqs[i].size; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 651 | |
Jon Ashburn | b2a6665 | 2015-01-16 09:37:43 -0700 | [diff] [blame] | 652 | /* allocate memory */ |
| 653 | err = xglAllocMemory(demo->device, &mem_alloc, |
| 654 | &(demo->depth.mem[i])); |
| 655 | assert(!err); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 656 | |
Jon Ashburn | b2a6665 | 2015-01-16 09:37:43 -0700 | [diff] [blame] | 657 | /* bind memory */ |
| 658 | err = xglBindObjectMemory(demo->depth.image, i, |
| 659 | demo->depth.mem[i], 0); |
| 660 | assert(!err); |
| 661 | } |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 662 | |
Courtney Goeltzenleuchter | 752eb8e | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 663 | demo_set_image_layout(demo, demo->depth.image, |
| 664 | XGL_IMAGE_LAYOUT_UNDEFINED, |
| 665 | XGL_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL); |
| 666 | |
Courtney Goeltzenleuchter | 2c2fbbf | 2015-04-07 17:13:38 -0600 | [diff] [blame] | 667 | demo_add_mem_refs(demo, demo->depth.num_mem, demo->depth.mem); |
Courtney Goeltzenleuchter | 752eb8e | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 668 | |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 669 | /* create image view */ |
| 670 | view.image = demo->depth.image; |
| 671 | err = xglCreateDepthStencilView(demo->device, &view, |
| 672 | &demo->depth.view); |
| 673 | assert(!err); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 674 | } |
| 675 | |
Courtney Goeltzenleuchter | 6836939 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 676 | /** loadTexture |
| 677 | * loads a png file into an memory object, using cstdio , libpng. |
| 678 | * |
| 679 | * \param demo : Needed to access XGL calls |
| 680 | * \param filename : the png file to be loaded |
| 681 | * \param width : width of png, to be updated as a side effect of this function |
| 682 | * \param height : height of png, to be updated as a side effect of this function |
| 683 | * |
| 684 | * \return bool : an opengl texture id. true if successful?, |
| 685 | * should be validated by the client of this function. |
| 686 | * |
| 687 | * Source: http://en.wikibooks.org/wiki/OpenGL_Programming/Intermediate/Textures |
| 688 | * Modified to copy image to memory |
| 689 | * |
| 690 | */ |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 691 | bool loadTexture(const char *filename, uint8_t *rgba_data, |
Courtney Goeltzenleuchter | 6836939 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 692 | XGL_SUBRESOURCE_LAYOUT *layout, |
Mark Lobodzinski | c4fffc7 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 693 | int32_t *width, int32_t *height) |
Courtney Goeltzenleuchter | 6836939 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 694 | { |
| 695 | //header for testing if it is a png |
| 696 | png_byte header[8]; |
Ian Elliott | 1e42dff | 2015-02-13 14:29:21 -0700 | [diff] [blame] | 697 | int is_png, bit_depth, color_type,rowbytes; |
| 698 | png_uint_32 i, twidth, theight; |
Courtney Goeltzenleuchter | 6836939 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 699 | png_structp png_ptr; |
| 700 | png_infop info_ptr, end_info; |
| 701 | png_byte *image_data; |
| 702 | png_bytep *row_pointers; |
| 703 | |
| 704 | //open file as binary |
| 705 | FILE *fp = fopen(filename, "rb"); |
| 706 | if (!fp) { |
| 707 | return false; |
| 708 | } |
| 709 | |
| 710 | //read the header |
| 711 | fread(header, 1, 8, fp); |
| 712 | |
| 713 | //test if png |
| 714 | is_png = !png_sig_cmp(header, 0, 8); |
| 715 | if (!is_png) { |
| 716 | fclose(fp); |
| 717 | return false; |
| 718 | } |
| 719 | |
| 720 | //create png struct |
| 721 | png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, |
| 722 | NULL, NULL); |
| 723 | if (!png_ptr) { |
| 724 | fclose(fp); |
| 725 | return (false); |
| 726 | } |
| 727 | |
| 728 | //create png info struct |
| 729 | info_ptr = png_create_info_struct(png_ptr); |
| 730 | if (!info_ptr) { |
| 731 | png_destroy_read_struct(&png_ptr, (png_infopp) NULL, (png_infopp) NULL); |
| 732 | fclose(fp); |
| 733 | return (false); |
| 734 | } |
| 735 | |
| 736 | //create png info struct |
| 737 | end_info = png_create_info_struct(png_ptr); |
| 738 | if (!end_info) { |
| 739 | png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp) NULL); |
| 740 | fclose(fp); |
| 741 | return (false); |
| 742 | } |
| 743 | |
| 744 | //png error stuff, not sure libpng man suggests this. |
| 745 | if (setjmp(png_jmpbuf(png_ptr))) { |
| 746 | png_destroy_read_struct(&png_ptr, &info_ptr, &end_info); |
| 747 | fclose(fp); |
| 748 | return (false); |
| 749 | } |
| 750 | |
| 751 | //init png reading |
| 752 | png_init_io(png_ptr, fp); |
| 753 | |
| 754 | //let libpng know you already read the first 8 bytes |
| 755 | png_set_sig_bytes(png_ptr, 8); |
| 756 | |
| 757 | // read all the info up to the image data |
| 758 | png_read_info(png_ptr, info_ptr); |
| 759 | |
| 760 | // get info about png |
| 761 | png_get_IHDR(png_ptr, info_ptr, &twidth, &theight, &bit_depth, &color_type, |
| 762 | NULL, NULL, NULL); |
| 763 | |
| 764 | //update width and height based on png info |
| 765 | *width = twidth; |
| 766 | *height = theight; |
| 767 | |
| 768 | // Require that incoming texture be 8bits per color component |
| 769 | // and 4 components (RGBA). |
| 770 | if (png_get_bit_depth(png_ptr, info_ptr) != 8 || |
| 771 | png_get_channels(png_ptr, info_ptr) != 4) { |
| 772 | return false; |
| 773 | } |
| 774 | |
| 775 | if (rgba_data == NULL) { |
| 776 | // If data pointer is null, we just want the width & height |
| 777 | // clean up memory and close stuff |
| 778 | png_destroy_read_struct(&png_ptr, &info_ptr, &end_info); |
| 779 | fclose(fp); |
| 780 | |
| 781 | return true; |
| 782 | } |
| 783 | |
| 784 | // Update the png info struct. |
| 785 | png_read_update_info(png_ptr, info_ptr); |
| 786 | |
| 787 | // Row size in bytes. |
| 788 | rowbytes = png_get_rowbytes(png_ptr, info_ptr); |
| 789 | |
| 790 | // Allocate the image_data as a big block, to be given to opengl |
| 791 | image_data = (png_byte *)malloc(rowbytes * theight * sizeof(png_byte)); |
| 792 | if (!image_data) { |
| 793 | //clean up memory and close stuff |
| 794 | png_destroy_read_struct(&png_ptr, &info_ptr, &end_info); |
| 795 | fclose(fp); |
| 796 | return false; |
| 797 | } |
| 798 | |
| 799 | // row_pointers is for pointing to image_data for reading the png with libpng |
| 800 | row_pointers = (png_bytep *)malloc(theight * sizeof(png_bytep)); |
| 801 | if (!row_pointers) { |
| 802 | //clean up memory and close stuff |
| 803 | png_destroy_read_struct(&png_ptr, &info_ptr, &end_info); |
| 804 | // delete[] image_data; |
| 805 | fclose(fp); |
| 806 | return false; |
| 807 | } |
| 808 | // set the individual row_pointers to point at the correct offsets of image_data |
| 809 | for (i = 0; i < theight; ++i) |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 810 | row_pointers[theight - 1 - i] = rgba_data + i * layout->rowPitch; |
Courtney Goeltzenleuchter | 6836939 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 811 | |
| 812 | // read the png into image_data through row_pointers |
| 813 | png_read_image(png_ptr, row_pointers); |
| 814 | |
| 815 | // clean up memory and close stuff |
| 816 | png_destroy_read_struct(&png_ptr, &info_ptr, &end_info); |
| 817 | free(row_pointers); |
| 818 | free(image_data); |
| 819 | fclose(fp); |
| 820 | |
| 821 | return true; |
| 822 | } |
Courtney Goeltzenleuchter | 6836939 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 823 | |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 824 | static void demo_prepare_texture_image(struct demo *demo, |
| 825 | const char *filename, |
Courtney Goeltzenleuchter | 752eb8e | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 826 | struct texture_object *tex_obj, |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 827 | XGL_IMAGE_TILING tiling, |
| 828 | XGL_FLAGS mem_props) |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 829 | { |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 830 | const XGL_FORMAT tex_format = XGL_FMT_B8G8R8A8_UNORM; |
Mark Lobodzinski | c4fffc7 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 831 | int32_t tex_width; |
| 832 | int32_t tex_height; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 833 | XGL_RESULT err; |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 834 | |
| 835 | err = loadTexture(filename, NULL, NULL, &tex_width, &tex_height); |
| 836 | assert(err); |
| 837 | |
Courtney Goeltzenleuchter | 752eb8e | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 838 | tex_obj->tex_width = tex_width; |
| 839 | tex_obj->tex_height = tex_height; |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 840 | |
| 841 | const XGL_IMAGE_CREATE_INFO image_create_info = { |
| 842 | .sType = XGL_STRUCTURE_TYPE_IMAGE_CREATE_INFO, |
| 843 | .pNext = NULL, |
| 844 | .imageType = XGL_IMAGE_2D, |
| 845 | .format = tex_format, |
| 846 | .extent = { tex_width, tex_height, 1 }, |
| 847 | .mipLevels = 1, |
| 848 | .arraySize = 1, |
| 849 | .samples = 1, |
| 850 | .tiling = tiling, |
| 851 | .usage = XGL_IMAGE_USAGE_TRANSFER_SOURCE_BIT, |
| 852 | .flags = 0, |
| 853 | }; |
Piers Daniell | 735ee53 | 2015-02-23 16:23:13 -0700 | [diff] [blame] | 854 | XGL_MEMORY_ALLOC_BUFFER_INFO buf_alloc = { |
| 855 | .sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_BUFFER_INFO, |
| 856 | .pNext = NULL, |
| 857 | }; |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 858 | XGL_MEMORY_ALLOC_IMAGE_INFO img_alloc = { |
| 859 | .sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_IMAGE_INFO, |
Piers Daniell | 735ee53 | 2015-02-23 16:23:13 -0700 | [diff] [blame] | 860 | .pNext = &buf_alloc, |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 861 | }; |
| 862 | XGL_MEMORY_ALLOC_INFO mem_alloc = { |
| 863 | .sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_INFO, |
| 864 | .pNext = &img_alloc, |
| 865 | .allocationSize = 0, |
| 866 | .memProps = mem_props, |
| 867 | .memType = XGL_MEMORY_TYPE_IMAGE, |
| 868 | .memPriority = XGL_MEMORY_PRIORITY_NORMAL, |
| 869 | }; |
| 870 | |
| 871 | XGL_MEMORY_REQUIREMENTS *mem_reqs; |
| 872 | size_t mem_reqs_size = sizeof(XGL_MEMORY_REQUIREMENTS); |
Piers Daniell | 735ee53 | 2015-02-23 16:23:13 -0700 | [diff] [blame] | 873 | XGL_BUFFER_MEMORY_REQUIREMENTS buf_reqs; |
| 874 | size_t buf_reqs_size = sizeof(XGL_BUFFER_MEMORY_REQUIREMENTS); |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 875 | XGL_IMAGE_MEMORY_REQUIREMENTS img_reqs; |
| 876 | size_t img_reqs_size = sizeof(XGL_IMAGE_MEMORY_REQUIREMENTS); |
| 877 | uint32_t num_allocations = 0; |
| 878 | size_t num_alloc_size = sizeof(num_allocations); |
| 879 | |
| 880 | err = xglCreateImage(demo->device, &image_create_info, |
Courtney Goeltzenleuchter | 752eb8e | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 881 | &tex_obj->image); |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 882 | assert(!err); |
| 883 | |
Courtney Goeltzenleuchter | 752eb8e | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 884 | err = xglGetObjectInfo(tex_obj->image, |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 885 | XGL_INFO_TYPE_MEMORY_ALLOCATION_COUNT, |
| 886 | &num_alloc_size, &num_allocations); |
| 887 | assert(!err && num_alloc_size == sizeof(num_allocations)); |
| 888 | mem_reqs = malloc(num_allocations * sizeof(XGL_MEMORY_REQUIREMENTS)); |
Courtney Goeltzenleuchter | 752eb8e | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 889 | tex_obj->mem = malloc(num_allocations * sizeof(XGL_GPU_MEMORY)); |
| 890 | err = xglGetObjectInfo(tex_obj->image, |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 891 | XGL_INFO_TYPE_MEMORY_REQUIREMENTS, |
| 892 | &mem_reqs_size, mem_reqs); |
| 893 | assert(!err && mem_reqs_size == num_allocations * sizeof(XGL_MEMORY_REQUIREMENTS)); |
Courtney Goeltzenleuchter | 752eb8e | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 894 | err = xglGetObjectInfo(tex_obj->image, |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 895 | XGL_INFO_TYPE_IMAGE_MEMORY_REQUIREMENTS, |
| 896 | &img_reqs_size, &img_reqs); |
| 897 | assert(!err && img_reqs_size == sizeof(XGL_IMAGE_MEMORY_REQUIREMENTS)); |
| 898 | img_alloc.usage = img_reqs.usage; |
| 899 | img_alloc.formatClass = img_reqs.formatClass; |
| 900 | img_alloc.samples = img_reqs.samples; |
| 901 | mem_alloc.memProps = XGL_MEMORY_PROPERTY_CPU_VISIBLE_BIT; |
| 902 | for (uint32_t j = 0; j < num_allocations; j ++) { |
Courtney Goeltzenleuchter | 02ba99a | 2015-02-25 11:48:28 -0700 | [diff] [blame] | 903 | mem_alloc.memType = mem_reqs[j].memType; |
Piers Daniell | 735ee53 | 2015-02-23 16:23:13 -0700 | [diff] [blame] | 904 | mem_alloc.allocationSize = mem_reqs[j].size; |
| 905 | |
| 906 | if (mem_alloc.memType == XGL_MEMORY_TYPE_BUFFER) { |
Courtney Goeltzenleuchter | 752eb8e | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 907 | err = xglGetObjectInfo(tex_obj->image, |
Piers Daniell | 735ee53 | 2015-02-23 16:23:13 -0700 | [diff] [blame] | 908 | XGL_INFO_TYPE_BUFFER_MEMORY_REQUIREMENTS, |
| 909 | &buf_reqs_size, &buf_reqs); |
| 910 | assert(!err && buf_reqs_size == sizeof(XGL_BUFFER_MEMORY_REQUIREMENTS)); |
| 911 | buf_alloc.usage = buf_reqs.usage; |
| 912 | img_alloc.pNext = &buf_alloc; |
| 913 | } else { |
| 914 | img_alloc.pNext = 0; |
| 915 | } |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 916 | |
| 917 | /* allocate memory */ |
| 918 | err = xglAllocMemory(demo->device, &mem_alloc, |
Courtney Goeltzenleuchter | 752eb8e | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 919 | &(tex_obj->mem[j])); |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 920 | assert(!err); |
| 921 | |
| 922 | /* bind memory */ |
Courtney Goeltzenleuchter | 752eb8e | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 923 | err = xglBindObjectMemory(tex_obj->image, j, tex_obj->mem[j], 0); |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 924 | assert(!err); |
| 925 | } |
| 926 | free(mem_reqs); |
| 927 | mem_reqs = NULL; |
| 928 | |
Courtney Goeltzenleuchter | 752eb8e | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 929 | tex_obj->num_mem = num_allocations; |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 930 | |
| 931 | if (mem_props & XGL_MEMORY_PROPERTY_CPU_VISIBLE_BIT) { |
| 932 | const XGL_IMAGE_SUBRESOURCE subres = { |
| 933 | .aspect = XGL_IMAGE_ASPECT_COLOR, |
| 934 | .mipLevel = 0, |
| 935 | .arraySlice = 0, |
| 936 | }; |
| 937 | XGL_SUBRESOURCE_LAYOUT layout; |
| 938 | size_t layout_size = sizeof(XGL_SUBRESOURCE_LAYOUT); |
| 939 | void *data; |
| 940 | |
Courtney Goeltzenleuchter | 752eb8e | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 941 | err = xglGetImageSubresourceInfo(tex_obj->image, &subres, |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 942 | XGL_INFO_TYPE_SUBRESOURCE_LAYOUT, |
| 943 | &layout_size, &layout); |
| 944 | assert(!err && layout_size == sizeof(layout)); |
| 945 | /* Linear texture must be within a single memory object */ |
| 946 | assert(num_allocations == 1); |
| 947 | |
Courtney Goeltzenleuchter | 752eb8e | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 948 | err = xglMapMemory(tex_obj->mem[0], 0, &data); |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 949 | assert(!err); |
| 950 | |
| 951 | if (!loadTexture(filename, data, &layout, &tex_width, &tex_height)) { |
| 952 | fprintf(stderr, "Error loading texture: %s\n", filename); |
| 953 | } |
| 954 | |
Courtney Goeltzenleuchter | 752eb8e | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 955 | err = xglUnmapMemory(tex_obj->mem[0]); |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 956 | assert(!err); |
| 957 | } |
Courtney Goeltzenleuchter | 752eb8e | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 958 | |
| 959 | tex_obj->imageLayout = XGL_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL; |
| 960 | demo_set_image_layout(demo, tex_obj->image, |
| 961 | XGL_IMAGE_LAYOUT_UNDEFINED, |
| 962 | tex_obj->imageLayout); |
| 963 | /* 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] | 964 | } |
| 965 | |
Courtney Goeltzenleuchter | 752eb8e | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 966 | static void demo_destroy_texture_image(struct texture_object *tex_objs) |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 967 | { |
| 968 | /* clean up staging resources */ |
| 969 | for (uint32_t j = 0; j < tex_objs->num_mem; j ++) { |
| 970 | xglBindObjectMemory(tex_objs->image, j, XGL_NULL_HANDLE, 0); |
| 971 | xglFreeMemory(tex_objs->mem[j]); |
| 972 | } |
| 973 | |
| 974 | free(tex_objs->mem); |
| 975 | xglDestroyObject(tex_objs->image); |
| 976 | } |
| 977 | |
| 978 | static void demo_prepare_textures(struct demo *demo) |
| 979 | { |
| 980 | const XGL_FORMAT tex_format = XGL_FMT_R8G8B8A8_UNORM; |
| 981 | XGL_FORMAT_PROPERTIES props; |
| 982 | size_t size = sizeof(props); |
| 983 | XGL_RESULT err; |
Mark Lobodzinski | c4fffc7 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 984 | uint32_t i; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 985 | |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 986 | err = xglGetFormatInfo(demo->device, tex_format, |
| 987 | XGL_INFO_TYPE_FORMAT_PROPERTIES, |
| 988 | &size, &props); |
| 989 | assert(!err); |
| 990 | |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 991 | for (i = 0; i < DEMO_TEXTURE_COUNT; i++) { |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 992 | |
| 993 | if (props.linearTilingFeatures & XGL_FORMAT_IMAGE_SHADER_READ_BIT && !demo->use_staging_buffer) { |
| 994 | /* Device can texture using linear textures */ |
| 995 | demo_prepare_texture_image(demo, tex_files[i], &demo->textures[i], |
| 996 | XGL_LINEAR_TILING, XGL_MEMORY_PROPERTY_CPU_VISIBLE_BIT); |
Courtney Goeltzenleuchter | 752eb8e | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 997 | } else if (props.optimalTilingFeatures & XGL_FORMAT_IMAGE_SHADER_READ_BIT) { |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 998 | /* Must use staging buffer to copy linear texture to optimized */ |
Courtney Goeltzenleuchter | 752eb8e | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 999 | struct texture_object staging_texture; |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 1000 | |
| 1001 | memset(&staging_texture, 0, sizeof(staging_texture)); |
| 1002 | demo_prepare_texture_image(demo, tex_files[i], &staging_texture, |
| 1003 | XGL_LINEAR_TILING, XGL_MEMORY_PROPERTY_CPU_VISIBLE_BIT); |
| 1004 | |
| 1005 | demo_prepare_texture_image(demo, tex_files[i], &demo->textures[i], |
| 1006 | XGL_OPTIMAL_TILING, XGL_MEMORY_PROPERTY_GPU_ONLY); |
| 1007 | |
Courtney Goeltzenleuchter | 752eb8e | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 1008 | demo_set_image_layout(demo, staging_texture.image, |
| 1009 | staging_texture.imageLayout, |
| 1010 | XGL_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL); |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 1011 | |
Courtney Goeltzenleuchter | 752eb8e | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 1012 | demo_set_image_layout(demo, demo->textures[i].image, |
| 1013 | demo->textures[i].imageLayout, |
| 1014 | XGL_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL); |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 1015 | |
| 1016 | XGL_IMAGE_COPY copy_region = { |
| 1017 | .srcSubresource = { XGL_IMAGE_ASPECT_COLOR, 0, 0 }, |
| 1018 | .srcOffset = { 0, 0, 0 }, |
| 1019 | .destSubresource = { XGL_IMAGE_ASPECT_COLOR, 0, 0 }, |
| 1020 | .destOffset = { 0, 0, 0 }, |
| 1021 | .extent = { staging_texture.tex_width, staging_texture.tex_height, 1 }, |
| 1022 | }; |
Courtney Goeltzenleuchter | 752eb8e | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 1023 | xglCmdCopyImage(demo->cmd, |
| 1024 | staging_texture.image, XGL_IMAGE_LAYOUT_TRANSFER_SOURCE_OPTIMAL, |
| 1025 | demo->textures[i].image, XGL_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL, |
Courtney Goeltzenleuchter | 8e89a31 | 2015-03-25 11:25:10 -0600 | [diff] [blame] | 1026 | 1, ©_region); |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 1027 | |
Courtney Goeltzenleuchter | 2c2fbbf | 2015-04-07 17:13:38 -0600 | [diff] [blame] | 1028 | demo_add_mem_refs(demo, staging_texture.num_mem, staging_texture.mem); |
| 1029 | 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] | 1030 | |
Courtney Goeltzenleuchter | 752eb8e | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 1031 | demo_set_image_layout(demo, demo->textures[i].image, |
| 1032 | XGL_IMAGE_LAYOUT_TRANSFER_DESTINATION_OPTIMAL, |
| 1033 | demo->textures[i].imageLayout); |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 1034 | |
Courtney Goeltzenleuchter | 752eb8e | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 1035 | demo_flush_init_cmd(demo); |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 1036 | |
| 1037 | demo_destroy_texture_image(&staging_texture); |
Courtney Goeltzenleuchter | 1359b7a | 2015-04-02 16:25:42 -0600 | [diff] [blame] | 1038 | demo_remove_mem_refs(demo, staging_texture.num_mem, staging_texture.mem); |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 1039 | } else { |
| 1040 | /* Can't support XGL_FMT_B8G8R8A8_UNORM !? */ |
| 1041 | assert(!"No support for tB8G8R8A8_UNORM as texture image format"); |
| 1042 | } |
| 1043 | |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1044 | const XGL_SAMPLER_CREATE_INFO sampler = { |
| 1045 | .sType = XGL_STRUCTURE_TYPE_SAMPLER_CREATE_INFO, |
| 1046 | .pNext = NULL, |
| 1047 | .magFilter = XGL_TEX_FILTER_NEAREST, |
| 1048 | .minFilter = XGL_TEX_FILTER_NEAREST, |
| 1049 | .mipMode = XGL_TEX_MIPMAP_BASE, |
Courtney Goeltzenleuchter | 6836939 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 1050 | .addressU = XGL_TEX_ADDRESS_CLAMP, |
| 1051 | .addressV = XGL_TEX_ADDRESS_CLAMP, |
| 1052 | .addressW = XGL_TEX_ADDRESS_CLAMP, |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1053 | .mipLodBias = 0.0f, |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 1054 | .maxAnisotropy = 1, |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1055 | .compareFunc = XGL_COMPARE_NEVER, |
| 1056 | .minLod = 0.0f, |
| 1057 | .maxLod = 0.0f, |
| 1058 | .borderColorType = XGL_BORDER_COLOR_OPAQUE_WHITE, |
| 1059 | }; |
Courtney Goeltzenleuchter | 6836939 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 1060 | |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1061 | XGL_IMAGE_VIEW_CREATE_INFO view = { |
| 1062 | .sType = XGL_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO, |
| 1063 | .pNext = NULL, |
| 1064 | .image = XGL_NULL_HANDLE, |
| 1065 | .viewType = XGL_IMAGE_VIEW_2D, |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 1066 | .format = tex_format, |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1067 | .channels = { XGL_CHANNEL_SWIZZLE_R, |
| 1068 | XGL_CHANNEL_SWIZZLE_G, |
| 1069 | XGL_CHANNEL_SWIZZLE_B, |
| 1070 | XGL_CHANNEL_SWIZZLE_A, }, |
| 1071 | .subresourceRange = { XGL_IMAGE_ASPECT_COLOR, 0, 1, 0, 1 }, |
| 1072 | .minLod = 0.0f, |
| 1073 | }; |
Jon Ashburn | b2a6665 | 2015-01-16 09:37:43 -0700 | [diff] [blame] | 1074 | |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1075 | /* create sampler */ |
| 1076 | err = xglCreateSampler(demo->device, &sampler, |
| 1077 | &demo->textures[i].sampler); |
| 1078 | assert(!err); |
| 1079 | |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1080 | /* create image view */ |
| 1081 | view.image = demo->textures[i].image; |
| 1082 | err = xglCreateImageView(demo->device, &view, |
| 1083 | &demo->textures[i].view); |
| 1084 | assert(!err); |
| 1085 | } |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1086 | } |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1087 | |
Courtney Goeltzenleuchter | 4984d15 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 1088 | void demo_prepare_cube_data_buffer(struct demo *demo) |
| 1089 | { |
Chia-I Wu | 8ea21c7 | 2015-01-01 07:55:04 +0800 | [diff] [blame] | 1090 | XGL_BUFFER_CREATE_INFO buf_info; |
| 1091 | XGL_BUFFER_VIEW_CREATE_INFO view_info; |
Jon Ashburn | ae7c21c | 2015-01-19 15:00:26 -0700 | [diff] [blame] | 1092 | XGL_MEMORY_ALLOC_BUFFER_INFO buf_alloc = { |
| 1093 | .sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_BUFFER_INFO, |
| 1094 | .pNext = NULL, |
| 1095 | }; |
| 1096 | XGL_MEMORY_ALLOC_INFO alloc_info = { |
| 1097 | .sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_INFO, |
| 1098 | .pNext = &buf_alloc, |
| 1099 | .allocationSize = 0, |
Jon Ashburn | c483a8c | 2015-01-20 13:55:32 -0700 | [diff] [blame] | 1100 | .memProps = XGL_MEMORY_PROPERTY_CPU_VISIBLE_BIT, |
Jon Ashburn | bdee4e8 | 2015-01-20 15:06:59 -0700 | [diff] [blame] | 1101 | .memType = XGL_MEMORY_TYPE_BUFFER, |
Jon Ashburn | ae7c21c | 2015-01-19 15:00:26 -0700 | [diff] [blame] | 1102 | .memPriority = XGL_MEMORY_PRIORITY_NORMAL, |
| 1103 | }; |
| 1104 | XGL_MEMORY_REQUIREMENTS *mem_reqs; |
Mark Lobodzinski | c4fffc7 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 1105 | size_t mem_reqs_size = sizeof(XGL_MEMORY_REQUIREMENTS); |
Jon Ashburn | ae7c21c | 2015-01-19 15:00:26 -0700 | [diff] [blame] | 1106 | XGL_BUFFER_MEMORY_REQUIREMENTS buf_reqs; |
Mark Lobodzinski | c4fffc7 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 1107 | size_t buf_reqs_size = sizeof(XGL_BUFFER_MEMORY_REQUIREMENTS); |
| 1108 | uint32_t num_allocations = 0; |
| 1109 | size_t num_alloc_size = sizeof(num_allocations); |
| 1110 | uint8_t *pData; |
Courtney Goeltzenleuchter | 4984d15 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 1111 | int i; |
| 1112 | mat4x4 MVP, VP; |
Courtney Goeltzenleuchter | 4984d15 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 1113 | XGL_RESULT err; |
| 1114 | struct xgltexcube_vs_uniform data; |
| 1115 | |
Courtney Goeltzenleuchter | 4984d15 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 1116 | mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix); |
Courtney Goeltzenleuchter | 4984d15 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 1117 | mat4x4_mul(MVP, VP, demo->model_matrix); |
| 1118 | memcpy(data.mvp, MVP, sizeof(MVP)); |
Courtney Goeltzenleuchter | 8879d3a | 2014-10-29 08:29:35 -0600 | [diff] [blame] | 1119 | // dumpMatrix("MVP", MVP); |
Courtney Goeltzenleuchter | 4984d15 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 1120 | |
| 1121 | for (i=0; i<12*3; i++) { |
| 1122 | data.position[i][0] = g_vertex_buffer_data[i*3]; |
| 1123 | data.position[i][1] = g_vertex_buffer_data[i*3+1]; |
| 1124 | data.position[i][2] = g_vertex_buffer_data[i*3+2]; |
| 1125 | data.position[i][3] = 1.0f; |
| 1126 | data.attr[i][0] = g_uv_buffer_data[2*i]; |
| 1127 | data.attr[i][1] = g_uv_buffer_data[2*i + 1]; |
| 1128 | data.attr[i][2] = 0; |
| 1129 | data.attr[i][3] = 0; |
| 1130 | } |
| 1131 | |
Chia-I Wu | 8ea21c7 | 2015-01-01 07:55:04 +0800 | [diff] [blame] | 1132 | memset(&buf_info, 0, sizeof(buf_info)); |
| 1133 | buf_info.sType = XGL_STRUCTURE_TYPE_BUFFER_CREATE_INFO; |
| 1134 | buf_info.size = sizeof(data); |
| 1135 | buf_info.usage = XGL_BUFFER_USAGE_UNIFORM_READ_BIT; |
| 1136 | err = xglCreateBuffer(demo->device, &buf_info, &demo->uniform_data.buf); |
| 1137 | assert(!err); |
| 1138 | |
| 1139 | err = xglGetObjectInfo(demo->uniform_data.buf, |
Jon Ashburn | ae7c21c | 2015-01-19 15:00:26 -0700 | [diff] [blame] | 1140 | XGL_INFO_TYPE_MEMORY_ALLOCATION_COUNT, |
| 1141 | &num_alloc_size, &num_allocations); |
| 1142 | assert(!err && num_alloc_size == sizeof(num_allocations)); |
| 1143 | mem_reqs = malloc(num_allocations * sizeof(XGL_MEMORY_REQUIREMENTS)); |
| 1144 | demo->uniform_data.mem = malloc(num_allocations * sizeof(XGL_GPU_MEMORY)); |
| 1145 | demo->uniform_data.num_mem = num_allocations; |
| 1146 | err = xglGetObjectInfo(demo->uniform_data.buf, |
Chia-I Wu | 8ea21c7 | 2015-01-01 07:55:04 +0800 | [diff] [blame] | 1147 | XGL_INFO_TYPE_MEMORY_REQUIREMENTS, |
Jon Ashburn | ae7c21c | 2015-01-19 15:00:26 -0700 | [diff] [blame] | 1148 | &mem_reqs_size, mem_reqs); |
| 1149 | assert(!err && mem_reqs_size == num_allocations * sizeof(*mem_reqs)); |
| 1150 | err = xglGetObjectInfo(demo->uniform_data.buf, |
| 1151 | XGL_INFO_TYPE_BUFFER_MEMORY_REQUIREMENTS, |
| 1152 | &buf_reqs_size, &buf_reqs); |
| 1153 | assert(!err && buf_reqs_size == sizeof(XGL_BUFFER_MEMORY_REQUIREMENTS)); |
| 1154 | buf_alloc.usage = buf_reqs.usage; |
Mark Lobodzinski | c4fffc7 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 1155 | for (uint32_t i = 0; i < num_allocations; i ++) { |
Jon Ashburn | ae7c21c | 2015-01-19 15:00:26 -0700 | [diff] [blame] | 1156 | alloc_info.allocationSize = mem_reqs[i].size; |
Chia-I Wu | 8ea21c7 | 2015-01-01 07:55:04 +0800 | [diff] [blame] | 1157 | |
Jon Ashburn | ae7c21c | 2015-01-19 15:00:26 -0700 | [diff] [blame] | 1158 | err = xglAllocMemory(demo->device, &alloc_info, &(demo->uniform_data.mem[i])); |
| 1159 | assert(!err); |
Courtney Goeltzenleuchter | 4984d15 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 1160 | |
Mark Lobodzinski | c4fffc7 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 1161 | err = xglMapMemory(demo->uniform_data.mem[i], 0, (void **) &pData); |
Jon Ashburn | ae7c21c | 2015-01-19 15:00:26 -0700 | [diff] [blame] | 1162 | assert(!err); |
Courtney Goeltzenleuchter | 4984d15 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 1163 | |
Piers Daniell | 735ee53 | 2015-02-23 16:23:13 -0700 | [diff] [blame] | 1164 | memcpy(pData, &data, (size_t)alloc_info.allocationSize); |
Courtney Goeltzenleuchter | 4984d15 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 1165 | |
Jon Ashburn | ae7c21c | 2015-01-19 15:00:26 -0700 | [diff] [blame] | 1166 | err = xglUnmapMemory(demo->uniform_data.mem[i]); |
| 1167 | assert(!err); |
Courtney Goeltzenleuchter | 4984d15 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 1168 | |
Jon Ashburn | ae7c21c | 2015-01-19 15:00:26 -0700 | [diff] [blame] | 1169 | err = xglBindObjectMemory(demo->uniform_data.buf, i, |
| 1170 | demo->uniform_data.mem[i], 0); |
| 1171 | assert(!err); |
| 1172 | } |
Courtney Goeltzenleuchter | 2c2fbbf | 2015-04-07 17:13:38 -0600 | [diff] [blame] | 1173 | 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] | 1174 | |
| 1175 | memset(&view_info, 0, sizeof(view_info)); |
| 1176 | view_info.sType = XGL_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO; |
| 1177 | view_info.buffer = demo->uniform_data.buf; |
Chia-I Wu | 378caea | 2015-01-16 22:31:25 +0800 | [diff] [blame] | 1178 | view_info.viewType = XGL_BUFFER_VIEW_RAW; |
Chia-I Wu | 8ea21c7 | 2015-01-01 07:55:04 +0800 | [diff] [blame] | 1179 | view_info.offset = 0; |
| 1180 | view_info.range = sizeof(data); |
| 1181 | |
| 1182 | err = xglCreateBufferView(demo->device, &view_info, &demo->uniform_data.view); |
| 1183 | assert(!err); |
| 1184 | |
| 1185 | demo->uniform_data.attach.sType = XGL_STRUCTURE_TYPE_BUFFER_VIEW_ATTACH_INFO; |
| 1186 | demo->uniform_data.attach.view = demo->uniform_data.view; |
Courtney Goeltzenleuchter | 4984d15 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 1187 | } |
| 1188 | |
Chia-I Wu | 6a3c897 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 1189 | static void demo_prepare_descriptor_layout(struct demo *demo) |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1190 | { |
Chia-I Wu | a2aa863 | 2015-03-26 15:04:41 +0800 | [diff] [blame] | 1191 | const XGL_DESCRIPTOR_SET_LAYOUT_BINDING layout_bindings[2] = { |
| 1192 | [0] = { |
| 1193 | .descriptorType = XGL_DESCRIPTOR_TYPE_UNIFORM_BUFFER, |
| 1194 | .count = 1, |
| 1195 | .stageFlags = XGL_SHADER_STAGE_FLAGS_VERTEX_BIT, |
Chia-I Wu | 91e8e21 | 2015-03-27 12:56:09 +0800 | [diff] [blame] | 1196 | .pImmutableSamplers = NULL, |
Chia-I Wu | a2aa863 | 2015-03-26 15:04:41 +0800 | [diff] [blame] | 1197 | }, |
| 1198 | [1] = { |
| 1199 | .descriptorType = XGL_DESCRIPTOR_TYPE_SAMPLER_TEXTURE, |
| 1200 | .count = DEMO_TEXTURE_COUNT, |
| 1201 | .stageFlags = XGL_SHADER_STAGE_FLAGS_FRAGMENT_BIT, |
Chia-I Wu | 91e8e21 | 2015-03-27 12:56:09 +0800 | [diff] [blame] | 1202 | .pImmutableSamplers = NULL, |
Chia-I Wu | a2aa863 | 2015-03-26 15:04:41 +0800 | [diff] [blame] | 1203 | }, |
| 1204 | }; |
| 1205 | const XGL_DESCRIPTOR_SET_LAYOUT_CREATE_INFO descriptor_layout = { |
Chia-I Wu | 6a3c897 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 1206 | .sType = XGL_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO, |
| 1207 | .pNext = NULL, |
Chia-I Wu | a2aa863 | 2015-03-26 15:04:41 +0800 | [diff] [blame] | 1208 | .count = 2, |
| 1209 | .pBinding = layout_bindings, |
Chia-I Wu | 6a3c897 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 1210 | }; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1211 | XGL_RESULT err; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1212 | |
Chia-I Wu | 6a3c897 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 1213 | err = xglCreateDescriptorSetLayout(demo->device, |
Chia-I Wu | b58c24a | 2015-03-26 15:27:55 +0800 | [diff] [blame] | 1214 | &descriptor_layout, &demo->desc_layout); |
| 1215 | assert(!err); |
| 1216 | |
| 1217 | err = xglCreateDescriptorSetLayoutChain(demo->device, |
| 1218 | 1, &demo->desc_layout, &demo->desc_layout_chain); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1219 | assert(!err); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1220 | } |
| 1221 | |
| 1222 | static XGL_SHADER demo_prepare_shader(struct demo *demo, |
| 1223 | XGL_PIPELINE_SHADER_STAGE stage, |
| 1224 | const void *code, |
Mark Lobodzinski | c4fffc7 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 1225 | size_t size) |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1226 | { |
| 1227 | XGL_SHADER_CREATE_INFO createInfo; |
| 1228 | XGL_SHADER shader; |
| 1229 | XGL_RESULT err; |
| 1230 | |
Courtney Goeltzenleuchter | 97db1c1 | 2014-10-30 15:14:16 -0600 | [diff] [blame] | 1231 | |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1232 | createInfo.sType = XGL_STRUCTURE_TYPE_SHADER_CREATE_INFO; |
| 1233 | createInfo.pNext = NULL; |
| 1234 | |
Cody Northrop | 8b12a1f | 2015-03-17 15:55:58 -0600 | [diff] [blame] | 1235 | #ifdef EXTERNAL_SPV |
Courtney Goeltzenleuchter | 97db1c1 | 2014-10-30 15:14:16 -0600 | [diff] [blame] | 1236 | createInfo.codeSize = size; |
| 1237 | createInfo.pCode = code; |
Courtney Goeltzenleuchter | 6836939 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 1238 | createInfo.flags = 0; |
| 1239 | |
Courtney Goeltzenleuchter | 6836939 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 1240 | err = xglCreateShader(demo->device, &createInfo, &shader); |
| 1241 | if (err) { |
| 1242 | free((void *) createInfo.pCode); |
| 1243 | } |
| 1244 | #else |
Cody Northrop | 8b12a1f | 2015-03-17 15:55:58 -0600 | [diff] [blame] | 1245 | // Create fake SPV structure to feed GLSL |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1246 | // to the driver "under the covers" |
| 1247 | createInfo.codeSize = 3 * sizeof(uint32_t) + size + 1; |
| 1248 | createInfo.pCode = malloc(createInfo.codeSize); |
| 1249 | createInfo.flags = 0; |
| 1250 | |
| 1251 | /* try version 0 first: XGL_PIPELINE_SHADER_STAGE followed by GLSL */ |
Cody Northrop | 8b12a1f | 2015-03-17 15:55:58 -0600 | [diff] [blame] | 1252 | ((uint32_t *) createInfo.pCode)[0] = ICD_SPV_MAGIC; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1253 | ((uint32_t *) createInfo.pCode)[1] = 0; |
| 1254 | ((uint32_t *) createInfo.pCode)[2] = stage; |
| 1255 | memcpy(((uint32_t *) createInfo.pCode + 3), code, size + 1); |
| 1256 | |
| 1257 | err = xglCreateShader(demo->device, &createInfo, &shader); |
| 1258 | if (err) { |
| 1259 | free((void *) createInfo.pCode); |
Courtney Goeltzenleuchter | fc3b953 | 2014-10-28 10:32:57 -0600 | [diff] [blame] | 1260 | return NULL; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1261 | } |
Courtney Goeltzenleuchter | 6836939 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 1262 | #endif |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1263 | |
| 1264 | return shader; |
| 1265 | } |
| 1266 | |
Cody Northrop | 8b12a1f | 2015-03-17 15:55:58 -0600 | [diff] [blame] | 1267 | char *demo_read_spv(const char *filename, size_t *psize) |
Courtney Goeltzenleuchter | 97db1c1 | 2014-10-30 15:14:16 -0600 | [diff] [blame] | 1268 | { |
| 1269 | long int size; |
| 1270 | void *shader_code; |
| 1271 | |
| 1272 | FILE *fp = fopen(filename, "rb"); |
| 1273 | if (!fp) return NULL; |
| 1274 | |
| 1275 | fseek(fp, 0L, SEEK_END); |
| 1276 | size = ftell(fp); |
| 1277 | |
| 1278 | fseek(fp, 0L, SEEK_SET); |
| 1279 | |
| 1280 | shader_code = malloc(size); |
| 1281 | fread(shader_code, size, 1, fp); |
| 1282 | |
| 1283 | *psize = size; |
| 1284 | |
| 1285 | return shader_code; |
| 1286 | } |
| 1287 | |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1288 | static XGL_SHADER demo_prepare_vs(struct demo *demo) |
| 1289 | { |
Cody Northrop | 8b12a1f | 2015-03-17 15:55:58 -0600 | [diff] [blame] | 1290 | #ifdef EXTERNAL_SPV |
Courtney Goeltzenleuchter | 97db1c1 | 2014-10-30 15:14:16 -0600 | [diff] [blame] | 1291 | void *vertShaderCode; |
Mark Lobodzinski | c4fffc7 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 1292 | size_t size; |
Courtney Goeltzenleuchter | 97db1c1 | 2014-10-30 15:14:16 -0600 | [diff] [blame] | 1293 | |
Cody Northrop | 8b12a1f | 2015-03-17 15:55:58 -0600 | [diff] [blame] | 1294 | vertShaderCode = demo_read_spv("cube-vert.spv", &size); |
Courtney Goeltzenleuchter | 97db1c1 | 2014-10-30 15:14:16 -0600 | [diff] [blame] | 1295 | |
| 1296 | return demo_prepare_shader(demo, XGL_SHADER_STAGE_VERTEX, |
| 1297 | vertShaderCode, size); |
| 1298 | #else |
Courtney Goeltzenleuchter | 8879d3a | 2014-10-29 08:29:35 -0600 | [diff] [blame] | 1299 | static const char *vertShaderText = |
Courtney Goeltzenleuchter | 4984d15 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 1300 | "#version 140\n" |
| 1301 | "#extension GL_ARB_separate_shader_objects : enable\n" |
| 1302 | "#extension GL_ARB_shading_language_420pack : enable\n" |
| 1303 | "\n" |
| 1304 | "layout(binding = 0) uniform buf {\n" |
| 1305 | " mat4 MVP;\n" |
| 1306 | " vec4 position[12*3];\n" |
| 1307 | " vec4 attr[12*3];\n" |
| 1308 | "} ubuf;\n" |
| 1309 | "\n" |
| 1310 | "layout (location = 0) out vec4 texcoord;\n" |
Courtney Goeltzenleuchter | 4984d15 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 1311 | "\n" |
| 1312 | "void main() \n" |
| 1313 | "{\n" |
| 1314 | " texcoord = ubuf.attr[gl_VertexID];\n" |
Courtney Goeltzenleuchter | 4984d15 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 1315 | " gl_Position = ubuf.MVP * ubuf.position[gl_VertexID];\n" |
| 1316 | "}\n"; |
Courtney Goeltzenleuchter | fc3b953 | 2014-10-28 10:32:57 -0600 | [diff] [blame] | 1317 | |
Courtney Goeltzenleuchter | 8879d3a | 2014-10-29 08:29:35 -0600 | [diff] [blame] | 1318 | return demo_prepare_shader(demo, XGL_SHADER_STAGE_VERTEX, |
| 1319 | (const void *) vertShaderText, |
| 1320 | strlen(vertShaderText)); |
Courtney Goeltzenleuchter | 97db1c1 | 2014-10-30 15:14:16 -0600 | [diff] [blame] | 1321 | #endif |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1322 | } |
| 1323 | |
| 1324 | static XGL_SHADER demo_prepare_fs(struct demo *demo) |
| 1325 | { |
Cody Northrop | 8b12a1f | 2015-03-17 15:55:58 -0600 | [diff] [blame] | 1326 | #ifdef EXTERNAL_SPV |
Courtney Goeltzenleuchter | 97db1c1 | 2014-10-30 15:14:16 -0600 | [diff] [blame] | 1327 | void *fragShaderCode; |
Mark Lobodzinski | c4fffc7 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 1328 | size_t size; |
Courtney Goeltzenleuchter | 97db1c1 | 2014-10-30 15:14:16 -0600 | [diff] [blame] | 1329 | |
Cody Northrop | 8b12a1f | 2015-03-17 15:55:58 -0600 | [diff] [blame] | 1330 | fragShaderCode = demo_read_spv("cube-frag.spv", &size); |
Courtney Goeltzenleuchter | 97db1c1 | 2014-10-30 15:14:16 -0600 | [diff] [blame] | 1331 | |
| 1332 | return demo_prepare_shader(demo, XGL_SHADER_STAGE_FRAGMENT, |
| 1333 | fragShaderCode, size); |
| 1334 | #else |
Courtney Goeltzenleuchter | 8879d3a | 2014-10-29 08:29:35 -0600 | [diff] [blame] | 1335 | static const char *fragShaderText = |
Courtney Goeltzenleuchter | 4984d15 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 1336 | "#version 140\n" |
| 1337 | "#extension GL_ARB_separate_shader_objects : enable\n" |
| 1338 | "#extension GL_ARB_shading_language_420pack : enable\n" |
Courtney Goeltzenleuchter | 4d1acf1 | 2015-02-25 15:13:35 -0700 | [diff] [blame] | 1339 | "layout (binding = 1) uniform sampler2D tex;\n" |
Courtney Goeltzenleuchter | 4984d15 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 1340 | "\n" |
| 1341 | "layout (location = 0) in vec4 texcoord;\n" |
| 1342 | "void main() {\n" |
| 1343 | " gl_FragColor = texture(tex, texcoord.xy);\n" |
| 1344 | "}\n"; |
Courtney Goeltzenleuchter | fc3b953 | 2014-10-28 10:32:57 -0600 | [diff] [blame] | 1345 | |
Courtney Goeltzenleuchter | 8879d3a | 2014-10-29 08:29:35 -0600 | [diff] [blame] | 1346 | return demo_prepare_shader(demo, XGL_SHADER_STAGE_FRAGMENT, |
| 1347 | (const void *) fragShaderText, |
| 1348 | strlen(fragShaderText)); |
Courtney Goeltzenleuchter | 97db1c1 | 2014-10-30 15:14:16 -0600 | [diff] [blame] | 1349 | #endif |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1350 | } |
| 1351 | |
| 1352 | static void demo_prepare_pipeline(struct demo *demo) |
| 1353 | { |
| 1354 | XGL_GRAPHICS_PIPELINE_CREATE_INFO pipeline; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1355 | XGL_PIPELINE_IA_STATE_CREATE_INFO ia; |
| 1356 | XGL_PIPELINE_RS_STATE_CREATE_INFO rs; |
Tony Barbour | 29645d0 | 2015-01-16 14:27:35 -0700 | [diff] [blame] | 1357 | XGL_PIPELINE_CB_STATE_CREATE_INFO cb; |
| 1358 | XGL_PIPELINE_DS_STATE_CREATE_INFO ds; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1359 | XGL_PIPELINE_SHADER_STAGE_CREATE_INFO vs; |
| 1360 | XGL_PIPELINE_SHADER_STAGE_CREATE_INFO fs; |
Tony Barbour | 29645d0 | 2015-01-16 14:27:35 -0700 | [diff] [blame] | 1361 | XGL_PIPELINE_VP_STATE_CREATE_INFO vp; |
| 1362 | XGL_PIPELINE_MS_STATE_CREATE_INFO ms; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1363 | XGL_RESULT err; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1364 | |
| 1365 | memset(&pipeline, 0, sizeof(pipeline)); |
| 1366 | pipeline.sType = XGL_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO; |
Chia-I Wu | b58c24a | 2015-03-26 15:27:55 +0800 | [diff] [blame] | 1367 | pipeline.pSetLayoutChain = demo->desc_layout_chain; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1368 | |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1369 | memset(&ia, 0, sizeof(ia)); |
| 1370 | ia.sType = XGL_STRUCTURE_TYPE_PIPELINE_IA_STATE_CREATE_INFO; |
| 1371 | ia.topology = XGL_TOPOLOGY_TRIANGLE_LIST; |
| 1372 | |
| 1373 | memset(&rs, 0, sizeof(rs)); |
| 1374 | rs.sType = XGL_STRUCTURE_TYPE_PIPELINE_RS_STATE_CREATE_INFO; |
Tony Barbour | 29645d0 | 2015-01-16 14:27:35 -0700 | [diff] [blame] | 1375 | rs.fillMode = XGL_FILL_SOLID; |
Mike Stroyan | 16b3d98 | 2015-03-19 14:29:04 -0600 | [diff] [blame] | 1376 | rs.cullMode = XGL_CULL_BACK; |
Tony Barbour | 29645d0 | 2015-01-16 14:27:35 -0700 | [diff] [blame] | 1377 | rs.frontFace = XGL_FRONT_FACE_CCW; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1378 | |
| 1379 | memset(&cb, 0, sizeof(cb)); |
| 1380 | cb.sType = XGL_STRUCTURE_TYPE_PIPELINE_CB_STATE_CREATE_INFO; |
Tony Barbour | 29645d0 | 2015-01-16 14:27:35 -0700 | [diff] [blame] | 1381 | XGL_PIPELINE_CB_ATTACHMENT_STATE att_state[1]; |
| 1382 | memset(att_state, 0, sizeof(att_state)); |
| 1383 | att_state[0].format = demo->format; |
| 1384 | att_state[0].channelWriteMask = 0xf; |
| 1385 | att_state[0].blendEnable = XGL_FALSE; |
| 1386 | cb.attachmentCount = 1; |
| 1387 | cb.pAttachments = att_state; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1388 | |
Tony Barbour | 29645d0 | 2015-01-16 14:27:35 -0700 | [diff] [blame] | 1389 | memset(&vp, 0, sizeof(vp)); |
| 1390 | vp.sType = XGL_STRUCTURE_TYPE_PIPELINE_VP_STATE_CREATE_INFO; |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 1391 | vp.numViewports = 1; |
Mike Stroyan | 259bc54 | 2015-03-24 15:13:34 -0600 | [diff] [blame] | 1392 | vp.clipOrigin = XGL_COORDINATE_ORIGIN_LOWER_LEFT; |
Tony Barbour | 29645d0 | 2015-01-16 14:27:35 -0700 | [diff] [blame] | 1393 | |
| 1394 | memset(&ds, 0, sizeof(ds)); |
| 1395 | ds.sType = XGL_STRUCTURE_TYPE_PIPELINE_DS_STATE_CREATE_INFO; |
| 1396 | ds.format = demo->depth.format; |
| 1397 | ds.depthTestEnable = XGL_TRUE; |
| 1398 | ds.depthWriteEnable = XGL_TRUE; |
| 1399 | ds.depthFunc = XGL_COMPARE_LESS_EQUAL; |
| 1400 | ds.depthBoundsEnable = XGL_FALSE; |
| 1401 | ds.back.stencilFailOp = XGL_STENCIL_OP_KEEP; |
| 1402 | ds.back.stencilPassOp = XGL_STENCIL_OP_KEEP; |
| 1403 | ds.back.stencilFunc = XGL_COMPARE_ALWAYS; |
| 1404 | ds.stencilTestEnable = XGL_FALSE; |
| 1405 | ds.front = ds.back; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1406 | |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1407 | memset(&vs, 0, sizeof(vs)); |
| 1408 | vs.sType = XGL_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO; |
| 1409 | vs.shader.stage = XGL_SHADER_STAGE_VERTEX; |
Courtney Goeltzenleuchter | 4984d15 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 1410 | vs.shader.shader = demo_prepare_vs(demo); |
Courtney Goeltzenleuchter | fc3b953 | 2014-10-28 10:32:57 -0600 | [diff] [blame] | 1411 | assert(vs.shader.shader != NULL); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1412 | |
| 1413 | memset(&fs, 0, sizeof(fs)); |
| 1414 | fs.sType = XGL_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO; |
| 1415 | fs.shader.stage = XGL_SHADER_STAGE_FRAGMENT; |
| 1416 | fs.shader.shader = demo_prepare_fs(demo); |
Courtney Goeltzenleuchter | fc3b953 | 2014-10-28 10:32:57 -0600 | [diff] [blame] | 1417 | assert(fs.shader.shader != NULL); |
Tony Barbour | 29645d0 | 2015-01-16 14:27:35 -0700 | [diff] [blame] | 1418 | |
| 1419 | memset(&ms, 0, sizeof(ms)); |
| 1420 | ms.sType = XGL_STRUCTURE_TYPE_PIPELINE_MS_STATE_CREATE_INFO; |
| 1421 | ms.sampleMask = 1; |
| 1422 | ms.multisampleEnable = XGL_FALSE; |
| 1423 | ms.samples = 1; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1424 | |
Mark Lobodzinski | c4fffc7 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 1425 | pipeline.pNext = (const void *) &ia; |
| 1426 | ia.pNext = (const void *) &rs; |
| 1427 | rs.pNext = (const void *) &cb; |
| 1428 | cb.pNext = (const void *) &ms; |
| 1429 | ms.pNext = (const void *) &vp; |
| 1430 | vp.pNext = (const void *) &ds; |
| 1431 | ds.pNext = (const void *) &vs; |
| 1432 | vs.pNext = (const void *) &fs; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1433 | |
| 1434 | err = xglCreateGraphicsPipeline(demo->device, &pipeline, &demo->pipeline); |
| 1435 | assert(!err); |
| 1436 | |
| 1437 | xglDestroyObject(vs.shader.shader); |
| 1438 | xglDestroyObject(fs.shader.shader); |
| 1439 | } |
| 1440 | |
| 1441 | static void demo_prepare_dynamic_states(struct demo *demo) |
| 1442 | { |
Tony Barbour | 29645d0 | 2015-01-16 14:27:35 -0700 | [diff] [blame] | 1443 | XGL_DYNAMIC_VP_STATE_CREATE_INFO viewport_create; |
| 1444 | XGL_DYNAMIC_RS_STATE_CREATE_INFO raster; |
| 1445 | XGL_DYNAMIC_CB_STATE_CREATE_INFO color_blend; |
| 1446 | XGL_DYNAMIC_DS_STATE_CREATE_INFO depth_stencil; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1447 | XGL_RESULT err; |
| 1448 | |
Tony Barbour | 29645d0 | 2015-01-16 14:27:35 -0700 | [diff] [blame] | 1449 | memset(&viewport_create, 0, sizeof(viewport_create)); |
| 1450 | viewport_create.sType = XGL_STRUCTURE_TYPE_DYNAMIC_VP_STATE_CREATE_INFO; |
Courtney Goeltzenleuchter | 1710d8d | 2015-02-11 14:13:34 -0700 | [diff] [blame] | 1451 | viewport_create.viewportAndScissorCount = 1; |
Tony Barbour | 29645d0 | 2015-01-16 14:27:35 -0700 | [diff] [blame] | 1452 | XGL_VIEWPORT viewport; |
Piers Daniell | 735ee53 | 2015-02-23 16:23:13 -0700 | [diff] [blame] | 1453 | memset(&viewport, 0, sizeof(viewport)); |
Mark Lobodzinski | c4fffc7 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 1454 | viewport.height = (float) demo->height; |
| 1455 | viewport.width = (float) demo->width; |
| 1456 | viewport.minDepth = (float) 0.0f; |
| 1457 | viewport.maxDepth = (float) 1.0f; |
Piers Daniell | 735ee53 | 2015-02-23 16:23:13 -0700 | [diff] [blame] | 1458 | viewport_create.pViewports = &viewport; |
| 1459 | XGL_RECT scissor; |
| 1460 | memset(&scissor, 0, sizeof(scissor)); |
Courtney Goeltzenleuchter | 1710d8d | 2015-02-11 14:13:34 -0700 | [diff] [blame] | 1461 | scissor.extent.width = demo->width; |
| 1462 | scissor.extent.height = demo->height; |
| 1463 | scissor.offset.x = 0; |
| 1464 | scissor.offset.y = 0; |
Courtney Goeltzenleuchter | 1710d8d | 2015-02-11 14:13:34 -0700 | [diff] [blame] | 1465 | viewport_create.pScissors = &scissor; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1466 | |
| 1467 | memset(&raster, 0, sizeof(raster)); |
Tony Barbour | 29645d0 | 2015-01-16 14:27:35 -0700 | [diff] [blame] | 1468 | raster.sType = XGL_STRUCTURE_TYPE_DYNAMIC_RS_STATE_CREATE_INFO; |
Piers Daniell | 735ee53 | 2015-02-23 16:23:13 -0700 | [diff] [blame] | 1469 | raster.pointSize = 1.0; |
| 1470 | raster.lineWidth = 1.0; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1471 | |
| 1472 | memset(&color_blend, 0, sizeof(color_blend)); |
Tony Barbour | 29645d0 | 2015-01-16 14:27:35 -0700 | [diff] [blame] | 1473 | color_blend.sType = XGL_STRUCTURE_TYPE_DYNAMIC_CB_STATE_CREATE_INFO; |
Piers Daniell | 735ee53 | 2015-02-23 16:23:13 -0700 | [diff] [blame] | 1474 | color_blend.blendConst[0] = 1.0f; |
| 1475 | color_blend.blendConst[1] = 1.0f; |
| 1476 | color_blend.blendConst[2] = 1.0f; |
| 1477 | color_blend.blendConst[3] = 1.0f; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1478 | |
| 1479 | memset(&depth_stencil, 0, sizeof(depth_stencil)); |
Tony Barbour | 29645d0 | 2015-01-16 14:27:35 -0700 | [diff] [blame] | 1480 | depth_stencil.sType = XGL_STRUCTURE_TYPE_DYNAMIC_DS_STATE_CREATE_INFO; |
Piers Daniell | 735ee53 | 2015-02-23 16:23:13 -0700 | [diff] [blame] | 1481 | depth_stencil.minDepth = 0.0f; |
| 1482 | depth_stencil.maxDepth = 1.0f; |
Tony Barbour | 29645d0 | 2015-01-16 14:27:35 -0700 | [diff] [blame] | 1483 | depth_stencil.stencilBackRef = 0; |
| 1484 | depth_stencil.stencilFrontRef = 0; |
| 1485 | depth_stencil.stencilReadMask = 0xff; |
| 1486 | depth_stencil.stencilWriteMask = 0xff; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1487 | |
Tony Barbour | 29645d0 | 2015-01-16 14:27:35 -0700 | [diff] [blame] | 1488 | err = xglCreateDynamicViewportState(demo->device, &viewport_create, &demo->viewport); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1489 | assert(!err); |
| 1490 | |
Tony Barbour | 29645d0 | 2015-01-16 14:27:35 -0700 | [diff] [blame] | 1491 | err = xglCreateDynamicRasterState(demo->device, &raster, &demo->raster); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1492 | assert(!err); |
| 1493 | |
Tony Barbour | 29645d0 | 2015-01-16 14:27:35 -0700 | [diff] [blame] | 1494 | err = xglCreateDynamicColorBlendState(demo->device, |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1495 | &color_blend, &demo->color_blend); |
| 1496 | assert(!err); |
| 1497 | |
Tony Barbour | 29645d0 | 2015-01-16 14:27:35 -0700 | [diff] [blame] | 1498 | err = xglCreateDynamicDepthStencilState(demo->device, |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1499 | &depth_stencil, &demo->depth_stencil); |
| 1500 | assert(!err); |
| 1501 | } |
| 1502 | |
Chia-I Wu | 63ea926 | 2015-03-26 13:14:16 +0800 | [diff] [blame] | 1503 | static void demo_prepare_descriptor_pool(struct demo *demo) |
Chia-I Wu | 6a3c897 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 1504 | { |
| 1505 | const XGL_DESCRIPTOR_TYPE_COUNT type_counts[2] = { |
| 1506 | [0] = { |
| 1507 | .type = XGL_DESCRIPTOR_TYPE_UNIFORM_BUFFER, |
| 1508 | .count = 1, |
| 1509 | }, |
| 1510 | [1] = { |
| 1511 | .type = XGL_DESCRIPTOR_TYPE_SAMPLER_TEXTURE, |
| 1512 | .count = DEMO_TEXTURE_COUNT, |
| 1513 | }, |
| 1514 | }; |
Chia-I Wu | 63ea926 | 2015-03-26 13:14:16 +0800 | [diff] [blame] | 1515 | const XGL_DESCRIPTOR_POOL_CREATE_INFO descriptor_pool = { |
| 1516 | .sType = XGL_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO, |
Chia-I Wu | 6a3c897 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 1517 | .pNext = NULL, |
| 1518 | .count = 2, |
| 1519 | .pTypeCount = type_counts, |
| 1520 | }; |
| 1521 | XGL_RESULT err; |
| 1522 | |
Chia-I Wu | 63ea926 | 2015-03-26 13:14:16 +0800 | [diff] [blame] | 1523 | err = xglCreateDescriptorPool(demo->device, |
| 1524 | XGL_DESCRIPTOR_POOL_USAGE_ONE_SHOT, 1, |
| 1525 | &descriptor_pool, &demo->desc_pool); |
Chia-I Wu | 6a3c897 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 1526 | assert(!err); |
| 1527 | } |
| 1528 | |
| 1529 | static void demo_prepare_descriptor_set(struct demo *demo) |
| 1530 | { |
Chia-I Wu | 6a3c897 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 1531 | XGL_IMAGE_VIEW_ATTACH_INFO view_info[DEMO_TEXTURE_COUNT]; |
| 1532 | XGL_SAMPLER_IMAGE_VIEW_INFO combined_info[DEMO_TEXTURE_COUNT]; |
| 1533 | XGL_UPDATE_SAMPLER_TEXTURES update_fs; |
| 1534 | XGL_UPDATE_BUFFERS update_vs; |
Chia-I Wu | b58c24a | 2015-03-26 15:27:55 +0800 | [diff] [blame] | 1535 | const void *update_array[2] = { &update_vs, &update_fs }; |
Chia-I Wu | 6a3c897 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 1536 | XGL_RESULT err; |
| 1537 | uint32_t count; |
Mark Lobodzinski | c4fffc7 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 1538 | uint32_t i; |
Chia-I Wu | 6a3c897 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 1539 | |
| 1540 | for (i = 0; i < DEMO_TEXTURE_COUNT; i++) { |
| 1541 | view_info[i].sType = XGL_STRUCTURE_TYPE_IMAGE_VIEW_ATTACH_INFO; |
| 1542 | view_info[i].pNext = NULL; |
| 1543 | view_info[i].view = demo->textures[i].view, |
| 1544 | view_info[i].layout = XGL_IMAGE_LAYOUT_GENERAL; |
| 1545 | |
Chia-I Wu | b58c24a | 2015-03-26 15:27:55 +0800 | [diff] [blame] | 1546 | combined_info[i].sampler = demo->textures[i].sampler; |
Chia-I Wu | 6a3c897 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 1547 | combined_info[i].pImageView = &view_info[i]; |
| 1548 | } |
| 1549 | |
| 1550 | memset(&update_vs, 0, sizeof(update_vs)); |
| 1551 | update_vs.sType = XGL_STRUCTURE_TYPE_UPDATE_BUFFERS; |
| 1552 | update_vs.pNext = &update_fs; |
| 1553 | update_vs.descriptorType = XGL_DESCRIPTOR_TYPE_UNIFORM_BUFFER; |
| 1554 | update_vs.count = 1; |
Chia-I Wu | b58c24a | 2015-03-26 15:27:55 +0800 | [diff] [blame] | 1555 | update_vs.pBufferViews = &demo->uniform_data.attach; |
Chia-I Wu | 6a3c897 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 1556 | |
| 1557 | memset(&update_fs, 0, sizeof(update_fs)); |
| 1558 | update_fs.sType = XGL_STRUCTURE_TYPE_UPDATE_SAMPLER_TEXTURES; |
Chia-I Wu | b58c24a | 2015-03-26 15:27:55 +0800 | [diff] [blame] | 1559 | update_fs.binding = 1; |
Chia-I Wu | 6a3c897 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 1560 | update_fs.count = DEMO_TEXTURE_COUNT; |
| 1561 | update_fs.pSamplerImageViews = combined_info; |
| 1562 | |
Chia-I Wu | 63ea926 | 2015-03-26 13:14:16 +0800 | [diff] [blame] | 1563 | err = xglAllocDescriptorSets(demo->desc_pool, |
Chia-I Wu | 6a3c897 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 1564 | XGL_DESCRIPTOR_SET_USAGE_STATIC, |
Chia-I Wu | 87544e7 | 2015-02-23 10:41:08 -0700 | [diff] [blame] | 1565 | 1, &demo->desc_layout, |
Chia-I Wu | 6a3c897 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 1566 | &demo->desc_set, &count); |
| 1567 | assert(!err && count == 1); |
| 1568 | |
Chia-I Wu | 63ea926 | 2015-03-26 13:14:16 +0800 | [diff] [blame] | 1569 | xglBeginDescriptorPoolUpdate(demo->device, |
Chia-I Wu | 6a3c897 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 1570 | XGL_DESCRIPTOR_UPDATE_MODE_FASTEST); |
| 1571 | |
Chia-I Wu | 63ea926 | 2015-03-26 13:14:16 +0800 | [diff] [blame] | 1572 | xglClearDescriptorSets(demo->desc_pool, 1, &demo->desc_set); |
Chia-I Wu | b58c24a | 2015-03-26 15:27:55 +0800 | [diff] [blame] | 1573 | xglUpdateDescriptors(demo->desc_set, 2, update_array); |
Chia-I Wu | 6a3c897 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 1574 | |
Chia-I Wu | 63ea926 | 2015-03-26 13:14:16 +0800 | [diff] [blame] | 1575 | xglEndDescriptorPoolUpdate(demo->device, demo->buffers[demo->current_buffer].cmd); |
Chia-I Wu | 6a3c897 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 1576 | } |
| 1577 | |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1578 | static void demo_prepare(struct demo *demo) |
| 1579 | { |
| 1580 | const XGL_CMD_BUFFER_CREATE_INFO cmd = { |
| 1581 | .sType = XGL_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO, |
| 1582 | .pNext = NULL, |
Courtney Goeltzenleuchter | ed667a5 | 2015-03-05 18:09:39 -0700 | [diff] [blame] | 1583 | .queueNodeIndex = demo->graphics_queue_node_index, |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1584 | .flags = 0, |
| 1585 | }; |
| 1586 | XGL_RESULT err; |
| 1587 | |
| 1588 | demo_prepare_buffers(demo); |
| 1589 | demo_prepare_depth(demo); |
| 1590 | demo_prepare_textures(demo); |
Courtney Goeltzenleuchter | 8879d3a | 2014-10-29 08:29:35 -0600 | [diff] [blame] | 1591 | demo_prepare_cube_data_buffer(demo); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1592 | |
Chia-I Wu | 6a3c897 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 1593 | demo_prepare_descriptor_layout(demo); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1594 | demo_prepare_pipeline(demo); |
| 1595 | demo_prepare_dynamic_states(demo); |
| 1596 | |
Courtney Goeltzenleuchter | efc58ae | 2015-02-17 09:48:44 -0700 | [diff] [blame] | 1597 | for (int i = 0; i < DEMO_BUFFER_COUNT; i++) { |
| 1598 | err = xglCreateCommandBuffer(demo->device, &cmd, &demo->buffers[i].cmd); |
| 1599 | assert(!err); |
| 1600 | } |
Chia-I Wu | 6a3c897 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 1601 | |
Chia-I Wu | 63ea926 | 2015-03-26 13:14:16 +0800 | [diff] [blame] | 1602 | demo_prepare_descriptor_pool(demo); |
Chia-I Wu | 6a3c897 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 1603 | demo_prepare_descriptor_set(demo); |
Courtney Goeltzenleuchter | efc58ae | 2015-02-17 09:48:44 -0700 | [diff] [blame] | 1604 | |
| 1605 | |
| 1606 | for (int i = 0; i < DEMO_BUFFER_COUNT; i++) { |
| 1607 | demo->current_buffer = i; |
| 1608 | demo_draw_build_cmd(demo, demo->buffers[i].cmd); |
| 1609 | } |
| 1610 | |
Courtney Goeltzenleuchter | 752eb8e | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 1611 | /* |
| 1612 | * Prepare functions above may generate pipeline commands |
| 1613 | * that need to be flushed before beginning the render loop. |
| 1614 | */ |
| 1615 | demo_flush_init_cmd(demo); |
| 1616 | |
Courtney Goeltzenleuchter | efc58ae | 2015-02-17 09:48:44 -0700 | [diff] [blame] | 1617 | demo->current_buffer = 0; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1618 | } |
| 1619 | |
| 1620 | static void demo_handle_event(struct demo *demo, |
| 1621 | const xcb_generic_event_t *event) |
| 1622 | { |
Piers Daniell | 735ee53 | 2015-02-23 16:23:13 -0700 | [diff] [blame] | 1623 | uint8_t event_code = event->response_type & 0x7f; |
Courtney Goeltzenleuchter | 98cb2cb | 2014-11-06 14:27:52 -0700 | [diff] [blame] | 1624 | switch (event_code) { |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1625 | case XCB_EXPOSE: |
Courtney Goeltzenleuchter | c465d6c | 2014-11-18 11:28:09 -0700 | [diff] [blame] | 1626 | // TODO: Resize window |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1627 | break; |
Courtney Goeltzenleuchter | 98cb2cb | 2014-11-06 14:27:52 -0700 | [diff] [blame] | 1628 | case XCB_CLIENT_MESSAGE: |
| 1629 | if((*(xcb_client_message_event_t*)event).data.data32[0] == |
| 1630 | (*demo->atom_wm_delete_window).atom) { |
| 1631 | demo->quit = true; |
| 1632 | } |
| 1633 | break; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1634 | case XCB_KEY_RELEASE: |
| 1635 | { |
| 1636 | const xcb_key_release_event_t *key = |
| 1637 | (const xcb_key_release_event_t *) event; |
| 1638 | |
Courtney Goeltzenleuchter | fc3b953 | 2014-10-28 10:32:57 -0600 | [diff] [blame] | 1639 | switch (key->detail) { |
| 1640 | case 0x9: // Escape |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1641 | demo->quit = true; |
Courtney Goeltzenleuchter | fc3b953 | 2014-10-28 10:32:57 -0600 | [diff] [blame] | 1642 | break; |
Courtney Goeltzenleuchter | 4984d15 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 1643 | case 0x71: // left arrow key |
Courtney Goeltzenleuchter | 8879d3a | 2014-10-29 08:29:35 -0600 | [diff] [blame] | 1644 | demo->spin_angle += demo->spin_increment; |
Courtney Goeltzenleuchter | 4984d15 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 1645 | break; |
Courtney Goeltzenleuchter | fc3b953 | 2014-10-28 10:32:57 -0600 | [diff] [blame] | 1646 | case 0x72: // right arrow key |
Courtney Goeltzenleuchter | 8879d3a | 2014-10-29 08:29:35 -0600 | [diff] [blame] | 1647 | demo->spin_angle -= demo->spin_increment; |
Courtney Goeltzenleuchter | fc3b953 | 2014-10-28 10:32:57 -0600 | [diff] [blame] | 1648 | break; |
Courtney Goeltzenleuchter | 8879d3a | 2014-10-29 08:29:35 -0600 | [diff] [blame] | 1649 | case 0x41: |
| 1650 | demo->pause = !demo->pause; |
Piers Daniell | 735ee53 | 2015-02-23 16:23:13 -0700 | [diff] [blame] | 1651 | break; |
Courtney Goeltzenleuchter | fc3b953 | 2014-10-28 10:32:57 -0600 | [diff] [blame] | 1652 | } |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1653 | } |
| 1654 | break; |
| 1655 | default: |
| 1656 | break; |
| 1657 | } |
| 1658 | } |
| 1659 | |
| 1660 | static void demo_run(struct demo *demo) |
| 1661 | { |
| 1662 | xcb_flush(demo->connection); |
| 1663 | |
| 1664 | while (!demo->quit) { |
| 1665 | xcb_generic_event_t *event; |
| 1666 | |
Courtney Goeltzenleuchter | c465d6c | 2014-11-18 11:28:09 -0700 | [diff] [blame] | 1667 | if (demo->pause) { |
| 1668 | event = xcb_wait_for_event(demo->connection); |
| 1669 | } else { |
| 1670 | event = xcb_poll_for_event(demo->connection); |
| 1671 | } |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1672 | if (event) { |
| 1673 | demo_handle_event(demo, event); |
| 1674 | free(event); |
Courtney Goeltzenleuchter | 9808555 | 2014-11-10 11:13:13 -0700 | [diff] [blame] | 1675 | } |
Courtney Goeltzenleuchter | c465d6c | 2014-11-18 11:28:09 -0700 | [diff] [blame] | 1676 | |
| 1677 | // Wait for work to finish before updating MVP. |
| 1678 | xglDeviceWaitIdle(demo->device); |
| 1679 | demo_update_data_buffer(demo); |
| 1680 | |
Courtney Goeltzenleuchter | 9808555 | 2014-11-10 11:13:13 -0700 | [diff] [blame] | 1681 | demo_draw(demo); |
Courtney Goeltzenleuchter | 1454f3c | 2014-11-18 11:28:09 -0700 | [diff] [blame] | 1682 | |
Courtney Goeltzenleuchter | 9808555 | 2014-11-10 11:13:13 -0700 | [diff] [blame] | 1683 | // Wait for work to finish before updating MVP. |
| 1684 | xglDeviceWaitIdle(demo->device); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1685 | } |
| 1686 | } |
| 1687 | |
| 1688 | static void demo_create_window(struct demo *demo) |
| 1689 | { |
| 1690 | uint32_t value_mask, value_list[32]; |
| 1691 | |
| 1692 | demo->window = xcb_generate_id(demo->connection); |
| 1693 | |
| 1694 | value_mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK; |
| 1695 | value_list[0] = demo->screen->black_pixel; |
| 1696 | value_list[1] = XCB_EVENT_MASK_KEY_RELEASE | |
| 1697 | XCB_EVENT_MASK_EXPOSURE; |
| 1698 | |
| 1699 | xcb_create_window(demo->connection, |
| 1700 | XCB_COPY_FROM_PARENT, |
| 1701 | demo->window, demo->screen->root, |
| 1702 | 0, 0, demo->width, demo->height, 0, |
| 1703 | XCB_WINDOW_CLASS_INPUT_OUTPUT, |
| 1704 | demo->screen->root_visual, |
| 1705 | value_mask, value_list); |
| 1706 | |
Courtney Goeltzenleuchter | 98cb2cb | 2014-11-06 14:27:52 -0700 | [diff] [blame] | 1707 | /* Magic code that will send notification when window is destroyed */ |
| 1708 | xcb_intern_atom_cookie_t cookie = xcb_intern_atom(demo->connection, 1, 12, |
| 1709 | "WM_PROTOCOLS"); |
| 1710 | xcb_intern_atom_reply_t* reply = xcb_intern_atom_reply(demo->connection, cookie, 0); |
| 1711 | |
| 1712 | xcb_intern_atom_cookie_t cookie2 = xcb_intern_atom(demo->connection, 0, 16, "WM_DELETE_WINDOW"); |
| 1713 | demo->atom_wm_delete_window = xcb_intern_atom_reply(demo->connection, cookie2, 0); |
| 1714 | |
| 1715 | xcb_change_property(demo->connection, XCB_PROP_MODE_REPLACE, |
| 1716 | demo->window, (*reply).atom, 4, 32, 1, |
| 1717 | &(*demo->atom_wm_delete_window).atom); |
| 1718 | free(reply); |
| 1719 | |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1720 | xcb_map_window(demo->connection, demo->window); |
| 1721 | } |
| 1722 | |
| 1723 | static void demo_init_xgl(struct demo *demo) |
| 1724 | { |
| 1725 | const XGL_APPLICATION_INFO app = { |
| 1726 | .sType = XGL_STRUCTURE_TYPE_APPLICATION_INFO, |
| 1727 | .pNext = NULL, |
Chia-I Wu | b2c8193 | 2014-12-27 15:16:07 +0800 | [diff] [blame] | 1728 | .pAppName = "cube", |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1729 | .appVersion = 0, |
Chia-I Wu | b2c8193 | 2014-12-27 15:16:07 +0800 | [diff] [blame] | 1730 | .pEngineName = "cube", |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1731 | .engineVersion = 0, |
Courtney Goeltzenleuchter | 97a89c4 | 2015-02-23 17:40:15 -0700 | [diff] [blame] | 1732 | .apiVersion = XGL_API_VERSION, |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1733 | }; |
| 1734 | const XGL_WSI_X11_CONNECTION_INFO connection = { |
| 1735 | .pConnection = demo->connection, |
| 1736 | .root = demo->screen->root, |
| 1737 | .provider = 0, |
| 1738 | }; |
| 1739 | const XGL_DEVICE_QUEUE_CREATE_INFO queue = { |
| 1740 | .queueNodeIndex = 0, |
| 1741 | .queueCount = 1, |
| 1742 | }; |
Mark Lobodzinski | c4fffc7 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 1743 | const char *ext_names[] = { |
Chia-I Wu | b2c8193 | 2014-12-27 15:16:07 +0800 | [diff] [blame] | 1744 | "XGL_WSI_X11", |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1745 | }; |
| 1746 | const XGL_DEVICE_CREATE_INFO device = { |
| 1747 | .sType = XGL_STRUCTURE_TYPE_DEVICE_CREATE_INFO, |
| 1748 | .pNext = NULL, |
| 1749 | .queueRecordCount = 1, |
| 1750 | .pRequestedQueues = &queue, |
| 1751 | .extensionCount = 1, |
| 1752 | .ppEnabledExtensionNames = ext_names, |
| 1753 | .maxValidationLevel = XGL_VALIDATION_LEVEL_END_RANGE, |
| 1754 | .flags = XGL_DEVICE_CREATE_VALIDATION_BIT, |
| 1755 | }; |
| 1756 | XGL_RESULT err; |
Mark Lobodzinski | c4fffc7 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 1757 | uint32_t gpu_count; |
| 1758 | uint32_t i; |
Courtney Goeltzenleuchter | ed667a5 | 2015-03-05 18:09:39 -0700 | [diff] [blame] | 1759 | size_t data_size; |
| 1760 | uint32_t queue_count; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1761 | |
Jon Ashburn | 93cfc43 | 2015-01-29 15:47:01 -0700 | [diff] [blame] | 1762 | err = xglCreateInstance(&app, NULL, &demo->inst); |
Ian Elliott | 3979e28 | 2015-04-03 15:24:55 -0600 | [diff] [blame] | 1763 | if (err == XGL_ERROR_INCOMPATIBLE_DRIVER) { |
| 1764 | printf("Cannot find a compatible Vulkan installable client driver " |
| 1765 | "(ICD).\nExiting ...\n"); |
| 1766 | fflush(stdout); |
| 1767 | exit(1); |
| 1768 | } else { |
| 1769 | assert(!err); |
| 1770 | } |
Jon Ashburn | 93cfc43 | 2015-01-29 15:47:01 -0700 | [diff] [blame] | 1771 | err = xglEnumerateGpus(demo->inst, 1, &gpu_count, &demo->gpu); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1772 | assert(!err && gpu_count == 1); |
| 1773 | |
| 1774 | for (i = 0; i < device.extensionCount; i++) { |
| 1775 | err = xglGetExtensionSupport(demo->gpu, ext_names[i]); |
| 1776 | assert(!err); |
| 1777 | } |
| 1778 | |
| 1779 | err = xglWsiX11AssociateConnection(demo->gpu, &connection); |
| 1780 | assert(!err); |
| 1781 | |
| 1782 | err = xglCreateDevice(demo->gpu, &device, &demo->device); |
| 1783 | assert(!err); |
| 1784 | |
Courtney Goeltzenleuchter | 752eb8e | 2015-03-25 13:36:41 -0600 | [diff] [blame] | 1785 | err = xglGetGpuInfo(demo->gpu, XGL_INFO_TYPE_PHYSICAL_GPU_PROPERTIES, |
| 1786 | &data_size, NULL); |
| 1787 | assert(!err); |
| 1788 | |
| 1789 | demo->gpu_props = (XGL_PHYSICAL_GPU_PROPERTIES *) malloc(data_size); |
| 1790 | err = xglGetGpuInfo(demo->gpu, XGL_INFO_TYPE_PHYSICAL_GPU_PROPERTIES, |
| 1791 | &data_size, demo->gpu_props); |
| 1792 | assert(!err); |
| 1793 | |
Courtney Goeltzenleuchter | ed667a5 | 2015-03-05 18:09:39 -0700 | [diff] [blame] | 1794 | err = xglGetGpuInfo(demo->gpu, XGL_INFO_TYPE_PHYSICAL_GPU_QUEUE_PROPERTIES, |
| 1795 | &data_size, NULL); |
| 1796 | assert(!err); |
| 1797 | |
| 1798 | demo->queue_props = (XGL_PHYSICAL_GPU_QUEUE_PROPERTIES *) malloc(data_size); |
| 1799 | err = xglGetGpuInfo(demo->gpu, XGL_INFO_TYPE_PHYSICAL_GPU_QUEUE_PROPERTIES, |
| 1800 | &data_size, demo->queue_props); |
| 1801 | assert(!err); |
Ian Elliott | 8e777be | 2015-04-03 09:54:13 -0600 | [diff] [blame] | 1802 | queue_count = (uint32_t)(data_size / sizeof(XGL_PHYSICAL_GPU_QUEUE_PROPERTIES)); |
Courtney Goeltzenleuchter | ed667a5 | 2015-03-05 18:09:39 -0700 | [diff] [blame] | 1803 | assert(queue_count >= 1); |
| 1804 | |
| 1805 | for (i = 0; i < queue_count; i++) { |
| 1806 | if (demo->queue_props[i].queueFlags & XGL_QUEUE_GRAPHICS_BIT) |
| 1807 | break; |
| 1808 | } |
| 1809 | assert(i < queue_count); |
| 1810 | demo->graphics_queue_node_index = i; |
| 1811 | |
| 1812 | err = xglGetDeviceQueue(demo->device, demo->graphics_queue_node_index, |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1813 | 0, &demo->queue); |
| 1814 | assert(!err); |
| 1815 | } |
| 1816 | |
| 1817 | static void demo_init_connection(struct demo *demo) |
| 1818 | { |
| 1819 | const xcb_setup_t *setup; |
| 1820 | xcb_screen_iterator_t iter; |
| 1821 | int scr; |
| 1822 | |
| 1823 | demo->connection = xcb_connect(NULL, &scr); |
Ian Elliott | 3979e28 | 2015-04-03 15:24:55 -0600 | [diff] [blame] | 1824 | if (demo->connection == NULL) { |
| 1825 | printf("Cannot find a compatible Vulkan installable client driver " |
| 1826 | "(ICD).\nExiting ...\n"); |
| 1827 | fflush(stdout); |
| 1828 | exit(1); |
| 1829 | } |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1830 | |
| 1831 | setup = xcb_get_setup(demo->connection); |
| 1832 | iter = xcb_setup_roots_iterator(setup); |
| 1833 | while (scr-- > 0) |
| 1834 | xcb_screen_next(&iter); |
| 1835 | |
| 1836 | demo->screen = iter.data; |
| 1837 | } |
| 1838 | |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 1839 | static void demo_init(struct demo *demo, int argc, char **argv) |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1840 | { |
Courtney Goeltzenleuchter | 8879d3a | 2014-10-29 08:29:35 -0600 | [diff] [blame] | 1841 | vec3 eye = {0.0f, 3.0f, 5.0f}; |
| 1842 | vec3 origin = {0, 0, 0}; |
| 1843 | vec3 up = {0.0f, -1.0f, 0.0}; |
| 1844 | |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1845 | memset(demo, 0, sizeof(*demo)); |
| 1846 | |
Piers Daniell | 735ee53 | 2015-02-23 16:23:13 -0700 | [diff] [blame] | 1847 | for (int i = 1; i < argc; i++) { |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 1848 | if (strncmp(argv[i], "--use_staging", strlen("--use_staging")) == 0) |
| 1849 | demo->use_staging_buffer = true; |
| 1850 | } |
| 1851 | |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1852 | demo_init_connection(demo); |
| 1853 | demo_init_xgl(demo); |
| 1854 | |
Courtney Goeltzenleuchter | 8879d3a | 2014-10-29 08:29:35 -0600 | [diff] [blame] | 1855 | demo->width = 500; |
| 1856 | demo->height = 500; |
Jeremy Hayes | 9958ea8 | 2015-01-23 08:51:43 -0700 | [diff] [blame] | 1857 | demo->format = XGL_FMT_B8G8R8A8_UNORM; |
Courtney Goeltzenleuchter | 8879d3a | 2014-10-29 08:29:35 -0600 | [diff] [blame] | 1858 | |
| 1859 | demo->spin_angle = 0.01f; |
| 1860 | demo->spin_increment = 0.01f; |
| 1861 | demo->pause = false; |
| 1862 | |
Piers Daniell | 735ee53 | 2015-02-23 16:23:13 -0700 | [diff] [blame] | 1863 | 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] | 1864 | mat4x4_look_at(demo->view_matrix, eye, origin, up); |
| 1865 | mat4x4_identity(demo->model_matrix); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1866 | } |
| 1867 | |
| 1868 | static void demo_cleanup(struct demo *demo) |
| 1869 | { |
Mark Lobodzinski | c4fffc7 | 2015-01-29 08:55:56 -0600 | [diff] [blame] | 1870 | uint32_t i, j; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1871 | |
Chia-I Wu | 6a3c897 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 1872 | xglDestroyObject(demo->desc_set); |
Chia-I Wu | 63ea926 | 2015-03-26 13:14:16 +0800 | [diff] [blame] | 1873 | xglDestroyObject(demo->desc_pool); |
Chia-I Wu | 6a3c897 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 1874 | |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1875 | xglDestroyObject(demo->viewport); |
| 1876 | xglDestroyObject(demo->raster); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1877 | xglDestroyObject(demo->color_blend); |
| 1878 | xglDestroyObject(demo->depth_stencil); |
| 1879 | |
| 1880 | xglDestroyObject(demo->pipeline); |
Chia-I Wu | b58c24a | 2015-03-26 15:27:55 +0800 | [diff] [blame] | 1881 | xglDestroyObject(demo->desc_layout_chain); |
Chia-I Wu | 87544e7 | 2015-02-23 10:41:08 -0700 | [diff] [blame] | 1882 | xglDestroyObject(demo->desc_layout); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1883 | |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1884 | for (i = 0; i < DEMO_TEXTURE_COUNT; i++) { |
| 1885 | xglDestroyObject(demo->textures[i].view); |
Mark Lobodzinski | 7b8fee6 | 2015-02-18 16:38:17 -0600 | [diff] [blame] | 1886 | xglBindObjectMemory(demo->textures[i].image, 0, XGL_NULL_HANDLE, 0); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1887 | xglDestroyObject(demo->textures[i].image); |
Courtney Goeltzenleuchter | 1359b7a | 2015-04-02 16:25:42 -0600 | [diff] [blame] | 1888 | 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] | 1889 | for (j = 0; j < demo->textures[i].num_mem; j++) |
| 1890 | xglFreeMemory(demo->textures[i].mem[j]); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1891 | xglDestroyObject(demo->textures[i].sampler); |
| 1892 | } |
| 1893 | |
| 1894 | xglDestroyObject(demo->depth.view); |
Mark Lobodzinski | 7b8fee6 | 2015-02-18 16:38:17 -0600 | [diff] [blame] | 1895 | xglBindObjectMemory(demo->depth.image, 0, XGL_NULL_HANDLE, 0); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1896 | xglDestroyObject(demo->depth.image); |
Courtney Goeltzenleuchter | 1359b7a | 2015-04-02 16:25:42 -0600 | [diff] [blame] | 1897 | demo_remove_mem_refs(demo, demo->depth.num_mem, demo->depth.mem); |
| 1898 | for (j = 0; j < demo->depth.num_mem; j++) { |
Jon Ashburn | b2a6665 | 2015-01-16 09:37:43 -0700 | [diff] [blame] | 1899 | xglFreeMemory(demo->depth.mem[j]); |
Courtney Goeltzenleuchter | 1359b7a | 2015-04-02 16:25:42 -0600 | [diff] [blame] | 1900 | } |
Mark Lobodzinski | 7b8fee6 | 2015-02-18 16:38:17 -0600 | [diff] [blame] | 1901 | |
| 1902 | xglDestroyObject(demo->uniform_data.view); |
| 1903 | xglBindObjectMemory(demo->uniform_data.buf, 0, XGL_NULL_HANDLE, 0); |
Jon Ashburn | ae7c21c | 2015-01-19 15:00:26 -0700 | [diff] [blame] | 1904 | xglDestroyObject(demo->uniform_data.buf); |
Courtney Goeltzenleuchter | 1359b7a | 2015-04-02 16:25:42 -0600 | [diff] [blame] | 1905 | 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] | 1906 | for (j = 0; j < demo->uniform_data.num_mem; j++) |
| 1907 | xglFreeMemory(demo->uniform_data.mem[j]); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1908 | |
| 1909 | for (i = 0; i < DEMO_BUFFER_COUNT; i++) { |
Chia-I Wu | bb57f64 | 2014-11-07 14:30:34 +0800 | [diff] [blame] | 1910 | xglDestroyObject(demo->buffers[i].fence); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1911 | xglDestroyObject(demo->buffers[i].view); |
| 1912 | xglDestroyObject(demo->buffers[i].image); |
Courtney Goeltzenleuchter | efc58ae | 2015-02-17 09:48:44 -0700 | [diff] [blame] | 1913 | xglDestroyObject(demo->buffers[i].cmd); |
Courtney Goeltzenleuchter | 1359b7a | 2015-04-02 16:25:42 -0600 | [diff] [blame] | 1914 | demo_remove_mem_refs(demo, 1, &demo->buffers[i].mem); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1915 | } |
| 1916 | |
| 1917 | xglDestroyDevice(demo->device); |
Jon Ashburn | 93cfc43 | 2015-01-29 15:47:01 -0700 | [diff] [blame] | 1918 | xglDestroyInstance(demo->inst); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1919 | |
| 1920 | xcb_destroy_window(demo->connection, demo->window); |
| 1921 | xcb_disconnect(demo->connection); |
| 1922 | } |
| 1923 | |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 1924 | int main(int argc, char **argv) |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1925 | { |
| 1926 | struct demo demo; |
| 1927 | |
Courtney Goeltzenleuchter | b4fc007 | 2015-02-17 12:54:31 -0700 | [diff] [blame] | 1928 | demo_init(&demo, argc, argv); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1929 | |
| 1930 | demo_prepare(&demo); |
| 1931 | demo_create_window(&demo); |
| 1932 | demo_run(&demo); |
| 1933 | |
| 1934 | demo_cleanup(&demo); |
| 1935 | |
| 1936 | return 0; |
| 1937 | } |