i965: Make a helper for finding an existing shader variant.
[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 perf_debug("Recompiling tessellation evaluation shader for program %d\n",
42 prog->Id);
43
44 bool found = false;
45 const struct brw_tes_prog_key *old_key =
46 brw_find_previous_compile(&brw->cache, BRW_CACHE_TES_PROG,
47 key->program_string_id);
48
49 if (!old_key) {
50 perf_debug(" Didn't find previous compile in the shader cache for "
51 "debug\n");
52 return;
53 }
54
55 found |= brw_debug_recompile_sampler_key(brw, &old_key->tex, &key->tex);
56 found |= key_debug(brw, "inputs read", old_key->inputs_read,
57 key->inputs_read);
58 found |= key_debug(brw, "patch inputs read", old_key->patch_inputs_read,
59 key->patch_inputs_read);
60
61 if (!found) {
62 perf_debug(" Something else\n");
63 }
64 }
65
66 static bool
67 brw_codegen_tes_prog(struct brw_context *brw,
68 struct brw_program *tep,
69 struct brw_tes_prog_key *key)
70 {
71 const struct brw_compiler *compiler = brw->screen->compiler;
72 const struct gen_device_info *devinfo = &brw->screen->devinfo;
73 struct brw_stage_state *stage_state = &brw->tes.base;
74 nir_shader *nir = tep->program.nir;
75 struct brw_tes_prog_data prog_data;
76 bool start_busy = false;
77 double start_time = 0;
78
79 memset(&prog_data, 0, sizeof(prog_data));
80
81 brw_assign_common_binding_table_offsets(devinfo, &tep->program,
82 &prog_data.base.base, 0);
83
84 /* Allocate the references to the uniforms that will end up in the
85 * prog_data associated with the compiled program, and which will be freed
86 * by the state cache.
87 *
88 * Note: param_count needs to be num_uniform_components * 4, since we add
89 * padding around uniform values below vec4 size, so the worst case is that
90 * every uniform is a float which gets padded to the size of a vec4.
91 */
92 int param_count = nir->num_uniforms / 4;
93
94 prog_data.base.base.param =
95 rzalloc_array(NULL, const gl_constant_value *, param_count);
96 prog_data.base.base.pull_param =
97 rzalloc_array(NULL, const gl_constant_value *, param_count);
98 prog_data.base.base.image_param =
99 rzalloc_array(NULL, struct brw_image_param,
100 tep->program.info.num_images);
101 prog_data.base.base.nr_params = param_count;
102 prog_data.base.base.nr_image_params = tep->program.info.num_images;
103
104 brw_nir_setup_glsl_uniforms(nir, &tep->program, &prog_data.base.base,
105 compiler->scalar_stage[MESA_SHADER_TESS_EVAL]);
106
107 int st_index = -1;
108 if (unlikely(INTEL_DEBUG & DEBUG_SHADER_TIME))
109 st_index = brw_get_shader_time_index(brw, &tep->program, ST_TES, true);
110
111 if (unlikely(brw->perf_debug)) {
112 start_busy = brw->batch.last_bo && drm_intel_bo_busy(brw->batch.last_bo);
113 start_time = get_time();
114 }
115
116 struct brw_vue_map input_vue_map;
117 brw_compute_tess_vue_map(&input_vue_map, key->inputs_read,
118 key->patch_inputs_read);
119
120 void *mem_ctx = ralloc_context(NULL);
121 unsigned program_size;
122 char *error_str;
123 const unsigned *program =
124 brw_compile_tes(compiler, brw, mem_ctx, key, &input_vue_map, &prog_data,
125 nir, &tep->program, st_index, &program_size, &error_str);
126 if (program == NULL) {
127 tep->program.sh.data->LinkStatus = false;
128 ralloc_strcat(&tep->program.sh.data->InfoLog, error_str);
129
130 _mesa_problem(NULL, "Failed to compile tessellation evaluation shader: "
131 "%s\n", error_str);
132
133 ralloc_free(mem_ctx);
134 return false;
135 }
136
137 if (unlikely(brw->perf_debug)) {
138 if (tep->compiled_once) {
139 brw_tes_debug_recompile(brw, &tep->program, key);
140 }
141 if (start_busy && !drm_intel_bo_busy(brw->batch.last_bo)) {
142 perf_debug("TES compile took %.03f ms and stalled the GPU\n",
143 (get_time() - start_time) * 1000);
144 }
145 tep->compiled_once = true;
146 }
147
148 /* Scratch space is used for register spilling */
149 brw_alloc_stage_scratch(brw, stage_state,
150 prog_data.base.base.total_scratch,
151 devinfo->max_tes_threads);
152
153 brw_upload_cache(&brw->cache, BRW_CACHE_TES_PROG,
154 key, sizeof(*key),
155 program, program_size,
156 &prog_data, sizeof(prog_data),
157 &stage_state->prog_offset, &brw->tes.base.prog_data);
158 ralloc_free(mem_ctx);
159
160 return true;
161 }
162
163 void
164 brw_tes_populate_key(struct brw_context *brw,
165 struct brw_tes_prog_key *key)
166 {
167 struct brw_program *tcp = (struct brw_program *) brw->tess_ctrl_program;
168 struct brw_program *tep = (struct brw_program *) brw->tess_eval_program;
169 struct gl_program *prog = &tep->program;
170
171 uint64_t per_vertex_slots = prog->info.inputs_read;
172 uint32_t per_patch_slots = prog->info.patch_inputs_read;
173
174 memset(key, 0, sizeof(*key));
175
176 key->program_string_id = tep->id;
177
178 /* The TCS may have additional outputs which aren't read by the
179 * TES (possibly for cross-thread communication). These need to
180 * be stored in the Patch URB Entry as well.
181 */
182 if (tcp) {
183 struct gl_program *tcp_prog = &tcp->program;
184 per_vertex_slots |= tcp_prog->info.outputs_written &
185 ~(VARYING_BIT_TESS_LEVEL_INNER | VARYING_BIT_TESS_LEVEL_OUTER);
186 per_patch_slots |= tcp_prog->info.patch_outputs_written;
187 }
188
189 key->inputs_read = per_vertex_slots;
190 key->patch_inputs_read = per_patch_slots;
191
192 /* _NEW_TEXTURE */
193 brw_populate_sampler_prog_key_data(&brw->ctx, prog, &key->tex);
194 }
195
196 void
197 brw_upload_tes_prog(struct brw_context *brw)
198 {
199 struct brw_stage_state *stage_state = &brw->tes.base;
200 struct brw_tes_prog_key key;
201 /* BRW_NEW_TESS_PROGRAMS */
202 struct brw_program *tep = (struct brw_program *) brw->tess_eval_program;
203
204 if (!brw_state_dirty(brw,
205 _NEW_TEXTURE,
206 BRW_NEW_TESS_PROGRAMS))
207 return;
208
209 brw_tes_populate_key(brw, &key);
210
211 if (!brw_search_cache(&brw->cache, BRW_CACHE_TES_PROG,
212 &key, sizeof(key),
213 &stage_state->prog_offset,
214 &brw->tes.base.prog_data)) {
215 bool success = brw_codegen_tes_prog(brw, tep, &key);
216 assert(success);
217 (void)success;
218 }
219 }
220
221
222 bool
223 brw_tes_precompile(struct gl_context *ctx,
224 struct gl_shader_program *shader_prog,
225 struct gl_program *prog)
226 {
227 struct brw_context *brw = brw_context(ctx);
228 struct brw_tes_prog_key key;
229 uint32_t old_prog_offset = brw->tes.base.prog_offset;
230 struct brw_stage_prog_data *old_prog_data = brw->tes.base.prog_data;
231 bool success;
232
233 struct brw_program *btep = brw_program(prog);
234
235 memset(&key, 0, sizeof(key));
236
237 key.program_string_id = btep->id;
238 key.inputs_read = prog->nir->info->inputs_read;
239 key.patch_inputs_read = prog->nir->info->patch_inputs_read;
240
241 if (shader_prog->_LinkedShaders[MESA_SHADER_TESS_CTRL]) {
242 struct gl_program *tcp =
243 shader_prog->_LinkedShaders[MESA_SHADER_TESS_CTRL]->Program;
244 key.inputs_read |= tcp->nir->info->outputs_written &
245 ~(VARYING_BIT_TESS_LEVEL_INNER | VARYING_BIT_TESS_LEVEL_OUTER);
246 key.patch_inputs_read |= tcp->nir->info->patch_outputs_written;
247 }
248
249 brw_setup_tex_for_precompile(brw, &key.tex, prog);
250
251 success = brw_codegen_tes_prog(brw, btep, &key);
252
253 brw->tes.base.prog_offset = old_prog_offset;
254 brw->tes.base.prog_data = old_prog_data;
255
256 return success;
257 }