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\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_POINT_SMOOTH
) ? "point-smooth, " : "",
137 (flags
& DD_POINT_ATTEN
) ? "point-atten, " : "",
138 (flags
& DD_TRI_CULL_FRONT_BACK
) ? "cull-all, " : ""
144 * Print information about this Mesa version and build options.
146 void _mesa_print_info( void )
148 _mesa_debug(NULL
, "Mesa GL_VERSION = %s\n",
149 (char *) _mesa_GetString(GL_VERSION
));
150 _mesa_debug(NULL
, "Mesa GL_RENDERER = %s\n",
151 (char *) _mesa_GetString(GL_RENDERER
));
152 _mesa_debug(NULL
, "Mesa GL_VENDOR = %s\n",
153 (char *) _mesa_GetString(GL_VENDOR
));
154 _mesa_debug(NULL
, "Mesa GL_EXTENSIONS = %s\n",
155 (char *) _mesa_GetString(GL_EXTENSIONS
));
157 _mesa_debug(NULL
, "Mesa thread-safe: YES\n");
159 _mesa_debug(NULL
, "Mesa thread-safe: NO\n");
161 #if defined(USE_X86_ASM)
162 _mesa_debug(NULL
, "Mesa x86-optimized: YES\n");
164 _mesa_debug(NULL
, "Mesa x86-optimized: NO\n");
166 #if defined(USE_SPARC_ASM)
167 _mesa_debug(NULL
, "Mesa sparc-optimized: YES\n");
169 _mesa_debug(NULL
, "Mesa sparc-optimized: NO\n");
175 * Set the debugging flags.
177 * \param debug debug string
179 * If compiled with debugging support then search for keywords in \p debug and
180 * enables the verbose debug output of the respective feature.
182 static void add_debug_flags( const char *debug
)
185 struct debug_option
{
189 static const struct debug_option debug_opt
[] = {
190 { "varray", VERBOSE_VARRAY
},
191 { "tex", VERBOSE_TEXTURE
},
192 { "mat", VERBOSE_MATERIAL
},
193 { "pipe", VERBOSE_PIPELINE
},
194 { "driver", VERBOSE_DRIVER
},
195 { "state", VERBOSE_STATE
},
196 { "api", VERBOSE_API
},
197 { "list", VERBOSE_DISPLAY_LIST
},
198 { "lighting", VERBOSE_LIGHTING
},
199 { "disassem", VERBOSE_DISASSEM
},
200 { "draw", VERBOSE_DRAW
},
201 { "swap", VERBOSE_SWAPBUFFERS
}
206 for (i
= 0; i
< Elements(debug_opt
); i
++) {
207 if (strstr(debug
, debug_opt
[i
].name
))
208 MESA_VERBOSE
|= debug_opt
[i
].flag
;
213 if (strstr(debug
, "flush"))
214 MESA_DEBUG_FLAGS
|= DEBUG_ALWAYS_FLUSH
;
216 #if defined(_FPU_GETCW) && defined(_FPU_SETCW)
217 if (strstr(debug
, "fpexceptions")) {
218 /* raise FP exceptions */
221 mask
&= ~(_FPU_MASK_IM
| _FPU_MASK_DM
| _FPU_MASK_ZM
222 | _FPU_MASK_OM
| _FPU_MASK_UM
);
234 _mesa_init_debug( struct gl_context
*ctx
)
239 ctx
->NoDither
= _mesa_getenv("MESA_NO_DITHER") ? GL_TRUE
: GL_FALSE
;
241 if (_mesa_getenv("MESA_DEBUG")) {
242 _mesa_debug(ctx
, "MESA_NO_DITHER set - dithering disabled\n");
244 ctx
->Color
.DitherFlag
= GL_FALSE
;
247 c
= _mesa_getenv("MESA_DEBUG");
251 c
= _mesa_getenv("MESA_VERBOSE");
261 write_ppm(const char *filename
, const GLubyte
*buffer
, int width
, int height
,
262 int comps
, int rcomp
, int gcomp
, int bcomp
, GLboolean invert
)
264 FILE *f
= fopen( filename
, "w" );
267 const GLubyte
*ptr
= buffer
;
269 fprintf(f
,"# ppm-file created by osdemo.c\n");
270 fprintf(f
,"%i %i\n", width
,height
);
273 f
= fopen( filename
, "ab" ); /* reopen in binary append mode */
274 for (y
=0; y
< height
; y
++) {
275 for (x
= 0; x
< width
; x
++) {
276 int yy
= invert
? (height
- 1 - y
) : y
;
277 int i
= (yy
* width
+ x
) * comps
;
278 fputc(ptr
[i
+rcomp
], f
); /* write red */
279 fputc(ptr
[i
+gcomp
], f
); /* write green */
280 fputc(ptr
[i
+bcomp
], f
); /* write blue */
289 * Write a texture image to a ppm file.
290 * \param face cube face in [0,5]
291 * \param level mipmap level
294 write_texture_image(struct gl_texture_object
*texObj
,
295 GLuint face
, GLuint level
)
297 struct gl_texture_image
*img
= texObj
->Image
[face
][level
];
299 GET_CURRENT_CONTEXT(ctx
);
300 struct gl_pixelstore_attrib store
;
304 buffer
= (GLubyte
*) malloc(img
->Width
* img
->Height
307 store
= ctx
->Pack
; /* save */
308 ctx
->Pack
= ctx
->DefaultPacking
;
310 ctx
->Driver
.GetTexImage(ctx
, texObj
->Target
, level
,
311 GL_RGBA
, GL_UNSIGNED_BYTE
,
312 buffer
, texObj
, img
);
315 _mesa_snprintf(s
, sizeof(s
), "/tmp/tex%u.l%u.f%u.ppm", texObj
->Name
, level
, face
);
317 printf(" Writing image level %u to %s\n", level
, s
);
318 write_ppm(s
, buffer
, img
->Width
, img
->Height
, 4, 0, 1, 2, GL_FALSE
);
320 ctx
->Pack
= store
; /* restore */
328 * Write renderbuffer image to a ppm file.
331 write_renderbuffer_image(const struct gl_renderbuffer
*rb
)
333 GET_CURRENT_CONTEXT(ctx
);
338 if (rb
->_BaseFormat
== GL_RGB
||
339 rb
->_BaseFormat
== GL_RGBA
) {
341 type
= GL_UNSIGNED_BYTE
;
343 else if (rb
->_BaseFormat
== GL_DEPTH_STENCIL
) {
344 format
= GL_DEPTH_STENCIL
;
345 type
= GL_UNSIGNED_INT_24_8
;
351 buffer
= (GLubyte
*) malloc(rb
->Width
* rb
->Height
* 4);
353 ctx
->Driver
.ReadPixels(ctx
, 0, 0, rb
->Width
, rb
->Height
,
354 format
, type
, &ctx
->DefaultPacking
, buffer
);
357 _mesa_snprintf(s
, sizeof(s
), "/tmp/renderbuffer%u.ppm", rb
->Name
);
359 printf(" Writing renderbuffer image to %s\n", s
);
360 write_ppm(s
, buffer
, rb
->Width
, rb
->Height
, 4, 0, 1, 2, GL_TRUE
);
366 /** How many texture images (mipmap levels, faces) to write to files */
371 static GLuint WriteImages
;
375 dump_texture(struct gl_texture_object
*texObj
, GLuint writeImages
)
377 const GLuint numFaces
= texObj
->Target
== GL_TEXTURE_CUBE_MAP
? 6 : 1;
378 GLboolean written
= GL_FALSE
;
381 printf("Texture %u\n", texObj
->Name
);
382 printf(" Target %s\n", tex_target_name(texObj
->Target
));
383 for (i
= 0; i
< MAX_TEXTURE_LEVELS
; i
++) {
384 for (j
= 0; j
< numFaces
; j
++) {
385 struct gl_texture_image
*texImg
= texObj
->Image
[j
][i
];
387 printf(" Face %u level %u: %d x %d x %d, format %s at %p\n",
389 texImg
->Width
, texImg
->Height
, texImg
->Depth
,
390 _mesa_get_format_name(texImg
->TexFormat
),
392 if (writeImages
== WRITE_ALL
||
393 (writeImages
== WRITE_ONE
&& !written
)) {
394 write_texture_image(texObj
, j
, i
);
404 * Dump a single texture.
407 _mesa_dump_texture(GLuint texture
, GLuint writeImages
)
409 GET_CURRENT_CONTEXT(ctx
);
410 struct gl_texture_object
*texObj
= _mesa_lookup_texture(ctx
, texture
);
412 dump_texture(texObj
, writeImages
);
418 dump_texture_cb(GLuint id
, void *data
, void *userData
)
420 struct gl_texture_object
*texObj
= (struct gl_texture_object
*) data
;
422 dump_texture(texObj
, WriteImages
);
427 * Print basic info about all texture objext to stdout.
428 * If dumpImages is true, write PPM of level[0] image to a file.
431 _mesa_dump_textures(GLuint writeImages
)
433 GET_CURRENT_CONTEXT(ctx
);
434 WriteImages
= writeImages
;
435 _mesa_HashWalk(ctx
->Shared
->TexObjects
, dump_texture_cb
, ctx
);
440 dump_renderbuffer(const struct gl_renderbuffer
*rb
, GLboolean writeImage
)
442 printf("Renderbuffer %u: %u x %u IntFormat = %s\n",
443 rb
->Name
, rb
->Width
, rb
->Height
,
444 _mesa_lookup_enum_by_nr(rb
->InternalFormat
));
446 write_renderbuffer_image(rb
);
452 dump_renderbuffer_cb(GLuint id
, void *data
, void *userData
)
454 const struct gl_renderbuffer
*rb
= (const struct gl_renderbuffer
*) data
;
456 dump_renderbuffer(rb
, WriteImages
);
461 * Print basic info about all renderbuffers to stdout.
462 * If dumpImages is true, write PPM of level[0] image to a file.
465 _mesa_dump_renderbuffers(GLboolean writeImages
)
467 GET_CURRENT_CONTEXT(ctx
);
468 WriteImages
= writeImages
;
469 _mesa_HashWalk(ctx
->Shared
->RenderBuffers
, dump_renderbuffer_cb
, ctx
);
475 _mesa_dump_color_buffer(const char *filename
)
477 GET_CURRENT_CONTEXT(ctx
);
478 const GLuint w
= ctx
->DrawBuffer
->Width
;
479 const GLuint h
= ctx
->DrawBuffer
->Height
;
482 buf
= (GLubyte
*) malloc(w
* h
* 4);
484 _mesa_PushClientAttrib(GL_CLIENT_PIXEL_STORE_BIT
);
485 _mesa_PixelStorei(GL_PACK_ALIGNMENT
, 1);
486 _mesa_PixelStorei(GL_PACK_INVERT_MESA
, GL_TRUE
);
488 _mesa_ReadPixels(0, 0, w
, h
, GL_RGBA
, GL_UNSIGNED_BYTE
, buf
);
490 printf("ReadBuffer %p 0x%x DrawBuffer %p 0x%x\n",
491 (void *) ctx
->ReadBuffer
->_ColorReadBuffer
,
492 ctx
->ReadBuffer
->ColorReadBuffer
,
493 (void *) ctx
->DrawBuffer
->_ColorDrawBuffers
[0],
494 ctx
->DrawBuffer
->ColorDrawBuffer
[0]);
495 printf("Writing %d x %d color buffer to %s\n", w
, h
, filename
);
496 write_ppm(filename
, buf
, w
, h
, 4, 0, 1, 2, GL_TRUE
);
498 _mesa_PopClientAttrib();
505 _mesa_dump_depth_buffer(const char *filename
)
507 GET_CURRENT_CONTEXT(ctx
);
508 const GLuint w
= ctx
->DrawBuffer
->Width
;
509 const GLuint h
= ctx
->DrawBuffer
->Height
;
514 buf
= (GLuint
*) malloc(w
* h
* 4); /* 4 bpp */
515 buf2
= (GLubyte
*) malloc(w
* h
* 3); /* 3 bpp */
517 _mesa_PushClientAttrib(GL_CLIENT_PIXEL_STORE_BIT
);
518 _mesa_PixelStorei(GL_PACK_ALIGNMENT
, 1);
519 _mesa_PixelStorei(GL_PACK_INVERT_MESA
, GL_TRUE
);
521 _mesa_ReadPixels(0, 0, w
, h
, GL_DEPTH_COMPONENT
, GL_UNSIGNED_INT
, buf
);
523 /* spread 24 bits of Z across R, G, B */
524 for (i
= 0; i
< w
* h
; i
++) {
525 buf2
[i
*3+0] = (buf
[i
] >> 24) & 0xff;
526 buf2
[i
*3+1] = (buf
[i
] >> 16) & 0xff;
527 buf2
[i
*3+2] = (buf
[i
] >> 8) & 0xff;
530 printf("Writing %d x %d depth buffer to %s\n", w
, h
, filename
);
531 write_ppm(filename
, buf2
, w
, h
, 3, 0, 1, 2, GL_TRUE
);
533 _mesa_PopClientAttrib();
541 _mesa_dump_stencil_buffer(const char *filename
)
543 GET_CURRENT_CONTEXT(ctx
);
544 const GLuint w
= ctx
->DrawBuffer
->Width
;
545 const GLuint h
= ctx
->DrawBuffer
->Height
;
550 buf
= (GLubyte
*) malloc(w
* h
); /* 1 bpp */
551 buf2
= (GLubyte
*) malloc(w
* h
* 3); /* 3 bpp */
553 _mesa_PushClientAttrib(GL_CLIENT_PIXEL_STORE_BIT
);
554 _mesa_PixelStorei(GL_PACK_ALIGNMENT
, 1);
555 _mesa_PixelStorei(GL_PACK_INVERT_MESA
, GL_TRUE
);
557 _mesa_ReadPixels(0, 0, w
, h
, GL_STENCIL_INDEX
, GL_UNSIGNED_BYTE
, buf
);
559 for (i
= 0; i
< w
* h
; i
++) {
560 buf2
[i
*3+0] = buf
[i
];
561 buf2
[i
*3+1] = (buf
[i
] & 127) * 2;
562 buf2
[i
*3+2] = (buf
[i
] - 128) * 2;
565 printf("Writing %d x %d stencil buffer to %s\n", w
, h
, filename
);
566 write_ppm(filename
, buf2
, w
, h
, 3, 0, 1, 2, GL_TRUE
);
568 _mesa_PopClientAttrib();
576 * Quick and dirty function to "print" a texture to stdout.
579 _mesa_print_texture(struct gl_context
*ctx
, const struct gl_texture_image
*img
)
581 #if CHAN_TYPE != GL_UNSIGNED_BYTE
582 _mesa_problem(NULL
, "PrintTexture not supported");
585 const GLubyte
*data
= (const GLubyte
*) img
->Data
;
588 printf("No texture data\n");
592 /* XXX add more formats or make into a new format utility function */
593 switch (img
->TexFormat
) {
597 case MESA_FORMAT_CI8
:
600 case MESA_FORMAT_AL88
:
601 case MESA_FORMAT_AL88_REV
:
604 case MESA_FORMAT_RGB888
:
605 case MESA_FORMAT_BGR888
:
608 case MESA_FORMAT_RGBA8888
:
609 case MESA_FORMAT_ARGB8888
:
613 _mesa_problem(NULL
, "error in PrintTexture\n");
617 for (i
= 0; i
< img
->Height
; i
++) {
618 for (j
= 0; j
< img
->Width
; j
++) {
620 printf("%02x ", data
[0]);
622 printf("%02x%02x ", data
[0], data
[1]);
624 printf("%02x%02x%02x ", data
[0], data
[1], data
[2]);
626 printf("%02x%02x%02x%02x ", data
[0], data
[1], data
[2], data
[3]);
627 data
+= (img
->RowStride
- img
->Width
) * c
;
629 /* XXX use img->ImageStride here */