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
);
74 _swrast_span_default_secondary_color(ctx
, &span
);
76 _swrast_span_default_z(ctx
, &span
);
77 if (swrast
->_FogEnabled
)
78 _swrast_span_default_fog(ctx
, &span
);
79 if (ctx
->Texture
._EnabledCoordUnits
)
80 _swrast_span_default_texcoords(ctx
, &span
);
82 /* copy input params since clipping may change them */
89 /* check for simple zooming and clipping */
90 if (ctx
->Pixel
.ZoomX
== 1.0F
&&
91 (ctx
->Pixel
.ZoomY
== 1.0F
|| ctx
->Pixel
.ZoomY
== -1.0F
)) {
92 if (!_mesa_clip_drawpixels(ctx
, &destX
, &destY
,
93 &drawWidth
, &drawHeight
, &unpack
)) {
94 /* image was completely clipped: no-op, all done */
98 yStep
= (GLint
) ctx
->Pixel
.ZoomY
;
99 ASSERT(yStep
== 1 || yStep
== -1);
102 /* non-simple zooming */
103 simpleZoom
= GL_FALSE
;
105 if (unpack
.RowLength
== 0)
106 unpack
.RowLength
= width
;
113 if (format
== GL_RGBA
&& type
== rbType
) {
115 = (const GLubyte
*) _mesa_image_address2d(&unpack
, pixels
, width
,
116 height
, format
, type
, 0, 0);
117 const GLint srcStride
= _mesa_image_row_stride(&unpack
, width
,
121 for (row
= 0; row
< drawHeight
; row
++) {
122 rb
->PutRow(ctx
, rb
, drawWidth
, destX
, destY
, src
, NULL
);
130 for (row
= 0; row
< drawHeight
; row
++) {
132 span
.y
= destY
+ row
;
133 span
.end
= drawWidth
;
134 span
.array
->ChanType
= rbType
;
135 _swrast_write_zoomed_rgba_span(ctx
, imgX
, imgY
, &span
, src
);
138 span
.array
->ChanType
= CHAN_TYPE
;
143 if (format
== GL_RGB
&& type
== rbType
) {
145 = (const GLubyte
*) _mesa_image_address2d(&unpack
, pixels
, width
,
146 height
, format
, type
, 0, 0);
147 const GLint srcStride
= _mesa_image_row_stride(&unpack
, width
,
151 for (row
= 0; row
< drawHeight
; row
++) {
152 rb
->PutRowRGB(ctx
, rb
, drawWidth
, destX
, destY
, src
, NULL
);
160 for (row
= 0; row
< drawHeight
; row
++) {
163 span
.end
= drawWidth
;
164 span
.array
->ChanType
= rbType
;
165 _swrast_write_zoomed_rgb_span(ctx
, imgX
, imgY
, &span
, src
);
169 span
.array
->ChanType
= CHAN_TYPE
;
174 /* Remaining cases haven't been tested with alignment != 1 */
175 if (userUnpack
->Alignment
!= 1)
178 if (format
== GL_LUMINANCE
&& type
== CHAN_TYPE
&& rbType
== CHAN_TYPE
) {
179 const GLchan
*src
= (const GLchan
*) pixels
180 + (unpack
.SkipRows
* unpack
.RowLength
+ unpack
.SkipPixels
);
184 ASSERT(drawWidth
<= MAX_WIDTH
);
185 for (row
= 0; row
< drawHeight
; row
++) {
186 GLchan rgb
[MAX_WIDTH
][3];
188 for (i
= 0;i
<drawWidth
;i
++) {
193 rb
->PutRowRGB(ctx
, rb
, drawWidth
, destX
, destY
, rgb
, NULL
);
194 src
+= unpack
.RowLength
;
201 ASSERT(drawWidth
<= MAX_WIDTH
);
202 for (row
= 0; row
< drawHeight
; row
++) {
203 GLchan rgb
[MAX_WIDTH
][3];
205 for (i
= 0;i
<drawWidth
;i
++) {
212 span
.end
= drawWidth
;
213 _swrast_write_zoomed_rgb_span(ctx
, imgX
, imgY
, &span
, rgb
);
214 src
+= unpack
.RowLength
;
221 if (format
== GL_LUMINANCE_ALPHA
&& type
== CHAN_TYPE
&& rbType
== CHAN_TYPE
) {
222 const GLchan
*src
= (const GLchan
*) pixels
223 + (unpack
.SkipRows
* unpack
.RowLength
+ unpack
.SkipPixels
)*2;
226 ASSERT(drawWidth
<= MAX_WIDTH
);
227 for (row
= 0; row
< drawHeight
; row
++) {
229 const GLchan
*ptr
= src
;
230 for (i
= 0;i
<drawWidth
;i
++) {
231 span
.array
->rgba
[i
][0] = *ptr
;
232 span
.array
->rgba
[i
][1] = *ptr
;
233 span
.array
->rgba
[i
][2] = *ptr
++;
234 span
.array
->rgba
[i
][3] = *ptr
++;
236 rb
->PutRow(ctx
, rb
, drawWidth
, destX
, destY
,
237 span
.array
->rgba
, NULL
);
238 src
+= unpack
.RowLength
*2;
245 ASSERT(drawWidth
<= MAX_WIDTH
);
246 for (row
= 0; row
< drawHeight
; row
++) {
247 const GLchan
*ptr
= src
;
249 for (i
= 0;i
<drawWidth
;i
++) {
250 span
.array
->rgba
[i
][0] = *ptr
;
251 span
.array
->rgba
[i
][1] = *ptr
;
252 span
.array
->rgba
[i
][2] = *ptr
++;
253 span
.array
->rgba
[i
][3] = *ptr
++;
257 span
.end
= drawWidth
;
258 _swrast_write_zoomed_rgba_span(ctx
, imgX
, imgY
, &span
,
260 src
+= unpack
.RowLength
*2;
267 if (format
== GL_COLOR_INDEX
&& type
== GL_UNSIGNED_BYTE
) {
268 const GLubyte
*src
= (const GLubyte
*) pixels
269 + unpack
.SkipRows
* unpack
.RowLength
+ unpack
.SkipPixels
;
270 if (ctx
->Visual
.rgbMode
&& rbType
== GL_UNSIGNED_BYTE
) {
271 /* convert ubyte/CI data to ubyte/RGBA */
274 for (row
= 0; row
< drawHeight
; row
++) {
275 ASSERT(drawWidth
<= MAX_WIDTH
);
276 _mesa_map_ci8_to_rgba8(ctx
, drawWidth
, src
,
277 span
.array
->color
.sz1
.rgba
);
278 rb
->PutRow(ctx
, rb
, drawWidth
, destX
, destY
,
279 span
.array
->color
.sz1
.rgba
, NULL
);
280 src
+= unpack
.RowLength
;
285 /* ubyte/CI to ubyte/RGBA with zooming */
287 for (row
= 0; row
< drawHeight
; row
++) {
288 ASSERT(drawWidth
<= MAX_WIDTH
);
289 _mesa_map_ci8_to_rgba8(ctx
, drawWidth
, src
,
290 span
.array
->color
.sz1
.rgba
);
293 span
.end
= drawWidth
;
294 _swrast_write_zoomed_rgba_span(ctx
, imgX
, imgY
, &span
,
295 span
.array
->color
.sz1
.rgba
);
296 src
+= unpack
.RowLength
;
302 else if (!ctx
->Visual
.rgbMode
&& rbType
== GL_UNSIGNED_INT
) {
303 /* write CI data to CI frame buffer */
306 for (row
= 0; row
< drawHeight
; row
++) {
307 GLuint index32
[MAX_WIDTH
];
309 for (col
= 0; col
< drawWidth
; col
++)
310 index32
[col
] = src
[col
];
311 rb
->PutRow(ctx
, rb
, drawWidth
, destX
, destY
, index32
, NULL
);
312 src
+= unpack
.RowLength
;
320 /* can't handle this pixel format and/or data type */
327 * Draw color index image.
330 draw_index_pixels( GLcontext
*ctx
, GLint x
, GLint y
,
331 GLsizei width
, GLsizei height
,
333 const struct gl_pixelstore_attrib
*unpack
,
334 const GLvoid
*pixels
)
336 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
337 const GLint imgX
= x
, imgY
= y
;
338 const GLboolean zoom
= ctx
->Pixel
.ZoomX
!=1.0 || ctx
->Pixel
.ZoomY
!=1.0;
339 GLint row
, skipPixels
;
342 INIT_SPAN(span
, GL_BITMAP
, 0, 0, SPAN_INDEX
);
345 _swrast_span_default_z(ctx
, &span
);
346 if (swrast
->_FogEnabled
)
347 _swrast_span_default_fog(ctx
, &span
);
353 while (skipPixels
< width
) {
354 const GLint spanWidth
= MIN2(width
- skipPixels
, MAX_WIDTH
);
355 ASSERT(spanWidth
<= MAX_WIDTH
);
356 for (row
= 0; row
< height
; row
++) {
357 const GLvoid
*source
= _mesa_image_address2d(unpack
, pixels
,
359 GL_COLOR_INDEX
, type
,
361 _mesa_unpack_index_span(ctx
, spanWidth
, GL_UNSIGNED_INT
,
362 span
.array
->index
, type
, source
, unpack
,
363 ctx
->_ImageTransferState
);
365 /* These may get changed during writing/clipping */
366 span
.x
= x
+ skipPixels
;
368 span
.end
= spanWidth
;
371 _swrast_write_zoomed_index_span(ctx
, imgX
, imgY
, &span
);
373 _swrast_write_index_span(ctx
, &span
);
375 skipPixels
+= spanWidth
;
382 * Draw stencil image.
385 draw_stencil_pixels( GLcontext
*ctx
, GLint x
, GLint y
,
386 GLsizei width
, GLsizei height
,
388 const struct gl_pixelstore_attrib
*unpack
,
389 const GLvoid
*pixels
)
391 const GLboolean zoom
= ctx
->Pixel
.ZoomX
!= 1.0 || ctx
->Pixel
.ZoomY
!= 1.0;
394 /* if width > MAX_WIDTH, have to process image in chunks */
396 while (skipPixels
< width
) {
397 const GLint spanX
= x
+ skipPixels
;
398 const GLint spanWidth
= MIN2(width
- skipPixels
, MAX_WIDTH
);
400 for (row
= 0; row
< height
; row
++) {
401 const GLint spanY
= y
+ row
;
402 GLstencil values
[MAX_WIDTH
];
403 GLenum destType
= (sizeof(GLstencil
) == sizeof(GLubyte
))
404 ? GL_UNSIGNED_BYTE
: GL_UNSIGNED_SHORT
;
405 const GLvoid
*source
= _mesa_image_address2d(unpack
, pixels
,
407 GL_COLOR_INDEX
, type
,
409 _mesa_unpack_index_span(ctx
, spanWidth
, destType
, values
,
410 type
, source
, unpack
,
411 ctx
->_ImageTransferState
);
412 _mesa_apply_stencil_transfer_ops(ctx
, spanWidth
, values
);
414 _swrast_write_zoomed_stencil_span(ctx
, x
, y
, spanWidth
,
415 spanX
, spanY
, values
);
418 _swrast_write_stencil_span(ctx
, spanWidth
, spanX
, spanY
, values
);
421 skipPixels
+= spanWidth
;
430 draw_depth_pixels( GLcontext
*ctx
, GLint x
, GLint y
,
431 GLsizei width
, GLsizei height
,
433 const struct gl_pixelstore_attrib
*unpack
,
434 const GLvoid
*pixels
)
436 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
437 const GLboolean scaleOrBias
438 = ctx
->Pixel
.DepthScale
!= 1.0 || ctx
->Pixel
.DepthBias
!= 0.0;
439 const GLboolean zoom
= ctx
->Pixel
.ZoomX
!= 1.0 || ctx
->Pixel
.ZoomY
!= 1.0;
442 INIT_SPAN(span
, GL_BITMAP
, 0, 0, SPAN_Z
);
444 _swrast_span_default_color(ctx
, &span
);
445 _swrast_span_default_secondary_color(ctx
, &span
);
446 if (swrast
->_FogEnabled
)
447 _swrast_span_default_fog(ctx
, &span
);
448 if (ctx
->Texture
._EnabledCoordUnits
)
449 _swrast_span_default_texcoords(ctx
, &span
);
451 if (type
== GL_UNSIGNED_SHORT
452 && ctx
->DrawBuffer
->Visual
.depthBits
== 16
455 && ctx
->Visual
.rgbMode
456 && width
<= MAX_WIDTH
457 && !unpack
->SwapBytes
) {
458 /* Special case: directly write 16-bit depth values */
460 for (row
= 0; row
< height
; row
++) {
461 const GLushort
*zSrc
= (const GLushort
*)
462 _mesa_image_address2d(unpack
, pixels
, width
, height
,
463 GL_DEPTH_COMPONENT
, type
, row
, 0);
465 for (i
= 0; i
< width
; i
++)
466 span
.array
->z
[i
] = zSrc
[i
];
470 _swrast_write_rgba_span(ctx
, &span
);
473 else if (type
== GL_UNSIGNED_INT
476 && ctx
->Visual
.rgbMode
477 && width
<= MAX_WIDTH
478 && !unpack
->SwapBytes
) {
479 /* Special case: shift 32-bit values down to Visual.depthBits */
480 const GLint shift
= 32 - ctx
->DrawBuffer
->Visual
.depthBits
;
482 for (row
= 0; row
< height
; row
++) {
483 const GLuint
*zSrc
= (const GLuint
*)
484 _mesa_image_address2d(unpack
, pixels
, width
, height
,
485 GL_DEPTH_COMPONENT
, type
, row
, 0);
487 _mesa_memcpy(span
.array
->z
, zSrc
, width
* sizeof(GLuint
));
491 for (col
= 0; col
< width
; col
++)
492 span
.array
->z
[col
] = zSrc
[col
] >> shift
;
497 _swrast_write_rgba_span(ctx
, &span
);
502 const GLfloat depthMax
= ctx
->DrawBuffer
->_DepthMaxF
;
503 GLint skipPixels
= 0;
505 /* in case width > MAX_WIDTH do the copy in chunks */
506 while (skipPixels
< width
) {
507 const GLint spanWidth
= MIN2(width
- skipPixels
, MAX_WIDTH
);
509 ASSERT(span
.end
<= MAX_WIDTH
);
510 for (row
= 0; row
< height
; row
++) {
511 const GLvoid
*zSrc
= _mesa_image_address2d(unpack
,
512 pixels
, width
, height
,
513 GL_DEPTH_COMPONENT
, type
,
516 /* Set these for each row since the _swrast_write_* function may
517 * change them while clipping.
519 span
.x
= x
+ skipPixels
;
521 span
.end
= spanWidth
;
523 _mesa_unpack_depth_span(ctx
, spanWidth
,
524 GL_UNSIGNED_INT
, span
.array
->z
, depthMax
,
527 _swrast_write_zoomed_depth_span(ctx
, x
, y
, &span
);
529 else if (ctx
->Visual
.rgbMode
) {
530 _swrast_write_rgba_span(ctx
, &span
);
533 _swrast_write_index_span(ctx
, &span
);
536 skipPixels
+= spanWidth
;
547 draw_rgba_pixels( GLcontext
*ctx
, GLint x
, GLint y
,
548 GLsizei width
, GLsizei height
,
549 GLenum format
, GLenum type
,
550 const struct gl_pixelstore_attrib
*unpack
,
551 const GLvoid
*pixels
)
553 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
554 const GLint imgX
= x
, imgY
= y
;
555 const GLboolean zoom
= ctx
->Pixel
.ZoomX
!=1.0 || ctx
->Pixel
.ZoomY
!=1.0;
556 GLfloat
*convImage
= NULL
;
557 GLbitfield transferOps
= ctx
->_ImageTransferState
;
560 /* Try an optimized glDrawPixels first */
561 if (fast_draw_rgba_pixels(ctx
, x
, y
, width
, height
, format
, type
,
565 INIT_SPAN(span
, GL_BITMAP
, 0, 0, SPAN_RGBA
);
566 _swrast_span_default_secondary_color(ctx
, &span
);
568 _swrast_span_default_z(ctx
, &span
);
569 if (swrast
->_FogEnabled
)
570 _swrast_span_default_fog(ctx
, &span
);
571 if (ctx
->Texture
._EnabledCoordUnits
)
572 _swrast_span_default_texcoords(ctx
, &span
);
574 if (ctx
->Pixel
.Convolution2DEnabled
|| ctx
->Pixel
.Separable2DEnabled
) {
575 /* Convolution has to be handled specially. We'll create an
576 * intermediate image, applying all pixel transfer operations
577 * up to convolution. Then we'll convolve the image. Then
578 * we'll proceed with the rest of the transfer operations and
579 * rasterize the image.
582 GLfloat
*dest
, *tmpImage
;
584 tmpImage
= (GLfloat
*) _mesa_malloc(width
* height
* 4 * sizeof(GLfloat
));
586 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glDrawPixels");
589 convImage
= (GLfloat
*) _mesa_malloc(width
* height
* 4 * sizeof(GLfloat
));
591 _mesa_free(tmpImage
);
592 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glDrawPixels");
596 /* Unpack the image and apply transfer ops up to convolution */
598 for (row
= 0; row
< height
; row
++) {
599 const GLvoid
*source
= _mesa_image_address2d(unpack
,
600 pixels
, width
, height
, format
, type
, row
, 0);
601 _mesa_unpack_color_span_float(ctx
, width
, GL_RGBA
, (GLfloat
*) dest
,
602 format
, type
, source
, unpack
,
603 transferOps
& IMAGE_PRE_CONVOLUTION_BITS
);
608 if (ctx
->Pixel
.Convolution2DEnabled
) {
609 _mesa_convolve_2d_image(ctx
, &width
, &height
, tmpImage
, convImage
);
612 ASSERT(ctx
->Pixel
.Separable2DEnabled
);
613 _mesa_convolve_sep_image(ctx
, &width
, &height
, tmpImage
, convImage
);
615 _mesa_free(tmpImage
);
617 /* continue transfer ops and draw the convolved image */
618 unpack
= &ctx
->DefaultPacking
;
622 transferOps
&= IMAGE_POST_CONVOLUTION_BITS
;
624 else if (ctx
->Pixel
.Convolution1DEnabled
) {
625 /* we only want to apply 1D convolution to glTexImage1D */
626 transferOps
&= ~(IMAGE_CONVOLUTION_BIT
|
627 IMAGE_POST_CONVOLUTION_SCALE_BIAS
);
630 if (ctx
->DrawBuffer
->_NumColorDrawBuffers
[0] > 0 &&
631 ctx
->DrawBuffer
->_ColorDrawBuffers
[0][0]->DataType
!= GL_FLOAT
&&
632 ctx
->Color
.ClampFragmentColor
!= GL_FALSE
) {
633 /* need to clamp colors before applying fragment ops */
634 transferOps
|= IMAGE_CLAMP_BIT
;
641 const GLboolean sink
= (ctx
->Pixel
.MinMaxEnabled
&& ctx
->MinMax
.Sink
)
642 || (ctx
->Pixel
.HistogramEnabled
&& ctx
->Histogram
.Sink
);
643 const GLbitfield interpMask
= span
.interpMask
;
644 const GLbitfield arrayMask
= span
.arrayMask
;
645 const GLint srcStride
646 = _mesa_image_row_stride(unpack
, width
, format
, type
);
647 GLint skipPixels
= 0;
648 /* use span array for temp color storage */
649 GLfloat
*rgba
= (GLfloat
*) span
.array
->attribs
[FRAG_ATTRIB_COL0
];
651 /* if the span is wider than MAX_WIDTH we have to do it in chunks */
652 while (skipPixels
< width
) {
653 const GLint spanWidth
= MIN2(width
- skipPixels
, MAX_WIDTH
);
654 const GLubyte
*source
655 = (const GLubyte
*) _mesa_image_address2d(unpack
, pixels
,
656 width
, height
, format
,
657 type
, 0, skipPixels
);
660 for (row
= 0; row
< height
; row
++) {
661 /* get image row as float/RGBA */
662 _mesa_unpack_color_span_float(ctx
, spanWidth
, GL_RGBA
, rgba
,
663 format
, type
, source
, unpack
,
667 /* Set these for each row since the _swrast_write_* functions
668 * may change them while clipping/rendering.
670 span
.array
->ChanType
= GL_FLOAT
;
671 span
.x
= x
+ skipPixels
;
673 span
.end
= spanWidth
;
674 span
.arrayMask
= arrayMask
;
675 span
.interpMask
= interpMask
;
677 _swrast_write_zoomed_rgba_span(ctx
, imgX
, imgY
, &span
, rgba
);
680 _swrast_write_rgba_span(ctx
, &span
);
687 skipPixels
+= spanWidth
;
688 } /* while skipPixels < width */
690 /* XXX this is ugly/temporary, to undo above change */
691 span
.array
->ChanType
= CHAN_TYPE
;
695 _mesa_free(convImage
);
701 * This is a bit different from drawing GL_DEPTH_COMPONENT pixels.
702 * The only per-pixel operations that apply are depth scale/bias,
703 * stencil offset/shift, GL_DEPTH_WRITEMASK and GL_STENCIL_WRITEMASK,
705 * Also, only the depth buffer and stencil buffers are touched, not the
709 draw_depth_stencil_pixels(GLcontext
*ctx
, GLint x
, GLint y
,
710 GLsizei width
, GLsizei height
, GLenum type
,
711 const struct gl_pixelstore_attrib
*unpack
,
712 const GLvoid
*pixels
)
714 const GLint imgX
= x
, imgY
= y
;
715 const GLboolean scaleOrBias
716 = ctx
->Pixel
.DepthScale
!= 1.0 || ctx
->Pixel
.DepthBias
!= 0.0;
717 const GLfloat depthScale
= ctx
->DrawBuffer
->_DepthMaxF
;
718 const GLuint stencilMask
= ctx
->Stencil
.WriteMask
[0];
719 const GLuint stencilType
= (STENCIL_BITS
== 8) ?
720 GL_UNSIGNED_BYTE
: GL_UNSIGNED_SHORT
;
721 const GLboolean zoom
= ctx
->Pixel
.ZoomX
!= 1.0 || ctx
->Pixel
.ZoomY
!= 1.0;
722 struct gl_renderbuffer
*depthRb
, *stencilRb
;
723 struct gl_pixelstore_attrib clippedUnpack
= *unpack
;
726 if (!_mesa_clip_drawpixels(ctx
, &x
, &y
, &width
, &height
,
728 /* totally clipped */
733 depthRb
= ctx
->ReadBuffer
->Attachment
[BUFFER_DEPTH
].Renderbuffer
;
734 stencilRb
= ctx
->ReadBuffer
->Attachment
[BUFFER_STENCIL
].Renderbuffer
;
738 if (depthRb
->_BaseFormat
== GL_DEPTH_STENCIL_EXT
&&
739 stencilRb
->_BaseFormat
== GL_DEPTH_STENCIL_EXT
&&
740 depthRb
== stencilRb
&&
744 (stencilMask
& 0xff) == 0xff) {
745 /* This is the ideal case.
746 * Drawing GL_DEPTH_STENCIL pixels into a combined depth/stencil buffer.
747 * Plus, no pixel transfer ops, zooming, or masking needed.
750 for (i
= 0; i
< height
; i
++) {
751 const GLuint
*src
= (const GLuint
*)
752 _mesa_image_address2d(&clippedUnpack
, pixels
, width
, height
,
753 GL_DEPTH_STENCIL_EXT
, type
, i
, 0);
754 depthRb
->PutRow(ctx
, depthRb
, width
, x
, y
+ i
, src
, NULL
);
758 /* sub-optimal cases:
759 * Separate depth/stencil buffers, or pixel transfer ops required.
761 /* XXX need to handle very wide images (skippixels) */
764 depthRb
= ctx
->DrawBuffer
->_DepthBuffer
;
765 stencilRb
= ctx
->DrawBuffer
->_StencilBuffer
;
767 for (i
= 0; i
< height
; i
++) {
768 const GLuint
*depthStencilSrc
= (const GLuint
*)
769 _mesa_image_address2d(&clippedUnpack
, pixels
, width
, height
,
770 GL_DEPTH_STENCIL_EXT
, type
, i
, 0);
772 if (ctx
->Depth
.Mask
) {
773 if (!scaleOrBias
&& ctx
->DrawBuffer
->Visual
.depthBits
== 24) {
774 /* fast path 24-bit zbuffer */
775 GLuint zValues
[MAX_WIDTH
];
777 ASSERT(depthRb
->DataType
== GL_UNSIGNED_INT
);
778 for (j
= 0; j
< width
; j
++) {
779 zValues
[j
] = depthStencilSrc
[j
] >> 8;
782 _swrast_write_zoomed_z_span(ctx
, imgX
, imgY
, width
,
785 depthRb
->PutRow(ctx
, depthRb
, width
, x
, y
+ i
, zValues
,NULL
);
787 else if (!scaleOrBias
&& ctx
->DrawBuffer
->Visual
.depthBits
== 16) {
788 /* fast path 16-bit zbuffer */
789 GLushort zValues
[MAX_WIDTH
];
791 ASSERT(depthRb
->DataType
== GL_UNSIGNED_SHORT
);
792 for (j
= 0; j
< width
; j
++) {
793 zValues
[j
] = depthStencilSrc
[j
] >> 16;
796 _swrast_write_zoomed_z_span(ctx
, imgX
, imgY
, width
,
799 depthRb
->PutRow(ctx
, depthRb
, width
, x
, y
+ i
, zValues
,NULL
);
803 GLuint zValues
[MAX_WIDTH
]; /* 16 or 32-bit Z value storage */
804 _mesa_unpack_depth_span(ctx
, width
,
805 depthRb
->DataType
, zValues
, depthScale
,
806 type
, depthStencilSrc
, &clippedUnpack
);
808 _swrast_write_zoomed_z_span(ctx
, imgX
, imgY
, width
, x
,
812 depthRb
->PutRow(ctx
, depthRb
, width
, x
, y
+ i
, zValues
,NULL
);
817 if (stencilMask
!= 0x0) {
818 GLstencil stencilValues
[MAX_WIDTH
];
819 /* get stencil values, with shift/offset/mapping */
820 _mesa_unpack_stencil_span(ctx
, width
, stencilType
, stencilValues
,
821 type
, depthStencilSrc
, &clippedUnpack
,
822 ctx
->_ImageTransferState
);
824 _swrast_write_zoomed_stencil_span(ctx
, imgX
, imgY
, width
,
825 x
, y
+ i
, stencilValues
);
827 _swrast_write_stencil_span(ctx
, width
, x
, y
+ i
, stencilValues
);
836 * Execute software-based glDrawPixels.
837 * By time we get here, all error checking will have been done.
840 _swrast_DrawPixels( GLcontext
*ctx
,
842 GLsizei width
, GLsizei height
,
843 GLenum format
, GLenum type
,
844 const struct gl_pixelstore_attrib
*unpack
,
845 const GLvoid
*pixels
)
847 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
849 RENDER_START(swrast
,ctx
);
852 _mesa_update_state(ctx
);
854 if (swrast
->NewState
)
855 _swrast_validate_derived( ctx
);
857 if (unpack
->BufferObj
->Name
) {
858 /* unpack from PBO */
860 if (!_mesa_validate_pbo_access(2, unpack
, width
, height
, 1,
861 format
, type
, pixels
)) {
862 _mesa_error(ctx
, GL_INVALID_OPERATION
,
863 "glDrawPixels(invalid PBO access)");
866 buf
= (GLubyte
*) ctx
->Driver
.MapBuffer(ctx
, GL_PIXEL_UNPACK_BUFFER_EXT
,
870 /* buffer is already mapped - that's an error */
871 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glDrawPixels(PBO is mapped)");
874 pixels
= ADD_POINTERS(buf
, pixels
);
878 case GL_STENCIL_INDEX
:
879 draw_stencil_pixels( ctx
, x
, y
, width
, height
, type
, unpack
, pixels
);
881 case GL_DEPTH_COMPONENT
:
882 draw_depth_pixels( ctx
, x
, y
, width
, height
, type
, unpack
, pixels
);
885 if (ctx
->Visual
.rgbMode
)
886 draw_rgba_pixels(ctx
, x
,y
, width
, height
, format
, type
, unpack
, pixels
);
888 draw_index_pixels(ctx
, x
, y
, width
, height
, type
, unpack
, pixels
);
895 case GL_LUMINANCE_ALPHA
:
901 draw_rgba_pixels(ctx
, x
, y
, width
, height
, format
, type
, unpack
, pixels
);
903 case GL_DEPTH_STENCIL_EXT
:
904 draw_depth_stencil_pixels(ctx
, x
, y
, width
, height
,
905 type
, unpack
, pixels
);
908 _mesa_problem(ctx
, "unexpected format in _swrast_DrawPixels");
909 /* don't return yet, clean-up */
914 RENDER_FINISH(swrast
,ctx
);
916 if (unpack
->BufferObj
->Name
) {
917 /* done with PBO so unmap it now */
918 ctx
->Driver
.UnmapBuffer(ctx
, GL_PIXEL_UNPACK_BUFFER_EXT
,
925 #if 0 /* experimental */
927 * Execute glDrawDepthPixelsMESA().
930 _swrast_DrawDepthPixelsMESA( GLcontext
*ctx
,
932 GLsizei width
, GLsizei height
,
933 GLenum colorFormat
, GLenum colorType
,
934 const GLvoid
*colors
,
935 GLenum depthType
, const GLvoid
*depths
,
936 const struct gl_pixelstore_attrib
*unpack
)
938 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
940 if (swrast
->NewState
)
941 _swrast_validate_derived( ctx
);
943 RENDER_START(swrast
,ctx
);
945 switch (colorFormat
) {
947 if (ctx
->Visual
.rgbMode
)
948 draw_rgba_pixels(ctx
, x
,y
, width
, height
, colorFormat
, colorType
,
951 draw_index_pixels(ctx
, x
, y
, width
, height
, colorType
,
959 case GL_LUMINANCE_ALPHA
:
965 draw_rgba_pixels(ctx
, x
, y
, width
, height
, colorFormat
, colorType
,
969 _mesa_problem(ctx
, "unexpected format in glDrawDepthPixelsMESA");
972 RENDER_FINISH(swrast
,ctx
);