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