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/macros.h"
37 #include "main/texcompress.h"
38 #include "main/texcompress_fxt1.h"
39 #include "main/texcompress_s3tc.h"
40 #include "main/texcompress_rgtc.h"
41 #include "main/texcompress_etc.h"
42 #include "main/teximage.h"
43 #include "main/samplerobj.h"
44 #include "s_context.h"
45 #include "s_texfetch.h"
46 #include "../../gallium/auxiliary/util/u_format_rgb9e5.h"
47 #include "../../gallium/auxiliary/util/u_format_r11g11b10f.h"
48 #include "util/format_srgb.h"
51 /* Texel fetch routines for all supported formats
54 #include "s_texfetch_tmp.h"
57 #include "s_texfetch_tmp.h"
60 #include "s_texfetch_tmp.h"
64 * All compressed texture texel fetching is done though this function.
65 * Basically just call a core-Mesa texel fetch function.
68 fetch_compressed(const struct swrast_texture_image
*swImage
,
69 GLint i
, GLint j
, GLint k
, GLfloat
*texel
)
71 /* The FetchCompressedTexel function takes an integer pixel rowstride,
72 * while the image's rowstride is bytes per row of blocks.
75 GLuint texelBytes
= _mesa_get_format_bytes(swImage
->Base
.TexFormat
);
76 _mesa_get_format_block_size(swImage
->Base
.TexFormat
, &bw
, &bh
);
77 assert(swImage
->RowStride
* bw
% texelBytes
== 0);
79 swImage
->FetchCompressedTexel(swImage
->ImageSlices
[k
],
80 swImage
->RowStride
* bw
/ texelBytes
,
87 * Null texel fetch function.
89 * Have to have this so the FetchTexel function pointer is never NULL.
91 static void fetch_null_texelf( const struct swrast_texture_image
*texImage
,
92 GLint i
, GLint j
, GLint k
, GLfloat
*texel
)
94 (void) texImage
; (void) i
; (void) j
; (void) k
;
99 _mesa_warning(NULL
, "fetch_null_texelf() called!");
103 #define FETCH_FUNCS(NAME) \
105 MESA_FORMAT_ ## NAME, \
106 fetch_texel_1d_ ## NAME, \
107 fetch_texel_2d_ ## NAME, \
108 fetch_texel_3d_ ## NAME, \
111 #define FETCH_NULL(NAME) \
113 MESA_FORMAT_ ## NAME, \
120 * Table to map MESA_FORMAT_ to texel fetch/store funcs.
124 FetchTexelFunc Fetch1D
;
125 FetchTexelFunc Fetch2D
;
126 FetchTexelFunc Fetch3D
;
137 /* Packed unorm formats */
138 FETCH_FUNCS(A8B8G8R8_UNORM
),
139 FETCH_FUNCS(X8B8G8R8_UNORM
),
140 FETCH_FUNCS(R8G8B8A8_UNORM
),
141 FETCH_FUNCS(R8G8B8X8_UNORM
),
142 FETCH_FUNCS(B8G8R8A8_UNORM
),
143 FETCH_FUNCS(B8G8R8X8_UNORM
),
144 FETCH_FUNCS(A8R8G8B8_UNORM
),
145 FETCH_FUNCS(X8R8G8B8_UNORM
),
146 FETCH_FUNCS(L16A16_UNORM
),
147 FETCH_FUNCS(A16L16_UNORM
),
148 FETCH_FUNCS(B5G6R5_UNORM
),
149 FETCH_FUNCS(R5G6B5_UNORM
),
150 FETCH_FUNCS(B4G4R4A4_UNORM
),
151 FETCH_NULL(B4G4R4X4_UNORM
),
152 FETCH_FUNCS(A4R4G4B4_UNORM
),
153 FETCH_FUNCS(A1B5G5R5_UNORM
),
154 FETCH_FUNCS(B5G5R5A1_UNORM
),
155 FETCH_NULL(B5G5R5X1_UNORM
),
156 FETCH_FUNCS(A1R5G5B5_UNORM
),
157 FETCH_FUNCS(L8A8_UNORM
),
158 FETCH_FUNCS(A8L8_UNORM
),
159 FETCH_FUNCS(R8G8_UNORM
),
160 FETCH_FUNCS(G8R8_UNORM
),
161 FETCH_FUNCS(L4A4_UNORM
),
162 FETCH_FUNCS(B2G3R3_UNORM
),
163 FETCH_FUNCS(R16G16_UNORM
),
164 FETCH_FUNCS(G16R16_UNORM
),
165 FETCH_FUNCS(B10G10R10A2_UNORM
),
166 FETCH_NULL(B10G10R10X2_UNORM
),
167 FETCH_FUNCS(R10G10B10A2_UNORM
),
168 FETCH_NULL(R10G10B10X2_UNORM
),
170 FETCH_FUNCS(S8_UINT_Z24_UNORM
),
172 MESA_FORMAT_X8_UINT_Z24_UNORM
,
173 fetch_texel_1d_S8_UINT_Z24_UNORM
,
174 fetch_texel_2d_S8_UINT_Z24_UNORM
,
175 fetch_texel_3d_S8_UINT_Z24_UNORM
177 FETCH_FUNCS(Z24_UNORM_S8_UINT
),
179 MESA_FORMAT_Z24_UNORM_X8_UINT
,
180 fetch_texel_1d_Z24_UNORM_S8_UINT
,
181 fetch_texel_2d_Z24_UNORM_S8_UINT
,
182 fetch_texel_3d_Z24_UNORM_S8_UINT
184 FETCH_NULL(R3G3B2_UNORM
),
185 FETCH_NULL(A4B4G4R4_UNORM
),
186 FETCH_NULL(R4G4B4A4_UNORM
),
187 FETCH_NULL(R5G5B5A1_UNORM
),
188 FETCH_NULL(A2B10G10R10_UNORM
),
189 FETCH_NULL(A2R10G10B10_UNORM
),
192 FETCH_FUNCS(YCBCR_REV
),
194 /* Array unorm formats */
195 FETCH_FUNCS(A_UNORM8
),
196 FETCH_FUNCS(A_UNORM16
),
197 FETCH_FUNCS(L_UNORM8
),
198 FETCH_FUNCS(L_UNORM16
),
199 FETCH_FUNCS(I_UNORM8
),
200 FETCH_FUNCS(I_UNORM16
),
201 FETCH_FUNCS(R_UNORM8
),
202 FETCH_FUNCS(R_UNORM16
),
203 FETCH_FUNCS(BGR_UNORM8
),
204 FETCH_FUNCS(RGB_UNORM8
),
205 FETCH_FUNCS(RGBA_UNORM16
),
206 FETCH_FUNCS(RGBX_UNORM16
),
207 FETCH_FUNCS(Z_UNORM16
),
208 FETCH_FUNCS(Z_UNORM32
),
211 /* Packed signed/normalized formats */
212 FETCH_FUNCS(A8B8G8R8_SNORM
),
213 FETCH_FUNCS(X8B8G8R8_SNORM
),
214 FETCH_FUNCS(R8G8B8A8_SNORM
),
215 FETCH_NULL(R8G8B8X8_SNORM
),
216 FETCH_FUNCS(R16G16_SNORM
),
217 FETCH_NULL(G16R16_SNORM
),
218 FETCH_FUNCS(R8G8_SNORM
),
219 FETCH_NULL(G8R8_SNORM
),
220 FETCH_FUNCS(L8A8_SNORM
),
221 FETCH_FUNCS(A8L8_SNORM
),
223 /* Array signed/normalized formats */
224 FETCH_FUNCS(A_SNORM8
),
225 FETCH_FUNCS(A_SNORM16
),
226 FETCH_FUNCS(L_SNORM8
),
227 FETCH_FUNCS(L_SNORM16
),
228 FETCH_FUNCS(I_SNORM8
),
229 FETCH_FUNCS(I_SNORM16
),
230 FETCH_FUNCS(R_SNORM8
),
231 FETCH_FUNCS(R_SNORM16
),
232 FETCH_FUNCS(LA_SNORM16
),
233 FETCH_FUNCS(RGB_SNORM16
),
234 FETCH_FUNCS(RGBA_SNORM16
),
235 FETCH_NULL(RGBX_SNORM16
),
237 /* Packed sRGB formats */
238 FETCH_FUNCS(A8B8G8R8_SRGB
),
239 FETCH_FUNCS(B8G8R8A8_SRGB
),
240 FETCH_FUNCS(A8R8G8B8_SRGB
),
241 FETCH_NULL(B8G8R8X8_SRGB
),
242 FETCH_NULL(X8R8G8B8_SRGB
),
243 FETCH_FUNCS(R8G8B8A8_SRGB
),
244 FETCH_FUNCS(R8G8B8X8_SRGB
),
245 FETCH_FUNCS(X8B8G8R8_SRGB
),
246 FETCH_FUNCS(L8A8_SRGB
),
247 FETCH_FUNCS(A8L8_SRGB
),
249 /* Array sRGB formats */
250 FETCH_FUNCS(L_SRGB8
),
251 FETCH_FUNCS(BGR_SRGB8
),
253 /* Packed float formats */
254 FETCH_FUNCS(R9G9B9E5_FLOAT
),
255 FETCH_FUNCS(R11G11B10_FLOAT
),
256 FETCH_FUNCS(Z32_FLOAT_S8X24_UINT
),
258 /* Array float formats */
259 FETCH_FUNCS(A_FLOAT16
),
260 FETCH_FUNCS(A_FLOAT32
),
261 FETCH_FUNCS(L_FLOAT16
),
262 FETCH_FUNCS(L_FLOAT32
),
263 FETCH_FUNCS(LA_FLOAT16
),
264 FETCH_FUNCS(LA_FLOAT32
),
265 FETCH_FUNCS(I_FLOAT16
),
266 FETCH_FUNCS(I_FLOAT32
),
267 FETCH_FUNCS(R_FLOAT16
),
268 FETCH_FUNCS(R_FLOAT32
),
269 FETCH_FUNCS(RG_FLOAT16
),
270 FETCH_FUNCS(RG_FLOAT32
),
271 FETCH_FUNCS(RGB_FLOAT16
),
272 FETCH_FUNCS(RGB_FLOAT32
),
273 FETCH_FUNCS(RGBA_FLOAT16
),
274 FETCH_FUNCS(RGBA_FLOAT32
),
275 FETCH_FUNCS(RGBX_FLOAT16
),
276 FETCH_FUNCS(RGBX_FLOAT32
),
278 MESA_FORMAT_Z_FLOAT32
,
279 fetch_texel_1d_R_FLOAT32
, /* Reuse the R32F functions. */
280 fetch_texel_2d_R_FLOAT32
,
281 fetch_texel_3d_R_FLOAT32
284 /* Packed signed/unsigned non-normalized integer formats */
285 FETCH_NULL(B10G10R10A2_UINT
),
286 FETCH_NULL(R10G10B10A2_UINT
),
287 FETCH_NULL(A2B10G10R10_UINT
),
288 FETCH_NULL(A2R10G10B10_UINT
),
290 /* Array signed/unsigned non-normalized integer formats */
292 FETCH_NULL(A_UINT16
),
293 FETCH_NULL(A_UINT32
),
295 FETCH_NULL(A_SINT16
),
296 FETCH_NULL(A_SINT32
),
298 FETCH_NULL(I_UINT16
),
299 FETCH_NULL(I_UINT32
),
301 FETCH_NULL(I_SINT16
),
302 FETCH_NULL(I_SINT32
),
304 FETCH_NULL(L_UINT16
),
305 FETCH_NULL(L_UINT32
),
307 FETCH_NULL(L_SINT16
),
308 FETCH_NULL(L_SINT32
),
309 FETCH_NULL(LA_UINT8
),
310 FETCH_NULL(LA_UINT16
),
311 FETCH_NULL(LA_UINT32
),
312 FETCH_NULL(LA_SINT8
),
313 FETCH_NULL(LA_SINT16
),
314 FETCH_NULL(LA_SINT32
),
316 FETCH_NULL(R_UINT16
),
317 FETCH_NULL(R_UINT32
),
319 FETCH_NULL(R_SINT16
),
320 FETCH_NULL(R_SINT32
),
321 FETCH_NULL(RG_UINT8
),
322 FETCH_NULL(RG_UINT16
),
323 FETCH_NULL(RG_UINT32
),
324 FETCH_NULL(RG_SINT8
),
325 FETCH_NULL(RG_SINT16
),
326 FETCH_NULL(RG_SINT32
),
327 FETCH_NULL(RGB_UINT8
),
328 FETCH_NULL(RGB_UINT16
),
329 FETCH_NULL(RGB_UINT32
),
330 FETCH_NULL(RGB_SINT8
),
331 FETCH_NULL(RGB_SINT16
),
332 FETCH_NULL(RGB_SINT32
),
333 FETCH_FUNCS(RGBA_UINT8
),
334 FETCH_FUNCS(RGBA_UINT16
),
335 FETCH_FUNCS(RGBA_UINT32
),
336 FETCH_FUNCS(RGBA_SINT8
),
337 FETCH_FUNCS(RGBA_SINT16
),
338 FETCH_FUNCS(RGBA_SINT32
),
339 FETCH_NULL(RGBX_UINT8
),
340 FETCH_NULL(RGBX_UINT16
),
341 FETCH_NULL(RGBX_UINT32
),
342 FETCH_NULL(RGBX_SINT8
),
343 FETCH_NULL(RGBX_SINT16
),
344 FETCH_NULL(RGBX_SINT32
),
346 /* DXT compressed formats */
348 MESA_FORMAT_RGB_DXT1
,
354 MESA_FORMAT_RGBA_DXT1
,
360 MESA_FORMAT_RGBA_DXT3
,
366 MESA_FORMAT_RGBA_DXT5
,
372 /* DXT sRGB compressed formats */
374 MESA_FORMAT_SRGB_DXT1
,
380 MESA_FORMAT_SRGBA_DXT1
,
386 MESA_FORMAT_SRGBA_DXT3
,
392 MESA_FORMAT_SRGBA_DXT5
,
398 /* FXT1 compressed formats */
400 MESA_FORMAT_RGB_FXT1
,
406 MESA_FORMAT_RGBA_FXT1
,
412 /* RGTC compressed formats */
414 MESA_FORMAT_R_RGTC1_UNORM
,
420 MESA_FORMAT_R_RGTC1_SNORM
,
426 MESA_FORMAT_RG_RGTC2_UNORM
,
432 MESA_FORMAT_RG_RGTC2_SNORM
,
438 /* LATC1/2 compressed formats */
440 MESA_FORMAT_L_LATC1_UNORM
,
446 MESA_FORMAT_L_LATC1_SNORM
,
452 MESA_FORMAT_LA_LATC2_UNORM
,
458 MESA_FORMAT_LA_LATC2_SNORM
,
464 /* ETC1/2 compressed formats */
466 MESA_FORMAT_ETC1_RGB8
,
472 MESA_FORMAT_ETC2_RGB8
,
478 MESA_FORMAT_ETC2_SRGB8
,
484 MESA_FORMAT_ETC2_RGBA8_EAC
,
490 MESA_FORMAT_ETC2_SRGB8_ALPHA8_EAC
,
496 MESA_FORMAT_ETC2_R11_EAC
,
502 MESA_FORMAT_ETC2_RG11_EAC
,
508 MESA_FORMAT_ETC2_SIGNED_R11_EAC
,
514 MESA_FORMAT_ETC2_SIGNED_RG11_EAC
,
520 MESA_FORMAT_ETC2_RGB8_PUNCHTHROUGH_ALPHA1
,
526 MESA_FORMAT_ETC2_SRGB8_PUNCHTHROUGH_ALPHA1
,
532 MESA_FORMAT_BPTC_RGBA_UNORM
,
538 MESA_FORMAT_BPTC_SRGB_ALPHA_UNORM
,
544 MESA_FORMAT_BPTC_RGB_SIGNED_FLOAT
,
550 MESA_FORMAT_BPTC_RGB_UNSIGNED_FLOAT
,
556 /* ASTC compressed formats */
557 FETCH_NULL(RGBA_ASTC_4x4
),
558 FETCH_NULL(RGBA_ASTC_5x4
),
559 FETCH_NULL(RGBA_ASTC_5x5
),
560 FETCH_NULL(RGBA_ASTC_6x5
),
561 FETCH_NULL(RGBA_ASTC_6x6
),
562 FETCH_NULL(RGBA_ASTC_8x5
),
563 FETCH_NULL(RGBA_ASTC_8x6
),
564 FETCH_NULL(RGBA_ASTC_8x8
),
565 FETCH_NULL(RGBA_ASTC_10x5
),
566 FETCH_NULL(RGBA_ASTC_10x6
),
567 FETCH_NULL(RGBA_ASTC_10x8
),
568 FETCH_NULL(RGBA_ASTC_10x10
),
569 FETCH_NULL(RGBA_ASTC_12x10
),
570 FETCH_NULL(RGBA_ASTC_12x12
),
571 FETCH_NULL(SRGB8_ALPHA8_ASTC_4x4
),
572 FETCH_NULL(SRGB8_ALPHA8_ASTC_5x4
),
573 FETCH_NULL(SRGB8_ALPHA8_ASTC_5x5
),
574 FETCH_NULL(SRGB8_ALPHA8_ASTC_6x5
),
575 FETCH_NULL(SRGB8_ALPHA8_ASTC_6x6
),
576 FETCH_NULL(SRGB8_ALPHA8_ASTC_8x5
),
577 FETCH_NULL(SRGB8_ALPHA8_ASTC_8x6
),
578 FETCH_NULL(SRGB8_ALPHA8_ASTC_8x8
),
579 FETCH_NULL(SRGB8_ALPHA8_ASTC_10x5
),
580 FETCH_NULL(SRGB8_ALPHA8_ASTC_10x6
),
581 FETCH_NULL(SRGB8_ALPHA8_ASTC_10x8
),
582 FETCH_NULL(SRGB8_ALPHA8_ASTC_10x10
),
583 FETCH_NULL(SRGB8_ALPHA8_ASTC_12x10
),
584 FETCH_NULL(SRGB8_ALPHA8_ASTC_12x12
)
589 * Initialize the texture image's FetchTexel methods.
592 set_fetch_functions(const struct gl_sampler_object
*samp
,
593 struct swrast_texture_image
*texImage
, GLuint dims
)
595 mesa_format format
= texImage
->Base
.TexFormat
;
598 /* check that the table entries are sorted by format name */
600 for (fmt
= 0; fmt
< MESA_FORMAT_COUNT
; fmt
++) {
601 assert(texfetch_funcs
[fmt
].Name
== fmt
);
605 STATIC_ASSERT(ARRAY_SIZE(texfetch_funcs
) == MESA_FORMAT_COUNT
);
607 if (samp
->sRGBDecode
== GL_SKIP_DECODE_EXT
&&
608 _mesa_get_format_color_encoding(format
) == GL_SRGB
) {
609 format
= _mesa_get_srgb_format_linear(format
);
612 assert(format
< MESA_FORMAT_COUNT
);
616 texImage
->FetchTexel
= texfetch_funcs
[format
].Fetch1D
;
619 texImage
->FetchTexel
= texfetch_funcs
[format
].Fetch2D
;
622 texImage
->FetchTexel
= texfetch_funcs
[format
].Fetch3D
;
625 assert(!"Bad dims in set_fetch_functions()");
628 texImage
->FetchCompressedTexel
= _mesa_get_compressed_fetch_func(format
);
630 assert(texImage
->FetchTexel
);
634 _mesa_update_fetch_functions(struct gl_context
*ctx
, GLuint unit
)
636 struct gl_texture_object
*texObj
= ctx
->Texture
.Unit
[unit
]._Current
;
637 struct gl_sampler_object
*samp
;
644 samp
= _mesa_get_samplerobj(ctx
, unit
);
646 dims
= _mesa_get_texture_dimensions(texObj
->Target
);
648 for (face
= 0; face
< 6; face
++) {
649 for (i
= 0; i
< MAX_TEXTURE_LEVELS
; i
++) {
650 if (texObj
->Image
[face
][i
]) {
651 set_fetch_functions(samp
,
652 swrast_texture_image(texObj
->Image
[face
][i
]),