1 /**************************************************************************
3 * Copyright 2009-2010 VMware, Inc.
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
26 **************************************************************************/
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"
43 union tgsi_any_token
{
44 struct tgsi_header header
;
45 struct tgsi_processor processor
;
46 struct tgsi_token token
;
47 struct tgsi_property prop
;
48 struct tgsi_property_data prop_data
;
49 struct tgsi_declaration decl
;
50 struct tgsi_declaration_range decl_range
;
51 struct tgsi_declaration_dimension decl_dim
;
52 struct tgsi_declaration_interp decl_interp
;
53 struct tgsi_declaration_image decl_image
;
54 struct tgsi_declaration_semantic decl_semantic
;
55 struct tgsi_declaration_sampler_view decl_sampler_view
;
56 struct tgsi_declaration_array array
;
57 struct tgsi_immediate imm
;
58 union tgsi_immediate_data imm_data
;
59 struct tgsi_instruction insn
;
60 struct tgsi_instruction_label insn_label
;
61 struct tgsi_instruction_texture insn_texture
;
62 struct tgsi_instruction_memory insn_memory
;
63 struct tgsi_texture_offset insn_texture_offset
;
64 struct tgsi_src_register src
;
65 struct tgsi_ind_register ind
;
66 struct tgsi_dimension dim
;
67 struct tgsi_dst_register dst
;
73 union tgsi_any_token
*tokens
;
79 #define UREG_MAX_INPUT (4 * PIPE_MAX_SHADER_INPUTS)
80 #define UREG_MAX_SYSTEM_VALUE PIPE_MAX_ATTRIBS
81 #define UREG_MAX_OUTPUT (4 * PIPE_MAX_SHADER_OUTPUTS)
82 #define UREG_MAX_CONSTANT_RANGE 32
83 #define UREG_MAX_IMMEDIATE 4096
84 #define UREG_MAX_ADDR 3
85 #define UREG_MAX_ARRAY_TEMPS 256
91 } constant_range
[UREG_MAX_CONSTANT_RANGE
];
92 unsigned nr_constant_ranges
;
101 bool supports_any_inout_decl_range
;
102 int next_shader_processor
;
105 unsigned semantic_name
;
106 unsigned semantic_index
;
108 unsigned char cylindrical_wrap
;
109 unsigned char usage_mask
;
110 unsigned interp_location
;
114 } input
[UREG_MAX_INPUT
];
115 unsigned nr_inputs
, nr_input_regs
;
117 unsigned vs_inputs
[PIPE_MAX_ATTRIBS
/32];
120 unsigned semantic_name
;
121 unsigned semantic_index
;
122 } system_value
[UREG_MAX_SYSTEM_VALUE
];
123 unsigned nr_system_values
;
126 unsigned semantic_name
;
127 unsigned semantic_index
;
129 unsigned usage_mask
; /* = TGSI_WRITEMASK_* */
133 } output
[UREG_MAX_OUTPUT
];
134 unsigned nr_outputs
, nr_output_regs
;
144 } immediate
[UREG_MAX_IMMEDIATE
];
145 unsigned nr_immediates
;
147 struct ureg_src sampler
[PIPE_MAX_SAMPLERS
];
148 unsigned nr_samplers
;
153 unsigned return_type_x
;
154 unsigned return_type_y
;
155 unsigned return_type_z
;
156 unsigned return_type_w
;
157 } sampler_view
[PIPE_MAX_SHADER_SAMPLER_VIEWS
];
158 unsigned nr_sampler_views
;
166 } image
[PIPE_MAX_SHADER_IMAGES
];
172 } buffer
[PIPE_MAX_SHADER_BUFFERS
];
175 struct util_bitmask
*free_temps
;
176 struct util_bitmask
*local_temps
;
177 struct util_bitmask
*decl_temps
;
180 unsigned array_temps
[UREG_MAX_ARRAY_TEMPS
];
181 unsigned nr_array_temps
;
183 struct const_decl const_decls
;
184 struct const_decl const_decls2D
[PIPE_MAX_CONSTANT_BUFFERS
];
186 unsigned properties
[TGSI_PROPERTY_COUNT
];
189 unsigned nr_instructions
;
191 struct ureg_tokens domain
[2];
193 bool use_memory
[TGSI_MEMORY_TYPE_COUNT
];
196 static union tgsi_any_token error_tokens
[32];
198 static void tokens_error( struct ureg_tokens
*tokens
)
200 if (tokens
->tokens
&& tokens
->tokens
!= error_tokens
)
201 FREE(tokens
->tokens
);
203 tokens
->tokens
= error_tokens
;
204 tokens
->size
= ARRAY_SIZE(error_tokens
);
209 static void tokens_expand( struct ureg_tokens
*tokens
,
212 unsigned old_size
= tokens
->size
* sizeof(unsigned);
214 if (tokens
->tokens
== error_tokens
) {
218 while (tokens
->count
+ count
> tokens
->size
) {
219 tokens
->size
= (1 << ++tokens
->order
);
222 tokens
->tokens
= REALLOC(tokens
->tokens
,
224 tokens
->size
* sizeof(unsigned));
225 if (tokens
->tokens
== NULL
) {
226 tokens_error(tokens
);
230 static void set_bad( struct ureg_program
*ureg
)
232 tokens_error(&ureg
->domain
[0]);
237 static union tgsi_any_token
*get_tokens( struct ureg_program
*ureg
,
241 struct ureg_tokens
*tokens
= &ureg
->domain
[domain
];
242 union tgsi_any_token
*result
;
244 if (tokens
->count
+ count
> tokens
->size
)
245 tokens_expand(tokens
, count
);
247 result
= &tokens
->tokens
[tokens
->count
];
248 tokens
->count
+= count
;
253 static union tgsi_any_token
*retrieve_token( struct ureg_program
*ureg
,
257 if (ureg
->domain
[domain
].tokens
== error_tokens
)
258 return &error_tokens
[0];
260 return &ureg
->domain
[domain
].tokens
[nr
];
265 ureg_property(struct ureg_program
*ureg
, unsigned name
, unsigned value
)
267 assert(name
< ARRAY_SIZE(ureg
->properties
));
268 ureg
->properties
[name
] = value
;
272 ureg_DECL_fs_input_cyl_centroid_layout(struct ureg_program
*ureg
,
273 unsigned semantic_name
,
274 unsigned semantic_index
,
275 unsigned interp_mode
,
276 unsigned cylindrical_wrap
,
277 unsigned interp_location
,
285 assert(usage_mask
!= 0);
286 assert(usage_mask
<= TGSI_WRITEMASK_XYZW
);
288 for (i
= 0; i
< ureg
->nr_inputs
; i
++) {
289 if (ureg
->input
[i
].semantic_name
== semantic_name
&&
290 ureg
->input
[i
].semantic_index
== semantic_index
) {
291 assert(ureg
->input
[i
].interp
== interp_mode
);
292 assert(ureg
->input
[i
].cylindrical_wrap
== cylindrical_wrap
);
293 assert(ureg
->input
[i
].interp_location
== interp_location
);
294 if (ureg
->input
[i
].array_id
== array_id
) {
295 ureg
->input
[i
].usage_mask
|= usage_mask
;
298 assert((ureg
->input
[i
].usage_mask
& usage_mask
) == 0);
302 if (ureg
->nr_inputs
< UREG_MAX_INPUT
) {
303 assert(array_size
>= 1);
304 ureg
->input
[i
].semantic_name
= semantic_name
;
305 ureg
->input
[i
].semantic_index
= semantic_index
;
306 ureg
->input
[i
].interp
= interp_mode
;
307 ureg
->input
[i
].cylindrical_wrap
= cylindrical_wrap
;
308 ureg
->input
[i
].interp_location
= interp_location
;
309 ureg
->input
[i
].first
= index
;
310 ureg
->input
[i
].last
= index
+ array_size
- 1;
311 ureg
->input
[i
].array_id
= array_id
;
312 ureg
->input
[i
].usage_mask
= usage_mask
;
313 ureg
->nr_input_regs
= MAX2(ureg
->nr_input_regs
, index
+ array_size
);
320 return ureg_src_array_register(TGSI_FILE_INPUT
, ureg
->input
[i
].first
,
325 ureg_DECL_fs_input_cyl_centroid(struct ureg_program
*ureg
,
326 unsigned semantic_name
,
327 unsigned semantic_index
,
328 unsigned interp_mode
,
329 unsigned cylindrical_wrap
,
330 unsigned interp_location
,
334 return ureg_DECL_fs_input_cyl_centroid_layout(ureg
,
335 semantic_name
, semantic_index
, interp_mode
, cylindrical_wrap
, interp_location
,
336 ureg
->nr_input_regs
, TGSI_WRITEMASK_XYZW
, array_id
, array_size
);
341 ureg_DECL_vs_input( struct ureg_program
*ureg
,
344 assert(ureg
->processor
== PIPE_SHADER_VERTEX
);
345 assert(index
/ 32 < ARRAY_SIZE(ureg
->vs_inputs
));
347 ureg
->vs_inputs
[index
/32] |= 1 << (index
% 32);
348 return ureg_src_register( TGSI_FILE_INPUT
, index
);
353 ureg_DECL_input_layout(struct ureg_program
*ureg
,
354 unsigned semantic_name
,
355 unsigned semantic_index
,
361 return ureg_DECL_fs_input_cyl_centroid_layout(ureg
,
362 semantic_name
, semantic_index
, 0, 0, 0,
363 index
, usage_mask
, array_id
, array_size
);
368 ureg_DECL_input(struct ureg_program
*ureg
,
369 unsigned semantic_name
,
370 unsigned semantic_index
,
374 return ureg_DECL_fs_input_cyl_centroid(ureg
, semantic_name
, semantic_index
,
375 0, 0, 0, array_id
, array_size
);
380 ureg_DECL_system_value(struct ureg_program
*ureg
,
381 unsigned semantic_name
,
382 unsigned semantic_index
)
386 for (i
= 0; i
< ureg
->nr_system_values
; i
++) {
387 if (ureg
->system_value
[i
].semantic_name
== semantic_name
&&
388 ureg
->system_value
[i
].semantic_index
== semantic_index
) {
393 if (ureg
->nr_system_values
< UREG_MAX_SYSTEM_VALUE
) {
394 ureg
->system_value
[ureg
->nr_system_values
].semantic_name
= semantic_name
;
395 ureg
->system_value
[ureg
->nr_system_values
].semantic_index
= semantic_index
;
396 i
= ureg
->nr_system_values
;
397 ureg
->nr_system_values
++;
403 return ureg_src_register(TGSI_FILE_SYSTEM_VALUE
, i
);
408 ureg_DECL_output_layout(struct ureg_program
*ureg
,
409 unsigned semantic_name
,
410 unsigned semantic_index
,
419 assert(usage_mask
!= 0);
420 assert(!(streams
& 0x03) || (usage_mask
& 1));
421 assert(!(streams
& 0x0c) || (usage_mask
& 2));
422 assert(!(streams
& 0x30) || (usage_mask
& 4));
423 assert(!(streams
& 0xc0) || (usage_mask
& 8));
425 for (i
= 0; i
< ureg
->nr_outputs
; i
++) {
426 if (ureg
->output
[i
].semantic_name
== semantic_name
&&
427 ureg
->output
[i
].semantic_index
== semantic_index
) {
428 if (ureg
->output
[i
].array_id
== array_id
) {
429 ureg
->output
[i
].usage_mask
|= usage_mask
;
432 assert((ureg
->output
[i
].usage_mask
& usage_mask
) == 0);
436 if (ureg
->nr_outputs
< UREG_MAX_OUTPUT
) {
437 ureg
->output
[i
].semantic_name
= semantic_name
;
438 ureg
->output
[i
].semantic_index
= semantic_index
;
439 ureg
->output
[i
].usage_mask
= usage_mask
;
440 ureg
->output
[i
].first
= index
;
441 ureg
->output
[i
].last
= index
+ array_size
- 1;
442 ureg
->output
[i
].array_id
= array_id
;
443 ureg
->nr_output_regs
= MAX2(ureg
->nr_output_regs
, index
+ array_size
);
452 ureg
->output
[i
].streams
|= streams
;
454 return ureg_dst_array_register(TGSI_FILE_OUTPUT
, ureg
->output
[i
].first
,
460 ureg_DECL_output_masked(struct ureg_program
*ureg
,
467 return ureg_DECL_output_layout(ureg
, name
, index
, 0,
468 ureg
->nr_output_regs
, usage_mask
, array_id
, array_size
);
473 ureg_DECL_output(struct ureg_program
*ureg
,
477 return ureg_DECL_output_masked(ureg
, name
, index
, TGSI_WRITEMASK_XYZW
,
482 ureg_DECL_output_array(struct ureg_program
*ureg
,
483 unsigned semantic_name
,
484 unsigned semantic_index
,
488 return ureg_DECL_output_masked(ureg
, semantic_name
, semantic_index
,
490 array_id
, array_size
);
494 /* Returns a new constant register. Keep track of which have been
495 * referred to so that we can emit decls later.
497 * Constant operands declared with this function must be addressed
498 * with a two-dimensional index.
500 * There is nothing in this code to bind this constant to any tracked
501 * value or manage any constant_buffer contents -- that's the
502 * resposibility of the calling code.
505 ureg_DECL_constant2D(struct ureg_program
*ureg
,
510 struct const_decl
*decl
= &ureg
->const_decls2D
[index2D
];
512 assert(index2D
< PIPE_MAX_CONSTANT_BUFFERS
);
514 if (decl
->nr_constant_ranges
< UREG_MAX_CONSTANT_RANGE
) {
515 uint i
= decl
->nr_constant_ranges
++;
517 decl
->constant_range
[i
].first
= first
;
518 decl
->constant_range
[i
].last
= last
;
523 /* A one-dimensional, deprecated version of ureg_DECL_constant2D().
525 * Constant operands declared with this function must be addressed
526 * with a one-dimensional index.
529 ureg_DECL_constant(struct ureg_program
*ureg
,
532 struct const_decl
*decl
= &ureg
->const_decls
;
533 unsigned minconst
= index
, maxconst
= index
;
536 /* Inside existing range?
538 for (i
= 0; i
< decl
->nr_constant_ranges
; i
++) {
539 if (decl
->constant_range
[i
].first
<= index
&&
540 decl
->constant_range
[i
].last
>= index
) {
545 /* Extend existing range?
547 for (i
= 0; i
< decl
->nr_constant_ranges
; i
++) {
548 if (decl
->constant_range
[i
].last
== index
- 1) {
549 decl
->constant_range
[i
].last
= index
;
553 if (decl
->constant_range
[i
].first
== index
+ 1) {
554 decl
->constant_range
[i
].first
= index
;
558 minconst
= MIN2(minconst
, decl
->constant_range
[i
].first
);
559 maxconst
= MAX2(maxconst
, decl
->constant_range
[i
].last
);
564 if (decl
->nr_constant_ranges
< UREG_MAX_CONSTANT_RANGE
) {
565 i
= decl
->nr_constant_ranges
++;
566 decl
->constant_range
[i
].first
= index
;
567 decl
->constant_range
[i
].last
= index
;
571 /* Collapse all ranges down to one:
574 decl
->constant_range
[0].first
= minconst
;
575 decl
->constant_range
[0].last
= maxconst
;
576 decl
->nr_constant_ranges
= 1;
579 assert(i
< decl
->nr_constant_ranges
);
580 assert(decl
->constant_range
[i
].first
<= index
);
581 assert(decl
->constant_range
[i
].last
>= index
);
582 return ureg_src_register(TGSI_FILE_CONSTANT
, index
);
585 static struct ureg_dst
alloc_temporary( struct ureg_program
*ureg
,
590 /* Look for a released temporary.
592 for (i
= util_bitmask_get_first_index(ureg
->free_temps
);
593 i
!= UTIL_BITMASK_INVALID_INDEX
;
594 i
= util_bitmask_get_next_index(ureg
->free_temps
, i
+ 1)) {
595 if (util_bitmask_get(ureg
->local_temps
, i
) == local
)
599 /* Or allocate a new one.
601 if (i
== UTIL_BITMASK_INVALID_INDEX
) {
602 i
= ureg
->nr_temps
++;
605 util_bitmask_set(ureg
->local_temps
, i
);
607 /* Start a new declaration when the local flag changes */
608 if (!i
|| util_bitmask_get(ureg
->local_temps
, i
- 1) != local
)
609 util_bitmask_set(ureg
->decl_temps
, i
);
612 util_bitmask_clear(ureg
->free_temps
, i
);
614 return ureg_dst_register( TGSI_FILE_TEMPORARY
, i
);
617 struct ureg_dst
ureg_DECL_temporary( struct ureg_program
*ureg
)
619 return alloc_temporary(ureg
, FALSE
);
622 struct ureg_dst
ureg_DECL_local_temporary( struct ureg_program
*ureg
)
624 return alloc_temporary(ureg
, TRUE
);
627 struct ureg_dst
ureg_DECL_array_temporary( struct ureg_program
*ureg
,
631 unsigned i
= ureg
->nr_temps
;
632 struct ureg_dst dst
= ureg_dst_register( TGSI_FILE_TEMPORARY
, i
);
635 util_bitmask_set(ureg
->local_temps
, i
);
637 /* Always start a new declaration at the start */
638 util_bitmask_set(ureg
->decl_temps
, i
);
640 ureg
->nr_temps
+= size
;
642 /* and also at the end of the array */
643 util_bitmask_set(ureg
->decl_temps
, ureg
->nr_temps
);
645 if (ureg
->nr_array_temps
< UREG_MAX_ARRAY_TEMPS
) {
646 ureg
->array_temps
[ureg
->nr_array_temps
++] = i
;
647 dst
.ArrayID
= ureg
->nr_array_temps
;
653 void ureg_release_temporary( struct ureg_program
*ureg
,
654 struct ureg_dst tmp
)
656 if(tmp
.File
== TGSI_FILE_TEMPORARY
)
657 util_bitmask_set(ureg
->free_temps
, tmp
.Index
);
661 /* Allocate a new address register.
663 struct ureg_dst
ureg_DECL_address( struct ureg_program
*ureg
)
665 if (ureg
->nr_addrs
< UREG_MAX_ADDR
)
666 return ureg_dst_register( TGSI_FILE_ADDRESS
, ureg
->nr_addrs
++ );
669 return ureg_dst_register( TGSI_FILE_ADDRESS
, 0 );
672 /* Allocate a new sampler.
674 struct ureg_src
ureg_DECL_sampler( struct ureg_program
*ureg
,
679 for (i
= 0; i
< ureg
->nr_samplers
; i
++)
680 if (ureg
->sampler
[i
].Index
== nr
)
681 return ureg
->sampler
[i
];
683 if (i
< PIPE_MAX_SAMPLERS
) {
684 ureg
->sampler
[i
] = ureg_src_register( TGSI_FILE_SAMPLER
, nr
);
686 return ureg
->sampler
[i
];
690 return ureg
->sampler
[0];
694 * Allocate a new shader sampler view.
697 ureg_DECL_sampler_view(struct ureg_program
*ureg
,
700 unsigned return_type_x
,
701 unsigned return_type_y
,
702 unsigned return_type_z
,
703 unsigned return_type_w
)
705 struct ureg_src reg
= ureg_src_register(TGSI_FILE_SAMPLER_VIEW
, index
);
708 for (i
= 0; i
< ureg
->nr_sampler_views
; i
++) {
709 if (ureg
->sampler_view
[i
].index
== index
) {
714 if (i
< PIPE_MAX_SHADER_SAMPLER_VIEWS
) {
715 ureg
->sampler_view
[i
].index
= index
;
716 ureg
->sampler_view
[i
].target
= target
;
717 ureg
->sampler_view
[i
].return_type_x
= return_type_x
;
718 ureg
->sampler_view
[i
].return_type_y
= return_type_y
;
719 ureg
->sampler_view
[i
].return_type_z
= return_type_z
;
720 ureg
->sampler_view
[i
].return_type_w
= return_type_w
;
721 ureg
->nr_sampler_views
++;
729 /* Allocate a new image.
732 ureg_DECL_image(struct ureg_program
*ureg
,
739 struct ureg_src reg
= ureg_src_register(TGSI_FILE_IMAGE
, index
);
742 for (i
= 0; i
< ureg
->nr_images
; i
++)
743 if (ureg
->image
[i
].index
== index
)
746 if (i
< PIPE_MAX_SHADER_IMAGES
) {
747 ureg
->image
[i
].index
= index
;
748 ureg
->image
[i
].target
= target
;
749 ureg
->image
[i
].wr
= wr
;
750 ureg
->image
[i
].raw
= raw
;
751 ureg
->image
[i
].format
= format
;
760 /* Allocate a new buffer.
762 struct ureg_src
ureg_DECL_buffer(struct ureg_program
*ureg
, unsigned nr
,
765 struct ureg_src reg
= ureg_src_register(TGSI_FILE_BUFFER
, nr
);
768 for (i
= 0; i
< ureg
->nr_buffers
; i
++)
769 if (ureg
->buffer
[i
].index
== nr
)
772 if (i
< PIPE_MAX_SHADER_BUFFERS
) {
773 ureg
->buffer
[i
].index
= nr
;
774 ureg
->buffer
[i
].atomic
= atomic
;
783 /* Allocate a memory area.
785 struct ureg_src
ureg_DECL_memory(struct ureg_program
*ureg
,
786 unsigned memory_type
)
788 struct ureg_src reg
= ureg_src_register(TGSI_FILE_MEMORY
, memory_type
);
790 ureg
->use_memory
[memory_type
] = true;
795 match_or_expand_immediate64( const unsigned *v
,
802 unsigned nr2
= *pnr2
;
806 for (i
= 0; i
< nr
; i
+= 2) {
807 boolean found
= FALSE
;
809 for (j
= 0; j
< nr2
&& !found
; j
+= 2) {
810 if (v
[i
] == v2
[j
] && v
[i
+ 1] == v2
[j
+ 1]) {
811 *swizzle
|= (j
<< (i
* 2)) | ((j
+ 1) << ((i
+ 1) * 2));
821 v2
[nr2
+ 1] = v
[i
+ 1];
823 *swizzle
|= (nr2
<< (i
* 2)) | ((nr2
+ 1) << ((i
+ 1) * 2));
828 /* Actually expand immediate only when fully succeeded.
835 match_or_expand_immediate( const unsigned *v
,
842 unsigned nr2
= *pnr2
;
845 if (type
== TGSI_IMM_FLOAT64
||
846 type
== TGSI_IMM_UINT64
||
847 type
== TGSI_IMM_INT64
)
848 return match_or_expand_immediate64(v
, type
, nr
, v2
, pnr2
, swizzle
);
852 for (i
= 0; i
< nr
; i
++) {
853 boolean found
= FALSE
;
855 for (j
= 0; j
< nr2
&& !found
; j
++) {
857 *swizzle
|= j
<< (i
* 2);
868 *swizzle
|= nr2
<< (i
* 2);
873 /* Actually expand immediate only when fully succeeded.
880 static struct ureg_src
881 decl_immediate( struct ureg_program
*ureg
,
887 unsigned swizzle
= 0;
889 /* Could do a first pass where we examine all existing immediates
893 for (i
= 0; i
< ureg
->nr_immediates
; i
++) {
894 if (ureg
->immediate
[i
].type
!= type
) {
897 if (match_or_expand_immediate(v
,
900 ureg
->immediate
[i
].value
.u
,
901 &ureg
->immediate
[i
].nr
,
907 if (ureg
->nr_immediates
< UREG_MAX_IMMEDIATE
) {
908 i
= ureg
->nr_immediates
++;
909 ureg
->immediate
[i
].type
= type
;
910 if (match_or_expand_immediate(v
,
913 ureg
->immediate
[i
].value
.u
,
914 &ureg
->immediate
[i
].nr
,
923 /* Make sure that all referenced elements are from this immediate.
924 * Has the effect of making size-one immediates into scalars.
926 if (type
== TGSI_IMM_FLOAT64
||
927 type
== TGSI_IMM_UINT64
||
928 type
== TGSI_IMM_INT64
) {
929 for (j
= nr
; j
< 4; j
+=2) {
930 swizzle
|= (swizzle
& 0xf) << (j
* 2);
933 for (j
= nr
; j
< 4; j
++) {
934 swizzle
|= (swizzle
& 0x3) << (j
* 2);
937 return ureg_swizzle(ureg_src_register(TGSI_FILE_IMMEDIATE
, i
),
938 (swizzle
>> 0) & 0x3,
939 (swizzle
>> 2) & 0x3,
940 (swizzle
>> 4) & 0x3,
941 (swizzle
>> 6) & 0x3);
946 ureg_DECL_immediate( struct ureg_program
*ureg
,
956 for (i
= 0; i
< nr
; i
++) {
960 return decl_immediate(ureg
, fu
.u
, nr
, TGSI_IMM_FLOAT32
);
964 ureg_DECL_immediate_f64( struct ureg_program
*ureg
,
974 assert((nr
/ 2) < 3);
975 for (i
= 0; i
< nr
/ 2; i
++) {
979 return decl_immediate(ureg
, fu
.u
, nr
, TGSI_IMM_FLOAT64
);
983 ureg_DECL_immediate_uint( struct ureg_program
*ureg
,
987 return decl_immediate(ureg
, v
, nr
, TGSI_IMM_UINT32
);
992 ureg_DECL_immediate_block_uint( struct ureg_program
*ureg
,
999 if (ureg
->nr_immediates
+ (nr
+ 3) / 4 > UREG_MAX_IMMEDIATE
) {
1001 return ureg_src_register(TGSI_FILE_IMMEDIATE
, 0);
1004 index
= ureg
->nr_immediates
;
1005 ureg
->nr_immediates
+= (nr
+ 3) / 4;
1007 for (i
= index
; i
< ureg
->nr_immediates
; i
++) {
1008 ureg
->immediate
[i
].type
= TGSI_IMM_UINT32
;
1009 ureg
->immediate
[i
].nr
= nr
> 4 ? 4 : nr
;
1010 memcpy(ureg
->immediate
[i
].value
.u
,
1011 &v
[(i
- index
) * 4],
1012 ureg
->immediate
[i
].nr
* sizeof(uint
));
1016 return ureg_src_register(TGSI_FILE_IMMEDIATE
, index
);
1021 ureg_DECL_immediate_int( struct ureg_program
*ureg
,
1025 return decl_immediate(ureg
, (const unsigned *)v
, nr
, TGSI_IMM_INT32
);
1029 ureg_DECL_immediate_uint64( struct ureg_program
*ureg
,
1039 assert((nr
/ 2) < 3);
1040 for (i
= 0; i
< nr
/ 2; i
++) {
1044 return decl_immediate(ureg
, fu
.u
, nr
, TGSI_IMM_UINT64
);
1048 ureg_DECL_immediate_int64( struct ureg_program
*ureg
,
1058 assert((nr
/ 2) < 3);
1059 for (i
= 0; i
< nr
/ 2; i
++) {
1063 return decl_immediate(ureg
, fu
.u
, nr
, TGSI_IMM_INT64
);
1067 ureg_emit_src( struct ureg_program
*ureg
,
1068 struct ureg_src src
)
1070 unsigned size
= 1 + (src
.Indirect
? 1 : 0) +
1071 (src
.Dimension
? (src
.DimIndirect
? 2 : 1) : 0);
1073 union tgsi_any_token
*out
= get_tokens( ureg
, DOMAIN_INSN
, size
);
1076 assert(src
.File
!= TGSI_FILE_NULL
);
1077 assert(src
.File
< TGSI_FILE_COUNT
);
1080 out
[n
].src
.File
= src
.File
;
1081 out
[n
].src
.SwizzleX
= src
.SwizzleX
;
1082 out
[n
].src
.SwizzleY
= src
.SwizzleY
;
1083 out
[n
].src
.SwizzleZ
= src
.SwizzleZ
;
1084 out
[n
].src
.SwizzleW
= src
.SwizzleW
;
1085 out
[n
].src
.Index
= src
.Index
;
1086 out
[n
].src
.Negate
= src
.Negate
;
1087 out
[0].src
.Absolute
= src
.Absolute
;
1091 out
[0].src
.Indirect
= 1;
1093 out
[n
].ind
.File
= src
.IndirectFile
;
1094 out
[n
].ind
.Swizzle
= src
.IndirectSwizzle
;
1095 out
[n
].ind
.Index
= src
.IndirectIndex
;
1096 if (!ureg
->supports_any_inout_decl_range
&&
1097 (src
.File
== TGSI_FILE_INPUT
|| src
.File
== TGSI_FILE_OUTPUT
))
1098 out
[n
].ind
.ArrayID
= 0;
1100 out
[n
].ind
.ArrayID
= src
.ArrayID
;
1104 if (src
.Dimension
) {
1105 out
[0].src
.Dimension
= 1;
1106 out
[n
].dim
.Dimension
= 0;
1107 out
[n
].dim
.Padding
= 0;
1108 if (src
.DimIndirect
) {
1109 out
[n
].dim
.Indirect
= 1;
1110 out
[n
].dim
.Index
= src
.DimensionIndex
;
1113 out
[n
].ind
.File
= src
.DimIndFile
;
1114 out
[n
].ind
.Swizzle
= src
.DimIndSwizzle
;
1115 out
[n
].ind
.Index
= src
.DimIndIndex
;
1116 if (!ureg
->supports_any_inout_decl_range
&&
1117 (src
.File
== TGSI_FILE_INPUT
|| src
.File
== TGSI_FILE_OUTPUT
))
1118 out
[n
].ind
.ArrayID
= 0;
1120 out
[n
].ind
.ArrayID
= src
.ArrayID
;
1122 out
[n
].dim
.Indirect
= 0;
1123 out
[n
].dim
.Index
= src
.DimensionIndex
;
1133 ureg_emit_dst( struct ureg_program
*ureg
,
1134 struct ureg_dst dst
)
1136 unsigned size
= 1 + (dst
.Indirect
? 1 : 0) +
1137 (dst
.Dimension
? (dst
.DimIndirect
? 2 : 1) : 0);
1139 union tgsi_any_token
*out
= get_tokens( ureg
, DOMAIN_INSN
, size
);
1142 assert(dst
.File
!= TGSI_FILE_NULL
);
1143 assert(dst
.File
!= TGSI_FILE_SAMPLER
);
1144 assert(dst
.File
!= TGSI_FILE_SAMPLER_VIEW
);
1145 assert(dst
.File
!= TGSI_FILE_IMMEDIATE
);
1146 assert(dst
.File
< TGSI_FILE_COUNT
);
1149 out
[n
].dst
.File
= dst
.File
;
1150 out
[n
].dst
.WriteMask
= dst
.WriteMask
;
1151 out
[n
].dst
.Indirect
= dst
.Indirect
;
1152 out
[n
].dst
.Index
= dst
.Index
;
1157 out
[n
].ind
.File
= dst
.IndirectFile
;
1158 out
[n
].ind
.Swizzle
= dst
.IndirectSwizzle
;
1159 out
[n
].ind
.Index
= dst
.IndirectIndex
;
1160 if (!ureg
->supports_any_inout_decl_range
&&
1161 (dst
.File
== TGSI_FILE_INPUT
|| dst
.File
== TGSI_FILE_OUTPUT
))
1162 out
[n
].ind
.ArrayID
= 0;
1164 out
[n
].ind
.ArrayID
= dst
.ArrayID
;
1168 if (dst
.Dimension
) {
1169 out
[0].dst
.Dimension
= 1;
1170 out
[n
].dim
.Dimension
= 0;
1171 out
[n
].dim
.Padding
= 0;
1172 if (dst
.DimIndirect
) {
1173 out
[n
].dim
.Indirect
= 1;
1174 out
[n
].dim
.Index
= dst
.DimensionIndex
;
1177 out
[n
].ind
.File
= dst
.DimIndFile
;
1178 out
[n
].ind
.Swizzle
= dst
.DimIndSwizzle
;
1179 out
[n
].ind
.Index
= dst
.DimIndIndex
;
1180 if (!ureg
->supports_any_inout_decl_range
&&
1181 (dst
.File
== TGSI_FILE_INPUT
|| dst
.File
== TGSI_FILE_OUTPUT
))
1182 out
[n
].ind
.ArrayID
= 0;
1184 out
[n
].ind
.ArrayID
= dst
.ArrayID
;
1186 out
[n
].dim
.Indirect
= 0;
1187 out
[n
].dim
.Index
= dst
.DimensionIndex
;
1196 static void validate( unsigned opcode
,
1201 const struct tgsi_opcode_info
*info
= tgsi_get_opcode_info( opcode
);
1204 assert(nr_dst
== info
->num_dst
);
1205 assert(nr_src
== info
->num_src
);
1210 struct ureg_emit_insn_result
1211 ureg_emit_insn(struct ureg_program
*ureg
,
1217 union tgsi_any_token
*out
;
1219 struct ureg_emit_insn_result result
;
1221 validate( opcode
, num_dst
, num_src
);
1223 out
= get_tokens( ureg
, DOMAIN_INSN
, count
);
1224 out
[0].insn
= tgsi_default_instruction();
1225 out
[0].insn
.Opcode
= opcode
;
1226 out
[0].insn
.Saturate
= saturate
;
1227 out
[0].insn
.NumDstRegs
= num_dst
;
1228 out
[0].insn
.NumSrcRegs
= num_src
;
1230 result
.insn_token
= ureg
->domain
[DOMAIN_INSN
].count
- count
;
1231 result
.extended_token
= result
.insn_token
;
1233 ureg
->nr_instructions
++;
1240 * Emit a label token.
1241 * \param label_token returns a token number indicating where the label
1242 * needs to be patched later. Later, this value should be passed to the
1243 * ureg_fixup_label() function.
1246 ureg_emit_label(struct ureg_program
*ureg
,
1247 unsigned extended_token
,
1248 unsigned *label_token
)
1250 union tgsi_any_token
*out
, *insn
;
1255 out
= get_tokens( ureg
, DOMAIN_INSN
, 1 );
1258 insn
= retrieve_token( ureg
, DOMAIN_INSN
, extended_token
);
1259 insn
->insn
.Label
= 1;
1261 *label_token
= ureg
->domain
[DOMAIN_INSN
].count
- 1;
1264 /* Will return a number which can be used in a label to point to the
1265 * next instruction to be emitted.
1268 ureg_get_instruction_number( struct ureg_program
*ureg
)
1270 return ureg
->nr_instructions
;
1273 /* Patch a given label (expressed as a token number) to point to a
1274 * given instruction (expressed as an instruction number).
1277 ureg_fixup_label(struct ureg_program
*ureg
,
1278 unsigned label_token
,
1279 unsigned instruction_number
)
1281 union tgsi_any_token
*out
= retrieve_token( ureg
, DOMAIN_INSN
, label_token
);
1283 out
->insn_label
.Label
= instruction_number
;
1288 ureg_emit_texture(struct ureg_program
*ureg
,
1289 unsigned extended_token
,
1290 unsigned target
, unsigned return_type
, unsigned num_offsets
)
1292 union tgsi_any_token
*out
, *insn
;
1294 out
= get_tokens( ureg
, DOMAIN_INSN
, 1 );
1295 insn
= retrieve_token( ureg
, DOMAIN_INSN
, extended_token
);
1297 insn
->insn
.Texture
= 1;
1300 out
[0].insn_texture
.Texture
= target
;
1301 out
[0].insn_texture
.NumOffsets
= num_offsets
;
1302 out
[0].insn_texture
.ReturnType
= return_type
;
1306 ureg_emit_texture_offset(struct ureg_program
*ureg
,
1307 const struct tgsi_texture_offset
*offset
)
1309 union tgsi_any_token
*out
;
1311 out
= get_tokens( ureg
, DOMAIN_INSN
, 1);
1314 out
[0].insn_texture_offset
= *offset
;
1319 ureg_emit_memory(struct ureg_program
*ureg
,
1320 unsigned extended_token
,
1325 union tgsi_any_token
*out
, *insn
;
1327 out
= get_tokens( ureg
, DOMAIN_INSN
, 1 );
1328 insn
= retrieve_token( ureg
, DOMAIN_INSN
, extended_token
);
1330 insn
->insn
.Memory
= 1;
1333 out
[0].insn_memory
.Qualifier
= qualifier
;
1334 out
[0].insn_memory
.Texture
= texture
;
1335 out
[0].insn_memory
.Format
= format
;
1339 ureg_fixup_insn_size(struct ureg_program
*ureg
,
1342 union tgsi_any_token
*out
= retrieve_token( ureg
, DOMAIN_INSN
, insn
);
1344 assert(out
->insn
.Type
== TGSI_TOKEN_TYPE_INSTRUCTION
);
1345 out
->insn
.NrTokens
= ureg
->domain
[DOMAIN_INSN
].count
- insn
- 1;
1350 ureg_insn(struct ureg_program
*ureg
,
1352 const struct ureg_dst
*dst
,
1354 const struct ureg_src
*src
,
1357 struct ureg_emit_insn_result insn
;
1361 if (nr_dst
&& ureg_dst_is_empty(dst
[0])) {
1365 saturate
= nr_dst
? dst
[0].Saturate
: FALSE
;
1367 insn
= ureg_emit_insn(ureg
,
1373 for (i
= 0; i
< nr_dst
; i
++)
1374 ureg_emit_dst( ureg
, dst
[i
] );
1376 for (i
= 0; i
< nr_src
; i
++)
1377 ureg_emit_src( ureg
, src
[i
] );
1379 ureg_fixup_insn_size( ureg
, insn
.insn_token
);
1383 ureg_tex_insn(struct ureg_program
*ureg
,
1385 const struct ureg_dst
*dst
,
1388 unsigned return_type
,
1389 const struct tgsi_texture_offset
*texoffsets
,
1391 const struct ureg_src
*src
,
1394 struct ureg_emit_insn_result insn
;
1398 if (nr_dst
&& ureg_dst_is_empty(dst
[0])) {
1402 saturate
= nr_dst
? dst
[0].Saturate
: FALSE
;
1404 insn
= ureg_emit_insn(ureg
,
1410 ureg_emit_texture( ureg
, insn
.extended_token
, target
, return_type
,
1413 for (i
= 0; i
< nr_offset
; i
++)
1414 ureg_emit_texture_offset( ureg
, &texoffsets
[i
]);
1416 for (i
= 0; i
< nr_dst
; i
++)
1417 ureg_emit_dst( ureg
, dst
[i
] );
1419 for (i
= 0; i
< nr_src
; i
++)
1420 ureg_emit_src( ureg
, src
[i
] );
1422 ureg_fixup_insn_size( ureg
, insn
.insn_token
);
1427 ureg_memory_insn(struct ureg_program
*ureg
,
1429 const struct ureg_dst
*dst
,
1431 const struct ureg_src
*src
,
1437 struct ureg_emit_insn_result insn
;
1440 insn
= ureg_emit_insn(ureg
,
1446 ureg_emit_memory(ureg
, insn
.extended_token
, qualifier
, texture
, format
);
1448 for (i
= 0; i
< nr_dst
; i
++)
1449 ureg_emit_dst(ureg
, dst
[i
]);
1451 for (i
= 0; i
< nr_src
; i
++)
1452 ureg_emit_src(ureg
, src
[i
]);
1454 ureg_fixup_insn_size(ureg
, insn
.insn_token
);
1459 emit_decl_semantic(struct ureg_program
*ureg
,
1463 unsigned semantic_name
,
1464 unsigned semantic_index
,
1466 unsigned usage_mask
,
1469 union tgsi_any_token
*out
= get_tokens(ureg
, DOMAIN_DECL
, array_id
? 4 : 3);
1472 out
[0].decl
.Type
= TGSI_TOKEN_TYPE_DECLARATION
;
1473 out
[0].decl
.NrTokens
= 3;
1474 out
[0].decl
.File
= file
;
1475 out
[0].decl
.UsageMask
= usage_mask
;
1476 out
[0].decl
.Semantic
= 1;
1477 out
[0].decl
.Array
= array_id
!= 0;
1480 out
[1].decl_range
.First
= first
;
1481 out
[1].decl_range
.Last
= last
;
1484 out
[2].decl_semantic
.Name
= semantic_name
;
1485 out
[2].decl_semantic
.Index
= semantic_index
;
1486 out
[2].decl_semantic
.StreamX
= streams
& 3;
1487 out
[2].decl_semantic
.StreamY
= (streams
>> 2) & 3;
1488 out
[2].decl_semantic
.StreamZ
= (streams
>> 4) & 3;
1489 out
[2].decl_semantic
.StreamW
= (streams
>> 6) & 3;
1493 out
[3].array
.ArrayID
= array_id
;
1499 emit_decl_fs(struct ureg_program
*ureg
,
1503 unsigned semantic_name
,
1504 unsigned semantic_index
,
1505 unsigned interpolate
,
1506 unsigned cylindrical_wrap
,
1507 unsigned interpolate_location
,
1509 unsigned usage_mask
)
1511 union tgsi_any_token
*out
= get_tokens(ureg
, DOMAIN_DECL
,
1515 out
[0].decl
.Type
= TGSI_TOKEN_TYPE_DECLARATION
;
1516 out
[0].decl
.NrTokens
= 4;
1517 out
[0].decl
.File
= file
;
1518 out
[0].decl
.UsageMask
= usage_mask
;
1519 out
[0].decl
.Interpolate
= 1;
1520 out
[0].decl
.Semantic
= 1;
1521 out
[0].decl
.Array
= array_id
!= 0;
1524 out
[1].decl_range
.First
= first
;
1525 out
[1].decl_range
.Last
= last
;
1528 out
[2].decl_interp
.Interpolate
= interpolate
;
1529 out
[2].decl_interp
.CylindricalWrap
= cylindrical_wrap
;
1530 out
[2].decl_interp
.Location
= interpolate_location
;
1533 out
[3].decl_semantic
.Name
= semantic_name
;
1534 out
[3].decl_semantic
.Index
= semantic_index
;
1538 out
[4].array
.ArrayID
= array_id
;
1543 emit_decl_temps( struct ureg_program
*ureg
,
1544 unsigned first
, unsigned last
,
1548 union tgsi_any_token
*out
= get_tokens( ureg
, DOMAIN_DECL
,
1552 out
[0].decl
.Type
= TGSI_TOKEN_TYPE_DECLARATION
;
1553 out
[0].decl
.NrTokens
= 2;
1554 out
[0].decl
.File
= TGSI_FILE_TEMPORARY
;
1555 out
[0].decl
.UsageMask
= TGSI_WRITEMASK_XYZW
;
1556 out
[0].decl
.Local
= local
;
1559 out
[1].decl_range
.First
= first
;
1560 out
[1].decl_range
.Last
= last
;
1563 out
[0].decl
.Array
= 1;
1565 out
[2].array
.ArrayID
= arrayid
;
1569 static void emit_decl_range( struct ureg_program
*ureg
,
1574 union tgsi_any_token
*out
= get_tokens( ureg
, DOMAIN_DECL
, 2 );
1577 out
[0].decl
.Type
= TGSI_TOKEN_TYPE_DECLARATION
;
1578 out
[0].decl
.NrTokens
= 2;
1579 out
[0].decl
.File
= file
;
1580 out
[0].decl
.UsageMask
= TGSI_WRITEMASK_XYZW
;
1581 out
[0].decl
.Semantic
= 0;
1584 out
[1].decl_range
.First
= first
;
1585 out
[1].decl_range
.Last
= first
+ count
- 1;
1589 emit_decl_range2D(struct ureg_program
*ureg
,
1595 union tgsi_any_token
*out
= get_tokens(ureg
, DOMAIN_DECL
, 3);
1598 out
[0].decl
.Type
= TGSI_TOKEN_TYPE_DECLARATION
;
1599 out
[0].decl
.NrTokens
= 3;
1600 out
[0].decl
.File
= file
;
1601 out
[0].decl
.UsageMask
= TGSI_WRITEMASK_XYZW
;
1602 out
[0].decl
.Dimension
= 1;
1605 out
[1].decl_range
.First
= first
;
1606 out
[1].decl_range
.Last
= last
;
1609 out
[2].decl_dim
.Index2D
= index2D
;
1613 emit_decl_sampler_view(struct ureg_program
*ureg
,
1616 unsigned return_type_x
,
1617 unsigned return_type_y
,
1618 unsigned return_type_z
,
1619 unsigned return_type_w
)
1621 union tgsi_any_token
*out
= get_tokens(ureg
, DOMAIN_DECL
, 3);
1624 out
[0].decl
.Type
= TGSI_TOKEN_TYPE_DECLARATION
;
1625 out
[0].decl
.NrTokens
= 3;
1626 out
[0].decl
.File
= TGSI_FILE_SAMPLER_VIEW
;
1627 out
[0].decl
.UsageMask
= TGSI_WRITEMASK_XYZW
;
1630 out
[1].decl_range
.First
= index
;
1631 out
[1].decl_range
.Last
= index
;
1634 out
[2].decl_sampler_view
.Resource
= target
;
1635 out
[2].decl_sampler_view
.ReturnTypeX
= return_type_x
;
1636 out
[2].decl_sampler_view
.ReturnTypeY
= return_type_y
;
1637 out
[2].decl_sampler_view
.ReturnTypeZ
= return_type_z
;
1638 out
[2].decl_sampler_view
.ReturnTypeW
= return_type_w
;
1642 emit_decl_image(struct ureg_program
*ureg
,
1649 union tgsi_any_token
*out
= get_tokens(ureg
, DOMAIN_DECL
, 3);
1652 out
[0].decl
.Type
= TGSI_TOKEN_TYPE_DECLARATION
;
1653 out
[0].decl
.NrTokens
= 3;
1654 out
[0].decl
.File
= TGSI_FILE_IMAGE
;
1655 out
[0].decl
.UsageMask
= TGSI_WRITEMASK_XYZW
;
1658 out
[1].decl_range
.First
= index
;
1659 out
[1].decl_range
.Last
= index
;
1662 out
[2].decl_image
.Resource
= target
;
1663 out
[2].decl_image
.Writable
= wr
;
1664 out
[2].decl_image
.Raw
= raw
;
1665 out
[2].decl_image
.Format
= format
;
1669 emit_decl_buffer(struct ureg_program
*ureg
,
1673 union tgsi_any_token
*out
= get_tokens(ureg
, DOMAIN_DECL
, 2);
1676 out
[0].decl
.Type
= TGSI_TOKEN_TYPE_DECLARATION
;
1677 out
[0].decl
.NrTokens
= 2;
1678 out
[0].decl
.File
= TGSI_FILE_BUFFER
;
1679 out
[0].decl
.UsageMask
= TGSI_WRITEMASK_XYZW
;
1680 out
[0].decl
.Atomic
= atomic
;
1683 out
[1].decl_range
.First
= index
;
1684 out
[1].decl_range
.Last
= index
;
1688 emit_decl_memory(struct ureg_program
*ureg
, unsigned memory_type
)
1690 union tgsi_any_token
*out
= get_tokens(ureg
, DOMAIN_DECL
, 2);
1693 out
[0].decl
.Type
= TGSI_TOKEN_TYPE_DECLARATION
;
1694 out
[0].decl
.NrTokens
= 2;
1695 out
[0].decl
.File
= TGSI_FILE_MEMORY
;
1696 out
[0].decl
.UsageMask
= TGSI_WRITEMASK_XYZW
;
1697 out
[0].decl
.MemType
= memory_type
;
1700 out
[1].decl_range
.First
= memory_type
;
1701 out
[1].decl_range
.Last
= memory_type
;
1705 emit_immediate( struct ureg_program
*ureg
,
1709 union tgsi_any_token
*out
= get_tokens( ureg
, DOMAIN_DECL
, 5 );
1712 out
[0].imm
.Type
= TGSI_TOKEN_TYPE_IMMEDIATE
;
1713 out
[0].imm
.NrTokens
= 5;
1714 out
[0].imm
.DataType
= type
;
1715 out
[0].imm
.Padding
= 0;
1717 out
[1].imm_data
.Uint
= v
[0];
1718 out
[2].imm_data
.Uint
= v
[1];
1719 out
[3].imm_data
.Uint
= v
[2];
1720 out
[4].imm_data
.Uint
= v
[3];
1724 emit_property(struct ureg_program
*ureg
,
1728 union tgsi_any_token
*out
= get_tokens(ureg
, DOMAIN_DECL
, 2);
1731 out
[0].prop
.Type
= TGSI_TOKEN_TYPE_PROPERTY
;
1732 out
[0].prop
.NrTokens
= 2;
1733 out
[0].prop
.PropertyName
= name
;
1735 out
[1].prop_data
.Data
= data
;
1739 static void emit_decls( struct ureg_program
*ureg
)
1743 for (i
= 0; i
< ARRAY_SIZE(ureg
->properties
); i
++)
1744 if (ureg
->properties
[i
] != ~0)
1745 emit_property(ureg
, i
, ureg
->properties
[i
]);
1747 if (ureg
->processor
== PIPE_SHADER_VERTEX
) {
1748 for (i
= 0; i
< PIPE_MAX_ATTRIBS
; i
++) {
1749 if (ureg
->vs_inputs
[i
/32] & (1u << (i
%32))) {
1750 emit_decl_range( ureg
, TGSI_FILE_INPUT
, i
, 1 );
1753 } else if (ureg
->processor
== PIPE_SHADER_FRAGMENT
) {
1754 if (ureg
->supports_any_inout_decl_range
) {
1755 for (i
= 0; i
< ureg
->nr_inputs
; i
++) {
1758 ureg
->input
[i
].first
,
1759 ureg
->input
[i
].last
,
1760 ureg
->input
[i
].semantic_name
,
1761 ureg
->input
[i
].semantic_index
,
1762 ureg
->input
[i
].interp
,
1763 ureg
->input
[i
].cylindrical_wrap
,
1764 ureg
->input
[i
].interp_location
,
1765 ureg
->input
[i
].array_id
,
1766 ureg
->input
[i
].usage_mask
);
1770 for (i
= 0; i
< ureg
->nr_inputs
; i
++) {
1771 for (j
= ureg
->input
[i
].first
; j
<= ureg
->input
[i
].last
; j
++) {
1775 ureg
->input
[i
].semantic_name
,
1776 ureg
->input
[i
].semantic_index
+
1777 (j
- ureg
->input
[i
].first
),
1778 ureg
->input
[i
].interp
,
1779 ureg
->input
[i
].cylindrical_wrap
,
1780 ureg
->input
[i
].interp_location
, 0,
1781 ureg
->input
[i
].usage_mask
);
1786 if (ureg
->supports_any_inout_decl_range
) {
1787 for (i
= 0; i
< ureg
->nr_inputs
; i
++) {
1788 emit_decl_semantic(ureg
,
1790 ureg
->input
[i
].first
,
1791 ureg
->input
[i
].last
,
1792 ureg
->input
[i
].semantic_name
,
1793 ureg
->input
[i
].semantic_index
,
1795 TGSI_WRITEMASK_XYZW
,
1796 ureg
->input
[i
].array_id
);
1800 for (i
= 0; i
< ureg
->nr_inputs
; i
++) {
1801 for (j
= ureg
->input
[i
].first
; j
<= ureg
->input
[i
].last
; j
++) {
1802 emit_decl_semantic(ureg
,
1805 ureg
->input
[i
].semantic_name
,
1806 ureg
->input
[i
].semantic_index
+
1807 (j
- ureg
->input
[i
].first
),
1809 TGSI_WRITEMASK_XYZW
, 0);
1815 for (i
= 0; i
< ureg
->nr_system_values
; i
++) {
1816 emit_decl_semantic(ureg
,
1817 TGSI_FILE_SYSTEM_VALUE
,
1820 ureg
->system_value
[i
].semantic_name
,
1821 ureg
->system_value
[i
].semantic_index
,
1823 TGSI_WRITEMASK_XYZW
, 0);
1826 if (ureg
->supports_any_inout_decl_range
) {
1827 for (i
= 0; i
< ureg
->nr_outputs
; i
++) {
1828 emit_decl_semantic(ureg
,
1830 ureg
->output
[i
].first
,
1831 ureg
->output
[i
].last
,
1832 ureg
->output
[i
].semantic_name
,
1833 ureg
->output
[i
].semantic_index
,
1834 ureg
->output
[i
].streams
,
1835 ureg
->output
[i
].usage_mask
,
1836 ureg
->output
[i
].array_id
);
1840 for (i
= 0; i
< ureg
->nr_outputs
; i
++) {
1841 for (j
= ureg
->output
[i
].first
; j
<= ureg
->output
[i
].last
; j
++) {
1842 emit_decl_semantic(ureg
,
1845 ureg
->output
[i
].semantic_name
,
1846 ureg
->output
[i
].semantic_index
+
1847 (j
- ureg
->output
[i
].first
),
1848 ureg
->output
[i
].streams
,
1849 ureg
->output
[i
].usage_mask
, 0);
1854 for (i
= 0; i
< ureg
->nr_samplers
; i
++) {
1855 emit_decl_range( ureg
,
1857 ureg
->sampler
[i
].Index
, 1 );
1860 for (i
= 0; i
< ureg
->nr_sampler_views
; i
++) {
1861 emit_decl_sampler_view(ureg
,
1862 ureg
->sampler_view
[i
].index
,
1863 ureg
->sampler_view
[i
].target
,
1864 ureg
->sampler_view
[i
].return_type_x
,
1865 ureg
->sampler_view
[i
].return_type_y
,
1866 ureg
->sampler_view
[i
].return_type_z
,
1867 ureg
->sampler_view
[i
].return_type_w
);
1870 for (i
= 0; i
< ureg
->nr_images
; i
++) {
1871 emit_decl_image(ureg
,
1872 ureg
->image
[i
].index
,
1873 ureg
->image
[i
].target
,
1874 ureg
->image
[i
].format
,
1876 ureg
->image
[i
].raw
);
1879 for (i
= 0; i
< ureg
->nr_buffers
; i
++) {
1880 emit_decl_buffer(ureg
, ureg
->buffer
[i
].index
, ureg
->buffer
[i
].atomic
);
1883 for (i
= 0; i
< TGSI_MEMORY_TYPE_COUNT
; i
++) {
1884 if (ureg
->use_memory
[i
])
1885 emit_decl_memory(ureg
, i
);
1888 if (ureg
->const_decls
.nr_constant_ranges
) {
1889 for (i
= 0; i
< ureg
->const_decls
.nr_constant_ranges
; i
++) {
1890 emit_decl_range(ureg
,
1892 ureg
->const_decls
.constant_range
[i
].first
,
1893 ureg
->const_decls
.constant_range
[i
].last
- ureg
->const_decls
.constant_range
[i
].first
+ 1);
1897 for (i
= 0; i
< PIPE_MAX_CONSTANT_BUFFERS
; i
++) {
1898 struct const_decl
*decl
= &ureg
->const_decls2D
[i
];
1900 if (decl
->nr_constant_ranges
) {
1903 for (j
= 0; j
< decl
->nr_constant_ranges
; j
++) {
1904 emit_decl_range2D(ureg
,
1906 decl
->constant_range
[j
].first
,
1907 decl
->constant_range
[j
].last
,
1913 if (ureg
->nr_temps
) {
1915 for (i
= 0; i
< ureg
->nr_temps
;) {
1916 boolean local
= util_bitmask_get(ureg
->local_temps
, i
);
1918 i
= util_bitmask_get_next_index(ureg
->decl_temps
, i
+ 1);
1919 if (i
== UTIL_BITMASK_INVALID_INDEX
)
1922 if (array
< ureg
->nr_array_temps
&& ureg
->array_temps
[array
] == first
)
1923 emit_decl_temps( ureg
, first
, i
- 1, local
, ++array
);
1925 emit_decl_temps( ureg
, first
, i
- 1, local
, 0 );
1929 if (ureg
->nr_addrs
) {
1930 emit_decl_range( ureg
,
1932 0, ureg
->nr_addrs
);
1935 for (i
= 0; i
< ureg
->nr_immediates
; i
++) {
1936 emit_immediate( ureg
,
1937 ureg
->immediate
[i
].value
.u
,
1938 ureg
->immediate
[i
].type
);
1942 /* Append the instruction tokens onto the declarations to build a
1943 * contiguous stream suitable to send to the driver.
1945 static void copy_instructions( struct ureg_program
*ureg
)
1947 unsigned nr_tokens
= ureg
->domain
[DOMAIN_INSN
].count
;
1948 union tgsi_any_token
*out
= get_tokens( ureg
,
1953 ureg
->domain
[DOMAIN_INSN
].tokens
,
1954 nr_tokens
* sizeof out
[0] );
1959 fixup_header_size(struct ureg_program
*ureg
)
1961 union tgsi_any_token
*out
= retrieve_token( ureg
, DOMAIN_DECL
, 0 );
1963 out
->header
.BodySize
= ureg
->domain
[DOMAIN_DECL
].count
- 2;
1968 emit_header( struct ureg_program
*ureg
)
1970 union tgsi_any_token
*out
= get_tokens( ureg
, DOMAIN_DECL
, 2 );
1972 out
[0].header
.HeaderSize
= 2;
1973 out
[0].header
.BodySize
= 0;
1975 out
[1].processor
.Processor
= ureg
->processor
;
1976 out
[1].processor
.Padding
= 0;
1980 const struct tgsi_token
*ureg_finalize( struct ureg_program
*ureg
)
1982 const struct tgsi_token
*tokens
;
1984 switch (ureg
->processor
) {
1985 case PIPE_SHADER_VERTEX
:
1986 case PIPE_SHADER_TESS_EVAL
:
1987 ureg_property(ureg
, TGSI_PROPERTY_NEXT_SHADER
,
1988 ureg
->next_shader_processor
== -1 ?
1989 PIPE_SHADER_FRAGMENT
:
1990 ureg
->next_shader_processor
);
1994 emit_header( ureg
);
1996 copy_instructions( ureg
);
1997 fixup_header_size( ureg
);
1999 if (ureg
->domain
[0].tokens
== error_tokens
||
2000 ureg
->domain
[1].tokens
== error_tokens
) {
2001 debug_printf("%s: error in generated shader\n", __FUNCTION__
);
2006 tokens
= &ureg
->domain
[DOMAIN_DECL
].tokens
[0].token
;
2009 debug_printf("%s: emitted shader %d tokens:\n", __FUNCTION__
,
2010 ureg
->domain
[DOMAIN_DECL
].count
);
2011 tgsi_dump( tokens
, 0 );
2015 if (tokens
&& !tgsi_sanity_check(tokens
)) {
2016 debug_printf("tgsi_ureg.c, sanity check failed on generated tokens:\n");
2017 tgsi_dump(tokens
, 0);
2027 void *ureg_create_shader( struct ureg_program
*ureg
,
2028 struct pipe_context
*pipe
,
2029 const struct pipe_stream_output_info
*so
)
2031 struct pipe_shader_state state
;
2033 pipe_shader_state_from_tgsi(&state
, ureg_finalize(ureg
));
2038 state
.stream_output
= *so
;
2040 switch (ureg
->processor
) {
2041 case PIPE_SHADER_VERTEX
:
2042 return pipe
->create_vs_state(pipe
, &state
);
2043 case PIPE_SHADER_TESS_CTRL
:
2044 return pipe
->create_tcs_state(pipe
, &state
);
2045 case PIPE_SHADER_TESS_EVAL
:
2046 return pipe
->create_tes_state(pipe
, &state
);
2047 case PIPE_SHADER_GEOMETRY
:
2048 return pipe
->create_gs_state(pipe
, &state
);
2049 case PIPE_SHADER_FRAGMENT
:
2050 return pipe
->create_fs_state(pipe
, &state
);
2057 const struct tgsi_token
*ureg_get_tokens( struct ureg_program
*ureg
,
2058 unsigned *nr_tokens
)
2060 const struct tgsi_token
*tokens
;
2062 ureg_finalize(ureg
);
2064 tokens
= &ureg
->domain
[DOMAIN_DECL
].tokens
[0].token
;
2067 *nr_tokens
= ureg
->domain
[DOMAIN_DECL
].count
;
2069 ureg
->domain
[DOMAIN_DECL
].tokens
= 0;
2070 ureg
->domain
[DOMAIN_DECL
].size
= 0;
2071 ureg
->domain
[DOMAIN_DECL
].order
= 0;
2072 ureg
->domain
[DOMAIN_DECL
].count
= 0;
2078 void ureg_free_tokens( const struct tgsi_token
*tokens
)
2080 FREE((struct tgsi_token
*)tokens
);
2084 struct ureg_program
*
2085 ureg_create(unsigned processor
)
2087 return ureg_create_with_screen(processor
, NULL
);
2091 struct ureg_program
*
2092 ureg_create_with_screen(unsigned processor
, struct pipe_screen
*screen
)
2095 struct ureg_program
*ureg
= CALLOC_STRUCT( ureg_program
);
2099 ureg
->processor
= processor
;
2100 ureg
->supports_any_inout_decl_range
=
2102 screen
->get_shader_param(screen
, processor
,
2103 PIPE_SHADER_CAP_TGSI_ANY_INOUT_DECL_RANGE
) != 0;
2104 ureg
->next_shader_processor
= -1;
2106 for (i
= 0; i
< ARRAY_SIZE(ureg
->properties
); i
++)
2107 ureg
->properties
[i
] = ~0;
2109 ureg
->free_temps
= util_bitmask_create();
2110 if (ureg
->free_temps
== NULL
)
2113 ureg
->local_temps
= util_bitmask_create();
2114 if (ureg
->local_temps
== NULL
)
2115 goto no_local_temps
;
2117 ureg
->decl_temps
= util_bitmask_create();
2118 if (ureg
->decl_temps
== NULL
)
2124 util_bitmask_destroy(ureg
->local_temps
);
2126 util_bitmask_destroy(ureg
->free_temps
);
2135 ureg_set_next_shader_processor(struct ureg_program
*ureg
, unsigned processor
)
2137 ureg
->next_shader_processor
= processor
;
2142 ureg_get_nr_outputs( const struct ureg_program
*ureg
)
2146 return ureg
->nr_outputs
;
2150 void ureg_destroy( struct ureg_program
*ureg
)
2154 for (i
= 0; i
< ARRAY_SIZE(ureg
->domain
); i
++) {
2155 if (ureg
->domain
[i
].tokens
&&
2156 ureg
->domain
[i
].tokens
!= error_tokens
)
2157 FREE(ureg
->domain
[i
].tokens
);
2160 util_bitmask_destroy(ureg
->free_temps
);
2161 util_bitmask_destroy(ureg
->local_temps
);
2162 util_bitmask_destroy(ureg
->decl_temps
);