-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmesh_test.c
More file actions
664 lines (550 loc) · 23 KB
/
Copy pathmesh_test.c
File metadata and controls
664 lines (550 loc) · 23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
#include "pal2/pal_graphics.h"
#include "pal2/pal_system.h"
#include "pal2/pal_video.h"
#include "tests.h"
#define WINDOW_WIDTH 640
#define WINDOW_HEIGHT 480
#define MAX_FRAMES_IN_FLIGHT 2
static void PAL_CALL onGraphicsDebug(
void* userData,
PalDebugMessageSeverity severity,
PalDebugMessageType type,
const char* msg)
{
palLog(nullptr, msg);
}
PalBool meshTest()
{
PalResult result;
PalWindow* window = nullptr;
PalEventDriver* eventDriver = nullptr;
PalAdapter* adapter = nullptr;
PalDevice* device = nullptr;
PalSurface* surface = nullptr;
PalQueue* queue = nullptr;
PalSwapchain* swapchain = nullptr;
PalCommandPool* cmdPool = nullptr;
PalImageView** imageViews = nullptr;
PalCommandBuffer* cmdBuffers[MAX_FRAMES_IN_FLIGHT];
PalSemaphore* imageAvailableSemaphores[MAX_FRAMES_IN_FLIGHT];
PalFence* inFlightFences[MAX_FRAMES_IN_FLIGHT];
PalSemaphore** renderFinishedSemaphores; // count of swapchain images
PalFence** inFlightImages; // count of swapchain images
PalPipelineLayout* pipelineLayout = nullptr;
PalPipeline* pipeline = nullptr;
PalShader* shaders[2];
PalEventDriverCreateInfo eventDriverCreateInfo = {0};
result = palCreateEventDriver(&eventDriverCreateInfo, &eventDriver);
if (result != PAL_RESULT_SUCCESS) {
logResult(result, "Failed to create event driver");
return PAL_FALSE;
}
palSetEventDispatchMode(eventDriver, PAL_EVENT_TYPE_WINDOW_CLOSE, PAL_DISPATCH_MODE_POLL);
palSetEventDispatchMode(eventDriver, PAL_EVENT_TYPE_KEYDOWN, PAL_DISPATCH_MODE_POLL);
result = palInitVideo(nullptr, eventDriver, nullptr);
if (result != PAL_RESULT_SUCCESS) {
logResult(result, "Failed to initialize video");
return PAL_FALSE;
}
PalWindowCreateInfo windowCreateInfo = {0};
windowCreateInfo.height = WINDOW_HEIGHT;
windowCreateInfo.width = WINDOW_WIDTH;
windowCreateInfo.show = PAL_TRUE;
windowCreateInfo.title = "Mesh Window";
PalVideoFeatures videoFeatures = palGetVideoFeatures();
if (!(videoFeatures & PAL_VIDEO_FEATURE_DECORATED_WINDOW)) {
windowCreateInfo.style |= PAL_WINDOW_STYLE_BORDERLESS;
}
result = palCreateWindow(&windowCreateInfo, &window);
if (result != PAL_RESULT_SUCCESS) {
logResult(result, "Failed to create window");
return PAL_FALSE;
}
// get window handle. You can use any window from any library
// so long as you can get the window handle and display (if on X11, wayland)
// If pal video system will not be used, there is no need to initialize it
PalWindowHandleInfo winHandle = {0};
palGetWindowHandleInfo(window, &winHandle);
// using pal_system.h will be easy to know the underlying windowing API or use typedefs.
// We will use the pal_system module.
PalPlatformInfo platformInfo = {0};
palGetPlatformInfo(&platformInfo);
PalWindowInstanceType windowInstanceType = PAL_WINDOW_INSTANCE_TYPE_XCB;
if (platformInfo.apiType == PAL_PLATFORM_API_TYPE_WAYLAND) {
windowInstanceType = PAL_WINDOW_INSTANCE_TYPE_WAYLAND;
} else if (platformInfo.apiType == PAL_PLATFORM_API_TYPE_X11) {
windowInstanceType = PAL_WINDOW_INSTANCE_TYPE_X11;
} else if (platformInfo.apiType == PAL_PLATFORM_API_TYPE_WIN32) {
windowInstanceType = PAL_WINDOW_INSTANCE_TYPE_WIN32;
}
PalGraphicsDebugger debugger = {0};
debugger.callback = onGraphicsDebug;
debugger.userData = nullptr;
result = palInitGraphics(nullptr, nullptr, 0, nullptr);
if (result != PAL_RESULT_SUCCESS) {
logResult(result, "Failed to initialize graphics");
return PAL_FALSE;
}
// enumerate all available adapters
uint32_t adapterCount = 0;
result = palEnumerateAdapters(&adapterCount, nullptr);
if (result != PAL_RESULT_SUCCESS) {
logResult(result, "Failed to get adapters");
return PAL_FALSE;
}
if (adapterCount == 0) {
palLog(nullptr, "No adapters found");
return PAL_FALSE;
}
palLog(nullptr, "Adapter count: %d", adapterCount);
PalAdapter** adapters = nullptr;
adapters = palAllocate(nullptr, sizeof(PalAdapter*) * adapterCount, 0);
if (!adapters) {
palLog(nullptr, "Failed to allocate memory");
return PAL_FALSE;
}
result = palEnumerateAdapters(&adapterCount, adapters);
if (result != PAL_RESULT_SUCCESS) {
logResult(result, "Failed to get adapters");
return PAL_FALSE;
}
PalAdapterCapabilities caps = {0};
PalAdapterFeatures adapterFeatures = 0;
PalAdapterInfo adapterInfo = {0};
for (int32_t i = 0; i < adapterCount; i++) {
adapter = adapters[i];
palGetAdapterCapabilities(adapter, &caps);
if (caps.maxGraphicsQueues == 0) {
adapter = nullptr;
continue;
}
adapterFeatures = palGetAdapterFeatures(adapter);
if (!(adapterFeatures & PAL_ADAPTER_FEATURE_MESH_SHADER)) {
adapter = nullptr;
continue;
}
// We want an adapter that supports spirv 1.5 or dxil 6.5
palGetAdapterInfo(adapter, &adapterInfo);
// we prefer spirv first if an adapter supports multiple shader formats
uint32_t target = 0;
if (adapterInfo.shaderFormats & PAL_SHADER_FORMAT_SPIRV) {
target = palGetHighestSupportedShaderTarget(adapter, PAL_SHADER_FORMAT_SPIRV);
if (target >= PAL_MAKE_SHADER_TARGET(1, 5)) {
break;
}
}
if (adapterInfo.shaderFormats & PAL_SHADER_FORMAT_DXIL) {
target = palGetHighestSupportedShaderTarget(adapter, PAL_SHADER_FORMAT_DXIL);
if (target >= PAL_MAKE_SHADER_TARGET(6, 5)) {
break;
}
}
adapter = nullptr;
continue;
}
palFree(nullptr, adapters);
if (!adapter) {
palLog(nullptr, "Failed to find a required adapter");
return PAL_FALSE;
}
// create a device
PalAdapterFeatures features = PAL_ADAPTER_FEATURE_SWAPCHAIN;
features |= PAL_ADAPTER_FEATURE_MESH_SHADER;
if (adapterFeatures & PAL_ADAPTER_FEATURE_FENCE_RESET) {
features |= PAL_ADAPTER_FEATURE_FENCE_RESET;
}
result = palCreateDevice(adapter, features, &device);
if (result != PAL_RESULT_SUCCESS) {
logResult(result, "Failed to create device");
return PAL_FALSE;
}
// create surface
result = palCreateSurface(
device,
winHandle.nativeWindow,
winHandle.nativeInstance,
windowInstanceType,
&surface);
if (result != PAL_RESULT_SUCCESS) {
logResult(result, "Failed to create surface");
return PAL_FALSE;
}
// create a graphics command queue and check if its supports presenting to the surface
PalBool foundQueue = PAL_FALSE;
for (int i = 0; i < caps.maxGraphicsQueues; i++) {
result = palCreateQueue(device, PAL_QUEUE_TYPE_GRAPHICS, &queue);
if (result != PAL_RESULT_SUCCESS) {
logResult(result, "Failed to create queue");
return PAL_FALSE;
}
if (!palCanQueuePresent(queue, surface)) {
palDestroyQueue(queue);
queue = nullptr;
} else {
// found a queue
foundQueue = PAL_TRUE;
break;
}
}
if (!foundQueue) {
palLog(nullptr, "Failed to find a queue that can present to the surface");
return PAL_FALSE;
}
// create a swapchain with the graphics queue
PalSurfaceCapabilities surfaceCaps = {0};
palGetSurfaceCapabilities(device, surface, &surfaceCaps);
PalSwapchainCreateInfo swapchainCreateInfo = {0};
swapchainCreateInfo.clipped = PAL_TRUE;
swapchainCreateInfo.compositeAlpha = PAL_COMPOSITE_ALPHA_OPAQUE;
swapchainCreateInfo.height = WINDOW_HEIGHT;
swapchainCreateInfo.width = WINDOW_WIDTH;
swapchainCreateInfo.imageArrayLayerCount = 1;
swapchainCreateInfo.presentMode = PAL_PRESENT_MODE_FIFO;
swapchainCreateInfo.format = PAL_SURFACE_FORMAT_BGRA8_UNORM_SRGB_NONLINEAR;
// rare but possible on andriod
if (WINDOW_WIDTH > surfaceCaps.maxImageWidth) {
swapchainCreateInfo.width = surfaceCaps.maxImageWidth / 2;
}
if (WINDOW_HEIGHT > surfaceCaps.maxImageHeight) {
swapchainCreateInfo.height = surfaceCaps.maxImageHeight / 2;
}
// check if the minimal image count is not good for you
// and increase it but not pass the max count
swapchainCreateInfo.imageCount = surfaceCaps.minImageCount;
if (swapchainCreateInfo.imageCount == 1) {
swapchainCreateInfo.imageCount++;
if (surfaceCaps.maxImageCount < 2) {
palLog(nullptr, "Surface does not support double buffers");
return PAL_FALSE;
}
}
result = palCreateSwapchain(device, queue, surface, &swapchainCreateInfo, &swapchain);
if (result != PAL_RESULT_SUCCESS) {
logResult(result, "Failed to create swapchain");
return PAL_FALSE;
}
// get all swapchain images and create image views for them
uint32_t imageCount = swapchainCreateInfo.imageCount;
imageViews = palAllocate(nullptr, sizeof(PalImageView*) * imageCount, 0);
inFlightImages = palAllocate(nullptr, sizeof(PalFence*) * imageCount, 0);
renderFinishedSemaphores = palAllocate(nullptr, sizeof(PalSemaphore*) * imageCount, 0);
if (!imageViews || !inFlightImages || !renderFinishedSemaphores) {
palLog(nullptr, "Failed to allocate memory");
return PAL_FALSE;
}
PalImageInfo imageInfo;
palGetImageInfo(palGetSwapchainImage(swapchain, 0), &imageInfo);
PalImageViewCreateInfo imageViewCreateInfo = {0};
imageViewCreateInfo.type = PAL_IMAGE_VIEW_TYPE_2D;
imageViewCreateInfo.subresourceRange.layerArrayCount = 1;
imageViewCreateInfo.subresourceRange.mipLevelCount = 1;
imageViewCreateInfo.subresourceRange.startArrayLayer = 0;
imageViewCreateInfo.subresourceRange.startMipLevel = 0;
imageViewCreateInfo.format = imageInfo.format;
for (int i = 0; i < imageCount; i++) {
// get swapchain image
// this is fast since the images are cache by PAL
PalImage* image = palGetSwapchainImage(swapchain, i);
if (!image) {
palLog(nullptr, "Failed to get swapchain image");
}
result = palCreateImageView(device, image, &imageViewCreateInfo, &imageViews[i]);
if (result != PAL_RESULT_SUCCESS) {
logResult(result, "Failed to create image view");
return PAL_FALSE;
}
// create render finished semaphores
result = palCreateSemaphore(device, PAL_FALSE, &renderFinishedSemaphores[i]);
if (result != PAL_RESULT_SUCCESS) {
logResult(result, "Failed to create semaphore");
return PAL_FALSE;
}
inFlightImages[i] = nullptr;
}
result = palCreateCommandPool(device, queue, &cmdPool);
if (result != PAL_RESULT_SUCCESS) {
logResult(result, "Failed to create command pool");
return PAL_FALSE;
}
// create synchronization objects and command buffers
for (int i = 0; i < MAX_FRAMES_IN_FLIGHT; i++) {
result = palCreateSemaphore(device, PAL_FALSE, &imageAvailableSemaphores[i]);
if (result != PAL_RESULT_SUCCESS) {
logResult(result, "Failed to create semaphore");
return PAL_FALSE;
}
result = palCreateFence(device, PAL_TRUE, &inFlightFences[i]);
if (result != PAL_RESULT_SUCCESS) {
logResult(result, "Failed to create fence");
return PAL_FALSE;
}
result = palAllocateCommandBuffer(
device,
cmdPool,
PAL_COMMAND_BUFFER_TYPE_PRIMARY,
&cmdBuffers[i]);
if (result != PAL_RESULT_SUCCESS) {
logResult(result, "Failed to allocate command buffer");
return PAL_FALSE;
}
}
// create shaders
uint32_t bytecodeSize = 0;
void* bytecode = nullptr;
const char* sources[2];
PalShaderEntryInfo entries[2];
PalViewport viewport = {0};
viewport.width = (float)WINDOW_WIDTH;
viewport.maxDepth = 1.0f;
PalShaderCreateInfo shaderCreateInfo = {0};
if (adapterInfo.shaderFormats & PAL_SHADER_FORMAT_SPIRV) {
sources[0] = "graphics/shaders/bin/spirv/mesh.spv";
sources[1] = "graphics/shaders/bin/spirv/triangle_frag.spv";
viewport.height = -(float)WINDOW_HEIGHT;
viewport.y = (float)WINDOW_HEIGHT;
} else if (adapterInfo.shaderFormats & PAL_SHADER_FORMAT_DXIL) {
sources[0] = "graphics/shaders/bin/dxil/mesh.dxil";
sources[1] = "graphics/shaders/bin/dxil/triangle_frag.dxil";
viewport.height = (float)WINDOW_HEIGHT;
}
entries[0].stage = PAL_SHADER_STAGE_MESH;
entries[0].entryName = "main";
entries[0].patchControlPoints = 0;
entries[1].stage = PAL_SHADER_STAGE_FRAGMENT;
entries[1].entryName = "main";
entries[1].patchControlPoints = 0;
for (int i = 0; i < 2; i++) {
// read file
if (!readFile(sources[i], nullptr, &bytecodeSize)) {
palLog(nullptr, "Failed to read shader file");
return PAL_FALSE;
}
bytecode = palAllocate(nullptr, bytecodeSize, 0);
if (!bytecode) {
palLog(nullptr, "Failed to allocate memory");
return PAL_FALSE;
}
readFile(sources[i], bytecode, &bytecodeSize);
shaderCreateInfo.code = bytecode;
shaderCreateInfo.codeSize = bytecodeSize;
shaderCreateInfo.entries = &entries[i];
shaderCreateInfo.entryCount = 1;
result = palCreateShader(device, &shaderCreateInfo, &shaders[i]);
if (result != PAL_RESULT_SUCCESS) {
logResult(result, "Failed to create shader");
return PAL_FALSE;
}
palFree(nullptr, bytecode);
}
// create a pipeline layout
PalPipelineLayoutCreateInfo pipelineLayoutCreateInfo = {0};
result = palCreatePipelineLayout(device, &pipelineLayoutCreateInfo, &pipelineLayout);
if (result != PAL_RESULT_SUCCESS) {
logResult(result, "Failed to create pipeline layout");
return PAL_FALSE;
}
PalRenderingLayoutInfo renderingLayoutInfo = {0};
renderingLayoutInfo.colorAttachentCount = 1;
renderingLayoutInfo.colorAttachmentsFormat = &imageInfo.format;
renderingLayoutInfo.sampleCount = PAL_SAMPLE_COUNT_1;
renderingLayoutInfo.viewCount = 1;
// create graphics pipeline
PalGraphicsPipelineCreateInfo pipelineCreateInfo = {0};
// color blend attachment
PalColorBlendAttachment blendAttachment = {0};
blendAttachment.colorWriteMask |= PAL_COLOR_MASK_RED;
blendAttachment.colorWriteMask |= PAL_COLOR_MASK_GREEN;
blendAttachment.colorWriteMask |= PAL_COLOR_MASK_BLUE;
blendAttachment.colorWriteMask |= PAL_COLOR_MASK_ALPHA;
pipelineCreateInfo.colorBlendAttachments = &blendAttachment;
pipelineCreateInfo.colorBlendAttachmentCount = 1;
// shaders
pipelineCreateInfo.shaderCount = 2;
pipelineCreateInfo.shaders = shaders;
pipelineCreateInfo.topology = PAL_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
pipelineCreateInfo.pipelineLayout = pipelineLayout;
pipelineCreateInfo.renderingLayout = &renderingLayoutInfo;
result = palCreateGraphicsPipeline(device, &pipelineCreateInfo, &pipeline);
if (result != PAL_RESULT_SUCCESS) {
logResult(result, "Failed to create pipeline");
return PAL_FALSE;
}
for (int i = 0; i < 2; i++) {
palDestroyShader(shaders[i]);
}
// main loop
uint32_t currentFrame = 0;
PalBool running = PAL_TRUE;
PalRect2D scissor = {0};
scissor.height = WINDOW_HEIGHT;
scissor.width = WINDOW_WIDTH;
while (running) {
// update the video system to push video events
palUpdateVideo();
PalEvent event;
while (palPollEvent(eventDriver, &event)) {
switch (event.type) {
case PAL_EVENT_TYPE_WINDOW_CLOSE: {
running = PAL_FALSE;
break;
}
case PAL_EVENT_TYPE_KEYDOWN: {
PalKeycode keycode = 0;
palUnpackUint32(event.data, &keycode, nullptr);
if (keycode == PAL_KEYCODE_ESCAPE) {
running = PAL_FALSE;
}
break;
}
}
}
result = palWaitFence(inFlightFences[currentFrame], PAL_INFINITE);
if (result != PAL_RESULT_SUCCESS) {
logResult(result, "Failed to wait fence");
return PAL_FALSE;
}
// get next swapchain image
PalSwapchainNextImageInfo nextImageInfo = {0};
nextImageInfo.fence = nullptr;
nextImageInfo.signalSemaphore = imageAvailableSemaphores[currentFrame];
nextImageInfo.timeout = PAL_INFINITE;
uint32_t imageIndex = 0;
result = palGetNextSwapchainImage(swapchain, &nextImageInfo, &imageIndex);
if (result != PAL_RESULT_SUCCESS) {
logResult(result, "Failed to get next swapchain image");
return PAL_FALSE;
}
if (inFlightImages[imageIndex] != nullptr) {
result = palWaitFence(inFlightImages[imageIndex], PAL_INFINITE);
if (result != PAL_RESULT_SUCCESS) {
logResult(result, "Failed to wait fence");
return PAL_FALSE;
}
}
inFlightImages[imageIndex] = inFlightFences[currentFrame];
if (adapterFeatures & PAL_ADAPTER_FEATURE_FENCE_RESET) {
result = palResetFence(inFlightFences[currentFrame]);
if (result != PAL_RESULT_SUCCESS) {
logResult(result, "Failed to wait fence");
return PAL_FALSE;
}
} else {
// recreate since we dont support fence resetting
palDestroyFence(inFlightFences[currentFrame]);
result = palCreateFence(device, PAL_FALSE, &inFlightFences[currentFrame]);
if (result != PAL_RESULT_SUCCESS) {
logResult(result, "Failed to wait fence");
return PAL_FALSE;
}
}
// reset the command buffer
result = palResetCommandBuffer(cmdBuffers[currentFrame]);
if (result != PAL_RESULT_SUCCESS) {
logResult(result, "Failed to reset command buffer");
return PAL_FALSE;
}
result = palCmdBegin(cmdBuffers[currentFrame], nullptr);
if (result != PAL_RESULT_SUCCESS) {
logResult(result, "Failed to begin command buffer");
return PAL_FALSE;
}
// change the state of the image view to make it renderable
PalBarrierInfo barrierInfo = {0};
barrierInfo.newState = PAL_USAGE_STATE_COLOR_ATTACHMENT_WRITE;
barrierInfo.dstStages = PAL_PIPELINE_STAGE_COLOR_ATTACHMENT;
PalImageSubresourceRange imageRange = {0};
imageRange.layerArrayCount = 1;
imageRange.mipLevelCount = 1;
imageRange.startArrayLayer = 0;
imageRange.startMipLevel = 0;
PalImage* image = palGetSwapchainImage(swapchain, imageIndex);
palCmdImageBarrier(cmdBuffers[currentFrame], image, &imageRange, &barrierInfo);
PalClearValue clearValue;
clearValue.color[0] = 0.2f;
clearValue.color[1] = 0.2f;
clearValue.color[2] = 0.2f;
clearValue.color[3] = 1.0f;
PalAttachmentDesc colorAttachment = {0};
colorAttachment.loadOp = PAL_LOAD_OP_CLEAR;
colorAttachment.storeOp = PAL_STORE_OP_STORE;
colorAttachment.clearValue = clearValue;
colorAttachment.imageView = imageViews[imageIndex];
PalRenderingInfo renderingInfo = {0};
renderingInfo.viewCount = 1;
renderingInfo.colorAttachentCount = 1;
renderingInfo.colorAttachments = &colorAttachment;
renderingInfo.arrayLayerCount = 1;
renderingInfo.viewCount = 1;
renderingInfo.renderArea.width = WINDOW_WIDTH;
renderingInfo.renderArea.height = WINDOW_HEIGHT;
palCmdBeginRendering(cmdBuffers[currentFrame], &renderingInfo);
palCmdBindPipeline(cmdBuffers[currentFrame], pipeline);
palCmdSetViewport(cmdBuffers[currentFrame], 1, &viewport);
palCmdSetScissors(cmdBuffers[currentFrame], 1, &scissor);
// draw a single triangle with the mesh shader
// palBuildWorkGroupInfo() is a helper to build
// the workgroup count per axis using normal
// workCount (image size, buffer size)
palCmdDrawMeshTasks(cmdBuffers[currentFrame], 1, 1, 1);
palCmdEndRendering(cmdBuffers[currentFrame]);
// change the state of the image view to make it presentable
barrierInfo.oldState = barrierInfo.newState;
barrierInfo.srcStages = barrierInfo.dstStages;
barrierInfo.newState = PAL_USAGE_STATE_PRESENT;
barrierInfo.dstStages = PAL_PIPELINE_STAGE_NONE;
palCmdImageBarrier(cmdBuffers[currentFrame], image, &imageRange, &barrierInfo);
result = palCmdEnd(cmdBuffers[currentFrame]);
if (result != PAL_RESULT_SUCCESS) {
logResult(result, "Failed to end command buffer");
return PAL_FALSE;
}
// submit command buffer
PalCommandBufferSubmitInfo submitInfo = {0};
submitInfo.cmdBuffer = cmdBuffers[currentFrame];
submitInfo.fence = inFlightFences[currentFrame];
submitInfo.waitSemaphore = imageAvailableSemaphores[currentFrame];
submitInfo.signalSemaphore = renderFinishedSemaphores[imageIndex];
submitInfo.waitStages = PAL_PIPELINE_STAGE_COLOR_ATTACHMENT;
result = palSubmitCommandBuffer(queue, &submitInfo);
if (result != PAL_RESULT_SUCCESS) {
logResult(result, "Failed to submit command buffer");
return PAL_FALSE;
}
// present
result = palPresentSwapchain(swapchain, imageIndex, renderFinishedSemaphores[imageIndex]);
if (result != PAL_RESULT_SUCCESS) {
logResult(result, "Failed to present swapchain");
return PAL_FALSE;
}
currentFrame = (currentFrame + 1) % MAX_FRAMES_IN_FLIGHT;
}
result = palWaitQueue(queue);
if (result != PAL_RESULT_SUCCESS) {
logResult(result, "Failed to wait queue");
return PAL_FALSE;
}
palDestroyPipeline(pipeline);
palDestroyPipelineLayout(pipelineLayout);
for (int i = 0; i < MAX_FRAMES_IN_FLIGHT; i++) {
palDestroySemaphore(imageAvailableSemaphores[i]);
palDestroyFence(inFlightFences[i]);
palFreeCommandBuffer(cmdBuffers[i]);
}
for (int i = 0; i < imageCount; i++) {
palDestroySemaphore(renderFinishedSemaphores[i]);
palDestroyImageView(imageViews[i]);
}
palDestroyCommandPool(cmdPool);
palDestroySwapchain(swapchain);
palDestroySurface(surface);
palDestroyQueue(queue);
palDestroyDevice(device);
palShutdownGraphics();
palFree(nullptr, imageViews);
palFree(nullptr, renderFinishedSemaphores);
palFree(nullptr, inFlightImages);
palDestroyWindow(window);
palShutdownVideo();
palDestroyEventDriver(eventDriver);
return PAL_TRUE;
}