tgsi_ureg.c revision c69d9296fbf02ed2a50e37456a733bef885995ca
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
608
609
610
611static void emit_decl( struct ureg_program *ureg,
612                       unsigned file,
613                       unsigned index,
614                       unsigned semantic_name,
615                       unsigned semantic_index,
616                       unsigned interp )
617{
618   union tgsi_any_token *out = get_tokens( ureg, DOMAIN_DECL, 3 );
619
620   out[0].value = 0;
621   out[0].decl.Type = TGSI_TOKEN_TYPE_DECLARATION;
622   out[0].decl.NrTokens = 3;
623   out[0].decl.File = file;
624   out[0].decl.UsageMask = TGSI_WRITEMASK_XYZW; /* FIXME! */
625   out[0].decl.Interpolate = interp;
626   out[0].decl.Semantic = 1;
627
628   out[1].value = 0;
629   out[1].decl_range.First =
630      out[1].decl_range.Last = index;
631
632   out[2].value = 0;
633   out[2].decl_semantic.SemanticName = semantic_name;
634   out[2].decl_semantic.SemanticIndex = semantic_index;
635
636}
637
638
639static void emit_decl_range( struct ureg_program *ureg,
640                             unsigned file,
641                             unsigned first,
642                             unsigned count )
643{
644   union tgsi_any_token *out = get_tokens( ureg, DOMAIN_DECL, 2 );
645
646   out[0].value = 0;
647   out[0].decl.Type = TGSI_TOKEN_TYPE_DECLARATION;
648   out[0].decl.NrTokens = 2;
649   out[0].decl.File = file;
650   out[0].decl.UsageMask = 0xf;
651   out[0].decl.Interpolate = TGSI_INTERPOLATE_CONSTANT;
652   out[0].decl.Semantic = 0;
653
654   out[1].value = 0;
655   out[1].decl_range.First = first;
656   out[1].decl_range.Last = first + count - 1;
657}
658
659static void emit_immediate( struct ureg_program *ureg,
660                            const float *v )
661{
662   union tgsi_any_token *out = get_tokens( ureg, DOMAIN_DECL, 5 );
663
664   out[0].value = 0;
665   out[0].imm.Type = TGSI_TOKEN_TYPE_IMMEDIATE;
666   out[0].imm.NrTokens = 5;
667   out[0].imm.DataType = TGSI_IMM_FLOAT32;
668   out[0].imm.Padding = 0;
669   out[0].imm.Extended = 0;
670
671   out[1].imm_data.Float = v[0];
672   out[2].imm_data.Float = v[1];
673   out[3].imm_data.Float = v[2];
674   out[4].imm_data.Float = v[3];
675}
676
677
678
679
680static void emit_decls( struct ureg_program *ureg )
681{
682   unsigned i;
683
684   for (i = 0; i < ureg->nr_inputs; i++) {
685      emit_decl( ureg,
686                 TGSI_FILE_INPUT,
687                 i,
688                 ureg->input[i].semantic_name,
689                 ureg->input[i].semantic_index,
690                 ureg->input[i].interp );
691   }
692
693   for (i = 0; i < ureg->nr_outputs; i++) {
694      emit_decl( ureg,
695                 TGSI_FILE_OUTPUT,
696                 i,
697                 ureg->output[i].semantic_name,
698                 ureg->output[i].semantic_index,
699                 TGSI_INTERPOLATE_CONSTANT );
700   }
701
702   if (ureg->nr_samplers) {
703      emit_decl_range( ureg,
704                       TGSI_FILE_SAMPLER,
705                       0, ureg->nr_samplers );
706   }
707
708   if (ureg->nr_constants) {
709      emit_decl_range( ureg,
710                       TGSI_FILE_CONSTANT,
711                       0, ureg->nr_constants );
712   }
713
714   if (ureg->nr_temps) {
715      emit_decl_range( ureg,
716                       TGSI_FILE_TEMPORARY,
717                       0, ureg->nr_temps );
718   }
719
720   for (i = 0; i < ureg->nr_immediates; i++) {
721      emit_immediate( ureg,
722                      ureg->immediate[i].v );
723   }
724}
725
726/* Append the instruction tokens onto the declarations to build a
727 * contiguous stream suitable to send to the driver.
728 */
729static void copy_instructions( struct ureg_program *ureg )
730{
731   unsigned nr_tokens = ureg->domain[DOMAIN_INSN].count;
732   union tgsi_any_token *out = get_tokens( ureg,
733                                           DOMAIN_DECL,
734                                           nr_tokens );
735
736   memcpy(out,
737          ureg->domain[DOMAIN_INSN].tokens,
738          nr_tokens * sizeof out[0] );
739}
740
741
742static void
743fixup_header_size(struct ureg_program *ureg )
744{
745   union tgsi_any_token *out = retrieve_token( ureg, DOMAIN_DECL, 1 );
746
747   out->header.BodySize = ureg->domain[DOMAIN_DECL].count - 3;
748}
749
750
751static void
752emit_header( struct ureg_program *ureg )
753{
754   union tgsi_any_token *out = get_tokens( ureg, DOMAIN_DECL, 3 );
755
756   out[0].version.MajorVersion = 1;
757   out[0].version.MinorVersion = 1;
758   out[0].version.Padding = 0;
759
760   out[1].header.HeaderSize = 2;
761   out[1].header.BodySize = 0;
762
763   out[2].processor.Processor = ureg->processor;
764   out[2].processor.Padding = 0;
765}
766
767
768void *ureg_create_shader( struct ureg_program *ureg )
769{
770   struct pipe_shader_state state;
771
772   emit_header( ureg );
773   emit_decls( ureg );
774   copy_instructions( ureg );
775   fixup_header_size( ureg );
776
777   if (ureg->domain[0].tokens == error_tokens ||
778       ureg->domain[1].tokens == error_tokens) {
779      debug_printf("%s: error in generated shader\n", __FUNCTION__);
780      assert(0);
781      return NULL;
782   }
783
784   state.tokens = (const struct tgsi_token *)ureg->domain[DOMAIN_DECL].tokens;
785
786   if (0) {
787      debug_printf("%s: emitted shader %d tokens:\n", __FUNCTION__,
788                   ureg->domain[DOMAIN_DECL].count);
789      tgsi_dump( state.tokens, 0 );
790   }
791
792   if (ureg->processor == TGSI_PROCESSOR_VERTEX)
793      return ureg->pipe->create_vs_state( ureg->pipe, &state );
794   else
795      return ureg->pipe->create_fs_state( ureg->pipe, &state );
796}
797
798
799
800
801struct ureg_program *ureg_create( struct pipe_context *pipe,
802                                  unsigned processor )
803{
804   struct ureg_program *ureg = CALLOC_STRUCT( ureg_program );
805   if (ureg == NULL)
806      return NULL;
807
808   ureg->pipe = pipe;
809   ureg->processor = processor;
810   return ureg;
811}
812
813
814void ureg_destroy( struct ureg_program *ureg )
815{
816   unsigned i;
817
818   for (i = 0; i < Elements(ureg->domain); i++) {
819      if (ureg->domain[i].tokens &&
820          ureg->domain[i].tokens != error_tokens)
821         FREE(ureg->domain[i].tokens);
822   }
823
824   FREE(ureg);
825}
826