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 "main/viewport.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 struct intel_context *intel = intel_context(ctx);
72 GLuint texname;
73 GLfloat vertices[4][4];
74 GLfloat z;
75 GLint old_active_texture;
76 GLenum internalFormat;
77
78 /* We're going to mess with texturing with no regard to existing texture
79 * state, so if there is some set up we have to bail.
80 */
81 if (ctx->Texture._EnabledUnits != 0) {
82 if (INTEL_DEBUG & DEBUG_FALLBACKS)
83 fprintf(stderr, "glDrawPixels() fallback: texturing enabled\n");
84 return GL_FALSE;
85 }
86
87 /* Can't do textured DrawPixels with a fragment program, unless we were
88 * to generate a new program that sampled our texture and put the results
89 * in the fragment color before the user's program started.
90 */
91 if (ctx->FragmentProgram.Enabled) {
92 if (INTEL_DEBUG & DEBUG_FALLBACKS)
93 fprintf(stderr, "glDrawPixels() fallback: fragment program enabled\n");
94 return GL_FALSE;
95 }
96
97 /* We don't have a way to generate fragments with stencil values which
98 * will set the resulting stencil value.
99 */
100 if (format == GL_STENCIL_INDEX || format == GL_DEPTH_STENCIL)
101 return GL_FALSE;
102
103 /* Check that we can load in a texture this big. */
104 if (width > (1 << (ctx->Const.MaxTextureLevels - 1)) ||
105 height > (1 << (ctx->Const.MaxTextureLevels - 1))) {
106 if (INTEL_DEBUG & DEBUG_FALLBACKS)
107 fprintf(stderr, "glDrawPixels() fallback: bitmap too large (%dx%d)\n",
108 width, height);
109 return GL_FALSE;
110 }
111
112 /* To do DEPTH_COMPONENT, we would need to change our setup to not draw to
113 * the color buffer, and sample the texture values into the fragment depth
114 * in a program.
115 */
116 if (format == GL_DEPTH_COMPONENT) {
117 if (INTEL_DEBUG & DEBUG_FALLBACKS)
118 fprintf(stderr,
119 "glDrawPixels() fallback: format == GL_DEPTH_COMPONENT\n");
120 return GL_FALSE;
121 }
122
123 if (!ctx->Extensions.ARB_texture_non_power_of_two &&
124 (!is_power_of_two(width) || !is_power_of_two(height))) {
125 if (INTEL_DEBUG & DEBUG_FALLBACKS)
126 fprintf(stderr,
127 "glDrawPixels() fallback: NPOT texture\n");
128 return GL_FALSE;
129 }
130
131 _mesa_PushAttrib(GL_ENABLE_BIT | GL_TEXTURE_BIT |
132 GL_CURRENT_BIT | GL_VIEWPORT_BIT);
133 _mesa_PushClientAttrib(GL_CLIENT_VERTEX_ARRAY_BIT);
134
135 /* XXX: pixel store stuff */
136 _mesa_Disable(GL_POLYGON_STIPPLE);
137
138 old_active_texture = ctx->Texture.CurrentUnit;
139 _mesa_ActiveTextureARB(GL_TEXTURE0_ARB);
140 _mesa_Enable(GL_TEXTURE_2D);
141 _mesa_GenTextures(1, &texname);
142 _mesa_BindTexture(GL_TEXTURE_2D, texname);
143 _mesa_TexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
144 _mesa_TexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
145 _mesa_TexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
146 if (type == GL_ALPHA)
147 internalFormat = GL_ALPHA;
148 else
149 internalFormat = GL_RGBA;
150 _mesa_TexImage2D(GL_TEXTURE_2D, 0, internalFormat, width, height, 0, format,
151 type, pixels);
152
153 meta_set_passthrough_transform(&intel->meta);
154
155 /* convert rasterpos Z from [0,1] to NDC coord in [-1,1] */
156 z = -1.0 + 2.0 * ctx->Current.RasterPos[2];
157
158 /* RasterPos[2] already takes into account the DepthRange mapping. */
159 _mesa_DepthRange(0.0, 1.0);
160
161 /* Create the vertex buffer based on the current raster pos. The x and y
162 * we're handed are ctx->Current.RasterPos[0,1] rounded to integers.
163 * We also apply the depth. However, the W component is already multiplied
164 * into ctx->Current.RasterPos[0,1,2] and we can ignore it at this point.
165 */
166 vertices[0][0] = x;
167 vertices[0][1] = y;
168 vertices[0][2] = z;
169 vertices[0][3] = 1.0;
170 vertices[1][0] = x + width * ctx->Pixel.ZoomX;
171 vertices[1][1] = y;
172 vertices[1][2] = z;
173 vertices[1][3] = 1.0;
174 vertices[2][0] = x + width * ctx->Pixel.ZoomX;
175 vertices[2][1] = y + height * ctx->Pixel.ZoomY;
176 vertices[2][2] = z;
177 vertices[2][3] = 1.0;
178 vertices[3][0] = x;
179 vertices[3][1] = y + height * ctx->Pixel.ZoomY;
180 vertices[3][2] = z;
181 vertices[3][3] = 1.0;
182
183 _mesa_VertexPointer(4, GL_FLOAT, 4 * sizeof(GLfloat), &vertices);
184 _mesa_Enable(GL_VERTEX_ARRAY);
185 meta_set_default_texrect(&intel->meta);
186
187 _mesa_DrawArrays(GL_TRIANGLE_FAN, 0, 4);
188
189 meta_restore_texcoords(&intel->meta);
190 meta_restore_transform(&intel->meta);
191
192 _mesa_ActiveTextureARB(GL_TEXTURE0_ARB + old_active_texture);
193 _mesa_PopClientAttrib();
194 _mesa_PopAttrib();
195
196 _mesa_DeleteTextures(1, &texname);
197
198 return GL_TRUE;
199 }
200
201 static GLboolean
202 intel_stencil_drawpixels(GLcontext * ctx,
203 GLint x, GLint y,
204 GLsizei width, GLsizei height,
205 GLenum format,
206 GLenum type,
207 const struct gl_pixelstore_attrib *unpack,
208 const GLvoid *pixels)
209 {
210 struct intel_context *intel = intel_context(ctx);
211 GLuint texname, rb_name, fb_name, old_fb_name;
212 GLfloat vertices[4][2];
213 struct intel_renderbuffer *irb;
214 struct intel_renderbuffer *depth_irb;
215 struct gl_renderbuffer *rb;
216 struct gl_pixelstore_attrib old_unpack;
217 GLstencil *stencil_pixels;
218 int row, y1, y2;
219 GLint old_active_texture;
220 GLboolean rendering_to_fbo = ctx->DrawBuffer->Name != 0;
221
222 if (format != GL_STENCIL_INDEX)
223 return GL_FALSE;
224
225 /* If there's nothing to write, we're done. */
226 if (ctx->Stencil.WriteMask[0] == 0)
227 return GL_TRUE;
228
229 /* Can't do a per-bit writemask while treating stencil as rgba data. */
230 if ((ctx->Stencil.WriteMask[0] & 0xff) != 0xff) {
231 if (INTEL_DEBUG & DEBUG_FALLBACKS)
232 fprintf(stderr, "glDrawPixels(STENCIL_INDEX) fallback: "
233 "stencil mask enabled\n");
234 return GL_FALSE;
235 }
236
237 /* We don't support stencil testing/ops here */
238 if (ctx->Stencil._Enabled)
239 return GL_FALSE;
240
241 /* We use FBOs for our wrapping of the depthbuffer into a color
242 * destination.
243 */
244 if (!ctx->Extensions.EXT_framebuffer_object)
245 return GL_FALSE;
246
247 /* We're going to mess with texturing with no regard to existing texture
248 * state, so if there is some set up we have to bail.
249 */
250 if (ctx->Texture._EnabledUnits != 0) {
251 if (INTEL_DEBUG & DEBUG_FALLBACKS)
252 fprintf(stderr, "glDrawPixels(STENCIL_INDEX) fallback: "
253 "texturing enabled\n");
254 return GL_FALSE;
255 }
256
257 /* Can't do textured DrawPixels with a fragment program, unless we were
258 * to generate a new program that sampled our texture and put the results
259 * in the fragment color before the user's program started.
260 */
261 if (ctx->FragmentProgram.Enabled) {
262 if (INTEL_DEBUG & DEBUG_FALLBACKS)
263 fprintf(stderr, "glDrawPixels(STENCIL_INDEX) fallback: "
264 "fragment program enabled\n");
265 return GL_FALSE;
266 }
267
268 /* Check that we can load in a texture this big. */
269 if (width > (1 << (ctx->Const.MaxTextureLevels - 1)) ||
270 height > (1 << (ctx->Const.MaxTextureLevels - 1))) {
271 if (INTEL_DEBUG & DEBUG_FALLBACKS)
272 fprintf(stderr, "glDrawPixels(STENCIL_INDEX) fallback: "
273 "bitmap too large (%dx%d)\n",
274 width, height);
275 return GL_FALSE;
276 }
277
278 if (!ctx->Extensions.ARB_texture_non_power_of_two &&
279 (!is_power_of_two(width) || !is_power_of_two(height))) {
280 if (INTEL_DEBUG & DEBUG_FALLBACKS)
281 fprintf(stderr,
282 "glDrawPixels(GL_STENCIL_INDEX) fallback: NPOT texture\n");
283 return GL_FALSE;
284 }
285
286 _mesa_PushAttrib(GL_ENABLE_BIT | GL_TEXTURE_BIT |
287 GL_CURRENT_BIT | GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
288 _mesa_PushClientAttrib(GL_CLIENT_VERTEX_ARRAY_BIT);
289 old_fb_name = ctx->DrawBuffer->Name;
290 old_active_texture = ctx->Texture.CurrentUnit;
291
292 _mesa_Disable(GL_POLYGON_STIPPLE);
293 _mesa_Disable(GL_DEPTH_TEST);
294 _mesa_Disable(GL_STENCIL_TEST);
295
296 /* Unpack the supplied stencil values into a ubyte buffer. */
297 assert(sizeof(GLstencil) == sizeof(GLubyte));
298 stencil_pixels = _mesa_malloc(width * height * sizeof(GLstencil));
299 for (row = 0; row < height; row++) {
300 GLvoid *source = _mesa_image_address2d(unpack, pixels,
301 width, height,
302 GL_COLOR_INDEX, type,
303 row, 0);
304 _mesa_unpack_stencil_span(ctx, width, GL_UNSIGNED_BYTE,
305 stencil_pixels +
306 row * width * sizeof(GLstencil),
307 type, source, unpack, ctx->_ImageTransferState);
308 }
309
310 /* Take the current depth/stencil renderbuffer, and make a new one wrapping
311 * it which will be treated as GL_RGBA8 so we can render to it as a color
312 * buffer.
313 */
314 depth_irb = intel_get_renderbuffer(ctx->DrawBuffer, BUFFER_DEPTH);
315 irb = intel_create_renderbuffer(GL_RGBA8);
316 rb = &irb->Base;
317 irb->Base.Width = depth_irb->Base.Width;
318 irb->Base.Height = depth_irb->Base.Height;
319 intel_renderbuffer_set_region(irb, depth_irb->region);
320
321 /* Create a name for our renderbuffer, which lets us use other mesa
322 * rb functions for convenience.
323 */
324 _mesa_GenRenderbuffersEXT(1, &rb_name);
325 irb->Base.RefCount++;
326 _mesa_HashInsert(ctx->Shared->RenderBuffers, rb_name, &irb->Base);
327
328 /* Bind the new renderbuffer to the color attachment point. */
329 _mesa_GenFramebuffersEXT(1, &fb_name);
330 _mesa_BindFramebufferEXT(GL_FRAMEBUFFER_EXT, fb_name);
331 _mesa_FramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT,
332 GL_COLOR_ATTACHMENT0_EXT,
333 GL_RENDERBUFFER_EXT,
334 rb_name);
335 /* Choose to render to the color attachment. */
336 _mesa_DrawBuffer(GL_COLOR_ATTACHMENT0_EXT);
337
338 _mesa_DepthMask(GL_FALSE);
339 _mesa_ColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_TRUE);
340
341 _mesa_ActiveTextureARB(GL_TEXTURE0_ARB);
342 _mesa_Enable(GL_TEXTURE_2D);
343 _mesa_GenTextures(1, &texname);
344 _mesa_BindTexture(GL_TEXTURE_2D, texname);
345 _mesa_TexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
346 _mesa_TexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
347 _mesa_TexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
348 old_unpack = ctx->Unpack;
349 ctx->Unpack = ctx->DefaultPacking;
350 _mesa_TexImage2D(GL_TEXTURE_2D, 0, GL_INTENSITY, width, height, 0,
351 GL_RED, GL_UNSIGNED_BYTE, stencil_pixels);
352 ctx->Unpack = old_unpack;
353 _mesa_free(stencil_pixels);
354
355 meta_set_passthrough_transform(&intel->meta);
356
357 /* Since we're rendering to the framebuffer as if it was an FBO,
358 * if it's the window system we have to flip the coordinates.
359 */
360 if (rendering_to_fbo) {
361 y1 = y;
362 y2 = y + height * ctx->Pixel.ZoomY;
363 } else {
364 y1 = irb->Base.Height - (y + height * ctx->Pixel.ZoomY);
365 y2 = irb->Base.Height - y;
366 }
367 vertices[0][0] = x;
368 vertices[0][1] = y1;
369 vertices[1][0] = x + width * ctx->Pixel.ZoomX;
370 vertices[1][1] = y1;
371 vertices[2][0] = x + width * ctx->Pixel.ZoomX;
372 vertices[2][1] = y2;
373 vertices[3][0] = x;
374 vertices[3][1] = y2;
375
376 _mesa_VertexPointer(2, GL_FLOAT, 2 * sizeof(GLfloat), &vertices);
377 _mesa_Enable(GL_VERTEX_ARRAY);
378 meta_set_default_texrect(&intel->meta);
379
380 _mesa_DrawArrays(GL_TRIANGLE_FAN, 0, 4);
381
382 meta_restore_texcoords(&intel->meta);
383 meta_restore_transform(&intel->meta);
384
385 _mesa_ActiveTextureARB(GL_TEXTURE0_ARB + old_active_texture);
386 _mesa_BindFramebufferEXT(GL_FRAMEBUFFER_EXT, old_fb_name);
387
388 _mesa_PopClientAttrib();
389 _mesa_PopAttrib();
390
391 _mesa_DeleteTextures(1, &texname);
392 _mesa_DeleteFramebuffersEXT(1, &fb_name);
393 _mesa_DeleteRenderbuffersEXT(1, &rb_name);
394
395 return GL_TRUE;
396 }
397
398 void
399 intelDrawPixels(GLcontext * ctx,
400 GLint x, GLint y,
401 GLsizei width, GLsizei height,
402 GLenum format,
403 GLenum type,
404 const struct gl_pixelstore_attrib *unpack,
405 const GLvoid * pixels)
406 {
407 if (intel_texture_drawpixels(ctx, x, y, width, height, format, type,
408 unpack, pixels))
409 return;
410
411 if (intel_stencil_drawpixels(ctx, x, y, width, height, format, type,
412 unpack, pixels))
413 return;
414
415 if (INTEL_DEBUG & DEBUG_PIXEL)
416 _mesa_printf("%s: fallback to swrast\n", __FUNCTION__);
417
418 _swrast_DrawPixels(ctx, x, y, width, height, format, type,
419 unpack, pixels);
420 }