aboutsummaryrefslogtreecommitdiffstats
path: root/src/mesa/main/genmipmap.c
blob: dcbe9675070f6e116ea43d92ae2b4e3eb15cb5fb (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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
/*
 * Mesa 3-D graphics library
 *
 * Copyright (C) 1999-2008  Brian Paul   All Rights Reserved.
 * Copyright (C) 1999-2013  VMware, Inc.  All Rights Reserved.
 *
 * 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 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.
 */


/*
 * glGenerateMipmap function
 */

#include "context.h"
#include "enums.h"
#include "genmipmap.h"
#include "glformats.h"
#include "macros.h"
#include "mtypes.h"
#include "teximage.h"
#include "texobj.h"
#include "hash.h"

bool
_mesa_is_valid_generate_texture_mipmap_target(struct gl_context *ctx,
                                              GLenum target)
{
   bool error;

   switch (target) {
   case GL_TEXTURE_1D:
      error = _mesa_is_gles(ctx);
      break;
   case GL_TEXTURE_2D:
      error = false;
      break;
   case GL_TEXTURE_3D:
      error = ctx->API == API_OPENGLES;
      break;
   case GL_TEXTURE_CUBE_MAP:
      error = !ctx->Extensions.ARB_texture_cube_map;
      break;
   case GL_TEXTURE_1D_ARRAY:
      error = _mesa_is_gles(ctx) || !ctx->Extensions.EXT_texture_array;
      break;
   case GL_TEXTURE_2D_ARRAY:
      error = (_mesa_is_gles(ctx) && ctx->Version < 30)
         || !ctx->Extensions.EXT_texture_array;
      break;
   case GL_TEXTURE_CUBE_MAP_ARRAY:
      error = !_mesa_has_texture_cube_map_array(ctx);
      break;
   default:
      error = true;
   }

   return !error;
}

bool
_mesa_is_valid_generate_texture_mipmap_internalformat(struct gl_context *ctx,
                                                      GLenum internalformat)
{
   if (_mesa_is_gles3(ctx)) {
      /* From the ES 3.2 specification's description of GenerateMipmap():
       * "An INVALID_OPERATION error is generated if the levelbase array was
       *  not specified with an unsized internal format from table 8.3 or a
       *  sized internal format that is both color-renderable and
       *  texture-filterable according to table 8.10."
       *
       * GL_EXT_texture_format_BGRA8888 adds a GL_BGRA_EXT unsized internal
       * format, and includes it in a very similar looking table.  So we
       * include it here as well.
       */
      return internalformat == GL_RGBA || internalformat == GL_RGB ||
             internalformat == GL_LUMINANCE_ALPHA ||
             internalformat == GL_LUMINANCE || internalformat == GL_ALPHA ||
             internalformat == GL_BGRA_EXT ||
             (_mesa_is_es3_color_renderable(ctx, internalformat) &&
              _mesa_is_es3_texture_filterable(ctx, internalformat));
   }

   return (!_mesa_is_enum_format_integer(internalformat) &&
           !_mesa_is_depthstencil_format(internalformat) &&
           !_mesa_is_astc_format(internalformat) &&
           !_mesa_is_stencil_format(internalformat));
}

/**
 * Implements glGenerateMipmap and glGenerateTextureMipmap.
 * Generates all the mipmap levels below the base level.
 * Error-checking is done only if caller is not NULL.
 */
static ALWAYS_INLINE void
generate_texture_mipmap(struct gl_context *ctx,
                        struct gl_texture_object *texObj, GLenum target,
                        const char* caller)
{
   struct gl_texture_image *srcImage;

   FLUSH_VERTICES(ctx, 0);

   if (texObj->BaseLevel >= texObj->MaxLevel) {
      /* nothing to do */
      return;
   }

   if (caller && texObj->Target == GL_TEXTURE_CUBE_MAP &&
       !_mesa_cube_complete(texObj)) {
      _mesa_error(ctx, GL_INVALID_OPERATION,
                  "%s(incomplete cube map)", caller);
      return;
   }

   _mesa_lock_texture(ctx, texObj);

   srcImage = _mesa_select_tex_image(texObj, target, texObj->BaseLevel);
   if (caller) {
      if (!srcImage) {
         _mesa_unlock_texture(ctx, texObj);
         _mesa_error(ctx, GL_INVALID_OPERATION,
                     "%s(zero size base image)", caller);
         return;
      }

      if (!_mesa_is_valid_generate_texture_mipmap_internalformat(ctx,
                                                                 srcImage->InternalFormat)) {
         _mesa_unlock_texture(ctx, texObj);
         _mesa_error(ctx, GL_INVALID_OPERATION,
                     "%s(invalid internal format %s)", caller,
                     _mesa_enum_to_string(srcImage->InternalFormat));
         return;
      }
   }

   if (srcImage->Width == 0 || srcImage->Height == 0) {
      _mesa_unlock_texture(ctx, texObj);
      return;
   }

   if (target == GL_TEXTURE_CUBE_MAP) {
      GLuint face;
      for (face = 0; face < 6; face++) {
         ctx->Driver.GenerateMipmap(ctx,
                      GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, texObj);
      }
   }
   else {
      ctx->Driver.GenerateMipmap(ctx, target, texObj);
   }
   _mesa_unlock_texture(ctx, texObj);
}

/**
 * Generate all the mipmap levels below the base level.
 * Note: this GL function would be more useful if one could specify a
 * cube face, a set of array slices, etc.
 */
void GLAPIENTRY
_mesa_GenerateMipmap_no_error(GLenum target)
{
   GET_CURRENT_CONTEXT(ctx);

   struct gl_texture_object *texObj = _mesa_get_current_tex_object(ctx, target);
   generate_texture_mipmap(ctx, texObj, target, NULL);
}

void GLAPIENTRY
_mesa_GenerateMipmap(GLenum target)
{
   struct gl_texture_object *texObj;
   GET_CURRENT_CONTEXT(ctx);

   if (!_mesa_is_valid_generate_texture_mipmap_target(ctx, target)) {
      _mesa_error(ctx, GL_INVALID_ENUM, "glGenerateMipmap(target=%s)",
                  _mesa_enum_to_string(target));
      return;
   }

   texObj = _mesa_get_current_tex_object(ctx, target);
   if (!texObj)
      return;

   generate_texture_mipmap(ctx, texObj, target, "glGenerateMipmap");
}

/**
 * Generate all the mipmap levels below the base level.
 */
void GLAPIENTRY
_mesa_GenerateTextureMipmap_no_error(GLuint texture)
{
   GET_CURRENT_CONTEXT(ctx);

   struct gl_texture_object *texObj = _mesa_lookup_texture(ctx, texture);
   generate_texture_mipmap(ctx, texObj, texObj->Target, NULL);
}

static void
validate_params_and_generate_mipmap(struct gl_texture_object *texObj, const char* caller)
{
   GET_CURRENT_CONTEXT(ctx);

   if (!texObj)
      return;

   if (!_mesa_is_valid_generate_texture_mipmap_target(ctx, texObj->Target)) {
      _mesa_error(ctx, GL_INVALID_ENUM, "%s(target=%s)",
                  caller,
                  _mesa_enum_to_string(texObj->Target));
      return;
   }

   generate_texture_mipmap(ctx, texObj, texObj->Target, caller);
}

void GLAPIENTRY
_mesa_GenerateTextureMipmap(GLuint texture)
{
   struct gl_texture_object *texObj;
   GET_CURRENT_CONTEXT(ctx);

   texObj = _mesa_lookup_texture_err(ctx, texture, "glGenerateTextureMipmap");
   validate_params_and_generate_mipmap(texObj, "glGenerateTextureMipmap");
}

void GLAPIENTRY
_mesa_GenerateTextureMipmapEXT(GLuint texture, GLenum target)
{
   struct gl_texture_object *texObj;
   GET_CURRENT_CONTEXT(ctx);

   texObj = _mesa_lookup_or_create_texture(ctx, target, texture,
                                           false, true,
                                           "glGenerateTextureMipmapEXT");
   validate_params_and_generate_mipmap(texObj,
                                       "glGenerateTextureMipmapEXT");
}

void GLAPIENTRY
_mesa_GenerateMultiTexMipmapEXT(GLenum texunit, GLenum target)
{
   struct gl_texture_object *texObj;
   GET_CURRENT_CONTEXT(ctx);

   texObj = _mesa_get_texobj_by_target_and_texunit(ctx, target,
                                                   texunit - GL_TEXTURE0,
                                                   true,
                                                   "glGenerateMultiTexMipmapEXT");
   validate_params_and_generate_mipmap(texObj,
                                       "glGenerateMultiTexMipmapEXT");
}