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