-
Notifications
You must be signed in to change notification settings - Fork 183
/
FrameBuffer.cpp
1810 lines (1559 loc) · 57.9 KB
/
FrameBuffer.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
991
992
993
994
995
996
997
998
999
1000
#include <assert.h>
#include <math.h>
#include <algorithm>
#include <vector>
#include "FrameBuffer.h"
#include "DepthBuffer.h"
#include "N64.h"
#include "RSP.h"
#include "RDP.h"
#include "VI.h"
#include "Textures.h"
#include "Combiner.h"
#include "Types.h"
#include "Config.h"
#include "Debugger.h"
#include "DebugDump.h"
#include "PostProcessor.h"
#include "FrameBufferInfo.h"
#include "Log.h"
#include "MemoryStatus.h"
#include "BufferCopy/ColorBufferToRDRAM.h"
#include "BufferCopy/DepthBufferToRDRAM.h"
#include "BufferCopy/RDRAMtoColorBuffer.h"
#include <Graphics/Context.h>
#include <Graphics/Parameters.h>
#include <Graphics/ColorBufferReader.h>
#include "DisplayWindow.h"
using namespace std;
using namespace graphics;
FrameBuffer::FrameBuffer()
: m_copyFBO(ObjectHandle::defaultFramebuffer)
, m_ColorBufferFBO(0)
, m_pColorBufferTexture(nullptr)
{
m_pTexture = textureCache().addFrameBufferTexture(config.video.multisampling != 0 ?
textureTarget::TEXTURE_2D_MULTISAMPLE : textureTarget::TEXTURE_2D);
m_FBO = gfxContext.createFramebuffer();
if (config.frameBufferEmulation.copyDepthToMainDepthBuffer != 0)
m_depthFBO = gfxContext.createFramebuffer();
}
FrameBuffer::~FrameBuffer()
{
gfxContext.deleteFramebuffer(m_FBO);
gfxContext.deleteFramebuffer(m_depthFBO);
gfxContext.deleteFramebuffer(m_resolveFBO);
gfxContext.deleteFramebuffer(m_SubFBO);
gfxContext.deleteFramebuffer(m_copyFBO);
textureCache().removeFrameBufferTexture(m_pTexture);
textureCache().removeFrameBufferTexture(m_pDepthTexture);
textureCache().removeFrameBufferTexture(m_pResolveTexture);
textureCache().removeFrameBufferTexture(m_pSubTexture);
textureCache().removeFrameBufferTexture(m_pFrameBufferCopyTexture);
_destroyColorFBTexure();
}
static
void _initFrameBufferTexture(u32 _address, u16 _width, u16 _height, f32 _scale, u16 _format, u16 _size, CachedTexture *_pTexture)
{
const FramebufferTextureFormats & fbTexFormats = gfxContext.getFramebufferTextureFormats();
_pTexture->width = static_cast<u16>(static_cast<u32>(static_cast<f32>(_width) * _scale));
_pTexture->height = static_cast<u16>(static_cast<u32>(static_cast<f32>(_height) * _scale));
_pTexture->format = _format;
_pTexture->size = _size;
_pTexture->clampS = 1;
_pTexture->clampT = 1;
_pTexture->address = _address;
_pTexture->clampWidth = _width;
_pTexture->clampHeight = _height;
_pTexture->frameBufferTexture = CachedTexture::fbOneSample;
_pTexture->maskS = 0;
_pTexture->maskT = 0;
_pTexture->mirrorS = 0;
_pTexture->mirrorT = 0;
_pTexture->textureBytes = _pTexture->width * _pTexture->height;
_pTexture->hdRatioS = _scale;
_pTexture->hdRatioT = _scale;
if (_size > G_IM_SIZ_8b)
_pTexture->textureBytes *= fbTexFormats.colorFormatBytes;
else
_pTexture->textureBytes *= fbTexFormats.monochromeFormatBytes;
}
void FrameBuffer::_initTexture(u16 _width, u16 _height, u16 _format, u16 _size, CachedTexture *_pTexture)
{
_initFrameBufferTexture(m_startAddress, _width, _height, m_scale, _format, _size, _pTexture);
}
static
void _setAndAttachBufferTexture(ObjectHandle _fbo, CachedTexture *_pTexture, u32 _t, bool _multisampling)
{
const FramebufferTextureFormats & fbTexFormat = gfxContext.getFramebufferTextureFormats();
Context::InitTextureParams initParams;
initParams.handle = _pTexture->name;
initParams.textureUnitIndex = textureIndices::Tex[_t];
if (_multisampling)
initParams.msaaLevel = config.video.multisampling;
initParams.width = _pTexture->width;
initParams.height = _pTexture->height;
if (_pTexture->size > G_IM_SIZ_8b) {
initParams.internalFormat = fbTexFormat.colorInternalFormat;
initParams.format = fbTexFormat.colorFormat;
initParams.dataType = fbTexFormat.colorType;
} else {
initParams.internalFormat = fbTexFormat.monochromeInternalFormat;
initParams.format = fbTexFormat.monochromeFormat;
initParams.dataType = fbTexFormat.monochromeType;
}
gfxContext.init2DTexture(initParams);
if (!_multisampling) {
Context::TexParameters texParams;
texParams.handle = _pTexture->name;
texParams.target = textureTarget::TEXTURE_2D;
texParams.textureUnitIndex = textureIndices::Tex[_t];
texParams.minFilter = textureParameters::FILTER_NEAREST;
texParams.magFilter = textureParameters::FILTER_NEAREST;
gfxContext.setTextureParameters(texParams);
}
Context::FrameBufferRenderTarget bufTarget;
bufTarget.bufferHandle = _fbo;
bufTarget.bufferTarget = bufferTarget::FRAMEBUFFER;
bufTarget.attachment = bufferAttachment::COLOR_ATTACHMENT0;
bufTarget.textureTarget = _multisampling ? textureTarget::TEXTURE_2D_MULTISAMPLE : textureTarget::TEXTURE_2D;
bufTarget.textureHandle = _pTexture->name;
gfxContext.addFrameBufferRenderTarget(bufTarget);
assert(!gfxContext.isFramebufferError());
}
void FrameBuffer::_setAndAttachTexture(ObjectHandle _fbo, CachedTexture *_pTexture, u32 _t, bool _multisampling)
{
_setAndAttachBufferTexture(_fbo, _pTexture, _t, _multisampling);
}
bool FrameBuffer::isAuxiliary() const
{
return m_width != VI.width || m_size < G_IM_SIZ_16b;
}
void FrameBuffer::init(u32 _address, u16 _format, u16 _size, u16 _width, bool _cfb)
{
m_startAddress = _address;
m_width = _width;
m_height = _cfb ? VI.height : 1;
// m_height = VI.height;
m_size = _size;
updateEndAddress();
if (isAuxiliary() && config.frameBufferEmulation.copyAuxToRDRAM != 0) {
m_scale = 1.0f;
} else if (config.frameBufferEmulation.nativeResFactor != 0 && config.frameBufferEmulation.enable != 0) {
m_scale = static_cast<float>(config.frameBufferEmulation.nativeResFactor);
} else {
m_scale = std::max(dwnd().getScaleX(), 1.0f);
}
m_cfb = _cfb;
m_cleared = false;
m_fingerprint = false;
m_swapCount = dwnd().getBuffersSwapCount();
const u16 maxHeight = VI_GetMaxBufferHeight(_width);
_initTexture(_width, maxHeight, _format, _size, m_pTexture);
if (config.video.multisampling != 0) {
_setAndAttachTexture(m_FBO, m_pTexture, 0, true);
m_pTexture->frameBufferTexture = CachedTexture::fbMultiSample;
m_pResolveTexture = textureCache().addFrameBufferTexture(textureTarget::TEXTURE_2D);
_initTexture(_width, maxHeight, _format, _size, m_pResolveTexture);
m_resolveFBO = gfxContext.createFramebuffer();
_setAndAttachTexture(m_resolveFBO, m_pResolveTexture, 0, false);
assert(!gfxContext.isFramebufferError());
gfxContext.bindFramebuffer(bufferTarget::FRAMEBUFFER, m_FBO);
} else
_setAndAttachTexture(m_FBO, m_pTexture, 0, false);
// gfxContext.clearColorBuffer(0.0f, 0.0f, 0.0f, 0.0f);
}
void FrameBuffer::updateEndAddress()
{
const u32 height = max(1U, m_height);
m_endAddress = min(RDRAMSize, m_startAddress + (((m_width * height) << m_size >> 1) - 1));
}
inline
u32 _cutHeight(u32 _address, u32 _height, u32 _stride)
{
if (_address > RDRAMSize)
return 0;
if (_address + _stride * _height > (RDRAMSize + 1))
return (RDRAMSize + 1 - _address) / _stride;
return _height;
}
void FrameBuffer::setBufferClearParams(u32 _fillcolor, s32 _ulx, s32 _uly, s32 _lrx, s32 _lry)
{
m_cleared = true;
m_clearParams.fillcolor = _fillcolor;
m_clearParams.ulx = _ulx;
m_clearParams.lrx = _lrx;
m_clearParams.uly = _uly;
m_clearParams.lry = _lry;
}
void FrameBuffer::copyRdram()
{
const u32 stride = m_width << m_size >> 1;
const u32 height = _cutHeight(m_startAddress, m_height, stride);
if (height == 0)
return;
m_cleared = false;
const u32 dataSize = stride * height;
// Auxiliary frame buffer
if (isAuxiliary() && config.frameBufferEmulation.copyAuxToRDRAM == 0) {
// Write small amount of data to the start of the buffer.
// This is necessary for auxilary buffers: game can restore content of RDRAM when buffer is not needed anymore
// Thus content of RDRAM on moment of buffer creation will be the same as when buffer becomes obsolete.
// Validity check will see that the RDRAM is the same and thus the buffer is valid, which is false.
const u32 twoPercent = max(4U, dataSize / 200);
u32 start = m_startAddress >> 2;
u32 * pData = reinterpret_cast<u32*>(RDRAM);
for (u32 i = 0; i < twoPercent; ++i) {
if (i < 4)
pData[start++] = fingerprint[i];
else
pData[start++] = 0;
}
m_fingerprint = true;
return;
}
m_RdramCopy.resize(dataSize);
memcpy(m_RdramCopy.data(), RDRAM + m_startAddress, dataSize);
}
void FrameBuffer::setDirty()
{
m_cleared = false;
m_RdramCopy.clear();
}
bool FrameBuffer::isValid(bool _forceCheck) const
{
if (!_forceCheck) {
if (m_validityChecked == dwnd().getBuffersSwapCount())
return true; // Already checked
m_validityChecked = dwnd().getBuffersSwapCount();
}
const u32 * const pData = reinterpret_cast<const u32*>(RDRAM);
if (m_cleared) {
const u32 testColor = m_clearParams.fillcolor & 0xFFFEFFFE;
const u32 stride = m_width << m_size >> 1;
const s32 lry = static_cast<s32>(_cutHeight(m_startAddress, static_cast<u32>(m_clearParams.lry), stride));
if (lry == 0)
return false;
const u32 ci_width_in_dwords = m_width >> (3 - m_size);
const u32 start = (m_startAddress >> 2) + static_cast<u32>(m_clearParams.uly) * ci_width_in_dwords;
const u32 * dst = pData + start;
u32 wrongPixels = 0;
for (s32 y = m_clearParams.uly; y < lry; ++y) {
for (s32 x = m_clearParams.ulx; x < m_clearParams.lrx; ++x) {
if ((dst[x] & 0xFFFEFFFE) != testColor)
++wrongPixels;
}
dst += ci_width_in_dwords;
}
return wrongPixels < (m_endAddress - m_startAddress) / 400; // threshold level 1% of dwords
} else if (m_fingerprint) {
//check if our fingerprint is still there
u32 start = m_startAddress >> 2;
for (u32 i = 0; i < 4; ++i)
if ((pData[start++] & 0xFFFEFFFE) != (fingerprint[i] & 0xFFFEFFFE))
return false;
return true;
} else if (!m_RdramCopy.empty()) {
const u32 * const pCopy = reinterpret_cast<const u32* >(m_RdramCopy.data());
const u32 size = static_cast<u32>(m_RdramCopy.size());
const u32 size_dwords = size >> 2;
u32 start = m_startAddress >> 2;
u32 wrongPixels = 0;
for (u32 i = 0; i < size_dwords; ++i) {
if ((pData[start++] & 0xFFFEFFFE) != (pCopy[i] & 0xFFFEFFFE))
++wrongPixels;
}
return wrongPixels < size / 400; // threshold level 1% of dwords
}
return true; // No data to decide
}
void FrameBuffer::resolveMultisampledTexture(bool _bForce)
{
if (!Context::Multisampling)
return;
if (m_resolved && !_bForce)
return;
if (!m_pResolveTexture)
return;
Context::BlitFramebuffersParams blitParams;
blitParams.readBuffer = m_FBO;
blitParams.drawBuffer = m_resolveFBO;
blitParams.srcX0 = 0;
blitParams.srcY0 = 0;
blitParams.srcX1 = m_pTexture->width;
blitParams.srcY1 = m_pTexture->height;
blitParams.dstX0 = 0;
blitParams.dstY0 = 0;
blitParams.dstX1 = m_pResolveTexture->width;
blitParams.dstY1 = m_pResolveTexture->height;
blitParams.mask = blitMask::COLOR_BUFFER;
blitParams.filter = textureParameters::FILTER_NEAREST;
gfxContext.blitFramebuffers(blitParams);
gfxContext.bindFramebuffer(bufferTarget::READ_FRAMEBUFFER, ObjectHandle::defaultFramebuffer);
frameBufferList().setCurrentDrawBuffer();
m_resolved = true;
}
void FrameBuffer::copyDepthTexture()
{
if (config.frameBufferEmulation.copyDepthToMainDepthBuffer != 0)
DepthBuffer::copyDepthBufferTexture(this, m_pDepthTexture, m_depthFBO);
}
bool FrameBuffer::_initSubTexture(u32 _t)
{
if (!m_SubFBO.isNotNull())
m_SubFBO = gfxContext.createFramebuffer();
gDPTile * pTile = gSP.textureTile[_t];
if (pTile->lrs < pTile->uls || pTile->lrt < pTile->ult)
return false;
const u32 width = pTile->lrs - pTile->uls + 1;
const u32 height = pTile->lrt - pTile->ult + 1;
if (m_pSubTexture != nullptr) {
if (m_pSubTexture->size == m_pTexture->size &&
m_pSubTexture->clampWidth == width &&
m_pSubTexture->clampHeight == height)
return true;
textureCache().removeFrameBufferTexture(m_pSubTexture);
}
m_pSubTexture = textureCache().addFrameBufferTexture(textureTarget::TEXTURE_2D);
_initTexture(static_cast<u16>(width), static_cast<u16>(height), m_pTexture->format, m_pTexture->size, m_pSubTexture);
m_pSubTexture->clampS = pTile->clamps;
m_pSubTexture->clampT = pTile->clampt;
m_pSubTexture->offsetS = 0.0f;
m_pSubTexture->offsetT = 0.0f;
m_pSubTexture->hdRatioS = m_pTexture->hdRatioS;
m_pSubTexture->hdRatioT = m_pTexture->hdRatioT;
_setAndAttachTexture(m_SubFBO, m_pSubTexture, _t, false);
return true;
}
CachedTexture * FrameBuffer::_getSubTexture(u32 _t)
{
if (!Context::BlitFramebuffer)
return m_pTexture;
if (!_initSubTexture(_t))
return m_pTexture;
s32 x0 = static_cast<s32>(m_pTexture->offsetS * m_scale);
s32 y0 = static_cast<s32>(m_pTexture->offsetT * m_scale);
s32 copyWidth = m_pSubTexture->width;
if (x0 + copyWidth > m_pTexture->width)
copyWidth = m_pTexture->width - x0;
s32 copyHeight = m_pSubTexture->height;
if (y0 + copyHeight > m_pTexture->height)
copyHeight = m_pTexture->height - y0;
ObjectHandle readFBO = m_FBO;
if (Context::WeakBlitFramebuffer &&
m_pTexture->frameBufferTexture == CachedTexture::fbMultiSample) {
resolveMultisampledTexture(true);
readFBO = m_resolveFBO;
}
Context::BlitFramebuffersParams blitParams;
blitParams.readBuffer = readFBO;
blitParams.drawBuffer = m_SubFBO;
blitParams.srcX0 = x0;
blitParams.srcY0 = y0;
blitParams.srcX1 = x0 + copyWidth;
blitParams.srcY1 = y0 + copyHeight;
blitParams.dstX0 = 0;
blitParams.dstY0 = 0;
blitParams.dstX1 = copyWidth;
blitParams.dstY1 = copyHeight;
blitParams.mask = blitMask::COLOR_BUFFER;
blitParams.filter = textureParameters::FILTER_NEAREST;
gfxContext.blitFramebuffers(blitParams);
gfxContext.bindFramebuffer(bufferTarget::READ_FRAMEBUFFER, ObjectHandle::defaultFramebuffer);
frameBufferList().setCurrentDrawBuffer();
return m_pSubTexture;
}
void FrameBuffer::_initCopyTexture()
{
m_copyFBO = gfxContext.createFramebuffer();
m_pFrameBufferCopyTexture = textureCache().addFrameBufferTexture(config.video.multisampling != 0 ?
textureTarget::TEXTURE_2D_MULTISAMPLE : textureTarget::TEXTURE_2D);
_initTexture(static_cast<u16>(m_width), VI_GetMaxBufferHeight(static_cast<u16>(m_width)),
m_pTexture->format, m_pTexture->size, m_pFrameBufferCopyTexture);
_setAndAttachTexture(m_copyFBO, m_pFrameBufferCopyTexture, 0, config.video.multisampling != 0);
if (config.video.multisampling != 0)
m_pFrameBufferCopyTexture->frameBufferTexture = CachedTexture::fbMultiSample;
}
CachedTexture * FrameBuffer::_copyFrameBufferTexture()
{
if (m_copied)
return m_pFrameBufferCopyTexture;
if (m_pFrameBufferCopyTexture == nullptr)
_initCopyTexture();
Context::BlitFramebuffersParams blitParams;
blitParams.readBuffer = m_FBO;
blitParams.drawBuffer = m_copyFBO;
blitParams.srcX0 = 0;
blitParams.srcY0 = 0;
blitParams.srcX1 = m_pTexture->width;
blitParams.srcY1 = m_pTexture->height;
blitParams.dstX0 = 0;
blitParams.dstY0 = 0;
blitParams.dstX1 = m_pTexture->width;
blitParams.dstY1 = m_pTexture->height;
blitParams.mask = blitMask::COLOR_BUFFER;
blitParams.filter = textureParameters::FILTER_NEAREST;
gfxContext.blitFramebuffers(blitParams);
gfxContext.bindFramebuffer(bufferTarget::READ_FRAMEBUFFER, ObjectHandle::defaultFramebuffer);
frameBufferList().setCurrentDrawBuffer();
m_copied = true;
return m_pFrameBufferCopyTexture;
}
CachedTexture * FrameBuffer::getTexture(u32 _t)
{
const bool getDepthTexture = m_isDepthBuffer &&
gDP.colorImage.address == gDP.depthImageAddress &&
m_pDepthBuffer != nullptr &&
(config.generalEmulation.hacks & hack_ZeldaMM) == 0;
CachedTexture *pTexture = getDepthTexture ? m_pDepthBuffer->m_pDepthBufferTexture : m_pTexture;
if (this == frameBufferList().getCurrent()) {
if (Context::TextureBarrier)
gfxContext.textureBarrier();
else if (Context::BlitFramebuffer)
pTexture = getDepthTexture ? m_pDepthBuffer->copyDepthBufferTexture(this) : _copyFrameBufferTexture();
}
const u32 shift = (gSP.textureTile[_t]->imageAddress - m_startAddress) >> (m_size - 1);
const u32 factor = m_width;
if (m_loadType == LOADTYPE_TILE) {
pTexture->offsetS = static_cast<f32>(m_loadTileOrigin.uls + (shift % factor));
pTexture->offsetT = static_cast<f32>(m_loadTileOrigin.ult + shift / factor);
} else {
pTexture->offsetS = static_cast<f32>(shift % factor);
pTexture->offsetT = static_cast<f32>(shift / factor);
}
pTexture->hdRatioS = m_pTexture->hdRatioS;
pTexture->hdRatioT = m_pTexture->hdRatioT;
if (!getDepthTexture && (gSP.textureTile[_t]->clamps == 0 || gSP.textureTile[_t]->clampt == 0))
pTexture = _getSubTexture(_t);
pTexture->scaleS = m_scale / static_cast<f32>(pTexture->width);
pTexture->scaleT = m_scale / static_cast<f32>(pTexture->height);
pTexture->shiftScaleS = calcShiftScaleS(*gSP.textureTile[_t]);
pTexture->shiftScaleT = calcShiftScaleT(*gSP.textureTile[_t]);
return pTexture;
}
CachedTexture * FrameBuffer::getTextureBG()
{
CachedTexture *pTexture = m_pTexture;
if (this == frameBufferList().getCurrent()) {
if (Context::TextureBarrier)
gfxContext.textureBarrier();
else if (Context::BlitFramebuffer)
pTexture = _copyFrameBufferTexture();
}
pTexture->scaleS = m_scale / static_cast<f32>(pTexture->width);
pTexture->scaleT = m_scale / static_cast<f32>(pTexture->height);
pTexture->shiftScaleS = 1.0f;
pTexture->shiftScaleT = 1.0f;
pTexture->offsetS = gSP.bgImage.imageX;
pTexture->offsetT = gSP.bgImage.imageY;
return pTexture;
}
CachedTexture * FrameBuffer::getColorFbTexture()
{
if(m_pColorBufferTexture == nullptr ||
m_pColorBufferTexture->width != m_width ||
m_pColorBufferTexture->height != VI_GetMaxBufferHeight(m_width))
{
_destroyColorFBTexure();
_initColorFBTexture(m_width);
}
return m_pColorBufferTexture;
}
graphics::ObjectHandle FrameBuffer::getColorFbFbo()
{
return m_ColorBufferFBO;
}
const u8 * FrameBuffer::readPixels(s32 _x0, s32 _y0, u32 _width, u32 _height, u32 _size, bool _sync)
{
return m_bufferReader->readPixels(_x0, _y0, _width, _height, _size, _sync);
}
void FrameBuffer::cleanUp()
{
m_bufferReader->cleanUp();
}
void FrameBuffer::_initColorFBTexture(int _width)
{
m_ColorBufferFBO = gfxContext.createFramebuffer();
const FramebufferTextureFormats & fbTexFormat = gfxContext.getFramebufferTextureFormats();
m_pColorBufferTexture = textureCache().addFrameBufferTexture(Context::EglImage ? textureTarget::TEXTURE_EXTERNAL : textureTarget::TEXTURE_2D);
m_pColorBufferTexture->format = G_IM_FMT_RGBA;
m_pColorBufferTexture->size = 2;
m_pColorBufferTexture->clampS = 1;
m_pColorBufferTexture->clampT = 1;
m_pColorBufferTexture->frameBufferTexture = CachedTexture::fbOneSample;
m_pColorBufferTexture->maskS = 0;
m_pColorBufferTexture->maskT = 0;
m_pColorBufferTexture->mirrorS = 0;
m_pColorBufferTexture->mirrorT = 0;
m_pColorBufferTexture->width = _width;
m_pColorBufferTexture->height = VI_GetMaxBufferHeight(_width);
m_pColorBufferTexture->textureBytes = m_pColorBufferTexture->width * m_pColorBufferTexture->height * fbTexFormat.colorFormatBytes;
m_bufferReader.reset(gfxContext.createColorBufferReader(m_pColorBufferTexture));
// Skip this since texture is initialized in the EGL color buffer reader
if (!Context::EglImage)
{
Context::InitTextureParams params;
params.handle = m_pColorBufferTexture->name;
params.target = textureTarget::TEXTURE_2D;
params.width = m_pColorBufferTexture->width;
params.height = m_pColorBufferTexture->height;
params.internalFormat = fbTexFormat.colorInternalFormat;
params.format = fbTexFormat.colorFormat;
params.dataType = fbTexFormat.colorType;
gfxContext.init2DTexture(params);
}
{
Context::TexParameters params;
params.handle = m_pColorBufferTexture->name;
params.target = Context::EglImage ? textureTarget::TEXTURE_EXTERNAL : textureTarget::TEXTURE_2D;
params.textureUnitIndex = textureIndices::Tex[0];
params.minFilter = textureParameters::FILTER_LINEAR;
params.magFilter = textureParameters::FILTER_LINEAR;
gfxContext.setTextureParameters(params);
}
{
Context::FrameBufferRenderTarget bufTarget;
bufTarget.bufferHandle = ObjectHandle(m_ColorBufferFBO);
bufTarget.bufferTarget = bufferTarget::DRAW_FRAMEBUFFER;
bufTarget.attachment = bufferAttachment::COLOR_ATTACHMENT0;
bufTarget.textureTarget = Context::EglImageFramebuffer ? textureTarget::TEXTURE_EXTERNAL : textureTarget::TEXTURE_2D;
bufTarget.textureHandle = m_pColorBufferTexture->name;
gfxContext.addFrameBufferRenderTarget(bufTarget);
}
// check if everything is OK
assert(!gfxContext.isFramebufferError());
gfxContext.bindFramebuffer(graphics::bufferTarget::DRAW_FRAMEBUFFER, graphics::ObjectHandle::defaultFramebuffer);
}
void FrameBuffer::_destroyColorFBTexure()
{
m_bufferReader.reset();
if (m_pColorBufferTexture != nullptr) {
textureCache().removeFrameBufferTexture(m_pColorBufferTexture);
m_pColorBufferTexture = nullptr;
}
if (m_ColorBufferFBO.isNotNull()) {
gfxContext.deleteFramebuffer(m_ColorBufferFBO);
m_ColorBufferFBO.reset();
}
}
FrameBufferList & FrameBufferList::get()
{
static FrameBufferList frameBufferList;
return frameBufferList;
}
void FrameBufferList::init()
{
m_pCurrent = nullptr;
m_pCopy = nullptr;
gfxContext.bindFramebuffer(bufferTarget::DRAW_FRAMEBUFFER, ObjectHandle::defaultFramebuffer);
m_prevColorImageHeight = 0;
m_overscan.init();
m_rdpUpdate.init();
}
void FrameBufferList::destroy() {
gfxContext.bindFramebuffer(bufferTarget::FRAMEBUFFER, ObjectHandle::defaultFramebuffer);
m_list.clear();
m_pCurrent = nullptr;
m_pCopy = nullptr;
m_overscan.destroy();
}
void FrameBufferList::setBufferChanged(f32 _maxY)
{
gDP.colorImage.changed = TRUE;
gDP.colorImage.height = max(gDP.colorImage.height, static_cast<u32>(_maxY));
gDP.colorImage.height = min(gDP.colorImage.height, static_cast<u32>(gDP.scissor.lry));
if (m_pCurrent != nullptr) {
if (m_pCurrent->m_isMainBuffer)
m_pCurrent->m_height = max(m_pCurrent->m_height, min(gDP.colorImage.height, VI.height));
else
m_pCurrent->m_height = max(m_pCurrent->m_height, gDP.colorImage.height);
m_pCurrent->m_cfb = false;
m_pCurrent->m_changed = true;
m_pCurrent->m_copiedToRdram = false;
}
}
void FrameBufferList::clearBuffersChanged()
{
gDP.colorImage.changed = FALSE;
FrameBuffer * pBuffer = frameBufferList().findBuffer(*REG.VI_ORIGIN & 0xffffff);
if (pBuffer != nullptr)
pBuffer->m_changed = false;
}
void FrameBufferList::setCurrentDrawBuffer() const
{
if (m_pCurrent != nullptr)
gfxContext.bindFramebuffer(bufferTarget::DRAW_FRAMEBUFFER, m_pCurrent->m_FBO);
else if (!m_list.empty())
gfxContext.bindFramebuffer(bufferTarget::DRAW_FRAMEBUFFER, m_list.back().m_FBO);
}
FrameBuffer * FrameBufferList::findBuffer(u32 _startAddress)
{
for (auto iter = m_list.begin(); iter != m_list.end(); ++iter) {
if (iter->m_startAddress <= _startAddress && iter->m_endAddress >= _startAddress) // [ { ]
return &(*iter);
}
return nullptr;
}
FrameBuffer * FrameBufferList::getBuffer(u32 _startAddress)
{
for (auto iter = m_list.begin(); iter != m_list.end(); ++iter) {
if (iter->m_startAddress == _startAddress)
return &(*iter);
}
return nullptr;
}
inline
bool isOverlapping(const FrameBuffer * _buf1, const FrameBuffer * _buf2)
{
if (_buf1->m_endAddress < _buf2->m_endAddress && _buf1->m_width == _buf2->m_width && _buf1->m_size == _buf2->m_size) {
const u32 diff = _buf1->m_endAddress - _buf2->m_startAddress + 1;
const u32 stride = _buf1->m_width << _buf1->m_size >> 1;
if ((diff % stride == 0) && (diff / stride < 5))
return true;
else
return false;
}
return false;
}
void FrameBufferList::removeIntersections()
{
assert(!m_list.empty());
FrameBuffers::iterator iter = m_list.end();
do {
--iter;
if (&(*iter) == m_pCurrent)
continue;
if (iter->m_startAddress <= m_pCurrent->m_startAddress && iter->m_endAddress >= m_pCurrent->m_startAddress) { // [ { ]
if (isOverlapping(&(*iter), m_pCurrent)) {
iter->m_endAddress = m_pCurrent->m_startAddress - 1;
continue;
}
iter = m_list.erase(iter);
} else if (m_pCurrent->m_startAddress <= iter->m_startAddress && m_pCurrent->m_endAddress >= iter->m_startAddress) { // { [ }
if (isOverlapping(m_pCurrent, &(*iter))) {
m_pCurrent->m_endAddress = iter->m_startAddress - 1;
continue;
}
iter = m_list.erase(iter);
}
} while (iter != m_list.begin());
}
FrameBuffer * FrameBufferList::findTmpBuffer(u32 _address)
{
for (auto iter = m_list.begin(); iter != m_list.end(); ++iter)
if (iter->m_startAddress > _address || iter->m_endAddress < _address)
return &(*iter);
return nullptr;
}
void FrameBufferList::updateCurrentBufferEndAddress()
{
if (m_pCurrent == nullptr)
return;
m_pCurrent->updateEndAddress();
removeIntersections();
}
void FrameBufferList::_createScreenSizeBuffer()
{
if (VI.height == 0)
return;
m_list.emplace_front();
FrameBuffer & buffer = m_list.front();
buffer.init(VI.width * 2, G_IM_FMT_RGBA, G_IM_SIZ_16b, static_cast<u16>(VI.width), false);
}
void FrameBufferList::saveBuffer(u32 _address, u16 _format, u16 _size, u16 _width, bool _cfb)
{
if (_width > 640)
return;
if (_width == 512 && (config.generalEmulation.hacks & hack_RE2) != 0)
_width = static_cast<u16>(*REG.VI_WIDTH);
if (config.frameBufferEmulation.enable == 0) {
if (m_list.empty())
_createScreenSizeBuffer();
return;
}
if (m_pCurrent != nullptr &&
config.frameBufferEmulation.copyAuxToRDRAM != 0 &&
(config.generalEmulation.hacks & hack_Snap) == 0) {
if (m_pCurrent->isAuxiliary()) {
FrameBuffer_CopyToRDRAM(m_pCurrent->m_startAddress, true);
removeBuffer(m_pCurrent->m_startAddress);
}
}
DisplayWindow & wnd = dwnd();
bool bPrevIsDepth = false;
if (m_pCurrent != nullptr) {
bPrevIsDepth = m_pCurrent->m_isDepthBuffer;
m_pCurrent->m_readable = true;
m_pCurrent->updateEndAddress();
if (!m_pCurrent->m_isDepthBuffer &&
!m_pCurrent->m_copiedToRdram &&
!m_pCurrent->m_cfb &&
!m_pCurrent->m_cleared &&
m_pCurrent->m_RdramCopy.empty() &&
m_pCurrent->m_height > 1) {
m_pCurrent->copyRdram();
}
removeIntersections();
}
const float scaleX = config.frameBufferEmulation.nativeResFactor == 0 ?
wnd.getScaleX() :
static_cast<float>(config.frameBufferEmulation.nativeResFactor);
if (m_pCurrent == nullptr || m_pCurrent->m_startAddress != _address || m_pCurrent->m_width != _width)
m_pCurrent = findBuffer(_address);
auto isSubBuffer = [_address, _width, _size, &wnd](const FrameBuffer * _pBuffer) -> bool
{
if (_pBuffer->m_swapCount == wnd.getBuffersSwapCount() &&
!_pBuffer->m_cfb &&
_pBuffer->m_width == _width &&
_pBuffer->m_size == _size)
{
const u32 stride = _width << _size >> 1;
const u32 diffFromStart = _address - _pBuffer->m_startAddress;
if (diffFromStart % stride != 0)
return true;
const u32 diffFromEnd = _pBuffer->m_endAddress - _address + 1;
if ((diffFromEnd / stride > 5))
return true;
}
return false;
};
auto isOverlappingBuffer = [_address, _width, _size](const FrameBuffer * _pBuffer) -> bool
{
if (_pBuffer->m_width == _width && _pBuffer->m_size == _size) {
const u32 stride = _width << _size >> 1;
const u32 diffEnd = _pBuffer->m_endAddress - _address + 1;
if ((diffEnd / stride < 5))
return true;
}
return false;
};
if (m_pCurrent != nullptr) {
m_pCurrent->m_originX = m_pCurrent->m_originY = 0;
if ((m_pCurrent->m_startAddress != _address)) {
if (isSubBuffer(m_pCurrent)) {
const u32 stride = _width << _size >> 1;
const u32 addrOffset = _address - m_pCurrent->m_startAddress;
m_pCurrent->m_originX = (addrOffset % stride) >> (_size - 1);
m_pCurrent->m_originY = addrOffset / stride;
gSP.changed |= CHANGED_VIEWPORT;
gDP.changed |= CHANGED_SCISSOR;
return;
} else if (isOverlappingBuffer(m_pCurrent)) {
m_pCurrent->m_endAddress = _address - 1;
m_pCurrent = nullptr;
} else {
removeBuffer(m_pCurrent->m_startAddress);
m_pCurrent = nullptr;
}
} else if ((m_pCurrent->m_width != _width) ||
(m_pCurrent->m_size < _size) ||
(m_pCurrent->m_scale != scaleX)) {
removeBuffer(m_pCurrent->m_startAddress);
m_pCurrent = nullptr;
} else {
m_pCurrent->m_resolved = false;
gfxContext.bindFramebuffer(bufferTarget::FRAMEBUFFER, m_pCurrent->m_FBO);
if (m_pCurrent->m_size != _size) {
f32 fillColor[4];
gDPGetFillColor(fillColor);
wnd.getDrawer().clearColorBuffer(fillColor);
m_pCurrent->m_size = _size;
m_pCurrent->m_pTexture->format = _format;
m_pCurrent->m_pTexture->size = _size;
if (m_pCurrent->m_pResolveTexture != nullptr) {
m_pCurrent->m_pResolveTexture->format = _format;
m_pCurrent->m_pResolveTexture->size = _size;
}
if (m_pCurrent->m_copiedToRdram)
m_pCurrent->copyRdram();
}
}
}
const bool bNew = m_pCurrent == nullptr;
if (bNew) {
// Wasn't found or removed, create a new one
m_list.emplace_front();
FrameBuffer & buffer = m_list.front();
buffer.init(_address, _format, _size, _width, _cfb);
m_pCurrent = &buffer;
RDRAMtoColorBuffer::get().copyFromRDRAM(m_pCurrent);
if (_cfb)
m_pCurrent->copyRdram();
}
if (_address == gDP.depthImageAddress)
depthBufferList().saveBuffer(_address);
else
attachDepthBuffer();
DebugMsg( DEBUG_NORMAL, "FrameBuffer_SaveBuffer( 0x%08X )\n", _address);
if (m_pCurrent->isAuxiliary() &&
m_pCurrent->m_pDepthBuffer != nullptr &&
bPrevIsDepth &&
(config.generalEmulation.hacks&hack_LoadDepthTextures) == 0) {
// N64 games may use partial depth buffer clear for aux buffers
// It will not work for GL, so we have to force clear depth buffer for aux buffer
wnd.getDrawer().clearDepthBuffer();
}
if ((config.generalEmulation.hacks & hack_subscreen) != 0u &&
_format == G_IM_FMT_I && _size == G_IM_SIZ_8b)
gDP.m_subscreen = gDP.otherMode._u64 == 0x00000cf00f0a0004;
m_pCurrent->m_isDepthBuffer = _address == gDP.depthImageAddress;
m_pCurrent->m_isPauseScreen = m_pCurrent->m_isOBScreen = false;
m_pCurrent->m_copied = false;
m_pCurrent->m_swapCount = wnd.getBuffersSwapCount();
}
void FrameBufferList::copyAux()
{
for (auto iter = m_list.begin(); iter != m_list.end(); ++iter) {
if (iter->isAuxiliary())
FrameBuffer_CopyToRDRAM(iter->m_startAddress, true);
}
}
void FrameBufferList::removeAux()
{
for (auto iter = m_list.begin(); iter != m_list.end(); ++iter) {
while (iter->isAuxiliary()) {
if (&(*iter) == m_pCurrent) {
m_pCurrent = nullptr;
gfxContext.bindFramebuffer(bufferTarget::DRAW_FRAMEBUFFER, ObjectHandle::defaultFramebuffer);
}
iter = m_list.erase(iter);
if (iter == m_list.end())
return;
}
}
}
void FrameBufferList::removeBuffer(u32 _address )
{
for (auto iter = m_list.begin(); iter != m_list.end(); ++iter)
if (iter->m_startAddress == _address) {
if (&(*iter) == m_pCurrent) {
m_pCurrent = nullptr;
gfxContext.bindFramebuffer(bufferTarget::DRAW_FRAMEBUFFER, ObjectHandle::defaultFramebuffer);
}
m_list.erase(iter);
return;
}
}
void FrameBufferList::removeBuffers(u32 _width)
{
m_pCurrent = nullptr;
for (auto iter = m_list.begin(); iter != m_list.end(); ++iter) {
while (iter->m_width == _width) {
if (&(*iter) == m_pCurrent) {
m_pCurrent = nullptr;
gfxContext.bindFramebuffer(bufferTarget::DRAW_FRAMEBUFFER, ObjectHandle::defaultFramebuffer);
}
iter = m_list.erase(iter);
if (iter == m_list.end())
return;
}
}
}
void FrameBufferList::depthBufferCopyRdram()
{
FrameBuffer * pCurrentDepthBuffer = findBuffer(gDP.depthImageAddress);
if (pCurrentDepthBuffer != nullptr)
pCurrentDepthBuffer->copyRdram();
}
void FrameBufferList::fillBufferInfo(void * _pinfo, u32 _size)
{
FBInfo::FrameBufferInfo* pInfo = reinterpret_cast<FBInfo::FrameBufferInfo*>(_pinfo);
u32 idx = 0;
for (auto iter = m_list.begin(); iter != m_list.end(); ++iter) {
if (iter->m_width == VI.width && !iter->m_cfb && !iter->m_isDepthBuffer) {
pInfo[idx].addr = iter->m_startAddress;
pInfo[idx].width = iter->m_width;
pInfo[idx].height = iter->m_height;
pInfo[idx++].size = iter->m_size;