swrast: fix blit code's nearest/linear coordinate arithmetic
[mesa.git] / src / mesa / swrast / s_blit.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 6.5
4 *
5 * Copyright (C) 1999-2006 Brian Paul All Rights Reserved.
6 *
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:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
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.
23 */
24
25
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"
33
34
35 #define ABS(X) ((X) < 0 ? -(X) : (X))
36
37
38 /**
39 * Generate a row resampler function for GL_NEAREST mode.
40 */
41 #define RESAMPLE(NAME, PIXELTYPE, SIZE) \
42 static void \
43 NAME(GLint srcWidth, GLint dstWidth, \
44 const GLvoid *srcBuffer, GLvoid *dstBuffer, \
45 GLboolean flip) \
46 { \
47 const PIXELTYPE *src = (const PIXELTYPE *) srcBuffer;\
48 PIXELTYPE *dst = (PIXELTYPE *) dstBuffer; \
49 GLint dstCol; \
50 \
51 if (flip) { \
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 */ \
57 if (SIZE == 1) { \
58 dst[dstCol] = src[srcCol]; \
59 } \
60 else if (SIZE == 2) { \
61 dst[dstCol*2+0] = src[srcCol*2+0]; \
62 dst[dstCol*2+1] = src[srcCol*2+1]; \
63 } \
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]; \
69 } \
70 } \
71 } \
72 else { \
73 for (dstCol = 0; dstCol < dstWidth; dstCol++) { \
74 GLint srcCol = (dstCol * srcWidth) / dstWidth; \
75 ASSERT(srcCol >= 0); \
76 ASSERT(srcCol < srcWidth); \
77 if (SIZE == 1) { \
78 dst[dstCol] = src[srcCol]; \
79 } \
80 else if (SIZE == 2) { \
81 dst[dstCol*2+0] = src[srcCol*2+0]; \
82 dst[dstCol*2+1] = src[srcCol*2+1]; \
83 } \
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]; \
89 } \
90 } \
91 } \
92 }
93
94 /**
95 * Resamplers for 1, 2, 4, 8 and 16-byte pixels.
96 */
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)
102
103
104 /**
105 * Blit color, depth or stencil with GL_NEAREST filtering.
106 */
107 static void
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,
111 GLbitfield buffer)
112 {
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;
118 GLuint i;
119
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);
124
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);
129
130 const GLboolean invertX = (srcX1 < srcX0) ^ (dstX1 < dstX0);
131 const GLboolean invertY = (srcY1 < srcY0) ^ (dstY1 < dstY0);
132 enum mode {
133 DIRECT,
134 UNPACK_RGBA_FLOAT,
135 UNPACK_Z_FLOAT,
136 UNPACK_Z_INT,
137 UNPACK_S,
138 } mode;
139 GLubyte *srcMap, *dstMap;
140 GLint srcRowStride, dstRowStride;
141 GLint dstRow;
142
143 GLint pixelSize;
144 GLvoid *srcBuffer, *dstBuffer;
145 GLint prevY = -1;
146
147 typedef void (*resample_func)(GLint srcWidth, GLint dstWidth,
148 const GLvoid *srcBuffer, GLvoid *dstBuffer,
149 GLboolean flip);
150 resample_func resampleRow;
151
152 switch (buffer) {
153 case GL_COLOR_BUFFER_BIT:
154 readAtt = &readFb->Attachment[readFb->_ColorReadBufferIndex];
155 readRb = readFb->_ColorReadBuffer;
156 numDrawBuffers = drawFb->_NumColorDrawBuffers;
157 break;
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;
163 numDrawBuffers = 1;
164
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.
168 */
169 if (readRb->Format == MESA_FORMAT_Z32_FLOAT ||
170 readRb->Format == MESA_FORMAT_Z32_FLOAT_X24S8) {
171 mode = UNPACK_Z_FLOAT;
172 } else {
173 mode = UNPACK_Z_INT;
174 }
175 pixelSize = 4;
176 break;
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;
182 numDrawBuffers = 1;
183 mode = UNPACK_S;
184 pixelSize = 1;
185 break;
186 default:
187 _mesa_problem(ctx, "unexpected buffer in blit_nearest()");
188 return;
189 }
190
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];
195 if (idx == -1)
196 continue;
197 drawAtt = &drawFb->Attachment[idx];
198 drawRb = drawAtt->Renderbuffer;
199
200 if (!drawRb)
201 continue;
202
203 if (readRb->Format == drawRb->Format) {
204 mode = DIRECT;
205 pixelSize = _mesa_get_format_bytes(readRb->Format);
206 } else {
207 mode = UNPACK_RGBA_FLOAT;
208 pixelSize = 16;
209 }
210 }
211
212 /* choose row resampler */
213 switch (pixelSize) {
214 case 1:
215 resampleRow = resample_row_1;
216 break;
217 case 2:
218 resampleRow = resample_row_2;
219 break;
220 case 4:
221 resampleRow = resample_row_4;
222 break;
223 case 8:
224 resampleRow = resample_row_8;
225 break;
226 case 16:
227 resampleRow = resample_row_16;
228 break;
229 default:
230 _mesa_problem(ctx, "unexpected pixel size (%d) in blit_nearest",
231 pixelSize);
232 return;
233 }
234
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.
241 */
242 GLubyte *map;
243 GLint rowStride;
244 GLint formatSize = _mesa_get_format_bytes(readRb->Format);
245
246 ctx->Driver.MapRenderbuffer(ctx, readRb, 0, 0,
247 readRb->Width, readRb->Height,
248 GL_MAP_READ_BIT | GL_MAP_WRITE_BIT,
249 &map, &rowStride);
250 if (!map) {
251 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBlitFramebuffer");
252 return;
253 }
254
255 srcMap = map + srcYpos * rowStride + srcXpos * formatSize;
256 dstMap = map + dstYpos * rowStride + dstXpos * formatSize;
257
258 /* this handles overlapping copies */
259 if (srcY0 < dstY0) {
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;
265 }
266 else {
267 /* copy in normal (bottom->up) order */
268 srcRowStride = rowStride;
269 dstRowStride = rowStride;
270 }
271 }
272 else {
273 /* different src/dst buffers */
274 ctx->Driver.MapRenderbuffer(ctx, readRb,
275 srcXpos, srcYpos,
276 srcWidth, srcHeight,
277 GL_MAP_READ_BIT, &srcMap, &srcRowStride);
278 if (!srcMap) {
279 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBlitFramebuffer");
280 return;
281 }
282 ctx->Driver.MapRenderbuffer(ctx, drawRb,
283 dstXpos, dstYpos,
284 dstWidth, dstHeight,
285 GL_MAP_WRITE_BIT, &dstMap, &dstRowStride);
286 if (!dstMap) {
287 ctx->Driver.UnmapRenderbuffer(ctx, readRb);
288 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBlitFramebuffer");
289 return;
290 }
291 }
292
293 /* allocate the src/dst row buffers */
294 srcBuffer = malloc(pixelSize * srcWidth);
295 if (!srcBuffer) {
296 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBlitFrameBufferEXT");
297 return;
298 }
299 dstBuffer = malloc(pixelSize * dstWidth);
300 if (!dstBuffer) {
301 free(srcBuffer);
302 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBlitFrameBufferEXT");
303 return;
304 }
305
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;
310
311 ASSERT(srcRow >= 0);
312 ASSERT(srcRow < srcHeight);
313
314 if (invertY) {
315 srcRow = srcHeight - 1 - srcRow;
316 }
317
318 /* get pixel row from source and resample to match dest width */
319 if (prevY != srcRow) {
320 GLubyte *srcRowStart = srcMap + srcRowStride * srcRow;
321
322 switch (mode) {
323 case DIRECT:
324 memcpy(srcBuffer, srcRowStart, pixelSize * srcWidth);
325 break;
326 case UNPACK_RGBA_FLOAT:
327 _mesa_unpack_rgba_row(readRb->Format, srcWidth, srcRowStart,
328 srcBuffer);
329 break;
330 case UNPACK_Z_FLOAT:
331 _mesa_unpack_float_z_row(readRb->Format, srcWidth, srcRowStart,
332 srcBuffer);
333 break;
334 case UNPACK_Z_INT:
335 _mesa_unpack_uint_z_row(readRb->Format, srcWidth, srcRowStart,
336 srcBuffer);
337 break;
338 case UNPACK_S:
339 _mesa_unpack_ubyte_stencil_row(readRb->Format, srcWidth,
340 srcRowStart, srcBuffer);
341 break;
342 }
343
344 (*resampleRow)(srcWidth, dstWidth, srcBuffer, dstBuffer, invertX);
345 prevY = srcRow;
346 }
347
348 /* store pixel row in destination */
349 switch (mode) {
350 case DIRECT:
351 memcpy(dstRowStart, dstBuffer, pixelSize * dstWidth);
352 break;
353 case UNPACK_RGBA_FLOAT:
354 _mesa_pack_float_rgba_row(drawRb->Format, dstWidth, dstBuffer,
355 dstRowStart);
356 break;
357 case UNPACK_Z_FLOAT:
358 _mesa_pack_float_z_row(drawRb->Format, dstWidth, dstBuffer,
359 dstRowStart);
360 break;
361 case UNPACK_Z_INT:
362 _mesa_pack_uint_z_row(drawRb->Format, dstWidth, dstBuffer,
363 dstRowStart);
364 break;
365 case UNPACK_S:
366 _mesa_pack_ubyte_stencil_row(drawRb->Format, dstWidth, dstBuffer,
367 dstRowStart);
368 break;
369 }
370 }
371
372 free(srcBuffer);
373 free(dstBuffer);
374
375 ctx->Driver.UnmapRenderbuffer(ctx, readRb);
376 if (drawRb != readRb) {
377 ctx->Driver.UnmapRenderbuffer(ctx, drawRb);
378 }
379 }
380 }
381
382
383
384 #define LERP(T, A, B) ( (A) + (T) * ((B) - (A)) )
385
386 static inline GLfloat
387 lerp_2d(GLfloat a, GLfloat b,
388 GLfloat v00, GLfloat v10, GLfloat v01, GLfloat v11)
389 {
390 const GLfloat temp0 = LERP(a, v00, v10);
391 const GLfloat temp1 = LERP(a, v01, v11);
392 return LERP(b, temp0, temp1);
393 }
394
395
396 /**
397 * Bilinear interpolation of two source rows.
398 * GLubyte pixels.
399 */
400 static void
401 resample_linear_row_ub(GLint srcWidth, GLint dstWidth,
402 const GLvoid *srcBuffer0, const GLvoid *srcBuffer1,
403 GLvoid *dstBuffer, GLboolean flip, GLfloat rowWeight)
404 {
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;
408 GLint dstCol;
409
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;
416
417 ASSERT(srcCol0 < srcWidth);
418 ASSERT(srcCol1 <= srcWidth);
419
420 if (srcCol1 == srcWidth) {
421 /* last column fudge */
422 srcCol1--;
423 colWeight = 0.0;
424 }
425
426 if (flip) {
427 srcCol0 = srcWidth - 1 - srcCol0;
428 srcCol1 = srcWidth - 1 - srcCol1;
429 }
430
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]);
443
444 dstColor[dstCol][RCOMP] = IFLOOR(red);
445 dstColor[dstCol][GCOMP] = IFLOOR(green);
446 dstColor[dstCol][BCOMP] = IFLOOR(blue);
447 dstColor[dstCol][ACOMP] = IFLOOR(alpha);
448 }
449 }
450
451
452 /**
453 * Bilinear interpolation of two source rows. floating point pixels.
454 */
455 static void
456 resample_linear_row_float(GLint srcWidth, GLint dstWidth,
457 const GLvoid *srcBuffer0, const GLvoid *srcBuffer1,
458 GLvoid *dstBuffer, GLboolean flip, GLfloat rowWeight)
459 {
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;
463 GLint dstCol;
464
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;
471
472 ASSERT(srcCol0 < srcWidth);
473 ASSERT(srcCol1 <= srcWidth);
474
475 if (srcCol1 == srcWidth) {
476 /* last column fudge */
477 srcCol1--;
478 colWeight = 0.0;
479 }
480
481 if (flip) {
482 srcCol0 = srcWidth - 1 - srcCol0;
483 srcCol1 = srcWidth - 1 - srcCol1;
484 }
485
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]);
498
499 dstColor[dstCol][RCOMP] = red;
500 dstColor[dstCol][GCOMP] = green;
501 dstColor[dstCol][BCOMP] = blue;
502 dstColor[dstCol][ACOMP] = alpha;
503 }
504 }
505
506
507
508 /**
509 * Bilinear filtered blit (color only, non-integer values).
510 */
511 static void
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)
515 {
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];
521
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);
526
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);
531
532 const GLboolean invertX = (srcX1 < srcX0) ^ (dstX1 < dstX0);
533 const GLboolean invertY = (srcY1 < srcY0) ^ (dstY1 < dstY0);
534
535 GLint dstRow;
536
537 GLint pixelSize;
538 GLvoid *srcBuffer0, *srcBuffer1;
539 GLint srcBufferY0 = -1, srcBufferY1 = -1;
540 GLvoid *dstBuffer;
541
542 gl_format readFormat = _mesa_get_srgb_format_linear(readRb->Format);
543 GLuint bpp = _mesa_get_format_bytes(readFormat);
544
545 GLenum pixelType;
546
547 GLubyte *srcMap, *dstMap;
548 GLint srcRowStride, dstRowStride;
549 GLuint i;
550
551
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);
557 }
558 else {
559 pixelType = GL_FLOAT;
560 pixelSize = 4 * sizeof(GLfloat);
561 }
562
563 /* Allocate the src/dst row buffers.
564 * Keep two adjacent src rows around for bilinear sampling.
565 */
566 srcBuffer0 = malloc(pixelSize * srcWidth);
567 if (!srcBuffer0) {
568 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBlitFrameBufferEXT");
569 return;
570 }
571 srcBuffer1 = malloc(pixelSize * srcWidth);
572 if (!srcBuffer1) {
573 free(srcBuffer0);
574 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBlitFrameBufferEXT");
575 return;
576 }
577 dstBuffer = malloc(pixelSize * dstWidth);
578 if (!dstBuffer) {
579 free(srcBuffer0);
580 free(srcBuffer1);
581 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBlitFrameBufferEXT");
582 return;
583 }
584
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;
590
591 if (idx == -1)
592 continue;
593
594 drawAtt = &drawFb->Attachment[idx];
595 drawRb = drawAtt->Renderbuffer;
596 if (!drawRb)
597 continue;
598
599 drawFormat = _mesa_get_srgb_format_linear(drawRb->Format);
600
601 /*
602 * Map src / dst renderbuffers
603 */
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);
612 if (!srcMap) {
613 free(srcBuffer0);
614 free(srcBuffer1);
615 free(dstBuffer);
616 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBlitFramebuffer");
617 return;
618 }
619
620 dstMap = srcMap;
621 dstRowStride = srcRowStride;
622 }
623 else {
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.
627 */
628 ctx->Driver.MapRenderbuffer(ctx, readRb,
629 0, 0, readRb->Width, readRb->Height,
630 GL_MAP_READ_BIT, &srcMap, &srcRowStride);
631 if (!srcMap) {
632 free(srcBuffer0);
633 free(srcBuffer1);
634 free(dstBuffer);
635 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBlitFramebuffer");
636 return;
637 }
638 ctx->Driver.MapRenderbuffer(ctx, drawRb,
639 0, 0, drawRb->Width, drawRb->Height,
640 GL_MAP_WRITE_BIT, &dstMap, &dstRowStride);
641 if (!dstMap) {
642 ctx->Driver.UnmapRenderbuffer(ctx, readRb);
643 free(srcBuffer0);
644 free(srcBuffer1);
645 free(dstBuffer);
646 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBlitFramebuffer");
647 return;
648 }
649 }
650
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 */
657
658 if (srcRow1 == srcHeight) {
659 /* last row fudge */
660 srcRow1 = srcRow0;
661 rowWeight = 0.0;
662 }
663
664 if (invertY) {
665 srcRow0 = srcHeight - 1 - srcRow0;
666 srcRow1 = srcHeight - 1 - srcRow1;
667 }
668
669 srcY0 = srcYpos + srcRow0;
670 srcY1 = srcYpos + srcRow1;
671
672 /* get the two source rows */
673 if (srcY0 == srcBufferY0 && srcY1 == srcBufferY1) {
674 /* use same source row buffers again */
675 }
676 else if (srcY0 == srcBufferY1) {
677 /* move buffer1 into buffer0 by swapping pointers */
678 GLvoid *tmp = srcBuffer0;
679 srcBuffer0 = srcBuffer1;
680 srcBuffer1 = tmp;
681 /* get y1 row */
682 {
683 GLubyte *src = srcMap + srcY1 * srcRowStride + srcXpos * bpp;
684 if (pixelType == GL_UNSIGNED_BYTE) {
685 _mesa_unpack_ubyte_rgba_row(readFormat, srcWidth,
686 src, srcBuffer1);
687 }
688 else {
689 _mesa_unpack_rgba_row(readFormat, srcWidth,
690 src, srcBuffer1);
691 }
692 }
693 srcBufferY0 = srcY0;
694 srcBufferY1 = srcY1;
695 }
696 else {
697 /* get both new rows */
698 {
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,
703 src0, srcBuffer0);
704 _mesa_unpack_ubyte_rgba_row(readFormat, srcWidth,
705 src1, srcBuffer1);
706 }
707 else {
708 _mesa_unpack_rgba_row(readFormat, srcWidth, src0, srcBuffer0);
709 _mesa_unpack_rgba_row(readFormat, srcWidth, src1, srcBuffer1);
710 }
711 }
712 srcBufferY0 = srcY0;
713 srcBufferY1 = srcY1;
714 }
715
716 if (pixelType == GL_UNSIGNED_BYTE) {
717 resample_linear_row_ub(srcWidth, dstWidth, srcBuffer0, srcBuffer1,
718 dstBuffer, invertX, rowWeight);
719 }
720 else {
721 resample_linear_row_float(srcWidth, dstWidth, srcBuffer0, srcBuffer1,
722 dstBuffer, invertX, rowWeight);
723 }
724
725 /* store pixel row in destination */
726 {
727 GLubyte *dst = dstMap + dstY * dstRowStride + dstXpos * bpp;
728 if (pixelType == GL_UNSIGNED_BYTE) {
729 _mesa_pack_ubyte_rgba_row(drawFormat, dstWidth, dstBuffer, dst);
730 }
731 else {
732 _mesa_pack_float_rgba_row(drawFormat, dstWidth, dstBuffer, dst);
733 }
734 }
735 }
736
737 free(srcBuffer0);
738 free(srcBuffer1);
739 free(dstBuffer);
740
741 ctx->Driver.UnmapRenderbuffer(ctx, readRb);
742 if (drawRb != readRb) {
743 ctx->Driver.UnmapRenderbuffer(ctx, drawRb);
744 }
745 }
746 }
747
748
749
750 /**
751 * Software fallback for glBlitFramebufferEXT().
752 */
753 void
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)
758 {
759 static const GLbitfield buffers[3] = {
760 GL_COLOR_BUFFER_BIT,
761 GL_DEPTH_BUFFER_BIT,
762 GL_STENCIL_BUFFER_BIT
763 };
764 static const GLenum buffer_enums[3] = {
765 GL_COLOR,
766 GL_DEPTH,
767 GL_STENCIL,
768 };
769 GLint i;
770
771 if (!_mesa_clip_blit(ctx, &srcX0, &srcY0, &srcX1, &srcY1,
772 &dstX0, &dstY0, &dstX1, &dstY1)) {
773 return;
774 }
775
776 if (SWRAST_CONTEXT(ctx)->NewState)
777 _swrast_validate_derived(ctx);
778
779 /* First, try covering whatever buffers possible using the fast 1:1 copy
780 * path.
781 */
782 if (srcX1 - srcX0 == dstX1 - dstX0 &&
783 srcY1 - srcY0 == dstY1 - dstY0 &&
784 srcX0 < srcX1 &&
785 srcY0 < srcY1 &&
786 dstX0 < dstX1 &&
787 dstY0 < dstY1) {
788 for (i = 0; i < 3; i++) {
789 if (mask & buffers[i]) {
790 if (swrast_fast_copy_pixels(ctx,
791 srcX0, srcY0,
792 srcX1 - srcX0, srcY1 - srcY0,
793 dstX0, dstY0,
794 buffer_enums[i])) {
795 mask &= ~buffers[i];
796 }
797 }
798 }
799
800 if (!mask)
801 return;
802 }
803
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]);
809 }
810 }
811 }
812 else {
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);
817 }
818 }
819
820 }