nv50: prepare for having multiple functions
[mesa.git] / src / gallium / drivers / nv50 / nv50_program.c
1 /*
2 * Copyright 2010 Chrsitoph Bumiller
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 shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
18 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
19 * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 * SOFTWARE.
21 */
22
23 /* #define NV50_PROGRAM_DEBUG */
24
25 #include "nv50_program.h"
26 #include "nv50_pc.h"
27 #include "nv50_context.h"
28
29 #include "pipe/p_shader_tokens.h"
30 #include "tgsi/tgsi_parse.h"
31 #include "tgsi/tgsi_util.h"
32 #include "tgsi/tgsi_dump.h"
33
34 static INLINE unsigned
35 bitcount4(const uint32_t val)
36 {
37 static const unsigned cnt[16]
38 = { 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4 };
39 return cnt[val & 0xf];
40 }
41
42 static unsigned
43 nv50_tgsi_src_mask(const struct tgsi_full_instruction *inst, int c)
44 {
45 unsigned mask = inst->Dst[0].Register.WriteMask;
46
47 switch (inst->Instruction.Opcode) {
48 case TGSI_OPCODE_COS:
49 case TGSI_OPCODE_SIN:
50 return (mask & 0x8) | ((mask & 0x7) ? 0x1 : 0x0);
51 case TGSI_OPCODE_DP3:
52 return 0x7;
53 case TGSI_OPCODE_DP4:
54 case TGSI_OPCODE_DPH:
55 case TGSI_OPCODE_KIL: /* WriteMask ignored */
56 return 0xf;
57 case TGSI_OPCODE_DST:
58 return mask & (c ? 0xa : 0x6);
59 case TGSI_OPCODE_EX2:
60 case TGSI_OPCODE_EXP:
61 case TGSI_OPCODE_LG2:
62 case TGSI_OPCODE_LOG:
63 case TGSI_OPCODE_POW:
64 case TGSI_OPCODE_RCP:
65 case TGSI_OPCODE_RSQ:
66 case TGSI_OPCODE_SCS:
67 return 0x1;
68 case TGSI_OPCODE_IF:
69 return 0x1;
70 case TGSI_OPCODE_LIT:
71 return 0xb;
72 case TGSI_OPCODE_TEX:
73 case TGSI_OPCODE_TXB:
74 case TGSI_OPCODE_TXL:
75 case TGSI_OPCODE_TXP:
76 {
77 const struct tgsi_instruction_texture *tex;
78
79 assert(inst->Instruction.Texture);
80 tex = &inst->Texture;
81
82 mask = 0x7;
83 if (inst->Instruction.Opcode != TGSI_OPCODE_TEX &&
84 inst->Instruction.Opcode != TGSI_OPCODE_TXD)
85 mask |= 0x8; /* bias, lod or proj */
86
87 switch (tex->Texture) {
88 case TGSI_TEXTURE_1D:
89 mask &= 0x9;
90 break;
91 case TGSI_TEXTURE_SHADOW1D:
92 mask &= 0x5;
93 break;
94 case TGSI_TEXTURE_2D:
95 mask &= 0xb;
96 break;
97 default:
98 break;
99 }
100 }
101 return mask;
102 case TGSI_OPCODE_XPD:
103 {
104 unsigned x = 0;
105 if (mask & 1) x |= 0x6;
106 if (mask & 2) x |= 0x5;
107 if (mask & 4) x |= 0x3;
108 return x;
109 }
110 default:
111 break;
112 }
113
114 return mask;
115 }
116
117 static void
118 nv50_indirect_inputs(struct nv50_translation_info *ti, int id)
119 {
120 int i, c;
121
122 for (i = 0; i < PIPE_MAX_SHADER_INPUTS; ++i)
123 for (c = 0; c < 4; ++c)
124 ti->input_access[i][c] = id;
125
126 ti->indirect_inputs = TRUE;
127 }
128
129 static void
130 nv50_indirect_outputs(struct nv50_translation_info *ti, int id)
131 {
132 int i, c;
133
134 for (i = 0; i < PIPE_MAX_SHADER_OUTPUTS; ++i)
135 for (c = 0; c < 4; ++c)
136 ti->output_access[i][c] = id;
137
138 ti->indirect_outputs = TRUE;
139 }
140
141 static void
142 prog_inst(struct nv50_translation_info *ti,
143 const struct tgsi_full_instruction *inst, int id)
144 {
145 const struct tgsi_dst_register *dst;
146 const struct tgsi_src_register *src;
147 int s, c, k;
148 unsigned mask;
149
150 if (inst->Instruction.Opcode == TGSI_OPCODE_BGNSUB) {
151 ti->subr[ti->subr_nr].pos = id - 1;
152 ti->subr[ti->subr_nr].id = ti->subr_nr + 1; /* id 0 is main program */
153 ++ti->subr_nr;
154 }
155
156 if (inst->Dst[0].Register.File == TGSI_FILE_OUTPUT) {
157 dst = &inst->Dst[0].Register;
158
159 for (c = 0; c < 4; ++c) {
160 if (dst->Indirect)
161 nv50_indirect_outputs(ti, id);
162 if (!(dst->WriteMask & (1 << c)))
163 continue;
164 ti->output_access[dst->Index][c] = id;
165 }
166
167 if (inst->Instruction.Opcode == TGSI_OPCODE_MOV &&
168 inst->Src[0].Register.File == TGSI_FILE_INPUT &&
169 dst->Index == ti->edgeflag_out)
170 ti->p->vp.edgeflag = inst->Src[0].Register.Index;
171 }
172
173 for (s = 0; s < inst->Instruction.NumSrcRegs; ++s) {
174 src = &inst->Src[s].Register;
175 if (src->File != TGSI_FILE_INPUT)
176 continue;
177 mask = nv50_tgsi_src_mask(inst, s);
178
179 if (inst->Src[s].Register.Indirect)
180 nv50_indirect_inputs(ti, id);
181
182 for (c = 0; c < 4; ++c) {
183 if (!(mask & (1 << c)))
184 continue;
185 k = tgsi_util_get_full_src_register_swizzle(&inst->Src[s], c);
186 if (k <= TGSI_SWIZZLE_W)
187 ti->input_access[src->Index][k] = id;
188 }
189 }
190 }
191
192 /* Probably should introduce something like struct tgsi_function_declaration
193 * instead of trying to guess inputs/outputs.
194 */
195 static void
196 prog_subroutine_inst(struct nv50_subroutine *subr,
197 const struct tgsi_full_instruction *inst)
198 {
199 const struct tgsi_dst_register *dst;
200 const struct tgsi_src_register *src;
201 int s, c, k;
202 unsigned mask;
203
204 for (s = 0; s < inst->Instruction.NumSrcRegs; ++s) {
205 src = &inst->Src[s].Register;
206 if (src->File != TGSI_FILE_TEMPORARY)
207 continue;
208 mask = nv50_tgsi_src_mask(inst, s);
209
210 assert(!inst->Src[s].Register.Indirect);
211
212 for (c = 0; c < 4; ++c) {
213 k = tgsi_util_get_full_src_register_swizzle(&inst->Src[s], c);
214
215 if ((mask & (1 << c)) && k < TGSI_SWIZZLE_W)
216 if (!(subr->retv[src->Index / 32][k] & (1 << (src->Index % 32))))
217 subr->argv[src->Index / 32][k] |= 1 << (src->Index % 32);
218 }
219 }
220
221 if (inst->Dst[0].Register.File == TGSI_FILE_TEMPORARY) {
222 dst = &inst->Dst[0].Register;
223
224 for (c = 0; c < 4; ++c)
225 if (dst->WriteMask & (1 << c))
226 subr->retv[dst->Index / 32][c] |= 1 << (dst->Index % 32);
227 }
228 }
229
230 static void
231 prog_immediate(struct nv50_translation_info *ti,
232 const struct tgsi_full_immediate *imm)
233 {
234 int c;
235 unsigned n = ti->immd32_nr++;
236
237 assert(ti->immd32_nr <= ti->scan.immediate_count);
238
239 for (c = 0; c < 4; ++c)
240 ti->immd32[n * 4 + c] = imm->u[c].Uint;
241
242 ti->immd32_ty[n] = imm->Immediate.DataType;
243 }
244
245 static INLINE unsigned
246 translate_interpolate(const struct tgsi_full_declaration *decl)
247 {
248 unsigned mode;
249
250 if (decl->Declaration.Interpolate == TGSI_INTERPOLATE_CONSTANT)
251 mode = NV50_INTERP_FLAT;
252 else
253 if (decl->Declaration.Interpolate == TGSI_INTERPOLATE_PERSPECTIVE)
254 mode = 0;
255 else
256 mode = NV50_INTERP_LINEAR;
257
258 if (decl->Declaration.Centroid)
259 mode |= NV50_INTERP_CENTROID;
260
261 return mode;
262 }
263
264 static void
265 prog_decl(struct nv50_translation_info *ti,
266 const struct tgsi_full_declaration *decl)
267 {
268 unsigned i, first, last, sn = 0, si = 0;
269
270 first = decl->Range.First;
271 last = decl->Range.Last;
272
273 if (decl->Declaration.Semantic) {
274 sn = decl->Semantic.Name;
275 si = decl->Semantic.Index;
276 }
277
278 switch (decl->Declaration.File) {
279 case TGSI_FILE_INPUT:
280 for (i = first; i <= last; ++i)
281 ti->interp_mode[i] = translate_interpolate(decl);
282
283 if (!decl->Declaration.Semantic)
284 break;
285
286 for (i = first; i <= last; ++i) {
287 ti->p->in[i].sn = sn;
288 ti->p->in[i].si = si;
289 }
290
291 switch (sn) {
292 case TGSI_SEMANTIC_FACE:
293 break;
294 case TGSI_SEMANTIC_COLOR:
295 if (ti->p->type == PIPE_SHADER_FRAGMENT)
296 ti->p->vp.bfc[si] = first;
297 break;
298 }
299 break;
300 case TGSI_FILE_OUTPUT:
301 if (!decl->Declaration.Semantic)
302 break;
303
304 for (i = first; i <= last; ++i) {
305 ti->p->out[i].sn = sn;
306 ti->p->out[i].si = si;
307 }
308
309 switch (sn) {
310 case TGSI_SEMANTIC_BCOLOR:
311 ti->p->vp.bfc[si] = first;
312 break;
313 case TGSI_SEMANTIC_PSIZE:
314 ti->p->vp.psiz = first;
315 break;
316 case TGSI_SEMANTIC_EDGEFLAG:
317 ti->edgeflag_out = first;
318 break;
319 default:
320 break;
321 }
322 break;
323 case TGSI_FILE_SYSTEM_VALUE:
324 switch (decl->Semantic.Name) {
325 case TGSI_SEMANTIC_FACE:
326 break;
327 case TGSI_SEMANTIC_INSTANCEID:
328 break;
329 case TGSI_SEMANTIC_PRIMID:
330 break;
331 /*
332 case TGSI_SEMANTIC_PRIMIDIN:
333 break;
334 case TGSI_SEMANTIC_VERTEXID:
335 break;
336 */
337 default:
338 break;
339 }
340 break;
341 case TGSI_FILE_CONSTANT:
342 ti->p->parm_size = MAX2(ti->p->parm_size, (last + 1) * 16);
343 break;
344 case TGSI_FILE_ADDRESS:
345 case TGSI_FILE_SAMPLER:
346 case TGSI_FILE_TEMPORARY:
347 break;
348 default:
349 assert(0);
350 break;
351 }
352 }
353
354 static int
355 nv50_vertprog_prepare(struct nv50_translation_info *ti)
356 {
357 struct nv50_program *p = ti->p;
358 int i, c;
359 unsigned num_inputs = 0;
360
361 ti->input_file = NV_FILE_MEM_S;
362 ti->output_file = NV_FILE_OUT;
363
364 for (i = 0; i <= ti->scan.file_max[TGSI_FILE_INPUT]; ++i) {
365 p->in[i].id = i;
366 p->in[i].hw = num_inputs;
367
368 for (c = 0; c < 4; ++c) {
369 if (!ti->input_access[i][c])
370 continue;
371 ti->input_map[i][c] = num_inputs++;
372 p->vp.attrs[(4 * i + c) / 32] |= 1 << ((i * 4 + c) % 32);
373 }
374 }
375
376 for (i = 0; i <= ti->scan.file_max[TGSI_FILE_OUTPUT]; ++i) {
377 p->out[i].id = i;
378 p->out[i].hw = p->max_out;
379
380 for (c = 0; c < 4; ++c) {
381 if (!ti->output_access[i][c])
382 continue;
383 ti->output_map[i][c] = p->max_out++;
384 p->out[i].mask |= 1 << c;
385 }
386 }
387
388 if (p->vp.psiz < 0x40)
389 p->vp.psiz = p->out[p->vp.psiz].hw;
390
391 return 0;
392 }
393
394 static int
395 nv50_fragprog_prepare(struct nv50_translation_info *ti)
396 {
397 struct nv50_program *p = ti->p;
398 int i, j, c;
399 unsigned nvary, nintp, depr;
400 unsigned n = 0, m = 0, skip = 0;
401 ubyte sn[16], si[16];
402
403 /* FP flags */
404
405 if (ti->scan.writes_z) {
406 p->fp.flags[1] = 0x11;
407 p->fp.flags[0] |= NV50TCL_FP_CONTROL_EXPORTS_Z;
408 }
409
410 if (ti->scan.uses_kill)
411 p->fp.flags[0] |= NV50TCL_FP_CONTROL_USES_KIL;
412
413 /* FP inputs */
414
415 ti->input_file = NV_FILE_MEM_V;
416 ti->output_file = NV_FILE_GPR;
417
418 /* count non-flat inputs, save semantic info */
419 for (i = 0; i < p->in_nr; ++i) {
420 m += (ti->interp_mode[i] & NV50_INTERP_FLAT) ? 0 : 1;
421 sn[i] = p->in[i].sn;
422 si[i] = p->in[i].si;
423 }
424
425 /* reorder p->in[] so that non-flat inputs are first and
426 * kick out special inputs that don't use VP/GP_RESULT_MAP
427 */
428 nintp = 0;
429 for (i = 0; i < p->in_nr; ++i) {
430 if (sn[i] == TGSI_SEMANTIC_POSITION) {
431 for (c = 0; c < 4; ++c) {
432 ti->input_map[i][c] = nintp;
433 if (ti->input_access[i][c]) {
434 p->fp.interp |= 1 << (24 + c);
435 ++nintp;
436 }
437 }
438 skip++;
439 continue;
440 } else
441 if (sn[i] == TGSI_SEMANTIC_FACE) {
442 ti->input_map[i][0] = 255;
443 skip++;
444 continue;
445 }
446
447 j = (ti->interp_mode[i] & NV50_INTERP_FLAT) ? m++ : n++;
448
449 if (sn[i] == TGSI_SEMANTIC_COLOR)
450 p->vp.bfc[si[i]] = j;
451
452 p->in[j].linear = (ti->interp_mode[i] & NV50_INTERP_LINEAR) ? 1 : 0;
453 p->in[j].id = i;
454 p->in[j].sn = sn[i];
455 p->in[j].si = si[i];
456 }
457 assert(n <= m);
458 p->in_nr -= skip;
459
460 if (!(p->fp.interp & (8 << 24))) {
461 p->fp.interp |= (8 << 24);
462 ++nintp;
463 }
464
465 p->fp.colors = (1 << 24) | 4; /* CLAMP, FFC0_ID = 4 */
466
467 for (i = 0; i < p->in_nr; ++i) {
468 int j = p->in[i].id;
469 p->in[i].hw = nintp;
470
471 for (c = 0; c < 4; ++c) {
472 if (!ti->input_access[j][c])
473 continue;
474 p->in[i].mask |= 1 << c;
475 ti->input_map[j][c] = nintp++;
476 }
477 /* count color inputs */
478 if (i == p->vp.bfc[0] || i == p->vp.bfc[1])
479 p->fp.colors += bitcount4(p->in[i].mask) << 16;
480 }
481 nintp -= bitcount4(p->fp.interp >> 24); /* subtract position inputs */
482 nvary = nintp;
483 if (n < m)
484 nvary -= p->in[n].hw;
485
486 p->fp.interp |= nvary << NV50TCL_FP_INTERPOLANT_CTRL_COUNT_NONFLAT_SHIFT;
487 p->fp.interp |= nintp << NV50TCL_FP_INTERPOLANT_CTRL_COUNT_SHIFT;
488
489 /* FP outputs */
490
491 if (p->out_nr > (1 + (ti->scan.writes_z ? 1 : 0)))
492 p->fp.flags[0] |= NV50TCL_FP_CONTROL_MULTIPLE_RESULTS;
493
494 depr = p->out_nr;
495 for (i = 0; i < p->out_nr; ++i) {
496 p->out[i].id = i;
497 if (p->out[i].sn == TGSI_SEMANTIC_POSITION) {
498 depr = i;
499 continue;
500 }
501 p->out[i].hw = p->max_out;
502 p->out[i].mask = 0xf;
503
504 for (c = 0; c < 4; ++c)
505 ti->output_map[i][c] = p->max_out++;
506 }
507 if (depr < p->out_nr) {
508 p->out[depr].mask = 0x4;
509 p->out[depr].hw = ti->output_map[depr][2] = p->max_out++;
510 }
511
512 return 0;
513 }
514
515 static int
516 nv50_geomprog_prepare(struct nv50_translation_info *ti)
517 {
518 ti->input_file = NV_FILE_MEM_S;
519 ti->output_file = NV_FILE_OUT;
520
521 assert(0);
522 return 1;
523 }
524
525 static int
526 nv50_prog_scan(struct nv50_translation_info *ti)
527 {
528 struct nv50_program *p = ti->p;
529 struct tgsi_parse_context parse;
530 int ret, i;
531
532 p->vp.edgeflag = 0x40;
533 p->vp.psiz = 0x40;
534 p->vp.bfc[0] = 0x40;
535 p->vp.bfc[1] = 0x40;
536 p->gp.primid = 0x80;
537
538 tgsi_scan_shader(p->pipe.tokens, &ti->scan);
539
540 #ifdef NV50_PROGRAM_DEBUG
541 tgsi_dump(p->pipe.tokens, 0);
542 #endif
543
544 ti->subr =
545 CALLOC(ti->scan.opcode_count[TGSI_OPCODE_BGNSUB], sizeof(ti->subr[0]));
546
547 ti->immd32 = (uint32_t *)MALLOC(ti->scan.immediate_count * 16);
548 ti->immd32_ty = (ubyte *)MALLOC(ti->scan.immediate_count * sizeof(ubyte));
549
550 ti->insns = MALLOC(ti->scan.num_instructions * sizeof(ti->insns[0]));
551
552 tgsi_parse_init(&parse, p->pipe.tokens);
553 while (!tgsi_parse_end_of_tokens(&parse)) {
554 tgsi_parse_token(&parse);
555
556 switch (parse.FullToken.Token.Type) {
557 case TGSI_TOKEN_TYPE_IMMEDIATE:
558 prog_immediate(ti, &parse.FullToken.FullImmediate);
559 break;
560 case TGSI_TOKEN_TYPE_DECLARATION:
561 prog_decl(ti, &parse.FullToken.FullDeclaration);
562 break;
563 case TGSI_TOKEN_TYPE_INSTRUCTION:
564 ti->insns[ti->inst_nr] = parse.FullToken.FullInstruction;
565 prog_inst(ti, &parse.FullToken.FullInstruction, ++ti->inst_nr);
566 break;
567 }
568 }
569
570 /* Scan to determine which registers are inputs/outputs of a subroutine. */
571 for (i = 0; i < ti->subr_nr; ++i) {
572 int pc = ti->subr[i].id;
573 while (ti->insns[pc].Instruction.Opcode != TGSI_OPCODE_ENDSUB)
574 prog_subroutine_inst(&ti->subr[i], &ti->insns[pc++]);
575 }
576
577 p->in_nr = ti->scan.file_max[TGSI_FILE_INPUT] + 1;
578 p->out_nr = ti->scan.file_max[TGSI_FILE_OUTPUT] + 1;
579
580 switch (p->type) {
581 case PIPE_SHADER_VERTEX:
582 ret = nv50_vertprog_prepare(ti);
583 break;
584 case PIPE_SHADER_FRAGMENT:
585 ret = nv50_fragprog_prepare(ti);
586 break;
587 case PIPE_SHADER_GEOMETRY:
588 ret = nv50_geomprog_prepare(ti);
589 break;
590 default:
591 assert(!"unsupported program type");
592 ret = -1;
593 break;
594 }
595
596 assert(!ret);
597 return ret;
598 }
599
600 boolean
601 nv50_program_tx(struct nv50_program *p)
602 {
603 struct nv50_translation_info *ti;
604 int ret;
605
606 ti = CALLOC_STRUCT(nv50_translation_info);
607 ti->p = p;
608
609 ti->edgeflag_out = PIPE_MAX_SHADER_OUTPUTS;
610
611 ret = nv50_prog_scan(ti);
612 if (ret) {
613 NOUVEAU_ERR("unsupported shader program\n");
614 goto out;
615 }
616
617 ret = nv50_generate_code(ti);
618 if (ret) {
619 NOUVEAU_ERR("error during shader translation\n");
620 goto out;
621 }
622
623 out:
624 if (ti->immd32)
625 FREE(ti->immd32);
626 if (ti->immd32_ty)
627 FREE(ti->immd32_ty);
628 if (ti->insns)
629 FREE(ti->insns);
630 if (ti->subr)
631 FREE(ti->subr);
632 FREE(ti);
633 return ret ? FALSE : TRUE;
634 }
635
636 void
637 nv50_program_destroy(struct nv50_context *nv50, struct nv50_program *p)
638 {
639 nouveau_bo_ref(NULL, &p->bo);
640
641 so_ref(NULL, &p->so);
642
643 if (p->code)
644 FREE(p->code);
645
646 p->translated = FALSE;
647 }