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 | 6836939 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 22 | static char *tex_files[] = { |
| 23 | "demos/lunarg-logo-256x256-solid.png" |
| 24 | }; |
| 25 | |
Courtney Goeltzenleuchter | 4984d15 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 26 | struct xglcube_vs_uniform { |
| 27 | // Must start with MVP |
| 28 | XGL_FLOAT mvp[4][4]; |
| 29 | XGL_FLOAT position[12*3][4]; |
| 30 | XGL_FLOAT color[12*3][4]; |
| 31 | }; |
| 32 | |
| 33 | struct xgltexcube_vs_uniform { |
| 34 | // Must start with MVP |
| 35 | XGL_FLOAT mvp[4][4]; |
| 36 | XGL_FLOAT position[12*3][4]; |
| 37 | XGL_FLOAT attr[12*3][4]; |
| 38 | }; |
Courtney Goeltzenleuchter | fc3b953 | 2014-10-28 10:32:57 -0600 | [diff] [blame] | 39 | |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 40 | //-------------------------------------------------------------------------------------- |
| 41 | // Mesh and VertexFormat Data |
| 42 | //-------------------------------------------------------------------------------------- |
| 43 | struct Vertex |
| 44 | { |
| 45 | XGL_FLOAT posX, posY, posZ, posW; // Position data |
| 46 | XGL_FLOAT r, g, b, a; // Color |
| 47 | }; |
| 48 | |
Courtney Goeltzenleuchter | 4984d15 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 49 | struct VertexPosTex |
| 50 | { |
| 51 | XGL_FLOAT posX, posY, posZ, posW; // Position data |
| 52 | XGL_FLOAT u, v, s, t; // Texcoord |
| 53 | }; |
| 54 | |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 55 | #define XYZ1(_x_, _y_, _z_) (_x_), (_y_), (_z_), 1.f |
Courtney Goeltzenleuchter | 4984d15 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 56 | #define UV(_u_, _v_) (_u_), (_v_), 0.f, 1.f |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 57 | |
Courtney Goeltzenleuchter | 4984d15 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 58 | static const XGL_FLOAT g_vertex_buffer_data[] = { |
Courtney Goeltzenleuchter | 6836939 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 59 | -1.0f,-1.0f,-1.0f, // Vertex 0 |
| 60 | -1.0f,-1.0f, 1.0f, |
| 61 | -1.0f, 1.0f, 1.0f, |
| 62 | |
| 63 | -1.0f, 1.0f, 1.0f, // Vertex 1 |
| 64 | -1.0f, 1.0f,-1.0f, |
| 65 | -1.0f,-1.0f,-1.0f, |
| 66 | |
| 67 | -1.0f,-1.0f,-1.0f, // Vertex 2 |
| 68 | 1.0f, 1.0f,-1.0f, |
| 69 | 1.0f,-1.0f,-1.0f, |
| 70 | |
| 71 | -1.0f,-1.0f,-1.0f, // Vertex 3 |
| 72 | 1.0f, 1.0f,-1.0f, |
| 73 | -1.0f, 1.0f,-1.0f, |
| 74 | |
| 75 | -1.0f,-1.0f,-1.0f, // Vertex 4 |
| 76 | 1.0f,-1.0f, 1.0f, |
| 77 | 1.0f,-1.0f,-1.0f, |
| 78 | |
| 79 | -1.0f,-1.0f,-1.0f, // Vertex 5 |
| 80 | -1.0f,-1.0f, 1.0f, |
| 81 | 1.0f,-1.0f, 1.0f, |
| 82 | |
| 83 | -1.0f, 1.0f,-1.0f, // Vertex 6 |
| 84 | -1.0f, 1.0f, 1.0f, |
| 85 | 1.0f, 1.0f, 1.0f, |
| 86 | |
| 87 | -1.0f, 1.0f,-1.0f, // Vertex 7 |
| 88 | 1.0f, 1.0f,-1.0f, |
| 89 | 1.0f, 1.0f, 1.0f, |
| 90 | |
| 91 | 1.0f, 1.0f,-1.0f, // Vertex 8 |
| 92 | 1.0f, 1.0f, 1.0f, |
| 93 | 1.0f,-1.0f, 1.0f, |
| 94 | |
| 95 | 1.0f,-1.0f, 1.0f, // Vertex 9 |
| 96 | 1.0f,-1.0f,-1.0f, |
| 97 | 1.0f, 1.0f,-1.0f, |
| 98 | |
| 99 | -1.0f, 1.0f, 1.0f, // Vertex 10 |
| 100 | 1.0f, 1.0f, 1.0f, |
| 101 | -1.0f,-1.0f, 1.0f, |
| 102 | |
| 103 | -1.0f,-1.0f, 1.0f, // Vertex 11 |
| 104 | 1.0f,-1.0f, 1.0f, |
| 105 | 1.0f, 1.0f, 1.0f, |
Courtney Goeltzenleuchter | 4984d15 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 106 | }; |
| 107 | |
| 108 | static const XGL_FLOAT g_uv_buffer_data[] = { |
Courtney Goeltzenleuchter | 8879d3a | 2014-10-29 08:29:35 -0600 | [diff] [blame^] | 109 | 1.0f, 0.0f, // Vertex 0 |
Courtney Goeltzenleuchter | 6836939 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 110 | 0.0f, 0.0f, |
| 111 | 0.0f, 1.0f, |
| 112 | |
Courtney Goeltzenleuchter | 8879d3a | 2014-10-29 08:29:35 -0600 | [diff] [blame^] | 113 | 0.0f, 1.0f, // Vertex 1 |
Courtney Goeltzenleuchter | 6836939 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 114 | 1.0f, 1.0f, |
Courtney Goeltzenleuchter | 8879d3a | 2014-10-29 08:29:35 -0600 | [diff] [blame^] | 115 | 1.0f, 0.0f, |
| 116 | |
| 117 | // 0.0f, 1.0f, // Vertex 2 |
| 118 | // 1.0f, 0.0f, |
| 119 | // 0.0f, 0.0f, |
| 120 | |
| 121 | // 0.0f, 1.0f, // Vertex 3 |
| 122 | // 1.0f, 0.0f, |
| 123 | // 1.0f, 1.0f, |
| 124 | |
| 125 | 0.0f, 0.0f, // Vertex 2 |
| 126 | 1.0f, 1.0f, |
| 127 | 1.0f, 0.0f, |
| 128 | |
| 129 | 0.0f, 0.0f, // Vertex 3 |
| 130 | 1.0f, 1.0f, |
| 131 | 0.0f, 1.0f, |
Courtney Goeltzenleuchter | 6836939 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 132 | |
| 133 | 0.0f, 1.0f, // Vertex 4 |
| 134 | 1.0f, 0.0f, |
| 135 | 0.0f, 0.0f, |
| 136 | |
| 137 | 0.0f, 1.0f, // Vertex 5 |
| 138 | 1.0f, 1.0f, |
| 139 | 1.0f, 0.0f, |
| 140 | |
| 141 | 0.0f, 1.0f, // Vertex 6 |
| 142 | 1.0f, 1.0f, |
| 143 | 1.0f, 0.0f, |
| 144 | |
| 145 | 0.0f, 1.0f, // Vertex 7 |
| 146 | 0.0f, 0.0f, |
| 147 | 1.0f, 0.0f, |
| 148 | |
Courtney Goeltzenleuchter | 8879d3a | 2014-10-29 08:29:35 -0600 | [diff] [blame^] | 149 | 0.0f, 1.0f, // Vertex 8 |
Courtney Goeltzenleuchter | 6836939 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 150 | 1.0f, 1.0f, |
Courtney Goeltzenleuchter | 6836939 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 151 | 1.0f, 0.0f, |
Courtney Goeltzenleuchter | 8879d3a | 2014-10-29 08:29:35 -0600 | [diff] [blame^] | 152 | |
| 153 | 1.0f, 0.0f, // Vertex 9 |
Courtney Goeltzenleuchter | 6836939 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 154 | 0.0f, 0.0f, |
Courtney Goeltzenleuchter | 6836939 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 155 | 0.0f, 1.0f, |
| 156 | |
Courtney Goeltzenleuchter | 8879d3a | 2014-10-29 08:29:35 -0600 | [diff] [blame^] | 157 | 1.0f, 1.0f, // Vertex 10 |
| 158 | 0.0f, 1.0f, |
Courtney Goeltzenleuchter | 6836939 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 159 | 1.0f, 0.0f, |
Courtney Goeltzenleuchter | 8879d3a | 2014-10-29 08:29:35 -0600 | [diff] [blame^] | 160 | |
| 161 | 1.0f, 0.0f, // Vertex 11 |
| 162 | 0.0f, 0.0f, |
| 163 | 0.0f, 1.0f, |
Courtney Goeltzenleuchter | 4984d15 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 164 | }; |
| 165 | |
| 166 | void dumpMatrix(const char *note, mat4x4 MVP) |
| 167 | { |
| 168 | int i; |
| 169 | |
| 170 | printf("%s: \n", note); |
| 171 | for (i=0; i<4; i++) { |
| 172 | printf("%f, %f, %f, %f\n", MVP[i][0], MVP[i][1], MVP[i][2], MVP[i][3]); |
| 173 | } |
| 174 | printf("\n"); |
| 175 | fflush(stdout); |
| 176 | } |
| 177 | |
| 178 | void dumpVec4(const char *note, vec4 vector) |
| 179 | { |
| 180 | printf("%s: \n", note); |
| 181 | printf("%f, %f, %f, %f\n", vector[0], vector[1], vector[2], vector[3]); |
| 182 | printf("\n"); |
| 183 | fflush(stdout); |
| 184 | } |
| 185 | |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 186 | struct demo { |
| 187 | xcb_connection_t *connection; |
| 188 | xcb_screen_t *screen; |
| 189 | |
| 190 | XGL_PHYSICAL_GPU gpu; |
| 191 | XGL_DEVICE device; |
| 192 | XGL_QUEUE queue; |
| 193 | |
| 194 | int width, height; |
| 195 | XGL_FORMAT format; |
| 196 | |
| 197 | struct { |
| 198 | XGL_IMAGE image; |
| 199 | XGL_GPU_MEMORY mem; |
| 200 | |
| 201 | XGL_COLOR_ATTACHMENT_VIEW view; |
| 202 | } buffers[DEMO_BUFFER_COUNT]; |
| 203 | |
| 204 | struct { |
| 205 | XGL_FORMAT format; |
| 206 | |
| 207 | XGL_IMAGE image; |
| 208 | XGL_GPU_MEMORY mem; |
| 209 | XGL_DEPTH_STENCIL_VIEW view; |
| 210 | } depth; |
| 211 | |
| 212 | struct { |
| 213 | XGL_SAMPLER sampler; |
| 214 | |
Courtney Goeltzenleuchter | 6836939 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 215 | char *filename; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 216 | XGL_IMAGE image; |
| 217 | XGL_GPU_MEMORY mem; |
| 218 | XGL_IMAGE_VIEW view; |
| 219 | } textures[DEMO_TEXTURE_COUNT]; |
| 220 | |
| 221 | struct { |
| 222 | XGL_GPU_MEMORY mem; |
| 223 | XGL_MEMORY_VIEW_ATTACH_INFO view; |
| 224 | |
| 225 | XGL_PIPELINE_VERTEX_INPUT_CREATE_INFO vi; |
| 226 | XGL_VERTEX_INPUT_BINDING_DESCRIPTION vi_bindings[1]; |
| 227 | XGL_VERTEX_INPUT_ATTRIBUTE_DESCRIPTION vi_attrs[2]; |
| 228 | } vertices; |
| 229 | |
Courtney Goeltzenleuchter | fc3b953 | 2014-10-28 10:32:57 -0600 | [diff] [blame] | 230 | struct { |
| 231 | XGL_GPU_MEMORY mem; |
| 232 | XGL_MEMORY_VIEW_ATTACH_INFO view; |
| 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 | |
| 239 | XGL_VIEWPORT_STATE_OBJECT viewport; |
| 240 | XGL_RASTER_STATE_OBJECT raster; |
| 241 | XGL_MSAA_STATE_OBJECT msaa; |
| 242 | XGL_COLOR_BLEND_STATE_OBJECT color_blend; |
| 243 | XGL_DEPTH_STENCIL_STATE_OBJECT depth_stencil; |
| 244 | |
Courtney Goeltzenleuchter | fc3b953 | 2014-10-28 10:32:57 -0600 | [diff] [blame] | 245 | mat4x4 projection_matrix; |
| 246 | mat4x4 view_matrix; |
| 247 | mat4x4 model_matrix; |
| 248 | |
Courtney Goeltzenleuchter | 8879d3a | 2014-10-29 08:29:35 -0600 | [diff] [blame^] | 249 | XGL_FLOAT spin_angle; |
| 250 | XGL_FLOAT spin_increment; |
| 251 | bool pause; |
| 252 | |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 253 | XGL_CMD_BUFFER cmd; |
| 254 | |
| 255 | xcb_window_t window; |
| 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; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 275 | XGL_RESULT err; |
| 276 | |
| 277 | err = xglBeginCommandBuffer(demo->cmd, |
| 278 | XGL_CMD_BUFFER_OPTIMIZE_GPU_SMALL_BATCH_BIT | |
| 279 | XGL_CMD_BUFFER_OPTIMIZE_ONE_TIME_SUBMIT_BIT); |
| 280 | assert(!err); |
| 281 | |
| 282 | xglCmdBindPipeline(demo->cmd, XGL_PIPELINE_BIND_POINT_GRAPHICS, |
| 283 | demo->pipeline); |
| 284 | xglCmdBindDescriptorSet(demo->cmd, XGL_PIPELINE_BIND_POINT_GRAPHICS, |
| 285 | 0, demo->dset, 0); |
| 286 | |
| 287 | xglCmdBindStateObject(demo->cmd, XGL_STATE_BIND_VIEWPORT, demo->viewport); |
| 288 | xglCmdBindStateObject(demo->cmd, XGL_STATE_BIND_RASTER, demo->raster); |
| 289 | xglCmdBindStateObject(demo->cmd, XGL_STATE_BIND_MSAA, demo->msaa); |
| 290 | xglCmdBindStateObject(demo->cmd, XGL_STATE_BIND_COLOR_BLEND, |
| 291 | demo->color_blend); |
| 292 | xglCmdBindStateObject(demo->cmd, XGL_STATE_BIND_DEPTH_STENCIL, |
| 293 | demo->depth_stencil); |
| 294 | |
| 295 | xglCmdBindAttachments(demo->cmd, 1, &color_attachment, &depth_stencil); |
| 296 | |
Courtney Goeltzenleuchter | fc3b953 | 2014-10-28 10:32:57 -0600 | [diff] [blame] | 297 | clear_range.aspect = XGL_IMAGE_ASPECT_COLOR; |
| 298 | clear_range.baseMipLevel = 0; |
| 299 | clear_range.mipLevels = 1; |
| 300 | clear_range.baseArraySlice = 0; |
| 301 | clear_range.arraySize = 1; |
| 302 | xglCmdClearColorImage(demo->cmd, |
| 303 | demo->buffers[demo->current_buffer].image, |
| 304 | clear_color, 1, &clear_range); |
| 305 | |
| 306 | clear_range.aspect = XGL_IMAGE_ASPECT_DEPTH; |
| 307 | xglCmdClearDepthStencil(demo->cmd, demo->depth.image, |
| 308 | clear_depth, 0, 1, &clear_range); |
| 309 | |
Courtney Goeltzenleuchter | 8879d3a | 2014-10-29 08:29:35 -0600 | [diff] [blame^] | 310 | xglCmdDraw(demo->cmd, 0, 12 * 3, 0, 1); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 311 | |
| 312 | err = xglEndCommandBuffer(demo->cmd); |
| 313 | assert(!err); |
| 314 | } |
| 315 | |
Courtney Goeltzenleuchter | fc3b953 | 2014-10-28 10:32:57 -0600 | [diff] [blame] | 316 | |
Courtney Goeltzenleuchter | 8879d3a | 2014-10-29 08:29:35 -0600 | [diff] [blame^] | 317 | void demo_update_data_buffer(struct demo *demo) |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 318 | { |
Courtney Goeltzenleuchter | fc3b953 | 2014-10-28 10:32:57 -0600 | [diff] [blame] | 319 | mat4x4 MVP, Model, VP; |
| 320 | int matrixSize = sizeof(MVP); |
| 321 | XGL_UINT8 *pData; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 322 | XGL_RESULT err; |
| 323 | |
Courtney Goeltzenleuchter | fc3b953 | 2014-10-28 10:32:57 -0600 | [diff] [blame] | 324 | mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 325 | |
Courtney Goeltzenleuchter | fc3b953 | 2014-10-28 10:32:57 -0600 | [diff] [blame] | 326 | // Rotate 22.5 degrees around the Y axis |
Courtney Goeltzenleuchter | 4984d15 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 327 | mat4x4_dup(Model, demo->model_matrix); |
Courtney Goeltzenleuchter | 8879d3a | 2014-10-29 08:29:35 -0600 | [diff] [blame^] | 328 | 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] | 329 | mat4x4_mul(MVP, VP, demo->model_matrix); |
Courtney Goeltzenleuchter | fc3b953 | 2014-10-28 10:32:57 -0600 | [diff] [blame] | 330 | |
| 331 | err = xglMapMemory(demo->uniform_data.mem, 0, (XGL_VOID **) &pData); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 332 | assert(!err); |
| 333 | |
Courtney Goeltzenleuchter | fc3b953 | 2014-10-28 10:32:57 -0600 | [diff] [blame] | 334 | memcpy(pData, (const void*) &MVP[0][0], matrixSize); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 335 | |
Courtney Goeltzenleuchter | fc3b953 | 2014-10-28 10:32:57 -0600 | [diff] [blame] | 336 | err = xglUnmapMemory(demo->uniform_data.mem); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 337 | assert(!err); |
| 338 | } |
| 339 | |
| 340 | static void demo_draw(struct demo *demo) |
| 341 | { |
| 342 | const XGL_WSI_X11_PRESENT_INFO present = { |
| 343 | .destWindow = demo->window, |
| 344 | .srcImage = demo->buffers[demo->current_buffer].image, |
Courtney Goeltzenleuchter | 8879d3a | 2014-10-29 08:29:35 -0600 | [diff] [blame^] | 345 | .async = false |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 346 | }; |
| 347 | XGL_RESULT err; |
| 348 | |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 349 | demo_draw_build_cmd(demo); |
| 350 | |
| 351 | err = xglQueueSubmit(demo->queue, 1, &demo->cmd, |
| 352 | 0, NULL, XGL_NULL_HANDLE); |
| 353 | assert(!err); |
| 354 | |
| 355 | err = xglWsiX11QueuePresent(demo->queue, &present, XGL_NULL_HANDLE); |
| 356 | assert(!err); |
| 357 | |
| 358 | demo->current_buffer = (demo->current_buffer + 1) % DEMO_BUFFER_COUNT; |
| 359 | } |
| 360 | |
| 361 | static void demo_prepare_buffers(struct demo *demo) |
| 362 | { |
| 363 | const XGL_WSI_X11_PRESENTABLE_IMAGE_CREATE_INFO presentable_image = { |
| 364 | .format = demo->format, |
| 365 | .usage = XGL_IMAGE_USAGE_COLOR_ATTACHMENT_BIT, |
| 366 | .extent = { |
| 367 | .width = demo->width, |
| 368 | .height = demo->height, |
| 369 | }, |
| 370 | .flags = 0, |
| 371 | }; |
| 372 | XGL_RESULT err; |
| 373 | XGL_UINT i; |
| 374 | |
| 375 | for (i = 0; i < DEMO_BUFFER_COUNT; i++) { |
| 376 | XGL_COLOR_ATTACHMENT_VIEW_CREATE_INFO color_attachment_view = { |
| 377 | .sType = XGL_STRUCTURE_TYPE_COLOR_ATTACHMENT_VIEW_CREATE_INFO, |
| 378 | .pNext = NULL, |
| 379 | .format = demo->format, |
| 380 | .mipLevel = 0, |
| 381 | .baseArraySlice = 0, |
| 382 | .arraySize = 1, |
| 383 | }; |
| 384 | |
| 385 | err = xglWsiX11CreatePresentableImage(demo->device, &presentable_image, |
| 386 | &demo->buffers[i].image, &demo->buffers[i].mem); |
| 387 | assert(!err); |
| 388 | |
| 389 | color_attachment_view.image = demo->buffers[i].image; |
| 390 | |
| 391 | err = xglCreateColorAttachmentView(demo->device, |
| 392 | &color_attachment_view, &demo->buffers[i].view); |
| 393 | assert(!err); |
| 394 | } |
| 395 | } |
| 396 | |
| 397 | static void demo_prepare_depth(struct demo *demo) |
| 398 | { |
| 399 | const XGL_FORMAT depth_format = { XGL_CH_FMT_R16, XGL_NUM_FMT_DS }; |
| 400 | const XGL_IMAGE_CREATE_INFO image = { |
| 401 | .sType = XGL_STRUCTURE_TYPE_IMAGE_CREATE_INFO, |
| 402 | .pNext = NULL, |
| 403 | .imageType = XGL_IMAGE_2D, |
| 404 | .format = depth_format, |
| 405 | .extent = { demo->width, demo->height, 1 }, |
| 406 | .mipLevels = 1, |
| 407 | .arraySize = 1, |
| 408 | .samples = 1, |
| 409 | .tiling = XGL_OPTIMAL_TILING, |
| 410 | .usage = XGL_IMAGE_USAGE_DEPTH_STENCIL_BIT, |
| 411 | .flags = 0, |
| 412 | }; |
| 413 | XGL_MEMORY_ALLOC_INFO mem_alloc = { |
| 414 | .sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_INFO, |
| 415 | .pNext = NULL, |
| 416 | .allocationSize = 0, |
| 417 | .alignment = 0, |
| 418 | .flags = 0, |
| 419 | .heapCount = 0, |
| 420 | .memPriority = XGL_MEMORY_PRIORITY_NORMAL, |
| 421 | }; |
| 422 | XGL_DEPTH_STENCIL_VIEW_CREATE_INFO view = { |
| 423 | .sType = XGL_STRUCTURE_TYPE_DEPTH_STENCIL_VIEW_CREATE_INFO, |
| 424 | .pNext = NULL, |
| 425 | .image = XGL_NULL_HANDLE, |
| 426 | .mipLevel = 0, |
| 427 | .baseArraySlice = 0, |
| 428 | .arraySize = 1, |
| 429 | .flags = 0, |
| 430 | }; |
| 431 | XGL_MEMORY_REQUIREMENTS mem_reqs; |
| 432 | XGL_SIZE mem_reqs_size; |
| 433 | XGL_RESULT err; |
| 434 | |
| 435 | demo->depth.format = depth_format; |
| 436 | |
| 437 | /* create image */ |
| 438 | err = xglCreateImage(demo->device, &image, |
| 439 | &demo->depth.image); |
| 440 | assert(!err); |
| 441 | |
| 442 | err = xglGetObjectInfo(demo->depth.image, |
| 443 | XGL_INFO_TYPE_MEMORY_REQUIREMENTS, |
| 444 | &mem_reqs_size, &mem_reqs); |
| 445 | assert(!err && mem_reqs_size == sizeof(mem_reqs)); |
| 446 | |
| 447 | mem_alloc.allocationSize = mem_reqs.size; |
| 448 | mem_alloc.alignment = mem_reqs.alignment; |
| 449 | mem_alloc.heapCount = mem_reqs.heapCount; |
| 450 | memcpy(mem_alloc.heaps, mem_reqs.heaps, |
| 451 | sizeof(mem_reqs.heaps[0]) * mem_reqs.heapCount); |
| 452 | |
| 453 | /* allocate memory */ |
| 454 | err = xglAllocMemory(demo->device, &mem_alloc, |
| 455 | &demo->depth.mem); |
| 456 | assert(!err); |
| 457 | |
| 458 | /* bind memory */ |
| 459 | err = xglBindObjectMemory(demo->depth.image, |
| 460 | demo->depth.mem, 0); |
| 461 | assert(!err); |
| 462 | |
| 463 | /* create image view */ |
| 464 | view.image = demo->depth.image; |
| 465 | err = xglCreateDepthStencilView(demo->device, &view, |
| 466 | &demo->depth.view); |
| 467 | assert(!err); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 468 | } |
| 469 | |
Courtney Goeltzenleuchter | 6836939 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 470 | /** loadTexture |
| 471 | * loads a png file into an memory object, using cstdio , libpng. |
| 472 | * |
| 473 | * \param demo : Needed to access XGL calls |
| 474 | * \param filename : the png file to be loaded |
| 475 | * \param width : width of png, to be updated as a side effect of this function |
| 476 | * \param height : height of png, to be updated as a side effect of this function |
| 477 | * |
| 478 | * \return bool : an opengl texture id. true if successful?, |
| 479 | * should be validated by the client of this function. |
| 480 | * |
| 481 | * Source: http://en.wikibooks.org/wiki/OpenGL_Programming/Intermediate/Textures |
| 482 | * Modified to copy image to memory |
| 483 | * |
| 484 | */ |
| 485 | bool loadTexture(char *filename, XGL_UINT8 *rgba_data, |
| 486 | XGL_SUBRESOURCE_LAYOUT *layout, |
| 487 | XGL_INT *width, XGL_INT *height) |
| 488 | { |
| 489 | //header for testing if it is a png |
| 490 | png_byte header[8]; |
| 491 | int i, is_png, bit_depth, color_type,rowbytes; |
| 492 | png_uint_32 twidth, theight; |
| 493 | png_structp png_ptr; |
| 494 | png_infop info_ptr, end_info; |
| 495 | png_byte *image_data; |
| 496 | png_bytep *row_pointers; |
| 497 | |
| 498 | //open file as binary |
| 499 | FILE *fp = fopen(filename, "rb"); |
| 500 | if (!fp) { |
| 501 | return false; |
| 502 | } |
| 503 | |
| 504 | //read the header |
| 505 | fread(header, 1, 8, fp); |
| 506 | |
| 507 | //test if png |
| 508 | is_png = !png_sig_cmp(header, 0, 8); |
| 509 | if (!is_png) { |
| 510 | fclose(fp); |
| 511 | return false; |
| 512 | } |
| 513 | |
| 514 | //create png struct |
| 515 | png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, |
| 516 | NULL, NULL); |
| 517 | if (!png_ptr) { |
| 518 | fclose(fp); |
| 519 | return (false); |
| 520 | } |
| 521 | |
| 522 | //create png info struct |
| 523 | info_ptr = png_create_info_struct(png_ptr); |
| 524 | if (!info_ptr) { |
| 525 | png_destroy_read_struct(&png_ptr, (png_infopp) NULL, (png_infopp) NULL); |
| 526 | fclose(fp); |
| 527 | return (false); |
| 528 | } |
| 529 | |
| 530 | //create png info struct |
| 531 | end_info = png_create_info_struct(png_ptr); |
| 532 | if (!end_info) { |
| 533 | png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp) NULL); |
| 534 | fclose(fp); |
| 535 | return (false); |
| 536 | } |
| 537 | |
| 538 | //png error stuff, not sure libpng man suggests this. |
| 539 | if (setjmp(png_jmpbuf(png_ptr))) { |
| 540 | png_destroy_read_struct(&png_ptr, &info_ptr, &end_info); |
| 541 | fclose(fp); |
| 542 | return (false); |
| 543 | } |
| 544 | |
| 545 | //init png reading |
| 546 | png_init_io(png_ptr, fp); |
| 547 | |
| 548 | //let libpng know you already read the first 8 bytes |
| 549 | png_set_sig_bytes(png_ptr, 8); |
| 550 | |
| 551 | // read all the info up to the image data |
| 552 | png_read_info(png_ptr, info_ptr); |
| 553 | |
| 554 | // get info about png |
| 555 | png_get_IHDR(png_ptr, info_ptr, &twidth, &theight, &bit_depth, &color_type, |
| 556 | NULL, NULL, NULL); |
| 557 | |
| 558 | //update width and height based on png info |
| 559 | *width = twidth; |
| 560 | *height = theight; |
| 561 | |
| 562 | // Require that incoming texture be 8bits per color component |
| 563 | // and 4 components (RGBA). |
| 564 | if (png_get_bit_depth(png_ptr, info_ptr) != 8 || |
| 565 | png_get_channels(png_ptr, info_ptr) != 4) { |
| 566 | return false; |
| 567 | } |
| 568 | |
| 569 | if (rgba_data == NULL) { |
| 570 | // If data pointer is null, we just want the width & height |
| 571 | // clean up memory and close stuff |
| 572 | png_destroy_read_struct(&png_ptr, &info_ptr, &end_info); |
| 573 | fclose(fp); |
| 574 | |
| 575 | return true; |
| 576 | } |
| 577 | |
| 578 | // Update the png info struct. |
| 579 | png_read_update_info(png_ptr, info_ptr); |
| 580 | |
| 581 | // Row size in bytes. |
| 582 | rowbytes = png_get_rowbytes(png_ptr, info_ptr); |
| 583 | |
| 584 | // Allocate the image_data as a big block, to be given to opengl |
| 585 | image_data = (png_byte *)malloc(rowbytes * theight * sizeof(png_byte)); |
| 586 | if (!image_data) { |
| 587 | //clean up memory and close stuff |
| 588 | png_destroy_read_struct(&png_ptr, &info_ptr, &end_info); |
| 589 | fclose(fp); |
| 590 | return false; |
| 591 | } |
| 592 | |
| 593 | // row_pointers is for pointing to image_data for reading the png with libpng |
| 594 | row_pointers = (png_bytep *)malloc(theight * sizeof(png_bytep)); |
| 595 | if (!row_pointers) { |
| 596 | //clean up memory and close stuff |
| 597 | png_destroy_read_struct(&png_ptr, &info_ptr, &end_info); |
| 598 | // delete[] image_data; |
| 599 | fclose(fp); |
| 600 | return false; |
| 601 | } |
| 602 | // set the individual row_pointers to point at the correct offsets of image_data |
| 603 | for (i = 0; i < theight; ++i) |
| 604 | row_pointers[theight - 1 - i] = rgba_data + i * rowbytes; |
| 605 | |
| 606 | // read the png into image_data through row_pointers |
| 607 | png_read_image(png_ptr, row_pointers); |
| 608 | |
| 609 | // clean up memory and close stuff |
| 610 | png_destroy_read_struct(&png_ptr, &info_ptr, &end_info); |
| 611 | free(row_pointers); |
| 612 | free(image_data); |
| 613 | fclose(fp); |
| 614 | |
| 615 | return true; |
| 616 | } |
Courtney Goeltzenleuchter | 6836939 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 617 | |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 618 | static void demo_prepare_textures(struct demo *demo) |
| 619 | { |
Courtney Goeltzenleuchter | 4984d15 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 620 | 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] | 621 | XGL_INT tex_width; |
| 622 | XGL_INT tex_height; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 623 | XGL_RESULT err; |
| 624 | XGL_UINT i; |
| 625 | |
| 626 | for (i = 0; i < DEMO_TEXTURE_COUNT; i++) { |
| 627 | const XGL_SAMPLER_CREATE_INFO sampler = { |
| 628 | .sType = XGL_STRUCTURE_TYPE_SAMPLER_CREATE_INFO, |
| 629 | .pNext = NULL, |
| 630 | .magFilter = XGL_TEX_FILTER_NEAREST, |
| 631 | .minFilter = XGL_TEX_FILTER_NEAREST, |
| 632 | .mipMode = XGL_TEX_MIPMAP_BASE, |
Courtney Goeltzenleuchter | 6836939 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 633 | .addressU = XGL_TEX_ADDRESS_CLAMP, |
| 634 | .addressV = XGL_TEX_ADDRESS_CLAMP, |
| 635 | .addressW = XGL_TEX_ADDRESS_CLAMP, |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 636 | .mipLodBias = 0.0f, |
| 637 | .maxAnisotropy = 0, |
| 638 | .compareFunc = XGL_COMPARE_NEVER, |
| 639 | .minLod = 0.0f, |
| 640 | .maxLod = 0.0f, |
| 641 | .borderColorType = XGL_BORDER_COLOR_OPAQUE_WHITE, |
| 642 | }; |
Courtney Goeltzenleuchter | 6836939 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 643 | |
| 644 | assert(loadTexture(tex_files[i], NULL, NULL, &tex_width, &tex_height)); |
| 645 | |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 646 | const XGL_IMAGE_CREATE_INFO image = { |
| 647 | .sType = XGL_STRUCTURE_TYPE_IMAGE_CREATE_INFO, |
| 648 | .pNext = NULL, |
| 649 | .imageType = XGL_IMAGE_2D, |
| 650 | .format = tex_format, |
| 651 | .extent = { tex_width, tex_height, 1 }, |
| 652 | .mipLevels = 1, |
| 653 | .arraySize = 1, |
| 654 | .samples = 1, |
| 655 | .tiling = XGL_LINEAR_TILING, |
| 656 | .usage = XGL_IMAGE_USAGE_SHADER_ACCESS_READ_BIT, |
| 657 | .flags = 0, |
| 658 | }; |
| 659 | XGL_MEMORY_ALLOC_INFO mem_alloc = { |
| 660 | .sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_INFO, |
| 661 | .pNext = NULL, |
| 662 | .allocationSize = 0, |
| 663 | .alignment = 0, |
| 664 | .flags = 0, |
| 665 | .heapCount = 0, |
| 666 | .memPriority = XGL_MEMORY_PRIORITY_NORMAL, |
| 667 | }; |
| 668 | XGL_IMAGE_VIEW_CREATE_INFO view = { |
| 669 | .sType = XGL_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO, |
| 670 | .pNext = NULL, |
| 671 | .image = XGL_NULL_HANDLE, |
| 672 | .viewType = XGL_IMAGE_VIEW_2D, |
| 673 | .format = image.format, |
| 674 | .channels = { XGL_CHANNEL_SWIZZLE_R, |
| 675 | XGL_CHANNEL_SWIZZLE_G, |
| 676 | XGL_CHANNEL_SWIZZLE_B, |
| 677 | XGL_CHANNEL_SWIZZLE_A, }, |
| 678 | .subresourceRange = { XGL_IMAGE_ASPECT_COLOR, 0, 1, 0, 1 }, |
| 679 | .minLod = 0.0f, |
| 680 | }; |
| 681 | XGL_MEMORY_REQUIREMENTS mem_reqs; |
| 682 | XGL_SIZE mem_reqs_size; |
| 683 | |
| 684 | /* create sampler */ |
| 685 | err = xglCreateSampler(demo->device, &sampler, |
| 686 | &demo->textures[i].sampler); |
| 687 | assert(!err); |
| 688 | |
| 689 | /* create image */ |
| 690 | err = xglCreateImage(demo->device, &image, |
| 691 | &demo->textures[i].image); |
| 692 | assert(!err); |
| 693 | |
| 694 | err = xglGetObjectInfo(demo->textures[i].image, |
| 695 | XGL_INFO_TYPE_MEMORY_REQUIREMENTS, |
| 696 | &mem_reqs_size, &mem_reqs); |
| 697 | assert(!err && mem_reqs_size == sizeof(mem_reqs)); |
| 698 | |
| 699 | mem_alloc.allocationSize = mem_reqs.size; |
| 700 | mem_alloc.alignment = mem_reqs.alignment; |
| 701 | mem_alloc.heapCount = mem_reqs.heapCount; |
| 702 | memcpy(mem_alloc.heaps, mem_reqs.heaps, |
| 703 | sizeof(mem_reqs.heaps[0]) * mem_reqs.heapCount); |
| 704 | |
| 705 | /* allocate memory */ |
| 706 | err = xglAllocMemory(demo->device, &mem_alloc, |
| 707 | &demo->textures[i].mem); |
| 708 | assert(!err); |
| 709 | |
| 710 | /* bind memory */ |
| 711 | err = xglBindObjectMemory(demo->textures[i].image, |
| 712 | demo->textures[i].mem, 0); |
| 713 | assert(!err); |
| 714 | |
| 715 | /* create image view */ |
| 716 | view.image = demo->textures[i].image; |
| 717 | err = xglCreateImageView(demo->device, &view, |
| 718 | &demo->textures[i].view); |
| 719 | assert(!err); |
| 720 | } |
| 721 | |
| 722 | for (i = 0; i < DEMO_TEXTURE_COUNT; i++) { |
| 723 | const XGL_IMAGE_SUBRESOURCE subres = { |
| 724 | .aspect = XGL_IMAGE_ASPECT_COLOR, |
| 725 | .mipLevel = 0, |
| 726 | .arraySlice = 0, |
| 727 | }; |
| 728 | XGL_SUBRESOURCE_LAYOUT layout; |
| 729 | XGL_SIZE layout_size; |
| 730 | XGL_VOID *data; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 731 | |
| 732 | err = xglGetImageSubresourceInfo(demo->textures[i].image, &subres, |
| 733 | XGL_INFO_TYPE_SUBRESOURCE_LAYOUT, &layout_size, &layout); |
| 734 | assert(!err && layout_size == sizeof(layout)); |
| 735 | |
| 736 | err = xglMapMemory(demo->textures[i].mem, 0, &data); |
| 737 | assert(!err); |
| 738 | |
Courtney Goeltzenleuchter | 6836939 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 739 | loadTexture(tex_files[i], data, &layout, &tex_width, &tex_height); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 740 | |
| 741 | err = xglUnmapMemory(demo->textures[i].mem); |
| 742 | assert(!err); |
| 743 | } |
| 744 | } |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 745 | |
Courtney Goeltzenleuchter | 4984d15 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 746 | void demo_prepare_cube_data_buffer(struct demo *demo) |
| 747 | { |
| 748 | XGL_MEMORY_ALLOC_INFO alloc_info; |
| 749 | XGL_UINT8 *pData; |
| 750 | int i; |
| 751 | mat4x4 MVP, VP; |
Courtney Goeltzenleuchter | 4984d15 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 752 | XGL_RESULT err; |
| 753 | struct xgltexcube_vs_uniform data; |
| 754 | |
Courtney Goeltzenleuchter | 4984d15 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 755 | mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix); |
Courtney Goeltzenleuchter | 4984d15 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 756 | mat4x4_mul(MVP, VP, demo->model_matrix); |
| 757 | memcpy(data.mvp, MVP, sizeof(MVP)); |
Courtney Goeltzenleuchter | 8879d3a | 2014-10-29 08:29:35 -0600 | [diff] [blame^] | 758 | // dumpMatrix("MVP", MVP); |
Courtney Goeltzenleuchter | 4984d15 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 759 | |
| 760 | for (i=0; i<12*3; i++) { |
| 761 | data.position[i][0] = g_vertex_buffer_data[i*3]; |
| 762 | data.position[i][1] = g_vertex_buffer_data[i*3+1]; |
| 763 | data.position[i][2] = g_vertex_buffer_data[i*3+2]; |
| 764 | data.position[i][3] = 1.0f; |
| 765 | data.attr[i][0] = g_uv_buffer_data[2*i]; |
| 766 | data.attr[i][1] = g_uv_buffer_data[2*i + 1]; |
| 767 | data.attr[i][2] = 0; |
| 768 | data.attr[i][3] = 0; |
| 769 | } |
| 770 | |
| 771 | memset(&alloc_info, 0, sizeof(alloc_info)); |
| 772 | alloc_info.sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_INFO; |
| 773 | alloc_info.allocationSize = sizeof(data); |
| 774 | alloc_info.alignment = 0; |
| 775 | alloc_info.heapCount = 1; |
| 776 | alloc_info.heaps[0] = 0; // TODO: Use known existing heap |
| 777 | |
| 778 | alloc_info.flags = XGL_MEMORY_HEAP_CPU_VISIBLE_BIT; |
| 779 | alloc_info.memPriority = XGL_MEMORY_PRIORITY_NORMAL; |
| 780 | |
| 781 | err = xglAllocMemory(demo->device, &alloc_info, &demo->uniform_data.mem); |
| 782 | assert(!err); |
| 783 | |
| 784 | err = xglMapMemory(demo->uniform_data.mem, 0, (XGL_VOID **) &pData); |
| 785 | assert(!err); |
| 786 | |
| 787 | memcpy(pData, &data, alloc_info.allocationSize); |
| 788 | |
| 789 | err = xglUnmapMemory(demo->uniform_data.mem); |
| 790 | assert(!err); |
| 791 | |
| 792 | // set up the memory view for the constant buffer |
| 793 | demo->uniform_data.view.sType = XGL_STRUCTURE_TYPE_MEMORY_VIEW_ATTACH_INFO; |
| 794 | demo->uniform_data.view.stride = 16; |
| 795 | demo->uniform_data.view.range = alloc_info.allocationSize; |
| 796 | demo->uniform_data.view.offset = 0; |
| 797 | demo->uniform_data.view.mem = demo->uniform_data.mem; |
| 798 | demo->uniform_data.view.format.channelFormat = XGL_CH_FMT_R32G32B32A32; |
| 799 | demo->uniform_data.view.format.numericFormat = XGL_NUM_FMT_FLOAT; |
| 800 | } |
| 801 | |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 802 | static void demo_prepare_descriptor_set(struct demo *demo) |
| 803 | { |
| 804 | const XGL_DESCRIPTOR_SET_CREATE_INFO descriptor_set = { |
| 805 | .sType = XGL_STRUCTURE_TYPE_DESCRIPTOR_SET_CREATE_INFO, |
| 806 | .pNext = NULL, |
Courtney Goeltzenleuchter | fc3b953 | 2014-10-28 10:32:57 -0600 | [diff] [blame] | 807 | .slots = DEMO_TEXTURE_COUNT * 2 + 2, |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 808 | }; |
| 809 | XGL_RESULT err; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 810 | |
| 811 | err = xglCreateDescriptorSet(demo->device, &descriptor_set, &demo->dset); |
| 812 | assert(!err); |
| 813 | |
| 814 | xglBeginDescriptorSetUpdate(demo->dset); |
Courtney Goeltzenleuchter | fc3b953 | 2014-10-28 10:32:57 -0600 | [diff] [blame] | 815 | xglClearDescriptorSetSlots(demo->dset, 0, DEMO_TEXTURE_COUNT * 2 + 2); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 816 | |
Courtney Goeltzenleuchter | 4984d15 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 817 | // xglAttachMemoryViewDescriptors(demo->dset, 0, 1, &demo->vertices.view); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 818 | |
Courtney Goeltzenleuchter | 4984d15 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 819 | xglAttachMemoryViewDescriptors(demo->dset, 0, 1, &demo->uniform_data.view); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 820 | |
Courtney Goeltzenleuchter | fc3b953 | 2014-10-28 10:32:57 -0600 | [diff] [blame] | 821 | XGL_IMAGE_VIEW_ATTACH_INFO image_view; |
| 822 | |
| 823 | image_view.sType = XGL_STRUCTURE_TYPE_IMAGE_VIEW_ATTACH_INFO; |
| 824 | image_view.pNext = NULL; |
| 825 | image_view.view = demo->textures[0].view; |
| 826 | image_view.state = XGL_IMAGE_STATE_GRAPHICS_SHADER_READ_ONLY; |
| 827 | |
Courtney Goeltzenleuchter | 4984d15 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 828 | xglAttachSamplerDescriptors(demo->dset, 1, 1, &demo->textures[0].sampler); |
| 829 | xglAttachImageViewDescriptors(demo->dset, 2, 1, &image_view); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 830 | |
| 831 | xglEndDescriptorSetUpdate(demo->dset); |
| 832 | } |
| 833 | |
Courtney Goeltzenleuchter | 6836939 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 834 | #define USE_BIL |
| 835 | |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 836 | static XGL_SHADER demo_prepare_shader(struct demo *demo, |
| 837 | XGL_PIPELINE_SHADER_STAGE stage, |
| 838 | const void *code, |
| 839 | XGL_SIZE size) |
| 840 | { |
| 841 | XGL_SHADER_CREATE_INFO createInfo; |
| 842 | XGL_SHADER shader; |
| 843 | XGL_RESULT err; |
| 844 | |
Courtney Goeltzenleuchter | 6836939 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 845 | #ifdef USE_BIL |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 846 | createInfo.sType = XGL_STRUCTURE_TYPE_SHADER_CREATE_INFO; |
| 847 | createInfo.pNext = NULL; |
| 848 | |
Courtney Goeltzenleuchter | 6836939 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 849 | createInfo.codeSize = 3 * sizeof(uint32_t) + size + 1; |
| 850 | createInfo.pCode = malloc(createInfo.codeSize); |
| 851 | createInfo.flags = 0; |
| 852 | |
| 853 | /* try version 0 first: XGL_PIPELINE_SHADER_STAGE followed by GLSL */ |
| 854 | ((uint32_t *) createInfo.pCode)[0] = ICD_BIL_MAGIC; |
| 855 | ((uint32_t *) createInfo.pCode)[1] = 0; |
| 856 | ((uint32_t *) createInfo.pCode)[2] = stage; |
| 857 | memcpy(((uint32_t *) createInfo.pCode + 3), code, size + 1); |
| 858 | |
| 859 | err = xglCreateShader(demo->device, &createInfo, &shader); |
| 860 | if (err) { |
| 861 | free((void *) createInfo.pCode); |
| 862 | } |
| 863 | #else |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 864 | // Create fake BIL structure to feed GLSL |
| 865 | // to the driver "under the covers" |
| 866 | createInfo.codeSize = 3 * sizeof(uint32_t) + size + 1; |
| 867 | createInfo.pCode = malloc(createInfo.codeSize); |
| 868 | createInfo.flags = 0; |
| 869 | |
| 870 | /* try version 0 first: XGL_PIPELINE_SHADER_STAGE followed by GLSL */ |
| 871 | ((uint32_t *) createInfo.pCode)[0] = ICD_BIL_MAGIC; |
| 872 | ((uint32_t *) createInfo.pCode)[1] = 0; |
| 873 | ((uint32_t *) createInfo.pCode)[2] = stage; |
| 874 | memcpy(((uint32_t *) createInfo.pCode + 3), code, size + 1); |
| 875 | |
| 876 | err = xglCreateShader(demo->device, &createInfo, &shader); |
| 877 | if (err) { |
| 878 | free((void *) createInfo.pCode); |
Courtney Goeltzenleuchter | fc3b953 | 2014-10-28 10:32:57 -0600 | [diff] [blame] | 879 | return NULL; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 880 | } |
Courtney Goeltzenleuchter | 6836939 | 2014-10-29 15:59:49 -0600 | [diff] [blame] | 881 | #endif |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 882 | |
| 883 | return shader; |
| 884 | } |
| 885 | |
| 886 | static XGL_SHADER demo_prepare_vs(struct demo *demo) |
| 887 | { |
Courtney Goeltzenleuchter | 8879d3a | 2014-10-29 08:29:35 -0600 | [diff] [blame^] | 888 | static const char *vertShaderText = |
Courtney Goeltzenleuchter | 4984d15 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 889 | "#version 140\n" |
| 890 | "#extension GL_ARB_separate_shader_objects : enable\n" |
| 891 | "#extension GL_ARB_shading_language_420pack : enable\n" |
| 892 | "\n" |
| 893 | "layout(binding = 0) uniform buf {\n" |
| 894 | " mat4 MVP;\n" |
| 895 | " vec4 position[12*3];\n" |
| 896 | " vec4 attr[12*3];\n" |
| 897 | "} ubuf;\n" |
| 898 | "\n" |
| 899 | "layout (location = 0) out vec4 texcoord;\n" |
Courtney Goeltzenleuchter | 4984d15 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 900 | "\n" |
| 901 | "void main() \n" |
| 902 | "{\n" |
| 903 | " texcoord = ubuf.attr[gl_VertexID];\n" |
Courtney Goeltzenleuchter | 4984d15 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 904 | " gl_Position = ubuf.MVP * ubuf.position[gl_VertexID];\n" |
| 905 | "}\n"; |
Courtney Goeltzenleuchter | fc3b953 | 2014-10-28 10:32:57 -0600 | [diff] [blame] | 906 | |
Courtney Goeltzenleuchter | 8879d3a | 2014-10-29 08:29:35 -0600 | [diff] [blame^] | 907 | return demo_prepare_shader(demo, XGL_SHADER_STAGE_VERTEX, |
| 908 | (const void *) vertShaderText, |
| 909 | strlen(vertShaderText)); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 910 | } |
| 911 | |
| 912 | static XGL_SHADER demo_prepare_fs(struct demo *demo) |
| 913 | { |
Courtney Goeltzenleuchter | 8879d3a | 2014-10-29 08:29:35 -0600 | [diff] [blame^] | 914 | static const char *fragShaderText = |
Courtney Goeltzenleuchter | 4984d15 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 915 | "#version 140\n" |
| 916 | "#extension GL_ARB_separate_shader_objects : enable\n" |
| 917 | "#extension GL_ARB_shading_language_420pack : enable\n" |
| 918 | "uniform sampler2D tex;\n" |
| 919 | "\n" |
| 920 | "layout (location = 0) in vec4 texcoord;\n" |
| 921 | "void main() {\n" |
| 922 | " gl_FragColor = texture(tex, texcoord.xy);\n" |
| 923 | "}\n"; |
Courtney Goeltzenleuchter | fc3b953 | 2014-10-28 10:32:57 -0600 | [diff] [blame] | 924 | |
Courtney Goeltzenleuchter | 8879d3a | 2014-10-29 08:29:35 -0600 | [diff] [blame^] | 925 | return demo_prepare_shader(demo, XGL_SHADER_STAGE_FRAGMENT, |
| 926 | (const void *) fragShaderText, |
| 927 | strlen(fragShaderText)); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 928 | } |
| 929 | |
| 930 | static void demo_prepare_pipeline(struct demo *demo) |
| 931 | { |
| 932 | XGL_GRAPHICS_PIPELINE_CREATE_INFO pipeline; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 933 | XGL_PIPELINE_IA_STATE_CREATE_INFO ia; |
| 934 | XGL_PIPELINE_RS_STATE_CREATE_INFO rs; |
| 935 | XGL_PIPELINE_CB_STATE cb; |
| 936 | XGL_PIPELINE_DB_STATE_CREATE_INFO db; |
| 937 | XGL_PIPELINE_SHADER_STAGE_CREATE_INFO vs; |
| 938 | XGL_PIPELINE_SHADER_STAGE_CREATE_INFO fs; |
Courtney Goeltzenleuchter | 4984d15 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 939 | XGL_DESCRIPTOR_SLOT_INFO vs_slots[3]; |
| 940 | XGL_DESCRIPTOR_SLOT_INFO fs_slots[3]; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 941 | XGL_RESULT err; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 942 | |
| 943 | memset(&pipeline, 0, sizeof(pipeline)); |
| 944 | pipeline.sType = XGL_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO; |
| 945 | |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 946 | memset(&ia, 0, sizeof(ia)); |
| 947 | ia.sType = XGL_STRUCTURE_TYPE_PIPELINE_IA_STATE_CREATE_INFO; |
| 948 | ia.topology = XGL_TOPOLOGY_TRIANGLE_LIST; |
| 949 | |
| 950 | memset(&rs, 0, sizeof(rs)); |
| 951 | rs.sType = XGL_STRUCTURE_TYPE_PIPELINE_RS_STATE_CREATE_INFO; |
| 952 | |
| 953 | memset(&cb, 0, sizeof(cb)); |
| 954 | cb.sType = XGL_STRUCTURE_TYPE_PIPELINE_CB_STATE_CREATE_INFO; |
| 955 | cb.attachment[0].format = demo->format; |
| 956 | cb.attachment[0].channelWriteMask = 0xf; |
| 957 | |
| 958 | memset(&db, 0, sizeof(db)); |
| 959 | db.sType = XGL_STRUCTURE_TYPE_PIPELINE_DB_STATE_CREATE_INFO; |
| 960 | db.format = demo->depth.format; |
| 961 | |
| 962 | memset(&vs_slots, 0, sizeof(vs_slots)); |
Courtney Goeltzenleuchter | 4984d15 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 963 | vs_slots[0].slotObjectType = XGL_SLOT_SHADER_RESOURCE; |
| 964 | vs_slots[0].shaderEntityIndex = 0; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 965 | |
| 966 | memset(&fs_slots, 0, sizeof(fs_slots)); |
Courtney Goeltzenleuchter | 4984d15 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 967 | fs_slots[1].slotObjectType = XGL_SLOT_SHADER_SAMPLER; |
| 968 | fs_slots[1].shaderEntityIndex = 0; |
| 969 | fs_slots[2].slotObjectType = XGL_SLOT_SHADER_RESOURCE; |
| 970 | fs_slots[2].shaderEntityIndex = 0; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 971 | |
| 972 | memset(&vs, 0, sizeof(vs)); |
| 973 | vs.sType = XGL_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO; |
| 974 | vs.shader.stage = XGL_SHADER_STAGE_VERTEX; |
Courtney Goeltzenleuchter | 4984d15 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 975 | vs.shader.shader = demo_prepare_vs(demo); |
Courtney Goeltzenleuchter | fc3b953 | 2014-10-28 10:32:57 -0600 | [diff] [blame] | 976 | assert(vs.shader.shader != NULL); |
Courtney Goeltzenleuchter | 4984d15 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 977 | vs.shader.descriptorSetMapping[0].descriptorCount = 3; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 978 | vs.shader.descriptorSetMapping[0].pDescriptorInfo = vs_slots; |
| 979 | |
| 980 | memset(&fs, 0, sizeof(fs)); |
| 981 | fs.sType = XGL_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO; |
| 982 | fs.shader.stage = XGL_SHADER_STAGE_FRAGMENT; |
| 983 | fs.shader.shader = demo_prepare_fs(demo); |
Courtney Goeltzenleuchter | fc3b953 | 2014-10-28 10:32:57 -0600 | [diff] [blame] | 984 | assert(fs.shader.shader != NULL); |
Courtney Goeltzenleuchter | 4984d15 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 985 | fs.shader.descriptorSetMapping[0].descriptorCount = 3; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 986 | fs.shader.descriptorSetMapping[0].pDescriptorInfo = fs_slots; |
| 987 | |
Courtney Goeltzenleuchter | 4984d15 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 988 | pipeline.pNext = (const XGL_VOID *) &ia; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 989 | ia.pNext = (const XGL_VOID *) &rs; |
| 990 | rs.pNext = (const XGL_VOID *) &cb; |
| 991 | cb.pNext = (const XGL_VOID *) &db; |
| 992 | db.pNext = (const XGL_VOID *) &vs; |
| 993 | vs.pNext = (const XGL_VOID *) &fs; |
| 994 | |
| 995 | err = xglCreateGraphicsPipeline(demo->device, &pipeline, &demo->pipeline); |
| 996 | assert(!err); |
| 997 | |
| 998 | xglDestroyObject(vs.shader.shader); |
| 999 | xglDestroyObject(fs.shader.shader); |
| 1000 | } |
| 1001 | |
| 1002 | static void demo_prepare_dynamic_states(struct demo *demo) |
| 1003 | { |
| 1004 | XGL_VIEWPORT_STATE_CREATE_INFO viewport; |
| 1005 | XGL_RASTER_STATE_CREATE_INFO raster; |
| 1006 | XGL_MSAA_STATE_CREATE_INFO msaa; |
| 1007 | XGL_COLOR_BLEND_STATE_CREATE_INFO color_blend; |
| 1008 | XGL_DEPTH_STENCIL_STATE_CREATE_INFO depth_stencil; |
| 1009 | XGL_RESULT err; |
| 1010 | |
| 1011 | memset(&viewport, 0, sizeof(viewport)); |
| 1012 | viewport.viewportCount = 1; |
| 1013 | viewport.scissorEnable = XGL_FALSE; |
| 1014 | viewport.viewports[0].width = (XGL_FLOAT) demo->width; |
| 1015 | viewport.viewports[0].height = (XGL_FLOAT) demo->height; |
| 1016 | viewport.viewports[0].minDepth = (XGL_FLOAT) 0.0f; |
| 1017 | viewport.viewports[0].maxDepth = (XGL_FLOAT) 1.0f; |
| 1018 | |
| 1019 | memset(&raster, 0, sizeof(raster)); |
| 1020 | raster.sType = XGL_STRUCTURE_TYPE_RASTER_STATE_CREATE_INFO; |
| 1021 | raster.fillMode = XGL_FILL_SOLID; |
| 1022 | raster.cullMode = XGL_CULL_NONE; |
| 1023 | raster.frontFace = XGL_FRONT_FACE_CCW; |
| 1024 | |
| 1025 | memset(&msaa, 0, sizeof(msaa)); |
| 1026 | msaa.sType = XGL_STRUCTURE_TYPE_MSAA_STATE_CREATE_INFO; |
| 1027 | msaa.samples = 1; |
| 1028 | msaa.sampleMask = 0x1; |
| 1029 | |
| 1030 | memset(&color_blend, 0, sizeof(color_blend)); |
| 1031 | color_blend.sType = XGL_STRUCTURE_TYPE_COLOR_BLEND_STATE_CREATE_INFO; |
| 1032 | |
| 1033 | memset(&depth_stencil, 0, sizeof(depth_stencil)); |
| 1034 | depth_stencil.sType = XGL_STRUCTURE_TYPE_DEPTH_STENCIL_STATE_CREATE_INFO; |
| 1035 | depth_stencil.depthTestEnable = XGL_TRUE; |
| 1036 | depth_stencil.depthWriteEnable = XGL_TRUE; |
| 1037 | depth_stencil.depthFunc = XGL_COMPARE_LESS_EQUAL; |
| 1038 | depth_stencil.depthBoundsEnable = XGL_FALSE; |
| 1039 | |
| 1040 | err = xglCreateViewportState(demo->device, &viewport, &demo->viewport); |
| 1041 | assert(!err); |
| 1042 | |
| 1043 | err = xglCreateRasterState(demo->device, &raster, &demo->raster); |
| 1044 | assert(!err); |
| 1045 | |
| 1046 | err = xglCreateMsaaState(demo->device, &msaa, &demo->msaa); |
| 1047 | assert(!err); |
| 1048 | |
| 1049 | err = xglCreateColorBlendState(demo->device, |
| 1050 | &color_blend, &demo->color_blend); |
| 1051 | assert(!err); |
| 1052 | |
| 1053 | err = xglCreateDepthStencilState(demo->device, |
| 1054 | &depth_stencil, &demo->depth_stencil); |
| 1055 | assert(!err); |
| 1056 | } |
| 1057 | |
| 1058 | static void demo_prepare(struct demo *demo) |
| 1059 | { |
| 1060 | const XGL_CMD_BUFFER_CREATE_INFO cmd = { |
| 1061 | .sType = XGL_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO, |
| 1062 | .pNext = NULL, |
| 1063 | .queueType = XGL_QUEUE_TYPE_GRAPHICS, |
| 1064 | .flags = 0, |
| 1065 | }; |
| 1066 | XGL_RESULT err; |
| 1067 | |
| 1068 | demo_prepare_buffers(demo); |
| 1069 | demo_prepare_depth(demo); |
| 1070 | demo_prepare_textures(demo); |
Courtney Goeltzenleuchter | 8879d3a | 2014-10-29 08:29:35 -0600 | [diff] [blame^] | 1071 | demo_prepare_cube_data_buffer(demo); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1072 | demo_prepare_descriptor_set(demo); |
| 1073 | |
| 1074 | demo_prepare_pipeline(demo); |
| 1075 | demo_prepare_dynamic_states(demo); |
| 1076 | |
| 1077 | err = xglCreateCommandBuffer(demo->device, &cmd, &demo->cmd); |
| 1078 | assert(!err); |
| 1079 | } |
| 1080 | |
| 1081 | static void demo_handle_event(struct demo *demo, |
| 1082 | const xcb_generic_event_t *event) |
| 1083 | { |
| 1084 | switch (event->response_type & 0x7f) { |
| 1085 | case XCB_EXPOSE: |
| 1086 | demo_draw(demo); |
| 1087 | break; |
| 1088 | case XCB_KEY_RELEASE: |
| 1089 | { |
| 1090 | const xcb_key_release_event_t *key = |
| 1091 | (const xcb_key_release_event_t *) event; |
| 1092 | |
Courtney Goeltzenleuchter | fc3b953 | 2014-10-28 10:32:57 -0600 | [diff] [blame] | 1093 | switch (key->detail) { |
| 1094 | case 0x9: // Escape |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1095 | demo->quit = true; |
Courtney Goeltzenleuchter | fc3b953 | 2014-10-28 10:32:57 -0600 | [diff] [blame] | 1096 | break; |
Courtney Goeltzenleuchter | 4984d15 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 1097 | case 0x71: // left arrow key |
Courtney Goeltzenleuchter | 8879d3a | 2014-10-29 08:29:35 -0600 | [diff] [blame^] | 1098 | demo->spin_angle += demo->spin_increment; |
Courtney Goeltzenleuchter | 4984d15 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 1099 | break; |
Courtney Goeltzenleuchter | fc3b953 | 2014-10-28 10:32:57 -0600 | [diff] [blame] | 1100 | case 0x72: // right arrow key |
Courtney Goeltzenleuchter | 8879d3a | 2014-10-29 08:29:35 -0600 | [diff] [blame^] | 1101 | demo->spin_angle -= demo->spin_increment; |
Courtney Goeltzenleuchter | fc3b953 | 2014-10-28 10:32:57 -0600 | [diff] [blame] | 1102 | break; |
Courtney Goeltzenleuchter | 8879d3a | 2014-10-29 08:29:35 -0600 | [diff] [blame^] | 1103 | case 0x41: |
| 1104 | demo->pause = !demo->pause; |
| 1105 | break; |
Courtney Goeltzenleuchter | fc3b953 | 2014-10-28 10:32:57 -0600 | [diff] [blame] | 1106 | } |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1107 | } |
| 1108 | break; |
| 1109 | default: |
| 1110 | break; |
| 1111 | } |
| 1112 | } |
| 1113 | |
| 1114 | static void demo_run(struct demo *demo) |
| 1115 | { |
| 1116 | xcb_flush(demo->connection); |
| 1117 | |
| 1118 | while (!demo->quit) { |
| 1119 | xcb_generic_event_t *event; |
| 1120 | |
Courtney Goeltzenleuchter | 8879d3a | 2014-10-29 08:29:35 -0600 | [diff] [blame^] | 1121 | event = xcb_poll_for_event(demo->connection); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1122 | if (event) { |
| 1123 | demo_handle_event(demo, event); |
| 1124 | free(event); |
Courtney Goeltzenleuchter | 8879d3a | 2014-10-29 08:29:35 -0600 | [diff] [blame^] | 1125 | } else if (!demo->pause){ |
| 1126 | // Wait for work to finish before updating MVP. |
| 1127 | xglDeviceWaitIdle(demo->device); |
| 1128 | |
| 1129 | demo_update_data_buffer(demo); |
| 1130 | |
| 1131 | demo_update_data_buffer(demo); |
| 1132 | |
| 1133 | demo_draw(demo); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1134 | } |
| 1135 | } |
| 1136 | } |
| 1137 | |
| 1138 | static void demo_create_window(struct demo *demo) |
| 1139 | { |
| 1140 | uint32_t value_mask, value_list[32]; |
| 1141 | |
| 1142 | demo->window = xcb_generate_id(demo->connection); |
| 1143 | |
| 1144 | value_mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK; |
| 1145 | value_list[0] = demo->screen->black_pixel; |
| 1146 | value_list[1] = XCB_EVENT_MASK_KEY_RELEASE | |
| 1147 | XCB_EVENT_MASK_EXPOSURE; |
| 1148 | |
| 1149 | xcb_create_window(demo->connection, |
| 1150 | XCB_COPY_FROM_PARENT, |
| 1151 | demo->window, demo->screen->root, |
| 1152 | 0, 0, demo->width, demo->height, 0, |
| 1153 | XCB_WINDOW_CLASS_INPUT_OUTPUT, |
| 1154 | demo->screen->root_visual, |
| 1155 | value_mask, value_list); |
| 1156 | |
| 1157 | xcb_map_window(demo->connection, demo->window); |
| 1158 | } |
| 1159 | |
| 1160 | static void demo_init_xgl(struct demo *demo) |
| 1161 | { |
| 1162 | const XGL_APPLICATION_INFO app = { |
| 1163 | .sType = XGL_STRUCTURE_TYPE_APPLICATION_INFO, |
| 1164 | .pNext = NULL, |
| 1165 | .pAppName = (const XGL_CHAR *) "cube", |
| 1166 | .appVersion = 0, |
| 1167 | .pEngineName = (const XGL_CHAR *) "cube", |
| 1168 | .engineVersion = 0, |
| 1169 | .apiVersion = XGL_MAKE_VERSION(0, 22, 0), |
| 1170 | }; |
| 1171 | const XGL_WSI_X11_CONNECTION_INFO connection = { |
| 1172 | .pConnection = demo->connection, |
| 1173 | .root = demo->screen->root, |
| 1174 | .provider = 0, |
| 1175 | }; |
| 1176 | const XGL_DEVICE_QUEUE_CREATE_INFO queue = { |
| 1177 | .queueNodeIndex = 0, |
| 1178 | .queueCount = 1, |
| 1179 | }; |
| 1180 | const XGL_CHAR *ext_names[] = { |
| 1181 | (const XGL_CHAR *) "XGL_WSI_X11", |
| 1182 | }; |
| 1183 | const XGL_DEVICE_CREATE_INFO device = { |
| 1184 | .sType = XGL_STRUCTURE_TYPE_DEVICE_CREATE_INFO, |
| 1185 | .pNext = NULL, |
| 1186 | .queueRecordCount = 1, |
| 1187 | .pRequestedQueues = &queue, |
| 1188 | .extensionCount = 1, |
| 1189 | .ppEnabledExtensionNames = ext_names, |
| 1190 | .maxValidationLevel = XGL_VALIDATION_LEVEL_END_RANGE, |
| 1191 | .flags = XGL_DEVICE_CREATE_VALIDATION_BIT, |
| 1192 | }; |
| 1193 | XGL_RESULT err; |
| 1194 | XGL_UINT gpu_count; |
| 1195 | XGL_UINT i; |
| 1196 | |
| 1197 | err = xglInitAndEnumerateGpus(&app, NULL, 1, &gpu_count, &demo->gpu); |
| 1198 | assert(!err && gpu_count == 1); |
| 1199 | |
| 1200 | for (i = 0; i < device.extensionCount; i++) { |
| 1201 | err = xglGetExtensionSupport(demo->gpu, ext_names[i]); |
| 1202 | assert(!err); |
| 1203 | } |
| 1204 | |
| 1205 | err = xglWsiX11AssociateConnection(demo->gpu, &connection); |
| 1206 | assert(!err); |
| 1207 | |
| 1208 | err = xglCreateDevice(demo->gpu, &device, &demo->device); |
| 1209 | assert(!err); |
| 1210 | |
| 1211 | err = xglGetDeviceQueue(demo->device, XGL_QUEUE_TYPE_GRAPHICS, |
| 1212 | 0, &demo->queue); |
| 1213 | assert(!err); |
| 1214 | } |
| 1215 | |
| 1216 | static void demo_init_connection(struct demo *demo) |
| 1217 | { |
| 1218 | const xcb_setup_t *setup; |
| 1219 | xcb_screen_iterator_t iter; |
| 1220 | int scr; |
| 1221 | |
| 1222 | demo->connection = xcb_connect(NULL, &scr); |
| 1223 | |
| 1224 | setup = xcb_get_setup(demo->connection); |
| 1225 | iter = xcb_setup_roots_iterator(setup); |
| 1226 | while (scr-- > 0) |
| 1227 | xcb_screen_next(&iter); |
| 1228 | |
| 1229 | demo->screen = iter.data; |
| 1230 | } |
| 1231 | |
| 1232 | static void demo_init(struct demo *demo) |
| 1233 | { |
Courtney Goeltzenleuchter | 8879d3a | 2014-10-29 08:29:35 -0600 | [diff] [blame^] | 1234 | vec3 eye = {0.0f, 3.0f, 5.0f}; |
| 1235 | vec3 origin = {0, 0, 0}; |
| 1236 | vec3 up = {0.0f, -1.0f, 0.0}; |
| 1237 | |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1238 | memset(demo, 0, sizeof(*demo)); |
| 1239 | |
| 1240 | demo_init_connection(demo); |
| 1241 | demo_init_xgl(demo); |
| 1242 | |
Courtney Goeltzenleuchter | 8879d3a | 2014-10-29 08:29:35 -0600 | [diff] [blame^] | 1243 | demo->width = 500; |
| 1244 | demo->height = 500; |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1245 | demo->format.channelFormat = XGL_CH_FMT_B8G8R8A8; |
| 1246 | demo->format.numericFormat = XGL_NUM_FMT_UNORM; |
Courtney Goeltzenleuchter | 8879d3a | 2014-10-29 08:29:35 -0600 | [diff] [blame^] | 1247 | |
| 1248 | demo->spin_angle = 0.01f; |
| 1249 | demo->spin_increment = 0.01f; |
| 1250 | demo->pause = false; |
| 1251 | |
| 1252 | mat4x4_perspective(demo->projection_matrix, degreesToRadians(45.0f), 1.0f, 0.1f, 100.0f); |
| 1253 | mat4x4_look_at(demo->view_matrix, eye, origin, up); |
| 1254 | mat4x4_identity(demo->model_matrix); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1255 | } |
| 1256 | |
| 1257 | static void demo_cleanup(struct demo *demo) |
| 1258 | { |
| 1259 | XGL_UINT i; |
| 1260 | |
| 1261 | xglDestroyObject(demo->cmd); |
| 1262 | |
| 1263 | xglDestroyObject(demo->viewport); |
| 1264 | xglDestroyObject(demo->raster); |
| 1265 | xglDestroyObject(demo->msaa); |
| 1266 | xglDestroyObject(demo->color_blend); |
| 1267 | xglDestroyObject(demo->depth_stencil); |
| 1268 | |
| 1269 | xglDestroyObject(demo->pipeline); |
| 1270 | |
| 1271 | xglDestroyObject(demo->dset); |
| 1272 | |
Courtney Goeltzenleuchter | 4984d15 | 2014-10-28 14:50:30 -0600 | [diff] [blame] | 1273 | // xglFreeMemory(demo->vertices.mem); |
Courtney Goeltzenleuchter | 7be88a8 | 2014-10-23 13:16:59 -0600 | [diff] [blame] | 1274 | |
| 1275 | for (i = 0; i < DEMO_TEXTURE_COUNT; i++) { |
| 1276 | xglDestroyObject(demo->textures[i].view); |
| 1277 | xglDestroyObject(demo->textures[i].image); |
| 1278 | xglFreeMemory(demo->textures[i].mem); |
| 1279 | xglDestroyObject(demo->textures[i].sampler); |
| 1280 | } |
| 1281 | |
| 1282 | xglDestroyObject(demo->depth.view); |
| 1283 | xglDestroyObject(demo->depth.image); |
| 1284 | xglFreeMemory(demo->depth.mem); |
| 1285 | |
| 1286 | for (i = 0; i < DEMO_BUFFER_COUNT; i++) { |
| 1287 | xglDestroyObject(demo->buffers[i].view); |
| 1288 | xglDestroyObject(demo->buffers[i].image); |
| 1289 | } |
| 1290 | |
| 1291 | xglDestroyDevice(demo->device); |
| 1292 | |
| 1293 | xcb_destroy_window(demo->connection, demo->window); |
| 1294 | xcb_disconnect(demo->connection); |
| 1295 | } |
| 1296 | |
| 1297 | int main(void) |
| 1298 | { |
| 1299 | struct demo demo; |
| 1300 | |
| 1301 | demo_init(&demo); |
| 1302 | |
| 1303 | demo_prepare(&demo); |
| 1304 | demo_create_window(&demo); |
| 1305 | demo_run(&demo); |
| 1306 | |
| 1307 | demo_cleanup(&demo); |
| 1308 | |
| 1309 | return 0; |
| 1310 | } |