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%s\n",
91 (state
& _NEW_MODELVIEW
) ? "ctx->ModelView, " : "",
92 (state
& _NEW_PROJECTION
) ? "ctx->Projection, " : "",
93 (state
& _NEW_TEXTURE_MATRIX
) ? "ctx->TextureMatrix, " : "",
94 (state
& _NEW_COLOR_MATRIX
) ? "ctx->ColorMatrix, " : "",
95 (state
& _NEW_ACCUM
) ? "ctx->Accum, " : "",
96 (state
& _NEW_COLOR
) ? "ctx->Color, " : "",
97 (state
& _NEW_DEPTH
) ? "ctx->Depth, " : "",
98 (state
& _NEW_EVAL
) ? "ctx->Eval/EvalMap, " : "",
99 (state
& _NEW_FOG
) ? "ctx->Fog, " : "",
100 (state
& _NEW_HINT
) ? "ctx->Hint, " : "",
101 (state
& _NEW_LIGHT
) ? "ctx->Light, " : "",
102 (state
& _NEW_LINE
) ? "ctx->Line, " : "",
103 (state
& _NEW_PIXEL
) ? "ctx->Pixel, " : "",
104 (state
& _NEW_POINT
) ? "ctx->Point, " : "",
105 (state
& _NEW_POLYGON
) ? "ctx->Polygon, " : "",
106 (state
& _NEW_POLYGONSTIPPLE
) ? "ctx->PolygonStipple, " : "",
107 (state
& _NEW_SCISSOR
) ? "ctx->Scissor, " : "",
108 (state
& _NEW_STENCIL
) ? "ctx->Stencil, " : "",
109 (state
& _NEW_TEXTURE
) ? "ctx->Texture, " : "",
110 (state
& _NEW_TRANSFORM
) ? "ctx->Transform, " : "",
111 (state
& _NEW_VIEWPORT
) ? "ctx->Viewport, " : "",
112 (state
& _NEW_PACKUNPACK
) ? "ctx->Pack/Unpack, " : "",
113 (state
& _NEW_ARRAY
) ? "ctx->Array, " : "",
114 (state
& _NEW_RENDERMODE
) ? "ctx->RenderMode, " : "",
115 (state
& _NEW_BUFFERS
) ? "ctx->Visual, ctx->DrawBuffer,, " : "");
121 _mesa_print_tri_caps( const char *name
, GLuint flags
)
124 "%s: (0x%x) %s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n",
127 (flags
& DD_FLATSHADE
) ? "flat-shade, " : "",
128 (flags
& DD_SEPARATE_SPECULAR
) ? "separate-specular, " : "",
129 (flags
& DD_TRI_LIGHT_TWOSIDE
) ? "tri-light-twoside, " : "",
130 (flags
& DD_TRI_TWOSTENCIL
) ? "tri-twostencil, " : "",
131 (flags
& DD_TRI_UNFILLED
) ? "tri-unfilled, " : "",
132 (flags
& DD_TRI_STIPPLE
) ? "tri-stipple, " : "",
133 (flags
& DD_TRI_OFFSET
) ? "tri-offset, " : "",
134 (flags
& DD_TRI_SMOOTH
) ? "tri-smooth, " : "",
135 (flags
& DD_LINE_SMOOTH
) ? "line-smooth, " : "",
136 (flags
& DD_LINE_STIPPLE
) ? "line-stipple, " : "",
137 (flags
& DD_LINE_WIDTH
) ? "line-wide, " : "",
138 (flags
& DD_POINT_SMOOTH
) ? "point-smooth, " : "",
139 (flags
& DD_POINT_SIZE
) ? "point-size, " : "",
140 (flags
& DD_POINT_ATTEN
) ? "point-atten, " : "",
141 (flags
& DD_TRI_CULL_FRONT_BACK
) ? "cull-all, " : ""
147 * Print information about this Mesa version and build options.
149 void _mesa_print_info( void )
151 _mesa_debug(NULL
, "Mesa GL_VERSION = %s\n",
152 (char *) _mesa_GetString(GL_VERSION
));
153 _mesa_debug(NULL
, "Mesa GL_RENDERER = %s\n",
154 (char *) _mesa_GetString(GL_RENDERER
));
155 _mesa_debug(NULL
, "Mesa GL_VENDOR = %s\n",
156 (char *) _mesa_GetString(GL_VENDOR
));
157 _mesa_debug(NULL
, "Mesa GL_EXTENSIONS = %s\n",
158 (char *) _mesa_GetString(GL_EXTENSIONS
));
160 _mesa_debug(NULL
, "Mesa thread-safe: YES\n");
162 _mesa_debug(NULL
, "Mesa thread-safe: NO\n");
164 #if defined(USE_X86_ASM)
165 _mesa_debug(NULL
, "Mesa x86-optimized: YES\n");
167 _mesa_debug(NULL
, "Mesa x86-optimized: NO\n");
169 #if defined(USE_SPARC_ASM)
170 _mesa_debug(NULL
, "Mesa sparc-optimized: YES\n");
172 _mesa_debug(NULL
, "Mesa sparc-optimized: NO\n");
178 * Set the debugging flags.
180 * \param debug debug string
182 * If compiled with debugging support then search for keywords in \p debug and
183 * enables the verbose debug output of the respective feature.
185 static void add_debug_flags( const char *debug
)
188 struct debug_option
{
192 static const struct debug_option debug_opt
[] = {
193 { "varray", VERBOSE_VARRAY
},
194 { "tex", VERBOSE_TEXTURE
},
195 { "mat", VERBOSE_MATERIAL
},
196 { "pipe", VERBOSE_PIPELINE
},
197 { "driver", VERBOSE_DRIVER
},
198 { "state", VERBOSE_STATE
},
199 { "api", VERBOSE_API
},
200 { "list", VERBOSE_DISPLAY_LIST
},
201 { "lighting", VERBOSE_LIGHTING
},
202 { "disassem", VERBOSE_DISASSEM
},
203 { "draw", VERBOSE_DRAW
},
204 { "swap", VERBOSE_SWAPBUFFERS
}
209 for (i
= 0; i
< Elements(debug_opt
); i
++) {
210 if (strstr(debug
, debug_opt
[i
].name
))
211 MESA_VERBOSE
|= debug_opt
[i
].flag
;
216 if (strstr(debug
, "flush"))
217 MESA_DEBUG_FLAGS
|= DEBUG_ALWAYS_FLUSH
;
219 #if defined(_FPU_GETCW) && defined(_FPU_SETCW)
220 if (strstr(debug
, "fpexceptions")) {
221 /* raise FP exceptions */
224 mask
&= ~(_FPU_MASK_IM
| _FPU_MASK_DM
| _FPU_MASK_ZM
225 | _FPU_MASK_OM
| _FPU_MASK_UM
);
237 _mesa_init_debug( GLcontext
*ctx
)
242 ctx
->NoDither
= _mesa_getenv("MESA_NO_DITHER") ? GL_TRUE
: GL_FALSE
;
244 if (_mesa_getenv("MESA_DEBUG")) {
245 _mesa_debug(ctx
, "MESA_NO_DITHER set - dithering disabled\n");
247 ctx
->Color
.DitherFlag
= GL_FALSE
;
250 c
= _mesa_getenv("MESA_DEBUG");
254 c
= _mesa_getenv("MESA_VERBOSE");
264 write_ppm(const char *filename
, const GLubyte
*buffer
, int width
, int height
,
265 int comps
, int rcomp
, int gcomp
, int bcomp
, GLboolean invert
)
267 FILE *f
= fopen( filename
, "w" );
270 const GLubyte
*ptr
= buffer
;
272 fprintf(f
,"# ppm-file created by osdemo.c\n");
273 fprintf(f
,"%i %i\n", width
,height
);
276 f
= fopen( filename
, "ab" ); /* reopen in binary append mode */
277 for (y
=0; y
< height
; y
++) {
278 for (x
= 0; x
< width
; x
++) {
279 int yy
= invert
? (height
- 1 - y
) : y
;
280 int i
= (yy
* width
+ x
) * comps
;
281 fputc(ptr
[i
+rcomp
], f
); /* write red */
282 fputc(ptr
[i
+gcomp
], f
); /* write green */
283 fputc(ptr
[i
+bcomp
], f
); /* write blue */
292 * Write a texture image to a ppm file.
293 * \param face cube face in [0,5]
294 * \param level mipmap level
297 write_texture_image(struct gl_texture_object
*texObj
,
298 GLuint face
, GLuint level
)
300 struct gl_texture_image
*img
= texObj
->Image
[face
][level
];
302 GET_CURRENT_CONTEXT(ctx
);
303 struct gl_pixelstore_attrib store
;
307 buffer
= (GLubyte
*) malloc(img
->Width
* img
->Height
310 store
= ctx
->Pack
; /* save */
311 ctx
->Pack
= ctx
->DefaultPacking
;
313 ctx
->Driver
.GetTexImage(ctx
, texObj
->Target
, level
,
314 GL_RGBA
, GL_UNSIGNED_BYTE
,
315 buffer
, texObj
, img
);
318 sprintf(s
, "/tmp/tex%u.l%u.f%u.ppm", texObj
->Name
, level
, face
);
320 printf(" Writing image level %u to %s\n", level
, s
);
321 write_ppm(s
, buffer
, img
->Width
, img
->Height
, 4, 0, 1, 2, GL_FALSE
);
323 ctx
->Pack
= store
; /* restore */
331 * Write renderbuffer image to a ppm file.
334 write_renderbuffer_image(const struct gl_renderbuffer
*rb
)
336 GET_CURRENT_CONTEXT(ctx
);
341 if (rb
->_BaseFormat
== GL_RGB
||
342 rb
->_BaseFormat
== GL_RGBA
) {
344 type
= GL_UNSIGNED_BYTE
;
346 else if (rb
->_BaseFormat
== GL_DEPTH_STENCIL
) {
347 format
= GL_DEPTH_STENCIL
;
348 type
= GL_UNSIGNED_INT_24_8
;
354 buffer
= (GLubyte
*) malloc(rb
->Width
* rb
->Height
* 4);
356 ctx
->Driver
.ReadPixels(ctx
, 0, 0, rb
->Width
, rb
->Height
,
357 format
, type
, &ctx
->DefaultPacking
, buffer
);
360 sprintf(s
, "/tmp/renderbuffer%u.ppm", rb
->Name
);
362 printf(" Writing renderbuffer image to %s\n", s
);
363 write_ppm(s
, buffer
, rb
->Width
, rb
->Height
, 4, 0, 1, 2, GL_TRUE
);
369 /** How many texture images (mipmap levels, faces) to write to files */
374 static GLuint WriteImages
;
378 dump_texture(struct gl_texture_object
*texObj
, GLuint writeImages
)
380 const GLuint numFaces
= texObj
->Target
== GL_TEXTURE_CUBE_MAP
? 6 : 1;
381 GLboolean written
= GL_FALSE
;
384 printf("Texture %u\n", texObj
->Name
);
385 printf(" Target %s\n", tex_target_name(texObj
->Target
));
386 for (i
= 0; i
< MAX_TEXTURE_LEVELS
; i
++) {
387 for (j
= 0; j
< numFaces
; j
++) {
388 struct gl_texture_image
*texImg
= texObj
->Image
[j
][i
];
390 printf(" Face %u level %u: %d x %d x %d, format %s at %p\n",
392 texImg
->Width
, texImg
->Height
, texImg
->Depth
,
393 _mesa_get_format_name(texImg
->TexFormat
),
395 if (writeImages
== WRITE_ALL
||
396 (writeImages
== WRITE_ONE
&& !written
)) {
397 write_texture_image(texObj
, j
, i
);
407 * Dump a single texture.
410 _mesa_dump_texture(GLuint texture
, GLuint writeImages
)
412 GET_CURRENT_CONTEXT(ctx
);
413 struct gl_texture_object
*texObj
= _mesa_lookup_texture(ctx
, texture
);
415 dump_texture(texObj
, writeImages
);
421 dump_texture_cb(GLuint id
, void *data
, void *userData
)
423 struct gl_texture_object
*texObj
= (struct gl_texture_object
*) data
;
425 dump_texture(texObj
, WriteImages
);
430 * Print basic info about all texture objext to stdout.
431 * If dumpImages is true, write PPM of level[0] image to a file.
434 _mesa_dump_textures(GLuint writeImages
)
436 GET_CURRENT_CONTEXT(ctx
);
437 WriteImages
= writeImages
;
438 _mesa_HashWalk(ctx
->Shared
->TexObjects
, dump_texture_cb
, ctx
);
443 dump_renderbuffer(const struct gl_renderbuffer
*rb
, GLboolean writeImage
)
445 printf("Renderbuffer %u: %u x %u IntFormat = %s\n",
446 rb
->Name
, rb
->Width
, rb
->Height
,
447 _mesa_lookup_enum_by_nr(rb
->InternalFormat
));
449 write_renderbuffer_image(rb
);
455 dump_renderbuffer_cb(GLuint id
, void *data
, void *userData
)
457 const struct gl_renderbuffer
*rb
= (const struct gl_renderbuffer
*) data
;
459 dump_renderbuffer(rb
, WriteImages
);
464 * Print basic info about all renderbuffers to stdout.
465 * If dumpImages is true, write PPM of level[0] image to a file.
468 _mesa_dump_renderbuffers(GLboolean writeImages
)
470 GET_CURRENT_CONTEXT(ctx
);
471 WriteImages
= writeImages
;
472 _mesa_HashWalk(ctx
->Shared
->RenderBuffers
, dump_renderbuffer_cb
, ctx
);
478 _mesa_dump_color_buffer(const char *filename
)
480 GET_CURRENT_CONTEXT(ctx
);
481 const GLuint w
= ctx
->DrawBuffer
->Width
;
482 const GLuint h
= ctx
->DrawBuffer
->Height
;
485 buf
= (GLubyte
*) malloc(w
* h
* 4);
487 _mesa_PushClientAttrib(GL_CLIENT_PIXEL_STORE_BIT
);
488 _mesa_PixelStorei(GL_PACK_ALIGNMENT
, 1);
489 _mesa_PixelStorei(GL_PACK_INVERT_MESA
, GL_TRUE
);
491 _mesa_ReadPixels(0, 0, w
, h
, GL_RGBA
, GL_UNSIGNED_BYTE
, buf
);
493 printf("ReadBuffer %p 0x%x DrawBuffer %p 0x%x\n",
494 (void *) ctx
->ReadBuffer
->_ColorReadBuffer
,
495 ctx
->ReadBuffer
->ColorReadBuffer
,
496 (void *) ctx
->DrawBuffer
->_ColorDrawBuffers
[0],
497 ctx
->DrawBuffer
->ColorDrawBuffer
[0]);
498 printf("Writing %d x %d color buffer to %s\n", w
, h
, filename
);
499 write_ppm(filename
, buf
, w
, h
, 4, 0, 1, 2, GL_TRUE
);
501 _mesa_PopClientAttrib();
508 _mesa_dump_depth_buffer(const char *filename
)
510 GET_CURRENT_CONTEXT(ctx
);
511 const GLuint w
= ctx
->DrawBuffer
->Width
;
512 const GLuint h
= ctx
->DrawBuffer
->Height
;
517 buf
= (GLuint
*) malloc(w
* h
* 4); /* 4 bpp */
518 buf2
= (GLubyte
*) malloc(w
* h
* 3); /* 3 bpp */
520 _mesa_PushClientAttrib(GL_CLIENT_PIXEL_STORE_BIT
);
521 _mesa_PixelStorei(GL_PACK_ALIGNMENT
, 1);
522 _mesa_PixelStorei(GL_PACK_INVERT_MESA
, GL_TRUE
);
524 _mesa_ReadPixels(0, 0, w
, h
, GL_DEPTH_COMPONENT
, GL_UNSIGNED_INT
, buf
);
526 /* spread 24 bits of Z across R, G, B */
527 for (i
= 0; i
< w
* h
; i
++) {
528 buf2
[i
*3+0] = (buf
[i
] >> 24) & 0xff;
529 buf2
[i
*3+1] = (buf
[i
] >> 16) & 0xff;
530 buf2
[i
*3+2] = (buf
[i
] >> 8) & 0xff;
533 printf("Writing %d x %d depth buffer to %s\n", w
, h
, filename
);
534 write_ppm(filename
, buf2
, w
, h
, 3, 0, 1, 2, GL_TRUE
);
536 _mesa_PopClientAttrib();
544 _mesa_dump_stencil_buffer(const char *filename
)
546 GET_CURRENT_CONTEXT(ctx
);
547 const GLuint w
= ctx
->DrawBuffer
->Width
;
548 const GLuint h
= ctx
->DrawBuffer
->Height
;
553 buf
= (GLubyte
*) malloc(w
* h
); /* 1 bpp */
554 buf2
= (GLubyte
*) malloc(w
* h
* 3); /* 3 bpp */
556 _mesa_PushClientAttrib(GL_CLIENT_PIXEL_STORE_BIT
);
557 _mesa_PixelStorei(GL_PACK_ALIGNMENT
, 1);
558 _mesa_PixelStorei(GL_PACK_INVERT_MESA
, GL_TRUE
);
560 _mesa_ReadPixels(0, 0, w
, h
, GL_STENCIL_INDEX
, GL_UNSIGNED_BYTE
, buf
);
562 for (i
= 0; i
< w
* h
; i
++) {
563 buf2
[i
*3+0] = buf
[i
];
564 buf2
[i
*3+1] = (buf
[i
] & 127) * 2;
565 buf2
[i
*3+2] = (buf
[i
] - 128) * 2;
568 printf("Writing %d x %d stencil buffer to %s\n", w
, h
, filename
);
569 write_ppm(filename
, buf2
, w
, h
, 3, 0, 1, 2, GL_TRUE
);
571 _mesa_PopClientAttrib();
579 * Quick and dirty function to "print" a texture to stdout.
582 _mesa_print_texture(GLcontext
*ctx
, const struct gl_texture_image
*img
)
584 #if CHAN_TYPE != GL_UNSIGNED_BYTE
585 _mesa_problem(NULL
, "PrintTexture not supported");
588 const GLubyte
*data
= (const GLubyte
*) img
->Data
;
591 printf("No texture data\n");
595 /* XXX add more formats or make into a new format utility function */
596 switch (img
->TexFormat
) {
600 case MESA_FORMAT_CI8
:
603 case MESA_FORMAT_AL88
:
604 case MESA_FORMAT_AL88_REV
:
607 case MESA_FORMAT_RGB888
:
608 case MESA_FORMAT_BGR888
:
611 case MESA_FORMAT_RGBA8888
:
612 case MESA_FORMAT_ARGB8888
:
616 _mesa_problem(NULL
, "error in PrintTexture\n");
620 for (i
= 0; i
< img
->Height
; i
++) {
621 for (j
= 0; j
< img
->Width
; j
++) {
623 printf("%02x ", data
[0]);
625 printf("%02x%02x ", data
[0], data
[1]);
627 printf("%02x%02x%02x ", data
[0], data
[1], data
[2]);
629 printf("%02x%02x%02x%02x ", data
[0], data
[1], data
[2], data
[3]);
630 data
+= (img
->RowStride
- img
->Width
) * c
;
632 /* XXX use img->ImageStride here */