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