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