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"
45 * Execute glDrawPixels
47 static void GLAPIENTRY
48 _mesa_DrawPixels( GLsizei width
, GLsizei height
,
49 GLenum format
, GLenum type
, const GLvoid
*pixels
)
52 GET_CURRENT_CONTEXT(ctx
);
53 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
55 if (MESA_VERBOSE
& VERBOSE_API
)
56 _mesa_debug(ctx
, "glDrawPixels(%d, %d, %s, %s, %p) // to %s at %d, %d\n",
58 _mesa_lookup_enum_by_nr(format
),
59 _mesa_lookup_enum_by_nr(type
),
61 _mesa_lookup_enum_by_nr(ctx
->DrawBuffer
->ColorDrawBuffer
[0]),
62 IROUND(ctx
->Current
.RasterPos
[0]),
63 IROUND(ctx
->Current
.RasterPos
[1]));
66 if (width
< 0 || height
< 0) {
67 _mesa_error( ctx
, GL_INVALID_VALUE
, "glDrawPixels(width or height < 0)" );
71 /* We're not using the current vertex program, and the driver may install
72 * its own. Note: this may dirty some state.
74 _mesa_set_vp_override(ctx
, GL_TRUE
);
76 /* Note: this call does state validation */
77 if (!_mesa_valid_to_render(ctx
, "glDrawPixels")) {
78 goto end
; /* the error code was recorded */
81 /* GL 3.0 introduced a new restriction on glDrawPixels() over what was in
82 * GL_EXT_texture_integer. From section 3.7.4 ("Rasterization of Pixel
83 * Rectangles) on page 151 of the GL 3.0 specification:
85 * "If format contains integer components, as shown in table 3.6, an
86 * INVALID OPERATION error is generated."
88 * Since DrawPixels rendering would be merely undefined if not an error (due
89 * to a lack of defined mapping from integer data to gl_Color fragment shader
90 * input), NVIDIA's implementation also just returns this error despite
91 * exposing GL_EXT_texture_integer, just return an error regardless.
93 if (_mesa_is_enum_format_integer(format
)) {
94 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glDrawPixels(integer format)");
98 err
= _mesa_error_check_format_and_type(ctx
, format
, type
);
99 if (err
!= GL_NO_ERROR
) {
100 _mesa_error(ctx
, err
, "glDrawPixels(invalid format %s and/or type %s)",
101 _mesa_lookup_enum_by_nr(format
),
102 _mesa_lookup_enum_by_nr(type
));
106 /* do special format-related checks */
108 case GL_STENCIL_INDEX
:
109 case GL_DEPTH_COMPONENT
:
110 case GL_DEPTH_STENCIL_EXT
:
111 /* these buffers must exist */
112 if (!_mesa_dest_buffer_exists(ctx
, format
)) {
113 _mesa_error(ctx
, GL_INVALID_OPERATION
,
114 "glDrawPixels(missing deest buffer)");
119 if (ctx
->PixelMaps
.ItoR
.Size
== 0 ||
120 ctx
->PixelMaps
.ItoG
.Size
== 0 ||
121 ctx
->PixelMaps
.ItoB
.Size
== 0) {
122 _mesa_error(ctx
, GL_INVALID_OPERATION
,
123 "glDrawPixels(drawing color index pixels into RGB buffer)");
128 /* for color formats it's not an error if the destination color
129 * buffer doesn't exist.
134 if (ctx
->RasterDiscard
) {
138 if (!ctx
->Current
.RasterPosValid
) {
139 goto end
; /* no-op, not an error */
142 if (ctx
->RenderMode
== GL_RENDER
) {
143 if (width
> 0 && height
> 0) {
144 /* Round, to satisfy conformance tests (matches SGI's OpenGL) */
145 GLint x
= IROUND(ctx
->Current
.RasterPos
[0]);
146 GLint y
= IROUND(ctx
->Current
.RasterPos
[1]);
148 if (_mesa_is_bufferobj(ctx
->Unpack
.BufferObj
)) {
149 /* unpack from PBO */
150 if (!_mesa_validate_pbo_access(2, &ctx
->Unpack
, width
, height
,
151 1, format
, type
, INT_MAX
, pixels
)) {
152 _mesa_error(ctx
, GL_INVALID_OPERATION
,
153 "glDrawPixels(invalid PBO access)");
156 if (_mesa_bufferobj_mapped(ctx
->Unpack
.BufferObj
)) {
157 /* buffer is mapped - that's an error */
158 _mesa_error(ctx
, GL_INVALID_OPERATION
,
159 "glDrawPixels(PBO is mapped)");
164 ctx
->Driver
.DrawPixels(ctx
, x
, y
, width
, height
, format
, type
,
165 &ctx
->Unpack
, pixels
);
168 else if (ctx
->RenderMode
== GL_FEEDBACK
) {
169 /* Feedback the current raster pos info */
170 FLUSH_CURRENT( ctx
, 0 );
171 _mesa_feedback_token( ctx
, (GLfloat
) (GLint
) GL_DRAW_PIXEL_TOKEN
);
172 _mesa_feedback_vertex( ctx
,
173 ctx
->Current
.RasterPos
,
174 ctx
->Current
.RasterColor
,
175 ctx
->Current
.RasterTexCoords
[0] );
178 ASSERT(ctx
->RenderMode
== GL_SELECT
);
179 /* Do nothing. See OpenGL Spec, Appendix B, Corollary 6. */
183 _mesa_set_vp_override(ctx
, GL_FALSE
);
185 if (MESA_DEBUG_FLAGS
& DEBUG_ALWAYS_FLUSH
) {
191 static void GLAPIENTRY
192 _mesa_CopyPixels( GLint srcx
, GLint srcy
, GLsizei width
, GLsizei height
,
195 GET_CURRENT_CONTEXT(ctx
);
196 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
198 if (MESA_VERBOSE
& VERBOSE_API
)
200 "glCopyPixels(%d, %d, %d, %d, %s) // from %s to %s at %d, %d\n",
201 srcx
, srcy
, width
, height
,
202 _mesa_lookup_enum_by_nr(type
),
203 _mesa_lookup_enum_by_nr(ctx
->ReadBuffer
->ColorReadBuffer
),
204 _mesa_lookup_enum_by_nr(ctx
->DrawBuffer
->ColorDrawBuffer
[0]),
205 IROUND(ctx
->Current
.RasterPos
[0]),
206 IROUND(ctx
->Current
.RasterPos
[1]));
208 if (width
< 0 || height
< 0) {
209 _mesa_error(ctx
, GL_INVALID_VALUE
, "glCopyPixels(width or height < 0)");
213 /* Note: more detailed 'type' checking is done by the
214 * _mesa_source/dest_buffer_exists() calls below. That's where we
215 * check if the stencil buffer exists, etc.
217 if (type
!= GL_COLOR
&&
219 type
!= GL_STENCIL
&&
220 type
!= GL_DEPTH_STENCIL
) {
221 _mesa_error(ctx
, GL_INVALID_ENUM
, "glCopyPixels(type=%s)",
222 _mesa_lookup_enum_by_nr(type
));
226 /* We're not using the current vertex program, and the driver may install
227 * it's own. Note: this may dirty some state.
229 _mesa_set_vp_override(ctx
, GL_TRUE
);
231 /* Note: this call does state validation */
232 if (!_mesa_valid_to_render(ctx
, "glCopyPixels")) {
233 goto end
; /* the error code was recorded */
236 /* Check read buffer's status (draw buffer was already checked) */
237 if (ctx
->ReadBuffer
->_Status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
238 _mesa_error(ctx
, GL_INVALID_FRAMEBUFFER_OPERATION_EXT
,
239 "glCopyPixels(incomplete framebuffer)" );
243 if (ctx
->ReadBuffer
->Name
!= 0 && ctx
->ReadBuffer
->Visual
.samples
> 0) {
244 _mesa_error(ctx
, GL_INVALID_OPERATION
,
245 "glCopyPixels(multisample FBO)");
249 if (!_mesa_source_buffer_exists(ctx
, type
) ||
250 !_mesa_dest_buffer_exists(ctx
, type
)) {
251 _mesa_error(ctx
, GL_INVALID_OPERATION
,
252 "glCopyPixels(missing source or dest buffer)");
256 if (ctx
->RasterDiscard
) {
260 if (!ctx
->Current
.RasterPosValid
|| width
== 0 || height
== 0) {
261 goto end
; /* no-op, not an error */
264 if (ctx
->RenderMode
== GL_RENDER
) {
265 /* Round to satisfy conformance tests (matches SGI's OpenGL) */
266 if (width
> 0 && height
> 0) {
267 GLint destx
= IROUND(ctx
->Current
.RasterPos
[0]);
268 GLint desty
= IROUND(ctx
->Current
.RasterPos
[1]);
269 ctx
->Driver
.CopyPixels( ctx
, srcx
, srcy
, width
, height
, destx
, desty
,
273 else if (ctx
->RenderMode
== GL_FEEDBACK
) {
274 FLUSH_CURRENT( ctx
, 0 );
275 _mesa_feedback_token( ctx
, (GLfloat
) (GLint
) GL_COPY_PIXEL_TOKEN
);
276 _mesa_feedback_vertex( ctx
,
277 ctx
->Current
.RasterPos
,
278 ctx
->Current
.RasterColor
,
279 ctx
->Current
.RasterTexCoords
[0] );
282 ASSERT(ctx
->RenderMode
== GL_SELECT
);
283 /* Do nothing. See OpenGL Spec, Appendix B, Corollary 6. */
287 _mesa_set_vp_override(ctx
, GL_FALSE
);
289 if (MESA_DEBUG_FLAGS
& DEBUG_ALWAYS_FLUSH
) {
295 static void GLAPIENTRY
296 _mesa_Bitmap( GLsizei width
, GLsizei height
,
297 GLfloat xorig
, GLfloat yorig
, GLfloat xmove
, GLfloat ymove
,
298 const GLubyte
*bitmap
)
300 GET_CURRENT_CONTEXT(ctx
);
301 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
303 if (width
< 0 || height
< 0) {
304 _mesa_error( ctx
, GL_INVALID_VALUE
, "glBitmap(width or height < 0)" );
308 if (!ctx
->Current
.RasterPosValid
) {
309 return; /* do nothing */
312 /* Note: this call does state validation */
313 if (!_mesa_valid_to_render(ctx
, "glBitmap")) {
314 /* the error code was recorded */
318 if (ctx
->RasterDiscard
)
321 if (ctx
->RenderMode
== GL_RENDER
) {
322 /* Truncate, to satisfy conformance tests (matches SGI's OpenGL). */
323 if (width
> 0 && height
> 0) {
324 const GLfloat epsilon
= 0.0001F
;
325 GLint x
= IFLOOR(ctx
->Current
.RasterPos
[0] + epsilon
- xorig
);
326 GLint y
= IFLOOR(ctx
->Current
.RasterPos
[1] + epsilon
- yorig
);
328 if (_mesa_is_bufferobj(ctx
->Unpack
.BufferObj
)) {
329 /* unpack from PBO */
330 if (!_mesa_validate_pbo_access(2, &ctx
->Unpack
, width
, height
,
331 1, GL_COLOR_INDEX
, GL_BITMAP
,
332 INT_MAX
, (const GLvoid
*) bitmap
)) {
333 _mesa_error(ctx
, GL_INVALID_OPERATION
,
334 "glBitmap(invalid PBO access)");
337 if (_mesa_bufferobj_mapped(ctx
->Unpack
.BufferObj
)) {
338 /* buffer is mapped - that's an error */
339 _mesa_error(ctx
, GL_INVALID_OPERATION
,
340 "glBitmap(PBO is mapped)");
345 ctx
->Driver
.Bitmap( ctx
, x
, y
, width
, height
, &ctx
->Unpack
, bitmap
);
349 else if (ctx
->RenderMode
== GL_FEEDBACK
) {
350 FLUSH_CURRENT(ctx
, 0);
351 _mesa_feedback_token( ctx
, (GLfloat
) (GLint
) GL_BITMAP_TOKEN
);
352 _mesa_feedback_vertex( ctx
,
353 ctx
->Current
.RasterPos
,
354 ctx
->Current
.RasterColor
,
355 ctx
->Current
.RasterTexCoords
[0] );
358 ASSERT(ctx
->RenderMode
== GL_SELECT
);
359 /* Do nothing. See OpenGL Spec, Appendix B, Corollary 6. */
363 /* update raster position */
364 ctx
->Current
.RasterPos
[0] += xmove
;
365 ctx
->Current
.RasterPos
[1] += ymove
;
367 if (MESA_DEBUG_FLAGS
& DEBUG_ALWAYS_FLUSH
) {
374 _mesa_init_drawpix_dispatch(struct _glapi_table
*disp
)
376 SET_Bitmap(disp
, _mesa_Bitmap
);
377 SET_CopyPixels(disp
, _mesa_CopyPixels
);
378 SET_DrawPixels(disp
, _mesa_DrawPixels
);
382 #endif /* FEATURE_drawpix */