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
;
65 enum lp_build_tex_modifier
{
66 LP_BLD_TEX_MODIFIER_NONE
= 0,
67 LP_BLD_TEX_MODIFIER_PROJECTED
,
68 LP_BLD_TEX_MODIFIER_LOD_BIAS
,
69 LP_BLD_TEX_MODIFIER_EXPLICIT_LOD
,
70 LP_BLD_TEX_MODIFIER_EXPLICIT_DERIV
75 * Describe a channel of a register.
78 * - immediate value (i.e. derived from a IMM register)
81 * - undetermined (when .file == TGSI_FILE_NULL)
83 * This is one of the analysis results, and is used to described
84 * the output color in terms of inputs.
86 struct lp_tgsi_channel_info
88 unsigned file
:4; /* TGSI_FILE_* */
89 unsigned swizzle
:3; /* PIPE_SWIZZLE_x */
92 float value
; /* for TGSI_FILE_IMMEDIATE */
98 * Describe a texture sampler interpolator.
100 * The interpolation is described in terms of regular inputs.
102 struct lp_tgsi_texture_info
104 struct lp_tgsi_channel_info coord
[4];
105 unsigned target
:8; /* TGSI_TEXTURE_* */
106 unsigned unit
:8; /* Sampler unit */
107 unsigned modifier
:8; /* LP_BLD_TEX_MODIFIER_* */
113 struct tgsi_shader_info base
;
116 * Whether any of the texture opcodes access a register file other than
119 * We could also handle TGSI_FILE_CONST/IMMEDIATE here, but there is little
122 unsigned indirect_textures
:1;
125 * Texture opcode description. Aimed at detecting and described direct
129 struct lp_tgsi_texture_info tex
[PIPE_MAX_SAMPLERS
];
132 * Output description. Aimed at detecting and describing simple blit
135 struct lp_tgsi_channel_info output
[PIPE_MAX_SHADER_OUTPUTS
][4];
138 * Shortcut pointers into the above (for fragment shaders).
140 const struct lp_tgsi_channel_info
*cbuf
[PIPE_MAX_COLOR_BUFS
];
144 * Sampler code generation interface.
146 * Although texture sampling is a requirement for TGSI translation, it is
147 * a very different problem with several different approaches to it. This
148 * structure establishes an interface for texture sampling code generation, so
149 * that we can easily use different texture sampling strategies.
151 struct lp_build_sampler_soa
154 (*destroy
)( struct lp_build_sampler_soa
*sampler
);
157 (*emit_fetch_texel
)( const struct lp_build_sampler_soa
*sampler
,
158 struct gallivm_state
*gallivm
,
162 const LLVMValueRef
*coords
,
163 const LLVMValueRef
*ddx
,
164 const LLVMValueRef
*ddy
,
165 LLVMValueRef lod_bias
, /* optional */
166 LLVMValueRef explicit_lod
, /* optional */
167 LLVMValueRef
*texel
);
171 struct lp_build_sampler_aos
174 (*emit_fetch_texel
)( struct lp_build_sampler_aos
*sampler
,
175 struct lp_build_context
*bld
,
176 unsigned target
, /* TGSI_TEXTURE_* */
181 enum lp_build_tex_modifier modifier
);
186 lp_build_tgsi_info(const struct tgsi_token
*tokens
,
187 struct lp_tgsi_info
*info
);
191 lp_build_tgsi_soa(struct gallivm_state
*gallivm
,
192 const struct tgsi_token
*tokens
,
194 struct lp_build_mask_context
*mask
,
195 LLVMValueRef consts_ptr
,
196 LLVMValueRef system_values_array
,
197 const LLVMValueRef
*pos
,
198 const LLVMValueRef (*inputs
)[4],
199 LLVMValueRef (*outputs
)[4],
200 struct lp_build_sampler_soa
*sampler
,
201 const struct tgsi_shader_info
*info
);
205 lp_build_tgsi_aos(struct gallivm_state
*gallivm
,
206 const struct tgsi_token
*tokens
,
208 const unsigned char swizzles
[4],
209 LLVMValueRef consts_ptr
,
210 const LLVMValueRef
*inputs
,
211 LLVMValueRef
*outputs
,
212 struct lp_build_sampler_aos
*sampler
,
213 const struct tgsi_shader_info
*info
);
217 lp_build_system_values_array(struct gallivm_state
*gallivm
,
218 const struct tgsi_shader_info
*info
,
219 LLVMValueRef instance_id
,
220 LLVMValueRef facing
);
223 struct lp_exec_mask
{
224 struct lp_build_context
*bld
;
228 LLVMTypeRef int_vec_type
;
230 LLVMValueRef cond_stack
[LP_MAX_TGSI_NESTING
];
232 LLVMValueRef cond_mask
;
234 LLVMBasicBlockRef loop_block
;
235 LLVMValueRef cont_mask
;
236 LLVMValueRef break_mask
;
237 LLVMValueRef break_var
;
239 LLVMBasicBlockRef loop_block
;
240 LLVMValueRef cont_mask
;
241 LLVMValueRef break_mask
;
242 LLVMValueRef break_var
;
243 } loop_stack
[LP_MAX_TGSI_NESTING
];
246 LLVMValueRef ret_mask
;
249 LLVMValueRef ret_mask
;
250 } call_stack
[LP_MAX_TGSI_NESTING
];
253 LLVMValueRef exec_mask
;
254 LLVMValueRef loop_limiter
;
257 struct lp_build_tgsi_inst_list
259 struct tgsi_full_instruction
*instructions
;
260 uint max_instructions
;
261 uint num_instructions
;
264 unsigned lp_bld_tgsi_list_init(struct lp_build_tgsi_context
* bld_base
);
267 unsigned lp_bld_tgsi_add_instruction(
268 struct lp_build_tgsi_context
* bld_base
,
269 struct tgsi_full_instruction
*inst_to_add
);
272 struct lp_build_tgsi_context
;
275 typedef LLVMValueRef (*lp_build_emit_fetch_fn
)(struct lp_build_tgsi_context
*,
276 const struct tgsi_full_src_register
*,
277 enum tgsi_opcode_type
,
280 struct lp_build_tgsi_context
282 struct lp_build_context base
;
284 struct lp_build_context uint_bld
;
285 struct lp_build_context int_bld
;
287 /** This array stores functions that are used to transform TGSI opcodes to
290 struct lp_build_tgsi_action op_actions
[TGSI_OPCODE_LAST
];
292 /* TGSI_OPCODE_RSQ is defined as 1 / sqrt( abs(src0.x) ), rsq_action
293 * should compute 1 / sqrt (src0.x) */
294 struct lp_build_tgsi_action rsq_action
;
296 const struct tgsi_shader_info
*info
;
298 lp_build_emit_fetch_fn emit_fetch_funcs
[TGSI_FILE_COUNT
];
300 LLVMValueRef (*emit_swizzle
)(struct lp_build_tgsi_context
*,
301 LLVMValueRef
, unsigned, unsigned, unsigned, unsigned);
303 void (*emit_store
)(struct lp_build_tgsi_context
*,
304 const struct tgsi_full_instruction
*,
305 const struct tgsi_opcode_info
*,
306 LLVMValueRef dst
[4]);
308 void (*emit_declaration
)(struct lp_build_tgsi_context
*,
309 const struct tgsi_full_declaration
*decl
);
311 void (*emit_immediate
)(struct lp_build_tgsi_context
*,
312 const struct tgsi_full_immediate
*imm
);
315 /* Allow the user to store data in this structure rather than passing it
316 * to every function. */
323 struct tgsi_full_instruction
*instructions
;
324 uint max_instructions
;
325 uint num_instructions
;
327 /** This function allows the user to insert some instructions at the
328 * beginning of the program. It is optional and does not need to be
331 void (*emit_prologue
)(struct lp_build_tgsi_context
*);
333 /** This function allows the user to insert some instructions at the end of
334 * the program. This callback is intended to be used for emitting
335 * instructions to handle the export for the output registers, but it can
336 * be used for any purpose. Implementing this function is optiona, but
339 void (*emit_epilogue
)(struct lp_build_tgsi_context
*);
342 struct lp_build_tgsi_soa_context
344 struct lp_build_tgsi_context bld_base
;
346 /* Builder for scalar elements of shader's data type (float) */
347 struct lp_build_context elem_bld
;
349 LLVMValueRef consts_ptr
;
350 const LLVMValueRef
*pos
;
351 const LLVMValueRef (*inputs
)[TGSI_NUM_CHANNELS
];
352 LLVMValueRef (*outputs
)[TGSI_NUM_CHANNELS
];
354 const struct lp_build_sampler_soa
*sampler
;
356 LLVMValueRef immediates
[LP_MAX_TGSI_IMMEDIATES
][TGSI_NUM_CHANNELS
];
357 LLVMValueRef temps
[LP_MAX_TGSI_TEMPS
][TGSI_NUM_CHANNELS
];
358 LLVMValueRef addr
[LP_MAX_TGSI_ADDRS
][TGSI_NUM_CHANNELS
];
359 LLVMValueRef preds
[LP_MAX_TGSI_PREDS
][TGSI_NUM_CHANNELS
];
361 /* We allocate/use this array of temps if (1 << TGSI_FILE_TEMPORARY) is
362 * set in the indirect_files field.
363 * The temps[] array above is unused then.
365 LLVMValueRef temps_array
;
367 /* We allocate/use this array of output if (1 << TGSI_FILE_OUTPUT) is
368 * set in the indirect_files field.
369 * The outputs[] array above is unused then.
371 LLVMValueRef outputs_array
;
373 /* We allocate/use this array of inputs if (1 << TGSI_FILE_INPUT) is
374 * set in the indirect_files field.
375 * The inputs[] array above is unused then.
377 LLVMValueRef inputs_array
;
379 LLVMValueRef system_values_array
;
381 /** bitmask indicating which register files are accessed indirectly */
382 unsigned indirect_files
;
384 struct lp_build_mask_context
*mask
;
385 struct lp_exec_mask exec_mask
;
392 lp_emit_declaration_soa(
393 struct lp_build_tgsi_context
*bld
,
394 const struct tgsi_full_declaration
*decl
);
396 void lp_emit_immediate_soa(
397 struct lp_build_tgsi_context
*bld_base
,
398 const struct tgsi_full_immediate
*imm
);
401 lp_emit_instruction_soa(
402 struct lp_build_tgsi_soa_context
*bld
,
403 const struct tgsi_full_instruction
*inst
,
404 const struct tgsi_opcode_info
*info
);
409 struct lp_build_tgsi_soa_context
*bld
,
415 struct lp_build_tgsi_soa_context
*bld
,
419 struct lp_build_tgsi_aos_context
421 struct lp_build_tgsi_context bld_base
;
423 /* Builder for integer masks and indices */
424 struct lp_build_context int_bld
;
428 * - swizzles[0] = red index
429 * - swizzles[1] = green index
430 * - swizzles[2] = blue index
431 * - swizzles[3] = alpha index
433 unsigned char swizzles
[4];
434 unsigned char inv_swizzles
[4];
436 LLVMValueRef consts_ptr
;
437 const LLVMValueRef
*inputs
;
438 LLVMValueRef
*outputs
;
440 struct lp_build_sampler_aos
*sampler
;
442 LLVMValueRef immediates
[LP_MAX_TGSI_IMMEDIATES
];
443 LLVMValueRef temps
[LP_MAX_TGSI_TEMPS
];
444 LLVMValueRef addr
[LP_MAX_TGSI_ADDRS
];
445 LLVMValueRef preds
[LP_MAX_TGSI_PREDS
];
447 /* We allocate/use this array of temps if (1 << TGSI_FILE_TEMPORARY) is
448 * set in the indirect_files field.
449 * The temps[] array above is unused then.
451 LLVMValueRef temps_array
;
453 /** bitmask indicating which register files are accessed indirectly */
454 unsigned indirect_files
;
458 static INLINE
struct lp_build_tgsi_soa_context
*
459 lp_soa_context(struct lp_build_tgsi_context
*bld_base
)
461 return (struct lp_build_tgsi_soa_context
*)bld_base
;
464 static INLINE
struct lp_build_tgsi_aos_context
*
465 lp_aos_context(struct lp_build_tgsi_context
*bld_base
)
467 return (struct lp_build_tgsi_aos_context
*)bld_base
;
471 lp_emit_declaration_aos(
472 struct lp_build_tgsi_aos_context
*bld
,
473 const struct tgsi_full_declaration
*decl
);
477 lp_emit_instruction_aos(
478 struct lp_build_tgsi_aos_context
*bld
,
479 const struct tgsi_full_instruction
*inst
,
480 const struct tgsi_opcode_info
*info
,
485 struct lp_build_tgsi_aos_context
*bld
,
486 const struct tgsi_full_instruction
*inst
,
490 void lp_build_fetch_args(
491 struct lp_build_tgsi_context
* bld_base
,
492 struct lp_build_emit_data
* emit_data
);
495 lp_build_tgsi_inst_llvm_aos(
496 struct lp_build_tgsi_context
* bld_base
,
497 const struct tgsi_full_instruction
*inst
);
500 lp_build_tgsi_intrinsic(
501 const struct lp_build_tgsi_action
* action
,
502 struct lp_build_tgsi_context
* bld_base
,
503 struct lp_build_emit_data
* emit_data
);
507 struct lp_build_tgsi_context
*bld_base
,
508 unsigned tgsi_opcode
,
509 struct lp_build_emit_data
* emit_data
);
512 lp_build_emit_llvm_unary(
513 struct lp_build_tgsi_context
*bld_base
,
514 unsigned tgsi_opcode
,
518 lp_build_emit_llvm_binary(
519 struct lp_build_tgsi_context
*bld_base
,
520 unsigned tgsi_opcode
,
525 lp_build_emit_llvm_ternary(
526 struct lp_build_tgsi_context
*bld_base
,
527 unsigned tgsi_opcode
,
533 lp_build_tgsi_inst_llvm(
534 struct lp_build_tgsi_context
* bld_base
,
535 const struct tgsi_full_instruction
*inst
);
539 struct lp_build_tgsi_context
*bld_base
,
540 const struct tgsi_full_instruction
*inst
,
542 const unsigned chan_index
);
546 struct lp_build_tgsi_context
* bld_base
,
547 const struct tgsi_token
*tokens
);
549 #endif /* LP_BLD_TGSI_H */