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