mesa: choose texture format in core mesa, not drivers
[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, convolution!, 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 "context.h"
59 #include "convolve.h"
60 #include "image.h"
61 #include "macros.h"
62 #include "mipmap.h"
63 #include "imports.h"
64 #include "texcompress.h"
65 #include "texcompress_fxt1.h"
66 #include "texcompress_s3tc.h"
67 #include "teximage.h"
68 #include "texstore.h"
69 #include "enums.h"
70
71
72 enum {
73 ZERO = 4,
74 ONE = 5
75 };
76
77
78 /**
79 * Texture image storage function.
80 */
81 typedef GLboolean (*StoreTexImageFunc)(TEXSTORE_PARAMS);
82
83
84 /**
85 * Return GL_TRUE if the given image format is one that be converted
86 * to another format by swizzling.
87 */
88 static GLboolean
89 can_swizzle(GLenum logicalBaseFormat)
90 {
91 switch (logicalBaseFormat) {
92 case GL_RGBA:
93 case GL_RGB:
94 case GL_LUMINANCE_ALPHA:
95 case GL_INTENSITY:
96 case GL_ALPHA:
97 case GL_LUMINANCE:
98 case GL_RED:
99 case GL_GREEN:
100 case GL_BLUE:
101 case GL_BGR:
102 case GL_BGRA:
103 case GL_ABGR_EXT:
104 return GL_TRUE;
105 default:
106 return GL_FALSE;
107 }
108 }
109
110
111
112 enum {
113 IDX_LUMINANCE = 0,
114 IDX_ALPHA,
115 IDX_INTENSITY,
116 IDX_LUMINANCE_ALPHA,
117 IDX_RGB,
118 IDX_RGBA,
119 IDX_RED,
120 IDX_GREEN,
121 IDX_BLUE,
122 IDX_BGR,
123 IDX_BGRA,
124 IDX_ABGR,
125 MAX_IDX
126 };
127
128 #define MAP1(x) MAP4(x, ZERO, ZERO, ZERO)
129 #define MAP2(x,y) MAP4(x, y, ZERO, ZERO)
130 #define MAP3(x,y,z) MAP4(x, y, z, ZERO)
131 #define MAP4(x,y,z,w) { x, y, z, w, ZERO, ONE }
132
133
134 static const struct {
135 GLubyte format_idx;
136 GLubyte to_rgba[6];
137 GLubyte from_rgba[6];
138 } mappings[MAX_IDX] =
139 {
140 {
141 IDX_LUMINANCE,
142 MAP4(0,0,0,ONE),
143 MAP1(0)
144 },
145
146 {
147 IDX_ALPHA,
148 MAP4(ZERO, ZERO, ZERO, 0),
149 MAP1(3)
150 },
151
152 {
153 IDX_INTENSITY,
154 MAP4(0, 0, 0, 0),
155 MAP1(0),
156 },
157
158 {
159 IDX_LUMINANCE_ALPHA,
160 MAP4(0,0,0,1),
161 MAP2(0,3)
162 },
163
164 {
165 IDX_RGB,
166 MAP4(0,1,2,ONE),
167 MAP3(0,1,2)
168 },
169
170 {
171 IDX_RGBA,
172 MAP4(0,1,2,3),
173 MAP4(0,1,2,3),
174 },
175
176
177 {
178 IDX_RED,
179 MAP4(0, ZERO, ZERO, ONE),
180 MAP1(0),
181 },
182
183 {
184 IDX_GREEN,
185 MAP4(ZERO, 0, ZERO, ONE),
186 MAP1(1),
187 },
188
189 {
190 IDX_BLUE,
191 MAP4(ZERO, ZERO, 0, ONE),
192 MAP1(2),
193 },
194
195 {
196 IDX_BGR,
197 MAP4(2,1,0,ONE),
198 MAP3(2,1,0)
199 },
200
201 {
202 IDX_BGRA,
203 MAP4(2,1,0,3),
204 MAP4(2,1,0,3)
205 },
206
207 {
208 IDX_ABGR,
209 MAP4(3,2,1,0),
210 MAP4(3,2,1,0)
211 },
212 };
213
214
215
216 /**
217 * Convert a GL image format enum to an IDX_* value (see above).
218 */
219 static int
220 get_map_idx(GLenum value)
221 {
222 switch (value) {
223 case GL_LUMINANCE: return IDX_LUMINANCE;
224 case GL_ALPHA: return IDX_ALPHA;
225 case GL_INTENSITY: return IDX_INTENSITY;
226 case GL_LUMINANCE_ALPHA: return IDX_LUMINANCE_ALPHA;
227 case GL_RGB: return IDX_RGB;
228 case GL_RGBA: return IDX_RGBA;
229 case GL_RED: return IDX_RED;
230 case GL_GREEN: return IDX_GREEN;
231 case GL_BLUE: return IDX_BLUE;
232 case GL_BGR: return IDX_BGR;
233 case GL_BGRA: return IDX_BGRA;
234 case GL_ABGR_EXT: return IDX_ABGR;
235 default:
236 _mesa_problem(NULL, "Unexpected inFormat");
237 return 0;
238 }
239 }
240
241
242 /**
243 * When promoting texture formats (see below) we need to compute the
244 * mapping of dest components back to source components.
245 * This function does that.
246 * \param inFormat the incoming format of the texture
247 * \param outFormat the final texture format
248 * \return map[6] a full 6-component map
249 */
250 static void
251 compute_component_mapping(GLenum inFormat, GLenum outFormat,
252 GLubyte *map)
253 {
254 const int inFmt = get_map_idx(inFormat);
255 const int outFmt = get_map_idx(outFormat);
256 const GLubyte *in2rgba = mappings[inFmt].to_rgba;
257 const GLubyte *rgba2out = mappings[outFmt].from_rgba;
258 int i;
259
260 for (i = 0; i < 4; i++)
261 map[i] = in2rgba[rgba2out[i]];
262
263 map[ZERO] = ZERO;
264 map[ONE] = ONE;
265
266 /*
267 _mesa_printf("from %x/%s to %x/%s map %d %d %d %d %d %d\n",
268 inFormat, _mesa_lookup_enum_by_nr(inFormat),
269 outFormat, _mesa_lookup_enum_by_nr(outFormat),
270 map[0],
271 map[1],
272 map[2],
273 map[3],
274 map[4],
275 map[5]);
276 */
277 }
278
279
280 #if !FEATURE_convolve
281 static void
282 _mesa_adjust_image_for_convolution(GLcontext *ctx, GLuint dims,
283 GLsizei *srcWidth, GLsizei *srcHeight)
284 {
285 /* no-op */
286 }
287 #endif
288
289
290 /**
291 * Make a temporary (color) texture image with GLfloat components.
292 * Apply all needed pixel unpacking and pixel transfer operations.
293 * Note that there are both logicalBaseFormat and textureBaseFormat parameters.
294 * Suppose the user specifies GL_LUMINANCE as the internal texture format
295 * but the graphics hardware doesn't support luminance textures. So, might
296 * use an RGB hardware format instead.
297 * If logicalBaseFormat != textureBaseFormat we have some extra work to do.
298 *
299 * \param ctx the rendering context
300 * \param dims image dimensions: 1, 2 or 3
301 * \param logicalBaseFormat basic texture derived from the user's
302 * internal texture format value
303 * \param textureBaseFormat the actual basic format of the texture
304 * \param srcWidth source image width
305 * \param srcHeight source image height
306 * \param srcDepth source image depth
307 * \param srcFormat source image format
308 * \param srcType source image type
309 * \param srcAddr source image address
310 * \param srcPacking source image pixel packing
311 * \return resulting image with format = textureBaseFormat and type = GLfloat.
312 */
313 static GLfloat *
314 make_temp_float_image(GLcontext *ctx, GLuint dims,
315 GLenum logicalBaseFormat,
316 GLenum textureBaseFormat,
317 GLint srcWidth, GLint srcHeight, GLint srcDepth,
318 GLenum srcFormat, GLenum srcType,
319 const GLvoid *srcAddr,
320 const struct gl_pixelstore_attrib *srcPacking)
321 {
322 GLuint transferOps = ctx->_ImageTransferState;
323 GLfloat *tempImage;
324
325 ASSERT(dims >= 1 && dims <= 3);
326
327 ASSERT(logicalBaseFormat == GL_RGBA ||
328 logicalBaseFormat == GL_RGB ||
329 logicalBaseFormat == GL_LUMINANCE_ALPHA ||
330 logicalBaseFormat == GL_LUMINANCE ||
331 logicalBaseFormat == GL_ALPHA ||
332 logicalBaseFormat == GL_INTENSITY ||
333 logicalBaseFormat == GL_COLOR_INDEX ||
334 logicalBaseFormat == GL_DEPTH_COMPONENT);
335
336 ASSERT(textureBaseFormat == GL_RGBA ||
337 textureBaseFormat == GL_RGB ||
338 textureBaseFormat == GL_LUMINANCE_ALPHA ||
339 textureBaseFormat == GL_LUMINANCE ||
340 textureBaseFormat == GL_ALPHA ||
341 textureBaseFormat == GL_INTENSITY ||
342 textureBaseFormat == GL_COLOR_INDEX ||
343 textureBaseFormat == GL_DEPTH_COMPONENT);
344
345 /* conventional color image */
346
347 if ((dims == 1 && ctx->Pixel.Convolution1DEnabled) ||
348 (dims >= 2 && ctx->Pixel.Convolution2DEnabled) ||
349 (dims >= 2 && ctx->Pixel.Separable2DEnabled)) {
350 /* need image convolution */
351 const GLuint preConvTransferOps
352 = (transferOps & IMAGE_PRE_CONVOLUTION_BITS) | IMAGE_CLAMP_BIT;
353 const GLuint postConvTransferOps
354 = (transferOps & IMAGE_POST_CONVOLUTION_BITS) | IMAGE_CLAMP_BIT;
355 GLint img, row;
356 GLint convWidth, convHeight;
357 GLfloat *convImage;
358
359 /* pre-convolution image buffer (3D) */
360 tempImage = (GLfloat *) _mesa_malloc(srcWidth * srcHeight * srcDepth
361 * 4 * sizeof(GLfloat));
362 if (!tempImage)
363 return NULL;
364
365 /* post-convolution image buffer (2D) */
366 convImage = (GLfloat *) _mesa_malloc(srcWidth * srcHeight
367 * 4 * sizeof(GLfloat));
368 if (!convImage) {
369 _mesa_free(tempImage);
370 return NULL;
371 }
372
373 /* loop over 3D image slices */
374 for (img = 0; img < srcDepth; img++) {
375 GLfloat *dst = tempImage + img * (srcWidth * srcHeight * 4);
376
377 /* unpack and do transfer ops up to convolution */
378 for (row = 0; row < srcHeight; row++) {
379 const GLvoid *src = _mesa_image_address(dims, srcPacking,
380 srcAddr, srcWidth, srcHeight,
381 srcFormat, srcType, img, row, 0);
382 _mesa_unpack_color_span_float(ctx, srcWidth, GL_RGBA, dst,
383 srcFormat, srcType, src,
384 srcPacking,
385 preConvTransferOps);
386 dst += srcWidth * 4;
387 }
388
389 /* size after optional convolution */
390 convWidth = srcWidth;
391 convHeight = srcHeight;
392
393 #if FEATURE_convolve
394 /* do convolution */
395 {
396 GLfloat *src = tempImage + img * (srcWidth * srcHeight * 4);
397 if (dims == 1) {
398 ASSERT(ctx->Pixel.Convolution1DEnabled);
399 _mesa_convolve_1d_image(ctx, &convWidth, src, convImage);
400 }
401 else {
402 if (ctx->Pixel.Convolution2DEnabled) {
403 _mesa_convolve_2d_image(ctx, &convWidth, &convHeight,
404 src, convImage);
405 }
406 else {
407 ASSERT(ctx->Pixel.Separable2DEnabled);
408 _mesa_convolve_sep_image(ctx, &convWidth, &convHeight,
409 src, convImage);
410 }
411 }
412 }
413 #endif
414 /* do post-convolution transfer and pack into tempImage */
415 {
416 const GLint logComponents
417 = _mesa_components_in_format(logicalBaseFormat);
418 const GLfloat *src = convImage;
419 GLfloat *dst = tempImage + img * (convWidth * convHeight * 4);
420 for (row = 0; row < convHeight; row++) {
421 _mesa_pack_rgba_span_float(ctx, convWidth,
422 (GLfloat (*)[4]) src,
423 logicalBaseFormat, GL_FLOAT,
424 dst, &ctx->DefaultPacking,
425 postConvTransferOps);
426 src += convWidth * 4;
427 dst += convWidth * logComponents;
428 }
429 }
430 } /* loop over 3D image slices */
431
432 _mesa_free(convImage);
433
434 /* might need these below */
435 srcWidth = convWidth;
436 srcHeight = convHeight;
437 }
438 else {
439 /* no convolution */
440 const GLint components = _mesa_components_in_format(logicalBaseFormat);
441 const GLint srcStride =
442 _mesa_image_row_stride(srcPacking, srcWidth, srcFormat, srcType);
443 GLfloat *dst;
444 GLint img, row;
445
446 tempImage = (GLfloat *) _mesa_malloc(srcWidth * srcHeight * srcDepth
447 * components * sizeof(GLfloat));
448 if (!tempImage)
449 return NULL;
450
451 dst = tempImage;
452 for (img = 0; img < srcDepth; img++) {
453 const GLubyte *src
454 = (const GLubyte *) _mesa_image_address(dims, srcPacking, srcAddr,
455 srcWidth, srcHeight,
456 srcFormat, srcType,
457 img, 0, 0);
458 for (row = 0; row < srcHeight; row++) {
459 _mesa_unpack_color_span_float(ctx, srcWidth, logicalBaseFormat,
460 dst, srcFormat, srcType, src,
461 srcPacking, transferOps);
462 dst += srcWidth * components;
463 src += srcStride;
464 }
465 }
466 }
467
468 if (logicalBaseFormat != textureBaseFormat) {
469 /* more work */
470 GLint texComponents = _mesa_components_in_format(textureBaseFormat);
471 GLint logComponents = _mesa_components_in_format(logicalBaseFormat);
472 GLfloat *newImage;
473 GLint i, n;
474 GLubyte map[6];
475
476 /* we only promote up to RGB, RGBA and LUMINANCE_ALPHA formats for now */
477 ASSERT(textureBaseFormat == GL_RGB || textureBaseFormat == GL_RGBA ||
478 textureBaseFormat == GL_LUMINANCE_ALPHA);
479
480 /* The actual texture format should have at least as many components
481 * as the logical texture format.
482 */
483 ASSERT(texComponents >= logComponents);
484
485 newImage = (GLfloat *) _mesa_malloc(srcWidth * srcHeight * srcDepth
486 * texComponents * sizeof(GLfloat));
487 if (!newImage) {
488 _mesa_free(tempImage);
489 return NULL;
490 }
491
492 compute_component_mapping(logicalBaseFormat, textureBaseFormat, map);
493
494 n = srcWidth * srcHeight * srcDepth;
495 for (i = 0; i < n; i++) {
496 GLint k;
497 for (k = 0; k < texComponents; k++) {
498 GLint j = map[k];
499 if (j == ZERO)
500 newImage[i * texComponents + k] = 0.0F;
501 else if (j == ONE)
502 newImage[i * texComponents + k] = 1.0F;
503 else
504 newImage[i * texComponents + k] = tempImage[i * logComponents + j];
505 }
506 }
507
508 _mesa_free(tempImage);
509 tempImage = newImage;
510 }
511
512 return tempImage;
513 }
514
515
516 /**
517 * Make a temporary (color) texture image with GLchan components.
518 * Apply all needed pixel unpacking and pixel transfer operations.
519 * Note that there are both logicalBaseFormat and textureBaseFormat parameters.
520 * Suppose the user specifies GL_LUMINANCE as the internal texture format
521 * but the graphics hardware doesn't support luminance textures. So, might
522 * use an RGB hardware format instead.
523 * If logicalBaseFormat != textureBaseFormat we have some extra work to do.
524 *
525 * \param ctx the rendering context
526 * \param dims image dimensions: 1, 2 or 3
527 * \param logicalBaseFormat basic texture derived from the user's
528 * internal texture format value
529 * \param textureBaseFormat the actual basic format of the texture
530 * \param srcWidth source image width
531 * \param srcHeight source image height
532 * \param srcDepth source image depth
533 * \param srcFormat source image format
534 * \param srcType source image type
535 * \param srcAddr source image address
536 * \param srcPacking source image pixel packing
537 * \return resulting image with format = textureBaseFormat and type = GLchan.
538 */
539 GLchan *
540 _mesa_make_temp_chan_image(GLcontext *ctx, GLuint dims,
541 GLenum logicalBaseFormat,
542 GLenum textureBaseFormat,
543 GLint srcWidth, GLint srcHeight, GLint srcDepth,
544 GLenum srcFormat, GLenum srcType,
545 const GLvoid *srcAddr,
546 const struct gl_pixelstore_attrib *srcPacking)
547 {
548 GLuint transferOps = ctx->_ImageTransferState;
549 const GLint components = _mesa_components_in_format(logicalBaseFormat);
550 GLboolean freeSrcImage = GL_FALSE;
551 GLint img, row;
552 GLchan *tempImage, *dst;
553
554 ASSERT(dims >= 1 && dims <= 3);
555
556 ASSERT(logicalBaseFormat == GL_RGBA ||
557 logicalBaseFormat == GL_RGB ||
558 logicalBaseFormat == GL_LUMINANCE_ALPHA ||
559 logicalBaseFormat == GL_LUMINANCE ||
560 logicalBaseFormat == GL_ALPHA ||
561 logicalBaseFormat == GL_INTENSITY);
562
563 ASSERT(textureBaseFormat == GL_RGBA ||
564 textureBaseFormat == GL_RGB ||
565 textureBaseFormat == GL_LUMINANCE_ALPHA ||
566 textureBaseFormat == GL_LUMINANCE ||
567 textureBaseFormat == GL_ALPHA ||
568 textureBaseFormat == GL_INTENSITY);
569
570 #if FEATURE_convolve
571 if ((dims == 1 && ctx->Pixel.Convolution1DEnabled) ||
572 (dims >= 2 && ctx->Pixel.Convolution2DEnabled) ||
573 (dims >= 2 && ctx->Pixel.Separable2DEnabled)) {
574 /* get convolved image */
575 GLfloat *convImage = make_temp_float_image(ctx, dims,
576 logicalBaseFormat,
577 logicalBaseFormat,
578 srcWidth, srcHeight, srcDepth,
579 srcFormat, srcType,
580 srcAddr, srcPacking);
581 if (!convImage)
582 return NULL;
583 /* the convolved image is our new source image */
584 srcAddr = convImage;
585 srcFormat = logicalBaseFormat;
586 srcType = GL_FLOAT;
587 srcPacking = &ctx->DefaultPacking;
588 _mesa_adjust_image_for_convolution(ctx, dims, &srcWidth, &srcHeight);
589 transferOps = 0;
590 freeSrcImage = GL_TRUE;
591 }
592 #endif
593
594 /* unpack and transfer the source image */
595 tempImage = (GLchan *) _mesa_malloc(srcWidth * srcHeight * srcDepth
596 * components * sizeof(GLchan));
597 if (!tempImage)
598 return NULL;
599
600 dst = tempImage;
601 for (img = 0; img < srcDepth; img++) {
602 const GLint srcStride =
603 _mesa_image_row_stride(srcPacking, srcWidth, srcFormat, srcType);
604 const GLubyte *src =
605 (const GLubyte *) _mesa_image_address(dims, srcPacking, srcAddr,
606 srcWidth, srcHeight,
607 srcFormat, srcType,
608 img, 0, 0);
609 for (row = 0; row < srcHeight; row++) {
610 _mesa_unpack_color_span_chan(ctx, srcWidth, logicalBaseFormat, dst,
611 srcFormat, srcType, src, srcPacking,
612 transferOps);
613 dst += srcWidth * components;
614 src += srcStride;
615 }
616 }
617
618 /* If we made a temporary image for convolution, free it here */
619 if (freeSrcImage) {
620 _mesa_free((void *) srcAddr);
621 }
622
623 if (logicalBaseFormat != textureBaseFormat) {
624 /* one more conversion step */
625 GLint texComponents = _mesa_components_in_format(textureBaseFormat);
626 GLint logComponents = _mesa_components_in_format(logicalBaseFormat);
627 GLchan *newImage;
628 GLint i, n;
629 GLubyte map[6];
630
631 /* we only promote up to RGB, RGBA and LUMINANCE_ALPHA formats for now */
632 ASSERT(textureBaseFormat == GL_RGB || textureBaseFormat == GL_RGBA ||
633 textureBaseFormat == GL_LUMINANCE_ALPHA);
634
635 /* The actual texture format should have at least as many components
636 * as the logical texture format.
637 */
638 ASSERT(texComponents >= logComponents);
639
640 newImage = (GLchan *) _mesa_malloc(srcWidth * srcHeight * srcDepth
641 * texComponents * sizeof(GLchan));
642 if (!newImage) {
643 _mesa_free(tempImage);
644 return NULL;
645 }
646
647 compute_component_mapping(logicalBaseFormat, textureBaseFormat, map);
648
649 n = srcWidth * srcHeight * srcDepth;
650 for (i = 0; i < n; i++) {
651 GLint k;
652 for (k = 0; k < texComponents; k++) {
653 GLint j = map[k];
654 if (j == ZERO)
655 newImage[i * texComponents + k] = 0;
656 else if (j == ONE)
657 newImage[i * texComponents + k] = CHAN_MAX;
658 else
659 newImage[i * texComponents + k] = tempImage[i * logComponents + j];
660 }
661 }
662
663 _mesa_free(tempImage);
664 tempImage = newImage;
665 }
666
667 return tempImage;
668 }
669
670
671 /**
672 * Copy GLubyte pixels from <src> to <dst> with swizzling.
673 * \param dst destination pixels
674 * \param dstComponents number of color components in destination pixels
675 * \param src source pixels
676 * \param srcComponents number of color components in source pixels
677 * \param map the swizzle mapping. map[X] says where to find the X component
678 * in the source image's pixels. For example, if the source image
679 * is GL_BGRA and X = red, map[0] yields 2.
680 * \param count number of pixels to copy/swizzle.
681 */
682 static void
683 swizzle_copy(GLubyte *dst, GLuint dstComponents, const GLubyte *src,
684 GLuint srcComponents, const GLubyte *map, GLuint count)
685 {
686 #define SWZ_CPY(dst, src, count, dstComps, srcComps) \
687 do { \
688 GLuint i; \
689 for (i = 0; i < count; i++) { \
690 GLuint j; \
691 if (srcComps == 4) { \
692 COPY_4UBV(tmp, src); \
693 } \
694 else { \
695 for (j = 0; j < srcComps; j++) { \
696 tmp[j] = src[j]; \
697 } \
698 } \
699 src += srcComps; \
700 for (j = 0; j < dstComps; j++) { \
701 dst[j] = tmp[map[j]]; \
702 } \
703 dst += dstComps; \
704 } \
705 } while (0)
706
707 GLubyte tmp[6];
708
709 tmp[ZERO] = 0x0;
710 tmp[ONE] = 0xff;
711
712 ASSERT(srcComponents <= 4);
713 ASSERT(dstComponents <= 4);
714
715 switch (dstComponents) {
716 case 4:
717 switch (srcComponents) {
718 case 4:
719 SWZ_CPY(dst, src, count, 4, 4);
720 break;
721 case 3:
722 SWZ_CPY(dst, src, count, 4, 3);
723 break;
724 case 2:
725 SWZ_CPY(dst, src, count, 4, 2);
726 break;
727 case 1:
728 SWZ_CPY(dst, src, count, 4, 1);
729 break;
730 default:
731 ;
732 }
733 break;
734 case 3:
735 switch (srcComponents) {
736 case 4:
737 SWZ_CPY(dst, src, count, 3, 4);
738 break;
739 case 3:
740 SWZ_CPY(dst, src, count, 3, 3);
741 break;
742 case 2:
743 SWZ_CPY(dst, src, count, 3, 2);
744 break;
745 case 1:
746 SWZ_CPY(dst, src, count, 3, 1);
747 break;
748 default:
749 ;
750 }
751 break;
752 case 2:
753 switch (srcComponents) {
754 case 4:
755 SWZ_CPY(dst, src, count, 2, 4);
756 break;
757 case 3:
758 SWZ_CPY(dst, src, count, 2, 3);
759 break;
760 case 2:
761 SWZ_CPY(dst, src, count, 2, 2);
762 break;
763 case 1:
764 SWZ_CPY(dst, src, count, 2, 1);
765 break;
766 default:
767 ;
768 }
769 break;
770 case 1:
771 switch (srcComponents) {
772 case 4:
773 SWZ_CPY(dst, src, count, 1, 4);
774 break;
775 case 3:
776 SWZ_CPY(dst, src, count, 1, 3);
777 break;
778 case 2:
779 SWZ_CPY(dst, src, count, 1, 2);
780 break;
781 case 1:
782 SWZ_CPY(dst, src, count, 1, 1);
783 break;
784 default:
785 ;
786 }
787 break;
788 default:
789 ;
790 }
791 #undef SWZ_CPY
792 }
793
794
795
796 static const GLubyte map_identity[6] = { 0, 1, 2, 3, ZERO, ONE };
797 static const GLubyte map_3210[6] = { 3, 2, 1, 0, ZERO, ONE };
798
799 /* Deal with the _REV input types:
800 */
801 static const GLubyte *
802 type_mapping( GLenum srcType )
803 {
804 switch (srcType) {
805 case GL_BYTE:
806 case GL_UNSIGNED_BYTE:
807 return map_identity;
808 case GL_UNSIGNED_INT_8_8_8_8:
809 return _mesa_little_endian() ? map_3210 : map_identity;
810 case GL_UNSIGNED_INT_8_8_8_8_REV:
811 return _mesa_little_endian() ? map_identity : map_3210;
812 default:
813 return NULL;
814 }
815 }
816
817 /* Mapping required if input type is
818 */
819 static const GLubyte *
820 byteswap_mapping( GLboolean swapBytes,
821 GLenum srcType )
822 {
823 if (!swapBytes)
824 return map_identity;
825
826 switch (srcType) {
827 case GL_BYTE:
828 case GL_UNSIGNED_BYTE:
829 return map_identity;
830 case GL_UNSIGNED_INT_8_8_8_8:
831 case GL_UNSIGNED_INT_8_8_8_8_REV:
832 return map_3210;
833 default:
834 return NULL;
835 }
836 }
837
838
839
840 /**
841 * Transfer a GLubyte texture image with component swizzling.
842 */
843 static void
844 _mesa_swizzle_ubyte_image(GLcontext *ctx,
845 GLuint dimensions,
846 GLenum srcFormat,
847 GLenum srcType,
848
849 GLenum baseInternalFormat,
850
851 const GLubyte *rgba2dst,
852 GLuint dstComponents,
853
854 GLvoid *dstAddr,
855 GLint dstXoffset, GLint dstYoffset, GLint dstZoffset,
856 GLint dstRowStride,
857 const GLuint *dstImageOffsets,
858
859 GLint srcWidth, GLint srcHeight, GLint srcDepth,
860 const GLvoid *srcAddr,
861 const struct gl_pixelstore_attrib *srcPacking )
862 {
863 GLint srcComponents = _mesa_components_in_format(srcFormat);
864 const GLubyte *srctype2ubyte, *swap;
865 GLubyte map[4], src2base[6], base2rgba[6];
866 GLint i;
867 const GLint srcRowStride =
868 _mesa_image_row_stride(srcPacking, srcWidth,
869 srcFormat, GL_UNSIGNED_BYTE);
870 const GLint srcImageStride
871 = _mesa_image_image_stride(srcPacking, srcWidth, srcHeight, srcFormat,
872 GL_UNSIGNED_BYTE);
873 const GLubyte *srcImage
874 = (const GLubyte *) _mesa_image_address(dimensions, srcPacking, srcAddr,
875 srcWidth, srcHeight, srcFormat,
876 GL_UNSIGNED_BYTE, 0, 0, 0);
877
878 (void) ctx;
879
880 /* Translate from src->baseInternal->GL_RGBA->dst. This will
881 * correctly deal with RGBA->RGB->RGBA conversions where the final
882 * A value must be 0xff regardless of the incoming alpha values.
883 */
884 compute_component_mapping(srcFormat, baseInternalFormat, src2base);
885 compute_component_mapping(baseInternalFormat, GL_RGBA, base2rgba);
886 swap = byteswap_mapping(srcPacking->SwapBytes, srcType);
887 srctype2ubyte = type_mapping(srcType);
888
889
890 for (i = 0; i < 4; i++)
891 map[i] = srctype2ubyte[swap[src2base[base2rgba[rgba2dst[i]]]]];
892
893 /* _mesa_printf("map %d %d %d %d\n", map[0], map[1], map[2], map[3]); */
894
895 if (srcComponents == dstComponents &&
896 srcRowStride == dstRowStride &&
897 srcRowStride == srcWidth * srcComponents &&
898 dimensions < 3) {
899 /* 1 and 2D images only */
900 GLubyte *dstImage = (GLubyte *) dstAddr
901 + dstYoffset * dstRowStride
902 + dstXoffset * dstComponents;
903 swizzle_copy(dstImage, dstComponents, srcImage, srcComponents, map,
904 srcWidth * srcHeight);
905 }
906 else {
907 GLint img, row;
908 for (img = 0; img < srcDepth; img++) {
909 const GLubyte *srcRow = srcImage;
910 GLubyte *dstRow = (GLubyte *) dstAddr
911 + dstImageOffsets[dstZoffset + img] * dstComponents
912 + dstYoffset * dstRowStride
913 + dstXoffset * dstComponents;
914 for (row = 0; row < srcHeight; row++) {
915 swizzle_copy(dstRow, dstComponents, srcRow, srcComponents, map, srcWidth);
916 dstRow += dstRowStride;
917 srcRow += srcRowStride;
918 }
919 srcImage += srcImageStride;
920 }
921 }
922 }
923
924
925 /**
926 * Teximage storage routine for when a simple memcpy will do.
927 * No pixel transfer operations or special texel encodings allowed.
928 * 1D, 2D and 3D images supported.
929 */
930 static void
931 memcpy_texture(GLcontext *ctx,
932 GLuint dimensions,
933 gl_format dstFormat,
934 GLvoid *dstAddr,
935 GLint dstXoffset, GLint dstYoffset, GLint dstZoffset,
936 GLint dstRowStride,
937 const GLuint *dstImageOffsets,
938 GLint srcWidth, GLint srcHeight, GLint srcDepth,
939 GLenum srcFormat, GLenum srcType,
940 const GLvoid *srcAddr,
941 const struct gl_pixelstore_attrib *srcPacking)
942 {
943 const GLint srcRowStride = _mesa_image_row_stride(srcPacking, srcWidth,
944 srcFormat, srcType);
945 const GLint srcImageStride = _mesa_image_image_stride(srcPacking,
946 srcWidth, srcHeight, srcFormat, srcType);
947 const GLubyte *srcImage = (const GLubyte *) _mesa_image_address(dimensions,
948 srcPacking, srcAddr, srcWidth, srcHeight, srcFormat, srcType, 0, 0, 0);
949 const GLuint texelBytes = _mesa_get_format_bytes(dstFormat);
950 const GLint bytesPerRow = srcWidth * texelBytes;
951
952 #if 0
953 /* XXX update/re-enable for dstImageOffsets array */
954 const GLint bytesPerImage = srcHeight * bytesPerRow;
955 const GLint bytesPerTexture = srcDepth * bytesPerImage;
956 GLubyte *dstImage = (GLubyte *) dstAddr
957 + dstZoffset * dstImageStride
958 + dstYoffset * dstRowStride
959 + dstXoffset * texelBytes;
960
961 if (dstRowStride == srcRowStride &&
962 dstRowStride == bytesPerRow &&
963 ((dstImageStride == srcImageStride &&
964 dstImageStride == bytesPerImage) ||
965 (srcDepth == 1))) {
966 /* one big memcpy */
967 ctx->Driver.TextureMemCpy(dstImage, srcImage, bytesPerTexture);
968 }
969 else
970 {
971 GLint img, row;
972 for (img = 0; img < srcDepth; img++) {
973 const GLubyte *srcRow = srcImage;
974 GLubyte *dstRow = dstImage;
975 for (row = 0; row < srcHeight; row++) {
976 ctx->Driver.TextureMemCpy(dstRow, srcRow, bytesPerRow);
977 dstRow += dstRowStride;
978 srcRow += srcRowStride;
979 }
980 srcImage += srcImageStride;
981 dstImage += dstImageStride;
982 }
983 }
984 #endif
985
986 GLint img, row;
987 for (img = 0; img < srcDepth; img++) {
988 const GLubyte *srcRow = srcImage;
989 GLubyte *dstRow = (GLubyte *) dstAddr
990 + dstImageOffsets[dstZoffset + img] * texelBytes
991 + dstYoffset * dstRowStride
992 + dstXoffset * texelBytes;
993 for (row = 0; row < srcHeight; row++) {
994 ctx->Driver.TextureMemCpy(dstRow, srcRow, bytesPerRow);
995 dstRow += dstRowStride;
996 srcRow += srcRowStride;
997 }
998 srcImage += srcImageStride;
999 }
1000 }
1001
1002
1003
1004 /**
1005 * Store a 32-bit integer depth component texture image.
1006 */
1007 static GLboolean
1008 _mesa_texstore_z32(TEXSTORE_PARAMS)
1009 {
1010 const GLuint depthScale = 0xffffffff;
1011 const GLuint texelBytes = _mesa_get_format_bytes(dstFormat);
1012 (void) dims;
1013 ASSERT(dstFormat == MESA_FORMAT_Z32);
1014 ASSERT(texelBytes == sizeof(GLuint));
1015
1016 if (ctx->Pixel.DepthScale == 1.0f &&
1017 ctx->Pixel.DepthBias == 0.0f &&
1018 !srcPacking->SwapBytes &&
1019 baseInternalFormat == GL_DEPTH_COMPONENT &&
1020 srcFormat == GL_DEPTH_COMPONENT &&
1021 srcType == GL_UNSIGNED_INT) {
1022 /* simple memcpy path */
1023 memcpy_texture(ctx, dims,
1024 dstFormat, dstAddr, dstXoffset, dstYoffset, dstZoffset,
1025 dstRowStride,
1026 dstImageOffsets,
1027 srcWidth, srcHeight, srcDepth, srcFormat, srcType,
1028 srcAddr, srcPacking);
1029 }
1030 else {
1031 /* general path */
1032 GLint img, row;
1033 for (img = 0; img < srcDepth; img++) {
1034 GLubyte *dstRow = (GLubyte *) dstAddr
1035 + dstImageOffsets[dstZoffset + img] * texelBytes
1036 + dstYoffset * dstRowStride
1037 + dstXoffset * texelBytes;
1038 for (row = 0; row < srcHeight; row++) {
1039 const GLvoid *src = _mesa_image_address(dims, srcPacking,
1040 srcAddr, srcWidth, srcHeight, srcFormat, srcType, img, row, 0);
1041 _mesa_unpack_depth_span(ctx, srcWidth,
1042 GL_UNSIGNED_INT, (GLuint *) dstRow,
1043 depthScale, srcType, src, srcPacking);
1044 dstRow += dstRowStride;
1045 }
1046 }
1047 }
1048 return GL_TRUE;
1049 }
1050
1051
1052 /**
1053 * Store a 24-bit integer depth component texture image.
1054 */
1055 static GLboolean
1056 _mesa_texstore_x8_z24(TEXSTORE_PARAMS)
1057 {
1058 const GLuint depthScale = 0xffffff;
1059 const GLuint texelBytes = 4;
1060
1061 (void) dims;
1062 ASSERT(dstFormat == MESA_FORMAT_X8_Z24);
1063
1064 {
1065 /* general path */
1066 GLint img, row;
1067 for (img = 0; img < srcDepth; img++) {
1068 GLubyte *dstRow = (GLubyte *) dstAddr
1069 + dstImageOffsets[dstZoffset + img] * texelBytes
1070 + dstYoffset * dstRowStride
1071 + dstXoffset * texelBytes;
1072 for (row = 0; row < srcHeight; row++) {
1073 const GLvoid *src = _mesa_image_address(dims, srcPacking,
1074 srcAddr, srcWidth, srcHeight, srcFormat, srcType, img, row, 0);
1075 _mesa_unpack_depth_span(ctx, srcWidth,
1076 GL_UNSIGNED_INT, (GLuint *) dstRow,
1077 depthScale, srcType, src, srcPacking);
1078 dstRow += dstRowStride;
1079 }
1080 }
1081 }
1082 return GL_TRUE;
1083 }
1084
1085
1086 #define STRIDE_3D 0
1087
1088 /**
1089 * Store a 16-bit integer depth component texture image.
1090 */
1091 static GLboolean
1092 _mesa_texstore_z16(TEXSTORE_PARAMS)
1093 {
1094 const GLuint depthScale = 0xffff;
1095 const GLuint texelBytes = _mesa_get_format_bytes(dstFormat);
1096 (void) dims;
1097 ASSERT(dstFormat == MESA_FORMAT_Z16);
1098 ASSERT(texelBytes == sizeof(GLushort));
1099
1100 if (ctx->Pixel.DepthScale == 1.0f &&
1101 ctx->Pixel.DepthBias == 0.0f &&
1102 !srcPacking->SwapBytes &&
1103 baseInternalFormat == GL_DEPTH_COMPONENT &&
1104 srcFormat == GL_DEPTH_COMPONENT &&
1105 srcType == GL_UNSIGNED_SHORT) {
1106 /* simple memcpy path */
1107 memcpy_texture(ctx, dims,
1108 dstFormat, dstAddr, dstXoffset, dstYoffset, dstZoffset,
1109 dstRowStride,
1110 dstImageOffsets,
1111 srcWidth, srcHeight, srcDepth, srcFormat, srcType,
1112 srcAddr, srcPacking);
1113 }
1114 else {
1115 /* general path */
1116 GLint img, row;
1117 for (img = 0; img < srcDepth; img++) {
1118 GLubyte *dstRow = (GLubyte *) dstAddr
1119 + dstImageOffsets[dstZoffset + img] * texelBytes
1120 + dstYoffset * dstRowStride
1121 + dstXoffset * texelBytes;
1122 for (row = 0; row < srcHeight; row++) {
1123 const GLvoid *src = _mesa_image_address(dims, srcPacking,
1124 srcAddr, srcWidth, srcHeight, srcFormat, srcType, img, row, 0);
1125 GLushort *dst16 = (GLushort *) dstRow;
1126 _mesa_unpack_depth_span(ctx, srcWidth,
1127 GL_UNSIGNED_SHORT, dst16, depthScale,
1128 srcType, src, srcPacking);
1129 dstRow += dstRowStride;
1130 }
1131 }
1132 }
1133 return GL_TRUE;
1134 }
1135
1136
1137 /**
1138 * Store an rgb565 or rgb565_rev texture image.
1139 */
1140 static GLboolean
1141 _mesa_texstore_rgb565(TEXSTORE_PARAMS)
1142 {
1143 const GLuint texelBytes = _mesa_get_format_bytes(dstFormat);
1144 const GLenum baseFormat = _mesa_get_format_base_format(dstFormat);
1145
1146 ASSERT(dstFormat == MESA_FORMAT_RGB565 ||
1147 dstFormat == MESA_FORMAT_RGB565_REV);
1148 ASSERT(texelBytes == 2);
1149
1150 if (!ctx->_ImageTransferState &&
1151 !srcPacking->SwapBytes &&
1152 dstFormat == MESA_FORMAT_RGB565 &&
1153 baseInternalFormat == GL_RGB &&
1154 srcFormat == GL_RGB &&
1155 srcType == GL_UNSIGNED_SHORT_5_6_5) {
1156 /* simple memcpy path */
1157 memcpy_texture(ctx, dims,
1158 dstFormat, dstAddr, dstXoffset, dstYoffset, dstZoffset,
1159 dstRowStride,
1160 dstImageOffsets,
1161 srcWidth, srcHeight, srcDepth, srcFormat, srcType,
1162 srcAddr, srcPacking);
1163 }
1164 else if (!ctx->_ImageTransferState &&
1165 !srcPacking->SwapBytes &&
1166 baseInternalFormat == GL_RGB &&
1167 srcFormat == GL_RGB &&
1168 srcType == GL_UNSIGNED_BYTE &&
1169 dims == 2) {
1170 /* do optimized tex store */
1171 const GLint srcRowStride =
1172 _mesa_image_row_stride(srcPacking, srcWidth, srcFormat, srcType);
1173 const GLubyte *src = (const GLubyte *)
1174 _mesa_image_address(dims, srcPacking, srcAddr, srcWidth, srcHeight,
1175 srcFormat, srcType, 0, 0, 0);
1176 GLubyte *dst = (GLubyte *) dstAddr
1177 + dstYoffset * dstRowStride
1178 + dstXoffset * texelBytes;
1179 GLint row, col;
1180 for (row = 0; row < srcHeight; row++) {
1181 const GLubyte *srcUB = (const GLubyte *) src;
1182 GLushort *dstUS = (GLushort *) dst;
1183 /* check for byteswapped format */
1184 if (dstFormat == MESA_FORMAT_RGB565) {
1185 for (col = 0; col < srcWidth; col++) {
1186 dstUS[col] = PACK_COLOR_565( srcUB[0], srcUB[1], srcUB[2] );
1187 srcUB += 3;
1188 }
1189 }
1190 else {
1191 for (col = 0; col < srcWidth; col++) {
1192 dstUS[col] = PACK_COLOR_565_REV( srcUB[0], srcUB[1], srcUB[2] );
1193 srcUB += 3;
1194 }
1195 }
1196 dst += dstRowStride;
1197 src += srcRowStride;
1198 }
1199 }
1200 else {
1201 /* general path */
1202 const GLchan *tempImage = _mesa_make_temp_chan_image(ctx, dims,
1203 baseInternalFormat,
1204 baseFormat,
1205 srcWidth, srcHeight, srcDepth,
1206 srcFormat, srcType, srcAddr,
1207 srcPacking);
1208 const GLchan *src = tempImage;
1209 GLint img, row, col;
1210 if (!tempImage)
1211 return GL_FALSE;
1212 _mesa_adjust_image_for_convolution(ctx, dims, &srcWidth, &srcHeight);
1213 for (img = 0; img < srcDepth; img++) {
1214 GLubyte *dstRow = (GLubyte *) dstAddr
1215 + dstImageOffsets[dstZoffset + img] * texelBytes
1216 + dstYoffset * dstRowStride
1217 + dstXoffset * texelBytes;
1218 for (row = 0; row < srcHeight; row++) {
1219 GLushort *dstUS = (GLushort *) dstRow;
1220 /* check for byteswapped format */
1221 if (dstFormat == MESA_FORMAT_RGB565) {
1222 for (col = 0; col < srcWidth; col++) {
1223 dstUS[col] = PACK_COLOR_565( CHAN_TO_UBYTE(src[RCOMP]),
1224 CHAN_TO_UBYTE(src[GCOMP]),
1225 CHAN_TO_UBYTE(src[BCOMP]) );
1226 src += 3;
1227 }
1228 }
1229 else {
1230 for (col = 0; col < srcWidth; col++) {
1231 dstUS[col] = PACK_COLOR_565_REV( CHAN_TO_UBYTE(src[RCOMP]),
1232 CHAN_TO_UBYTE(src[GCOMP]),
1233 CHAN_TO_UBYTE(src[BCOMP]) );
1234 src += 3;
1235 }
1236 }
1237 dstRow += dstRowStride;
1238 }
1239 }
1240 _mesa_free((void *) tempImage);
1241 }
1242 return GL_TRUE;
1243 }
1244
1245
1246 /**
1247 * Store a texture in MESA_FORMAT_RGBA8888 or MESA_FORMAT_RGBA8888_REV.
1248 */
1249 static GLboolean
1250 _mesa_texstore_rgba8888(TEXSTORE_PARAMS)
1251 {
1252 const GLboolean littleEndian = _mesa_little_endian();
1253 const GLuint texelBytes = _mesa_get_format_bytes(dstFormat);
1254 const GLenum baseFormat = _mesa_get_format_base_format(dstFormat);
1255
1256 ASSERT(dstFormat == MESA_FORMAT_RGBA8888 ||
1257 dstFormat == MESA_FORMAT_RGBA8888_REV);
1258 ASSERT(texelBytes == 4);
1259
1260 if (!ctx->_ImageTransferState &&
1261 !srcPacking->SwapBytes &&
1262 dstFormat == MESA_FORMAT_RGBA8888 &&
1263 baseInternalFormat == GL_RGBA &&
1264 ((srcFormat == GL_RGBA && srcType == GL_UNSIGNED_INT_8_8_8_8) ||
1265 (srcFormat == GL_RGBA && srcType == GL_UNSIGNED_BYTE && !littleEndian) ||
1266 (srcFormat == GL_ABGR_EXT && srcType == GL_UNSIGNED_INT_8_8_8_8_REV) ||
1267 (srcFormat == GL_ABGR_EXT && srcType == GL_UNSIGNED_BYTE && littleEndian))) {
1268 /* simple memcpy path */
1269 memcpy_texture(ctx, dims,
1270 dstFormat, dstAddr, dstXoffset, dstYoffset, dstZoffset,
1271 dstRowStride,
1272 dstImageOffsets,
1273 srcWidth, srcHeight, srcDepth, srcFormat, srcType,
1274 srcAddr, srcPacking);
1275 }
1276 else if (!ctx->_ImageTransferState &&
1277 !srcPacking->SwapBytes &&
1278 dstFormat == MESA_FORMAT_RGBA8888_REV &&
1279 baseInternalFormat == GL_RGBA &&
1280 ((srcFormat == GL_RGBA && srcType == GL_UNSIGNED_INT_8_8_8_8_REV) ||
1281 (srcFormat == GL_RGBA && srcType == GL_UNSIGNED_BYTE && littleEndian) ||
1282 (srcFormat == GL_ABGR_EXT && srcType == GL_UNSIGNED_INT_8_8_8_8) ||
1283 (srcFormat == GL_ABGR_EXT && srcType == GL_UNSIGNED_BYTE && !littleEndian))) {
1284 /* simple memcpy path */
1285 memcpy_texture(ctx, dims,
1286 dstFormat, dstAddr, dstXoffset, dstYoffset, dstZoffset,
1287 dstRowStride,
1288 dstImageOffsets,
1289 srcWidth, srcHeight, srcDepth, srcFormat, srcType,
1290 srcAddr, srcPacking);
1291 }
1292 else if (!ctx->_ImageTransferState &&
1293 (srcType == GL_UNSIGNED_BYTE ||
1294 srcType == GL_UNSIGNED_INT_8_8_8_8 ||
1295 srcType == GL_UNSIGNED_INT_8_8_8_8_REV) &&
1296 can_swizzle(baseInternalFormat) &&
1297 can_swizzle(srcFormat)) {
1298
1299 GLubyte dstmap[4];
1300
1301 /* dstmap - how to swizzle from RGBA to dst format:
1302 */
1303 if ((littleEndian && dstFormat == MESA_FORMAT_RGBA8888) ||
1304 (!littleEndian && dstFormat == MESA_FORMAT_RGBA8888_REV)) {
1305 dstmap[3] = 0;
1306 dstmap[2] = 1;
1307 dstmap[1] = 2;
1308 dstmap[0] = 3;
1309 }
1310 else {
1311 dstmap[3] = 3;
1312 dstmap[2] = 2;
1313 dstmap[1] = 1;
1314 dstmap[0] = 0;
1315 }
1316
1317 _mesa_swizzle_ubyte_image(ctx, dims,
1318 srcFormat,
1319 srcType,
1320 baseInternalFormat,
1321 dstmap, 4,
1322 dstAddr, dstXoffset, dstYoffset, dstZoffset,
1323 dstRowStride, dstImageOffsets,
1324 srcWidth, srcHeight, srcDepth, srcAddr,
1325 srcPacking);
1326 }
1327 else {
1328 /* general path */
1329 const GLchan *tempImage = _mesa_make_temp_chan_image(ctx, dims,
1330 baseInternalFormat,
1331 baseFormat,
1332 srcWidth, srcHeight, srcDepth,
1333 srcFormat, srcType, srcAddr,
1334 srcPacking);
1335 const GLchan *src = tempImage;
1336 GLint img, row, col;
1337 if (!tempImage)
1338 return GL_FALSE;
1339 _mesa_adjust_image_for_convolution(ctx, dims, &srcWidth, &srcHeight);
1340 for (img = 0; img < srcDepth; img++) {
1341 GLubyte *dstRow = (GLubyte *) dstAddr
1342 + dstImageOffsets[dstZoffset + img] * texelBytes
1343 + dstYoffset * dstRowStride
1344 + dstXoffset * texelBytes;
1345 for (row = 0; row < srcHeight; row++) {
1346 GLuint *dstUI = (GLuint *) dstRow;
1347 if (dstFormat == MESA_FORMAT_RGBA8888) {
1348 for (col = 0; col < srcWidth; col++) {
1349 dstUI[col] = PACK_COLOR_8888( CHAN_TO_UBYTE(src[RCOMP]),
1350 CHAN_TO_UBYTE(src[GCOMP]),
1351 CHAN_TO_UBYTE(src[BCOMP]),
1352 CHAN_TO_UBYTE(src[ACOMP]) );
1353 src += 4;
1354 }
1355 }
1356 else {
1357 for (col = 0; col < srcWidth; col++) {
1358 dstUI[col] = PACK_COLOR_8888_REV( CHAN_TO_UBYTE(src[RCOMP]),
1359 CHAN_TO_UBYTE(src[GCOMP]),
1360 CHAN_TO_UBYTE(src[BCOMP]),
1361 CHAN_TO_UBYTE(src[ACOMP]) );
1362 src += 4;
1363 }
1364 }
1365 dstRow += dstRowStride;
1366 }
1367 }
1368 _mesa_free((void *) tempImage);
1369 }
1370 return GL_TRUE;
1371 }
1372
1373
1374 static GLboolean
1375 _mesa_texstore_argb8888(TEXSTORE_PARAMS)
1376 {
1377 const GLboolean littleEndian = _mesa_little_endian();
1378 const GLuint texelBytes = _mesa_get_format_bytes(dstFormat);
1379 const GLenum baseFormat = _mesa_get_format_base_format(dstFormat);
1380
1381 ASSERT(dstFormat == MESA_FORMAT_ARGB8888 ||
1382 dstFormat == MESA_FORMAT_ARGB8888_REV ||
1383 dstFormat == MESA_FORMAT_XRGB8888);
1384 ASSERT(texelBytes == 4);
1385
1386 if (!ctx->_ImageTransferState &&
1387 !srcPacking->SwapBytes &&
1388 (dstFormat == MESA_FORMAT_ARGB8888 ||
1389 dstFormat == MESA_FORMAT_XRGB8888) &&
1390 baseInternalFormat == GL_RGBA &&
1391 srcFormat == GL_BGRA &&
1392 ((srcType == GL_UNSIGNED_BYTE && littleEndian) ||
1393 srcType == GL_UNSIGNED_INT_8_8_8_8_REV)) {
1394 /* simple memcpy path (little endian) */
1395 memcpy_texture(ctx, dims,
1396 dstFormat, dstAddr, dstXoffset, dstYoffset, dstZoffset,
1397 dstRowStride,
1398 dstImageOffsets,
1399 srcWidth, srcHeight, srcDepth, srcFormat, srcType,
1400 srcAddr, srcPacking);
1401 }
1402 else if (!ctx->_ImageTransferState &&
1403 !srcPacking->SwapBytes &&
1404 dstFormat == MESA_FORMAT_ARGB8888_REV &&
1405 baseInternalFormat == GL_RGBA &&
1406 srcFormat == GL_BGRA &&
1407 ((srcType == GL_UNSIGNED_BYTE && !littleEndian) ||
1408 srcType == GL_UNSIGNED_INT_8_8_8_8)) {
1409 /* simple memcpy path (big endian) */
1410 memcpy_texture(ctx, dims,
1411 dstFormat, dstAddr, dstXoffset, dstYoffset, dstZoffset,
1412 dstRowStride,
1413 dstImageOffsets,
1414 srcWidth, srcHeight, srcDepth, srcFormat, srcType,
1415 srcAddr, srcPacking);
1416 }
1417 else if (!ctx->_ImageTransferState &&
1418 !srcPacking->SwapBytes &&
1419 (dstFormat == MESA_FORMAT_ARGB8888 ||
1420 dstFormat == MESA_FORMAT_XRGB8888) &&
1421 srcFormat == GL_RGB &&
1422 (baseInternalFormat == GL_RGBA ||
1423 baseInternalFormat == GL_RGB) &&
1424 srcType == GL_UNSIGNED_BYTE) {
1425 int img, row, col;
1426 for (img = 0; img < srcDepth; img++) {
1427 const GLint srcRowStride =
1428 _mesa_image_row_stride(srcPacking, srcWidth, srcFormat, srcType);
1429 GLubyte *srcRow = (GLubyte *) _mesa_image_address(dims, srcPacking,
1430 srcAddr, srcWidth, srcHeight, srcFormat, srcType, img, 0, 0);
1431 GLubyte *dstRow = (GLubyte *) dstAddr
1432 + dstImageOffsets[dstZoffset + img] * texelBytes
1433 + dstYoffset * dstRowStride
1434 + dstXoffset * texelBytes;
1435 for (row = 0; row < srcHeight; row++) {
1436 GLuint *d4 = (GLuint *) dstRow;
1437 for (col = 0; col < srcWidth; col++) {
1438 d4[col] = PACK_COLOR_8888(0xff,
1439 srcRow[col * 3 + RCOMP],
1440 srcRow[col * 3 + GCOMP],
1441 srcRow[col * 3 + BCOMP]);
1442 }
1443 dstRow += dstRowStride;
1444 srcRow += srcRowStride;
1445 }
1446 }
1447 }
1448 else if (!ctx->_ImageTransferState &&
1449 !srcPacking->SwapBytes &&
1450 dstFormat == MESA_FORMAT_ARGB8888 &&
1451 srcFormat == GL_RGBA &&
1452 baseInternalFormat == GL_RGBA &&
1453 srcType == GL_UNSIGNED_BYTE) {
1454 /* same as above case, but src data has alpha too */
1455 GLint img, row, col;
1456 /* For some reason, streaming copies to write-combined regions
1457 * are extremely sensitive to the characteristics of how the
1458 * source data is retrieved. By reordering the source reads to
1459 * be in-order, the speed of this operation increases by half.
1460 * Strangely the same isn't required for the RGB path, above.
1461 */
1462 for (img = 0; img < srcDepth; img++) {
1463 const GLint srcRowStride =
1464 _mesa_image_row_stride(srcPacking, srcWidth, srcFormat, srcType);
1465 GLubyte *srcRow = (GLubyte *) _mesa_image_address(dims, srcPacking,
1466 srcAddr, srcWidth, srcHeight, srcFormat, srcType, img, 0, 0);
1467 GLubyte *dstRow = (GLubyte *) dstAddr
1468 + dstImageOffsets[dstZoffset + img] * texelBytes
1469 + dstYoffset * dstRowStride
1470 + dstXoffset * texelBytes;
1471 for (row = 0; row < srcHeight; row++) {
1472 GLuint *d4 = (GLuint *) dstRow;
1473 for (col = 0; col < srcWidth; col++) {
1474 d4[col] = PACK_COLOR_8888(srcRow[col * 4 + ACOMP],
1475 srcRow[col * 4 + RCOMP],
1476 srcRow[col * 4 + GCOMP],
1477 srcRow[col * 4 + BCOMP]);
1478 }
1479 dstRow += dstRowStride;
1480 srcRow += srcRowStride;
1481 }
1482 }
1483 }
1484 else if (!ctx->_ImageTransferState &&
1485 (srcType == GL_UNSIGNED_BYTE ||
1486 srcType == GL_UNSIGNED_INT_8_8_8_8 ||
1487 srcType == GL_UNSIGNED_INT_8_8_8_8_REV) &&
1488 can_swizzle(baseInternalFormat) &&
1489 can_swizzle(srcFormat)) {
1490
1491 GLubyte dstmap[4];
1492
1493 /* dstmap - how to swizzle from RGBA to dst format:
1494 */
1495 if ((littleEndian && dstFormat == MESA_FORMAT_ARGB8888) ||
1496 (littleEndian && dstFormat == MESA_FORMAT_XRGB8888) ||
1497 (!littleEndian && dstFormat == MESA_FORMAT_ARGB8888_REV)) {
1498 dstmap[3] = 3; /* alpha */
1499 dstmap[2] = 0; /* red */
1500 dstmap[1] = 1; /* green */
1501 dstmap[0] = 2; /* blue */
1502 }
1503 else {
1504 assert((littleEndian && dstFormat == MESA_FORMAT_ARGB8888_REV) ||
1505 (!littleEndian && dstFormat == MESA_FORMAT_ARGB8888) ||
1506 (!littleEndian && dstFormat == MESA_FORMAT_XRGB8888));
1507 dstmap[3] = 2;
1508 dstmap[2] = 1;
1509 dstmap[1] = 0;
1510 dstmap[0] = 3;
1511 }
1512
1513 _mesa_swizzle_ubyte_image(ctx, dims,
1514 srcFormat,
1515 srcType,
1516
1517 baseInternalFormat,
1518 dstmap, 4,
1519 dstAddr, dstXoffset, dstYoffset, dstZoffset,
1520 dstRowStride,
1521 dstImageOffsets,
1522 srcWidth, srcHeight, srcDepth, srcAddr,
1523 srcPacking);
1524 }
1525 else {
1526 /* general path */
1527 const GLchan *tempImage = _mesa_make_temp_chan_image(ctx, dims,
1528 baseInternalFormat,
1529 baseFormat,
1530 srcWidth, srcHeight, srcDepth,
1531 srcFormat, srcType, srcAddr,
1532 srcPacking);
1533 const GLchan *src = tempImage;
1534 GLint img, row, col;
1535 if (!tempImage)
1536 return GL_FALSE;
1537 _mesa_adjust_image_for_convolution(ctx, dims, &srcWidth, &srcHeight);
1538 for (img = 0; img < srcDepth; img++) {
1539 GLubyte *dstRow = (GLubyte *) dstAddr
1540 + dstImageOffsets[dstZoffset + img] * texelBytes
1541 + dstYoffset * dstRowStride
1542 + dstXoffset * texelBytes;
1543 for (row = 0; row < srcHeight; row++) {
1544 GLuint *dstUI = (GLuint *) dstRow;
1545 if (dstFormat == MESA_FORMAT_ARGB8888) {
1546 for (col = 0; col < srcWidth; col++) {
1547 dstUI[col] = PACK_COLOR_8888( CHAN_TO_UBYTE(src[ACOMP]),
1548 CHAN_TO_UBYTE(src[RCOMP]),
1549 CHAN_TO_UBYTE(src[GCOMP]),
1550 CHAN_TO_UBYTE(src[BCOMP]) );
1551 src += 4;
1552 }
1553 }
1554 else if (dstFormat == MESA_FORMAT_XRGB8888) {
1555 for (col = 0; col < srcWidth; col++) {
1556 dstUI[col] = PACK_COLOR_8888( 0xff,
1557 CHAN_TO_UBYTE(src[RCOMP]),
1558 CHAN_TO_UBYTE(src[GCOMP]),
1559 CHAN_TO_UBYTE(src[BCOMP]) );
1560 src += 4;
1561 }
1562 }
1563 else {
1564 for (col = 0; col < srcWidth; col++) {
1565 dstUI[col] = PACK_COLOR_8888_REV( CHAN_TO_UBYTE(src[ACOMP]),
1566 CHAN_TO_UBYTE(src[RCOMP]),
1567 CHAN_TO_UBYTE(src[GCOMP]),
1568 CHAN_TO_UBYTE(src[BCOMP]) );
1569 src += 4;
1570 }
1571 }
1572 dstRow += dstRowStride;
1573 }
1574 }
1575 _mesa_free((void *) tempImage);
1576 }
1577 return GL_TRUE;
1578 }
1579
1580
1581 static GLboolean
1582 _mesa_texstore_rgb888(TEXSTORE_PARAMS)
1583 {
1584 const GLboolean littleEndian = _mesa_little_endian();
1585 const GLuint texelBytes = _mesa_get_format_bytes(dstFormat);
1586 const GLenum baseFormat = _mesa_get_format_base_format(dstFormat);
1587
1588 ASSERT(dstFormat == MESA_FORMAT_RGB888);
1589 ASSERT(texelBytes == 3);
1590
1591 if (!ctx->_ImageTransferState &&
1592 !srcPacking->SwapBytes &&
1593 baseInternalFormat == GL_RGB &&
1594 srcFormat == GL_BGR &&
1595 srcType == GL_UNSIGNED_BYTE &&
1596 littleEndian) {
1597 /* simple memcpy path */
1598 memcpy_texture(ctx, dims,
1599 dstFormat, dstAddr, dstXoffset, dstYoffset, dstZoffset,
1600 dstRowStride,
1601 dstImageOffsets,
1602 srcWidth, srcHeight, srcDepth, srcFormat, srcType,
1603 srcAddr, srcPacking);
1604 }
1605 else if (!ctx->_ImageTransferState &&
1606 !srcPacking->SwapBytes &&
1607 srcFormat == GL_RGBA &&
1608 srcType == GL_UNSIGNED_BYTE) {
1609 /* extract RGB from RGBA */
1610 GLint img, row, col;
1611 for (img = 0; img < srcDepth; img++) {
1612 const GLint srcRowStride =
1613 _mesa_image_row_stride(srcPacking, srcWidth, srcFormat, srcType);
1614 GLubyte *srcRow = (GLubyte *) _mesa_image_address(dims, srcPacking,
1615 srcAddr, srcWidth, srcHeight, srcFormat, srcType, img, 0, 0);
1616 GLubyte *dstRow = (GLubyte *) dstAddr
1617 + dstImageOffsets[dstZoffset + img] * texelBytes
1618 + dstYoffset * dstRowStride
1619 + dstXoffset * texelBytes;
1620 for (row = 0; row < srcHeight; row++) {
1621 for (col = 0; col < srcWidth; col++) {
1622 dstRow[col * 3 + 0] = srcRow[col * 4 + BCOMP];
1623 dstRow[col * 3 + 1] = srcRow[col * 4 + GCOMP];
1624 dstRow[col * 3 + 2] = srcRow[col * 4 + RCOMP];
1625 }
1626 dstRow += dstRowStride;
1627 srcRow += srcRowStride;
1628 }
1629 }
1630 }
1631 else if (!ctx->_ImageTransferState &&
1632 srcType == GL_UNSIGNED_BYTE &&
1633 can_swizzle(baseInternalFormat) &&
1634 can_swizzle(srcFormat)) {
1635
1636 GLubyte dstmap[4];
1637
1638 /* dstmap - how to swizzle from RGBA to dst format:
1639 */
1640 dstmap[0] = 2;
1641 dstmap[1] = 1;
1642 dstmap[2] = 0;
1643 dstmap[3] = ONE; /* ? */
1644
1645 _mesa_swizzle_ubyte_image(ctx, dims,
1646 srcFormat,
1647 srcType,
1648 baseInternalFormat,
1649 dstmap, 3,
1650 dstAddr, dstXoffset, dstYoffset, dstZoffset,
1651 dstRowStride, dstImageOffsets,
1652 srcWidth, srcHeight, srcDepth, srcAddr,
1653 srcPacking);
1654 }
1655 else {
1656 /* general path */
1657 const GLchan *tempImage = _mesa_make_temp_chan_image(ctx, dims,
1658 baseInternalFormat,
1659 baseFormat,
1660 srcWidth, srcHeight, srcDepth,
1661 srcFormat, srcType, srcAddr,
1662 srcPacking);
1663 const GLchan *src = (const GLchan *) tempImage;
1664 GLint img, row, col;
1665 if (!tempImage)
1666 return GL_FALSE;
1667 _mesa_adjust_image_for_convolution(ctx, dims, &srcWidth, &srcHeight);
1668 for (img = 0; img < srcDepth; img++) {
1669 GLubyte *dstRow = (GLubyte *) dstAddr
1670 + dstImageOffsets[dstZoffset + img] * texelBytes
1671 + dstYoffset * dstRowStride
1672 + dstXoffset * texelBytes;
1673 for (row = 0; row < srcHeight; row++) {
1674 #if 0
1675 if (littleEndian) {
1676 for (col = 0; col < srcWidth; col++) {
1677 dstRow[col * 3 + 0] = CHAN_TO_UBYTE(src[RCOMP]);
1678 dstRow[col * 3 + 1] = CHAN_TO_UBYTE(src[GCOMP]);
1679 dstRow[col * 3 + 2] = CHAN_TO_UBYTE(src[BCOMP]);
1680 srcUB += 3;
1681 }
1682 }
1683 else {
1684 for (col = 0; col < srcWidth; col++) {
1685 dstRow[col * 3 + 0] = srcUB[BCOMP];
1686 dstRow[col * 3 + 1] = srcUB[GCOMP];
1687 dstRow[col * 3 + 2] = srcUB[RCOMP];
1688 srcUB += 3;
1689 }
1690 }
1691 #else
1692 for (col = 0; col < srcWidth; col++) {
1693 dstRow[col * 3 + 0] = CHAN_TO_UBYTE(src[BCOMP]);
1694 dstRow[col * 3 + 1] = CHAN_TO_UBYTE(src[GCOMP]);
1695 dstRow[col * 3 + 2] = CHAN_TO_UBYTE(src[RCOMP]);
1696 src += 3;
1697 }
1698 #endif
1699 dstRow += dstRowStride;
1700 }
1701 }
1702 _mesa_free((void *) tempImage);
1703 }
1704 return GL_TRUE;
1705 }
1706
1707
1708 static GLboolean
1709 _mesa_texstore_bgr888(TEXSTORE_PARAMS)
1710 {
1711 const GLboolean littleEndian = _mesa_little_endian();
1712 const GLuint texelBytes = _mesa_get_format_bytes(dstFormat);
1713 const GLenum baseFormat = _mesa_get_format_base_format(dstFormat);
1714
1715 ASSERT(dstFormat == MESA_FORMAT_BGR888);
1716 ASSERT(texelBytes == 3);
1717
1718 if (!ctx->_ImageTransferState &&
1719 !srcPacking->SwapBytes &&
1720 baseInternalFormat == GL_RGB &&
1721 srcFormat == GL_RGB &&
1722 srcType == GL_UNSIGNED_BYTE &&
1723 littleEndian) {
1724 /* simple memcpy path */
1725 memcpy_texture(ctx, dims,
1726 dstFormat, dstAddr, dstXoffset, dstYoffset, dstZoffset,
1727 dstRowStride,
1728 dstImageOffsets,
1729 srcWidth, srcHeight, srcDepth, srcFormat, srcType,
1730 srcAddr, srcPacking);
1731 }
1732 else if (!ctx->_ImageTransferState &&
1733 !srcPacking->SwapBytes &&
1734 srcFormat == GL_RGBA &&
1735 srcType == GL_UNSIGNED_BYTE) {
1736 /* extract BGR from RGBA */
1737 int img, row, col;
1738 for (img = 0; img < srcDepth; img++) {
1739 const GLint srcRowStride =
1740 _mesa_image_row_stride(srcPacking, srcWidth, srcFormat, srcType);
1741 GLubyte *srcRow = (GLubyte *) _mesa_image_address(dims, srcPacking,
1742 srcAddr, srcWidth, srcHeight, srcFormat, srcType, img, 0, 0);
1743 GLubyte *dstRow = (GLubyte *) dstAddr
1744 + dstImageOffsets[dstZoffset + img] * texelBytes
1745 + dstYoffset * dstRowStride
1746 + dstXoffset * texelBytes;
1747 for (row = 0; row < srcHeight; row++) {
1748 for (col = 0; col < srcWidth; col++) {
1749 dstRow[col * 3 + 0] = srcRow[col * 4 + RCOMP];
1750 dstRow[col * 3 + 1] = srcRow[col * 4 + GCOMP];
1751 dstRow[col * 3 + 2] = srcRow[col * 4 + BCOMP];
1752 }
1753 dstRow += dstRowStride;
1754 srcRow += srcRowStride;
1755 }
1756 }
1757 }
1758 else if (!ctx->_ImageTransferState &&
1759 srcType == GL_UNSIGNED_BYTE &&
1760 can_swizzle(baseInternalFormat) &&
1761 can_swizzle(srcFormat)) {
1762
1763 GLubyte dstmap[4];
1764
1765 /* dstmap - how to swizzle from RGBA to dst format:
1766 */
1767 dstmap[0] = 0;
1768 dstmap[1] = 1;
1769 dstmap[2] = 2;
1770 dstmap[3] = ONE; /* ? */
1771
1772 _mesa_swizzle_ubyte_image(ctx, dims,
1773 srcFormat,
1774 srcType,
1775 baseInternalFormat,
1776 dstmap, 3,
1777 dstAddr, dstXoffset, dstYoffset, dstZoffset,
1778 dstRowStride, dstImageOffsets,
1779 srcWidth, srcHeight, srcDepth, srcAddr,
1780 srcPacking);
1781 }
1782 else {
1783 /* general path */
1784 const GLchan *tempImage = _mesa_make_temp_chan_image(ctx, dims,
1785 baseInternalFormat,
1786 baseFormat,
1787 srcWidth, srcHeight, srcDepth,
1788 srcFormat, srcType, srcAddr,
1789 srcPacking);
1790 const GLchan *src = (const GLchan *) tempImage;
1791 GLint img, row, col;
1792 if (!tempImage)
1793 return GL_FALSE;
1794 _mesa_adjust_image_for_convolution(ctx, dims, &srcWidth, &srcHeight);
1795 for (img = 0; img < srcDepth; img++) {
1796 GLubyte *dstRow = (GLubyte *) dstAddr
1797 + dstImageOffsets[dstZoffset + img] * texelBytes
1798 + dstYoffset * dstRowStride
1799 + dstXoffset * texelBytes;
1800 for (row = 0; row < srcHeight; row++) {
1801 for (col = 0; col < srcWidth; col++) {
1802 dstRow[col * 3 + 0] = CHAN_TO_UBYTE(src[RCOMP]);
1803 dstRow[col * 3 + 1] = CHAN_TO_UBYTE(src[GCOMP]);
1804 dstRow[col * 3 + 2] = CHAN_TO_UBYTE(src[BCOMP]);
1805 src += 3;
1806 }
1807 dstRow += dstRowStride;
1808 }
1809 }
1810 _mesa_free((void *) tempImage);
1811 }
1812 return GL_TRUE;
1813 }
1814
1815
1816 static GLboolean
1817 _mesa_texstore_argb4444(TEXSTORE_PARAMS)
1818 {
1819 const GLuint texelBytes = _mesa_get_format_bytes(dstFormat);
1820 const GLenum baseFormat = _mesa_get_format_base_format(dstFormat);
1821
1822 ASSERT(dstFormat == MESA_FORMAT_ARGB4444 ||
1823 dstFormat == MESA_FORMAT_ARGB4444_REV);
1824 ASSERT(texelBytes == 2);
1825
1826 if (!ctx->_ImageTransferState &&
1827 !srcPacking->SwapBytes &&
1828 dstFormat == MESA_FORMAT_ARGB4444 &&
1829 baseInternalFormat == GL_RGBA &&
1830 srcFormat == GL_BGRA &&
1831 srcType == GL_UNSIGNED_SHORT_4_4_4_4_REV) {
1832 /* simple memcpy path */
1833 memcpy_texture(ctx, dims,
1834 dstFormat, dstAddr, dstXoffset, dstYoffset, dstZoffset,
1835 dstRowStride,
1836 dstImageOffsets,
1837 srcWidth, srcHeight, srcDepth, srcFormat, srcType,
1838 srcAddr, srcPacking);
1839 }
1840 else {
1841 /* general path */
1842 const GLchan *tempImage = _mesa_make_temp_chan_image(ctx, dims,
1843 baseInternalFormat,
1844 baseFormat,
1845 srcWidth, srcHeight, srcDepth,
1846 srcFormat, srcType, srcAddr,
1847 srcPacking);
1848 const GLchan *src = tempImage;
1849 GLint img, row, col;
1850 if (!tempImage)
1851 return GL_FALSE;
1852 _mesa_adjust_image_for_convolution(ctx, dims, &srcWidth, &srcHeight);
1853 for (img = 0; img < srcDepth; img++) {
1854 GLubyte *dstRow = (GLubyte *) dstAddr
1855 + dstImageOffsets[dstZoffset + img] * texelBytes
1856 + dstYoffset * dstRowStride
1857 + dstXoffset * texelBytes;
1858 for (row = 0; row < srcHeight; row++) {
1859 GLushort *dstUS = (GLushort *) dstRow;
1860 if (dstFormat == MESA_FORMAT_ARGB4444) {
1861 for (col = 0; col < srcWidth; col++) {
1862 dstUS[col] = PACK_COLOR_4444( CHAN_TO_UBYTE(src[ACOMP]),
1863 CHAN_TO_UBYTE(src[RCOMP]),
1864 CHAN_TO_UBYTE(src[GCOMP]),
1865 CHAN_TO_UBYTE(src[BCOMP]) );
1866 src += 4;
1867 }
1868 }
1869 else {
1870 for (col = 0; col < srcWidth; col++) {
1871 dstUS[col] = PACK_COLOR_4444_REV( CHAN_TO_UBYTE(src[ACOMP]),
1872 CHAN_TO_UBYTE(src[RCOMP]),
1873 CHAN_TO_UBYTE(src[GCOMP]),
1874 CHAN_TO_UBYTE(src[BCOMP]) );
1875 src += 4;
1876 }
1877 }
1878 dstRow += dstRowStride;
1879 }
1880 }
1881 _mesa_free((void *) tempImage);
1882 }
1883 return GL_TRUE;
1884 }
1885
1886 static GLboolean
1887 _mesa_texstore_rgba5551(TEXSTORE_PARAMS)
1888 {
1889 const GLuint texelBytes = _mesa_get_format_bytes(dstFormat);
1890 const GLenum baseFormat = _mesa_get_format_base_format(dstFormat);
1891
1892 ASSERT(dstFormat == MESA_FORMAT_RGBA5551);
1893 ASSERT(texelBytes == 2);
1894
1895 if (!ctx->_ImageTransferState &&
1896 !srcPacking->SwapBytes &&
1897 dstFormat == MESA_FORMAT_RGBA5551 &&
1898 baseInternalFormat == GL_RGBA &&
1899 srcFormat == GL_RGBA &&
1900 srcType == GL_UNSIGNED_SHORT_5_5_5_1) {
1901 /* simple memcpy path */
1902 memcpy_texture(ctx, dims,
1903 dstFormat, dstAddr, dstXoffset, dstYoffset, dstZoffset,
1904 dstRowStride,
1905 dstImageOffsets,
1906 srcWidth, srcHeight, srcDepth, srcFormat, srcType,
1907 srcAddr, srcPacking);
1908 }
1909 else {
1910 /* general path */
1911 const GLchan *tempImage = _mesa_make_temp_chan_image(ctx, dims,
1912 baseInternalFormat,
1913 baseFormat,
1914 srcWidth, srcHeight, srcDepth,
1915 srcFormat, srcType, srcAddr,
1916 srcPacking);
1917 const GLchan *src =tempImage;
1918 GLint img, row, col;
1919 if (!tempImage)
1920 return GL_FALSE;
1921 _mesa_adjust_image_for_convolution(ctx, dims, &srcWidth, &srcHeight);
1922 for (img = 0; img < srcDepth; img++) {
1923 GLubyte *dstRow = (GLubyte *) dstAddr
1924 + dstImageOffsets[dstZoffset + img] * texelBytes
1925 + dstYoffset * dstRowStride
1926 + dstXoffset * texelBytes;
1927 for (row = 0; row < srcHeight; row++) {
1928 GLushort *dstUS = (GLushort *) dstRow;
1929 for (col = 0; col < srcWidth; col++) {
1930 dstUS[col] = PACK_COLOR_5551( CHAN_TO_UBYTE(src[RCOMP]),
1931 CHAN_TO_UBYTE(src[GCOMP]),
1932 CHAN_TO_UBYTE(src[BCOMP]),
1933 CHAN_TO_UBYTE(src[ACOMP]) );
1934 src += 4;
1935 }
1936 dstRow += dstRowStride;
1937 }
1938 }
1939 _mesa_free((void *) tempImage);
1940 }
1941 return GL_TRUE;
1942 }
1943
1944 static GLboolean
1945 _mesa_texstore_argb1555(TEXSTORE_PARAMS)
1946 {
1947 const GLuint texelBytes = _mesa_get_format_bytes(dstFormat);
1948 const GLenum baseFormat = _mesa_get_format_base_format(dstFormat);
1949
1950 ASSERT(dstFormat == MESA_FORMAT_ARGB1555 ||
1951 dstFormat == MESA_FORMAT_ARGB1555_REV);
1952 ASSERT(texelBytes == 2);
1953
1954 if (!ctx->_ImageTransferState &&
1955 !srcPacking->SwapBytes &&
1956 dstFormat == MESA_FORMAT_ARGB1555 &&
1957 baseInternalFormat == GL_RGBA &&
1958 srcFormat == GL_BGRA &&
1959 srcType == GL_UNSIGNED_SHORT_1_5_5_5_REV) {
1960 /* simple memcpy path */
1961 memcpy_texture(ctx, dims,
1962 dstFormat, dstAddr, dstXoffset, dstYoffset, dstZoffset,
1963 dstRowStride,
1964 dstImageOffsets,
1965 srcWidth, srcHeight, srcDepth, srcFormat, srcType,
1966 srcAddr, srcPacking);
1967 }
1968 else {
1969 /* general path */
1970 const GLchan *tempImage = _mesa_make_temp_chan_image(ctx, dims,
1971 baseInternalFormat,
1972 baseFormat,
1973 srcWidth, srcHeight, srcDepth,
1974 srcFormat, srcType, srcAddr,
1975 srcPacking);
1976 const GLchan *src =tempImage;
1977 GLint img, row, col;
1978 if (!tempImage)
1979 return GL_FALSE;
1980 _mesa_adjust_image_for_convolution(ctx, dims, &srcWidth, &srcHeight);
1981 for (img = 0; img < srcDepth; img++) {
1982 GLubyte *dstRow = (GLubyte *) dstAddr
1983 + dstImageOffsets[dstZoffset + img] * texelBytes
1984 + dstYoffset * dstRowStride
1985 + dstXoffset * texelBytes;
1986 for (row = 0; row < srcHeight; row++) {
1987 GLushort *dstUS = (GLushort *) dstRow;
1988 if (dstFormat == MESA_FORMAT_ARGB1555) {
1989 for (col = 0; col < srcWidth; col++) {
1990 dstUS[col] = PACK_COLOR_1555( CHAN_TO_UBYTE(src[ACOMP]),
1991 CHAN_TO_UBYTE(src[RCOMP]),
1992 CHAN_TO_UBYTE(src[GCOMP]),
1993 CHAN_TO_UBYTE(src[BCOMP]) );
1994 src += 4;
1995 }
1996 }
1997 else {
1998 for (col = 0; col < srcWidth; col++) {
1999 dstUS[col] = PACK_COLOR_1555_REV( CHAN_TO_UBYTE(src[ACOMP]),
2000 CHAN_TO_UBYTE(src[RCOMP]),
2001 CHAN_TO_UBYTE(src[GCOMP]),
2002 CHAN_TO_UBYTE(src[BCOMP]) );
2003 src += 4;
2004 }
2005 }
2006 dstRow += dstRowStride;
2007 }
2008 }
2009 _mesa_free((void *) tempImage);
2010 }
2011 return GL_TRUE;
2012 }
2013
2014
2015 static GLboolean
2016 _mesa_texstore_al88(TEXSTORE_PARAMS)
2017 {
2018 const GLboolean littleEndian = _mesa_little_endian();
2019 const GLuint texelBytes = _mesa_get_format_bytes(dstFormat);
2020 const GLenum baseFormat = _mesa_get_format_base_format(dstFormat);
2021
2022 ASSERT(dstFormat == MESA_FORMAT_AL88 ||
2023 dstFormat == MESA_FORMAT_AL88_REV);
2024 ASSERT(texelBytes == 2);
2025
2026 if (!ctx->_ImageTransferState &&
2027 !srcPacking->SwapBytes &&
2028 dstFormat == MESA_FORMAT_AL88 &&
2029 baseInternalFormat == GL_LUMINANCE_ALPHA &&
2030 srcFormat == GL_LUMINANCE_ALPHA &&
2031 srcType == GL_UNSIGNED_BYTE &&
2032 littleEndian) {
2033 /* simple memcpy path */
2034 memcpy_texture(ctx, dims,
2035 dstFormat, dstAddr, dstXoffset, dstYoffset, dstZoffset,
2036 dstRowStride,
2037 dstImageOffsets,
2038 srcWidth, srcHeight, srcDepth, srcFormat, srcType,
2039 srcAddr, srcPacking);
2040 }
2041 else if (!ctx->_ImageTransferState &&
2042 littleEndian &&
2043 srcType == GL_UNSIGNED_BYTE &&
2044 can_swizzle(baseInternalFormat) &&
2045 can_swizzle(srcFormat)) {
2046
2047 GLubyte dstmap[4];
2048
2049 /* dstmap - how to swizzle from RGBA to dst format:
2050 */
2051 if ((littleEndian && dstFormat == MESA_FORMAT_AL88) ||
2052 (!littleEndian && dstFormat == MESA_FORMAT_AL88_REV)) {
2053 dstmap[0] = 0;
2054 dstmap[1] = 3;
2055 }
2056 else {
2057 dstmap[0] = 3;
2058 dstmap[1] = 0;
2059 }
2060 dstmap[2] = ZERO; /* ? */
2061 dstmap[3] = ONE; /* ? */
2062
2063 _mesa_swizzle_ubyte_image(ctx, dims,
2064 srcFormat,
2065 srcType,
2066 baseInternalFormat,
2067 dstmap, 2,
2068 dstAddr, dstXoffset, dstYoffset, dstZoffset,
2069 dstRowStride, dstImageOffsets,
2070 srcWidth, srcHeight, srcDepth, srcAddr,
2071 srcPacking);
2072 }
2073 else {
2074 /* general path */
2075 const GLchan *tempImage = _mesa_make_temp_chan_image(ctx, dims,
2076 baseInternalFormat,
2077 baseFormat,
2078 srcWidth, srcHeight, srcDepth,
2079 srcFormat, srcType, srcAddr,
2080 srcPacking);
2081 const GLchan *src = tempImage;
2082 GLint img, row, col;
2083 if (!tempImage)
2084 return GL_FALSE;
2085 _mesa_adjust_image_for_convolution(ctx, dims, &srcWidth, &srcHeight);
2086 for (img = 0; img < srcDepth; img++) {
2087 GLubyte *dstRow = (GLubyte *) dstAddr
2088 + dstImageOffsets[dstZoffset + img] * texelBytes
2089 + dstYoffset * dstRowStride
2090 + dstXoffset * texelBytes;
2091 for (row = 0; row < srcHeight; row++) {
2092 GLushort *dstUS = (GLushort *) dstRow;
2093 if (dstFormat == MESA_FORMAT_AL88) {
2094 for (col = 0; col < srcWidth; col++) {
2095 /* src[0] is luminance, src[1] is alpha */
2096 dstUS[col] = PACK_COLOR_88( CHAN_TO_UBYTE(src[1]),
2097 CHAN_TO_UBYTE(src[0]) );
2098 src += 2;
2099 }
2100 }
2101 else {
2102 for (col = 0; col < srcWidth; col++) {
2103 /* src[0] is luminance, src[1] is alpha */
2104 dstUS[col] = PACK_COLOR_88_REV( CHAN_TO_UBYTE(src[1]),
2105 CHAN_TO_UBYTE(src[0]) );
2106 src += 2;
2107 }
2108 }
2109 dstRow += dstRowStride;
2110 }
2111 }
2112 _mesa_free((void *) tempImage);
2113 }
2114 return GL_TRUE;
2115 }
2116
2117
2118 static GLboolean
2119 _mesa_texstore_rgb332(TEXSTORE_PARAMS)
2120 {
2121 const GLuint texelBytes = _mesa_get_format_bytes(dstFormat);
2122 const GLenum baseFormat = _mesa_get_format_base_format(dstFormat);
2123
2124 ASSERT(dstFormat == MESA_FORMAT_RGB332);
2125 ASSERT(texelBytes == 1);
2126
2127 if (!ctx->_ImageTransferState &&
2128 !srcPacking->SwapBytes &&
2129 baseInternalFormat == GL_RGB &&
2130 srcFormat == GL_RGB && srcType == GL_UNSIGNED_BYTE_3_3_2) {
2131 /* simple memcpy path */
2132 memcpy_texture(ctx, dims,
2133 dstFormat, dstAddr, dstXoffset, dstYoffset, dstZoffset,
2134 dstRowStride,
2135 dstImageOffsets,
2136 srcWidth, srcHeight, srcDepth, srcFormat, srcType,
2137 srcAddr, srcPacking);
2138 }
2139 else {
2140 /* general path */
2141 const GLchan *tempImage = _mesa_make_temp_chan_image(ctx, dims,
2142 baseInternalFormat,
2143 baseFormat,
2144 srcWidth, srcHeight, srcDepth,
2145 srcFormat, srcType, srcAddr,
2146 srcPacking);
2147 const GLchan *src = tempImage;
2148 GLint img, row, col;
2149 if (!tempImage)
2150 return GL_FALSE;
2151 _mesa_adjust_image_for_convolution(ctx, dims, &srcWidth, &srcHeight);
2152 for (img = 0; img < srcDepth; img++) {
2153 GLubyte *dstRow = (GLubyte *) dstAddr
2154 + dstImageOffsets[dstZoffset + img] * texelBytes
2155 + dstYoffset * dstRowStride
2156 + dstXoffset * texelBytes;
2157 for (row = 0; row < srcHeight; row++) {
2158 for (col = 0; col < srcWidth; col++) {
2159 dstRow[col] = PACK_COLOR_332( CHAN_TO_UBYTE(src[RCOMP]),
2160 CHAN_TO_UBYTE(src[GCOMP]),
2161 CHAN_TO_UBYTE(src[BCOMP]) );
2162 src += 3;
2163 }
2164 dstRow += dstRowStride;
2165 }
2166 }
2167 _mesa_free((void *) tempImage);
2168 }
2169 return GL_TRUE;
2170 }
2171
2172
2173 /**
2174 * Texstore for _mesa_texformat_a8, _mesa_texformat_l8, _mesa_texformat_i8.
2175 */
2176 static GLboolean
2177 _mesa_texstore_a8(TEXSTORE_PARAMS)
2178 {
2179 const GLuint texelBytes = _mesa_get_format_bytes(dstFormat);
2180 const GLenum baseFormat = _mesa_get_format_base_format(dstFormat);
2181
2182 ASSERT(dstFormat == MESA_FORMAT_A8 ||
2183 dstFormat == MESA_FORMAT_L8 ||
2184 dstFormat == MESA_FORMAT_I8);
2185 ASSERT(texelBytes == 1);
2186
2187 if (!ctx->_ImageTransferState &&
2188 !srcPacking->SwapBytes &&
2189 baseInternalFormat == srcFormat &&
2190 srcType == GL_UNSIGNED_BYTE) {
2191 /* simple memcpy path */
2192 memcpy_texture(ctx, dims,
2193 dstFormat, dstAddr, dstXoffset, dstYoffset, dstZoffset,
2194 dstRowStride,
2195 dstImageOffsets,
2196 srcWidth, srcHeight, srcDepth, srcFormat, srcType,
2197 srcAddr, srcPacking);
2198 }
2199 else if (!ctx->_ImageTransferState &&
2200 srcType == GL_UNSIGNED_BYTE &&
2201 can_swizzle(baseInternalFormat) &&
2202 can_swizzle(srcFormat)) {
2203
2204 GLubyte dstmap[4];
2205
2206 /* dstmap - how to swizzle from RGBA to dst format:
2207 */
2208 if (dstFormat == MESA_FORMAT_A8) {
2209 dstmap[0] = 3;
2210 }
2211 else {
2212 dstmap[0] = 0;
2213 }
2214 dstmap[1] = ZERO; /* ? */
2215 dstmap[2] = ZERO; /* ? */
2216 dstmap[3] = ONE; /* ? */
2217
2218 _mesa_swizzle_ubyte_image(ctx, dims,
2219 srcFormat,
2220 srcType,
2221 baseInternalFormat,
2222 dstmap, 1,
2223 dstAddr, dstXoffset, dstYoffset, dstZoffset,
2224 dstRowStride, dstImageOffsets,
2225 srcWidth, srcHeight, srcDepth, srcAddr,
2226 srcPacking);
2227 }
2228 else {
2229 /* general path */
2230 const GLchan *tempImage = _mesa_make_temp_chan_image(ctx, dims,
2231 baseInternalFormat,
2232 baseFormat,
2233 srcWidth, srcHeight, srcDepth,
2234 srcFormat, srcType, srcAddr,
2235 srcPacking);
2236 const GLchan *src = tempImage;
2237 GLint img, row, col;
2238 if (!tempImage)
2239 return GL_FALSE;
2240 _mesa_adjust_image_for_convolution(ctx, dims, &srcWidth, &srcHeight);
2241 for (img = 0; img < srcDepth; img++) {
2242 GLubyte *dstRow = (GLubyte *) dstAddr
2243 + dstImageOffsets[dstZoffset + img] * texelBytes
2244 + dstYoffset * dstRowStride
2245 + dstXoffset * texelBytes;
2246 for (row = 0; row < srcHeight; row++) {
2247 for (col = 0; col < srcWidth; col++) {
2248 dstRow[col] = CHAN_TO_UBYTE(src[col]);
2249 }
2250 dstRow += dstRowStride;
2251 src += srcWidth;
2252 }
2253 }
2254 _mesa_free((void *) tempImage);
2255 }
2256 return GL_TRUE;
2257 }
2258
2259
2260
2261 static GLboolean
2262 _mesa_texstore_ci8(TEXSTORE_PARAMS)
2263 {
2264 const GLuint texelBytes = _mesa_get_format_bytes(dstFormat);
2265
2266 (void) dims; (void) baseInternalFormat;
2267 ASSERT(dstFormat == MESA_FORMAT_CI8);
2268 ASSERT(texelBytes == 1);
2269 ASSERT(baseInternalFormat == GL_COLOR_INDEX);
2270
2271 if (!ctx->_ImageTransferState &&
2272 !srcPacking->SwapBytes &&
2273 srcFormat == GL_COLOR_INDEX &&
2274 srcType == GL_UNSIGNED_BYTE) {
2275 /* simple memcpy path */
2276 memcpy_texture(ctx, dims,
2277 dstFormat, dstAddr, dstXoffset, dstYoffset, dstZoffset,
2278 dstRowStride,
2279 dstImageOffsets,
2280 srcWidth, srcHeight, srcDepth, srcFormat, srcType,
2281 srcAddr, srcPacking);
2282 }
2283 else {
2284 /* general path */
2285 GLint img, row;
2286 for (img = 0; img < srcDepth; img++) {
2287 GLubyte *dstRow = (GLubyte *) dstAddr
2288 + dstImageOffsets[dstZoffset + img] * texelBytes
2289 + dstYoffset * dstRowStride
2290 + dstXoffset * texelBytes;
2291 for (row = 0; row < srcHeight; row++) {
2292 const GLvoid *src = _mesa_image_address(dims, srcPacking,
2293 srcAddr, srcWidth, srcHeight, srcFormat, srcType, img, row, 0);
2294 _mesa_unpack_index_span(ctx, srcWidth, GL_UNSIGNED_BYTE, dstRow,
2295 srcType, src, srcPacking,
2296 ctx->_ImageTransferState);
2297 dstRow += dstRowStride;
2298 }
2299 }
2300 }
2301 return GL_TRUE;
2302 }
2303
2304
2305 /**
2306 * Texstore for _mesa_texformat_ycbcr or _mesa_texformat_ycbcr_REV.
2307 */
2308 static GLboolean
2309 _mesa_texstore_ycbcr(TEXSTORE_PARAMS)
2310 {
2311 const GLboolean littleEndian = _mesa_little_endian();
2312 const GLuint texelBytes = _mesa_get_format_bytes(dstFormat);
2313
2314 (void) ctx; (void) dims; (void) baseInternalFormat;
2315
2316 ASSERT((dstFormat == MESA_FORMAT_YCBCR) ||
2317 (dstFormat == MESA_FORMAT_YCBCR_REV));
2318 ASSERT(texelBytes == 2);
2319 ASSERT(ctx->Extensions.MESA_ycbcr_texture);
2320 ASSERT(srcFormat == GL_YCBCR_MESA);
2321 ASSERT((srcType == GL_UNSIGNED_SHORT_8_8_MESA) ||
2322 (srcType == GL_UNSIGNED_SHORT_8_8_REV_MESA));
2323 ASSERT(baseInternalFormat == GL_YCBCR_MESA);
2324
2325 /* always just memcpy since no pixel transfer ops apply */
2326 memcpy_texture(ctx, dims,
2327 dstFormat, dstAddr, dstXoffset, dstYoffset, dstZoffset,
2328 dstRowStride,
2329 dstImageOffsets,
2330 srcWidth, srcHeight, srcDepth, srcFormat, srcType,
2331 srcAddr, srcPacking);
2332
2333 /* Check if we need byte swapping */
2334 /* XXX the logic here _might_ be wrong */
2335 if (srcPacking->SwapBytes ^
2336 (srcType == GL_UNSIGNED_SHORT_8_8_REV_MESA) ^
2337 (dstFormat == MESA_FORMAT_YCBCR_REV) ^
2338 !littleEndian) {
2339 GLint img, row;
2340 for (img = 0; img < srcDepth; img++) {
2341 GLubyte *dstRow = (GLubyte *) dstAddr
2342 + dstImageOffsets[dstZoffset + img] * texelBytes
2343 + dstYoffset * dstRowStride
2344 + dstXoffset * texelBytes;
2345 for (row = 0; row < srcHeight; row++) {
2346 _mesa_swap2((GLushort *) dstRow, srcWidth);
2347 dstRow += dstRowStride;
2348 }
2349 }
2350 }
2351 return GL_TRUE;
2352 }
2353
2354 static GLboolean
2355 _mesa_texstore_dudv8(TEXSTORE_PARAMS)
2356 {
2357 const GLboolean littleEndian = _mesa_little_endian();
2358 const GLuint texelBytes = _mesa_get_format_bytes(dstFormat);
2359
2360 ASSERT(dstFormat == MESA_FORMAT_DUDV8);
2361 ASSERT(texelBytes == 2);
2362 ASSERT(ctx->Extensions.ATI_envmap_bumpmap);
2363 ASSERT((srcFormat == GL_DU8DV8_ATI) ||
2364 (srcFormat == GL_DUDV_ATI));
2365 ASSERT(baseInternalFormat == GL_DUDV_ATI);
2366
2367 if (!srcPacking->SwapBytes && srcType == GL_BYTE &&
2368 littleEndian) {
2369 /* simple memcpy path */
2370 memcpy_texture(ctx, dims,
2371 dstFormat, dstAddr, dstXoffset, dstYoffset, dstZoffset,
2372 dstRowStride,
2373 dstImageOffsets,
2374 srcWidth, srcHeight, srcDepth, srcFormat, srcType,
2375 srcAddr, srcPacking);
2376 }
2377 else if (srcType == GL_BYTE) {
2378
2379 GLubyte dstmap[4];
2380
2381 /* dstmap - how to swizzle from RGBA to dst format:
2382 */
2383 if (littleEndian) {
2384 dstmap[0] = 0;
2385 dstmap[1] = 3;
2386 }
2387 else {
2388 dstmap[0] = 3;
2389 dstmap[1] = 0;
2390 }
2391 dstmap[2] = ZERO; /* ? */
2392 dstmap[3] = ONE; /* ? */
2393
2394 _mesa_swizzle_ubyte_image(ctx, dims,
2395 GL_LUMINANCE_ALPHA, /* hack */
2396 GL_UNSIGNED_BYTE, /* hack */
2397 GL_LUMINANCE_ALPHA, /* hack */
2398 dstmap, 2,
2399 dstAddr, dstXoffset, dstYoffset, dstZoffset,
2400 dstRowStride, dstImageOffsets,
2401 srcWidth, srcHeight, srcDepth, srcAddr,
2402 srcPacking);
2403 }
2404 else {
2405 /* general path - note this is defined for 2d textures only */
2406 const GLint components = _mesa_components_in_format(baseInternalFormat);
2407 const GLint srcStride = _mesa_image_row_stride(srcPacking, srcWidth,
2408 srcFormat, srcType);
2409 GLbyte *tempImage, *dst, *src;
2410 GLint row;
2411
2412 tempImage = (GLbyte *) _mesa_malloc(srcWidth * srcHeight * srcDepth
2413 * components * sizeof(GLbyte));
2414 if (!tempImage)
2415 return GL_FALSE;
2416
2417 src = (GLbyte *) _mesa_image_address(dims, srcPacking, srcAddr,
2418 srcWidth, srcHeight,
2419 srcFormat, srcType,
2420 0, 0, 0);
2421
2422 dst = tempImage;
2423 for (row = 0; row < srcHeight; row++) {
2424 _mesa_unpack_dudv_span_byte(ctx, srcWidth, baseInternalFormat,
2425 dst, srcFormat, srcType, src,
2426 srcPacking, 0);
2427 dst += srcWidth * components;
2428 src += srcStride;
2429 }
2430
2431 src = tempImage;
2432 dst = (GLbyte *) dstAddr
2433 + dstYoffset * dstRowStride
2434 + dstXoffset * texelBytes;
2435 for (row = 0; row < srcHeight; row++) {
2436 memcpy(dst, src, srcWidth * texelBytes);
2437 dst += dstRowStride;
2438 src += srcWidth * texelBytes;
2439 }
2440 _mesa_free((void *) tempImage);
2441 }
2442 return GL_TRUE;
2443 }
2444
2445 /**
2446 * Store a texture in MESA_FORMAT_SIGNED_RGBA8888 or MESA_FORMAT_SIGNED_RGBA8888_REV
2447 */
2448 static GLboolean
2449 _mesa_texstore_signed_rgba8888(TEXSTORE_PARAMS)
2450 {
2451 const GLboolean littleEndian = _mesa_little_endian();
2452 const GLuint texelBytes = _mesa_get_format_bytes(dstFormat);
2453 const GLenum baseFormat = _mesa_get_format_base_format(dstFormat);
2454
2455 ASSERT(dstFormat == MESA_FORMAT_SIGNED_RGBA8888 ||
2456 dstFormat == MESA_FORMAT_SIGNED_RGBA8888_REV);
2457 ASSERT(texelBytes == 4);
2458
2459 if (!ctx->_ImageTransferState &&
2460 !srcPacking->SwapBytes &&
2461 dstFormat == MESA_FORMAT_SIGNED_RGBA8888 &&
2462 baseInternalFormat == GL_RGBA &&
2463 ((srcFormat == GL_RGBA && srcType == GL_BYTE && !littleEndian) ||
2464 (srcFormat == GL_ABGR_EXT && srcType == GL_BYTE && littleEndian))) {
2465 /* simple memcpy path */
2466 memcpy_texture(ctx, dims,
2467 dstFormat, dstAddr, dstXoffset, dstYoffset, dstZoffset,
2468 dstRowStride,
2469 dstImageOffsets,
2470 srcWidth, srcHeight, srcDepth, srcFormat, srcType,
2471 srcAddr, srcPacking);
2472 }
2473 else if (!ctx->_ImageTransferState &&
2474 !srcPacking->SwapBytes &&
2475 dstFormat == MESA_FORMAT_SIGNED_RGBA8888_REV &&
2476 baseInternalFormat == GL_RGBA &&
2477 ((srcFormat == GL_RGBA && srcType == GL_BYTE && littleEndian) ||
2478 (srcFormat == GL_ABGR_EXT && srcType == GL_BYTE && !littleEndian))) {
2479 /* simple memcpy path */
2480 memcpy_texture(ctx, dims,
2481 dstFormat, dstAddr, dstXoffset, dstYoffset, dstZoffset,
2482 dstRowStride,
2483 dstImageOffsets,
2484 srcWidth, srcHeight, srcDepth, srcFormat, srcType,
2485 srcAddr, srcPacking);
2486 }
2487 else if (!ctx->_ImageTransferState &&
2488 (srcType == GL_BYTE) &&
2489 can_swizzle(baseInternalFormat) &&
2490 can_swizzle(srcFormat)) {
2491
2492 GLubyte dstmap[4];
2493
2494 /* dstmap - how to swizzle from RGBA to dst format:
2495 */
2496 if ((littleEndian && dstFormat == MESA_FORMAT_SIGNED_RGBA8888) ||
2497 (!littleEndian && dstFormat == MESA_FORMAT_SIGNED_RGBA8888_REV)) {
2498 dstmap[3] = 0;
2499 dstmap[2] = 1;
2500 dstmap[1] = 2;
2501 dstmap[0] = 3;
2502 }
2503 else {
2504 dstmap[3] = 3;
2505 dstmap[2] = 2;
2506 dstmap[1] = 1;
2507 dstmap[0] = 0;
2508 }
2509
2510 _mesa_swizzle_ubyte_image(ctx, dims,
2511 srcFormat,
2512 srcType,
2513 baseInternalFormat,
2514 dstmap, 4,
2515 dstAddr, dstXoffset, dstYoffset, dstZoffset,
2516 dstRowStride, dstImageOffsets,
2517 srcWidth, srcHeight, srcDepth, srcAddr,
2518 srcPacking);
2519 }
2520 else {
2521 /* general path */
2522 const GLfloat *tempImage = make_temp_float_image(ctx, dims,
2523 baseInternalFormat,
2524 baseFormat,
2525 srcWidth, srcHeight, srcDepth,
2526 srcFormat, srcType, srcAddr,
2527 srcPacking);
2528 const GLfloat *srcRow = tempImage;
2529 GLint img, row, col;
2530 if (!tempImage)
2531 return GL_FALSE;
2532 _mesa_adjust_image_for_convolution(ctx, dims, &srcWidth, &srcHeight);
2533 for (img = 0; img < srcDepth; img++) {
2534 GLubyte *dstRow = (GLubyte *) dstAddr
2535 + dstImageOffsets[dstZoffset + img] * texelBytes
2536 + dstYoffset * dstRowStride
2537 + dstXoffset * texelBytes;
2538 for (row = 0; row < srcHeight; row++) {
2539 GLuint *dstUI = (GLuint *) dstRow;
2540 if (dstFormat == MESA_FORMAT_SIGNED_RGBA8888) {
2541 for (col = 0; col < srcWidth; col++) {
2542 dstUI[col] = PACK_COLOR_8888( FLOAT_TO_BYTE_TEX(srcRow[RCOMP]),
2543 FLOAT_TO_BYTE_TEX(srcRow[GCOMP]),
2544 FLOAT_TO_BYTE_TEX(srcRow[BCOMP]),
2545 FLOAT_TO_BYTE_TEX(srcRow[ACOMP]) );
2546 srcRow += 4;
2547 }
2548 }
2549 else {
2550 for (col = 0; col < srcWidth; col++) {
2551 dstUI[col] = PACK_COLOR_8888_REV( FLOAT_TO_BYTE_TEX(srcRow[RCOMP]),
2552 FLOAT_TO_BYTE_TEX(srcRow[GCOMP]),
2553 FLOAT_TO_BYTE_TEX(srcRow[BCOMP]),
2554 FLOAT_TO_BYTE_TEX(srcRow[ACOMP]) );
2555 srcRow += 4;
2556 }
2557 }
2558 dstRow += dstRowStride;
2559 }
2560 }
2561 _mesa_free((void *) tempImage);
2562 }
2563 return GL_TRUE;
2564 }
2565
2566 /**
2567 * Store a combined depth/stencil texture image.
2568 */
2569 static GLboolean
2570 _mesa_texstore_z24_s8(TEXSTORE_PARAMS)
2571 {
2572 const GLfloat depthScale = (GLfloat) 0xffffff;
2573 const GLint srcRowStride
2574 = _mesa_image_row_stride(srcPacking, srcWidth, srcFormat, srcType)
2575 / sizeof(GLuint);
2576 GLint img, row;
2577
2578 ASSERT(dstFormat == MESA_FORMAT_Z24_S8);
2579 ASSERT(srcFormat == GL_DEPTH_STENCIL_EXT || srcFormat == GL_DEPTH_COMPONENT);
2580 ASSERT(srcFormat != GL_DEPTH_STENCIL_EXT || srcType == GL_UNSIGNED_INT_24_8_EXT);
2581
2582 /* In case we only upload depth we need to preserve the stencil */
2583 if (srcFormat == GL_DEPTH_COMPONENT) {
2584 for (img = 0; img < srcDepth; img++) {
2585 GLuint *dstRow = (GLuint *) dstAddr
2586 + dstImageOffsets[dstZoffset + img]
2587 + dstYoffset * dstRowStride / sizeof(GLuint)
2588 + dstXoffset;
2589 const GLuint *src
2590 = (const GLuint *) _mesa_image_address(dims, srcPacking, srcAddr,
2591 srcWidth, srcHeight,
2592 srcFormat, srcType,
2593 img, 0, 0);
2594 for (row = 0; row < srcHeight; row++) {
2595 GLuint depth[MAX_WIDTH];
2596 GLint i;
2597 _mesa_unpack_depth_span(ctx, srcWidth,
2598 GL_UNSIGNED_INT, /* dst type */
2599 depth, /* dst addr */
2600 depthScale,
2601 srcType, src, srcPacking);
2602
2603 for (i = 0; i < srcWidth; i++)
2604 dstRow[i] = depth[i] << 8 | (dstRow[i] & 0x000000FF);
2605
2606 src += srcRowStride;
2607 dstRow += dstRowStride / sizeof(GLuint);
2608 }
2609 }
2610 }
2611 else if (ctx->Pixel.DepthScale == 1.0f &&
2612 ctx->Pixel.DepthBias == 0.0f &&
2613 !srcPacking->SwapBytes) {
2614 /* simple path */
2615 memcpy_texture(ctx, dims,
2616 dstFormat, dstAddr, dstXoffset, dstYoffset, dstZoffset,
2617 dstRowStride,
2618 dstImageOffsets,
2619 srcWidth, srcHeight, srcDepth, srcFormat, srcType,
2620 srcAddr, srcPacking);
2621 }
2622 else {
2623 /* general path */
2624 const GLint srcRowStride
2625 = _mesa_image_row_stride(srcPacking, srcWidth, srcFormat, srcType)
2626 / sizeof(GLuint);
2627 GLint img, row;
2628
2629 for (img = 0; img < srcDepth; img++) {
2630 GLuint *dstRow = (GLuint *) dstAddr
2631 + dstImageOffsets[dstZoffset + img]
2632 + dstYoffset * dstRowStride / sizeof(GLuint)
2633 + dstXoffset;
2634 const GLuint *src
2635 = (const GLuint *) _mesa_image_address(dims, srcPacking, srcAddr,
2636 srcWidth, srcHeight,
2637 srcFormat, srcType,
2638 img, 0, 0);
2639 for (row = 0; row < srcHeight; row++) {
2640 GLubyte stencil[MAX_WIDTH];
2641 GLint i;
2642 /* the 24 depth bits will be in the high position: */
2643 _mesa_unpack_depth_span(ctx, srcWidth,
2644 GL_UNSIGNED_INT_24_8_EXT, /* dst type */
2645 dstRow, /* dst addr */
2646 (GLuint) depthScale,
2647 srcType, src, srcPacking);
2648 /* get the 8-bit stencil values */
2649 _mesa_unpack_stencil_span(ctx, srcWidth,
2650 GL_UNSIGNED_BYTE, /* dst type */
2651 stencil, /* dst addr */
2652 srcType, src, srcPacking,
2653 ctx->_ImageTransferState);
2654 /* merge stencil values into depth values */
2655 for (i = 0; i < srcWidth; i++)
2656 dstRow[i] |= stencil[i];
2657
2658 src += srcRowStride;
2659 dstRow += dstRowStride / sizeof(GLuint);
2660 }
2661 }
2662 }
2663 return GL_TRUE;
2664 }
2665
2666
2667 /**
2668 * Store a combined depth/stencil texture image.
2669 */
2670 static GLboolean
2671 _mesa_texstore_s8_z24(TEXSTORE_PARAMS)
2672 {
2673 const GLuint depthScale = 0xffffff;
2674 const GLint srcRowStride
2675 = _mesa_image_row_stride(srcPacking, srcWidth, srcFormat, srcType)
2676 / sizeof(GLuint);
2677 GLint img, row;
2678
2679 ASSERT(dstFormat == MESA_FORMAT_S8_Z24);
2680 ASSERT(srcFormat == GL_DEPTH_STENCIL_EXT || srcFormat == GL_DEPTH_COMPONENT);
2681 ASSERT(srcFormat != GL_DEPTH_STENCIL_EXT || srcType == GL_UNSIGNED_INT_24_8_EXT);
2682
2683 /* In case we only upload depth we need to preserve the stencil */
2684 if (srcFormat == GL_DEPTH_COMPONENT) {
2685 for (img = 0; img < srcDepth; img++) {
2686 GLuint *dstRow = (GLuint *) dstAddr
2687 + dstImageOffsets[dstZoffset + img]
2688 + dstYoffset * dstRowStride / sizeof(GLuint)
2689 + dstXoffset;
2690 const GLuint *src
2691 = (const GLuint *) _mesa_image_address(dims, srcPacking, srcAddr,
2692 srcWidth, srcHeight,
2693 srcFormat, srcType,
2694 img, 0, 0);
2695 for (row = 0; row < srcHeight; row++) {
2696 GLuint depth[MAX_WIDTH];
2697 GLint i;
2698 _mesa_unpack_depth_span(ctx, srcWidth,
2699 GL_UNSIGNED_INT, /* dst type */
2700 depth, /* dst addr */
2701 depthScale,
2702 srcType, src, srcPacking);
2703
2704 for (i = 0; i < srcWidth; i++)
2705 dstRow[i] = depth[i] | (dstRow[i] & 0xFF000000);
2706
2707 src += srcRowStride;
2708 dstRow += dstRowStride / sizeof(GLuint);
2709 }
2710 }
2711 }
2712 else {
2713 for (img = 0; img < srcDepth; img++) {
2714 GLuint *dstRow = (GLuint *) dstAddr
2715 + dstImageOffsets[dstZoffset + img]
2716 + dstYoffset * dstRowStride / sizeof(GLuint)
2717 + dstXoffset;
2718 const GLuint *src
2719 = (const GLuint *) _mesa_image_address(dims, srcPacking, srcAddr,
2720 srcWidth, srcHeight,
2721 srcFormat, srcType,
2722 img, 0, 0);
2723 for (row = 0; row < srcHeight; row++) {
2724 GLubyte stencil[MAX_WIDTH];
2725 GLint i;
2726 /* the 24 depth bits will be in the low position: */
2727 _mesa_unpack_depth_span(ctx, srcWidth,
2728 GL_UNSIGNED_INT, /* dst type */
2729 dstRow, /* dst addr */
2730 depthScale,
2731 srcType, src, srcPacking);
2732 /* get the 8-bit stencil values */
2733 _mesa_unpack_stencil_span(ctx, srcWidth,
2734 GL_UNSIGNED_BYTE, /* dst type */
2735 stencil, /* dst addr */
2736 srcType, src, srcPacking,
2737 ctx->_ImageTransferState);
2738 /* merge stencil values into depth values */
2739 for (i = 0; i < srcWidth; i++)
2740 dstRow[i] |= stencil[i] << 24;
2741
2742 src += srcRowStride;
2743 dstRow += dstRowStride / sizeof(GLuint);
2744 }
2745 }
2746 }
2747 return GL_TRUE;
2748 }
2749
2750 /**
2751 * Store an image in any of the formats:
2752 * _mesa_texformat_rgba_float32
2753 * _mesa_texformat_rgb_float32
2754 * _mesa_texformat_alpha_float32
2755 * _mesa_texformat_luminance_float32
2756 * _mesa_texformat_luminance_alpha_float32
2757 * _mesa_texformat_intensity_float32
2758 */
2759 static GLboolean
2760 _mesa_texstore_rgba_float32(TEXSTORE_PARAMS)
2761 {
2762 const GLuint texelBytes = _mesa_get_format_bytes(dstFormat);
2763 const GLenum baseFormat = _mesa_get_format_base_format(dstFormat);
2764 const GLint components = _mesa_components_in_format(baseFormat);
2765
2766 ASSERT(dstFormat == MESA_FORMAT_RGBA_FLOAT32 ||
2767 dstFormat == MESA_FORMAT_RGB_FLOAT32 ||
2768 dstFormat == MESA_FORMAT_ALPHA_FLOAT32 ||
2769 dstFormat == MESA_FORMAT_LUMINANCE_FLOAT32 ||
2770 dstFormat == MESA_FORMAT_LUMINANCE_ALPHA_FLOAT32 ||
2771 dstFormat == MESA_FORMAT_INTENSITY_FLOAT32);
2772 ASSERT(baseInternalFormat == GL_RGBA ||
2773 baseInternalFormat == GL_RGB ||
2774 baseInternalFormat == GL_ALPHA ||
2775 baseInternalFormat == GL_LUMINANCE ||
2776 baseInternalFormat == GL_LUMINANCE_ALPHA ||
2777 baseInternalFormat == GL_INTENSITY);
2778 ASSERT(texelBytes == components * sizeof(GLfloat));
2779
2780 if (!ctx->_ImageTransferState &&
2781 !srcPacking->SwapBytes &&
2782 baseInternalFormat == srcFormat &&
2783 srcType == GL_FLOAT) {
2784 /* simple memcpy path */
2785 memcpy_texture(ctx, dims,
2786 dstFormat, dstAddr, dstXoffset, dstYoffset, dstZoffset,
2787 dstRowStride,
2788 dstImageOffsets,
2789 srcWidth, srcHeight, srcDepth, srcFormat, srcType,
2790 srcAddr, srcPacking);
2791 }
2792 else {
2793 /* general path */
2794 const GLfloat *tempImage = make_temp_float_image(ctx, dims,
2795 baseInternalFormat,
2796 baseFormat,
2797 srcWidth, srcHeight, srcDepth,
2798 srcFormat, srcType, srcAddr,
2799 srcPacking);
2800 const GLfloat *srcRow = tempImage;
2801 GLint bytesPerRow;
2802 GLint img, row;
2803 if (!tempImage)
2804 return GL_FALSE;
2805 _mesa_adjust_image_for_convolution(ctx, dims, &srcWidth, &srcHeight);
2806 bytesPerRow = srcWidth * components * sizeof(GLfloat);
2807 for (img = 0; img < srcDepth; img++) {
2808 GLubyte *dstRow = (GLubyte *) dstAddr
2809 + dstImageOffsets[dstZoffset + img] * texelBytes
2810 + dstYoffset * dstRowStride
2811 + dstXoffset * texelBytes;
2812 for (row = 0; row < srcHeight; row++) {
2813 _mesa_memcpy(dstRow, srcRow, bytesPerRow);
2814 dstRow += dstRowStride;
2815 srcRow += srcWidth * components;
2816 }
2817 }
2818
2819 _mesa_free((void *) tempImage);
2820 }
2821 return GL_TRUE;
2822 }
2823
2824
2825 /**
2826 * As above, but store 16-bit floats.
2827 */
2828 static GLboolean
2829 _mesa_texstore_rgba_float16(TEXSTORE_PARAMS)
2830 {
2831 const GLuint texelBytes = _mesa_get_format_bytes(dstFormat);
2832 const GLenum baseFormat = _mesa_get_format_base_format(dstFormat);
2833 const GLint components = _mesa_components_in_format(baseFormat);
2834
2835 ASSERT(dstFormat == MESA_FORMAT_RGBA_FLOAT16 ||
2836 dstFormat == MESA_FORMAT_RGB_FLOAT16 ||
2837 dstFormat == MESA_FORMAT_ALPHA_FLOAT16 ||
2838 dstFormat == MESA_FORMAT_LUMINANCE_FLOAT16 ||
2839 dstFormat == MESA_FORMAT_LUMINANCE_ALPHA_FLOAT16 ||
2840 dstFormat == MESA_FORMAT_INTENSITY_FLOAT16);
2841 ASSERT(baseInternalFormat == GL_RGBA ||
2842 baseInternalFormat == GL_RGB ||
2843 baseInternalFormat == GL_ALPHA ||
2844 baseInternalFormat == GL_LUMINANCE ||
2845 baseInternalFormat == GL_LUMINANCE_ALPHA ||
2846 baseInternalFormat == GL_INTENSITY);
2847 ASSERT(texelBytes == components * sizeof(GLhalfARB));
2848
2849 if (!ctx->_ImageTransferState &&
2850 !srcPacking->SwapBytes &&
2851 baseInternalFormat == srcFormat &&
2852 srcType == GL_HALF_FLOAT_ARB) {
2853 /* simple memcpy path */
2854 memcpy_texture(ctx, dims,
2855 dstFormat, dstAddr, dstXoffset, dstYoffset, dstZoffset,
2856 dstRowStride,
2857 dstImageOffsets,
2858 srcWidth, srcHeight, srcDepth, srcFormat, srcType,
2859 srcAddr, srcPacking);
2860 }
2861 else {
2862 /* general path */
2863 const GLfloat *tempImage = make_temp_float_image(ctx, dims,
2864 baseInternalFormat,
2865 baseFormat,
2866 srcWidth, srcHeight, srcDepth,
2867 srcFormat, srcType, srcAddr,
2868 srcPacking);
2869 const GLfloat *src = tempImage;
2870 GLint img, row;
2871 if (!tempImage)
2872 return GL_FALSE;
2873 _mesa_adjust_image_for_convolution(ctx, dims, &srcWidth, &srcHeight);
2874 for (img = 0; img < srcDepth; img++) {
2875 GLubyte *dstRow = (GLubyte *) dstAddr
2876 + dstImageOffsets[dstZoffset + img] * texelBytes
2877 + dstYoffset * dstRowStride
2878 + dstXoffset * texelBytes;
2879 for (row = 0; row < srcHeight; row++) {
2880 GLhalfARB *dstTexel = (GLhalfARB *) dstRow;
2881 GLint i;
2882 for (i = 0; i < srcWidth * components; i++) {
2883 dstTexel[i] = _mesa_float_to_half(src[i]);
2884 }
2885 dstRow += dstRowStride;
2886 src += srcWidth * components;
2887 }
2888 }
2889
2890 _mesa_free((void *) tempImage);
2891 }
2892 return GL_TRUE;
2893 }
2894
2895
2896 #if FEATURE_EXT_texture_sRGB
2897 static GLboolean
2898 _mesa_texstore_srgb8(TEXSTORE_PARAMS)
2899 {
2900 gl_format newDstFormat;
2901 GLboolean k;
2902
2903 ASSERT(dstFormat == MESA_FORMAT_SRGB8);
2904
2905 /* reuse normal rgb texstore code */
2906 newDstFormat = MESA_FORMAT_RGB888;
2907
2908 k = _mesa_texstore_rgb888(ctx, dims, baseInternalFormat,
2909 newDstFormat, dstAddr,
2910 dstXoffset, dstYoffset, dstZoffset,
2911 dstRowStride, dstImageOffsets,
2912 srcWidth, srcHeight, srcDepth,
2913 srcFormat, srcType,
2914 srcAddr, srcPacking);
2915 return k;
2916 }
2917
2918
2919 static GLboolean
2920 _mesa_texstore_srgba8(TEXSTORE_PARAMS)
2921 {
2922 gl_format newDstFormat;
2923 GLboolean k;
2924
2925 ASSERT(dstFormat == MESA_FORMAT_SRGBA8);
2926
2927 /* reuse normal rgba texstore code */
2928 newDstFormat = MESA_FORMAT_RGBA8888;
2929 k = _mesa_texstore_rgba8888(ctx, dims, baseInternalFormat,
2930 newDstFormat, dstAddr,
2931 dstXoffset, dstYoffset, dstZoffset,
2932 dstRowStride, dstImageOffsets,
2933 srcWidth, srcHeight, srcDepth,
2934 srcFormat, srcType,
2935 srcAddr, srcPacking);
2936 return k;
2937 }
2938
2939
2940 static GLboolean
2941 _mesa_texstore_sargb8(TEXSTORE_PARAMS)
2942 {
2943 gl_format newDstFormat;
2944 GLboolean k;
2945
2946 ASSERT(dstFormat == MESA_FORMAT_SARGB8);
2947
2948 /* reuse normal rgba texstore code */
2949 newDstFormat = MESA_FORMAT_ARGB8888;
2950
2951 k = _mesa_texstore_argb8888(ctx, dims, baseInternalFormat,
2952 newDstFormat, dstAddr,
2953 dstXoffset, dstYoffset, dstZoffset,
2954 dstRowStride, dstImageOffsets,
2955 srcWidth, srcHeight, srcDepth,
2956 srcFormat, srcType,
2957 srcAddr, srcPacking);
2958 return k;
2959 }
2960
2961
2962 static GLboolean
2963 _mesa_texstore_sl8(TEXSTORE_PARAMS)
2964 {
2965 gl_format newDstFormat;
2966 GLboolean k;
2967
2968 ASSERT(dstFormat == MESA_FORMAT_SL8);
2969
2970 newDstFormat = MESA_FORMAT_L8;
2971
2972 /* _mesa_textore_a8 handles luminance8 too */
2973 k = _mesa_texstore_a8(ctx, dims, baseInternalFormat,
2974 newDstFormat, dstAddr,
2975 dstXoffset, dstYoffset, dstZoffset,
2976 dstRowStride, dstImageOffsets,
2977 srcWidth, srcHeight, srcDepth,
2978 srcFormat, srcType,
2979 srcAddr, srcPacking);
2980 return k;
2981 }
2982
2983
2984 static GLboolean
2985 _mesa_texstore_sla8(TEXSTORE_PARAMS)
2986 {
2987 gl_format newDstFormat;
2988 GLboolean k;
2989
2990 ASSERT(dstFormat == MESA_FORMAT_SLA8);
2991
2992 /* reuse normal luminance/alpha texstore code */
2993 newDstFormat = MESA_FORMAT_AL88;
2994
2995 k = _mesa_texstore_al88(ctx, dims, baseInternalFormat,
2996 newDstFormat, dstAddr,
2997 dstXoffset, dstYoffset, dstZoffset,
2998 dstRowStride, dstImageOffsets,
2999 srcWidth, srcHeight, srcDepth,
3000 srcFormat, srcType,
3001 srcAddr, srcPacking);
3002 return k;
3003 }
3004
3005 #endif /* FEATURE_EXT_texture_sRGB */
3006
3007
3008
3009
3010 /**
3011 * Table mapping MESA_FORMAT_8 to _mesa_texstore_*()
3012 * XXX this is somewhat temporary.
3013 */
3014 static struct {
3015 gl_format Name;
3016 StoreTexImageFunc Store;
3017 }
3018 texstore_funcs[MESA_FORMAT_COUNT] =
3019 {
3020 { MESA_FORMAT_NONE, NULL },
3021 { MESA_FORMAT_RGBA8888, _mesa_texstore_rgba8888 },
3022 { MESA_FORMAT_RGBA8888_REV, _mesa_texstore_rgba8888 },
3023 { MESA_FORMAT_ARGB8888, _mesa_texstore_argb8888 },
3024 { MESA_FORMAT_ARGB8888_REV, _mesa_texstore_argb8888 },
3025 { MESA_FORMAT_XRGB8888, _mesa_texstore_argb8888 },
3026 { MESA_FORMAT_RGB888, _mesa_texstore_rgb888 },
3027 { MESA_FORMAT_BGR888, _mesa_texstore_bgr888 },
3028 { MESA_FORMAT_RGB565, _mesa_texstore_rgb565 },
3029 { MESA_FORMAT_RGB565_REV, _mesa_texstore_rgb565 },
3030 { MESA_FORMAT_ARGB4444, _mesa_texstore_argb4444 },
3031 { MESA_FORMAT_ARGB4444_REV, _mesa_texstore_argb4444 },
3032 { MESA_FORMAT_RGBA5551, _mesa_texstore_rgba5551 },
3033 { MESA_FORMAT_ARGB1555, _mesa_texstore_argb1555 },
3034 { MESA_FORMAT_ARGB1555_REV, _mesa_texstore_argb1555 },
3035 { MESA_FORMAT_AL88, _mesa_texstore_al88 },
3036 { MESA_FORMAT_AL88_REV, _mesa_texstore_al88 },
3037 { MESA_FORMAT_RGB332, _mesa_texstore_rgb332 },
3038 { MESA_FORMAT_A8, _mesa_texstore_a8 },
3039 { MESA_FORMAT_L8, _mesa_texstore_a8 },
3040 { MESA_FORMAT_I8, _mesa_texstore_a8 },
3041 { MESA_FORMAT_CI8, _mesa_texstore_ci8 },
3042 { MESA_FORMAT_YCBCR, _mesa_texstore_ycbcr },
3043 { MESA_FORMAT_YCBCR_REV, _mesa_texstore_ycbcr },
3044 { MESA_FORMAT_Z24_S8, _mesa_texstore_z24_s8 },
3045 { MESA_FORMAT_S8_Z24, _mesa_texstore_s8_z24 },
3046 { MESA_FORMAT_Z16, _mesa_texstore_z16 },
3047 { MESA_FORMAT_X8_Z24, _mesa_texstore_x8_z24 },
3048 { MESA_FORMAT_Z32, _mesa_texstore_z32 },
3049 { MESA_FORMAT_S8, NULL/*_mesa_texstore_s8*/ },
3050 { MESA_FORMAT_SRGB8, _mesa_texstore_srgb8 },
3051 { MESA_FORMAT_SRGBA8, _mesa_texstore_srgba8 },
3052 { MESA_FORMAT_SARGB8, _mesa_texstore_sargb8 },
3053 { MESA_FORMAT_SL8, _mesa_texstore_sl8 },
3054 { MESA_FORMAT_SLA8, _mesa_texstore_sla8 },
3055 { MESA_FORMAT_SRGB_DXT1, _mesa_texstore_rgb_dxt1 },
3056 { MESA_FORMAT_SRGBA_DXT1, _mesa_texstore_rgba_dxt1 },
3057 { MESA_FORMAT_SRGBA_DXT3, _mesa_texstore_rgba_dxt3 },
3058 { MESA_FORMAT_SRGBA_DXT5, _mesa_texstore_rgba_dxt5 },
3059 { MESA_FORMAT_RGB_FXT1, _mesa_texstore_rgb_fxt1 },
3060 { MESA_FORMAT_RGBA_FXT1, _mesa_texstore_rgba_fxt1 },
3061 { MESA_FORMAT_RGB_DXT1, _mesa_texstore_rgb_dxt1 },
3062 { MESA_FORMAT_RGBA_DXT1, _mesa_texstore_rgba_dxt1 },
3063 { MESA_FORMAT_RGBA_DXT3, _mesa_texstore_rgba_dxt3 },
3064 { MESA_FORMAT_RGBA_DXT5, _mesa_texstore_rgba_dxt5 },
3065 { MESA_FORMAT_RGBA_FLOAT32, _mesa_texstore_rgba_float32 },
3066 { MESA_FORMAT_RGBA_FLOAT16, _mesa_texstore_rgba_float16 },
3067 { MESA_FORMAT_RGB_FLOAT32, _mesa_texstore_rgba_float32 },
3068 { MESA_FORMAT_RGB_FLOAT16, _mesa_texstore_rgba_float16 },
3069 { MESA_FORMAT_ALPHA_FLOAT32, _mesa_texstore_rgba_float32 },
3070 { MESA_FORMAT_ALPHA_FLOAT16, _mesa_texstore_rgba_float16 },
3071 { MESA_FORMAT_LUMINANCE_FLOAT32, _mesa_texstore_rgba_float32 },
3072 { MESA_FORMAT_LUMINANCE_FLOAT16, _mesa_texstore_rgba_float16 },
3073 { MESA_FORMAT_LUMINANCE_ALPHA_FLOAT32, _mesa_texstore_rgba_float32 },
3074 { MESA_FORMAT_LUMINANCE_ALPHA_FLOAT16, _mesa_texstore_rgba_float16 },
3075 { MESA_FORMAT_INTENSITY_FLOAT32, _mesa_texstore_rgba_float32 },
3076 { MESA_FORMAT_INTENSITY_FLOAT16, _mesa_texstore_rgba_float16 },
3077 { MESA_FORMAT_DUDV8, _mesa_texstore_dudv8 },
3078 { MESA_FORMAT_SIGNED_RGBA8888, _mesa_texstore_signed_rgba8888 },
3079 { MESA_FORMAT_SIGNED_RGBA8888_REV, _mesa_texstore_signed_rgba8888 },
3080 { MESA_FORMAT_SIGNED_RGBA_16, NULL },
3081 };
3082
3083
3084 /**
3085 * Return the StoreTexImageFunc pointer to store an image in the given format.
3086 */
3087 static StoreTexImageFunc
3088 _mesa_get_texstore_func(gl_format format)
3089 {
3090 GLuint i;
3091 #ifdef DEBUG
3092 for (i = 0; i < MESA_FORMAT_COUNT; i++) {
3093 ASSERT(texstore_funcs[i].Name == i);
3094 }
3095 #endif
3096 ASSERT(texstore_funcs[format].Name == format);
3097 return texstore_funcs[format].Store;
3098 }
3099
3100
3101 /**
3102 * Store user data into texture memory.
3103 * Called via glTex[Sub]Image1/2/3D()
3104 */
3105 GLboolean
3106 _mesa_texstore(TEXSTORE_PARAMS)
3107 {
3108 StoreTexImageFunc storeImage;
3109 GLboolean success;
3110
3111 storeImage = _mesa_get_texstore_func(dstFormat);
3112
3113 assert(storeImage);
3114
3115 success = storeImage(ctx, dims, baseInternalFormat,
3116 dstFormat, dstAddr, dstXoffset, dstYoffset, dstZoffset,
3117 dstRowStride, dstImageOffsets,
3118 srcWidth, srcHeight, srcDepth,
3119 srcFormat, srcType, srcAddr, srcPacking);
3120 return success;
3121 }
3122
3123
3124 /**
3125 * Check if an unpack PBO is active prior to fetching a texture image.
3126 * If so, do bounds checking and map the buffer into main memory.
3127 * Any errors detected will be recorded.
3128 * The caller _must_ call _mesa_unmap_teximage_pbo() too!
3129 */
3130 const GLvoid *
3131 _mesa_validate_pbo_teximage(GLcontext *ctx, GLuint dimensions,
3132 GLsizei width, GLsizei height, GLsizei depth,
3133 GLenum format, GLenum type, const GLvoid *pixels,
3134 const struct gl_pixelstore_attrib *unpack,
3135 const char *funcName)
3136 {
3137 GLubyte *buf;
3138
3139 if (!_mesa_is_bufferobj(unpack->BufferObj)) {
3140 /* no PBO */
3141 return pixels;
3142 }
3143 if (!_mesa_validate_pbo_access(dimensions, unpack, width, height, depth,
3144 format, type, pixels)) {
3145 _mesa_error(ctx, GL_INVALID_OPERATION, funcName, "(invalid PBO access");
3146 return NULL;
3147 }
3148
3149 buf = (GLubyte *) ctx->Driver.MapBuffer(ctx, GL_PIXEL_UNPACK_BUFFER_EXT,
3150 GL_READ_ONLY_ARB, unpack->BufferObj);
3151 if (!buf) {
3152 _mesa_error(ctx, GL_INVALID_OPERATION, funcName, "(PBO is mapped");
3153 return NULL;
3154 }
3155
3156 return ADD_POINTERS(buf, pixels);
3157 }
3158
3159
3160 /**
3161 * Check if an unpack PBO is active prior to fetching a compressed texture
3162 * image.
3163 * If so, do bounds checking and map the buffer into main memory.
3164 * Any errors detected will be recorded.
3165 * The caller _must_ call _mesa_unmap_teximage_pbo() too!
3166 */
3167 const GLvoid *
3168 _mesa_validate_pbo_compressed_teximage(GLcontext *ctx,
3169 GLsizei imageSize, const GLvoid *pixels,
3170 const struct gl_pixelstore_attrib *packing,
3171 const char *funcName)
3172 {
3173 GLubyte *buf;
3174
3175 if (!_mesa_is_bufferobj(packing->BufferObj)) {
3176 /* not using a PBO - return pointer unchanged */
3177 return pixels;
3178 }
3179 if ((const GLubyte *) pixels + imageSize >
3180 ((const GLubyte *) 0) + packing->BufferObj->Size) {
3181 /* out of bounds read! */
3182 _mesa_error(ctx, GL_INVALID_OPERATION, funcName, "(invalid PBO access");
3183 return NULL;
3184 }
3185
3186 buf = (GLubyte*) ctx->Driver.MapBuffer(ctx, GL_PIXEL_UNPACK_BUFFER_EXT,
3187 GL_READ_ONLY_ARB, packing->BufferObj);
3188 if (!buf) {
3189 _mesa_error(ctx, GL_INVALID_OPERATION, funcName, "(PBO is mapped");
3190 return NULL;
3191 }
3192
3193 return ADD_POINTERS(buf, pixels);
3194 }
3195
3196
3197 /**
3198 * This function must be called after either of the validate_pbo_*_teximage()
3199 * functions. It unmaps the PBO buffer if it was mapped earlier.
3200 */
3201 void
3202 _mesa_unmap_teximage_pbo(GLcontext *ctx,
3203 const struct gl_pixelstore_attrib *unpack)
3204 {
3205 if (_mesa_is_bufferobj(unpack->BufferObj)) {
3206 ctx->Driver.UnmapBuffer(ctx, GL_PIXEL_UNPACK_BUFFER_EXT,
3207 unpack->BufferObj);
3208 }
3209 }
3210
3211
3212 /** Return texture size in bytes */
3213 static GLuint
3214 texture_size(const struct gl_texture_image *texImage)
3215 {
3216 GLuint sz = _mesa_format_image_size(texImage->TexFormat, texImage->Width,
3217 texImage->Height, texImage->Depth);
3218 return sz;
3219 }
3220
3221
3222 /** Return row stride in bytes */
3223 static GLuint
3224 texture_row_stride(const struct gl_texture_image *texImage)
3225 {
3226 GLuint stride = _mesa_format_row_stride(texImage->TexFormat,
3227 texImage->Width);
3228 return stride;
3229 }
3230
3231
3232
3233 /**
3234 * This is the software fallback for Driver.TexImage1D()
3235 * and Driver.CopyTexImage1D().
3236 * \sa _mesa_store_teximage2d()
3237 * Note that the width may not be the actual texture width since it may
3238 * be changed by convolution w/ GL_REDUCE. The texImage->Width field will
3239 * have the actual texture size.
3240 */
3241 void
3242 _mesa_store_teximage1d(GLcontext *ctx, GLenum target, GLint level,
3243 GLint internalFormat,
3244 GLint width, GLint border,
3245 GLenum format, GLenum type, const GLvoid *pixels,
3246 const struct gl_pixelstore_attrib *packing,
3247 struct gl_texture_object *texObj,
3248 struct gl_texture_image *texImage)
3249 {
3250 GLuint sizeInBytes;
3251 (void) border;
3252
3253 /* allocate memory */
3254 sizeInBytes = texture_size(texImage);
3255 texImage->Data = _mesa_alloc_texmemory(sizeInBytes);
3256 if (!texImage->Data) {
3257 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexImage1D");
3258 return;
3259 }
3260
3261 pixels = _mesa_validate_pbo_teximage(ctx, 1, width, 1, 1, format, type,
3262 pixels, packing, "glTexImage1D");
3263 if (!pixels) {
3264 /* Note: we check for a NULL image pointer here, _after_ we allocated
3265 * memory for the texture. That's what the GL spec calls for.
3266 */
3267 return;
3268 }
3269 else {
3270 const GLint dstRowStride = 0;
3271 GLboolean success = _mesa_texstore(ctx, 1, texImage->_BaseFormat,
3272 texImage->TexFormat,
3273 texImage->Data,
3274 0, 0, 0, /* dstX/Y/Zoffset */
3275 dstRowStride,
3276 texImage->ImageOffsets,
3277 width, 1, 1,
3278 format, type, pixels, packing);
3279 if (!success) {
3280 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexImage1D");
3281 }
3282 }
3283
3284 _mesa_unmap_teximage_pbo(ctx, packing);
3285 }
3286
3287
3288 /**
3289 * This is the software fallback for Driver.TexImage2D()
3290 * and Driver.CopyTexImage2D().
3291 *
3292 * This function is oriented toward storing images in main memory, rather
3293 * than VRAM. Device driver's can easily plug in their own replacement.
3294 *
3295 * Note: width and height may be pre-convolved dimensions, but
3296 * texImage->Width and texImage->Height will be post-convolved dimensions.
3297 */
3298 void
3299 _mesa_store_teximage2d(GLcontext *ctx, GLenum target, GLint level,
3300 GLint internalFormat,
3301 GLint width, GLint height, GLint border,
3302 GLenum format, GLenum type, const void *pixels,
3303 const struct gl_pixelstore_attrib *packing,
3304 struct gl_texture_object *texObj,
3305 struct gl_texture_image *texImage)
3306 {
3307 GLuint sizeInBytes;
3308 (void) border;
3309
3310 /* allocate memory */
3311 sizeInBytes = texture_size(texImage);
3312 texImage->Data = _mesa_alloc_texmemory(sizeInBytes);
3313 if (!texImage->Data) {
3314 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexImage2D");
3315 return;
3316 }
3317
3318 pixels = _mesa_validate_pbo_teximage(ctx, 2, width, height, 1, format, type,
3319 pixels, packing, "glTexImage2D");
3320 if (!pixels) {
3321 /* Note: we check for a NULL image pointer here, _after_ we allocated
3322 * memory for the texture. That's what the GL spec calls for.
3323 */
3324 return;
3325 }
3326 else {
3327 GLint dstRowStride = texture_row_stride(texImage);
3328 GLboolean success = _mesa_texstore(ctx, 2, texImage->_BaseFormat,
3329 texImage->TexFormat,
3330 texImage->Data,
3331 0, 0, 0, /* dstX/Y/Zoffset */
3332 dstRowStride,
3333 texImage->ImageOffsets,
3334 width, height, 1,
3335 format, type, pixels, packing);
3336 if (!success) {
3337 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexImage2D");
3338 }
3339 }
3340
3341 _mesa_unmap_teximage_pbo(ctx, packing);
3342 }
3343
3344
3345
3346 /**
3347 * This is the software fallback for Driver.TexImage3D()
3348 * and Driver.CopyTexImage3D().
3349 * \sa _mesa_store_teximage2d()
3350 */
3351 void
3352 _mesa_store_teximage3d(GLcontext *ctx, GLenum target, GLint level,
3353 GLint internalFormat,
3354 GLint width, GLint height, GLint depth, GLint border,
3355 GLenum format, GLenum type, const void *pixels,
3356 const struct gl_pixelstore_attrib *packing,
3357 struct gl_texture_object *texObj,
3358 struct gl_texture_image *texImage)
3359 {
3360 GLuint sizeInBytes;
3361 (void) border;
3362
3363 /* allocate memory */
3364 sizeInBytes = texture_size(texImage);
3365 texImage->Data = _mesa_alloc_texmemory(sizeInBytes);
3366 if (!texImage->Data) {
3367 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexImage3D");
3368 return;
3369 }
3370
3371 pixels = _mesa_validate_pbo_teximage(ctx, 3, width, height, depth, format,
3372 type, pixels, packing, "glTexImage3D");
3373 if (!pixels) {
3374 /* Note: we check for a NULL image pointer here, _after_ we allocated
3375 * memory for the texture. That's what the GL spec calls for.
3376 */
3377 return;
3378 }
3379 else {
3380 GLint dstRowStride = texture_row_stride(texImage);
3381 GLboolean success = _mesa_texstore(ctx, 3, texImage->_BaseFormat,
3382 texImage->TexFormat,
3383 texImage->Data,
3384 0, 0, 0, /* dstX/Y/Zoffset */
3385 dstRowStride,
3386 texImage->ImageOffsets,
3387 width, height, depth,
3388 format, type, pixels, packing);
3389 if (!success) {
3390 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexImage3D");
3391 }
3392 }
3393
3394 _mesa_unmap_teximage_pbo(ctx, packing);
3395 }
3396
3397
3398
3399
3400 /*
3401 * This is the software fallback for Driver.TexSubImage1D()
3402 * and Driver.CopyTexSubImage1D().
3403 */
3404 void
3405 _mesa_store_texsubimage1d(GLcontext *ctx, GLenum target, GLint level,
3406 GLint xoffset, GLint width,
3407 GLenum format, GLenum type, const void *pixels,
3408 const struct gl_pixelstore_attrib *packing,
3409 struct gl_texture_object *texObj,
3410 struct gl_texture_image *texImage)
3411 {
3412 /* get pointer to src pixels (may be in a pbo which we'll map here) */
3413 pixels = _mesa_validate_pbo_teximage(ctx, 1, width, 1, 1, format, type,
3414 pixels, packing, "glTexSubImage1D");
3415 if (!pixels)
3416 return;
3417
3418 {
3419 const GLint dstRowStride = 0;
3420 GLboolean success = _mesa_texstore(ctx, 1, texImage->_BaseFormat,
3421 texImage->TexFormat,
3422 texImage->Data,
3423 xoffset, 0, 0, /* offsets */
3424 dstRowStride,
3425 texImage->ImageOffsets,
3426 width, 1, 1,
3427 format, type, pixels, packing);
3428 if (!success) {
3429 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexSubImage1D");
3430 }
3431 }
3432
3433 _mesa_unmap_teximage_pbo(ctx, packing);
3434 }
3435
3436
3437
3438 /**
3439 * This is the software fallback for Driver.TexSubImage2D()
3440 * and Driver.CopyTexSubImage2D().
3441 */
3442 void
3443 _mesa_store_texsubimage2d(GLcontext *ctx, GLenum target, GLint level,
3444 GLint xoffset, GLint yoffset,
3445 GLint width, GLint height,
3446 GLenum format, GLenum type, const void *pixels,
3447 const struct gl_pixelstore_attrib *packing,
3448 struct gl_texture_object *texObj,
3449 struct gl_texture_image *texImage)
3450 {
3451 /* get pointer to src pixels (may be in a pbo which we'll map here) */
3452 pixels = _mesa_validate_pbo_teximage(ctx, 2, width, height, 1, format, type,
3453 pixels, packing, "glTexSubImage2D");
3454 if (!pixels)
3455 return;
3456
3457 {
3458 GLint dstRowStride = texture_row_stride(texImage);
3459 GLboolean success = _mesa_texstore(ctx, 2, texImage->_BaseFormat,
3460 texImage->TexFormat,
3461 texImage->Data,
3462 xoffset, yoffset, 0,
3463 dstRowStride,
3464 texImage->ImageOffsets,
3465 width, height, 1,
3466 format, type, pixels, packing);
3467 if (!success) {
3468 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexSubImage2D");
3469 }
3470 }
3471
3472 _mesa_unmap_teximage_pbo(ctx, packing);
3473 }
3474
3475
3476 /*
3477 * This is the software fallback for Driver.TexSubImage3D().
3478 * and Driver.CopyTexSubImage3D().
3479 */
3480 void
3481 _mesa_store_texsubimage3d(GLcontext *ctx, GLenum target, GLint level,
3482 GLint xoffset, GLint yoffset, GLint zoffset,
3483 GLint width, GLint height, GLint depth,
3484 GLenum format, GLenum type, const void *pixels,
3485 const struct gl_pixelstore_attrib *packing,
3486 struct gl_texture_object *texObj,
3487 struct gl_texture_image *texImage)
3488 {
3489 /* get pointer to src pixels (may be in a pbo which we'll map here) */
3490 pixels = _mesa_validate_pbo_teximage(ctx, 3, width, height, depth, format,
3491 type, pixels, packing,
3492 "glTexSubImage3D");
3493 if (!pixels)
3494 return;
3495
3496 {
3497 GLint dstRowStride = texture_row_stride(texImage);
3498 GLboolean success = _mesa_texstore(ctx, 3, texImage->_BaseFormat,
3499 texImage->TexFormat,
3500 texImage->Data,
3501 xoffset, yoffset, zoffset,
3502 dstRowStride,
3503 texImage->ImageOffsets,
3504 width, height, depth,
3505 format, type, pixels, packing);
3506 if (!success) {
3507 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexSubImage3D");
3508 }
3509 }
3510
3511 _mesa_unmap_teximage_pbo(ctx, packing);
3512 }
3513
3514
3515 /*
3516 * Fallback for Driver.CompressedTexImage1D()
3517 */
3518 void
3519 _mesa_store_compressed_teximage1d(GLcontext *ctx, GLenum target, GLint level,
3520 GLint internalFormat,
3521 GLint width, GLint border,
3522 GLsizei imageSize, const GLvoid *data,
3523 struct gl_texture_object *texObj,
3524 struct gl_texture_image *texImage)
3525 {
3526 /* this space intentionally left blank */
3527 (void) ctx;
3528 (void) target; (void) level;
3529 (void) internalFormat;
3530 (void) width; (void) border;
3531 (void) imageSize; (void) data;
3532 (void) texObj;
3533 (void) texImage;
3534 }
3535
3536
3537
3538 /**
3539 * Fallback for Driver.CompressedTexImage2D()
3540 */
3541 void
3542 _mesa_store_compressed_teximage2d(GLcontext *ctx, GLenum target, GLint level,
3543 GLint internalFormat,
3544 GLint width, GLint height, GLint border,
3545 GLsizei imageSize, const GLvoid *data,
3546 struct gl_texture_object *texObj,
3547 struct gl_texture_image *texImage)
3548 {
3549 (void) width; (void) height; (void) border;
3550
3551 /* This is pretty simple, basically just do a memcpy without worrying
3552 * about the usual image unpacking or image transfer operations.
3553 */
3554 ASSERT(texObj);
3555 ASSERT(texImage);
3556 ASSERT(texImage->Width > 0);
3557 ASSERT(texImage->Height > 0);
3558 ASSERT(texImage->Depth == 1);
3559 ASSERT(texImage->Data == NULL); /* was freed in glCompressedTexImage2DARB */
3560
3561 /* allocate storage */
3562 texImage->Data = _mesa_alloc_texmemory(imageSize);
3563 if (!texImage->Data) {
3564 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCompressedTexImage2DARB");
3565 return;
3566 }
3567
3568 data = _mesa_validate_pbo_compressed_teximage(ctx, imageSize, data,
3569 &ctx->Unpack,
3570 "glCompressedTexImage2D");
3571 if (!data)
3572 return;
3573
3574 /* copy the data */
3575 MEMCPY(texImage->Data, data, imageSize);
3576
3577 _mesa_unmap_teximage_pbo(ctx, &ctx->Unpack);
3578 }
3579
3580
3581
3582 /*
3583 * Fallback for Driver.CompressedTexImage3D()
3584 */
3585 void
3586 _mesa_store_compressed_teximage3d(GLcontext *ctx, GLenum target, GLint level,
3587 GLint internalFormat,
3588 GLint width, GLint height, GLint depth,
3589 GLint border,
3590 GLsizei imageSize, const GLvoid *data,
3591 struct gl_texture_object *texObj,
3592 struct gl_texture_image *texImage)
3593 {
3594 /* this space intentionally left blank */
3595 (void) ctx;
3596 (void) target; (void) level;
3597 (void) internalFormat;
3598 (void) width; (void) height; (void) depth;
3599 (void) border;
3600 (void) imageSize; (void) data;
3601 (void) texObj;
3602 (void) texImage;
3603 }
3604
3605
3606
3607 /**
3608 * Fallback for Driver.CompressedTexSubImage1D()
3609 */
3610 void
3611 _mesa_store_compressed_texsubimage1d(GLcontext *ctx, GLenum target,
3612 GLint level,
3613 GLint xoffset, GLsizei width,
3614 GLenum format,
3615 GLsizei imageSize, const GLvoid *data,
3616 struct gl_texture_object *texObj,
3617 struct gl_texture_image *texImage)
3618 {
3619 /* there are no compressed 1D texture formats yet */
3620 (void) ctx;
3621 (void) target; (void) level;
3622 (void) xoffset; (void) width;
3623 (void) format;
3624 (void) imageSize; (void) data;
3625 (void) texObj;
3626 (void) texImage;
3627 }
3628
3629
3630 /**
3631 * Fallback for Driver.CompressedTexSubImage2D()
3632 */
3633 void
3634 _mesa_store_compressed_texsubimage2d(GLcontext *ctx, GLenum target,
3635 GLint level,
3636 GLint xoffset, GLint yoffset,
3637 GLsizei width, GLsizei height,
3638 GLenum format,
3639 GLsizei imageSize, const GLvoid *data,
3640 struct gl_texture_object *texObj,
3641 struct gl_texture_image *texImage)
3642 {
3643 GLint bytesPerRow, destRowStride, srcRowStride;
3644 GLint i, rows;
3645 GLubyte *dest;
3646 const GLubyte *src;
3647 const gl_format texFormat = texImage->TexFormat;
3648 const GLint destWidth = texImage->Width;
3649 GLuint bw, bh;
3650
3651 _mesa_get_format_block_size(texFormat, &bw, &bh);
3652
3653 (void) level;
3654 (void) format;
3655
3656 /* these should have been caught sooner */
3657 ASSERT((width % bw) == 0 || width == 2 || width == 1);
3658 ASSERT((height % bh) == 0 || height == 2 || height == 1);
3659 ASSERT((xoffset % bw) == 0);
3660 ASSERT((yoffset % bh) == 0);
3661
3662 /* get pointer to src pixels (may be in a pbo which we'll map here) */
3663 data = _mesa_validate_pbo_compressed_teximage(ctx, imageSize, data,
3664 &ctx->Unpack,
3665 "glCompressedTexSubImage2D");
3666 if (!data)
3667 return;
3668
3669 srcRowStride = _mesa_format_row_stride(texFormat, width);
3670 src = (const GLubyte *) data;
3671
3672 destRowStride = _mesa_format_row_stride(texFormat, destWidth);
3673 dest = _mesa_compressed_image_address(xoffset, yoffset, 0,
3674 texFormat, destWidth,
3675 (GLubyte *) texImage->Data);
3676
3677 bytesPerRow = srcRowStride; /* bytes per row of blocks */
3678 rows = height / bh; /* rows in blocks */
3679
3680 /* copy rows of blocks */
3681 for (i = 0; i < rows; i++) {
3682 MEMCPY(dest, src, bytesPerRow);
3683 dest += destRowStride;
3684 src += srcRowStride;
3685 }
3686
3687 _mesa_unmap_teximage_pbo(ctx, &ctx->Unpack);
3688 }
3689
3690
3691 /**
3692 * Fallback for Driver.CompressedTexSubImage3D()
3693 */
3694 void
3695 _mesa_store_compressed_texsubimage3d(GLcontext *ctx, GLenum target,
3696 GLint level,
3697 GLint xoffset, GLint yoffset, GLint zoffset,
3698 GLsizei width, GLsizei height, GLsizei depth,
3699 GLenum format,
3700 GLsizei imageSize, const GLvoid *data,
3701 struct gl_texture_object *texObj,
3702 struct gl_texture_image *texImage)
3703 {
3704 /* there are no compressed 3D texture formats yet */
3705 (void) ctx;
3706 (void) target; (void) level;
3707 (void) xoffset; (void) yoffset; (void) zoffset;
3708 (void) width; (void) height; (void) depth;
3709 (void) format;
3710 (void) imageSize; (void) data;
3711 (void) texObj;
3712 (void) texImage;
3713 }