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
|
/*
* 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 "texcompress.h"
#include "texgetimage.h"
#include "teximage.h"
#include "texstore.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,
GLenum format, GLenum type, GLvoid *pixels,
struct gl_texture_image *texImage)
{
const GLint width = texImage->Width;
const GLint height = texImage->Height;
const GLint depth = texImage->Depth;
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, img,
0, 0, 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, 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,
GLenum format, GLenum type, GLvoid *pixels,
struct gl_texture_image *texImage)
{
const GLint width = texImage->Width;
const GLint height = texImage->Height;
const GLint depth = texImage->Depth;
GLint img, row;
assert(format == GL_DEPTH_STENCIL);
assert(type == GL_UNSIGNED_INT_24_8 ||
type == GL_FLOAT_32_UNSIGNED_INT_24_8_REV);
for (img = 0; img < depth; img++) {
GLubyte *srcMap;
GLint rowstride;
/* map src texture buffer */
ctx->Driver.MapTextureImage(ctx, texImage, img,
0, 0, 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_depth_stencil_row(texImage->TexFormat,
width,
(const GLuint *) src,
type, dest);
if (ctx->Pack.SwapBytes) {
_mesa_swap4((GLuint *) dest, width);
}
}
ctx->Driver.UnmapTextureImage(ctx, texImage, 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,
GLenum format, GLenum type, GLvoid *pixels,
struct gl_texture_image *texImage)
{
const GLint width = texImage->Width;
const GLint height = texImage->Height;
const GLint depth = texImage->Depth;
GLint img, row;
for (img = 0; img < depth; img++) {
GLubyte *srcMap;
GLint rowstride;
/* map src texture buffer */
ctx->Driver.MapTextureImage(ctx, texImage, img,
0, 0, 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, img);
}
else {
_mesa_error(ctx, GL_OUT_OF_MEMORY, "glGetTexImage");
break;
}
}
}
/**
* Get a color texture image with decompression.
*/
static void
get_tex_rgba_compressed(struct gl_context *ctx, GLuint dimensions,
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);
const GLenum destBaseFormat = _mesa_base_tex_format(ctx, format);
GLenum rebaseFormat = GL_NONE;
const GLuint width = texImage->Width;
const GLuint height = texImage->Height;
const GLuint depth = texImage->Depth;
GLfloat *tempImage, *tempSlice, *srcRow;
GLuint row, slice;
/* 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, slice,
0, 0, width, height,
GL_MAP_READ_BIT,
&srcMap, &srcRowStride);
if (srcMap) {
_mesa_decompress_image(texFormat, width, height,
srcMap, srcRowStride, tempSlice);
ctx->Driver.UnmapTextureImage(ctx, texImage, slice);
}
else {
_mesa_error(ctx, GL_OUT_OF_MEMORY, "glGetTexImage");
free(tempImage);
return;
}
}
if (baseFormat == GL_LUMINANCE ||
baseFormat == GL_INTENSITY ||
baseFormat == GL_LUMINANCE_ALPHA) {
/* If a luminance (or intensity) texture is read back as RGB(A), the
* returned value should be (L,0,0,1), not (L,L,L,1). Set rebaseFormat
* here to get G=B=0.
*/
rebaseFormat = texImage->_BaseFormat;
}
else if ((baseFormat == GL_RGBA ||
baseFormat == GL_RGB ||
baseFormat == GL_RG) &&
(destBaseFormat == GL_LUMINANCE ||
destBaseFormat == GL_LUMINANCE_ALPHA ||
destBaseFormat == GL_LUMINANCE_INTEGER_EXT ||
destBaseFormat == GL_LUMINANCE_ALPHA_INTEGER_EXT)) {
/* If we're reading back an RGB(A) texture as luminance then we need
* to return L=tex(R). Note, that's different from glReadPixels which
* returns L=R+G+B.
*/
rebaseFormat = GL_LUMINANCE_ALPHA; /* this covers GL_LUMINANCE too */
}
if (rebaseFormat) {
_mesa_rebase_rgba_float(width * height, (GLfloat (*)[4]) tempImage,
rebaseFormat);
}
tempSlice = tempImage;
for (slice = 0; slice < depth; slice++) {
srcRow = tempSlice;
for (row = 0; row < height; row++) {
void *dest = _mesa_image_address(dimensions, &ctx->Pack, pixels,
width, height, format, type,
slice, row, 0);
_mesa_pack_rgba_span_float(ctx, width, (GLfloat (*)[4]) srcRow,
format, type, dest, &ctx->Pack, transferOps);
srcRow += 4 * width;
}
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,
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 GLuint width = texImage->Width;
GLenum destBaseFormat = _mesa_base_pack_format(format);
GLenum rebaseFormat = GL_NONE;
GLuint height = texImage->Height;
GLuint depth = texImage->Depth;
GLuint img, row;
GLfloat (*rgba)[4];
GLuint (*rgba_uint)[4];
GLboolean tex_is_integer = _mesa_is_format_integer_color(texImage->TexFormat);
GLboolean tex_is_uint = _mesa_is_format_unsigned(texImage->TexFormat);
GLenum texBaseFormat = _mesa_get_format_base_format(texImage->TexFormat);
/* Allocate buffer for one row of texels */
rgba = malloc(4 * width * sizeof(GLfloat));
rgba_uint = (GLuint (*)[4]) rgba;
if (!rgba) {
_mesa_error(ctx, GL_OUT_OF_MEMORY, "glGetTexImage()");
return;
}
if (texImage->TexObject->Target == GL_TEXTURE_1D_ARRAY) {
depth = height;
height = 1;
}
if (texImage->_BaseFormat == GL_LUMINANCE ||
texImage->_BaseFormat == GL_INTENSITY ||
texImage->_BaseFormat == GL_LUMINANCE_ALPHA) {
/* If a luminance (or intensity) texture is read back as RGB(A), the
* returned value should be (L,0,0,1), not (L,L,L,1). Set rebaseFormat
* here to get G=B=0.
*/
rebaseFormat = texImage->_BaseFormat;
}
else if ((texImage->_BaseFormat == GL_RGBA ||
texImage->_BaseFormat == GL_RGB ||
texImage->_BaseFormat == GL_RG) &&
(destBaseFormat == GL_LUMINANCE ||
destBaseFormat == GL_LUMINANCE_ALPHA ||
destBaseFormat == GL_LUMINANCE_INTEGER_EXT ||
destBaseFormat == GL_LUMINANCE_ALPHA_INTEGER_EXT)) {
/* If we're reading back an RGB(A) texture as luminance then we need
* to return L=tex(R). Note, that's different from glReadPixels which
* returns L=R+G+B.
*/
rebaseFormat = GL_LUMINANCE_ALPHA; /* this covers GL_LUMINANCE too */
}
else if (texImage->_BaseFormat != texBaseFormat) {
/* The internal format and the real format differ, so we can't rely
* on the unpack functions setting the correct constant values.
* (e.g. reading back GL_RGB8 which is actually RGBA won't set alpha=1)
*/
switch (texImage->_BaseFormat) {
case GL_RED:
if ((texBaseFormat == GL_RGBA ||
texBaseFormat == GL_RGB ||
texBaseFormat == GL_RG) &&
(destBaseFormat == GL_RGBA ||
destBaseFormat == GL_RGB ||
destBaseFormat == GL_RG ||
destBaseFormat == GL_GREEN)) {
rebaseFormat = texImage->_BaseFormat;
break;
}
/* fall through */
case GL_RG:
if ((texBaseFormat == GL_RGBA ||
texBaseFormat == GL_RGB) &&
(destBaseFormat == GL_RGBA ||
destBaseFormat == GL_RGB ||
destBaseFormat == GL_BLUE)) {
rebaseFormat = texImage->_BaseFormat;
break;
}
/* fall through */
case GL_RGB:
if (texBaseFormat == GL_RGBA &&
(destBaseFormat == GL_RGBA ||
destBaseFormat == GL_ALPHA ||
destBaseFormat == GL_LUMINANCE_ALPHA)) {
rebaseFormat = texImage->_BaseFormat;
}
break;
case GL_ALPHA:
if (destBaseFormat != GL_ALPHA) {
rebaseFormat = texImage->_BaseFormat;
}
break;
}
}
for (img = 0; img < depth; img++) {
GLubyte *srcMap;
GLint rowstride;
/* map src texture buffer */
ctx->Driver.MapTextureImage(ctx, texImage, img,
0, 0, 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);
if (tex_is_integer) {
_mesa_unpack_uint_rgba_row(texFormat, width, src, rgba_uint);
if (rebaseFormat)
_mesa_rebase_rgba_uint(width, rgba_uint, rebaseFormat);
if (tex_is_uint) {
_mesa_pack_rgba_span_from_uints(ctx, width,
(GLuint (*)[4]) rgba_uint,
format, type, dest);
} else {
_mesa_pack_rgba_span_from_ints(ctx, width,
(GLint (*)[4]) rgba_uint,
format, type, dest);
}
} else {
_mesa_unpack_rgba_row(texFormat, width, src, rgba);
if (rebaseFormat)
_mesa_rebase_rgba_float(width, rgba, rebaseFormat);
_mesa_pack_rgba_span_float(ctx, width, (GLfloat (*)[4]) rgba,
format, type, dest,
&ctx->Pack, transferOps);
}
}
/* Unmap the src texture buffer */
ctx->Driver.UnmapTextureImage(ctx, texImage, img);
}
else {
_mesa_error(ctx, GL_OUT_OF_MEMORY, "glGetTexImage");
break;
}
}
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,
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, format, type,
pixels, texImage, transferOps);
}
else {
get_tex_rgba_uncompressed(ctx, dimensions, 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, 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);
}
if (memCopy) {
const GLuint bpp = _mesa_get_format_bytes(texImage->TexFormat);
const GLuint bytesPerRow = texImage->Width * bpp;
GLubyte *dst =
_mesa_image_address2d(&ctx->Pack, pixels, texImage->Width,
texImage->Height, format, type, 0, 0);
const GLint dstRowStride =
_mesa_image_row_stride(&ctx->Pack, texImage->Width, format, type);
GLubyte *src;
GLint srcRowStride;
/* map src texture buffer */
ctx->Driver.MapTextureImage(ctx, texImage, 0,
0, 0, texImage->Width, texImage->Height,
GL_MAP_READ_BIT, &src, &srcRowStride);
if (src) {
if (bytesPerRow == dstRowStride && bytesPerRow == srcRowStride) {
memcpy(dst, src, bytesPerRow * texImage->Height);
}
else {
GLuint row;
for (row = 0; row < texImage->Height; row++) {
memcpy(dst, src, bytesPerRow);
dst += dstRowStride;
src += srcRowStride;
}
}
/* unmap src texture buffer */
ctx->Driver.UnmapTextureImage(ctx, texImage, 0);
}
else {
_mesa_error(ctx, GL_OUT_OF_MEMORY, "glGetTexImage");
}
}
return memCopy;
}
/**
* This is the software fallback for Driver.GetTexImage().
* 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_get_teximage(struct gl_context *ctx,
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);
}
if (get_tex_memcpy(ctx, format, type, pixels, texImage)) {
/* all done */
}
else if (format == GL_DEPTH_COMPONENT) {
get_tex_depth(ctx, dimensions, format, type, pixels, texImage);
}
else if (format == GL_DEPTH_STENCIL_EXT) {
get_tex_depth_stencil(ctx, dimensions, format, type, pixels, texImage);
}
else if (format == GL_YCBCR_MESA) {
get_tex_ycbcr(ctx, dimensions, format, type, pixels, texImage);
}
else {
get_tex_rgba(ctx, dimensions, format, type, pixels, texImage);
}
if (_mesa_is_bufferobj(ctx->Pack.BufferObj)) {
ctx->Driver.UnmapBuffer(ctx, ctx->Pack.BufferObj, MAP_INTERNAL);
}
}
/**
* This is the software fallback for Driver.GetCompressedTexImage().
* All error checking will have been done before this routine is called.
*/
void
_mesa_get_compressed_teximage(struct gl_context *ctx,
struct gl_texture_image *texImage,
GLvoid *img)
{
const GLuint dimensions =
_mesa_get_texture_dimensions(texImage->TexObject->Target);
struct compressed_pixelstore store;
GLuint i, slice;
GLubyte *dest;
_mesa_compute_compressed_pixelstore(dimensions, texImage,
texImage->Width, texImage->Height,
texImage->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, 0,
0, 0, texImage->Width, texImage->Height,
GL_MAP_READ_BIT, &src, &srcRowStride);
if (src) {
for (i = 0; i < store.CopyRowsPerSlice; i++) {
memcpy(dest, src, store.CopyBytesPerRow);
dest += store.TotalBytesPerRow;
src += srcRowStride;
}
ctx->Driver.UnmapTextureImage(ctx, texImage, 0);
/* 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 glTexImage or
* glCompressedTexImage.
*/
static GLboolean
legal_getteximage_target(struct gl_context *ctx, GLenum target)
{
switch (target) {
case GL_TEXTURE_1D:
case GL_TEXTURE_2D:
case GL_TEXTURE_3D:
return GL_TRUE;
case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB:
case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB:
case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB:
case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB:
case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB:
case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB:
return ctx->Extensions.ARB_texture_cube_map;
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;
default:
return GL_FALSE;
}
}
/**
* Do error checking for a glGetTexImage() call.
* \return GL_TRUE if any error, GL_FALSE if no errors.
*/
static GLboolean
getteximage_error_check(struct gl_context *ctx, GLenum target, GLint level,
GLenum format, GLenum type, GLsizei clientMemSize,
GLvoid *pixels )
{
struct gl_texture_object *texObj;
struct gl_texture_image *texImage;
const GLint maxLevels = _mesa_max_texture_levels(ctx, target);
const GLuint dimensions = (target == GL_TEXTURE_3D) ? 3 : 2;
GLenum baseFormat, err;
if (!legal_getteximage_target(ctx, target)) {
_mesa_error(ctx, GL_INVALID_ENUM, "glGetTexImage(target=0x%x)", target);
return GL_TRUE;
}
assert(maxLevels != 0);
if (level < 0 || level >= maxLevels) {
_mesa_error( ctx, GL_INVALID_VALUE, "glGetTexImage(level)" );
return GL_TRUE;
}
err = _mesa_error_check_format_and_type(ctx, format, type);
if (err != GL_NO_ERROR) {
_mesa_error(ctx, err, "glGetTexImage(format/type)");
return GL_TRUE;
}
texObj = _mesa_get_current_tex_object(ctx, target);
if (!texObj) {
_mesa_error(ctx, GL_INVALID_ENUM, "glGetTexImage(target)");
return GL_TRUE;
}
texImage = _mesa_select_tex_image(ctx, texObj, target, level);
if (!texImage) {
/* non-existant texture image */
return GL_TRUE;
}
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, "glGetTexImage(format mismatch)");
return GL_TRUE;
}
else if (_mesa_is_depth_format(format)
&& !_mesa_is_depth_format(baseFormat)
&& !_mesa_is_depthstencil_format(baseFormat)) {
_mesa_error(ctx, GL_INVALID_OPERATION, "glGetTexImage(format mismatch)");
return GL_TRUE;
}
else if (_mesa_is_stencil_format(format)
&& !ctx->Extensions.ARB_texture_stencil8) {
_mesa_error(ctx, GL_INVALID_ENUM, "glGetTexImage(format=GL_STENCIL_INDEX)");
return GL_TRUE;
}
else if (_mesa_is_ycbcr_format(format)
&& !_mesa_is_ycbcr_format(baseFormat)) {
_mesa_error(ctx, GL_INVALID_OPERATION, "glGetTexImage(format mismatch)");
return GL_TRUE;
}
else if (_mesa_is_depthstencil_format(format)
&& !_mesa_is_depthstencil_format(baseFormat)) {
_mesa_error(ctx, GL_INVALID_OPERATION, "glGetTexImage(format mismatch)");
return GL_TRUE;
}
else if (_mesa_is_dudv_format(format)
&& !_mesa_is_dudv_format(baseFormat)) {
_mesa_error(ctx, GL_INVALID_OPERATION, "glGetTexImage(format mismatch)");
return GL_TRUE;
}
else if (_mesa_is_enum_format_integer(format) !=
_mesa_is_format_integer(texImage->TexFormat)) {
_mesa_error(ctx, GL_INVALID_OPERATION, "glGetTexImage(format mismatch)");
return GL_TRUE;
}
if (!_mesa_validate_pbo_access(dimensions, &ctx->Pack, texImage->Width,
texImage->Height, texImage->Depth,
format, type, clientMemSize, pixels)) {
if (_mesa_is_bufferobj(ctx->Pack.BufferObj)) {
_mesa_error(ctx, GL_INVALID_OPERATION,
"glGetTexImage(out of bounds PBO access)");
} else {
_mesa_error(ctx, GL_INVALID_OPERATION,
"glGetnTexImageARB(out of bounds access:"
" bufSize (%d) is too small)", clientMemSize);
}
return GL_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,
"glGetTexImage(PBO is mapped)");
return GL_TRUE;
}
}
return GL_FALSE;
}
/**
* Get texture image. Called by glGetTexImage.
*
* \param target texture target.
* \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.
*/
void GLAPIENTRY
_mesa_GetnTexImageARB( GLenum target, GLint level, GLenum format,
GLenum type, GLsizei bufSize, GLvoid *pixels )
{
struct gl_texture_object *texObj;
struct gl_texture_image *texImage;
GET_CURRENT_CONTEXT(ctx);
FLUSH_VERTICES(ctx, 0);
if (getteximage_error_check(ctx, target, level, format, type,
bufSize, pixels)) {
return;
}
if (!_mesa_is_bufferobj(ctx->Pack.BufferObj) && !pixels) {
/* not an error, do nothing */
return;
}
texObj = _mesa_get_current_tex_object(ctx, target);
texImage = _mesa_select_tex_image(ctx, texObj, target, level);
if (_mesa_is_zero_size_texture(texImage))
return;
if (MESA_VERBOSE & (VERBOSE_API | VERBOSE_TEXTURE)) {
_mesa_debug(ctx, "glGetTexImage(tex %u) format = %s, w=%d, h=%d,"
" dstFmt=0x%x, dstType=0x%x\n",
texObj->Name,
_mesa_get_format_name(texImage->TexFormat),
texImage->Width, texImage->Height,
format, type);
}
_mesa_lock_texture(ctx, texObj);
{
ctx->Driver.GetTexImage(ctx, format, type, pixels, texImage);
}
_mesa_unlock_texture(ctx, texObj);
}
void GLAPIENTRY
_mesa_GetTexImage( GLenum target, GLint level, GLenum format,
GLenum type, GLvoid *pixels )
{
_mesa_GetnTexImageARB(target, level, format, type, INT_MAX, pixels);
}
/**
* Do error checking for a glGetCompressedTexImage() call.
* \return GL_TRUE if any error, GL_FALSE if no errors.
*/
static GLboolean
getcompressedteximage_error_check(struct gl_context *ctx, GLenum target,
GLint level, GLsizei clientMemSize, GLvoid *img)
{
struct gl_texture_object *texObj;
struct gl_texture_image *texImage;
const GLint maxLevels = _mesa_max_texture_levels(ctx, target);
GLuint compressedSize, dimensions;
if (!legal_getteximage_target(ctx, target)) {
_mesa_error(ctx, GL_INVALID_ENUM, "glGetCompressedTexImage(target=0x%x)",
target);
return GL_TRUE;
}
assert(maxLevels != 0);
if (level < 0 || level >= maxLevels) {
_mesa_error(ctx, GL_INVALID_VALUE,
"glGetCompressedTexImageARB(bad level = %d)", level);
return GL_TRUE;
}
texObj = _mesa_get_current_tex_object(ctx, target);
if (!texObj) {
_mesa_error(ctx, GL_INVALID_ENUM, "glGetCompressedTexImageARB(target)");
return GL_TRUE;
}
texImage = _mesa_select_tex_image(ctx, texObj, target, level);
if (!texImage) {
/* probably invalid mipmap level */
_mesa_error(ctx, GL_INVALID_VALUE,
"glGetCompressedTexImageARB(level)");
return GL_TRUE;
}
if (!_mesa_is_format_compressed(texImage->TexFormat)) {
_mesa_error(ctx, GL_INVALID_OPERATION,
"glGetCompressedTexImageARB(texture is not compressed)");
return GL_TRUE;
}
compressedSize = _mesa_format_image_size(texImage->TexFormat,
texImage->Width,
texImage->Height,
texImage->Depth);
/* Check for invalid pixel storage modes */
dimensions = _mesa_get_texture_dimensions(texImage->TexObject->Target);
if (!_mesa_compressed_texture_pixel_storage_error_check(ctx, dimensions,
&ctx->Pack,
"glGetCompressedTexImageARB")) {
return GL_TRUE;
}
if (!_mesa_is_bufferobj(ctx->Pack.BufferObj)) {
/* do bounds checking on writing to client memory */
if (clientMemSize < (GLsizei) compressedSize) {
_mesa_error(ctx, GL_INVALID_OPERATION,
"glGetnCompressedTexImageARB(out of bounds access:"
" bufSize (%d) is too small)", clientMemSize);
return GL_TRUE;
}
} else {
/* do bounds checking on PBO write */
if ((const GLubyte *) img + compressedSize >
(const GLubyte *) ctx->Pack.BufferObj->Size) {
_mesa_error(ctx, GL_INVALID_OPERATION,
"glGetCompressedTexImage(out of bounds PBO access)");
return GL_TRUE;
}
/* make sure PBO is not mapped */
if (_mesa_check_disallowed_mapping(ctx->Pack.BufferObj)) {
_mesa_error(ctx, GL_INVALID_OPERATION,
"glGetCompressedTexImage(PBO is mapped)");
return GL_TRUE;
}
}
return GL_FALSE;
}
void GLAPIENTRY
_mesa_GetnCompressedTexImageARB(GLenum target, GLint level, GLsizei bufSize,
GLvoid *img)
{
struct gl_texture_object *texObj;
struct gl_texture_image *texImage;
GET_CURRENT_CONTEXT(ctx);
FLUSH_VERTICES(ctx, 0);
if (getcompressedteximage_error_check(ctx, target, level, bufSize, img)) {
return;
}
if (!_mesa_is_bufferobj(ctx->Pack.BufferObj) && !img) {
/* not an error, do nothing */
return;
}
texObj = _mesa_get_current_tex_object(ctx, target);
texImage = _mesa_select_tex_image(ctx, texObj, target, level);
if (_mesa_is_zero_size_texture(texImage))
return;
if (MESA_VERBOSE & (VERBOSE_API | VERBOSE_TEXTURE)) {
_mesa_debug(ctx,
"glGetCompressedTexImage(tex %u) format = %s, w=%d, h=%d\n",
texObj->Name,
_mesa_get_format_name(texImage->TexFormat),
texImage->Width, texImage->Height);
}
_mesa_lock_texture(ctx, texObj);
{
ctx->Driver.GetCompressedTexImage(ctx, texImage, img);
}
_mesa_unlock_texture(ctx, texObj);
}
void GLAPIENTRY
_mesa_GetCompressedTexImage(GLenum target, GLint level, GLvoid *img)
{
_mesa_GetnCompressedTexImageARB(target, level, INT_MAX, img);
}
|