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