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"
32 #include "framebuffer.h"
36 #include "glapi/dispatch.h"
43 * If a fragment program is enabled, check that it's valid.
44 * \return GL_TRUE if valid, GL_FALSE otherwise
47 valid_fragment_program(GLcontext *ctx)
49 return !(ctx->FragmentProgram.Enabled && !ctx->FragmentProgram._Enabled);
54 * Execute glDrawPixels
56 static void GLAPIENTRY
57 _mesa_DrawPixels( GLsizei width, GLsizei height,
58 GLenum format, GLenum type, const GLvoid *pixels )
60 GET_CURRENT_CONTEXT(ctx);
61 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
63 if (width < 0 || height < 0) {
64 _mesa_error( ctx, GL_INVALID_VALUE, "glDrawPixels(width or height < 0" );
68 /* We're not using the current vertex program, and the driver may install
71 _mesa_set_vp_override(ctx, GL_TRUE);
74 _mesa_update_state(ctx);
77 if (!valid_fragment_program(ctx)) {
78 _mesa_error(ctx, GL_INVALID_OPERATION,
79 "glDrawPixels (invalid fragment program)");
83 if (_mesa_error_check_format_type(ctx, format, type, GL_TRUE)) {
84 /* the error was already recorded */
88 if (ctx->DrawBuffer->_Status != GL_FRAMEBUFFER_COMPLETE_EXT) {
89 _mesa_error(ctx, GL_INVALID_FRAMEBUFFER_OPERATION_EXT,
90 "glDrawPixels(incomplete framebuffer)" );
94 if (!ctx->Current.RasterPosValid) {
95 goto end; /* no-op, not an error */
98 if (ctx->RenderMode == GL_RENDER) {
99 if (width > 0 && height > 0) {
100 /* Round, to satisfy conformance tests (matches SGI's OpenGL) */
101 GLint x = IROUND(ctx->Current.RasterPos[0]);
102 GLint y = IROUND(ctx->Current.RasterPos[1]);
104 if (ctx->Unpack.BufferObj->Name) {
105 /* unpack from PBO */
106 if (!_mesa_validate_pbo_access(2, &ctx->Unpack, width, height, 1,
107 format, type, pixels)) {
108 _mesa_error(ctx, GL_INVALID_OPERATION,
109 "glDrawPixels(invalid PBO access)");
112 if (_mesa_bufferobj_mapped(ctx->Unpack.BufferObj)) {
113 /* buffer is mapped - that's an error */
114 _mesa_error(ctx, GL_INVALID_OPERATION,
115 "glDrawPixels(PBO is mapped)");
120 ctx->Driver.DrawPixels(ctx, x, y, width, height, format, type,
121 &ctx->Unpack, pixels);
124 else if (ctx->RenderMode == GL_FEEDBACK) {
125 /* Feedback the current raster pos info */
126 FLUSH_CURRENT( ctx, 0 );
127 _mesa_feedback_token( ctx, (GLfloat) (GLint) GL_DRAW_PIXEL_TOKEN );
128 _mesa_feedback_vertex( ctx,
129 ctx->Current.RasterPos,
130 ctx->Current.RasterColor,
131 ctx->Current.RasterIndex,
132 ctx->Current.RasterTexCoords[0] );
135 ASSERT(ctx->RenderMode == GL_SELECT);
136 /* Do nothing. See OpenGL Spec, Appendix B, Corollary 6. */
140 _mesa_set_vp_override(ctx, GL_FALSE);
144 static void GLAPIENTRY
145 _mesa_CopyPixels( GLint srcx, GLint srcy, GLsizei width, GLsizei height,
148 GET_CURRENT_CONTEXT(ctx);
149 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
151 if (width < 0 || height < 0) {
152 _mesa_error(ctx, GL_INVALID_VALUE, "glCopyPixels(width or height < 0)");
156 /* Note: more detailed 'type' checking is done by the
157 * _mesa_source/dest_buffer_exists() calls below. That's where we
158 * check if the stencil buffer exists, etc.
160 if (type != GL_COLOR &&
162 type != GL_STENCIL &&
163 type != GL_DEPTH_STENCIL) {
164 _mesa_error(ctx, GL_INVALID_ENUM, "glCopyPixels(type=%s)",
165 _mesa_lookup_enum_by_nr(type));
169 /* We're not using the current vertex program, and the driver may install
172 _mesa_set_vp_override(ctx, GL_TRUE);
175 _mesa_update_state(ctx);
178 if (!valid_fragment_program(ctx)) {
179 _mesa_error(ctx, GL_INVALID_OPERATION,
180 "glCopyPixels (invalid fragment program)");
184 if (ctx->DrawBuffer->_Status != GL_FRAMEBUFFER_COMPLETE_EXT ||
185 ctx->ReadBuffer->_Status != GL_FRAMEBUFFER_COMPLETE_EXT) {
186 _mesa_error(ctx, GL_INVALID_FRAMEBUFFER_OPERATION_EXT,
187 "glCopyPixels(incomplete framebuffer)" );
191 if (!_mesa_source_buffer_exists(ctx, type) ||
192 !_mesa_dest_buffer_exists(ctx, type)) {
193 _mesa_error(ctx, GL_INVALID_OPERATION,
194 "glCopyPixels(missing source or dest buffer)");
198 if (!ctx->Current.RasterPosValid || width ==0 || height == 0) {
199 goto end; /* no-op, not an error */
202 if (ctx->RenderMode == GL_RENDER) {
203 /* Round to satisfy conformance tests (matches SGI's OpenGL) */
204 if (width > 0 && height > 0) {
205 GLint destx = IROUND(ctx->Current.RasterPos[0]);
206 GLint desty = IROUND(ctx->Current.RasterPos[1]);
207 ctx->Driver.CopyPixels( ctx, srcx, srcy, width, height, destx, desty,
211 else if (ctx->RenderMode == GL_FEEDBACK) {
212 FLUSH_CURRENT( ctx, 0 );
213 _mesa_feedback_token( ctx, (GLfloat) (GLint) GL_COPY_PIXEL_TOKEN );
214 _mesa_feedback_vertex( ctx,
215 ctx->Current.RasterPos,
216 ctx->Current.RasterColor,
217 ctx->Current.RasterIndex,
218 ctx->Current.RasterTexCoords[0] );
221 ASSERT(ctx->RenderMode == GL_SELECT);
222 /* Do nothing. See OpenGL Spec, Appendix B, Corollary 6. */
226 _mesa_set_vp_override(ctx, GL_FALSE);
230 static void GLAPIENTRY
231 _mesa_Bitmap( GLsizei width, GLsizei height,
232 GLfloat xorig, GLfloat yorig, GLfloat xmove, GLfloat ymove,
233 const GLubyte *bitmap )
235 GET_CURRENT_CONTEXT(ctx);
236 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
238 if (width < 0 || height < 0) {
239 _mesa_error( ctx, GL_INVALID_VALUE, "glBitmap(width or height < 0)" );
243 if (!ctx->Current.RasterPosValid) {
244 return; /* do nothing */
248 _mesa_update_state(ctx);
251 if (!valid_fragment_program(ctx)) {
252 _mesa_error(ctx, GL_INVALID_OPERATION,
253 "glBitmap (invalid fragment program)");
257 if (ctx->DrawBuffer->_Status != GL_FRAMEBUFFER_COMPLETE_EXT) {
258 _mesa_error(ctx, GL_INVALID_FRAMEBUFFER_OPERATION_EXT,
259 "glBitmap(incomplete framebuffer)");
263 if (ctx->RenderMode == GL_RENDER) {
264 /* Truncate, to satisfy conformance tests (matches SGI's OpenGL). */
265 if (width > 0 && height > 0) {
266 const GLfloat epsilon = 0.0001F;
267 GLint x = IFLOOR(ctx->Current.RasterPos[0] + epsilon - xorig);
268 GLint y = IFLOOR(ctx->Current.RasterPos[1] + epsilon - yorig);
270 if (ctx->Unpack.BufferObj->Name) {
271 /* unpack from PBO */
272 if (!_mesa_validate_pbo_access(2, &ctx->Unpack, width, height, 1,
273 GL_COLOR_INDEX, GL_BITMAP,
274 (GLvoid *) bitmap)) {
275 _mesa_error(ctx, GL_INVALID_OPERATION,
276 "glBitmap(invalid PBO access)");
279 if (_mesa_bufferobj_mapped(ctx->Unpack.BufferObj)) {
280 /* buffer is mapped - that's an error */
281 _mesa_error(ctx, GL_INVALID_OPERATION,
282 "glBitmap(PBO is mapped)");
287 ctx->Driver.Bitmap( ctx, x, y, width, height, &ctx->Unpack, bitmap );
291 else if (ctx->RenderMode == GL_FEEDBACK) {
292 FLUSH_CURRENT(ctx, 0);
293 _mesa_feedback_token( ctx, (GLfloat) (GLint) GL_BITMAP_TOKEN );
294 _mesa_feedback_vertex( ctx,
295 ctx->Current.RasterPos,
296 ctx->Current.RasterColor,
297 ctx->Current.RasterIndex,
298 ctx->Current.RasterTexCoords[0] );
301 ASSERT(ctx->RenderMode == GL_SELECT);
302 /* Do nothing. See OpenGL Spec, Appendix B, Corollary 6. */
306 /* update raster position */
307 ctx->Current.RasterPos[0] += xmove;
308 ctx->Current.RasterPos[1] += ymove;
313 _mesa_init_drawpix_dispatch(struct _glapi_table *disp)
315 SET_Bitmap(disp, _mesa_Bitmap);
316 SET_CopyPixels(disp, _mesa_CopyPixels);
317 SET_DrawPixels(disp, _mesa_DrawPixels);
321 #endif /* FEATURE_drawpix */