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