etnaviv: split dump_shader
[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_screen.h"
33 #include "etnaviv_util.h"
34
35 #include "tgsi/tgsi_parse.h"
36 #include "nir/tgsi_to_nir.h"
37 #include "util/u_math.h"
38 #include "util/u_memory.h"
39
40 /* Upload shader code to bo, if not already done */
41 static bool etna_icache_upload_shader(struct etna_context *ctx, struct etna_shader_variant *v)
42 {
43 if (v->bo)
44 return true;
45 v->bo = etna_bo_new(ctx->screen->dev, v->code_size*4, DRM_ETNA_GEM_CACHE_WC);
46 if (!v->bo)
47 return false;
48
49 void *buf = etna_bo_map(v->bo);
50 etna_bo_cpu_prep(v->bo, DRM_ETNA_PREP_WRITE);
51 memcpy(buf, v->code, v->code_size*4);
52 etna_bo_cpu_fini(v->bo);
53 DBG("Uploaded %s of %u words to bo %p", v->stage == MESA_SHADER_FRAGMENT ? "fs":"vs", v->code_size, v->bo);
54 return true;
55 }
56
57 /* Link vs and fs together: fill in shader_state from vs and fs
58 * as this function is called every time a new fs or vs is bound, the goal is to
59 * do little processing as possible here, and to precompute as much as possible in
60 * the vs/fs shader_object.
61 *
62 * XXX we could cache the link result for a certain set of VS/PS; usually a pair
63 * of VS and PS will be used together anyway.
64 */
65 static bool
66 etna_link_shaders(struct etna_context *ctx, struct compiled_shader_state *cs,
67 struct etna_shader_variant *vs, struct etna_shader_variant *fs)
68 {
69 struct etna_shader_link_info link = { };
70
71 assert(vs->stage == MESA_SHADER_VERTEX);
72 assert(fs->stage == MESA_SHADER_FRAGMENT);
73
74 #ifdef DEBUG
75 if (DBG_ENABLED(ETNA_DBG_DUMP_SHADERS)) {
76 if (DBG_ENABLED(ETNA_DBG_NIR)) {
77 etna_dump_shader_nir(vs);
78 etna_dump_shader_nir(fs);
79 } else {
80 etna_dump_shader(vs);
81 etna_dump_shader(fs);
82 }
83 }
84 #endif
85
86 if (etna_link_shader(&link, vs, fs)) {
87 /* linking failed: some fs inputs do not have corresponding
88 * vs outputs */
89 assert(0);
90
91 return false;
92 }
93
94 if (DBG_ENABLED(ETNA_DBG_LINKER_MSGS)) {
95 debug_printf("link result:\n");
96 debug_printf(" vs -> fs comps use pa_attr\n");
97
98 for (int idx = 0; idx < link.num_varyings; ++idx)
99 debug_printf(" t%-2u -> t%-2u %-5.*s %u,%u,%u,%u 0x%08x\n",
100 link.varyings[idx].reg, idx + 1,
101 link.varyings[idx].num_components, "xyzw",
102 link.varyings[idx].use[0], link.varyings[idx].use[1],
103 link.varyings[idx].use[2], link.varyings[idx].use[3],
104 link.varyings[idx].pa_attributes);
105 }
106
107 /* set last_varying_2x flag if the last varying has 1 or 2 components */
108 bool last_varying_2x = false;
109 if (link.num_varyings > 0 && link.varyings[link.num_varyings - 1].num_components <= 2)
110 last_varying_2x = true;
111
112 cs->RA_CONTROL = VIVS_RA_CONTROL_UNK0 |
113 COND(last_varying_2x, VIVS_RA_CONTROL_LAST_VARYING_2X);
114
115 cs->PA_ATTRIBUTE_ELEMENT_COUNT = VIVS_PA_ATTRIBUTE_ELEMENT_COUNT_COUNT(link.num_varyings);
116 for (int idx = 0; idx < link.num_varyings; ++idx)
117 cs->PA_SHADER_ATTRIBUTES[idx] = link.varyings[idx].pa_attributes;
118
119 cs->VS_END_PC = vs->code_size / 4;
120 cs->VS_OUTPUT_COUNT = 1 + link.num_varyings; /* position + varyings */
121
122 /* vs outputs (varyings) */
123 DEFINE_ETNA_BITARRAY(vs_output, 16, 8) = {0};
124 int varid = 0;
125 etna_bitarray_set(vs_output, 8, varid++, vs->vs_pos_out_reg);
126 for (int idx = 0; idx < link.num_varyings; ++idx)
127 etna_bitarray_set(vs_output, 8, varid++, link.varyings[idx].reg);
128 if (vs->vs_pointsize_out_reg >= 0)
129 etna_bitarray_set(vs_output, 8, varid++, vs->vs_pointsize_out_reg); /* pointsize is last */
130
131 for (int idx = 0; idx < ARRAY_SIZE(cs->VS_OUTPUT); ++idx)
132 cs->VS_OUTPUT[idx] = vs_output[idx];
133
134 if (vs->vs_pointsize_out_reg != -1) {
135 /* vertex shader outputs point coordinate, provide extra output and make
136 * sure PA config is
137 * not masked */
138 cs->PA_CONFIG = ~0;
139 cs->VS_OUTPUT_COUNT_PSIZE = cs->VS_OUTPUT_COUNT + 1;
140 } else {
141 /* vertex shader does not output point coordinate, make sure thate
142 * POINT_SIZE_ENABLE is masked
143 * and no extra output is given */
144 cs->PA_CONFIG = ~VIVS_PA_CONFIG_POINT_SIZE_ENABLE;
145 cs->VS_OUTPUT_COUNT_PSIZE = cs->VS_OUTPUT_COUNT;
146 }
147
148 cs->VS_LOAD_BALANCING = vs->vs_load_balancing;
149 cs->VS_START_PC = 0;
150
151 cs->PS_END_PC = fs->code_size / 4;
152 cs->PS_OUTPUT_REG = fs->ps_color_out_reg;
153 cs->PS_INPUT_COUNT =
154 VIVS_PS_INPUT_COUNT_COUNT(link.num_varyings + 1) | /* Number of inputs plus position */
155 VIVS_PS_INPUT_COUNT_UNK8(fs->input_count_unk8);
156 cs->PS_TEMP_REGISTER_CONTROL =
157 VIVS_PS_TEMP_REGISTER_CONTROL_NUM_TEMPS(MAX2(fs->num_temps, link.num_varyings + 1));
158 cs->PS_CONTROL = VIVS_PS_CONTROL_UNK1; /* XXX when can we set BYPASS? */
159 cs->PS_START_PC = 0;
160
161 /* Precompute PS_INPUT_COUNT and TEMP_REGISTER_CONTROL in the case of MSAA
162 * mode, avoids some fumbling in sync_context. */
163 cs->PS_INPUT_COUNT_MSAA =
164 VIVS_PS_INPUT_COUNT_COUNT(link.num_varyings + 2) | /* MSAA adds another input */
165 VIVS_PS_INPUT_COUNT_UNK8(fs->input_count_unk8);
166 cs->PS_TEMP_REGISTER_CONTROL_MSAA =
167 VIVS_PS_TEMP_REGISTER_CONTROL_NUM_TEMPS(MAX2(fs->num_temps, link.num_varyings + 2));
168
169 uint32_t total_components = 0;
170 DEFINE_ETNA_BITARRAY(num_components, ETNA_NUM_VARYINGS, 4) = {0};
171 DEFINE_ETNA_BITARRAY(component_use, 4 * ETNA_NUM_VARYINGS, 2) = {0};
172 for (int idx = 0; idx < link.num_varyings; ++idx) {
173 const struct etna_varying *varying = &link.varyings[idx];
174
175 etna_bitarray_set(num_components, 4, idx, varying->num_components);
176 for (int comp = 0; comp < varying->num_components; ++comp) {
177 etna_bitarray_set(component_use, 2, total_components, varying->use[comp]);
178 total_components += 1;
179 }
180 }
181
182 cs->GL_VARYING_TOTAL_COMPONENTS =
183 VIVS_GL_VARYING_TOTAL_COMPONENTS_NUM(align(total_components, 2));
184 cs->GL_VARYING_NUM_COMPONENTS = num_components[0];
185 cs->GL_VARYING_COMPONENT_USE[0] = component_use[0];
186 cs->GL_VARYING_COMPONENT_USE[1] = component_use[1];
187
188 cs->GL_HALTI5_SH_SPECIALS =
189 0x7f7f0000 | /* unknown bits, probably other PS inputs */
190 /* pointsize is last (see above) */
191 VIVS_GL_HALTI5_SH_SPECIALS_VS_PSIZE_OUT((vs->vs_pointsize_out_reg != -1) ?
192 cs->VS_OUTPUT_COUNT * 4 : 0x00) |
193 VIVS_GL_HALTI5_SH_SPECIALS_PS_PCOORD_IN((link.pcoord_varying_comp_ofs != -1) ?
194 link.pcoord_varying_comp_ofs : 0x7f);
195
196 /* reference instruction memory */
197 cs->vs_inst_mem_size = vs->code_size;
198 cs->VS_INST_MEM = vs->code;
199
200 cs->ps_inst_mem_size = fs->code_size;
201 cs->PS_INST_MEM = fs->code;
202
203 if (vs->needs_icache | fs->needs_icache) {
204 /* If either of the shaders needs ICACHE, we use it for both. It is
205 * either switched on or off for the entire shader processor.
206 */
207 if (!etna_icache_upload_shader(ctx, vs) ||
208 !etna_icache_upload_shader(ctx, fs)) {
209 assert(0);
210 return false;
211 }
212
213 cs->VS_INST_ADDR.bo = vs->bo;
214 cs->VS_INST_ADDR.offset = 0;
215 cs->VS_INST_ADDR.flags = ETNA_RELOC_READ;
216 cs->PS_INST_ADDR.bo = fs->bo;
217 cs->PS_INST_ADDR.offset = 0;
218 cs->PS_INST_ADDR.flags = ETNA_RELOC_READ;
219 } else {
220 /* clear relocs */
221 memset(&cs->VS_INST_ADDR, 0, sizeof(cs->VS_INST_ADDR));
222 memset(&cs->PS_INST_ADDR, 0, sizeof(cs->PS_INST_ADDR));
223 }
224
225 return true;
226 }
227
228 bool
229 etna_shader_link(struct etna_context *ctx)
230 {
231 if (!ctx->shader.vs || !ctx->shader.fs)
232 return false;
233
234 /* re-link vs and fs if needed */
235 return etna_link_shaders(ctx, &ctx->shader_state, ctx->shader.vs, ctx->shader.fs);
236 }
237
238 static bool
239 etna_shader_update_vs_inputs(struct compiled_shader_state *cs,
240 const struct etna_shader_variant *vs,
241 const struct compiled_vertex_elements_state *ves)
242 {
243 unsigned num_temps, cur_temp, num_vs_inputs;
244
245 if (!vs)
246 return false;
247
248 /* Number of vertex elements determines number of VS inputs. Otherwise,
249 * the GPU crashes. Allocate any unused vertex elements to VS temporary
250 * registers. */
251 num_vs_inputs = MAX2(ves->num_elements, vs->infile.num_reg);
252 if (num_vs_inputs != ves->num_elements) {
253 BUG("Number of elements %u does not match the number of VS inputs %zu",
254 ves->num_elements, vs->infile.num_reg);
255 return false;
256 }
257
258 cur_temp = vs->num_temps;
259 num_temps = num_vs_inputs - vs->infile.num_reg + cur_temp;
260
261 cs->VS_INPUT_COUNT = VIVS_VS_INPUT_COUNT_COUNT(num_vs_inputs) |
262 VIVS_VS_INPUT_COUNT_UNK8(vs->input_count_unk8);
263 cs->VS_TEMP_REGISTER_CONTROL =
264 VIVS_VS_TEMP_REGISTER_CONTROL_NUM_TEMPS(num_temps);
265
266 /* vs inputs (attributes) */
267 DEFINE_ETNA_BITARRAY(vs_input, 16, 8) = {0};
268 for (int idx = 0; idx < num_vs_inputs; ++idx) {
269 if (idx < vs->infile.num_reg)
270 etna_bitarray_set(vs_input, 8, idx, vs->infile.reg[idx].reg);
271 else
272 etna_bitarray_set(vs_input, 8, idx, cur_temp++);
273 }
274
275 for (int idx = 0; idx < ARRAY_SIZE(cs->VS_INPUT); ++idx)
276 cs->VS_INPUT[idx] = vs_input[idx];
277
278 return true;
279 }
280
281 static inline const char *
282 etna_shader_stage(struct etna_shader_variant *shader)
283 {
284 switch (shader->stage) {
285 case MESA_SHADER_VERTEX: return "VERT";
286 case MESA_SHADER_FRAGMENT: return "FRAG";
287 case MESA_SHADER_COMPUTE: return "CL";
288 default:
289 unreachable("invalid type");
290 return NULL;
291 }
292 }
293
294 static void
295 dump_shader_info(struct etna_shader_variant *v, struct pipe_debug_callback *debug)
296 {
297 if (!unlikely(etna_mesa_debug & ETNA_DBG_SHADERDB))
298 return;
299
300 pipe_debug_message(debug, SHADER_INFO, "\n"
301 "SHADER-DB: %s prog %d/%d: %u instructions %u temps\n"
302 "SHADER-DB: %s prog %d/%d: %u immediates %u loops\n",
303 etna_shader_stage(v),
304 v->shader->id, v->id,
305 v->code_size,
306 v->num_temps,
307 etna_shader_stage(v),
308 v->shader->id, v->id,
309 v->uniforms.imm_count,
310 v->num_loops);
311 }
312
313 bool
314 etna_shader_update_vertex(struct etna_context *ctx)
315 {
316 return etna_shader_update_vs_inputs(&ctx->shader_state, ctx->shader.vs,
317 ctx->vertex_elements);
318 }
319
320 static struct etna_shader_variant *
321 create_variant(struct etna_shader *shader, struct etna_shader_key key)
322 {
323 struct etna_shader_variant *v = CALLOC_STRUCT(etna_shader_variant);
324 int ret;
325
326 if (!v)
327 return NULL;
328
329 v->shader = shader;
330 v->key = key;
331
332 ret = etna_compile_shader(v);
333 if (!ret) {
334 debug_error("compile failed!");
335 goto fail;
336 }
337
338 v->id = ++shader->variant_count;
339
340 return v;
341
342 fail:
343 FREE(v);
344 return NULL;
345 }
346
347 struct etna_shader_variant *
348 etna_shader_variant(struct etna_shader *shader, struct etna_shader_key key,
349 struct pipe_debug_callback *debug)
350 {
351 struct etna_shader_variant *v;
352
353 for (v = shader->variants; v; v = v->next)
354 if (etna_shader_key_equal(&key, &v->key))
355 return v;
356
357 /* compile new variant if it doesn't exist already */
358 v = create_variant(shader, key);
359 if (v) {
360 v->next = shader->variants;
361 shader->variants = v;
362 dump_shader_info(v, debug);
363 }
364
365 return v;
366 }
367
368 static void *
369 etna_create_shader_state(struct pipe_context *pctx,
370 const struct pipe_shader_state *pss)
371 {
372 struct etna_context *ctx = etna_context(pctx);
373 struct etna_shader *shader = CALLOC_STRUCT(etna_shader);
374
375 if (!shader)
376 return NULL;
377
378 static uint32_t id;
379 shader->id = id++;
380 shader->specs = &ctx->specs;
381
382 if (DBG_ENABLED(ETNA_DBG_NIR))
383 shader->nir = (pss->type == PIPE_SHADER_IR_NIR) ? pss->ir.nir :
384 tgsi_to_nir(pss->tokens, pctx->screen);
385 else
386 shader->tokens = tgsi_dup_tokens(pss->tokens);
387
388
389
390 if (etna_mesa_debug & ETNA_DBG_SHADERDB) {
391 /* if shader-db run, create a standard variant immediately
392 * (as otherwise nothing will trigger the shader to be
393 * actually compiled).
394 */
395 struct etna_shader_key key = {};
396 etna_shader_variant(shader, key, &ctx->debug);
397 }
398
399 return shader;
400 }
401
402 static void
403 etna_delete_shader_state(struct pipe_context *pctx, void *ss)
404 {
405 struct etna_shader *shader = ss;
406 struct etna_shader_variant *v, *t;
407
408 v = shader->variants;
409 while (v) {
410 t = v;
411 v = v->next;
412 if (t->bo)
413 etna_bo_del(t->bo);
414 etna_destroy_shader(t);
415 }
416
417 ralloc_free(shader->nir);
418 FREE(shader);
419 }
420
421 static void
422 etna_bind_fs_state(struct pipe_context *pctx, void *hwcso)
423 {
424 struct etna_context *ctx = etna_context(pctx);
425
426 ctx->shader.bind_fs = hwcso;
427 ctx->dirty |= ETNA_DIRTY_SHADER;
428 }
429
430 static void
431 etna_bind_vs_state(struct pipe_context *pctx, void *hwcso)
432 {
433 struct etna_context *ctx = etna_context(pctx);
434
435 ctx->shader.bind_vs = hwcso;
436 ctx->dirty |= ETNA_DIRTY_SHADER;
437 }
438
439 void
440 etna_shader_init(struct pipe_context *pctx)
441 {
442 pctx->create_fs_state = etna_create_shader_state;
443 pctx->bind_fs_state = etna_bind_fs_state;
444 pctx->delete_fs_state = etna_delete_shader_state;
445 pctx->create_vs_state = etna_create_shader_state;
446 pctx->bind_vs_state = etna_bind_vs_state;
447 pctx->delete_vs_state = etna_delete_shader_state;
448 }