2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2008 Brian Paul 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 * 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.
27 #include "bufferobj.h"
31 #include "framebuffer.h"
40 * Do error checking of the format/type parameters to glReadPixels and
42 * \param drawing if GL_TRUE do checking for DrawPixels, else do checking
44 * \return GL_TRUE if error detected, GL_FALSE if no errors
47 _mesa_error_check_format_type(struct gl_context
*ctx
, GLenum format
,
48 GLenum type
, GLboolean drawing
)
50 const char *readDraw
= drawing
? "Draw" : "Read";
51 const GLboolean reading
= !drawing
;
53 /* state validation should have already been done */
54 ASSERT(ctx
->NewState
== 0x0);
56 if (ctx
->Extensions
.EXT_packed_depth_stencil
57 && type
== GL_UNSIGNED_INT_24_8_EXT
58 && format
!= GL_DEPTH_STENCIL_EXT
) {
59 _mesa_error(ctx
, GL_INVALID_OPERATION
,
60 "gl%sPixels(format is not GL_DEPTH_STENCIL_EXT)", readDraw
);
64 /* basic combinations test */
65 if (!_mesa_is_legal_format_and_type(ctx
, format
, type
)) {
66 _mesa_error(ctx
, GL_INVALID_ENUM
,
67 "gl%sPixels(format or type)", readDraw
);
71 /* additional checks */
79 case GL_LUMINANCE_ALPHA
:
85 case GL_RED_INTEGER_EXT
:
86 case GL_GREEN_INTEGER_EXT
:
87 case GL_BLUE_INTEGER_EXT
:
88 case GL_ALPHA_INTEGER_EXT
:
89 case GL_RGB_INTEGER_EXT
:
90 case GL_RGBA_INTEGER_EXT
:
91 case GL_BGR_INTEGER_EXT
:
92 case GL_BGRA_INTEGER_EXT
:
93 case GL_LUMINANCE_INTEGER_EXT
:
94 case GL_LUMINANCE_ALPHA_INTEGER_EXT
:
97 if (!_mesa_source_buffer_exists(ctx
, GL_COLOR
)) {
98 _mesa_error(ctx
, GL_INVALID_OPERATION
,
99 "glReadPixels(no color buffer)");
106 if (ctx
->PixelMaps
.ItoR
.Size
== 0 ||
107 ctx
->PixelMaps
.ItoG
.Size
== 0 ||
108 ctx
->PixelMaps
.ItoB
.Size
== 0) {
109 _mesa_error(ctx
, GL_INVALID_OPERATION
,
110 "glDrawPixels(drawing color index pixels into RGB buffer)");
116 if (!_mesa_source_buffer_exists(ctx
, GL_COLOR
)) {
117 _mesa_error(ctx
, GL_INVALID_OPERATION
,
118 "glReadPixels(no color buffer)");
121 /* We no longer support CI-mode color buffers so trying to read
122 * GL_COLOR_INDEX pixels is always an error.
124 _mesa_error(ctx
, GL_INVALID_OPERATION
,
125 "glReadPixels(color buffer is RGB)");
129 case GL_STENCIL_INDEX
:
130 if ((drawing
&& !_mesa_dest_buffer_exists(ctx
, format
)) ||
131 (reading
&& !_mesa_source_buffer_exists(ctx
, format
))) {
132 _mesa_error(ctx
, GL_INVALID_OPERATION
,
133 "gl%sPixels(no stencil buffer)", readDraw
);
137 case GL_DEPTH_COMPONENT
:
138 if ((drawing
&& !_mesa_dest_buffer_exists(ctx
, format
))) {
139 _mesa_error(ctx
, GL_INVALID_OPERATION
,
140 "gl%sPixels(no depth buffer)", readDraw
);
144 case GL_DEPTH_STENCIL_EXT
:
145 if (!ctx
->Extensions
.EXT_packed_depth_stencil
||
146 type
!= GL_UNSIGNED_INT_24_8_EXT
) {
147 _mesa_error(ctx
, GL_INVALID_ENUM
, "gl%sPixels(type)", readDraw
);
150 if ((drawing
&& !_mesa_dest_buffer_exists(ctx
, format
)) ||
151 (reading
&& !_mesa_source_buffer_exists(ctx
, format
))) {
152 _mesa_error(ctx
, GL_INVALID_OPERATION
,
153 "gl%sPixels(no depth or stencil buffer)", readDraw
);
158 /* this should have been caught in _mesa_is_legal_format_type() */
159 _mesa_problem(ctx
, "unexpected format in _mesa_%sPixels", readDraw
);
170 _mesa_ReadnPixelsARB( GLint x
, GLint y
, GLsizei width
, GLsizei height
,
171 GLenum format
, GLenum type
, GLsizei bufSize
,
174 GET_CURRENT_CONTEXT(ctx
);
175 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
177 FLUSH_CURRENT(ctx
, 0);
179 if (MESA_VERBOSE
& VERBOSE_API
)
180 _mesa_debug(ctx
, "glReadPixels(%d, %d, %s, %s, %p)\n",
182 _mesa_lookup_enum_by_nr(format
),
183 _mesa_lookup_enum_by_nr(type
),
186 if (width
< 0 || height
< 0) {
187 _mesa_error( ctx
, GL_INVALID_VALUE
,
188 "glReadPixels(width=%d height=%d)", width
, height
);
193 _mesa_update_state(ctx
);
195 if (_mesa_error_check_format_type(ctx
, format
, type
, GL_FALSE
)) {
200 /* Check that the destination format and source buffer are both
201 * integer-valued or both non-integer-valued.
203 if (ctx
->Extensions
.EXT_texture_integer
&& _mesa_is_color_format(format
)) {
204 const struct gl_renderbuffer
*rb
= ctx
->ReadBuffer
->_ColorReadBuffer
;
205 const GLboolean srcInteger
= _mesa_is_format_integer_color(rb
->Format
);
206 const GLboolean dstInteger
= _mesa_is_integer_format(format
);
207 if (dstInteger
!= srcInteger
) {
208 _mesa_error(ctx
, GL_INVALID_OPERATION
,
209 "glReadPixels(integer / non-integer format mismatch");
214 if (ctx
->ReadBuffer
->_Status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
215 _mesa_error(ctx
, GL_INVALID_FRAMEBUFFER_OPERATION_EXT
,
216 "glReadPixels(incomplete framebuffer)" );
220 if (!_mesa_source_buffer_exists(ctx
, format
)) {
221 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glReadPixels(no readbuffer)");
225 if (width
== 0 || height
== 0)
226 return; /* nothing to do */
228 if (!_mesa_validate_pbo_access(2, &ctx
->Pack
, width
, height
, 1,
229 format
, type
, bufSize
, pixels
)) {
230 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
231 _mesa_error(ctx
, GL_INVALID_OPERATION
,
232 "glReadPixels(out of bounds PBO access)");
234 _mesa_error(ctx
, GL_INVALID_OPERATION
,
235 "glReadnPixelsARB(out of bounds access:"
236 " bufSize (%d) is too small)", bufSize
);
241 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
) &&
242 _mesa_bufferobj_mapped(ctx
->Pack
.BufferObj
)) {
243 /* buffer is mapped - that's an error */
244 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glReadPixels(PBO is mapped)");
248 ctx
->Driver
.ReadPixels(ctx
, x
, y
, width
, height
,
249 format
, type
, &ctx
->Pack
, pixels
);
253 _mesa_ReadPixels( GLint x
, GLint y
, GLsizei width
, GLsizei height
,
254 GLenum format
, GLenum type
, GLvoid
*pixels
)
256 _mesa_ReadnPixelsARB(x
, y
, width
, height
, format
, type
, INT_MAX
, pixels
);