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"
51 #define LP_CHAN_ALL ~0
53 #define LP_MAX_INSTRUCTIONS 256
55 struct tgsi_full_declaration
;
56 struct tgsi_full_immediate
;
57 struct tgsi_full_instruction
;
58 struct tgsi_full_src_register
;
59 struct tgsi_opcode_info
;
61 struct tgsi_shader_info
;
62 struct lp_build_mask_context
;
64 struct lp_derivatives
;
65 struct lp_build_tgsi_gs_iface
;
68 enum lp_build_tex_modifier
{
69 LP_BLD_TEX_MODIFIER_NONE
= 0,
70 LP_BLD_TEX_MODIFIER_PROJECTED
,
71 LP_BLD_TEX_MODIFIER_LOD_BIAS
,
72 LP_BLD_TEX_MODIFIER_EXPLICIT_LOD
,
73 LP_BLD_TEX_MODIFIER_EXPLICIT_DERIV
,
74 LP_BLD_TEX_MODIFIER_LOD_ZERO
79 * Describe a channel of a register.
82 * - immediate value (i.e. derived from a IMM register)
85 * - undetermined (when .file == TGSI_FILE_NULL)
87 * This is one of the analysis results, and is used to described
88 * the output color in terms of inputs.
90 struct lp_tgsi_channel_info
92 unsigned file
:4; /* TGSI_FILE_* */
93 unsigned swizzle
:3; /* PIPE_SWIZZLE_x */
96 float value
; /* for TGSI_FILE_IMMEDIATE */
102 * Describe a texture sampler interpolator.
104 * The interpolation is described in terms of regular inputs.
106 struct lp_tgsi_texture_info
108 struct lp_tgsi_channel_info coord
[4];
109 unsigned target
:8; /* TGSI_TEXTURE_* */
110 unsigned sampler_unit
:8; /* Sampler unit */
111 unsigned texture_unit
:8; /* Texture unit */
112 unsigned modifier
:8; /* LP_BLD_TEX_MODIFIER_* */
118 struct tgsi_shader_info base
;
121 * Whether any of the texture opcodes access a register file other than
124 * We could also handle TGSI_FILE_CONST/IMMEDIATE here, but there is little
127 unsigned indirect_textures
:1;
130 * Whether any immediate values are outside the range of 0 and 1
132 unsigned unclamped_immediates
:1;
135 * Texture opcode description. Aimed at detecting and described direct
139 struct lp_tgsi_texture_info tex
[PIPE_MAX_SAMPLERS
];
142 * Output description. Aimed at detecting and describing simple blit
145 struct lp_tgsi_channel_info output
[PIPE_MAX_SHADER_OUTPUTS
][4];
148 * Shortcut pointers into the above (for fragment shaders).
150 const struct lp_tgsi_channel_info
*cbuf
[PIPE_MAX_COLOR_BUFS
];
154 * Reference to system values.
156 struct lp_bld_tgsi_system_values
{
157 LLVMValueRef instance_id
;
158 LLVMValueRef vertex_id
;
159 LLVMValueRef prim_id
;
164 * Sampler code generation interface.
166 * Although texture sampling is a requirement for TGSI translation, it is
167 * a very different problem with several different approaches to it. This
168 * structure establishes an interface for texture sampling code generation, so
169 * that we can easily use different texture sampling strategies.
171 struct lp_build_sampler_soa
174 (*destroy
)( struct lp_build_sampler_soa
*sampler
);
177 (*emit_fetch_texel
)( const struct lp_build_sampler_soa
*sampler
,
178 struct gallivm_state
*gallivm
,
181 unsigned texture_index
,
182 unsigned sampler_index
,
183 const LLVMValueRef
*coords
,
184 const LLVMValueRef
*offsets
,
185 const struct lp_derivatives
*derivs
,
186 LLVMValueRef lod_bias
, /* optional */
187 LLVMValueRef explicit_lod
, /* optional */
188 enum lp_sampler_lod_property
,
189 LLVMValueRef
*texel
);
192 (*emit_size_query
)( const struct lp_build_sampler_soa
*sampler
,
193 struct gallivm_state
*gallivm
,
197 boolean need_nr_mips
,
198 enum lp_sampler_lod_property
,
199 LLVMValueRef explicit_lod
, /* optional */
200 LLVMValueRef
*sizes_out
);
204 struct lp_build_sampler_aos
207 (*emit_fetch_texel
)( struct lp_build_sampler_aos
*sampler
,
208 struct lp_build_context
*bld
,
209 unsigned target
, /* TGSI_TEXTURE_* */
212 const struct lp_derivatives derivs
,
213 enum lp_build_tex_modifier modifier
);
218 lp_build_tgsi_info(const struct tgsi_token
*tokens
,
219 struct lp_tgsi_info
*info
);
223 lp_build_tgsi_soa(struct gallivm_state
*gallivm
,
224 const struct tgsi_token
*tokens
,
226 struct lp_build_mask_context
*mask
,
227 LLVMValueRef consts_ptr
,
228 LLVMValueRef const_sizes_ptr
,
229 const struct lp_bld_tgsi_system_values
*system_values
,
230 const LLVMValueRef (*inputs
)[4],
231 LLVMValueRef (*outputs
)[4],
232 struct lp_build_sampler_soa
*sampler
,
233 const struct tgsi_shader_info
*info
,
234 const struct lp_build_tgsi_gs_iface
*gs_iface
);
238 lp_build_tgsi_aos(struct gallivm_state
*gallivm
,
239 const struct tgsi_token
*tokens
,
241 const unsigned char swizzles
[4],
242 LLVMValueRef consts_ptr
,
243 const LLVMValueRef
*inputs
,
244 LLVMValueRef
*outputs
,
245 struct lp_build_sampler_aos
*sampler
,
246 const struct tgsi_shader_info
*info
);
249 enum lp_exec_mask_break_type
{
250 LP_EXEC_MASK_BREAK_TYPE_LOOP
,
251 LP_EXEC_MASK_BREAK_TYPE_SWITCH
255 struct lp_exec_mask
{
256 struct lp_build_context
*bld
;
261 LLVMTypeRef int_vec_type
;
263 LLVMValueRef cond_stack
[LP_MAX_TGSI_NESTING
];
265 LLVMValueRef cond_mask
;
267 /* keep track if break belongs to switch or loop */
268 enum lp_exec_mask_break_type break_type_stack
[LP_MAX_TGSI_NESTING
];
269 enum lp_exec_mask_break_type break_type
;
272 LLVMValueRef switch_val
;
273 LLVMValueRef switch_mask
;
274 LLVMValueRef switch_mask_default
;
275 boolean switch_in_default
;
277 } switch_stack
[LP_MAX_TGSI_NESTING
];
278 int switch_stack_size
;
279 LLVMValueRef switch_val
;
280 LLVMValueRef switch_mask
; /* current switch exec mask */
281 LLVMValueRef switch_mask_default
; /* reverse of switch mask used for default */
282 boolean switch_in_default
; /* if switch exec is currently in default */
283 unsigned switch_pc
; /* when used points to default or endswitch-1 */
285 LLVMBasicBlockRef loop_block
;
286 LLVMValueRef cont_mask
;
287 LLVMValueRef break_mask
;
288 LLVMValueRef break_var
;
290 LLVMBasicBlockRef loop_block
;
291 LLVMValueRef cont_mask
;
292 LLVMValueRef break_mask
;
293 LLVMValueRef break_var
;
294 } loop_stack
[LP_MAX_TGSI_NESTING
];
297 LLVMValueRef ret_mask
;
300 LLVMValueRef ret_mask
;
301 } call_stack
[LP_MAX_TGSI_NESTING
];
304 LLVMValueRef exec_mask
;
305 LLVMValueRef loop_limiter
;
308 struct lp_build_tgsi_inst_list
310 struct tgsi_full_instruction
*instructions
;
311 uint max_instructions
;
312 uint num_instructions
;
315 unsigned lp_bld_tgsi_list_init(struct lp_build_tgsi_context
* bld_base
);
318 unsigned lp_bld_tgsi_add_instruction(
319 struct lp_build_tgsi_context
* bld_base
,
320 struct tgsi_full_instruction
*inst_to_add
);
323 struct lp_build_tgsi_context
;
326 typedef LLVMValueRef (*lp_build_emit_fetch_fn
)(struct lp_build_tgsi_context
*,
327 const struct tgsi_full_src_register
*,
328 enum tgsi_opcode_type
,
331 struct lp_build_tgsi_context
333 struct lp_build_context base
;
335 struct lp_build_context uint_bld
;
336 struct lp_build_context int_bld
;
338 /** This array stores functions that are used to transform TGSI opcodes to
341 struct lp_build_tgsi_action op_actions
[TGSI_OPCODE_LAST
];
343 /* TGSI_OPCODE_RSQ is defined as 1 / sqrt( abs(src0.x) ), rsq_action
344 * should compute 1 / sqrt (src0.x) */
345 struct lp_build_tgsi_action rsq_action
;
347 struct lp_build_tgsi_action sqrt_action
;
349 const struct tgsi_shader_info
*info
;
351 lp_build_emit_fetch_fn emit_fetch_funcs
[TGSI_FILE_COUNT
];
353 LLVMValueRef (*emit_swizzle
)(struct lp_build_tgsi_context
*,
354 LLVMValueRef
, unsigned, unsigned, unsigned, unsigned);
357 void (*emit_debug
)(struct lp_build_tgsi_context
*,
358 const struct tgsi_full_instruction
*,
359 const struct tgsi_opcode_info
*);
361 void (*emit_store
)(struct lp_build_tgsi_context
*,
362 const struct tgsi_full_instruction
*,
363 const struct tgsi_opcode_info
*,
364 LLVMValueRef dst
[4]);
366 void (*emit_declaration
)(struct lp_build_tgsi_context
*,
367 const struct tgsi_full_declaration
*decl
);
369 void (*emit_immediate
)(struct lp_build_tgsi_context
*,
370 const struct tgsi_full_immediate
*imm
);
373 /* Allow the user to store data in this structure rather than passing it
374 * to every function. */
381 struct tgsi_full_instruction
*instructions
;
382 uint max_instructions
;
383 uint num_instructions
;
385 /** This function allows the user to insert some instructions at the
386 * beginning of the program. It is optional and does not need to be
389 void (*emit_prologue
)(struct lp_build_tgsi_context
*);
391 /** This function allows the user to insert some instructions at the end of
392 * the program. This callback is intended to be used for emitting
393 * instructions to handle the export for the output registers, but it can
394 * be used for any purpose. Implementing this function is optiona, but
397 void (*emit_epilogue
)(struct lp_build_tgsi_context
*);
400 struct lp_build_tgsi_gs_iface
402 LLVMValueRef (*fetch_input
)(const struct lp_build_tgsi_gs_iface
*gs_iface
,
403 struct lp_build_tgsi_context
* bld_base
,
404 boolean is_vindex_indirect
,
405 LLVMValueRef vertex_index
,
406 boolean is_aindex_indirect
,
407 LLVMValueRef attrib_index
,
408 LLVMValueRef swizzle_index
);
409 void (*emit_vertex
)(const struct lp_build_tgsi_gs_iface
*gs_iface
,
410 struct lp_build_tgsi_context
* bld_base
,
411 LLVMValueRef (*outputs
)[4],
412 LLVMValueRef emitted_vertices_vec
);
413 void (*end_primitive
)(const struct lp_build_tgsi_gs_iface
*gs_iface
,
414 struct lp_build_tgsi_context
* bld_base
,
415 LLVMValueRef verts_per_prim_vec
,
416 LLVMValueRef emitted_prims_vec
);
417 void (*gs_epilogue
)(const struct lp_build_tgsi_gs_iface
*gs_iface
,
418 struct lp_build_tgsi_context
* bld_base
,
419 LLVMValueRef total_emitted_vertices_vec
,
420 LLVMValueRef emitted_prims_vec
);
423 struct lp_build_tgsi_soa_context
425 struct lp_build_tgsi_context bld_base
;
427 /* Builder for scalar elements of shader's data type (float) */
428 struct lp_build_context elem_bld
;
430 const struct lp_build_tgsi_gs_iface
*gs_iface
;
431 LLVMValueRef emitted_prims_vec_ptr
;
432 LLVMValueRef total_emitted_vertices_vec_ptr
;
433 LLVMValueRef emitted_vertices_vec_ptr
;
434 LLVMValueRef max_output_vertices_vec
;
436 LLVMValueRef consts_ptr
;
437 LLVMValueRef const_sizes_ptr
;
438 const LLVMValueRef (*inputs
)[TGSI_NUM_CHANNELS
];
439 LLVMValueRef (*outputs
)[TGSI_NUM_CHANNELS
];
441 const struct lp_build_sampler_soa
*sampler
;
443 struct tgsi_declaration_sampler_view sv
[PIPE_MAX_SHADER_SAMPLER_VIEWS
];
445 LLVMValueRef immediates
[LP_MAX_TGSI_IMMEDIATES
][TGSI_NUM_CHANNELS
];
446 LLVMValueRef temps
[LP_MAX_TGSI_TEMPS
][TGSI_NUM_CHANNELS
];
447 LLVMValueRef addr
[LP_MAX_TGSI_ADDRS
][TGSI_NUM_CHANNELS
];
448 LLVMValueRef preds
[LP_MAX_TGSI_PREDS
][TGSI_NUM_CHANNELS
];
450 /* We allocate/use this array of temps if (1 << TGSI_FILE_TEMPORARY) is
451 * set in the indirect_files field.
452 * The temps[] array above is unused then.
454 LLVMValueRef temps_array
;
456 /* We allocate/use this array of output if (1 << TGSI_FILE_OUTPUT) is
457 * set in the indirect_files field.
458 * The outputs[] array above is unused then.
460 LLVMValueRef outputs_array
;
462 /* We allocate/use this array of inputs if (1 << TGSI_FILE_INPUT) is
463 * set in the indirect_files field.
464 * The inputs[] array above is unused then.
466 LLVMValueRef inputs_array
;
468 /* We allocate/use this array of temps if (1 << TGSI_FILE_IMMEDIATE) is
469 * set in the indirect_files field.
471 LLVMValueRef imms_array
;
474 struct lp_bld_tgsi_system_values system_values
;
476 /** bitmask indicating which register files are accessed indirectly */
477 unsigned indirect_files
;
479 struct lp_build_mask_context
*mask
;
480 struct lp_exec_mask exec_mask
;
487 lp_emit_declaration_soa(
488 struct lp_build_tgsi_context
*bld
,
489 const struct tgsi_full_declaration
*decl
);
491 void lp_emit_immediate_soa(
492 struct lp_build_tgsi_context
*bld_base
,
493 const struct tgsi_full_immediate
*imm
);
496 lp_emit_instruction_soa(
497 struct lp_build_tgsi_soa_context
*bld
,
498 const struct tgsi_full_instruction
*inst
,
499 const struct tgsi_opcode_info
*info
);
504 struct lp_build_tgsi_soa_context
*bld
,
510 struct lp_build_tgsi_soa_context
*bld
,
514 struct lp_build_tgsi_aos_context
516 struct lp_build_tgsi_context bld_base
;
518 /* Builder for integer masks and indices */
519 struct lp_build_context int_bld
;
523 * - swizzles[0] = red index
524 * - swizzles[1] = green index
525 * - swizzles[2] = blue index
526 * - swizzles[3] = alpha index
528 unsigned char swizzles
[4];
529 unsigned char inv_swizzles
[4];
531 LLVMValueRef consts_ptr
;
532 const LLVMValueRef
*inputs
;
533 LLVMValueRef
*outputs
;
535 struct lp_build_sampler_aos
*sampler
;
537 LLVMValueRef immediates
[LP_MAX_TGSI_IMMEDIATES
];
538 LLVMValueRef temps
[LP_MAX_TGSI_TEMPS
];
539 LLVMValueRef addr
[LP_MAX_TGSI_ADDRS
];
540 LLVMValueRef preds
[LP_MAX_TGSI_PREDS
];
542 /* We allocate/use this array of temps if (1 << TGSI_FILE_TEMPORARY) is
543 * set in the indirect_files field.
544 * The temps[] array above is unused then.
546 LLVMValueRef temps_array
;
548 /** bitmask indicating which register files are accessed indirectly */
549 unsigned indirect_files
;
553 static INLINE
struct lp_build_tgsi_soa_context
*
554 lp_soa_context(struct lp_build_tgsi_context
*bld_base
)
556 return (struct lp_build_tgsi_soa_context
*)bld_base
;
559 static INLINE
struct lp_build_tgsi_aos_context
*
560 lp_aos_context(struct lp_build_tgsi_context
*bld_base
)
562 return (struct lp_build_tgsi_aos_context
*)bld_base
;
566 lp_emit_declaration_aos(
567 struct lp_build_tgsi_aos_context
*bld
,
568 const struct tgsi_full_declaration
*decl
);
572 lp_emit_instruction_aos(
573 struct lp_build_tgsi_aos_context
*bld
,
574 const struct tgsi_full_instruction
*inst
,
575 const struct tgsi_opcode_info
*info
,
580 struct lp_build_tgsi_aos_context
*bld
,
581 const struct tgsi_full_instruction
*inst
,
585 void lp_build_fetch_args(
586 struct lp_build_tgsi_context
* bld_base
,
587 struct lp_build_emit_data
* emit_data
);
590 lp_build_tgsi_inst_llvm_aos(
591 struct lp_build_tgsi_context
* bld_base
,
592 const struct tgsi_full_instruction
*inst
);
595 lp_build_tgsi_intrinsic(
596 const struct lp_build_tgsi_action
* action
,
597 struct lp_build_tgsi_context
* bld_base
,
598 struct lp_build_emit_data
* emit_data
);
602 struct lp_build_tgsi_context
*bld_base
,
603 unsigned tgsi_opcode
,
604 struct lp_build_emit_data
* emit_data
);
607 lp_build_emit_llvm_unary(
608 struct lp_build_tgsi_context
*bld_base
,
609 unsigned tgsi_opcode
,
613 lp_build_emit_llvm_binary(
614 struct lp_build_tgsi_context
*bld_base
,
615 unsigned tgsi_opcode
,
620 lp_build_emit_llvm_ternary(
621 struct lp_build_tgsi_context
*bld_base
,
622 unsigned tgsi_opcode
,
628 lp_build_tgsi_inst_llvm(
629 struct lp_build_tgsi_context
* bld_base
,
630 const struct tgsi_full_instruction
*inst
);
634 struct lp_build_tgsi_context
*bld_base
,
635 const struct tgsi_full_instruction
*inst
,
637 const unsigned chan_index
);
641 lp_build_emit_fetch_texoffset(
642 struct lp_build_tgsi_context
*bld_base
,
643 const struct tgsi_full_instruction
*inst
,
645 const unsigned chan_index
);
649 struct lp_build_tgsi_context
* bld_base
,
650 const struct tgsi_token
*tokens
);
652 #endif /* LP_BLD_TGSI_H */