1c4d79886388e0aaedb47d5f2345c3e3b2326f94
[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 "compiler/brw_nir.h"
32 #include "brw_program.h"
33 #include "brw_state.h"
34 #include "program/prog_parameter.h"
35 #include "nir_builder.h"
36
37 static bool
38 brw_codegen_tcs_prog(struct brw_context *brw, struct brw_program *tcp,
39 struct brw_program *tep, struct brw_tcs_prog_key *key)
40 {
41 struct gl_context *ctx = &brw->ctx;
42 const struct brw_compiler *compiler = brw->screen->compiler;
43 const struct gen_device_info *devinfo = compiler->devinfo;
44 struct brw_stage_state *stage_state = &brw->tcs.base;
45 nir_shader *nir;
46 struct brw_tcs_prog_data prog_data;
47 bool start_busy = false;
48 double start_time = 0;
49
50 void *mem_ctx = ralloc_context(NULL);
51 if (tcp) {
52 nir = nir_shader_clone(mem_ctx, tcp->program.nir);
53 } else {
54 const nir_shader_compiler_options *options =
55 ctx->Const.ShaderCompilerOptions[MESA_SHADER_TESS_CTRL].NirOptions;
56 nir = brw_nir_create_passthrough_tcs(mem_ctx, compiler, options, key);
57 }
58
59 memset(&prog_data, 0, sizeof(prog_data));
60
61 if (tcp) {
62 brw_assign_common_binding_table_offsets(devinfo, &tcp->program,
63 &prog_data.base.base, 0);
64
65 brw_nir_setup_glsl_uniforms(mem_ctx, nir, &tcp->program,
66 &prog_data.base.base,
67 compiler->scalar_stage[MESA_SHADER_TESS_CTRL]);
68 brw_nir_analyze_ubo_ranges(compiler, nir, NULL,
69 prog_data.base.base.ubo_ranges);
70 } else {
71 /* Upload the Patch URB Header as the first two uniforms.
72 * Do the annoying scrambling so the shader doesn't have to.
73 */
74 assert(nir->num_uniforms == 32);
75 prog_data.base.base.param = rzalloc_array(mem_ctx, uint32_t, 8);
76 prog_data.base.base.nr_params = 8;
77
78 uint32_t *param = prog_data.base.base.param;
79 for (int i = 0; i < 8; i++)
80 param[i] = BRW_PARAM_BUILTIN_ZERO;
81
82 if (key->tes_primitive_mode == GL_QUADS) {
83 for (int i = 0; i < 4; i++)
84 param[7 - i] = BRW_PARAM_BUILTIN_TESS_LEVEL_OUTER_X + i;
85
86 param[3] = BRW_PARAM_BUILTIN_TESS_LEVEL_INNER_X;
87 param[2] = BRW_PARAM_BUILTIN_TESS_LEVEL_INNER_Y;
88 } else if (key->tes_primitive_mode == GL_TRIANGLES) {
89 for (int i = 0; i < 3; i++)
90 param[7 - i] = BRW_PARAM_BUILTIN_TESS_LEVEL_OUTER_X + i;
91
92 param[4] = BRW_PARAM_BUILTIN_TESS_LEVEL_INNER_X;
93 } else {
94 assert(key->tes_primitive_mode == GL_ISOLINES);
95 param[7] = BRW_PARAM_BUILTIN_TESS_LEVEL_OUTER_Y;
96 param[6] = BRW_PARAM_BUILTIN_TESS_LEVEL_OUTER_X;
97 }
98 }
99
100 int st_index = -1;
101 if (unlikely((INTEL_DEBUG & DEBUG_SHADER_TIME) && tep))
102 st_index = brw_get_shader_time_index(brw, &tep->program, ST_TCS, true);
103
104 if (unlikely(brw->perf_debug)) {
105 start_busy = brw->batch.last_bo && brw_bo_busy(brw->batch.last_bo);
106 start_time = get_time();
107 }
108
109 char *error_str;
110 const unsigned *program =
111 brw_compile_tcs(compiler, brw, mem_ctx, key, &prog_data, nir, st_index,
112 &error_str);
113 if (program == NULL) {
114 if (tep) {
115 tep->program.sh.data->LinkStatus = LINKING_FAILURE;
116 ralloc_strcat(&tep->program.sh.data->InfoLog, error_str);
117 }
118
119 _mesa_problem(NULL, "Failed to compile tessellation control shader: "
120 "%s\n", error_str);
121
122 ralloc_free(mem_ctx);
123 return false;
124 }
125
126 if (unlikely(brw->perf_debug)) {
127 if (tcp) {
128 if (tcp->compiled_once) {
129 brw_debug_recompile(brw, MESA_SHADER_TESS_CTRL, tcp->program.Id,
130 key->program_string_id, key);
131 }
132 tcp->compiled_once = true;
133 }
134
135 if (start_busy && !brw_bo_busy(brw->batch.last_bo)) {
136 perf_debug("TCS compile took %.03f ms and stalled the GPU\n",
137 (get_time() - start_time) * 1000);
138 }
139 }
140
141 /* Scratch space is used for register spilling */
142 brw_alloc_stage_scratch(brw, stage_state,
143 prog_data.base.base.total_scratch);
144
145 /* The param and pull_param arrays will be freed by the shader cache. */
146 ralloc_steal(NULL, prog_data.base.base.param);
147 ralloc_steal(NULL, prog_data.base.base.pull_param);
148 brw_upload_cache(&brw->cache, BRW_CACHE_TCS_PROG,
149 key, sizeof(*key),
150 program, prog_data.base.base.program_size,
151 &prog_data, sizeof(prog_data),
152 &stage_state->prog_offset, &brw->tcs.base.prog_data);
153 ralloc_free(mem_ctx);
154
155 return true;
156 }
157
158 void
159 brw_tcs_populate_key(struct brw_context *brw,
160 struct brw_tcs_prog_key *key)
161 {
162 const struct gen_device_info *devinfo = &brw->screen->devinfo;
163 struct brw_program *tcp =
164 (struct brw_program *) brw->programs[MESA_SHADER_TESS_CTRL];
165 struct brw_program *tep =
166 (struct brw_program *) brw->programs[MESA_SHADER_TESS_EVAL];
167 struct gl_program *tes_prog = &tep->program;
168
169 uint64_t per_vertex_slots = tes_prog->info.inputs_read;
170 uint32_t per_patch_slots = tes_prog->info.patch_inputs_read;
171
172 memset(key, 0, sizeof(*key));
173
174 if (tcp) {
175 struct gl_program *prog = &tcp->program;
176 per_vertex_slots |= prog->info.outputs_written;
177 per_patch_slots |= prog->info.patch_outputs_written;
178 }
179
180 if (devinfo->gen < 8 || !tcp)
181 key->input_vertices = brw->ctx.TessCtrlProgram.patch_vertices;
182 key->outputs_written = per_vertex_slots;
183 key->patch_outputs_written = per_patch_slots;
184
185 /* We need to specialize our code generation for tessellation levels
186 * based on the domain the DS is expecting to tessellate.
187 */
188 key->tes_primitive_mode = tep->program.info.tess.primitive_mode;
189 key->quads_workaround = devinfo->gen < 9 &&
190 tep->program.info.tess.primitive_mode == GL_QUADS &&
191 tep->program.info.tess.spacing == TESS_SPACING_EQUAL;
192
193 if (tcp) {
194 key->program_string_id = tcp->id;
195
196 /* _NEW_TEXTURE */
197 brw_populate_sampler_prog_key_data(&brw->ctx, &tcp->program, &key->tex);
198 }
199 }
200
201 void
202 brw_upload_tcs_prog(struct brw_context *brw)
203 {
204 struct brw_stage_state *stage_state = &brw->tcs.base;
205 struct brw_tcs_prog_key key;
206 /* BRW_NEW_TESS_PROGRAMS */
207 struct brw_program *tcp =
208 (struct brw_program *) brw->programs[MESA_SHADER_TESS_CTRL];
209 MAYBE_UNUSED struct brw_program *tep =
210 (struct brw_program *) brw->programs[MESA_SHADER_TESS_EVAL];
211 assert(tep);
212
213 if (!brw_state_dirty(brw,
214 _NEW_TEXTURE,
215 BRW_NEW_PATCH_PRIMITIVE |
216 BRW_NEW_TESS_PROGRAMS))
217 return;
218
219 brw_tcs_populate_key(brw, &key);
220
221 if (brw_search_cache(&brw->cache, BRW_CACHE_TCS_PROG, &key, sizeof(key),
222 &stage_state->prog_offset, &brw->tcs.base.prog_data,
223 true))
224 return;
225
226 if (brw_disk_cache_upload_program(brw, MESA_SHADER_TESS_CTRL))
227 return;
228
229 tcp = (struct brw_program *) brw->programs[MESA_SHADER_TESS_CTRL];
230 if (tcp)
231 tcp->id = key.program_string_id;
232
233 MAYBE_UNUSED bool success = brw_codegen_tcs_prog(brw, tcp, tep, &key);
234 assert(success);
235 }
236
237 void
238 brw_tcs_populate_default_key(const struct brw_compiler *compiler,
239 struct brw_tcs_prog_key *key,
240 struct gl_shader_program *sh_prog,
241 struct gl_program *prog)
242 {
243 const struct gen_device_info *devinfo = compiler->devinfo;
244 struct brw_program *btcp = brw_program(prog);
245 const struct gl_linked_shader *tes =
246 sh_prog->_LinkedShaders[MESA_SHADER_TESS_EVAL];
247
248 memset(key, 0, sizeof(*key));
249
250 key->program_string_id = btcp->id;
251 brw_setup_tex_for_precompile(devinfo, &key->tex, prog);
252
253 /* Guess that the input and output patches have the same dimensionality. */
254 if (devinfo->gen < 8)
255 key->input_vertices = prog->info.tess.tcs_vertices_out;
256
257 if (tes) {
258 key->tes_primitive_mode = tes->Program->info.tess.primitive_mode;
259 key->quads_workaround = devinfo->gen < 9 &&
260 tes->Program->info.tess.primitive_mode == GL_QUADS &&
261 tes->Program->info.tess.spacing == TESS_SPACING_EQUAL;
262 } else {
263 key->tes_primitive_mode = GL_TRIANGLES;
264 }
265
266 key->outputs_written = prog->nir->info.outputs_written;
267 key->patch_outputs_written = prog->nir->info.patch_outputs_written;
268 }
269
270 bool
271 brw_tcs_precompile(struct gl_context *ctx,
272 struct gl_shader_program *shader_prog,
273 struct gl_program *prog)
274 {
275 struct brw_context *brw = brw_context(ctx);
276 const struct brw_compiler *compiler = brw->screen->compiler;
277 struct brw_tcs_prog_key key;
278 uint32_t old_prog_offset = brw->tcs.base.prog_offset;
279 struct brw_stage_prog_data *old_prog_data = brw->tcs.base.prog_data;
280 bool success;
281
282 struct brw_program *btcp = brw_program(prog);
283 const struct gl_linked_shader *tes =
284 shader_prog->_LinkedShaders[MESA_SHADER_TESS_EVAL];
285 struct brw_program *btep = tes ? brw_program(tes->Program) : NULL;
286
287 brw_tcs_populate_default_key(compiler, &key, shader_prog, prog);
288
289 success = brw_codegen_tcs_prog(brw, btcp, btep, &key);
290
291 brw->tcs.base.prog_offset = old_prog_offset;
292 brw->tcs.base.prog_data = old_prog_data;
293
294 return success;
295 }