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/context.h"
28 #include "main/colormac.h"
29 #include "main/convolve.h"
30 #include "main/histogram.h"
31 #include "main/image.h"
32 #include "main/macros.h"
33 #include "main/imports.h"
34 #include "main/pixel.h"
36 #include "s_context.h"
39 #include "s_stencil.h"
45 * Determine if there's overlap in an image copy.
46 * This test also compensates for the fact that copies are done from
47 * bottom to top and overlaps can sometimes be handled correctly
48 * without making a temporary image copy.
49 * \return GL_TRUE if the regions overlap, GL_FALSE otherwise.
52 regions_overlap(GLint srcx
, GLint srcy
,
53 GLint dstx
, GLint dsty
,
54 GLint width
, GLint height
,
55 GLfloat zoomX
, GLfloat zoomY
)
57 if (zoomX
== 1.0 && zoomY
== 1.0) {
59 if (srcx
>= dstx
+ width
|| (srcx
+ width
<= dstx
)) {
62 else if (srcy
< dsty
) { /* this is OK */
65 else if (srcy
> dsty
+ height
) {
73 /* add one pixel of slop when zooming, just to be safe */
74 if (srcx
> (dstx
+ ((zoomX
> 0.0F
) ? (width
* zoomX
+ 1.0F
) : 0.0F
))) {
75 /* src is completely right of dest */
78 else if (srcx
+ width
+ 1.0F
< dstx
+ ((zoomX
> 0.0F
) ? 0.0F
: (width
* zoomX
))) {
79 /* src is completely left of dest */
82 else if ((srcy
< dsty
) && (srcy
+ height
< dsty
+ (height
* zoomY
))) {
83 /* src is completely below dest */
86 else if ((srcy
> dsty
) && (srcy
+ height
> dsty
+ (height
* zoomY
))) {
87 /* src is completely above dest */
98 * RGBA copypixels with convolution.
101 copy_conv_rgba_pixels(GLcontext
*ctx
, GLint srcx
, GLint srcy
,
102 GLint width
, GLint height
, GLint destx
, GLint desty
)
105 const GLboolean zoom
= ctx
->Pixel
.ZoomX
!= 1.0F
|| ctx
->Pixel
.ZoomY
!= 1.0F
;
106 const GLbitfield transferOps
= ctx
->_ImageTransferState
;
107 const GLboolean sink
= (ctx
->Pixel
.MinMaxEnabled
&& ctx
->MinMax
.Sink
)
108 || (ctx
->Pixel
.HistogramEnabled
&& ctx
->Histogram
.Sink
);
109 GLfloat
*dest
, *tmpImage
, *convImage
;
112 INIT_SPAN(span
, GL_BITMAP
);
113 _swrast_span_default_attribs(ctx
, &span
);
114 span
.arrayMask
= SPAN_RGBA
;
115 span
.arrayAttribs
= FRAG_BIT_COL0
;
117 /* allocate space for GLfloat image */
118 tmpImage
= (GLfloat
*) _mesa_malloc(width
* height
* 4 * sizeof(GLfloat
));
120 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyPixels");
123 convImage
= (GLfloat
*) _mesa_malloc(width
* height
* 4 * sizeof(GLfloat
));
125 _mesa_free(tmpImage
);
126 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyPixels");
130 /* read source image as float/RGBA */
132 for (row
= 0; row
< height
; row
++) {
133 _swrast_read_rgba_span(ctx
, ctx
->ReadBuffer
->_ColorReadBuffer
,
134 width
, srcx
, srcy
+ row
, GL_FLOAT
, dest
);
138 /* do the image transfer ops which preceed convolution */
139 for (row
= 0; row
< height
; row
++) {
140 GLfloat (*rgba
)[4] = (GLfloat (*)[4]) (tmpImage
+ row
* width
* 4);
141 _mesa_apply_rgba_transfer_ops(ctx
,
142 transferOps
& IMAGE_PRE_CONVOLUTION_BITS
,
147 if (ctx
->Pixel
.Convolution2DEnabled
) {
148 _mesa_convolve_2d_image(ctx
, &width
, &height
, tmpImage
, convImage
);
151 ASSERT(ctx
->Pixel
.Separable2DEnabled
);
152 _mesa_convolve_sep_image(ctx
, &width
, &height
, tmpImage
, convImage
);
154 _mesa_free(tmpImage
);
156 /* do remaining post-convolution image transfer ops */
157 for (row
= 0; row
< height
; row
++) {
158 GLfloat (*rgba
)[4] = (GLfloat (*)[4]) (convImage
+ row
* width
* 4);
159 _mesa_apply_rgba_transfer_ops(ctx
,
160 transferOps
& IMAGE_POST_CONVOLUTION_BITS
,
165 /* write the new image */
166 for (row
= 0; row
< height
; row
++) {
167 const GLfloat
*src
= convImage
+ row
* width
* 4;
168 GLfloat
*rgba
= (GLfloat
*) span
.array
->attribs
[FRAG_ATTRIB_COL0
];
170 /* copy convolved colors into span array */
171 _mesa_memcpy(rgba
, src
, width
* 4 * sizeof(GLfloat
));
175 span
.y
= desty
+ row
;
177 span
.array
->ChanType
= GL_FLOAT
;
179 _swrast_write_zoomed_rgba_span(ctx
, destx
, desty
, &span
, rgba
);
182 _swrast_write_rgba_span(ctx
, &span
);
186 span
.array
->ChanType
= CHAN_TYPE
;
189 _mesa_free(convImage
);
197 copy_rgba_pixels(GLcontext
*ctx
, GLint srcx
, GLint srcy
,
198 GLint width
, GLint height
, GLint destx
, GLint desty
)
200 GLfloat
*tmpImage
, *p
;
201 GLint sy
, dy
, stepy
, row
;
202 const GLboolean zoom
= ctx
->Pixel
.ZoomX
!= 1.0F
|| ctx
->Pixel
.ZoomY
!= 1.0F
;
204 GLuint transferOps
= ctx
->_ImageTransferState
;
207 if (!ctx
->ReadBuffer
->_ColorReadBuffer
) {
208 /* no readbuffer - OK */
212 if (ctx
->Pixel
.Convolution2DEnabled
|| ctx
->Pixel
.Separable2DEnabled
) {
213 copy_conv_rgba_pixels(ctx
, srcx
, srcy
, width
, height
, destx
, desty
);
216 else if (ctx
->Pixel
.Convolution1DEnabled
) {
217 /* make sure we don't apply 1D convolution */
218 transferOps
&= ~(IMAGE_CONVOLUTION_BIT
|
219 IMAGE_POST_CONVOLUTION_SCALE_BIAS
);
222 if (ctx
->DrawBuffer
== ctx
->ReadBuffer
) {
223 overlapping
= regions_overlap(srcx
, srcy
, destx
, desty
, width
, height
,
224 ctx
->Pixel
.ZoomX
, ctx
->Pixel
.ZoomY
);
227 overlapping
= GL_FALSE
;
230 /* Determine if copy should be done bottom-to-top or top-to-bottom */
231 if (!overlapping
&& srcy
< desty
) {
232 /* top-down max-to-min */
233 sy
= srcy
+ height
- 1;
234 dy
= desty
+ height
- 1;
238 /* bottom-up min-to-max */
244 INIT_SPAN(span
, GL_BITMAP
);
245 _swrast_span_default_attribs(ctx
, &span
);
246 span
.arrayMask
= SPAN_RGBA
;
247 span
.arrayAttribs
= FRAG_BIT_COL0
; /* we'll fill in COL0 attrib values */
250 tmpImage
= (GLfloat
*) _mesa_malloc(width
* height
* sizeof(GLfloat
) * 4);
252 _mesa_error( ctx
, GL_OUT_OF_MEMORY
, "glCopyPixels" );
255 /* read the source image as RGBA/float */
257 for (row
= 0; row
< height
; row
++) {
258 _swrast_read_rgba_span( ctx
, ctx
->ReadBuffer
->_ColorReadBuffer
,
259 width
, srcx
, sy
+ row
, GL_FLOAT
, p
);
265 tmpImage
= NULL
; /* silence compiler warnings */
269 ASSERT(width
< MAX_WIDTH
);
271 for (row
= 0; row
< height
; row
++, sy
+= stepy
, dy
+= stepy
) {
272 GLvoid
*rgba
= span
.array
->attribs
[FRAG_ATTRIB_COL0
];
274 /* Get row/span of source pixels */
276 /* get from buffered image */
277 _mesa_memcpy(rgba
, p
, width
* sizeof(GLfloat
) * 4);
281 /* get from framebuffer */
282 _swrast_read_rgba_span( ctx
, ctx
->ReadBuffer
->_ColorReadBuffer
,
283 width
, srcx
, sy
, GL_FLOAT
, rgba
);
287 _mesa_apply_rgba_transfer_ops(ctx
, transferOps
, width
,
288 (GLfloat (*)[4]) rgba
);
291 /* Write color span */
295 span
.array
->ChanType
= GL_FLOAT
;
297 _swrast_write_zoomed_rgba_span(ctx
, destx
, desty
, &span
, rgba
);
300 _swrast_write_rgba_span(ctx
, &span
);
304 span
.array
->ChanType
= CHAN_TYPE
; /* restore */
307 _mesa_free(tmpImage
);
312 copy_ci_pixels( GLcontext
*ctx
, GLint srcx
, GLint srcy
,
313 GLint width
, GLint height
,
314 GLint destx
, GLint desty
)
319 const GLboolean zoom
= ctx
->Pixel
.ZoomX
!= 1.0F
|| ctx
->Pixel
.ZoomY
!= 1.0F
;
323 if (!ctx
->ReadBuffer
->_ColorReadBuffer
) {
324 /* no readbuffer - OK */
328 INIT_SPAN(span
, GL_BITMAP
);
329 _swrast_span_default_attribs(ctx
, &span
);
330 span
.arrayMask
= SPAN_INDEX
;
332 if (ctx
->DrawBuffer
== ctx
->ReadBuffer
) {
333 overlapping
= regions_overlap(srcx
, srcy
, destx
, desty
, width
, height
,
334 ctx
->Pixel
.ZoomX
, ctx
->Pixel
.ZoomY
);
337 overlapping
= GL_FALSE
;
340 /* Determine if copy should be bottom-to-top or top-to-bottom */
341 if (!overlapping
&& srcy
< desty
) {
342 /* top-down max-to-min */
343 sy
= srcy
+ height
- 1;
344 dy
= desty
+ height
- 1;
348 /* bottom-up min-to-max */
356 tmpImage
= (GLuint
*) _mesa_malloc(width
* height
* sizeof(GLuint
));
358 _mesa_error( ctx
, GL_OUT_OF_MEMORY
, "glCopyPixels" );
363 for (j
= 0; j
< height
; j
++, ssy
+= stepy
) {
364 _swrast_read_index_span( ctx
, ctx
->ReadBuffer
->_ColorReadBuffer
,
365 width
, srcx
, ssy
, p
);
371 tmpImage
= NULL
; /* silence compiler warning */
375 for (j
= 0; j
< height
; j
++, sy
+= stepy
, dy
+= stepy
) {
376 /* Get color indexes */
378 _mesa_memcpy(span
.array
->index
, p
, width
* sizeof(GLuint
));
382 _swrast_read_index_span( ctx
, ctx
->ReadBuffer
->_ColorReadBuffer
,
383 width
, srcx
, sy
, span
.array
->index
);
386 if (ctx
->_ImageTransferState
)
387 _mesa_apply_ci_transfer_ops(ctx
, ctx
->_ImageTransferState
,
388 width
, span
.array
->index
);
390 /* write color indexes */
395 _swrast_write_zoomed_index_span(ctx
, destx
, desty
, &span
);
397 _swrast_write_index_span(ctx
, &span
);
401 _mesa_free(tmpImage
);
406 * Convert floating point Z values to integer Z values with pixel transfer's
410 scale_and_bias_z(GLcontext
*ctx
, GLuint width
,
411 const GLfloat depth
[], GLuint z
[])
413 const GLuint depthMax
= ctx
->DrawBuffer
->_DepthMax
;
416 if (depthMax
<= 0xffffff &&
417 ctx
->Pixel
.DepthScale
== 1.0 &&
418 ctx
->Pixel
.DepthBias
== 0.0) {
419 /* no scale or bias and no clamping and no worry of overflow */
420 const GLfloat depthMaxF
= ctx
->DrawBuffer
->_DepthMaxF
;
421 for (i
= 0; i
< width
; i
++) {
422 z
[i
] = (GLuint
) (depth
[i
] * depthMaxF
);
426 /* need to be careful with overflow */
427 const GLdouble depthMaxF
= ctx
->DrawBuffer
->_DepthMaxF
;
428 for (i
= 0; i
< width
; i
++) {
429 GLdouble d
= depth
[i
] * ctx
->Pixel
.DepthScale
+ ctx
->Pixel
.DepthBias
;
430 d
= CLAMP(d
, 0.0, 1.0) * depthMaxF
;
445 copy_depth_pixels( GLcontext
*ctx
, GLint srcx
, GLint srcy
,
446 GLint width
, GLint height
,
447 GLint destx
, GLint desty
)
449 struct gl_framebuffer
*fb
= ctx
->ReadBuffer
;
450 struct gl_renderbuffer
*readRb
= fb
->_DepthBuffer
;
451 GLfloat
*p
, *tmpImage
;
454 const GLboolean zoom
= ctx
->Pixel
.ZoomX
!= 1.0F
|| ctx
->Pixel
.ZoomY
!= 1.0F
;
459 /* no readbuffer - OK */
463 INIT_SPAN(span
, GL_BITMAP
);
464 _swrast_span_default_attribs(ctx
, &span
);
465 span
.arrayMask
= SPAN_Z
;
467 if (ctx
->DrawBuffer
== ctx
->ReadBuffer
) {
468 overlapping
= regions_overlap(srcx
, srcy
, destx
, desty
, width
, height
,
469 ctx
->Pixel
.ZoomX
, ctx
->Pixel
.ZoomY
);
472 overlapping
= GL_FALSE
;
475 /* Determine if copy should be bottom-to-top or top-to-bottom */
476 if (!overlapping
&& srcy
< desty
) {
477 /* top-down max-to-min */
478 sy
= srcy
+ height
- 1;
479 dy
= desty
+ height
- 1;
483 /* bottom-up min-to-max */
491 tmpImage
= (GLfloat
*) _mesa_malloc(width
* height
* sizeof(GLfloat
));
493 _mesa_error( ctx
, GL_OUT_OF_MEMORY
, "glCopyPixels" );
497 for (j
= 0; j
< height
; j
++, ssy
+= stepy
) {
498 _swrast_read_depth_span_float(ctx
, readRb
, width
, srcx
, ssy
, p
);
504 tmpImage
= NULL
; /* silence compiler warning */
508 for (j
= 0; j
< height
; j
++, sy
+= stepy
, dy
+= stepy
) {
509 GLfloat depth
[MAX_WIDTH
];
510 /* get depth values */
512 _mesa_memcpy(depth
, p
, width
* sizeof(GLfloat
));
516 _swrast_read_depth_span_float(ctx
, readRb
, width
, srcx
, sy
, depth
);
519 /* apply scale and bias */
520 scale_and_bias_z(ctx
, width
, depth
, span
.array
->z
);
522 /* write depth values */
526 if (fb
->Visual
.rgbMode
) {
528 _swrast_write_zoomed_depth_span(ctx
, destx
, desty
, &span
);
530 _swrast_write_rgba_span(ctx
, &span
);
534 _swrast_write_zoomed_depth_span(ctx
, destx
, desty
, &span
);
536 _swrast_write_index_span(ctx
, &span
);
541 _mesa_free(tmpImage
);
547 copy_stencil_pixels( GLcontext
*ctx
, GLint srcx
, GLint srcy
,
548 GLint width
, GLint height
,
549 GLint destx
, GLint desty
)
551 struct gl_framebuffer
*fb
= ctx
->ReadBuffer
;
552 struct gl_renderbuffer
*rb
= fb
->_StencilBuffer
;
555 GLstencil
*p
, *tmpImage
;
556 const GLboolean zoom
= ctx
->Pixel
.ZoomX
!= 1.0F
|| ctx
->Pixel
.ZoomY
!= 1.0F
;
560 /* no readbuffer - OK */
564 if (ctx
->DrawBuffer
== ctx
->ReadBuffer
) {
565 overlapping
= regions_overlap(srcx
, srcy
, destx
, desty
, width
, height
,
566 ctx
->Pixel
.ZoomX
, ctx
->Pixel
.ZoomY
);
569 overlapping
= GL_FALSE
;
572 /* Determine if copy should be bottom-to-top or top-to-bottom */
573 if (!overlapping
&& srcy
< desty
) {
574 /* top-down max-to-min */
575 sy
= srcy
+ height
- 1;
576 dy
= desty
+ height
- 1;
580 /* bottom-up min-to-max */
588 tmpImage
= (GLstencil
*) _mesa_malloc(width
* height
* sizeof(GLstencil
));
590 _mesa_error( ctx
, GL_OUT_OF_MEMORY
, "glCopyPixels" );
594 for (j
= 0; j
< height
; j
++, ssy
+= stepy
) {
595 _swrast_read_stencil_span( ctx
, rb
, width
, srcx
, ssy
, p
);
601 tmpImage
= NULL
; /* silence compiler warning */
605 for (j
= 0; j
< height
; j
++, sy
+= stepy
, dy
+= stepy
) {
606 GLstencil stencil
[MAX_WIDTH
];
608 /* Get stencil values */
610 _mesa_memcpy(stencil
, p
, width
* sizeof(GLstencil
));
614 _swrast_read_stencil_span( ctx
, rb
, width
, srcx
, sy
, stencil
);
617 _mesa_apply_stencil_transfer_ops(ctx
, width
, stencil
);
619 /* Write stencil values */
621 _swrast_write_zoomed_stencil_span(ctx
, destx
, desty
, width
,
625 _swrast_write_stencil_span( ctx
, width
, destx
, dy
, stencil
);
630 _mesa_free(tmpImage
);
635 * This isn't terribly efficient. If a driver really has combined
636 * depth/stencil buffers the driver should implement an optimized
637 * CopyPixels function.
640 copy_depth_stencil_pixels(GLcontext
*ctx
,
641 const GLint srcX
, const GLint srcY
,
642 const GLint width
, const GLint height
,
643 const GLint destX
, const GLint destY
)
645 struct gl_renderbuffer
*stencilReadRb
, *depthReadRb
, *depthDrawRb
;
648 GLstencil
*tempStencilImage
= NULL
, *stencilPtr
= NULL
;
649 GLfloat
*tempDepthImage
= NULL
, *depthPtr
= NULL
;
650 const GLfloat depthScale
= ctx
->DrawBuffer
->_DepthMaxF
;
651 const GLuint stencilMask
= ctx
->Stencil
.WriteMask
[0];
652 const GLboolean zoom
= ctx
->Pixel
.ZoomX
!= 1.0F
|| ctx
->Pixel
.ZoomY
!= 1.0F
;
653 const GLboolean scaleOrBias
654 = ctx
->Pixel
.DepthScale
!= 1.0 || ctx
->Pixel
.DepthBias
!= 0.0;
657 depthDrawRb
= ctx
->DrawBuffer
->_DepthBuffer
;
658 depthReadRb
= ctx
->ReadBuffer
->_DepthBuffer
;
659 stencilReadRb
= ctx
->ReadBuffer
->_StencilBuffer
;
663 ASSERT(stencilReadRb
);
665 if (ctx
->DrawBuffer
== ctx
->ReadBuffer
) {
666 overlapping
= regions_overlap(srcX
, srcY
, destX
, destY
, width
, height
,
667 ctx
->Pixel
.ZoomX
, ctx
->Pixel
.ZoomY
);
670 overlapping
= GL_FALSE
;
673 /* Determine if copy should be bottom-to-top or top-to-bottom */
674 if (!overlapping
&& srcY
< destY
) {
675 /* top-down max-to-min */
676 sy
= srcY
+ height
- 1;
677 dy
= destY
+ height
- 1;
681 /* bottom-up min-to-max */
690 if (stencilMask
!= 0x0) {
692 = (GLstencil
*) _mesa_malloc(width
* height
* sizeof(GLstencil
));
693 if (!tempStencilImage
) {
694 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyPixels");
698 /* get copy of stencil pixels */
699 stencilPtr
= tempStencilImage
;
700 for (j
= 0; j
< height
; j
++, ssy
+= stepy
) {
701 _swrast_read_stencil_span(ctx
, stencilReadRb
,
702 width
, srcX
, ssy
, stencilPtr
);
705 stencilPtr
= tempStencilImage
;
708 if (ctx
->Depth
.Mask
) {
710 = (GLfloat
*) _mesa_malloc(width
* height
* sizeof(GLfloat
));
711 if (!tempDepthImage
) {
712 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyPixels");
713 _mesa_free(tempStencilImage
);
717 /* get copy of depth pixels */
718 depthPtr
= tempDepthImage
;
719 for (j
= 0; j
< height
; j
++, ssy
+= stepy
) {
720 _swrast_read_depth_span_float(ctx
, depthReadRb
,
721 width
, srcX
, ssy
, depthPtr
);
724 depthPtr
= tempDepthImage
;
728 for (j
= 0; j
< height
; j
++, sy
+= stepy
, dy
+= stepy
) {
729 if (stencilMask
!= 0x0) {
730 GLstencil stencil
[MAX_WIDTH
];
732 /* Get stencil values */
734 _mesa_memcpy(stencil
, stencilPtr
, width
* sizeof(GLstencil
));
738 _swrast_read_stencil_span(ctx
, stencilReadRb
,
739 width
, srcX
, sy
, stencil
);
742 _mesa_apply_stencil_transfer_ops(ctx
, width
, stencil
);
746 _swrast_write_zoomed_stencil_span(ctx
, destX
, destY
, width
,
750 _swrast_write_stencil_span( ctx
, width
, destX
, dy
, stencil
);
754 if (ctx
->Depth
.Mask
) {
755 GLfloat depth
[MAX_WIDTH
];
756 GLuint zVals32
[MAX_WIDTH
];
757 GLushort zVals16
[MAX_WIDTH
];
761 /* get depth values */
763 _mesa_memcpy(depth
, depthPtr
, width
* sizeof(GLfloat
));
767 _swrast_read_depth_span_float(ctx
, depthReadRb
,
768 width
, srcX
, sy
, depth
);
773 _mesa_scale_and_bias_depth(ctx
, width
, depth
);
775 /* convert to integer Z values */
776 if (depthDrawRb
->DataType
== GL_UNSIGNED_SHORT
) {
778 for (k
= 0; k
< width
; k
++)
779 zVals16
[k
] = (GLushort
) (depth
[k
] * depthScale
);
785 for (k
= 0; k
< width
; k
++)
786 zVals32
[k
] = (GLuint
) (depth
[k
] * depthScale
);
793 _swrast_write_zoomed_z_span(ctx
, destX
, destY
, width
,
797 _swrast_put_row(ctx
, depthDrawRb
, width
, destX
, dy
, zVals
, zBytes
);
802 if (tempStencilImage
)
803 _mesa_free(tempStencilImage
);
806 _mesa_free(tempDepthImage
);
812 * Try to do a fast copy pixels.
815 fast_copy_pixels(GLcontext
*ctx
,
816 GLint srcX
, GLint srcY
, GLsizei width
, GLsizei height
,
817 GLint dstX
, GLint dstY
, GLenum type
)
819 struct gl_framebuffer
*srcFb
= ctx
->ReadBuffer
;
820 struct gl_framebuffer
*dstFb
= ctx
->DrawBuffer
;
821 struct gl_renderbuffer
*srcRb
, *dstRb
;
824 if (SWRAST_CONTEXT(ctx
)->_RasterMask
!= 0x0 ||
825 ctx
->Pixel
.ZoomX
!= 1.0F
||
826 ctx
->Pixel
.ZoomY
!= 1.0F
||
827 ctx
->_ImageTransferState
) {
828 /* can't handle these */
832 if (type
== GL_COLOR
) {
833 if (dstFb
->_NumColorDrawBuffers
!= 1)
835 srcRb
= srcFb
->_ColorReadBuffer
;
836 dstRb
= dstFb
->_ColorDrawBuffers
[0];
838 else if (type
== GL_STENCIL
) {
839 srcRb
= srcFb
->_StencilBuffer
;
840 dstRb
= dstFb
->_StencilBuffer
;
842 else if (type
== GL_DEPTH
) {
843 srcRb
= srcFb
->_DepthBuffer
;
844 dstRb
= dstFb
->_DepthBuffer
;
847 ASSERT(type
== GL_DEPTH_STENCIL_EXT
);
849 srcRb
= srcFb
->Attachment
[BUFFER_DEPTH
].Renderbuffer
;
850 dstRb
= dstFb
->Attachment
[BUFFER_DEPTH
].Renderbuffer
;
853 /* src and dst renderbuffers must be same format and type */
854 if (!srcRb
|| !dstRb
||
855 srcRb
->DataType
!= dstRb
->DataType
||
856 srcRb
->_BaseFormat
!= dstRb
->_BaseFormat
) {
860 /* clipping not supported */
861 if (srcX
< 0 || srcX
+ width
> (GLint
) srcFb
->Width
||
862 srcY
< 0 || srcY
+ height
> (GLint
) srcFb
->Height
||
863 dstX
< dstFb
->_Xmin
|| dstX
+ width
> dstFb
->_Xmax
||
864 dstY
< dstFb
->_Ymin
|| dstY
+ height
> dstFb
->_Ymax
) {
868 /* overlapping src/dst doesn't matter, just determine Y direction */
870 /* top-down max-to-min */
871 srcY
= srcY
+ height
- 1;
872 dstY
= dstY
+ height
- 1;
876 /* bottom-up min-to-max */
880 for (row
= 0; row
< height
; row
++) {
881 GLuint temp
[MAX_WIDTH
][4];
882 srcRb
->GetRow(ctx
, srcRb
, width
, srcX
, srcY
, temp
);
883 dstRb
->PutRow(ctx
, dstRb
, width
, dstX
, dstY
, temp
, NULL
);
893 * Do software-based glCopyPixels.
894 * By time we get here, all parameters will have been error-checked.
897 _swrast_CopyPixels( GLcontext
*ctx
,
898 GLint srcx
, GLint srcy
, GLsizei width
, GLsizei height
,
899 GLint destx
, GLint desty
, GLenum type
)
901 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
902 swrast_render_start(ctx
);
904 if (swrast
->NewState
)
905 _swrast_validate_derived( ctx
);
907 if (!fast_copy_pixels(ctx
, srcx
, srcy
, width
, height
, destx
, desty
, type
)) {
910 if (ctx
->Visual
.rgbMode
) {
911 copy_rgba_pixels( ctx
, srcx
, srcy
, width
, height
, destx
, desty
);
914 copy_ci_pixels( ctx
, srcx
, srcy
, width
, height
, destx
, desty
);
918 copy_depth_pixels( ctx
, srcx
, srcy
, width
, height
, destx
, desty
);
921 copy_stencil_pixels( ctx
, srcx
, srcy
, width
, height
, destx
, desty
);
923 case GL_DEPTH_STENCIL_EXT
:
924 copy_depth_stencil_pixels(ctx
, srcx
, srcy
, width
, height
, destx
, desty
);
927 _mesa_problem(ctx
, "unexpected type in _swrast_CopyPixels");
931 swrast_render_finish(ctx
);