2 * Mesa 3-D graphics library
4 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
5 * Copyright (c) 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.
30 * Texel fetch/store functions
32 * \author Gareth Hughes
36 #include "main/colormac.h"
37 #include "main/macros.h"
38 #include "main/texcompress.h"
39 #include "main/texcompress_fxt1.h"
40 #include "main/texcompress_s3tc.h"
41 #include "main/texcompress_rgtc.h"
42 #include "main/texcompress_etc.h"
43 #include "main/teximage.h"
44 #include "main/samplerobj.h"
45 #include "s_context.h"
46 #include "s_texfetch.h"
47 #include "../../gallium/auxiliary/util/u_format_rgb9e5.h"
48 #include "../../gallium/auxiliary/util/u_format_r11g11b10f.h"
52 * Convert an 8-bit sRGB value from non-linear space to a
53 * linear RGB value in [0, 1].
54 * Implemented with a 256-entry lookup table.
57 nonlinear_to_linear(GLubyte cs8
)
59 static GLfloat table
[256];
60 static GLboolean tableReady
= GL_FALSE
;
62 /* compute lookup table now */
64 for (i
= 0; i
< 256; i
++) {
65 const GLfloat cs
= UBYTE_TO_FLOAT(i
);
67 table
[i
] = cs
/ 12.92f
;
70 table
[i
] = (GLfloat
) pow((cs
+ 0.055) / 1.055, 2.4);
80 /* Texel fetch routines for all supported formats
83 #include "s_texfetch_tmp.h"
86 #include "s_texfetch_tmp.h"
89 #include "s_texfetch_tmp.h"
93 * All compressed texture texel fetching is done though this function.
94 * Basically just call a core-Mesa texel fetch function.
97 fetch_compressed(const struct swrast_texture_image
*swImage
,
98 GLint i
, GLint j
, GLint k
, GLfloat
*texel
)
100 /* The FetchCompressedTexel function takes an integer pixel rowstride,
101 * while the image's rowstride is bytes per row of blocks.
104 GLuint texelBytes
= _mesa_get_format_bytes(swImage
->Base
.TexFormat
);
105 _mesa_get_format_block_size(swImage
->Base
.TexFormat
, &bw
, &bh
);
106 assert(swImage
->RowStride
* bw
% texelBytes
== 0);
108 swImage
->FetchCompressedTexel(swImage
->ImageSlices
[k
],
109 swImage
->RowStride
* bw
/ texelBytes
,
116 * Null texel fetch function.
118 * Have to have this so the FetchTexel function pointer is never NULL.
120 static void fetch_null_texelf( const struct swrast_texture_image
*texImage
,
121 GLint i
, GLint j
, GLint k
, GLfloat
*texel
)
123 (void) texImage
; (void) i
; (void) j
; (void) k
;
128 _mesa_warning(NULL
, "fetch_null_texelf() called!");
132 #define FETCH_FUNCS(NAME) \
134 MESA_FORMAT_ ## NAME, \
135 fetch_texel_1d_ ## NAME, \
136 fetch_texel_2d_ ## NAME, \
137 fetch_texel_3d_ ## NAME, \
140 #define FETCH_NULL(NAME) \
142 MESA_FORMAT_ ## NAME, \
149 * Table to map MESA_FORMAT_ to texel fetch/store funcs.
153 FetchTexelFunc Fetch1D
;
154 FetchTexelFunc Fetch2D
;
155 FetchTexelFunc Fetch3D
;
166 /* Packed unorm formats */
167 FETCH_FUNCS(A8B8G8R8_UNORM
),
168 FETCH_FUNCS(X8B8G8R8_UNORM
),
169 FETCH_FUNCS(R8G8B8A8_UNORM
),
170 FETCH_FUNCS(R8G8B8X8_UNORM
),
171 FETCH_FUNCS(B8G8R8A8_UNORM
),
172 FETCH_FUNCS(B8G8R8X8_UNORM
),
173 FETCH_FUNCS(A8R8G8B8_UNORM
),
174 FETCH_FUNCS(X8R8G8B8_UNORM
),
175 FETCH_FUNCS(L16A16_UNORM
),
176 FETCH_FUNCS(A16L16_UNORM
),
177 FETCH_FUNCS(B5G6R5_UNORM
),
178 FETCH_FUNCS(R5G6B5_UNORM
),
179 FETCH_FUNCS(B4G4R4A4_UNORM
),
180 FETCH_NULL(B4G4R4X4_UNORM
),
181 FETCH_FUNCS(A4R4G4B4_UNORM
),
182 FETCH_FUNCS(A1B5G5R5_UNORM
),
183 FETCH_FUNCS(B5G5R5A1_UNORM
),
184 FETCH_NULL(B5G5R5X1_UNORM
),
185 FETCH_FUNCS(A1R5G5B5_UNORM
),
186 FETCH_FUNCS(L8A8_UNORM
),
187 FETCH_FUNCS(A8L8_UNORM
),
188 FETCH_FUNCS(R8G8_UNORM
),
189 FETCH_FUNCS(G8R8_UNORM
),
190 FETCH_FUNCS(L4A4_UNORM
),
191 FETCH_FUNCS(B2G3R3_UNORM
),
192 FETCH_FUNCS(R16G16_UNORM
),
193 FETCH_FUNCS(G16R16_UNORM
),
194 FETCH_FUNCS(B10G10R10A2_UNORM
),
195 FETCH_NULL(B10G10R10X2_UNORM
),
196 FETCH_FUNCS(R10G10B10A2_UNORM
),
197 FETCH_FUNCS(S8_UINT_Z24_UNORM
),
199 MESA_FORMAT_X8_UINT_Z24_UNORM
,
200 fetch_texel_1d_S8_UINT_Z24_UNORM
,
201 fetch_texel_2d_S8_UINT_Z24_UNORM
,
202 fetch_texel_3d_S8_UINT_Z24_UNORM
204 FETCH_FUNCS(Z24_UNORM_S8_UINT
),
206 MESA_FORMAT_Z24_UNORM_X8_UINT
,
207 fetch_texel_1d_Z24_UNORM_S8_UINT
,
208 fetch_texel_2d_Z24_UNORM_S8_UINT
,
209 fetch_texel_3d_Z24_UNORM_S8_UINT
212 FETCH_FUNCS(YCBCR_REV
),
214 /* Array unorm formats */
215 FETCH_FUNCS(A_UNORM8
),
216 FETCH_FUNCS(A_UNORM16
),
217 FETCH_FUNCS(L_UNORM8
),
218 FETCH_FUNCS(L_UNORM16
),
219 FETCH_FUNCS(I_UNORM8
),
220 FETCH_FUNCS(I_UNORM16
),
221 FETCH_FUNCS(R_UNORM8
),
222 FETCH_FUNCS(R_UNORM16
),
223 FETCH_FUNCS(BGR_UNORM8
),
224 FETCH_FUNCS(RGB_UNORM8
),
225 FETCH_FUNCS(RGBA_UNORM16
),
226 FETCH_FUNCS(RGBX_UNORM16
),
227 FETCH_FUNCS(Z_UNORM16
),
228 FETCH_FUNCS(Z_UNORM32
),
231 /* Packed signed/normalized formats */
232 FETCH_FUNCS(A8B8G8R8_SNORM
),
233 FETCH_FUNCS(X8B8G8R8_SNORM
),
234 FETCH_FUNCS(R8G8B8A8_SNORM
),
235 FETCH_NULL(R8G8B8X8_SNORM
),
236 FETCH_FUNCS(R16G16_SNORM
),
237 FETCH_NULL(G16R16_SNORM
),
238 FETCH_FUNCS(R8G8_SNORM
),
239 FETCH_NULL(G8R8_SNORM
),
240 FETCH_FUNCS(L8A8_SNORM
),
242 /* Array signed/normalized formats */
243 FETCH_FUNCS(A_SNORM8
),
244 FETCH_FUNCS(A_SNORM16
),
245 FETCH_FUNCS(L_SNORM8
),
246 FETCH_FUNCS(L_SNORM16
),
247 FETCH_FUNCS(I_SNORM8
),
248 FETCH_FUNCS(I_SNORM16
),
249 FETCH_FUNCS(R_SNORM8
),
250 FETCH_FUNCS(R_SNORM16
),
251 FETCH_FUNCS(LA_SNORM16
),
252 FETCH_FUNCS(RGB_SNORM16
),
253 FETCH_FUNCS(RGBA_SNORM16
),
254 FETCH_NULL(RGBX_SNORM16
),
256 /* Packed sRGB formats */
257 FETCH_FUNCS(A8B8G8R8_SRGB
),
258 FETCH_FUNCS(B8G8R8A8_SRGB
),
259 FETCH_NULL(B8G8R8X8_SRGB
),
260 FETCH_FUNCS(R8G8B8A8_SRGB
),
261 FETCH_FUNCS(R8G8B8X8_SRGB
),
262 FETCH_FUNCS(L8A8_SRGB
),
264 /* Array sRGB formats */
265 FETCH_FUNCS(L_SRGB8
),
266 FETCH_FUNCS(BGR_SRGB8
),
268 /* Packed float formats */
269 FETCH_FUNCS(R9G9B9E5_FLOAT
),
270 FETCH_FUNCS(R11G11B10_FLOAT
),
271 FETCH_FUNCS(Z32_FLOAT_S8X24_UINT
),
273 /* Array float formats */
274 FETCH_FUNCS(A_FLOAT16
),
275 FETCH_FUNCS(A_FLOAT32
),
276 FETCH_FUNCS(L_FLOAT16
),
277 FETCH_FUNCS(L_FLOAT32
),
278 FETCH_FUNCS(LA_FLOAT16
),
279 FETCH_FUNCS(LA_FLOAT32
),
280 FETCH_FUNCS(I_FLOAT16
),
281 FETCH_FUNCS(I_FLOAT32
),
282 FETCH_FUNCS(R_FLOAT16
),
283 FETCH_FUNCS(R_FLOAT32
),
284 FETCH_FUNCS(RG_FLOAT16
),
285 FETCH_FUNCS(RG_FLOAT32
),
286 FETCH_FUNCS(RGB_FLOAT16
),
287 FETCH_FUNCS(RGB_FLOAT32
),
288 FETCH_FUNCS(RGBA_FLOAT16
),
289 FETCH_FUNCS(RGBA_FLOAT32
),
290 FETCH_FUNCS(RGBX_FLOAT16
),
291 FETCH_FUNCS(RGBX_FLOAT32
),
293 MESA_FORMAT_Z_FLOAT32
,
294 fetch_texel_1d_R_FLOAT32
, /* Reuse the R32F functions. */
295 fetch_texel_2d_R_FLOAT32
,
296 fetch_texel_3d_R_FLOAT32
299 /* Packed signed/unsigned non-normalized integer formats */
300 FETCH_NULL(B10G10R10A2_UINT
),
301 FETCH_NULL(R10G10B10A2_UINT
),
303 /* Array signed/unsigned non-normalized integer formats */
305 FETCH_NULL(A_UINT16
),
306 FETCH_NULL(A_UINT32
),
308 FETCH_NULL(A_SINT16
),
309 FETCH_NULL(A_SINT32
),
311 FETCH_NULL(I_UINT16
),
312 FETCH_NULL(I_UINT32
),
314 FETCH_NULL(I_SINT16
),
315 FETCH_NULL(I_SINT32
),
317 FETCH_NULL(L_UINT16
),
318 FETCH_NULL(L_UINT32
),
320 FETCH_NULL(L_SINT16
),
321 FETCH_NULL(L_SINT32
),
322 FETCH_NULL(LA_UINT8
),
323 FETCH_NULL(LA_UINT16
),
324 FETCH_NULL(LA_UINT32
),
325 FETCH_NULL(LA_SINT8
),
326 FETCH_NULL(LA_SINT16
),
327 FETCH_NULL(LA_SINT32
),
329 FETCH_NULL(R_UINT16
),
330 FETCH_NULL(R_UINT32
),
332 FETCH_NULL(R_SINT16
),
333 FETCH_NULL(R_SINT32
),
334 FETCH_NULL(RG_UINT8
),
335 FETCH_NULL(RG_UINT16
),
336 FETCH_NULL(RG_UINT32
),
337 FETCH_NULL(RG_SINT8
),
338 FETCH_NULL(RG_SINT16
),
339 FETCH_NULL(RG_SINT32
),
340 FETCH_NULL(RGB_UINT8
),
341 FETCH_NULL(RGB_UINT16
),
342 FETCH_NULL(RGB_UINT32
),
343 FETCH_NULL(RGB_SINT8
),
344 FETCH_NULL(RGB_SINT16
),
345 FETCH_NULL(RGB_SINT32
),
346 FETCH_FUNCS(RGBA_UINT8
),
347 FETCH_FUNCS(RGBA_UINT16
),
348 FETCH_FUNCS(RGBA_UINT32
),
349 FETCH_FUNCS(RGBA_SINT8
),
350 FETCH_FUNCS(RGBA_SINT16
),
351 FETCH_FUNCS(RGBA_SINT32
),
352 FETCH_NULL(RGBX_UINT8
),
353 FETCH_NULL(RGBX_UINT16
),
354 FETCH_NULL(RGBX_UINT32
),
355 FETCH_NULL(RGBX_SINT8
),
356 FETCH_NULL(RGBX_SINT16
),
357 FETCH_NULL(RGBX_SINT32
),
359 /* DXT compressed formats */
361 MESA_FORMAT_RGB_DXT1
,
367 MESA_FORMAT_RGBA_DXT1
,
373 MESA_FORMAT_RGBA_DXT3
,
379 MESA_FORMAT_RGBA_DXT5
,
385 /* DXT sRGB compressed formats */
387 MESA_FORMAT_SRGB_DXT1
,
393 MESA_FORMAT_SRGBA_DXT1
,
399 MESA_FORMAT_SRGBA_DXT3
,
405 MESA_FORMAT_SRGBA_DXT5
,
411 /* FXT1 compressed formats */
413 MESA_FORMAT_RGB_FXT1
,
419 MESA_FORMAT_RGBA_FXT1
,
425 /* RGTC compressed formats */
427 MESA_FORMAT_R_RGTC1_UNORM
,
433 MESA_FORMAT_R_RGTC1_SNORM
,
439 MESA_FORMAT_RG_RGTC2_UNORM
,
445 MESA_FORMAT_RG_RGTC2_SNORM
,
451 /* LATC1/2 compressed formats */
453 MESA_FORMAT_L_LATC1_UNORM
,
459 MESA_FORMAT_L_LATC1_SNORM
,
465 MESA_FORMAT_LA_LATC2_UNORM
,
471 MESA_FORMAT_LA_LATC2_SNORM
,
477 /* ETC1/2 compressed formats */
479 MESA_FORMAT_ETC1_RGB8
,
485 MESA_FORMAT_ETC2_RGB8
,
491 MESA_FORMAT_ETC2_SRGB8
,
497 MESA_FORMAT_ETC2_RGBA8_EAC
,
503 MESA_FORMAT_ETC2_SRGB8_ALPHA8_EAC
,
509 MESA_FORMAT_ETC2_R11_EAC
,
515 MESA_FORMAT_ETC2_RG11_EAC
,
521 MESA_FORMAT_ETC2_SIGNED_R11_EAC
,
527 MESA_FORMAT_ETC2_SIGNED_RG11_EAC
,
533 MESA_FORMAT_ETC2_RGB8_PUNCHTHROUGH_ALPHA1
,
539 MESA_FORMAT_ETC2_SRGB8_PUNCHTHROUGH_ALPHA1
,
548 * Initialize the texture image's FetchTexel methods.
551 set_fetch_functions(const struct gl_sampler_object
*samp
,
552 struct swrast_texture_image
*texImage
, GLuint dims
)
554 mesa_format format
= texImage
->Base
.TexFormat
;
557 /* check that the table entries are sorted by format name */
559 for (fmt
= 0; fmt
< MESA_FORMAT_COUNT
; fmt
++) {
560 assert(texfetch_funcs
[fmt
].Name
== fmt
);
564 STATIC_ASSERT(Elements(texfetch_funcs
) == MESA_FORMAT_COUNT
);
566 if (samp
->sRGBDecode
== GL_SKIP_DECODE_EXT
&&
567 _mesa_get_format_color_encoding(format
) == GL_SRGB
) {
568 format
= _mesa_get_srgb_format_linear(format
);
571 assert(format
< MESA_FORMAT_COUNT
);
575 texImage
->FetchTexel
= texfetch_funcs
[format
].Fetch1D
;
578 texImage
->FetchTexel
= texfetch_funcs
[format
].Fetch2D
;
581 texImage
->FetchTexel
= texfetch_funcs
[format
].Fetch3D
;
584 assert(!"Bad dims in set_fetch_functions()");
587 texImage
->FetchCompressedTexel
= _mesa_get_compressed_fetch_func(format
);
589 ASSERT(texImage
->FetchTexel
);
593 _mesa_update_fetch_functions(struct gl_context
*ctx
, GLuint unit
)
595 struct gl_texture_object
*texObj
= ctx
->Texture
.Unit
[unit
]._Current
;
596 struct gl_sampler_object
*samp
;
603 samp
= _mesa_get_samplerobj(ctx
, unit
);
605 dims
= _mesa_get_texture_dimensions(texObj
->Target
);
607 for (face
= 0; face
< 6; face
++) {
608 for (i
= 0; i
< MAX_TEXTURE_LEVELS
; i
++) {
609 if (texObj
->Image
[face
][i
]) {
610 set_fetch_functions(samp
,
611 swrast_texture_image(texObj
->Image
[face
][i
]),