2 * Copyright (C) 1999-2007 Brian Paul All Rights Reserved.
3 * Copyright © 2009 Intel Corporation
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * 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 OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
25 * Eric Anholt <eric@anholt.net>
29 #include "main/glheader.h"
30 #include "main/enums.h"
31 #include "main/image.h"
32 #include "main/mtypes.h"
33 #include "main/macros.h"
34 #include "main/bufferobj.h"
35 #include "main/teximage.h"
36 #include "main/texenv.h"
37 #include "main/texobj.h"
38 #include "main/texstate.h"
39 #include "main/texparam.h"
40 #include "main/varray.h"
41 #include "main/attrib.h"
42 #include "main/enable.h"
43 #include "main/buffers.h"
44 #include "main/fbobject.h"
45 #include "main/framebuffer.h"
46 #include "main/renderbuffer.h"
47 #include "main/depth.h"
48 #include "main/hash.h"
49 #include "main/mipmap.h"
50 #include "main/blend.h"
51 #include "glapi/dispatch.h"
52 #include "swrast/swrast.h"
54 #include "intel_screen.h"
55 #include "intel_context.h"
56 #include "intel_batchbuffer.h"
57 #include "intel_pixel.h"
58 #include "intel_tex.h"
59 #include "intel_mipmap_tree.h"
61 static const char *intel_fp_tex2d
=
63 "TEX result.color, fragment.texcoord[0], texture[0], 2D;\n"
67 intel_generate_mipmap_level(GLcontext
*ctx
, GLuint tex_name
,
68 int level
, int width
, int height
)
70 struct intel_context
*intel
= intel_context(ctx
);
71 GLfloat vertices
[4][2];
74 /* Set to source from the previous level */
75 _mesa_TexParameterf(GL_TEXTURE_2D
, GL_TEXTURE_BASE_LEVEL
, level
- 1);
76 _mesa_TexParameterf(GL_TEXTURE_2D
, GL_TEXTURE_MAX_LEVEL
, level
- 1);
78 /* Set to draw into the current level */
79 _mesa_FramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT
,
80 GL_COLOR_ATTACHMENT0_EXT
,
84 /* Choose to render to the color attachment. */
85 _mesa_DrawBuffer(GL_COLOR_ATTACHMENT0_EXT
);
87 status
= glCheckFramebufferStatusEXT (GL_FRAMEBUFFER_EXT
);
88 if (status
!= GL_FRAMEBUFFER_COMPLETE_EXT
)
91 intel_meta_set_passthrough_transform(intel
);
93 /* XXX: Doing it right would involve setting up the transformation to do
94 * 0-1 mapping or something, and not changing the vertex data.
98 vertices
[1][0] = width
;
100 vertices
[2][0] = width
;
101 vertices
[2][1] = height
;
103 vertices
[3][1] = height
;
105 _mesa_VertexPointer(2, GL_FLOAT
, 2 * sizeof(GLfloat
), &vertices
);
106 _mesa_Enable(GL_VERTEX_ARRAY
);
107 intel_meta_set_default_texrect(intel
);
109 CALL_DrawArrays(ctx
->Exec
, (GL_TRIANGLE_FAN
, 0, 4));
111 intel_meta_restore_texcoords(intel
);
112 intel_meta_restore_transform(intel
);
118 intel_generate_mipmap_2d(GLcontext
*ctx
,
120 struct gl_texture_object
*texObj
)
122 struct intel_context
*intel
= intel_context(ctx
);
123 GLint old_active_texture
;
124 int level
, max_levels
, start_level
, end_level
;
126 GLboolean success
= GL_FALSE
;
127 struct gl_framebuffer
*saved_fbo
= NULL
;
129 _mesa_PushAttrib(GL_ENABLE_BIT
| GL_TEXTURE_BIT
|
130 GL_CURRENT_BIT
| GL_COLOR_BUFFER_BIT
|
131 GL_DEPTH_BUFFER_BIT
);
132 _mesa_PushClientAttrib(GL_CLIENT_VERTEX_ARRAY_BIT
);
133 old_active_texture
= ctx
->Texture
.CurrentUnit
;
134 _mesa_reference_framebuffer(&saved_fbo
, ctx
->DrawBuffer
);
136 _mesa_Disable(GL_POLYGON_STIPPLE
);
137 _mesa_Disable(GL_DEPTH_TEST
);
138 _mesa_Disable(GL_STENCIL_TEST
);
139 _mesa_ColorMask(GL_TRUE
, GL_TRUE
, GL_TRUE
, GL_TRUE
);
140 _mesa_DepthMask(GL_FALSE
);
142 /* Bind the given texture to GL_TEXTURE_2D with linear filtering for our
145 _mesa_ActiveTextureARB(GL_TEXTURE0_ARB
);
146 _mesa_Enable(GL_TEXTURE_2D
);
147 _mesa_BindTexture(GL_TEXTURE_2D
, texObj
->Name
);
148 _mesa_TexParameterf(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
,
149 GL_LINEAR_MIPMAP_NEAREST
);
150 _mesa_TexParameterf(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_LINEAR
);
152 /* Bind the new renderbuffer to the color attachment point. */
153 _mesa_GenFramebuffersEXT(1, &fb_name
);
154 _mesa_BindFramebufferEXT(GL_FRAMEBUFFER_EXT
, fb_name
);
156 intel_meta_set_fragment_program(intel
, &intel
->meta
.tex2d_fp
,
158 intel_meta_set_passthrough_vertex_program(intel
);
160 max_levels
= _mesa_max_texture_levels(ctx
, texObj
->Target
);
161 start_level
= texObj
->BaseLevel
;
162 end_level
= texObj
->MaxLevel
;
164 /* Loop generating level+1 from level. */
165 for (level
= start_level
; level
< end_level
&& level
< max_levels
- 1; level
++) {
166 const struct gl_texture_image
*srcImage
;
169 srcImage
= _mesa_select_tex_image(ctx
, texObj
, target
, level
);
170 if (srcImage
->Border
!= 0)
173 width
= srcImage
->Width
/ 2;
176 height
= srcImage
->Height
/ 2;
180 if (width
== srcImage
->Width
&&
181 height
== srcImage
->Height
) {
182 /* Neither _mesa_max_texture_levels nor texObj->MaxLevel are the
183 * maximum texture level for the object, so break out when we've gone
189 /* Make sure that there's space allocated for the target level.
190 * We could skip this if there's already space allocated and save some
193 _mesa_TexImage2D(GL_TEXTURE_2D
, level
+ 1, srcImage
->InternalFormat
,
195 GL_RGBA
, GL_UNSIGNED_INT
, NULL
);
197 if (!intel_generate_mipmap_level(ctx
, texObj
->Name
, level
+ 1,
205 intel_meta_restore_fragment_program(intel
);
206 intel_meta_restore_vertex_program(intel
);
208 _mesa_DeleteFramebuffersEXT(1, &fb_name
);
209 _mesa_ActiveTextureARB(GL_TEXTURE0_ARB
+ old_active_texture
);
211 _mesa_BindFramebufferEXT(GL_FRAMEBUFFER_EXT
, saved_fbo
->Name
);
212 _mesa_reference_framebuffer(&saved_fbo
, NULL
);
213 _mesa_PopClientAttrib();
221 * Generate new mipmap data from BASE+1 to BASE+p (the minimally-sized mipmap
224 * The texture object's miptree must be mapped.
226 * It would be really nice if this was just called by Mesa whenever mipmaps
227 * needed to be regenerated, rather than us having to remember to do so in
228 * each texture image modification path.
230 * This function should also include an accelerated path.
233 intel_generate_mipmap(GLcontext
*ctx
, GLenum target
,
234 struct gl_texture_object
*texObj
)
236 struct intel_context
*intel
= intel_context(ctx
);
237 struct intel_texture_object
*intelObj
= intel_texture_object(texObj
);
238 GLuint nr_faces
= (intelObj
->base
.Target
== GL_TEXTURE_CUBE_MAP
) ? 6 : 1;
242 if (target
== GL_TEXTURE_2D
&&
243 ctx
->Extensions
.EXT_framebuffer_object
&&
244 ctx
->Extensions
.ARB_fragment_program
&&
245 ctx
->Extensions
.ARB_vertex_program
) {
248 /* We'll be accessing this texture using GL entrypoints, which should
249 * be resilient against other access to this texture.
251 _mesa_unlock_texture(ctx
, texObj
);
252 success
= intel_generate_mipmap_2d(ctx
, target
, texObj
);
253 _mesa_lock_texture(ctx
, texObj
);
260 intel_tex_map_level_images(intel
, intelObj
, texObj
->BaseLevel
);
261 _mesa_generate_mipmap(ctx
, target
, texObj
);
262 intel_tex_unmap_level_images(intel
, intelObj
, texObj
->BaseLevel
);
264 /* Update the level information in our private data in the new images, since
265 * it didn't get set as part of a normal TexImage path.
267 for (face
= 0; face
< nr_faces
; face
++) {
268 for (i
= texObj
->BaseLevel
+ 1; i
< texObj
->MaxLevel
; i
++) {
269 struct intel_texture_image
*intelImage
;
271 intelImage
= intel_texture_image(texObj
->Image
[face
][i
]);
272 if (intelImage
== NULL
)
275 intelImage
->level
= i
;
276 intelImage
->face
= face
;
277 /* Unreference the miptree to signal that the new Data is a bare
280 intel_miptree_release(intel
, &intelImage
->mt
);