nvc0: fix PointCoord enable in FP header
[mesa.git] / src / gallium / drivers / nvc0 / nvc0_program.c
1 /*
2 * Copyright 2010 Christoph 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 #include "pipe/p_shader_tokens.h"
24 #include "pipe/p_defines.h"
25
26 #define NOUVEAU_DEBUG
27
28 #include "tgsi/tgsi_parse.h"
29 #include "tgsi/tgsi_util.h"
30 #include "tgsi/tgsi_dump.h"
31
32 #include "nvc0_context.h"
33 #include "nvc0_pc.h"
34
35 static unsigned
36 nvc0_tgsi_src_mask(const struct tgsi_full_instruction *inst, int c)
37 {
38 unsigned mask = inst->Dst[0].Register.WriteMask;
39
40 switch (inst->Instruction.Opcode) {
41 case TGSI_OPCODE_COS:
42 case TGSI_OPCODE_SIN:
43 return (mask & 0x8) | ((mask & 0x7) ? 0x1 : 0x0);
44 case TGSI_OPCODE_DP3:
45 return 0x7;
46 case TGSI_OPCODE_DP4:
47 case TGSI_OPCODE_DPH:
48 case TGSI_OPCODE_KIL: /* WriteMask ignored */
49 return 0xf;
50 case TGSI_OPCODE_DST:
51 return mask & (c ? 0xa : 0x6);
52 case TGSI_OPCODE_EX2:
53 case TGSI_OPCODE_EXP:
54 case TGSI_OPCODE_LG2:
55 case TGSI_OPCODE_LOG:
56 case TGSI_OPCODE_POW:
57 case TGSI_OPCODE_RCP:
58 case TGSI_OPCODE_RSQ:
59 case TGSI_OPCODE_SCS:
60 return 0x1;
61 case TGSI_OPCODE_IF:
62 return 0x1;
63 case TGSI_OPCODE_LIT:
64 return 0xb;
65 case TGSI_OPCODE_TEX:
66 case TGSI_OPCODE_TXB:
67 case TGSI_OPCODE_TXL:
68 case TGSI_OPCODE_TXP:
69 {
70 const struct tgsi_instruction_texture *tex;
71
72 assert(inst->Instruction.Texture);
73 tex = &inst->Texture;
74
75 mask = 0x7;
76 if (inst->Instruction.Opcode != TGSI_OPCODE_TEX &&
77 inst->Instruction.Opcode != TGSI_OPCODE_TXD)
78 mask |= 0x8; /* bias, lod or proj */
79
80 switch (tex->Texture) {
81 case TGSI_TEXTURE_1D:
82 mask &= 0x9;
83 break;
84 case TGSI_TEXTURE_SHADOW1D:
85 mask &= 0x5;
86 break;
87 case TGSI_TEXTURE_2D:
88 mask &= 0xb;
89 break;
90 default:
91 break;
92 }
93 }
94 return mask;
95 case TGSI_OPCODE_XPD:
96 {
97 unsigned x = 0;
98 if (mask & 1) x |= 0x6;
99 if (mask & 2) x |= 0x5;
100 if (mask & 4) x |= 0x3;
101 return x;
102 }
103 default:
104 break;
105 }
106
107 return mask;
108 }
109
110 static void
111 nvc0_indirect_inputs(struct nvc0_translation_info *ti, int id)
112 {
113 int i, c;
114
115 for (i = 0; i < PIPE_MAX_SHADER_INPUTS; ++i)
116 for (c = 0; c < 4; ++c)
117 ti->input_access[i][c] = id;
118
119 ti->indirect_inputs = TRUE;
120 }
121
122 static void
123 nvc0_indirect_outputs(struct nvc0_translation_info *ti, int id)
124 {
125 int i, c;
126
127 for (i = 0; i < PIPE_MAX_SHADER_OUTPUTS; ++i)
128 for (c = 0; c < 4; ++c)
129 ti->output_access[i][c] = id;
130
131 ti->indirect_outputs = TRUE;
132 }
133
134 static INLINE unsigned
135 nvc0_system_value_location(unsigned sn, unsigned si, boolean *is_input)
136 {
137 /* NOTE: locations 0xfxx indicate special regs */
138 switch (sn) {
139 /*
140 case TGSI_SEMANTIC_VERTEXID:
141 *is_input = TRUE;
142 return 0x2fc;
143 */
144 case TGSI_SEMANTIC_PRIMID:
145 *is_input = TRUE;
146 return 0x60;
147 /*
148 case TGSI_SEMANTIC_LAYER_INDEX:
149 return 0x64;
150 case TGSI_SEMANTIC_VIEWPORT_INDEX:
151 return 0x68;
152 */
153 case TGSI_SEMANTIC_INSTANCEID:
154 *is_input = TRUE;
155 return 0x2f8;
156 case TGSI_SEMANTIC_FACE:
157 *is_input = TRUE;
158 return 0x3fc;
159 /*
160 case TGSI_SEMANTIC_INVOCATIONID:
161 return 0xf11;
162 */
163 default:
164 assert(0);
165 return 0x000;
166 }
167 }
168
169 static INLINE unsigned
170 nvc0_varying_location(unsigned sn, unsigned si)
171 {
172 switch (sn) {
173 case TGSI_SEMANTIC_POSITION:
174 return 0x70;
175 case TGSI_SEMANTIC_COLOR:
176 return 0x280 + (si * 16); /* are these hard-wired ? */
177 case TGSI_SEMANTIC_BCOLOR:
178 return 0x2a0 + (si * 16);
179 case TGSI_SEMANTIC_FOG:
180 return 0x270;
181 case TGSI_SEMANTIC_PSIZE:
182 return 0x6c;
183 /*
184 case TGSI_SEMANTIC_PNTC:
185 return 0x2e0;
186 */
187 case TGSI_SEMANTIC_GENERIC:
188 /* We'd really like to distinguish between TEXCOORD and GENERIC here,
189 * since only 0x300 to 0x37c can be replaced by sprite coordinates.
190 * Also, gl_PointCoord should be a system value and must be assigned to
191 * address 0x2e0. For now, let's cheat:
192 */
193 assert(si < 31);
194 if (si <= 7)
195 return 0x300 + si * 16;
196 if (si == 9)
197 return 0x2e0;
198 return 0x80 + ((si - 8) * 16);
199 case TGSI_SEMANTIC_NORMAL:
200 return 0x360;
201 case TGSI_SEMANTIC_PRIMID:
202 return 0x40;
203 case TGSI_SEMANTIC_FACE:
204 return 0x3fc;
205 /*
206 case TGSI_SEMANTIC_CLIP_DISTANCE:
207 return 0x2c0 + (si * 4);
208 */
209 default:
210 assert(0);
211 return 0x000;
212 }
213 }
214
215 static INLINE unsigned
216 nvc0_interp_mode(const struct tgsi_full_declaration *decl)
217 {
218 unsigned mode;
219
220 if (decl->Declaration.Interpolate == TGSI_INTERPOLATE_CONSTANT)
221 mode = NVC0_INTERP_FLAT;
222 else
223 if (decl->Declaration.Interpolate == TGSI_INTERPOLATE_PERSPECTIVE)
224 mode = NVC0_INTERP_PERSPECTIVE;
225 else
226 mode = NVC0_INTERP_LINEAR;
227
228 if (decl->Declaration.Centroid)
229 mode |= NVC0_INTERP_CENTROID;
230
231 return mode;
232 }
233
234 static void
235 prog_immediate(struct nvc0_translation_info *ti,
236 const struct tgsi_full_immediate *imm)
237 {
238 int c;
239 unsigned n = ti->immd32_nr++;
240
241 assert(ti->immd32_nr <= ti->scan.immediate_count);
242
243 for (c = 0; c < 4; ++c)
244 ti->immd32[n * 4 + c] = imm->u[c].Uint;
245
246 ti->immd32_ty[n] = imm->Immediate.DataType;
247 }
248
249 static boolean
250 prog_decl(struct nvc0_translation_info *ti,
251 const struct tgsi_full_declaration *decl)
252 {
253 unsigned i, c;
254 unsigned sn = TGSI_SEMANTIC_GENERIC;
255 unsigned si = 0;
256 const unsigned first = decl->Range.First;
257 const unsigned last = decl->Range.Last;
258
259 if (decl->Declaration.Semantic) {
260 sn = decl->Semantic.Name;
261 si = decl->Semantic.Index;
262 }
263
264 switch (decl->Declaration.File) {
265 case TGSI_FILE_INPUT:
266 for (i = first; i <= last; ++i) {
267 if (ti->prog->type == PIPE_SHADER_VERTEX) {
268 for (c = 0; c < 4; ++c)
269 ti->input_loc[i][c] = 0x80 + i * 16 + c * 4;
270 } else {
271 for (c = 0; c < 4; ++c)
272 ti->input_loc[i][c] = nvc0_varying_location(sn, si) + c * 4;
273 /* for sprite coordinates: */
274 ti->prog->fp.in_pos[i] = ti->input_loc[i][0] / 4;
275 }
276 if (ti->prog->type == PIPE_SHADER_FRAGMENT)
277 ti->interp_mode[i] = nvc0_interp_mode(decl);
278 }
279 break;
280 case TGSI_FILE_OUTPUT:
281 for (i = first; i <= last; ++i, ++si) {
282 if (ti->prog->type == PIPE_SHADER_FRAGMENT) {
283 si = i;
284 if (i == ti->fp_depth_output) {
285 ti->output_loc[i][2] = (ti->scan.num_outputs - 1) * 4;
286 } else {
287 if (i > ti->fp_depth_output)
288 si -= 1;
289 for (c = 0; c < 4; ++c)
290 ti->output_loc[i][c] = si * 4 + c;
291 }
292 } else {
293 for (c = 0; c < 4; ++c)
294 ti->output_loc[i][c] = nvc0_varying_location(sn, si) + c * 4;
295 /* for TFB_VARYING_LOCS: */
296 ti->prog->vp.out_pos[i] = ti->output_loc[i][0] / 4;
297 }
298 }
299 break;
300 case TGSI_FILE_SYSTEM_VALUE:
301 ti->sysval_loc[i] = nvc0_system_value_location(sn, si, &ti->sysval_in[i]);
302 assert(first == last);
303 break;
304 case TGSI_FILE_TEMPORARY:
305 ti->temp128_nr = MAX2(ti->temp128_nr, last + 1);
306 break;
307 case TGSI_FILE_NULL:
308 case TGSI_FILE_CONSTANT:
309 case TGSI_FILE_SAMPLER:
310 case TGSI_FILE_ADDRESS:
311 case TGSI_FILE_IMMEDIATE:
312 case TGSI_FILE_PREDICATE:
313 break;
314 default:
315 NOUVEAU_ERR("unhandled TGSI_FILE %d\n", decl->Declaration.File);
316 return FALSE;
317 }
318 return TRUE;
319 }
320
321 static void
322 prog_inst(struct nvc0_translation_info *ti,
323 const struct tgsi_full_instruction *inst, int id)
324 {
325 const struct tgsi_dst_register *dst;
326 const struct tgsi_src_register *src;
327 int s, c, k;
328 unsigned mask;
329
330 if (inst->Instruction.Opcode == TGSI_OPCODE_BGNSUB) {
331 ti->subr[ti->num_subrs].first_insn = id - 1;
332 ti->subr[ti->num_subrs].id = ti->num_subrs + 1; /* id 0 is main program */
333 ++ti->num_subrs;
334 }
335
336 if (inst->Dst[0].Register.File == TGSI_FILE_OUTPUT) {
337 dst = &inst->Dst[0].Register;
338
339 for (c = 0; c < 4; ++c) {
340 if (dst->Indirect)
341 nvc0_indirect_outputs(ti, id);
342 if (!(dst->WriteMask & (1 << c)))
343 continue;
344 ti->output_access[dst->Index][c] = id;
345 }
346
347 if (inst->Instruction.Opcode == TGSI_OPCODE_MOV &&
348 inst->Src[0].Register.File == TGSI_FILE_INPUT &&
349 dst->Index == ti->edgeflag_out)
350 ti->prog->vp.edgeflag = inst->Src[0].Register.Index;
351 } else
352 if (inst->Dst[0].Register.File == TGSI_FILE_TEMPORARY) {
353 if (inst->Dst[0].Register.Indirect)
354 ti->require_stores = TRUE;
355 }
356
357 for (s = 0; s < inst->Instruction.NumSrcRegs; ++s) {
358 src = &inst->Src[s].Register;
359 if (src->File == TGSI_FILE_TEMPORARY)
360 if (inst->Src[s].Register.Indirect)
361 ti->require_stores = TRUE;
362 if (src->File != TGSI_FILE_INPUT)
363 continue;
364 mask = nvc0_tgsi_src_mask(inst, s);
365
366 if (inst->Src[s].Register.Indirect)
367 nvc0_indirect_inputs(ti, id);
368
369 for (c = 0; c < 4; ++c) {
370 if (!(mask & (1 << c)))
371 continue;
372 k = tgsi_util_get_full_src_register_swizzle(&inst->Src[s], c);
373 if (k <= TGSI_SWIZZLE_W)
374 ti->input_access[src->Index][k] = id;
375 }
376 }
377 }
378
379 /* Probably should introduce something like struct tgsi_function_declaration
380 * instead of trying to guess inputs/outputs.
381 */
382 static void
383 prog_subroutine_inst(struct nvc0_subroutine *subr,
384 const struct tgsi_full_instruction *inst)
385 {
386 const struct tgsi_dst_register *dst;
387 const struct tgsi_src_register *src;
388 int s, c, k;
389 unsigned mask;
390
391 for (s = 0; s < inst->Instruction.NumSrcRegs; ++s) {
392 src = &inst->Src[s].Register;
393 if (src->File != TGSI_FILE_TEMPORARY)
394 continue;
395 mask = nvc0_tgsi_src_mask(inst, s);
396
397 for (c = 0; c < 4; ++c) {
398 k = tgsi_util_get_full_src_register_swizzle(&inst->Src[s], c);
399
400 if ((mask & (1 << c)) && k < TGSI_SWIZZLE_W)
401 if (!(subr->retv[src->Index / 32][k] & (1 << (src->Index % 32))))
402 subr->argv[src->Index / 32][k] |= 1 << (src->Index % 32);
403 }
404 }
405
406 if (inst->Dst[0].Register.File == TGSI_FILE_TEMPORARY) {
407 dst = &inst->Dst[0].Register;
408
409 for (c = 0; c < 4; ++c)
410 if (dst->WriteMask & (1 << c))
411 subr->retv[dst->Index / 32][c] |= 1 << (dst->Index % 32);
412 }
413 }
414
415 static int
416 nvc0_vp_gp_gen_header(struct nvc0_program *vp, struct nvc0_translation_info *ti)
417 {
418 int i, c;
419 unsigned a;
420
421 for (a = 0x80/4, i = 0; i <= ti->scan.file_max[TGSI_FILE_INPUT]; ++i) {
422 for (c = 0; c < 4; ++c, ++a)
423 if (ti->input_access[i][c])
424 vp->hdr[5 + a / 32] |= 1 << (a % 32); /* VP_ATTR_EN */
425 }
426
427 for (i = 0; i <= ti->scan.file_max[TGSI_FILE_OUTPUT]; ++i) {
428 a = (ti->output_loc[i][0] - 0x40) / 4;
429 for (c = 0; c < 4; ++c, ++a) {
430 if (!ti->output_access[i][c])
431 continue;
432 vp->hdr[13 + a / 32] |= 1 << (a % 32); /* VP_EXPORT_EN */
433 }
434 }
435
436 for (i = 0; i < TGSI_SEMANTIC_COUNT; ++i) {
437 a = ti->sysval_loc[i] / 4;
438 if (a > 0 && a < (0xf00 / 4))
439 vp->hdr[(ti->sysval_in[i] ? 5 : 13) + a / 32] |= 1 << (a % 32);
440 }
441
442 return 0;
443 }
444
445 static int
446 nvc0_vp_gen_header(struct nvc0_program *vp, struct nvc0_translation_info *ti)
447 {
448 vp->hdr[0] = 0x20461;
449 vp->hdr[4] = 0xff000;
450
451 vp->hdr[18] = (1 << vp->vp.num_ucps) - 1;
452
453 return nvc0_vp_gp_gen_header(vp, ti);
454 }
455
456 static int
457 nvc0_gp_gen_header(struct nvc0_program *gp, struct nvc0_translation_info *ti)
458 {
459 unsigned invocations = 1;
460 unsigned max_output_verts, output_prim;
461 unsigned i;
462
463 gp->hdr[0] = 0x21061;
464
465 for (i = 0; i < ti->scan.num_properties; ++i) {
466 switch (ti->scan.properties[i].name) {
467 case TGSI_PROPERTY_GS_OUTPUT_PRIM:
468 output_prim = ti->scan.properties[i].data[0];
469 break;
470 case TGSI_PROPERTY_GS_MAX_OUTPUT_VERTICES:
471 max_output_verts = ti->scan.properties[i].data[0];
472 assert(max_output_verts < 512);
473 break;
474 /*
475 case TGSI_PROPERTY_GS_INVOCATIONS:
476 invocations = ti->scan.properties[i].data[0];
477 assert(invocations <= 32);
478 break;
479 */
480 default:
481 break;
482 }
483 }
484
485 gp->hdr[2] = MIN2(invocations, 32) << 24;
486
487 switch (output_prim) {
488 case PIPE_PRIM_POINTS:
489 gp->hdr[3] = 0x01000000;
490 gp->hdr[0] |= 0xf0000000;
491 break;
492 case PIPE_PRIM_LINE_STRIP:
493 gp->hdr[3] = 0x06000000;
494 gp->hdr[0] |= 0x10000000;
495 break;
496 case PIPE_PRIM_TRIANGLE_STRIP:
497 gp->hdr[3] = 0x07000000;
498 gp->hdr[0] |= 0x10000000;
499 break;
500 default:
501 assert(0);
502 break;
503 }
504
505 gp->hdr[4] = max_output_verts & 0x1ff;
506
507 return nvc0_vp_gp_gen_header(gp, ti);
508 }
509
510 static int
511 nvc0_fp_gen_header(struct nvc0_program *fp, struct nvc0_translation_info *ti)
512 {
513 int i, c;
514 unsigned a, m;
515
516 fp->hdr[0] = 0x21462;
517 fp->hdr[5] = 0x80000000; /* getting a trap if FRAG_COORD_UMASK.w = 0 */
518
519 if (ti->scan.uses_kill)
520 fp->hdr[0] |= 0x8000;
521 if (ti->scan.writes_z) {
522 fp->hdr[19] |= 0x2;
523 if (ti->scan.num_outputs > 2)
524 fp->hdr[0] |= 0x4000; /* FP_MULTIPLE_COLOR_OUTPUTS */
525 } else {
526 if (ti->scan.num_outputs > 1)
527 fp->hdr[0] |= 0x4000; /* FP_MULTIPLE_COLOR_OUTPUTS */
528 }
529
530 for (i = 0; i <= ti->scan.file_max[TGSI_FILE_INPUT]; ++i) {
531 m = ti->interp_mode[i];
532 for (c = 0; c < 4; ++c) {
533 if (!ti->input_access[i][c])
534 continue;
535 a = ti->input_loc[i][c] / 2;
536 if (ti->input_loc[i][c] >= 0x2c0)
537 a -= 32;
538 if (ti->input_loc[i][0] == 0x70)
539 fp->hdr[5] |= 1 << (28 + c); /* FRAG_COORD_UMASK */
540 else
541 if (ti->input_loc[i][0] == 0x2e0)
542 fp->hdr[14] |= 1 << (24 + c); /* POINT_COORD */
543 else
544 fp->hdr[4 + a / 32] |= m << (a % 32);
545 }
546 }
547
548 for (i = 0; i <= ti->scan.file_max[TGSI_FILE_OUTPUT]; ++i) {
549 if (i != ti->fp_depth_output)
550 fp->hdr[18] |= 0xf << ti->output_loc[i][0];
551 }
552
553 for (i = 0; i < TGSI_SEMANTIC_COUNT; ++i) {
554 a = ti->sysval_loc[i] / 2;
555 if ((a > 0) && (a < 0xf00 / 2))
556 fp->hdr[4 + a / 32] |= NVC0_INTERP_FLAT << (a % 32);
557 }
558
559 return 0;
560 }
561
562 static boolean
563 nvc0_prog_scan(struct nvc0_translation_info *ti)
564 {
565 struct nvc0_program *prog = ti->prog;
566 struct tgsi_parse_context parse;
567 int ret;
568 unsigned i;
569
570 #ifdef NOUVEAU_DEBUG
571 tgsi_dump(prog->pipe.tokens, 0);
572 #endif
573
574 tgsi_scan_shader(prog->pipe.tokens, &ti->scan);
575
576 if (ti->prog->type == PIPE_SHADER_FRAGMENT) {
577 ti->fp_depth_output = 255;
578 for (i = 0; i < ti->scan.num_outputs; ++i)
579 if (ti->scan.output_semantic_name[i] == TGSI_SEMANTIC_POSITION)
580 ti->fp_depth_output = i;
581 }
582
583 ti->subr =
584 CALLOC(ti->scan.opcode_count[TGSI_OPCODE_BGNSUB], sizeof(ti->subr[0]));
585
586 ti->immd32 = (uint32_t *)MALLOC(ti->scan.immediate_count * 16);
587 ti->immd32_ty = (ubyte *)MALLOC(ti->scan.immediate_count * sizeof(ubyte));
588
589 ti->insns = MALLOC(ti->scan.num_instructions * sizeof(ti->insns[0]));
590
591 tgsi_parse_init(&parse, prog->pipe.tokens);
592 while (!tgsi_parse_end_of_tokens(&parse)) {
593 tgsi_parse_token(&parse);
594
595 switch (parse.FullToken.Token.Type) {
596 case TGSI_TOKEN_TYPE_IMMEDIATE:
597 prog_immediate(ti, &parse.FullToken.FullImmediate);
598 break;
599 case TGSI_TOKEN_TYPE_DECLARATION:
600 prog_decl(ti, &parse.FullToken.FullDeclaration);
601 break;
602 case TGSI_TOKEN_TYPE_INSTRUCTION:
603 ti->insns[ti->num_insns] = parse.FullToken.FullInstruction;
604 prog_inst(ti, &parse.FullToken.FullInstruction, ++ti->num_insns);
605 break;
606 default:
607 break;
608 }
609 }
610
611 for (i = 0; i < ti->num_subrs; ++i) {
612 unsigned pc = ti->subr[i].id;
613 while (ti->insns[pc].Instruction.Opcode != TGSI_OPCODE_ENDSUB)
614 prog_subroutine_inst(&ti->subr[i], &ti->insns[pc++]);
615 }
616
617 switch (prog->type) {
618 case PIPE_SHADER_VERTEX:
619 ti->input_file = NV_FILE_MEM_A;
620 ti->output_file = NV_FILE_MEM_V;
621 ret = nvc0_vp_gen_header(prog, ti);
622 break;
623 /*
624 case PIPE_SHADER_TESSELLATION_CONTROL:
625 ret = nvc0_tcp_gen_header(ti);
626 break;
627 case PIPE_SHADER_TESSELLATION_EVALUATION:
628 ret = nvc0_tep_gen_header(ti);
629 break;
630 case PIPE_SHADER_GEOMETRY:
631 ret = nvc0_gp_gen_header(ti);
632 break;
633 */
634 case PIPE_SHADER_FRAGMENT:
635 ti->input_file = NV_FILE_MEM_V;
636 ti->output_file = NV_FILE_GPR;
637
638 if (ti->scan.writes_z)
639 prog->flags[0] = 0x11; /* ? */
640 else
641 if (!ti->scan.uses_kill && !ti->global_stores)
642 prog->fp.early_z = 1;
643
644 ret = nvc0_fp_gen_header(prog, ti);
645 break;
646 default:
647 assert(!"unsupported program type");
648 ret = -1;
649 break;
650 }
651
652 if (ti->require_stores) {
653 prog->hdr[0] |= 1 << 26;
654 prog->hdr[1] |= ti->temp128_nr * 16; /* l[] size */
655 }
656
657 assert(!ret);
658 return ret;
659 }
660
661 boolean
662 nvc0_program_translate(struct nvc0_program *prog)
663 {
664 struct nvc0_translation_info *ti;
665 int ret;
666
667 ti = CALLOC_STRUCT(nvc0_translation_info);
668 ti->prog = prog;
669
670 ti->edgeflag_out = PIPE_MAX_SHADER_OUTPUTS;
671
672 if (prog->type == PIPE_SHADER_VERTEX && prog->vp.num_ucps)
673 ti->append_ucp = TRUE;
674
675 ret = nvc0_prog_scan(ti);
676 if (ret) {
677 NOUVEAU_ERR("unsupported shader program\n");
678 goto out;
679 }
680
681 ret = nvc0_generate_code(ti);
682 if (ret)
683 NOUVEAU_ERR("shader translation failed\n");
684
685 {
686 unsigned i;
687 for (i = 0; i < sizeof(prog->hdr) / sizeof(prog->hdr[0]); ++i)
688 debug_printf("HDR[%02lx] = 0x%08x\n",
689 i * sizeof(prog->hdr[0]), prog->hdr[i]);
690 }
691
692 out:
693 if (ti->immd32)
694 FREE(ti->immd32);
695 if (ti->immd32_ty)
696 FREE(ti->immd32_ty);
697 if (ti->insns)
698 FREE(ti->insns);
699 if (ti->subr)
700 FREE(ti->subr);
701 FREE(ti);
702 return ret ? FALSE : TRUE;
703 }
704
705 void
706 nvc0_program_destroy(struct nvc0_context *nvc0, struct nvc0_program *prog)
707 {
708 if (prog->res)
709 nouveau_resource_free(&prog->res);
710
711 if (prog->code)
712 FREE(prog->code);
713 if (prog->relocs)
714 FREE(prog->relocs);
715
716 memset(prog->hdr, 0, sizeof(prog->hdr));
717
718 prog->translated = FALSE;
719 }