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