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