freedreno/ir3: optimize shader key comparision
[mesa.git] / src / gallium / drivers / freedreno / ir3 / ir3_shader.c
1 /* -*- mode: C; c-file-style: "k&r"; tab-width 4; indent-tabs-mode: t; -*- */
2
3 /*
4 * Copyright (C) 2014 Rob Clark <robclark@freedesktop.org>
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice (including the next
14 * paragraph) shall be included in all copies or substantial portions of the
15 * Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 * SOFTWARE.
24 *
25 * Authors:
26 * Rob Clark <robclark@freedesktop.org>
27 */
28
29 #include "pipe/p_state.h"
30 #include "util/u_string.h"
31 #include "util/u_memory.h"
32 #include "util/u_inlines.h"
33 #include "util/u_format.h"
34 #include "tgsi/tgsi_dump.h"
35 #include "tgsi/tgsi_parse.h"
36
37 #include "freedreno_context.h"
38 #include "freedreno_util.h"
39
40 #include "ir3_shader.h"
41 #include "ir3_compiler.h"
42
43
44 static void
45 delete_variant(struct ir3_shader_variant *v)
46 {
47 if (v->ir)
48 ir3_destroy(v->ir);
49 fd_bo_del(v->bo);
50 free(v);
51 }
52
53 static void
54 assemble_variant(struct ir3_shader_variant *v)
55 {
56 struct fd_context *ctx = fd_context(v->shader->pctx);
57 uint32_t sz, *bin;
58
59 bin = ir3_assemble(v->ir, &v->info);
60 sz = v->info.sizedwords * 4;
61
62 v->bo = fd_bo_new(ctx->dev, sz,
63 DRM_FREEDRENO_GEM_CACHE_WCOMBINE |
64 DRM_FREEDRENO_GEM_TYPE_KMEM);
65
66 memcpy(fd_bo_map(v->bo), bin, sz);
67
68 free(bin);
69
70 v->instrlen = v->info.sizedwords / 8;
71
72 /* NOTE: if relative addressing is used, we set constlen in
73 * the compiler (to worst-case value) since we don't know in
74 * the assembler what the max addr reg value can be:
75 */
76 v->constlen = MAX2(v->constlen, v->info.max_const + 1);
77
78 /* no need to keep the ir around beyond this point: */
79 ir3_destroy(v->ir);
80 v->ir = NULL;
81 }
82
83 /* for vertex shader, the inputs are loaded into registers before the shader
84 * is executed, so max_regs from the shader instructions might not properly
85 * reflect the # of registers actually used:
86 */
87 static void
88 fixup_vp_regfootprint(struct ir3_shader_variant *v)
89 {
90 unsigned i;
91 for (i = 0; i < v->inputs_count; i++) {
92 if (v->inputs[i].compmask) {
93 int32_t regid = (v->inputs[i].regid + 3) >> 2;
94 v->info.max_reg = MAX2(v->info.max_reg, regid);
95 }
96 }
97 for (i = 0; i < v->outputs_count; i++) {
98 int32_t regid = (v->outputs[i].regid + 3) >> 2;
99 v->info.max_reg = MAX2(v->info.max_reg, regid);
100 }
101 }
102
103 /* reset before attempting to compile again.. */
104 static void reset_variant(struct ir3_shader_variant *v, const char *msg)
105 {
106 debug_error(msg);
107 v->inputs_count = 0;
108 v->outputs_count = 0;
109 v->total_in = 0;
110 v->has_samp = false;
111 v->immediates_count = 0;
112 }
113
114 static struct ir3_shader_variant *
115 create_variant(struct ir3_shader *shader, struct ir3_shader_key key)
116 {
117 struct ir3_shader_variant *v = CALLOC_STRUCT(ir3_shader_variant);
118 const struct tgsi_token *tokens = shader->tokens;
119 int ret;
120
121 if (!v)
122 return NULL;
123
124 v->shader = shader;
125 v->key = key;
126 v->type = shader->type;
127
128 if (fd_mesa_debug & FD_DBG_DISASM) {
129 DBG("dump tgsi: type=%d, k={bp=%u,cts=%u,hp=%u}", shader->type,
130 key.binning_pass, key.color_two_side, key.half_precision);
131 tgsi_dump(tokens, 0);
132 }
133
134 if (!(fd_mesa_debug & FD_DBG_NOOPT)) {
135 ret = ir3_compile_shader(v, tokens, key, true);
136 if (ret) {
137 reset_variant(v, "new compiler failed, trying without copy propagation!");
138 ret = ir3_compile_shader(v, tokens, key, false);
139 if (ret)
140 reset_variant(v, "new compiler failed, trying fallback!");
141 }
142 } else {
143 ret = -1; /* force fallback to old compiler */
144 }
145
146 if (ret)
147 ret = ir3_compile_shader_old(v, tokens, key);
148
149 if (ret) {
150 debug_error("compile failed!");
151 goto fail;
152 }
153
154 assemble_variant(v);
155 if (!v->bo) {
156 debug_error("assemble failed!");
157 goto fail;
158 }
159
160 if (shader->type == SHADER_VERTEX)
161 fixup_vp_regfootprint(v);
162
163 if (fd_mesa_debug & FD_DBG_DISASM) {
164 DBG("disassemble: type=%d, k={bp=%u,cts=%u,hp=%u}", v->type,
165 key.binning_pass, key.color_two_side, key.half_precision);
166 disasm_a3xx(fd_bo_map(v->bo), v->info.sizedwords, 0, v->type);
167 }
168
169 return v;
170
171 fail:
172 delete_variant(v);
173 return NULL;
174 }
175
176 struct ir3_shader_variant *
177 ir3_shader_variant(struct ir3_shader *shader, struct ir3_shader_key key)
178 {
179 struct ir3_shader_variant *v;
180
181 /* some shader key values only apply to vertex or frag shader,
182 * so normalize the key to avoid constructing multiple identical
183 * variants:
184 */
185 switch (shader->type) {
186 case SHADER_FRAGMENT:
187 case SHADER_COMPUTE:
188 key.binning_pass = false;
189 if (key.has_per_samp) {
190 key.vsaturate_s = 0;
191 key.vsaturate_t = 0;
192 key.vsaturate_r = 0;
193 }
194 break;
195 case SHADER_VERTEX:
196 key.color_two_side = false;
197 key.half_precision = false;
198 key.alpha = false;
199 if (key.has_per_samp) {
200 key.fsaturate_s = 0;
201 key.fsaturate_t = 0;
202 key.fsaturate_r = 0;
203 }
204 break;
205 }
206
207 for (v = shader->variants; v; v = v->next)
208 if (ir3_shader_key_equal(&key, &v->key))
209 return v;
210
211 /* compile new variant if it doesn't exist already: */
212 v = create_variant(shader, key);
213 v->next = shader->variants;
214 shader->variants = v;
215
216 return v;
217 }
218
219
220 void
221 ir3_shader_destroy(struct ir3_shader *shader)
222 {
223 struct ir3_shader_variant *v, *t;
224 for (v = shader->variants; v; ) {
225 t = v;
226 v = v->next;
227 delete_variant(t);
228 }
229 free((void *)shader->tokens);
230 free(shader);
231 }
232
233 struct ir3_shader *
234 ir3_shader_create(struct pipe_context *pctx, const struct tgsi_token *tokens,
235 enum shader_t type)
236 {
237 struct ir3_shader *shader = CALLOC_STRUCT(ir3_shader);
238 shader->pctx = pctx;
239 shader->type = type;
240 shader->tokens = tgsi_dup_tokens(tokens);
241 return shader;
242 }