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