• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
fetch_compressed(const struct swrast_texture_image * swImage,GLint i,GLint j,GLint k,GLfloat * texel)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  */
fetch_null_texelf(const struct swrast_texture_image * texImage,GLint i,GLint j,GLint k,GLfloat * texel)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(L4A4_UNORM),
145    FETCH_FUNCS(B2G3R3_UNORM),
146    FETCH_FUNCS(B10G10R10A2_UNORM),
147    FETCH_FUNCS(R10G10B10A2_UNORM),
148 
149    FETCH_FUNCS(S8_UINT_Z24_UNORM),
150    [MESA_FORMAT_X8_UINT_Z24_UNORM] = {
151       fetch_texel_1d_S8_UINT_Z24_UNORM,
152       fetch_texel_2d_S8_UINT_Z24_UNORM,
153       fetch_texel_3d_S8_UINT_Z24_UNORM
154    },
155    FETCH_FUNCS(Z24_UNORM_S8_UINT),
156    [MESA_FORMAT_Z24_UNORM_X8_UINT] = {
157       fetch_texel_1d_Z24_UNORM_S8_UINT,
158       fetch_texel_2d_Z24_UNORM_S8_UINT,
159       fetch_texel_3d_Z24_UNORM_S8_UINT
160    },
161 
162    FETCH_FUNCS(YCBCR),
163    FETCH_FUNCS(YCBCR_REV),
164 
165    /* Array unorm formats */
166    FETCH_FUNCS(A_UNORM8),
167    FETCH_FUNCS(A_UNORM16),
168    FETCH_FUNCS(L_UNORM8),
169    FETCH_FUNCS(L_UNORM16),
170    FETCH_FUNCS(LA_UNORM8),
171    FETCH_FUNCS(LA_UNORM16),
172    FETCH_FUNCS(I_UNORM8),
173    FETCH_FUNCS(I_UNORM16),
174    FETCH_FUNCS(R_UNORM8),
175    FETCH_FUNCS(R_UNORM16),
176    FETCH_FUNCS(RG_UNORM8),
177    FETCH_FUNCS(RG_UNORM16),
178    FETCH_FUNCS(BGR_UNORM8),
179    FETCH_FUNCS(RGB_UNORM8),
180    FETCH_FUNCS(RGBA_UNORM16),
181    FETCH_FUNCS(RGBX_UNORM16),
182    FETCH_FUNCS(Z_UNORM16),
183    FETCH_FUNCS(Z_UNORM32),
184 
185    /* Packed signed/normalized formats */
186    FETCH_FUNCS(A8B8G8R8_SNORM),
187    FETCH_FUNCS(X8B8G8R8_SNORM),
188    FETCH_FUNCS(R8G8B8A8_SNORM),
189 
190    /* Array signed/normalized formats */
191    FETCH_FUNCS(A_SNORM8),
192    FETCH_FUNCS(A_SNORM16),
193    FETCH_FUNCS(L_SNORM8),
194    FETCH_FUNCS(L_SNORM16),
195    FETCH_FUNCS(LA_SNORM8),
196    FETCH_FUNCS(LA_SNORM16),
197    FETCH_FUNCS(I_SNORM8),
198    FETCH_FUNCS(I_SNORM16),
199    FETCH_FUNCS(R_SNORM8),
200    FETCH_FUNCS(R_SNORM16),
201    FETCH_FUNCS(RG_SNORM8),
202    FETCH_FUNCS(RG_SNORM16),
203    FETCH_FUNCS(RGB_SNORM16),
204    FETCH_FUNCS(RGBA_SNORM16),
205 
206    /* Packed sRGB formats */
207    FETCH_FUNCS(A8B8G8R8_SRGB),
208    FETCH_FUNCS(B8G8R8A8_SRGB),
209    FETCH_FUNCS(A8R8G8B8_SRGB),
210    FETCH_FUNCS(R8G8B8A8_SRGB),
211    FETCH_FUNCS(R8G8B8X8_SRGB),
212    FETCH_FUNCS(X8B8G8R8_SRGB),
213 
214    /* Array sRGB formats */
215    FETCH_FUNCS(R_SRGB8),
216    FETCH_FUNCS(L_SRGB8),
217    FETCH_FUNCS(LA_SRGB8),
218    FETCH_FUNCS(BGR_SRGB8),
219 
220    /* Packed float formats */
221    FETCH_FUNCS(R9G9B9E5_FLOAT),
222    FETCH_FUNCS(R11G11B10_FLOAT),
223    FETCH_FUNCS(Z32_FLOAT_S8X24_UINT),
224 
225    /* Array float formats */
226    FETCH_FUNCS(A_FLOAT16),
227    FETCH_FUNCS(A_FLOAT32),
228    FETCH_FUNCS(L_FLOAT16),
229    FETCH_FUNCS(L_FLOAT32),
230    FETCH_FUNCS(LA_FLOAT16),
231    FETCH_FUNCS(LA_FLOAT32),
232    FETCH_FUNCS(I_FLOAT16),
233    FETCH_FUNCS(I_FLOAT32),
234    FETCH_FUNCS(R_FLOAT16),
235    FETCH_FUNCS(R_FLOAT32),
236    FETCH_FUNCS(RG_FLOAT16),
237    FETCH_FUNCS(RG_FLOAT32),
238    FETCH_FUNCS(RGB_FLOAT16),
239    FETCH_FUNCS(RGB_FLOAT32),
240    FETCH_FUNCS(RGBA_FLOAT16),
241    FETCH_FUNCS(RGBA_FLOAT32),
242    FETCH_FUNCS(RGBX_FLOAT16),
243    FETCH_FUNCS(RGBX_FLOAT32),
244    [MESA_FORMAT_Z_FLOAT32] = {
245       fetch_texel_1d_R_FLOAT32, /* Reuse the R32F functions. */
246       fetch_texel_2d_R_FLOAT32,
247       fetch_texel_3d_R_FLOAT32
248    },
249 
250    /* Packed signed/unsigned non-normalized integer formats */
251 
252    /* Array signed/unsigned non-normalized integer formats */
253    FETCH_FUNCS(RGBA_UINT16),
254    FETCH_FUNCS(RGBA_UINT32),
255    FETCH_FUNCS(RGBA_SINT8),
256    FETCH_FUNCS(RGBA_SINT16),
257    FETCH_FUNCS(RGBA_SINT32),
258 
259    /* DXT compressed formats */
260    FETCH_COMPRESSED(RGB_DXT1),
261    FETCH_COMPRESSED(RGBA_DXT1),
262    FETCH_COMPRESSED(RGBA_DXT3),
263    FETCH_COMPRESSED(RGBA_DXT5),
264 
265    /* DXT sRGB compressed formats */
266    FETCH_COMPRESSED(SRGB_DXT1),
267    FETCH_COMPRESSED(SRGBA_DXT1),
268    FETCH_COMPRESSED(SRGBA_DXT3),
269    FETCH_COMPRESSED(SRGBA_DXT5),
270 
271    /* FXT1 compressed formats */
272    FETCH_COMPRESSED(RGB_FXT1),
273    FETCH_COMPRESSED(RGBA_FXT1),
274 
275    /* RGTC compressed formats */
276    FETCH_COMPRESSED(R_RGTC1_UNORM),
277    FETCH_COMPRESSED(R_RGTC1_SNORM),
278    FETCH_COMPRESSED(RG_RGTC2_UNORM),
279    FETCH_COMPRESSED(RG_RGTC2_SNORM),
280 
281    /* LATC1/2 compressed formats */
282    FETCH_COMPRESSED(L_LATC1_UNORM),
283    FETCH_COMPRESSED(L_LATC1_SNORM),
284    FETCH_COMPRESSED(LA_LATC2_UNORM),
285    FETCH_COMPRESSED(LA_LATC2_SNORM),
286 
287    /* ETC1/2 compressed formats */
288    FETCH_COMPRESSED(ETC1_RGB8),
289    FETCH_COMPRESSED(ETC2_RGB8),
290    FETCH_COMPRESSED(ETC2_SRGB8),
291    FETCH_COMPRESSED(ETC2_RGBA8_EAC),
292    FETCH_COMPRESSED(ETC2_SRGB8_ALPHA8_EAC),
293    FETCH_COMPRESSED(ETC2_R11_EAC),
294    FETCH_COMPRESSED(ETC2_RG11_EAC),
295    FETCH_COMPRESSED(ETC2_SIGNED_R11_EAC),
296    FETCH_COMPRESSED(ETC2_SIGNED_RG11_EAC),
297    FETCH_COMPRESSED(ETC2_RGB8_PUNCHTHROUGH_ALPHA1),
298    FETCH_COMPRESSED(ETC2_SRGB8_PUNCHTHROUGH_ALPHA1),
299    FETCH_COMPRESSED(BPTC_RGBA_UNORM),
300    FETCH_COMPRESSED(BPTC_SRGB_ALPHA_UNORM),
301    FETCH_COMPRESSED(BPTC_RGB_SIGNED_FLOAT),
302    FETCH_COMPRESSED(BPTC_RGB_UNSIGNED_FLOAT),
303 };
304 
305 
306 /**
307  * Initialize the texture image's FetchTexel methods.
308  */
309 static void
set_fetch_functions(const struct gl_sampler_object * samp,struct swrast_texture_image * texImage,GLuint dims)310 set_fetch_functions(const struct gl_sampler_object *samp,
311                     struct swrast_texture_image *texImage, GLuint dims)
312 {
313    mesa_format format = texImage->Base.TexFormat;
314 
315    if (samp->sRGBDecode == GL_SKIP_DECODE_EXT)
316       format = _mesa_get_srgb_format_linear(format);
317 
318    texImage->FetchTexel = NULL;
319 
320    if (format < ARRAY_SIZE(texfetch_funcs)) {
321       switch (dims) {
322       case 1:
323          texImage->FetchTexel = texfetch_funcs[format].Fetch1D;
324          break;
325       case 2:
326          texImage->FetchTexel = texfetch_funcs[format].Fetch2D;
327          break;
328       case 3:
329          texImage->FetchTexel = texfetch_funcs[format].Fetch3D;
330          break;
331       default:
332          assert(!"Bad dims in set_fetch_functions()");
333       }
334    }
335 
336    if (!texImage->FetchTexel)
337       texImage->FetchTexel = fetch_null_texelf;
338 
339    texImage->FetchCompressedTexel = _mesa_get_compressed_fetch_func(format);
340 
341    assert(texImage->FetchTexel);
342 }
343 
344 void
_mesa_update_fetch_functions(struct gl_context * ctx,GLuint unit)345 _mesa_update_fetch_functions(struct gl_context *ctx, GLuint unit)
346 {
347    struct gl_texture_object *texObj = ctx->Texture.Unit[unit]._Current;
348    struct gl_sampler_object *samp;
349    GLuint face, i;
350    GLuint dims;
351 
352    if (!texObj)
353       return;
354 
355    samp = _mesa_get_samplerobj(ctx, unit);
356 
357    dims = _mesa_get_texture_dimensions(texObj->Target);
358 
359    for (face = 0; face < 6; face++) {
360       for (i = 0; i < MAX_TEXTURE_LEVELS; i++) {
361          if (texObj->Image[face][i]) {
362 	    set_fetch_functions(samp,
363                                 swrast_texture_image(texObj->Image[face][i]),
364                                 dims);
365          }
366       }
367    }
368 }
369