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"
30 #include "framebuffer.h"
36 * Do error checking of the format/type parameters to glReadPixels and
38 * \param drawing if GL_TRUE do checking for DrawPixels, else do checking
40 * \return GL_TRUE if error detected, GL_FALSE if no errors
43 _mesa_error_check_format_type(GLcontext
*ctx
, GLenum format
, GLenum type
,
46 const char *readDraw
= drawing
? "Draw" : "Read";
47 const GLboolean reading
= !drawing
;
49 /* state validation should have already been done */
50 ASSERT(ctx
->NewState
== 0x0);
52 if (ctx
->Extensions
.EXT_packed_depth_stencil
53 && type
== GL_UNSIGNED_INT_24_8_EXT
54 && format
!= GL_DEPTH_STENCIL_EXT
) {
55 _mesa_error(ctx
, GL_INVALID_OPERATION
,
56 "gl%sPixels(format is not GL_DEPTH_STENCIL_EXT)", readDraw
);
60 /* basic combinations test */
61 if (!_mesa_is_legal_format_and_type(ctx
, format
, type
)) {
62 _mesa_error(ctx
, GL_INVALID_ENUM
,
63 "gl%sPixels(format or type)", readDraw
);
67 /* additional checks */
74 case GL_LUMINANCE_ALPHA
:
81 if (!ctx
->DrawBuffer
->Visual
.rgbMode
) {
82 _mesa_error(ctx
, GL_INVALID_OPERATION
,
83 "glDrawPixels(drawing RGB pixels into color index buffer)");
89 if (!_mesa_source_buffer_exists(ctx
, GL_COLOR
)) {
90 _mesa_error(ctx
, GL_INVALID_OPERATION
,
91 "glReadPixels(no color buffer)");
98 if (ctx
->DrawBuffer
->Visual
.rgbMode
&&
99 (ctx
->PixelMaps
.ItoR
.Size
== 0 ||
100 ctx
->PixelMaps
.ItoG
.Size
== 0 ||
101 ctx
->PixelMaps
.ItoB
.Size
== 0)) {
102 _mesa_error(ctx
, GL_INVALID_OPERATION
,
103 "glDrawPixels(drawing color index pixels into RGB buffer)");
109 if (!_mesa_source_buffer_exists(ctx
, GL_COLOR
)) {
110 _mesa_error(ctx
, GL_INVALID_OPERATION
,
111 "glReadPixels(no color buffer)");
116 case GL_STENCIL_INDEX
:
117 if ((drawing
&& !_mesa_dest_buffer_exists(ctx
, format
)) ||
118 (reading
&& !_mesa_source_buffer_exists(ctx
, format
))) {
119 _mesa_error(ctx
, GL_INVALID_OPERATION
,
120 "gl%sPixels(no stencil buffer)", readDraw
);
124 case GL_DEPTH_COMPONENT
:
125 if ((drawing
&& !_mesa_dest_buffer_exists(ctx
, format
))) {
126 _mesa_error(ctx
, GL_INVALID_OPERATION
,
127 "gl%sPixels(no depth buffer)", readDraw
);
131 case GL_DEPTH_STENCIL_EXT
:
132 if (!ctx
->Extensions
.EXT_packed_depth_stencil
||
133 type
!= GL_UNSIGNED_INT_24_8_EXT
) {
134 _mesa_error(ctx
, GL_INVALID_ENUM
, "gl%sPixels(type)", readDraw
);
137 if ((drawing
&& !_mesa_dest_buffer_exists(ctx
, format
)) ||
138 (reading
&& !_mesa_source_buffer_exists(ctx
, format
))) {
139 _mesa_error(ctx
, GL_INVALID_OPERATION
,
140 "gl%sPixels(no depth or stencil buffer)", readDraw
);
145 /* this should have been caught in _mesa_is_legal_format_type() */
146 _mesa_problem(ctx
, "unexpected format in _mesa_%sPixels", readDraw
);
157 _mesa_ReadPixels( GLint x
, GLint y
, GLsizei width
, GLsizei height
,
158 GLenum format
, GLenum type
, GLvoid
*pixels
)
160 GET_CURRENT_CONTEXT(ctx
);
161 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
163 FLUSH_CURRENT(ctx
, 0);
165 if (width
< 0 || height
< 0) {
166 _mesa_error( ctx
, GL_INVALID_VALUE
,
167 "glReadPixels(width=%d height=%d)", width
, height
);
172 _mesa_update_state(ctx
);
174 if (_mesa_error_check_format_type(ctx
, format
, type
, GL_FALSE
)) {
179 if (ctx
->ReadBuffer
->_Status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
180 _mesa_error(ctx
, GL_INVALID_FRAMEBUFFER_OPERATION_EXT
,
181 "glReadPixels(incomplete framebuffer)" );
185 if (!_mesa_source_buffer_exists(ctx
, format
)) {
186 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glReadPixels(no readbuffer)");
190 if (width
== 0 || height
== 0)
191 return; /* nothing to do */
193 if (_mesa_is_bufferobj(ctx
->Pack
.BufferObj
)) {
194 if (!_mesa_validate_pbo_access(2, &ctx
->Pack
, width
, height
, 1,
195 format
, type
, pixels
)) {
196 _mesa_error(ctx
, GL_INVALID_OPERATION
,
197 "glReadPixels(invalid PBO access)");
201 if (_mesa_bufferobj_mapped(ctx
->Pack
.BufferObj
)) {
202 /* buffer is mapped - that's an error */
203 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glReadPixels(PBO is mapped)");
208 ctx
->Driver
.ReadPixels(ctx
, x
, y
, width
, height
,
209 format
, type
, &ctx
->Pack
, pixels
);