1 /**************************************************************************
3 * Copyright 2011-2012 Advanced Micro Devices, Inc.
4 * Copyright 2009 VMware, Inc.
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the
9 * "Software"), to deal in the Software without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, sub license, and/or sell copies of the Software, and to
12 * permit persons to whom the Software is furnished to do so, subject to
13 * the following conditions:
15 * The above copyright notice and this permission notice (including the
16 * next paragraph) shall be included in all copies or substantial portions
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
22 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
23 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
24 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
25 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 **************************************************************************/
31 * TGSI to LLVM IR translation.
33 * @author Jose Fonseca <jfonseca@vmware.com>
34 * @author Tom Stellard <thomas.stellard@amd.com>
40 #include "gallivm/lp_bld.h"
41 #include "gallivm/lp_bld_tgsi_action.h"
42 #include "gallivm/lp_bld_limits.h"
43 #include "gallivm/lp_bld_sample.h"
44 #include "lp_bld_type.h"
45 #include "pipe/p_compiler.h"
46 #include "pipe/p_state.h"
47 #include "tgsi/tgsi_exec.h"
48 #include "tgsi/tgsi_scan.h"
49 #include "tgsi/tgsi_info.h"
55 #define LP_CHAN_ALL ~0u
57 #define LP_MAX_INSTRUCTIONS 256
59 struct tgsi_full_declaration
;
60 struct tgsi_full_immediate
;
61 struct tgsi_full_instruction
;
62 struct tgsi_full_src_register
;
63 struct tgsi_opcode_info
;
65 struct tgsi_shader_info
;
66 struct lp_build_mask_context
;
68 struct lp_derivatives
;
69 struct lp_build_gs_iface
;
71 enum lp_build_tex_modifier
{
72 LP_BLD_TEX_MODIFIER_NONE
= 0,
73 LP_BLD_TEX_MODIFIER_PROJECTED
,
74 LP_BLD_TEX_MODIFIER_LOD_BIAS
,
75 LP_BLD_TEX_MODIFIER_EXPLICIT_LOD
,
76 LP_BLD_TEX_MODIFIER_EXPLICIT_DERIV
,
77 LP_BLD_TEX_MODIFIER_LOD_ZERO
82 * Describe a channel of a register.
85 * - immediate value (i.e. derived from a IMM register)
88 * - undetermined (when .file == TGSI_FILE_NULL)
90 * This is one of the analysis results, and is used to described
91 * the output color in terms of inputs.
93 struct lp_tgsi_channel_info
95 unsigned file
:4; /* TGSI_FILE_* */
96 unsigned swizzle
:3; /* PIPE_SWIZZLE_x */
99 float value
; /* for TGSI_FILE_IMMEDIATE */
105 * Describe a texture sampler interpolator.
107 * The interpolation is described in terms of regular inputs.
109 struct lp_tgsi_texture_info
111 struct lp_tgsi_channel_info coord
[4];
112 unsigned target
:8; /* TGSI_TEXTURE_* */
113 unsigned sampler_unit
:8; /* Sampler unit */
114 unsigned texture_unit
:8; /* Texture unit */
115 unsigned modifier
:8; /* LP_BLD_TEX_MODIFIER_* */
121 struct tgsi_shader_info base
;
124 * Whether any of the texture opcodes access a register file other than
127 * We could also handle TGSI_FILE_CONST/IMMEDIATE here, but there is little
130 unsigned indirect_textures
:1;
133 * Whether any of the texture (sample) ocpodes use different sampler
134 * and sampler view unit.
136 unsigned sampler_texture_units_different
:1;
139 * Whether any immediate values are outside the range of 0 and 1
141 unsigned unclamped_immediates
:1;
144 * Texture opcode description. Aimed at detecting and described direct
148 struct lp_tgsi_texture_info tex
[PIPE_MAX_SAMPLERS
];
151 * Output description. Aimed at detecting and describing simple blit
154 struct lp_tgsi_channel_info output
[PIPE_MAX_SHADER_OUTPUTS
][4];
157 * Shortcut pointers into the above (for fragment shaders).
159 const struct lp_tgsi_channel_info
*cbuf
[PIPE_MAX_COLOR_BUFS
];
163 * Reference to system values.
165 struct lp_bld_tgsi_system_values
{
166 LLVMValueRef instance_id
;
167 LLVMValueRef vertex_id
;
168 LLVMValueRef vertex_id_nobase
;
169 LLVMValueRef prim_id
;
170 LLVMValueRef basevertex
;
171 LLVMValueRef invocation_id
;
172 LLVMValueRef thread_id
;
173 LLVMValueRef block_id
;
174 LLVMValueRef grid_size
;
179 * Sampler code generation interface.
181 * Although texture sampling is a requirement for TGSI translation, it is
182 * a very different problem with several different approaches to it. This
183 * structure establishes an interface for texture sampling code generation, so
184 * that we can easily use different texture sampling strategies.
186 struct lp_build_sampler_soa
189 (*destroy
)( struct lp_build_sampler_soa
*sampler
);
192 (*emit_tex_sample
)(const struct lp_build_sampler_soa
*sampler
,
193 struct gallivm_state
*gallivm
,
194 const struct lp_sampler_params
*params
);
197 (*emit_size_query
)( const struct lp_build_sampler_soa
*sampler
,
198 struct gallivm_state
*gallivm
,
199 const struct lp_sampler_size_query_params
*params
);
203 struct lp_build_sampler_aos
206 (*emit_fetch_texel
)( const struct lp_build_sampler_aos
*sampler
,
207 struct lp_build_context
*bld
,
208 unsigned target
, /* TGSI_TEXTURE_* */
211 const struct lp_derivatives derivs
,
212 enum lp_build_tex_modifier modifier
);
215 struct lp_img_params
;
217 struct lp_build_image_soa
220 (*destroy
)( struct lp_build_image_soa
*image
);
223 (*emit_op
)(const struct lp_build_image_soa
*image
,
224 struct gallivm_state
*gallivm
,
225 const struct lp_img_params
*params
);
228 (*emit_size_query
)( const struct lp_build_image_soa
*sampler
,
229 struct gallivm_state
*gallivm
,
230 const struct lp_sampler_size_query_params
*params
);
234 lp_build_tgsi_info(const struct tgsi_token
*tokens
,
235 struct lp_tgsi_info
*info
);
238 struct lp_build_tgsi_params
{
240 struct lp_build_mask_context
*mask
;
241 LLVMValueRef consts_ptr
;
242 LLVMValueRef const_sizes_ptr
;
243 const struct lp_bld_tgsi_system_values
*system_values
;
244 const LLVMValueRef (*inputs
)[4];
245 LLVMValueRef context_ptr
;
246 LLVMValueRef thread_data_ptr
;
247 const struct lp_build_sampler_soa
*sampler
;
248 const struct tgsi_shader_info
*info
;
249 const struct lp_build_gs_iface
*gs_iface
;
250 LLVMValueRef ssbo_ptr
;
251 LLVMValueRef ssbo_sizes_ptr
;
252 const struct lp_build_image_soa
*image
;
253 LLVMValueRef shared_ptr
;
254 const struct lp_build_coro_suspend_info
*coro
;
258 lp_build_tgsi_soa(struct gallivm_state
*gallivm
,
259 const struct tgsi_token
*tokens
,
260 const struct lp_build_tgsi_params
*params
,
261 LLVMValueRef (*outputs
)[4]);
264 lp_build_tgsi_aos(struct gallivm_state
*gallivm
,
265 const struct tgsi_token
*tokens
,
267 const unsigned char swizzles
[4],
268 LLVMValueRef consts_ptr
,
269 const LLVMValueRef
*inputs
,
270 LLVMValueRef
*outputs
,
271 const struct lp_build_sampler_aos
*sampler
,
272 const struct tgsi_shader_info
*info
);
275 enum lp_exec_mask_break_type
{
276 LP_EXEC_MASK_BREAK_TYPE_LOOP
,
277 LP_EXEC_MASK_BREAK_TYPE_SWITCH
281 struct lp_exec_mask
{
282 struct lp_build_context
*bld
;
287 LLVMTypeRef int_vec_type
;
289 LLVMValueRef exec_mask
;
291 LLVMValueRef ret_mask
;
292 LLVMValueRef cond_mask
;
293 LLVMValueRef switch_mask
; /* current switch exec mask */
294 LLVMValueRef cont_mask
;
295 LLVMValueRef break_mask
;
297 struct function_ctx
{
299 LLVMValueRef ret_mask
;
301 LLVMValueRef cond_stack
[LP_MAX_TGSI_NESTING
];
304 /* keep track if break belongs to switch or loop */
305 enum lp_exec_mask_break_type break_type_stack
[LP_MAX_TGSI_NESTING
];
306 enum lp_exec_mask_break_type break_type
;
309 LLVMValueRef switch_val
;
310 LLVMValueRef switch_mask
;
311 LLVMValueRef switch_mask_default
;
312 boolean switch_in_default
;
314 } switch_stack
[LP_MAX_TGSI_NESTING
];
315 int switch_stack_size
;
316 LLVMValueRef switch_val
;
317 LLVMValueRef switch_mask_default
; /* reverse of switch mask used for default */
318 boolean switch_in_default
; /* if switch exec is currently in default */
319 unsigned switch_pc
; /* when used points to default or endswitch-1 */
321 LLVMValueRef loop_limiter
;
322 LLVMBasicBlockRef loop_block
;
323 LLVMValueRef break_var
;
325 LLVMBasicBlockRef loop_block
;
326 LLVMValueRef cont_mask
;
327 LLVMValueRef break_mask
;
328 LLVMValueRef break_var
;
329 } loop_stack
[LP_MAX_TGSI_NESTING
];
333 int function_stack_size
;
336 struct lp_build_tgsi_inst_list
338 struct tgsi_full_instruction
*instructions
;
339 uint max_instructions
;
340 uint num_instructions
;
343 unsigned lp_bld_tgsi_list_init(struct lp_build_tgsi_context
* bld_base
);
346 unsigned lp_bld_tgsi_add_instruction(
347 struct lp_build_tgsi_context
* bld_base
,
348 const struct tgsi_full_instruction
*inst_to_add
);
351 struct lp_build_tgsi_context
;
354 typedef LLVMValueRef (*lp_build_emit_fetch_fn
)(struct lp_build_tgsi_context
*,
355 const struct tgsi_full_src_register
*,
356 enum tgsi_opcode_type
,
359 struct lp_build_tgsi_context
361 struct lp_build_context base
;
363 struct lp_build_context uint_bld
;
364 struct lp_build_context int_bld
;
366 struct lp_build_context dbl_bld
;
368 struct lp_build_context uint64_bld
;
369 struct lp_build_context int64_bld
;
371 /** This array stores functions that are used to transform TGSI opcodes to
374 struct lp_build_tgsi_action op_actions
[TGSI_OPCODE_LAST
];
376 /* TGSI_OPCODE_RSQ is defined as 1 / sqrt( abs(src0.x) ), rsq_action
377 * should compute 1 / sqrt (src0.x) */
378 struct lp_build_tgsi_action rsq_action
;
380 struct lp_build_tgsi_action sqrt_action
;
382 struct lp_build_tgsi_action drsq_action
;
384 struct lp_build_tgsi_action dsqrt_action
;
385 const struct tgsi_shader_info
*info
;
387 lp_build_emit_fetch_fn emit_fetch_funcs
[TGSI_FILE_COUNT
];
389 LLVMValueRef (*emit_swizzle
)(struct lp_build_tgsi_context
*,
390 LLVMValueRef
, unsigned, unsigned, unsigned, unsigned);
393 void (*emit_debug
)(struct lp_build_tgsi_context
*,
394 const struct tgsi_full_instruction
*,
395 const struct tgsi_opcode_info
*);
397 void (*emit_store
)(struct lp_build_tgsi_context
*,
398 const struct tgsi_full_instruction
*,
399 const struct tgsi_opcode_info
*,
401 LLVMValueRef dst
[4]);
403 void (*emit_declaration
)(struct lp_build_tgsi_context
*,
404 const struct tgsi_full_declaration
*decl
);
406 void (*emit_immediate
)(struct lp_build_tgsi_context
*,
407 const struct tgsi_full_immediate
*imm
);
410 /* Allow the user to store data in this structure rather than passing it
411 * to every function. */
418 struct tgsi_full_instruction
*instructions
;
419 uint max_instructions
;
420 uint num_instructions
;
422 /** This function allows the user to insert some instructions at the
423 * beginning of the program. It is optional and does not need to be
426 void (*emit_prologue
)(struct lp_build_tgsi_context
*);
428 /** This function allows the user to insert some instructions at the end of
429 * the program. This callback is intended to be used for emitting
430 * instructions to handle the export for the output registers, but it can
431 * be used for any purpose. Implementing this function is optiona, but
434 void (*emit_epilogue
)(struct lp_build_tgsi_context
*);
437 struct lp_build_gs_iface
439 LLVMValueRef (*fetch_input
)(const struct lp_build_gs_iface
*gs_iface
,
440 struct lp_build_context
* bld
,
441 boolean is_vindex_indirect
,
442 LLVMValueRef vertex_index
,
443 boolean is_aindex_indirect
,
444 LLVMValueRef attrib_index
,
445 LLVMValueRef swizzle_index
);
446 void (*emit_vertex
)(const struct lp_build_gs_iface
*gs_iface
,
447 struct lp_build_context
* bld
,
448 LLVMValueRef (*outputs
)[4],
449 LLVMValueRef emitted_vertices_vec
,
450 LLVMValueRef stream_id
);
451 void (*end_primitive
)(const struct lp_build_gs_iface
*gs_iface
,
452 struct lp_build_context
* bld
,
453 LLVMValueRef total_emitted_vertices_vec
,
454 LLVMValueRef verts_per_prim_vec
,
455 LLVMValueRef emitted_prims_vec
,
456 LLVMValueRef mask_vec
);
457 void (*gs_epilogue
)(const struct lp_build_gs_iface
*gs_iface
,
458 LLVMValueRef total_emitted_vertices_vec
,
459 LLVMValueRef emitted_prims_vec
);
462 struct lp_build_tgsi_soa_context
464 struct lp_build_tgsi_context bld_base
;
466 /* Builder for scalar elements of shader's data type (float) */
467 struct lp_build_context elem_bld
;
469 const struct lp_build_gs_iface
*gs_iface
;
470 LLVMValueRef emitted_prims_vec_ptr
;
471 LLVMValueRef total_emitted_vertices_vec_ptr
;
472 LLVMValueRef emitted_vertices_vec_ptr
;
473 LLVMValueRef max_output_vertices_vec
;
475 LLVMValueRef consts_ptr
;
476 LLVMValueRef const_sizes_ptr
;
477 LLVMValueRef consts
[LP_MAX_TGSI_CONST_BUFFERS
];
478 LLVMValueRef consts_sizes
[LP_MAX_TGSI_CONST_BUFFERS
];
479 const LLVMValueRef (*inputs
)[TGSI_NUM_CHANNELS
];
480 LLVMValueRef (*outputs
)[TGSI_NUM_CHANNELS
];
481 LLVMValueRef context_ptr
;
482 LLVMValueRef thread_data_ptr
;
484 LLVMValueRef ssbo_ptr
;
485 LLVMValueRef ssbo_sizes_ptr
;
486 LLVMValueRef ssbos
[LP_MAX_TGSI_SHADER_BUFFERS
];
487 LLVMValueRef ssbo_sizes
[LP_MAX_TGSI_SHADER_BUFFERS
];
489 LLVMValueRef shared_ptr
;
491 const struct lp_build_coro_suspend_info
*coro
;
493 const struct lp_build_sampler_soa
*sampler
;
494 const struct lp_build_image_soa
*image
;
496 struct tgsi_declaration_sampler_view sv
[PIPE_MAX_SHADER_SAMPLER_VIEWS
];
498 LLVMValueRef immediates
[LP_MAX_INLINED_IMMEDIATES
][TGSI_NUM_CHANNELS
];
499 LLVMValueRef temps
[LP_MAX_INLINED_TEMPS
][TGSI_NUM_CHANNELS
];
500 LLVMValueRef addr
[LP_MAX_TGSI_ADDRS
][TGSI_NUM_CHANNELS
];
502 /* We allocate/use this array of temps if (1 << TGSI_FILE_TEMPORARY) is
503 * set in the indirect_files field.
504 * The temps[] array above is unused then.
506 LLVMValueRef temps_array
;
508 /* We allocate/use this array of output if (1 << TGSI_FILE_OUTPUT) is
509 * set in the indirect_files field.
510 * The outputs[] array above is unused then.
512 LLVMValueRef outputs_array
;
514 /* We allocate/use this array of inputs if (1 << TGSI_FILE_INPUT) is
515 * set in the indirect_files field.
516 * The inputs[] array above is unused then.
518 LLVMValueRef inputs_array
;
520 /* We allocate/use this array of temps if (1 << TGSI_FILE_IMMEDIATE) is
521 * set in the indirect_files field.
523 LLVMValueRef imms_array
;
526 struct lp_bld_tgsi_system_values system_values
;
528 /** bitmask indicating which register files are accessed indirectly */
529 unsigned indirect_files
;
531 struct lp_build_mask_context
*mask
;
532 struct lp_exec_mask exec_mask
;
535 boolean use_immediates_array
;
539 lp_emit_declaration_soa(
540 struct lp_build_tgsi_context
*bld
,
541 const struct tgsi_full_declaration
*decl
);
543 void lp_emit_immediate_soa(
544 struct lp_build_tgsi_context
*bld_base
,
545 const struct tgsi_full_immediate
*imm
);
548 lp_emit_instruction_soa(
549 struct lp_build_tgsi_soa_context
*bld
,
550 const struct tgsi_full_instruction
*inst
,
551 const struct tgsi_opcode_info
*info
);
556 struct lp_build_tgsi_soa_context
*bld
,
562 struct lp_build_tgsi_soa_context
*bld
,
566 struct lp_build_tgsi_aos_context
568 struct lp_build_tgsi_context bld_base
;
570 /* Builder for integer masks and indices */
571 struct lp_build_context int_bld
;
575 * - swizzles[0] = red index
576 * - swizzles[1] = green index
577 * - swizzles[2] = blue index
578 * - swizzles[3] = alpha index
580 unsigned char swizzles
[4];
581 unsigned char inv_swizzles
[4];
583 LLVMValueRef consts_ptr
;
584 const LLVMValueRef
*inputs
;
585 LLVMValueRef
*outputs
;
587 const struct lp_build_sampler_aos
*sampler
;
589 struct tgsi_declaration_sampler_view sv
[PIPE_MAX_SHADER_SAMPLER_VIEWS
];
591 LLVMValueRef immediates
[LP_MAX_INLINED_IMMEDIATES
];
592 LLVMValueRef temps
[LP_MAX_INLINED_TEMPS
];
593 LLVMValueRef addr
[LP_MAX_TGSI_ADDRS
];
595 /* We allocate/use this array of temps if (1 << TGSI_FILE_TEMPORARY) is
596 * set in the indirect_files field.
597 * The temps[] array above is unused then.
599 LLVMValueRef temps_array
;
601 /** bitmask indicating which register files are accessed indirectly */
602 unsigned indirect_files
;
606 static inline struct lp_build_tgsi_soa_context
*
607 lp_soa_context(struct lp_build_tgsi_context
*bld_base
)
609 return (struct lp_build_tgsi_soa_context
*)bld_base
;
612 static inline struct lp_build_tgsi_aos_context
*
613 lp_aos_context(struct lp_build_tgsi_context
*bld_base
)
615 return (struct lp_build_tgsi_aos_context
*)bld_base
;
619 lp_emit_declaration_aos(
620 struct lp_build_tgsi_aos_context
*bld
,
621 const struct tgsi_full_declaration
*decl
);
625 lp_emit_instruction_aos(
626 struct lp_build_tgsi_aos_context
*bld
,
627 const struct tgsi_full_instruction
*inst
,
628 const struct tgsi_opcode_info
*info
,
633 struct lp_build_tgsi_aos_context
*bld
,
634 const struct tgsi_full_instruction
*inst
,
638 void lp_build_fetch_args(
639 struct lp_build_tgsi_context
* bld_base
,
640 struct lp_build_emit_data
* emit_data
);
643 lp_build_tgsi_inst_llvm_aos(
644 struct lp_build_tgsi_context
* bld_base
,
645 const struct tgsi_full_instruction
*inst
);
648 lp_build_tgsi_intrinsic(
649 const struct lp_build_tgsi_action
* action
,
650 struct lp_build_tgsi_context
* bld_base
,
651 struct lp_build_emit_data
* emit_data
);
655 struct lp_build_tgsi_context
*bld_base
,
656 unsigned tgsi_opcode
,
657 struct lp_build_emit_data
* emit_data
);
660 lp_build_emit_llvm_unary(
661 struct lp_build_tgsi_context
*bld_base
,
662 unsigned tgsi_opcode
,
666 lp_build_emit_llvm_binary(
667 struct lp_build_tgsi_context
*bld_base
,
668 unsigned tgsi_opcode
,
673 lp_build_emit_llvm_ternary(
674 struct lp_build_tgsi_context
*bld_base
,
675 unsigned tgsi_opcode
,
681 lp_build_tgsi_inst_llvm(
682 struct lp_build_tgsi_context
* bld_base
,
683 const struct tgsi_full_instruction
*inst
);
686 lp_build_emit_fetch_src(
687 struct lp_build_tgsi_context
*bld_base
,
688 const struct tgsi_full_src_register
*reg
,
689 enum tgsi_opcode_type stype
,
690 const unsigned chan_index
);
694 struct lp_build_tgsi_context
*bld_base
,
695 const struct tgsi_full_instruction
*inst
,
697 const unsigned chan_index
);
701 lp_build_emit_fetch_texoffset(
702 struct lp_build_tgsi_context
*bld_base
,
703 const struct tgsi_full_instruction
*inst
,
705 const unsigned chan_index
);
709 struct lp_build_tgsi_context
* bld_base
,
710 const struct tgsi_token
*tokens
);
716 #endif /* LP_BLD_TGSI_H */