forked from libretro/RetroArch
-
Notifications
You must be signed in to change notification settings - Fork 2
/
psp2_video.c
1411 lines (1196 loc) · 42.5 KB
/
psp2_video.c
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
991
992
993
994
995
996
997
998
999
1000
/* RetroArch - A frontend for libretro.
* Copyright (C) 2010-2014 - Hans-Kristian Arntzen
* Copyright (C) 2011-2014 - Daniel De Matteis
*
* RetroArch is free software: you can redistribute it and/or modify it under the terms
* of the GNU General Public License as published by the Free Software Found-
* ation, either version 3 of the License, or (at your option) any later version.
*
* RetroArch is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with RetroArch.
* If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdlib.h>
#include <assert.h>
#include <sceconst.h>
#include <display.h>
#include <ctrl.h>
#include <gxm.h>
#include <gxt.h>
#include <math.h>
#include <kernel.h>
#include <libdbgfont.h>
#include <stdio.h>
#include <vectormath.h>
#include <gxm.h>
#include <kernel.h>
// Display dimensions
#define DISPLAY_WIDTH 960
#define DISPLAY_HEIGHT 544
#define DISPLAY_STRIDE_IN_PIXELS 1024
#define DISPLAY_PIXEL_FORMAT SCE_DISPLAY_PIXELFORMAT_A8B8G8R8
#define DISPLAY_BUFFER_COUNT 3
#define DISPLAY_MAX_PENDING_SWAPS 2
// Supported flip modes
enum FlipMode {
FLIP_MODE_HSYNC, ///< Flip on next HSYNC
FLIP_MODE_VSYNC, ///< Flip on next VSYNC
FLIP_MODE_VSYNC_2 ///< Flip on next VSYNC, display for 2 VSYNCs minimum
};
#define ALIGN(x, a) (((x) + ((a) - 1)) & ~((a) - 1))
#define MAX(a, b) (((a) > (b)) ? (a) : (b))
// Data structure to pass through the display queue
typedef struct DisplayData
{
void *address; ///< Framebuffer address
uint32_t width; ///< Framebuffer width
uint32_t height; ///< Framebuffer height
uint32_t strideInPixels; ///< Framebuffer stride in pixels
uint32_t flipMode; ///< From #FlipMode
} DisplayData;
typedef struct psp2_video
{
SceGxmRenderTarget *g_mainRenderTarget;
SceGxmContext *g_context;
const SceGxmProgramParameter *g_clearColorParam;
SceGxmShaderPatcher *g_shaderPatcher;
SceGxmColorSurface g_displaySurface[DISPLAY_BUFFER_COUNT];
SceGxmSyncObject *g_displayBufferSync[DISPLAY_BUFFER_COUNT];
SceUID g_displayBufferUid[DISPLAY_BUFFER_COUNT];
void *g_displayBufferData[DISPLAY_BUFFER_COUNT];
void *g_initializeHostMem;
bool smooth;
int rotation;
bool vsync;
bool rgb32;
unsigned width, height;
} psp2_video_t;
uint32_t g_displayFrontBufferIndex = DISPLAY_BUFFER_COUNT - 1;
// initialization parameters
SceUID g_initializeDriverUid = 0;
SceUID g_initializeParameterBufferUid = 0;
// libgxm context
void *g_contextHostMem = NULL;
SceUID g_vdmRingBufferUid = 0;
SceUID g_vertexRingBufferUid = 0;
SceUID g_fragmentRingBufferUid = 0;
SceUID g_fragmentUsseRingBufferUid = 0;
// libgxm shader patcher
SceUID g_patcherBufferUid = 0;
SceUID g_patcherCombinedUsseUid = 0;
// libgxm display queue
uint32_t g_displayBackBufferIndex = 0;
// Depth buffer for display surface
SceUID g_mainDepthBufferUid = 0;
SceGxmDepthStencilSurface g_mainDepthSurface;
static void *patcherHostAlloc(void *userData, uint32_t size)
{
(void)userData;
return malloc(size);
}
// Callback function to allocate memory for the shader patcher
static void patcherHostFree(void *userData, void *mem)
{
(void)userData;
free(mem);
}
static void displayCallback(const void *callbackData)
{
int err = SCE_OK;
(void)err;
// Cast the parameters back
const DisplayData *displayData = (const DisplayData *)callbackData;
// Check this buffer has been displayed for the necessary number of VSYNCs
// (Avoids queuing a flip before the second VSYNC has happened)
if (displayData->flipMode == FLIP_MODE_VSYNC_2)
err = sceDisplayWaitSetFrameBufMulti(2);
// Swap to the new buffer
SceDisplayFrameBuf framebuf;
memset(&framebuf, 0x00, sizeof(SceDisplayFrameBuf));
framebuf.size = sizeof(SceDisplayFrameBuf);
framebuf.base = displayData->address;
framebuf.pitch = displayData->strideInPixels;
framebuf.pixelformat = DISPLAY_PIXEL_FORMAT;
framebuf.width = displayData->width;
framebuf.height = displayData->height;
err = sceDisplaySetFrameBuf(&framebuf,
(displayData->flipMode == FLIP_MODE_HSYNC)
? SCE_DISPLAY_UPDATETIMING_NEXTHSYNC
: SCE_DISPLAY_UPDATETIMING_NEXTVSYNC);
assert(err == SCE_OK);
// Block this callback until the swap has occurred and the old buffer
// is no longer displayed
if (displayData->flipMode != FLIP_MODE_HSYNC)
{
err = sceDisplayWaitSetFrameBuf();
assert(err == SCE_OK);
}
}
static void createGxmShaderPatcher(void *data)
{
psp2_video_t *psp = (psp2_video_t*)data;
int err = SCE_OK;
(void)err;
// set buffer sizes for this sample
const uint32_t patcherBufferSize = 64*1024;
const uint32_t patcherCombinedUsseSize = 64*1024;
// allocate memory for buffers and USSE code
void *patcherBuffer = gmmAlloc(
SCE_KERNEL_MEMBLOCK_TYPE_USER_RWDATA_UNCACHE,
patcherBufferSize,
4,
SCE_GXM_MEMORY_ATTRIB_READ | SCE_GXM_MEMORY_ATTRIB_WRITE,
&g_patcherBufferUid);
uint32_t patcherVertexUsseOffset;
uint32_t patcherFragmentUsseOffset;
void *patcherCombinedUsse = combinedUsseAlloc(
patcherCombinedUsseSize,
&g_patcherCombinedUsseUid,
&patcherVertexUsseOffset,
&patcherFragmentUsseOffset);
// create a shader patcher
SceGxmShaderPatcherParams patcherParams;
memset(&patcherParams, 0, sizeof(SceGxmShaderPatcherParams));
patcherParams.userData = NULL;
patcherParams.hostAllocCallback = &patcherHostAlloc;
patcherParams.hostFreeCallback = &patcherHostFree;
patcherParams.bufferAllocCallback = NULL;
patcherParams.bufferFreeCallback = NULL;
patcherParams.bufferMem = patcherBuffer;
patcherParams.bufferMemSize = patcherBufferSize;
patcherParams.vertexUsseAllocCallback = NULL;
patcherParams.vertexUsseFreeCallback = NULL;
patcherParams.vertexUsseMem = patcherCombinedUsse;
patcherParams.vertexUsseMemSize = patcherCombinedUsseSize;
patcherParams.vertexUsseOffset = patcherVertexUsseOffset;
patcherParams.fragmentUsseAllocCallback = NULL;
patcherParams.fragmentUsseFreeCallback = NULL;
patcherParams.fragmentUsseMem = patcherCombinedUsse;
patcherParams.fragmentUsseMemSize = patcherCombinedUsseSize;
patcherParams.fragmentUsseOffset = patcherFragmentUsseOffset;
err = sceGxmShaderPatcherCreate(&patcherParams, &psp->g_shaderPatcher);
assert(err == SCE_OK);
}
// Queue a display swap and cycle our buffers
static int cycleDisplayBuffers(void *data, FlipMode flipMode, uint32_t width, uint32_t height, uint32_t strideInPixels)
{
psp2_video_t *psp = (psp2_video_t*)data;
int err = SCE_OK;
(void)err;
// queue the display swap for this frame
DisplayData displayData;
displayData.address = psp->g_displayBufferData[g_displayBackBufferIndex];
displayData.width = width;
displayData.height = height;
displayData.strideInPixels = strideInPixels;
displayData.flipMode = flipMode;
err = sceGxmDisplayQueueAddEntry(
psp->g_displayBufferSync[psp->g_displayFrontBufferIndex], // front buffer is OLD buffer
psp->g_displayBufferSync[g_displayBackBufferIndex], // back buffer is NEW buffer
&displayData);
assert(err == SCE_OK);
// update buffer indices
psp->g_displayFrontBufferIndex = g_displayBackBufferIndex;
g_displayBackBufferIndex = (g_displayBackBufferIndex + 1) % DISPLAY_BUFFER_COUNT;
// done
return err;
}
static SceGxmRenderTarget *createRenderTarget(uint32_t width, uint32_t height, SceGxmMultisampleMode msaaMode)
{
int err = SCE_OK;
(void)err;
// set up parameters
SceGxmRenderTargetParams params;
memset(¶ms, 0, sizeof(SceGxmRenderTargetParams));
params.flags = 0;
params.width = width;
params.height = height;
params.scenesPerFrame = 1;
params.multisampleMode = msaaMode;
params.multisampleLocations = 0;
params.hostMem = NULL;
params.hostMemSize = 0;
params.driverMemBlock = -1;
// compute sizes
uint32_t hostMemSize, driverMemSize;
err = sceGxmGetRenderTargetMemSizes(¶ms, &hostMemSize, &driverMemSize);
assert(err == SCE_OK);
// allocate host memory
params.hostMem = malloc(hostMemSize);
params.hostMemSize = hostMemSize;
// allocate driver memory
params.driverMemBlock = sceKernelAllocMemBlock(
"SampleRT",
SCE_KERNEL_MEMBLOCK_TYPE_USER_RWDATA_UNCACHE,
driverMemSize,
NULL);
assert(params.driverMemBlock >= SCE_OK);
// create the render target
SceGxmRenderTarget *renderTarget;
err = sceGxmCreateRenderTarget(¶ms, &renderTarget);
assert(err == SCE_OK);
return renderTarget;
}
static void destroyRenderTarget(void *data, SceGxmRenderTarget *renderTarget)
{
psp2_video_t *psp = (psp2_video_t*)data;
int err = SCE_OK;
(void)err;
// grab the host memory and driver memblock
void *hostMem;
err = sceGxmRenderTargetGetHostMem(renderTarget, &hostMem);
assert(err == SCE_OK);
SceUID driverMemBlock;
err = sceGxmRenderTargetGetDriverMemBlock(renderTarget, &driverMemBlock);
assert(err == SCE_OK);
// destroy the render target
err = sceGxmDestroyRenderTarget(renderTarget);
assert(err == SCE_OK);
// free memory
sceKernelFreeMemBlock(driverMemBlock);
free(hostMem);
}
static void *gmmAlloc(SceKernelMemBlockType type, uint32_t size, uint32_t alignment, uint32_t attribs, SceUID *uid)
{
int err = SCE_OK;
(void)err;
// page align the size
if (type == SCE_KERNEL_MEMBLOCK_TYPE_USER_CDRAM_RWDATA)
{
// CDRAM memblocks must be 256KiB aligned
assert(alignment <= 256*1024);
size = ALIGN(size, 256*1024);
}
else
{
// LPDDR memblocks must be 4KiB aligned
assert(alignment <= 4*1024);
size = ALIGN(size, 4*1024);
}
(void)alignment;
// allocate some memory
*uid = sceKernelAllocMemBlock("common", type, size, NULL);
assert(*uid >= SCE_OK);
// grab the base address
void *mem = NULL;
err = sceKernelGetMemBlockBase(*uid, &mem);
assert(err == SCE_OK);
// map for the GPU
err = sceGxmMapMemory(mem, size, attribs);
assert(err == SCE_OK);
// done
return mem;
}
static void gmmFree(SceUID uid)
{
int err = SCE_OK;
(void)err;
// grab the base address
void *mem = NULL;
err = sceKernelGetMemBlockBase(uid, &mem);
assert(err == SCE_OK);
// unmap memory
err = sceGxmUnmapMemory(mem);
assert(err == SCE_OK);
// free the memory block
err = sceKernelFreeMemBlock(uid);
assert(err == SCE_OK);
}
static void *vertexUsseAlloc(uint32_t size, SceUID *uid, uint32_t *usseOffset)
{
int err = SCE_OK;
(void)err;
// align to memblock alignment for LPDDR
size = ALIGN(size, 4096);
// allocate some memory
*uid = sceKernelAllocMemBlock("basic", SCE_KERNEL_MEMBLOCK_TYPE_USER_RWDATA_UNCACHE, size, NULL);
assert(*uid >= SCE_OK);
// grab the base address
void *mem = NULL;
err = sceKernelGetMemBlockBase(*uid, &mem);
assert(err == SCE_OK);
// map as vertex USSE code for the GPU
err = sceGxmMapVertexUsseMemory(mem, size, usseOffset);
assert(err == SCE_OK);
// done
return mem;
}
static void vertexUsseFree(SceUID uid)
{
int err = SCE_OK;
(void)err;
// grab the base address
void *mem = NULL;
err = sceKernelGetMemBlockBase(uid, &mem);
assert(err == SCE_OK);
// unmap memory
err = sceGxmUnmapVertexUsseMemory(mem);
assert(err == SCE_OK);
// free the memory block
err = sceKernelFreeMemBlock(uid);
assert(err == SCE_OK);
}
static void *fragmentUsseAlloc(uint32_t size, SceUID *uid, uint32_t *usseOffset)
{
int err = SCE_OK;
(void)err;
// align to memblock alignment for LPDDR
size = ALIGN(size, 4096);
// allocate some memory
*uid = sceKernelAllocMemBlock("basic", SCE_KERNEL_MEMBLOCK_TYPE_USER_RWDATA_UNCACHE, size, NULL);
assert(*uid >= SCE_OK);
// grab the base address
void *mem = NULL;
err = sceKernelGetMemBlockBase(*uid, &mem);
assert(err == SCE_OK);
// map as fragment USSE code for the GPU
err = sceGxmMapFragmentUsseMemory(mem, size, usseOffset);
assert(err == SCE_OK);
// done
return mem;
}
static void fragmentUsseFree(SceUID uid)
{
int err = SCE_OK;
(void)err;
// grab the base address
void *mem = NULL;
err = sceKernelGetMemBlockBase(uid, &mem);
assert(err == SCE_OK);
// unmap memory
err = sceGxmUnmapFragmentUsseMemory(mem);
assert(err == SCE_OK);
// free the memory block
err = sceKernelFreeMemBlock(uid);
assert(err == SCE_OK);
}
static void *combinedUsseAlloc(uint32_t size, SceUID *uid, uint32_t *vertexUsseOffset, uint32_t *fragmentUsseOffset)
{
int err = SCE_OK;
(void)err;
// align to memblock alignment for LPDDR
size = ALIGN(size, 4096);
// allocate some memory
*uid = sceKernelAllocMemBlock("basic", SCE_KERNEL_MEMBLOCK_TYPE_USER_RWDATA_UNCACHE, size, NULL);
assert(*uid >= SCE_OK);
// grab the base address
void *mem = NULL;
err = sceKernelGetMemBlockBase(*uid, &mem);
assert(err == SCE_OK);
// map as both vertex and fragment USSE for code the GPU
err = sceGxmMapVertexUsseMemory(mem, size, vertexUsseOffset);
assert(err == SCE_OK);
err = sceGxmMapFragmentUsseMemory(mem, size, fragmentUsseOffset);
assert(err == SCE_OK);
// done
return mem;
}
void combinedUsseFree(SceUID uid)
{
int err = SCE_OK;
(void)err;
// grab the base address
void *mem = NULL;
err = sceKernelGetMemBlockBase(uid, &mem);
assert(err == SCE_OK);
// unmap memory
err = sceGxmUnmapFragmentUsseMemory(mem);
assert(err == SCE_OK);
err = sceGxmUnmapVertexUsseMemory(mem);
assert(err == SCE_OK);
// free the memory block
err = sceKernelFreeMemBlock(uid);
assert(err == SCE_OK);
}
using namespace sce::Vectormath::Scalar::Aos;
// Embedded GXM shader programs
extern const SceGxmProgram _binary_clear_v_gxp_start;
extern const SceGxmProgram _binary_clear_f_gxp_start;;
extern const SceGxmProgram _binary_cube_v_gxp_start;
extern const SceGxmProgram _binary_cube_f_gxp_start;
extern const uint8_t _binary_test_gxt_start[];
// Data structure for clear geometry
typedef struct ClearVertex {
float x;
float y;
} ClearVertex;
// Data structure for basic geometry
typedef struct BasicVertex {
float x;
float y;
float z;
uint32_t color;
uint16_t u;
uint16_t v;
} BasicVertex;
// clear geometry data
SceGxmShaderPatcherId g_clearVertexProgramId;
SceGxmShaderPatcherId g_clearFragmentProgramId;
SceGxmVertexProgram *g_clearVertexProgram = NULL;
SceGxmFragmentProgram *g_clearFragmentProgram = NULL;
SceUID g_clearVerticesUid;
SceUID g_clearIndicesUid;
ClearVertex *g_clearVertices = NULL;
uint16_t *g_clearIndices = NULL;
// cube geometry data
SceGxmShaderPatcherId g_cubeVertexProgramId;
SceGxmShaderPatcherId g_cubeFragmentProgramId;
SceGxmVertexProgram *g_cubeVertexProgram = NULL;
SceGxmFragmentProgram *g_cubeFragmentProgram = NULL;
SceUID g_cubeVerticesUid;
SceUID g_cubeIndicesUid;
BasicVertex *g_cubeVertices = NULL;
uint16_t *g_cubeIndices = NULL;
const SceGxmProgramParameter *g_cubeWvpParam = NULL;
// offscreen surface data and render target
SceUID g_offscreenColorBufferUid;
void *g_offscreenColorBufferData;
SceGxmColorSurface g_offscreenColorSurface;
SceGxmTexture g_offscreenTexture;
SceUID g_offscreenDepthBufferUid;
void *g_offscreenDepthBufferData;
SceGxmDepthStencilSurface g_offscreenDepthSurface;
SceGxmRenderTarget *g_offscreenRenderTarget;
// test texture
SceUID g_testTextureDataUid;
uint8_t *g_testTextureData;
SceGxmTexture g_testTexture;
// update data
float g_rotationAngle = 0.0f;
Matrix4 g_offscreenWvpMatrix;
Matrix4 g_mainWvpMatrix;
// Create data for clear draw call
static void createClearData(void *data)
{
psp2_video_t *psp = (psp2_video_t*)data;
int err = SCE_OK;
(void)err;
// register programs with the shader patcher
err = sceGxmShaderPatcherRegisterProgram(psp->g_shaderPatcher, &_binary_clear_v_gxp_start, &g_clearVertexProgramId);
assert(err == SCE_OK);
err = sceGxmShaderPatcherRegisterProgram(psp->g_shaderPatcher, &_binary_clear_f_gxp_start, &g_clearFragmentProgramId);
assert(err == SCE_OK);
// find attributes by name to create vertex format bindings
const SceGxmProgram *clearVertexProgram = sceGxmShaderPatcherGetProgramFromId(g_clearVertexProgramId);
const SceGxmProgramParameter *paramPositionAttribute = sceGxmProgramFindParameterByName(clearVertexProgram, "aPosition");
assert(paramPositionAttribute && (sceGxmProgramParameterGetCategory(paramPositionAttribute) == SCE_GXM_PARAMETER_CATEGORY_ATTRIBUTE));
// find fragment uniforms by name and cache parameter info
// note: name lookup is a slow load-time operation
const SceGxmProgram *clearFragmentProgram = sceGxmShaderPatcherGetProgramFromId(g_clearFragmentProgramId);
assert(clearFragmentProgram);
psp->g_clearColorParam = sceGxmProgramFindParameterByName(clearFragmentProgram, "color");
assert(psp->g_clearColorParam && (sceGxmProgramParameterGetCategory(psp->g_clearColorParam) == SCE_GXM_PARAMETER_CATEGORY_UNIFORM));
// create clear vertex format
SceGxmVertexAttribute clearVertexAttributes[1];
SceGxmVertexStream clearVertexStreams[1];
clearVertexAttributes[0].streamIndex = 0;
clearVertexAttributes[0].offset = 0;
clearVertexAttributes[0].format = SCE_GXM_ATTRIBUTE_FORMAT_F32;
clearVertexAttributes[0].componentCount = 2;
clearVertexAttributes[0].regIndex = sceGxmProgramParameterGetResourceIndex(paramPositionAttribute);
clearVertexStreams[0].stride = sizeof(ClearVertex);
clearVertexStreams[0].indexSource = SCE_GXM_INDEX_SOURCE_INDEX_16BIT;
// create clear programs
err = sceGxmShaderPatcherCreateVertexProgram(
psp->g_shaderPatcher,
g_clearVertexProgramId,
clearVertexAttributes,
1,
clearVertexStreams,
1,
&g_clearVertexProgram);
assert(err == SCE_OK);
err = sceGxmShaderPatcherCreateFragmentProgram(
psp->g_shaderPatcher,
g_clearFragmentProgramId,
SCE_GXM_OUTPUT_REGISTER_FORMAT_UCHAR4,
SCE_GXM_MULTISAMPLE_NONE,
NULL,
sceGxmShaderPatcherGetProgramFromId(g_clearVertexProgramId),
&g_clearFragmentProgram);
assert(err == SCE_OK);
// allocate vertices and indices
g_clearVertices = (ClearVertex *)gmmAlloc(
SCE_KERNEL_MEMBLOCK_TYPE_USER_RWDATA_UNCACHE,
3*sizeof(ClearVertex),
4,
SCE_GXM_MEMORY_ATTRIB_READ,
&g_clearVerticesUid);
g_clearIndices = (uint16_t *)gmmAlloc(
SCE_KERNEL_MEMBLOCK_TYPE_USER_RWDATA_UNCACHE,
3*sizeof(uint16_t),
2,
SCE_GXM_MEMORY_ATTRIB_READ,
&g_clearIndicesUid);
// write vertex data
g_clearVertices[0].x = -1.0f;
g_clearVertices[0].y = -1.0f;
g_clearVertices[1].x = 3.0f;
g_clearVertices[1].y = -1.0f;
g_clearVertices[2].x = -1.0f;
g_clearVertices[2].y = 3.0f;
// write index data
g_clearIndices[0] = 0;
g_clearIndices[1] = 1;
g_clearIndices[2] = 2;
}
static void destroyClearData(void *data)
{
psp2_video_t *psp = (psp2_video_t*)data;
int err = SCE_OK;
(void)err;
// release the shaderss
err = sceGxmShaderPatcherReleaseFragmentProgram(psp->g_shaderPatcher, g_clearFragmentProgram);
assert(err == SCE_OK);
err = sceGxmShaderPatcherReleaseVertexProgram(psp->g_shaderPatcher, g_clearVertexProgram);
assert(err == SCE_OK);
// free the memory used for vertices and indices
gmmFree(g_clearIndicesUid);
gmmFree(g_clearVerticesUid);
// unregister programs since we don't need them any more
err = sceGxmShaderPatcherUnregisterProgram(psp->g_shaderPatcher, g_clearFragmentProgramId);
assert(err == SCE_OK);
err = sceGxmShaderPatcherUnregisterProgram(psp->g_shaderPatcher, g_clearVertexProgramId);
assert(err == SCE_OK);
}
// Create data for cube draw call
static void createCubeData(void *data)
{
psp2_video_t *psp = (psp2_video_t*)data;
int err = SCE_OK;
(void)err;
// register programs with the patcher
err = sceGxmShaderPatcherRegisterProgram(psp->g_shaderPatcher, &_binary_cube_v_gxp_start, &g_cubeVertexProgramId);
assert(err == SCE_OK);
err = sceGxmShaderPatcherRegisterProgram(psp->g_shaderPatcher, &_binary_cube_f_gxp_start, &g_cubeFragmentProgramId);
assert(err == SCE_OK);
// find vertex uniforms by name and cache parameter info
// note: name lookup is a slow load-time operation
const SceGxmProgram *cubeVertexProgram = sceGxmShaderPatcherGetProgramFromId(g_cubeVertexProgramId);
assert(cubeVertexProgram);
g_cubeWvpParam = sceGxmProgramFindParameterByName(cubeVertexProgram, "wvp");
assert(g_cubeWvpParam && (sceGxmProgramParameterGetCategory(g_cubeWvpParam) == SCE_GXM_PARAMETER_CATEGORY_UNIFORM));
// find attributes by name to create vertex format bindings
const SceGxmProgramParameter *paramPositionAttribute = sceGxmProgramFindParameterByName(cubeVertexProgram, "aPosition");
assert(paramPositionAttribute && (sceGxmProgramParameterGetCategory(paramPositionAttribute) == SCE_GXM_PARAMETER_CATEGORY_ATTRIBUTE));
const SceGxmProgramParameter *paramColorAttribute = sceGxmProgramFindParameterByName(cubeVertexProgram, "aColor");
assert(paramColorAttribute && (sceGxmProgramParameterGetCategory(paramColorAttribute) == SCE_GXM_PARAMETER_CATEGORY_ATTRIBUTE));
const SceGxmProgramParameter *paramTexCoordAttribute = sceGxmProgramFindParameterByName(cubeVertexProgram, "aTexCoord");
assert(paramTexCoordAttribute && (sceGxmProgramParameterGetCategory(paramTexCoordAttribute) == SCE_GXM_PARAMETER_CATEGORY_ATTRIBUTE));
// create shaded triangle vertex format
SceGxmVertexAttribute basicVertexAttributes[3];
SceGxmVertexStream basicVertexStreams[1];
basicVertexAttributes[0].streamIndex = 0;
basicVertexAttributes[0].offset = 0;
basicVertexAttributes[0].format = SCE_GXM_ATTRIBUTE_FORMAT_F32;
basicVertexAttributes[0].componentCount = 3;
basicVertexAttributes[0].regIndex = sceGxmProgramParameterGetResourceIndex(paramPositionAttribute);
basicVertexAttributes[1].streamIndex = 0;
basicVertexAttributes[1].offset = 12;
basicVertexAttributes[1].format = SCE_GXM_ATTRIBUTE_FORMAT_U8N;
basicVertexAttributes[1].componentCount = 4;
basicVertexAttributes[1].regIndex = sceGxmProgramParameterGetResourceIndex(paramColorAttribute);
basicVertexAttributes[2].streamIndex = 0;
basicVertexAttributes[2].offset = 16;
basicVertexAttributes[2].format = SCE_GXM_ATTRIBUTE_FORMAT_F16;
basicVertexAttributes[2].componentCount = 2;
basicVertexAttributes[2].regIndex = sceGxmProgramParameterGetResourceIndex(paramTexCoordAttribute);
basicVertexStreams[0].stride = sizeof(BasicVertex);
basicVertexStreams[0].indexSource = SCE_GXM_INDEX_SOURCE_INDEX_16BIT;
// create cube vertex program
err = sceGxmShaderPatcherCreateVertexProgram(
psp->g_shaderPatcher,
g_cubeVertexProgramId,
basicVertexAttributes,
3,
basicVertexStreams,
1,
&g_cubeVertexProgram);
assert(err == SCE_OK);
// create cube fragment program
err = sceGxmShaderPatcherCreateFragmentProgram(
psp->g_shaderPatcher,
g_cubeFragmentProgramId,
SCE_GXM_OUTPUT_REGISTER_FORMAT_UCHAR4,
SCE_GXM_MULTISAMPLE_NONE,
NULL,
sceGxmShaderPatcherGetProgramFromId(g_cubeVertexProgramId),
&g_cubeFragmentProgram);
assert(err == SCE_OK);
// allocate memory for vertex and index data
g_cubeVertices = (BasicVertex *)gmmAlloc(
SCE_KERNEL_MEMBLOCK_TYPE_USER_RWDATA_UNCACHE,
24*sizeof(BasicVertex),
4,
SCE_GXM_MEMORY_ATTRIB_READ,
&g_cubeVerticesUid);
g_cubeIndices = (uint16_t *)gmmAlloc(
SCE_KERNEL_MEMBLOCK_TYPE_USER_RWDATA_UNCACHE,
36*sizeof(uint16_t),
2,
SCE_GXM_MEMORY_ATTRIB_READ,
&g_cubeIndicesUid);
// write vertices
BasicVertex *vertexData = g_cubeVertices;
for (uint32_t face = 0; face < 6; ++face)
{
float sign = ((face & 0x1) ? 1.0f : -1.0f);
uint32_t axis = face >> 1;
float ox = (axis == 0) ? sign : 0.0f;
float oy = (axis == 1) ? sign : 0.0f;
float oz = (axis == 2) ? sign : 0.0f;
float ux = (axis != 0) ? 1.0f : 0.0f;
float uy = (axis == 0) ? 1.0f : 0.0f;
float uz = 0.0f;
float vx = 0.0f;
float vy = (axis == 2) ? 1.0f : 0.0f;
float vz = (axis != 2) ? 1.0f : 0.0f;
uint16_t half0 = 0x0000;
uint16_t half1 = 0x3c00;
uint32_t color = 0;
switch (axis)
{
case 0:
color = 0xffffffff;
break;
case 1:
color = 0xffdddddd;
break;
case 2:
color = 0xffbbbbbb;
break;
}
vertexData->x = ox - ux - vx;
vertexData->y = oy - uy - vy;
vertexData->z = oz - uz - vz;
vertexData->color = color;
vertexData->u = half0;
vertexData->v = half1;
++vertexData;
vertexData->x = ox + ux - vx;
vertexData->y = oy + uy - vy;
vertexData->z = oz + uz - vz;
vertexData->color = color;
vertexData->u = half1;
vertexData->v = half1;
++vertexData;
vertexData->x = ox + ux + vx;
vertexData->y = oy + uy + vy;
vertexData->z = oz + uz + vz;
vertexData->color = color;
vertexData->u = half1;
vertexData->v = half0;
++vertexData;
vertexData->x = ox - ux + vx;
vertexData->y = oy - uy + vy;
vertexData->z = oz - uz + vz;
vertexData->color = color;
vertexData->u = half0;
vertexData->v = half0;
++vertexData;
}
// write indices
uint16_t *indexData = g_cubeIndices;
for (uint32_t face = 0; face < 6; ++face)
{
uint32_t offset = 4*face;
*indexData++ = offset + 0;
*indexData++ = offset + 1;
*indexData++ = offset + 2;
*indexData++ = offset + 2;
*indexData++ = offset + 3;
*indexData++ = offset + 0;
}
}
static void destroyCubeData(void *data)
{
psp2_video_t *psp = (psp2_video_t*)data;
int err = SCE_OK;
(void)err;
// release the shaderss
err = sceGxmShaderPatcherReleaseFragmentProgram(psp->g_shaderPatcher, g_cubeFragmentProgram);
assert(err == SCE_OK);
err = sceGxmShaderPatcherReleaseVertexProgram(psp->g_shaderPatcher, g_cubeVertexProgram);
assert(err == SCE_OK);
// free the memory used for vertices and indices
gmmFree(g_cubeIndicesUid);
gmmFree(g_cubeVerticesUid);
// unregister programs since we don't need them any more
err = sceGxmShaderPatcherUnregisterProgram(psp->g_shaderPatcher, g_cubeFragmentProgramId);
assert(err == SCE_OK);
err = sceGxmShaderPatcherUnregisterProgram(psp->g_shaderPatcher, g_cubeVertexProgramId);
assert(err == SCE_OK);
}
// Create test texture data
static void createTestTextureData(void *data)
{
psp2_video_t *psp = (psp2_video_t*)data;
int err = SCE_OK;
(void)err;
// validate gxt
const void *gxt = _binary_test_gxt_start;
assert(sceGxtCheckData(gxt) == SCE_OK);
// get the size of the texture data
const uint32_t dataSize = sceGxtGetDataSize(gxt);
// allocate memory
g_testTextureData = (uint8_t *)gmmAlloc(
SCE_KERNEL_MEMBLOCK_TYPE_USER_RWDATA_UNCACHE,
dataSize,
SCE_GXM_TEXTURE_ALIGNMENT,
SCE_GXM_MEMORY_ATTRIB_READ,
&g_testTextureDataUid);
// copy texture data
const void *dataSrc = sceGxtGetDataAddress(gxt);
memcpy(g_testTextureData, dataSrc, dataSize);
// set up the texture control words
err = sceGxtInitTexture(&g_testTexture, gxt, g_testTextureData, 0);
assert(err == SCE_OK);
// set linear filtering
err = sceGxmTextureSetMagFilter(
&g_testTexture,
SCE_GXM_TEXTURE_FILTER_LINEAR);
assert(err == SCE_OK);
err = sceGxmTextureSetMinFilter(
&g_testTexture,
SCE_GXM_TEXTURE_FILTER_LINEAR);
assert(err == SCE_OK);
}
static void destroyTestTextureData(void *data)
{
psp2_video_t *psp = (psp2_video_t*)data;
gmmFree(g_testTextureDataUid);
}
static void createOffscreenBuffer(void *data, const video_info_t *video)
{
psp2_video_t *psp = (psp2_video_t*)data;
int err = SCE_OK;
(void)err;
/* TODO - ensure width/height is POT - if libgxm cares about that */
// allocate memory
g_offscreenColorBufferData = gmmAlloc(
SCE_KERNEL_MEMBLOCK_TYPE_USER_CDRAM_RWDATA,
video->width * video->height * (video->rgb32 ? 4 : 2),
MAX(SCE_GXM_TEXTURE_ALIGNMENT, SCE_GXM_COLOR_SURFACE_ALIGNMENT),
SCE_GXM_MEMORY_ATTRIB_READ | SCE_GXM_MEMORY_ATTRIB_WRITE,
&g_offscreenColorBufferUid);
// set up the surface
err = sceGxmColorSurfaceInit(
&g_offscreenColorSurface,
video->rgb32 ? SCE_GXM_COLOR_FORMAT_A8R8G8B8 : SCE_GXM_COLOR_FORMAT_R5G6B5,
SCE_GXM_COLOR_SURFACE_LINEAR,
SCE_GXM_COLOR_SURFACE_SCALE_NONE,
SCE_GXM_OUTPUT_REGISTER_SIZE_32BIT,
video->width,
video->height,
video->width,
g_offscreenColorBufferData);
assert(err == SCE_OK);
// set up the texture
err = sceGxmTextureInitLinear(
&g_offscreenTexture,
g_offscreenColorBufferData,
video->rgb32 ? SCE_GXM_TEXTURE_FORMAT_A8B8G8R8 : SCE_GXM_TEXTURE_FORMAT_R5G6B5,
video->width,
video->height,
1);
assert(err == SCE_OK);
// set linear filtering
err = sceGxmTextureSetMagFilter(&g_offscreenTexture, SCE_GXM_TEXTURE_FILTER_LINEAR);
assert(err == SCE_OK);
err = sceGxmTextureSetMinFilter(&g_offscreenTexture, SCE_GXM_TEXTURE_FILTER_LINEAR);
assert(err == SCE_OK);
// create the depth/stencil surface
const uint32_t alignedWidth = ALIGN(DISPLAY_WIDTH, SCE_GXM_TILE_SIZEX);
const uint32_t alignedHeight = ALIGN(DISPLAY_HEIGHT, SCE_GXM_TILE_SIZEY);
uint32_t sampleCount = alignedWidth*alignedHeight;
uint32_t depthStrideInSamples = alignedWidth;
g_offscreenDepthBufferData = gmmAlloc(
SCE_KERNEL_MEMBLOCK_TYPE_USER_RWDATA_UNCACHE,
4*sampleCount,
SCE_GXM_DEPTHSTENCIL_SURFACE_ALIGNMENT,
SCE_GXM_MEMORY_ATTRIB_READ | SCE_GXM_MEMORY_ATTRIB_WRITE,
&g_offscreenDepthBufferUid);
err = sceGxmDepthStencilSurfaceInit(
&g_offscreenDepthSurface,
SCE_GXM_DEPTH_STENCIL_FORMAT_S8D24,
SCE_GXM_DEPTH_STENCIL_SURFACE_TILED,
depthStrideInSamples,
g_offscreenDepthBufferData,
NULL);
// create a render target
g_offscreenRenderTarget = createRenderTarget(video->width, video->height, SCE_GXM_MULTISAMPLE_NONE);
}
static void destroyOffscreenBuffer(void *data)
{
psp2_video_t *psp = (psp2_video_t*)data;
// destroy render target
destroyRenderTarget(psp, g_offscreenRenderTarget);
// free the memory
gmmFree(g_offscreenDepthBufferUid);
gmmFree(g_offscreenColorBufferUid);
}
static void update(void *data)
{
psp2_video_t *psp = (psp2_video_t*)data;
// advance rotation
g_rotationAngle += 0.25f*SCE_MATH_TWOPI/60.0f;
if (g_rotationAngle > SCE_MATH_TWOPI)
g_rotationAngle -= SCE_MATH_TWOPI;
// copmute our matrices
Matrix4 offscreenProjectionMatrix = Matrix4::perspective(
SCE_MATH_PI/4.0f,
(float)psp->width /(float)psp->height,
0.1f,
10.0f);
Matrix4 mainProjectionMatrix = Matrix4::perspective(
SCE_MATH_PI/4.0f,
(float)DISPLAY_WIDTH/(float)DISPLAY_HEIGHT,