freedreno/ir3: Improve shader key normalization.
[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 struct ir3_shader_variant *
74 ir3_shader_variant(struct ir3_shader *shader, struct ir3_shader_key key,
75 bool binning_pass, struct pipe_debug_callback *debug)
76 {
77 struct ir3_shader_variant *v;
78 bool created = false;
79
80 /* Some shader key values may not be used by a given ir3_shader (for
81 * example, fragment shader saturates in the vertex shader), so clean out
82 * those flags to avoid recompiling.
83 */
84 ir3_key_clear_unused(&key, shader);
85
86 v = ir3_shader_get_variant(shader, &key, binning_pass, &created);
87
88 if (created) {
89 if (shader->initial_variants_done) {
90 pipe_debug_message(debug, SHADER_INFO,
91 "%s shader: recompiling at draw time: global 0x%08x, vsats %x/%x/%x, fsats %x/%x/%x, vfsamples %x/%x, astc %x/%x\n",
92 ir3_shader_stage(v),
93 key.global,
94 key.vsaturate_s, key.vsaturate_t, key.vsaturate_r,
95 key.fsaturate_s, key.fsaturate_t, key.fsaturate_r,
96 key.vsamples, key.fsamples,
97 key.vastc_srgb, key.fastc_srgb);
98
99 }
100 dump_shader_info(v, binning_pass, debug);
101 }
102
103 return v;
104 }
105
106 static void
107 copy_stream_out(struct ir3_stream_output_info *i,
108 const struct pipe_stream_output_info *p)
109 {
110 STATIC_ASSERT(ARRAY_SIZE(i->stride) == ARRAY_SIZE(p->stride));
111 STATIC_ASSERT(ARRAY_SIZE(i->output) == ARRAY_SIZE(p->output));
112
113 i->num_outputs = p->num_outputs;
114 for (int n = 0; n < ARRAY_SIZE(i->stride); n++)
115 i->stride[n] = p->stride[n];
116
117 for (int n = 0; n < ARRAY_SIZE(i->output); n++) {
118 i->output[n].register_index = p->output[n].register_index;
119 i->output[n].start_component = p->output[n].start_component;
120 i->output[n].num_components = p->output[n].num_components;
121 i->output[n].output_buffer = p->output[n].output_buffer;
122 i->output[n].dst_offset = p->output[n].dst_offset;
123 i->output[n].stream = p->output[n].stream;
124 }
125 }
126
127 struct ir3_shader *
128 ir3_shader_create(struct ir3_compiler *compiler,
129 const struct pipe_shader_state *cso,
130 struct pipe_debug_callback *debug,
131 struct pipe_screen *screen)
132 {
133 nir_shader *nir;
134 if (cso->type == PIPE_SHADER_IR_NIR) {
135 /* we take ownership of the reference: */
136 nir = cso->ir.nir;
137 } else {
138 debug_assert(cso->type == PIPE_SHADER_IR_TGSI);
139 if (ir3_shader_debug & IR3_DBG_DISASM) {
140 tgsi_dump(cso->tokens, 0);
141 }
142 nir = tgsi_to_nir(cso->tokens, screen);
143 }
144
145 struct ir3_stream_output_info stream_output;
146 copy_stream_out(&stream_output, &cso->stream_output);
147
148 struct ir3_shader *shader = ir3_shader_from_nir(compiler, nir, &stream_output);
149
150 if (fd_mesa_debug & FD_DBG_SHADERDB) {
151 /* if shader-db run, create a standard variant immediately
152 * (as otherwise nothing will trigger the shader to be
153 * actually compiled)
154 */
155 struct ir3_shader_key key = {
156 .tessellation = IR3_TESS_NONE,
157 };
158
159 switch (nir->info.stage) {
160 case MESA_SHADER_TESS_EVAL:
161 key.tessellation = ir3_tess_mode(nir->info.tess.primitive_mode);
162 break;
163
164 case MESA_SHADER_TESS_CTRL:
165 /* The primitive_mode field, while it exists for TCS, is not
166 * populated (since separable shaders between TCS/TES are legal,
167 * so TCS wouldn't have access to TES's declaration). Make a
168 * guess so that we shader-db something plausible for TCS.
169 */
170 if (nir->info.outputs_written & VARYING_BIT_TESS_LEVEL_INNER)
171 key.tessellation = IR3_TESS_TRIANGLES;
172 else
173 key.tessellation = IR3_TESS_ISOLINES;
174 break;
175
176 case MESA_SHADER_GEOMETRY:
177 key.has_gs = true;
178 break;
179
180 default:
181 break;
182 }
183
184 ir3_shader_variant(shader, key, false, debug);
185
186 if (nir->info.stage == MESA_SHADER_VERTEX)
187 ir3_shader_variant(shader, key, true, debug);
188 }
189
190 shader->initial_variants_done = true;
191
192 return shader;
193 }
194
195 /* a bit annoying that compute-shader and normal shader state objects
196 * aren't a bit more aligned.
197 */
198 struct ir3_shader *
199 ir3_shader_create_compute(struct ir3_compiler *compiler,
200 const struct pipe_compute_state *cso,
201 struct pipe_debug_callback *debug,
202 struct pipe_screen *screen)
203 {
204 nir_shader *nir;
205 if (cso->ir_type == PIPE_SHADER_IR_NIR) {
206 /* we take ownership of the reference: */
207 nir = (nir_shader *)cso->prog;
208 } else {
209 debug_assert(cso->ir_type == PIPE_SHADER_IR_TGSI);
210 if (ir3_shader_debug & IR3_DBG_DISASM) {
211 tgsi_dump(cso->prog, 0);
212 }
213 nir = tgsi_to_nir(cso->prog, screen);
214 }
215
216 struct ir3_shader *shader = ir3_shader_from_nir(compiler, nir, NULL);
217
218 if (fd_mesa_debug & FD_DBG_SHADERDB) {
219 /* if shader-db run, create a standard variant immediately
220 * (as otherwise nothing will trigger the shader to be
221 * actually compiled)
222 */
223 static struct ir3_shader_key key; /* static is implicitly zeroed */
224 ir3_shader_variant(shader, key, false, debug);
225 }
226
227 shader->initial_variants_done = true;
228
229 return shader;
230 }
231
232 static void *
233 ir3_shader_state_create(struct pipe_context *pctx, const struct pipe_shader_state *cso)
234 {
235 struct fd_context *ctx = fd_context(pctx);
236 struct ir3_compiler *compiler = ctx->screen->compiler;
237 return ir3_shader_create(compiler, cso, &ctx->debug, pctx->screen);
238 }
239
240 static void
241 ir3_shader_state_delete(struct pipe_context *pctx, void *hwcso)
242 {
243 struct ir3_shader *so = hwcso;
244 ir3_shader_destroy(so);
245 }
246
247 void
248 ir3_prog_init(struct pipe_context *pctx)
249 {
250 pctx->create_vs_state = ir3_shader_state_create;
251 pctx->delete_vs_state = ir3_shader_state_delete;
252
253 pctx->create_tcs_state = ir3_shader_state_create;
254 pctx->delete_tcs_state = ir3_shader_state_delete;
255
256 pctx->create_tes_state = ir3_shader_state_create;
257 pctx->delete_tes_state = ir3_shader_state_delete;
258
259 pctx->create_gs_state = ir3_shader_state_create;
260 pctx->delete_gs_state = ir3_shader_state_delete;
261
262 pctx->create_fs_state = ir3_shader_state_create;
263 pctx->delete_fs_state = ir3_shader_state_delete;
264 }