[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 "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),
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 /* Note: this call does state validation */
70 if (!_mesa_valid_to_render(ctx, "glDrawPixels")) {
71 return; /* the error code was recorded */
72 }
73
74 /* GL 3.0 introduced a new restriction on glDrawPixels() over what was in
75 * GL_EXT_texture_integer. From section 3.7.4 ("Rasterization of Pixel
76 * Rectangles) on page 151 of the GL 3.0 specification:
77 *
78 * "If format contains integer components, as shown in table 3.6, an
79 * INVALID OPERATION error is generated."
80 *
81 * Since DrawPixels rendering would be merely undefined if not an error (due
82 * to a lack of defined mapping from integer data to gl_Color fragment shader
83 * input), NVIDIA's implementation also just returns this error despite
84 * exposing GL_EXT_texture_integer, just return an error regardless.
85 */
86 if (_mesa_is_integer_format(format)) {
87 _mesa_error(ctx, GL_INVALID_OPERATION, "glDrawPixels(integer format)");
88 return;
89 }
90
91 if (_mesa_error_check_format_type(ctx, format, type, GL_TRUE)) {
92 return; /* the error code was recorded */
93 }
94
95 if (ctx->RasterDiscard) {
96 return;
97 }
98
99 if (!ctx->Current.RasterPosValid) {
100 return; /* no-op, not an error */
101 }
102
103 if (ctx->RenderMode == GL_RENDER) {
104 if (width > 0 && height > 0) {
105 /* Round, to satisfy conformance tests (matches SGI's OpenGL) */
106 GLint x = IROUND(ctx->Current.RasterPos[0]);
107 GLint y = IROUND(ctx->Current.RasterPos[1]);
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 );
121 }
122 else {
123 ASSERT(ctx->RenderMode == GL_SELECT);
124 /* Do nothing. See OpenGL Spec, Appendix B, Corollary 6. */
125 }
126 }
127
128
129 static void GLAPIENTRY
130 _mesa_CopyPixels( GLint srcx, GLint srcy, GLsizei width, GLsizei height,
131 GLenum type )
132 {
133 GET_CURRENT_CONTEXT(ctx);
134 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
135
136 if (MESA_VERBOSE & VERBOSE_API)
137 _mesa_debug(ctx,
138 "glCopyPixels(%d, %d, %d, %d, %s) // from %s to %s at %d, %d\n",
139 srcx, srcy, width, height,
140 _mesa_lookup_enum_by_nr(type),
141 _mesa_lookup_enum_by_nr(ctx->ReadBuffer->ColorReadBuffer),
142 _mesa_lookup_enum_by_nr(ctx->DrawBuffer->ColorDrawBuffer),
143 IROUND(ctx->Current.RasterPos[0]),
144 IROUND(ctx->Current.RasterPos[1]));
145
146 if (width < 0 || height < 0) {
147 _mesa_error(ctx, GL_INVALID_VALUE, "glCopyPixels(width or height < 0)");
148 return;
149 }
150
151 /* Note: more detailed 'type' checking is done by the
152 * _mesa_source/dest_buffer_exists() calls below. That's where we
153 * check if the stencil buffer exists, etc.
154 */
155 if (type != GL_COLOR &&
156 type != GL_DEPTH &&
157 type != GL_STENCIL) {
158 _mesa_error(ctx, GL_INVALID_ENUM, "glCopyPixels(type=%s)",
159 _mesa_lookup_enum_by_nr(type));
160 return;
161 }
162
163 /* Note: this call does state validation */
164 if (!_mesa_valid_to_render(ctx, "glCopyPixels")) {
165 return; /* the error code was recorded */
166 }
167
168 if (!_mesa_source_buffer_exists(ctx, type) ||
169 !_mesa_dest_buffer_exists(ctx, type)) {
170 _mesa_error(ctx, GL_INVALID_OPERATION,
171 "glCopyPixels(missing source or dest buffer)");
172 return;
173 }
174
175 if (ctx->RasterDiscard) {
176 return;
177 }
178
179 if (!ctx->Current.RasterPosValid || width == 0 || height == 0) {
180 return; /* no-op, not an error */
181 }
182
183 if (ctx->RenderMode == GL_RENDER) {
184 /* Round to satisfy conformance tests (matches SGI's OpenGL) */
185 if (width > 0 && height > 0) {
186 GLint destx = IROUND(ctx->Current.RasterPos[0]);
187 GLint desty = IROUND(ctx->Current.RasterPos[1]);
188 ctx->Driver.CopyPixels( ctx, srcx, srcy, width, height, destx, desty,
189 type );
190 }
191 }
192 else if (ctx->RenderMode == GL_FEEDBACK) {
193 FLUSH_CURRENT( ctx, 0 );
194 _mesa_feedback_token( ctx, (GLfloat) (GLint) GL_COPY_PIXEL_TOKEN );
195 _mesa_feedback_vertex( ctx,
196 ctx->Current.RasterPos,
197 ctx->Current.RasterColor,
198 ctx->Current.RasterTexCoords );
199 }
200 else {
201 ASSERT(ctx->RenderMode == GL_SELECT);
202 /* Do nothing. See OpenGL Spec, Appendix B, Corollary 6. */
203 }
204 }
205
206
207 static void GLAPIENTRY
208 _mesa_Bitmap( GLsizei width, GLsizei height,
209 GLfloat xorig, GLfloat yorig, GLfloat xmove, GLfloat ymove,
210 const GLubyte *bitmap )
211 {
212 GET_CURRENT_CONTEXT(ctx);
213 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
214
215 if (width < 0 || height < 0) {
216 _mesa_error( ctx, GL_INVALID_VALUE, "glBitmap(width or height < 0)" );
217 return;
218 }
219
220 if (!ctx->Current.RasterPosValid) {
221 return; /* do nothing */
222 }
223
224 /* Note: this call does state validation */
225 if (!_mesa_valid_to_render(ctx, "glBitmap")) {
226 /* the error code was recorded */
227 return;
228 }
229
230 if (ctx->RasterDiscard)
231 return;
232
233 if (ctx->RenderMode == GL_RENDER) {
234 /* Truncate, to satisfy conformance tests (matches SGI's OpenGL). */
235 if (width > 0 && height > 0) {
236 const GLfloat epsilon = 0.0001F;
237 GLint x = IFLOOR(ctx->Current.RasterPos[0] + epsilon - xorig);
238 GLint y = IFLOOR(ctx->Current.RasterPos[1] + epsilon - yorig);
239
240 ctx->Driver.Bitmap( ctx, x, y, width, height, &ctx->Unpack, bitmap );
241 }
242 }
243 #if _HAVE_FULL_GL
244 else if (ctx->RenderMode == GL_FEEDBACK) {
245 FLUSH_CURRENT(ctx, 0);
246 _mesa_feedback_token( ctx, (GLfloat) (GLint) GL_BITMAP_TOKEN );
247 _mesa_feedback_vertex( ctx,
248 ctx->Current.RasterPos,
249 ctx->Current.RasterColor,
250 ctx->Current.RasterTexCoords );
251 }
252 else {
253 ASSERT(ctx->RenderMode == GL_SELECT);
254 /* Do nothing. See OpenGL Spec, Appendix B, Corollary 6. */
255 }
256 #endif
257
258 /* update raster position */
259 ctx->Current.RasterPos[0] += xmove;
260 ctx->Current.RasterPos[1] += ymove;
261 }
262
263
264 void
265 _mesa_init_drawpix_dispatch(struct _glapi_table *disp)
266 {
267 SET_Bitmap(disp, _mesa_Bitmap);
268 SET_CopyPixels(disp, _mesa_CopyPixels);
269 SET_DrawPixels(disp, _mesa_DrawPixels);
270 }
271
272
273 #endif /* FEATURE_drawpix */