1 /*
2 * Mesa 3-D graphics library
3 *
4 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
5 * Copyright (C) 1999-2013 VMware, Inc. All Rights Reserved.
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 * glGenerateMipmap function
29 */
30
31 #include "context.h"
32 #include "enums.h"
33 #include "genmipmap.h"
34 #include "glformats.h"
35 #include "macros.h"
36 #include "mtypes.h"
37 #include "teximage.h"
38 #include "texobj.h"
39 #include "hash.h"
40 #include "api_exec_decl.h"
41
42 #include "state_tracker/st_gen_mipmap.h"
43
44 bool
_mesa_is_valid_generate_texture_mipmap_target(struct gl_context * ctx,GLenum target)45 _mesa_is_valid_generate_texture_mipmap_target(struct gl_context *ctx,
46 GLenum target)
47 {
48 bool error;
49
50 switch (target) {
51 case GL_TEXTURE_1D:
52 error = _mesa_is_gles(ctx);
53 break;
54 case GL_TEXTURE_2D:
55 error = false;
56 break;
57 case GL_TEXTURE_3D:
58 error = ctx->API == API_OPENGLES;
59 break;
60 case GL_TEXTURE_CUBE_MAP:
61 error = false;
62 break;
63 case GL_TEXTURE_1D_ARRAY:
64 error = _mesa_is_gles(ctx) || !ctx->Extensions.EXT_texture_array;
65 break;
66 case GL_TEXTURE_2D_ARRAY:
67 error = (_mesa_is_gles(ctx) && ctx->Version < 30)
68 || !ctx->Extensions.EXT_texture_array;
69 break;
70 case GL_TEXTURE_CUBE_MAP_ARRAY:
71 error = !_mesa_has_texture_cube_map_array(ctx);
72 break;
73 default:
74 error = true;
75 }
76
77 return !error;
78 }
79
80 bool
_mesa_is_valid_generate_texture_mipmap_internalformat(struct gl_context * ctx,GLenum internalformat)81 _mesa_is_valid_generate_texture_mipmap_internalformat(struct gl_context *ctx,
82 GLenum internalformat)
83 {
84 if (_mesa_is_gles3(ctx)) {
85 /* From the ES 3.2 specification's description of GenerateMipmap():
86 * "An INVALID_OPERATION error is generated if the levelbase array was
87 * not specified with an unsized internal format from table 8.3 or a
88 * sized internal format that is both color-renderable and
89 * texture-filterable according to table 8.10."
90 *
91 * GL_EXT_texture_format_BGRA8888 adds a GL_BGRA_EXT unsized internal
92 * format, and includes it in a very similar looking table. So we
93 * include it here as well.
94 */
95 return internalformat == GL_RGBA || internalformat == GL_RGB ||
96 internalformat == GL_LUMINANCE_ALPHA ||
97 internalformat == GL_LUMINANCE || internalformat == GL_ALPHA ||
98 internalformat == GL_BGRA_EXT ||
99 (_mesa_is_es3_color_renderable(ctx, internalformat) &&
100 _mesa_is_es3_texture_filterable(ctx, internalformat));
101 }
102
103 return (!_mesa_is_enum_format_integer(internalformat) &&
104 !_mesa_is_depthstencil_format(internalformat) &&
105 !_mesa_is_astc_format(internalformat) &&
106 !_mesa_is_stencil_format(internalformat));
107 }
108
109 /**
110 * Implements glGenerateMipmap and glGenerateTextureMipmap.
111 * Generates all the mipmap levels below the base level.
112 * Error-checking is done only if caller is not NULL.
113 */
114 static ALWAYS_INLINE void
generate_texture_mipmap(struct gl_context * ctx,struct gl_texture_object * texObj,GLenum target,const char * caller)115 generate_texture_mipmap(struct gl_context *ctx,
116 struct gl_texture_object *texObj, GLenum target,
117 const char* caller)
118 {
119 struct gl_texture_image *srcImage;
120
121 FLUSH_VERTICES(ctx, 0, 0);
122
123 if (texObj->Attrib.BaseLevel >= texObj->Attrib.MaxLevel) {
124 /* nothing to do */
125 return;
126 }
127
128 if (caller && texObj->Target == GL_TEXTURE_CUBE_MAP &&
129 !_mesa_cube_complete(texObj)) {
130 _mesa_error(ctx, GL_INVALID_OPERATION,
131 "%s(incomplete cube map)", caller);
132 return;
133 }
134
135 _mesa_lock_texture(ctx, texObj);
136
137 texObj->External = GL_FALSE;
138
139 srcImage = _mesa_select_tex_image(texObj, target, texObj->Attrib.BaseLevel);
140 if (caller) {
141 if (!srcImage) {
142 _mesa_unlock_texture(ctx, texObj);
143 _mesa_error(ctx, GL_INVALID_OPERATION,
144 "%s(zero size base image)", caller);
145 return;
146 }
147
148 if (!_mesa_is_valid_generate_texture_mipmap_internalformat(ctx,
149 srcImage->InternalFormat)) {
150 _mesa_unlock_texture(ctx, texObj);
151 _mesa_error(ctx, GL_INVALID_OPERATION,
152 "%s(invalid internal format %s)", caller,
153 _mesa_enum_to_string(srcImage->InternalFormat));
154 return;
155 }
156
157 /* The GLES 2.0 spec says:
158 *
159 * "If the level zero array is stored in a compressed internal format,
160 * the error INVALID_OPERATION is generated."
161 *
162 * and this text is gone from the GLES 3.0 spec.
163 */
164 if (ctx->API == API_OPENGLES2 && ctx->Version < 30 &&
165 _mesa_is_format_compressed(srcImage->TexFormat)) {
166 _mesa_unlock_texture(ctx, texObj);
167 _mesa_error(ctx, GL_INVALID_OPERATION, "generate mipmaps on compressed texture");
168 return;
169 }
170 }
171
172 if (srcImage->Width == 0 || srcImage->Height == 0) {
173 _mesa_unlock_texture(ctx, texObj);
174 return;
175 }
176
177 if (target == GL_TEXTURE_CUBE_MAP) {
178 GLuint face;
179 for (face = 0; face < 6; face++) {
180 st_generate_mipmap(ctx,
181 GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, texObj);
182 }
183 }
184 else {
185 st_generate_mipmap(ctx, target, texObj);
186 }
187 _mesa_unlock_texture(ctx, texObj);
188 }
189
190 /**
191 * Generate all the mipmap levels below the base level.
192 * Note: this GL function would be more useful if one could specify a
193 * cube face, a set of array slices, etc.
194 */
195 void GLAPIENTRY
_mesa_GenerateMipmap_no_error(GLenum target)196 _mesa_GenerateMipmap_no_error(GLenum target)
197 {
198 GET_CURRENT_CONTEXT(ctx);
199
200 struct gl_texture_object *texObj = _mesa_get_current_tex_object(ctx, target);
201 generate_texture_mipmap(ctx, texObj, target, NULL);
202 }
203
204 void GLAPIENTRY
_mesa_GenerateMipmap(GLenum target)205 _mesa_GenerateMipmap(GLenum target)
206 {
207 struct gl_texture_object *texObj;
208 GET_CURRENT_CONTEXT(ctx);
209
210 if (!_mesa_is_valid_generate_texture_mipmap_target(ctx, target)) {
211 _mesa_error(ctx, GL_INVALID_ENUM, "glGenerateMipmap(target=%s)",
212 _mesa_enum_to_string(target));
213 return;
214 }
215
216 texObj = _mesa_get_current_tex_object(ctx, target);
217 if (!texObj)
218 return;
219
220 generate_texture_mipmap(ctx, texObj, target, "glGenerateMipmap");
221 }
222
223 /**
224 * Generate all the mipmap levels below the base level.
225 */
226 void GLAPIENTRY
_mesa_GenerateTextureMipmap_no_error(GLuint texture)227 _mesa_GenerateTextureMipmap_no_error(GLuint texture)
228 {
229 GET_CURRENT_CONTEXT(ctx);
230
231 struct gl_texture_object *texObj = _mesa_lookup_texture(ctx, texture);
232 generate_texture_mipmap(ctx, texObj, texObj->Target, NULL);
233 }
234
235 static void
validate_params_and_generate_mipmap(struct gl_texture_object * texObj,const char * caller)236 validate_params_and_generate_mipmap(struct gl_texture_object *texObj, const char* caller)
237 {
238 GET_CURRENT_CONTEXT(ctx);
239
240 if (!texObj)
241 return;
242
243 if (!_mesa_is_valid_generate_texture_mipmap_target(ctx, texObj->Target)) {
244 _mesa_error(ctx, GL_INVALID_ENUM, "%s(target=%s)",
245 caller,
246 _mesa_enum_to_string(texObj->Target));
247 return;
248 }
249
250 generate_texture_mipmap(ctx, texObj, texObj->Target, caller);
251 }
252
253 void GLAPIENTRY
_mesa_GenerateTextureMipmap(GLuint texture)254 _mesa_GenerateTextureMipmap(GLuint texture)
255 {
256 struct gl_texture_object *texObj;
257 GET_CURRENT_CONTEXT(ctx);
258
259 texObj = _mesa_lookup_texture_err(ctx, texture, "glGenerateTextureMipmap");
260 validate_params_and_generate_mipmap(texObj, "glGenerateTextureMipmap");
261 }
262
263 void GLAPIENTRY
_mesa_GenerateTextureMipmapEXT(GLuint texture,GLenum target)264 _mesa_GenerateTextureMipmapEXT(GLuint texture, GLenum target)
265 {
266 struct gl_texture_object *texObj;
267 GET_CURRENT_CONTEXT(ctx);
268
269 texObj = _mesa_lookup_or_create_texture(ctx, target, texture,
270 false, true,
271 "glGenerateTextureMipmapEXT");
272 validate_params_and_generate_mipmap(texObj,
273 "glGenerateTextureMipmapEXT");
274 }
275
276 void GLAPIENTRY
_mesa_GenerateMultiTexMipmapEXT(GLenum texunit,GLenum target)277 _mesa_GenerateMultiTexMipmapEXT(GLenum texunit, GLenum target)
278 {
279 struct gl_texture_object *texObj;
280 GET_CURRENT_CONTEXT(ctx);
281
282 texObj = _mesa_get_texobj_by_target_and_texunit(ctx, target,
283 texunit - GL_TEXTURE0,
284 true,
285 "glGenerateMultiTexMipmapEXT");
286 validate_params_and_generate_mipmap(texObj,
287 "glGenerateMultiTexMipmapEXT");
288 }
289