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