1bf215546Sopenharmony_ci/*
2bf215546Sopenharmony_ci * Copyright (C) 2011 Red Hat Inc.
3bf215546Sopenharmony_ci *
4bf215546Sopenharmony_ci * block compression parts are:
5bf215546Sopenharmony_ci * Copyright (C) 2004  Roland Scheidegger   All Rights Reserved.
6bf215546Sopenharmony_ci *
7bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
8bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
9bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation
10bf215546Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11bf215546Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the
12bf215546Sopenharmony_ci * Software is furnished to do so, subject to the following conditions:
13bf215546Sopenharmony_ci *
14bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the next
15bf215546Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the
16bf215546Sopenharmony_ci * Software.
17bf215546Sopenharmony_ci *
18bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19bf215546Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
21bf215546Sopenharmony_ci * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22bf215546Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23bf215546Sopenharmony_ci * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24bf215546Sopenharmony_ci * DEALINGS IN THE SOFTWARE.
25bf215546Sopenharmony_ci *
26bf215546Sopenharmony_ci * Author:
27bf215546Sopenharmony_ci *    Dave Airlie
28bf215546Sopenharmony_ci */
29bf215546Sopenharmony_ci
30bf215546Sopenharmony_ci/**
31bf215546Sopenharmony_ci * \file texcompress_rgtc.c
32bf215546Sopenharmony_ci * GL_EXT_texture_compression_rgtc support.
33bf215546Sopenharmony_ci */
34bf215546Sopenharmony_ci
35bf215546Sopenharmony_ci#include <stdlib.h>
36bf215546Sopenharmony_ci
37bf215546Sopenharmony_ci#include "config.h"
38bf215546Sopenharmony_ci#include "glheader.h"
39bf215546Sopenharmony_ci
40bf215546Sopenharmony_ci#include "image.h"
41bf215546Sopenharmony_ci#include "macros.h"
42bf215546Sopenharmony_ci#include "mipmap.h"
43bf215546Sopenharmony_ci#include "texcompress.h"
44bf215546Sopenharmony_ci#include "util/rgtc.h"
45bf215546Sopenharmony_ci#include "texcompress_rgtc.h"
46bf215546Sopenharmony_ci#include "texstore.h"
47bf215546Sopenharmony_ci
48bf215546Sopenharmony_cistatic void extractsrc_u( GLubyte srcpixels[4][4], const GLubyte *srcaddr,
49bf215546Sopenharmony_ci			  GLint srcRowStride, GLint numxpixels, GLint numypixels, GLint comps)
50bf215546Sopenharmony_ci{
51bf215546Sopenharmony_ci   GLubyte i, j;
52bf215546Sopenharmony_ci   const GLubyte *curaddr;
53bf215546Sopenharmony_ci   for (j = 0; j < numypixels; j++) {
54bf215546Sopenharmony_ci      curaddr = srcaddr + j * srcRowStride * comps;
55bf215546Sopenharmony_ci      for (i = 0; i < numxpixels; i++) {
56bf215546Sopenharmony_ci	 srcpixels[j][i] = *curaddr;
57bf215546Sopenharmony_ci	 curaddr += comps;
58bf215546Sopenharmony_ci      }
59bf215546Sopenharmony_ci   }
60bf215546Sopenharmony_ci}
61bf215546Sopenharmony_ci
62bf215546Sopenharmony_cistatic void extractsrc_s( GLbyte srcpixels[4][4], const GLfloat *srcaddr,
63bf215546Sopenharmony_ci			  GLint srcRowStride, GLint numxpixels, GLint numypixels, GLint comps)
64bf215546Sopenharmony_ci{
65bf215546Sopenharmony_ci   GLubyte i, j;
66bf215546Sopenharmony_ci   const GLfloat *curaddr;
67bf215546Sopenharmony_ci   for (j = 0; j < numypixels; j++) {
68bf215546Sopenharmony_ci      curaddr = srcaddr + j * srcRowStride * comps;
69bf215546Sopenharmony_ci      for (i = 0; i < numxpixels; i++) {
70bf215546Sopenharmony_ci	 srcpixels[j][i] = FLOAT_TO_BYTE_TEX(*curaddr);
71bf215546Sopenharmony_ci	 curaddr += comps;
72bf215546Sopenharmony_ci      }
73bf215546Sopenharmony_ci   }
74bf215546Sopenharmony_ci}
75bf215546Sopenharmony_ci
76bf215546Sopenharmony_ci
77bf215546Sopenharmony_ciGLboolean
78bf215546Sopenharmony_ci_mesa_texstore_red_rgtc1(TEXSTORE_PARAMS)
79bf215546Sopenharmony_ci{
80bf215546Sopenharmony_ci   GLubyte *dst;
81bf215546Sopenharmony_ci   const GLubyte *tempImage = NULL;
82bf215546Sopenharmony_ci   int i, j;
83bf215546Sopenharmony_ci   int numxpixels, numypixels;
84bf215546Sopenharmony_ci   const GLubyte *srcaddr;
85bf215546Sopenharmony_ci   GLubyte srcpixels[4][4];
86bf215546Sopenharmony_ci   GLubyte *blkaddr;
87bf215546Sopenharmony_ci   GLint dstRowDiff, redRowStride;
88bf215546Sopenharmony_ci   GLubyte *tempImageSlices[1];
89bf215546Sopenharmony_ci
90bf215546Sopenharmony_ci   assert(dstFormat == MESA_FORMAT_R_RGTC1_UNORM ||
91bf215546Sopenharmony_ci          dstFormat == MESA_FORMAT_L_LATC1_UNORM);
92bf215546Sopenharmony_ci
93bf215546Sopenharmony_ci   tempImage = malloc(srcWidth * srcHeight * 1 * sizeof(GLubyte));
94bf215546Sopenharmony_ci   if (!tempImage)
95bf215546Sopenharmony_ci      return GL_FALSE; /* out of memory */
96bf215546Sopenharmony_ci   redRowStride = 1 * srcWidth * sizeof(GLubyte);
97bf215546Sopenharmony_ci   tempImageSlices[0] = (GLubyte *) tempImage;
98bf215546Sopenharmony_ci   _mesa_texstore(ctx, dims,
99bf215546Sopenharmony_ci                  baseInternalFormat,
100bf215546Sopenharmony_ci                  MESA_FORMAT_R_UNORM8,
101bf215546Sopenharmony_ci                  redRowStride, tempImageSlices,
102bf215546Sopenharmony_ci                  srcWidth, srcHeight, srcDepth,
103bf215546Sopenharmony_ci                  srcFormat, srcType, srcAddr,
104bf215546Sopenharmony_ci                  srcPacking);
105bf215546Sopenharmony_ci
106bf215546Sopenharmony_ci   dst = dstSlices[0];
107bf215546Sopenharmony_ci
108bf215546Sopenharmony_ci   blkaddr = dst;
109bf215546Sopenharmony_ci   dstRowDiff = dstRowStride >= (srcWidth * 2) ? dstRowStride - (((srcWidth + 3) & ~3) * 2) : 0;
110bf215546Sopenharmony_ci   for (j = 0; j < srcHeight; j+=4) {
111bf215546Sopenharmony_ci      if (srcHeight > j + 3) numypixels = 4;
112bf215546Sopenharmony_ci      else numypixels = srcHeight - j;
113bf215546Sopenharmony_ci      srcaddr = tempImage + j * srcWidth;
114bf215546Sopenharmony_ci      for (i = 0; i < srcWidth; i += 4) {
115bf215546Sopenharmony_ci	 if (srcWidth > i + 3) numxpixels = 4;
116bf215546Sopenharmony_ci	 else numxpixels = srcWidth - i;
117bf215546Sopenharmony_ci	 extractsrc_u(srcpixels, srcaddr, srcWidth, numxpixels, numypixels, 1);
118bf215546Sopenharmony_ci	 util_format_unsigned_encode_rgtc_ubyte(blkaddr, srcpixels, numxpixels, numypixels);
119bf215546Sopenharmony_ci	 srcaddr += numxpixels;
120bf215546Sopenharmony_ci	 blkaddr += 8;
121bf215546Sopenharmony_ci      }
122bf215546Sopenharmony_ci      blkaddr += dstRowDiff;
123bf215546Sopenharmony_ci   }
124bf215546Sopenharmony_ci
125bf215546Sopenharmony_ci   free((void *) tempImage);
126bf215546Sopenharmony_ci
127bf215546Sopenharmony_ci   return GL_TRUE;
128bf215546Sopenharmony_ci}
129bf215546Sopenharmony_ci
130bf215546Sopenharmony_ciGLboolean
131bf215546Sopenharmony_ci_mesa_texstore_signed_red_rgtc1(TEXSTORE_PARAMS)
132bf215546Sopenharmony_ci{
133bf215546Sopenharmony_ci   GLbyte *dst;
134bf215546Sopenharmony_ci   const GLfloat *tempImage = NULL;
135bf215546Sopenharmony_ci   int i, j;
136bf215546Sopenharmony_ci   int numxpixels, numypixels;
137bf215546Sopenharmony_ci   const GLfloat *srcaddr;
138bf215546Sopenharmony_ci   GLbyte srcpixels[4][4];
139bf215546Sopenharmony_ci   GLbyte *blkaddr;
140bf215546Sopenharmony_ci   GLint dstRowDiff, redRowStride;
141bf215546Sopenharmony_ci   GLfloat *tempImageSlices[1];
142bf215546Sopenharmony_ci
143bf215546Sopenharmony_ci   assert(dstFormat == MESA_FORMAT_R_RGTC1_SNORM ||
144bf215546Sopenharmony_ci          dstFormat == MESA_FORMAT_L_LATC1_SNORM);
145bf215546Sopenharmony_ci
146bf215546Sopenharmony_ci   redRowStride = 1 * srcWidth * sizeof(GLfloat);
147bf215546Sopenharmony_ci   tempImage = malloc(srcWidth * srcHeight * 1 * sizeof(GLfloat));
148bf215546Sopenharmony_ci   if (!tempImage)
149bf215546Sopenharmony_ci      return GL_FALSE; /* out of memory */
150bf215546Sopenharmony_ci   tempImageSlices[0] = (GLfloat *) tempImage;
151bf215546Sopenharmony_ci   _mesa_texstore(ctx, dims,
152bf215546Sopenharmony_ci                  baseInternalFormat,
153bf215546Sopenharmony_ci                  MESA_FORMAT_R_FLOAT32,
154bf215546Sopenharmony_ci                  redRowStride, (GLubyte **)tempImageSlices,
155bf215546Sopenharmony_ci                  srcWidth, srcHeight, srcDepth,
156bf215546Sopenharmony_ci                  srcFormat, srcType, srcAddr,
157bf215546Sopenharmony_ci                  srcPacking);
158bf215546Sopenharmony_ci
159bf215546Sopenharmony_ci   dst = (GLbyte *) dstSlices[0];
160bf215546Sopenharmony_ci
161bf215546Sopenharmony_ci   blkaddr = dst;
162bf215546Sopenharmony_ci   dstRowDiff = dstRowStride >= (srcWidth * 2) ? dstRowStride - (((srcWidth + 3) & ~3) * 2) : 0;
163bf215546Sopenharmony_ci   for (j = 0; j < srcHeight; j+=4) {
164bf215546Sopenharmony_ci      if (srcHeight > j + 3) numypixels = 4;
165bf215546Sopenharmony_ci      else numypixels = srcHeight - j;
166bf215546Sopenharmony_ci      srcaddr = tempImage + j * srcWidth;
167bf215546Sopenharmony_ci      for (i = 0; i < srcWidth; i += 4) {
168bf215546Sopenharmony_ci	 if (srcWidth > i + 3) numxpixels = 4;
169bf215546Sopenharmony_ci	 else numxpixels = srcWidth - i;
170bf215546Sopenharmony_ci	 extractsrc_s(srcpixels, srcaddr, srcWidth, numxpixels, numypixels, 1);
171bf215546Sopenharmony_ci	 util_format_signed_encode_rgtc_ubyte(blkaddr, srcpixels, numxpixels, numypixels);
172bf215546Sopenharmony_ci	 srcaddr += numxpixels;
173bf215546Sopenharmony_ci	 blkaddr += 8;
174bf215546Sopenharmony_ci      }
175bf215546Sopenharmony_ci      blkaddr += dstRowDiff;
176bf215546Sopenharmony_ci   }
177bf215546Sopenharmony_ci
178bf215546Sopenharmony_ci   free((void *) tempImage);
179bf215546Sopenharmony_ci
180bf215546Sopenharmony_ci   return GL_TRUE;
181bf215546Sopenharmony_ci}
182bf215546Sopenharmony_ci
183bf215546Sopenharmony_ciGLboolean
184bf215546Sopenharmony_ci_mesa_texstore_rg_rgtc2(TEXSTORE_PARAMS)
185bf215546Sopenharmony_ci{
186bf215546Sopenharmony_ci   GLubyte *dst;
187bf215546Sopenharmony_ci   const GLubyte *tempImage = NULL;
188bf215546Sopenharmony_ci   int i, j;
189bf215546Sopenharmony_ci   int numxpixels, numypixels;
190bf215546Sopenharmony_ci   const GLubyte *srcaddr;
191bf215546Sopenharmony_ci   GLubyte srcpixels[4][4];
192bf215546Sopenharmony_ci   GLubyte *blkaddr;
193bf215546Sopenharmony_ci   GLint dstRowDiff, rgRowStride;
194bf215546Sopenharmony_ci   mesa_format tempFormat;
195bf215546Sopenharmony_ci   GLubyte *tempImageSlices[1];
196bf215546Sopenharmony_ci
197bf215546Sopenharmony_ci   assert(dstFormat == MESA_FORMAT_RG_RGTC2_UNORM ||
198bf215546Sopenharmony_ci          dstFormat == MESA_FORMAT_LA_LATC2_UNORM);
199bf215546Sopenharmony_ci
200bf215546Sopenharmony_ci   if (baseInternalFormat == GL_RG)
201bf215546Sopenharmony_ci      tempFormat = MESA_FORMAT_RG_UNORM8;
202bf215546Sopenharmony_ci   else
203bf215546Sopenharmony_ci      tempFormat = MESA_FORMAT_LA_UNORM8;
204bf215546Sopenharmony_ci
205bf215546Sopenharmony_ci   rgRowStride = 2 * srcWidth * sizeof(GLubyte);
206bf215546Sopenharmony_ci   tempImage = malloc(srcWidth * srcHeight * 2 * sizeof(GLubyte));
207bf215546Sopenharmony_ci   if (!tempImage)
208bf215546Sopenharmony_ci      return GL_FALSE; /* out of memory */
209bf215546Sopenharmony_ci   tempImageSlices[0] = (GLubyte *) tempImage;
210bf215546Sopenharmony_ci   _mesa_texstore(ctx, dims,
211bf215546Sopenharmony_ci                  baseInternalFormat,
212bf215546Sopenharmony_ci                  tempFormat,
213bf215546Sopenharmony_ci                  rgRowStride, tempImageSlices,
214bf215546Sopenharmony_ci                  srcWidth, srcHeight, srcDepth,
215bf215546Sopenharmony_ci                  srcFormat, srcType, srcAddr,
216bf215546Sopenharmony_ci                  srcPacking);
217bf215546Sopenharmony_ci
218bf215546Sopenharmony_ci   dst = dstSlices[0];
219bf215546Sopenharmony_ci
220bf215546Sopenharmony_ci   blkaddr = dst;
221bf215546Sopenharmony_ci   dstRowDiff = dstRowStride >= (srcWidth * 4) ? dstRowStride - (((srcWidth + 3) & ~3) * 4) : 0;
222bf215546Sopenharmony_ci   for (j = 0; j < srcHeight; j+=4) {
223bf215546Sopenharmony_ci      if (srcHeight > j + 3) numypixels = 4;
224bf215546Sopenharmony_ci      else numypixels = srcHeight - j;
225bf215546Sopenharmony_ci      srcaddr = tempImage + j * srcWidth * 2;
226bf215546Sopenharmony_ci      for (i = 0; i < srcWidth; i += 4) {
227bf215546Sopenharmony_ci	 if (srcWidth > i + 3) numxpixels = 4;
228bf215546Sopenharmony_ci	 else numxpixels = srcWidth - i;
229bf215546Sopenharmony_ci	 extractsrc_u(srcpixels, srcaddr, srcWidth, numxpixels, numypixels, 2);
230bf215546Sopenharmony_ci	 util_format_unsigned_encode_rgtc_ubyte(blkaddr, srcpixels, numxpixels, numypixels);
231bf215546Sopenharmony_ci
232bf215546Sopenharmony_ci	 blkaddr += 8;
233bf215546Sopenharmony_ci	 extractsrc_u(srcpixels, (GLubyte *)srcaddr + 1, srcWidth, numxpixels, numypixels, 2);
234bf215546Sopenharmony_ci	 util_format_unsigned_encode_rgtc_ubyte(blkaddr, srcpixels, numxpixels, numypixels);
235bf215546Sopenharmony_ci
236bf215546Sopenharmony_ci	 blkaddr += 8;
237bf215546Sopenharmony_ci
238bf215546Sopenharmony_ci	 srcaddr += numxpixels * 2;
239bf215546Sopenharmony_ci      }
240bf215546Sopenharmony_ci      blkaddr += dstRowDiff;
241bf215546Sopenharmony_ci   }
242bf215546Sopenharmony_ci
243bf215546Sopenharmony_ci   free((void *) tempImage);
244bf215546Sopenharmony_ci
245bf215546Sopenharmony_ci   return GL_TRUE;
246bf215546Sopenharmony_ci}
247bf215546Sopenharmony_ci
248bf215546Sopenharmony_ciGLboolean
249bf215546Sopenharmony_ci_mesa_texstore_signed_rg_rgtc2(TEXSTORE_PARAMS)
250bf215546Sopenharmony_ci{
251bf215546Sopenharmony_ci   GLbyte *dst;
252bf215546Sopenharmony_ci   const GLfloat *tempImage = NULL;
253bf215546Sopenharmony_ci   int i, j;
254bf215546Sopenharmony_ci   int numxpixels, numypixels;
255bf215546Sopenharmony_ci   const GLfloat *srcaddr;
256bf215546Sopenharmony_ci   GLbyte srcpixels[4][4];
257bf215546Sopenharmony_ci   GLbyte *blkaddr;
258bf215546Sopenharmony_ci   GLint dstRowDiff, rgRowStride;
259bf215546Sopenharmony_ci   mesa_format tempFormat;
260bf215546Sopenharmony_ci   GLfloat *tempImageSlices[1];
261bf215546Sopenharmony_ci
262bf215546Sopenharmony_ci   assert(dstFormat == MESA_FORMAT_RG_RGTC2_SNORM ||
263bf215546Sopenharmony_ci          dstFormat == MESA_FORMAT_LA_LATC2_SNORM);
264bf215546Sopenharmony_ci
265bf215546Sopenharmony_ci   if (baseInternalFormat == GL_RG)
266bf215546Sopenharmony_ci      tempFormat = MESA_FORMAT_RG_FLOAT32;
267bf215546Sopenharmony_ci   else
268bf215546Sopenharmony_ci      tempFormat = MESA_FORMAT_LA_FLOAT32;
269bf215546Sopenharmony_ci
270bf215546Sopenharmony_ci   rgRowStride = 2 * srcWidth * sizeof(GLfloat);
271bf215546Sopenharmony_ci   tempImage = malloc(srcWidth * srcHeight * 2 * sizeof(GLfloat));
272bf215546Sopenharmony_ci   if (!tempImage)
273bf215546Sopenharmony_ci      return GL_FALSE; /* out of memory */
274bf215546Sopenharmony_ci   tempImageSlices[0] = (GLfloat *) tempImage;
275bf215546Sopenharmony_ci   _mesa_texstore(ctx, dims,
276bf215546Sopenharmony_ci                  baseInternalFormat,
277bf215546Sopenharmony_ci                  tempFormat,
278bf215546Sopenharmony_ci                  rgRowStride, (GLubyte **)tempImageSlices,
279bf215546Sopenharmony_ci                  srcWidth, srcHeight, srcDepth,
280bf215546Sopenharmony_ci                  srcFormat, srcType, srcAddr,
281bf215546Sopenharmony_ci                  srcPacking);
282bf215546Sopenharmony_ci
283bf215546Sopenharmony_ci   dst = (GLbyte *) dstSlices[0];
284bf215546Sopenharmony_ci
285bf215546Sopenharmony_ci   blkaddr = dst;
286bf215546Sopenharmony_ci   dstRowDiff = dstRowStride >= (srcWidth * 4) ? dstRowStride - (((srcWidth + 3) & ~3) * 4) : 0;
287bf215546Sopenharmony_ci   for (j = 0; j < srcHeight; j += 4) {
288bf215546Sopenharmony_ci      if (srcHeight > j + 3) numypixels = 4;
289bf215546Sopenharmony_ci      else numypixels = srcHeight - j;
290bf215546Sopenharmony_ci      srcaddr = tempImage + j * srcWidth * 2;
291bf215546Sopenharmony_ci      for (i = 0; i < srcWidth; i += 4) {
292bf215546Sopenharmony_ci	 if (srcWidth > i + 3) numxpixels = 4;
293bf215546Sopenharmony_ci	 else numxpixels = srcWidth - i;
294bf215546Sopenharmony_ci
295bf215546Sopenharmony_ci	 extractsrc_s(srcpixels, srcaddr, srcWidth, numxpixels, numypixels, 2);
296bf215546Sopenharmony_ci	 util_format_signed_encode_rgtc_ubyte(blkaddr, srcpixels, numxpixels, numypixels);
297bf215546Sopenharmony_ci	 blkaddr += 8;
298bf215546Sopenharmony_ci
299bf215546Sopenharmony_ci	 extractsrc_s(srcpixels, srcaddr + 1, srcWidth, numxpixels, numypixels, 2);
300bf215546Sopenharmony_ci	 util_format_signed_encode_rgtc_ubyte(blkaddr, srcpixels, numxpixels, numypixels);
301bf215546Sopenharmony_ci	 blkaddr += 8;
302bf215546Sopenharmony_ci
303bf215546Sopenharmony_ci	 srcaddr += numxpixels * 2;
304bf215546Sopenharmony_ci
305bf215546Sopenharmony_ci      }
306bf215546Sopenharmony_ci      blkaddr += dstRowDiff;
307bf215546Sopenharmony_ci   }
308bf215546Sopenharmony_ci
309bf215546Sopenharmony_ci   free((void *) tempImage);
310bf215546Sopenharmony_ci
311bf215546Sopenharmony_ci   return GL_TRUE;
312bf215546Sopenharmony_ci}
313bf215546Sopenharmony_ci
314bf215546Sopenharmony_cistatic void
315bf215546Sopenharmony_cifetch_red_rgtc1(const GLubyte *map,
316bf215546Sopenharmony_ci                GLint rowStride, GLint i, GLint j, GLfloat *texel)
317bf215546Sopenharmony_ci{
318bf215546Sopenharmony_ci   GLubyte red;
319bf215546Sopenharmony_ci   util_format_unsigned_fetch_texel_rgtc(rowStride, map, i, j, &red, 1);
320bf215546Sopenharmony_ci   texel[RCOMP] = UBYTE_TO_FLOAT(red);
321bf215546Sopenharmony_ci   texel[GCOMP] = 0.0;
322bf215546Sopenharmony_ci   texel[BCOMP] = 0.0;
323bf215546Sopenharmony_ci   texel[ACOMP] = 1.0;
324bf215546Sopenharmony_ci}
325bf215546Sopenharmony_ci
326bf215546Sopenharmony_cistatic void
327bf215546Sopenharmony_cifetch_l_latc1(const GLubyte *map,
328bf215546Sopenharmony_ci              GLint rowStride, GLint i, GLint j, GLfloat *texel)
329bf215546Sopenharmony_ci{
330bf215546Sopenharmony_ci   GLubyte red;
331bf215546Sopenharmony_ci   util_format_unsigned_fetch_texel_rgtc(rowStride, map, i, j, &red, 1);
332bf215546Sopenharmony_ci   texel[RCOMP] =
333bf215546Sopenharmony_ci   texel[GCOMP] =
334bf215546Sopenharmony_ci   texel[BCOMP] = UBYTE_TO_FLOAT(red);
335bf215546Sopenharmony_ci   texel[ACOMP] = 1.0;
336bf215546Sopenharmony_ci}
337bf215546Sopenharmony_ci
338bf215546Sopenharmony_cistatic void
339bf215546Sopenharmony_cifetch_signed_red_rgtc1(const GLubyte *map,
340bf215546Sopenharmony_ci                       GLint rowStride, GLint i, GLint j, GLfloat *texel)
341bf215546Sopenharmony_ci{
342bf215546Sopenharmony_ci   GLbyte red;
343bf215546Sopenharmony_ci   util_format_signed_fetch_texel_rgtc(rowStride, (const GLbyte *) map,
344bf215546Sopenharmony_ci                           i, j, &red, 1);
345bf215546Sopenharmony_ci   texel[RCOMP] = BYTE_TO_FLOAT_TEX(red);
346bf215546Sopenharmony_ci   texel[GCOMP] = 0.0;
347bf215546Sopenharmony_ci   texel[BCOMP] = 0.0;
348bf215546Sopenharmony_ci   texel[ACOMP] = 1.0;
349bf215546Sopenharmony_ci}
350bf215546Sopenharmony_ci
351bf215546Sopenharmony_cistatic void
352bf215546Sopenharmony_cifetch_signed_l_latc1(const GLubyte *map,
353bf215546Sopenharmony_ci                     GLint rowStride, GLint i, GLint j, GLfloat *texel)
354bf215546Sopenharmony_ci{
355bf215546Sopenharmony_ci   GLbyte red;
356bf215546Sopenharmony_ci   util_format_signed_fetch_texel_rgtc(rowStride, (GLbyte *) map,
357bf215546Sopenharmony_ci                           i, j, &red, 1);
358bf215546Sopenharmony_ci   texel[RCOMP] =
359bf215546Sopenharmony_ci   texel[GCOMP] =
360bf215546Sopenharmony_ci   texel[BCOMP] = BYTE_TO_FLOAT(red);
361bf215546Sopenharmony_ci   texel[ACOMP] = 1.0;
362bf215546Sopenharmony_ci}
363bf215546Sopenharmony_ci
364bf215546Sopenharmony_cistatic void
365bf215546Sopenharmony_cifetch_rg_rgtc2(const GLubyte *map,
366bf215546Sopenharmony_ci               GLint rowStride, GLint i, GLint j, GLfloat *texel)
367bf215546Sopenharmony_ci{
368bf215546Sopenharmony_ci   GLubyte red, green;
369bf215546Sopenharmony_ci   util_format_unsigned_fetch_texel_rgtc(rowStride,
370bf215546Sopenharmony_ci                             map,
371bf215546Sopenharmony_ci                             i, j, &red, 2);
372bf215546Sopenharmony_ci   util_format_unsigned_fetch_texel_rgtc(rowStride,
373bf215546Sopenharmony_ci                             map + 8,
374bf215546Sopenharmony_ci                             i, j, &green, 2);
375bf215546Sopenharmony_ci   texel[RCOMP] = UBYTE_TO_FLOAT(red);
376bf215546Sopenharmony_ci   texel[GCOMP] = UBYTE_TO_FLOAT(green);
377bf215546Sopenharmony_ci   texel[BCOMP] = 0.0;
378bf215546Sopenharmony_ci   texel[ACOMP] = 1.0;
379bf215546Sopenharmony_ci}
380bf215546Sopenharmony_ci
381bf215546Sopenharmony_cistatic void
382bf215546Sopenharmony_cifetch_la_latc2(const GLubyte *map,
383bf215546Sopenharmony_ci               GLint rowStride, GLint i, GLint j, GLfloat *texel)
384bf215546Sopenharmony_ci{
385bf215546Sopenharmony_ci   GLubyte red, green;
386bf215546Sopenharmony_ci   util_format_unsigned_fetch_texel_rgtc(rowStride,
387bf215546Sopenharmony_ci                             map,
388bf215546Sopenharmony_ci                             i, j, &red, 2);
389bf215546Sopenharmony_ci   util_format_unsigned_fetch_texel_rgtc(rowStride,
390bf215546Sopenharmony_ci                             map + 8,
391bf215546Sopenharmony_ci                             i, j, &green, 2);
392bf215546Sopenharmony_ci   texel[RCOMP] =
393bf215546Sopenharmony_ci   texel[GCOMP] =
394bf215546Sopenharmony_ci   texel[BCOMP] = UBYTE_TO_FLOAT(red);
395bf215546Sopenharmony_ci   texel[ACOMP] = UBYTE_TO_FLOAT(green);
396bf215546Sopenharmony_ci}
397bf215546Sopenharmony_ci
398bf215546Sopenharmony_ci
399bf215546Sopenharmony_cistatic void
400bf215546Sopenharmony_cifetch_signed_rg_rgtc2(const GLubyte *map,
401bf215546Sopenharmony_ci                      GLint rowStride, GLint i, GLint j, GLfloat *texel)
402bf215546Sopenharmony_ci{
403bf215546Sopenharmony_ci   GLbyte red, green;
404bf215546Sopenharmony_ci   util_format_signed_fetch_texel_rgtc(rowStride,
405bf215546Sopenharmony_ci                           (GLbyte *) map,
406bf215546Sopenharmony_ci                           i, j, &red, 2);
407bf215546Sopenharmony_ci   util_format_signed_fetch_texel_rgtc(rowStride,
408bf215546Sopenharmony_ci                           (GLbyte *) map + 8,
409bf215546Sopenharmony_ci                           i, j, &green, 2);
410bf215546Sopenharmony_ci   texel[RCOMP] = BYTE_TO_FLOAT_TEX(red);
411bf215546Sopenharmony_ci   texel[GCOMP] = BYTE_TO_FLOAT_TEX(green);
412bf215546Sopenharmony_ci   texel[BCOMP] = 0.0;
413bf215546Sopenharmony_ci   texel[ACOMP] = 1.0;
414bf215546Sopenharmony_ci}
415bf215546Sopenharmony_ci
416bf215546Sopenharmony_ci
417bf215546Sopenharmony_cistatic void
418bf215546Sopenharmony_cifetch_signed_la_latc2(const GLubyte *map,
419bf215546Sopenharmony_ci                      GLint rowStride, GLint i, GLint j, GLfloat *texel)
420bf215546Sopenharmony_ci{
421bf215546Sopenharmony_ci   GLbyte red, green;
422bf215546Sopenharmony_ci   util_format_signed_fetch_texel_rgtc(rowStride,
423bf215546Sopenharmony_ci                           (GLbyte *) map,
424bf215546Sopenharmony_ci                           i, j, &red, 2);
425bf215546Sopenharmony_ci   util_format_signed_fetch_texel_rgtc(rowStride,
426bf215546Sopenharmony_ci                           (GLbyte *) map + 8,
427bf215546Sopenharmony_ci                           i, j, &green, 2);
428bf215546Sopenharmony_ci   texel[RCOMP] =
429bf215546Sopenharmony_ci   texel[GCOMP] =
430bf215546Sopenharmony_ci   texel[BCOMP] = BYTE_TO_FLOAT_TEX(red);
431bf215546Sopenharmony_ci   texel[ACOMP] = BYTE_TO_FLOAT_TEX(green);
432bf215546Sopenharmony_ci}
433bf215546Sopenharmony_ci
434bf215546Sopenharmony_ci
435bf215546Sopenharmony_cicompressed_fetch_func
436bf215546Sopenharmony_ci_mesa_get_compressed_rgtc_func(mesa_format format)
437bf215546Sopenharmony_ci{
438bf215546Sopenharmony_ci   switch (format) {
439bf215546Sopenharmony_ci   case MESA_FORMAT_R_RGTC1_UNORM:
440bf215546Sopenharmony_ci      return fetch_red_rgtc1;
441bf215546Sopenharmony_ci   case MESA_FORMAT_L_LATC1_UNORM:
442bf215546Sopenharmony_ci      return fetch_l_latc1;
443bf215546Sopenharmony_ci   case MESA_FORMAT_R_RGTC1_SNORM:
444bf215546Sopenharmony_ci      return fetch_signed_red_rgtc1;
445bf215546Sopenharmony_ci   case MESA_FORMAT_L_LATC1_SNORM:
446bf215546Sopenharmony_ci      return fetch_signed_l_latc1;
447bf215546Sopenharmony_ci   case MESA_FORMAT_RG_RGTC2_UNORM:
448bf215546Sopenharmony_ci      return fetch_rg_rgtc2;
449bf215546Sopenharmony_ci   case MESA_FORMAT_LA_LATC2_UNORM:
450bf215546Sopenharmony_ci      return fetch_la_latc2;
451bf215546Sopenharmony_ci   case MESA_FORMAT_RG_RGTC2_SNORM:
452bf215546Sopenharmony_ci      return fetch_signed_rg_rgtc2;
453bf215546Sopenharmony_ci   case MESA_FORMAT_LA_LATC2_SNORM:
454bf215546Sopenharmony_ci      return fetch_signed_la_latc2;
455bf215546Sopenharmony_ci   default:
456bf215546Sopenharmony_ci      return NULL;
457bf215546Sopenharmony_ci   }
458bf215546Sopenharmony_ci}
459