Merge remote-tracking branch 'mesa-public/master' into vulkan
[mesa.git] / src / mesa / swrast / s_texfetch.c
1 /*
2 * Mesa 3-D graphics library
3 *
4 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
5 * Copyright (c) 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 /**
28 * \file s_texfetch.c
29 *
30 * Texel fetch/store functions
31 *
32 * \author Gareth Hughes
33 */
34
35
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"
49
50
51 /* Texel fetch routines for all supported formats
52 */
53 #define DIM 1
54 #include "s_texfetch_tmp.h"
55
56 #define DIM 2
57 #include "s_texfetch_tmp.h"
58
59 #define DIM 3
60 #include "s_texfetch_tmp.h"
61
62
63 /**
64 * All compressed texture texel fetching is done though this function.
65 * Basically just call a core-Mesa texel fetch function.
66 */
67 static void
68 fetch_compressed(const struct swrast_texture_image *swImage,
69 GLint i, GLint j, GLint k, GLfloat *texel)
70 {
71 /* The FetchCompressedTexel function takes an integer pixel rowstride,
72 * while the image's rowstride is bytes per row of blocks.
73 */
74 GLuint bw, bh;
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);
78
79 swImage->FetchCompressedTexel(swImage->ImageSlices[k],
80 swImage->RowStride * bw / texelBytes,
81 i, j, texel);
82 }
83
84
85
86 /**
87 * Null texel fetch function.
88 *
89 * Have to have this so the FetchTexel function pointer is never NULL.
90 */
91 static void fetch_null_texelf( const struct swrast_texture_image *texImage,
92 GLint i, GLint j, GLint k, GLfloat *texel )
93 {
94 (void) texImage; (void) i; (void) j; (void) k;
95 texel[RCOMP] = 0.0;
96 texel[GCOMP] = 0.0;
97 texel[BCOMP] = 0.0;
98 texel[ACOMP] = 0.0;
99 _mesa_warning(NULL, "fetch_null_texelf() called!");
100 }
101
102
103 #define FETCH_FUNCS(NAME) \
104 { \
105 MESA_FORMAT_ ## NAME, \
106 fetch_texel_1d_ ## NAME, \
107 fetch_texel_2d_ ## NAME, \
108 fetch_texel_3d_ ## NAME, \
109 }
110
111 #define FETCH_NULL(NAME) \
112 { \
113 MESA_FORMAT_ ## NAME, \
114 NULL, \
115 NULL, \
116 NULL \
117 }
118
119 #define FETCH_COMPRESSED(NAME) \
120 { \
121 MESA_FORMAT_ ## NAME, \
122 fetch_compressed, \
123 fetch_compressed, \
124 fetch_compressed \
125 }
126
127 /**
128 * Table to map MESA_FORMAT_ to texel fetch/store funcs.
129 */
130 static struct {
131 mesa_format Name;
132 FetchTexelFunc Fetch1D;
133 FetchTexelFunc Fetch2D;
134 FetchTexelFunc Fetch3D;
135 }
136 texfetch_funcs[] =
137 {
138 {
139 MESA_FORMAT_NONE,
140 fetch_null_texelf,
141 fetch_null_texelf,
142 fetch_null_texelf
143 },
144
145 /* Packed unorm formats */
146 FETCH_FUNCS(A8B8G8R8_UNORM),
147 FETCH_FUNCS(X8B8G8R8_UNORM),
148 FETCH_FUNCS(R8G8B8A8_UNORM),
149 FETCH_FUNCS(R8G8B8X8_UNORM),
150 FETCH_FUNCS(B8G8R8A8_UNORM),
151 FETCH_FUNCS(B8G8R8X8_UNORM),
152 FETCH_FUNCS(A8R8G8B8_UNORM),
153 FETCH_FUNCS(X8R8G8B8_UNORM),
154 FETCH_FUNCS(L16A16_UNORM),
155 FETCH_FUNCS(A16L16_UNORM),
156 FETCH_FUNCS(B5G6R5_UNORM),
157 FETCH_FUNCS(R5G6B5_UNORM),
158 FETCH_FUNCS(B4G4R4A4_UNORM),
159 FETCH_NULL(B4G4R4X4_UNORM),
160 FETCH_FUNCS(A4R4G4B4_UNORM),
161 FETCH_FUNCS(A1B5G5R5_UNORM),
162 FETCH_FUNCS(B5G5R5A1_UNORM),
163 FETCH_NULL(B5G5R5X1_UNORM),
164 FETCH_FUNCS(A1R5G5B5_UNORM),
165 FETCH_FUNCS(L8A8_UNORM),
166 FETCH_FUNCS(A8L8_UNORM),
167 FETCH_FUNCS(R8G8_UNORM),
168 FETCH_FUNCS(G8R8_UNORM),
169 FETCH_FUNCS(L4A4_UNORM),
170 FETCH_FUNCS(B2G3R3_UNORM),
171 FETCH_FUNCS(R16G16_UNORM),
172 FETCH_FUNCS(G16R16_UNORM),
173 FETCH_FUNCS(B10G10R10A2_UNORM),
174 FETCH_NULL(B10G10R10X2_UNORM),
175 FETCH_FUNCS(R10G10B10A2_UNORM),
176 FETCH_NULL(R10G10B10X2_UNORM),
177
178 FETCH_FUNCS(S8_UINT_Z24_UNORM),
179 {
180 MESA_FORMAT_X8_UINT_Z24_UNORM,
181 fetch_texel_1d_S8_UINT_Z24_UNORM,
182 fetch_texel_2d_S8_UINT_Z24_UNORM,
183 fetch_texel_3d_S8_UINT_Z24_UNORM
184 },
185 FETCH_FUNCS(Z24_UNORM_S8_UINT),
186 {
187 MESA_FORMAT_Z24_UNORM_X8_UINT,
188 fetch_texel_1d_Z24_UNORM_S8_UINT,
189 fetch_texel_2d_Z24_UNORM_S8_UINT,
190 fetch_texel_3d_Z24_UNORM_S8_UINT
191 },
192 FETCH_NULL(R3G3B2_UNORM),
193 FETCH_NULL(A4B4G4R4_UNORM),
194 FETCH_NULL(R4G4B4A4_UNORM),
195 FETCH_NULL(R5G5B5A1_UNORM),
196 FETCH_NULL(A2B10G10R10_UNORM),
197 FETCH_NULL(A2R10G10B10_UNORM),
198
199 FETCH_FUNCS(YCBCR),
200 FETCH_FUNCS(YCBCR_REV),
201
202 /* Array unorm formats */
203 FETCH_FUNCS(A_UNORM8),
204 FETCH_FUNCS(A_UNORM16),
205 FETCH_FUNCS(L_UNORM8),
206 FETCH_FUNCS(L_UNORM16),
207 FETCH_FUNCS(I_UNORM8),
208 FETCH_FUNCS(I_UNORM16),
209 FETCH_FUNCS(R_UNORM8),
210 FETCH_FUNCS(R_UNORM16),
211 FETCH_FUNCS(BGR_UNORM8),
212 FETCH_FUNCS(RGB_UNORM8),
213 FETCH_FUNCS(RGBA_UNORM16),
214 FETCH_FUNCS(RGBX_UNORM16),
215 FETCH_FUNCS(Z_UNORM16),
216 FETCH_FUNCS(Z_UNORM32),
217 FETCH_NULL(S_UINT8),
218
219 /* Packed signed/normalized formats */
220 FETCH_FUNCS(A8B8G8R8_SNORM),
221 FETCH_FUNCS(X8B8G8R8_SNORM),
222 FETCH_FUNCS(R8G8B8A8_SNORM),
223 FETCH_NULL(R8G8B8X8_SNORM),
224 FETCH_FUNCS(R16G16_SNORM),
225 FETCH_NULL(G16R16_SNORM),
226 FETCH_FUNCS(R8G8_SNORM),
227 FETCH_NULL(G8R8_SNORM),
228 FETCH_FUNCS(L8A8_SNORM),
229 FETCH_FUNCS(A8L8_SNORM),
230
231 /* Array signed/normalized formats */
232 FETCH_FUNCS(A_SNORM8),
233 FETCH_FUNCS(A_SNORM16),
234 FETCH_FUNCS(L_SNORM8),
235 FETCH_FUNCS(L_SNORM16),
236 FETCH_FUNCS(I_SNORM8),
237 FETCH_FUNCS(I_SNORM16),
238 FETCH_FUNCS(R_SNORM8),
239 FETCH_FUNCS(R_SNORM16),
240 FETCH_FUNCS(LA_SNORM16),
241 FETCH_FUNCS(RGB_SNORM16),
242 FETCH_FUNCS(RGBA_SNORM16),
243 FETCH_NULL(RGBX_SNORM16),
244
245 /* Packed sRGB formats */
246 FETCH_FUNCS(A8B8G8R8_SRGB),
247 FETCH_FUNCS(B8G8R8A8_SRGB),
248 FETCH_FUNCS(A8R8G8B8_SRGB),
249 FETCH_NULL(B8G8R8X8_SRGB),
250 FETCH_NULL(X8R8G8B8_SRGB),
251 FETCH_FUNCS(R8G8B8A8_SRGB),
252 FETCH_FUNCS(R8G8B8X8_SRGB),
253 FETCH_FUNCS(X8B8G8R8_SRGB),
254 FETCH_FUNCS(L8A8_SRGB),
255 FETCH_FUNCS(A8L8_SRGB),
256
257 /* Array sRGB formats */
258 FETCH_FUNCS(L_SRGB8),
259 FETCH_FUNCS(BGR_SRGB8),
260
261 /* Packed float formats */
262 FETCH_FUNCS(R9G9B9E5_FLOAT),
263 FETCH_FUNCS(R11G11B10_FLOAT),
264 FETCH_FUNCS(Z32_FLOAT_S8X24_UINT),
265
266 /* Array float formats */
267 FETCH_FUNCS(A_FLOAT16),
268 FETCH_FUNCS(A_FLOAT32),
269 FETCH_FUNCS(L_FLOAT16),
270 FETCH_FUNCS(L_FLOAT32),
271 FETCH_FUNCS(LA_FLOAT16),
272 FETCH_FUNCS(LA_FLOAT32),
273 FETCH_FUNCS(I_FLOAT16),
274 FETCH_FUNCS(I_FLOAT32),
275 FETCH_FUNCS(R_FLOAT16),
276 FETCH_FUNCS(R_FLOAT32),
277 FETCH_FUNCS(RG_FLOAT16),
278 FETCH_FUNCS(RG_FLOAT32),
279 FETCH_FUNCS(RGB_FLOAT16),
280 FETCH_FUNCS(RGB_FLOAT32),
281 FETCH_FUNCS(RGBA_FLOAT16),
282 FETCH_FUNCS(RGBA_FLOAT32),
283 FETCH_FUNCS(RGBX_FLOAT16),
284 FETCH_FUNCS(RGBX_FLOAT32),
285 {
286 MESA_FORMAT_Z_FLOAT32,
287 fetch_texel_1d_R_FLOAT32, /* Reuse the R32F functions. */
288 fetch_texel_2d_R_FLOAT32,
289 fetch_texel_3d_R_FLOAT32
290 },
291
292 /* Packed signed/unsigned non-normalized integer formats */
293 FETCH_NULL(B10G10R10A2_UINT),
294 FETCH_NULL(R10G10B10A2_UINT),
295 FETCH_NULL(A2B10G10R10_UINT),
296 FETCH_NULL(A2R10G10B10_UINT),
297
298 /* Array signed/unsigned non-normalized integer formats */
299 FETCH_NULL(A_UINT8),
300 FETCH_NULL(A_UINT16),
301 FETCH_NULL(A_UINT32),
302 FETCH_NULL(A_SINT8),
303 FETCH_NULL(A_SINT16),
304 FETCH_NULL(A_SINT32),
305 FETCH_NULL(I_UINT8),
306 FETCH_NULL(I_UINT16),
307 FETCH_NULL(I_UINT32),
308 FETCH_NULL(I_SINT8),
309 FETCH_NULL(I_SINT16),
310 FETCH_NULL(I_SINT32),
311 FETCH_NULL(L_UINT8),
312 FETCH_NULL(L_UINT16),
313 FETCH_NULL(L_UINT32),
314 FETCH_NULL(L_SINT8),
315 FETCH_NULL(L_SINT16),
316 FETCH_NULL(L_SINT32),
317 FETCH_NULL(LA_UINT8),
318 FETCH_NULL(LA_UINT16),
319 FETCH_NULL(LA_UINT32),
320 FETCH_NULL(LA_SINT8),
321 FETCH_NULL(LA_SINT16),
322 FETCH_NULL(LA_SINT32),
323 FETCH_NULL(R_UINT8),
324 FETCH_NULL(R_UINT16),
325 FETCH_NULL(R_UINT32),
326 FETCH_NULL(R_SINT8),
327 FETCH_NULL(R_SINT16),
328 FETCH_NULL(R_SINT32),
329 FETCH_NULL(RG_UINT8),
330 FETCH_NULL(RG_UINT16),
331 FETCH_NULL(RG_UINT32),
332 FETCH_NULL(RG_SINT8),
333 FETCH_NULL(RG_SINT16),
334 FETCH_NULL(RG_SINT32),
335 FETCH_NULL(RGB_UINT8),
336 FETCH_NULL(RGB_UINT16),
337 FETCH_NULL(RGB_UINT32),
338 FETCH_NULL(RGB_SINT8),
339 FETCH_NULL(RGB_SINT16),
340 FETCH_NULL(RGB_SINT32),
341 FETCH_FUNCS(RGBA_UINT8),
342 FETCH_FUNCS(RGBA_UINT16),
343 FETCH_FUNCS(RGBA_UINT32),
344 FETCH_FUNCS(RGBA_SINT8),
345 FETCH_FUNCS(RGBA_SINT16),
346 FETCH_FUNCS(RGBA_SINT32),
347 FETCH_NULL(RGBX_UINT8),
348 FETCH_NULL(RGBX_UINT16),
349 FETCH_NULL(RGBX_UINT32),
350 FETCH_NULL(RGBX_SINT8),
351 FETCH_NULL(RGBX_SINT16),
352 FETCH_NULL(RGBX_SINT32),
353
354 /* DXT compressed formats */
355 FETCH_COMPRESSED(RGB_DXT1),
356 FETCH_COMPRESSED(RGBA_DXT1),
357 FETCH_COMPRESSED(RGBA_DXT3),
358 FETCH_COMPRESSED(RGBA_DXT5),
359
360 /* DXT sRGB compressed formats */
361 FETCH_COMPRESSED(SRGB_DXT1),
362 FETCH_COMPRESSED(SRGBA_DXT1),
363 FETCH_COMPRESSED(SRGBA_DXT3),
364 FETCH_COMPRESSED(SRGBA_DXT5),
365
366 /* FXT1 compressed formats */
367 FETCH_COMPRESSED(RGB_FXT1),
368 FETCH_COMPRESSED(RGBA_FXT1),
369
370 /* RGTC compressed formats */
371 FETCH_COMPRESSED(R_RGTC1_UNORM),
372 FETCH_COMPRESSED(R_RGTC1_SNORM),
373 FETCH_COMPRESSED(RG_RGTC2_UNORM),
374 FETCH_COMPRESSED(RG_RGTC2_SNORM),
375
376 /* LATC1/2 compressed formats */
377 FETCH_COMPRESSED(L_LATC1_UNORM),
378 FETCH_COMPRESSED(L_LATC1_SNORM),
379 FETCH_COMPRESSED(LA_LATC2_UNORM),
380 FETCH_COMPRESSED(LA_LATC2_SNORM),
381
382 /* ETC1/2 compressed formats */
383 FETCH_COMPRESSED(ETC1_RGB8),
384 FETCH_COMPRESSED(ETC2_RGB8),
385 FETCH_COMPRESSED(ETC2_SRGB8),
386 FETCH_COMPRESSED(ETC2_RGBA8_EAC),
387 FETCH_COMPRESSED(ETC2_SRGB8_ALPHA8_EAC),
388 FETCH_COMPRESSED(ETC2_R11_EAC),
389 FETCH_COMPRESSED(ETC2_RG11_EAC),
390 FETCH_COMPRESSED(ETC2_SIGNED_R11_EAC),
391 FETCH_COMPRESSED(ETC2_SIGNED_RG11_EAC),
392 FETCH_COMPRESSED(ETC2_RGB8_PUNCHTHROUGH_ALPHA1),
393 FETCH_COMPRESSED(ETC2_SRGB8_PUNCHTHROUGH_ALPHA1),
394 FETCH_COMPRESSED(BPTC_RGBA_UNORM),
395 FETCH_COMPRESSED(BPTC_SRGB_ALPHA_UNORM),
396 FETCH_COMPRESSED(BPTC_RGB_SIGNED_FLOAT),
397 FETCH_COMPRESSED(BPTC_RGB_UNSIGNED_FLOAT),
398
399 /* ASTC compressed formats */
400 FETCH_NULL(RGBA_ASTC_4x4),
401 FETCH_NULL(RGBA_ASTC_5x4),
402 FETCH_NULL(RGBA_ASTC_5x5),
403 FETCH_NULL(RGBA_ASTC_6x5),
404 FETCH_NULL(RGBA_ASTC_6x6),
405 FETCH_NULL(RGBA_ASTC_8x5),
406 FETCH_NULL(RGBA_ASTC_8x6),
407 FETCH_NULL(RGBA_ASTC_8x8),
408 FETCH_NULL(RGBA_ASTC_10x5),
409 FETCH_NULL(RGBA_ASTC_10x6),
410 FETCH_NULL(RGBA_ASTC_10x8),
411 FETCH_NULL(RGBA_ASTC_10x10),
412 FETCH_NULL(RGBA_ASTC_12x10),
413 FETCH_NULL(RGBA_ASTC_12x12),
414 FETCH_NULL(SRGB8_ALPHA8_ASTC_4x4),
415 FETCH_NULL(SRGB8_ALPHA8_ASTC_5x4),
416 FETCH_NULL(SRGB8_ALPHA8_ASTC_5x5),
417 FETCH_NULL(SRGB8_ALPHA8_ASTC_6x5),
418 FETCH_NULL(SRGB8_ALPHA8_ASTC_6x6),
419 FETCH_NULL(SRGB8_ALPHA8_ASTC_8x5),
420 FETCH_NULL(SRGB8_ALPHA8_ASTC_8x6),
421 FETCH_NULL(SRGB8_ALPHA8_ASTC_8x8),
422 FETCH_NULL(SRGB8_ALPHA8_ASTC_10x5),
423 FETCH_NULL(SRGB8_ALPHA8_ASTC_10x6),
424 FETCH_NULL(SRGB8_ALPHA8_ASTC_10x8),
425 FETCH_NULL(SRGB8_ALPHA8_ASTC_10x10),
426 FETCH_NULL(SRGB8_ALPHA8_ASTC_12x10),
427 FETCH_NULL(SRGB8_ALPHA8_ASTC_12x12)
428 };
429
430
431 /**
432 * Initialize the texture image's FetchTexel methods.
433 */
434 static void
435 set_fetch_functions(const struct gl_sampler_object *samp,
436 struct swrast_texture_image *texImage, GLuint dims)
437 {
438 mesa_format format = texImage->Base.TexFormat;
439
440 #ifdef DEBUG
441 /* check that the table entries are sorted by format name */
442 mesa_format fmt;
443 for (fmt = 0; fmt < MESA_FORMAT_COUNT; fmt++) {
444 assert(texfetch_funcs[fmt].Name == fmt);
445 }
446 #endif
447
448 STATIC_ASSERT(ARRAY_SIZE(texfetch_funcs) == MESA_FORMAT_COUNT);
449
450 if (samp->sRGBDecode == GL_SKIP_DECODE_EXT &&
451 _mesa_get_format_color_encoding(format) == GL_SRGB) {
452 format = _mesa_get_srgb_format_linear(format);
453 }
454
455 assert(format < MESA_FORMAT_COUNT);
456
457 switch (dims) {
458 case 1:
459 texImage->FetchTexel = texfetch_funcs[format].Fetch1D;
460 break;
461 case 2:
462 texImage->FetchTexel = texfetch_funcs[format].Fetch2D;
463 break;
464 case 3:
465 texImage->FetchTexel = texfetch_funcs[format].Fetch3D;
466 break;
467 default:
468 assert(!"Bad dims in set_fetch_functions()");
469 }
470
471 texImage->FetchCompressedTexel = _mesa_get_compressed_fetch_func(format);
472
473 assert(texImage->FetchTexel);
474 }
475
476 void
477 _mesa_update_fetch_functions(struct gl_context *ctx, GLuint unit)
478 {
479 struct gl_texture_object *texObj = ctx->Texture.Unit[unit]._Current;
480 struct gl_sampler_object *samp;
481 GLuint face, i;
482 GLuint dims;
483
484 if (!texObj)
485 return;
486
487 samp = _mesa_get_samplerobj(ctx, unit);
488
489 dims = _mesa_get_texture_dimensions(texObj->Target);
490
491 for (face = 0; face < 6; face++) {
492 for (i = 0; i < MAX_TEXTURE_LEVELS; i++) {
493 if (texObj->Image[face][i]) {
494 set_fetch_functions(samp,
495 swrast_texture_image(texObj->Image[face][i]),
496 dims);
497 }
498 }
499 }
500 }