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"
44 * Execute glDrawPixels
46 static void GLAPIENTRY
47 _mesa_DrawPixels( GLsizei width
, GLsizei height
,
48 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
[0]),
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 /* We're not using the current vertex program, and the driver may install
71 * its own. Note: this may dirty some state.
73 _mesa_set_vp_override(ctx
, GL_TRUE
);
75 /* Note: this call does state validation */
76 if (!_mesa_valid_to_render(ctx
, "glDrawPixels")) {
77 goto end
; /* the error code was recorded */
80 /* GL 3.0 introduced a new restriction on glDrawPixels() over what was in
81 * GL_EXT_texture_integer. From section 3.7.4 ("Rasterization of Pixel
82 * Rectangles) on page 151 of the GL 3.0 specification:
84 * "If format contains integer components, as shown in table 3.6, an
85 * INVALID OPERATION error is generated."
87 * Since DrawPixels rendering would be merely undefined if not an error (due
88 * to a lack of defined mapping from integer data to gl_Color fragment shader
89 * input), NVIDIA's implementation also just returns this error despite
90 * exposing GL_EXT_texture_integer, just return an error regardless.
92 if (_mesa_is_integer_format(format
)) {
93 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glDrawPixels(integer format)");
97 err
= _mesa_error_check_format_and_type(ctx
, format
, type
);
98 if (err
!= GL_NO_ERROR
) {
99 _mesa_error(ctx
, err
, "glDrawPixels(invalid format %s and/or type %s)",
100 _mesa_lookup_enum_by_nr(format
),
101 _mesa_lookup_enum_by_nr(type
));
105 /* do special format-related checks */
107 case GL_STENCIL_INDEX
:
108 case GL_DEPTH_COMPONENT
:
109 case GL_DEPTH_STENCIL_EXT
:
110 /* these buffers must exist */
111 if (!_mesa_dest_buffer_exists(ctx
, format
)) {
112 _mesa_error(ctx
, GL_INVALID_OPERATION
,
113 "glDrawPixels(missing deest buffer)");
118 if (ctx
->PixelMaps
.ItoR
.Size
== 0 ||
119 ctx
->PixelMaps
.ItoG
.Size
== 0 ||
120 ctx
->PixelMaps
.ItoB
.Size
== 0) {
121 _mesa_error(ctx
, GL_INVALID_OPERATION
,
122 "glDrawPixels(drawing color index pixels into RGB buffer)");
127 /* for color formats it's not an error if the destination color
128 * buffer doesn't exist.
133 if (ctx
->RasterDiscard
) {
137 if (!ctx
->Current
.RasterPosValid
) {
138 goto end
; /* no-op, not an error */
141 if (ctx
->RenderMode
== GL_RENDER
) {
142 if (width
> 0 && height
> 0) {
143 /* Round, to satisfy conformance tests (matches SGI's OpenGL) */
144 GLint x
= IROUND(ctx
->Current
.RasterPos
[0]);
145 GLint y
= IROUND(ctx
->Current
.RasterPos
[1]);
147 if (_mesa_is_bufferobj(ctx
->Unpack
.BufferObj
)) {
148 /* unpack from PBO */
149 if (!_mesa_validate_pbo_access(2, &ctx
->Unpack
, width
, height
,
150 1, format
, type
, INT_MAX
, pixels
)) {
151 _mesa_error(ctx
, GL_INVALID_OPERATION
,
152 "glDrawPixels(invalid PBO access)");
155 if (_mesa_bufferobj_mapped(ctx
->Unpack
.BufferObj
)) {
156 /* buffer is mapped - that's an error */
157 _mesa_error(ctx
, GL_INVALID_OPERATION
,
158 "glDrawPixels(PBO is mapped)");
163 ctx
->Driver
.DrawPixels(ctx
, x
, y
, width
, height
, format
, type
,
164 &ctx
->Unpack
, pixels
);
167 else if (ctx
->RenderMode
== GL_FEEDBACK
) {
168 /* Feedback the current raster pos info */
169 FLUSH_CURRENT( ctx
, 0 );
170 _mesa_feedback_token( ctx
, (GLfloat
) (GLint
) GL_DRAW_PIXEL_TOKEN
);
171 _mesa_feedback_vertex( ctx
,
172 ctx
->Current
.RasterPos
,
173 ctx
->Current
.RasterColor
,
174 ctx
->Current
.RasterTexCoords
[0] );
177 ASSERT(ctx
->RenderMode
== GL_SELECT
);
178 /* Do nothing. See OpenGL Spec, Appendix B, Corollary 6. */
182 _mesa_set_vp_override(ctx
, GL_FALSE
);
186 static void GLAPIENTRY
187 _mesa_CopyPixels( GLint srcx
, GLint srcy
, GLsizei width
, GLsizei height
,
190 GET_CURRENT_CONTEXT(ctx
);
191 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
193 if (MESA_VERBOSE
& VERBOSE_API
)
195 "glCopyPixels(%d, %d, %d, %d, %s) // from %s to %s at %d, %d\n",
196 srcx
, srcy
, width
, height
,
197 _mesa_lookup_enum_by_nr(type
),
198 _mesa_lookup_enum_by_nr(ctx
->ReadBuffer
->ColorReadBuffer
),
199 _mesa_lookup_enum_by_nr(ctx
->DrawBuffer
->ColorDrawBuffer
[0]),
200 IROUND(ctx
->Current
.RasterPos
[0]),
201 IROUND(ctx
->Current
.RasterPos
[1]));
203 if (width
< 0 || height
< 0) {
204 _mesa_error(ctx
, GL_INVALID_VALUE
, "glCopyPixels(width or height < 0)");
208 /* Note: more detailed 'type' checking is done by the
209 * _mesa_source/dest_buffer_exists() calls below. That's where we
210 * check if the stencil buffer exists, etc.
212 if (type
!= GL_COLOR
&&
214 type
!= GL_STENCIL
&&
215 type
!= GL_DEPTH_STENCIL
) {
216 _mesa_error(ctx
, GL_INVALID_ENUM
, "glCopyPixels(type=%s)",
217 _mesa_lookup_enum_by_nr(type
));
221 /* We're not using the current vertex program, and the driver may install
222 * it's own. Note: this may dirty some state.
224 _mesa_set_vp_override(ctx
, GL_TRUE
);
226 /* Note: this call does state validation */
227 if (!_mesa_valid_to_render(ctx
, "glCopyPixels")) {
228 goto end
; /* the error code was recorded */
231 /* Check read buffer's status (draw buffer was already checked) */
232 if (ctx
->ReadBuffer
->_Status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
233 _mesa_error(ctx
, GL_INVALID_FRAMEBUFFER_OPERATION_EXT
,
234 "glCopyPixels(incomplete framebuffer)" );
238 if (ctx
->ReadBuffer
->Name
!= 0 && ctx
->ReadBuffer
->Visual
.samples
> 0) {
239 _mesa_error(ctx
, GL_INVALID_FRAMEBUFFER_OPERATION
,
240 "glCopyPixels(multisample FBO)");
244 if (!_mesa_source_buffer_exists(ctx
, type
) ||
245 !_mesa_dest_buffer_exists(ctx
, type
)) {
246 _mesa_error(ctx
, GL_INVALID_OPERATION
,
247 "glCopyPixels(missing source or dest buffer)");
251 if (ctx
->RasterDiscard
) {
255 if (!ctx
->Current
.RasterPosValid
|| width
== 0 || height
== 0) {
256 goto end
; /* no-op, not an error */
259 if (ctx
->RenderMode
== GL_RENDER
) {
260 /* Round to satisfy conformance tests (matches SGI's OpenGL) */
261 if (width
> 0 && height
> 0) {
262 GLint destx
= IROUND(ctx
->Current
.RasterPos
[0]);
263 GLint desty
= IROUND(ctx
->Current
.RasterPos
[1]);
264 ctx
->Driver
.CopyPixels( ctx
, srcx
, srcy
, width
, height
, destx
, desty
,
268 else if (ctx
->RenderMode
== GL_FEEDBACK
) {
269 FLUSH_CURRENT( ctx
, 0 );
270 _mesa_feedback_token( ctx
, (GLfloat
) (GLint
) GL_COPY_PIXEL_TOKEN
);
271 _mesa_feedback_vertex( ctx
,
272 ctx
->Current
.RasterPos
,
273 ctx
->Current
.RasterColor
,
274 ctx
->Current
.RasterTexCoords
[0] );
277 ASSERT(ctx
->RenderMode
== GL_SELECT
);
278 /* Do nothing. See OpenGL Spec, Appendix B, Corollary 6. */
282 _mesa_set_vp_override(ctx
, GL_FALSE
);
286 static void GLAPIENTRY
287 _mesa_Bitmap( GLsizei width
, GLsizei height
,
288 GLfloat xorig
, GLfloat yorig
, GLfloat xmove
, GLfloat ymove
,
289 const GLubyte
*bitmap
)
291 GET_CURRENT_CONTEXT(ctx
);
292 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
294 if (width
< 0 || height
< 0) {
295 _mesa_error( ctx
, GL_INVALID_VALUE
, "glBitmap(width or height < 0)" );
299 if (!ctx
->Current
.RasterPosValid
) {
300 return; /* do nothing */
303 /* Note: this call does state validation */
304 if (!_mesa_valid_to_render(ctx
, "glBitmap")) {
305 /* the error code was recorded */
309 if (ctx
->RasterDiscard
)
312 if (ctx
->RenderMode
== GL_RENDER
) {
313 /* Truncate, to satisfy conformance tests (matches SGI's OpenGL). */
314 if (width
> 0 && height
> 0) {
315 const GLfloat epsilon
= 0.0001F
;
316 GLint x
= IFLOOR(ctx
->Current
.RasterPos
[0] + epsilon
- xorig
);
317 GLint y
= IFLOOR(ctx
->Current
.RasterPos
[1] + epsilon
- yorig
);
319 if (_mesa_is_bufferobj(ctx
->Unpack
.BufferObj
)) {
320 /* unpack from PBO */
321 if (!_mesa_validate_pbo_access(2, &ctx
->Unpack
, width
, height
,
322 1, GL_COLOR_INDEX
, GL_BITMAP
,
323 INT_MAX
, (const GLvoid
*) bitmap
)) {
324 _mesa_error(ctx
, GL_INVALID_OPERATION
,
325 "glBitmap(invalid PBO access)");
328 if (_mesa_bufferobj_mapped(ctx
->Unpack
.BufferObj
)) {
329 /* buffer is mapped - that's an error */
330 _mesa_error(ctx
, GL_INVALID_OPERATION
,
331 "glBitmap(PBO is mapped)");
336 ctx
->Driver
.Bitmap( ctx
, x
, y
, width
, height
, &ctx
->Unpack
, bitmap
);
340 else if (ctx
->RenderMode
== GL_FEEDBACK
) {
341 FLUSH_CURRENT(ctx
, 0);
342 _mesa_feedback_token( ctx
, (GLfloat
) (GLint
) GL_BITMAP_TOKEN
);
343 _mesa_feedback_vertex( ctx
,
344 ctx
->Current
.RasterPos
,
345 ctx
->Current
.RasterColor
,
346 ctx
->Current
.RasterTexCoords
[0] );
349 ASSERT(ctx
->RenderMode
== GL_SELECT
);
350 /* Do nothing. See OpenGL Spec, Appendix B, Corollary 6. */
354 /* update raster position */
355 ctx
->Current
.RasterPos
[0] += xmove
;
356 ctx
->Current
.RasterPos
[1] += ymove
;
361 _mesa_init_drawpix_dispatch(struct _glapi_table
*disp
)
363 SET_Bitmap(disp
, _mesa_Bitmap
);
364 SET_CopyPixels(disp
, _mesa_CopyPixels
);
365 SET_DrawPixels(disp
, _mesa_DrawPixels
);
369 #endif /* FEATURE_drawpix */