2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2005 Brian Paul All Rights Reserved.
6 * Copyright (C) 2009 VMware, Inc. All Rights Reserved.
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
15 * The above copyright notice and this permission notice shall be included
16 * in all copies or substantial portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
22 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35 #include "pixelstore.h"
43 const char *_mesa_prim_name
[GL_POLYGON
+4] = {
55 "inside unknown primitive",
61 tex_target_name(GLenum tgt
)
67 { GL_TEXTURE_1D
, "GL_TEXTURE_1D" },
68 { GL_TEXTURE_2D
, "GL_TEXTURE_2D" },
69 { GL_TEXTURE_3D
, "GL_TEXTURE_3D" },
70 { GL_TEXTURE_CUBE_MAP
, "GL_TEXTURE_CUBE_MAP" },
71 { GL_TEXTURE_RECTANGLE
, "GL_TEXTURE_RECTANGLE" },
72 { GL_TEXTURE_1D_ARRAY_EXT
, "GL_TEXTURE_1D_ARRAY" },
73 { GL_TEXTURE_2D_ARRAY_EXT
, "GL_TEXTURE_2D_ARRAY" }
76 for (i
= 0; i
< Elements(tex_targets
); i
++) {
77 if (tex_targets
[i
].target
== tgt
)
78 return tex_targets
[i
].name
;
80 return "UNKNOWN TEX TARGET";
85 _mesa_print_state( const char *msg
, GLuint state
)
88 "%s: (0x%x) %s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n",
91 (state
& _NEW_MODELVIEW
) ? "ctx->ModelView, " : "",
92 (state
& _NEW_PROJECTION
) ? "ctx->Projection, " : "",
93 (state
& _NEW_TEXTURE_MATRIX
) ? "ctx->TextureMatrix, " : "",
94 (state
& _NEW_ACCUM
) ? "ctx->Accum, " : "",
95 (state
& _NEW_COLOR
) ? "ctx->Color, " : "",
96 (state
& _NEW_DEPTH
) ? "ctx->Depth, " : "",
97 (state
& _NEW_EVAL
) ? "ctx->Eval/EvalMap, " : "",
98 (state
& _NEW_FOG
) ? "ctx->Fog, " : "",
99 (state
& _NEW_HINT
) ? "ctx->Hint, " : "",
100 (state
& _NEW_LIGHT
) ? "ctx->Light, " : "",
101 (state
& _NEW_LINE
) ? "ctx->Line, " : "",
102 (state
& _NEW_PIXEL
) ? "ctx->Pixel, " : "",
103 (state
& _NEW_POINT
) ? "ctx->Point, " : "",
104 (state
& _NEW_POLYGON
) ? "ctx->Polygon, " : "",
105 (state
& _NEW_POLYGONSTIPPLE
) ? "ctx->PolygonStipple, " : "",
106 (state
& _NEW_SCISSOR
) ? "ctx->Scissor, " : "",
107 (state
& _NEW_STENCIL
) ? "ctx->Stencil, " : "",
108 (state
& _NEW_TEXTURE
) ? "ctx->Texture, " : "",
109 (state
& _NEW_TRANSFORM
) ? "ctx->Transform, " : "",
110 (state
& _NEW_VIEWPORT
) ? "ctx->Viewport, " : "",
111 (state
& _NEW_PACKUNPACK
) ? "ctx->Pack/Unpack, " : "",
112 (state
& _NEW_ARRAY
) ? "ctx->Array, " : "",
113 (state
& _NEW_RENDERMODE
) ? "ctx->RenderMode, " : "",
114 (state
& _NEW_BUFFERS
) ? "ctx->Visual, ctx->DrawBuffer,, " : "");
120 _mesa_print_tri_caps( const char *name
, GLuint flags
)
123 "%s: (0x%x) %s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n",
126 (flags
& DD_FLATSHADE
) ? "flat-shade, " : "",
127 (flags
& DD_SEPARATE_SPECULAR
) ? "separate-specular, " : "",
128 (flags
& DD_TRI_LIGHT_TWOSIDE
) ? "tri-light-twoside, " : "",
129 (flags
& DD_TRI_TWOSTENCIL
) ? "tri-twostencil, " : "",
130 (flags
& DD_TRI_UNFILLED
) ? "tri-unfilled, " : "",
131 (flags
& DD_TRI_STIPPLE
) ? "tri-stipple, " : "",
132 (flags
& DD_TRI_OFFSET
) ? "tri-offset, " : "",
133 (flags
& DD_TRI_SMOOTH
) ? "tri-smooth, " : "",
134 (flags
& DD_LINE_SMOOTH
) ? "line-smooth, " : "",
135 (flags
& DD_LINE_STIPPLE
) ? "line-stipple, " : "",
136 (flags
& DD_LINE_WIDTH
) ? "line-wide, " : "",
137 (flags
& DD_POINT_SMOOTH
) ? "point-smooth, " : "",
138 (flags
& DD_POINT_SIZE
) ? "point-size, " : "",
139 (flags
& DD_POINT_ATTEN
) ? "point-atten, " : "",
140 (flags
& DD_TRI_CULL_FRONT_BACK
) ? "cull-all, " : ""
146 * Print information about this Mesa version and build options.
148 void _mesa_print_info( void )
150 _mesa_debug(NULL
, "Mesa GL_VERSION = %s\n",
151 (char *) _mesa_GetString(GL_VERSION
));
152 _mesa_debug(NULL
, "Mesa GL_RENDERER = %s\n",
153 (char *) _mesa_GetString(GL_RENDERER
));
154 _mesa_debug(NULL
, "Mesa GL_VENDOR = %s\n",
155 (char *) _mesa_GetString(GL_VENDOR
));
156 _mesa_debug(NULL
, "Mesa GL_EXTENSIONS = %s\n",
157 (char *) _mesa_GetString(GL_EXTENSIONS
));
159 _mesa_debug(NULL
, "Mesa thread-safe: YES\n");
161 _mesa_debug(NULL
, "Mesa thread-safe: NO\n");
163 #if defined(USE_X86_ASM)
164 _mesa_debug(NULL
, "Mesa x86-optimized: YES\n");
166 _mesa_debug(NULL
, "Mesa x86-optimized: NO\n");
168 #if defined(USE_SPARC_ASM)
169 _mesa_debug(NULL
, "Mesa sparc-optimized: YES\n");
171 _mesa_debug(NULL
, "Mesa sparc-optimized: NO\n");
177 * Set the debugging flags.
179 * \param debug debug string
181 * If compiled with debugging support then search for keywords in \p debug and
182 * enables the verbose debug output of the respective feature.
184 static void add_debug_flags( const char *debug
)
187 struct debug_option
{
191 static const struct debug_option debug_opt
[] = {
192 { "varray", VERBOSE_VARRAY
},
193 { "tex", VERBOSE_TEXTURE
},
194 { "mat", VERBOSE_MATERIAL
},
195 { "pipe", VERBOSE_PIPELINE
},
196 { "driver", VERBOSE_DRIVER
},
197 { "state", VERBOSE_STATE
},
198 { "api", VERBOSE_API
},
199 { "list", VERBOSE_DISPLAY_LIST
},
200 { "lighting", VERBOSE_LIGHTING
},
201 { "disassem", VERBOSE_DISASSEM
},
202 { "draw", VERBOSE_DRAW
},
203 { "swap", VERBOSE_SWAPBUFFERS
}
208 for (i
= 0; i
< Elements(debug_opt
); i
++) {
209 if (strstr(debug
, debug_opt
[i
].name
))
210 MESA_VERBOSE
|= debug_opt
[i
].flag
;
215 if (strstr(debug
, "flush"))
216 MESA_DEBUG_FLAGS
|= DEBUG_ALWAYS_FLUSH
;
218 #if defined(_FPU_GETCW) && defined(_FPU_SETCW)
219 if (strstr(debug
, "fpexceptions")) {
220 /* raise FP exceptions */
223 mask
&= ~(_FPU_MASK_IM
| _FPU_MASK_DM
| _FPU_MASK_ZM
224 | _FPU_MASK_OM
| _FPU_MASK_UM
);
236 _mesa_init_debug( struct gl_context
*ctx
)
241 ctx
->NoDither
= _mesa_getenv("MESA_NO_DITHER") ? GL_TRUE
: GL_FALSE
;
243 if (_mesa_getenv("MESA_DEBUG")) {
244 _mesa_debug(ctx
, "MESA_NO_DITHER set - dithering disabled\n");
246 ctx
->Color
.DitherFlag
= GL_FALSE
;
249 c
= _mesa_getenv("MESA_DEBUG");
253 c
= _mesa_getenv("MESA_VERBOSE");
263 write_ppm(const char *filename
, const GLubyte
*buffer
, int width
, int height
,
264 int comps
, int rcomp
, int gcomp
, int bcomp
, GLboolean invert
)
266 FILE *f
= fopen( filename
, "w" );
269 const GLubyte
*ptr
= buffer
;
271 fprintf(f
,"# ppm-file created by osdemo.c\n");
272 fprintf(f
,"%i %i\n", width
,height
);
275 f
= fopen( filename
, "ab" ); /* reopen in binary append mode */
276 for (y
=0; y
< height
; y
++) {
277 for (x
= 0; x
< width
; x
++) {
278 int yy
= invert
? (height
- 1 - y
) : y
;
279 int i
= (yy
* width
+ x
) * comps
;
280 fputc(ptr
[i
+rcomp
], f
); /* write red */
281 fputc(ptr
[i
+gcomp
], f
); /* write green */
282 fputc(ptr
[i
+bcomp
], f
); /* write blue */
291 * Write a texture image to a ppm file.
292 * \param face cube face in [0,5]
293 * \param level mipmap level
296 write_texture_image(struct gl_texture_object
*texObj
,
297 GLuint face
, GLuint level
)
299 struct gl_texture_image
*img
= texObj
->Image
[face
][level
];
301 GET_CURRENT_CONTEXT(ctx
);
302 struct gl_pixelstore_attrib store
;
306 buffer
= (GLubyte
*) malloc(img
->Width
* img
->Height
309 store
= ctx
->Pack
; /* save */
310 ctx
->Pack
= ctx
->DefaultPacking
;
312 ctx
->Driver
.GetTexImage(ctx
, texObj
->Target
, level
,
313 GL_RGBA
, GL_UNSIGNED_BYTE
,
314 buffer
, texObj
, img
);
317 _mesa_snprintf(s
, sizeof(s
), "/tmp/tex%u.l%u.f%u.ppm", texObj
->Name
, level
, face
);
319 printf(" Writing image level %u to %s\n", level
, s
);
320 write_ppm(s
, buffer
, img
->Width
, img
->Height
, 4, 0, 1, 2, GL_FALSE
);
322 ctx
->Pack
= store
; /* restore */
330 * Write renderbuffer image to a ppm file.
333 write_renderbuffer_image(const struct gl_renderbuffer
*rb
)
335 GET_CURRENT_CONTEXT(ctx
);
340 if (rb
->_BaseFormat
== GL_RGB
||
341 rb
->_BaseFormat
== GL_RGBA
) {
343 type
= GL_UNSIGNED_BYTE
;
345 else if (rb
->_BaseFormat
== GL_DEPTH_STENCIL
) {
346 format
= GL_DEPTH_STENCIL
;
347 type
= GL_UNSIGNED_INT_24_8
;
353 buffer
= (GLubyte
*) malloc(rb
->Width
* rb
->Height
* 4);
355 ctx
->Driver
.ReadPixels(ctx
, 0, 0, rb
->Width
, rb
->Height
,
356 format
, type
, &ctx
->DefaultPacking
, buffer
);
359 _mesa_snprintf(s
, sizeof(s
), "/tmp/renderbuffer%u.ppm", rb
->Name
);
361 printf(" Writing renderbuffer image to %s\n", s
);
362 write_ppm(s
, buffer
, rb
->Width
, rb
->Height
, 4, 0, 1, 2, GL_TRUE
);
368 /** How many texture images (mipmap levels, faces) to write to files */
373 static GLuint WriteImages
;
377 dump_texture(struct gl_texture_object
*texObj
, GLuint writeImages
)
379 const GLuint numFaces
= texObj
->Target
== GL_TEXTURE_CUBE_MAP
? 6 : 1;
380 GLboolean written
= GL_FALSE
;
383 printf("Texture %u\n", texObj
->Name
);
384 printf(" Target %s\n", tex_target_name(texObj
->Target
));
385 for (i
= 0; i
< MAX_TEXTURE_LEVELS
; i
++) {
386 for (j
= 0; j
< numFaces
; j
++) {
387 struct gl_texture_image
*texImg
= texObj
->Image
[j
][i
];
389 printf(" Face %u level %u: %d x %d x %d, format %s at %p\n",
391 texImg
->Width
, texImg
->Height
, texImg
->Depth
,
392 _mesa_get_format_name(texImg
->TexFormat
),
394 if (writeImages
== WRITE_ALL
||
395 (writeImages
== WRITE_ONE
&& !written
)) {
396 write_texture_image(texObj
, j
, i
);
406 * Dump a single texture.
409 _mesa_dump_texture(GLuint texture
, GLuint writeImages
)
411 GET_CURRENT_CONTEXT(ctx
);
412 struct gl_texture_object
*texObj
= _mesa_lookup_texture(ctx
, texture
);
414 dump_texture(texObj
, writeImages
);
420 dump_texture_cb(GLuint id
, void *data
, void *userData
)
422 struct gl_texture_object
*texObj
= (struct gl_texture_object
*) data
;
424 dump_texture(texObj
, WriteImages
);
429 * Print basic info about all texture objext to stdout.
430 * If dumpImages is true, write PPM of level[0] image to a file.
433 _mesa_dump_textures(GLuint writeImages
)
435 GET_CURRENT_CONTEXT(ctx
);
436 WriteImages
= writeImages
;
437 _mesa_HashWalk(ctx
->Shared
->TexObjects
, dump_texture_cb
, ctx
);
442 dump_renderbuffer(const struct gl_renderbuffer
*rb
, GLboolean writeImage
)
444 printf("Renderbuffer %u: %u x %u IntFormat = %s\n",
445 rb
->Name
, rb
->Width
, rb
->Height
,
446 _mesa_lookup_enum_by_nr(rb
->InternalFormat
));
448 write_renderbuffer_image(rb
);
454 dump_renderbuffer_cb(GLuint id
, void *data
, void *userData
)
456 const struct gl_renderbuffer
*rb
= (const struct gl_renderbuffer
*) data
;
458 dump_renderbuffer(rb
, WriteImages
);
463 * Print basic info about all renderbuffers to stdout.
464 * If dumpImages is true, write PPM of level[0] image to a file.
467 _mesa_dump_renderbuffers(GLboolean writeImages
)
469 GET_CURRENT_CONTEXT(ctx
);
470 WriteImages
= writeImages
;
471 _mesa_HashWalk(ctx
->Shared
->RenderBuffers
, dump_renderbuffer_cb
, ctx
);
477 _mesa_dump_color_buffer(const char *filename
)
479 GET_CURRENT_CONTEXT(ctx
);
480 const GLuint w
= ctx
->DrawBuffer
->Width
;
481 const GLuint h
= ctx
->DrawBuffer
->Height
;
484 buf
= (GLubyte
*) malloc(w
* h
* 4);
486 _mesa_PushClientAttrib(GL_CLIENT_PIXEL_STORE_BIT
);
487 _mesa_PixelStorei(GL_PACK_ALIGNMENT
, 1);
488 _mesa_PixelStorei(GL_PACK_INVERT_MESA
, GL_TRUE
);
490 _mesa_ReadPixels(0, 0, w
, h
, GL_RGBA
, GL_UNSIGNED_BYTE
, buf
);
492 printf("ReadBuffer %p 0x%x DrawBuffer %p 0x%x\n",
493 (void *) ctx
->ReadBuffer
->_ColorReadBuffer
,
494 ctx
->ReadBuffer
->ColorReadBuffer
,
495 (void *) ctx
->DrawBuffer
->_ColorDrawBuffers
[0],
496 ctx
->DrawBuffer
->ColorDrawBuffer
[0]);
497 printf("Writing %d x %d color buffer to %s\n", w
, h
, filename
);
498 write_ppm(filename
, buf
, w
, h
, 4, 0, 1, 2, GL_TRUE
);
500 _mesa_PopClientAttrib();
507 _mesa_dump_depth_buffer(const char *filename
)
509 GET_CURRENT_CONTEXT(ctx
);
510 const GLuint w
= ctx
->DrawBuffer
->Width
;
511 const GLuint h
= ctx
->DrawBuffer
->Height
;
516 buf
= (GLuint
*) malloc(w
* h
* 4); /* 4 bpp */
517 buf2
= (GLubyte
*) malloc(w
* h
* 3); /* 3 bpp */
519 _mesa_PushClientAttrib(GL_CLIENT_PIXEL_STORE_BIT
);
520 _mesa_PixelStorei(GL_PACK_ALIGNMENT
, 1);
521 _mesa_PixelStorei(GL_PACK_INVERT_MESA
, GL_TRUE
);
523 _mesa_ReadPixels(0, 0, w
, h
, GL_DEPTH_COMPONENT
, GL_UNSIGNED_INT
, buf
);
525 /* spread 24 bits of Z across R, G, B */
526 for (i
= 0; i
< w
* h
; i
++) {
527 buf2
[i
*3+0] = (buf
[i
] >> 24) & 0xff;
528 buf2
[i
*3+1] = (buf
[i
] >> 16) & 0xff;
529 buf2
[i
*3+2] = (buf
[i
] >> 8) & 0xff;
532 printf("Writing %d x %d depth buffer to %s\n", w
, h
, filename
);
533 write_ppm(filename
, buf2
, w
, h
, 3, 0, 1, 2, GL_TRUE
);
535 _mesa_PopClientAttrib();
543 _mesa_dump_stencil_buffer(const char *filename
)
545 GET_CURRENT_CONTEXT(ctx
);
546 const GLuint w
= ctx
->DrawBuffer
->Width
;
547 const GLuint h
= ctx
->DrawBuffer
->Height
;
552 buf
= (GLubyte
*) malloc(w
* h
); /* 1 bpp */
553 buf2
= (GLubyte
*) malloc(w
* h
* 3); /* 3 bpp */
555 _mesa_PushClientAttrib(GL_CLIENT_PIXEL_STORE_BIT
);
556 _mesa_PixelStorei(GL_PACK_ALIGNMENT
, 1);
557 _mesa_PixelStorei(GL_PACK_INVERT_MESA
, GL_TRUE
);
559 _mesa_ReadPixels(0, 0, w
, h
, GL_STENCIL_INDEX
, GL_UNSIGNED_BYTE
, buf
);
561 for (i
= 0; i
< w
* h
; i
++) {
562 buf2
[i
*3+0] = buf
[i
];
563 buf2
[i
*3+1] = (buf
[i
] & 127) * 2;
564 buf2
[i
*3+2] = (buf
[i
] - 128) * 2;
567 printf("Writing %d x %d stencil buffer to %s\n", w
, h
, filename
);
568 write_ppm(filename
, buf2
, w
, h
, 3, 0, 1, 2, GL_TRUE
);
570 _mesa_PopClientAttrib();
578 * Quick and dirty function to "print" a texture to stdout.
581 _mesa_print_texture(struct gl_context
*ctx
, const struct gl_texture_image
*img
)
583 #if CHAN_TYPE != GL_UNSIGNED_BYTE
584 _mesa_problem(NULL
, "PrintTexture not supported");
587 const GLubyte
*data
= (const GLubyte
*) img
->Data
;
590 printf("No texture data\n");
594 /* XXX add more formats or make into a new format utility function */
595 switch (img
->TexFormat
) {
599 case MESA_FORMAT_CI8
:
602 case MESA_FORMAT_AL88
:
603 case MESA_FORMAT_AL88_REV
:
606 case MESA_FORMAT_RGB888
:
607 case MESA_FORMAT_BGR888
:
610 case MESA_FORMAT_RGBA8888
:
611 case MESA_FORMAT_ARGB8888
:
615 _mesa_problem(NULL
, "error in PrintTexture\n");
619 for (i
= 0; i
< img
->Height
; i
++) {
620 for (j
= 0; j
< img
->Width
; j
++) {
622 printf("%02x ", data
[0]);
624 printf("%02x%02x ", data
[0], data
[1]);
626 printf("%02x%02x%02x ", data
[0], data
[1], data
[2]);
628 printf("%02x%02x%02x%02x ", data
[0], data
[1], data
[2], data
[3]);
629 data
+= (img
->RowStride
- img
->Width
) * c
;
631 /* XXX use img->ImageStride here */