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