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