2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
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:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
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.
28 * Pixel transfer functions (glPixelZoom, glPixelMap, glPixelTransfer)
32 #include "bufferobj.h"
36 #include "mfeatures.h"
40 #include "main/dispatch.h"
43 #if FEATURE_pixel_transfer
46 /**********************************************************************/
47 /***** glPixelZoom *****/
48 /**********************************************************************/
50 static void GLAPIENTRY
51 _mesa_PixelZoom( GLfloat xfactor, GLfloat yfactor )
53 GET_CURRENT_CONTEXT(ctx);
55 ASSERT_OUTSIDE_BEGIN_END(ctx);
57 if (ctx->Pixel.ZoomX == xfactor &&
58 ctx->Pixel.ZoomY == yfactor)
61 FLUSH_VERTICES(ctx, _NEW_PIXEL);
62 ctx->Pixel.ZoomX = xfactor;
63 ctx->Pixel.ZoomY = yfactor;
68 /**********************************************************************/
69 /***** glPixelMap *****/
70 /**********************************************************************/
73 * Return pointer to a pixelmap by name.
75 static struct gl_pixelmap *
76 get_pixelmap(struct gl_context *ctx, GLenum map)
79 case GL_PIXEL_MAP_I_TO_I:
80 return &ctx->PixelMaps.ItoI;
81 case GL_PIXEL_MAP_S_TO_S:
82 return &ctx->PixelMaps.StoS;
83 case GL_PIXEL_MAP_I_TO_R:
84 return &ctx->PixelMaps.ItoR;
85 case GL_PIXEL_MAP_I_TO_G:
86 return &ctx->PixelMaps.ItoG;
87 case GL_PIXEL_MAP_I_TO_B:
88 return &ctx->PixelMaps.ItoB;
89 case GL_PIXEL_MAP_I_TO_A:
90 return &ctx->PixelMaps.ItoA;
91 case GL_PIXEL_MAP_R_TO_R:
92 return &ctx->PixelMaps.RtoR;
93 case GL_PIXEL_MAP_G_TO_G:
94 return &ctx->PixelMaps.GtoG;
95 case GL_PIXEL_MAP_B_TO_B:
96 return &ctx->PixelMaps.BtoB;
97 case GL_PIXEL_MAP_A_TO_A:
98 return &ctx->PixelMaps.AtoA;
106 * Helper routine used by the other _mesa_PixelMap() functions.
109 store_pixelmap(struct gl_context *ctx, GLenum map, GLsizei mapsize,
110 const GLfloat *values)
113 struct gl_pixelmap *pm = get_pixelmap(ctx, map);
115 _mesa_error(ctx, GL_INVALID_ENUM, "glPixelMap(map)");
120 case GL_PIXEL_MAP_S_TO_S:
122 ctx->PixelMaps.StoS.Size = mapsize;
123 for (i = 0; i < mapsize; i++) {
124 ctx->PixelMaps.StoS.Map[i] = (GLfloat)IROUND(values[i]);
127 case GL_PIXEL_MAP_I_TO_I:
129 ctx->PixelMaps.ItoI.Size = mapsize;
130 for (i = 0; i < mapsize; i++) {
131 ctx->PixelMaps.ItoI.Map[i] = values[i];
137 for (i = 0; i < mapsize; i++) {
138 GLfloat val = CLAMP(values[i], 0.0F, 1.0F);
140 pm->Map8[i] = (GLint) (val * 255.0F);
147 * Convenience wrapper for _mesa_validate_pbo_access() for gl[Get]PixelMap().
150 validate_pbo_access(struct gl_context *ctx,
151 struct gl_pixelstore_attrib *pack, GLsizei mapsize,
152 GLenum format, GLenum type, GLsizei clientMemSize,
157 /* Note, need to use DefaultPacking and Unpack's buffer object */
158 _mesa_reference_buffer_object(ctx,
159 &ctx->DefaultPacking.BufferObj,
162 ok = _mesa_validate_pbo_access(1, &ctx->DefaultPacking, mapsize, 1, 1,
163 format, type, clientMemSize, ptr);
166 _mesa_reference_buffer_object(ctx,
167 &ctx->DefaultPacking.BufferObj,
168 ctx->Shared->NullBufferObj);
171 if (_mesa_is_bufferobj(pack->BufferObj)) {
172 _mesa_error(ctx, GL_INVALID_OPERATION,
173 "gl[Get]PixelMap*v(out of bounds PBO access)");
175 _mesa_error(ctx, GL_INVALID_OPERATION,
176 "glGetnPixelMap*vARB(out of bounds access:"
177 " bufSize (%d) is too small)", clientMemSize);
184 static void GLAPIENTRY
185 _mesa_PixelMapfv( GLenum map, GLsizei mapsize, const GLfloat *values )
187 GET_CURRENT_CONTEXT(ctx);
188 ASSERT_OUTSIDE_BEGIN_END(ctx);
190 /* XXX someday, test against ctx->Const.MaxPixelMapTableSize */
191 if (mapsize < 1 || mapsize > MAX_PIXEL_MAP_TABLE) {
192 _mesa_error( ctx, GL_INVALID_VALUE, "glPixelMapfv(mapsize)" );
196 if (map >= GL_PIXEL_MAP_S_TO_S && map <= GL_PIXEL_MAP_I_TO_A) {
197 /* test that mapsize is a power of two */
198 if (!_mesa_is_pow_two(mapsize)) {
199 _mesa_error( ctx, GL_INVALID_VALUE, "glPixelMapfv(mapsize)" );
204 FLUSH_VERTICES(ctx, _NEW_PIXEL);
206 if (!validate_pbo_access(ctx, &ctx->Unpack, mapsize, GL_INTENSITY,
207 GL_FLOAT, INT_MAX, values)) {
211 values = (const GLfloat *) _mesa_map_pbo_source(ctx, &ctx->Unpack, values);
213 if (_mesa_is_bufferobj(ctx->Unpack.BufferObj)) {
214 _mesa_error(ctx, GL_INVALID_OPERATION,
215 "glPixelMapfv(PBO is mapped)");
220 store_pixelmap(ctx, map, mapsize, values);
222 _mesa_unmap_pbo_source(ctx, &ctx->Unpack);
226 static void GLAPIENTRY
227 _mesa_PixelMapuiv(GLenum map, GLsizei mapsize, const GLuint *values )
229 GLfloat fvalues[MAX_PIXEL_MAP_TABLE];
230 GET_CURRENT_CONTEXT(ctx);
231 ASSERT_OUTSIDE_BEGIN_END(ctx);
233 if (mapsize < 1 || mapsize > MAX_PIXEL_MAP_TABLE) {
234 _mesa_error( ctx, GL_INVALID_VALUE, "glPixelMapuiv(mapsize)" );
238 if (map >= GL_PIXEL_MAP_S_TO_S && map <= GL_PIXEL_MAP_I_TO_A) {
239 /* test that mapsize is a power of two */
240 if (!_mesa_is_pow_two(mapsize)) {
241 _mesa_error( ctx, GL_INVALID_VALUE, "glPixelMapuiv(mapsize)" );
246 FLUSH_VERTICES(ctx, _NEW_PIXEL);
248 if (!validate_pbo_access(ctx, &ctx->Unpack, mapsize, GL_INTENSITY,
249 GL_UNSIGNED_INT, INT_MAX, values)) {
253 values = (const GLuint *) _mesa_map_pbo_source(ctx, &ctx->Unpack, values);
255 if (_mesa_is_bufferobj(ctx->Unpack.BufferObj)) {
256 _mesa_error(ctx, GL_INVALID_OPERATION,
257 "glPixelMapuiv(PBO is mapped)");
262 /* convert to floats */
263 if (map == GL_PIXEL_MAP_I_TO_I || map == GL_PIXEL_MAP_S_TO_S) {
265 for (i = 0; i < mapsize; i++) {
266 fvalues[i] = (GLfloat) values[i];
271 for (i = 0; i < mapsize; i++) {
272 fvalues[i] = UINT_TO_FLOAT( values[i] );
276 _mesa_unmap_pbo_source(ctx, &ctx->Unpack);
278 store_pixelmap(ctx, map, mapsize, fvalues);
282 static void GLAPIENTRY
283 _mesa_PixelMapusv(GLenum map, GLsizei mapsize, const GLushort *values )
285 GLfloat fvalues[MAX_PIXEL_MAP_TABLE];
286 GET_CURRENT_CONTEXT(ctx);
287 ASSERT_OUTSIDE_BEGIN_END(ctx);
289 if (mapsize < 1 || mapsize > MAX_PIXEL_MAP_TABLE) {
290 _mesa_error( ctx, GL_INVALID_VALUE, "glPixelMapusv(mapsize)" );
294 if (map >= GL_PIXEL_MAP_S_TO_S && map <= GL_PIXEL_MAP_I_TO_A) {
295 /* test that mapsize is a power of two */
296 if (!_mesa_is_pow_two(mapsize)) {
297 _mesa_error( ctx, GL_INVALID_VALUE, "glPixelMapuiv(mapsize)" );
302 FLUSH_VERTICES(ctx, _NEW_PIXEL);
304 if (!validate_pbo_access(ctx, &ctx->Unpack, mapsize, GL_INTENSITY,
305 GL_UNSIGNED_SHORT, INT_MAX, values)) {
309 values = (const GLushort *) _mesa_map_pbo_source(ctx, &ctx->Unpack, values);
311 if (_mesa_is_bufferobj(ctx->Unpack.BufferObj)) {
312 _mesa_error(ctx, GL_INVALID_OPERATION,
313 "glPixelMapusv(PBO is mapped)");
318 /* convert to floats */
319 if (map == GL_PIXEL_MAP_I_TO_I || map == GL_PIXEL_MAP_S_TO_S) {
321 for (i = 0; i < mapsize; i++) {
322 fvalues[i] = (GLfloat) values[i];
327 for (i = 0; i < mapsize; i++) {
328 fvalues[i] = USHORT_TO_FLOAT( values[i] );
332 _mesa_unmap_pbo_source(ctx, &ctx->Unpack);
334 store_pixelmap(ctx, map, mapsize, fvalues);
338 static void GLAPIENTRY
339 _mesa_GetnPixelMapfvARB( GLenum map, GLsizei bufSize, GLfloat *values )
341 GET_CURRENT_CONTEXT(ctx);
343 const struct gl_pixelmap *pm;
345 ASSERT_OUTSIDE_BEGIN_END(ctx);
347 pm = get_pixelmap(ctx, map);
349 _mesa_error(ctx, GL_INVALID_ENUM, "glGetPixelMapfv(map)");
355 if (!validate_pbo_access(ctx, &ctx->Pack, mapsize, GL_INTENSITY,
356 GL_FLOAT, bufSize, values)) {
360 values = (GLfloat *) _mesa_map_pbo_dest(ctx, &ctx->Pack, values);
362 if (_mesa_is_bufferobj(ctx->Pack.BufferObj)) {
363 _mesa_error(ctx, GL_INVALID_OPERATION,
364 "glGetPixelMapfv(PBO is mapped)");
369 if (map == GL_PIXEL_MAP_S_TO_S) {
371 for (i = 0; i < mapsize; i++) {
372 values[i] = (GLfloat) ctx->PixelMaps.StoS.Map[i];
376 memcpy(values, pm->Map, mapsize * sizeof(GLfloat));
379 _mesa_unmap_pbo_dest(ctx, &ctx->Pack);
383 static void GLAPIENTRY
384 _mesa_GetPixelMapfv( GLenum map, GLfloat *values )
386 _mesa_GetnPixelMapfvARB(map, INT_MAX, values);
389 static void GLAPIENTRY
390 _mesa_GetnPixelMapuivARB( GLenum map, GLsizei bufSize, GLuint *values )
392 GET_CURRENT_CONTEXT(ctx);
394 const struct gl_pixelmap *pm;
396 ASSERT_OUTSIDE_BEGIN_END(ctx);
398 pm = get_pixelmap(ctx, map);
400 _mesa_error(ctx, GL_INVALID_ENUM, "glGetPixelMapuiv(map)");
406 if (!validate_pbo_access(ctx, &ctx->Pack, mapsize, GL_INTENSITY,
407 GL_UNSIGNED_INT, bufSize, values)) {
411 values = (GLuint *) _mesa_map_pbo_dest(ctx, &ctx->Pack, values);
413 if (_mesa_is_bufferobj(ctx->Pack.BufferObj)) {
414 _mesa_error(ctx, GL_INVALID_OPERATION,
415 "glGetPixelMapuiv(PBO is mapped)");
420 if (map == GL_PIXEL_MAP_S_TO_S) {
422 memcpy(values, ctx->PixelMaps.StoS.Map, mapsize * sizeof(GLint));
425 for (i = 0; i < mapsize; i++) {
426 values[i] = FLOAT_TO_UINT( pm->Map[i] );
430 _mesa_unmap_pbo_dest(ctx, &ctx->Pack);
434 static void GLAPIENTRY
435 _mesa_GetPixelMapuiv( GLenum map, GLuint *values )
437 _mesa_GetnPixelMapuivARB(map, INT_MAX, values);
440 static void GLAPIENTRY
441 _mesa_GetnPixelMapusvARB( GLenum map, GLsizei bufSize, GLushort *values )
443 GET_CURRENT_CONTEXT(ctx);
445 const struct gl_pixelmap *pm;
447 ASSERT_OUTSIDE_BEGIN_END(ctx);
449 pm = get_pixelmap(ctx, map);
451 _mesa_error(ctx, GL_INVALID_ENUM, "glGetPixelMapusv(map)");
457 if (!validate_pbo_access(ctx, &ctx->Pack, mapsize, GL_INTENSITY,
458 GL_UNSIGNED_SHORT, bufSize, values)) {
462 values = (GLushort *) _mesa_map_pbo_dest(ctx, &ctx->Pack, values);
464 if (_mesa_is_bufferobj(ctx->Pack.BufferObj)) {
465 _mesa_error(ctx, GL_INVALID_OPERATION,
466 "glGetPixelMapusv(PBO is mapped)");
473 case GL_PIXEL_MAP_I_TO_I:
474 for (i = 0; i < mapsize; i++) {
475 values[i] = (GLushort) CLAMP(ctx->PixelMaps.ItoI.Map[i], 0.0, 65535.);
478 case GL_PIXEL_MAP_S_TO_S:
479 for (i = 0; i < mapsize; i++) {
480 values[i] = (GLushort) CLAMP(ctx->PixelMaps.StoS.Map[i], 0.0, 65535.);
484 for (i = 0; i < mapsize; i++) {
485 CLAMPED_FLOAT_TO_USHORT(values[i], pm->Map[i] );
489 _mesa_unmap_pbo_dest(ctx, &ctx->Pack);
493 static void GLAPIENTRY
494 _mesa_GetPixelMapusv( GLenum map, GLushort *values )
496 _mesa_GetnPixelMapusvARB(map, INT_MAX, values);
500 /**********************************************************************/
501 /***** glPixelTransfer *****/
502 /**********************************************************************/
506 * Implements glPixelTransfer[fi] whether called immediately or from a
510 _mesa_PixelTransferf( GLenum pname, GLfloat param )
512 GET_CURRENT_CONTEXT(ctx);
513 ASSERT_OUTSIDE_BEGIN_END(ctx);
517 if (ctx->Pixel.MapColorFlag == (param ? GL_TRUE : GL_FALSE))
519 FLUSH_VERTICES(ctx, _NEW_PIXEL);
520 ctx->Pixel.MapColorFlag = param ? GL_TRUE : GL_FALSE;
523 if (ctx->Pixel.MapStencilFlag == (param ? GL_TRUE : GL_FALSE))
525 FLUSH_VERTICES(ctx, _NEW_PIXEL);
526 ctx->Pixel.MapStencilFlag = param ? GL_TRUE : GL_FALSE;
529 if (ctx->Pixel.IndexShift == (GLint) param)
531 FLUSH_VERTICES(ctx, _NEW_PIXEL);
532 ctx->Pixel.IndexShift = (GLint) param;
534 case GL_INDEX_OFFSET:
535 if (ctx->Pixel.IndexOffset == (GLint) param)
537 FLUSH_VERTICES(ctx, _NEW_PIXEL);
538 ctx->Pixel.IndexOffset = (GLint) param;
541 if (ctx->Pixel.RedScale == param)
543 FLUSH_VERTICES(ctx, _NEW_PIXEL);
544 ctx->Pixel.RedScale = param;
547 if (ctx->Pixel.RedBias == param)
549 FLUSH_VERTICES(ctx, _NEW_PIXEL);
550 ctx->Pixel.RedBias = param;
553 if (ctx->Pixel.GreenScale == param)
555 FLUSH_VERTICES(ctx, _NEW_PIXEL);
556 ctx->Pixel.GreenScale = param;
559 if (ctx->Pixel.GreenBias == param)
561 FLUSH_VERTICES(ctx, _NEW_PIXEL);
562 ctx->Pixel.GreenBias = param;
565 if (ctx->Pixel.BlueScale == param)
567 FLUSH_VERTICES(ctx, _NEW_PIXEL);
568 ctx->Pixel.BlueScale = param;
571 if (ctx->Pixel.BlueBias == param)
573 FLUSH_VERTICES(ctx, _NEW_PIXEL);
574 ctx->Pixel.BlueBias = param;
577 if (ctx->Pixel.AlphaScale == param)
579 FLUSH_VERTICES(ctx, _NEW_PIXEL);
580 ctx->Pixel.AlphaScale = param;
583 if (ctx->Pixel.AlphaBias == param)
585 FLUSH_VERTICES(ctx, _NEW_PIXEL);
586 ctx->Pixel.AlphaBias = param;
589 if (ctx->Pixel.DepthScale == param)
591 FLUSH_VERTICES(ctx, _NEW_PIXEL);
592 ctx->Pixel.DepthScale = param;
595 if (ctx->Pixel.DepthBias == param)
597 FLUSH_VERTICES(ctx, _NEW_PIXEL);
598 ctx->Pixel.DepthBias = param;
601 _mesa_error( ctx, GL_INVALID_ENUM, "glPixelTransfer(pname)" );
607 static void GLAPIENTRY
608 _mesa_PixelTransferi( GLenum pname, GLint param )
610 _mesa_PixelTransferf( pname, (GLfloat) param );
615 /**********************************************************************/
616 /***** State Management *****/
617 /**********************************************************************/
620 * Return a bitmask of IMAGE_*_BIT flags which to indicate which
621 * pixel transfer operations are enabled.
624 update_image_transfer_state(struct gl_context *ctx)
628 if (ctx->Pixel.RedScale != 1.0F || ctx->Pixel.RedBias != 0.0F ||
629 ctx->Pixel.GreenScale != 1.0F || ctx->Pixel.GreenBias != 0.0F ||
630 ctx->Pixel.BlueScale != 1.0F || ctx->Pixel.BlueBias != 0.0F ||
631 ctx->Pixel.AlphaScale != 1.0F || ctx->Pixel.AlphaBias != 0.0F)
632 mask |= IMAGE_SCALE_BIAS_BIT;
634 if (ctx->Pixel.IndexShift || ctx->Pixel.IndexOffset)
635 mask |= IMAGE_SHIFT_OFFSET_BIT;
637 if (ctx->Pixel.MapColorFlag)
638 mask |= IMAGE_MAP_COLOR_BIT;
640 ctx->_ImageTransferState = mask;
645 * Update mesa pixel transfer derived state.
647 void _mesa_update_pixel( struct gl_context *ctx, GLuint new_state )
649 if (new_state & _NEW_PIXEL)
650 update_image_transfer_state(ctx);
655 _mesa_init_pixel_dispatch(struct _glapi_table *disp)
657 SET_GetPixelMapfv(disp, _mesa_GetPixelMapfv);
658 SET_GetPixelMapuiv(disp, _mesa_GetPixelMapuiv);
659 SET_GetPixelMapusv(disp, _mesa_GetPixelMapusv);
660 SET_PixelMapfv(disp, _mesa_PixelMapfv);
661 SET_PixelMapuiv(disp, _mesa_PixelMapuiv);
662 SET_PixelMapusv(disp, _mesa_PixelMapusv);
663 SET_PixelTransferf(disp, _mesa_PixelTransferf);
664 SET_PixelTransferi(disp, _mesa_PixelTransferi);
665 SET_PixelZoom(disp, _mesa_PixelZoom);
667 /* GL_ARB_robustness */
668 SET_GetnPixelMapfvARB(disp, _mesa_GetnPixelMapfvARB);
669 SET_GetnPixelMapuivARB(disp, _mesa_GetnPixelMapuivARB);
670 SET_GetnPixelMapusvARB(disp, _mesa_GetnPixelMapusvARB);
674 #endif /* FEATURE_pixel_transfer */
677 /**********************************************************************/
678 /***** Initialization *****/
679 /**********************************************************************/
682 init_pixelmap(struct gl_pixelmap *map)
691 * Initialize the context's PIXEL attribute group.
694 _mesa_init_pixel( struct gl_context *ctx )
697 ctx->Pixel.RedBias = 0.0;
698 ctx->Pixel.RedScale = 1.0;
699 ctx->Pixel.GreenBias = 0.0;
700 ctx->Pixel.GreenScale = 1.0;
701 ctx->Pixel.BlueBias = 0.0;
702 ctx->Pixel.BlueScale = 1.0;
703 ctx->Pixel.AlphaBias = 0.0;
704 ctx->Pixel.AlphaScale = 1.0;
705 ctx->Pixel.DepthBias = 0.0;
706 ctx->Pixel.DepthScale = 1.0;
707 ctx->Pixel.IndexOffset = 0;
708 ctx->Pixel.IndexShift = 0;
709 ctx->Pixel.ZoomX = 1.0;
710 ctx->Pixel.ZoomY = 1.0;
711 ctx->Pixel.MapColorFlag = GL_FALSE;
712 ctx->Pixel.MapStencilFlag = GL_FALSE;
713 init_pixelmap(&ctx->PixelMaps.StoS);
714 init_pixelmap(&ctx->PixelMaps.ItoI);
715 init_pixelmap(&ctx->PixelMaps.ItoR);
716 init_pixelmap(&ctx->PixelMaps.ItoG);
717 init_pixelmap(&ctx->PixelMaps.ItoB);
718 init_pixelmap(&ctx->PixelMaps.ItoA);
719 init_pixelmap(&ctx->PixelMaps.RtoR);
720 init_pixelmap(&ctx->PixelMaps.GtoG);
721 init_pixelmap(&ctx->PixelMaps.BtoB);
722 init_pixelmap(&ctx->PixelMaps.AtoA);
724 if (ctx->Visual.doubleBufferMode) {
725 ctx->Pixel.ReadBuffer = GL_BACK;
728 ctx->Pixel.ReadBuffer = GL_FRONT;
732 ctx->_ImageTransferState = 0;