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 "lp_bld_type.h"
44 #include "pipe/p_compiler.h"
45 #include "pipe/p_state.h"
46 #include "tgsi/tgsi_exec.h"
47 #include "tgsi/tgsi_scan.h"
48 #include "tgsi/tgsi_info.h"
50 #define LP_CHAN_ALL ~0
52 #define LP_MAX_INSTRUCTIONS 256
54 struct tgsi_full_declaration
;
55 struct tgsi_full_immediate
;
56 struct tgsi_full_instruction
;
57 struct tgsi_full_src_register
;
58 struct tgsi_opcode_info
;
60 struct tgsi_shader_info
;
61 struct lp_build_mask_context
;
63 struct lp_derivatives
;
66 enum lp_build_tex_modifier
{
67 LP_BLD_TEX_MODIFIER_NONE
= 0,
68 LP_BLD_TEX_MODIFIER_PROJECTED
,
69 LP_BLD_TEX_MODIFIER_LOD_BIAS
,
70 LP_BLD_TEX_MODIFIER_EXPLICIT_LOD
,
71 LP_BLD_TEX_MODIFIER_EXPLICIT_DERIV
76 * Describe a channel of a register.
79 * - immediate value (i.e. derived from a IMM register)
82 * - undetermined (when .file == TGSI_FILE_NULL)
84 * This is one of the analysis results, and is used to described
85 * the output color in terms of inputs.
87 struct lp_tgsi_channel_info
89 unsigned file
:4; /* TGSI_FILE_* */
90 unsigned swizzle
:3; /* PIPE_SWIZZLE_x */
93 float value
; /* for TGSI_FILE_IMMEDIATE */
99 * Describe a texture sampler interpolator.
101 * The interpolation is described in terms of regular inputs.
103 struct lp_tgsi_texture_info
105 struct lp_tgsi_channel_info coord
[4];
106 unsigned target
:8; /* TGSI_TEXTURE_* */
107 unsigned unit
:8; /* Sampler unit */
108 unsigned modifier
:8; /* LP_BLD_TEX_MODIFIER_* */
114 struct tgsi_shader_info base
;
117 * Whether any of the texture opcodes access a register file other than
120 * We could also handle TGSI_FILE_CONST/IMMEDIATE here, but there is little
123 unsigned indirect_textures
:1;
126 * Whether any immediate values are outside the range of 0 and 1
128 unsigned unclamped_immediates
:1;
131 * Texture opcode description. Aimed at detecting and described direct
135 struct lp_tgsi_texture_info tex
[PIPE_MAX_SAMPLERS
];
138 * Output description. Aimed at detecting and describing simple blit
141 struct lp_tgsi_channel_info output
[PIPE_MAX_SHADER_OUTPUTS
][4];
144 * Shortcut pointers into the above (for fragment shaders).
146 const struct lp_tgsi_channel_info
*cbuf
[PIPE_MAX_COLOR_BUFS
];
150 * Reference to system values.
152 struct lp_bld_tgsi_system_values
{
153 LLVMValueRef instance_id
;
154 LLVMValueRef vertex_id
;
159 * Sampler code generation interface.
161 * Although texture sampling is a requirement for TGSI translation, it is
162 * a very different problem with several different approaches to it. This
163 * structure establishes an interface for texture sampling code generation, so
164 * that we can easily use different texture sampling strategies.
166 struct lp_build_sampler_soa
169 (*destroy
)( struct lp_build_sampler_soa
*sampler
);
172 (*emit_fetch_texel
)( const struct lp_build_sampler_soa
*sampler
,
173 struct gallivm_state
*gallivm
,
177 const LLVMValueRef
*coords
,
178 const struct lp_derivatives
*derivs
,
179 LLVMValueRef lod_bias
, /* optional */
180 LLVMValueRef explicit_lod
, /* optional */
181 LLVMValueRef
*texel
);
184 (*emit_size_query
)( const struct lp_build_sampler_soa
*sampler
,
185 struct gallivm_state
*gallivm
,
188 LLVMValueRef explicit_lod
, /* optional */
189 LLVMValueRef
*sizes_out
);
193 struct lp_build_sampler_aos
196 (*emit_fetch_texel
)( struct lp_build_sampler_aos
*sampler
,
197 struct lp_build_context
*bld
,
198 unsigned target
, /* TGSI_TEXTURE_* */
201 const struct lp_derivatives derivs
,
202 enum lp_build_tex_modifier modifier
);
207 lp_build_tgsi_info(const struct tgsi_token
*tokens
,
208 struct lp_tgsi_info
*info
);
212 lp_build_tgsi_soa(struct gallivm_state
*gallivm
,
213 const struct tgsi_token
*tokens
,
215 struct lp_build_mask_context
*mask
,
216 LLVMValueRef consts_ptr
,
217 const struct lp_bld_tgsi_system_values
*system_values
,
218 const LLVMValueRef
*pos
,
219 const LLVMValueRef (*inputs
)[4],
220 LLVMValueRef (*outputs
)[4],
221 struct lp_build_sampler_soa
*sampler
,
222 const struct tgsi_shader_info
*info
);
226 lp_build_tgsi_aos(struct gallivm_state
*gallivm
,
227 const struct tgsi_token
*tokens
,
229 const unsigned char swizzles
[4],
230 LLVMValueRef consts_ptr
,
231 const LLVMValueRef
*inputs
,
232 LLVMValueRef
*outputs
,
233 struct lp_build_sampler_aos
*sampler
,
234 const struct tgsi_shader_info
*info
);
237 struct lp_exec_mask
{
238 struct lp_build_context
*bld
;
242 LLVMTypeRef int_vec_type
;
244 LLVMValueRef cond_stack
[LP_MAX_TGSI_NESTING
];
246 LLVMValueRef cond_mask
;
248 LLVMBasicBlockRef loop_block
;
249 LLVMValueRef cont_mask
;
250 LLVMValueRef break_mask
;
251 LLVMValueRef break_var
;
253 LLVMBasicBlockRef loop_block
;
254 LLVMValueRef cont_mask
;
255 LLVMValueRef break_mask
;
256 LLVMValueRef break_var
;
257 } loop_stack
[LP_MAX_TGSI_NESTING
];
260 LLVMValueRef ret_mask
;
263 LLVMValueRef ret_mask
;
264 } call_stack
[LP_MAX_TGSI_NESTING
];
267 LLVMValueRef exec_mask
;
268 LLVMValueRef loop_limiter
;
271 struct lp_build_tgsi_inst_list
273 struct tgsi_full_instruction
*instructions
;
274 uint max_instructions
;
275 uint num_instructions
;
278 unsigned lp_bld_tgsi_list_init(struct lp_build_tgsi_context
* bld_base
);
281 unsigned lp_bld_tgsi_add_instruction(
282 struct lp_build_tgsi_context
* bld_base
,
283 struct tgsi_full_instruction
*inst_to_add
);
286 struct lp_build_tgsi_context
;
289 typedef LLVMValueRef (*lp_build_emit_fetch_fn
)(struct lp_build_tgsi_context
*,
290 const struct tgsi_full_src_register
*,
291 enum tgsi_opcode_type
,
294 struct lp_build_tgsi_context
296 struct lp_build_context base
;
298 struct lp_build_context uint_bld
;
299 struct lp_build_context int_bld
;
301 /** This array stores functions that are used to transform TGSI opcodes to
304 struct lp_build_tgsi_action op_actions
[TGSI_OPCODE_LAST
];
306 /* TGSI_OPCODE_RSQ is defined as 1 / sqrt( abs(src0.x) ), rsq_action
307 * should compute 1 / sqrt (src0.x) */
308 struct lp_build_tgsi_action rsq_action
;
310 const struct tgsi_shader_info
*info
;
312 lp_build_emit_fetch_fn emit_fetch_funcs
[TGSI_FILE_COUNT
];
314 LLVMValueRef (*emit_swizzle
)(struct lp_build_tgsi_context
*,
315 LLVMValueRef
, unsigned, unsigned, unsigned, unsigned);
317 void (*emit_store
)(struct lp_build_tgsi_context
*,
318 const struct tgsi_full_instruction
*,
319 const struct tgsi_opcode_info
*,
320 LLVMValueRef dst
[4]);
322 void (*emit_declaration
)(struct lp_build_tgsi_context
*,
323 const struct tgsi_full_declaration
*decl
);
325 void (*emit_immediate
)(struct lp_build_tgsi_context
*,
326 const struct tgsi_full_immediate
*imm
);
329 /* Allow the user to store data in this structure rather than passing it
330 * to every function. */
337 struct tgsi_full_instruction
*instructions
;
338 uint max_instructions
;
339 uint num_instructions
;
341 /** This function allows the user to insert some instructions at the
342 * beginning of the program. It is optional and does not need to be
345 void (*emit_prologue
)(struct lp_build_tgsi_context
*);
347 /** This function allows the user to insert some instructions at the end of
348 * the program. This callback is intended to be used for emitting
349 * instructions to handle the export for the output registers, but it can
350 * be used for any purpose. Implementing this function is optiona, but
353 void (*emit_epilogue
)(struct lp_build_tgsi_context
*);
356 struct lp_build_tgsi_soa_context
358 struct lp_build_tgsi_context bld_base
;
360 /* Builder for scalar elements of shader's data type (float) */
361 struct lp_build_context elem_bld
;
363 LLVMValueRef consts_ptr
;
364 const LLVMValueRef
*pos
;
365 const LLVMValueRef (*inputs
)[TGSI_NUM_CHANNELS
];
366 LLVMValueRef (*outputs
)[TGSI_NUM_CHANNELS
];
368 const struct lp_build_sampler_soa
*sampler
;
370 LLVMValueRef immediates
[LP_MAX_TGSI_IMMEDIATES
][TGSI_NUM_CHANNELS
];
371 LLVMValueRef temps
[LP_MAX_TGSI_TEMPS
][TGSI_NUM_CHANNELS
];
372 LLVMValueRef addr
[LP_MAX_TGSI_ADDRS
][TGSI_NUM_CHANNELS
];
373 LLVMValueRef preds
[LP_MAX_TGSI_PREDS
][TGSI_NUM_CHANNELS
];
375 /* We allocate/use this array of temps if (1 << TGSI_FILE_TEMPORARY) is
376 * set in the indirect_files field.
377 * The temps[] array above is unused then.
379 LLVMValueRef temps_array
;
381 /* We allocate/use this array of output if (1 << TGSI_FILE_OUTPUT) is
382 * set in the indirect_files field.
383 * The outputs[] array above is unused then.
385 LLVMValueRef outputs_array
;
387 /* We allocate/use this array of inputs if (1 << TGSI_FILE_INPUT) is
388 * set in the indirect_files field.
389 * The inputs[] array above is unused then.
391 LLVMValueRef inputs_array
;
393 struct lp_bld_tgsi_system_values system_values
;
395 /** bitmask indicating which register files are accessed indirectly */
396 unsigned indirect_files
;
398 struct lp_build_mask_context
*mask
;
399 struct lp_exec_mask exec_mask
;
406 lp_emit_declaration_soa(
407 struct lp_build_tgsi_context
*bld
,
408 const struct tgsi_full_declaration
*decl
);
410 void lp_emit_immediate_soa(
411 struct lp_build_tgsi_context
*bld_base
,
412 const struct tgsi_full_immediate
*imm
);
415 lp_emit_instruction_soa(
416 struct lp_build_tgsi_soa_context
*bld
,
417 const struct tgsi_full_instruction
*inst
,
418 const struct tgsi_opcode_info
*info
);
423 struct lp_build_tgsi_soa_context
*bld
,
429 struct lp_build_tgsi_soa_context
*bld
,
433 struct lp_build_tgsi_aos_context
435 struct lp_build_tgsi_context bld_base
;
437 /* Builder for integer masks and indices */
438 struct lp_build_context int_bld
;
442 * - swizzles[0] = red index
443 * - swizzles[1] = green index
444 * - swizzles[2] = blue index
445 * - swizzles[3] = alpha index
447 unsigned char swizzles
[4];
448 unsigned char inv_swizzles
[4];
450 LLVMValueRef consts_ptr
;
451 const LLVMValueRef
*inputs
;
452 LLVMValueRef
*outputs
;
454 struct lp_build_sampler_aos
*sampler
;
456 LLVMValueRef immediates
[LP_MAX_TGSI_IMMEDIATES
];
457 LLVMValueRef temps
[LP_MAX_TGSI_TEMPS
];
458 LLVMValueRef addr
[LP_MAX_TGSI_ADDRS
];
459 LLVMValueRef preds
[LP_MAX_TGSI_PREDS
];
461 /* We allocate/use this array of temps if (1 << TGSI_FILE_TEMPORARY) is
462 * set in the indirect_files field.
463 * The temps[] array above is unused then.
465 LLVMValueRef temps_array
;
467 /** bitmask indicating which register files are accessed indirectly */
468 unsigned indirect_files
;
472 static INLINE
struct lp_build_tgsi_soa_context
*
473 lp_soa_context(struct lp_build_tgsi_context
*bld_base
)
475 return (struct lp_build_tgsi_soa_context
*)bld_base
;
478 static INLINE
struct lp_build_tgsi_aos_context
*
479 lp_aos_context(struct lp_build_tgsi_context
*bld_base
)
481 return (struct lp_build_tgsi_aos_context
*)bld_base
;
485 lp_emit_declaration_aos(
486 struct lp_build_tgsi_aos_context
*bld
,
487 const struct tgsi_full_declaration
*decl
);
491 lp_emit_instruction_aos(
492 struct lp_build_tgsi_aos_context
*bld
,
493 const struct tgsi_full_instruction
*inst
,
494 const struct tgsi_opcode_info
*info
,
499 struct lp_build_tgsi_aos_context
*bld
,
500 const struct tgsi_full_instruction
*inst
,
504 void lp_build_fetch_args(
505 struct lp_build_tgsi_context
* bld_base
,
506 struct lp_build_emit_data
* emit_data
);
509 lp_build_tgsi_inst_llvm_aos(
510 struct lp_build_tgsi_context
* bld_base
,
511 const struct tgsi_full_instruction
*inst
);
514 lp_build_tgsi_intrinsic(
515 const struct lp_build_tgsi_action
* action
,
516 struct lp_build_tgsi_context
* bld_base
,
517 struct lp_build_emit_data
* emit_data
);
521 struct lp_build_tgsi_context
*bld_base
,
522 unsigned tgsi_opcode
,
523 struct lp_build_emit_data
* emit_data
);
526 lp_build_emit_llvm_unary(
527 struct lp_build_tgsi_context
*bld_base
,
528 unsigned tgsi_opcode
,
532 lp_build_emit_llvm_binary(
533 struct lp_build_tgsi_context
*bld_base
,
534 unsigned tgsi_opcode
,
539 lp_build_emit_llvm_ternary(
540 struct lp_build_tgsi_context
*bld_base
,
541 unsigned tgsi_opcode
,
547 lp_build_tgsi_inst_llvm(
548 struct lp_build_tgsi_context
* bld_base
,
549 const struct tgsi_full_instruction
*inst
);
553 struct lp_build_tgsi_context
*bld_base
,
554 const struct tgsi_full_instruction
*inst
,
556 const unsigned chan_index
);
560 struct lp_build_tgsi_context
* bld_base
,
561 const struct tgsi_token
*tokens
);
563 #endif /* LP_BLD_TGSI_H */