i965: Update brw_wm_debug_recompile() for newer key entries.
[mesa.git] / src / mesa / drivers / dri / i965 / brw_tes.c
1 /*
2 * Copyright © 2014 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 (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 */
23
24 /**
25 * \file brw_tes.c
26 *
27 * Tessellation evaluation shader state upload code.
28 */
29
30 #include "brw_context.h"
31 #include "compiler/brw_nir.h"
32 #include "brw_program.h"
33 #include "brw_state.h"
34 #include "program/prog_parameter.h"
35
36 static void
37 brw_tes_debug_recompile(struct brw_context *brw, struct gl_program *prog,
38 const struct brw_tes_prog_key *key)
39 {
40 perf_debug("Recompiling tessellation evaluation shader for program %d\n",
41 prog->Id);
42
43 bool found = false;
44 const struct brw_tes_prog_key *old_key =
45 brw_find_previous_compile(&brw->cache, BRW_CACHE_TES_PROG,
46 key->program_string_id);
47
48 if (!old_key) {
49 perf_debug(" Didn't find previous compile in the shader cache for "
50 "debug\n");
51 return;
52 }
53
54 found |= brw_debug_recompile_sampler_key(brw, &old_key->tex, &key->tex);
55 found |= key_debug(brw, "inputs read", old_key->inputs_read,
56 key->inputs_read);
57 found |= key_debug(brw, "patch inputs read", old_key->patch_inputs_read,
58 key->patch_inputs_read);
59
60 if (!found) {
61 perf_debug(" Something else\n");
62 }
63 }
64
65 static bool
66 brw_codegen_tes_prog(struct brw_context *brw,
67 struct brw_program *tep,
68 struct brw_tes_prog_key *key)
69 {
70 const struct brw_compiler *compiler = brw->screen->compiler;
71 const struct gen_device_info *devinfo = &brw->screen->devinfo;
72 struct brw_stage_state *stage_state = &brw->tes.base;
73 nir_shader *nir = tep->program.nir;
74 struct brw_tes_prog_data prog_data;
75 bool start_busy = false;
76 double start_time = 0;
77
78 memset(&prog_data, 0, sizeof(prog_data));
79
80 void *mem_ctx = ralloc_context(NULL);
81
82 brw_assign_common_binding_table_offsets(devinfo, &tep->program,
83 &prog_data.base.base, 0);
84
85 brw_nir_setup_glsl_uniforms(mem_ctx, nir, &tep->program,
86 &prog_data.base.base,
87 compiler->scalar_stage[MESA_SHADER_TESS_EVAL]);
88 brw_nir_analyze_ubo_ranges(compiler, tep->program.nir,
89 prog_data.base.base.ubo_ranges);
90
91 int st_index = -1;
92 if (unlikely(INTEL_DEBUG & DEBUG_SHADER_TIME))
93 st_index = brw_get_shader_time_index(brw, &tep->program, ST_TES, true);
94
95 if (unlikely(brw->perf_debug)) {
96 start_busy = brw->batch.last_bo && brw_bo_busy(brw->batch.last_bo);
97 start_time = get_time();
98 }
99
100 struct brw_vue_map input_vue_map;
101 brw_compute_tess_vue_map(&input_vue_map, key->inputs_read,
102 key->patch_inputs_read);
103
104 unsigned program_size;
105 char *error_str;
106 const unsigned *program =
107 brw_compile_tes(compiler, brw, mem_ctx, key, &input_vue_map, &prog_data,
108 nir, &tep->program, st_index, &program_size, &error_str);
109 if (program == NULL) {
110 tep->program.sh.data->LinkStatus = linking_failure;
111 ralloc_strcat(&tep->program.sh.data->InfoLog, error_str);
112
113 _mesa_problem(NULL, "Failed to compile tessellation evaluation shader: "
114 "%s\n", error_str);
115
116 ralloc_free(mem_ctx);
117 return false;
118 }
119
120 if (unlikely(brw->perf_debug)) {
121 if (tep->compiled_once) {
122 brw_tes_debug_recompile(brw, &tep->program, key);
123 }
124 if (start_busy && !brw_bo_busy(brw->batch.last_bo)) {
125 perf_debug("TES compile took %.03f ms and stalled the GPU\n",
126 (get_time() - start_time) * 1000);
127 }
128 tep->compiled_once = true;
129 }
130
131 /* Scratch space is used for register spilling */
132 brw_alloc_stage_scratch(brw, stage_state,
133 prog_data.base.base.total_scratch,
134 devinfo->max_tes_threads);
135
136 /* The param and pull_param arrays will be freed by the shader cache. */
137 ralloc_steal(NULL, prog_data.base.base.param);
138 ralloc_steal(NULL, prog_data.base.base.pull_param);
139 brw_upload_cache(&brw->cache, BRW_CACHE_TES_PROG,
140 key, sizeof(*key),
141 program, program_size,
142 &prog_data, sizeof(prog_data),
143 &stage_state->prog_offset, &brw->tes.base.prog_data);
144 ralloc_free(mem_ctx);
145
146 return true;
147 }
148
149 void
150 brw_tes_populate_key(struct brw_context *brw,
151 struct brw_tes_prog_key *key)
152 {
153 struct brw_program *tcp =
154 (struct brw_program *) brw->programs[MESA_SHADER_TESS_CTRL];
155 struct brw_program *tep =
156 (struct brw_program *) brw->programs[MESA_SHADER_TESS_EVAL];
157 struct gl_program *prog = &tep->program;
158
159 uint64_t per_vertex_slots = prog->info.inputs_read;
160 uint32_t per_patch_slots = prog->info.patch_inputs_read;
161
162 memset(key, 0, sizeof(*key));
163
164 key->program_string_id = tep->id;
165
166 /* The TCS may have additional outputs which aren't read by the
167 * TES (possibly for cross-thread communication). These need to
168 * be stored in the Patch URB Entry as well.
169 */
170 if (tcp) {
171 struct gl_program *tcp_prog = &tcp->program;
172 per_vertex_slots |= tcp_prog->info.outputs_written &
173 ~(VARYING_BIT_TESS_LEVEL_INNER | VARYING_BIT_TESS_LEVEL_OUTER);
174 per_patch_slots |= tcp_prog->info.patch_outputs_written;
175 }
176
177 key->inputs_read = per_vertex_slots;
178 key->patch_inputs_read = per_patch_slots;
179
180 /* _NEW_TEXTURE */
181 brw_populate_sampler_prog_key_data(&brw->ctx, prog, &key->tex);
182 }
183
184 void
185 brw_upload_tes_prog(struct brw_context *brw)
186 {
187 struct brw_stage_state *stage_state = &brw->tes.base;
188 struct brw_tes_prog_key key;
189 /* BRW_NEW_TESS_PROGRAMS */
190 struct brw_program *tep =
191 (struct brw_program *) brw->programs[MESA_SHADER_TESS_EVAL];
192
193 if (!brw_state_dirty(brw,
194 _NEW_TEXTURE,
195 BRW_NEW_TESS_PROGRAMS))
196 return;
197
198 brw_tes_populate_key(brw, &key);
199
200 if (!brw_search_cache(&brw->cache, BRW_CACHE_TES_PROG,
201 &key, sizeof(key),
202 &stage_state->prog_offset,
203 &brw->tes.base.prog_data)) {
204 bool success = brw_codegen_tes_prog(brw, tep, &key);
205 assert(success);
206 (void)success;
207 }
208 }
209
210
211 bool
212 brw_tes_precompile(struct gl_context *ctx,
213 struct gl_shader_program *shader_prog,
214 struct gl_program *prog)
215 {
216 struct brw_context *brw = brw_context(ctx);
217 struct brw_tes_prog_key key;
218 uint32_t old_prog_offset = brw->tes.base.prog_offset;
219 struct brw_stage_prog_data *old_prog_data = brw->tes.base.prog_data;
220 bool success;
221
222 struct brw_program *btep = brw_program(prog);
223
224 memset(&key, 0, sizeof(key));
225
226 key.program_string_id = btep->id;
227 key.inputs_read = prog->nir->info.inputs_read;
228 key.patch_inputs_read = prog->nir->info.patch_inputs_read;
229
230 if (shader_prog->_LinkedShaders[MESA_SHADER_TESS_CTRL]) {
231 struct gl_program *tcp =
232 shader_prog->_LinkedShaders[MESA_SHADER_TESS_CTRL]->Program;
233 key.inputs_read |= tcp->nir->info.outputs_written &
234 ~(VARYING_BIT_TESS_LEVEL_INNER | VARYING_BIT_TESS_LEVEL_OUTER);
235 key.patch_inputs_read |= tcp->nir->info.patch_outputs_written;
236 }
237
238 brw_setup_tex_for_precompile(brw, &key.tex, prog);
239
240 success = brw_codegen_tes_prog(brw, btep, &key);
241
242 brw->tes.base.prog_offset = old_prog_offset;
243 brw->tes.base.prog_data = old_prog_data;
244
245 return success;
246 }