meta/generate_mipmap: Don't leak the framebuffer object
[mesa.git] / src / mesa / drivers / common / meta_generate_mipmap.c
1 /*
2 * Mesa 3-D graphics library
3 *
4 * Copyright (C) 2009 VMware, Inc. All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25 /**
26 * Meta operations. Some GL operations can be expressed in terms of
27 * other GL operations. For example, glBlitFramebuffer() can be done
28 * with texture mapping and glClear() can be done with polygon rendering.
29 *
30 * \author Brian Paul
31 */
32
33 #include "main/arrayobj.h"
34 #include "main/buffers.h"
35 #include "main/enums.h"
36 #include "main/enable.h"
37 #include "main/fbobject.h"
38 #include "main/macros.h"
39 #include "main/mipmap.h"
40 #include "main/teximage.h"
41 #include "main/texobj.h"
42 #include "main/texparam.h"
43 #include "main/varray.h"
44 #include "main/viewport.h"
45 #include "drivers/common/meta.h"
46 #include "program/prog_instruction.h"
47
48
49 /**
50 * Check if the call to _mesa_meta_GenerateMipmap() will require a
51 * software fallback. The fallback path will require that the texture
52 * images are mapped.
53 * \return GL_TRUE if a fallback is needed, GL_FALSE otherwise
54 */
55 static bool
56 fallback_required(struct gl_context *ctx, GLenum target,
57 struct gl_texture_object *texObj)
58 {
59 const GLuint fboSave = ctx->DrawBuffer->Name;
60 struct gen_mipmap_state *mipmap = &ctx->Meta->Mipmap;
61 struct gl_texture_image *baseImage;
62 GLuint srcLevel;
63 GLenum status;
64
65 /* check for fallbacks */
66 if (target == GL_TEXTURE_3D) {
67 _mesa_perf_debug(ctx, MESA_DEBUG_SEVERITY_HIGH,
68 "glGenerateMipmap() to %s target\n",
69 _mesa_enum_to_string(target));
70 return true;
71 }
72
73 srcLevel = texObj->BaseLevel;
74 baseImage = _mesa_select_tex_image(texObj, target, srcLevel);
75 if (!baseImage) {
76 _mesa_perf_debug(ctx, MESA_DEBUG_SEVERITY_HIGH,
77 "glGenerateMipmap() couldn't find base teximage\n");
78 return true;
79 }
80
81 if (_mesa_is_format_compressed(baseImage->TexFormat)) {
82 _mesa_perf_debug(ctx, MESA_DEBUG_SEVERITY_HIGH,
83 "glGenerateMipmap() with %s format\n",
84 _mesa_get_format_name(baseImage->TexFormat));
85 return true;
86 }
87
88 if (_mesa_get_format_color_encoding(baseImage->TexFormat) == GL_SRGB &&
89 !ctx->Extensions.EXT_texture_sRGB_decode) {
90 /* The texture format is sRGB but we can't turn off sRGB->linear
91 * texture sample conversion. So we won't be able to generate the
92 * right colors when rendering. Need to use a fallback.
93 */
94 _mesa_perf_debug(ctx, MESA_DEBUG_SEVERITY_HIGH,
95 "glGenerateMipmap() of sRGB texture without "
96 "sRGB decode\n");
97 return true;
98 }
99
100 /*
101 * Test that we can actually render in the texture's format.
102 */
103 if (!mipmap->FBO)
104 _mesa_GenFramebuffers(1, &mipmap->FBO);
105 _mesa_BindFramebuffer(GL_DRAW_FRAMEBUFFER, mipmap->FBO);
106
107 _mesa_meta_bind_fbo_image(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, baseImage, 0);
108
109 status = _mesa_CheckFramebufferStatus(GL_DRAW_FRAMEBUFFER);
110
111 _mesa_BindFramebuffer(GL_DRAW_FRAMEBUFFER, fboSave);
112
113 if (status != GL_FRAMEBUFFER_COMPLETE_EXT) {
114 _mesa_perf_debug(ctx, MESA_DEBUG_SEVERITY_HIGH,
115 "glGenerateMipmap() got incomplete FBO\n");
116 return true;
117 }
118
119 return false;
120 }
121
122 void
123 _mesa_meta_glsl_generate_mipmap_cleanup(struct gen_mipmap_state *mipmap)
124 {
125 if (mipmap->VAO == 0)
126 return;
127 _mesa_DeleteVertexArrays(1, &mipmap->VAO);
128 mipmap->VAO = 0;
129 _mesa_DeleteBuffers(1, &mipmap->VBO);
130 mipmap->VBO = 0;
131 _mesa_DeleteSamplers(1, &mipmap->Sampler);
132 mipmap->Sampler = 0;
133
134 if (mipmap->FBO != 0) {
135 _mesa_DeleteFramebuffers(1, &mipmap->FBO);
136 mipmap->FBO = 0;
137 }
138
139 _mesa_meta_blit_shader_table_cleanup(&mipmap->shaders);
140 }
141
142 static GLboolean
143 prepare_mipmap_level(struct gl_context *ctx,
144 struct gl_texture_object *texObj, GLuint level,
145 GLsizei width, GLsizei height, GLsizei depth,
146 GLenum intFormat, mesa_format format)
147 {
148 if (texObj->Target == GL_TEXTURE_1D_ARRAY) {
149 /* Work around Mesa expecting the number of array slices in "height". */
150 height = depth;
151 depth = 1;
152 }
153
154 return _mesa_prepare_mipmap_level(ctx, texObj, level, width, height, depth,
155 0, intFormat, format);
156 }
157
158 /**
159 * Called via ctx->Driver.GenerateMipmap()
160 * Note: We don't yet support 3D textures, or texture borders.
161 */
162 void
163 _mesa_meta_GenerateMipmap(struct gl_context *ctx, GLenum target,
164 struct gl_texture_object *texObj)
165 {
166 struct gen_mipmap_state *mipmap = &ctx->Meta->Mipmap;
167 struct vertex verts[4];
168 const GLuint baseLevel = texObj->BaseLevel;
169 const GLuint maxLevel = texObj->MaxLevel;
170 const GLint maxLevelSave = texObj->MaxLevel;
171 const GLboolean genMipmapSave = texObj->GenerateMipmap;
172 const GLboolean use_glsl_version = ctx->Extensions.ARB_vertex_shader &&
173 ctx->Extensions.ARB_fragment_shader;
174 GLenum faceTarget;
175 GLuint dstLevel;
176 GLuint samplerSave;
177 GLint swizzle[4];
178 GLboolean swizzleSaved = GL_FALSE;
179
180 if (fallback_required(ctx, target, texObj)) {
181 _mesa_generate_mipmap(ctx, target, texObj);
182 return;
183 }
184
185 if (target >= GL_TEXTURE_CUBE_MAP_POSITIVE_X &&
186 target <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z) {
187 faceTarget = target;
188 target = GL_TEXTURE_CUBE_MAP;
189 } else {
190 faceTarget = target;
191 }
192
193 _mesa_meta_begin(ctx, MESA_META_ALL & ~MESA_META_DRAW_BUFFERS);
194
195 /* Choose between glsl version and fixed function version of
196 * GenerateMipmap function.
197 */
198 if (use_glsl_version) {
199 _mesa_meta_setup_vertex_objects(&mipmap->VAO, &mipmap->VBO, true,
200 2, 4, 0);
201 _mesa_meta_setup_blit_shader(ctx, target, false, &mipmap->shaders);
202 } else {
203 _mesa_meta_setup_ff_tnl_for_blit(&mipmap->VAO, &mipmap->VBO, 3);
204 _mesa_set_enable(ctx, target, GL_TRUE);
205 }
206
207 samplerSave = ctx->Texture.Unit[ctx->Texture.CurrentUnit].Sampler ?
208 ctx->Texture.Unit[ctx->Texture.CurrentUnit].Sampler->Name : 0;
209
210 /* We may have been called from glGenerateTextureMipmap with CurrentUnit
211 * still set to 0, so we don't know when we can skip binding the texture.
212 * Assume that _mesa_BindTexture will be fast if we're rebinding the same
213 * texture.
214 */
215 _mesa_BindTexture(target, texObj->Name);
216
217 if (!mipmap->Sampler) {
218 _mesa_GenSamplers(1, &mipmap->Sampler);
219 _mesa_BindSampler(ctx->Texture.CurrentUnit, mipmap->Sampler);
220
221 _mesa_SamplerParameteri(mipmap->Sampler,
222 GL_TEXTURE_MIN_FILTER,
223 GL_LINEAR_MIPMAP_LINEAR);
224 _mesa_SamplerParameteri(mipmap->Sampler, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
225 _mesa_SamplerParameteri(mipmap->Sampler, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
226 _mesa_SamplerParameteri(mipmap->Sampler, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
227 _mesa_SamplerParameteri(mipmap->Sampler, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
228
229 /* We don't want to encode or decode sRGB values; treat them as linear.
230 * This is not technically correct for GLES3 but we don't get any API
231 * error at the moment.
232 */
233 if (ctx->Extensions.EXT_texture_sRGB_decode) {
234 _mesa_SamplerParameteri(mipmap->Sampler, GL_TEXTURE_SRGB_DECODE_EXT,
235 GL_SKIP_DECODE_EXT);
236 }
237 } else {
238 _mesa_BindSampler(ctx->Texture.CurrentUnit, mipmap->Sampler);
239 }
240
241 assert(mipmap->FBO != 0);
242 _mesa_BindFramebuffer(GL_FRAMEBUFFER_EXT, mipmap->FBO);
243
244 _mesa_TexParameteri(target, GL_GENERATE_MIPMAP, GL_FALSE);
245
246 if (texObj->_Swizzle != SWIZZLE_NOOP) {
247 static const GLint swizzleNoop[4] = { GL_RED, GL_GREEN, GL_BLUE, GL_ALPHA };
248 memcpy(swizzle, texObj->Swizzle, sizeof(swizzle));
249 swizzleSaved = GL_TRUE;
250 _mesa_TexParameteriv(target, GL_TEXTURE_SWIZZLE_RGBA, swizzleNoop);
251 }
252
253 /* Silence valgrind warnings about reading uninitialized stack. */
254 memset(verts, 0, sizeof(verts));
255
256 /* setup vertex positions */
257 verts[0].x = -1.0F;
258 verts[0].y = -1.0F;
259 verts[1].x = 1.0F;
260 verts[1].y = -1.0F;
261 verts[2].x = 1.0F;
262 verts[2].y = 1.0F;
263 verts[3].x = -1.0F;
264 verts[3].y = 1.0F;
265
266 /* texture is already locked, unlock now */
267 _mesa_unlock_texture(ctx, texObj);
268
269 for (dstLevel = baseLevel + 1; dstLevel <= maxLevel; dstLevel++) {
270 const struct gl_texture_image *srcImage;
271 struct gl_texture_image *dstImage;
272 const GLuint srcLevel = dstLevel - 1;
273 GLuint layer;
274 GLsizei srcWidth, srcHeight, srcDepth;
275 GLsizei dstWidth, dstHeight, dstDepth;
276
277 srcImage = _mesa_select_tex_image(texObj, faceTarget, srcLevel);
278 assert(srcImage->Border == 0);
279
280 /* src size */
281 srcWidth = srcImage->Width;
282 if (target == GL_TEXTURE_1D_ARRAY) {
283 srcHeight = 1;
284 srcDepth = srcImage->Height;
285 } else {
286 srcHeight = srcImage->Height;
287 srcDepth = srcImage->Depth;
288 }
289
290 /* new dst size */
291 dstWidth = minify(srcWidth, 1);
292 dstHeight = minify(srcHeight, 1);
293 dstDepth = target == GL_TEXTURE_3D ? minify(srcDepth, 1) : srcDepth;
294
295 if (dstWidth == srcWidth &&
296 dstHeight == srcHeight &&
297 dstDepth == srcDepth) {
298 /* all done */
299 break;
300 }
301
302 /* Allocate storage for the destination mipmap image(s) */
303
304 /* Set MaxLevel large enough to hold the new level when we allocate it */
305 _mesa_TexParameteri(target, GL_TEXTURE_MAX_LEVEL, dstLevel);
306
307 if (!prepare_mipmap_level(ctx, texObj, dstLevel,
308 dstWidth, dstHeight, dstDepth,
309 srcImage->InternalFormat,
310 srcImage->TexFormat)) {
311 /* All done. We either ran out of memory or we would go beyond the
312 * last valid level of an immutable texture if we continued.
313 */
314 break;
315 }
316 dstImage = _mesa_select_tex_image(texObj, faceTarget, dstLevel);
317
318 /* limit minification to src level */
319 _mesa_TexParameteri(target, GL_TEXTURE_MAX_LEVEL, srcLevel);
320
321 /* setup viewport */
322 _mesa_set_viewport(ctx, 0, 0, 0, dstWidth, dstHeight);
323 _mesa_DrawBuffer(GL_COLOR_ATTACHMENT0);
324
325 for (layer = 0; layer < dstDepth; ++layer) {
326 /* Setup texture coordinates */
327 _mesa_meta_setup_texture_coords(faceTarget,
328 layer,
329 0, 0, /* xoffset, yoffset */
330 srcWidth, srcHeight, /* img size */
331 srcWidth, srcHeight, srcDepth,
332 verts[0].tex,
333 verts[1].tex,
334 verts[2].tex,
335 verts[3].tex);
336
337 /* upload vertex data */
338 _mesa_BufferData(GL_ARRAY_BUFFER_ARB, sizeof(verts),
339 verts, GL_DYNAMIC_DRAW_ARB);
340
341 _mesa_meta_bind_fbo_image(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, dstImage, layer);
342
343 /* sanity check */
344 if (_mesa_CheckFramebufferStatus(GL_FRAMEBUFFER) !=
345 GL_FRAMEBUFFER_COMPLETE) {
346 _mesa_problem(ctx, "Unexpected incomplete framebuffer in "
347 "_mesa_meta_GenerateMipmap()");
348 break;
349 }
350
351 assert(dstWidth == ctx->DrawBuffer->Width);
352 if (target == GL_TEXTURE_1D_ARRAY) {
353 assert(dstHeight == 1);
354 } else {
355 assert(dstHeight == ctx->DrawBuffer->Height);
356 }
357
358 _mesa_DrawArrays(GL_TRIANGLE_FAN, 0, 4);
359 }
360 }
361
362 _mesa_lock_texture(ctx, texObj); /* relock */
363
364 _mesa_BindSampler(ctx->Texture.CurrentUnit, samplerSave);
365
366 _mesa_meta_end(ctx);
367
368 _mesa_TexParameteri(target, GL_TEXTURE_MAX_LEVEL, maxLevelSave);
369 if (genMipmapSave)
370 _mesa_TexParameteri(target, GL_GENERATE_MIPMAP, genMipmapSave);
371 if (swizzleSaved)
372 _mesa_TexParameteriv(target, GL_TEXTURE_SWIZZLE_RGBA, swizzle);
373 }