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