2 * Mesa 3-D graphics library
4 * Copyright (C) 1999-2007 Brian Paul All Rights Reserved.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * OTHER DEALINGS IN THE SOFTWARE.
26 #include "main/glheader.h"
27 #include "main/context.h"
28 #include "main/condrender.h"
29 #include "main/macros.h"
30 #include "main/pixeltransfer.h"
31 #include "main/imports.h"
33 #include "s_context.h"
36 #include "s_stencil.h"
42 * Determine if there's overlap in an image copy.
43 * This test also compensates for the fact that copies are done from
44 * bottom to top and overlaps can sometimes be handled correctly
45 * without making a temporary image copy.
46 * \return GL_TRUE if the regions overlap, GL_FALSE otherwise.
49 regions_overlap(GLint srcx
, GLint srcy
,
50 GLint dstx
, GLint dsty
,
51 GLint width
, GLint height
,
52 GLfloat zoomX
, GLfloat zoomY
)
54 if (zoomX
== 1.0 && zoomY
== 1.0) {
56 if (srcx
>= dstx
+ width
|| (srcx
+ width
<= dstx
)) {
59 else if (srcy
< dsty
) { /* this is OK */
62 else if (srcy
> dsty
+ height
) {
70 /* add one pixel of slop when zooming, just to be safe */
71 if (srcx
> (dstx
+ ((zoomX
> 0.0F
) ? (width
* zoomX
+ 1.0F
) : 0.0F
))) {
72 /* src is completely right of dest */
75 else if (srcx
+ width
+ 1.0F
< dstx
+ ((zoomX
> 0.0F
) ? 0.0F
: (width
* zoomX
))) {
76 /* src is completely left of dest */
79 else if ((srcy
< dsty
) && (srcy
+ height
< dsty
+ (height
* zoomY
))) {
80 /* src is completely below dest */
83 else if ((srcy
> dsty
) && (srcy
+ height
> dsty
+ (height
* zoomY
))) {
84 /* src is completely above dest */
98 copy_rgba_pixels(struct gl_context
*ctx
, GLint srcx
, GLint srcy
,
99 GLint width
, GLint height
, GLint destx
, GLint desty
)
101 GLfloat
*tmpImage
, *p
;
102 GLint sy
, dy
, stepy
, row
;
103 const GLboolean zoom
= ctx
->Pixel
.ZoomX
!= 1.0F
|| ctx
->Pixel
.ZoomY
!= 1.0F
;
105 GLuint transferOps
= ctx
->_ImageTransferState
;
108 if (!ctx
->ReadBuffer
->_ColorReadBuffer
) {
109 /* no readbuffer - OK */
113 if (ctx
->DrawBuffer
== ctx
->ReadBuffer
) {
114 overlapping
= regions_overlap(srcx
, srcy
, destx
, desty
, width
, height
,
115 ctx
->Pixel
.ZoomX
, ctx
->Pixel
.ZoomY
);
118 overlapping
= GL_FALSE
;
121 /* Determine if copy should be done bottom-to-top or top-to-bottom */
122 if (!overlapping
&& srcy
< desty
) {
123 /* top-down max-to-min */
124 sy
= srcy
+ height
- 1;
125 dy
= desty
+ height
- 1;
129 /* bottom-up min-to-max */
135 INIT_SPAN(span
, GL_BITMAP
);
136 _swrast_span_default_attribs(ctx
, &span
);
137 span
.arrayMask
= SPAN_RGBA
;
138 span
.arrayAttribs
= VARYING_BIT_COL0
; /* we'll fill in COL0 attrib values */
141 tmpImage
= malloc(width
* height
* sizeof(GLfloat
) * 4);
143 _mesa_error( ctx
, GL_OUT_OF_MEMORY
, "glCopyPixels" );
146 /* read the source image as RGBA/float */
148 for (row
= 0; row
< height
; row
++) {
149 _swrast_read_rgba_span( ctx
, ctx
->ReadBuffer
->_ColorReadBuffer
,
150 width
, srcx
, sy
+ row
, p
);
156 tmpImage
= NULL
; /* silence compiler warnings */
160 assert(width
< SWRAST_MAX_WIDTH
);
162 for (row
= 0; row
< height
; row
++, sy
+= stepy
, dy
+= stepy
) {
163 GLvoid
*rgba
= span
.array
->attribs
[VARYING_SLOT_COL0
];
165 /* Get row/span of source pixels */
167 /* get from buffered image */
168 memcpy(rgba
, p
, width
* sizeof(GLfloat
) * 4);
172 /* get from framebuffer */
173 _swrast_read_rgba_span( ctx
, ctx
->ReadBuffer
->_ColorReadBuffer
,
174 width
, srcx
, sy
, rgba
);
178 _mesa_apply_rgba_transfer_ops(ctx
, transferOps
, width
,
179 (GLfloat (*)[4]) rgba
);
182 /* Write color span */
186 span
.array
->ChanType
= GL_FLOAT
;
188 _swrast_write_zoomed_rgba_span(ctx
, destx
, desty
, &span
, rgba
);
191 _swrast_write_rgba_span(ctx
, &span
);
195 span
.array
->ChanType
= CHAN_TYPE
; /* restore */
203 * Convert floating point Z values to integer Z values with pixel transfer's
207 scale_and_bias_z(struct gl_context
*ctx
, GLuint width
,
208 const GLfloat depth
[], GLuint z
[])
210 const GLuint depthMax
= ctx
->DrawBuffer
->_DepthMax
;
213 if (depthMax
<= 0xffffff &&
214 ctx
->Pixel
.DepthScale
== 1.0 &&
215 ctx
->Pixel
.DepthBias
== 0.0) {
216 /* no scale or bias and no clamping and no worry of overflow */
217 const GLfloat depthMaxF
= ctx
->DrawBuffer
->_DepthMaxF
;
218 for (i
= 0; i
< width
; i
++) {
219 z
[i
] = (GLuint
) (depth
[i
] * depthMaxF
);
223 /* need to be careful with overflow */
224 const GLdouble depthMaxF
= ctx
->DrawBuffer
->_DepthMaxF
;
225 for (i
= 0; i
< width
; i
++) {
226 GLdouble d
= depth
[i
] * ctx
->Pixel
.DepthScale
+ ctx
->Pixel
.DepthBias
;
227 d
= CLAMP(d
, 0.0, 1.0) * depthMaxF
;
242 copy_depth_pixels( struct gl_context
*ctx
, GLint srcx
, GLint srcy
,
243 GLint width
, GLint height
,
244 GLint destx
, GLint desty
)
246 struct gl_framebuffer
*fb
= ctx
->ReadBuffer
;
247 struct gl_renderbuffer
*readRb
= fb
->Attachment
[BUFFER_DEPTH
].Renderbuffer
;
248 GLfloat
*p
, *tmpImage
, *depth
;
251 const GLboolean zoom
= ctx
->Pixel
.ZoomX
!= 1.0F
|| ctx
->Pixel
.ZoomY
!= 1.0F
;
256 /* no readbuffer - OK */
260 INIT_SPAN(span
, GL_BITMAP
);
261 _swrast_span_default_attribs(ctx
, &span
);
262 span
.arrayMask
= SPAN_Z
;
264 if (ctx
->DrawBuffer
== ctx
->ReadBuffer
) {
265 overlapping
= regions_overlap(srcx
, srcy
, destx
, desty
, width
, height
,
266 ctx
->Pixel
.ZoomX
, ctx
->Pixel
.ZoomY
);
269 overlapping
= GL_FALSE
;
272 /* Determine if copy should be bottom-to-top or top-to-bottom */
273 if (!overlapping
&& srcy
< desty
) {
274 /* top-down max-to-min */
275 sy
= srcy
+ height
- 1;
276 dy
= desty
+ height
- 1;
280 /* bottom-up min-to-max */
288 tmpImage
= malloc(width
* height
* sizeof(GLfloat
));
290 _mesa_error( ctx
, GL_OUT_OF_MEMORY
, "glCopyPixels" );
294 for (j
= 0; j
< height
; j
++, ssy
+= stepy
) {
295 _swrast_read_depth_span_float(ctx
, readRb
, width
, srcx
, ssy
, p
);
301 tmpImage
= NULL
; /* silence compiler warning */
305 depth
= malloc(width
* sizeof(GLfloat
));
307 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyPixels()");
311 for (j
= 0; j
< height
; j
++, sy
+= stepy
, dy
+= stepy
) {
312 /* get depth values */
314 memcpy(depth
, p
, width
* sizeof(GLfloat
));
318 _swrast_read_depth_span_float(ctx
, readRb
, width
, srcx
, sy
, depth
);
321 /* apply scale and bias */
322 scale_and_bias_z(ctx
, width
, depth
, span
.array
->z
);
324 /* write depth values */
329 _swrast_write_zoomed_depth_span(ctx
, destx
, desty
, &span
);
331 _swrast_write_rgba_span(ctx
, &span
);
344 copy_stencil_pixels( struct gl_context
*ctx
, GLint srcx
, GLint srcy
,
345 GLint width
, GLint height
,
346 GLint destx
, GLint desty
)
348 struct gl_framebuffer
*fb
= ctx
->ReadBuffer
;
349 struct gl_renderbuffer
*rb
= fb
->Attachment
[BUFFER_STENCIL
].Renderbuffer
;
352 GLubyte
*p
, *tmpImage
, *stencil
;
353 const GLboolean zoom
= ctx
->Pixel
.ZoomX
!= 1.0F
|| ctx
->Pixel
.ZoomY
!= 1.0F
;
357 /* no readbuffer - OK */
361 if (ctx
->DrawBuffer
== ctx
->ReadBuffer
) {
362 overlapping
= regions_overlap(srcx
, srcy
, destx
, desty
, width
, height
,
363 ctx
->Pixel
.ZoomX
, ctx
->Pixel
.ZoomY
);
366 overlapping
= GL_FALSE
;
369 /* Determine if copy should be bottom-to-top or top-to-bottom */
370 if (!overlapping
&& srcy
< desty
) {
371 /* top-down max-to-min */
372 sy
= srcy
+ height
- 1;
373 dy
= desty
+ height
- 1;
377 /* bottom-up min-to-max */
385 tmpImage
= malloc(width
* height
* sizeof(GLubyte
));
387 _mesa_error( ctx
, GL_OUT_OF_MEMORY
, "glCopyPixels" );
391 for (j
= 0; j
< height
; j
++, ssy
+= stepy
) {
392 _swrast_read_stencil_span( ctx
, rb
, width
, srcx
, ssy
, p
);
398 tmpImage
= NULL
; /* silence compiler warning */
402 stencil
= malloc(width
* sizeof(GLubyte
));
404 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyPixels()");
408 for (j
= 0; j
< height
; j
++, sy
+= stepy
, dy
+= stepy
) {
409 /* Get stencil values */
411 memcpy(stencil
, p
, width
* sizeof(GLubyte
));
415 _swrast_read_stencil_span( ctx
, rb
, width
, srcx
, sy
, stencil
);
418 _mesa_apply_stencil_transfer_ops(ctx
, width
, stencil
);
420 /* Write stencil values */
422 _swrast_write_zoomed_stencil_span(ctx
, destx
, desty
, width
,
426 _swrast_write_stencil_span( ctx
, width
, destx
, dy
, stencil
);
439 * Try to do a fast 1:1 blit with memcpy.
440 * \return GL_TRUE if successful, GL_FALSE otherwise.
443 swrast_fast_copy_pixels(struct gl_context
*ctx
,
444 struct gl_framebuffer
*srcFb
,
445 struct gl_framebuffer
*dstFb
,
446 GLint srcX
, GLint srcY
, GLsizei width
, GLsizei height
,
447 GLint dstX
, GLint dstY
, GLenum type
)
449 struct gl_renderbuffer
*srcRb
, *dstRb
;
451 GLuint pixelBytes
, widthInBytes
;
452 GLubyte
*srcMap
, *dstMap
;
453 GLint srcRowStride
, dstRowStride
;
455 if (type
== GL_COLOR
) {
456 if (dstFb
->_NumColorDrawBuffers
!= 1)
458 srcRb
= srcFb
->_ColorReadBuffer
;
459 dstRb
= dstFb
->_ColorDrawBuffers
[0];
461 else if (type
== GL_STENCIL
) {
462 srcRb
= srcFb
->Attachment
[BUFFER_STENCIL
].Renderbuffer
;
463 dstRb
= dstFb
->Attachment
[BUFFER_STENCIL
].Renderbuffer
;
465 else if (type
== GL_DEPTH
) {
466 srcRb
= srcFb
->Attachment
[BUFFER_DEPTH
].Renderbuffer
;
467 dstRb
= dstFb
->Attachment
[BUFFER_DEPTH
].Renderbuffer
;
470 assert(type
== GL_DEPTH_STENCIL_EXT
);
472 srcRb
= srcFb
->Attachment
[BUFFER_DEPTH
].Renderbuffer
;
473 dstRb
= dstFb
->Attachment
[BUFFER_DEPTH
].Renderbuffer
;
476 /* src and dst renderbuffers must be same format */
477 if (!srcRb
|| !dstRb
|| srcRb
->Format
!= dstRb
->Format
) {
481 if (type
== GL_STENCIL
|| type
== GL_DEPTH_COMPONENT
) {
482 /* can't handle packed depth+stencil here */
483 if (_mesa_is_format_packed_depth_stencil(srcRb
->Format
) ||
484 _mesa_is_format_packed_depth_stencil(dstRb
->Format
))
487 else if (type
== GL_DEPTH_STENCIL
) {
488 /* can't handle separate depth/stencil buffers */
489 if (srcRb
!= srcFb
->Attachment
[BUFFER_STENCIL
].Renderbuffer
||
490 dstRb
!= dstFb
->Attachment
[BUFFER_STENCIL
].Renderbuffer
)
494 /* clipping not supported */
495 if (srcX
< 0 || srcX
+ width
> (GLint
) srcFb
->Width
||
496 srcY
< 0 || srcY
+ height
> (GLint
) srcFb
->Height
||
497 dstX
< dstFb
->_Xmin
|| dstX
+ width
> dstFb
->_Xmax
||
498 dstY
< dstFb
->_Ymin
|| dstY
+ height
> dstFb
->_Ymax
) {
502 pixelBytes
= _mesa_get_format_bytes(srcRb
->Format
);
503 widthInBytes
= width
* pixelBytes
;
505 if (srcRb
== dstRb
) {
506 /* map whole buffer for read/write */
507 /* XXX we could be clever and just map the union region of the
508 * source and dest rects.
513 ctx
->Driver
.MapRenderbuffer(ctx
, srcRb
, 0, 0,
514 srcRb
->Width
, srcRb
->Height
,
515 GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
,
518 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyPixels");
519 return GL_TRUE
; /* don't retry with slow path */
522 srcMap
= map
+ srcY
* rowStride
+ srcX
* pixelBytes
;
523 dstMap
= map
+ dstY
* rowStride
+ dstX
* pixelBytes
;
525 /* this handles overlapping copies */
527 /* copy in reverse (top->down) order */
528 srcMap
+= rowStride
* (height
- 1);
529 dstMap
+= rowStride
* (height
- 1);
530 srcRowStride
= -rowStride
;
531 dstRowStride
= -rowStride
;
534 /* copy in normal (bottom->up) order */
535 srcRowStride
= rowStride
;
536 dstRowStride
= rowStride
;
540 /* different src/dst buffers */
541 ctx
->Driver
.MapRenderbuffer(ctx
, srcRb
, srcX
, srcY
,
543 GL_MAP_READ_BIT
, &srcMap
, &srcRowStride
);
545 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyPixels");
546 return GL_TRUE
; /* don't retry with slow path */
548 ctx
->Driver
.MapRenderbuffer(ctx
, dstRb
, dstX
, dstY
,
550 GL_MAP_WRITE_BIT
, &dstMap
, &dstRowStride
);
552 ctx
->Driver
.UnmapRenderbuffer(ctx
, srcRb
);
553 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyPixels");
554 return GL_TRUE
; /* don't retry with slow path */
558 for (row
= 0; row
< height
; row
++) {
559 /* memmove() in case of overlap */
560 memmove(dstMap
, srcMap
, widthInBytes
);
561 dstMap
+= dstRowStride
;
562 srcMap
+= srcRowStride
;
565 ctx
->Driver
.UnmapRenderbuffer(ctx
, srcRb
);
566 if (dstRb
!= srcRb
) {
567 ctx
->Driver
.UnmapRenderbuffer(ctx
, dstRb
);
575 * Find/map the renderbuffer that we'll be reading from.
576 * The swrast_render_start() function only maps the drawing buffers,
577 * not the read buffer.
579 static struct gl_renderbuffer
*
580 map_readbuffer(struct gl_context
*ctx
, GLenum type
)
582 struct gl_framebuffer
*fb
= ctx
->ReadBuffer
;
583 struct gl_renderbuffer
*rb
;
584 struct swrast_renderbuffer
*srb
;
588 rb
= fb
->Attachment
[fb
->_ColorReadBufferIndex
].Renderbuffer
;
591 case GL_DEPTH_STENCIL
:
592 rb
= fb
->Attachment
[BUFFER_DEPTH
].Renderbuffer
;
595 rb
= fb
->Attachment
[BUFFER_STENCIL
].Renderbuffer
;
601 srb
= swrast_renderbuffer(rb
);
603 if (!srb
|| srb
->Map
) {
604 /* no buffer, or buffer is mapped already, we're done */
608 ctx
->Driver
.MapRenderbuffer(ctx
, rb
,
609 0, 0, rb
->Width
, rb
->Height
,
611 &srb
->Map
, &srb
->RowStride
);
618 * Do software-based glCopyPixels.
619 * By time we get here, all parameters will have been error-checked.
622 _swrast_CopyPixels(struct gl_context
*ctx
,
623 GLint srcx
, GLint srcy
, GLsizei width
, GLsizei height
,
624 GLint destx
, GLint desty
, GLenum type
)
626 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
627 struct gl_renderbuffer
*rb
;
629 if (!_mesa_check_conditional_render(ctx
))
630 return; /* don't copy */
632 if (swrast
->NewState
)
633 _swrast_validate_derived( ctx
);
635 if (!(SWRAST_CONTEXT(ctx
)->_RasterMask
!= 0x0 ||
636 ctx
->Pixel
.ZoomX
!= 1.0F
||
637 ctx
->Pixel
.ZoomY
!= 1.0F
||
638 ctx
->_ImageTransferState
) &&
639 swrast_fast_copy_pixels(ctx
, ctx
->ReadBuffer
, ctx
->DrawBuffer
,
640 srcx
, srcy
, width
, height
, destx
, desty
,
646 swrast_render_start(ctx
);
647 rb
= map_readbuffer(ctx
, type
);
651 copy_rgba_pixels( ctx
, srcx
, srcy
, width
, height
, destx
, desty
);
654 copy_depth_pixels( ctx
, srcx
, srcy
, width
, height
, destx
, desty
);
657 copy_stencil_pixels( ctx
, srcx
, srcy
, width
, height
, destx
, desty
);
659 case GL_DEPTH_STENCIL_EXT
:
660 /* Copy buffers separately (if the fast copy path wasn't taken) */
661 copy_depth_pixels(ctx
, srcx
, srcy
, width
, height
, destx
, desty
);
662 copy_stencil_pixels(ctx
, srcx
, srcy
, width
, height
, destx
, desty
);
665 _mesa_problem(ctx
, "unexpected type in _swrast_CopyPixels");
668 swrast_render_finish(ctx
);
671 struct swrast_renderbuffer
*srb
= swrast_renderbuffer(rb
);
672 ctx
->Driver
.UnmapRenderbuffer(ctx
, rb
);