[MESA]
[reactos.git] / reactos / dll / opengl / 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 "readpix.h"
37 #include "state.h"
38 #include "dispatch.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 GET_CURRENT_CONTEXT(ctx);
52 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
53
54 if (MESA_VERBOSE & VERBOSE_API)
55 _mesa_debug(ctx, "glDrawPixels(%d, %d, %s, %s, %p) // to %s at %d, %d\n",
56 width, height,
57 _mesa_lookup_enum_by_nr(format),
58 _mesa_lookup_enum_by_nr(type),
59 pixels,
60 _mesa_lookup_enum_by_nr(ctx->DrawBuffer->ColorDrawBuffer),
61 IROUND(ctx->Current.RasterPos[0]),
62 IROUND(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 /* Note: this call does state validation */
71 if (!_mesa_valid_to_render(ctx, "glDrawPixels")) {
72 return; /* the error code was recorded */
73 }
74
75 /* GL 3.0 introduced a new restriction on glDrawPixels() over what was in
76 * GL_EXT_texture_integer. From section 3.7.4 ("Rasterization of Pixel
77 * Rectangles) on page 151 of the GL 3.0 specification:
78 *
79 * "If format contains integer components, as shown in table 3.6, an
80 * INVALID OPERATION error is generated."
81 *
82 * Since DrawPixels rendering would be merely undefined if not an error (due
83 * to a lack of defined mapping from integer data to gl_Color fragment shader
84 * input), NVIDIA's implementation also just returns this error despite
85 * exposing GL_EXT_texture_integer, just return an error regardless.
86 */
87 if (_mesa_is_integer_format(format)) {
88 _mesa_error(ctx, GL_INVALID_OPERATION, "glDrawPixels(integer format)");
89 return;
90 }
91
92 if (_mesa_error_check_format_type(ctx, format, type, GL_TRUE)) {
93 return; /* the error code was recorded */
94 }
95
96 if (ctx->RasterDiscard) {
97 return;
98 }
99
100 if (!ctx->Current.RasterPosValid) {
101 return; /* no-op, not an error */
102 }
103
104 if (ctx->RenderMode == GL_RENDER) {
105 if (width > 0 && height > 0) {
106 /* Round, to satisfy conformance tests (matches SGI's OpenGL) */
107 GLint x = IROUND(ctx->Current.RasterPos[0]);
108 GLint y = IROUND(ctx->Current.RasterPos[1]);
109
110 if (_mesa_is_bufferobj(ctx->Unpack.BufferObj)) {
111 /* unpack from PBO */
112 if (!_mesa_validate_pbo_access(2, &ctx->Unpack, width, height,
113 1, format, type, INT_MAX, pixels)) {
114 _mesa_error(ctx, GL_INVALID_OPERATION,
115 "glDrawPixels(invalid PBO access)");
116 return;
117 }
118 if (_mesa_bufferobj_mapped(ctx->Unpack.BufferObj)) {
119 /* buffer is mapped - that's an error */
120 _mesa_error(ctx, GL_INVALID_OPERATION,
121 "glDrawPixels(PBO is mapped)");
122 return;
123 }
124 }
125
126 ctx->Driver.DrawPixels(ctx, x, y, width, height, format, type,
127 &ctx->Unpack, pixels);
128 }
129 }
130 else if (ctx->RenderMode == GL_FEEDBACK) {
131 /* Feedback the current raster pos info */
132 FLUSH_CURRENT( ctx, 0 );
133 _mesa_feedback_token( ctx, (GLfloat) (GLint) GL_DRAW_PIXEL_TOKEN );
134 _mesa_feedback_vertex( ctx,
135 ctx->Current.RasterPos,
136 ctx->Current.RasterColor,
137 ctx->Current.RasterTexCoords );
138 }
139 else {
140 ASSERT(ctx->RenderMode == GL_SELECT);
141 /* Do nothing. See OpenGL Spec, Appendix B, Corollary 6. */
142 }
143 }
144
145
146 static void GLAPIENTRY
147 _mesa_CopyPixels( GLint srcx, GLint srcy, GLsizei width, GLsizei height,
148 GLenum type )
149 {
150 GET_CURRENT_CONTEXT(ctx);
151 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
152
153 if (MESA_VERBOSE & VERBOSE_API)
154 _mesa_debug(ctx,
155 "glCopyPixels(%d, %d, %d, %d, %s) // from %s to %s at %d, %d\n",
156 srcx, srcy, width, height,
157 _mesa_lookup_enum_by_nr(type),
158 _mesa_lookup_enum_by_nr(ctx->ReadBuffer->ColorReadBuffer),
159 _mesa_lookup_enum_by_nr(ctx->DrawBuffer->ColorDrawBuffer),
160 IROUND(ctx->Current.RasterPos[0]),
161 IROUND(ctx->Current.RasterPos[1]));
162
163 if (width < 0 || height < 0) {
164 _mesa_error(ctx, GL_INVALID_VALUE, "glCopyPixels(width or height < 0)");
165 return;
166 }
167
168 /* Note: more detailed 'type' checking is done by the
169 * _mesa_source/dest_buffer_exists() calls below. That's where we
170 * check if the stencil buffer exists, etc.
171 */
172 if (type != GL_COLOR &&
173 type != GL_DEPTH &&
174 type != GL_STENCIL) {
175 _mesa_error(ctx, GL_INVALID_ENUM, "glCopyPixels(type=%s)",
176 _mesa_lookup_enum_by_nr(type));
177 return;
178 }
179
180 /* Note: this call does state validation */
181 if (!_mesa_valid_to_render(ctx, "glCopyPixels")) {
182 return; /* the error code was recorded */
183 }
184
185 if (!_mesa_source_buffer_exists(ctx, type) ||
186 !_mesa_dest_buffer_exists(ctx, type)) {
187 _mesa_error(ctx, GL_INVALID_OPERATION,
188 "glCopyPixels(missing source or dest buffer)");
189 return;
190 }
191
192 if (ctx->RasterDiscard) {
193 return;
194 }
195
196 if (!ctx->Current.RasterPosValid || width == 0 || height == 0) {
197 return; /* 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 );
216 }
217 else {
218 ASSERT(ctx->RenderMode == GL_SELECT);
219 /* Do nothing. See OpenGL Spec, Appendix B, Corollary 6. */
220 }
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->RasterDiscard)
248 return;
249
250 if (ctx->RenderMode == GL_RENDER) {
251 /* Truncate, to satisfy conformance tests (matches SGI's OpenGL). */
252 if (width > 0 && height > 0) {
253 const GLfloat epsilon = 0.0001F;
254 GLint x = IFLOOR(ctx->Current.RasterPos[0] + epsilon - xorig);
255 GLint y = IFLOOR(ctx->Current.RasterPos[1] + epsilon - yorig);
256
257 if (_mesa_is_bufferobj(ctx->Unpack.BufferObj)) {
258 /* unpack from PBO */
259 if (!_mesa_validate_pbo_access(2, &ctx->Unpack, width, height,
260 1, GL_COLOR_INDEX, GL_BITMAP,
261 INT_MAX, (const GLvoid *) bitmap)) {
262 _mesa_error(ctx, GL_INVALID_OPERATION,
263 "glBitmap(invalid PBO access)");
264 return;
265 }
266 if (_mesa_bufferobj_mapped(ctx->Unpack.BufferObj)) {
267 /* buffer is mapped - that's an error */
268 _mesa_error(ctx, GL_INVALID_OPERATION,
269 "glBitmap(PBO is mapped)");
270 return;
271 }
272 }
273
274 ctx->Driver.Bitmap( ctx, x, y, width, height, &ctx->Unpack, bitmap );
275 }
276 }
277 #if _HAVE_FULL_GL
278 else if (ctx->RenderMode == GL_FEEDBACK) {
279 FLUSH_CURRENT(ctx, 0);
280 _mesa_feedback_token( ctx, (GLfloat) (GLint) GL_BITMAP_TOKEN );
281 _mesa_feedback_vertex( ctx,
282 ctx->Current.RasterPos,
283 ctx->Current.RasterColor,
284 ctx->Current.RasterTexCoords );
285 }
286 else {
287 ASSERT(ctx->RenderMode == GL_SELECT);
288 /* Do nothing. See OpenGL Spec, Appendix B, Corollary 6. */
289 }
290 #endif
291
292 /* update raster position */
293 ctx->Current.RasterPos[0] += xmove;
294 ctx->Current.RasterPos[1] += ymove;
295 }
296
297
298 void
299 _mesa_init_drawpix_dispatch(struct _glapi_table *disp)
300 {
301 SET_Bitmap(disp, _mesa_Bitmap);
302 SET_CopyPixels(disp, _mesa_CopyPixels);
303 SET_DrawPixels(disp, _mesa_DrawPixels);
304 }
305
306
307 #endif /* FEATURE_drawpix */