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
) {
114 = (const GLubyte
*) _mesa_image_address2d(&unpack
, pixels
, width
,
115 height
, format
, type
, 0, 0);
116 const GLint srcStride
= _mesa_image_row_stride(&unpack
, width
,
120 for (row
= 0; row
< drawHeight
; row
++) {
121 rb
->PutRow(ctx
, rb
, drawWidth
, destX
, destY
, src
, NULL
);
129 for (row
= 0; row
< drawHeight
; row
++) {
131 span
.y
= destY
+ row
;
132 span
.end
= drawWidth
;
133 span
.array
->ChanType
= rbType
;
134 _swrast_write_zoomed_rgba_span(ctx
, imgX
, imgY
, &span
, src
);
137 span
.array
->ChanType
= CHAN_TYPE
;
142 if (format
== GL_RGB
&& type
== rbType
) {
144 = (const GLubyte
*) _mesa_image_address2d(&unpack
, pixels
, width
,
145 height
, format
, type
, 0, 0);
146 const GLint srcStride
= _mesa_image_row_stride(&unpack
, width
,
150 for (row
= 0; row
< drawHeight
; row
++) {
151 rb
->PutRowRGB(ctx
, rb
, drawWidth
, destX
, destY
, src
, NULL
);
159 for (row
= 0; row
< drawHeight
; row
++) {
162 span
.end
= drawWidth
;
163 span
.array
->ChanType
= rbType
;
164 _swrast_write_zoomed_rgb_span(ctx
, imgX
, imgY
, &span
, src
);
168 span
.array
->ChanType
= CHAN_TYPE
;
173 /* Remaining cases haven't been tested with alignment != 1 */
174 if (userUnpack
->Alignment
!= 1)
177 if (format
== GL_LUMINANCE
&& type
== CHAN_TYPE
&& rbType
== CHAN_TYPE
) {
178 const GLchan
*src
= (const GLchan
*) pixels
179 + (unpack
.SkipRows
* unpack
.RowLength
+ unpack
.SkipPixels
);
183 ASSERT(drawWidth
<= MAX_WIDTH
);
184 for (row
= 0; row
< drawHeight
; row
++) {
185 GLchan rgb
[MAX_WIDTH
][3];
187 for (i
= 0;i
<drawWidth
;i
++) {
192 rb
->PutRowRGB(ctx
, rb
, drawWidth
, destX
, destY
, rgb
, NULL
);
193 src
+= unpack
.RowLength
;
200 ASSERT(drawWidth
<= MAX_WIDTH
);
201 for (row
= 0; row
< drawHeight
; row
++) {
202 GLchan rgb
[MAX_WIDTH
][3];
204 for (i
= 0;i
<drawWidth
;i
++) {
211 span
.end
= drawWidth
;
212 _swrast_write_zoomed_rgb_span(ctx
, imgX
, imgY
, &span
, rgb
);
213 src
+= unpack
.RowLength
;
220 if (format
== GL_LUMINANCE_ALPHA
&& type
== CHAN_TYPE
&& rbType
== CHAN_TYPE
) {
221 const GLchan
*src
= (const GLchan
*) pixels
222 + (unpack
.SkipRows
* unpack
.RowLength
+ unpack
.SkipPixels
)*2;
225 ASSERT(drawWidth
<= MAX_WIDTH
);
226 for (row
= 0; row
< drawHeight
; row
++) {
228 const GLchan
*ptr
= src
;
229 for (i
= 0;i
<drawWidth
;i
++) {
230 span
.array
->rgba
[i
][0] = *ptr
;
231 span
.array
->rgba
[i
][1] = *ptr
;
232 span
.array
->rgba
[i
][2] = *ptr
++;
233 span
.array
->rgba
[i
][3] = *ptr
++;
235 rb
->PutRow(ctx
, rb
, drawWidth
, destX
, destY
,
236 span
.array
->rgba
, NULL
);
237 src
+= unpack
.RowLength
*2;
244 ASSERT(drawWidth
<= MAX_WIDTH
);
245 for (row
= 0; row
< drawHeight
; row
++) {
246 const GLchan
*ptr
= src
;
248 for (i
= 0;i
<drawWidth
;i
++) {
249 span
.array
->rgba
[i
][0] = *ptr
;
250 span
.array
->rgba
[i
][1] = *ptr
;
251 span
.array
->rgba
[i
][2] = *ptr
++;
252 span
.array
->rgba
[i
][3] = *ptr
++;
256 span
.end
= drawWidth
;
257 _swrast_write_zoomed_rgba_span(ctx
, imgX
, imgY
, &span
,
259 src
+= unpack
.RowLength
*2;
266 if (format
== GL_COLOR_INDEX
&& type
== GL_UNSIGNED_BYTE
) {
267 const GLubyte
*src
= (const GLubyte
*) pixels
268 + unpack
.SkipRows
* unpack
.RowLength
+ unpack
.SkipPixels
;
269 if (ctx
->Visual
.rgbMode
&& rbType
== GL_UNSIGNED_BYTE
) {
270 /* convert ubyte/CI data to ubyte/RGBA */
273 for (row
= 0; row
< drawHeight
; row
++) {
274 ASSERT(drawWidth
<= MAX_WIDTH
);
275 _mesa_map_ci8_to_rgba8(ctx
, drawWidth
, src
,
276 span
.array
->color
.sz1
.rgba
);
277 rb
->PutRow(ctx
, rb
, drawWidth
, destX
, destY
,
278 span
.array
->color
.sz1
.rgba
, NULL
);
279 src
+= unpack
.RowLength
;
284 /* ubyte/CI to ubyte/RGBA with zooming */
286 for (row
= 0; row
< drawHeight
; row
++) {
287 ASSERT(drawWidth
<= MAX_WIDTH
);
288 _mesa_map_ci8_to_rgba8(ctx
, drawWidth
, src
,
289 span
.array
->color
.sz1
.rgba
);
292 span
.end
= drawWidth
;
293 _swrast_write_zoomed_rgba_span(ctx
, imgX
, imgY
, &span
,
294 span
.array
->color
.sz1
.rgba
);
295 src
+= unpack
.RowLength
;
301 else if (!ctx
->Visual
.rgbMode
&& rbType
== GL_UNSIGNED_INT
) {
302 /* write CI data to CI frame buffer */
305 for (row
= 0; row
< drawHeight
; row
++) {
306 GLuint index32
[MAX_WIDTH
];
308 for (col
= 0; col
< drawWidth
; col
++)
309 index32
[col
] = src
[col
];
310 rb
->PutRow(ctx
, rb
, drawWidth
, destX
, destY
, index32
, NULL
);
311 src
+= unpack
.RowLength
;
319 /* can't handle this pixel format and/or data type */
326 * Draw color index image.
329 draw_index_pixels( GLcontext
*ctx
, GLint x
, GLint y
,
330 GLsizei width
, GLsizei height
,
332 const struct gl_pixelstore_attrib
*unpack
,
333 const GLvoid
*pixels
)
335 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
336 const GLint imgX
= x
, imgY
= y
;
337 const GLboolean zoom
= ctx
->Pixel
.ZoomX
!=1.0 || ctx
->Pixel
.ZoomY
!=1.0;
338 GLint row
, skipPixels
;
341 INIT_SPAN(span
, GL_BITMAP
, 0, 0, SPAN_INDEX
);
344 _swrast_span_default_z(ctx
, &span
);
345 if (swrast
->_FogEnabled
)
346 _swrast_span_default_fog(ctx
, &span
);
352 while (skipPixels
< width
) {
353 const GLint spanWidth
= MIN2(width
- skipPixels
, MAX_WIDTH
);
354 ASSERT(spanWidth
<= MAX_WIDTH
);
355 for (row
= 0; row
< height
; row
++) {
356 const GLvoid
*source
= _mesa_image_address2d(unpack
, pixels
,
358 GL_COLOR_INDEX
, type
,
360 _mesa_unpack_index_span(ctx
, spanWidth
, GL_UNSIGNED_INT
,
361 span
.array
->index
, type
, source
, unpack
,
362 ctx
->_ImageTransferState
);
364 /* These may get changed during writing/clipping */
365 span
.x
= x
+ skipPixels
;
367 span
.end
= spanWidth
;
370 _swrast_write_zoomed_index_span(ctx
, imgX
, imgY
, &span
);
372 _swrast_write_index_span(ctx
, &span
);
374 skipPixels
+= spanWidth
;
381 * Draw stencil image.
384 draw_stencil_pixels( GLcontext
*ctx
, GLint x
, GLint y
,
385 GLsizei width
, GLsizei height
,
387 const struct gl_pixelstore_attrib
*unpack
,
388 const GLvoid
*pixels
)
390 const GLboolean zoom
= ctx
->Pixel
.ZoomX
!= 1.0 || ctx
->Pixel
.ZoomY
!= 1.0;
393 /* if width > MAX_WIDTH, have to process image in chunks */
395 while (skipPixels
< width
) {
396 const GLint spanX
= x
+ skipPixels
;
397 const GLint spanWidth
= MIN2(width
- skipPixels
, MAX_WIDTH
);
399 for (row
= 0; row
< height
; row
++) {
400 const GLint spanY
= y
+ row
;
401 GLstencil values
[MAX_WIDTH
];
402 GLenum destType
= (sizeof(GLstencil
) == sizeof(GLubyte
))
403 ? GL_UNSIGNED_BYTE
: GL_UNSIGNED_SHORT
;
404 const GLvoid
*source
= _mesa_image_address2d(unpack
, pixels
,
406 GL_COLOR_INDEX
, type
,
408 _mesa_unpack_index_span(ctx
, spanWidth
, destType
, values
,
409 type
, source
, unpack
,
410 ctx
->_ImageTransferState
);
411 _mesa_apply_stencil_transfer_ops(ctx
, spanWidth
, values
);
413 _swrast_write_zoomed_stencil_span(ctx
, x
, y
, spanWidth
,
414 spanX
, spanY
, values
);
417 _swrast_write_stencil_span(ctx
, spanWidth
, spanX
, spanY
, values
);
420 skipPixels
+= spanWidth
;
429 draw_depth_pixels( GLcontext
*ctx
, GLint x
, GLint y
,
430 GLsizei width
, GLsizei height
,
432 const struct gl_pixelstore_attrib
*unpack
,
433 const GLvoid
*pixels
)
435 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
436 const GLboolean scaleOrBias
437 = ctx
->Pixel
.DepthScale
!= 1.0 || ctx
->Pixel
.DepthBias
!= 0.0;
438 const GLboolean zoom
= ctx
->Pixel
.ZoomX
!= 1.0 || ctx
->Pixel
.ZoomY
!= 1.0;
441 INIT_SPAN(span
, GL_BITMAP
, 0, 0, SPAN_Z
);
443 _swrast_span_default_color(ctx
, &span
);
445 if (swrast
->_FogEnabled
)
446 _swrast_span_default_fog(ctx
, &span
);
447 if (ctx
->Texture
._EnabledCoordUnits
)
448 _swrast_span_default_texcoords(ctx
, &span
);
450 if (type
== GL_UNSIGNED_SHORT
451 && ctx
->DrawBuffer
->Visual
.depthBits
== 16
454 && ctx
->Visual
.rgbMode
455 && width
<= MAX_WIDTH
) {
456 /* Special case: directly write 16-bit depth values */
458 for (row
= 0; row
< height
; row
++) {
459 const GLushort
*zSrc
= (const GLushort
*)
460 _mesa_image_address2d(unpack
, pixels
, width
, height
,
461 GL_DEPTH_COMPONENT
, type
, row
, 0);
463 for (i
= 0; i
< width
; i
++)
464 span
.array
->z
[i
] = zSrc
[i
];
468 _swrast_write_rgba_span(ctx
, &span
);
471 else if (type
== GL_UNSIGNED_INT
474 && ctx
->Visual
.rgbMode
475 && width
<= MAX_WIDTH
) {
476 /* Special case: shift 32-bit values down to Visual.depthBits */
477 const GLint shift
= 32 - ctx
->DrawBuffer
->Visual
.depthBits
;
479 for (row
= 0; row
< height
; row
++) {
480 const GLuint
*zSrc
= (const GLuint
*)
481 _mesa_image_address2d(unpack
, pixels
, width
, height
,
482 GL_DEPTH_COMPONENT
, type
, row
, 0);
484 _mesa_memcpy(span
.array
->z
, zSrc
, width
* sizeof(GLuint
));
488 for (col
= 0; col
< width
; col
++)
489 span
.array
->z
[col
] = zSrc
[col
] >> shift
;
494 _swrast_write_rgba_span(ctx
, &span
);
499 const GLfloat depthMax
= ctx
->DrawBuffer
->_DepthMaxF
;
500 GLint skipPixels
= 0;
502 /* in case width > MAX_WIDTH do the copy in chunks */
503 while (skipPixels
< width
) {
504 const GLint spanWidth
= MIN2(width
- skipPixels
, MAX_WIDTH
);
506 ASSERT(span
.end
<= MAX_WIDTH
);
507 for (row
= 0; row
< height
; row
++) {
508 const GLvoid
*zSrc
= _mesa_image_address2d(unpack
,
509 pixels
, width
, height
,
510 GL_DEPTH_COMPONENT
, type
,
513 /* Set these for each row since the _swrast_write_* function may
514 * change them while clipping.
516 span
.x
= x
+ skipPixels
;
518 span
.end
= spanWidth
;
520 _mesa_unpack_depth_span(ctx
, spanWidth
,
521 GL_UNSIGNED_INT
, span
.array
->z
, depthMax
,
524 _swrast_write_zoomed_depth_span(ctx
, x
, y
, &span
);
526 else if (ctx
->Visual
.rgbMode
) {
527 _swrast_write_rgba_span(ctx
, &span
);
530 _swrast_write_index_span(ctx
, &span
);
533 skipPixels
+= spanWidth
;
544 draw_rgba_pixels( GLcontext
*ctx
, GLint x
, GLint y
,
545 GLsizei width
, GLsizei height
,
546 GLenum format
, GLenum type
,
547 const struct gl_pixelstore_attrib
*unpack
,
548 const GLvoid
*pixels
)
550 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
551 const GLint imgX
= x
, imgY
= y
;
552 const GLboolean zoom
= ctx
->Pixel
.ZoomX
!=1.0 || ctx
->Pixel
.ZoomY
!=1.0;
553 GLfloat
*convImage
= NULL
;
554 GLbitfield transferOps
= ctx
->_ImageTransferState
;
557 /* Try an optimized glDrawPixels first */
558 if (fast_draw_rgba_pixels(ctx
, x
, y
, width
, height
, format
, type
,
562 INIT_SPAN(span
, GL_BITMAP
, 0, 0, SPAN_RGBA
);
564 _swrast_span_default_z(ctx
, &span
);
565 if (swrast
->_FogEnabled
)
566 _swrast_span_default_fog(ctx
, &span
);
567 if (ctx
->Texture
._EnabledCoordUnits
)
568 _swrast_span_default_texcoords(ctx
, &span
);
570 if (ctx
->Pixel
.Convolution2DEnabled
|| ctx
->Pixel
.Separable2DEnabled
) {
571 /* Convolution has to be handled specially. We'll create an
572 * intermediate image, applying all pixel transfer operations
573 * up to convolution. Then we'll convolve the image. Then
574 * we'll proceed with the rest of the transfer operations and
575 * rasterize the image.
578 GLfloat
*dest
, *tmpImage
;
580 tmpImage
= (GLfloat
*) _mesa_malloc(width
* height
* 4 * sizeof(GLfloat
));
582 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glDrawPixels");
585 convImage
= (GLfloat
*) _mesa_malloc(width
* height
* 4 * sizeof(GLfloat
));
587 _mesa_free(tmpImage
);
588 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glDrawPixels");
592 /* Unpack the image and apply transfer ops up to convolution */
594 for (row
= 0; row
< height
; row
++) {
595 const GLvoid
*source
= _mesa_image_address2d(unpack
,
596 pixels
, width
, height
, format
, type
, row
, 0);
597 _mesa_unpack_color_span_float(ctx
, width
, GL_RGBA
, (GLfloat
*) dest
,
598 format
, type
, source
, unpack
,
599 transferOps
& IMAGE_PRE_CONVOLUTION_BITS
);
604 if (ctx
->Pixel
.Convolution2DEnabled
) {
605 _mesa_convolve_2d_image(ctx
, &width
, &height
, tmpImage
, convImage
);
608 ASSERT(ctx
->Pixel
.Separable2DEnabled
);
609 _mesa_convolve_sep_image(ctx
, &width
, &height
, tmpImage
, convImage
);
611 _mesa_free(tmpImage
);
613 /* continue transfer ops and draw the convolved image */
614 unpack
= &ctx
->DefaultPacking
;
618 transferOps
&= IMAGE_POST_CONVOLUTION_BITS
;
621 if (ctx
->DrawBuffer
->_NumColorDrawBuffers
[0] > 0 &&
622 ctx
->DrawBuffer
->_ColorDrawBuffers
[0][0]->DataType
!= GL_FLOAT
&&
623 ctx
->Color
.ClampFragmentColor
!= GL_FALSE
) {
624 /* need to clamp colors before applying fragment ops */
625 transferOps
|= IMAGE_CLAMP_BIT
;
632 const GLboolean sink
= (ctx
->Pixel
.MinMaxEnabled
&& ctx
->MinMax
.Sink
)
633 || (ctx
->Pixel
.HistogramEnabled
&& ctx
->Histogram
.Sink
);
634 const GLbitfield interpMask
= span
.interpMask
;
635 const GLbitfield arrayMask
= span
.arrayMask
;
636 const GLint srcStride
637 = _mesa_image_row_stride(unpack
, width
, format
, type
);
638 GLint skipPixels
= 0;
639 /* use span array for temp color storage */
640 GLfloat
*rgba
= (GLfloat
*) span
.array
->color
.sz4
.rgba
;
642 /* if the span is wider than MAX_WIDTH we have to do it in chunks */
643 while (skipPixels
< width
) {
644 const GLint spanWidth
= MIN2(width
- skipPixels
, MAX_WIDTH
);
645 const GLubyte
*source
646 = (const GLubyte
*) _mesa_image_address2d(unpack
, pixels
,
647 width
, height
, format
,
648 type
, 0, skipPixels
);
651 for (row
= 0; row
< height
; row
++) {
652 /* get image row as float/RGBA */
653 _mesa_unpack_color_span_float(ctx
, spanWidth
, GL_RGBA
, rgba
,
654 format
, type
, source
, unpack
,
658 /* Set these for each row since the _swrast_write_* functions
659 * may change them while clipping/rendering.
661 span
.array
->ChanType
= GL_FLOAT
;
662 span
.x
= x
+ skipPixels
;
664 span
.end
= spanWidth
;
665 span
.arrayMask
= arrayMask
;
666 span
.interpMask
= interpMask
;
668 _swrast_write_zoomed_rgba_span(ctx
, imgX
, imgY
, &span
, rgba
);
671 _swrast_write_rgba_span(ctx
, &span
);
678 skipPixels
+= spanWidth
;
679 } /* while skipPixels < width */
681 /* XXX this is ugly/temporary, to undo above change */
682 span
.array
->ChanType
= CHAN_TYPE
;
686 _mesa_free(convImage
);
692 * This is a bit different from drawing GL_DEPTH_COMPONENT pixels.
693 * The only per-pixel operations that apply are depth scale/bias,
694 * stencil offset/shift, GL_DEPTH_WRITEMASK and GL_STENCIL_WRITEMASK,
696 * Also, only the depth buffer and stencil buffers are touched, not the
700 draw_depth_stencil_pixels(GLcontext
*ctx
, GLint x
, GLint y
,
701 GLsizei width
, GLsizei height
, GLenum type
,
702 const struct gl_pixelstore_attrib
*unpack
,
703 const GLvoid
*pixels
)
705 const GLint imgX
= x
, imgY
= y
;
706 const GLboolean scaleOrBias
707 = ctx
->Pixel
.DepthScale
!= 1.0 || ctx
->Pixel
.DepthBias
!= 0.0;
708 const GLfloat depthScale
= ctx
->DrawBuffer
->_DepthMaxF
;
709 const GLuint stencilMask
= ctx
->Stencil
.WriteMask
[0];
710 const GLuint stencilType
= (STENCIL_BITS
== 8) ?
711 GL_UNSIGNED_BYTE
: GL_UNSIGNED_SHORT
;
712 const GLboolean zoom
= ctx
->Pixel
.ZoomX
!= 1.0 || ctx
->Pixel
.ZoomY
!= 1.0;
713 struct gl_renderbuffer
*depthRb
, *stencilRb
;
714 struct gl_pixelstore_attrib clippedUnpack
= *unpack
;
717 if (!_mesa_clip_drawpixels(ctx
, &x
, &y
, &width
, &height
,
719 /* totally clipped */
724 depthRb
= ctx
->ReadBuffer
->Attachment
[BUFFER_DEPTH
].Renderbuffer
;
725 stencilRb
= ctx
->ReadBuffer
->Attachment
[BUFFER_STENCIL
].Renderbuffer
;
729 if (depthRb
->_BaseFormat
== GL_DEPTH_STENCIL_EXT
&&
730 stencilRb
->_BaseFormat
== GL_DEPTH_STENCIL_EXT
&&
731 depthRb
== stencilRb
&&
735 (stencilMask
& 0xff) == 0xff) {
736 /* This is the ideal case.
737 * Drawing GL_DEPTH_STENCIL pixels into a combined depth/stencil buffer.
738 * Plus, no pixel transfer ops, zooming, or masking needed.
741 for (i
= 0; i
< height
; i
++) {
742 const GLuint
*src
= (const GLuint
*)
743 _mesa_image_address2d(&clippedUnpack
, pixels
, width
, height
,
744 GL_DEPTH_STENCIL_EXT
, type
, i
, 0);
745 depthRb
->PutRow(ctx
, depthRb
, width
, x
, y
+ i
, src
, NULL
);
749 /* sub-optimal cases:
750 * Separate depth/stencil buffers, or pixel transfer ops required.
752 /* XXX need to handle very wide images (skippixels) */
755 depthRb
= ctx
->DrawBuffer
->_DepthBuffer
;
756 stencilRb
= ctx
->DrawBuffer
->_StencilBuffer
;
758 for (i
= 0; i
< height
; i
++) {
759 const GLuint
*depthStencilSrc
= (const GLuint
*)
760 _mesa_image_address2d(&clippedUnpack
, pixels
, width
, height
,
761 GL_DEPTH_STENCIL_EXT
, type
, i
, 0);
763 if (ctx
->Depth
.Mask
) {
764 if (!scaleOrBias
&& ctx
->DrawBuffer
->Visual
.depthBits
== 24) {
765 /* fast path 24-bit zbuffer */
766 GLuint zValues
[MAX_WIDTH
];
768 ASSERT(depthRb
->DataType
== GL_UNSIGNED_INT
);
769 for (j
= 0; j
< width
; j
++) {
770 zValues
[j
] = depthStencilSrc
[j
] >> 8;
773 _swrast_write_zoomed_z_span(ctx
, imgX
, imgY
, width
,
776 depthRb
->PutRow(ctx
, depthRb
, width
, x
, y
+ i
, zValues
,NULL
);
778 else if (!scaleOrBias
&& ctx
->DrawBuffer
->Visual
.depthBits
== 16) {
779 /* fast path 16-bit zbuffer */
780 GLushort zValues
[MAX_WIDTH
];
782 ASSERT(depthRb
->DataType
== GL_UNSIGNED_SHORT
);
783 for (j
= 0; j
< width
; j
++) {
784 zValues
[j
] = depthStencilSrc
[j
] >> 16;
787 _swrast_write_zoomed_z_span(ctx
, imgX
, imgY
, width
,
790 depthRb
->PutRow(ctx
, depthRb
, width
, x
, y
+ i
, zValues
,NULL
);
794 GLuint zValues
[MAX_WIDTH
]; /* 16 or 32-bit Z value storage */
795 _mesa_unpack_depth_span(ctx
, width
,
796 depthRb
->DataType
, zValues
, depthScale
,
797 type
, depthStencilSrc
, &clippedUnpack
);
799 _swrast_write_zoomed_z_span(ctx
, imgX
, imgY
, width
, x
,
803 depthRb
->PutRow(ctx
, depthRb
, width
, x
, y
+ i
, zValues
,NULL
);
808 if (stencilMask
!= 0x0) {
809 GLstencil stencilValues
[MAX_WIDTH
];
810 /* get stencil values, with shift/offset/mapping */
811 _mesa_unpack_stencil_span(ctx
, width
, stencilType
, stencilValues
,
812 type
, depthStencilSrc
, &clippedUnpack
,
813 ctx
->_ImageTransferState
);
815 _swrast_write_zoomed_stencil_span(ctx
, imgX
, imgY
, width
,
816 x
, y
+ i
, stencilValues
);
818 _swrast_write_stencil_span(ctx
, width
, x
, y
+ i
, stencilValues
);
827 * Execute software-based glDrawPixels.
828 * By time we get here, all error checking will have been done.
831 _swrast_DrawPixels( GLcontext
*ctx
,
833 GLsizei width
, GLsizei height
,
834 GLenum format
, GLenum type
,
835 const struct gl_pixelstore_attrib
*unpack
,
836 const GLvoid
*pixels
)
838 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
840 RENDER_START(swrast
,ctx
);
843 _mesa_update_state(ctx
);
845 if (swrast
->NewState
)
846 _swrast_validate_derived( ctx
);
848 if (unpack
->BufferObj
->Name
) {
849 /* unpack from PBO */
851 if (!_mesa_validate_pbo_access(2, unpack
, width
, height
, 1,
852 format
, type
, pixels
)) {
853 _mesa_error(ctx
, GL_INVALID_OPERATION
,
854 "glDrawPixels(invalid PBO access)");
857 buf
= (GLubyte
*) ctx
->Driver
.MapBuffer(ctx
, GL_PIXEL_UNPACK_BUFFER_EXT
,
861 /* buffer is already mapped - that's an error */
862 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glDrawPixels(PBO is mapped)");
865 pixels
= ADD_POINTERS(buf
, pixels
);
869 case GL_STENCIL_INDEX
:
870 draw_stencil_pixels( ctx
, x
, y
, width
, height
, type
, unpack
, pixels
);
872 case GL_DEPTH_COMPONENT
:
873 draw_depth_pixels( ctx
, x
, y
, width
, height
, type
, unpack
, pixels
);
876 if (ctx
->Visual
.rgbMode
)
877 draw_rgba_pixels(ctx
, x
,y
, width
, height
, format
, type
, unpack
, pixels
);
879 draw_index_pixels(ctx
, x
, y
, width
, height
, type
, unpack
, pixels
);
886 case GL_LUMINANCE_ALPHA
:
892 draw_rgba_pixels(ctx
, x
, y
, width
, height
, format
, type
, unpack
, pixels
);
894 case GL_DEPTH_STENCIL_EXT
:
895 draw_depth_stencil_pixels(ctx
, x
, y
, width
, height
,
896 type
, unpack
, pixels
);
899 _mesa_problem(ctx
, "unexpected format in _swrast_DrawPixels");
900 /* don't return yet, clean-up */
905 RENDER_FINISH(swrast
,ctx
);
907 if (unpack
->BufferObj
->Name
) {
908 /* done with PBO so unmap it now */
909 ctx
->Driver
.UnmapBuffer(ctx
, GL_PIXEL_UNPACK_BUFFER_EXT
,
916 #if 0 /* experimental */
918 * Execute glDrawDepthPixelsMESA().
921 _swrast_DrawDepthPixelsMESA( GLcontext
*ctx
,
923 GLsizei width
, GLsizei height
,
924 GLenum colorFormat
, GLenum colorType
,
925 const GLvoid
*colors
,
926 GLenum depthType
, const GLvoid
*depths
,
927 const struct gl_pixelstore_attrib
*unpack
)
929 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
931 if (swrast
->NewState
)
932 _swrast_validate_derived( ctx
);
934 RENDER_START(swrast
,ctx
);
936 switch (colorFormat
) {
938 if (ctx
->Visual
.rgbMode
)
939 draw_rgba_pixels(ctx
, x
,y
, width
, height
, colorFormat
, colorType
,
942 draw_index_pixels(ctx
, x
, y
, width
, height
, colorType
,
950 case GL_LUMINANCE_ALPHA
:
956 draw_rgba_pixels(ctx
, x
, y
, width
, height
, colorFormat
, colorType
,
960 _mesa_problem(ctx
, "unexpected format in glDrawDepthPixelsMESA");
963 RENDER_FINISH(swrast
,ctx
);