i965: pass gl_program to the brw_*_debug_recompile() functions
[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 "brw_nir.h"
32 #include "brw_program.h"
33 #include "brw_shader.h"
34 #include "brw_state.h"
35 #include "program/prog_parameter.h"
36
37 static void
38 brw_tes_debug_recompile(struct brw_context *brw, struct gl_program *prog,
39 const struct brw_tes_prog_key *key)
40 {
41 struct brw_cache_item *c = NULL;
42 const struct brw_tes_prog_key *old_key = NULL;
43 bool found = false;
44
45 perf_debug("Recompiling tessellation evaluation shader for program %d\n",
46 prog->Id);
47
48 for (unsigned int i = 0; i < brw->cache.size; i++) {
49 for (c = brw->cache.items[i]; c; c = c->next) {
50 if (c->cache_id == BRW_CACHE_TES_PROG) {
51 old_key = c->key;
52
53 if (old_key->program_string_id == key->program_string_id)
54 break;
55 }
56 }
57 if (c)
58 break;
59 }
60
61 if (!c) {
62 perf_debug(" Didn't find previous compile in the shader cache for "
63 "debug\n");
64 return;
65 }
66
67 found |= brw_debug_recompile_sampler_key(brw, &old_key->tex, &key->tex);
68 found |= key_debug(brw, "inputs read", old_key->inputs_read,
69 key->inputs_read);
70 found |= key_debug(brw, "patch inputs read", old_key->patch_inputs_read,
71 key->patch_inputs_read);
72
73 if (!found) {
74 perf_debug(" Something else\n");
75 }
76 }
77
78 static bool
79 brw_codegen_tes_prog(struct brw_context *brw,
80 struct gl_shader_program *shader_prog,
81 struct brw_program *tep,
82 struct brw_tes_prog_key *key)
83 {
84 const struct brw_compiler *compiler = brw->screen->compiler;
85 const struct gen_device_info *devinfo = &brw->screen->devinfo;
86 struct brw_stage_state *stage_state = &brw->tes.base;
87 nir_shader *nir = tep->program.nir;
88 struct brw_tes_prog_data prog_data;
89 bool start_busy = false;
90 double start_time = 0;
91
92 memset(&prog_data, 0, sizeof(prog_data));
93
94 brw_assign_common_binding_table_offsets(MESA_SHADER_TESS_EVAL, devinfo,
95 shader_prog, &tep->program,
96 &prog_data.base.base, 0);
97
98 switch (tep->program.info.tes.spacing) {
99 case GL_EQUAL:
100 prog_data.partitioning = BRW_TESS_PARTITIONING_INTEGER;
101 break;
102 case GL_FRACTIONAL_ODD:
103 prog_data.partitioning = BRW_TESS_PARTITIONING_ODD_FRACTIONAL;
104 break;
105 case GL_FRACTIONAL_EVEN:
106 prog_data.partitioning = BRW_TESS_PARTITIONING_EVEN_FRACTIONAL;
107 break;
108 default:
109 unreachable("invalid domain shader spacing");
110 }
111
112 switch (tep->program.info.tes.primitive_mode) {
113 case GL_QUADS:
114 prog_data.domain = BRW_TESS_DOMAIN_QUAD;
115 break;
116 case GL_TRIANGLES:
117 prog_data.domain = BRW_TESS_DOMAIN_TRI;
118 break;
119 case GL_ISOLINES:
120 prog_data.domain = BRW_TESS_DOMAIN_ISOLINE;
121 break;
122 default:
123 unreachable("invalid domain shader primitive mode");
124 }
125
126 if (tep->program.info.tes.point_mode) {
127 prog_data.output_topology = BRW_TESS_OUTPUT_TOPOLOGY_POINT;
128 } else if (tep->program.info.tes.primitive_mode == GL_ISOLINES) {
129 prog_data.output_topology = BRW_TESS_OUTPUT_TOPOLOGY_LINE;
130 } else {
131 /* Hardware winding order is backwards from OpenGL */
132 switch (tep->program.info.tes.vertex_order) {
133 case GL_CCW:
134 prog_data.output_topology = BRW_TESS_OUTPUT_TOPOLOGY_TRI_CW;
135 break;
136 case GL_CW:
137 prog_data.output_topology = BRW_TESS_OUTPUT_TOPOLOGY_TRI_CCW;
138 break;
139 default:
140 unreachable("invalid domain shader vertex order");
141 }
142 }
143
144 /* Allocate the references to the uniforms that will end up in the
145 * prog_data associated with the compiled program, and which will be freed
146 * by the state cache.
147 *
148 * Note: param_count needs to be num_uniform_components * 4, since we add
149 * padding around uniform values below vec4 size, so the worst case is that
150 * every uniform is a float which gets padded to the size of a vec4.
151 */
152 int param_count = nir->num_uniforms / 4;
153
154 prog_data.base.base.param =
155 rzalloc_array(NULL, const gl_constant_value *, param_count);
156 prog_data.base.base.pull_param =
157 rzalloc_array(NULL, const gl_constant_value *, param_count);
158 prog_data.base.base.image_param =
159 rzalloc_array(NULL, struct brw_image_param,
160 tep->program.info.num_images);
161 prog_data.base.base.nr_params = param_count;
162 prog_data.base.base.nr_image_params = tep->program.info.num_images;
163
164 brw_nir_setup_glsl_uniforms(nir, shader_prog, &tep->program,
165 &prog_data.base.base,
166 compiler->scalar_stage[MESA_SHADER_TESS_EVAL]);
167
168 int st_index = -1;
169 if (unlikely(INTEL_DEBUG & DEBUG_SHADER_TIME))
170 st_index = brw_get_shader_time_index(brw, &tep->program, ST_TES, true);
171
172 if (unlikely(brw->perf_debug)) {
173 start_busy = brw->batch.last_bo && drm_intel_bo_busy(brw->batch.last_bo);
174 start_time = get_time();
175 }
176
177 void *mem_ctx = ralloc_context(NULL);
178 unsigned program_size;
179 char *error_str;
180 const unsigned *program =
181 brw_compile_tes(compiler, brw, mem_ctx, key, &prog_data, nir,
182 shader_prog, st_index, &program_size, &error_str);
183 if (program == NULL) {
184 tep->program.sh.data->LinkStatus = false;
185 ralloc_strcat(&tep->program.sh.data->InfoLog, error_str);
186
187 _mesa_problem(NULL, "Failed to compile tessellation evaluation shader: "
188 "%s\n", error_str);
189
190 ralloc_free(mem_ctx);
191 return false;
192 }
193
194 if (unlikely(brw->perf_debug)) {
195 if (tep->compiled_once) {
196 brw_tes_debug_recompile(brw, &tep->program, key);
197 }
198 if (start_busy && !drm_intel_bo_busy(brw->batch.last_bo)) {
199 perf_debug("TES compile took %.03f ms and stalled the GPU\n",
200 (get_time() - start_time) * 1000);
201 }
202 tep->compiled_once = true;
203 }
204
205 /* Scratch space is used for register spilling */
206 brw_alloc_stage_scratch(brw, stage_state,
207 prog_data.base.base.total_scratch,
208 devinfo->max_tes_threads);
209
210 brw_upload_cache(&brw->cache, BRW_CACHE_TES_PROG,
211 key, sizeof(*key),
212 program, program_size,
213 &prog_data, sizeof(prog_data),
214 &stage_state->prog_offset, &brw->tes.base.prog_data);
215 ralloc_free(mem_ctx);
216
217 return true;
218 }
219
220 void
221 brw_tes_populate_key(struct brw_context *brw,
222 struct brw_tes_prog_key *key)
223 {
224 struct brw_program *tcp = (struct brw_program *) brw->tess_ctrl_program;
225 struct brw_program *tep = (struct brw_program *) brw->tess_eval_program;
226 struct gl_program *prog = &tep->program;
227
228 uint64_t per_vertex_slots = prog->info.inputs_read;
229 uint32_t per_patch_slots = prog->info.patch_inputs_read;
230
231 memset(key, 0, sizeof(*key));
232
233 key->program_string_id = tep->id;
234
235 /* The TCS may have additional outputs which aren't read by the
236 * TES (possibly for cross-thread communication). These need to
237 * be stored in the Patch URB Entry as well.
238 */
239 if (tcp) {
240 struct gl_program *tcp_prog = &tcp->program;
241 per_vertex_slots |= tcp_prog->info.outputs_written;
242 per_patch_slots |= tcp_prog->info.patch_outputs_written;
243 }
244
245 /* Ignore gl_TessLevelInner/Outer - we treat them as system values,
246 * not inputs, and they're always present in the URB entry regardless
247 * of whether or not we read them.
248 */
249 key->inputs_read = per_vertex_slots &
250 ~(VARYING_BIT_TESS_LEVEL_INNER | VARYING_BIT_TESS_LEVEL_OUTER);
251 key->patch_inputs_read = per_patch_slots;
252
253 /* _NEW_TEXTURE */
254 brw_populate_sampler_prog_key_data(&brw->ctx, prog, &key->tex);
255 }
256
257 void
258 brw_upload_tes_prog(struct brw_context *brw)
259 {
260 struct gl_shader_program **current = brw->ctx._Shader->CurrentProgram;
261 struct brw_stage_state *stage_state = &brw->tes.base;
262 struct brw_tes_prog_key key;
263 /* BRW_NEW_TESS_PROGRAMS */
264 struct brw_program *tep = (struct brw_program *) brw->tess_eval_program;
265
266 if (!brw_state_dirty(brw,
267 _NEW_TEXTURE,
268 BRW_NEW_TESS_PROGRAMS))
269 return;
270
271 brw_tes_populate_key(brw, &key);
272
273 if (!brw_search_cache(&brw->cache, BRW_CACHE_TES_PROG,
274 &key, sizeof(key),
275 &stage_state->prog_offset,
276 &brw->tes.base.prog_data)) {
277 bool success = brw_codegen_tes_prog(brw, current[MESA_SHADER_TESS_EVAL],
278 tep, &key);
279 assert(success);
280 (void)success;
281 }
282 }
283
284
285 bool
286 brw_tes_precompile(struct gl_context *ctx,
287 struct gl_shader_program *shader_prog,
288 struct gl_program *prog)
289 {
290 struct brw_context *brw = brw_context(ctx);
291 struct brw_tes_prog_key key;
292 uint32_t old_prog_offset = brw->tes.base.prog_offset;
293 struct brw_stage_prog_data *old_prog_data = brw->tes.base.prog_data;
294 bool success;
295
296 struct brw_program *btep = brw_program(prog);
297
298 memset(&key, 0, sizeof(key));
299
300 key.program_string_id = btep->id;
301 key.inputs_read = prog->nir->info->inputs_read;
302 key.patch_inputs_read = prog->nir->info->patch_inputs_read;
303
304 if (shader_prog->_LinkedShaders[MESA_SHADER_TESS_CTRL]) {
305 struct gl_program *tcp =
306 shader_prog->_LinkedShaders[MESA_SHADER_TESS_CTRL]->Program;
307 key.inputs_read |= tcp->nir->info->outputs_written;
308 key.patch_inputs_read |= tcp->nir->info->patch_outputs_written;
309 }
310
311 /* Ignore gl_TessLevelInner/Outer - they're system values. */
312 key.inputs_read &= ~(VARYING_BIT_TESS_LEVEL_INNER |
313 VARYING_BIT_TESS_LEVEL_OUTER);
314
315 brw_setup_tex_for_precompile(brw, &key.tex, prog);
316
317 success = brw_codegen_tes_prog(brw, shader_prog, btep, &key);
318
319 brw->tes.base.prog_offset = old_prog_offset;
320 brw->tes.base.prog_data = old_prog_data;
321
322 return success;
323 }