mesa: fix texstore with GL_COLOR_INDEX data
[mesa.git] / src / mesa / main / texstore.c
1 /*
2 * Mesa 3-D graphics library
3 *
4 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
5 * Copyright (c) 2008-2009 VMware, Inc.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
21 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 * 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.TexImage = _mesa_store_teximage;
41 * ctx->Driver.TexSubImage = _mesa_store_texsubimage;
42 * etc...
43 *
44 * Texture image processing is actually kind of complicated. We have to do:
45 * Format/type conversions
46 * pixel unpacking
47 * pixel transfer (scale, bais, lookup, etc)
48 *
49 * These functions can handle most everything, including processing full
50 * images and sub-images.
51 */
52
53
54 #include "glheader.h"
55 #include "bufferobj.h"
56 #include "colormac.h"
57 #include "format_pack.h"
58 #include "format_utils.h"
59 #include "image.h"
60 #include "macros.h"
61 #include "mipmap.h"
62 #include "mtypes.h"
63 #include "pack.h"
64 #include "pbo.h"
65 #include "imports.h"
66 #include "texcompress.h"
67 #include "texcompress_fxt1.h"
68 #include "texcompress_rgtc.h"
69 #include "texcompress_s3tc.h"
70 #include "texcompress_etc.h"
71 #include "texcompress_bptc.h"
72 #include "teximage.h"
73 #include "texstore.h"
74 #include "enums.h"
75 #include "glformats.h"
76 #include "../../gallium/auxiliary/util/u_format_rgb9e5.h"
77 #include "../../gallium/auxiliary/util/u_format_r11g11b10f.h"
78
79
80 enum {
81 ZERO = 4,
82 ONE = 5
83 };
84
85
86 /**
87 * Texture image storage function.
88 */
89 typedef GLboolean (*StoreTexImageFunc)(TEXSTORE_PARAMS);
90
91
92 enum {
93 IDX_LUMINANCE = 0,
94 IDX_ALPHA,
95 IDX_INTENSITY,
96 IDX_LUMINANCE_ALPHA,
97 IDX_RGB,
98 IDX_RGBA,
99 IDX_RED,
100 IDX_GREEN,
101 IDX_BLUE,
102 IDX_BGR,
103 IDX_BGRA,
104 IDX_ABGR,
105 IDX_RG,
106 MAX_IDX
107 };
108
109 #define MAP1(x) MAP4(x, ZERO, ZERO, ZERO)
110 #define MAP2(x,y) MAP4(x, y, ZERO, ZERO)
111 #define MAP3(x,y,z) MAP4(x, y, z, ZERO)
112 #define MAP4(x,y,z,w) { x, y, z, w, ZERO, ONE }
113
114
115 static const struct {
116 GLubyte format_idx;
117 GLubyte to_rgba[6];
118 GLubyte from_rgba[6];
119 } mappings[MAX_IDX] =
120 {
121 {
122 IDX_LUMINANCE,
123 MAP4(0,0,0,ONE),
124 MAP1(0)
125 },
126
127 {
128 IDX_ALPHA,
129 MAP4(ZERO, ZERO, ZERO, 0),
130 MAP1(3)
131 },
132
133 {
134 IDX_INTENSITY,
135 MAP4(0, 0, 0, 0),
136 MAP1(0),
137 },
138
139 {
140 IDX_LUMINANCE_ALPHA,
141 MAP4(0,0,0,1),
142 MAP2(0,3)
143 },
144
145 {
146 IDX_RGB,
147 MAP4(0,1,2,ONE),
148 MAP3(0,1,2)
149 },
150
151 {
152 IDX_RGBA,
153 MAP4(0,1,2,3),
154 MAP4(0,1,2,3),
155 },
156
157 {
158 IDX_RED,
159 MAP4(0, ZERO, ZERO, ONE),
160 MAP1(0),
161 },
162
163 {
164 IDX_GREEN,
165 MAP4(ZERO, 0, ZERO, ONE),
166 MAP1(1),
167 },
168
169 {
170 IDX_BLUE,
171 MAP4(ZERO, ZERO, 0, ONE),
172 MAP1(2),
173 },
174
175 {
176 IDX_BGR,
177 MAP4(2,1,0,ONE),
178 MAP3(2,1,0)
179 },
180
181 {
182 IDX_BGRA,
183 MAP4(2,1,0,3),
184 MAP4(2,1,0,3)
185 },
186
187 {
188 IDX_ABGR,
189 MAP4(3,2,1,0),
190 MAP4(3,2,1,0)
191 },
192
193 {
194 IDX_RG,
195 MAP4(0, 1, ZERO, ONE),
196 MAP2(0, 1)
197 },
198 };
199
200
201
202 /**
203 * Convert a GL image format enum to an IDX_* value (see above).
204 */
205 static int
206 get_map_idx(GLenum value)
207 {
208 switch (value) {
209 case GL_LUMINANCE:
210 case GL_LUMINANCE_INTEGER_EXT:
211 return IDX_LUMINANCE;
212 case GL_ALPHA:
213 case GL_ALPHA_INTEGER:
214 return IDX_ALPHA;
215 case GL_INTENSITY:
216 return IDX_INTENSITY;
217 case GL_LUMINANCE_ALPHA:
218 case GL_LUMINANCE_ALPHA_INTEGER_EXT:
219 return IDX_LUMINANCE_ALPHA;
220 case GL_RGB:
221 case GL_RGB_INTEGER:
222 return IDX_RGB;
223 case GL_RGBA:
224 case GL_RGBA_INTEGER:
225 return IDX_RGBA;
226 case GL_RED:
227 case GL_RED_INTEGER:
228 return IDX_RED;
229 case GL_GREEN:
230 return IDX_GREEN;
231 case GL_BLUE:
232 return IDX_BLUE;
233 case GL_BGR:
234 case GL_BGR_INTEGER:
235 return IDX_BGR;
236 case GL_BGRA:
237 case GL_BGRA_INTEGER:
238 return IDX_BGRA;
239 case GL_ABGR_EXT:
240 return IDX_ABGR;
241 case GL_RG:
242 case GL_RG_INTEGER:
243 return IDX_RG;
244 default:
245 _mesa_problem(NULL, "Unexpected inFormat %s",
246 _mesa_lookup_enum_by_nr(value));
247 return 0;
248 }
249 }
250
251
252 /**
253 * When promoting texture formats (see below) we need to compute the
254 * mapping of dest components back to source components.
255 * This function does that.
256 * \param inFormat the incoming format of the texture
257 * \param outFormat the final texture format
258 * \return map[6] a full 6-component map
259 */
260 static void
261 compute_component_mapping(GLenum inFormat, GLenum outFormat,
262 GLubyte *map)
263 {
264 const int inFmt = get_map_idx(inFormat);
265 const int outFmt = get_map_idx(outFormat);
266 const GLubyte *in2rgba = mappings[inFmt].to_rgba;
267 const GLubyte *rgba2out = mappings[outFmt].from_rgba;
268 int i;
269
270 for (i = 0; i < 4; i++)
271 map[i] = in2rgba[rgba2out[i]];
272
273 map[ZERO] = ZERO;
274 map[ONE] = ONE;
275
276 #if 0
277 printf("from %x/%s to %x/%s map %d %d %d %d %d %d\n",
278 inFormat, _mesa_lookup_enum_by_nr(inFormat),
279 outFormat, _mesa_lookup_enum_by_nr(outFormat),
280 map[0],
281 map[1],
282 map[2],
283 map[3],
284 map[4],
285 map[5]);
286 #endif
287 }
288
289
290 /**
291 * Make a temporary (color) texture image with GLfloat components.
292 * Apply all needed pixel unpacking and pixel transfer operations.
293 * Note that there are both logicalBaseFormat and textureBaseFormat parameters.
294 * Suppose the user specifies GL_LUMINANCE as the internal texture format
295 * but the graphics hardware doesn't support luminance textures. So, we might
296 * use an RGB hardware format instead.
297 * If logicalBaseFormat != textureBaseFormat we have some extra work to do.
298 *
299 * \param ctx the rendering context
300 * \param dims image dimensions: 1, 2 or 3
301 * \param logicalBaseFormat basic texture derived from the user's
302 * internal texture format value
303 * \param textureBaseFormat the actual basic format of the texture
304 * \param srcWidth source image width
305 * \param srcHeight source image height
306 * \param srcDepth source image depth
307 * \param srcFormat source image format
308 * \param srcType source image type
309 * \param srcAddr source image address
310 * \param srcPacking source image pixel packing
311 * \return resulting image with format = textureBaseFormat and type = GLfloat.
312 */
313 GLfloat *
314 _mesa_make_temp_float_image(struct gl_context *ctx, GLuint dims,
315 GLenum logicalBaseFormat,
316 GLenum textureBaseFormat,
317 GLint srcWidth, GLint srcHeight, GLint srcDepth,
318 GLenum srcFormat, GLenum srcType,
319 const GLvoid *srcAddr,
320 const struct gl_pixelstore_attrib *srcPacking,
321 GLbitfield transferOps)
322 {
323 GLfloat *tempImage;
324 const GLint components = _mesa_components_in_format(logicalBaseFormat);
325 const GLint srcStride =
326 _mesa_image_row_stride(srcPacking, srcWidth, srcFormat, srcType);
327 GLfloat *dst;
328 GLint img, row;
329
330 ASSERT(dims >= 1 && dims <= 3);
331
332 ASSERT(logicalBaseFormat == GL_RGBA ||
333 logicalBaseFormat == GL_RGB ||
334 logicalBaseFormat == GL_RG ||
335 logicalBaseFormat == GL_RED ||
336 logicalBaseFormat == GL_LUMINANCE_ALPHA ||
337 logicalBaseFormat == GL_LUMINANCE ||
338 logicalBaseFormat == GL_ALPHA ||
339 logicalBaseFormat == GL_INTENSITY ||
340 logicalBaseFormat == GL_DEPTH_COMPONENT);
341
342 ASSERT(textureBaseFormat == GL_RGBA ||
343 textureBaseFormat == GL_RGB ||
344 textureBaseFormat == GL_RG ||
345 textureBaseFormat == GL_RED ||
346 textureBaseFormat == GL_LUMINANCE_ALPHA ||
347 textureBaseFormat == GL_LUMINANCE ||
348 textureBaseFormat == GL_ALPHA ||
349 textureBaseFormat == GL_INTENSITY ||
350 textureBaseFormat == GL_DEPTH_COMPONENT);
351
352 tempImage = malloc(srcWidth * srcHeight * srcDepth
353 * components * sizeof(GLfloat));
354 if (!tempImage)
355 return NULL;
356
357 dst = tempImage;
358 for (img = 0; img < srcDepth; img++) {
359 const GLubyte *src
360 = (const GLubyte *) _mesa_image_address(dims, srcPacking, srcAddr,
361 srcWidth, srcHeight,
362 srcFormat, srcType,
363 img, 0, 0);
364 for (row = 0; row < srcHeight; row++) {
365 _mesa_unpack_color_span_float(ctx, srcWidth, logicalBaseFormat,
366 dst, srcFormat, srcType, src,
367 srcPacking, transferOps);
368 dst += srcWidth * components;
369 src += srcStride;
370 }
371 }
372
373 if (logicalBaseFormat != textureBaseFormat) {
374 /* more work */
375 GLint texComponents = _mesa_components_in_format(textureBaseFormat);
376 GLint logComponents = _mesa_components_in_format(logicalBaseFormat);
377 GLfloat *newImage;
378 GLint i, n;
379 GLubyte map[6];
380
381 /* we only promote up to RGB, RGBA and LUMINANCE_ALPHA formats for now */
382 ASSERT(textureBaseFormat == GL_RGB || textureBaseFormat == GL_RGBA ||
383 textureBaseFormat == GL_LUMINANCE_ALPHA);
384
385 /* The actual texture format should have at least as many components
386 * as the logical texture format.
387 */
388 ASSERT(texComponents >= logComponents);
389
390 newImage = malloc(srcWidth * srcHeight * srcDepth
391 * texComponents * sizeof(GLfloat));
392 if (!newImage) {
393 free(tempImage);
394 return NULL;
395 }
396
397 compute_component_mapping(logicalBaseFormat, textureBaseFormat, map);
398
399 n = srcWidth * srcHeight * srcDepth;
400 for (i = 0; i < n; i++) {
401 GLint k;
402 for (k = 0; k < texComponents; k++) {
403 GLint j = map[k];
404 if (j == ZERO)
405 newImage[i * texComponents + k] = 0.0F;
406 else if (j == ONE)
407 newImage[i * texComponents + k] = 1.0F;
408 else
409 newImage[i * texComponents + k] = tempImage[i * logComponents + j];
410 }
411 }
412
413 free(tempImage);
414 tempImage = newImage;
415 }
416
417 return tempImage;
418 }
419
420
421 /**
422 * Make temporary image with uint pixel values. Used for unsigned
423 * integer-valued textures.
424 */
425 static GLuint *
426 make_temp_uint_image(struct gl_context *ctx, GLuint dims,
427 GLenum logicalBaseFormat,
428 GLenum textureBaseFormat,
429 GLint srcWidth, GLint srcHeight, GLint srcDepth,
430 GLenum srcFormat, GLenum srcType,
431 const GLvoid *srcAddr,
432 const struct gl_pixelstore_attrib *srcPacking)
433 {
434 GLuint *tempImage;
435 const GLint components = _mesa_components_in_format(logicalBaseFormat);
436 const GLint srcStride =
437 _mesa_image_row_stride(srcPacking, srcWidth, srcFormat, srcType);
438 GLuint *dst;
439 GLint img, row;
440
441 ASSERT(dims >= 1 && dims <= 3);
442
443 ASSERT(logicalBaseFormat == GL_RGBA ||
444 logicalBaseFormat == GL_RGB ||
445 logicalBaseFormat == GL_RG ||
446 logicalBaseFormat == GL_RED ||
447 logicalBaseFormat == GL_LUMINANCE_ALPHA ||
448 logicalBaseFormat == GL_LUMINANCE ||
449 logicalBaseFormat == GL_INTENSITY ||
450 logicalBaseFormat == GL_ALPHA);
451
452 ASSERT(textureBaseFormat == GL_RGBA ||
453 textureBaseFormat == GL_RGB ||
454 textureBaseFormat == GL_RG ||
455 textureBaseFormat == GL_RED ||
456 textureBaseFormat == GL_LUMINANCE_ALPHA ||
457 textureBaseFormat == GL_LUMINANCE ||
458 textureBaseFormat == GL_INTENSITY ||
459 textureBaseFormat == GL_ALPHA);
460
461 tempImage = malloc(srcWidth * srcHeight * srcDepth
462 * components * sizeof(GLuint));
463 if (!tempImage)
464 return NULL;
465
466 dst = tempImage;
467 for (img = 0; img < srcDepth; img++) {
468 const GLubyte *src
469 = (const GLubyte *) _mesa_image_address(dims, srcPacking, srcAddr,
470 srcWidth, srcHeight,
471 srcFormat, srcType,
472 img, 0, 0);
473 for (row = 0; row < srcHeight; row++) {
474 _mesa_unpack_color_span_uint(ctx, srcWidth, logicalBaseFormat,
475 dst, srcFormat, srcType, src,
476 srcPacking);
477 dst += srcWidth * components;
478 src += srcStride;
479 }
480 }
481
482 if (logicalBaseFormat != textureBaseFormat) {
483 /* more work */
484 GLint texComponents = _mesa_components_in_format(textureBaseFormat);
485 GLint logComponents = _mesa_components_in_format(logicalBaseFormat);
486 GLuint *newImage;
487 GLint i, n;
488 GLubyte map[6];
489
490 /* we only promote up to RGB, RGBA and LUMINANCE_ALPHA formats for now */
491 ASSERT(textureBaseFormat == GL_RGB || textureBaseFormat == GL_RGBA ||
492 textureBaseFormat == GL_LUMINANCE_ALPHA);
493
494 /* The actual texture format should have at least as many components
495 * as the logical texture format.
496 */
497 ASSERT(texComponents >= logComponents);
498
499 newImage = malloc(srcWidth * srcHeight * srcDepth
500 * texComponents * sizeof(GLuint));
501 if (!newImage) {
502 free(tempImage);
503 return NULL;
504 }
505
506 compute_component_mapping(logicalBaseFormat, textureBaseFormat, map);
507
508 n = srcWidth * srcHeight * srcDepth;
509 for (i = 0; i < n; i++) {
510 GLint k;
511 for (k = 0; k < texComponents; k++) {
512 GLint j = map[k];
513 if (j == ZERO)
514 newImage[i * texComponents + k] = 0;
515 else if (j == ONE)
516 newImage[i * texComponents + k] = 1;
517 else
518 newImage[i * texComponents + k] = tempImage[i * logComponents + j];
519 }
520 }
521
522 free(tempImage);
523 tempImage = newImage;
524 }
525
526 return tempImage;
527 }
528
529
530
531 /**
532 * Make a temporary (color) texture image with GLubyte components.
533 * Apply all needed pixel unpacking and pixel transfer operations.
534 * Note that there are both logicalBaseFormat and textureBaseFormat parameters.
535 * Suppose the user specifies GL_LUMINANCE as the internal texture format
536 * but the graphics hardware doesn't support luminance textures. So, we might
537 * use an RGB hardware format instead.
538 * If logicalBaseFormat != textureBaseFormat we have some extra work to do.
539 *
540 * \param ctx the rendering context
541 * \param dims image dimensions: 1, 2 or 3
542 * \param logicalBaseFormat basic texture derived from the user's
543 * internal texture format value
544 * \param textureBaseFormat the actual basic format of the texture
545 * \param srcWidth source image width
546 * \param srcHeight source image height
547 * \param srcDepth source image depth
548 * \param srcFormat source image format
549 * \param srcType source image type
550 * \param srcAddr source image address
551 * \param srcPacking source image pixel packing
552 * \return resulting image with format = textureBaseFormat and type = GLubyte.
553 */
554 GLubyte *
555 _mesa_make_temp_ubyte_image(struct gl_context *ctx, GLuint dims,
556 GLenum logicalBaseFormat,
557 GLenum textureBaseFormat,
558 GLint srcWidth, GLint srcHeight, GLint srcDepth,
559 GLenum srcFormat, GLenum srcType,
560 const GLvoid *srcAddr,
561 const struct gl_pixelstore_attrib *srcPacking)
562 {
563 GLuint transferOps = ctx->_ImageTransferState;
564 const GLint components = _mesa_components_in_format(logicalBaseFormat);
565 GLint img, row;
566 GLubyte *tempImage, *dst;
567
568 ASSERT(dims >= 1 && dims <= 3);
569
570 ASSERT(logicalBaseFormat == GL_RGBA ||
571 logicalBaseFormat == GL_RGB ||
572 logicalBaseFormat == GL_RG ||
573 logicalBaseFormat == GL_RED ||
574 logicalBaseFormat == GL_LUMINANCE_ALPHA ||
575 logicalBaseFormat == GL_LUMINANCE ||
576 logicalBaseFormat == GL_ALPHA ||
577 logicalBaseFormat == GL_INTENSITY);
578
579 ASSERT(textureBaseFormat == GL_RGBA ||
580 textureBaseFormat == GL_RGB ||
581 textureBaseFormat == GL_RG ||
582 textureBaseFormat == GL_RED ||
583 textureBaseFormat == GL_LUMINANCE_ALPHA ||
584 textureBaseFormat == GL_LUMINANCE ||
585 textureBaseFormat == GL_ALPHA ||
586 textureBaseFormat == GL_INTENSITY);
587
588 /* unpack and transfer the source image */
589 tempImage = malloc(srcWidth * srcHeight * srcDepth
590 * components * sizeof(GLubyte));
591 if (!tempImage) {
592 return NULL;
593 }
594
595 dst = tempImage;
596 for (img = 0; img < srcDepth; img++) {
597 const GLint srcStride =
598 _mesa_image_row_stride(srcPacking, srcWidth, srcFormat, srcType);
599 const GLubyte *src =
600 (const GLubyte *) _mesa_image_address(dims, srcPacking, srcAddr,
601 srcWidth, srcHeight,
602 srcFormat, srcType,
603 img, 0, 0);
604 for (row = 0; row < srcHeight; row++) {
605 _mesa_unpack_color_span_ubyte(ctx, srcWidth, logicalBaseFormat, dst,
606 srcFormat, srcType, src, srcPacking,
607 transferOps);
608 dst += srcWidth * components;
609 src += srcStride;
610 }
611 }
612
613 if (logicalBaseFormat != textureBaseFormat) {
614 /* one more conversion step */
615 GLint texComponents = _mesa_components_in_format(textureBaseFormat);
616 GLint logComponents = _mesa_components_in_format(logicalBaseFormat);
617 GLubyte *newImage;
618 GLint i, n;
619 GLubyte map[6];
620
621 /* we only promote up to RGB, RGBA and LUMINANCE_ALPHA formats for now */
622 ASSERT(textureBaseFormat == GL_RGB || textureBaseFormat == GL_RGBA ||
623 textureBaseFormat == GL_LUMINANCE_ALPHA);
624
625 /* The actual texture format should have at least as many components
626 * as the logical texture format.
627 */
628 ASSERT(texComponents >= logComponents);
629
630 newImage = malloc(srcWidth * srcHeight * srcDepth
631 * texComponents * sizeof(GLubyte));
632 if (!newImage) {
633 free(tempImage);
634 return NULL;
635 }
636
637 compute_component_mapping(logicalBaseFormat, textureBaseFormat, map);
638
639 n = srcWidth * srcHeight * srcDepth;
640 for (i = 0; i < n; i++) {
641 GLint k;
642 for (k = 0; k < texComponents; k++) {
643 GLint j = map[k];
644 if (j == ZERO)
645 newImage[i * texComponents + k] = 0;
646 else if (j == ONE)
647 newImage[i * texComponents + k] = 255;
648 else
649 newImage[i * texComponents + k] = tempImage[i * logComponents + j];
650 }
651 }
652
653 free(tempImage);
654 tempImage = newImage;
655 }
656
657 return tempImage;
658 }
659
660
661 static const GLubyte map_identity[6] = { 0, 1, 2, 3, ZERO, ONE };
662 static const GLubyte map_3210[6] = { 3, 2, 1, 0, ZERO, ONE };
663 static const GLubyte map_1032[6] = { 1, 0, 3, 2, ZERO, ONE };
664
665
666 /**
667 * Teximage storage routine for when a simple memcpy will do.
668 * No pixel transfer operations or special texel encodings allowed.
669 * 1D, 2D and 3D images supported.
670 */
671 static void
672 memcpy_texture(struct gl_context *ctx,
673 GLuint dimensions,
674 mesa_format dstFormat,
675 GLint dstRowStride,
676 GLubyte **dstSlices,
677 GLint srcWidth, GLint srcHeight, GLint srcDepth,
678 GLenum srcFormat, GLenum srcType,
679 const GLvoid *srcAddr,
680 const struct gl_pixelstore_attrib *srcPacking)
681 {
682 const GLint srcRowStride = _mesa_image_row_stride(srcPacking, srcWidth,
683 srcFormat, srcType);
684 const GLint srcImageStride = _mesa_image_image_stride(srcPacking,
685 srcWidth, srcHeight, srcFormat, srcType);
686 const GLubyte *srcImage = (const GLubyte *) _mesa_image_address(dimensions,
687 srcPacking, srcAddr, srcWidth, srcHeight, srcFormat, srcType, 0, 0, 0);
688 const GLuint texelBytes = _mesa_get_format_bytes(dstFormat);
689 const GLint bytesPerRow = srcWidth * texelBytes;
690
691 if (dstRowStride == srcRowStride &&
692 dstRowStride == bytesPerRow) {
693 /* memcpy image by image */
694 GLint img;
695 for (img = 0; img < srcDepth; img++) {
696 GLubyte *dstImage = dstSlices[img];
697 memcpy(dstImage, srcImage, bytesPerRow * srcHeight);
698 srcImage += srcImageStride;
699 }
700 }
701 else {
702 /* memcpy row by row */
703 GLint img, row;
704 for (img = 0; img < srcDepth; img++) {
705 const GLubyte *srcRow = srcImage;
706 GLubyte *dstRow = dstSlices[img];
707 for (row = 0; row < srcHeight; row++) {
708 memcpy(dstRow, srcRow, bytesPerRow);
709 dstRow += dstRowStride;
710 srcRow += srcRowStride;
711 }
712 srcImage += srcImageStride;
713 }
714 }
715 }
716
717
718 /**
719 * General-case function for storing a color texture images with
720 * components that can be represented with ubytes. Example destination
721 * texture formats are MESA_FORMAT_ARGB888, ARGB4444, RGB565.
722 */
723 static GLboolean
724 store_ubyte_texture(TEXSTORE_PARAMS)
725 {
726 const GLint srcRowStride = srcWidth * 4 * sizeof(GLubyte);
727 GLubyte *tempImage, *src;
728 GLint img;
729
730 tempImage = _mesa_make_temp_ubyte_image(ctx, dims,
731 baseInternalFormat,
732 GL_RGBA,
733 srcWidth, srcHeight, srcDepth,
734 srcFormat, srcType, srcAddr,
735 srcPacking);
736 if (!tempImage)
737 return GL_FALSE;
738
739 /* This way we will use the RGB versions of the packing functions and it
740 * will work for both RGB and sRGB textures*/
741 dstFormat = _mesa_get_srgb_format_linear(dstFormat);
742
743 src = tempImage;
744 for (img = 0; img < srcDepth; img++) {
745 _mesa_pack_ubyte_rgba_rect(dstFormat, srcWidth, srcHeight,
746 src, srcRowStride,
747 dstSlices[img], dstRowStride);
748 src += srcHeight * srcRowStride;
749 }
750 free(tempImage);
751
752 return GL_TRUE;
753 }
754
755
756
757
758 /**
759 * Store a 32-bit integer or float depth component texture image.
760 */
761 static GLboolean
762 _mesa_texstore_z32(TEXSTORE_PARAMS)
763 {
764 const GLuint depthScale = 0xffffffff;
765 GLenum dstType;
766 (void) dims;
767 ASSERT(dstFormat == MESA_FORMAT_Z_UNORM32 ||
768 dstFormat == MESA_FORMAT_Z_FLOAT32);
769 ASSERT(_mesa_get_format_bytes(dstFormat) == sizeof(GLuint));
770
771 if (dstFormat == MESA_FORMAT_Z_UNORM32)
772 dstType = GL_UNSIGNED_INT;
773 else
774 dstType = GL_FLOAT;
775
776 {
777 /* general path */
778 GLint img, row;
779 for (img = 0; img < srcDepth; img++) {
780 GLubyte *dstRow = dstSlices[img];
781 for (row = 0; row < srcHeight; row++) {
782 const GLvoid *src = _mesa_image_address(dims, srcPacking,
783 srcAddr, srcWidth, srcHeight, srcFormat, srcType, img, row, 0);
784 _mesa_unpack_depth_span(ctx, srcWidth,
785 dstType, dstRow,
786 depthScale, srcType, src, srcPacking);
787 dstRow += dstRowStride;
788 }
789 }
790 }
791 return GL_TRUE;
792 }
793
794
795 /**
796 * Store a 24-bit integer depth component texture image.
797 */
798 static GLboolean
799 _mesa_texstore_x8_z24(TEXSTORE_PARAMS)
800 {
801 const GLuint depthScale = 0xffffff;
802
803 (void) dims;
804 ASSERT(dstFormat == MESA_FORMAT_Z24_UNORM_X8_UINT);
805
806 {
807 /* general path */
808 GLint img, row;
809 for (img = 0; img < srcDepth; img++) {
810 GLubyte *dstRow = dstSlices[img];
811 for (row = 0; row < srcHeight; row++) {
812 const GLvoid *src = _mesa_image_address(dims, srcPacking,
813 srcAddr, srcWidth, srcHeight, srcFormat, srcType, img, row, 0);
814 _mesa_unpack_depth_span(ctx, srcWidth,
815 GL_UNSIGNED_INT, (GLuint *) dstRow,
816 depthScale, srcType, src, srcPacking);
817 dstRow += dstRowStride;
818 }
819 }
820 }
821 return GL_TRUE;
822 }
823
824
825 /**
826 * Store a 24-bit integer depth component texture image.
827 */
828 static GLboolean
829 _mesa_texstore_z24_x8(TEXSTORE_PARAMS)
830 {
831 const GLuint depthScale = 0xffffff;
832
833 (void) dims;
834 ASSERT(dstFormat == MESA_FORMAT_X8_UINT_Z24_UNORM);
835
836 {
837 /* general path */
838 GLint img, row;
839 for (img = 0; img < srcDepth; img++) {
840 GLubyte *dstRow = dstSlices[img];
841 for (row = 0; row < srcHeight; row++) {
842 const GLvoid *src = _mesa_image_address(dims, srcPacking,
843 srcAddr, srcWidth, srcHeight, srcFormat, srcType, img, row, 0);
844 GLuint *dst = (GLuint *) dstRow;
845 GLint i;
846 _mesa_unpack_depth_span(ctx, srcWidth,
847 GL_UNSIGNED_INT, dst,
848 depthScale, srcType, src, srcPacking);
849 for (i = 0; i < srcWidth; i++)
850 dst[i] <<= 8;
851 dstRow += dstRowStride;
852 }
853 }
854 }
855 return GL_TRUE;
856 }
857
858
859 /**
860 * Store a 16-bit integer depth component texture image.
861 */
862 static GLboolean
863 _mesa_texstore_z16(TEXSTORE_PARAMS)
864 {
865 const GLuint depthScale = 0xffff;
866 (void) dims;
867 ASSERT(dstFormat == MESA_FORMAT_Z_UNORM16);
868 ASSERT(_mesa_get_format_bytes(dstFormat) == sizeof(GLushort));
869
870 {
871 /* general path */
872 GLint img, row;
873 for (img = 0; img < srcDepth; img++) {
874 GLubyte *dstRow = dstSlices[img];
875 for (row = 0; row < srcHeight; row++) {
876 const GLvoid *src = _mesa_image_address(dims, srcPacking,
877 srcAddr, srcWidth, srcHeight, srcFormat, srcType, img, row, 0);
878 GLushort *dst16 = (GLushort *) dstRow;
879 _mesa_unpack_depth_span(ctx, srcWidth,
880 GL_UNSIGNED_SHORT, dst16, depthScale,
881 srcType, src, srcPacking);
882 dstRow += dstRowStride;
883 }
884 }
885 }
886 return GL_TRUE;
887 }
888
889
890 /**
891 * Store an rgb565 or rgb565_rev texture image.
892 */
893 static GLboolean
894 _mesa_texstore_rgb565(TEXSTORE_PARAMS)
895 {
896 ASSERT(dstFormat == MESA_FORMAT_B5G6R5_UNORM ||
897 dstFormat == MESA_FORMAT_R5G6B5_UNORM);
898 ASSERT(_mesa_get_format_bytes(dstFormat) == 2);
899
900 if (!ctx->_ImageTransferState &&
901 !srcPacking->SwapBytes &&
902 baseInternalFormat == GL_RGB &&
903 srcFormat == GL_RGB &&
904 srcType == GL_UNSIGNED_BYTE &&
905 dims == 2) {
906 /* do optimized tex store */
907 const GLint srcRowStride =
908 _mesa_image_row_stride(srcPacking, srcWidth, srcFormat, srcType);
909 const GLubyte *src = (const GLubyte *)
910 _mesa_image_address(dims, srcPacking, srcAddr, srcWidth, srcHeight,
911 srcFormat, srcType, 0, 0, 0);
912 GLubyte *dst = dstSlices[0];
913 GLint row, col;
914 for (row = 0; row < srcHeight; row++) {
915 const GLubyte *srcUB = (const GLubyte *) src;
916 GLushort *dstUS = (GLushort *) dst;
917 /* check for byteswapped format */
918 if (dstFormat == MESA_FORMAT_B5G6R5_UNORM) {
919 for (col = 0; col < srcWidth; col++) {
920 dstUS[col] = PACK_COLOR_565( srcUB[0], srcUB[1], srcUB[2] );
921 srcUB += 3;
922 }
923 }
924 else {
925 for (col = 0; col < srcWidth; col++) {
926 dstUS[col] = PACK_COLOR_565_REV( srcUB[0], srcUB[1], srcUB[2] );
927 srcUB += 3;
928 }
929 }
930 dst += dstRowStride;
931 src += srcRowStride;
932 }
933 return GL_TRUE;
934 } else {
935 return GL_FALSE;
936 }
937 }
938
939
940 /**
941 * Texstore for _mesa_texformat_ycbcr or _mesa_texformat_ycbcr_REV.
942 */
943 static GLboolean
944 _mesa_texstore_ycbcr(TEXSTORE_PARAMS)
945 {
946 const GLboolean littleEndian = _mesa_little_endian();
947
948 (void) ctx; (void) dims; (void) baseInternalFormat;
949
950 ASSERT((dstFormat == MESA_FORMAT_YCBCR) ||
951 (dstFormat == MESA_FORMAT_YCBCR_REV));
952 ASSERT(_mesa_get_format_bytes(dstFormat) == 2);
953 ASSERT(ctx->Extensions.MESA_ycbcr_texture);
954 ASSERT(srcFormat == GL_YCBCR_MESA);
955 ASSERT((srcType == GL_UNSIGNED_SHORT_8_8_MESA) ||
956 (srcType == GL_UNSIGNED_SHORT_8_8_REV_MESA));
957 ASSERT(baseInternalFormat == GL_YCBCR_MESA);
958
959 /* always just memcpy since no pixel transfer ops apply */
960 memcpy_texture(ctx, dims,
961 dstFormat,
962 dstRowStride, dstSlices,
963 srcWidth, srcHeight, srcDepth, srcFormat, srcType,
964 srcAddr, srcPacking);
965
966 /* Check if we need byte swapping */
967 /* XXX the logic here _might_ be wrong */
968 if (srcPacking->SwapBytes ^
969 (srcType == GL_UNSIGNED_SHORT_8_8_REV_MESA) ^
970 (dstFormat == MESA_FORMAT_YCBCR_REV) ^
971 !littleEndian) {
972 GLint img, row;
973 for (img = 0; img < srcDepth; img++) {
974 GLubyte *dstRow = dstSlices[img];
975 for (row = 0; row < srcHeight; row++) {
976 _mesa_swap2((GLushort *) dstRow, srcWidth);
977 dstRow += dstRowStride;
978 }
979 }
980 }
981 return GL_TRUE;
982 }
983
984
985 /**
986 * Store a combined depth/stencil texture image.
987 */
988 static GLboolean
989 _mesa_texstore_z24_s8(TEXSTORE_PARAMS)
990 {
991 const GLuint depthScale = 0xffffff;
992 const GLint srcRowStride
993 = _mesa_image_row_stride(srcPacking, srcWidth, srcFormat, srcType);
994 GLint img, row;
995 GLuint *depth = malloc(srcWidth * sizeof(GLuint));
996 GLubyte *stencil = malloc(srcWidth * sizeof(GLubyte));
997
998 ASSERT(dstFormat == MESA_FORMAT_S8_UINT_Z24_UNORM);
999 ASSERT(srcFormat == GL_DEPTH_STENCIL_EXT ||
1000 srcFormat == GL_DEPTH_COMPONENT ||
1001 srcFormat == GL_STENCIL_INDEX);
1002 ASSERT(srcFormat != GL_DEPTH_STENCIL_EXT ||
1003 srcType == GL_UNSIGNED_INT_24_8_EXT ||
1004 srcType == GL_FLOAT_32_UNSIGNED_INT_24_8_REV);
1005
1006 if (!depth || !stencil) {
1007 free(depth);
1008 free(stencil);
1009 return GL_FALSE;
1010 }
1011
1012 /* In case we only upload depth we need to preserve the stencil */
1013 for (img = 0; img < srcDepth; img++) {
1014 GLuint *dstRow = (GLuint *) dstSlices[img];
1015 const GLubyte *src
1016 = (const GLubyte *) _mesa_image_address(dims, srcPacking, srcAddr,
1017 srcWidth, srcHeight,
1018 srcFormat, srcType,
1019 img, 0, 0);
1020 for (row = 0; row < srcHeight; row++) {
1021 GLint i;
1022 GLboolean keepdepth = GL_FALSE, keepstencil = GL_FALSE;
1023
1024 if (srcFormat == GL_DEPTH_COMPONENT) { /* preserve stencil */
1025 keepstencil = GL_TRUE;
1026 }
1027 else if (srcFormat == GL_STENCIL_INDEX) { /* preserve depth */
1028 keepdepth = GL_TRUE;
1029 }
1030
1031 if (keepdepth == GL_FALSE)
1032 /* the 24 depth bits will be in the low position: */
1033 _mesa_unpack_depth_span(ctx, srcWidth,
1034 GL_UNSIGNED_INT, /* dst type */
1035 keepstencil ? depth : dstRow, /* dst addr */
1036 depthScale,
1037 srcType, src, srcPacking);
1038
1039 if (keepstencil == GL_FALSE)
1040 /* get the 8-bit stencil values */
1041 _mesa_unpack_stencil_span(ctx, srcWidth,
1042 GL_UNSIGNED_BYTE, /* dst type */
1043 stencil, /* dst addr */
1044 srcType, src, srcPacking,
1045 ctx->_ImageTransferState);
1046
1047 for (i = 0; i < srcWidth; i++) {
1048 if (keepstencil)
1049 dstRow[i] = depth[i] << 8 | (dstRow[i] & 0x000000FF);
1050 else
1051 dstRow[i] = (dstRow[i] & 0xFFFFFF00) | (stencil[i] & 0xFF);
1052 }
1053 src += srcRowStride;
1054 dstRow += dstRowStride / sizeof(GLuint);
1055 }
1056 }
1057
1058 free(depth);
1059 free(stencil);
1060 return GL_TRUE;
1061 }
1062
1063
1064 /**
1065 * Store a combined depth/stencil texture image.
1066 */
1067 static GLboolean
1068 _mesa_texstore_s8_z24(TEXSTORE_PARAMS)
1069 {
1070 const GLuint depthScale = 0xffffff;
1071 const GLint srcRowStride
1072 = _mesa_image_row_stride(srcPacking, srcWidth, srcFormat, srcType);
1073 GLint img, row;
1074 GLuint *depth;
1075 GLubyte *stencil;
1076
1077 ASSERT(dstFormat == MESA_FORMAT_Z24_UNORM_S8_UINT);
1078 ASSERT(srcFormat == GL_DEPTH_STENCIL_EXT ||
1079 srcFormat == GL_DEPTH_COMPONENT ||
1080 srcFormat == GL_STENCIL_INDEX);
1081 ASSERT(srcFormat != GL_DEPTH_STENCIL_EXT ||
1082 srcType == GL_UNSIGNED_INT_24_8_EXT ||
1083 srcType == GL_FLOAT_32_UNSIGNED_INT_24_8_REV);
1084
1085 depth = malloc(srcWidth * sizeof(GLuint));
1086 stencil = malloc(srcWidth * sizeof(GLubyte));
1087
1088 if (!depth || !stencil) {
1089 free(depth);
1090 free(stencil);
1091 return GL_FALSE;
1092 }
1093
1094 for (img = 0; img < srcDepth; img++) {
1095 GLuint *dstRow = (GLuint *) dstSlices[img];
1096 const GLubyte *src
1097 = (const GLubyte *) _mesa_image_address(dims, srcPacking, srcAddr,
1098 srcWidth, srcHeight,
1099 srcFormat, srcType,
1100 img, 0, 0);
1101 for (row = 0; row < srcHeight; row++) {
1102 GLint i;
1103 GLboolean keepdepth = GL_FALSE, keepstencil = GL_FALSE;
1104
1105 if (srcFormat == GL_DEPTH_COMPONENT) { /* preserve stencil */
1106 keepstencil = GL_TRUE;
1107 }
1108 else if (srcFormat == GL_STENCIL_INDEX) { /* preserve depth */
1109 keepdepth = GL_TRUE;
1110 }
1111
1112 if (keepdepth == GL_FALSE)
1113 /* the 24 depth bits will be in the low position: */
1114 _mesa_unpack_depth_span(ctx, srcWidth,
1115 GL_UNSIGNED_INT, /* dst type */
1116 keepstencil ? depth : dstRow, /* dst addr */
1117 depthScale,
1118 srcType, src, srcPacking);
1119
1120 if (keepstencil == GL_FALSE)
1121 /* get the 8-bit stencil values */
1122 _mesa_unpack_stencil_span(ctx, srcWidth,
1123 GL_UNSIGNED_BYTE, /* dst type */
1124 stencil, /* dst addr */
1125 srcType, src, srcPacking,
1126 ctx->_ImageTransferState);
1127
1128 /* merge stencil values into depth values */
1129 for (i = 0; i < srcWidth; i++) {
1130 if (keepstencil)
1131 dstRow[i] = depth[i] | (dstRow[i] & 0xFF000000);
1132 else
1133 dstRow[i] = (dstRow[i] & 0xFFFFFF) | (stencil[i] << 24);
1134
1135 }
1136 src += srcRowStride;
1137 dstRow += dstRowStride / sizeof(GLuint);
1138 }
1139 }
1140
1141 free(depth);
1142 free(stencil);
1143
1144 return GL_TRUE;
1145 }
1146
1147
1148 /**
1149 * Store simple 8-bit/value stencil texture data.
1150 */
1151 static GLboolean
1152 _mesa_texstore_s8(TEXSTORE_PARAMS)
1153 {
1154 ASSERT(dstFormat == MESA_FORMAT_S_UINT8);
1155 ASSERT(srcFormat == GL_STENCIL_INDEX);
1156
1157 {
1158 const GLint srcRowStride
1159 = _mesa_image_row_stride(srcPacking, srcWidth, srcFormat, srcType);
1160 GLint img, row;
1161 GLubyte *stencil = malloc(srcWidth * sizeof(GLubyte));
1162
1163 if (!stencil)
1164 return GL_FALSE;
1165
1166 for (img = 0; img < srcDepth; img++) {
1167 GLubyte *dstRow = dstSlices[img];
1168 const GLubyte *src
1169 = (const GLubyte *) _mesa_image_address(dims, srcPacking, srcAddr,
1170 srcWidth, srcHeight,
1171 srcFormat, srcType,
1172 img, 0, 0);
1173 for (row = 0; row < srcHeight; row++) {
1174 GLint i;
1175
1176 /* get the 8-bit stencil values */
1177 _mesa_unpack_stencil_span(ctx, srcWidth,
1178 GL_UNSIGNED_BYTE, /* dst type */
1179 stencil, /* dst addr */
1180 srcType, src, srcPacking,
1181 ctx->_ImageTransferState);
1182 /* merge stencil values into depth values */
1183 for (i = 0; i < srcWidth; i++)
1184 dstRow[i] = stencil[i];
1185
1186 src += srcRowStride;
1187 dstRow += dstRowStride / sizeof(GLubyte);
1188 }
1189 }
1190
1191 free(stencil);
1192 }
1193
1194 return GL_TRUE;
1195 }
1196
1197
1198 static GLboolean
1199 _mesa_texstore_z32f_x24s8(TEXSTORE_PARAMS)
1200 {
1201 GLint img, row;
1202 const GLint srcRowStride
1203 = _mesa_image_row_stride(srcPacking, srcWidth, srcFormat, srcType)
1204 / sizeof(uint64_t);
1205
1206 ASSERT(dstFormat == MESA_FORMAT_Z32_FLOAT_S8X24_UINT);
1207 ASSERT(srcFormat == GL_DEPTH_STENCIL ||
1208 srcFormat == GL_DEPTH_COMPONENT ||
1209 srcFormat == GL_STENCIL_INDEX);
1210 ASSERT(srcFormat != GL_DEPTH_STENCIL ||
1211 srcType == GL_UNSIGNED_INT_24_8 ||
1212 srcType == GL_FLOAT_32_UNSIGNED_INT_24_8_REV);
1213
1214 /* In case we only upload depth we need to preserve the stencil */
1215 for (img = 0; img < srcDepth; img++) {
1216 uint64_t *dstRow = (uint64_t *) dstSlices[img];
1217 const uint64_t *src
1218 = (const uint64_t *) _mesa_image_address(dims, srcPacking, srcAddr,
1219 srcWidth, srcHeight,
1220 srcFormat, srcType,
1221 img, 0, 0);
1222 for (row = 0; row < srcHeight; row++) {
1223 /* The unpack functions with:
1224 * dstType = GL_FLOAT_32_UNSIGNED_INT_24_8_REV
1225 * only write their own dword, so the other dword (stencil
1226 * or depth) is preserved. */
1227 if (srcFormat != GL_STENCIL_INDEX)
1228 _mesa_unpack_depth_span(ctx, srcWidth,
1229 GL_FLOAT_32_UNSIGNED_INT_24_8_REV, /* dst type */
1230 dstRow, /* dst addr */
1231 ~0U, srcType, src, srcPacking);
1232
1233 if (srcFormat != GL_DEPTH_COMPONENT)
1234 _mesa_unpack_stencil_span(ctx, srcWidth,
1235 GL_FLOAT_32_UNSIGNED_INT_24_8_REV, /* dst type */
1236 dstRow, /* dst addr */
1237 srcType, src, srcPacking,
1238 ctx->_ImageTransferState);
1239
1240 src += srcRowStride;
1241 dstRow += dstRowStride / sizeof(uint64_t);
1242 }
1243 }
1244 return GL_TRUE;
1245 }
1246
1247 static GLboolean
1248 _mesa_texstore_argb2101010_uint(TEXSTORE_PARAMS)
1249 {
1250 const GLenum baseFormat = _mesa_get_format_base_format(dstFormat);
1251
1252 ASSERT(dstFormat == MESA_FORMAT_B10G10R10A2_UINT);
1253 ASSERT(_mesa_get_format_bytes(dstFormat) == 4);
1254
1255 {
1256 /* general path */
1257 const GLuint *tempImage = make_temp_uint_image(ctx, dims,
1258 baseInternalFormat,
1259 baseFormat,
1260 srcWidth, srcHeight,
1261 srcDepth, srcFormat,
1262 srcType, srcAddr,
1263 srcPacking);
1264 const GLuint *src = tempImage;
1265 GLint img, row, col;
1266 GLboolean is_unsigned = _mesa_is_type_unsigned(srcType);
1267 if (!tempImage)
1268 return GL_FALSE;
1269 for (img = 0; img < srcDepth; img++) {
1270 GLubyte *dstRow = dstSlices[img];
1271
1272 for (row = 0; row < srcHeight; row++) {
1273 GLuint *dstUI = (GLuint *) dstRow;
1274 if (is_unsigned) {
1275 for (col = 0; col < srcWidth; col++) {
1276 GLushort a,r,g,b;
1277 r = MIN2(src[RCOMP], 0x3ff);
1278 g = MIN2(src[GCOMP], 0x3ff);
1279 b = MIN2(src[BCOMP], 0x3ff);
1280 a = MIN2(src[ACOMP], 0x003);
1281 dstUI[col] = (a << 30) | (r << 20) | (g << 10) | (b);
1282 src += 4;
1283 }
1284 } else {
1285 for (col = 0; col < srcWidth; col++) {
1286 GLushort a,r,g,b;
1287 r = CLAMP((GLint) src[RCOMP], 0, 0x3ff);
1288 g = CLAMP((GLint) src[GCOMP], 0, 0x3ff);
1289 b = CLAMP((GLint) src[BCOMP], 0, 0x3ff);
1290 a = CLAMP((GLint) src[ACOMP], 0, 0x003);
1291 dstUI[col] = (a << 30) | (r << 20) | (g << 10) | (b);
1292 src += 4;
1293 }
1294 }
1295 dstRow += dstRowStride;
1296 }
1297 }
1298 free((void *) tempImage);
1299 }
1300 return GL_TRUE;
1301 }
1302
1303 static GLboolean
1304 _mesa_texstore_abgr2101010_uint(TEXSTORE_PARAMS)
1305 {
1306 const GLenum baseFormat = _mesa_get_format_base_format(dstFormat);
1307
1308 ASSERT(dstFormat == MESA_FORMAT_R10G10B10A2_UINT);
1309 ASSERT(_mesa_get_format_bytes(dstFormat) == 4);
1310
1311 {
1312 /* general path */
1313 const GLuint *tempImage = make_temp_uint_image(ctx, dims,
1314 baseInternalFormat,
1315 baseFormat,
1316 srcWidth, srcHeight,
1317 srcDepth, srcFormat,
1318 srcType, srcAddr,
1319 srcPacking);
1320 const GLuint *src = tempImage;
1321 GLint img, row, col;
1322 GLboolean is_unsigned = _mesa_is_type_unsigned(srcType);
1323 if (!tempImage)
1324 return GL_FALSE;
1325 for (img = 0; img < srcDepth; img++) {
1326 GLubyte *dstRow = dstSlices[img];
1327
1328 for (row = 0; row < srcHeight; row++) {
1329 GLuint *dstUI = (GLuint *) dstRow;
1330 if (is_unsigned) {
1331 for (col = 0; col < srcWidth; col++) {
1332 GLushort a,r,g,b;
1333 r = MIN2(src[RCOMP], 0x3ff);
1334 g = MIN2(src[GCOMP], 0x3ff);
1335 b = MIN2(src[BCOMP], 0x3ff);
1336 a = MIN2(src[ACOMP], 0x003);
1337 dstUI[col] = (a << 30) | (b << 20) | (g << 10) | (r);
1338 src += 4;
1339 }
1340 } else {
1341 for (col = 0; col < srcWidth; col++) {
1342 GLushort a,r,g,b;
1343 r = CLAMP((GLint) src[RCOMP], 0, 0x3ff);
1344 g = CLAMP((GLint) src[GCOMP], 0, 0x3ff);
1345 b = CLAMP((GLint) src[BCOMP], 0, 0x3ff);
1346 a = CLAMP((GLint) src[ACOMP], 0, 0x003);
1347 dstUI[col] = (a << 30) | (b << 20) | (g << 10) | (r);
1348 src += 4;
1349 }
1350 }
1351 dstRow += dstRowStride;
1352 }
1353 }
1354 free((void *) tempImage);
1355 }
1356 return GL_TRUE;
1357 }
1358
1359
1360 static GLboolean
1361 texstore_depth_stencil(TEXSTORE_PARAMS)
1362 {
1363 static StoreTexImageFunc table[MESA_FORMAT_COUNT];
1364 static GLboolean initialized = GL_FALSE;
1365
1366 if (!initialized) {
1367 memset(table, 0, sizeof table);
1368
1369 table[MESA_FORMAT_S8_UINT_Z24_UNORM] = _mesa_texstore_z24_s8;
1370 table[MESA_FORMAT_Z24_UNORM_S8_UINT] = _mesa_texstore_s8_z24;
1371 table[MESA_FORMAT_Z_UNORM16] = _mesa_texstore_z16;
1372 table[MESA_FORMAT_Z24_UNORM_X8_UINT] = _mesa_texstore_x8_z24;
1373 table[MESA_FORMAT_X8_UINT_Z24_UNORM] = _mesa_texstore_z24_x8;
1374 table[MESA_FORMAT_Z_UNORM32] = _mesa_texstore_z32;
1375 table[MESA_FORMAT_S_UINT8] = _mesa_texstore_s8;
1376 table[MESA_FORMAT_Z_FLOAT32] = _mesa_texstore_z32;
1377 table[MESA_FORMAT_Z32_FLOAT_S8X24_UINT] = _mesa_texstore_z32f_x24s8;
1378
1379 initialized = GL_TRUE;
1380 }
1381
1382 ASSERT(table[dstFormat]);
1383 return table[dstFormat](ctx, dims, baseInternalFormat,
1384 dstFormat, dstRowStride, dstSlices,
1385 srcWidth, srcHeight, srcDepth,
1386 srcFormat, srcType, srcAddr, srcPacking);
1387 }
1388
1389 static GLboolean
1390 texstore_compressed(TEXSTORE_PARAMS)
1391 {
1392 static StoreTexImageFunc table[MESA_FORMAT_COUNT];
1393 static GLboolean initialized = GL_FALSE;
1394
1395 if (!initialized) {
1396 memset(table, 0, sizeof table);
1397
1398 table[MESA_FORMAT_SRGB_DXT1] = _mesa_texstore_rgb_dxt1;
1399 table[MESA_FORMAT_SRGBA_DXT1] = _mesa_texstore_rgba_dxt1;
1400 table[MESA_FORMAT_SRGBA_DXT3] = _mesa_texstore_rgba_dxt3;
1401 table[MESA_FORMAT_SRGBA_DXT5] = _mesa_texstore_rgba_dxt5;
1402 table[MESA_FORMAT_RGB_FXT1] = _mesa_texstore_rgb_fxt1;
1403 table[MESA_FORMAT_RGBA_FXT1] = _mesa_texstore_rgba_fxt1;
1404 table[MESA_FORMAT_RGB_DXT1] = _mesa_texstore_rgb_dxt1;
1405 table[MESA_FORMAT_RGBA_DXT1] = _mesa_texstore_rgba_dxt1;
1406 table[MESA_FORMAT_RGBA_DXT3] = _mesa_texstore_rgba_dxt3;
1407 table[MESA_FORMAT_RGBA_DXT5] = _mesa_texstore_rgba_dxt5;
1408 table[MESA_FORMAT_R_RGTC1_UNORM] = _mesa_texstore_red_rgtc1;
1409 table[MESA_FORMAT_R_RGTC1_SNORM] = _mesa_texstore_signed_red_rgtc1;
1410 table[MESA_FORMAT_RG_RGTC2_UNORM] = _mesa_texstore_rg_rgtc2;
1411 table[MESA_FORMAT_RG_RGTC2_SNORM] = _mesa_texstore_signed_rg_rgtc2;
1412 table[MESA_FORMAT_L_LATC1_UNORM] = _mesa_texstore_red_rgtc1;
1413 table[MESA_FORMAT_L_LATC1_SNORM] = _mesa_texstore_signed_red_rgtc1;
1414 table[MESA_FORMAT_LA_LATC2_UNORM] = _mesa_texstore_rg_rgtc2;
1415 table[MESA_FORMAT_LA_LATC2_SNORM] = _mesa_texstore_signed_rg_rgtc2;
1416 table[MESA_FORMAT_ETC1_RGB8] = _mesa_texstore_etc1_rgb8;
1417 table[MESA_FORMAT_ETC2_RGB8] = _mesa_texstore_etc2_rgb8;
1418 table[MESA_FORMAT_ETC2_SRGB8] = _mesa_texstore_etc2_srgb8;
1419 table[MESA_FORMAT_ETC2_RGBA8_EAC] = _mesa_texstore_etc2_rgba8_eac;
1420 table[MESA_FORMAT_ETC2_SRGB8_ALPHA8_EAC] = _mesa_texstore_etc2_srgb8_alpha8_eac;
1421 table[MESA_FORMAT_ETC2_R11_EAC] = _mesa_texstore_etc2_r11_eac;
1422 table[MESA_FORMAT_ETC2_RG11_EAC] = _mesa_texstore_etc2_rg11_eac;
1423 table[MESA_FORMAT_ETC2_SIGNED_R11_EAC] = _mesa_texstore_etc2_signed_r11_eac;
1424 table[MESA_FORMAT_ETC2_SIGNED_RG11_EAC] = _mesa_texstore_etc2_signed_rg11_eac;
1425 table[MESA_FORMAT_ETC2_RGB8_PUNCHTHROUGH_ALPHA1] =
1426 _mesa_texstore_etc2_rgb8_punchthrough_alpha1;
1427 table[MESA_FORMAT_ETC2_SRGB8_PUNCHTHROUGH_ALPHA1] =
1428 _mesa_texstore_etc2_srgb8_punchthrough_alpha1;
1429
1430 table[MESA_FORMAT_BPTC_RGBA_UNORM] =
1431 _mesa_texstore_bptc_rgba_unorm;
1432 table[MESA_FORMAT_BPTC_SRGB_ALPHA_UNORM] =
1433 _mesa_texstore_bptc_rgba_unorm;
1434 table[MESA_FORMAT_BPTC_RGB_SIGNED_FLOAT] =
1435 _mesa_texstore_bptc_rgb_signed_float;
1436 table[MESA_FORMAT_BPTC_RGB_UNSIGNED_FLOAT] =
1437 _mesa_texstore_bptc_rgb_unsigned_float;
1438
1439 initialized = GL_TRUE;
1440 }
1441
1442 ASSERT(table[dstFormat]);
1443 return table[dstFormat](ctx, dims, baseInternalFormat,
1444 dstFormat, dstRowStride, dstSlices,
1445 srcWidth, srcHeight, srcDepth,
1446 srcFormat, srcType, srcAddr, srcPacking);
1447 }
1448
1449 static void
1450 invert_swizzle(uint8_t dst[4], const uint8_t src[4])
1451 {
1452 int i, j;
1453
1454 dst[0] = MESA_FORMAT_SWIZZLE_NONE;
1455 dst[1] = MESA_FORMAT_SWIZZLE_NONE;
1456 dst[2] = MESA_FORMAT_SWIZZLE_NONE;
1457 dst[3] = MESA_FORMAT_SWIZZLE_NONE;
1458
1459 for (i = 0; i < 4; ++i)
1460 for (j = 0; j < 4; ++j)
1461 if (src[j] == i && dst[i] == MESA_FORMAT_SWIZZLE_NONE)
1462 dst[i] = j;
1463 }
1464
1465 /** Store a texture by per-channel conversions and swizzling.
1466 *
1467 * This function attempts to perform a texstore operation by doing simple
1468 * per-channel conversions and swizzling. This covers a huge chunk of the
1469 * texture storage operations that anyone cares about. If this function is
1470 * incapable of performing the operation, it bails and returns GL_FALSE.
1471 */
1472 static GLboolean
1473 texstore_swizzle(TEXSTORE_PARAMS)
1474 {
1475 const GLint srcRowStride = _mesa_image_row_stride(srcPacking, srcWidth,
1476 srcFormat, srcType);
1477 const GLint srcImageStride = _mesa_image_image_stride(srcPacking,
1478 srcWidth, srcHeight, srcFormat, srcType);
1479 const GLubyte *srcImage = (const GLubyte *) _mesa_image_address(dims,
1480 srcPacking, srcAddr, srcWidth, srcHeight, srcFormat, srcType, 0, 0, 0);
1481 const int src_components = _mesa_components_in_format(srcFormat);
1482
1483 GLubyte swizzle[4], rgba2base[6], base2src[6], rgba2dst[4], dst2rgba[4];
1484 const GLubyte *swap;
1485 GLenum dst_type;
1486 int dst_components;
1487 bool is_array, normalized, need_swap;
1488 GLint i, img, row;
1489 const GLubyte *src_row;
1490 GLubyte *dst_row;
1491
1492 is_array = _mesa_format_to_array(dstFormat, &dst_type, &dst_components,
1493 rgba2dst, &normalized);
1494
1495 if (!is_array)
1496 return GL_FALSE;
1497
1498 if (srcFormat == GL_COLOR_INDEX)
1499 return GL_FALSE;
1500
1501 switch (srcType) {
1502 case GL_FLOAT:
1503 case GL_UNSIGNED_BYTE:
1504 case GL_BYTE:
1505 case GL_UNSIGNED_SHORT:
1506 case GL_SHORT:
1507 case GL_UNSIGNED_INT:
1508 case GL_INT:
1509 /* If wa have to swap bytes in a multi-byte datatype, that means
1510 * we're not doing an array conversion anymore */
1511 if (srcPacking->SwapBytes)
1512 return GL_FALSE;
1513 need_swap = false;
1514 break;
1515 case GL_UNSIGNED_INT_8_8_8_8:
1516 need_swap = srcPacking->SwapBytes;
1517 if (_mesa_little_endian())
1518 need_swap = !need_swap;
1519 srcType = GL_UNSIGNED_BYTE;
1520 break;
1521 case GL_UNSIGNED_INT_8_8_8_8_REV:
1522 need_swap = srcPacking->SwapBytes;
1523 if (!_mesa_little_endian())
1524 need_swap = !need_swap;
1525 srcType = GL_UNSIGNED_BYTE;
1526 break;
1527 default:
1528 return GL_FALSE;
1529 }
1530 swap = need_swap ? map_3210 : map_identity;
1531
1532 compute_component_mapping(srcFormat, baseInternalFormat, base2src);
1533 compute_component_mapping(baseInternalFormat, GL_RGBA, rgba2base);
1534 invert_swizzle(dst2rgba, rgba2dst);
1535
1536 for (i = 0; i < 4; i++) {
1537 if (dst2rgba[i] == MESA_FORMAT_SWIZZLE_NONE)
1538 swizzle[i] = MESA_FORMAT_SWIZZLE_NONE;
1539 else
1540 swizzle[i] = swap[base2src[rgba2base[dst2rgba[i]]]];
1541 }
1542
1543 /* Is it normalized? */
1544 normalized |= !_mesa_is_enum_format_integer(srcFormat);
1545
1546 for (img = 0; img < srcDepth; img++) {
1547 if (dstRowStride == srcWidth * dst_components &&
1548 srcRowStride == srcWidth * src_components) {
1549 _mesa_swizzle_and_convert(dstSlices[img], dst_type, dst_components,
1550 srcImage, srcType, src_components,
1551 swizzle, normalized, srcWidth * srcHeight);
1552 } else {
1553 src_row = srcImage;
1554 dst_row = dstSlices[img];
1555 for (row = 0; row < srcHeight; row++) {
1556 _mesa_swizzle_and_convert(dst_row, dst_type, dst_components,
1557 src_row, srcType, src_components,
1558 swizzle, normalized, srcWidth);
1559 dst_row += dstRowStride;
1560 src_row += srcRowStride;
1561 }
1562 }
1563 srcImage += srcImageStride;
1564 }
1565
1566 return GL_TRUE;
1567 }
1568
1569
1570 /** Stores a texture by converting float and then to the texture format
1571 *
1572 * This function performs a texstore operation by converting to float,
1573 * applying pixel transfer ops, and then converting to the texture's
1574 * internal format using pixel store functions. This function will work
1575 * for any rgb or srgb textore format.
1576 */
1577 static GLboolean
1578 texstore_via_float(TEXSTORE_PARAMS)
1579 {
1580 GLuint i, img, row;
1581 const GLint src_stride =
1582 _mesa_image_row_stride(srcPacking, srcWidth, srcFormat, srcType);
1583 float *tmp_row;
1584 bool need_convert;
1585 uint8_t *src_row, *dst_row, map[4], rgba2base[6], base2rgba[6];
1586
1587 tmp_row = malloc(srcWidth * 4 * sizeof(*tmp_row));
1588 if (!tmp_row)
1589 return GL_FALSE;
1590
1591 /* The GL spec (4.0, compatibility profile) only specifies srgb
1592 * conversion as something that is done in the sampler during the
1593 * filtering process before the colors are handed to the shader.
1594 * Furthermore, the flowchart (Figure 3.7 in the 4.0 compatibility spec)
1595 * does not list RGB <-> sRGB conversions anywhere. Therefore, we just
1596 * treat sRGB formats the same as RGB formats for the purposes of
1597 * texture upload and transfer ops.
1598 */
1599 dstFormat = _mesa_get_srgb_format_linear(dstFormat);
1600
1601 need_convert = false;
1602 if (baseInternalFormat != _mesa_get_format_base_format(dstFormat)) {
1603 compute_component_mapping(GL_RGBA, baseInternalFormat, base2rgba);
1604 compute_component_mapping(baseInternalFormat, GL_RGBA, rgba2base);
1605 for (i = 0; i < 4; ++i) {
1606 map[i] = base2rgba[rgba2base[i]];
1607 if (map[i] != i)
1608 need_convert = true;
1609 }
1610 }
1611
1612 for (img = 0; img < srcDepth; img++) {
1613 dst_row = dstSlices[img];
1614 src_row = _mesa_image_address(dims, srcPacking, srcAddr,
1615 srcWidth, srcHeight,
1616 srcFormat, srcType,
1617 img, 0, 0);
1618 for (row = 0; row < srcHeight; row++) {
1619 _mesa_unpack_color_span_float(ctx, srcWidth, GL_RGBA, tmp_row,
1620 srcFormat, srcType, src_row,
1621 srcPacking, ctx->_ImageTransferState);
1622 if (need_convert)
1623 _mesa_swizzle_and_convert(tmp_row, GL_FLOAT, 4,
1624 tmp_row, GL_FLOAT, 4,
1625 map, false, srcWidth);
1626 _mesa_pack_float_rgba_row(dstFormat, srcWidth,
1627 (const GLfloat (*)[4])tmp_row,
1628 dst_row);
1629 dst_row += dstRowStride;
1630 src_row += src_stride;
1631 }
1632 }
1633
1634 return GL_TRUE;
1635 }
1636
1637 /** Stores an integer rgba texture
1638 *
1639 * This function performs an integer texture storage operation by unpacking
1640 * the texture to 32-bit integers, and repacking it into the internal
1641 * format of the texture. This will work for any integer rgb texture
1642 * storage operation.
1643 */
1644 static GLboolean
1645 texstore_rgba_integer(TEXSTORE_PARAMS)
1646 {
1647 GLuint i, img, row, *tmp_row;
1648 GLenum dst_type, tmp_type;
1649 const GLint src_stride =
1650 _mesa_image_row_stride(srcPacking, srcWidth, srcFormat, srcType);
1651 int num_dst_components;
1652 bool is_array, normalized;
1653 uint8_t *src_row, *dst_row;
1654 uint8_t swizzle[4], rgba2base[6], base2rgba[6], rgba2dst[4], dst2rgba[4];
1655
1656 tmp_row = malloc(srcWidth * 4 * sizeof(*tmp_row));
1657 if (!tmp_row)
1658 return GL_FALSE;
1659
1660 is_array = _mesa_format_to_array(dstFormat, &dst_type, &num_dst_components,
1661 rgba2dst, &normalized);
1662
1663 assert(is_array && !normalized);
1664
1665 if (!is_array)
1666 return GL_FALSE;
1667
1668 invert_swizzle(dst2rgba, rgba2dst);
1669 compute_component_mapping(GL_RGBA, baseInternalFormat, base2rgba);
1670 compute_component_mapping(baseInternalFormat, GL_RGBA, rgba2base);
1671
1672 for (i = 0; i < 4; ++i) {
1673 if (dst2rgba[i] == MESA_FORMAT_SWIZZLE_NONE)
1674 swizzle[i] = MESA_FORMAT_SWIZZLE_NONE;
1675 else
1676 swizzle[i] = base2rgba[rgba2base[dst2rgba[i]]];
1677 }
1678
1679 if (_mesa_is_type_unsigned(srcType)) {
1680 tmp_type = GL_UNSIGNED_INT;
1681 } else {
1682 tmp_type = GL_INT;
1683 }
1684
1685 for (img = 0; img < srcDepth; img++) {
1686 dst_row = dstSlices[img];
1687 src_row = _mesa_image_address(dims, srcPacking, srcAddr,
1688 srcWidth, srcHeight,
1689 srcFormat, srcType,
1690 img, 0, 0);
1691 for (row = 0; row < srcHeight; row++) {
1692 _mesa_unpack_color_span_uint(ctx, srcWidth, GL_RGBA, tmp_row,
1693 srcFormat, srcType, src_row, srcPacking);
1694 _mesa_swizzle_and_convert(dst_row, dst_type, num_dst_components,
1695 tmp_row, tmp_type, 4,
1696 swizzle, false, srcWidth);
1697 dst_row += dstRowStride;
1698 src_row += src_stride;
1699 }
1700 }
1701
1702 return GL_TRUE;
1703 }
1704
1705 static GLboolean
1706 texstore_rgba(TEXSTORE_PARAMS)
1707 {
1708 static StoreTexImageFunc table[MESA_FORMAT_COUNT];
1709 static GLboolean initialized = GL_FALSE;
1710
1711 if (!initialized) {
1712 memset(table, 0, sizeof table);
1713
1714 table[MESA_FORMAT_B5G6R5_UNORM] = _mesa_texstore_rgb565;
1715 table[MESA_FORMAT_R5G6B5_UNORM] = _mesa_texstore_rgb565;
1716 table[MESA_FORMAT_YCBCR] = _mesa_texstore_ycbcr;
1717 table[MESA_FORMAT_YCBCR_REV] = _mesa_texstore_ycbcr;
1718
1719 table[MESA_FORMAT_B10G10R10A2_UINT] = _mesa_texstore_argb2101010_uint;
1720 table[MESA_FORMAT_R10G10B10A2_UINT] = _mesa_texstore_abgr2101010_uint;
1721
1722 initialized = GL_TRUE;
1723 }
1724
1725 if (table[dstFormat] && table[dstFormat](ctx, dims, baseInternalFormat,
1726 dstFormat, dstRowStride, dstSlices,
1727 srcWidth, srcHeight, srcDepth,
1728 srcFormat, srcType, srcAddr,
1729 srcPacking)) {
1730 return GL_TRUE;
1731 }
1732
1733 if (texstore_swizzle(ctx, dims, baseInternalFormat,
1734 dstFormat,
1735 dstRowStride, dstSlices,
1736 srcWidth, srcHeight, srcDepth,
1737 srcFormat, srcType, srcAddr, srcPacking)) {
1738 return GL_TRUE;
1739 }
1740
1741 if (_mesa_is_format_integer(dstFormat)) {
1742 return texstore_rgba_integer(ctx, dims, baseInternalFormat,
1743 dstFormat, dstRowStride, dstSlices,
1744 srcWidth, srcHeight, srcDepth,
1745 srcFormat, srcType, srcAddr,
1746 srcPacking);
1747 } else if (_mesa_get_format_max_bits(dstFormat) <= 8 &&
1748 !_mesa_is_format_signed(dstFormat)) {
1749 return store_ubyte_texture(ctx, dims, baseInternalFormat,
1750 dstFormat,
1751 dstRowStride, dstSlices,
1752 srcWidth, srcHeight, srcDepth,
1753 srcFormat, srcType, srcAddr, srcPacking);
1754 } else {
1755 return texstore_via_float(ctx, dims, baseInternalFormat,
1756 dstFormat, dstRowStride, dstSlices,
1757 srcWidth, srcHeight, srcDepth,
1758 srcFormat, srcType, srcAddr,
1759 srcPacking);
1760 }
1761 }
1762
1763 GLboolean
1764 _mesa_texstore_needs_transfer_ops(struct gl_context *ctx,
1765 GLenum baseInternalFormat,
1766 mesa_format dstFormat)
1767 {
1768 GLenum dstType;
1769
1770 /* There are different rules depending on the base format. */
1771 switch (baseInternalFormat) {
1772 case GL_DEPTH_COMPONENT:
1773 case GL_DEPTH_STENCIL:
1774 return ctx->Pixel.DepthScale != 1.0f ||
1775 ctx->Pixel.DepthBias != 0.0f;
1776
1777 case GL_STENCIL_INDEX:
1778 return GL_FALSE;
1779
1780 default:
1781 /* Color formats.
1782 * Pixel transfer ops (scale, bias, table lookup) do not apply
1783 * to integer formats.
1784 */
1785 dstType = _mesa_get_format_datatype(dstFormat);
1786
1787 return dstType != GL_INT && dstType != GL_UNSIGNED_INT &&
1788 ctx->_ImageTransferState;
1789 }
1790 }
1791
1792
1793 GLboolean
1794 _mesa_texstore_can_use_memcpy(struct gl_context *ctx,
1795 GLenum baseInternalFormat, mesa_format dstFormat,
1796 GLenum srcFormat, GLenum srcType,
1797 const struct gl_pixelstore_attrib *srcPacking)
1798 {
1799 if (_mesa_texstore_needs_transfer_ops(ctx, baseInternalFormat, dstFormat)) {
1800 return GL_FALSE;
1801 }
1802
1803 /* The base internal format and the base Mesa format must match. */
1804 if (baseInternalFormat != _mesa_get_format_base_format(dstFormat)) {
1805 return GL_FALSE;
1806 }
1807
1808 /* The Mesa format must match the input format and type. */
1809 if (!_mesa_format_matches_format_and_type(dstFormat, srcFormat, srcType,
1810 srcPacking->SwapBytes)) {
1811 return GL_FALSE;
1812 }
1813
1814 /* Depth texture data needs clamping in following cases:
1815 * - Floating point dstFormat with signed srcType: clamp to [0.0, 1.0].
1816 * - Fixed point dstFormat with signed srcType: clamp to [0, 2^n -1].
1817 *
1818 * All the cases except one (float dstFormat with float srcType) are ruled
1819 * out by _mesa_format_matches_format_and_type() check above. Handle the
1820 * remaining case here.
1821 */
1822 if ((baseInternalFormat == GL_DEPTH_COMPONENT ||
1823 baseInternalFormat == GL_DEPTH_STENCIL) &&
1824 (srcType == GL_FLOAT ||
1825 srcType == GL_FLOAT_32_UNSIGNED_INT_24_8_REV)) {
1826 return GL_FALSE;
1827 }
1828
1829 return GL_TRUE;
1830 }
1831
1832 static GLboolean
1833 _mesa_texstore_memcpy(TEXSTORE_PARAMS)
1834 {
1835 if (!_mesa_texstore_can_use_memcpy(ctx, baseInternalFormat, dstFormat,
1836 srcFormat, srcType, srcPacking)) {
1837 return GL_FALSE;
1838 }
1839
1840 memcpy_texture(ctx, dims,
1841 dstFormat,
1842 dstRowStride, dstSlices,
1843 srcWidth, srcHeight, srcDepth, srcFormat, srcType,
1844 srcAddr, srcPacking);
1845 return GL_TRUE;
1846 }
1847 /**
1848 * Store user data into texture memory.
1849 * Called via glTex[Sub]Image1/2/3D()
1850 * \return GL_TRUE for success, GL_FALSE for failure (out of memory).
1851 */
1852 GLboolean
1853 _mesa_texstore(TEXSTORE_PARAMS)
1854 {
1855 if (_mesa_texstore_memcpy(ctx, dims, baseInternalFormat,
1856 dstFormat,
1857 dstRowStride, dstSlices,
1858 srcWidth, srcHeight, srcDepth,
1859 srcFormat, srcType, srcAddr, srcPacking)) {
1860 return GL_TRUE;
1861 }
1862
1863 if (_mesa_is_depth_or_stencil_format(baseInternalFormat)) {
1864 return texstore_depth_stencil(ctx, dims, baseInternalFormat,
1865 dstFormat, dstRowStride, dstSlices,
1866 srcWidth, srcHeight, srcDepth,
1867 srcFormat, srcType, srcAddr, srcPacking);
1868 } else if (_mesa_is_format_compressed(dstFormat)) {
1869 return texstore_compressed(ctx, dims, baseInternalFormat,
1870 dstFormat, dstRowStride, dstSlices,
1871 srcWidth, srcHeight, srcDepth,
1872 srcFormat, srcType, srcAddr, srcPacking);
1873 } else {
1874 return texstore_rgba(ctx, dims, baseInternalFormat,
1875 dstFormat, dstRowStride, dstSlices,
1876 srcWidth, srcHeight, srcDepth,
1877 srcFormat, srcType, srcAddr, srcPacking);
1878 }
1879 }
1880
1881
1882 /**
1883 * Normally, we'll only _write_ texel data to a texture when we map it.
1884 * But if the user is providing depth or stencil values and the texture
1885 * image is a combined depth/stencil format, we'll actually read from
1886 * the texture buffer too (in order to insert the depth or stencil values.
1887 * \param userFormat the user-provided image format
1888 * \param texFormat the destination texture format
1889 */
1890 static GLbitfield
1891 get_read_write_mode(GLenum userFormat, mesa_format texFormat)
1892 {
1893 if ((userFormat == GL_STENCIL_INDEX || userFormat == GL_DEPTH_COMPONENT)
1894 && _mesa_get_format_base_format(texFormat) == GL_DEPTH_STENCIL)
1895 return GL_MAP_READ_BIT | GL_MAP_WRITE_BIT;
1896 else
1897 return GL_MAP_WRITE_BIT | GL_MAP_INVALIDATE_RANGE_BIT;
1898 }
1899
1900
1901 /**
1902 * Helper function for storing 1D, 2D, 3D whole and subimages into texture
1903 * memory.
1904 * The source of the image data may be user memory or a PBO. In the later
1905 * case, we'll map the PBO, copy from it, then unmap it.
1906 */
1907 static void
1908 store_texsubimage(struct gl_context *ctx,
1909 struct gl_texture_image *texImage,
1910 GLint xoffset, GLint yoffset, GLint zoffset,
1911 GLint width, GLint height, GLint depth,
1912 GLenum format, GLenum type, const GLvoid *pixels,
1913 const struct gl_pixelstore_attrib *packing,
1914 const char *caller)
1915
1916 {
1917 const GLbitfield mapMode = get_read_write_mode(format, texImage->TexFormat);
1918 const GLenum target = texImage->TexObject->Target;
1919 GLboolean success = GL_FALSE;
1920 GLuint dims, slice, numSlices = 1, sliceOffset = 0;
1921 GLint srcImageStride = 0;
1922 const GLubyte *src;
1923
1924 assert(xoffset + width <= texImage->Width);
1925 assert(yoffset + height <= texImage->Height);
1926 assert(zoffset + depth <= texImage->Depth);
1927
1928 switch (target) {
1929 case GL_TEXTURE_1D:
1930 dims = 1;
1931 break;
1932 case GL_TEXTURE_2D_ARRAY:
1933 case GL_TEXTURE_CUBE_MAP_ARRAY:
1934 case GL_TEXTURE_3D:
1935 dims = 3;
1936 break;
1937 default:
1938 dims = 2;
1939 }
1940
1941 /* get pointer to src pixels (may be in a pbo which we'll map here) */
1942 src = (const GLubyte *)
1943 _mesa_validate_pbo_teximage(ctx, dims, width, height, depth,
1944 format, type, pixels, packing, caller);
1945 if (!src)
1946 return;
1947
1948 /* compute slice info (and do some sanity checks) */
1949 switch (target) {
1950 case GL_TEXTURE_2D:
1951 case GL_TEXTURE_RECTANGLE:
1952 case GL_TEXTURE_CUBE_MAP:
1953 case GL_TEXTURE_EXTERNAL_OES:
1954 /* one image slice, nothing special needs to be done */
1955 break;
1956 case GL_TEXTURE_1D:
1957 assert(height == 1);
1958 assert(depth == 1);
1959 assert(yoffset == 0);
1960 assert(zoffset == 0);
1961 break;
1962 case GL_TEXTURE_1D_ARRAY:
1963 assert(depth == 1);
1964 assert(zoffset == 0);
1965 numSlices = height;
1966 sliceOffset = yoffset;
1967 height = 1;
1968 yoffset = 0;
1969 srcImageStride = _mesa_image_row_stride(packing, width, format, type);
1970 break;
1971 case GL_TEXTURE_2D_ARRAY:
1972 numSlices = depth;
1973 sliceOffset = zoffset;
1974 depth = 1;
1975 zoffset = 0;
1976 srcImageStride = _mesa_image_image_stride(packing, width, height,
1977 format, type);
1978 break;
1979 case GL_TEXTURE_3D:
1980 /* we'll store 3D images as a series of slices */
1981 numSlices = depth;
1982 sliceOffset = zoffset;
1983 srcImageStride = _mesa_image_image_stride(packing, width, height,
1984 format, type);
1985 break;
1986 case GL_TEXTURE_CUBE_MAP_ARRAY:
1987 numSlices = depth;
1988 sliceOffset = zoffset;
1989 srcImageStride = _mesa_image_image_stride(packing, width, height,
1990 format, type);
1991 break;
1992 default:
1993 _mesa_warning(ctx, "Unexpected target 0x%x in store_texsubimage()", target);
1994 return;
1995 }
1996
1997 assert(numSlices == 1 || srcImageStride != 0);
1998
1999 for (slice = 0; slice < numSlices; slice++) {
2000 GLubyte *dstMap;
2001 GLint dstRowStride;
2002
2003 ctx->Driver.MapTextureImage(ctx, texImage,
2004 slice + sliceOffset,
2005 xoffset, yoffset, width, height,
2006 mapMode, &dstMap, &dstRowStride);
2007 if (dstMap) {
2008 /* Note: we're only storing a 2D (or 1D) slice at a time but we need
2009 * to pass the right 'dims' value so that GL_UNPACK_SKIP_IMAGES is
2010 * used for 3D images.
2011 */
2012 success = _mesa_texstore(ctx, dims, texImage->_BaseFormat,
2013 texImage->TexFormat,
2014 dstRowStride,
2015 &dstMap,
2016 width, height, 1, /* w, h, d */
2017 format, type, src, packing);
2018
2019 ctx->Driver.UnmapTextureImage(ctx, texImage, slice + sliceOffset);
2020 }
2021
2022 src += srcImageStride;
2023
2024 if (!success)
2025 break;
2026 }
2027
2028 if (!success)
2029 _mesa_error(ctx, GL_OUT_OF_MEMORY, "%s", caller);
2030
2031 _mesa_unmap_teximage_pbo(ctx, packing);
2032 }
2033
2034
2035
2036 /**
2037 * Fallback code for ctx->Driver.TexImage().
2038 * Basically, allocate storage for the texture image, then copy the
2039 * user's image into it.
2040 */
2041 void
2042 _mesa_store_teximage(struct gl_context *ctx,
2043 GLuint dims,
2044 struct gl_texture_image *texImage,
2045 GLenum format, GLenum type, const GLvoid *pixels,
2046 const struct gl_pixelstore_attrib *packing)
2047 {
2048 assert(dims == 1 || dims == 2 || dims == 3);
2049
2050 if (texImage->Width == 0 || texImage->Height == 0 || texImage->Depth == 0)
2051 return;
2052
2053 /* allocate storage for texture data */
2054 if (!ctx->Driver.AllocTextureImageBuffer(ctx, texImage)) {
2055 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexImage%uD", dims);
2056 return;
2057 }
2058
2059 store_texsubimage(ctx, texImage,
2060 0, 0, 0, texImage->Width, texImage->Height, texImage->Depth,
2061 format, type, pixels, packing, "glTexImage");
2062 }
2063
2064
2065 /*
2066 * Fallback for Driver.TexSubImage().
2067 */
2068 void
2069 _mesa_store_texsubimage(struct gl_context *ctx, GLuint dims,
2070 struct gl_texture_image *texImage,
2071 GLint xoffset, GLint yoffset, GLint zoffset,
2072 GLint width, GLint height, GLint depth,
2073 GLenum format, GLenum type, const void *pixels,
2074 const struct gl_pixelstore_attrib *packing)
2075 {
2076 store_texsubimage(ctx, texImage,
2077 xoffset, yoffset, zoffset, width, height, depth,
2078 format, type, pixels, packing, "glTexSubImage");
2079 }
2080
2081 static void
2082 clear_image_to_zero(GLubyte *dstMap, GLint dstRowStride,
2083 GLsizei width, GLsizei height,
2084 GLsizei clearValueSize)
2085 {
2086 GLsizei y;
2087
2088 for (y = 0; y < height; y++) {
2089 memset(dstMap, 0, clearValueSize * width);
2090 dstMap += dstRowStride;
2091 }
2092 }
2093
2094 static void
2095 clear_image_to_value(GLubyte *dstMap, GLint dstRowStride,
2096 GLsizei width, GLsizei height,
2097 const GLvoid *clearValue,
2098 GLsizei clearValueSize)
2099 {
2100 GLsizei y, x;
2101
2102 for (y = 0; y < height; y++) {
2103 for (x = 0; x < width; x++) {
2104 memcpy(dstMap, clearValue, clearValueSize);
2105 dstMap += clearValueSize;
2106 }
2107 dstMap += dstRowStride - clearValueSize * width;
2108 }
2109 }
2110
2111 /*
2112 * Fallback for Driver.ClearTexSubImage().
2113 */
2114 void
2115 _mesa_store_cleartexsubimage(struct gl_context *ctx,
2116 struct gl_texture_image *texImage,
2117 GLint xoffset, GLint yoffset, GLint zoffset,
2118 GLsizei width, GLsizei height, GLsizei depth,
2119 const GLvoid *clearValue)
2120 {
2121 GLubyte *dstMap;
2122 GLint dstRowStride;
2123 GLsizeiptr clearValueSize;
2124 GLsizei z;
2125
2126 clearValueSize = _mesa_get_format_bytes(texImage->TexFormat);
2127
2128 for (z = 0; z < depth; z++) {
2129 ctx->Driver.MapTextureImage(ctx, texImage,
2130 z + zoffset, xoffset, yoffset,
2131 width, height,
2132 GL_MAP_WRITE_BIT,
2133 &dstMap, &dstRowStride);
2134 if (dstMap == NULL) {
2135 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glClearTex*Image");
2136 return;
2137 }
2138
2139 if (clearValue) {
2140 clear_image_to_value(dstMap, dstRowStride,
2141 width, height,
2142 clearValue,
2143 clearValueSize);
2144 } else {
2145 clear_image_to_zero(dstMap, dstRowStride,
2146 width, height,
2147 clearValueSize);
2148 }
2149
2150 ctx->Driver.UnmapTextureImage(ctx, texImage, z + zoffset);
2151 }
2152 }
2153
2154 /**
2155 * Fallback for Driver.CompressedTexImage()
2156 */
2157 void
2158 _mesa_store_compressed_teximage(struct gl_context *ctx, GLuint dims,
2159 struct gl_texture_image *texImage,
2160 GLsizei imageSize, const GLvoid *data)
2161 {
2162 /* only 2D and 3D compressed images are supported at this time */
2163 if (dims == 1) {
2164 _mesa_problem(ctx, "Unexpected glCompressedTexImage1D call");
2165 return;
2166 }
2167
2168 /* This is pretty simple, because unlike the general texstore path we don't
2169 * have to worry about the usual image unpacking or image transfer
2170 * operations.
2171 */
2172 ASSERT(texImage);
2173 ASSERT(texImage->Width > 0);
2174 ASSERT(texImage->Height > 0);
2175 ASSERT(texImage->Depth > 0);
2176
2177 /* allocate storage for texture data */
2178 if (!ctx->Driver.AllocTextureImageBuffer(ctx, texImage)) {
2179 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCompressedTexImage%uD", dims);
2180 return;
2181 }
2182
2183 _mesa_store_compressed_texsubimage(ctx, dims, texImage,
2184 0, 0, 0,
2185 texImage->Width, texImage->Height, texImage->Depth,
2186 texImage->TexFormat,
2187 imageSize, data);
2188 }
2189
2190
2191 void
2192 _mesa_compute_compressed_pixelstore(GLuint dims, struct gl_texture_image *texImage,
2193 GLsizei width, GLsizei height, GLsizei depth,
2194 const struct gl_pixelstore_attrib *packing,
2195 struct compressed_pixelstore *store)
2196 {
2197 GLuint bw, bh;
2198 const mesa_format texFormat = texImage->TexFormat;
2199
2200 _mesa_get_format_block_size(texFormat, &bw, &bh);
2201
2202 store->SkipBytes = 0;
2203 store->TotalBytesPerRow = store->CopyBytesPerRow =
2204 _mesa_format_row_stride(texFormat, width);
2205 store->TotalRowsPerSlice = store->CopyRowsPerSlice =
2206 (height + bh - 1) / bh;
2207 store->CopySlices = depth;
2208
2209 if (packing->CompressedBlockWidth &&
2210 packing->CompressedBlockSize) {
2211
2212 bw = packing->CompressedBlockWidth;
2213
2214 if (packing->RowLength) {
2215 store->TotalBytesPerRow = packing->CompressedBlockSize *
2216 (packing->RowLength + bw - 1) / bw;
2217 }
2218
2219 store->SkipBytes += packing->SkipPixels * packing->CompressedBlockSize / bw;
2220 }
2221
2222 if (dims > 1 && packing->CompressedBlockHeight &&
2223 packing->CompressedBlockSize) {
2224
2225 bh = packing->CompressedBlockHeight;
2226
2227 store->SkipBytes += packing->SkipRows * store->TotalBytesPerRow / bh;
2228 store->CopyRowsPerSlice = (height + bh - 1) / bh; /* rows in blocks */
2229
2230 if (packing->ImageHeight) {
2231 store->TotalRowsPerSlice = (packing->ImageHeight + bh - 1) / bh;
2232 }
2233 }
2234
2235 if (dims > 2 && packing->CompressedBlockDepth &&
2236 packing->CompressedBlockSize) {
2237
2238 int bd = packing->CompressedBlockDepth;
2239
2240 store->SkipBytes += packing->SkipImages * store->TotalBytesPerRow *
2241 store->TotalRowsPerSlice / bd;
2242 }
2243 }
2244
2245
2246 /**
2247 * Fallback for Driver.CompressedTexSubImage()
2248 */
2249 void
2250 _mesa_store_compressed_texsubimage(struct gl_context *ctx, GLuint dims,
2251 struct gl_texture_image *texImage,
2252 GLint xoffset, GLint yoffset, GLint zoffset,
2253 GLsizei width, GLsizei height, GLsizei depth,
2254 GLenum format,
2255 GLsizei imageSize, const GLvoid *data)
2256 {
2257 struct compressed_pixelstore store;
2258 GLint dstRowStride;
2259 GLint i, slice;
2260 GLubyte *dstMap;
2261 const GLubyte *src;
2262
2263 if (dims == 1) {
2264 _mesa_problem(ctx, "Unexpected 1D compressed texsubimage call");
2265 return;
2266 }
2267
2268 _mesa_compute_compressed_pixelstore(dims, texImage, width, height, depth,
2269 &ctx->Unpack, &store);
2270
2271 /* get pointer to src pixels (may be in a pbo which we'll map here) */
2272 data = _mesa_validate_pbo_compressed_teximage(ctx, dims, imageSize, data,
2273 &ctx->Unpack,
2274 "glCompressedTexSubImage");
2275 if (!data)
2276 return;
2277
2278 src = (const GLubyte *) data + store.SkipBytes;
2279
2280 for (slice = 0; slice < store.CopySlices; slice++) {
2281 /* Map dest texture buffer */
2282 ctx->Driver.MapTextureImage(ctx, texImage, slice + zoffset,
2283 xoffset, yoffset, width, height,
2284 GL_MAP_WRITE_BIT | GL_MAP_INVALIDATE_RANGE_BIT,
2285 &dstMap, &dstRowStride);
2286
2287 if (dstMap) {
2288
2289 /* copy rows of blocks */
2290 for (i = 0; i < store.CopyRowsPerSlice; i++) {
2291 memcpy(dstMap, src, store.CopyBytesPerRow);
2292 dstMap += dstRowStride;
2293 src += store.TotalBytesPerRow;
2294 }
2295
2296 ctx->Driver.UnmapTextureImage(ctx, texImage, slice + zoffset);
2297
2298 /* advance to next slice */
2299 src += store.TotalBytesPerRow * (store.TotalRowsPerSlice - store.CopyRowsPerSlice);
2300 }
2301 else {
2302 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCompressedTexSubImage%uD",
2303 dims);
2304 }
2305 }
2306
2307 _mesa_unmap_teximage_pbo(ctx, &ctx->Unpack);
2308 }