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_tgsi_gs_iface
;
72 enum lp_build_tex_modifier
{
73 LP_BLD_TEX_MODIFIER_NONE
= 0,
74 LP_BLD_TEX_MODIFIER_PROJECTED
,
75 LP_BLD_TEX_MODIFIER_LOD_BIAS
,
76 LP_BLD_TEX_MODIFIER_EXPLICIT_LOD
,
77 LP_BLD_TEX_MODIFIER_EXPLICIT_DERIV
,
78 LP_BLD_TEX_MODIFIER_LOD_ZERO
83 * Describe a channel of a register.
86 * - immediate value (i.e. derived from a IMM register)
89 * - undetermined (when .file == TGSI_FILE_NULL)
91 * This is one of the analysis results, and is used to described
92 * the output color in terms of inputs.
94 struct lp_tgsi_channel_info
96 unsigned file
:4; /* TGSI_FILE_* */
97 unsigned swizzle
:3; /* PIPE_SWIZZLE_x */
100 float value
; /* for TGSI_FILE_IMMEDIATE */
106 * Describe a texture sampler interpolator.
108 * The interpolation is described in terms of regular inputs.
110 struct lp_tgsi_texture_info
112 struct lp_tgsi_channel_info coord
[4];
113 unsigned target
:8; /* TGSI_TEXTURE_* */
114 unsigned sampler_unit
:8; /* Sampler unit */
115 unsigned texture_unit
:8; /* Texture unit */
116 unsigned modifier
:8; /* LP_BLD_TEX_MODIFIER_* */
122 struct tgsi_shader_info base
;
125 * Whether any of the texture opcodes access a register file other than
128 * We could also handle TGSI_FILE_CONST/IMMEDIATE here, but there is little
131 unsigned indirect_textures
:1;
134 * Whether any of the texture (sample) ocpodes use different sampler
135 * and sampler view unit.
137 unsigned sampler_texture_units_different
:1;
140 * Whether any immediate values are outside the range of 0 and 1
142 unsigned unclamped_immediates
:1;
145 * Texture opcode description. Aimed at detecting and described direct
149 struct lp_tgsi_texture_info tex
[PIPE_MAX_SAMPLERS
];
152 * Output description. Aimed at detecting and describing simple blit
155 struct lp_tgsi_channel_info output
[PIPE_MAX_SHADER_OUTPUTS
][4];
158 * Shortcut pointers into the above (for fragment shaders).
160 const struct lp_tgsi_channel_info
*cbuf
[PIPE_MAX_COLOR_BUFS
];
164 * Reference to system values.
166 struct lp_bld_tgsi_system_values
{
167 LLVMValueRef instance_id
;
168 LLVMValueRef vertex_id
;
169 LLVMValueRef vertex_id_nobase
;
170 LLVMValueRef prim_id
;
171 LLVMValueRef basevertex
;
172 LLVMValueRef invocation_id
;
177 * Sampler code generation interface.
179 * Although texture sampling is a requirement for TGSI translation, it is
180 * a very different problem with several different approaches to it. This
181 * structure establishes an interface for texture sampling code generation, so
182 * that we can easily use different texture sampling strategies.
184 struct lp_build_sampler_soa
187 (*destroy
)( struct lp_build_sampler_soa
*sampler
);
190 (*emit_tex_sample
)(const struct lp_build_sampler_soa
*sampler
,
191 struct gallivm_state
*gallivm
,
192 const struct lp_sampler_params
*params
);
195 (*emit_size_query
)( const struct lp_build_sampler_soa
*sampler
,
196 struct gallivm_state
*gallivm
,
197 const struct lp_sampler_size_query_params
*params
);
201 struct lp_build_sampler_aos
204 (*emit_fetch_texel
)( struct lp_build_sampler_aos
*sampler
,
205 struct lp_build_context
*bld
,
206 unsigned target
, /* TGSI_TEXTURE_* */
209 const struct lp_derivatives derivs
,
210 enum lp_build_tex_modifier modifier
);
215 lp_build_tgsi_info(const struct tgsi_token
*tokens
,
216 struct lp_tgsi_info
*info
);
220 lp_build_tgsi_soa(struct gallivm_state
*gallivm
,
221 const struct tgsi_token
*tokens
,
223 struct lp_build_mask_context
*mask
,
224 LLVMValueRef consts_ptr
,
225 LLVMValueRef const_sizes_ptr
,
226 const struct lp_bld_tgsi_system_values
*system_values
,
227 const LLVMValueRef (*inputs
)[4],
228 LLVMValueRef (*outputs
)[4],
229 LLVMValueRef context_ptr
,
230 LLVMValueRef thread_data_ptr
,
231 struct lp_build_sampler_soa
*sampler
,
232 const struct tgsi_shader_info
*info
,
233 const struct lp_build_tgsi_gs_iface
*gs_iface
);
237 lp_build_tgsi_aos(struct gallivm_state
*gallivm
,
238 const struct tgsi_token
*tokens
,
240 const unsigned char swizzles
[4],
241 LLVMValueRef consts_ptr
,
242 const LLVMValueRef
*inputs
,
243 LLVMValueRef
*outputs
,
244 struct lp_build_sampler_aos
*sampler
,
245 const struct tgsi_shader_info
*info
);
248 enum lp_exec_mask_break_type
{
249 LP_EXEC_MASK_BREAK_TYPE_LOOP
,
250 LP_EXEC_MASK_BREAK_TYPE_SWITCH
254 struct lp_exec_mask
{
255 struct lp_build_context
*bld
;
260 LLVMTypeRef int_vec_type
;
262 LLVMValueRef exec_mask
;
264 LLVMValueRef ret_mask
;
265 LLVMValueRef cond_mask
;
266 LLVMValueRef switch_mask
; /* current switch exec mask */
267 LLVMValueRef cont_mask
;
268 LLVMValueRef break_mask
;
270 struct function_ctx
{
272 LLVMValueRef ret_mask
;
274 LLVMValueRef cond_stack
[LP_MAX_TGSI_NESTING
];
277 /* keep track if break belongs to switch or loop */
278 enum lp_exec_mask_break_type break_type_stack
[LP_MAX_TGSI_NESTING
];
279 enum lp_exec_mask_break_type break_type
;
282 LLVMValueRef switch_val
;
283 LLVMValueRef switch_mask
;
284 LLVMValueRef switch_mask_default
;
285 boolean switch_in_default
;
287 } switch_stack
[LP_MAX_TGSI_NESTING
];
288 int switch_stack_size
;
289 LLVMValueRef switch_val
;
290 LLVMValueRef switch_mask_default
; /* reverse of switch mask used for default */
291 boolean switch_in_default
; /* if switch exec is currently in default */
292 unsigned switch_pc
; /* when used points to default or endswitch-1 */
294 LLVMValueRef loop_limiter
;
295 LLVMBasicBlockRef loop_block
;
296 LLVMValueRef break_var
;
298 LLVMBasicBlockRef loop_block
;
299 LLVMValueRef cont_mask
;
300 LLVMValueRef break_mask
;
301 LLVMValueRef break_var
;
302 } loop_stack
[LP_MAX_TGSI_NESTING
];
306 int function_stack_size
;
309 struct lp_build_tgsi_inst_list
311 struct tgsi_full_instruction
*instructions
;
312 uint max_instructions
;
313 uint num_instructions
;
316 unsigned lp_bld_tgsi_list_init(struct lp_build_tgsi_context
* bld_base
);
319 unsigned lp_bld_tgsi_add_instruction(
320 struct lp_build_tgsi_context
* bld_base
,
321 const struct tgsi_full_instruction
*inst_to_add
);
324 struct lp_build_tgsi_context
;
327 typedef LLVMValueRef (*lp_build_emit_fetch_fn
)(struct lp_build_tgsi_context
*,
328 const struct tgsi_full_src_register
*,
329 enum tgsi_opcode_type
,
332 struct lp_build_tgsi_context
334 struct lp_build_context base
;
336 struct lp_build_context uint_bld
;
337 struct lp_build_context int_bld
;
339 struct lp_build_context dbl_bld
;
340 /** This array stores functions that are used to transform TGSI opcodes to
343 struct lp_build_tgsi_action op_actions
[TGSI_OPCODE_LAST
];
345 /* TGSI_OPCODE_RSQ is defined as 1 / sqrt( abs(src0.x) ), rsq_action
346 * should compute 1 / sqrt (src0.x) */
347 struct lp_build_tgsi_action rsq_action
;
349 struct lp_build_tgsi_action sqrt_action
;
351 struct lp_build_tgsi_action drsq_action
;
353 struct lp_build_tgsi_action dsqrt_action
;
354 const struct tgsi_shader_info
*info
;
356 lp_build_emit_fetch_fn emit_fetch_funcs
[TGSI_FILE_COUNT
];
358 LLVMValueRef (*emit_swizzle
)(struct lp_build_tgsi_context
*,
359 LLVMValueRef
, unsigned, unsigned, unsigned, unsigned);
362 void (*emit_debug
)(struct lp_build_tgsi_context
*,
363 const struct tgsi_full_instruction
*,
364 const struct tgsi_opcode_info
*);
366 void (*emit_store
)(struct lp_build_tgsi_context
*,
367 const struct tgsi_full_instruction
*,
368 const struct tgsi_opcode_info
*,
369 LLVMValueRef dst
[4]);
371 void (*emit_declaration
)(struct lp_build_tgsi_context
*,
372 const struct tgsi_full_declaration
*decl
);
374 void (*emit_immediate
)(struct lp_build_tgsi_context
*,
375 const struct tgsi_full_immediate
*imm
);
378 /* Allow the user to store data in this structure rather than passing it
379 * to every function. */
386 struct tgsi_full_instruction
*instructions
;
387 uint max_instructions
;
388 uint num_instructions
;
390 /** This function allows the user to insert some instructions at the
391 * beginning of the program. It is optional and does not need to be
394 void (*emit_prologue
)(struct lp_build_tgsi_context
*);
396 /** This function allows the user to insert some instructions at the end of
397 * the program. This callback is intended to be used for emitting
398 * instructions to handle the export for the output registers, but it can
399 * be used for any purpose. Implementing this function is optiona, but
402 void (*emit_epilogue
)(struct lp_build_tgsi_context
*);
405 struct lp_build_tgsi_gs_iface
407 LLVMValueRef (*fetch_input
)(const struct lp_build_tgsi_gs_iface
*gs_iface
,
408 struct lp_build_tgsi_context
* bld_base
,
409 boolean is_vindex_indirect
,
410 LLVMValueRef vertex_index
,
411 boolean is_aindex_indirect
,
412 LLVMValueRef attrib_index
,
413 LLVMValueRef swizzle_index
);
414 void (*emit_vertex
)(const struct lp_build_tgsi_gs_iface
*gs_iface
,
415 struct lp_build_tgsi_context
* bld_base
,
416 LLVMValueRef (*outputs
)[4],
417 LLVMValueRef emitted_vertices_vec
);
418 void (*end_primitive
)(const struct lp_build_tgsi_gs_iface
*gs_iface
,
419 struct lp_build_tgsi_context
* bld_base
,
420 LLVMValueRef verts_per_prim_vec
,
421 LLVMValueRef emitted_prims_vec
);
422 void (*gs_epilogue
)(const struct lp_build_tgsi_gs_iface
*gs_iface
,
423 struct lp_build_tgsi_context
* bld_base
,
424 LLVMValueRef total_emitted_vertices_vec
,
425 LLVMValueRef emitted_prims_vec
);
428 struct lp_build_tgsi_soa_context
430 struct lp_build_tgsi_context bld_base
;
432 /* Builder for scalar elements of shader's data type (float) */
433 struct lp_build_context elem_bld
;
435 const struct lp_build_tgsi_gs_iface
*gs_iface
;
436 LLVMValueRef emitted_prims_vec_ptr
;
437 LLVMValueRef total_emitted_vertices_vec_ptr
;
438 LLVMValueRef emitted_vertices_vec_ptr
;
439 LLVMValueRef max_output_vertices_vec
;
441 LLVMValueRef consts_ptr
;
442 LLVMValueRef const_sizes_ptr
;
443 LLVMValueRef consts
[LP_MAX_TGSI_CONST_BUFFERS
];
444 LLVMValueRef consts_sizes
[LP_MAX_TGSI_CONST_BUFFERS
];
445 const LLVMValueRef (*inputs
)[TGSI_NUM_CHANNELS
];
446 LLVMValueRef (*outputs
)[TGSI_NUM_CHANNELS
];
447 LLVMValueRef context_ptr
;
448 LLVMValueRef thread_data_ptr
;
450 const struct lp_build_sampler_soa
*sampler
;
452 struct tgsi_declaration_sampler_view sv
[PIPE_MAX_SHADER_SAMPLER_VIEWS
];
454 LLVMValueRef immediates
[LP_MAX_INLINED_IMMEDIATES
][TGSI_NUM_CHANNELS
];
455 LLVMValueRef temps
[LP_MAX_INLINED_TEMPS
][TGSI_NUM_CHANNELS
];
456 LLVMValueRef addr
[LP_MAX_TGSI_ADDRS
][TGSI_NUM_CHANNELS
];
457 LLVMValueRef preds
[LP_MAX_TGSI_PREDS
][TGSI_NUM_CHANNELS
];
459 /* We allocate/use this array of temps if (1 << TGSI_FILE_TEMPORARY) is
460 * set in the indirect_files field.
461 * The temps[] array above is unused then.
463 LLVMValueRef temps_array
;
465 /* We allocate/use this array of output if (1 << TGSI_FILE_OUTPUT) is
466 * set in the indirect_files field.
467 * The outputs[] array above is unused then.
469 LLVMValueRef outputs_array
;
471 /* We allocate/use this array of inputs if (1 << TGSI_FILE_INPUT) is
472 * set in the indirect_files field.
473 * The inputs[] array above is unused then.
475 LLVMValueRef inputs_array
;
477 /* We allocate/use this array of temps if (1 << TGSI_FILE_IMMEDIATE) is
478 * set in the indirect_files field.
480 LLVMValueRef imms_array
;
483 struct lp_bld_tgsi_system_values system_values
;
485 /** bitmask indicating which register files are accessed indirectly */
486 unsigned indirect_files
;
488 struct lp_build_mask_context
*mask
;
489 struct lp_exec_mask exec_mask
;
492 boolean use_immediates_array
;
496 lp_emit_declaration_soa(
497 struct lp_build_tgsi_context
*bld
,
498 const struct tgsi_full_declaration
*decl
);
500 void lp_emit_immediate_soa(
501 struct lp_build_tgsi_context
*bld_base
,
502 const struct tgsi_full_immediate
*imm
);
505 lp_emit_instruction_soa(
506 struct lp_build_tgsi_soa_context
*bld
,
507 const struct tgsi_full_instruction
*inst
,
508 const struct tgsi_opcode_info
*info
);
513 struct lp_build_tgsi_soa_context
*bld
,
519 struct lp_build_tgsi_soa_context
*bld
,
523 struct lp_build_tgsi_aos_context
525 struct lp_build_tgsi_context bld_base
;
527 /* Builder for integer masks and indices */
528 struct lp_build_context int_bld
;
532 * - swizzles[0] = red index
533 * - swizzles[1] = green index
534 * - swizzles[2] = blue index
535 * - swizzles[3] = alpha index
537 unsigned char swizzles
[4];
538 unsigned char inv_swizzles
[4];
540 LLVMValueRef consts_ptr
;
541 const LLVMValueRef
*inputs
;
542 LLVMValueRef
*outputs
;
544 struct lp_build_sampler_aos
*sampler
;
546 struct tgsi_declaration_sampler_view sv
[PIPE_MAX_SHADER_SAMPLER_VIEWS
];
548 LLVMValueRef immediates
[LP_MAX_INLINED_IMMEDIATES
];
549 LLVMValueRef temps
[LP_MAX_INLINED_TEMPS
];
550 LLVMValueRef addr
[LP_MAX_TGSI_ADDRS
];
551 LLVMValueRef preds
[LP_MAX_TGSI_PREDS
];
553 /* We allocate/use this array of temps if (1 << TGSI_FILE_TEMPORARY) is
554 * set in the indirect_files field.
555 * The temps[] array above is unused then.
557 LLVMValueRef temps_array
;
559 /** bitmask indicating which register files are accessed indirectly */
560 unsigned indirect_files
;
564 static inline struct lp_build_tgsi_soa_context
*
565 lp_soa_context(struct lp_build_tgsi_context
*bld_base
)
567 return (struct lp_build_tgsi_soa_context
*)bld_base
;
570 static inline struct lp_build_tgsi_aos_context
*
571 lp_aos_context(struct lp_build_tgsi_context
*bld_base
)
573 return (struct lp_build_tgsi_aos_context
*)bld_base
;
577 lp_emit_declaration_aos(
578 struct lp_build_tgsi_aos_context
*bld
,
579 const struct tgsi_full_declaration
*decl
);
583 lp_emit_instruction_aos(
584 struct lp_build_tgsi_aos_context
*bld
,
585 const struct tgsi_full_instruction
*inst
,
586 const struct tgsi_opcode_info
*info
,
591 struct lp_build_tgsi_aos_context
*bld
,
592 const struct tgsi_full_instruction
*inst
,
596 void lp_build_fetch_args(
597 struct lp_build_tgsi_context
* bld_base
,
598 struct lp_build_emit_data
* emit_data
);
601 lp_build_tgsi_inst_llvm_aos(
602 struct lp_build_tgsi_context
* bld_base
,
603 const struct tgsi_full_instruction
*inst
);
606 lp_build_tgsi_intrinsic(
607 const struct lp_build_tgsi_action
* action
,
608 struct lp_build_tgsi_context
* bld_base
,
609 struct lp_build_emit_data
* emit_data
);
613 struct lp_build_tgsi_context
*bld_base
,
614 unsigned tgsi_opcode
,
615 struct lp_build_emit_data
* emit_data
);
618 lp_build_emit_llvm_unary(
619 struct lp_build_tgsi_context
*bld_base
,
620 unsigned tgsi_opcode
,
624 lp_build_emit_llvm_binary(
625 struct lp_build_tgsi_context
*bld_base
,
626 unsigned tgsi_opcode
,
631 lp_build_emit_llvm_ternary(
632 struct lp_build_tgsi_context
*bld_base
,
633 unsigned tgsi_opcode
,
639 lp_build_tgsi_inst_llvm(
640 struct lp_build_tgsi_context
* bld_base
,
641 const struct tgsi_full_instruction
*inst
);
645 struct lp_build_tgsi_context
*bld_base
,
646 const struct tgsi_full_instruction
*inst
,
648 const unsigned chan_index
);
652 lp_build_emit_fetch_texoffset(
653 struct lp_build_tgsi_context
*bld_base
,
654 const struct tgsi_full_instruction
*inst
,
656 const unsigned chan_index
);
660 struct lp_build_tgsi_context
* bld_base
,
661 const struct tgsi_token
*tokens
);
667 #endif /* LP_BLD_TGSI_H */