2 * Mesa 3-D graphics library
4 * Copyright (C) 2009 VMware, Inc. All Rights Reserved.
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:
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
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.
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.
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"
51 * Check if the call to _mesa_meta_GenerateMipmap() will require a
52 * software fallback. The fallback path will require that the texture
54 * \return GL_TRUE if a fallback is needed, GL_FALSE otherwise
57 fallback_required(struct gl_context
*ctx
, GLenum target
,
58 struct gl_texture_object
*texObj
)
60 struct gen_mipmap_state
*mipmap
= &ctx
->Meta
->Mipmap
;
61 struct gl_texture_image
*baseImage
;
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
));
73 srcLevel
= texObj
->BaseLevel
;
74 baseImage
= _mesa_select_tex_image(texObj
, target
, srcLevel
);
76 _mesa_perf_debug(ctx
, MESA_DEBUG_SEVERITY_HIGH
,
77 "glGenerateMipmap() couldn't find base teximage\n");
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
));
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.
94 _mesa_perf_debug(ctx
, MESA_DEBUG_SEVERITY_HIGH
,
95 "glGenerateMipmap() of sRGB texture without "
101 * Test that we can actually render in the texture's format.
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");
112 _mesa_meta_framebuffer_texture_image(ctx
, mipmap
->fb
,
113 GL_COLOR_ATTACHMENT0
, baseImage
, 0);
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");
126 _mesa_meta_glsl_generate_mipmap_cleanup(struct gl_context
*ctx
,
127 struct gen_mipmap_state
*mipmap
)
129 if (mipmap
->VAO
== 0)
131 _mesa_DeleteVertexArrays(1, &mipmap
->VAO
);
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
);
137 _mesa_meta_blit_shader_table_cleanup(ctx
, &mipmap
->shaders
);
141 prepare_mipmap_level(struct gl_context
*ctx
,
142 struct gl_texture_object
*texObj
, GLuint level
,
143 GLsizei width
, GLsizei height
, GLsizei depth
,
144 GLenum intFormat
, mesa_format format
)
146 if (texObj
->Target
== GL_TEXTURE_1D_ARRAY
) {
147 /* Work around Mesa expecting the number of array slices in "height". */
152 return _mesa_prepare_mipmap_level(ctx
, texObj
, level
, width
, height
, depth
,
153 0, intFormat
, format
);
157 * Called via ctx->Driver.GenerateMipmap()
158 * Note: We don't yet support 3D textures, or texture borders.
161 _mesa_meta_GenerateMipmap(struct gl_context
*ctx
, GLenum target
,
162 struct gl_texture_object
*texObj
)
164 struct gen_mipmap_state
*mipmap
= &ctx
->Meta
->Mipmap
;
165 struct vertex verts
[4];
166 const GLuint baseLevel
= texObj
->BaseLevel
;
167 const GLuint maxLevel
= texObj
->MaxLevel
;
168 const GLint maxLevelSave
= texObj
->MaxLevel
;
169 const GLboolean genMipmapSave
= texObj
->GenerateMipmap
;
170 const GLboolean use_glsl_version
= ctx
->Extensions
.ARB_vertex_shader
&&
171 ctx
->Extensions
.ARB_fragment_shader
;
174 struct gl_sampler_object
*samp_obj_save
= NULL
;
176 GLboolean swizzleSaved
= GL_FALSE
;
178 /* GLint so the compiler won't complain about type signedness mismatch in
179 * the calls to _mesa_texture_parameteriv below.
181 static const GLint always_false
= GL_FALSE
;
182 static const GLint always_true
= GL_TRUE
;
184 if (fallback_required(ctx
, target
, texObj
)) {
185 _mesa_generate_mipmap(ctx
, target
, texObj
);
189 if (target
>= GL_TEXTURE_CUBE_MAP_POSITIVE_X
&&
190 target
<= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
) {
192 target
= GL_TEXTURE_CUBE_MAP
;
197 _mesa_meta_begin(ctx
, MESA_META_ALL
& ~MESA_META_DRAW_BUFFERS
);
199 /* Choose between glsl version and fixed function version of
200 * GenerateMipmap function.
202 if (use_glsl_version
) {
203 _mesa_meta_setup_vertex_objects(ctx
, &mipmap
->VAO
, &mipmap
->buf_obj
, true,
205 _mesa_meta_setup_blit_shader(ctx
, target
, false, &mipmap
->shaders
);
207 _mesa_meta_setup_ff_tnl_for_blit(ctx
, &mipmap
->VAO
, &mipmap
->buf_obj
, 3);
208 _mesa_set_enable(ctx
, target
, GL_TRUE
);
211 _mesa_reference_sampler_object(ctx
, &samp_obj_save
,
212 ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
].Sampler
);
214 /* We may have been called from glGenerateTextureMipmap with CurrentUnit
215 * still set to 0, so we don't know when we can skip binding the texture.
216 * Assume that _mesa_BindTexture will be fast if we're rebinding the same
219 _mesa_BindTexture(target
, texObj
->Name
);
221 if (mipmap
->samp_obj
== NULL
) {
222 mipmap
->samp_obj
= ctx
->Driver
.NewSamplerObject(ctx
, 0xDEADBEEF);
223 if (mipmap
->samp_obj
== NULL
) {
224 /* This is a bit lazy. Flag out of memory, and then don't bother to
225 * clean up. Once out of memory is flagged, the only realistic next
226 * move is to destroy the context. That will trigger all the right
229 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGenerateMipmap");
233 _mesa_set_sampler_filters(ctx
, mipmap
->samp_obj
, GL_LINEAR_MIPMAP_LINEAR
,
235 _mesa_set_sampler_wrap(ctx
, mipmap
->samp_obj
, GL_CLAMP_TO_EDGE
,
236 GL_CLAMP_TO_EDGE
, GL_CLAMP_TO_EDGE
);
238 /* We don't want to encode or decode sRGB values; treat them as linear. */
239 _mesa_set_sampler_srgb_decode(ctx
, mipmap
->samp_obj
, GL_SKIP_DECODE_EXT
);
242 _mesa_bind_sampler(ctx
, ctx
->Texture
.CurrentUnit
, mipmap
->samp_obj
);
244 assert(mipmap
->fb
!= NULL
);
245 _mesa_bind_framebuffers(ctx
, mipmap
->fb
, mipmap
->fb
);
247 _mesa_texture_parameteriv(ctx
, texObj
, GL_GENERATE_MIPMAP
, &always_false
, false);
249 if (texObj
->_Swizzle
!= SWIZZLE_NOOP
) {
250 static const GLint swizzleNoop
[4] = { GL_RED
, GL_GREEN
, GL_BLUE
, GL_ALPHA
};
251 memcpy(swizzle
, texObj
->Swizzle
, sizeof(swizzle
));
252 swizzleSaved
= GL_TRUE
;
253 _mesa_texture_parameteriv(ctx
, texObj
, GL_TEXTURE_SWIZZLE_RGBA
,
257 /* Silence valgrind warnings about reading uninitialized stack. */
258 memset(verts
, 0, sizeof(verts
));
260 /* setup vertex positions */
270 /* texture is already locked, unlock now */
271 _mesa_unlock_texture(ctx
, texObj
);
273 for (dstLevel
= baseLevel
+ 1; dstLevel
<= maxLevel
; dstLevel
++) {
274 const struct gl_texture_image
*srcImage
;
275 struct gl_texture_image
*dstImage
;
276 const GLuint srcLevel
= dstLevel
- 1;
278 GLsizei srcWidth
, srcHeight
, srcDepth
;
279 GLsizei dstWidth
, dstHeight
, dstDepth
;
281 srcImage
= _mesa_select_tex_image(texObj
, faceTarget
, srcLevel
);
282 assert(srcImage
->Border
== 0);
285 srcWidth
= srcImage
->Width
;
286 if (target
== GL_TEXTURE_1D_ARRAY
) {
288 srcDepth
= srcImage
->Height
;
290 srcHeight
= srcImage
->Height
;
291 srcDepth
= srcImage
->Depth
;
295 dstWidth
= minify(srcWidth
, 1);
296 dstHeight
= minify(srcHeight
, 1);
297 dstDepth
= target
== GL_TEXTURE_3D
? minify(srcDepth
, 1) : srcDepth
;
299 if (dstWidth
== srcWidth
&&
300 dstHeight
== srcHeight
&&
301 dstDepth
== srcDepth
) {
306 /* Allocate storage for the destination mipmap image(s) */
308 /* Set MaxLevel large enough to hold the new level when we allocate it */
309 _mesa_texture_parameteriv(ctx
, texObj
, GL_TEXTURE_MAX_LEVEL
,
310 (GLint
*) &dstLevel
, false);
312 if (!prepare_mipmap_level(ctx
, texObj
, dstLevel
,
313 dstWidth
, dstHeight
, dstDepth
,
314 srcImage
->InternalFormat
,
315 srcImage
->TexFormat
)) {
316 /* All done. We either ran out of memory or we would go beyond the
317 * last valid level of an immutable texture if we continued.
321 dstImage
= _mesa_select_tex_image(texObj
, faceTarget
, dstLevel
);
323 /* limit minification to src level */
324 _mesa_texture_parameteriv(ctx
, texObj
, GL_TEXTURE_MAX_LEVEL
,
325 (GLint
*) &srcLevel
, false);
328 _mesa_set_viewport(ctx
, 0, 0, 0, dstWidth
, dstHeight
);
329 _mesa_DrawBuffer(GL_COLOR_ATTACHMENT0
);
331 for (layer
= 0; layer
< dstDepth
; ++layer
) {
332 /* Setup texture coordinates */
333 _mesa_meta_setup_texture_coords(faceTarget
,
335 0, 0, /* xoffset, yoffset */
336 srcWidth
, srcHeight
, /* img size */
337 srcWidth
, srcHeight
, srcDepth
,
343 /* upload vertex data */
344 _mesa_buffer_data(ctx
, mipmap
->buf_obj
, GL_NONE
, sizeof(verts
), verts
,
345 GL_DYNAMIC_DRAW
, __func__
);
347 _mesa_meta_framebuffer_texture_image(ctx
, ctx
->DrawBuffer
,
348 GL_COLOR_ATTACHMENT0
, dstImage
,
352 if (_mesa_check_framebuffer_status(ctx
, ctx
->DrawBuffer
) !=
353 GL_FRAMEBUFFER_COMPLETE
) {
354 _mesa_problem(ctx
, "Unexpected incomplete framebuffer in "
355 "_mesa_meta_GenerateMipmap()");
359 assert(dstWidth
== ctx
->DrawBuffer
->Width
);
360 if (target
== GL_TEXTURE_1D_ARRAY
) {
361 assert(dstHeight
== 1);
363 assert(dstHeight
== ctx
->DrawBuffer
->Height
);
366 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
370 _mesa_lock_texture(ctx
, texObj
); /* relock */
372 _mesa_bind_sampler(ctx
, ctx
->Texture
.CurrentUnit
, samp_obj_save
);
373 _mesa_reference_sampler_object(ctx
, &samp_obj_save
, NULL
);
377 _mesa_texture_parameteriv(ctx
, texObj
, GL_TEXTURE_MAX_LEVEL
, &maxLevelSave
,
380 _mesa_texture_parameteriv(ctx
, texObj
, GL_GENERATE_MIPMAP
, &always_true
,
383 _mesa_texture_parameteriv(ctx
, texObj
, GL_TEXTURE_SWIZZLE_RGBA
, swizzle
,