2 * Mesa 3-D graphics library
4 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
5 * Copyright (c) 2008-2009 VMware, Inc.
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:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
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.
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
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
40 * ctx->Driver.TexImage = _mesa_store_teximage;
41 * ctx->Driver.TexSubImage = _mesa_store_texsubimage;
44 * Texture image processing is actually kind of complicated. We have to do:
45 * Format/type conversions
47 * pixel transfer (scale, bais, lookup, etc)
49 * These functions can handle most everything, including processing full
50 * images and sub-images.
55 #include "bufferobj.h"
57 #include "format_pack.h"
58 #include "format_utils.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"
75 #include "glformats.h"
76 #include "../../gallium/auxiliary/util/u_format_rgb9e5.h"
77 #include "../../gallium/auxiliary/util/u_format_r11g11b10f.h"
87 * Texture image storage function.
89 typedef GLboolean (*StoreTexImageFunc
)(TEXSTORE_PARAMS
);
90 static const GLubyte map_identity
[6] = { 0, 1, 2, 3, ZERO
, ONE
};
91 static const GLubyte map_3210
[6] = { 3, 2, 1, 0, ZERO
, ONE
};
92 static const GLubyte map_1032
[6] = { 1, 0, 3, 2, ZERO
, ONE
};
96 * Teximage storage routine for when a simple memcpy will do.
97 * No pixel transfer operations or special texel encodings allowed.
98 * 1D, 2D and 3D images supported.
101 memcpy_texture(struct gl_context
*ctx
,
103 mesa_format dstFormat
,
106 GLint srcWidth
, GLint srcHeight
, GLint srcDepth
,
107 GLenum srcFormat
, GLenum srcType
,
108 const GLvoid
*srcAddr
,
109 const struct gl_pixelstore_attrib
*srcPacking
)
111 const GLint srcRowStride
= _mesa_image_row_stride(srcPacking
, srcWidth
,
113 const GLint srcImageStride
= _mesa_image_image_stride(srcPacking
,
114 srcWidth
, srcHeight
, srcFormat
, srcType
);
115 const GLubyte
*srcImage
= (const GLubyte
*) _mesa_image_address(dimensions
,
116 srcPacking
, srcAddr
, srcWidth
, srcHeight
, srcFormat
, srcType
, 0, 0, 0);
117 const GLuint texelBytes
= _mesa_get_format_bytes(dstFormat
);
118 const GLint bytesPerRow
= srcWidth
* texelBytes
;
120 if (dstRowStride
== srcRowStride
&&
121 dstRowStride
== bytesPerRow
) {
122 /* memcpy image by image */
124 for (img
= 0; img
< srcDepth
; img
++) {
125 GLubyte
*dstImage
= dstSlices
[img
];
126 memcpy(dstImage
, srcImage
, bytesPerRow
* srcHeight
);
127 srcImage
+= srcImageStride
;
131 /* memcpy row by row */
133 for (img
= 0; img
< srcDepth
; img
++) {
134 const GLubyte
*srcRow
= srcImage
;
135 GLubyte
*dstRow
= dstSlices
[img
];
136 for (row
= 0; row
< srcHeight
; row
++) {
137 memcpy(dstRow
, srcRow
, bytesPerRow
);
138 dstRow
+= dstRowStride
;
139 srcRow
+= srcRowStride
;
141 srcImage
+= srcImageStride
;
148 * Store a 32-bit integer or float depth component texture image.
151 _mesa_texstore_z32(TEXSTORE_PARAMS
)
153 const GLuint depthScale
= 0xffffffff;
156 ASSERT(dstFormat
== MESA_FORMAT_Z_UNORM32
||
157 dstFormat
== MESA_FORMAT_Z_FLOAT32
);
158 ASSERT(_mesa_get_format_bytes(dstFormat
) == sizeof(GLuint
));
160 if (dstFormat
== MESA_FORMAT_Z_UNORM32
)
161 dstType
= GL_UNSIGNED_INT
;
168 for (img
= 0; img
< srcDepth
; img
++) {
169 GLubyte
*dstRow
= dstSlices
[img
];
170 for (row
= 0; row
< srcHeight
; row
++) {
171 const GLvoid
*src
= _mesa_image_address(dims
, srcPacking
,
172 srcAddr
, srcWidth
, srcHeight
, srcFormat
, srcType
, img
, row
, 0);
173 _mesa_unpack_depth_span(ctx
, srcWidth
,
175 depthScale
, srcType
, src
, srcPacking
);
176 dstRow
+= dstRowStride
;
185 * Store a 24-bit integer depth component texture image.
188 _mesa_texstore_x8_z24(TEXSTORE_PARAMS
)
190 const GLuint depthScale
= 0xffffff;
193 ASSERT(dstFormat
== MESA_FORMAT_Z24_UNORM_X8_UINT
);
198 for (img
= 0; img
< srcDepth
; img
++) {
199 GLubyte
*dstRow
= dstSlices
[img
];
200 for (row
= 0; row
< srcHeight
; row
++) {
201 const GLvoid
*src
= _mesa_image_address(dims
, srcPacking
,
202 srcAddr
, srcWidth
, srcHeight
, srcFormat
, srcType
, img
, row
, 0);
203 _mesa_unpack_depth_span(ctx
, srcWidth
,
204 GL_UNSIGNED_INT
, (GLuint
*) dstRow
,
205 depthScale
, srcType
, src
, srcPacking
);
206 dstRow
+= dstRowStride
;
215 * Store a 24-bit integer depth component texture image.
218 _mesa_texstore_z24_x8(TEXSTORE_PARAMS
)
220 const GLuint depthScale
= 0xffffff;
223 ASSERT(dstFormat
== MESA_FORMAT_X8_UINT_Z24_UNORM
);
228 for (img
= 0; img
< srcDepth
; img
++) {
229 GLubyte
*dstRow
= dstSlices
[img
];
230 for (row
= 0; row
< srcHeight
; row
++) {
231 const GLvoid
*src
= _mesa_image_address(dims
, srcPacking
,
232 srcAddr
, srcWidth
, srcHeight
, srcFormat
, srcType
, img
, row
, 0);
233 GLuint
*dst
= (GLuint
*) dstRow
;
235 _mesa_unpack_depth_span(ctx
, srcWidth
,
236 GL_UNSIGNED_INT
, dst
,
237 depthScale
, srcType
, src
, srcPacking
);
238 for (i
= 0; i
< srcWidth
; i
++)
240 dstRow
+= dstRowStride
;
249 * Store a 16-bit integer depth component texture image.
252 _mesa_texstore_z16(TEXSTORE_PARAMS
)
254 const GLuint depthScale
= 0xffff;
256 ASSERT(dstFormat
== MESA_FORMAT_Z_UNORM16
);
257 ASSERT(_mesa_get_format_bytes(dstFormat
) == sizeof(GLushort
));
262 for (img
= 0; img
< srcDepth
; img
++) {
263 GLubyte
*dstRow
= dstSlices
[img
];
264 for (row
= 0; row
< srcHeight
; row
++) {
265 const GLvoid
*src
= _mesa_image_address(dims
, srcPacking
,
266 srcAddr
, srcWidth
, srcHeight
, srcFormat
, srcType
, img
, row
, 0);
267 GLushort
*dst16
= (GLushort
*) dstRow
;
268 _mesa_unpack_depth_span(ctx
, srcWidth
,
269 GL_UNSIGNED_SHORT
, dst16
, depthScale
,
270 srcType
, src
, srcPacking
);
271 dstRow
+= dstRowStride
;
280 * Texstore for _mesa_texformat_ycbcr or _mesa_texformat_ycbcr_REV.
283 _mesa_texstore_ycbcr(TEXSTORE_PARAMS
)
285 const GLboolean littleEndian
= _mesa_little_endian();
287 (void) ctx
; (void) dims
; (void) baseInternalFormat
;
289 ASSERT((dstFormat
== MESA_FORMAT_YCBCR
) ||
290 (dstFormat
== MESA_FORMAT_YCBCR_REV
));
291 ASSERT(_mesa_get_format_bytes(dstFormat
) == 2);
292 ASSERT(ctx
->Extensions
.MESA_ycbcr_texture
);
293 ASSERT(srcFormat
== GL_YCBCR_MESA
);
294 ASSERT((srcType
== GL_UNSIGNED_SHORT_8_8_MESA
) ||
295 (srcType
== GL_UNSIGNED_SHORT_8_8_REV_MESA
));
296 ASSERT(baseInternalFormat
== GL_YCBCR_MESA
);
298 /* always just memcpy since no pixel transfer ops apply */
299 memcpy_texture(ctx
, dims
,
301 dstRowStride
, dstSlices
,
302 srcWidth
, srcHeight
, srcDepth
, srcFormat
, srcType
,
303 srcAddr
, srcPacking
);
305 /* Check if we need byte swapping */
306 /* XXX the logic here _might_ be wrong */
307 if (srcPacking
->SwapBytes
^
308 (srcType
== GL_UNSIGNED_SHORT_8_8_REV_MESA
) ^
309 (dstFormat
== MESA_FORMAT_YCBCR_REV
) ^
312 for (img
= 0; img
< srcDepth
; img
++) {
313 GLubyte
*dstRow
= dstSlices
[img
];
314 for (row
= 0; row
< srcHeight
; row
++) {
315 _mesa_swap2((GLushort
*) dstRow
, srcWidth
);
316 dstRow
+= dstRowStride
;
325 * Store a combined depth/stencil texture image.
328 _mesa_texstore_z24_s8(TEXSTORE_PARAMS
)
330 const GLuint depthScale
= 0xffffff;
331 const GLint srcRowStride
332 = _mesa_image_row_stride(srcPacking
, srcWidth
, srcFormat
, srcType
);
334 GLuint
*depth
= malloc(srcWidth
* sizeof(GLuint
));
335 GLubyte
*stencil
= malloc(srcWidth
* sizeof(GLubyte
));
337 ASSERT(dstFormat
== MESA_FORMAT_S8_UINT_Z24_UNORM
);
338 ASSERT(srcFormat
== GL_DEPTH_STENCIL_EXT
||
339 srcFormat
== GL_DEPTH_COMPONENT
||
340 srcFormat
== GL_STENCIL_INDEX
);
341 ASSERT(srcFormat
!= GL_DEPTH_STENCIL_EXT
||
342 srcType
== GL_UNSIGNED_INT_24_8_EXT
||
343 srcType
== GL_FLOAT_32_UNSIGNED_INT_24_8_REV
);
345 if (!depth
|| !stencil
) {
351 /* In case we only upload depth we need to preserve the stencil */
352 for (img
= 0; img
< srcDepth
; img
++) {
353 GLuint
*dstRow
= (GLuint
*) dstSlices
[img
];
355 = (const GLubyte
*) _mesa_image_address(dims
, srcPacking
, srcAddr
,
359 for (row
= 0; row
< srcHeight
; row
++) {
361 GLboolean keepdepth
= GL_FALSE
, keepstencil
= GL_FALSE
;
363 if (srcFormat
== GL_DEPTH_COMPONENT
) { /* preserve stencil */
364 keepstencil
= GL_TRUE
;
366 else if (srcFormat
== GL_STENCIL_INDEX
) { /* preserve depth */
370 if (keepdepth
== GL_FALSE
)
371 /* the 24 depth bits will be in the low position: */
372 _mesa_unpack_depth_span(ctx
, srcWidth
,
373 GL_UNSIGNED_INT
, /* dst type */
374 keepstencil
? depth
: dstRow
, /* dst addr */
376 srcType
, src
, srcPacking
);
378 if (keepstencil
== GL_FALSE
)
379 /* get the 8-bit stencil values */
380 _mesa_unpack_stencil_span(ctx
, srcWidth
,
381 GL_UNSIGNED_BYTE
, /* dst type */
382 stencil
, /* dst addr */
383 srcType
, src
, srcPacking
,
384 ctx
->_ImageTransferState
);
386 for (i
= 0; i
< srcWidth
; i
++) {
388 dstRow
[i
] = depth
[i
] << 8 | (dstRow
[i
] & 0x000000FF);
390 dstRow
[i
] = (dstRow
[i
] & 0xFFFFFF00) | (stencil
[i
] & 0xFF);
393 dstRow
+= dstRowStride
/ sizeof(GLuint
);
404 * Store a combined depth/stencil texture image.
407 _mesa_texstore_s8_z24(TEXSTORE_PARAMS
)
409 const GLuint depthScale
= 0xffffff;
410 const GLint srcRowStride
411 = _mesa_image_row_stride(srcPacking
, srcWidth
, srcFormat
, srcType
);
416 ASSERT(dstFormat
== MESA_FORMAT_Z24_UNORM_S8_UINT
);
417 ASSERT(srcFormat
== GL_DEPTH_STENCIL_EXT
||
418 srcFormat
== GL_DEPTH_COMPONENT
||
419 srcFormat
== GL_STENCIL_INDEX
);
420 ASSERT(srcFormat
!= GL_DEPTH_STENCIL_EXT
||
421 srcType
== GL_UNSIGNED_INT_24_8_EXT
||
422 srcType
== GL_FLOAT_32_UNSIGNED_INT_24_8_REV
);
424 depth
= malloc(srcWidth
* sizeof(GLuint
));
425 stencil
= malloc(srcWidth
* sizeof(GLubyte
));
427 if (!depth
|| !stencil
) {
433 for (img
= 0; img
< srcDepth
; img
++) {
434 GLuint
*dstRow
= (GLuint
*) dstSlices
[img
];
436 = (const GLubyte
*) _mesa_image_address(dims
, srcPacking
, srcAddr
,
440 for (row
= 0; row
< srcHeight
; row
++) {
442 GLboolean keepdepth
= GL_FALSE
, keepstencil
= GL_FALSE
;
444 if (srcFormat
== GL_DEPTH_COMPONENT
) { /* preserve stencil */
445 keepstencil
= GL_TRUE
;
447 else if (srcFormat
== GL_STENCIL_INDEX
) { /* preserve depth */
451 if (keepdepth
== GL_FALSE
)
452 /* the 24 depth bits will be in the low position: */
453 _mesa_unpack_depth_span(ctx
, srcWidth
,
454 GL_UNSIGNED_INT
, /* dst type */
455 keepstencil
? depth
: dstRow
, /* dst addr */
457 srcType
, src
, srcPacking
);
459 if (keepstencil
== GL_FALSE
)
460 /* get the 8-bit stencil values */
461 _mesa_unpack_stencil_span(ctx
, srcWidth
,
462 GL_UNSIGNED_BYTE
, /* dst type */
463 stencil
, /* dst addr */
464 srcType
, src
, srcPacking
,
465 ctx
->_ImageTransferState
);
467 /* merge stencil values into depth values */
468 for (i
= 0; i
< srcWidth
; i
++) {
470 dstRow
[i
] = depth
[i
] | (dstRow
[i
] & 0xFF000000);
472 dstRow
[i
] = (dstRow
[i
] & 0xFFFFFF) | (stencil
[i
] << 24);
476 dstRow
+= dstRowStride
/ sizeof(GLuint
);
488 * Store simple 8-bit/value stencil texture data.
491 _mesa_texstore_s8(TEXSTORE_PARAMS
)
493 ASSERT(dstFormat
== MESA_FORMAT_S_UINT8
);
494 ASSERT(srcFormat
== GL_STENCIL_INDEX
);
497 const GLint srcRowStride
498 = _mesa_image_row_stride(srcPacking
, srcWidth
, srcFormat
, srcType
);
500 GLubyte
*stencil
= malloc(srcWidth
* sizeof(GLubyte
));
505 for (img
= 0; img
< srcDepth
; img
++) {
506 GLubyte
*dstRow
= dstSlices
[img
];
508 = (const GLubyte
*) _mesa_image_address(dims
, srcPacking
, srcAddr
,
512 for (row
= 0; row
< srcHeight
; row
++) {
515 /* get the 8-bit stencil values */
516 _mesa_unpack_stencil_span(ctx
, srcWidth
,
517 GL_UNSIGNED_BYTE
, /* dst type */
518 stencil
, /* dst addr */
519 srcType
, src
, srcPacking
,
520 ctx
->_ImageTransferState
);
521 /* merge stencil values into depth values */
522 for (i
= 0; i
< srcWidth
; i
++)
523 dstRow
[i
] = stencil
[i
];
526 dstRow
+= dstRowStride
/ sizeof(GLubyte
);
538 _mesa_texstore_z32f_x24s8(TEXSTORE_PARAMS
)
541 const GLint srcRowStride
542 = _mesa_image_row_stride(srcPacking
, srcWidth
, srcFormat
, srcType
)
545 ASSERT(dstFormat
== MESA_FORMAT_Z32_FLOAT_S8X24_UINT
);
546 ASSERT(srcFormat
== GL_DEPTH_STENCIL
||
547 srcFormat
== GL_DEPTH_COMPONENT
||
548 srcFormat
== GL_STENCIL_INDEX
);
549 ASSERT(srcFormat
!= GL_DEPTH_STENCIL
||
550 srcType
== GL_UNSIGNED_INT_24_8
||
551 srcType
== GL_FLOAT_32_UNSIGNED_INT_24_8_REV
);
553 /* In case we only upload depth we need to preserve the stencil */
554 for (img
= 0; img
< srcDepth
; img
++) {
555 uint64_t *dstRow
= (uint64_t *) dstSlices
[img
];
557 = (const uint64_t *) _mesa_image_address(dims
, srcPacking
, srcAddr
,
561 for (row
= 0; row
< srcHeight
; row
++) {
562 /* The unpack functions with:
563 * dstType = GL_FLOAT_32_UNSIGNED_INT_24_8_REV
564 * only write their own dword, so the other dword (stencil
565 * or depth) is preserved. */
566 if (srcFormat
!= GL_STENCIL_INDEX
)
567 _mesa_unpack_depth_span(ctx
, srcWidth
,
568 GL_FLOAT_32_UNSIGNED_INT_24_8_REV
, /* dst type */
569 dstRow
, /* dst addr */
570 ~0U, srcType
, src
, srcPacking
);
572 if (srcFormat
!= GL_DEPTH_COMPONENT
)
573 _mesa_unpack_stencil_span(ctx
, srcWidth
,
574 GL_FLOAT_32_UNSIGNED_INT_24_8_REV
, /* dst type */
575 dstRow
, /* dst addr */
576 srcType
, src
, srcPacking
,
577 ctx
->_ImageTransferState
);
580 dstRow
+= dstRowStride
/ sizeof(uint64_t);
587 texstore_depth_stencil(TEXSTORE_PARAMS
)
589 static StoreTexImageFunc table
[MESA_FORMAT_COUNT
];
590 static GLboolean initialized
= GL_FALSE
;
593 memset(table
, 0, sizeof table
);
595 table
[MESA_FORMAT_S8_UINT_Z24_UNORM
] = _mesa_texstore_z24_s8
;
596 table
[MESA_FORMAT_Z24_UNORM_S8_UINT
] = _mesa_texstore_s8_z24
;
597 table
[MESA_FORMAT_Z_UNORM16
] = _mesa_texstore_z16
;
598 table
[MESA_FORMAT_Z24_UNORM_X8_UINT
] = _mesa_texstore_x8_z24
;
599 table
[MESA_FORMAT_X8_UINT_Z24_UNORM
] = _mesa_texstore_z24_x8
;
600 table
[MESA_FORMAT_Z_UNORM32
] = _mesa_texstore_z32
;
601 table
[MESA_FORMAT_S_UINT8
] = _mesa_texstore_s8
;
602 table
[MESA_FORMAT_Z_FLOAT32
] = _mesa_texstore_z32
;
603 table
[MESA_FORMAT_Z32_FLOAT_S8X24_UINT
] = _mesa_texstore_z32f_x24s8
;
605 initialized
= GL_TRUE
;
608 ASSERT(table
[dstFormat
]);
609 return table
[dstFormat
](ctx
, dims
, baseInternalFormat
,
610 dstFormat
, dstRowStride
, dstSlices
,
611 srcWidth
, srcHeight
, srcDepth
,
612 srcFormat
, srcType
, srcAddr
, srcPacking
);
616 texstore_compressed(TEXSTORE_PARAMS
)
618 static StoreTexImageFunc table
[MESA_FORMAT_COUNT
];
619 static GLboolean initialized
= GL_FALSE
;
622 memset(table
, 0, sizeof table
);
624 table
[MESA_FORMAT_SRGB_DXT1
] = _mesa_texstore_rgb_dxt1
;
625 table
[MESA_FORMAT_SRGBA_DXT1
] = _mesa_texstore_rgba_dxt1
;
626 table
[MESA_FORMAT_SRGBA_DXT3
] = _mesa_texstore_rgba_dxt3
;
627 table
[MESA_FORMAT_SRGBA_DXT5
] = _mesa_texstore_rgba_dxt5
;
628 table
[MESA_FORMAT_RGB_FXT1
] = _mesa_texstore_rgb_fxt1
;
629 table
[MESA_FORMAT_RGBA_FXT1
] = _mesa_texstore_rgba_fxt1
;
630 table
[MESA_FORMAT_RGB_DXT1
] = _mesa_texstore_rgb_dxt1
;
631 table
[MESA_FORMAT_RGBA_DXT1
] = _mesa_texstore_rgba_dxt1
;
632 table
[MESA_FORMAT_RGBA_DXT3
] = _mesa_texstore_rgba_dxt3
;
633 table
[MESA_FORMAT_RGBA_DXT5
] = _mesa_texstore_rgba_dxt5
;
634 table
[MESA_FORMAT_R_RGTC1_UNORM
] = _mesa_texstore_red_rgtc1
;
635 table
[MESA_FORMAT_R_RGTC1_SNORM
] = _mesa_texstore_signed_red_rgtc1
;
636 table
[MESA_FORMAT_RG_RGTC2_UNORM
] = _mesa_texstore_rg_rgtc2
;
637 table
[MESA_FORMAT_RG_RGTC2_SNORM
] = _mesa_texstore_signed_rg_rgtc2
;
638 table
[MESA_FORMAT_L_LATC1_UNORM
] = _mesa_texstore_red_rgtc1
;
639 table
[MESA_FORMAT_L_LATC1_SNORM
] = _mesa_texstore_signed_red_rgtc1
;
640 table
[MESA_FORMAT_LA_LATC2_UNORM
] = _mesa_texstore_rg_rgtc2
;
641 table
[MESA_FORMAT_LA_LATC2_SNORM
] = _mesa_texstore_signed_rg_rgtc2
;
642 table
[MESA_FORMAT_ETC1_RGB8
] = _mesa_texstore_etc1_rgb8
;
643 table
[MESA_FORMAT_ETC2_RGB8
] = _mesa_texstore_etc2_rgb8
;
644 table
[MESA_FORMAT_ETC2_SRGB8
] = _mesa_texstore_etc2_srgb8
;
645 table
[MESA_FORMAT_ETC2_RGBA8_EAC
] = _mesa_texstore_etc2_rgba8_eac
;
646 table
[MESA_FORMAT_ETC2_SRGB8_ALPHA8_EAC
] = _mesa_texstore_etc2_srgb8_alpha8_eac
;
647 table
[MESA_FORMAT_ETC2_R11_EAC
] = _mesa_texstore_etc2_r11_eac
;
648 table
[MESA_FORMAT_ETC2_RG11_EAC
] = _mesa_texstore_etc2_rg11_eac
;
649 table
[MESA_FORMAT_ETC2_SIGNED_R11_EAC
] = _mesa_texstore_etc2_signed_r11_eac
;
650 table
[MESA_FORMAT_ETC2_SIGNED_RG11_EAC
] = _mesa_texstore_etc2_signed_rg11_eac
;
651 table
[MESA_FORMAT_ETC2_RGB8_PUNCHTHROUGH_ALPHA1
] =
652 _mesa_texstore_etc2_rgb8_punchthrough_alpha1
;
653 table
[MESA_FORMAT_ETC2_SRGB8_PUNCHTHROUGH_ALPHA1
] =
654 _mesa_texstore_etc2_srgb8_punchthrough_alpha1
;
656 table
[MESA_FORMAT_BPTC_RGBA_UNORM
] =
657 _mesa_texstore_bptc_rgba_unorm
;
658 table
[MESA_FORMAT_BPTC_SRGB_ALPHA_UNORM
] =
659 _mesa_texstore_bptc_rgba_unorm
;
660 table
[MESA_FORMAT_BPTC_RGB_SIGNED_FLOAT
] =
661 _mesa_texstore_bptc_rgb_signed_float
;
662 table
[MESA_FORMAT_BPTC_RGB_UNSIGNED_FLOAT
] =
663 _mesa_texstore_bptc_rgb_unsigned_float
;
665 initialized
= GL_TRUE
;
668 ASSERT(table
[dstFormat
]);
669 return table
[dstFormat
](ctx
, dims
, baseInternalFormat
,
670 dstFormat
, dstRowStride
, dstSlices
,
671 srcWidth
, srcHeight
, srcDepth
,
672 srcFormat
, srcType
, srcAddr
, srcPacking
);
676 texstore_rgba(TEXSTORE_PARAMS
)
678 void *tempImage
= NULL
;
679 int srcRowStride
, img
;
681 uint32_t srcMesaFormat
;
682 uint8_t rebaseSwizzle
[4];
685 /* We have to handle MESA_FORMAT_YCBCR manually because it is a special case
686 * and _mesa_format_convert does not support it. In this case the we only
687 * allow conversions between YCBCR formats and it is mostly a memcpy.
689 if (dstFormat
== MESA_FORMAT_YCBCR
|| dstFormat
== MESA_FORMAT_YCBCR_REV
) {
690 return _mesa_texstore_ycbcr(ctx
, dims
, baseInternalFormat
,
691 dstFormat
, dstRowStride
, dstSlices
,
692 srcWidth
, srcHeight
, srcDepth
,
693 srcFormat
, srcType
, srcAddr
,
697 /* We have to deal with GL_COLOR_INDEX manually because
698 * _mesa_format_convert does not handle this format. So what we do here is
699 * convert it to RGBA ubyte first and then convert from that to dst as usual.
701 if (srcFormat
== GL_COLOR_INDEX
) {
702 /* Notice that this will already handle byte swapping if necessary */
704 _mesa_unpack_color_index_to_rgba_ubyte(ctx
, dims
,
705 srcAddr
, srcFormat
, srcType
,
706 srcWidth
, srcHeight
, srcDepth
,
708 ctx
->_ImageTransferState
);
712 /* Now we only have to adjust our src info for a conversion from
713 * the RGBA ubyte and then we continue as usual.
717 srcType
= GL_UNSIGNED_BYTE
;
718 } else if (srcPacking
->SwapBytes
) {
719 /* We have to handle byte-swapping scenarios before calling
720 * _mesa_format_convert
722 GLint swapSize
= _mesa_sizeof_packed_type(srcType
);
723 if (swapSize
== 2 || swapSize
== 4) {
724 int components
= _mesa_components_in_format(srcFormat
);
725 int elementCount
= srcWidth
* srcHeight
* components
;
726 tempImage
= malloc(elementCount
* swapSize
);
730 _mesa_swap2_copy(tempImage
, (GLushort
*) srcAddr
, elementCount
);
732 _mesa_swap4_copy(tempImage
, (GLuint
*) srcAddr
, elementCount
);
738 _mesa_image_row_stride(srcPacking
, srcWidth
, srcFormat
, srcType
);
741 _mesa_image_address(dims
, srcPacking
, srcAddr
, srcWidth
, srcHeight
,
742 srcFormat
, srcType
, 0, 0, 0);
744 srcMesaFormat
= _mesa_format_from_format_and_type(srcFormat
, srcType
);
745 dstFormat
= _mesa_get_srgb_format_linear(dstFormat
);
747 if (_mesa_get_format_base_format(dstFormat
) != baseInternalFormat
) {
749 _mesa_compute_rgba2base2rgba_component_mapping(baseInternalFormat
,
755 for (img
= 0; img
< srcDepth
; img
++) {
756 _mesa_format_convert(dstSlices
[img
], dstFormat
, dstRowStride
,
757 src
, srcMesaFormat
, srcRowStride
,
759 needRebase
? rebaseSwizzle
: NULL
);
760 src
+= srcHeight
* srcRowStride
;
769 _mesa_texstore_needs_transfer_ops(struct gl_context
*ctx
,
770 GLenum baseInternalFormat
,
771 mesa_format dstFormat
)
775 /* There are different rules depending on the base format. */
776 switch (baseInternalFormat
) {
777 case GL_DEPTH_COMPONENT
:
778 case GL_DEPTH_STENCIL
:
779 return ctx
->Pixel
.DepthScale
!= 1.0f
||
780 ctx
->Pixel
.DepthBias
!= 0.0f
;
782 case GL_STENCIL_INDEX
:
787 * Pixel transfer ops (scale, bias, table lookup) do not apply
788 * to integer formats.
790 dstType
= _mesa_get_format_datatype(dstFormat
);
792 return dstType
!= GL_INT
&& dstType
!= GL_UNSIGNED_INT
&&
793 ctx
->_ImageTransferState
;
799 _mesa_texstore_can_use_memcpy(struct gl_context
*ctx
,
800 GLenum baseInternalFormat
, mesa_format dstFormat
,
801 GLenum srcFormat
, GLenum srcType
,
802 const struct gl_pixelstore_attrib
*srcPacking
)
804 if (_mesa_texstore_needs_transfer_ops(ctx
, baseInternalFormat
, dstFormat
)) {
808 /* The base internal format and the base Mesa format must match. */
809 if (baseInternalFormat
!= _mesa_get_format_base_format(dstFormat
)) {
813 /* The Mesa format must match the input format and type. */
814 if (!_mesa_format_matches_format_and_type(dstFormat
, srcFormat
, srcType
,
815 srcPacking
->SwapBytes
)) {
819 /* Depth texture data needs clamping in following cases:
820 * - Floating point dstFormat with signed srcType: clamp to [0.0, 1.0].
821 * - Fixed point dstFormat with signed srcType: clamp to [0, 2^n -1].
823 * All the cases except one (float dstFormat with float srcType) are ruled
824 * out by _mesa_format_matches_format_and_type() check above. Handle the
825 * remaining case here.
827 if ((baseInternalFormat
== GL_DEPTH_COMPONENT
||
828 baseInternalFormat
== GL_DEPTH_STENCIL
) &&
829 (srcType
== GL_FLOAT
||
830 srcType
== GL_FLOAT_32_UNSIGNED_INT_24_8_REV
)) {
838 _mesa_texstore_memcpy(TEXSTORE_PARAMS
)
840 if (!_mesa_texstore_can_use_memcpy(ctx
, baseInternalFormat
, dstFormat
,
841 srcFormat
, srcType
, srcPacking
)) {
845 memcpy_texture(ctx
, dims
,
847 dstRowStride
, dstSlices
,
848 srcWidth
, srcHeight
, srcDepth
, srcFormat
, srcType
,
849 srcAddr
, srcPacking
);
853 * Store user data into texture memory.
854 * Called via glTex[Sub]Image1/2/3D()
855 * \return GL_TRUE for success, GL_FALSE for failure (out of memory).
858 _mesa_texstore(TEXSTORE_PARAMS
)
860 if (_mesa_texstore_memcpy(ctx
, dims
, baseInternalFormat
,
862 dstRowStride
, dstSlices
,
863 srcWidth
, srcHeight
, srcDepth
,
864 srcFormat
, srcType
, srcAddr
, srcPacking
)) {
868 if (_mesa_is_depth_or_stencil_format(baseInternalFormat
)) {
869 return texstore_depth_stencil(ctx
, dims
, baseInternalFormat
,
870 dstFormat
, dstRowStride
, dstSlices
,
871 srcWidth
, srcHeight
, srcDepth
,
872 srcFormat
, srcType
, srcAddr
, srcPacking
);
873 } else if (_mesa_is_format_compressed(dstFormat
)) {
874 return texstore_compressed(ctx
, dims
, baseInternalFormat
,
875 dstFormat
, dstRowStride
, dstSlices
,
876 srcWidth
, srcHeight
, srcDepth
,
877 srcFormat
, srcType
, srcAddr
, srcPacking
);
879 return texstore_rgba(ctx
, dims
, baseInternalFormat
,
880 dstFormat
, dstRowStride
, dstSlices
,
881 srcWidth
, srcHeight
, srcDepth
,
882 srcFormat
, srcType
, srcAddr
, srcPacking
);
888 * Normally, we'll only _write_ texel data to a texture when we map it.
889 * But if the user is providing depth or stencil values and the texture
890 * image is a combined depth/stencil format, we'll actually read from
891 * the texture buffer too (in order to insert the depth or stencil values.
892 * \param userFormat the user-provided image format
893 * \param texFormat the destination texture format
896 get_read_write_mode(GLenum userFormat
, mesa_format texFormat
)
898 if ((userFormat
== GL_STENCIL_INDEX
|| userFormat
== GL_DEPTH_COMPONENT
)
899 && _mesa_get_format_base_format(texFormat
) == GL_DEPTH_STENCIL
)
900 return GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
;
902 return GL_MAP_WRITE_BIT
| GL_MAP_INVALIDATE_RANGE_BIT
;
907 * Helper function for storing 1D, 2D, 3D whole and subimages into texture
909 * The source of the image data may be user memory or a PBO. In the later
910 * case, we'll map the PBO, copy from it, then unmap it.
913 store_texsubimage(struct gl_context
*ctx
,
914 struct gl_texture_image
*texImage
,
915 GLint xoffset
, GLint yoffset
, GLint zoffset
,
916 GLint width
, GLint height
, GLint depth
,
917 GLenum format
, GLenum type
, const GLvoid
*pixels
,
918 const struct gl_pixelstore_attrib
*packing
,
922 const GLbitfield mapMode
= get_read_write_mode(format
, texImage
->TexFormat
);
923 const GLenum target
= texImage
->TexObject
->Target
;
924 GLboolean success
= GL_FALSE
;
925 GLuint dims
, slice
, numSlices
= 1, sliceOffset
= 0;
926 GLint srcImageStride
= 0;
929 assert(xoffset
+ width
<= texImage
->Width
);
930 assert(yoffset
+ height
<= texImage
->Height
);
931 assert(zoffset
+ depth
<= texImage
->Depth
);
937 case GL_TEXTURE_2D_ARRAY
:
938 case GL_TEXTURE_CUBE_MAP_ARRAY
:
946 /* get pointer to src pixels (may be in a pbo which we'll map here) */
947 src
= (const GLubyte
*)
948 _mesa_validate_pbo_teximage(ctx
, dims
, width
, height
, depth
,
949 format
, type
, pixels
, packing
, caller
);
953 /* compute slice info (and do some sanity checks) */
956 case GL_TEXTURE_RECTANGLE
:
957 case GL_TEXTURE_CUBE_MAP
:
958 case GL_TEXTURE_EXTERNAL_OES
:
959 /* one image slice, nothing special needs to be done */
964 assert(yoffset
== 0);
965 assert(zoffset
== 0);
967 case GL_TEXTURE_1D_ARRAY
:
969 assert(zoffset
== 0);
971 sliceOffset
= yoffset
;
974 srcImageStride
= _mesa_image_row_stride(packing
, width
, format
, type
);
976 case GL_TEXTURE_2D_ARRAY
:
978 sliceOffset
= zoffset
;
981 srcImageStride
= _mesa_image_image_stride(packing
, width
, height
,
985 /* we'll store 3D images as a series of slices */
987 sliceOffset
= zoffset
;
988 srcImageStride
= _mesa_image_image_stride(packing
, width
, height
,
991 case GL_TEXTURE_CUBE_MAP_ARRAY
:
993 sliceOffset
= zoffset
;
994 srcImageStride
= _mesa_image_image_stride(packing
, width
, height
,
998 _mesa_warning(ctx
, "Unexpected target 0x%x in store_texsubimage()", target
);
1002 assert(numSlices
== 1 || srcImageStride
!= 0);
1004 for (slice
= 0; slice
< numSlices
; slice
++) {
1008 ctx
->Driver
.MapTextureImage(ctx
, texImage
,
1009 slice
+ sliceOffset
,
1010 xoffset
, yoffset
, width
, height
,
1011 mapMode
, &dstMap
, &dstRowStride
);
1013 /* Note: we're only storing a 2D (or 1D) slice at a time but we need
1014 * to pass the right 'dims' value so that GL_UNPACK_SKIP_IMAGES is
1015 * used for 3D images.
1017 success
= _mesa_texstore(ctx
, dims
, texImage
->_BaseFormat
,
1018 texImage
->TexFormat
,
1021 width
, height
, 1, /* w, h, d */
1022 format
, type
, src
, packing
);
1024 ctx
->Driver
.UnmapTextureImage(ctx
, texImage
, slice
+ sliceOffset
);
1027 src
+= srcImageStride
;
1034 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "%s", caller
);
1036 _mesa_unmap_teximage_pbo(ctx
, packing
);
1042 * Fallback code for ctx->Driver.TexImage().
1043 * Basically, allocate storage for the texture image, then copy the
1044 * user's image into it.
1047 _mesa_store_teximage(struct gl_context
*ctx
,
1049 struct gl_texture_image
*texImage
,
1050 GLenum format
, GLenum type
, const GLvoid
*pixels
,
1051 const struct gl_pixelstore_attrib
*packing
)
1053 assert(dims
== 1 || dims
== 2 || dims
== 3);
1055 if (texImage
->Width
== 0 || texImage
->Height
== 0 || texImage
->Depth
== 0)
1058 /* allocate storage for texture data */
1059 if (!ctx
->Driver
.AllocTextureImageBuffer(ctx
, texImage
)) {
1060 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glTexImage%uD", dims
);
1064 store_texsubimage(ctx
, texImage
,
1065 0, 0, 0, texImage
->Width
, texImage
->Height
, texImage
->Depth
,
1066 format
, type
, pixels
, packing
, "glTexImage");
1071 * Fallback for Driver.TexSubImage().
1074 _mesa_store_texsubimage(struct gl_context
*ctx
, GLuint dims
,
1075 struct gl_texture_image
*texImage
,
1076 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1077 GLint width
, GLint height
, GLint depth
,
1078 GLenum format
, GLenum type
, const void *pixels
,
1079 const struct gl_pixelstore_attrib
*packing
)
1081 store_texsubimage(ctx
, texImage
,
1082 xoffset
, yoffset
, zoffset
, width
, height
, depth
,
1083 format
, type
, pixels
, packing
, "glTexSubImage");
1087 clear_image_to_zero(GLubyte
*dstMap
, GLint dstRowStride
,
1088 GLsizei width
, GLsizei height
,
1089 GLsizei clearValueSize
)
1093 for (y
= 0; y
< height
; y
++) {
1094 memset(dstMap
, 0, clearValueSize
* width
);
1095 dstMap
+= dstRowStride
;
1100 clear_image_to_value(GLubyte
*dstMap
, GLint dstRowStride
,
1101 GLsizei width
, GLsizei height
,
1102 const GLvoid
*clearValue
,
1103 GLsizei clearValueSize
)
1107 for (y
= 0; y
< height
; y
++) {
1108 for (x
= 0; x
< width
; x
++) {
1109 memcpy(dstMap
, clearValue
, clearValueSize
);
1110 dstMap
+= clearValueSize
;
1112 dstMap
+= dstRowStride
- clearValueSize
* width
;
1117 * Fallback for Driver.ClearTexSubImage().
1120 _mesa_store_cleartexsubimage(struct gl_context
*ctx
,
1121 struct gl_texture_image
*texImage
,
1122 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1123 GLsizei width
, GLsizei height
, GLsizei depth
,
1124 const GLvoid
*clearValue
)
1128 GLsizeiptr clearValueSize
;
1131 clearValueSize
= _mesa_get_format_bytes(texImage
->TexFormat
);
1133 for (z
= 0; z
< depth
; z
++) {
1134 ctx
->Driver
.MapTextureImage(ctx
, texImage
,
1135 z
+ zoffset
, xoffset
, yoffset
,
1138 &dstMap
, &dstRowStride
);
1139 if (dstMap
== NULL
) {
1140 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glClearTex*Image");
1145 clear_image_to_value(dstMap
, dstRowStride
,
1150 clear_image_to_zero(dstMap
, dstRowStride
,
1155 ctx
->Driver
.UnmapTextureImage(ctx
, texImage
, z
+ zoffset
);
1160 * Fallback for Driver.CompressedTexImage()
1163 _mesa_store_compressed_teximage(struct gl_context
*ctx
, GLuint dims
,
1164 struct gl_texture_image
*texImage
,
1165 GLsizei imageSize
, const GLvoid
*data
)
1167 /* only 2D and 3D compressed images are supported at this time */
1169 _mesa_problem(ctx
, "Unexpected glCompressedTexImage1D call");
1173 /* This is pretty simple, because unlike the general texstore path we don't
1174 * have to worry about the usual image unpacking or image transfer
1178 ASSERT(texImage
->Width
> 0);
1179 ASSERT(texImage
->Height
> 0);
1180 ASSERT(texImage
->Depth
> 0);
1182 /* allocate storage for texture data */
1183 if (!ctx
->Driver
.AllocTextureImageBuffer(ctx
, texImage
)) {
1184 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCompressedTexImage%uD", dims
);
1188 _mesa_store_compressed_texsubimage(ctx
, dims
, texImage
,
1190 texImage
->Width
, texImage
->Height
, texImage
->Depth
,
1191 texImage
->TexFormat
,
1197 * Compute compressed_pixelstore parameters for copying compressed
1199 * \param dims number of texture image dimensions: 1, 2 or 3
1200 * \param texFormat the compressed texture format
1201 * \param width, height, depth size of image to copy
1202 * \param packing pixelstore parameters describing user-space image packing
1203 * \param store returns the compressed_pixelstore parameters
1206 _mesa_compute_compressed_pixelstore(GLuint dims
, mesa_format texFormat
,
1207 GLsizei width
, GLsizei height
,
1209 const struct gl_pixelstore_attrib
*packing
,
1210 struct compressed_pixelstore
*store
)
1214 _mesa_get_format_block_size(texFormat
, &bw
, &bh
);
1216 store
->SkipBytes
= 0;
1217 store
->TotalBytesPerRow
= store
->CopyBytesPerRow
=
1218 _mesa_format_row_stride(texFormat
, width
);
1219 store
->TotalRowsPerSlice
= store
->CopyRowsPerSlice
=
1220 (height
+ bh
- 1) / bh
;
1221 store
->CopySlices
= depth
;
1223 if (packing
->CompressedBlockWidth
&&
1224 packing
->CompressedBlockSize
) {
1226 bw
= packing
->CompressedBlockWidth
;
1228 if (packing
->RowLength
) {
1229 store
->TotalBytesPerRow
= packing
->CompressedBlockSize
*
1230 ((packing
->RowLength
+ bw
- 1) / bw
);
1233 store
->SkipBytes
+= packing
->SkipPixels
* packing
->CompressedBlockSize
/ bw
;
1236 if (dims
> 1 && packing
->CompressedBlockHeight
&&
1237 packing
->CompressedBlockSize
) {
1239 bh
= packing
->CompressedBlockHeight
;
1241 store
->SkipBytes
+= packing
->SkipRows
* store
->TotalBytesPerRow
/ bh
;
1242 store
->CopyRowsPerSlice
= (height
+ bh
- 1) / bh
; /* rows in blocks */
1244 if (packing
->ImageHeight
) {
1245 store
->TotalRowsPerSlice
= (packing
->ImageHeight
+ bh
- 1) / bh
;
1249 if (dims
> 2 && packing
->CompressedBlockDepth
&&
1250 packing
->CompressedBlockSize
) {
1252 int bd
= packing
->CompressedBlockDepth
;
1254 store
->SkipBytes
+= packing
->SkipImages
* store
->TotalBytesPerRow
*
1255 store
->TotalRowsPerSlice
/ bd
;
1261 * Fallback for Driver.CompressedTexSubImage()
1264 _mesa_store_compressed_texsubimage(struct gl_context
*ctx
, GLuint dims
,
1265 struct gl_texture_image
*texImage
,
1266 GLint xoffset
, GLint yoffset
, GLint zoffset
,
1267 GLsizei width
, GLsizei height
, GLsizei depth
,
1269 GLsizei imageSize
, const GLvoid
*data
)
1271 struct compressed_pixelstore store
;
1278 _mesa_problem(ctx
, "Unexpected 1D compressed texsubimage call");
1282 _mesa_compute_compressed_pixelstore(dims
, texImage
->TexFormat
,
1283 width
, height
, depth
,
1284 &ctx
->Unpack
, &store
);
1286 /* get pointer to src pixels (may be in a pbo which we'll map here) */
1287 data
= _mesa_validate_pbo_compressed_teximage(ctx
, dims
, imageSize
, data
,
1289 "glCompressedTexSubImage");
1293 src
= (const GLubyte
*) data
+ store
.SkipBytes
;
1295 for (slice
= 0; slice
< store
.CopySlices
; slice
++) {
1296 /* Map dest texture buffer */
1297 ctx
->Driver
.MapTextureImage(ctx
, texImage
, slice
+ zoffset
,
1298 xoffset
, yoffset
, width
, height
,
1299 GL_MAP_WRITE_BIT
| GL_MAP_INVALIDATE_RANGE_BIT
,
1300 &dstMap
, &dstRowStride
);
1304 /* copy rows of blocks */
1305 for (i
= 0; i
< store
.CopyRowsPerSlice
; i
++) {
1306 memcpy(dstMap
, src
, store
.CopyBytesPerRow
);
1307 dstMap
+= dstRowStride
;
1308 src
+= store
.TotalBytesPerRow
;
1311 ctx
->Driver
.UnmapTextureImage(ctx
, texImage
, slice
+ zoffset
);
1313 /* advance to next slice */
1314 src
+= store
.TotalBytesPerRow
* (store
.TotalRowsPerSlice
- store
.CopyRowsPerSlice
);
1317 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCompressedTexSubImage%uD",
1322 _mesa_unmap_teximage_pbo(ctx
, &ctx
->Unpack
);