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