824eff2af4180e824c20285fb0c7bf8e5dabdc24
[mesa.git] / src / gallium / auxiliary / gallivm / lp_bld_tgsi.h
1 /**************************************************************************
2 *
3 * Copyright 2011-2012 Advanced Micro Devices, Inc.
4 * Copyright 2009 VMware, Inc.
5 * All Rights Reserved.
6 *
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:
14 *
15 * The above copyright notice and this permission notice (including the
16 * next paragraph) shall be included in all copies or substantial portions
17 * of the Software.
18 *
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.
26 *
27 **************************************************************************/
28
29 /**
30 * @file
31 * TGSI to LLVM IR translation.
32 *
33 * @author Jose Fonseca <jfonseca@vmware.com>
34 * @author Tom Stellard <thomas.stellard@amd.com>
35 */
36
37 #ifndef LP_BLD_TGSI_H
38 #define LP_BLD_TGSI_H
39
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 "lp_bld_type.h"
45 #include "pipe/p_compiler.h"
46 #include "pipe/p_state.h"
47 #include "tgsi/tgsi_exec.h"
48 #include "tgsi/tgsi_scan.h"
49 #include "tgsi/tgsi_info.h"
50
51 #ifdef __cplusplus
52 extern "C" {
53 #endif
54
55 #define LP_CHAN_ALL ~0u
56
57 #define LP_MAX_INSTRUCTIONS 256
58
59 struct tgsi_full_declaration;
60 struct tgsi_full_immediate;
61 struct tgsi_full_instruction;
62 struct tgsi_full_src_register;
63 struct tgsi_opcode_info;
64 struct tgsi_token;
65 struct tgsi_shader_info;
66 struct lp_build_mask_context;
67 struct gallivm_state;
68 struct lp_derivatives;
69 struct lp_build_tgsi_gs_iface;
70
71
72 enum lp_build_tex_modifier {
73 LP_BLD_TEX_MODIFIER_NONE = 0,
74 LP_BLD_TEX_MODIFIER_PROJECTED,
75 LP_BLD_TEX_MODIFIER_LOD_BIAS,
76 LP_BLD_TEX_MODIFIER_EXPLICIT_LOD,
77 LP_BLD_TEX_MODIFIER_EXPLICIT_DERIV,
78 LP_BLD_TEX_MODIFIER_LOD_ZERO
79 };
80
81
82 /**
83 * Describe a channel of a register.
84 *
85 * The value can be a:
86 * - immediate value (i.e. derived from a IMM register)
87 * - CONST[n].x/y/z/w
88 * - IN[n].x/y/z/w
89 * - undetermined (when .file == TGSI_FILE_NULL)
90 *
91 * This is one of the analysis results, and is used to described
92 * the output color in terms of inputs.
93 */
94 struct lp_tgsi_channel_info
95 {
96 unsigned file:4; /* TGSI_FILE_* */
97 unsigned swizzle:3; /* PIPE_SWIZZLE_x */
98 union {
99 uint32_t index;
100 float value; /* for TGSI_FILE_IMMEDIATE */
101 } u;
102 };
103
104
105 /**
106 * Describe a texture sampler interpolator.
107 *
108 * The interpolation is described in terms of regular inputs.
109 */
110 struct lp_tgsi_texture_info
111 {
112 struct lp_tgsi_channel_info coord[4];
113 unsigned target:8; /* TGSI_TEXTURE_* */
114 unsigned sampler_unit:8; /* Sampler unit */
115 unsigned texture_unit:8; /* Texture unit */
116 unsigned modifier:8; /* LP_BLD_TEX_MODIFIER_* */
117 };
118
119
120 struct lp_tgsi_info
121 {
122 struct tgsi_shader_info base;
123
124 /*
125 * Whether any of the texture opcodes access a register file other than
126 * TGSI_FILE_INPUT.
127 *
128 * We could also handle TGSI_FILE_CONST/IMMEDIATE here, but there is little
129 * benefit.
130 */
131 unsigned indirect_textures:1;
132
133 /*
134 * Whether any of the texture (sample) ocpodes use different sampler
135 * and sampler view unit.
136 */
137 unsigned sampler_texture_units_different:1;
138
139 /*
140 * Whether any immediate values are outside the range of 0 and 1
141 */
142 unsigned unclamped_immediates:1;
143
144 /*
145 * Texture opcode description. Aimed at detecting and described direct
146 * texture opcodes.
147 */
148 unsigned num_texs;
149 struct lp_tgsi_texture_info tex[PIPE_MAX_SAMPLERS];
150
151 /*
152 * Output description. Aimed at detecting and describing simple blit
153 * shaders.
154 */
155 struct lp_tgsi_channel_info output[PIPE_MAX_SHADER_OUTPUTS][4];
156
157 /*
158 * Shortcut pointers into the above (for fragment shaders).
159 */
160 const struct lp_tgsi_channel_info *cbuf[PIPE_MAX_COLOR_BUFS];
161 };
162
163 /**
164 * Reference to system values.
165 */
166 struct lp_bld_tgsi_system_values {
167 LLVMValueRef instance_id;
168 LLVMValueRef vertex_id;
169 LLVMValueRef vertex_id_nobase;
170 LLVMValueRef prim_id;
171 LLVMValueRef basevertex;
172 LLVMValueRef invocation_id;
173 };
174
175
176 /**
177 * Sampler code generation interface.
178 *
179 * Although texture sampling is a requirement for TGSI translation, it is
180 * a very different problem with several different approaches to it. This
181 * structure establishes an interface for texture sampling code generation, so
182 * that we can easily use different texture sampling strategies.
183 */
184 struct lp_build_sampler_soa
185 {
186 void
187 (*destroy)( struct lp_build_sampler_soa *sampler );
188
189 void
190 (*emit_tex_sample)(const struct lp_build_sampler_soa *sampler,
191 struct gallivm_state *gallivm,
192 const struct lp_sampler_params *params);
193
194 void
195 (*emit_size_query)( const struct lp_build_sampler_soa *sampler,
196 struct gallivm_state *gallivm,
197 const struct lp_sampler_size_query_params *params);
198 };
199
200
201 struct lp_build_sampler_aos
202 {
203 LLVMValueRef
204 (*emit_fetch_texel)( const struct lp_build_sampler_aos *sampler,
205 struct lp_build_context *bld,
206 unsigned target, /* TGSI_TEXTURE_* */
207 unsigned unit,
208 LLVMValueRef coords,
209 const struct lp_derivatives derivs,
210 enum lp_build_tex_modifier modifier);
211 };
212
213
214 void
215 lp_build_tgsi_info(const struct tgsi_token *tokens,
216 struct lp_tgsi_info *info);
217
218
219 void
220 lp_build_tgsi_soa(struct gallivm_state *gallivm,
221 const struct tgsi_token *tokens,
222 struct lp_type type,
223 struct lp_build_mask_context *mask,
224 LLVMValueRef consts_ptr,
225 LLVMValueRef const_sizes_ptr,
226 const struct lp_bld_tgsi_system_values *system_values,
227 const LLVMValueRef (*inputs)[4],
228 LLVMValueRef (*outputs)[4],
229 LLVMValueRef context_ptr,
230 LLVMValueRef thread_data_ptr,
231 const struct lp_build_sampler_soa *sampler,
232 const struct tgsi_shader_info *info,
233 const struct lp_build_tgsi_gs_iface *gs_iface,
234 LLVMValueRef ssbo_ptr,
235 LLVMValueRef ssbo_sizes_ptr);
236
237
238 void
239 lp_build_tgsi_aos(struct gallivm_state *gallivm,
240 const struct tgsi_token *tokens,
241 struct lp_type type,
242 const unsigned char swizzles[4],
243 LLVMValueRef consts_ptr,
244 const LLVMValueRef *inputs,
245 LLVMValueRef *outputs,
246 const struct lp_build_sampler_aos *sampler,
247 const struct tgsi_shader_info *info);
248
249
250 enum lp_exec_mask_break_type {
251 LP_EXEC_MASK_BREAK_TYPE_LOOP,
252 LP_EXEC_MASK_BREAK_TYPE_SWITCH
253 };
254
255
256 struct lp_exec_mask {
257 struct lp_build_context *bld;
258
259 boolean has_mask;
260 boolean ret_in_main;
261
262 LLVMTypeRef int_vec_type;
263
264 LLVMValueRef exec_mask;
265
266 LLVMValueRef ret_mask;
267 LLVMValueRef cond_mask;
268 LLVMValueRef switch_mask; /* current switch exec mask */
269 LLVMValueRef cont_mask;
270 LLVMValueRef break_mask;
271
272 struct function_ctx {
273 int pc;
274 LLVMValueRef ret_mask;
275
276 LLVMValueRef cond_stack[LP_MAX_TGSI_NESTING];
277 int cond_stack_size;
278
279 /* keep track if break belongs to switch or loop */
280 enum lp_exec_mask_break_type break_type_stack[LP_MAX_TGSI_NESTING];
281 enum lp_exec_mask_break_type break_type;
282
283 struct {
284 LLVMValueRef switch_val;
285 LLVMValueRef switch_mask;
286 LLVMValueRef switch_mask_default;
287 boolean switch_in_default;
288 unsigned switch_pc;
289 } switch_stack[LP_MAX_TGSI_NESTING];
290 int switch_stack_size;
291 LLVMValueRef switch_val;
292 LLVMValueRef switch_mask_default; /* reverse of switch mask used for default */
293 boolean switch_in_default; /* if switch exec is currently in default */
294 unsigned switch_pc; /* when used points to default or endswitch-1 */
295
296 LLVMValueRef loop_limiter;
297 LLVMBasicBlockRef loop_block;
298 LLVMValueRef break_var;
299 struct {
300 LLVMBasicBlockRef loop_block;
301 LLVMValueRef cont_mask;
302 LLVMValueRef break_mask;
303 LLVMValueRef break_var;
304 } loop_stack[LP_MAX_TGSI_NESTING];
305 int loop_stack_size;
306
307 } *function_stack;
308 int function_stack_size;
309 };
310
311 struct lp_build_tgsi_inst_list
312 {
313 struct tgsi_full_instruction *instructions;
314 uint max_instructions;
315 uint num_instructions;
316 };
317
318 unsigned lp_bld_tgsi_list_init(struct lp_build_tgsi_context * bld_base);
319
320
321 unsigned lp_bld_tgsi_add_instruction(
322 struct lp_build_tgsi_context * bld_base,
323 const struct tgsi_full_instruction *inst_to_add);
324
325
326 struct lp_build_tgsi_context;
327
328
329 typedef LLVMValueRef (*lp_build_emit_fetch_fn)(struct lp_build_tgsi_context *,
330 const struct tgsi_full_src_register *,
331 enum tgsi_opcode_type,
332 unsigned);
333
334 struct lp_build_tgsi_context
335 {
336 struct lp_build_context base;
337
338 struct lp_build_context uint_bld;
339 struct lp_build_context int_bld;
340
341 struct lp_build_context dbl_bld;
342
343 struct lp_build_context uint64_bld;
344 struct lp_build_context int64_bld;
345
346 /** This array stores functions that are used to transform TGSI opcodes to
347 * LLVM instructions.
348 */
349 struct lp_build_tgsi_action op_actions[TGSI_OPCODE_LAST];
350
351 /* TGSI_OPCODE_RSQ is defined as 1 / sqrt( abs(src0.x) ), rsq_action
352 * should compute 1 / sqrt (src0.x) */
353 struct lp_build_tgsi_action rsq_action;
354
355 struct lp_build_tgsi_action sqrt_action;
356
357 struct lp_build_tgsi_action drsq_action;
358
359 struct lp_build_tgsi_action dsqrt_action;
360 const struct tgsi_shader_info *info;
361
362 lp_build_emit_fetch_fn emit_fetch_funcs[TGSI_FILE_COUNT];
363
364 LLVMValueRef (*emit_swizzle)(struct lp_build_tgsi_context *,
365 LLVMValueRef, unsigned, unsigned, unsigned, unsigned);
366
367
368 void (*emit_debug)(struct lp_build_tgsi_context *,
369 const struct tgsi_full_instruction *,
370 const struct tgsi_opcode_info *);
371
372 void (*emit_store)(struct lp_build_tgsi_context *,
373 const struct tgsi_full_instruction *,
374 const struct tgsi_opcode_info *,
375 unsigned index,
376 LLVMValueRef dst[4]);
377
378 void (*emit_declaration)(struct lp_build_tgsi_context *,
379 const struct tgsi_full_declaration *decl);
380
381 void (*emit_immediate)(struct lp_build_tgsi_context *,
382 const struct tgsi_full_immediate *imm);
383
384
385 /* Allow the user to store data in this structure rather than passing it
386 * to every function. */
387 void * userdata;
388
389 boolean soa;
390
391 int pc;
392
393 struct tgsi_full_instruction *instructions;
394 uint max_instructions;
395 uint num_instructions;
396
397 /** This function allows the user to insert some instructions at the
398 * beginning of the program. It is optional and does not need to be
399 * implemented.
400 */
401 void (*emit_prologue)(struct lp_build_tgsi_context*);
402
403 /** This function allows the user to insert some instructions at the end of
404 * the program. This callback is intended to be used for emitting
405 * instructions to handle the export for the output registers, but it can
406 * be used for any purpose. Implementing this function is optiona, but
407 * recommended.
408 */
409 void (*emit_epilogue)(struct lp_build_tgsi_context*);
410 };
411
412 struct lp_build_tgsi_gs_iface
413 {
414 LLVMValueRef (*fetch_input)(const struct lp_build_tgsi_gs_iface *gs_iface,
415 struct lp_build_tgsi_context * bld_base,
416 boolean is_vindex_indirect,
417 LLVMValueRef vertex_index,
418 boolean is_aindex_indirect,
419 LLVMValueRef attrib_index,
420 LLVMValueRef swizzle_index);
421 void (*emit_vertex)(const struct lp_build_tgsi_gs_iface *gs_iface,
422 struct lp_build_tgsi_context * bld_base,
423 LLVMValueRef (*outputs)[4],
424 LLVMValueRef emitted_vertices_vec);
425 void (*end_primitive)(const struct lp_build_tgsi_gs_iface *gs_iface,
426 struct lp_build_tgsi_context * bld_base,
427 LLVMValueRef verts_per_prim_vec,
428 LLVMValueRef emitted_prims_vec);
429 void (*gs_epilogue)(const struct lp_build_tgsi_gs_iface *gs_iface,
430 struct lp_build_tgsi_context * bld_base,
431 LLVMValueRef total_emitted_vertices_vec,
432 LLVMValueRef emitted_prims_vec);
433 };
434
435 struct lp_build_tgsi_soa_context
436 {
437 struct lp_build_tgsi_context bld_base;
438
439 /* Builder for scalar elements of shader's data type (float) */
440 struct lp_build_context elem_bld;
441
442 const struct lp_build_tgsi_gs_iface *gs_iface;
443 LLVMValueRef emitted_prims_vec_ptr;
444 LLVMValueRef total_emitted_vertices_vec_ptr;
445 LLVMValueRef emitted_vertices_vec_ptr;
446 LLVMValueRef max_output_vertices_vec;
447
448 LLVMValueRef consts_ptr;
449 LLVMValueRef const_sizes_ptr;
450 LLVMValueRef consts[LP_MAX_TGSI_CONST_BUFFERS];
451 LLVMValueRef consts_sizes[LP_MAX_TGSI_CONST_BUFFERS];
452 const LLVMValueRef (*inputs)[TGSI_NUM_CHANNELS];
453 LLVMValueRef (*outputs)[TGSI_NUM_CHANNELS];
454 LLVMValueRef context_ptr;
455 LLVMValueRef thread_data_ptr;
456
457 LLVMValueRef ssbo_ptr;
458 LLVMValueRef ssbo_sizes_ptr;
459
460 const struct lp_build_sampler_soa *sampler;
461
462 struct tgsi_declaration_sampler_view sv[PIPE_MAX_SHADER_SAMPLER_VIEWS];
463
464 LLVMValueRef immediates[LP_MAX_INLINED_IMMEDIATES][TGSI_NUM_CHANNELS];
465 LLVMValueRef temps[LP_MAX_INLINED_TEMPS][TGSI_NUM_CHANNELS];
466 LLVMValueRef addr[LP_MAX_TGSI_ADDRS][TGSI_NUM_CHANNELS];
467
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.
471 */
472 LLVMValueRef temps_array;
473
474 /* We allocate/use this array of output if (1 << TGSI_FILE_OUTPUT) is
475 * set in the indirect_files field.
476 * The outputs[] array above is unused then.
477 */
478 LLVMValueRef outputs_array;
479
480 /* We allocate/use this array of inputs if (1 << TGSI_FILE_INPUT) is
481 * set in the indirect_files field.
482 * The inputs[] array above is unused then.
483 */
484 LLVMValueRef inputs_array;
485
486 /* We allocate/use this array of temps if (1 << TGSI_FILE_IMMEDIATE) is
487 * set in the indirect_files field.
488 */
489 LLVMValueRef imms_array;
490
491
492 struct lp_bld_tgsi_system_values system_values;
493
494 /** bitmask indicating which register files are accessed indirectly */
495 unsigned indirect_files;
496
497 struct lp_build_mask_context *mask;
498 struct lp_exec_mask exec_mask;
499
500 uint num_immediates;
501 boolean use_immediates_array;
502 };
503
504 void
505 lp_emit_declaration_soa(
506 struct lp_build_tgsi_context *bld,
507 const struct tgsi_full_declaration *decl);
508
509 void lp_emit_immediate_soa(
510 struct lp_build_tgsi_context *bld_base,
511 const struct tgsi_full_immediate *imm);
512
513 boolean
514 lp_emit_instruction_soa(
515 struct lp_build_tgsi_soa_context *bld,
516 const struct tgsi_full_instruction *inst,
517 const struct tgsi_opcode_info *info);
518
519
520 LLVMValueRef
521 lp_get_temp_ptr_soa(
522 struct lp_build_tgsi_soa_context *bld,
523 unsigned index,
524 unsigned chan);
525
526 LLVMValueRef
527 lp_get_output_ptr(
528 struct lp_build_tgsi_soa_context *bld,
529 unsigned index,
530 unsigned chan);
531
532 struct lp_build_tgsi_aos_context
533 {
534 struct lp_build_tgsi_context bld_base;
535
536 /* Builder for integer masks and indices */
537 struct lp_build_context int_bld;
538
539 /*
540 * AoS swizzle used:
541 * - swizzles[0] = red index
542 * - swizzles[1] = green index
543 * - swizzles[2] = blue index
544 * - swizzles[3] = alpha index
545 */
546 unsigned char swizzles[4];
547 unsigned char inv_swizzles[4];
548
549 LLVMValueRef consts_ptr;
550 const LLVMValueRef *inputs;
551 LLVMValueRef *outputs;
552
553 const struct lp_build_sampler_aos *sampler;
554
555 struct tgsi_declaration_sampler_view sv[PIPE_MAX_SHADER_SAMPLER_VIEWS];
556
557 LLVMValueRef immediates[LP_MAX_INLINED_IMMEDIATES];
558 LLVMValueRef temps[LP_MAX_INLINED_TEMPS];
559 LLVMValueRef addr[LP_MAX_TGSI_ADDRS];
560
561 /* We allocate/use this array of temps if (1 << TGSI_FILE_TEMPORARY) is
562 * set in the indirect_files field.
563 * The temps[] array above is unused then.
564 */
565 LLVMValueRef temps_array;
566
567 /** bitmask indicating which register files are accessed indirectly */
568 unsigned indirect_files;
569
570 };
571
572 static inline struct lp_build_tgsi_soa_context *
573 lp_soa_context(struct lp_build_tgsi_context *bld_base)
574 {
575 return (struct lp_build_tgsi_soa_context *)bld_base;
576 }
577
578 static inline struct lp_build_tgsi_aos_context *
579 lp_aos_context(struct lp_build_tgsi_context *bld_base)
580 {
581 return (struct lp_build_tgsi_aos_context *)bld_base;
582 }
583
584 void
585 lp_emit_declaration_aos(
586 struct lp_build_tgsi_aos_context *bld,
587 const struct tgsi_full_declaration *decl);
588
589
590 boolean
591 lp_emit_instruction_aos(
592 struct lp_build_tgsi_aos_context *bld,
593 const struct tgsi_full_instruction *inst,
594 const struct tgsi_opcode_info *info,
595 int *pc);
596
597 void
598 lp_emit_store_aos(
599 struct lp_build_tgsi_aos_context *bld,
600 const struct tgsi_full_instruction *inst,
601 unsigned index,
602 LLVMValueRef value);
603
604 void lp_build_fetch_args(
605 struct lp_build_tgsi_context * bld_base,
606 struct lp_build_emit_data * emit_data);
607
608 LLVMValueRef
609 lp_build_tgsi_inst_llvm_aos(
610 struct lp_build_tgsi_context * bld_base,
611 const struct tgsi_full_instruction *inst);
612
613 void
614 lp_build_tgsi_intrinsic(
615 const struct lp_build_tgsi_action * action,
616 struct lp_build_tgsi_context * bld_base,
617 struct lp_build_emit_data * emit_data);
618
619 LLVMValueRef
620 lp_build_emit_llvm(
621 struct lp_build_tgsi_context *bld_base,
622 unsigned tgsi_opcode,
623 struct lp_build_emit_data * emit_data);
624
625 LLVMValueRef
626 lp_build_emit_llvm_unary(
627 struct lp_build_tgsi_context *bld_base,
628 unsigned tgsi_opcode,
629 LLVMValueRef arg0);
630
631 LLVMValueRef
632 lp_build_emit_llvm_binary(
633 struct lp_build_tgsi_context *bld_base,
634 unsigned tgsi_opcode,
635 LLVMValueRef arg0,
636 LLVMValueRef arg1);
637
638 LLVMValueRef
639 lp_build_emit_llvm_ternary(
640 struct lp_build_tgsi_context *bld_base,
641 unsigned tgsi_opcode,
642 LLVMValueRef arg0,
643 LLVMValueRef arg1,
644 LLVMValueRef arg2);
645
646 boolean
647 lp_build_tgsi_inst_llvm(
648 struct lp_build_tgsi_context * bld_base,
649 const struct tgsi_full_instruction *inst);
650
651 LLVMValueRef
652 lp_build_emit_fetch_src(
653 struct lp_build_tgsi_context *bld_base,
654 const struct tgsi_full_src_register *reg,
655 enum tgsi_opcode_type stype,
656 const unsigned chan_index);
657
658 LLVMValueRef
659 lp_build_emit_fetch(
660 struct lp_build_tgsi_context *bld_base,
661 const struct tgsi_full_instruction *inst,
662 unsigned src_op,
663 const unsigned chan_index);
664
665
666 LLVMValueRef
667 lp_build_emit_fetch_texoffset(
668 struct lp_build_tgsi_context *bld_base,
669 const struct tgsi_full_instruction *inst,
670 unsigned tex_off_op,
671 const unsigned chan_index);
672
673 boolean
674 lp_build_tgsi_llvm(
675 struct lp_build_tgsi_context * bld_base,
676 const struct tgsi_token *tokens);
677
678 #ifdef __cplusplus
679 }
680 #endif
681
682 #endif /* LP_BLD_TGSI_H */