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 "gallivm/lp_bld_sample.h"
44 #include "gallivm/lp_bld_ir_common.h"
45 #include "lp_bld_type.h"
46 #include "pipe/p_compiler.h"
47 #include "pipe/p_state.h"
48 #include "tgsi/tgsi_exec.h"
49 #include "tgsi/tgsi_scan.h"
50 #include "tgsi/tgsi_info.h"
56 #define LP_CHAN_ALL ~0u
58 #define LP_MAX_INSTRUCTIONS 256
60 struct tgsi_full_declaration
;
61 struct tgsi_full_immediate
;
62 struct tgsi_full_instruction
;
63 struct tgsi_full_src_register
;
64 struct tgsi_full_dst_register
;
65 struct tgsi_opcode_info
;
67 struct tgsi_shader_info
;
68 struct lp_build_mask_context
;
70 struct lp_derivatives
;
71 struct lp_build_gs_iface
;
73 enum lp_build_tex_modifier
{
74 LP_BLD_TEX_MODIFIER_NONE
= 0,
75 LP_BLD_TEX_MODIFIER_PROJECTED
,
76 LP_BLD_TEX_MODIFIER_LOD_BIAS
,
77 LP_BLD_TEX_MODIFIER_EXPLICIT_LOD
,
78 LP_BLD_TEX_MODIFIER_EXPLICIT_DERIV
,
79 LP_BLD_TEX_MODIFIER_LOD_ZERO
84 * Describe a channel of a register.
87 * - immediate value (i.e. derived from a IMM register)
90 * - undetermined (when .file == TGSI_FILE_NULL)
92 * This is one of the analysis results, and is used to described
93 * the output color in terms of inputs.
95 struct lp_tgsi_channel_info
97 unsigned file
:4; /* TGSI_FILE_* */
98 unsigned swizzle
:3; /* PIPE_SWIZZLE_x */
101 float value
; /* for TGSI_FILE_IMMEDIATE */
107 * Describe a texture sampler interpolator.
109 * The interpolation is described in terms of regular inputs.
111 struct lp_tgsi_texture_info
113 struct lp_tgsi_channel_info coord
[4];
114 unsigned target
:8; /* TGSI_TEXTURE_* */
115 unsigned sampler_unit
:8; /* Sampler unit */
116 unsigned texture_unit
:8; /* Texture unit */
117 unsigned modifier
:8; /* LP_BLD_TEX_MODIFIER_* */
123 struct tgsi_shader_info base
;
126 * Whether any of the texture opcodes access a register file other than
129 * We could also handle TGSI_FILE_CONST/IMMEDIATE here, but there is little
132 unsigned indirect_textures
:1;
135 * Whether any of the texture (sample) ocpodes use different sampler
136 * and sampler view unit.
138 unsigned sampler_texture_units_different
:1;
141 * Whether any immediate values are outside the range of 0 and 1
143 unsigned unclamped_immediates
:1;
146 * Texture opcode description. Aimed at detecting and described direct
150 struct lp_tgsi_texture_info tex
[PIPE_MAX_SAMPLERS
];
153 * Output description. Aimed at detecting and describing simple blit
156 struct lp_tgsi_channel_info output
[PIPE_MAX_SHADER_OUTPUTS
][4];
159 * Shortcut pointers into the above (for fragment shaders).
161 const struct lp_tgsi_channel_info
*cbuf
[PIPE_MAX_COLOR_BUFS
];
165 * Reference to system values.
167 struct lp_bld_tgsi_system_values
{
168 LLVMValueRef instance_id
;
169 LLVMValueRef base_instance
;
170 LLVMValueRef vertex_id
;
171 LLVMValueRef vertex_id_nobase
;
172 LLVMValueRef prim_id
;
173 LLVMValueRef basevertex
;
174 LLVMValueRef invocation_id
;
175 LLVMValueRef draw_id
;
176 LLVMValueRef thread_id
;
177 LLVMValueRef block_id
;
178 LLVMValueRef grid_size
;
179 LLVMValueRef front_facing
;
180 LLVMValueRef work_dim
;
181 LLVMValueRef block_size
;
182 LLVMValueRef tess_coord
;
183 LLVMValueRef tess_outer
;
184 LLVMValueRef tess_inner
;
185 LLVMValueRef vertices_in
;
190 * Sampler code generation interface.
192 * Although texture sampling is a requirement for TGSI translation, it is
193 * a very different problem with several different approaches to it. This
194 * structure establishes an interface for texture sampling code generation, so
195 * that we can easily use different texture sampling strategies.
197 struct lp_build_sampler_soa
200 (*destroy
)( struct lp_build_sampler_soa
*sampler
);
203 (*emit_tex_sample
)(const struct lp_build_sampler_soa
*sampler
,
204 struct gallivm_state
*gallivm
,
205 const struct lp_sampler_params
*params
);
208 (*emit_size_query
)( const struct lp_build_sampler_soa
*sampler
,
209 struct gallivm_state
*gallivm
,
210 const struct lp_sampler_size_query_params
*params
);
214 struct lp_build_sampler_aos
217 (*emit_fetch_texel
)( const struct lp_build_sampler_aos
*sampler
,
218 struct lp_build_context
*bld
,
219 unsigned target
, /* TGSI_TEXTURE_* */
222 const struct lp_derivatives derivs
,
223 enum lp_build_tex_modifier modifier
);
226 struct lp_img_params
;
228 struct lp_build_image_soa
231 (*destroy
)( struct lp_build_image_soa
*image
);
234 (*emit_op
)(const struct lp_build_image_soa
*image
,
235 struct gallivm_state
*gallivm
,
236 const struct lp_img_params
*params
);
239 (*emit_size_query
)( const struct lp_build_image_soa
*sampler
,
240 struct gallivm_state
*gallivm
,
241 const struct lp_sampler_size_query_params
*params
);
245 lp_build_tgsi_info(const struct tgsi_token
*tokens
,
246 struct lp_tgsi_info
*info
);
249 struct lp_build_tgsi_params
{
251 struct lp_build_mask_context
*mask
;
252 LLVMValueRef consts_ptr
;
253 LLVMValueRef const_sizes_ptr
;
254 const struct lp_bld_tgsi_system_values
*system_values
;
255 const LLVMValueRef (*inputs
)[4];
256 LLVMValueRef context_ptr
;
257 LLVMValueRef thread_data_ptr
;
258 const struct lp_build_sampler_soa
*sampler
;
259 const struct tgsi_shader_info
*info
;
260 const struct lp_build_gs_iface
*gs_iface
;
261 const struct lp_build_tcs_iface
*tcs_iface
;
262 const struct lp_build_tes_iface
*tes_iface
;
263 LLVMValueRef ssbo_ptr
;
264 LLVMValueRef ssbo_sizes_ptr
;
265 const struct lp_build_image_soa
*image
;
266 LLVMValueRef shared_ptr
;
267 const struct lp_build_coro_suspend_info
*coro
;
268 LLVMValueRef kernel_args
;
272 lp_build_tgsi_soa(struct gallivm_state
*gallivm
,
273 const struct tgsi_token
*tokens
,
274 const struct lp_build_tgsi_params
*params
,
275 LLVMValueRef (*outputs
)[4]);
278 lp_build_tgsi_aos(struct gallivm_state
*gallivm
,
279 const struct tgsi_token
*tokens
,
281 const unsigned char swizzles
[4],
282 LLVMValueRef consts_ptr
,
283 const LLVMValueRef
*inputs
,
284 LLVMValueRef
*outputs
,
285 const struct lp_build_sampler_aos
*sampler
,
286 const struct tgsi_shader_info
*info
);
289 struct lp_build_tgsi_inst_list
291 struct tgsi_full_instruction
*instructions
;
292 uint max_instructions
;
293 uint num_instructions
;
296 unsigned lp_bld_tgsi_list_init(struct lp_build_tgsi_context
* bld_base
);
299 unsigned lp_bld_tgsi_add_instruction(
300 struct lp_build_tgsi_context
* bld_base
,
301 const struct tgsi_full_instruction
*inst_to_add
);
304 struct lp_build_tgsi_context
;
307 typedef LLVMValueRef (*lp_build_emit_fetch_fn
)(struct lp_build_tgsi_context
*,
308 const struct tgsi_full_src_register
*,
309 enum tgsi_opcode_type
,
312 typedef void (*lp_build_emit_store_reg_fn
)(struct lp_build_tgsi_context
*,
313 enum tgsi_opcode_type
,
314 const struct tgsi_full_dst_register
*,
320 struct lp_build_tgsi_context
322 struct lp_build_context base
;
324 struct lp_build_context uint_bld
;
325 struct lp_build_context int_bld
;
327 struct lp_build_context dbl_bld
;
329 struct lp_build_context uint64_bld
;
330 struct lp_build_context int64_bld
;
332 /** This array stores functions that are used to transform TGSI opcodes to
335 struct lp_build_tgsi_action op_actions
[TGSI_OPCODE_LAST
];
337 /* TGSI_OPCODE_RSQ is defined as 1 / sqrt( abs(src0.x) ), rsq_action
338 * should compute 1 / sqrt (src0.x) */
339 struct lp_build_tgsi_action rsq_action
;
341 struct lp_build_tgsi_action sqrt_action
;
343 struct lp_build_tgsi_action drsq_action
;
345 struct lp_build_tgsi_action dsqrt_action
;
346 const struct tgsi_shader_info
*info
;
348 lp_build_emit_fetch_fn emit_fetch_funcs
[TGSI_FILE_COUNT
];
349 lp_build_emit_store_reg_fn emit_store_reg_funcs
[TGSI_FILE_COUNT
];
351 LLVMValueRef (*emit_swizzle
)(struct lp_build_tgsi_context
*,
352 LLVMValueRef
, unsigned, unsigned, unsigned, unsigned);
355 void (*emit_debug
)(struct lp_build_tgsi_context
*,
356 const struct tgsi_full_instruction
*,
357 const struct tgsi_opcode_info
*);
359 void (*emit_store
)(struct lp_build_tgsi_context
*,
360 const struct tgsi_full_instruction
*,
361 const struct tgsi_opcode_info
*,
363 LLVMValueRef dst
[4]);
365 void (*emit_declaration
)(struct lp_build_tgsi_context
*,
366 const struct tgsi_full_declaration
*decl
);
368 void (*emit_immediate
)(struct lp_build_tgsi_context
*,
369 const struct tgsi_full_immediate
*imm
);
372 /* Allow the user to store data in this structure rather than passing it
373 * to every function. */
380 struct tgsi_full_instruction
*instructions
;
381 uint max_instructions
;
382 uint num_instructions
;
384 /** This function allows the user to insert some instructions at the
385 * beginning of the program. It is optional and does not need to be
388 void (*emit_prologue
)(struct lp_build_tgsi_context
*);
390 /** This function allows the user to insert some instructions after
391 * declarations section, but before any other code.
392 * It is optional and does not need to be implemented.
394 void (*emit_prologue_post_decl
)(struct lp_build_tgsi_context
*);
396 /** This function allows the user to insert some instructions at the end of
397 * the program. This callback is intended to be used for emitting
398 * instructions to handle the export for the output registers, but it can
399 * be used for any purpose. Implementing this function is optiona, but
402 void (*emit_epilogue
)(struct lp_build_tgsi_context
*);
405 struct lp_build_gs_iface
407 LLVMValueRef (*fetch_input
)(const struct lp_build_gs_iface
*gs_iface
,
408 struct lp_build_context
* bld
,
409 boolean is_vindex_indirect
,
410 LLVMValueRef vertex_index
,
411 boolean is_aindex_indirect
,
412 LLVMValueRef attrib_index
,
413 LLVMValueRef swizzle_index
);
414 void (*emit_vertex
)(const struct lp_build_gs_iface
*gs_iface
,
415 struct lp_build_context
* bld
,
416 LLVMValueRef (*outputs
)[4],
417 LLVMValueRef emitted_vertices_vec
,
418 LLVMValueRef stream_id
);
419 void (*end_primitive
)(const struct lp_build_gs_iface
*gs_iface
,
420 struct lp_build_context
* bld
,
421 LLVMValueRef total_emitted_vertices_vec
,
422 LLVMValueRef verts_per_prim_vec
,
423 LLVMValueRef emitted_prims_vec
,
424 LLVMValueRef mask_vec
);
425 void (*gs_epilogue
)(const struct lp_build_gs_iface
*gs_iface
,
426 LLVMValueRef total_emitted_vertices_vec
,
427 LLVMValueRef emitted_prims_vec
, unsigned stream
);
430 struct lp_build_tcs_iface
432 void (*emit_prologue
)(struct lp_build_context
* bld
);
433 void (*emit_epilogue
)(struct lp_build_context
* bld
);
434 void (*emit_barrier
)(struct lp_build_context
*bld_base
);
436 void (*emit_store_output
)(const struct lp_build_tcs_iface
*tcs_iface
,
437 struct lp_build_context
* bld
,
439 boolean is_vindex_indirect
,
440 LLVMValueRef vertex_index
,
441 boolean is_aindex_indirect
,
442 LLVMValueRef attrib_index
,
443 LLVMValueRef swizzle_index
,
446 LLVMValueRef (*emit_fetch_input
)(const struct lp_build_tcs_iface
*tcs_iface
,
447 struct lp_build_context
* bld
,
448 boolean is_vindex_indirect
,
449 LLVMValueRef vertex_index
,
450 boolean is_aindex_indirect
,
451 LLVMValueRef attrib_index
,
452 LLVMValueRef swizzle_index
);
454 LLVMValueRef (*emit_fetch_output
)(const struct lp_build_tcs_iface
*tcs_iface
,
455 struct lp_build_context
* bld
,
456 boolean is_vindex_indirect
,
457 LLVMValueRef vertex_index
,
458 boolean is_aindex_indirect
,
459 LLVMValueRef attrib_index
,
460 LLVMValueRef swizzle_index
,
464 struct lp_build_tes_iface
466 LLVMValueRef (*fetch_vertex_input
)(const struct lp_build_tes_iface
*tes_iface
,
467 struct lp_build_context
* bld
,
468 boolean is_vindex_indirect
,
469 LLVMValueRef vertex_index
,
470 boolean is_aindex_indirect
,
471 LLVMValueRef attrib_index
,
472 LLVMValueRef swizzle_index
);
474 LLVMValueRef (*fetch_patch_input
)(const struct lp_build_tes_iface
*tes_iface
,
475 struct lp_build_context
* bld
,
476 boolean is_aindex_indirect
,
477 LLVMValueRef attrib_index
,
478 LLVMValueRef swizzle_index
);
481 struct lp_build_tgsi_soa_context
483 struct lp_build_tgsi_context bld_base
;
485 /* Builder for scalar elements of shader's data type (float) */
486 struct lp_build_context elem_bld
;
488 const struct lp_build_gs_iface
*gs_iface
;
489 const struct lp_build_tcs_iface
*tcs_iface
;
490 const struct lp_build_tes_iface
*tes_iface
;
492 LLVMValueRef emitted_prims_vec_ptr
;
493 LLVMValueRef total_emitted_vertices_vec_ptr
;
494 LLVMValueRef emitted_vertices_vec_ptr
;
495 LLVMValueRef max_output_vertices_vec
;
497 LLVMValueRef consts_ptr
;
498 LLVMValueRef const_sizes_ptr
;
499 LLVMValueRef consts
[LP_MAX_TGSI_CONST_BUFFERS
];
500 LLVMValueRef consts_sizes
[LP_MAX_TGSI_CONST_BUFFERS
];
501 const LLVMValueRef (*inputs
)[TGSI_NUM_CHANNELS
];
502 LLVMValueRef (*outputs
)[TGSI_NUM_CHANNELS
];
503 LLVMValueRef context_ptr
;
504 LLVMValueRef thread_data_ptr
;
506 LLVMValueRef ssbo_ptr
;
507 LLVMValueRef ssbo_sizes_ptr
;
508 LLVMValueRef ssbos
[LP_MAX_TGSI_SHADER_BUFFERS
];
509 LLVMValueRef ssbo_sizes
[LP_MAX_TGSI_SHADER_BUFFERS
];
511 LLVMValueRef shared_ptr
;
513 const struct lp_build_coro_suspend_info
*coro
;
515 const struct lp_build_sampler_soa
*sampler
;
516 const struct lp_build_image_soa
*image
;
518 struct tgsi_declaration_sampler_view sv
[PIPE_MAX_SHADER_SAMPLER_VIEWS
];
520 LLVMValueRef immediates
[LP_MAX_INLINED_IMMEDIATES
][TGSI_NUM_CHANNELS
];
521 LLVMValueRef temps
[LP_MAX_INLINED_TEMPS
][TGSI_NUM_CHANNELS
];
522 LLVMValueRef addr
[LP_MAX_TGSI_ADDRS
][TGSI_NUM_CHANNELS
];
524 /* We allocate/use this array of temps if (1 << TGSI_FILE_TEMPORARY) is
525 * set in the indirect_files field.
526 * The temps[] array above is unused then.
528 LLVMValueRef temps_array
;
530 /* We allocate/use this array of output if (1 << TGSI_FILE_OUTPUT) is
531 * set in the indirect_files field.
532 * The outputs[] array above is unused then.
534 LLVMValueRef outputs_array
;
536 /* We allocate/use this array of inputs if (1 << TGSI_FILE_INPUT) is
537 * set in the indirect_files field.
538 * The inputs[] array above is unused then.
540 LLVMValueRef inputs_array
;
542 /* We allocate/use this array of temps if (1 << TGSI_FILE_IMMEDIATE) is
543 * set in the indirect_files field.
545 LLVMValueRef imms_array
;
548 struct lp_bld_tgsi_system_values system_values
;
550 /** bitmask indicating which register files are accessed indirectly */
551 unsigned indirect_files
;
553 struct lp_build_mask_context
*mask
;
554 struct lp_exec_mask exec_mask
;
557 boolean use_immediates_array
;
561 lp_emit_declaration_soa(
562 struct lp_build_tgsi_context
*bld
,
563 const struct tgsi_full_declaration
*decl
);
565 void lp_emit_immediate_soa(
566 struct lp_build_tgsi_context
*bld_base
,
567 const struct tgsi_full_immediate
*imm
);
570 lp_emit_instruction_soa(
571 struct lp_build_tgsi_soa_context
*bld
,
572 const struct tgsi_full_instruction
*inst
,
573 const struct tgsi_opcode_info
*info
);
578 struct lp_build_tgsi_soa_context
*bld
,
584 struct lp_build_tgsi_soa_context
*bld
,
588 struct lp_build_tgsi_aos_context
590 struct lp_build_tgsi_context bld_base
;
592 /* Builder for integer masks and indices */
593 struct lp_build_context int_bld
;
597 * - swizzles[0] = red index
598 * - swizzles[1] = green index
599 * - swizzles[2] = blue index
600 * - swizzles[3] = alpha index
602 unsigned char swizzles
[4];
603 unsigned char inv_swizzles
[4];
605 LLVMValueRef consts_ptr
;
606 const LLVMValueRef
*inputs
;
607 LLVMValueRef
*outputs
;
609 const struct lp_build_sampler_aos
*sampler
;
611 struct tgsi_declaration_sampler_view sv
[PIPE_MAX_SHADER_SAMPLER_VIEWS
];
613 LLVMValueRef immediates
[LP_MAX_INLINED_IMMEDIATES
];
614 LLVMValueRef temps
[LP_MAX_INLINED_TEMPS
];
615 LLVMValueRef addr
[LP_MAX_TGSI_ADDRS
];
617 /* We allocate/use this array of temps if (1 << TGSI_FILE_TEMPORARY) is
618 * set in the indirect_files field.
619 * The temps[] array above is unused then.
621 LLVMValueRef temps_array
;
623 /** bitmask indicating which register files are accessed indirectly */
624 unsigned indirect_files
;
628 static inline struct lp_build_tgsi_soa_context
*
629 lp_soa_context(struct lp_build_tgsi_context
*bld_base
)
631 return (struct lp_build_tgsi_soa_context
*)bld_base
;
634 static inline struct lp_build_tgsi_aos_context
*
635 lp_aos_context(struct lp_build_tgsi_context
*bld_base
)
637 return (struct lp_build_tgsi_aos_context
*)bld_base
;
641 lp_emit_declaration_aos(
642 struct lp_build_tgsi_aos_context
*bld
,
643 const struct tgsi_full_declaration
*decl
);
647 lp_emit_instruction_aos(
648 struct lp_build_tgsi_aos_context
*bld
,
649 const struct tgsi_full_instruction
*inst
,
650 const struct tgsi_opcode_info
*info
,
655 struct lp_build_tgsi_aos_context
*bld
,
656 const struct tgsi_full_instruction
*inst
,
660 void lp_build_fetch_args(
661 struct lp_build_tgsi_context
* bld_base
,
662 struct lp_build_emit_data
* emit_data
);
665 lp_build_tgsi_inst_llvm_aos(
666 struct lp_build_tgsi_context
* bld_base
,
667 const struct tgsi_full_instruction
*inst
);
670 lp_build_tgsi_intrinsic(
671 const struct lp_build_tgsi_action
* action
,
672 struct lp_build_tgsi_context
* bld_base
,
673 struct lp_build_emit_data
* emit_data
);
677 struct lp_build_tgsi_context
*bld_base
,
678 unsigned tgsi_opcode
,
679 struct lp_build_emit_data
* emit_data
);
682 lp_build_emit_llvm_unary(
683 struct lp_build_tgsi_context
*bld_base
,
684 unsigned tgsi_opcode
,
688 lp_build_emit_llvm_binary(
689 struct lp_build_tgsi_context
*bld_base
,
690 unsigned tgsi_opcode
,
695 lp_build_emit_llvm_ternary(
696 struct lp_build_tgsi_context
*bld_base
,
697 unsigned tgsi_opcode
,
703 lp_build_tgsi_inst_llvm(
704 struct lp_build_tgsi_context
* bld_base
,
705 const struct tgsi_full_instruction
*inst
);
708 lp_build_emit_fetch_src(
709 struct lp_build_tgsi_context
*bld_base
,
710 const struct tgsi_full_src_register
*reg
,
711 enum tgsi_opcode_type stype
,
712 const unsigned chan_index
);
716 struct lp_build_tgsi_context
*bld_base
,
717 const struct tgsi_full_instruction
*inst
,
719 const unsigned chan_index
);
723 lp_build_emit_fetch_texoffset(
724 struct lp_build_tgsi_context
*bld_base
,
725 const struct tgsi_full_instruction
*inst
,
727 const unsigned chan_index
);
731 struct lp_build_tgsi_context
* bld_base
,
732 const struct tgsi_token
*tokens
);
738 #endif /* LP_BLD_TGSI_H */