Merge remote branch 'origin/nvc0'
[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 "mfeatures.h"
34 #include "pbo.h"
35 #include "readpix.h"
36 #include "state.h"
37 #include "dispatch.h"
38
39
40 #if FEATURE_drawpix
41
42
43 /*
44 * Execute glDrawPixels
45 */
46 static void GLAPIENTRY
47 _mesa_DrawPixels( GLsizei width, GLsizei height,
48 GLenum format, GLenum type, const GLvoid *pixels )
49 {
50 GET_CURRENT_CONTEXT(ctx);
51 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
52
53 if (MESA_VERBOSE & VERBOSE_API)
54 _mesa_debug(ctx, "glDrawPixels(%d, %d, %s, %s, %p) // to %s at %d, %d\n",
55 width, height,
56 _mesa_lookup_enum_by_nr(format),
57 _mesa_lookup_enum_by_nr(type),
58 pixels,
59 _mesa_lookup_enum_by_nr(ctx->DrawBuffer->ColorDrawBuffer[0]),
60 IROUND(ctx->Current.RasterPos[0]),
61 IROUND(ctx->Current.RasterPos[1]));
62
63
64 if (width < 0 || height < 0) {
65 _mesa_error( ctx, GL_INVALID_VALUE, "glDrawPixels(width or height < 0" );
66 return;
67 }
68
69 /* We're not using the current vertex program, and the driver may install
70 * its own. Note: this may dirty some state.
71 */
72 _mesa_set_vp_override(ctx, GL_TRUE);
73
74 /* Note: this call does state validation */
75 if (!_mesa_valid_to_render(ctx, "glDrawPixels")) {
76 goto end; /* the error code was recorded */
77 }
78
79 if (_mesa_error_check_format_type(ctx, format, type, GL_TRUE)) {
80 goto end; /* the error code was recorded */
81 }
82
83 if (!ctx->Current.RasterPosValid) {
84 goto end; /* no-op, not an error */
85 }
86
87 if (ctx->RenderMode == GL_RENDER) {
88 if (width > 0 && height > 0) {
89 /* Round, to satisfy conformance tests (matches SGI's OpenGL) */
90 GLint x = IROUND(ctx->Current.RasterPos[0]);
91 GLint y = IROUND(ctx->Current.RasterPos[1]);
92
93 if (ctx->Unpack.BufferObj->Name) {
94 /* unpack from PBO */
95 if (!_mesa_validate_pbo_access(2, &ctx->Unpack, width, height, 1,
96 format, type, pixels)) {
97 _mesa_error(ctx, GL_INVALID_OPERATION,
98 "glDrawPixels(invalid PBO access)");
99 goto end;
100 }
101 if (_mesa_bufferobj_mapped(ctx->Unpack.BufferObj)) {
102 /* buffer is mapped - that's an error */
103 _mesa_error(ctx, GL_INVALID_OPERATION,
104 "glDrawPixels(PBO is mapped)");
105 goto end;
106 }
107 }
108
109 ctx->Driver.DrawPixels(ctx, x, y, width, height, format, type,
110 &ctx->Unpack, pixels);
111 }
112 }
113 else if (ctx->RenderMode == GL_FEEDBACK) {
114 /* Feedback the current raster pos info */
115 FLUSH_CURRENT( ctx, 0 );
116 _mesa_feedback_token( ctx, (GLfloat) (GLint) GL_DRAW_PIXEL_TOKEN );
117 _mesa_feedback_vertex( ctx,
118 ctx->Current.RasterPos,
119 ctx->Current.RasterColor,
120 ctx->Current.RasterTexCoords[0] );
121 }
122 else {
123 ASSERT(ctx->RenderMode == GL_SELECT);
124 /* Do nothing. See OpenGL Spec, Appendix B, Corollary 6. */
125 }
126
127 end:
128 _mesa_set_vp_override(ctx, GL_FALSE);
129 }
130
131
132 static void GLAPIENTRY
133 _mesa_CopyPixels( GLint srcx, GLint srcy, GLsizei width, GLsizei height,
134 GLenum type )
135 {
136 GET_CURRENT_CONTEXT(ctx);
137 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
138
139 _mesa_finish(ctx);
140
141 if (MESA_VERBOSE & VERBOSE_API)
142 _mesa_debug(ctx,
143 "glCopyPixels(%d, %d, %d, %d, %s) // from %s to %s at %d, %d\n",
144 srcx, srcy, width, height,
145 _mesa_lookup_enum_by_nr(type),
146 _mesa_lookup_enum_by_nr(ctx->ReadBuffer->ColorReadBuffer),
147 _mesa_lookup_enum_by_nr(ctx->DrawBuffer->ColorDrawBuffer[0]),
148 IROUND(ctx->Current.RasterPos[0]),
149 IROUND(ctx->Current.RasterPos[1]));
150
151 if (width < 0 || height < 0) {
152 _mesa_error(ctx, GL_INVALID_VALUE, "glCopyPixels(width or height < 0)");
153 return;
154 }
155
156 /* Note: more detailed 'type' checking is done by the
157 * _mesa_source/dest_buffer_exists() calls below. That's where we
158 * check if the stencil buffer exists, etc.
159 */
160 if (type != GL_COLOR &&
161 type != GL_DEPTH &&
162 type != GL_STENCIL &&
163 type != GL_DEPTH_STENCIL) {
164 _mesa_error(ctx, GL_INVALID_ENUM, "glCopyPixels(type=%s)",
165 _mesa_lookup_enum_by_nr(type));
166 return;
167 }
168
169 /* We're not using the current vertex program, and the driver may install
170 * it's own. Note: this may dirty some state.
171 */
172 _mesa_set_vp_override(ctx, GL_TRUE);
173
174 /* Note: this call does state validation */
175 if (!_mesa_valid_to_render(ctx, "glCopyPixels")) {
176 goto end; /* the error code was recorded */
177 }
178
179 /* Check read buffer's status (draw buffer was already checked) */
180 if (ctx->ReadBuffer->_Status != GL_FRAMEBUFFER_COMPLETE_EXT) {
181 _mesa_error(ctx, GL_INVALID_FRAMEBUFFER_OPERATION_EXT,
182 "glCopyPixels(incomplete framebuffer)" );
183 goto end;
184 }
185
186 if (!_mesa_source_buffer_exists(ctx, type) ||
187 !_mesa_dest_buffer_exists(ctx, type)) {
188 _mesa_error(ctx, GL_INVALID_OPERATION,
189 "glCopyPixels(missing source or dest buffer)");
190 goto end;
191 }
192
193 if (!ctx->Current.RasterPosValid || width ==0 || height == 0) {
194 goto end; /* no-op, not an error */
195 }
196
197 if (ctx->RenderMode == GL_RENDER) {
198 /* Round to satisfy conformance tests (matches SGI's OpenGL) */
199 if (width > 0 && height > 0) {
200 GLint destx = IROUND(ctx->Current.RasterPos[0]);
201 GLint desty = IROUND(ctx->Current.RasterPos[1]);
202 ctx->Driver.CopyPixels( ctx, srcx, srcy, width, height, destx, desty,
203 type );
204 }
205 }
206 else if (ctx->RenderMode == GL_FEEDBACK) {
207 FLUSH_CURRENT( ctx, 0 );
208 _mesa_feedback_token( ctx, (GLfloat) (GLint) GL_COPY_PIXEL_TOKEN );
209 _mesa_feedback_vertex( ctx,
210 ctx->Current.RasterPos,
211 ctx->Current.RasterColor,
212 ctx->Current.RasterTexCoords[0] );
213 }
214 else {
215 ASSERT(ctx->RenderMode == GL_SELECT);
216 /* Do nothing. See OpenGL Spec, Appendix B, Corollary 6. */
217 }
218
219 end:
220 _mesa_set_vp_override(ctx, GL_FALSE);
221 }
222
223
224 static void GLAPIENTRY
225 _mesa_Bitmap( GLsizei width, GLsizei height,
226 GLfloat xorig, GLfloat yorig, GLfloat xmove, GLfloat ymove,
227 const GLubyte *bitmap )
228 {
229 GET_CURRENT_CONTEXT(ctx);
230 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
231
232 if (width < 0 || height < 0) {
233 _mesa_error( ctx, GL_INVALID_VALUE, "glBitmap(width or height < 0)" );
234 return;
235 }
236
237 if (!ctx->Current.RasterPosValid) {
238 return; /* do nothing */
239 }
240
241 /* Note: this call does state validation */
242 if (!_mesa_valid_to_render(ctx, "glBitmap")) {
243 /* the error code was recorded */
244 return;
245 }
246
247 if (ctx->RenderMode == GL_RENDER) {
248 /* Truncate, to satisfy conformance tests (matches SGI's OpenGL). */
249 if (width > 0 && height > 0) {
250 const GLfloat epsilon = 0.0001F;
251 GLint x = IFLOOR(ctx->Current.RasterPos[0] + epsilon - xorig);
252 GLint y = IFLOOR(ctx->Current.RasterPos[1] + epsilon - yorig);
253
254 if (ctx->Unpack.BufferObj->Name) {
255 /* unpack from PBO */
256 if (!_mesa_validate_pbo_access(2, &ctx->Unpack, width, height, 1,
257 GL_COLOR_INDEX, GL_BITMAP,
258 (GLvoid *) bitmap)) {
259 _mesa_error(ctx, GL_INVALID_OPERATION,
260 "glBitmap(invalid PBO access)");
261 return;
262 }
263 if (_mesa_bufferobj_mapped(ctx->Unpack.BufferObj)) {
264 /* buffer is mapped - that's an error */
265 _mesa_error(ctx, GL_INVALID_OPERATION,
266 "glBitmap(PBO is mapped)");
267 return;
268 }
269 }
270
271 ctx->Driver.Bitmap( ctx, x, y, width, height, &ctx->Unpack, bitmap );
272 }
273 }
274 #if _HAVE_FULL_GL
275 else if (ctx->RenderMode == GL_FEEDBACK) {
276 FLUSH_CURRENT(ctx, 0);
277 _mesa_feedback_token( ctx, (GLfloat) (GLint) GL_BITMAP_TOKEN );
278 _mesa_feedback_vertex( ctx,
279 ctx->Current.RasterPos,
280 ctx->Current.RasterColor,
281 ctx->Current.RasterTexCoords[0] );
282 }
283 else {
284 ASSERT(ctx->RenderMode == GL_SELECT);
285 /* Do nothing. See OpenGL Spec, Appendix B, Corollary 6. */
286 }
287 #endif
288
289 /* update raster position */
290 ctx->Current.RasterPos[0] += xmove;
291 ctx->Current.RasterPos[1] += ymove;
292 }
293
294
295 void
296 _mesa_init_drawpix_dispatch(struct _glapi_table *disp)
297 {
298 SET_Bitmap(disp, _mesa_Bitmap);
299 SET_CopyPixels(disp, _mesa_CopyPixels);
300 SET_DrawPixels(disp, _mesa_DrawPixels);
301 }
302
303
304 #endif /* FEATURE_drawpix */