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
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
|
/*
* Mesa 3-D graphics library
*
* Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
* Copyright (c) 2009 VMware, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/
/**
* Code for glGetTexImage() and glGetCompressedTexImage().
*/
#include "glheader.h"
#include "bufferobj.h"
#include "enums.h"
#include "context.h"
#include "formats.h"
#include "format_unpack.h"
#include "glformats.h"
#include "image.h"
#include "mtypes.h"
#include "pack.h"
#include "pbo.h"
#include "pixelstore.h"
#include "texcompress.h"
#include "texgetimage.h"
#include "teximage.h"
#include "texobj.h"
#include "texstore.h"
#include "format_utils.h"
#include "pixeltransfer.h"
/**
* Can the given type represent negative values?
*/
static inline GLboolean
type_needs_clamping(GLenum type)
{
switch (type) {
case GL_BYTE:
case GL_SHORT:
case GL_INT:
case GL_FLOAT:
case GL_HALF_FLOAT_ARB:
case GL_UNSIGNED_INT_10F_11F_11F_REV:
case GL_UNSIGNED_INT_5_9_9_9_REV:
return GL_FALSE;
default:
return GL_TRUE;
}
}
/**
* glGetTexImage for depth/Z pixels.
*/
static void
get_tex_depth(struct gl_context *ctx, GLuint dimensions,
GLint xoffset, GLint yoffset, GLint zoffset,
GLsizei width, GLsizei height, GLint depth,
GLenum format, GLenum type, GLvoid *pixels,
struct gl_texture_image *texImage)
{
GLint img, row;
GLfloat *depthRow = malloc(width * sizeof(GLfloat));
if (!depthRow) {
_mesa_error(ctx, GL_OUT_OF_MEMORY, "glGetTexImage");
return;
}
for (img = 0; img < depth; img++) {
GLubyte *srcMap;
GLint srcRowStride;
/* map src texture buffer */
ctx->Driver.MapTextureImage(ctx, texImage, zoffset + img,
xoffset, yoffset, width, height,
GL_MAP_READ_BIT, &srcMap, &srcRowStride);
if (srcMap) {
for (row = 0; row < height; row++) {
void *dest = _mesa_image_address(dimensions, &ctx->Pack, pixels,
width, height, format, type,
img, row, 0);
const GLubyte *src = srcMap + row * srcRowStride;
_mesa_unpack_float_z_row(texImage->TexFormat, width, src, depthRow);
_mesa_pack_depth_span(ctx, width, dest, type, depthRow, &ctx->Pack);
}
ctx->Driver.UnmapTextureImage(ctx, texImage, zoffset + img);
}
else {
_mesa_error(ctx, GL_OUT_OF_MEMORY, "glGetTexImage");
break;
}
}
free(depthRow);
}
/**
* glGetTexImage for depth/stencil pixels.
*/
static void
get_tex_depth_stencil(struct gl_context *ctx, GLuint dimensions,
GLint xoffset, GLint yoffset, GLint zoffset,
GLsizei width, GLsizei height, GLint depth,
GLenum format, GLenum type, GLvoid *pixels,
struct gl_texture_image *texImage)
{
GLint img, row;
assert(format == GL_DEPTH_STENCIL);
for (img = 0; img < depth; img++) {
GLubyte *srcMap;
GLint rowstride;
/* map src texture buffer */
ctx->Driver.MapTextureImage(ctx, texImage, zoffset + img,
xoffset, yoffset, width, height,
GL_MAP_READ_BIT, &srcMap, &rowstride);
if (srcMap) {
for (row = 0; row < height; row++) {
const GLubyte *src = srcMap + row * rowstride;
void *dest = _mesa_image_address(dimensions, &ctx->Pack, pixels,
width, height, format, type,
img, row, 0);
switch (type) {
case GL_UNSIGNED_INT_24_8:
_mesa_unpack_uint_24_8_depth_stencil_row(texImage->TexFormat,
width, src, dest);
break;
case GL_FLOAT_32_UNSIGNED_INT_24_8_REV:
_mesa_unpack_float_32_uint_24_8_depth_stencil_row(texImage->TexFormat,
width,
src, dest);
break;
default:
unreachable("bad type in get_tex_depth_stencil()");
}
if (ctx->Pack.SwapBytes) {
_mesa_swap4((GLuint *) dest, width);
}
}
ctx->Driver.UnmapTextureImage(ctx, texImage, zoffset + img);
}
else {
_mesa_error(ctx, GL_OUT_OF_MEMORY, "glGetTexImage");
break;
}
}
}
/**
* glGetTexImage for stencil pixels.
*/
static void
get_tex_stencil(struct gl_context *ctx, GLuint dimensions,
GLint xoffset, GLint yoffset, GLint zoffset,
GLsizei width, GLsizei height, GLint depth,
GLenum format, GLenum type, GLvoid *pixels,
struct gl_texture_image *texImage)
{
GLint img, row;
assert(format == GL_STENCIL_INDEX);
for (img = 0; img < depth; img++) {
GLubyte *srcMap;
GLint rowstride;
/* map src texture buffer */
ctx->Driver.MapTextureImage(ctx, texImage, zoffset + img,
xoffset, yoffset, width, height,
GL_MAP_READ_BIT,
&srcMap, &rowstride);
if (srcMap) {
for (row = 0; row < height; row++) {
const GLubyte *src = srcMap + row * rowstride;
void *dest = _mesa_image_address(dimensions, &ctx->Pack, pixels,
width, height, format, type,
img, row, 0);
_mesa_unpack_ubyte_stencil_row(texImage->TexFormat,
width,
(const GLuint *) src,
dest);
}
ctx->Driver.UnmapTextureImage(ctx, texImage, zoffset + img);
}
else {
_mesa_error(ctx, GL_OUT_OF_MEMORY, "glGetTexImage");
break;
}
}
}
/**
* glGetTexImage for YCbCr pixels.
*/
static void
get_tex_ycbcr(struct gl_context *ctx, GLuint dimensions,
GLint xoffset, GLint yoffset, GLint zoffset,
GLsizei width, GLsizei height, GLint depth,
GLenum format, GLenum type, GLvoid *pixels,
struct gl_texture_image *texImage)
{
GLint img, row;
for (img = 0; img < depth; img++) {
GLubyte *srcMap;
GLint rowstride;
/* map src texture buffer */
ctx->Driver.MapTextureImage(ctx, texImage, zoffset + img,
xoffset, yoffset, width, height,
GL_MAP_READ_BIT, &srcMap, &rowstride);
if (srcMap) {
for (row = 0; row < height; row++) {
const GLubyte *src = srcMap + row * rowstride;
void *dest = _mesa_image_address(dimensions, &ctx->Pack, pixels,
width, height, format, type,
img, row, 0);
memcpy(dest, src, width * sizeof(GLushort));
/* check for byte swapping */
if ((texImage->TexFormat == MESA_FORMAT_YCBCR
&& type == GL_UNSIGNED_SHORT_8_8_REV_MESA) ||
(texImage->TexFormat == MESA_FORMAT_YCBCR_REV
&& type == GL_UNSIGNED_SHORT_8_8_MESA)) {
if (!ctx->Pack.SwapBytes)
_mesa_swap2((GLushort *) dest, width);
}
else if (ctx->Pack.SwapBytes) {
_mesa_swap2((GLushort *) dest, width);
}
}
ctx->Driver.UnmapTextureImage(ctx, texImage, zoffset + img);
}
else {
_mesa_error(ctx, GL_OUT_OF_MEMORY, "glGetTexImage");
break;
}
}
}
/**
* Depending on the base format involved we may need to apply a rebase
* transform (for example: if we download to a Luminance format we want
* G=0 and B=0).
*/
static bool
teximage_needs_rebase(mesa_format texFormat, GLenum baseFormat,
bool is_compressed, uint8_t *rebaseSwizzle)
{
bool needsRebase = false;
if (baseFormat == GL_LUMINANCE ||
baseFormat == GL_INTENSITY) {
needsRebase = true;
rebaseSwizzle[0] = MESA_FORMAT_SWIZZLE_X;
rebaseSwizzle[1] = MESA_FORMAT_SWIZZLE_ZERO;
rebaseSwizzle[2] = MESA_FORMAT_SWIZZLE_ZERO;
rebaseSwizzle[3] = MESA_FORMAT_SWIZZLE_ONE;
} else if (baseFormat == GL_LUMINANCE_ALPHA) {
needsRebase = true;
rebaseSwizzle[0] = MESA_FORMAT_SWIZZLE_X;
rebaseSwizzle[1] = MESA_FORMAT_SWIZZLE_ZERO;
rebaseSwizzle[2] = MESA_FORMAT_SWIZZLE_ZERO;
rebaseSwizzle[3] = MESA_FORMAT_SWIZZLE_W;
} else if (!is_compressed &&
(baseFormat != _mesa_get_format_base_format(texFormat))) {
needsRebase =
_mesa_compute_rgba2base2rgba_component_mapping(baseFormat,
rebaseSwizzle);
}
return needsRebase;
}
/**
* Get a color texture image with decompression.
*/
static void
get_tex_rgba_compressed(struct gl_context *ctx, GLuint dimensions,
GLint xoffset, GLint yoffset, GLint zoffset,
GLsizei width, GLsizei height, GLint depth,
GLenum format, GLenum type, GLvoid *pixels,
struct gl_texture_image *texImage,
GLbitfield transferOps)
{
/* don't want to apply sRGB -> RGB conversion here so override the format */
const mesa_format texFormat =
_mesa_get_srgb_format_linear(texImage->TexFormat);
const GLenum baseFormat = _mesa_get_format_base_format(texFormat);
GLfloat *tempImage, *tempSlice;
GLuint slice;
int srcStride, dstStride;
uint32_t dstFormat;
bool needsRebase;
uint8_t rebaseSwizzle[4];
/* Decompress into temp float buffer, then pack into user buffer */
tempImage = malloc(width * height * depth * 4 * sizeof(GLfloat));
if (!tempImage) {
_mesa_error(ctx, GL_OUT_OF_MEMORY, "glGetTexImage()");
return;
}
/* Decompress the texture image slices - results in 'tempImage' */
for (slice = 0; slice < depth; slice++) {
GLubyte *srcMap;
GLint srcRowStride;
tempSlice = tempImage + slice * 4 * width * height;
ctx->Driver.MapTextureImage(ctx, texImage, zoffset + slice,
xoffset, yoffset, width, height,
GL_MAP_READ_BIT,
&srcMap, &srcRowStride);
if (srcMap) {
_mesa_decompress_image(texFormat, width, height,
srcMap, srcRowStride, tempSlice);
ctx->Driver.UnmapTextureImage(ctx, texImage, zoffset + slice);
}
else {
_mesa_error(ctx, GL_OUT_OF_MEMORY, "glGetTexImage");
free(tempImage);
return;
}
}
needsRebase = teximage_needs_rebase(texFormat, baseFormat, true,
rebaseSwizzle);
srcStride = 4 * width * sizeof(GLfloat);
dstStride = _mesa_image_row_stride(&ctx->Pack, width, format, type);
dstFormat = _mesa_format_from_format_and_type(format, type);
tempSlice = tempImage;
for (slice = 0; slice < depth; slice++) {
void *dest = _mesa_image_address(dimensions, &ctx->Pack, pixels,
width, height, format, type,
slice, 0, 0);
_mesa_format_convert(dest, dstFormat, dstStride,
tempSlice, RGBA32_FLOAT, srcStride,
width, height,
needsRebase ? rebaseSwizzle : NULL);
/* Handle byte swapping if required */
if (ctx->Pack.SwapBytes) {
_mesa_swap_bytes_2d_image(format, type, &ctx->Pack,
width, height, dest, dest);
}
tempSlice += 4 * width * height;
}
free(tempImage);
}
/**
* Return a base GL format given the user-requested format
* for glGetTexImage().
*/
GLenum
_mesa_base_pack_format(GLenum format)
{
switch (format) {
case GL_ABGR_EXT:
case GL_BGRA:
case GL_BGRA_INTEGER:
case GL_RGBA_INTEGER:
return GL_RGBA;
case GL_BGR:
case GL_BGR_INTEGER:
case GL_RGB_INTEGER:
return GL_RGB;
case GL_RED_INTEGER:
return GL_RED;
case GL_GREEN_INTEGER:
return GL_GREEN;
case GL_BLUE_INTEGER:
return GL_BLUE;
case GL_ALPHA_INTEGER:
return GL_ALPHA;
case GL_LUMINANCE_INTEGER_EXT:
return GL_LUMINANCE;
case GL_LUMINANCE_ALPHA_INTEGER_EXT:
return GL_LUMINANCE_ALPHA;
default:
return format;
}
}
/**
* Get an uncompressed color texture image.
*/
static void
get_tex_rgba_uncompressed(struct gl_context *ctx, GLuint dimensions,
GLint xoffset, GLint yoffset, GLint zoffset,
GLsizei width, GLsizei height, GLint depth,
GLenum format, GLenum type, GLvoid *pixels,
struct gl_texture_image *texImage,
GLbitfield transferOps)
{
/* don't want to apply sRGB -> RGB conversion here so override the format */
const mesa_format texFormat =
_mesa_get_srgb_format_linear(texImage->TexFormat);
GLuint img;
GLboolean dst_is_integer;
uint32_t dst_format;
int dst_stride;
uint8_t rebaseSwizzle[4];
bool needsRebase;
void *rgba = NULL;
needsRebase = teximage_needs_rebase(texFormat, texImage->_BaseFormat, false,
rebaseSwizzle);
/* Describe the dst format */
dst_is_integer = _mesa_is_enum_format_integer(format);
dst_format = _mesa_format_from_format_and_type(format, type);
dst_stride = _mesa_image_row_stride(&ctx->Pack, width, format, type);
/* Since _mesa_format_convert does not handle transferOps we need to handle
* them before we call the function. This requires to convert to RGBA float
* first so we can call _mesa_apply_rgba_transfer_ops. If the dst format is
* integer then transferOps do not apply.
*/
assert(!transferOps || (transferOps && !dst_is_integer));
(void) dst_is_integer; /* silence unused var warning */
for (img = 0; img < depth; img++) {
GLubyte *srcMap;
GLint rowstride;
GLubyte *img_src;
void *dest;
void *src;
int src_stride;
uint32_t src_format;
/* map src texture buffer */
ctx->Driver.MapTextureImage(ctx, texImage, zoffset + img,
xoffset, yoffset, width, height,
GL_MAP_READ_BIT,
&srcMap, &rowstride);
if (!srcMap) {
_mesa_error(ctx, GL_OUT_OF_MEMORY, "glGetTexImage");
goto done;
}
img_src = srcMap;
dest = _mesa_image_address(dimensions, &ctx->Pack, pixels,
width, height, format, type,
img, 0, 0);
if (transferOps) {
uint32_t rgba_format;
int rgba_stride;
bool need_convert = false;
/* We will convert to RGBA float */
rgba_format = RGBA32_FLOAT;
rgba_stride = width * 4 * sizeof(GLfloat);
/* If we are lucky and the dst format matches the RGBA format we need
* to convert to, then we can convert directly into the dst buffer
* and avoid the final conversion/copy from the rgba buffer to the dst
* buffer.
*/
if (format == rgba_format) {
rgba = dest;
} else {
need_convert = true;
if (rgba == NULL) { /* Allocate the RGBA buffer only once */
rgba = malloc(height * rgba_stride);
if (!rgba) {
_mesa_error(ctx, GL_OUT_OF_MEMORY, "glGetTexImage()");
ctx->Driver.UnmapTextureImage(ctx, texImage, img);
return;
}
}
}
_mesa_format_convert(rgba, rgba_format, rgba_stride,
img_src, texFormat, rowstride,
width, height,
needsRebase ? rebaseSwizzle : NULL);
/* Handle transfer ops now */
_mesa_apply_rgba_transfer_ops(ctx, transferOps, width * height, rgba);
/* If we had to rebase, we have already handled that */
needsRebase = false;
/* If we were lucky and our RGBA conversion matches the dst format,
* then we are done.
*/
if (!need_convert)
goto do_swap;
/* Otherwise, we need to convert from RGBA to dst next */
src = rgba;
src_format = rgba_format;
src_stride = rgba_stride;
} else {
/* No RGBA conversion needed, convert directly to dst */
src = img_src;
src_format = texFormat;
src_stride = rowstride;
}
/* Do the conversion to destination format */
_mesa_format_convert(dest, dst_format, dst_stride,
src, src_format, src_stride,
width, height,
needsRebase ? rebaseSwizzle : NULL);
do_swap:
/* Handle byte swapping if required */
if (ctx->Pack.SwapBytes)
_mesa_swap_bytes_2d_image(format, type, &ctx->Pack,
width, height, dest, dest);
/* Unmap the src texture buffer */
ctx->Driver.UnmapTextureImage(ctx, texImage, zoffset + img);
}
done:
free(rgba);
}
/**
* glGetTexImage for color formats (RGBA, RGB, alpha, LA, etc).
* Compressed textures are handled here as well.
*/
static void
get_tex_rgba(struct gl_context *ctx, GLuint dimensions,
GLint xoffset, GLint yoffset, GLint zoffset,
GLsizei width, GLsizei height, GLint depth,
GLenum format, GLenum type, GLvoid *pixels,
struct gl_texture_image *texImage)
{
const GLenum dataType = _mesa_get_format_datatype(texImage->TexFormat);
GLbitfield transferOps = 0x0;
/* In general, clamping does not apply to glGetTexImage, except when
* the returned type of the image can't hold negative values.
*/
if (type_needs_clamping(type)) {
/* the returned image type can't have negative values */
if (dataType == GL_FLOAT ||
dataType == GL_HALF_FLOAT ||
dataType == GL_SIGNED_NORMALIZED ||
format == GL_LUMINANCE ||
format == GL_LUMINANCE_ALPHA) {
transferOps |= IMAGE_CLAMP_BIT;
}
}
if (_mesa_is_format_compressed(texImage->TexFormat)) {
get_tex_rgba_compressed(ctx, dimensions,
xoffset, yoffset, zoffset,
width, height, depth,
format, type,
pixels, texImage, transferOps);
}
else {
get_tex_rgba_uncompressed(ctx, dimensions,
xoffset, yoffset, zoffset,
width, height, depth,
format, type,
pixels, texImage, transferOps);
}
}
/**
* Try to do glGetTexImage() with simple memcpy().
* \return GL_TRUE if done, GL_FALSE otherwise
*/
static GLboolean
get_tex_memcpy(struct gl_context *ctx,
GLint xoffset, GLint yoffset, GLint zoffset,
GLsizei width, GLsizei height, GLint depth,
GLenum format, GLenum type, GLvoid *pixels,
struct gl_texture_image *texImage)
{
const GLenum target = texImage->TexObject->Target;
GLboolean memCopy = GL_FALSE;
GLenum texBaseFormat = _mesa_get_format_base_format(texImage->TexFormat);
/*
* Check if we can use memcpy to copy from the hardware texture
* format to the user's format/type.
* Note that GL's pixel transfer ops don't apply to glGetTexImage()
*/
if ((target == GL_TEXTURE_1D ||
target == GL_TEXTURE_2D ||
target == GL_TEXTURE_RECTANGLE ||
_mesa_is_cube_face(target)) &&
texBaseFormat == texImage->_BaseFormat) {
memCopy = _mesa_format_matches_format_and_type(texImage->TexFormat,
format, type,
ctx->Pack.SwapBytes, NULL);
}
if (depth > 1) {
/* only a single slice is supported at this time */
memCopy = GL_FALSE;
}
if (memCopy) {
const GLuint bpp = _mesa_get_format_bytes(texImage->TexFormat);
const GLint bytesPerRow = width * bpp;
GLubyte *dst =
_mesa_image_address2d(&ctx->Pack, pixels, width, height,
format, type, 0, 0);
const GLint dstRowStride =
_mesa_image_row_stride(&ctx->Pack, width, format, type);
GLubyte *src;
GLint srcRowStride;
/* map src texture buffer */
ctx->Driver.MapTextureImage(ctx, texImage, zoffset,
xoffset, yoffset, width, height,
GL_MAP_READ_BIT, &src, &srcRowStride);
if (src) {
if (bytesPerRow == dstRowStride && bytesPerRow == srcRowStride) {
memcpy(dst, src, bytesPerRow * height);
}
else {
GLuint row;
for (row = 0; row < height; row++) {
memcpy(dst, src, bytesPerRow);
dst += dstRowStride;
src += srcRowStride;
}
}
/* unmap src texture buffer */
ctx->Driver.UnmapTextureImage(ctx, texImage, zoffset);
}
else {
_mesa_error(ctx, GL_OUT_OF_MEMORY, "glGetTexImage");
}
}
return memCopy;
}
/**
* This is the software fallback for Driver.GetTexSubImage().
* All error checking will have been done before this routine is called.
* We'll call ctx->Driver.MapTextureImage() to access the data, then
* unmap with ctx->Driver.UnmapTextureImage().
*/
void
_mesa_GetTexSubImage_sw(struct gl_context *ctx,
GLint xoffset, GLint yoffset, GLint zoffset,
GLsizei width, GLsizei height, GLint depth,
GLenum format, GLenum type, GLvoid *pixels,
struct gl_texture_image *texImage)
{
const GLuint dimensions =
_mesa_get_texture_dimensions(texImage->TexObject->Target);
/* map dest buffer, if PBO */
if (_mesa_is_bufferobj(ctx->Pack.BufferObj)) {
/* Packing texture image into a PBO.
* Map the (potentially) VRAM-based buffer into our process space so
* we can write into it with the code below.
* A hardware driver might use a sophisticated blit to move the
* texture data to the PBO if the PBO is in VRAM along with the texture.
*/
GLubyte *buf = (GLubyte *)
ctx->Driver.MapBufferRange(ctx, 0, ctx->Pack.BufferObj->Size,
GL_MAP_WRITE_BIT, ctx->Pack.BufferObj,
MAP_INTERNAL);
if (!buf) {
/* out of memory or other unexpected error */
_mesa_error(ctx, GL_OUT_OF_MEMORY, "glGetTexImage(map PBO failed)");
return;
}
/* <pixels> was an offset into the PBO.
* Now make it a real, client-side pointer inside the mapped region.
*/
pixels = ADD_POINTERS(buf, pixels);
}
/* for all array textures, the Z axis selects the layer */
if (texImage->TexObject->Target == GL_TEXTURE_1D_ARRAY) {
depth = height;
height = 1;
zoffset = yoffset;
yoffset = 0;
assert(zoffset + depth <= texImage->Height);
} else {
assert(zoffset + depth <= texImage->Depth);
}
if (get_tex_memcpy(ctx, xoffset, yoffset, zoffset, width, height, depth,
format, type, pixels, texImage)) {
/* all done */
}
else if (format == GL_DEPTH_COMPONENT) {
get_tex_depth(ctx, dimensions, xoffset, yoffset, zoffset,
width, height, depth, format, type, pixels, texImage);
}
else if (format == GL_DEPTH_STENCIL_EXT) {
get_tex_depth_stencil(ctx, dimensions, xoffset, yoffset, zoffset,
width, height, depth, format, type, pixels,
texImage);
}
else if (format == GL_STENCIL_INDEX) {
get_tex_stencil(ctx, dimensions, xoffset, yoffset, zoffset,
width, height, depth, format, type, pixels, texImage);
}
else if (format == GL_YCBCR_MESA) {
get_tex_ycbcr(ctx, dimensions, xoffset, yoffset, zoffset,
width, height, depth, format, type, pixels, texImage);
}
else {
get_tex_rgba(ctx, dimensions, xoffset, yoffset, zoffset,
width, height, depth, format, type, pixels, texImage);
}
if (_mesa_is_bufferobj(ctx->Pack.BufferObj)) {
ctx->Driver.UnmapBuffer(ctx, ctx->Pack.BufferObj, MAP_INTERNAL);
}
}
/**
* This function assumes that all error checking has been done.
*/
static void
get_compressed_texsubimage_sw(struct gl_context *ctx,
struct gl_texture_image *texImage,
GLint xoffset, GLint yoffset,
GLint zoffset, GLsizei width,
GLint height, GLint depth,
GLvoid *img)
{
const GLuint dimensions =
_mesa_get_texture_dimensions(texImage->TexObject->Target);
struct compressed_pixelstore store;
GLint slice;
GLubyte *dest;
_mesa_compute_compressed_pixelstore(dimensions, texImage->TexFormat,
width, height, depth,
&ctx->Pack, &store);
if (_mesa_is_bufferobj(ctx->Pack.BufferObj)) {
/* pack texture image into a PBO */
dest = (GLubyte *)
ctx->Driver.MapBufferRange(ctx, 0, ctx->Pack.BufferObj->Size,
GL_MAP_WRITE_BIT, ctx->Pack.BufferObj,
MAP_INTERNAL);
if (!dest) {
/* out of memory or other unexpected error */
_mesa_error(ctx, GL_OUT_OF_MEMORY,
"glGetCompresssedTexImage(map PBO failed)");
return;
}
dest = ADD_POINTERS(dest, img);
} else {
dest = img;
}
dest += store.SkipBytes;
for (slice = 0; slice < store.CopySlices; slice++) {
GLint srcRowStride;
GLubyte *src;
/* map src texture buffer */
ctx->Driver.MapTextureImage(ctx, texImage, zoffset + slice,
xoffset, yoffset, width, height,
GL_MAP_READ_BIT, &src, &srcRowStride);
if (src) {
GLint i;
for (i = 0; i < store.CopyRowsPerSlice; i++) {
memcpy(dest, src, store.CopyBytesPerRow);
dest += store.TotalBytesPerRow;
src += srcRowStride;
}
ctx->Driver.UnmapTextureImage(ctx, texImage, zoffset + slice);
/* Advance to next slice */
dest += store.TotalBytesPerRow * (store.TotalRowsPerSlice -
store.CopyRowsPerSlice);
} else {
_mesa_error(ctx, GL_OUT_OF_MEMORY, "glGetCompresssedTexImage");
}
}
if (_mesa_is_bufferobj(ctx->Pack.BufferObj)) {
ctx->Driver.UnmapBuffer(ctx, ctx->Pack.BufferObj, MAP_INTERNAL);
}
}
/**
* Validate the texture target enum supplied to glGetTex(ture)Image or
* glGetCompressedTex(ture)Image.
*/
static GLboolean
legal_getteximage_target(struct gl_context *ctx, GLenum target, bool dsa)
{
switch (target) {
case GL_TEXTURE_1D:
case GL_TEXTURE_2D:
case GL_TEXTURE_3D:
return GL_TRUE;
case GL_TEXTURE_RECTANGLE_NV:
return ctx->Extensions.NV_texture_rectangle;
case GL_TEXTURE_1D_ARRAY_EXT:
case GL_TEXTURE_2D_ARRAY_EXT:
return ctx->Extensions.EXT_texture_array;
case GL_TEXTURE_CUBE_MAP_ARRAY:
return ctx->Extensions.ARB_texture_cube_map_array;
/* Section 8.11 (Texture Queries) of the OpenGL 4.5 core profile spec
* (30.10.2014) says:
* "An INVALID_ENUM error is generated if the effective target is not
* one of TEXTURE_1D, TEXTURE_2D, TEXTURE_3D, TEXTURE_1D_ARRAY,
* TEXTURE_2D_ARRAY, TEXTURE_CUBE_MAP_ARRAY, TEXTURE_RECTANGLE, one of
* the targets from table 8.19 (for GetTexImage and GetnTexImage *only*),
* or TEXTURE_CUBE_MAP (for GetTextureImage *only*)." (Emphasis added.)
*/
case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
return dsa ? GL_FALSE : ctx->Extensions.ARB_texture_cube_map;
case GL_TEXTURE_CUBE_MAP:
return dsa ? GL_TRUE : GL_FALSE;
default:
return GL_FALSE;
}
}
/**
* Wrapper for _mesa_select_tex_image() which can handle target being
* GL_TEXTURE_CUBE_MAP in which case we use zoffset to select a cube face.
* This can happen for glGetTextureImage and glGetTextureSubImage (DSA
* functions).
*/
static struct gl_texture_image *
select_tex_image(const struct gl_texture_object *texObj, GLenum target,
GLint level, GLint zoffset)
{
assert(level >= 0);
assert(level < MAX_TEXTURE_LEVELS);
if (target == GL_TEXTURE_CUBE_MAP) {
assert(zoffset >= 0);
assert(zoffset < 6);
target = GL_TEXTURE_CUBE_MAP_POSITIVE_X + zoffset;
}
return _mesa_select_tex_image(texObj, target, level);
}
/**
* Error-check the offset and size arguments to
* glGet[Compressed]TextureSubImage().
* \return true if error, false if no error.
*/
static bool
dimensions_error_check(struct gl_context *ctx,
struct gl_texture_object *texObj,
GLenum target, GLint level,
GLint xoffset, GLint yoffset, GLint zoffset,
GLsizei width, GLsizei height, GLsizei depth,
const char *caller)
{
const struct gl_texture_image *texImage;
GLuint imageWidth = 0, imageHeight = 0, imageDepth = 0;
if (xoffset < 0) {
_mesa_error(ctx, GL_INVALID_VALUE, "%s(xoffset = %d)", caller, xoffset);
return true;
}
if (yoffset < 0) {
_mesa_error(ctx, GL_INVALID_VALUE, "%s(yoffset = %d)", caller, yoffset);
return true;
}
if (zoffset < 0) {
_mesa_error(ctx, GL_INVALID_VALUE, "%s(zoffset = %d)", caller, zoffset);
return true;
}
if (width < 0) {
_mesa_error(ctx, GL_INVALID_VALUE, "%s(width = %d)", caller, width);
return true;
}
if (height < 0) {
_mesa_error(ctx, GL_INVALID_VALUE, "%s(height = %d)", caller, height);
return true;
}
if (depth < 0) {
_mesa_error(ctx, GL_INVALID_VALUE, "%s(depth = %d)", caller, depth);
return true;
}
/* do special per-target checks */
switch (target) {
case GL_TEXTURE_1D:
if (yoffset != 0) {
_mesa_error(ctx, GL_INVALID_VALUE,
"%s(1D, yoffset = %d)", caller, yoffset);
return true;
}
if (height != 1) {
_mesa_error(ctx, GL_INVALID_VALUE,
"%s(1D, height = %d)", caller, height);
return true;
}
/* fall-through */
case GL_TEXTURE_1D_ARRAY:
case GL_TEXTURE_2D:
case GL_TEXTURE_RECTANGLE:
if (zoffset != 0) {
_mesa_error(ctx, GL_INVALID_VALUE,
"%s(zoffset = %d)", caller, zoffset);
return true;
}
if (depth != 1) {
_mesa_error(ctx, GL_INVALID_VALUE,
"%s(depth = %d)", caller, depth);
return true;
}
break;
case GL_TEXTURE_CUBE_MAP:
/* Non-array cube maps are special because we have a gl_texture_image
* per face.
*/
if (zoffset + depth > 6) {
_mesa_error(ctx, GL_INVALID_VALUE,
"%s(zoffset + depth = %d)", caller, zoffset + depth);
return true;
}
break;
default:
; /* nothing */
}
texImage = select_tex_image(texObj, target, level, zoffset);
if (texImage) {
imageWidth = texImage->Width;
imageHeight = texImage->Height;
imageDepth = texImage->Depth;
}
if (xoffset + width > imageWidth) {
_mesa_error(ctx, GL_INVALID_VALUE,
"%s(xoffset %d + width %d > %u)",
caller, xoffset, width, imageWidth);
return true;
}
if (yoffset + height > imageHeight) {
_mesa_error(ctx, GL_INVALID_VALUE,
"%s(yoffset %d + height %d > %u)",
caller, yoffset, height, imageHeight);
return true;
}
if (target != GL_TEXTURE_CUBE_MAP) {
/* Cube map error checking was done above */
if (zoffset + depth > imageDepth) {
_mesa_error(ctx, GL_INVALID_VALUE,
"%s(zoffset %d + depth %d > %u)",
caller, zoffset, depth, imageDepth);
return true;
}
}
/* Extra checks for compressed textures */
if (texImage) {
GLuint bw, bh, bd;
_mesa_get_format_block_size_3d(texImage->TexFormat, &bw, &bh, &bd);
if (bw > 1 || bh > 1 || bd > 1) {
/* offset must be multiple of block size */
if (xoffset % bw != 0) {
_mesa_error(ctx, GL_INVALID_VALUE,
"%s(xoffset = %d)", caller, xoffset);
return true;
}
if (target != GL_TEXTURE_1D && target != GL_TEXTURE_1D_ARRAY) {
if (yoffset % bh != 0) {
_mesa_error(ctx, GL_INVALID_VALUE,
"%s(yoffset = %d)", caller, yoffset);
return true;
}
}
if (zoffset % bd != 0) {
_mesa_error(ctx, GL_INVALID_VALUE,
"%s(zoffset = %d)", caller, zoffset);
return true;
}
/* The size must be a multiple of bw x bh x bd, or we must be using a
* offset+size that exactly hits the edge of the image.
*/
if ((width % bw != 0) &&
(xoffset + width != (GLint) texImage->Width)) {
_mesa_error(ctx, GL_INVALID_VALUE,
"%s(width = %d)", caller, width);
return true;
}
if ((height % bh != 0) &&
(yoffset + height != (GLint) texImage->Height)) {
_mesa_error(ctx, GL_INVALID_VALUE,
"%s(height = %d)", caller, height);
return true;
}
if ((depth % bd != 0) &&
(zoffset + depth != (GLint) texImage->Depth)) {
_mesa_error(ctx, GL_INVALID_VALUE,
"%s(depth = %d)", caller, depth);
return true;
}
}
}
if (width == 0 || height == 0 || depth == 0) {
/* Not an error, but nothing to do. Return 'true' so that the
* caller simply returns.
*/
return true;
}
return false;
}
/**
* Do PBO-related error checking for getting uncompressed images.
* \return true if there was an error (or the GetTexImage is to be a no-op)
*/
static bool
pbo_error_check(struct gl_context *ctx, GLenum target,
GLsizei width, GLsizei height, GLsizei depth,
GLenum format, GLenum type, GLsizei clientMemSize,
GLvoid *pixels,
const char *caller)
{
const GLuint dimensions = (target == GL_TEXTURE_3D) ? 3 : 2;
if (!_mesa_validate_pbo_access(dimensions, &ctx->Pack, width, height, depth,
format, type, clientMemSize, pixels)) {
if (_mesa_is_bufferobj(ctx->Pack.BufferObj)) {
_mesa_error(ctx, GL_INVALID_OPERATION,
"%s(out of bounds PBO access)", caller);
} else {
_mesa_error(ctx, GL_INVALID_OPERATION,
"%s(out of bounds access: bufSize (%d) is too small)",
caller, clientMemSize);
}
return true;
}
if (_mesa_is_bufferobj(ctx->Pack.BufferObj)) {
/* PBO should not be mapped */
if (_mesa_check_disallowed_mapping(ctx->Pack.BufferObj)) {
_mesa_error(ctx, GL_INVALID_OPERATION,
"%s(PBO is mapped)", caller);
return true;
}
}
if (!_mesa_is_bufferobj(ctx->Pack.BufferObj) && !pixels) {
/* not an error, do nothing */
return true;
}
return false;
}
/**
* Do teximage-related error checking for getting uncompressed images.
* \return true if there was an error
*/
static bool
teximage_error_check(struct gl_context *ctx,
struct gl_texture_image *texImage,
GLenum format, const char *caller)
{
GLenum baseFormat;
assert(texImage);
/*
* Format and type checking has been moved up to GetnTexImage and
* GetTextureImage so that it happens before getting the texImage object.
*/
baseFormat = _mesa_get_format_base_format(texImage->TexFormat);
/* Make sure the requested image format is compatible with the
* texture's format.
*/
if (_mesa_is_color_format(format)
&& !_mesa_is_color_format(baseFormat)) {
_mesa_error(ctx, GL_INVALID_OPERATION,
"%s(format mismatch)", caller);
return true;
}
else if (_mesa_is_depth_format(format)
&& !_mesa_is_depth_format(baseFormat)
&& !_mesa_is_depthstencil_format(baseFormat)) {
_mesa_error(ctx, GL_INVALID_OPERATION,
"%s(format mismatch)", caller);
return true;
}
else if (_mesa_is_stencil_format(format)
&& !ctx->Extensions.ARB_texture_stencil8) {
_mesa_error(ctx, GL_INVALID_ENUM,
"%s(format=GL_STENCIL_INDEX)", caller);
return true;
}
else if (_mesa_is_stencil_format(format)
&& !_mesa_is_depthstencil_format(baseFormat)
&& !_mesa_is_stencil_format(baseFormat)) {
_mesa_error(ctx, GL_INVALID_OPERATION,
"%s(format mismatch)", caller);
return true;
}
else if (_mesa_is_ycbcr_format(format)
&& !_mesa_is_ycbcr_format(baseFormat)) {
_mesa_error(ctx, GL_INVALID_OPERATION,
"%s(format mismatch)", caller);
return true;
}
else if (_mesa_is_depthstencil_format(format)
&& !_mesa_is_depthstencil_format(baseFormat)) {
_mesa_error(ctx, GL_INVALID_OPERATION,
"%s(format mismatch)", caller);
return true;
}
else if (!_mesa_is_stencil_format(format) &&
_mesa_is_enum_format_integer(format) !=
_mesa_is_format_integer(texImage->TexFormat)) {
_mesa_error(ctx, GL_INVALID_OPERATION,
"%s(format mismatch)", caller);
return true;
}
return false;
}
/**
* Do common teximage-related error checking for getting uncompressed images.
* \return true if there was an error
*/
static bool
common_error_check(struct gl_context *ctx,
struct gl_texture_object *texObj,
GLenum target, GLint level,
GLsizei width, GLsizei height, GLsizei depth,
GLenum format, GLenum type, GLsizei bufSize,
GLvoid *pixels, const char *caller)
{
GLenum err;
GLint maxLevels;
if (texObj->Target == 0) {
_mesa_error(ctx, GL_INVALID_OPERATION, "%s(invalid texture)", caller);
return true;
}
maxLevels = _mesa_max_texture_levels(ctx, target);
if (level < 0 || level >= maxLevels) {
_mesa_error(ctx, GL_INVALID_VALUE, "%s(level = %d)", caller, level);
return true;
}
err = _mesa_error_check_format_and_type(ctx, format, type);
if (err != GL_NO_ERROR) {
_mesa_error(ctx, err, "%s(format/type)", caller);
return true;
}
/* According to OpenGL 4.6 spec, section 8.11.4 ("Texture Image Queries"):
*
* "An INVALID_OPERATION error is generated by GetTextureImage if the
* effective target is TEXTURE_CUBE_MAP or TEXTURE_CUBE_MAP_ARRAY ,
* and the texture object is not cube complete or cube array complete,
* respectively."
*
* This applies also to GetTextureSubImage, GetCompressedTexImage,
* GetCompressedTextureImage, and GetnCompressedTexImage.
*/
if (target == GL_TEXTURE_CUBE_MAP && !_mesa_cube_complete(texObj)) {
_mesa_error(ctx, GL_INVALID_OPERATION,
"%s(cube incomplete)", caller);
return true;
}
return false;
}
/**
* Do error checking for all (non-compressed) get-texture-image functions.
* \return true if any error, false if no errors.
*/
static bool
getteximage_error_check(struct gl_context *ctx,
struct gl_texture_object *texObj,
GLenum target, GLint level,
GLsizei width, GLsizei height, GLsizei depth,
GLenum format, GLenum type, GLsizei bufSize,
GLvoid *pixels, const char *caller)
{
struct gl_texture_image *texImage;
assert(texObj);
if (common_error_check(ctx, texObj, target, level, width, height, depth,
format, type, bufSize, pixels, caller)) {
return true;
}
if (width == 0 || height == 0 || depth == 0) {
/* Not an error, but nothing to do. Return 'true' so that the
* caller simply returns.
*/
return true;
}
if (pbo_error_check(ctx, target, width, height, depth,
format, type, bufSize, pixels, caller)) {
return true;
}
texImage = select_tex_image(texObj, target, level, 0);
if (teximage_error_check(ctx, texImage, format, caller)) {
return true;
}
return false;
}
/**
* Do error checking for all (non-compressed) get-texture-image functions.
* \return true if any error, false if no errors.
*/
static bool
gettexsubimage_error_check(struct gl_context *ctx,
struct gl_texture_object *texObj,
GLenum target, GLint level,
GLint xoffset, GLint yoffset, GLint zoffset,
GLsizei width, GLsizei height, GLsizei depth,
GLenum format, GLenum type, GLsizei bufSize,
GLvoid *pixels, const char *caller)
{
struct gl_texture_image *texImage;
assert(texObj);
if (common_error_check(ctx, texObj, target, level, width, height, depth,
format, type, bufSize, pixels, caller)) {
return true;
}
if (dimensions_error_check(ctx, texObj, target, level,
xoffset, yoffset, zoffset,
width, height, depth, caller)) {
return true;
}
if (pbo_error_check(ctx, target, width, height, depth,
format, type, bufSize, pixels, caller)) {
return true;
}
texImage = select_tex_image(texObj, target, level, zoffset);
if (teximage_error_check(ctx, texImage, format, caller)) {
return true;
}
return false;
}
/**
* Return the width, height and depth of a texture image.
* This function must be resilient to bad parameter values since
* this is called before full error checking.
*/
static void
get_texture_image_dims(const struct gl_texture_object *texObj,
GLenum target, GLint level,
GLsizei *width, GLsizei *height, GLsizei *depth)
{
const struct gl_texture_image *texImage = NULL;
if (level >= 0 && level < MAX_TEXTURE_LEVELS) {
texImage = _mesa_select_tex_image(texObj, target, level);
}
if (texImage) {
*width = texImage->Width;
*height = texImage->Height;
if (target == GL_TEXTURE_CUBE_MAP) {
*depth = 6;
}
else {
*depth = texImage->Depth;
}
}
else {
*width = *height = *depth = 0;
}
}
/**
* Common code for all (uncompressed) get-texture-image functions.
* \param texObj the texture object (should not be null)
* \param target user-provided target, or 0 for DSA
* \param level image level.
* \param format pixel data format for returned image.
* \param type pixel data type for returned image.
* \param bufSize size of the pixels data buffer.
* \param pixels returned pixel data.
* \param caller name of calling function
*/
static void
get_texture_image(struct gl_context *ctx,
struct gl_texture_object *texObj,
GLenum target, GLint level,
GLint xoffset, GLint yoffset, GLint zoffset,
GLsizei width, GLsizei height, GLint depth,
GLenum format, GLenum type,
GLvoid *pixels, const char *caller)
{
struct gl_texture_image *texImage;
unsigned firstFace, numFaces, i;
GLint imageStride;
FLUSH_VERTICES(ctx, 0);
texImage = select_tex_image(texObj, target, level, zoffset);
assert(texImage); /* should have been error checked already */
if (_mesa_is_zero_size_texture(texImage)) {
/* no image data to return */
return;
}
if (MESA_VERBOSE & (VERBOSE_API | VERBOSE_TEXTURE)) {
_mesa_debug(ctx, "%s(tex %u) format = %s, w=%d, h=%d,"
" dstFmt=0x%x, dstType=0x%x\n",
caller, texObj->Name,
_mesa_get_format_name(texImage->TexFormat),
texImage->Width, texImage->Height,
format, type);
}
if (target == GL_TEXTURE_CUBE_MAP) {
/* Compute stride between cube faces */
imageStride = _mesa_image_image_stride(&ctx->Pack, width, height,
format, type);
firstFace = zoffset;
numFaces = depth;
zoffset = 0;
depth = 1;
}
else {
imageStride = 0;
firstFace = _mesa_tex_target_to_face(target);
numFaces = 1;
}
_mesa_lock_texture(ctx, texObj);
for (i = 0; i < numFaces; i++) {
texImage = texObj->Image[firstFace + i][level];
assert(texImage);
ctx->Driver.GetTexSubImage(ctx, xoffset, yoffset, zoffset,
width, height, depth,
format, type, pixels, texImage);
/* next cube face */
pixels = (GLubyte *) pixels + imageStride;
}
_mesa_unlock_texture(ctx, texObj);
}
static void
_get_texture_image(struct gl_context *ctx,
struct gl_texture_object *texObj,
GLenum target, GLint level,
GLenum format, GLenum type,
GLsizei bufSize, GLvoid *pixels,
const char *caller)
{
GLsizei width, height, depth;
/* EXT/ARB direct_state_access variants don't call _get_texture_image
* with a NULL texObj */
bool is_dsa = texObj != NULL;
if (!is_dsa) {
texObj = _mesa_get_current_tex_object(ctx, target);
assert(texObj);
}
get_texture_image_dims(texObj, target, level, &width, &height, &depth);
if (getteximage_error_check(ctx, texObj, target, level,
width, height, depth,
format, type, bufSize, pixels, caller)) {
return;
}
get_texture_image(ctx, texObj, target, level,
0, 0, 0, width, height, depth,
format, type, pixels, caller);
}
void GLAPIENTRY
_mesa_GetnTexImageARB(GLenum target, GLint level, GLenum format, GLenum type,
GLsizei bufSize, GLvoid *pixels)
{
GET_CURRENT_CONTEXT(ctx);
static const char *caller = "glGetnTexImageARB";
if (!legal_getteximage_target(ctx, target, false)) {
_mesa_error(ctx, GL_INVALID_ENUM, "%s", caller);
return;
}
_get_texture_image(ctx, NULL, target, level, format, type,
bufSize, pixels, caller);
}
void GLAPIENTRY
_mesa_GetTexImage(GLenum target, GLint level, GLenum format, GLenum type,
GLvoid *pixels )
{
GET_CURRENT_CONTEXT(ctx);
static const char *caller = "glGetTexImage";
if (!legal_getteximage_target(ctx, target, false)) {
_mesa_error(ctx, GL_INVALID_ENUM, "%s", caller);
return;
}
_get_texture_image(ctx, NULL, target, level, format, type,
INT_MAX, pixels, caller);
}
void GLAPIENTRY
_mesa_GetTextureImage(GLuint texture, GLint level, GLenum format, GLenum type,
GLsizei bufSize, GLvoid *pixels)
{
GET_CURRENT_CONTEXT(ctx);
static const char *caller = "glGetTextureImage";
struct gl_texture_object *texObj =
_mesa_lookup_texture_err(ctx, texture, caller);
if (!texObj) {
return;
}
if (!legal_getteximage_target(ctx, texObj->Target, true)) {
_mesa_error(ctx, GL_INVALID_OPERATION, "%s", caller);
return;
}
_get_texture_image(ctx, texObj, texObj->Target, level, format, type,
bufSize, pixels, caller);
}
void GLAPIENTRY
_mesa_GetTextureImageEXT(GLuint texture, GLenum target, GLint level,
GLenum format, GLenum type, GLvoid *pixels)
{
GET_CURRENT_CONTEXT(ctx);
static const char *caller = "glGetTextureImageEXT";
struct gl_texture_object *texObj =
_mesa_lookup_or_create_texture(ctx, target, texture,
false, true, caller);
if (!texObj) {
return;
}
if (!legal_getteximage_target(ctx, target, true)) {
_mesa_error(ctx, GL_INVALID_ENUM, "%s", caller);
return;
}
_get_texture_image(ctx, texObj, target, level, format, type,
INT_MAX, pixels, caller);
}
void GLAPIENTRY
_mesa_GetMultiTexImageEXT(GLenum texunit, GLenum target, GLint level,
GLenum format, GLenum type, GLvoid *pixels)
{
GET_CURRENT_CONTEXT(ctx);
GLsizei width, height, depth;
static const char *caller = "glGetMultiTexImageEXT";
struct gl_texture_object *texObj =
_mesa_get_texobj_by_target_and_texunit(ctx, target,
texunit - GL_TEXTURE0,
false,
caller);
if (!texObj) {
return;
}
if (!legal_getteximage_target(ctx, texObj->Target, true)) {
_mesa_error(ctx, GL_INVALID_OPERATION, "%s", caller);
return;
}
get_texture_image_dims(texObj, texObj->Target, level,
&width, &height, &depth);
if (getteximage_error_check(ctx, texObj, texObj->Target, level,
width, height, depth,
format, type, INT_MAX, pixels, caller)) {
return;
}
get_texture_image(ctx, texObj, texObj->Target, level,
0, 0, 0, width, height, depth,
format, type, pixels, caller);
}
void GLAPIENTRY
_mesa_GetTextureSubImage(GLuint texture, GLint level,
GLint xoffset, GLint yoffset, GLint zoffset,
GLsizei width, GLsizei height, GLsizei depth,
GLenum format, GLenum type, GLsizei bufSize,
void *pixels)
{
GET_CURRENT_CONTEXT(ctx);
static const char *caller = "glGetTextureSubImage";
struct gl_texture_object *texObj =
_mesa_lookup_texture_err(ctx, texture, caller);
if (!texObj) {
return;
}
if (!legal_getteximage_target(ctx, texObj->Target, true)) {
_mesa_error(ctx, GL_INVALID_OPERATION,
"%s(buffer/multisample texture)", caller);
return;
}
if (gettexsubimage_error_check(ctx, texObj, texObj->Target, level,
xoffset, yoffset, zoffset,
width, height, depth,
format, type, bufSize, pixels, caller)) {
return;
}
get_texture_image(ctx, texObj, texObj->Target, level,
xoffset, yoffset, zoffset, width, height, depth,
format, type, pixels, caller);
}
/**
* Compute the number of bytes which will be written when retrieving
* a sub-region of a compressed texture.
*/
static GLsizei
packed_compressed_size(GLuint dimensions, mesa_format format,
GLsizei width, GLsizei height, GLsizei depth,
const struct gl_pixelstore_attrib *packing)
{
struct compressed_pixelstore st;
GLsizei totalBytes;
_mesa_compute_compressed_pixelstore(dimensions, format,
width, height, depth,
packing, &st);
totalBytes =
(st.CopySlices - 1) * st.TotalRowsPerSlice * st.TotalBytesPerRow +
st.SkipBytes +
(st.CopyRowsPerSlice - 1) * st.TotalBytesPerRow +
st.CopyBytesPerRow;
return totalBytes;
}
/**
* Do error checking for getting compressed texture images.
* \return true if any error, false if no errors.
*/
static bool
getcompressedteximage_error_check(struct gl_context *ctx,
struct gl_texture_object *texObj,
GLenum target, GLint level,
GLint xoffset, GLint yoffset, GLint zoffset,
GLsizei width, GLsizei height, GLsizei depth,
GLsizei bufSize, GLvoid *pixels,
const char *caller)
{
struct gl_texture_image *texImage;
GLint maxLevels;
GLsizei totalBytes;
GLuint dimensions;
assert(texObj);
if (texObj->Target == 0) {
_mesa_error(ctx, GL_INVALID_OPERATION, "%s(invalid texture)", caller);
return true;
}
maxLevels = _mesa_max_texture_levels(ctx, target);
if (level < 0 || level >= maxLevels) {
_mesa_error(ctx, GL_INVALID_VALUE,
"%s(bad level = %d)", caller, level);
return true;
}
if (dimensions_error_check(ctx, texObj, target, level,
xoffset, yoffset, zoffset,
width, height, depth, caller)) {
return true;
}
texImage = select_tex_image(texObj, target, level, zoffset);
assert(texImage);
if (!_mesa_is_format_compressed(texImage->TexFormat)) {
_mesa_error(ctx, GL_INVALID_OPERATION,
"%s(texture is not compressed)", caller);
return true;
}
/* Check for invalid pixel storage modes */
dimensions = _mesa_get_texture_dimensions(texObj->Target);
if (!_mesa_compressed_pixel_storage_error_check(ctx, dimensions,
&ctx->Pack,
caller)) {
return true;
}
/* Compute number of bytes that may be touched in the dest buffer */
totalBytes = packed_compressed_size(dimensions, texImage->TexFormat,
width, height, depth,
&ctx->Pack);
/* Do dest buffer bounds checking */
if (_mesa_is_bufferobj(ctx->Pack.BufferObj)) {
/* do bounds checking on PBO write */
if ((GLubyte *) pixels + totalBytes >
(GLubyte *) ctx->Pack.BufferObj->Size) {
_mesa_error(ctx, GL_INVALID_OPERATION,
"%s(out of bounds PBO access)", caller);
return true;
}
/* make sure PBO is not mapped */
if (_mesa_check_disallowed_mapping(ctx->Pack.BufferObj)) {
_mesa_error(ctx, GL_INVALID_OPERATION, "%s(PBO is mapped)", caller);
return true;
}
}
else {
/* do bounds checking on writing to client memory */
if (totalBytes > bufSize) {
_mesa_error(ctx, GL_INVALID_OPERATION,
"%s(out of bounds access: bufSize (%d) is too small)",
caller, bufSize);
return true;
}
}
if (!_mesa_is_bufferobj(ctx->Pack.BufferObj) && !pixels) {
/* not an error, but do nothing */
return true;
}
return false;
}
/**
* Common helper for all glGetCompressed-teximage functions.
*/
static void
get_compressed_texture_image(struct gl_context *ctx,
struct gl_texture_object *texObj,
GLenum target, GLint level,
GLint xoffset, GLint yoffset, GLint zoffset,
GLsizei width, GLsizei height, GLint depth,
GLvoid *pixels,
const char *caller)
{
struct gl_texture_image *texImage;
unsigned firstFace, numFaces, i, imageStride;
FLUSH_VERTICES(ctx, 0);
texImage = select_tex_image(texObj, target, level, zoffset);
assert(texImage); /* should have been error checked already */
if (_mesa_is_zero_size_texture(texImage))
return;
if (MESA_VERBOSE & (VERBOSE_API | VERBOSE_TEXTURE)) {
_mesa_debug(ctx,
"%s(tex %u) format = %s, w=%d, h=%d\n",
caller, texObj->Name,
_mesa_get_format_name(texImage->TexFormat),
texImage->Width, texImage->Height);
}
if (target == GL_TEXTURE_CUBE_MAP) {
struct compressed_pixelstore store;
/* Compute image stride between cube faces */
_mesa_compute_compressed_pixelstore(2, texImage->TexFormat,
width, height, depth,
&ctx->Pack, &store);
imageStride = store.TotalBytesPerRow * store.TotalRowsPerSlice;
firstFace = zoffset;
numFaces = depth;
zoffset = 0;
depth = 1;
}
else {
imageStride = 0;
firstFace = _mesa_tex_target_to_face(target);
numFaces = 1;
}
_mesa_lock_texture(ctx, texObj);
for (i = 0; i < numFaces; i++) {
texImage = texObj->Image[firstFace + i][level];
assert(texImage);
get_compressed_texsubimage_sw(ctx, texImage,
xoffset, yoffset, zoffset,
width, height, depth, pixels);
/* next cube face */
pixels = (GLubyte *) pixels + imageStride;
}
_mesa_unlock_texture(ctx, texObj);
}
void GLAPIENTRY
_mesa_GetnCompressedTexImageARB(GLenum target, GLint level, GLsizei bufSize,
GLvoid *pixels)
{
GET_CURRENT_CONTEXT(ctx);
static const char *caller = "glGetnCompressedTexImageARB";
GLsizei width, height, depth;
struct gl_texture_object *texObj;
if (!legal_getteximage_target(ctx, target, false)) {
_mesa_error(ctx, GL_INVALID_ENUM, "%s", caller);
return;
}
texObj = _mesa_get_current_tex_object(ctx, target);
assert(texObj);
get_texture_image_dims(texObj, target, level, &width, &height, &depth);
if (getcompressedteximage_error_check(ctx, texObj, target, level,
0, 0, 0, width, height, depth,
INT_MAX, pixels, caller)) {
return;
}
get_compressed_texture_image(ctx, texObj, target, level,
0, 0, 0, width, height, depth,
pixels, caller);
}
void GLAPIENTRY
_mesa_GetCompressedTexImage(GLenum target, GLint level, GLvoid *pixels)
{
GET_CURRENT_CONTEXT(ctx);
static const char *caller = "glGetCompressedTexImage";
GLsizei width, height, depth;
struct gl_texture_object *texObj;
if (!legal_getteximage_target(ctx, target, false)) {
_mesa_error(ctx, GL_INVALID_ENUM, "%s", caller);
return;
}
texObj = _mesa_get_current_tex_object(ctx, target);
assert(texObj);
get_texture_image_dims(texObj, target, level,
&width, &height, &depth);
if (getcompressedteximage_error_check(ctx, texObj, target, level,
0, 0, 0, width, height, depth,
INT_MAX, pixels, caller)) {
return;
}
get_compressed_texture_image(ctx, texObj, target, level,
0, 0, 0, width, height, depth,
pixels, caller);
}
void GLAPIENTRY
_mesa_GetCompressedTextureImageEXT(GLuint texture, GLenum target, GLint level,
GLvoid *pixels)
{
GET_CURRENT_CONTEXT(ctx);
struct gl_texture_object* texObj;
GLsizei width, height, depth;
static const char *caller = "glGetCompressedTextureImageEXT";
texObj = _mesa_lookup_or_create_texture(ctx, target, texture,
false, true, caller);
get_texture_image_dims(texObj, texObj->Target, level,
&width, &height, &depth);
if (getcompressedteximage_error_check(ctx, texObj, texObj->Target, level,
0, 0, 0, width, height, depth,
INT_MAX, pixels, caller)) {
return;
}
get_compressed_texture_image(ctx, texObj, texObj->Target, level,
0, 0, 0, width, height, depth,
pixels, caller);
}
void GLAPIENTRY
_mesa_GetCompressedMultiTexImageEXT(GLenum texunit, GLenum target, GLint level,
GLvoid *pixels)
{
GET_CURRENT_CONTEXT(ctx);
struct gl_texture_object* texObj;
GLsizei width, height, depth;
static const char *caller = "glGetCompressedMultiTexImageEXT";
texObj = _mesa_get_texobj_by_target_and_texunit(ctx, target,
texunit - GL_TEXTURE0,
false,
caller);
get_texture_image_dims(texObj, texObj->Target, level,
&width, &height, &depth);
if (getcompressedteximage_error_check(ctx, texObj, texObj->Target, level,
0, 0, 0, width, height, depth,
INT_MAX, pixels, caller)) {
return;
}
get_compressed_texture_image(ctx, texObj, texObj->Target, level,
0, 0, 0, width, height, depth,
pixels, caller);
}
void GLAPIENTRY
_mesa_GetCompressedTextureImage(GLuint texture, GLint level,
GLsizei bufSize, GLvoid *pixels)
{
GET_CURRENT_CONTEXT(ctx);
static const char *caller = "glGetCompressedTextureImage";
GLsizei width, height, depth;
struct gl_texture_object *texObj =
_mesa_lookup_texture_err(ctx, texture, caller);
if (!texObj) {
return;
}
get_texture_image_dims(texObj, texObj->Target, level,
&width, &height, &depth);
if (getcompressedteximage_error_check(ctx, texObj, texObj->Target, level,
0, 0, 0, width, height, depth,
bufSize, pixels, caller)) {
return;
}
get_compressed_texture_image(ctx, texObj, texObj->Target, level,
0, 0, 0, width, height, depth,
pixels, caller);
}
void GLAPIENTRY
_mesa_GetCompressedTextureSubImage(GLuint texture, GLint level,
GLint xoffset, GLint yoffset,
GLint zoffset, GLsizei width,
GLsizei height, GLsizei depth,
GLsizei bufSize, void *pixels)
{
GET_CURRENT_CONTEXT(ctx);
static const char *caller = "glGetCompressedTextureImage";
struct gl_texture_object *texObj = NULL;
texObj = _mesa_lookup_texture_err(ctx, texture, caller);
if (!texObj) {
return;
}
if (getcompressedteximage_error_check(ctx, texObj, texObj->Target, level,
xoffset, yoffset, zoffset,
width, height, depth,
bufSize, pixels, caller)) {
return;
}
get_compressed_texture_image(ctx, texObj, texObj->Target, level,
xoffset, yoffset, zoffset,
width, height, depth,
pixels, caller);
}
|