c30ef1f74a2de066f5858746c9ac685919abe410
[mesa.git] / src / mesa / main / texstore.c
1 /*
2 * Mesa 3-D graphics library
3 *
4 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
5 * Copyright (c) 2008-2009 VMware, Inc.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
21 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 * OTHER DEALINGS IN THE SOFTWARE.
24 */
25
26 /*
27 * Authors:
28 * Brian Paul
29 */
30
31 /**
32 * The GL texture image functions in teximage.c basically just do
33 * error checking and data structure allocation. They in turn call
34 * device driver functions which actually copy/convert/store the user's
35 * texture image data.
36 *
37 * However, most device drivers will be able to use the fallback functions
38 * in this file. That is, most drivers will have the following bit of
39 * code:
40 * ctx->Driver.TexImage = _mesa_store_teximage;
41 * ctx->Driver.TexSubImage = _mesa_store_texsubimage;
42 * etc...
43 *
44 * Texture image processing is actually kind of complicated. We have to do:
45 * Format/type conversions
46 * pixel unpacking
47 * pixel transfer (scale, bais, lookup, etc)
48 *
49 * These functions can handle most everything, including processing full
50 * images and sub-images.
51 */
52
53
54 #include "glheader.h"
55 #include "bufferobj.h"
56 #include "colormac.h"
57 #include "format_pack.h"
58 #include "format_utils.h"
59 #include "image.h"
60 #include "macros.h"
61 #include "mipmap.h"
62 #include "mtypes.h"
63 #include "pack.h"
64 #include "pbo.h"
65 #include "imports.h"
66 #include "texcompress.h"
67 #include "texcompress_fxt1.h"
68 #include "texcompress_rgtc.h"
69 #include "texcompress_s3tc.h"
70 #include "texcompress_etc.h"
71 #include "teximage.h"
72 #include "texstore.h"
73 #include "enums.h"
74 #include "glformats.h"
75 #include "../../gallium/auxiliary/util/u_format_rgb9e5.h"
76 #include "../../gallium/auxiliary/util/u_format_r11g11b10f.h"
77
78
79 enum {
80 ZERO = 4,
81 ONE = 5
82 };
83
84
85 /**
86 * Texture image storage function.
87 */
88 typedef GLboolean (*StoreTexImageFunc)(TEXSTORE_PARAMS);
89
90
91 /**
92 * Return GL_TRUE if the given image format is one that be converted
93 * to another format by swizzling.
94 */
95 static GLboolean
96 can_swizzle(GLenum logicalBaseFormat)
97 {
98 switch (logicalBaseFormat) {
99 case GL_RGBA:
100 case GL_RGB:
101 case GL_LUMINANCE_ALPHA:
102 case GL_INTENSITY:
103 case GL_ALPHA:
104 case GL_LUMINANCE:
105 case GL_RED:
106 case GL_GREEN:
107 case GL_BLUE:
108 case GL_BGR:
109 case GL_BGRA:
110 case GL_ABGR_EXT:
111 case GL_RG:
112 return GL_TRUE;
113 default:
114 return GL_FALSE;
115 }
116 }
117
118
119
120 enum {
121 IDX_LUMINANCE = 0,
122 IDX_ALPHA,
123 IDX_INTENSITY,
124 IDX_LUMINANCE_ALPHA,
125 IDX_RGB,
126 IDX_RGBA,
127 IDX_RED,
128 IDX_GREEN,
129 IDX_BLUE,
130 IDX_BGR,
131 IDX_BGRA,
132 IDX_ABGR,
133 IDX_RG,
134 MAX_IDX
135 };
136
137 #define MAP1(x) MAP4(x, ZERO, ZERO, ZERO)
138 #define MAP2(x,y) MAP4(x, y, ZERO, ZERO)
139 #define MAP3(x,y,z) MAP4(x, y, z, ZERO)
140 #define MAP4(x,y,z,w) { x, y, z, w, ZERO, ONE }
141
142
143 static const struct {
144 GLubyte format_idx;
145 GLubyte to_rgba[6];
146 GLubyte from_rgba[6];
147 } mappings[MAX_IDX] =
148 {
149 {
150 IDX_LUMINANCE,
151 MAP4(0,0,0,ONE),
152 MAP1(0)
153 },
154
155 {
156 IDX_ALPHA,
157 MAP4(ZERO, ZERO, ZERO, 0),
158 MAP1(3)
159 },
160
161 {
162 IDX_INTENSITY,
163 MAP4(0, 0, 0, 0),
164 MAP1(0),
165 },
166
167 {
168 IDX_LUMINANCE_ALPHA,
169 MAP4(0,0,0,1),
170 MAP2(0,3)
171 },
172
173 {
174 IDX_RGB,
175 MAP4(0,1,2,ONE),
176 MAP3(0,1,2)
177 },
178
179 {
180 IDX_RGBA,
181 MAP4(0,1,2,3),
182 MAP4(0,1,2,3),
183 },
184
185 {
186 IDX_RED,
187 MAP4(0, ZERO, ZERO, ONE),
188 MAP1(0),
189 },
190
191 {
192 IDX_GREEN,
193 MAP4(ZERO, 0, ZERO, ONE),
194 MAP1(1),
195 },
196
197 {
198 IDX_BLUE,
199 MAP4(ZERO, ZERO, 0, ONE),
200 MAP1(2),
201 },
202
203 {
204 IDX_BGR,
205 MAP4(2,1,0,ONE),
206 MAP3(2,1,0)
207 },
208
209 {
210 IDX_BGRA,
211 MAP4(2,1,0,3),
212 MAP4(2,1,0,3)
213 },
214
215 {
216 IDX_ABGR,
217 MAP4(3,2,1,0),
218 MAP4(3,2,1,0)
219 },
220
221 {
222 IDX_RG,
223 MAP4(0, 1, ZERO, ONE),
224 MAP2(0, 1)
225 },
226 };
227
228
229
230 /**
231 * Convert a GL image format enum to an IDX_* value (see above).
232 */
233 static int
234 get_map_idx(GLenum value)
235 {
236 switch (value) {
237 case GL_LUMINANCE:
238 case GL_LUMINANCE_INTEGER_EXT:
239 return IDX_LUMINANCE;
240 case GL_ALPHA:
241 case GL_ALPHA_INTEGER:
242 return IDX_ALPHA;
243 case GL_INTENSITY:
244 return IDX_INTENSITY;
245 case GL_LUMINANCE_ALPHA:
246 case GL_LUMINANCE_ALPHA_INTEGER_EXT:
247 return IDX_LUMINANCE_ALPHA;
248 case GL_RGB:
249 case GL_RGB_INTEGER:
250 return IDX_RGB;
251 case GL_RGBA:
252 case GL_RGBA_INTEGER:
253 return IDX_RGBA;
254 case GL_RED:
255 case GL_RED_INTEGER:
256 return IDX_RED;
257 case GL_GREEN:
258 return IDX_GREEN;
259 case GL_BLUE:
260 return IDX_BLUE;
261 case GL_BGR:
262 case GL_BGR_INTEGER:
263 return IDX_BGR;
264 case GL_BGRA:
265 case GL_BGRA_INTEGER:
266 return IDX_BGRA;
267 case GL_ABGR_EXT:
268 return IDX_ABGR;
269 case GL_RG:
270 case GL_RG_INTEGER:
271 return IDX_RG;
272 default:
273 _mesa_problem(NULL, "Unexpected inFormat %s",
274 _mesa_lookup_enum_by_nr(value));
275 return 0;
276 }
277 }
278
279
280 /**
281 * When promoting texture formats (see below) we need to compute the
282 * mapping of dest components back to source components.
283 * This function does that.
284 * \param inFormat the incoming format of the texture
285 * \param outFormat the final texture format
286 * \return map[6] a full 6-component map
287 */
288 static void
289 compute_component_mapping(GLenum inFormat, GLenum outFormat,
290 GLubyte *map)
291 {
292 const int inFmt = get_map_idx(inFormat);
293 const int outFmt = get_map_idx(outFormat);
294 const GLubyte *in2rgba = mappings[inFmt].to_rgba;
295 const GLubyte *rgba2out = mappings[outFmt].from_rgba;
296 int i;
297
298 for (i = 0; i < 4; i++)
299 map[i] = in2rgba[rgba2out[i]];
300
301 map[ZERO] = ZERO;
302 map[ONE] = ONE;
303
304 #if 0
305 printf("from %x/%s to %x/%s map %d %d %d %d %d %d\n",
306 inFormat, _mesa_lookup_enum_by_nr(inFormat),
307 outFormat, _mesa_lookup_enum_by_nr(outFormat),
308 map[0],
309 map[1],
310 map[2],
311 map[3],
312 map[4],
313 map[5]);
314 #endif
315 }
316
317
318 /**
319 * Make a temporary (color) texture image with GLfloat components.
320 * Apply all needed pixel unpacking and pixel transfer operations.
321 * Note that there are both logicalBaseFormat and textureBaseFormat parameters.
322 * Suppose the user specifies GL_LUMINANCE as the internal texture format
323 * but the graphics hardware doesn't support luminance textures. So, we might
324 * use an RGB hardware format instead.
325 * If logicalBaseFormat != textureBaseFormat we have some extra work to do.
326 *
327 * \param ctx the rendering context
328 * \param dims image dimensions: 1, 2 or 3
329 * \param logicalBaseFormat basic texture derived from the user's
330 * internal texture format value
331 * \param textureBaseFormat the actual basic format of the texture
332 * \param srcWidth source image width
333 * \param srcHeight source image height
334 * \param srcDepth source image depth
335 * \param srcFormat source image format
336 * \param srcType source image type
337 * \param srcAddr source image address
338 * \param srcPacking source image pixel packing
339 * \return resulting image with format = textureBaseFormat and type = GLfloat.
340 */
341 GLfloat *
342 _mesa_make_temp_float_image(struct gl_context *ctx, GLuint dims,
343 GLenum logicalBaseFormat,
344 GLenum textureBaseFormat,
345 GLint srcWidth, GLint srcHeight, GLint srcDepth,
346 GLenum srcFormat, GLenum srcType,
347 const GLvoid *srcAddr,
348 const struct gl_pixelstore_attrib *srcPacking,
349 GLbitfield transferOps)
350 {
351 GLfloat *tempImage;
352 const GLint components = _mesa_components_in_format(logicalBaseFormat);
353 const GLint srcStride =
354 _mesa_image_row_stride(srcPacking, srcWidth, srcFormat, srcType);
355 GLfloat *dst;
356 GLint img, row;
357
358 ASSERT(dims >= 1 && dims <= 3);
359
360 ASSERT(logicalBaseFormat == GL_RGBA ||
361 logicalBaseFormat == GL_RGB ||
362 logicalBaseFormat == GL_RG ||
363 logicalBaseFormat == GL_RED ||
364 logicalBaseFormat == GL_LUMINANCE_ALPHA ||
365 logicalBaseFormat == GL_LUMINANCE ||
366 logicalBaseFormat == GL_ALPHA ||
367 logicalBaseFormat == GL_INTENSITY ||
368 logicalBaseFormat == GL_DEPTH_COMPONENT);
369
370 ASSERT(textureBaseFormat == GL_RGBA ||
371 textureBaseFormat == GL_RGB ||
372 textureBaseFormat == GL_RG ||
373 textureBaseFormat == GL_RED ||
374 textureBaseFormat == GL_LUMINANCE_ALPHA ||
375 textureBaseFormat == GL_LUMINANCE ||
376 textureBaseFormat == GL_ALPHA ||
377 textureBaseFormat == GL_INTENSITY ||
378 textureBaseFormat == GL_DEPTH_COMPONENT);
379
380 tempImage = malloc(srcWidth * srcHeight * srcDepth
381 * components * sizeof(GLfloat));
382 if (!tempImage)
383 return NULL;
384
385 dst = tempImage;
386 for (img = 0; img < srcDepth; img++) {
387 const GLubyte *src
388 = (const GLubyte *) _mesa_image_address(dims, srcPacking, srcAddr,
389 srcWidth, srcHeight,
390 srcFormat, srcType,
391 img, 0, 0);
392 for (row = 0; row < srcHeight; row++) {
393 _mesa_unpack_color_span_float(ctx, srcWidth, logicalBaseFormat,
394 dst, srcFormat, srcType, src,
395 srcPacking, transferOps);
396 dst += srcWidth * components;
397 src += srcStride;
398 }
399 }
400
401 if (logicalBaseFormat != textureBaseFormat) {
402 /* more work */
403 GLint texComponents = _mesa_components_in_format(textureBaseFormat);
404 GLint logComponents = _mesa_components_in_format(logicalBaseFormat);
405 GLfloat *newImage;
406 GLint i, n;
407 GLubyte map[6];
408
409 /* we only promote up to RGB, RGBA and LUMINANCE_ALPHA formats for now */
410 ASSERT(textureBaseFormat == GL_RGB || textureBaseFormat == GL_RGBA ||
411 textureBaseFormat == GL_LUMINANCE_ALPHA);
412
413 /* The actual texture format should have at least as many components
414 * as the logical texture format.
415 */
416 ASSERT(texComponents >= logComponents);
417
418 newImage = malloc(srcWidth * srcHeight * srcDepth
419 * texComponents * sizeof(GLfloat));
420 if (!newImage) {
421 free(tempImage);
422 return NULL;
423 }
424
425 compute_component_mapping(logicalBaseFormat, textureBaseFormat, map);
426
427 n = srcWidth * srcHeight * srcDepth;
428 for (i = 0; i < n; i++) {
429 GLint k;
430 for (k = 0; k < texComponents; k++) {
431 GLint j = map[k];
432 if (j == ZERO)
433 newImage[i * texComponents + k] = 0.0F;
434 else if (j == ONE)
435 newImage[i * texComponents + k] = 1.0F;
436 else
437 newImage[i * texComponents + k] = tempImage[i * logComponents + j];
438 }
439 }
440
441 free(tempImage);
442 tempImage = newImage;
443 }
444
445 return tempImage;
446 }
447
448
449 /**
450 * Make temporary image with uint pixel values. Used for unsigned
451 * integer-valued textures.
452 */
453 static GLuint *
454 make_temp_uint_image(struct gl_context *ctx, GLuint dims,
455 GLenum logicalBaseFormat,
456 GLenum textureBaseFormat,
457 GLint srcWidth, GLint srcHeight, GLint srcDepth,
458 GLenum srcFormat, GLenum srcType,
459 const GLvoid *srcAddr,
460 const struct gl_pixelstore_attrib *srcPacking)
461 {
462 GLuint *tempImage;
463 const GLint components = _mesa_components_in_format(logicalBaseFormat);
464 const GLint srcStride =
465 _mesa_image_row_stride(srcPacking, srcWidth, srcFormat, srcType);
466 GLuint *dst;
467 GLint img, row;
468
469 ASSERT(dims >= 1 && dims <= 3);
470
471 ASSERT(logicalBaseFormat == GL_RGBA ||
472 logicalBaseFormat == GL_RGB ||
473 logicalBaseFormat == GL_RG ||
474 logicalBaseFormat == GL_RED ||
475 logicalBaseFormat == GL_LUMINANCE_ALPHA ||
476 logicalBaseFormat == GL_LUMINANCE ||
477 logicalBaseFormat == GL_INTENSITY ||
478 logicalBaseFormat == GL_ALPHA);
479
480 ASSERT(textureBaseFormat == GL_RGBA ||
481 textureBaseFormat == GL_RGB ||
482 textureBaseFormat == GL_RG ||
483 textureBaseFormat == GL_RED ||
484 textureBaseFormat == GL_LUMINANCE_ALPHA ||
485 textureBaseFormat == GL_LUMINANCE ||
486 textureBaseFormat == GL_INTENSITY ||
487 textureBaseFormat == GL_ALPHA);
488
489 tempImage = malloc(srcWidth * srcHeight * srcDepth
490 * components * sizeof(GLuint));
491 if (!tempImage)
492 return NULL;
493
494 dst = tempImage;
495 for (img = 0; img < srcDepth; img++) {
496 const GLubyte *src
497 = (const GLubyte *) _mesa_image_address(dims, srcPacking, srcAddr,
498 srcWidth, srcHeight,
499 srcFormat, srcType,
500 img, 0, 0);
501 for (row = 0; row < srcHeight; row++) {
502 _mesa_unpack_color_span_uint(ctx, srcWidth, logicalBaseFormat,
503 dst, srcFormat, srcType, src,
504 srcPacking);
505 dst += srcWidth * components;
506 src += srcStride;
507 }
508 }
509
510 if (logicalBaseFormat != textureBaseFormat) {
511 /* more work */
512 GLint texComponents = _mesa_components_in_format(textureBaseFormat);
513 GLint logComponents = _mesa_components_in_format(logicalBaseFormat);
514 GLuint *newImage;
515 GLint i, n;
516 GLubyte map[6];
517
518 /* we only promote up to RGB, RGBA and LUMINANCE_ALPHA formats for now */
519 ASSERT(textureBaseFormat == GL_RGB || textureBaseFormat == GL_RGBA ||
520 textureBaseFormat == GL_LUMINANCE_ALPHA);
521
522 /* The actual texture format should have at least as many components
523 * as the logical texture format.
524 */
525 ASSERT(texComponents >= logComponents);
526
527 newImage = malloc(srcWidth * srcHeight * srcDepth
528 * texComponents * sizeof(GLuint));
529 if (!newImage) {
530 free(tempImage);
531 return NULL;
532 }
533
534 compute_component_mapping(logicalBaseFormat, textureBaseFormat, map);
535
536 n = srcWidth * srcHeight * srcDepth;
537 for (i = 0; i < n; i++) {
538 GLint k;
539 for (k = 0; k < texComponents; k++) {
540 GLint j = map[k];
541 if (j == ZERO)
542 newImage[i * texComponents + k] = 0;
543 else if (j == ONE)
544 newImage[i * texComponents + k] = 1;
545 else
546 newImage[i * texComponents + k] = tempImage[i * logComponents + j];
547 }
548 }
549
550 free(tempImage);
551 tempImage = newImage;
552 }
553
554 return tempImage;
555 }
556
557
558
559 /**
560 * Make a temporary (color) texture image with GLubyte components.
561 * Apply all needed pixel unpacking and pixel transfer operations.
562 * Note that there are both logicalBaseFormat and textureBaseFormat parameters.
563 * Suppose the user specifies GL_LUMINANCE as the internal texture format
564 * but the graphics hardware doesn't support luminance textures. So, we might
565 * use an RGB hardware format instead.
566 * If logicalBaseFormat != textureBaseFormat we have some extra work to do.
567 *
568 * \param ctx the rendering context
569 * \param dims image dimensions: 1, 2 or 3
570 * \param logicalBaseFormat basic texture derived from the user's
571 * internal texture format value
572 * \param textureBaseFormat the actual basic format of the texture
573 * \param srcWidth source image width
574 * \param srcHeight source image height
575 * \param srcDepth source image depth
576 * \param srcFormat source image format
577 * \param srcType source image type
578 * \param srcAddr source image address
579 * \param srcPacking source image pixel packing
580 * \return resulting image with format = textureBaseFormat and type = GLubyte.
581 */
582 GLubyte *
583 _mesa_make_temp_ubyte_image(struct gl_context *ctx, GLuint dims,
584 GLenum logicalBaseFormat,
585 GLenum textureBaseFormat,
586 GLint srcWidth, GLint srcHeight, GLint srcDepth,
587 GLenum srcFormat, GLenum srcType,
588 const GLvoid *srcAddr,
589 const struct gl_pixelstore_attrib *srcPacking)
590 {
591 GLuint transferOps = ctx->_ImageTransferState;
592 const GLint components = _mesa_components_in_format(logicalBaseFormat);
593 GLint img, row;
594 GLubyte *tempImage, *dst;
595
596 ASSERT(dims >= 1 && dims <= 3);
597
598 ASSERT(logicalBaseFormat == GL_RGBA ||
599 logicalBaseFormat == GL_RGB ||
600 logicalBaseFormat == GL_RG ||
601 logicalBaseFormat == GL_RED ||
602 logicalBaseFormat == GL_LUMINANCE_ALPHA ||
603 logicalBaseFormat == GL_LUMINANCE ||
604 logicalBaseFormat == GL_ALPHA ||
605 logicalBaseFormat == GL_INTENSITY);
606
607 ASSERT(textureBaseFormat == GL_RGBA ||
608 textureBaseFormat == GL_RGB ||
609 textureBaseFormat == GL_RG ||
610 textureBaseFormat == GL_RED ||
611 textureBaseFormat == GL_LUMINANCE_ALPHA ||
612 textureBaseFormat == GL_LUMINANCE ||
613 textureBaseFormat == GL_ALPHA ||
614 textureBaseFormat == GL_INTENSITY);
615
616 /* unpack and transfer the source image */
617 tempImage = malloc(srcWidth * srcHeight * srcDepth
618 * components * sizeof(GLubyte));
619 if (!tempImage) {
620 return NULL;
621 }
622
623 dst = tempImage;
624 for (img = 0; img < srcDepth; img++) {
625 const GLint srcStride =
626 _mesa_image_row_stride(srcPacking, srcWidth, srcFormat, srcType);
627 const GLubyte *src =
628 (const GLubyte *) _mesa_image_address(dims, srcPacking, srcAddr,
629 srcWidth, srcHeight,
630 srcFormat, srcType,
631 img, 0, 0);
632 for (row = 0; row < srcHeight; row++) {
633 _mesa_unpack_color_span_ubyte(ctx, srcWidth, logicalBaseFormat, dst,
634 srcFormat, srcType, src, srcPacking,
635 transferOps);
636 dst += srcWidth * components;
637 src += srcStride;
638 }
639 }
640
641 if (logicalBaseFormat != textureBaseFormat) {
642 /* one more conversion step */
643 GLint texComponents = _mesa_components_in_format(textureBaseFormat);
644 GLint logComponents = _mesa_components_in_format(logicalBaseFormat);
645 GLubyte *newImage;
646 GLint i, n;
647 GLubyte map[6];
648
649 /* we only promote up to RGB, RGBA and LUMINANCE_ALPHA formats for now */
650 ASSERT(textureBaseFormat == GL_RGB || textureBaseFormat == GL_RGBA ||
651 textureBaseFormat == GL_LUMINANCE_ALPHA);
652
653 /* The actual texture format should have at least as many components
654 * as the logical texture format.
655 */
656 ASSERT(texComponents >= logComponents);
657
658 newImage = malloc(srcWidth * srcHeight * srcDepth
659 * texComponents * sizeof(GLubyte));
660 if (!newImage) {
661 free(tempImage);
662 return NULL;
663 }
664
665 compute_component_mapping(logicalBaseFormat, textureBaseFormat, map);
666
667 n = srcWidth * srcHeight * srcDepth;
668 for (i = 0; i < n; i++) {
669 GLint k;
670 for (k = 0; k < texComponents; k++) {
671 GLint j = map[k];
672 if (j == ZERO)
673 newImage[i * texComponents + k] = 0;
674 else if (j == ONE)
675 newImage[i * texComponents + k] = 255;
676 else
677 newImage[i * texComponents + k] = tempImage[i * logComponents + j];
678 }
679 }
680
681 free(tempImage);
682 tempImage = newImage;
683 }
684
685 return tempImage;
686 }
687
688
689 /**
690 * Copy GLubyte pixels from <src> to <dst> with swizzling.
691 * \param dst destination pixels
692 * \param dstComponents number of color components in destination pixels
693 * \param src source pixels
694 * \param srcComponents number of color components in source pixels
695 * \param map the swizzle mapping. map[X] says where to find the X component
696 * in the source image's pixels. For example, if the source image
697 * is GL_BGRA and X = red, map[0] yields 2.
698 * \param count number of pixels to copy/swizzle.
699 */
700 static void
701 swizzle_copy(GLubyte *dst, GLuint dstComponents, const GLubyte *src,
702 GLuint srcComponents, const GLubyte *map, GLuint count)
703 {
704 #define SWZ_CPY(dst, src, count, dstComps, srcComps) \
705 do { \
706 GLuint i; \
707 for (i = 0; i < count; i++) { \
708 GLuint j; \
709 if (srcComps == 4) { \
710 COPY_4UBV(tmp, src); \
711 } \
712 else { \
713 for (j = 0; j < srcComps; j++) { \
714 tmp[j] = src[j]; \
715 } \
716 } \
717 src += srcComps; \
718 for (j = 0; j < dstComps; j++) { \
719 dst[j] = tmp[map[j]]; \
720 } \
721 dst += dstComps; \
722 } \
723 } while (0)
724
725 GLubyte tmp[6];
726
727 tmp[ZERO] = 0x0;
728 tmp[ONE] = 0xff;
729
730 ASSERT(srcComponents <= 4);
731 ASSERT(dstComponents <= 4);
732
733 switch (dstComponents) {
734 case 4:
735 switch (srcComponents) {
736 case 4:
737 SWZ_CPY(dst, src, count, 4, 4);
738 break;
739 case 3:
740 SWZ_CPY(dst, src, count, 4, 3);
741 break;
742 case 2:
743 SWZ_CPY(dst, src, count, 4, 2);
744 break;
745 case 1:
746 SWZ_CPY(dst, src, count, 4, 1);
747 break;
748 default:
749 ;
750 }
751 break;
752 case 3:
753 switch (srcComponents) {
754 case 4:
755 SWZ_CPY(dst, src, count, 3, 4);
756 break;
757 case 3:
758 SWZ_CPY(dst, src, count, 3, 3);
759 break;
760 case 2:
761 SWZ_CPY(dst, src, count, 3, 2);
762 break;
763 case 1:
764 SWZ_CPY(dst, src, count, 3, 1);
765 break;
766 default:
767 ;
768 }
769 break;
770 case 2:
771 switch (srcComponents) {
772 case 4:
773 SWZ_CPY(dst, src, count, 2, 4);
774 break;
775 case 3:
776 SWZ_CPY(dst, src, count, 2, 3);
777 break;
778 case 2:
779 SWZ_CPY(dst, src, count, 2, 2);
780 break;
781 case 1:
782 SWZ_CPY(dst, src, count, 2, 1);
783 break;
784 default:
785 ;
786 }
787 break;
788 case 1:
789 switch (srcComponents) {
790 case 4:
791 SWZ_CPY(dst, src, count, 1, 4);
792 break;
793 case 3:
794 SWZ_CPY(dst, src, count, 1, 3);
795 break;
796 case 2:
797 SWZ_CPY(dst, src, count, 1, 2);
798 break;
799 case 1:
800 SWZ_CPY(dst, src, count, 1, 1);
801 break;
802 default:
803 ;
804 }
805 break;
806 default:
807 ;
808 }
809 #undef SWZ_CPY
810 }
811
812
813
814 static const GLubyte map_identity[6] = { 0, 1, 2, 3, ZERO, ONE };
815 static const GLubyte map_3210[6] = { 3, 2, 1, 0, ZERO, ONE };
816 static const GLubyte map_1032[6] = { 1, 0, 3, 2, ZERO, ONE };
817
818
819 /**
820 * For 1-byte/pixel formats (or 8_8_8_8 packed formats), return a
821 * mapping array depending on endianness.
822 */
823 static const GLubyte *
824 type_mapping( GLenum srcType )
825 {
826 switch (srcType) {
827 case GL_BYTE:
828 case GL_UNSIGNED_BYTE:
829 return map_identity;
830 case GL_UNSIGNED_INT_8_8_8_8:
831 return _mesa_little_endian() ? map_3210 : map_identity;
832 case GL_UNSIGNED_INT_8_8_8_8_REV:
833 return _mesa_little_endian() ? map_identity : map_3210;
834 default:
835 return NULL;
836 }
837 }
838
839
840 /**
841 * For 1-byte/pixel formats (or 8_8_8_8 packed formats), return a
842 * mapping array depending on pixelstore byte swapping state.
843 */
844 static const GLubyte *
845 byteswap_mapping( GLboolean swapBytes,
846 GLenum srcType )
847 {
848 if (!swapBytes)
849 return map_identity;
850
851 switch (srcType) {
852 case GL_BYTE:
853 case GL_UNSIGNED_BYTE:
854 case GL_SHORT:
855 case GL_UNSIGNED_SHORT:
856 case GL_INT:
857 case GL_UNSIGNED_INT:
858 case GL_FLOAT:
859 case GL_HALF_FLOAT:
860 return map_identity;
861 case GL_UNSIGNED_INT_8_8_8_8:
862 case GL_UNSIGNED_INT_8_8_8_8_REV:
863 return map_3210;
864 default:
865 return NULL;
866 }
867 }
868
869
870
871 /**
872 * Transfer a GLubyte texture image with component swizzling.
873 */
874 static void
875 _mesa_swizzle_ubyte_image(struct gl_context *ctx,
876 GLuint dimensions,
877 GLenum srcFormat,
878 GLenum srcType,
879
880 GLenum baseInternalFormat,
881
882 const GLubyte *rgba2dst,
883 GLuint dstComponents,
884
885 GLint dstRowStride,
886 GLubyte **dstSlices,
887
888 GLint srcWidth, GLint srcHeight, GLint srcDepth,
889 const GLvoid *srcAddr,
890 const struct gl_pixelstore_attrib *srcPacking )
891 {
892 GLint srcComponents = _mesa_components_in_format(srcFormat);
893 const GLubyte *srctype2ubyte, *swap;
894 GLubyte map[4], src2base[6], base2rgba[6];
895 GLint i;
896 const GLint srcRowStride =
897 _mesa_image_row_stride(srcPacking, srcWidth,
898 srcFormat, GL_UNSIGNED_BYTE);
899 const GLint srcImageStride
900 = _mesa_image_image_stride(srcPacking, srcWidth, srcHeight, srcFormat,
901 GL_UNSIGNED_BYTE);
902 const GLubyte *srcImage
903 = (const GLubyte *) _mesa_image_address(dimensions, srcPacking, srcAddr,
904 srcWidth, srcHeight, srcFormat,
905 GL_UNSIGNED_BYTE, 0, 0, 0);
906
907 (void) ctx;
908
909 /* Translate from src->baseInternal->GL_RGBA->dst. This will
910 * correctly deal with RGBA->RGB->RGBA conversions where the final
911 * A value must be 0xff regardless of the incoming alpha values.
912 */
913 compute_component_mapping(srcFormat, baseInternalFormat, src2base);
914 compute_component_mapping(baseInternalFormat, GL_RGBA, base2rgba);
915 swap = byteswap_mapping(srcPacking->SwapBytes, srcType);
916 srctype2ubyte = type_mapping(srcType);
917
918
919 for (i = 0; i < 4; i++)
920 map[i] = srctype2ubyte[swap[src2base[base2rgba[rgba2dst[i]]]]];
921
922 /* printf("map %d %d %d %d\n", map[0], map[1], map[2], map[3]); */
923
924 if (srcComponents == dstComponents &&
925 srcRowStride == dstRowStride &&
926 srcRowStride == srcWidth * srcComponents &&
927 dimensions < 3) {
928 /* 1 and 2D images only */
929 GLubyte *dstImage = dstSlices[0];
930 swizzle_copy(dstImage, dstComponents, srcImage, srcComponents, map,
931 srcWidth * srcHeight);
932 }
933 else {
934 GLint img, row;
935 for (img = 0; img < srcDepth; img++) {
936 const GLubyte *srcRow = srcImage;
937 GLubyte *dstRow = dstSlices[img];
938 for (row = 0; row < srcHeight; row++) {
939 swizzle_copy(dstRow, dstComponents, srcRow, srcComponents, map, srcWidth);
940 dstRow += dstRowStride;
941 srcRow += srcRowStride;
942 }
943 srcImage += srcImageStride;
944 }
945 }
946 }
947
948
949 /**
950 * Teximage storage routine for when a simple memcpy will do.
951 * No pixel transfer operations or special texel encodings allowed.
952 * 1D, 2D and 3D images supported.
953 */
954 static void
955 memcpy_texture(struct gl_context *ctx,
956 GLuint dimensions,
957 mesa_format dstFormat,
958 GLint dstRowStride,
959 GLubyte **dstSlices,
960 GLint srcWidth, GLint srcHeight, GLint srcDepth,
961 GLenum srcFormat, GLenum srcType,
962 const GLvoid *srcAddr,
963 const struct gl_pixelstore_attrib *srcPacking)
964 {
965 const GLint srcRowStride = _mesa_image_row_stride(srcPacking, srcWidth,
966 srcFormat, srcType);
967 const GLint srcImageStride = _mesa_image_image_stride(srcPacking,
968 srcWidth, srcHeight, srcFormat, srcType);
969 const GLubyte *srcImage = (const GLubyte *) _mesa_image_address(dimensions,
970 srcPacking, srcAddr, srcWidth, srcHeight, srcFormat, srcType, 0, 0, 0);
971 const GLuint texelBytes = _mesa_get_format_bytes(dstFormat);
972 const GLint bytesPerRow = srcWidth * texelBytes;
973
974 if (dstRowStride == srcRowStride &&
975 dstRowStride == bytesPerRow) {
976 /* memcpy image by image */
977 GLint img;
978 for (img = 0; img < srcDepth; img++) {
979 GLubyte *dstImage = dstSlices[img];
980 memcpy(dstImage, srcImage, bytesPerRow * srcHeight);
981 srcImage += srcImageStride;
982 }
983 }
984 else {
985 /* memcpy row by row */
986 GLint img, row;
987 for (img = 0; img < srcDepth; img++) {
988 const GLubyte *srcRow = srcImage;
989 GLubyte *dstRow = dstSlices[img];
990 for (row = 0; row < srcHeight; row++) {
991 memcpy(dstRow, srcRow, bytesPerRow);
992 dstRow += dstRowStride;
993 srcRow += srcRowStride;
994 }
995 srcImage += srcImageStride;
996 }
997 }
998 }
999
1000
1001 /**
1002 * General-case function for storing a color texture images with
1003 * components that can be represented with ubytes. Example destination
1004 * texture formats are MESA_FORMAT_ARGB888, ARGB4444, RGB565.
1005 */
1006 static GLboolean
1007 store_ubyte_texture(TEXSTORE_PARAMS)
1008 {
1009 const GLint srcRowStride = srcWidth * 4 * sizeof(GLubyte);
1010 GLubyte *tempImage, *src;
1011 GLint img;
1012
1013 tempImage = _mesa_make_temp_ubyte_image(ctx, dims,
1014 baseInternalFormat,
1015 GL_RGBA,
1016 srcWidth, srcHeight, srcDepth,
1017 srcFormat, srcType, srcAddr,
1018 srcPacking);
1019 if (!tempImage)
1020 return GL_FALSE;
1021
1022 src = tempImage;
1023 for (img = 0; img < srcDepth; img++) {
1024 _mesa_pack_ubyte_rgba_rect(dstFormat, srcWidth, srcHeight,
1025 src, srcRowStride,
1026 dstSlices[img], dstRowStride);
1027 src += srcHeight * srcRowStride;
1028 }
1029 free(tempImage);
1030
1031 return GL_TRUE;
1032 }
1033
1034
1035
1036
1037 /**
1038 * Store a 32-bit integer or float depth component texture image.
1039 */
1040 static GLboolean
1041 _mesa_texstore_z32(TEXSTORE_PARAMS)
1042 {
1043 const GLuint depthScale = 0xffffffff;
1044 GLenum dstType;
1045 (void) dims;
1046 ASSERT(dstFormat == MESA_FORMAT_Z_UNORM32 ||
1047 dstFormat == MESA_FORMAT_Z_FLOAT32);
1048 ASSERT(_mesa_get_format_bytes(dstFormat) == sizeof(GLuint));
1049
1050 if (dstFormat == MESA_FORMAT_Z_UNORM32)
1051 dstType = GL_UNSIGNED_INT;
1052 else
1053 dstType = GL_FLOAT;
1054
1055 {
1056 /* general path */
1057 GLint img, row;
1058 for (img = 0; img < srcDepth; img++) {
1059 GLubyte *dstRow = dstSlices[img];
1060 for (row = 0; row < srcHeight; row++) {
1061 const GLvoid *src = _mesa_image_address(dims, srcPacking,
1062 srcAddr, srcWidth, srcHeight, srcFormat, srcType, img, row, 0);
1063 _mesa_unpack_depth_span(ctx, srcWidth,
1064 dstType, dstRow,
1065 depthScale, srcType, src, srcPacking);
1066 dstRow += dstRowStride;
1067 }
1068 }
1069 }
1070 return GL_TRUE;
1071 }
1072
1073
1074 /**
1075 * Store a 24-bit integer depth component texture image.
1076 */
1077 static GLboolean
1078 _mesa_texstore_x8_z24(TEXSTORE_PARAMS)
1079 {
1080 const GLuint depthScale = 0xffffff;
1081
1082 (void) dims;
1083 ASSERT(dstFormat == MESA_FORMAT_Z24_UNORM_X8_UINT);
1084
1085 {
1086 /* general path */
1087 GLint img, row;
1088 for (img = 0; img < srcDepth; img++) {
1089 GLubyte *dstRow = dstSlices[img];
1090 for (row = 0; row < srcHeight; row++) {
1091 const GLvoid *src = _mesa_image_address(dims, srcPacking,
1092 srcAddr, srcWidth, srcHeight, srcFormat, srcType, img, row, 0);
1093 _mesa_unpack_depth_span(ctx, srcWidth,
1094 GL_UNSIGNED_INT, (GLuint *) dstRow,
1095 depthScale, srcType, src, srcPacking);
1096 dstRow += dstRowStride;
1097 }
1098 }
1099 }
1100 return GL_TRUE;
1101 }
1102
1103
1104 /**
1105 * Store a 24-bit integer depth component texture image.
1106 */
1107 static GLboolean
1108 _mesa_texstore_z24_x8(TEXSTORE_PARAMS)
1109 {
1110 const GLuint depthScale = 0xffffff;
1111
1112 (void) dims;
1113 ASSERT(dstFormat == MESA_FORMAT_X8_UINT_Z24_UNORM);
1114
1115 {
1116 /* general path */
1117 GLint img, row;
1118 for (img = 0; img < srcDepth; img++) {
1119 GLubyte *dstRow = dstSlices[img];
1120 for (row = 0; row < srcHeight; row++) {
1121 const GLvoid *src = _mesa_image_address(dims, srcPacking,
1122 srcAddr, srcWidth, srcHeight, srcFormat, srcType, img, row, 0);
1123 GLuint *dst = (GLuint *) dstRow;
1124 GLint i;
1125 _mesa_unpack_depth_span(ctx, srcWidth,
1126 GL_UNSIGNED_INT, dst,
1127 depthScale, srcType, src, srcPacking);
1128 for (i = 0; i < srcWidth; i++)
1129 dst[i] <<= 8;
1130 dstRow += dstRowStride;
1131 }
1132 }
1133 }
1134 return GL_TRUE;
1135 }
1136
1137
1138 /**
1139 * Store a 16-bit integer depth component texture image.
1140 */
1141 static GLboolean
1142 _mesa_texstore_z16(TEXSTORE_PARAMS)
1143 {
1144 const GLuint depthScale = 0xffff;
1145 (void) dims;
1146 ASSERT(dstFormat == MESA_FORMAT_Z_UNORM16);
1147 ASSERT(_mesa_get_format_bytes(dstFormat) == sizeof(GLushort));
1148
1149 {
1150 /* general path */
1151 GLint img, row;
1152 for (img = 0; img < srcDepth; img++) {
1153 GLubyte *dstRow = dstSlices[img];
1154 for (row = 0; row < srcHeight; row++) {
1155 const GLvoid *src = _mesa_image_address(dims, srcPacking,
1156 srcAddr, srcWidth, srcHeight, srcFormat, srcType, img, row, 0);
1157 GLushort *dst16 = (GLushort *) dstRow;
1158 _mesa_unpack_depth_span(ctx, srcWidth,
1159 GL_UNSIGNED_SHORT, dst16, depthScale,
1160 srcType, src, srcPacking);
1161 dstRow += dstRowStride;
1162 }
1163 }
1164 }
1165 return GL_TRUE;
1166 }
1167
1168
1169 /**
1170 * Store an rgb565 or rgb565_rev texture image.
1171 */
1172 static GLboolean
1173 _mesa_texstore_rgb565(TEXSTORE_PARAMS)
1174 {
1175 ASSERT(dstFormat == MESA_FORMAT_B5G6R5_UNORM ||
1176 dstFormat == MESA_FORMAT_R5G6B5_UNORM);
1177 ASSERT(_mesa_get_format_bytes(dstFormat) == 2);
1178
1179 if (!ctx->_ImageTransferState &&
1180 !srcPacking->SwapBytes &&
1181 baseInternalFormat == GL_RGB &&
1182 srcFormat == GL_RGB &&
1183 srcType == GL_UNSIGNED_BYTE &&
1184 dims == 2) {
1185 /* do optimized tex store */
1186 const GLint srcRowStride =
1187 _mesa_image_row_stride(srcPacking, srcWidth, srcFormat, srcType);
1188 const GLubyte *src = (const GLubyte *)
1189 _mesa_image_address(dims, srcPacking, srcAddr, srcWidth, srcHeight,
1190 srcFormat, srcType, 0, 0, 0);
1191 GLubyte *dst = dstSlices[0];
1192 GLint row, col;
1193 for (row = 0; row < srcHeight; row++) {
1194 const GLubyte *srcUB = (const GLubyte *) src;
1195 GLushort *dstUS = (GLushort *) dst;
1196 /* check for byteswapped format */
1197 if (dstFormat == MESA_FORMAT_B5G6R5_UNORM) {
1198 for (col = 0; col < srcWidth; col++) {
1199 dstUS[col] = PACK_COLOR_565( srcUB[0], srcUB[1], srcUB[2] );
1200 srcUB += 3;
1201 }
1202 }
1203 else {
1204 for (col = 0; col < srcWidth; col++) {
1205 dstUS[col] = PACK_COLOR_565_REV( srcUB[0], srcUB[1], srcUB[2] );
1206 srcUB += 3;
1207 }
1208 }
1209 dst += dstRowStride;
1210 src += srcRowStride;
1211 }
1212 }
1213 else {
1214 return store_ubyte_texture(ctx, dims, baseInternalFormat,
1215 dstFormat, dstRowStride, dstSlices,
1216 srcWidth, srcHeight, srcDepth,
1217 srcFormat, srcType, srcAddr, srcPacking);
1218 }
1219 return GL_TRUE;
1220 }
1221
1222
1223 /**
1224 * Store a texture in MESA_FORMAT_A8B8G8R8_UNORM or MESA_FORMAT_R8G8B8A8_UNORM.
1225 */
1226 static GLboolean
1227 _mesa_texstore_rgba8888(TEXSTORE_PARAMS)
1228 {
1229 const GLboolean littleEndian = _mesa_little_endian();
1230
1231 ASSERT(dstFormat == MESA_FORMAT_A8B8G8R8_UNORM ||
1232 dstFormat == MESA_FORMAT_R8G8B8A8_UNORM ||
1233 dstFormat == MESA_FORMAT_X8B8G8R8_UNORM ||
1234 dstFormat == MESA_FORMAT_R8G8B8X8_UNORM);
1235 ASSERT(_mesa_get_format_bytes(dstFormat) == 4);
1236
1237 if (!ctx->_ImageTransferState &&
1238 (srcType == GL_UNSIGNED_BYTE ||
1239 srcType == GL_UNSIGNED_INT_8_8_8_8 ||
1240 srcType == GL_UNSIGNED_INT_8_8_8_8_REV) &&
1241 can_swizzle(baseInternalFormat) &&
1242 can_swizzle(srcFormat)) {
1243
1244 GLubyte dstmap[4];
1245
1246 /* dstmap - how to swizzle from RGBA to dst format:
1247 */
1248 if ((littleEndian && (dstFormat == MESA_FORMAT_A8B8G8R8_UNORM ||
1249 dstFormat == MESA_FORMAT_X8B8G8R8_UNORM)) ||
1250 (!littleEndian && (dstFormat == MESA_FORMAT_R8G8B8A8_UNORM ||
1251 dstFormat == MESA_FORMAT_R8G8B8X8_UNORM))) {
1252 dstmap[3] = 0;
1253 dstmap[2] = 1;
1254 dstmap[1] = 2;
1255 dstmap[0] = 3;
1256 }
1257 else {
1258 dstmap[3] = 3;
1259 dstmap[2] = 2;
1260 dstmap[1] = 1;
1261 dstmap[0] = 0;
1262 }
1263
1264 _mesa_swizzle_ubyte_image(ctx, dims,
1265 srcFormat,
1266 srcType,
1267 baseInternalFormat,
1268 dstmap, 4,
1269 dstRowStride, dstSlices,
1270 srcWidth, srcHeight, srcDepth, srcAddr,
1271 srcPacking);
1272 }
1273 else {
1274 return store_ubyte_texture(ctx, dims, baseInternalFormat,
1275 dstFormat, dstRowStride, dstSlices,
1276 srcWidth, srcHeight, srcDepth,
1277 srcFormat, srcType, srcAddr, srcPacking);
1278 }
1279 return GL_TRUE;
1280 }
1281
1282
1283 static GLboolean
1284 _mesa_texstore_argb8888(TEXSTORE_PARAMS)
1285 {
1286 const GLboolean littleEndian = _mesa_little_endian();
1287
1288 ASSERT(dstFormat == MESA_FORMAT_B8G8R8A8_UNORM ||
1289 dstFormat == MESA_FORMAT_A8R8G8B8_UNORM ||
1290 dstFormat == MESA_FORMAT_B8G8R8X8_UNORM ||
1291 dstFormat == MESA_FORMAT_X8R8G8B8_UNORM );
1292 ASSERT(_mesa_get_format_bytes(dstFormat) == 4);
1293
1294 if (!ctx->_ImageTransferState &&
1295 !srcPacking->SwapBytes &&
1296 (dstFormat == MESA_FORMAT_B8G8R8A8_UNORM ||
1297 dstFormat == MESA_FORMAT_B8G8R8X8_UNORM) &&
1298 srcFormat == GL_RGB &&
1299 (baseInternalFormat == GL_RGBA ||
1300 baseInternalFormat == GL_RGB) &&
1301 srcType == GL_UNSIGNED_BYTE) {
1302 int img, row, col;
1303 for (img = 0; img < srcDepth; img++) {
1304 const GLint srcRowStride =
1305 _mesa_image_row_stride(srcPacking, srcWidth, srcFormat, srcType);
1306 GLubyte *srcRow = (GLubyte *) _mesa_image_address(dims, srcPacking,
1307 srcAddr, srcWidth, srcHeight, srcFormat, srcType, img, 0, 0);
1308 GLubyte *dstRow = dstSlices[img];
1309 for (row = 0; row < srcHeight; row++) {
1310 GLuint *d4 = (GLuint *) dstRow;
1311 for (col = 0; col < srcWidth; col++) {
1312 d4[col] = PACK_COLOR_8888(0xff,
1313 srcRow[col * 3 + RCOMP],
1314 srcRow[col * 3 + GCOMP],
1315 srcRow[col * 3 + BCOMP]);
1316 }
1317 dstRow += dstRowStride;
1318 srcRow += srcRowStride;
1319 }
1320 }
1321 }
1322 else if (!ctx->_ImageTransferState &&
1323 !srcPacking->SwapBytes &&
1324 dstFormat == MESA_FORMAT_B8G8R8A8_UNORM &&
1325 srcFormat == GL_LUMINANCE_ALPHA &&
1326 baseInternalFormat == GL_RGBA &&
1327 srcType == GL_UNSIGNED_BYTE) {
1328 /* special case of storing LA -> ARGB8888 */
1329 int img, row, col;
1330 const GLint srcRowStride =
1331 _mesa_image_row_stride(srcPacking, srcWidth, srcFormat, srcType);
1332 for (img = 0; img < srcDepth; img++) {
1333 const GLubyte *srcRow = (const GLubyte *)
1334 _mesa_image_address(dims, srcPacking, srcAddr, srcWidth,
1335 srcHeight, srcFormat, srcType, img, 0, 0);
1336 GLubyte *dstRow = dstSlices[img];
1337 for (row = 0; row < srcHeight; row++) {
1338 GLuint *d4 = (GLuint *) dstRow;
1339 for (col = 0; col < srcWidth; col++) {
1340 GLubyte l = srcRow[col * 2 + 0], a = srcRow[col * 2 + 1];
1341 d4[col] = PACK_COLOR_8888(a, l, l, l);
1342 }
1343 dstRow += dstRowStride;
1344 srcRow += srcRowStride;
1345 }
1346 }
1347 }
1348 else if (!ctx->_ImageTransferState &&
1349 !srcPacking->SwapBytes &&
1350 dstFormat == MESA_FORMAT_B8G8R8A8_UNORM &&
1351 srcFormat == GL_RGBA &&
1352 baseInternalFormat == GL_RGBA &&
1353 srcType == GL_UNSIGNED_BYTE) {
1354 /* same as above case, but src data has alpha too */
1355 GLint img, row, col;
1356 /* For some reason, streaming copies to write-combined regions
1357 * are extremely sensitive to the characteristics of how the
1358 * source data is retrieved. By reordering the source reads to
1359 * be in-order, the speed of this operation increases by half.
1360 * Strangely the same isn't required for the RGB path, above.
1361 */
1362 for (img = 0; img < srcDepth; img++) {
1363 const GLint srcRowStride =
1364 _mesa_image_row_stride(srcPacking, srcWidth, srcFormat, srcType);
1365 GLubyte *srcRow = (GLubyte *) _mesa_image_address(dims, srcPacking,
1366 srcAddr, srcWidth, srcHeight, srcFormat, srcType, img, 0, 0);
1367 GLubyte *dstRow = dstSlices[img];
1368 for (row = 0; row < srcHeight; row++) {
1369 GLuint *d4 = (GLuint *) dstRow;
1370 for (col = 0; col < srcWidth; col++) {
1371 d4[col] = PACK_COLOR_8888(srcRow[col * 4 + ACOMP],
1372 srcRow[col * 4 + RCOMP],
1373 srcRow[col * 4 + GCOMP],
1374 srcRow[col * 4 + BCOMP]);
1375 }
1376 dstRow += dstRowStride;
1377 srcRow += srcRowStride;
1378 }
1379 }
1380 }
1381 else if (!ctx->_ImageTransferState &&
1382 (srcType == GL_UNSIGNED_BYTE ||
1383 srcType == GL_UNSIGNED_INT_8_8_8_8 ||
1384 srcType == GL_UNSIGNED_INT_8_8_8_8_REV) &&
1385 can_swizzle(baseInternalFormat) &&
1386 can_swizzle(srcFormat)) {
1387
1388 GLubyte dstmap[4];
1389
1390 /* dstmap - how to swizzle from RGBA to dst format:
1391 */
1392 if ((littleEndian && dstFormat == MESA_FORMAT_B8G8R8A8_UNORM) ||
1393 (littleEndian && dstFormat == MESA_FORMAT_B8G8R8X8_UNORM) ||
1394 (!littleEndian && dstFormat == MESA_FORMAT_A8R8G8B8_UNORM) ||
1395 (!littleEndian && dstFormat == MESA_FORMAT_X8R8G8B8_UNORM)) {
1396 dstmap[3] = 3; /* alpha */
1397 dstmap[2] = 0; /* red */
1398 dstmap[1] = 1; /* green */
1399 dstmap[0] = 2; /* blue */
1400 }
1401 else {
1402 assert((littleEndian && dstFormat == MESA_FORMAT_A8R8G8B8_UNORM) ||
1403 (!littleEndian && dstFormat == MESA_FORMAT_B8G8R8A8_UNORM) ||
1404 (littleEndian && dstFormat == MESA_FORMAT_X8R8G8B8_UNORM) ||
1405 (!littleEndian && dstFormat == MESA_FORMAT_B8G8R8X8_UNORM));
1406 dstmap[3] = 2;
1407 dstmap[2] = 1;
1408 dstmap[1] = 0;
1409 dstmap[0] = 3;
1410 }
1411
1412 _mesa_swizzle_ubyte_image(ctx, dims,
1413 srcFormat,
1414 srcType,
1415 baseInternalFormat,
1416 dstmap, 4,
1417 dstRowStride,
1418 dstSlices,
1419 srcWidth, srcHeight, srcDepth, srcAddr,
1420 srcPacking);
1421 }
1422 else {
1423 return store_ubyte_texture(ctx, dims, baseInternalFormat,
1424 dstFormat, dstRowStride, dstSlices,
1425 srcWidth, srcHeight, srcDepth,
1426 srcFormat, srcType, srcAddr, srcPacking);
1427 }
1428 return GL_TRUE;
1429 }
1430
1431
1432 static GLboolean
1433 _mesa_texstore_rgb888(TEXSTORE_PARAMS)
1434 {
1435 ASSERT(dstFormat == MESA_FORMAT_BGR_UNORM8);
1436 ASSERT(_mesa_get_format_bytes(dstFormat) == 3);
1437
1438 if (!ctx->_ImageTransferState &&
1439 !srcPacking->SwapBytes &&
1440 srcFormat == GL_RGBA &&
1441 srcType == GL_UNSIGNED_BYTE) {
1442 /* extract RGB from RGBA */
1443 GLint img, row, col;
1444 for (img = 0; img < srcDepth; img++) {
1445 const GLint srcRowStride =
1446 _mesa_image_row_stride(srcPacking, srcWidth, srcFormat, srcType);
1447 GLubyte *srcRow = (GLubyte *) _mesa_image_address(dims, srcPacking,
1448 srcAddr, srcWidth, srcHeight, srcFormat, srcType, img, 0, 0);
1449 GLubyte *dstRow = dstSlices[img];
1450 for (row = 0; row < srcHeight; row++) {
1451 for (col = 0; col < srcWidth; col++) {
1452 dstRow[col * 3 + 0] = srcRow[col * 4 + BCOMP];
1453 dstRow[col * 3 + 1] = srcRow[col * 4 + GCOMP];
1454 dstRow[col * 3 + 2] = srcRow[col * 4 + RCOMP];
1455 }
1456 dstRow += dstRowStride;
1457 srcRow += srcRowStride;
1458 }
1459 }
1460 }
1461 else if (!ctx->_ImageTransferState &&
1462 srcType == GL_UNSIGNED_BYTE &&
1463 can_swizzle(baseInternalFormat) &&
1464 can_swizzle(srcFormat)) {
1465
1466 GLubyte dstmap[4];
1467
1468 /* dstmap - how to swizzle from RGBA to dst format:
1469 */
1470 dstmap[0] = 2;
1471 dstmap[1] = 1;
1472 dstmap[2] = 0;
1473 dstmap[3] = ONE; /* ? */
1474
1475 _mesa_swizzle_ubyte_image(ctx, dims,
1476 srcFormat,
1477 srcType,
1478 baseInternalFormat,
1479 dstmap, 3,
1480 dstRowStride, dstSlices,
1481 srcWidth, srcHeight, srcDepth, srcAddr,
1482 srcPacking);
1483 }
1484 else {
1485 return store_ubyte_texture(ctx, dims, baseInternalFormat,
1486 dstFormat, dstRowStride, dstSlices,
1487 srcWidth, srcHeight, srcDepth,
1488 srcFormat, srcType, srcAddr, srcPacking);
1489 }
1490 return GL_TRUE;
1491 }
1492
1493
1494 static GLboolean
1495 _mesa_texstore_bgr888(TEXSTORE_PARAMS)
1496 {
1497 ASSERT(dstFormat == MESA_FORMAT_RGB_UNORM8);
1498 ASSERT(_mesa_get_format_bytes(dstFormat) == 3);
1499
1500 if (!ctx->_ImageTransferState &&
1501 !srcPacking->SwapBytes &&
1502 srcFormat == GL_RGBA &&
1503 srcType == GL_UNSIGNED_BYTE) {
1504 /* extract BGR from RGBA */
1505 int img, row, col;
1506 for (img = 0; img < srcDepth; img++) {
1507 const GLint srcRowStride =
1508 _mesa_image_row_stride(srcPacking, srcWidth, srcFormat, srcType);
1509 GLubyte *srcRow = (GLubyte *) _mesa_image_address(dims, srcPacking,
1510 srcAddr, srcWidth, srcHeight, srcFormat, srcType, img, 0, 0);
1511 GLubyte *dstRow = dstSlices[img];
1512 for (row = 0; row < srcHeight; row++) {
1513 for (col = 0; col < srcWidth; col++) {
1514 dstRow[col * 3 + 0] = srcRow[col * 4 + RCOMP];
1515 dstRow[col * 3 + 1] = srcRow[col * 4 + GCOMP];
1516 dstRow[col * 3 + 2] = srcRow[col * 4 + BCOMP];
1517 }
1518 dstRow += dstRowStride;
1519 srcRow += srcRowStride;
1520 }
1521 }
1522 }
1523 else if (!ctx->_ImageTransferState &&
1524 srcType == GL_UNSIGNED_BYTE &&
1525 can_swizzle(baseInternalFormat) &&
1526 can_swizzle(srcFormat)) {
1527
1528 GLubyte dstmap[4];
1529
1530 /* dstmap - how to swizzle from RGBA to dst format:
1531 */
1532 dstmap[0] = 0;
1533 dstmap[1] = 1;
1534 dstmap[2] = 2;
1535 dstmap[3] = ONE; /* ? */
1536
1537 _mesa_swizzle_ubyte_image(ctx, dims,
1538 srcFormat,
1539 srcType,
1540 baseInternalFormat,
1541 dstmap, 3,
1542 dstRowStride, dstSlices,
1543 srcWidth, srcHeight, srcDepth, srcAddr,
1544 srcPacking);
1545 }
1546 else {
1547 return store_ubyte_texture(ctx, dims, baseInternalFormat,
1548 dstFormat, dstRowStride, dstSlices,
1549 srcWidth, srcHeight, srcDepth,
1550 srcFormat, srcType, srcAddr, srcPacking);
1551 }
1552 return GL_TRUE;
1553 }
1554
1555
1556 static GLboolean
1557 _mesa_texstore_argb2101010(TEXSTORE_PARAMS)
1558 {
1559 ASSERT(dstFormat == MESA_FORMAT_B10G10R10A2_UNORM ||
1560 dstFormat == MESA_FORMAT_B10G10R10X2_UNORM);
1561 ASSERT(_mesa_get_format_bytes(dstFormat) == 4);
1562
1563 {
1564 /* general path */
1565 /* Hardcode GL_RGBA as the base format, which forces alpha to 1.0
1566 * if the internal format is RGB. */
1567 const GLfloat *tempImage = _mesa_make_temp_float_image(ctx, dims,
1568 baseInternalFormat,
1569 GL_RGBA,
1570 srcWidth, srcHeight, srcDepth,
1571 srcFormat, srcType, srcAddr,
1572 srcPacking,
1573 ctx->_ImageTransferState);
1574 const GLfloat *src = tempImage;
1575 GLint img, row, col;
1576 if (!tempImage)
1577 return GL_FALSE;
1578 for (img = 0; img < srcDepth; img++) {
1579 GLubyte *dstRow = dstSlices[img];
1580 if (baseInternalFormat == GL_RGBA || baseInternalFormat == GL_RGB) {
1581 for (row = 0; row < srcHeight; row++) {
1582 GLuint *dstUI = (GLuint *) dstRow;
1583 for (col = 0; col < srcWidth; col++) {
1584 GLushort a,r,g,b;
1585
1586 UNCLAMPED_FLOAT_TO_USHORT(a, src[ACOMP]);
1587 UNCLAMPED_FLOAT_TO_USHORT(r, src[RCOMP]);
1588 UNCLAMPED_FLOAT_TO_USHORT(g, src[GCOMP]);
1589 UNCLAMPED_FLOAT_TO_USHORT(b, src[BCOMP]);
1590 dstUI[col] = PACK_COLOR_2101010_US(a, r, g, b);
1591 src += 4;
1592 }
1593 dstRow += dstRowStride;
1594 }
1595 } else {
1596 ASSERT(0);
1597 }
1598 }
1599 free((void *) tempImage);
1600 }
1601 return GL_TRUE;
1602 }
1603
1604
1605 /**
1606 * Do texstore for 2-channel, 4-bit/channel, unsigned normalized formats.
1607 */
1608 static GLboolean
1609 _mesa_texstore_unorm44(TEXSTORE_PARAMS)
1610 {
1611 const GLenum baseFormat = _mesa_get_format_base_format(dstFormat);
1612
1613 ASSERT(dstFormat == MESA_FORMAT_L4A4_UNORM);
1614 ASSERT(_mesa_get_format_bytes(dstFormat) == 1);
1615
1616 {
1617 /* general path */
1618 const GLubyte *tempImage = _mesa_make_temp_ubyte_image(ctx, dims,
1619 baseInternalFormat,
1620 baseFormat,
1621 srcWidth, srcHeight, srcDepth,
1622 srcFormat, srcType, srcAddr,
1623 srcPacking);
1624 const GLubyte *src = tempImage;
1625 GLint img, row, col;
1626 if (!tempImage)
1627 return GL_FALSE;
1628 for (img = 0; img < srcDepth; img++) {
1629 GLubyte *dstRow = dstSlices[img];
1630 for (row = 0; row < srcHeight; row++) {
1631 GLubyte *dstUS = (GLubyte *) dstRow;
1632 for (col = 0; col < srcWidth; col++) {
1633 /* src[0] is luminance, src[1] is alpha */
1634 dstUS[col] = PACK_COLOR_44( src[1],
1635 src[0] );
1636 src += 2;
1637 }
1638 dstRow += dstRowStride;
1639 }
1640 }
1641 free((void *) tempImage);
1642 }
1643 return GL_TRUE;
1644 }
1645
1646
1647 /**
1648 * Do texstore for 2-channel, 8-bit/channel, unsigned normalized formats.
1649 */
1650 static GLboolean
1651 _mesa_texstore_unorm88(TEXSTORE_PARAMS)
1652 {
1653 const GLboolean littleEndian = _mesa_little_endian();
1654 const GLenum baseFormat = _mesa_get_format_base_format(dstFormat);
1655
1656 ASSERT(dstFormat == MESA_FORMAT_L8A8_UNORM ||
1657 dstFormat == MESA_FORMAT_A8L8_UNORM ||
1658 dstFormat == MESA_FORMAT_R8G8_UNORM ||
1659 dstFormat == MESA_FORMAT_G8R8_UNORM);
1660 ASSERT(_mesa_get_format_bytes(dstFormat) == 2);
1661
1662 if (!ctx->_ImageTransferState &&
1663 littleEndian &&
1664 srcType == GL_UNSIGNED_BYTE &&
1665 can_swizzle(baseInternalFormat) &&
1666 can_swizzle(srcFormat)) {
1667 GLubyte dstmap[4];
1668
1669 /* dstmap - how to swizzle from RGBA to dst format:
1670 */
1671 if (dstFormat == MESA_FORMAT_L8A8_UNORM || dstFormat == MESA_FORMAT_A8L8_UNORM) {
1672 if ((littleEndian && dstFormat == MESA_FORMAT_L8A8_UNORM) ||
1673 (!littleEndian && dstFormat == MESA_FORMAT_A8L8_UNORM)) {
1674 dstmap[0] = 0;
1675 dstmap[1] = 3;
1676 }
1677 else {
1678 dstmap[0] = 3;
1679 dstmap[1] = 0;
1680 }
1681 }
1682 else {
1683 if ((littleEndian && dstFormat == MESA_FORMAT_R8G8_UNORM) ||
1684 (!littleEndian && dstFormat == MESA_FORMAT_G8R8_UNORM)) {
1685 dstmap[0] = 0;
1686 dstmap[1] = 1;
1687 }
1688 else {
1689 dstmap[0] = 1;
1690 dstmap[1] = 0;
1691 }
1692 }
1693 dstmap[2] = ZERO; /* ? */
1694 dstmap[3] = ONE; /* ? */
1695
1696 _mesa_swizzle_ubyte_image(ctx, dims,
1697 srcFormat,
1698 srcType,
1699 baseInternalFormat,
1700 dstmap, 2,
1701 dstRowStride, dstSlices,
1702 srcWidth, srcHeight, srcDepth, srcAddr,
1703 srcPacking);
1704 }
1705 else {
1706 /* general path */
1707 const GLubyte *tempImage = _mesa_make_temp_ubyte_image(ctx, dims,
1708 baseInternalFormat,
1709 baseFormat,
1710 srcWidth, srcHeight, srcDepth,
1711 srcFormat, srcType, srcAddr,
1712 srcPacking);
1713 const GLubyte *src = tempImage;
1714 GLint img, row, col;
1715 if (!tempImage)
1716 return GL_FALSE;
1717 for (img = 0; img < srcDepth; img++) {
1718 GLubyte *dstRow = dstSlices[img];
1719 for (row = 0; row < srcHeight; row++) {
1720 GLushort *dstUS = (GLushort *) dstRow;
1721 if (dstFormat == MESA_FORMAT_L8A8_UNORM ||
1722 dstFormat == MESA_FORMAT_R8G8_UNORM) {
1723 for (col = 0; col < srcWidth; col++) {
1724 /* src[0] is luminance (or R), src[1] is alpha (or G) */
1725 dstUS[col] = PACK_COLOR_88( src[1],
1726 src[0] );
1727 src += 2;
1728 }
1729 }
1730 else {
1731 for (col = 0; col < srcWidth; col++) {
1732 /* src[0] is luminance (or R), src[1] is alpha (or G) */
1733 dstUS[col] = PACK_COLOR_88_REV( src[1],
1734 src[0] );
1735 src += 2;
1736 }
1737 }
1738 dstRow += dstRowStride;
1739 }
1740 }
1741 free((void *) tempImage);
1742 }
1743 return GL_TRUE;
1744 }
1745
1746
1747 /**
1748 * Do texstore for 2-channel, 16-bit/channel, unsigned normalized formats.
1749 */
1750 static GLboolean
1751 _mesa_texstore_unorm1616(TEXSTORE_PARAMS)
1752 {
1753 const GLenum baseFormat = _mesa_get_format_base_format(dstFormat);
1754
1755 ASSERT(dstFormat == MESA_FORMAT_L16A16_UNORM ||
1756 dstFormat == MESA_FORMAT_A16L16_UNORM ||
1757 dstFormat == MESA_FORMAT_R16G16_UNORM ||
1758 dstFormat == MESA_FORMAT_G16R16_UNORM);
1759 ASSERT(_mesa_get_format_bytes(dstFormat) == 4);
1760
1761 {
1762 /* general path */
1763 const GLfloat *tempImage = _mesa_make_temp_float_image(ctx, dims,
1764 baseInternalFormat,
1765 baseFormat,
1766 srcWidth, srcHeight, srcDepth,
1767 srcFormat, srcType, srcAddr,
1768 srcPacking,
1769 ctx->_ImageTransferState);
1770 const GLfloat *src = tempImage;
1771 GLint img, row, col;
1772 if (!tempImage)
1773 return GL_FALSE;
1774 for (img = 0; img < srcDepth; img++) {
1775 GLubyte *dstRow = dstSlices[img];
1776 for (row = 0; row < srcHeight; row++) {
1777 GLuint *dstUI = (GLuint *) dstRow;
1778 if (dstFormat == MESA_FORMAT_L16A16_UNORM ||
1779 dstFormat == MESA_FORMAT_R16G16_UNORM) {
1780 for (col = 0; col < srcWidth; col++) {
1781 GLushort l, a;
1782
1783 UNCLAMPED_FLOAT_TO_USHORT(l, src[0]);
1784 UNCLAMPED_FLOAT_TO_USHORT(a, src[1]);
1785 dstUI[col] = PACK_COLOR_1616(a, l);
1786 src += 2;
1787 }
1788 }
1789 else {
1790 for (col = 0; col < srcWidth; col++) {
1791 GLushort l, a;
1792
1793 UNCLAMPED_FLOAT_TO_USHORT(l, src[0]);
1794 UNCLAMPED_FLOAT_TO_USHORT(a, src[1]);
1795 dstUI[col] = PACK_COLOR_1616_REV(a, l);
1796 src += 2;
1797 }
1798 }
1799 dstRow += dstRowStride;
1800 }
1801 }
1802 free((void *) tempImage);
1803 }
1804 return GL_TRUE;
1805 }
1806
1807
1808 /* Texstore for R16, A16, L16, I16. */
1809 static GLboolean
1810 _mesa_texstore_unorm16(TEXSTORE_PARAMS)
1811 {
1812 const GLenum baseFormat = _mesa_get_format_base_format(dstFormat);
1813
1814 ASSERT(dstFormat == MESA_FORMAT_R_UNORM16 ||
1815 dstFormat == MESA_FORMAT_A_UNORM16 ||
1816 dstFormat == MESA_FORMAT_L_UNORM16 ||
1817 dstFormat == MESA_FORMAT_I_UNORM16);
1818 ASSERT(_mesa_get_format_bytes(dstFormat) == 2);
1819
1820 {
1821 /* general path */
1822 const GLfloat *tempImage = _mesa_make_temp_float_image(ctx, dims,
1823 baseInternalFormat,
1824 baseFormat,
1825 srcWidth, srcHeight, srcDepth,
1826 srcFormat, srcType, srcAddr,
1827 srcPacking,
1828 ctx->_ImageTransferState);
1829 const GLfloat *src = tempImage;
1830 GLint img, row, col;
1831 if (!tempImage)
1832 return GL_FALSE;
1833 for (img = 0; img < srcDepth; img++) {
1834 GLubyte *dstRow = dstSlices[img];
1835 for (row = 0; row < srcHeight; row++) {
1836 GLushort *dstUS = (GLushort *) dstRow;
1837 for (col = 0; col < srcWidth; col++) {
1838 GLushort r;
1839
1840 UNCLAMPED_FLOAT_TO_USHORT(r, src[0]);
1841 dstUS[col] = r;
1842 src += 1;
1843 }
1844 dstRow += dstRowStride;
1845 }
1846 }
1847 free((void *) tempImage);
1848 }
1849 return GL_TRUE;
1850 }
1851
1852
1853 static GLboolean
1854 _mesa_texstore_rgba_16(TEXSTORE_PARAMS)
1855 {
1856 ASSERT(dstFormat == MESA_FORMAT_RGBA_UNORM16 ||
1857 dstFormat == MESA_FORMAT_RGBX_UNORM16);
1858 ASSERT(_mesa_get_format_bytes(dstFormat) == 8);
1859
1860 {
1861 /* general path */
1862 /* Hardcode GL_RGBA as the base format, which forces alpha to 1.0
1863 * if the internal format is RGB. */
1864 const GLfloat *tempImage = _mesa_make_temp_float_image(ctx, dims,
1865 baseInternalFormat,
1866 GL_RGBA,
1867 srcWidth, srcHeight, srcDepth,
1868 srcFormat, srcType, srcAddr,
1869 srcPacking,
1870 ctx->_ImageTransferState);
1871 const GLfloat *src = tempImage;
1872 GLint img, row, col;
1873
1874 if (!tempImage)
1875 return GL_FALSE;
1876
1877 for (img = 0; img < srcDepth; img++) {
1878 GLubyte *dstRow = dstSlices[img];
1879 for (row = 0; row < srcHeight; row++) {
1880 GLushort *dstUS = (GLushort *) dstRow;
1881 for (col = 0; col < srcWidth; col++) {
1882 GLushort r, g, b, a;
1883
1884 UNCLAMPED_FLOAT_TO_USHORT(r, src[0]);
1885 UNCLAMPED_FLOAT_TO_USHORT(g, src[1]);
1886 UNCLAMPED_FLOAT_TO_USHORT(b, src[2]);
1887 UNCLAMPED_FLOAT_TO_USHORT(a, src[3]);
1888 dstUS[col*4+0] = r;
1889 dstUS[col*4+1] = g;
1890 dstUS[col*4+2] = b;
1891 dstUS[col*4+3] = a;
1892 src += 4;
1893 }
1894 dstRow += dstRowStride;
1895 }
1896 }
1897 free((void *) tempImage);
1898 }
1899 return GL_TRUE;
1900 }
1901
1902
1903 static GLboolean
1904 _mesa_texstore_signed_rgba_16(TEXSTORE_PARAMS)
1905 {
1906 const GLenum baseFormat = _mesa_get_format_base_format(dstFormat);
1907
1908 ASSERT(dstFormat == MESA_FORMAT_RGB_SNORM16 ||
1909 dstFormat == MESA_FORMAT_RGBA_SNORM16 ||
1910 dstFormat == MESA_FORMAT_RGBX_SNORM16);
1911
1912 {
1913 /* general path */
1914 const GLfloat *tempImage = _mesa_make_temp_float_image(ctx, dims,
1915 baseInternalFormat,
1916 baseFormat,
1917 srcWidth, srcHeight, srcDepth,
1918 srcFormat, srcType, srcAddr,
1919 srcPacking,
1920 ctx->_ImageTransferState);
1921 const GLfloat *src = tempImage;
1922 const GLuint comps = _mesa_get_format_bytes(dstFormat) / 2;
1923 GLint img, row, col;
1924
1925 if (!tempImage)
1926 return GL_FALSE;
1927
1928 /* Note: tempImage is always float[4] / RGBA. We convert to 1, 2,
1929 * 3 or 4 components/pixel here.
1930 */
1931 for (img = 0; img < srcDepth; img++) {
1932 GLubyte *dstRow = dstSlices[img];
1933 for (row = 0; row < srcHeight; row++) {
1934 GLshort *dstRowS = (GLshort *) dstRow;
1935 if (dstFormat == MESA_FORMAT_RGBA_SNORM16) {
1936 for (col = 0; col < srcWidth; col++) {
1937 GLuint c;
1938 for (c = 0; c < comps; c++) {
1939 GLshort p;
1940 UNCLAMPED_FLOAT_TO_SHORT(p, src[col * 4 + c]);
1941 dstRowS[col * comps + c] = p;
1942 }
1943 }
1944 dstRow += dstRowStride;
1945 src += 4 * srcWidth;
1946 }
1947 else if (dstFormat == MESA_FORMAT_RGBX_SNORM16) {
1948 for (col = 0; col < srcWidth; col++) {
1949 GLuint c;
1950
1951 for (c = 0; c < 3; c++) {
1952 GLshort p;
1953 UNCLAMPED_FLOAT_TO_SHORT(p, src[col * 3 + c]);
1954 dstRowS[col * comps + c] = p;
1955 }
1956 dstRowS[col * comps + 3] = 32767;
1957 }
1958 dstRow += dstRowStride;
1959 src += 3 * srcWidth;
1960 }
1961 else {
1962 for (col = 0; col < srcWidth; col++) {
1963 GLuint c;
1964 for (c = 0; c < comps; c++) {
1965 GLshort p;
1966 UNCLAMPED_FLOAT_TO_SHORT(p, src[col * 3 + c]);
1967 dstRowS[col * comps + c] = p;
1968 }
1969 }
1970 dstRow += dstRowStride;
1971 src += 3 * srcWidth;
1972 }
1973 }
1974 }
1975 free((void *) tempImage);
1976 }
1977 return GL_TRUE;
1978 }
1979
1980
1981 /**
1982 * Texstore for _mesa_texformat_a8, _mesa_texformat_l8, _mesa_texformat_i8.
1983 */
1984 static GLboolean
1985 _mesa_texstore_unorm8(TEXSTORE_PARAMS)
1986 {
1987 const GLenum baseFormat = _mesa_get_format_base_format(dstFormat);
1988
1989 ASSERT(dstFormat == MESA_FORMAT_A_UNORM8 ||
1990 dstFormat == MESA_FORMAT_L_UNORM8 ||
1991 dstFormat == MESA_FORMAT_I_UNORM8 ||
1992 dstFormat == MESA_FORMAT_R_UNORM8);
1993 ASSERT(_mesa_get_format_bytes(dstFormat) == 1);
1994
1995 if (!ctx->_ImageTransferState &&
1996 srcType == GL_UNSIGNED_BYTE &&
1997 can_swizzle(baseInternalFormat) &&
1998 can_swizzle(srcFormat)) {
1999 GLubyte dstmap[4];
2000
2001 /* dstmap - how to swizzle from RGBA to dst format:
2002 */
2003 if (dstFormat == MESA_FORMAT_A_UNORM8) {
2004 dstmap[0] = 3;
2005 }
2006 else {
2007 dstmap[0] = 0;
2008 }
2009 dstmap[1] = ZERO; /* ? */
2010 dstmap[2] = ZERO; /* ? */
2011 dstmap[3] = ONE; /* ? */
2012
2013 _mesa_swizzle_ubyte_image(ctx, dims,
2014 srcFormat,
2015 srcType,
2016 baseInternalFormat,
2017 dstmap, 1,
2018 dstRowStride, dstSlices,
2019 srcWidth, srcHeight, srcDepth, srcAddr,
2020 srcPacking);
2021 }
2022 else {
2023 /* general path */
2024 const GLubyte *tempImage = _mesa_make_temp_ubyte_image(ctx, dims,
2025 baseInternalFormat,
2026 baseFormat,
2027 srcWidth, srcHeight, srcDepth,
2028 srcFormat, srcType, srcAddr,
2029 srcPacking);
2030 const GLubyte *src = tempImage;
2031 GLint img, row, col;
2032 if (!tempImage)
2033 return GL_FALSE;
2034 for (img = 0; img < srcDepth; img++) {
2035 GLubyte *dstRow = dstSlices[img];
2036 for (row = 0; row < srcHeight; row++) {
2037 for (col = 0; col < srcWidth; col++) {
2038 dstRow[col] = src[col];
2039 }
2040 dstRow += dstRowStride;
2041 src += srcWidth;
2042 }
2043 }
2044 free((void *) tempImage);
2045 }
2046 return GL_TRUE;
2047 }
2048
2049
2050
2051 /**
2052 * Texstore for _mesa_texformat_ycbcr or _mesa_texformat_ycbcr_REV.
2053 */
2054 static GLboolean
2055 _mesa_texstore_ycbcr(TEXSTORE_PARAMS)
2056 {
2057 const GLboolean littleEndian = _mesa_little_endian();
2058
2059 (void) ctx; (void) dims; (void) baseInternalFormat;
2060
2061 ASSERT((dstFormat == MESA_FORMAT_YCBCR) ||
2062 (dstFormat == MESA_FORMAT_YCBCR_REV));
2063 ASSERT(_mesa_get_format_bytes(dstFormat) == 2);
2064 ASSERT(ctx->Extensions.MESA_ycbcr_texture);
2065 ASSERT(srcFormat == GL_YCBCR_MESA);
2066 ASSERT((srcType == GL_UNSIGNED_SHORT_8_8_MESA) ||
2067 (srcType == GL_UNSIGNED_SHORT_8_8_REV_MESA));
2068 ASSERT(baseInternalFormat == GL_YCBCR_MESA);
2069
2070 /* always just memcpy since no pixel transfer ops apply */
2071 memcpy_texture(ctx, dims,
2072 dstFormat,
2073 dstRowStride, dstSlices,
2074 srcWidth, srcHeight, srcDepth, srcFormat, srcType,
2075 srcAddr, srcPacking);
2076
2077 /* Check if we need byte swapping */
2078 /* XXX the logic here _might_ be wrong */
2079 if (srcPacking->SwapBytes ^
2080 (srcType == GL_UNSIGNED_SHORT_8_8_REV_MESA) ^
2081 (dstFormat == MESA_FORMAT_YCBCR_REV) ^
2082 !littleEndian) {
2083 GLint img, row;
2084 for (img = 0; img < srcDepth; img++) {
2085 GLubyte *dstRow = dstSlices[img];
2086 for (row = 0; row < srcHeight; row++) {
2087 _mesa_swap2((GLushort *) dstRow, srcWidth);
2088 dstRow += dstRowStride;
2089 }
2090 }
2091 }
2092 return GL_TRUE;
2093 }
2094
2095
2096 /**
2097 * Store a texture in a signed normalized 8-bit format.
2098 */
2099 static GLboolean
2100 _mesa_texstore_snorm8(TEXSTORE_PARAMS)
2101 {
2102 const GLenum baseFormat = _mesa_get_format_base_format(dstFormat);
2103
2104 ASSERT(dstFormat == MESA_FORMAT_A_SNORM8 ||
2105 dstFormat == MESA_FORMAT_L_SNORM8 ||
2106 dstFormat == MESA_FORMAT_I_SNORM8 ||
2107 dstFormat == MESA_FORMAT_R_SNORM8);
2108 ASSERT(_mesa_get_format_bytes(dstFormat) == 1);
2109
2110 {
2111 /* general path */
2112 const GLfloat *tempImage = _mesa_make_temp_float_image(ctx, dims,
2113 baseInternalFormat,
2114 baseFormat,
2115 srcWidth, srcHeight, srcDepth,
2116 srcFormat, srcType, srcAddr,
2117 srcPacking,
2118 ctx->_ImageTransferState);
2119 const GLfloat *src = tempImage;
2120 GLint img, row, col;
2121 if (!tempImage)
2122 return GL_FALSE;
2123 for (img = 0; img < srcDepth; img++) {
2124 GLbyte *dstRow = (GLbyte *) dstSlices[img];
2125 for (row = 0; row < srcHeight; row++) {
2126 for (col = 0; col < srcWidth; col++) {
2127 dstRow[col] = FLOAT_TO_BYTE_TEX(src[col]);
2128 }
2129 dstRow += dstRowStride;
2130 src += srcWidth;
2131 }
2132 }
2133 free((void *) tempImage);
2134 }
2135 return GL_TRUE;
2136 }
2137
2138
2139 /**
2140 * Store a texture in a signed normalized two-channel 16-bit format.
2141 */
2142 static GLboolean
2143 _mesa_texstore_snorm88(TEXSTORE_PARAMS)
2144 {
2145 const GLenum baseFormat = _mesa_get_format_base_format(dstFormat);
2146
2147 ASSERT(dstFormat == MESA_FORMAT_L8A8_SNORM ||
2148 dstFormat == MESA_FORMAT_G8R8_SNORM ||
2149 dstFormat == MESA_FORMAT_R8G8_SNORM);
2150 ASSERT(_mesa_get_format_bytes(dstFormat) == 2);
2151
2152 {
2153 /* general path */
2154 const GLfloat *tempImage = _mesa_make_temp_float_image(ctx, dims,
2155 baseInternalFormat,
2156 baseFormat,
2157 srcWidth, srcHeight, srcDepth,
2158 srcFormat, srcType, srcAddr,
2159 srcPacking,
2160 ctx->_ImageTransferState);
2161 const GLfloat *src = tempImage;
2162 GLint img, row, col;
2163 if (!tempImage)
2164 return GL_FALSE;
2165 for (img = 0; img < srcDepth; img++) {
2166 GLbyte *dstRow = (GLbyte *) dstSlices[img];
2167 for (row = 0; row < srcHeight; row++) {
2168 GLushort *dst = (GLushort *) dstRow;
2169
2170 if (dstFormat == MESA_FORMAT_L8A8_SNORM ||
2171 dstFormat == MESA_FORMAT_R8G8_SNORM) {
2172 for (col = 0; col < srcWidth; col++) {
2173 GLubyte l = FLOAT_TO_BYTE_TEX(src[0]);
2174 GLubyte a = FLOAT_TO_BYTE_TEX(src[1]);
2175
2176 dst[col] = PACK_COLOR_88_REV(l, a);
2177 src += 2;
2178 }
2179 } else {
2180 for (col = 0; col < srcWidth; col++) {
2181 GLubyte l = FLOAT_TO_BYTE_TEX(src[0]);
2182 GLubyte a = FLOAT_TO_BYTE_TEX(src[1]);
2183
2184 dst[col] = PACK_COLOR_88(l, a);
2185 src += 2;
2186 }
2187 }
2188
2189 dstRow += dstRowStride;
2190 }
2191 }
2192 free((void *) tempImage);
2193 }
2194 return GL_TRUE;
2195 }
2196
2197 /* Texstore for signed R16, A16, L16, I16. */
2198 static GLboolean
2199 _mesa_texstore_snorm16(TEXSTORE_PARAMS)
2200 {
2201 const GLenum baseFormat = _mesa_get_format_base_format(dstFormat);
2202
2203 ASSERT(dstFormat == MESA_FORMAT_R_SNORM16 ||
2204 dstFormat == MESA_FORMAT_A_SNORM16 ||
2205 dstFormat == MESA_FORMAT_L_SNORM16 ||
2206 dstFormat == MESA_FORMAT_I_SNORM16);
2207 ASSERT(_mesa_get_format_bytes(dstFormat) == 2);
2208
2209 {
2210 /* general path */
2211 const GLfloat *tempImage = _mesa_make_temp_float_image(ctx, dims,
2212 baseInternalFormat,
2213 baseFormat,
2214 srcWidth, srcHeight, srcDepth,
2215 srcFormat, srcType, srcAddr,
2216 srcPacking,
2217 ctx->_ImageTransferState);
2218 const GLfloat *src = tempImage;
2219 GLint img, row, col;
2220 if (!tempImage)
2221 return GL_FALSE;
2222 for (img = 0; img < srcDepth; img++) {
2223 GLubyte *dstRow = dstSlices[img];
2224 for (row = 0; row < srcHeight; row++) {
2225 GLshort *dstUS = (GLshort *) dstRow;
2226 for (col = 0; col < srcWidth; col++) {
2227 GLushort r;
2228
2229 UNCLAMPED_FLOAT_TO_SHORT(r, src[0]);
2230 dstUS[col] = r;
2231 src += 1;
2232 }
2233 dstRow += dstRowStride;
2234 }
2235 }
2236 free((void *) tempImage);
2237 }
2238 return GL_TRUE;
2239 }
2240
2241 /**
2242 * Do texstore for 2-channel, 16-bit/channel, signed normalized formats.
2243 */
2244 static GLboolean
2245 _mesa_texstore_snorm1616(TEXSTORE_PARAMS)
2246 {
2247 const GLenum baseFormat = _mesa_get_format_base_format(dstFormat);
2248
2249 ASSERT(dstFormat == MESA_FORMAT_LA_SNORM16 ||
2250 dstFormat == MESA_FORMAT_G16R16_SNORM ||
2251 dstFormat == MESA_FORMAT_R16G16_SNORM);
2252 ASSERT(_mesa_get_format_bytes(dstFormat) == 4);
2253
2254 {
2255 /* general path */
2256 const GLfloat *tempImage = _mesa_make_temp_float_image(ctx, dims,
2257 baseInternalFormat,
2258 baseFormat,
2259 srcWidth, srcHeight, srcDepth,
2260 srcFormat, srcType, srcAddr,
2261 srcPacking,
2262 ctx->_ImageTransferState);
2263 const GLfloat *src = tempImage;
2264 GLint img, row, col;
2265 if (!tempImage)
2266 return GL_FALSE;
2267 for (img = 0; img < srcDepth; img++) {
2268 GLubyte *dstRow = dstSlices[img];
2269 for (row = 0; row < srcHeight; row++) {
2270 GLuint *dst = (GLuint *) dstRow;
2271
2272 if (dstFormat == MESA_FORMAT_LA_SNORM16 ||
2273 dstFormat == MESA_FORMAT_R16G16_SNORM) {
2274 for (col = 0; col < srcWidth; col++) {
2275 GLushort l, a;
2276
2277 UNCLAMPED_FLOAT_TO_SHORT(l, src[0]);
2278 UNCLAMPED_FLOAT_TO_SHORT(a, src[1]);
2279 dst[col] = PACK_COLOR_1616_REV(l, a);
2280 src += 2;
2281 }
2282 } else {
2283 for (col = 0; col < srcWidth; col++) {
2284 GLushort l, a;
2285
2286 UNCLAMPED_FLOAT_TO_SHORT(l, src[0]);
2287 UNCLAMPED_FLOAT_TO_SHORT(a, src[1]);
2288 dst[col] = PACK_COLOR_1616_REV(l, a);
2289 src += 2;
2290 }
2291 }
2292
2293 dstRow += dstRowStride;
2294 }
2295 }
2296 free((void *) tempImage);
2297 }
2298 return GL_TRUE;
2299 }
2300
2301 /**
2302 * Store a texture in MESA_FORMAT_X8B8G8R8_SNORM or
2303 * MESA_FORMAT_R8G8B8X8_SNORM.
2304 */
2305 static GLboolean
2306 _mesa_texstore_signed_rgbx8888(TEXSTORE_PARAMS)
2307 {
2308 const GLenum baseFormat = _mesa_get_format_base_format(dstFormat);
2309
2310 ASSERT(dstFormat == MESA_FORMAT_X8B8G8R8_SNORM ||
2311 dstFormat == MESA_FORMAT_R8G8B8X8_SNORM);
2312 ASSERT(_mesa_get_format_bytes(dstFormat) == 4);
2313
2314 {
2315 /* general path */
2316 const GLfloat *tempImage = _mesa_make_temp_float_image(ctx, dims,
2317 baseInternalFormat,
2318 baseFormat,
2319 srcWidth, srcHeight, srcDepth,
2320 srcFormat, srcType, srcAddr,
2321 srcPacking,
2322 ctx->_ImageTransferState);
2323 const GLfloat *srcRow = tempImage;
2324 GLint img, row, col;
2325 if (!tempImage)
2326 return GL_FALSE;
2327 for (img = 0; img < srcDepth; img++) {
2328 GLbyte *dstRow = (GLbyte *) dstSlices[img];
2329 for (row = 0; row < srcHeight; row++) {
2330 GLbyte *dst = dstRow;
2331 if (dstFormat == MESA_FORMAT_X8B8G8R8_SNORM) {
2332 for (col = 0; col < srcWidth; col++) {
2333 dst[3] = FLOAT_TO_BYTE_TEX(srcRow[RCOMP]);
2334 dst[2] = FLOAT_TO_BYTE_TEX(srcRow[GCOMP]);
2335 dst[1] = FLOAT_TO_BYTE_TEX(srcRow[BCOMP]);
2336 dst[0] = 127;
2337 srcRow += 3;
2338 dst += 4;
2339 }
2340 }
2341 else {
2342 for (col = 0; col < srcWidth; col++) {
2343 dst[0] = FLOAT_TO_BYTE_TEX(srcRow[RCOMP]);
2344 dst[1] = FLOAT_TO_BYTE_TEX(srcRow[GCOMP]);
2345 dst[2] = FLOAT_TO_BYTE_TEX(srcRow[BCOMP]);
2346 dst[3] = 127;
2347 srcRow += 3;
2348 dst += 4;
2349 }
2350 }
2351 dstRow += dstRowStride;
2352 }
2353 }
2354 free((void *) tempImage);
2355 }
2356 return GL_TRUE;
2357 }
2358
2359
2360
2361 /**
2362 * Store a texture in MESA_FORMAT_A8B8G8R8_SNORM or
2363 * MESA_FORMAT_R8G8B8A8_SNORM
2364 */
2365 static GLboolean
2366 _mesa_texstore_signed_rgba8888(TEXSTORE_PARAMS)
2367 {
2368 const GLenum baseFormat = _mesa_get_format_base_format(dstFormat);
2369
2370 ASSERT(dstFormat == MESA_FORMAT_A8B8G8R8_SNORM ||
2371 dstFormat == MESA_FORMAT_R8G8B8A8_SNORM);
2372 ASSERT(_mesa_get_format_bytes(dstFormat) == 4);
2373
2374 {
2375 /* general path */
2376 const GLfloat *tempImage = _mesa_make_temp_float_image(ctx, dims,
2377 baseInternalFormat,
2378 baseFormat,
2379 srcWidth, srcHeight, srcDepth,
2380 srcFormat, srcType, srcAddr,
2381 srcPacking,
2382 ctx->_ImageTransferState);
2383 const GLfloat *srcRow = tempImage;
2384 GLint img, row, col;
2385 if (!tempImage)
2386 return GL_FALSE;
2387 for (img = 0; img < srcDepth; img++) {
2388 GLbyte *dstRow = (GLbyte *) dstSlices[img];
2389 for (row = 0; row < srcHeight; row++) {
2390 GLbyte *dst = dstRow;
2391 if (dstFormat == MESA_FORMAT_A8B8G8R8_SNORM) {
2392 for (col = 0; col < srcWidth; col++) {
2393 dst[3] = FLOAT_TO_BYTE_TEX(srcRow[RCOMP]);
2394 dst[2] = FLOAT_TO_BYTE_TEX(srcRow[GCOMP]);
2395 dst[1] = FLOAT_TO_BYTE_TEX(srcRow[BCOMP]);
2396 dst[0] = FLOAT_TO_BYTE_TEX(srcRow[ACOMP]);
2397 srcRow += 4;
2398 dst += 4;
2399 }
2400 }
2401 else {
2402 for (col = 0; col < srcWidth; col++) {
2403 dst[0] = FLOAT_TO_BYTE_TEX(srcRow[RCOMP]);
2404 dst[1] = FLOAT_TO_BYTE_TEX(srcRow[GCOMP]);
2405 dst[2] = FLOAT_TO_BYTE_TEX(srcRow[BCOMP]);
2406 dst[3] = FLOAT_TO_BYTE_TEX(srcRow[ACOMP]);
2407 srcRow += 4;
2408 dst += 4;
2409 }
2410 }
2411 dstRow += dstRowStride;
2412 }
2413 }
2414 free((void *) tempImage);
2415 }
2416 return GL_TRUE;
2417 }
2418
2419
2420 /**
2421 * Store a combined depth/stencil texture image.
2422 */
2423 static GLboolean
2424 _mesa_texstore_z24_s8(TEXSTORE_PARAMS)
2425 {
2426 const GLuint depthScale = 0xffffff;
2427 const GLint srcRowStride
2428 = _mesa_image_row_stride(srcPacking, srcWidth, srcFormat, srcType);
2429 GLint img, row;
2430 GLuint *depth = malloc(srcWidth * sizeof(GLuint));
2431 GLubyte *stencil = malloc(srcWidth * sizeof(GLubyte));
2432
2433 ASSERT(dstFormat == MESA_FORMAT_S8_UINT_Z24_UNORM);
2434 ASSERT(srcFormat == GL_DEPTH_STENCIL_EXT ||
2435 srcFormat == GL_DEPTH_COMPONENT ||
2436 srcFormat == GL_STENCIL_INDEX);
2437 ASSERT(srcFormat != GL_DEPTH_STENCIL_EXT ||
2438 srcType == GL_UNSIGNED_INT_24_8_EXT ||
2439 srcType == GL_FLOAT_32_UNSIGNED_INT_24_8_REV);
2440
2441 if (!depth || !stencil) {
2442 free(depth);
2443 free(stencil);
2444 return GL_FALSE;
2445 }
2446
2447 /* In case we only upload depth we need to preserve the stencil */
2448 for (img = 0; img < srcDepth; img++) {
2449 GLuint *dstRow = (GLuint *) dstSlices[img];
2450 const GLubyte *src
2451 = (const GLubyte *) _mesa_image_address(dims, srcPacking, srcAddr,
2452 srcWidth, srcHeight,
2453 srcFormat, srcType,
2454 img, 0, 0);
2455 for (row = 0; row < srcHeight; row++) {
2456 GLint i;
2457 GLboolean keepdepth = GL_FALSE, keepstencil = GL_FALSE;
2458
2459 if (srcFormat == GL_DEPTH_COMPONENT) { /* preserve stencil */
2460 keepstencil = GL_TRUE;
2461 }
2462 else if (srcFormat == GL_STENCIL_INDEX) { /* preserve depth */
2463 keepdepth = GL_TRUE;
2464 }
2465
2466 if (keepdepth == GL_FALSE)
2467 /* the 24 depth bits will be in the low position: */
2468 _mesa_unpack_depth_span(ctx, srcWidth,
2469 GL_UNSIGNED_INT, /* dst type */
2470 keepstencil ? depth : dstRow, /* dst addr */
2471 depthScale,
2472 srcType, src, srcPacking);
2473
2474 if (keepstencil == GL_FALSE)
2475 /* get the 8-bit stencil values */
2476 _mesa_unpack_stencil_span(ctx, srcWidth,
2477 GL_UNSIGNED_BYTE, /* dst type */
2478 stencil, /* dst addr */
2479 srcType, src, srcPacking,
2480 ctx->_ImageTransferState);
2481
2482 for (i = 0; i < srcWidth; i++) {
2483 if (keepstencil)
2484 dstRow[i] = depth[i] << 8 | (dstRow[i] & 0x000000FF);
2485 else
2486 dstRow[i] = (dstRow[i] & 0xFFFFFF00) | (stencil[i] & 0xFF);
2487 }
2488 src += srcRowStride;
2489 dstRow += dstRowStride / sizeof(GLuint);
2490 }
2491 }
2492
2493 free(depth);
2494 free(stencil);
2495 return GL_TRUE;
2496 }
2497
2498
2499 /**
2500 * Store a combined depth/stencil texture image.
2501 */
2502 static GLboolean
2503 _mesa_texstore_s8_z24(TEXSTORE_PARAMS)
2504 {
2505 const GLuint depthScale = 0xffffff;
2506 const GLint srcRowStride
2507 = _mesa_image_row_stride(srcPacking, srcWidth, srcFormat, srcType);
2508 GLint img, row;
2509 GLuint *depth;
2510 GLubyte *stencil;
2511
2512 ASSERT(dstFormat == MESA_FORMAT_Z24_UNORM_S8_UINT);
2513 ASSERT(srcFormat == GL_DEPTH_STENCIL_EXT ||
2514 srcFormat == GL_DEPTH_COMPONENT ||
2515 srcFormat == GL_STENCIL_INDEX);
2516 ASSERT(srcFormat != GL_DEPTH_STENCIL_EXT ||
2517 srcType == GL_UNSIGNED_INT_24_8_EXT ||
2518 srcType == GL_FLOAT_32_UNSIGNED_INT_24_8_REV);
2519
2520 depth = malloc(srcWidth * sizeof(GLuint));
2521 stencil = malloc(srcWidth * sizeof(GLubyte));
2522
2523 if (!depth || !stencil) {
2524 free(depth);
2525 free(stencil);
2526 return GL_FALSE;
2527 }
2528
2529 for (img = 0; img < srcDepth; img++) {
2530 GLuint *dstRow = (GLuint *) dstSlices[img];
2531 const GLubyte *src
2532 = (const GLubyte *) _mesa_image_address(dims, srcPacking, srcAddr,
2533 srcWidth, srcHeight,
2534 srcFormat, srcType,
2535 img, 0, 0);
2536 for (row = 0; row < srcHeight; row++) {
2537 GLint i;
2538 GLboolean keepdepth = GL_FALSE, keepstencil = GL_FALSE;
2539
2540 if (srcFormat == GL_DEPTH_COMPONENT) { /* preserve stencil */
2541 keepstencil = GL_TRUE;
2542 }
2543 else if (srcFormat == GL_STENCIL_INDEX) { /* preserve depth */
2544 keepdepth = GL_TRUE;
2545 }
2546
2547 if (keepdepth == GL_FALSE)
2548 /* the 24 depth bits will be in the low position: */
2549 _mesa_unpack_depth_span(ctx, srcWidth,
2550 GL_UNSIGNED_INT, /* dst type */
2551 keepstencil ? depth : dstRow, /* dst addr */
2552 depthScale,
2553 srcType, src, srcPacking);
2554
2555 if (keepstencil == GL_FALSE)
2556 /* get the 8-bit stencil values */
2557 _mesa_unpack_stencil_span(ctx, srcWidth,
2558 GL_UNSIGNED_BYTE, /* dst type */
2559 stencil, /* dst addr */
2560 srcType, src, srcPacking,
2561 ctx->_ImageTransferState);
2562
2563 /* merge stencil values into depth values */
2564 for (i = 0; i < srcWidth; i++) {
2565 if (keepstencil)
2566 dstRow[i] = depth[i] | (dstRow[i] & 0xFF000000);
2567 else
2568 dstRow[i] = (dstRow[i] & 0xFFFFFF) | (stencil[i] << 24);
2569
2570 }
2571 src += srcRowStride;
2572 dstRow += dstRowStride / sizeof(GLuint);
2573 }
2574 }
2575
2576 free(depth);
2577 free(stencil);
2578
2579 return GL_TRUE;
2580 }
2581
2582
2583 /**
2584 * Store simple 8-bit/value stencil texture data.
2585 */
2586 static GLboolean
2587 _mesa_texstore_s8(TEXSTORE_PARAMS)
2588 {
2589 ASSERT(dstFormat == MESA_FORMAT_S_UINT8);
2590 ASSERT(srcFormat == GL_STENCIL_INDEX);
2591
2592 {
2593 const GLint srcRowStride
2594 = _mesa_image_row_stride(srcPacking, srcWidth, srcFormat, srcType);
2595 GLint img, row;
2596 GLubyte *stencil = malloc(srcWidth * sizeof(GLubyte));
2597
2598 if (!stencil)
2599 return GL_FALSE;
2600
2601 for (img = 0; img < srcDepth; img++) {
2602 GLubyte *dstRow = dstSlices[img];
2603 const GLubyte *src
2604 = (const GLubyte *) _mesa_image_address(dims, srcPacking, srcAddr,
2605 srcWidth, srcHeight,
2606 srcFormat, srcType,
2607 img, 0, 0);
2608 for (row = 0; row < srcHeight; row++) {
2609 GLint i;
2610
2611 /* get the 8-bit stencil values */
2612 _mesa_unpack_stencil_span(ctx, srcWidth,
2613 GL_UNSIGNED_BYTE, /* dst type */
2614 stencil, /* dst addr */
2615 srcType, src, srcPacking,
2616 ctx->_ImageTransferState);
2617 /* merge stencil values into depth values */
2618 for (i = 0; i < srcWidth; i++)
2619 dstRow[i] = stencil[i];
2620
2621 src += srcRowStride;
2622 dstRow += dstRowStride / sizeof(GLubyte);
2623 }
2624 }
2625
2626 free(stencil);
2627 }
2628
2629 return GL_TRUE;
2630 }
2631
2632
2633 /**
2634 * Store an image in any of the formats:
2635 * _mesa_texformat_rgba_float32
2636 * _mesa_texformat_rgb_float32
2637 * _mesa_texformat_alpha_float32
2638 * _mesa_texformat_luminance_float32
2639 * _mesa_texformat_luminance_alpha_float32
2640 * _mesa_texformat_intensity_float32
2641 */
2642 static GLboolean
2643 _mesa_texstore_rgba_float32(TEXSTORE_PARAMS)
2644 {
2645 GLenum baseFormat = _mesa_get_format_base_format(dstFormat);
2646 GLint components = _mesa_components_in_format(baseFormat);
2647
2648 /* this forces alpha to 1 in _mesa_make_temp_float_image */
2649 if (dstFormat == MESA_FORMAT_RGBX_FLOAT32) {
2650 baseFormat = GL_RGBA;
2651 components = 4;
2652 }
2653
2654 ASSERT(dstFormat == MESA_FORMAT_RGBA_FLOAT32 ||
2655 dstFormat == MESA_FORMAT_RGB_FLOAT32 ||
2656 dstFormat == MESA_FORMAT_A_FLOAT32 ||
2657 dstFormat == MESA_FORMAT_L_FLOAT32 ||
2658 dstFormat == MESA_FORMAT_LA_FLOAT32 ||
2659 dstFormat == MESA_FORMAT_I_FLOAT32 ||
2660 dstFormat == MESA_FORMAT_R_FLOAT32 ||
2661 dstFormat == MESA_FORMAT_RG_FLOAT32 ||
2662 dstFormat == MESA_FORMAT_RGBX_FLOAT32);
2663 ASSERT(baseInternalFormat == GL_RGBA ||
2664 baseInternalFormat == GL_RGB ||
2665 baseInternalFormat == GL_ALPHA ||
2666 baseInternalFormat == GL_LUMINANCE ||
2667 baseInternalFormat == GL_LUMINANCE_ALPHA ||
2668 baseInternalFormat == GL_INTENSITY ||
2669 baseInternalFormat == GL_RED ||
2670 baseInternalFormat == GL_RG);
2671 ASSERT(_mesa_get_format_bytes(dstFormat) == components * sizeof(GLfloat));
2672
2673 {
2674 /* general path */
2675 const GLfloat *tempImage = _mesa_make_temp_float_image(ctx, dims,
2676 baseInternalFormat,
2677 baseFormat,
2678 srcWidth, srcHeight, srcDepth,
2679 srcFormat, srcType, srcAddr,
2680 srcPacking,
2681 ctx->_ImageTransferState);
2682 const GLfloat *srcRow = tempImage;
2683 GLint bytesPerRow;
2684 GLint img, row;
2685 if (!tempImage)
2686 return GL_FALSE;
2687 bytesPerRow = srcWidth * components * sizeof(GLfloat);
2688 for (img = 0; img < srcDepth; img++) {
2689 GLubyte *dstRow = dstSlices[img];
2690 for (row = 0; row < srcHeight; row++) {
2691 memcpy(dstRow, srcRow, bytesPerRow);
2692 dstRow += dstRowStride;
2693 srcRow += srcWidth * components;
2694 }
2695 }
2696
2697 free((void *) tempImage);
2698 }
2699 return GL_TRUE;
2700 }
2701
2702
2703
2704 /**
2705 * As above, but store 16-bit floats.
2706 */
2707 static GLboolean
2708 _mesa_texstore_rgba_float16(TEXSTORE_PARAMS)
2709 {
2710 GLenum baseFormat = _mesa_get_format_base_format(dstFormat);
2711 GLint components = _mesa_components_in_format(baseFormat);
2712
2713 /* this forces alpha to 1 in _mesa_make_temp_float_image */
2714 if (dstFormat == MESA_FORMAT_RGBX_FLOAT16) {
2715 baseFormat = GL_RGBA;
2716 components = 4;
2717 }
2718
2719 ASSERT(dstFormat == MESA_FORMAT_RGBA_FLOAT16 ||
2720 dstFormat == MESA_FORMAT_RGB_FLOAT16 ||
2721 dstFormat == MESA_FORMAT_A_FLOAT16 ||
2722 dstFormat == MESA_FORMAT_L_FLOAT16 ||
2723 dstFormat == MESA_FORMAT_LA_FLOAT16 ||
2724 dstFormat == MESA_FORMAT_I_FLOAT16 ||
2725 dstFormat == MESA_FORMAT_R_FLOAT16 ||
2726 dstFormat == MESA_FORMAT_RG_FLOAT16 ||
2727 dstFormat == MESA_FORMAT_RGBX_FLOAT16);
2728 ASSERT(baseInternalFormat == GL_RGBA ||
2729 baseInternalFormat == GL_RGB ||
2730 baseInternalFormat == GL_ALPHA ||
2731 baseInternalFormat == GL_LUMINANCE ||
2732 baseInternalFormat == GL_LUMINANCE_ALPHA ||
2733 baseInternalFormat == GL_INTENSITY ||
2734 baseInternalFormat == GL_RED ||
2735 baseInternalFormat == GL_RG);
2736 ASSERT(_mesa_get_format_bytes(dstFormat) == components * sizeof(GLhalfARB));
2737
2738 {
2739 /* general path */
2740 const GLfloat *tempImage = _mesa_make_temp_float_image(ctx, dims,
2741 baseInternalFormat,
2742 baseFormat,
2743 srcWidth, srcHeight, srcDepth,
2744 srcFormat, srcType, srcAddr,
2745 srcPacking,
2746 ctx->_ImageTransferState);
2747 const GLfloat *src = tempImage;
2748 GLint img, row;
2749 if (!tempImage)
2750 return GL_FALSE;
2751 for (img = 0; img < srcDepth; img++) {
2752 GLubyte *dstRow = dstSlices[img];
2753 for (row = 0; row < srcHeight; row++) {
2754 GLhalfARB *dstTexel = (GLhalfARB *) dstRow;
2755 GLint i;
2756 for (i = 0; i < srcWidth * components; i++) {
2757 dstTexel[i] = _mesa_float_to_half(src[i]);
2758 }
2759 dstRow += dstRowStride;
2760 src += srcWidth * components;
2761 }
2762 }
2763
2764 free((void *) tempImage);
2765 }
2766 return GL_TRUE;
2767 }
2768
2769
2770 /* non-normalized, signed int8 */
2771 static GLboolean
2772 _mesa_texstore_rgba_int8(TEXSTORE_PARAMS)
2773 {
2774 GLenum baseFormat = _mesa_get_format_base_format(dstFormat);
2775 GLint components = _mesa_components_in_format(baseFormat);
2776
2777 /* this forces alpha to 1 in make_temp_uint_image */
2778 if (dstFormat == MESA_FORMAT_RGBX_SINT8) {
2779 baseFormat = GL_RGBA;
2780 components = 4;
2781 }
2782
2783 ASSERT(dstFormat == MESA_FORMAT_R_SINT8 ||
2784 dstFormat == MESA_FORMAT_RG_SINT8 ||
2785 dstFormat == MESA_FORMAT_RGB_SINT8 ||
2786 dstFormat == MESA_FORMAT_RGBA_SINT8 ||
2787 dstFormat == MESA_FORMAT_A_SINT8 ||
2788 dstFormat == MESA_FORMAT_I_SINT8 ||
2789 dstFormat == MESA_FORMAT_L_SINT8 ||
2790 dstFormat == MESA_FORMAT_LA_SINT8 ||
2791 dstFormat == MESA_FORMAT_RGBX_SINT8);
2792 ASSERT(baseInternalFormat == GL_RGBA ||
2793 baseInternalFormat == GL_RGB ||
2794 baseInternalFormat == GL_RG ||
2795 baseInternalFormat == GL_RED ||
2796 baseInternalFormat == GL_ALPHA ||
2797 baseInternalFormat == GL_LUMINANCE ||
2798 baseInternalFormat == GL_LUMINANCE_ALPHA ||
2799 baseInternalFormat == GL_INTENSITY);
2800 ASSERT(_mesa_get_format_bytes(dstFormat) == components * sizeof(GLbyte));
2801
2802 {
2803 /* general path */
2804 const GLuint *tempImage = make_temp_uint_image(ctx, dims,
2805 baseInternalFormat,
2806 baseFormat,
2807 srcWidth, srcHeight, srcDepth,
2808 srcFormat, srcType,
2809 srcAddr,
2810 srcPacking);
2811 const GLuint *src = tempImage;
2812 GLint img, row;
2813 GLboolean is_unsigned = _mesa_is_type_unsigned(srcType);
2814 if (!tempImage)
2815 return GL_FALSE;
2816 for (img = 0; img < srcDepth; img++) {
2817 GLubyte *dstRow = dstSlices[img];
2818 for (row = 0; row < srcHeight; row++) {
2819 GLbyte *dstTexel = (GLbyte *) dstRow;
2820 GLint i;
2821 if (is_unsigned) {
2822 for (i = 0; i < srcWidth * components; i++) {
2823 dstTexel[i] = (GLbyte) MIN2(src[i], 0x7f);
2824 }
2825 } else {
2826 for (i = 0; i < srcWidth * components; i++) {
2827 dstTexel[i] = (GLbyte) CLAMP((GLint) src[i], -0x80, 0x7f);
2828 }
2829 }
2830 dstRow += dstRowStride;
2831 src += srcWidth * components;
2832 }
2833 }
2834
2835 free((void *) tempImage);
2836 }
2837 return GL_TRUE;
2838 }
2839
2840
2841 /* non-normalized, signed int16 */
2842 static GLboolean
2843 _mesa_texstore_rgba_int16(TEXSTORE_PARAMS)
2844 {
2845 GLenum baseFormat = _mesa_get_format_base_format(dstFormat);
2846 GLint components = _mesa_components_in_format(baseFormat);
2847
2848 /* this forces alpha to 1 in make_temp_uint_image */
2849 if (dstFormat == MESA_FORMAT_RGBX_SINT16) {
2850 baseFormat = GL_RGBA;
2851 components = 4;
2852 }
2853
2854 ASSERT(dstFormat == MESA_FORMAT_R_SINT16 ||
2855 dstFormat == MESA_FORMAT_RG_SINT16 ||
2856 dstFormat == MESA_FORMAT_RGB_SINT16 ||
2857 dstFormat == MESA_FORMAT_RGBA_SINT16 ||
2858 dstFormat == MESA_FORMAT_A_SINT16 ||
2859 dstFormat == MESA_FORMAT_L_SINT16 ||
2860 dstFormat == MESA_FORMAT_I_SINT16 ||
2861 dstFormat == MESA_FORMAT_LA_SINT16 ||
2862 dstFormat == MESA_FORMAT_RGBX_SINT16);
2863 ASSERT(baseInternalFormat == GL_RGBA ||
2864 baseInternalFormat == GL_RGB ||
2865 baseInternalFormat == GL_RG ||
2866 baseInternalFormat == GL_RED ||
2867 baseInternalFormat == GL_ALPHA ||
2868 baseInternalFormat == GL_LUMINANCE ||
2869 baseInternalFormat == GL_LUMINANCE_ALPHA ||
2870 baseInternalFormat == GL_INTENSITY);
2871 ASSERT(_mesa_get_format_bytes(dstFormat) == components * sizeof(GLshort));
2872
2873 {
2874 /* general path */
2875 const GLuint *tempImage = make_temp_uint_image(ctx, dims,
2876 baseInternalFormat,
2877 baseFormat,
2878 srcWidth, srcHeight, srcDepth,
2879 srcFormat, srcType,
2880 srcAddr,
2881 srcPacking);
2882 const GLuint *src = tempImage;
2883 GLint img, row;
2884 GLboolean is_unsigned = _mesa_is_type_unsigned(srcType);
2885 if (!tempImage)
2886 return GL_FALSE;
2887 for (img = 0; img < srcDepth; img++) {
2888 GLubyte *dstRow = dstSlices[img];
2889 for (row = 0; row < srcHeight; row++) {
2890 GLshort *dstTexel = (GLshort *) dstRow;
2891 GLint i;
2892 if (is_unsigned) {
2893 for (i = 0; i < srcWidth * components; i++) {
2894 dstTexel[i] = (GLshort) MIN2(src[i], 0x7fff);
2895 }
2896 } else {
2897 for (i = 0; i < srcWidth * components; i++) {
2898 dstTexel[i] = (GLshort)CLAMP((GLint) src[i], -0x8000, 0x7fff);
2899 }
2900 }
2901 dstRow += dstRowStride;
2902 src += srcWidth * components;
2903 }
2904 }
2905
2906 free((void *) tempImage);
2907 }
2908 return GL_TRUE;
2909 }
2910
2911
2912 /* non-normalized, signed int32 */
2913 static GLboolean
2914 _mesa_texstore_rgba_int32(TEXSTORE_PARAMS)
2915 {
2916 GLenum baseFormat = _mesa_get_format_base_format(dstFormat);
2917 GLint components = _mesa_components_in_format(baseFormat);
2918
2919 /* this forces alpha to 1 in make_temp_uint_image */
2920 if (dstFormat == MESA_FORMAT_RGBX_SINT32) {
2921 baseFormat = GL_RGBA;
2922 components = 4;
2923 }
2924
2925 ASSERT(dstFormat == MESA_FORMAT_R_SINT32 ||
2926 dstFormat == MESA_FORMAT_RG_SINT32 ||
2927 dstFormat == MESA_FORMAT_RGB_SINT32 ||
2928 dstFormat == MESA_FORMAT_RGBA_SINT32 ||
2929 dstFormat == MESA_FORMAT_A_SINT32 ||
2930 dstFormat == MESA_FORMAT_I_SINT32 ||
2931 dstFormat == MESA_FORMAT_L_SINT32 ||
2932 dstFormat == MESA_FORMAT_LA_SINT32 ||
2933 dstFormat == MESA_FORMAT_RGBX_SINT32);
2934 ASSERT(baseInternalFormat == GL_RGBA ||
2935 baseInternalFormat == GL_RGB ||
2936 baseInternalFormat == GL_RG ||
2937 baseInternalFormat == GL_RED ||
2938 baseInternalFormat == GL_ALPHA ||
2939 baseInternalFormat == GL_LUMINANCE ||
2940 baseInternalFormat == GL_LUMINANCE_ALPHA ||
2941 baseInternalFormat == GL_INTENSITY);
2942 ASSERT(_mesa_get_format_bytes(dstFormat) == components * sizeof(GLint));
2943
2944 {
2945 /* general path */
2946 const GLuint *tempImage = make_temp_uint_image(ctx, dims,
2947 baseInternalFormat,
2948 baseFormat,
2949 srcWidth, srcHeight, srcDepth,
2950 srcFormat, srcType,
2951 srcAddr,
2952 srcPacking);
2953 const GLuint *src = tempImage;
2954 GLint img, row;
2955 GLboolean is_unsigned = _mesa_is_type_unsigned(srcType);
2956 if (!tempImage)
2957 return GL_FALSE;
2958 for (img = 0; img < srcDepth; img++) {
2959 GLubyte *dstRow = dstSlices[img];
2960 for (row = 0; row < srcHeight; row++) {
2961 GLint *dstTexel = (GLint *) dstRow;
2962 GLint i;
2963 if (is_unsigned) {
2964 for (i = 0; i < srcWidth * components; i++) {
2965 dstTexel[i] = (GLint) MIN2(src[i], 0x7fffffff);
2966 }
2967 } else {
2968 for (i = 0; i < srcWidth * components; i++) {
2969 dstTexel[i] = (GLint) src[i];
2970 }
2971 }
2972 dstRow += dstRowStride;
2973 src += srcWidth * components;
2974 }
2975 }
2976
2977 free((void *) tempImage);
2978 }
2979 return GL_TRUE;
2980 }
2981
2982
2983 /* non-normalized, unsigned int8 */
2984 static GLboolean
2985 _mesa_texstore_rgba_uint8(TEXSTORE_PARAMS)
2986 {
2987 GLenum baseFormat = _mesa_get_format_base_format(dstFormat);
2988 GLint components = _mesa_components_in_format(baseFormat);
2989
2990 /* this forces alpha to 1 in make_temp_uint_image */
2991 if (dstFormat == MESA_FORMAT_RGBX_UINT8) {
2992 baseFormat = GL_RGBA;
2993 components = 4;
2994 }
2995
2996 ASSERT(dstFormat == MESA_FORMAT_R_UINT8 ||
2997 dstFormat == MESA_FORMAT_RG_UINT8 ||
2998 dstFormat == MESA_FORMAT_RGB_UINT8 ||
2999 dstFormat == MESA_FORMAT_RGBA_UINT8 ||
3000 dstFormat == MESA_FORMAT_A_UINT8 ||
3001 dstFormat == MESA_FORMAT_I_UINT8 ||
3002 dstFormat == MESA_FORMAT_L_UINT8 ||
3003 dstFormat == MESA_FORMAT_LA_UINT8 ||
3004 dstFormat == MESA_FORMAT_RGBX_UINT8);
3005 ASSERT(baseInternalFormat == GL_RGBA ||
3006 baseInternalFormat == GL_RGB ||
3007 baseInternalFormat == GL_RG ||
3008 baseInternalFormat == GL_RED ||
3009 baseInternalFormat == GL_ALPHA ||
3010 baseInternalFormat == GL_LUMINANCE ||
3011 baseInternalFormat == GL_LUMINANCE_ALPHA ||
3012 baseInternalFormat == GL_INTENSITY);
3013 ASSERT(_mesa_get_format_bytes(dstFormat) == components * sizeof(GLubyte));
3014
3015 {
3016 /* general path */
3017 const GLuint *tempImage =
3018 make_temp_uint_image(ctx, dims, baseInternalFormat, baseFormat,
3019 srcWidth, srcHeight, srcDepth,
3020 srcFormat, srcType, srcAddr, srcPacking);
3021 const GLuint *src = tempImage;
3022 GLint img, row;
3023 GLboolean is_unsigned = _mesa_is_type_unsigned(srcType);
3024 if (!tempImage)
3025 return GL_FALSE;
3026 for (img = 0; img < srcDepth; img++) {
3027 GLubyte *dstRow = dstSlices[img];
3028 for (row = 0; row < srcHeight; row++) {
3029 GLubyte *dstTexel = (GLubyte *) dstRow;
3030 GLint i;
3031 if (is_unsigned) {
3032 for (i = 0; i < srcWidth * components; i++) {
3033 dstTexel[i] = (GLubyte) MIN2(src[i], 0xff);
3034 }
3035 } else {
3036 for (i = 0; i < srcWidth * components; i++) {
3037 dstTexel[i] = (GLubyte) CLAMP((GLint) src[i], 0, 0xff);
3038 }
3039 }
3040 dstRow += dstRowStride;
3041 src += srcWidth * components;
3042 }
3043 }
3044
3045 free((void *) tempImage);
3046 }
3047 return GL_TRUE;
3048 }
3049
3050
3051 /* non-normalized, unsigned int16 */
3052 static GLboolean
3053 _mesa_texstore_rgba_uint16(TEXSTORE_PARAMS)
3054 {
3055 GLenum baseFormat = _mesa_get_format_base_format(dstFormat);
3056 GLint components = _mesa_components_in_format(baseFormat);
3057
3058 /* this forces alpha to 1 in make_temp_uint_image */
3059 if (dstFormat == MESA_FORMAT_RGBX_UINT16) {
3060 baseFormat = GL_RGBA;
3061 components = 4;
3062 }
3063
3064 ASSERT(dstFormat == MESA_FORMAT_R_UINT16 ||
3065 dstFormat == MESA_FORMAT_RG_UINT16 ||
3066 dstFormat == MESA_FORMAT_RGB_UINT16 ||
3067 dstFormat == MESA_FORMAT_RGBA_UINT16 ||
3068 dstFormat == MESA_FORMAT_A_UINT16 ||
3069 dstFormat == MESA_FORMAT_I_UINT16 ||
3070 dstFormat == MESA_FORMAT_L_UINT16 ||
3071 dstFormat == MESA_FORMAT_LA_UINT16 ||
3072 dstFormat == MESA_FORMAT_RGBX_UINT16);
3073 ASSERT(baseInternalFormat == GL_RGBA ||
3074 baseInternalFormat == GL_RGB ||
3075 baseInternalFormat == GL_RG ||
3076 baseInternalFormat == GL_RED ||
3077 baseInternalFormat == GL_ALPHA ||
3078 baseInternalFormat == GL_LUMINANCE ||
3079 baseInternalFormat == GL_LUMINANCE_ALPHA ||
3080 baseInternalFormat == GL_INTENSITY);
3081 ASSERT(_mesa_get_format_bytes(dstFormat) == components * sizeof(GLushort));
3082
3083 {
3084 /* general path */
3085 const GLuint *tempImage =
3086 make_temp_uint_image(ctx, dims, baseInternalFormat, baseFormat,
3087 srcWidth, srcHeight, srcDepth,
3088 srcFormat, srcType, srcAddr, srcPacking);
3089 const GLuint *src = tempImage;
3090 GLint img, row;
3091 GLboolean is_unsigned = _mesa_is_type_unsigned(srcType);
3092 if (!tempImage)
3093 return GL_FALSE;
3094 for (img = 0; img < srcDepth; img++) {
3095 GLubyte *dstRow = dstSlices[img];
3096 for (row = 0; row < srcHeight; row++) {
3097 GLushort *dstTexel = (GLushort *) dstRow;
3098 GLint i;
3099 if (is_unsigned) {
3100 for (i = 0; i < srcWidth * components; i++) {
3101 dstTexel[i] = (GLushort) MIN2(src[i], 0xffff);
3102 }
3103 } else {
3104 for (i = 0; i < srcWidth * components; i++) {
3105 dstTexel[i] = (GLushort) CLAMP((GLint) src[i], 0, 0xffff);
3106 }
3107 }
3108 dstRow += dstRowStride;
3109 src += srcWidth * components;
3110 }
3111 }
3112
3113 free((void *) tempImage);
3114 }
3115 return GL_TRUE;
3116 }
3117
3118
3119 /* non-normalized, unsigned int32 */
3120 static GLboolean
3121 _mesa_texstore_rgba_uint32(TEXSTORE_PARAMS)
3122 {
3123 GLenum baseFormat = _mesa_get_format_base_format(dstFormat);
3124 GLint components = _mesa_components_in_format(baseFormat);
3125
3126 /* this forces alpha to 1 in make_temp_uint_image */
3127 if (dstFormat == MESA_FORMAT_RGBX_UINT32) {
3128 baseFormat = GL_RGBA;
3129 components = 4;
3130 }
3131
3132 ASSERT(dstFormat == MESA_FORMAT_R_UINT32 ||
3133 dstFormat == MESA_FORMAT_RG_UINT32 ||
3134 dstFormat == MESA_FORMAT_RGB_UINT32 ||
3135 dstFormat == MESA_FORMAT_RGBA_UINT32 ||
3136 dstFormat == MESA_FORMAT_A_UINT32 ||
3137 dstFormat == MESA_FORMAT_I_UINT32 ||
3138 dstFormat == MESA_FORMAT_L_UINT32 ||
3139 dstFormat == MESA_FORMAT_LA_UINT32 ||
3140 dstFormat == MESA_FORMAT_RGBX_UINT32);
3141 ASSERT(baseInternalFormat == GL_RGBA ||
3142 baseInternalFormat == GL_RGB ||
3143 baseInternalFormat == GL_RG ||
3144 baseInternalFormat == GL_RED ||
3145 baseInternalFormat == GL_ALPHA ||
3146 baseInternalFormat == GL_LUMINANCE ||
3147 baseInternalFormat == GL_LUMINANCE_ALPHA ||
3148 baseInternalFormat == GL_INTENSITY);
3149 ASSERT(_mesa_get_format_bytes(dstFormat) == components * sizeof(GLuint));
3150
3151 {
3152 /* general path */
3153 const GLuint *tempImage =
3154 make_temp_uint_image(ctx, dims, baseInternalFormat, baseFormat,
3155 srcWidth, srcHeight, srcDepth,
3156 srcFormat, srcType, srcAddr, srcPacking);
3157 const GLuint *src = tempImage;
3158 GLboolean is_unsigned = _mesa_is_type_unsigned(srcType);
3159 GLint img, row;
3160 if (!tempImage)
3161 return GL_FALSE;
3162 for (img = 0; img < srcDepth; img++) {
3163 GLubyte *dstRow = dstSlices[img];
3164 for (row = 0; row < srcHeight; row++) {
3165 GLuint *dstTexel = (GLuint *) dstRow;
3166 GLint i;
3167 if (is_unsigned) {
3168 for (i = 0; i < srcWidth * components; i++) {
3169 dstTexel[i] = src[i];
3170 }
3171 } else {
3172 for (i = 0; i < srcWidth * components; i++) {
3173 dstTexel[i] = MAX2((GLint) src[i], 0);
3174 }
3175 }
3176 dstRow += dstRowStride;
3177 src += srcWidth * components;
3178 }
3179 }
3180
3181 free((void *) tempImage);
3182 }
3183 return GL_TRUE;
3184 }
3185
3186
3187 static GLboolean
3188 _mesa_texstore_srgb8(TEXSTORE_PARAMS)
3189 {
3190 mesa_format newDstFormat;
3191 GLboolean k;
3192
3193 ASSERT(dstFormat == MESA_FORMAT_BGR_SRGB8);
3194
3195 /* reuse normal rgb texstore code */
3196 newDstFormat = MESA_FORMAT_BGR_UNORM8;
3197
3198 k = _mesa_texstore_rgb888(ctx, dims, baseInternalFormat,
3199 newDstFormat,
3200 dstRowStride, dstSlices,
3201 srcWidth, srcHeight, srcDepth,
3202 srcFormat, srcType,
3203 srcAddr, srcPacking);
3204 return k;
3205 }
3206
3207
3208 static GLboolean
3209 _mesa_texstore_srgba8(TEXSTORE_PARAMS)
3210 {
3211 mesa_format newDstFormat;
3212 GLboolean k;
3213
3214 ASSERT(dstFormat == MESA_FORMAT_A8B8G8R8_SRGB ||
3215 dstFormat == MESA_FORMAT_R8G8B8X8_SRGB ||
3216 dstFormat == MESA_FORMAT_R8G8B8A8_SRGB);
3217
3218 newDstFormat = _mesa_get_srgb_format_linear(dstFormat);
3219
3220 k = _mesa_texstore_rgba8888(ctx, dims, baseInternalFormat,
3221 newDstFormat,
3222 dstRowStride, dstSlices,
3223 srcWidth, srcHeight, srcDepth,
3224 srcFormat, srcType,
3225 srcAddr, srcPacking);
3226 return k;
3227 }
3228
3229
3230 static GLboolean
3231 _mesa_texstore_sargb8(TEXSTORE_PARAMS)
3232 {
3233 mesa_format newDstFormat;
3234 GLboolean k;
3235
3236 assert(dstFormat == MESA_FORMAT_B8G8R8A8_SRGB ||
3237 dstFormat == MESA_FORMAT_B8G8R8X8_SRGB);
3238
3239 newDstFormat = _mesa_get_srgb_format_linear(dstFormat);
3240
3241 k = _mesa_texstore_argb8888(ctx, dims, baseInternalFormat,
3242 newDstFormat,
3243 dstRowStride, dstSlices,
3244 srcWidth, srcHeight, srcDepth,
3245 srcFormat, srcType,
3246 srcAddr, srcPacking);
3247 return k;
3248 }
3249
3250
3251 static GLboolean
3252 _mesa_texstore_sl8(TEXSTORE_PARAMS)
3253 {
3254 mesa_format newDstFormat;
3255 GLboolean k;
3256
3257 ASSERT(dstFormat == MESA_FORMAT_L_SRGB8);
3258
3259 newDstFormat = MESA_FORMAT_L_UNORM8;
3260
3261 /* _mesa_textore_a8 handles luminance8 too */
3262 k = _mesa_texstore_unorm8(ctx, dims, baseInternalFormat,
3263 newDstFormat,
3264 dstRowStride, dstSlices,
3265 srcWidth, srcHeight, srcDepth,
3266 srcFormat, srcType,
3267 srcAddr, srcPacking);
3268 return k;
3269 }
3270
3271
3272 static GLboolean
3273 _mesa_texstore_sla8(TEXSTORE_PARAMS)
3274 {
3275 mesa_format newDstFormat;
3276 GLboolean k;
3277
3278 ASSERT(dstFormat == MESA_FORMAT_L8A8_SRGB);
3279
3280 /* reuse normal luminance/alpha texstore code */
3281 newDstFormat = MESA_FORMAT_L8A8_UNORM;
3282
3283 k = _mesa_texstore_unorm88(ctx, dims, baseInternalFormat,
3284 newDstFormat,
3285 dstRowStride, dstSlices,
3286 srcWidth, srcHeight, srcDepth,
3287 srcFormat, srcType,
3288 srcAddr, srcPacking);
3289 return k;
3290 }
3291
3292 static GLboolean
3293 _mesa_texstore_rgb9_e5(TEXSTORE_PARAMS)
3294 {
3295 const GLenum baseFormat = _mesa_get_format_base_format(dstFormat);
3296
3297 ASSERT(dstFormat == MESA_FORMAT_R9G9B9E5_FLOAT);
3298 ASSERT(baseInternalFormat == GL_RGB);
3299
3300 {
3301 /* general path */
3302 const GLfloat *tempImage = _mesa_make_temp_float_image(ctx, dims,
3303 baseInternalFormat,
3304 baseFormat,
3305 srcWidth, srcHeight, srcDepth,
3306 srcFormat, srcType, srcAddr,
3307 srcPacking,
3308 ctx->_ImageTransferState);
3309 const GLfloat *srcRow = tempImage;
3310 GLint img, row, col;
3311 if (!tempImage)
3312 return GL_FALSE;
3313 for (img = 0; img < srcDepth; img++) {
3314 GLubyte *dstRow = dstSlices[img];
3315 for (row = 0; row < srcHeight; row++) {
3316 GLuint *dstUI = (GLuint*)dstRow;
3317 for (col = 0; col < srcWidth; col++) {
3318 dstUI[col] = float3_to_rgb9e5(&srcRow[col * 3]);
3319 }
3320 dstRow += dstRowStride;
3321 srcRow += srcWidth * 3;
3322 }
3323 }
3324
3325 free((void *) tempImage);
3326 }
3327 return GL_TRUE;
3328 }
3329
3330 static GLboolean
3331 _mesa_texstore_r11_g11_b10f(TEXSTORE_PARAMS)
3332 {
3333 const GLenum baseFormat = _mesa_get_format_base_format(dstFormat);
3334
3335 ASSERT(dstFormat == MESA_FORMAT_R11G11B10_FLOAT);
3336 ASSERT(baseInternalFormat == GL_RGB);
3337
3338 {
3339 /* general path */
3340 const GLfloat *tempImage = _mesa_make_temp_float_image(ctx, dims,
3341 baseInternalFormat,
3342 baseFormat,
3343 srcWidth, srcHeight, srcDepth,
3344 srcFormat, srcType, srcAddr,
3345 srcPacking,
3346 ctx->_ImageTransferState);
3347 const GLfloat *srcRow = tempImage;
3348 GLint img, row, col;
3349 if (!tempImage)
3350 return GL_FALSE;
3351 for (img = 0; img < srcDepth; img++) {
3352 GLubyte *dstRow = dstSlices[img];
3353 for (row = 0; row < srcHeight; row++) {
3354 GLuint *dstUI = (GLuint*)dstRow;
3355 for (col = 0; col < srcWidth; col++) {
3356 dstUI[col] = float3_to_r11g11b10f(&srcRow[col * 3]);
3357 }
3358 dstRow += dstRowStride;
3359 srcRow += srcWidth * 3;
3360 }
3361 }
3362
3363 free((void *) tempImage);
3364 }
3365 return GL_TRUE;
3366 }
3367
3368
3369 static GLboolean
3370 _mesa_texstore_z32f_x24s8(TEXSTORE_PARAMS)
3371 {
3372 GLint img, row;
3373 const GLint srcRowStride
3374 = _mesa_image_row_stride(srcPacking, srcWidth, srcFormat, srcType)
3375 / sizeof(uint64_t);
3376
3377 ASSERT(dstFormat == MESA_FORMAT_Z32_FLOAT_S8X24_UINT);
3378 ASSERT(srcFormat == GL_DEPTH_STENCIL ||
3379 srcFormat == GL_DEPTH_COMPONENT ||
3380 srcFormat == GL_STENCIL_INDEX);
3381 ASSERT(srcFormat != GL_DEPTH_STENCIL ||
3382 srcType == GL_UNSIGNED_INT_24_8 ||
3383 srcType == GL_FLOAT_32_UNSIGNED_INT_24_8_REV);
3384
3385 /* In case we only upload depth we need to preserve the stencil */
3386 for (img = 0; img < srcDepth; img++) {
3387 uint64_t *dstRow = (uint64_t *) dstSlices[img];
3388 const uint64_t *src
3389 = (const uint64_t *) _mesa_image_address(dims, srcPacking, srcAddr,
3390 srcWidth, srcHeight,
3391 srcFormat, srcType,
3392 img, 0, 0);
3393 for (row = 0; row < srcHeight; row++) {
3394 /* The unpack functions with:
3395 * dstType = GL_FLOAT_32_UNSIGNED_INT_24_8_REV
3396 * only write their own dword, so the other dword (stencil
3397 * or depth) is preserved. */
3398 if (srcFormat != GL_STENCIL_INDEX)
3399 _mesa_unpack_depth_span(ctx, srcWidth,
3400 GL_FLOAT_32_UNSIGNED_INT_24_8_REV, /* dst type */
3401 dstRow, /* dst addr */
3402 ~0U, srcType, src, srcPacking);
3403
3404 if (srcFormat != GL_DEPTH_COMPONENT)
3405 _mesa_unpack_stencil_span(ctx, srcWidth,
3406 GL_FLOAT_32_UNSIGNED_INT_24_8_REV, /* dst type */
3407 dstRow, /* dst addr */
3408 srcType, src, srcPacking,
3409 ctx->_ImageTransferState);
3410
3411 src += srcRowStride;
3412 dstRow += dstRowStride / sizeof(uint64_t);
3413 }
3414 }
3415 return GL_TRUE;
3416 }
3417
3418 static GLboolean
3419 _mesa_texstore_argb2101010_uint(TEXSTORE_PARAMS)
3420 {
3421 const GLenum baseFormat = _mesa_get_format_base_format(dstFormat);
3422
3423 ASSERT(dstFormat == MESA_FORMAT_B10G10R10A2_UINT);
3424 ASSERT(_mesa_get_format_bytes(dstFormat) == 4);
3425
3426 {
3427 /* general path */
3428 const GLuint *tempImage = make_temp_uint_image(ctx, dims,
3429 baseInternalFormat,
3430 baseFormat,
3431 srcWidth, srcHeight,
3432 srcDepth, srcFormat,
3433 srcType, srcAddr,
3434 srcPacking);
3435 const GLuint *src = tempImage;
3436 GLint img, row, col;
3437 GLboolean is_unsigned = _mesa_is_type_unsigned(srcType);
3438 if (!tempImage)
3439 return GL_FALSE;
3440 for (img = 0; img < srcDepth; img++) {
3441 GLubyte *dstRow = dstSlices[img];
3442
3443 for (row = 0; row < srcHeight; row++) {
3444 GLuint *dstUI = (GLuint *) dstRow;
3445 if (is_unsigned) {
3446 for (col = 0; col < srcWidth; col++) {
3447 GLushort a,r,g,b;
3448 r = MIN2(src[RCOMP], 0x3ff);
3449 g = MIN2(src[GCOMP], 0x3ff);
3450 b = MIN2(src[BCOMP], 0x3ff);
3451 a = MIN2(src[ACOMP], 0x003);
3452 dstUI[col] = (a << 30) | (r << 20) | (g << 10) | (b);
3453 src += 4;
3454 }
3455 } else {
3456 for (col = 0; col < srcWidth; col++) {
3457 GLushort a,r,g,b;
3458 r = CLAMP((GLint) src[RCOMP], 0, 0x3ff);
3459 g = CLAMP((GLint) src[GCOMP], 0, 0x3ff);
3460 b = CLAMP((GLint) src[BCOMP], 0, 0x3ff);
3461 a = CLAMP((GLint) src[ACOMP], 0, 0x003);
3462 dstUI[col] = (a << 30) | (r << 20) | (g << 10) | (b);
3463 src += 4;
3464 }
3465 }
3466 dstRow += dstRowStride;
3467 }
3468 }
3469 free((void *) tempImage);
3470 }
3471 return GL_TRUE;
3472 }
3473
3474 static GLboolean
3475 _mesa_texstore_abgr2101010_uint(TEXSTORE_PARAMS)
3476 {
3477 const GLenum baseFormat = _mesa_get_format_base_format(dstFormat);
3478
3479 ASSERT(dstFormat == MESA_FORMAT_R10G10B10A2_UINT);
3480 ASSERT(_mesa_get_format_bytes(dstFormat) == 4);
3481
3482 {
3483 /* general path */
3484 const GLuint *tempImage = make_temp_uint_image(ctx, dims,
3485 baseInternalFormat,
3486 baseFormat,
3487 srcWidth, srcHeight,
3488 srcDepth, srcFormat,
3489 srcType, srcAddr,
3490 srcPacking);
3491 const GLuint *src = tempImage;
3492 GLint img, row, col;
3493 GLboolean is_unsigned = _mesa_is_type_unsigned(srcType);
3494 if (!tempImage)
3495 return GL_FALSE;
3496 for (img = 0; img < srcDepth; img++) {
3497 GLubyte *dstRow = dstSlices[img];
3498
3499 for (row = 0; row < srcHeight; row++) {
3500 GLuint *dstUI = (GLuint *) dstRow;
3501 if (is_unsigned) {
3502 for (col = 0; col < srcWidth; col++) {
3503 GLushort a,r,g,b;
3504 r = MIN2(src[RCOMP], 0x3ff);
3505 g = MIN2(src[GCOMP], 0x3ff);
3506 b = MIN2(src[BCOMP], 0x3ff);
3507 a = MIN2(src[ACOMP], 0x003);
3508 dstUI[col] = (a << 30) | (b << 20) | (g << 10) | (r);
3509 src += 4;
3510 }
3511 } else {
3512 for (col = 0; col < srcWidth; col++) {
3513 GLushort a,r,g,b;
3514 r = CLAMP((GLint) src[RCOMP], 0, 0x3ff);
3515 g = CLAMP((GLint) src[GCOMP], 0, 0x3ff);
3516 b = CLAMP((GLint) src[BCOMP], 0, 0x3ff);
3517 a = CLAMP((GLint) src[ACOMP], 0, 0x003);
3518 dstUI[col] = (a << 30) | (b << 20) | (g << 10) | (r);
3519 src += 4;
3520 }
3521 }
3522 dstRow += dstRowStride;
3523 }
3524 }
3525 free((void *) tempImage);
3526 }
3527 return GL_TRUE;
3528 }
3529
3530 static GLboolean
3531 _mesa_texstore_abgr2101010(TEXSTORE_PARAMS)
3532 {
3533 const GLenum baseFormat = _mesa_get_format_base_format(dstFormat);
3534
3535 ASSERT(dstFormat == MESA_FORMAT_R10G10B10A2_UNORM);
3536 ASSERT(_mesa_get_format_bytes(dstFormat) == 4);
3537
3538 {
3539 /* general path */
3540 const GLfloat *tempImage = _mesa_make_temp_float_image(ctx, dims,
3541 baseInternalFormat,
3542 baseFormat,
3543 srcWidth, srcHeight, srcDepth,
3544 srcFormat, srcType, srcAddr,
3545 srcPacking,
3546 ctx->_ImageTransferState);
3547 const GLfloat *src = tempImage;
3548 GLint img, row, col;
3549 if (!tempImage)
3550 return GL_FALSE;
3551 for (img = 0; img < srcDepth; img++) {
3552 GLubyte *dstRow = dstSlices[img];
3553
3554 for (row = 0; row < srcHeight; row++) {
3555 GLuint *dstUI = (GLuint *) dstRow;
3556 for (col = 0; col < srcWidth; col++) {
3557 GLushort a,r,g,b;
3558
3559 UNCLAMPED_FLOAT_TO_USHORT(a, src[ACOMP]);
3560 UNCLAMPED_FLOAT_TO_USHORT(r, src[RCOMP]);
3561 UNCLAMPED_FLOAT_TO_USHORT(g, src[GCOMP]);
3562 UNCLAMPED_FLOAT_TO_USHORT(b, src[BCOMP]);
3563 dstUI[col] = PACK_COLOR_2101010_US(a, b, g, r);
3564 src += 4;
3565 }
3566 dstRow += dstRowStride;
3567 }
3568 }
3569 free((void *) tempImage);
3570 }
3571 return GL_TRUE;
3572 }
3573
3574
3575 static GLboolean
3576 texstore_depth_stencil(TEXSTORE_PARAMS)
3577 {
3578 static StoreTexImageFunc table[MESA_FORMAT_COUNT];
3579 static GLboolean initialized = GL_FALSE;
3580
3581 if (!initialized) {
3582 memset(table, 0, sizeof table);
3583
3584 table[MESA_FORMAT_S8_UINT_Z24_UNORM] = _mesa_texstore_z24_s8;
3585 table[MESA_FORMAT_Z24_UNORM_S8_UINT] = _mesa_texstore_s8_z24;
3586 table[MESA_FORMAT_Z_UNORM16] = _mesa_texstore_z16;
3587 table[MESA_FORMAT_Z24_UNORM_X8_UINT] = _mesa_texstore_x8_z24;
3588 table[MESA_FORMAT_X8_UINT_Z24_UNORM] = _mesa_texstore_z24_x8;
3589 table[MESA_FORMAT_Z_UNORM32] = _mesa_texstore_z32;
3590 table[MESA_FORMAT_S_UINT8] = _mesa_texstore_s8;
3591 table[MESA_FORMAT_Z_FLOAT32] = _mesa_texstore_z32;
3592 table[MESA_FORMAT_Z32_FLOAT_S8X24_UINT] = _mesa_texstore_z32f_x24s8;
3593
3594 initialized = GL_TRUE;
3595 }
3596
3597 ASSERT(table[dstFormat]);
3598 return table[dstFormat](ctx, dims, baseInternalFormat,
3599 dstFormat, dstRowStride, dstSlices,
3600 srcWidth, srcHeight, srcDepth,
3601 srcFormat, srcType, srcAddr, srcPacking);
3602 }
3603
3604 static GLboolean
3605 texstore_compressed(TEXSTORE_PARAMS)
3606 {
3607 static StoreTexImageFunc table[MESA_FORMAT_COUNT];
3608 static GLboolean initialized = GL_FALSE;
3609
3610 if (!initialized) {
3611 memset(table, 0, sizeof table);
3612
3613 table[MESA_FORMAT_SRGB_DXT1] = _mesa_texstore_rgb_dxt1;
3614 table[MESA_FORMAT_SRGBA_DXT1] = _mesa_texstore_rgba_dxt1;
3615 table[MESA_FORMAT_SRGBA_DXT3] = _mesa_texstore_rgba_dxt3;
3616 table[MESA_FORMAT_SRGBA_DXT5] = _mesa_texstore_rgba_dxt5;
3617 table[MESA_FORMAT_RGB_FXT1] = _mesa_texstore_rgb_fxt1;
3618 table[MESA_FORMAT_RGBA_FXT1] = _mesa_texstore_rgba_fxt1;
3619 table[MESA_FORMAT_RGB_DXT1] = _mesa_texstore_rgb_dxt1;
3620 table[MESA_FORMAT_RGBA_DXT1] = _mesa_texstore_rgba_dxt1;
3621 table[MESA_FORMAT_RGBA_DXT3] = _mesa_texstore_rgba_dxt3;
3622 table[MESA_FORMAT_RGBA_DXT5] = _mesa_texstore_rgba_dxt5;
3623 table[MESA_FORMAT_R_RGTC1_UNORM] = _mesa_texstore_red_rgtc1;
3624 table[MESA_FORMAT_R_RGTC1_SNORM] = _mesa_texstore_signed_red_rgtc1;
3625 table[MESA_FORMAT_RG_RGTC2_UNORM] = _mesa_texstore_rg_rgtc2;
3626 table[MESA_FORMAT_RG_RGTC2_SNORM] = _mesa_texstore_signed_rg_rgtc2;
3627 table[MESA_FORMAT_L_LATC1_UNORM] = _mesa_texstore_red_rgtc1;
3628 table[MESA_FORMAT_L_LATC1_SNORM] = _mesa_texstore_signed_red_rgtc1;
3629 table[MESA_FORMAT_LA_LATC2_UNORM] = _mesa_texstore_rg_rgtc2;
3630 table[MESA_FORMAT_LA_LATC2_SNORM] = _mesa_texstore_signed_rg_rgtc2;
3631 table[MESA_FORMAT_ETC1_RGB8] = _mesa_texstore_etc1_rgb8;
3632 table[MESA_FORMAT_ETC2_RGB8] = _mesa_texstore_etc2_rgb8;
3633 table[MESA_FORMAT_ETC2_SRGB8] = _mesa_texstore_etc2_srgb8;
3634 table[MESA_FORMAT_ETC2_RGBA8_EAC] = _mesa_texstore_etc2_rgba8_eac;
3635 table[MESA_FORMAT_ETC2_SRGB8_ALPHA8_EAC] = _mesa_texstore_etc2_srgb8_alpha8_eac;
3636 table[MESA_FORMAT_ETC2_R11_EAC] = _mesa_texstore_etc2_r11_eac;
3637 table[MESA_FORMAT_ETC2_RG11_EAC] = _mesa_texstore_etc2_rg11_eac;
3638 table[MESA_FORMAT_ETC2_SIGNED_R11_EAC] = _mesa_texstore_etc2_signed_r11_eac;
3639 table[MESA_FORMAT_ETC2_SIGNED_RG11_EAC] = _mesa_texstore_etc2_signed_rg11_eac;
3640 table[MESA_FORMAT_ETC2_RGB8_PUNCHTHROUGH_ALPHA1] =
3641 _mesa_texstore_etc2_rgb8_punchthrough_alpha1;
3642 table[MESA_FORMAT_ETC2_SRGB8_PUNCHTHROUGH_ALPHA1] =
3643 _mesa_texstore_etc2_srgb8_punchthrough_alpha1;
3644
3645 initialized = GL_TRUE;
3646 }
3647
3648 ASSERT(table[dstFormat]);
3649 return table[dstFormat](ctx, dims, baseInternalFormat,
3650 dstFormat, dstRowStride, dstSlices,
3651 srcWidth, srcHeight, srcDepth,
3652 srcFormat, srcType, srcAddr, srcPacking);
3653 }
3654
3655 static void
3656 invert_swizzle(uint8_t dst[4], const uint8_t src[4])
3657 {
3658 int i, j;
3659
3660 dst[0] = MESA_FORMAT_SWIZZLE_NONE;
3661 dst[1] = MESA_FORMAT_SWIZZLE_NONE;
3662 dst[2] = MESA_FORMAT_SWIZZLE_NONE;
3663 dst[3] = MESA_FORMAT_SWIZZLE_NONE;
3664
3665 for (i = 0; i < 4; ++i)
3666 for (j = 0; j < 4; ++j)
3667 if (src[j] == i && dst[i] == MESA_FORMAT_SWIZZLE_NONE)
3668 dst[i] = j;
3669 }
3670
3671 /** Store a texture by per-channel conversions and swizzling.
3672 *
3673 * This function attempts to perform a texstore operation by doing simple
3674 * per-channel conversions and swizzling. This covers a huge chunk of the
3675 * texture storage operations that anyone cares about. If this function is
3676 * incapable of performing the operation, it bails and returns GL_FALSE.
3677 */
3678 static GLboolean
3679 texstore_swizzle(TEXSTORE_PARAMS)
3680 {
3681 const GLint srcRowStride = _mesa_image_row_stride(srcPacking, srcWidth,
3682 srcFormat, srcType);
3683 const GLint srcImageStride = _mesa_image_image_stride(srcPacking,
3684 srcWidth, srcHeight, srcFormat, srcType);
3685 const GLubyte *srcImage = (const GLubyte *) _mesa_image_address(dims,
3686 srcPacking, srcAddr, srcWidth, srcHeight, srcFormat, srcType, 0, 0, 0);
3687 const int src_components = _mesa_components_in_format(srcFormat);
3688
3689 GLubyte swizzle[4], rgba2base[6], base2src[6], rgba2dst[4], dst2rgba[4];
3690 const GLubyte *swap;
3691 GLenum dst_type;
3692 int dst_components;
3693 bool is_array, normalized, need_swap;
3694 GLint i, img, row;
3695 const GLubyte *src_row;
3696 GLubyte *dst_row;
3697
3698 is_array = _mesa_format_to_array(dstFormat, &dst_type, &dst_components,
3699 rgba2dst, &normalized);
3700
3701 if (!is_array)
3702 return GL_FALSE;
3703
3704 switch (srcType) {
3705 case GL_FLOAT:
3706 case GL_UNSIGNED_BYTE:
3707 case GL_BYTE:
3708 case GL_UNSIGNED_SHORT:
3709 case GL_SHORT:
3710 case GL_UNSIGNED_INT:
3711 case GL_INT:
3712 /* If wa have to swap bytes in a multi-byte datatype, that means
3713 * we're not doing an array conversion anymore */
3714 if (srcPacking->SwapBytes)
3715 return GL_FALSE;
3716 need_swap = false;
3717 break;
3718 case GL_UNSIGNED_INT_8_8_8_8:
3719 need_swap = srcPacking->SwapBytes;
3720 if (_mesa_little_endian())
3721 need_swap = !need_swap;
3722 srcType = GL_UNSIGNED_BYTE;
3723 break;
3724 case GL_UNSIGNED_INT_8_8_8_8_REV:
3725 need_swap = srcPacking->SwapBytes;
3726 if (!_mesa_little_endian())
3727 need_swap = !need_swap;
3728 srcType = GL_UNSIGNED_BYTE;
3729 break;
3730 default:
3731 return GL_FALSE;
3732 }
3733 swap = need_swap ? map_3210 : map_identity;
3734
3735 compute_component_mapping(srcFormat, baseInternalFormat, base2src);
3736 compute_component_mapping(baseInternalFormat, GL_RGBA, rgba2base);
3737 invert_swizzle(dst2rgba, rgba2dst);
3738
3739 for (i = 0; i < 4; i++) {
3740 if (dst2rgba[i] == MESA_FORMAT_SWIZZLE_NONE)
3741 swizzle[i] = MESA_FORMAT_SWIZZLE_NONE;
3742 else
3743 swizzle[i] = swap[base2src[rgba2base[dst2rgba[i]]]];
3744 }
3745
3746 /* Is it normalized? */
3747 normalized |= !_mesa_is_enum_format_integer(srcFormat);
3748
3749 for (img = 0; img < srcDepth; img++) {
3750 if (dstRowStride == srcWidth * dst_components &&
3751 srcRowStride == srcWidth * src_components) {
3752 _mesa_swizzle_and_convert(dstSlices[img], dst_type, dst_components,
3753 srcImage, srcType, src_components,
3754 swizzle, normalized, srcWidth * srcHeight);
3755 } else {
3756 src_row = srcImage;
3757 dst_row = dstSlices[img];
3758 for (row = 0; row < srcHeight; row++) {
3759 _mesa_swizzle_and_convert(dst_row, dst_type, dst_components,
3760 src_row, srcType, src_components,
3761 swizzle, normalized, srcWidth);
3762 dst_row += dstRowStride;
3763 src_row += srcRowStride;
3764 }
3765 }
3766 srcImage += srcImageStride;
3767 }
3768
3769 return GL_TRUE;
3770 }
3771
3772
3773 static GLboolean
3774 texstore_rgba(TEXSTORE_PARAMS)
3775 {
3776 static StoreTexImageFunc table[MESA_FORMAT_COUNT];
3777 static GLboolean initialized = GL_FALSE;
3778
3779 if (!initialized) {
3780 memset(table, 0, sizeof table);
3781
3782 table[MESA_FORMAT_A8B8G8R8_UNORM] = _mesa_texstore_rgba8888;
3783 table[MESA_FORMAT_R8G8B8A8_UNORM] = _mesa_texstore_rgba8888;
3784 table[MESA_FORMAT_B8G8R8A8_UNORM] = _mesa_texstore_argb8888;
3785 table[MESA_FORMAT_A8R8G8B8_UNORM] = _mesa_texstore_argb8888;
3786 table[MESA_FORMAT_X8B8G8R8_UNORM] = _mesa_texstore_rgba8888;
3787 table[MESA_FORMAT_R8G8B8X8_UNORM] = _mesa_texstore_rgba8888;
3788 table[MESA_FORMAT_B8G8R8X8_UNORM] = _mesa_texstore_argb8888;
3789 table[MESA_FORMAT_X8R8G8B8_UNORM] = _mesa_texstore_argb8888;
3790 table[MESA_FORMAT_BGR_UNORM8] = _mesa_texstore_rgb888;
3791 table[MESA_FORMAT_RGB_UNORM8] = _mesa_texstore_bgr888;
3792 table[MESA_FORMAT_B5G6R5_UNORM] = _mesa_texstore_rgb565;
3793 table[MESA_FORMAT_R5G6B5_UNORM] = _mesa_texstore_rgb565;
3794 table[MESA_FORMAT_B4G4R4A4_UNORM] = store_ubyte_texture;
3795 table[MESA_FORMAT_A4R4G4B4_UNORM] = store_ubyte_texture;
3796 table[MESA_FORMAT_A1B5G5R5_UNORM] = store_ubyte_texture;
3797 table[MESA_FORMAT_B5G5R5A1_UNORM] = store_ubyte_texture;
3798 table[MESA_FORMAT_A1R5G5B5_UNORM] = store_ubyte_texture;
3799 table[MESA_FORMAT_L4A4_UNORM] = _mesa_texstore_unorm44;
3800 table[MESA_FORMAT_L8A8_UNORM] = _mesa_texstore_unorm88;
3801 table[MESA_FORMAT_A8L8_UNORM] = _mesa_texstore_unorm88;
3802 table[MESA_FORMAT_L16A16_UNORM] = _mesa_texstore_unorm1616;
3803 table[MESA_FORMAT_A16L16_UNORM] = _mesa_texstore_unorm1616;
3804 table[MESA_FORMAT_B2G3R3_UNORM] = store_ubyte_texture;
3805 table[MESA_FORMAT_A_UNORM8] = _mesa_texstore_unorm8;
3806 table[MESA_FORMAT_A_UNORM16] = _mesa_texstore_unorm16;
3807 table[MESA_FORMAT_L_UNORM8] = _mesa_texstore_unorm8;
3808 table[MESA_FORMAT_L_UNORM16] = _mesa_texstore_unorm16;
3809 table[MESA_FORMAT_I_UNORM8] = _mesa_texstore_unorm8;
3810 table[MESA_FORMAT_I_UNORM16] = _mesa_texstore_unorm16;
3811 table[MESA_FORMAT_YCBCR] = _mesa_texstore_ycbcr;
3812 table[MESA_FORMAT_YCBCR_REV] = _mesa_texstore_ycbcr;
3813 table[MESA_FORMAT_R_UNORM8] = _mesa_texstore_unorm8;
3814 table[MESA_FORMAT_R8G8_UNORM] = _mesa_texstore_unorm88;
3815 table[MESA_FORMAT_G8R8_UNORM] = _mesa_texstore_unorm88;
3816 table[MESA_FORMAT_R_UNORM16] = _mesa_texstore_unorm16;
3817 table[MESA_FORMAT_R16G16_UNORM] = _mesa_texstore_unorm1616;
3818 table[MESA_FORMAT_G16R16_UNORM] = _mesa_texstore_unorm1616;
3819 table[MESA_FORMAT_B10G10R10A2_UNORM] = _mesa_texstore_argb2101010;
3820 table[MESA_FORMAT_BGR_SRGB8] = _mesa_texstore_srgb8;
3821 table[MESA_FORMAT_A8B8G8R8_SRGB] = _mesa_texstore_srgba8;
3822 table[MESA_FORMAT_B8G8R8A8_SRGB] = _mesa_texstore_sargb8;
3823 table[MESA_FORMAT_L_SRGB8] = _mesa_texstore_sl8;
3824 table[MESA_FORMAT_L8A8_SRGB] = _mesa_texstore_sla8;
3825 table[MESA_FORMAT_RGBA_FLOAT32] = _mesa_texstore_rgba_float32;
3826 table[MESA_FORMAT_RGBA_FLOAT16] = _mesa_texstore_rgba_float16;
3827 table[MESA_FORMAT_RGB_FLOAT32] = _mesa_texstore_rgba_float32;
3828 table[MESA_FORMAT_RGB_FLOAT16] = _mesa_texstore_rgba_float16;
3829 table[MESA_FORMAT_A_FLOAT32] = _mesa_texstore_rgba_float32;
3830 table[MESA_FORMAT_A_FLOAT16] = _mesa_texstore_rgba_float16;
3831 table[MESA_FORMAT_L_FLOAT32] = _mesa_texstore_rgba_float32;
3832 table[MESA_FORMAT_L_FLOAT16] = _mesa_texstore_rgba_float16;
3833 table[MESA_FORMAT_LA_FLOAT32] = _mesa_texstore_rgba_float32;
3834 table[MESA_FORMAT_LA_FLOAT16] = _mesa_texstore_rgba_float16;
3835 table[MESA_FORMAT_I_FLOAT32] = _mesa_texstore_rgba_float32;
3836 table[MESA_FORMAT_I_FLOAT16] = _mesa_texstore_rgba_float16;
3837 table[MESA_FORMAT_R_FLOAT32] = _mesa_texstore_rgba_float32;
3838 table[MESA_FORMAT_R_FLOAT16] = _mesa_texstore_rgba_float16;
3839 table[MESA_FORMAT_RG_FLOAT32] = _mesa_texstore_rgba_float32;
3840 table[MESA_FORMAT_RG_FLOAT16] = _mesa_texstore_rgba_float16;
3841 table[MESA_FORMAT_R_SNORM8] = _mesa_texstore_snorm8;
3842 table[MESA_FORMAT_R8G8_SNORM] = _mesa_texstore_snorm88;
3843 table[MESA_FORMAT_X8B8G8R8_SNORM] = _mesa_texstore_signed_rgbx8888;
3844 table[MESA_FORMAT_A8B8G8R8_SNORM] = _mesa_texstore_signed_rgba8888;
3845 table[MESA_FORMAT_R8G8B8A8_SNORM] = _mesa_texstore_signed_rgba8888;
3846 table[MESA_FORMAT_R_SNORM16] = _mesa_texstore_snorm16;
3847 table[MESA_FORMAT_R16G16_SNORM] = _mesa_texstore_snorm1616;
3848 table[MESA_FORMAT_RGB_SNORM16] = _mesa_texstore_signed_rgba_16;
3849 table[MESA_FORMAT_RGBA_SNORM16] = _mesa_texstore_signed_rgba_16;
3850 table[MESA_FORMAT_RGBA_UNORM16] = _mesa_texstore_rgba_16;
3851 table[MESA_FORMAT_A_SNORM8] = _mesa_texstore_snorm8;
3852 table[MESA_FORMAT_L_SNORM8] = _mesa_texstore_snorm8;
3853 table[MESA_FORMAT_L8A8_SNORM] = _mesa_texstore_snorm88;
3854 table[MESA_FORMAT_I_SNORM8] = _mesa_texstore_snorm8;
3855 table[MESA_FORMAT_A_SNORM16] = _mesa_texstore_snorm16;
3856 table[MESA_FORMAT_L_SNORM16] = _mesa_texstore_snorm16;
3857 table[MESA_FORMAT_LA_SNORM16] = _mesa_texstore_snorm1616;
3858 table[MESA_FORMAT_I_SNORM16] = _mesa_texstore_snorm16;
3859 table[MESA_FORMAT_R9G9B9E5_FLOAT] = _mesa_texstore_rgb9_e5;
3860 table[MESA_FORMAT_R11G11B10_FLOAT] = _mesa_texstore_r11_g11_b10f;
3861 table[MESA_FORMAT_Z_FLOAT32] = _mesa_texstore_z32;
3862 table[MESA_FORMAT_Z32_FLOAT_S8X24_UINT] = _mesa_texstore_z32f_x24s8;
3863
3864 table[MESA_FORMAT_A_UINT8] = _mesa_texstore_rgba_uint8;
3865 table[MESA_FORMAT_A_UINT16] = _mesa_texstore_rgba_uint16;
3866 table[MESA_FORMAT_A_UINT32] = _mesa_texstore_rgba_uint32;
3867 table[MESA_FORMAT_A_SINT8] = _mesa_texstore_rgba_int8;
3868 table[MESA_FORMAT_A_SINT16] = _mesa_texstore_rgba_int16;
3869 table[MESA_FORMAT_A_SINT32] = _mesa_texstore_rgba_int32;
3870
3871 table[MESA_FORMAT_I_UINT8] = _mesa_texstore_rgba_uint8;
3872 table[MESA_FORMAT_I_UINT16] = _mesa_texstore_rgba_uint16;
3873 table[MESA_FORMAT_I_UINT32] = _mesa_texstore_rgba_uint32;
3874 table[MESA_FORMAT_I_SINT8] = _mesa_texstore_rgba_int8;
3875 table[MESA_FORMAT_I_SINT16] = _mesa_texstore_rgba_int16;
3876 table[MESA_FORMAT_I_SINT32] = _mesa_texstore_rgba_int32;
3877
3878 table[MESA_FORMAT_L_UINT8] = _mesa_texstore_rgba_uint8;
3879 table[MESA_FORMAT_L_UINT16] = _mesa_texstore_rgba_uint16;
3880 table[MESA_FORMAT_L_UINT32] = _mesa_texstore_rgba_uint32;
3881 table[MESA_FORMAT_L_SINT8] = _mesa_texstore_rgba_int8;
3882 table[MESA_FORMAT_L_SINT16] = _mesa_texstore_rgba_int16;
3883 table[MESA_FORMAT_L_SINT32] = _mesa_texstore_rgba_int32;
3884
3885 table[MESA_FORMAT_LA_UINT8] = _mesa_texstore_rgba_uint8;
3886 table[MESA_FORMAT_LA_UINT16] = _mesa_texstore_rgba_uint16;
3887 table[MESA_FORMAT_LA_UINT32] = _mesa_texstore_rgba_uint32;
3888 table[MESA_FORMAT_LA_SINT8] = _mesa_texstore_rgba_int8;
3889 table[MESA_FORMAT_LA_SINT16] = _mesa_texstore_rgba_int16;
3890 table[MESA_FORMAT_LA_SINT32] = _mesa_texstore_rgba_int32;
3891
3892 table[MESA_FORMAT_R_SINT8] = _mesa_texstore_rgba_int8;
3893 table[MESA_FORMAT_RG_SINT8] = _mesa_texstore_rgba_int8;
3894 table[MESA_FORMAT_RGB_SINT8] = _mesa_texstore_rgba_int8;
3895 table[MESA_FORMAT_RGBA_SINT8] = _mesa_texstore_rgba_int8;
3896 table[MESA_FORMAT_R_SINT16] = _mesa_texstore_rgba_int16;
3897 table[MESA_FORMAT_RG_SINT16] = _mesa_texstore_rgba_int16;
3898 table[MESA_FORMAT_RGB_SINT16] = _mesa_texstore_rgba_int16;
3899 table[MESA_FORMAT_RGBA_SINT16] = _mesa_texstore_rgba_int16;
3900 table[MESA_FORMAT_R_SINT32] = _mesa_texstore_rgba_int32;
3901 table[MESA_FORMAT_RG_SINT32] = _mesa_texstore_rgba_int32;
3902 table[MESA_FORMAT_RGB_SINT32] = _mesa_texstore_rgba_int32;
3903 table[MESA_FORMAT_RGBA_SINT32] = _mesa_texstore_rgba_int32;
3904
3905 table[MESA_FORMAT_R_UINT8] = _mesa_texstore_rgba_uint8;
3906 table[MESA_FORMAT_RG_UINT8] = _mesa_texstore_rgba_uint8;
3907 table[MESA_FORMAT_RGB_UINT8] = _mesa_texstore_rgba_uint8;
3908 table[MESA_FORMAT_RGBA_UINT8] = _mesa_texstore_rgba_uint8;
3909 table[MESA_FORMAT_R_UINT16] = _mesa_texstore_rgba_uint16;
3910 table[MESA_FORMAT_RG_UINT16] = _mesa_texstore_rgba_uint16;
3911 table[MESA_FORMAT_RGB_UINT16] = _mesa_texstore_rgba_uint16;
3912 table[MESA_FORMAT_RGBA_UINT16] = _mesa_texstore_rgba_uint16;
3913 table[MESA_FORMAT_R_UINT32] = _mesa_texstore_rgba_uint32;
3914 table[MESA_FORMAT_RG_UINT32] = _mesa_texstore_rgba_uint32;
3915 table[MESA_FORMAT_RGB_UINT32] = _mesa_texstore_rgba_uint32;
3916 table[MESA_FORMAT_RGBA_UINT32] = _mesa_texstore_rgba_uint32;
3917
3918 table[MESA_FORMAT_B10G10R10A2_UINT] = _mesa_texstore_argb2101010_uint;
3919 table[MESA_FORMAT_R10G10B10A2_UINT] = _mesa_texstore_abgr2101010_uint;
3920
3921 table[MESA_FORMAT_B4G4R4X4_UNORM] = store_ubyte_texture;
3922 table[MESA_FORMAT_B5G5R5X1_UNORM] = store_ubyte_texture;
3923 table[MESA_FORMAT_R8G8B8X8_SNORM] = _mesa_texstore_signed_rgbx8888;
3924 table[MESA_FORMAT_R8G8B8X8_SRGB] = _mesa_texstore_srgba8;
3925 table[MESA_FORMAT_R8G8B8A8_SRGB] = _mesa_texstore_srgba8;
3926 table[MESA_FORMAT_RGBX_UINT8] = _mesa_texstore_rgba_uint8;
3927 table[MESA_FORMAT_RGBX_SINT8] = _mesa_texstore_rgba_int8;
3928 table[MESA_FORMAT_B10G10R10X2_UNORM] = _mesa_texstore_argb2101010;
3929 table[MESA_FORMAT_RGBX_UNORM16] = _mesa_texstore_rgba_16;
3930 table[MESA_FORMAT_RGBX_SNORM16] = _mesa_texstore_signed_rgba_16;
3931 table[MESA_FORMAT_RGBX_FLOAT16] = _mesa_texstore_rgba_float16;
3932 table[MESA_FORMAT_RGBX_UINT16] = _mesa_texstore_rgba_uint16;
3933 table[MESA_FORMAT_RGBX_SINT16] = _mesa_texstore_rgba_int16;
3934 table[MESA_FORMAT_RGBX_FLOAT32] = _mesa_texstore_rgba_float32;
3935 table[MESA_FORMAT_RGBX_UINT32] = _mesa_texstore_rgba_uint32;
3936 table[MESA_FORMAT_RGBX_SINT32] = _mesa_texstore_rgba_int32;
3937
3938 table[MESA_FORMAT_R10G10B10A2_UNORM] = _mesa_texstore_abgr2101010;
3939
3940 table[MESA_FORMAT_G8R8_SNORM] = _mesa_texstore_snorm88;
3941 table[MESA_FORMAT_G16R16_SNORM] = _mesa_texstore_snorm1616;
3942
3943 table[MESA_FORMAT_B8G8R8X8_SRGB] = _mesa_texstore_sargb8;
3944
3945 initialized = GL_TRUE;
3946 }
3947
3948 if (texstore_swizzle(ctx, dims, baseInternalFormat,
3949 dstFormat,
3950 dstRowStride, dstSlices,
3951 srcWidth, srcHeight, srcDepth,
3952 srcFormat, srcType, srcAddr, srcPacking)) {
3953 return GL_TRUE;
3954 }
3955
3956 ASSERT(table[dstFormat]);
3957 return table[dstFormat](ctx, dims, baseInternalFormat,
3958 dstFormat, dstRowStride, dstSlices,
3959 srcWidth, srcHeight, srcDepth,
3960 srcFormat, srcType, srcAddr, srcPacking);
3961 }
3962
3963 GLboolean
3964 _mesa_texstore_needs_transfer_ops(struct gl_context *ctx,
3965 GLenum baseInternalFormat,
3966 mesa_format dstFormat)
3967 {
3968 GLenum dstType;
3969
3970 /* There are different rules depending on the base format. */
3971 switch (baseInternalFormat) {
3972 case GL_DEPTH_COMPONENT:
3973 case GL_DEPTH_STENCIL:
3974 return ctx->Pixel.DepthScale != 1.0f ||
3975 ctx->Pixel.DepthBias != 0.0f;
3976
3977 case GL_STENCIL_INDEX:
3978 return GL_FALSE;
3979
3980 default:
3981 /* Color formats.
3982 * Pixel transfer ops (scale, bias, table lookup) do not apply
3983 * to integer formats.
3984 */
3985 dstType = _mesa_get_format_datatype(dstFormat);
3986
3987 return dstType != GL_INT && dstType != GL_UNSIGNED_INT &&
3988 ctx->_ImageTransferState;
3989 }
3990 }
3991
3992
3993 GLboolean
3994 _mesa_texstore_can_use_memcpy(struct gl_context *ctx,
3995 GLenum baseInternalFormat, mesa_format dstFormat,
3996 GLenum srcFormat, GLenum srcType,
3997 const struct gl_pixelstore_attrib *srcPacking)
3998 {
3999 if (_mesa_texstore_needs_transfer_ops(ctx, baseInternalFormat, dstFormat)) {
4000 return GL_FALSE;
4001 }
4002
4003 /* The base internal format and the base Mesa format must match. */
4004 if (baseInternalFormat != _mesa_get_format_base_format(dstFormat)) {
4005 return GL_FALSE;
4006 }
4007
4008 /* The Mesa format must match the input format and type. */
4009 if (!_mesa_format_matches_format_and_type(dstFormat, srcFormat, srcType,
4010 srcPacking->SwapBytes)) {
4011 return GL_FALSE;
4012 }
4013
4014 /* Depth texture data needs clamping in following cases:
4015 * - Floating point dstFormat with signed srcType: clamp to [0.0, 1.0].
4016 * - Fixed point dstFormat with signed srcType: clamp to [0, 2^n -1].
4017 *
4018 * All the cases except one (float dstFormat with float srcType) are ruled
4019 * out by _mesa_format_matches_format_and_type() check above. Handle the
4020 * remaining case here.
4021 */
4022 if ((baseInternalFormat == GL_DEPTH_COMPONENT ||
4023 baseInternalFormat == GL_DEPTH_STENCIL) &&
4024 (srcType == GL_FLOAT ||
4025 srcType == GL_FLOAT_32_UNSIGNED_INT_24_8_REV)) {
4026 return GL_FALSE;
4027 }
4028
4029 return GL_TRUE;
4030 }
4031
4032 static GLboolean
4033 _mesa_texstore_memcpy(TEXSTORE_PARAMS)
4034 {
4035 if (!_mesa_texstore_can_use_memcpy(ctx, baseInternalFormat, dstFormat,
4036 srcFormat, srcType, srcPacking)) {
4037 return GL_FALSE;
4038 }
4039
4040 memcpy_texture(ctx, dims,
4041 dstFormat,
4042 dstRowStride, dstSlices,
4043 srcWidth, srcHeight, srcDepth, srcFormat, srcType,
4044 srcAddr, srcPacking);
4045 return GL_TRUE;
4046 }
4047
4048 /**
4049 * Store user data into texture memory.
4050 * Called via glTex[Sub]Image1/2/3D()
4051 * \return GL_TRUE for success, GL_FALSE for failure (out of memory).
4052 */
4053 GLboolean
4054 _mesa_texstore(TEXSTORE_PARAMS)
4055 {
4056 if (_mesa_texstore_memcpy(ctx, dims, baseInternalFormat,
4057 dstFormat,
4058 dstRowStride, dstSlices,
4059 srcWidth, srcHeight, srcDepth,
4060 srcFormat, srcType, srcAddr, srcPacking)) {
4061 return GL_TRUE;
4062 }
4063
4064 if (_mesa_is_depth_or_stencil_format(baseInternalFormat)) {
4065 return texstore_depth_stencil(ctx, dims, baseInternalFormat,
4066 dstFormat, dstRowStride, dstSlices,
4067 srcWidth, srcHeight, srcDepth,
4068 srcFormat, srcType, srcAddr, srcPacking);
4069 } else if (_mesa_is_format_compressed(dstFormat)) {
4070 return texstore_compressed(ctx, dims, baseInternalFormat,
4071 dstFormat, dstRowStride, dstSlices,
4072 srcWidth, srcHeight, srcDepth,
4073 srcFormat, srcType, srcAddr, srcPacking);
4074 } else {
4075 return texstore_rgba(ctx, dims, baseInternalFormat,
4076 dstFormat, dstRowStride, dstSlices,
4077 srcWidth, srcHeight, srcDepth,
4078 srcFormat, srcType, srcAddr, srcPacking);
4079 }
4080 }
4081
4082
4083 /**
4084 * Normally, we'll only _write_ texel data to a texture when we map it.
4085 * But if the user is providing depth or stencil values and the texture
4086 * image is a combined depth/stencil format, we'll actually read from
4087 * the texture buffer too (in order to insert the depth or stencil values.
4088 * \param userFormat the user-provided image format
4089 * \param texFormat the destination texture format
4090 */
4091 static GLbitfield
4092 get_read_write_mode(GLenum userFormat, mesa_format texFormat)
4093 {
4094 if ((userFormat == GL_STENCIL_INDEX || userFormat == GL_DEPTH_COMPONENT)
4095 && _mesa_get_format_base_format(texFormat) == GL_DEPTH_STENCIL)
4096 return GL_MAP_READ_BIT | GL_MAP_WRITE_BIT;
4097 else
4098 return GL_MAP_WRITE_BIT | GL_MAP_INVALIDATE_RANGE_BIT;
4099 }
4100
4101
4102 /**
4103 * Helper function for storing 1D, 2D, 3D whole and subimages into texture
4104 * memory.
4105 * The source of the image data may be user memory or a PBO. In the later
4106 * case, we'll map the PBO, copy from it, then unmap it.
4107 */
4108 static void
4109 store_texsubimage(struct gl_context *ctx,
4110 struct gl_texture_image *texImage,
4111 GLint xoffset, GLint yoffset, GLint zoffset,
4112 GLint width, GLint height, GLint depth,
4113 GLenum format, GLenum type, const GLvoid *pixels,
4114 const struct gl_pixelstore_attrib *packing,
4115 const char *caller)
4116
4117 {
4118 const GLbitfield mapMode = get_read_write_mode(format, texImage->TexFormat);
4119 const GLenum target = texImage->TexObject->Target;
4120 GLboolean success = GL_FALSE;
4121 GLuint dims, slice, numSlices = 1, sliceOffset = 0;
4122 GLint srcImageStride = 0;
4123 const GLubyte *src;
4124
4125 assert(xoffset + width <= texImage->Width);
4126 assert(yoffset + height <= texImage->Height);
4127 assert(zoffset + depth <= texImage->Depth);
4128
4129 switch (target) {
4130 case GL_TEXTURE_1D:
4131 dims = 1;
4132 break;
4133 case GL_TEXTURE_2D_ARRAY:
4134 case GL_TEXTURE_CUBE_MAP_ARRAY:
4135 case GL_TEXTURE_3D:
4136 dims = 3;
4137 break;
4138 default:
4139 dims = 2;
4140 }
4141
4142 /* get pointer to src pixels (may be in a pbo which we'll map here) */
4143 src = (const GLubyte *)
4144 _mesa_validate_pbo_teximage(ctx, dims, width, height, depth,
4145 format, type, pixels, packing, caller);
4146 if (!src)
4147 return;
4148
4149 /* compute slice info (and do some sanity checks) */
4150 switch (target) {
4151 case GL_TEXTURE_2D:
4152 case GL_TEXTURE_RECTANGLE:
4153 case GL_TEXTURE_CUBE_MAP:
4154 case GL_TEXTURE_EXTERNAL_OES:
4155 /* one image slice, nothing special needs to be done */
4156 break;
4157 case GL_TEXTURE_1D:
4158 assert(height == 1);
4159 assert(depth == 1);
4160 assert(yoffset == 0);
4161 assert(zoffset == 0);
4162 break;
4163 case GL_TEXTURE_1D_ARRAY:
4164 assert(depth == 1);
4165 assert(zoffset == 0);
4166 numSlices = height;
4167 sliceOffset = yoffset;
4168 height = 1;
4169 yoffset = 0;
4170 srcImageStride = _mesa_image_row_stride(packing, width, format, type);
4171 break;
4172 case GL_TEXTURE_2D_ARRAY:
4173 numSlices = depth;
4174 sliceOffset = zoffset;
4175 depth = 1;
4176 zoffset = 0;
4177 srcImageStride = _mesa_image_image_stride(packing, width, height,
4178 format, type);
4179 break;
4180 case GL_TEXTURE_3D:
4181 /* we'll store 3D images as a series of slices */
4182 numSlices = depth;
4183 sliceOffset = zoffset;
4184 srcImageStride = _mesa_image_image_stride(packing, width, height,
4185 format, type);
4186 break;
4187 case GL_TEXTURE_CUBE_MAP_ARRAY:
4188 numSlices = depth;
4189 sliceOffset = zoffset;
4190 srcImageStride = _mesa_image_image_stride(packing, width, height,
4191 format, type);
4192 break;
4193 default:
4194 _mesa_warning(ctx, "Unexpected target 0x%x in store_texsubimage()", target);
4195 return;
4196 }
4197
4198 assert(numSlices == 1 || srcImageStride != 0);
4199
4200 for (slice = 0; slice < numSlices; slice++) {
4201 GLubyte *dstMap;
4202 GLint dstRowStride;
4203
4204 ctx->Driver.MapTextureImage(ctx, texImage,
4205 slice + sliceOffset,
4206 xoffset, yoffset, width, height,
4207 mapMode, &dstMap, &dstRowStride);
4208 if (dstMap) {
4209 /* Note: we're only storing a 2D (or 1D) slice at a time but we need
4210 * to pass the right 'dims' value so that GL_UNPACK_SKIP_IMAGES is
4211 * used for 3D images.
4212 */
4213 success = _mesa_texstore(ctx, dims, texImage->_BaseFormat,
4214 texImage->TexFormat,
4215 dstRowStride,
4216 &dstMap,
4217 width, height, 1, /* w, h, d */
4218 format, type, src, packing);
4219
4220 ctx->Driver.UnmapTextureImage(ctx, texImage, slice + sliceOffset);
4221 }
4222
4223 src += srcImageStride;
4224
4225 if (!success)
4226 break;
4227 }
4228
4229 if (!success)
4230 _mesa_error(ctx, GL_OUT_OF_MEMORY, "%s", caller);
4231
4232 _mesa_unmap_teximage_pbo(ctx, packing);
4233 }
4234
4235
4236
4237 /**
4238 * Fallback code for ctx->Driver.TexImage().
4239 * Basically, allocate storage for the texture image, then copy the
4240 * user's image into it.
4241 */
4242 void
4243 _mesa_store_teximage(struct gl_context *ctx,
4244 GLuint dims,
4245 struct gl_texture_image *texImage,
4246 GLenum format, GLenum type, const GLvoid *pixels,
4247 const struct gl_pixelstore_attrib *packing)
4248 {
4249 assert(dims == 1 || dims == 2 || dims == 3);
4250
4251 if (texImage->Width == 0 || texImage->Height == 0 || texImage->Depth == 0)
4252 return;
4253
4254 /* allocate storage for texture data */
4255 if (!ctx->Driver.AllocTextureImageBuffer(ctx, texImage)) {
4256 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexImage%uD", dims);
4257 return;
4258 }
4259
4260 store_texsubimage(ctx, texImage,
4261 0, 0, 0, texImage->Width, texImage->Height, texImage->Depth,
4262 format, type, pixels, packing, "glTexImage");
4263 }
4264
4265
4266 /*
4267 * Fallback for Driver.TexSubImage().
4268 */
4269 void
4270 _mesa_store_texsubimage(struct gl_context *ctx, GLuint dims,
4271 struct gl_texture_image *texImage,
4272 GLint xoffset, GLint yoffset, GLint zoffset,
4273 GLint width, GLint height, GLint depth,
4274 GLenum format, GLenum type, const void *pixels,
4275 const struct gl_pixelstore_attrib *packing)
4276 {
4277 store_texsubimage(ctx, texImage,
4278 xoffset, yoffset, zoffset, width, height, depth,
4279 format, type, pixels, packing, "glTexSubImage");
4280 }
4281
4282 static void
4283 clear_image_to_zero(GLubyte *dstMap, GLint dstRowStride,
4284 GLsizei width, GLsizei height,
4285 GLsizei clearValueSize)
4286 {
4287 GLsizei y;
4288
4289 for (y = 0; y < height; y++) {
4290 memset(dstMap, 0, clearValueSize * width);
4291 dstMap += dstRowStride;
4292 }
4293 }
4294
4295 static void
4296 clear_image_to_value(GLubyte *dstMap, GLint dstRowStride,
4297 GLsizei width, GLsizei height,
4298 const GLvoid *clearValue,
4299 GLsizei clearValueSize)
4300 {
4301 GLsizei y, x;
4302
4303 for (y = 0; y < height; y++) {
4304 for (x = 0; x < width; x++) {
4305 memcpy(dstMap, clearValue, clearValueSize);
4306 dstMap += clearValueSize;
4307 }
4308 dstMap += dstRowStride - clearValueSize * width;
4309 }
4310 }
4311
4312 /*
4313 * Fallback for Driver.ClearTexSubImage().
4314 */
4315 void
4316 _mesa_store_cleartexsubimage(struct gl_context *ctx,
4317 struct gl_texture_image *texImage,
4318 GLint xoffset, GLint yoffset, GLint zoffset,
4319 GLsizei width, GLsizei height, GLsizei depth,
4320 const GLvoid *clearValue)
4321 {
4322 GLubyte *dstMap;
4323 GLint dstRowStride;
4324 GLsizeiptr clearValueSize;
4325 GLsizei z;
4326
4327 clearValueSize = _mesa_get_format_bytes(texImage->TexFormat);
4328
4329 for (z = 0; z < depth; z++) {
4330 ctx->Driver.MapTextureImage(ctx, texImage,
4331 z + zoffset, xoffset, yoffset,
4332 width, height,
4333 GL_MAP_WRITE_BIT,
4334 &dstMap, &dstRowStride);
4335 if (dstMap == NULL) {
4336 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glClearTex*Image");
4337 return;
4338 }
4339
4340 if (clearValue) {
4341 clear_image_to_value(dstMap, dstRowStride,
4342 width, height,
4343 clearValue,
4344 clearValueSize);
4345 } else {
4346 clear_image_to_zero(dstMap, dstRowStride,
4347 width, height,
4348 clearValueSize);
4349 }
4350
4351 ctx->Driver.UnmapTextureImage(ctx, texImage, z + zoffset);
4352 }
4353 }
4354
4355 /**
4356 * Fallback for Driver.CompressedTexImage()
4357 */
4358 void
4359 _mesa_store_compressed_teximage(struct gl_context *ctx, GLuint dims,
4360 struct gl_texture_image *texImage,
4361 GLsizei imageSize, const GLvoid *data)
4362 {
4363 /* only 2D and 3D compressed images are supported at this time */
4364 if (dims == 1) {
4365 _mesa_problem(ctx, "Unexpected glCompressedTexImage1D call");
4366 return;
4367 }
4368
4369 /* This is pretty simple, because unlike the general texstore path we don't
4370 * have to worry about the usual image unpacking or image transfer
4371 * operations.
4372 */
4373 ASSERT(texImage);
4374 ASSERT(texImage->Width > 0);
4375 ASSERT(texImage->Height > 0);
4376 ASSERT(texImage->Depth > 0);
4377
4378 /* allocate storage for texture data */
4379 if (!ctx->Driver.AllocTextureImageBuffer(ctx, texImage)) {
4380 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCompressedTexImage%uD", dims);
4381 return;
4382 }
4383
4384 _mesa_store_compressed_texsubimage(ctx, dims, texImage,
4385 0, 0, 0,
4386 texImage->Width, texImage->Height, texImage->Depth,
4387 texImage->TexFormat,
4388 imageSize, data);
4389 }
4390
4391
4392 void
4393 _mesa_compute_compressed_pixelstore(GLuint dims, struct gl_texture_image *texImage,
4394 GLsizei width, GLsizei height, GLsizei depth,
4395 const struct gl_pixelstore_attrib *packing,
4396 struct compressed_pixelstore *store)
4397 {
4398 GLuint bw, bh;
4399 const mesa_format texFormat = texImage->TexFormat;
4400
4401 _mesa_get_format_block_size(texFormat, &bw, &bh);
4402
4403 store->SkipBytes = 0;
4404 store->TotalBytesPerRow = store->CopyBytesPerRow =
4405 _mesa_format_row_stride(texFormat, width);
4406 store->TotalRowsPerSlice = store->CopyRowsPerSlice =
4407 (height + bh - 1) / bh;
4408 store->CopySlices = depth;
4409
4410 if (packing->CompressedBlockWidth &&
4411 packing->CompressedBlockSize) {
4412
4413 bw = packing->CompressedBlockWidth;
4414
4415 if (packing->RowLength) {
4416 store->TotalBytesPerRow = packing->CompressedBlockSize *
4417 (packing->RowLength + bw - 1) / bw;
4418 }
4419
4420 store->SkipBytes += packing->SkipPixels * packing->CompressedBlockSize / bw;
4421 }
4422
4423 if (dims > 1 && packing->CompressedBlockHeight &&
4424 packing->CompressedBlockSize) {
4425
4426 bh = packing->CompressedBlockHeight;
4427
4428 store->SkipBytes += packing->SkipRows * store->TotalBytesPerRow / bh;
4429 store->CopyRowsPerSlice = (height + bh - 1) / bh; /* rows in blocks */
4430
4431 if (packing->ImageHeight) {
4432 store->TotalRowsPerSlice = (packing->ImageHeight + bh - 1) / bh;
4433 }
4434 }
4435
4436 if (dims > 2 && packing->CompressedBlockDepth &&
4437 packing->CompressedBlockSize) {
4438
4439 int bd = packing->CompressedBlockDepth;
4440
4441 store->SkipBytes += packing->SkipImages * store->TotalBytesPerRow *
4442 store->TotalRowsPerSlice / bd;
4443 }
4444 }
4445
4446
4447 /**
4448 * Fallback for Driver.CompressedTexSubImage()
4449 */
4450 void
4451 _mesa_store_compressed_texsubimage(struct gl_context *ctx, GLuint dims,
4452 struct gl_texture_image *texImage,
4453 GLint xoffset, GLint yoffset, GLint zoffset,
4454 GLsizei width, GLsizei height, GLsizei depth,
4455 GLenum format,
4456 GLsizei imageSize, const GLvoid *data)
4457 {
4458 struct compressed_pixelstore store;
4459 GLint dstRowStride;
4460 GLint i, slice;
4461 GLubyte *dstMap;
4462 const GLubyte *src;
4463
4464 if (dims == 1) {
4465 _mesa_problem(ctx, "Unexpected 1D compressed texsubimage call");
4466 return;
4467 }
4468
4469 _mesa_compute_compressed_pixelstore(dims, texImage, width, height, depth,
4470 &ctx->Unpack, &store);
4471
4472 /* get pointer to src pixels (may be in a pbo which we'll map here) */
4473 data = _mesa_validate_pbo_compressed_teximage(ctx, dims, imageSize, data,
4474 &ctx->Unpack,
4475 "glCompressedTexSubImage");
4476 if (!data)
4477 return;
4478
4479 src = (const GLubyte *) data + store.SkipBytes;
4480
4481 for (slice = 0; slice < store.CopySlices; slice++) {
4482 /* Map dest texture buffer */
4483 ctx->Driver.MapTextureImage(ctx, texImage, slice + zoffset,
4484 xoffset, yoffset, width, height,
4485 GL_MAP_WRITE_BIT | GL_MAP_INVALIDATE_RANGE_BIT,
4486 &dstMap, &dstRowStride);
4487
4488 if (dstMap) {
4489
4490 /* copy rows of blocks */
4491 for (i = 0; i < store.CopyRowsPerSlice; i++) {
4492 memcpy(dstMap, src, store.CopyBytesPerRow);
4493 dstMap += dstRowStride;
4494 src += store.TotalBytesPerRow;
4495 }
4496
4497 ctx->Driver.UnmapTextureImage(ctx, texImage, slice + zoffset);
4498
4499 /* advance to next slice */
4500 src += store.TotalBytesPerRow * (store.TotalRowsPerSlice - store.CopyRowsPerSlice);
4501 }
4502 else {
4503 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCompressedTexSubImage%uD",
4504 dims);
4505 }
4506 }
4507
4508 _mesa_unmap_teximage_pbo(ctx, &ctx->Unpack);
4509 }