2 * Mesa 3-D graphics library
4 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * OTHER DEALINGS IN THE SOFTWARE.
26 #include "draw_validate.h"
27 #include "bufferobj.h"
32 #include "framebuffer.h"
36 #include "glformats.h"
38 #include "util/u_math.h"
39 #include "util/rounding.h"
43 * Execute glDrawPixels
46 _mesa_DrawPixels( GLsizei width
, GLsizei height
,
47 GLenum format
, GLenum type
, const GLvoid
*pixels
)
50 GET_CURRENT_CONTEXT(ctx
);
52 FLUSH_VERTICES(ctx
, 0);
54 if (MESA_VERBOSE
& VERBOSE_API
)
55 _mesa_debug(ctx
, "glDrawPixels(%d, %d, %s, %s, %p) // to %s at %ld, %ld\n",
57 _mesa_enum_to_string(format
),
58 _mesa_enum_to_string(type
),
60 _mesa_enum_to_string(ctx
->DrawBuffer
->ColorDrawBuffer
[0]),
61 lroundf(ctx
->Current
.RasterPos
[0]),
62 lroundf(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_enum_format_integer(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_enum_to_string(format
),
101 _mesa_enum_to_string(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 dest 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
= lroundf(ctx
->Current
.RasterPos
[0]);
145 GLint y
= lroundf(ctx
->Current
.RasterPos
[1]);
147 if (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_check_disallowed_mapping(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
);
184 if (MESA_DEBUG_FLAGS
& DEBUG_ALWAYS_FLUSH
) {
191 _mesa_CopyPixels( GLint srcx
, GLint srcy
, GLsizei width
, GLsizei height
,
194 GET_CURRENT_CONTEXT(ctx
);
196 FLUSH_VERTICES(ctx
, 0);
198 if (MESA_VERBOSE
& VERBOSE_API
)
200 "glCopyPixels(%d, %d, %d, %d, %s) // from %s to %s at %ld, %ld\n",
201 srcx
, srcy
, width
, height
,
202 _mesa_enum_to_string(type
),
203 _mesa_enum_to_string(ctx
->ReadBuffer
->ColorReadBuffer
),
204 _mesa_enum_to_string(ctx
->DrawBuffer
->ColorDrawBuffer
[0]),
205 lroundf(ctx
->Current
.RasterPos
[0]),
206 lroundf(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 type
!= GL_DEPTH_STENCIL_TO_RGBA_NV
&&
222 type
!= GL_DEPTH_STENCIL_TO_BGRA_NV
) {
223 _mesa_error(ctx
, GL_INVALID_ENUM
, "glCopyPixels(type=%s)",
224 _mesa_enum_to_string(type
));
228 /* Return GL_INVALID_ENUM if the relevant extension is not enabled */
229 if ((type
== GL_DEPTH_STENCIL_TO_RGBA_NV
|| type
== GL_DEPTH_STENCIL_TO_BGRA_NV
) &&
230 !ctx
->Extensions
.NV_copy_depth_to_color
) {
231 _mesa_error(ctx
, GL_INVALID_ENUM
, "glCopyPixels(type=%s)",
232 _mesa_enum_to_string(type
));
236 /* We're not using the current vertex program, and the driver may install
237 * it's own. Note: this may dirty some state.
239 _mesa_set_vp_override(ctx
, GL_TRUE
);
241 /* Note: this call does state validation */
242 if (!_mesa_valid_to_render(ctx
, "glCopyPixels")) {
243 goto end
; /* the error code was recorded */
246 /* Check read buffer's status (draw buffer was already checked) */
247 if (ctx
->ReadBuffer
->_Status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
248 _mesa_error(ctx
, GL_INVALID_FRAMEBUFFER_OPERATION_EXT
,
249 "glCopyPixels(incomplete framebuffer)" );
253 if (_mesa_is_user_fbo(ctx
->ReadBuffer
) &&
254 ctx
->ReadBuffer
->Visual
.samples
> 0) {
255 _mesa_error(ctx
, GL_INVALID_OPERATION
,
256 "glCopyPixels(multisample FBO)");
260 if (!_mesa_source_buffer_exists(ctx
, type
) ||
261 !_mesa_dest_buffer_exists(ctx
, type
)) {
262 _mesa_error(ctx
, GL_INVALID_OPERATION
,
263 "glCopyPixels(missing source or dest buffer)");
267 if (ctx
->RasterDiscard
) {
271 if (!ctx
->Current
.RasterPosValid
|| width
== 0 || height
== 0) {
272 goto end
; /* no-op, not an error */
275 if (ctx
->RenderMode
== GL_RENDER
) {
276 /* Round to satisfy conformance tests (matches SGI's OpenGL) */
277 if (width
> 0 && height
> 0) {
278 GLint destx
= lroundf(ctx
->Current
.RasterPos
[0]);
279 GLint desty
= lroundf(ctx
->Current
.RasterPos
[1]);
280 ctx
->Driver
.CopyPixels( ctx
, srcx
, srcy
, width
, height
, destx
, desty
,
284 else if (ctx
->RenderMode
== GL_FEEDBACK
) {
285 FLUSH_CURRENT( ctx
, 0 );
286 _mesa_feedback_token( ctx
, (GLfloat
) (GLint
) GL_COPY_PIXEL_TOKEN
);
287 _mesa_feedback_vertex( ctx
,
288 ctx
->Current
.RasterPos
,
289 ctx
->Current
.RasterColor
,
290 ctx
->Current
.RasterTexCoords
[0] );
293 assert(ctx
->RenderMode
== GL_SELECT
);
294 /* Do nothing. See OpenGL Spec, Appendix B, Corollary 6. */
298 _mesa_set_vp_override(ctx
, GL_FALSE
);
300 if (MESA_DEBUG_FLAGS
& DEBUG_ALWAYS_FLUSH
) {
307 _mesa_Bitmap( GLsizei width
, GLsizei height
,
308 GLfloat xorig
, GLfloat yorig
, GLfloat xmove
, GLfloat ymove
,
309 const GLubyte
*bitmap
)
311 GET_CURRENT_CONTEXT(ctx
);
313 FLUSH_VERTICES(ctx
, 0);
315 if (width
< 0 || height
< 0) {
316 _mesa_error( ctx
, GL_INVALID_VALUE
, "glBitmap(width or height < 0)" );
320 if (!ctx
->Current
.RasterPosValid
) {
321 return; /* do nothing */
324 /* Note: this call does state validation */
325 if (!_mesa_valid_to_render(ctx
, "glBitmap")) {
326 /* the error code was recorded */
330 if (ctx
->RasterDiscard
)
333 if (ctx
->RenderMode
== GL_RENDER
) {
334 /* Truncate, to satisfy conformance tests (matches SGI's OpenGL). */
335 if (width
> 0 && height
> 0) {
336 const GLfloat epsilon
= 0.0001F
;
337 GLint x
= util_ifloor(ctx
->Current
.RasterPos
[0] + epsilon
- xorig
);
338 GLint y
= util_ifloor(ctx
->Current
.RasterPos
[1] + epsilon
- yorig
);
340 if (ctx
->Unpack
.BufferObj
) {
341 /* unpack from PBO */
342 if (!_mesa_validate_pbo_access(2, &ctx
->Unpack
, width
, height
,
343 1, GL_COLOR_INDEX
, GL_BITMAP
,
344 INT_MAX
, (const GLvoid
*) bitmap
)) {
345 _mesa_error(ctx
, GL_INVALID_OPERATION
,
346 "glBitmap(invalid PBO access)");
349 if (_mesa_check_disallowed_mapping(ctx
->Unpack
.BufferObj
)) {
350 /* buffer is mapped - that's an error */
351 _mesa_error(ctx
, GL_INVALID_OPERATION
,
352 "glBitmap(PBO is mapped)");
357 ctx
->Driver
.Bitmap( ctx
, x
, y
, width
, height
, &ctx
->Unpack
, bitmap
);
360 else if (ctx
->RenderMode
== GL_FEEDBACK
) {
361 FLUSH_CURRENT(ctx
, 0);
362 _mesa_feedback_token( ctx
, (GLfloat
) (GLint
) GL_BITMAP_TOKEN
);
363 _mesa_feedback_vertex( ctx
,
364 ctx
->Current
.RasterPos
,
365 ctx
->Current
.RasterColor
,
366 ctx
->Current
.RasterTexCoords
[0] );
369 assert(ctx
->RenderMode
== GL_SELECT
);
370 /* Do nothing. See OpenGL Spec, Appendix B, Corollary 6. */
373 /* update raster position */
374 ctx
->Current
.RasterPos
[0] += xmove
;
375 ctx
->Current
.RasterPos
[1] += ymove
;
377 if (MESA_DEBUG_FLAGS
& DEBUG_ALWAYS_FLUSH
) {