Merge commit 'origin/gallium-0.1' into gallium-0.2
[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/matrix.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/renderbuffer.h"
46 #include "main/depth.h"
47 #include "main/hash.h"
48 #include "main/blend.h"
49 #include "glapi/dispatch.h"
50 #include "swrast/swrast.h"
51
52 #include "intel_screen.h"
53 #include "intel_context.h"
54 #include "intel_batchbuffer.h"
55 #include "intel_blit.h"
56 #include "intel_buffers.h"
57 #include "intel_regions.h"
58 #include "intel_pixel.h"
59 #include "intel_buffer_objects.h"
60 #include "intel_fbo.h"
61
62 static GLboolean
63 intel_texture_drawpixels(GLcontext * ctx,
64 GLint x, GLint y,
65 GLsizei width, GLsizei height,
66 GLenum format,
67 GLenum type,
68 const struct gl_pixelstore_attrib *unpack,
69 const GLvoid *pixels)
70 {
71 GLuint texname;
72 GLfloat vertices[4][4];
73 GLfloat texcoords[4][2];
74
75 /* We're going to mess with texturing with no regard to existing texture
76 * state, so if there is some set up we have to bail.
77 */
78 if (ctx->Texture._EnabledUnits != 0)
79 return GL_FALSE;
80
81 /* Can't do textured DrawPixels with a fragment program, unless we were
82 * to generate a new program that sampled our texture and put the results
83 * in the fragment color before the user's program started.
84 */
85 if (ctx->FragmentProgram.Enabled)
86 return GL_FALSE;
87
88 /* Don't even want to think about it */
89 if (format == GL_COLOR_INDEX)
90 return GL_FALSE;
91
92 /* We don't have a way to generate fragments with stencil values which *
93 * will set the resulting stencil value.
94 */
95 if (format == GL_STENCIL_INDEX)
96 return GL_FALSE;
97
98 /* Check that we can load in a texture this big. */
99 if (width > (1 << (ctx->Const.MaxTextureLevels - 1)) ||
100 height > (1 << (ctx->Const.MaxTextureLevels - 1))) {
101 if (INTEL_DEBUG & DEBUG_FALLBACKS)
102 fprintf(stderr, "glDrawPixels() fallback: bitmap too large (%dx%d)\n",
103 width, height);
104 return GL_FALSE;
105 }
106
107 /* To do DEPTH_COMPONENT, we would need to change our setup to not draw to
108 * the color buffer, and sample the texture values into the fragment depth
109 * in a program.
110 */
111 if (format == GL_DEPTH_COMPONENT)
112 return GL_FALSE;
113
114 _mesa_PushAttrib(GL_ENABLE_BIT | GL_TRANSFORM_BIT | GL_TEXTURE_BIT |
115 GL_CURRENT_BIT);
116 _mesa_PushClientAttrib(GL_CLIENT_VERTEX_ARRAY_BIT);
117
118 /* XXX: pixel store stuff */
119 _mesa_Disable(GL_POLYGON_STIPPLE);
120
121 _mesa_ActiveTextureARB(GL_TEXTURE0_ARB);
122 _mesa_Enable(GL_TEXTURE_2D);
123 _mesa_GenTextures(1, &texname);
124 _mesa_BindTexture(GL_TEXTURE_2D, texname);
125 _mesa_TexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
126 _mesa_TexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
127 _mesa_TexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
128 /*
129 _mesa_TexEnvf(GL_TEXTURE_ENV, GL_COMBINE_RGB, GL_REPLACE);
130 _mesa_TexEnvf(GL_TEXTURE_ENV, GL_COMBINE_ALPHA, GL_REPLACE);
131 */
132 _mesa_TexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, format,
133 type, pixels);
134
135 _mesa_MatrixMode(GL_PROJECTION);
136 _mesa_PushMatrix();
137 _mesa_LoadIdentity();
138 _mesa_Ortho(0, ctx->DrawBuffer->Width, 0, ctx->DrawBuffer->Height, 1, -1);
139
140 _mesa_MatrixMode(GL_MODELVIEW);
141 _mesa_PushMatrix();
142 _mesa_LoadIdentity();
143
144 vertices[0][0] = x;
145 vertices[0][1] = y;
146 vertices[0][2] = ctx->Current.RasterPos[2];
147 vertices[0][3] = ctx->Current.RasterPos[3];
148 vertices[1][0] = x + width * ctx->Pixel.ZoomX;
149 vertices[1][1] = y;
150 vertices[1][2] = ctx->Current.RasterPos[2];
151 vertices[1][3] = ctx->Current.RasterPos[3];
152 vertices[2][0] = x + width * ctx->Pixel.ZoomX;
153 vertices[2][1] = y + height * ctx->Pixel.ZoomY;
154 vertices[2][2] = ctx->Current.RasterPos[2];
155 vertices[2][3] = ctx->Current.RasterPos[3];
156 vertices[3][0] = x;
157 vertices[3][1] = y + height * ctx->Pixel.ZoomY;
158 vertices[3][2] = ctx->Current.RasterPos[2];
159 vertices[3][3] = ctx->Current.RasterPos[3];
160
161 texcoords[0][0] = 0.0;
162 texcoords[0][1] = 0.0;
163 texcoords[1][0] = 1.0;
164 texcoords[1][1] = 0.0;
165 texcoords[2][0] = 1.0;
166 texcoords[2][1] = 1.0;
167 texcoords[3][0] = 0.0;
168 texcoords[3][1] = 1.0;
169
170 _mesa_VertexPointer(4, GL_FLOAT, 4 * sizeof(GLfloat), &vertices);
171 _mesa_TexCoordPointer(2, GL_FLOAT, 2 * sizeof(GLfloat), &texcoords);
172 _mesa_Enable(GL_VERTEX_ARRAY);
173 _mesa_Enable(GL_TEXTURE_COORD_ARRAY);
174 CALL_DrawArrays(ctx->Exec, (GL_TRIANGLE_FAN, 0, 4));
175
176 _mesa_MatrixMode(GL_PROJECTION);
177 _mesa_PopMatrix();
178 _mesa_MatrixMode(GL_MODELVIEW);
179 _mesa_PopMatrix();
180 _mesa_PopClientAttrib();
181 _mesa_PopAttrib();
182
183 _mesa_DeleteTextures(1, &texname);
184
185 return GL_TRUE;
186 }
187
188 static GLboolean
189 intel_stencil_drawpixels(GLcontext * ctx,
190 GLint x, GLint y,
191 GLsizei width, GLsizei height,
192 GLenum format,
193 GLenum type,
194 const struct gl_pixelstore_attrib *unpack,
195 const GLvoid *pixels)
196 {
197 GLuint texname, rb_name, fb_name, old_fb_name;
198 GLfloat vertices[4][2];
199 GLfloat texcoords[4][2];
200 struct intel_renderbuffer *irb;
201 struct intel_renderbuffer *depth_irb;
202 struct gl_renderbuffer *rb;
203 struct gl_pixelstore_attrib old_unpack;
204 GLstencil *stencil_pixels;
205 int row;
206
207 if (format != GL_STENCIL_INDEX)
208 return GL_FALSE;
209
210 /* If there's nothing to write, we're done. */
211 if (ctx->Stencil.WriteMask[0] == 0)
212 return GL_TRUE;
213
214 /* Can't do a per-bit writemask while treating stencil as rgba data. */
215 if ((ctx->Stencil.WriteMask[0] & 0xff) != 0xff)
216 return GL_FALSE;
217
218 /* We use FBOs for our wrapping of the depthbuffer into a color
219 * destination.
220 */
221 if (!ctx->Extensions.EXT_framebuffer_object)
222 return GL_FALSE;
223
224 /* We're going to mess with texturing with no regard to existing texture
225 * state, so if there is some set up we have to bail.
226 */
227 if (ctx->Texture._EnabledUnits != 0)
228 return GL_FALSE;
229
230 /* Can't do textured DrawPixels with a fragment program, unless we were
231 * to generate a new program that sampled our texture and put the results
232 * in the fragment color before the user's program started.
233 */
234 if (ctx->FragmentProgram.Enabled)
235 return GL_FALSE;
236
237 /* Check that we can load in a texture this big. */
238 if (width > (1 << (ctx->Const.MaxTextureLevels - 1)) ||
239 height > (1 << (ctx->Const.MaxTextureLevels - 1))) {
240 if (INTEL_DEBUG & DEBUG_FALLBACKS)
241 fprintf(stderr, "glDrawPixels(STENCIL_IDNEX) fallback: "
242 "bitmap too large (%dx%d)\n",
243 width, height);
244 return GL_FALSE;
245 }
246
247 _mesa_PushAttrib(GL_ENABLE_BIT | GL_TRANSFORM_BIT | GL_TEXTURE_BIT |
248 GL_CURRENT_BIT | GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
249 _mesa_PushClientAttrib(GL_CLIENT_VERTEX_ARRAY_BIT);
250 old_fb_name = ctx->DrawBuffer->Name;
251
252 _mesa_Disable(GL_POLYGON_STIPPLE);
253 _mesa_Disable(GL_DEPTH_TEST);
254 _mesa_Disable(GL_STENCIL_TEST);
255
256 /* Unpack the supplied stencil values into a ubyte buffer. */
257 assert(sizeof(GLstencil) == sizeof(GLubyte));
258 stencil_pixels = _mesa_malloc(width * height * sizeof(GLstencil));
259 for (row = 0; row < height; row++) {
260 GLvoid *source = _mesa_image_address2d(unpack, pixels,
261 width, height,
262 GL_COLOR_INDEX, type,
263 row, 0);
264 _mesa_unpack_stencil_span(ctx, width, GL_UNSIGNED_BYTE,
265 stencil_pixels +
266 row * width * sizeof(GLstencil),
267 type, source, unpack, ctx->_ImageTransferState);
268 }
269
270 /* Take the current depth/stencil renderbuffer, and make a new one wrapping
271 * it which will be treated as GL_RGBA8 so we can render to it as a color
272 * buffer.
273 */
274 depth_irb = intel_get_renderbuffer(ctx->DrawBuffer, BUFFER_DEPTH);
275 irb = intel_create_renderbuffer(GL_RGBA8);
276 rb = &irb->Base;
277 irb->Base.Width = depth_irb->Base.Width;
278 irb->Base.Height = depth_irb->Base.Height;
279 intel_renderbuffer_set_region(irb, depth_irb->region);
280
281 /* Create a name for our renderbuffer, which lets us use other mesa
282 * rb functions for convenience.
283 */
284 _mesa_GenRenderbuffersEXT(1, &rb_name);
285 irb->Base.RefCount++;
286 _mesa_HashInsert(ctx->Shared->RenderBuffers, rb_name, &irb->Base);
287
288 /* Bind the new renderbuffer to the color attachment point. */
289 _mesa_GenFramebuffersEXT(1, &fb_name);
290 _mesa_BindFramebufferEXT(GL_FRAMEBUFFER_EXT, fb_name);
291 _mesa_FramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT,
292 GL_COLOR_ATTACHMENT0_EXT,
293 GL_RENDERBUFFER_EXT,
294 rb_name);
295 /* Choose to render to the color attachment. */
296 _mesa_DrawBuffer(GL_COLOR_ATTACHMENT0_EXT);
297
298 _mesa_DepthMask(GL_FALSE);
299 _mesa_ColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_TRUE);
300
301 _mesa_ActiveTextureARB(GL_TEXTURE0_ARB);
302 _mesa_Enable(GL_TEXTURE_2D);
303 _mesa_GenTextures(1, &texname);
304 _mesa_BindTexture(GL_TEXTURE_2D, texname);
305 _mesa_TexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
306 _mesa_TexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
307 _mesa_TexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
308 old_unpack = ctx->Unpack;
309 ctx->Unpack = ctx->DefaultPacking;
310 _mesa_TexImage2D(GL_TEXTURE_2D, 0, GL_INTENSITY, width, height, 0,
311 GL_RED, GL_UNSIGNED_BYTE, stencil_pixels);
312 ctx->Unpack = old_unpack;
313 _mesa_free(stencil_pixels);
314
315 _mesa_MatrixMode(GL_PROJECTION);
316 _mesa_PushMatrix();
317 _mesa_LoadIdentity();
318 _mesa_Ortho(0, ctx->DrawBuffer->Width, 0, ctx->DrawBuffer->Height, 1, -1);
319
320 _mesa_MatrixMode(GL_MODELVIEW);
321 _mesa_PushMatrix();
322 _mesa_LoadIdentity();
323
324 vertices[0][0] = x;
325 vertices[0][1] = y;
326 vertices[1][0] = x + width * ctx->Pixel.ZoomX;
327 vertices[1][1] = y;
328 vertices[2][0] = x + width * ctx->Pixel.ZoomX;
329 vertices[2][1] = y + height * ctx->Pixel.ZoomY;
330 vertices[3][0] = x;
331 vertices[3][1] = y + height * ctx->Pixel.ZoomY;
332
333 texcoords[0][0] = 0.0;
334 texcoords[0][1] = 0.0;
335 texcoords[1][0] = 1.0;
336 texcoords[1][1] = 0.0;
337 texcoords[2][0] = 1.0;
338 texcoords[2][1] = 1.0;
339 texcoords[3][0] = 0.0;
340 texcoords[3][1] = 1.0;
341
342 _mesa_VertexPointer(2, GL_FLOAT, 2 * sizeof(GLfloat), &vertices);
343 _mesa_TexCoordPointer(2, GL_FLOAT, 2 * sizeof(GLfloat), &texcoords);
344 _mesa_Enable(GL_VERTEX_ARRAY);
345 _mesa_Enable(GL_TEXTURE_COORD_ARRAY);
346 CALL_DrawArrays(ctx->Exec, (GL_TRIANGLE_FAN, 0, 4));
347
348 _mesa_BindFramebufferEXT(GL_FRAMEBUFFER_EXT, old_fb_name);
349
350 _mesa_MatrixMode(GL_PROJECTION);
351 _mesa_PopMatrix();
352 _mesa_MatrixMode(GL_MODELVIEW);
353 _mesa_PopMatrix();
354 _mesa_PopClientAttrib();
355 _mesa_PopAttrib();
356
357 _mesa_DeleteTextures(1, &texname);
358 _mesa_DeleteFramebuffersEXT(1, &fb_name);
359 _mesa_DeleteRenderbuffersEXT(1, &rb_name);
360
361 return GL_TRUE;
362 }
363
364 void
365 intelDrawPixels(GLcontext * ctx,
366 GLint x, GLint y,
367 GLsizei width, GLsizei height,
368 GLenum format,
369 GLenum type,
370 const struct gl_pixelstore_attrib *unpack,
371 const GLvoid * pixels)
372 {
373 if (intel_texture_drawpixels(ctx, x, y, width, height, format, type,
374 unpack, pixels))
375 return;
376
377 if (intel_stencil_drawpixels(ctx, x, y, width, height, format, type,
378 unpack, pixels))
379 return;
380
381 if (INTEL_DEBUG & DEBUG_PIXEL)
382 _mesa_printf("%s: fallback to swrast\n", __FUNCTION__);
383
384 if (ctx->FragmentProgram._Current == ctx->FragmentProgram._TexEnvProgram) {
385 /*
386 * We don't want the i915 texenv program to be applied to DrawPixels.
387 * This is really just a performance optimization (mesa will other-
388 * wise happily run the fragment program on each pixel in the image).
389 */
390 struct gl_fragment_program *fpSave = ctx->FragmentProgram._Current;
391 /* can't just set current frag prog to 0 here as on buffer resize
392 we'll get new state checks which will segfault. Remains a hack. */
393 ctx->FragmentProgram._Current = NULL;
394 ctx->FragmentProgram._UseTexEnvProgram = GL_FALSE;
395 ctx->FragmentProgram._Active = GL_FALSE;
396 _swrast_DrawPixels( ctx, x, y, width, height, format, type,
397 unpack, pixels );
398 ctx->FragmentProgram._Current = fpSave;
399 ctx->FragmentProgram._UseTexEnvProgram = GL_TRUE;
400 ctx->FragmentProgram._Active = GL_TRUE;
401 _swrast_InvalidateState(ctx, _NEW_PROGRAM);
402 }
403 else {
404 _swrast_DrawPixels( ctx, x, y, width, height, format, type,
405 unpack, pixels );
406 }
407 }