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.
26 #include "main/glheader.h"
27 #include "main/condrender.h"
28 #include "main/image.h"
29 #include "main/macros.h"
30 #include "main/format_unpack.h"
31 #include "main/format_pack.h"
32 #include "s_context.h"
35 #define ABS(X) ((X) < 0 ? -(X) : (X))
39 * Generate a row resampler function for GL_NEAREST mode.
41 #define RESAMPLE(NAME, PIXELTYPE, SIZE) \
43 NAME(GLint srcWidth, GLint dstWidth, \
44 const GLvoid *srcBuffer, GLvoid *dstBuffer, \
47 const PIXELTYPE *src = (const PIXELTYPE *) srcBuffer;\
48 PIXELTYPE *dst = (PIXELTYPE *) dstBuffer; \
52 for (dstCol = 0; dstCol < dstWidth; dstCol++) { \
53 GLint srcCol = (dstCol * srcWidth) / dstWidth; \
54 ASSERT(srcCol >= 0); \
55 ASSERT(srcCol < srcWidth); \
56 srcCol = srcWidth - 1 - srcCol; /* flip */ \
58 dst[dstCol] = src[srcCol]; \
60 else if (SIZE == 2) { \
61 dst[dstCol*2+0] = src[srcCol*2+0]; \
62 dst[dstCol*2+1] = src[srcCol*2+1]; \
64 else if (SIZE == 4) { \
65 dst[dstCol*4+0] = src[srcCol*4+0]; \
66 dst[dstCol*4+1] = src[srcCol*4+1]; \
67 dst[dstCol*4+2] = src[srcCol*4+2]; \
68 dst[dstCol*4+3] = src[srcCol*4+3]; \
73 for (dstCol = 0; dstCol < dstWidth; dstCol++) { \
74 GLint srcCol = (dstCol * srcWidth) / dstWidth; \
75 ASSERT(srcCol >= 0); \
76 ASSERT(srcCol < srcWidth); \
78 dst[dstCol] = src[srcCol]; \
80 else if (SIZE == 2) { \
81 dst[dstCol*2+0] = src[srcCol*2+0]; \
82 dst[dstCol*2+1] = src[srcCol*2+1]; \
84 else if (SIZE == 4) { \
85 dst[dstCol*4+0] = src[srcCol*4+0]; \
86 dst[dstCol*4+1] = src[srcCol*4+1]; \
87 dst[dstCol*4+2] = src[srcCol*4+2]; \
88 dst[dstCol*4+3] = src[srcCol*4+3]; \
95 * Resamplers for 1, 2, 4, 8 and 16-byte pixels.
97 RESAMPLE(resample_row_1
, GLubyte
, 1)
98 RESAMPLE(resample_row_2
, GLushort
, 1)
99 RESAMPLE(resample_row_4
, GLuint
, 1)
100 RESAMPLE(resample_row_8
, GLuint
, 2)
101 RESAMPLE(resample_row_16
, GLuint
, 4)
105 * Blit color, depth or stencil with GL_NEAREST filtering.
108 blit_nearest(struct gl_context
*ctx
,
109 GLint srcX0
, GLint srcY0
, GLint srcX1
, GLint srcY1
,
110 GLint dstX0
, GLint dstY0
, GLint dstX1
, GLint dstY1
,
113 struct gl_renderbuffer
*readRb
, *drawRb
;
114 struct gl_renderbuffer_attachment
*readAtt
, *drawAtt
;
115 struct gl_framebuffer
*readFb
= ctx
->ReadBuffer
;
116 struct gl_framebuffer
*drawFb
= ctx
->DrawBuffer
;
117 GLuint numDrawBuffers
= 0;
120 const GLint srcWidth
= ABS(srcX1
- srcX0
);
121 const GLint dstWidth
= ABS(dstX1
- dstX0
);
122 const GLint srcHeight
= ABS(srcY1
- srcY0
);
123 const GLint dstHeight
= ABS(dstY1
- dstY0
);
125 const GLint srcXpos
= MIN2(srcX0
, srcX1
);
126 const GLint srcYpos
= MIN2(srcY0
, srcY1
);
127 const GLint dstXpos
= MIN2(dstX0
, dstX1
);
128 const GLint dstYpos
= MIN2(dstY0
, dstY1
);
130 const GLboolean invertX
= (srcX1
< srcX0
) ^ (dstX1
< dstX0
);
131 const GLboolean invertY
= (srcY1
< srcY0
) ^ (dstY1
< dstY0
);
139 GLubyte
*srcMap
, *dstMap
;
140 GLint srcRowStride
, dstRowStride
;
144 GLvoid
*srcBuffer
, *dstBuffer
;
147 typedef void (*resample_func
)(GLint srcWidth
, GLint dstWidth
,
148 const GLvoid
*srcBuffer
, GLvoid
*dstBuffer
,
150 resample_func resampleRow
;
153 case GL_COLOR_BUFFER_BIT
:
154 readAtt
= &readFb
->Attachment
[readFb
->_ColorReadBufferIndex
];
155 readRb
= readFb
->_ColorReadBuffer
;
156 numDrawBuffers
= drawFb
->_NumColorDrawBuffers
;
158 case GL_DEPTH_BUFFER_BIT
:
159 readAtt
= &readFb
->Attachment
[BUFFER_DEPTH
];
160 drawAtt
= &drawFb
->Attachment
[BUFFER_DEPTH
];
161 readRb
= readAtt
->Renderbuffer
;
162 drawRb
= drawAtt
->Renderbuffer
;
165 /* Note that for depth/stencil, the formats of src/dst must match. By
166 * using the core helpers for pack/unpack, we avoid needing to handle
167 * masking for things like DEPTH copies of Z24S8.
169 if (readRb
->Format
== MESA_FORMAT_Z32_FLOAT
||
170 readRb
->Format
== MESA_FORMAT_Z32_FLOAT_X24S8
) {
171 mode
= UNPACK_Z_FLOAT
;
177 case GL_STENCIL_BUFFER_BIT
:
178 readAtt
= &readFb
->Attachment
[BUFFER_STENCIL
];
179 drawAtt
= &drawFb
->Attachment
[BUFFER_STENCIL
];
180 readRb
= readAtt
->Renderbuffer
;
181 drawRb
= drawAtt
->Renderbuffer
;
187 _mesa_problem(ctx
, "unexpected buffer in blit_nearest()");
191 /* Blit to all the draw buffers */
192 for (i
= 0; i
< numDrawBuffers
; i
++) {
193 if (buffer
== GL_COLOR_BUFFER_BIT
) {
194 int idx
= drawFb
->_ColorDrawBufferIndexes
[i
];
197 drawAtt
= &drawFb
->Attachment
[idx
];
198 drawRb
= drawAtt
->Renderbuffer
;
203 if (readRb
->Format
== drawRb
->Format
) {
205 pixelSize
= _mesa_get_format_bytes(readRb
->Format
);
207 mode
= UNPACK_RGBA_FLOAT
;
212 /* choose row resampler */
215 resampleRow
= resample_row_1
;
218 resampleRow
= resample_row_2
;
221 resampleRow
= resample_row_4
;
224 resampleRow
= resample_row_8
;
227 resampleRow
= resample_row_16
;
230 _mesa_problem(ctx
, "unexpected pixel size (%d) in blit_nearest",
235 if ((readRb
== drawRb
) ||
236 (readAtt
->Texture
&& drawAtt
->Texture
&&
237 (readAtt
->Texture
== drawAtt
->Texture
))) {
238 /* map whole buffer for read/write */
239 /* XXX we could be clever and just map the union region of the
240 * source and dest rects.
244 GLint formatSize
= _mesa_get_format_bytes(readRb
->Format
);
246 ctx
->Driver
.MapRenderbuffer(ctx
, readRb
, 0, 0,
247 readRb
->Width
, readRb
->Height
,
248 GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
,
251 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBlitFramebuffer");
255 srcMap
= map
+ srcYpos
* rowStride
+ srcXpos
* formatSize
;
256 dstMap
= map
+ dstYpos
* rowStride
+ dstXpos
* formatSize
;
258 /* this handles overlapping copies */
260 /* copy in reverse (top->down) order */
261 srcMap
+= rowStride
* (readRb
->Height
- 1);
262 dstMap
+= rowStride
* (readRb
->Height
- 1);
263 srcRowStride
= -rowStride
;
264 dstRowStride
= -rowStride
;
267 /* copy in normal (bottom->up) order */
268 srcRowStride
= rowStride
;
269 dstRowStride
= rowStride
;
273 /* different src/dst buffers */
274 ctx
->Driver
.MapRenderbuffer(ctx
, readRb
,
277 GL_MAP_READ_BIT
, &srcMap
, &srcRowStride
);
279 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBlitFramebuffer");
282 ctx
->Driver
.MapRenderbuffer(ctx
, drawRb
,
285 GL_MAP_WRITE_BIT
, &dstMap
, &dstRowStride
);
287 ctx
->Driver
.UnmapRenderbuffer(ctx
, readRb
);
288 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBlitFramebuffer");
293 /* allocate the src/dst row buffers */
294 srcBuffer
= malloc(pixelSize
* srcWidth
);
296 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBlitFrameBufferEXT");
299 dstBuffer
= malloc(pixelSize
* dstWidth
);
302 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBlitFrameBufferEXT");
306 for (dstRow
= 0; dstRow
< dstHeight
; dstRow
++) {
307 GLfloat srcRowF
= (dstRow
+ 0.5F
) / dstHeight
* srcHeight
- 0.5F
;
308 GLint srcRow
= IROUND(srcRowF
);
309 GLubyte
*dstRowStart
= dstMap
+ dstRowStride
* dstRow
;
312 ASSERT(srcRow
< srcHeight
);
315 srcRow
= srcHeight
- 1 - srcRow
;
318 /* get pixel row from source and resample to match dest width */
319 if (prevY
!= srcRow
) {
320 GLubyte
*srcRowStart
= srcMap
+ srcRowStride
* srcRow
;
324 memcpy(srcBuffer
, srcRowStart
, pixelSize
* srcWidth
);
326 case UNPACK_RGBA_FLOAT
:
327 _mesa_unpack_rgba_row(readRb
->Format
, srcWidth
, srcRowStart
,
331 _mesa_unpack_float_z_row(readRb
->Format
, srcWidth
, srcRowStart
,
335 _mesa_unpack_uint_z_row(readRb
->Format
, srcWidth
, srcRowStart
,
339 _mesa_unpack_ubyte_stencil_row(readRb
->Format
, srcWidth
,
340 srcRowStart
, srcBuffer
);
344 (*resampleRow
)(srcWidth
, dstWidth
, srcBuffer
, dstBuffer
, invertX
);
348 /* store pixel row in destination */
351 memcpy(dstRowStart
, dstBuffer
, pixelSize
* dstWidth
);
353 case UNPACK_RGBA_FLOAT
:
354 _mesa_pack_float_rgba_row(drawRb
->Format
, dstWidth
, dstBuffer
,
358 _mesa_pack_float_z_row(drawRb
->Format
, dstWidth
, dstBuffer
,
362 _mesa_pack_uint_z_row(drawRb
->Format
, dstWidth
, dstBuffer
,
366 _mesa_pack_ubyte_stencil_row(drawRb
->Format
, dstWidth
, dstBuffer
,
375 ctx
->Driver
.UnmapRenderbuffer(ctx
, readRb
);
376 if (drawRb
!= readRb
) {
377 ctx
->Driver
.UnmapRenderbuffer(ctx
, drawRb
);
384 #define LERP(T, A, B) ( (A) + (T) * ((B) - (A)) )
386 static inline GLfloat
387 lerp_2d(GLfloat a
, GLfloat b
,
388 GLfloat v00
, GLfloat v10
, GLfloat v01
, GLfloat v11
)
390 const GLfloat temp0
= LERP(a
, v00
, v10
);
391 const GLfloat temp1
= LERP(a
, v01
, v11
);
392 return LERP(b
, temp0
, temp1
);
397 * Bilinear interpolation of two source rows.
401 resample_linear_row_ub(GLint srcWidth
, GLint dstWidth
,
402 const GLvoid
*srcBuffer0
, const GLvoid
*srcBuffer1
,
403 GLvoid
*dstBuffer
, GLboolean flip
, GLfloat rowWeight
)
405 const GLubyte (*srcColor0
)[4] = (const GLubyte (*)[4]) srcBuffer0
;
406 const GLubyte (*srcColor1
)[4] = (const GLubyte (*)[4]) srcBuffer1
;
407 GLubyte (*dstColor
)[4] = (GLubyte (*)[4]) dstBuffer
;
410 for (dstCol
= 0; dstCol
< dstWidth
; dstCol
++) {
411 const GLfloat srcCol
= (dstCol
+ 0.5F
) / dstWidth
* srcWidth
- 0.5F
;
412 GLint srcCol0
= MAX2(0, IFLOOR(srcCol
));
413 GLint srcCol1
= srcCol0
+ 1;
414 GLfloat colWeight
= srcCol
- srcCol0
; /* fractional part of srcCol */
415 GLfloat red
, green
, blue
, alpha
;
417 ASSERT(srcCol0
< srcWidth
);
418 ASSERT(srcCol1
<= srcWidth
);
420 if (srcCol1
== srcWidth
) {
421 /* last column fudge */
427 srcCol0
= srcWidth
- 1 - srcCol0
;
428 srcCol1
= srcWidth
- 1 - srcCol1
;
431 red
= lerp_2d(colWeight
, rowWeight
,
432 srcColor0
[srcCol0
][RCOMP
], srcColor0
[srcCol1
][RCOMP
],
433 srcColor1
[srcCol0
][RCOMP
], srcColor1
[srcCol1
][RCOMP
]);
434 green
= lerp_2d(colWeight
, rowWeight
,
435 srcColor0
[srcCol0
][GCOMP
], srcColor0
[srcCol1
][GCOMP
],
436 srcColor1
[srcCol0
][GCOMP
], srcColor1
[srcCol1
][GCOMP
]);
437 blue
= lerp_2d(colWeight
, rowWeight
,
438 srcColor0
[srcCol0
][BCOMP
], srcColor0
[srcCol1
][BCOMP
],
439 srcColor1
[srcCol0
][BCOMP
], srcColor1
[srcCol1
][BCOMP
]);
440 alpha
= lerp_2d(colWeight
, rowWeight
,
441 srcColor0
[srcCol0
][ACOMP
], srcColor0
[srcCol1
][ACOMP
],
442 srcColor1
[srcCol0
][ACOMP
], srcColor1
[srcCol1
][ACOMP
]);
444 dstColor
[dstCol
][RCOMP
] = IFLOOR(red
);
445 dstColor
[dstCol
][GCOMP
] = IFLOOR(green
);
446 dstColor
[dstCol
][BCOMP
] = IFLOOR(blue
);
447 dstColor
[dstCol
][ACOMP
] = IFLOOR(alpha
);
453 * Bilinear interpolation of two source rows. floating point pixels.
456 resample_linear_row_float(GLint srcWidth
, GLint dstWidth
,
457 const GLvoid
*srcBuffer0
, const GLvoid
*srcBuffer1
,
458 GLvoid
*dstBuffer
, GLboolean flip
, GLfloat rowWeight
)
460 const GLfloat (*srcColor0
)[4] = (const GLfloat (*)[4]) srcBuffer0
;
461 const GLfloat (*srcColor1
)[4] = (const GLfloat (*)[4]) srcBuffer1
;
462 GLfloat (*dstColor
)[4] = (GLfloat (*)[4]) dstBuffer
;
465 for (dstCol
= 0; dstCol
< dstWidth
; dstCol
++) {
466 const GLfloat srcCol
= (dstCol
+ 0.5F
) / dstWidth
* srcWidth
- 0.5F
;
467 GLint srcCol0
= MAX2(0, IFLOOR(srcCol
));
468 GLint srcCol1
= srcCol0
+ 1;
469 GLfloat colWeight
= srcCol
- srcCol0
; /* fractional part of srcCol */
470 GLfloat red
, green
, blue
, alpha
;
472 ASSERT(srcCol0
< srcWidth
);
473 ASSERT(srcCol1
<= srcWidth
);
475 if (srcCol1
== srcWidth
) {
476 /* last column fudge */
482 srcCol0
= srcWidth
- 1 - srcCol0
;
483 srcCol1
= srcWidth
- 1 - srcCol1
;
486 red
= lerp_2d(colWeight
, rowWeight
,
487 srcColor0
[srcCol0
][RCOMP
], srcColor0
[srcCol1
][RCOMP
],
488 srcColor1
[srcCol0
][RCOMP
], srcColor1
[srcCol1
][RCOMP
]);
489 green
= lerp_2d(colWeight
, rowWeight
,
490 srcColor0
[srcCol0
][GCOMP
], srcColor0
[srcCol1
][GCOMP
],
491 srcColor1
[srcCol0
][GCOMP
], srcColor1
[srcCol1
][GCOMP
]);
492 blue
= lerp_2d(colWeight
, rowWeight
,
493 srcColor0
[srcCol0
][BCOMP
], srcColor0
[srcCol1
][BCOMP
],
494 srcColor1
[srcCol0
][BCOMP
], srcColor1
[srcCol1
][BCOMP
]);
495 alpha
= lerp_2d(colWeight
, rowWeight
,
496 srcColor0
[srcCol0
][ACOMP
], srcColor0
[srcCol1
][ACOMP
],
497 srcColor1
[srcCol0
][ACOMP
], srcColor1
[srcCol1
][ACOMP
]);
499 dstColor
[dstCol
][RCOMP
] = red
;
500 dstColor
[dstCol
][GCOMP
] = green
;
501 dstColor
[dstCol
][BCOMP
] = blue
;
502 dstColor
[dstCol
][ACOMP
] = alpha
;
509 * Bilinear filtered blit (color only, non-integer values).
512 blit_linear(struct gl_context
*ctx
,
513 GLint srcX0
, GLint srcY0
, GLint srcX1
, GLint srcY1
,
514 GLint dstX0
, GLint dstY0
, GLint dstX1
, GLint dstY1
)
516 struct gl_framebuffer
*drawFb
= ctx
->DrawBuffer
;
517 struct gl_framebuffer
*readFb
= ctx
->ReadBuffer
;
518 struct gl_renderbuffer
*readRb
= readFb
->_ColorReadBuffer
;
519 struct gl_renderbuffer_attachment
*readAtt
=
520 &readFb
->Attachment
[readFb
->_ColorReadBufferIndex
];
522 const GLint srcWidth
= ABS(srcX1
- srcX0
);
523 const GLint dstWidth
= ABS(dstX1
- dstX0
);
524 const GLint srcHeight
= ABS(srcY1
- srcY0
);
525 const GLint dstHeight
= ABS(dstY1
- dstY0
);
527 const GLint srcXpos
= MIN2(srcX0
, srcX1
);
528 const GLint srcYpos
= MIN2(srcY0
, srcY1
);
529 const GLint dstXpos
= MIN2(dstX0
, dstX1
);
530 const GLint dstYpos
= MIN2(dstY0
, dstY1
);
532 const GLboolean invertX
= (srcX1
< srcX0
) ^ (dstX1
< dstX0
);
533 const GLboolean invertY
= (srcY1
< srcY0
) ^ (dstY1
< dstY0
);
538 GLvoid
*srcBuffer0
, *srcBuffer1
;
539 GLint srcBufferY0
= -1, srcBufferY1
= -1;
542 gl_format readFormat
= _mesa_get_srgb_format_linear(readRb
->Format
);
543 GLuint bpp
= _mesa_get_format_bytes(readFormat
);
547 GLubyte
*srcMap
, *dstMap
;
548 GLint srcRowStride
, dstRowStride
;
552 /* Determine datatype for resampling */
553 if (_mesa_get_format_max_bits(readFormat
) == 8 &&
554 _mesa_get_format_datatype(readFormat
) == GL_UNSIGNED_NORMALIZED
) {
555 pixelType
= GL_UNSIGNED_BYTE
;
556 pixelSize
= 4 * sizeof(GLubyte
);
559 pixelType
= GL_FLOAT
;
560 pixelSize
= 4 * sizeof(GLfloat
);
563 /* Allocate the src/dst row buffers.
564 * Keep two adjacent src rows around for bilinear sampling.
566 srcBuffer0
= malloc(pixelSize
* srcWidth
);
568 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBlitFrameBufferEXT");
571 srcBuffer1
= malloc(pixelSize
* srcWidth
);
574 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBlitFrameBufferEXT");
577 dstBuffer
= malloc(pixelSize
* dstWidth
);
581 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBlitFrameBufferEXT");
585 for (i
= 0; i
< drawFb
->_NumColorDrawBuffers
; i
++) {
586 GLint idx
= drawFb
->_ColorDrawBufferIndexes
[i
];
587 struct gl_renderbuffer_attachment
*drawAtt
;
588 struct gl_renderbuffer
*drawRb
;
589 gl_format drawFormat
;
594 drawAtt
= &drawFb
->Attachment
[idx
];
595 drawRb
= drawAtt
->Renderbuffer
;
599 drawFormat
= _mesa_get_srgb_format_linear(drawRb
->Format
);
602 * Map src / dst renderbuffers
604 if ((readRb
== drawRb
) ||
605 (readAtt
->Texture
&& drawAtt
->Texture
&&
606 (readAtt
->Texture
== drawAtt
->Texture
))) {
607 /* map whole buffer for read/write */
608 ctx
->Driver
.MapRenderbuffer(ctx
, readRb
,
609 0, 0, readRb
->Width
, readRb
->Height
,
610 GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
,
611 &srcMap
, &srcRowStride
);
616 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBlitFramebuffer");
621 dstRowStride
= srcRowStride
;
624 /* different src/dst buffers */
625 /* XXX with a bit of work we could just map the regions to be
626 * read/written instead of the whole buffers.
628 ctx
->Driver
.MapRenderbuffer(ctx
, readRb
,
629 0, 0, readRb
->Width
, readRb
->Height
,
630 GL_MAP_READ_BIT
, &srcMap
, &srcRowStride
);
635 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBlitFramebuffer");
638 ctx
->Driver
.MapRenderbuffer(ctx
, drawRb
,
639 0, 0, drawRb
->Width
, drawRb
->Height
,
640 GL_MAP_WRITE_BIT
, &dstMap
, &dstRowStride
);
642 ctx
->Driver
.UnmapRenderbuffer(ctx
, readRb
);
646 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBlitFramebuffer");
651 for (dstRow
= 0; dstRow
< dstHeight
; dstRow
++) {
652 const GLint dstY
= dstYpos
+ dstRow
;
653 GLfloat srcRow
= (dstRow
+ 0.5F
) / dstHeight
* srcHeight
- 0.5F
;
654 GLint srcRow0
= MAX2(0, IFLOOR(srcRow
));
655 GLint srcRow1
= srcRow0
+ 1;
656 GLfloat rowWeight
= srcRow
- srcRow0
; /* fractional part of srcRow */
658 if (srcRow1
== srcHeight
) {
665 srcRow0
= srcHeight
- 1 - srcRow0
;
666 srcRow1
= srcHeight
- 1 - srcRow1
;
669 srcY0
= srcYpos
+ srcRow0
;
670 srcY1
= srcYpos
+ srcRow1
;
672 /* get the two source rows */
673 if (srcY0
== srcBufferY0
&& srcY1
== srcBufferY1
) {
674 /* use same source row buffers again */
676 else if (srcY0
== srcBufferY1
) {
677 /* move buffer1 into buffer0 by swapping pointers */
678 GLvoid
*tmp
= srcBuffer0
;
679 srcBuffer0
= srcBuffer1
;
683 GLubyte
*src
= srcMap
+ srcY1
* srcRowStride
+ srcXpos
* bpp
;
684 if (pixelType
== GL_UNSIGNED_BYTE
) {
685 _mesa_unpack_ubyte_rgba_row(readFormat
, srcWidth
,
689 _mesa_unpack_rgba_row(readFormat
, srcWidth
,
697 /* get both new rows */
699 GLubyte
*src0
= srcMap
+ srcY0
* srcRowStride
+ srcXpos
* bpp
;
700 GLubyte
*src1
= srcMap
+ srcY1
* srcRowStride
+ srcXpos
* bpp
;
701 if (pixelType
== GL_UNSIGNED_BYTE
) {
702 _mesa_unpack_ubyte_rgba_row(readFormat
, srcWidth
,
704 _mesa_unpack_ubyte_rgba_row(readFormat
, srcWidth
,
708 _mesa_unpack_rgba_row(readFormat
, srcWidth
, src0
, srcBuffer0
);
709 _mesa_unpack_rgba_row(readFormat
, srcWidth
, src1
, srcBuffer1
);
716 if (pixelType
== GL_UNSIGNED_BYTE
) {
717 resample_linear_row_ub(srcWidth
, dstWidth
, srcBuffer0
, srcBuffer1
,
718 dstBuffer
, invertX
, rowWeight
);
721 resample_linear_row_float(srcWidth
, dstWidth
, srcBuffer0
, srcBuffer1
,
722 dstBuffer
, invertX
, rowWeight
);
725 /* store pixel row in destination */
727 GLubyte
*dst
= dstMap
+ dstY
* dstRowStride
+ dstXpos
* bpp
;
728 if (pixelType
== GL_UNSIGNED_BYTE
) {
729 _mesa_pack_ubyte_rgba_row(drawFormat
, dstWidth
, dstBuffer
, dst
);
732 _mesa_pack_float_rgba_row(drawFormat
, dstWidth
, dstBuffer
, dst
);
741 ctx
->Driver
.UnmapRenderbuffer(ctx
, readRb
);
742 if (drawRb
!= readRb
) {
743 ctx
->Driver
.UnmapRenderbuffer(ctx
, drawRb
);
751 * Software fallback for glBlitFramebufferEXT().
754 _swrast_BlitFramebuffer(struct gl_context
*ctx
,
755 GLint srcX0
, GLint srcY0
, GLint srcX1
, GLint srcY1
,
756 GLint dstX0
, GLint dstY0
, GLint dstX1
, GLint dstY1
,
757 GLbitfield mask
, GLenum filter
)
759 static const GLbitfield buffers
[3] = {
762 GL_STENCIL_BUFFER_BIT
764 static const GLenum buffer_enums
[3] = {
771 if (!_mesa_clip_blit(ctx
, &srcX0
, &srcY0
, &srcX1
, &srcY1
,
772 &dstX0
, &dstY0
, &dstX1
, &dstY1
)) {
776 if (SWRAST_CONTEXT(ctx
)->NewState
)
777 _swrast_validate_derived(ctx
);
779 /* First, try covering whatever buffers possible using the fast 1:1 copy
782 if (srcX1
- srcX0
== dstX1
- dstX0
&&
783 srcY1
- srcY0
== dstY1
- dstY0
&&
788 for (i
= 0; i
< 3; i
++) {
789 if (mask
& buffers
[i
]) {
790 if (swrast_fast_copy_pixels(ctx
,
792 srcX1
- srcX0
, srcY1
- srcY0
,
804 if (filter
== GL_NEAREST
) {
805 for (i
= 0; i
< 3; i
++) {
806 if (mask
& buffers
[i
]) {
807 blit_nearest(ctx
, srcX0
, srcY0
, srcX1
, srcY1
,
808 dstX0
, dstY0
, dstX1
, dstY1
, buffers
[i
]);
813 ASSERT(filter
== GL_LINEAR
);
814 if (mask
& GL_COLOR_BUFFER_BIT
) { /* depth/stencil not allowed */
815 blit_linear(ctx
, srcX0
, srcY0
, srcX1
, srcY1
,
816 dstX0
, dstY0
, dstX1
, dstY1
);