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/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"
50 * Check if the call to _mesa_meta_GenerateMipmap() will require a
51 * software fallback. The fallback path will require that the texture
53 * \return GL_TRUE if a fallback is needed, GL_FALSE otherwise
56 fallback_required(struct gl_context
*ctx
, GLenum target
,
57 struct gl_texture_object
*texObj
)
59 const GLuint fboSave
= ctx
->DrawBuffer
->Name
;
60 struct gen_mipmap_state
*mipmap
= &ctx
->Meta
->Mipmap
;
61 struct gl_texture_image
*baseImage
;
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.
71 const GLenum fbo_target
= ctx
->API
== API_OPENGLES
72 ? GL_FRAMEBUFFER
: GL_DRAW_FRAMEBUFFER
;
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
));
82 srcLevel
= texObj
->BaseLevel
;
83 baseImage
= _mesa_select_tex_image(texObj
, target
, srcLevel
);
85 _mesa_perf_debug(ctx
, MESA_DEBUG_SEVERITY_HIGH
,
86 "glGenerateMipmap() couldn't find base teximage\n");
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
));
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.
103 _mesa_perf_debug(ctx
, MESA_DEBUG_SEVERITY_HIGH
,
104 "glGenerateMipmap() of sRGB texture without "
110 * Test that we can actually render in the texture's format.
113 _mesa_GenFramebuffers(1, &mipmap
->FBO
);
114 _mesa_BindFramebuffer(fbo_target
, mipmap
->FBO
);
116 _mesa_meta_bind_fbo_image(fbo_target
, GL_COLOR_ATTACHMENT0
, baseImage
, 0);
118 status
= _mesa_CheckFramebufferStatus(fbo_target
);
120 _mesa_BindFramebuffer(fbo_target
, fboSave
);
122 if (status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
123 _mesa_perf_debug(ctx
, MESA_DEBUG_SEVERITY_HIGH
,
124 "glGenerateMipmap() got incomplete FBO\n");
132 _mesa_meta_glsl_generate_mipmap_cleanup(struct gl_context
*ctx
,
133 struct gen_mipmap_state
*mipmap
)
135 if (mipmap
->VAO
== 0)
137 _mesa_DeleteVertexArrays(1, &mipmap
->VAO
);
139 _mesa_reference_buffer_object(ctx
, &mipmap
->buf_obj
, NULL
);
140 _mesa_DeleteSamplers(1, &mipmap
->Sampler
);
143 if (mipmap
->FBO
!= 0) {
144 _mesa_DeleteFramebuffers(1, &mipmap
->FBO
);
148 _mesa_meta_blit_shader_table_cleanup(&mipmap
->shaders
);
152 prepare_mipmap_level(struct gl_context
*ctx
,
153 struct gl_texture_object
*texObj
, GLuint level
,
154 GLsizei width
, GLsizei height
, GLsizei depth
,
155 GLenum intFormat
, mesa_format format
)
157 if (texObj
->Target
== GL_TEXTURE_1D_ARRAY
) {
158 /* Work around Mesa expecting the number of array slices in "height". */
163 return _mesa_prepare_mipmap_level(ctx
, texObj
, level
, width
, height
, depth
,
164 0, intFormat
, format
);
168 * Called via ctx->Driver.GenerateMipmap()
169 * Note: We don't yet support 3D textures, or texture borders.
172 _mesa_meta_GenerateMipmap(struct gl_context
*ctx
, GLenum target
,
173 struct gl_texture_object
*texObj
)
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 GLboolean use_glsl_version
= ctx
->Extensions
.ARB_vertex_shader
&&
182 ctx
->Extensions
.ARB_fragment_shader
;
187 GLboolean swizzleSaved
= GL_FALSE
;
189 if (fallback_required(ctx
, target
, texObj
)) {
190 _mesa_generate_mipmap(ctx
, target
, texObj
);
194 if (target
>= GL_TEXTURE_CUBE_MAP_POSITIVE_X
&&
195 target
<= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
) {
197 target
= GL_TEXTURE_CUBE_MAP
;
202 _mesa_meta_begin(ctx
, MESA_META_ALL
& ~MESA_META_DRAW_BUFFERS
);
204 /* Choose between glsl version and fixed function version of
205 * GenerateMipmap function.
207 if (use_glsl_version
) {
208 _mesa_meta_setup_vertex_objects(ctx
, &mipmap
->VAO
, &mipmap
->buf_obj
, true,
210 _mesa_meta_setup_blit_shader(ctx
, target
, false, &mipmap
->shaders
);
212 _mesa_meta_setup_ff_tnl_for_blit(ctx
, &mipmap
->VAO
, &mipmap
->buf_obj
, 3);
213 _mesa_set_enable(ctx
, target
, GL_TRUE
);
216 samplerSave
= ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
].Sampler
?
217 ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
].Sampler
->Name
: 0;
219 /* We may have been called from glGenerateTextureMipmap with CurrentUnit
220 * still set to 0, so we don't know when we can skip binding the texture.
221 * Assume that _mesa_BindTexture will be fast if we're rebinding the same
224 _mesa_BindTexture(target
, texObj
->Name
);
226 if (!mipmap
->Sampler
) {
227 _mesa_GenSamplers(1, &mipmap
->Sampler
);
228 _mesa_BindSampler(ctx
->Texture
.CurrentUnit
, mipmap
->Sampler
);
230 _mesa_SamplerParameteri(mipmap
->Sampler
,
231 GL_TEXTURE_MIN_FILTER
,
232 GL_LINEAR_MIPMAP_LINEAR
);
233 _mesa_SamplerParameteri(mipmap
->Sampler
, GL_TEXTURE_MAG_FILTER
, GL_LINEAR
);
234 _mesa_SamplerParameteri(mipmap
->Sampler
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
235 _mesa_SamplerParameteri(mipmap
->Sampler
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
236 _mesa_SamplerParameteri(mipmap
->Sampler
, GL_TEXTURE_WRAP_R
, GL_CLAMP_TO_EDGE
);
238 /* We don't want to encode or decode sRGB values; treat them as linear.
239 * This is not technically correct for GLES3 but we don't get any API
240 * error at the moment.
242 if (ctx
->Extensions
.EXT_texture_sRGB_decode
) {
243 _mesa_SamplerParameteri(mipmap
->Sampler
, GL_TEXTURE_SRGB_DECODE_EXT
,
247 _mesa_BindSampler(ctx
->Texture
.CurrentUnit
, mipmap
->Sampler
);
250 assert(mipmap
->FBO
!= 0);
251 _mesa_BindFramebuffer(GL_FRAMEBUFFER_EXT
, mipmap
->FBO
);
253 _mesa_TexParameteri(target
, GL_GENERATE_MIPMAP
, GL_FALSE
);
255 if (texObj
->_Swizzle
!= SWIZZLE_NOOP
) {
256 static const GLint swizzleNoop
[4] = { GL_RED
, GL_GREEN
, GL_BLUE
, GL_ALPHA
};
257 memcpy(swizzle
, texObj
->Swizzle
, sizeof(swizzle
));
258 swizzleSaved
= GL_TRUE
;
259 _mesa_TexParameteriv(target
, GL_TEXTURE_SWIZZLE_RGBA
, swizzleNoop
);
262 /* Silence valgrind warnings about reading uninitialized stack. */
263 memset(verts
, 0, sizeof(verts
));
265 /* setup vertex positions */
275 /* texture is already locked, unlock now */
276 _mesa_unlock_texture(ctx
, texObj
);
278 for (dstLevel
= baseLevel
+ 1; dstLevel
<= maxLevel
; dstLevel
++) {
279 const struct gl_texture_image
*srcImage
;
280 struct gl_texture_image
*dstImage
;
281 const GLuint srcLevel
= dstLevel
- 1;
283 GLsizei srcWidth
, srcHeight
, srcDepth
;
284 GLsizei dstWidth
, dstHeight
, dstDepth
;
286 srcImage
= _mesa_select_tex_image(texObj
, faceTarget
, srcLevel
);
287 assert(srcImage
->Border
== 0);
290 srcWidth
= srcImage
->Width
;
291 if (target
== GL_TEXTURE_1D_ARRAY
) {
293 srcDepth
= srcImage
->Height
;
295 srcHeight
= srcImage
->Height
;
296 srcDepth
= srcImage
->Depth
;
300 dstWidth
= minify(srcWidth
, 1);
301 dstHeight
= minify(srcHeight
, 1);
302 dstDepth
= target
== GL_TEXTURE_3D
? minify(srcDepth
, 1) : srcDepth
;
304 if (dstWidth
== srcWidth
&&
305 dstHeight
== srcHeight
&&
306 dstDepth
== srcDepth
) {
311 /* Allocate storage for the destination mipmap image(s) */
313 /* Set MaxLevel large enough to hold the new level when we allocate it */
314 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, dstLevel
);
316 if (!prepare_mipmap_level(ctx
, texObj
, dstLevel
,
317 dstWidth
, dstHeight
, dstDepth
,
318 srcImage
->InternalFormat
,
319 srcImage
->TexFormat
)) {
320 /* All done. We either ran out of memory or we would go beyond the
321 * last valid level of an immutable texture if we continued.
325 dstImage
= _mesa_select_tex_image(texObj
, faceTarget
, dstLevel
);
327 /* limit minification to src level */
328 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, srcLevel
);
331 _mesa_set_viewport(ctx
, 0, 0, 0, dstWidth
, dstHeight
);
332 _mesa_DrawBuffer(GL_COLOR_ATTACHMENT0
);
334 for (layer
= 0; layer
< dstDepth
; ++layer
) {
335 /* Setup texture coordinates */
336 _mesa_meta_setup_texture_coords(faceTarget
,
338 0, 0, /* xoffset, yoffset */
339 srcWidth
, srcHeight
, /* img size */
340 srcWidth
, srcHeight
, srcDepth
,
346 /* upload vertex data */
347 _mesa_buffer_data(ctx
, mipmap
->buf_obj
, GL_NONE
, sizeof(verts
), verts
,
348 GL_DYNAMIC_DRAW
, __func__
);
350 _mesa_meta_bind_fbo_image(GL_FRAMEBUFFER
, GL_COLOR_ATTACHMENT0
, dstImage
, layer
);
353 if (_mesa_CheckFramebufferStatus(GL_FRAMEBUFFER
) !=
354 GL_FRAMEBUFFER_COMPLETE
) {
355 _mesa_problem(ctx
, "Unexpected incomplete framebuffer in "
356 "_mesa_meta_GenerateMipmap()");
360 assert(dstWidth
== ctx
->DrawBuffer
->Width
);
361 if (target
== GL_TEXTURE_1D_ARRAY
) {
362 assert(dstHeight
== 1);
364 assert(dstHeight
== ctx
->DrawBuffer
->Height
);
367 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
371 _mesa_lock_texture(ctx
, texObj
); /* relock */
373 _mesa_BindSampler(ctx
->Texture
.CurrentUnit
, samplerSave
);
377 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, maxLevelSave
);
379 _mesa_TexParameteri(target
, GL_GENERATE_MIPMAP
, genMipmapSave
);
381 _mesa_TexParameteriv(target
, GL_TEXTURE_SWIZZLE_RGBA
, swizzle
);