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