84c5b22286a73366b7e378acfbff7328efc84c1d
[mesa.git] / src / mesa / main / readpix.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 7.1
4 *
5 * Copyright (C) 1999-2008 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 #include "glheader.h"
26 #include "imports.h"
27 #include "bufferobj.h"
28 #include "context.h"
29 #include "enums.h"
30 #include "readpix.h"
31 #include "framebuffer.h"
32 #include "formats.h"
33 #include "image.h"
34 #include "mtypes.h"
35 #include "pbo.h"
36 #include "state.h"
37
38
39 /**
40 * Do error checking of the format/type parameters to glReadPixels and
41 * glDrawPixels.
42 * \param drawing if GL_TRUE do checking for DrawPixels, else do checking
43 * for ReadPixels.
44 * \return GL_TRUE if error detected, GL_FALSE if no errors
45 */
46 GLboolean
47 _mesa_error_check_format_type(struct gl_context *ctx, GLenum format,
48 GLenum type, GLboolean drawing)
49 {
50 const char *readDraw = drawing ? "Draw" : "Read";
51 const GLboolean reading = !drawing;
52
53 /* state validation should have already been done */
54 ASSERT(ctx->NewState == 0x0);
55
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);
61 return GL_TRUE;
62 }
63
64 if (ctx->Extensions.ARB_depth_buffer_float
65 && type == GL_FLOAT_32_UNSIGNED_INT_24_8_REV
66 && format != GL_DEPTH_STENCIL_EXT) {
67 _mesa_error(ctx, GL_INVALID_OPERATION,
68 "gl%sPixels(format is not GL_DEPTH_STENCIL_EXT)", readDraw);
69 return GL_TRUE;
70 }
71
72 /* basic combinations test */
73 if (!_mesa_is_legal_format_and_type(ctx, format, type)) {
74 _mesa_error(ctx, GL_INVALID_ENUM,
75 "gl%sPixels(format or type)", readDraw);
76 return GL_TRUE;
77 }
78
79 /* additional checks */
80 switch (format) {
81 case GL_RG:
82 case GL_RED:
83 case GL_GREEN:
84 case GL_BLUE:
85 case GL_ALPHA:
86 case GL_LUMINANCE:
87 case GL_LUMINANCE_ALPHA:
88 case GL_RGB:
89 case GL_BGR:
90 case GL_RGBA:
91 case GL_BGRA:
92 case GL_ABGR_EXT:
93 case GL_RED_INTEGER_EXT:
94 case GL_GREEN_INTEGER_EXT:
95 case GL_BLUE_INTEGER_EXT:
96 case GL_ALPHA_INTEGER_EXT:
97 case GL_RGB_INTEGER_EXT:
98 case GL_RGBA_INTEGER_EXT:
99 case GL_BGR_INTEGER_EXT:
100 case GL_BGRA_INTEGER_EXT:
101 case GL_LUMINANCE_INTEGER_EXT:
102 case GL_LUMINANCE_ALPHA_INTEGER_EXT:
103 if (!drawing) {
104 /* reading */
105 if (!_mesa_source_buffer_exists(ctx, GL_COLOR)) {
106 _mesa_error(ctx, GL_INVALID_OPERATION,
107 "glReadPixels(no color buffer)");
108 return GL_TRUE;
109 }
110 }
111 break;
112 case GL_COLOR_INDEX:
113 if (drawing) {
114 if (ctx->PixelMaps.ItoR.Size == 0 ||
115 ctx->PixelMaps.ItoG.Size == 0 ||
116 ctx->PixelMaps.ItoB.Size == 0) {
117 _mesa_error(ctx, GL_INVALID_OPERATION,
118 "glDrawPixels(drawing color index pixels into RGB buffer)");
119 return GL_TRUE;
120 }
121 }
122 else {
123 /* reading */
124 if (!_mesa_source_buffer_exists(ctx, GL_COLOR)) {
125 _mesa_error(ctx, GL_INVALID_OPERATION,
126 "glReadPixels(no color buffer)");
127 return GL_TRUE;
128 }
129 /* We no longer support CI-mode color buffers so trying to read
130 * GL_COLOR_INDEX pixels is always an error.
131 */
132 _mesa_error(ctx, GL_INVALID_OPERATION,
133 "glReadPixels(color buffer is RGB)");
134 return GL_TRUE;
135 }
136 break;
137 case GL_STENCIL_INDEX:
138 if ((drawing && !_mesa_dest_buffer_exists(ctx, format)) ||
139 (reading && !_mesa_source_buffer_exists(ctx, format))) {
140 _mesa_error(ctx, GL_INVALID_OPERATION,
141 "gl%sPixels(no stencil buffer)", readDraw);
142 return GL_TRUE;
143 }
144 break;
145 case GL_DEPTH_COMPONENT:
146 if ((drawing && !_mesa_dest_buffer_exists(ctx, format))) {
147 _mesa_error(ctx, GL_INVALID_OPERATION,
148 "gl%sPixels(no depth buffer)", readDraw);
149 return GL_TRUE;
150 }
151 break;
152 case GL_DEPTH_STENCIL_EXT:
153 /* Check validity of the type first. */
154 switch (type) {
155 case GL_UNSIGNED_INT_24_8_EXT:
156 if (!ctx->Extensions.EXT_packed_depth_stencil) {
157 _mesa_error(ctx, GL_INVALID_ENUM, "gl%sPixels(type)", readDraw);
158 return GL_TRUE;
159 }
160 break;
161 case GL_FLOAT_32_UNSIGNED_INT_24_8_REV:
162 if (!ctx->Extensions.ARB_depth_buffer_float) {
163 _mesa_error(ctx, GL_INVALID_ENUM, "gl%sPixels(type)", readDraw);
164 return GL_TRUE;
165 }
166 break;
167 default:
168 _mesa_error(ctx, GL_INVALID_ENUM, "gl%sPixels(type)", readDraw);
169 return GL_TRUE;
170 }
171 if ((drawing && !_mesa_dest_buffer_exists(ctx, format)) ||
172 (reading && !_mesa_source_buffer_exists(ctx, format))) {
173 _mesa_error(ctx, GL_INVALID_OPERATION,
174 "gl%sPixels(no depth or stencil buffer)", readDraw);
175 return GL_TRUE;
176 }
177 break;
178 default:
179 /* this should have been caught in _mesa_is_legal_format_type() */
180 _mesa_problem(ctx, "unexpected format in _mesa_%sPixels", readDraw);
181 return GL_TRUE;
182 }
183
184 /* no errors */
185 return GL_FALSE;
186 }
187
188
189
190 void GLAPIENTRY
191 _mesa_ReadnPixelsARB( GLint x, GLint y, GLsizei width, GLsizei height,
192 GLenum format, GLenum type, GLsizei bufSize,
193 GLvoid *pixels )
194 {
195 GET_CURRENT_CONTEXT(ctx);
196 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
197
198 FLUSH_CURRENT(ctx, 0);
199
200 if (MESA_VERBOSE & VERBOSE_API)
201 _mesa_debug(ctx, "glReadPixels(%d, %d, %s, %s, %p)\n",
202 width, height,
203 _mesa_lookup_enum_by_nr(format),
204 _mesa_lookup_enum_by_nr(type),
205 pixels);
206
207 if (width < 0 || height < 0) {
208 _mesa_error( ctx, GL_INVALID_VALUE,
209 "glReadPixels(width=%d height=%d)", width, height );
210 return;
211 }
212
213 if (ctx->NewState)
214 _mesa_update_state(ctx);
215
216 if (_mesa_error_check_format_type(ctx, format, type, GL_FALSE)) {
217 /* found an error */
218 return;
219 }
220
221 /* Check that the destination format and source buffer are both
222 * integer-valued or both non-integer-valued.
223 */
224 if (ctx->Extensions.EXT_texture_integer && _mesa_is_color_format(format)) {
225 const struct gl_renderbuffer *rb = ctx->ReadBuffer->_ColorReadBuffer;
226 const GLboolean srcInteger = _mesa_is_format_integer_color(rb->Format);
227 const GLboolean dstInteger = _mesa_is_integer_format(format);
228 if (dstInteger != srcInteger) {
229 _mesa_error(ctx, GL_INVALID_OPERATION,
230 "glReadPixels(integer / non-integer format mismatch");
231 return;
232 }
233 }
234
235 if (ctx->ReadBuffer->_Status != GL_FRAMEBUFFER_COMPLETE_EXT) {
236 _mesa_error(ctx, GL_INVALID_FRAMEBUFFER_OPERATION_EXT,
237 "glReadPixels(incomplete framebuffer)" );
238 return;
239 }
240
241 if (!_mesa_source_buffer_exists(ctx, format)) {
242 _mesa_error(ctx, GL_INVALID_OPERATION, "glReadPixels(no readbuffer)");
243 return;
244 }
245
246 if (width == 0 || height == 0)
247 return; /* nothing to do */
248
249 if (!_mesa_validate_pbo_access(2, &ctx->Pack, width, height, 1,
250 format, type, bufSize, pixels)) {
251 if (_mesa_is_bufferobj(ctx->Pack.BufferObj)) {
252 _mesa_error(ctx, GL_INVALID_OPERATION,
253 "glReadPixels(out of bounds PBO access)");
254 } else {
255 _mesa_error(ctx, GL_INVALID_OPERATION,
256 "glReadnPixelsARB(out of bounds access:"
257 " bufSize (%d) is too small)", bufSize);
258 }
259 return;
260 }
261
262 if (_mesa_is_bufferobj(ctx->Pack.BufferObj) &&
263 _mesa_bufferobj_mapped(ctx->Pack.BufferObj)) {
264 /* buffer is mapped - that's an error */
265 _mesa_error(ctx, GL_INVALID_OPERATION, "glReadPixels(PBO is mapped)");
266 return;
267 }
268
269 ctx->Driver.ReadPixels(ctx, x, y, width, height,
270 format, type, &ctx->Pack, pixels);
271 }
272
273 void GLAPIENTRY
274 _mesa_ReadPixels( GLint x, GLint y, GLsizei width, GLsizei height,
275 GLenum format, GLenum type, GLvoid *pixels )
276 {
277 _mesa_ReadnPixelsARB(x, y, width, height, format, type, INT_MAX, pixels);
278 }