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 /* 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.
104 _mesa_GenFramebuffers(1, &mipmap
->FBO
);
105 _mesa_BindFramebuffer(GL_FRAMEBUFFER_EXT
, mipmap
->FBO
);
107 _mesa_meta_bind_fbo_image(GL_FRAMEBUFFER
, GL_COLOR_ATTACHMENT0
, baseImage
, 0);
109 status
= _mesa_CheckFramebufferStatus(GL_FRAMEBUFFER_EXT
);
111 _mesa_BindFramebuffer(GL_FRAMEBUFFER_EXT
, fboSave
);
113 if (status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
114 _mesa_perf_debug(ctx
, MESA_DEBUG_SEVERITY_HIGH
,
115 "glGenerateMipmap() got incomplete FBO\n");
123 _mesa_meta_glsl_generate_mipmap_cleanup(struct gen_mipmap_state
*mipmap
)
125 if (mipmap
->VAO
== 0)
127 _mesa_DeleteVertexArrays(1, &mipmap
->VAO
);
129 _mesa_DeleteBuffers(1, &mipmap
->VBO
);
132 _mesa_meta_blit_shader_table_cleanup(&mipmap
->shaders
);
136 prepare_mipmap_level(struct gl_context
*ctx
,
137 struct gl_texture_object
*texObj
, GLuint level
,
138 GLsizei width
, GLsizei height
, GLsizei depth
,
139 GLenum intFormat
, mesa_format format
)
141 if (texObj
->Target
== GL_TEXTURE_1D_ARRAY
) {
142 /* Work around Mesa expecting the number of array slices in "height". */
147 return _mesa_prepare_mipmap_level(ctx
, texObj
, level
, width
, height
, depth
,
148 0, intFormat
, format
);
152 * Called via ctx->Driver.GenerateMipmap()
153 * Note: We don't yet support 3D textures, 1D/2D array textures or texture
157 _mesa_meta_GenerateMipmap(struct gl_context
*ctx
, GLenum target
,
158 struct gl_texture_object
*texObj
)
160 struct gen_mipmap_state
*mipmap
= &ctx
->Meta
->Mipmap
;
161 struct vertex verts
[4];
162 const GLuint baseLevel
= texObj
->BaseLevel
;
163 const GLuint maxLevel
= texObj
->MaxLevel
;
164 const GLint maxLevelSave
= texObj
->MaxLevel
;
165 const GLboolean genMipmapSave
= texObj
->GenerateMipmap
;
166 const GLboolean use_glsl_version
= ctx
->Extensions
.ARB_vertex_shader
&&
167 ctx
->Extensions
.ARB_fragment_shader
;
172 GLboolean swizzleSaved
= GL_FALSE
;
174 if (fallback_required(ctx
, target
, texObj
)) {
175 _mesa_generate_mipmap(ctx
, target
, texObj
);
179 if (target
>= GL_TEXTURE_CUBE_MAP_POSITIVE_X
&&
180 target
<= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
) {
182 target
= GL_TEXTURE_CUBE_MAP
;
187 _mesa_meta_begin(ctx
, MESA_META_ALL
& ~MESA_META_DRAW_BUFFERS
);
189 /* Choose between glsl version and fixed function version of
190 * GenerateMipmap function.
192 if (use_glsl_version
) {
193 _mesa_meta_setup_vertex_objects(&mipmap
->VAO
, &mipmap
->VBO
, true,
195 _mesa_meta_setup_blit_shader(ctx
, target
, false, &mipmap
->shaders
);
197 _mesa_meta_setup_ff_tnl_for_blit(&mipmap
->VAO
, &mipmap
->VBO
, 3);
198 _mesa_set_enable(ctx
, target
, GL_TRUE
);
201 samplerSave
= ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
].Sampler
?
202 ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
].Sampler
->Name
: 0;
204 /* We may have been called from glGenerateTextureMipmap with CurrentUnit
205 * still set to 0, so we don't know when we can skip binding the texture.
206 * Assume that _mesa_BindTexture will be fast if we're rebinding the same
209 _mesa_BindTexture(target
, texObj
->Name
);
211 if (!mipmap
->Sampler
) {
212 _mesa_GenSamplers(1, &mipmap
->Sampler
);
213 _mesa_BindSampler(ctx
->Texture
.CurrentUnit
, mipmap
->Sampler
);
215 _mesa_SamplerParameteri(mipmap
->Sampler
,
216 GL_TEXTURE_MIN_FILTER
,
217 GL_LINEAR_MIPMAP_LINEAR
);
218 _mesa_SamplerParameteri(mipmap
->Sampler
, GL_TEXTURE_MAG_FILTER
, GL_LINEAR
);
219 _mesa_SamplerParameteri(mipmap
->Sampler
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
220 _mesa_SamplerParameteri(mipmap
->Sampler
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
221 _mesa_SamplerParameteri(mipmap
->Sampler
, GL_TEXTURE_WRAP_R
, GL_CLAMP_TO_EDGE
);
223 /* We don't want to encode or decode sRGB values; treat them as linear.
224 * This is not technically correct for GLES3 but we don't get any API
225 * error at the moment.
227 if (ctx
->Extensions
.EXT_texture_sRGB_decode
) {
228 _mesa_SamplerParameteri(mipmap
->Sampler
, GL_TEXTURE_SRGB_DECODE_EXT
,
232 _mesa_BindSampler(ctx
->Texture
.CurrentUnit
, mipmap
->Sampler
);
235 assert(mipmap
->FBO
!= 0);
236 _mesa_BindFramebuffer(GL_FRAMEBUFFER_EXT
, mipmap
->FBO
);
238 _mesa_TexParameteri(target
, GL_GENERATE_MIPMAP
, GL_FALSE
);
240 if (texObj
->_Swizzle
!= SWIZZLE_NOOP
) {
241 static const GLint swizzleNoop
[4] = { GL_RED
, GL_GREEN
, GL_BLUE
, GL_ALPHA
};
242 memcpy(swizzle
, texObj
->Swizzle
, sizeof(swizzle
));
243 swizzleSaved
= GL_TRUE
;
244 _mesa_TexParameteriv(target
, GL_TEXTURE_SWIZZLE_RGBA
, swizzleNoop
);
247 /* Silence valgrind warnings about reading uninitialized stack. */
248 memset(verts
, 0, sizeof(verts
));
250 /* setup vertex positions */
260 /* texture is already locked, unlock now */
261 _mesa_unlock_texture(ctx
, texObj
);
263 for (dstLevel
= baseLevel
+ 1; dstLevel
<= maxLevel
; dstLevel
++) {
264 const struct gl_texture_image
*srcImage
;
265 struct gl_texture_image
*dstImage
;
266 const GLuint srcLevel
= dstLevel
- 1;
268 GLsizei srcWidth
, srcHeight
, srcDepth
;
269 GLsizei dstWidth
, dstHeight
, dstDepth
;
271 srcImage
= _mesa_select_tex_image(texObj
, faceTarget
, srcLevel
);
272 assert(srcImage
->Border
== 0);
275 srcWidth
= srcImage
->Width
;
276 if (target
== GL_TEXTURE_1D_ARRAY
) {
278 srcDepth
= srcImage
->Height
;
280 srcHeight
= srcImage
->Height
;
281 srcDepth
= srcImage
->Depth
;
285 dstWidth
= minify(srcWidth
, 1);
286 dstHeight
= minify(srcHeight
, 1);
287 dstDepth
= target
== GL_TEXTURE_3D
? minify(srcDepth
, 1) : srcDepth
;
289 if (dstWidth
== srcWidth
&&
290 dstHeight
== srcHeight
&&
291 dstDepth
== srcDepth
) {
296 /* Allocate storage for the destination mipmap image(s) */
298 /* Set MaxLevel large enough to hold the new level when we allocate it */
299 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, dstLevel
);
301 if (!prepare_mipmap_level(ctx
, texObj
, dstLevel
,
302 dstWidth
, dstHeight
, dstDepth
,
303 srcImage
->InternalFormat
,
304 srcImage
->TexFormat
)) {
305 /* All done. We either ran out of memory or we would go beyond the
306 * last valid level of an immutable texture if we continued.
310 dstImage
= _mesa_select_tex_image(texObj
, faceTarget
, dstLevel
);
312 /* limit minification to src level */
313 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, srcLevel
);
316 _mesa_set_viewport(ctx
, 0, 0, 0, dstWidth
, dstHeight
);
317 _mesa_DrawBuffer(GL_COLOR_ATTACHMENT0
);
319 for (layer
= 0; layer
< dstDepth
; ++layer
) {
320 /* Setup texture coordinates */
321 _mesa_meta_setup_texture_coords(faceTarget
,
323 0, 0, /* xoffset, yoffset */
324 srcWidth
, srcHeight
, /* img size */
325 srcWidth
, srcHeight
, srcDepth
,
331 /* upload vertex data */
332 _mesa_BufferData(GL_ARRAY_BUFFER_ARB
, sizeof(verts
),
333 verts
, GL_DYNAMIC_DRAW_ARB
);
335 _mesa_meta_bind_fbo_image(GL_FRAMEBUFFER
, GL_COLOR_ATTACHMENT0
, dstImage
, layer
);
338 if (_mesa_CheckFramebufferStatus(GL_FRAMEBUFFER
) !=
339 GL_FRAMEBUFFER_COMPLETE
) {
340 _mesa_problem(ctx
, "Unexpected incomplete framebuffer in "
341 "_mesa_meta_GenerateMipmap()");
345 assert(dstWidth
== ctx
->DrawBuffer
->Width
);
346 if (target
== GL_TEXTURE_1D_ARRAY
) {
347 assert(dstHeight
== 1);
349 assert(dstHeight
== ctx
->DrawBuffer
->Height
);
352 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
356 _mesa_lock_texture(ctx
, texObj
); /* relock */
358 _mesa_BindSampler(ctx
->Texture
.CurrentUnit
, samplerSave
);
362 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, maxLevelSave
);
364 _mesa_TexParameteri(target
, GL_GENERATE_MIPMAP
, genMipmapSave
);
366 _mesa_TexParameteriv(target
, GL_TEXTURE_SWIZZLE_RGBA
, swizzle
);