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