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