-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathxxGraphicGlide.cpp
816 lines (725 loc) · 29.8 KB
/
xxGraphicGlide.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
//==============================================================================
// xxGraphic : Glide Source
//
// Copyright (c) 2019-2025 TAiGA
// https://github.com/metarutaiga/xxGraphic
//==============================================================================
#include "internal/xxGraphicInternalGlide.h"
#include "xxVector.h"
#include "xxGraphicGlide.h"
#if defined(xxMACOS)
#import <AppKit/AppKit.h>
#endif
static void* g_glideLibrary = nullptr;
static FxU32 g_startAddress = 0;
static uint64_t g_vertexBuffer = 0;
static uint64_t g_vertexAttribute = 0;
static v4sf g_worldMatrix[4];
static v4sf g_viewMatrix[4];
static v4sf g_projectionMatrix[4];
static v4sf g_worldViewProjectionScreenMatrix[4];
//==============================================================================
// Instance
//==============================================================================
GrProc FX_CALL grGetProcAddress(char* name)
{
#if defined(xxMACOS)
if (g_glideLibrary == nullptr)
g_glideLibrary = xxLoadLibrary("glide3x.dylib");
if (g_glideLibrary == nullptr)
return gto_grGetProcAddress(name);
#elif defined(xxWINDOWS)
if (g_glideLibrary == nullptr)
g_glideLibrary = xxLoadLibrary("glide3x.dll");
if (g_glideLibrary == nullptr)
return gto_grGetProcAddress(name);
#if defined(_M_IX86)
for (int i = 0; i <= 40; i += 4)
{
char temp[64];
snprintf(temp, 64, "_%s@%d", name, i);
GrProc proc = (GrProc)xxGetProcAddress(g_glideLibrary, temp);
if (proc == nullptr)
continue;
return proc;
}
#endif
#endif
return (GrProc)xxGetProcAddress(g_glideLibrary, name);
}
//------------------------------------------------------------------------------
uint64_t xxCreateInstanceGlide()
{
grGlideInit();
grVertexLayout(GR_PARAM_XY, xxOffsetOf(GrVertex, x), GR_PARAM_ENABLE);
grVertexLayout(GR_PARAM_Z, xxOffsetOf(GrVertex, ooz), GR_PARAM_ENABLE);
grVertexLayout(GR_PARAM_Q, xxOffsetOf(GrVertex, oow), GR_PARAM_ENABLE);
grVertexLayout(GR_PARAM_PARGB, xxOffsetOf(GrVertex, rgba), GR_PARAM_ENABLE);
grVertexLayout(GR_PARAM_ST0, xxOffsetOf(GrVertex, sow), GR_PARAM_ENABLE);
g_startAddress = 0;
g_vertexBuffer = 0;
g_vertexAttribute = 0;
xxRegisterFunction(Glide);
return reinterpret_cast<uint64_t>(grGetString(GR_VENDOR));
}
//------------------------------------------------------------------------------
void xxDestroyInstanceGlide(uint64_t instance)
{
grGlideShutdown();
xxUnregisterFunction();
}
//==============================================================================
// Device
//==============================================================================
uint64_t xxCreateDeviceGlide(uint64_t instance)
{
return reinterpret_cast<uint64_t>(grGetString(GR_HARDWARE));
}
//------------------------------------------------------------------------------
void xxDestroyDeviceGlide(uint64_t device)
{
}
//------------------------------------------------------------------------------
bool xxResetDeviceGlide(uint64_t device)
{
return true;
}
//------------------------------------------------------------------------------
bool xxTestDeviceGlide(uint64_t device)
{
return true;
}
//==============================================================================
// Swapchain
//==============================================================================
uint64_t xxCreateSwapchainGlide(uint64_t device, uint64_t renderPass, void* view, int width, int height, uint64_t oldSwapchain)
{
xxDestroySwapchainGlide(oldSwapchain);
GrContext* grContext = xxAlloc(GrContext);
if (grContext == nullptr)
return 0;
#if defined(xxWINDOWS)
grContext->context = grSstWinOpen(view, GR_RESOLUTION_NONE, GR_REFRESH_NONE, GR_COLORFORMAT_ARGB, GR_ORIGIN_LOWER_LEFT, 2, 0);
#else
grContext->context = grSstWinOpen(view, GR_RESOLUTION_NONE, GR_REFRESH_NONE, GR_COLORFORMAT_ABGR, GR_ORIGIN_LOWER_LEFT, 2, 0);
#endif
grContext->view = view;
grContext->width = width;
grContext->height = height;
grContext->scale = 1.0f;
return reinterpret_cast<uint64_t>(grContext);
}
//------------------------------------------------------------------------------
void xxDestroySwapchainGlide(uint64_t swapchain)
{
GrContext* grContext = reinterpret_cast<GrContext*>(swapchain);
if (grContext == nullptr)
return;
grSstWinClose(grContext->context);
xxFree(grContext);
}
//------------------------------------------------------------------------------
void xxPresentSwapchainGlide(uint64_t swapchain)
{
GrContext* grContext = reinterpret_cast<GrContext*>(swapchain);
grSelectContext(grContext->context);
grBufferSwap(0);
}
//==============================================================================
// Framebuffer
//==============================================================================
uint64_t xxCreateFramebufferGlide(uint64_t device, uint64_t texture)
{
return 0;
}
//------------------------------------------------------------------------------
void xxDestroyFramebufferGlide(uint64_t framebuffer)
{
}
//------------------------------------------------------------------------------
uint64_t xxGetFramebufferGlide(uint64_t device, uint64_t swapchain, float* scale)
{
GrContext* grContext = reinterpret_cast<GrContext*>(swapchain);
if (scale)
{
#if defined(xxMACOS)
grContext->scale = [(__bridge NSWindow*)grContext->view backingScaleFactor];
#endif
(*scale) = grContext->scale;
}
return reinterpret_cast<uint64_t>(grContext);
}
//==============================================================================
// Command Buffer
//==============================================================================
uint64_t xxGetCommandBufferGlide(uint64_t device, uint64_t swapchain)
{
GrContext* grContext = reinterpret_cast<GrContext*>(swapchain);
grSelectContext(grContext->context);
return reinterpret_cast<uint64_t>(grContext);
}
//------------------------------------------------------------------------------
bool xxBeginCommandBufferGlide(uint64_t commandBuffer)
{
return true;
}
//------------------------------------------------------------------------------
void xxEndCommandBufferGlide(uint64_t commandBuffer)
{
}
//------------------------------------------------------------------------------
void xxSubmitCommandBufferGlide(uint64_t commandBuffer, uint64_t swapchain)
{
}
//==============================================================================
// Render Pass
//==============================================================================
uint64_t xxCreateRenderPassGlide(uint64_t device, bool clearColor, bool clearDepth, bool clearStencil, bool storeColor, bool storeDepth, bool storeStencil)
{
uint64_t renderPass = 0;
if (clearColor)
renderPass |= GR_PARAM_RGB | GR_PARAM_A;
if (clearDepth)
renderPass |= GR_PARAM_Z;
return renderPass;
}
//------------------------------------------------------------------------------
void xxDestroyRenderPassGlide(uint64_t renderPass)
{
}
//------------------------------------------------------------------------------
uint64_t xxBeginRenderPassGlide(uint64_t commandBuffer, uint64_t framebuffer, uint64_t renderPass, int width, int height, float color[4], float depth, unsigned char stencil)
{
if (renderPass & (GR_PARAM_RGB | GR_PARAM_A | GR_PARAM_Z))
{
GrColor_t grColor = 0;
GrAlpha_t grAlpha = 0;
grColor |= (int)(color[0] * 255);
grColor |= (int)(color[1] * 255) << 8;
grColor |= (int)(color[2] * 255) << 16;
grAlpha |= (int)(color[3] * 255);
grViewport(0, 0, width, height);
grClipWindow(0, 0, width, height);
grColorMask(renderPass & GR_PARAM_RGB ? FXTRUE : FXFALSE, renderPass & GR_PARAM_A ? FXTRUE : FXFALSE);
grDepthMask(renderPass & GR_PARAM_Z ? FXTRUE : FXFALSE);
grBufferClear(grColor, grAlpha, 0xFFFF);
grColorMask(FXTRUE, FXTRUE);
grDepthMask(FXTRUE);
}
return commandBuffer;
}
//------------------------------------------------------------------------------
void xxEndRenderPassGlide(uint64_t commandEncoder, uint64_t framebuffer, uint64_t renderPass)
{
}
//==============================================================================
// Vertex Attribute
//==============================================================================
uint64_t xxCreateVertexAttributeGlide(uint64_t device, int count, int* attribute)
{
GrVertexAttribute grVertexAttribute = {};
int stride = 0;
for (int i = 0; i < count; ++i)
{
int stream = (*attribute++);
int offset = (*attribute++);
int element = (*attribute++);
int size = (*attribute++);
(void)stream;
(void)offset;
stride += size;
if (element == 'POS3' && size == sizeof(float) * 3)
grVertexAttribute.flags |= GR_PARAM_XY | GR_PARAM_Z;
if (element == 'BON3' && size == sizeof(float) * 3)
grVertexAttribute.flags |= /*GR_PARAM_BONE*/0x80;
if (element == 'BON4' && size == sizeof(char) * 4)
grVertexAttribute.flags |= /*GR_PARAM_BONE*/0x80;
if (element == 'NOR3' && size == sizeof(float) * 3)
grVertexAttribute.flags |= /*GR_PARAM_NORMAL*/0x08;
if (element == 'COL4' && size == sizeof(char) * 4)
grVertexAttribute.flags |= GR_PARAM_PARGB;
if (element == 'TEX2' && size == sizeof(float) * 2)
grVertexAttribute.flags += GR_PARAM_ST0;
}
grVertexAttribute.stride = stride;
return static_cast<uint64_t>(grVertexAttribute.value);
}
//------------------------------------------------------------------------------
void xxDestroyVertexAttributeGlide(uint64_t vertexAttribute)
{
}
//==============================================================================
// Buffer
//==============================================================================
uint64_t xxCreateConstantBufferGlide(uint64_t device, int size)
{
char* grBuffer = xxAlloc(char, size);
return reinterpret_cast<uint64_t>(grBuffer);
}
//------------------------------------------------------------------------------
uint64_t xxCreateIndexBufferGlide(uint64_t device, int size, int bits)
{
char* grBuffer = xxAlloc(char, size);
return reinterpret_cast<uint64_t>(grBuffer);
}
//------------------------------------------------------------------------------
uint64_t xxCreateVertexBufferGlide(uint64_t device, int size, uint64_t vertexAttribute)
{
char* grBuffer = xxAlloc(char, size);
return reinterpret_cast<uint64_t>(grBuffer);
}
//------------------------------------------------------------------------------
uint64_t xxCreateStorageBufferGlide(uint64_t device, int size)
{
char* grBuffer = xxAlloc(char, size);
return reinterpret_cast<uint64_t>(grBuffer);
}
//------------------------------------------------------------------------------
uint64_t xxCreateInstanceBufferGlide(uint64_t device, int size)
{
char* grBuffer = xxAlloc(char, size);
return reinterpret_cast<uint64_t>(grBuffer);
}
//------------------------------------------------------------------------------
void xxDestroyBufferGlide(uint64_t device, uint64_t buffer)
{
char* grBuffer = reinterpret_cast<char*>(buffer);
xxFree(grBuffer);
}
//------------------------------------------------------------------------------
void* xxMapBufferGlide(uint64_t device, uint64_t buffer)
{
char* grBuffer = reinterpret_cast<char*>(buffer);
return grBuffer;
}
//------------------------------------------------------------------------------
void xxUnmapBufferGlide(uint64_t device, uint64_t buffer)
{
}
//==============================================================================
// Texture
//==============================================================================
uint64_t xxCreateTextureGlide(uint64_t device, uint64_t format, int width, int height, int depth, int mipmap, int array, void const* external)
{
GrTexture* info = xxAlloc(GrTexture);
if (info == nullptr)
return 0;
if (g_startAddress == 0)
g_startAddress = grTexMinAddress(GR_TMU0);
int widthLog2 = 31 - xxCountLeadingZeros(width);
int heightLog2 = 31 - xxCountLeadingZeros(height);
int largeLodLog2 = widthLog2 > heightLog2 ? widthLog2 : heightLog2;
GrAspectRatio_t aspectRatioLog2 = widthLog2 - heightLog2;
info->smallLodLog2 = largeLodLog2 >> (mipmap - 1);
info->largeLodLog2 = largeLodLog2;
info->aspectRatioLog2 = aspectRatioLog2;
info->format = GR_TEXFMT_ARGB_8888;
info->data = xxAlloc(uint32_t, width * height);
info->startAddress = g_startAddress;
info->width = width;
info->height = height;
info->stride = width * sizeof(uint32_t);
g_startAddress += grTexCalcMemRequired(info->smallLodLog2, info->largeLodLog2, info->aspectRatioLog2, info->format);
return reinterpret_cast<uint64_t>(info);
}
//------------------------------------------------------------------------------
void xxDestroyTextureGlide(uint64_t texture)
{
GrTexture* info = reinterpret_cast<GrTexture*>(texture);
if (info == nullptr)
return;
xxFree(info->data);
xxFree(info);
}
//------------------------------------------------------------------------------
void* xxMapTextureGlide(uint64_t device, uint64_t texture, int* stride, int level, int array)
{
GrTexture* info = reinterpret_cast<GrTexture*>(texture);
if (info == nullptr)
return nullptr;
if (stride)
{
(*stride) = info->stride;
}
return info->data;
}
//------------------------------------------------------------------------------
void xxUnmapTextureGlide(uint64_t device, uint64_t texture, int level, int array)
{
GrTexture* info = reinterpret_cast<GrTexture*>(texture);
if (info == nullptr)
return;
grTexDownloadMipMap(GR_TMU0, info->startAddress, GR_MIPMAPLEVELMASK_BOTH, info);
}
//==============================================================================
// Sampler
//==============================================================================
uint64_t xxCreateSamplerGlide(uint64_t device, bool clampU, bool clampV, bool clampW, bool linearMag, bool linearMin, bool linearMip, int anisotropy)
{
GrSampler grSampler = {};
grSampler.addressU = clampU ? GR_TEXTURECLAMP_CLAMP : GR_TEXTURECLAMP_WRAP;
grSampler.addressV = clampV ? GR_TEXTURECLAMP_CLAMP : GR_TEXTURECLAMP_WRAP;
grSampler.magFilter = linearMag ? GR_TEXTUREFILTER_BILINEAR : GR_TEXTUREFILTER_POINT_SAMPLED;
grSampler.minFilter = linearMin ? GR_TEXTUREFILTER_BILINEAR : GR_TEXTUREFILTER_POINT_SAMPLED;
grSampler.mipFilter = linearMip ? GR_MIPMAP_NEAREST_DITHER : GR_MIPMAP_NEAREST;
return static_cast<uint64_t>(grSampler.value);
}
//------------------------------------------------------------------------------
void xxDestroySamplerGlide(uint64_t sampler)
{
}
//==============================================================================
// Shader
//==============================================================================
uint64_t xxCreateMeshShaderGlide(uint64_t device, char const* shader)
{
return 0;
}
//------------------------------------------------------------------------------
uint64_t xxCreateVertexShaderGlide(uint64_t device, char const* shader, uint64_t vertexAttribute)
{
return 0;
}
//------------------------------------------------------------------------------
uint64_t xxCreateFragmentShaderGlide(uint64_t device, char const* shader)
{
return 0;
}
//------------------------------------------------------------------------------
void xxDestroyShaderGlide(uint64_t device, uint64_t shader)
{
}
//==============================================================================
// Pipeline
//==============================================================================
uint64_t xxCreateBlendStateGlide(uint64_t device, char const* sourceColor, char const* operationColor, char const* destinationColor, char const* sourceAlpha, char const* operationAlpha, char const* destinationAlpha)
{
GrPipeline grPipeline = {};
grPipeline.blendSourceColor = grBlendFactor(sourceColor);
grPipeline.blendDestinationColor = grBlendFactor(destinationColor);
grPipeline.blendSourceAlpha = grBlendFactor(sourceAlpha);
grPipeline.blendDestinationAlpha = grBlendFactor(destinationAlpha);
return static_cast<uint64_t>(grPipeline.value);
}
//------------------------------------------------------------------------------
uint64_t xxCreateDepthStencilStateGlide(uint64_t device, char const* depthTest, bool depthWrite)
{
GrPipeline grPipeline = {};
grPipeline.depthTest = grCompareOp(depthTest);
grPipeline.depthWrite = depthWrite;
return static_cast<uint64_t>(grPipeline.value);
}
//------------------------------------------------------------------------------
uint64_t xxCreateRasterizerStateGlide(uint64_t device, bool cull, bool scissor)
{
GrPipeline grPipeline = {};
grPipeline.cull = cull;
grPipeline.scissor = scissor;
return static_cast<uint64_t>(grPipeline.value);
}
//------------------------------------------------------------------------------
uint64_t xxCreatePipelineGlide(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)
{
GrPipeline grBlendState = { blendState };
GrPipeline grDepthStencilState = { depthStencilState };
GrPipeline grRasterizerState = { rasterizerState };
GrPipeline grPipeline = {};
grPipeline.blendSourceColor = grBlendState.blendSourceColor;
grPipeline.blendDestinationColor = grBlendState.blendDestinationColor;
grPipeline.depthTest = grDepthStencilState.depthTest;
grPipeline.depthWrite = grDepthStencilState.depthWrite;
grPipeline.cull = grRasterizerState.cull;
grPipeline.scissor = grRasterizerState.scissor;
return static_cast<uint64_t>(grPipeline.value);
}
//------------------------------------------------------------------------------
void xxDestroyBlendStateGlide(uint64_t blendState)
{
}
//------------------------------------------------------------------------------
void xxDestroyDepthStencilStateGlide(uint64_t depthStencilState)
{
}
//------------------------------------------------------------------------------
void xxDestroyRasterizerStateGlide(uint64_t rasterizerState)
{
}
//------------------------------------------------------------------------------
void xxDestroyPipelineGlide(uint64_t pipeline)
{
}
//==============================================================================
// Command
//==============================================================================
void xxSetViewportGlide(uint64_t commandEncoder, int x, int y, int width, int height, float minZ, float maxZ)
{
grViewport(x, y, width, height);
grDepthRange(minZ, maxZ);
}
//------------------------------------------------------------------------------
void xxSetScissorGlide(uint64_t commandEncoder, int x, int y, int width, int height)
{
grClipWindow(x, y, x + width, y + height);
}
//------------------------------------------------------------------------------
void xxSetPipelineGlide(uint64_t commandEncoder, uint64_t pipeline)
{
GrPipeline grPipeline = { pipeline };
grAlphaBlendFunction(grPipeline.blendSourceColor, grPipeline.blendDestinationColor, grPipeline.blendSourceAlpha, grPipeline.blendDestinationAlpha);
grDepthBufferFunction(grPipeline.depthTest);
grDepthMask(grPipeline.depthWrite ? FXTRUE : FXFALSE);
grCullMode(grPipeline.cull ? GR_CULL_NEGATIVE : GR_CULL_DISABLE);
}
//------------------------------------------------------------------------------
void xxSetMeshBuffersGlide(uint64_t commandEncoder, int count, const uint64_t* buffers)
{
}
//------------------------------------------------------------------------------
void xxSetVertexBuffersGlide(uint64_t commandEncoder, int count, const uint64_t* buffers, uint64_t vertexAttribute)
{
g_vertexBuffer = buffers[0];
g_vertexAttribute = vertexAttribute;
}
//------------------------------------------------------------------------------
void xxSetMeshTexturesGlide(uint64_t commandEncoder, int count, const uint64_t* textures)
{
}
//------------------------------------------------------------------------------
void xxSetVertexTexturesGlide(uint64_t commandEncoder, int count, const uint64_t* textures)
{
}
//------------------------------------------------------------------------------
void xxSetFragmentTexturesGlide(uint64_t commandEncoder, int count, const uint64_t* textures)
{
for (int i = 0; i < count; ++i)
{
GrTexture* info = reinterpret_cast<GrTexture*>(textures[i]);
grTexSource(GR_TMU0 + i, info->startAddress, GR_MIPMAPLEVELMASK_BOTH, info);
}
}
//------------------------------------------------------------------------------
void xxSetMeshSamplersGlide(uint64_t commandEncoder, int count, const uint64_t* samplers)
{
}
//------------------------------------------------------------------------------
void xxSetVertexSamplersGlide(uint64_t commandEncoder, int count, const uint64_t* samplers)
{
}
//------------------------------------------------------------------------------
void xxSetFragmentSamplersGlide(uint64_t commandEncoder, int count, const uint64_t* samplers)
{
for (int i = 0; i < count; ++i)
{
GrSampler grSampler = { samplers[i] };
grTexClampMode(GR_TMU0 + i, grSampler.addressU, grSampler.addressV);
grTexFilterMode(GR_TMU0 + i, grSampler.minFilter, grSampler.magFilter);
grTexMipMapMode(GR_TMU0 + i, grSampler.mipFilter, grSampler.mipFilter == GR_MIPMAP_NEAREST_DITHER ? FXTRUE : FXFALSE);
}
}
//------------------------------------------------------------------------------
void xxSetMeshConstantBufferGlide(uint64_t commandEncoder, uint64_t buffer, int size)
{
}
//------------------------------------------------------------------------------
void xxSetVertexConstantBufferGlide(uint64_t commandEncoder, uint64_t buffer, int size)
{
GrContext* grContext = reinterpret_cast<GrContext*>(commandEncoder);
v4sf* grBuffer = reinterpret_cast<v4sf*>(buffer);
if (size >= sizeof(g_worldMatrix))
{
size -= sizeof(g_worldMatrix);
g_worldMatrix[0] = (*grBuffer++);
g_worldMatrix[1] = (*grBuffer++);
g_worldMatrix[2] = (*grBuffer++);
g_worldMatrix[3] = (*grBuffer++);
}
if (size >= sizeof(g_viewMatrix))
{
size -= sizeof(g_viewMatrix);
g_viewMatrix[0] = (*grBuffer++);
g_viewMatrix[1] = (*grBuffer++);
g_viewMatrix[2] = (*grBuffer++);
g_viewMatrix[3] = (*grBuffer++);
}
if (size >= sizeof(g_projectionMatrix))
{
size -= sizeof(g_projectionMatrix);
g_projectionMatrix[0] = (*grBuffer++);
g_projectionMatrix[1] = (*grBuffer++);
g_projectionMatrix[2] = (*grBuffer++);
g_projectionMatrix[3] = (*grBuffer++);
}
float halfWidth = grContext->width * grContext->scale * 0.5f;
float halfHeight = grContext->height * grContext->scale * 0.5f;
v4sf screenMatrix[4] =
{
{ halfWidth, 0, 0, 0 },
{ 0, halfHeight, 0, 0 },
{ 0, 0, 1, 0 },
{ halfWidth, halfHeight, 0, 1 },
};
g_worldViewProjectionScreenMatrix[0] = __builtin_multiplyvector(screenMatrix, __builtin_multiplyvector(g_projectionMatrix, __builtin_multiplyvector(g_viewMatrix, g_worldMatrix[0])));
g_worldViewProjectionScreenMatrix[1] = __builtin_multiplyvector(screenMatrix, __builtin_multiplyvector(g_projectionMatrix, __builtin_multiplyvector(g_viewMatrix, g_worldMatrix[1])));
g_worldViewProjectionScreenMatrix[2] = __builtin_multiplyvector(screenMatrix, __builtin_multiplyvector(g_projectionMatrix, __builtin_multiplyvector(g_viewMatrix, g_worldMatrix[2])));
g_worldViewProjectionScreenMatrix[3] = __builtin_multiplyvector(screenMatrix, __builtin_multiplyvector(g_projectionMatrix, __builtin_multiplyvector(g_viewMatrix, g_worldMatrix[3])));
}
//------------------------------------------------------------------------------
void xxSetFragmentConstantBufferGlide(uint64_t commandEncoder, uint64_t buffer, int size)
{
}
//------------------------------------------------------------------------------
void xxDrawGlide(uint64_t commandEncoder, int vertexCount, int instanceCount, int firstVertex, int firstInstance)
{
GrVertexAttribute grVertexAttribute = { g_vertexAttribute };
for (int i = 0; i < vertexCount; i += 3)
{
float* v0 = reinterpret_cast<float*>(g_vertexBuffer + (firstVertex + i + 0) * grVertexAttribute.stride);
float* v1 = reinterpret_cast<float*>(g_vertexBuffer + (firstVertex + i + 1) * grVertexAttribute.stride);
float* v2 = reinterpret_cast<float*>(g_vertexBuffer + (firstVertex + i + 2) * grVertexAttribute.stride);
GrVertex t0;
GrVertex t1;
GrVertex t2;
if (grVertexAttribute.flags & GR_PARAM_XY)
{
v4sf p0 = __builtin_multiplyvector(g_worldViewProjectionScreenMatrix, v4sf{ v0[0], v0[1], v0[2], 1.0f });
v4sf p1 = __builtin_multiplyvector(g_worldViewProjectionScreenMatrix, v4sf{ v1[0], v1[1], v1[2], 1.0f });
v4sf p2 = __builtin_multiplyvector(g_worldViewProjectionScreenMatrix, v4sf{ v2[0], v2[1], v2[2], 1.0f });
t0.oow = 1.0f / p0[3];
t1.oow = 1.0f / p1[3];
t2.oow = 1.0f / p2[3];
t0.ooz = 65535.0f / p0[2];
t1.ooz = 65535.0f / p1[2];
t2.ooz = 65535.0f / p2[2];
t0.y = p0[1] * t0.oow;
t1.y = p1[1] * t1.oow;
t2.y = p2[1] * t2.oow;
t0.x = p0[0] * t0.oow;
t1.x = p1[0] * t1.oow;
t2.x = p2[0] * t2.oow;
v0 += 3;
v1 += 3;
v2 += 3;
}
if (grVertexAttribute.flags & /*GR_PARAM_BONE*/0x80)
{
v0 += 4;
v1 += 4;
v2 += 4;
}
if (grVertexAttribute.flags & /*GR_PARAM_NORMAL*/0x08)
{
v0 += 3;
v1 += 3;
v2 += 3;
}
if (grVertexAttribute.flags & GR_PARAM_PARGB)
{
t0.rgba = (int&)v0[0];
t1.rgba = (int&)v1[0];
t2.rgba = (int&)v2[0];
v0++;
v1++;
v2++;
}
if (grVertexAttribute.flags & GR_PARAM_ST0)
{
t0.sow = v0[0] * t0.oow;
t1.sow = v1[0] * t1.oow;
t2.sow = v2[0] * t2.oow;
t0.tow = v0[1] * t0.oow;
t1.tow = v1[1] * t1.oow;
t2.tow = v2[1] * t2.oow;
v0 += 2;
v1 += 2;
v2 += 2;
}
grDrawTriangle(&t0, &t1, &t2);
}
}
//------------------------------------------------------------------------------
void xxDrawMeshedGlide(uint64_t commandEncoder, int x, int y, int z)
{
}
//------------------------------------------------------------------------------
void xxDrawIndexedGlide(uint64_t commandEncoder, uint64_t indexBuffer, int indexCount, int vertexCount, int instanceCount, int firstIndex, int vertexOffset, int firstInstance)
{
GrVertexAttribute grVertexAttribute = { g_vertexAttribute };
uint16_t* grIndexBuffer16 = nullptr;
uint32_t* grIndexBuffer32 = nullptr;
if (vertexCount < 65536)
{
grIndexBuffer16 = reinterpret_cast<uint16_t*>(indexBuffer) + firstIndex;
}
else
{
grIndexBuffer32 = reinterpret_cast<uint32_t*>(indexBuffer) + firstIndex;
}
for (int i = 0; i < indexCount; i += 3)
{
int i0 = grIndexBuffer16 ? grIndexBuffer16[i + 0] : grIndexBuffer32[i + 0];
int i1 = grIndexBuffer16 ? grIndexBuffer16[i + 1] : grIndexBuffer32[i + 1];
int i2 = grIndexBuffer16 ? grIndexBuffer16[i + 2] : grIndexBuffer32[i + 2];
float* v0 = reinterpret_cast<float*>(g_vertexBuffer + (vertexOffset + i0) * grVertexAttribute.stride);
float* v1 = reinterpret_cast<float*>(g_vertexBuffer + (vertexOffset + i1) * grVertexAttribute.stride);
float* v2 = reinterpret_cast<float*>(g_vertexBuffer + (vertexOffset + i2) * grVertexAttribute.stride);
GrVertex t0;
GrVertex t1;
GrVertex t2;
if (grVertexAttribute.flags & GR_PARAM_XY)
{
v4sf p0 = __builtin_multiplyvector(g_worldViewProjectionScreenMatrix, v4sf{ v0[0], v0[1], v0[2], 1.0f });
v4sf p1 = __builtin_multiplyvector(g_worldViewProjectionScreenMatrix, v4sf{ v1[0], v1[1], v1[2], 1.0f });
v4sf p2 = __builtin_multiplyvector(g_worldViewProjectionScreenMatrix, v4sf{ v2[0], v2[1], v2[2], 1.0f });
t0.oow = 1.0f / p0[3];
t1.oow = 1.0f / p1[3];
t2.oow = 1.0f / p2[3];
t0.ooz = 65535.0f / p0[2];
t1.ooz = 65535.0f / p1[2];
t2.ooz = 65535.0f / p2[2];
t0.y = p0[1] * t0.oow;
t1.y = p1[1] * t1.oow;
t2.y = p2[1] * t2.oow;
t0.x = p0[0] * t0.oow;
t1.x = p1[0] * t1.oow;
t2.x = p2[0] * t2.oow;
v0 += 3;
v1 += 3;
v2 += 3;
}
if (grVertexAttribute.flags & /*GR_PARAM_BONE*/0x80)
{
v0 += 4;
v1 += 4;
v2 += 4;
}
if (grVertexAttribute.flags & /*GR_PARAM_NORMAL*/0x08)
{
v0 += 3;
v1 += 3;
v2 += 3;
}
if (grVertexAttribute.flags & GR_PARAM_PARGB)
{
t0.rgba = (int&)v0[0];
t1.rgba = (int&)v1[0];
t2.rgba = (int&)v2[0];
v0++;
v1++;
v2++;
}
if (grVertexAttribute.flags & GR_PARAM_ST0)
{
t0.sow = v0[0] * t0.oow;
t1.sow = v1[0] * t1.oow;
t2.sow = v2[0] * t2.oow;
t0.tow = v0[1] * t0.oow;
t1.tow = v1[1] * t1.oow;
t2.tow = v2[1] * t2.oow;
v0 += 2;
v1 += 2;
v2 += 2;
}
grDrawTriangle(&t0, &t1, &t2);
}
}
//==============================================================================