freedreno/a4xx: frag-depth fixes
[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 prim_id;
166 };
167
168
169 /**
170 * Sampler code generation interface.
171 *
172 * Although texture sampling is a requirement for TGSI translation, it is
173 * a very different problem with several different approaches to it. This
174 * structure establishes an interface for texture sampling code generation, so
175 * that we can easily use different texture sampling strategies.
176 */
177 struct lp_build_sampler_soa
178 {
179 void
180 (*destroy)( struct lp_build_sampler_soa *sampler );
181
182 void
183 (*emit_fetch_texel)( const struct lp_build_sampler_soa *sampler,
184 struct gallivm_state *gallivm,
185 struct lp_type type,
186 boolean is_fetch,
187 unsigned texture_index,
188 unsigned sampler_index,
189 const LLVMValueRef *coords,
190 const LLVMValueRef *offsets,
191 const struct lp_derivatives *derivs,
192 LLVMValueRef lod_bias, /* optional */
193 LLVMValueRef explicit_lod, /* optional */
194 enum lp_sampler_lod_property,
195 LLVMValueRef *texel);
196
197 void
198 (*emit_size_query)( const struct lp_build_sampler_soa *sampler,
199 struct gallivm_state *gallivm,
200 struct lp_type type,
201 unsigned unit,
202 unsigned target,
203 boolean need_nr_mips,
204 enum lp_sampler_lod_property,
205 LLVMValueRef explicit_lod, /* optional */
206 LLVMValueRef *sizes_out);
207 };
208
209
210 struct lp_build_sampler_aos
211 {
212 LLVMValueRef
213 (*emit_fetch_texel)( struct lp_build_sampler_aos *sampler,
214 struct lp_build_context *bld,
215 unsigned target, /* TGSI_TEXTURE_* */
216 unsigned unit,
217 LLVMValueRef coords,
218 const struct lp_derivatives derivs,
219 enum lp_build_tex_modifier modifier);
220 };
221
222
223 void
224 lp_build_tgsi_info(const struct tgsi_token *tokens,
225 struct lp_tgsi_info *info);
226
227
228 void
229 lp_build_tgsi_soa(struct gallivm_state *gallivm,
230 const struct tgsi_token *tokens,
231 struct lp_type type,
232 struct lp_build_mask_context *mask,
233 LLVMValueRef consts_ptr,
234 LLVMValueRef const_sizes_ptr,
235 const struct lp_bld_tgsi_system_values *system_values,
236 const LLVMValueRef (*inputs)[4],
237 LLVMValueRef (*outputs)[4],
238 struct lp_build_sampler_soa *sampler,
239 const struct tgsi_shader_info *info,
240 const struct lp_build_tgsi_gs_iface *gs_iface);
241
242
243 void
244 lp_build_tgsi_aos(struct gallivm_state *gallivm,
245 const struct tgsi_token *tokens,
246 struct lp_type type,
247 const unsigned char swizzles[4],
248 LLVMValueRef consts_ptr,
249 const LLVMValueRef *inputs,
250 LLVMValueRef *outputs,
251 struct lp_build_sampler_aos *sampler,
252 const struct tgsi_shader_info *info);
253
254
255 enum lp_exec_mask_break_type {
256 LP_EXEC_MASK_BREAK_TYPE_LOOP,
257 LP_EXEC_MASK_BREAK_TYPE_SWITCH
258 };
259
260
261 struct lp_exec_mask {
262 struct lp_build_context *bld;
263
264 boolean has_mask;
265 boolean ret_in_main;
266
267 LLVMTypeRef int_vec_type;
268
269 LLVMValueRef exec_mask;
270
271 LLVMValueRef ret_mask;
272 LLVMValueRef cond_mask;
273 LLVMValueRef switch_mask; /* current switch exec mask */
274 LLVMValueRef cont_mask;
275 LLVMValueRef break_mask;
276
277 struct function_ctx {
278 int pc;
279 LLVMValueRef ret_mask;
280
281 LLVMValueRef cond_stack[LP_MAX_TGSI_NESTING];
282 int cond_stack_size;
283
284 /* keep track if break belongs to switch or loop */
285 enum lp_exec_mask_break_type break_type_stack[LP_MAX_TGSI_NESTING];
286 enum lp_exec_mask_break_type break_type;
287
288 struct {
289 LLVMValueRef switch_val;
290 LLVMValueRef switch_mask;
291 LLVMValueRef switch_mask_default;
292 boolean switch_in_default;
293 unsigned switch_pc;
294 } switch_stack[LP_MAX_TGSI_NESTING];
295 int switch_stack_size;
296 LLVMValueRef switch_val;
297 LLVMValueRef switch_mask_default; /* reverse of switch mask used for default */
298 boolean switch_in_default; /* if switch exec is currently in default */
299 unsigned switch_pc; /* when used points to default or endswitch-1 */
300
301 LLVMValueRef loop_limiter;
302 LLVMBasicBlockRef loop_block;
303 LLVMValueRef break_var;
304 struct {
305 LLVMBasicBlockRef loop_block;
306 LLVMValueRef cont_mask;
307 LLVMValueRef break_mask;
308 LLVMValueRef break_var;
309 } loop_stack[LP_MAX_TGSI_NESTING];
310 int loop_stack_size;
311
312 } *function_stack;
313 int function_stack_size;
314 };
315
316 struct lp_build_tgsi_inst_list
317 {
318 struct tgsi_full_instruction *instructions;
319 uint max_instructions;
320 uint num_instructions;
321 };
322
323 unsigned lp_bld_tgsi_list_init(struct lp_build_tgsi_context * bld_base);
324
325
326 unsigned lp_bld_tgsi_add_instruction(
327 struct lp_build_tgsi_context * bld_base,
328 const struct tgsi_full_instruction *inst_to_add);
329
330
331 struct lp_build_tgsi_context;
332
333
334 typedef LLVMValueRef (*lp_build_emit_fetch_fn)(struct lp_build_tgsi_context *,
335 const struct tgsi_full_src_register *,
336 enum tgsi_opcode_type,
337 unsigned);
338
339 struct lp_build_tgsi_context
340 {
341 struct lp_build_context base;
342
343 struct lp_build_context uint_bld;
344 struct lp_build_context int_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 const struct tgsi_shader_info *info;
358
359 lp_build_emit_fetch_fn emit_fetch_funcs[TGSI_FILE_COUNT];
360
361 LLVMValueRef (*emit_swizzle)(struct lp_build_tgsi_context *,
362 LLVMValueRef, unsigned, unsigned, unsigned, unsigned);
363
364
365 void (*emit_debug)(struct lp_build_tgsi_context *,
366 const struct tgsi_full_instruction *,
367 const struct tgsi_opcode_info *);
368
369 void (*emit_store)(struct lp_build_tgsi_context *,
370 const struct tgsi_full_instruction *,
371 const struct tgsi_opcode_info *,
372 LLVMValueRef dst[4]);
373
374 void (*emit_declaration)(struct lp_build_tgsi_context *,
375 const struct tgsi_full_declaration *decl);
376
377 void (*emit_immediate)(struct lp_build_tgsi_context *,
378 const struct tgsi_full_immediate *imm);
379
380
381 /* Allow the user to store data in this structure rather than passing it
382 * to every function. */
383 void * userdata;
384
385 boolean soa;
386
387 int pc;
388
389 struct tgsi_full_instruction *instructions;
390 uint max_instructions;
391 uint num_instructions;
392
393 /** This function allows the user to insert some instructions at the
394 * beginning of the program. It is optional and does not need to be
395 * implemented.
396 */
397 void (*emit_prologue)(struct lp_build_tgsi_context*);
398
399 /** This function allows the user to insert some instructions at the end of
400 * the program. This callback is intended to be used for emitting
401 * instructions to handle the export for the output registers, but it can
402 * be used for any purpose. Implementing this function is optiona, but
403 * recommended.
404 */
405 void (*emit_epilogue)(struct lp_build_tgsi_context*);
406 };
407
408 struct lp_build_tgsi_gs_iface
409 {
410 LLVMValueRef (*fetch_input)(const struct lp_build_tgsi_gs_iface *gs_iface,
411 struct lp_build_tgsi_context * bld_base,
412 boolean is_vindex_indirect,
413 LLVMValueRef vertex_index,
414 boolean is_aindex_indirect,
415 LLVMValueRef attrib_index,
416 LLVMValueRef swizzle_index);
417 void (*emit_vertex)(const struct lp_build_tgsi_gs_iface *gs_iface,
418 struct lp_build_tgsi_context * bld_base,
419 LLVMValueRef (*outputs)[4],
420 LLVMValueRef emitted_vertices_vec);
421 void (*end_primitive)(const struct lp_build_tgsi_gs_iface *gs_iface,
422 struct lp_build_tgsi_context * bld_base,
423 LLVMValueRef verts_per_prim_vec,
424 LLVMValueRef emitted_prims_vec);
425 void (*gs_epilogue)(const struct lp_build_tgsi_gs_iface *gs_iface,
426 struct lp_build_tgsi_context * bld_base,
427 LLVMValueRef total_emitted_vertices_vec,
428 LLVMValueRef emitted_prims_vec);
429 };
430
431 struct lp_build_tgsi_soa_context
432 {
433 struct lp_build_tgsi_context bld_base;
434
435 /* Builder for scalar elements of shader's data type (float) */
436 struct lp_build_context elem_bld;
437
438 const struct lp_build_tgsi_gs_iface *gs_iface;
439 LLVMValueRef emitted_prims_vec_ptr;
440 LLVMValueRef total_emitted_vertices_vec_ptr;
441 LLVMValueRef emitted_vertices_vec_ptr;
442 LLVMValueRef max_output_vertices_vec;
443
444 LLVMValueRef consts_ptr;
445 LLVMValueRef const_sizes_ptr;
446 LLVMValueRef consts[LP_MAX_TGSI_CONST_BUFFERS];
447 LLVMValueRef consts_sizes[LP_MAX_TGSI_CONST_BUFFERS];
448 const LLVMValueRef (*inputs)[TGSI_NUM_CHANNELS];
449 LLVMValueRef (*outputs)[TGSI_NUM_CHANNELS];
450
451 const struct lp_build_sampler_soa *sampler;
452
453 struct tgsi_declaration_sampler_view sv[PIPE_MAX_SHADER_SAMPLER_VIEWS];
454
455 LLVMValueRef immediates[LP_MAX_INLINED_IMMEDIATES][TGSI_NUM_CHANNELS];
456 LLVMValueRef temps[LP_MAX_INLINED_TEMPS][TGSI_NUM_CHANNELS];
457 LLVMValueRef addr[LP_MAX_TGSI_ADDRS][TGSI_NUM_CHANNELS];
458 LLVMValueRef preds[LP_MAX_TGSI_PREDS][TGSI_NUM_CHANNELS];
459
460 /* We allocate/use this array of temps if (1 << TGSI_FILE_TEMPORARY) is
461 * set in the indirect_files field.
462 * The temps[] array above is unused then.
463 */
464 LLVMValueRef temps_array;
465
466 /* We allocate/use this array of output if (1 << TGSI_FILE_OUTPUT) is
467 * set in the indirect_files field.
468 * The outputs[] array above is unused then.
469 */
470 LLVMValueRef outputs_array;
471
472 /* We allocate/use this array of inputs if (1 << TGSI_FILE_INPUT) is
473 * set in the indirect_files field.
474 * The inputs[] array above is unused then.
475 */
476 LLVMValueRef inputs_array;
477
478 /* We allocate/use this array of temps if (1 << TGSI_FILE_IMMEDIATE) is
479 * set in the indirect_files field.
480 */
481 LLVMValueRef imms_array;
482
483
484 struct lp_bld_tgsi_system_values system_values;
485
486 /** bitmask indicating which register files are accessed indirectly */
487 unsigned indirect_files;
488
489 struct lp_build_mask_context *mask;
490 struct lp_exec_mask exec_mask;
491
492 uint num_immediates;
493 boolean use_immediates_array;
494 };
495
496 void
497 lp_emit_declaration_soa(
498 struct lp_build_tgsi_context *bld,
499 const struct tgsi_full_declaration *decl);
500
501 void lp_emit_immediate_soa(
502 struct lp_build_tgsi_context *bld_base,
503 const struct tgsi_full_immediate *imm);
504
505 boolean
506 lp_emit_instruction_soa(
507 struct lp_build_tgsi_soa_context *bld,
508 const struct tgsi_full_instruction *inst,
509 const struct tgsi_opcode_info *info);
510
511
512 LLVMValueRef
513 lp_get_temp_ptr_soa(
514 struct lp_build_tgsi_soa_context *bld,
515 unsigned index,
516 unsigned chan);
517
518 LLVMValueRef
519 lp_get_output_ptr(
520 struct lp_build_tgsi_soa_context *bld,
521 unsigned index,
522 unsigned chan);
523
524 struct lp_build_tgsi_aos_context
525 {
526 struct lp_build_tgsi_context bld_base;
527
528 /* Builder for integer masks and indices */
529 struct lp_build_context int_bld;
530
531 /*
532 * AoS swizzle used:
533 * - swizzles[0] = red index
534 * - swizzles[1] = green index
535 * - swizzles[2] = blue index
536 * - swizzles[3] = alpha index
537 */
538 unsigned char swizzles[4];
539 unsigned char inv_swizzles[4];
540
541 LLVMValueRef consts_ptr;
542 const LLVMValueRef *inputs;
543 LLVMValueRef *outputs;
544
545 struct lp_build_sampler_aos *sampler;
546
547 struct tgsi_declaration_sampler_view sv[PIPE_MAX_SHADER_SAMPLER_VIEWS];
548
549 LLVMValueRef immediates[LP_MAX_INLINED_IMMEDIATES];
550 LLVMValueRef temps[LP_MAX_INLINED_TEMPS];
551 LLVMValueRef addr[LP_MAX_TGSI_ADDRS];
552 LLVMValueRef preds[LP_MAX_TGSI_PREDS];
553
554 /* We allocate/use this array of temps if (1 << TGSI_FILE_TEMPORARY) is
555 * set in the indirect_files field.
556 * The temps[] array above is unused then.
557 */
558 LLVMValueRef temps_array;
559
560 /** bitmask indicating which register files are accessed indirectly */
561 unsigned indirect_files;
562
563 };
564
565 static INLINE struct lp_build_tgsi_soa_context *
566 lp_soa_context(struct lp_build_tgsi_context *bld_base)
567 {
568 return (struct lp_build_tgsi_soa_context *)bld_base;
569 }
570
571 static INLINE struct lp_build_tgsi_aos_context *
572 lp_aos_context(struct lp_build_tgsi_context *bld_base)
573 {
574 return (struct lp_build_tgsi_aos_context *)bld_base;
575 }
576
577 void
578 lp_emit_declaration_aos(
579 struct lp_build_tgsi_aos_context *bld,
580 const struct tgsi_full_declaration *decl);
581
582
583 boolean
584 lp_emit_instruction_aos(
585 struct lp_build_tgsi_aos_context *bld,
586 const struct tgsi_full_instruction *inst,
587 const struct tgsi_opcode_info *info,
588 int *pc);
589
590 void
591 lp_emit_store_aos(
592 struct lp_build_tgsi_aos_context *bld,
593 const struct tgsi_full_instruction *inst,
594 unsigned index,
595 LLVMValueRef value);
596
597 void lp_build_fetch_args(
598 struct lp_build_tgsi_context * bld_base,
599 struct lp_build_emit_data * emit_data);
600
601 LLVMValueRef
602 lp_build_tgsi_inst_llvm_aos(
603 struct lp_build_tgsi_context * bld_base,
604 const struct tgsi_full_instruction *inst);
605
606 void
607 lp_build_tgsi_intrinsic(
608 const struct lp_build_tgsi_action * action,
609 struct lp_build_tgsi_context * bld_base,
610 struct lp_build_emit_data * emit_data);
611
612 LLVMValueRef
613 lp_build_emit_llvm(
614 struct lp_build_tgsi_context *bld_base,
615 unsigned tgsi_opcode,
616 struct lp_build_emit_data * emit_data);
617
618 LLVMValueRef
619 lp_build_emit_llvm_unary(
620 struct lp_build_tgsi_context *bld_base,
621 unsigned tgsi_opcode,
622 LLVMValueRef arg0);
623
624 LLVMValueRef
625 lp_build_emit_llvm_binary(
626 struct lp_build_tgsi_context *bld_base,
627 unsigned tgsi_opcode,
628 LLVMValueRef arg0,
629 LLVMValueRef arg1);
630
631 LLVMValueRef
632 lp_build_emit_llvm_ternary(
633 struct lp_build_tgsi_context *bld_base,
634 unsigned tgsi_opcode,
635 LLVMValueRef arg0,
636 LLVMValueRef arg1,
637 LLVMValueRef arg2);
638
639 boolean
640 lp_build_tgsi_inst_llvm(
641 struct lp_build_tgsi_context * bld_base,
642 const struct tgsi_full_instruction *inst);
643
644 LLVMValueRef
645 lp_build_emit_fetch(
646 struct lp_build_tgsi_context *bld_base,
647 const struct tgsi_full_instruction *inst,
648 unsigned src_op,
649 const unsigned chan_index);
650
651
652 LLVMValueRef
653 lp_build_emit_fetch_texoffset(
654 struct lp_build_tgsi_context *bld_base,
655 const struct tgsi_full_instruction *inst,
656 unsigned tex_off_op,
657 const unsigned chan_index);
658
659 boolean
660 lp_build_tgsi_llvm(
661 struct lp_build_tgsi_context * bld_base,
662 const struct tgsi_token *tokens);
663
664 #endif /* LP_BLD_TGSI_H */