freedreno/ir3: fix constlen with relative addressing
[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_lowering.h"
39 #include "freedreno_util.h"
40
41 #include "ir3_shader.h"
42 #include "ir3_compiler.h"
43
44
45 static void
46 delete_variant(struct ir3_shader_variant *v)
47 {
48 if (v->ir)
49 ir3_destroy(v->ir);
50 fd_bo_del(v->bo);
51 free(v);
52 }
53
54 static void
55 assemble_variant(struct ir3_shader_variant *v)
56 {
57 struct fd_context *ctx = fd_context(v->shader->pctx);
58 uint32_t sz, *bin;
59
60 bin = ir3_assemble(v->ir, &v->info);
61 sz = v->info.sizedwords * 4;
62
63 v->bo = fd_bo_new(ctx->dev, sz,
64 DRM_FREEDRENO_GEM_CACHE_WCOMBINE |
65 DRM_FREEDRENO_GEM_TYPE_KMEM);
66
67 memcpy(fd_bo_map(v->bo), bin, sz);
68
69 free(bin);
70
71 v->instrlen = v->info.sizedwords / 8;
72
73 /* NOTE: if relative addressing is used, we set constlen in
74 * the compiler (to worst-case value) since we don't know in
75 * the assembler what the max addr reg value can be:
76 */
77 v->constlen = MAX2(v->constlen, v->info.max_const + 1);
78
79 /* no need to keep the ir around beyond this point: */
80 ir3_destroy(v->ir);
81 v->ir = NULL;
82 }
83
84 /* for vertex shader, the inputs are loaded into registers before the shader
85 * is executed, so max_regs from the shader instructions might not properly
86 * reflect the # of registers actually used:
87 */
88 static void
89 fixup_vp_regfootprint(struct ir3_shader_variant *v)
90 {
91 unsigned i;
92 for (i = 0; i < v->inputs_count; i++) {
93 if (v->inputs[i].compmask) {
94 uint32_t regid = (v->inputs[i].regid + 3) >> 2;
95 v->info.max_reg = MAX2(v->info.max_reg, regid);
96 }
97 }
98 for (i = 0; i < v->outputs_count; i++) {
99 uint32_t regid = (v->outputs[i].regid + 3) >> 2;
100 v->info.max_reg = MAX2(v->info.max_reg, regid);
101 }
102 }
103
104 static struct ir3_shader_variant *
105 create_variant(struct ir3_shader *shader, struct ir3_shader_key key)
106 {
107 struct ir3_shader_variant *v = CALLOC_STRUCT(ir3_shader_variant);
108 const struct tgsi_token *tokens = shader->tokens;
109 int ret;
110
111 if (!v)
112 return NULL;
113
114 v->shader = shader;
115 v->key = key;
116 v->type = shader->type;
117
118 if (fd_mesa_debug & FD_DBG_DISASM) {
119 DBG("dump tgsi: type=%d, k={bp=%u,cts=%u,hp=%u}", shader->type,
120 key.binning_pass, key.color_two_side, key.half_precision);
121 tgsi_dump(tokens, 0);
122 }
123
124 if (!(fd_mesa_debug & FD_DBG_NOOPT)) {
125 ret = ir3_compile_shader(v, tokens, key);
126 if (ret) {
127 debug_error("new compiler failed, trying fallback!");
128
129 v->inputs_count = 0;
130 v->outputs_count = 0;
131 v->total_in = 0;
132 v->has_samp = false;
133 v->immediates_count = 0;
134 }
135 } else {
136 ret = -1; /* force fallback to old compiler */
137 }
138
139 if (ret)
140 ret = ir3_compile_shader_old(v, tokens, key);
141
142 if (ret) {
143 debug_error("compile failed!");
144 goto fail;
145 }
146
147 assemble_variant(v);
148 if (!v->bo) {
149 debug_error("assemble failed!");
150 goto fail;
151 }
152
153 if (shader->type == SHADER_VERTEX)
154 fixup_vp_regfootprint(v);
155
156 if (fd_mesa_debug & FD_DBG_DISASM) {
157 DBG("disassemble: type=%d, k={bp=%u,cts=%u,hp=%u}", v->type,
158 key.binning_pass, key.color_two_side, key.half_precision);
159 disasm_a3xx(fd_bo_map(v->bo), v->info.sizedwords, 0, v->type);
160 }
161
162 return v;
163
164 fail:
165 delete_variant(v);
166 return NULL;
167 }
168
169 struct ir3_shader_variant *
170 ir3_shader_variant(struct ir3_shader *shader, struct ir3_shader_key key)
171 {
172 struct ir3_shader_variant *v;
173
174 /* some shader key values only apply to vertex or frag shader,
175 * so normalize the key to avoid constructing multiple identical
176 * variants:
177 */
178 if (shader->type == SHADER_FRAGMENT) {
179 key.binning_pass = false;
180 }
181 if (shader->type == SHADER_VERTEX) {
182 key.color_two_side = false;
183 key.half_precision = false;
184 }
185
186 for (v = shader->variants; v; v = v->next)
187 if (!memcmp(&key, &v->key, sizeof(key)))
188 return v;
189
190 /* compile new variant if it doesn't exist already: */
191 v = create_variant(shader, key);
192 v->next = shader->variants;
193 shader->variants = v;
194
195 return v;
196 }
197
198
199 void
200 ir3_shader_destroy(struct ir3_shader *shader)
201 {
202 struct ir3_shader_variant *v, *t;
203 for (v = shader->variants; v; ) {
204 t = v;
205 v = v->next;
206 delete_variant(t);
207 }
208 free((void *)shader->tokens);
209 free(shader);
210 }
211
212 struct ir3_shader *
213 ir3_shader_create(struct pipe_context *pctx, const struct tgsi_token *tokens,
214 enum shader_t type)
215 {
216 struct ir3_shader *shader = CALLOC_STRUCT(ir3_shader);
217 shader->pctx = pctx;
218 shader->type = type;
219 shader->tokens = tgsi_dup_tokens(tokens);
220 return shader;
221 }