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