2 * Mesa 3-D graphics library
4 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
5 * Copyright (C) 2009 VMware, Inc. All Rights Reserved.
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:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
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 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
21 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 * OTHER DEALINGS IN THE SOFTWARE.
35 #include "glformats.h"
44 * Flip the order of the 2 bytes in each word in the given array (src) and
45 * store the result in another array (dst). For in-place byte-swapping this
46 * function can be called with the same array for src and dst.
48 * \param dst the array where byte-swapped data will be stored.
49 * \param src the array with the source data we want to byte-swap.
50 * \param n number of words.
53 swap2_copy( GLushort
*dst
, GLushort
*src
, GLuint n
)
56 for (i
= 0; i
< n
; i
++) {
57 dst
[i
] = (src
[i
] >> 8) | ((src
[i
] << 8) & 0xff00);
62 _mesa_swap2(GLushort
*p
, GLuint n
)
68 * Flip the order of the 4 bytes in each word in the given array (src) and
69 * store the result in another array (dst). For in-place byte-swapping this
70 * function can be called with the same array for src and dst.
72 * \param dst the array where byte-swapped data will be stored.
73 * \param src the array with the source data we want to byte-swap.
74 * \param n number of words.
77 swap4_copy( GLuint
*dst
, GLuint
*src
, GLuint n
)
80 for (i
= 0; i
< n
; i
++) {
84 | ((b
<< 8) & 0xff0000)
85 | ((b
<< 24) & 0xff000000);
91 _mesa_swap4(GLuint
*p
, GLuint n
)
97 * Return the byte offset of a specific pixel in an image (1D, 2D or 3D).
99 * Pixel unpacking/packing parameters are observed according to \p packing.
101 * \param dimensions either 1, 2 or 3 to indicate dimensionality of image
102 * \param packing the pixelstore attributes
103 * \param width the image width
104 * \param height the image height
105 * \param format the pixel format (must be validated beforehand)
106 * \param type the pixel data type (must be validated beforehand)
107 * \param img which image in the volume (0 for 1D or 2D images)
108 * \param row row of pixel in the image (0 for 1D images)
109 * \param column column of pixel in the image
111 * \return offset of pixel.
113 * \sa gl_pixelstore_attrib.
116 _mesa_image_offset( GLuint dimensions
,
117 const struct gl_pixelstore_attrib
*packing
,
118 GLsizei width
, GLsizei height
,
119 GLenum format
, GLenum type
,
120 GLint img
, GLint row
, GLint column
)
122 GLint alignment
; /* 1, 2 or 4 */
123 GLint pixels_per_row
;
124 GLint rows_per_image
;
127 GLint skipimages
; /* for 3-D volume images */
130 assert(dimensions
>= 1 && dimensions
<= 3);
132 alignment
= packing
->Alignment
;
133 if (packing
->RowLength
> 0) {
134 pixels_per_row
= packing
->RowLength
;
137 pixels_per_row
= width
;
139 if (packing
->ImageHeight
> 0) {
140 rows_per_image
= packing
->ImageHeight
;
143 rows_per_image
= height
;
146 skippixels
= packing
->SkipPixels
;
147 /* Note: SKIP_ROWS _is_ used for 1D images */
148 skiprows
= packing
->SkipRows
;
149 /* Note: SKIP_IMAGES is only used for 3D images */
150 skipimages
= (dimensions
== 3) ? packing
->SkipImages
: 0;
152 if (type
== GL_BITMAP
) {
154 GLintptr bytes_per_row
;
155 GLintptr bytes_per_image
;
156 /* components per pixel for color or stencil index: */
157 const GLint comp_per_pixel
= 1;
159 /* The pixel type and format should have been error checked earlier */
160 assert(format
== GL_COLOR_INDEX
|| format
== GL_STENCIL_INDEX
);
162 bytes_per_row
= alignment
163 * DIV_ROUND_UP( comp_per_pixel
*pixels_per_row
, 8*alignment
);
165 bytes_per_image
= bytes_per_row
* rows_per_image
;
167 offset
= (skipimages
+ img
) * bytes_per_image
168 + (skiprows
+ row
) * bytes_per_row
169 + (skippixels
+ column
) / 8;
172 /* Non-BITMAP data */
173 GLintptr bytes_per_pixel
, bytes_per_row
, remainder
, bytes_per_image
;
176 bytes_per_pixel
= _mesa_bytes_per_pixel( format
, type
);
178 /* The pixel type and format should have been error checked earlier */
179 assert(bytes_per_pixel
> 0);
181 bytes_per_row
= pixels_per_row
* bytes_per_pixel
;
182 remainder
= bytes_per_row
% alignment
;
184 bytes_per_row
+= (alignment
- remainder
);
186 assert(bytes_per_row
% alignment
== 0);
188 bytes_per_image
= bytes_per_row
* rows_per_image
;
190 if (packing
->Invert
) {
191 /* set pixel_addr to the last row */
192 topOfImage
= bytes_per_row
* (height
- 1);
193 bytes_per_row
= -bytes_per_row
;
199 /* compute final pixel address */
200 offset
= (skipimages
+ img
) * bytes_per_image
202 + (skiprows
+ row
) * bytes_per_row
203 + (skippixels
+ column
) * bytes_per_pixel
;
211 * Return the address of a specific pixel in an image (1D, 2D or 3D).
213 * Pixel unpacking/packing parameters are observed according to \p packing.
215 * \param dimensions either 1, 2 or 3 to indicate dimensionality of image
216 * \param packing the pixelstore attributes
217 * \param image starting address of image data
218 * \param width the image width
219 * \param height the image height
220 * \param format the pixel format (must be validated beforehand)
221 * \param type the pixel data type (must be validated beforehand)
222 * \param img which image in the volume (0 for 1D or 2D images)
223 * \param row row of pixel in the image (0 for 1D images)
224 * \param column column of pixel in the image
226 * \return address of pixel.
228 * \sa gl_pixelstore_attrib.
231 _mesa_image_address( GLuint dimensions
,
232 const struct gl_pixelstore_attrib
*packing
,
234 GLsizei width
, GLsizei height
,
235 GLenum format
, GLenum type
,
236 GLint img
, GLint row
, GLint column
)
238 const GLubyte
*addr
= (const GLubyte
*) image
;
240 addr
+= _mesa_image_offset(dimensions
, packing
, width
, height
,
241 format
, type
, img
, row
, column
);
243 return (GLvoid
*) addr
;
248 _mesa_image_address1d( const struct gl_pixelstore_attrib
*packing
,
251 GLenum format
, GLenum type
,
254 return _mesa_image_address(1, packing
, image
, width
, 1,
255 format
, type
, 0, 0, column
);
260 _mesa_image_address2d( const struct gl_pixelstore_attrib
*packing
,
262 GLsizei width
, GLsizei height
,
263 GLenum format
, GLenum type
,
264 GLint row
, GLint column
)
266 return _mesa_image_address(2, packing
, image
, width
, height
,
267 format
, type
, 0, row
, column
);
272 _mesa_image_address3d( const struct gl_pixelstore_attrib
*packing
,
274 GLsizei width
, GLsizei height
,
275 GLenum format
, GLenum type
,
276 GLint img
, GLint row
, GLint column
)
278 return _mesa_image_address(3, packing
, image
, width
, height
,
279 format
, type
, img
, row
, column
);
285 * Compute the stride (in bytes) between image rows.
287 * \param packing the pixelstore attributes
288 * \param width image width.
289 * \param format pixel format.
290 * \param type pixel data type.
292 * \return the stride in bytes for the given parameters, or -1 if error
295 _mesa_image_row_stride( const struct gl_pixelstore_attrib
*packing
,
296 GLint width
, GLenum format
, GLenum type
)
298 GLint bytesPerRow
, remainder
;
302 if (type
== GL_BITMAP
) {
303 if (packing
->RowLength
== 0) {
304 bytesPerRow
= (width
+ 7) / 8;
307 bytesPerRow
= (packing
->RowLength
+ 7) / 8;
311 /* Non-BITMAP data */
312 const GLint bytesPerPixel
= _mesa_bytes_per_pixel(format
, type
);
313 if (bytesPerPixel
<= 0)
314 return -1; /* error */
315 if (packing
->RowLength
== 0) {
316 bytesPerRow
= bytesPerPixel
* width
;
319 bytesPerRow
= bytesPerPixel
* packing
->RowLength
;
323 remainder
= bytesPerRow
% packing
->Alignment
;
325 bytesPerRow
+= (packing
->Alignment
- remainder
);
328 if (packing
->Invert
) {
329 /* negate the bytes per row (negative row stride) */
330 bytesPerRow
= -bytesPerRow
;
338 * Compute the stride between images in a 3D texture (in bytes) for the given
339 * pixel packing parameters and image width, format and type.
342 _mesa_image_image_stride( const struct gl_pixelstore_attrib
*packing
,
343 GLint width
, GLint height
,
344 GLenum format
, GLenum type
)
346 GLint bytesPerRow
, bytesPerImage
, remainder
;
350 if (type
== GL_BITMAP
) {
351 if (packing
->RowLength
== 0) {
352 bytesPerRow
= (width
+ 7) / 8;
355 bytesPerRow
= (packing
->RowLength
+ 7) / 8;
359 const GLint bytesPerPixel
= _mesa_bytes_per_pixel(format
, type
);
361 if (bytesPerPixel
<= 0)
362 return -1; /* error */
363 if (packing
->RowLength
== 0) {
364 bytesPerRow
= bytesPerPixel
* width
;
367 bytesPerRow
= bytesPerPixel
* packing
->RowLength
;
371 remainder
= bytesPerRow
% packing
->Alignment
;
373 bytesPerRow
+= (packing
->Alignment
- remainder
);
375 if (packing
->ImageHeight
== 0)
376 bytesPerImage
= bytesPerRow
* height
;
378 bytesPerImage
= bytesPerRow
* packing
->ImageHeight
;
380 return bytesPerImage
;
386 * "Expand" a bitmap from 1-bit per pixel to 8-bits per pixel.
387 * This is typically used to convert a bitmap into a GLubyte/pixel texture.
388 * "On" bits will set texels to \p onValue.
389 * "Off" bits will not modify texels.
390 * \param width src bitmap width in pixels
391 * \param height src bitmap height in pixels
392 * \param unpack bitmap unpacking state
393 * \param bitmap the src bitmap data
394 * \param destBuffer start of dest buffer
395 * \param destStride row stride in dest buffer
396 * \param onValue if bit is 1, set destBuffer pixel to this value
399 _mesa_expand_bitmap(GLsizei width
, GLsizei height
,
400 const struct gl_pixelstore_attrib
*unpack
,
401 const GLubyte
*bitmap
,
402 GLubyte
*destBuffer
, GLint destStride
,
405 const GLubyte
*srcRow
= (const GLubyte
*)
406 _mesa_image_address2d(unpack
, bitmap
, width
, height
,
407 GL_COLOR_INDEX
, GL_BITMAP
, 0, 0);
408 const GLint srcStride
= _mesa_image_row_stride(unpack
, width
,
409 GL_COLOR_INDEX
, GL_BITMAP
);
411 GLubyte
*dstRow
= destBuffer
;
413 for (row
= 0; row
< height
; row
++) {
414 const GLubyte
*src
= srcRow
;
416 if (unpack
->LsbFirst
) {
418 GLubyte mask
= 1U << (unpack
->SkipPixels
& 0x7);
419 for (col
= 0; col
< width
; col
++) {
422 dstRow
[col
] = onValue
;
434 /* get ready for next row */
440 GLubyte mask
= 128U >> (unpack
->SkipPixels
& 0x7);
441 for (col
= 0; col
< width
; col
++) {
444 dstRow
[col
] = onValue
;
456 /* get ready for next row */
462 dstRow
+= destStride
;
470 * Convert an array of RGBA colors from one datatype to another.
471 * NOTE: src may equal dst. In that case, we use a temporary buffer.
474 _mesa_convert_colors(GLenum srcType
, const GLvoid
*src
,
475 GLenum dstType
, GLvoid
*dst
,
476 GLuint count
, const GLubyte mask
[])
479 const GLboolean useTemp
= (src
== dst
);
481 tempBuffer
= malloc(count
* MAX_PIXEL_BYTES
);
485 assert(srcType
!= dstType
);
488 case GL_UNSIGNED_BYTE
:
489 if (dstType
== GL_UNSIGNED_SHORT
) {
490 const GLubyte (*src1
)[4] = (const GLubyte (*)[4]) src
;
491 GLushort (*dst2
)[4] = (GLushort (*)[4]) (useTemp
? tempBuffer
: dst
);
493 for (i
= 0; i
< count
; i
++) {
494 if (!mask
|| mask
[i
]) {
495 dst2
[i
][RCOMP
] = UBYTE_TO_USHORT(src1
[i
][RCOMP
]);
496 dst2
[i
][GCOMP
] = UBYTE_TO_USHORT(src1
[i
][GCOMP
]);
497 dst2
[i
][BCOMP
] = UBYTE_TO_USHORT(src1
[i
][BCOMP
]);
498 dst2
[i
][ACOMP
] = UBYTE_TO_USHORT(src1
[i
][ACOMP
]);
502 memcpy(dst
, tempBuffer
, count
* 4 * sizeof(GLushort
));
505 const GLubyte (*src1
)[4] = (const GLubyte (*)[4]) src
;
506 GLfloat (*dst4
)[4] = (GLfloat (*)[4]) (useTemp
? tempBuffer
: dst
);
508 assert(dstType
== GL_FLOAT
);
509 for (i
= 0; i
< count
; i
++) {
510 if (!mask
|| mask
[i
]) {
511 dst4
[i
][RCOMP
] = UBYTE_TO_FLOAT(src1
[i
][RCOMP
]);
512 dst4
[i
][GCOMP
] = UBYTE_TO_FLOAT(src1
[i
][GCOMP
]);
513 dst4
[i
][BCOMP
] = UBYTE_TO_FLOAT(src1
[i
][BCOMP
]);
514 dst4
[i
][ACOMP
] = UBYTE_TO_FLOAT(src1
[i
][ACOMP
]);
518 memcpy(dst
, tempBuffer
, count
* 4 * sizeof(GLfloat
));
521 case GL_UNSIGNED_SHORT
:
522 if (dstType
== GL_UNSIGNED_BYTE
) {
523 const GLushort (*src2
)[4] = (const GLushort (*)[4]) src
;
524 GLubyte (*dst1
)[4] = (GLubyte (*)[4]) (useTemp
? tempBuffer
: dst
);
526 for (i
= 0; i
< count
; i
++) {
527 if (!mask
|| mask
[i
]) {
528 dst1
[i
][RCOMP
] = USHORT_TO_UBYTE(src2
[i
][RCOMP
]);
529 dst1
[i
][GCOMP
] = USHORT_TO_UBYTE(src2
[i
][GCOMP
]);
530 dst1
[i
][BCOMP
] = USHORT_TO_UBYTE(src2
[i
][BCOMP
]);
531 dst1
[i
][ACOMP
] = USHORT_TO_UBYTE(src2
[i
][ACOMP
]);
535 memcpy(dst
, tempBuffer
, count
* 4 * sizeof(GLubyte
));
538 const GLushort (*src2
)[4] = (const GLushort (*)[4]) src
;
539 GLfloat (*dst4
)[4] = (GLfloat (*)[4]) (useTemp
? tempBuffer
: dst
);
541 assert(dstType
== GL_FLOAT
);
542 for (i
= 0; i
< count
; i
++) {
543 if (!mask
|| mask
[i
]) {
544 dst4
[i
][RCOMP
] = USHORT_TO_FLOAT(src2
[i
][RCOMP
]);
545 dst4
[i
][GCOMP
] = USHORT_TO_FLOAT(src2
[i
][GCOMP
]);
546 dst4
[i
][BCOMP
] = USHORT_TO_FLOAT(src2
[i
][BCOMP
]);
547 dst4
[i
][ACOMP
] = USHORT_TO_FLOAT(src2
[i
][ACOMP
]);
551 memcpy(dst
, tempBuffer
, count
* 4 * sizeof(GLfloat
));
555 if (dstType
== GL_UNSIGNED_BYTE
) {
556 const GLfloat (*src4
)[4] = (const GLfloat (*)[4]) src
;
557 GLubyte (*dst1
)[4] = (GLubyte (*)[4]) (useTemp
? tempBuffer
: dst
);
559 for (i
= 0; i
< count
; i
++) {
560 if (!mask
|| mask
[i
])
561 _mesa_unclamped_float_rgba_to_ubyte(dst1
[i
], src4
[i
]);
564 memcpy(dst
, tempBuffer
, count
* 4 * sizeof(GLubyte
));
567 const GLfloat (*src4
)[4] = (const GLfloat (*)[4]) src
;
568 GLushort (*dst2
)[4] = (GLushort (*)[4]) (useTemp
? tempBuffer
: dst
);
570 assert(dstType
== GL_UNSIGNED_SHORT
);
571 for (i
= 0; i
< count
; i
++) {
572 if (!mask
|| mask
[i
]) {
573 UNCLAMPED_FLOAT_TO_USHORT(dst2
[i
][RCOMP
], src4
[i
][RCOMP
]);
574 UNCLAMPED_FLOAT_TO_USHORT(dst2
[i
][GCOMP
], src4
[i
][GCOMP
]);
575 UNCLAMPED_FLOAT_TO_USHORT(dst2
[i
][BCOMP
], src4
[i
][BCOMP
]);
576 UNCLAMPED_FLOAT_TO_USHORT(dst2
[i
][ACOMP
], src4
[i
][ACOMP
]);
580 memcpy(dst
, tempBuffer
, count
* 4 * sizeof(GLushort
));
584 unreachable("Invalid datatype in _mesa_convert_colors");
594 * Perform basic clipping for glDrawPixels. The image's position and size
595 * and the unpack SkipPixels and SkipRows are adjusted so that the image
596 * region is entirely within the window and scissor bounds.
597 * NOTE: this will only work when glPixelZoom is (1, 1) or (1, -1).
598 * If Pixel.ZoomY is -1, *destY will be changed to be the first row which
599 * we'll actually write. Beforehand, *destY-1 is the first drawing row.
601 * \return GL_TRUE if image is ready for drawing or
602 * GL_FALSE if image was completely clipped away (draw nothing)
605 _mesa_clip_drawpixels(const struct gl_context
*ctx
,
606 GLint
*destX
, GLint
*destY
,
607 GLsizei
*width
, GLsizei
*height
,
608 struct gl_pixelstore_attrib
*unpack
)
610 const struct gl_framebuffer
*buffer
= ctx
->DrawBuffer
;
612 if (unpack
->RowLength
== 0) {
613 unpack
->RowLength
= *width
;
616 assert(ctx
->Pixel
.ZoomX
== 1.0F
);
617 assert(ctx
->Pixel
.ZoomY
== 1.0F
|| ctx
->Pixel
.ZoomY
== -1.0F
);
620 if (*destX
< buffer
->_Xmin
) {
621 unpack
->SkipPixels
+= (buffer
->_Xmin
- *destX
);
622 *width
-= (buffer
->_Xmin
- *destX
);
623 *destX
= buffer
->_Xmin
;
626 if (*destX
+ *width
> buffer
->_Xmax
)
627 *width
-= (*destX
+ *width
- buffer
->_Xmax
);
632 if (ctx
->Pixel
.ZoomY
== 1.0F
) {
633 /* bottom clipping */
634 if (*destY
< buffer
->_Ymin
) {
635 unpack
->SkipRows
+= (buffer
->_Ymin
- *destY
);
636 *height
-= (buffer
->_Ymin
- *destY
);
637 *destY
= buffer
->_Ymin
;
640 if (*destY
+ *height
> buffer
->_Ymax
)
641 *height
-= (*destY
+ *height
- buffer
->_Ymax
);
643 else { /* upside down */
645 if (*destY
> buffer
->_Ymax
) {
646 unpack
->SkipRows
+= (*destY
- buffer
->_Ymax
);
647 *height
-= (*destY
- buffer
->_Ymax
);
648 *destY
= buffer
->_Ymax
;
650 /* bottom clipping */
651 if (*destY
- *height
< buffer
->_Ymin
)
652 *height
-= (buffer
->_Ymin
- (*destY
- *height
));
653 /* adjust destY so it's the first row to write to */
665 * Perform clipping for glReadPixels. The image's window position
666 * and size, and the pack skipPixels, skipRows and rowLength are adjusted
667 * so that the image region is entirely within the window bounds.
668 * Note: this is different from _mesa_clip_drawpixels() in that the
669 * scissor box is ignored, and we use the bounds of the current readbuffer
670 * surface or the attached image.
672 * \return GL_TRUE if region to read is in bounds
673 * GL_FALSE if region is completely out of bounds (nothing to read)
676 _mesa_clip_readpixels(const struct gl_context
*ctx
,
677 GLint
*srcX
, GLint
*srcY
,
678 GLsizei
*width
, GLsizei
*height
,
679 struct gl_pixelstore_attrib
*pack
)
681 const struct gl_framebuffer
*buffer
= ctx
->ReadBuffer
;
682 struct gl_renderbuffer
*rb
= buffer
->_ColorReadBuffer
;
687 clip_width
= rb
->Width
;
688 clip_height
= rb
->Height
;
690 clip_width
= buffer
->Width
;
691 clip_height
= buffer
->Height
;
695 if (pack
->RowLength
== 0) {
696 pack
->RowLength
= *width
;
701 pack
->SkipPixels
+= (0 - *srcX
);
702 *width
-= (0 - *srcX
);
706 if (*srcX
+ *width
> clip_width
)
707 *width
-= (*srcX
+ *width
- clip_width
);
712 /* bottom clipping */
714 pack
->SkipRows
+= (0 - *srcY
);
715 *height
-= (0 - *srcY
);
719 if (*srcY
+ *height
> clip_height
)
720 *height
-= (*srcY
+ *height
- clip_height
);
730 * Do clipping for a glCopyTexSubImage call.
731 * The framebuffer source region might extend outside the framebuffer
732 * bounds. Clip the source region against the framebuffer bounds and
733 * adjust the texture/dest position and size accordingly.
735 * \return GL_FALSE if region is totally clipped, GL_TRUE otherwise.
738 _mesa_clip_copytexsubimage(const struct gl_context
*ctx
,
739 GLint
*destX
, GLint
*destY
,
740 GLint
*srcX
, GLint
*srcY
,
741 GLsizei
*width
, GLsizei
*height
)
743 const struct gl_framebuffer
*fb
= ctx
->ReadBuffer
;
744 const GLint srcX0
= *srcX
, srcY0
= *srcY
;
746 if (_mesa_clip_to_region(0, 0, fb
->Width
, fb
->Height
,
747 srcX
, srcY
, width
, height
)) {
748 *destX
= *destX
+ *srcX
- srcX0
;
749 *destY
= *destY
+ *srcY
- srcY0
;
761 * Clip the rectangle defined by (x, y, width, height) against the bounds
762 * specified by [xmin, xmax) and [ymin, ymax).
763 * \return GL_FALSE if rect is totally clipped, GL_TRUE otherwise.
766 _mesa_clip_to_region(GLint xmin
, GLint ymin
,
767 GLint xmax
, GLint ymax
,
769 GLsizei
*width
, GLsizei
*height
)
773 *width
-= (xmin
- *x
);
778 if (*x
+ *width
> xmax
)
779 *width
-= (*x
+ *width
- xmax
);
784 /* bottom (or top) clipping */
786 *height
-= (ymin
- *y
);
790 /* top (or bottom) clipping */
791 if (*y
+ *height
> ymax
)
792 *height
-= (*y
+ *height
- ymax
);
802 * Clip dst coords against Xmax (or Ymax).
805 clip_right_or_top(GLint
*srcX0
, GLint
*srcX1
,
806 GLint
*dstX0
, GLint
*dstX1
,
811 if (*dstX1
> maxValue
) {
812 /* X1 outside right edge */
813 assert(*dstX0
< maxValue
); /* X0 should be inside right edge */
814 t
= (GLfloat
) (maxValue
- *dstX0
) / (GLfloat
) (*dstX1
- *dstX0
);
815 /* chop off [t, 1] part */
816 assert(t
>= 0.0 && t
<= 1.0);
818 bias
= (*srcX0
< *srcX1
) ? 0.5F
: -0.5F
;
819 *srcX1
= *srcX0
+ (GLint
) (t
* (*srcX1
- *srcX0
) + bias
);
821 else if (*dstX0
> maxValue
) {
822 /* X0 outside right edge */
823 assert(*dstX1
< maxValue
); /* X1 should be inside right edge */
824 t
= (GLfloat
) (maxValue
- *dstX1
) / (GLfloat
) (*dstX0
- *dstX1
);
825 /* chop off [t, 1] part */
826 assert(t
>= 0.0 && t
<= 1.0);
828 bias
= (*srcX0
< *srcX1
) ? -0.5F
: 0.5F
;
829 *srcX0
= *srcX1
+ (GLint
) (t
* (*srcX0
- *srcX1
) + bias
);
835 * Clip dst coords against Xmin (or Ymin).
838 clip_left_or_bottom(GLint
*srcX0
, GLint
*srcX1
,
839 GLint
*dstX0
, GLint
*dstX1
,
844 if (*dstX0
< minValue
) {
845 /* X0 outside left edge */
846 assert(*dstX1
> minValue
); /* X1 should be inside left edge */
847 t
= (GLfloat
) (minValue
- *dstX0
) / (GLfloat
) (*dstX1
- *dstX0
);
848 /* chop off [0, t] part */
849 assert(t
>= 0.0 && t
<= 1.0);
851 bias
= (*srcX0
< *srcX1
) ? 0.5F
: -0.5F
;
852 *srcX0
= *srcX0
+ (GLint
) (t
* (*srcX1
- *srcX0
) + bias
);
854 else if (*dstX1
< minValue
) {
855 /* X1 outside left edge */
856 assert(*dstX0
> minValue
); /* X0 should be inside left edge */
857 t
= (GLfloat
) (minValue
- *dstX1
) / (GLfloat
) (*dstX0
- *dstX1
);
858 /* chop off [0, t] part */
859 assert(t
>= 0.0 && t
<= 1.0);
861 bias
= (*srcX0
< *srcX1
) ? -0.5F
: 0.5F
;
862 *srcX1
= *srcX1
+ (GLint
) (t
* (*srcX0
- *srcX1
) + bias
);
868 * Do clipping of blit src/dest rectangles.
869 * The dest rect is clipped against both the buffer bounds and scissor bounds.
870 * The src rect is just clipped against the buffer bounds.
872 * When either the src or dest rect is clipped, the other is also clipped
875 * Note that X0 need not be less than X1 (same for Y) for either the source
876 * and dest rects. That makes the clipping a little trickier.
878 * \return GL_TRUE if anything is left to draw, GL_FALSE if totally clipped
881 _mesa_clip_blit(struct gl_context
*ctx
,
882 const struct gl_framebuffer
*readFb
,
883 const struct gl_framebuffer
*drawFb
,
884 GLint
*srcX0
, GLint
*srcY0
, GLint
*srcX1
, GLint
*srcY1
,
885 GLint
*dstX0
, GLint
*dstY0
, GLint
*dstX1
, GLint
*dstY1
)
887 const GLint srcXmin
= 0;
888 const GLint srcXmax
= readFb
->Width
;
889 const GLint srcYmin
= 0;
890 const GLint srcYmax
= readFb
->Height
;
892 /* these include scissor bounds */
893 const GLint dstXmin
= drawFb
->_Xmin
;
894 const GLint dstXmax
= drawFb
->_Xmax
;
895 const GLint dstYmin
= drawFb
->_Ymin
;
896 const GLint dstYmax
= drawFb
->_Ymax
;
899 printf("PreClipX: src: %d .. %d dst: %d .. %d\n",
900 *srcX0, *srcX1, *dstX0, *dstX1);
901 printf("PreClipY: src: %d .. %d dst: %d .. %d\n",
902 *srcY0, *srcY1, *dstY0, *dstY1);
905 /* trivial rejection tests */
906 if (*dstX0
== *dstX1
)
907 return GL_FALSE
; /* no width */
908 if (*dstX0
<= dstXmin
&& *dstX1
<= dstXmin
)
909 return GL_FALSE
; /* totally out (left) of bounds */
910 if (*dstX0
>= dstXmax
&& *dstX1
>= dstXmax
)
911 return GL_FALSE
; /* totally out (right) of bounds */
913 if (*dstY0
== *dstY1
)
915 if (*dstY0
<= dstYmin
&& *dstY1
<= dstYmin
)
917 if (*dstY0
>= dstYmax
&& *dstY1
>= dstYmax
)
920 if (*srcX0
== *srcX1
)
922 if (*srcX0
<= srcXmin
&& *srcX1
<= srcXmin
)
924 if (*srcX0
>= srcXmax
&& *srcX1
>= srcXmax
)
927 if (*srcY0
== *srcY1
)
929 if (*srcY0
<= srcYmin
&& *srcY1
<= srcYmin
)
931 if (*srcY0
>= srcYmax
&& *srcY1
>= srcYmax
)
937 clip_right_or_top(srcX0
, srcX1
, dstX0
, dstX1
, dstXmax
);
938 clip_right_or_top(srcY0
, srcY1
, dstY0
, dstY1
, dstYmax
);
939 clip_left_or_bottom(srcX0
, srcX1
, dstX0
, dstX1
, dstXmin
);
940 clip_left_or_bottom(srcY0
, srcY1
, dstY0
, dstY1
, dstYmin
);
943 * src clip (just swap src/dst values from above)
945 clip_right_or_top(dstX0
, dstX1
, srcX0
, srcX1
, srcXmax
);
946 clip_right_or_top(dstY0
, dstY1
, srcY0
, srcY1
, srcYmax
);
947 clip_left_or_bottom(dstX0
, dstX1
, srcX0
, srcX1
, srcXmin
);
948 clip_left_or_bottom(dstY0
, dstY1
, srcY0
, srcY1
, srcYmin
);
951 printf("PostClipX: src: %d .. %d dst: %d .. %d\n",
952 *srcX0, *srcX1, *dstX0, *dstX1);
953 printf("PostClipY: src: %d .. %d dst: %d .. %d\n",
954 *srcY0, *srcY1, *dstY0, *dstY1);
957 assert(*dstX0
>= dstXmin
);
958 assert(*dstX0
<= dstXmax
);
959 assert(*dstX1
>= dstXmin
);
960 assert(*dstX1
<= dstXmax
);
962 assert(*dstY0
>= dstYmin
);
963 assert(*dstY0
<= dstYmax
);
964 assert(*dstY1
>= dstYmin
);
965 assert(*dstY1
<= dstYmax
);
967 assert(*srcX0
>= srcXmin
);
968 assert(*srcX0
<= srcXmax
);
969 assert(*srcX1
>= srcXmin
);
970 assert(*srcX1
<= srcXmax
);
972 assert(*srcY0
>= srcYmin
);
973 assert(*srcY0
<= srcYmax
);
974 assert(*srcY1
>= srcYmin
);
975 assert(*srcY1
<= srcYmax
);
981 * Swap the bytes in a 2D image.
983 * using the packing information this swaps the bytes
984 * according to the format and type of data being input.
985 * It takes into a/c various packing parameters like
986 * Alignment and RowLength.
989 _mesa_swap_bytes_2d_image(GLenum format
, GLenum type
,
990 const struct gl_pixelstore_attrib
*packing
,
991 GLsizei width
, GLsizei height
,
992 GLvoid
*dst
, const GLvoid
*src
)
994 GLint swapSize
= _mesa_sizeof_packed_type(type
);
996 assert(packing
->SwapBytes
);
998 if (swapSize
== 2 || swapSize
== 4) {
999 int swapsPerPixel
= _mesa_bytes_per_pixel(format
, type
) / swapSize
;
1000 int stride
= _mesa_image_row_stride(packing
, width
, format
, type
);
1003 const uint8_t *srcrow
;
1004 assert(swapsPerPixel
> 0);
1005 assert(_mesa_bytes_per_pixel(format
, type
) % swapSize
== 0);
1008 for (row
= 0; row
< height
; row
++) {
1010 swap2_copy((GLushort
*)dstrow
, (GLushort
*)srcrow
, width
* swapsPerPixel
);
1011 else if (swapSize
== 4)
1012 swap4_copy((GLuint
*)dstrow
, (GLuint
*)srcrow
, width
* swapsPerPixel
);