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 void (*end_primitive
)(const struct lp_build_gs_iface
*gs_iface
,
451 struct lp_build_context
* bld
,
452 LLVMValueRef total_emitted_vertices_vec
,
453 LLVMValueRef verts_per_prim_vec
,
454 LLVMValueRef emitted_prims_vec
,
455 LLVMValueRef mask_vec
);
456 void (*gs_epilogue
)(const struct lp_build_gs_iface
*gs_iface
,
457 LLVMValueRef total_emitted_vertices_vec
,
458 LLVMValueRef emitted_prims_vec
);
461 struct lp_build_tgsi_soa_context
463 struct lp_build_tgsi_context bld_base
;
465 /* Builder for scalar elements of shader's data type (float) */
466 struct lp_build_context elem_bld
;
468 const struct lp_build_gs_iface
*gs_iface
;
469 LLVMValueRef emitted_prims_vec_ptr
;
470 LLVMValueRef total_emitted_vertices_vec_ptr
;
471 LLVMValueRef emitted_vertices_vec_ptr
;
472 LLVMValueRef max_output_vertices_vec
;
474 LLVMValueRef consts_ptr
;
475 LLVMValueRef const_sizes_ptr
;
476 LLVMValueRef consts
[LP_MAX_TGSI_CONST_BUFFERS
];
477 LLVMValueRef consts_sizes
[LP_MAX_TGSI_CONST_BUFFERS
];
478 const LLVMValueRef (*inputs
)[TGSI_NUM_CHANNELS
];
479 LLVMValueRef (*outputs
)[TGSI_NUM_CHANNELS
];
480 LLVMValueRef context_ptr
;
481 LLVMValueRef thread_data_ptr
;
483 LLVMValueRef ssbo_ptr
;
484 LLVMValueRef ssbo_sizes_ptr
;
485 LLVMValueRef ssbos
[LP_MAX_TGSI_SHADER_BUFFERS
];
486 LLVMValueRef ssbo_sizes
[LP_MAX_TGSI_SHADER_BUFFERS
];
488 LLVMValueRef shared_ptr
;
490 const struct lp_build_coro_suspend_info
*coro
;
492 const struct lp_build_sampler_soa
*sampler
;
493 const struct lp_build_image_soa
*image
;
495 struct tgsi_declaration_sampler_view sv
[PIPE_MAX_SHADER_SAMPLER_VIEWS
];
497 LLVMValueRef immediates
[LP_MAX_INLINED_IMMEDIATES
][TGSI_NUM_CHANNELS
];
498 LLVMValueRef temps
[LP_MAX_INLINED_TEMPS
][TGSI_NUM_CHANNELS
];
499 LLVMValueRef addr
[LP_MAX_TGSI_ADDRS
][TGSI_NUM_CHANNELS
];
501 /* We allocate/use this array of temps if (1 << TGSI_FILE_TEMPORARY) is
502 * set in the indirect_files field.
503 * The temps[] array above is unused then.
505 LLVMValueRef temps_array
;
507 /* We allocate/use this array of output if (1 << TGSI_FILE_OUTPUT) is
508 * set in the indirect_files field.
509 * The outputs[] array above is unused then.
511 LLVMValueRef outputs_array
;
513 /* We allocate/use this array of inputs if (1 << TGSI_FILE_INPUT) is
514 * set in the indirect_files field.
515 * The inputs[] array above is unused then.
517 LLVMValueRef inputs_array
;
519 /* We allocate/use this array of temps if (1 << TGSI_FILE_IMMEDIATE) is
520 * set in the indirect_files field.
522 LLVMValueRef imms_array
;
525 struct lp_bld_tgsi_system_values system_values
;
527 /** bitmask indicating which register files are accessed indirectly */
528 unsigned indirect_files
;
530 struct lp_build_mask_context
*mask
;
531 struct lp_exec_mask exec_mask
;
534 boolean use_immediates_array
;
538 lp_emit_declaration_soa(
539 struct lp_build_tgsi_context
*bld
,
540 const struct tgsi_full_declaration
*decl
);
542 void lp_emit_immediate_soa(
543 struct lp_build_tgsi_context
*bld_base
,
544 const struct tgsi_full_immediate
*imm
);
547 lp_emit_instruction_soa(
548 struct lp_build_tgsi_soa_context
*bld
,
549 const struct tgsi_full_instruction
*inst
,
550 const struct tgsi_opcode_info
*info
);
555 struct lp_build_tgsi_soa_context
*bld
,
561 struct lp_build_tgsi_soa_context
*bld
,
565 struct lp_build_tgsi_aos_context
567 struct lp_build_tgsi_context bld_base
;
569 /* Builder for integer masks and indices */
570 struct lp_build_context int_bld
;
574 * - swizzles[0] = red index
575 * - swizzles[1] = green index
576 * - swizzles[2] = blue index
577 * - swizzles[3] = alpha index
579 unsigned char swizzles
[4];
580 unsigned char inv_swizzles
[4];
582 LLVMValueRef consts_ptr
;
583 const LLVMValueRef
*inputs
;
584 LLVMValueRef
*outputs
;
586 const struct lp_build_sampler_aos
*sampler
;
588 struct tgsi_declaration_sampler_view sv
[PIPE_MAX_SHADER_SAMPLER_VIEWS
];
590 LLVMValueRef immediates
[LP_MAX_INLINED_IMMEDIATES
];
591 LLVMValueRef temps
[LP_MAX_INLINED_TEMPS
];
592 LLVMValueRef addr
[LP_MAX_TGSI_ADDRS
];
594 /* We allocate/use this array of temps if (1 << TGSI_FILE_TEMPORARY) is
595 * set in the indirect_files field.
596 * The temps[] array above is unused then.
598 LLVMValueRef temps_array
;
600 /** bitmask indicating which register files are accessed indirectly */
601 unsigned indirect_files
;
605 static inline struct lp_build_tgsi_soa_context
*
606 lp_soa_context(struct lp_build_tgsi_context
*bld_base
)
608 return (struct lp_build_tgsi_soa_context
*)bld_base
;
611 static inline struct lp_build_tgsi_aos_context
*
612 lp_aos_context(struct lp_build_tgsi_context
*bld_base
)
614 return (struct lp_build_tgsi_aos_context
*)bld_base
;
618 lp_emit_declaration_aos(
619 struct lp_build_tgsi_aos_context
*bld
,
620 const struct tgsi_full_declaration
*decl
);
624 lp_emit_instruction_aos(
625 struct lp_build_tgsi_aos_context
*bld
,
626 const struct tgsi_full_instruction
*inst
,
627 const struct tgsi_opcode_info
*info
,
632 struct lp_build_tgsi_aos_context
*bld
,
633 const struct tgsi_full_instruction
*inst
,
637 void lp_build_fetch_args(
638 struct lp_build_tgsi_context
* bld_base
,
639 struct lp_build_emit_data
* emit_data
);
642 lp_build_tgsi_inst_llvm_aos(
643 struct lp_build_tgsi_context
* bld_base
,
644 const struct tgsi_full_instruction
*inst
);
647 lp_build_tgsi_intrinsic(
648 const struct lp_build_tgsi_action
* action
,
649 struct lp_build_tgsi_context
* bld_base
,
650 struct lp_build_emit_data
* emit_data
);
654 struct lp_build_tgsi_context
*bld_base
,
655 unsigned tgsi_opcode
,
656 struct lp_build_emit_data
* emit_data
);
659 lp_build_emit_llvm_unary(
660 struct lp_build_tgsi_context
*bld_base
,
661 unsigned tgsi_opcode
,
665 lp_build_emit_llvm_binary(
666 struct lp_build_tgsi_context
*bld_base
,
667 unsigned tgsi_opcode
,
672 lp_build_emit_llvm_ternary(
673 struct lp_build_tgsi_context
*bld_base
,
674 unsigned tgsi_opcode
,
680 lp_build_tgsi_inst_llvm(
681 struct lp_build_tgsi_context
* bld_base
,
682 const struct tgsi_full_instruction
*inst
);
685 lp_build_emit_fetch_src(
686 struct lp_build_tgsi_context
*bld_base
,
687 const struct tgsi_full_src_register
*reg
,
688 enum tgsi_opcode_type stype
,
689 const unsigned chan_index
);
693 struct lp_build_tgsi_context
*bld_base
,
694 const struct tgsi_full_instruction
*inst
,
696 const unsigned chan_index
);
700 lp_build_emit_fetch_texoffset(
701 struct lp_build_tgsi_context
*bld_base
,
702 const struct tgsi_full_instruction
*inst
,
704 const unsigned chan_index
);
708 struct lp_build_tgsi_context
* bld_base
,
709 const struct tgsi_token
*tokens
);
715 #endif /* LP_BLD_TGSI_H */