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
;
64 struct lp_build_tgsi_gs_iface
;
67 enum lp_build_tex_modifier
{
68 LP_BLD_TEX_MODIFIER_NONE
= 0,
69 LP_BLD_TEX_MODIFIER_PROJECTED
,
70 LP_BLD_TEX_MODIFIER_LOD_BIAS
,
71 LP_BLD_TEX_MODIFIER_EXPLICIT_LOD
,
72 LP_BLD_TEX_MODIFIER_EXPLICIT_DERIV
,
73 LP_BLD_TEX_MODIFIER_LOD_ZERO
78 * Describe a channel of a register.
81 * - immediate value (i.e. derived from a IMM register)
84 * - undetermined (when .file == TGSI_FILE_NULL)
86 * This is one of the analysis results, and is used to described
87 * the output color in terms of inputs.
89 struct lp_tgsi_channel_info
91 unsigned file
:4; /* TGSI_FILE_* */
92 unsigned swizzle
:3; /* PIPE_SWIZZLE_x */
95 float value
; /* for TGSI_FILE_IMMEDIATE */
101 * Describe a texture sampler interpolator.
103 * The interpolation is described in terms of regular inputs.
105 struct lp_tgsi_texture_info
107 struct lp_tgsi_channel_info coord
[4];
108 unsigned target
:8; /* TGSI_TEXTURE_* */
109 unsigned sampler_unit
:8; /* Sampler unit */
110 unsigned texture_unit
:8; /* Texture unit */
111 unsigned modifier
:8; /* LP_BLD_TEX_MODIFIER_* */
117 struct tgsi_shader_info base
;
120 * Whether any of the texture opcodes access a register file other than
123 * We could also handle TGSI_FILE_CONST/IMMEDIATE here, but there is little
126 unsigned indirect_textures
:1;
129 * Whether any immediate values are outside the range of 0 and 1
131 unsigned unclamped_immediates
:1;
134 * Texture opcode description. Aimed at detecting and described direct
138 struct lp_tgsi_texture_info tex
[PIPE_MAX_SAMPLERS
];
141 * Output description. Aimed at detecting and describing simple blit
144 struct lp_tgsi_channel_info output
[PIPE_MAX_SHADER_OUTPUTS
][4];
147 * Shortcut pointers into the above (for fragment shaders).
149 const struct lp_tgsi_channel_info
*cbuf
[PIPE_MAX_COLOR_BUFS
];
153 * Reference to system values.
155 struct lp_bld_tgsi_system_values
{
156 LLVMValueRef instance_id
;
157 LLVMValueRef vertex_id
;
158 LLVMValueRef prim_id
;
163 * Sampler code generation interface.
165 * Although texture sampling is a requirement for TGSI translation, it is
166 * a very different problem with several different approaches to it. This
167 * structure establishes an interface for texture sampling code generation, so
168 * that we can easily use different texture sampling strategies.
170 struct lp_build_sampler_soa
173 (*destroy
)( struct lp_build_sampler_soa
*sampler
);
176 (*emit_fetch_texel
)( const struct lp_build_sampler_soa
*sampler
,
177 struct gallivm_state
*gallivm
,
180 unsigned texture_index
,
181 unsigned sampler_index
,
182 const LLVMValueRef
*coords
,
183 const LLVMValueRef
*offsets
,
184 const struct lp_derivatives
*derivs
,
185 LLVMValueRef lod_bias
, /* optional */
186 LLVMValueRef explicit_lod
, /* optional */
187 LLVMValueRef
*texel
);
190 (*emit_size_query
)( const struct lp_build_sampler_soa
*sampler
,
191 struct gallivm_state
*gallivm
,
194 boolean need_nr_mips
,
195 LLVMValueRef explicit_lod
, /* optional */
196 LLVMValueRef
*sizes_out
);
200 struct lp_build_sampler_aos
203 (*emit_fetch_texel
)( struct lp_build_sampler_aos
*sampler
,
204 struct lp_build_context
*bld
,
205 unsigned target
, /* TGSI_TEXTURE_* */
208 const struct lp_derivatives derivs
,
209 enum lp_build_tex_modifier modifier
);
214 lp_build_tgsi_info(const struct tgsi_token
*tokens
,
215 struct lp_tgsi_info
*info
);
219 lp_build_tgsi_soa(struct gallivm_state
*gallivm
,
220 const struct tgsi_token
*tokens
,
222 struct lp_build_mask_context
*mask
,
223 LLVMValueRef consts_ptr
,
224 const struct lp_bld_tgsi_system_values
*system_values
,
225 const LLVMValueRef
*pos
,
226 const LLVMValueRef (*inputs
)[4],
227 LLVMValueRef (*outputs
)[4],
228 struct lp_build_sampler_soa
*sampler
,
229 const struct tgsi_shader_info
*info
,
230 const struct lp_build_tgsi_gs_iface
*gs_iface
);
234 lp_build_tgsi_aos(struct gallivm_state
*gallivm
,
235 const struct tgsi_token
*tokens
,
237 const unsigned char swizzles
[4],
238 LLVMValueRef consts_ptr
,
239 const LLVMValueRef
*inputs
,
240 LLVMValueRef
*outputs
,
241 struct lp_build_sampler_aos
*sampler
,
242 const struct tgsi_shader_info
*info
);
245 struct lp_exec_mask
{
246 struct lp_build_context
*bld
;
251 LLVMTypeRef int_vec_type
;
253 LLVMValueRef cond_stack
[LP_MAX_TGSI_NESTING
];
255 LLVMValueRef cond_mask
;
257 LLVMBasicBlockRef loop_block
;
258 LLVMValueRef cont_mask
;
259 LLVMValueRef break_mask
;
260 LLVMValueRef break_var
;
262 LLVMBasicBlockRef loop_block
;
263 LLVMValueRef cont_mask
;
264 LLVMValueRef break_mask
;
265 LLVMValueRef break_var
;
266 } loop_stack
[LP_MAX_TGSI_NESTING
];
269 LLVMValueRef ret_mask
;
272 LLVMValueRef ret_mask
;
273 } call_stack
[LP_MAX_TGSI_NESTING
];
276 LLVMValueRef exec_mask
;
277 LLVMValueRef loop_limiter
;
280 struct lp_build_tgsi_inst_list
282 struct tgsi_full_instruction
*instructions
;
283 uint max_instructions
;
284 uint num_instructions
;
287 unsigned lp_bld_tgsi_list_init(struct lp_build_tgsi_context
* bld_base
);
290 unsigned lp_bld_tgsi_add_instruction(
291 struct lp_build_tgsi_context
* bld_base
,
292 struct tgsi_full_instruction
*inst_to_add
);
295 struct lp_build_tgsi_context
;
298 typedef LLVMValueRef (*lp_build_emit_fetch_fn
)(struct lp_build_tgsi_context
*,
299 const struct tgsi_full_src_register
*,
300 enum tgsi_opcode_type
,
303 struct lp_build_tgsi_context
305 struct lp_build_context base
;
307 struct lp_build_context uint_bld
;
308 struct lp_build_context int_bld
;
310 /** This array stores functions that are used to transform TGSI opcodes to
313 struct lp_build_tgsi_action op_actions
[TGSI_OPCODE_LAST
];
315 /* TGSI_OPCODE_RSQ is defined as 1 / sqrt( abs(src0.x) ), rsq_action
316 * should compute 1 / sqrt (src0.x) */
317 struct lp_build_tgsi_action rsq_action
;
319 struct lp_build_tgsi_action sqrt_action
;
321 const struct tgsi_shader_info
*info
;
323 lp_build_emit_fetch_fn emit_fetch_funcs
[TGSI_FILE_COUNT
];
325 LLVMValueRef (*emit_swizzle
)(struct lp_build_tgsi_context
*,
326 LLVMValueRef
, unsigned, unsigned, unsigned, unsigned);
328 void (*emit_store
)(struct lp_build_tgsi_context
*,
329 const struct tgsi_full_instruction
*,
330 const struct tgsi_opcode_info
*,
331 LLVMValueRef dst
[4]);
333 void (*emit_declaration
)(struct lp_build_tgsi_context
*,
334 const struct tgsi_full_declaration
*decl
);
336 void (*emit_immediate
)(struct lp_build_tgsi_context
*,
337 const struct tgsi_full_immediate
*imm
);
340 /* Allow the user to store data in this structure rather than passing it
341 * to every function. */
348 struct tgsi_full_instruction
*instructions
;
349 uint max_instructions
;
350 uint num_instructions
;
352 /** This function allows the user to insert some instructions at the
353 * beginning of the program. It is optional and does not need to be
356 void (*emit_prologue
)(struct lp_build_tgsi_context
*);
358 /** This function allows the user to insert some instructions at the end of
359 * the program. This callback is intended to be used for emitting
360 * instructions to handle the export for the output registers, but it can
361 * be used for any purpose. Implementing this function is optiona, but
364 void (*emit_epilogue
)(struct lp_build_tgsi_context
*);
367 struct lp_build_tgsi_gs_iface
369 LLVMValueRef (*fetch_input
)(const struct lp_build_tgsi_gs_iface
*gs_iface
,
370 struct lp_build_tgsi_context
* bld_base
,
371 LLVMValueRef vertex_index
,
372 LLVMValueRef attrib_index
,
373 LLVMValueRef swizzle_index
);
374 void (*emit_vertex
)(const struct lp_build_tgsi_gs_iface
*gs_iface
,
375 struct lp_build_tgsi_context
* bld_base
,
376 LLVMValueRef (*outputs
)[4],
377 LLVMValueRef emitted_vertices_vec
);
378 void (*end_primitive
)(const struct lp_build_tgsi_gs_iface
*gs_iface
,
379 struct lp_build_tgsi_context
* bld_base
,
380 LLVMValueRef verts_per_prim_vec
,
381 LLVMValueRef emitted_prims_vec
);
382 void (*gs_epilogue
)(const struct lp_build_tgsi_gs_iface
*gs_iface
,
383 struct lp_build_tgsi_context
* bld_base
,
384 LLVMValueRef total_emitted_vertices_vec
,
385 LLVMValueRef emitted_prims_vec
);
388 struct lp_build_tgsi_soa_context
390 struct lp_build_tgsi_context bld_base
;
392 /* Builder for scalar elements of shader's data type (float) */
393 struct lp_build_context elem_bld
;
395 const struct lp_build_tgsi_gs_iface
*gs_iface
;
396 LLVMValueRef emitted_prims_vec_ptr
;
397 LLVMValueRef total_emitted_vertices_vec_ptr
;
398 LLVMValueRef emitted_vertices_vec_ptr
;
399 /* if a shader doesn't have ENDPRIM instruction but it has
400 * a number of EMIT instructions it means the END instruction
401 * implicitly invokes ENDPRIM. handle this via a flag here
402 * in the future maybe we can enforce TGSI to always have
403 * an explicit ENDPRIM */
404 boolean pending_end_primitive
;
406 LLVMValueRef consts_ptr
;
407 const LLVMValueRef
*pos
;
408 const LLVMValueRef (*inputs
)[TGSI_NUM_CHANNELS
];
409 LLVMValueRef (*outputs
)[TGSI_NUM_CHANNELS
];
411 const struct lp_build_sampler_soa
*sampler
;
413 struct tgsi_declaration_sampler_view sv
[PIPE_MAX_SHADER_SAMPLER_VIEWS
];
415 LLVMValueRef immediates
[LP_MAX_TGSI_IMMEDIATES
][TGSI_NUM_CHANNELS
];
416 LLVMValueRef temps
[LP_MAX_TGSI_TEMPS
][TGSI_NUM_CHANNELS
];
417 LLVMValueRef addr
[LP_MAX_TGSI_ADDRS
][TGSI_NUM_CHANNELS
];
418 LLVMValueRef preds
[LP_MAX_TGSI_PREDS
][TGSI_NUM_CHANNELS
];
420 /* We allocate/use this array of temps if (1 << TGSI_FILE_TEMPORARY) is
421 * set in the indirect_files field.
422 * The temps[] array above is unused then.
424 LLVMValueRef temps_array
;
426 /* We allocate/use this array of output if (1 << TGSI_FILE_OUTPUT) is
427 * set in the indirect_files field.
428 * The outputs[] array above is unused then.
430 LLVMValueRef outputs_array
;
432 /* We allocate/use this array of inputs if (1 << TGSI_FILE_INPUT) is
433 * set in the indirect_files field.
434 * The inputs[] array above is unused then.
436 LLVMValueRef inputs_array
;
438 struct lp_bld_tgsi_system_values system_values
;
440 /** bitmask indicating which register files are accessed indirectly */
441 unsigned indirect_files
;
443 struct lp_build_mask_context
*mask
;
444 struct lp_exec_mask exec_mask
;
451 lp_emit_declaration_soa(
452 struct lp_build_tgsi_context
*bld
,
453 const struct tgsi_full_declaration
*decl
);
455 void lp_emit_immediate_soa(
456 struct lp_build_tgsi_context
*bld_base
,
457 const struct tgsi_full_immediate
*imm
);
460 lp_emit_instruction_soa(
461 struct lp_build_tgsi_soa_context
*bld
,
462 const struct tgsi_full_instruction
*inst
,
463 const struct tgsi_opcode_info
*info
);
468 struct lp_build_tgsi_soa_context
*bld
,
474 struct lp_build_tgsi_soa_context
*bld
,
478 struct lp_build_tgsi_aos_context
480 struct lp_build_tgsi_context bld_base
;
482 /* Builder for integer masks and indices */
483 struct lp_build_context int_bld
;
487 * - swizzles[0] = red index
488 * - swizzles[1] = green index
489 * - swizzles[2] = blue index
490 * - swizzles[3] = alpha index
492 unsigned char swizzles
[4];
493 unsigned char inv_swizzles
[4];
495 LLVMValueRef consts_ptr
;
496 const LLVMValueRef
*inputs
;
497 LLVMValueRef
*outputs
;
499 struct lp_build_sampler_aos
*sampler
;
501 LLVMValueRef immediates
[LP_MAX_TGSI_IMMEDIATES
];
502 LLVMValueRef temps
[LP_MAX_TGSI_TEMPS
];
503 LLVMValueRef addr
[LP_MAX_TGSI_ADDRS
];
504 LLVMValueRef preds
[LP_MAX_TGSI_PREDS
];
506 /* We allocate/use this array of temps if (1 << TGSI_FILE_TEMPORARY) is
507 * set in the indirect_files field.
508 * The temps[] array above is unused then.
510 LLVMValueRef temps_array
;
512 /** bitmask indicating which register files are accessed indirectly */
513 unsigned indirect_files
;
517 static INLINE
struct lp_build_tgsi_soa_context
*
518 lp_soa_context(struct lp_build_tgsi_context
*bld_base
)
520 return (struct lp_build_tgsi_soa_context
*)bld_base
;
523 static INLINE
struct lp_build_tgsi_aos_context
*
524 lp_aos_context(struct lp_build_tgsi_context
*bld_base
)
526 return (struct lp_build_tgsi_aos_context
*)bld_base
;
530 lp_emit_declaration_aos(
531 struct lp_build_tgsi_aos_context
*bld
,
532 const struct tgsi_full_declaration
*decl
);
536 lp_emit_instruction_aos(
537 struct lp_build_tgsi_aos_context
*bld
,
538 const struct tgsi_full_instruction
*inst
,
539 const struct tgsi_opcode_info
*info
,
544 struct lp_build_tgsi_aos_context
*bld
,
545 const struct tgsi_full_instruction
*inst
,
549 void lp_build_fetch_args(
550 struct lp_build_tgsi_context
* bld_base
,
551 struct lp_build_emit_data
* emit_data
);
554 lp_build_tgsi_inst_llvm_aos(
555 struct lp_build_tgsi_context
* bld_base
,
556 const struct tgsi_full_instruction
*inst
);
559 lp_build_tgsi_intrinsic(
560 const struct lp_build_tgsi_action
* action
,
561 struct lp_build_tgsi_context
* bld_base
,
562 struct lp_build_emit_data
* emit_data
);
566 struct lp_build_tgsi_context
*bld_base
,
567 unsigned tgsi_opcode
,
568 struct lp_build_emit_data
* emit_data
);
571 lp_build_emit_llvm_unary(
572 struct lp_build_tgsi_context
*bld_base
,
573 unsigned tgsi_opcode
,
577 lp_build_emit_llvm_binary(
578 struct lp_build_tgsi_context
*bld_base
,
579 unsigned tgsi_opcode
,
584 lp_build_emit_llvm_ternary(
585 struct lp_build_tgsi_context
*bld_base
,
586 unsigned tgsi_opcode
,
592 lp_build_tgsi_inst_llvm(
593 struct lp_build_tgsi_context
* bld_base
,
594 const struct tgsi_full_instruction
*inst
);
598 struct lp_build_tgsi_context
*bld_base
,
599 const struct tgsi_full_instruction
*inst
,
601 const unsigned chan_index
);
605 lp_build_emit_fetch_texoffset(
606 struct lp_build_tgsi_context
*bld_base
,
607 const struct tgsi_full_instruction
*inst
,
609 const unsigned chan_index
);
613 struct lp_build_tgsi_context
* bld_base
,
614 const struct tgsi_token
*tokens
);
616 #endif /* LP_BLD_TGSI_H */