meta/generate_mipmap: Track framebuffer using gl_framebuffer instead of GL API 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/framebuffer.h"
39 #include "main/macros.h"
40 #include "main/mipmap.h"
41 #include "main/teximage.h"
42 #include "main/texobj.h"
43 #include "main/texparam.h"
44 #include "main/varray.h"
45 #include "main/viewport.h"
46 #include "drivers/common/meta.h"
47 #include "program/prog_instruction.h"
48
49
50 /**
51 * Check if the call to _mesa_meta_GenerateMipmap() will require a
52 * software fallback. The fallback path will require that the texture
53 * images are mapped.
54 * \return GL_TRUE if a fallback is needed, GL_FALSE otherwise
55 */
56 static bool
57 fallback_required(struct gl_context *ctx, GLenum target,
58 struct gl_texture_object *texObj)
59 {
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->fb == NULL) {
104 GLuint FBO;
105
106 _mesa_CreateFramebuffers(1, &FBO);
107 mipmap->fb = _mesa_lookup_framebuffer(ctx, FBO);
108 assert(mipmap->fb != NULL && mipmap->fb->Name == FBO);
109 }
110
111 _mesa_meta_framebuffer_texture_image(ctx, mipmap->fb,
112 GL_COLOR_ATTACHMENT0, baseImage, 0);
113
114 status = _mesa_check_framebuffer_status(ctx, mipmap->fb);
115 if (status != GL_FRAMEBUFFER_COMPLETE_EXT) {
116 _mesa_perf_debug(ctx, MESA_DEBUG_SEVERITY_HIGH,
117 "glGenerateMipmap() got incomplete FBO\n");
118 return true;
119 }
120
121 return false;
122 }
123
124 void
125 _mesa_meta_glsl_generate_mipmap_cleanup(struct gl_context *ctx,
126 struct gen_mipmap_state *mipmap)
127 {
128 if (mipmap->VAO == 0)
129 return;
130 _mesa_DeleteVertexArrays(1, &mipmap->VAO);
131 mipmap->VAO = 0;
132 _mesa_reference_buffer_object(ctx, &mipmap->buf_obj, NULL);
133 _mesa_reference_sampler_object(ctx, &mipmap->samp_obj, NULL);
134
135 if (mipmap->fb != NULL) {
136 _mesa_DeleteFramebuffers(1, &mipmap->fb->Name);
137 mipmap->fb = NULL;
138 }
139
140 _mesa_meta_blit_shader_table_cleanup(&mipmap->shaders);
141 }
142
143 static GLboolean
144 prepare_mipmap_level(struct gl_context *ctx,
145 struct gl_texture_object *texObj, GLuint level,
146 GLsizei width, GLsizei height, GLsizei depth,
147 GLenum intFormat, mesa_format format)
148 {
149 if (texObj->Target == GL_TEXTURE_1D_ARRAY) {
150 /* Work around Mesa expecting the number of array slices in "height". */
151 height = depth;
152 depth = 1;
153 }
154
155 return _mesa_prepare_mipmap_level(ctx, texObj, level, width, height, depth,
156 0, intFormat, format);
157 }
158
159 /**
160 * Called via ctx->Driver.GenerateMipmap()
161 * Note: We don't yet support 3D textures, or texture borders.
162 */
163 void
164 _mesa_meta_GenerateMipmap(struct gl_context *ctx, GLenum target,
165 struct gl_texture_object *texObj)
166 {
167 struct gen_mipmap_state *mipmap = &ctx->Meta->Mipmap;
168 struct vertex verts[4];
169 const GLuint baseLevel = texObj->BaseLevel;
170 const GLuint maxLevel = texObj->MaxLevel;
171 const GLint maxLevelSave = texObj->MaxLevel;
172 const GLboolean genMipmapSave = texObj->GenerateMipmap;
173 const GLboolean use_glsl_version = ctx->Extensions.ARB_vertex_shader &&
174 ctx->Extensions.ARB_fragment_shader;
175 GLenum faceTarget;
176 GLuint dstLevel;
177 struct gl_sampler_object *samp_obj_save = NULL;
178 GLint swizzle[4];
179 GLboolean swizzleSaved = GL_FALSE;
180
181 /* GLint so the compiler won't complain about type signedness mismatch in
182 * the calls to _mesa_texture_parameteriv below.
183 */
184 static const GLint always_false = GL_FALSE;
185 static const GLint always_true = GL_TRUE;
186
187 if (fallback_required(ctx, target, texObj)) {
188 _mesa_generate_mipmap(ctx, target, texObj);
189 return;
190 }
191
192 if (target >= GL_TEXTURE_CUBE_MAP_POSITIVE_X &&
193 target <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z) {
194 faceTarget = target;
195 target = GL_TEXTURE_CUBE_MAP;
196 } else {
197 faceTarget = target;
198 }
199
200 _mesa_meta_begin(ctx, MESA_META_ALL & ~MESA_META_DRAW_BUFFERS);
201
202 /* Choose between glsl version and fixed function version of
203 * GenerateMipmap function.
204 */
205 if (use_glsl_version) {
206 _mesa_meta_setup_vertex_objects(ctx, &mipmap->VAO, &mipmap->buf_obj, true,
207 2, 4, 0);
208 _mesa_meta_setup_blit_shader(ctx, target, false, &mipmap->shaders);
209 } else {
210 _mesa_meta_setup_ff_tnl_for_blit(ctx, &mipmap->VAO, &mipmap->buf_obj, 3);
211 _mesa_set_enable(ctx, target, GL_TRUE);
212 }
213
214 _mesa_reference_sampler_object(ctx, &samp_obj_save,
215 ctx->Texture.Unit[ctx->Texture.CurrentUnit].Sampler);
216
217 /* We may have been called from glGenerateTextureMipmap with CurrentUnit
218 * still set to 0, so we don't know when we can skip binding the texture.
219 * Assume that _mesa_BindTexture will be fast if we're rebinding the same
220 * texture.
221 */
222 _mesa_BindTexture(target, texObj->Name);
223
224 if (mipmap->samp_obj == NULL) {
225 mipmap->samp_obj = ctx->Driver.NewSamplerObject(ctx, 0xDEADBEEF);
226 if (mipmap->samp_obj == NULL) {
227 /* This is a bit lazy. Flag out of memory, and then don't bother to
228 * clean up. Once out of memory is flagged, the only realistic next
229 * move is to destroy the context. That will trigger all the right
230 * clean up.
231 */
232 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glGenerateMipmap");
233 return;
234 }
235
236 _mesa_set_sampler_filters(ctx, mipmap->samp_obj, GL_LINEAR_MIPMAP_LINEAR,
237 GL_LINEAR);
238 _mesa_set_sampler_wrap(ctx, mipmap->samp_obj, GL_CLAMP_TO_EDGE,
239 GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE);
240
241 /* We don't want to encode or decode sRGB values; treat them as linear. */
242 _mesa_set_sampler_srgb_decode(ctx, mipmap->samp_obj, GL_SKIP_DECODE_EXT);
243 }
244
245 _mesa_bind_sampler(ctx, ctx->Texture.CurrentUnit, mipmap->samp_obj);
246
247 assert(mipmap->fb != NULL);
248 _mesa_bind_framebuffers(ctx, mipmap->fb, mipmap->fb);
249
250 _mesa_texture_parameteriv(ctx, texObj, GL_GENERATE_MIPMAP, &always_false, false);
251
252 if (texObj->_Swizzle != SWIZZLE_NOOP) {
253 static const GLint swizzleNoop[4] = { GL_RED, GL_GREEN, GL_BLUE, GL_ALPHA };
254 memcpy(swizzle, texObj->Swizzle, sizeof(swizzle));
255 swizzleSaved = GL_TRUE;
256 _mesa_texture_parameteriv(ctx, texObj, GL_TEXTURE_SWIZZLE_RGBA,
257 swizzleNoop, false);
258 }
259
260 /* Silence valgrind warnings about reading uninitialized stack. */
261 memset(verts, 0, sizeof(verts));
262
263 /* setup vertex positions */
264 verts[0].x = -1.0F;
265 verts[0].y = -1.0F;
266 verts[1].x = 1.0F;
267 verts[1].y = -1.0F;
268 verts[2].x = 1.0F;
269 verts[2].y = 1.0F;
270 verts[3].x = -1.0F;
271 verts[3].y = 1.0F;
272
273 /* texture is already locked, unlock now */
274 _mesa_unlock_texture(ctx, texObj);
275
276 for (dstLevel = baseLevel + 1; dstLevel <= maxLevel; dstLevel++) {
277 const struct gl_texture_image *srcImage;
278 struct gl_texture_image *dstImage;
279 const GLuint srcLevel = dstLevel - 1;
280 GLuint layer;
281 GLsizei srcWidth, srcHeight, srcDepth;
282 GLsizei dstWidth, dstHeight, dstDepth;
283
284 srcImage = _mesa_select_tex_image(texObj, faceTarget, srcLevel);
285 assert(srcImage->Border == 0);
286
287 /* src size */
288 srcWidth = srcImage->Width;
289 if (target == GL_TEXTURE_1D_ARRAY) {
290 srcHeight = 1;
291 srcDepth = srcImage->Height;
292 } else {
293 srcHeight = srcImage->Height;
294 srcDepth = srcImage->Depth;
295 }
296
297 /* new dst size */
298 dstWidth = minify(srcWidth, 1);
299 dstHeight = minify(srcHeight, 1);
300 dstDepth = target == GL_TEXTURE_3D ? minify(srcDepth, 1) : srcDepth;
301
302 if (dstWidth == srcWidth &&
303 dstHeight == srcHeight &&
304 dstDepth == srcDepth) {
305 /* all done */
306 break;
307 }
308
309 /* Allocate storage for the destination mipmap image(s) */
310
311 /* Set MaxLevel large enough to hold the new level when we allocate it */
312 _mesa_texture_parameteriv(ctx, texObj, GL_TEXTURE_MAX_LEVEL,
313 (GLint *) &dstLevel, false);
314
315 if (!prepare_mipmap_level(ctx, texObj, dstLevel,
316 dstWidth, dstHeight, dstDepth,
317 srcImage->InternalFormat,
318 srcImage->TexFormat)) {
319 /* All done. We either ran out of memory or we would go beyond the
320 * last valid level of an immutable texture if we continued.
321 */
322 break;
323 }
324 dstImage = _mesa_select_tex_image(texObj, faceTarget, dstLevel);
325
326 /* limit minification to src level */
327 _mesa_texture_parameteriv(ctx, texObj, GL_TEXTURE_MAX_LEVEL,
328 (GLint *) &srcLevel, false);
329
330 /* setup viewport */
331 _mesa_set_viewport(ctx, 0, 0, 0, dstWidth, dstHeight);
332 _mesa_DrawBuffer(GL_COLOR_ATTACHMENT0);
333
334 for (layer = 0; layer < dstDepth; ++layer) {
335 /* Setup texture coordinates */
336 _mesa_meta_setup_texture_coords(faceTarget,
337 layer,
338 0, 0, /* xoffset, yoffset */
339 srcWidth, srcHeight, /* img size */
340 srcWidth, srcHeight, srcDepth,
341 verts[0].tex,
342 verts[1].tex,
343 verts[2].tex,
344 verts[3].tex);
345
346 /* upload vertex data */
347 _mesa_buffer_data(ctx, mipmap->buf_obj, GL_NONE, sizeof(verts), verts,
348 GL_DYNAMIC_DRAW, __func__);
349
350 _mesa_meta_framebuffer_texture_image(ctx, ctx->DrawBuffer,
351 GL_COLOR_ATTACHMENT0, dstImage,
352 layer);
353
354 /* sanity check */
355 if (_mesa_check_framebuffer_status(ctx, ctx->DrawBuffer) !=
356 GL_FRAMEBUFFER_COMPLETE) {
357 _mesa_problem(ctx, "Unexpected incomplete framebuffer in "
358 "_mesa_meta_GenerateMipmap()");
359 break;
360 }
361
362 assert(dstWidth == ctx->DrawBuffer->Width);
363 if (target == GL_TEXTURE_1D_ARRAY) {
364 assert(dstHeight == 1);
365 } else {
366 assert(dstHeight == ctx->DrawBuffer->Height);
367 }
368
369 _mesa_DrawArrays(GL_TRIANGLE_FAN, 0, 4);
370 }
371 }
372
373 _mesa_lock_texture(ctx, texObj); /* relock */
374
375 _mesa_bind_sampler(ctx, ctx->Texture.CurrentUnit, samp_obj_save);
376 _mesa_reference_sampler_object(ctx, &samp_obj_save, NULL);
377
378 _mesa_meta_end(ctx);
379
380 _mesa_texture_parameteriv(ctx, texObj, GL_TEXTURE_MAX_LEVEL, &maxLevelSave,
381 false);
382 if (genMipmapSave)
383 _mesa_texture_parameteriv(ctx, texObj, GL_GENERATE_MIPMAP, &always_true,
384 false);
385 if (swizzleSaved)
386 _mesa_texture_parameteriv(ctx, texObj, GL_TEXTURE_SWIZZLE_RGBA, swizzle,
387 false);
388 }