-
Notifications
You must be signed in to change notification settings - Fork 5
/
xxGraphicMantle.cpp
990 lines (841 loc) · 36.1 KB
/
xxGraphicMantle.cpp
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
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
//==============================================================================
// xxGraphic : Mantle Source
//
// Copyright (c) 2019-2024 TAiGA
// https://github.com/metarutaiga/xxGraphic
//==============================================================================
#include "internal/xxGraphicInternalMantle.h"
#include "xxGraphicMantleAsm.h"
#include "xxGraphicMantle.h"
static void* g_mantleLibrary = nullptr;
static GR_QUEUE g_queue = GR_NULL_HANDLE;
static GR_FENCE g_fences[NUM_BACK_BUFFERS] = {};
static int g_fenceIndex = 0;
static GR_MEMORY_HEAP_PROPERTIES g_heapProps = {};
static GR_UINT g_suitableHeap = 0;
//==============================================================================
// Instance
//==============================================================================
void* grGetProcAddress(char const* name)
{
void* ptr = nullptr;
if (ptr == nullptr && g_mantleLibrary)
ptr = xxGetProcAddress(g_mantleLibrary, name);
if (ptr == nullptr)
xxLog("Mantle", "%s is not found", name);
return ptr;
}
//------------------------------------------------------------------------------
uint64_t xxCreateInstanceMantle()
{
if (g_mantleLibrary == nullptr)
g_mantleLibrary = xxLoadLibrary("mantle32.dll");
if (g_mantleLibrary == nullptr)
g_mantleLibrary = xxLoadLibrary("mantle64.dll");
if (g_mantleLibrary == nullptr)
return 0;
GR_APPLICATION_INFO appInfo = {};
appInfo.apiVersion = GR_API_VERSION;
GR_PHYSICAL_GPU gpus[GR_MAX_PHYSICAL_GPUS] = {};
GR_UINT gpuCount = GR_MAX_PHYSICAL_GPUS;
if (grInitAndEnumerateGpus(&appInfo, nullptr, &gpuCount, gpus) != GR_SUCCESS)
return 0;
xxRegisterFunction(Mantle);
return reinterpret_cast<uint64_t>(gpus[0]);
}
//------------------------------------------------------------------------------
void xxDestroyInstanceMantle(uint64_t instance)
{
if (g_mantleLibrary)
{
xxFreeLibrary(g_mantleLibrary);
g_mantleLibrary = nullptr;
}
xxUnregisterFunction();
}
//==============================================================================
// Device
//==============================================================================
uint64_t xxCreateDeviceMantle(uint64_t instance)
{
GR_PHYSICAL_GPU gpu = reinterpret_cast<GR_PHYSICAL_GPU>(instance);
GR_DEVICE_QUEUE_CREATE_INFO queueInfo = {};
queueInfo.queueType = GR_QUEUE_UNIVERSAL;
queueInfo.queueCount = 1;
const GR_CHAR* const extensions[] = { "GR_WSI_WINDOWS" };
GR_DEVICE_CREATE_INFO deviceInfo = {};
deviceInfo.queueRecordCount = 1;
deviceInfo.pRequestedQueues = &queueInfo;
deviceInfo.extensionCount = 1;
deviceInfo.ppEnabledExtensionNames = extensions;
deviceInfo.flags |= GR_DEVICE_CREATE_VALIDATION;
deviceInfo.maxValidationLevel = GR_VALIDATION_LEVEL_4;
GR_DEVICE device = GR_NULL_HANDLE;
if (grCreateDevice(gpu, &deviceInfo, &device) != GR_SUCCESS)
return 0;
if (grGetDeviceQueue(device, GR_QUEUE_UNIVERSAL, 0, &g_queue) != GR_SUCCESS)
return 0;
GR_FENCE_CREATE_INFO fenceCreateInfo = {};
for (int i = 0; i < NUM_BACK_BUFFERS; ++i)
{
GR_FENCE fence = GR_NULL_HANDLE;
grCreateFence(device, &fenceCreateInfo, &fence);
g_fences[i] = fence;
}
GR_UINT heapCount;
grGetMemoryHeapCount(device, &heapCount);
for (GR_UINT i = 0; i < heapCount; i++)
{
memset(&g_heapProps, 0, sizeof(g_heapProps));
GR_SIZE heapPropSize = sizeof(g_heapProps);
grGetMemoryHeapInfo(device, i, GR_INFO_TYPE_MEMORY_HEAP_PROPERTIES, &heapPropSize, &g_heapProps);
if (g_heapProps.flags & GR_MEMORY_HEAP_CPU_VISIBLE)
{
g_suitableHeap = i;
break;
}
}
return reinterpret_cast<uint64_t>(device);
}
//------------------------------------------------------------------------------
void xxDestroyDeviceMantle(uint64_t device)
{
GR_DEVICE grDevice = reinterpret_cast<GR_DEVICE>(device);
if (grDevice == GR_NULL_HANDLE)
return;
for (int i = 0; i < NUM_BACK_BUFFERS; ++i)
{
GR_FENCE fence = g_fences[i];
grDestroyObject(fence);
}
grDestroyDevice(grDevice);
}
//------------------------------------------------------------------------------
bool xxResetDeviceMantle(uint64_t device)
{
return true;
}
//------------------------------------------------------------------------------
bool xxTestDeviceMantle(uint64_t device)
{
return true;
}
//==============================================================================
// Swapchain
//==============================================================================
uint64_t xxCreateSwapchainMantle(uint64_t device, uint64_t renderPass, void* view, int width, int height, uint64_t oldSwapchain)
{
GR_DEVICE grDevice = reinterpret_cast<GR_DEVICE>(device);
if (grDevice == GR_NULL_HANDLE)
return 0;
GRSWAPCHAIN* grOldSwapchain = reinterpret_cast<GRSWAPCHAIN*>(oldSwapchain);
if (grOldSwapchain && grOldSwapchain->view == view && grOldSwapchain->width == width && grOldSwapchain->height == height)
return oldSwapchain;
GRSWAPCHAIN* grSwapchain = xxAlloc(GRSWAPCHAIN, 1);
if (grSwapchain == nullptr)
return 0;
xxDestroySwapchainMantle(oldSwapchain);
HWND hWnd = (HWND)view;
GR_WSI_WIN_PRESENTABLE_IMAGE_CREATE_INFO imageCreateInfo = {};
imageCreateInfo.format.channelFormat = GR_CH_FMT_B8G8R8A8;
imageCreateInfo.format.numericFormat = GR_NUM_FMT_UNORM;
imageCreateInfo.usage = GR_IMAGE_USAGE_COLOR_TARGET;
imageCreateInfo.extent.width = width;
imageCreateInfo.extent.height = height;
GR_IMAGE colorImage = GR_NULL_HANDLE;
GR_GPU_MEMORY colorMemory = GR_NULL_HANDLE;
if (grWsiWinCreatePresentableImage(grDevice, &imageCreateInfo, &colorImage, &colorMemory) != GR_SUCCESS)
return 0;
GR_COLOR_TARGET_VIEW_CREATE_INFO colorTargetViewCreateInfo = {};
colorTargetViewCreateInfo.image = colorImage;
colorTargetViewCreateInfo.arraySize = 1;
colorTargetViewCreateInfo.baseArraySlice = 0;
colorTargetViewCreateInfo.mipLevel = 0;
colorTargetViewCreateInfo.format.channelFormat = GR_CH_FMT_B8G8R8A8;
colorTargetViewCreateInfo.format.numericFormat = GR_NUM_FMT_UNORM;
GR_COLOR_TARGET_VIEW colorTargetView = GR_NULL_HANDLE;
grCreateColorTargetView(grDevice, &colorTargetViewCreateInfo, &colorTargetView);
GR_MEMORY_REF imageMemoryRef = {};
imageMemoryRef.mem = colorMemory;
GR_IMAGE_SUBRESOURCE_RANGE imageColorRange = {};
imageColorRange.aspect = GR_IMAGE_ASPECT_COLOR;
imageColorRange.baseMipLevel = 0;
imageColorRange.mipLevels = 1;
imageColorRange.baseArraySlice = 0;
imageColorRange.arraySize = 1;
GR_CMD_BUFFER_CREATE_INFO bufferCreateInfo = {};
bufferCreateInfo.queueType = GR_QUEUE_UNIVERSAL;
GR_CMD_BUFFER initCmdBuffer;
grCreateCommandBuffer(grDevice, &bufferCreateInfo, &initCmdBuffer);
grBeginCommandBuffer(initCmdBuffer, GR_CMD_BUFFER_OPTIMIZE_ONE_TIME_SUBMIT);
GR_IMAGE_STATE_TRANSITION initTransition = {};
initTransition.image = colorImage;
initTransition.oldState = GR_IMAGE_STATE_UNINITIALIZED;
initTransition.newState = GR_WSI_WIN_IMAGE_STATE_PRESENT_WINDOWED;
initTransition.subresourceRange = imageColorRange;
grCmdPrepareImages(initCmdBuffer, 1, &initTransition);
grEndCommandBuffer(initCmdBuffer);
GR_FENCE fence = g_fences[g_fenceIndex];
grQueueSubmit(g_queue, 1, &initCmdBuffer, 1, &imageMemoryRef, fence);
grWaitForFences(grDevice, 1, &fence, GR_TRUE, 2.0f);
grDestroyObject(initCmdBuffer);
g_fenceIndex++;
if (g_fenceIndex >= NUM_BACK_BUFFERS)
g_fenceIndex = 0;
grSwapchain->colorImage = colorImage;
grSwapchain->colorMemory = colorMemory;
grSwapchain->colorTargetView = colorTargetView;
for (int i = 0; i < NUM_BACK_BUFFERS; ++i)
{
GR_CMD_BUFFER commandBuffer;
grCreateCommandBuffer(grDevice, &bufferCreateInfo, &commandBuffer);
grSwapchain->commandBuffers[i] = commandBuffer;
}
grSwapchain->commandBufferIndex = 0;
grSwapchain->view = view;
grSwapchain->width = width;
grSwapchain->height = height;
return reinterpret_cast<uint64_t>(grSwapchain);
}
//------------------------------------------------------------------------------
void xxDestroySwapchainMantle(uint64_t swapchain)
{
GRSWAPCHAIN* grSwapchain = reinterpret_cast<GRSWAPCHAIN*>(swapchain);
if (grSwapchain == nullptr)
return;
xxFree(grSwapchain);
}
//------------------------------------------------------------------------------
void xxPresentSwapchainMantle(uint64_t swapchain)
{
GRSWAPCHAIN* grSwapchain = reinterpret_cast<GRSWAPCHAIN*>(swapchain);
if (grSwapchain == nullptr)
return;
GR_WSI_WIN_PRESENT_INFO presentInfo = {};
presentInfo.hWndDest = (HWND)grSwapchain->view;
presentInfo.srcImage = grSwapchain->colorImage;
presentInfo.presentMode = GR_WSI_WIN_PRESENT_MODE_WINDOWED;
grWsiWinQueuePresent(g_queue, &presentInfo);
grSwapchain->commandBufferIndex++;
if (grSwapchain->commandBufferIndex >= NUM_BACK_BUFFERS)
grSwapchain->commandBufferIndex = 0;
}
//------------------------------------------------------------------------------
uint64_t xxGetCommandBufferMantle(uint64_t device, uint64_t swapchain)
{
GR_DEVICE grDevice = reinterpret_cast<GR_DEVICE>(device);
if (grDevice == GR_NULL_HANDLE)
return 0;
GRSWAPCHAIN* grSwapchain = reinterpret_cast<GRSWAPCHAIN*>(swapchain);
if (grSwapchain == nullptr)
return 0;
GR_FENCE fence = g_fences[g_fenceIndex];
grWaitForFences(grDevice, 1, &fence, GR_TRUE, 1.0f);
g_fenceIndex++;
if (g_fenceIndex >= NUM_BACK_BUFFERS)
g_fenceIndex = 0;
GR_CMD_BUFFER commandBuffer = grSwapchain->commandBuffers[grSwapchain->commandBufferIndex];
grResetCommandBuffer(commandBuffer);
return reinterpret_cast<uint64_t>(commandBuffer);
}
//------------------------------------------------------------------------------
uint64_t xxGetFramebufferMantle(uint64_t device, uint64_t swapchain, float* scale)
{
if (scale)
{
(*scale) = 1.0f;
}
GRFRAMEBUFFER* grFramebuffer = reinterpret_cast<GRSWAPCHAIN*>(swapchain);
if (grFramebuffer == nullptr)
return 0;
return reinterpret_cast<uint64_t>(grFramebuffer);
}
//==============================================================================
// Command Buffer
//==============================================================================
bool xxBeginCommandBufferMantle(uint64_t commandBuffer)
{
GR_CMD_BUFFER grCommandBuffer = reinterpret_cast<GR_CMD_BUFFER>(commandBuffer);
if (grCommandBuffer == GR_NULL_HANDLE)
return false;
grBeginCommandBuffer(grCommandBuffer, 0);
return true;
}
//------------------------------------------------------------------------------
void xxEndCommandBufferMantle(uint64_t commandBuffer)
{
GR_CMD_BUFFER grCommandBuffer = reinterpret_cast<GR_CMD_BUFFER>(commandBuffer);
grEndCommandBuffer(grCommandBuffer);
}
//------------------------------------------------------------------------------
void xxSubmitCommandBufferMantle(uint64_t commandBuffer, uint64_t swapchain)
{
GR_CMD_BUFFER grCommandBuffer = reinterpret_cast<GR_CMD_BUFFER>(commandBuffer);
grQueueSubmit(g_queue, 1, &grCommandBuffer, 0, nullptr, g_fences[g_fenceIndex]);
}
//==============================================================================
// Render Pass
//==============================================================================
uint64_t xxCreateRenderPassMantle(uint64_t device, bool clearColor, bool clearDepth, bool clearStencil, bool storeColor, bool storeDepth, bool storeStencil)
{
GRRENDERPASS grRenderPass = {};
if (clearColor)
grRenderPass.clearColor = true;
if (clearDepth || clearStencil)
grRenderPass.clearDepthStencil = true;
return static_cast<uint64_t>(grRenderPass.value);
}
//------------------------------------------------------------------------------
void xxDestroyRenderPassMantle(uint64_t renderPass)
{
}
//------------------------------------------------------------------------------
uint64_t xxBeginRenderPassMantle(uint64_t commandBuffer, uint64_t framebuffer, uint64_t renderPass, int width, int height, float color[4], float depth, unsigned char stencil)
{
GR_CMD_BUFFER grCommandBuffer = reinterpret_cast<GR_CMD_BUFFER>(commandBuffer);
if (grCommandBuffer == GR_NULL_HANDLE)
return 0;
GRFRAMEBUFFER* grFramebuffer = reinterpret_cast<GRSWAPCHAIN*>(framebuffer);
if (grFramebuffer == nullptr)
return 0;
GRRENDERPASS grRenderPass = { renderPass };
if (grRenderPass.clearColor)
{
grCmdClearColorImage(grCommandBuffer, grFramebuffer->colorImage, color, 0, nullptr);
}
if (grRenderPass.clearDepthStencil)
{
grCmdClearDepthStencil(grCommandBuffer, grFramebuffer->depthStencilImage, depth, stencil, 0, nullptr);
}
return commandBuffer;
}
//------------------------------------------------------------------------------
void xxEndRenderPassMantle(uint64_t commandEncoder, uint64_t framebuffer, uint64_t renderPass)
{
}
//==============================================================================
// Vertex Attribute
//==============================================================================
uint64_t xxCreateVertexAttributeMantle(uint64_t device, int count, int* attribute)
{
GRVERTEXATTRIBUTE* grVertexAttribute = xxAlloc(GRVERTEXATTRIBUTE, 1);
if (grVertexAttribute == nullptr)
return 0;
GR_MEMORY_VIEW_ATTACH_INFO* infos = grVertexAttribute->infos;
int stride = 0;
for (int i = 0; i < count; ++i)
{
int stream = (*attribute++);
int offset = (*attribute++);
int element = (*attribute++);
int size = (*attribute++);
(void)stream;
stride += size;
GR_MEMORY_VIEW_ATTACH_INFO& info = infos[i];
info.mem = GR_NULL_HANDLE;
info.offset = offset;
info.range = size;
info.stride = 0;
info.format.channelFormat = GR_CH_FMT_R32;
info.format.numericFormat = GR_NUM_FMT_FLOAT;
info.state = GR_MEMORY_STATE_GRAPHICS_SHADER_READ_ONLY;
if (element == 'POS3' && size == sizeof(float) * 3)
{
info.format.channelFormat = GR_CH_FMT_R32G32B32;
continue;
}
if (element == 'BON3' && size == sizeof(float) * 3)
{
info.format.channelFormat = GR_CH_FMT_R32G32B32;
continue;
}
if (element == 'BON4' && size == sizeof(char) * 4)
{
info.format.channelFormat = GR_CH_FMT_R8G8B8A8;
info.format.numericFormat = GR_NUM_FMT_UINT;
continue;
}
if (element == 'COL4' && size == sizeof(char) * 4)
{
info.format.channelFormat = GR_CH_FMT_B8G8R8A8;
info.format.numericFormat = GR_NUM_FMT_UNORM;
continue;
}
if (element == 'TEX2' && size == sizeof(float) * 2)
{
info.format.channelFormat = GR_CH_FMT_R32G32;
continue;
}
}
for (int i = 0; i < count; ++i)
{
infos[i].stride = stride;
}
grVertexAttribute->count = count;
grVertexAttribute->stride = stride;
return reinterpret_cast<uint64_t>(grVertexAttribute);
}
//------------------------------------------------------------------------------
void xxDestroyVertexAttributeMantle(uint64_t vertexAttribute)
{
GRVERTEXATTRIBUTE* grVertexAttribute = reinterpret_cast<GRVERTEXATTRIBUTE*>(vertexAttribute);
xxFree(grVertexAttribute);
}
//==============================================================================
// Buffer
//==============================================================================
uint64_t xxCreateConstantBufferMantle(uint64_t device, int size)
{
GR_DEVICE grDevice = reinterpret_cast<GR_DEVICE>(device);
if (grDevice == GR_NULL_HANDLE)
return 0;
intptr_t pageSize = (intptr_t)g_heapProps.pageSize;
GR_MEMORY_ALLOC_INFO allocInfo = {};
allocInfo.size = ((1 + size) / pageSize) * pageSize;
allocInfo.alignment = 0;
allocInfo.memPriority = GR_MEMORY_PRIORITY_HIGH;
allocInfo.heapCount = 1;
allocInfo.heaps[0] = g_suitableHeap;
GR_GPU_MEMORY memory = GR_NULL_HANDLE;
grAllocMemory(grDevice, &allocInfo, &memory);
return reinterpret_cast<uint64_t>(memory);
}
//------------------------------------------------------------------------------
uint64_t xxCreateIndexBufferMantle(uint64_t device, int size, int bits)
{
GR_DEVICE grDevice = reinterpret_cast<GR_DEVICE>(device);
if (grDevice == GR_NULL_HANDLE)
return 0;
intptr_t pageSize = (intptr_t)g_heapProps.pageSize;
GR_MEMORY_ALLOC_INFO allocInfo = {};
allocInfo.size = ((1 + size) / pageSize) * pageSize;
allocInfo.alignment = 0;
allocInfo.memPriority = GR_MEMORY_PRIORITY_HIGH;
allocInfo.heapCount = 1;
allocInfo.heaps[0] = g_suitableHeap;
GR_GPU_MEMORY memory = GR_NULL_HANDLE;
grAllocMemory(grDevice, &allocInfo, &memory);
return reinterpret_cast<uint64_t>(memory);
}
//------------------------------------------------------------------------------
uint64_t xxCreateVertexBufferMantle(uint64_t device, int size, uint64_t vertexAttribute)
{
GR_DEVICE grDevice = reinterpret_cast<GR_DEVICE>(device);
if (grDevice == GR_NULL_HANDLE)
return 0;
intptr_t pageSize = (intptr_t)g_heapProps.pageSize;
GR_MEMORY_ALLOC_INFO allocInfo = {};
allocInfo.size = ((1 + size) / pageSize) * pageSize;
allocInfo.alignment = 0;
allocInfo.memPriority = GR_MEMORY_PRIORITY_HIGH;
allocInfo.heapCount = 1;
allocInfo.heaps[0] = g_suitableHeap;
GR_GPU_MEMORY memory = GR_NULL_HANDLE;
grAllocMemory(grDevice, &allocInfo, &memory);
return reinterpret_cast<uint64_t>(memory);
}
//------------------------------------------------------------------------------
uint64_t xxCreateStorageBufferMantle(uint64_t device, int size)
{
GR_DEVICE grDevice = reinterpret_cast<GR_DEVICE>(device);
if (grDevice == GR_NULL_HANDLE)
return 0;
intptr_t pageSize = (intptr_t)g_heapProps.pageSize;
GR_MEMORY_ALLOC_INFO allocInfo = {};
allocInfo.size = ((1 + size) / pageSize) * pageSize;
allocInfo.alignment = 0;
allocInfo.memPriority = GR_MEMORY_PRIORITY_HIGH;
allocInfo.heapCount = 1;
allocInfo.heaps[0] = g_suitableHeap;
GR_GPU_MEMORY memory = GR_NULL_HANDLE;
grAllocMemory(grDevice, &allocInfo, &memory);
return reinterpret_cast<uint64_t>(memory);
}
//------------------------------------------------------------------------------
void xxDestroyBufferMantle(uint64_t device, uint64_t buffer)
{
GR_GPU_MEMORY memory = reinterpret_cast<GR_GPU_MEMORY>(buffer);
if (memory == GR_NULL_HANDLE)
return;
grFreeMemory(memory);
}
//------------------------------------------------------------------------------
void* xxMapBufferMantle(uint64_t device, uint64_t buffer)
{
GR_GPU_MEMORY memory = reinterpret_cast<GR_GPU_MEMORY>(buffer);
if (memory == GR_NULL_HANDLE)
return nullptr;
void* ptr = nullptr;
grMapMemory(memory, 0, &ptr);
return ptr;
}
//------------------------------------------------------------------------------
void xxUnmapBufferMantle(uint64_t device, uint64_t buffer)
{
GR_GPU_MEMORY memory = reinterpret_cast<GR_GPU_MEMORY>(buffer);
if (memory == GR_NULL_HANDLE)
return;
grUnmapMemory(memory);
}
//==============================================================================
// Texture
//==============================================================================
uint64_t xxCreateTextureMantle(uint64_t device, uint64_t format, int width, int height, int depth, int mipmap, int array, void const* external)
{
return 0;
}
//------------------------------------------------------------------------------
void xxDestroyTextureMantle(uint64_t texture)
{
}
//------------------------------------------------------------------------------
void* xxMapTextureMantle(uint64_t device, uint64_t texture, int* stride, int level, int array)
{
return nullptr;
}
//------------------------------------------------------------------------------
void xxUnmapTextureMantle(uint64_t device, uint64_t texture, int level, int array)
{
}
//==============================================================================
// Sampler
//==============================================================================
uint64_t xxCreateSamplerMantle(uint64_t device, bool clampU, bool clampV, bool clampW, bool linearMag, bool linearMin, bool linearMip, int anisotropy)
{
GR_DEVICE grDevice = reinterpret_cast<GR_DEVICE>(device);
if (grDevice == GR_NULL_HANDLE)
return 0;
const GR_TEX_FILTER filters[2][2][2] =
{
{
{ GR_TEX_FILTER_MAG_POINT_MIN_POINT_MIP_POINT, GR_TEX_FILTER_MAG_POINT_MIN_POINT_MIP_LINEAR },
{ GR_TEX_FILTER_MAG_POINT_MIN_LINEAR_MIP_POINT, GR_TEX_FILTER_MAG_POINT_MIN_LINEAR_MIP_LINEAR },
}, {
{ GR_TEX_FILTER_MAG_LINEAR_MIN_POINT_MIP_POINT, GR_TEX_FILTER_MAG_LINEAR_MIN_POINT_MIP_LINEAR },
{ GR_TEX_FILTER_MAG_LINEAR_MIN_LINEAR_MIP_POINT, GR_TEX_FILTER_MAG_LINEAR_MIN_LINEAR_MIP_LINEAR },
}
};
GR_SAMPLER_CREATE_INFO info = {};
info.filter = filters[linearMag][linearMin][linearMip];
info.addressU = clampU ? GR_TEX_ADDRESS_CLAMP : GR_TEX_ADDRESS_WRAP;
info.addressV = clampV ? GR_TEX_ADDRESS_CLAMP : GR_TEX_ADDRESS_WRAP;
info.addressW = clampW ? GR_TEX_ADDRESS_CLAMP : GR_TEX_ADDRESS_WRAP;
info.compareFunc = GR_COMPARE_NEVER;
info.maxLod = 1000.0f;
if (anisotropy > 0)
{
info.maxAnisotropy = anisotropy;
}
GR_SAMPLER sampler = GR_NULL_HANDLE;
grCreateSampler(grDevice, &info, &sampler);
return reinterpret_cast<uint64_t>(sampler);
}
//------------------------------------------------------------------------------
void xxDestroySamplerMantle(uint64_t sampler)
{
GR_SAMPLER grSampler = reinterpret_cast<GR_SAMPLER>(sampler);
if (grSampler == GR_NULL_HANDLE)
return;
grDestroyObject(grSampler);
}
//==============================================================================
// Shader
//==============================================================================
uint64_t xxCreateMeshShaderMantle(uint64_t device, char const* shader)
{
return 0;
}
//------------------------------------------------------------------------------
uint64_t xxCreateVertexShaderMantle(uint64_t device, char const* shader, uint64_t vertexAttribute)
{
GR_DEVICE grDevice = reinterpret_cast<GR_DEVICE>(device);
if (grDevice == GR_NULL_HANDLE)
return 0;
if (strcmp(shader, "default") == 0)
{
GR_SHADER_CREATE_INFO info = {};
info.pCode = vertexShaderCodeIL;
info.codeSize = vertexShaderCodeILSize;
GR_SHADER grShader = GR_NULL_HANDLE;
grCreateShader(grDevice, &info, &grShader);
return reinterpret_cast<uint64_t>(grShader);
}
return 0;
}
//------------------------------------------------------------------------------
uint64_t xxCreateFragmentShaderMantle(uint64_t device, char const* shader)
{
GR_DEVICE grDevice = reinterpret_cast<GR_DEVICE>(device);
if (grDevice == GR_NULL_HANDLE)
return 0;
if (strcmp(shader, "default") == 0)
{
GR_SHADER_CREATE_INFO info = {};
info.pCode = pixelShaderCodeIL;
info.codeSize = pixelShaderCodeILSize;
GR_SHADER grShader = GR_NULL_HANDLE;
grCreateShader(grDevice, &info, &grShader);
return reinterpret_cast<uint64_t>(grShader);
}
return 0;
}
//------------------------------------------------------------------------------
void xxDestroyShaderMantle(uint64_t device, uint64_t shader)
{
GR_SHADER grShader = reinterpret_cast<GR_SHADER>(shader);
if (grShader == GR_NULL_HANDLE)
return;
grDestroyObject(grShader);
}
//==============================================================================
// Pipeline
//==============================================================================
uint64_t xxCreateBlendStateMantle(uint64_t device, char const* sourceColor, char const* operationColor, char const* destinationColor, char const* sourceAlpha, char const* operationAlpha, char const* destinationAlpha)
{
GR_DEVICE grDevice = reinterpret_cast<GR_DEVICE>(device);
if (grDevice == GR_NULL_HANDLE)
return 0;
GR_COLOR_BLEND_STATE_CREATE_INFO info = {};
info.target[0].srcBlendColor = grBlendFactor(sourceColor);
info.target[0].destBlendColor = grBlendFactor(destinationColor);
info.target[0].blendFuncColor = grBlendOp(operationColor);
info.target[0].srcBlendAlpha = grBlendFactor(sourceAlpha);
info.target[0].destBlendAlpha = grBlendFactor(destinationAlpha);
info.target[0].blendFuncAlpha = grBlendOp(operationAlpha);
info.target[0].blendEnable = (info.target[0].srcBlendColor != GR_BLEND_ONE ||
info.target[0].destBlendColor != GR_BLEND_ZERO ||
info.target[0].blendFuncColor != GR_BLEND_FUNC_ADD ||
info.target[0].srcBlendAlpha != GR_BLEND_ONE ||
info.target[0].destBlendAlpha != GR_BLEND_ZERO ||
info.target[0].blendFuncAlpha != GR_BLEND_FUNC_ADD) ? GR_TRUE : GR_FALSE;
GR_COLOR_BLEND_STATE_OBJECT blendState = GR_NULL_HANDLE;
grCreateColorBlendState(grDevice, &info, &blendState);
return reinterpret_cast<uint64_t>(blendState);
}
//------------------------------------------------------------------------------
uint64_t xxCreateDepthStencilStateMantle(uint64_t device, char const* depthTest, bool depthWrite)
{
GR_DEVICE grDevice = reinterpret_cast<GR_DEVICE>(device);
if (grDevice == GR_NULL_HANDLE)
return 0;
GR_DEPTH_STENCIL_STATE_CREATE_INFO info = {};
info.depthWriteEnable = depthWrite ? GR_TRUE : GR_FALSE;
info.depthFunc = grCompareOp(depthTest);
info.depthEnable = (info.depthFunc != GR_COMPARE_ALWAYS) ? GR_TRUE : GR_FALSE;
GR_DEPTH_STENCIL_STATE_OBJECT depthStencilState = GR_NULL_HANDLE;
grCreateDepthStencilState(grDevice, &info, &depthStencilState);
return reinterpret_cast<uint64_t>(depthStencilState);
}
//------------------------------------------------------------------------------
uint64_t xxCreateRasterizerStateMantle(uint64_t device, bool cull, bool scissor)
{
GR_DEVICE grDevice = reinterpret_cast<GR_DEVICE>(device);
if (grDevice == GR_NULL_HANDLE)
return 0;
GR_RASTER_STATE_CREATE_INFO info = {};
info.fillMode = GR_FILL_SOLID;
info.cullMode = cull ? GR_CULL_BACK : GR_CULL_NONE;
info.frontFace = GR_FRONT_FACE_CW;
GR_RASTER_STATE_OBJECT rasterizerState = GR_NULL_HANDLE;
grCreateRasterState(grDevice, &info, &rasterizerState);
return reinterpret_cast<uint64_t>(rasterizerState);
}
//------------------------------------------------------------------------------
uint64_t xxCreatePipelineMantle(uint64_t device, uint64_t renderPass, uint64_t blendState, uint64_t depthStencilState, uint64_t rasterizerState, uint64_t vertexAttribute, uint64_t meshShader, uint64_t vertexShader, uint64_t fragmentShader)
{
GR_DEVICE grDevice = reinterpret_cast<GR_DEVICE>(device);
if (grDevice == GR_NULL_HANDLE)
return 0;
GR_SHADER grVertexShader = reinterpret_cast<GR_SHADER>(vertexShader);
if (grVertexShader == GR_NULL_HANDLE)
return 0;
GR_SHADER grPixelShader = reinterpret_cast<GR_SHADER>(fragmentShader);
if (grPixelShader == GR_NULL_HANDLE)
return 0;
GR_DESCRIPTOR_SLOT_INFO slotInfo = {};
slotInfo.slotObjectType = GR_SLOT_SHADER_RESOURCE;
slotInfo.shaderEntityIndex = 0;
GR_GRAPHICS_PIPELINE_CREATE_INFO pipelineInfo = {};
pipelineInfo.vs.shader = grVertexShader;
pipelineInfo.vs.descriptorSetMapping[0].descriptorCount = 1;
pipelineInfo.vs.descriptorSetMapping[0].pDescriptorInfo = &slotInfo;
pipelineInfo.vs.dynamicMemoryViewMapping.slotObjectType = GR_SLOT_UNUSED;
pipelineInfo.ps.shader = grPixelShader;
pipelineInfo.ps.dynamicMemoryViewMapping.slotObjectType = GR_SLOT_UNUSED;
pipelineInfo.iaState.topology = GR_TOPOLOGY_TRIANGLE_LIST;
pipelineInfo.rsState.depthClipEnable = GR_FALSE;
pipelineInfo.cbState.logicOp = GR_LOGIC_OP_COPY;
pipelineInfo.cbState.target[0].blendEnable = GR_FALSE;
pipelineInfo.cbState.target[0].channelWriteMask = 0xF;
pipelineInfo.cbState.target[0].format.channelFormat = GR_CH_FMT_B8G8R8A8;
pipelineInfo.cbState.target[0].format.numericFormat = GR_NUM_FMT_UNORM;
pipelineInfo.dbState.format.channelFormat = GR_CH_FMT_R32G8;
pipelineInfo.dbState.format.numericFormat = GR_NUM_FMT_DS;
GR_PIPELINE pipeline = GR_NULL_HANDLE;
grCreateGraphicsPipeline(grDevice, &pipelineInfo, &pipeline);
return reinterpret_cast<uint64_t>(pipeline);
}
//------------------------------------------------------------------------------
void xxDestroyBlendStateMantle(uint64_t blendState)
{
GR_COLOR_BLEND_STATE_OBJECT grBlendState = reinterpret_cast<GR_COLOR_BLEND_STATE_OBJECT>(blendState);
if (grBlendState == GR_NULL_HANDLE)
return;
grDestroyObject(grBlendState);
}
//------------------------------------------------------------------------------
void xxDestroyDepthStencilStateMantle(uint64_t depthStencilState)
{
GR_DEPTH_STENCIL_STATE_OBJECT grDepthStencilState = reinterpret_cast<GR_DEPTH_STENCIL_STATE_OBJECT>(depthStencilState);
if (grDepthStencilState == GR_NULL_HANDLE)
return;
grDestroyObject(grDepthStencilState);
}
//------------------------------------------------------------------------------
void xxDestroyRasterizerStateMantle(uint64_t rasterizerState)
{
GR_RASTER_STATE_OBJECT grRasterizerState = reinterpret_cast<GR_PIPELINE>(rasterizerState);
if (grRasterizerState == GR_NULL_HANDLE)
return;
grDestroyObject(grRasterizerState);
}
//------------------------------------------------------------------------------
void xxDestroyPipelineMantle(uint64_t pipeline)
{
GR_PIPELINE grPipeline = reinterpret_cast<GR_PIPELINE>(pipeline);
if (grPipeline == GR_NULL_HANDLE)
return;
grDestroyObject(grPipeline);
}
//==============================================================================
// Command
//==============================================================================
void xxSetViewportMantle(uint64_t commandEncoder, int x, int y, int width, int height, float minZ, float maxZ)
{
}
//------------------------------------------------------------------------------
void xxSetScissorMantle(uint64_t commandEncoder, int x, int y, int width, int height)
{
}
//------------------------------------------------------------------------------
void xxSetPipelineMantle(uint64_t commandEncoder, uint64_t pipeline)
{
GR_CMD_BUFFER grCommandBuffer = reinterpret_cast<GR_CMD_BUFFER>(commandEncoder);
GR_PIPELINE grPipeline = reinterpret_cast<GR_PIPELINE>(pipeline);
grCmdBindPipeline(grCommandBuffer, GR_PIPELINE_BIND_POINT_GRAPHICS, grPipeline);
}
//------------------------------------------------------------------------------
void xxSetMeshBuffersMantle(uint64_t commandEncoder, int count, const uint64_t* buffers)
{
}
//------------------------------------------------------------------------------
void xxSetVertexBuffersMantle(uint64_t commandEncoder, int count, const uint64_t* buffers, uint64_t vertexAttribute)
{
GRVERTEXATTRIBUTE* grVertexAttribute = reinterpret_cast<GRVERTEXATTRIBUTE*>(vertexAttribute);
GR_MEMORY_VIEW_ATTACH_INFO* infos = grVertexAttribute->infos;
for (int i = 0; i < count; ++i)
{
GR_GPU_MEMORY grMemory = reinterpret_cast<GR_GPU_MEMORY>(buffers[i]);
GR_MEMORY_VIEW_ATTACH_INFO& info = infos[i];
info.mem = grMemory;
}
grAttachMemoryViewDescriptors(0, BASE_VERTEX_ATTRIBUTE, count, infos);
}
//------------------------------------------------------------------------------
void xxSetVertexTexturesMantle(uint64_t commandEncoder, int count, const uint64_t* textures)
{
GR_IMAGE_VIEW_ATTACH_INFO infos[8];
for (int i = 0; i < count; ++i)
{
GR_IMAGE_VIEW_ATTACH_INFO& info = infos[i];
info.view;
info.state = GR_MEMORY_STATE_GRAPHICS_SHADER_READ_ONLY;
}
grAttachImageViewDescriptors(0, BASE_VERTEX_TEXTURE, count, infos);
}
//------------------------------------------------------------------------------
void xxSetFragmentTexturesMantle(uint64_t commandEncoder, int count, const uint64_t* textures)
{
GR_IMAGE_VIEW_ATTACH_INFO infos[8];
for (int i = 0; i < count; ++i)
{
GR_IMAGE_VIEW_ATTACH_INFO& info = infos[i];
info.view;
info.state = GR_MEMORY_STATE_GRAPHICS_SHADER_READ_ONLY;
}
grAttachImageViewDescriptors(0, BASE_PIXEL_TEXTURE, count, infos);
}
//------------------------------------------------------------------------------
void xxSetVertexSamplersMantle(uint64_t commandEncoder, int count, const uint64_t* samplers)
{
GR_SAMPLER grSamplers[8];
for (int i = 0; i < count; ++i)
{
GR_SAMPLER grSampler = reinterpret_cast<GR_SAMPLER>(samplers[i]);
grSamplers[i] = grSampler;
}
grAttachSamplerDescriptors(0, BASE_VERTEX_SAMPLER, count, grSamplers);
}
//------------------------------------------------------------------------------
void xxSetFragmentSamplersMantle(uint64_t commandEncoder, int count, const uint64_t* samplers)
{
GR_SAMPLER grSamplers[8];
for (int i = 0; i < count; ++i)
{
GR_SAMPLER grSampler = reinterpret_cast<GR_SAMPLER>(samplers[i]);
grSamplers[i] = grSampler;
}
grAttachSamplerDescriptors(0, BASE_PIXEL_SAMPLER, count, grSamplers);
}
//------------------------------------------------------------------------------
void xxSetMeshConstantBufferMantle(uint64_t commandEncoder, uint64_t buffer, int size)
{
}
//------------------------------------------------------------------------------
void xxSetVertexConstantBufferMantle(uint64_t commandEncoder, uint64_t buffer, int size)
{
GR_GPU_MEMORY grMemory = reinterpret_cast<GR_GPU_MEMORY>(buffer);
GR_MEMORY_VIEW_ATTACH_INFO info = {};
info.mem = grMemory;
info.offset = 0;
info.range = size;
info.stride = 0;
info.format.channelFormat = GR_CH_FMT_R32G32B32A32;
info.format.numericFormat = GR_NUM_FMT_FLOAT;
info.state = GR_MEMORY_STATE_GRAPHICS_SHADER_READ_ONLY;
grAttachMemoryViewDescriptors(0, BASE_VERTEX_CONSTANT, 1, &info);
}
//------------------------------------------------------------------------------
void xxSetFragmentConstantBufferMantle(uint64_t commandEncoder, uint64_t buffer, int size)
{
GR_GPU_MEMORY grMemory = reinterpret_cast<GR_GPU_MEMORY>(buffer);
GR_MEMORY_VIEW_ATTACH_INFO info = {};
info.mem = grMemory;
info.offset = 0;
info.range = size;
info.stride = 0;
info.format.channelFormat = GR_CH_FMT_R32G32B32A32;
info.format.numericFormat = GR_NUM_FMT_FLOAT;
info.state = GR_MEMORY_STATE_GRAPHICS_SHADER_READ_ONLY;
grAttachMemoryViewDescriptors(0, BASE_PIXEL_CONSTANT, 1, &info);
}
//------------------------------------------------------------------------------
void xxDrawMantle(uint64_t commandEncoder, int vertexCount, int instanceCount, int firstVertex, int firstInstance)
{
GR_CMD_BUFFER grCommandBuffer = reinterpret_cast<GR_CMD_BUFFER>(commandEncoder);
grCmdDraw(grCommandBuffer, firstVertex, vertexCount, firstInstance, instanceCount);
}
//------------------------------------------------------------------------------
void xxDrawMeshedMantle(uint64_t commandEncoder, int x, int y, int z)
{
}
//------------------------------------------------------------------------------
void xxDrawIndexedMantle(uint64_t commandEncoder, uint64_t indexBuffer, int indexCount, int vertexCount, int instanceCount, int firstIndex, int vertexOffset, int firstInstance)
{
GR_CMD_BUFFER grCommandBuffer = reinterpret_cast<GR_CMD_BUFFER>(commandEncoder);
GR_GPU_MEMORY grIndexBuffer = reinterpret_cast<GR_GPU_MEMORY>(indexBuffer);
GR_INDEX_TYPE format = vertexCount < 65536 ? GR_INDEX_16 : GR_INDEX_32;
grCmdBindIndexData(grCommandBuffer, grIndexBuffer, 0, format);
grCmdDrawIndexed(grCommandBuffer, firstIndex, indexCount, vertexOffset, firstInstance, instanceCount);
}
//==============================================================================