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