1 /*
2 * Mesa 3-D graphics library
3 *
4 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
5 *
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:
12 *
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
15 *
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.
23 */
24
25 #include "glheader.h"
26 #include "draw_validate.h"
27 #include "bufferobj.h"
28 #include "context.h"
29 #include "drawpix.h"
30 #include "enums.h"
31 #include "feedback.h"
32 #include "framebuffer.h"
33 #include "image.h"
34 #include "pbo.h"
35 #include "state.h"
36 #include "glformats.h"
37 #include "fbobject.h"
38 #include "util/u_math.h"
39 #include "util/rounding.h"
40
41
42 /*
43 * Execute glDrawPixels
44 */
45 void GLAPIENTRY
_mesa_DrawPixels(GLsizei width,GLsizei height,GLenum format,GLenum type,const GLvoid * pixels)46 _mesa_DrawPixels( GLsizei width, GLsizei height,
47 GLenum format, GLenum type, const GLvoid *pixels )
48 {
49 GLenum err;
50 GET_CURRENT_CONTEXT(ctx);
51
52 FLUSH_VERTICES(ctx, 0);
53
54 if (MESA_VERBOSE & VERBOSE_API)
55 _mesa_debug(ctx, "glDrawPixels(%d, %d, %s, %s, %p) // to %s at %ld, %ld\n",
56 width, height,
57 _mesa_enum_to_string(format),
58 _mesa_enum_to_string(type),
59 pixels,
60 _mesa_enum_to_string(ctx->DrawBuffer->ColorDrawBuffer[0]),
61 lroundf(ctx->Current.RasterPos[0]),
62 lroundf(ctx->Current.RasterPos[1]));
63
64
65 if (width < 0 || height < 0) {
66 _mesa_error( ctx, GL_INVALID_VALUE, "glDrawPixels(width or height < 0)" );
67 return;
68 }
69
70 /* We're not using the current vertex program, and the driver may install
71 * its own. Note: this may dirty some state.
72 */
73 _mesa_set_vp_override(ctx, GL_TRUE);
74
75 /* Note: this call does state validation */
76 if (!_mesa_valid_to_render(ctx, "glDrawPixels")) {
77 goto end; /* the error code was recorded */
78 }
79
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:
83 *
84 * "If format contains integer components, as shown in table 3.6, an
85 * INVALID OPERATION error is generated."
86 *
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.
91 */
92 if (_mesa_is_enum_format_integer(format)) {
93 _mesa_error(ctx, GL_INVALID_OPERATION, "glDrawPixels(integer format)");
94 goto end;
95 }
96
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));
102 goto end;
103 }
104
105 /* do special format-related checks */
106 switch (format) {
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)");
114 goto end;
115 }
116 break;
117 case GL_COLOR_INDEX:
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)");
123 goto end;
124 }
125 break;
126 default:
127 /* for color formats it's not an error if the destination color
128 * buffer doesn't exist.
129 */
130 break;
131 }
132
133 if (ctx->RasterDiscard) {
134 goto end;
135 }
136
137 if (!ctx->Current.RasterPosValid) {
138 goto end; /* no-op, not an error */
139 }
140
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]);
146
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)");
153 goto end;
154 }
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)");
159 goto end;
160 }
161 }
162
163 ctx->Driver.DrawPixels(ctx, x, y, width, height, format, type,
164 &ctx->Unpack, pixels);
165 }
166 }
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] );
175 }
176 else {
177 assert(ctx->RenderMode == GL_SELECT);
178 /* Do nothing. See OpenGL Spec, Appendix B, Corollary 6. */
179 }
180
181 end:
182 _mesa_set_vp_override(ctx, GL_FALSE);
183
184 if (MESA_DEBUG_FLAGS & DEBUG_ALWAYS_FLUSH) {
185 _mesa_flush(ctx);
186 }
187 }
188
189
190 void GLAPIENTRY
_mesa_CopyPixels(GLint srcx,GLint srcy,GLsizei width,GLsizei height,GLenum type)191 _mesa_CopyPixels( GLint srcx, GLint srcy, GLsizei width, GLsizei height,
192 GLenum type )
193 {
194 GET_CURRENT_CONTEXT(ctx);
195
196 FLUSH_VERTICES(ctx, 0);
197
198 if (MESA_VERBOSE & VERBOSE_API)
199 _mesa_debug(ctx,
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]));
207
208 if (width < 0 || height < 0) {
209 _mesa_error(ctx, GL_INVALID_VALUE, "glCopyPixels(width or height < 0)");
210 return;
211 }
212
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.
216 */
217 if (type != GL_COLOR &&
218 type != GL_DEPTH &&
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));
225 return;
226 }
227
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));
233 return;
234 }
235
236 /* We're not using the current vertex program, and the driver may install
237 * it's own. Note: this may dirty some state.
238 */
239 _mesa_set_vp_override(ctx, GL_TRUE);
240
241 /* Note: this call does state validation */
242 if (!_mesa_valid_to_render(ctx, "glCopyPixels")) {
243 goto end; /* the error code was recorded */
244 }
245
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)" );
250 goto end;
251 }
252
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)");
257 goto end;
258 }
259
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)");
264 goto end;
265 }
266
267 if (ctx->RasterDiscard) {
268 goto end;
269 }
270
271 if (!ctx->Current.RasterPosValid || width == 0 || height == 0) {
272 goto end; /* no-op, not an error */
273 }
274
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,
281 type );
282 }
283 }
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] );
291 }
292 else {
293 assert(ctx->RenderMode == GL_SELECT);
294 /* Do nothing. See OpenGL Spec, Appendix B, Corollary 6. */
295 }
296
297 end:
298 _mesa_set_vp_override(ctx, GL_FALSE);
299
300 if (MESA_DEBUG_FLAGS & DEBUG_ALWAYS_FLUSH) {
301 _mesa_flush(ctx);
302 }
303 }
304
305
306 void GLAPIENTRY
_mesa_Bitmap(GLsizei width,GLsizei height,GLfloat xorig,GLfloat yorig,GLfloat xmove,GLfloat ymove,const GLubyte * bitmap)307 _mesa_Bitmap( GLsizei width, GLsizei height,
308 GLfloat xorig, GLfloat yorig, GLfloat xmove, GLfloat ymove,
309 const GLubyte *bitmap )
310 {
311 GET_CURRENT_CONTEXT(ctx);
312
313 FLUSH_VERTICES(ctx, 0);
314
315 if (width < 0 || height < 0) {
316 _mesa_error( ctx, GL_INVALID_VALUE, "glBitmap(width or height < 0)" );
317 return;
318 }
319
320 if (!ctx->Current.RasterPosValid) {
321 return; /* do nothing */
322 }
323
324 /* Note: this call does state validation */
325 if (!_mesa_valid_to_render(ctx, "glBitmap")) {
326 /* the error code was recorded */
327 return;
328 }
329
330 if (ctx->RasterDiscard)
331 return;
332
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);
339
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)");
347 return;
348 }
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)");
353 return;
354 }
355 }
356
357 ctx->Driver.Bitmap( ctx, x, y, width, height, &ctx->Unpack, bitmap );
358 }
359 }
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] );
367 }
368 else {
369 assert(ctx->RenderMode == GL_SELECT);
370 /* Do nothing. See OpenGL Spec, Appendix B, Corollary 6. */
371 }
372
373 /* update raster position */
374 ctx->Current.RasterPos[0] += xmove;
375 ctx->Current.RasterPos[1] += ymove;
376
377 if (MESA_DEBUG_FLAGS & DEBUG_ALWAYS_FLUSH) {
378 _mesa_flush(ctx);
379 }
380 }
381