1/*
2 * Copyright © 2019 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included
12 * in all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20 * DEALINGS IN THE SOFTWARE.
21 */
22
23/**
24 * @file brw_debug_recompiles.c
25 */
26
27#include <stdio.h>
28
29#include "brw_compiler.h"
30
31static bool
32key_debug(const struct brw_compiler *c, void *log,
33          const char *name, int a, int b)
34{
35   if (a != b) {
36      brw_shader_perf_log(c, log, "  %s %d->%d\n", name, a, b);
37      return true;
38   }
39   return false;
40}
41
42static bool
43key_debug_float(const struct brw_compiler *c, void *log,
44                const char *name, float a, float b)
45{
46   if (a != b) {
47      brw_shader_perf_log(c, log, "  %s %f->%f\n", name, a, b);
48      return true;
49   }
50   return false;
51}
52
53#define check(name, field) \
54   key_debug(c, log, name, old_key->field, key->field)
55#define check_float(name, field) \
56   key_debug_float(c, log, name, old_key->field, key->field)
57
58static bool
59debug_sampler_recompile(const struct brw_compiler *c, void *log,
60                        const struct brw_sampler_prog_key_data *old_key,
61                        const struct brw_sampler_prog_key_data *key)
62{
63   bool found = false;
64
65   found |= check("gather channel quirk", gather_channel_quirk_mask);
66   found |= check("compressed multisample layout",
67                  compressed_multisample_layout_mask);
68   found |= check("16x msaa", msaa_16);
69   found |= check("y_uv image bound", y_uv_image_mask);
70   found |= check("y_u_v image bound", y_u_v_image_mask);
71   found |= check("yx_xuxv image bound", yx_xuxv_image_mask);
72   found |= check("xy_uxvx image bound", xy_uxvx_image_mask);
73   found |= check("ayuv image bound", ayuv_image_mask);
74   found |= check("xyuv image bound", xyuv_image_mask);
75
76   for (unsigned i = 0; i < BRW_MAX_SAMPLERS; i++) {
77      found |= check("EXT_texture_swizzle or DEPTH_TEXTURE_MODE", swizzles[i]);
78      found |= check("textureGather workarounds", gfx6_gather_wa[i]);
79      found |= check_float("scale factor", scale_factors[i]);
80   }
81
82   for (unsigned i = 0; i < 3; i++) {
83      found |= check("GL_CLAMP enabled on any texture unit", gl_clamp_mask[i]);
84   }
85
86   return found;
87}
88
89static bool
90debug_base_recompile(const struct brw_compiler *c, void *log,
91                     const struct brw_base_prog_key *old_key,
92                     const struct brw_base_prog_key *key)
93{
94   return debug_sampler_recompile(c, log, &old_key->tex, &key->tex);
95}
96
97static void
98debug_vs_recompile(const struct brw_compiler *c, void *log,
99                   const struct brw_vs_prog_key *old_key,
100                   const struct brw_vs_prog_key *key)
101{
102   bool found = debug_base_recompile(c, log, &old_key->base, &key->base);
103
104   for (unsigned i = 0; i < VERT_ATTRIB_MAX; i++) {
105      found |= check("vertex attrib w/a flags", gl_attrib_wa_flags[i]);
106   }
107
108   found |= check("legacy user clipping", nr_userclip_plane_consts);
109   found |= check("copy edgeflag", copy_edgeflag);
110   found |= check("pointcoord replace", point_coord_replace);
111   found |= check("vertex color clamping", clamp_vertex_color);
112
113   if (!found) {
114      brw_shader_perf_log(c, log, "  something else\n");
115   }
116}
117
118static void
119debug_tcs_recompile(const struct brw_compiler *c, void *log,
120                    const struct brw_tcs_prog_key *old_key,
121                    const struct brw_tcs_prog_key *key)
122{
123   bool found = debug_base_recompile(c, log, &old_key->base, &key->base);
124
125   found |= check("input vertices", input_vertices);
126   found |= check("outputs written", outputs_written);
127   found |= check("patch outputs written", patch_outputs_written);
128   found |= check("tes primitive mode", _tes_primitive_mode);
129   found |= check("quads and equal_spacing workaround", quads_workaround);
130
131   if (!found) {
132      brw_shader_perf_log(c, log, "  something else\n");
133   }
134}
135
136static void
137debug_tes_recompile(const struct brw_compiler *c, void *log,
138                    const struct brw_tes_prog_key *old_key,
139                    const struct brw_tes_prog_key *key)
140{
141   bool found = debug_base_recompile(c, log, &old_key->base, &key->base);
142
143   found |= check("inputs read", inputs_read);
144   found |= check("patch inputs read", patch_inputs_read);
145
146   if (!found) {
147      brw_shader_perf_log(c, log, "  something else\n");
148   }
149}
150
151static void
152debug_gs_recompile(const struct brw_compiler *c, void *log,
153                   const struct brw_gs_prog_key *old_key,
154                   const struct brw_gs_prog_key *key)
155{
156   bool found = debug_base_recompile(c, log, &old_key->base, &key->base);
157
158   if (!found) {
159      brw_shader_perf_log(c, log, "  something else\n");
160   }
161}
162
163static void
164debug_fs_recompile(const struct brw_compiler *c, void *log,
165                   const struct brw_wm_prog_key *old_key,
166                   const struct brw_wm_prog_key *key)
167{
168   bool found = false;
169
170   found |= check("alphatest, computed depth, depth test, or depth write",
171                  iz_lookup);
172   found |= check("depth statistics", stats_wm);
173   found |= check("flat shading", flat_shade);
174   found |= check("number of color buffers", nr_color_regions);
175   found |= check("MRT alpha test", alpha_test_replicate_alpha);
176   found |= check("alpha to coverage", alpha_to_coverage);
177   found |= check("fragment color clamping", clamp_fragment_color);
178   found |= check("per-sample interpolation", persample_interp);
179   found |= check("multisampled FBO", multisample_fbo);
180   found |= check("line smoothing", line_aa);
181   found |= check("force dual color blending", force_dual_color_blend);
182   found |= check("coherent fb fetch", coherent_fb_fetch);
183   found |= check("ignore sample mask out", ignore_sample_mask_out);
184   found |= check("coarse pixel", coarse_pixel);
185
186   found |= check("input slots valid", input_slots_valid);
187   found |= check("mrt alpha test function", alpha_test_func);
188   found |= check("mrt alpha test reference value", alpha_test_ref);
189
190   found |= debug_base_recompile(c, log, &old_key->base, &key->base);
191
192   if (!found) {
193      brw_shader_perf_log(c, log, "  something else\n");
194   }
195}
196
197static void
198debug_cs_recompile(const struct brw_compiler *c, void *log,
199                   const struct brw_cs_prog_key *old_key,
200                   const struct brw_cs_prog_key *key)
201{
202   bool found = debug_base_recompile(c, log, &old_key->base, &key->base);
203
204   if (!found) {
205      brw_shader_perf_log(c, log, "  something else\n");
206   }
207}
208
209void
210brw_debug_key_recompile(const struct brw_compiler *c, void *log,
211                        gl_shader_stage stage,
212                        const struct brw_base_prog_key *old_key,
213                        const struct brw_base_prog_key *key)
214{
215   if (!old_key) {
216      brw_shader_perf_log(c, log, "  No previous compile found...\n");
217      return;
218   }
219
220   switch (stage) {
221   case MESA_SHADER_VERTEX:
222      debug_vs_recompile(c, log, (const struct brw_vs_prog_key *)old_key,
223                                 (const struct brw_vs_prog_key *)key);
224      break;
225   case MESA_SHADER_TESS_CTRL:
226      debug_tcs_recompile(c, log, (const struct brw_tcs_prog_key *)old_key,
227                                  (const struct brw_tcs_prog_key *)key);
228      break;
229   case MESA_SHADER_TESS_EVAL:
230      debug_tes_recompile(c, log, (const struct brw_tes_prog_key *)old_key,
231                                  (const struct brw_tes_prog_key *)key);
232      break;
233   case MESA_SHADER_GEOMETRY:
234      debug_gs_recompile(c, log, (const struct brw_gs_prog_key *)old_key,
235                                 (const struct brw_gs_prog_key *)key);
236      break;
237   case MESA_SHADER_FRAGMENT:
238      debug_fs_recompile(c, log, (const struct brw_wm_prog_key *)old_key,
239                                 (const struct brw_wm_prog_key *)key);
240      break;
241   case MESA_SHADER_COMPUTE:
242      debug_cs_recompile(c, log, (const struct brw_cs_prog_key *)old_key,
243                                 (const struct brw_cs_prog_key *)key);
244      break;
245   default:
246      break;
247   }
248}
249