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 bool
brw_codegen_tes_prog(struct brw_context * brw,struct brw_program * tep,struct brw_tes_prog_key * key)37 brw_codegen_tes_prog(struct brw_context *brw,
38 struct brw_program *tep,
39 struct brw_tes_prog_key *key)
40 {
41 const struct brw_compiler *compiler = brw->screen->compiler;
42 const struct intel_device_info *devinfo = &brw->screen->devinfo;
43 struct brw_stage_state *stage_state = &brw->tes.base;
44 struct brw_tes_prog_data prog_data;
45 bool start_busy = false;
46 double start_time = 0;
47
48 memset(&prog_data, 0, sizeof(prog_data));
49
50 void *mem_ctx = ralloc_context(NULL);
51
52 nir_shader *nir = nir_shader_clone(mem_ctx, tep->program.nir);
53
54 brw_assign_common_binding_table_offsets(devinfo, &tep->program,
55 &prog_data.base.base, 0);
56
57 brw_nir_setup_glsl_uniforms(mem_ctx, nir, &tep->program,
58 &prog_data.base.base,
59 compiler->scalar_stage[MESA_SHADER_TESS_EVAL]);
60 if (brw->can_push_ubos) {
61 brw_nir_analyze_ubo_ranges(compiler, nir, NULL,
62 prog_data.base.base.ubo_ranges);
63 }
64
65 int st_index = -1;
66 if (INTEL_DEBUG(DEBUG_SHADER_TIME))
67 st_index = brw_get_shader_time_index(brw, &tep->program, ST_TES, true);
68
69 if (unlikely(brw->perf_debug)) {
70 start_busy = brw->batch.last_bo && brw_bo_busy(brw->batch.last_bo);
71 start_time = get_time();
72 }
73
74 struct brw_vue_map input_vue_map;
75 brw_compute_tess_vue_map(&input_vue_map, key->inputs_read,
76 key->patch_inputs_read);
77
78 char *error_str;
79 const unsigned *program =
80 brw_compile_tes(compiler, brw, mem_ctx, key, &input_vue_map, &prog_data,
81 nir, st_index, NULL, &error_str);
82 if (program == NULL) {
83 tep->program.sh.data->LinkStatus = LINKING_FAILURE;
84 ralloc_strcat(&tep->program.sh.data->InfoLog, error_str);
85
86 _mesa_problem(NULL, "Failed to compile tessellation evaluation shader: "
87 "%s\n", error_str);
88
89 ralloc_free(mem_ctx);
90 return false;
91 }
92
93 if (unlikely(brw->perf_debug)) {
94 if (tep->compiled_once) {
95 brw_debug_recompile(brw, MESA_SHADER_TESS_EVAL, tep->program.Id,
96 &key->base);
97 }
98 if (start_busy && !brw_bo_busy(brw->batch.last_bo)) {
99 perf_debug("TES compile took %.03f ms and stalled the GPU\n",
100 (get_time() - start_time) * 1000);
101 }
102 tep->compiled_once = true;
103 }
104
105 /* Scratch space is used for register spilling */
106 brw_alloc_stage_scratch(brw, stage_state,
107 prog_data.base.base.total_scratch);
108
109 /* The param and pull_param arrays will be freed by the shader cache. */
110 ralloc_steal(NULL, prog_data.base.base.param);
111 ralloc_steal(NULL, prog_data.base.base.pull_param);
112 brw_upload_cache(&brw->cache, BRW_CACHE_TES_PROG,
113 key, sizeof(*key),
114 program, prog_data.base.base.program_size,
115 &prog_data, sizeof(prog_data),
116 &stage_state->prog_offset, &brw->tes.base.prog_data);
117 ralloc_free(mem_ctx);
118
119 return true;
120 }
121
122 void
brw_tes_populate_key(struct brw_context * brw,struct brw_tes_prog_key * key)123 brw_tes_populate_key(struct brw_context *brw,
124 struct brw_tes_prog_key *key)
125 {
126 struct brw_program *tcp =
127 (struct brw_program *) brw->programs[MESA_SHADER_TESS_CTRL];
128 struct brw_program *tep =
129 (struct brw_program *) brw->programs[MESA_SHADER_TESS_EVAL];
130 struct gl_program *prog = &tep->program;
131
132 uint64_t per_vertex_slots = prog->info.inputs_read;
133 uint32_t per_patch_slots = prog->info.patch_inputs_read;
134
135 memset(key, 0, sizeof(*key));
136
137 /* _NEW_TEXTURE */
138 brw_populate_base_prog_key(&brw->ctx, tep, &key->base);
139
140 /* The TCS may have additional outputs which aren't read by the
141 * TES (possibly for cross-thread communication). These need to
142 * be stored in the Patch URB Entry as well.
143 */
144 if (tcp) {
145 struct gl_program *tcp_prog = &tcp->program;
146 per_vertex_slots |= tcp_prog->info.outputs_written &
147 ~(VARYING_BIT_TESS_LEVEL_INNER | VARYING_BIT_TESS_LEVEL_OUTER);
148 per_patch_slots |= tcp_prog->info.patch_outputs_written;
149 }
150
151 key->inputs_read = per_vertex_slots;
152 key->patch_inputs_read = per_patch_slots;
153 }
154
155 void
brw_upload_tes_prog(struct brw_context * brw)156 brw_upload_tes_prog(struct brw_context *brw)
157 {
158 struct brw_stage_state *stage_state = &brw->tes.base;
159 struct brw_tes_prog_key key;
160 /* BRW_NEW_TESS_PROGRAMS */
161 struct brw_program *tep =
162 (struct brw_program *) brw->programs[MESA_SHADER_TESS_EVAL];
163
164 if (!brw_state_dirty(brw,
165 _NEW_TEXTURE,
166 BRW_NEW_TESS_PROGRAMS))
167 return;
168
169 brw_tes_populate_key(brw, &key);
170
171 if (brw_search_cache(&brw->cache, BRW_CACHE_TES_PROG, &key, sizeof(key),
172 &stage_state->prog_offset, &brw->tes.base.prog_data,
173 true))
174 return;
175
176 if (brw_disk_cache_upload_program(brw, MESA_SHADER_TESS_EVAL))
177 return;
178
179 tep = (struct brw_program *) brw->programs[MESA_SHADER_TESS_EVAL];
180 tep->id = key.base.program_string_id;
181
182 ASSERTED bool success = brw_codegen_tes_prog(brw, tep, &key);
183 assert(success);
184 }
185
186 void
brw_tes_populate_default_key(const struct brw_compiler * compiler,struct brw_tes_prog_key * key,struct gl_shader_program * sh_prog,struct gl_program * prog)187 brw_tes_populate_default_key(const struct brw_compiler *compiler,
188 struct brw_tes_prog_key *key,
189 struct gl_shader_program *sh_prog,
190 struct gl_program *prog)
191 {
192 const struct intel_device_info *devinfo = compiler->devinfo;
193 struct brw_program *btep = brw_program(prog);
194
195 memset(key, 0, sizeof(*key));
196
197 brw_populate_default_base_prog_key(devinfo, btep, &key->base);
198
199 key->inputs_read = prog->nir->info.inputs_read;
200 key->patch_inputs_read = prog->nir->info.patch_inputs_read;
201
202 if (sh_prog->_LinkedShaders[MESA_SHADER_TESS_CTRL]) {
203 struct gl_program *tcp =
204 sh_prog->_LinkedShaders[MESA_SHADER_TESS_CTRL]->Program;
205 key->inputs_read |= tcp->nir->info.outputs_written &
206 ~(VARYING_BIT_TESS_LEVEL_INNER | VARYING_BIT_TESS_LEVEL_OUTER);
207 key->patch_inputs_read |= tcp->nir->info.patch_outputs_written;
208 }
209 }
210
211 bool
brw_tes_precompile(struct gl_context * ctx,struct gl_shader_program * shader_prog,struct gl_program * prog)212 brw_tes_precompile(struct gl_context *ctx,
213 struct gl_shader_program *shader_prog,
214 struct gl_program *prog)
215 {
216 struct brw_context *brw = brw_context(ctx);
217 const struct brw_compiler *compiler = brw->screen->compiler;
218 struct brw_tes_prog_key key;
219 uint32_t old_prog_offset = brw->tes.base.prog_offset;
220 struct brw_stage_prog_data *old_prog_data = brw->tes.base.prog_data;
221 bool success;
222
223 struct brw_program *btep = brw_program(prog);
224
225 brw_tes_populate_default_key(compiler, &key, shader_prog, prog);
226
227 success = brw_codegen_tes_prog(brw, btep, &key);
228
229 brw->tes.base.prog_offset = old_prog_offset;
230 brw->tes.base.prog_data = old_prog_data;
231
232 return success;
233 }
234