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