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 = _mesa_is_gles1(ctx);
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 return internalformat == GL_RGBA || internalformat == GL_RGB ||
92 internalformat == GL_LUMINANCE_ALPHA ||
93 internalformat == GL_LUMINANCE || internalformat == GL_ALPHA ||
94 (_mesa_is_es3_color_renderable(ctx, internalformat) &&
95 _mesa_is_es3_texture_filterable(ctx, internalformat));
96 }
97
98 return (!_mesa_is_enum_format_integer(internalformat) &&
99 !_mesa_is_depthstencil_format(internalformat) &&
100 !_mesa_is_astc_format(internalformat) &&
101 !_mesa_is_stencil_format(internalformat));
102 }
103
104 /**
105 * Implements glGenerateMipmap and glGenerateTextureMipmap.
106 * Generates all the mipmap levels below the base level.
107 * Error-checking is done only if caller is not NULL.
108 */
109 static ALWAYS_INLINE void
generate_texture_mipmap(struct gl_context * ctx,struct gl_texture_object * texObj,GLenum target,const char * caller)110 generate_texture_mipmap(struct gl_context *ctx,
111 struct gl_texture_object *texObj, GLenum target,
112 const char* caller)
113 {
114 struct gl_texture_image *srcImage;
115
116 FLUSH_VERTICES(ctx, 0, 0);
117
118 if (texObj->Attrib.BaseLevel >= texObj->Attrib.MaxLevel) {
119 /* nothing to do */
120 return;
121 }
122
123 if (caller && texObj->Target == GL_TEXTURE_CUBE_MAP &&
124 !_mesa_cube_complete(texObj)) {
125 _mesa_error(ctx, GL_INVALID_OPERATION,
126 "%s(incomplete cube map)", caller);
127 return;
128 }
129
130 _mesa_lock_texture(ctx, texObj);
131
132 texObj->External = GL_FALSE;
133
134 srcImage = _mesa_select_tex_image(texObj, target, texObj->Attrib.BaseLevel);
135 if (caller) {
136 if (!srcImage) {
137 _mesa_unlock_texture(ctx, texObj);
138 _mesa_error(ctx, GL_INVALID_OPERATION,
139 "%s(zero size base image)", caller);
140 return;
141 }
142
143 if (!_mesa_is_valid_generate_texture_mipmap_internalformat(ctx,
144 srcImage->InternalFormat)) {
145 _mesa_unlock_texture(ctx, texObj);
146 _mesa_error(ctx, GL_INVALID_OPERATION,
147 "%s(invalid internal format %s)", caller,
148 _mesa_enum_to_string(srcImage->InternalFormat));
149 return;
150 }
151
152 /* The GLES 2.0 spec says:
153 *
154 * "If the level zero array is stored in a compressed internal format,
155 * the error INVALID_OPERATION is generated."
156 *
157 * and this text is gone from the GLES 3.0 spec.
158 */
159 if (_mesa_is_gles2(ctx) && ctx->Version < 30 &&
160 _mesa_is_format_compressed(srcImage->TexFormat)) {
161 _mesa_unlock_texture(ctx, texObj);
162 _mesa_error(ctx, GL_INVALID_OPERATION, "generate mipmaps on compressed texture");
163 return;
164 }
165 }
166
167 if (srcImage->Width == 0 || srcImage->Height == 0) {
168 _mesa_unlock_texture(ctx, texObj);
169 return;
170 }
171
172 if (target == GL_TEXTURE_CUBE_MAP) {
173 GLuint face;
174 for (face = 0; face < 6; face++) {
175 st_generate_mipmap(ctx,
176 GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, texObj);
177 }
178 }
179 else {
180 st_generate_mipmap(ctx, target, texObj);
181 }
182 _mesa_unlock_texture(ctx, texObj);
183 }
184
185 /**
186 * Generate all the mipmap levels below the base level.
187 * Note: this GL function would be more useful if one could specify a
188 * cube face, a set of array slices, etc.
189 */
190 void GLAPIENTRY
_mesa_GenerateMipmap_no_error(GLenum target)191 _mesa_GenerateMipmap_no_error(GLenum target)
192 {
193 GET_CURRENT_CONTEXT(ctx);
194
195 struct gl_texture_object *texObj = _mesa_get_current_tex_object(ctx, target);
196 generate_texture_mipmap(ctx, texObj, target, NULL);
197 }
198
199 void GLAPIENTRY
_mesa_GenerateMipmap(GLenum target)200 _mesa_GenerateMipmap(GLenum target)
201 {
202 struct gl_texture_object *texObj;
203 GET_CURRENT_CONTEXT(ctx);
204
205 if (!_mesa_is_valid_generate_texture_mipmap_target(ctx, target)) {
206 _mesa_error(ctx, GL_INVALID_ENUM, "glGenerateMipmap(target=%s)",
207 _mesa_enum_to_string(target));
208 return;
209 }
210
211 texObj = _mesa_get_current_tex_object(ctx, target);
212 if (!texObj)
213 return;
214
215 generate_texture_mipmap(ctx, texObj, target, "glGenerateMipmap");
216 }
217
218 /**
219 * Generate all the mipmap levels below the base level.
220 */
221 void GLAPIENTRY
_mesa_GenerateTextureMipmap_no_error(GLuint texture)222 _mesa_GenerateTextureMipmap_no_error(GLuint texture)
223 {
224 GET_CURRENT_CONTEXT(ctx);
225
226 struct gl_texture_object *texObj = _mesa_lookup_texture(ctx, texture);
227 generate_texture_mipmap(ctx, texObj, texObj->Target, NULL);
228 }
229
230 static void
validate_params_and_generate_mipmap(struct gl_texture_object * texObj,const char * caller)231 validate_params_and_generate_mipmap(struct gl_texture_object *texObj, const char* caller)
232 {
233 GET_CURRENT_CONTEXT(ctx);
234
235 if (!texObj)
236 return;
237
238 if (!_mesa_is_valid_generate_texture_mipmap_target(ctx, texObj->Target)) {
239 _mesa_error(ctx, GL_INVALID_ENUM, "%s(target=%s)",
240 caller,
241 _mesa_enum_to_string(texObj->Target));
242 return;
243 }
244
245 generate_texture_mipmap(ctx, texObj, texObj->Target, caller);
246 }
247
248 void GLAPIENTRY
_mesa_GenerateTextureMipmap(GLuint texture)249 _mesa_GenerateTextureMipmap(GLuint texture)
250 {
251 struct gl_texture_object *texObj;
252 GET_CURRENT_CONTEXT(ctx);
253
254 texObj = _mesa_lookup_texture_err(ctx, texture, "glGenerateTextureMipmap");
255 validate_params_and_generate_mipmap(texObj, "glGenerateTextureMipmap");
256 }
257
258 void GLAPIENTRY
_mesa_GenerateTextureMipmapEXT(GLuint texture,GLenum target)259 _mesa_GenerateTextureMipmapEXT(GLuint texture, GLenum target)
260 {
261 struct gl_texture_object *texObj;
262 GET_CURRENT_CONTEXT(ctx);
263
264 texObj = _mesa_lookup_or_create_texture(ctx, target, texture,
265 false, true,
266 "glGenerateTextureMipmapEXT");
267 validate_params_and_generate_mipmap(texObj,
268 "glGenerateTextureMipmapEXT");
269 }
270
271 void GLAPIENTRY
_mesa_GenerateMultiTexMipmapEXT(GLenum texunit,GLenum target)272 _mesa_GenerateMultiTexMipmapEXT(GLenum texunit, GLenum target)
273 {
274 struct gl_texture_object *texObj;
275 GET_CURRENT_CONTEXT(ctx);
276
277 texObj = _mesa_get_texobj_by_target_and_texunit(ctx, target,
278 texunit - GL_TEXTURE0,
279 true,
280 "glGenerateMultiTexMipmapEXT");
281 validate_params_and_generate_mipmap(texObj,
282 "glGenerateMultiTexMipmapEXT");
283 }
284