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