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