mesa: Replace the LA16_UNORM packed formats with one array format.
[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/errors.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 "util/format_rgb9e5.h"
48 #include "util/format_r11g11b10f.h"
49 #include "util/format_srgb.h"
50
51
52 /* Texel fetch routines for all supported formats
53 */
54 #define DIM 1
55 #include "s_texfetch_tmp.h"
56
57 #define DIM 2
58 #include "s_texfetch_tmp.h"
59
60 #define DIM 3
61 #include "s_texfetch_tmp.h"
62
63
64 /**
65 * All compressed texture texel fetching is done though this function.
66 * Basically just call a core-Mesa texel fetch function.
67 */
68 static void
69 fetch_compressed(const struct swrast_texture_image *swImage,
70 GLint i, GLint j, GLint k, GLfloat *texel)
71 {
72 /* The FetchCompressedTexel function takes an integer pixel rowstride,
73 * while the image's rowstride is bytes per row of blocks.
74 */
75 GLuint bw, bh;
76 GLuint texelBytes = _mesa_get_format_bytes(swImage->Base.TexFormat);
77 _mesa_get_format_block_size(swImage->Base.TexFormat, &bw, &bh);
78 assert(swImage->RowStride * bw % texelBytes == 0);
79
80 swImage->FetchCompressedTexel(swImage->ImageSlices[k],
81 swImage->RowStride * bw / texelBytes,
82 i, j, texel);
83 }
84
85
86
87 /**
88 * Null texel fetch function.
89 *
90 * Have to have this so the FetchTexel function pointer is never NULL.
91 */
92 static void fetch_null_texelf( const struct swrast_texture_image *texImage,
93 GLint i, GLint j, GLint k, GLfloat *texel )
94 {
95 (void) texImage; (void) i; (void) j; (void) k;
96 texel[RCOMP] = 0.0;
97 texel[GCOMP] = 0.0;
98 texel[BCOMP] = 0.0;
99 texel[ACOMP] = 0.0;
100 _mesa_warning(NULL, "fetch_null_texelf() called!");
101 }
102
103
104 #define FETCH_FUNCS(NAME) \
105 [MESA_FORMAT_ ## NAME] = { \
106 fetch_texel_1d_ ## NAME, \
107 fetch_texel_2d_ ## NAME, \
108 fetch_texel_3d_ ## NAME, \
109 }
110
111 #define FETCH_COMPRESSED(NAME) \
112 [MESA_FORMAT_ ## NAME] = { \
113 fetch_compressed, \
114 fetch_compressed, \
115 fetch_compressed \
116 }
117
118 /**
119 * Table to map MESA_FORMAT_ to texel fetch/store funcs.
120 */
121 static struct {
122 FetchTexelFunc Fetch1D;
123 FetchTexelFunc Fetch2D;
124 FetchTexelFunc Fetch3D;
125 }
126 texfetch_funcs[] =
127 {
128 /* Packed unorm formats */
129 FETCH_FUNCS(A8B8G8R8_UNORM),
130 FETCH_FUNCS(X8B8G8R8_UNORM),
131 FETCH_FUNCS(R8G8B8A8_UNORM),
132 FETCH_FUNCS(R8G8B8X8_UNORM),
133 FETCH_FUNCS(B8G8R8A8_UNORM),
134 FETCH_FUNCS(B8G8R8X8_UNORM),
135 FETCH_FUNCS(A8R8G8B8_UNORM),
136 FETCH_FUNCS(X8R8G8B8_UNORM),
137 FETCH_FUNCS(B5G6R5_UNORM),
138 FETCH_FUNCS(R5G6B5_UNORM),
139 FETCH_FUNCS(B4G4R4A4_UNORM),
140 FETCH_FUNCS(A4R4G4B4_UNORM),
141 FETCH_FUNCS(A1B5G5R5_UNORM),
142 FETCH_FUNCS(B5G5R5A1_UNORM),
143 FETCH_FUNCS(A1R5G5B5_UNORM),
144 FETCH_FUNCS(L8A8_UNORM),
145 FETCH_FUNCS(A8L8_UNORM),
146 FETCH_FUNCS(R8G8_UNORM),
147 FETCH_FUNCS(G8R8_UNORM),
148 FETCH_FUNCS(L4A4_UNORM),
149 FETCH_FUNCS(B2G3R3_UNORM),
150 FETCH_FUNCS(R16G16_UNORM),
151 FETCH_FUNCS(G16R16_UNORM),
152 FETCH_FUNCS(B10G10R10A2_UNORM),
153 FETCH_FUNCS(R10G10B10A2_UNORM),
154
155 FETCH_FUNCS(S8_UINT_Z24_UNORM),
156 [MESA_FORMAT_X8_UINT_Z24_UNORM] = {
157 fetch_texel_1d_S8_UINT_Z24_UNORM,
158 fetch_texel_2d_S8_UINT_Z24_UNORM,
159 fetch_texel_3d_S8_UINT_Z24_UNORM
160 },
161 FETCH_FUNCS(Z24_UNORM_S8_UINT),
162 [MESA_FORMAT_Z24_UNORM_X8_UINT] = {
163 fetch_texel_1d_Z24_UNORM_S8_UINT,
164 fetch_texel_2d_Z24_UNORM_S8_UINT,
165 fetch_texel_3d_Z24_UNORM_S8_UINT
166 },
167
168 FETCH_FUNCS(YCBCR),
169 FETCH_FUNCS(YCBCR_REV),
170
171 /* Array unorm formats */
172 FETCH_FUNCS(A_UNORM8),
173 FETCH_FUNCS(A_UNORM16),
174 FETCH_FUNCS(L_UNORM8),
175 FETCH_FUNCS(L_UNORM16),
176 FETCH_FUNCS(LA_UNORM16),
177 FETCH_FUNCS(I_UNORM8),
178 FETCH_FUNCS(I_UNORM16),
179 FETCH_FUNCS(R_UNORM8),
180 FETCH_FUNCS(R_UNORM16),
181 FETCH_FUNCS(BGR_UNORM8),
182 FETCH_FUNCS(RGB_UNORM8),
183 FETCH_FUNCS(RGBA_UNORM16),
184 FETCH_FUNCS(RGBX_UNORM16),
185 FETCH_FUNCS(Z_UNORM16),
186 FETCH_FUNCS(Z_UNORM32),
187
188 /* Packed signed/normalized formats */
189 FETCH_FUNCS(A8B8G8R8_SNORM),
190 FETCH_FUNCS(X8B8G8R8_SNORM),
191 FETCH_FUNCS(R8G8B8A8_SNORM),
192 FETCH_FUNCS(R16G16_SNORM),
193 FETCH_FUNCS(R8G8_SNORM),
194 FETCH_FUNCS(L8A8_SNORM),
195 FETCH_FUNCS(A8L8_SNORM),
196
197 /* Array signed/normalized formats */
198 FETCH_FUNCS(A_SNORM8),
199 FETCH_FUNCS(A_SNORM16),
200 FETCH_FUNCS(L_SNORM8),
201 FETCH_FUNCS(L_SNORM16),
202 FETCH_FUNCS(I_SNORM8),
203 FETCH_FUNCS(I_SNORM16),
204 FETCH_FUNCS(R_SNORM8),
205 FETCH_FUNCS(R_SNORM16),
206 FETCH_FUNCS(LA_SNORM16),
207 FETCH_FUNCS(RGB_SNORM16),
208 FETCH_FUNCS(RGBA_SNORM16),
209
210 /* Packed sRGB formats */
211 FETCH_FUNCS(A8B8G8R8_SRGB),
212 FETCH_FUNCS(B8G8R8A8_SRGB),
213 FETCH_FUNCS(A8R8G8B8_SRGB),
214 FETCH_FUNCS(R8G8B8A8_SRGB),
215 FETCH_FUNCS(R8G8B8X8_SRGB),
216 FETCH_FUNCS(X8B8G8R8_SRGB),
217 FETCH_FUNCS(L8A8_SRGB),
218 FETCH_FUNCS(A8L8_SRGB),
219
220 /* Array sRGB formats */
221 FETCH_FUNCS(R_SRGB8),
222 FETCH_FUNCS(L_SRGB8),
223 FETCH_FUNCS(BGR_SRGB8),
224
225 /* Packed float formats */
226 FETCH_FUNCS(R9G9B9E5_FLOAT),
227 FETCH_FUNCS(R11G11B10_FLOAT),
228 FETCH_FUNCS(Z32_FLOAT_S8X24_UINT),
229
230 /* Array float formats */
231 FETCH_FUNCS(A_FLOAT16),
232 FETCH_FUNCS(A_FLOAT32),
233 FETCH_FUNCS(L_FLOAT16),
234 FETCH_FUNCS(L_FLOAT32),
235 FETCH_FUNCS(LA_FLOAT16),
236 FETCH_FUNCS(LA_FLOAT32),
237 FETCH_FUNCS(I_FLOAT16),
238 FETCH_FUNCS(I_FLOAT32),
239 FETCH_FUNCS(R_FLOAT16),
240 FETCH_FUNCS(R_FLOAT32),
241 FETCH_FUNCS(RG_FLOAT16),
242 FETCH_FUNCS(RG_FLOAT32),
243 FETCH_FUNCS(RGB_FLOAT16),
244 FETCH_FUNCS(RGB_FLOAT32),
245 FETCH_FUNCS(RGBA_FLOAT16),
246 FETCH_FUNCS(RGBA_FLOAT32),
247 FETCH_FUNCS(RGBX_FLOAT16),
248 FETCH_FUNCS(RGBX_FLOAT32),
249 [MESA_FORMAT_Z_FLOAT32] = {
250 fetch_texel_1d_R_FLOAT32, /* Reuse the R32F functions. */
251 fetch_texel_2d_R_FLOAT32,
252 fetch_texel_3d_R_FLOAT32
253 },
254
255 /* Packed signed/unsigned non-normalized integer formats */
256
257 /* Array signed/unsigned non-normalized integer formats */
258 FETCH_FUNCS(RGBA_UINT8),
259 FETCH_FUNCS(RGBA_UINT16),
260 FETCH_FUNCS(RGBA_UINT32),
261 FETCH_FUNCS(RGBA_SINT8),
262 FETCH_FUNCS(RGBA_SINT16),
263 FETCH_FUNCS(RGBA_SINT32),
264
265 /* DXT compressed formats */
266 FETCH_COMPRESSED(RGB_DXT1),
267 FETCH_COMPRESSED(RGBA_DXT1),
268 FETCH_COMPRESSED(RGBA_DXT3),
269 FETCH_COMPRESSED(RGBA_DXT5),
270
271 /* DXT sRGB compressed formats */
272 FETCH_COMPRESSED(SRGB_DXT1),
273 FETCH_COMPRESSED(SRGBA_DXT1),
274 FETCH_COMPRESSED(SRGBA_DXT3),
275 FETCH_COMPRESSED(SRGBA_DXT5),
276
277 /* FXT1 compressed formats */
278 FETCH_COMPRESSED(RGB_FXT1),
279 FETCH_COMPRESSED(RGBA_FXT1),
280
281 /* RGTC compressed formats */
282 FETCH_COMPRESSED(R_RGTC1_UNORM),
283 FETCH_COMPRESSED(R_RGTC1_SNORM),
284 FETCH_COMPRESSED(RG_RGTC2_UNORM),
285 FETCH_COMPRESSED(RG_RGTC2_SNORM),
286
287 /* LATC1/2 compressed formats */
288 FETCH_COMPRESSED(L_LATC1_UNORM),
289 FETCH_COMPRESSED(L_LATC1_SNORM),
290 FETCH_COMPRESSED(LA_LATC2_UNORM),
291 FETCH_COMPRESSED(LA_LATC2_SNORM),
292
293 /* ETC1/2 compressed formats */
294 FETCH_COMPRESSED(ETC1_RGB8),
295 FETCH_COMPRESSED(ETC2_RGB8),
296 FETCH_COMPRESSED(ETC2_SRGB8),
297 FETCH_COMPRESSED(ETC2_RGBA8_EAC),
298 FETCH_COMPRESSED(ETC2_SRGB8_ALPHA8_EAC),
299 FETCH_COMPRESSED(ETC2_R11_EAC),
300 FETCH_COMPRESSED(ETC2_RG11_EAC),
301 FETCH_COMPRESSED(ETC2_SIGNED_R11_EAC),
302 FETCH_COMPRESSED(ETC2_SIGNED_RG11_EAC),
303 FETCH_COMPRESSED(ETC2_RGB8_PUNCHTHROUGH_ALPHA1),
304 FETCH_COMPRESSED(ETC2_SRGB8_PUNCHTHROUGH_ALPHA1),
305 FETCH_COMPRESSED(BPTC_RGBA_UNORM),
306 FETCH_COMPRESSED(BPTC_SRGB_ALPHA_UNORM),
307 FETCH_COMPRESSED(BPTC_RGB_SIGNED_FLOAT),
308 FETCH_COMPRESSED(BPTC_RGB_UNSIGNED_FLOAT),
309 };
310
311
312 /**
313 * Initialize the texture image's FetchTexel methods.
314 */
315 static void
316 set_fetch_functions(const struct gl_sampler_object *samp,
317 struct swrast_texture_image *texImage, GLuint dims)
318 {
319 mesa_format format = texImage->Base.TexFormat;
320
321 if (samp->sRGBDecode == GL_SKIP_DECODE_EXT)
322 format = _mesa_get_srgb_format_linear(format);
323
324 texImage->FetchTexel = NULL;
325
326 if (format < ARRAY_SIZE(texfetch_funcs)) {
327 switch (dims) {
328 case 1:
329 texImage->FetchTexel = texfetch_funcs[format].Fetch1D;
330 break;
331 case 2:
332 texImage->FetchTexel = texfetch_funcs[format].Fetch2D;
333 break;
334 case 3:
335 texImage->FetchTexel = texfetch_funcs[format].Fetch3D;
336 break;
337 default:
338 assert(!"Bad dims in set_fetch_functions()");
339 }
340 }
341
342 if (!texImage->FetchTexel)
343 texImage->FetchTexel = fetch_null_texelf;
344
345 texImage->FetchCompressedTexel = _mesa_get_compressed_fetch_func(format);
346
347 assert(texImage->FetchTexel);
348 }
349
350 void
351 _mesa_update_fetch_functions(struct gl_context *ctx, GLuint unit)
352 {
353 struct gl_texture_object *texObj = ctx->Texture.Unit[unit]._Current;
354 struct gl_sampler_object *samp;
355 GLuint face, i;
356 GLuint dims;
357
358 if (!texObj)
359 return;
360
361 samp = _mesa_get_samplerobj(ctx, unit);
362
363 dims = _mesa_get_texture_dimensions(texObj->Target);
364
365 for (face = 0; face < 6; face++) {
366 for (i = 0; i < MAX_TEXTURE_LEVELS; i++) {
367 if (texObj->Image[face][i]) {
368 set_fetch_functions(samp,
369 swrast_texture_image(texObj->Image[face][i]),
370 dims);
371 }
372 }
373 }
374 }