2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2006 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.
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 error_check_format_type(GLcontext
*ctx
, GLenum format
, GLenum type
,
46 const char *readDraw
= drawing
? "Draw" : "Read";
48 if (ctx
->Extensions
.EXT_packed_depth_stencil
49 && type
== GL_UNSIGNED_INT_24_8_EXT
50 && format
!= GL_DEPTH_STENCIL_EXT
) {
51 _mesa_error(ctx
, GL_INVALID_OPERATION
,
52 "gl%sPixels(format is not GL_DEPTH_STENCIL_EXT)", readDraw
);
56 /* basic combinations test */
57 if (!_mesa_is_legal_format_and_type(ctx
, format
, type
)) {
58 _mesa_error(ctx
, GL_INVALID_ENUM
,
59 "gl%sPixels(format or type)", readDraw
);
63 /* additional checks */
70 case GL_LUMINANCE_ALPHA
:
76 if (drawing
&& !ctx
->Visual
.rgbMode
) {
77 _mesa_error(ctx
, GL_INVALID_OPERATION
,
78 "glDrawPixels(drawing RGB pixels into color index buffer)");
81 if (!drawing
&& !_mesa_dest_buffer_exists(ctx
, GL_COLOR
)) {
82 _mesa_error(ctx
, GL_INVALID_OPERATION
,
83 "glReadPixels(no color buffer)");
88 if (!drawing
&& ctx
->Visual
.rgbMode
) {
89 _mesa_error(ctx
, GL_INVALID_OPERATION
,
90 "glReadPixels(reading color index format from RGB buffer)");
93 if (!drawing
&& !_mesa_dest_buffer_exists(ctx
, GL_COLOR
)) {
94 _mesa_error(ctx
, GL_INVALID_OPERATION
,
95 "glReadPixels(no color buffer)");
99 case GL_STENCIL_INDEX
:
100 if ((drawing
&& !_mesa_dest_buffer_exists(ctx
, format
)) ||
101 (!drawing
&& !_mesa_source_buffer_exists(ctx
, format
))) {
102 _mesa_error(ctx
, GL_INVALID_OPERATION
,
103 "gl%sPixels(no stencil buffer)", readDraw
);
107 case GL_DEPTH_COMPONENT
:
108 if ((drawing
&& !_mesa_dest_buffer_exists(ctx
, format
)) ||
109 (!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]);
186 ctx
->Driver
.DrawPixels(ctx
, x
, y
, width
, height
, format
, type
,
187 &ctx
->Unpack
, pixels
);
189 else if (ctx
->RenderMode
== GL_FEEDBACK
) {
190 /* Feedback the current raster pos info */
191 FLUSH_CURRENT( ctx
, 0 );
192 FEEDBACK_TOKEN( ctx
, (GLfloat
) (GLint
) GL_DRAW_PIXEL_TOKEN
);
193 _mesa_feedback_vertex( ctx
,
194 ctx
->Current
.RasterPos
,
195 ctx
->Current
.RasterColor
,
196 ctx
->Current
.RasterIndex
,
197 ctx
->Current
.RasterTexCoords
[0] );
200 ASSERT(ctx
->RenderMode
== GL_SELECT
);
201 /* Do nothing. See OpenGL Spec, Appendix B, Corollary 6. */
207 _mesa_CopyPixels( GLint srcx
, GLint srcy
, GLsizei width
, GLsizei height
,
210 GET_CURRENT_CONTEXT(ctx
);
211 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
214 _mesa_update_state(ctx
);
217 if (ctx
->FragmentProgram
.Enabled
&& !ctx
->FragmentProgram
._Enabled
) {
218 _mesa_error(ctx
, GL_INVALID_OPERATION
,
219 "glCopyPixels (invalid fragment program)");
223 if (width
< 0 || height
< 0) {
224 _mesa_error(ctx
, GL_INVALID_VALUE
, "glCopyPixels(width or height < 0)");
228 if (ctx
->DrawBuffer
->_Status
!= GL_FRAMEBUFFER_COMPLETE_EXT
||
229 ctx
->ReadBuffer
->_Status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
230 _mesa_error(ctx
, GL_INVALID_FRAMEBUFFER_OPERATION_EXT
,
231 "glCopyPixels(incomplete framebuffer)" );
235 if (!_mesa_source_buffer_exists(ctx
, type
) ||
236 !_mesa_dest_buffer_exists(ctx
, type
)) {
237 _mesa_error(ctx
, GL_INVALID_OPERATION
,
238 "glCopyPixels(missing source or dest buffer)");
242 if (!ctx
->Current
.RasterPosValid
) {
246 if (ctx
->RenderMode
== GL_RENDER
) {
247 /* Round to satisfy conformance tests (matches SGI's OpenGL) */
248 GLint destx
= IROUND(ctx
->Current
.RasterPos
[0]);
249 GLint desty
= IROUND(ctx
->Current
.RasterPos
[1]);
250 ctx
->Driver
.CopyPixels( ctx
, srcx
, srcy
, width
, height
, destx
, desty
,
253 else if (ctx
->RenderMode
== GL_FEEDBACK
) {
254 FLUSH_CURRENT( ctx
, 0 );
255 FEEDBACK_TOKEN( ctx
, (GLfloat
) (GLint
) GL_COPY_PIXEL_TOKEN
);
256 _mesa_feedback_vertex( ctx
,
257 ctx
->Current
.RasterPos
,
258 ctx
->Current
.RasterColor
,
259 ctx
->Current
.RasterIndex
,
260 ctx
->Current
.RasterTexCoords
[0] );
263 ASSERT(ctx
->RenderMode
== GL_SELECT
);
264 /* Do nothing. See OpenGL Spec, Appendix B, Corollary 6. */
268 #endif /* _HAVE_FULL_GL */
273 _mesa_ReadPixels( GLint x
, GLint y
, GLsizei width
, GLsizei height
,
274 GLenum format
, GLenum type
, GLvoid
*pixels
)
276 GET_CURRENT_CONTEXT(ctx
);
277 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
279 if (width
< 0 || height
< 0) {
280 _mesa_error( ctx
, GL_INVALID_VALUE
,
281 "glReadPixels(width=%d height=%d)", width
, height
);
286 _mesa_update_state(ctx
);
288 if (error_check_format_type(ctx
, format
, type
, GL_FALSE
)) {
293 if (ctx
->ReadBuffer
->_Status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
294 _mesa_error(ctx
, GL_INVALID_FRAMEBUFFER_OPERATION_EXT
,
295 "glReadPixels(incomplete framebuffer)" );
299 if (!_mesa_source_buffer_exists(ctx
, format
)) {
300 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glReadPixels(no readbuffer)");
304 ctx
->Driver
.ReadPixels(ctx
, x
, y
, width
, height
,
305 format
, type
, &ctx
->Pack
, pixels
);
311 _mesa_Bitmap( GLsizei width
, GLsizei height
,
312 GLfloat xorig
, GLfloat yorig
, GLfloat xmove
, GLfloat ymove
,
313 const GLubyte
*bitmap
)
315 GET_CURRENT_CONTEXT(ctx
);
316 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
318 if (width
< 0 || height
< 0) {
319 _mesa_error( ctx
, GL_INVALID_VALUE
, "glBitmap(width or height < 0)" );
323 if (!ctx
->Current
.RasterPosValid
) {
324 return; /* do nothing */
328 _mesa_update_state(ctx
);
331 if (ctx
->FragmentProgram
.Enabled
&& !ctx
->FragmentProgram
._Enabled
) {
332 _mesa_error(ctx
, GL_INVALID_OPERATION
,
333 "glBitmap (invalid fragment program)");
337 if (ctx
->DrawBuffer
->_Status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
338 _mesa_error(ctx
, GL_INVALID_FRAMEBUFFER_OPERATION_EXT
,
339 "glBitmap(incomplete framebuffer)");
343 if (ctx
->RenderMode
== GL_RENDER
) {
345 /* Truncate, to satisfy conformance tests (matches SGI's OpenGL). */
346 GLint x
= IFLOOR(ctx
->Current
.RasterPos
[0] - xorig
);
347 GLint y
= IFLOOR(ctx
->Current
.RasterPos
[1] - yorig
);
348 ctx
->Driver
.Bitmap( ctx
, x
, y
, width
, height
, &ctx
->Unpack
, bitmap
);
352 else if (ctx
->RenderMode
== GL_FEEDBACK
) {
353 FLUSH_CURRENT(ctx
, 0);
354 FEEDBACK_TOKEN( ctx
, (GLfloat
) (GLint
) GL_BITMAP_TOKEN
);
355 _mesa_feedback_vertex( ctx
,
356 ctx
->Current
.RasterPos
,
357 ctx
->Current
.RasterColor
,
358 ctx
->Current
.RasterIndex
,
359 ctx
->Current
.RasterTexCoords
[0] );
362 ASSERT(ctx
->RenderMode
== GL_SELECT
);
363 /* Do nothing. See OpenGL Spec, Appendix B, Corollary 6. */
367 /* update raster position */
368 ctx
->Current
.RasterPos
[0] += xmove
;
369 ctx
->Current
.RasterPos
[1] += ymove
;
374 #if 0 /* experimental */
376 * Execute glDrawDepthPixelsMESA(). This function accepts both a color
377 * image and depth (Z) image. Rasterization produces fragments with
378 * color and Z taken from these images. This function is intended for
379 * Z-compositing. Normally, this operation requires two glDrawPixels
380 * calls with stencil testing.
383 _mesa_DrawDepthPixelsMESA( GLsizei width
, GLsizei height
,
384 GLenum colorFormat
, GLenum colorType
,
385 const GLvoid
*colors
,
386 GLenum depthType
, const GLvoid
*depths
)
388 GET_CURRENT_CONTEXT(ctx
);
389 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
391 if (width
< 0 || height
< 0) {
392 _mesa_error( ctx
, GL_INVALID_VALUE
,
393 "glDrawDepthPixelsMESA(width or height < 0" );
397 if (!ctx
->Current
.RasterPosValid
) {
402 _mesa_update_state(ctx
);
405 if (ctx
->DrawBuffer
->_Status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
406 _mesa_error(ctx
, GL_INVALID_FRAMEBUFFER_OPERATION_EXT
,
407 "glDrawDepthPixelsMESA(incomplete framebuffer)");
411 if (ctx
->RenderMode
== GL_RENDER
) {
412 /* Round, to satisfy conformance tests (matches SGI's OpenGL) */
413 GLint x
= IROUND(ctx
->Current
.RasterPos
[0]);
414 GLint y
= IROUND(ctx
->Current
.RasterPos
[1]);
415 ctx
->Driver
.DrawDepthPixelsMESA(ctx
, x
, y
, width
, height
,
416 colorFormat
, colorType
, colors
,
417 depthType
, depths
, &ctx
->Unpack
);
419 else if (ctx
->RenderMode
== GL_FEEDBACK
) {
420 /* Feedback the current raster pos info */
421 FLUSH_CURRENT( ctx
, 0 );
422 FEEDBACK_TOKEN( ctx
, (GLfloat
) (GLint
) GL_DRAW_PIXEL_TOKEN
);
423 _mesa_feedback_vertex( ctx
,
424 ctx
->Current
.RasterPos
,
425 ctx
->Current
.RasterColor
,
426 ctx
->Current
.RasterIndex
,
427 ctx
->Current
.RasterTexCoords
[0] );
430 ASSERT(ctx
->RenderMode
== GL_SELECT
);
431 /* Do nothing. See OpenGL Spec, Appendix B, Corollary 6. */