r600g: add support for s3tc formats.
[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_NULL:
305 case TGSI_FILE_CONSTANT:
306 case TGSI_FILE_TEMPORARY:
307 case TGSI_FILE_SAMPLER:
308 case TGSI_FILE_ADDRESS:
309 case TGSI_FILE_IMMEDIATE:
310 case TGSI_FILE_PREDICATE:
311 break;
312 default:
313 NOUVEAU_ERR("unhandled TGSI_FILE %d\n", decl->Declaration.File);
314 return FALSE;
315 }
316 return TRUE;
317 }
318
319 static void
320 prog_inst(struct nvc0_translation_info *ti,
321 const struct tgsi_full_instruction *inst, int id)
322 {
323 const struct tgsi_dst_register *dst;
324 const struct tgsi_src_register *src;
325 int s, c, k;
326 unsigned mask;
327
328 if (inst->Instruction.Opcode == TGSI_OPCODE_BGNSUB) {
329 ti->subr[ti->num_subrs].first_insn = id - 1;
330 ti->subr[ti->num_subrs].id = ti->num_subrs + 1; /* id 0 is main program */
331 ++ti->num_subrs;
332 }
333
334 if (inst->Dst[0].Register.File == TGSI_FILE_OUTPUT) {
335 dst = &inst->Dst[0].Register;
336
337 for (c = 0; c < 4; ++c) {
338 if (dst->Indirect)
339 nvc0_indirect_outputs(ti, id);
340 if (!(dst->WriteMask & (1 << c)))
341 continue;
342 ti->output_access[dst->Index][c] = id;
343 }
344
345 if (inst->Instruction.Opcode == TGSI_OPCODE_MOV &&
346 inst->Src[0].Register.File == TGSI_FILE_INPUT &&
347 dst->Index == ti->edgeflag_out)
348 ti->prog->vp.edgeflag = inst->Src[0].Register.Index;
349 } else
350 if (inst->Dst[0].Register.File == TGSI_FILE_TEMPORARY) {
351 if (inst->Dst[0].Register.Indirect)
352 ti->require_stores = TRUE;
353 }
354
355 for (s = 0; s < inst->Instruction.NumSrcRegs; ++s) {
356 src = &inst->Src[s].Register;
357 if (src->File == TGSI_FILE_TEMPORARY)
358 if (inst->Src[s].Register.Indirect)
359 ti->require_stores = TRUE;
360 if (src->File != TGSI_FILE_INPUT)
361 continue;
362 mask = nvc0_tgsi_src_mask(inst, s);
363
364 if (inst->Src[s].Register.Indirect)
365 nvc0_indirect_inputs(ti, id);
366
367 for (c = 0; c < 4; ++c) {
368 if (!(mask & (1 << c)))
369 continue;
370 k = tgsi_util_get_full_src_register_swizzle(&inst->Src[s], c);
371 if (k <= TGSI_SWIZZLE_W)
372 ti->input_access[src->Index][k] = id;
373 }
374 }
375 }
376
377 /* Probably should introduce something like struct tgsi_function_declaration
378 * instead of trying to guess inputs/outputs.
379 */
380 static void
381 prog_subroutine_inst(struct nvc0_subroutine *subr,
382 const struct tgsi_full_instruction *inst)
383 {
384 const struct tgsi_dst_register *dst;
385 const struct tgsi_src_register *src;
386 int s, c, k;
387 unsigned mask;
388
389 for (s = 0; s < inst->Instruction.NumSrcRegs; ++s) {
390 src = &inst->Src[s].Register;
391 if (src->File != TGSI_FILE_TEMPORARY)
392 continue;
393 mask = nvc0_tgsi_src_mask(inst, s);
394
395 for (c = 0; c < 4; ++c) {
396 k = tgsi_util_get_full_src_register_swizzle(&inst->Src[s], c);
397
398 if ((mask & (1 << c)) && k < TGSI_SWIZZLE_W)
399 if (!(subr->retv[src->Index / 32][k] & (1 << (src->Index % 32))))
400 subr->argv[src->Index / 32][k] |= 1 << (src->Index % 32);
401 }
402 }
403
404 if (inst->Dst[0].Register.File == TGSI_FILE_TEMPORARY) {
405 dst = &inst->Dst[0].Register;
406
407 for (c = 0; c < 4; ++c)
408 if (dst->WriteMask & (1 << c))
409 subr->retv[dst->Index / 32][c] |= 1 << (dst->Index % 32);
410 }
411 }
412
413 static int
414 nvc0_vp_gp_gen_header(struct nvc0_program *vp, struct nvc0_translation_info *ti)
415 {
416 int i, c;
417 unsigned a;
418
419 for (a = 0x80/4, i = 0; i <= ti->scan.file_max[TGSI_FILE_INPUT]; ++i) {
420 for (c = 0; c < 4; ++c, ++a)
421 if (ti->input_access[i][c])
422 vp->hdr[5 + a / 32] |= 1 << (a % 32); /* VP_ATTR_EN */
423 }
424
425 for (i = 0; i <= ti->scan.file_max[TGSI_FILE_OUTPUT]; ++i) {
426 a = (ti->output_loc[i][0] - 0x40) / 4;
427 for (c = 0; c < 4; ++c, ++a) {
428 if (!ti->output_access[i][c])
429 continue;
430 vp->hdr[13 + a / 32] |= 1 << (a % 32); /* VP_EXPORT_EN */
431 }
432 }
433
434 for (i = 0; i < TGSI_SEMANTIC_COUNT; ++i) {
435 a = ti->sysval_loc[i] / 4;
436 if (a > 0 && a < (0xf00 / 4))
437 vp->hdr[(ti->sysval_in[i] ? 5 : 13) + a / 32] |= 1 << (a % 32);
438 }
439
440 return 0;
441 }
442
443 static int
444 nvc0_vp_gen_header(struct nvc0_program *vp, struct nvc0_translation_info *ti)
445 {
446 vp->hdr[0] = 0x20461;
447 vp->hdr[4] = 0xff000;
448
449 vp->hdr[18] = (1 << vp->vp.num_ucps) - 1;
450
451 return nvc0_vp_gp_gen_header(vp, ti);
452 }
453
454 static int
455 nvc0_gp_gen_header(struct nvc0_program *gp, struct nvc0_translation_info *ti)
456 {
457 unsigned invocations = 1;
458 unsigned max_output_verts, output_prim;
459 unsigned i;
460
461 gp->hdr[0] = 0x21061;
462
463 for (i = 0; i < ti->scan.num_properties; ++i) {
464 switch (ti->scan.properties[i].name) {
465 case TGSI_PROPERTY_GS_OUTPUT_PRIM:
466 output_prim = ti->scan.properties[i].data[0];
467 break;
468 case TGSI_PROPERTY_GS_MAX_OUTPUT_VERTICES:
469 max_output_verts = ti->scan.properties[i].data[0];
470 assert(max_output_verts < 512);
471 break;
472 /*
473 case TGSI_PROPERTY_GS_INVOCATIONS:
474 invocations = ti->scan.properties[i].data[0];
475 assert(invocations <= 32);
476 break;
477 */
478 default:
479 break;
480 }
481 }
482
483 gp->hdr[2] = MIN2(invocations, 32) << 24;
484
485 switch (output_prim) {
486 case PIPE_PRIM_POINTS:
487 gp->hdr[3] = 0x01000000;
488 gp->hdr[0] |= 0xf0000000;
489 break;
490 case PIPE_PRIM_LINE_STRIP:
491 gp->hdr[3] = 0x06000000;
492 gp->hdr[0] |= 0x10000000;
493 break;
494 case PIPE_PRIM_TRIANGLE_STRIP:
495 gp->hdr[3] = 0x07000000;
496 gp->hdr[0] |= 0x10000000;
497 break;
498 default:
499 assert(0);
500 break;
501 }
502
503 gp->hdr[4] = max_output_verts & 0x1ff;
504
505 return nvc0_vp_gp_gen_header(gp, ti);
506 }
507
508 static int
509 nvc0_fp_gen_header(struct nvc0_program *fp, struct nvc0_translation_info *ti)
510 {
511 int i, c;
512 unsigned a, m;
513
514 fp->hdr[0] = 0x21462;
515 fp->hdr[5] = 0x80000000; /* getting a trap if FRAG_COORD_UMASK.w = 0 */
516
517 if (ti->scan.uses_kill)
518 fp->hdr[0] |= 0x8000;
519 if (ti->scan.writes_z) {
520 fp->hdr[19] |= 0x2;
521 if (ti->scan.num_outputs > 2)
522 fp->hdr[0] |= 0x4000; /* FP_MULTIPLE_COLOR_OUTPUTS */
523 } else {
524 if (ti->scan.num_outputs > 1)
525 fp->hdr[0] |= 0x4000; /* FP_MULTIPLE_COLOR_OUTPUTS */
526 }
527
528 for (i = 0; i <= ti->scan.file_max[TGSI_FILE_INPUT]; ++i) {
529 m = ti->interp_mode[i];
530 for (c = 0; c < 4; ++c) {
531 if (!ti->input_access[i][c])
532 continue;
533 a = ti->input_loc[i][c] / 2;
534 if (ti->input_loc[i][c] >= 0x2c0)
535 a -= 32;
536 if ((a & ~7) == 0x70/2)
537 fp->hdr[5] |= 1 << (28 + (a & 7) / 2); /* FRAG_COORD_UMASK */
538 else
539 fp->hdr[4 + a / 32] |= m << (a % 32);
540 }
541 }
542
543 for (i = 0; i <= ti->scan.file_max[TGSI_FILE_OUTPUT]; ++i) {
544 if (i != ti->fp_depth_output)
545 fp->hdr[18] |= 0xf << ti->output_loc[i][0];
546 }
547
548 for (i = 0; i < TGSI_SEMANTIC_COUNT; ++i) {
549 a = ti->sysval_loc[i] / 2;
550 if ((a > 0) && (a < 0xf00 / 2))
551 fp->hdr[4 + a / 32] |= NVC0_INTERP_FLAT << (a % 32);
552 }
553
554 return 0;
555 }
556
557 static boolean
558 nvc0_prog_scan(struct nvc0_translation_info *ti)
559 {
560 struct nvc0_program *prog = ti->prog;
561 struct tgsi_parse_context parse;
562 int ret;
563 unsigned i;
564
565 #ifdef NOUVEAU_DEBUG
566 tgsi_dump(prog->pipe.tokens, 0);
567 #endif
568
569 tgsi_scan_shader(prog->pipe.tokens, &ti->scan);
570
571 if (ti->prog->type == PIPE_SHADER_FRAGMENT) {
572 ti->fp_depth_output = 255;
573 for (i = 0; i < ti->scan.num_outputs; ++i)
574 if (ti->scan.output_semantic_name[i] == TGSI_SEMANTIC_POSITION)
575 ti->fp_depth_output = i;
576 }
577
578 ti->subr =
579 CALLOC(ti->scan.opcode_count[TGSI_OPCODE_BGNSUB], sizeof(ti->subr[0]));
580
581 ti->immd32 = (uint32_t *)MALLOC(ti->scan.immediate_count * 16);
582 ti->immd32_ty = (ubyte *)MALLOC(ti->scan.immediate_count * sizeof(ubyte));
583
584 ti->insns = MALLOC(ti->scan.num_instructions * sizeof(ti->insns[0]));
585
586 tgsi_parse_init(&parse, prog->pipe.tokens);
587 while (!tgsi_parse_end_of_tokens(&parse)) {
588 tgsi_parse_token(&parse);
589
590 switch (parse.FullToken.Token.Type) {
591 case TGSI_TOKEN_TYPE_IMMEDIATE:
592 prog_immediate(ti, &parse.FullToken.FullImmediate);
593 break;
594 case TGSI_TOKEN_TYPE_DECLARATION:
595 prog_decl(ti, &parse.FullToken.FullDeclaration);
596 break;
597 case TGSI_TOKEN_TYPE_INSTRUCTION:
598 ti->insns[ti->num_insns] = parse.FullToken.FullInstruction;
599 prog_inst(ti, &parse.FullToken.FullInstruction, ++ti->num_insns);
600 break;
601 default:
602 break;
603 }
604 }
605
606 for (i = 0; i < ti->num_subrs; ++i) {
607 unsigned pc = ti->subr[i].id;
608 while (ti->insns[pc].Instruction.Opcode != TGSI_OPCODE_ENDSUB)
609 prog_subroutine_inst(&ti->subr[i], &ti->insns[pc++]);
610 }
611
612 switch (prog->type) {
613 case PIPE_SHADER_VERTEX:
614 ti->input_file = NV_FILE_MEM_A;
615 ti->output_file = NV_FILE_MEM_V;
616 ret = nvc0_vp_gen_header(prog, ti);
617 break;
618 /*
619 case PIPE_SHADER_TESSELLATION_CONTROL:
620 ret = nvc0_tcp_gen_header(ti);
621 break;
622 case PIPE_SHADER_TESSELLATION_EVALUATION:
623 ret = nvc0_tep_gen_header(ti);
624 break;
625 case PIPE_SHADER_GEOMETRY:
626 ret = nvc0_gp_gen_header(ti);
627 break;
628 */
629 case PIPE_SHADER_FRAGMENT:
630 ti->input_file = NV_FILE_MEM_V;
631 ti->output_file = NV_FILE_GPR;
632
633 if (ti->scan.writes_z)
634 prog->flags[0] = 0x11; /* ? */
635 else
636 if (!ti->global_stores)
637 prog->fp.early_z = 1;
638
639 ret = nvc0_fp_gen_header(prog, ti);
640 break;
641 default:
642 assert(!"unsupported program type");
643 ret = -1;
644 break;
645 }
646
647 assert(!ret);
648 return ret;
649 }
650
651 boolean
652 nvc0_program_translate(struct nvc0_program *prog)
653 {
654 struct nvc0_translation_info *ti;
655 int ret;
656
657 ti = CALLOC_STRUCT(nvc0_translation_info);
658 ti->prog = prog;
659
660 ti->edgeflag_out = PIPE_MAX_SHADER_OUTPUTS;
661
662 if (prog->type == PIPE_SHADER_VERTEX && prog->vp.num_ucps)
663 ti->append_ucp = TRUE;
664
665 ret = nvc0_prog_scan(ti);
666 if (ret) {
667 NOUVEAU_ERR("unsupported shader program\n");
668 goto out;
669 }
670
671 ret = nvc0_generate_code(ti);
672 if (ret)
673 NOUVEAU_ERR("shader translation failed\n");
674
675 {
676 unsigned i;
677 for (i = 0; i < sizeof(prog->hdr) / sizeof(prog->hdr[0]); ++i)
678 debug_printf("HDR[%02lx] = 0x%08x\n",
679 i * sizeof(prog->hdr[0]), prog->hdr[i]);
680 }
681
682 out:
683 if (ti->immd32)
684 FREE(ti->immd32);
685 if (ti->immd32_ty)
686 FREE(ti->immd32_ty);
687 if (ti->insns)
688 FREE(ti->insns);
689 if (ti->subr)
690 FREE(ti->subr);
691 FREE(ti);
692 return ret ? FALSE : TRUE;
693 }
694
695 void
696 nvc0_program_destroy(struct nvc0_context *nvc0, struct nvc0_program *prog)
697 {
698 if (prog->res)
699 nouveau_resource_free(&prog->res);
700
701 if (prog->code)
702 FREE(prog->code);
703 if (prog->relocs)
704 FREE(prog->relocs);
705
706 memset(prog->hdr, 0, sizeof(prog->hdr));
707
708 prog->translated = FALSE;
709 }