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