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
,
72 LP_BLD_TEX_MODIFIER_LOD_ZERO
77 * Describe a channel of a register.
80 * - immediate value (i.e. derived from a IMM register)
83 * - undetermined (when .file == TGSI_FILE_NULL)
85 * This is one of the analysis results, and is used to described
86 * the output color in terms of inputs.
88 struct lp_tgsi_channel_info
90 unsigned file
:4; /* TGSI_FILE_* */
91 unsigned swizzle
:3; /* PIPE_SWIZZLE_x */
94 float value
; /* for TGSI_FILE_IMMEDIATE */
100 * Describe a texture sampler interpolator.
102 * The interpolation is described in terms of regular inputs.
104 struct lp_tgsi_texture_info
106 struct lp_tgsi_channel_info coord
[4];
107 unsigned target
:8; /* TGSI_TEXTURE_* */
108 unsigned sampler_unit
:8; /* Sampler unit */
109 unsigned texture_unit
:8; /* Texture unit */
110 unsigned modifier
:8; /* LP_BLD_TEX_MODIFIER_* */
116 struct tgsi_shader_info base
;
119 * Whether any of the texture opcodes access a register file other than
122 * We could also handle TGSI_FILE_CONST/IMMEDIATE here, but there is little
125 unsigned indirect_textures
:1;
128 * Whether any immediate values are outside the range of 0 and 1
130 unsigned unclamped_immediates
:1;
133 * Texture opcode description. Aimed at detecting and described direct
137 struct lp_tgsi_texture_info tex
[PIPE_MAX_SAMPLERS
];
140 * Output description. Aimed at detecting and describing simple blit
143 struct lp_tgsi_channel_info output
[PIPE_MAX_SHADER_OUTPUTS
][4];
146 * Shortcut pointers into the above (for fragment shaders).
148 const struct lp_tgsi_channel_info
*cbuf
[PIPE_MAX_COLOR_BUFS
];
152 * Reference to system values.
154 struct lp_bld_tgsi_system_values
{
155 LLVMValueRef instance_id
;
156 LLVMValueRef vertex_id
;
161 * Sampler code generation interface.
163 * Although texture sampling is a requirement for TGSI translation, it is
164 * a very different problem with several different approaches to it. This
165 * structure establishes an interface for texture sampling code generation, so
166 * that we can easily use different texture sampling strategies.
168 struct lp_build_sampler_soa
171 (*destroy
)( struct lp_build_sampler_soa
*sampler
);
174 (*emit_fetch_texel
)( const struct lp_build_sampler_soa
*sampler
,
175 struct gallivm_state
*gallivm
,
178 unsigned texture_index
,
179 unsigned sampler_index
,
180 const LLVMValueRef
*coords
,
181 const LLVMValueRef
*offsets
,
182 const struct lp_derivatives
*derivs
,
183 LLVMValueRef lod_bias
, /* optional */
184 LLVMValueRef explicit_lod
, /* optional */
185 LLVMValueRef
*texel
);
188 (*emit_size_query
)( const struct lp_build_sampler_soa
*sampler
,
189 struct gallivm_state
*gallivm
,
192 boolean need_nr_mips
,
193 LLVMValueRef explicit_lod
, /* optional */
194 LLVMValueRef
*sizes_out
);
198 struct lp_build_sampler_aos
201 (*emit_fetch_texel
)( struct lp_build_sampler_aos
*sampler
,
202 struct lp_build_context
*bld
,
203 unsigned target
, /* TGSI_TEXTURE_* */
206 const struct lp_derivatives derivs
,
207 enum lp_build_tex_modifier modifier
);
212 lp_build_tgsi_info(const struct tgsi_token
*tokens
,
213 struct lp_tgsi_info
*info
);
217 lp_build_tgsi_soa(struct gallivm_state
*gallivm
,
218 const struct tgsi_token
*tokens
,
220 struct lp_build_mask_context
*mask
,
221 LLVMValueRef consts_ptr
,
222 const struct lp_bld_tgsi_system_values
*system_values
,
223 const LLVMValueRef
*pos
,
224 const LLVMValueRef (*inputs
)[4],
225 LLVMValueRef (*outputs
)[4],
226 struct lp_build_sampler_soa
*sampler
,
227 const struct tgsi_shader_info
*info
);
231 lp_build_tgsi_aos(struct gallivm_state
*gallivm
,
232 const struct tgsi_token
*tokens
,
234 const unsigned char swizzles
[4],
235 LLVMValueRef consts_ptr
,
236 const LLVMValueRef
*inputs
,
237 LLVMValueRef
*outputs
,
238 struct lp_build_sampler_aos
*sampler
,
239 const struct tgsi_shader_info
*info
);
242 struct lp_exec_mask
{
243 struct lp_build_context
*bld
;
247 LLVMTypeRef int_vec_type
;
249 LLVMValueRef cond_stack
[LP_MAX_TGSI_NESTING
];
251 LLVMValueRef cond_mask
;
253 LLVMBasicBlockRef loop_block
;
254 LLVMValueRef cont_mask
;
255 LLVMValueRef break_mask
;
256 LLVMValueRef break_var
;
258 LLVMBasicBlockRef loop_block
;
259 LLVMValueRef cont_mask
;
260 LLVMValueRef break_mask
;
261 LLVMValueRef break_var
;
262 } loop_stack
[LP_MAX_TGSI_NESTING
];
265 LLVMValueRef ret_mask
;
268 LLVMValueRef ret_mask
;
269 } call_stack
[LP_MAX_TGSI_NESTING
];
272 LLVMValueRef exec_mask
;
273 LLVMValueRef loop_limiter
;
276 struct lp_build_tgsi_inst_list
278 struct tgsi_full_instruction
*instructions
;
279 uint max_instructions
;
280 uint num_instructions
;
283 unsigned lp_bld_tgsi_list_init(struct lp_build_tgsi_context
* bld_base
);
286 unsigned lp_bld_tgsi_add_instruction(
287 struct lp_build_tgsi_context
* bld_base
,
288 struct tgsi_full_instruction
*inst_to_add
);
291 struct lp_build_tgsi_context
;
294 typedef LLVMValueRef (*lp_build_emit_fetch_fn
)(struct lp_build_tgsi_context
*,
295 const struct tgsi_full_src_register
*,
296 enum tgsi_opcode_type
,
299 struct lp_build_tgsi_context
301 struct lp_build_context base
;
303 struct lp_build_context uint_bld
;
304 struct lp_build_context int_bld
;
306 /** This array stores functions that are used to transform TGSI opcodes to
309 struct lp_build_tgsi_action op_actions
[TGSI_OPCODE_LAST
];
311 /* TGSI_OPCODE_RSQ is defined as 1 / sqrt( abs(src0.x) ), rsq_action
312 * should compute 1 / sqrt (src0.x) */
313 struct lp_build_tgsi_action rsq_action
;
315 struct lp_build_tgsi_action sqrt_action
;
317 const struct tgsi_shader_info
*info
;
319 lp_build_emit_fetch_fn emit_fetch_funcs
[TGSI_FILE_COUNT
];
321 LLVMValueRef (*emit_swizzle
)(struct lp_build_tgsi_context
*,
322 LLVMValueRef
, unsigned, unsigned, unsigned, unsigned);
324 void (*emit_store
)(struct lp_build_tgsi_context
*,
325 const struct tgsi_full_instruction
*,
326 const struct tgsi_opcode_info
*,
327 LLVMValueRef dst
[4]);
329 void (*emit_declaration
)(struct lp_build_tgsi_context
*,
330 const struct tgsi_full_declaration
*decl
);
332 void (*emit_immediate
)(struct lp_build_tgsi_context
*,
333 const struct tgsi_full_immediate
*imm
);
336 /* Allow the user to store data in this structure rather than passing it
337 * to every function. */
344 struct tgsi_full_instruction
*instructions
;
345 uint max_instructions
;
346 uint num_instructions
;
348 /** This function allows the user to insert some instructions at the
349 * beginning of the program. It is optional and does not need to be
352 void (*emit_prologue
)(struct lp_build_tgsi_context
*);
354 /** This function allows the user to insert some instructions at the end of
355 * the program. This callback is intended to be used for emitting
356 * instructions to handle the export for the output registers, but it can
357 * be used for any purpose. Implementing this function is optiona, but
360 void (*emit_epilogue
)(struct lp_build_tgsi_context
*);
363 struct lp_build_tgsi_soa_context
365 struct lp_build_tgsi_context bld_base
;
367 /* Builder for scalar elements of shader's data type (float) */
368 struct lp_build_context elem_bld
;
370 LLVMValueRef consts_ptr
;
371 const LLVMValueRef
*pos
;
372 const LLVMValueRef (*inputs
)[TGSI_NUM_CHANNELS
];
373 LLVMValueRef (*outputs
)[TGSI_NUM_CHANNELS
];
375 const struct lp_build_sampler_soa
*sampler
;
377 LLVMValueRef immediates
[LP_MAX_TGSI_IMMEDIATES
][TGSI_NUM_CHANNELS
];
378 LLVMValueRef temps
[LP_MAX_TGSI_TEMPS
][TGSI_NUM_CHANNELS
];
379 LLVMValueRef addr
[LP_MAX_TGSI_ADDRS
][TGSI_NUM_CHANNELS
];
380 LLVMValueRef preds
[LP_MAX_TGSI_PREDS
][TGSI_NUM_CHANNELS
];
382 /* We allocate/use this array of temps if (1 << TGSI_FILE_TEMPORARY) is
383 * set in the indirect_files field.
384 * The temps[] array above is unused then.
386 LLVMValueRef temps_array
;
388 /* We allocate/use this array of output if (1 << TGSI_FILE_OUTPUT) is
389 * set in the indirect_files field.
390 * The outputs[] array above is unused then.
392 LLVMValueRef outputs_array
;
394 /* We allocate/use this array of inputs if (1 << TGSI_FILE_INPUT) is
395 * set in the indirect_files field.
396 * The inputs[] array above is unused then.
398 LLVMValueRef inputs_array
;
400 struct lp_bld_tgsi_system_values system_values
;
402 /** bitmask indicating which register files are accessed indirectly */
403 unsigned indirect_files
;
405 struct lp_build_mask_context
*mask
;
406 struct lp_exec_mask exec_mask
;
413 lp_emit_declaration_soa(
414 struct lp_build_tgsi_context
*bld
,
415 const struct tgsi_full_declaration
*decl
);
417 void lp_emit_immediate_soa(
418 struct lp_build_tgsi_context
*bld_base
,
419 const struct tgsi_full_immediate
*imm
);
422 lp_emit_instruction_soa(
423 struct lp_build_tgsi_soa_context
*bld
,
424 const struct tgsi_full_instruction
*inst
,
425 const struct tgsi_opcode_info
*info
);
430 struct lp_build_tgsi_soa_context
*bld
,
436 struct lp_build_tgsi_soa_context
*bld
,
440 struct lp_build_tgsi_aos_context
442 struct lp_build_tgsi_context bld_base
;
444 /* Builder for integer masks and indices */
445 struct lp_build_context int_bld
;
449 * - swizzles[0] = red index
450 * - swizzles[1] = green index
451 * - swizzles[2] = blue index
452 * - swizzles[3] = alpha index
454 unsigned char swizzles
[4];
455 unsigned char inv_swizzles
[4];
457 LLVMValueRef consts_ptr
;
458 const LLVMValueRef
*inputs
;
459 LLVMValueRef
*outputs
;
461 struct lp_build_sampler_aos
*sampler
;
463 LLVMValueRef immediates
[LP_MAX_TGSI_IMMEDIATES
];
464 LLVMValueRef temps
[LP_MAX_TGSI_TEMPS
];
465 LLVMValueRef addr
[LP_MAX_TGSI_ADDRS
];
466 LLVMValueRef preds
[LP_MAX_TGSI_PREDS
];
468 /* We allocate/use this array of temps if (1 << TGSI_FILE_TEMPORARY) is
469 * set in the indirect_files field.
470 * The temps[] array above is unused then.
472 LLVMValueRef temps_array
;
474 /** bitmask indicating which register files are accessed indirectly */
475 unsigned indirect_files
;
479 static INLINE
struct lp_build_tgsi_soa_context
*
480 lp_soa_context(struct lp_build_tgsi_context
*bld_base
)
482 return (struct lp_build_tgsi_soa_context
*)bld_base
;
485 static INLINE
struct lp_build_tgsi_aos_context
*
486 lp_aos_context(struct lp_build_tgsi_context
*bld_base
)
488 return (struct lp_build_tgsi_aos_context
*)bld_base
;
492 lp_emit_declaration_aos(
493 struct lp_build_tgsi_aos_context
*bld
,
494 const struct tgsi_full_declaration
*decl
);
498 lp_emit_instruction_aos(
499 struct lp_build_tgsi_aos_context
*bld
,
500 const struct tgsi_full_instruction
*inst
,
501 const struct tgsi_opcode_info
*info
,
506 struct lp_build_tgsi_aos_context
*bld
,
507 const struct tgsi_full_instruction
*inst
,
511 void lp_build_fetch_args(
512 struct lp_build_tgsi_context
* bld_base
,
513 struct lp_build_emit_data
* emit_data
);
516 lp_build_tgsi_inst_llvm_aos(
517 struct lp_build_tgsi_context
* bld_base
,
518 const struct tgsi_full_instruction
*inst
);
521 lp_build_tgsi_intrinsic(
522 const struct lp_build_tgsi_action
* action
,
523 struct lp_build_tgsi_context
* bld_base
,
524 struct lp_build_emit_data
* emit_data
);
528 struct lp_build_tgsi_context
*bld_base
,
529 unsigned tgsi_opcode
,
530 struct lp_build_emit_data
* emit_data
);
533 lp_build_emit_llvm_unary(
534 struct lp_build_tgsi_context
*bld_base
,
535 unsigned tgsi_opcode
,
539 lp_build_emit_llvm_binary(
540 struct lp_build_tgsi_context
*bld_base
,
541 unsigned tgsi_opcode
,
546 lp_build_emit_llvm_ternary(
547 struct lp_build_tgsi_context
*bld_base
,
548 unsigned tgsi_opcode
,
554 lp_build_tgsi_inst_llvm(
555 struct lp_build_tgsi_context
* bld_base
,
556 const struct tgsi_full_instruction
*inst
);
560 struct lp_build_tgsi_context
*bld_base
,
561 const struct tgsi_full_instruction
*inst
,
563 const unsigned chan_index
);
567 lp_build_emit_fetch_texoffset(
568 struct lp_build_tgsi_context
*bld_base
,
569 const struct tgsi_full_instruction
*inst
,
571 const unsigned chan_index
);
575 struct lp_build_tgsi_context
* bld_base
,
576 const struct tgsi_token
*tokens
);
578 #endif /* LP_BLD_TGSI_H */