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