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