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