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