i965: move compiled_once flag to brw_program
[mesa.git] / src / mesa / drivers / dri / i965 / brw_tcs.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_tcs.c
26 *
27 * Tessellation control 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 #include "nir_builder.h"
37
38 static nir_shader *
39 create_passthrough_tcs(void *mem_ctx, const struct brw_compiler *compiler,
40 const nir_shader_compiler_options *options,
41 const struct brw_tcs_prog_key *key)
42 {
43 nir_builder b;
44 nir_builder_init_simple_shader(&b, mem_ctx, MESA_SHADER_TESS_CTRL,
45 options);
46 nir_shader *nir = b.shader;
47 nir_variable *var;
48 nir_intrinsic_instr *load;
49 nir_intrinsic_instr *store;
50 nir_ssa_def *zero = nir_imm_int(&b, 0);
51 nir_ssa_def *invoc_id =
52 nir_load_system_value(&b, nir_intrinsic_load_invocation_id, 0);
53
54 nir->info->inputs_read = key->outputs_written;
55 nir->info->outputs_written = key->outputs_written;
56 nir->info->tcs.vertices_out = key->input_vertices;
57 nir->info->name = ralloc_strdup(nir, "passthrough");
58 nir->num_uniforms = 8 * sizeof(uint32_t);
59
60 var = nir_variable_create(nir, nir_var_uniform, glsl_vec4_type(), "hdr_0");
61 var->data.location = 0;
62 var = nir_variable_create(nir, nir_var_uniform, glsl_vec4_type(), "hdr_1");
63 var->data.location = 1;
64
65 /* Write the patch URB header. */
66 for (int i = 0; i <= 1; i++) {
67 load = nir_intrinsic_instr_create(nir, nir_intrinsic_load_uniform);
68 load->num_components = 4;
69 load->src[0] = nir_src_for_ssa(zero);
70 nir_ssa_dest_init(&load->instr, &load->dest, 4, 32, NULL);
71 nir_intrinsic_set_base(load, i * 4 * sizeof(uint32_t));
72 nir_builder_instr_insert(&b, &load->instr);
73
74 store = nir_intrinsic_instr_create(nir, nir_intrinsic_store_output);
75 store->num_components = 4;
76 store->src[0] = nir_src_for_ssa(&load->dest.ssa);
77 store->src[1] = nir_src_for_ssa(zero);
78 nir_intrinsic_set_base(store, VARYING_SLOT_TESS_LEVEL_INNER - i);
79 nir_intrinsic_set_write_mask(store, WRITEMASK_XYZW);
80 nir_builder_instr_insert(&b, &store->instr);
81 }
82
83 /* Copy inputs to outputs. */
84 uint64_t varyings = key->outputs_written;
85
86 while (varyings != 0) {
87 const int varying = ffsll(varyings) - 1;
88
89 load = nir_intrinsic_instr_create(nir,
90 nir_intrinsic_load_per_vertex_input);
91 load->num_components = 4;
92 load->src[0] = nir_src_for_ssa(invoc_id);
93 load->src[1] = nir_src_for_ssa(zero);
94 nir_ssa_dest_init(&load->instr, &load->dest, 4, 32, NULL);
95 nir_intrinsic_set_base(load, varying);
96 nir_builder_instr_insert(&b, &load->instr);
97
98 store = nir_intrinsic_instr_create(nir,
99 nir_intrinsic_store_per_vertex_output);
100 store->num_components = 4;
101 store->src[0] = nir_src_for_ssa(&load->dest.ssa);
102 store->src[1] = nir_src_for_ssa(invoc_id);
103 store->src[2] = nir_src_for_ssa(zero);
104 nir_intrinsic_set_base(store, varying);
105 nir_intrinsic_set_write_mask(store, WRITEMASK_XYZW);
106 nir_builder_instr_insert(&b, &store->instr);
107
108 varyings &= ~BITFIELD64_BIT(varying);
109 }
110
111 nir_validate_shader(nir);
112
113 nir = brw_preprocess_nir(compiler, nir);
114
115 return nir;
116 }
117
118 static void
119 brw_tcs_debug_recompile(struct brw_context *brw,
120 struct gl_shader_program *shader_prog,
121 const struct brw_tcs_prog_key *key)
122 {
123 struct brw_cache_item *c = NULL;
124 const struct brw_tcs_prog_key *old_key = NULL;
125 bool found = false;
126
127 perf_debug("Recompiling tessellation control shader for program %d\n",
128 shader_prog->Name);
129
130 for (unsigned int i = 0; i < brw->cache.size; i++) {
131 for (c = brw->cache.items[i]; c; c = c->next) {
132 if (c->cache_id == BRW_CACHE_TCS_PROG) {
133 old_key = c->key;
134
135 if (old_key->program_string_id == key->program_string_id)
136 break;
137 }
138 }
139 if (c)
140 break;
141 }
142
143 if (!c) {
144 perf_debug(" Didn't find previous compile in the shader cache for "
145 "debug\n");
146 return;
147 }
148
149 found |= key_debug(brw, "input vertices", old_key->input_vertices,
150 key->input_vertices);
151 found |= key_debug(brw, "outputs written", old_key->outputs_written,
152 key->outputs_written);
153 found |= key_debug(brw, "patch outputs written", old_key->patch_outputs_written,
154 key->patch_outputs_written);
155 found |= key_debug(brw, "TES primitive mode", old_key->tes_primitive_mode,
156 key->tes_primitive_mode);
157 found |= key_debug(brw, "quads and equal_spacing workaround",
158 old_key->quads_workaround, key->quads_workaround);
159 found |= brw_debug_recompile_sampler_key(brw, &old_key->tex, &key->tex);
160
161 if (!found) {
162 perf_debug(" Something else\n");
163 }
164 }
165
166 static bool
167 brw_codegen_tcs_prog(struct brw_context *brw,
168 struct gl_shader_program *shader_prog,
169 struct brw_program *tcp,
170 struct brw_tcs_prog_key *key)
171 {
172 struct gl_context *ctx = &brw->ctx;
173 const struct brw_compiler *compiler = brw->screen->compiler;
174 const struct gen_device_info *devinfo = compiler->devinfo;
175 struct brw_stage_state *stage_state = &brw->tcs.base;
176 nir_shader *nir;
177 struct brw_tcs_prog_data prog_data;
178 bool start_busy = false;
179 double start_time = 0;
180
181 void *mem_ctx = ralloc_context(NULL);
182 if (tcp) {
183 nir = tcp->program.nir;
184 } else {
185 /* Create a dummy nir_shader. We won't actually use NIR code to
186 * generate assembly (it's easier to generate assembly directly),
187 * but the whole compiler assumes one of these exists.
188 */
189 const nir_shader_compiler_options *options =
190 ctx->Const.ShaderCompilerOptions[MESA_SHADER_TESS_CTRL].NirOptions;
191 nir = create_passthrough_tcs(mem_ctx, compiler, options, key);
192 }
193
194 memset(&prog_data, 0, sizeof(prog_data));
195
196 /* Allocate the references to the uniforms that will end up in the
197 * prog_data associated with the compiled program, and which will be freed
198 * by the state cache.
199 *
200 * Note: param_count needs to be num_uniform_components * 4, since we add
201 * padding around uniform values below vec4 size, so the worst case is that
202 * every uniform is a float which gets padded to the size of a vec4.
203 */
204 int param_count = nir->num_uniforms / 4;
205
206 prog_data.base.base.param =
207 rzalloc_array(NULL, const gl_constant_value *, param_count);
208 prog_data.base.base.pull_param =
209 rzalloc_array(NULL, const gl_constant_value *, param_count);
210 prog_data.base.base.nr_params = param_count;
211
212 if (tcp) {
213 brw_assign_common_binding_table_offsets(MESA_SHADER_TESS_CTRL, devinfo,
214 shader_prog, &tcp->program,
215 &prog_data.base.base, 0);
216
217 prog_data.base.base.image_param =
218 rzalloc_array(NULL, struct brw_image_param,
219 tcp->program.info.num_images);
220 prog_data.base.base.nr_image_params = tcp->program.info.num_images;
221
222 brw_nir_setup_glsl_uniforms(nir, shader_prog, &tcp->program,
223 &prog_data.base.base,
224 compiler->scalar_stage[MESA_SHADER_TESS_CTRL]);
225 } else {
226 /* Upload the Patch URB Header as the first two uniforms.
227 * Do the annoying scrambling so the shader doesn't have to.
228 */
229 const float **param = (const float **) prog_data.base.base.param;
230 static float zero = 0.0f;
231 for (int i = 0; i < 8; i++)
232 param[i] = &zero;
233
234 if (key->tes_primitive_mode == GL_QUADS) {
235 for (int i = 0; i < 4; i++)
236 param[7 - i] = &ctx->TessCtrlProgram.patch_default_outer_level[i];
237
238 param[3] = &ctx->TessCtrlProgram.patch_default_inner_level[0];
239 param[2] = &ctx->TessCtrlProgram.patch_default_inner_level[1];
240 } else if (key->tes_primitive_mode == GL_TRIANGLES) {
241 for (int i = 0; i < 3; i++)
242 param[7 - i] = &ctx->TessCtrlProgram.patch_default_outer_level[i];
243
244 param[4] = &ctx->TessCtrlProgram.patch_default_inner_level[0];
245 } else {
246 assert(key->tes_primitive_mode == GL_ISOLINES);
247 param[7] = &ctx->TessCtrlProgram.patch_default_outer_level[1];
248 param[6] = &ctx->TessCtrlProgram.patch_default_outer_level[0];
249 }
250 }
251
252 int st_index = -1;
253 if (unlikely(INTEL_DEBUG & DEBUG_SHADER_TIME))
254 st_index = brw_get_shader_time_index(brw, shader_prog, NULL, ST_TCS);
255
256 if (unlikely(brw->perf_debug)) {
257 start_busy = brw->batch.last_bo && drm_intel_bo_busy(brw->batch.last_bo);
258 start_time = get_time();
259 }
260
261 unsigned program_size;
262 char *error_str;
263 const unsigned *program =
264 brw_compile_tcs(compiler, brw, mem_ctx, key, &prog_data, nir, st_index,
265 &program_size, &error_str);
266 if (program == NULL) {
267 if (shader_prog) {
268 shader_prog->data->LinkStatus = false;
269 ralloc_strcat(&shader_prog->data->InfoLog, error_str);
270 }
271
272 _mesa_problem(NULL, "Failed to compile tessellation control shader: "
273 "%s\n", error_str);
274
275 ralloc_free(mem_ctx);
276 return false;
277 }
278
279 if (unlikely(brw->perf_debug)) {
280 if (tcp) {
281 if (tcp->compiled_once) {
282 brw_tcs_debug_recompile(brw, shader_prog, key);
283 }
284 tcp->compiled_once = true;
285 }
286
287 if (start_busy && !drm_intel_bo_busy(brw->batch.last_bo)) {
288 perf_debug("TCS compile took %.03f ms and stalled the GPU\n",
289 (get_time() - start_time) * 1000);
290 }
291 }
292
293 /* Scratch space is used for register spilling */
294 brw_alloc_stage_scratch(brw, stage_state,
295 prog_data.base.base.total_scratch,
296 devinfo->max_tcs_threads);
297
298 brw_upload_cache(&brw->cache, BRW_CACHE_TCS_PROG,
299 key, sizeof(*key),
300 program, program_size,
301 &prog_data, sizeof(prog_data),
302 &stage_state->prog_offset, &brw->tcs.base.prog_data);
303 ralloc_free(mem_ctx);
304
305 return true;
306 }
307
308 void
309 brw_tcs_populate_key(struct brw_context *brw,
310 struct brw_tcs_prog_key *key)
311 {
312 struct brw_program *tcp = (struct brw_program *) brw->tess_ctrl_program;
313 struct brw_program *tep = (struct brw_program *) brw->tess_eval_program;
314 struct gl_program *tes_prog = &tep->program;
315
316 uint64_t per_vertex_slots = tes_prog->info.inputs_read;
317 uint32_t per_patch_slots = tes_prog->info.patch_inputs_read;
318
319 memset(key, 0, sizeof(*key));
320
321 if (tcp) {
322 struct gl_program *prog = &tcp->program;
323 per_vertex_slots |= prog->info.outputs_written;
324 per_patch_slots |= prog->info.patch_outputs_written;
325 }
326
327 if (brw->gen < 8 || !tcp)
328 key->input_vertices = brw->ctx.TessCtrlProgram.patch_vertices;
329 key->outputs_written = per_vertex_slots;
330 key->patch_outputs_written = per_patch_slots;
331
332 /* We need to specialize our code generation for tessellation levels
333 * based on the domain the DS is expecting to tessellate.
334 */
335 key->tes_primitive_mode = tep->program.info.tes.primitive_mode;
336 key->quads_workaround = brw->gen < 9 &&
337 tep->program.info.tes.primitive_mode == GL_QUADS &&
338 tep->program.info.tes.spacing == GL_EQUAL;
339
340 if (tcp) {
341 key->program_string_id = tcp->id;
342
343 /* _NEW_TEXTURE */
344 brw_populate_sampler_prog_key_data(&brw->ctx, &tcp->program, &key->tex);
345 }
346 }
347
348 void
349 brw_upload_tcs_prog(struct brw_context *brw)
350 {
351 struct gl_shader_program **current = brw->ctx._Shader->CurrentProgram;
352 struct brw_stage_state *stage_state = &brw->tcs.base;
353 struct brw_tcs_prog_key key;
354 /* BRW_NEW_TESS_PROGRAMS */
355 struct brw_program *tcp = (struct brw_program *) brw->tess_ctrl_program;
356 MAYBE_UNUSED struct brw_program *tep =
357 (struct brw_program *) brw->tess_eval_program;
358 assert(tep);
359
360 if (!brw_state_dirty(brw,
361 _NEW_TEXTURE,
362 BRW_NEW_PATCH_PRIMITIVE |
363 BRW_NEW_TESS_PROGRAMS))
364 return;
365
366 brw_tcs_populate_key(brw, &key);
367
368 if (!brw_search_cache(&brw->cache, BRW_CACHE_TCS_PROG,
369 &key, sizeof(key),
370 &stage_state->prog_offset,
371 &brw->tcs.base.prog_data)) {
372 bool success = brw_codegen_tcs_prog(brw, current[MESA_SHADER_TESS_CTRL],
373 tcp, &key);
374 assert(success);
375 (void)success;
376 }
377 }
378
379
380 bool
381 brw_tcs_precompile(struct gl_context *ctx,
382 struct gl_shader_program *shader_prog,
383 struct gl_program *prog)
384 {
385 struct brw_context *brw = brw_context(ctx);
386 struct brw_tcs_prog_key key;
387 uint32_t old_prog_offset = brw->tcs.base.prog_offset;
388 struct brw_stage_prog_data *old_prog_data = brw->tcs.base.prog_data;
389 bool success;
390
391 struct brw_program *btcp = brw_program(prog);
392 const struct gl_linked_shader *tes =
393 shader_prog->_LinkedShaders[MESA_SHADER_TESS_EVAL];
394
395 memset(&key, 0, sizeof(key));
396
397 key.program_string_id = btcp->id;
398 brw_setup_tex_for_precompile(brw, &key.tex, prog);
399
400 /* Guess that the input and output patches have the same dimensionality. */
401 if (brw->gen < 8) {
402 key.input_vertices = shader_prog->
403 _LinkedShaders[MESA_SHADER_TESS_CTRL]->info.TessCtrl.VerticesOut;
404 }
405
406 if (tes) {
407 key.tes_primitive_mode = tes->info.TessEval.PrimitiveMode;
408 key.quads_workaround = brw->gen < 9 &&
409 tes->info.TessEval.PrimitiveMode == GL_QUADS &&
410 tes->info.TessEval.Spacing == GL_EQUAL;
411 } else {
412 key.tes_primitive_mode = GL_TRIANGLES;
413 }
414
415 key.outputs_written = prog->nir->info->outputs_written;
416 key.patch_outputs_written = prog->nir->info->patch_outputs_written;
417
418 success = brw_codegen_tcs_prog(brw, shader_prog, btcp, &key);
419
420 brw->tcs.base.prog_offset = old_prog_offset;
421 brw->tcs.base.prog_data = old_prog_data;
422
423 return success;
424 }