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.
36 #include "pixelstore.h"
38 #include "texgetimage.h"
45 const char *_mesa_prim_name
[GL_POLYGON
+4] = {
57 "inside unkown primitive",
63 tex_target_name(GLenum tgt
)
69 { GL_TEXTURE_1D
, "GL_TEXTURE_1D" },
70 { GL_TEXTURE_2D
, "GL_TEXTURE_2D" },
71 { GL_TEXTURE_3D
, "GL_TEXTURE_3D" },
72 { GL_TEXTURE_CUBE_MAP
, "GL_TEXTURE_CUBE_MAP" },
73 { GL_TEXTURE_RECTANGLE
, "GL_TEXTURE_RECTANGLE" },
74 { GL_TEXTURE_1D_ARRAY_EXT
, "GL_TEXTURE_1D_ARRAY" },
75 { GL_TEXTURE_2D_ARRAY_EXT
, "GL_TEXTURE_2D_ARRAY" }
78 for (i
= 0; i
< Elements(tex_targets
); i
++) {
79 if (tex_targets
[i
].target
== tgt
)
80 return tex_targets
[i
].name
;
82 return "UNKNOWN TEX TARGET";
87 _mesa_print_state( const char *msg
, GLuint state
)
90 "%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",
93 (state
& _NEW_MODELVIEW
) ? "ctx->ModelView, " : "",
94 (state
& _NEW_PROJECTION
) ? "ctx->Projection, " : "",
95 (state
& _NEW_TEXTURE_MATRIX
) ? "ctx->TextureMatrix, " : "",
96 (state
& _NEW_COLOR_MATRIX
) ? "ctx->ColorMatrix, " : "",
97 (state
& _NEW_ACCUM
) ? "ctx->Accum, " : "",
98 (state
& _NEW_COLOR
) ? "ctx->Color, " : "",
99 (state
& _NEW_DEPTH
) ? "ctx->Depth, " : "",
100 (state
& _NEW_EVAL
) ? "ctx->Eval/EvalMap, " : "",
101 (state
& _NEW_FOG
) ? "ctx->Fog, " : "",
102 (state
& _NEW_HINT
) ? "ctx->Hint, " : "",
103 (state
& _NEW_LIGHT
) ? "ctx->Light, " : "",
104 (state
& _NEW_LINE
) ? "ctx->Line, " : "",
105 (state
& _NEW_PIXEL
) ? "ctx->Pixel, " : "",
106 (state
& _NEW_POINT
) ? "ctx->Point, " : "",
107 (state
& _NEW_POLYGON
) ? "ctx->Polygon, " : "",
108 (state
& _NEW_POLYGONSTIPPLE
) ? "ctx->PolygonStipple, " : "",
109 (state
& _NEW_SCISSOR
) ? "ctx->Scissor, " : "",
110 (state
& _NEW_TEXTURE
) ? "ctx->Texture, " : "",
111 (state
& _NEW_TRANSFORM
) ? "ctx->Transform, " : "",
112 (state
& _NEW_VIEWPORT
) ? "ctx->Viewport, " : "",
113 (state
& _NEW_PACKUNPACK
) ? "ctx->Pack/Unpack, " : "",
114 (state
& _NEW_ARRAY
) ? "ctx->Array, " : "",
115 (state
& _NEW_RENDERMODE
) ? "ctx->RenderMode, " : "",
116 (state
& _NEW_BUFFERS
) ? "ctx->Visual, ctx->DrawBuffer,, " : "");
122 _mesa_print_tri_caps( const char *name
, GLuint flags
)
125 "%s: (0x%x) %s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n",
128 (flags
& DD_FLATSHADE
) ? "flat-shade, " : "",
129 (flags
& DD_SEPARATE_SPECULAR
) ? "separate-specular, " : "",
130 (flags
& DD_TRI_LIGHT_TWOSIDE
) ? "tri-light-twoside, " : "",
131 (flags
& DD_TRI_TWOSTENCIL
) ? "tri-twostencil, " : "",
132 (flags
& DD_TRI_UNFILLED
) ? "tri-unfilled, " : "",
133 (flags
& DD_TRI_STIPPLE
) ? "tri-stipple, " : "",
134 (flags
& DD_TRI_OFFSET
) ? "tri-offset, " : "",
135 (flags
& DD_TRI_SMOOTH
) ? "tri-smooth, " : "",
136 (flags
& DD_LINE_SMOOTH
) ? "line-smooth, " : "",
137 (flags
& DD_LINE_STIPPLE
) ? "line-stipple, " : "",
138 (flags
& DD_LINE_WIDTH
) ? "line-wide, " : "",
139 (flags
& DD_POINT_SMOOTH
) ? "point-smooth, " : "",
140 (flags
& DD_POINT_SIZE
) ? "point-size, " : "",
141 (flags
& DD_POINT_ATTEN
) ? "point-atten, " : "",
142 (flags
& DD_TRI_CULL_FRONT_BACK
) ? "cull-all, " : ""
148 * Print information about this Mesa version and build options.
150 void _mesa_print_info( void )
152 _mesa_debug(NULL
, "Mesa GL_VERSION = %s\n",
153 (char *) _mesa_GetString(GL_VERSION
));
154 _mesa_debug(NULL
, "Mesa GL_RENDERER = %s\n",
155 (char *) _mesa_GetString(GL_RENDERER
));
156 _mesa_debug(NULL
, "Mesa GL_VENDOR = %s\n",
157 (char *) _mesa_GetString(GL_VENDOR
));
158 _mesa_debug(NULL
, "Mesa GL_EXTENSIONS = %s\n",
159 (char *) _mesa_GetString(GL_EXTENSIONS
));
161 _mesa_debug(NULL
, "Mesa thread-safe: YES\n");
163 _mesa_debug(NULL
, "Mesa thread-safe: NO\n");
165 #if defined(USE_X86_ASM)
166 _mesa_debug(NULL
, "Mesa x86-optimized: YES\n");
168 _mesa_debug(NULL
, "Mesa x86-optimized: NO\n");
170 #if defined(USE_SPARC_ASM)
171 _mesa_debug(NULL
, "Mesa sparc-optimized: YES\n");
173 _mesa_debug(NULL
, "Mesa sparc-optimized: NO\n");
179 * Set the debugging flags.
181 * \param debug debug string
183 * If compiled with debugging support then search for keywords in \p debug and
184 * enables the verbose debug output of the respective feature.
186 static void add_debug_flags( const char *debug
)
189 struct debug_option
{
193 static const struct debug_option debug_opt
[] = {
194 { "varray", VERBOSE_VARRAY
},
195 { "tex", VERBOSE_TEXTURE
},
196 { "mat", VERBOSE_MATERIAL
},
197 { "pipe", VERBOSE_PIPELINE
},
198 { "driver", VERBOSE_DRIVER
},
199 { "state", VERBOSE_STATE
},
200 { "api", VERBOSE_API
},
201 { "list", VERBOSE_DISPLAY_LIST
},
202 { "lighting", VERBOSE_LIGHTING
},
203 { "disassem", VERBOSE_DISASSEM
},
204 { "draw", VERBOSE_DRAW
},
205 { "swap", VERBOSE_SWAPBUFFERS
}
210 for (i
= 0; i
< Elements(debug_opt
); i
++) {
211 if (_mesa_strstr(debug
, debug_opt
[i
].name
))
212 MESA_VERBOSE
|= debug_opt
[i
].flag
;
217 if (_mesa_strstr(debug
, "flush"))
218 MESA_DEBUG_FLAGS
|= DEBUG_ALWAYS_FLUSH
;
220 #if defined(_FPU_GETCW) && defined(_FPU_SETCW)
221 if (_mesa_strstr(debug
, "fpexceptions")) {
222 /* raise FP exceptions */
225 mask
&= ~(_FPU_MASK_IM
| _FPU_MASK_DM
| _FPU_MASK_ZM
226 | _FPU_MASK_OM
| _FPU_MASK_UM
);
238 _mesa_init_debug( GLcontext
*ctx
)
243 ctx
->NoDither
= _mesa_getenv("MESA_NO_DITHER") ? GL_TRUE
: GL_FALSE
;
245 if (_mesa_getenv("MESA_DEBUG")) {
246 _mesa_debug(ctx
, "MESA_NO_DITHER set - dithering disabled\n");
248 ctx
->Color
.DitherFlag
= GL_FALSE
;
251 c
= _mesa_getenv("MESA_DEBUG");
255 c
= _mesa_getenv("MESA_VERBOSE");
265 write_ppm(const char *filename
, const GLubyte
*buffer
, int width
, int height
,
266 int comps
, int rcomp
, int gcomp
, int bcomp
, GLboolean invert
)
268 FILE *f
= fopen( filename
, "w" );
271 const GLubyte
*ptr
= buffer
;
273 fprintf(f
,"# ppm-file created by osdemo.c\n");
274 fprintf(f
,"%i %i\n", width
,height
);
277 f
= fopen( filename
, "ab" ); /* reopen in binary append mode */
278 for (y
=0; y
< height
; y
++) {
279 for (x
= 0; x
< width
; x
++) {
280 int yy
= invert
? (height
- 1 - y
) : y
;
281 int i
= (yy
* width
+ x
) * comps
;
282 fputc(ptr
[i
+rcomp
], f
); /* write red */
283 fputc(ptr
[i
+gcomp
], f
); /* write green */
284 fputc(ptr
[i
+bcomp
], f
); /* write blue */
293 * Write a texture image to a ppm file.
294 * \param face cube face in [0,5]
295 * \param level mipmap level
298 write_texture_image(struct gl_texture_object
*texObj
,
299 GLuint face
, GLuint level
)
301 struct gl_texture_image
*img
= texObj
->Image
[face
][level
];
303 GET_CURRENT_CONTEXT(ctx
);
304 struct gl_pixelstore_attrib store
;
308 buffer
= (GLubyte
*) _mesa_malloc(img
->Width
* img
->Height
311 store
= ctx
->Pack
; /* save */
312 ctx
->Pack
= ctx
->DefaultPacking
;
314 ctx
->Driver
.GetTexImage(ctx
, texObj
->Target
, level
,
315 GL_RGBA
, GL_UNSIGNED_BYTE
,
316 buffer
, texObj
, img
);
319 _mesa_sprintf(s
, "/tmp/tex%u.l%u.f%u.ppm", texObj
->Name
, level
, face
);
321 _mesa_printf(" Writing image level %u to %s\n", level
, s
);
322 write_ppm(s
, buffer
, img
->Width
, img
->Height
, 4, 0, 1, 2, GL_FALSE
);
324 ctx
->Pack
= store
; /* restore */
332 * Write renderbuffer image to a ppm file.
335 write_renderbuffer_image(const struct gl_renderbuffer
*rb
)
337 GET_CURRENT_CONTEXT(ctx
);
342 if (rb
->_BaseFormat
== GL_RGB
||
343 rb
->_BaseFormat
== GL_RGBA
) {
345 type
= GL_UNSIGNED_BYTE
;
347 else if (rb
->_BaseFormat
== GL_DEPTH_STENCIL
) {
348 format
= GL_DEPTH_STENCIL
;
349 type
= GL_UNSIGNED_INT_24_8
;
355 buffer
= (GLubyte
*) _mesa_malloc(rb
->Width
* rb
->Height
* 4);
357 ctx
->Driver
.ReadPixels(ctx
, 0, 0, rb
->Width
, rb
->Height
,
358 format
, type
, &ctx
->DefaultPacking
, buffer
);
361 _mesa_sprintf(s
, "/tmp/renderbuffer%u.ppm", rb
->Name
);
363 _mesa_printf(" Writing renderbuffer image to %s\n", s
);
364 write_ppm(s
, buffer
, rb
->Width
, rb
->Height
, 4, 0, 1, 2, GL_TRUE
);
370 /** How many texture images (mipmap levels, faces) to write to files */
375 static GLuint WriteImages
;
379 dump_texture(struct gl_texture_object
*texObj
, GLuint writeImages
)
381 const GLuint numFaces
= texObj
->Target
== GL_TEXTURE_CUBE_MAP
? 6 : 1;
382 GLboolean written
= GL_FALSE
;
385 _mesa_printf("Texture %u\n", texObj
->Name
);
386 _mesa_printf(" Target %s\n", tex_target_name(texObj
->Target
));
387 for (i
= 0; i
< MAX_TEXTURE_LEVELS
; i
++) {
388 for (j
= 0; j
< numFaces
; j
++) {
389 struct gl_texture_image
*texImg
= texObj
->Image
[j
][i
];
391 _mesa_printf(" Face %u level %u: %d x %d x %d, format %s at %p\n",
393 texImg
->Width
, texImg
->Height
, texImg
->Depth
,
394 _mesa_get_format_name(texImg
->TexFormat
),
396 if (writeImages
== WRITE_ALL
||
397 (writeImages
== WRITE_ONE
&& !written
)) {
398 write_texture_image(texObj
, j
, i
);
408 * Dump a single texture.
411 _mesa_dump_texture(GLuint texture
, GLuint writeImages
)
413 GET_CURRENT_CONTEXT(ctx
);
414 struct gl_texture_object
*texObj
= _mesa_lookup_texture(ctx
, texture
);
416 dump_texture(texObj
, writeImages
);
422 dump_texture_cb(GLuint id
, void *data
, void *userData
)
424 struct gl_texture_object
*texObj
= (struct gl_texture_object
*) data
;
426 dump_texture(texObj
, WriteImages
);
431 * Print basic info about all texture objext to stdout.
432 * If dumpImages is true, write PPM of level[0] image to a file.
435 _mesa_dump_textures(GLuint writeImages
)
437 GET_CURRENT_CONTEXT(ctx
);
438 WriteImages
= writeImages
;
439 _mesa_HashWalk(ctx
->Shared
->TexObjects
, dump_texture_cb
, ctx
);
444 dump_renderbuffer(const struct gl_renderbuffer
*rb
, GLboolean writeImage
)
446 _mesa_printf("Renderbuffer %u: %u x %u IntFormat = %s\n",
447 rb
->Name
, rb
->Width
, rb
->Height
,
448 _mesa_lookup_enum_by_nr(rb
->InternalFormat
));
450 write_renderbuffer_image(rb
);
456 dump_renderbuffer_cb(GLuint id
, void *data
, void *userData
)
458 const struct gl_renderbuffer
*rb
= (const struct gl_renderbuffer
*) data
;
460 dump_renderbuffer(rb
, WriteImages
);
465 * Print basic info about all renderbuffers to stdout.
466 * If dumpImages is true, write PPM of level[0] image to a file.
469 _mesa_dump_renderbuffers(GLboolean writeImages
)
471 GET_CURRENT_CONTEXT(ctx
);
472 WriteImages
= writeImages
;
473 _mesa_HashWalk(ctx
->Shared
->RenderBuffers
, dump_renderbuffer_cb
, ctx
);
479 _mesa_dump_color_buffer(const char *filename
)
481 GET_CURRENT_CONTEXT(ctx
);
482 const GLuint w
= ctx
->DrawBuffer
->Width
;
483 const GLuint h
= ctx
->DrawBuffer
->Height
;
486 buf
= (GLubyte
*) _mesa_malloc(w
* h
* 4);
488 _mesa_PushClientAttrib(GL_CLIENT_PIXEL_STORE_BIT
);
489 _mesa_PixelStorei(GL_PACK_ALIGNMENT
, 1);
490 _mesa_PixelStorei(GL_PACK_INVERT_MESA
, GL_TRUE
);
492 _mesa_ReadPixels(0, 0, w
, h
, GL_RGBA
, GL_UNSIGNED_BYTE
, buf
);
494 _mesa_printf("ReadBuffer %p 0x%x DrawBuffer %p 0x%x\n",
495 ctx
->ReadBuffer
->_ColorReadBuffer
,
496 ctx
->ReadBuffer
->ColorReadBuffer
,
497 ctx
->DrawBuffer
->_ColorDrawBuffers
[0],
498 ctx
->DrawBuffer
->ColorDrawBuffer
[0]);
499 _mesa_printf("Writing %d x %d color buffer to %s\n", w
, h
, filename
);
500 write_ppm(filename
, buf
, w
, h
, 4, 0, 1, 2, GL_TRUE
);
502 _mesa_PopClientAttrib();
509 _mesa_dump_depth_buffer(const char *filename
)
511 GET_CURRENT_CONTEXT(ctx
);
512 const GLuint w
= ctx
->DrawBuffer
->Width
;
513 const GLuint h
= ctx
->DrawBuffer
->Height
;
518 buf
= (GLuint
*) _mesa_malloc(w
* h
* 4); /* 4 bpp */
519 buf2
= (GLubyte
*) _mesa_malloc(w
* h
* 3); /* 3 bpp */
521 _mesa_PushClientAttrib(GL_CLIENT_PIXEL_STORE_BIT
);
522 _mesa_PixelStorei(GL_PACK_ALIGNMENT
, 1);
523 _mesa_PixelStorei(GL_PACK_INVERT_MESA
, GL_TRUE
);
525 _mesa_ReadPixels(0, 0, w
, h
, GL_DEPTH_COMPONENT
, GL_UNSIGNED_INT
, buf
);
527 /* spread 24 bits of Z across R, G, B */
528 for (i
= 0; i
< w
* h
; i
++) {
529 buf2
[i
*3+0] = (buf
[i
] >> 24) & 0xff;
530 buf2
[i
*3+1] = (buf
[i
] >> 16) & 0xff;
531 buf2
[i
*3+2] = (buf
[i
] >> 8) & 0xff;
534 _mesa_printf("Writing %d x %d depth buffer to %s\n", w
, h
, filename
);
535 write_ppm(filename
, buf2
, w
, h
, 3, 0, 1, 2, GL_TRUE
);
537 _mesa_PopClientAttrib();
545 _mesa_dump_stencil_buffer(const char *filename
)
547 GET_CURRENT_CONTEXT(ctx
);
548 const GLuint w
= ctx
->DrawBuffer
->Width
;
549 const GLuint h
= ctx
->DrawBuffer
->Height
;
554 buf
= (GLubyte
*) _mesa_malloc(w
* h
); /* 1 bpp */
555 buf2
= (GLubyte
*) _mesa_malloc(w
* h
* 3); /* 3 bpp */
557 _mesa_PushClientAttrib(GL_CLIENT_PIXEL_STORE_BIT
);
558 _mesa_PixelStorei(GL_PACK_ALIGNMENT
, 1);
559 _mesa_PixelStorei(GL_PACK_INVERT_MESA
, GL_TRUE
);
561 _mesa_ReadPixels(0, 0, w
, h
, GL_STENCIL_INDEX
, GL_UNSIGNED_BYTE
, buf
);
563 for (i
= 0; i
< w
* h
; i
++) {
564 buf2
[i
*3+0] = buf
[i
];
565 buf2
[i
*3+1] = (buf
[i
] & 127) * 2;
566 buf2
[i
*3+2] = (buf
[i
] - 128) * 2;
569 _mesa_printf("Writing %d x %d stencil buffer to %s\n", w
, h
, filename
);
570 write_ppm(filename
, buf2
, w
, h
, 3, 0, 1, 2, GL_TRUE
);
572 _mesa_PopClientAttrib();
580 * Quick and dirty function to "print" a texture to stdout.
583 _mesa_print_texture(GLcontext
*ctx
, const struct gl_texture_image
*img
)
585 #if CHAN_TYPE != GL_UNSIGNED_BYTE
586 _mesa_problem(NULL
, "PrintTexture not supported");
589 const GLubyte
*data
= (const GLubyte
*) img
->Data
;
592 _mesa_printf("No texture data\n");
596 /* XXX add more formats or make into a new format utility function */
597 switch (img
->TexFormat
) {
601 case MESA_FORMAT_CI8
:
604 case MESA_FORMAT_AL88
:
605 case MESA_FORMAT_AL88_REV
:
608 case MESA_FORMAT_RGB888
:
609 case MESA_FORMAT_BGR888
:
612 case MESA_FORMAT_RGBA8888
:
613 case MESA_FORMAT_ARGB8888
:
617 _mesa_problem(NULL
, "error in PrintTexture\n");
621 for (i
= 0; i
< img
->Height
; i
++) {
622 for (j
= 0; j
< img
->Width
; j
++) {
624 _mesa_printf("%02x ", data
[0]);
626 _mesa_printf("%02x%02x ", data
[0], data
[1]);
628 _mesa_printf("%02x%02x%02x ", data
[0], data
[1], data
[2]);
630 _mesa_printf("%02x%02x%02x%02x ", data
[0], data
[1], data
[2], data
[3]);
631 data
+= (img
->RowStride
- img
->Width
) * c
;
633 /* XXX use img->ImageStride here */