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