1848b8605Smrg/*
2848b8605Smrg * Mesa 3-D graphics library
3848b8605Smrg *
4848b8605Smrg * Copyright (C) 1999-2008  Brian Paul   All Rights Reserved.
5848b8605Smrg * Copyright (c) 2009  VMware, Inc.
6848b8605Smrg *
7848b8605Smrg * Permission is hereby granted, free of charge, to any person obtaining a
8848b8605Smrg * copy of this software and associated documentation files (the "Software"),
9848b8605Smrg * to deal in the Software without restriction, including without limitation
10848b8605Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11848b8605Smrg * and/or sell copies of the Software, and to permit persons to whom the
12848b8605Smrg * Software is furnished to do so, subject to the following conditions:
13848b8605Smrg *
14848b8605Smrg * The above copyright notice and this permission notice shall be included
15848b8605Smrg * in all copies or substantial portions of the Software.
16848b8605Smrg *
17848b8605Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18848b8605Smrg * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19848b8605Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
20848b8605Smrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
21848b8605Smrg * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22848b8605Smrg * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23848b8605Smrg * OTHER DEALINGS IN THE SOFTWARE.
24848b8605Smrg */
25848b8605Smrg
26848b8605Smrg
27848b8605Smrg/**
28848b8605Smrg * \file s_texfetch.c
29848b8605Smrg *
30848b8605Smrg * Texel fetch/store functions
31848b8605Smrg *
32848b8605Smrg * \author Gareth Hughes
33848b8605Smrg */
34848b8605Smrg
35848b8605Smrg
36b8e80941Smrg#include "main/errors.h"
37848b8605Smrg#include "main/macros.h"
38848b8605Smrg#include "main/texcompress.h"
39848b8605Smrg#include "main/texcompress_fxt1.h"
40848b8605Smrg#include "main/texcompress_s3tc.h"
41848b8605Smrg#include "main/texcompress_rgtc.h"
42848b8605Smrg#include "main/texcompress_etc.h"
43848b8605Smrg#include "main/teximage.h"
44848b8605Smrg#include "main/samplerobj.h"
45848b8605Smrg#include "s_context.h"
46848b8605Smrg#include "s_texfetch.h"
47b8e80941Smrg#include "util/format_rgb9e5.h"
48b8e80941Smrg#include "util/format_r11g11b10f.h"
49848b8605Smrg#include "util/format_srgb.h"
50848b8605Smrg
51848b8605Smrg
52848b8605Smrg/* Texel fetch routines for all supported formats
53848b8605Smrg */
54848b8605Smrg#define DIM 1
55848b8605Smrg#include "s_texfetch_tmp.h"
56848b8605Smrg
57848b8605Smrg#define DIM 2
58848b8605Smrg#include "s_texfetch_tmp.h"
59848b8605Smrg
60848b8605Smrg#define DIM 3
61848b8605Smrg#include "s_texfetch_tmp.h"
62848b8605Smrg
63848b8605Smrg
64848b8605Smrg/**
65848b8605Smrg * All compressed texture texel fetching is done though this function.
66848b8605Smrg * Basically just call a core-Mesa texel fetch function.
67848b8605Smrg */
68848b8605Smrgstatic void
69848b8605Smrgfetch_compressed(const struct swrast_texture_image *swImage,
70848b8605Smrg                 GLint i, GLint j, GLint k, GLfloat *texel)
71848b8605Smrg{
72848b8605Smrg   /* The FetchCompressedTexel function takes an integer pixel rowstride,
73848b8605Smrg    * while the image's rowstride is bytes per row of blocks.
74848b8605Smrg    */
75848b8605Smrg   GLuint bw, bh;
76848b8605Smrg   GLuint texelBytes = _mesa_get_format_bytes(swImage->Base.TexFormat);
77848b8605Smrg   _mesa_get_format_block_size(swImage->Base.TexFormat, &bw, &bh);
78848b8605Smrg   assert(swImage->RowStride * bw % texelBytes == 0);
79848b8605Smrg
80848b8605Smrg   swImage->FetchCompressedTexel(swImage->ImageSlices[k],
81848b8605Smrg                                 swImage->RowStride * bw / texelBytes,
82848b8605Smrg                                 i, j, texel);
83848b8605Smrg}
84848b8605Smrg
85848b8605Smrg
86848b8605Smrg
87848b8605Smrg/**
88848b8605Smrg * Null texel fetch function.
89848b8605Smrg *
90848b8605Smrg * Have to have this so the FetchTexel function pointer is never NULL.
91848b8605Smrg */
92848b8605Smrgstatic void fetch_null_texelf( const struct swrast_texture_image *texImage,
93848b8605Smrg                               GLint i, GLint j, GLint k, GLfloat *texel )
94848b8605Smrg{
95848b8605Smrg   (void) texImage; (void) i; (void) j; (void) k;
96848b8605Smrg   texel[RCOMP] = 0.0;
97848b8605Smrg   texel[GCOMP] = 0.0;
98848b8605Smrg   texel[BCOMP] = 0.0;
99848b8605Smrg   texel[ACOMP] = 0.0;
100848b8605Smrg   _mesa_warning(NULL, "fetch_null_texelf() called!");
101848b8605Smrg}
102848b8605Smrg
103848b8605Smrg
104848b8605Smrg#define FETCH_FUNCS(NAME)       \
105848b8605Smrg   {                            \
106848b8605Smrg      MESA_FORMAT_ ## NAME,     \
107848b8605Smrg      fetch_texel_1d_ ## NAME,  \
108848b8605Smrg      fetch_texel_2d_ ## NAME,  \
109848b8605Smrg      fetch_texel_3d_ ## NAME,  \
110848b8605Smrg   }
111848b8605Smrg
112848b8605Smrg#define FETCH_NULL(NAME)        \
113848b8605Smrg   {                            \
114848b8605Smrg      MESA_FORMAT_ ## NAME,     \
115848b8605Smrg      NULL,                     \
116848b8605Smrg      NULL,                     \
117848b8605Smrg      NULL                      \
118848b8605Smrg   }
119848b8605Smrg
120b8e80941Smrg#define FETCH_COMPRESSED(NAME)  \
121b8e80941Smrg   {                            \
122b8e80941Smrg      MESA_FORMAT_ ## NAME,     \
123b8e80941Smrg      fetch_compressed,         \
124b8e80941Smrg      fetch_compressed,         \
125b8e80941Smrg      fetch_compressed          \
126b8e80941Smrg   }
127b8e80941Smrg
128848b8605Smrg/**
129848b8605Smrg * Table to map MESA_FORMAT_ to texel fetch/store funcs.
130848b8605Smrg */
131848b8605Smrgstatic struct {
132848b8605Smrg   mesa_format Name;
133848b8605Smrg   FetchTexelFunc Fetch1D;
134848b8605Smrg   FetchTexelFunc Fetch2D;
135848b8605Smrg   FetchTexelFunc Fetch3D;
136848b8605Smrg}
137848b8605Smrgtexfetch_funcs[] =
138848b8605Smrg{
139848b8605Smrg   {
140848b8605Smrg      MESA_FORMAT_NONE,
141848b8605Smrg      fetch_null_texelf,
142848b8605Smrg      fetch_null_texelf,
143848b8605Smrg      fetch_null_texelf
144848b8605Smrg   },
145848b8605Smrg
146848b8605Smrg   /* Packed unorm formats */
147848b8605Smrg   FETCH_FUNCS(A8B8G8R8_UNORM),
148848b8605Smrg   FETCH_FUNCS(X8B8G8R8_UNORM),
149848b8605Smrg   FETCH_FUNCS(R8G8B8A8_UNORM),
150848b8605Smrg   FETCH_FUNCS(R8G8B8X8_UNORM),
151848b8605Smrg   FETCH_FUNCS(B8G8R8A8_UNORM),
152848b8605Smrg   FETCH_FUNCS(B8G8R8X8_UNORM),
153848b8605Smrg   FETCH_FUNCS(A8R8G8B8_UNORM),
154848b8605Smrg   FETCH_FUNCS(X8R8G8B8_UNORM),
155848b8605Smrg   FETCH_FUNCS(L16A16_UNORM),
156848b8605Smrg   FETCH_FUNCS(A16L16_UNORM),
157848b8605Smrg   FETCH_FUNCS(B5G6R5_UNORM),
158848b8605Smrg   FETCH_FUNCS(R5G6B5_UNORM),
159848b8605Smrg   FETCH_FUNCS(B4G4R4A4_UNORM),
160848b8605Smrg   FETCH_NULL(B4G4R4X4_UNORM),
161848b8605Smrg   FETCH_FUNCS(A4R4G4B4_UNORM),
162848b8605Smrg   FETCH_FUNCS(A1B5G5R5_UNORM),
163b8e80941Smrg   FETCH_NULL(X1B5G5R5_UNORM),
164848b8605Smrg   FETCH_FUNCS(B5G5R5A1_UNORM),
165848b8605Smrg   FETCH_NULL(B5G5R5X1_UNORM),
166848b8605Smrg   FETCH_FUNCS(A1R5G5B5_UNORM),
167848b8605Smrg   FETCH_FUNCS(L8A8_UNORM),
168848b8605Smrg   FETCH_FUNCS(A8L8_UNORM),
169848b8605Smrg   FETCH_FUNCS(R8G8_UNORM),
170848b8605Smrg   FETCH_FUNCS(G8R8_UNORM),
171848b8605Smrg   FETCH_FUNCS(L4A4_UNORM),
172848b8605Smrg   FETCH_FUNCS(B2G3R3_UNORM),
173848b8605Smrg   FETCH_FUNCS(R16G16_UNORM),
174848b8605Smrg   FETCH_FUNCS(G16R16_UNORM),
175848b8605Smrg   FETCH_FUNCS(B10G10R10A2_UNORM),
176848b8605Smrg   FETCH_NULL(B10G10R10X2_UNORM),
177848b8605Smrg   FETCH_FUNCS(R10G10B10A2_UNORM),
178b8e80941Smrg   FETCH_NULL(R10G10B10X2_UNORM),
179b8e80941Smrg
180848b8605Smrg   FETCH_FUNCS(S8_UINT_Z24_UNORM),
181848b8605Smrg   {
182848b8605Smrg      MESA_FORMAT_X8_UINT_Z24_UNORM,
183848b8605Smrg      fetch_texel_1d_S8_UINT_Z24_UNORM,
184848b8605Smrg      fetch_texel_2d_S8_UINT_Z24_UNORM,
185848b8605Smrg      fetch_texel_3d_S8_UINT_Z24_UNORM
186848b8605Smrg   },
187848b8605Smrg   FETCH_FUNCS(Z24_UNORM_S8_UINT),
188848b8605Smrg   {
189848b8605Smrg      MESA_FORMAT_Z24_UNORM_X8_UINT,
190848b8605Smrg      fetch_texel_1d_Z24_UNORM_S8_UINT,
191848b8605Smrg      fetch_texel_2d_Z24_UNORM_S8_UINT,
192848b8605Smrg      fetch_texel_3d_Z24_UNORM_S8_UINT
193848b8605Smrg   },
194b8e80941Smrg   FETCH_NULL(R3G3B2_UNORM),
195b8e80941Smrg   FETCH_NULL(A4B4G4R4_UNORM),
196b8e80941Smrg   FETCH_NULL(R4G4B4A4_UNORM),
197b8e80941Smrg   FETCH_NULL(R5G5B5A1_UNORM),
198b8e80941Smrg   FETCH_NULL(A2B10G10R10_UNORM),
199b8e80941Smrg   FETCH_NULL(A2R10G10B10_UNORM),
200b8e80941Smrg
201848b8605Smrg   FETCH_FUNCS(YCBCR),
202848b8605Smrg   FETCH_FUNCS(YCBCR_REV),
203848b8605Smrg
204848b8605Smrg   /* Array unorm formats */
205848b8605Smrg   FETCH_FUNCS(A_UNORM8),
206848b8605Smrg   FETCH_FUNCS(A_UNORM16),
207848b8605Smrg   FETCH_FUNCS(L_UNORM8),
208848b8605Smrg   FETCH_FUNCS(L_UNORM16),
209848b8605Smrg   FETCH_FUNCS(I_UNORM8),
210848b8605Smrg   FETCH_FUNCS(I_UNORM16),
211848b8605Smrg   FETCH_FUNCS(R_UNORM8),
212848b8605Smrg   FETCH_FUNCS(R_UNORM16),
213848b8605Smrg   FETCH_FUNCS(BGR_UNORM8),
214848b8605Smrg   FETCH_FUNCS(RGB_UNORM8),
215848b8605Smrg   FETCH_FUNCS(RGBA_UNORM16),
216848b8605Smrg   FETCH_FUNCS(RGBX_UNORM16),
217848b8605Smrg   FETCH_FUNCS(Z_UNORM16),
218848b8605Smrg   FETCH_FUNCS(Z_UNORM32),
219848b8605Smrg   FETCH_NULL(S_UINT8),
220848b8605Smrg
221848b8605Smrg   /* Packed signed/normalized formats */
222848b8605Smrg   FETCH_FUNCS(A8B8G8R8_SNORM),
223848b8605Smrg   FETCH_FUNCS(X8B8G8R8_SNORM),
224848b8605Smrg   FETCH_FUNCS(R8G8B8A8_SNORM),
225848b8605Smrg   FETCH_NULL(R8G8B8X8_SNORM),
226848b8605Smrg   FETCH_FUNCS(R16G16_SNORM),
227848b8605Smrg   FETCH_NULL(G16R16_SNORM),
228848b8605Smrg   FETCH_FUNCS(R8G8_SNORM),
229848b8605Smrg   FETCH_NULL(G8R8_SNORM),
230848b8605Smrg   FETCH_FUNCS(L8A8_SNORM),
231b8e80941Smrg   FETCH_FUNCS(A8L8_SNORM),
232848b8605Smrg
233848b8605Smrg   /* Array signed/normalized formats */
234848b8605Smrg   FETCH_FUNCS(A_SNORM8),
235848b8605Smrg   FETCH_FUNCS(A_SNORM16),
236848b8605Smrg   FETCH_FUNCS(L_SNORM8),
237848b8605Smrg   FETCH_FUNCS(L_SNORM16),
238848b8605Smrg   FETCH_FUNCS(I_SNORM8),
239848b8605Smrg   FETCH_FUNCS(I_SNORM16),
240848b8605Smrg   FETCH_FUNCS(R_SNORM8),
241848b8605Smrg   FETCH_FUNCS(R_SNORM16),
242848b8605Smrg   FETCH_FUNCS(LA_SNORM16),
243848b8605Smrg   FETCH_FUNCS(RGB_SNORM16),
244848b8605Smrg   FETCH_FUNCS(RGBA_SNORM16),
245848b8605Smrg   FETCH_NULL(RGBX_SNORM16),
246848b8605Smrg
247848b8605Smrg   /* Packed sRGB formats */
248848b8605Smrg   FETCH_FUNCS(A8B8G8R8_SRGB),
249848b8605Smrg   FETCH_FUNCS(B8G8R8A8_SRGB),
250b8e80941Smrg   FETCH_FUNCS(A8R8G8B8_SRGB),
251848b8605Smrg   FETCH_NULL(B8G8R8X8_SRGB),
252b8e80941Smrg   FETCH_NULL(X8R8G8B8_SRGB),
253848b8605Smrg   FETCH_FUNCS(R8G8B8A8_SRGB),
254848b8605Smrg   FETCH_FUNCS(R8G8B8X8_SRGB),
255b8e80941Smrg   FETCH_FUNCS(X8B8G8R8_SRGB),
256848b8605Smrg   FETCH_FUNCS(L8A8_SRGB),
257b8e80941Smrg   FETCH_FUNCS(A8L8_SRGB),
258848b8605Smrg
259848b8605Smrg   /* Array sRGB formats */
260b8e80941Smrg   FETCH_FUNCS(R_SRGB8),
261848b8605Smrg   FETCH_FUNCS(L_SRGB8),
262848b8605Smrg   FETCH_FUNCS(BGR_SRGB8),
263848b8605Smrg
264848b8605Smrg   /* Packed float formats */
265848b8605Smrg   FETCH_FUNCS(R9G9B9E5_FLOAT),
266848b8605Smrg   FETCH_FUNCS(R11G11B10_FLOAT),
267848b8605Smrg   FETCH_FUNCS(Z32_FLOAT_S8X24_UINT),
268848b8605Smrg
269848b8605Smrg   /* Array float formats */
270848b8605Smrg   FETCH_FUNCS(A_FLOAT16),
271848b8605Smrg   FETCH_FUNCS(A_FLOAT32),
272848b8605Smrg   FETCH_FUNCS(L_FLOAT16),
273848b8605Smrg   FETCH_FUNCS(L_FLOAT32),
274848b8605Smrg   FETCH_FUNCS(LA_FLOAT16),
275848b8605Smrg   FETCH_FUNCS(LA_FLOAT32),
276848b8605Smrg   FETCH_FUNCS(I_FLOAT16),
277848b8605Smrg   FETCH_FUNCS(I_FLOAT32),
278848b8605Smrg   FETCH_FUNCS(R_FLOAT16),
279848b8605Smrg   FETCH_FUNCS(R_FLOAT32),
280848b8605Smrg   FETCH_FUNCS(RG_FLOAT16),
281848b8605Smrg   FETCH_FUNCS(RG_FLOAT32),
282848b8605Smrg   FETCH_FUNCS(RGB_FLOAT16),
283848b8605Smrg   FETCH_FUNCS(RGB_FLOAT32),
284848b8605Smrg   FETCH_FUNCS(RGBA_FLOAT16),
285848b8605Smrg   FETCH_FUNCS(RGBA_FLOAT32),
286848b8605Smrg   FETCH_FUNCS(RGBX_FLOAT16),
287848b8605Smrg   FETCH_FUNCS(RGBX_FLOAT32),
288848b8605Smrg   {
289848b8605Smrg      MESA_FORMAT_Z_FLOAT32,
290848b8605Smrg      fetch_texel_1d_R_FLOAT32, /* Reuse the R32F functions. */
291848b8605Smrg      fetch_texel_2d_R_FLOAT32,
292848b8605Smrg      fetch_texel_3d_R_FLOAT32
293848b8605Smrg   },
294848b8605Smrg
295848b8605Smrg   /* Packed signed/unsigned non-normalized integer formats */
296b8e80941Smrg   FETCH_NULL(A8B8G8R8_UINT),
297b8e80941Smrg   FETCH_NULL(A8R8G8B8_UINT),
298b8e80941Smrg   FETCH_NULL(R8G8B8A8_UINT),
299b8e80941Smrg   FETCH_NULL(B8G8R8A8_UINT),
300848b8605Smrg   FETCH_NULL(B10G10R10A2_UINT),
301848b8605Smrg   FETCH_NULL(R10G10B10A2_UINT),
302b8e80941Smrg   FETCH_NULL(A2B10G10R10_UINT),
303b8e80941Smrg   FETCH_NULL(A2R10G10B10_UINT),
304b8e80941Smrg   FETCH_NULL(B5G6R5_UINT),
305b8e80941Smrg   FETCH_NULL(R5G6B5_UINT),
306b8e80941Smrg   FETCH_NULL(B2G3R3_UINT),
307b8e80941Smrg   FETCH_NULL(R3G3B2_UINT),
308b8e80941Smrg   FETCH_NULL(A4B4G4R4_UINT),
309b8e80941Smrg   FETCH_NULL(R4G4B4A4_UINT),
310b8e80941Smrg   FETCH_NULL(B4G4R4A4_UINT),
311b8e80941Smrg   FETCH_NULL(A4R4G4B4_UINT),
312b8e80941Smrg   FETCH_NULL(A1B5G5R5_UINT),
313b8e80941Smrg   FETCH_NULL(B5G5R5A1_UINT),
314b8e80941Smrg   FETCH_NULL(A1R5G5B5_UINT),
315b8e80941Smrg   FETCH_NULL(R5G5B5A1_UINT),
316848b8605Smrg
317848b8605Smrg   /* Array signed/unsigned non-normalized integer formats */
318848b8605Smrg   FETCH_NULL(A_UINT8),
319848b8605Smrg   FETCH_NULL(A_UINT16),
320848b8605Smrg   FETCH_NULL(A_UINT32),
321848b8605Smrg   FETCH_NULL(A_SINT8),
322848b8605Smrg   FETCH_NULL(A_SINT16),
323848b8605Smrg   FETCH_NULL(A_SINT32),
324848b8605Smrg   FETCH_NULL(I_UINT8),
325848b8605Smrg   FETCH_NULL(I_UINT16),
326848b8605Smrg   FETCH_NULL(I_UINT32),
327848b8605Smrg   FETCH_NULL(I_SINT8),
328848b8605Smrg   FETCH_NULL(I_SINT16),
329848b8605Smrg   FETCH_NULL(I_SINT32),
330848b8605Smrg   FETCH_NULL(L_UINT8),
331848b8605Smrg   FETCH_NULL(L_UINT16),
332848b8605Smrg   FETCH_NULL(L_UINT32),
333848b8605Smrg   FETCH_NULL(L_SINT8),
334848b8605Smrg   FETCH_NULL(L_SINT16),
335848b8605Smrg   FETCH_NULL(L_SINT32),
336848b8605Smrg   FETCH_NULL(LA_UINT8),
337848b8605Smrg   FETCH_NULL(LA_UINT16),
338848b8605Smrg   FETCH_NULL(LA_UINT32),
339848b8605Smrg   FETCH_NULL(LA_SINT8),
340848b8605Smrg   FETCH_NULL(LA_SINT16),
341848b8605Smrg   FETCH_NULL(LA_SINT32),
342848b8605Smrg   FETCH_NULL(R_UINT8),
343848b8605Smrg   FETCH_NULL(R_UINT16),
344848b8605Smrg   FETCH_NULL(R_UINT32),
345848b8605Smrg   FETCH_NULL(R_SINT8),
346848b8605Smrg   FETCH_NULL(R_SINT16),
347848b8605Smrg   FETCH_NULL(R_SINT32),
348848b8605Smrg   FETCH_NULL(RG_UINT8),
349848b8605Smrg   FETCH_NULL(RG_UINT16),
350848b8605Smrg   FETCH_NULL(RG_UINT32),
351848b8605Smrg   FETCH_NULL(RG_SINT8),
352848b8605Smrg   FETCH_NULL(RG_SINT16),
353848b8605Smrg   FETCH_NULL(RG_SINT32),
354848b8605Smrg   FETCH_NULL(RGB_UINT8),
355848b8605Smrg   FETCH_NULL(RGB_UINT16),
356848b8605Smrg   FETCH_NULL(RGB_UINT32),
357848b8605Smrg   FETCH_NULL(RGB_SINT8),
358848b8605Smrg   FETCH_NULL(RGB_SINT16),
359848b8605Smrg   FETCH_NULL(RGB_SINT32),
360848b8605Smrg   FETCH_FUNCS(RGBA_UINT8),
361848b8605Smrg   FETCH_FUNCS(RGBA_UINT16),
362848b8605Smrg   FETCH_FUNCS(RGBA_UINT32),
363848b8605Smrg   FETCH_FUNCS(RGBA_SINT8),
364848b8605Smrg   FETCH_FUNCS(RGBA_SINT16),
365848b8605Smrg   FETCH_FUNCS(RGBA_SINT32),
366848b8605Smrg   FETCH_NULL(RGBX_UINT8),
367848b8605Smrg   FETCH_NULL(RGBX_UINT16),
368848b8605Smrg   FETCH_NULL(RGBX_UINT32),
369848b8605Smrg   FETCH_NULL(RGBX_SINT8),
370848b8605Smrg   FETCH_NULL(RGBX_SINT16),
371848b8605Smrg   FETCH_NULL(RGBX_SINT32),
372848b8605Smrg
373848b8605Smrg   /* DXT compressed formats */
374b8e80941Smrg   FETCH_COMPRESSED(RGB_DXT1),
375b8e80941Smrg   FETCH_COMPRESSED(RGBA_DXT1),
376b8e80941Smrg   FETCH_COMPRESSED(RGBA_DXT3),
377b8e80941Smrg   FETCH_COMPRESSED(RGBA_DXT5),
378848b8605Smrg
379848b8605Smrg   /* DXT sRGB compressed formats */
380b8e80941Smrg   FETCH_COMPRESSED(SRGB_DXT1),
381b8e80941Smrg   FETCH_COMPRESSED(SRGBA_DXT1),
382b8e80941Smrg   FETCH_COMPRESSED(SRGBA_DXT3),
383b8e80941Smrg   FETCH_COMPRESSED(SRGBA_DXT5),
384848b8605Smrg
385848b8605Smrg   /* FXT1 compressed formats */
386b8e80941Smrg   FETCH_COMPRESSED(RGB_FXT1),
387b8e80941Smrg   FETCH_COMPRESSED(RGBA_FXT1),
388848b8605Smrg
389848b8605Smrg   /* RGTC compressed formats */
390b8e80941Smrg   FETCH_COMPRESSED(R_RGTC1_UNORM),
391b8e80941Smrg   FETCH_COMPRESSED(R_RGTC1_SNORM),
392b8e80941Smrg   FETCH_COMPRESSED(RG_RGTC2_UNORM),
393b8e80941Smrg   FETCH_COMPRESSED(RG_RGTC2_SNORM),
394848b8605Smrg
395848b8605Smrg   /* LATC1/2 compressed formats */
396b8e80941Smrg   FETCH_COMPRESSED(L_LATC1_UNORM),
397b8e80941Smrg   FETCH_COMPRESSED(L_LATC1_SNORM),
398b8e80941Smrg   FETCH_COMPRESSED(LA_LATC2_UNORM),
399b8e80941Smrg   FETCH_COMPRESSED(LA_LATC2_SNORM),
400848b8605Smrg
401848b8605Smrg   /* ETC1/2 compressed formats */
402b8e80941Smrg   FETCH_COMPRESSED(ETC1_RGB8),
403b8e80941Smrg   FETCH_COMPRESSED(ETC2_RGB8),
404b8e80941Smrg   FETCH_COMPRESSED(ETC2_SRGB8),
405b8e80941Smrg   FETCH_COMPRESSED(ETC2_RGBA8_EAC),
406b8e80941Smrg   FETCH_COMPRESSED(ETC2_SRGB8_ALPHA8_EAC),
407b8e80941Smrg   FETCH_COMPRESSED(ETC2_R11_EAC),
408b8e80941Smrg   FETCH_COMPRESSED(ETC2_RG11_EAC),
409b8e80941Smrg   FETCH_COMPRESSED(ETC2_SIGNED_R11_EAC),
410b8e80941Smrg   FETCH_COMPRESSED(ETC2_SIGNED_RG11_EAC),
411b8e80941Smrg   FETCH_COMPRESSED(ETC2_RGB8_PUNCHTHROUGH_ALPHA1),
412b8e80941Smrg   FETCH_COMPRESSED(ETC2_SRGB8_PUNCHTHROUGH_ALPHA1),
413b8e80941Smrg   FETCH_COMPRESSED(BPTC_RGBA_UNORM),
414b8e80941Smrg   FETCH_COMPRESSED(BPTC_SRGB_ALPHA_UNORM),
415b8e80941Smrg   FETCH_COMPRESSED(BPTC_RGB_SIGNED_FLOAT),
416b8e80941Smrg   FETCH_COMPRESSED(BPTC_RGB_UNSIGNED_FLOAT),
417b8e80941Smrg
418b8e80941Smrg   /* ASTC compressed formats */
419b8e80941Smrg   FETCH_NULL(RGBA_ASTC_4x4),
420b8e80941Smrg   FETCH_NULL(RGBA_ASTC_5x4),
421b8e80941Smrg   FETCH_NULL(RGBA_ASTC_5x5),
422b8e80941Smrg   FETCH_NULL(RGBA_ASTC_6x5),
423b8e80941Smrg   FETCH_NULL(RGBA_ASTC_6x6),
424b8e80941Smrg   FETCH_NULL(RGBA_ASTC_8x5),
425b8e80941Smrg   FETCH_NULL(RGBA_ASTC_8x6),
426b8e80941Smrg   FETCH_NULL(RGBA_ASTC_8x8),
427b8e80941Smrg   FETCH_NULL(RGBA_ASTC_10x5),
428b8e80941Smrg   FETCH_NULL(RGBA_ASTC_10x6),
429b8e80941Smrg   FETCH_NULL(RGBA_ASTC_10x8),
430b8e80941Smrg   FETCH_NULL(RGBA_ASTC_10x10),
431b8e80941Smrg   FETCH_NULL(RGBA_ASTC_12x10),
432b8e80941Smrg   FETCH_NULL(RGBA_ASTC_12x12),
433b8e80941Smrg   FETCH_NULL(SRGB8_ALPHA8_ASTC_4x4),
434b8e80941Smrg   FETCH_NULL(SRGB8_ALPHA8_ASTC_5x4),
435b8e80941Smrg   FETCH_NULL(SRGB8_ALPHA8_ASTC_5x5),
436b8e80941Smrg   FETCH_NULL(SRGB8_ALPHA8_ASTC_6x5),
437b8e80941Smrg   FETCH_NULL(SRGB8_ALPHA8_ASTC_6x6),
438b8e80941Smrg   FETCH_NULL(SRGB8_ALPHA8_ASTC_8x5),
439b8e80941Smrg   FETCH_NULL(SRGB8_ALPHA8_ASTC_8x6),
440b8e80941Smrg   FETCH_NULL(SRGB8_ALPHA8_ASTC_8x8),
441b8e80941Smrg   FETCH_NULL(SRGB8_ALPHA8_ASTC_10x5),
442b8e80941Smrg   FETCH_NULL(SRGB8_ALPHA8_ASTC_10x6),
443b8e80941Smrg   FETCH_NULL(SRGB8_ALPHA8_ASTC_10x8),
444b8e80941Smrg   FETCH_NULL(SRGB8_ALPHA8_ASTC_10x10),
445b8e80941Smrg   FETCH_NULL(SRGB8_ALPHA8_ASTC_12x10),
446b8e80941Smrg   FETCH_NULL(SRGB8_ALPHA8_ASTC_12x12),
447b8e80941Smrg
448b8e80941Smrg   FETCH_NULL(RGBA_ASTC_3x3x3),
449b8e80941Smrg   FETCH_NULL(RGBA_ASTC_4x3x3),
450b8e80941Smrg   FETCH_NULL(RGBA_ASTC_4x4x3),
451b8e80941Smrg   FETCH_NULL(RGBA_ASTC_4x4x4),
452b8e80941Smrg   FETCH_NULL(RGBA_ASTC_5x4x4),
453b8e80941Smrg   FETCH_NULL(RGBA_ASTC_5x5x4),
454b8e80941Smrg   FETCH_NULL(RGBA_ASTC_5x5x5),
455b8e80941Smrg   FETCH_NULL(RGBA_ASTC_6x5x5),
456b8e80941Smrg   FETCH_NULL(RGBA_ASTC_6x6x5),
457b8e80941Smrg   FETCH_NULL(RGBA_ASTC_6x6x6),
458b8e80941Smrg   FETCH_NULL(SRGB8_ALPHA8_ASTC_3x3x3),
459b8e80941Smrg   FETCH_NULL(SRGB8_ALPHA8_ASTC_4x3x3),
460b8e80941Smrg   FETCH_NULL(SRGB8_ALPHA8_ASTC_4x4x3),
461b8e80941Smrg   FETCH_NULL(SRGB8_ALPHA8_ASTC_4x4x4),
462b8e80941Smrg   FETCH_NULL(SRGB8_ALPHA8_ASTC_5x4x4),
463b8e80941Smrg   FETCH_NULL(SRGB8_ALPHA8_ASTC_5x5x4),
464b8e80941Smrg   FETCH_NULL(SRGB8_ALPHA8_ASTC_5x5x5),
465b8e80941Smrg   FETCH_NULL(SRGB8_ALPHA8_ASTC_6x5x5),
466b8e80941Smrg   FETCH_NULL(SRGB8_ALPHA8_ASTC_6x6x5),
467b8e80941Smrg   FETCH_NULL(SRGB8_ALPHA8_ASTC_6x6x6),
468b8e80941Smrg
469b8e80941Smrg   FETCH_NULL(ATC_RGB),
470b8e80941Smrg   FETCH_NULL(ATC_RGBA_EXPLICIT),
471b8e80941Smrg   FETCH_NULL(ATC_RGBA_INTERPOLATED)
472848b8605Smrg};
473848b8605Smrg
474848b8605Smrg
475848b8605Smrg/**
476848b8605Smrg * Initialize the texture image's FetchTexel methods.
477848b8605Smrg */
478848b8605Smrgstatic void
479848b8605Smrgset_fetch_functions(const struct gl_sampler_object *samp,
480848b8605Smrg                    struct swrast_texture_image *texImage, GLuint dims)
481848b8605Smrg{
482848b8605Smrg   mesa_format format = texImage->Base.TexFormat;
483848b8605Smrg
484848b8605Smrg#ifdef DEBUG
485848b8605Smrg   /* check that the table entries are sorted by format name */
486848b8605Smrg   mesa_format fmt;
487848b8605Smrg   for (fmt = 0; fmt < MESA_FORMAT_COUNT; fmt++) {
488848b8605Smrg      assert(texfetch_funcs[fmt].Name == fmt);
489848b8605Smrg   }
490848b8605Smrg#endif
491848b8605Smrg
492b8e80941Smrg   STATIC_ASSERT(ARRAY_SIZE(texfetch_funcs) == MESA_FORMAT_COUNT);
493848b8605Smrg
494848b8605Smrg   if (samp->sRGBDecode == GL_SKIP_DECODE_EXT &&
495848b8605Smrg       _mesa_get_format_color_encoding(format) == GL_SRGB) {
496848b8605Smrg      format = _mesa_get_srgb_format_linear(format);
497848b8605Smrg   }
498848b8605Smrg
499848b8605Smrg   assert(format < MESA_FORMAT_COUNT);
500848b8605Smrg
501848b8605Smrg   switch (dims) {
502848b8605Smrg   case 1:
503848b8605Smrg      texImage->FetchTexel = texfetch_funcs[format].Fetch1D;
504848b8605Smrg      break;
505848b8605Smrg   case 2:
506848b8605Smrg      texImage->FetchTexel = texfetch_funcs[format].Fetch2D;
507848b8605Smrg      break;
508848b8605Smrg   case 3:
509848b8605Smrg      texImage->FetchTexel = texfetch_funcs[format].Fetch3D;
510848b8605Smrg      break;
511848b8605Smrg   default:
512848b8605Smrg      assert(!"Bad dims in set_fetch_functions()");
513848b8605Smrg   }
514848b8605Smrg
515848b8605Smrg   texImage->FetchCompressedTexel = _mesa_get_compressed_fetch_func(format);
516848b8605Smrg
517b8e80941Smrg   assert(texImage->FetchTexel);
518848b8605Smrg}
519848b8605Smrg
520848b8605Smrgvoid
521848b8605Smrg_mesa_update_fetch_functions(struct gl_context *ctx, GLuint unit)
522848b8605Smrg{
523848b8605Smrg   struct gl_texture_object *texObj = ctx->Texture.Unit[unit]._Current;
524848b8605Smrg   struct gl_sampler_object *samp;
525848b8605Smrg   GLuint face, i;
526848b8605Smrg   GLuint dims;
527848b8605Smrg
528848b8605Smrg   if (!texObj)
529848b8605Smrg      return;
530848b8605Smrg
531848b8605Smrg   samp = _mesa_get_samplerobj(ctx, unit);
532848b8605Smrg
533848b8605Smrg   dims = _mesa_get_texture_dimensions(texObj->Target);
534848b8605Smrg
535848b8605Smrg   for (face = 0; face < 6; face++) {
536848b8605Smrg      for (i = 0; i < MAX_TEXTURE_LEVELS; i++) {
537848b8605Smrg         if (texObj->Image[face][i]) {
538848b8605Smrg	    set_fetch_functions(samp,
539848b8605Smrg                                swrast_texture_image(texObj->Image[face][i]),
540848b8605Smrg                                dims);
541848b8605Smrg         }
542848b8605Smrg      }
543848b8605Smrg   }
544848b8605Smrg}
545