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