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