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
;
115 const GLint srcWidth
= ABS(srcX1
- srcX0
);
116 const GLint dstWidth
= ABS(dstX1
- dstX0
);
117 const GLint srcHeight
= ABS(srcY1
- srcY0
);
118 const GLint dstHeight
= ABS(dstY1
- dstY0
);
120 const GLint srcXpos
= MIN2(srcX0
, srcX1
);
121 const GLint srcYpos
= MIN2(srcY0
, srcY1
);
122 const GLint dstXpos
= MIN2(dstX0
, dstX1
);
123 const GLint dstYpos
= MIN2(dstY0
, dstY1
);
125 const GLboolean invertX
= (srcX1
< srcX0
) ^ (dstX1
< dstX0
);
126 const GLboolean invertY
= (srcY1
< srcY0
) ^ (dstY1
< dstY0
);
134 GLubyte
*srcMap
, *dstMap
;
135 GLint srcRowStride
, dstRowStride
;
139 GLvoid
*srcBuffer
, *dstBuffer
;
142 typedef void (*resample_func
)(GLint srcWidth
, GLint dstWidth
,
143 const GLvoid
*srcBuffer
, GLvoid
*dstBuffer
,
145 resample_func resampleRow
;
148 case GL_COLOR_BUFFER_BIT
:
149 readRb
= ctx
->ReadBuffer
->_ColorReadBuffer
;
150 drawRb
= ctx
->DrawBuffer
->_ColorDrawBuffers
[0];
152 if (readRb
->Format
== drawRb
->Format
) {
154 pixelSize
= _mesa_get_format_bytes(readRb
->Format
);
156 mode
= UNPACK_RGBA_FLOAT
;
161 case GL_DEPTH_BUFFER_BIT
:
162 readRb
= ctx
->ReadBuffer
->Attachment
[BUFFER_DEPTH
].Renderbuffer
;
163 drawRb
= ctx
->DrawBuffer
->Attachment
[BUFFER_DEPTH
].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 readRb
= ctx
->ReadBuffer
->Attachment
[BUFFER_STENCIL
].Renderbuffer
;
179 drawRb
= ctx
->DrawBuffer
->Attachment
[BUFFER_STENCIL
].Renderbuffer
;
184 _mesa_problem(ctx
, "unexpected buffer in blit_nearest()");
188 /* choose row resampler */
191 resampleRow
= resample_row_1
;
194 resampleRow
= resample_row_2
;
197 resampleRow
= resample_row_4
;
200 resampleRow
= resample_row_8
;
203 resampleRow
= resample_row_16
;
206 _mesa_problem(ctx
, "unexpected pixel size (%d) in blit_nearest",
211 if (readRb
== drawRb
) {
212 /* map whole buffer for read/write */
213 /* XXX we could be clever and just map the union region of the
214 * source and dest rects.
218 GLint formatSize
= _mesa_get_format_bytes(readRb
->Format
);
220 ctx
->Driver
.MapRenderbuffer(ctx
, readRb
, 0, 0,
221 readRb
->Width
, readRb
->Height
,
222 GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
,
225 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBlitFramebuffer");
229 srcMap
= map
+ srcYpos
* rowStride
+ srcXpos
* formatSize
;
230 dstMap
= map
+ dstYpos
* rowStride
+ dstXpos
* formatSize
;
232 /* this handles overlapping copies */
234 /* copy in reverse (top->down) order */
235 srcMap
+= rowStride
* (readRb
->Height
- 1);
236 dstMap
+= rowStride
* (readRb
->Height
- 1);
237 srcRowStride
= -rowStride
;
238 dstRowStride
= -rowStride
;
241 /* copy in normal (bottom->up) order */
242 srcRowStride
= rowStride
;
243 dstRowStride
= rowStride
;
247 /* different src/dst buffers */
248 ctx
->Driver
.MapRenderbuffer(ctx
, readRb
,
251 GL_MAP_READ_BIT
, &srcMap
, &srcRowStride
);
253 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBlitFramebuffer");
256 ctx
->Driver
.MapRenderbuffer(ctx
, drawRb
,
259 GL_MAP_WRITE_BIT
, &dstMap
, &dstRowStride
);
261 ctx
->Driver
.UnmapRenderbuffer(ctx
, readRb
);
262 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBlitFramebuffer");
267 /* allocate the src/dst row buffers */
268 srcBuffer
= malloc(pixelSize
* srcWidth
);
270 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBlitFrameBufferEXT");
273 dstBuffer
= malloc(pixelSize
* dstWidth
);
276 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBlitFrameBufferEXT");
280 for (dstRow
= 0; dstRow
< dstHeight
; dstRow
++) {
281 GLint srcRow
= (dstRow
* srcHeight
) / dstHeight
;
282 GLubyte
*dstRowStart
= dstMap
+ dstRowStride
* dstRow
;
285 ASSERT(srcRow
< srcHeight
);
288 srcRow
= srcHeight
- 1 - srcRow
;
291 /* get pixel row from source and resample to match dest width */
292 if (prevY
!= srcRow
) {
293 GLubyte
*srcRowStart
= srcMap
+ srcRowStride
* srcRow
;
297 memcpy(srcBuffer
, srcRowStart
, pixelSize
* srcWidth
);
299 case UNPACK_RGBA_FLOAT
:
300 _mesa_unpack_rgba_row(readRb
->Format
, srcWidth
, srcRowStart
,
304 _mesa_unpack_float_z_row(readRb
->Format
, srcWidth
, srcRowStart
,
308 _mesa_unpack_uint_z_row(readRb
->Format
, srcWidth
, srcRowStart
,
312 _mesa_unpack_ubyte_stencil_row(readRb
->Format
, srcWidth
,
313 srcRowStart
, srcBuffer
);
317 (*resampleRow
)(srcWidth
, dstWidth
, srcBuffer
, dstBuffer
, invertX
);
321 /* store pixel row in destination */
324 memcpy(dstRowStart
, dstBuffer
, pixelSize
* srcWidth
);
326 case UNPACK_RGBA_FLOAT
:
327 _mesa_pack_float_rgba_row(drawRb
->Format
, dstWidth
, dstBuffer
,
331 _mesa_pack_float_z_row(drawRb
->Format
, dstWidth
, dstBuffer
,
335 _mesa_pack_uint_z_row(drawRb
->Format
, dstWidth
, dstBuffer
,
339 _mesa_pack_ubyte_stencil_row(drawRb
->Format
, dstWidth
, dstBuffer
,
348 ctx
->Driver
.UnmapRenderbuffer(ctx
, readRb
);
349 if (drawRb
!= readRb
) {
350 ctx
->Driver
.UnmapRenderbuffer(ctx
, drawRb
);
356 #define LERP(T, A, B) ( (A) + (T) * ((B) - (A)) )
358 static inline GLfloat
359 lerp_2d(GLfloat a
, GLfloat b
,
360 GLfloat v00
, GLfloat v10
, GLfloat v01
, GLfloat v11
)
362 const GLfloat temp0
= LERP(a
, v00
, v10
);
363 const GLfloat temp1
= LERP(a
, v01
, v11
);
364 return LERP(b
, temp0
, temp1
);
369 * Bilinear interpolation of two source rows.
373 resample_linear_row_ub(GLint srcWidth
, GLint dstWidth
,
374 const GLvoid
*srcBuffer0
, const GLvoid
*srcBuffer1
,
375 GLvoid
*dstBuffer
, GLboolean flip
, GLfloat rowWeight
)
377 const GLubyte (*srcColor0
)[4] = (const GLubyte (*)[4]) srcBuffer0
;
378 const GLubyte (*srcColor1
)[4] = (const GLubyte (*)[4]) srcBuffer1
;
379 GLubyte (*dstColor
)[4] = (GLubyte (*)[4]) dstBuffer
;
380 const GLfloat dstWidthF
= (GLfloat
) dstWidth
;
383 for (dstCol
= 0; dstCol
< dstWidth
; dstCol
++) {
384 const GLfloat srcCol
= (dstCol
* srcWidth
) / dstWidthF
;
385 GLint srcCol0
= IFLOOR(srcCol
);
386 GLint srcCol1
= srcCol0
+ 1;
387 GLfloat colWeight
= srcCol
- srcCol0
; /* fractional part of srcCol */
388 GLfloat red
, green
, blue
, alpha
;
390 ASSERT(srcCol0
>= 0);
391 ASSERT(srcCol0
< srcWidth
);
392 ASSERT(srcCol1
<= srcWidth
);
394 if (srcCol1
== srcWidth
) {
395 /* last column fudge */
401 srcCol0
= srcWidth
- 1 - srcCol0
;
402 srcCol1
= srcWidth
- 1 - srcCol1
;
405 red
= lerp_2d(colWeight
, rowWeight
,
406 srcColor0
[srcCol0
][RCOMP
], srcColor0
[srcCol1
][RCOMP
],
407 srcColor1
[srcCol0
][RCOMP
], srcColor1
[srcCol1
][RCOMP
]);
408 green
= lerp_2d(colWeight
, rowWeight
,
409 srcColor0
[srcCol0
][GCOMP
], srcColor0
[srcCol1
][GCOMP
],
410 srcColor1
[srcCol0
][GCOMP
], srcColor1
[srcCol1
][GCOMP
]);
411 blue
= lerp_2d(colWeight
, rowWeight
,
412 srcColor0
[srcCol0
][BCOMP
], srcColor0
[srcCol1
][BCOMP
],
413 srcColor1
[srcCol0
][BCOMP
], srcColor1
[srcCol1
][BCOMP
]);
414 alpha
= lerp_2d(colWeight
, rowWeight
,
415 srcColor0
[srcCol0
][ACOMP
], srcColor0
[srcCol1
][ACOMP
],
416 srcColor1
[srcCol0
][ACOMP
], srcColor1
[srcCol1
][ACOMP
]);
418 dstColor
[dstCol
][RCOMP
] = IFLOOR(red
);
419 dstColor
[dstCol
][GCOMP
] = IFLOOR(green
);
420 dstColor
[dstCol
][BCOMP
] = IFLOOR(blue
);
421 dstColor
[dstCol
][ACOMP
] = IFLOOR(alpha
);
428 * Bilinear filtered blit (color only).
431 blit_linear(struct gl_context
*ctx
,
432 GLint srcX0
, GLint srcY0
, GLint srcX1
, GLint srcY1
,
433 GLint dstX0
, GLint dstY0
, GLint dstX1
, GLint dstY1
)
435 struct gl_renderbuffer
*readRb
= ctx
->ReadBuffer
->_ColorReadBuffer
;
436 struct gl_renderbuffer
*drawRb
= ctx
->DrawBuffer
->_ColorDrawBuffers
[0];
438 const GLint srcWidth
= ABS(srcX1
- srcX0
);
439 const GLint dstWidth
= ABS(dstX1
- dstX0
);
440 const GLint srcHeight
= ABS(srcY1
- srcY0
);
441 const GLint dstHeight
= ABS(dstY1
- dstY0
);
442 const GLfloat dstHeightF
= (GLfloat
) dstHeight
;
444 const GLint srcXpos
= MIN2(srcX0
, srcX1
);
445 const GLint srcYpos
= MIN2(srcY0
, srcY1
);
446 const GLint dstXpos
= MIN2(dstX0
, dstX1
);
447 const GLint dstYpos
= MIN2(dstY0
, dstY1
);
449 const GLboolean invertX
= (srcX1
< srcX0
) ^ (dstX1
< dstX0
);
450 const GLboolean invertY
= (srcY1
< srcY0
) ^ (dstY1
< dstY0
);
455 GLvoid
*srcBuffer0
, *srcBuffer1
;
456 GLint srcBufferY0
= -1, srcBufferY1
= -1;
459 switch (readRb
->DataType
) {
460 case GL_UNSIGNED_BYTE
:
461 pixelSize
= 4 * sizeof(GLubyte
);
463 case GL_UNSIGNED_SHORT
:
464 pixelSize
= 4 * sizeof(GLushort
);
466 case GL_UNSIGNED_INT
:
467 pixelSize
= 4 * sizeof(GLuint
);
470 pixelSize
= 4 * sizeof(GLfloat
);
473 _mesa_problem(ctx
, "unexpected buffer type (0x%x) in blit_nearest",
478 /* Allocate the src/dst row buffers.
479 * Keep two adjacent src rows around for bilinear sampling.
481 srcBuffer0
= malloc(pixelSize
* srcWidth
);
483 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBlitFrameBufferEXT");
486 srcBuffer1
= malloc(pixelSize
* srcWidth
);
489 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBlitFrameBufferEXT");
492 dstBuffer
= malloc(pixelSize
* dstWidth
);
496 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBlitFrameBufferEXT");
500 for (dstRow
= 0; dstRow
< dstHeight
; dstRow
++) {
501 const GLint dstY
= dstYpos
+ dstRow
;
502 const GLfloat srcRow
= (dstRow
* srcHeight
) / dstHeightF
;
503 GLint srcRow0
= IFLOOR(srcRow
);
504 GLint srcRow1
= srcRow0
+ 1;
505 GLfloat rowWeight
= srcRow
- srcRow0
; /* fractional part of srcRow */
508 ASSERT(srcRow
< srcHeight
);
510 if (srcRow1
== srcHeight
) {
517 srcRow0
= srcHeight
- 1 - srcRow0
;
518 srcRow1
= srcHeight
- 1 - srcRow1
;
521 srcY0
= srcYpos
+ srcRow0
;
522 srcY1
= srcYpos
+ srcRow1
;
524 /* get the two source rows */
525 if (srcY0
== srcBufferY0
&& srcY1
== srcBufferY1
) {
526 /* use same source row buffers again */
528 else if (srcY0
== srcBufferY1
) {
529 /* move buffer1 into buffer0 by swapping pointers */
530 GLvoid
*tmp
= srcBuffer0
;
531 srcBuffer0
= srcBuffer1
;
534 readRb
->GetRow(ctx
, readRb
, srcWidth
, srcXpos
, srcY1
, srcBuffer1
);
539 /* get both new rows */
540 readRb
->GetRow(ctx
, readRb
, srcWidth
, srcXpos
, srcY0
, srcBuffer0
);
541 readRb
->GetRow(ctx
, readRb
, srcWidth
, srcXpos
, srcY1
, srcBuffer1
);
546 if (readRb
->DataType
== GL_UNSIGNED_BYTE
) {
547 resample_linear_row_ub(srcWidth
, dstWidth
, srcBuffer0
, srcBuffer1
,
548 dstBuffer
, invertX
, rowWeight
);
551 _mesa_problem(ctx
, "Unsupported color channel type in sw blit");
555 /* store pixel row in destination */
556 drawRb
->PutRow(ctx
, drawRb
, dstWidth
, dstXpos
, dstY
, dstBuffer
, NULL
);
565 * Software fallback for glBlitFramebufferEXT().
568 _swrast_BlitFramebuffer(struct gl_context
*ctx
,
569 GLint srcX0
, GLint srcY0
, GLint srcX1
, GLint srcY1
,
570 GLint dstX0
, GLint dstY0
, GLint dstX1
, GLint dstY1
,
571 GLbitfield mask
, GLenum filter
)
573 static const GLbitfield buffers
[3] = {
576 GL_STENCIL_BUFFER_BIT
578 static const GLenum buffer_enums
[3] = {
585 if (!_mesa_clip_blit(ctx
, &srcX0
, &srcY0
, &srcX1
, &srcY1
,
586 &dstX0
, &dstY0
, &dstX1
, &dstY1
)) {
590 if (SWRAST_CONTEXT(ctx
)->NewState
)
591 _swrast_validate_derived(ctx
);
593 /* First, try covering whatever buffers possible using the fast 1:1 copy
596 if (srcX1
- srcX0
== dstX1
- dstX0
&&
597 srcY1
- srcY0
== dstY1
- dstY0
&&
602 for (i
= 0; i
< 3; i
++) {
603 if (mask
& buffers
[i
]) {
604 if (swrast_fast_copy_pixels(ctx
,
606 srcX1
- srcX0
, srcY1
- srcY0
,
618 if (filter
== GL_NEAREST
) {
619 for (i
= 0; i
< 3; i
++) {
620 if (mask
& buffers
[i
]) {
621 blit_nearest(ctx
, srcX0
, srcY0
, srcX1
, srcY1
,
622 dstX0
, dstY0
, dstX1
, dstY1
, buffers
[i
]);
627 ASSERT(filter
== GL_LINEAR
);
628 if (mask
& GL_COLOR_BUFFER_BIT
) { /* depth/stencil not allowed */
629 swrast_render_start(ctx
);
630 blit_linear(ctx
, srcX0
, srcY0
, srcX1
, srcY1
,
631 dstX0
, dstY0
, dstX1
, dstY1
);
632 swrast_render_finish(ctx
);