mesa/genmipmap: Added a function to check if the target is valid
[mesa.git] / src / mesa / main / genmipmap.c
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
41 bool
42 _mesa_is_valid_generate_texture_mipmap_target(struct gl_context *ctx,
43 GLenum target)
44 {
45 GLboolean error;
46
47 switch (target) {
48 case GL_TEXTURE_1D:
49 error = _mesa_is_gles(ctx);
50 break;
51 case GL_TEXTURE_2D:
52 error = GL_FALSE;
53 break;
54 case GL_TEXTURE_3D:
55 error = ctx->API == API_OPENGLES;
56 break;
57 case GL_TEXTURE_CUBE_MAP:
58 error = !ctx->Extensions.ARB_texture_cube_map;
59 break;
60 case GL_TEXTURE_1D_ARRAY:
61 error = _mesa_is_gles(ctx) || !ctx->Extensions.EXT_texture_array;
62 break;
63 case GL_TEXTURE_2D_ARRAY:
64 error = (_mesa_is_gles(ctx) && ctx->Version < 30)
65 || !ctx->Extensions.EXT_texture_array;
66 break;
67 case GL_TEXTURE_CUBE_MAP_ARRAY:
68 error = _mesa_is_gles(ctx) ||
69 !ctx->Extensions.ARB_texture_cube_map_array;
70 break;
71 default:
72 error = GL_TRUE;
73 }
74
75 return (error != GL_TRUE);
76 }
77
78 /**
79 * Implements glGenerateMipmap and glGenerateTextureMipmap.
80 * Generates all the mipmap levels below the base level.
81 */
82 void
83 _mesa_generate_texture_mipmap(struct gl_context *ctx,
84 struct gl_texture_object *texObj, GLenum target,
85 bool dsa)
86 {
87 struct gl_texture_image *srcImage;
88 const char *suffix = dsa ? "Texture" : "";
89
90 FLUSH_VERTICES(ctx, 0);
91
92 if (!_mesa_is_valid_generate_texture_mipmap_target(ctx, target)) {
93 _mesa_error(ctx, GL_INVALID_ENUM, "glGenerate%sMipmap(target=%s)",
94 suffix, _mesa_enum_to_string(target));
95 return;
96 }
97
98 if (texObj->BaseLevel >= texObj->MaxLevel) {
99 /* nothing to do */
100 return;
101 }
102
103 if (texObj->Target == GL_TEXTURE_CUBE_MAP &&
104 !_mesa_cube_complete(texObj)) {
105 _mesa_error(ctx, GL_INVALID_OPERATION,
106 "glGenerate%sMipmap(incomplete cube map)", suffix);
107 return;
108 }
109
110 _mesa_lock_texture(ctx, texObj);
111
112 srcImage = _mesa_select_tex_image(texObj, target, texObj->BaseLevel);
113 if (!srcImage) {
114 _mesa_unlock_texture(ctx, texObj);
115 _mesa_error(ctx, GL_INVALID_OPERATION,
116 "glGenerate%sMipmap(zero size base image)", suffix);
117 return;
118 }
119
120 if (_mesa_is_enum_format_integer(srcImage->InternalFormat) ||
121 _mesa_is_depthstencil_format(srcImage->InternalFormat) ||
122 _mesa_is_astc_format(srcImage->InternalFormat) ||
123 _mesa_is_stencil_format(srcImage->InternalFormat)) {
124 _mesa_unlock_texture(ctx, texObj);
125 _mesa_error(ctx, GL_INVALID_OPERATION,
126 "glGenerate%sMipmap(invalid internal format)", suffix);
127 return;
128 }
129
130 if (target == GL_TEXTURE_CUBE_MAP) {
131 GLuint face;
132 for (face = 0; face < 6; face++) {
133 ctx->Driver.GenerateMipmap(ctx,
134 GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, texObj);
135 }
136 }
137 else {
138 ctx->Driver.GenerateMipmap(ctx, target, texObj);
139 }
140 _mesa_unlock_texture(ctx, texObj);
141 }
142
143 /**
144 * Generate all the mipmap levels below the base level.
145 * Note: this GL function would be more useful if one could specify a
146 * cube face, a set of array slices, etc.
147 */
148 void GLAPIENTRY
149 _mesa_GenerateMipmap(GLenum target)
150 {
151 struct gl_texture_object *texObj;
152 GET_CURRENT_CONTEXT(ctx);
153
154 texObj = _mesa_get_current_tex_object(ctx, target);
155 if (!texObj)
156 return;
157
158 _mesa_generate_texture_mipmap(ctx, texObj, target, false);
159 }
160
161 /**
162 * Generate all the mipmap levels below the base level.
163 */
164 void GLAPIENTRY
165 _mesa_GenerateTextureMipmap(GLuint texture)
166 {
167 struct gl_texture_object *texObj;
168 GET_CURRENT_CONTEXT(ctx);
169
170 texObj = _mesa_lookup_texture_err(ctx, texture, "glGenerateTextureMipmap");
171 if (!texObj)
172 return;
173
174 _mesa_generate_texture_mipmap(ctx, texObj, texObj->Target, true);
175 }