2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2006 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.
27 #include "bufferobj.h"
36 #include "s_context.h"
37 #include "s_drawpix.h"
39 #include "s_stencil.h"
45 * Try to do a fast and simple RGB(a) glDrawPixels.
46 * Return: GL_TRUE if success, GL_FALSE if slow path must be used instead
49 fast_draw_rgba_pixels(GLcontext
*ctx
, GLint x
, GLint y
,
50 GLsizei width
, GLsizei height
,
51 GLenum format
, GLenum type
,
52 const struct gl_pixelstore_attrib
*userUnpack
,
55 const GLint imgX
= x
, imgY
= y
;
56 struct gl_renderbuffer
*rb
= ctx
->DrawBuffer
->_ColorDrawBuffers
[0][0];
57 const GLenum rbType
= rb
->DataType
;
58 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
61 GLint yStep
; /* +1 or -1 */
62 struct gl_pixelstore_attrib unpack
;
63 GLint destX
, destY
, drawWidth
, drawHeight
; /* post clipping */
65 if ((swrast
->_RasterMask
& ~CLIP_BIT
) ||
66 ctx
->Texture
._EnabledCoordUnits
||
67 userUnpack
->SwapBytes
||
68 ctx
->_ImageTransferState
) {
69 /* can't handle any of those conditions */
73 INIT_SPAN(span
, GL_BITMAP
, 0, 0, SPAN_RGBA
);
75 _swrast_span_default_z(ctx
, &span
);
76 if (swrast
->_FogEnabled
)
77 _swrast_span_default_fog(ctx
, &span
);
78 if (ctx
->Texture
._EnabledCoordUnits
)
79 _swrast_span_default_texcoords(ctx
, &span
);
81 /* copy input params since clipping may change them */
88 /* check for simple zooming and clipping */
89 if (ctx
->Pixel
.ZoomX
== 1.0F
&&
90 (ctx
->Pixel
.ZoomY
== 1.0F
|| ctx
->Pixel
.ZoomY
== -1.0F
)) {
91 if (!_mesa_clip_drawpixels(ctx
, &destX
, &destY
,
92 &drawWidth
, &drawHeight
, &unpack
)) {
93 /* image was completely clipped: no-op, all done */
97 yStep
= (GLint
) ctx
->Pixel
.ZoomY
;
98 ASSERT(yStep
== 1 || yStep
== -1);
101 /* non-simple zooming */
102 simpleZoom
= GL_FALSE
;
104 if (unpack
.RowLength
== 0)
105 unpack
.RowLength
= width
;
112 if (format
== GL_RGBA
&& type
== rbType
) {
113 const GLubyte
*src
= _mesa_image_address2d(&unpack
, pixels
, width
,
114 height
, format
, type
, 0, 0);
115 const GLint srcStride
= _mesa_image_row_stride(&unpack
, width
,
119 for (row
= 0; row
< drawHeight
; row
++) {
120 rb
->PutRow(ctx
, rb
, drawWidth
, destX
, destY
, src
, NULL
);
128 for (row
= 0; row
< drawHeight
; row
++) {
130 span
.y
= destY
+ row
;
131 span
.end
= drawWidth
;
132 span
.array
->ChanType
= rbType
;
133 _swrast_write_zoomed_rgba_span(ctx
, imgX
, imgY
, &span
, src
);
136 span
.array
->ChanType
= CHAN_TYPE
;
141 if (format
== GL_RGB
&& type
== rbType
) {
142 const GLubyte
*src
= _mesa_image_address2d(&unpack
, pixels
, width
,
143 height
, format
, type
, 0, 0);
144 const GLint srcStride
= _mesa_image_row_stride(&unpack
, width
,
148 for (row
= 0; row
< drawHeight
; row
++) {
149 rb
->PutRowRGB(ctx
, rb
, drawWidth
, destX
, destY
, src
, NULL
);
157 for (row
= 0; row
< drawHeight
; row
++) {
160 span
.end
= drawWidth
;
161 span
.array
->ChanType
= rbType
;
162 _swrast_write_zoomed_rgb_span(ctx
, imgX
, imgY
, &span
, src
);
166 span
.array
->ChanType
= CHAN_TYPE
;
171 /* Remaining cases haven't been tested with alignment != 1 */
172 if (userUnpack
->Alignment
!= 1)
175 if (format
== GL_LUMINANCE
&& type
== CHAN_TYPE
&& rbType
== CHAN_TYPE
) {
176 const GLchan
*src
= (const GLchan
*) pixels
177 + (unpack
.SkipRows
* unpack
.RowLength
+ unpack
.SkipPixels
);
181 ASSERT(drawWidth
<= MAX_WIDTH
);
182 for (row
= 0; row
< drawHeight
; row
++) {
183 GLchan rgb
[MAX_WIDTH
][3];
185 for (i
= 0;i
<drawWidth
;i
++) {
190 rb
->PutRowRGB(ctx
, rb
, drawWidth
, destX
, destY
, rgb
, NULL
);
191 src
+= unpack
.RowLength
;
198 ASSERT(drawWidth
<= MAX_WIDTH
);
199 for (row
= 0; row
< drawHeight
; row
++) {
200 GLchan rgb
[MAX_WIDTH
][3];
202 for (i
= 0;i
<drawWidth
;i
++) {
209 span
.end
= drawWidth
;
210 _swrast_write_zoomed_rgb_span(ctx
, imgX
, imgY
, &span
, rgb
);
211 src
+= unpack
.RowLength
;
218 if (format
== GL_LUMINANCE_ALPHA
&& type
== CHAN_TYPE
&& rbType
== CHAN_TYPE
) {
219 const GLchan
*src
= (const GLchan
*) pixels
220 + (unpack
.SkipRows
* unpack
.RowLength
+ unpack
.SkipPixels
)*2;
223 ASSERT(drawWidth
<= MAX_WIDTH
);
224 for (row
= 0; row
< drawHeight
; row
++) {
226 const GLchan
*ptr
= src
;
227 for (i
= 0;i
<drawWidth
;i
++) {
228 span
.array
->rgba
[i
][0] = *ptr
;
229 span
.array
->rgba
[i
][1] = *ptr
;
230 span
.array
->rgba
[i
][2] = *ptr
++;
231 span
.array
->rgba
[i
][3] = *ptr
++;
233 rb
->PutRow(ctx
, rb
, drawWidth
, destX
, destY
,
234 span
.array
->rgba
, NULL
);
235 src
+= unpack
.RowLength
*2;
242 ASSERT(drawWidth
<= MAX_WIDTH
);
243 for (row
= 0; row
< drawHeight
; row
++) {
244 const GLchan
*ptr
= src
;
246 for (i
= 0;i
<drawWidth
;i
++) {
247 span
.array
->rgba
[i
][0] = *ptr
;
248 span
.array
->rgba
[i
][1] = *ptr
;
249 span
.array
->rgba
[i
][2] = *ptr
++;
250 span
.array
->rgba
[i
][3] = *ptr
++;
254 span
.end
= drawWidth
;
255 _swrast_write_zoomed_rgba_span(ctx
, imgX
, imgY
, &span
,
257 src
+= unpack
.RowLength
*2;
264 if (format
== GL_COLOR_INDEX
&& type
== GL_UNSIGNED_BYTE
) {
265 const GLubyte
*src
= (const GLubyte
*) pixels
266 + unpack
.SkipRows
* unpack
.RowLength
+ unpack
.SkipPixels
;
267 if (ctx
->Visual
.rgbMode
&& rbType
== GL_UNSIGNED_BYTE
) {
268 /* convert ubyte/CI data to ubyte/RGBA */
271 for (row
= 0; row
< drawHeight
; row
++) {
272 ASSERT(drawWidth
<= MAX_WIDTH
);
273 _mesa_map_ci8_to_rgba8(ctx
, drawWidth
, src
,
274 span
.array
->color
.sz1
.rgba
);
275 rb
->PutRow(ctx
, rb
, drawWidth
, destX
, destY
,
276 span
.array
->color
.sz1
.rgba
, NULL
);
277 src
+= unpack
.RowLength
;
282 /* ubyte/CI to ubyte/RGBA with zooming */
284 for (row
= 0; row
< drawHeight
; row
++) {
285 ASSERT(drawWidth
<= MAX_WIDTH
);
286 _mesa_map_ci8_to_rgba8(ctx
, drawWidth
, src
,
287 span
.array
->color
.sz1
.rgba
);
290 span
.end
= drawWidth
;
291 _swrast_write_zoomed_rgba_span(ctx
, imgX
, imgY
, &span
,
292 span
.array
->color
.sz1
.rgba
);
293 src
+= unpack
.RowLength
;
299 else if (!ctx
->Visual
.rgbMode
&& rbType
== GL_UNSIGNED_INT
) {
300 /* write CI data to CI frame buffer */
303 for (row
= 0; row
< drawHeight
; row
++) {
304 GLuint index32
[MAX_WIDTH
];
306 for (col
= 0; col
< drawWidth
; col
++)
307 index32
[col
] = src
[col
];
308 rb
->PutRow(ctx
, rb
, drawWidth
, destX
, destY
, index32
, NULL
);
309 src
+= unpack
.RowLength
;
317 /* can't handle this pixel format and/or data type */
324 * Draw color index image.
327 draw_index_pixels( GLcontext
*ctx
, GLint x
, GLint y
,
328 GLsizei width
, GLsizei height
,
330 const struct gl_pixelstore_attrib
*unpack
,
331 const GLvoid
*pixels
)
333 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
334 const GLint imgX
= x
, imgY
= y
;
335 const GLboolean zoom
= ctx
->Pixel
.ZoomX
!=1.0 || ctx
->Pixel
.ZoomY
!=1.0;
336 GLint row
, skipPixels
;
339 INIT_SPAN(span
, GL_BITMAP
, 0, 0, SPAN_INDEX
);
342 _swrast_span_default_z(ctx
, &span
);
343 if (swrast
->_FogEnabled
)
344 _swrast_span_default_fog(ctx
, &span
);
350 while (skipPixels
< width
) {
351 const GLint spanWidth
= MIN2(width
- skipPixels
, MAX_WIDTH
);
352 ASSERT(spanWidth
<= MAX_WIDTH
);
353 for (row
= 0; row
< height
; row
++) {
354 const GLvoid
*source
= _mesa_image_address2d(unpack
, pixels
,
356 GL_COLOR_INDEX
, type
,
358 _mesa_unpack_index_span(ctx
, spanWidth
, GL_UNSIGNED_INT
,
359 span
.array
->index
, type
, source
, unpack
,
360 ctx
->_ImageTransferState
);
362 /* These may get changed during writing/clipping */
363 span
.x
= x
+ skipPixels
;
365 span
.end
= spanWidth
;
368 _swrast_write_zoomed_index_span(ctx
, imgX
, imgY
, &span
);
370 _swrast_write_index_span(ctx
, &span
);
372 skipPixels
+= spanWidth
;
379 * Draw stencil image.
382 draw_stencil_pixels( GLcontext
*ctx
, GLint x
, GLint y
,
383 GLsizei width
, GLsizei height
,
385 const struct gl_pixelstore_attrib
*unpack
,
386 const GLvoid
*pixels
)
388 const GLboolean zoom
= ctx
->Pixel
.ZoomX
!= 1.0 || ctx
->Pixel
.ZoomY
!= 1.0;
391 /* if width > MAX_WIDTH, have to process image in chunks */
393 while (skipPixels
< width
) {
394 const GLint spanX
= x
+ skipPixels
;
395 const GLint spanWidth
= MIN2(width
- skipPixels
, MAX_WIDTH
);
397 for (row
= 0; row
< height
; row
++) {
398 const GLint spanY
= y
+ row
;
399 GLstencil values
[MAX_WIDTH
];
400 GLenum destType
= (sizeof(GLstencil
) == sizeof(GLubyte
))
401 ? GL_UNSIGNED_BYTE
: GL_UNSIGNED_SHORT
;
402 const GLvoid
*source
= _mesa_image_address2d(unpack
, pixels
,
404 GL_COLOR_INDEX
, type
,
406 _mesa_unpack_index_span(ctx
, spanWidth
, destType
, values
,
407 type
, source
, unpack
,
408 ctx
->_ImageTransferState
);
409 _mesa_apply_stencil_transfer_ops(ctx
, spanWidth
, values
);
411 _swrast_write_zoomed_stencil_span(ctx
, x
, y
, spanWidth
,
412 spanX
, spanY
, values
);
415 _swrast_write_stencil_span(ctx
, spanWidth
, spanX
, spanY
, values
);
418 skipPixels
+= spanWidth
;
427 draw_depth_pixels( GLcontext
*ctx
, GLint x
, GLint y
,
428 GLsizei width
, GLsizei height
,
430 const struct gl_pixelstore_attrib
*unpack
,
431 const GLvoid
*pixels
)
433 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
434 const GLboolean scaleOrBias
435 = ctx
->Pixel
.DepthScale
!= 1.0 || ctx
->Pixel
.DepthBias
!= 0.0;
436 const GLboolean zoom
= ctx
->Pixel
.ZoomX
!= 1.0 || ctx
->Pixel
.ZoomY
!= 1.0;
439 INIT_SPAN(span
, GL_BITMAP
, 0, 0, SPAN_Z
);
441 _swrast_span_default_color(ctx
, &span
);
443 if (swrast
->_FogEnabled
)
444 _swrast_span_default_fog(ctx
, &span
);
445 if (ctx
->Texture
._EnabledCoordUnits
)
446 _swrast_span_default_texcoords(ctx
, &span
);
448 if (type
== GL_UNSIGNED_SHORT
449 && ctx
->DrawBuffer
->Visual
.depthBits
== 16
452 && ctx
->Visual
.rgbMode
453 && width
<= MAX_WIDTH
) {
454 /* Special case: directly write 16-bit depth values */
456 for (row
= 0; row
< height
; row
++) {
457 const GLushort
*zSrc
= (const GLushort
*)
458 _mesa_image_address2d(unpack
, pixels
, width
, height
,
459 GL_DEPTH_COMPONENT
, type
, row
, 0);
461 for (i
= 0; i
< width
; i
++)
462 span
.array
->z
[i
] = zSrc
[i
];
466 _swrast_write_rgba_span(ctx
, &span
);
469 else if (type
== GL_UNSIGNED_INT
472 && ctx
->Visual
.rgbMode
473 && width
<= MAX_WIDTH
) {
474 /* Special case: shift 32-bit values down to Visual.depthBits */
475 const GLint shift
= 32 - ctx
->DrawBuffer
->Visual
.depthBits
;
477 for (row
= 0; row
< height
; row
++) {
478 const GLuint
*zSrc
= (const GLuint
*)
479 _mesa_image_address2d(unpack
, pixels
, width
, height
,
480 GL_DEPTH_COMPONENT
, type
, row
, 0);
482 _mesa_memcpy(span
.array
->z
, zSrc
, width
* sizeof(GLuint
));
486 for (col
= 0; col
< width
; col
++)
487 span
.array
->z
[col
] = zSrc
[col
] >> shift
;
492 _swrast_write_rgba_span(ctx
, &span
);
497 const GLfloat depthMax
= ctx
->DrawBuffer
->_DepthMaxF
;
498 GLint skipPixels
= 0;
500 /* in case width > MAX_WIDTH do the copy in chunks */
501 while (skipPixels
< width
) {
502 const GLint spanWidth
= MIN2(width
- skipPixels
, MAX_WIDTH
);
504 ASSERT(span
.end
<= MAX_WIDTH
);
505 for (row
= 0; row
< height
; row
++) {
506 const GLvoid
*zSrc
= _mesa_image_address2d(unpack
,
507 pixels
, width
, height
,
508 GL_DEPTH_COMPONENT
, type
,
511 /* Set these for each row since the _swrast_write_* function may
512 * change them while clipping.
514 span
.x
= x
+ skipPixels
;
516 span
.end
= spanWidth
;
518 _mesa_unpack_depth_span(ctx
, spanWidth
,
519 GL_UNSIGNED_INT
, span
.array
->z
, depthMax
,
522 _swrast_write_zoomed_depth_span(ctx
, x
, y
, &span
);
524 else if (ctx
->Visual
.rgbMode
) {
525 _swrast_write_rgba_span(ctx
, &span
);
528 _swrast_write_index_span(ctx
, &span
);
531 skipPixels
+= spanWidth
;
542 draw_rgba_pixels( GLcontext
*ctx
, GLint x
, GLint y
,
543 GLsizei width
, GLsizei height
,
544 GLenum format
, GLenum type
,
545 const struct gl_pixelstore_attrib
*unpack
,
546 const GLvoid
*pixels
)
548 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
549 const GLint imgX
= x
, imgY
= y
;
550 const GLboolean zoom
= ctx
->Pixel
.ZoomX
!=1.0 || ctx
->Pixel
.ZoomY
!=1.0;
551 GLfloat
*convImage
= NULL
;
552 GLbitfield transferOps
= ctx
->_ImageTransferState
;
555 /* Try an optimized glDrawPixels first */
556 if (fast_draw_rgba_pixels(ctx
, x
, y
, width
, height
, format
, type
,
560 INIT_SPAN(span
, GL_BITMAP
, 0, 0, SPAN_RGBA
);
562 _swrast_span_default_z(ctx
, &span
);
563 if (swrast
->_FogEnabled
)
564 _swrast_span_default_fog(ctx
, &span
);
565 if (ctx
->Texture
._EnabledCoordUnits
)
566 _swrast_span_default_texcoords(ctx
, &span
);
568 if (ctx
->Pixel
.Convolution2DEnabled
|| ctx
->Pixel
.Separable2DEnabled
) {
569 /* Convolution has to be handled specially. We'll create an
570 * intermediate image, applying all pixel transfer operations
571 * up to convolution. Then we'll convolve the image. Then
572 * we'll proceed with the rest of the transfer operations and
573 * rasterize the image.
576 GLfloat
*dest
, *tmpImage
;
578 tmpImage
= (GLfloat
*) _mesa_malloc(width
* height
* 4 * sizeof(GLfloat
));
580 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glDrawPixels");
583 convImage
= (GLfloat
*) _mesa_malloc(width
* height
* 4 * sizeof(GLfloat
));
585 _mesa_free(tmpImage
);
586 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glDrawPixels");
590 /* Unpack the image and apply transfer ops up to convolution */
592 for (row
= 0; row
< height
; row
++) {
593 const GLvoid
*source
= _mesa_image_address2d(unpack
,
594 pixels
, width
, height
, format
, type
, row
, 0);
595 _mesa_unpack_color_span_float(ctx
, width
, GL_RGBA
, (GLfloat
*) dest
,
596 format
, type
, source
, unpack
,
597 transferOps
& IMAGE_PRE_CONVOLUTION_BITS
);
602 if (ctx
->Pixel
.Convolution2DEnabled
) {
603 _mesa_convolve_2d_image(ctx
, &width
, &height
, tmpImage
, convImage
);
606 ASSERT(ctx
->Pixel
.Separable2DEnabled
);
607 _mesa_convolve_sep_image(ctx
, &width
, &height
, tmpImage
, convImage
);
609 _mesa_free(tmpImage
);
611 /* continue transfer ops and draw the convolved image */
612 unpack
= &ctx
->DefaultPacking
;
616 transferOps
&= IMAGE_POST_CONVOLUTION_BITS
;
619 if (ctx
->DrawBuffer
->_NumColorDrawBuffers
[0] > 0 &&
620 ctx
->DrawBuffer
->_ColorDrawBuffers
[0][0]->DataType
!= GL_FLOAT
&&
621 ctx
->Color
.ClampFragmentColor
!= GL_FALSE
) {
622 /* need to clamp colors before applying fragment ops */
623 transferOps
|= IMAGE_CLAMP_BIT
;
630 const GLboolean sink
= (ctx
->Pixel
.MinMaxEnabled
&& ctx
->MinMax
.Sink
)
631 || (ctx
->Pixel
.HistogramEnabled
&& ctx
->Histogram
.Sink
);
632 const GLbitfield interpMask
= span
.interpMask
;
633 const GLbitfield arrayMask
= span
.arrayMask
;
634 const GLint srcStride
635 = _mesa_image_row_stride(unpack
, width
, format
, type
);
636 GLint skipPixels
= 0;
637 /* use span array for temp color storage */
638 GLfloat
*rgba
= (GLfloat
*) span
.array
->color
.sz4
.rgba
;
640 /* if the span is wider than MAX_WIDTH we have to do it in chunks */
641 while (skipPixels
< width
) {
642 const GLint spanWidth
= MIN2(width
- skipPixels
, MAX_WIDTH
);
643 const GLubyte
*source
= _mesa_image_address2d(unpack
, pixels
,
644 width
, height
, format
, type
, 0, skipPixels
);
647 for (row
= 0; row
< height
; row
++) {
648 /* get image row as float/RGBA */
649 _mesa_unpack_color_span_float(ctx
, spanWidth
, GL_RGBA
, rgba
,
650 format
, type
, source
, unpack
,
654 /* Set these for each row since the _swrast_write_* functions
655 * may change them while clipping/rendering.
657 span
.array
->ChanType
= GL_FLOAT
;
658 span
.x
= x
+ skipPixels
;
660 span
.end
= spanWidth
;
661 span
.arrayMask
= arrayMask
;
662 span
.interpMask
= interpMask
;
664 _swrast_write_zoomed_rgba_span(ctx
, imgX
, imgY
, &span
, rgba
);
667 _swrast_write_rgba_span(ctx
, &span
);
674 skipPixels
+= spanWidth
;
675 } /* while skipPixels < width */
677 /* XXX this is ugly/temporary, to undo above change */
678 span
.array
->ChanType
= CHAN_TYPE
;
682 _mesa_free(convImage
);
688 * This is a bit different from drawing GL_DEPTH_COMPONENT pixels.
689 * The only per-pixel operations that apply are depth scale/bias,
690 * stencil offset/shift, GL_DEPTH_WRITEMASK and GL_STENCIL_WRITEMASK,
692 * Also, only the depth buffer and stencil buffers are touched, not the
696 draw_depth_stencil_pixels(GLcontext
*ctx
, GLint x
, GLint y
,
697 GLsizei width
, GLsizei height
, GLenum type
,
698 const struct gl_pixelstore_attrib
*unpack
,
699 const GLvoid
*pixels
)
701 const GLint imgX
= x
, imgY
= y
;
702 const GLboolean scaleOrBias
703 = ctx
->Pixel
.DepthScale
!= 1.0 || ctx
->Pixel
.DepthBias
!= 0.0;
704 const GLfloat depthScale
= ctx
->DrawBuffer
->_DepthMaxF
;
705 const GLuint stencilMask
= ctx
->Stencil
.WriteMask
[0];
706 const GLuint stencilType
= (STENCIL_BITS
== 8) ?
707 GL_UNSIGNED_BYTE
: GL_UNSIGNED_SHORT
;
708 const GLboolean zoom
= ctx
->Pixel
.ZoomX
!= 1.0 || ctx
->Pixel
.ZoomY
!= 1.0;
709 struct gl_renderbuffer
*depthRb
, *stencilRb
;
710 struct gl_pixelstore_attrib clippedUnpack
= *unpack
;
713 if (!_mesa_clip_drawpixels(ctx
, &x
, &y
, &width
, &height
,
715 /* totally clipped */
720 depthRb
= ctx
->ReadBuffer
->Attachment
[BUFFER_DEPTH
].Renderbuffer
;
721 stencilRb
= ctx
->ReadBuffer
->Attachment
[BUFFER_STENCIL
].Renderbuffer
;
725 if (depthRb
->_BaseFormat
== GL_DEPTH_STENCIL_EXT
&&
726 stencilRb
->_BaseFormat
== GL_DEPTH_STENCIL_EXT
&&
727 depthRb
== stencilRb
&&
731 (stencilMask
& 0xff) == 0xff) {
732 /* This is the ideal case.
733 * Drawing GL_DEPTH_STENCIL pixels into a combined depth/stencil buffer.
734 * Plus, no pixel transfer ops, zooming, or masking needed.
737 for (i
= 0; i
< height
; i
++) {
738 const GLuint
*src
= (const GLuint
*)
739 _mesa_image_address2d(&clippedUnpack
, pixels
, width
, height
,
740 GL_DEPTH_STENCIL_EXT
, type
, i
, 0);
741 depthRb
->PutRow(ctx
, depthRb
, width
, x
, y
+ i
, src
, NULL
);
745 /* sub-optimal cases:
746 * Separate depth/stencil buffers, or pixel transfer ops required.
748 /* XXX need to handle very wide images (skippixels) */
751 depthRb
= ctx
->DrawBuffer
->_DepthBuffer
;
752 stencilRb
= ctx
->DrawBuffer
->_StencilBuffer
;
754 for (i
= 0; i
< height
; i
++) {
755 const GLuint
*depthStencilSrc
= (const GLuint
*)
756 _mesa_image_address2d(&clippedUnpack
, pixels
, width
, height
,
757 GL_DEPTH_STENCIL_EXT
, type
, i
, 0);
759 if (ctx
->Depth
.Mask
) {
760 if (!scaleOrBias
&& ctx
->DrawBuffer
->Visual
.depthBits
== 24) {
761 /* fast path 24-bit zbuffer */
762 GLuint zValues
[MAX_WIDTH
];
764 ASSERT(depthRb
->DataType
== GL_UNSIGNED_INT
);
765 for (j
= 0; j
< width
; j
++) {
766 zValues
[j
] = depthStencilSrc
[j
] >> 8;
769 _swrast_write_zoomed_z_span(ctx
, imgX
, imgY
, width
,
772 depthRb
->PutRow(ctx
, depthRb
, width
, x
, y
+ i
, zValues
,NULL
);
774 else if (!scaleOrBias
&& ctx
->DrawBuffer
->Visual
.depthBits
== 16) {
775 /* fast path 16-bit zbuffer */
776 GLushort zValues
[MAX_WIDTH
];
778 ASSERT(depthRb
->DataType
== GL_UNSIGNED_SHORT
);
779 for (j
= 0; j
< width
; j
++) {
780 zValues
[j
] = depthStencilSrc
[j
] >> 16;
783 _swrast_write_zoomed_z_span(ctx
, imgX
, imgY
, width
,
786 depthRb
->PutRow(ctx
, depthRb
, width
, x
, y
+ i
, zValues
,NULL
);
790 GLuint zValues
[MAX_WIDTH
]; /* 16 or 32-bit Z value storage */
791 _mesa_unpack_depth_span(ctx
, width
,
792 depthRb
->DataType
, zValues
, depthScale
,
793 type
, depthStencilSrc
, &clippedUnpack
);
795 _swrast_write_zoomed_z_span(ctx
, imgX
, imgY
, width
, x
,
799 depthRb
->PutRow(ctx
, depthRb
, width
, x
, y
+ i
, zValues
,NULL
);
804 if (stencilMask
!= 0x0) {
805 GLstencil stencilValues
[MAX_WIDTH
];
806 /* get stencil values, with shift/offset/mapping */
807 _mesa_unpack_stencil_span(ctx
, width
, stencilType
, stencilValues
,
808 type
, depthStencilSrc
, &clippedUnpack
,
809 ctx
->_ImageTransferState
);
811 _swrast_write_zoomed_stencil_span(ctx
, imgX
, imgY
, width
,
812 x
, y
+ i
, stencilValues
);
814 _swrast_write_stencil_span(ctx
, width
, x
, y
+ i
, stencilValues
);
823 * Execute software-based glDrawPixels.
824 * By time we get here, all error checking will have been done.
827 _swrast_DrawPixels( GLcontext
*ctx
,
829 GLsizei width
, GLsizei height
,
830 GLenum format
, GLenum type
,
831 const struct gl_pixelstore_attrib
*unpack
,
832 const GLvoid
*pixels
)
834 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
836 RENDER_START(swrast
,ctx
);
839 _mesa_update_state(ctx
);
841 if (swrast
->NewState
)
842 _swrast_validate_derived( ctx
);
844 if (unpack
->BufferObj
->Name
) {
845 /* unpack from PBO */
847 if (!_mesa_validate_pbo_access(2, unpack
, width
, height
, 1,
848 format
, type
, pixels
)) {
849 _mesa_error(ctx
, GL_INVALID_OPERATION
,
850 "glDrawPixels(invalid PBO access)");
853 buf
= (GLubyte
*) ctx
->Driver
.MapBuffer(ctx
, GL_PIXEL_UNPACK_BUFFER_EXT
,
857 /* buffer is already mapped - that's an error */
858 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glDrawPixels(PBO is mapped)");
861 pixels
= ADD_POINTERS(buf
, pixels
);
865 case GL_STENCIL_INDEX
:
866 draw_stencil_pixels( ctx
, x
, y
, width
, height
, type
, unpack
, pixels
);
868 case GL_DEPTH_COMPONENT
:
869 draw_depth_pixels( ctx
, x
, y
, width
, height
, type
, unpack
, pixels
);
872 if (ctx
->Visual
.rgbMode
)
873 draw_rgba_pixels(ctx
, x
,y
, width
, height
, format
, type
, unpack
, pixels
);
875 draw_index_pixels(ctx
, x
, y
, width
, height
, type
, unpack
, pixels
);
882 case GL_LUMINANCE_ALPHA
:
888 draw_rgba_pixels(ctx
, x
, y
, width
, height
, format
, type
, unpack
, pixels
);
890 case GL_DEPTH_STENCIL_EXT
:
891 draw_depth_stencil_pixels(ctx
, x
, y
, width
, height
,
892 type
, unpack
, pixels
);
895 _mesa_problem(ctx
, "unexpected format in _swrast_DrawPixels");
896 /* don't return yet, clean-up */
901 RENDER_FINISH(swrast
,ctx
);
903 if (unpack
->BufferObj
->Name
) {
904 /* done with PBO so unmap it now */
905 ctx
->Driver
.UnmapBuffer(ctx
, GL_PIXEL_UNPACK_BUFFER_EXT
,
912 #if 0 /* experimental */
914 * Execute glDrawDepthPixelsMESA().
917 _swrast_DrawDepthPixelsMESA( GLcontext
*ctx
,
919 GLsizei width
, GLsizei height
,
920 GLenum colorFormat
, GLenum colorType
,
921 const GLvoid
*colors
,
922 GLenum depthType
, const GLvoid
*depths
,
923 const struct gl_pixelstore_attrib
*unpack
)
925 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
927 if (swrast
->NewState
)
928 _swrast_validate_derived( ctx
);
930 RENDER_START(swrast
,ctx
);
932 switch (colorFormat
) {
934 if (ctx
->Visual
.rgbMode
)
935 draw_rgba_pixels(ctx
, x
,y
, width
, height
, colorFormat
, colorType
,
938 draw_index_pixels(ctx
, x
, y
, width
, height
, colorType
,
946 case GL_LUMINANCE_ALPHA
:
952 draw_rgba_pixels(ctx
, x
, y
, width
, height
, colorFormat
, colorType
,
956 _mesa_problem(ctx
, "unexpected format in glDrawDepthPixelsMESA");
959 RENDER_FINISH(swrast
,ctx
);