6ca6f228976d47965096adea6e2efda1acf95f07
[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
47 /**
48 * Bind a particular texture level/layer to mipmap->FBO's GL_COLOR_ATTACHMENT0.
49 */
50 static void
51 bind_fbo_image(struct gl_texture_object *texObj, GLenum target,
52 GLuint level, GLuint layer)
53 {
54 switch (target) {
55 case GL_TEXTURE_1D:
56 _mesa_FramebufferTexture1D(GL_FRAMEBUFFER,
57 GL_COLOR_ATTACHMENT0,
58 target,
59 texObj->Name,
60 level);
61 break;
62 case GL_TEXTURE_1D_ARRAY:
63 case GL_TEXTURE_2D_ARRAY:
64 case GL_TEXTURE_3D:
65 _mesa_FramebufferTextureLayer(GL_FRAMEBUFFER,
66 GL_COLOR_ATTACHMENT0,
67 texObj->Name,
68 level,
69 layer);
70 break;
71 default: /* 2D / cube */
72 _mesa_FramebufferTexture2D(GL_FRAMEBUFFER,
73 GL_COLOR_ATTACHMENT0,
74 target,
75 texObj->Name,
76 level);
77 }
78 }
79
80 /**
81 * Check if the call to _mesa_meta_GenerateMipmap() will require a
82 * software fallback. The fallback path will require that the texture
83 * images are mapped.
84 * \return GL_TRUE if a fallback is needed, GL_FALSE otherwise
85 */
86 static bool
87 fallback_required(struct gl_context *ctx, GLenum target,
88 struct gl_texture_object *texObj)
89 {
90 const GLuint fboSave = ctx->DrawBuffer->Name;
91 struct gen_mipmap_state *mipmap = &ctx->Meta->Mipmap;
92 struct gl_texture_image *baseImage;
93 GLuint srcLevel;
94 GLenum status;
95
96 /* check for fallbacks */
97 if (target == GL_TEXTURE_3D ||
98 target == GL_TEXTURE_1D_ARRAY ||
99 target == GL_TEXTURE_2D_ARRAY) {
100 _mesa_perf_debug(ctx, MESA_DEBUG_SEVERITY_HIGH,
101 "glGenerateMipmap() to %s target\n",
102 _mesa_lookup_enum_by_nr(target));
103 return true;
104 }
105
106 srcLevel = texObj->BaseLevel;
107 baseImage = _mesa_select_tex_image(ctx, texObj, target, srcLevel);
108 if (!baseImage) {
109 _mesa_perf_debug(ctx, MESA_DEBUG_SEVERITY_HIGH,
110 "glGenerateMipmap() couldn't find base teximage\n");
111 return true;
112 }
113
114 if (_mesa_is_format_compressed(baseImage->TexFormat)) {
115 _mesa_perf_debug(ctx, MESA_DEBUG_SEVERITY_HIGH,
116 "glGenerateMipmap() with %s format\n",
117 _mesa_get_format_name(baseImage->TexFormat));
118 return true;
119 }
120
121 if (_mesa_get_format_color_encoding(baseImage->TexFormat) == GL_SRGB &&
122 !ctx->Extensions.EXT_texture_sRGB_decode) {
123 /* The texture format is sRGB but we can't turn off sRGB->linear
124 * texture sample conversion. So we won't be able to generate the
125 * right colors when rendering. Need to use a fallback.
126 */
127 _mesa_perf_debug(ctx, MESA_DEBUG_SEVERITY_HIGH,
128 "glGenerateMipmap() of sRGB texture without "
129 "sRGB decode\n");
130 return true;
131 }
132
133 /*
134 * Test that we can actually render in the texture's format.
135 */
136 if (!mipmap->FBO)
137 _mesa_GenFramebuffers(1, &mipmap->FBO);
138 _mesa_BindFramebuffer(GL_FRAMEBUFFER_EXT, mipmap->FBO);
139
140 bind_fbo_image(texObj, target, srcLevel, 0);
141
142 status = _mesa_CheckFramebufferStatus(GL_FRAMEBUFFER_EXT);
143
144 _mesa_BindFramebuffer(GL_FRAMEBUFFER_EXT, fboSave);
145
146 if (status != GL_FRAMEBUFFER_COMPLETE_EXT) {
147 _mesa_perf_debug(ctx, MESA_DEBUG_SEVERITY_HIGH,
148 "glGenerateMipmap() got incomplete FBO\n");
149 return true;
150 }
151
152 return false;
153 }
154
155 void
156 _mesa_meta_glsl_generate_mipmap_cleanup(struct gen_mipmap_state *mipmap)
157 {
158 if (mipmap->VAO == 0)
159 return;
160 _mesa_DeleteVertexArrays(1, &mipmap->VAO);
161 mipmap->VAO = 0;
162 _mesa_DeleteBuffers(1, &mipmap->VBO);
163 mipmap->VBO = 0;
164
165 _mesa_meta_blit_shader_table_cleanup(&mipmap->shaders);
166 }
167
168 /**
169 * Called via ctx->Driver.GenerateMipmap()
170 * Note: We don't yet support 3D textures, 1D/2D array textures or texture
171 * borders.
172 */
173 void
174 _mesa_meta_GenerateMipmap(struct gl_context *ctx, GLenum target,
175 struct gl_texture_object *texObj)
176 {
177 struct gen_mipmap_state *mipmap = &ctx->Meta->Mipmap;
178 struct vertex verts[4];
179 const GLuint baseLevel = texObj->BaseLevel;
180 const GLuint maxLevel = texObj->MaxLevel;
181 const GLint maxLevelSave = texObj->MaxLevel;
182 const GLboolean genMipmapSave = texObj->GenerateMipmap;
183 const GLuint fboSave = ctx->DrawBuffer->Name;
184 const GLuint currentTexUnitSave = ctx->Texture.CurrentUnit;
185 const GLboolean use_glsl_version = ctx->Extensions.ARB_vertex_shader &&
186 ctx->Extensions.ARB_fragment_shader;
187 GLenum faceTarget;
188 GLuint dstLevel;
189 const GLint slice = 0;
190 GLuint samplerSave;
191
192 if (fallback_required(ctx, target, texObj)) {
193 _mesa_generate_mipmap(ctx, target, texObj);
194 return;
195 }
196
197 if (target >= GL_TEXTURE_CUBE_MAP_POSITIVE_X &&
198 target <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z) {
199 faceTarget = target;
200 target = GL_TEXTURE_CUBE_MAP;
201 } else {
202 faceTarget = target;
203 }
204
205 _mesa_meta_begin(ctx, MESA_META_ALL);
206
207 /* Choose between glsl version and fixed function version of
208 * GenerateMipmap function.
209 */
210 if (use_glsl_version) {
211 _mesa_meta_setup_vertex_objects(&mipmap->VAO, &mipmap->VBO, true,
212 2, 3, 0);
213 _mesa_meta_setup_blit_shader(ctx, target, &mipmap->shaders);
214 } else {
215 _mesa_meta_setup_ff_tnl_for_blit(&mipmap->VAO, &mipmap->VBO, 3);
216 _mesa_set_enable(ctx, target, GL_TRUE);
217 }
218
219 samplerSave = ctx->Texture.Unit[ctx->Texture.CurrentUnit].Sampler ?
220 ctx->Texture.Unit[ctx->Texture.CurrentUnit].Sampler->Name : 0;
221
222 if (currentTexUnitSave != 0)
223 _mesa_BindTexture(target, texObj->Name);
224
225 if (!mipmap->Sampler) {
226 _mesa_GenSamplers(1, &mipmap->Sampler);
227 _mesa_BindSampler(ctx->Texture.CurrentUnit, mipmap->Sampler);
228
229 _mesa_SamplerParameteri(mipmap->Sampler,
230 GL_TEXTURE_MIN_FILTER,
231 GL_LINEAR_MIPMAP_LINEAR);
232 _mesa_SamplerParameteri(mipmap->Sampler, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
233 _mesa_SamplerParameteri(mipmap->Sampler, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
234 _mesa_SamplerParameteri(mipmap->Sampler, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
235 _mesa_SamplerParameteri(mipmap->Sampler, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
236
237 /* We don't want to encode or decode sRGB values; treat them as linear.
238 * This is not technically correct for GLES3 but we don't get any API
239 * error at the moment.
240 */
241 if (ctx->Extensions.EXT_texture_sRGB_decode) {
242 _mesa_SamplerParameteri(mipmap->Sampler, GL_TEXTURE_SRGB_DECODE_EXT,
243 GL_SKIP_DECODE_EXT);
244 }
245 } else {
246 _mesa_BindSampler(ctx->Texture.CurrentUnit, mipmap->Sampler);
247 }
248
249 assert(mipmap->FBO != 0);
250 _mesa_BindFramebuffer(GL_FRAMEBUFFER_EXT, mipmap->FBO);
251
252 _mesa_TexParameteri(target, GL_GENERATE_MIPMAP, GL_FALSE);
253
254 /* Silence valgrind warnings about reading uninitialized stack. */
255 memset(verts, 0, sizeof(verts));
256
257 /* Setup texture coordinates */
258 _mesa_meta_setup_texture_coords(faceTarget,
259 slice,
260 0, 0, 1, /* width, height never used here */
261 verts[0].tex,
262 verts[1].tex,
263 verts[2].tex,
264 verts[3].tex);
265
266 /* setup vertex positions */
267 verts[0].x = -1.0F;
268 verts[0].y = -1.0F;
269 verts[1].x = 1.0F;
270 verts[1].y = -1.0F;
271 verts[2].x = 1.0F;
272 verts[2].y = 1.0F;
273 verts[3].x = -1.0F;
274 verts[3].y = 1.0F;
275
276 /* upload vertex data */
277 _mesa_BufferData(GL_ARRAY_BUFFER_ARB, sizeof(verts),
278 verts, GL_DYNAMIC_DRAW_ARB);
279
280 /* texture is already locked, unlock now */
281 _mesa_unlock_texture(ctx, texObj);
282
283 for (dstLevel = baseLevel + 1; dstLevel <= maxLevel; dstLevel++) {
284 const struct gl_texture_image *srcImage;
285 const GLuint srcLevel = dstLevel - 1;
286 GLsizei srcWidth, srcHeight, srcDepth;
287 GLsizei dstWidth, dstHeight, dstDepth;
288
289 srcImage = _mesa_select_tex_image(ctx, texObj, faceTarget, srcLevel);
290 assert(srcImage->Border == 0);
291
292 /* src size */
293 srcWidth = srcImage->Width;
294 srcHeight = srcImage->Height;
295 srcDepth = srcImage->Depth;
296
297 /* new dst size */
298 dstWidth = minify(srcWidth, 1);
299 dstHeight = minify(srcHeight, 1);
300 dstDepth = minify(srcDepth, 1);
301
302 if (dstWidth == srcImage->Width &&
303 dstHeight == srcImage->Height &&
304 dstDepth == srcImage->Depth) {
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_TexParameteri(target, GL_TEXTURE_MAX_LEVEL, dstLevel);
313
314 if (!_mesa_prepare_mipmap_level(ctx, texObj, dstLevel,
315 dstWidth, dstHeight, dstDepth,
316 srcImage->Border,
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
325 /* limit minification to src level */
326 _mesa_TexParameteri(target, GL_TEXTURE_MAX_LEVEL, srcLevel);
327
328 bind_fbo_image(texObj, faceTarget, dstLevel, 0);
329
330 _mesa_DrawBuffer(GL_COLOR_ATTACHMENT0_EXT);
331
332 /* sanity check */
333 if (_mesa_CheckFramebufferStatus(GL_FRAMEBUFFER) !=
334 GL_FRAMEBUFFER_COMPLETE) {
335 _mesa_problem(ctx, "Unexpected incomplete framebuffer in "
336 "_mesa_meta_GenerateMipmap()");
337 break;
338 }
339
340 assert(dstWidth == ctx->DrawBuffer->Width);
341 assert(dstHeight == ctx->DrawBuffer->Height);
342
343 /* setup viewport */
344 _mesa_set_viewport(ctx, 0, 0, 0, dstWidth, dstHeight);
345
346 _mesa_DrawArrays(GL_TRIANGLE_FAN, 0, 4);
347 }
348
349 _mesa_lock_texture(ctx, texObj); /* relock */
350
351 _mesa_BindSampler(ctx->Texture.CurrentUnit, samplerSave);
352
353 _mesa_meta_end(ctx);
354
355 _mesa_TexParameteri(target, GL_TEXTURE_MAX_LEVEL, maxLevelSave);
356 if (genMipmapSave)
357 _mesa_TexParameteri(target, GL_GENERATE_MIPMAP, genMipmapSave);
358
359 _mesa_BindFramebuffer(GL_FRAMEBUFFER_EXT, fboSave);
360 }