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