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