meta/generate_mipmap: Track sampler using gl_sampler_object instead of GL API object...
[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_bind_fbo_image(fbo_target, GL_COLOR_ATTACHMENT0, baseImage, 0);
117
118 status = _mesa_CheckFramebufferStatus(fbo_target);
119
120 _mesa_BindFramebuffer(fbo_target, fboSave);
121
122 if (status != GL_FRAMEBUFFER_COMPLETE_EXT) {
123 _mesa_perf_debug(ctx, MESA_DEBUG_SEVERITY_HIGH,
124 "glGenerateMipmap() got incomplete FBO\n");
125 return true;
126 }
127
128 return false;
129 }
130
131 void
132 _mesa_meta_glsl_generate_mipmap_cleanup(struct gl_context *ctx,
133 struct gen_mipmap_state *mipmap)
134 {
135 if (mipmap->VAO == 0)
136 return;
137 _mesa_DeleteVertexArrays(1, &mipmap->VAO);
138 mipmap->VAO = 0;
139 _mesa_reference_buffer_object(ctx, &mipmap->buf_obj, NULL);
140
141 if (mipmap->samp_obj != NULL) {
142 _mesa_DeleteSamplers(1, &mipmap->samp_obj->Name);
143 mipmap->samp_obj = NULL;
144 }
145
146 if (mipmap->FBO != 0) {
147 _mesa_DeleteFramebuffers(1, &mipmap->FBO);
148 mipmap->FBO = 0;
149 }
150
151 _mesa_meta_blit_shader_table_cleanup(&mipmap->shaders);
152 }
153
154 static GLboolean
155 prepare_mipmap_level(struct gl_context *ctx,
156 struct gl_texture_object *texObj, GLuint level,
157 GLsizei width, GLsizei height, GLsizei depth,
158 GLenum intFormat, mesa_format format)
159 {
160 if (texObj->Target == GL_TEXTURE_1D_ARRAY) {
161 /* Work around Mesa expecting the number of array slices in "height". */
162 height = depth;
163 depth = 1;
164 }
165
166 return _mesa_prepare_mipmap_level(ctx, texObj, level, width, height, depth,
167 0, intFormat, format);
168 }
169
170 /**
171 * Called via ctx->Driver.GenerateMipmap()
172 * Note: We don't yet support 3D textures, or texture borders.
173 */
174 void
175 _mesa_meta_GenerateMipmap(struct gl_context *ctx, GLenum target,
176 struct gl_texture_object *texObj)
177 {
178 struct gen_mipmap_state *mipmap = &ctx->Meta->Mipmap;
179 struct vertex verts[4];
180 const GLuint baseLevel = texObj->BaseLevel;
181 const GLuint maxLevel = texObj->MaxLevel;
182 const GLint maxLevelSave = texObj->MaxLevel;
183 const GLboolean genMipmapSave = texObj->GenerateMipmap;
184 const GLboolean use_glsl_version = ctx->Extensions.ARB_vertex_shader &&
185 ctx->Extensions.ARB_fragment_shader;
186 GLenum faceTarget;
187 GLuint dstLevel;
188 GLuint samplerSave;
189 GLint swizzle[4];
190 GLboolean swizzleSaved = GL_FALSE;
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 & ~MESA_META_DRAW_BUFFERS);
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(ctx, &mipmap->VAO, &mipmap->buf_obj, true,
212 2, 4, 0);
213 _mesa_meta_setup_blit_shader(ctx, target, false, &mipmap->shaders);
214 } else {
215 _mesa_meta_setup_ff_tnl_for_blit(ctx, &mipmap->VAO, &mipmap->buf_obj, 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 /* We may have been called from glGenerateTextureMipmap with CurrentUnit
223 * still set to 0, so we don't know when we can skip binding the texture.
224 * Assume that _mesa_BindTexture will be fast if we're rebinding the same
225 * texture.
226 */
227 _mesa_BindTexture(target, texObj->Name);
228
229 if (mipmap->samp_obj == NULL) {
230 GLuint sampler;
231
232 _mesa_GenSamplers(1, &sampler);
233
234 mipmap->samp_obj = _mesa_lookup_samplerobj(ctx, sampler);
235 assert(mipmap->samp_obj != NULL && mipmap->samp_obj->Name == sampler);
236
237 _mesa_bind_sampler(ctx, ctx->Texture.CurrentUnit, mipmap->samp_obj);
238
239 _mesa_set_sampler_filters(ctx, mipmap->samp_obj, GL_LINEAR_MIPMAP_LINEAR,
240 GL_LINEAR);
241 _mesa_set_sampler_wrap(ctx, mipmap->samp_obj, GL_CLAMP_TO_EDGE,
242 GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE);
243
244 /* We don't want to encode or decode sRGB values; treat them as linear.
245 * This is not technically correct for GLES3 but we don't get any API
246 * error at the moment.
247 */
248 if (ctx->Extensions.EXT_texture_sRGB_decode) {
249 _mesa_set_sampler_srgb_decode(ctx, mipmap->samp_obj, GL_SKIP_DECODE_EXT);
250 }
251 } else {
252 _mesa_bind_sampler(ctx, ctx->Texture.CurrentUnit, mipmap->samp_obj);
253 }
254
255 assert(mipmap->FBO != 0);
256 _mesa_BindFramebuffer(GL_FRAMEBUFFER_EXT, mipmap->FBO);
257
258 _mesa_TexParameteri(target, GL_GENERATE_MIPMAP, GL_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_TexParameteriv(target, GL_TEXTURE_SWIZZLE_RGBA, swizzleNoop);
265 }
266
267 /* Silence valgrind warnings about reading uninitialized stack. */
268 memset(verts, 0, sizeof(verts));
269
270 /* setup vertex positions */
271 verts[0].x = -1.0F;
272 verts[0].y = -1.0F;
273 verts[1].x = 1.0F;
274 verts[1].y = -1.0F;
275 verts[2].x = 1.0F;
276 verts[2].y = 1.0F;
277 verts[3].x = -1.0F;
278 verts[3].y = 1.0F;
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 struct gl_texture_image *dstImage;
286 const GLuint srcLevel = dstLevel - 1;
287 GLuint layer;
288 GLsizei srcWidth, srcHeight, srcDepth;
289 GLsizei dstWidth, dstHeight, dstDepth;
290
291 srcImage = _mesa_select_tex_image(texObj, faceTarget, srcLevel);
292 assert(srcImage->Border == 0);
293
294 /* src size */
295 srcWidth = srcImage->Width;
296 if (target == GL_TEXTURE_1D_ARRAY) {
297 srcHeight = 1;
298 srcDepth = srcImage->Height;
299 } else {
300 srcHeight = srcImage->Height;
301 srcDepth = srcImage->Depth;
302 }
303
304 /* new dst size */
305 dstWidth = minify(srcWidth, 1);
306 dstHeight = minify(srcHeight, 1);
307 dstDepth = target == GL_TEXTURE_3D ? minify(srcDepth, 1) : srcDepth;
308
309 if (dstWidth == srcWidth &&
310 dstHeight == srcHeight &&
311 dstDepth == srcDepth) {
312 /* all done */
313 break;
314 }
315
316 /* Allocate storage for the destination mipmap image(s) */
317
318 /* Set MaxLevel large enough to hold the new level when we allocate it */
319 _mesa_TexParameteri(target, GL_TEXTURE_MAX_LEVEL, dstLevel);
320
321 if (!prepare_mipmap_level(ctx, texObj, dstLevel,
322 dstWidth, dstHeight, dstDepth,
323 srcImage->InternalFormat,
324 srcImage->TexFormat)) {
325 /* All done. We either ran out of memory or we would go beyond the
326 * last valid level of an immutable texture if we continued.
327 */
328 break;
329 }
330 dstImage = _mesa_select_tex_image(texObj, faceTarget, dstLevel);
331
332 /* limit minification to src level */
333 _mesa_TexParameteri(target, GL_TEXTURE_MAX_LEVEL, srcLevel);
334
335 /* setup viewport */
336 _mesa_set_viewport(ctx, 0, 0, 0, dstWidth, dstHeight);
337 _mesa_DrawBuffer(GL_COLOR_ATTACHMENT0);
338
339 for (layer = 0; layer < dstDepth; ++layer) {
340 /* Setup texture coordinates */
341 _mesa_meta_setup_texture_coords(faceTarget,
342 layer,
343 0, 0, /* xoffset, yoffset */
344 srcWidth, srcHeight, /* img size */
345 srcWidth, srcHeight, srcDepth,
346 verts[0].tex,
347 verts[1].tex,
348 verts[2].tex,
349 verts[3].tex);
350
351 /* upload vertex data */
352 _mesa_buffer_data(ctx, mipmap->buf_obj, GL_NONE, sizeof(verts), verts,
353 GL_DYNAMIC_DRAW, __func__);
354
355 _mesa_meta_bind_fbo_image(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, dstImage, layer);
356
357 /* sanity check */
358 if (_mesa_CheckFramebufferStatus(GL_FRAMEBUFFER) !=
359 GL_FRAMEBUFFER_COMPLETE) {
360 _mesa_problem(ctx, "Unexpected incomplete framebuffer in "
361 "_mesa_meta_GenerateMipmap()");
362 break;
363 }
364
365 assert(dstWidth == ctx->DrawBuffer->Width);
366 if (target == GL_TEXTURE_1D_ARRAY) {
367 assert(dstHeight == 1);
368 } else {
369 assert(dstHeight == ctx->DrawBuffer->Height);
370 }
371
372 _mesa_DrawArrays(GL_TRIANGLE_FAN, 0, 4);
373 }
374 }
375
376 _mesa_lock_texture(ctx, texObj); /* relock */
377
378 _mesa_BindSampler(ctx->Texture.CurrentUnit, samplerSave);
379
380 _mesa_meta_end(ctx);
381
382 _mesa_TexParameteri(target, GL_TEXTURE_MAX_LEVEL, maxLevelSave);
383 if (genMipmapSave)
384 _mesa_TexParameteri(target, GL_GENERATE_MIPMAP, genMipmapSave);
385 if (swizzleSaved)
386 _mesa_TexParameteriv(target, GL_TEXTURE_SWIZZLE_RGBA, swizzle);
387 }