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