freedreno: Share constlen between different stages properly
[mesa.git] / src / gallium / drivers / freedreno / ir3 / ir3_gallium.c
1 /*
2 * Copyright (C) 2014 Rob Clark <robclark@freedesktop.org>
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 FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 * SOFTWARE.
22 *
23 * Authors:
24 * Rob Clark <robclark@freedesktop.org>
25 */
26
27 #include "pipe/p_state.h"
28 #include "pipe/p_screen.h"
29 #include "util/u_string.h"
30 #include "util/u_memory.h"
31 #include "util/u_inlines.h"
32 #include "util/format/u_format.h"
33 #include "tgsi/tgsi_dump.h"
34 #include "tgsi/tgsi_parse.h"
35
36 #include "nir/tgsi_to_nir.h"
37
38 #include "freedreno_context.h"
39 #include "freedreno_util.h"
40
41 #include "ir3/ir3_shader.h"
42 #include "ir3/ir3_gallium.h"
43 #include "ir3/ir3_compiler.h"
44 #include "ir3/ir3_nir.h"
45
46 static void
47 dump_shader_info(struct ir3_shader_variant *v, bool binning_pass,
48 struct pipe_debug_callback *debug)
49 {
50 if (!unlikely(fd_mesa_debug & FD_DBG_SHADERDB))
51 return;
52
53 pipe_debug_message(debug, SHADER_INFO,
54 "%s shader: %u inst, %u nops, %u non-nops, %u mov, %u cov, "
55 "%u dwords, %u last-baryf, %u half, %u full, %u constlen, "
56 "%u sstall, %u (ss), %u (sy), %d max_sun, %d loops\n",
57 ir3_shader_stage(v),
58 v->info.instrs_count,
59 v->info.nops_count,
60 v->info.instrs_count - v->info.nops_count,
61 v->info.mov_count,
62 v->info.cov_count,
63 v->info.sizedwords,
64 v->info.last_baryf,
65 v->info.max_half_reg + 1,
66 v->info.max_reg + 1,
67 v->constlen,
68 v->info.sstall,
69 v->info.ss, v->info.sy,
70 v->max_sun, v->loops);
71 }
72
73 static void
74 upload_shader_variant(struct ir3_shader_variant *v)
75 {
76 struct shader_info *info = &v->shader->nir->info;
77 struct ir3_compiler *compiler = v->shader->compiler;
78
79 assert(!v->bo);
80
81 unsigned sz = v->info.sizedwords * 4;
82
83 v->bo = fd_bo_new(compiler->dev, sz,
84 DRM_FREEDRENO_GEM_CACHE_WCOMBINE |
85 DRM_FREEDRENO_GEM_TYPE_KMEM,
86 "%s:%s", ir3_shader_stage(v), info->name);
87
88 /* Always include shaders in kernel crash dumps. */
89 fd_bo_mark_for_dump(v->bo);
90
91 memcpy(fd_bo_map(v->bo), v->bin, sz);
92 }
93
94 struct ir3_shader_variant *
95 ir3_shader_variant(struct ir3_shader *shader, struct ir3_shader_key key,
96 bool binning_pass, struct pipe_debug_callback *debug)
97 {
98 struct ir3_shader_variant *v;
99 bool created = false;
100
101 /* Some shader key values may not be used by a given ir3_shader (for
102 * example, fragment shader saturates in the vertex shader), so clean out
103 * those flags to avoid recompiling.
104 */
105 ir3_key_clear_unused(&key, shader);
106
107 v = ir3_shader_get_variant(shader, &key, binning_pass, &created);
108
109 if (created) {
110 if (shader->initial_variants_done) {
111 pipe_debug_message(debug, SHADER_INFO,
112 "%s shader: recompiling at draw time: global 0x%08x, vsats %x/%x/%x, fsats %x/%x/%x, vfsamples %x/%x, astc %x/%x\n",
113 ir3_shader_stage(v),
114 key.global,
115 key.vsaturate_s, key.vsaturate_t, key.vsaturate_r,
116 key.fsaturate_s, key.fsaturate_t, key.fsaturate_r,
117 key.vsamples, key.fsamples,
118 key.vastc_srgb, key.fastc_srgb);
119
120 }
121 dump_shader_info(v, binning_pass, debug);
122
123 upload_shader_variant(v);
124 }
125
126 return v;
127 }
128
129 static void
130 copy_stream_out(struct ir3_stream_output_info *i,
131 const struct pipe_stream_output_info *p)
132 {
133 STATIC_ASSERT(ARRAY_SIZE(i->stride) == ARRAY_SIZE(p->stride));
134 STATIC_ASSERT(ARRAY_SIZE(i->output) == ARRAY_SIZE(p->output));
135
136 i->num_outputs = p->num_outputs;
137 for (int n = 0; n < ARRAY_SIZE(i->stride); n++)
138 i->stride[n] = p->stride[n];
139
140 for (int n = 0; n < ARRAY_SIZE(i->output); n++) {
141 i->output[n].register_index = p->output[n].register_index;
142 i->output[n].start_component = p->output[n].start_component;
143 i->output[n].num_components = p->output[n].num_components;
144 i->output[n].output_buffer = p->output[n].output_buffer;
145 i->output[n].dst_offset = p->output[n].dst_offset;
146 i->output[n].stream = p->output[n].stream;
147 }
148 }
149
150 struct ir3_shader *
151 ir3_shader_create(struct ir3_compiler *compiler,
152 const struct pipe_shader_state *cso,
153 struct pipe_debug_callback *debug,
154 struct pipe_screen *screen)
155 {
156 nir_shader *nir;
157 if (cso->type == PIPE_SHADER_IR_NIR) {
158 /* we take ownership of the reference: */
159 nir = cso->ir.nir;
160 } else {
161 debug_assert(cso->type == PIPE_SHADER_IR_TGSI);
162 if (ir3_shader_debug & IR3_DBG_DISASM) {
163 tgsi_dump(cso->tokens, 0);
164 }
165 nir = tgsi_to_nir(cso->tokens, screen, false);
166 }
167
168 struct ir3_stream_output_info stream_output;
169 copy_stream_out(&stream_output, &cso->stream_output);
170
171 struct ir3_shader *shader = ir3_shader_from_nir(compiler, nir, 0, &stream_output);
172
173 /* Compile standard variants immediately to try to avoid draw-time stalls
174 * to run the compiler.
175 */
176 struct ir3_shader_key key = {
177 .tessellation = IR3_TESS_NONE,
178 .msaa = true,
179 };
180
181 switch (nir->info.stage) {
182 case MESA_SHADER_TESS_EVAL:
183 key.tessellation = ir3_tess_mode(nir->info.tess.primitive_mode);
184 break;
185
186 case MESA_SHADER_TESS_CTRL:
187 /* The primitive_mode field, while it exists for TCS, is not
188 * populated (since separable shaders between TCS/TES are legal,
189 * so TCS wouldn't have access to TES's declaration). Make a
190 * guess so that we shader-db something plausible for TCS.
191 */
192 if (nir->info.outputs_written & VARYING_BIT_TESS_LEVEL_INNER)
193 key.tessellation = IR3_TESS_TRIANGLES;
194 else
195 key.tessellation = IR3_TESS_ISOLINES;
196 break;
197
198 case MESA_SHADER_GEOMETRY:
199 key.has_gs = true;
200 break;
201
202 default:
203 break;
204 }
205
206 key.safe_constlen = false;
207 struct ir3_shader_variant *v = ir3_shader_variant(shader, key, false, debug);
208 if (!v)
209 return NULL;
210
211 if (v->constlen > compiler->max_const_safe) {
212 key.safe_constlen = true;
213 ir3_shader_variant(shader, key, false, debug);
214 }
215
216 if (nir->info.stage == MESA_SHADER_VERTEX) {
217 key.safe_constlen = false;
218 v = ir3_shader_variant(shader, key, true, debug);
219 if (!v)
220 return NULL;
221
222 if (v->constlen > compiler->max_const_safe) {
223 key.safe_constlen = true;
224 ir3_shader_variant(shader, key, true, debug);
225 }
226 }
227
228 shader->initial_variants_done = true;
229
230 return shader;
231 }
232
233 /* a bit annoying that compute-shader and normal shader state objects
234 * aren't a bit more aligned.
235 */
236 struct ir3_shader *
237 ir3_shader_create_compute(struct ir3_compiler *compiler,
238 const struct pipe_compute_state *cso,
239 struct pipe_debug_callback *debug,
240 struct pipe_screen *screen)
241 {
242 nir_shader *nir;
243 if (cso->ir_type == PIPE_SHADER_IR_NIR) {
244 /* we take ownership of the reference: */
245 nir = (nir_shader *)cso->prog;
246 } else {
247 debug_assert(cso->ir_type == PIPE_SHADER_IR_TGSI);
248 if (ir3_shader_debug & IR3_DBG_DISASM) {
249 tgsi_dump(cso->prog, 0);
250 }
251 nir = tgsi_to_nir(cso->prog, screen, false);
252 }
253
254 struct ir3_shader *shader = ir3_shader_from_nir(compiler, nir, 0, NULL);
255
256 /* Immediately compile a standard variant. We have so few variants in our
257 * shaders, that doing so almost eliminates draw-time recompiles. (This
258 * is also how we get data from shader-db's ./run)
259 */
260 static struct ir3_shader_key key; /* static is implicitly zeroed */
261 ir3_shader_variant(shader, key, false, debug);
262
263 shader->initial_variants_done = true;
264
265 return shader;
266 }
267
268 void *
269 ir3_shader_state_create(struct pipe_context *pctx, const struct pipe_shader_state *cso)
270 {
271 struct fd_context *ctx = fd_context(pctx);
272 struct ir3_compiler *compiler = ctx->screen->compiler;
273 return ir3_shader_create(compiler, cso, &ctx->debug, pctx->screen);
274 }
275
276 void
277 ir3_shader_state_delete(struct pipe_context *pctx, void *hwcso)
278 {
279 struct ir3_shader *so = hwcso;
280
281 /* free the uploaded shaders, since this is handled outside of the
282 * shared ir3 code (ie. not used by turnip):
283 */
284 for (struct ir3_shader_variant *v = so->variants; v; v = v->next) {
285 fd_bo_del(v->bo);
286 v->bo = NULL;
287
288 if (v->binning) {
289 fd_bo_del(v->binning->bo);
290 v->binning->bo = NULL;
291 }
292 }
293
294 ir3_shader_destroy(so);
295 }
296
297 void
298 ir3_prog_init(struct pipe_context *pctx)
299 {
300 pctx->create_vs_state = ir3_shader_state_create;
301 pctx->delete_vs_state = ir3_shader_state_delete;
302
303 pctx->create_tcs_state = ir3_shader_state_create;
304 pctx->delete_tcs_state = ir3_shader_state_delete;
305
306 pctx->create_tes_state = ir3_shader_state_create;
307 pctx->delete_tes_state = ir3_shader_state_delete;
308
309 pctx->create_gs_state = ir3_shader_state_create;
310 pctx->delete_gs_state = ir3_shader_state_delete;
311
312 pctx->create_fs_state = ir3_shader_state_create;
313 pctx->delete_fs_state = ir3_shader_state_delete;
314 }