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