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/condrender.h"
30 #include "main/context.h"
31 #include "main/format_pack.h"
32 #include "main/image.h"
33 #include "main/imports.h"
34 #include "main/macros.h"
35 #include "main/pack.h"
37 #include "main/pixeltransfer.h"
38 #include "main/state.h"
40 #include "s_context.h"
42 #include "s_stencil.h"
47 * Handle a common case of drawing GL_RGB/GL_UNSIGNED_BYTE into a
48 * MESA_FORMAT_XRGB888 or MESA_FORMAT_ARGB888 renderbuffer.
51 fast_draw_rgb_ubyte_pixels(struct gl_context
*ctx
,
52 struct gl_renderbuffer
*rb
,
54 GLsizei width
, GLsizei height
,
55 const struct gl_pixelstore_attrib
*unpack
,
58 const GLubyte
*src
= (const GLubyte
*)
59 _mesa_image_address2d(unpack
, pixels
, width
,
60 height
, GL_RGB
, GL_UNSIGNED_BYTE
, 0, 0);
61 const GLint srcRowStride
= _mesa_image_row_stride(unpack
, width
,
62 GL_RGB
, GL_UNSIGNED_BYTE
);
67 ctx
->Driver
.MapRenderbuffer(ctx
, rb
, x
, y
, width
, height
,
68 GL_MAP_WRITE_BIT
, &dst
, &dstRowStride
);
71 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glDrawPixels");
75 if (ctx
->Pixel
.ZoomY
== -1.0f
) {
76 dst
= dst
+ (height
- 1) * dstRowStride
;
77 dstRowStride
= -dstRowStride
;
80 for (i
= 0; i
< height
; i
++) {
81 GLuint
*dst4
= (GLuint
*) dst
;
82 for (j
= 0; j
< width
; j
++) {
83 dst4
[j
] = PACK_COLOR_8888(0xff, src
[j
*3+0], src
[j
*3+1], src
[j
*3+2]);
89 ctx
->Driver
.UnmapRenderbuffer(ctx
, rb
);
94 * Handle a common case of drawing GL_RGBA/GL_UNSIGNED_BYTE into a
95 * MESA_FORMAT_ARGB888 or MESA_FORMAT_xRGB888 renderbuffer.
98 fast_draw_rgba_ubyte_pixels(struct gl_context
*ctx
,
99 struct gl_renderbuffer
*rb
,
101 GLsizei width
, GLsizei height
,
102 const struct gl_pixelstore_attrib
*unpack
,
103 const GLvoid
*pixels
)
105 const GLubyte
*src
= (const GLubyte
*)
106 _mesa_image_address2d(unpack
, pixels
, width
,
107 height
, GL_RGBA
, GL_UNSIGNED_BYTE
, 0, 0);
108 const GLint srcRowStride
=
109 _mesa_image_row_stride(unpack
, width
, GL_RGBA
, GL_UNSIGNED_BYTE
);
114 ctx
->Driver
.MapRenderbuffer(ctx
, rb
, x
, y
, width
, height
,
115 GL_MAP_WRITE_BIT
, &dst
, &dstRowStride
);
118 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glDrawPixels");
122 if (ctx
->Pixel
.ZoomY
== -1.0f
) {
123 dst
= dst
+ (height
- 1) * dstRowStride
;
124 dstRowStride
= -dstRowStride
;
127 for (i
= 0; i
< height
; i
++) {
128 GLuint
*dst4
= (GLuint
*) dst
;
129 for (j
= 0; j
< width
; j
++) {
130 dst4
[j
] = PACK_COLOR_8888(src
[j
*4+3], src
[j
*4+0],
131 src
[j
*4+1], src
[j
*4+2]);
137 ctx
->Driver
.UnmapRenderbuffer(ctx
, rb
);
142 * Handle a common case of drawing a format/type combination that
143 * exactly matches the renderbuffer format.
146 fast_draw_generic_pixels(struct gl_context
*ctx
,
147 struct gl_renderbuffer
*rb
,
149 GLsizei width
, GLsizei height
,
150 GLenum format
, GLenum type
,
151 const struct gl_pixelstore_attrib
*unpack
,
152 const GLvoid
*pixels
)
154 const GLubyte
*src
= (const GLubyte
*)
155 _mesa_image_address2d(unpack
, pixels
, width
,
156 height
, format
, type
, 0, 0);
157 const GLint srcRowStride
=
158 _mesa_image_row_stride(unpack
, width
, format
, type
);
159 const GLint rowLength
= width
* _mesa_get_format_bytes(rb
->Format
);
164 ctx
->Driver
.MapRenderbuffer(ctx
, rb
, x
, y
, width
, height
,
165 GL_MAP_WRITE_BIT
, &dst
, &dstRowStride
);
168 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glDrawPixels");
172 if (ctx
->Pixel
.ZoomY
== -1.0f
) {
173 dst
= dst
+ (height
- 1) * dstRowStride
;
174 dstRowStride
= -dstRowStride
;
177 for (i
= 0; i
< height
; i
++) {
178 memcpy(dst
, src
, rowLength
);
183 ctx
->Driver
.UnmapRenderbuffer(ctx
, rb
);
188 * Try to do a fast and simple RGB(a) glDrawPixels.
189 * Return: GL_TRUE if success, GL_FALSE if slow path must be used instead
192 fast_draw_rgba_pixels(struct gl_context
*ctx
, GLint x
, GLint y
,
193 GLsizei width
, GLsizei height
,
194 GLenum format
, GLenum type
,
195 const struct gl_pixelstore_attrib
*userUnpack
,
196 const GLvoid
*pixels
)
198 struct gl_renderbuffer
*rb
= ctx
->DrawBuffer
->_ColorDrawBuffers
[0];
199 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
200 struct gl_pixelstore_attrib unpack
;
203 return GL_TRUE
; /* no-op */
205 if (ctx
->DrawBuffer
->_NumColorDrawBuffers
> 1 ||
206 (swrast
->_RasterMask
& ~CLIP_BIT
) ||
207 ctx
->Texture
._EnabledCoordUnits
||
208 userUnpack
->SwapBytes
||
209 ctx
->Pixel
.ZoomX
!= 1.0f
||
210 fabsf(ctx
->Pixel
.ZoomY
) != 1.0f
||
211 ctx
->_ImageTransferState
) {
212 /* can't handle any of those conditions */
216 unpack
= *userUnpack
;
219 if (!_mesa_clip_drawpixels(ctx
, &x
, &y
, &width
, &height
, &unpack
)) {
220 /* image was completely clipped: no-op, all done */
224 if (format
== GL_RGB
&&
225 type
== GL_UNSIGNED_BYTE
&&
226 (rb
->Format
== MESA_FORMAT_XRGB8888
||
227 rb
->Format
== MESA_FORMAT_ARGB8888
)) {
228 fast_draw_rgb_ubyte_pixels(ctx
, rb
, x
, y
, width
, height
,
233 if (format
== GL_RGBA
&&
234 type
== GL_UNSIGNED_BYTE
&&
235 (rb
->Format
== MESA_FORMAT_XRGB8888
||
236 rb
->Format
== MESA_FORMAT_ARGB8888
)) {
237 fast_draw_rgba_ubyte_pixels(ctx
, rb
, x
, y
, width
, height
,
242 if (_mesa_format_matches_format_and_type(rb
->Format
, format
, type
)) {
243 fast_draw_generic_pixels(ctx
, rb
, x
, y
, width
, height
,
244 format
, type
, &unpack
, pixels
);
248 /* can't handle this pixel format and/or data type */
255 * Draw stencil image.
258 draw_stencil_pixels( struct gl_context
*ctx
, GLint x
, GLint y
,
259 GLsizei width
, GLsizei height
,
261 const struct gl_pixelstore_attrib
*unpack
,
262 const GLvoid
*pixels
)
264 const GLboolean zoom
= ctx
->Pixel
.ZoomX
!= 1.0 || ctx
->Pixel
.ZoomY
!= 1.0;
265 const GLenum destType
= GL_UNSIGNED_BYTE
;
268 /* if width > MAX_WIDTH, have to process image in chunks */
270 while (skipPixels
< width
) {
271 const GLint spanX
= x
+ skipPixels
;
272 const GLint spanWidth
= MIN2(width
- skipPixels
, MAX_WIDTH
);
274 for (row
= 0; row
< height
; row
++) {
275 const GLint spanY
= y
+ row
;
276 GLubyte values
[MAX_WIDTH
];
277 const GLvoid
*source
= _mesa_image_address2d(unpack
, pixels
,
279 GL_STENCIL_INDEX
, type
,
281 _mesa_unpack_stencil_span(ctx
, spanWidth
, destType
, values
,
282 type
, source
, unpack
,
283 ctx
->_ImageTransferState
);
285 _swrast_write_zoomed_stencil_span(ctx
, x
, y
, spanWidth
,
286 spanX
, spanY
, values
);
289 _swrast_write_stencil_span(ctx
, spanWidth
, spanX
, spanY
, values
);
292 skipPixels
+= spanWidth
;
301 draw_depth_pixels( struct gl_context
*ctx
, GLint x
, GLint y
,
302 GLsizei width
, GLsizei height
,
304 const struct gl_pixelstore_attrib
*unpack
,
305 const GLvoid
*pixels
)
307 const GLboolean scaleOrBias
308 = ctx
->Pixel
.DepthScale
!= 1.0 || ctx
->Pixel
.DepthBias
!= 0.0;
309 const GLboolean zoom
= ctx
->Pixel
.ZoomX
!= 1.0 || ctx
->Pixel
.ZoomY
!= 1.0;
312 INIT_SPAN(span
, GL_BITMAP
);
313 span
.arrayMask
= SPAN_Z
;
314 _swrast_span_default_attribs(ctx
, &span
);
316 if (type
== GL_UNSIGNED_SHORT
317 && ctx
->DrawBuffer
->Visual
.depthBits
== 16
320 && width
<= MAX_WIDTH
321 && !unpack
->SwapBytes
) {
322 /* Special case: directly write 16-bit depth values */
324 for (row
= 0; row
< height
; row
++) {
325 const GLushort
*zSrc
= (const GLushort
*)
326 _mesa_image_address2d(unpack
, pixels
, width
, height
,
327 GL_DEPTH_COMPONENT
, type
, row
, 0);
329 for (i
= 0; i
< width
; i
++)
330 span
.array
->z
[i
] = zSrc
[i
];
334 _swrast_write_rgba_span(ctx
, &span
);
337 else if (type
== GL_UNSIGNED_INT
340 && width
<= MAX_WIDTH
341 && !unpack
->SwapBytes
) {
342 /* Special case: shift 32-bit values down to Visual.depthBits */
343 const GLint shift
= 32 - ctx
->DrawBuffer
->Visual
.depthBits
;
345 for (row
= 0; row
< height
; row
++) {
346 const GLuint
*zSrc
= (const GLuint
*)
347 _mesa_image_address2d(unpack
, pixels
, width
, height
,
348 GL_DEPTH_COMPONENT
, type
, row
, 0);
350 memcpy(span
.array
->z
, zSrc
, width
* sizeof(GLuint
));
354 for (col
= 0; col
< width
; col
++)
355 span
.array
->z
[col
] = zSrc
[col
] >> shift
;
360 _swrast_write_rgba_span(ctx
, &span
);
365 const GLuint depthMax
= ctx
->DrawBuffer
->_DepthMax
;
366 GLint skipPixels
= 0;
368 /* in case width > MAX_WIDTH do the copy in chunks */
369 while (skipPixels
< width
) {
370 const GLint spanWidth
= MIN2(width
- skipPixels
, MAX_WIDTH
);
372 ASSERT(span
.end
<= MAX_WIDTH
);
373 for (row
= 0; row
< height
; row
++) {
374 const GLvoid
*zSrc
= _mesa_image_address2d(unpack
,
375 pixels
, width
, height
,
376 GL_DEPTH_COMPONENT
, type
,
379 /* Set these for each row since the _swrast_write_* function may
380 * change them while clipping.
382 span
.x
= x
+ skipPixels
;
384 span
.end
= spanWidth
;
386 _mesa_unpack_depth_span(ctx
, spanWidth
,
387 GL_UNSIGNED_INT
, span
.array
->z
, depthMax
,
390 _swrast_write_zoomed_depth_span(ctx
, x
, y
, &span
);
393 _swrast_write_rgba_span(ctx
, &span
);
396 skipPixels
+= spanWidth
;
407 draw_rgba_pixels( struct gl_context
*ctx
, GLint x
, GLint y
,
408 GLsizei width
, GLsizei height
,
409 GLenum format
, GLenum type
,
410 const struct gl_pixelstore_attrib
*unpack
,
411 const GLvoid
*pixels
)
413 const GLint imgX
= x
, imgY
= y
;
414 const GLboolean zoom
= ctx
->Pixel
.ZoomX
!=1.0 || ctx
->Pixel
.ZoomY
!=1.0;
415 GLfloat
*convImage
= NULL
;
416 GLbitfield transferOps
= ctx
->_ImageTransferState
;
419 /* Try an optimized glDrawPixels first */
420 if (fast_draw_rgba_pixels(ctx
, x
, y
, width
, height
, format
, type
,
425 swrast_render_start(ctx
);
427 INIT_SPAN(span
, GL_BITMAP
);
428 _swrast_span_default_attribs(ctx
, &span
);
429 span
.arrayMask
= SPAN_RGBA
;
430 span
.arrayAttribs
= FRAG_BIT_COL0
; /* we're fill in COL0 attrib values */
432 if (ctx
->DrawBuffer
->_NumColorDrawBuffers
> 0 &&
433 ctx
->DrawBuffer
->_ColorDrawBuffers
[0]->DataType
!= GL_FLOAT
&&
434 ctx
->Color
.ClampFragmentColor
!= GL_FALSE
) {
435 /* need to clamp colors before applying fragment ops */
436 transferOps
|= IMAGE_CLAMP_BIT
;
443 const GLbitfield interpMask
= span
.interpMask
;
444 const GLbitfield arrayMask
= span
.arrayMask
;
445 const GLint srcStride
446 = _mesa_image_row_stride(unpack
, width
, format
, type
);
447 GLint skipPixels
= 0;
448 /* use span array for temp color storage */
449 GLfloat
*rgba
= (GLfloat
*) span
.array
->attribs
[FRAG_ATTRIB_COL0
];
451 /* if the span is wider than MAX_WIDTH we have to do it in chunks */
452 while (skipPixels
< width
) {
453 const GLint spanWidth
= MIN2(width
- skipPixels
, MAX_WIDTH
);
454 const GLubyte
*source
455 = (const GLubyte
*) _mesa_image_address2d(unpack
, pixels
,
456 width
, height
, format
,
457 type
, 0, skipPixels
);
460 for (row
= 0; row
< height
; row
++) {
461 /* get image row as float/RGBA */
462 _mesa_unpack_color_span_float(ctx
, spanWidth
, GL_RGBA
, rgba
,
463 format
, type
, source
, unpack
,
465 /* Set these for each row since the _swrast_write_* functions
466 * may change them while clipping/rendering.
468 span
.array
->ChanType
= GL_FLOAT
;
469 span
.x
= x
+ skipPixels
;
471 span
.end
= spanWidth
;
472 span
.arrayMask
= arrayMask
;
473 span
.interpMask
= interpMask
;
475 _swrast_write_zoomed_rgba_span(ctx
, imgX
, imgY
, &span
, rgba
);
478 _swrast_write_rgba_span(ctx
, &span
);
484 skipPixels
+= spanWidth
;
485 } /* while skipPixels < width */
487 /* XXX this is ugly/temporary, to undo above change */
488 span
.array
->ChanType
= CHAN_TYPE
;
495 swrast_render_finish(ctx
);
500 * Draw depth+stencil values into a MESA_FORAMT_Z24_S8 or MESA_FORMAT_S8_Z24
501 * renderbuffer. No masking, zooming, scaling, etc.
504 fast_draw_depth_stencil(struct gl_context
*ctx
, GLint x
, GLint y
,
505 GLsizei width
, GLsizei height
,
506 const struct gl_pixelstore_attrib
*unpack
,
507 const GLvoid
*pixels
)
509 const GLenum format
= GL_DEPTH_STENCIL_EXT
;
510 const GLenum type
= GL_UNSIGNED_INT_24_8
;
511 struct gl_renderbuffer
*rb
=
512 ctx
->DrawBuffer
->Attachment
[BUFFER_DEPTH
].Renderbuffer
;
514 GLint srcRowStride
, dstRowStride
;
517 src
= _mesa_image_address2d(unpack
, pixels
, width
, height
,
519 srcRowStride
= _mesa_image_row_stride(unpack
, width
, format
, type
);
521 dst
= _swrast_pixel_address(rb
, x
, y
);
522 dstRowStride
= rb
->RowStride
* 4;
524 for (i
= 0; i
< height
; i
++) {
525 _mesa_pack_uint_24_8_depth_stencil_row(rb
->Format
, width
,
526 (const GLuint
*) src
, dst
);
535 * This is a bit different from drawing GL_DEPTH_COMPONENT pixels.
536 * The only per-pixel operations that apply are depth scale/bias,
537 * stencil offset/shift, GL_DEPTH_WRITEMASK and GL_STENCIL_WRITEMASK,
539 * Also, only the depth buffer and stencil buffers are touched, not the
543 draw_depth_stencil_pixels(struct gl_context
*ctx
, GLint x
, GLint y
,
544 GLsizei width
, GLsizei height
, GLenum type
,
545 const struct gl_pixelstore_attrib
*unpack
,
546 const GLvoid
*pixels
)
548 const GLint imgX
= x
, imgY
= y
;
549 const GLboolean scaleOrBias
550 = ctx
->Pixel
.DepthScale
!= 1.0 || ctx
->Pixel
.DepthBias
!= 0.0;
551 const GLuint stencilMask
= ctx
->Stencil
.WriteMask
[0];
552 const GLenum stencilType
= GL_UNSIGNED_BYTE
;
553 const GLboolean zoom
= ctx
->Pixel
.ZoomX
!= 1.0 || ctx
->Pixel
.ZoomY
!= 1.0;
554 struct gl_renderbuffer
*depthRb
, *stencilRb
;
555 struct gl_pixelstore_attrib clippedUnpack
= *unpack
;
558 if (!_mesa_clip_drawpixels(ctx
, &x
, &y
, &width
, &height
,
560 /* totally clipped */
565 depthRb
= ctx
->ReadBuffer
->Attachment
[BUFFER_DEPTH
].Renderbuffer
;
566 stencilRb
= ctx
->ReadBuffer
->Attachment
[BUFFER_STENCIL
].Renderbuffer
;
570 if (depthRb
== stencilRb
&&
571 (depthRb
->Format
== MESA_FORMAT_Z24_S8
||
572 depthRb
->Format
== MESA_FORMAT_S8_Z24
) &&
573 type
== GL_UNSIGNED_INT_24_8
&&
577 (stencilMask
& 0xff) == 0xff) {
578 fast_draw_depth_stencil(ctx
, x
, y
, width
, height
,
579 &clippedUnpack
, pixels
);
582 /* sub-optimal cases:
583 * Separate depth/stencil buffers, or pixel transfer ops required.
585 /* XXX need to handle very wide images (skippixels) */
588 for (i
= 0; i
< height
; i
++) {
589 const GLuint
*depthStencilSrc
= (const GLuint
*)
590 _mesa_image_address2d(&clippedUnpack
, pixels
, width
, height
,
591 GL_DEPTH_STENCIL_EXT
, type
, i
, 0);
593 if (ctx
->Depth
.Mask
) {
594 GLuint zValues
[MAX_WIDTH
]; /* 32-bit Z values */
595 _mesa_unpack_depth_span(ctx
, width
,
596 GL_UNSIGNED_INT
, /* dest type */
597 zValues
, /* dest addr */
598 0xffffffff, /* depth max */
600 depthStencilSrc
, /* src addr */
603 _swrast_write_zoomed_z_span(ctx
, imgX
, imgY
, width
, x
,
607 GLubyte
*dst
= _swrast_pixel_address(depthRb
, x
, y
+ i
);
608 _mesa_pack_uint_z_row(depthRb
->Format
, width
, zValues
, dst
);
612 if (stencilMask
!= 0x0) {
613 GLubyte stencilValues
[MAX_WIDTH
];
614 /* get stencil values, with shift/offset/mapping */
615 _mesa_unpack_stencil_span(ctx
, width
, stencilType
, stencilValues
,
616 type
, depthStencilSrc
, &clippedUnpack
,
617 ctx
->_ImageTransferState
);
619 _swrast_write_zoomed_stencil_span(ctx
, imgX
, imgY
, width
,
620 x
, y
+ i
, stencilValues
);
622 _swrast_write_stencil_span(ctx
, width
, x
, y
+ i
, stencilValues
);
630 * Execute software-based glDrawPixels.
631 * By time we get here, all error checking will have been done.
634 _swrast_DrawPixels( struct gl_context
*ctx
,
636 GLsizei width
, GLsizei height
,
637 GLenum format
, GLenum type
,
638 const struct gl_pixelstore_attrib
*unpack
,
639 const GLvoid
*pixels
)
641 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
642 GLboolean save_vp_override
= ctx
->VertexProgram
._Overriden
;
644 if (!_mesa_check_conditional_render(ctx
))
645 return; /* don't draw */
647 /* We are creating fragments directly, without going through vertex
650 * This override flag tells the fragment processing code that its input
651 * comes from a non-standard source, and it may therefore not rely on
652 * optimizations that assume e.g. constant color if there is no color
655 _mesa_set_vp_override(ctx
, GL_TRUE
);
658 _mesa_update_state(ctx
);
660 if (swrast
->NewState
)
661 _swrast_validate_derived( ctx
);
663 pixels
= _mesa_map_pbo_source(ctx
, unpack
, pixels
);
665 _mesa_set_vp_override(ctx
, save_vp_override
);
670 * By time we get here, all error checking should have been done.
673 case GL_STENCIL_INDEX
:
674 swrast_render_start(ctx
);
675 draw_stencil_pixels( ctx
, x
, y
, width
, height
, type
, unpack
, pixels
);
676 swrast_render_finish(ctx
);
678 case GL_DEPTH_COMPONENT
:
679 swrast_render_start(ctx
);
680 draw_depth_pixels( ctx
, x
, y
, width
, height
, type
, unpack
, pixels
);
681 swrast_render_finish(ctx
);
683 case GL_DEPTH_STENCIL_EXT
:
684 swrast_render_start(ctx
);
685 draw_depth_stencil_pixels(ctx
, x
, y
, width
, height
, type
, unpack
, pixels
);
686 swrast_render_finish(ctx
);
689 /* all other formats should be color formats */
690 draw_rgba_pixels(ctx
, x
, y
, width
, height
, format
, type
, unpack
, pixels
);
693 _mesa_set_vp_override(ctx
, save_vp_override
);
695 _mesa_unmap_pbo_source(ctx
, unpack
);