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