b9407d2f739d6574a7d426c356637b0ebc48ea93
[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( srcUB[2], srcUB[1], srcUB[0] );
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 if (_mesa_texstore_needs_transfer_ops(ctx, baseInternalFormat, dstFormat))
1502 return GL_FALSE;
1503
1504 switch (srcType) {
1505 case GL_FLOAT:
1506 case GL_UNSIGNED_BYTE:
1507 case GL_BYTE:
1508 case GL_UNSIGNED_SHORT:
1509 case GL_SHORT:
1510 case GL_UNSIGNED_INT:
1511 case GL_INT:
1512 /* If wa have to swap bytes in a multi-byte datatype, that means
1513 * we're not doing an array conversion anymore */
1514 if (srcPacking->SwapBytes)
1515 return GL_FALSE;
1516 need_swap = false;
1517 break;
1518 case GL_UNSIGNED_INT_8_8_8_8:
1519 need_swap = srcPacking->SwapBytes;
1520 if (_mesa_little_endian())
1521 need_swap = !need_swap;
1522 srcType = GL_UNSIGNED_BYTE;
1523 break;
1524 case GL_UNSIGNED_INT_8_8_8_8_REV:
1525 need_swap = srcPacking->SwapBytes;
1526 if (!_mesa_little_endian())
1527 need_swap = !need_swap;
1528 srcType = GL_UNSIGNED_BYTE;
1529 break;
1530 default:
1531 return GL_FALSE;
1532 }
1533 swap = need_swap ? map_3210 : map_identity;
1534
1535 compute_component_mapping(srcFormat, baseInternalFormat, base2src);
1536 compute_component_mapping(baseInternalFormat, GL_RGBA, rgba2base);
1537 invert_swizzle(dst2rgba, rgba2dst);
1538
1539 for (i = 0; i < 4; i++) {
1540 if (dst2rgba[i] == MESA_FORMAT_SWIZZLE_NONE)
1541 swizzle[i] = MESA_FORMAT_SWIZZLE_NONE;
1542 else
1543 swizzle[i] = swap[base2src[rgba2base[dst2rgba[i]]]];
1544 }
1545
1546 /* Is it normalized? */
1547 normalized |= !_mesa_is_enum_format_integer(srcFormat);
1548
1549 for (img = 0; img < srcDepth; img++) {
1550 if (dstRowStride == srcWidth * dst_components &&
1551 srcRowStride == srcWidth * src_components) {
1552 _mesa_swizzle_and_convert(dstSlices[img], dst_type, dst_components,
1553 srcImage, srcType, src_components,
1554 swizzle, normalized, srcWidth * srcHeight);
1555 } else {
1556 src_row = srcImage;
1557 dst_row = dstSlices[img];
1558 for (row = 0; row < srcHeight; row++) {
1559 _mesa_swizzle_and_convert(dst_row, dst_type, dst_components,
1560 src_row, srcType, src_components,
1561 swizzle, normalized, srcWidth);
1562 dst_row += dstRowStride;
1563 src_row += srcRowStride;
1564 }
1565 }
1566 srcImage += srcImageStride;
1567 }
1568
1569 return GL_TRUE;
1570 }
1571
1572
1573 /** Stores a texture by converting float and then to the texture format
1574 *
1575 * This function performs a texstore operation by converting to float,
1576 * applying pixel transfer ops, and then converting to the texture's
1577 * internal format using pixel store functions. This function will work
1578 * for any rgb or srgb textore format.
1579 */
1580 static GLboolean
1581 texstore_via_float(TEXSTORE_PARAMS)
1582 {
1583 GLuint i, img, row;
1584 const GLint src_stride =
1585 _mesa_image_row_stride(srcPacking, srcWidth, srcFormat, srcType);
1586 float *tmp_row;
1587 bool need_convert;
1588 uint8_t *src_row, *dst_row, map[4], rgba2base[6], base2rgba[6];
1589
1590 tmp_row = malloc(srcWidth * 4 * sizeof(*tmp_row));
1591 if (!tmp_row)
1592 return GL_FALSE;
1593
1594 /* The GL spec (4.0, compatibility profile) only specifies srgb
1595 * conversion as something that is done in the sampler during the
1596 * filtering process before the colors are handed to the shader.
1597 * Furthermore, the flowchart (Figure 3.7 in the 4.0 compatibility spec)
1598 * does not list RGB <-> sRGB conversions anywhere. Therefore, we just
1599 * treat sRGB formats the same as RGB formats for the purposes of
1600 * texture upload and transfer ops.
1601 */
1602 dstFormat = _mesa_get_srgb_format_linear(dstFormat);
1603
1604 need_convert = false;
1605 if (baseInternalFormat != _mesa_get_format_base_format(dstFormat)) {
1606 compute_component_mapping(GL_RGBA, baseInternalFormat, base2rgba);
1607 compute_component_mapping(baseInternalFormat, GL_RGBA, rgba2base);
1608 for (i = 0; i < 4; ++i) {
1609 map[i] = base2rgba[rgba2base[i]];
1610 if (map[i] != i)
1611 need_convert = true;
1612 }
1613 }
1614
1615 for (img = 0; img < srcDepth; img++) {
1616 dst_row = dstSlices[img];
1617 src_row = _mesa_image_address(dims, srcPacking, srcAddr,
1618 srcWidth, srcHeight,
1619 srcFormat, srcType,
1620 img, 0, 0);
1621 for (row = 0; row < srcHeight; row++) {
1622 _mesa_unpack_color_span_float(ctx, srcWidth, GL_RGBA, tmp_row,
1623 srcFormat, srcType, src_row,
1624 srcPacking, ctx->_ImageTransferState);
1625 if (need_convert)
1626 _mesa_swizzle_and_convert(tmp_row, GL_FLOAT, 4,
1627 tmp_row, GL_FLOAT, 4,
1628 map, false, srcWidth);
1629 _mesa_pack_float_rgba_row(dstFormat, srcWidth,
1630 (const GLfloat (*)[4])tmp_row,
1631 dst_row);
1632 dst_row += dstRowStride;
1633 src_row += src_stride;
1634 }
1635 }
1636
1637 free(tmp_row);
1638
1639 return GL_TRUE;
1640 }
1641
1642 /** Stores an integer rgba texture
1643 *
1644 * This function performs an integer texture storage operation by unpacking
1645 * the texture to 32-bit integers, and repacking it into the internal
1646 * format of the texture. This will work for any integer rgb texture
1647 * storage operation.
1648 */
1649 static GLboolean
1650 texstore_rgba_integer(TEXSTORE_PARAMS)
1651 {
1652 GLuint i, img, row, *tmp_row;
1653 GLenum dst_type, tmp_type;
1654 const GLint src_stride =
1655 _mesa_image_row_stride(srcPacking, srcWidth, srcFormat, srcType);
1656 int num_dst_components;
1657 bool is_array, normalized;
1658 uint8_t *src_row, *dst_row;
1659 uint8_t swizzle[4], rgba2base[6], base2rgba[6], rgba2dst[4], dst2rgba[4];
1660
1661 tmp_row = malloc(srcWidth * 4 * sizeof(*tmp_row));
1662 if (!tmp_row)
1663 return GL_FALSE;
1664
1665 is_array = _mesa_format_to_array(dstFormat, &dst_type, &num_dst_components,
1666 rgba2dst, &normalized);
1667
1668 assert(is_array && !normalized);
1669
1670 if (!is_array) {
1671 free(tmp_row);
1672 return GL_FALSE;
1673 }
1674
1675 invert_swizzle(dst2rgba, rgba2dst);
1676 compute_component_mapping(GL_RGBA, baseInternalFormat, base2rgba);
1677 compute_component_mapping(baseInternalFormat, GL_RGBA, rgba2base);
1678
1679 for (i = 0; i < 4; ++i) {
1680 if (dst2rgba[i] == MESA_FORMAT_SWIZZLE_NONE)
1681 swizzle[i] = MESA_FORMAT_SWIZZLE_NONE;
1682 else
1683 swizzle[i] = base2rgba[rgba2base[dst2rgba[i]]];
1684 }
1685
1686 if (_mesa_is_type_unsigned(srcType)) {
1687 tmp_type = GL_UNSIGNED_INT;
1688 } else {
1689 tmp_type = GL_INT;
1690 }
1691
1692 for (img = 0; img < srcDepth; img++) {
1693 dst_row = dstSlices[img];
1694 src_row = _mesa_image_address(dims, srcPacking, srcAddr,
1695 srcWidth, srcHeight,
1696 srcFormat, srcType,
1697 img, 0, 0);
1698 for (row = 0; row < srcHeight; row++) {
1699 _mesa_unpack_color_span_uint(ctx, srcWidth, GL_RGBA, tmp_row,
1700 srcFormat, srcType, src_row, srcPacking);
1701 _mesa_swizzle_and_convert(dst_row, dst_type, num_dst_components,
1702 tmp_row, tmp_type, 4,
1703 swizzle, false, srcWidth);
1704 dst_row += dstRowStride;
1705 src_row += src_stride;
1706 }
1707 }
1708
1709 free(tmp_row);
1710
1711 return GL_TRUE;
1712 }
1713
1714 static GLboolean
1715 texstore_rgba(TEXSTORE_PARAMS)
1716 {
1717 static StoreTexImageFunc table[MESA_FORMAT_COUNT];
1718 static GLboolean initialized = GL_FALSE;
1719
1720 if (!initialized) {
1721 memset(table, 0, sizeof table);
1722
1723 table[MESA_FORMAT_B5G6R5_UNORM] = _mesa_texstore_rgb565;
1724 table[MESA_FORMAT_R5G6B5_UNORM] = _mesa_texstore_rgb565;
1725 table[MESA_FORMAT_YCBCR] = _mesa_texstore_ycbcr;
1726 table[MESA_FORMAT_YCBCR_REV] = _mesa_texstore_ycbcr;
1727
1728 table[MESA_FORMAT_B10G10R10A2_UINT] = _mesa_texstore_argb2101010_uint;
1729 table[MESA_FORMAT_R10G10B10A2_UINT] = _mesa_texstore_abgr2101010_uint;
1730
1731 initialized = GL_TRUE;
1732 }
1733
1734 if (table[dstFormat] && table[dstFormat](ctx, dims, baseInternalFormat,
1735 dstFormat, dstRowStride, dstSlices,
1736 srcWidth, srcHeight, srcDepth,
1737 srcFormat, srcType, srcAddr,
1738 srcPacking)) {
1739 return GL_TRUE;
1740 }
1741
1742 if (texstore_swizzle(ctx, dims, baseInternalFormat,
1743 dstFormat,
1744 dstRowStride, dstSlices,
1745 srcWidth, srcHeight, srcDepth,
1746 srcFormat, srcType, srcAddr, srcPacking)) {
1747 return GL_TRUE;
1748 }
1749
1750 if (_mesa_is_format_integer(dstFormat)) {
1751 return texstore_rgba_integer(ctx, dims, baseInternalFormat,
1752 dstFormat, dstRowStride, dstSlices,
1753 srcWidth, srcHeight, srcDepth,
1754 srcFormat, srcType, srcAddr,
1755 srcPacking);
1756 } else if (_mesa_get_format_max_bits(dstFormat) <= 8 &&
1757 !_mesa_is_format_signed(dstFormat)) {
1758 return store_ubyte_texture(ctx, dims, baseInternalFormat,
1759 dstFormat,
1760 dstRowStride, dstSlices,
1761 srcWidth, srcHeight, srcDepth,
1762 srcFormat, srcType, srcAddr, srcPacking);
1763 } else {
1764 return texstore_via_float(ctx, dims, baseInternalFormat,
1765 dstFormat, dstRowStride, dstSlices,
1766 srcWidth, srcHeight, srcDepth,
1767 srcFormat, srcType, srcAddr,
1768 srcPacking);
1769 }
1770 }
1771
1772 GLboolean
1773 _mesa_texstore_needs_transfer_ops(struct gl_context *ctx,
1774 GLenum baseInternalFormat,
1775 mesa_format dstFormat)
1776 {
1777 GLenum dstType;
1778
1779 /* There are different rules depending on the base format. */
1780 switch (baseInternalFormat) {
1781 case GL_DEPTH_COMPONENT:
1782 case GL_DEPTH_STENCIL:
1783 return ctx->Pixel.DepthScale != 1.0f ||
1784 ctx->Pixel.DepthBias != 0.0f;
1785
1786 case GL_STENCIL_INDEX:
1787 return GL_FALSE;
1788
1789 default:
1790 /* Color formats.
1791 * Pixel transfer ops (scale, bias, table lookup) do not apply
1792 * to integer formats.
1793 */
1794 dstType = _mesa_get_format_datatype(dstFormat);
1795
1796 return dstType != GL_INT && dstType != GL_UNSIGNED_INT &&
1797 ctx->_ImageTransferState;
1798 }
1799 }
1800
1801
1802 GLboolean
1803 _mesa_texstore_can_use_memcpy(struct gl_context *ctx,
1804 GLenum baseInternalFormat, mesa_format dstFormat,
1805 GLenum srcFormat, GLenum srcType,
1806 const struct gl_pixelstore_attrib *srcPacking)
1807 {
1808 if (_mesa_texstore_needs_transfer_ops(ctx, baseInternalFormat, dstFormat)) {
1809 return GL_FALSE;
1810 }
1811
1812 /* The base internal format and the base Mesa format must match. */
1813 if (baseInternalFormat != _mesa_get_format_base_format(dstFormat)) {
1814 return GL_FALSE;
1815 }
1816
1817 /* The Mesa format must match the input format and type. */
1818 if (!_mesa_format_matches_format_and_type(dstFormat, srcFormat, srcType,
1819 srcPacking->SwapBytes)) {
1820 return GL_FALSE;
1821 }
1822
1823 /* Depth texture data needs clamping in following cases:
1824 * - Floating point dstFormat with signed srcType: clamp to [0.0, 1.0].
1825 * - Fixed point dstFormat with signed srcType: clamp to [0, 2^n -1].
1826 *
1827 * All the cases except one (float dstFormat with float srcType) are ruled
1828 * out by _mesa_format_matches_format_and_type() check above. Handle the
1829 * remaining case here.
1830 */
1831 if ((baseInternalFormat == GL_DEPTH_COMPONENT ||
1832 baseInternalFormat == GL_DEPTH_STENCIL) &&
1833 (srcType == GL_FLOAT ||
1834 srcType == GL_FLOAT_32_UNSIGNED_INT_24_8_REV)) {
1835 return GL_FALSE;
1836 }
1837
1838 return GL_TRUE;
1839 }
1840
1841 static GLboolean
1842 _mesa_texstore_memcpy(TEXSTORE_PARAMS)
1843 {
1844 if (!_mesa_texstore_can_use_memcpy(ctx, baseInternalFormat, dstFormat,
1845 srcFormat, srcType, srcPacking)) {
1846 return GL_FALSE;
1847 }
1848
1849 memcpy_texture(ctx, dims,
1850 dstFormat,
1851 dstRowStride, dstSlices,
1852 srcWidth, srcHeight, srcDepth, srcFormat, srcType,
1853 srcAddr, srcPacking);
1854 return GL_TRUE;
1855 }
1856 /**
1857 * Store user data into texture memory.
1858 * Called via glTex[Sub]Image1/2/3D()
1859 * \return GL_TRUE for success, GL_FALSE for failure (out of memory).
1860 */
1861 GLboolean
1862 _mesa_texstore(TEXSTORE_PARAMS)
1863 {
1864 if (_mesa_texstore_memcpy(ctx, dims, baseInternalFormat,
1865 dstFormat,
1866 dstRowStride, dstSlices,
1867 srcWidth, srcHeight, srcDepth,
1868 srcFormat, srcType, srcAddr, srcPacking)) {
1869 return GL_TRUE;
1870 }
1871
1872 if (_mesa_is_depth_or_stencil_format(baseInternalFormat)) {
1873 return texstore_depth_stencil(ctx, dims, baseInternalFormat,
1874 dstFormat, dstRowStride, dstSlices,
1875 srcWidth, srcHeight, srcDepth,
1876 srcFormat, srcType, srcAddr, srcPacking);
1877 } else if (_mesa_is_format_compressed(dstFormat)) {
1878 return texstore_compressed(ctx, dims, baseInternalFormat,
1879 dstFormat, dstRowStride, dstSlices,
1880 srcWidth, srcHeight, srcDepth,
1881 srcFormat, srcType, srcAddr, srcPacking);
1882 } else {
1883 return texstore_rgba(ctx, dims, baseInternalFormat,
1884 dstFormat, dstRowStride, dstSlices,
1885 srcWidth, srcHeight, srcDepth,
1886 srcFormat, srcType, srcAddr, srcPacking);
1887 }
1888 }
1889
1890
1891 /**
1892 * Normally, we'll only _write_ texel data to a texture when we map it.
1893 * But if the user is providing depth or stencil values and the texture
1894 * image is a combined depth/stencil format, we'll actually read from
1895 * the texture buffer too (in order to insert the depth or stencil values.
1896 * \param userFormat the user-provided image format
1897 * \param texFormat the destination texture format
1898 */
1899 static GLbitfield
1900 get_read_write_mode(GLenum userFormat, mesa_format texFormat)
1901 {
1902 if ((userFormat == GL_STENCIL_INDEX || userFormat == GL_DEPTH_COMPONENT)
1903 && _mesa_get_format_base_format(texFormat) == GL_DEPTH_STENCIL)
1904 return GL_MAP_READ_BIT | GL_MAP_WRITE_BIT;
1905 else
1906 return GL_MAP_WRITE_BIT | GL_MAP_INVALIDATE_RANGE_BIT;
1907 }
1908
1909
1910 /**
1911 * Helper function for storing 1D, 2D, 3D whole and subimages into texture
1912 * memory.
1913 * The source of the image data may be user memory or a PBO. In the later
1914 * case, we'll map the PBO, copy from it, then unmap it.
1915 */
1916 static void
1917 store_texsubimage(struct gl_context *ctx,
1918 struct gl_texture_image *texImage,
1919 GLint xoffset, GLint yoffset, GLint zoffset,
1920 GLint width, GLint height, GLint depth,
1921 GLenum format, GLenum type, const GLvoid *pixels,
1922 const struct gl_pixelstore_attrib *packing,
1923 const char *caller)
1924
1925 {
1926 const GLbitfield mapMode = get_read_write_mode(format, texImage->TexFormat);
1927 const GLenum target = texImage->TexObject->Target;
1928 GLboolean success = GL_FALSE;
1929 GLuint dims, slice, numSlices = 1, sliceOffset = 0;
1930 GLint srcImageStride = 0;
1931 const GLubyte *src;
1932
1933 assert(xoffset + width <= texImage->Width);
1934 assert(yoffset + height <= texImage->Height);
1935 assert(zoffset + depth <= texImage->Depth);
1936
1937 switch (target) {
1938 case GL_TEXTURE_1D:
1939 dims = 1;
1940 break;
1941 case GL_TEXTURE_2D_ARRAY:
1942 case GL_TEXTURE_CUBE_MAP_ARRAY:
1943 case GL_TEXTURE_3D:
1944 dims = 3;
1945 break;
1946 default:
1947 dims = 2;
1948 }
1949
1950 /* get pointer to src pixels (may be in a pbo which we'll map here) */
1951 src = (const GLubyte *)
1952 _mesa_validate_pbo_teximage(ctx, dims, width, height, depth,
1953 format, type, pixels, packing, caller);
1954 if (!src)
1955 return;
1956
1957 /* compute slice info (and do some sanity checks) */
1958 switch (target) {
1959 case GL_TEXTURE_2D:
1960 case GL_TEXTURE_RECTANGLE:
1961 case GL_TEXTURE_CUBE_MAP:
1962 case GL_TEXTURE_EXTERNAL_OES:
1963 /* one image slice, nothing special needs to be done */
1964 break;
1965 case GL_TEXTURE_1D:
1966 assert(height == 1);
1967 assert(depth == 1);
1968 assert(yoffset == 0);
1969 assert(zoffset == 0);
1970 break;
1971 case GL_TEXTURE_1D_ARRAY:
1972 assert(depth == 1);
1973 assert(zoffset == 0);
1974 numSlices = height;
1975 sliceOffset = yoffset;
1976 height = 1;
1977 yoffset = 0;
1978 srcImageStride = _mesa_image_row_stride(packing, width, format, type);
1979 break;
1980 case GL_TEXTURE_2D_ARRAY:
1981 numSlices = depth;
1982 sliceOffset = zoffset;
1983 depth = 1;
1984 zoffset = 0;
1985 srcImageStride = _mesa_image_image_stride(packing, width, height,
1986 format, type);
1987 break;
1988 case GL_TEXTURE_3D:
1989 /* we'll store 3D images as a series of slices */
1990 numSlices = depth;
1991 sliceOffset = zoffset;
1992 srcImageStride = _mesa_image_image_stride(packing, width, height,
1993 format, type);
1994 break;
1995 case GL_TEXTURE_CUBE_MAP_ARRAY:
1996 numSlices = depth;
1997 sliceOffset = zoffset;
1998 srcImageStride = _mesa_image_image_stride(packing, width, height,
1999 format, type);
2000 break;
2001 default:
2002 _mesa_warning(ctx, "Unexpected target 0x%x in store_texsubimage()", target);
2003 return;
2004 }
2005
2006 assert(numSlices == 1 || srcImageStride != 0);
2007
2008 for (slice = 0; slice < numSlices; slice++) {
2009 GLubyte *dstMap;
2010 GLint dstRowStride;
2011
2012 ctx->Driver.MapTextureImage(ctx, texImage,
2013 slice + sliceOffset,
2014 xoffset, yoffset, width, height,
2015 mapMode, &dstMap, &dstRowStride);
2016 if (dstMap) {
2017 /* Note: we're only storing a 2D (or 1D) slice at a time but we need
2018 * to pass the right 'dims' value so that GL_UNPACK_SKIP_IMAGES is
2019 * used for 3D images.
2020 */
2021 success = _mesa_texstore(ctx, dims, texImage->_BaseFormat,
2022 texImage->TexFormat,
2023 dstRowStride,
2024 &dstMap,
2025 width, height, 1, /* w, h, d */
2026 format, type, src, packing);
2027
2028 ctx->Driver.UnmapTextureImage(ctx, texImage, slice + sliceOffset);
2029 }
2030
2031 src += srcImageStride;
2032
2033 if (!success)
2034 break;
2035 }
2036
2037 if (!success)
2038 _mesa_error(ctx, GL_OUT_OF_MEMORY, "%s", caller);
2039
2040 _mesa_unmap_teximage_pbo(ctx, packing);
2041 }
2042
2043
2044
2045 /**
2046 * Fallback code for ctx->Driver.TexImage().
2047 * Basically, allocate storage for the texture image, then copy the
2048 * user's image into it.
2049 */
2050 void
2051 _mesa_store_teximage(struct gl_context *ctx,
2052 GLuint dims,
2053 struct gl_texture_image *texImage,
2054 GLenum format, GLenum type, const GLvoid *pixels,
2055 const struct gl_pixelstore_attrib *packing)
2056 {
2057 assert(dims == 1 || dims == 2 || dims == 3);
2058
2059 if (texImage->Width == 0 || texImage->Height == 0 || texImage->Depth == 0)
2060 return;
2061
2062 /* allocate storage for texture data */
2063 if (!ctx->Driver.AllocTextureImageBuffer(ctx, texImage)) {
2064 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexImage%uD", dims);
2065 return;
2066 }
2067
2068 store_texsubimage(ctx, texImage,
2069 0, 0, 0, texImage->Width, texImage->Height, texImage->Depth,
2070 format, type, pixels, packing, "glTexImage");
2071 }
2072
2073
2074 /*
2075 * Fallback for Driver.TexSubImage().
2076 */
2077 void
2078 _mesa_store_texsubimage(struct gl_context *ctx, GLuint dims,
2079 struct gl_texture_image *texImage,
2080 GLint xoffset, GLint yoffset, GLint zoffset,
2081 GLint width, GLint height, GLint depth,
2082 GLenum format, GLenum type, const void *pixels,
2083 const struct gl_pixelstore_attrib *packing)
2084 {
2085 store_texsubimage(ctx, texImage,
2086 xoffset, yoffset, zoffset, width, height, depth,
2087 format, type, pixels, packing, "glTexSubImage");
2088 }
2089
2090 static void
2091 clear_image_to_zero(GLubyte *dstMap, GLint dstRowStride,
2092 GLsizei width, GLsizei height,
2093 GLsizei clearValueSize)
2094 {
2095 GLsizei y;
2096
2097 for (y = 0; y < height; y++) {
2098 memset(dstMap, 0, clearValueSize * width);
2099 dstMap += dstRowStride;
2100 }
2101 }
2102
2103 static void
2104 clear_image_to_value(GLubyte *dstMap, GLint dstRowStride,
2105 GLsizei width, GLsizei height,
2106 const GLvoid *clearValue,
2107 GLsizei clearValueSize)
2108 {
2109 GLsizei y, x;
2110
2111 for (y = 0; y < height; y++) {
2112 for (x = 0; x < width; x++) {
2113 memcpy(dstMap, clearValue, clearValueSize);
2114 dstMap += clearValueSize;
2115 }
2116 dstMap += dstRowStride - clearValueSize * width;
2117 }
2118 }
2119
2120 /*
2121 * Fallback for Driver.ClearTexSubImage().
2122 */
2123 void
2124 _mesa_store_cleartexsubimage(struct gl_context *ctx,
2125 struct gl_texture_image *texImage,
2126 GLint xoffset, GLint yoffset, GLint zoffset,
2127 GLsizei width, GLsizei height, GLsizei depth,
2128 const GLvoid *clearValue)
2129 {
2130 GLubyte *dstMap;
2131 GLint dstRowStride;
2132 GLsizeiptr clearValueSize;
2133 GLsizei z;
2134
2135 clearValueSize = _mesa_get_format_bytes(texImage->TexFormat);
2136
2137 for (z = 0; z < depth; z++) {
2138 ctx->Driver.MapTextureImage(ctx, texImage,
2139 z + zoffset, xoffset, yoffset,
2140 width, height,
2141 GL_MAP_WRITE_BIT,
2142 &dstMap, &dstRowStride);
2143 if (dstMap == NULL) {
2144 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glClearTex*Image");
2145 return;
2146 }
2147
2148 if (clearValue) {
2149 clear_image_to_value(dstMap, dstRowStride,
2150 width, height,
2151 clearValue,
2152 clearValueSize);
2153 } else {
2154 clear_image_to_zero(dstMap, dstRowStride,
2155 width, height,
2156 clearValueSize);
2157 }
2158
2159 ctx->Driver.UnmapTextureImage(ctx, texImage, z + zoffset);
2160 }
2161 }
2162
2163 /**
2164 * Fallback for Driver.CompressedTexImage()
2165 */
2166 void
2167 _mesa_store_compressed_teximage(struct gl_context *ctx, GLuint dims,
2168 struct gl_texture_image *texImage,
2169 GLsizei imageSize, const GLvoid *data)
2170 {
2171 /* only 2D and 3D compressed images are supported at this time */
2172 if (dims == 1) {
2173 _mesa_problem(ctx, "Unexpected glCompressedTexImage1D call");
2174 return;
2175 }
2176
2177 /* This is pretty simple, because unlike the general texstore path we don't
2178 * have to worry about the usual image unpacking or image transfer
2179 * operations.
2180 */
2181 ASSERT(texImage);
2182 ASSERT(texImage->Width > 0);
2183 ASSERT(texImage->Height > 0);
2184 ASSERT(texImage->Depth > 0);
2185
2186 /* allocate storage for texture data */
2187 if (!ctx->Driver.AllocTextureImageBuffer(ctx, texImage)) {
2188 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCompressedTexImage%uD", dims);
2189 return;
2190 }
2191
2192 _mesa_store_compressed_texsubimage(ctx, dims, texImage,
2193 0, 0, 0,
2194 texImage->Width, texImage->Height, texImage->Depth,
2195 texImage->TexFormat,
2196 imageSize, data);
2197 }
2198
2199
2200 /**
2201 * Compute compressed_pixelstore parameters for copying compressed
2202 * texture data.
2203 * \param dims number of texture image dimensions: 1, 2 or 3
2204 * \param texFormat the compressed texture format
2205 * \param width, height, depth size of image to copy
2206 * \param packing pixelstore parameters describing user-space image packing
2207 * \param store returns the compressed_pixelstore parameters
2208 */
2209 void
2210 _mesa_compute_compressed_pixelstore(GLuint dims, mesa_format texFormat,
2211 GLsizei width, GLsizei height,
2212 GLsizei depth,
2213 const struct gl_pixelstore_attrib *packing,
2214 struct compressed_pixelstore *store)
2215 {
2216 GLuint bw, bh;
2217
2218 _mesa_get_format_block_size(texFormat, &bw, &bh);
2219
2220 store->SkipBytes = 0;
2221 store->TotalBytesPerRow = store->CopyBytesPerRow =
2222 _mesa_format_row_stride(texFormat, width);
2223 store->TotalRowsPerSlice = store->CopyRowsPerSlice =
2224 (height + bh - 1) / bh;
2225 store->CopySlices = depth;
2226
2227 if (packing->CompressedBlockWidth &&
2228 packing->CompressedBlockSize) {
2229
2230 bw = packing->CompressedBlockWidth;
2231
2232 if (packing->RowLength) {
2233 store->TotalBytesPerRow = packing->CompressedBlockSize *
2234 ((packing->RowLength + bw - 1) / bw);
2235 }
2236
2237 store->SkipBytes += packing->SkipPixels * packing->CompressedBlockSize / bw;
2238 }
2239
2240 if (dims > 1 && packing->CompressedBlockHeight &&
2241 packing->CompressedBlockSize) {
2242
2243 bh = packing->CompressedBlockHeight;
2244
2245 store->SkipBytes += packing->SkipRows * store->TotalBytesPerRow / bh;
2246 store->CopyRowsPerSlice = (height + bh - 1) / bh; /* rows in blocks */
2247
2248 if (packing->ImageHeight) {
2249 store->TotalRowsPerSlice = (packing->ImageHeight + bh - 1) / bh;
2250 }
2251 }
2252
2253 if (dims > 2 && packing->CompressedBlockDepth &&
2254 packing->CompressedBlockSize) {
2255
2256 int bd = packing->CompressedBlockDepth;
2257
2258 store->SkipBytes += packing->SkipImages * store->TotalBytesPerRow *
2259 store->TotalRowsPerSlice / bd;
2260 }
2261 }
2262
2263
2264 /**
2265 * Fallback for Driver.CompressedTexSubImage()
2266 */
2267 void
2268 _mesa_store_compressed_texsubimage(struct gl_context *ctx, GLuint dims,
2269 struct gl_texture_image *texImage,
2270 GLint xoffset, GLint yoffset, GLint zoffset,
2271 GLsizei width, GLsizei height, GLsizei depth,
2272 GLenum format,
2273 GLsizei imageSize, const GLvoid *data)
2274 {
2275 struct compressed_pixelstore store;
2276 GLint dstRowStride;
2277 GLint i, slice;
2278 GLubyte *dstMap;
2279 const GLubyte *src;
2280
2281 if (dims == 1) {
2282 _mesa_problem(ctx, "Unexpected 1D compressed texsubimage call");
2283 return;
2284 }
2285
2286 _mesa_compute_compressed_pixelstore(dims, texImage->TexFormat,
2287 width, height, depth,
2288 &ctx->Unpack, &store);
2289
2290 /* get pointer to src pixels (may be in a pbo which we'll map here) */
2291 data = _mesa_validate_pbo_compressed_teximage(ctx, dims, imageSize, data,
2292 &ctx->Unpack,
2293 "glCompressedTexSubImage");
2294 if (!data)
2295 return;
2296
2297 src = (const GLubyte *) data + store.SkipBytes;
2298
2299 for (slice = 0; slice < store.CopySlices; slice++) {
2300 /* Map dest texture buffer */
2301 ctx->Driver.MapTextureImage(ctx, texImage, slice + zoffset,
2302 xoffset, yoffset, width, height,
2303 GL_MAP_WRITE_BIT | GL_MAP_INVALIDATE_RANGE_BIT,
2304 &dstMap, &dstRowStride);
2305
2306 if (dstMap) {
2307
2308 /* copy rows of blocks */
2309 for (i = 0; i < store.CopyRowsPerSlice; i++) {
2310 memcpy(dstMap, src, store.CopyBytesPerRow);
2311 dstMap += dstRowStride;
2312 src += store.TotalBytesPerRow;
2313 }
2314
2315 ctx->Driver.UnmapTextureImage(ctx, texImage, slice + zoffset);
2316
2317 /* advance to next slice */
2318 src += store.TotalBytesPerRow * (store.TotalRowsPerSlice - store.CopyRowsPerSlice);
2319 }
2320 else {
2321 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCompressedTexSubImage%uD",
2322 dims);
2323 }
2324 }
2325
2326 _mesa_unmap_teximage_pbo(ctx, &ctx->Unpack);
2327 }