2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2006 Brian Paul All Rights Reserved.
6 * Copyright (c) 2008 VMware, Inc.
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
15 * The above copyright notice and this permission notice shall be included
16 * in all copies or substantial portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
22 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 * Helper functions for texture compression.
39 #include "texcompress.h"
40 #include "texformat.h"
45 * Return list of (and count of) all specific texture compression
46 * formats that are supported.
48 * \param ctx the GL context
49 * \param formats the resulting format list (may be NULL).
50 * \param all if true return all formats, even those with some kind
51 * of restrictions/limitations (See GL_ARB_texture_compression
52 * spec for more info).
54 * \return number of formats.
57 _mesa_get_compressed_formats(GLcontext
*ctx
, GLint
*formats
, GLboolean all
)
60 if (ctx
->Extensions
.TDFX_texture_compression_FXT1
) {
62 formats
[n
++] = GL_COMPRESSED_RGB_FXT1_3DFX
;
63 formats
[n
++] = GL_COMPRESSED_RGBA_FXT1_3DFX
;
69 if (ctx
->Extensions
.EXT_texture_compression_s3tc
) {
71 formats
[n
++] = GL_COMPRESSED_RGB_S3TC_DXT1_EXT
;
72 /* This format has some restrictions/limitations and so should
73 * not be returned via the GL_COMPRESSED_TEXTURE_FORMATS query.
74 * Specifically, all transparent pixels become black. NVIDIA
75 * omits this format too.
78 formats
[n
++] = GL_COMPRESSED_RGBA_S3TC_DXT1_EXT
;
79 formats
[n
++] = GL_COMPRESSED_RGBA_S3TC_DXT3_EXT
;
80 formats
[n
++] = GL_COMPRESSED_RGBA_S3TC_DXT5_EXT
;
88 if (ctx
->Extensions
.S3_s3tc
) {
90 formats
[n
++] = GL_RGB_S3TC
;
91 formats
[n
++] = GL_RGB4_S3TC
;
92 formats
[n
++] = GL_RGBA_S3TC
;
93 formats
[n
++] = GL_RGBA4_S3TC
;
99 #if FEATURE_EXT_texture_sRGB
100 if (ctx
->Extensions
.EXT_texture_sRGB
) {
102 formats
[n
++] = GL_COMPRESSED_SRGB_S3TC_DXT1_EXT
;
103 formats
[n
++] = GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT
;
104 formats
[n
++] = GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT
;
105 formats
[n
++] = GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT
;
111 #endif /* FEATURE_EXT_texture_sRGB */
118 * Return number of bytes needed to store a texture of the given size
119 * using the specified compressed format.
120 * This is called via the ctx->Driver.CompressedTextureSize function,
121 * unless a device driver overrides it.
123 * \param width texture width in texels.
124 * \param height texture height in texels.
125 * \param depth texture depth in texels.
126 * \param mesaFormat one of the MESA_FORMAT_* compressed formats
128 * \return size in bytes, or zero if bad format
131 _mesa_compressed_texture_size( GLcontext
*ctx
,
132 GLsizei width
, GLsizei height
, GLsizei depth
,
141 switch (mesaFormat
) {
142 #if FEATURE_texture_fxt1
143 case MESA_FORMAT_RGB_FXT1
:
144 case MESA_FORMAT_RGBA_FXT1
:
145 /* round up width to next multiple of 8, height to next multiple of 4 */
146 width
= (width
+ 7) & ~7;
147 height
= (height
+ 3) & ~3;
148 /* 16 bytes per 8x4 tile of RGB[A] texels */
149 size
= width
* height
/ 2;
150 /* Textures smaller than 8x4 will effectively be made into 8x4 and
155 #if FEATURE_texture_s3tc
156 case MESA_FORMAT_RGB_DXT1
:
157 case MESA_FORMAT_RGBA_DXT1
:
158 #if FEATURE_EXT_texture_sRGB
159 case MESA_FORMAT_SRGB_DXT1
:
160 case MESA_FORMAT_SRGBA_DXT1
:
162 /* round up width, height to next multiple of 4 */
163 width
= (width
+ 3) & ~3;
164 height
= (height
+ 3) & ~3;
165 /* 8 bytes per 4x4 tile of RGB[A] texels */
166 size
= width
* height
/ 2;
167 /* Textures smaller than 4x4 will effectively be made into 4x4 and
171 case MESA_FORMAT_RGBA_DXT3
:
172 case MESA_FORMAT_RGBA_DXT5
:
173 #if FEATURE_EXT_texture_sRGB
174 case MESA_FORMAT_SRGBA_DXT3
:
175 case MESA_FORMAT_SRGBA_DXT5
:
177 /* round up width, height to next multiple of 4 */
178 width
= (width
+ 3) & ~3;
179 height
= (height
+ 3) & ~3;
180 /* 16 bytes per 4x4 tile of RGBA texels */
181 size
= width
* height
; /* simple! */
182 /* Textures smaller than 4x4 will effectively be made into 4x4 and
188 _mesa_problem(ctx
, "bad mesaFormat in _mesa_compressed_texture_size");
195 * As above, but format is specified by a GLenum (GL_COMPRESSED_*) token.
197 * Note: This function CAN NOT return a padded hardware texture size.
198 * That's why we don't call the ctx->Driver.CompressedTextureSize() function.
200 * We use this function to validate the <imageSize> parameter
201 * of glCompressedTex[Sub]Image1/2/3D(), which must be an exact match.
204 _mesa_compressed_texture_size_glenum(GLcontext
*ctx
,
205 GLsizei width
, GLsizei height
,
206 GLsizei depth
, GLenum glformat
)
211 #if FEATURE_texture_fxt1
212 case GL_COMPRESSED_RGB_FXT1_3DFX
:
213 mesaFormat
= MESA_FORMAT_RGB_FXT1
;
215 case GL_COMPRESSED_RGBA_FXT1_3DFX
:
216 mesaFormat
= MESA_FORMAT_RGBA_FXT1
;
219 #if FEATURE_texture_s3tc
220 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT
:
222 mesaFormat
= MESA_FORMAT_RGB_DXT1
;
224 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT
:
226 mesaFormat
= MESA_FORMAT_RGBA_DXT1
;
228 case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT
:
230 mesaFormat
= MESA_FORMAT_RGBA_DXT3
;
232 case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT
:
234 mesaFormat
= MESA_FORMAT_RGBA_DXT5
;
236 #if FEATURE_EXT_texture_sRGB
237 case GL_COMPRESSED_SRGB_S3TC_DXT1_EXT
:
238 mesaFormat
= MESA_FORMAT_SRGB_DXT1
;
240 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT
:
241 mesaFormat
= MESA_FORMAT_SRGBA_DXT1
;
243 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT
:
244 mesaFormat
= MESA_FORMAT_SRGBA_DXT3
;
246 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT
:
247 mesaFormat
= MESA_FORMAT_SRGBA_DXT5
;
255 return _mesa_compressed_texture_size(ctx
, width
, height
, depth
, mesaFormat
);
260 * Compute the bytes per row in a compressed texture image.
261 * We use this for computing the destination address for sub-texture updates.
262 * \param mesaFormat one of the MESA_FORMAT_* compressed formats
263 * \param width image width in pixels
264 * \return stride, in bytes, between rows for compressed image
267 _mesa_compressed_row_stride(GLuint mesaFormat
, GLsizei width
)
271 switch (mesaFormat
) {
272 #if FEATURE_texture_fxt1
273 case MESA_FORMAT_RGB_FXT1
:
274 case MESA_FORMAT_RGBA_FXT1
:
275 stride
= ((width
+ 7) / 8) * 16; /* 16 bytes per 8x4 tile */
278 #if FEATURE_texture_s3tc
279 case MESA_FORMAT_RGB_DXT1
:
280 case MESA_FORMAT_RGBA_DXT1
:
281 #if FEATURE_EXT_texture_sRGB
282 case MESA_FORMAT_SRGB_DXT1
:
283 case MESA_FORMAT_SRGBA_DXT1
:
285 stride
= ((width
+ 3) / 4) * 8; /* 8 bytes per 4x4 tile */
287 case MESA_FORMAT_RGBA_DXT3
:
288 case MESA_FORMAT_RGBA_DXT5
:
289 #if FEATURE_EXT_texture_sRGB
290 case MESA_FORMAT_SRGBA_DXT3
:
291 case MESA_FORMAT_SRGBA_DXT5
:
293 stride
= ((width
+ 3) / 4) * 16; /* 16 bytes per 4x4 tile */
297 _mesa_problem(NULL
, "bad mesaFormat in _mesa_compressed_row_stride");
306 * Return the address of the pixel at (col, row, img) in a
307 * compressed texture image.
308 * \param col, row, img - image position (3D)
309 * \param format - compressed image format
310 * \param width - image width
311 * \param image - the image address
312 * \return address of pixel at (row, col)
315 _mesa_compressed_image_address(GLint col
, GLint row
, GLint img
,
317 GLsizei width
, const GLubyte
*image
)
323 /* We try to spot a "complete" subtexture "above" ROW, COL;
324 * this texture is given by appropriate rounding of WIDTH x ROW.
325 * Then we just add the amount left (usually on the left).
327 * Example for X*Y microtiles (Z bytes each)
328 * offset = Z * (((width + X - 1) / X) * (row / Y) + col / X);
331 switch (mesaFormat
) {
332 #if FEATURE_texture_fxt1
333 case MESA_FORMAT_RGB_FXT1
:
334 case MESA_FORMAT_RGBA_FXT1
:
335 addr
= (GLubyte
*) image
+ 16 * (((width
+ 7) / 8) * (row
/ 4) + col
/ 8);
338 #if FEATURE_texture_s3tc
339 case MESA_FORMAT_RGB_DXT1
:
340 case MESA_FORMAT_RGBA_DXT1
:
341 #if FEATURE_EXT_texture_sRGB
342 case MESA_FORMAT_SRGB_DXT1
:
343 case MESA_FORMAT_SRGBA_DXT1
:
345 addr
= (GLubyte
*) image
+ 8 * (((width
+ 3) / 4) * (row
/ 4) + col
/ 4);
347 case MESA_FORMAT_RGBA_DXT3
:
348 case MESA_FORMAT_RGBA_DXT5
:
349 #if FEATURE_EXT_texture_sRGB
350 case MESA_FORMAT_SRGBA_DXT3
:
351 case MESA_FORMAT_SRGBA_DXT5
:
353 addr
= (GLubyte
*) image
+ 16 * (((width
+ 3) / 4) * (row
/ 4) + col
/ 4);
357 _mesa_problem(NULL
, "bad mesaFormat in _mesa_compressed_image_address");
366 * Given a compressed MESA_FORMAT_x value, return the corresponding
367 * GLenum for that format.
368 * This is needed for glGetTexLevelParameter(GL_TEXTURE_INTERNAL_FORMAT)
369 * which must return the specific texture format used when the user might
370 * have originally specified a generic compressed format in their
371 * glTexImage2D() call.
372 * For non-compressed textures, we always return the user-specified
373 * internal format unchanged.
376 _mesa_compressed_format_to_glenum(GLcontext
*ctx
, GLuint mesaFormat
)
378 switch (mesaFormat
) {
379 #if FEATURE_texture_fxt1
380 case MESA_FORMAT_RGB_FXT1
:
381 return GL_COMPRESSED_RGB_FXT1_3DFX
;
382 case MESA_FORMAT_RGBA_FXT1
:
383 return GL_COMPRESSED_RGBA_FXT1_3DFX
;
385 #if FEATURE_texture_s3tc
386 case MESA_FORMAT_RGB_DXT1
:
387 return GL_COMPRESSED_RGB_S3TC_DXT1_EXT
;
388 case MESA_FORMAT_RGBA_DXT1
:
389 return GL_COMPRESSED_RGBA_S3TC_DXT1_EXT
;
390 case MESA_FORMAT_RGBA_DXT3
:
391 return GL_COMPRESSED_RGBA_S3TC_DXT3_EXT
;
392 case MESA_FORMAT_RGBA_DXT5
:
393 return GL_COMPRESSED_RGBA_S3TC_DXT5_EXT
;
394 #if FEATURE_EXT_texture_sRGB
395 case MESA_FORMAT_SRGB_DXT1
:
396 return GL_COMPRESSED_SRGB_S3TC_DXT1_EXT
;
397 case MESA_FORMAT_SRGBA_DXT1
:
398 return GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT
;
399 case MESA_FORMAT_SRGBA_DXT3
:
400 return GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT
;
401 case MESA_FORMAT_SRGBA_DXT5
:
402 return GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT
;
406 _mesa_problem(ctx
, "Unexpected mesa texture format in"
407 " _mesa_compressed_format_to_glenum()");