mesa: move generate mipmap calls
[mesa.git] / src / mesa / drivers / dri / intel / intel_generatemipmap.c
1 /*
2 * Copyright (C) 1999-2007 Brian Paul All Rights Reserved.
3 * Copyright © 2009 Intel Corporation
4 *
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:
11 *
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
14 * Software.
15 *
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
22 * IN THE SOFTWARE.
23 *
24 * Authors:
25 * Eric Anholt <eric@anholt.net>
26 *
27 */
28
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"
53
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"
60
61 static const char *intel_fp_tex2d =
62 "!!ARBfp1.0\n"
63 "TEX result.color, fragment.texcoord[0], texture[0], 2D;\n"
64 "END\n";
65
66 static GLboolean
67 intel_generate_mipmap_level(GLcontext *ctx, GLuint tex_name,
68 int level, int width, int height)
69 {
70 struct intel_context *intel = intel_context(ctx);
71 GLfloat vertices[4][2];
72 GLint status;
73
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);
77
78 /* Set to draw into the current level */
79 _mesa_FramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT,
80 GL_COLOR_ATTACHMENT0_EXT,
81 GL_TEXTURE_2D,
82 tex_name,
83 level);
84 /* Choose to render to the color attachment. */
85 _mesa_DrawBuffer(GL_COLOR_ATTACHMENT0_EXT);
86
87 status = _mesa_CheckFramebufferStatusEXT (GL_FRAMEBUFFER_EXT);
88 if (status != GL_FRAMEBUFFER_COMPLETE_EXT)
89 return GL_FALSE;
90
91 meta_set_passthrough_transform(&intel->meta);
92
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.
95 */
96 vertices[0][0] = 0;
97 vertices[0][1] = 0;
98 vertices[1][0] = width;
99 vertices[1][1] = 0;
100 vertices[2][0] = width;
101 vertices[2][1] = height;
102 vertices[3][0] = 0;
103 vertices[3][1] = height;
104
105 _mesa_VertexPointer(2, GL_FLOAT, 2 * sizeof(GLfloat), &vertices);
106 _mesa_Enable(GL_VERTEX_ARRAY);
107 meta_set_default_texrect(&intel->meta);
108
109 _mesa_DrawArrays(GL_TRIANGLE_FAN, 0, 4);
110
111 meta_restore_texcoords(&intel->meta);
112 meta_restore_transform(&intel->meta);
113
114 return GL_TRUE;
115 }
116
117 static GLboolean
118 intel_generate_mipmap_2d(GLcontext *ctx,
119 GLenum target,
120 struct gl_texture_object *texObj)
121 {
122 struct intel_context *intel = intel_context(ctx);
123 GLint old_active_texture;
124 int level, max_levels, start_level, end_level;
125 GLuint fb_name;
126 GLboolean success = GL_FALSE;
127 struct gl_framebuffer *saved_fbo = NULL;
128
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);
135
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);
141
142 /* Bind the given texture to GL_TEXTURE_2D with linear filtering for our
143 * minification.
144 */
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);
151
152 /* Bind the new renderbuffer to the color attachment point. */
153 _mesa_GenFramebuffersEXT(1, &fb_name);
154 _mesa_BindFramebufferEXT(GL_FRAMEBUFFER_EXT, fb_name);
155
156 meta_set_fragment_program(&intel->meta, &intel->meta.tex2d_fp,
157 intel_fp_tex2d);
158 meta_set_passthrough_vertex_program(&intel->meta);
159
160 max_levels = _mesa_max_texture_levels(ctx, texObj->Target);
161 start_level = texObj->BaseLevel;
162 end_level = texObj->MaxLevel;
163
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;
167 int width, height;
168
169 srcImage = _mesa_select_tex_image(ctx, texObj, target, level);
170 if (srcImage->Border != 0)
171 goto fail;
172
173 width = srcImage->Width / 2;
174 if (width < 1)
175 width = 1;
176 height = srcImage->Height / 2;
177 if (height < 1)
178 height = 1;
179
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
184 * over the edge.
185 */
186 break;
187 }
188
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
191 * time.
192 */
193 _mesa_TexImage2D(GL_TEXTURE_2D, level + 1, srcImage->InternalFormat,
194 width, height, 0,
195 GL_RGBA, GL_UNSIGNED_INT, NULL);
196
197 if (!intel_generate_mipmap_level(ctx, texObj->Name, level + 1,
198 width, height))
199 goto fail;
200 }
201
202 success = GL_TRUE;
203
204 fail:
205 meta_restore_fragment_program(&intel->meta);
206 meta_restore_vertex_program(&intel->meta);
207
208 _mesa_DeleteFramebuffersEXT(1, &fb_name);
209 _mesa_ActiveTextureARB(GL_TEXTURE0_ARB + old_active_texture);
210 if (saved_fbo)
211 _mesa_BindFramebufferEXT(GL_FRAMEBUFFER_EXT, saved_fbo->Name);
212 _mesa_reference_framebuffer(&saved_fbo, NULL);
213 _mesa_PopClientAttrib();
214 _mesa_PopAttrib();
215
216 return success;
217 }
218
219
220 /**
221 * Generate new mipmap data from BASE+1 to BASE+p (the minimally-sized mipmap
222 * level).
223 *
224 * The texture object's miptree must be mapped.
225 *
226 * This function should also include an accelerated path.
227 */
228 void
229 intel_generate_mipmap(GLcontext *ctx, GLenum target,
230 struct gl_texture_object *texObj)
231 {
232 struct intel_context *intel = intel_context(ctx);
233 struct intel_texture_object *intelObj = intel_texture_object(texObj);
234 GLuint nr_faces = (intelObj->base.Target == GL_TEXTURE_CUBE_MAP) ? 6 : 1;
235 int face, i;
236
237 /* HW path */
238 if (target == GL_TEXTURE_2D &&
239 ctx->Extensions.EXT_framebuffer_object &&
240 ctx->Extensions.ARB_fragment_program &&
241 ctx->Extensions.ARB_vertex_program) {
242 GLboolean success;
243
244 /* We'll be accessing this texture using GL entrypoints, which should
245 * be resilient against other access to this texture.
246 */
247 _mesa_unlock_texture(ctx, texObj);
248 success = intel_generate_mipmap_2d(ctx, target, texObj);
249 _mesa_lock_texture(ctx, texObj);
250
251 if (success)
252 return;
253 }
254
255 /* SW path */
256 intel_tex_map_level_images(intel, intelObj, texObj->BaseLevel);
257 _mesa_generate_mipmap(ctx, target, texObj);
258 intel_tex_unmap_level_images(intel, intelObj, texObj->BaseLevel);
259
260 /* Update the level information in our private data in the new images, since
261 * it didn't get set as part of a normal TexImage path.
262 */
263 for (face = 0; face < nr_faces; face++) {
264 for (i = texObj->BaseLevel + 1; i < texObj->MaxLevel; i++) {
265 struct intel_texture_image *intelImage;
266
267 intelImage = intel_texture_image(texObj->Image[face][i]);
268 if (intelImage == NULL)
269 break;
270
271 intelImage->level = i;
272 intelImage->face = face;
273 /* Unreference the miptree to signal that the new Data is a bare
274 * pointer from mesa.
275 */
276 intel_miptree_release(intel, &intelImage->mt);
277 }
278 }
279 }