tgsi_ureg.c revision acc7da90356a96efb93bb3a6a53e0b5f67ce993a
1/**************************************************************************
2 *
3 * Copyright 2009 VMware, Inc.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL VMWARE, INC AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28
29#include "pipe/p_context.h"
30#include "pipe/p_state.h"
31#include "tgsi/tgsi_ureg.h"
32#include "tgsi/tgsi_dump.h"
33#include "util/u_memory.h"
34#include "util/u_math.h"
35
36union tgsi_any_token {
37   struct tgsi_version version;
38   struct tgsi_header header;
39   struct tgsi_processor processor;
40   struct tgsi_token token;
41   struct tgsi_declaration decl;
42   struct tgsi_declaration_range decl_range;
43   struct tgsi_declaration_semantic decl_semantic;
44   struct tgsi_immediate imm;
45   union  tgsi_immediate_data imm_data;
46   struct tgsi_instruction insn;
47   struct tgsi_instruction_ext_nv insn_ext_nv;
48   struct tgsi_instruction_ext_label insn_ext_label;
49   struct tgsi_instruction_ext_texture insn_ext_texture;
50   struct tgsi_instruction_ext_predicate insn_ext_predicate;
51   struct tgsi_src_register src;
52   struct tgsi_src_register_ext_swz src_ext_swz;
53   struct tgsi_src_register_ext_mod src_ext_mod;
54   struct tgsi_dimension dim;
55   struct tgsi_dst_register dst;
56   struct tgsi_dst_register_ext_concode dst_ext_code;
57   struct tgsi_dst_register_ext_modulate dst_ext_mod;
58   struct tgsi_dst_register_ext_predicate dst_ext_pred;
59   unsigned value;
60};
61
62
63struct ureg_tokens {
64   union tgsi_any_token *tokens;
65   unsigned size;
66   unsigned order;
67   unsigned count;
68};
69
70#define UREG_MAX_INPUT PIPE_MAX_ATTRIBS
71#define UREG_MAX_OUTPUT PIPE_MAX_ATTRIBS
72#define UREG_MAX_IMMEDIATE 32
73#define UREG_MAX_TEMP 256
74
75#define DOMAIN_DECL 0
76#define DOMAIN_INSN 1
77
78struct ureg_program
79{
80   unsigned processor;
81   struct pipe_context *pipe;
82
83   struct {
84      unsigned semantic_name;
85      unsigned semantic_index;
86      unsigned interp;
87   } input[UREG_MAX_INPUT];
88   unsigned nr_inputs;
89
90   struct {
91      unsigned semantic_name;
92      unsigned semantic_index;
93   } output[UREG_MAX_OUTPUT];
94   unsigned nr_outputs;
95
96   struct {
97      float v[4];
98      unsigned nr;
99   } immediate[UREG_MAX_IMMEDIATE];
100   unsigned nr_immediates;
101
102   unsigned temps_active[UREG_MAX_TEMP / 32];
103   unsigned nr_temps;
104
105   unsigned nr_constants;
106   unsigned nr_samplers;
107   unsigned nr_instructions;
108
109   struct ureg_tokens domain[2];
110};
111
112static union tgsi_any_token error_tokens[32];
113
114static void tokens_error( struct ureg_tokens *tokens )
115{
116   tokens->tokens = error_tokens;
117   tokens->size = Elements(error_tokens);
118   tokens->count = 0;
119}
120
121
122static void tokens_expand( struct ureg_tokens *tokens,
123                           unsigned count )
124{
125   unsigned old_size = tokens->size * sizeof(unsigned);
126
127   if (tokens->tokens == error_tokens)
128      goto fail;
129
130   while (tokens->count + count > tokens->size) {
131      tokens->size = (1 << ++tokens->order);
132   }
133
134   tokens->tokens = REALLOC(tokens->tokens,
135                            old_size,
136                            tokens->size * sizeof(unsigned));
137   if (tokens->tokens == NULL)
138      goto fail;
139
140   return;
141
142fail:
143   tokens_error(tokens);
144}
145
146static void set_bad( struct ureg_program *ureg )
147{
148   tokens_error(&ureg->domain[0]);
149}
150
151
152
153static union tgsi_any_token *get_tokens( struct ureg_program *ureg,
154                                         unsigned domain,
155                                         unsigned count )
156{
157   struct ureg_tokens *tokens = &ureg->domain[domain];
158   union tgsi_any_token *result;
159
160   if (tokens->count + count > tokens->size)
161      tokens_expand(tokens, count);
162
163   result = &tokens->tokens[tokens->count];
164   tokens->count += count;
165   return result;
166}
167
168
169static union tgsi_any_token *retrieve_token( struct ureg_program *ureg,
170                                            unsigned domain,
171                                            unsigned nr )
172{
173   if (ureg->domain[domain].tokens == error_tokens)
174      return &error_tokens[0];
175
176   return &ureg->domain[domain].tokens[nr];
177}
178
179
180
181static INLINE struct ureg_dst
182ureg_dst_register( unsigned file,
183                   unsigned index )
184{
185   struct ureg_dst dst;
186
187   dst.File      = file;
188   dst.WriteMask = TGSI_WRITEMASK_XYZW;
189   dst.Indirect  = 0;
190   dst.Saturate  = 0;
191   dst.Index     = index;
192   dst.Pad1      = 0;
193   dst.Pad2      = 0;
194
195   return dst;
196}
197
198static INLINE struct ureg_src
199ureg_src_register( unsigned file,
200                   unsigned index )
201{
202   struct ureg_src src;
203
204   src.File     = file;
205   src.SwizzleX = TGSI_SWIZZLE_X;
206   src.SwizzleY = TGSI_SWIZZLE_Y;
207   src.SwizzleZ = TGSI_SWIZZLE_Z;
208   src.SwizzleW = TGSI_SWIZZLE_W;
209   src.Pad      = 0;
210   src.Indirect = 0;
211   src.Absolute = 0;
212   src.Index    = index;
213   src.Negate   = 0;
214
215   return src;
216}
217
218
219
220
221static struct ureg_src
222ureg_DECL_input( struct ureg_program *ureg,
223                 unsigned name,
224                 unsigned index,
225                 unsigned interp_mode )
226{
227   unsigned i;
228
229   for (i = 0; i < ureg->nr_inputs; i++) {
230      if (ureg->input[i].semantic_name == name &&
231          ureg->input[i].semantic_index == index)
232         goto out;
233   }
234
235   if (ureg->nr_inputs < UREG_MAX_INPUT) {
236      ureg->input[i].semantic_name = name;
237      ureg->input[i].semantic_index = index;
238      ureg->input[i].interp = interp_mode;
239      ureg->nr_inputs++;
240   }
241   else {
242      set_bad( ureg );
243   }
244
245out:
246   return ureg_src_register( TGSI_FILE_INPUT, i );
247}
248
249
250
251struct ureg_src
252ureg_DECL_fs_input( struct ureg_program *ureg,
253                    unsigned name,
254                    unsigned index,
255                    unsigned interp )
256{
257   return ureg_DECL_input( ureg, name, index, interp );
258}
259
260
261struct ureg_src
262ureg_DECL_vs_input( struct ureg_program *ureg,
263                    unsigned name,
264                    unsigned index )
265{
266   return ureg_DECL_input( ureg, name, index, TGSI_INTERPOLATE_CONSTANT );
267}
268
269
270struct ureg_dst
271ureg_DECL_output( struct ureg_program *ureg,
272                  unsigned name,
273                  unsigned index )
274{
275   unsigned i;
276
277   for (i = 0; i < ureg->nr_outputs; i++) {
278      if (ureg->output[i].semantic_name == name &&
279          ureg->output[i].semantic_index == index)
280         goto out;
281   }
282
283   if (ureg->nr_outputs < UREG_MAX_OUTPUT) {
284      ureg->output[i].semantic_name = name;
285      ureg->output[i].semantic_index = index;
286      ureg->nr_outputs++;
287   }
288   else {
289      set_bad( ureg );
290   }
291
292out:
293   return ureg_dst_register( TGSI_FILE_OUTPUT, i );
294}
295
296
297/* Returns a new constant register.  Keep track of which have been
298 * referred to so that we can emit decls later.
299 *
300 * There is nothing in this code to bind this constant to any tracked
301 * value or manage any constant_buffer contents -- that's the
302 * resposibility of the calling code.
303 */
304struct ureg_src ureg_DECL_constant(struct ureg_program *ureg )
305{
306   return ureg_src_register( TGSI_FILE_CONSTANT, ureg->nr_constants++ );
307}
308
309
310/* Allocate a new temporary.  Temporaries greater than UREG_MAX_TEMP
311 * are legal, but will not be released.
312 */
313struct ureg_dst ureg_DECL_temporary( struct ureg_program *ureg )
314{
315   unsigned i;
316
317   for (i = 0; i < UREG_MAX_TEMP; i += 32) {
318      int bit = ffs(~ureg->temps_active[i/32]);
319      if (bit != 0) {
320         i += bit - 1;
321         goto out;
322      }
323   }
324
325   /* No reusable temps, so allocate a new one:
326    */
327   i = ureg->nr_temps++;
328
329out:
330   if (i < UREG_MAX_TEMP)
331      ureg->temps_active[i/32] |= 1 << (i % 32);
332
333   if (i >= ureg->nr_temps)
334      ureg->nr_temps = i + 1;
335
336   return ureg_dst_register( TGSI_FILE_TEMPORARY, i );
337}
338
339
340void ureg_release_temporary( struct ureg_program *ureg,
341                             struct ureg_dst tmp )
342{
343   if (tmp.Index < UREG_MAX_TEMP)
344      ureg->temps_active[tmp.Index/32] &= ~(1 << (tmp.Index % 32));
345}
346
347
348/* Allocate a new sampler.
349 */
350struct ureg_src ureg_DECL_sampler( struct ureg_program *ureg )
351{
352   return ureg_src_register( TGSI_FILE_SAMPLER, ureg->nr_samplers++ );
353}
354
355
356
357
358static int match_or_expand_immediate( const float *v,
359                                      unsigned nr,
360                                      float *v2,
361                                      unsigned *nr2,
362                                      unsigned *swizzle )
363{
364   unsigned i, j;
365
366   for (i = 0; i < nr; i++) {
367      boolean found = FALSE;
368
369      for (j = 0; j < *nr2 && !found; j++) {
370         if (v[i] == v2[j]) {
371            *swizzle |= j << (i * 2);
372            found = TRUE;
373         }
374      }
375
376      if (!found) {
377         if (*nr2 >= 4)
378            return FALSE;
379
380         v2[*nr2] = v[i];
381         *swizzle |= *nr2 << (i * 2);
382         (*nr2)++;
383      }
384   }
385
386   return TRUE;
387}
388
389
390
391
392struct ureg_src ureg_DECL_immediate( struct ureg_program *ureg,
393                                     const float *v,
394                                     unsigned nr )
395{
396   unsigned i;
397   unsigned swizzle = 0;
398
399   /* Could do a first pass where we examine all existing immediates
400    * without expanding.
401    */
402
403   for (i = 0; i < ureg->nr_immediates; i++) {
404      if (match_or_expand_immediate( v,
405                                     nr,
406                                     ureg->immediate[i].v,
407                                     &ureg->immediate[i].nr,
408                                     &swizzle ))
409         goto out;
410   }
411
412   if (ureg->nr_immediates < UREG_MAX_IMMEDIATE) {
413      i = ureg->nr_immediates++;
414      if (match_or_expand_immediate( v,
415                                     nr,
416                                     ureg->immediate[i].v,
417                                     &ureg->immediate[i].nr,
418                                     &swizzle ))
419         goto out;
420   }
421
422   set_bad( ureg );
423
424out:
425   return ureg_swizzle( ureg_src_register( TGSI_FILE_IMMEDIATE, i ),
426                        (swizzle >> 0) & 0x3,
427                        (swizzle >> 2) & 0x3,
428                        (swizzle >> 4) & 0x3,
429                        (swizzle >> 6) & 0x3);
430}
431
432
433void
434ureg_emit_src( struct ureg_program *ureg,
435               struct ureg_src src )
436{
437   unsigned size = (1 +
438                    (src.Absolute ? 1 : 0) +
439                    (src.Indirect ? 1 : 0));
440
441   union tgsi_any_token *out = get_tokens( ureg, DOMAIN_INSN, size );
442   unsigned n = 0;
443
444   out[n].value = 0;
445   out[n].src.File = src.File;
446   out[n].src.SwizzleX = src.SwizzleX;
447   out[n].src.SwizzleY = src.SwizzleY;
448   out[n].src.SwizzleZ = src.SwizzleZ;
449   out[n].src.SwizzleW = src.SwizzleW;
450   out[n].src.Indirect = src.Indirect;
451   out[n].src.Index = src.Index;
452   n++;
453
454   if (src.Absolute) {
455      out[n].value = 0;
456      out[n].src_ext_mod.Absolute = 1;
457      n++;
458   }
459
460   if (src.Indirect) {
461      out[n].value = 0;
462      out[n].src.File = TGSI_FILE_ADDRESS;
463      out[n].src.SwizzleX = TGSI_SWIZZLE_X;
464      out[n].src.SwizzleY = TGSI_SWIZZLE_X;
465      out[n].src.SwizzleZ = TGSI_SWIZZLE_X;
466      out[n].src.SwizzleW = TGSI_SWIZZLE_X;
467      out[n].src.Indirect = 0;
468      out[n].src.Index = 0;
469      n++;
470   }
471
472   assert(n == size);
473}
474
475
476void
477ureg_emit_dst( struct ureg_program *ureg,
478               struct ureg_dst dst )
479{
480   unsigned size = (1 +
481                    (dst.Indirect ? 1 : 0));
482
483   union tgsi_any_token *out = get_tokens( ureg, DOMAIN_INSN, size );
484   unsigned n = 0;
485
486   out[n].value = 0;
487   out[n].dst.File = dst.File;
488   out[n].dst.WriteMask = dst.WriteMask;
489   out[n].dst.Indirect = dst.Indirect;
490   out[n].dst.Index = dst.Index;
491   n++;
492
493   if (dst.Indirect) {
494      out[n].value = 0;
495      out[n].src.File = TGSI_FILE_ADDRESS;
496      out[n].src.SwizzleX = TGSI_SWIZZLE_X;
497      out[n].src.SwizzleY = TGSI_SWIZZLE_X;
498      out[n].src.SwizzleZ = TGSI_SWIZZLE_X;
499      out[n].src.SwizzleW = TGSI_SWIZZLE_X;
500      out[n].src.Indirect = 0;
501      out[n].src.Index = 0;
502      n++;
503   }
504
505   assert(n == size);
506}
507
508
509
510unsigned
511ureg_emit_insn(struct ureg_program *ureg,
512               unsigned opcode,
513               boolean saturate,
514               unsigned num_dst,
515               unsigned num_src )
516{
517   union tgsi_any_token *out;
518
519   out = get_tokens( ureg, DOMAIN_INSN, 1 );
520   out[0].value = 0;
521   out[0].insn.Type = TGSI_TOKEN_TYPE_INSTRUCTION;
522   out[0].insn.NrTokens = 0;
523   out[0].insn.Opcode = opcode;
524   out[0].insn.Saturate = saturate;
525   out[0].insn.NrTokens = 0;
526   out[0].insn.NumDstRegs = num_dst;
527   out[0].insn.NumSrcRegs = num_src;
528   out[0].insn.Padding = 0;
529   out[0].insn.Extended = 0;
530
531   ureg->nr_instructions++;
532
533   return ureg->domain[DOMAIN_INSN].count - 1;
534}
535
536
537void
538ureg_emit_label(struct ureg_program *ureg,
539                unsigned insn_token,
540                unsigned *label_token )
541{
542   union tgsi_any_token *out, *insn;
543
544   out = get_tokens( ureg, DOMAIN_INSN, 1 );
545   insn = retrieve_token( ureg, DOMAIN_INSN, insn_token );
546
547   insn->insn.Extended = 1;
548
549   out[0].value = 0;
550   out[0].insn_ext_label.Type = TGSI_INSTRUCTION_EXT_TYPE_LABEL;
551
552   *label_token = ureg->domain[DOMAIN_INSN].count - 1;
553}
554
555/* Will return a number which can be used in a label to point to the
556 * next instruction to be emitted.
557 */
558unsigned
559ureg_get_instruction_number( struct ureg_program *ureg )
560{
561   return ureg->nr_instructions;
562}
563
564/* Patch a given label (expressed as a token number) to point to a
565 * given instruction (expressed as an instruction number).
566 */
567void
568ureg_fixup_label(struct ureg_program *ureg,
569                 unsigned label_token,
570                 unsigned instruction_number )
571{
572   union tgsi_any_token *out = retrieve_token( ureg, DOMAIN_INSN, label_token );
573
574   assert(out->insn_ext_label.Type == TGSI_INSTRUCTION_EXT_TYPE_LABEL);
575   out->insn_ext_label.Label = instruction_number;
576}
577
578
579void
580ureg_emit_texture(struct ureg_program *ureg,
581                  unsigned insn_token,
582                  unsigned target )
583{
584   union tgsi_any_token *out, *insn;
585
586   out = get_tokens( ureg, DOMAIN_INSN, 1 );
587   insn = retrieve_token( ureg, DOMAIN_INSN, insn_token );
588
589   insn->insn.Extended = 1;
590
591   out[0].value = 0;
592   out[0].insn_ext_texture.Type = TGSI_INSTRUCTION_EXT_TYPE_TEXTURE;
593   out[0].insn_ext_texture.Texture = target;
594}
595
596
597void
598ureg_fixup_insn_size(struct ureg_program *ureg,
599                     unsigned insn )
600{
601   union tgsi_any_token *out = retrieve_token( ureg, DOMAIN_INSN, insn );
602
603   assert(out->insn.Type == TGSI_TOKEN_TYPE_INSTRUCTION);
604   out->insn.NrTokens = ureg->domain[DOMAIN_INSN].count - insn - 1;
605}
606
607
608void
609ureg_insn(struct ureg_program *ureg,
610          unsigned opcode,
611          const struct ureg_dst *dst,
612          unsigned nr_dst,
613          const struct ureg_src *src,
614          unsigned nr_src )
615{
616   unsigned insn, i;
617   boolean saturate;
618
619   saturate = nr_dst ? dst[0].Saturate : FALSE;
620
621   insn = ureg_emit_insn( ureg, opcode, saturate, nr_dst, nr_src );
622
623   for (i = 0; i < nr_dst; i++)
624      ureg_emit_dst( ureg, dst[i] );
625
626   for (i = 0; i < nr_src; i++)
627      ureg_emit_src( ureg, src[i] );
628
629   ureg_fixup_insn_size( ureg, insn );
630}
631
632
633
634static void emit_decl( struct ureg_program *ureg,
635                       unsigned file,
636                       unsigned index,
637                       unsigned semantic_name,
638                       unsigned semantic_index,
639                       unsigned interp )
640{
641   union tgsi_any_token *out = get_tokens( ureg, DOMAIN_DECL, 3 );
642
643   out[0].value = 0;
644   out[0].decl.Type = TGSI_TOKEN_TYPE_DECLARATION;
645   out[0].decl.NrTokens = 3;
646   out[0].decl.File = file;
647   out[0].decl.UsageMask = TGSI_WRITEMASK_XYZW; /* FIXME! */
648   out[0].decl.Interpolate = interp;
649   out[0].decl.Semantic = 1;
650
651   out[1].value = 0;
652   out[1].decl_range.First =
653      out[1].decl_range.Last = index;
654
655   out[2].value = 0;
656   out[2].decl_semantic.SemanticName = semantic_name;
657   out[2].decl_semantic.SemanticIndex = semantic_index;
658
659}
660
661
662static void emit_decl_range( struct ureg_program *ureg,
663                             unsigned file,
664                             unsigned first,
665                             unsigned count )
666{
667   union tgsi_any_token *out = get_tokens( ureg, DOMAIN_DECL, 2 );
668
669   out[0].value = 0;
670   out[0].decl.Type = TGSI_TOKEN_TYPE_DECLARATION;
671   out[0].decl.NrTokens = 2;
672   out[0].decl.File = file;
673   out[0].decl.UsageMask = 0xf;
674   out[0].decl.Interpolate = TGSI_INTERPOLATE_CONSTANT;
675   out[0].decl.Semantic = 0;
676
677   out[1].value = 0;
678   out[1].decl_range.First = first;
679   out[1].decl_range.Last = first + count - 1;
680}
681
682static void emit_immediate( struct ureg_program *ureg,
683                            const float *v )
684{
685   union tgsi_any_token *out = get_tokens( ureg, DOMAIN_DECL, 5 );
686
687   out[0].value = 0;
688   out[0].imm.Type = TGSI_TOKEN_TYPE_IMMEDIATE;
689   out[0].imm.NrTokens = 5;
690   out[0].imm.DataType = TGSI_IMM_FLOAT32;
691   out[0].imm.Padding = 0;
692   out[0].imm.Extended = 0;
693
694   out[1].imm_data.Float = v[0];
695   out[2].imm_data.Float = v[1];
696   out[3].imm_data.Float = v[2];
697   out[4].imm_data.Float = v[3];
698}
699
700
701
702
703static void emit_decls( struct ureg_program *ureg )
704{
705   unsigned i;
706
707   for (i = 0; i < ureg->nr_inputs; i++) {
708      emit_decl( ureg,
709                 TGSI_FILE_INPUT,
710                 i,
711                 ureg->input[i].semantic_name,
712                 ureg->input[i].semantic_index,
713                 ureg->input[i].interp );
714   }
715
716   for (i = 0; i < ureg->nr_outputs; i++) {
717      emit_decl( ureg,
718                 TGSI_FILE_OUTPUT,
719                 i,
720                 ureg->output[i].semantic_name,
721                 ureg->output[i].semantic_index,
722                 TGSI_INTERPOLATE_CONSTANT );
723   }
724
725   if (ureg->nr_samplers) {
726      emit_decl_range( ureg,
727                       TGSI_FILE_SAMPLER,
728                       0, ureg->nr_samplers );
729   }
730
731   if (ureg->nr_constants) {
732      emit_decl_range( ureg,
733                       TGSI_FILE_CONSTANT,
734                       0, ureg->nr_constants );
735   }
736
737   if (ureg->nr_temps) {
738      emit_decl_range( ureg,
739                       TGSI_FILE_TEMPORARY,
740                       0, ureg->nr_temps );
741   }
742
743   for (i = 0; i < ureg->nr_immediates; i++) {
744      emit_immediate( ureg,
745                      ureg->immediate[i].v );
746   }
747}
748
749/* Append the instruction tokens onto the declarations to build a
750 * contiguous stream suitable to send to the driver.
751 */
752static void copy_instructions( struct ureg_program *ureg )
753{
754   unsigned nr_tokens = ureg->domain[DOMAIN_INSN].count;
755   union tgsi_any_token *out = get_tokens( ureg,
756                                           DOMAIN_DECL,
757                                           nr_tokens );
758
759   memcpy(out,
760          ureg->domain[DOMAIN_INSN].tokens,
761          nr_tokens * sizeof out[0] );
762}
763
764
765static void
766fixup_header_size(struct ureg_program *ureg )
767{
768   union tgsi_any_token *out = retrieve_token( ureg, DOMAIN_DECL, 1 );
769
770   out->header.BodySize = ureg->domain[DOMAIN_DECL].count - 3;
771}
772
773
774static void
775emit_header( struct ureg_program *ureg )
776{
777   union tgsi_any_token *out = get_tokens( ureg, DOMAIN_DECL, 3 );
778
779   out[0].version.MajorVersion = 1;
780   out[0].version.MinorVersion = 1;
781   out[0].version.Padding = 0;
782
783   out[1].header.HeaderSize = 2;
784   out[1].header.BodySize = 0;
785
786   out[2].processor.Processor = ureg->processor;
787   out[2].processor.Padding = 0;
788}
789
790
791const struct tgsi_token *ureg_finalize( struct ureg_program *ureg )
792{
793   const struct tgsi_token *tokens;
794
795   emit_header( ureg );
796   emit_decls( ureg );
797   copy_instructions( ureg );
798   fixup_header_size( ureg );
799
800   if (ureg->domain[0].tokens == error_tokens ||
801       ureg->domain[1].tokens == error_tokens) {
802      debug_printf("%s: error in generated shader\n", __FUNCTION__);
803      assert(0);
804      return NULL;
805   }
806
807   tokens = &ureg->domain[DOMAIN_DECL].tokens[0].token;
808
809   if (0) {
810      debug_printf("%s: emitted shader %d tokens:\n", __FUNCTION__,
811                   ureg->domain[DOMAIN_DECL].count);
812      tgsi_dump( tokens, 0 );
813   }
814
815   return tokens;
816}
817
818
819void *ureg_create_shader( struct ureg_program *ureg,
820                          struct pipe_context *pipe )
821{
822   struct pipe_shader_state state;
823
824   state.tokens = ureg_finalize(ureg);
825   if(!state.tokens)
826      return NULL;
827
828   if (ureg->processor == TGSI_PROCESSOR_VERTEX)
829      return pipe->create_vs_state( pipe, &state );
830   else
831      return pipe->create_fs_state( pipe, &state );
832}
833
834
835
836
837struct ureg_program *ureg_create( unsigned processor )
838{
839   struct ureg_program *ureg = CALLOC_STRUCT( ureg_program );
840   if (ureg == NULL)
841      return NULL;
842
843   ureg->processor = processor;
844   return ureg;
845}
846
847
848void ureg_destroy( struct ureg_program *ureg )
849{
850   unsigned i;
851
852   for (i = 0; i < Elements(ureg->domain); i++) {
853      if (ureg->domain[i].tokens &&
854          ureg->domain[i].tokens != error_tokens)
855         FREE(ureg->domain[i].tokens);
856   }
857
858   FREE(ureg);
859}
860