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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
|
/* $Id$ */
/** @file
* Blitter API implementation
*/
/*
* Copyright (C) 2013 Oracle Corporation
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* you can redistribute it and/or modify it under the terms of the GNU
* General Public License (GPL) as published by the Free Software
* Foundation, in version 2 as it comes in the "COPYING" file of the
* VirtualBox OSE distribution. VirtualBox OSE is distributed in the
* hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
*/
#include "cr_blitter.h"
#include "cr_spu.h"
#include "chromium.h"
#include "cr_error.h"
#include "cr_net.h"
#include "cr_rand.h"
#include "cr_mem.h"
#include "cr_string.h"
#include <iprt/cdefs.h>
#include <iprt/types.h>
#include <iprt/mem.h>
/* @param pCtxBase - contains the blitter context info. Its value is treated differently depending on the fCreateNewCtx value
* @param fCreateNewCtx - if true - the pCtxBase must NOT be NULL. its visualBits is used as a visual bits info for the new context,
* its id field is used to specified the shared context id to be used for blitter context.
* The id can be null to specify no shared context is needed
* if false - if pCtxBase is NOT null AND its id field is NOT null -
* specified the blitter context to be used
* blitter treats it as if it has default ogl state.
* otherwise -
* the blitter works in a "no-context" mode, i.e. a caller is responsible
* to making a proper context current before calling the blitter.
* Note that BltEnter/Leave MUST still be called, but the proper context
* must be set before doing BltEnter, and ResoreContext info is ignored in that case.
* Also note that blitter caches the current window info, and assumes the current context's values are preserved
* wrt that window before the calls, so if one uses different contexts for one blitter,
* the blitter current window values must be explicitly reset by doing CrBltMuralSetCurrentInfo(pBlitter, NULL)
* @param fForceDrawBlt - if true - forces the blitter to always use glDrawXxx-based blits even if GL_EXT_framebuffer_blit.
* This is needed because BlitFramebufferEXT is known to be often buggy, and glDrawXxx-based blits appear to be more reliable
*/
VBOXBLITTERDECL(int) CrBltInit(PCR_BLITTER pBlitter, const CR_BLITTER_CONTEXT *pCtxBase, bool fCreateNewCtx, bool fForceDrawBlt, const CR_GLSL_CACHE *pShaders, SPUDispatchTable *pDispatch)
{
if (pCtxBase && pCtxBase->Base.id < 0)
{
crWarning("Default share context not initialized!");
return VERR_INVALID_PARAMETER;
}
if (!pCtxBase && fCreateNewCtx)
{
crWarning("pCtxBase is zero while fCreateNewCtx is set!");
return VERR_INVALID_PARAMETER;
}
memset(pBlitter, 0, sizeof (*pBlitter));
pBlitter->pDispatch = pDispatch;
if (pCtxBase)
pBlitter->CtxInfo = *pCtxBase;
pBlitter->Flags.ForceDrawBlit = fForceDrawBlt;
if (fCreateNewCtx)
{
pBlitter->CtxInfo.Base.id = pDispatch->CreateContext("", pCtxBase->Base.visualBits, pCtxBase->Base.id);
if (!pBlitter->CtxInfo.Base.id)
{
memset(pBlitter, 0, sizeof (*pBlitter));
crWarning("CreateContext failed!");
return VERR_GENERAL_FAILURE;
}
pBlitter->Flags.CtxCreated = 1;
}
if (pShaders)
{
pBlitter->pGlslCache = pShaders;
pBlitter->Flags.ShadersGloal = 1;
}
else
{
CrGlslInit(&pBlitter->LocalGlslCache, pDispatch);
pBlitter->pGlslCache = &pBlitter->LocalGlslCache;
}
return VINF_SUCCESS;
}
VBOXBLITTERDECL(int) CrBltCleanup(PCR_BLITTER pBlitter)
{
if (CrBltIsEntered(pBlitter))
{
WARN(("CrBltBlitTexTex: blitter is entered"));
return VERR_INVALID_STATE;
}
if (pBlitter->Flags.ShadersGloal || !CrGlslNeedsCleanup(&pBlitter->LocalGlslCache))
return VINF_SUCCESS;
int rc = CrBltEnter(pBlitter);
if (!RT_SUCCESS(rc))
{
WARN(("CrBltEnter failed, rc %d", rc));
return rc;
}
CrGlslCleanup(&pBlitter->LocalGlslCache);
CrBltLeave(pBlitter);
return VINF_SUCCESS;
}
void CrBltTerm(PCR_BLITTER pBlitter)
{
if (pBlitter->Flags.CtxCreated)
pBlitter->pDispatch->DestroyContext(pBlitter->CtxInfo.Base.id);
memset(pBlitter, 0, sizeof (*pBlitter));
}
int CrBltMuralSetCurrentInfo(PCR_BLITTER pBlitter, const CR_BLITTER_WINDOW *pMural)
{
if (pMural)
{
if (!memcmp(&pBlitter->CurrentMural, pMural, sizeof (pBlitter->CurrentMural)))
return VINF_SUCCESS;
memcpy(&pBlitter->CurrentMural, pMural, sizeof (pBlitter->CurrentMural));
}
else
{
if (CrBltIsEntered(pBlitter))
{
WARN(("can not set null mural for entered bleater"));
return VERR_INVALID_STATE;
}
if (!pBlitter->CurrentMural.Base.id)
return VINF_SUCCESS;
pBlitter->CurrentMural.Base.id = 0;
}
pBlitter->Flags.CurrentMuralChanged = 1;
if (!CrBltIsEntered(pBlitter))
return VINF_SUCCESS;
else if (!pBlitter->CtxInfo.Base.id)
{
WARN(("setting current mural for entered no-context blitter"));
return VERR_INVALID_STATE;
}
WARN(("changing mural for entered blitter, is is somewhat expected?"));
pBlitter->pDispatch->Flush();
pBlitter->pDispatch->MakeCurrent(pMural->Base.id, pBlitter->i32MakeCurrentUserData, pBlitter->CtxInfo.Base.id);
return VINF_SUCCESS;
}
static DECLCALLBACK(int) crBltBlitTexBufImplFbo(PCR_BLITTER pBlitter, const VBOXVR_TEXTURE *pSrc, const RTRECT *paSrcRect, const RTRECTSIZE *pDstSize, const RTRECT *paDstRect, uint32_t cRects, uint32_t fFlags)
{
GLenum filter = CRBLT_FILTER_FROM_FLAGS(fFlags);
pBlitter->pDispatch->BindFramebufferEXT(GL_READ_FRAMEBUFFER, pBlitter->idFBO);
pBlitter->pDispatch->FramebufferTexture2DEXT(GL_READ_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, pSrc->target, pSrc->hwid, 0);
pBlitter->pDispatch->ReadBuffer(GL_COLOR_ATTACHMENT0);
for (uint32_t i = 0; i < cRects; ++i)
{
const RTRECT * pSrcRect = &paSrcRect[i];
const RTRECT * pDstRect = &paDstRect[i];
int32_t srcY1;
int32_t srcY2;
int32_t dstY1;
int32_t dstY2;
int32_t srcX1 = pSrcRect->xLeft;
int32_t srcX2 = pSrcRect->xRight;
int32_t dstX1 = pDstRect->xLeft;
int32_t dstX2 = pDstRect->xRight;
if (CRBLT_F_INVERT_SRC_YCOORDS & fFlags)
{
srcY1 = pSrc->height - pSrcRect->yTop;
srcY2 = pSrc->height - pSrcRect->yBottom;
}
else
{
srcY1 = pSrcRect->yTop;
srcY2 = pSrcRect->yBottom;
}
if (CRBLT_F_INVERT_DST_YCOORDS & fFlags)
{
dstY1 = pDstSize->cy - pDstRect->yTop;
dstY2 = pDstSize->cy - pDstRect->yBottom;
}
else
{
dstY1 = pDstRect->yTop;
dstY2 = pDstRect->yBottom;
}
if (srcY1 > srcY2)
{
if (dstY1 > dstY2)
{
/* use srcY1 < srcY2 && dstY1 < dstY2 whenever possible to avoid GPU driver bugs */
int32_t tmp = srcY1;
srcY1 = srcY2;
srcY2 = tmp;
tmp = dstY1;
dstY1 = dstY2;
dstY2 = tmp;
}
}
if (srcX1 > srcX2)
{
if (dstX1 > dstX2)
{
/* use srcX1 < srcX2 && dstX1 < dstX2 whenever possible to avoid GPU driver bugs */
int32_t tmp = srcX1;
srcX1 = srcX2;
srcX2 = tmp;
tmp = dstX1;
dstX1 = dstX2;
dstX2 = tmp;
}
}
pBlitter->pDispatch->BlitFramebufferEXT(srcX1, srcY1, srcX2, srcY2,
dstX1, dstY1, dstX2, dstY2,
GL_COLOR_BUFFER_BIT, filter);
}
return VINF_SUCCESS;
}
/* GL_TRIANGLE_FAN */
DECLINLINE(GLfloat*) crBltVtRectTFNormalized(const RTRECT *pRect, uint32_t normalX, uint32_t normalY, GLfloat* pBuff, uint32_t height)
{
/* going ccw:
* 1. (left;top) 4. (right;top)
* | ^
* > |
* 2. (left;bottom) -> 3. (right;bottom) */
/* xLeft yTop */
pBuff[0] = ((float)pRect->xLeft)/((float)normalX);
pBuff[1] = ((float)(height ? height - pRect->yTop : pRect->yTop))/((float)normalY);
/* xLeft yBottom */
pBuff[2] = pBuff[0];
pBuff[3] = ((float)(height ? height - pRect->yBottom : pRect->yBottom))/((float)normalY);
/* xRight yBottom */
pBuff[4] = ((float)pRect->xRight)/((float)normalX);
pBuff[5] = pBuff[3];
/* xRight yTop */
pBuff[6] = pBuff[4];
pBuff[7] = pBuff[1];
return &pBuff[8];
}
DECLINLINE(GLfloat*) crBltVtRectsTFNormalized(const RTRECT *paRects, uint32_t cRects, uint32_t normalX, uint32_t normalY, GLfloat* pBuff, uint32_t height)
{
for (uint32_t i = 0; i < cRects; ++i)
{
pBuff = crBltVtRectTFNormalized(&paRects[i], normalX, normalY, pBuff, height);
}
return pBuff;
}
DECLINLINE(GLint*) crBltVtRectTF(const RTRECT *pRect, uint32_t normalX, uint32_t normalY, GLint* pBuff, uint32_t height)
{
/* xLeft yTop */
pBuff[0] = pRect->xLeft;
pBuff[1] = height ? height - pRect->yTop : pRect->yTop;
/* xLeft yBottom */
pBuff[2] = pBuff[0];
pBuff[3] = height ? height - pRect->yBottom : pRect->yBottom;
/* xRight yBottom */
pBuff[4] = pRect->xRight;
pBuff[5] = pBuff[3];
/* xRight yTop */
pBuff[6] = pBuff[4];
pBuff[7] = pBuff[1];
return &pBuff[8];
}
DECLINLINE(GLubyte*) crBltVtFillRectIndicies(GLubyte *pIndex, GLubyte *piBase)
{
GLubyte iBase = *piBase;
/* triangle 1 */
pIndex[0] = iBase;
pIndex[1] = iBase + 1;
pIndex[2] = iBase + 2;
/* triangle 2 */
pIndex[3] = iBase;
pIndex[4] = iBase + 2;
pIndex[5] = iBase + 3;
*piBase = iBase + 4;
return pIndex + 6;
}
/* Indexed GL_TRIANGLES */
DECLINLINE(GLfloat*) crBltVtRectITNormalized(const RTRECT *pRect, uint32_t normalX, uint32_t normalY, GLfloat* pBuff, uint32_t height)
{
GLfloat* ret = crBltVtRectTFNormalized(pRect, normalX, normalY, pBuff, height);
return ret;
}
DECLINLINE(GLint*) crBltVtRectIT(RTRECT *pRect, uint32_t normalX, uint32_t normalY, GLint* pBuff, GLubyte **ppIndex, GLubyte *piBase, uint32_t height)
{
GLint* ret = crBltVtRectTF(pRect, normalX, normalY, pBuff, height);
if (ppIndex)
*ppIndex = crBltVtFillRectIndicies(*ppIndex, piBase);
return ret;
}
DECLINLINE(GLuint) crBltVtGetNumVerticiesTF(GLuint cRects)
{
return cRects * 4;
}
#define crBltVtGetNumVerticiesIT crBltVtGetNumVerticiesTF
DECLINLINE(GLuint) crBltVtGetNumIndiciesIT(GLuint cRects)
{
return 6 * cRects;
}
static GLfloat* crBltVtRectsITNormalized(const RTRECT *paRects, uint32_t cRects, uint32_t normalX, uint32_t normalY, GLfloat* pBuff, GLubyte **ppIndex, GLubyte *piBase, uint32_t height)
{
uint32_t i;
for (i = 0; i < cRects; ++i)
{
pBuff = crBltVtRectITNormalized(&paRects[i], normalX, normalY, pBuff, height);
}
if (ppIndex)
{
GLubyte *pIndex = (GLubyte*)pBuff;
*ppIndex = pIndex;
for (i = 0; i < cRects; ++i)
{
pIndex = crBltVtFillRectIndicies(pIndex, piBase);
}
pBuff = (GLfloat*)pIndex;
}
return pBuff;
}
static void* crBltBufGet(PCR_BLITTER_BUFFER pBuffer, GLuint cbBuffer)
{
if (pBuffer->cbBuffer < cbBuffer)
{
if (pBuffer->pvBuffer)
{
RTMemFree(pBuffer->pvBuffer);
}
#ifndef DEBUG_misha
/* debugging: ensure we calculate proper buffer size */
cbBuffer += 16;
#endif
pBuffer->pvBuffer = RTMemAlloc(cbBuffer);
if (pBuffer->pvBuffer)
pBuffer->cbBuffer = cbBuffer;
else
{
crWarning("failed to allocate buffer of size %d", cbBuffer);
pBuffer->cbBuffer = 0;
}
}
return pBuffer->pvBuffer;
}
static void crBltCheckSetupViewport(PCR_BLITTER pBlitter, const RTRECTSIZE *pDstSize, bool fFBODraw)
{
bool fUpdateViewport = pBlitter->Flags.CurrentMuralChanged;
if (pBlitter->CurrentSetSize.cx != pDstSize->cx
|| pBlitter->CurrentSetSize.cy != pDstSize->cy)
{
pBlitter->CurrentSetSize = *pDstSize;
pBlitter->pDispatch->MatrixMode(GL_PROJECTION);
pBlitter->pDispatch->LoadIdentity();
pBlitter->pDispatch->Ortho(0, pDstSize->cx, 0, pDstSize->cy, -1, 1);
fUpdateViewport = true;
}
if (fUpdateViewport)
{
pBlitter->pDispatch->Viewport(0, 0, pBlitter->CurrentSetSize.cx, pBlitter->CurrentSetSize.cy);
pBlitter->Flags.CurrentMuralChanged = 0;
}
pBlitter->Flags.LastWasFBODraw = fFBODraw;
}
static DECLCALLBACK(int) crBltBlitTexBufImplDraw2D(PCR_BLITTER pBlitter, const VBOXVR_TEXTURE *pSrc, const RTRECT *paSrcRect, const RTRECTSIZE *pDstSize, const RTRECT *paDstRect, uint32_t cRects, uint32_t fFlags)
{
GLuint normalX, normalY;
uint32_t srcHeight = (fFlags & CRBLT_F_INVERT_SRC_YCOORDS) ? pSrc->height : 0;
uint32_t dstHeight = (fFlags & CRBLT_F_INVERT_DST_YCOORDS) ? pDstSize->cy : 0;
switch (pSrc->target)
{
case GL_TEXTURE_2D:
{
normalX = pSrc->width;
normalY = pSrc->height;
break;
}
case GL_TEXTURE_RECTANGLE_ARB:
{
normalX = 1;
normalY = 1;
break;
}
default:
{
crWarning("Unsupported texture target 0x%x", pSrc->target);
return VERR_INVALID_PARAMETER;
}
}
Assert(pSrc->hwid);
pBlitter->pDispatch->BindTexture(pSrc->target, pSrc->hwid);
if (cRects == 1)
{
/* just optimization to draw a single rect with GL_TRIANGLE_FAN */
GLfloat *pVerticies;
GLfloat *pTexCoords;
GLuint cElements = crBltVtGetNumVerticiesTF(cRects);
pVerticies = (GLfloat*)crBltBufGet(&pBlitter->Verticies, cElements * 2 * 2 * sizeof (*pVerticies));
pTexCoords = crBltVtRectsTFNormalized(paDstRect, cRects, 1, 1, pVerticies, dstHeight);
crBltVtRectsTFNormalized(paSrcRect, cRects, normalX, normalY, pTexCoords, srcHeight);
pBlitter->pDispatch->EnableClientState(GL_VERTEX_ARRAY);
pBlitter->pDispatch->VertexPointer(2, GL_FLOAT, 0, pVerticies);
pBlitter->pDispatch->EnableClientState(GL_TEXTURE_COORD_ARRAY);
pBlitter->pDispatch->TexCoordPointer(2, GL_FLOAT, 0, pTexCoords);
pBlitter->pDispatch->Enable(pSrc->target);
pBlitter->pDispatch->DrawArrays(GL_TRIANGLE_FAN, 0, cElements);
pBlitter->pDispatch->Disable(pSrc->target);
pBlitter->pDispatch->DisableClientState(GL_TEXTURE_COORD_ARRAY);
pBlitter->pDispatch->DisableClientState(GL_VERTEX_ARRAY);
}
else
{
GLfloat *pVerticies;
GLfloat *pTexCoords;
GLubyte *pIndicies;
GLuint cElements = crBltVtGetNumVerticiesIT(cRects);
GLuint cIndicies = crBltVtGetNumIndiciesIT(cRects);
GLubyte iIdxBase = 0;
pVerticies = (GLfloat*)crBltBufGet(&pBlitter->Verticies, cElements * 2 * 2 * sizeof (*pVerticies) + cIndicies * sizeof (*pIndicies));
pTexCoords = crBltVtRectsITNormalized(paDstRect, cRects, 1, 1, pVerticies, &pIndicies, &iIdxBase, dstHeight);
crBltVtRectsITNormalized(paSrcRect, cRects, normalX, normalY, pTexCoords, NULL, NULL, srcHeight);
pBlitter->pDispatch->EnableClientState(GL_VERTEX_ARRAY);
pBlitter->pDispatch->VertexPointer(2, GL_FLOAT, 0, pVerticies);
pBlitter->pDispatch->EnableClientState(GL_TEXTURE_COORD_ARRAY);
pBlitter->pDispatch->TexCoordPointer(2, GL_FLOAT, 0, pTexCoords);
pBlitter->pDispatch->Enable(pSrc->target);
pBlitter->pDispatch->DrawElements(GL_TRIANGLES, cIndicies, GL_UNSIGNED_BYTE, pIndicies);
pBlitter->pDispatch->Disable(pSrc->target);
pBlitter->pDispatch->DisableClientState(GL_TEXTURE_COORD_ARRAY);
pBlitter->pDispatch->DisableClientState(GL_VERTEX_ARRAY);
}
pBlitter->pDispatch->BindTexture(pSrc->target, 0);
return VINF_SUCCESS;
}
static int crBltInitOnMakeCurent(PCR_BLITTER pBlitter)
{
const char * pszExtension = (const char*)pBlitter->pDispatch->GetString(GL_EXTENSIONS);
if (crStrstr(pszExtension, "GL_EXT_framebuffer_object"))
{
pBlitter->Flags.SupportsFBO = 1;
pBlitter->pDispatch->GenFramebuffersEXT(1, &pBlitter->idFBO);
Assert(pBlitter->idFBO);
}
else
crWarning("GL_EXT_framebuffer_object not supported, blitter can only blit to window");
if (crStrstr(pszExtension, "GL_ARB_pixel_buffer_object"))
pBlitter->Flags.SupportsPBO = 1;
else
crWarning("GL_ARB_pixel_buffer_object not supported");
/* BlitFramebuffer seems to be buggy on Intel,
* try always glDrawXxx for now */
if (!pBlitter->Flags.ForceDrawBlit && crStrstr(pszExtension, "GL_EXT_framebuffer_blit"))
{
pBlitter->pfnBlt = crBltBlitTexBufImplFbo;
}
else
{
// crWarning("GL_EXT_framebuffer_blit not supported, will use Draw functions for blitting, which might be less efficient");
pBlitter->pfnBlt = crBltBlitTexBufImplDraw2D;
}
/* defaults. but just in case */
pBlitter->pDispatch->MatrixMode(GL_TEXTURE);
pBlitter->pDispatch->LoadIdentity();
pBlitter->pDispatch->MatrixMode(GL_MODELVIEW);
pBlitter->pDispatch->LoadIdentity();
return VINF_SUCCESS;
}
void CrBltLeave(PCR_BLITTER pBlitter)
{
if (!pBlitter->cEnters)
{
WARN(("blitter not entered!"));
return;
}
if (--pBlitter->cEnters)
return;
if (pBlitter->Flags.SupportsFBO)
{
pBlitter->pDispatch->BindFramebufferEXT(GL_FRAMEBUFFER, 0);
pBlitter->pDispatch->DrawBuffer(GL_BACK);
pBlitter->pDispatch->ReadBuffer(GL_BACK);
}
pBlitter->pDispatch->Flush();
if (pBlitter->CtxInfo.Base.id)
pBlitter->pDispatch->MakeCurrent(0, 0, 0);
}
int CrBltEnter(PCR_BLITTER pBlitter)
{
if (!pBlitter->CurrentMural.Base.id && pBlitter->CtxInfo.Base.id)
{
WARN(("current mural not initialized!"));
return VERR_INVALID_STATE;
}
if (pBlitter->cEnters++)
return VINF_SUCCESS;
if (pBlitter->CurrentMural.Base.id) /* <- pBlitter->CurrentMural.Base.id can be null if the blitter is in a "no-context" mode (see comments to BltInit for detail)*/
{
pBlitter->pDispatch->MakeCurrent(pBlitter->CurrentMural.Base.id, pBlitter->i32MakeCurrentUserData, pBlitter->CtxInfo.Base.id);
}
if (pBlitter->Flags.Initialized)
return VINF_SUCCESS;
int rc = crBltInitOnMakeCurent(pBlitter);
if (RT_SUCCESS(rc))
{
pBlitter->Flags.Initialized = 1;
return VINF_SUCCESS;
}
WARN(("crBltInitOnMakeCurent failed, rc %d", rc));
CrBltLeave(pBlitter);
return rc;
}
static void crBltBlitTexBuf(PCR_BLITTER pBlitter, const VBOXVR_TEXTURE *pSrc, const RTRECT *paSrcRects, GLenum enmDstBuff, const RTRECTSIZE *pDstSize, const RTRECT *paDstRects, uint32_t cRects, uint32_t fFlags)
{
pBlitter->pDispatch->DrawBuffer(enmDstBuff);
crBltCheckSetupViewport(pBlitter, pDstSize, enmDstBuff == GL_DRAW_FRAMEBUFFER);
if (!(fFlags & CRBLT_F_NOALPHA))
pBlitter->pfnBlt(pBlitter, pSrc, paSrcRects, pDstSize, paDstRects, cRects, fFlags);
else
{
int rc = pBlitter->Flags.ShadersGloal ?
CrGlslProgUseNoAlpha(pBlitter->pGlslCache, pSrc->target)
:
CrGlslProgUseGenNoAlpha(&pBlitter->LocalGlslCache, pSrc->target);
if (!RT_SUCCESS(rc))
{
crWarning("Failed to use no-alpha program rc %d!, falling back to default blit", rc);
pBlitter->pfnBlt(pBlitter, pSrc, paSrcRects, pDstSize, paDstRects, cRects, fFlags);
return;
}
/* since we use shaders, we need to use draw commands rather than framebuffer blits.
* force using draw-based blitting */
crBltBlitTexBufImplDraw2D(pBlitter, pSrc, paSrcRects, pDstSize, paDstRects, cRects, fFlags);
Assert(pBlitter->Flags.ShadersGloal || &pBlitter->LocalGlslCache == pBlitter->pGlslCache);
CrGlslProgClear(pBlitter->pGlslCache);
}
}
void CrBltCheckUpdateViewport(PCR_BLITTER pBlitter)
{
RTRECTSIZE DstSize = {pBlitter->CurrentMural.width, pBlitter->CurrentMural.height};
crBltCheckSetupViewport(pBlitter, &DstSize, false);
}
void CrBltBlitTexMural(PCR_BLITTER pBlitter, bool fBb, const VBOXVR_TEXTURE *pSrc, const RTRECT *paSrcRects, const RTRECT *paDstRects, uint32_t cRects, uint32_t fFlags)
{
if (!CrBltIsEntered(pBlitter))
{
WARN(("CrBltBlitTexMural: blitter not entered"));
return;
}
RTRECTSIZE DstSize = {pBlitter->CurrentMural.width, pBlitter->CurrentMural.height};
pBlitter->pDispatch->BindFramebufferEXT(GL_DRAW_FRAMEBUFFER, 0);
crBltBlitTexBuf(pBlitter, pSrc, paSrcRects, fBb ? GL_BACK : GL_FRONT, &DstSize, paDstRects, cRects, fFlags);
}
void CrBltBlitTexTex(PCR_BLITTER pBlitter, const VBOXVR_TEXTURE *pSrc, const RTRECT *pSrcRect, const VBOXVR_TEXTURE *pDst, const RTRECT *pDstRect, uint32_t cRects, uint32_t fFlags)
{
if (!CrBltIsEntered(pBlitter))
{
WARN(("CrBltBlitTexTex: blitter not entered"));
return;
}
RTRECTSIZE DstSize = {(uint32_t)pDst->width, (uint32_t)pDst->height};
pBlitter->pDispatch->BindFramebufferEXT(GL_DRAW_FRAMEBUFFER, pBlitter->idFBO);
/* TODO: mag/min filters ? */
pBlitter->pDispatch->FramebufferTexture2DEXT(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, pDst->target, pDst->hwid, 0);
// pBlitter->pDispatch->FramebufferTexture2DEXT(GL_DRAW_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, 0, 0);
// pBlitter->pDispatch->FramebufferTexture2DEXT(GL_DRAW_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_TEXTURE_2D, 0, 0);
crBltBlitTexBuf(pBlitter, pSrc, pSrcRect, GL_DRAW_FRAMEBUFFER, &DstSize, pDstRect, cRects, fFlags);
pBlitter->pDispatch->FramebufferTexture2DEXT(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, pDst->target, 0, 0);
}
void CrBltPresent(PCR_BLITTER pBlitter)
{
if (!CrBltIsEntered(pBlitter))
{
WARN(("CrBltPresent: blitter not entered"));
return;
}
if (pBlitter->CtxInfo.Base.visualBits & CR_DOUBLE_BIT)
pBlitter->pDispatch->SwapBuffers(pBlitter->CurrentMural.Base.id, 0);
else
pBlitter->pDispatch->Flush();
}
static int crBltImgInitBaseForTex(const VBOXVR_TEXTURE *pSrc, CR_BLITTER_IMG *pDst, GLenum enmFormat)
{
memset(pDst, 0, sizeof (*pDst));
if (enmFormat != GL_RGBA
&& enmFormat != GL_BGRA)
{
WARN(("unsupported format 0x%x", enmFormat));
return VERR_NOT_IMPLEMENTED;
}
uint32_t bpp = 32;
uint32_t pitch = ((bpp * pSrc->width) + 7) >> 3;
uint32_t cbData = pitch * pSrc->height;
pDst->cbData = cbData;
pDst->enmFormat = enmFormat;
pDst->width = pSrc->width;
pDst->height = pSrc->height;
pDst->bpp = bpp;
pDst->pitch = pitch;
return VINF_SUCCESS;
}
static int crBltImgCreateForTex(const VBOXVR_TEXTURE *pSrc, CR_BLITTER_IMG *pDst, GLenum enmFormat)
{
int rc = crBltImgInitBaseForTex(pSrc, pDst, enmFormat);
if (!RT_SUCCESS(rc))
{
crWarning("crBltImgInitBaseForTex failed rc %d", rc);
return rc;
}
uint32_t cbData = pDst->cbData;
pDst->pvData = RTMemAllocZ(cbData);
if (!pDst->pvData)
{
crWarning("RTMemAlloc failed");
return VERR_NO_MEMORY;
}
#ifdef DEBUG_misha
{
char *pTmp = (char*)pDst->pvData;
for (uint32_t i = 0; i < cbData; ++i)
{
pTmp[i] = (char)((1 << i) % 255);
}
}
#endif
return VINF_SUCCESS;
}
VBOXBLITTERDECL(int) CrBltImgGetTex(PCR_BLITTER pBlitter, const VBOXVR_TEXTURE *pSrc, GLenum enmFormat, CR_BLITTER_IMG *pDst)
{
if (!CrBltIsEntered(pBlitter))
{
WARN(("CrBltImgGetTex: blitter not entered"));
return VERR_INVALID_STATE;
}
int rc = crBltImgCreateForTex(pSrc, pDst, enmFormat);
if (!RT_SUCCESS(rc))
{
crWarning("crBltImgCreateForTex failed, rc %d", rc);
return rc;
}
pBlitter->pDispatch->BindTexture(pSrc->target, pSrc->hwid);
#ifdef DEBUG_misha
{
GLint width = 0, height = 0, depth = 0;
pBlitter->pDispatch->GetTexLevelParameteriv(pSrc->target, 0, GL_TEXTURE_WIDTH, &width);
pBlitter->pDispatch->GetTexLevelParameteriv(pSrc->target, 0, GL_TEXTURE_HEIGHT, &height);
pBlitter->pDispatch->GetTexLevelParameteriv(pSrc->target, 0, GL_TEXTURE_DEPTH, &depth);
Assert(width == pSrc->width);
Assert(height == pSrc->height);
// Assert(depth == pSrc->depth);
}
#endif
pBlitter->pDispatch->GetTexImage(pSrc->target, 0, enmFormat, GL_UNSIGNED_BYTE, pDst->pvData);
pBlitter->pDispatch->BindTexture(pSrc->target, 0);
return VINF_SUCCESS;
}
VBOXBLITTERDECL(int) CrBltImgGetMural(PCR_BLITTER pBlitter, bool fBb, CR_BLITTER_IMG *pDst)
{
if (!CrBltIsEntered(pBlitter))
{
WARN(("CrBltImgGetMural: blitter not entered"));
return VERR_INVALID_STATE;
}
crWarning("NOT IMPLEMENTED");
return VERR_NOT_IMPLEMENTED;
}
VBOXBLITTERDECL(void) CrBltImgFree(PCR_BLITTER pBlitter, CR_BLITTER_IMG *pDst)
{
if (!CrBltIsEntered(pBlitter))
{
WARN(("CrBltImgFree: blitter not entered"));
return;
}
if (pDst->pvData)
{
RTMemFree(pDst->pvData);
pDst->pvData = NULL;
}
}
VBOXBLITTERDECL(bool) CrGlslIsSupported(CR_GLSL_CACHE *pCache)
{
if (pCache->iGlVersion == 0)
{
const char * pszStr = (const char*)pCache->pDispatch->GetString(GL_VERSION);
pCache->iGlVersion = crStrParseGlVersion(pszStr);
if (pCache->iGlVersion <= 0)
{
crWarning("crStrParseGlVersion returned %d", pCache->iGlVersion);
pCache->iGlVersion = -1;
}
}
if (pCache->iGlVersion >= CR_GLVERSION_COMPOSE(2, 0, 0))
return true;
crWarning("GLSL unsuported, gl version %d", pCache->iGlVersion);
/* @todo: we could also check for GL_ARB_shader_objects and GL_ARB_fragment_shader,
* but seems like chromium does not support properly gl*Object versions of shader functions used with those extensions */
return false;
}
#define CR_GLSL_STR_V_120 "#version 120\n"
#define CR_GLSL_STR_EXT_TR "#extension GL_ARB_texture_rectangle : enable\n"
#define CR_GLSL_STR_2D "2D"
#define CR_GLSL_STR_2DRECT "2DRect"
#define CR_GLSL_PATTERN_FS_NOALPHA(_ver, _ext, _tex) \
_ver \
_ext \
"uniform sampler" _tex " sampler0;\n" \
"void main()\n" \
"{\n" \
"vec2 srcCoord = vec2(gl_TexCoord[0]);\n" \
"gl_FragData[0].xyz = (texture" _tex "(sampler0, srcCoord).xyz);\n" \
"gl_FragData[0].w = 1.0;\n" \
"}\n"
static const char* crGlslGetFsStringNoAlpha(CR_GLSL_CACHE *pCache, GLenum enmTexTarget)
{
if (!CrGlslIsSupported(pCache))
{
crWarning("CrGlslIsSupported is false");
return NULL;
}
if (pCache->iGlVersion >= CR_GLVERSION_COMPOSE(2, 1, 0))
{
if (enmTexTarget == GL_TEXTURE_2D)
return CR_GLSL_PATTERN_FS_NOALPHA(CR_GLSL_STR_V_120, "", CR_GLSL_STR_2D);
else if (enmTexTarget == GL_TEXTURE_RECTANGLE_ARB)
return CR_GLSL_PATTERN_FS_NOALPHA(CR_GLSL_STR_V_120, CR_GLSL_STR_EXT_TR, CR_GLSL_STR_2DRECT);
crWarning("invalid enmTexTarget %#x", enmTexTarget);
return NULL;
}
else if (pCache->iGlVersion >= CR_GLVERSION_COMPOSE(2, 0, 0))
{
if (enmTexTarget == GL_TEXTURE_2D)
return CR_GLSL_PATTERN_FS_NOALPHA("", "", CR_GLSL_STR_2D);
else if (enmTexTarget == GL_TEXTURE_RECTANGLE_ARB)
return CR_GLSL_PATTERN_FS_NOALPHA("", CR_GLSL_STR_EXT_TR, CR_GLSL_STR_2DRECT);
crWarning("invalid enmTexTarget %#x", enmTexTarget);
return NULL;
}
crError("crGlslGetFsStringNoAlpha: we should not be here!");
return NULL;
}
static int crGlslProgGenNoAlpha(CR_GLSL_CACHE *pCache, GLenum enmTexTarget, GLuint *puiProgram)
{
*puiProgram = 0;
const char*pStrFsShader = crGlslGetFsStringNoAlpha(pCache, enmTexTarget);
if (!pStrFsShader)
{
crWarning("crGlslGetFsStringNoAlpha failed");
return VERR_NOT_SUPPORTED;
}
int rc = VINF_SUCCESS;
GLchar * pBuf = NULL;
GLuint uiProgram = 0;
GLint iUniform = -1;
GLuint uiShader = pCache->pDispatch->CreateShader(GL_FRAGMENT_SHADER);
if (!uiShader)
{
crWarning("CreateShader failed");
return VERR_NOT_SUPPORTED;
}
pCache->pDispatch->ShaderSource(uiShader, 1, &pStrFsShader, NULL);
pCache->pDispatch->CompileShader(uiShader);
GLint compiled = 0;
pCache->pDispatch->GetShaderiv(uiShader, GL_COMPILE_STATUS, &compiled);
#ifndef DEBUG_misha
if(!compiled)
#endif
{
if (!pBuf)
pBuf = (GLchar *)RTMemAlloc(16300);
pCache->pDispatch->GetShaderInfoLog(uiShader, 16300, NULL, pBuf);
#ifdef DEBUG_misha
if (compiled)
crDebug("compile success:\n-------------------\n%s\n--------\n", pBuf);
else
#endif
{
crWarning("compile FAILURE:\n-------------------\n%s\n--------\n", pBuf);
rc = VERR_NOT_SUPPORTED;
goto end;
}
}
Assert(compiled);
uiProgram = pCache->pDispatch->CreateProgram();
if (!uiProgram)
{
rc = VERR_NOT_SUPPORTED;
goto end;
}
pCache->pDispatch->AttachShader(uiProgram, uiShader);
pCache->pDispatch->LinkProgram(uiProgram);
GLint linked;
pCache->pDispatch->GetProgramiv(uiProgram, GL_LINK_STATUS, &linked);
#ifndef DEBUG_misha
if(!linked)
#endif
{
if (!pBuf)
pBuf = (GLchar *)RTMemAlloc(16300);
pCache->pDispatch->GetProgramInfoLog(uiProgram, 16300, NULL, pBuf);
#ifdef DEBUG_misha
if (linked)
crDebug("link success:\n-------------------\n%s\n--------\n", pBuf);
else
#endif
{
crWarning("link FAILURE:\n-------------------\n%s\n--------\n", pBuf);
rc = VERR_NOT_SUPPORTED;
goto end;
}
}
Assert(linked);
iUniform = pCache->pDispatch->GetUniformLocation(uiProgram, "sampler0");
if (iUniform == -1)
{
crWarning("GetUniformLocation failed for sampler0");
}
else
{
pCache->pDispatch->Uniform1i(iUniform, 0);
}
*puiProgram = uiProgram;
/* avoid end finalizer from cleaning it */
uiProgram = 0;
end:
if (uiShader)
pCache->pDispatch->DeleteShader(uiShader);
if (uiProgram)
pCache->pDispatch->DeleteProgram(uiProgram);
if (pBuf)
RTMemFree(pBuf);
return rc;
}
DECLINLINE(GLuint) crGlslProgGetNoAlpha(const CR_GLSL_CACHE *pCache, GLenum enmTexTarget)
{
switch (enmTexTarget)
{
case GL_TEXTURE_2D:
return pCache->uNoAlpha2DProg;
case GL_TEXTURE_RECTANGLE_ARB:
return pCache->uNoAlpha2DRectProg;
default:
crWarning("invalid tex enmTexTarget %#x", enmTexTarget);
return 0;
}
}
DECLINLINE(GLuint*) crGlslProgGetNoAlphaPtr(CR_GLSL_CACHE *pCache, GLenum enmTexTarget)
{
switch (enmTexTarget)
{
case GL_TEXTURE_2D:
return &pCache->uNoAlpha2DProg;
case GL_TEXTURE_RECTANGLE_ARB:
return &pCache->uNoAlpha2DRectProg;
default:
crWarning("invalid tex enmTexTarget %#x", enmTexTarget);
return NULL;
}
}
VBOXBLITTERDECL(int) CrGlslProgGenNoAlpha(CR_GLSL_CACHE *pCache, GLenum enmTexTarget)
{
GLuint*puiProgram = crGlslProgGetNoAlphaPtr(pCache, enmTexTarget);
if (!puiProgram)
return VERR_INVALID_PARAMETER;
if (*puiProgram)
return VINF_SUCCESS;
return crGlslProgGenNoAlpha(pCache, enmTexTarget, puiProgram);
}
VBOXBLITTERDECL(int) CrGlslProgGenAllNoAlpha(CR_GLSL_CACHE *pCache)
{
int rc = CrGlslProgGenNoAlpha(pCache, GL_TEXTURE_2D);
if (!RT_SUCCESS(rc))
{
crWarning("CrGlslProgGenNoAlpha GL_TEXTURE_2D failed rc %d", rc);
return rc;
}
rc = CrGlslProgGenNoAlpha(pCache, GL_TEXTURE_RECTANGLE_ARB);
if (!RT_SUCCESS(rc))
{
crWarning("CrGlslProgGenNoAlpha GL_TEXTURE_RECTANGLE failed rc %d", rc);
return rc;
}
return VINF_SUCCESS;
}
VBOXBLITTERDECL(void) CrGlslProgClear(const CR_GLSL_CACHE *pCache)
{
pCache->pDispatch->UseProgram(0);
}
VBOXBLITTERDECL(int) CrGlslProgUseNoAlpha(const CR_GLSL_CACHE *pCache, GLenum enmTexTarget)
{
GLuint uiProg = crGlslProgGetNoAlpha(pCache, enmTexTarget);
if (!uiProg)
{
crWarning("request to use inexistent program!");
return VERR_INVALID_STATE;
}
Assert(uiProg);
pCache->pDispatch->UseProgram(uiProg);
return VINF_SUCCESS;
}
VBOXBLITTERDECL(int) CrGlslProgUseGenNoAlpha(CR_GLSL_CACHE *pCache, GLenum enmTexTarget)
{
GLuint uiProg = crGlslProgGetNoAlpha(pCache, enmTexTarget);
if (!uiProg)
{
int rc = CrGlslProgGenNoAlpha(pCache, enmTexTarget);
if (!RT_SUCCESS(rc))
{
crWarning("CrGlslProgGenNoAlpha failed, rc %d", rc);
return rc;
}
uiProg = crGlslProgGetNoAlpha(pCache, enmTexTarget);
CRASSERT(uiProg);
}
Assert(uiProg);
pCache->pDispatch->UseProgram(uiProg);
return VINF_SUCCESS;
}
VBOXBLITTERDECL(bool) CrGlslNeedsCleanup(const CR_GLSL_CACHE *pCache)
{
return pCache->uNoAlpha2DProg || pCache->uNoAlpha2DRectProg;
}
VBOXBLITTERDECL(void) CrGlslCleanup(CR_GLSL_CACHE *pCache)
{
if (pCache->uNoAlpha2DProg)
{
pCache->pDispatch->DeleteProgram(pCache->uNoAlpha2DProg);
pCache->uNoAlpha2DProg = 0;
}
if (pCache->uNoAlpha2DRectProg)
{
pCache->pDispatch->DeleteProgram(pCache->uNoAlpha2DRectProg);
pCache->uNoAlpha2DRectProg = 0;
}
}
VBOXBLITTERDECL(void) CrGlslTerm(CR_GLSL_CACHE *pCache)
{
CRASSERT(!CrGlslNeedsCleanup(pCache));
CrGlslCleanup(pCache);
/* sanity */
memset(pCache, 0, sizeof (*pCache));
}
/*TdBlt*/
static void crTdBltCheckPBO(PCR_TEXDATA pTex)
{
if (pTex->idPBO)
return;
PCR_BLITTER pBlitter = pTex->pBlitter;
if (!pBlitter->Flags.SupportsPBO)
return;
pBlitter->pDispatch->GenBuffersARB(1, &pTex->idPBO);
if (!pTex->idPBO)
{
crWarning("PBO create failed");
return;
}
pBlitter->pDispatch->BindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, pTex->idPBO);
pBlitter->pDispatch->BufferDataARB(GL_PIXEL_PACK_BUFFER_ARB,
pTex->Tex.width*pTex->Tex.height*4,
0, GL_STREAM_READ_ARB);
pBlitter->pDispatch->BindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, 0);
}
static uint32_t crTdBltTexCreate(PCR_BLITTER pBlitter, uint32_t width, uint32_t height, GLenum enmTarget)
{
uint32_t tex = 0;
pBlitter->pDispatch->GenTextures(1, &tex);
if (!tex)
{
crWarning("Tex create failed");
return 0;
}
pBlitter->pDispatch->BindTexture(enmTarget, tex);
pBlitter->pDispatch->TexParameteri(enmTarget, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
pBlitter->pDispatch->TexParameteri(enmTarget, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
pBlitter->pDispatch->TexParameteri(enmTarget, GL_TEXTURE_WRAP_S, GL_CLAMP);
pBlitter->pDispatch->TexParameteri(enmTarget, GL_TEXTURE_WRAP_T, GL_CLAMP);
pBlitter->pDispatch->TexImage2D(enmTarget, 0, GL_RGBA8,
width, height,
0, GL_BGRA, GL_UNSIGNED_BYTE, NULL);
/*Restore gl state*/
pBlitter->pDispatch->BindTexture(enmTarget, 0);
return tex;
}
int crTdBltCheckInvertTex(PCR_TEXDATA pTex)
{
if (pTex->idInvertTex)
return VINF_SUCCESS;
pTex->idInvertTex = crTdBltTexCreate(pTex->pBlitter, pTex->Tex.width, pTex->Tex.height, pTex->Tex.target);
if (!pTex->idInvertTex)
{
crWarning("Invert Tex create failed");
return VERR_GENERAL_FAILURE;
}
return VINF_SUCCESS;
}
void crTdBltImgRelease(PCR_TEXDATA pTex)
{
pTex->Flags.DataValid = 0;
}
void crTdBltImgFree(PCR_TEXDATA pTex)
{
if (!pTex->Img.pvData)
{
Assert(!pTex->Flags.DataValid);
return;
}
crTdBltImgRelease(pTex);
Assert(!pTex->Flags.DataValid);
if (pTex->idPBO)
{
PCR_BLITTER pBlitter = pTex->pBlitter;
Assert(CrBltIsEntered(pBlitter));
pBlitter->pDispatch->BindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, pTex->idPBO);
pBlitter->pDispatch->UnmapBufferARB(GL_PIXEL_PACK_BUFFER_ARB);
pBlitter->pDispatch->BindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, 0);
}
else
{
Assert(pTex->Img.pvData);
RTMemFree(pTex->Img.pvData);
}
pTex->Img.pvData = NULL;
}
int crTdBltImgAcquire(PCR_TEXDATA pTex, GLenum enmFormat, bool fInverted)
{
void *pvData = pTex->Img.pvData;
Assert(!pTex->Flags.DataValid);
int rc = crBltImgInitBaseForTex(&pTex->Tex, &pTex->Img, enmFormat);
if (!RT_SUCCESS(rc))
{
WARN(("crBltImgInitBaseForTex failed rc %d", rc));
return rc;
}
PCR_BLITTER pBlitter = pTex->pBlitter;
Assert(CrBltIsEntered(pBlitter));
pBlitter->pDispatch->BindTexture(pTex->Tex.target, fInverted ? pTex->idInvertTex : pTex->Tex.hwid);
pBlitter->pDispatch->BindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, pTex->idPBO);
if (pvData)
{
if (pTex->idPBO)
{
pBlitter->pDispatch->UnmapBufferARB(GL_PIXEL_PACK_BUFFER_ARB);
pvData = NULL;
}
}
else
{
if (!pTex->idPBO)
{
pvData = RTMemAlloc(4*pTex->Tex.width*pTex->Tex.height);
if (!pvData)
{
WARN(("Out of memory in crTdBltImgAcquire"));
pBlitter->pDispatch->BindTexture(pTex->Tex.target, 0);
return VERR_NO_MEMORY;
}
}
}
Assert(!pvData == !!pTex->idPBO);
/*read the texture, note pixels are NULL for PBO case as it's offset in the buffer*/
pBlitter->pDispatch->GetTexImage(GL_TEXTURE_2D, 0, enmFormat, GL_UNSIGNED_BYTE, pvData);
/*restore gl state*/
pBlitter->pDispatch->BindTexture(pTex->Tex.target, 0);
if (pTex->idPBO)
{
pvData = pBlitter->pDispatch->MapBufferARB(GL_PIXEL_PACK_BUFFER_ARB, GL_READ_ONLY);
if (!pvData)
{
WARN(("Failed to MapBuffer in CrHlpGetTexImage"));
return VERR_GENERAL_FAILURE;
}
pBlitter->pDispatch->BindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, 0);
}
Assert(pvData);
pTex->Img.pvData = pvData;
pTex->Flags.DataValid = 1;
pTex->Flags.DataInverted = fInverted;
return VINF_SUCCESS;
}
/* release the texture data, the data remains cached in the CR_TEXDATA object until it is discarded with CrTdBltDataInvalidateNe or CrTdBltDataCleanup */
VBOXBLITTERDECL(int) CrTdBltDataRelease(PCR_TEXDATA pTex)
{
if (!pTex->Flags.Entered)
{
WARN(("tex not entered"));
return VERR_INVALID_STATE;
}
if (!pTex->Flags.DataAcquired)
{
WARN(("Data NOT acquired"));
return VERR_INVALID_STATE;
}
Assert(pTex->Img.pvData);
Assert(pTex->Flags.DataValid);
pTex->Flags.DataAcquired = 0;
return VINF_SUCCESS;
}
static void crTdBltDataFree(PCR_TEXDATA pTex)
{
crTdBltImgFree(pTex);
if (pTex->pScaledCache)
CrTdBltDataFreeNe(pTex->pScaledCache);
}
/* discard the texture data cached with previous CrTdBltDataAcquire.
* Must be called wit data released (CrTdBltDataRelease) */
VBOXBLITTERDECL(int) CrTdBltDataFree(PCR_TEXDATA pTex)
{
if (!pTex->Flags.Entered)
{
WARN(("tex not entered"));
return VERR_INVALID_STATE;
}
crTdBltDataFree(pTex);
return VINF_SUCCESS;
}
VBOXBLITTERDECL(void) CrTdBltDataInvalidateNe(PCR_TEXDATA pTex)
{
crTdBltImgRelease(pTex);
if (pTex->pScaledCache)
CrTdBltDataInvalidateNe(pTex->pScaledCache);
}
VBOXBLITTERDECL(int) CrTdBltDataFreeNe(PCR_TEXDATA pTex)
{
if (!pTex->Img.pvData)
return VINF_SUCCESS;
bool fEntered = false;
if (pTex->idPBO)
{
int rc = CrTdBltEnter(pTex);
if (!RT_SUCCESS(rc))
{
WARN(("err"));
return rc;
}
fEntered = true;
}
crTdBltDataFree(pTex);
if (fEntered)
CrTdBltLeave(pTex);
return VINF_SUCCESS;
}
static void crTdBltSdCleanupCacheNe(PCR_TEXDATA pTex)
{
if (pTex->pScaledCache)
{
CrTdBltDataCleanupNe(pTex->pScaledCache);
CrTdRelease(pTex->pScaledCache);
pTex->pScaledCache = NULL;
}
}
static void crTdBltDataCleanup(PCR_TEXDATA pTex)
{
crTdBltImgFree(pTex);
PCR_BLITTER pBlitter = pTex->pBlitter;
if (pTex->idPBO)
{
Assert(CrBltIsEntered(pBlitter));
pBlitter->pDispatch->DeleteBuffersARB(1, &pTex->idPBO);
pTex->idPBO = 0;
}
if (pTex->idInvertTex)
{
Assert(CrBltIsEntered(pBlitter));
pBlitter->pDispatch->DeleteTextures(1, &pTex->idInvertTex);
pTex->idInvertTex = 0;
}
crTdBltSdCleanupCacheNe(pTex);
}
/* does same as CrTdBltDataFree, and in addition cleans up */
VBOXBLITTERDECL(int) CrTdBltDataCleanup(PCR_TEXDATA pTex)
{
if (!pTex->Flags.Entered)
{
WARN(("tex not entered"));
return VERR_INVALID_STATE;
}
crTdBltDataCleanup(pTex);
return VINF_SUCCESS;
}
VBOXBLITTERDECL(int) CrTdBltDataCleanupNe(PCR_TEXDATA pTex)
{
bool fEntered = false;
if (pTex->idPBO || pTex->idInvertTex)
{
int rc = CrTdBltEnter(pTex);
if (!RT_SUCCESS(rc))
{
WARN(("err"));
return rc;
}
fEntered = true;
}
crTdBltDataCleanup(pTex);
if (fEntered)
CrTdBltLeave(pTex);
return VINF_SUCCESS;
}
/* acquire the texture data, returns the cached data in case it is cached.
* the data remains cached in the CR_TEXDATA object until it is discarded with CrTdBltDataFree or CrTdBltDataCleanup.
* */
VBOXBLITTERDECL(int) CrTdBltDataAcquire(PCR_TEXDATA pTex, GLenum enmFormat, bool fInverted, const CR_BLITTER_IMG**ppImg)
{
if (!pTex->Flags.Entered)
{
WARN(("tex not entered"));
return VERR_INVALID_STATE;
}
if (pTex->Flags.DataAcquired)
{
WARN(("Data acquired already"));
return VERR_INVALID_STATE;
}
if (pTex->Flags.DataValid && pTex->Img.enmFormat == enmFormat && !pTex->Flags.DataInverted == !fInverted)
{
Assert(pTex->Img.pvData);
*ppImg = &pTex->Img;
pTex->Flags.DataAcquired = 1;
return VINF_SUCCESS;
}
crTdBltImgRelease(pTex);
crTdBltCheckPBO(pTex);
int rc;
if (fInverted)
{
rc = crTdBltCheckInvertTex(pTex);
if (!RT_SUCCESS(rc))
{
WARN(("crTdBltCheckInvertTex failed rc %d", rc));
return rc;
}
RTRECT SrcRect, DstRect;
VBOXVR_TEXTURE InvertTex;
InvertTex = pTex->Tex;
InvertTex.hwid = pTex->idInvertTex;
SrcRect.xLeft = 0;
SrcRect.yTop = InvertTex.height;
SrcRect.xRight = InvertTex.width;
SrcRect.yBottom = 0;
DstRect.xLeft = 0;
DstRect.yTop = 0;
DstRect.xRight = InvertTex.width;
DstRect.yBottom = InvertTex.height;
CrBltBlitTexTex(pTex->pBlitter, &pTex->Tex, &SrcRect, &InvertTex, &DstRect, 1, 0);
}
rc = crTdBltImgAcquire(pTex, enmFormat, fInverted);
if (!RT_SUCCESS(rc))
{
WARN(("crTdBltImgAcquire failed rc %d", rc));
return rc;
}
Assert(pTex->Img.pvData);
*ppImg = &pTex->Img;
pTex->Flags.DataAcquired = 1;
return VINF_SUCCESS;
}
DECLINLINE(void) crTdResize(PCR_TEXDATA pTex, const VBOXVR_TEXTURE *pVrTex)
{
crTdBltDataCleanup(pTex);
pTex->Tex = *pVrTex;
}
static DECLCALLBACK(void) ctTdBltSdReleased(struct CR_TEXDATA *pTexture)
{
PCR_BLITTER pBlitter = pTexture->pBlitter;
int rc = CrBltEnter(pBlitter);
if (!RT_SUCCESS(rc))
{
WARN(("CrBltEnter failed, rc %d", rc));
return;
}
CrTdBltDataCleanupNe(pTexture);
pBlitter->pDispatch->DeleteTextures(1, &pTexture->Tex.hwid);
CrBltLeave(pBlitter);
RTMemFree(pTexture);
}
static int ctTdBltSdCreate(PCR_BLITTER pBlitter, uint32_t width, uint32_t height, GLenum enmTarget, PCR_TEXDATA *ppScaledCache)
{
PCR_TEXDATA pScaledCache;
Assert(CrBltIsEntered(pBlitter));
*ppScaledCache = NULL;
pScaledCache = (PCR_TEXDATA)RTMemAlloc(sizeof (*pScaledCache));
if (!pScaledCache)
{
WARN(("RTMemAlloc failed"));
return VERR_NO_MEMORY;
}
VBOXVR_TEXTURE Tex;
Tex.width = width;
Tex.height = height;
Tex.target = enmTarget;
Tex.hwid = crTdBltTexCreate(pBlitter, width, height, enmTarget);
if (!Tex.hwid)
{
WARN(("Tex create failed"));
RTMemFree(pScaledCache);
return VERR_GENERAL_FAILURE;
}
CrTdInit(pScaledCache, &Tex, pBlitter, ctTdBltSdReleased);
*ppScaledCache = pScaledCache;
return VINF_SUCCESS;
}
static int ctTdBltSdGet(PCR_TEXDATA pTex, uint32_t width, uint32_t height, PCR_TEXDATA *ppScaledCache)
{
Assert(CrBltIsEntered(pTex->pBlitter));
PCR_TEXDATA pScaledCache;
*ppScaledCache = NULL;
if (!pTex->pScaledCache)
{
int rc = ctTdBltSdCreate(pTex->pBlitter, width, height, pTex->Tex.target, &pScaledCache);
if (!RT_SUCCESS(rc))
{
WARN(("ctTdBltSdCreate failed %d", rc));
return rc;
}
pTex->pScaledCache = pScaledCache;
}
else
{
int cmp = pTex->pScaledCache->Tex.width - width;
if (cmp <= 0)
cmp = pTex->pScaledCache->Tex.height - height;
if (!cmp)
pScaledCache = pTex->pScaledCache;
else if (cmp < 0) /* current cache is "less" than the requested */
{
int rc = ctTdBltSdCreate(pTex->pBlitter, width, height, pTex->Tex.target, &pScaledCache);
if (!RT_SUCCESS(rc))
{
WARN(("ctTdBltSdCreate failed %d", rc));
return rc;
}
pScaledCache->pScaledCache = pTex->pScaledCache;
pTex->pScaledCache = pScaledCache;
}
else /* cmp > 0 */
{
int rc = ctTdBltSdGet(pTex->pScaledCache, width, height, &pScaledCache);
if (!RT_SUCCESS(rc))
{
WARN(("ctTdBltSdGet failed %d", rc));
return rc;
}
}
}
Assert(pScaledCache);
#if 0
{
VBOXVR_TEXTURE Tex;
Tex.width = width;
Tex.height = height;
Tex.target = pTex->Tex.target;
Tex.hwid = crTdBltTexCreate(pTex, width, height);
if (!Tex.hwid)
{
WARN(("Tex create failed"));
return VERR_GENERAL_FAILURE;
}
pTex->pBlitter->pDispatch->DeleteTextures(1, &pTex->pScaledCache->Tex.hwid);
crTdResize(pTex->pScaledCache, &Tex);
}
#endif
*ppScaledCache = pScaledCache;
return VINF_SUCCESS;
}
static int ctTdBltSdGetUpdated(PCR_TEXDATA pTex, uint32_t width, uint32_t height, PCR_TEXDATA *ppScaledCache)
{
PCR_TEXDATA pScaledCache;
*ppScaledCache = NULL;
int rc = ctTdBltSdGet(pTex, width, height, &pScaledCache);
if (!RT_SUCCESS(rc))
{
WARN(("ctTdBltSdGet failed %d", rc));
return rc;
}
Assert(width == pScaledCache->Tex.width);
Assert(height == pScaledCache->Tex.height);
if (!pScaledCache->Flags.DataValid)
{
RTRECT SrcRect, DstRect;
SrcRect.xLeft = 0;
SrcRect.yTop = 0;
SrcRect.xRight = pTex->Tex.width;
SrcRect.yBottom = pTex->Tex.height;
DstRect.xLeft = 0;
DstRect.yTop = 0;
DstRect.xRight = width;
DstRect.yBottom = height;
CrBltBlitTexTex(pTex->pBlitter, &pTex->Tex, &SrcRect, &pScaledCache->Tex, &DstRect, 1, 0);
}
*ppScaledCache = pScaledCache;
return VINF_SUCCESS;
}
VBOXBLITTERDECL(int) CrTdBltDataAcquireScaled(PCR_TEXDATA pTex, GLenum enmFormat, bool fInverted, uint32_t width, uint32_t height, const CR_BLITTER_IMG**ppImg)
{
if (pTex->Tex.width == width && pTex->Tex.height == height)
return CrTdBltDataAcquire(pTex, enmFormat, fInverted, ppImg);
if (!pTex->Flags.Entered)
{
WARN(("tex not entered"));
return VERR_INVALID_STATE;
}
PCR_TEXDATA pScaledCache;
int rc = ctTdBltSdGetUpdated(pTex, width, height, &pScaledCache);
if (!RT_SUCCESS(rc))
{
WARN(("ctTdBltSdGetUpdated failed rc %d", rc));
return rc;
}
rc = CrTdBltEnter(pScaledCache);
if (!RT_SUCCESS(rc))
{
WARN(("CrTdBltEnter failed rc %d", rc));
return rc;
}
rc = CrTdBltDataAcquire(pScaledCache, enmFormat, fInverted, ppImg);
if (!RT_SUCCESS(rc))
{
WARN(("CrTdBltDataAcquire failed rc %d", rc));
CrTdBltLeave(pTex->pScaledCache);
return rc;
}
return VINF_SUCCESS;
}
VBOXBLITTERDECL(int) CrTdBltDataReleaseScaled(PCR_TEXDATA pTex, const CR_BLITTER_IMG *pImg)
{
PCR_TEXDATA pScaledCache = RT_FROM_MEMBER(pImg, CR_TEXDATA, Img);
int rc = CrTdBltDataRelease(pScaledCache);
if (!RT_SUCCESS(rc))
{
WARN(("CrTdBltDataRelease failed rc %d", rc));
return rc;
}
if (pScaledCache != pTex)
CrTdBltLeave(pScaledCache);
return VINF_SUCCESS;
}
VBOXBLITTERDECL(void) CrTdBltScaleCacheMoveTo(PCR_TEXDATA pTex, PCR_TEXDATA pDstTex)
{
if (!pTex->pScaledCache)
return;
crTdBltSdCleanupCacheNe(pDstTex);
pDstTex->pScaledCache = pTex->pScaledCache;
pTex->pScaledCache = NULL;
}
|