tgsi_ureg.c revision bdbe77f9c6f06cfaa155f27c2ade3c523d7fbea7
1/**************************************************************************
2 *
3 * Copyright 2009-2010 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_build.h"
33#include "tgsi/tgsi_info.h"
34#include "tgsi/tgsi_dump.h"
35#include "tgsi/tgsi_sanity.h"
36#include "util/u_debug.h"
37#include "util/u_memory.h"
38#include "util/u_math.h"
39
40union tgsi_any_token {
41   struct tgsi_header header;
42   struct tgsi_processor processor;
43   struct tgsi_token token;
44   struct tgsi_property prop;
45   struct tgsi_property_data prop_data;
46   struct tgsi_declaration decl;
47   struct tgsi_declaration_range decl_range;
48   struct tgsi_declaration_dimension decl_dim;
49   struct tgsi_declaration_semantic decl_semantic;
50   struct tgsi_declaration_resource decl_resource;
51   struct tgsi_immediate imm;
52   union  tgsi_immediate_data imm_data;
53   struct tgsi_instruction insn;
54   struct tgsi_instruction_predicate insn_predicate;
55   struct tgsi_instruction_label insn_label;
56   struct tgsi_instruction_texture insn_texture;
57   struct tgsi_src_register src;
58   struct tgsi_dimension dim;
59   struct tgsi_dst_register dst;
60   unsigned value;
61};
62
63
64struct ureg_tokens {
65   union tgsi_any_token *tokens;
66   unsigned size;
67   unsigned order;
68   unsigned count;
69};
70
71#define UREG_MAX_INPUT PIPE_MAX_ATTRIBS
72#define UREG_MAX_SYSTEM_VALUE PIPE_MAX_ATTRIBS
73#define UREG_MAX_OUTPUT PIPE_MAX_ATTRIBS
74#define UREG_MAX_CONSTANT_RANGE 32
75#define UREG_MAX_IMMEDIATE 32
76#define UREG_MAX_TEMP 256
77#define UREG_MAX_ADDR 2
78#define UREG_MAX_PRED 1
79
80struct const_decl {
81   struct {
82      unsigned first;
83      unsigned last;
84   } constant_range[UREG_MAX_CONSTANT_RANGE];
85   unsigned nr_constant_ranges;
86};
87
88#define DOMAIN_DECL 0
89#define DOMAIN_INSN 1
90
91struct ureg_program
92{
93   unsigned processor;
94   struct pipe_context *pipe;
95
96   struct {
97      unsigned semantic_name;
98      unsigned semantic_index;
99      unsigned interp;
100      unsigned char cylindrical_wrap;
101      unsigned char centroid;
102   } fs_input[UREG_MAX_INPUT];
103   unsigned nr_fs_inputs;
104
105   unsigned vs_inputs[UREG_MAX_INPUT/32];
106
107   struct {
108      unsigned index;
109      unsigned semantic_name;
110      unsigned semantic_index;
111   } gs_input[UREG_MAX_INPUT];
112   unsigned nr_gs_inputs;
113
114   struct {
115      unsigned index;
116      unsigned semantic_name;
117      unsigned semantic_index;
118   } system_value[UREG_MAX_SYSTEM_VALUE];
119   unsigned nr_system_values;
120
121   struct {
122      unsigned semantic_name;
123      unsigned semantic_index;
124   } output[UREG_MAX_OUTPUT];
125   unsigned nr_outputs;
126
127   struct {
128      union {
129         float f[4];
130         unsigned u[4];
131         int i[4];
132      } value;
133      unsigned nr;
134      unsigned type;
135   } immediate[UREG_MAX_IMMEDIATE];
136   unsigned nr_immediates;
137
138   struct ureg_src sampler[PIPE_MAX_SAMPLERS];
139   unsigned nr_samplers;
140
141   struct {
142      unsigned index;
143      unsigned target;
144      unsigned return_type_x;
145      unsigned return_type_y;
146      unsigned return_type_z;
147      unsigned return_type_w;
148   } resource[PIPE_MAX_SHADER_RESOURCES];
149   unsigned nr_resources;
150
151   unsigned temps_active[UREG_MAX_TEMP / 32];
152   unsigned nr_temps;
153
154   struct const_decl const_decls;
155   struct const_decl const_decls2D[PIPE_MAX_CONSTANT_BUFFERS];
156
157   unsigned property_gs_input_prim;
158   unsigned property_gs_output_prim;
159   unsigned property_gs_max_vertices;
160   unsigned char property_fs_coord_origin; /* = TGSI_FS_COORD_ORIGIN_* */
161   unsigned char property_fs_coord_pixel_center; /* = TGSI_FS_COORD_PIXEL_CENTER_* */
162   unsigned char property_fs_color0_writes_all_cbufs; /* = TGSI_FS_COLOR0_WRITES_ALL_CBUFS * */
163
164   unsigned nr_addrs;
165   unsigned nr_preds;
166   unsigned nr_instructions;
167
168   struct ureg_tokens domain[2];
169};
170
171static union tgsi_any_token error_tokens[32];
172
173static void tokens_error( struct ureg_tokens *tokens )
174{
175   if (tokens->tokens && tokens->tokens != error_tokens)
176      FREE(tokens->tokens);
177
178   tokens->tokens = error_tokens;
179   tokens->size = Elements(error_tokens);
180   tokens->count = 0;
181}
182
183
184static void tokens_expand( struct ureg_tokens *tokens,
185                           unsigned count )
186{
187   unsigned old_size = tokens->size * sizeof(unsigned);
188
189   if (tokens->tokens == error_tokens) {
190      return;
191   }
192
193   while (tokens->count + count > tokens->size) {
194      tokens->size = (1 << ++tokens->order);
195   }
196
197   tokens->tokens = REALLOC(tokens->tokens,
198                            old_size,
199                            tokens->size * sizeof(unsigned));
200   if (tokens->tokens == NULL) {
201      tokens_error(tokens);
202   }
203}
204
205static void set_bad( struct ureg_program *ureg )
206{
207   tokens_error(&ureg->domain[0]);
208}
209
210
211
212static union tgsi_any_token *get_tokens( struct ureg_program *ureg,
213                                         unsigned domain,
214                                         unsigned count )
215{
216   struct ureg_tokens *tokens = &ureg->domain[domain];
217   union tgsi_any_token *result;
218
219   if (tokens->count + count > tokens->size)
220      tokens_expand(tokens, count);
221
222   result = &tokens->tokens[tokens->count];
223   tokens->count += count;
224   return result;
225}
226
227
228static union tgsi_any_token *retrieve_token( struct ureg_program *ureg,
229                                            unsigned domain,
230                                            unsigned nr )
231{
232   if (ureg->domain[domain].tokens == error_tokens)
233      return &error_tokens[0];
234
235   return &ureg->domain[domain].tokens[nr];
236}
237
238
239
240static INLINE struct ureg_dst
241ureg_dst_register( unsigned file,
242                   unsigned index )
243{
244   struct ureg_dst dst;
245
246   dst.File      = file;
247   dst.WriteMask = TGSI_WRITEMASK_XYZW;
248   dst.Indirect  = 0;
249   dst.IndirectIndex = 0;
250   dst.IndirectSwizzle = 0;
251   dst.Saturate  = 0;
252   dst.Predicate = 0;
253   dst.PredNegate = 0;
254   dst.PredSwizzleX = TGSI_SWIZZLE_X;
255   dst.PredSwizzleY = TGSI_SWIZZLE_Y;
256   dst.PredSwizzleZ = TGSI_SWIZZLE_Z;
257   dst.PredSwizzleW = TGSI_SWIZZLE_W;
258   dst.Index     = index;
259
260   return dst;
261}
262
263
264void
265ureg_property_gs_input_prim(struct ureg_program *ureg,
266                            unsigned input_prim)
267{
268   ureg->property_gs_input_prim = input_prim;
269}
270
271void
272ureg_property_gs_output_prim(struct ureg_program *ureg,
273                             unsigned output_prim)
274{
275   ureg->property_gs_output_prim = output_prim;
276}
277
278void
279ureg_property_gs_max_vertices(struct ureg_program *ureg,
280                              unsigned max_vertices)
281{
282   ureg->property_gs_max_vertices = max_vertices;
283}
284
285void
286ureg_property_fs_coord_origin(struct ureg_program *ureg,
287                            unsigned fs_coord_origin)
288{
289   ureg->property_fs_coord_origin = fs_coord_origin;
290}
291
292void
293ureg_property_fs_coord_pixel_center(struct ureg_program *ureg,
294                            unsigned fs_coord_pixel_center)
295{
296   ureg->property_fs_coord_pixel_center = fs_coord_pixel_center;
297}
298
299void
300ureg_property_fs_color0_writes_all_cbufs(struct ureg_program *ureg,
301                            unsigned fs_color0_writes_all_cbufs)
302{
303   ureg->property_fs_color0_writes_all_cbufs = fs_color0_writes_all_cbufs;
304}
305
306struct ureg_src
307ureg_DECL_fs_input_cyl_centroid(struct ureg_program *ureg,
308                       unsigned semantic_name,
309                       unsigned semantic_index,
310                       unsigned interp_mode,
311                       unsigned cylindrical_wrap,
312                       unsigned centroid)
313{
314   unsigned i;
315
316   for (i = 0; i < ureg->nr_fs_inputs; i++) {
317      if (ureg->fs_input[i].semantic_name == semantic_name &&
318          ureg->fs_input[i].semantic_index == semantic_index) {
319         goto out;
320      }
321   }
322
323   if (ureg->nr_fs_inputs < UREG_MAX_INPUT) {
324      ureg->fs_input[i].semantic_name = semantic_name;
325      ureg->fs_input[i].semantic_index = semantic_index;
326      ureg->fs_input[i].interp = interp_mode;
327      ureg->fs_input[i].cylindrical_wrap = cylindrical_wrap;
328      ureg->fs_input[i].centroid = centroid;
329      ureg->nr_fs_inputs++;
330   } else {
331      set_bad(ureg);
332   }
333
334out:
335   return ureg_src_register(TGSI_FILE_INPUT, i);
336}
337
338
339struct ureg_src
340ureg_DECL_vs_input( struct ureg_program *ureg,
341                    unsigned index )
342{
343   assert(ureg->processor == TGSI_PROCESSOR_VERTEX);
344
345   ureg->vs_inputs[index/32] |= 1 << (index % 32);
346   return ureg_src_register( TGSI_FILE_INPUT, index );
347}
348
349
350struct ureg_src
351ureg_DECL_gs_input(struct ureg_program *ureg,
352                   unsigned index,
353                   unsigned semantic_name,
354                   unsigned semantic_index)
355{
356   if (ureg->nr_gs_inputs < UREG_MAX_INPUT) {
357      ureg->gs_input[ureg->nr_gs_inputs].index = index;
358      ureg->gs_input[ureg->nr_gs_inputs].semantic_name = semantic_name;
359      ureg->gs_input[ureg->nr_gs_inputs].semantic_index = semantic_index;
360      ureg->nr_gs_inputs++;
361   } else {
362      set_bad(ureg);
363   }
364
365   /* XXX: Add suport for true 2D input registers. */
366   return ureg_src_register(TGSI_FILE_INPUT, index);
367}
368
369
370struct ureg_src
371ureg_DECL_system_value(struct ureg_program *ureg,
372                       unsigned index,
373                       unsigned semantic_name,
374                       unsigned semantic_index)
375{
376   if (ureg->nr_system_values < UREG_MAX_SYSTEM_VALUE) {
377      ureg->system_value[ureg->nr_system_values].index = index;
378      ureg->system_value[ureg->nr_system_values].semantic_name = semantic_name;
379      ureg->system_value[ureg->nr_system_values].semantic_index = semantic_index;
380      ureg->nr_system_values++;
381   } else {
382      set_bad(ureg);
383   }
384
385   return ureg_src_register(TGSI_FILE_SYSTEM_VALUE, index);
386}
387
388
389struct ureg_dst
390ureg_DECL_output( struct ureg_program *ureg,
391                  unsigned name,
392                  unsigned index )
393{
394   unsigned i;
395
396   for (i = 0; i < ureg->nr_outputs; i++) {
397      if (ureg->output[i].semantic_name == name &&
398          ureg->output[i].semantic_index == index)
399         goto out;
400   }
401
402   if (ureg->nr_outputs < UREG_MAX_OUTPUT) {
403      ureg->output[i].semantic_name = name;
404      ureg->output[i].semantic_index = index;
405      ureg->nr_outputs++;
406   }
407   else {
408      set_bad( ureg );
409   }
410
411out:
412   return ureg_dst_register( TGSI_FILE_OUTPUT, i );
413}
414
415
416/* Returns a new constant register.  Keep track of which have been
417 * referred to so that we can emit decls later.
418 *
419 * Constant operands declared with this function must be addressed
420 * with a two-dimensional index.
421 *
422 * There is nothing in this code to bind this constant to any tracked
423 * value or manage any constant_buffer contents -- that's the
424 * resposibility of the calling code.
425 */
426void
427ureg_DECL_constant2D(struct ureg_program *ureg,
428                     unsigned first,
429                     unsigned last,
430                     unsigned index2D)
431{
432   struct const_decl *decl = &ureg->const_decls2D[index2D];
433
434   assert(index2D < PIPE_MAX_CONSTANT_BUFFERS);
435
436   if (decl->nr_constant_ranges < UREG_MAX_CONSTANT_RANGE) {
437      uint i = decl->nr_constant_ranges++;
438
439      decl->constant_range[i].first = first;
440      decl->constant_range[i].last = last;
441   }
442}
443
444
445/* A one-dimensional, depricated version of ureg_DECL_constant2D().
446 *
447 * Constant operands declared with this function must be addressed
448 * with a one-dimensional index.
449 */
450struct ureg_src
451ureg_DECL_constant(struct ureg_program *ureg,
452                   unsigned index)
453{
454   struct const_decl *decl = &ureg->const_decls;
455   unsigned minconst = index, maxconst = index;
456   unsigned i;
457
458   /* Inside existing range?
459    */
460   for (i = 0; i < decl->nr_constant_ranges; i++) {
461      if (decl->constant_range[i].first <= index &&
462          decl->constant_range[i].last >= index) {
463         goto out;
464      }
465   }
466
467   /* Extend existing range?
468    */
469   for (i = 0; i < decl->nr_constant_ranges; i++) {
470      if (decl->constant_range[i].last == index - 1) {
471         decl->constant_range[i].last = index;
472         goto out;
473      }
474
475      if (decl->constant_range[i].first == index + 1) {
476         decl->constant_range[i].first = index;
477         goto out;
478      }
479
480      minconst = MIN2(minconst, decl->constant_range[i].first);
481      maxconst = MAX2(maxconst, decl->constant_range[i].last);
482   }
483
484   /* Create new range?
485    */
486   if (decl->nr_constant_ranges < UREG_MAX_CONSTANT_RANGE) {
487      i = decl->nr_constant_ranges++;
488      decl->constant_range[i].first = index;
489      decl->constant_range[i].last = index;
490      goto out;
491   }
492
493   /* Collapse all ranges down to one:
494    */
495   i = 0;
496   decl->constant_range[0].first = minconst;
497   decl->constant_range[0].last = maxconst;
498   decl->nr_constant_ranges = 1;
499
500out:
501   assert(i < decl->nr_constant_ranges);
502   assert(decl->constant_range[i].first <= index);
503   assert(decl->constant_range[i].last >= index);
504   return ureg_src_register(TGSI_FILE_CONSTANT, index);
505}
506
507
508/* Allocate a new temporary.  Temporaries greater than UREG_MAX_TEMP
509 * are legal, but will not be released.
510 */
511struct ureg_dst ureg_DECL_temporary( struct ureg_program *ureg )
512{
513   unsigned i;
514
515   for (i = 0; i < UREG_MAX_TEMP; i += 32) {
516      int bit = ffs(~ureg->temps_active[i/32]);
517      if (bit != 0) {
518         i += bit - 1;
519         goto out;
520      }
521   }
522
523   /* No reusable temps, so allocate a new one:
524    */
525   i = ureg->nr_temps++;
526
527out:
528   if (i < UREG_MAX_TEMP)
529      ureg->temps_active[i/32] |= 1 << (i % 32);
530
531   if (i >= ureg->nr_temps)
532      ureg->nr_temps = i + 1;
533
534   return ureg_dst_register( TGSI_FILE_TEMPORARY, i );
535}
536
537
538void ureg_release_temporary( struct ureg_program *ureg,
539                             struct ureg_dst tmp )
540{
541   if(tmp.File == TGSI_FILE_TEMPORARY)
542      if (tmp.Index < UREG_MAX_TEMP)
543         ureg->temps_active[tmp.Index/32] &= ~(1 << (tmp.Index % 32));
544}
545
546
547/* Allocate a new address register.
548 */
549struct ureg_dst ureg_DECL_address( struct ureg_program *ureg )
550{
551   if (ureg->nr_addrs < UREG_MAX_ADDR)
552      return ureg_dst_register( TGSI_FILE_ADDRESS, ureg->nr_addrs++ );
553
554   assert( 0 );
555   return ureg_dst_register( TGSI_FILE_ADDRESS, 0 );
556}
557
558/* Allocate a new predicate register.
559 */
560struct ureg_dst
561ureg_DECL_predicate(struct ureg_program *ureg)
562{
563   if (ureg->nr_preds < UREG_MAX_PRED) {
564      return ureg_dst_register(TGSI_FILE_PREDICATE, ureg->nr_preds++);
565   }
566
567   assert(0);
568   return ureg_dst_register(TGSI_FILE_PREDICATE, 0);
569}
570
571/* Allocate a new sampler.
572 */
573struct ureg_src ureg_DECL_sampler( struct ureg_program *ureg,
574                                   unsigned nr )
575{
576   unsigned i;
577
578   for (i = 0; i < ureg->nr_samplers; i++)
579      if (ureg->sampler[i].Index == nr)
580         return ureg->sampler[i];
581
582   if (i < PIPE_MAX_SAMPLERS) {
583      ureg->sampler[i] = ureg_src_register( TGSI_FILE_SAMPLER, nr );
584      ureg->nr_samplers++;
585      return ureg->sampler[i];
586   }
587
588   assert( 0 );
589   return ureg->sampler[0];
590}
591
592/*
593 * Allocate a new shader resource.
594 */
595struct ureg_src
596ureg_DECL_resource(struct ureg_program *ureg,
597                   unsigned index,
598                   unsigned target,
599                   unsigned return_type_x,
600                   unsigned return_type_y,
601                   unsigned return_type_z,
602                   unsigned return_type_w)
603{
604   struct ureg_src reg = ureg_src_register(TGSI_FILE_RESOURCE, index);
605   uint i;
606
607   for (i = 0; i < ureg->nr_resources; i++) {
608      if (ureg->resource[i].index == index) {
609         return reg;
610      }
611   }
612
613   if (i < PIPE_MAX_SHADER_RESOURCES) {
614      ureg->resource[i].index = index;
615      ureg->resource[i].target = target;
616      ureg->resource[i].return_type_x = return_type_x;
617      ureg->resource[i].return_type_y = return_type_y;
618      ureg->resource[i].return_type_z = return_type_z;
619      ureg->resource[i].return_type_w = return_type_w;
620      ureg->nr_resources++;
621      return reg;
622   }
623
624   assert(0);
625   return reg;
626}
627
628static int
629match_or_expand_immediate( const unsigned *v,
630                           unsigned nr,
631                           unsigned *v2,
632                           unsigned *pnr2,
633                           unsigned *swizzle )
634{
635   unsigned nr2 = *pnr2;
636   unsigned i, j;
637
638   *swizzle = 0;
639
640   for (i = 0; i < nr; i++) {
641      boolean found = FALSE;
642
643      for (j = 0; j < nr2 && !found; j++) {
644         if (v[i] == v2[j]) {
645            *swizzle |= j << (i * 2);
646            found = TRUE;
647         }
648      }
649
650      if (!found) {
651         if (nr2 >= 4) {
652            return FALSE;
653         }
654
655         v2[nr2] = v[i];
656         *swizzle |= nr2 << (i * 2);
657         nr2++;
658      }
659   }
660
661   /* Actually expand immediate only when fully succeeded.
662    */
663   *pnr2 = nr2;
664   return TRUE;
665}
666
667
668static struct ureg_src
669decl_immediate( struct ureg_program *ureg,
670                const unsigned *v,
671                unsigned nr,
672                unsigned type )
673{
674   unsigned i, j;
675   unsigned swizzle = 0;
676
677   /* Could do a first pass where we examine all existing immediates
678    * without expanding.
679    */
680
681   for (i = 0; i < ureg->nr_immediates; i++) {
682      if (ureg->immediate[i].type != type) {
683         continue;
684      }
685      if (match_or_expand_immediate(v,
686                                    nr,
687                                    ureg->immediate[i].value.u,
688                                    &ureg->immediate[i].nr,
689                                    &swizzle)) {
690         goto out;
691      }
692   }
693
694   if (ureg->nr_immediates < UREG_MAX_IMMEDIATE) {
695      i = ureg->nr_immediates++;
696      ureg->immediate[i].type = type;
697      if (match_or_expand_immediate(v,
698                                    nr,
699                                    ureg->immediate[i].value.u,
700                                    &ureg->immediate[i].nr,
701                                    &swizzle)) {
702         goto out;
703      }
704   }
705
706   set_bad(ureg);
707
708out:
709   /* Make sure that all referenced elements are from this immediate.
710    * Has the effect of making size-one immediates into scalars.
711    */
712   for (j = nr; j < 4; j++) {
713      swizzle |= (swizzle & 0x3) << (j * 2);
714   }
715
716   return ureg_swizzle(ureg_src_register(TGSI_FILE_IMMEDIATE, i),
717                       (swizzle >> 0) & 0x3,
718                       (swizzle >> 2) & 0x3,
719                       (swizzle >> 4) & 0x3,
720                       (swizzle >> 6) & 0x3);
721}
722
723
724struct ureg_src
725ureg_DECL_immediate( struct ureg_program *ureg,
726                     const float *v,
727                     unsigned nr )
728{
729   union {
730      float f[4];
731      unsigned u[4];
732   } fu;
733   unsigned int i;
734
735   for (i = 0; i < nr; i++) {
736      fu.f[i] = v[i];
737   }
738
739   return decl_immediate(ureg, fu.u, nr, TGSI_IMM_FLOAT32);
740}
741
742
743struct ureg_src
744ureg_DECL_immediate_uint( struct ureg_program *ureg,
745                          const unsigned *v,
746                          unsigned nr )
747{
748   return decl_immediate(ureg, v, nr, TGSI_IMM_UINT32);
749}
750
751
752struct ureg_src
753ureg_DECL_immediate_block_uint( struct ureg_program *ureg,
754                                const unsigned *v,
755                                unsigned nr )
756{
757   uint index;
758   uint i;
759
760   if (ureg->nr_immediates + (nr + 3) / 4 > UREG_MAX_IMMEDIATE) {
761      set_bad(ureg);
762      return ureg_src_register(TGSI_FILE_IMMEDIATE, 0);
763   }
764
765   index = ureg->nr_immediates;
766   ureg->nr_immediates += (nr + 3) / 4;
767
768   for (i = index; i < ureg->nr_immediates; i++) {
769      ureg->immediate[i].type = TGSI_IMM_UINT32;
770      ureg->immediate[i].nr = nr > 4 ? 4 : nr;
771      memcpy(ureg->immediate[i].value.u,
772             &v[(i - index) * 4],
773             ureg->immediate[i].nr * sizeof(uint));
774      nr -= 4;
775   }
776
777   return ureg_src_register(TGSI_FILE_IMMEDIATE, index);
778}
779
780
781struct ureg_src
782ureg_DECL_immediate_int( struct ureg_program *ureg,
783                         const int *v,
784                         unsigned nr )
785{
786   return decl_immediate(ureg, (const unsigned *)v, nr, TGSI_IMM_INT32);
787}
788
789
790void
791ureg_emit_src( struct ureg_program *ureg,
792               struct ureg_src src )
793{
794   unsigned size = 1 + (src.Indirect ? 1 : 0) +
795                   (src.Dimension ? (src.DimIndirect ? 2 : 1) : 0);
796
797   union tgsi_any_token *out = get_tokens( ureg, DOMAIN_INSN, size );
798   unsigned n = 0;
799
800   assert(src.File != TGSI_FILE_NULL);
801   assert(src.File != TGSI_FILE_OUTPUT);
802   assert(src.File < TGSI_FILE_COUNT);
803
804   out[n].value = 0;
805   out[n].src.File = src.File;
806   out[n].src.SwizzleX = src.SwizzleX;
807   out[n].src.SwizzleY = src.SwizzleY;
808   out[n].src.SwizzleZ = src.SwizzleZ;
809   out[n].src.SwizzleW = src.SwizzleW;
810   out[n].src.Index = src.Index;
811   out[n].src.Negate = src.Negate;
812   out[0].src.Absolute = src.Absolute;
813   n++;
814
815   if (src.Indirect) {
816      out[0].src.Indirect = 1;
817      out[n].value = 0;
818      out[n].src.File = src.IndirectFile;
819      out[n].src.SwizzleX = src.IndirectSwizzle;
820      out[n].src.SwizzleY = src.IndirectSwizzle;
821      out[n].src.SwizzleZ = src.IndirectSwizzle;
822      out[n].src.SwizzleW = src.IndirectSwizzle;
823      out[n].src.Index = src.IndirectIndex;
824      n++;
825   }
826
827   if (src.Dimension) {
828      if (src.DimIndirect) {
829         out[0].src.Dimension = 1;
830         out[n].dim.Indirect = 1;
831         out[n].dim.Dimension = 0;
832         out[n].dim.Padding = 0;
833         out[n].dim.Index = src.DimensionIndex;
834         n++;
835         out[n].value = 0;
836         out[n].src.File = src.DimIndFile;
837         out[n].src.SwizzleX = src.DimIndSwizzle;
838         out[n].src.SwizzleY = src.DimIndSwizzle;
839         out[n].src.SwizzleZ = src.DimIndSwizzle;
840         out[n].src.SwizzleW = src.DimIndSwizzle;
841         out[n].src.Index = src.DimIndIndex;
842      } else {
843         out[0].src.Dimension = 1;
844         out[n].dim.Indirect = 0;
845         out[n].dim.Dimension = 0;
846         out[n].dim.Padding = 0;
847         out[n].dim.Index = src.DimensionIndex;
848      }
849      n++;
850   }
851
852   assert(n == size);
853}
854
855
856void
857ureg_emit_dst( struct ureg_program *ureg,
858               struct ureg_dst dst )
859{
860   unsigned size = (1 +
861                    (dst.Indirect ? 1 : 0));
862
863   union tgsi_any_token *out = get_tokens( ureg, DOMAIN_INSN, size );
864   unsigned n = 0;
865
866   assert(dst.File != TGSI_FILE_NULL);
867   assert(dst.File != TGSI_FILE_CONSTANT);
868   assert(dst.File != TGSI_FILE_INPUT);
869   assert(dst.File != TGSI_FILE_SAMPLER);
870   assert(dst.File != TGSI_FILE_RESOURCE);
871   assert(dst.File != TGSI_FILE_IMMEDIATE);
872   assert(dst.File < TGSI_FILE_COUNT);
873
874   out[n].value = 0;
875   out[n].dst.File = dst.File;
876   out[n].dst.WriteMask = dst.WriteMask;
877   out[n].dst.Indirect = dst.Indirect;
878   out[n].dst.Index = dst.Index;
879   n++;
880
881   if (dst.Indirect) {
882      out[n].value = 0;
883      out[n].src.File = TGSI_FILE_ADDRESS;
884      out[n].src.SwizzleX = dst.IndirectSwizzle;
885      out[n].src.SwizzleY = dst.IndirectSwizzle;
886      out[n].src.SwizzleZ = dst.IndirectSwizzle;
887      out[n].src.SwizzleW = dst.IndirectSwizzle;
888      out[n].src.Index = dst.IndirectIndex;
889      n++;
890   }
891
892   assert(n == size);
893}
894
895
896static void validate( unsigned opcode,
897                      unsigned nr_dst,
898                      unsigned nr_src )
899{
900#ifdef DEBUG
901   const struct tgsi_opcode_info *info = tgsi_get_opcode_info( opcode );
902   assert(info);
903   if(info) {
904      assert(nr_dst == info->num_dst);
905      assert(nr_src == info->num_src);
906   }
907#endif
908}
909
910struct ureg_emit_insn_result
911ureg_emit_insn(struct ureg_program *ureg,
912               unsigned opcode,
913               boolean saturate,
914               boolean predicate,
915               boolean pred_negate,
916               unsigned pred_swizzle_x,
917               unsigned pred_swizzle_y,
918               unsigned pred_swizzle_z,
919               unsigned pred_swizzle_w,
920               unsigned num_dst,
921               unsigned num_src )
922{
923   union tgsi_any_token *out;
924   uint count = predicate ? 2 : 1;
925   struct ureg_emit_insn_result result;
926
927   validate( opcode, num_dst, num_src );
928
929   out = get_tokens( ureg, DOMAIN_INSN, count );
930   out[0].insn = tgsi_default_instruction();
931   out[0].insn.Opcode = opcode;
932   out[0].insn.Saturate = saturate;
933   out[0].insn.NumDstRegs = num_dst;
934   out[0].insn.NumSrcRegs = num_src;
935
936   result.insn_token = ureg->domain[DOMAIN_INSN].count - count;
937   result.extended_token = result.insn_token;
938
939   if (predicate) {
940      out[0].insn.Predicate = 1;
941      out[1].insn_predicate = tgsi_default_instruction_predicate();
942      out[1].insn_predicate.Negate = pred_negate;
943      out[1].insn_predicate.SwizzleX = pred_swizzle_x;
944      out[1].insn_predicate.SwizzleY = pred_swizzle_y;
945      out[1].insn_predicate.SwizzleZ = pred_swizzle_z;
946      out[1].insn_predicate.SwizzleW = pred_swizzle_w;
947   }
948
949   ureg->nr_instructions++;
950
951   return result;
952}
953
954
955void
956ureg_emit_label(struct ureg_program *ureg,
957                unsigned extended_token,
958                unsigned *label_token )
959{
960   union tgsi_any_token *out, *insn;
961
962   if(!label_token)
963      return;
964
965   out = get_tokens( ureg, DOMAIN_INSN, 1 );
966   out[0].value = 0;
967
968   insn = retrieve_token( ureg, DOMAIN_INSN, extended_token );
969   insn->insn.Label = 1;
970
971   *label_token = ureg->domain[DOMAIN_INSN].count - 1;
972}
973
974/* Will return a number which can be used in a label to point to the
975 * next instruction to be emitted.
976 */
977unsigned
978ureg_get_instruction_number( struct ureg_program *ureg )
979{
980   return ureg->nr_instructions;
981}
982
983/* Patch a given label (expressed as a token number) to point to a
984 * given instruction (expressed as an instruction number).
985 */
986void
987ureg_fixup_label(struct ureg_program *ureg,
988                 unsigned label_token,
989                 unsigned instruction_number )
990{
991   union tgsi_any_token *out = retrieve_token( ureg, DOMAIN_INSN, label_token );
992
993   out->insn_label.Label = instruction_number;
994}
995
996
997void
998ureg_emit_texture(struct ureg_program *ureg,
999                  unsigned extended_token,
1000                  unsigned target )
1001{
1002   union tgsi_any_token *out, *insn;
1003
1004   out = get_tokens( ureg, DOMAIN_INSN, 1 );
1005   insn = retrieve_token( ureg, DOMAIN_INSN, extended_token );
1006
1007   insn->insn.Texture = 1;
1008
1009   out[0].value = 0;
1010   out[0].insn_texture.Texture = target;
1011}
1012
1013
1014void
1015ureg_fixup_insn_size(struct ureg_program *ureg,
1016                     unsigned insn )
1017{
1018   union tgsi_any_token *out = retrieve_token( ureg, DOMAIN_INSN, insn );
1019
1020   assert(out->insn.Type == TGSI_TOKEN_TYPE_INSTRUCTION);
1021   out->insn.NrTokens = ureg->domain[DOMAIN_INSN].count - insn - 1;
1022}
1023
1024
1025void
1026ureg_insn(struct ureg_program *ureg,
1027          unsigned opcode,
1028          const struct ureg_dst *dst,
1029          unsigned nr_dst,
1030          const struct ureg_src *src,
1031          unsigned nr_src )
1032{
1033   struct ureg_emit_insn_result insn;
1034   unsigned i;
1035   boolean saturate;
1036   boolean predicate;
1037   boolean negate = FALSE;
1038   unsigned swizzle[4] = { 0 };
1039
1040   saturate = nr_dst ? dst[0].Saturate : FALSE;
1041   predicate = nr_dst ? dst[0].Predicate : FALSE;
1042   if (predicate) {
1043      negate = dst[0].PredNegate;
1044      swizzle[0] = dst[0].PredSwizzleX;
1045      swizzle[1] = dst[0].PredSwizzleY;
1046      swizzle[2] = dst[0].PredSwizzleZ;
1047      swizzle[3] = dst[0].PredSwizzleW;
1048   }
1049
1050   insn = ureg_emit_insn(ureg,
1051                         opcode,
1052                         saturate,
1053                         predicate,
1054                         negate,
1055                         swizzle[0],
1056                         swizzle[1],
1057                         swizzle[2],
1058                         swizzle[3],
1059                         nr_dst,
1060                         nr_src);
1061
1062   for (i = 0; i < nr_dst; i++)
1063      ureg_emit_dst( ureg, dst[i] );
1064
1065   for (i = 0; i < nr_src; i++)
1066      ureg_emit_src( ureg, src[i] );
1067
1068   ureg_fixup_insn_size( ureg, insn.insn_token );
1069}
1070
1071void
1072ureg_tex_insn(struct ureg_program *ureg,
1073              unsigned opcode,
1074              const struct ureg_dst *dst,
1075              unsigned nr_dst,
1076              unsigned target,
1077              const struct ureg_src *src,
1078              unsigned nr_src )
1079{
1080   struct ureg_emit_insn_result insn;
1081   unsigned i;
1082   boolean saturate;
1083   boolean predicate;
1084   boolean negate = FALSE;
1085   unsigned swizzle[4] = { 0 };
1086
1087   saturate = nr_dst ? dst[0].Saturate : FALSE;
1088   predicate = nr_dst ? dst[0].Predicate : FALSE;
1089   if (predicate) {
1090      negate = dst[0].PredNegate;
1091      swizzle[0] = dst[0].PredSwizzleX;
1092      swizzle[1] = dst[0].PredSwizzleY;
1093      swizzle[2] = dst[0].PredSwizzleZ;
1094      swizzle[3] = dst[0].PredSwizzleW;
1095   }
1096
1097   insn = ureg_emit_insn(ureg,
1098                         opcode,
1099                         saturate,
1100                         predicate,
1101                         negate,
1102                         swizzle[0],
1103                         swizzle[1],
1104                         swizzle[2],
1105                         swizzle[3],
1106                         nr_dst,
1107                         nr_src);
1108
1109   ureg_emit_texture( ureg, insn.extended_token, target );
1110
1111   for (i = 0; i < nr_dst; i++)
1112      ureg_emit_dst( ureg, dst[i] );
1113
1114   for (i = 0; i < nr_src; i++)
1115      ureg_emit_src( ureg, src[i] );
1116
1117   ureg_fixup_insn_size( ureg, insn.insn_token );
1118}
1119
1120
1121void
1122ureg_label_insn(struct ureg_program *ureg,
1123                unsigned opcode,
1124                const struct ureg_src *src,
1125                unsigned nr_src,
1126                unsigned *label_token )
1127{
1128   struct ureg_emit_insn_result insn;
1129   unsigned i;
1130
1131   insn = ureg_emit_insn(ureg,
1132                         opcode,
1133                         FALSE,
1134                         FALSE,
1135                         FALSE,
1136                         TGSI_SWIZZLE_X,
1137                         TGSI_SWIZZLE_Y,
1138                         TGSI_SWIZZLE_Z,
1139                         TGSI_SWIZZLE_W,
1140                         0,
1141                         nr_src);
1142
1143   ureg_emit_label( ureg, insn.extended_token, label_token );
1144
1145   for (i = 0; i < nr_src; i++)
1146      ureg_emit_src( ureg, src[i] );
1147
1148   ureg_fixup_insn_size( ureg, insn.insn_token );
1149}
1150
1151
1152static void
1153emit_decl_semantic(struct ureg_program *ureg,
1154                   unsigned file,
1155                   unsigned index,
1156                   unsigned semantic_name,
1157                   unsigned semantic_index)
1158{
1159   union tgsi_any_token *out = get_tokens(ureg, DOMAIN_DECL, 3);
1160
1161   out[0].value = 0;
1162   out[0].decl.Type = TGSI_TOKEN_TYPE_DECLARATION;
1163   out[0].decl.NrTokens = 3;
1164   out[0].decl.File = file;
1165   out[0].decl.UsageMask = TGSI_WRITEMASK_XYZW; /* FIXME! */
1166   out[0].decl.Semantic = 1;
1167
1168   out[1].value = 0;
1169   out[1].decl_range.First = index;
1170   out[1].decl_range.Last = index;
1171
1172   out[2].value = 0;
1173   out[2].decl_semantic.Name = semantic_name;
1174   out[2].decl_semantic.Index = semantic_index;
1175}
1176
1177
1178static void
1179emit_decl_fs(struct ureg_program *ureg,
1180             unsigned file,
1181             unsigned index,
1182             unsigned semantic_name,
1183             unsigned semantic_index,
1184             unsigned interpolate,
1185             unsigned cylindrical_wrap,
1186             unsigned centroid)
1187{
1188   union tgsi_any_token *out = get_tokens(ureg, DOMAIN_DECL, 3);
1189
1190   out[0].value = 0;
1191   out[0].decl.Type = TGSI_TOKEN_TYPE_DECLARATION;
1192   out[0].decl.NrTokens = 3;
1193   out[0].decl.File = file;
1194   out[0].decl.UsageMask = TGSI_WRITEMASK_XYZW; /* FIXME! */
1195   out[0].decl.Interpolate = interpolate;
1196   out[0].decl.Semantic = 1;
1197   out[0].decl.CylindricalWrap = cylindrical_wrap;
1198   out[0].decl.Centroid = centroid;
1199
1200   out[1].value = 0;
1201   out[1].decl_range.First = index;
1202   out[1].decl_range.Last = index;
1203
1204   out[2].value = 0;
1205   out[2].decl_semantic.Name = semantic_name;
1206   out[2].decl_semantic.Index = semantic_index;
1207}
1208
1209
1210static void emit_decl_range( struct ureg_program *ureg,
1211                             unsigned file,
1212                             unsigned first,
1213                             unsigned count )
1214{
1215   union tgsi_any_token *out = get_tokens( ureg, DOMAIN_DECL, 2 );
1216
1217   out[0].value = 0;
1218   out[0].decl.Type = TGSI_TOKEN_TYPE_DECLARATION;
1219   out[0].decl.NrTokens = 2;
1220   out[0].decl.File = file;
1221   out[0].decl.UsageMask = TGSI_WRITEMASK_XYZW;
1222   out[0].decl.Interpolate = TGSI_INTERPOLATE_CONSTANT;
1223   out[0].decl.Semantic = 0;
1224
1225   out[1].value = 0;
1226   out[1].decl_range.First = first;
1227   out[1].decl_range.Last = first + count - 1;
1228}
1229
1230static void
1231emit_decl_range2D(struct ureg_program *ureg,
1232                  unsigned file,
1233                  unsigned first,
1234                  unsigned last,
1235                  unsigned index2D)
1236{
1237   union tgsi_any_token *out = get_tokens(ureg, DOMAIN_DECL, 3);
1238
1239   out[0].value = 0;
1240   out[0].decl.Type = TGSI_TOKEN_TYPE_DECLARATION;
1241   out[0].decl.NrTokens = 3;
1242   out[0].decl.File = file;
1243   out[0].decl.UsageMask = TGSI_WRITEMASK_XYZW;
1244   out[0].decl.Interpolate = TGSI_INTERPOLATE_CONSTANT;
1245   out[0].decl.Dimension = 1;
1246
1247   out[1].value = 0;
1248   out[1].decl_range.First = first;
1249   out[1].decl_range.Last = last;
1250
1251   out[2].value = 0;
1252   out[2].decl_dim.Index2D = index2D;
1253}
1254
1255static void
1256emit_decl_resource(struct ureg_program *ureg,
1257                   unsigned index,
1258                   unsigned target,
1259                   unsigned return_type_x,
1260                   unsigned return_type_y,
1261                   unsigned return_type_z,
1262                   unsigned return_type_w )
1263{
1264   union tgsi_any_token *out = get_tokens(ureg, DOMAIN_DECL, 3);
1265
1266   out[0].value = 0;
1267   out[0].decl.Type = TGSI_TOKEN_TYPE_DECLARATION;
1268   out[0].decl.NrTokens = 3;
1269   out[0].decl.File = TGSI_FILE_RESOURCE;
1270   out[0].decl.UsageMask = 0xf;
1271   out[0].decl.Interpolate = TGSI_INTERPOLATE_CONSTANT;
1272
1273   out[1].value = 0;
1274   out[1].decl_range.First = index;
1275   out[1].decl_range.Last = index;
1276
1277   out[2].value = 0;
1278   out[2].decl_resource.Resource    = target;
1279   out[2].decl_resource.ReturnTypeX = return_type_x;
1280   out[2].decl_resource.ReturnTypeY = return_type_y;
1281   out[2].decl_resource.ReturnTypeZ = return_type_z;
1282   out[2].decl_resource.ReturnTypeW = return_type_w;
1283}
1284
1285static void
1286emit_immediate( struct ureg_program *ureg,
1287                const unsigned *v,
1288                unsigned type )
1289{
1290   union tgsi_any_token *out = get_tokens( ureg, DOMAIN_DECL, 5 );
1291
1292   out[0].value = 0;
1293   out[0].imm.Type = TGSI_TOKEN_TYPE_IMMEDIATE;
1294   out[0].imm.NrTokens = 5;
1295   out[0].imm.DataType = type;
1296   out[0].imm.Padding = 0;
1297
1298   out[1].imm_data.Uint = v[0];
1299   out[2].imm_data.Uint = v[1];
1300   out[3].imm_data.Uint = v[2];
1301   out[4].imm_data.Uint = v[3];
1302}
1303
1304static void
1305emit_property(struct ureg_program *ureg,
1306              unsigned name,
1307              unsigned data)
1308{
1309   union tgsi_any_token *out = get_tokens(ureg, DOMAIN_DECL, 2);
1310
1311   out[0].value = 0;
1312   out[0].prop.Type = TGSI_TOKEN_TYPE_PROPERTY;
1313   out[0].prop.NrTokens = 2;
1314   out[0].prop.PropertyName = name;
1315
1316   out[1].prop_data.Data = data;
1317}
1318
1319
1320static void emit_decls( struct ureg_program *ureg )
1321{
1322   unsigned i;
1323
1324   if (ureg->property_gs_input_prim != ~0) {
1325      assert(ureg->processor == TGSI_PROCESSOR_GEOMETRY);
1326
1327      emit_property(ureg,
1328                    TGSI_PROPERTY_GS_INPUT_PRIM,
1329                    ureg->property_gs_input_prim);
1330   }
1331
1332   if (ureg->property_gs_output_prim != ~0) {
1333      assert(ureg->processor == TGSI_PROCESSOR_GEOMETRY);
1334
1335      emit_property(ureg,
1336                    TGSI_PROPERTY_GS_OUTPUT_PRIM,
1337                    ureg->property_gs_output_prim);
1338   }
1339
1340   if (ureg->property_gs_max_vertices != ~0) {
1341      assert(ureg->processor == TGSI_PROCESSOR_GEOMETRY);
1342
1343      emit_property(ureg,
1344                    TGSI_PROPERTY_GS_MAX_OUTPUT_VERTICES,
1345                    ureg->property_gs_max_vertices);
1346   }
1347
1348   if (ureg->property_fs_coord_origin) {
1349      assert(ureg->processor == TGSI_PROCESSOR_FRAGMENT);
1350
1351      emit_property(ureg,
1352                    TGSI_PROPERTY_FS_COORD_ORIGIN,
1353                    ureg->property_fs_coord_origin);
1354   }
1355
1356   if (ureg->property_fs_coord_pixel_center) {
1357      assert(ureg->processor == TGSI_PROCESSOR_FRAGMENT);
1358
1359      emit_property(ureg,
1360                    TGSI_PROPERTY_FS_COORD_PIXEL_CENTER,
1361                    ureg->property_fs_coord_pixel_center);
1362   }
1363
1364   if (ureg->property_fs_color0_writes_all_cbufs) {
1365      assert(ureg->processor == TGSI_PROCESSOR_FRAGMENT);
1366
1367      emit_property(ureg,
1368                    TGSI_PROPERTY_FS_COLOR0_WRITES_ALL_CBUFS,
1369                    ureg->property_fs_color0_writes_all_cbufs);
1370   }
1371
1372   if (ureg->processor == TGSI_PROCESSOR_VERTEX) {
1373      for (i = 0; i < UREG_MAX_INPUT; i++) {
1374         if (ureg->vs_inputs[i/32] & (1 << (i%32))) {
1375            emit_decl_range( ureg, TGSI_FILE_INPUT, i, 1 );
1376         }
1377      }
1378   } else if (ureg->processor == TGSI_PROCESSOR_FRAGMENT) {
1379      for (i = 0; i < ureg->nr_fs_inputs; i++) {
1380         emit_decl_fs(ureg,
1381                      TGSI_FILE_INPUT,
1382                      i,
1383                      ureg->fs_input[i].semantic_name,
1384                      ureg->fs_input[i].semantic_index,
1385                      ureg->fs_input[i].interp,
1386                      ureg->fs_input[i].cylindrical_wrap,
1387                      ureg->fs_input[i].centroid);
1388      }
1389   } else {
1390      for (i = 0; i < ureg->nr_gs_inputs; i++) {
1391         emit_decl_semantic(ureg,
1392                            TGSI_FILE_INPUT,
1393                            ureg->gs_input[i].index,
1394                            ureg->gs_input[i].semantic_name,
1395                            ureg->gs_input[i].semantic_index);
1396      }
1397   }
1398
1399   for (i = 0; i < ureg->nr_system_values; i++) {
1400      emit_decl_semantic(ureg,
1401                         TGSI_FILE_SYSTEM_VALUE,
1402                         ureg->system_value[i].index,
1403                         ureg->system_value[i].semantic_name,
1404                         ureg->system_value[i].semantic_index);
1405   }
1406
1407   for (i = 0; i < ureg->nr_outputs; i++) {
1408      emit_decl_semantic(ureg,
1409                         TGSI_FILE_OUTPUT,
1410                         i,
1411                         ureg->output[i].semantic_name,
1412                         ureg->output[i].semantic_index);
1413   }
1414
1415   for (i = 0; i < ureg->nr_samplers; i++) {
1416      emit_decl_range( ureg,
1417                       TGSI_FILE_SAMPLER,
1418                       ureg->sampler[i].Index, 1 );
1419   }
1420
1421   for (i = 0; i < ureg->nr_resources; i++) {
1422      emit_decl_resource(ureg,
1423                         ureg->resource[i].index,
1424                         ureg->resource[i].target,
1425                         ureg->resource[i].return_type_x,
1426                         ureg->resource[i].return_type_y,
1427                         ureg->resource[i].return_type_z,
1428                         ureg->resource[i].return_type_w);
1429   }
1430
1431   if (ureg->const_decls.nr_constant_ranges) {
1432      for (i = 0; i < ureg->const_decls.nr_constant_ranges; i++) {
1433         emit_decl_range(ureg,
1434                         TGSI_FILE_CONSTANT,
1435                         ureg->const_decls.constant_range[i].first,
1436                         ureg->const_decls.constant_range[i].last - ureg->const_decls.constant_range[i].first + 1);
1437      }
1438   }
1439
1440   for (i = 0; i < PIPE_MAX_CONSTANT_BUFFERS; i++) {
1441      struct const_decl *decl = &ureg->const_decls2D[i];
1442
1443      if (decl->nr_constant_ranges) {
1444         uint j;
1445
1446         for (j = 0; j < decl->nr_constant_ranges; j++) {
1447            emit_decl_range2D(ureg,
1448                              TGSI_FILE_CONSTANT,
1449                              decl->constant_range[j].first,
1450                              decl->constant_range[j].last,
1451                              i);
1452         }
1453      }
1454   }
1455
1456   if (ureg->nr_temps) {
1457      emit_decl_range( ureg,
1458                       TGSI_FILE_TEMPORARY,
1459                       0, ureg->nr_temps );
1460   }
1461
1462   if (ureg->nr_addrs) {
1463      emit_decl_range( ureg,
1464                       TGSI_FILE_ADDRESS,
1465                       0, ureg->nr_addrs );
1466   }
1467
1468   if (ureg->nr_preds) {
1469      emit_decl_range(ureg,
1470                      TGSI_FILE_PREDICATE,
1471                      0,
1472                      ureg->nr_preds);
1473   }
1474
1475   for (i = 0; i < ureg->nr_immediates; i++) {
1476      emit_immediate( ureg,
1477                      ureg->immediate[i].value.u,
1478                      ureg->immediate[i].type );
1479   }
1480}
1481
1482/* Append the instruction tokens onto the declarations to build a
1483 * contiguous stream suitable to send to the driver.
1484 */
1485static void copy_instructions( struct ureg_program *ureg )
1486{
1487   unsigned nr_tokens = ureg->domain[DOMAIN_INSN].count;
1488   union tgsi_any_token *out = get_tokens( ureg,
1489                                           DOMAIN_DECL,
1490                                           nr_tokens );
1491
1492   memcpy(out,
1493          ureg->domain[DOMAIN_INSN].tokens,
1494          nr_tokens * sizeof out[0] );
1495}
1496
1497
1498static void
1499fixup_header_size(struct ureg_program *ureg)
1500{
1501   union tgsi_any_token *out = retrieve_token( ureg, DOMAIN_DECL, 0 );
1502
1503   out->header.BodySize = ureg->domain[DOMAIN_DECL].count - 2;
1504}
1505
1506
1507static void
1508emit_header( struct ureg_program *ureg )
1509{
1510   union tgsi_any_token *out = get_tokens( ureg, DOMAIN_DECL, 2 );
1511
1512   out[0].header.HeaderSize = 2;
1513   out[0].header.BodySize = 0;
1514
1515   out[1].processor.Processor = ureg->processor;
1516   out[1].processor.Padding = 0;
1517}
1518
1519
1520const struct tgsi_token *ureg_finalize( struct ureg_program *ureg )
1521{
1522   const struct tgsi_token *tokens;
1523
1524   emit_header( ureg );
1525   emit_decls( ureg );
1526   copy_instructions( ureg );
1527   fixup_header_size( ureg );
1528
1529   if (ureg->domain[0].tokens == error_tokens ||
1530       ureg->domain[1].tokens == error_tokens) {
1531      debug_printf("%s: error in generated shader\n", __FUNCTION__);
1532      assert(0);
1533      return NULL;
1534   }
1535
1536   tokens = &ureg->domain[DOMAIN_DECL].tokens[0].token;
1537
1538   if (0) {
1539      debug_printf("%s: emitted shader %d tokens:\n", __FUNCTION__,
1540                   ureg->domain[DOMAIN_DECL].count);
1541      tgsi_dump( tokens, 0 );
1542   }
1543
1544#if DEBUG
1545   if (tokens && !tgsi_sanity_check(tokens)) {
1546      debug_printf("tgsi_ureg.c, sanity check failed on generated tokens:\n");
1547      tgsi_dump(tokens, 0);
1548      assert(0);
1549   }
1550#endif
1551
1552
1553   return tokens;
1554}
1555
1556
1557void *ureg_create_shader( struct ureg_program *ureg,
1558                          struct pipe_context *pipe )
1559{
1560   struct pipe_shader_state state;
1561
1562   state.tokens = ureg_finalize(ureg);
1563   if(!state.tokens)
1564      return NULL;
1565
1566   if (ureg->processor == TGSI_PROCESSOR_VERTEX)
1567      return pipe->create_vs_state( pipe, &state );
1568   else
1569      return pipe->create_fs_state( pipe, &state );
1570}
1571
1572
1573const struct tgsi_token *ureg_get_tokens( struct ureg_program *ureg,
1574                                          unsigned *nr_tokens )
1575{
1576   const struct tgsi_token *tokens;
1577
1578   ureg_finalize(ureg);
1579
1580   tokens = &ureg->domain[DOMAIN_DECL].tokens[0].token;
1581
1582   if (nr_tokens)
1583      *nr_tokens = ureg->domain[DOMAIN_DECL].size;
1584
1585   ureg->domain[DOMAIN_DECL].tokens = 0;
1586   ureg->domain[DOMAIN_DECL].size = 0;
1587   ureg->domain[DOMAIN_DECL].order = 0;
1588   ureg->domain[DOMAIN_DECL].count = 0;
1589
1590   return tokens;
1591}
1592
1593
1594void ureg_free_tokens( const struct tgsi_token *tokens )
1595{
1596   FREE((struct tgsi_token *)tokens);
1597}
1598
1599
1600struct ureg_program *ureg_create( unsigned processor )
1601{
1602   struct ureg_program *ureg = CALLOC_STRUCT( ureg_program );
1603   if (ureg == NULL)
1604      return NULL;
1605
1606   ureg->processor = processor;
1607   ureg->property_gs_input_prim = ~0;
1608   ureg->property_gs_output_prim = ~0;
1609   ureg->property_gs_max_vertices = ~0;
1610   return ureg;
1611}
1612
1613
1614void ureg_destroy( struct ureg_program *ureg )
1615{
1616   unsigned i;
1617
1618   for (i = 0; i < Elements(ureg->domain); i++) {
1619      if (ureg->domain[i].tokens &&
1620          ureg->domain[i].tokens != error_tokens)
1621         FREE(ureg->domain[i].tokens);
1622   }
1623
1624   FREE(ureg);
1625}
1626