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