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 LLVMValueRef
*offsets
,
179 const struct lp_derivatives
*derivs
,
180 LLVMValueRef lod_bias
, /* optional */
181 LLVMValueRef explicit_lod
, /* optional */
182 LLVMValueRef
*texel
);
185 (*emit_size_query
)( const struct lp_build_sampler_soa
*sampler
,
186 struct gallivm_state
*gallivm
,
189 LLVMValueRef explicit_lod
, /* optional */
190 LLVMValueRef
*sizes_out
);
194 struct lp_build_sampler_aos
197 (*emit_fetch_texel
)( struct lp_build_sampler_aos
*sampler
,
198 struct lp_build_context
*bld
,
199 unsigned target
, /* TGSI_TEXTURE_* */
202 const struct lp_derivatives derivs
,
203 enum lp_build_tex_modifier modifier
);
208 lp_build_tgsi_info(const struct tgsi_token
*tokens
,
209 struct lp_tgsi_info
*info
);
213 lp_build_tgsi_soa(struct gallivm_state
*gallivm
,
214 const struct tgsi_token
*tokens
,
216 struct lp_build_mask_context
*mask
,
217 LLVMValueRef consts_ptr
,
218 const struct lp_bld_tgsi_system_values
*system_values
,
219 const LLVMValueRef
*pos
,
220 const LLVMValueRef (*inputs
)[4],
221 LLVMValueRef (*outputs
)[4],
222 struct lp_build_sampler_soa
*sampler
,
223 const struct tgsi_shader_info
*info
);
227 lp_build_tgsi_aos(struct gallivm_state
*gallivm
,
228 const struct tgsi_token
*tokens
,
230 const unsigned char swizzles
[4],
231 LLVMValueRef consts_ptr
,
232 const LLVMValueRef
*inputs
,
233 LLVMValueRef
*outputs
,
234 struct lp_build_sampler_aos
*sampler
,
235 const struct tgsi_shader_info
*info
);
238 struct lp_exec_mask
{
239 struct lp_build_context
*bld
;
243 LLVMTypeRef int_vec_type
;
245 LLVMValueRef cond_stack
[LP_MAX_TGSI_NESTING
];
247 LLVMValueRef cond_mask
;
249 LLVMBasicBlockRef loop_block
;
250 LLVMValueRef cont_mask
;
251 LLVMValueRef break_mask
;
252 LLVMValueRef break_var
;
254 LLVMBasicBlockRef loop_block
;
255 LLVMValueRef cont_mask
;
256 LLVMValueRef break_mask
;
257 LLVMValueRef break_var
;
258 } loop_stack
[LP_MAX_TGSI_NESTING
];
261 LLVMValueRef ret_mask
;
264 LLVMValueRef ret_mask
;
265 } call_stack
[LP_MAX_TGSI_NESTING
];
268 LLVMValueRef exec_mask
;
269 LLVMValueRef loop_limiter
;
272 struct lp_build_tgsi_inst_list
274 struct tgsi_full_instruction
*instructions
;
275 uint max_instructions
;
276 uint num_instructions
;
279 unsigned lp_bld_tgsi_list_init(struct lp_build_tgsi_context
* bld_base
);
282 unsigned lp_bld_tgsi_add_instruction(
283 struct lp_build_tgsi_context
* bld_base
,
284 struct tgsi_full_instruction
*inst_to_add
);
287 struct lp_build_tgsi_context
;
290 typedef LLVMValueRef (*lp_build_emit_fetch_fn
)(struct lp_build_tgsi_context
*,
291 const struct tgsi_full_src_register
*,
292 enum tgsi_opcode_type
,
295 struct lp_build_tgsi_context
297 struct lp_build_context base
;
299 struct lp_build_context uint_bld
;
300 struct lp_build_context int_bld
;
302 /** This array stores functions that are used to transform TGSI opcodes to
305 struct lp_build_tgsi_action op_actions
[TGSI_OPCODE_LAST
];
307 /* TGSI_OPCODE_RSQ is defined as 1 / sqrt( abs(src0.x) ), rsq_action
308 * should compute 1 / sqrt (src0.x) */
309 struct lp_build_tgsi_action rsq_action
;
311 const struct tgsi_shader_info
*info
;
313 lp_build_emit_fetch_fn emit_fetch_funcs
[TGSI_FILE_COUNT
];
315 LLVMValueRef (*emit_swizzle
)(struct lp_build_tgsi_context
*,
316 LLVMValueRef
, unsigned, unsigned, unsigned, unsigned);
318 void (*emit_store
)(struct lp_build_tgsi_context
*,
319 const struct tgsi_full_instruction
*,
320 const struct tgsi_opcode_info
*,
321 LLVMValueRef dst
[4]);
323 void (*emit_declaration
)(struct lp_build_tgsi_context
*,
324 const struct tgsi_full_declaration
*decl
);
326 void (*emit_immediate
)(struct lp_build_tgsi_context
*,
327 const struct tgsi_full_immediate
*imm
);
330 /* Allow the user to store data in this structure rather than passing it
331 * to every function. */
338 struct tgsi_full_instruction
*instructions
;
339 uint max_instructions
;
340 uint num_instructions
;
342 /** This function allows the user to insert some instructions at the
343 * beginning of the program. It is optional and does not need to be
346 void (*emit_prologue
)(struct lp_build_tgsi_context
*);
348 /** This function allows the user to insert some instructions at the end of
349 * the program. This callback is intended to be used for emitting
350 * instructions to handle the export for the output registers, but it can
351 * be used for any purpose. Implementing this function is optiona, but
354 void (*emit_epilogue
)(struct lp_build_tgsi_context
*);
357 struct lp_build_tgsi_soa_context
359 struct lp_build_tgsi_context bld_base
;
361 /* Builder for scalar elements of shader's data type (float) */
362 struct lp_build_context elem_bld
;
364 LLVMValueRef consts_ptr
;
365 const LLVMValueRef
*pos
;
366 const LLVMValueRef (*inputs
)[TGSI_NUM_CHANNELS
];
367 LLVMValueRef (*outputs
)[TGSI_NUM_CHANNELS
];
369 const struct lp_build_sampler_soa
*sampler
;
371 LLVMValueRef immediates
[LP_MAX_TGSI_IMMEDIATES
][TGSI_NUM_CHANNELS
];
372 LLVMValueRef temps
[LP_MAX_TGSI_TEMPS
][TGSI_NUM_CHANNELS
];
373 LLVMValueRef addr
[LP_MAX_TGSI_ADDRS
][TGSI_NUM_CHANNELS
];
374 LLVMValueRef preds
[LP_MAX_TGSI_PREDS
][TGSI_NUM_CHANNELS
];
376 /* We allocate/use this array of temps if (1 << TGSI_FILE_TEMPORARY) is
377 * set in the indirect_files field.
378 * The temps[] array above is unused then.
380 LLVMValueRef temps_array
;
382 /* We allocate/use this array of output if (1 << TGSI_FILE_OUTPUT) is
383 * set in the indirect_files field.
384 * The outputs[] array above is unused then.
386 LLVMValueRef outputs_array
;
388 /* We allocate/use this array of inputs if (1 << TGSI_FILE_INPUT) is
389 * set in the indirect_files field.
390 * The inputs[] array above is unused then.
392 LLVMValueRef inputs_array
;
394 struct lp_bld_tgsi_system_values system_values
;
396 /** bitmask indicating which register files are accessed indirectly */
397 unsigned indirect_files
;
399 struct lp_build_mask_context
*mask
;
400 struct lp_exec_mask exec_mask
;
407 lp_emit_declaration_soa(
408 struct lp_build_tgsi_context
*bld
,
409 const struct tgsi_full_declaration
*decl
);
411 void lp_emit_immediate_soa(
412 struct lp_build_tgsi_context
*bld_base
,
413 const struct tgsi_full_immediate
*imm
);
416 lp_emit_instruction_soa(
417 struct lp_build_tgsi_soa_context
*bld
,
418 const struct tgsi_full_instruction
*inst
,
419 const struct tgsi_opcode_info
*info
);
424 struct lp_build_tgsi_soa_context
*bld
,
430 struct lp_build_tgsi_soa_context
*bld
,
434 struct lp_build_tgsi_aos_context
436 struct lp_build_tgsi_context bld_base
;
438 /* Builder for integer masks and indices */
439 struct lp_build_context int_bld
;
443 * - swizzles[0] = red index
444 * - swizzles[1] = green index
445 * - swizzles[2] = blue index
446 * - swizzles[3] = alpha index
448 unsigned char swizzles
[4];
449 unsigned char inv_swizzles
[4];
451 LLVMValueRef consts_ptr
;
452 const LLVMValueRef
*inputs
;
453 LLVMValueRef
*outputs
;
455 struct lp_build_sampler_aos
*sampler
;
457 LLVMValueRef immediates
[LP_MAX_TGSI_IMMEDIATES
];
458 LLVMValueRef temps
[LP_MAX_TGSI_TEMPS
];
459 LLVMValueRef addr
[LP_MAX_TGSI_ADDRS
];
460 LLVMValueRef preds
[LP_MAX_TGSI_PREDS
];
462 /* We allocate/use this array of temps if (1 << TGSI_FILE_TEMPORARY) is
463 * set in the indirect_files field.
464 * The temps[] array above is unused then.
466 LLVMValueRef temps_array
;
468 /** bitmask indicating which register files are accessed indirectly */
469 unsigned indirect_files
;
473 static INLINE
struct lp_build_tgsi_soa_context
*
474 lp_soa_context(struct lp_build_tgsi_context
*bld_base
)
476 return (struct lp_build_tgsi_soa_context
*)bld_base
;
479 static INLINE
struct lp_build_tgsi_aos_context
*
480 lp_aos_context(struct lp_build_tgsi_context
*bld_base
)
482 return (struct lp_build_tgsi_aos_context
*)bld_base
;
486 lp_emit_declaration_aos(
487 struct lp_build_tgsi_aos_context
*bld
,
488 const struct tgsi_full_declaration
*decl
);
492 lp_emit_instruction_aos(
493 struct lp_build_tgsi_aos_context
*bld
,
494 const struct tgsi_full_instruction
*inst
,
495 const struct tgsi_opcode_info
*info
,
500 struct lp_build_tgsi_aos_context
*bld
,
501 const struct tgsi_full_instruction
*inst
,
505 void lp_build_fetch_args(
506 struct lp_build_tgsi_context
* bld_base
,
507 struct lp_build_emit_data
* emit_data
);
510 lp_build_tgsi_inst_llvm_aos(
511 struct lp_build_tgsi_context
* bld_base
,
512 const struct tgsi_full_instruction
*inst
);
515 lp_build_tgsi_intrinsic(
516 const struct lp_build_tgsi_action
* action
,
517 struct lp_build_tgsi_context
* bld_base
,
518 struct lp_build_emit_data
* emit_data
);
522 struct lp_build_tgsi_context
*bld_base
,
523 unsigned tgsi_opcode
,
524 struct lp_build_emit_data
* emit_data
);
527 lp_build_emit_llvm_unary(
528 struct lp_build_tgsi_context
*bld_base
,
529 unsigned tgsi_opcode
,
533 lp_build_emit_llvm_binary(
534 struct lp_build_tgsi_context
*bld_base
,
535 unsigned tgsi_opcode
,
540 lp_build_emit_llvm_ternary(
541 struct lp_build_tgsi_context
*bld_base
,
542 unsigned tgsi_opcode
,
548 lp_build_tgsi_inst_llvm(
549 struct lp_build_tgsi_context
* bld_base
,
550 const struct tgsi_full_instruction
*inst
);
554 struct lp_build_tgsi_context
*bld_base
,
555 const struct tgsi_full_instruction
*inst
,
557 const unsigned chan_index
);
561 lp_build_emit_fetch_texoffset(
562 struct lp_build_tgsi_context
*bld_base
,
563 const struct tgsi_full_instruction
*inst
,
565 const unsigned chan_index
);
569 struct lp_build_tgsi_context
* bld_base
,
570 const struct tgsi_token
*tokens
);
572 #endif /* LP_BLD_TGSI_H */