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