Merge branch 'mesa_7_7_branch'
[mesa.git] / src / mesa / swrast / s_copypix.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 7.1
4 *
5 * Copyright (C) 1999-2007 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/context.h"
28 #include "main/colormac.h"
29 #include "main/condrender.h"
30 #include "main/convolve.h"
31 #include "main/image.h"
32 #include "main/macros.h"
33 #include "main/imports.h"
34
35 #include "s_context.h"
36 #include "s_depth.h"
37 #include "s_span.h"
38 #include "s_stencil.h"
39 #include "s_zoom.h"
40
41
42
43 /**
44 * Determine if there's overlap in an image copy.
45 * This test also compensates for the fact that copies are done from
46 * bottom to top and overlaps can sometimes be handled correctly
47 * without making a temporary image copy.
48 * \return GL_TRUE if the regions overlap, GL_FALSE otherwise.
49 */
50 static GLboolean
51 regions_overlap(GLint srcx, GLint srcy,
52 GLint dstx, GLint dsty,
53 GLint width, GLint height,
54 GLfloat zoomX, GLfloat zoomY)
55 {
56 if (zoomX == 1.0 && zoomY == 1.0) {
57 /* no zoom */
58 if (srcx >= dstx + width || (srcx + width <= dstx)) {
59 return GL_FALSE;
60 }
61 else if (srcy < dsty) { /* this is OK */
62 return GL_FALSE;
63 }
64 else if (srcy > dsty + height) {
65 return GL_FALSE;
66 }
67 else {
68 return GL_TRUE;
69 }
70 }
71 else {
72 /* add one pixel of slop when zooming, just to be safe */
73 if (srcx > (dstx + ((zoomX > 0.0F) ? (width * zoomX + 1.0F) : 0.0F))) {
74 /* src is completely right of dest */
75 return GL_FALSE;
76 }
77 else if (srcx + width + 1.0F < dstx + ((zoomX > 0.0F) ? 0.0F : (width * zoomX))) {
78 /* src is completely left of dest */
79 return GL_FALSE;
80 }
81 else if ((srcy < dsty) && (srcy + height < dsty + (height * zoomY))) {
82 /* src is completely below dest */
83 return GL_FALSE;
84 }
85 else if ((srcy > dsty) && (srcy + height > dsty + (height * zoomY))) {
86 /* src is completely above dest */
87 return GL_FALSE;
88 }
89 else {
90 return GL_TRUE;
91 }
92 }
93 }
94
95
96 /**
97 * RGBA copypixels with convolution.
98 */
99 static void
100 copy_conv_rgba_pixels(GLcontext *ctx, GLint srcx, GLint srcy,
101 GLint width, GLint height, GLint destx, GLint desty)
102 {
103 GLint row;
104 const GLboolean zoom = ctx->Pixel.ZoomX != 1.0F || ctx->Pixel.ZoomY != 1.0F;
105 const GLbitfield transferOps = ctx->_ImageTransferState;
106 const GLboolean sink = (ctx->Pixel.MinMaxEnabled && ctx->MinMax.Sink)
107 || (ctx->Pixel.HistogramEnabled && ctx->Histogram.Sink);
108 GLfloat *dest, *tmpImage, *convImage;
109 SWspan span;
110
111 INIT_SPAN(span, GL_BITMAP);
112 _swrast_span_default_attribs(ctx, &span);
113 span.arrayMask = SPAN_RGBA;
114 span.arrayAttribs = FRAG_BIT_COL0;
115
116 /* allocate space for GLfloat image */
117 tmpImage = (GLfloat *) _mesa_malloc(width * height * 4 * sizeof(GLfloat));
118 if (!tmpImage) {
119 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCopyPixels");
120 return;
121 }
122 convImage = (GLfloat *) _mesa_malloc(width * height * 4 * sizeof(GLfloat));
123 if (!convImage) {
124 _mesa_free(tmpImage);
125 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCopyPixels");
126 return;
127 }
128
129 /* read source image as float/RGBA */
130 dest = tmpImage;
131 for (row = 0; row < height; row++) {
132 _swrast_read_rgba_span(ctx, ctx->ReadBuffer->_ColorReadBuffer,
133 width, srcx, srcy + row, GL_FLOAT, dest);
134 dest += 4 * width;
135 }
136
137 /* do the image transfer ops which preceed convolution */
138 for (row = 0; row < height; row++) {
139 GLfloat (*rgba)[4] = (GLfloat (*)[4]) (tmpImage + row * width * 4);
140 _mesa_apply_rgba_transfer_ops(ctx,
141 transferOps & IMAGE_PRE_CONVOLUTION_BITS,
142 width, rgba);
143 }
144
145 /* do convolution */
146 if (ctx->Pixel.Convolution2DEnabled) {
147 _mesa_convolve_2d_image(ctx, &width, &height, tmpImage, convImage);
148 }
149 else {
150 ASSERT(ctx->Pixel.Separable2DEnabled);
151 _mesa_convolve_sep_image(ctx, &width, &height, tmpImage, convImage);
152 }
153 _mesa_free(tmpImage);
154
155 /* do remaining post-convolution image transfer ops */
156 for (row = 0; row < height; row++) {
157 GLfloat (*rgba)[4] = (GLfloat (*)[4]) (convImage + row * width * 4);
158 _mesa_apply_rgba_transfer_ops(ctx,
159 transferOps & IMAGE_POST_CONVOLUTION_BITS,
160 width, rgba);
161 }
162
163 if (!sink) {
164 /* write the new image */
165 for (row = 0; row < height; row++) {
166 const GLfloat *src = convImage + row * width * 4;
167 GLfloat *rgba = (GLfloat *) span.array->attribs[FRAG_ATTRIB_COL0];
168
169 /* copy convolved colors into span array */
170 _mesa_memcpy(rgba, src, width * 4 * sizeof(GLfloat));
171
172 /* write span */
173 span.x = destx;
174 span.y = desty + row;
175 span.end = width;
176 span.array->ChanType = GL_FLOAT;
177 if (zoom) {
178 _swrast_write_zoomed_rgba_span(ctx, destx, desty, &span, rgba);
179 }
180 else {
181 _swrast_write_rgba_span(ctx, &span);
182 }
183 }
184 /* restore this */
185 span.array->ChanType = CHAN_TYPE;
186 }
187
188 _mesa_free(convImage);
189 }
190
191
192 /**
193 * RGBA copypixels
194 */
195 static void
196 copy_rgba_pixels(GLcontext *ctx, GLint srcx, GLint srcy,
197 GLint width, GLint height, GLint destx, GLint desty)
198 {
199 GLfloat *tmpImage, *p;
200 GLint sy, dy, stepy, row;
201 const GLboolean zoom = ctx->Pixel.ZoomX != 1.0F || ctx->Pixel.ZoomY != 1.0F;
202 GLint overlapping;
203 GLuint transferOps = ctx->_ImageTransferState;
204 SWspan span;
205
206 if (!ctx->ReadBuffer->_ColorReadBuffer) {
207 /* no readbuffer - OK */
208 return;
209 }
210
211 if (ctx->Pixel.Convolution2DEnabled || ctx->Pixel.Separable2DEnabled) {
212 copy_conv_rgba_pixels(ctx, srcx, srcy, width, height, destx, desty);
213 return;
214 }
215 else if (ctx->Pixel.Convolution1DEnabled) {
216 /* make sure we don't apply 1D convolution */
217 transferOps &= ~(IMAGE_CONVOLUTION_BIT |
218 IMAGE_POST_CONVOLUTION_SCALE_BIAS);
219 }
220
221 if (ctx->DrawBuffer == ctx->ReadBuffer) {
222 overlapping = regions_overlap(srcx, srcy, destx, desty, width, height,
223 ctx->Pixel.ZoomX, ctx->Pixel.ZoomY);
224 }
225 else {
226 overlapping = GL_FALSE;
227 }
228
229 /* Determine if copy should be done bottom-to-top or top-to-bottom */
230 if (!overlapping && srcy < desty) {
231 /* top-down max-to-min */
232 sy = srcy + height - 1;
233 dy = desty + height - 1;
234 stepy = -1;
235 }
236 else {
237 /* bottom-up min-to-max */
238 sy = srcy;
239 dy = desty;
240 stepy = 1;
241 }
242
243 INIT_SPAN(span, GL_BITMAP);
244 _swrast_span_default_attribs(ctx, &span);
245 span.arrayMask = SPAN_RGBA;
246 span.arrayAttribs = FRAG_BIT_COL0; /* we'll fill in COL0 attrib values */
247
248 if (overlapping) {
249 tmpImage = (GLfloat *) _mesa_malloc(width * height * sizeof(GLfloat) * 4);
250 if (!tmpImage) {
251 _mesa_error( ctx, GL_OUT_OF_MEMORY, "glCopyPixels" );
252 return;
253 }
254 /* read the source image as RGBA/float */
255 p = tmpImage;
256 for (row = 0; row < height; row++) {
257 _swrast_read_rgba_span( ctx, ctx->ReadBuffer->_ColorReadBuffer,
258 width, srcx, sy + row, GL_FLOAT, p );
259 p += width * 4;
260 }
261 p = tmpImage;
262 }
263 else {
264 tmpImage = NULL; /* silence compiler warnings */
265 p = NULL;
266 }
267
268 ASSERT(width < MAX_WIDTH);
269
270 for (row = 0; row < height; row++, sy += stepy, dy += stepy) {
271 GLvoid *rgba = span.array->attribs[FRAG_ATTRIB_COL0];
272
273 /* Get row/span of source pixels */
274 if (overlapping) {
275 /* get from buffered image */
276 _mesa_memcpy(rgba, p, width * sizeof(GLfloat) * 4);
277 p += width * 4;
278 }
279 else {
280 /* get from framebuffer */
281 _swrast_read_rgba_span( ctx, ctx->ReadBuffer->_ColorReadBuffer,
282 width, srcx, sy, GL_FLOAT, rgba );
283 }
284
285 if (transferOps) {
286 _mesa_apply_rgba_transfer_ops(ctx, transferOps, width,
287 (GLfloat (*)[4]) rgba);
288 }
289
290 /* Write color span */
291 span.x = destx;
292 span.y = dy;
293 span.end = width;
294 span.array->ChanType = GL_FLOAT;
295 if (zoom) {
296 _swrast_write_zoomed_rgba_span(ctx, destx, desty, &span, rgba);
297 }
298 else {
299 _swrast_write_rgba_span(ctx, &span);
300 }
301 }
302
303 span.array->ChanType = CHAN_TYPE; /* restore */
304
305 if (overlapping)
306 _mesa_free(tmpImage);
307 }
308
309
310 static void
311 copy_ci_pixels( GLcontext *ctx, GLint srcx, GLint srcy,
312 GLint width, GLint height,
313 GLint destx, GLint desty )
314 {
315 GLuint *tmpImage,*p;
316 GLint sy, dy, stepy;
317 GLint j;
318 const GLboolean zoom = ctx->Pixel.ZoomX != 1.0F || ctx->Pixel.ZoomY != 1.0F;
319 GLint overlapping;
320 SWspan span;
321
322 if (!ctx->ReadBuffer->_ColorReadBuffer) {
323 /* no readbuffer - OK */
324 return;
325 }
326
327 INIT_SPAN(span, GL_BITMAP);
328 _swrast_span_default_attribs(ctx, &span);
329 span.arrayMask = SPAN_INDEX;
330
331 if (ctx->DrawBuffer == ctx->ReadBuffer) {
332 overlapping = regions_overlap(srcx, srcy, destx, desty, width, height,
333 ctx->Pixel.ZoomX, ctx->Pixel.ZoomY);
334 }
335 else {
336 overlapping = GL_FALSE;
337 }
338
339 /* Determine if copy should be bottom-to-top or top-to-bottom */
340 if (!overlapping && srcy < desty) {
341 /* top-down max-to-min */
342 sy = srcy + height - 1;
343 dy = desty + height - 1;
344 stepy = -1;
345 }
346 else {
347 /* bottom-up min-to-max */
348 sy = srcy;
349 dy = desty;
350 stepy = 1;
351 }
352
353 if (overlapping) {
354 GLint ssy = sy;
355 tmpImage = (GLuint *) _mesa_malloc(width * height * sizeof(GLuint));
356 if (!tmpImage) {
357 _mesa_error( ctx, GL_OUT_OF_MEMORY, "glCopyPixels" );
358 return;
359 }
360 /* read the image */
361 p = tmpImage;
362 for (j = 0; j < height; j++, ssy += stepy) {
363 _swrast_read_index_span( ctx, ctx->ReadBuffer->_ColorReadBuffer,
364 width, srcx, ssy, p );
365 p += width;
366 }
367 p = tmpImage;
368 }
369 else {
370 tmpImage = NULL; /* silence compiler warning */
371 p = NULL;
372 }
373
374 for (j = 0; j < height; j++, sy += stepy, dy += stepy) {
375 /* Get color indexes */
376 if (overlapping) {
377 _mesa_memcpy(span.array->index, p, width * sizeof(GLuint));
378 p += width;
379 }
380 else {
381 _swrast_read_index_span( ctx, ctx->ReadBuffer->_ColorReadBuffer,
382 width, srcx, sy, span.array->index );
383 }
384
385 if (ctx->_ImageTransferState)
386 _mesa_apply_ci_transfer_ops(ctx, ctx->_ImageTransferState,
387 width, span.array->index);
388
389 /* write color indexes */
390 span.x = destx;
391 span.y = dy;
392 span.end = width;
393 if (zoom)
394 _swrast_write_zoomed_index_span(ctx, destx, desty, &span);
395 else
396 _swrast_write_index_span(ctx, &span);
397 }
398
399 if (overlapping)
400 _mesa_free(tmpImage);
401 }
402
403
404 /**
405 * Convert floating point Z values to integer Z values with pixel transfer's
406 * Z scale and bias.
407 */
408 static void
409 scale_and_bias_z(GLcontext *ctx, GLuint width,
410 const GLfloat depth[], GLuint z[])
411 {
412 const GLuint depthMax = ctx->DrawBuffer->_DepthMax;
413 GLuint i;
414
415 if (depthMax <= 0xffffff &&
416 ctx->Pixel.DepthScale == 1.0 &&
417 ctx->Pixel.DepthBias == 0.0) {
418 /* no scale or bias and no clamping and no worry of overflow */
419 const GLfloat depthMaxF = ctx->DrawBuffer->_DepthMaxF;
420 for (i = 0; i < width; i++) {
421 z[i] = (GLuint) (depth[i] * depthMaxF);
422 }
423 }
424 else {
425 /* need to be careful with overflow */
426 const GLdouble depthMaxF = ctx->DrawBuffer->_DepthMaxF;
427 for (i = 0; i < width; i++) {
428 GLdouble d = depth[i] * ctx->Pixel.DepthScale + ctx->Pixel.DepthBias;
429 d = CLAMP(d, 0.0, 1.0) * depthMaxF;
430 if (d >= depthMaxF)
431 z[i] = depthMax;
432 else
433 z[i] = (GLuint) d;
434 }
435 }
436 }
437
438
439
440 /*
441 * TODO: Optimize!!!!
442 */
443 static void
444 copy_depth_pixels( GLcontext *ctx, GLint srcx, GLint srcy,
445 GLint width, GLint height,
446 GLint destx, GLint desty )
447 {
448 struct gl_framebuffer *fb = ctx->ReadBuffer;
449 struct gl_renderbuffer *readRb = fb->_DepthBuffer;
450 GLfloat *p, *tmpImage;
451 GLint sy, dy, stepy;
452 GLint j;
453 const GLboolean zoom = ctx->Pixel.ZoomX != 1.0F || ctx->Pixel.ZoomY != 1.0F;
454 GLint overlapping;
455 SWspan span;
456
457 if (!readRb) {
458 /* no readbuffer - OK */
459 return;
460 }
461
462 INIT_SPAN(span, GL_BITMAP);
463 _swrast_span_default_attribs(ctx, &span);
464 span.arrayMask = SPAN_Z;
465
466 if (ctx->DrawBuffer == ctx->ReadBuffer) {
467 overlapping = regions_overlap(srcx, srcy, destx, desty, width, height,
468 ctx->Pixel.ZoomX, ctx->Pixel.ZoomY);
469 }
470 else {
471 overlapping = GL_FALSE;
472 }
473
474 /* Determine if copy should be bottom-to-top or top-to-bottom */
475 if (!overlapping && srcy < desty) {
476 /* top-down max-to-min */
477 sy = srcy + height - 1;
478 dy = desty + height - 1;
479 stepy = -1;
480 }
481 else {
482 /* bottom-up min-to-max */
483 sy = srcy;
484 dy = desty;
485 stepy = 1;
486 }
487
488 if (overlapping) {
489 GLint ssy = sy;
490 tmpImage = (GLfloat *) _mesa_malloc(width * height * sizeof(GLfloat));
491 if (!tmpImage) {
492 _mesa_error( ctx, GL_OUT_OF_MEMORY, "glCopyPixels" );
493 return;
494 }
495 p = tmpImage;
496 for (j = 0; j < height; j++, ssy += stepy) {
497 _swrast_read_depth_span_float(ctx, readRb, width, srcx, ssy, p);
498 p += width;
499 }
500 p = tmpImage;
501 }
502 else {
503 tmpImage = NULL; /* silence compiler warning */
504 p = NULL;
505 }
506
507 for (j = 0; j < height; j++, sy += stepy, dy += stepy) {
508 GLfloat depth[MAX_WIDTH];
509 /* get depth values */
510 if (overlapping) {
511 _mesa_memcpy(depth, p, width * sizeof(GLfloat));
512 p += width;
513 }
514 else {
515 _swrast_read_depth_span_float(ctx, readRb, width, srcx, sy, depth);
516 }
517
518 /* apply scale and bias */
519 scale_and_bias_z(ctx, width, depth, span.array->z);
520
521 /* write depth values */
522 span.x = destx;
523 span.y = dy;
524 span.end = width;
525 if (fb->Visual.rgbMode) {
526 if (zoom)
527 _swrast_write_zoomed_depth_span(ctx, destx, desty, &span);
528 else
529 _swrast_write_rgba_span(ctx, &span);
530 }
531 else {
532 if (zoom)
533 _swrast_write_zoomed_depth_span(ctx, destx, desty, &span);
534 else
535 _swrast_write_index_span(ctx, &span);
536 }
537 }
538
539 if (overlapping)
540 _mesa_free(tmpImage);
541 }
542
543
544
545 static void
546 copy_stencil_pixels( GLcontext *ctx, GLint srcx, GLint srcy,
547 GLint width, GLint height,
548 GLint destx, GLint desty )
549 {
550 struct gl_framebuffer *fb = ctx->ReadBuffer;
551 struct gl_renderbuffer *rb = fb->_StencilBuffer;
552 GLint sy, dy, stepy;
553 GLint j;
554 GLstencil *p, *tmpImage;
555 const GLboolean zoom = ctx->Pixel.ZoomX != 1.0F || ctx->Pixel.ZoomY != 1.0F;
556 GLint overlapping;
557
558 if (!rb) {
559 /* no readbuffer - OK */
560 return;
561 }
562
563 if (ctx->DrawBuffer == ctx->ReadBuffer) {
564 overlapping = regions_overlap(srcx, srcy, destx, desty, width, height,
565 ctx->Pixel.ZoomX, ctx->Pixel.ZoomY);
566 }
567 else {
568 overlapping = GL_FALSE;
569 }
570
571 /* Determine if copy should be bottom-to-top or top-to-bottom */
572 if (!overlapping && srcy < desty) {
573 /* top-down max-to-min */
574 sy = srcy + height - 1;
575 dy = desty + height - 1;
576 stepy = -1;
577 }
578 else {
579 /* bottom-up min-to-max */
580 sy = srcy;
581 dy = desty;
582 stepy = 1;
583 }
584
585 if (overlapping) {
586 GLint ssy = sy;
587 tmpImage = (GLstencil *) _mesa_malloc(width * height * sizeof(GLstencil));
588 if (!tmpImage) {
589 _mesa_error( ctx, GL_OUT_OF_MEMORY, "glCopyPixels" );
590 return;
591 }
592 p = tmpImage;
593 for (j = 0; j < height; j++, ssy += stepy) {
594 _swrast_read_stencil_span( ctx, rb, width, srcx, ssy, p );
595 p += width;
596 }
597 p = tmpImage;
598 }
599 else {
600 tmpImage = NULL; /* silence compiler warning */
601 p = NULL;
602 }
603
604 for (j = 0; j < height; j++, sy += stepy, dy += stepy) {
605 GLstencil stencil[MAX_WIDTH];
606
607 /* Get stencil values */
608 if (overlapping) {
609 _mesa_memcpy(stencil, p, width * sizeof(GLstencil));
610 p += width;
611 }
612 else {
613 _swrast_read_stencil_span( ctx, rb, width, srcx, sy, stencil );
614 }
615
616 _mesa_apply_stencil_transfer_ops(ctx, width, stencil);
617
618 /* Write stencil values */
619 if (zoom) {
620 _swrast_write_zoomed_stencil_span(ctx, destx, desty, width,
621 destx, dy, stencil);
622 }
623 else {
624 _swrast_write_stencil_span( ctx, width, destx, dy, stencil );
625 }
626 }
627
628 if (overlapping)
629 _mesa_free(tmpImage);
630 }
631
632
633 /**
634 * This isn't terribly efficient. If a driver really has combined
635 * depth/stencil buffers the driver should implement an optimized
636 * CopyPixels function.
637 */
638 static void
639 copy_depth_stencil_pixels(GLcontext *ctx,
640 const GLint srcX, const GLint srcY,
641 const GLint width, const GLint height,
642 const GLint destX, const GLint destY)
643 {
644 struct gl_renderbuffer *stencilReadRb, *depthReadRb, *depthDrawRb;
645 GLint sy, dy, stepy;
646 GLint j;
647 GLstencil *tempStencilImage = NULL, *stencilPtr = NULL;
648 GLfloat *tempDepthImage = NULL, *depthPtr = NULL;
649 const GLfloat depthScale = ctx->DrawBuffer->_DepthMaxF;
650 const GLuint stencilMask = ctx->Stencil.WriteMask[0];
651 const GLboolean zoom = ctx->Pixel.ZoomX != 1.0F || ctx->Pixel.ZoomY != 1.0F;
652 const GLboolean scaleOrBias
653 = ctx->Pixel.DepthScale != 1.0 || ctx->Pixel.DepthBias != 0.0;
654 GLint overlapping;
655
656 depthDrawRb = ctx->DrawBuffer->_DepthBuffer;
657 depthReadRb = ctx->ReadBuffer->_DepthBuffer;
658 stencilReadRb = ctx->ReadBuffer->_StencilBuffer;
659
660 ASSERT(depthDrawRb);
661 ASSERT(depthReadRb);
662 ASSERT(stencilReadRb);
663
664 if (ctx->DrawBuffer == ctx->ReadBuffer) {
665 overlapping = regions_overlap(srcX, srcY, destX, destY, width, height,
666 ctx->Pixel.ZoomX, ctx->Pixel.ZoomY);
667 }
668 else {
669 overlapping = GL_FALSE;
670 }
671
672 /* Determine if copy should be bottom-to-top or top-to-bottom */
673 if (!overlapping && srcY < destY) {
674 /* top-down max-to-min */
675 sy = srcY + height - 1;
676 dy = destY + height - 1;
677 stepy = -1;
678 }
679 else {
680 /* bottom-up min-to-max */
681 sy = srcY;
682 dy = destY;
683 stepy = 1;
684 }
685
686 if (overlapping) {
687 GLint ssy = sy;
688
689 if (stencilMask != 0x0) {
690 tempStencilImage
691 = (GLstencil *) _mesa_malloc(width * height * sizeof(GLstencil));
692 if (!tempStencilImage) {
693 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCopyPixels");
694 return;
695 }
696
697 /* get copy of stencil pixels */
698 stencilPtr = tempStencilImage;
699 for (j = 0; j < height; j++, ssy += stepy) {
700 _swrast_read_stencil_span(ctx, stencilReadRb,
701 width, srcX, ssy, stencilPtr);
702 stencilPtr += width;
703 }
704 stencilPtr = tempStencilImage;
705 }
706
707 if (ctx->Depth.Mask) {
708 tempDepthImage
709 = (GLfloat *) _mesa_malloc(width * height * sizeof(GLfloat));
710 if (!tempDepthImage) {
711 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCopyPixels");
712 _mesa_free(tempStencilImage);
713 return;
714 }
715
716 /* get copy of depth pixels */
717 depthPtr = tempDepthImage;
718 for (j = 0; j < height; j++, ssy += stepy) {
719 _swrast_read_depth_span_float(ctx, depthReadRb,
720 width, srcX, ssy, depthPtr);
721 depthPtr += width;
722 }
723 depthPtr = tempDepthImage;
724 }
725 }
726
727 for (j = 0; j < height; j++, sy += stepy, dy += stepy) {
728 if (stencilMask != 0x0) {
729 GLstencil stencil[MAX_WIDTH];
730
731 /* Get stencil values */
732 if (overlapping) {
733 _mesa_memcpy(stencil, stencilPtr, width * sizeof(GLstencil));
734 stencilPtr += width;
735 }
736 else {
737 _swrast_read_stencil_span(ctx, stencilReadRb,
738 width, srcX, sy, stencil);
739 }
740
741 _mesa_apply_stencil_transfer_ops(ctx, width, stencil);
742
743 /* Write values */
744 if (zoom) {
745 _swrast_write_zoomed_stencil_span(ctx, destX, destY, width,
746 destX, dy, stencil);
747 }
748 else {
749 _swrast_write_stencil_span( ctx, width, destX, dy, stencil );
750 }
751 }
752
753 if (ctx->Depth.Mask) {
754 GLfloat depth[MAX_WIDTH];
755 GLuint zVals32[MAX_WIDTH];
756 GLushort zVals16[MAX_WIDTH];
757 GLvoid *zVals;
758 GLuint zBytes;
759
760 /* get depth values */
761 if (overlapping) {
762 _mesa_memcpy(depth, depthPtr, width * sizeof(GLfloat));
763 depthPtr += width;
764 }
765 else {
766 _swrast_read_depth_span_float(ctx, depthReadRb,
767 width, srcX, sy, depth);
768 }
769
770 /* scale & bias */
771 if (scaleOrBias) {
772 _mesa_scale_and_bias_depth(ctx, width, depth);
773 }
774 /* convert to integer Z values */
775 if (depthDrawRb->DataType == GL_UNSIGNED_SHORT) {
776 GLint k;
777 for (k = 0; k < width; k++)
778 zVals16[k] = (GLushort) (depth[k] * depthScale);
779 zVals = zVals16;
780 zBytes = 2;
781 }
782 else {
783 GLint k;
784 for (k = 0; k < width; k++)
785 zVals32[k] = (GLuint) (depth[k] * depthScale);
786 zVals = zVals32;
787 zBytes = 4;
788 }
789
790 /* Write values */
791 if (zoom) {
792 _swrast_write_zoomed_z_span(ctx, destX, destY, width,
793 destX, dy, zVals);
794 }
795 else {
796 _swrast_put_row(ctx, depthDrawRb, width, destX, dy, zVals, zBytes);
797 }
798 }
799 }
800
801 if (tempStencilImage)
802 _mesa_free(tempStencilImage);
803
804 if (tempDepthImage)
805 _mesa_free(tempDepthImage);
806 }
807
808
809
810 /**
811 * Try to do a fast copy pixels.
812 */
813 static GLboolean
814 fast_copy_pixels(GLcontext *ctx,
815 GLint srcX, GLint srcY, GLsizei width, GLsizei height,
816 GLint dstX, GLint dstY, GLenum type)
817 {
818 struct gl_framebuffer *srcFb = ctx->ReadBuffer;
819 struct gl_framebuffer *dstFb = ctx->DrawBuffer;
820 struct gl_renderbuffer *srcRb, *dstRb;
821 GLint row, yStep;
822
823 if (SWRAST_CONTEXT(ctx)->_RasterMask != 0x0 ||
824 ctx->Pixel.ZoomX != 1.0F ||
825 ctx->Pixel.ZoomY != 1.0F ||
826 ctx->_ImageTransferState) {
827 /* can't handle these */
828 return GL_FALSE;
829 }
830
831 if (type == GL_COLOR) {
832 if (dstFb->_NumColorDrawBuffers != 1)
833 return GL_FALSE;
834 srcRb = srcFb->_ColorReadBuffer;
835 dstRb = dstFb->_ColorDrawBuffers[0];
836 }
837 else if (type == GL_STENCIL) {
838 srcRb = srcFb->_StencilBuffer;
839 dstRb = dstFb->_StencilBuffer;
840 }
841 else if (type == GL_DEPTH) {
842 srcRb = srcFb->_DepthBuffer;
843 dstRb = dstFb->_DepthBuffer;
844 }
845 else {
846 ASSERT(type == GL_DEPTH_STENCIL_EXT);
847 /* XXX correct? */
848 srcRb = srcFb->Attachment[BUFFER_DEPTH].Renderbuffer;
849 dstRb = dstFb->Attachment[BUFFER_DEPTH].Renderbuffer;
850 }
851
852 /* src and dst renderbuffers must be same format and type */
853 if (!srcRb || !dstRb ||
854 srcRb->DataType != dstRb->DataType ||
855 srcRb->_BaseFormat != dstRb->_BaseFormat) {
856 return GL_FALSE;
857 }
858
859 /* clipping not supported */
860 if (srcX < 0 || srcX + width > (GLint) srcFb->Width ||
861 srcY < 0 || srcY + height > (GLint) srcFb->Height ||
862 dstX < dstFb->_Xmin || dstX + width > dstFb->_Xmax ||
863 dstY < dstFb->_Ymin || dstY + height > dstFb->_Ymax) {
864 return GL_FALSE;
865 }
866
867 /* overlapping src/dst doesn't matter, just determine Y direction */
868 if (srcY < dstY) {
869 /* top-down max-to-min */
870 srcY = srcY + height - 1;
871 dstY = dstY + height - 1;
872 yStep = -1;
873 }
874 else {
875 /* bottom-up min-to-max */
876 yStep = 1;
877 }
878
879 for (row = 0; row < height; row++) {
880 GLuint temp[MAX_WIDTH][4];
881 srcRb->GetRow(ctx, srcRb, width, srcX, srcY, temp);
882 dstRb->PutRow(ctx, dstRb, width, dstX, dstY, temp, NULL);
883 srcY += yStep;
884 dstY += yStep;
885 }
886
887 return GL_TRUE;
888 }
889
890
891 /**
892 * Do software-based glCopyPixels.
893 * By time we get here, all parameters will have been error-checked.
894 */
895 void
896 _swrast_CopyPixels( GLcontext *ctx,
897 GLint srcx, GLint srcy, GLsizei width, GLsizei height,
898 GLint destx, GLint desty, GLenum type )
899 {
900 SWcontext *swrast = SWRAST_CONTEXT(ctx);
901 swrast_render_start(ctx);
902
903 if (!_mesa_check_conditional_render(ctx))
904 return; /* don't copy */
905
906 if (swrast->NewState)
907 _swrast_validate_derived( ctx );
908
909 if (!fast_copy_pixels(ctx, srcx, srcy, width, height, destx, desty, type)) {
910 switch (type) {
911 case GL_COLOR:
912 if (ctx->Visual.rgbMode) {
913 copy_rgba_pixels( ctx, srcx, srcy, width, height, destx, desty );
914 }
915 else {
916 copy_ci_pixels( ctx, srcx, srcy, width, height, destx, desty );
917 }
918 break;
919 case GL_DEPTH:
920 copy_depth_pixels( ctx, srcx, srcy, width, height, destx, desty );
921 break;
922 case GL_STENCIL:
923 copy_stencil_pixels( ctx, srcx, srcy, width, height, destx, desty );
924 break;
925 case GL_DEPTH_STENCIL_EXT:
926 copy_depth_stencil_pixels(ctx, srcx, srcy, width, height, destx, desty);
927 break;
928 default:
929 _mesa_problem(ctx, "unexpected type in _swrast_CopyPixels");
930 }
931 }
932
933 swrast_render_finish(ctx);
934 }