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 | |
| 13 | #include "icd-bil.h" |
| 14 | |
Courtney Goeltzenleuchter | fc3b953 | 2014-10-28 10:32:57 -0600 | [diff] [blame] | 15 | #include "linmath.h" |
Courtney Goeltzenleuchter | 8879d3a | 2014-10-29 08:29:35 -0600 | [diff] [blame] | 16 | #include <unistd.h> |
Courtney Goeltzenleuchter | 6836939 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 17 | #include <png.h> |
Courtney Goeltzenleuchter | fc3b953 | 2014-10-28 10:32:57 -0600 | [diff] [blame] | 18 | |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 19 | #define DEMO_BUFFER_COUNT 2 |
| 20 | #define DEMO_TEXTURE_COUNT 1 |
| 21 | |
Courtney Goeltzenleuchter | 97db1c1 | 2014-10-30 15:14:16 -0600 | [diff] [blame] | 22 | /* |
| 23 | * When not defined, code will use built-in GLSL compiler |
| 24 | * which may not be supported on all drivers |
| 25 | */ |
| 26 | #define EXTERNAL_BIL |
| 27 | |
Courtney Goeltzenleuchter | 6836939 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 28 | static char *tex_files[] = { |
Courtney Goeltzenleuchter | 97db1c1 | 2014-10-30 15:14:16 -0600 | [diff] [blame] | 29 | "lunarg-logo-256x256-solid.png" |
Courtney Goeltzenleuchter | 6836939 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 30 | }; |
| 31 | |
Courtney Goeltzenleuchter | 4984d15 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 32 | struct xglcube_vs_uniform { |
| 33 | // Must start with MVP |
| 34 | XGL_FLOAT mvp[4][4]; |
| 35 | XGL_FLOAT position[12*3][4]; |
| 36 | XGL_FLOAT color[12*3][4]; |
| 37 | }; |
| 38 | |
| 39 | struct xgltexcube_vs_uniform { |
| 40 | // Must start with MVP |
| 41 | XGL_FLOAT mvp[4][4]; |
| 42 | XGL_FLOAT position[12*3][4]; |
| 43 | XGL_FLOAT attr[12*3][4]; |
| 44 | }; |
Courtney Goeltzenleuchter | fc3b953 | 2014-10-28 10:32:57 -0600 | [diff] [blame] | 45 | |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 46 | //-------------------------------------------------------------------------------------- |
| 47 | // Mesh and VertexFormat Data |
| 48 | //-------------------------------------------------------------------------------------- |
| 49 | struct Vertex |
| 50 | { |
| 51 | XGL_FLOAT posX, posY, posZ, posW; // Position data |
| 52 | XGL_FLOAT r, g, b, a; // Color |
| 53 | }; |
| 54 | |
Courtney Goeltzenleuchter | 4984d15 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 55 | struct VertexPosTex |
| 56 | { |
| 57 | XGL_FLOAT posX, posY, posZ, posW; // Position data |
| 58 | XGL_FLOAT u, v, s, t; // Texcoord |
| 59 | }; |
| 60 | |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 61 | #define XYZ1(_x_, _y_, _z_) (_x_), (_y_), (_z_), 1.f |
Courtney Goeltzenleuchter | 4984d15 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 62 | #define UV(_u_, _v_) (_u_), (_v_), 0.f, 1.f |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 63 | |
Courtney Goeltzenleuchter | 4984d15 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 64 | static const XGL_FLOAT g_vertex_buffer_data[] = { |
Courtney Goeltzenleuchter | 6836939 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 65 | -1.0f,-1.0f,-1.0f, // Vertex 0 |
| 66 | -1.0f,-1.0f, 1.0f, |
| 67 | -1.0f, 1.0f, 1.0f, |
| 68 | |
| 69 | -1.0f, 1.0f, 1.0f, // Vertex 1 |
| 70 | -1.0f, 1.0f,-1.0f, |
| 71 | -1.0f,-1.0f,-1.0f, |
| 72 | |
| 73 | -1.0f,-1.0f,-1.0f, // Vertex 2 |
| 74 | 1.0f, 1.0f,-1.0f, |
| 75 | 1.0f,-1.0f,-1.0f, |
| 76 | |
| 77 | -1.0f,-1.0f,-1.0f, // Vertex 3 |
| 78 | 1.0f, 1.0f,-1.0f, |
| 79 | -1.0f, 1.0f,-1.0f, |
| 80 | |
| 81 | -1.0f,-1.0f,-1.0f, // Vertex 4 |
| 82 | 1.0f,-1.0f, 1.0f, |
| 83 | 1.0f,-1.0f,-1.0f, |
| 84 | |
| 85 | -1.0f,-1.0f,-1.0f, // Vertex 5 |
| 86 | -1.0f,-1.0f, 1.0f, |
| 87 | 1.0f,-1.0f, 1.0f, |
| 88 | |
| 89 | -1.0f, 1.0f,-1.0f, // Vertex 6 |
| 90 | -1.0f, 1.0f, 1.0f, |
| 91 | 1.0f, 1.0f, 1.0f, |
| 92 | |
| 93 | -1.0f, 1.0f,-1.0f, // Vertex 7 |
| 94 | 1.0f, 1.0f,-1.0f, |
| 95 | 1.0f, 1.0f, 1.0f, |
| 96 | |
| 97 | 1.0f, 1.0f,-1.0f, // Vertex 8 |
| 98 | 1.0f, 1.0f, 1.0f, |
| 99 | 1.0f,-1.0f, 1.0f, |
| 100 | |
| 101 | 1.0f,-1.0f, 1.0f, // Vertex 9 |
| 102 | 1.0f,-1.0f,-1.0f, |
| 103 | 1.0f, 1.0f,-1.0f, |
| 104 | |
| 105 | -1.0f, 1.0f, 1.0f, // Vertex 10 |
| 106 | 1.0f, 1.0f, 1.0f, |
| 107 | -1.0f,-1.0f, 1.0f, |
| 108 | |
| 109 | -1.0f,-1.0f, 1.0f, // Vertex 11 |
| 110 | 1.0f,-1.0f, 1.0f, |
| 111 | 1.0f, 1.0f, 1.0f, |
Courtney Goeltzenleuchter | 4984d15 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 112 | }; |
| 113 | |
| 114 | static const XGL_FLOAT g_uv_buffer_data[] = { |
Courtney Goeltzenleuchter | 8879d3a | 2014-10-29 08:29:35 -0600 | [diff] [blame] | 115 | 1.0f, 0.0f, // Vertex 0 |
Courtney Goeltzenleuchter | 6836939 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 116 | 0.0f, 0.0f, |
| 117 | 0.0f, 1.0f, |
| 118 | |
Courtney Goeltzenleuchter | 8879d3a | 2014-10-29 08:29:35 -0600 | [diff] [blame] | 119 | 0.0f, 1.0f, // Vertex 1 |
Courtney Goeltzenleuchter | 6836939 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 120 | 1.0f, 1.0f, |
Courtney Goeltzenleuchter | 8879d3a | 2014-10-29 08:29:35 -0600 | [diff] [blame] | 121 | 1.0f, 0.0f, |
| 122 | |
| 123 | // 0.0f, 1.0f, // Vertex 2 |
| 124 | // 1.0f, 0.0f, |
| 125 | // 0.0f, 0.0f, |
| 126 | |
| 127 | // 0.0f, 1.0f, // Vertex 3 |
| 128 | // 1.0f, 0.0f, |
| 129 | // 1.0f, 1.0f, |
| 130 | |
| 131 | 0.0f, 0.0f, // Vertex 2 |
| 132 | 1.0f, 1.0f, |
| 133 | 1.0f, 0.0f, |
| 134 | |
| 135 | 0.0f, 0.0f, // Vertex 3 |
| 136 | 1.0f, 1.0f, |
| 137 | 0.0f, 1.0f, |
Courtney Goeltzenleuchter | 6836939 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 138 | |
| 139 | 0.0f, 1.0f, // Vertex 4 |
| 140 | 1.0f, 0.0f, |
| 141 | 0.0f, 0.0f, |
| 142 | |
| 143 | 0.0f, 1.0f, // Vertex 5 |
| 144 | 1.0f, 1.0f, |
| 145 | 1.0f, 0.0f, |
| 146 | |
| 147 | 0.0f, 1.0f, // Vertex 6 |
| 148 | 1.0f, 1.0f, |
| 149 | 1.0f, 0.0f, |
| 150 | |
| 151 | 0.0f, 1.0f, // Vertex 7 |
| 152 | 0.0f, 0.0f, |
| 153 | 1.0f, 0.0f, |
| 154 | |
Courtney Goeltzenleuchter | 8879d3a | 2014-10-29 08:29:35 -0600 | [diff] [blame] | 155 | 0.0f, 1.0f, // Vertex 8 |
Courtney Goeltzenleuchter | 6836939 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 156 | 1.0f, 1.0f, |
Courtney Goeltzenleuchter | 6836939 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 157 | 1.0f, 0.0f, |
Courtney Goeltzenleuchter | 8879d3a | 2014-10-29 08:29:35 -0600 | [diff] [blame] | 158 | |
| 159 | 1.0f, 0.0f, // Vertex 9 |
Courtney Goeltzenleuchter | 6836939 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 160 | 0.0f, 0.0f, |
Courtney Goeltzenleuchter | 6836939 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 161 | 0.0f, 1.0f, |
| 162 | |
Courtney Goeltzenleuchter | 8879d3a | 2014-10-29 08:29:35 -0600 | [diff] [blame] | 163 | 1.0f, 1.0f, // Vertex 10 |
| 164 | 0.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 11 |
| 168 | 0.0f, 0.0f, |
| 169 | 0.0f, 1.0f, |
Courtney Goeltzenleuchter | 4984d15 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 170 | }; |
| 171 | |
| 172 | void dumpMatrix(const char *note, mat4x4 MVP) |
| 173 | { |
| 174 | int i; |
| 175 | |
| 176 | printf("%s: \n", note); |
| 177 | for (i=0; i<4; i++) { |
| 178 | printf("%f, %f, %f, %f\n", MVP[i][0], MVP[i][1], MVP[i][2], MVP[i][3]); |
| 179 | } |
| 180 | printf("\n"); |
| 181 | fflush(stdout); |
| 182 | } |
| 183 | |
| 184 | void dumpVec4(const char *note, vec4 vector) |
| 185 | { |
| 186 | printf("%s: \n", note); |
| 187 | printf("%f, %f, %f, %f\n", vector[0], vector[1], vector[2], vector[3]); |
| 188 | printf("\n"); |
| 189 | fflush(stdout); |
| 190 | } |
| 191 | |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 192 | struct demo { |
| 193 | xcb_connection_t *connection; |
| 194 | xcb_screen_t *screen; |
| 195 | |
| 196 | XGL_PHYSICAL_GPU gpu; |
| 197 | XGL_DEVICE device; |
| 198 | XGL_QUEUE queue; |
| 199 | |
| 200 | int width, height; |
| 201 | XGL_FORMAT format; |
| 202 | |
| 203 | struct { |
| 204 | XGL_IMAGE image; |
| 205 | XGL_GPU_MEMORY mem; |
| 206 | |
| 207 | XGL_COLOR_ATTACHMENT_VIEW view; |
Chia-I Wu | bb57f64 | 2014-11-07 14:30:34 +0800 | [diff] [blame] | 208 | XGL_FENCE fence; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 209 | } buffers[DEMO_BUFFER_COUNT]; |
| 210 | |
| 211 | struct { |
| 212 | XGL_FORMAT format; |
| 213 | |
| 214 | XGL_IMAGE image; |
Jon Ashburn | b2a6665 | 2015-01-16 09:37:43 -0700 | [diff] [blame] | 215 | XGL_UINT num_mem; |
| 216 | XGL_GPU_MEMORY *mem; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 217 | XGL_DEPTH_STENCIL_VIEW view; |
| 218 | } depth; |
| 219 | |
| 220 | struct { |
| 221 | XGL_SAMPLER sampler; |
| 222 | |
Courtney Goeltzenleuchter | 6836939 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 223 | char *filename; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 224 | XGL_IMAGE image; |
Jon Ashburn | b2a6665 | 2015-01-16 09:37:43 -0700 | [diff] [blame] | 225 | XGL_UINT num_mem; |
| 226 | XGL_GPU_MEMORY *mem; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 227 | XGL_IMAGE_VIEW view; |
| 228 | } textures[DEMO_TEXTURE_COUNT]; |
| 229 | |
| 230 | struct { |
Chia-I Wu | 8ea21c7 | 2015-01-01 07:55:04 +0800 | [diff] [blame] | 231 | XGL_BUFFER buf; |
Jon Ashburn | ae7c21c | 2015-01-19 15:00:26 -0700 | [diff] [blame] | 232 | XGL_UINT num_mem; |
| 233 | XGL_GPU_MEMORY *mem; |
Chia-I Wu | 8ea21c7 | 2015-01-01 07:55:04 +0800 | [diff] [blame] | 234 | XGL_BUFFER_VIEW view; |
| 235 | XGL_BUFFER_VIEW_ATTACH_INFO attach; |
Courtney Goeltzenleuchter | fc3b953 | 2014-10-28 10:32:57 -0600 | [diff] [blame] | 236 | } uniform_data; |
| 237 | |
Chia-I Wu | 6a3c897 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 238 | XGL_DESCRIPTOR_SET_LAYOUT desc_layout_vs; |
| 239 | XGL_DESCRIPTOR_SET_LAYOUT desc_layout_fs; |
| 240 | XGL_DESCRIPTOR_SET_LAYOUT *desc_layout_last; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 241 | XGL_PIPELINE pipeline; |
| 242 | |
Tony Barbour | 29645d0 | 2015-01-16 14:27:35 -0700 | [diff] [blame] | 243 | XGL_DYNAMIC_VP_STATE_OBJECT viewport; |
| 244 | XGL_DYNAMIC_RS_STATE_OBJECT raster; |
| 245 | XGL_DYNAMIC_CB_STATE_OBJECT color_blend; |
| 246 | XGL_DYNAMIC_DS_STATE_OBJECT depth_stencil; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 247 | |
Courtney Goeltzenleuchter | fc3b953 | 2014-10-28 10:32:57 -0600 | [diff] [blame] | 248 | mat4x4 projection_matrix; |
| 249 | mat4x4 view_matrix; |
| 250 | mat4x4 model_matrix; |
| 251 | |
Courtney Goeltzenleuchter | 8879d3a | 2014-10-29 08:29:35 -0600 | [diff] [blame] | 252 | XGL_FLOAT spin_angle; |
| 253 | XGL_FLOAT spin_increment; |
| 254 | bool pause; |
| 255 | |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 256 | XGL_CMD_BUFFER cmd; |
| 257 | |
Chia-I Wu | 6a3c897 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 258 | XGL_DESCRIPTOR_REGION desc_region; |
| 259 | XGL_DESCRIPTOR_SET desc_set; |
| 260 | |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 261 | xcb_window_t window; |
Courtney Goeltzenleuchter | 98cb2cb | 2014-11-06 14:27:52 -0700 | [diff] [blame] | 262 | xcb_intern_atom_reply_t *atom_wm_delete_window; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 263 | |
| 264 | bool quit; |
| 265 | XGL_UINT current_buffer; |
| 266 | }; |
| 267 | |
| 268 | static void demo_draw_build_cmd(struct demo *demo) |
| 269 | { |
| 270 | const XGL_COLOR_ATTACHMENT_BIND_INFO color_attachment = { |
| 271 | .view = demo->buffers[demo->current_buffer].view, |
Mike Stroyan | 3bd2d89 | 2014-12-04 11:08:39 +0000 | [diff] [blame] | 272 | .layout = XGL_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 273 | }; |
| 274 | const XGL_DEPTH_STENCIL_BIND_INFO depth_stencil = { |
| 275 | .view = demo->depth.view, |
Mike Stroyan | 3bd2d89 | 2014-12-04 11:08:39 +0000 | [diff] [blame] | 276 | .layout = XGL_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL, |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 277 | }; |
Courtney Goeltzenleuchter | fc3b953 | 2014-10-28 10:32:57 -0600 | [diff] [blame] | 278 | const XGL_FLOAT clear_color[4] = { 0.2f, 0.2f, 0.2f, 0.2f }; |
Courtney Goeltzenleuchter | 4984d15 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 279 | const XGL_FLOAT clear_depth = 1.0f; |
Courtney Goeltzenleuchter | fc3b953 | 2014-10-28 10:32:57 -0600 | [diff] [blame] | 280 | XGL_IMAGE_SUBRESOURCE_RANGE clear_range; |
Jon Ashburn | 08f6eb9 | 2015-01-02 18:24:05 -0700 | [diff] [blame] | 281 | XGL_CMD_BUFFER_GRAPHICS_BEGIN_INFO graphics_cmd_buf_info = { |
| 282 | .sType = XGL_STRUCTURE_TYPE_CMD_BUFFER_GRAPHICS_BEGIN_INFO, |
| 283 | .pNext = NULL, |
| 284 | .operation = XGL_RENDER_PASS_OPERATION_BEGIN_AND_END, |
| 285 | }; |
Jon Ashburn | 60ccfbe | 2014-12-31 17:08:35 -0700 | [diff] [blame] | 286 | XGL_CMD_BUFFER_BEGIN_INFO cmd_buf_info = { |
| 287 | .sType = XGL_STRUCTURE_TYPE_CMD_BUFFER_BEGIN_INFO, |
Jon Ashburn | 08f6eb9 | 2015-01-02 18:24:05 -0700 | [diff] [blame] | 288 | .pNext = &graphics_cmd_buf_info, |
Jon Ashburn | 60ccfbe | 2014-12-31 17:08:35 -0700 | [diff] [blame] | 289 | .flags = XGL_CMD_BUFFER_OPTIMIZE_GPU_SMALL_BATCH_BIT | |
| 290 | XGL_CMD_BUFFER_OPTIMIZE_ONE_TIME_SUBMIT_BIT, |
| 291 | }; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 292 | XGL_RESULT err; |
Jon Ashburn | 08f6eb9 | 2015-01-02 18:24:05 -0700 | [diff] [blame] | 293 | XGL_ATTACHMENT_LOAD_OP load_op = XGL_ATTACHMENT_LOAD_OP_DONT_CARE; |
| 294 | XGL_ATTACHMENT_STORE_OP store_op = XGL_ATTACHMENT_STORE_OP_DONT_CARE; |
| 295 | const XGL_FRAMEBUFFER_CREATE_INFO fb_info = { |
| 296 | .sType = XGL_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO, |
| 297 | .pNext = NULL, |
| 298 | .colorAttachmentCount = 1, |
| 299 | .pColorAttachments = (XGL_COLOR_ATTACHMENT_BIND_INFO*) &color_attachment, |
| 300 | .pDepthStencilAttachment = (XGL_DEPTH_STENCIL_BIND_INFO*) &depth_stencil, |
| 301 | .sampleCount = 1, |
| 302 | }; |
| 303 | XGL_RENDER_PASS_CREATE_INFO rp_info; |
| 304 | |
| 305 | memset(&rp_info, 0 , sizeof(rp_info)); |
| 306 | err = xglCreateFramebuffer(demo->device, &fb_info, &(rp_info.framebuffer)); |
| 307 | assert(!err); |
| 308 | rp_info.sType = XGL_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO; |
| 309 | rp_info.renderArea.extent.width = demo->width; |
| 310 | rp_info.renderArea.extent.height = demo->height; |
| 311 | rp_info.pColorLoadOps = &load_op; |
| 312 | rp_info.pColorStoreOps = &store_op; |
| 313 | rp_info.depthLoadOp = XGL_ATTACHMENT_LOAD_OP_DONT_CARE; |
| 314 | rp_info.depthStoreOp = XGL_ATTACHMENT_STORE_OP_DONT_CARE; |
| 315 | rp_info.stencilLoadOp = XGL_ATTACHMENT_LOAD_OP_DONT_CARE; |
| 316 | rp_info.stencilStoreOp = XGL_ATTACHMENT_STORE_OP_DONT_CARE; |
| 317 | err = xglCreateRenderPass(demo->device, &rp_info, &(graphics_cmd_buf_info.renderPass)); |
| 318 | assert(!err); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 319 | |
Jon Ashburn | 60ccfbe | 2014-12-31 17:08:35 -0700 | [diff] [blame] | 320 | err = xglBeginCommandBuffer(demo->cmd, &cmd_buf_info); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 321 | assert(!err); |
| 322 | |
| 323 | xglCmdBindPipeline(demo->cmd, XGL_PIPELINE_BIND_POINT_GRAPHICS, |
| 324 | demo->pipeline); |
| 325 | xglCmdBindDescriptorSet(demo->cmd, XGL_PIPELINE_BIND_POINT_GRAPHICS, |
Chia-I Wu | 6a3c897 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 326 | demo->desc_set, NULL); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 327 | |
Tony Barbour | 29645d0 | 2015-01-16 14:27:35 -0700 | [diff] [blame] | 328 | xglCmdBindDynamicStateObject(demo->cmd, XGL_STATE_BIND_VIEWPORT, demo->viewport); |
| 329 | xglCmdBindDynamicStateObject(demo->cmd, XGL_STATE_BIND_RASTER, demo->raster); |
| 330 | xglCmdBindDynamicStateObject(demo->cmd, XGL_STATE_BIND_COLOR_BLEND, |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 331 | demo->color_blend); |
Tony Barbour | 29645d0 | 2015-01-16 14:27:35 -0700 | [diff] [blame] | 332 | xglCmdBindDynamicStateObject(demo->cmd, XGL_STATE_BIND_DEPTH_STENCIL, |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 333 | demo->depth_stencil); |
| 334 | |
Courtney Goeltzenleuchter | fc3b953 | 2014-10-28 10:32:57 -0600 | [diff] [blame] | 335 | clear_range.aspect = XGL_IMAGE_ASPECT_COLOR; |
| 336 | clear_range.baseMipLevel = 0; |
| 337 | clear_range.mipLevels = 1; |
| 338 | clear_range.baseArraySlice = 0; |
| 339 | clear_range.arraySize = 1; |
| 340 | xglCmdClearColorImage(demo->cmd, |
| 341 | demo->buffers[demo->current_buffer].image, |
| 342 | clear_color, 1, &clear_range); |
| 343 | |
| 344 | clear_range.aspect = XGL_IMAGE_ASPECT_DEPTH; |
| 345 | xglCmdClearDepthStencil(demo->cmd, demo->depth.image, |
| 346 | clear_depth, 0, 1, &clear_range); |
| 347 | |
Courtney Goeltzenleuchter | 8879d3a | 2014-10-29 08:29:35 -0600 | [diff] [blame] | 348 | xglCmdDraw(demo->cmd, 0, 12 * 3, 0, 1); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 349 | |
| 350 | err = xglEndCommandBuffer(demo->cmd); |
| 351 | assert(!err); |
| 352 | } |
| 353 | |
Courtney Goeltzenleuchter | fc3b953 | 2014-10-28 10:32:57 -0600 | [diff] [blame] | 354 | |
Courtney Goeltzenleuchter | 8879d3a | 2014-10-29 08:29:35 -0600 | [diff] [blame] | 355 | void demo_update_data_buffer(struct demo *demo) |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 356 | { |
Courtney Goeltzenleuchter | fc3b953 | 2014-10-28 10:32:57 -0600 | [diff] [blame] | 357 | mat4x4 MVP, Model, VP; |
| 358 | int matrixSize = sizeof(MVP); |
| 359 | XGL_UINT8 *pData; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 360 | XGL_RESULT err; |
| 361 | |
Courtney Goeltzenleuchter | fc3b953 | 2014-10-28 10:32:57 -0600 | [diff] [blame] | 362 | mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 363 | |
Courtney Goeltzenleuchter | fc3b953 | 2014-10-28 10:32:57 -0600 | [diff] [blame] | 364 | // Rotate 22.5 degrees around the Y axis |
Courtney Goeltzenleuchter | 4984d15 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 365 | mat4x4_dup(Model, demo->model_matrix); |
Courtney Goeltzenleuchter | 8879d3a | 2014-10-29 08:29:35 -0600 | [diff] [blame] | 366 | mat4x4_rotate(demo->model_matrix, Model, 0.0f, 1.0f, 0.0f, degreesToRadians(demo->spin_angle)); |
Courtney Goeltzenleuchter | 4984d15 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 367 | mat4x4_mul(MVP, VP, demo->model_matrix); |
Courtney Goeltzenleuchter | fc3b953 | 2014-10-28 10:32:57 -0600 | [diff] [blame] | 368 | |
Jon Ashburn | ae7c21c | 2015-01-19 15:00:26 -0700 | [diff] [blame] | 369 | assert(demo->uniform_data.num_mem == 1); |
| 370 | err = xglMapMemory(demo->uniform_data.mem[0], 0, (XGL_VOID **) &pData); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 371 | assert(!err); |
| 372 | |
Courtney Goeltzenleuchter | fc3b953 | 2014-10-28 10:32:57 -0600 | [diff] [blame] | 373 | memcpy(pData, (const void*) &MVP[0][0], matrixSize); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 374 | |
Jon Ashburn | ae7c21c | 2015-01-19 15:00:26 -0700 | [diff] [blame] | 375 | err = xglUnmapMemory(demo->uniform_data.mem[0]); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 376 | assert(!err); |
| 377 | } |
| 378 | |
| 379 | static void demo_draw(struct demo *demo) |
| 380 | { |
| 381 | const XGL_WSI_X11_PRESENT_INFO present = { |
| 382 | .destWindow = demo->window, |
| 383 | .srcImage = demo->buffers[demo->current_buffer].image, |
Chia-I Wu | bb57f64 | 2014-11-07 14:30:34 +0800 | [diff] [blame] | 384 | .async = true, |
| 385 | .flip = false, |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 386 | }; |
Chia-I Wu | bb57f64 | 2014-11-07 14:30:34 +0800 | [diff] [blame] | 387 | XGL_FENCE fence = demo->buffers[demo->current_buffer].fence; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 388 | XGL_RESULT err; |
| 389 | |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 390 | demo_draw_build_cmd(demo); |
| 391 | |
Chia-I Wu | bb57f64 | 2014-11-07 14:30:34 +0800 | [diff] [blame] | 392 | err = xglWaitForFences(demo->device, 1, &fence, XGL_TRUE, ~((XGL_UINT64) 0)); |
| 393 | assert(err == XGL_SUCCESS || err == XGL_ERROR_UNAVAILABLE); |
| 394 | |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 395 | err = xglQueueSubmit(demo->queue, 1, &demo->cmd, |
| 396 | 0, NULL, XGL_NULL_HANDLE); |
| 397 | assert(!err); |
| 398 | |
Chia-I Wu | bb57f64 | 2014-11-07 14:30:34 +0800 | [diff] [blame] | 399 | err = xglWsiX11QueuePresent(demo->queue, &present, fence); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 400 | assert(!err); |
| 401 | |
| 402 | demo->current_buffer = (demo->current_buffer + 1) % DEMO_BUFFER_COUNT; |
| 403 | } |
| 404 | |
| 405 | static void demo_prepare_buffers(struct demo *demo) |
| 406 | { |
| 407 | const XGL_WSI_X11_PRESENTABLE_IMAGE_CREATE_INFO presentable_image = { |
| 408 | .format = demo->format, |
| 409 | .usage = XGL_IMAGE_USAGE_COLOR_ATTACHMENT_BIT, |
| 410 | .extent = { |
| 411 | .width = demo->width, |
| 412 | .height = demo->height, |
| 413 | }, |
| 414 | .flags = 0, |
| 415 | }; |
Chia-I Wu | bb57f64 | 2014-11-07 14:30:34 +0800 | [diff] [blame] | 416 | const XGL_FENCE_CREATE_INFO fence = { |
| 417 | .sType = XGL_STRUCTURE_TYPE_FENCE_CREATE_INFO, |
| 418 | .pNext = NULL, |
| 419 | .flags = 0, |
| 420 | }; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 421 | XGL_RESULT err; |
| 422 | XGL_UINT i; |
| 423 | |
| 424 | for (i = 0; i < DEMO_BUFFER_COUNT; i++) { |
| 425 | XGL_COLOR_ATTACHMENT_VIEW_CREATE_INFO color_attachment_view = { |
| 426 | .sType = XGL_STRUCTURE_TYPE_COLOR_ATTACHMENT_VIEW_CREATE_INFO, |
| 427 | .pNext = NULL, |
| 428 | .format = demo->format, |
| 429 | .mipLevel = 0, |
| 430 | .baseArraySlice = 0, |
| 431 | .arraySize = 1, |
| 432 | }; |
| 433 | |
| 434 | err = xglWsiX11CreatePresentableImage(demo->device, &presentable_image, |
| 435 | &demo->buffers[i].image, &demo->buffers[i].mem); |
| 436 | assert(!err); |
| 437 | |
| 438 | color_attachment_view.image = demo->buffers[i].image; |
| 439 | |
| 440 | err = xglCreateColorAttachmentView(demo->device, |
| 441 | &color_attachment_view, &demo->buffers[i].view); |
| 442 | assert(!err); |
Chia-I Wu | bb57f64 | 2014-11-07 14:30:34 +0800 | [diff] [blame] | 443 | |
| 444 | err = xglCreateFence(demo->device, |
| 445 | &fence, &demo->buffers[i].fence); |
| 446 | assert(!err); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 447 | } |
| 448 | } |
| 449 | |
| 450 | static void demo_prepare_depth(struct demo *demo) |
| 451 | { |
| 452 | const XGL_FORMAT depth_format = { XGL_CH_FMT_R16, XGL_NUM_FMT_DS }; |
| 453 | const XGL_IMAGE_CREATE_INFO image = { |
| 454 | .sType = XGL_STRUCTURE_TYPE_IMAGE_CREATE_INFO, |
| 455 | .pNext = NULL, |
| 456 | .imageType = XGL_IMAGE_2D, |
| 457 | .format = depth_format, |
| 458 | .extent = { demo->width, demo->height, 1 }, |
| 459 | .mipLevels = 1, |
| 460 | .arraySize = 1, |
| 461 | .samples = 1, |
| 462 | .tiling = XGL_OPTIMAL_TILING, |
| 463 | .usage = XGL_IMAGE_USAGE_DEPTH_STENCIL_BIT, |
| 464 | .flags = 0, |
| 465 | }; |
Jon Ashburn | ae7c21c | 2015-01-19 15:00:26 -0700 | [diff] [blame] | 466 | XGL_MEMORY_ALLOC_IMAGE_INFO img_alloc = { |
| 467 | .sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_IMAGE_INFO, |
| 468 | .pNext = NULL, |
| 469 | }; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 470 | XGL_MEMORY_ALLOC_INFO mem_alloc = { |
| 471 | .sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_INFO, |
Jon Ashburn | ae7c21c | 2015-01-19 15:00:26 -0700 | [diff] [blame] | 472 | .pNext = &img_alloc, |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 473 | .allocationSize = 0, |
| 474 | .alignment = 0, |
Jon Ashburn | c483a8c | 2015-01-20 13:55:32 -0700 | [diff] [blame^] | 475 | .memProps = XGL_MEMORY_PROPERTY_GPU_ONLY, |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 476 | .flags = 0, |
| 477 | .heapCount = 0, |
| 478 | .memPriority = XGL_MEMORY_PRIORITY_NORMAL, |
| 479 | }; |
| 480 | XGL_DEPTH_STENCIL_VIEW_CREATE_INFO view = { |
| 481 | .sType = XGL_STRUCTURE_TYPE_DEPTH_STENCIL_VIEW_CREATE_INFO, |
| 482 | .pNext = NULL, |
| 483 | .image = XGL_NULL_HANDLE, |
| 484 | .mipLevel = 0, |
| 485 | .baseArraySlice = 0, |
| 486 | .arraySize = 1, |
| 487 | .flags = 0, |
| 488 | }; |
Jon Ashburn | b2a6665 | 2015-01-16 09:37:43 -0700 | [diff] [blame] | 489 | XGL_MEMORY_REQUIREMENTS *mem_reqs; |
Jon Ashburn | bd75a81 | 2014-11-21 13:29:30 -0700 | [diff] [blame] | 490 | XGL_SIZE mem_reqs_size = sizeof(XGL_MEMORY_REQUIREMENTS); |
Jon Ashburn | ae7c21c | 2015-01-19 15:00:26 -0700 | [diff] [blame] | 491 | XGL_IMAGE_MEMORY_REQUIREMENTS img_reqs; |
| 492 | XGL_SIZE img_reqs_size = sizeof(XGL_IMAGE_MEMORY_REQUIREMENTS); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 493 | XGL_RESULT err; |
Jon Ashburn | b2a6665 | 2015-01-16 09:37:43 -0700 | [diff] [blame] | 494 | XGL_UINT num_allocations = 0; |
| 495 | XGL_SIZE num_alloc_size = sizeof(num_allocations); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 496 | |
| 497 | demo->depth.format = depth_format; |
| 498 | |
| 499 | /* create image */ |
| 500 | err = xglCreateImage(demo->device, &image, |
| 501 | &demo->depth.image); |
| 502 | assert(!err); |
| 503 | |
Jon Ashburn | b2a6665 | 2015-01-16 09:37:43 -0700 | [diff] [blame] | 504 | |
| 505 | err = xglGetObjectInfo(demo->depth.image, XGL_INFO_TYPE_MEMORY_ALLOCATION_COUNT, &num_alloc_size, &num_allocations); |
| 506 | assert(!err && num_alloc_size == sizeof(num_allocations)); |
| 507 | mem_reqs = malloc(num_allocations * sizeof(XGL_MEMORY_REQUIREMENTS)); |
| 508 | demo->depth.mem = malloc(num_allocations * sizeof(XGL_GPU_MEMORY)); |
| 509 | demo->depth.num_mem = num_allocations; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 510 | err = xglGetObjectInfo(demo->depth.image, |
Jon Ashburn | b2a6665 | 2015-01-16 09:37:43 -0700 | [diff] [blame] | 511 | XGL_INFO_TYPE_MEMORY_REQUIREMENTS, |
| 512 | &mem_reqs_size, mem_reqs); |
| 513 | assert(!err && mem_reqs_size == num_allocations * sizeof(XGL_MEMORY_REQUIREMENTS)); |
Jon Ashburn | ae7c21c | 2015-01-19 15:00:26 -0700 | [diff] [blame] | 514 | err = xglGetObjectInfo(demo->depth.image, |
| 515 | XGL_INFO_TYPE_IMAGE_MEMORY_REQUIREMENTS, |
| 516 | &img_reqs_size, &img_reqs); |
| 517 | assert(!err && img_reqs_size == sizeof(XGL_IMAGE_MEMORY_REQUIREMENTS)); |
| 518 | img_alloc.usage = img_reqs.usage; |
| 519 | img_alloc.formatClass = img_reqs.formatClass; |
| 520 | img_alloc.samples = img_reqs.samples; |
Jon Ashburn | b2a6665 | 2015-01-16 09:37:43 -0700 | [diff] [blame] | 521 | for (XGL_UINT i = 0; i < num_allocations; i ++) { |
| 522 | mem_alloc.allocationSize = mem_reqs[i].size; |
| 523 | mem_alloc.alignment = mem_reqs[i].alignment; |
| 524 | mem_alloc.heapCount = mem_reqs[i].heapCount; |
| 525 | XGL_UINT heapInfo[mem_reqs[i].heapCount]; |
| 526 | mem_alloc.pHeaps = (const XGL_UINT *) heapInfo; |
| 527 | memcpy(heapInfo, mem_reqs[i].pHeaps, |
| 528 | sizeof(mem_reqs[i].pHeaps[0]) * mem_reqs[i].heapCount); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 529 | |
Jon Ashburn | b2a6665 | 2015-01-16 09:37:43 -0700 | [diff] [blame] | 530 | /* allocate memory */ |
| 531 | err = xglAllocMemory(demo->device, &mem_alloc, |
| 532 | &(demo->depth.mem[i])); |
| 533 | assert(!err); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 534 | |
Jon Ashburn | b2a6665 | 2015-01-16 09:37:43 -0700 | [diff] [blame] | 535 | /* bind memory */ |
| 536 | err = xglBindObjectMemory(demo->depth.image, i, |
| 537 | demo->depth.mem[i], 0); |
| 538 | assert(!err); |
| 539 | } |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 540 | |
| 541 | /* create image view */ |
| 542 | view.image = demo->depth.image; |
| 543 | err = xglCreateDepthStencilView(demo->device, &view, |
| 544 | &demo->depth.view); |
| 545 | assert(!err); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 546 | } |
| 547 | |
Courtney Goeltzenleuchter | 6836939 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 548 | /** loadTexture |
| 549 | * loads a png file into an memory object, using cstdio , libpng. |
| 550 | * |
| 551 | * \param demo : Needed to access XGL calls |
| 552 | * \param filename : the png file to be loaded |
| 553 | * \param width : width of png, to be updated as a side effect of this function |
| 554 | * \param height : height of png, to be updated as a side effect of this function |
| 555 | * |
| 556 | * \return bool : an opengl texture id. true if successful?, |
| 557 | * should be validated by the client of this function. |
| 558 | * |
| 559 | * Source: http://en.wikibooks.org/wiki/OpenGL_Programming/Intermediate/Textures |
| 560 | * Modified to copy image to memory |
| 561 | * |
| 562 | */ |
| 563 | bool loadTexture(char *filename, XGL_UINT8 *rgba_data, |
| 564 | XGL_SUBRESOURCE_LAYOUT *layout, |
| 565 | XGL_INT *width, XGL_INT *height) |
| 566 | { |
| 567 | //header for testing if it is a png |
| 568 | png_byte header[8]; |
| 569 | int i, is_png, bit_depth, color_type,rowbytes; |
| 570 | png_uint_32 twidth, theight; |
| 571 | png_structp png_ptr; |
| 572 | png_infop info_ptr, end_info; |
| 573 | png_byte *image_data; |
| 574 | png_bytep *row_pointers; |
| 575 | |
| 576 | //open file as binary |
| 577 | FILE *fp = fopen(filename, "rb"); |
| 578 | if (!fp) { |
| 579 | return false; |
| 580 | } |
| 581 | |
| 582 | //read the header |
| 583 | fread(header, 1, 8, fp); |
| 584 | |
| 585 | //test if png |
| 586 | is_png = !png_sig_cmp(header, 0, 8); |
| 587 | if (!is_png) { |
| 588 | fclose(fp); |
| 589 | return false; |
| 590 | } |
| 591 | |
| 592 | //create png struct |
| 593 | png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, |
| 594 | NULL, NULL); |
| 595 | if (!png_ptr) { |
| 596 | fclose(fp); |
| 597 | return (false); |
| 598 | } |
| 599 | |
| 600 | //create png info struct |
| 601 | info_ptr = png_create_info_struct(png_ptr); |
| 602 | if (!info_ptr) { |
| 603 | png_destroy_read_struct(&png_ptr, (png_infopp) NULL, (png_infopp) NULL); |
| 604 | fclose(fp); |
| 605 | return (false); |
| 606 | } |
| 607 | |
| 608 | //create png info struct |
| 609 | end_info = png_create_info_struct(png_ptr); |
| 610 | if (!end_info) { |
| 611 | png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp) NULL); |
| 612 | fclose(fp); |
| 613 | return (false); |
| 614 | } |
| 615 | |
| 616 | //png error stuff, not sure libpng man suggests this. |
| 617 | if (setjmp(png_jmpbuf(png_ptr))) { |
| 618 | png_destroy_read_struct(&png_ptr, &info_ptr, &end_info); |
| 619 | fclose(fp); |
| 620 | return (false); |
| 621 | } |
| 622 | |
| 623 | //init png reading |
| 624 | png_init_io(png_ptr, fp); |
| 625 | |
| 626 | //let libpng know you already read the first 8 bytes |
| 627 | png_set_sig_bytes(png_ptr, 8); |
| 628 | |
| 629 | // read all the info up to the image data |
| 630 | png_read_info(png_ptr, info_ptr); |
| 631 | |
| 632 | // get info about png |
| 633 | png_get_IHDR(png_ptr, info_ptr, &twidth, &theight, &bit_depth, &color_type, |
| 634 | NULL, NULL, NULL); |
| 635 | |
| 636 | //update width and height based on png info |
| 637 | *width = twidth; |
| 638 | *height = theight; |
| 639 | |
| 640 | // Require that incoming texture be 8bits per color component |
| 641 | // and 4 components (RGBA). |
| 642 | if (png_get_bit_depth(png_ptr, info_ptr) != 8 || |
| 643 | png_get_channels(png_ptr, info_ptr) != 4) { |
| 644 | return false; |
| 645 | } |
| 646 | |
| 647 | if (rgba_data == NULL) { |
| 648 | // If data pointer is null, we just want the width & height |
| 649 | // clean up memory and close stuff |
| 650 | png_destroy_read_struct(&png_ptr, &info_ptr, &end_info); |
| 651 | fclose(fp); |
| 652 | |
| 653 | return true; |
| 654 | } |
| 655 | |
| 656 | // Update the png info struct. |
| 657 | png_read_update_info(png_ptr, info_ptr); |
| 658 | |
| 659 | // Row size in bytes. |
| 660 | rowbytes = png_get_rowbytes(png_ptr, info_ptr); |
| 661 | |
| 662 | // Allocate the image_data as a big block, to be given to opengl |
| 663 | image_data = (png_byte *)malloc(rowbytes * theight * sizeof(png_byte)); |
| 664 | if (!image_data) { |
| 665 | //clean up memory and close stuff |
| 666 | png_destroy_read_struct(&png_ptr, &info_ptr, &end_info); |
| 667 | fclose(fp); |
| 668 | return false; |
| 669 | } |
| 670 | |
| 671 | // row_pointers is for pointing to image_data for reading the png with libpng |
| 672 | row_pointers = (png_bytep *)malloc(theight * sizeof(png_bytep)); |
| 673 | if (!row_pointers) { |
| 674 | //clean up memory and close stuff |
| 675 | png_destroy_read_struct(&png_ptr, &info_ptr, &end_info); |
| 676 | // delete[] image_data; |
| 677 | fclose(fp); |
| 678 | return false; |
| 679 | } |
| 680 | // set the individual row_pointers to point at the correct offsets of image_data |
| 681 | for (i = 0; i < theight; ++i) |
| 682 | row_pointers[theight - 1 - i] = rgba_data + i * rowbytes; |
| 683 | |
| 684 | // read the png into image_data through row_pointers |
| 685 | png_read_image(png_ptr, row_pointers); |
| 686 | |
| 687 | // clean up memory and close stuff |
| 688 | png_destroy_read_struct(&png_ptr, &info_ptr, &end_info); |
| 689 | free(row_pointers); |
| 690 | free(image_data); |
| 691 | fclose(fp); |
| 692 | |
| 693 | return true; |
| 694 | } |
Courtney Goeltzenleuchter | 6836939 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 695 | |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 696 | static void demo_prepare_textures(struct demo *demo) |
| 697 | { |
Courtney Goeltzenleuchter | 4984d15 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 698 | const XGL_FORMAT tex_format = { XGL_CH_FMT_R8G8B8A8, XGL_NUM_FMT_UNORM }; |
Courtney Goeltzenleuchter | 6836939 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 699 | XGL_INT tex_width; |
| 700 | XGL_INT tex_height; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 701 | XGL_RESULT err; |
| 702 | XGL_UINT i; |
| 703 | |
| 704 | for (i = 0; i < DEMO_TEXTURE_COUNT; i++) { |
| 705 | const XGL_SAMPLER_CREATE_INFO sampler = { |
| 706 | .sType = XGL_STRUCTURE_TYPE_SAMPLER_CREATE_INFO, |
| 707 | .pNext = NULL, |
| 708 | .magFilter = XGL_TEX_FILTER_NEAREST, |
| 709 | .minFilter = XGL_TEX_FILTER_NEAREST, |
| 710 | .mipMode = XGL_TEX_MIPMAP_BASE, |
Courtney Goeltzenleuchter | 6836939 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 711 | .addressU = XGL_TEX_ADDRESS_CLAMP, |
| 712 | .addressV = XGL_TEX_ADDRESS_CLAMP, |
| 713 | .addressW = XGL_TEX_ADDRESS_CLAMP, |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 714 | .mipLodBias = 0.0f, |
| 715 | .maxAnisotropy = 0, |
| 716 | .compareFunc = XGL_COMPARE_NEVER, |
| 717 | .minLod = 0.0f, |
| 718 | .maxLod = 0.0f, |
| 719 | .borderColorType = XGL_BORDER_COLOR_OPAQUE_WHITE, |
| 720 | }; |
Courtney Goeltzenleuchter | 6836939 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 721 | |
| 722 | assert(loadTexture(tex_files[i], NULL, NULL, &tex_width, &tex_height)); |
| 723 | |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 724 | const XGL_IMAGE_CREATE_INFO image = { |
| 725 | .sType = XGL_STRUCTURE_TYPE_IMAGE_CREATE_INFO, |
| 726 | .pNext = NULL, |
| 727 | .imageType = XGL_IMAGE_2D, |
| 728 | .format = tex_format, |
| 729 | .extent = { tex_width, tex_height, 1 }, |
| 730 | .mipLevels = 1, |
| 731 | .arraySize = 1, |
| 732 | .samples = 1, |
| 733 | .tiling = XGL_LINEAR_TILING, |
| 734 | .usage = XGL_IMAGE_USAGE_SHADER_ACCESS_READ_BIT, |
| 735 | .flags = 0, |
| 736 | }; |
Jon Ashburn | ae7c21c | 2015-01-19 15:00:26 -0700 | [diff] [blame] | 737 | XGL_MEMORY_ALLOC_IMAGE_INFO img_alloc = { |
| 738 | .sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_IMAGE_INFO, |
| 739 | .pNext = NULL, |
| 740 | }; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 741 | XGL_MEMORY_ALLOC_INFO mem_alloc = { |
| 742 | .sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_INFO, |
Jon Ashburn | ae7c21c | 2015-01-19 15:00:26 -0700 | [diff] [blame] | 743 | .pNext = &img_alloc, |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 744 | .allocationSize = 0, |
| 745 | .alignment = 0, |
Jon Ashburn | c483a8c | 2015-01-20 13:55:32 -0700 | [diff] [blame^] | 746 | .memProps = XGL_MEMORY_PROPERTY_GPU_ONLY, |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 747 | .flags = 0, |
| 748 | .heapCount = 0, |
Tony Barbour | 29645d0 | 2015-01-16 14:27:35 -0700 | [diff] [blame] | 749 | .pHeaps = 0, |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 750 | .memPriority = XGL_MEMORY_PRIORITY_NORMAL, |
| 751 | }; |
| 752 | XGL_IMAGE_VIEW_CREATE_INFO view = { |
| 753 | .sType = XGL_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO, |
| 754 | .pNext = NULL, |
| 755 | .image = XGL_NULL_HANDLE, |
| 756 | .viewType = XGL_IMAGE_VIEW_2D, |
| 757 | .format = image.format, |
| 758 | .channels = { XGL_CHANNEL_SWIZZLE_R, |
| 759 | XGL_CHANNEL_SWIZZLE_G, |
| 760 | XGL_CHANNEL_SWIZZLE_B, |
| 761 | XGL_CHANNEL_SWIZZLE_A, }, |
| 762 | .subresourceRange = { XGL_IMAGE_ASPECT_COLOR, 0, 1, 0, 1 }, |
| 763 | .minLod = 0.0f, |
| 764 | }; |
Jon Ashburn | b2a6665 | 2015-01-16 09:37:43 -0700 | [diff] [blame] | 765 | |
| 766 | XGL_MEMORY_REQUIREMENTS *mem_reqs; |
Jon Ashburn | bd75a81 | 2014-11-21 13:29:30 -0700 | [diff] [blame] | 767 | XGL_SIZE mem_reqs_size = sizeof(XGL_MEMORY_REQUIREMENTS); |
Jon Ashburn | ae7c21c | 2015-01-19 15:00:26 -0700 | [diff] [blame] | 768 | XGL_IMAGE_MEMORY_REQUIREMENTS img_reqs; |
| 769 | XGL_SIZE img_reqs_size = sizeof(XGL_IMAGE_MEMORY_REQUIREMENTS); |
Jon Ashburn | b2a6665 | 2015-01-16 09:37:43 -0700 | [diff] [blame] | 770 | XGL_UINT num_allocations = 0; |
| 771 | XGL_SIZE num_alloc_size = sizeof(num_allocations); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 772 | |
| 773 | /* create sampler */ |
| 774 | err = xglCreateSampler(demo->device, &sampler, |
| 775 | &demo->textures[i].sampler); |
| 776 | assert(!err); |
| 777 | |
| 778 | /* create image */ |
| 779 | err = xglCreateImage(demo->device, &image, |
| 780 | &demo->textures[i].image); |
| 781 | assert(!err); |
| 782 | |
| 783 | err = xglGetObjectInfo(demo->textures[i].image, |
Jon Ashburn | b2a6665 | 2015-01-16 09:37:43 -0700 | [diff] [blame] | 784 | XGL_INFO_TYPE_MEMORY_ALLOCATION_COUNT, |
| 785 | &num_alloc_size, &num_allocations); |
| 786 | assert(!err && num_alloc_size == sizeof(num_allocations)); |
| 787 | mem_reqs = malloc(num_allocations * sizeof(XGL_MEMORY_REQUIREMENTS)); |
| 788 | demo->textures[i].mem = malloc(num_allocations * sizeof(XGL_GPU_MEMORY)); |
| 789 | demo->textures[i].num_mem = num_allocations; |
| 790 | err = xglGetObjectInfo(demo->textures[i].image, |
| 791 | XGL_INFO_TYPE_MEMORY_REQUIREMENTS, |
| 792 | &mem_reqs_size, mem_reqs); |
| 793 | assert(!err && mem_reqs_size == num_allocations * sizeof(XGL_MEMORY_REQUIREMENTS)); |
Jon Ashburn | ae7c21c | 2015-01-19 15:00:26 -0700 | [diff] [blame] | 794 | err = xglGetObjectInfo(demo->textures[i].image, |
| 795 | XGL_INFO_TYPE_IMAGE_MEMORY_REQUIREMENTS, |
| 796 | &img_reqs_size, &img_reqs); |
| 797 | assert(!err && img_reqs_size == sizeof(XGL_IMAGE_MEMORY_REQUIREMENTS)); |
| 798 | img_alloc.usage = img_reqs.usage; |
| 799 | img_alloc.formatClass = img_reqs.formatClass; |
| 800 | img_alloc.samples = img_reqs.samples; |
Jon Ashburn | b2a6665 | 2015-01-16 09:37:43 -0700 | [diff] [blame] | 801 | for (XGL_UINT j = 0; j < num_allocations; j ++) { |
| 802 | mem_alloc.allocationSize = mem_reqs[j].size; |
| 803 | mem_alloc.alignment = mem_reqs[j].alignment; |
| 804 | mem_alloc.heapCount = mem_reqs[j].heapCount; |
| 805 | XGL_UINT heapInfo[mem_reqs[j].heapCount]; |
| 806 | mem_alloc.pHeaps = (const XGL_UINT *)heapInfo; |
| 807 | memcpy(heapInfo, mem_reqs[j].pHeaps, |
| 808 | sizeof(mem_reqs[j].pHeaps[0]) * mem_reqs[j].heapCount); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 809 | |
Jon Ashburn | b2a6665 | 2015-01-16 09:37:43 -0700 | [diff] [blame] | 810 | /* allocate memory */ |
| 811 | err = xglAllocMemory(demo->device, &mem_alloc, |
| 812 | &(demo->textures[i].mem[j])); |
| 813 | assert(!err); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 814 | |
Jon Ashburn | b2a6665 | 2015-01-16 09:37:43 -0700 | [diff] [blame] | 815 | /* bind memory */ |
| 816 | err = xglBindObjectMemory(demo->textures[i].image, j, |
| 817 | demo->textures[i].mem[j], 0); |
| 818 | assert(!err); |
| 819 | } |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 820 | |
| 821 | /* create image view */ |
| 822 | view.image = demo->textures[i].image; |
| 823 | err = xglCreateImageView(demo->device, &view, |
| 824 | &demo->textures[i].view); |
| 825 | assert(!err); |
| 826 | } |
| 827 | |
| 828 | for (i = 0; i < DEMO_TEXTURE_COUNT; i++) { |
| 829 | const XGL_IMAGE_SUBRESOURCE subres = { |
| 830 | .aspect = XGL_IMAGE_ASPECT_COLOR, |
| 831 | .mipLevel = 0, |
| 832 | .arraySlice = 0, |
| 833 | }; |
| 834 | XGL_SUBRESOURCE_LAYOUT layout; |
Chia-I Wu | 170de77 | 2015-01-05 16:27:42 +0800 | [diff] [blame] | 835 | XGL_SIZE layout_size = sizeof(layout); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 836 | XGL_VOID *data; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 837 | |
| 838 | err = xglGetImageSubresourceInfo(demo->textures[i].image, &subres, |
| 839 | XGL_INFO_TYPE_SUBRESOURCE_LAYOUT, &layout_size, &layout); |
| 840 | assert(!err && layout_size == sizeof(layout)); |
Jon Ashburn | b2a6665 | 2015-01-16 09:37:43 -0700 | [diff] [blame] | 841 | assert(demo->textures[i].num_mem == 1); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 842 | |
Jon Ashburn | b2a6665 | 2015-01-16 09:37:43 -0700 | [diff] [blame] | 843 | err = xglMapMemory(demo->textures[i].mem[0], 0, &data); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 844 | assert(!err); |
| 845 | |
Courtney Goeltzenleuchter | 6836939 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 846 | loadTexture(tex_files[i], data, &layout, &tex_width, &tex_height); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 847 | |
Jon Ashburn | b2a6665 | 2015-01-16 09:37:43 -0700 | [diff] [blame] | 848 | err = xglUnmapMemory(demo->textures[i].mem[0]); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 849 | assert(!err); |
| 850 | } |
| 851 | } |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 852 | |
Courtney Goeltzenleuchter | 4984d15 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 853 | void demo_prepare_cube_data_buffer(struct demo *demo) |
| 854 | { |
Chia-I Wu | 8ea21c7 | 2015-01-01 07:55:04 +0800 | [diff] [blame] | 855 | XGL_BUFFER_CREATE_INFO buf_info; |
| 856 | XGL_BUFFER_VIEW_CREATE_INFO view_info; |
Jon Ashburn | ae7c21c | 2015-01-19 15:00:26 -0700 | [diff] [blame] | 857 | XGL_MEMORY_ALLOC_BUFFER_INFO buf_alloc = { |
| 858 | .sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_BUFFER_INFO, |
| 859 | .pNext = NULL, |
| 860 | }; |
| 861 | XGL_MEMORY_ALLOC_INFO alloc_info = { |
| 862 | .sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_INFO, |
| 863 | .pNext = &buf_alloc, |
| 864 | .allocationSize = 0, |
| 865 | .alignment = 0, |
Jon Ashburn | c483a8c | 2015-01-20 13:55:32 -0700 | [diff] [blame^] | 866 | .memProps = XGL_MEMORY_PROPERTY_CPU_VISIBLE_BIT, |
Jon Ashburn | ae7c21c | 2015-01-19 15:00:26 -0700 | [diff] [blame] | 867 | .flags = 0, |
| 868 | .heapCount = 0, |
| 869 | .memPriority = XGL_MEMORY_PRIORITY_NORMAL, |
| 870 | }; |
| 871 | XGL_MEMORY_REQUIREMENTS *mem_reqs; |
Chia-I Wu | 8ea21c7 | 2015-01-01 07:55:04 +0800 | [diff] [blame] | 872 | XGL_SIZE mem_reqs_size = sizeof(XGL_MEMORY_REQUIREMENTS); |
Jon Ashburn | ae7c21c | 2015-01-19 15:00:26 -0700 | [diff] [blame] | 873 | XGL_BUFFER_MEMORY_REQUIREMENTS buf_reqs; |
| 874 | XGL_SIZE buf_reqs_size = sizeof(XGL_BUFFER_MEMORY_REQUIREMENTS); |
| 875 | XGL_UINT num_allocations = 0; |
| 876 | XGL_SIZE num_alloc_size = sizeof(num_allocations); |
Courtney Goeltzenleuchter | 4984d15 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 877 | XGL_UINT8 *pData; |
| 878 | int i; |
| 879 | mat4x4 MVP, VP; |
Courtney Goeltzenleuchter | 4984d15 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 880 | XGL_RESULT err; |
| 881 | struct xgltexcube_vs_uniform data; |
| 882 | |
Courtney Goeltzenleuchter | 4984d15 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 883 | mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix); |
Courtney Goeltzenleuchter | 4984d15 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 884 | mat4x4_mul(MVP, VP, demo->model_matrix); |
| 885 | memcpy(data.mvp, MVP, sizeof(MVP)); |
Courtney Goeltzenleuchter | 8879d3a | 2014-10-29 08:29:35 -0600 | [diff] [blame] | 886 | // dumpMatrix("MVP", MVP); |
Courtney Goeltzenleuchter | 4984d15 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 887 | |
| 888 | for (i=0; i<12*3; i++) { |
| 889 | data.position[i][0] = g_vertex_buffer_data[i*3]; |
| 890 | data.position[i][1] = g_vertex_buffer_data[i*3+1]; |
| 891 | data.position[i][2] = g_vertex_buffer_data[i*3+2]; |
| 892 | data.position[i][3] = 1.0f; |
| 893 | data.attr[i][0] = g_uv_buffer_data[2*i]; |
| 894 | data.attr[i][1] = g_uv_buffer_data[2*i + 1]; |
| 895 | data.attr[i][2] = 0; |
| 896 | data.attr[i][3] = 0; |
| 897 | } |
| 898 | |
Chia-I Wu | 8ea21c7 | 2015-01-01 07:55:04 +0800 | [diff] [blame] | 899 | memset(&buf_info, 0, sizeof(buf_info)); |
| 900 | buf_info.sType = XGL_STRUCTURE_TYPE_BUFFER_CREATE_INFO; |
| 901 | buf_info.size = sizeof(data); |
| 902 | buf_info.usage = XGL_BUFFER_USAGE_UNIFORM_READ_BIT; |
| 903 | err = xglCreateBuffer(demo->device, &buf_info, &demo->uniform_data.buf); |
| 904 | assert(!err); |
| 905 | |
| 906 | err = xglGetObjectInfo(demo->uniform_data.buf, |
Jon Ashburn | ae7c21c | 2015-01-19 15:00:26 -0700 | [diff] [blame] | 907 | XGL_INFO_TYPE_MEMORY_ALLOCATION_COUNT, |
| 908 | &num_alloc_size, &num_allocations); |
| 909 | assert(!err && num_alloc_size == sizeof(num_allocations)); |
| 910 | mem_reqs = malloc(num_allocations * sizeof(XGL_MEMORY_REQUIREMENTS)); |
| 911 | demo->uniform_data.mem = malloc(num_allocations * sizeof(XGL_GPU_MEMORY)); |
| 912 | demo->uniform_data.num_mem = num_allocations; |
| 913 | err = xglGetObjectInfo(demo->uniform_data.buf, |
Chia-I Wu | 8ea21c7 | 2015-01-01 07:55:04 +0800 | [diff] [blame] | 914 | XGL_INFO_TYPE_MEMORY_REQUIREMENTS, |
Jon Ashburn | ae7c21c | 2015-01-19 15:00:26 -0700 | [diff] [blame] | 915 | &mem_reqs_size, mem_reqs); |
| 916 | assert(!err && mem_reqs_size == num_allocations * sizeof(*mem_reqs)); |
| 917 | err = xglGetObjectInfo(demo->uniform_data.buf, |
| 918 | XGL_INFO_TYPE_BUFFER_MEMORY_REQUIREMENTS, |
| 919 | &buf_reqs_size, &buf_reqs); |
| 920 | assert(!err && buf_reqs_size == sizeof(XGL_BUFFER_MEMORY_REQUIREMENTS)); |
| 921 | buf_alloc.usage = buf_reqs.usage; |
| 922 | for (XGL_UINT i = 0; i < num_allocations; i ++) { |
| 923 | alloc_info.allocationSize = mem_reqs[i].size; |
| 924 | alloc_info.alignment = mem_reqs[i].alignment; |
| 925 | alloc_info.heapCount = mem_reqs[i].heapCount; |
| 926 | XGL_UINT heapInfo[mem_reqs[i].heapCount]; |
| 927 | alloc_info.pHeaps = (const XGL_UINT *)heapInfo; |
| 928 | memcpy(heapInfo, mem_reqs[i].pHeaps, |
| 929 | sizeof(mem_reqs[i].pHeaps[0]) * mem_reqs[i].heapCount); |
Chia-I Wu | 8ea21c7 | 2015-01-01 07:55:04 +0800 | [diff] [blame] | 930 | |
Jon Ashburn | ae7c21c | 2015-01-19 15:00:26 -0700 | [diff] [blame] | 931 | err = xglAllocMemory(demo->device, &alloc_info, &(demo->uniform_data.mem[i])); |
| 932 | assert(!err); |
Courtney Goeltzenleuchter | 4984d15 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 933 | |
Jon Ashburn | ae7c21c | 2015-01-19 15:00:26 -0700 | [diff] [blame] | 934 | err = xglMapMemory(demo->uniform_data.mem[i], 0, (XGL_VOID **) &pData); |
| 935 | assert(!err); |
Courtney Goeltzenleuchter | 4984d15 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 936 | |
Jon Ashburn | ae7c21c | 2015-01-19 15:00:26 -0700 | [diff] [blame] | 937 | memcpy(pData, &data, alloc_info.allocationSize); |
Courtney Goeltzenleuchter | 4984d15 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 938 | |
Jon Ashburn | ae7c21c | 2015-01-19 15:00:26 -0700 | [diff] [blame] | 939 | err = xglUnmapMemory(demo->uniform_data.mem[i]); |
| 940 | assert(!err); |
Courtney Goeltzenleuchter | 4984d15 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 941 | |
Jon Ashburn | ae7c21c | 2015-01-19 15:00:26 -0700 | [diff] [blame] | 942 | err = xglBindObjectMemory(demo->uniform_data.buf, i, |
| 943 | demo->uniform_data.mem[i], 0); |
| 944 | assert(!err); |
| 945 | } |
Chia-I Wu | 8ea21c7 | 2015-01-01 07:55:04 +0800 | [diff] [blame] | 946 | |
| 947 | memset(&view_info, 0, sizeof(view_info)); |
| 948 | view_info.sType = XGL_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO; |
| 949 | view_info.buffer = demo->uniform_data.buf; |
Chia-I Wu | 378caea | 2015-01-16 22:31:25 +0800 | [diff] [blame] | 950 | view_info.viewType = XGL_BUFFER_VIEW_RAW; |
Chia-I Wu | 8ea21c7 | 2015-01-01 07:55:04 +0800 | [diff] [blame] | 951 | view_info.offset = 0; |
| 952 | view_info.range = sizeof(data); |
| 953 | |
| 954 | err = xglCreateBufferView(demo->device, &view_info, &demo->uniform_data.view); |
| 955 | assert(!err); |
| 956 | |
| 957 | demo->uniform_data.attach.sType = XGL_STRUCTURE_TYPE_BUFFER_VIEW_ATTACH_INFO; |
| 958 | demo->uniform_data.attach.view = demo->uniform_data.view; |
Courtney Goeltzenleuchter | 4984d15 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 959 | } |
| 960 | |
Chia-I Wu | 6a3c897 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 961 | static void demo_prepare_descriptor_layout(struct demo *demo) |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 962 | { |
Chia-I Wu | 6a3c897 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 963 | const XGL_DESCRIPTOR_SET_LAYOUT_CREATE_INFO descriptor_layout_vs = { |
| 964 | .sType = XGL_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO, |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 965 | .pNext = NULL, |
Chia-I Wu | 6a3c897 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 966 | .descriptorType = XGL_DESCRIPTOR_TYPE_UNIFORM_BUFFER, |
| 967 | .count = 1, |
| 968 | .stageFlags = XGL_SHADER_STAGE_FLAGS_VERTEX_BIT, |
| 969 | .immutableSampler = XGL_NULL_HANDLE, |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 970 | }; |
Chia-I Wu | 6a3c897 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 971 | const XGL_DESCRIPTOR_SET_LAYOUT_CREATE_INFO descriptor_layout_fs = { |
| 972 | .sType = XGL_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO, |
| 973 | .pNext = NULL, |
| 974 | .descriptorType = XGL_DESCRIPTOR_TYPE_SAMPLER_TEXTURE, |
| 975 | .count = DEMO_TEXTURE_COUNT, |
| 976 | .stageFlags = XGL_SHADER_STAGE_FLAGS_FRAGMENT_BIT, |
| 977 | .immutableSampler = XGL_NULL_HANDLE, |
| 978 | }; |
| 979 | const uint32_t bind_point = 0; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 980 | XGL_RESULT err; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 981 | |
Chia-I Wu | 6a3c897 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 982 | err = xglCreateDescriptorSetLayout(demo->device, |
| 983 | XGL_SHADER_STAGE_FLAGS_VERTEX_BIT, &bind_point, |
| 984 | XGL_NULL_HANDLE, &descriptor_layout_vs, |
| 985 | &demo->desc_layout_vs); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 986 | assert(!err); |
| 987 | |
Chia-I Wu | 6a3c897 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 988 | err = xglCreateDescriptorSetLayout(demo->device, |
| 989 | XGL_SHADER_STAGE_FLAGS_FRAGMENT_BIT, &bind_point, |
| 990 | demo->desc_layout_vs, &descriptor_layout_fs, |
| 991 | &demo->desc_layout_fs); |
| 992 | assert(!err); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 993 | |
Chia-I Wu | 6a3c897 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 994 | demo->desc_layout_last = &demo->desc_layout_fs; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 995 | } |
| 996 | |
| 997 | static XGL_SHADER demo_prepare_shader(struct demo *demo, |
| 998 | XGL_PIPELINE_SHADER_STAGE stage, |
| 999 | const void *code, |
| 1000 | XGL_SIZE size) |
| 1001 | { |
| 1002 | XGL_SHADER_CREATE_INFO createInfo; |
| 1003 | XGL_SHADER shader; |
| 1004 | XGL_RESULT err; |
| 1005 | |
Courtney Goeltzenleuchter | 97db1c1 | 2014-10-30 15:14:16 -0600 | [diff] [blame] | 1006 | |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1007 | createInfo.sType = XGL_STRUCTURE_TYPE_SHADER_CREATE_INFO; |
| 1008 | createInfo.pNext = NULL; |
| 1009 | |
Courtney Goeltzenleuchter | 97db1c1 | 2014-10-30 15:14:16 -0600 | [diff] [blame] | 1010 | #ifdef EXTERNAL_BIL |
| 1011 | createInfo.codeSize = size; |
| 1012 | createInfo.pCode = code; |
Courtney Goeltzenleuchter | 6836939 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 1013 | createInfo.flags = 0; |
| 1014 | |
Courtney Goeltzenleuchter | 6836939 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 1015 | err = xglCreateShader(demo->device, &createInfo, &shader); |
| 1016 | if (err) { |
| 1017 | free((void *) createInfo.pCode); |
| 1018 | } |
| 1019 | #else |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1020 | // Create fake BIL structure to feed GLSL |
| 1021 | // to the driver "under the covers" |
| 1022 | createInfo.codeSize = 3 * sizeof(uint32_t) + size + 1; |
| 1023 | createInfo.pCode = malloc(createInfo.codeSize); |
| 1024 | createInfo.flags = 0; |
| 1025 | |
| 1026 | /* try version 0 first: XGL_PIPELINE_SHADER_STAGE followed by GLSL */ |
| 1027 | ((uint32_t *) createInfo.pCode)[0] = ICD_BIL_MAGIC; |
| 1028 | ((uint32_t *) createInfo.pCode)[1] = 0; |
| 1029 | ((uint32_t *) createInfo.pCode)[2] = stage; |
| 1030 | memcpy(((uint32_t *) createInfo.pCode + 3), code, size + 1); |
| 1031 | |
| 1032 | err = xglCreateShader(demo->device, &createInfo, &shader); |
| 1033 | if (err) { |
| 1034 | free((void *) createInfo.pCode); |
Courtney Goeltzenleuchter | fc3b953 | 2014-10-28 10:32:57 -0600 | [diff] [blame] | 1035 | return NULL; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1036 | } |
Courtney Goeltzenleuchter | 6836939 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 1037 | #endif |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1038 | |
| 1039 | return shader; |
| 1040 | } |
| 1041 | |
Courtney Goeltzenleuchter | 97db1c1 | 2014-10-30 15:14:16 -0600 | [diff] [blame] | 1042 | char *demo_read_bil(const char *filename, XGL_SIZE *psize) |
| 1043 | { |
| 1044 | long int size; |
| 1045 | void *shader_code; |
| 1046 | |
| 1047 | FILE *fp = fopen(filename, "rb"); |
| 1048 | if (!fp) return NULL; |
| 1049 | |
| 1050 | fseek(fp, 0L, SEEK_END); |
| 1051 | size = ftell(fp); |
| 1052 | |
| 1053 | fseek(fp, 0L, SEEK_SET); |
| 1054 | |
| 1055 | shader_code = malloc(size); |
| 1056 | fread(shader_code, size, 1, fp); |
| 1057 | |
| 1058 | *psize = size; |
| 1059 | |
| 1060 | return shader_code; |
| 1061 | } |
| 1062 | |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1063 | static XGL_SHADER demo_prepare_vs(struct demo *demo) |
| 1064 | { |
Courtney Goeltzenleuchter | 97db1c1 | 2014-10-30 15:14:16 -0600 | [diff] [blame] | 1065 | #ifdef EXTERNAL_BIL |
| 1066 | void *vertShaderCode; |
| 1067 | XGL_SIZE size; |
| 1068 | |
| 1069 | vertShaderCode = demo_read_bil("cube-vert.bil", &size); |
| 1070 | |
| 1071 | return demo_prepare_shader(demo, XGL_SHADER_STAGE_VERTEX, |
| 1072 | vertShaderCode, size); |
| 1073 | #else |
Courtney Goeltzenleuchter | 8879d3a | 2014-10-29 08:29:35 -0600 | [diff] [blame] | 1074 | static const char *vertShaderText = |
Courtney Goeltzenleuchter | 4984d15 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 1075 | "#version 140\n" |
| 1076 | "#extension GL_ARB_separate_shader_objects : enable\n" |
| 1077 | "#extension GL_ARB_shading_language_420pack : enable\n" |
| 1078 | "\n" |
| 1079 | "layout(binding = 0) uniform buf {\n" |
| 1080 | " mat4 MVP;\n" |
| 1081 | " vec4 position[12*3];\n" |
| 1082 | " vec4 attr[12*3];\n" |
| 1083 | "} ubuf;\n" |
| 1084 | "\n" |
| 1085 | "layout (location = 0) out vec4 texcoord;\n" |
Courtney Goeltzenleuchter | 4984d15 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 1086 | "\n" |
| 1087 | "void main() \n" |
| 1088 | "{\n" |
| 1089 | " texcoord = ubuf.attr[gl_VertexID];\n" |
Courtney Goeltzenleuchter | 4984d15 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 1090 | " gl_Position = ubuf.MVP * ubuf.position[gl_VertexID];\n" |
| 1091 | "}\n"; |
Courtney Goeltzenleuchter | fc3b953 | 2014-10-28 10:32:57 -0600 | [diff] [blame] | 1092 | |
Courtney Goeltzenleuchter | 8879d3a | 2014-10-29 08:29:35 -0600 | [diff] [blame] | 1093 | return demo_prepare_shader(demo, XGL_SHADER_STAGE_VERTEX, |
| 1094 | (const void *) vertShaderText, |
| 1095 | strlen(vertShaderText)); |
Courtney Goeltzenleuchter | 97db1c1 | 2014-10-30 15:14:16 -0600 | [diff] [blame] | 1096 | #endif |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1097 | } |
| 1098 | |
| 1099 | static XGL_SHADER demo_prepare_fs(struct demo *demo) |
| 1100 | { |
Courtney Goeltzenleuchter | 97db1c1 | 2014-10-30 15:14:16 -0600 | [diff] [blame] | 1101 | #ifdef EXTERNAL_BIL |
| 1102 | void *fragShaderCode; |
| 1103 | XGL_SIZE size; |
| 1104 | |
| 1105 | fragShaderCode = demo_read_bil("cube-frag.bil", &size); |
| 1106 | |
| 1107 | return demo_prepare_shader(demo, XGL_SHADER_STAGE_FRAGMENT, |
| 1108 | fragShaderCode, size); |
| 1109 | #else |
Courtney Goeltzenleuchter | 8879d3a | 2014-10-29 08:29:35 -0600 | [diff] [blame] | 1110 | static const char *fragShaderText = |
Courtney Goeltzenleuchter | 4984d15 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 1111 | "#version 140\n" |
| 1112 | "#extension GL_ARB_separate_shader_objects : enable\n" |
| 1113 | "#extension GL_ARB_shading_language_420pack : enable\n" |
Courtney Goeltzenleuchter | 97db1c1 | 2014-10-30 15:14:16 -0600 | [diff] [blame] | 1114 | "layout (binding = 0) uniform sampler2D tex;\n" |
Courtney Goeltzenleuchter | 4984d15 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 1115 | "\n" |
| 1116 | "layout (location = 0) in vec4 texcoord;\n" |
| 1117 | "void main() {\n" |
| 1118 | " gl_FragColor = texture(tex, texcoord.xy);\n" |
| 1119 | "}\n"; |
Courtney Goeltzenleuchter | fc3b953 | 2014-10-28 10:32:57 -0600 | [diff] [blame] | 1120 | |
Courtney Goeltzenleuchter | 8879d3a | 2014-10-29 08:29:35 -0600 | [diff] [blame] | 1121 | return demo_prepare_shader(demo, XGL_SHADER_STAGE_FRAGMENT, |
| 1122 | (const void *) fragShaderText, |
| 1123 | strlen(fragShaderText)); |
Courtney Goeltzenleuchter | 97db1c1 | 2014-10-30 15:14:16 -0600 | [diff] [blame] | 1124 | #endif |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1125 | } |
| 1126 | |
| 1127 | static void demo_prepare_pipeline(struct demo *demo) |
| 1128 | { |
| 1129 | XGL_GRAPHICS_PIPELINE_CREATE_INFO pipeline; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1130 | XGL_PIPELINE_IA_STATE_CREATE_INFO ia; |
| 1131 | XGL_PIPELINE_RS_STATE_CREATE_INFO rs; |
Tony Barbour | 29645d0 | 2015-01-16 14:27:35 -0700 | [diff] [blame] | 1132 | XGL_PIPELINE_CB_STATE_CREATE_INFO cb; |
| 1133 | XGL_PIPELINE_DS_STATE_CREATE_INFO ds; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1134 | XGL_PIPELINE_SHADER_STAGE_CREATE_INFO vs; |
| 1135 | XGL_PIPELINE_SHADER_STAGE_CREATE_INFO fs; |
Tony Barbour | 29645d0 | 2015-01-16 14:27:35 -0700 | [diff] [blame] | 1136 | XGL_PIPELINE_VP_STATE_CREATE_INFO vp; |
| 1137 | XGL_PIPELINE_MS_STATE_CREATE_INFO ms; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1138 | XGL_RESULT err; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1139 | |
| 1140 | memset(&pipeline, 0, sizeof(pipeline)); |
| 1141 | pipeline.sType = XGL_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO; |
Chia-I Wu | 6a3c897 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 1142 | pipeline.lastSetLayout = *demo->desc_layout_last; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1143 | |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1144 | memset(&ia, 0, sizeof(ia)); |
| 1145 | ia.sType = XGL_STRUCTURE_TYPE_PIPELINE_IA_STATE_CREATE_INFO; |
| 1146 | ia.topology = XGL_TOPOLOGY_TRIANGLE_LIST; |
| 1147 | |
| 1148 | memset(&rs, 0, sizeof(rs)); |
| 1149 | rs.sType = XGL_STRUCTURE_TYPE_PIPELINE_RS_STATE_CREATE_INFO; |
Tony Barbour | 29645d0 | 2015-01-16 14:27:35 -0700 | [diff] [blame] | 1150 | rs.fillMode = XGL_FILL_SOLID; |
| 1151 | rs.cullMode = XGL_CULL_NONE; |
| 1152 | rs.frontFace = XGL_FRONT_FACE_CCW; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1153 | |
| 1154 | memset(&cb, 0, sizeof(cb)); |
| 1155 | cb.sType = XGL_STRUCTURE_TYPE_PIPELINE_CB_STATE_CREATE_INFO; |
Tony Barbour | 29645d0 | 2015-01-16 14:27:35 -0700 | [diff] [blame] | 1156 | XGL_PIPELINE_CB_ATTACHMENT_STATE att_state[1]; |
| 1157 | memset(att_state, 0, sizeof(att_state)); |
| 1158 | att_state[0].format = demo->format; |
| 1159 | att_state[0].channelWriteMask = 0xf; |
| 1160 | att_state[0].blendEnable = XGL_FALSE; |
| 1161 | cb.attachmentCount = 1; |
| 1162 | cb.pAttachments = att_state; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1163 | |
Tony Barbour | 29645d0 | 2015-01-16 14:27:35 -0700 | [diff] [blame] | 1164 | memset(&vp, 0, sizeof(vp)); |
| 1165 | vp.sType = XGL_STRUCTURE_TYPE_PIPELINE_VP_STATE_CREATE_INFO; |
| 1166 | vp.scissorEnable = XGL_FALSE; |
| 1167 | |
| 1168 | memset(&ds, 0, sizeof(ds)); |
| 1169 | ds.sType = XGL_STRUCTURE_TYPE_PIPELINE_DS_STATE_CREATE_INFO; |
| 1170 | ds.format = demo->depth.format; |
| 1171 | ds.depthTestEnable = XGL_TRUE; |
| 1172 | ds.depthWriteEnable = XGL_TRUE; |
| 1173 | ds.depthFunc = XGL_COMPARE_LESS_EQUAL; |
| 1174 | ds.depthBoundsEnable = XGL_FALSE; |
| 1175 | ds.back.stencilFailOp = XGL_STENCIL_OP_KEEP; |
| 1176 | ds.back.stencilPassOp = XGL_STENCIL_OP_KEEP; |
| 1177 | ds.back.stencilFunc = XGL_COMPARE_ALWAYS; |
| 1178 | ds.stencilTestEnable = XGL_FALSE; |
| 1179 | ds.front = ds.back; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1180 | |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1181 | memset(&vs, 0, sizeof(vs)); |
| 1182 | vs.sType = XGL_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO; |
| 1183 | vs.shader.stage = XGL_SHADER_STAGE_VERTEX; |
Courtney Goeltzenleuchter | 4984d15 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 1184 | vs.shader.shader = demo_prepare_vs(demo); |
Courtney Goeltzenleuchter | fc3b953 | 2014-10-28 10:32:57 -0600 | [diff] [blame] | 1185 | assert(vs.shader.shader != NULL); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1186 | |
| 1187 | memset(&fs, 0, sizeof(fs)); |
| 1188 | fs.sType = XGL_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO; |
| 1189 | fs.shader.stage = XGL_SHADER_STAGE_FRAGMENT; |
| 1190 | fs.shader.shader = demo_prepare_fs(demo); |
Courtney Goeltzenleuchter | fc3b953 | 2014-10-28 10:32:57 -0600 | [diff] [blame] | 1191 | assert(fs.shader.shader != NULL); |
Tony Barbour | 29645d0 | 2015-01-16 14:27:35 -0700 | [diff] [blame] | 1192 | |
| 1193 | memset(&ms, 0, sizeof(ms)); |
| 1194 | ms.sType = XGL_STRUCTURE_TYPE_PIPELINE_MS_STATE_CREATE_INFO; |
| 1195 | ms.sampleMask = 1; |
| 1196 | ms.multisampleEnable = XGL_FALSE; |
| 1197 | ms.samples = 1; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1198 | |
Courtney Goeltzenleuchter | 4984d15 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 1199 | pipeline.pNext = (const XGL_VOID *) &ia; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1200 | ia.pNext = (const XGL_VOID *) &rs; |
| 1201 | rs.pNext = (const XGL_VOID *) &cb; |
Tony Barbour | 29645d0 | 2015-01-16 14:27:35 -0700 | [diff] [blame] | 1202 | cb.pNext = (const XGL_VOID *) &ms; |
| 1203 | ms.pNext = (const XGL_VOID *) &vp; |
| 1204 | vp.pNext = (const XGL_VOID *) &ds; |
| 1205 | ds.pNext = (const XGL_VOID *) &vs; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1206 | vs.pNext = (const XGL_VOID *) &fs; |
| 1207 | |
| 1208 | err = xglCreateGraphicsPipeline(demo->device, &pipeline, &demo->pipeline); |
| 1209 | assert(!err); |
| 1210 | |
| 1211 | xglDestroyObject(vs.shader.shader); |
| 1212 | xglDestroyObject(fs.shader.shader); |
| 1213 | } |
| 1214 | |
| 1215 | static void demo_prepare_dynamic_states(struct demo *demo) |
| 1216 | { |
Tony Barbour | 29645d0 | 2015-01-16 14:27:35 -0700 | [diff] [blame] | 1217 | XGL_DYNAMIC_VP_STATE_CREATE_INFO viewport_create; |
| 1218 | XGL_DYNAMIC_RS_STATE_CREATE_INFO raster; |
| 1219 | XGL_DYNAMIC_CB_STATE_CREATE_INFO color_blend; |
| 1220 | XGL_DYNAMIC_DS_STATE_CREATE_INFO depth_stencil; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1221 | XGL_RESULT err; |
| 1222 | |
Tony Barbour | 29645d0 | 2015-01-16 14:27:35 -0700 | [diff] [blame] | 1223 | memset(&viewport_create, 0, sizeof(viewport_create)); |
| 1224 | viewport_create.sType = XGL_STRUCTURE_TYPE_DYNAMIC_VP_STATE_CREATE_INFO; |
| 1225 | viewport_create.viewportCount = 1; |
| 1226 | XGL_VIEWPORT viewport; |
| 1227 | viewport.height = (XGL_FLOAT) demo->height; |
| 1228 | viewport.width = (XGL_FLOAT) demo->width; |
| 1229 | viewport.minDepth = (XGL_FLOAT) 0.0f; |
| 1230 | viewport.maxDepth = (XGL_FLOAT) 1.0f; |
| 1231 | viewport_create.pViewports = &viewport; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1232 | |
| 1233 | memset(&raster, 0, sizeof(raster)); |
Tony Barbour | 29645d0 | 2015-01-16 14:27:35 -0700 | [diff] [blame] | 1234 | raster.sType = XGL_STRUCTURE_TYPE_DYNAMIC_RS_STATE_CREATE_INFO; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1235 | |
| 1236 | memset(&color_blend, 0, sizeof(color_blend)); |
Tony Barbour | 29645d0 | 2015-01-16 14:27:35 -0700 | [diff] [blame] | 1237 | color_blend.sType = XGL_STRUCTURE_TYPE_DYNAMIC_CB_STATE_CREATE_INFO; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1238 | |
| 1239 | memset(&depth_stencil, 0, sizeof(depth_stencil)); |
Tony Barbour | 29645d0 | 2015-01-16 14:27:35 -0700 | [diff] [blame] | 1240 | depth_stencil.sType = XGL_STRUCTURE_TYPE_DYNAMIC_DS_STATE_CREATE_INFO; |
| 1241 | depth_stencil.stencilBackRef = 0; |
| 1242 | depth_stencil.stencilFrontRef = 0; |
| 1243 | depth_stencil.stencilReadMask = 0xff; |
| 1244 | depth_stencil.stencilWriteMask = 0xff; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1245 | |
Tony Barbour | 29645d0 | 2015-01-16 14:27:35 -0700 | [diff] [blame] | 1246 | err = xglCreateDynamicViewportState(demo->device, &viewport_create, &demo->viewport); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1247 | assert(!err); |
| 1248 | |
Tony Barbour | 29645d0 | 2015-01-16 14:27:35 -0700 | [diff] [blame] | 1249 | err = xglCreateDynamicRasterState(demo->device, &raster, &demo->raster); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1250 | assert(!err); |
| 1251 | |
Tony Barbour | 29645d0 | 2015-01-16 14:27:35 -0700 | [diff] [blame] | 1252 | err = xglCreateDynamicColorBlendState(demo->device, |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1253 | &color_blend, &demo->color_blend); |
| 1254 | assert(!err); |
| 1255 | |
Tony Barbour | 29645d0 | 2015-01-16 14:27:35 -0700 | [diff] [blame] | 1256 | err = xglCreateDynamicDepthStencilState(demo->device, |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1257 | &depth_stencil, &demo->depth_stencil); |
| 1258 | assert(!err); |
| 1259 | } |
| 1260 | |
Chia-I Wu | 6a3c897 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 1261 | static void demo_prepare_descriptor_region(struct demo *demo) |
| 1262 | { |
| 1263 | const XGL_DESCRIPTOR_TYPE_COUNT type_counts[2] = { |
| 1264 | [0] = { |
| 1265 | .type = XGL_DESCRIPTOR_TYPE_UNIFORM_BUFFER, |
| 1266 | .count = 1, |
| 1267 | }, |
| 1268 | [1] = { |
| 1269 | .type = XGL_DESCRIPTOR_TYPE_SAMPLER_TEXTURE, |
| 1270 | .count = DEMO_TEXTURE_COUNT, |
| 1271 | }, |
| 1272 | }; |
| 1273 | const XGL_DESCRIPTOR_REGION_CREATE_INFO descriptor_region = { |
| 1274 | .sType = XGL_STRUCTURE_TYPE_DESCRIPTOR_REGION_CREATE_INFO, |
| 1275 | .pNext = NULL, |
| 1276 | .count = 2, |
| 1277 | .pTypeCount = type_counts, |
| 1278 | }; |
| 1279 | XGL_RESULT err; |
| 1280 | |
| 1281 | err = xglCreateDescriptorRegion(demo->device, |
| 1282 | XGL_DESCRIPTOR_REGION_USAGE_ONE_SHOT, 1, |
| 1283 | &descriptor_region, &demo->desc_region); |
| 1284 | assert(!err); |
| 1285 | } |
| 1286 | |
| 1287 | static void demo_prepare_descriptor_set(struct demo *demo) |
| 1288 | { |
| 1289 | const XGL_BUFFER_VIEW_ATTACH_INFO *view_info_vs = |
| 1290 | &demo->uniform_data.attach; |
| 1291 | XGL_IMAGE_VIEW_ATTACH_INFO view_info[DEMO_TEXTURE_COUNT]; |
| 1292 | XGL_SAMPLER_IMAGE_VIEW_INFO combined_info[DEMO_TEXTURE_COUNT]; |
| 1293 | XGL_UPDATE_SAMPLER_TEXTURES update_fs; |
| 1294 | XGL_UPDATE_BUFFERS update_vs; |
| 1295 | XGL_RESULT err; |
| 1296 | uint32_t count; |
| 1297 | XGL_UINT i; |
| 1298 | |
| 1299 | for (i = 0; i < DEMO_TEXTURE_COUNT; i++) { |
| 1300 | view_info[i].sType = XGL_STRUCTURE_TYPE_IMAGE_VIEW_ATTACH_INFO; |
| 1301 | view_info[i].pNext = NULL; |
| 1302 | view_info[i].view = demo->textures[i].view, |
| 1303 | view_info[i].layout = XGL_IMAGE_LAYOUT_GENERAL; |
| 1304 | |
| 1305 | combined_info[i].pSampler = demo->textures[i].sampler; |
| 1306 | combined_info[i].pImageView = &view_info[i]; |
| 1307 | } |
| 1308 | |
| 1309 | memset(&update_vs, 0, sizeof(update_vs)); |
| 1310 | update_vs.sType = XGL_STRUCTURE_TYPE_UPDATE_BUFFERS; |
| 1311 | update_vs.pNext = &update_fs; |
| 1312 | update_vs.descriptorType = XGL_DESCRIPTOR_TYPE_UNIFORM_BUFFER; |
| 1313 | update_vs.count = 1; |
| 1314 | update_vs.pBufferViews = &view_info_vs; |
| 1315 | |
| 1316 | memset(&update_fs, 0, sizeof(update_fs)); |
| 1317 | update_fs.sType = XGL_STRUCTURE_TYPE_UPDATE_SAMPLER_TEXTURES; |
| 1318 | update_fs.index = 1; |
| 1319 | update_fs.count = DEMO_TEXTURE_COUNT; |
| 1320 | update_fs.pSamplerImageViews = combined_info; |
| 1321 | |
| 1322 | err = xglAllocDescriptorSets(demo->desc_region, |
| 1323 | XGL_DESCRIPTOR_SET_USAGE_STATIC, |
| 1324 | 1, demo->desc_layout_last, |
| 1325 | &demo->desc_set, &count); |
| 1326 | assert(!err && count == 1); |
| 1327 | |
| 1328 | xglBeginDescriptorRegionUpdate(demo->device, |
| 1329 | XGL_DESCRIPTOR_UPDATE_MODE_FASTEST); |
| 1330 | |
| 1331 | xglClearDescriptorSets(demo->desc_region, 1, &demo->desc_set); |
| 1332 | xglUpdateDescriptors(demo->desc_set, &update_vs); |
| 1333 | |
| 1334 | xglEndDescriptorRegionUpdate(demo->device, demo->cmd); |
| 1335 | } |
| 1336 | |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1337 | static void demo_prepare(struct demo *demo) |
| 1338 | { |
| 1339 | const XGL_CMD_BUFFER_CREATE_INFO cmd = { |
| 1340 | .sType = XGL_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO, |
| 1341 | .pNext = NULL, |
| 1342 | .queueType = XGL_QUEUE_TYPE_GRAPHICS, |
| 1343 | .flags = 0, |
| 1344 | }; |
| 1345 | XGL_RESULT err; |
| 1346 | |
| 1347 | demo_prepare_buffers(demo); |
| 1348 | demo_prepare_depth(demo); |
| 1349 | demo_prepare_textures(demo); |
Courtney Goeltzenleuchter | 8879d3a | 2014-10-29 08:29:35 -0600 | [diff] [blame] | 1350 | demo_prepare_cube_data_buffer(demo); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1351 | |
Chia-I Wu | 6a3c897 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 1352 | demo_prepare_descriptor_layout(demo); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1353 | demo_prepare_pipeline(demo); |
| 1354 | demo_prepare_dynamic_states(demo); |
| 1355 | |
| 1356 | err = xglCreateCommandBuffer(demo->device, &cmd, &demo->cmd); |
| 1357 | assert(!err); |
Chia-I Wu | 6a3c897 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 1358 | |
| 1359 | demo_prepare_descriptor_region(demo); |
| 1360 | demo_prepare_descriptor_set(demo); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1361 | } |
| 1362 | |
| 1363 | static void demo_handle_event(struct demo *demo, |
| 1364 | const xcb_generic_event_t *event) |
| 1365 | { |
Courtney Goeltzenleuchter | 98cb2cb | 2014-11-06 14:27:52 -0700 | [diff] [blame] | 1366 | u_int8_t event_code = event->response_type & 0x7f; |
| 1367 | switch (event_code) { |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1368 | case XCB_EXPOSE: |
Courtney Goeltzenleuchter | c465d6c | 2014-11-18 11:28:09 -0700 | [diff] [blame] | 1369 | // TODO: Resize window |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1370 | break; |
Courtney Goeltzenleuchter | 98cb2cb | 2014-11-06 14:27:52 -0700 | [diff] [blame] | 1371 | case XCB_CLIENT_MESSAGE: |
| 1372 | if((*(xcb_client_message_event_t*)event).data.data32[0] == |
| 1373 | (*demo->atom_wm_delete_window).atom) { |
| 1374 | demo->quit = true; |
| 1375 | } |
| 1376 | break; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1377 | case XCB_KEY_RELEASE: |
| 1378 | { |
| 1379 | const xcb_key_release_event_t *key = |
| 1380 | (const xcb_key_release_event_t *) event; |
| 1381 | |
Courtney Goeltzenleuchter | fc3b953 | 2014-10-28 10:32:57 -0600 | [diff] [blame] | 1382 | switch (key->detail) { |
| 1383 | case 0x9: // Escape |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1384 | demo->quit = true; |
Courtney Goeltzenleuchter | fc3b953 | 2014-10-28 10:32:57 -0600 | [diff] [blame] | 1385 | break; |
Courtney Goeltzenleuchter | 4984d15 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 1386 | case 0x71: // left arrow key |
Courtney Goeltzenleuchter | 8879d3a | 2014-10-29 08:29:35 -0600 | [diff] [blame] | 1387 | demo->spin_angle += demo->spin_increment; |
Courtney Goeltzenleuchter | 4984d15 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 1388 | break; |
Courtney Goeltzenleuchter | fc3b953 | 2014-10-28 10:32:57 -0600 | [diff] [blame] | 1389 | case 0x72: // right arrow key |
Courtney Goeltzenleuchter | 8879d3a | 2014-10-29 08:29:35 -0600 | [diff] [blame] | 1390 | demo->spin_angle -= demo->spin_increment; |
Courtney Goeltzenleuchter | fc3b953 | 2014-10-28 10:32:57 -0600 | [diff] [blame] | 1391 | break; |
Courtney Goeltzenleuchter | 8879d3a | 2014-10-29 08:29:35 -0600 | [diff] [blame] | 1392 | case 0x41: |
| 1393 | demo->pause = !demo->pause; |
| 1394 | break; |
Courtney Goeltzenleuchter | fc3b953 | 2014-10-28 10:32:57 -0600 | [diff] [blame] | 1395 | } |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1396 | } |
| 1397 | break; |
| 1398 | default: |
| 1399 | break; |
| 1400 | } |
| 1401 | } |
| 1402 | |
| 1403 | static void demo_run(struct demo *demo) |
| 1404 | { |
| 1405 | xcb_flush(demo->connection); |
| 1406 | |
| 1407 | while (!demo->quit) { |
| 1408 | xcb_generic_event_t *event; |
| 1409 | |
Courtney Goeltzenleuchter | c465d6c | 2014-11-18 11:28:09 -0700 | [diff] [blame] | 1410 | if (demo->pause) { |
| 1411 | event = xcb_wait_for_event(demo->connection); |
| 1412 | } else { |
| 1413 | event = xcb_poll_for_event(demo->connection); |
| 1414 | } |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1415 | if (event) { |
| 1416 | demo_handle_event(demo, event); |
| 1417 | free(event); |
Courtney Goeltzenleuchter | 9808555 | 2014-11-10 11:13:13 -0700 | [diff] [blame] | 1418 | } |
Courtney Goeltzenleuchter | c465d6c | 2014-11-18 11:28:09 -0700 | [diff] [blame] | 1419 | |
| 1420 | // Wait for work to finish before updating MVP. |
| 1421 | xglDeviceWaitIdle(demo->device); |
| 1422 | demo_update_data_buffer(demo); |
| 1423 | |
Courtney Goeltzenleuchter | 9808555 | 2014-11-10 11:13:13 -0700 | [diff] [blame] | 1424 | demo_draw(demo); |
Courtney Goeltzenleuchter | 1454f3c | 2014-11-18 11:28:09 -0700 | [diff] [blame] | 1425 | |
Courtney Goeltzenleuchter | 9808555 | 2014-11-10 11:13:13 -0700 | [diff] [blame] | 1426 | // Wait for work to finish before updating MVP. |
| 1427 | xglDeviceWaitIdle(demo->device); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1428 | } |
| 1429 | } |
| 1430 | |
| 1431 | static void demo_create_window(struct demo *demo) |
| 1432 | { |
| 1433 | uint32_t value_mask, value_list[32]; |
| 1434 | |
| 1435 | demo->window = xcb_generate_id(demo->connection); |
| 1436 | |
| 1437 | value_mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK; |
| 1438 | value_list[0] = demo->screen->black_pixel; |
| 1439 | value_list[1] = XCB_EVENT_MASK_KEY_RELEASE | |
| 1440 | XCB_EVENT_MASK_EXPOSURE; |
| 1441 | |
| 1442 | xcb_create_window(demo->connection, |
| 1443 | XCB_COPY_FROM_PARENT, |
| 1444 | demo->window, demo->screen->root, |
| 1445 | 0, 0, demo->width, demo->height, 0, |
| 1446 | XCB_WINDOW_CLASS_INPUT_OUTPUT, |
| 1447 | demo->screen->root_visual, |
| 1448 | value_mask, value_list); |
| 1449 | |
Courtney Goeltzenleuchter | 98cb2cb | 2014-11-06 14:27:52 -0700 | [diff] [blame] | 1450 | /* Magic code that will send notification when window is destroyed */ |
| 1451 | xcb_intern_atom_cookie_t cookie = xcb_intern_atom(demo->connection, 1, 12, |
| 1452 | "WM_PROTOCOLS"); |
| 1453 | xcb_intern_atom_reply_t* reply = xcb_intern_atom_reply(demo->connection, cookie, 0); |
| 1454 | |
| 1455 | xcb_intern_atom_cookie_t cookie2 = xcb_intern_atom(demo->connection, 0, 16, "WM_DELETE_WINDOW"); |
| 1456 | demo->atom_wm_delete_window = xcb_intern_atom_reply(demo->connection, cookie2, 0); |
| 1457 | |
| 1458 | xcb_change_property(demo->connection, XCB_PROP_MODE_REPLACE, |
| 1459 | demo->window, (*reply).atom, 4, 32, 1, |
| 1460 | &(*demo->atom_wm_delete_window).atom); |
| 1461 | free(reply); |
| 1462 | |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1463 | xcb_map_window(demo->connection, demo->window); |
| 1464 | } |
| 1465 | |
| 1466 | static void demo_init_xgl(struct demo *demo) |
| 1467 | { |
| 1468 | const XGL_APPLICATION_INFO app = { |
| 1469 | .sType = XGL_STRUCTURE_TYPE_APPLICATION_INFO, |
| 1470 | .pNext = NULL, |
Chia-I Wu | b2c8193 | 2014-12-27 15:16:07 +0800 | [diff] [blame] | 1471 | .pAppName = "cube", |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1472 | .appVersion = 0, |
Chia-I Wu | b2c8193 | 2014-12-27 15:16:07 +0800 | [diff] [blame] | 1473 | .pEngineName = "cube", |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1474 | .engineVersion = 0, |
| 1475 | .apiVersion = XGL_MAKE_VERSION(0, 22, 0), |
| 1476 | }; |
| 1477 | const XGL_WSI_X11_CONNECTION_INFO connection = { |
| 1478 | .pConnection = demo->connection, |
| 1479 | .root = demo->screen->root, |
| 1480 | .provider = 0, |
| 1481 | }; |
| 1482 | const XGL_DEVICE_QUEUE_CREATE_INFO queue = { |
| 1483 | .queueNodeIndex = 0, |
| 1484 | .queueCount = 1, |
| 1485 | }; |
| 1486 | const XGL_CHAR *ext_names[] = { |
Chia-I Wu | b2c8193 | 2014-12-27 15:16:07 +0800 | [diff] [blame] | 1487 | "XGL_WSI_X11", |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1488 | }; |
| 1489 | const XGL_DEVICE_CREATE_INFO device = { |
| 1490 | .sType = XGL_STRUCTURE_TYPE_DEVICE_CREATE_INFO, |
| 1491 | .pNext = NULL, |
| 1492 | .queueRecordCount = 1, |
| 1493 | .pRequestedQueues = &queue, |
| 1494 | .extensionCount = 1, |
| 1495 | .ppEnabledExtensionNames = ext_names, |
| 1496 | .maxValidationLevel = XGL_VALIDATION_LEVEL_END_RANGE, |
| 1497 | .flags = XGL_DEVICE_CREATE_VALIDATION_BIT, |
| 1498 | }; |
| 1499 | XGL_RESULT err; |
| 1500 | XGL_UINT gpu_count; |
| 1501 | XGL_UINT i; |
| 1502 | |
| 1503 | err = xglInitAndEnumerateGpus(&app, NULL, 1, &gpu_count, &demo->gpu); |
| 1504 | assert(!err && gpu_count == 1); |
| 1505 | |
| 1506 | for (i = 0; i < device.extensionCount; i++) { |
| 1507 | err = xglGetExtensionSupport(demo->gpu, ext_names[i]); |
| 1508 | assert(!err); |
| 1509 | } |
| 1510 | |
| 1511 | err = xglWsiX11AssociateConnection(demo->gpu, &connection); |
| 1512 | assert(!err); |
| 1513 | |
| 1514 | err = xglCreateDevice(demo->gpu, &device, &demo->device); |
| 1515 | assert(!err); |
| 1516 | |
| 1517 | err = xglGetDeviceQueue(demo->device, XGL_QUEUE_TYPE_GRAPHICS, |
| 1518 | 0, &demo->queue); |
| 1519 | assert(!err); |
| 1520 | } |
| 1521 | |
| 1522 | static void demo_init_connection(struct demo *demo) |
| 1523 | { |
| 1524 | const xcb_setup_t *setup; |
| 1525 | xcb_screen_iterator_t iter; |
| 1526 | int scr; |
| 1527 | |
| 1528 | demo->connection = xcb_connect(NULL, &scr); |
| 1529 | |
| 1530 | setup = xcb_get_setup(demo->connection); |
| 1531 | iter = xcb_setup_roots_iterator(setup); |
| 1532 | while (scr-- > 0) |
| 1533 | xcb_screen_next(&iter); |
| 1534 | |
| 1535 | demo->screen = iter.data; |
| 1536 | } |
| 1537 | |
| 1538 | static void demo_init(struct demo *demo) |
| 1539 | { |
Courtney Goeltzenleuchter | 8879d3a | 2014-10-29 08:29:35 -0600 | [diff] [blame] | 1540 | vec3 eye = {0.0f, 3.0f, 5.0f}; |
| 1541 | vec3 origin = {0, 0, 0}; |
| 1542 | vec3 up = {0.0f, -1.0f, 0.0}; |
| 1543 | |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1544 | memset(demo, 0, sizeof(*demo)); |
| 1545 | |
| 1546 | demo_init_connection(demo); |
| 1547 | demo_init_xgl(demo); |
| 1548 | |
Courtney Goeltzenleuchter | 8879d3a | 2014-10-29 08:29:35 -0600 | [diff] [blame] | 1549 | demo->width = 500; |
| 1550 | demo->height = 500; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1551 | demo->format.channelFormat = XGL_CH_FMT_B8G8R8A8; |
| 1552 | demo->format.numericFormat = XGL_NUM_FMT_UNORM; |
Courtney Goeltzenleuchter | 8879d3a | 2014-10-29 08:29:35 -0600 | [diff] [blame] | 1553 | |
| 1554 | demo->spin_angle = 0.01f; |
| 1555 | demo->spin_increment = 0.01f; |
| 1556 | demo->pause = false; |
| 1557 | |
| 1558 | mat4x4_perspective(demo->projection_matrix, degreesToRadians(45.0f), 1.0f, 0.1f, 100.0f); |
| 1559 | mat4x4_look_at(demo->view_matrix, eye, origin, up); |
| 1560 | mat4x4_identity(demo->model_matrix); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1561 | } |
| 1562 | |
| 1563 | static void demo_cleanup(struct demo *demo) |
| 1564 | { |
Jon Ashburn | b2a6665 | 2015-01-16 09:37:43 -0700 | [diff] [blame] | 1565 | XGL_UINT i, j; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1566 | |
Chia-I Wu | 6a3c897 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 1567 | xglDestroyObject(demo->desc_set); |
| 1568 | xglDestroyObject(demo->desc_region); |
| 1569 | |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1570 | xglDestroyObject(demo->cmd); |
| 1571 | |
| 1572 | xglDestroyObject(demo->viewport); |
| 1573 | xglDestroyObject(demo->raster); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1574 | xglDestroyObject(demo->color_blend); |
| 1575 | xglDestroyObject(demo->depth_stencil); |
| 1576 | |
| 1577 | xglDestroyObject(demo->pipeline); |
Chia-I Wu | 6a3c897 | 2015-01-04 16:27:24 +0800 | [diff] [blame] | 1578 | xglDestroyObject(demo->desc_layout_fs); |
| 1579 | xglDestroyObject(demo->desc_layout_vs); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1580 | |
Courtney Goeltzenleuchter | 4984d15 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 1581 | // xglFreeMemory(demo->vertices.mem); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1582 | |
| 1583 | for (i = 0; i < DEMO_TEXTURE_COUNT; i++) { |
| 1584 | xglDestroyObject(demo->textures[i].view); |
| 1585 | xglDestroyObject(demo->textures[i].image); |
Jon Ashburn | b2a6665 | 2015-01-16 09:37:43 -0700 | [diff] [blame] | 1586 | for (j = 0; j < demo->textures[i].num_mem; j++) |
| 1587 | xglFreeMemory(demo->textures[i].mem[j]); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1588 | xglDestroyObject(demo->textures[i].sampler); |
| 1589 | } |
| 1590 | |
| 1591 | xglDestroyObject(demo->depth.view); |
| 1592 | xglDestroyObject(demo->depth.image); |
Jon Ashburn | b2a6665 | 2015-01-16 09:37:43 -0700 | [diff] [blame] | 1593 | for (j = 0; j < demo->depth.num_mem; j++) |
| 1594 | xglFreeMemory(demo->depth.mem[j]); |
Jon Ashburn | ae7c21c | 2015-01-19 15:00:26 -0700 | [diff] [blame] | 1595 | xglDestroyObject(demo->uniform_data.buf); |
| 1596 | for (j = 0; j < demo->uniform_data.num_mem; j++) |
| 1597 | xglFreeMemory(demo->uniform_data.mem[j]); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1598 | |
| 1599 | for (i = 0; i < DEMO_BUFFER_COUNT; i++) { |
Chia-I Wu | bb57f64 | 2014-11-07 14:30:34 +0800 | [diff] [blame] | 1600 | xglDestroyObject(demo->buffers[i].fence); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1601 | xglDestroyObject(demo->buffers[i].view); |
| 1602 | xglDestroyObject(demo->buffers[i].image); |
| 1603 | } |
| 1604 | |
| 1605 | xglDestroyDevice(demo->device); |
| 1606 | |
| 1607 | xcb_destroy_window(demo->connection, demo->window); |
| 1608 | xcb_disconnect(demo->connection); |
| 1609 | } |
| 1610 | |
| 1611 | int main(void) |
| 1612 | { |
| 1613 | struct demo demo; |
| 1614 | |
| 1615 | demo_init(&demo); |
| 1616 | |
| 1617 | demo_prepare(&demo); |
| 1618 | demo_create_window(&demo); |
| 1619 | demo_run(&demo); |
| 1620 | |
| 1621 | demo_cleanup(&demo); |
| 1622 | |
| 1623 | return 0; |
| 1624 | } |