Merge branch 'master' of ssh://git.freedesktop.org/git/mesa/mesa into r600_state_predict
[mesa.git] / src / mesa / drivers / dri / intel / intel_pixel_bitmap.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/colormac.h"
32 #include "main/mtypes.h"
33 #include "main/macros.h"
34 #include "main/bufferobj.h"
35 #include "main/pixelstore.h"
36 #include "main/state.h"
37 #include "main/teximage.h"
38 #include "main/texenv.h"
39 #include "main/texobj.h"
40 #include "main/texstate.h"
41 #include "main/texparam.h"
42 #include "main/varray.h"
43 #include "main/attrib.h"
44 #include "main/enable.h"
45 #include "main/viewport.h"
46 #include "shader/arbprogram.h"
47 #include "glapi/dispatch.h"
48 #include "swrast/swrast.h"
49
50 #include "intel_screen.h"
51 #include "intel_context.h"
52 #include "intel_batchbuffer.h"
53 #include "intel_blit.h"
54 #include "intel_regions.h"
55 #include "intel_buffer_objects.h"
56 #include "intel_buffers.h"
57 #include "intel_pixel.h"
58 #include "intel_reg.h"
59
60
61 #define FILE_DEBUG_FLAG DEBUG_PIXEL
62
63
64 /* Unlike the other intel_pixel_* functions, the expectation here is
65 * that the incoming data is not in a PBO. With the XY_TEXT blit
66 * method, there's no benefit haveing it in a PBO, but we could
67 * implement a path based on XY_MONO_SRC_COPY_BLIT which might benefit
68 * PBO bitmaps. I think they are probably pretty rare though - I
69 * wonder if Xgl uses them?
70 */
71 static const GLubyte *map_pbo( GLcontext *ctx,
72 GLsizei width, GLsizei height,
73 const struct gl_pixelstore_attrib *unpack,
74 const GLubyte *bitmap )
75 {
76 GLubyte *buf;
77
78 if (!_mesa_validate_pbo_access(2, unpack, width, height, 1,
79 GL_COLOR_INDEX, GL_BITMAP,
80 (GLvoid *) bitmap)) {
81 _mesa_error(ctx, GL_INVALID_OPERATION,"glBitmap(invalid PBO access)");
82 return NULL;
83 }
84
85 buf = (GLubyte *) ctx->Driver.MapBuffer(ctx, GL_PIXEL_UNPACK_BUFFER_EXT,
86 GL_READ_ONLY_ARB,
87 unpack->BufferObj);
88 if (!buf) {
89 _mesa_error(ctx, GL_INVALID_OPERATION, "glBitmap(PBO is mapped)");
90 return NULL;
91 }
92
93 return ADD_POINTERS(buf, bitmap);
94 }
95
96 static GLboolean test_bit( const GLubyte *src,
97 GLuint bit )
98 {
99 return (src[bit/8] & (1<<(bit % 8))) ? 1 : 0;
100 }
101
102 static GLboolean test_msb_bit(const GLubyte *src, GLuint bit)
103 {
104 return (src[bit/8] & (1<<(7 - (bit % 8)))) ? 1 : 0;
105 }
106
107 static void set_bit( GLubyte *dest,
108 GLuint bit )
109 {
110 dest[bit/8] |= 1 << (bit % 8);
111 }
112
113 /* Extract a rectangle's worth of data from the bitmap. Called
114 * per-cliprect.
115 */
116 static GLuint get_bitmap_rect(GLsizei width, GLsizei height,
117 const struct gl_pixelstore_attrib *unpack,
118 const GLubyte *bitmap,
119 GLuint x, GLuint y,
120 GLuint w, GLuint h,
121 GLubyte *dest,
122 GLuint row_align,
123 GLboolean invert)
124 {
125 GLuint src_offset = (x + unpack->SkipPixels) & 0x7;
126 GLuint mask = unpack->LsbFirst ? 0 : 7;
127 GLuint bit = 0;
128 GLint row, col;
129 GLint first, last;
130 GLint incr;
131 GLuint count = 0;
132
133 if (INTEL_DEBUG & DEBUG_PIXEL)
134 _mesa_printf("%s %d,%d %dx%d bitmap %dx%d skip %d src_offset %d mask %d\n",
135 __FUNCTION__, x,y,w,h,width,height,unpack->SkipPixels, src_offset, mask);
136
137 if (invert) {
138 first = h-1;
139 last = 0;
140 incr = -1;
141 }
142 else {
143 first = 0;
144 last = h-1;
145 incr = 1;
146 }
147
148 /* Require that dest be pre-zero'd.
149 */
150 for (row = first; row != (last+incr); row += incr) {
151 const GLubyte *rowsrc = _mesa_image_address2d(unpack, bitmap,
152 width, height,
153 GL_COLOR_INDEX, GL_BITMAP,
154 y + row, x);
155
156 for (col = 0; col < w; col++, bit++) {
157 if (test_bit(rowsrc, (col + src_offset) ^ mask)) {
158 set_bit(dest, bit ^ 7);
159 count++;
160 }
161 }
162
163 if (row_align)
164 bit = ALIGN(bit, row_align);
165 }
166
167 return count;
168 }
169
170 /**
171 * Returns the low Y value of the vertical range given, flipped according to
172 * whether the framebuffer is or not.
173 */
174 static inline int
175 y_flip(struct gl_framebuffer *fb, int y, int height)
176 {
177 if (fb->Name != 0)
178 return y;
179 else
180 return fb->Height - y - height;
181 }
182
183 /*
184 * Render a bitmap.
185 */
186 static GLboolean
187 do_blit_bitmap( GLcontext *ctx,
188 GLint dstx, GLint dsty,
189 GLsizei width, GLsizei height,
190 const struct gl_pixelstore_attrib *unpack,
191 const GLubyte *bitmap )
192 {
193 struct intel_context *intel = intel_context(ctx);
194 struct intel_region *dst = intel_drawbuf_region(intel);
195 struct gl_framebuffer *fb = ctx->DrawBuffer;
196 GLfloat tmpColor[4];
197 GLubyte ubcolor[4];
198 GLuint color;
199 unsigned int num_cliprects;
200 drm_clip_rect_t *cliprects;
201 int x_off, y_off;
202 GLsizei bitmap_width = width;
203 GLsizei bitmap_height = height;
204
205 /* Update draw buffer bounds */
206 _mesa_update_state(ctx);
207
208 if (ctx->Depth.Test) {
209 /* The blit path produces incorrect results when depth testing is on.
210 * It seems the blit Z coord is always 1.0 (the far plane) so fragments
211 * will likely be obscured by other, closer geometry.
212 */
213 return GL_FALSE;
214 }
215
216 if (!dst)
217 return GL_FALSE;
218
219 if (unpack->BufferObj->Name) {
220 bitmap = map_pbo(ctx, width, height, unpack, bitmap);
221 if (bitmap == NULL)
222 return GL_TRUE; /* even though this is an error, we're done */
223 }
224
225 COPY_4V(tmpColor, ctx->Current.RasterColor);
226
227 if (NEED_SECONDARY_COLOR(ctx)) {
228 ADD_3V(tmpColor, tmpColor, ctx->Current.RasterSecondaryColor);
229 }
230
231 UNCLAMPED_FLOAT_TO_UBYTE(ubcolor[0], tmpColor[0]);
232 UNCLAMPED_FLOAT_TO_UBYTE(ubcolor[1], tmpColor[1]);
233 UNCLAMPED_FLOAT_TO_UBYTE(ubcolor[2], tmpColor[2]);
234 UNCLAMPED_FLOAT_TO_UBYTE(ubcolor[3], tmpColor[3]);
235
236 if (dst->cpp == 2)
237 color = INTEL_PACKCOLOR565(ubcolor[0], ubcolor[1], ubcolor[2]);
238 else
239 color = INTEL_PACKCOLOR8888(ubcolor[0], ubcolor[1],
240 ubcolor[2], ubcolor[3]);
241
242 if (!intel_check_blit_fragment_ops(ctx, tmpColor[3] == 1.0F))
243 return GL_FALSE;
244
245 LOCK_HARDWARE(intel);
246
247 intel_get_cliprects(intel, &cliprects, &num_cliprects, &x_off, &y_off);
248 if (num_cliprects != 0) {
249 GLuint i;
250 GLint orig_dstx = dstx;
251 GLint orig_dsty = dsty;
252
253 /* Clip to buffer bounds and scissor. */
254 if (!_mesa_clip_to_region(fb->_Xmin, fb->_Ymin,
255 fb->_Xmax, fb->_Ymax,
256 &dstx, &dsty, &width, &height))
257 goto out;
258
259 dstx = x_off + dstx;
260 dsty = y_off + y_flip(fb, dsty, height);
261
262 for (i = 0; i < num_cliprects; i++) {
263 int box_x, box_y, box_w, box_h;
264 GLint px, py;
265 GLuint stipple[32];
266
267 box_x = dstx;
268 box_y = dsty;
269 box_w = width;
270 box_h = height;
271
272 /* Clip to drawable cliprect */
273 if (!_mesa_clip_to_region(cliprects[i].x1,
274 cliprects[i].y1,
275 cliprects[i].x2,
276 cliprects[i].y2,
277 &box_x, &box_y, &box_w, &box_h))
278 continue;
279
280 #define DY 32
281 #define DX 32
282
283 /* Then, finally, chop it all into chunks that can be
284 * digested by hardware:
285 */
286 for (py = 0; py < box_h; py += DY) {
287 for (px = 0; px < box_w; px += DX) {
288 int h = MIN2(DY, box_h - py);
289 int w = MIN2(DX, box_w - px);
290 GLuint sz = ALIGN(ALIGN(w,8) * h, 64)/8;
291 GLenum logic_op = ctx->Color.ColorLogicOpEnabled ?
292 ctx->Color.LogicOp : GL_COPY;
293
294 assert(sz <= sizeof(stipple));
295 memset(stipple, 0, sz);
296
297 /* May need to adjust this when padding has been introduced in
298 * sz above:
299 *
300 * Have to translate destination coordinates back into source
301 * coordinates.
302 */
303 if (get_bitmap_rect(bitmap_width, bitmap_height, unpack,
304 bitmap,
305 -orig_dstx + (box_x + px - x_off),
306 -orig_dsty + y_flip(fb,
307 box_y + py - y_off, h),
308 w, h,
309 (GLubyte *)stipple,
310 8,
311 fb->Name == 0 ? GL_TRUE : GL_FALSE) == 0)
312 continue;
313
314 if (!intelEmitImmediateColorExpandBlit(intel,
315 dst->cpp,
316 (GLubyte *)stipple,
317 sz,
318 color,
319 dst->pitch,
320 dst->buffer,
321 0,
322 dst->tiling,
323 box_x + px,
324 box_y + py,
325 w, h,
326 logic_op)) {
327 return GL_FALSE;
328 }
329 }
330 }
331 }
332 }
333 out:
334 UNLOCK_HARDWARE(intel);
335
336 if (INTEL_DEBUG & DEBUG_SYNC)
337 intel_batchbuffer_flush(intel->batch);
338
339 if (unpack->BufferObj->Name) {
340 /* done with PBO so unmap it now */
341 ctx->Driver.UnmapBuffer(ctx, GL_PIXEL_UNPACK_BUFFER_EXT,
342 unpack->BufferObj);
343 }
344
345 return GL_TRUE;
346 }
347
348 static GLboolean
349 intel_texture_bitmap(GLcontext * ctx,
350 GLint dst_x, GLint dst_y,
351 GLsizei width, GLsizei height,
352 const struct gl_pixelstore_attrib *unpack,
353 const GLubyte *bitmap)
354 {
355 struct intel_context *intel = intel_context(ctx);
356 static const char *fp =
357 "!!ARBfp1.0\n"
358 "TEMP val;\n"
359 "PARAM color=program.local[0];\n"
360 "TEX val, fragment.texcoord[0], texture[0], 2D;\n"
361 "ADD val, val.wwww, {-.5, -.5, -.5, -.5};\n"
362 "KIL val;\n"
363 "MOV result.color, color;\n"
364 "END\n";
365 GLuint texname;
366 GLfloat vertices[4][4];
367 GLint old_active_texture;
368 GLubyte *unpacked_bitmap;
369 GLubyte *a8_bitmap;
370 int x, y;
371 GLfloat dst_z;
372
373 /* We need a fragment program for the KIL effect */
374 if (!ctx->Extensions.ARB_fragment_program ||
375 !ctx->Extensions.ARB_vertex_program) {
376 if (INTEL_DEBUG & DEBUG_FALLBACKS)
377 fprintf(stderr,
378 "glBitmap fallback: No fragment/vertex program support\n");
379 return GL_FALSE;
380 }
381
382 /* We're going to mess with texturing with no regard to existing texture
383 * state, so if there is some set up we have to bail.
384 */
385 if (ctx->Texture._EnabledUnits != 0) {
386 if (INTEL_DEBUG & DEBUG_FALLBACKS)
387 fprintf(stderr, "glBitmap fallback: texturing enabled\n");
388 return GL_FALSE;
389 }
390
391 /* Can't do textured DrawPixels with a fragment program, unless we were
392 * to generate a new program that sampled our texture and put the results
393 * in the fragment color before the user's program started.
394 */
395 if (ctx->FragmentProgram.Enabled) {
396 if (INTEL_DEBUG & DEBUG_FALLBACKS)
397 fprintf(stderr, "glBitmap fallback: fragment program enabled\n");
398 return GL_FALSE;
399 }
400
401 if (ctx->VertexProgram.Enabled) {
402 if (INTEL_DEBUG & DEBUG_FALLBACKS)
403 fprintf(stderr, "glBitmap fallback: vertex program enabled\n");
404 return GL_FALSE;
405 }
406
407 if (!ctx->Extensions.ARB_texture_non_power_of_two &&
408 (!is_power_of_two(width) || !is_power_of_two(height))) {
409 if (INTEL_DEBUG & DEBUG_FALLBACKS)
410 fprintf(stderr,
411 "glBitmap() fallback: NPOT texture\n");
412 return GL_FALSE;
413 }
414
415 if (ctx->Fog.Enabled) {
416 if (INTEL_DEBUG & DEBUG_FALLBACKS)
417 fprintf(stderr, "glBitmap() fallback: fog\n");
418 return GL_FALSE;
419 }
420
421 /* Check that we can load in a texture this big. */
422 if (width > (1 << (ctx->Const.MaxTextureLevels - 1)) ||
423 height > (1 << (ctx->Const.MaxTextureLevels - 1))) {
424 if (INTEL_DEBUG & DEBUG_FALLBACKS)
425 fprintf(stderr, "glBitmap fallback: bitmap too large (%dx%d)\n",
426 width, height);
427 return GL_FALSE;
428 }
429
430 /* Convert the A1 bitmap to an A8 format suitable for glTexImage */
431 if (unpack->BufferObj->Name) {
432 bitmap = map_pbo(ctx, width, height, unpack, bitmap);
433 if (bitmap == NULL)
434 return GL_TRUE; /* even though this is an error, we're done */
435 }
436 unpacked_bitmap = _mesa_unpack_bitmap(width, height, bitmap,
437 unpack);
438 a8_bitmap = _mesa_calloc(width * height);
439 for (y = 0; y < height; y++) {
440 for (x = 0; x < width; x++) {
441 if (test_msb_bit(unpacked_bitmap, ALIGN(width, 8) * y + x))
442 a8_bitmap[y * width + x] = 0xff;
443 }
444 }
445 _mesa_free(unpacked_bitmap);
446 if (unpack->BufferObj->Name) {
447 /* done with PBO so unmap it now */
448 ctx->Driver.UnmapBuffer(ctx, GL_PIXEL_UNPACK_BUFFER_EXT,
449 unpack->BufferObj);
450 }
451
452 /* Save GL state before we start setting up our drawing */
453 _mesa_PushAttrib(GL_ENABLE_BIT | GL_CURRENT_BIT |
454 GL_VIEWPORT_BIT);
455 _mesa_PushClientAttrib(GL_CLIENT_VERTEX_ARRAY_BIT |
456 GL_CLIENT_PIXEL_STORE_BIT);
457 old_active_texture = ctx->Texture.CurrentUnit;
458
459 _mesa_Disable(GL_POLYGON_STIPPLE);
460
461 /* Upload our bitmap data to an alpha texture */
462 _mesa_ActiveTextureARB(GL_TEXTURE0_ARB);
463 _mesa_Enable(GL_TEXTURE_2D);
464 _mesa_GenTextures(1, &texname);
465 _mesa_BindTexture(GL_TEXTURE_2D, texname);
466 _mesa_TexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
467 _mesa_TexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
468
469 _mesa_PixelStorei(GL_UNPACK_SWAP_BYTES, GL_FALSE);
470 _mesa_PixelStorei(GL_UNPACK_LSB_FIRST, GL_FALSE);
471 _mesa_PixelStorei(GL_UNPACK_ROW_LENGTH, 0);
472 _mesa_PixelStorei(GL_UNPACK_SKIP_PIXELS, 0);
473 _mesa_PixelStorei(GL_UNPACK_SKIP_ROWS, 0);
474 _mesa_PixelStorei(GL_UNPACK_ALIGNMENT, 1);
475 _mesa_TexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, width, height, 0,
476 GL_ALPHA, GL_UNSIGNED_BYTE, a8_bitmap);
477 _mesa_free(a8_bitmap);
478
479 meta_set_fragment_program(&intel->meta, &intel->meta.bitmap_fp, fp);
480 _mesa_ProgramLocalParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB, 0,
481 ctx->Current.RasterColor);
482 meta_set_passthrough_vertex_program(&intel->meta);
483 meta_set_passthrough_transform(&intel->meta);
484
485 /* convert rasterpos Z from [0,1] to NDC coord in [-1,1] */
486 dst_z = -1.0 + 2.0 * ctx->Current.RasterPos[2];
487
488 /* RasterPos[2] already takes into account the DepthRange mapping. */
489 _mesa_DepthRange(0.0, 1.0);
490
491 vertices[0][0] = dst_x;
492 vertices[0][1] = dst_y;
493 vertices[0][2] = dst_z;
494 vertices[0][3] = 1.0;
495 vertices[1][0] = dst_x + width;
496 vertices[1][1] = dst_y;
497 vertices[1][2] = dst_z;
498 vertices[1][3] = 1.0;
499 vertices[2][0] = dst_x + width;
500 vertices[2][1] = dst_y + height;
501 vertices[2][2] = dst_z;
502 vertices[2][3] = 1.0;
503 vertices[3][0] = dst_x;
504 vertices[3][1] = dst_y + height;
505 vertices[3][2] = dst_z;
506 vertices[3][3] = 1.0;
507
508 _mesa_VertexPointer(4, GL_FLOAT, 4 * sizeof(GLfloat), &vertices);
509 _mesa_Enable(GL_VERTEX_ARRAY);
510 meta_set_default_texrect(&intel->meta);
511 _mesa_DrawArrays(GL_TRIANGLE_FAN, 0, 4);
512
513 meta_restore_texcoords(&intel->meta);
514 meta_restore_transform(&intel->meta);
515 meta_restore_fragment_program(&intel->meta);
516 meta_restore_vertex_program(&intel->meta);
517
518 _mesa_PopClientAttrib();
519 _mesa_Disable(GL_TEXTURE_2D); /* asserted that it was disabled at entry */
520 _mesa_ActiveTextureARB(GL_TEXTURE0_ARB + old_active_texture);
521 _mesa_PopAttrib();
522
523 _mesa_DeleteTextures(1, &texname);
524
525 return GL_TRUE;
526 }
527
528 /* There are a large number of possible ways to implement bitmap on
529 * this hardware, most of them have some sort of drawback. Here are a
530 * few that spring to mind:
531 *
532 * Blit:
533 * - XY_MONO_SRC_BLT_CMD
534 * - use XY_SETUP_CLIP_BLT for cliprect clipping.
535 * - XY_TEXT_BLT
536 * - XY_TEXT_IMMEDIATE_BLT
537 * - blit per cliprect, subject to maximum immediate data size.
538 * - XY_COLOR_BLT
539 * - per pixel or run of pixels
540 * - XY_PIXEL_BLT
541 * - good for sparse bitmaps
542 *
543 * 3D engine:
544 * - Point per pixel
545 * - Translate bitmap to an alpha texture and render as a quad
546 * - Chop bitmap up into 32x32 squares and render w/polygon stipple.
547 */
548 void
549 intelBitmap(GLcontext * ctx,
550 GLint x, GLint y,
551 GLsizei width, GLsizei height,
552 const struct gl_pixelstore_attrib *unpack,
553 const GLubyte * pixels)
554 {
555 if (do_blit_bitmap(ctx, x, y, width, height,
556 unpack, pixels))
557 return;
558
559 if (intel_texture_bitmap(ctx, x, y, width, height,
560 unpack, pixels))
561 return;
562
563 if (INTEL_DEBUG & DEBUG_PIXEL)
564 _mesa_printf("%s: fallback to swrast\n", __FUNCTION__);
565
566 _swrast_Bitmap(ctx, x, y, width, height, unpack, pixels);
567 }