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