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"
37 * Do error checking of the format/type parameters to glReadPixels and
39 * \param drawing if GL_TRUE do checking for DrawPixels, else do checking
41 * \return GL_TRUE if error detected, GL_FALSE if no errors
44 error_check_format_type(GLcontext
*ctx
, GLenum format
, GLenum type
,
47 const char *readDraw
= drawing
? "Draw" : "Read";
49 if (ctx
->Extensions
.EXT_packed_depth_stencil
50 && type
== GL_UNSIGNED_INT_24_8_EXT
51 && format
!= GL_DEPTH_STENCIL_EXT
) {
52 _mesa_error(ctx
, GL_INVALID_OPERATION
,
53 "gl%sPixels(format is not GL_DEPTH_STENCIL_EXT)", readDraw
);
57 /* basic combinations test */
58 if (!_mesa_is_legal_format_and_type(ctx
, format
, type
)) {
59 _mesa_error(ctx
, GL_INVALID_ENUM
,
60 "gl%sPixels(format or type)", readDraw
);
64 /* additional checks */
71 case GL_LUMINANCE_ALPHA
:
77 if (drawing
&& !ctx
->Visual
.rgbMode
) {
78 _mesa_error(ctx
, GL_INVALID_OPERATION
,
79 "glDrawPixels(drawing RGB pixels into color index buffer)");
82 if (!drawing
&& !_mesa_dest_buffer_exists(ctx
, GL_COLOR
)) {
83 _mesa_error(ctx
, GL_INVALID_OPERATION
,
84 "glReadPixels(no color buffer)");
89 if (!drawing
&& ctx
->Visual
.rgbMode
) {
90 _mesa_error(ctx
, GL_INVALID_OPERATION
,
91 "glReadPixels(reading color index format from RGB buffer)");
94 if (!drawing
&& !_mesa_dest_buffer_exists(ctx
, GL_COLOR
)) {
95 _mesa_error(ctx
, GL_INVALID_OPERATION
,
96 "glReadPixels(no color buffer)");
100 case GL_STENCIL_INDEX
:
101 if ((drawing
&& !_mesa_dest_buffer_exists(ctx
, format
)) ||
102 (!drawing
&& !_mesa_source_buffer_exists(ctx
, format
))) {
103 _mesa_error(ctx
, GL_INVALID_OPERATION
,
104 "gl%sPixels(no stencil buffer)", readDraw
);
108 case GL_DEPTH_COMPONENT
:
109 if (!drawing
&& !_mesa_source_buffer_exists(ctx
, format
)) {
110 _mesa_error(ctx
, GL_INVALID_OPERATION
,
111 "gl%sPixels(no depth buffer)", readDraw
);
115 case GL_DEPTH_STENCIL_EXT
:
116 if (!ctx
->Extensions
.EXT_packed_depth_stencil
||
117 type
!= GL_UNSIGNED_INT_24_8_EXT
) {
118 _mesa_error(ctx
, GL_INVALID_ENUM
, "gl%sPixels(type)", readDraw
);
121 if ((drawing
&& !_mesa_dest_buffer_exists(ctx
, format
)) ||
122 (!drawing
&& !_mesa_source_buffer_exists(ctx
, format
))) {
123 _mesa_error(ctx
, GL_INVALID_OPERATION
,
124 "gl%sPixels(no depth or stencil buffer)", readDraw
);
129 /* this should have been caught in _mesa_is_legal_format_type() */
130 _mesa_problem(ctx
, "unexpected format in _mesa_%sPixels", readDraw
);
143 * Execute glDrawPixels
146 _mesa_DrawPixels( GLsizei width
, GLsizei height
,
147 GLenum format
, GLenum type
, const GLvoid
*pixels
)
149 GET_CURRENT_CONTEXT(ctx
);
150 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
152 if (width
< 0 || height
< 0) {
153 _mesa_error( ctx
, GL_INVALID_VALUE
, "glDrawPixels(width or height < 0" );
158 _mesa_update_state(ctx
);
161 if (ctx
->FragmentProgram
.Enabled
&& !ctx
->FragmentProgram
._Enabled
) {
162 _mesa_error(ctx
, GL_INVALID_OPERATION
,
163 "glDrawPixels (invalid fragment program)");
167 if (error_check_format_type(ctx
, format
, type
, GL_TRUE
)) {
172 if (ctx
->DrawBuffer
->_Status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
173 _mesa_error(ctx
, GL_INVALID_FRAMEBUFFER_OPERATION_EXT
,
174 "glDrawPixels(incomplete framebuffer)" );
178 if (!ctx
->Current
.RasterPosValid
) {
182 if (ctx
->RenderMode
== GL_RENDER
) {
183 /* Round, to satisfy conformance tests (matches SGI's OpenGL) */
184 GLint x
= IROUND(ctx
->Current
.RasterPos
[0]);
185 GLint y
= IROUND(ctx
->Current
.RasterPos
[1]);
187 if (ctx
->Unpack
.BufferObj
->Name
) {
188 /* unpack from PBO */
189 if (!_mesa_validate_pbo_access(2, &ctx
->Unpack
, width
, height
, 1,
190 format
, type
, pixels
)) {
191 _mesa_error(ctx
, GL_INVALID_OPERATION
,
192 "glDrawPixels(invalid PBO access)");
195 if (ctx
->Unpack
.BufferObj
->Pointer
) {
196 /* buffer is mapped - that's an error */
197 _mesa_error(ctx
, GL_INVALID_OPERATION
,
198 "glDrawPixels(PBO is mapped)");
203 ctx
->Driver
.DrawPixels(ctx
, x
, y
, width
, height
, format
, type
,
204 &ctx
->Unpack
, pixels
);
206 else if (ctx
->RenderMode
== GL_FEEDBACK
) {
207 /* Feedback the current raster pos info */
208 FLUSH_CURRENT( ctx
, 0 );
209 FEEDBACK_TOKEN( ctx
, (GLfloat
) (GLint
) GL_DRAW_PIXEL_TOKEN
);
210 _mesa_feedback_vertex( ctx
,
211 ctx
->Current
.RasterPos
,
212 ctx
->Current
.RasterColor
,
213 ctx
->Current
.RasterIndex
,
214 ctx
->Current
.RasterTexCoords
[0] );
217 ASSERT(ctx
->RenderMode
== GL_SELECT
);
218 /* Do nothing. See OpenGL Spec, Appendix B, Corollary 6. */
224 _mesa_CopyPixels( GLint srcx
, GLint srcy
, GLsizei width
, GLsizei height
,
227 GET_CURRENT_CONTEXT(ctx
);
228 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
231 _mesa_update_state(ctx
);
234 if (ctx
->FragmentProgram
.Enabled
&& !ctx
->FragmentProgram
._Enabled
) {
235 _mesa_error(ctx
, GL_INVALID_OPERATION
,
236 "glCopyPixels (invalid fragment program)");
240 if (width
< 0 || height
< 0) {
241 _mesa_error(ctx
, GL_INVALID_VALUE
, "glCopyPixels(width or height < 0)");
245 if (ctx
->DrawBuffer
->_Status
!= GL_FRAMEBUFFER_COMPLETE_EXT
||
246 ctx
->ReadBuffer
->_Status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
247 _mesa_error(ctx
, GL_INVALID_FRAMEBUFFER_OPERATION_EXT
,
248 "glCopyPixels(incomplete framebuffer)" );
252 if (!_mesa_source_buffer_exists(ctx
, type
) ||
253 !_mesa_dest_buffer_exists(ctx
, type
)) {
254 _mesa_error(ctx
, GL_INVALID_OPERATION
,
255 "glCopyPixels(missing source or dest buffer)");
259 if (!ctx
->Current
.RasterPosValid
) {
263 if (ctx
->RenderMode
== GL_RENDER
) {
264 /* Round to satisfy conformance tests (matches SGI's OpenGL) */
265 GLint destx
= IROUND(ctx
->Current
.RasterPos
[0]);
266 GLint desty
= IROUND(ctx
->Current
.RasterPos
[1]);
267 ctx
->Driver
.CopyPixels( ctx
, srcx
, srcy
, width
, height
, destx
, desty
,
270 else if (ctx
->RenderMode
== GL_FEEDBACK
) {
271 FLUSH_CURRENT( ctx
, 0 );
272 FEEDBACK_TOKEN( ctx
, (GLfloat
) (GLint
) GL_COPY_PIXEL_TOKEN
);
273 _mesa_feedback_vertex( ctx
,
274 ctx
->Current
.RasterPos
,
275 ctx
->Current
.RasterColor
,
276 ctx
->Current
.RasterIndex
,
277 ctx
->Current
.RasterTexCoords
[0] );
280 ASSERT(ctx
->RenderMode
== GL_SELECT
);
281 /* Do nothing. See OpenGL Spec, Appendix B, Corollary 6. */
285 #endif /* _HAVE_FULL_GL */
290 _mesa_ReadPixels( GLint x
, GLint y
, GLsizei width
, GLsizei height
,
291 GLenum format
, GLenum type
, GLvoid
*pixels
)
293 GET_CURRENT_CONTEXT(ctx
);
294 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
296 if (width
< 0 || height
< 0) {
297 _mesa_error( ctx
, GL_INVALID_VALUE
,
298 "glReadPixels(width=%d height=%d)", width
, height
);
303 _mesa_update_state(ctx
);
305 if (error_check_format_type(ctx
, format
, type
, GL_FALSE
)) {
310 if (ctx
->ReadBuffer
->_Status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
311 _mesa_error(ctx
, GL_INVALID_FRAMEBUFFER_OPERATION_EXT
,
312 "glReadPixels(incomplete framebuffer)" );
316 if (!_mesa_source_buffer_exists(ctx
, format
)) {
317 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glReadPixels(no readbuffer)");
321 if (ctx
->Pack
.BufferObj
->Name
) {
322 if (!_mesa_validate_pbo_access(2, &ctx
->Pack
, width
, height
, 1,
323 format
, type
, pixels
)) {
324 _mesa_error(ctx
, GL_INVALID_OPERATION
,
325 "glReadPixels(invalid PBO access)");
329 if (ctx
->Pack
.BufferObj
->Pointer
) {
330 /* buffer is mapped - that's an error */
331 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glReadPixels(PBO is mapped)");
336 ctx
->Driver
.ReadPixels(ctx
, x
, y
, width
, height
,
337 format
, type
, &ctx
->Pack
, pixels
);
343 _mesa_Bitmap( GLsizei width
, GLsizei height
,
344 GLfloat xorig
, GLfloat yorig
, GLfloat xmove
, GLfloat ymove
,
345 const GLubyte
*bitmap
)
347 GET_CURRENT_CONTEXT(ctx
);
348 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
350 if (width
< 0 || height
< 0) {
351 _mesa_error( ctx
, GL_INVALID_VALUE
, "glBitmap(width or height < 0)" );
355 if (!ctx
->Current
.RasterPosValid
) {
356 return; /* do nothing */
360 _mesa_update_state(ctx
);
363 if (ctx
->FragmentProgram
.Enabled
&& !ctx
->FragmentProgram
._Enabled
) {
364 _mesa_error(ctx
, GL_INVALID_OPERATION
,
365 "glBitmap (invalid fragment program)");
369 if (ctx
->DrawBuffer
->_Status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
370 _mesa_error(ctx
, GL_INVALID_FRAMEBUFFER_OPERATION_EXT
,
371 "glBitmap(incomplete framebuffer)");
375 if (ctx
->RenderMode
== GL_RENDER
) {
376 /* Truncate, to satisfy conformance tests (matches SGI's OpenGL). */
377 const GLfloat epsilon
= 0.0001;
378 GLint x
= IFLOOR(ctx
->Current
.RasterPos
[0] + epsilon
- xorig
);
379 GLint y
= IFLOOR(ctx
->Current
.RasterPos
[1] + epsilon
- yorig
);
381 if (ctx
->Unpack
.BufferObj
->Name
) {
382 /* unpack from PBO */
383 if (!_mesa_validate_pbo_access(2, &ctx
->Unpack
, width
, height
, 1,
384 GL_COLOR_INDEX
, GL_BITMAP
,
385 (GLvoid
*) bitmap
)) {
386 _mesa_error(ctx
, GL_INVALID_OPERATION
,
387 "glBitmap(invalid PBO access)");
390 if (ctx
->Unpack
.BufferObj
->Pointer
) {
391 /* buffer is mapped - that's an error */
392 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glBitmap(PBO is mapped)");
397 ctx
->Driver
.Bitmap( ctx
, x
, y
, width
, height
, &ctx
->Unpack
, bitmap
);
400 else if (ctx
->RenderMode
== GL_FEEDBACK
) {
401 FLUSH_CURRENT(ctx
, 0);
402 FEEDBACK_TOKEN( ctx
, (GLfloat
) (GLint
) GL_BITMAP_TOKEN
);
403 _mesa_feedback_vertex( ctx
,
404 ctx
->Current
.RasterPos
,
405 ctx
->Current
.RasterColor
,
406 ctx
->Current
.RasterIndex
,
407 ctx
->Current
.RasterTexCoords
[0] );
410 ASSERT(ctx
->RenderMode
== GL_SELECT
);
411 /* Do nothing. See OpenGL Spec, Appendix B, Corollary 6. */
415 /* update raster position */
416 ctx
->Current
.RasterPos
[0] += xmove
;
417 ctx
->Current
.RasterPos
[1] += ymove
;
422 #if 0 /* experimental */
424 * Execute glDrawDepthPixelsMESA(). This function accepts both a color
425 * image and depth (Z) image. Rasterization produces fragments with
426 * color and Z taken from these images. This function is intended for
427 * Z-compositing. Normally, this operation requires two glDrawPixels
428 * calls with stencil testing.
431 _mesa_DrawDepthPixelsMESA( GLsizei width
, GLsizei height
,
432 GLenum colorFormat
, GLenum colorType
,
433 const GLvoid
*colors
,
434 GLenum depthType
, const GLvoid
*depths
)
436 GET_CURRENT_CONTEXT(ctx
);
437 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
439 if (width
< 0 || height
< 0) {
440 _mesa_error( ctx
, GL_INVALID_VALUE
,
441 "glDrawDepthPixelsMESA(width or height < 0" );
445 if (!ctx
->Current
.RasterPosValid
) {
450 _mesa_update_state(ctx
);
453 if (ctx
->DrawBuffer
->_Status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
454 _mesa_error(ctx
, GL_INVALID_FRAMEBUFFER_OPERATION_EXT
,
455 "glDrawDepthPixelsMESA(incomplete framebuffer)");
459 if (ctx
->RenderMode
== GL_RENDER
) {
460 /* Round, to satisfy conformance tests (matches SGI's OpenGL) */
461 GLint x
= IROUND(ctx
->Current
.RasterPos
[0]);
462 GLint y
= IROUND(ctx
->Current
.RasterPos
[1]);
463 ctx
->Driver
.DrawDepthPixelsMESA(ctx
, x
, y
, width
, height
,
464 colorFormat
, colorType
, colors
,
465 depthType
, depths
, &ctx
->Unpack
);
467 else if (ctx
->RenderMode
== GL_FEEDBACK
) {
468 /* Feedback the current raster pos info */
469 FLUSH_CURRENT( ctx
, 0 );
470 FEEDBACK_TOKEN( ctx
, (GLfloat
) (GLint
) GL_DRAW_PIXEL_TOKEN
);
471 _mesa_feedback_vertex( ctx
,
472 ctx
->Current
.RasterPos
,
473 ctx
->Current
.RasterColor
,
474 ctx
->Current
.RasterIndex
,
475 ctx
->Current
.RasterTexCoords
[0] );
478 ASSERT(ctx
->RenderMode
== GL_SELECT
);
479 /* Do nothing. See OpenGL Spec, Appendix B, Corollary 6. */