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