util: Move DRI parse_debug_string() to util
[mesa.git] / src / mesa / drivers / dri / i965 / intel_debug.c
1 /*
2 * Copyright 2003 VMware, Inc.
3 * Copyright © 2006 Intel Corporation
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22 * IN THE SOFTWARE.
23 */
24
25 /**
26 * \file intel_debug.c
27 *
28 * Support for the INTEL_DEBUG environment variable, along with other
29 * miscellaneous debugging code.
30 */
31
32 #include "brw_context.h"
33 #include "intel_debug.h"
34 #include "utils.h"
35 #include "util/u_atomic.h" /* for p_atomic_cmpxchg */
36 #include "util/debug.h"
37
38 uint64_t INTEL_DEBUG = 0;
39
40 static const struct debug_control debug_control[] = {
41 { "tex", DEBUG_TEXTURE},
42 { "state", DEBUG_STATE},
43 { "blit", DEBUG_BLIT},
44 { "mip", DEBUG_MIPTREE},
45 { "fall", DEBUG_PERF},
46 { "perf", DEBUG_PERF},
47 { "perfmon", DEBUG_PERFMON},
48 { "bat", DEBUG_BATCH},
49 { "pix", DEBUG_PIXEL},
50 { "buf", DEBUG_BUFMGR},
51 { "fbo", DEBUG_FBO},
52 { "fs", DEBUG_WM },
53 { "gs", DEBUG_GS},
54 { "sync", DEBUG_SYNC},
55 { "prim", DEBUG_PRIMS },
56 { "vert", DEBUG_VERTS },
57 { "dri", DEBUG_DRI },
58 { "sf", DEBUG_SF },
59 { "stats", DEBUG_STATS },
60 { "wm", DEBUG_WM },
61 { "urb", DEBUG_URB },
62 { "vs", DEBUG_VS },
63 { "clip", DEBUG_CLIP },
64 { "aub", DEBUG_AUB },
65 { "shader_time", DEBUG_SHADER_TIME },
66 { "no16", DEBUG_NO16 },
67 { "blorp", DEBUG_BLORP },
68 { "nodualobj", DEBUG_NO_DUAL_OBJECT_GS },
69 { "optimizer", DEBUG_OPTIMIZER },
70 { "ann", DEBUG_ANNOTATION },
71 { "no8", DEBUG_NO8 },
72 { "vec4", DEBUG_VEC4VS },
73 { "spill_fs", DEBUG_SPILL_FS },
74 { "spill_vec4", DEBUG_SPILL_VEC4 },
75 { "cs", DEBUG_CS },
76 { NULL, 0 }
77 };
78
79 uint64_t
80 intel_debug_flag_for_shader_stage(gl_shader_stage stage)
81 {
82 uint64_t flags[] = {
83 [MESA_SHADER_VERTEX] = DEBUG_VS,
84 [MESA_SHADER_TESS_CTRL] = 0,
85 [MESA_SHADER_TESS_EVAL] = 0,
86 [MESA_SHADER_GEOMETRY] = DEBUG_GS,
87 [MESA_SHADER_FRAGMENT] = DEBUG_WM,
88 [MESA_SHADER_COMPUTE] = DEBUG_CS,
89 };
90 STATIC_ASSERT(MESA_SHADER_STAGES == 6);
91 return flags[stage];
92 }
93
94 void
95 brw_process_intel_debug_variable(struct intel_screen *screen)
96 {
97 uint64_t intel_debug = parse_debug_string(getenv("INTEL_DEBUG"), debug_control);
98 (void) p_atomic_cmpxchg(&INTEL_DEBUG, 0, intel_debug);
99
100 if (INTEL_DEBUG & DEBUG_BUFMGR)
101 dri_bufmgr_set_debug(screen->bufmgr, true);
102
103 if ((INTEL_DEBUG & DEBUG_SHADER_TIME) && screen->devinfo->gen < 7) {
104 fprintf(stderr,
105 "shader_time debugging requires gen7 (Ivybridge) or better.\n");
106 INTEL_DEBUG &= ~DEBUG_SHADER_TIME;
107 }
108
109 if (INTEL_DEBUG & DEBUG_AUB)
110 drm_intel_bufmgr_gem_set_aub_dump(screen->bufmgr, true);
111 }
112
113 /**
114 * Reads an environment variable and interprets its value as a boolean.
115 *
116 * Recognizes 0/false/no and 1/true/yes. Other values result in the default value.
117 */
118 bool
119 brw_env_var_as_boolean(const char *var_name, bool default_value)
120 {
121 const char *str = getenv(var_name);
122 if (str == NULL)
123 return default_value;
124
125 if (strcmp(str, "1") == 0 ||
126 strcasecmp(str, "true") == 0 ||
127 strcasecmp(str, "yes") == 0) {
128 return true;
129 } else if (strcmp(str, "0") == 0 ||
130 strcasecmp(str, "false") == 0 ||
131 strcasecmp(str, "no") == 0) {
132 return false;
133 } else {
134 return default_value;
135 }
136 }