967373ccdaef173a8094a1f46ddf40a55a5a235c
[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 #define LP_CHAN_ALL ~0
52
53 #define LP_MAX_INSTRUCTIONS 256
54
55 struct tgsi_full_declaration;
56 struct tgsi_full_immediate;
57 struct tgsi_full_instruction;
58 struct tgsi_full_src_register;
59 struct tgsi_opcode_info;
60 struct tgsi_token;
61 struct tgsi_shader_info;
62 struct lp_build_mask_context;
63 struct gallivm_state;
64 struct lp_derivatives;
65 struct lp_build_tgsi_gs_iface;
66
67
68 enum lp_build_tex_modifier {
69 LP_BLD_TEX_MODIFIER_NONE = 0,
70 LP_BLD_TEX_MODIFIER_PROJECTED,
71 LP_BLD_TEX_MODIFIER_LOD_BIAS,
72 LP_BLD_TEX_MODIFIER_EXPLICIT_LOD,
73 LP_BLD_TEX_MODIFIER_EXPLICIT_DERIV,
74 LP_BLD_TEX_MODIFIER_LOD_ZERO
75 };
76
77
78 /**
79 * Describe a channel of a register.
80 *
81 * The value can be a:
82 * - immediate value (i.e. derived from a IMM register)
83 * - CONST[n].x/y/z/w
84 * - IN[n].x/y/z/w
85 * - undetermined (when .file == TGSI_FILE_NULL)
86 *
87 * This is one of the analysis results, and is used to described
88 * the output color in terms of inputs.
89 */
90 struct lp_tgsi_channel_info
91 {
92 unsigned file:4; /* TGSI_FILE_* */
93 unsigned swizzle:3; /* PIPE_SWIZZLE_x */
94 union {
95 uint32_t index;
96 float value; /* for TGSI_FILE_IMMEDIATE */
97 } u;
98 };
99
100
101 /**
102 * Describe a texture sampler interpolator.
103 *
104 * The interpolation is described in terms of regular inputs.
105 */
106 struct lp_tgsi_texture_info
107 {
108 struct lp_tgsi_channel_info coord[4];
109 unsigned target:8; /* TGSI_TEXTURE_* */
110 unsigned sampler_unit:8; /* Sampler unit */
111 unsigned texture_unit:8; /* Texture unit */
112 unsigned modifier:8; /* LP_BLD_TEX_MODIFIER_* */
113 };
114
115
116 struct lp_tgsi_info
117 {
118 struct tgsi_shader_info base;
119
120 /*
121 * Whether any of the texture opcodes access a register file other than
122 * TGSI_FILE_INPUT.
123 *
124 * We could also handle TGSI_FILE_CONST/IMMEDIATE here, but there is little
125 * benefit.
126 */
127 unsigned indirect_textures:1;
128
129 /*
130 * Whether any of the texture (sample) ocpodes use different sampler
131 * and sampler view unit.
132 */
133 unsigned sampler_texture_units_different:1;
134
135 /*
136 * Whether any immediate values are outside the range of 0 and 1
137 */
138 unsigned unclamped_immediates:1;
139
140 /*
141 * Texture opcode description. Aimed at detecting and described direct
142 * texture opcodes.
143 */
144 unsigned num_texs;
145 struct lp_tgsi_texture_info tex[PIPE_MAX_SAMPLERS];
146
147 /*
148 * Output description. Aimed at detecting and describing simple blit
149 * shaders.
150 */
151 struct lp_tgsi_channel_info output[PIPE_MAX_SHADER_OUTPUTS][4];
152
153 /*
154 * Shortcut pointers into the above (for fragment shaders).
155 */
156 const struct lp_tgsi_channel_info *cbuf[PIPE_MAX_COLOR_BUFS];
157 };
158
159 /**
160 * Reference to system values.
161 */
162 struct lp_bld_tgsi_system_values {
163 LLVMValueRef instance_id;
164 LLVMValueRef vertex_id;
165 LLVMValueRef vertex_id_nobase;
166 LLVMValueRef prim_id;
167 LLVMValueRef basevertex;
168 LLVMValueRef invocation_id;
169 };
170
171
172 /**
173 * Sampler code generation interface.
174 *
175 * Although texture sampling is a requirement for TGSI translation, it is
176 * a very different problem with several different approaches to it. This
177 * structure establishes an interface for texture sampling code generation, so
178 * that we can easily use different texture sampling strategies.
179 */
180 struct lp_build_sampler_soa
181 {
182 void
183 (*destroy)( struct lp_build_sampler_soa *sampler );
184
185 void
186 (*emit_tex_sample)(const struct lp_build_sampler_soa *sampler,
187 struct gallivm_state *gallivm,
188 const struct lp_sampler_params *params);
189
190 void
191 (*emit_size_query)( const struct lp_build_sampler_soa *sampler,
192 struct gallivm_state *gallivm,
193 struct lp_type type,
194 unsigned unit,
195 unsigned target,
196 LLVMValueRef context_ptr,
197 boolean need_nr_mips,
198 enum lp_sampler_lod_property,
199 LLVMValueRef explicit_lod, /* optional */
200 LLVMValueRef *sizes_out);
201 };
202
203
204 struct lp_build_sampler_aos
205 {
206 LLVMValueRef
207 (*emit_fetch_texel)( struct lp_build_sampler_aos *sampler,
208 struct lp_build_context *bld,
209 unsigned target, /* TGSI_TEXTURE_* */
210 unsigned unit,
211 LLVMValueRef coords,
212 const struct lp_derivatives derivs,
213 enum lp_build_tex_modifier modifier);
214 };
215
216
217 void
218 lp_build_tgsi_info(const struct tgsi_token *tokens,
219 struct lp_tgsi_info *info);
220
221
222 void
223 lp_build_tgsi_soa(struct gallivm_state *gallivm,
224 const struct tgsi_token *tokens,
225 struct lp_type type,
226 struct lp_build_mask_context *mask,
227 LLVMValueRef consts_ptr,
228 LLVMValueRef const_sizes_ptr,
229 const struct lp_bld_tgsi_system_values *system_values,
230 const LLVMValueRef (*inputs)[4],
231 LLVMValueRef (*outputs)[4],
232 LLVMValueRef context_ptr,
233 struct lp_build_sampler_soa *sampler,
234 const struct tgsi_shader_info *info,
235 const struct lp_build_tgsi_gs_iface *gs_iface);
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 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 /** This array stores functions that are used to transform TGSI opcodes to
342 * LLVM instructions.
343 */
344 struct lp_build_tgsi_action op_actions[TGSI_OPCODE_LAST];
345
346 /* TGSI_OPCODE_RSQ is defined as 1 / sqrt( abs(src0.x) ), rsq_action
347 * should compute 1 / sqrt (src0.x) */
348 struct lp_build_tgsi_action rsq_action;
349
350 struct lp_build_tgsi_action sqrt_action;
351
352 const struct tgsi_shader_info *info;
353
354 lp_build_emit_fetch_fn emit_fetch_funcs[TGSI_FILE_COUNT];
355
356 LLVMValueRef (*emit_swizzle)(struct lp_build_tgsi_context *,
357 LLVMValueRef, unsigned, unsigned, unsigned, unsigned);
358
359
360 void (*emit_debug)(struct lp_build_tgsi_context *,
361 const struct tgsi_full_instruction *,
362 const struct tgsi_opcode_info *);
363
364 void (*emit_store)(struct lp_build_tgsi_context *,
365 const struct tgsi_full_instruction *,
366 const struct tgsi_opcode_info *,
367 LLVMValueRef dst[4]);
368
369 void (*emit_declaration)(struct lp_build_tgsi_context *,
370 const struct tgsi_full_declaration *decl);
371
372 void (*emit_immediate)(struct lp_build_tgsi_context *,
373 const struct tgsi_full_immediate *imm);
374
375
376 /* Allow the user to store data in this structure rather than passing it
377 * to every function. */
378 void * userdata;
379
380 boolean soa;
381
382 int pc;
383
384 struct tgsi_full_instruction *instructions;
385 uint max_instructions;
386 uint num_instructions;
387
388 /** This function allows the user to insert some instructions at the
389 * beginning of the program. It is optional and does not need to be
390 * implemented.
391 */
392 void (*emit_prologue)(struct lp_build_tgsi_context*);
393
394 /** This function allows the user to insert some instructions at the end of
395 * the program. This callback is intended to be used for emitting
396 * instructions to handle the export for the output registers, but it can
397 * be used for any purpose. Implementing this function is optiona, but
398 * recommended.
399 */
400 void (*emit_epilogue)(struct lp_build_tgsi_context*);
401 };
402
403 struct lp_build_tgsi_gs_iface
404 {
405 LLVMValueRef (*fetch_input)(const struct lp_build_tgsi_gs_iface *gs_iface,
406 struct lp_build_tgsi_context * bld_base,
407 boolean is_vindex_indirect,
408 LLVMValueRef vertex_index,
409 boolean is_aindex_indirect,
410 LLVMValueRef attrib_index,
411 LLVMValueRef swizzle_index);
412 void (*emit_vertex)(const struct lp_build_tgsi_gs_iface *gs_iface,
413 struct lp_build_tgsi_context * bld_base,
414 LLVMValueRef (*outputs)[4],
415 LLVMValueRef emitted_vertices_vec);
416 void (*end_primitive)(const struct lp_build_tgsi_gs_iface *gs_iface,
417 struct lp_build_tgsi_context * bld_base,
418 LLVMValueRef verts_per_prim_vec,
419 LLVMValueRef emitted_prims_vec);
420 void (*gs_epilogue)(const struct lp_build_tgsi_gs_iface *gs_iface,
421 struct lp_build_tgsi_context * bld_base,
422 LLVMValueRef total_emitted_vertices_vec,
423 LLVMValueRef emitted_prims_vec);
424 };
425
426 struct lp_build_tgsi_soa_context
427 {
428 struct lp_build_tgsi_context bld_base;
429
430 /* Builder for scalar elements of shader's data type (float) */
431 struct lp_build_context elem_bld;
432
433 const struct lp_build_tgsi_gs_iface *gs_iface;
434 LLVMValueRef emitted_prims_vec_ptr;
435 LLVMValueRef total_emitted_vertices_vec_ptr;
436 LLVMValueRef emitted_vertices_vec_ptr;
437 LLVMValueRef max_output_vertices_vec;
438
439 LLVMValueRef consts_ptr;
440 LLVMValueRef const_sizes_ptr;
441 LLVMValueRef consts[LP_MAX_TGSI_CONST_BUFFERS];
442 LLVMValueRef consts_sizes[LP_MAX_TGSI_CONST_BUFFERS];
443 const LLVMValueRef (*inputs)[TGSI_NUM_CHANNELS];
444 LLVMValueRef (*outputs)[TGSI_NUM_CHANNELS];
445 LLVMValueRef context_ptr;
446
447 const struct lp_build_sampler_soa *sampler;
448
449 struct tgsi_declaration_sampler_view sv[PIPE_MAX_SHADER_SAMPLER_VIEWS];
450
451 LLVMValueRef immediates[LP_MAX_INLINED_IMMEDIATES][TGSI_NUM_CHANNELS];
452 LLVMValueRef temps[LP_MAX_INLINED_TEMPS][TGSI_NUM_CHANNELS];
453 LLVMValueRef addr[LP_MAX_TGSI_ADDRS][TGSI_NUM_CHANNELS];
454 LLVMValueRef preds[LP_MAX_TGSI_PREDS][TGSI_NUM_CHANNELS];
455
456 /* We allocate/use this array of temps if (1 << TGSI_FILE_TEMPORARY) is
457 * set in the indirect_files field.
458 * The temps[] array above is unused then.
459 */
460 LLVMValueRef temps_array;
461
462 /* We allocate/use this array of output if (1 << TGSI_FILE_OUTPUT) is
463 * set in the indirect_files field.
464 * The outputs[] array above is unused then.
465 */
466 LLVMValueRef outputs_array;
467
468 /* We allocate/use this array of inputs if (1 << TGSI_FILE_INPUT) is
469 * set in the indirect_files field.
470 * The inputs[] array above is unused then.
471 */
472 LLVMValueRef inputs_array;
473
474 /* We allocate/use this array of temps if (1 << TGSI_FILE_IMMEDIATE) is
475 * set in the indirect_files field.
476 */
477 LLVMValueRef imms_array;
478
479
480 struct lp_bld_tgsi_system_values system_values;
481
482 /** bitmask indicating which register files are accessed indirectly */
483 unsigned indirect_files;
484
485 struct lp_build_mask_context *mask;
486 struct lp_exec_mask exec_mask;
487
488 uint num_immediates;
489 boolean use_immediates_array;
490 };
491
492 void
493 lp_emit_declaration_soa(
494 struct lp_build_tgsi_context *bld,
495 const struct tgsi_full_declaration *decl);
496
497 void lp_emit_immediate_soa(
498 struct lp_build_tgsi_context *bld_base,
499 const struct tgsi_full_immediate *imm);
500
501 boolean
502 lp_emit_instruction_soa(
503 struct lp_build_tgsi_soa_context *bld,
504 const struct tgsi_full_instruction *inst,
505 const struct tgsi_opcode_info *info);
506
507
508 LLVMValueRef
509 lp_get_temp_ptr_soa(
510 struct lp_build_tgsi_soa_context *bld,
511 unsigned index,
512 unsigned chan);
513
514 LLVMValueRef
515 lp_get_output_ptr(
516 struct lp_build_tgsi_soa_context *bld,
517 unsigned index,
518 unsigned chan);
519
520 struct lp_build_tgsi_aos_context
521 {
522 struct lp_build_tgsi_context bld_base;
523
524 /* Builder for integer masks and indices */
525 struct lp_build_context int_bld;
526
527 /*
528 * AoS swizzle used:
529 * - swizzles[0] = red index
530 * - swizzles[1] = green index
531 * - swizzles[2] = blue index
532 * - swizzles[3] = alpha index
533 */
534 unsigned char swizzles[4];
535 unsigned char inv_swizzles[4];
536
537 LLVMValueRef consts_ptr;
538 const LLVMValueRef *inputs;
539 LLVMValueRef *outputs;
540
541 struct lp_build_sampler_aos *sampler;
542
543 struct tgsi_declaration_sampler_view sv[PIPE_MAX_SHADER_SAMPLER_VIEWS];
544
545 LLVMValueRef immediates[LP_MAX_INLINED_IMMEDIATES];
546 LLVMValueRef temps[LP_MAX_INLINED_TEMPS];
547 LLVMValueRef addr[LP_MAX_TGSI_ADDRS];
548 LLVMValueRef preds[LP_MAX_TGSI_PREDS];
549
550 /* We allocate/use this array of temps if (1 << TGSI_FILE_TEMPORARY) is
551 * set in the indirect_files field.
552 * The temps[] array above is unused then.
553 */
554 LLVMValueRef temps_array;
555
556 /** bitmask indicating which register files are accessed indirectly */
557 unsigned indirect_files;
558
559 };
560
561 static INLINE struct lp_build_tgsi_soa_context *
562 lp_soa_context(struct lp_build_tgsi_context *bld_base)
563 {
564 return (struct lp_build_tgsi_soa_context *)bld_base;
565 }
566
567 static INLINE struct lp_build_tgsi_aos_context *
568 lp_aos_context(struct lp_build_tgsi_context *bld_base)
569 {
570 return (struct lp_build_tgsi_aos_context *)bld_base;
571 }
572
573 void
574 lp_emit_declaration_aos(
575 struct lp_build_tgsi_aos_context *bld,
576 const struct tgsi_full_declaration *decl);
577
578
579 boolean
580 lp_emit_instruction_aos(
581 struct lp_build_tgsi_aos_context *bld,
582 const struct tgsi_full_instruction *inst,
583 const struct tgsi_opcode_info *info,
584 int *pc);
585
586 void
587 lp_emit_store_aos(
588 struct lp_build_tgsi_aos_context *bld,
589 const struct tgsi_full_instruction *inst,
590 unsigned index,
591 LLVMValueRef value);
592
593 void lp_build_fetch_args(
594 struct lp_build_tgsi_context * bld_base,
595 struct lp_build_emit_data * emit_data);
596
597 LLVMValueRef
598 lp_build_tgsi_inst_llvm_aos(
599 struct lp_build_tgsi_context * bld_base,
600 const struct tgsi_full_instruction *inst);
601
602 void
603 lp_build_tgsi_intrinsic(
604 const struct lp_build_tgsi_action * action,
605 struct lp_build_tgsi_context * bld_base,
606 struct lp_build_emit_data * emit_data);
607
608 LLVMValueRef
609 lp_build_emit_llvm(
610 struct lp_build_tgsi_context *bld_base,
611 unsigned tgsi_opcode,
612 struct lp_build_emit_data * emit_data);
613
614 LLVMValueRef
615 lp_build_emit_llvm_unary(
616 struct lp_build_tgsi_context *bld_base,
617 unsigned tgsi_opcode,
618 LLVMValueRef arg0);
619
620 LLVMValueRef
621 lp_build_emit_llvm_binary(
622 struct lp_build_tgsi_context *bld_base,
623 unsigned tgsi_opcode,
624 LLVMValueRef arg0,
625 LLVMValueRef arg1);
626
627 LLVMValueRef
628 lp_build_emit_llvm_ternary(
629 struct lp_build_tgsi_context *bld_base,
630 unsigned tgsi_opcode,
631 LLVMValueRef arg0,
632 LLVMValueRef arg1,
633 LLVMValueRef arg2);
634
635 boolean
636 lp_build_tgsi_inst_llvm(
637 struct lp_build_tgsi_context * bld_base,
638 const struct tgsi_full_instruction *inst);
639
640 LLVMValueRef
641 lp_build_emit_fetch(
642 struct lp_build_tgsi_context *bld_base,
643 const struct tgsi_full_instruction *inst,
644 unsigned src_op,
645 const unsigned chan_index);
646
647
648 LLVMValueRef
649 lp_build_emit_fetch_texoffset(
650 struct lp_build_tgsi_context *bld_base,
651 const struct tgsi_full_instruction *inst,
652 unsigned tex_off_op,
653 const unsigned chan_index);
654
655 boolean
656 lp_build_tgsi_llvm(
657 struct lp_build_tgsi_context * bld_base,
658 const struct tgsi_token *tokens);
659
660 #endif /* LP_BLD_TGSI_H */