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