Merge branch 'mesa_7_5_branch'
[mesa.git] / src / mesa / drivers / dri / intel / intel_pixel_draw.c
1 /**************************************************************************
2 *
3 * Copyright 2006 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portionsalloc
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28 #include "main/glheader.h"
29 #include "main/enums.h"
30 #include "main/image.h"
31 #include "main/mtypes.h"
32 #include "main/macros.h"
33 #include "main/bufferobj.h"
34 #include "main/teximage.h"
35 #include "main/texenv.h"
36 #include "main/texobj.h"
37 #include "main/texstate.h"
38 #include "main/texparam.h"
39 #include "main/varray.h"
40 #include "main/attrib.h"
41 #include "main/enable.h"
42 #include "main/buffers.h"
43 #include "main/fbobject.h"
44 #include "main/renderbuffer.h"
45 #include "main/depth.h"
46 #include "main/hash.h"
47 #include "main/blend.h"
48 #include "glapi/dispatch.h"
49 #include "swrast/swrast.h"
50
51 #include "intel_screen.h"
52 #include "intel_context.h"
53 #include "intel_batchbuffer.h"
54 #include "intel_blit.h"
55 #include "intel_buffers.h"
56 #include "intel_regions.h"
57 #include "intel_pixel.h"
58 #include "intel_buffer_objects.h"
59 #include "intel_fbo.h"
60
61 static GLboolean
62 intel_texture_drawpixels(GLcontext * ctx,
63 GLint x, GLint y,
64 GLsizei width, GLsizei height,
65 GLenum format,
66 GLenum type,
67 const struct gl_pixelstore_attrib *unpack,
68 const GLvoid *pixels)
69 {
70 struct intel_context *intel = intel_context(ctx);
71 GLuint texname;
72 GLfloat vertices[4][4];
73 GLfloat z;
74 GLint old_active_texture;
75 GLenum internalFormat;
76
77 /* We're going to mess with texturing with no regard to existing texture
78 * state, so if there is some set up we have to bail.
79 */
80 if (ctx->Texture._EnabledUnits != 0) {
81 if (INTEL_DEBUG & DEBUG_FALLBACKS)
82 fprintf(stderr, "glDrawPixels() fallback: texturing enabled\n");
83 return GL_FALSE;
84 }
85
86 /* Can't do textured DrawPixels with a fragment program, unless we were
87 * to generate a new program that sampled our texture and put the results
88 * in the fragment color before the user's program started.
89 */
90 if (ctx->FragmentProgram.Enabled) {
91 if (INTEL_DEBUG & DEBUG_FALLBACKS)
92 fprintf(stderr, "glDrawPixels() fallback: fragment program enabled\n");
93 return GL_FALSE;
94 }
95
96 /* We don't have a way to generate fragments with stencil values which
97 * will set the resulting stencil value.
98 */
99 if (format == GL_STENCIL_INDEX || format == GL_DEPTH_STENCIL)
100 return GL_FALSE;
101
102 /* Check that we can load in a texture this big. */
103 if (width > (1 << (ctx->Const.MaxTextureLevels - 1)) ||
104 height > (1 << (ctx->Const.MaxTextureLevels - 1))) {
105 if (INTEL_DEBUG & DEBUG_FALLBACKS)
106 fprintf(stderr, "glDrawPixels() fallback: bitmap too large (%dx%d)\n",
107 width, height);
108 return GL_FALSE;
109 }
110
111 /* To do DEPTH_COMPONENT, we would need to change our setup to not draw to
112 * the color buffer, and sample the texture values into the fragment depth
113 * in a program.
114 */
115 if (format == GL_DEPTH_COMPONENT) {
116 if (INTEL_DEBUG & DEBUG_FALLBACKS)
117 fprintf(stderr,
118 "glDrawPixels() fallback: format == GL_DEPTH_COMPONENT\n");
119 return GL_FALSE;
120 }
121
122 if (!ctx->Extensions.ARB_texture_non_power_of_two &&
123 (!is_power_of_two(width) || !is_power_of_two(height))) {
124 if (INTEL_DEBUG & DEBUG_FALLBACKS)
125 fprintf(stderr,
126 "glDrawPixels() fallback: NPOT texture\n");
127 return GL_FALSE;
128 }
129
130 _mesa_PushAttrib(GL_ENABLE_BIT | GL_TEXTURE_BIT |
131 GL_CURRENT_BIT);
132 _mesa_PushClientAttrib(GL_CLIENT_VERTEX_ARRAY_BIT);
133
134 /* XXX: pixel store stuff */
135 _mesa_Disable(GL_POLYGON_STIPPLE);
136
137 old_active_texture = ctx->Texture.CurrentUnit;
138 _mesa_ActiveTextureARB(GL_TEXTURE0_ARB);
139 _mesa_Enable(GL_TEXTURE_2D);
140 _mesa_GenTextures(1, &texname);
141 _mesa_BindTexture(GL_TEXTURE_2D, texname);
142 _mesa_TexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
143 _mesa_TexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
144 _mesa_TexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
145 if (type == GL_ALPHA)
146 internalFormat = GL_ALPHA;
147 else
148 internalFormat = GL_RGBA;
149 _mesa_TexImage2D(GL_TEXTURE_2D, 0, internalFormat, width, height, 0, format,
150 type, pixels);
151
152 intel_meta_set_passthrough_transform(intel);
153
154 /* convert rasterpos Z from [0,1] to NDC coord in [-1,1] */
155 z = -1.0 + 2.0 * ctx->Current.RasterPos[2];
156
157 /* Create the vertex buffer based on the current raster pos. The x and y
158 * we're handed are ctx->Current.RasterPos[0,1] rounded to integers.
159 * We also apply the depth. However, the W component is already multiplied
160 * into ctx->Current.RasterPos[0,1,2] and we can ignore it at this point.
161 */
162 vertices[0][0] = x;
163 vertices[0][1] = y;
164 vertices[0][2] = z;
165 vertices[0][3] = 1.0;
166 vertices[1][0] = x + width * ctx->Pixel.ZoomX;
167 vertices[1][1] = y;
168 vertices[1][2] = z;
169 vertices[1][3] = 1.0;
170 vertices[2][0] = x + width * ctx->Pixel.ZoomX;
171 vertices[2][1] = y + height * ctx->Pixel.ZoomY;
172 vertices[2][2] = z;
173 vertices[2][3] = 1.0;
174 vertices[3][0] = x;
175 vertices[3][1] = y + height * ctx->Pixel.ZoomY;
176 vertices[3][2] = z;
177 vertices[3][3] = 1.0;
178
179 _mesa_VertexPointer(4, GL_FLOAT, 4 * sizeof(GLfloat), &vertices);
180 _mesa_Enable(GL_VERTEX_ARRAY);
181 intel_meta_set_default_texrect(intel);
182
183 CALL_DrawArrays(ctx->Exec, (GL_TRIANGLE_FAN, 0, 4));
184
185 intel_meta_restore_texcoords(intel);
186 intel_meta_restore_transform(intel);
187
188 _mesa_ActiveTextureARB(GL_TEXTURE0_ARB + old_active_texture);
189 _mesa_PopClientAttrib();
190 _mesa_PopAttrib();
191
192 _mesa_DeleteTextures(1, &texname);
193
194 return GL_TRUE;
195 }
196
197 static GLboolean
198 intel_stencil_drawpixels(GLcontext * ctx,
199 GLint x, GLint y,
200 GLsizei width, GLsizei height,
201 GLenum format,
202 GLenum type,
203 const struct gl_pixelstore_attrib *unpack,
204 const GLvoid *pixels)
205 {
206 struct intel_context *intel = intel_context(ctx);
207 GLuint texname, rb_name, fb_name, old_fb_name;
208 GLfloat vertices[4][2];
209 struct intel_renderbuffer *irb;
210 struct intel_renderbuffer *depth_irb;
211 struct gl_renderbuffer *rb;
212 struct gl_pixelstore_attrib old_unpack;
213 GLstencil *stencil_pixels;
214 int row;
215 GLint old_active_texture;
216
217 if (format != GL_STENCIL_INDEX)
218 return GL_FALSE;
219
220 /* If there's nothing to write, we're done. */
221 if (ctx->Stencil.WriteMask[0] == 0)
222 return GL_TRUE;
223
224 /* Can't do a per-bit writemask while treating stencil as rgba data. */
225 if ((ctx->Stencil.WriteMask[0] & 0xff) != 0xff) {
226 if (INTEL_DEBUG & DEBUG_FALLBACKS)
227 fprintf(stderr, "glDrawPixels(STENCIL_INDEX) fallback: "
228 "stencil mask enabled\n");
229 return GL_FALSE;
230 }
231
232 /* We don't support stencil testing/ops here */
233 if (ctx->Stencil._Enabled)
234 return GL_FALSE;
235
236 /* We use FBOs for our wrapping of the depthbuffer into a color
237 * destination.
238 */
239 if (!ctx->Extensions.EXT_framebuffer_object)
240 return GL_FALSE;
241
242 /* We're going to mess with texturing with no regard to existing texture
243 * state, so if there is some set up we have to bail.
244 */
245 if (ctx->Texture._EnabledUnits != 0) {
246 if (INTEL_DEBUG & DEBUG_FALLBACKS)
247 fprintf(stderr, "glDrawPixels(STENCIL_INDEX) fallback: "
248 "texturing enabled\n");
249 return GL_FALSE;
250 }
251
252 /* Can't do textured DrawPixels with a fragment program, unless we were
253 * to generate a new program that sampled our texture and put the results
254 * in the fragment color before the user's program started.
255 */
256 if (ctx->FragmentProgram.Enabled) {
257 if (INTEL_DEBUG & DEBUG_FALLBACKS)
258 fprintf(stderr, "glDrawPixels(STENCIL_INDEX) fallback: "
259 "fragment program enabled\n");
260 return GL_FALSE;
261 }
262
263 /* Check that we can load in a texture this big. */
264 if (width > (1 << (ctx->Const.MaxTextureLevels - 1)) ||
265 height > (1 << (ctx->Const.MaxTextureLevels - 1))) {
266 if (INTEL_DEBUG & DEBUG_FALLBACKS)
267 fprintf(stderr, "glDrawPixels(STENCIL_INDEX) fallback: "
268 "bitmap too large (%dx%d)\n",
269 width, height);
270 return GL_FALSE;
271 }
272
273 if (!ctx->Extensions.ARB_texture_non_power_of_two &&
274 (!is_power_of_two(width) || !is_power_of_two(height))) {
275 if (INTEL_DEBUG & DEBUG_FALLBACKS)
276 fprintf(stderr,
277 "glDrawPixels(GL_STENCIL_INDEX) fallback: NPOT texture\n");
278 return GL_FALSE;
279 }
280
281 _mesa_PushAttrib(GL_ENABLE_BIT | GL_TEXTURE_BIT |
282 GL_CURRENT_BIT | GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
283 _mesa_PushClientAttrib(GL_CLIENT_VERTEX_ARRAY_BIT);
284 old_fb_name = ctx->DrawBuffer->Name;
285 old_active_texture = ctx->Texture.CurrentUnit;
286
287 _mesa_Disable(GL_POLYGON_STIPPLE);
288 _mesa_Disable(GL_DEPTH_TEST);
289 _mesa_Disable(GL_STENCIL_TEST);
290
291 /* Unpack the supplied stencil values into a ubyte buffer. */
292 assert(sizeof(GLstencil) == sizeof(GLubyte));
293 stencil_pixels = _mesa_malloc(width * height * sizeof(GLstencil));
294 for (row = 0; row < height; row++) {
295 GLvoid *source = _mesa_image_address2d(unpack, pixels,
296 width, height,
297 GL_COLOR_INDEX, type,
298 row, 0);
299 _mesa_unpack_stencil_span(ctx, width, GL_UNSIGNED_BYTE,
300 stencil_pixels +
301 row * width * sizeof(GLstencil),
302 type, source, unpack, ctx->_ImageTransferState);
303 }
304
305 /* Take the current depth/stencil renderbuffer, and make a new one wrapping
306 * it which will be treated as GL_RGBA8 so we can render to it as a color
307 * buffer.
308 */
309 depth_irb = intel_get_renderbuffer(ctx->DrawBuffer, BUFFER_DEPTH);
310 irb = intel_create_renderbuffer(GL_RGBA8);
311 rb = &irb->Base;
312 irb->Base.Width = depth_irb->Base.Width;
313 irb->Base.Height = depth_irb->Base.Height;
314 intel_renderbuffer_set_region(irb, depth_irb->region);
315
316 /* Create a name for our renderbuffer, which lets us use other mesa
317 * rb functions for convenience.
318 */
319 _mesa_GenRenderbuffersEXT(1, &rb_name);
320 irb->Base.RefCount++;
321 _mesa_HashInsert(ctx->Shared->RenderBuffers, rb_name, &irb->Base);
322
323 /* Bind the new renderbuffer to the color attachment point. */
324 _mesa_GenFramebuffersEXT(1, &fb_name);
325 _mesa_BindFramebufferEXT(GL_FRAMEBUFFER_EXT, fb_name);
326 _mesa_FramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT,
327 GL_COLOR_ATTACHMENT0_EXT,
328 GL_RENDERBUFFER_EXT,
329 rb_name);
330 /* Choose to render to the color attachment. */
331 _mesa_DrawBuffer(GL_COLOR_ATTACHMENT0_EXT);
332
333 _mesa_DepthMask(GL_FALSE);
334 _mesa_ColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_TRUE);
335
336 _mesa_ActiveTextureARB(GL_TEXTURE0_ARB);
337 _mesa_Enable(GL_TEXTURE_2D);
338 _mesa_GenTextures(1, &texname);
339 _mesa_BindTexture(GL_TEXTURE_2D, texname);
340 _mesa_TexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
341 _mesa_TexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
342 _mesa_TexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
343 old_unpack = ctx->Unpack;
344 ctx->Unpack = ctx->DefaultPacking;
345 _mesa_TexImage2D(GL_TEXTURE_2D, 0, GL_INTENSITY, width, height, 0,
346 GL_RED, GL_UNSIGNED_BYTE, stencil_pixels);
347 ctx->Unpack = old_unpack;
348 _mesa_free(stencil_pixels);
349
350 intel_meta_set_passthrough_transform(intel);
351 vertices[0][0] = x;
352 vertices[0][1] = y;
353 vertices[1][0] = x + width * ctx->Pixel.ZoomX;
354 vertices[1][1] = y;
355 vertices[2][0] = x + width * ctx->Pixel.ZoomX;
356 vertices[2][1] = y + height * ctx->Pixel.ZoomY;
357 vertices[3][0] = x;
358 vertices[3][1] = y + height * ctx->Pixel.ZoomY;
359
360 _mesa_VertexPointer(2, GL_FLOAT, 2 * sizeof(GLfloat), &vertices);
361 _mesa_Enable(GL_VERTEX_ARRAY);
362 intel_meta_set_default_texrect(intel);
363
364 CALL_DrawArrays(ctx->Exec, (GL_TRIANGLE_FAN, 0, 4));
365
366 intel_meta_restore_texcoords(intel);
367 intel_meta_restore_transform(intel);
368
369 _mesa_ActiveTextureARB(GL_TEXTURE0_ARB + old_active_texture);
370 _mesa_BindFramebufferEXT(GL_FRAMEBUFFER_EXT, old_fb_name);
371
372 _mesa_PopClientAttrib();
373 _mesa_PopAttrib();
374
375 _mesa_DeleteTextures(1, &texname);
376 _mesa_DeleteFramebuffersEXT(1, &fb_name);
377 _mesa_DeleteRenderbuffersEXT(1, &rb_name);
378
379 return GL_TRUE;
380 }
381
382 void
383 intelDrawPixels(GLcontext * ctx,
384 GLint x, GLint y,
385 GLsizei width, GLsizei height,
386 GLenum format,
387 GLenum type,
388 const struct gl_pixelstore_attrib *unpack,
389 const GLvoid * pixels)
390 {
391 if (intel_texture_drawpixels(ctx, x, y, width, height, format, type,
392 unpack, pixels))
393 return;
394
395 if (intel_stencil_drawpixels(ctx, x, y, width, height, format, type,
396 unpack, pixels))
397 return;
398
399 if (INTEL_DEBUG & DEBUG_PIXEL)
400 _mesa_printf("%s: fallback to swrast\n", __FUNCTION__);
401
402 _swrast_DrawPixels(ctx, x, y, width, height, format, type,
403 unpack, pixels);
404 }