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