summaryrefslogtreecommitdiffstats
path: root/src/mesa/main/texcompress_bptc.c
blob: 46279f144ff6d526334bb79c0828926c1c515bfc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/*
 * Copyright (C) 2014 Intel Corporation
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 */

/**
 * \file texcompress_bptc.c
 * GL_ARB_texture_compression_bptc support.
 */

#include <stdbool.h>
#include "texcompress.h"
#include "texcompress_bptc.h"
#include "texcompress_bptc_tmp.h"
#include "texstore.h"
#include "image.h"
#include "mtypes.h"

static void
fetch_bptc_rgb_float(const GLubyte *map,
                     GLint rowStride, GLint i, GLint j,
                     GLfloat *texel,
                     bool is_signed)
{
   const GLubyte *block;

   block = map + (((rowStride + 3) / 4) * (j / 4) + (i / 4)) * 16;

   fetch_rgb_float_from_block(block, texel, (i % 4) + (j % 4) * 4, is_signed);
}

static void
fetch_bptc_rgb_signed_float(const GLubyte *map,
                            GLint rowStride, GLint i, GLint j,
                            GLfloat *texel)
{
   fetch_bptc_rgb_float(map, rowStride, i, j, texel, true);
}

static void
fetch_bptc_rgb_unsigned_float(const GLubyte *map,
                              GLint rowStride, GLint i, GLint j,
                              GLfloat *texel)
{
   fetch_bptc_rgb_float(map, rowStride, i, j, texel, false);
}

static void
fetch_bptc_rgba_unorm_bytes(const GLubyte *map,
                            GLint rowStride, GLint i, GLint j,
                            GLubyte *texel)
{
   const GLubyte *block;

   block = map + (((rowStride + 3) / 4) * (j / 4) + (i / 4)) * 16;

   fetch_rgba_unorm_from_block(block, texel, (i % 4) + (j % 4) * 4);
}

static void
fetch_bptc_rgba_unorm(const GLubyte *map,
                      GLint rowStride, GLint i, GLint j,
                      GLfloat *texel)
{
   GLubyte texel_bytes[4];

   fetch_bptc_rgba_unorm_bytes(map, rowStride, i, j, texel_bytes);

   texel[RCOMP] = UBYTE_TO_FLOAT(texel_bytes[0]);
   texel[GCOMP] = UBYTE_TO_FLOAT(texel_bytes[1]);
   texel[BCOMP] = UBYTE_TO_FLOAT(texel_bytes[2]);
   texel[ACOMP] = UBYTE_TO_FLOAT(texel_bytes[3]);
}

static void
fetch_bptc_srgb_alpha_unorm(const GLubyte *map,
                            GLint rowStride, GLint i, GLint j,
                            GLfloat *texel)
{
   GLubyte texel_bytes[4];

   fetch_bptc_rgba_unorm_bytes(map, rowStride, i, j, texel_bytes);

   texel[RCOMP] = util_format_srgb_8unorm_to_linear_float(texel_bytes[0]);
   texel[GCOMP] = util_format_srgb_8unorm_to_linear_float(texel_bytes[1]);
   texel[BCOMP] = util_format_srgb_8unorm_to_linear_float(texel_bytes[2]);
   texel[ACOMP] = UBYTE_TO_FLOAT(texel_bytes[3]);
}

compressed_fetch_func
_mesa_get_bptc_fetch_func(mesa_format format)
{
   switch (format) {
   case MESA_FORMAT_BPTC_RGBA_UNORM:
      return fetch_bptc_rgba_unorm;
   case MESA_FORMAT_BPTC_SRGB_ALPHA_UNORM:
      return fetch_bptc_srgb_alpha_unorm;
   case MESA_FORMAT_BPTC_RGB_SIGNED_FLOAT:
      return fetch_bptc_rgb_signed_float;
   case MESA_FORMAT_BPTC_RGB_UNSIGNED_FLOAT:
      return fetch_bptc_rgb_unsigned_float;
   default:
      return NULL;
   }
}

GLboolean
_mesa_texstore_bptc_rgba_unorm(TEXSTORE_PARAMS)
{
   const GLubyte *pixels;
   const GLubyte *tempImage = NULL;
   int rowstride;

   if (srcFormat != GL_RGBA ||
       srcType != GL_UNSIGNED_BYTE ||
       ctx->_ImageTransferState ||
       srcPacking->SwapBytes) {
      /* convert image to RGBA/ubyte */
      GLubyte *tempImageSlices[1];
      int rgbaRowStride = 4 * srcWidth * sizeof(GLubyte);
      tempImage = malloc(srcWidth * srcHeight * 4 * sizeof(GLubyte));
      if (!tempImage)
         return GL_FALSE; /* out of memory */
      tempImageSlices[0] = (GLubyte *) tempImage;
      _mesa_texstore(ctx, dims,
                     baseInternalFormat,
                     _mesa_little_endian() ? MESA_FORMAT_R8G8B8A8_UNORM
                                           : MESA_FORMAT_A8B8G8R8_UNORM,
                     rgbaRowStride, tempImageSlices,
                     srcWidth, srcHeight, srcDepth,
                     srcFormat, srcType, srcAddr,
                     srcPacking);

      pixels = tempImage;
      rowstride = srcWidth * 4;
   } else {
      pixels = _mesa_image_address2d(srcPacking, srcAddr, srcWidth, srcHeight,
                                     srcFormat, srcType, 0, 0);
      rowstride = _mesa_image_row_stride(srcPacking, srcWidth,
                                         srcFormat, srcType);
   }

   compress_rgba_unorm(srcWidth, srcHeight,
                       pixels, rowstride,
                       dstSlices[0], dstRowStride);

   free((void *) tempImage);

   return GL_TRUE;
}

static GLboolean
texstore_bptc_rgb_float(TEXSTORE_PARAMS,
                        bool is_signed)
{
   const float *pixels;
   const float *tempImage = NULL;
   int rowstride;

   if (srcFormat != GL_RGB ||
       srcType != GL_FLOAT ||
       ctx->_ImageTransferState ||
       srcPacking->SwapBytes) {
      /* convert image to RGB/float */
      GLfloat *tempImageSlices[1];
      int rgbRowStride = 3 * srcWidth * sizeof(GLfloat);
      tempImage = malloc(srcWidth * srcHeight * 3 * sizeof(GLfloat));
      if (!tempImage)
         return GL_FALSE; /* out of memory */
      tempImageSlices[0] = (GLfloat *) tempImage;
      _mesa_texstore(ctx, dims,
                     baseInternalFormat,
                     MESA_FORMAT_RGB_FLOAT32,
                     rgbRowStride, (GLubyte **)tempImageSlices,
                     srcWidth, srcHeight, srcDepth,
                     srcFormat, srcType, srcAddr,
                     srcPacking);

      pixels = tempImage;
      rowstride = srcWidth * sizeof(float) * 3;
   } else {
      pixels = _mesa_image_address2d(srcPacking, srcAddr, srcWidth, srcHeight,
                                     srcFormat, srcType, 0, 0);
      rowstride = _mesa_image_row_stride(srcPacking, srcWidth,
                                         srcFormat, srcType);
   }

   compress_rgb_float(srcWidth, srcHeight,
                      pixels, rowstride,
                      dstSlices[0], dstRowStride,
                      is_signed);

   free((void *) tempImage);

   return GL_TRUE;
}

GLboolean
_mesa_texstore_bptc_rgb_signed_float(TEXSTORE_PARAMS)
{
   assert(dstFormat == MESA_FORMAT_BPTC_RGB_SIGNED_FLOAT);

   return texstore_bptc_rgb_float(ctx, dims, baseInternalFormat,
                                  dstFormat, dstRowStride, dstSlices,
                                  srcWidth, srcHeight, srcDepth,
                                  srcFormat, srcType,
                                  srcAddr, srcPacking,
                                  true /* signed */);
}

GLboolean
_mesa_texstore_bptc_rgb_unsigned_float(TEXSTORE_PARAMS)
{
   assert(dstFormat == MESA_FORMAT_BPTC_RGB_UNSIGNED_FLOAT);

   return texstore_bptc_rgb_float(ctx, dims, baseInternalFormat,
                                  dstFormat, dstRowStride, dstSlices,
                                  srcWidth, srcHeight, srcDepth,
                                  srcFormat, srcType,
                                  srcAddr, srcPacking,
                                  false /* unsigned */);
}