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"
38 #include "glformats.h"
43 * Execute glDrawPixels
45 static void GLAPIENTRY
46 _mesa_DrawPixels( GLsizei width
, GLsizei height
,
47 GLenum format
, GLenum type
, const GLvoid
*pixels
)
50 GET_CURRENT_CONTEXT(ctx
);
51 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
53 if (MESA_VERBOSE
& VERBOSE_API
)
54 _mesa_debug(ctx
, "glDrawPixels(%d, %d, %s, %s, %p) // to %s at %d, %d\n",
56 _mesa_lookup_enum_by_nr(format
),
57 _mesa_lookup_enum_by_nr(type
),
59 _mesa_lookup_enum_by_nr(ctx
->DrawBuffer
->ColorDrawBuffer
[0]),
60 IROUND(ctx
->Current
.RasterPos
[0]),
61 IROUND(ctx
->Current
.RasterPos
[1]));
64 if (width
< 0 || height
< 0) {
65 _mesa_error( ctx
, GL_INVALID_VALUE
, "glDrawPixels(width or height < 0)" );
69 /* We're not using the current vertex program, and the driver may install
70 * its own. Note: this may dirty some state.
72 _mesa_set_vp_override(ctx
, GL_TRUE
);
74 /* Note: this call does state validation */
75 if (!_mesa_valid_to_render(ctx
, "glDrawPixels")) {
76 goto end
; /* the error code was recorded */
79 /* GL 3.0 introduced a new restriction on glDrawPixels() over what was in
80 * GL_EXT_texture_integer. From section 3.7.4 ("Rasterization of Pixel
81 * Rectangles) on page 151 of the GL 3.0 specification:
83 * "If format contains integer components, as shown in table 3.6, an
84 * INVALID OPERATION error is generated."
86 * Since DrawPixels rendering would be merely undefined if not an error (due
87 * to a lack of defined mapping from integer data to gl_Color fragment shader
88 * input), NVIDIA's implementation also just returns this error despite
89 * exposing GL_EXT_texture_integer, just return an error regardless.
91 if (_mesa_is_enum_format_integer(format
)) {
92 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glDrawPixels(integer format)");
96 err
= _mesa_error_check_format_and_type(ctx
, format
, type
);
97 if (err
!= GL_NO_ERROR
) {
98 _mesa_error(ctx
, err
, "glDrawPixels(invalid format %s and/or type %s)",
99 _mesa_lookup_enum_by_nr(format
),
100 _mesa_lookup_enum_by_nr(type
));
104 /* do special format-related checks */
106 case GL_STENCIL_INDEX
:
107 case GL_DEPTH_COMPONENT
:
108 case GL_DEPTH_STENCIL_EXT
:
109 /* these buffers must exist */
110 if (!_mesa_dest_buffer_exists(ctx
, format
)) {
111 _mesa_error(ctx
, GL_INVALID_OPERATION
,
112 "glDrawPixels(missing deest buffer)");
117 if (ctx
->PixelMaps
.ItoR
.Size
== 0 ||
118 ctx
->PixelMaps
.ItoG
.Size
== 0 ||
119 ctx
->PixelMaps
.ItoB
.Size
== 0) {
120 _mesa_error(ctx
, GL_INVALID_OPERATION
,
121 "glDrawPixels(drawing color index pixels into RGB buffer)");
126 /* for color formats it's not an error if the destination color
127 * buffer doesn't exist.
132 if (ctx
->RasterDiscard
) {
136 if (!ctx
->Current
.RasterPosValid
) {
137 goto end
; /* no-op, not an error */
140 if (ctx
->RenderMode
== GL_RENDER
) {
141 if (width
> 0 && height
> 0) {
142 /* Round, to satisfy conformance tests (matches SGI's OpenGL) */
143 GLint x
= IROUND(ctx
->Current
.RasterPos
[0]);
144 GLint y
= IROUND(ctx
->Current
.RasterPos
[1]);
146 if (_mesa_is_bufferobj(ctx
->Unpack
.BufferObj
)) {
147 /* unpack from PBO */
148 if (!_mesa_validate_pbo_access(2, &ctx
->Unpack
, width
, height
,
149 1, format
, type
, INT_MAX
, pixels
)) {
150 _mesa_error(ctx
, GL_INVALID_OPERATION
,
151 "glDrawPixels(invalid PBO access)");
154 if (_mesa_bufferobj_mapped(ctx
->Unpack
.BufferObj
)) {
155 /* buffer is mapped - that's an error */
156 _mesa_error(ctx
, GL_INVALID_OPERATION
,
157 "glDrawPixels(PBO is mapped)");
162 ctx
->Driver
.DrawPixels(ctx
, x
, y
, width
, height
, format
, type
,
163 &ctx
->Unpack
, pixels
);
166 else if (ctx
->RenderMode
== GL_FEEDBACK
) {
167 /* Feedback the current raster pos info */
168 FLUSH_CURRENT( ctx
, 0 );
169 _mesa_feedback_token( ctx
, (GLfloat
) (GLint
) GL_DRAW_PIXEL_TOKEN
);
170 _mesa_feedback_vertex( ctx
,
171 ctx
->Current
.RasterPos
,
172 ctx
->Current
.RasterColor
,
173 ctx
->Current
.RasterTexCoords
[0] );
176 ASSERT(ctx
->RenderMode
== GL_SELECT
);
177 /* Do nothing. See OpenGL Spec, Appendix B, Corollary 6. */
181 _mesa_set_vp_override(ctx
, GL_FALSE
);
183 if (MESA_DEBUG_FLAGS
& DEBUG_ALWAYS_FLUSH
) {
189 static void GLAPIENTRY
190 _mesa_CopyPixels( GLint srcx
, GLint srcy
, GLsizei width
, GLsizei height
,
193 GET_CURRENT_CONTEXT(ctx
);
194 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
196 if (MESA_VERBOSE
& VERBOSE_API
)
198 "glCopyPixels(%d, %d, %d, %d, %s) // from %s to %s at %d, %d\n",
199 srcx
, srcy
, width
, height
,
200 _mesa_lookup_enum_by_nr(type
),
201 _mesa_lookup_enum_by_nr(ctx
->ReadBuffer
->ColorReadBuffer
),
202 _mesa_lookup_enum_by_nr(ctx
->DrawBuffer
->ColorDrawBuffer
[0]),
203 IROUND(ctx
->Current
.RasterPos
[0]),
204 IROUND(ctx
->Current
.RasterPos
[1]));
206 if (width
< 0 || height
< 0) {
207 _mesa_error(ctx
, GL_INVALID_VALUE
, "glCopyPixels(width or height < 0)");
211 /* Note: more detailed 'type' checking is done by the
212 * _mesa_source/dest_buffer_exists() calls below. That's where we
213 * check if the stencil buffer exists, etc.
215 if (type
!= GL_COLOR
&&
217 type
!= GL_STENCIL
&&
218 type
!= GL_DEPTH_STENCIL
) {
219 _mesa_error(ctx
, GL_INVALID_ENUM
, "glCopyPixels(type=%s)",
220 _mesa_lookup_enum_by_nr(type
));
224 /* We're not using the current vertex program, and the driver may install
225 * it's own. Note: this may dirty some state.
227 _mesa_set_vp_override(ctx
, GL_TRUE
);
229 /* Note: this call does state validation */
230 if (!_mesa_valid_to_render(ctx
, "glCopyPixels")) {
231 goto end
; /* the error code was recorded */
234 /* Check read buffer's status (draw buffer was already checked) */
235 if (ctx
->ReadBuffer
->_Status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
236 _mesa_error(ctx
, GL_INVALID_FRAMEBUFFER_OPERATION_EXT
,
237 "glCopyPixels(incomplete framebuffer)" );
241 if (_mesa_is_user_fbo(ctx
->ReadBuffer
) &&
242 ctx
->ReadBuffer
->Visual
.samples
> 0) {
243 _mesa_error(ctx
, GL_INVALID_OPERATION
,
244 "glCopyPixels(multisample FBO)");
248 if (!_mesa_source_buffer_exists(ctx
, type
) ||
249 !_mesa_dest_buffer_exists(ctx
, type
)) {
250 _mesa_error(ctx
, GL_INVALID_OPERATION
,
251 "glCopyPixels(missing source or dest buffer)");
255 if (ctx
->RasterDiscard
) {
259 if (!ctx
->Current
.RasterPosValid
|| width
== 0 || height
== 0) {
260 goto end
; /* no-op, not an error */
263 if (ctx
->RenderMode
== GL_RENDER
) {
264 /* Round to satisfy conformance tests (matches SGI's OpenGL) */
265 if (width
> 0 && height
> 0) {
266 GLint destx
= IROUND(ctx
->Current
.RasterPos
[0]);
267 GLint desty
= IROUND(ctx
->Current
.RasterPos
[1]);
268 ctx
->Driver
.CopyPixels( ctx
, srcx
, srcy
, width
, height
, destx
, desty
,
272 else if (ctx
->RenderMode
== GL_FEEDBACK
) {
273 FLUSH_CURRENT( ctx
, 0 );
274 _mesa_feedback_token( ctx
, (GLfloat
) (GLint
) GL_COPY_PIXEL_TOKEN
);
275 _mesa_feedback_vertex( ctx
,
276 ctx
->Current
.RasterPos
,
277 ctx
->Current
.RasterColor
,
278 ctx
->Current
.RasterTexCoords
[0] );
281 ASSERT(ctx
->RenderMode
== GL_SELECT
);
282 /* Do nothing. See OpenGL Spec, Appendix B, Corollary 6. */
286 _mesa_set_vp_override(ctx
, GL_FALSE
);
288 if (MESA_DEBUG_FLAGS
& DEBUG_ALWAYS_FLUSH
) {
294 static void GLAPIENTRY
295 _mesa_Bitmap( GLsizei width
, GLsizei height
,
296 GLfloat xorig
, GLfloat yorig
, GLfloat xmove
, GLfloat ymove
,
297 const GLubyte
*bitmap
)
299 GET_CURRENT_CONTEXT(ctx
);
300 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
302 if (width
< 0 || height
< 0) {
303 _mesa_error( ctx
, GL_INVALID_VALUE
, "glBitmap(width or height < 0)" );
307 if (!ctx
->Current
.RasterPosValid
) {
308 return; /* do nothing */
311 /* Note: this call does state validation */
312 if (!_mesa_valid_to_render(ctx
, "glBitmap")) {
313 /* the error code was recorded */
317 if (ctx
->RasterDiscard
)
320 if (ctx
->RenderMode
== GL_RENDER
) {
321 /* Truncate, to satisfy conformance tests (matches SGI's OpenGL). */
322 if (width
> 0 && height
> 0) {
323 const GLfloat epsilon
= 0.0001F
;
324 GLint x
= IFLOOR(ctx
->Current
.RasterPos
[0] + epsilon
- xorig
);
325 GLint y
= IFLOOR(ctx
->Current
.RasterPos
[1] + epsilon
- yorig
);
327 if (_mesa_is_bufferobj(ctx
->Unpack
.BufferObj
)) {
328 /* unpack from PBO */
329 if (!_mesa_validate_pbo_access(2, &ctx
->Unpack
, width
, height
,
330 1, GL_COLOR_INDEX
, GL_BITMAP
,
331 INT_MAX
, (const GLvoid
*) bitmap
)) {
332 _mesa_error(ctx
, GL_INVALID_OPERATION
,
333 "glBitmap(invalid PBO access)");
336 if (_mesa_bufferobj_mapped(ctx
->Unpack
.BufferObj
)) {
337 /* buffer is mapped - that's an error */
338 _mesa_error(ctx
, GL_INVALID_OPERATION
,
339 "glBitmap(PBO is mapped)");
344 ctx
->Driver
.Bitmap( ctx
, x
, y
, width
, height
, &ctx
->Unpack
, bitmap
);
347 else if (ctx
->RenderMode
== GL_FEEDBACK
) {
348 FLUSH_CURRENT(ctx
, 0);
349 _mesa_feedback_token( ctx
, (GLfloat
) (GLint
) GL_BITMAP_TOKEN
);
350 _mesa_feedback_vertex( ctx
,
351 ctx
->Current
.RasterPos
,
352 ctx
->Current
.RasterColor
,
353 ctx
->Current
.RasterTexCoords
[0] );
356 ASSERT(ctx
->RenderMode
== GL_SELECT
);
357 /* Do nothing. See OpenGL Spec, Appendix B, Corollary 6. */
360 /* update raster position */
361 ctx
->Current
.RasterPos
[0] += xmove
;
362 ctx
->Current
.RasterPos
[1] += ymove
;
364 if (MESA_DEBUG_FLAGS
& DEBUG_ALWAYS_FLUSH
) {
371 _mesa_init_drawpix_dispatch(struct _glapi_table
*disp
)
373 SET_Bitmap(disp
, _mesa_Bitmap
);
374 SET_CopyPixels(disp
, _mesa_CopyPixels
);
375 SET_DrawPixels(disp
, _mesa_DrawPixels
);