etnaviv: add struct etna_shader_state
[mesa.git] / src / gallium / drivers / etnaviv / etnaviv_shader.c
1 /*
2 * Copyright (c) 2012-2015 Etnaviv Project
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, sub license,
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
12 * next paragraph) shall be included in all copies or substantial portions
13 * of the 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 NON-INFRINGEMENT. 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
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 *
23 * Authors:
24 * Wladimir J. van der Laan <laanwj@gmail.com>
25 */
26
27 #include "etnaviv_shader.h"
28
29 #include "etnaviv_compiler.h"
30 #include "etnaviv_context.h"
31 #include "etnaviv_debug.h"
32 #include "etnaviv_util.h"
33
34 #include "tgsi/tgsi_parse.h"
35 #include "util/u_math.h"
36 #include "util/u_memory.h"
37
38 /* Link vs and fs together: fill in shader_state from vs and fs
39 * as this function is called every time a new fs or vs is bound, the goal is to
40 * do little processing as possible here, and to precompute as much as possible in
41 * the vs/fs shader_object.
42 *
43 * XXX we could cache the link result for a certain set of VS/PS; usually a pair
44 * of VS and PS will be used together anyway.
45 */
46 static bool
47 etna_link_shaders(struct etna_context *ctx, struct compiled_shader_state *cs,
48 const struct etna_shader_variant *vs, const struct etna_shader_variant *fs)
49 {
50 struct etna_shader_link_info link = { };
51
52 assert(vs->processor == PIPE_SHADER_VERTEX);
53 assert(fs->processor == PIPE_SHADER_FRAGMENT);
54
55 #ifdef DEBUG
56 if (DBG_ENABLED(ETNA_DBG_DUMP_SHADERS)) {
57 etna_dump_shader(vs);
58 etna_dump_shader(fs);
59 }
60 #endif
61
62 if (etna_link_shader(&link, vs, fs)) {
63 /* linking failed: some fs inputs do not have corresponding
64 * vs outputs */
65 assert(0);
66
67 return false;
68 }
69
70 if (DBG_ENABLED(ETNA_DBG_LINKER_MSGS)) {
71 debug_printf("link result:\n");
72 debug_printf(" vs -> fs comps use pa_attr\n");
73
74 for (int idx = 0; idx < link.num_varyings; ++idx)
75 debug_printf(" t%-2u -> t%-2u %-5.*s %u,%u,%u,%u 0x%08x\n",
76 link.varyings[idx].reg, idx + 1,
77 link.varyings[idx].num_components, "xyzw",
78 link.varyings[idx].use[0], link.varyings[idx].use[1],
79 link.varyings[idx].use[2], link.varyings[idx].use[3],
80 link.varyings[idx].pa_attributes);
81 }
82
83 /* set last_varying_2x flag if the last varying has 1 or 2 components */
84 bool last_varying_2x = false;
85 if (link.num_varyings > 0 && link.varyings[link.num_varyings - 1].num_components <= 2)
86 last_varying_2x = true;
87
88 cs->RA_CONTROL = VIVS_RA_CONTROL_UNK0 |
89 COND(last_varying_2x, VIVS_RA_CONTROL_LAST_VARYING_2X);
90
91 cs->PA_ATTRIBUTE_ELEMENT_COUNT = VIVS_PA_ATTRIBUTE_ELEMENT_COUNT_COUNT(link.num_varyings);
92 for (int idx = 0; idx < link.num_varyings; ++idx)
93 cs->PA_SHADER_ATTRIBUTES[idx] = link.varyings[idx].pa_attributes;
94
95 cs->VS_END_PC = vs->code_size / 4;
96 cs->VS_OUTPUT_COUNT = 1 + link.num_varyings; /* position + varyings */
97
98 /* vs outputs (varyings) */
99 DEFINE_ETNA_BITARRAY(vs_output, 16, 8) = {0};
100 int varid = 0;
101 etna_bitarray_set(vs_output, 8, varid++, vs->vs_pos_out_reg);
102 for (int idx = 0; idx < link.num_varyings; ++idx)
103 etna_bitarray_set(vs_output, 8, varid++, link.varyings[idx].reg);
104 if (vs->vs_pointsize_out_reg >= 0)
105 etna_bitarray_set(vs_output, 8, varid++, vs->vs_pointsize_out_reg); /* pointsize is last */
106
107 for (int idx = 0; idx < ARRAY_SIZE(cs->VS_OUTPUT); ++idx)
108 cs->VS_OUTPUT[idx] = vs_output[idx];
109
110 if (vs->vs_pointsize_out_reg != -1) {
111 /* vertex shader outputs point coordinate, provide extra output and make
112 * sure PA config is
113 * not masked */
114 cs->PA_CONFIG = ~0;
115 cs->VS_OUTPUT_COUNT_PSIZE = cs->VS_OUTPUT_COUNT + 1;
116 } else {
117 /* vertex shader does not output point coordinate, make sure thate
118 * POINT_SIZE_ENABLE is masked
119 * and no extra output is given */
120 cs->PA_CONFIG = ~VIVS_PA_CONFIG_POINT_SIZE_ENABLE;
121 cs->VS_OUTPUT_COUNT_PSIZE = cs->VS_OUTPUT_COUNT;
122 }
123
124 cs->VS_LOAD_BALANCING = vs->vs_load_balancing;
125 cs->VS_START_PC = 0;
126
127 cs->PS_END_PC = fs->code_size / 4;
128 cs->PS_OUTPUT_REG = fs->ps_color_out_reg;
129 cs->PS_INPUT_COUNT =
130 VIVS_PS_INPUT_COUNT_COUNT(link.num_varyings + 1) | /* Number of inputs plus position */
131 VIVS_PS_INPUT_COUNT_UNK8(fs->input_count_unk8);
132 cs->PS_TEMP_REGISTER_CONTROL =
133 VIVS_PS_TEMP_REGISTER_CONTROL_NUM_TEMPS(MAX2(fs->num_temps, link.num_varyings + 1));
134 cs->PS_CONTROL = VIVS_PS_CONTROL_UNK1; /* XXX when can we set BYPASS? */
135 cs->PS_START_PC = 0;
136
137 /* Precompute PS_INPUT_COUNT and TEMP_REGISTER_CONTROL in the case of MSAA
138 * mode, avoids some fumbling in sync_context. */
139 cs->PS_INPUT_COUNT_MSAA =
140 VIVS_PS_INPUT_COUNT_COUNT(link.num_varyings + 2) | /* MSAA adds another input */
141 VIVS_PS_INPUT_COUNT_UNK8(fs->input_count_unk8);
142 cs->PS_TEMP_REGISTER_CONTROL_MSAA =
143 VIVS_PS_TEMP_REGISTER_CONTROL_NUM_TEMPS(MAX2(fs->num_temps, link.num_varyings + 2));
144
145 uint32_t total_components = 0;
146 DEFINE_ETNA_BITARRAY(num_components, ETNA_NUM_VARYINGS, 4) = {0};
147 DEFINE_ETNA_BITARRAY(component_use, 4 * ETNA_NUM_VARYINGS, 2) = {0};
148 for (int idx = 0; idx < link.num_varyings; ++idx) {
149 const struct etna_varying *varying = &link.varyings[idx];
150
151 etna_bitarray_set(num_components, 4, idx, varying->num_components);
152 for (int comp = 0; comp < varying->num_components; ++comp) {
153 etna_bitarray_set(component_use, 2, total_components, varying->use[comp]);
154 total_components += 1;
155 }
156 }
157
158 cs->GL_VARYING_TOTAL_COMPONENTS =
159 VIVS_GL_VARYING_TOTAL_COMPONENTS_NUM(align(total_components, 2));
160 cs->GL_VARYING_NUM_COMPONENTS = num_components[0];
161 cs->GL_VARYING_COMPONENT_USE[0] = component_use[0];
162 cs->GL_VARYING_COMPONENT_USE[1] = component_use[1];
163
164 /* reference instruction memory */
165 cs->vs_inst_mem_size = vs->code_size;
166 cs->VS_INST_MEM = vs->code;
167 cs->ps_inst_mem_size = fs->code_size;
168 cs->PS_INST_MEM = fs->code;
169
170 return true;
171 }
172
173 bool
174 etna_shader_link(struct etna_context *ctx)
175 {
176 if (!ctx->shader.vs || !ctx->shader.fs)
177 return false;
178
179 /* re-link vs and fs if needed */
180 return etna_link_shaders(ctx, &ctx->shader_state, ctx->shader.vs, ctx->shader.fs);
181 }
182
183 static bool
184 etna_shader_update_vs_inputs(struct etna_context *ctx,
185 struct compiled_shader_state *cs,
186 const struct etna_shader_variant *vs,
187 const struct compiled_vertex_elements_state *ves)
188 {
189 unsigned num_temps, cur_temp, num_vs_inputs;
190
191 if (!vs)
192 return false;
193
194 /* Number of vertex elements determines number of VS inputs. Otherwise,
195 * the GPU crashes. Allocate any unused vertex elements to VS temporary
196 * registers. */
197 num_vs_inputs = MAX2(ves->num_elements, vs->infile.num_reg);
198 if (num_vs_inputs != ves->num_elements) {
199 BUG("Number of elements %u does not match the number of VS inputs %zu",
200 ctx->vertex_elements->num_elements, ctx->shader.vs->infile.num_reg);
201 return false;
202 }
203
204 cur_temp = vs->num_temps;
205 num_temps = num_vs_inputs - vs->infile.num_reg + cur_temp;
206
207 cs->VS_INPUT_COUNT = VIVS_VS_INPUT_COUNT_COUNT(num_vs_inputs) |
208 VIVS_VS_INPUT_COUNT_UNK8(vs->input_count_unk8);
209 cs->VS_TEMP_REGISTER_CONTROL =
210 VIVS_VS_TEMP_REGISTER_CONTROL_NUM_TEMPS(num_temps);
211
212 /* vs inputs (attributes) */
213 DEFINE_ETNA_BITARRAY(vs_input, 16, 8) = {0};
214 for (int idx = 0; idx < num_vs_inputs; ++idx) {
215 if (idx < vs->infile.num_reg)
216 etna_bitarray_set(vs_input, 8, idx, vs->infile.reg[idx].reg);
217 else
218 etna_bitarray_set(vs_input, 8, idx, cur_temp++);
219 }
220
221 for (int idx = 0; idx < ARRAY_SIZE(cs->VS_INPUT); ++idx)
222 cs->VS_INPUT[idx] = vs_input[idx];
223
224 return true;
225 }
226
227 static inline const char *
228 etna_shader_stage(struct etna_shader_variant *shader)
229 {
230 switch (shader->processor) {
231 case PIPE_SHADER_VERTEX: return "VERT";
232 case PIPE_SHADER_FRAGMENT: return "FRAG";
233 case PIPE_SHADER_COMPUTE: return "CL";
234 default:
235 unreachable("invalid type");
236 return NULL;
237 }
238 }
239
240 static void
241 dump_shader_info(struct etna_shader_variant *shader, struct pipe_debug_callback *debug)
242 {
243 if (!unlikely(etna_mesa_debug & ETNA_DBG_SHADERDB))
244 return;
245
246 pipe_debug_message(debug, SHADER_INFO, "\n"
247 "SHADER-DB: %s prog %d: %u instructions %u temps\n"
248 "SHADER-DB: %s prog %d: %u immediates %u consts\n"
249 "SHADER-DB: %s prog %d: %u loops\n",
250 etna_shader_stage(shader),
251 shader->id,
252 shader->code_size,
253 shader->num_temps,
254 etna_shader_stage(shader),
255 shader->id,
256 shader->uniforms.imm_count,
257 shader->uniforms.const_count,
258 etna_shader_stage(shader),
259 shader->id,
260 shader->num_loops);
261 }
262
263 bool
264 etna_shader_update_vertex(struct etna_context *ctx)
265 {
266 return etna_shader_update_vs_inputs(ctx, &ctx->shader_state, ctx->shader.vs,
267 ctx->vertex_elements);
268 }
269
270 static struct etna_shader_variant *
271 create_variant(struct etna_shader *shader)
272 {
273 struct etna_shader_variant *v;
274
275 v = etna_compile_shader(shader->specs, shader->tokens);
276 if (!v) {
277 debug_error("compile failed!");
278 return NULL;
279 }
280
281 v->id = ++shader->variant_count;
282
283 return v;
284 }
285
286 static void *
287 etna_create_shader_state(struct pipe_context *pctx,
288 const struct pipe_shader_state *pss)
289 {
290 struct etna_context *ctx = etna_context(pctx);
291 struct etna_shader *shader = CALLOC_STRUCT(etna_shader);
292
293 if (!shader)
294 return NULL;
295
296 static uint32_t id;
297 shader->id = id++;
298 shader->specs = &ctx->specs;
299 shader->tokens = tgsi_dup_tokens(pss->tokens);
300
301 /* compile new variant */
302 struct etna_shader_variant *v;
303
304 v = create_variant(shader);
305 if (v) {
306 v->next = shader->variants;
307 shader->variants = v;
308 dump_shader_info(v, &ctx->debug);
309 }
310
311 return v;
312 }
313
314 static void
315 etna_delete_shader_state(struct pipe_context *pctx, void *ss)
316 {
317 struct etna_shader *shader = ss;
318 struct etna_shader_variant *v, *t;
319
320 v = shader->variants;
321 while (v) {
322 t = v;
323 v = v->next;
324 etna_destroy_shader(t);
325 }
326
327 FREE(shader->tokens);
328 FREE(shader);
329 }
330
331 static void
332 etna_bind_fs_state(struct pipe_context *pctx, void *fss_)
333 {
334 struct etna_context *ctx = etna_context(pctx);
335 struct etna_shader_variant *fss = fss_;
336
337 if (ctx->shader.fs == fss) /* skip if already bound */
338 return;
339
340 assert(fss == NULL || fss->processor == PIPE_SHADER_FRAGMENT);
341 ctx->shader.fs = fss;
342 ctx->dirty |= ETNA_DIRTY_SHADER;
343 }
344
345 static void
346 etna_bind_vs_state(struct pipe_context *pctx, void *vss_)
347 {
348 struct etna_context *ctx = etna_context(pctx);
349 struct etna_shader_variant *vss = vss_;
350
351 if (ctx->shader.vs == vss) /* skip if already bound */
352 return;
353
354 assert(vss == NULL || vss->processor == PIPE_SHADER_VERTEX);
355 ctx->shader.vs = vss;
356 ctx->dirty |= ETNA_DIRTY_SHADER;
357 }
358
359 void
360 etna_shader_init(struct pipe_context *pctx)
361 {
362 pctx->create_fs_state = etna_create_shader_state;
363 pctx->bind_fs_state = etna_bind_fs_state;
364 pctx->delete_fs_state = etna_delete_shader_state;
365 pctx->create_vs_state = etna_create_shader_state;
366 pctx->bind_vs_state = etna_bind_vs_state;
367 pctx->delete_vs_state = etna_delete_shader_state;
368 }