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"
34 #include "mfeatures.h"
45 * Execute glDrawPixels
47 static void GLAPIENTRY
48 _mesa_DrawPixels( GLsizei width
, GLsizei height
,
49 GLenum format
, GLenum type
, const GLvoid
*pixels
)
51 GET_CURRENT_CONTEXT(ctx
);
52 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
54 if (MESA_VERBOSE
& VERBOSE_API
)
55 _mesa_debug(ctx
, "glDrawPixels(%d, %d, %s, %s, %p) // to %s at %d, %d\n",
57 _mesa_lookup_enum_by_nr(format
),
58 _mesa_lookup_enum_by_nr(type
),
60 _mesa_lookup_enum_by_nr(ctx
->DrawBuffer
->ColorDrawBuffer
),
61 IROUND(ctx
->Current
.RasterPos
[0]),
62 IROUND(ctx
->Current
.RasterPos
[1]));
65 if (width
< 0 || height
< 0) {
66 _mesa_error( ctx
, GL_INVALID_VALUE
, "glDrawPixels(width or height < 0)" );
70 /* Note: this call does state validation */
71 if (!_mesa_valid_to_render(ctx
, "glDrawPixels")) {
72 return; /* the error code was recorded */
75 /* GL 3.0 introduced a new restriction on glDrawPixels() over what was in
76 * GL_EXT_texture_integer. From section 3.7.4 ("Rasterization of Pixel
77 * Rectangles) on page 151 of the GL 3.0 specification:
79 * "If format contains integer components, as shown in table 3.6, an
80 * INVALID OPERATION error is generated."
82 * Since DrawPixels rendering would be merely undefined if not an error (due
83 * to a lack of defined mapping from integer data to gl_Color fragment shader
84 * input), NVIDIA's implementation also just returns this error despite
85 * exposing GL_EXT_texture_integer, just return an error regardless.
87 if (_mesa_is_integer_format(format
)) {
88 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glDrawPixels(integer format)");
92 if (_mesa_error_check_format_type(ctx
, format
, type
, GL_TRUE
)) {
93 return; /* the error code was recorded */
96 if (ctx
->RasterDiscard
) {
100 if (!ctx
->Current
.RasterPosValid
) {
101 return; /* no-op, not an error */
104 if (ctx
->RenderMode
== GL_RENDER
) {
105 if (width
> 0 && height
> 0) {
106 /* Round, to satisfy conformance tests (matches SGI's OpenGL) */
107 GLint x
= IROUND(ctx
->Current
.RasterPos
[0]);
108 GLint y
= IROUND(ctx
->Current
.RasterPos
[1]);
110 if (_mesa_is_bufferobj(ctx
->Unpack
.BufferObj
)) {
111 /* unpack from PBO */
112 if (!_mesa_validate_pbo_access(2, &ctx
->Unpack
, width
, height
,
113 1, format
, type
, INT_MAX
, pixels
)) {
114 _mesa_error(ctx
, GL_INVALID_OPERATION
,
115 "glDrawPixels(invalid PBO access)");
118 if (_mesa_bufferobj_mapped(ctx
->Unpack
.BufferObj
)) {
119 /* buffer is mapped - that's an error */
120 _mesa_error(ctx
, GL_INVALID_OPERATION
,
121 "glDrawPixels(PBO is mapped)");
126 ctx
->Driver
.DrawPixels(ctx
, x
, y
, width
, height
, format
, type
,
127 &ctx
->Unpack
, pixels
);
130 else if (ctx
->RenderMode
== GL_FEEDBACK
) {
131 /* Feedback the current raster pos info */
132 FLUSH_CURRENT( ctx
, 0 );
133 _mesa_feedback_token( ctx
, (GLfloat
) (GLint
) GL_DRAW_PIXEL_TOKEN
);
134 _mesa_feedback_vertex( ctx
,
135 ctx
->Current
.RasterPos
,
136 ctx
->Current
.RasterColor
,
137 ctx
->Current
.RasterTexCoords
);
140 ASSERT(ctx
->RenderMode
== GL_SELECT
);
141 /* Do nothing. See OpenGL Spec, Appendix B, Corollary 6. */
146 static void GLAPIENTRY
147 _mesa_CopyPixels( GLint srcx
, GLint srcy
, GLsizei width
, GLsizei height
,
150 GET_CURRENT_CONTEXT(ctx
);
151 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
153 if (MESA_VERBOSE
& VERBOSE_API
)
155 "glCopyPixels(%d, %d, %d, %d, %s) // from %s to %s at %d, %d\n",
156 srcx
, srcy
, width
, height
,
157 _mesa_lookup_enum_by_nr(type
),
158 _mesa_lookup_enum_by_nr(ctx
->ReadBuffer
->ColorReadBuffer
),
159 _mesa_lookup_enum_by_nr(ctx
->DrawBuffer
->ColorDrawBuffer
),
160 IROUND(ctx
->Current
.RasterPos
[0]),
161 IROUND(ctx
->Current
.RasterPos
[1]));
163 if (width
< 0 || height
< 0) {
164 _mesa_error(ctx
, GL_INVALID_VALUE
, "glCopyPixels(width or height < 0)");
168 /* Note: more detailed 'type' checking is done by the
169 * _mesa_source/dest_buffer_exists() calls below. That's where we
170 * check if the stencil buffer exists, etc.
172 if (type
!= GL_COLOR
&&
174 type
!= GL_STENCIL
) {
175 _mesa_error(ctx
, GL_INVALID_ENUM
, "glCopyPixels(type=%s)",
176 _mesa_lookup_enum_by_nr(type
));
180 /* Note: this call does state validation */
181 if (!_mesa_valid_to_render(ctx
, "glCopyPixels")) {
182 return; /* the error code was recorded */
185 if (!_mesa_source_buffer_exists(ctx
, type
) ||
186 !_mesa_dest_buffer_exists(ctx
, type
)) {
187 _mesa_error(ctx
, GL_INVALID_OPERATION
,
188 "glCopyPixels(missing source or dest buffer)");
192 if (ctx
->RasterDiscard
) {
196 if (!ctx
->Current
.RasterPosValid
|| width
== 0 || height
== 0) {
197 return; /* no-op, not an error */
200 if (ctx
->RenderMode
== GL_RENDER
) {
201 /* Round to satisfy conformance tests (matches SGI's OpenGL) */
202 if (width
> 0 && height
> 0) {
203 GLint destx
= IROUND(ctx
->Current
.RasterPos
[0]);
204 GLint desty
= IROUND(ctx
->Current
.RasterPos
[1]);
205 ctx
->Driver
.CopyPixels( ctx
, srcx
, srcy
, width
, height
, destx
, desty
,
209 else if (ctx
->RenderMode
== GL_FEEDBACK
) {
210 FLUSH_CURRENT( ctx
, 0 );
211 _mesa_feedback_token( ctx
, (GLfloat
) (GLint
) GL_COPY_PIXEL_TOKEN
);
212 _mesa_feedback_vertex( ctx
,
213 ctx
->Current
.RasterPos
,
214 ctx
->Current
.RasterColor
,
215 ctx
->Current
.RasterTexCoords
);
218 ASSERT(ctx
->RenderMode
== GL_SELECT
);
219 /* Do nothing. See OpenGL Spec, Appendix B, Corollary 6. */
224 static void GLAPIENTRY
225 _mesa_Bitmap( GLsizei width
, GLsizei height
,
226 GLfloat xorig
, GLfloat yorig
, GLfloat xmove
, GLfloat ymove
,
227 const GLubyte
*bitmap
)
229 GET_CURRENT_CONTEXT(ctx
);
230 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
232 if (width
< 0 || height
< 0) {
233 _mesa_error( ctx
, GL_INVALID_VALUE
, "glBitmap(width or height < 0)" );
237 if (!ctx
->Current
.RasterPosValid
) {
238 return; /* do nothing */
241 /* Note: this call does state validation */
242 if (!_mesa_valid_to_render(ctx
, "glBitmap")) {
243 /* the error code was recorded */
247 if (ctx
->RasterDiscard
)
250 if (ctx
->RenderMode
== GL_RENDER
) {
251 /* Truncate, to satisfy conformance tests (matches SGI's OpenGL). */
252 if (width
> 0 && height
> 0) {
253 const GLfloat epsilon
= 0.0001F
;
254 GLint x
= IFLOOR(ctx
->Current
.RasterPos
[0] + epsilon
- xorig
);
255 GLint y
= IFLOOR(ctx
->Current
.RasterPos
[1] + epsilon
- yorig
);
257 if (_mesa_is_bufferobj(ctx
->Unpack
.BufferObj
)) {
258 /* unpack from PBO */
259 if (!_mesa_validate_pbo_access(2, &ctx
->Unpack
, width
, height
,
260 1, GL_COLOR_INDEX
, GL_BITMAP
,
261 INT_MAX
, (const GLvoid
*) bitmap
)) {
262 _mesa_error(ctx
, GL_INVALID_OPERATION
,
263 "glBitmap(invalid PBO access)");
266 if (_mesa_bufferobj_mapped(ctx
->Unpack
.BufferObj
)) {
267 /* buffer is mapped - that's an error */
268 _mesa_error(ctx
, GL_INVALID_OPERATION
,
269 "glBitmap(PBO is mapped)");
274 ctx
->Driver
.Bitmap( ctx
, x
, y
, width
, height
, &ctx
->Unpack
, bitmap
);
278 else if (ctx
->RenderMode
== GL_FEEDBACK
) {
279 FLUSH_CURRENT(ctx
, 0);
280 _mesa_feedback_token( ctx
, (GLfloat
) (GLint
) GL_BITMAP_TOKEN
);
281 _mesa_feedback_vertex( ctx
,
282 ctx
->Current
.RasterPos
,
283 ctx
->Current
.RasterColor
,
284 ctx
->Current
.RasterTexCoords
);
287 ASSERT(ctx
->RenderMode
== GL_SELECT
);
288 /* Do nothing. See OpenGL Spec, Appendix B, Corollary 6. */
292 /* update raster position */
293 ctx
->Current
.RasterPos
[0] += xmove
;
294 ctx
->Current
.RasterPos
[1] += ymove
;
299 _mesa_init_drawpix_dispatch(struct _glapi_table
*disp
)
301 SET_Bitmap(disp
, _mesa_Bitmap
);
302 SET_CopyPixels(disp
, _mesa_CopyPixels
);
303 SET_DrawPixels(disp
, _mesa_DrawPixels
);
307 #endif /* FEATURE_drawpix */