| Brian Paul | fbd8f21 | 1999-11-11 01:22:25 +0000 | [diff] [blame] | 1 | /* $Id: drawpix.c,v 1.6 1999/11/11 01:22:26 brianp Exp $ */ |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 2 | |
| 3 | /* |
| 4 | * Mesa 3-D graphics library |
| Brian Paul | fbd8f21 | 1999-11-11 01:22:25 +0000 | [diff] [blame] | 5 | * Version: 3.3 |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 6 | * |
| 7 | * Copyright (C) 1999 Brian Paul All Rights Reserved. |
| 8 | * |
| 9 | * Permission is hereby granted, free of charge, to any person obtaining a |
| 10 | * copy of this software and associated documentation files (the "Software"), |
| 11 | * to deal in the Software without restriction, including without limitation |
| 12 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 13 | * and/or sell copies of the Software, and to permit persons to whom the |
| 14 | * Software is furnished to do so, subject to the following conditions: |
| 15 | * |
| 16 | * The above copyright notice and this permission notice shall be included |
| 17 | * in all copies or substantial portions of the Software. |
| 18 | * |
| 19 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 20 | * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 21 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 22 | * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN |
| 23 | * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 24 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 25 | */ |
| 26 | |
| 27 | |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 28 | #ifdef PC_HEADER |
| 29 | #include "all.h" |
| 30 | #else |
| Brian Paul | fbd8f21 | 1999-11-11 01:22:25 +0000 | [diff] [blame] | 31 | #include "glheader.h" |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 32 | #include "context.h" |
| 33 | #include "drawpix.h" |
| 34 | #include "feedback.h" |
| 35 | #include "image.h" |
| 36 | #include "macros.h" |
| 37 | #include "mmath.h" |
| 38 | #include "pixel.h" |
| 39 | #include "span.h" |
| 40 | #include "stencil.h" |
| 41 | #include "types.h" |
| 42 | #include "zoom.h" |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 43 | #endif |
| 44 | |
| 45 | |
| 46 | |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 47 | /* |
| Brian Paul | fbd8f21 | 1999-11-11 01:22:25 +0000 | [diff] [blame] | 48 | * Try to do a fast and simple RGB(a) glDrawPixels. |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 49 | * Return: GL_TRUE if success, GL_FALSE if slow path must be used instead |
| 50 | */ |
| Brian Paul | fbd8f21 | 1999-11-11 01:22:25 +0000 | [diff] [blame] | 51 | static GLboolean |
| 52 | simple_DrawPixels( GLcontext *ctx, GLint x, GLint y, |
| 53 | GLsizei width, GLsizei height, GLenum format, GLenum type, |
| 54 | const GLvoid *pixels ) |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 55 | { |
| Brian Paul | fbd8f21 | 1999-11-11 01:22:25 +0000 | [diff] [blame] | 56 | const struct gl_pixelstore_attrib *unpack = &ctx->Unpack; |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 57 | GLubyte rgb[MAX_WIDTH][3]; |
| 58 | GLubyte rgba[MAX_WIDTH][4]; |
| 59 | |
| 60 | ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH_WITH_RETVAL(ctx, "glDrawPixels", |
| 61 | GL_FALSE); |
| 62 | |
| 63 | |
| 64 | if (!ctx->Current.RasterPosValid) { |
| 65 | /* no-op */ |
| 66 | return GL_TRUE; |
| 67 | } |
| 68 | |
| 69 | if (ctx->NewState) { |
| 70 | gl_update_state(ctx); |
| 71 | } |
| 72 | |
| 73 | /* see if device driver can do the drawpix */ |
| Brian Paul | fbd8f21 | 1999-11-11 01:22:25 +0000 | [diff] [blame] | 74 | if (ctx->Driver.DrawPixels |
| 75 | && (*ctx->Driver.DrawPixels)(ctx, x, y, width, height, format, type, |
| 76 | unpack, pixels)) { |
| 77 | return GL_TRUE; |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | if ((ctx->RasterMask&(~(SCISSOR_BIT|WINCLIP_BIT)))==0 |
| 81 | && ctx->Pixel.RedBias==0.0 && ctx->Pixel.RedScale==1.0 |
| 82 | && ctx->Pixel.GreenBias==0.0 && ctx->Pixel.GreenScale==1.0 |
| 83 | && ctx->Pixel.BlueBias==0.0 && ctx->Pixel.BlueScale==1.0 |
| 84 | && ctx->Pixel.AlphaBias==0.0 && ctx->Pixel.AlphaScale==1.0 |
| 85 | && ctx->Pixel.IndexShift==0 && ctx->Pixel.IndexOffset==0 |
| 86 | && ctx->Pixel.MapColorFlag==0 |
| 87 | && unpack->Alignment==1 |
| 88 | && !unpack->SwapBytes |
| 89 | && !unpack->LsbFirst) { |
| 90 | |
| Brian Paul | fbd8f21 | 1999-11-11 01:22:25 +0000 | [diff] [blame] | 91 | GLint destX = x; |
| 92 | GLint destY = y; |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 93 | GLint drawWidth = width; /* actual width drawn */ |
| 94 | GLint drawHeight = height; /* actual height drawn */ |
| 95 | GLint skipPixels = unpack->SkipPixels; |
| 96 | GLint skipRows = unpack->SkipRows; |
| 97 | GLint rowLength; |
| 98 | GLdepth zSpan[MAX_WIDTH]; /* only used when zooming */ |
| 99 | GLint zoomY0; |
| 100 | |
| 101 | if (unpack->RowLength > 0) |
| 102 | rowLength = unpack->RowLength; |
| 103 | else |
| 104 | rowLength = width; |
| 105 | |
| 106 | /* If we're not using pixel zoom then do all clipping calculations |
| 107 | * now. Otherwise, we'll let the gl_write_zoomed_*_span() functions |
| 108 | * handle the clipping. |
| 109 | */ |
| 110 | if (ctx->Pixel.ZoomX==1.0F && ctx->Pixel.ZoomY==1.0F) { |
| 111 | /* horizontal clipping */ |
| 112 | if (destX < ctx->Buffer->Xmin) { |
| 113 | skipPixels += (ctx->Buffer->Xmin - destX); |
| 114 | drawWidth -= (ctx->Buffer->Xmin - destX); |
| 115 | destX = ctx->Buffer->Xmin; |
| 116 | } |
| 117 | if (destX + drawWidth > ctx->Buffer->Xmax) |
| 118 | drawWidth -= (destX + drawWidth - ctx->Buffer->Xmax - 1); |
| 119 | if (drawWidth <= 0) |
| 120 | return GL_TRUE; |
| 121 | |
| 122 | /* vertical clipping */ |
| 123 | if (destY < ctx->Buffer->Ymin) { |
| 124 | skipRows += (ctx->Buffer->Ymin - destY); |
| 125 | drawHeight -= (ctx->Buffer->Ymin - destY); |
| 126 | destY = ctx->Buffer->Ymin; |
| 127 | } |
| 128 | if (destY + drawHeight > ctx->Buffer->Ymax) |
| 129 | drawHeight -= (destY + drawHeight - ctx->Buffer->Ymax - 1); |
| 130 | if (drawHeight <= 0) |
| 131 | return GL_TRUE; |
| 132 | } |
| 133 | else { |
| 134 | /* setup array of fragment Z value to pass to zoom function */ |
| 135 | GLdepth z = (GLdepth) (ctx->Current.RasterPos[2] * DEPTH_SCALE); |
| 136 | GLint i; |
| 137 | assert(drawWidth < MAX_WIDTH); |
| 138 | for (i=0; i<drawWidth; i++) |
| 139 | zSpan[i] = z; |
| 140 | |
| 141 | /* save Y value of first row */ |
| 142 | zoomY0 = (GLint) (ctx->Current.RasterPos[1] + 0.5F); |
| 143 | } |
| 144 | |
| 145 | |
| 146 | /* |
| 147 | * Ready to draw! |
| 148 | * The window region at (destX, destY) of size (drawWidth, drawHeight) |
| 149 | * will be written to. |
| 150 | * We'll take pixel data from buffer pointed to by "pixels" but we'll |
| 151 | * skip "skipRows" rows and skip "skipPixels" pixels/row. |
| 152 | */ |
| 153 | |
| 154 | if (format==GL_RGBA && type==GL_UNSIGNED_BYTE) { |
| 155 | if (ctx->Visual->RGBAflag) { |
| 156 | GLubyte *src = (GLubyte *) pixels |
| 157 | + (skipRows * rowLength + skipPixels) * 4; |
| 158 | if (ctx->Pixel.ZoomX==1.0F && ctx->Pixel.ZoomY==1.0F) { |
| 159 | /* no zooming */ |
| 160 | GLint row; |
| 161 | for (row=0; row<drawHeight; row++) { |
| 162 | (*ctx->Driver.WriteRGBASpan)(ctx, drawWidth, destX, destY, |
| 163 | (void *) src, NULL); |
| 164 | src += rowLength * 4; |
| 165 | destY++; |
| 166 | } |
| 167 | } |
| 168 | else { |
| 169 | /* with zooming */ |
| 170 | GLint row; |
| 171 | for (row=0; row<drawHeight; row++) { |
| 172 | gl_write_zoomed_rgba_span(ctx, drawWidth, destX, destY, |
| 173 | zSpan, (void *) src, zoomY0); |
| 174 | src += rowLength * 4; |
| 175 | destY++; |
| 176 | } |
| 177 | } |
| 178 | } |
| 179 | return GL_TRUE; |
| 180 | } |
| 181 | else if (format==GL_RGB && type==GL_UNSIGNED_BYTE) { |
| 182 | if (ctx->Visual->RGBAflag) { |
| 183 | GLubyte *src = (GLubyte *) pixels |
| 184 | + (skipRows * rowLength + skipPixels) * 3; |
| 185 | if (ctx->Pixel.ZoomX==1.0F && ctx->Pixel.ZoomY==1.0F) { |
| 186 | GLint row; |
| 187 | for (row=0; row<drawHeight; row++) { |
| 188 | (*ctx->Driver.WriteRGBSpan)(ctx, drawWidth, destX, destY, |
| 189 | (void *) src, NULL); |
| 190 | src += rowLength * 3; |
| 191 | destY++; |
| 192 | } |
| 193 | } |
| 194 | else { |
| 195 | /* with zooming */ |
| 196 | GLint row; |
| 197 | for (row=0; row<drawHeight; row++) { |
| 198 | gl_write_zoomed_rgb_span(ctx, drawWidth, destX, destY, |
| 199 | zSpan, (void *) src, zoomY0); |
| 200 | src += rowLength * 3; |
| 201 | destY++; |
| 202 | } |
| 203 | } |
| 204 | } |
| 205 | return GL_TRUE; |
| 206 | } |
| 207 | else if (format==GL_LUMINANCE && type==GL_UNSIGNED_BYTE) { |
| 208 | if (ctx->Visual->RGBAflag) { |
| 209 | GLubyte *src = (GLubyte *) pixels |
| 210 | + (skipRows * rowLength + skipPixels); |
| 211 | if (ctx->Pixel.ZoomX==1.0F && ctx->Pixel.ZoomY==1.0F) { |
| 212 | /* no zooming */ |
| 213 | GLint row; |
| 214 | assert(drawWidth < MAX_WIDTH); |
| 215 | for (row=0; row<drawHeight; row++) { |
| 216 | GLint i; |
| 217 | for (i=0;i<drawWidth;i++) { |
| 218 | rgb[i][0] = src[i]; |
| 219 | rgb[i][1] = src[i]; |
| 220 | rgb[i][2] = src[i]; |
| 221 | } |
| 222 | (*ctx->Driver.WriteRGBSpan)(ctx, drawWidth, destX, destY, |
| 223 | (void *) rgb, NULL); |
| 224 | src += rowLength; |
| 225 | destY++; |
| 226 | } |
| 227 | } |
| 228 | else { |
| 229 | /* with zooming */ |
| 230 | GLint row; |
| 231 | assert(drawWidth < MAX_WIDTH); |
| 232 | for (row=0; row<drawHeight; row++) { |
| 233 | GLint i; |
| 234 | for (i=0;i<drawWidth;i++) { |
| 235 | rgb[i][0] = src[i]; |
| 236 | rgb[i][1] = src[i]; |
| 237 | rgb[i][2] = src[i]; |
| 238 | } |
| 239 | gl_write_zoomed_rgb_span(ctx, drawWidth, destX, destY, |
| 240 | zSpan, (void *) rgb, zoomY0); |
| 241 | src += rowLength; |
| 242 | destY++; |
| 243 | } |
| 244 | } |
| 245 | } |
| 246 | return GL_TRUE; |
| 247 | } |
| 248 | else if (format==GL_LUMINANCE_ALPHA && type==GL_UNSIGNED_BYTE) { |
| 249 | if (ctx->Visual->RGBAflag) { |
| 250 | GLubyte *src = (GLubyte *) pixels |
| 251 | + (skipRows * rowLength + skipPixels)*2; |
| 252 | if (ctx->Pixel.ZoomX==1.0F && ctx->Pixel.ZoomY==1.0F) { |
| 253 | /* no zooming */ |
| 254 | GLint row; |
| 255 | assert(drawWidth < MAX_WIDTH); |
| 256 | for (row=0; row<drawHeight; row++) { |
| 257 | GLint i; |
| 258 | GLubyte *ptr = src; |
| 259 | for (i=0;i<drawWidth;i++) { |
| 260 | rgba[i][0] = *ptr; |
| 261 | rgba[i][1] = *ptr; |
| 262 | rgba[i][2] = *ptr++; |
| 263 | rgba[i][3] = *ptr++; |
| 264 | } |
| 265 | (*ctx->Driver.WriteRGBASpan)(ctx, drawWidth, destX, destY, |
| 266 | (void *) rgba, NULL); |
| 267 | src += rowLength*2; |
| 268 | destY++; |
| 269 | } |
| 270 | } |
| 271 | else { |
| 272 | /* with zooming */ |
| 273 | GLint row; |
| 274 | assert(drawWidth < MAX_WIDTH); |
| 275 | for (row=0; row<drawHeight; row++) { |
| 276 | GLubyte *ptr = src; |
| 277 | GLint i; |
| 278 | for (i=0;i<drawWidth;i++) { |
| 279 | rgba[i][0] = *ptr; |
| 280 | rgba[i][1] = *ptr; |
| 281 | rgba[i][2] = *ptr++; |
| 282 | rgba[i][3] = *ptr++; |
| 283 | } |
| 284 | gl_write_zoomed_rgba_span(ctx, drawWidth, destX, destY, |
| 285 | zSpan, (void *) rgba, zoomY0); |
| 286 | src += rowLength*2; |
| 287 | destY++; |
| 288 | } |
| 289 | } |
| 290 | } |
| 291 | return GL_TRUE; |
| 292 | } |
| 293 | else if (format==GL_COLOR_INDEX && type==GL_UNSIGNED_BYTE) { |
| 294 | GLubyte *src = (GLubyte *) pixels + skipRows * rowLength + skipPixels; |
| 295 | if (ctx->Visual->RGBAflag) { |
| 296 | /* convert CI data to RGBA */ |
| 297 | if (ctx->Pixel.ZoomX==1.0F && ctx->Pixel.ZoomY==1.0F) { |
| 298 | /* no zooming */ |
| 299 | GLint row; |
| 300 | for (row=0; row<drawHeight; row++) { |
| 301 | assert(drawWidth < MAX_WIDTH); |
| 302 | gl_map_ci8_to_rgba(ctx, drawWidth, src, rgba); |
| 303 | (*ctx->Driver.WriteRGBASpan)(ctx, drawWidth, destX, destY, |
| 304 | (const GLubyte (*)[4])rgba, |
| 305 | NULL); |
| 306 | src += rowLength; |
| 307 | destY++; |
| 308 | } |
| 309 | return GL_TRUE; |
| 310 | } |
| 311 | else { |
| 312 | /* with zooming */ |
| 313 | GLint row; |
| 314 | for (row=0; row<drawHeight; row++) { |
| 315 | assert(drawWidth < MAX_WIDTH); |
| 316 | gl_map_ci8_to_rgba(ctx, drawWidth, src, rgba); |
| 317 | gl_write_zoomed_rgba_span(ctx, drawWidth, destX, destY, |
| 318 | zSpan, (void *) rgba, zoomY0); |
| 319 | src += rowLength; |
| 320 | destY++; |
| 321 | } |
| 322 | return GL_TRUE; |
| 323 | } |
| 324 | } |
| 325 | else { |
| 326 | /* write CI data to CI frame buffer */ |
| 327 | GLint row; |
| 328 | if (ctx->Pixel.ZoomX==1.0F && ctx->Pixel.ZoomY==1.0F) { |
| 329 | /* no zooming */ |
| 330 | for (row=0; row<drawHeight; row++) { |
| 331 | (*ctx->Driver.WriteCI8Span)(ctx, drawWidth, destX, destY, |
| 332 | src, NULL); |
| 333 | src += rowLength; |
| 334 | destY++; |
| 335 | } |
| 336 | return GL_TRUE; |
| 337 | } |
| 338 | else { |
| 339 | /* with zooming */ |
| 340 | return GL_FALSE; |
| 341 | } |
| 342 | } |
| 343 | } |
| 344 | else { |
| 345 | /* can't handle this pixel format and/or data type here */ |
| 346 | return GL_FALSE; |
| 347 | } |
| 348 | } |
| Brian Paul | fbd8f21 | 1999-11-11 01:22:25 +0000 | [diff] [blame] | 349 | |
| 350 | /* can't do a simple draw, have to use slow path */ |
| 351 | return GL_FALSE; |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 352 | } |
| 353 | |
| 354 | |
| 355 | |
| 356 | /* |
| 357 | * Do glDrawPixels of index pixels. |
| 358 | */ |
| Brian Paul | fbd8f21 | 1999-11-11 01:22:25 +0000 | [diff] [blame] | 359 | static void |
| 360 | draw_index_pixels( GLcontext *ctx, GLint x, GLint y, |
| 361 | GLsizei width, GLsizei height, |
| 362 | GLenum type, const GLvoid *pixels ) |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 363 | { |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 364 | const GLboolean zoom = ctx->Pixel.ZoomX!=1.0 || ctx->Pixel.ZoomY!=1.0; |
| Brian Paul | fbd8f21 | 1999-11-11 01:22:25 +0000 | [diff] [blame] | 365 | const GLint desty = y; |
| 366 | GLint row, drawWidth; |
| 367 | GLdepth zspan[MAX_WIDTH]; |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 368 | |
| Brian Paul | fbd8f21 | 1999-11-11 01:22:25 +0000 | [diff] [blame] | 369 | drawWidth = (width > MAX_WIDTH) ? MAX_WIDTH : width; |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 370 | |
| 371 | /* Fragment depth values */ |
| 372 | if (ctx->Depth.Test) { |
| 373 | GLdepth zval = (GLdepth) (ctx->Current.RasterPos[2] * DEPTH_SCALE); |
| Brian Paul | fbd8f21 | 1999-11-11 01:22:25 +0000 | [diff] [blame] | 374 | GLint i; |
| 375 | for (i = 0; i < drawWidth; i++) { |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 376 | zspan[i] = zval; |
| 377 | } |
| 378 | } |
| 379 | |
| Brian Paul | fbd8f21 | 1999-11-11 01:22:25 +0000 | [diff] [blame] | 380 | /* |
| 381 | * General solution |
| 382 | */ |
| 383 | for (row = 0; row < height; row++, y++) { |
| 384 | GLuint indexes[MAX_WIDTH]; |
| 385 | const GLvoid *source = gl_pixel_addr_in_image(&ctx->Unpack, |
| 386 | pixels, width, height, GL_COLOR_INDEX, type, 0, row, 0); |
| 387 | _mesa_unpack_index_span(ctx, drawWidth, GL_UNSIGNED_INT, indexes, |
| 388 | type, source, &ctx->Unpack, GL_TRUE); |
| 389 | if (zoom) { |
| 390 | gl_write_zoomed_index_span(ctx, drawWidth, x, y, zspan, indexes, desty); |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 391 | } |
| 392 | else { |
| Brian Paul | fbd8f21 | 1999-11-11 01:22:25 +0000 | [diff] [blame] | 393 | gl_write_index_span(ctx, drawWidth, x, y, zspan, indexes, GL_BITMAP); |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 394 | } |
| 395 | } |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 396 | } |
| 397 | |
| 398 | |
| 399 | |
| 400 | /* |
| 401 | * Do glDrawPixels of stencil image. The image datatype may either |
| 402 | * be GLubyte or GLbitmap. |
| 403 | */ |
| Brian Paul | fbd8f21 | 1999-11-11 01:22:25 +0000 | [diff] [blame] | 404 | static void |
| 405 | draw_stencil_pixels( GLcontext *ctx, GLint x, GLint y, |
| 406 | GLsizei width, GLsizei height, |
| 407 | GLenum type, const GLvoid *pixels ) |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 408 | { |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 409 | const GLboolean zoom = ctx->Pixel.ZoomX!=1.0 || ctx->Pixel.ZoomY!=1.0; |
| Brian Paul | fbd8f21 | 1999-11-11 01:22:25 +0000 | [diff] [blame] | 410 | const GLint desty = y; |
| 411 | GLint row, drawWidth; |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 412 | |
| Brian Paul | fbd8f21 | 1999-11-11 01:22:25 +0000 | [diff] [blame] | 413 | if (type != GL_BYTE && |
| 414 | type != GL_UNSIGNED_BYTE && |
| 415 | type != GL_SHORT && |
| 416 | type != GL_UNSIGNED_SHORT && |
| 417 | type != GL_INT && |
| 418 | type != GL_UNSIGNED_INT && |
| 419 | type != GL_FLOAT && |
| 420 | type != GL_BITMAP) { |
| 421 | gl_error( ctx, GL_INVALID_ENUM, "glDrawPixels(stencil type)"); |
| Brian Paul | bc41b08 | 1999-10-19 20:33:57 +0000 | [diff] [blame] | 422 | return; |
| 423 | } |
| 424 | |
| Brian Paul | fbd8f21 | 1999-11-11 01:22:25 +0000 | [diff] [blame] | 425 | drawWidth = (width > MAX_WIDTH) ? MAX_WIDTH : width; |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 426 | |
| Brian Paul | fbd8f21 | 1999-11-11 01:22:25 +0000 | [diff] [blame] | 427 | for (row = 0; row < height; row++, y++) { |
| 428 | GLstencil values[MAX_WIDTH]; |
| 429 | GLenum destType = (sizeof(GLstencil) == sizeof(GLubyte)) |
| 430 | ? GL_UNSIGNED_BYTE : GL_UNSIGNED_SHORT; |
| 431 | const GLvoid *source = gl_pixel_addr_in_image(&ctx->Unpack, |
| 432 | pixels, width, height, GL_COLOR_INDEX, type, 0, row, 0); |
| 433 | _mesa_unpack_index_span(ctx, drawWidth, destType, values, |
| 434 | type, source, &ctx->Unpack, GL_TRUE); |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 435 | |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 436 | if (zoom) { |
| Brian Paul | fbd8f21 | 1999-11-11 01:22:25 +0000 | [diff] [blame] | 437 | gl_write_zoomed_stencil_span( ctx, (GLuint) drawWidth, x, y, |
| 438 | values, desty ); |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 439 | } |
| 440 | else { |
| Brian Paul | fbd8f21 | 1999-11-11 01:22:25 +0000 | [diff] [blame] | 441 | gl_write_stencil_span( ctx, (GLuint) drawWidth, x, y, values ); |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 442 | } |
| 443 | } |
| 444 | } |
| 445 | |
| 446 | |
| 447 | |
| 448 | /* |
| 449 | * Do a glDrawPixels of depth values. |
| 450 | */ |
| Brian Paul | fbd8f21 | 1999-11-11 01:22:25 +0000 | [diff] [blame] | 451 | static void |
| 452 | draw_depth_pixels( GLcontext *ctx, GLint x, GLint y, |
| 453 | GLsizei width, GLsizei height, |
| 454 | GLenum type, const GLvoid *pixels ) |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 455 | { |
| Brian Paul | fbd8f21 | 1999-11-11 01:22:25 +0000 | [diff] [blame] | 456 | const GLboolean bias_or_scale = ctx->Pixel.DepthBias!=0.0 || ctx->Pixel.DepthScale!=1.0; |
| 457 | const GLboolean zoom = ctx->Pixel.ZoomX!=1.0 || ctx->Pixel.ZoomY!=1.0; |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 458 | const GLint desty = y; |
| 459 | GLubyte rgba[MAX_WIDTH][4]; |
| 460 | GLuint ispan[MAX_WIDTH]; |
| Brian Paul | fbd8f21 | 1999-11-11 01:22:25 +0000 | [diff] [blame] | 461 | GLint drawWidth = (width > MAX_WIDTH) ? MAX_WIDTH : width; |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 462 | |
| Brian Paul | fbd8f21 | 1999-11-11 01:22:25 +0000 | [diff] [blame] | 463 | if (type != GL_UNSIGNED_BYTE |
| 464 | && type != GL_UNSIGNED_BYTE |
| 465 | && type != GL_UNSIGNED_SHORT |
| 466 | && type != GL_UNSIGNED_SHORT |
| 467 | && type != GL_UNSIGNED_INT |
| 468 | && type != GL_UNSIGNED_INT |
| 469 | && type != GL_FLOAT) { |
| 470 | gl_error(ctx, GL_INVALID_ENUM, "glDrawPixels(type)"); |
| 471 | return; |
| 472 | } |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 473 | |
| Brian Paul | fbd8f21 | 1999-11-11 01:22:25 +0000 | [diff] [blame] | 474 | /* Colors or indexes */ |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 475 | if (ctx->Visual->RGBAflag) { |
| 476 | GLint r = (GLint) (ctx->Current.RasterColor[0] * 255.0F); |
| 477 | GLint g = (GLint) (ctx->Current.RasterColor[1] * 255.0F); |
| 478 | GLint b = (GLint) (ctx->Current.RasterColor[2] * 255.0F); |
| 479 | GLint a = (GLint) (ctx->Current.RasterColor[3] * 255.0F); |
| 480 | GLint i; |
| Brian Paul | fbd8f21 | 1999-11-11 01:22:25 +0000 | [diff] [blame] | 481 | for (i = 0; i < drawWidth; i++) { |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 482 | rgba[i][RCOMP] = r; |
| 483 | rgba[i][GCOMP] = g; |
| 484 | rgba[i][BCOMP] = b; |
| 485 | rgba[i][ACOMP] = a; |
| 486 | } |
| 487 | } |
| 488 | else { |
| 489 | GLint i; |
| Brian Paul | fbd8f21 | 1999-11-11 01:22:25 +0000 | [diff] [blame] | 490 | for (i = 0; i < drawWidth; i++) { |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 491 | ispan[i] = ctx->Current.RasterIndex; |
| 492 | } |
| 493 | } |
| 494 | |
| Brian Paul | fbd8f21 | 1999-11-11 01:22:25 +0000 | [diff] [blame] | 495 | if (type==GL_UNSIGNED_SHORT && sizeof(GLdepth)==sizeof(GLushort) |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 496 | && !bias_or_scale && !zoom && ctx->Visual->RGBAflag) { |
| 497 | /* Special case: directly write 16-bit depth values */ |
| Brian Paul | fbd8f21 | 1999-11-11 01:22:25 +0000 | [diff] [blame] | 498 | GLint row; |
| 499 | for (row = 0; row < height; row++, y++) { |
| 500 | const GLdepth *zptr = gl_pixel_addr_in_image(&ctx->Unpack, |
| 501 | pixels, width, height, GL_DEPTH_COMPONENT, type, 0, row, 0); |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 502 | gl_write_rgba_span( ctx, width, x, y, zptr, rgba, GL_BITMAP ); |
| 503 | } |
| 504 | } |
| Brian Paul | fbd8f21 | 1999-11-11 01:22:25 +0000 | [diff] [blame] | 505 | else if (type==GL_UNSIGNED_INT && sizeof(GLdepth)==sizeof(GLuint) |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 506 | && !bias_or_scale && !zoom && ctx->Visual->RGBAflag) { |
| 507 | /* Special case: directly write 32-bit depth values */ |
| Brian Paul | fbd8f21 | 1999-11-11 01:22:25 +0000 | [diff] [blame] | 508 | GLint i, row; |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 509 | /* Compute shift value to scale 32-bit uints down to depth values. */ |
| 510 | GLuint shift = 0; |
| 511 | GLuint max = MAX_DEPTH; |
| Brian Paul | fbd8f21 | 1999-11-11 01:22:25 +0000 | [diff] [blame] | 512 | while ((max & 0x80000000) == 0) { |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 513 | max = max << 1; |
| 514 | shift++; |
| 515 | } |
| Brian Paul | fbd8f21 | 1999-11-11 01:22:25 +0000 | [diff] [blame] | 516 | for (row = 0; row < height; row++, y++) { |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 517 | GLdepth zspan[MAX_WIDTH]; |
| Brian Paul | fbd8f21 | 1999-11-11 01:22:25 +0000 | [diff] [blame] | 518 | const GLdepth *zptr = gl_pixel_addr_in_image(&ctx->Unpack, |
| 519 | pixels, width, height, GL_DEPTH_COMPONENT, type, 0, row, 0); |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 520 | for (i=0;i<width;i++) { |
| 521 | zspan[i] = zptr[i] >> shift; |
| 522 | } |
| 523 | gl_write_rgba_span( ctx, width, x, y, zspan, rgba, GL_BITMAP ); |
| 524 | } |
| 525 | } |
| 526 | else { |
| Brian Paul | fbd8f21 | 1999-11-11 01:22:25 +0000 | [diff] [blame] | 527 | /* General case */ |
| 528 | GLint row; |
| 529 | for (row = 0; row < height; row++, y++) { |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 530 | GLdepth zspan[MAX_WIDTH]; |
| Brian Paul | fbd8f21 | 1999-11-11 01:22:25 +0000 | [diff] [blame] | 531 | const GLvoid *src = gl_pixel_addr_in_image(&ctx->Unpack, |
| 532 | pixels, width, height, GL_DEPTH_COMPONENT, type, 0, row, 0); |
| 533 | _mesa_unpack_depth_span( ctx, drawWidth, zspan, type, src, |
| 534 | &ctx->Unpack, GL_TRUE ); |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 535 | if (ctx->Visual->RGBAflag) { |
| 536 | if (zoom) { |
| Brian Paul | fbd8f21 | 1999-11-11 01:22:25 +0000 | [diff] [blame] | 537 | gl_write_zoomed_rgba_span(ctx, width, x, y, zspan, |
| 538 | (const GLubyte (*)[4])rgba, desty); |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 539 | } |
| 540 | else { |
| Brian Paul | fbd8f21 | 1999-11-11 01:22:25 +0000 | [diff] [blame] | 541 | gl_write_rgba_span(ctx, width, x, y, zspan, rgba, GL_BITMAP); |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 542 | } |
| 543 | } |
| 544 | else { |
| 545 | if (zoom) { |
| Brian Paul | fbd8f21 | 1999-11-11 01:22:25 +0000 | [diff] [blame] | 546 | gl_write_zoomed_index_span(ctx, width, x, y, zspan, |
| 547 | ispan, GL_BITMAP); |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 548 | } |
| 549 | else { |
| Brian Paul | fbd8f21 | 1999-11-11 01:22:25 +0000 | [diff] [blame] | 550 | gl_write_index_span(ctx, width, x, y, zspan, ispan, GL_BITMAP); |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 551 | } |
| 552 | } |
| 553 | |
| 554 | } |
| 555 | } |
| 556 | } |
| 557 | |
| 558 | |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 559 | /* |
| 560 | * Do glDrawPixels of RGBA pixels. |
| 561 | */ |
| Brian Paul | fbd8f21 | 1999-11-11 01:22:25 +0000 | [diff] [blame] | 562 | static void |
| 563 | draw_rgba_pixels( GLcontext *ctx, GLint x, GLint y, |
| 564 | GLsizei width, GLsizei height, |
| 565 | GLenum format, GLenum type, const GLvoid *pixels ) |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 566 | { |
| Brian Paul | fbd8f21 | 1999-11-11 01:22:25 +0000 | [diff] [blame] | 567 | const struct gl_pixelstore_attrib *unpack = &ctx->Unpack; |
| 568 | const GLboolean zoom = ctx->Pixel.ZoomX!=1.0 || ctx->Pixel.ZoomY!=1.0; |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 569 | const GLint desty = y; |
| 570 | GLdepth zspan[MAX_WIDTH]; |
| 571 | GLboolean quickDraw; |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 572 | |
| 573 | /* Try an optimized glDrawPixels first */ |
| Brian Paul | fbd8f21 | 1999-11-11 01:22:25 +0000 | [diff] [blame] | 574 | if (simple_DrawPixels(ctx, x, y, width, height, format, type, pixels)) |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 575 | return; |
| 576 | |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 577 | /* Fragment depth values */ |
| 578 | if (ctx->Depth.Test) { |
| 579 | /* fill in array of z values */ |
| 580 | GLdepth z = (GLdepth) (ctx->Current.RasterPos[2] * DEPTH_SCALE); |
| Brian Paul | fbd8f21 | 1999-11-11 01:22:25 +0000 | [diff] [blame] | 581 | GLint i; |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 582 | for (i=0;i<width;i++) { |
| 583 | zspan[i] = z; |
| 584 | } |
| 585 | } |
| 586 | |
| Brian Paul | fbd8f21 | 1999-11-11 01:22:25 +0000 | [diff] [blame] | 587 | |
| 588 | if (ctx->RasterMask == 0 && !zoom |
| 589 | && x >= 0 && y >= 0 |
| 590 | && x + width <= ctx->Buffer->Width |
| 591 | && y + height <= ctx->Buffer->Height) { |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 592 | quickDraw = GL_TRUE; |
| 593 | } |
| 594 | else { |
| 595 | quickDraw = GL_FALSE; |
| 596 | } |
| 597 | |
| Brian Paul | fbd8f21 | 1999-11-11 01:22:25 +0000 | [diff] [blame] | 598 | /* |
| 599 | * General solution |
| 600 | */ |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 601 | { |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 602 | GLubyte rgba[MAX_WIDTH][4]; |
| Brian Paul | fbd8f21 | 1999-11-11 01:22:25 +0000 | [diff] [blame] | 603 | GLint row; |
| 604 | if (width > MAX_WIDTH) |
| 605 | width = MAX_WIDTH; |
| 606 | for (row = 0; row < height; row++, y++) { |
| 607 | const GLvoid *source = gl_pixel_addr_in_image(unpack, |
| 608 | pixels, width, height, format, type, 0, row, 0); |
| 609 | _mesa_unpack_ubyte_color_span(ctx, width, GL_RGBA, (void*) rgba, |
| 610 | format, type, source, unpack, GL_TRUE); |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 611 | |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 612 | if (quickDraw) { |
| Brian Paul | fbd8f21 | 1999-11-11 01:22:25 +0000 | [diff] [blame] | 613 | (*ctx->Driver.WriteRGBASpan)( ctx, width, x, y, |
| 614 | (CONST GLubyte (*)[]) rgba, NULL); |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 615 | } |
| 616 | else if (zoom) { |
| 617 | gl_write_zoomed_rgba_span( ctx, width, x, y, zspan, |
| Brian Paul | fbd8f21 | 1999-11-11 01:22:25 +0000 | [diff] [blame] | 618 | (CONST GLubyte (*)[]) rgba, desty ); |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 619 | } |
| 620 | else { |
| 621 | gl_write_rgba_span( ctx, (GLuint) width, x, y, zspan, rgba, GL_BITMAP); |
| 622 | } |
| 623 | } |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 624 | } |
| 625 | } |
| 626 | |
| 627 | |
| 628 | |
| 629 | /* |
| 630 | * Execute glDrawPixels |
| 631 | */ |
| Brian Paul | fbd8f21 | 1999-11-11 01:22:25 +0000 | [diff] [blame] | 632 | void |
| 633 | _mesa_DrawPixels( GLsizei width, GLsizei height, |
| 634 | GLenum format, GLenum type, const GLvoid *pixels ) |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 635 | { |
| Brian Paul | fbd8f21 | 1999-11-11 01:22:25 +0000 | [diff] [blame] | 636 | GET_CURRENT_CONTEXT(ctx); |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 637 | ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glDrawPixels"); |
| 638 | |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 639 | if (ctx->RenderMode==GL_RENDER) { |
| 640 | GLint x, y; |
| Brian Paul | fbd8f21 | 1999-11-11 01:22:25 +0000 | [diff] [blame] | 641 | if (!pixels || !ctx->Current.RasterPosValid) { |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 642 | return; |
| 643 | } |
| 644 | |
| 645 | x = (GLint) (ctx->Current.RasterPos[0] + 0.5F); |
| 646 | y = (GLint) (ctx->Current.RasterPos[1] + 0.5F); |
| 647 | |
| Brian Paul | fbd8f21 | 1999-11-11 01:22:25 +0000 | [diff] [blame] | 648 | switch (format) { |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 649 | case GL_STENCIL_INDEX: |
| Brian Paul | fbd8f21 | 1999-11-11 01:22:25 +0000 | [diff] [blame] | 650 | draw_stencil_pixels( ctx, x, y, width, height, type, pixels ); |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 651 | break; |
| 652 | case GL_DEPTH_COMPONENT: |
| Brian Paul | fbd8f21 | 1999-11-11 01:22:25 +0000 | [diff] [blame] | 653 | draw_depth_pixels( ctx, x, y, width, height, type, pixels ); |
| 654 | break; |
| 655 | case GL_COLOR_INDEX: |
| 656 | if (ctx->Visual->RGBAflag) |
| 657 | draw_index_pixels(ctx, x, y, width, height, type, pixels); |
| 658 | else |
| 659 | draw_rgba_pixels(ctx, x,y, width, height, format, type, pixels); |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 660 | break; |
| 661 | case GL_RED: |
| 662 | case GL_GREEN: |
| 663 | case GL_BLUE: |
| 664 | case GL_ALPHA: |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 665 | case GL_LUMINANCE: |
| 666 | case GL_LUMINANCE_ALPHA: |
| Brian Paul | fbd8f21 | 1999-11-11 01:22:25 +0000 | [diff] [blame] | 667 | case GL_RGB: |
| 668 | case GL_BGR: |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 669 | case GL_RGBA: |
| Brian Paul | fbd8f21 | 1999-11-11 01:22:25 +0000 | [diff] [blame] | 670 | case GL_BGRA: |
| 671 | case GL_ABGR_EXT: |
| 672 | draw_rgba_pixels(ctx, x, y, width, height, format, type, pixels); |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 673 | break; |
| 674 | default: |
| Brian Paul | fbd8f21 | 1999-11-11 01:22:25 +0000 | [diff] [blame] | 675 | gl_error( ctx, GL_INVALID_ENUM, "glDrawPixels(format)" ); |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 676 | return; |
| 677 | } |
| 678 | } |
| 679 | else if (ctx->RenderMode==GL_FEEDBACK) { |
| 680 | if (ctx->Current.RasterPosValid) { |
| 681 | GLfloat color[4]; |
| 682 | GLfloat texcoord[4], invq; |
| 683 | UBYTE_RGBA_TO_FLOAT_RGBA(color, ctx->Current.ByteColor); |
| 684 | invq = 1.0F / ctx->Current.Texcoord[0][3]; |
| 685 | texcoord[0] = ctx->Current.Texcoord[0][0] * invq; |
| 686 | texcoord[1] = ctx->Current.Texcoord[0][1] * invq; |
| 687 | texcoord[2] = ctx->Current.Texcoord[0][2] * invq; |
| 688 | texcoord[3] = ctx->Current.Texcoord[0][3]; |
| 689 | FEEDBACK_TOKEN( ctx, (GLfloat) (GLint) GL_DRAW_PIXEL_TOKEN ); |
| 690 | gl_feedback_vertex( ctx, |
| Keith Whitwell | 1bf9dfa | 1999-09-18 20:41:22 +0000 | [diff] [blame] | 691 | ctx->Current.RasterPos, |
| jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 692 | color, ctx->Current.Index, texcoord ); |
| 693 | } |
| 694 | } |
| 695 | else if (ctx->RenderMode==GL_SELECT) { |
| 696 | if (ctx->Current.RasterPosValid) { |
| 697 | gl_update_hitflag( ctx, ctx->Current.RasterPos[2] ); |
| 698 | } |
| 699 | } |
| 700 | } |
| 701 | |