2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
6 * Copyright (C) 2009 VMware, Inc. All Rights Reserved.
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
15 * The above copyright notice and this permission notice shall be included
16 * in all copies or substantial portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
22 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
23 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
24 * OTHER DEALINGS IN THE SOFTWARE.
36 #include "glformats.h"
45 * Flip the order of the 2 bytes in each word in the given array.
48 * \param n number of words.
51 _mesa_swap2( GLushort
*p
, GLuint n
)
54 for (i
= 0; i
< n
; i
++) {
55 p
[i
] = (p
[i
] >> 8) | ((p
[i
] << 8) & 0xff00);
62 * Flip the order of the 4 bytes in each word in the given array.
65 _mesa_swap4( GLuint
*p
, GLuint n
)
68 for (i
= 0; i
< n
; i
++) {
72 | ((b
<< 8) & 0xff0000)
73 | ((b
<< 24) & 0xff000000);
80 * Return the byte offset of a specific pixel in an image (1D, 2D or 3D).
82 * Pixel unpacking/packing parameters are observed according to \p packing.
84 * \param dimensions either 1, 2 or 3 to indicate dimensionality of image
85 * \param packing the pixelstore attributes
86 * \param width the image width
87 * \param height the image height
88 * \param format the pixel format (must be validated beforehand)
89 * \param type the pixel data type (must be validated beforehand)
90 * \param img which image in the volume (0 for 1D or 2D images)
91 * \param row row of pixel in the image (0 for 1D images)
92 * \param column column of pixel in the image
94 * \return offset of pixel.
96 * \sa gl_pixelstore_attrib.
99 _mesa_image_offset( GLuint dimensions
,
100 const struct gl_pixelstore_attrib
*packing
,
101 GLsizei width
, GLsizei height
,
102 GLenum format
, GLenum type
,
103 GLint img
, GLint row
, GLint column
)
105 GLint alignment
; /* 1, 2 or 4 */
106 GLint pixels_per_row
;
107 GLint rows_per_image
;
110 GLint skipimages
; /* for 3-D volume images */
113 ASSERT(dimensions
>= 1 && dimensions
<= 3);
115 alignment
= packing
->Alignment
;
116 if (packing
->RowLength
> 0) {
117 pixels_per_row
= packing
->RowLength
;
120 pixels_per_row
= width
;
122 if (packing
->ImageHeight
> 0) {
123 rows_per_image
= packing
->ImageHeight
;
126 rows_per_image
= height
;
129 skippixels
= packing
->SkipPixels
;
130 /* Note: SKIP_ROWS _is_ used for 1D images */
131 skiprows
= packing
->SkipRows
;
132 /* Note: SKIP_IMAGES is only used for 3D images */
133 skipimages
= (dimensions
== 3) ? packing
->SkipImages
: 0;
135 if (type
== GL_BITMAP
) {
138 GLint bytes_per_image
;
139 /* components per pixel for color or stencil index: */
140 const GLint comp_per_pixel
= 1;
142 /* The pixel type and format should have been error checked earlier */
143 assert(format
== GL_COLOR_INDEX
|| format
== GL_STENCIL_INDEX
);
145 bytes_per_row
= alignment
146 * CEILING( comp_per_pixel
*pixels_per_row
, 8*alignment
);
148 bytes_per_image
= bytes_per_row
* rows_per_image
;
150 offset
= (skipimages
+ img
) * bytes_per_image
151 + (skiprows
+ row
) * bytes_per_row
152 + (skippixels
+ column
) / 8;
155 /* Non-BITMAP data */
156 GLint bytes_per_pixel
, bytes_per_row
, remainder
, bytes_per_image
;
159 bytes_per_pixel
= _mesa_bytes_per_pixel( format
, type
);
161 /* The pixel type and format should have been error checked earlier */
162 assert(bytes_per_pixel
> 0);
164 bytes_per_row
= pixels_per_row
* bytes_per_pixel
;
165 remainder
= bytes_per_row
% alignment
;
167 bytes_per_row
+= (alignment
- remainder
);
169 ASSERT(bytes_per_row
% alignment
== 0);
171 bytes_per_image
= bytes_per_row
* rows_per_image
;
173 if (packing
->Invert
) {
174 /* set pixel_addr to the last row */
175 topOfImage
= bytes_per_row
* (height
- 1);
176 bytes_per_row
= -bytes_per_row
;
182 /* compute final pixel address */
183 offset
= (skipimages
+ img
) * bytes_per_image
185 + (skiprows
+ row
) * bytes_per_row
186 + (skippixels
+ column
) * bytes_per_pixel
;
194 * Return the address of a specific pixel in an image (1D, 2D or 3D).
196 * Pixel unpacking/packing parameters are observed according to \p packing.
198 * \param dimensions either 1, 2 or 3 to indicate dimensionality of image
199 * \param packing the pixelstore attributes
200 * \param image starting address of image data
201 * \param width the image width
202 * \param height the image height
203 * \param format the pixel format (must be validated beforehand)
204 * \param type the pixel data type (must be validated beforehand)
205 * \param img which image in the volume (0 for 1D or 2D images)
206 * \param row row of pixel in the image (0 for 1D images)
207 * \param column column of pixel in the image
209 * \return address of pixel.
211 * \sa gl_pixelstore_attrib.
214 _mesa_image_address( GLuint dimensions
,
215 const struct gl_pixelstore_attrib
*packing
,
217 GLsizei width
, GLsizei height
,
218 GLenum format
, GLenum type
,
219 GLint img
, GLint row
, GLint column
)
221 const GLubyte
*addr
= (const GLubyte
*) image
;
223 addr
+= _mesa_image_offset(dimensions
, packing
, width
, height
,
224 format
, type
, img
, row
, column
);
226 return (GLvoid
*) addr
;
231 _mesa_image_address1d( const struct gl_pixelstore_attrib
*packing
,
234 GLenum format
, GLenum type
,
237 return _mesa_image_address(1, packing
, image
, width
, 1,
238 format
, type
, 0, 0, column
);
243 _mesa_image_address2d( const struct gl_pixelstore_attrib
*packing
,
245 GLsizei width
, GLsizei height
,
246 GLenum format
, GLenum type
,
247 GLint row
, GLint column
)
249 return _mesa_image_address(2, packing
, image
, width
, height
,
250 format
, type
, 0, row
, column
);
255 _mesa_image_address3d( const struct gl_pixelstore_attrib
*packing
,
257 GLsizei width
, GLsizei height
,
258 GLenum format
, GLenum type
,
259 GLint img
, GLint row
, GLint column
)
261 return _mesa_image_address(3, packing
, image
, width
, height
,
262 format
, type
, img
, row
, column
);
268 * Compute the stride (in bytes) between image rows.
270 * \param packing the pixelstore attributes
271 * \param width image width.
272 * \param format pixel format.
273 * \param type pixel data type.
275 * \return the stride in bytes for the given parameters, or -1 if error
278 _mesa_image_row_stride( const struct gl_pixelstore_attrib
*packing
,
279 GLint width
, GLenum format
, GLenum type
)
281 GLint bytesPerRow
, remainder
;
285 if (type
== GL_BITMAP
) {
286 if (packing
->RowLength
== 0) {
287 bytesPerRow
= (width
+ 7) / 8;
290 bytesPerRow
= (packing
->RowLength
+ 7) / 8;
294 /* Non-BITMAP data */
295 const GLint bytesPerPixel
= _mesa_bytes_per_pixel(format
, type
);
296 if (bytesPerPixel
<= 0)
297 return -1; /* error */
298 if (packing
->RowLength
== 0) {
299 bytesPerRow
= bytesPerPixel
* width
;
302 bytesPerRow
= bytesPerPixel
* packing
->RowLength
;
306 remainder
= bytesPerRow
% packing
->Alignment
;
308 bytesPerRow
+= (packing
->Alignment
- remainder
);
311 if (packing
->Invert
) {
312 /* negate the bytes per row (negative row stride) */
313 bytesPerRow
= -bytesPerRow
;
321 * Compute the stride between images in a 3D texture (in bytes) for the given
322 * pixel packing parameters and image width, format and type.
325 _mesa_image_image_stride( const struct gl_pixelstore_attrib
*packing
,
326 GLint width
, GLint height
,
327 GLenum format
, GLenum type
)
329 GLint bytesPerRow
, bytesPerImage
, remainder
;
333 if (type
== GL_BITMAP
) {
334 if (packing
->RowLength
== 0) {
335 bytesPerRow
= (width
+ 7) / 8;
338 bytesPerRow
= (packing
->RowLength
+ 7) / 8;
342 const GLint bytesPerPixel
= _mesa_bytes_per_pixel(format
, type
);
344 if (bytesPerPixel
<= 0)
345 return -1; /* error */
346 if (packing
->RowLength
== 0) {
347 bytesPerRow
= bytesPerPixel
* width
;
350 bytesPerRow
= bytesPerPixel
* packing
->RowLength
;
354 remainder
= bytesPerRow
% packing
->Alignment
;
356 bytesPerRow
+= (packing
->Alignment
- remainder
);
358 if (packing
->ImageHeight
== 0)
359 bytesPerImage
= bytesPerRow
* height
;
361 bytesPerImage
= bytesPerRow
* packing
->ImageHeight
;
363 return bytesPerImage
;
369 * "Expand" a bitmap from 1-bit per pixel to 8-bits per pixel.
370 * This is typically used to convert a bitmap into a GLubyte/pixel texture.
371 * "On" bits will set texels to \p onValue.
372 * "Off" bits will not modify texels.
373 * \param width src bitmap width in pixels
374 * \param height src bitmap height in pixels
375 * \param unpack bitmap unpacking state
376 * \param bitmap the src bitmap data
377 * \param destBuffer start of dest buffer
378 * \param destStride row stride in dest buffer
379 * \param onValue if bit is 1, set destBuffer pixel to this value
382 _mesa_expand_bitmap(GLsizei width
, GLsizei height
,
383 const struct gl_pixelstore_attrib
*unpack
,
384 const GLubyte
*bitmap
,
385 GLubyte
*destBuffer
, GLint destStride
,
388 const GLubyte
*srcRow
= (const GLubyte
*)
389 _mesa_image_address2d(unpack
, bitmap
, width
, height
,
390 GL_COLOR_INDEX
, GL_BITMAP
, 0, 0);
391 const GLint srcStride
= _mesa_image_row_stride(unpack
, width
,
392 GL_COLOR_INDEX
, GL_BITMAP
);
395 #define SET_PIXEL(COL, ROW) \
396 destBuffer[(ROW) * destStride + (COL)] = onValue;
398 for (row
= 0; row
< height
; row
++) {
399 const GLubyte
*src
= srcRow
;
401 if (unpack
->LsbFirst
) {
403 GLubyte mask
= 1U << (unpack
->SkipPixels
& 0x7);
404 for (col
= 0; col
< width
; col
++) {
419 /* get ready for next row */
425 GLubyte mask
= 128U >> (unpack
->SkipPixels
& 0x7);
426 for (col
= 0; col
< width
; col
++) {
441 /* get ready for next row */
456 * Convert an array of RGBA colors from one datatype to another.
457 * NOTE: src may equal dst. In that case, we use a temporary buffer.
460 _mesa_convert_colors(GLenum srcType
, const GLvoid
*src
,
461 GLenum dstType
, GLvoid
*dst
,
462 GLuint count
, const GLubyte mask
[])
465 const GLboolean useTemp
= (src
== dst
);
467 tempBuffer
= malloc(count
* MAX_PIXEL_BYTES
);
471 ASSERT(srcType
!= dstType
);
474 case GL_UNSIGNED_BYTE
:
475 if (dstType
== GL_UNSIGNED_SHORT
) {
476 const GLubyte (*src1
)[4] = (const GLubyte (*)[4]) src
;
477 GLushort (*dst2
)[4] = (GLushort (*)[4]) (useTemp
? tempBuffer
: dst
);
479 for (i
= 0; i
< count
; i
++) {
480 if (!mask
|| mask
[i
]) {
481 dst2
[i
][RCOMP
] = UBYTE_TO_USHORT(src1
[i
][RCOMP
]);
482 dst2
[i
][GCOMP
] = UBYTE_TO_USHORT(src1
[i
][GCOMP
]);
483 dst2
[i
][BCOMP
] = UBYTE_TO_USHORT(src1
[i
][BCOMP
]);
484 dst2
[i
][ACOMP
] = UBYTE_TO_USHORT(src1
[i
][ACOMP
]);
488 memcpy(dst
, tempBuffer
, count
* 4 * sizeof(GLushort
));
491 const GLubyte (*src1
)[4] = (const GLubyte (*)[4]) src
;
492 GLfloat (*dst4
)[4] = (GLfloat (*)[4]) (useTemp
? tempBuffer
: dst
);
494 ASSERT(dstType
== GL_FLOAT
);
495 for (i
= 0; i
< count
; i
++) {
496 if (!mask
|| mask
[i
]) {
497 dst4
[i
][RCOMP
] = UBYTE_TO_FLOAT(src1
[i
][RCOMP
]);
498 dst4
[i
][GCOMP
] = UBYTE_TO_FLOAT(src1
[i
][GCOMP
]);
499 dst4
[i
][BCOMP
] = UBYTE_TO_FLOAT(src1
[i
][BCOMP
]);
500 dst4
[i
][ACOMP
] = UBYTE_TO_FLOAT(src1
[i
][ACOMP
]);
504 memcpy(dst
, tempBuffer
, count
* 4 * sizeof(GLfloat
));
507 case GL_UNSIGNED_SHORT
:
508 if (dstType
== GL_UNSIGNED_BYTE
) {
509 const GLushort (*src2
)[4] = (const GLushort (*)[4]) src
;
510 GLubyte (*dst1
)[4] = (GLubyte (*)[4]) (useTemp
? tempBuffer
: dst
);
512 for (i
= 0; i
< count
; i
++) {
513 if (!mask
|| mask
[i
]) {
514 dst1
[i
][RCOMP
] = USHORT_TO_UBYTE(src2
[i
][RCOMP
]);
515 dst1
[i
][GCOMP
] = USHORT_TO_UBYTE(src2
[i
][GCOMP
]);
516 dst1
[i
][BCOMP
] = USHORT_TO_UBYTE(src2
[i
][BCOMP
]);
517 dst1
[i
][ACOMP
] = USHORT_TO_UBYTE(src2
[i
][ACOMP
]);
521 memcpy(dst
, tempBuffer
, count
* 4 * sizeof(GLubyte
));
524 const GLushort (*src2
)[4] = (const GLushort (*)[4]) src
;
525 GLfloat (*dst4
)[4] = (GLfloat (*)[4]) (useTemp
? tempBuffer
: dst
);
527 ASSERT(dstType
== GL_FLOAT
);
528 for (i
= 0; i
< count
; i
++) {
529 if (!mask
|| mask
[i
]) {
530 dst4
[i
][RCOMP
] = USHORT_TO_FLOAT(src2
[i
][RCOMP
]);
531 dst4
[i
][GCOMP
] = USHORT_TO_FLOAT(src2
[i
][GCOMP
]);
532 dst4
[i
][BCOMP
] = USHORT_TO_FLOAT(src2
[i
][BCOMP
]);
533 dst4
[i
][ACOMP
] = USHORT_TO_FLOAT(src2
[i
][ACOMP
]);
537 memcpy(dst
, tempBuffer
, count
* 4 * sizeof(GLfloat
));
541 if (dstType
== GL_UNSIGNED_BYTE
) {
542 const GLfloat (*src4
)[4] = (const GLfloat (*)[4]) src
;
543 GLubyte (*dst1
)[4] = (GLubyte (*)[4]) (useTemp
? tempBuffer
: dst
);
545 for (i
= 0; i
< count
; i
++) {
546 if (!mask
|| mask
[i
])
547 _mesa_unclamped_float_rgba_to_ubyte(dst1
[i
], src4
[i
]);
550 memcpy(dst
, tempBuffer
, count
* 4 * sizeof(GLubyte
));
553 const GLfloat (*src4
)[4] = (const GLfloat (*)[4]) src
;
554 GLushort (*dst2
)[4] = (GLushort (*)[4]) (useTemp
? tempBuffer
: dst
);
556 ASSERT(dstType
== GL_UNSIGNED_SHORT
);
557 for (i
= 0; i
< count
; i
++) {
558 if (!mask
|| mask
[i
]) {
559 UNCLAMPED_FLOAT_TO_USHORT(dst2
[i
][RCOMP
], src4
[i
][RCOMP
]);
560 UNCLAMPED_FLOAT_TO_USHORT(dst2
[i
][GCOMP
], src4
[i
][GCOMP
]);
561 UNCLAMPED_FLOAT_TO_USHORT(dst2
[i
][BCOMP
], src4
[i
][BCOMP
]);
562 UNCLAMPED_FLOAT_TO_USHORT(dst2
[i
][ACOMP
], src4
[i
][ACOMP
]);
566 memcpy(dst
, tempBuffer
, count
* 4 * sizeof(GLushort
));
570 _mesa_problem(NULL
, "Invalid datatype in _mesa_convert_colors");
580 * Perform basic clipping for glDrawPixels. The image's position and size
581 * and the unpack SkipPixels and SkipRows are adjusted so that the image
582 * region is entirely within the window and scissor bounds.
583 * NOTE: this will only work when glPixelZoom is (1, 1) or (1, -1).
584 * If Pixel.ZoomY is -1, *destY will be changed to be the first row which
585 * we'll actually write. Beforehand, *destY-1 is the first drawing row.
587 * \return GL_TRUE if image is ready for drawing or
588 * GL_FALSE if image was completely clipped away (draw nothing)
591 _mesa_clip_drawpixels(const struct gl_context
*ctx
,
592 GLint
*destX
, GLint
*destY
,
593 GLsizei
*width
, GLsizei
*height
,
594 struct gl_pixelstore_attrib
*unpack
)
596 const struct gl_framebuffer
*buffer
= ctx
->DrawBuffer
;
598 if (unpack
->RowLength
== 0) {
599 unpack
->RowLength
= *width
;
602 ASSERT(ctx
->Pixel
.ZoomX
== 1.0F
);
603 ASSERT(ctx
->Pixel
.ZoomY
== 1.0F
|| ctx
->Pixel
.ZoomY
== -1.0F
);
606 if (*destX
< buffer
->_Xmin
) {
607 unpack
->SkipPixels
+= (buffer
->_Xmin
- *destX
);
608 *width
-= (buffer
->_Xmin
- *destX
);
609 *destX
= buffer
->_Xmin
;
612 if (*destX
+ *width
> buffer
->_Xmax
)
613 *width
-= (*destX
+ *width
- buffer
->_Xmax
);
618 if (ctx
->Pixel
.ZoomY
== 1.0F
) {
619 /* bottom clipping */
620 if (*destY
< buffer
->_Ymin
) {
621 unpack
->SkipRows
+= (buffer
->_Ymin
- *destY
);
622 *height
-= (buffer
->_Ymin
- *destY
);
623 *destY
= buffer
->_Ymin
;
626 if (*destY
+ *height
> buffer
->_Ymax
)
627 *height
-= (*destY
+ *height
- buffer
->_Ymax
);
629 else { /* upside down */
631 if (*destY
> buffer
->_Ymax
) {
632 unpack
->SkipRows
+= (*destY
- buffer
->_Ymax
);
633 *height
-= (*destY
- buffer
->_Ymax
);
634 *destY
= buffer
->_Ymax
;
636 /* bottom clipping */
637 if (*destY
- *height
< buffer
->_Ymin
)
638 *height
-= (buffer
->_Ymin
- (*destY
- *height
));
639 /* adjust destY so it's the first row to write to */
651 * Perform clipping for glReadPixels. The image's window position
652 * and size, and the pack skipPixels, skipRows and rowLength are adjusted
653 * so that the image region is entirely within the window bounds.
654 * Note: this is different from _mesa_clip_drawpixels() in that the
655 * scissor box is ignored, and we use the bounds of the current readbuffer
658 * \return GL_TRUE if region to read is in bounds
659 * GL_FALSE if region is completely out of bounds (nothing to read)
662 _mesa_clip_readpixels(const struct gl_context
*ctx
,
663 GLint
*srcX
, GLint
*srcY
,
664 GLsizei
*width
, GLsizei
*height
,
665 struct gl_pixelstore_attrib
*pack
)
667 const struct gl_framebuffer
*buffer
= ctx
->ReadBuffer
;
669 if (pack
->RowLength
== 0) {
670 pack
->RowLength
= *width
;
675 pack
->SkipPixels
+= (0 - *srcX
);
676 *width
-= (0 - *srcX
);
680 if (*srcX
+ *width
> (GLsizei
) buffer
->Width
)
681 *width
-= (*srcX
+ *width
- buffer
->Width
);
686 /* bottom clipping */
688 pack
->SkipRows
+= (0 - *srcY
);
689 *height
-= (0 - *srcY
);
693 if (*srcY
+ *height
> (GLsizei
) buffer
->Height
)
694 *height
-= (*srcY
+ *height
- buffer
->Height
);
704 * Do clipping for a glCopyTexSubImage call.
705 * The framebuffer source region might extend outside the framebuffer
706 * bounds. Clip the source region against the framebuffer bounds and
707 * adjust the texture/dest position and size accordingly.
709 * \return GL_FALSE if region is totally clipped, GL_TRUE otherwise.
712 _mesa_clip_copytexsubimage(const struct gl_context
*ctx
,
713 GLint
*destX
, GLint
*destY
,
714 GLint
*srcX
, GLint
*srcY
,
715 GLsizei
*width
, GLsizei
*height
)
717 const struct gl_framebuffer
*fb
= ctx
->ReadBuffer
;
718 const GLint srcX0
= *srcX
, srcY0
= *srcY
;
720 if (_mesa_clip_to_region(0, 0, fb
->Width
, fb
->Height
,
721 srcX
, srcY
, width
, height
)) {
722 *destX
= *destX
+ *srcX
- srcX0
;
723 *destY
= *destY
+ *srcY
- srcY0
;
735 * Clip the rectangle defined by (x, y, width, height) against the bounds
736 * specified by [xmin, xmax) and [ymin, ymax).
737 * \return GL_FALSE if rect is totally clipped, GL_TRUE otherwise.
740 _mesa_clip_to_region(GLint xmin
, GLint ymin
,
741 GLint xmax
, GLint ymax
,
743 GLsizei
*width
, GLsizei
*height
)
747 *width
-= (xmin
- *x
);
752 if (*x
+ *width
> xmax
)
753 *width
-= (*x
+ *width
- xmax
);
758 /* bottom (or top) clipping */
760 *height
-= (ymin
- *y
);
764 /* top (or bottom) clipping */
765 if (*y
+ *height
> ymax
)
766 *height
-= (*y
+ *height
- ymax
);
776 * Clip dst coords against Xmax (or Ymax).
779 clip_right_or_top(GLint
*srcX0
, GLint
*srcX1
,
780 GLint
*dstX0
, GLint
*dstX1
,
785 if (*dstX1
> maxValue
) {
786 /* X1 outside right edge */
787 ASSERT(*dstX0
< maxValue
); /* X0 should be inside right edge */
788 t
= (GLfloat
) (maxValue
- *dstX0
) / (GLfloat
) (*dstX1
- *dstX0
);
789 /* chop off [t, 1] part */
790 ASSERT(t
>= 0.0 && t
<= 1.0);
792 bias
= (*srcX0
< *srcX1
) ? 0.5F
: -0.5F
;
793 *srcX1
= *srcX0
+ (GLint
) (t
* (*srcX1
- *srcX0
) + bias
);
795 else if (*dstX0
> maxValue
) {
796 /* X0 outside right edge */
797 ASSERT(*dstX1
< maxValue
); /* X1 should be inside right edge */
798 t
= (GLfloat
) (maxValue
- *dstX1
) / (GLfloat
) (*dstX0
- *dstX1
);
799 /* chop off [t, 1] part */
800 ASSERT(t
>= 0.0 && t
<= 1.0);
802 bias
= (*srcX0
< *srcX1
) ? -0.5F
: 0.5F
;
803 *srcX0
= *srcX1
+ (GLint
) (t
* (*srcX0
- *srcX1
) + bias
);
809 * Clip dst coords against Xmin (or Ymin).
812 clip_left_or_bottom(GLint
*srcX0
, GLint
*srcX1
,
813 GLint
*dstX0
, GLint
*dstX1
,
818 if (*dstX0
< minValue
) {
819 /* X0 outside left edge */
820 ASSERT(*dstX1
> minValue
); /* X1 should be inside left edge */
821 t
= (GLfloat
) (minValue
- *dstX0
) / (GLfloat
) (*dstX1
- *dstX0
);
822 /* chop off [0, t] part */
823 ASSERT(t
>= 0.0 && t
<= 1.0);
825 bias
= (*srcX0
< *srcX1
) ? 0.5F
: -0.5F
;
826 *srcX0
= *srcX0
+ (GLint
) (t
* (*srcX1
- *srcX0
) + bias
);
828 else if (*dstX1
< minValue
) {
829 /* X1 outside left edge */
830 ASSERT(*dstX0
> minValue
); /* X0 should be inside left edge */
831 t
= (GLfloat
) (minValue
- *dstX1
) / (GLfloat
) (*dstX0
- *dstX1
);
832 /* chop off [0, t] part */
833 ASSERT(t
>= 0.0 && t
<= 1.0);
835 bias
= (*srcX0
< *srcX1
) ? -0.5F
: 0.5F
;
836 *srcX1
= *srcX1
+ (GLint
) (t
* (*srcX0
- *srcX1
) + bias
);
842 * Do clipping of blit src/dest rectangles.
843 * The dest rect is clipped against both the buffer bounds and scissor bounds.
844 * The src rect is just clipped against the buffer bounds.
846 * When either the src or dest rect is clipped, the other is also clipped
849 * Note that X0 need not be less than X1 (same for Y) for either the source
850 * and dest rects. That makes the clipping a little trickier.
852 * \return GL_TRUE if anything is left to draw, GL_FALSE if totally clipped
855 _mesa_clip_blit(struct gl_context
*ctx
,
856 GLint
*srcX0
, GLint
*srcY0
, GLint
*srcX1
, GLint
*srcY1
,
857 GLint
*dstX0
, GLint
*dstY0
, GLint
*dstX1
, GLint
*dstY1
)
859 const GLint srcXmin
= 0;
860 const GLint srcXmax
= ctx
->ReadBuffer
->Width
;
861 const GLint srcYmin
= 0;
862 const GLint srcYmax
= ctx
->ReadBuffer
->Height
;
864 /* these include scissor bounds */
865 const GLint dstXmin
= ctx
->DrawBuffer
->_Xmin
;
866 const GLint dstXmax
= ctx
->DrawBuffer
->_Xmax
;
867 const GLint dstYmin
= ctx
->DrawBuffer
->_Ymin
;
868 const GLint dstYmax
= ctx
->DrawBuffer
->_Ymax
;
871 printf("PreClipX: src: %d .. %d dst: %d .. %d\n",
872 *srcX0, *srcX1, *dstX0, *dstX1);
873 printf("PreClipY: src: %d .. %d dst: %d .. %d\n",
874 *srcY0, *srcY1, *dstY0, *dstY1);
877 /* trivial rejection tests */
878 if (*dstX0
== *dstX1
)
879 return GL_FALSE
; /* no width */
880 if (*dstX0
<= dstXmin
&& *dstX1
<= dstXmin
)
881 return GL_FALSE
; /* totally out (left) of bounds */
882 if (*dstX0
>= dstXmax
&& *dstX1
>= dstXmax
)
883 return GL_FALSE
; /* totally out (right) of bounds */
885 if (*dstY0
== *dstY1
)
887 if (*dstY0
<= dstYmin
&& *dstY1
<= dstYmin
)
889 if (*dstY0
>= dstYmax
&& *dstY1
>= dstYmax
)
892 if (*srcX0
== *srcX1
)
894 if (*srcX0
<= srcXmin
&& *srcX1
<= srcXmin
)
896 if (*srcX0
>= srcXmax
&& *srcX1
>= srcXmax
)
899 if (*srcY0
== *srcY1
)
901 if (*srcY0
<= srcYmin
&& *srcY1
<= srcYmin
)
903 if (*srcY0
>= srcYmax
&& *srcY1
>= srcYmax
)
909 clip_right_or_top(srcX0
, srcX1
, dstX0
, dstX1
, dstXmax
);
910 clip_right_or_top(srcY0
, srcY1
, dstY0
, dstY1
, dstYmax
);
911 clip_left_or_bottom(srcX0
, srcX1
, dstX0
, dstX1
, dstXmin
);
912 clip_left_or_bottom(srcY0
, srcY1
, dstY0
, dstY1
, dstYmin
);
915 * src clip (just swap src/dst values from above)
917 clip_right_or_top(dstX0
, dstX1
, srcX0
, srcX1
, srcXmax
);
918 clip_right_or_top(dstY0
, dstY1
, srcY0
, srcY1
, srcYmax
);
919 clip_left_or_bottom(dstX0
, dstX1
, srcX0
, srcX1
, srcXmin
);
920 clip_left_or_bottom(dstY0
, dstY1
, srcY0
, srcY1
, srcYmin
);
923 printf("PostClipX: src: %d .. %d dst: %d .. %d\n",
924 *srcX0, *srcX1, *dstX0, *dstX1);
925 printf("PostClipY: src: %d .. %d dst: %d .. %d\n",
926 *srcY0, *srcY1, *dstY0, *dstY1);
929 ASSERT(*dstX0
>= dstXmin
);
930 ASSERT(*dstX0
<= dstXmax
);
931 ASSERT(*dstX1
>= dstXmin
);
932 ASSERT(*dstX1
<= dstXmax
);
934 ASSERT(*dstY0
>= dstYmin
);
935 ASSERT(*dstY0
<= dstYmax
);
936 ASSERT(*dstY1
>= dstYmin
);
937 ASSERT(*dstY1
<= dstYmax
);
939 ASSERT(*srcX0
>= srcXmin
);
940 ASSERT(*srcX0
<= srcXmax
);
941 ASSERT(*srcX1
>= srcXmin
);
942 ASSERT(*srcX1
<= srcXmax
);
944 ASSERT(*srcY0
>= srcYmin
);
945 ASSERT(*srcY0
<= srcYmax
);
946 ASSERT(*srcY1
>= srcYmin
);
947 ASSERT(*srcY1
<= srcYmax
);