blob: bb10d987ac84e11801f7cc72efe55af1a36962bc [file] [log] [blame]
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001#define _GNU_SOURCE
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06002#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
13#include "icd-bil.h"
14
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -060015#include "linmath.h"
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -060016#include <png.h>
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -060017
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060018#define DEMO_BUFFER_COUNT 2
19#define DEMO_TEXTURE_COUNT 1
20
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -060021static char *tex_files[] = {
22 "demos/lunarg-logo-256x256-solid.png"
23};
24
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -060025// HACK
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -060026bool do_tri = false;
27
28struct xgltriangle_vs_uniform {
29 // Must start with MVP
30 XGL_FLOAT mvp[4][4];
31 XGL_FLOAT position[3][4];
32 XGL_FLOAT color[3][4];
33};
34
35struct xgltextriangle_vs_uniform {
36 // Must start with MVP
37 XGL_FLOAT mvp[4][4];
38 XGL_FLOAT position[3][4];
39 XGL_FLOAT attr[3][4];
40};
41
42struct xglcube_vs_uniform {
43 // Must start with MVP
44 XGL_FLOAT mvp[4][4];
45 XGL_FLOAT position[12*3][4];
46 XGL_FLOAT color[12*3][4];
47};
48
49struct xgltexcube_vs_uniform {
50 // Must start with MVP
51 XGL_FLOAT mvp[4][4];
52 XGL_FLOAT position[12*3][4];
53 XGL_FLOAT attr[12*3][4];
54};
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -060055
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060056//--------------------------------------------------------------------------------------
57// Mesh and VertexFormat Data
58//--------------------------------------------------------------------------------------
59struct Vertex
60{
61 XGL_FLOAT posX, posY, posZ, posW; // Position data
62 XGL_FLOAT r, g, b, a; // Color
63};
64
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -060065struct VertexPosTex
66{
67 XGL_FLOAT posX, posY, posZ, posW; // Position data
68 XGL_FLOAT u, v, s, t; // Texcoord
69};
70
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060071#define XYZ1(_x_, _y_, _z_) (_x_), (_y_), (_z_), 1.f
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -060072#define UV(_u_, _v_) (_u_), (_v_), 0.f, 1.f
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -060073
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -060074static const XGL_FLOAT g_vertex_buffer_data[] = {
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -060075 -1.0f,-1.0f,-1.0f, // Vertex 0
76 -1.0f,-1.0f, 1.0f,
77 -1.0f, 1.0f, 1.0f,
78
79 -1.0f, 1.0f, 1.0f, // Vertex 1
80 -1.0f, 1.0f,-1.0f,
81 -1.0f,-1.0f,-1.0f,
82
83 -1.0f,-1.0f,-1.0f, // Vertex 2
84 1.0f, 1.0f,-1.0f,
85 1.0f,-1.0f,-1.0f,
86
87 -1.0f,-1.0f,-1.0f, // Vertex 3
88 1.0f, 1.0f,-1.0f,
89 -1.0f, 1.0f,-1.0f,
90
91 -1.0f,-1.0f,-1.0f, // Vertex 4
92 1.0f,-1.0f, 1.0f,
93 1.0f,-1.0f,-1.0f,
94
95 -1.0f,-1.0f,-1.0f, // Vertex 5
96 -1.0f,-1.0f, 1.0f,
97 1.0f,-1.0f, 1.0f,
98
99 -1.0f, 1.0f,-1.0f, // Vertex 6
100 -1.0f, 1.0f, 1.0f,
101 1.0f, 1.0f, 1.0f,
102
103 -1.0f, 1.0f,-1.0f, // Vertex 7
104 1.0f, 1.0f,-1.0f,
105 1.0f, 1.0f, 1.0f,
106
107 1.0f, 1.0f,-1.0f, // Vertex 8
108 1.0f, 1.0f, 1.0f,
109 1.0f,-1.0f, 1.0f,
110
111 1.0f,-1.0f, 1.0f, // Vertex 9
112 1.0f,-1.0f,-1.0f,
113 1.0f, 1.0f,-1.0f,
114
115 -1.0f, 1.0f, 1.0f, // Vertex 10
116 1.0f, 1.0f, 1.0f,
117 -1.0f,-1.0f, 1.0f,
118
119 -1.0f,-1.0f, 1.0f, // Vertex 11
120 1.0f,-1.0f, 1.0f,
121 1.0f, 1.0f, 1.0f,
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600122};
123
124static const XGL_FLOAT g_uv_buffer_data[] = {
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600125 0.0f, 1.0f, // Vertex 0
126 1.0f, 1.0f,
127 1.0f, 0.0f,
128
129 1.0f, 0.0f, // Vertex 1
130 0.0f, 0.0f,
131 0.0f, 1.0f,
132
133 0.0f, 1.0f, // Vertex 2
134 1.0f, 0.0f,
135 0.0f, 0.0f,
136
137 0.0f, 1.0f, // Vertex 3
138 1.0f, 0.0f,
139 1.0f, 1.0f,
140
141 0.0f, 1.0f, // Vertex 4
142 1.0f, 0.0f,
143 0.0f, 0.0f,
144
145 0.0f, 1.0f, // Vertex 5
146 1.0f, 1.0f,
147 1.0f, 0.0f,
148
149 0.0f, 1.0f, // Vertex 6
150 1.0f, 1.0f,
151 1.0f, 0.0f,
152
153 0.0f, 1.0f, // Vertex 7
154 0.0f, 0.0f,
155 1.0f, 0.0f,
156
157 0.0f, 0.0f, // Vertex 8
158 0.0f, 1.0f,
159 1.0f, 1.0f,
160
161 1.0f, 1.0f, // Vertex 9
162 1.0f, 0.0f,
163 0.0f, 0.0f,
164
165 0.0f, 0.0f, // Vertex 10
166 1.0f, 0.0f,
167 0.0f, 1.0f,
168
169 0.0f, 1.0f, // Vertex 11
170 1.0f, 1.0f,
171 1.0f, 0.0f,
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600172};
173
174void dumpMatrix(const char *note, mat4x4 MVP)
175{
176 int i;
177
178 printf("%s: \n", note);
179 for (i=0; i<4; i++) {
180 printf("%f, %f, %f, %f\n", MVP[i][0], MVP[i][1], MVP[i][2], MVP[i][3]);
181 }
182 printf("\n");
183 fflush(stdout);
184}
185
186void dumpVec4(const char *note, vec4 vector)
187{
188 printf("%s: \n", note);
189 printf("%f, %f, %f, %f\n", vector[0], vector[1], vector[2], vector[3]);
190 printf("\n");
191 fflush(stdout);
192}
193
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600194struct demo {
195 xcb_connection_t *connection;
196 xcb_screen_t *screen;
197
198 XGL_PHYSICAL_GPU gpu;
199 XGL_DEVICE device;
200 XGL_QUEUE queue;
201
202 int width, height;
203 XGL_FORMAT format;
204
205 struct {
206 XGL_IMAGE image;
207 XGL_GPU_MEMORY mem;
208
209 XGL_COLOR_ATTACHMENT_VIEW view;
210 } buffers[DEMO_BUFFER_COUNT];
211
212 struct {
213 XGL_FORMAT format;
214
215 XGL_IMAGE image;
216 XGL_GPU_MEMORY mem;
217 XGL_DEPTH_STENCIL_VIEW view;
218 } depth;
219
220 struct {
221 XGL_SAMPLER sampler;
222
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600223 char *filename;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600224 XGL_IMAGE image;
225 XGL_GPU_MEMORY mem;
226 XGL_IMAGE_VIEW view;
227 } textures[DEMO_TEXTURE_COUNT];
228
229 struct {
230 XGL_GPU_MEMORY mem;
231 XGL_MEMORY_VIEW_ATTACH_INFO view;
232
233 XGL_PIPELINE_VERTEX_INPUT_CREATE_INFO vi;
234 XGL_VERTEX_INPUT_BINDING_DESCRIPTION vi_bindings[1];
235 XGL_VERTEX_INPUT_ATTRIBUTE_DESCRIPTION vi_attrs[2];
236 } vertices;
237
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600238 struct {
239 XGL_GPU_MEMORY mem;
240 XGL_MEMORY_VIEW_ATTACH_INFO view;
241 } uniform_data;
242
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600243 XGL_DESCRIPTOR_SET dset;
244
245 XGL_PIPELINE pipeline;
246
247 XGL_VIEWPORT_STATE_OBJECT viewport;
248 XGL_RASTER_STATE_OBJECT raster;
249 XGL_MSAA_STATE_OBJECT msaa;
250 XGL_COLOR_BLEND_STATE_OBJECT color_blend;
251 XGL_DEPTH_STENCIL_STATE_OBJECT depth_stencil;
252
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600253 mat4x4 projection_matrix;
254 mat4x4 view_matrix;
255 mat4x4 model_matrix;
256
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600257 XGL_CMD_BUFFER cmd;
258
259 xcb_window_t window;
260
261 bool quit;
262 XGL_UINT current_buffer;
263};
264
265static void demo_draw_build_cmd(struct demo *demo)
266{
267 const XGL_COLOR_ATTACHMENT_BIND_INFO color_attachment = {
268 .view = demo->buffers[demo->current_buffer].view,
269 .colorAttachmentState = XGL_IMAGE_STATE_TARGET_RENDER_ACCESS_OPTIMAL,
270 };
271 const XGL_DEPTH_STENCIL_BIND_INFO depth_stencil = {
272 .view = demo->depth.view,
273 .depthState = XGL_IMAGE_STATE_TARGET_RENDER_ACCESS_OPTIMAL,
274 .stencilState = XGL_IMAGE_STATE_TARGET_RENDER_ACCESS_OPTIMAL,
275 };
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600276 const XGL_FLOAT clear_color[4] = { 0.2f, 0.2f, 0.2f, 0.2f };
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600277 const XGL_FLOAT clear_depth = 1.0f;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600278 XGL_IMAGE_SUBRESOURCE_RANGE clear_range;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600279 XGL_RESULT err;
280
281 err = xglBeginCommandBuffer(demo->cmd,
282 XGL_CMD_BUFFER_OPTIMIZE_GPU_SMALL_BATCH_BIT |
283 XGL_CMD_BUFFER_OPTIMIZE_ONE_TIME_SUBMIT_BIT);
284 assert(!err);
285
286 xglCmdBindPipeline(demo->cmd, XGL_PIPELINE_BIND_POINT_GRAPHICS,
287 demo->pipeline);
288 xglCmdBindDescriptorSet(demo->cmd, XGL_PIPELINE_BIND_POINT_GRAPHICS,
289 0, demo->dset, 0);
290
291 xglCmdBindStateObject(demo->cmd, XGL_STATE_BIND_VIEWPORT, demo->viewport);
292 xglCmdBindStateObject(demo->cmd, XGL_STATE_BIND_RASTER, demo->raster);
293 xglCmdBindStateObject(demo->cmd, XGL_STATE_BIND_MSAA, demo->msaa);
294 xglCmdBindStateObject(demo->cmd, XGL_STATE_BIND_COLOR_BLEND,
295 demo->color_blend);
296 xglCmdBindStateObject(demo->cmd, XGL_STATE_BIND_DEPTH_STENCIL,
297 demo->depth_stencil);
298
299 xglCmdBindAttachments(demo->cmd, 1, &color_attachment, &depth_stencil);
300
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600301 clear_range.aspect = XGL_IMAGE_ASPECT_COLOR;
302 clear_range.baseMipLevel = 0;
303 clear_range.mipLevels = 1;
304 clear_range.baseArraySlice = 0;
305 clear_range.arraySize = 1;
306 xglCmdClearColorImage(demo->cmd,
307 demo->buffers[demo->current_buffer].image,
308 clear_color, 1, &clear_range);
309
310 clear_range.aspect = XGL_IMAGE_ASPECT_DEPTH;
311 xglCmdClearDepthStencil(demo->cmd, demo->depth.image,
312 clear_depth, 0, 1, &clear_range);
313
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600314 if (do_tri) {
315 xglCmdDraw(demo->cmd, 0, 3, 0, 1);
316 } else {
317 xglCmdDraw(demo->cmd, 0, 12 * 3, 0, 1);
318 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600319
320 err = xglEndCommandBuffer(demo->cmd);
321 assert(!err);
322}
323
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600324
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600325void demo_update_data_buffer(struct demo *demo, bool forward)
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600326{
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600327 mat4x4 MVP, Model, VP;
328 int matrixSize = sizeof(MVP);
329 XGL_UINT8 *pData;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600330 XGL_RESULT err;
331
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600332 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600333
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600334 // Rotate 22.5 degrees around the Y axis
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600335 mat4x4_dup(Model, demo->model_matrix);
336 if (forward) {
337 mat4x4_rotate(demo->model_matrix, Model, 0.0f, 1.0f, 0.0f, degreesToRadians(22.5f));
338 } else {
339 mat4x4_rotate(demo->model_matrix, Model, 0.0f, 1.0f, 0.0f, degreesToRadians(-22.5f));
340 }
341 mat4x4_mul(MVP, VP, demo->model_matrix);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600342
343 err = xglMapMemory(demo->uniform_data.mem, 0, (XGL_VOID **) &pData);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600344 assert(!err);
345
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600346 memcpy(pData, (const void*) &MVP[0][0], matrixSize);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600347
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600348 err = xglUnmapMemory(demo->uniform_data.mem);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600349 assert(!err);
350}
351
352static void demo_draw(struct demo *demo)
353{
354 const XGL_WSI_X11_PRESENT_INFO present = {
355 .destWindow = demo->window,
356 .srcImage = demo->buffers[demo->current_buffer].image,
357 };
358 XGL_RESULT err;
359
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600360 demo_draw_build_cmd(demo);
361
362 err = xglQueueSubmit(demo->queue, 1, &demo->cmd,
363 0, NULL, XGL_NULL_HANDLE);
364 assert(!err);
365
366 err = xglWsiX11QueuePresent(demo->queue, &present, XGL_NULL_HANDLE);
367 assert(!err);
368
369 demo->current_buffer = (demo->current_buffer + 1) % DEMO_BUFFER_COUNT;
370}
371
372static void demo_prepare_buffers(struct demo *demo)
373{
374 const XGL_WSI_X11_PRESENTABLE_IMAGE_CREATE_INFO presentable_image = {
375 .format = demo->format,
376 .usage = XGL_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
377 .extent = {
378 .width = demo->width,
379 .height = demo->height,
380 },
381 .flags = 0,
382 };
383 XGL_RESULT err;
384 XGL_UINT i;
385
386 for (i = 0; i < DEMO_BUFFER_COUNT; i++) {
387 XGL_COLOR_ATTACHMENT_VIEW_CREATE_INFO color_attachment_view = {
388 .sType = XGL_STRUCTURE_TYPE_COLOR_ATTACHMENT_VIEW_CREATE_INFO,
389 .pNext = NULL,
390 .format = demo->format,
391 .mipLevel = 0,
392 .baseArraySlice = 0,
393 .arraySize = 1,
394 };
395
396 err = xglWsiX11CreatePresentableImage(demo->device, &presentable_image,
397 &demo->buffers[i].image, &demo->buffers[i].mem);
398 assert(!err);
399
400 color_attachment_view.image = demo->buffers[i].image;
401
402 err = xglCreateColorAttachmentView(demo->device,
403 &color_attachment_view, &demo->buffers[i].view);
404 assert(!err);
405 }
406}
407
408static void demo_prepare_depth(struct demo *demo)
409{
410 const XGL_FORMAT depth_format = { XGL_CH_FMT_R16, XGL_NUM_FMT_DS };
411 const XGL_IMAGE_CREATE_INFO image = {
412 .sType = XGL_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
413 .pNext = NULL,
414 .imageType = XGL_IMAGE_2D,
415 .format = depth_format,
416 .extent = { demo->width, demo->height, 1 },
417 .mipLevels = 1,
418 .arraySize = 1,
419 .samples = 1,
420 .tiling = XGL_OPTIMAL_TILING,
421 .usage = XGL_IMAGE_USAGE_DEPTH_STENCIL_BIT,
422 .flags = 0,
423 };
424 XGL_MEMORY_ALLOC_INFO mem_alloc = {
425 .sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
426 .pNext = NULL,
427 .allocationSize = 0,
428 .alignment = 0,
429 .flags = 0,
430 .heapCount = 0,
431 .memPriority = XGL_MEMORY_PRIORITY_NORMAL,
432 };
433 XGL_DEPTH_STENCIL_VIEW_CREATE_INFO view = {
434 .sType = XGL_STRUCTURE_TYPE_DEPTH_STENCIL_VIEW_CREATE_INFO,
435 .pNext = NULL,
436 .image = XGL_NULL_HANDLE,
437 .mipLevel = 0,
438 .baseArraySlice = 0,
439 .arraySize = 1,
440 .flags = 0,
441 };
442 XGL_MEMORY_REQUIREMENTS mem_reqs;
443 XGL_SIZE mem_reqs_size;
444 XGL_RESULT err;
445
446 demo->depth.format = depth_format;
447
448 /* create image */
449 err = xglCreateImage(demo->device, &image,
450 &demo->depth.image);
451 assert(!err);
452
453 err = xglGetObjectInfo(demo->depth.image,
454 XGL_INFO_TYPE_MEMORY_REQUIREMENTS,
455 &mem_reqs_size, &mem_reqs);
456 assert(!err && mem_reqs_size == sizeof(mem_reqs));
457
458 mem_alloc.allocationSize = mem_reqs.size;
459 mem_alloc.alignment = mem_reqs.alignment;
460 mem_alloc.heapCount = mem_reqs.heapCount;
461 memcpy(mem_alloc.heaps, mem_reqs.heaps,
462 sizeof(mem_reqs.heaps[0]) * mem_reqs.heapCount);
463
464 /* allocate memory */
465 err = xglAllocMemory(demo->device, &mem_alloc,
466 &demo->depth.mem);
467 assert(!err);
468
469 /* bind memory */
470 err = xglBindObjectMemory(demo->depth.image,
471 demo->depth.mem, 0);
472 assert(!err);
473
474 /* create image view */
475 view.image = demo->depth.image;
476 err = xglCreateDepthStencilView(demo->device, &view,
477 &demo->depth.view);
478 assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600479}
480
481#if 1
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600482/** loadTexture
483 * loads a png file into an memory object, using cstdio , libpng.
484 *
485 * \param demo : Needed to access XGL calls
486 * \param filename : the png file to be loaded
487 * \param width : width of png, to be updated as a side effect of this function
488 * \param height : height of png, to be updated as a side effect of this function
489 *
490 * \return bool : an opengl texture id. true if successful?,
491 * should be validated by the client of this function.
492 *
493 * Source: http://en.wikibooks.org/wiki/OpenGL_Programming/Intermediate/Textures
494 * Modified to copy image to memory
495 *
496 */
497bool loadTexture(char *filename, XGL_UINT8 *rgba_data,
498 XGL_SUBRESOURCE_LAYOUT *layout,
499 XGL_INT *width, XGL_INT *height)
500{
501 //header for testing if it is a png
502 png_byte header[8];
503 int i, is_png, bit_depth, color_type,rowbytes;
504 png_uint_32 twidth, theight;
505 png_structp png_ptr;
506 png_infop info_ptr, end_info;
507 png_byte *image_data;
508 png_bytep *row_pointers;
509
510 //open file as binary
511 FILE *fp = fopen(filename, "rb");
512 if (!fp) {
513 return false;
514 }
515
516 //read the header
517 fread(header, 1, 8, fp);
518
519 //test if png
520 is_png = !png_sig_cmp(header, 0, 8);
521 if (!is_png) {
522 fclose(fp);
523 return false;
524 }
525
526 //create png struct
527 png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL,
528 NULL, NULL);
529 if (!png_ptr) {
530 fclose(fp);
531 return (false);
532 }
533
534 //create png info struct
535 info_ptr = png_create_info_struct(png_ptr);
536 if (!info_ptr) {
537 png_destroy_read_struct(&png_ptr, (png_infopp) NULL, (png_infopp) NULL);
538 fclose(fp);
539 return (false);
540 }
541
542 //create png info struct
543 end_info = png_create_info_struct(png_ptr);
544 if (!end_info) {
545 png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp) NULL);
546 fclose(fp);
547 return (false);
548 }
549
550 //png error stuff, not sure libpng man suggests this.
551 if (setjmp(png_jmpbuf(png_ptr))) {
552 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
553 fclose(fp);
554 return (false);
555 }
556
557 //init png reading
558 png_init_io(png_ptr, fp);
559
560 //let libpng know you already read the first 8 bytes
561 png_set_sig_bytes(png_ptr, 8);
562
563 // read all the info up to the image data
564 png_read_info(png_ptr, info_ptr);
565
566 // get info about png
567 png_get_IHDR(png_ptr, info_ptr, &twidth, &theight, &bit_depth, &color_type,
568 NULL, NULL, NULL);
569
570 //update width and height based on png info
571 *width = twidth;
572 *height = theight;
573
574 // Require that incoming texture be 8bits per color component
575 // and 4 components (RGBA).
576 if (png_get_bit_depth(png_ptr, info_ptr) != 8 ||
577 png_get_channels(png_ptr, info_ptr) != 4) {
578 return false;
579 }
580
581 if (rgba_data == NULL) {
582 // If data pointer is null, we just want the width & height
583 // clean up memory and close stuff
584 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
585 fclose(fp);
586
587 return true;
588 }
589
590 // Update the png info struct.
591 png_read_update_info(png_ptr, info_ptr);
592
593 // Row size in bytes.
594 rowbytes = png_get_rowbytes(png_ptr, info_ptr);
595
596 // Allocate the image_data as a big block, to be given to opengl
597 image_data = (png_byte *)malloc(rowbytes * theight * sizeof(png_byte));
598 if (!image_data) {
599 //clean up memory and close stuff
600 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
601 fclose(fp);
602 return false;
603 }
604
605 // row_pointers is for pointing to image_data for reading the png with libpng
606 row_pointers = (png_bytep *)malloc(theight * sizeof(png_bytep));
607 if (!row_pointers) {
608 //clean up memory and close stuff
609 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
610 // delete[] image_data;
611 fclose(fp);
612 return false;
613 }
614 // set the individual row_pointers to point at the correct offsets of image_data
615 for (i = 0; i < theight; ++i)
616 row_pointers[theight - 1 - i] = rgba_data + i * rowbytes;
617
618 // read the png into image_data through row_pointers
619 png_read_image(png_ptr, row_pointers);
620
621 // clean up memory and close stuff
622 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
623 free(row_pointers);
624 free(image_data);
625 fclose(fp);
626
627 return true;
628}
629#endif
630
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600631static void demo_prepare_textures(struct demo *demo)
632{
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600633 const XGL_FORMAT tex_format = { XGL_CH_FMT_R8G8B8A8, XGL_NUM_FMT_UNORM };
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600634 XGL_INT tex_width;
635 XGL_INT tex_height;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600636 XGL_RESULT err;
637 XGL_UINT i;
638
639 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
640 const XGL_SAMPLER_CREATE_INFO sampler = {
641 .sType = XGL_STRUCTURE_TYPE_SAMPLER_CREATE_INFO,
642 .pNext = NULL,
643 .magFilter = XGL_TEX_FILTER_NEAREST,
644 .minFilter = XGL_TEX_FILTER_NEAREST,
645 .mipMode = XGL_TEX_MIPMAP_BASE,
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600646 .addressU = XGL_TEX_ADDRESS_CLAMP,
647 .addressV = XGL_TEX_ADDRESS_CLAMP,
648 .addressW = XGL_TEX_ADDRESS_CLAMP,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600649 .mipLodBias = 0.0f,
650 .maxAnisotropy = 0,
651 .compareFunc = XGL_COMPARE_NEVER,
652 .minLod = 0.0f,
653 .maxLod = 0.0f,
654 .borderColorType = XGL_BORDER_COLOR_OPAQUE_WHITE,
655 };
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600656
657 assert(loadTexture(tex_files[i], NULL, NULL, &tex_width, &tex_height));
658
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600659 const XGL_IMAGE_CREATE_INFO image = {
660 .sType = XGL_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
661 .pNext = NULL,
662 .imageType = XGL_IMAGE_2D,
663 .format = tex_format,
664 .extent = { tex_width, tex_height, 1 },
665 .mipLevels = 1,
666 .arraySize = 1,
667 .samples = 1,
668 .tiling = XGL_LINEAR_TILING,
669 .usage = XGL_IMAGE_USAGE_SHADER_ACCESS_READ_BIT,
670 .flags = 0,
671 };
672 XGL_MEMORY_ALLOC_INFO mem_alloc = {
673 .sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
674 .pNext = NULL,
675 .allocationSize = 0,
676 .alignment = 0,
677 .flags = 0,
678 .heapCount = 0,
679 .memPriority = XGL_MEMORY_PRIORITY_NORMAL,
680 };
681 XGL_IMAGE_VIEW_CREATE_INFO view = {
682 .sType = XGL_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
683 .pNext = NULL,
684 .image = XGL_NULL_HANDLE,
685 .viewType = XGL_IMAGE_VIEW_2D,
686 .format = image.format,
687 .channels = { XGL_CHANNEL_SWIZZLE_R,
688 XGL_CHANNEL_SWIZZLE_G,
689 XGL_CHANNEL_SWIZZLE_B,
690 XGL_CHANNEL_SWIZZLE_A, },
691 .subresourceRange = { XGL_IMAGE_ASPECT_COLOR, 0, 1, 0, 1 },
692 .minLod = 0.0f,
693 };
694 XGL_MEMORY_REQUIREMENTS mem_reqs;
695 XGL_SIZE mem_reqs_size;
696
697 /* create sampler */
698 err = xglCreateSampler(demo->device, &sampler,
699 &demo->textures[i].sampler);
700 assert(!err);
701
702 /* create image */
703 err = xglCreateImage(demo->device, &image,
704 &demo->textures[i].image);
705 assert(!err);
706
707 err = xglGetObjectInfo(demo->textures[i].image,
708 XGL_INFO_TYPE_MEMORY_REQUIREMENTS,
709 &mem_reqs_size, &mem_reqs);
710 assert(!err && mem_reqs_size == sizeof(mem_reqs));
711
712 mem_alloc.allocationSize = mem_reqs.size;
713 mem_alloc.alignment = mem_reqs.alignment;
714 mem_alloc.heapCount = mem_reqs.heapCount;
715 memcpy(mem_alloc.heaps, mem_reqs.heaps,
716 sizeof(mem_reqs.heaps[0]) * mem_reqs.heapCount);
717
718 /* allocate memory */
719 err = xglAllocMemory(demo->device, &mem_alloc,
720 &demo->textures[i].mem);
721 assert(!err);
722
723 /* bind memory */
724 err = xglBindObjectMemory(demo->textures[i].image,
725 demo->textures[i].mem, 0);
726 assert(!err);
727
728 /* create image view */
729 view.image = demo->textures[i].image;
730 err = xglCreateImageView(demo->device, &view,
731 &demo->textures[i].view);
732 assert(!err);
733 }
734
735 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
736 const XGL_IMAGE_SUBRESOURCE subres = {
737 .aspect = XGL_IMAGE_ASPECT_COLOR,
738 .mipLevel = 0,
739 .arraySlice = 0,
740 };
741 XGL_SUBRESOURCE_LAYOUT layout;
742 XGL_SIZE layout_size;
743 XGL_VOID *data;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600744
745 err = xglGetImageSubresourceInfo(demo->textures[i].image, &subres,
746 XGL_INFO_TYPE_SUBRESOURCE_LAYOUT, &layout_size, &layout);
747 assert(!err && layout_size == sizeof(layout));
748
749 err = xglMapMemory(demo->textures[i].mem, 0, &data);
750 assert(!err);
751
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600752 loadTexture(tex_files[i], data, &layout, &tex_width, &tex_height);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600753
754 err = xglUnmapMemory(demo->textures[i].mem);
755 assert(!err);
756 }
757}
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600758
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600759void demo_prepare_cube_data_buffer(struct demo *demo)
760{
761 XGL_MEMORY_ALLOC_INFO alloc_info;
762 XGL_UINT8 *pData;
763 int i;
764 mat4x4 MVP, VP;
765 vec3 eye = {0.0f, 3.0f, 10.0f};
766 vec3 origin = {0, 0, 0};
767 vec3 up = {0.0f, 1.0f, 0.0};
768 XGL_RESULT err;
769 struct xgltexcube_vs_uniform data;
770
771 mat4x4_perspective(demo->projection_matrix, degreesToRadians(45.0f), 1.0f, 0.1f, 100.0f);
772 mat4x4_look_at(demo->view_matrix, eye, origin, up);
773// mat4x4_identity(demo->projection_matrix);
774// mat4x4_identity(demo->view_matrix);
775 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
776// mat4x4_translate(demo->model_matrix, 0.25, 0, 0);
777 mat4x4_identity(demo->model_matrix);
778 mat4x4_mul(MVP, VP, demo->model_matrix);
779 memcpy(data.mvp, MVP, sizeof(MVP));
780 dumpMatrix("MVP", MVP);
781
782 for (i=0; i<12*3; i++) {
783 data.position[i][0] = g_vertex_buffer_data[i*3];
784 data.position[i][1] = g_vertex_buffer_data[i*3+1];
785 data.position[i][2] = g_vertex_buffer_data[i*3+2];
786 data.position[i][3] = 1.0f;
787 data.attr[i][0] = g_uv_buffer_data[2*i];
788 data.attr[i][1] = g_uv_buffer_data[2*i + 1];
789 data.attr[i][2] = 0;
790 data.attr[i][3] = 0;
791 }
792
793 memset(&alloc_info, 0, sizeof(alloc_info));
794 alloc_info.sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_INFO;
795 alloc_info.allocationSize = sizeof(data);
796 alloc_info.alignment = 0;
797 alloc_info.heapCount = 1;
798 alloc_info.heaps[0] = 0; // TODO: Use known existing heap
799
800 alloc_info.flags = XGL_MEMORY_HEAP_CPU_VISIBLE_BIT;
801 alloc_info.memPriority = XGL_MEMORY_PRIORITY_NORMAL;
802
803 err = xglAllocMemory(demo->device, &alloc_info, &demo->uniform_data.mem);
804 assert(!err);
805
806 err = xglMapMemory(demo->uniform_data.mem, 0, (XGL_VOID **) &pData);
807 assert(!err);
808
809 memcpy(pData, &data, alloc_info.allocationSize);
810
811 err = xglUnmapMemory(demo->uniform_data.mem);
812 assert(!err);
813
814 // set up the memory view for the constant buffer
815 demo->uniform_data.view.sType = XGL_STRUCTURE_TYPE_MEMORY_VIEW_ATTACH_INFO;
816 demo->uniform_data.view.stride = 16;
817 demo->uniform_data.view.range = alloc_info.allocationSize;
818 demo->uniform_data.view.offset = 0;
819 demo->uniform_data.view.mem = demo->uniform_data.mem;
820 demo->uniform_data.view.format.channelFormat = XGL_CH_FMT_R32G32B32A32;
821 demo->uniform_data.view.format.numericFormat = XGL_NUM_FMT_FLOAT;
822}
823
824#if 0
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600825// this function will create the vertex buffer and fill it with the mesh data
826void demo_prepare_mesh( struct demo *demo )
827{
828 XGL_UINT32 m_numVertices;
829 XGL_GPU_SIZE vbStride;
830 XGL_RESULT err = XGL_SUCCESS;
831
832 m_numVertices = sizeof(g_vb_solid_face_colors_Data)/sizeof(g_vb_solid_face_colors_Data[0]);
833 vbStride = sizeof(g_vb_solid_face_colors_Data[0]);
834
835 XGL_MEMORY_ALLOC_INFO alloc_info;
836 XGL_UINT8 *pData;
837
838 memset(&alloc_info, 0, sizeof(alloc_info));
839 alloc_info.sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_INFO;
840// alloc_info.pNext = NULL;
841 alloc_info.allocationSize = m_numVertices * vbStride;
842 alloc_info.alignment = 0;
843 alloc_info.heapCount = 1;
844 alloc_info.heaps[0] = 0; // TODO: Use known existing heap
845
846 alloc_info.flags = XGL_MEMORY_HEAP_CPU_VISIBLE_BIT;
847 alloc_info.memPriority = XGL_MEMORY_PRIORITY_NORMAL;
848
849 err = xglAllocMemory(demo->device, &alloc_info, &demo->vertices.mem);
850 assert(!err);
851
852 err = xglMapMemory(demo->vertices.mem, 0, (XGL_VOID **) &pData);
853 assert(!err);
854
855 memcpy(pData, g_vb_solid_face_colors_Data, alloc_info.allocationSize);
856
857 err = xglUnmapMemory(demo->vertices.mem);
858 assert(!err);
859
860 // set up the memory view for the vertex buffer
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600861 demo->vertices.view.sType = XGL_STRUCTURE_TYPE_MEMORY_VIEW_ATTACH_INFO;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600862 demo->vertices.view.pNext = NULL;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600863 demo->vertices.view.mem = demo->vertices.mem;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600864 demo->vertices.view.offset = 0;
865 demo->vertices.view.range = sizeof(g_vb_solid_face_colors_Data);
866 demo->vertices.view.stride = vbStride;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600867 demo->vertices.view.format.channelFormat = XGL_CH_FMT_UNDEFINED;
868 demo->vertices.view.format.numericFormat = XGL_NUM_FMT_UNDEFINED;
869 demo->vertices.view.state = XGL_MEMORY_STATE_GRAPHICS_SHADER_READ_ONLY;
870
871 demo->vertices.vi_bindings[0].strideInBytes = vbStride; // strideInBytes; Distance between vertices in bytes (0 = no advancement)
872 demo->vertices.vi_bindings[0].stepRate = XGL_VERTEX_INPUT_STEP_RATE_VERTEX; // stepRate;
873
874 // this is the current description of g_vbData
875 demo->vertices.vi_attrs[0].binding = 0; // index into vertexBindingDescriptions
876 demo->vertices.vi_attrs[0].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
877 demo->vertices.vi_attrs[0].format.numericFormat = XGL_NUM_FMT_FLOAT;
878 demo->vertices.vi_attrs[0].offsetInBytes = 0; // Offset of first element in bytes from base of vertex
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600879
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600880 demo->vertices.vi_attrs[1].binding = 0; // index into vertexBindingDescriptions
881 demo->vertices.vi_attrs[1].format.channelFormat = XGL_CH_FMT_R32G32B32A32; // format of source data
882 demo->vertices.vi_attrs[1].format.numericFormat = XGL_NUM_FMT_FLOAT;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600883 demo->vertices.vi_attrs[1].offsetInBytes = sizeof(float) * 4; // Offset of first element in bytes from base of vertex
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600884
885 demo->vertices.vi.sType = XGL_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_CREATE_INFO;
886 demo->vertices.vi.pNext = NULL;
887 demo->vertices.vi.bindingCount = 1;
888 demo->vertices.vi.pVertexBindingDescriptions = demo->vertices.vi_bindings;
889 demo->vertices.vi.attributeCount = 2;
890 demo->vertices.vi.pVertexAttributeDescriptions = demo->vertices.vi_attrs;
891}
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600892#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600893
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600894#if 0
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600895static void demo_prepare_vertices(struct demo *demo)
896{
897 const float vb[3][5] = {
898 /* position texcoord */
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600899 { -1.0f, -1.0f, -0.6f, 0.0f, 0.0f },
900 { 1.0f, -1.0f, -0.5f, 1.0f, 0.0f },
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600901 { 0.0f, 1.0f, 1.0f, 0.5f, 1.0f },
902 };
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600903
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600904 const XGL_MEMORY_ALLOC_INFO mem_alloc = {
905 .sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_INFO,
906 .pNext = NULL,
907 .allocationSize = sizeof(vb),
908 .alignment = 0,
909 .flags = 0,
910 .heapCount = 1,
911 .heaps[0] = 0,
912 .memPriority = XGL_MEMORY_PRIORITY_NORMAL,
913 };
914 XGL_RESULT err;
915 void *data;
916
917 memset(&demo->vertices, 0, sizeof(demo->vertices));
918
919 err = xglAllocMemory(demo->device, &mem_alloc, &demo->vertices.mem);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600920 XGL_RESULT err; assert(!err);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -0600921
922 err = xglMapMemory(demo->vertices.mem, 0, &data);
923 assert(!err);
924
925 memcpy(data, vb, sizeof(vb));
926
927 err = xglUnmapMemory(demo->vertices.mem);
928 assert(!err);
929
930 demo->vertices.view.sType = XGL_STRUCTURE_TYPE_MEMORY_VIEW_ATTACH_INFO;
931 demo->vertices.view.pNext = NULL;
932 demo->vertices.view.mem = demo->vertices.mem;
933 demo->vertices.view.offset = 0;
934 demo->vertices.view.range = sizeof(vb);
935 demo->vertices.view.stride = sizeof(vb[0]);
936 demo->vertices.view.format.channelFormat = XGL_CH_FMT_UNDEFINED;
937 demo->vertices.view.format.numericFormat = XGL_NUM_FMT_UNDEFINED;
938 demo->vertices.view.state = XGL_MEMORY_STATE_GRAPHICS_SHADER_READ_ONLY;
939
940 demo->vertices.vi.sType = XGL_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_CREATE_INFO;
941 demo->vertices.vi.pNext = NULL;
942 demo->vertices.vi.bindingCount = 1;
943 demo->vertices.vi.pVertexBindingDescriptions = demo->vertices.vi_bindings;
944 demo->vertices.vi.attributeCount = 2;
945 demo->vertices.vi.pVertexAttributeDescriptions = demo->vertices.vi_attrs;
946
947 demo->vertices.vi_bindings[0].strideInBytes = sizeof(vb[0]);
948 demo->vertices.vi_bindings[0].stepRate = XGL_VERTEX_INPUT_STEP_RATE_VERTEX;
949
950 demo->vertices.vi_attrs[0].binding = 0;
951 demo->vertices.vi_attrs[0].format.channelFormat = XGL_CH_FMT_R32G32B32;
952 demo->vertices.vi_attrs[0].format.numericFormat = XGL_NUM_FMT_FLOAT;
953 demo->vertices.vi_attrs[0].offsetInBytes = 0;
954
955 demo->vertices.vi_attrs[1].binding = 0;
956 demo->vertices.vi_attrs[1].format.channelFormat = XGL_CH_FMT_R32G32;
957 demo->vertices.vi_attrs[1].format.numericFormat = XGL_NUM_FMT_FLOAT;
958 demo->vertices.vi_attrs[1].offsetInBytes = sizeof(float) * 3;
959}
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600960#endif
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600961
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600962void demo_prepare_tri_data_buffer(struct demo *demo)
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600963{
964 XGL_MEMORY_ALLOC_INFO alloc_info;
965 XGL_UINT8 *pData;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600966 int i;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600967 mat4x4 MVP, VP;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600968 vec3 eye = {0.0f, 3.0f, 10.0f};
969 vec3 origin = {0, 0, 0};
970 vec3 up = {0.0f, 1.0f, 0.0};
971 struct xgltextriangle_vs_uniform data;
972 const struct VertexPosTex tri_data[] =
973 {
974 /* position texcoord */
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -0600975// { XYZ1(-1.0f, -1.0f, -0.6f), UV(0.0f, 0.0f) },
976// { XYZ1(1.0f, -1.0f, -0.5f), UV(1.0f, 0.0f) },
977// { XYZ1(0.0f, 1.0f, 1.0f), UV(0.5f, 1.0f) },
978 { XYZ1(-1.0f, -1.0f, 0.0f), UV(0.0f, 0.0f) },
979 { XYZ1(1.0f, -1.0f, 0.0f), UV(1.0f, 0.0f) },
980 { XYZ1(-1.0f, 1.0f, 0.0f), UV(0.0f, 1.0f) },
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600981 };
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -0600982 XGL_RESULT err;
983
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -0600984// glm::mat4 Projection = glm::perspective(glm::radians(45.0f), 1.0f, 0.1f, 100.0f);
985// dumpMatrix("Projection", Projection);
986
987 // Camera matrix
988 /* glm::mat4 View = glm::lookAt(
989 glm::vec3(0,3,10), // Camera is at (0,3,10), in World Space
990 glm::vec3(0,0,0), // and looks at the origin
991 glm::vec3(0,1,0) // Head is up (set to 0,-1,0 to look upside-down)
992 );
993 */
994 mat4x4_perspective(demo->projection_matrix, degreesToRadians(45.0f), 1.0f, 0.1f, 100.0f);
995 dumpMatrix("Projection", demo->projection_matrix);
996 mat4x4_look_at(demo->view_matrix, eye, origin, up);
997 dumpMatrix("View", demo->view_matrix);
998// mat4x4_identity(demo->projection_matrix);
999// mat4x4_identity(demo->view_matrix);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001000 mat4x4_mul(VP, demo->projection_matrix, demo->view_matrix);
1001 mat4x4_identity(demo->model_matrix);
1002 mat4x4_mul(MVP, VP, demo->model_matrix);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001003 mat4x4_identity(MVP);
1004// mat4x4_translate(MVP, 0.25, 0, 0);
1005 dumpMatrix("MVP", MVP);
1006 memcpy(data.mvp, MVP, sizeof(MVP));
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001007
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001008 for (i=0; i<3; i++) {
1009 data.position[i][0] = tri_data[i].posX;
1010 data.position[i][1] = tri_data[i].posY;
1011 data.position[i][2] = tri_data[i].posZ;
1012 data.position[i][3] = tri_data[i].posW;
1013 data.attr[i][0] = tri_data[i].u;
1014 data.attr[i][1] = tri_data[i].v;
1015 data.attr[i][2] = tri_data[i].s;
1016 data.attr[i][3] = tri_data[i].t;
1017 }
1018
1019 memset(&alloc_info, 0, sizeof(alloc_info));
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001020 alloc_info.sType = XGL_STRUCTURE_TYPE_MEMORY_ALLOC_INFO;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001021 alloc_info.allocationSize = sizeof(data);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001022 alloc_info.alignment = 0;
1023 alloc_info.heapCount = 1;
1024 alloc_info.heaps[0] = 0; // TODO: Use known existing heap
1025
1026 alloc_info.flags = XGL_MEMORY_HEAP_CPU_VISIBLE_BIT;
1027 alloc_info.memPriority = XGL_MEMORY_PRIORITY_NORMAL;
1028
1029 err = xglAllocMemory(demo->device, &alloc_info, &demo->uniform_data.mem);
1030 assert(!err);
1031
1032 err = xglMapMemory(demo->uniform_data.mem, 0, (XGL_VOID **) &pData);
1033 assert(!err);
1034
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001035 memcpy(pData, &data, alloc_info.allocationSize);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001036
1037 err = xglUnmapMemory(demo->uniform_data.mem);
1038 assert(!err);
1039
1040 // set up the memory view for the constant buffer
1041 demo->uniform_data.view.sType = XGL_STRUCTURE_TYPE_MEMORY_VIEW_ATTACH_INFO;
1042 demo->uniform_data.view.stride = 16;
1043 demo->uniform_data.view.range = alloc_info.allocationSize;
1044 demo->uniform_data.view.offset = 0;
1045 demo->uniform_data.view.mem = demo->uniform_data.mem;
1046 demo->uniform_data.view.format.channelFormat = XGL_CH_FMT_R32G32B32A32;
1047 demo->uniform_data.view.format.numericFormat = XGL_NUM_FMT_FLOAT;
1048}
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001049
1050static void demo_prepare_descriptor_set(struct demo *demo)
1051{
1052 const XGL_DESCRIPTOR_SET_CREATE_INFO descriptor_set = {
1053 .sType = XGL_STRUCTURE_TYPE_DESCRIPTOR_SET_CREATE_INFO,
1054 .pNext = NULL,
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001055 .slots = DEMO_TEXTURE_COUNT * 2 + 2,
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001056 };
1057 XGL_RESULT err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001058
1059 err = xglCreateDescriptorSet(demo->device, &descriptor_set, &demo->dset);
1060 assert(!err);
1061
1062 xglBeginDescriptorSetUpdate(demo->dset);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001063 xglClearDescriptorSetSlots(demo->dset, 0, DEMO_TEXTURE_COUNT * 2 + 2);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001064
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001065// xglAttachMemoryViewDescriptors(demo->dset, 0, 1, &demo->vertices.view);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001066
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001067 xglAttachMemoryViewDescriptors(demo->dset, 0, 1, &demo->uniform_data.view);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001068
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001069 XGL_IMAGE_VIEW_ATTACH_INFO image_view;
1070
1071 image_view.sType = XGL_STRUCTURE_TYPE_IMAGE_VIEW_ATTACH_INFO;
1072 image_view.pNext = NULL;
1073 image_view.view = demo->textures[0].view;
1074 image_view.state = XGL_IMAGE_STATE_GRAPHICS_SHADER_READ_ONLY;
1075
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001076 xglAttachSamplerDescriptors(demo->dset, 1, 1, &demo->textures[0].sampler);
1077 xglAttachImageViewDescriptors(demo->dset, 2, 1, &image_view);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001078
1079 xglEndDescriptorSetUpdate(demo->dset);
1080}
1081
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001082#define USE_BIL
1083
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001084static XGL_SHADER demo_prepare_shader(struct demo *demo,
1085 XGL_PIPELINE_SHADER_STAGE stage,
1086 const void *code,
1087 XGL_SIZE size)
1088{
1089 XGL_SHADER_CREATE_INFO createInfo;
1090 XGL_SHADER shader;
1091 XGL_RESULT err;
1092
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001093#ifdef USE_BIL
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001094 createInfo.sType = XGL_STRUCTURE_TYPE_SHADER_CREATE_INFO;
1095 createInfo.pNext = NULL;
1096
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001097 createInfo.codeSize = 3 * sizeof(uint32_t) + size + 1;
1098 createInfo.pCode = malloc(createInfo.codeSize);
1099 createInfo.flags = 0;
1100
1101 /* try version 0 first: XGL_PIPELINE_SHADER_STAGE followed by GLSL */
1102 ((uint32_t *) createInfo.pCode)[0] = ICD_BIL_MAGIC;
1103 ((uint32_t *) createInfo.pCode)[1] = 0;
1104 ((uint32_t *) createInfo.pCode)[2] = stage;
1105 memcpy(((uint32_t *) createInfo.pCode + 3), code, size + 1);
1106
1107 err = xglCreateShader(demo->device, &createInfo, &shader);
1108 if (err) {
1109 free((void *) createInfo.pCode);
1110 }
1111#else
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001112 // Create fake BIL structure to feed GLSL
1113 // to the driver "under the covers"
1114 createInfo.codeSize = 3 * sizeof(uint32_t) + size + 1;
1115 createInfo.pCode = malloc(createInfo.codeSize);
1116 createInfo.flags = 0;
1117
1118 /* try version 0 first: XGL_PIPELINE_SHADER_STAGE followed by GLSL */
1119 ((uint32_t *) createInfo.pCode)[0] = ICD_BIL_MAGIC;
1120 ((uint32_t *) createInfo.pCode)[1] = 0;
1121 ((uint32_t *) createInfo.pCode)[2] = stage;
1122 memcpy(((uint32_t *) createInfo.pCode + 3), code, size + 1);
1123
1124 err = xglCreateShader(demo->device, &createInfo, &shader);
1125 if (err) {
1126 free((void *) createInfo.pCode);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001127 return NULL;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001128 }
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001129#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001130
1131 return shader;
1132}
1133
1134static XGL_SHADER demo_prepare_vs(struct demo *demo)
1135{
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001136 if (do_tri) {
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001137 static const char *vertShaderText =
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001138 "#version 140\n"
1139 "#extension GL_ARB_separate_shader_objects : enable\n"
1140 "#extension GL_ARB_shading_language_420pack : enable\n"
1141 "\n"
1142 "layout(binding = 0) uniform buf {\n"
1143 " mat4 MVP;\n"
1144 " vec4 position[3];\n"
1145 " vec4 attr[3];\n"
1146 "} ubuf;\n"
1147 "\n"
1148 "layout (location = 0) out vec4 texcoord;\n"
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001149 "\n"
1150 "void main() \n"
1151 "{\n"
1152 " texcoord = ubuf.attr[gl_VertexID];\n"
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001153 " gl_Position = ubuf.MVP * ubuf.position[gl_VertexID];\n"
1154 "}\n";
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001155
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001156 return demo_prepare_shader(demo, XGL_SHADER_STAGE_VERTEX,
1157 (const void *) vertShaderText,
1158 strlen(vertShaderText));
1159 } else {
1160 static const char *vertShaderText =
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001161 "#version 140\n"
1162 "#extension GL_ARB_separate_shader_objects : enable\n"
1163 "#extension GL_ARB_shading_language_420pack : enable\n"
1164 "\n"
1165 "layout(binding = 0) uniform buf {\n"
1166 " mat4 MVP;\n"
1167 " vec4 position[12*3];\n"
1168 " vec4 attr[12*3];\n"
1169 "} ubuf;\n"
1170 "\n"
1171 "layout (location = 0) out vec4 texcoord;\n"
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001172 "\n"
1173 "void main() \n"
1174 "{\n"
1175 " texcoord = ubuf.attr[gl_VertexID];\n"
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001176 " gl_Position = ubuf.MVP * ubuf.position[gl_VertexID];\n"
1177 "}\n";
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001178
1179 return demo_prepare_shader(demo, XGL_SHADER_STAGE_VERTEX,
1180 (const void *) vertShaderText,
1181 strlen(vertShaderText));
1182 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001183}
1184
1185static XGL_SHADER demo_prepare_fs(struct demo *demo)
1186{
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001187 if (do_tri) {
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001188#if 1
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001189 static const char *fragShaderText =
1190 "#version 140\n"
1191 "#extension GL_ARB_separate_shader_objects : enable\n"
1192 "#extension GL_ARB_shading_language_420pack : enable\n"
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001193 "uniform sampler2D tex;\n"
1194// "in vec2 texcoord;\n"
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001195 "layout (location = 0) in vec4 texcoord;\n"
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001196 "void main() {\n"
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001197 " gl_FragColor = texture(tex, texcoord.xy);\n"
1198// " gl_FragColor = texture(tex, vec2(0, 0));\n"
1199// " gl_FragColor = texcoord;"
1200// " gl_FragColor = vec4(0, 1, 0, 1);"
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001201 "}\n";
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001202#endif
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001203
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001204 return demo_prepare_shader(demo, XGL_SHADER_STAGE_FRAGMENT,
1205 (const void *) fragShaderText,
1206 strlen(fragShaderText));
1207 } else {
1208 static const char *fragShaderText =
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001209 "#version 140\n"
1210 "#extension GL_ARB_separate_shader_objects : enable\n"
1211 "#extension GL_ARB_shading_language_420pack : enable\n"
1212 "uniform sampler2D tex;\n"
1213 "\n"
1214 "layout (location = 0) in vec4 texcoord;\n"
1215 "void main() {\n"
1216 " gl_FragColor = texture(tex, texcoord.xy);\n"
1217 "}\n";
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001218
1219 return demo_prepare_shader(demo, XGL_SHADER_STAGE_FRAGMENT,
1220 (const void *) fragShaderText,
1221 strlen(fragShaderText));
1222 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001223}
1224
1225static void demo_prepare_pipeline(struct demo *demo)
1226{
1227 XGL_GRAPHICS_PIPELINE_CREATE_INFO pipeline;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001228// XGL_PIPELINE_VERTEX_INPUT_CREATE_INFO vi;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001229 XGL_PIPELINE_IA_STATE_CREATE_INFO ia;
1230 XGL_PIPELINE_RS_STATE_CREATE_INFO rs;
1231 XGL_PIPELINE_CB_STATE cb;
1232 XGL_PIPELINE_DB_STATE_CREATE_INFO db;
1233 XGL_PIPELINE_SHADER_STAGE_CREATE_INFO vs;
1234 XGL_PIPELINE_SHADER_STAGE_CREATE_INFO fs;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001235 XGL_DESCRIPTOR_SLOT_INFO vs_slots[3];
1236 XGL_DESCRIPTOR_SLOT_INFO fs_slots[3];
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001237 XGL_RESULT err;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001238
1239 memset(&pipeline, 0, sizeof(pipeline));
1240 pipeline.sType = XGL_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
1241
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001242 memset(&ia, 0, sizeof(ia));
1243 ia.sType = XGL_STRUCTURE_TYPE_PIPELINE_IA_STATE_CREATE_INFO;
1244 ia.topology = XGL_TOPOLOGY_TRIANGLE_LIST;
1245
1246 memset(&rs, 0, sizeof(rs));
1247 rs.sType = XGL_STRUCTURE_TYPE_PIPELINE_RS_STATE_CREATE_INFO;
1248
1249 memset(&cb, 0, sizeof(cb));
1250 cb.sType = XGL_STRUCTURE_TYPE_PIPELINE_CB_STATE_CREATE_INFO;
1251 cb.attachment[0].format = demo->format;
1252 cb.attachment[0].channelWriteMask = 0xf;
1253
1254 memset(&db, 0, sizeof(db));
1255 db.sType = XGL_STRUCTURE_TYPE_PIPELINE_DB_STATE_CREATE_INFO;
1256 db.format = demo->depth.format;
1257
1258 memset(&vs_slots, 0, sizeof(vs_slots));
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001259 vs_slots[0].slotObjectType = XGL_SLOT_SHADER_RESOURCE;
1260 vs_slots[0].shaderEntityIndex = 0;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001261
1262 memset(&fs_slots, 0, sizeof(fs_slots));
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001263 fs_slots[1].slotObjectType = XGL_SLOT_SHADER_SAMPLER;
1264 fs_slots[1].shaderEntityIndex = 0;
1265 fs_slots[2].slotObjectType = XGL_SLOT_SHADER_RESOURCE;
1266 fs_slots[2].shaderEntityIndex = 0;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001267
1268 memset(&vs, 0, sizeof(vs));
1269 vs.sType = XGL_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1270 vs.shader.stage = XGL_SHADER_STAGE_VERTEX;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001271 vs.shader.shader = demo_prepare_vs(demo);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001272 assert(vs.shader.shader != NULL);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001273 vs.shader.descriptorSetMapping[0].descriptorCount = 3;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001274 vs.shader.descriptorSetMapping[0].pDescriptorInfo = vs_slots;
1275
1276 memset(&fs, 0, sizeof(fs));
1277 fs.sType = XGL_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1278 fs.shader.stage = XGL_SHADER_STAGE_FRAGMENT;
1279 fs.shader.shader = demo_prepare_fs(demo);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001280 assert(fs.shader.shader != NULL);
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001281 fs.shader.descriptorSetMapping[0].descriptorCount = 3;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001282 fs.shader.descriptorSetMapping[0].pDescriptorInfo = fs_slots;
1283
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001284 pipeline.pNext = (const XGL_VOID *) &ia;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001285 ia.pNext = (const XGL_VOID *) &rs;
1286 rs.pNext = (const XGL_VOID *) &cb;
1287 cb.pNext = (const XGL_VOID *) &db;
1288 db.pNext = (const XGL_VOID *) &vs;
1289 vs.pNext = (const XGL_VOID *) &fs;
1290
1291 err = xglCreateGraphicsPipeline(demo->device, &pipeline, &demo->pipeline);
1292 assert(!err);
1293
1294 xglDestroyObject(vs.shader.shader);
1295 xglDestroyObject(fs.shader.shader);
1296}
1297
1298static void demo_prepare_dynamic_states(struct demo *demo)
1299{
1300 XGL_VIEWPORT_STATE_CREATE_INFO viewport;
1301 XGL_RASTER_STATE_CREATE_INFO raster;
1302 XGL_MSAA_STATE_CREATE_INFO msaa;
1303 XGL_COLOR_BLEND_STATE_CREATE_INFO color_blend;
1304 XGL_DEPTH_STENCIL_STATE_CREATE_INFO depth_stencil;
1305 XGL_RESULT err;
1306
1307 memset(&viewport, 0, sizeof(viewport));
1308 viewport.viewportCount = 1;
1309 viewport.scissorEnable = XGL_FALSE;
1310 viewport.viewports[0].width = (XGL_FLOAT) demo->width;
1311 viewport.viewports[0].height = (XGL_FLOAT) demo->height;
1312 viewport.viewports[0].minDepth = (XGL_FLOAT) 0.0f;
1313 viewport.viewports[0].maxDepth = (XGL_FLOAT) 1.0f;
1314
1315 memset(&raster, 0, sizeof(raster));
1316 raster.sType = XGL_STRUCTURE_TYPE_RASTER_STATE_CREATE_INFO;
1317 raster.fillMode = XGL_FILL_SOLID;
1318 raster.cullMode = XGL_CULL_NONE;
1319 raster.frontFace = XGL_FRONT_FACE_CCW;
1320
1321 memset(&msaa, 0, sizeof(msaa));
1322 msaa.sType = XGL_STRUCTURE_TYPE_MSAA_STATE_CREATE_INFO;
1323 msaa.samples = 1;
1324 msaa.sampleMask = 0x1;
1325
1326 memset(&color_blend, 0, sizeof(color_blend));
1327 color_blend.sType = XGL_STRUCTURE_TYPE_COLOR_BLEND_STATE_CREATE_INFO;
1328
1329 memset(&depth_stencil, 0, sizeof(depth_stencil));
1330 depth_stencil.sType = XGL_STRUCTURE_TYPE_DEPTH_STENCIL_STATE_CREATE_INFO;
1331 depth_stencil.depthTestEnable = XGL_TRUE;
1332 depth_stencil.depthWriteEnable = XGL_TRUE;
1333 depth_stencil.depthFunc = XGL_COMPARE_LESS_EQUAL;
1334 depth_stencil.depthBoundsEnable = XGL_FALSE;
1335
1336 err = xglCreateViewportState(demo->device, &viewport, &demo->viewport);
1337 assert(!err);
1338
1339 err = xglCreateRasterState(demo->device, &raster, &demo->raster);
1340 assert(!err);
1341
1342 err = xglCreateMsaaState(demo->device, &msaa, &demo->msaa);
1343 assert(!err);
1344
1345 err = xglCreateColorBlendState(demo->device,
1346 &color_blend, &demo->color_blend);
1347 assert(!err);
1348
1349 err = xglCreateDepthStencilState(demo->device,
1350 &depth_stencil, &demo->depth_stencil);
1351 assert(!err);
1352}
1353
1354static void demo_prepare(struct demo *demo)
1355{
1356 const XGL_CMD_BUFFER_CREATE_INFO cmd = {
1357 .sType = XGL_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO,
1358 .pNext = NULL,
1359 .queueType = XGL_QUEUE_TYPE_GRAPHICS,
1360 .flags = 0,
1361 };
1362 XGL_RESULT err;
1363
1364 demo_prepare_buffers(demo);
1365 demo_prepare_depth(demo);
1366 demo_prepare_textures(demo);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001367 if (do_tri) {
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001368// demo_prepare_vertices(demo);
1369 demo_prepare_tri_data_buffer(demo);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001370 } else {
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001371 demo_prepare_cube_data_buffer(demo);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001372 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001373 demo_prepare_descriptor_set(demo);
1374
1375 demo_prepare_pipeline(demo);
1376 demo_prepare_dynamic_states(demo);
1377
1378 err = xglCreateCommandBuffer(demo->device, &cmd, &demo->cmd);
1379 assert(!err);
1380}
1381
1382static void demo_handle_event(struct demo *demo,
1383 const xcb_generic_event_t *event)
1384{
1385 switch (event->response_type & 0x7f) {
1386 case XCB_EXPOSE:
1387 demo_draw(demo);
1388 break;
1389 case XCB_KEY_RELEASE:
1390 {
1391 const xcb_key_release_event_t *key =
1392 (const xcb_key_release_event_t *) event;
1393
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001394 switch (key->detail) {
1395 case 0x9: // Escape
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001396 demo->quit = true;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001397 break;
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001398 case 0x71: // left arrow key
1399 // Wait for work to finish before updating MVP.
1400 xglDeviceWaitIdle(demo->device);
1401
1402 demo_update_data_buffer(demo, false);
1403
1404 demo_draw(demo);
1405 break;
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001406 case 0x72: // right arrow key
1407 // Wait for work to finish before updating MVP.
1408 xglDeviceWaitIdle(demo->device);
1409
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001410 demo_update_data_buffer(demo, true);
Courtney Goeltzenleuchterfc3b9532014-10-28 10:32:57 -06001411
1412 demo_draw(demo);
1413 break;
1414 }
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001415 }
1416 break;
1417 default:
1418 break;
1419 }
1420}
1421
1422static void demo_run(struct demo *demo)
1423{
1424 xcb_flush(demo->connection);
1425
1426 while (!demo->quit) {
1427 xcb_generic_event_t *event;
1428
1429 event = xcb_wait_for_event(demo->connection);
1430 if (event) {
1431 demo_handle_event(demo, event);
1432 free(event);
1433 }
1434 }
1435}
1436
1437static void demo_create_window(struct demo *demo)
1438{
1439 uint32_t value_mask, value_list[32];
1440
1441 demo->window = xcb_generate_id(demo->connection);
1442
1443 value_mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK;
1444 value_list[0] = demo->screen->black_pixel;
1445 value_list[1] = XCB_EVENT_MASK_KEY_RELEASE |
1446 XCB_EVENT_MASK_EXPOSURE;
1447
1448 xcb_create_window(demo->connection,
1449 XCB_COPY_FROM_PARENT,
1450 demo->window, demo->screen->root,
1451 0, 0, demo->width, demo->height, 0,
1452 XCB_WINDOW_CLASS_INPUT_OUTPUT,
1453 demo->screen->root_visual,
1454 value_mask, value_list);
1455
1456 xcb_map_window(demo->connection, demo->window);
1457}
1458
1459static void demo_init_xgl(struct demo *demo)
1460{
1461 const XGL_APPLICATION_INFO app = {
1462 .sType = XGL_STRUCTURE_TYPE_APPLICATION_INFO,
1463 .pNext = NULL,
1464 .pAppName = (const XGL_CHAR *) "cube",
1465 .appVersion = 0,
1466 .pEngineName = (const XGL_CHAR *) "cube",
1467 .engineVersion = 0,
1468 .apiVersion = XGL_MAKE_VERSION(0, 22, 0),
1469 };
1470 const XGL_WSI_X11_CONNECTION_INFO connection = {
1471 .pConnection = demo->connection,
1472 .root = demo->screen->root,
1473 .provider = 0,
1474 };
1475 const XGL_DEVICE_QUEUE_CREATE_INFO queue = {
1476 .queueNodeIndex = 0,
1477 .queueCount = 1,
1478 };
1479 const XGL_CHAR *ext_names[] = {
1480 (const XGL_CHAR *) "XGL_WSI_X11",
1481 };
1482 const XGL_DEVICE_CREATE_INFO device = {
1483 .sType = XGL_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
1484 .pNext = NULL,
1485 .queueRecordCount = 1,
1486 .pRequestedQueues = &queue,
1487 .extensionCount = 1,
1488 .ppEnabledExtensionNames = ext_names,
1489 .maxValidationLevel = XGL_VALIDATION_LEVEL_END_RANGE,
1490 .flags = XGL_DEVICE_CREATE_VALIDATION_BIT,
1491 };
1492 XGL_RESULT err;
1493 XGL_UINT gpu_count;
1494 XGL_UINT i;
1495
1496 err = xglInitAndEnumerateGpus(&app, NULL, 1, &gpu_count, &demo->gpu);
1497 assert(!err && gpu_count == 1);
1498
1499 for (i = 0; i < device.extensionCount; i++) {
1500 err = xglGetExtensionSupport(demo->gpu, ext_names[i]);
1501 assert(!err);
1502 }
1503
1504 err = xglWsiX11AssociateConnection(demo->gpu, &connection);
1505 assert(!err);
1506
1507 err = xglCreateDevice(demo->gpu, &device, &demo->device);
1508 assert(!err);
1509
1510 err = xglGetDeviceQueue(demo->device, XGL_QUEUE_TYPE_GRAPHICS,
1511 0, &demo->queue);
1512 assert(!err);
1513}
1514
1515static void demo_init_connection(struct demo *demo)
1516{
1517 const xcb_setup_t *setup;
1518 xcb_screen_iterator_t iter;
1519 int scr;
1520
1521 demo->connection = xcb_connect(NULL, &scr);
1522
1523 setup = xcb_get_setup(demo->connection);
1524 iter = xcb_setup_roots_iterator(setup);
1525 while (scr-- > 0)
1526 xcb_screen_next(&iter);
1527
1528 demo->screen = iter.data;
1529}
1530
1531static void demo_init(struct demo *demo)
1532{
1533 memset(demo, 0, sizeof(*demo));
1534
1535 demo_init_connection(demo);
1536 demo_init_xgl(demo);
1537
Courtney Goeltzenleuchter68369392014-10-29 15:59:49 -06001538 demo->width = 1024;
1539 demo->height = 1024;
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001540 demo->format.channelFormat = XGL_CH_FMT_B8G8R8A8;
1541 demo->format.numericFormat = XGL_NUM_FMT_UNORM;
1542}
1543
1544static void demo_cleanup(struct demo *demo)
1545{
1546 XGL_UINT i;
1547
1548 xglDestroyObject(demo->cmd);
1549
1550 xglDestroyObject(demo->viewport);
1551 xglDestroyObject(demo->raster);
1552 xglDestroyObject(demo->msaa);
1553 xglDestroyObject(demo->color_blend);
1554 xglDestroyObject(demo->depth_stencil);
1555
1556 xglDestroyObject(demo->pipeline);
1557
1558 xglDestroyObject(demo->dset);
1559
Courtney Goeltzenleuchter4984d152014-10-28 14:50:30 -06001560// xglFreeMemory(demo->vertices.mem);
Courtney Goeltzenleuchter7be88a82014-10-23 13:16:59 -06001561
1562 for (i = 0; i < DEMO_TEXTURE_COUNT; i++) {
1563 xglDestroyObject(demo->textures[i].view);
1564 xglDestroyObject(demo->textures[i].image);
1565 xglFreeMemory(demo->textures[i].mem);
1566 xglDestroyObject(demo->textures[i].sampler);
1567 }
1568
1569 xglDestroyObject(demo->depth.view);
1570 xglDestroyObject(demo->depth.image);
1571 xglFreeMemory(demo->depth.mem);
1572
1573 for (i = 0; i < DEMO_BUFFER_COUNT; i++) {
1574 xglDestroyObject(demo->buffers[i].view);
1575 xglDestroyObject(demo->buffers[i].image);
1576 }
1577
1578 xglDestroyDevice(demo->device);
1579
1580 xcb_destroy_window(demo->connection, demo->window);
1581 xcb_disconnect(demo->connection);
1582}
1583
1584int main(void)
1585{
1586 struct demo demo;
1587
1588 demo_init(&demo);
1589
1590 demo_prepare(&demo);
1591 demo_create_window(&demo);
1592 demo_run(&demo);
1593
1594 demo_cleanup(&demo);
1595
1596 return 0;
1597}