1 /**************************************************************************
3 * Copyright 2006 Tungsten Graphics, Inc., Cedar Park, Texas.
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portionsalloc
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.
26 **************************************************************************/
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/polygon.h"
36 #include "main/pixelstore.h"
37 #include "main/polygon.h"
38 #include "main/state.h"
39 #include "main/teximage.h"
40 #include "main/texobj.h"
41 #include "main/texstate.h"
42 #include "main/texparam.h"
43 #include "main/varray.h"
44 #include "main/attrib.h"
45 #include "main/enable.h"
46 #include "main/viewport.h"
47 #include "shader/arbprogram.h"
48 #include "swrast/swrast.h"
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_buffers.h"
56 #include "intel_pixel.h"
57 #include "intel_reg.h"
60 #define FILE_DEBUG_FLAG DEBUG_PIXEL
63 /* Unlike the other intel_pixel_* functions, the expectation here is
64 * that the incoming data is not in a PBO. With the XY_TEXT blit
65 * method, there's no benefit haveing it in a PBO, but we could
66 * implement a path based on XY_MONO_SRC_COPY_BLIT which might benefit
67 * PBO bitmaps. I think they are probably pretty rare though - I
68 * wonder if Xgl uses them?
70 static const GLubyte
*map_pbo( GLcontext
*ctx
,
71 GLsizei width
, GLsizei height
,
72 const struct gl_pixelstore_attrib
*unpack
,
73 const GLubyte
*bitmap
)
77 if (!_mesa_validate_pbo_access(2, unpack
, width
, height
, 1,
78 GL_COLOR_INDEX
, GL_BITMAP
,
80 _mesa_error(ctx
, GL_INVALID_OPERATION
,"glBitmap(invalid PBO access)");
84 buf
= (GLubyte
*) ctx
->Driver
.MapBuffer(ctx
, GL_PIXEL_UNPACK_BUFFER_EXT
,
88 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glBitmap(PBO is mapped)");
92 return ADD_POINTERS(buf
, bitmap
);
95 static GLboolean
test_bit( const GLubyte
*src
, GLuint bit
)
97 return (src
[bit
/8] & (1<<(bit
% 8))) ? 1 : 0;
100 static void set_bit( GLubyte
*dest
, GLuint bit
)
102 dest
[bit
/8] |= 1 << (bit
% 8);
105 /* Extract a rectangle's worth of data from the bitmap. Called
106 * per chunk of HW-sized bitmap.
108 static GLuint
get_bitmap_rect(GLsizei width
, GLsizei height
,
109 const struct gl_pixelstore_attrib
*unpack
,
110 const GLubyte
*bitmap
,
117 GLuint src_offset
= (x
+ unpack
->SkipPixels
) & 0x7;
118 GLuint mask
= unpack
->LsbFirst
? 0 : 7;
125 if (INTEL_DEBUG
& DEBUG_PIXEL
)
126 printf("%s %d,%d %dx%d bitmap %dx%d skip %d src_offset %d mask %d\n",
127 __FUNCTION__
, x
,y
,w
,h
,width
,height
,unpack
->SkipPixels
, src_offset
, mask
);
140 /* Require that dest be pre-zero'd.
142 for (row
= first
; row
!= (last
+incr
); row
+= incr
) {
143 const GLubyte
*rowsrc
= _mesa_image_address2d(unpack
, bitmap
,
145 GL_COLOR_INDEX
, GL_BITMAP
,
148 for (col
= 0; col
< w
; col
++, bit
++) {
149 if (test_bit(rowsrc
, (col
+ src_offset
) ^ mask
)) {
150 set_bit(dest
, bit
^ 7);
156 bit
= ALIGN(bit
, row_align
);
163 * Returns the low Y value of the vertical range given, flipped according to
164 * whether the framebuffer is or not.
167 y_flip(struct gl_framebuffer
*fb
, int y
, int height
)
172 return fb
->Height
- y
- height
;
179 do_blit_bitmap( GLcontext
*ctx
,
180 GLint dstx
, GLint dsty
,
181 GLsizei width
, GLsizei height
,
182 const struct gl_pixelstore_attrib
*unpack
,
183 const GLubyte
*bitmap
)
185 struct intel_context
*intel
= intel_context(ctx
);
186 struct intel_region
*dst
= intel_drawbuf_region(intel
);
187 struct gl_framebuffer
*fb
= ctx
->DrawBuffer
;
191 GLsizei bitmap_width
= width
;
192 GLsizei bitmap_height
= height
;
195 GLint orig_dstx
= dstx
;
196 GLint orig_dsty
= dsty
;
198 /* Update draw buffer bounds */
199 _mesa_update_state(ctx
);
201 if (ctx
->Depth
.Test
) {
202 /* The blit path produces incorrect results when depth testing is on.
203 * It seems the blit Z coord is always 1.0 (the far plane) so fragments
204 * will likely be obscured by other, closer geometry.
212 if (_mesa_is_bufferobj(unpack
->BufferObj
)) {
213 bitmap
= map_pbo(ctx
, width
, height
, unpack
, bitmap
);
215 return GL_TRUE
; /* even though this is an error, we're done */
218 COPY_4V(tmpColor
, ctx
->Current
.RasterColor
);
220 if (NEED_SECONDARY_COLOR(ctx
)) {
221 ADD_3V(tmpColor
, tmpColor
, ctx
->Current
.RasterSecondaryColor
);
224 UNCLAMPED_FLOAT_TO_UBYTE(ubcolor
[0], tmpColor
[0]);
225 UNCLAMPED_FLOAT_TO_UBYTE(ubcolor
[1], tmpColor
[1]);
226 UNCLAMPED_FLOAT_TO_UBYTE(ubcolor
[2], tmpColor
[2]);
227 UNCLAMPED_FLOAT_TO_UBYTE(ubcolor
[3], tmpColor
[3]);
230 color
= PACK_COLOR_565(ubcolor
[0], ubcolor
[1], ubcolor
[2]);
232 color
= PACK_COLOR_8888(ubcolor
[3], ubcolor
[0], ubcolor
[1], ubcolor
[2]);
234 if (!intel_check_blit_fragment_ops(ctx
, tmpColor
[3] == 1.0F
))
237 intel_prepare_render(intel
);
239 /* Clip to buffer bounds and scissor. */
240 if (!_mesa_clip_to_region(fb
->_Xmin
, fb
->_Ymin
,
241 fb
->_Xmax
, fb
->_Ymax
,
242 &dstx
, &dsty
, &width
, &height
))
245 dsty
= y_flip(fb
, dsty
, height
);
250 /* Chop it all into chunks that can be digested by hardware: */
251 for (py
= 0; py
< height
; py
+= DY
) {
252 for (px
= 0; px
< width
; px
+= DX
) {
253 int h
= MIN2(DY
, height
- py
);
254 int w
= MIN2(DX
, width
- px
);
255 GLuint sz
= ALIGN(ALIGN(w
,8) * h
, 64)/8;
256 GLenum logic_op
= ctx
->Color
.ColorLogicOpEnabled
?
257 ctx
->Color
.LogicOp
: GL_COPY
;
259 assert(sz
<= sizeof(stipple
));
260 memset(stipple
, 0, sz
);
262 /* May need to adjust this when padding has been introduced in
265 * Have to translate destination coordinates back into source
268 if (get_bitmap_rect(bitmap_width
, bitmap_height
, unpack
,
270 -orig_dstx
+ (dstx
+ px
),
271 -orig_dsty
+ y_flip(fb
, dsty
+ py
, h
),
275 fb
->Name
== 0 ? GL_TRUE
: GL_FALSE
) == 0)
278 if (!intelEmitImmediateColorExpandBlit(intel
,
297 if (INTEL_DEBUG
& DEBUG_SYNC
)
298 intel_batchbuffer_flush(intel
->batch
);
300 if (_mesa_is_bufferobj(unpack
->BufferObj
)) {
301 /* done with PBO so unmap it now */
302 ctx
->Driver
.UnmapBuffer(ctx
, GL_PIXEL_UNPACK_BUFFER_EXT
,
306 intel_check_front_buffer_rendering(intel
);
312 intel_texture_bitmap(GLcontext
* ctx
,
313 GLint dst_x
, GLint dst_y
,
314 GLsizei width
, GLsizei height
,
315 const struct gl_pixelstore_attrib
*unpack
,
316 const GLubyte
*bitmap
)
318 struct intel_context
*intel
= intel_context(ctx
);
319 static const char *fp
=
322 "PARAM color=program.local[0];\n"
323 "TEX val, fragment.texcoord[0], texture[0], 2D;\n"
324 "ADD val, val.wwww, {-.5, -.5, -.5, -.5};\n"
326 "MOV result.color, color;\n"
329 GLfloat vertices
[4][4];
330 GLint old_active_texture
;
334 /* We need a fragment program for the KIL effect */
335 if (!ctx
->Extensions
.ARB_fragment_program
||
336 !ctx
->Extensions
.ARB_vertex_program
) {
337 if (INTEL_DEBUG
& DEBUG_FALLBACKS
)
339 "glBitmap fallback: No fragment/vertex program support\n");
343 /* We're going to mess with texturing with no regard to existing texture
344 * state, so if there is some set up we have to bail.
346 if (ctx
->Texture
._EnabledUnits
!= 0) {
347 if (INTEL_DEBUG
& DEBUG_FALLBACKS
)
348 fprintf(stderr
, "glBitmap fallback: texturing enabled\n");
352 /* Can't do textured DrawPixels with a fragment program, unless we were
353 * to generate a new program that sampled our texture and put the results
354 * in the fragment color before the user's program started.
356 if (ctx
->FragmentProgram
.Enabled
) {
357 if (INTEL_DEBUG
& DEBUG_FALLBACKS
)
358 fprintf(stderr
, "glBitmap fallback: fragment program enabled\n");
362 if (ctx
->VertexProgram
.Enabled
) {
363 if (INTEL_DEBUG
& DEBUG_FALLBACKS
)
364 fprintf(stderr
, "glBitmap fallback: vertex program enabled\n");
368 if (!ctx
->Extensions
.ARB_texture_non_power_of_two
&&
369 (!is_power_of_two(width
) || !is_power_of_two(height
))) {
370 if (INTEL_DEBUG
& DEBUG_FALLBACKS
)
372 "glBitmap() fallback: NPOT texture\n");
376 if (ctx
->Fog
.Enabled
) {
377 if (INTEL_DEBUG
& DEBUG_FALLBACKS
)
378 fprintf(stderr
, "glBitmap() fallback: fog\n");
382 /* Check that we can load in a texture this big. */
383 if (width
> (1 << (ctx
->Const
.MaxTextureLevels
- 1)) ||
384 height
> (1 << (ctx
->Const
.MaxTextureLevels
- 1))) {
385 if (INTEL_DEBUG
& DEBUG_FALLBACKS
)
386 fprintf(stderr
, "glBitmap fallback: bitmap too large (%dx%d)\n",
391 if (_mesa_is_bufferobj(unpack
->BufferObj
)) {
392 bitmap
= map_pbo(ctx
, width
, height
, unpack
, bitmap
);
394 return GL_TRUE
; /* even though this is an error, we're done */
397 /* Convert the A1 bitmap to an A8 format suitable for glTexImage */
398 a8_bitmap
= calloc(1, width
* height
);
399 _mesa_expand_bitmap(width
, height
, unpack
, bitmap
, a8_bitmap
, width
, 0xff);
401 if (_mesa_is_bufferobj(unpack
->BufferObj
)) {
402 /* done with PBO so unmap it now */
403 ctx
->Driver
.UnmapBuffer(ctx
, GL_PIXEL_UNPACK_BUFFER_EXT
,
407 /* Save GL state before we start setting up our drawing */
408 _mesa_PushAttrib(GL_ENABLE_BIT
| GL_CURRENT_BIT
| GL_POLYGON_BIT
|
409 GL_TEXTURE_BIT
| GL_VIEWPORT_BIT
);
410 _mesa_PushClientAttrib(GL_CLIENT_VERTEX_ARRAY_BIT
|
411 GL_CLIENT_PIXEL_STORE_BIT
);
412 old_active_texture
= ctx
->Texture
.CurrentUnit
;
414 _mesa_Disable(GL_POLYGON_STIPPLE
);
415 _mesa_PolygonMode(GL_FRONT_AND_BACK
, GL_FILL
);
417 /* Upload our bitmap data to an alpha texture */
418 _mesa_ActiveTextureARB(GL_TEXTURE0_ARB
);
419 _mesa_Enable(GL_TEXTURE_2D
);
420 _mesa_GenTextures(1, &texname
);
421 _mesa_BindTexture(GL_TEXTURE_2D
, texname
);
422 _mesa_TexParameterf(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
423 _mesa_TexParameterf(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
425 _mesa_PixelStorei(GL_UNPACK_SWAP_BYTES
, GL_FALSE
);
426 _mesa_PixelStorei(GL_UNPACK_LSB_FIRST
, GL_FALSE
);
427 _mesa_PixelStorei(GL_UNPACK_ROW_LENGTH
, 0);
428 _mesa_PixelStorei(GL_UNPACK_SKIP_PIXELS
, 0);
429 _mesa_PixelStorei(GL_UNPACK_SKIP_ROWS
, 0);
430 _mesa_PixelStorei(GL_UNPACK_ALIGNMENT
, 1);
431 _mesa_TexImage2D(GL_TEXTURE_2D
, 0, GL_ALPHA
, width
, height
, 0,
432 GL_ALPHA
, GL_UNSIGNED_BYTE
, a8_bitmap
);
435 meta_set_fragment_program(&intel
->meta
, &intel
->meta
.bitmap_fp
, fp
);
436 _mesa_ProgramLocalParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB
, 0,
437 ctx
->Current
.RasterColor
);
438 meta_set_passthrough_vertex_program(&intel
->meta
);
439 meta_set_passthrough_transform(&intel
->meta
);
441 /* convert rasterpos Z from [0,1] to NDC coord in [-1,1] */
442 dst_z
= -1.0 + 2.0 * ctx
->Current
.RasterPos
[2];
444 /* RasterPos[2] already takes into account the DepthRange mapping. */
445 _mesa_DepthRange(0.0, 1.0);
447 vertices
[0][0] = dst_x
;
448 vertices
[0][1] = dst_y
;
449 vertices
[0][2] = dst_z
;
450 vertices
[0][3] = 1.0;
451 vertices
[1][0] = dst_x
+ width
;
452 vertices
[1][1] = dst_y
;
453 vertices
[1][2] = dst_z
;
454 vertices
[1][3] = 1.0;
455 vertices
[2][0] = dst_x
+ width
;
456 vertices
[2][1] = dst_y
+ height
;
457 vertices
[2][2] = dst_z
;
458 vertices
[2][3] = 1.0;
459 vertices
[3][0] = dst_x
;
460 vertices
[3][1] = dst_y
+ height
;
461 vertices
[3][2] = dst_z
;
462 vertices
[3][3] = 1.0;
464 _mesa_VertexPointer(4, GL_FLOAT
, 4 * sizeof(GLfloat
), &vertices
);
465 _mesa_Enable(GL_VERTEX_ARRAY
);
466 meta_set_default_texrect(&intel
->meta
);
467 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
469 meta_restore_texcoords(&intel
->meta
);
470 meta_restore_transform(&intel
->meta
);
471 meta_restore_fragment_program(&intel
->meta
);
472 meta_restore_vertex_program(&intel
->meta
);
474 _mesa_ActiveTextureARB(GL_TEXTURE0_ARB
+ old_active_texture
);
475 _mesa_PopClientAttrib();
478 _mesa_DeleteTextures(1, &texname
);
483 /* There are a large number of possible ways to implement bitmap on
484 * this hardware, most of them have some sort of drawback. Here are a
485 * few that spring to mind:
488 * - XY_MONO_SRC_BLT_CMD
489 * - use XY_SETUP_CLIP_BLT for cliprect clipping.
491 * - XY_TEXT_IMMEDIATE_BLT
492 * - blit per cliprect, subject to maximum immediate data size.
494 * - per pixel or run of pixels
496 * - good for sparse bitmaps
500 * - Translate bitmap to an alpha texture and render as a quad
501 * - Chop bitmap up into 32x32 squares and render w/polygon stipple.
504 intelBitmap(GLcontext
* ctx
,
506 GLsizei width
, GLsizei height
,
507 const struct gl_pixelstore_attrib
*unpack
,
508 const GLubyte
* pixels
)
510 if (do_blit_bitmap(ctx
, x
, y
, width
, height
,
514 if (intel_texture_bitmap(ctx
, x
, y
, width
, height
,
518 if (INTEL_DEBUG
& DEBUG_PIXEL
)
519 printf("%s: fallback to swrast\n", __FUNCTION__
);
521 _swrast_Bitmap(ctx
, x
, y
, width
, height
, unpack
, pixels
);