2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2007 Brian Paul All Rights Reserved.
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 #include "main/glheader.h"
27 #include "main/bufferobj.h"
28 #include "main/colormac.h"
29 #include "main/context.h"
30 #include "main/format_pack.h"
31 #include "main/image.h"
32 #include "main/imports.h"
33 #include "main/macros.h"
34 #include "main/pack.h"
36 #include "main/pixeltransfer.h"
37 #include "main/state.h"
39 #include "s_context.h"
41 #include "s_stencil.h"
46 * Handle a common case of drawing GL_RGB/GL_UNSIGNED_BYTE into a
47 * MESA_FORMAT_XRGB888 or MESA_FORMAT_ARGB888 renderbuffer.
50 fast_draw_rgb_ubyte_pixels(struct gl_context
*ctx
,
51 struct gl_renderbuffer
*rb
,
53 GLsizei width
, GLsizei height
,
54 const struct gl_pixelstore_attrib
*unpack
,
57 const GLubyte
*src
= (const GLubyte
*)
58 _mesa_image_address2d(unpack
, pixels
, width
,
59 height
, GL_RGB
, GL_UNSIGNED_BYTE
, 0, 0);
60 const GLint srcRowStride
= _mesa_image_row_stride(unpack
, width
,
61 GL_RGB
, GL_UNSIGNED_BYTE
);
66 ctx
->Driver
.MapRenderbuffer(ctx
, rb
, x
, y
, width
, height
,
67 GL_MAP_WRITE_BIT
, &dst
, &dstRowStride
);
70 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glDrawPixels");
74 if (ctx
->Pixel
.ZoomY
== -1.0f
) {
75 dst
= dst
+ (height
- 1) * dstRowStride
;
76 dstRowStride
= -dstRowStride
;
79 for (i
= 0; i
< height
; i
++) {
80 GLuint
*dst4
= (GLuint
*) dst
;
81 for (j
= 0; j
< width
; j
++) {
82 dst4
[j
] = PACK_COLOR_8888(0xff, src
[j
*3+0], src
[j
*3+1], src
[j
*3+2]);
88 ctx
->Driver
.UnmapRenderbuffer(ctx
, rb
);
93 * Handle a common case of drawing GL_RGBA/GL_UNSIGNED_BYTE into a
94 * MESA_FORMAT_ARGB888 or MESA_FORMAT_xRGB888 renderbuffer.
97 fast_draw_rgba_ubyte_pixels(struct gl_context
*ctx
,
98 struct gl_renderbuffer
*rb
,
100 GLsizei width
, GLsizei height
,
101 const struct gl_pixelstore_attrib
*unpack
,
102 const GLvoid
*pixels
)
104 const GLubyte
*src
= (const GLubyte
*)
105 _mesa_image_address2d(unpack
, pixels
, width
,
106 height
, GL_RGBA
, GL_UNSIGNED_BYTE
, 0, 0);
107 const GLint srcRowStride
=
108 _mesa_image_row_stride(unpack
, width
, GL_RGBA
, GL_UNSIGNED_BYTE
);
113 ctx
->Driver
.MapRenderbuffer(ctx
, rb
, x
, y
, width
, height
,
114 GL_MAP_WRITE_BIT
, &dst
, &dstRowStride
);
117 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glDrawPixels");
121 if (ctx
->Pixel
.ZoomY
== -1.0f
) {
122 dst
= dst
+ (height
- 1) * dstRowStride
;
123 dstRowStride
= -dstRowStride
;
126 for (i
= 0; i
< height
; i
++) {
127 GLuint
*dst4
= (GLuint
*) dst
;
128 for (j
= 0; j
< width
; j
++) {
129 dst4
[j
] = PACK_COLOR_8888(src
[j
*4+3], src
[j
*4+0],
130 src
[j
*4+1], src
[j
*4+2]);
136 ctx
->Driver
.UnmapRenderbuffer(ctx
, rb
);
141 * Handle a common case of drawing a format/type combination that
142 * exactly matches the renderbuffer format.
145 fast_draw_generic_pixels(struct gl_context
*ctx
,
146 struct gl_renderbuffer
*rb
,
148 GLsizei width
, GLsizei height
,
149 GLenum format
, GLenum type
,
150 const struct gl_pixelstore_attrib
*unpack
,
151 const GLvoid
*pixels
)
153 const GLubyte
*src
= (const GLubyte
*)
154 _mesa_image_address2d(unpack
, pixels
, width
,
155 height
, format
, type
, 0, 0);
156 const GLint srcRowStride
=
157 _mesa_image_row_stride(unpack
, width
, format
, type
);
158 const GLint rowLength
= width
* _mesa_get_format_bytes(rb
->Format
);
163 ctx
->Driver
.MapRenderbuffer(ctx
, rb
, x
, y
, width
, height
,
164 GL_MAP_WRITE_BIT
, &dst
, &dstRowStride
);
167 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glDrawPixels");
171 if (ctx
->Pixel
.ZoomY
== -1.0f
) {
172 dst
= dst
+ (height
- 1) * dstRowStride
;
173 dstRowStride
= -dstRowStride
;
176 for (i
= 0; i
< height
; i
++) {
177 memcpy(dst
, src
, rowLength
);
182 ctx
->Driver
.UnmapRenderbuffer(ctx
, rb
);
187 * Try to do a fast and simple RGB(a) glDrawPixels.
188 * Return: GL_TRUE if success, GL_FALSE if slow path must be used instead
191 fast_draw_rgba_pixels(struct gl_context
*ctx
, GLint x
, GLint y
,
192 GLsizei width
, GLsizei height
,
193 GLenum format
, GLenum type
,
194 const struct gl_pixelstore_attrib
*userUnpack
,
195 const GLvoid
*pixels
)
197 struct gl_renderbuffer
*rb
= ctx
->DrawBuffer
->_ColorDrawBuffer
;
198 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
199 struct gl_pixelstore_attrib unpack
;
202 return GL_TRUE
; /* no-op */
204 if ((swrast
->_RasterMask
& ~CLIP_BIT
) ||
205 ctx
->Texture
._EnabledCoordUnits
||
206 userUnpack
->SwapBytes
||
207 ctx
->Pixel
.ZoomX
!= 1.0f
||
208 fabsf(ctx
->Pixel
.ZoomY
) != 1.0f
||
209 ctx
->_ImageTransferState
) {
210 /* can't handle any of those conditions */
214 unpack
= *userUnpack
;
217 if (!_mesa_clip_drawpixels(ctx
, &x
, &y
, &width
, &height
, &unpack
)) {
218 /* image was completely clipped: no-op, all done */
222 if (format
== GL_RGB
&&
223 type
== GL_UNSIGNED_BYTE
&&
224 (rb
->Format
== MESA_FORMAT_XRGB8888
||
225 rb
->Format
== MESA_FORMAT_ARGB8888
)) {
226 fast_draw_rgb_ubyte_pixels(ctx
, rb
, x
, y
, width
, height
,
231 if (format
== GL_RGBA
&&
232 type
== GL_UNSIGNED_BYTE
&&
233 (rb
->Format
== MESA_FORMAT_XRGB8888
||
234 rb
->Format
== MESA_FORMAT_ARGB8888
)) {
235 fast_draw_rgba_ubyte_pixels(ctx
, rb
, x
, y
, width
, height
,
240 if (_mesa_format_matches_format_and_type(rb
->Format
, format
, type
)) {
241 fast_draw_generic_pixels(ctx
, rb
, x
, y
, width
, height
,
242 format
, type
, &unpack
, pixels
);
246 /* can't handle this pixel format and/or data type */
253 * Draw stencil image.
256 draw_stencil_pixels( struct gl_context
*ctx
, GLint x
, GLint y
,
257 GLsizei width
, GLsizei height
,
259 const struct gl_pixelstore_attrib
*unpack
,
260 const GLvoid
*pixels
)
262 const GLboolean zoom
= ctx
->Pixel
.ZoomX
!= 1.0 || ctx
->Pixel
.ZoomY
!= 1.0;
263 const GLenum destType
= GL_UNSIGNED_BYTE
;
266 /* if width > MAX_WIDTH, have to process image in chunks */
268 while (skipPixels
< width
) {
269 const GLint spanX
= x
+ skipPixels
;
270 const GLint spanWidth
= MIN2(width
- skipPixels
, MAX_WIDTH
);
272 for (row
= 0; row
< height
; row
++) {
273 const GLint spanY
= y
+ row
;
274 GLubyte values
[MAX_WIDTH
];
275 const GLvoid
*source
= _mesa_image_address2d(unpack
, pixels
,
277 GL_STENCIL_INDEX
, type
,
279 _mesa_unpack_stencil_span(ctx
, spanWidth
, destType
, values
,
280 type
, source
, unpack
,
281 ctx
->_ImageTransferState
);
283 _swrast_write_zoomed_stencil_span(ctx
, x
, y
, spanWidth
,
284 spanX
, spanY
, values
);
287 _swrast_write_stencil_span(ctx
, spanWidth
, spanX
, spanY
, values
);
290 skipPixels
+= spanWidth
;
299 draw_depth_pixels( struct gl_context
*ctx
, GLint x
, GLint y
,
300 GLsizei width
, GLsizei height
,
302 const struct gl_pixelstore_attrib
*unpack
,
303 const GLvoid
*pixels
)
305 const GLboolean scaleOrBias
306 = ctx
->Pixel
.DepthScale
!= 1.0 || ctx
->Pixel
.DepthBias
!= 0.0;
307 const GLboolean zoom
= ctx
->Pixel
.ZoomX
!= 1.0 || ctx
->Pixel
.ZoomY
!= 1.0;
310 INIT_SPAN(span
, GL_BITMAP
);
311 span
.arrayMask
= SPAN_Z
;
312 _swrast_span_default_attribs(ctx
, &span
);
314 if (type
== GL_UNSIGNED_SHORT
315 && ctx
->DrawBuffer
->Visual
.depthBits
== 16
318 && width
<= MAX_WIDTH
319 && !unpack
->SwapBytes
) {
320 /* Special case: directly write 16-bit depth values */
322 for (row
= 0; row
< height
; row
++) {
323 const GLushort
*zSrc
= (const GLushort
*)
324 _mesa_image_address2d(unpack
, pixels
, width
, height
,
325 GL_DEPTH_COMPONENT
, type
, row
, 0);
327 for (i
= 0; i
< width
; i
++)
328 span
.array
->z
[i
] = zSrc
[i
];
332 _swrast_write_rgba_span(ctx
, &span
);
335 else if (type
== GL_UNSIGNED_INT
338 && width
<= MAX_WIDTH
339 && !unpack
->SwapBytes
) {
340 /* Special case: shift 32-bit values down to Visual.depthBits */
341 const GLint shift
= 32 - ctx
->DrawBuffer
->Visual
.depthBits
;
343 for (row
= 0; row
< height
; row
++) {
344 const GLuint
*zSrc
= (const GLuint
*)
345 _mesa_image_address2d(unpack
, pixels
, width
, height
,
346 GL_DEPTH_COMPONENT
, type
, row
, 0);
348 memcpy(span
.array
->z
, zSrc
, width
* sizeof(GLuint
));
352 for (col
= 0; col
< width
; col
++)
353 span
.array
->z
[col
] = zSrc
[col
] >> shift
;
358 _swrast_write_rgba_span(ctx
, &span
);
363 const GLuint depthMax
= ctx
->DrawBuffer
->_DepthMax
;
364 GLint skipPixels
= 0;
366 /* in case width > MAX_WIDTH do the copy in chunks */
367 while (skipPixels
< width
) {
368 const GLint spanWidth
= MIN2(width
- skipPixels
, MAX_WIDTH
);
370 ASSERT(span
.end
<= MAX_WIDTH
);
371 for (row
= 0; row
< height
; row
++) {
372 const GLvoid
*zSrc
= _mesa_image_address2d(unpack
,
373 pixels
, width
, height
,
374 GL_DEPTH_COMPONENT
, type
,
377 /* Set these for each row since the _swrast_write_* function may
378 * change them while clipping.
380 span
.x
= x
+ skipPixels
;
382 span
.end
= spanWidth
;
384 _mesa_unpack_depth_span(ctx
, spanWidth
,
385 GL_UNSIGNED_INT
, span
.array
->z
, depthMax
,
388 _swrast_write_zoomed_depth_span(ctx
, x
, y
, &span
);
391 _swrast_write_rgba_span(ctx
, &span
);
394 skipPixels
+= spanWidth
;
405 draw_rgba_pixels( struct gl_context
*ctx
, GLint x
, GLint y
,
406 GLsizei width
, GLsizei height
,
407 GLenum format
, GLenum type
,
408 const struct gl_pixelstore_attrib
*unpack
,
409 const GLvoid
*pixels
)
411 const GLint imgX
= x
, imgY
= y
;
412 const GLboolean zoom
= ctx
->Pixel
.ZoomX
!=1.0 || ctx
->Pixel
.ZoomY
!=1.0;
413 GLfloat
*convImage
= NULL
;
414 GLbitfield transferOps
= ctx
->_ImageTransferState
;
417 /* Try an optimized glDrawPixels first */
418 if (fast_draw_rgba_pixels(ctx
, x
, y
, width
, height
, format
, type
,
423 swrast_render_start(ctx
);
425 INIT_SPAN(span
, GL_BITMAP
);
426 _swrast_span_default_attribs(ctx
, &span
);
427 span
.arrayMask
= SPAN_RGBA
;
428 span
.arrayAttribs
= FRAG_BIT_COL0
; /* we're fill in COL0 attrib values */
430 if (_mesa_get_format_datatype(ctx
->DrawBuffer
->_ColorDrawBuffer
->Format
) != GL_FLOAT
&&
431 ctx
->Color
.ClampFragmentColor
!= GL_FALSE
) {
432 /* need to clamp colors before applying fragment ops */
433 transferOps
|= IMAGE_CLAMP_BIT
;
440 const GLbitfield interpMask
= span
.interpMask
;
441 const GLbitfield arrayMask
= span
.arrayMask
;
442 const GLint srcStride
443 = _mesa_image_row_stride(unpack
, width
, format
, type
);
444 GLint skipPixels
= 0;
445 /* use span array for temp color storage */
446 GLfloat
*rgba
= (GLfloat
*) span
.array
->attribs
[FRAG_ATTRIB_COL0
];
448 /* if the span is wider than MAX_WIDTH we have to do it in chunks */
449 while (skipPixels
< width
) {
450 const GLint spanWidth
= MIN2(width
- skipPixels
, MAX_WIDTH
);
451 const GLubyte
*source
452 = (const GLubyte
*) _mesa_image_address2d(unpack
, pixels
,
453 width
, height
, format
,
454 type
, 0, skipPixels
);
457 for (row
= 0; row
< height
; row
++) {
458 /* get image row as float/RGBA */
459 _mesa_unpack_color_span_float(ctx
, spanWidth
, GL_RGBA
, rgba
,
460 format
, type
, source
, unpack
,
462 /* Set these for each row since the _swrast_write_* functions
463 * may change them while clipping/rendering.
465 span
.array
->ChanType
= GL_FLOAT
;
466 span
.x
= x
+ skipPixels
;
468 span
.end
= spanWidth
;
469 span
.arrayMask
= arrayMask
;
470 span
.interpMask
= interpMask
;
472 _swrast_write_zoomed_rgba_span(ctx
, imgX
, imgY
, &span
, rgba
);
475 _swrast_write_rgba_span(ctx
, &span
);
481 skipPixels
+= spanWidth
;
482 } /* while skipPixels < width */
484 /* XXX this is ugly/temporary, to undo above change */
485 span
.array
->ChanType
= CHAN_TYPE
;
492 swrast_render_finish(ctx
);
496 * Execute software-based glDrawPixels.
497 * By time we get here, all error checking will have been done.
500 _swrast_DrawPixels( struct gl_context
*ctx
,
502 GLsizei width
, GLsizei height
,
503 GLenum format
, GLenum type
,
504 const struct gl_pixelstore_attrib
*unpack
,
505 const GLvoid
*pixels
)
507 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
508 GLboolean save_vp_override
= ctx
->VertexProgram
._Overriden
;
510 /* We are creating fragments directly, without going through vertex
513 * This override flag tells the fragment processing code that its input
514 * comes from a non-standard source, and it may therefore not rely on
515 * optimizations that assume e.g. constant color if there is no color
518 _mesa_set_vp_override(ctx
, GL_TRUE
);
521 _mesa_update_state(ctx
);
523 if (swrast
->NewState
)
524 _swrast_validate_derived( ctx
);
526 pixels
= _mesa_map_pbo_source(ctx
, unpack
, pixels
);
528 _mesa_set_vp_override(ctx
, save_vp_override
);
533 * By time we get here, all error checking should have been done.
536 case GL_STENCIL_INDEX
:
537 swrast_render_start(ctx
);
538 draw_stencil_pixels( ctx
, x
, y
, width
, height
, type
, unpack
, pixels
);
539 swrast_render_finish(ctx
);
541 case GL_DEPTH_COMPONENT
:
542 swrast_render_start(ctx
);
543 draw_depth_pixels( ctx
, x
, y
, width
, height
, type
, unpack
, pixels
);
544 swrast_render_finish(ctx
);
547 /* all other formats should be color formats */
548 draw_rgba_pixels(ctx
, x
, y
, width
, height
, format
, type
, unpack
, pixels
);
551 _mesa_set_vp_override(ctx
, save_vp_override
);
553 _mesa_unmap_pbo_source(ctx
, unpack
);