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"
41 tex_target_name(GLenum tgt
)
47 { GL_TEXTURE_1D
, "GL_TEXTURE_1D" },
48 { GL_TEXTURE_2D
, "GL_TEXTURE_2D" },
49 { GL_TEXTURE_3D
, "GL_TEXTURE_3D" },
50 { GL_TEXTURE_CUBE_MAP
, "GL_TEXTURE_CUBE_MAP" }
53 for (i
= 0; i
< Elements(tex_targets
); i
++) {
54 if (tex_targets
[i
].target
== tgt
)
55 return tex_targets
[i
].name
;
57 return "UNKNOWN TEX TARGET";
62 _mesa_print_state( const char *msg
, GLuint state
)
65 "%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\n",
68 (state
& _NEW_MODELVIEW
) ? "ctx->ModelView, " : "",
69 (state
& _NEW_PROJECTION
) ? "ctx->Projection, " : "",
70 (state
& _NEW_TEXTURE_MATRIX
) ? "ctx->TextureMatrix, " : "",
71 (state
& _NEW_COLOR
) ? "ctx->Color, " : "",
72 (state
& _NEW_DEPTH
) ? "ctx->Depth, " : "",
73 (state
& _NEW_EVAL
) ? "ctx->Eval/EvalMap, " : "",
74 (state
& _NEW_FOG
) ? "ctx->Fog, " : "",
75 (state
& _NEW_HINT
) ? "ctx->Hint, " : "",
76 (state
& _NEW_LIGHT
) ? "ctx->Light, " : "",
77 (state
& _NEW_LINE
) ? "ctx->Line, " : "",
78 (state
& _NEW_PIXEL
) ? "ctx->Pixel, " : "",
79 (state
& _NEW_POINT
) ? "ctx->Point, " : "",
80 (state
& _NEW_POLYGON
) ? "ctx->Polygon, " : "",
81 (state
& _NEW_POLYGONSTIPPLE
) ? "ctx->PolygonStipple, " : "",
82 (state
& _NEW_SCISSOR
) ? "ctx->Scissor, " : "",
83 (state
& _NEW_STENCIL
) ? "ctx->Stencil, " : "",
84 (state
& _NEW_TEXTURE
) ? "ctx->Texture, " : "",
85 (state
& _NEW_TRANSFORM
) ? "ctx->Transform, " : "",
86 (state
& _NEW_VIEWPORT
) ? "ctx->Viewport, " : "",
87 (state
& _NEW_PACKUNPACK
) ? "ctx->Pack/Unpack, " : "",
88 (state
& _NEW_ARRAY
) ? "ctx->Array, " : "",
89 (state
& _NEW_RENDERMODE
) ? "ctx->RenderMode, " : "",
90 (state
& _NEW_BUFFERS
) ? "ctx->Visual, ctx->DrawBuffer,, " : "");
96 _mesa_print_tri_caps( const char *name
, GLuint flags
)
99 "%s: (0x%x) %s%s%s%s%s%s%s%s%s%s%s%s%s\n",
102 (flags
& DD_FLATSHADE
) ? "flat-shade, " : "",
103 (flags
& DD_SEPARATE_SPECULAR
) ? "separate-specular, " : "",
104 (flags
& DD_TRI_LIGHT_TWOSIDE
) ? "tri-light-twoside, " : "",
105 (flags
& DD_TRI_TWOSTENCIL
) ? "tri-twostencil, " : "",
106 (flags
& DD_TRI_UNFILLED
) ? "tri-unfilled, " : "",
107 (flags
& DD_TRI_STIPPLE
) ? "tri-stipple, " : "",
108 (flags
& DD_TRI_OFFSET
) ? "tri-offset, " : "",
109 (flags
& DD_TRI_SMOOTH
) ? "tri-smooth, " : "",
110 (flags
& DD_LINE_SMOOTH
) ? "line-smooth, " : "",
111 (flags
& DD_LINE_STIPPLE
) ? "line-stipple, " : "",
112 (flags
& DD_POINT_SMOOTH
) ? "point-smooth, " : "",
113 (flags
& DD_POINT_ATTEN
) ? "point-atten, " : "",
114 (flags
& DD_TRI_CULL_FRONT_BACK
) ? "cull-all, " : ""
120 * Print information about this Mesa version and build options.
122 void _mesa_print_info( void )
124 _mesa_debug(NULL
, "Mesa GL_VERSION = %s\n",
125 (char *) _mesa_GetString(GL_VERSION
));
126 _mesa_debug(NULL
, "Mesa GL_RENDERER = %s\n",
127 (char *) _mesa_GetString(GL_RENDERER
));
128 _mesa_debug(NULL
, "Mesa GL_VENDOR = %s\n",
129 (char *) _mesa_GetString(GL_VENDOR
));
130 _mesa_debug(NULL
, "Mesa GL_EXTENSIONS = %s\n",
131 (char *) _mesa_GetString(GL_EXTENSIONS
));
133 _mesa_debug(NULL
, "Mesa thread-safe: YES\n");
135 _mesa_debug(NULL
, "Mesa thread-safe: NO\n");
137 #if defined(USE_X86_ASM)
138 _mesa_debug(NULL
, "Mesa x86-optimized: YES\n");
140 _mesa_debug(NULL
, "Mesa x86-optimized: NO\n");
142 #if defined(USE_SPARC_ASM)
143 _mesa_debug(NULL
, "Mesa sparc-optimized: YES\n");
145 _mesa_debug(NULL
, "Mesa sparc-optimized: NO\n");
151 * Set the debugging flags.
153 * \param debug debug string
155 * If compiled with debugging support then search for keywords in \p debug and
156 * enables the verbose debug output of the respective feature.
158 static void add_debug_flags( const char *debug
)
161 struct debug_option
{
165 static const struct debug_option debug_opt
[] = {
166 { "varray", VERBOSE_VARRAY
},
167 { "tex", VERBOSE_TEXTURE
},
168 { "mat", VERBOSE_MATERIAL
},
169 { "pipe", VERBOSE_PIPELINE
},
170 { "driver", VERBOSE_DRIVER
},
171 { "state", VERBOSE_STATE
},
172 { "api", VERBOSE_API
},
173 { "list", VERBOSE_DISPLAY_LIST
},
174 { "lighting", VERBOSE_LIGHTING
},
175 { "disassem", VERBOSE_DISASSEM
},
176 { "draw", VERBOSE_DRAW
},
177 { "swap", VERBOSE_SWAPBUFFERS
}
182 for (i
= 0; i
< Elements(debug_opt
); i
++) {
183 if (strstr(debug
, debug_opt
[i
].name
) || strcmp(debug
, "all") == 0)
184 MESA_VERBOSE
|= debug_opt
[i
].flag
;
189 if (strstr(debug
, "flush"))
190 MESA_DEBUG_FLAGS
|= DEBUG_ALWAYS_FLUSH
;
199 _mesa_init_debug( struct gl_context
*ctx
)
202 c
= _mesa_getenv("MESA_DEBUG");
206 c
= _mesa_getenv("MESA_VERBOSE");
216 write_ppm(const char *filename
, const GLubyte
*buffer
, int width
, int height
,
217 int comps
, int rcomp
, int gcomp
, int bcomp
, GLboolean invert
)
219 FILE *f
= fopen( filename
, "w" );
222 const GLubyte
*ptr
= buffer
;
224 fprintf(f
,"# ppm-file created by osdemo.c\n");
225 fprintf(f
,"%i %i\n", width
,height
);
228 f
= fopen( filename
, "ab" ); /* reopen in binary append mode */
229 for (y
=0; y
< height
; y
++) {
230 for (x
= 0; x
< width
; x
++) {
231 int yy
= invert
? (height
- 1 - y
) : y
;
232 int i
= (yy
* width
+ x
) * comps
;
233 fputc(ptr
[i
+rcomp
], f
); /* write red */
234 fputc(ptr
[i
+gcomp
], f
); /* write green */
235 fputc(ptr
[i
+bcomp
], f
); /* write blue */
241 fprintf(stderr
, "Unable to create %s in write_ppm()\n", filename
);
247 * Write a texture image to a ppm file.
248 * \param face cube face in [0,5]
249 * \param level mipmap level
252 write_texture_image(struct gl_texture_object
*texObj
,
253 GLuint face
, GLuint level
)
255 struct gl_texture_image
*img
= texObj
->Image
[face
][level
];
257 GET_CURRENT_CONTEXT(ctx
);
258 struct gl_pixelstore_attrib store
;
262 buffer
= (GLubyte
*) malloc(img
->Width
* img
->Height
265 store
= ctx
->Pack
; /* save */
266 ctx
->Pack
= ctx
->DefaultPacking
;
268 ctx
->Driver
.GetTexImage(ctx
, GL_RGBA
, GL_UNSIGNED_BYTE
, buffer
, img
);
271 _mesa_snprintf(s
, sizeof(s
), "/tmp/tex%u.l%u.f%u.ppm", texObj
->Name
, level
, face
);
273 printf(" Writing image level %u to %s\n", level
, s
);
274 write_ppm(s
, buffer
, img
->Width
, img
->Height
, 4, 0, 1, 2, GL_FALSE
);
276 ctx
->Pack
= store
; /* restore */
284 * Write renderbuffer image to a ppm file.
287 _mesa_write_renderbuffer_image(const struct gl_renderbuffer
*rb
)
289 GET_CURRENT_CONTEXT(ctx
);
294 if (rb
->_BaseFormat
== GL_RGB
||
295 rb
->_BaseFormat
== GL_RGBA
) {
297 type
= GL_UNSIGNED_BYTE
;
301 "Unsupported BaseFormat 0x%x in "
302 "_mesa_write_renderbuffer_image()\n",
307 buffer
= (GLubyte
*) malloc(rb
->Width
* rb
->Height
* 4);
309 ctx
->Driver
.ReadPixels(ctx
, 0, 0, rb
->Width
, rb
->Height
,
310 format
, type
, &ctx
->DefaultPacking
, buffer
);
313 _mesa_snprintf(s
, sizeof(s
), "/tmp/renderbuffer.ppm");
314 _mesa_snprintf(s
, sizeof(s
), "C:\\renderbuffer.ppm");
316 printf(" Writing renderbuffer image to %s\n", s
);
318 _mesa_debug(NULL
, " Writing renderbuffer image to %s\n", s
);
320 write_ppm(s
, buffer
, rb
->Width
, rb
->Height
, 4, 0, 1, 2, GL_TRUE
);
326 /** How many texture images (mipmap levels, faces) to write to files */
331 static GLuint WriteImages
;
335 dump_texture(struct gl_texture_object
*texObj
, GLuint writeImages
)
337 const GLuint numFaces
= texObj
->Target
== GL_TEXTURE_CUBE_MAP
? 6 : 1;
338 GLboolean written
= GL_FALSE
;
341 printf("Texture %u\n", texObj
->Name
);
342 printf(" Target %s\n", tex_target_name(texObj
->Target
));
343 for (i
= 0; i
< MAX_TEXTURE_LEVELS
; i
++) {
344 for (j
= 0; j
< numFaces
; j
++) {
345 struct gl_texture_image
*texImg
= texObj
->Image
[j
][i
];
347 printf(" Face %u level %u: %d x %d x %d, format %s\n",
349 texImg
->Width
, texImg
->Height
, texImg
->Depth
,
350 _mesa_get_format_name(texImg
->TexFormat
));
351 if (writeImages
== WRITE_ALL
||
352 (writeImages
== WRITE_ONE
&& !written
)) {
353 write_texture_image(texObj
, j
, i
);
363 * Dump a single texture.
366 _mesa_dump_texture(GLuint texture
, GLuint writeImages
)
368 GET_CURRENT_CONTEXT(ctx
);
369 struct gl_texture_object
*texObj
= _mesa_lookup_texture(ctx
, texture
);
371 dump_texture(texObj
, writeImages
);
377 dump_texture_cb(GLuint id
, void *data
, void *userData
)
379 struct gl_texture_object
*texObj
= (struct gl_texture_object
*) data
;
381 dump_texture(texObj
, WriteImages
);
386 * Print basic info about all texture objext to stdout.
387 * If dumpImages is true, write PPM of level[0] image to a file.
390 _mesa_dump_textures(GLuint writeImages
)
392 GET_CURRENT_CONTEXT(ctx
);
393 WriteImages
= writeImages
;
394 _mesa_HashWalk(ctx
->Shared
->TexObjects
, dump_texture_cb
, ctx
);
399 dump_renderbuffer(const struct gl_renderbuffer
*rb
, GLboolean writeImage
)
401 printf("Renderbuffer: %u x %u IntFormat = %s\n",
402 rb
->Width
, rb
->Height
,
403 _mesa_lookup_enum_by_nr(rb
->InternalFormat
));
405 _mesa_write_renderbuffer_image(rb
);
411 dump_renderbuffer_cb(GLuint id
, void *data
, void *userData
)
413 const struct gl_renderbuffer
*rb
= (const struct gl_renderbuffer
*) data
;
415 dump_renderbuffer(rb
, WriteImages
);
420 * Print basic info about all renderbuffers to stdout.
421 * If dumpImages is true, write PPM of level[0] image to a file.
424 _mesa_dump_renderbuffers(GLboolean writeImages
)
426 GET_CURRENT_CONTEXT(ctx
);
427 WriteImages
= writeImages
;
428 _mesa_HashWalk(ctx
->Shared
->RenderBuffers
, dump_renderbuffer_cb
, ctx
);
434 _mesa_dump_color_buffer(const char *filename
)
436 GET_CURRENT_CONTEXT(ctx
);
437 const GLuint w
= ctx
->DrawBuffer
->Width
;
438 const GLuint h
= ctx
->DrawBuffer
->Height
;
441 buf
= (GLubyte
*) malloc(w
* h
* 4);
443 _mesa_PushClientAttrib(GL_CLIENT_PIXEL_STORE_BIT
);
444 _mesa_PixelStorei(GL_PACK_ALIGNMENT
, 1);
445 _mesa_PixelStorei(GL_PACK_INVERT_MESA
, GL_TRUE
);
447 _mesa_ReadPixels(0, 0, w
, h
, GL_RGBA
, GL_UNSIGNED_BYTE
, buf
);
449 printf("ReadBuffer %p 0x%x DrawBuffer %p 0x%x\n",
450 (void *) ctx
->ReadBuffer
->_ColorReadBuffer
,
451 ctx
->ReadBuffer
->ColorReadBuffer
,
452 (void *) ctx
->DrawBuffer
->_ColorDrawBuffer
,
453 ctx
->DrawBuffer
->ColorDrawBuffer
);
454 printf("Writing %d x %d color buffer to %s\n", w
, h
, filename
);
455 write_ppm(filename
, buf
, w
, h
, 4, 0, 1, 2, GL_TRUE
);
457 _mesa_PopClientAttrib();
464 _mesa_dump_depth_buffer(const char *filename
)
466 GET_CURRENT_CONTEXT(ctx
);
467 const GLuint w
= ctx
->DrawBuffer
->Width
;
468 const GLuint h
= ctx
->DrawBuffer
->Height
;
473 buf
= (GLuint
*) malloc(w
* h
* 4); /* 4 bpp */
474 buf2
= (GLubyte
*) malloc(w
* h
* 3); /* 3 bpp */
476 _mesa_PushClientAttrib(GL_CLIENT_PIXEL_STORE_BIT
);
477 _mesa_PixelStorei(GL_PACK_ALIGNMENT
, 1);
478 _mesa_PixelStorei(GL_PACK_INVERT_MESA
, GL_TRUE
);
480 _mesa_ReadPixels(0, 0, w
, h
, GL_DEPTH_COMPONENT
, GL_UNSIGNED_INT
, buf
);
482 /* spread 24 bits of Z across R, G, B */
483 for (i
= 0; i
< w
* h
; i
++) {
484 buf2
[i
*3+0] = (buf
[i
] >> 24) & 0xff;
485 buf2
[i
*3+1] = (buf
[i
] >> 16) & 0xff;
486 buf2
[i
*3+2] = (buf
[i
] >> 8) & 0xff;
489 printf("Writing %d x %d depth buffer to %s\n", w
, h
, filename
);
490 write_ppm(filename
, buf2
, w
, h
, 3, 0, 1, 2, GL_TRUE
);
492 _mesa_PopClientAttrib();
500 _mesa_dump_stencil_buffer(const char *filename
)
502 GET_CURRENT_CONTEXT(ctx
);
503 const GLuint w
= ctx
->DrawBuffer
->Width
;
504 const GLuint h
= ctx
->DrawBuffer
->Height
;
509 buf
= (GLubyte
*) malloc(w
* h
); /* 1 bpp */
510 buf2
= (GLubyte
*) malloc(w
* h
* 3); /* 3 bpp */
512 _mesa_PushClientAttrib(GL_CLIENT_PIXEL_STORE_BIT
);
513 _mesa_PixelStorei(GL_PACK_ALIGNMENT
, 1);
514 _mesa_PixelStorei(GL_PACK_INVERT_MESA
, GL_TRUE
);
516 _mesa_ReadPixels(0, 0, w
, h
, GL_STENCIL_INDEX
, GL_UNSIGNED_BYTE
, buf
);
518 for (i
= 0; i
< w
* h
; i
++) {
519 buf2
[i
*3+0] = buf
[i
];
520 buf2
[i
*3+1] = (buf
[i
] & 127) * 2;
521 buf2
[i
*3+2] = (buf
[i
] - 128) * 2;
524 printf("Writing %d x %d stencil buffer to %s\n", w
, h
, filename
);
525 write_ppm(filename
, buf2
, w
, h
, 3, 0, 1, 2, GL_TRUE
);
527 _mesa_PopClientAttrib();
535 _mesa_dump_image(const char *filename
, const void *image
, GLuint w
, GLuint h
,
536 GLenum format
, GLenum type
)
538 GLboolean invert
= GL_TRUE
;
540 if (format
== GL_RGBA
&& type
== GL_UNSIGNED_BYTE
) {
541 write_ppm(filename
, image
, w
, h
, 4, 0, 1, 2, invert
);
543 else if (format
== GL_BGRA
&& type
== GL_UNSIGNED_BYTE
) {
544 write_ppm(filename
, image
, w
, h
, 4, 2, 1, 0, invert
);
546 else if (format
== GL_LUMINANCE_ALPHA
&& type
== GL_UNSIGNED_BYTE
) {
547 write_ppm(filename
, image
, w
, h
, 2, 1, 0, 0, invert
);
550 _mesa_problem(NULL
, "Unsupported format/type in _mesa_dump_image()");
556 * Quick and dirty function to "print" a texture to stdout.
559 _mesa_print_texture(struct gl_context
*ctx
, struct gl_texture_image
*img
)
561 const GLint slice
= 0;
566 ctx
->Driver
.MapTextureImage(ctx
, img
, slice
,
567 0, 0, img
->Width
, img
->Height
, GL_MAP_READ_BIT
,
568 &data
, &srcRowStride
);
571 printf("No texture data\n");
574 /* XXX add more formats or make into a new format utility function */
575 switch (img
->TexFormat
) {
581 case MESA_FORMAT_AL88
:
582 case MESA_FORMAT_AL88_REV
:
585 case MESA_FORMAT_RGB888
:
586 case MESA_FORMAT_BGR888
:
589 case MESA_FORMAT_RGBA8888
:
590 case MESA_FORMAT_ARGB8888
:
594 _mesa_problem(NULL
, "error in PrintTexture\n");
598 for (i
= 0; i
< img
->Height
; i
++) {
599 for (j
= 0; j
< img
->Width
; j
++) {
601 printf("%02x ", data
[0]);
603 printf("%02x%02x ", data
[0], data
[1]);
605 printf("%02x%02x%02x ", data
[0], data
[1], data
[2]);
607 printf("%02x%02x%02x%02x ", data
[0], data
[1], data
[2], data
[3]);
608 data
+= (srcRowStride
- img
->Width
) * c
;
610 /* XXX use img->ImageStride here */
616 ctx
->Driver
.UnmapTextureImage(ctx
, img
, slice
);