mesa: add glformats integer type/format detection routines
[mesa.git] / src / mesa / main / drawpix.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 7.1
4 *
5 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
6 *
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:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
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.
23 */
24
25 #include "glheader.h"
26 #include "imports.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 "mfeatures.h"
35 #include "pbo.h"
36 #include "state.h"
37 #include "dispatch.h"
38 #include "glformats.h"
39
40
41 #if FEATURE_drawpix
42
43
44 /*
45 * Execute glDrawPixels
46 */
47 static void GLAPIENTRY
48 _mesa_DrawPixels( GLsizei width, GLsizei height,
49 GLenum format, GLenum type, const GLvoid *pixels )
50 {
51 GLenum err;
52 GET_CURRENT_CONTEXT(ctx);
53 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
54
55 if (MESA_VERBOSE & VERBOSE_API)
56 _mesa_debug(ctx, "glDrawPixels(%d, %d, %s, %s, %p) // to %s at %d, %d\n",
57 width, height,
58 _mesa_lookup_enum_by_nr(format),
59 _mesa_lookup_enum_by_nr(type),
60 pixels,
61 _mesa_lookup_enum_by_nr(ctx->DrawBuffer->ColorDrawBuffer[0]),
62 IROUND(ctx->Current.RasterPos[0]),
63 IROUND(ctx->Current.RasterPos[1]));
64
65
66 if (width < 0 || height < 0) {
67 _mesa_error( ctx, GL_INVALID_VALUE, "glDrawPixels(width or height < 0)" );
68 return;
69 }
70
71 /* We're not using the current vertex program, and the driver may install
72 * its own. Note: this may dirty some state.
73 */
74 _mesa_set_vp_override(ctx, GL_TRUE);
75
76 /* Note: this call does state validation */
77 if (!_mesa_valid_to_render(ctx, "glDrawPixels")) {
78 goto end; /* the error code was recorded */
79 }
80
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:
84 *
85 * "If format contains integer components, as shown in table 3.6, an
86 * INVALID OPERATION error is generated."
87 *
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.
92 */
93 if (_mesa_is_enum_format_integer(format)) {
94 _mesa_error(ctx, GL_INVALID_OPERATION, "glDrawPixels(integer format)");
95 goto end;
96 }
97
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));
103 goto end;
104 }
105
106 /* do special format-related checks */
107 switch (format) {
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)");
115 goto end;
116 }
117 break;
118 case GL_COLOR_INDEX:
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)");
124 goto end;
125 }
126 break;
127 default:
128 /* for color formats it's not an error if the destination color
129 * buffer doesn't exist.
130 */
131 break;
132 }
133
134 if (ctx->RasterDiscard) {
135 goto end;
136 }
137
138 if (!ctx->Current.RasterPosValid) {
139 goto end; /* no-op, not an error */
140 }
141
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]);
147
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)");
154 goto end;
155 }
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)");
160 goto end;
161 }
162 }
163
164 ctx->Driver.DrawPixels(ctx, x, y, width, height, format, type,
165 &ctx->Unpack, pixels);
166 }
167 }
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] );
176 }
177 else {
178 ASSERT(ctx->RenderMode == GL_SELECT);
179 /* Do nothing. See OpenGL Spec, Appendix B, Corollary 6. */
180 }
181
182 end:
183 _mesa_set_vp_override(ctx, GL_FALSE);
184
185 if (MESA_DEBUG_FLAGS & DEBUG_ALWAYS_FLUSH) {
186 _mesa_flush(ctx);
187 }
188 }
189
190
191 static void GLAPIENTRY
192 _mesa_CopyPixels( GLint srcx, GLint srcy, GLsizei width, GLsizei height,
193 GLenum type )
194 {
195 GET_CURRENT_CONTEXT(ctx);
196 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
197
198 if (MESA_VERBOSE & VERBOSE_API)
199 _mesa_debug(ctx,
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]));
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 _mesa_error(ctx, GL_INVALID_ENUM, "glCopyPixels(type=%s)",
222 _mesa_lookup_enum_by_nr(type));
223 return;
224 }
225
226 /* We're not using the current vertex program, and the driver may install
227 * it's own. Note: this may dirty some state.
228 */
229 _mesa_set_vp_override(ctx, GL_TRUE);
230
231 /* Note: this call does state validation */
232 if (!_mesa_valid_to_render(ctx, "glCopyPixels")) {
233 goto end; /* the error code was recorded */
234 }
235
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)" );
240 goto end;
241 }
242
243 if (ctx->ReadBuffer->Name != 0 && ctx->ReadBuffer->Visual.samples > 0) {
244 _mesa_error(ctx, GL_INVALID_OPERATION,
245 "glCopyPixels(multisample FBO)");
246 goto end;
247 }
248
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)");
253 goto end;
254 }
255
256 if (ctx->RasterDiscard) {
257 goto end;
258 }
259
260 if (!ctx->Current.RasterPosValid || width == 0 || height == 0) {
261 goto end; /* no-op, not an error */
262 }
263
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,
270 type );
271 }
272 }
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] );
280 }
281 else {
282 ASSERT(ctx->RenderMode == GL_SELECT);
283 /* Do nothing. See OpenGL Spec, Appendix B, Corollary 6. */
284 }
285
286 end:
287 _mesa_set_vp_override(ctx, GL_FALSE);
288
289 if (MESA_DEBUG_FLAGS & DEBUG_ALWAYS_FLUSH) {
290 _mesa_flush(ctx);
291 }
292 }
293
294
295 static void GLAPIENTRY
296 _mesa_Bitmap( GLsizei width, GLsizei height,
297 GLfloat xorig, GLfloat yorig, GLfloat xmove, GLfloat ymove,
298 const GLubyte *bitmap )
299 {
300 GET_CURRENT_CONTEXT(ctx);
301 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
302
303 if (width < 0 || height < 0) {
304 _mesa_error( ctx, GL_INVALID_VALUE, "glBitmap(width or height < 0)" );
305 return;
306 }
307
308 if (!ctx->Current.RasterPosValid) {
309 return; /* do nothing */
310 }
311
312 /* Note: this call does state validation */
313 if (!_mesa_valid_to_render(ctx, "glBitmap")) {
314 /* the error code was recorded */
315 return;
316 }
317
318 if (ctx->RasterDiscard)
319 return;
320
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);
327
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)");
335 return;
336 }
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)");
341 return;
342 }
343 }
344
345 ctx->Driver.Bitmap( ctx, x, y, width, height, &ctx->Unpack, bitmap );
346 }
347 }
348 #if _HAVE_FULL_GL
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] );
356 }
357 else {
358 ASSERT(ctx->RenderMode == GL_SELECT);
359 /* Do nothing. See OpenGL Spec, Appendix B, Corollary 6. */
360 }
361 #endif
362
363 /* update raster position */
364 ctx->Current.RasterPos[0] += xmove;
365 ctx->Current.RasterPos[1] += ymove;
366
367 if (MESA_DEBUG_FLAGS & DEBUG_ALWAYS_FLUSH) {
368 _mesa_flush(ctx);
369 }
370 }
371
372
373 void
374 _mesa_init_drawpix_dispatch(struct _glapi_table *disp)
375 {
376 SET_Bitmap(disp, _mesa_Bitmap);
377 SET_CopyPixels(disp, _mesa_CopyPixels);
378 SET_DrawPixels(disp, _mesa_DrawPixels);
379 }
380
381
382 #endif /* FEATURE_drawpix */