gallivm: cleanup the gs 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 "lp_bld_type.h"
44 #include "pipe/p_compiler.h"
45 #include "pipe/p_state.h"
46 #include "tgsi/tgsi_exec.h"
47 #include "tgsi/tgsi_scan.h"
48 #include "tgsi/tgsi_info.h"
49
50 #define LP_CHAN_ALL ~0
51
52 #define LP_MAX_INSTRUCTIONS 256
53
54 struct tgsi_full_declaration;
55 struct tgsi_full_immediate;
56 struct tgsi_full_instruction;
57 struct tgsi_full_src_register;
58 struct tgsi_opcode_info;
59 struct tgsi_token;
60 struct tgsi_shader_info;
61 struct lp_build_mask_context;
62 struct gallivm_state;
63 struct lp_derivatives;
64 struct lp_build_tgsi_gs_iface;
65
66
67 enum lp_build_tex_modifier {
68 LP_BLD_TEX_MODIFIER_NONE = 0,
69 LP_BLD_TEX_MODIFIER_PROJECTED,
70 LP_BLD_TEX_MODIFIER_LOD_BIAS,
71 LP_BLD_TEX_MODIFIER_EXPLICIT_LOD,
72 LP_BLD_TEX_MODIFIER_EXPLICIT_DERIV,
73 LP_BLD_TEX_MODIFIER_LOD_ZERO
74 };
75
76
77 /**
78 * Describe a channel of a register.
79 *
80 * The value can be a:
81 * - immediate value (i.e. derived from a IMM register)
82 * - CONST[n].x/y/z/w
83 * - IN[n].x/y/z/w
84 * - undetermined (when .file == TGSI_FILE_NULL)
85 *
86 * This is one of the analysis results, and is used to described
87 * the output color in terms of inputs.
88 */
89 struct lp_tgsi_channel_info
90 {
91 unsigned file:4; /* TGSI_FILE_* */
92 unsigned swizzle:3; /* PIPE_SWIZZLE_x */
93 union {
94 uint32_t index;
95 float value; /* for TGSI_FILE_IMMEDIATE */
96 } u;
97 };
98
99
100 /**
101 * Describe a texture sampler interpolator.
102 *
103 * The interpolation is described in terms of regular inputs.
104 */
105 struct lp_tgsi_texture_info
106 {
107 struct lp_tgsi_channel_info coord[4];
108 unsigned target:8; /* TGSI_TEXTURE_* */
109 unsigned sampler_unit:8; /* Sampler unit */
110 unsigned texture_unit:8; /* Texture unit */
111 unsigned modifier:8; /* LP_BLD_TEX_MODIFIER_* */
112 };
113
114
115 struct lp_tgsi_info
116 {
117 struct tgsi_shader_info base;
118
119 /*
120 * Whether any of the texture opcodes access a register file other than
121 * TGSI_FILE_INPUT.
122 *
123 * We could also handle TGSI_FILE_CONST/IMMEDIATE here, but there is little
124 * benefit.
125 */
126 unsigned indirect_textures:1;
127
128 /*
129 * Whether any immediate values are outside the range of 0 and 1
130 */
131 unsigned unclamped_immediates:1;
132
133 /*
134 * Texture opcode description. Aimed at detecting and described direct
135 * texture opcodes.
136 */
137 unsigned num_texs;
138 struct lp_tgsi_texture_info tex[PIPE_MAX_SAMPLERS];
139
140 /*
141 * Output description. Aimed at detecting and describing simple blit
142 * shaders.
143 */
144 struct lp_tgsi_channel_info output[PIPE_MAX_SHADER_OUTPUTS][4];
145
146 /*
147 * Shortcut pointers into the above (for fragment shaders).
148 */
149 const struct lp_tgsi_channel_info *cbuf[PIPE_MAX_COLOR_BUFS];
150 };
151
152 /**
153 * Reference to system values.
154 */
155 struct lp_bld_tgsi_system_values {
156 LLVMValueRef instance_id;
157 LLVMValueRef vertex_id;
158 };
159
160
161 /**
162 * Sampler code generation interface.
163 *
164 * Although texture sampling is a requirement for TGSI translation, it is
165 * a very different problem with several different approaches to it. This
166 * structure establishes an interface for texture sampling code generation, so
167 * that we can easily use different texture sampling strategies.
168 */
169 struct lp_build_sampler_soa
170 {
171 void
172 (*destroy)( struct lp_build_sampler_soa *sampler );
173
174 void
175 (*emit_fetch_texel)( const struct lp_build_sampler_soa *sampler,
176 struct gallivm_state *gallivm,
177 struct lp_type type,
178 boolean is_fetch,
179 unsigned texture_index,
180 unsigned sampler_index,
181 const LLVMValueRef *coords,
182 const LLVMValueRef *offsets,
183 const struct lp_derivatives *derivs,
184 LLVMValueRef lod_bias, /* optional */
185 LLVMValueRef explicit_lod, /* optional */
186 LLVMValueRef *texel);
187
188 void
189 (*emit_size_query)( const struct lp_build_sampler_soa *sampler,
190 struct gallivm_state *gallivm,
191 struct lp_type type,
192 unsigned unit,
193 boolean need_nr_mips,
194 LLVMValueRef explicit_lod, /* optional */
195 LLVMValueRef *sizes_out);
196 };
197
198
199 struct lp_build_sampler_aos
200 {
201 LLVMValueRef
202 (*emit_fetch_texel)( struct lp_build_sampler_aos *sampler,
203 struct lp_build_context *bld,
204 unsigned target, /* TGSI_TEXTURE_* */
205 unsigned unit,
206 LLVMValueRef coords,
207 const struct lp_derivatives derivs,
208 enum lp_build_tex_modifier modifier);
209 };
210
211
212 void
213 lp_build_tgsi_info(const struct tgsi_token *tokens,
214 struct lp_tgsi_info *info);
215
216
217 void
218 lp_build_tgsi_soa(struct gallivm_state *gallivm,
219 const struct tgsi_token *tokens,
220 struct lp_type type,
221 struct lp_build_mask_context *mask,
222 LLVMValueRef consts_ptr,
223 const struct lp_bld_tgsi_system_values *system_values,
224 const LLVMValueRef *pos,
225 const LLVMValueRef (*inputs)[4],
226 LLVMValueRef (*outputs)[4],
227 struct lp_build_sampler_soa *sampler,
228 const struct tgsi_shader_info *info,
229 const struct lp_build_tgsi_gs_iface *gs_iface);
230
231
232 void
233 lp_build_tgsi_aos(struct gallivm_state *gallivm,
234 const struct tgsi_token *tokens,
235 struct lp_type type,
236 const unsigned char swizzles[4],
237 LLVMValueRef consts_ptr,
238 const LLVMValueRef *inputs,
239 LLVMValueRef *outputs,
240 struct lp_build_sampler_aos *sampler,
241 const struct tgsi_shader_info *info);
242
243
244 struct lp_exec_mask {
245 struct lp_build_context *bld;
246
247 boolean has_mask;
248 boolean ret_in_main;
249
250 LLVMTypeRef int_vec_type;
251
252 LLVMValueRef cond_stack[LP_MAX_TGSI_NESTING];
253 int cond_stack_size;
254 LLVMValueRef cond_mask;
255
256 LLVMBasicBlockRef loop_block;
257 LLVMValueRef cont_mask;
258 LLVMValueRef break_mask;
259 LLVMValueRef break_var;
260 struct {
261 LLVMBasicBlockRef loop_block;
262 LLVMValueRef cont_mask;
263 LLVMValueRef break_mask;
264 LLVMValueRef break_var;
265 } loop_stack[LP_MAX_TGSI_NESTING];
266 int loop_stack_size;
267
268 LLVMValueRef ret_mask;
269 struct {
270 int pc;
271 LLVMValueRef ret_mask;
272 } call_stack[LP_MAX_TGSI_NESTING];
273 int call_stack_size;
274
275 LLVMValueRef exec_mask;
276 LLVMValueRef loop_limiter;
277 };
278
279 struct lp_build_tgsi_inst_list
280 {
281 struct tgsi_full_instruction *instructions;
282 uint max_instructions;
283 uint num_instructions;
284 };
285
286 unsigned lp_bld_tgsi_list_init(struct lp_build_tgsi_context * bld_base);
287
288
289 unsigned lp_bld_tgsi_add_instruction(
290 struct lp_build_tgsi_context * bld_base,
291 struct tgsi_full_instruction *inst_to_add);
292
293
294 struct lp_build_tgsi_context;
295
296
297 typedef LLVMValueRef (*lp_build_emit_fetch_fn)(struct lp_build_tgsi_context *,
298 const struct tgsi_full_src_register *,
299 enum tgsi_opcode_type,
300 unsigned);
301
302 struct lp_build_tgsi_context
303 {
304 struct lp_build_context base;
305
306 struct lp_build_context uint_bld;
307 struct lp_build_context int_bld;
308
309 /** This array stores functions that are used to transform TGSI opcodes to
310 * LLVM instructions.
311 */
312 struct lp_build_tgsi_action op_actions[TGSI_OPCODE_LAST];
313
314 /* TGSI_OPCODE_RSQ is defined as 1 / sqrt( abs(src0.x) ), rsq_action
315 * should compute 1 / sqrt (src0.x) */
316 struct lp_build_tgsi_action rsq_action;
317
318 struct lp_build_tgsi_action sqrt_action;
319
320 const struct tgsi_shader_info *info;
321
322 lp_build_emit_fetch_fn emit_fetch_funcs[TGSI_FILE_COUNT];
323
324 LLVMValueRef (*emit_swizzle)(struct lp_build_tgsi_context *,
325 LLVMValueRef, unsigned, unsigned, unsigned, unsigned);
326
327 void (*emit_store)(struct lp_build_tgsi_context *,
328 const struct tgsi_full_instruction *,
329 const struct tgsi_opcode_info *,
330 LLVMValueRef dst[4]);
331
332 void (*emit_declaration)(struct lp_build_tgsi_context *,
333 const struct tgsi_full_declaration *decl);
334
335 void (*emit_immediate)(struct lp_build_tgsi_context *,
336 const struct tgsi_full_immediate *imm);
337
338
339 /* Allow the user to store data in this structure rather than passing it
340 * to every function. */
341 void * userdata;
342
343 boolean soa;
344
345 int pc;
346
347 struct tgsi_full_instruction *instructions;
348 uint max_instructions;
349 uint num_instructions;
350
351 /** This function allows the user to insert some instructions at the
352 * beginning of the program. It is optional and does not need to be
353 * implemented.
354 */
355 void (*emit_prologue)(struct lp_build_tgsi_context*);
356
357 /** This function allows the user to insert some instructions at the end of
358 * the program. This callback is intended to be used for emitting
359 * instructions to handle the export for the output registers, but it can
360 * be used for any purpose. Implementing this function is optiona, but
361 * recommended.
362 */
363 void (*emit_epilogue)(struct lp_build_tgsi_context*);
364 };
365
366 struct lp_build_tgsi_gs_iface
367 {
368 LLVMValueRef (*fetch_input)(const struct lp_build_tgsi_gs_iface *gs_iface,
369 struct lp_build_tgsi_context * bld_base,
370 LLVMValueRef vertex_index,
371 LLVMValueRef attrib_index,
372 LLVMValueRef swizzle_index);
373 void (*emit_vertex)(const struct lp_build_tgsi_gs_iface *gs_iface,
374 struct lp_build_tgsi_context * bld_base,
375 LLVMValueRef (*outputs)[4],
376 LLVMValueRef emitted_vertices_vec);
377 void (*end_primitive)(const struct lp_build_tgsi_gs_iface *gs_iface,
378 struct lp_build_tgsi_context * bld_base,
379 LLVMValueRef verts_per_prim_vec,
380 LLVMValueRef emitted_prims_vec);
381 void (*gs_epilogue)(const struct lp_build_tgsi_gs_iface *gs_iface,
382 struct lp_build_tgsi_context * bld_base,
383 LLVMValueRef total_emitted_vertices_vec,
384 LLVMValueRef emitted_prims_vec);
385 };
386
387 struct lp_build_tgsi_soa_context
388 {
389 struct lp_build_tgsi_context bld_base;
390
391 /* Builder for scalar elements of shader's data type (float) */
392 struct lp_build_context elem_bld;
393
394 const struct lp_build_tgsi_gs_iface *gs_iface;
395 LLVMValueRef emitted_prims_vec;
396 LLVMValueRef total_emitted_vertices_vec;
397 LLVMValueRef emitted_vertices_vec;
398 /* if a shader doesn't have ENDPRIM instruction but it has
399 * a number of EMIT instructions it means the END instruction
400 * implicitly invokes ENDPRIM. handle this via a flag here
401 * in the future maybe we can enforce TGSI to always have
402 * an explicit ENDPRIM */
403 boolean pending_end_primitive;
404
405 LLVMValueRef consts_ptr;
406 const LLVMValueRef *pos;
407 const LLVMValueRef (*inputs)[TGSI_NUM_CHANNELS];
408 LLVMValueRef (*outputs)[TGSI_NUM_CHANNELS];
409
410 const struct lp_build_sampler_soa *sampler;
411
412 struct tgsi_declaration_sampler_view sv[PIPE_MAX_SHADER_SAMPLER_VIEWS];
413
414 LLVMValueRef immediates[LP_MAX_TGSI_IMMEDIATES][TGSI_NUM_CHANNELS];
415 LLVMValueRef temps[LP_MAX_TGSI_TEMPS][TGSI_NUM_CHANNELS];
416 LLVMValueRef addr[LP_MAX_TGSI_ADDRS][TGSI_NUM_CHANNELS];
417 LLVMValueRef preds[LP_MAX_TGSI_PREDS][TGSI_NUM_CHANNELS];
418
419 /* We allocate/use this array of temps if (1 << TGSI_FILE_TEMPORARY) is
420 * set in the indirect_files field.
421 * The temps[] array above is unused then.
422 */
423 LLVMValueRef temps_array;
424
425 /* We allocate/use this array of output if (1 << TGSI_FILE_OUTPUT) is
426 * set in the indirect_files field.
427 * The outputs[] array above is unused then.
428 */
429 LLVMValueRef outputs_array;
430
431 /* We allocate/use this array of inputs if (1 << TGSI_FILE_INPUT) is
432 * set in the indirect_files field.
433 * The inputs[] array above is unused then.
434 */
435 LLVMValueRef inputs_array;
436
437 struct lp_bld_tgsi_system_values system_values;
438
439 /** bitmask indicating which register files are accessed indirectly */
440 unsigned indirect_files;
441
442 struct lp_build_mask_context *mask;
443 struct lp_exec_mask exec_mask;
444
445 uint num_immediates;
446
447 };
448
449 void
450 lp_emit_declaration_soa(
451 struct lp_build_tgsi_context *bld,
452 const struct tgsi_full_declaration *decl);
453
454 void lp_emit_immediate_soa(
455 struct lp_build_tgsi_context *bld_base,
456 const struct tgsi_full_immediate *imm);
457
458 boolean
459 lp_emit_instruction_soa(
460 struct lp_build_tgsi_soa_context *bld,
461 const struct tgsi_full_instruction *inst,
462 const struct tgsi_opcode_info *info);
463
464
465 LLVMValueRef
466 lp_get_temp_ptr_soa(
467 struct lp_build_tgsi_soa_context *bld,
468 unsigned index,
469 unsigned chan);
470
471 LLVMValueRef
472 lp_get_output_ptr(
473 struct lp_build_tgsi_soa_context *bld,
474 unsigned index,
475 unsigned chan);
476
477 struct lp_build_tgsi_aos_context
478 {
479 struct lp_build_tgsi_context bld_base;
480
481 /* Builder for integer masks and indices */
482 struct lp_build_context int_bld;
483
484 /*
485 * AoS swizzle used:
486 * - swizzles[0] = red index
487 * - swizzles[1] = green index
488 * - swizzles[2] = blue index
489 * - swizzles[3] = alpha index
490 */
491 unsigned char swizzles[4];
492 unsigned char inv_swizzles[4];
493
494 LLVMValueRef consts_ptr;
495 const LLVMValueRef *inputs;
496 LLVMValueRef *outputs;
497
498 struct lp_build_sampler_aos *sampler;
499
500 LLVMValueRef immediates[LP_MAX_TGSI_IMMEDIATES];
501 LLVMValueRef temps[LP_MAX_TGSI_TEMPS];
502 LLVMValueRef addr[LP_MAX_TGSI_ADDRS];
503 LLVMValueRef preds[LP_MAX_TGSI_PREDS];
504
505 /* We allocate/use this array of temps if (1 << TGSI_FILE_TEMPORARY) is
506 * set in the indirect_files field.
507 * The temps[] array above is unused then.
508 */
509 LLVMValueRef temps_array;
510
511 /** bitmask indicating which register files are accessed indirectly */
512 unsigned indirect_files;
513
514 };
515
516 static INLINE struct lp_build_tgsi_soa_context *
517 lp_soa_context(struct lp_build_tgsi_context *bld_base)
518 {
519 return (struct lp_build_tgsi_soa_context *)bld_base;
520 }
521
522 static INLINE struct lp_build_tgsi_aos_context *
523 lp_aos_context(struct lp_build_tgsi_context *bld_base)
524 {
525 return (struct lp_build_tgsi_aos_context *)bld_base;
526 }
527
528 void
529 lp_emit_declaration_aos(
530 struct lp_build_tgsi_aos_context *bld,
531 const struct tgsi_full_declaration *decl);
532
533
534 boolean
535 lp_emit_instruction_aos(
536 struct lp_build_tgsi_aos_context *bld,
537 const struct tgsi_full_instruction *inst,
538 const struct tgsi_opcode_info *info,
539 int *pc);
540
541 void
542 lp_emit_store_aos(
543 struct lp_build_tgsi_aos_context *bld,
544 const struct tgsi_full_instruction *inst,
545 unsigned index,
546 LLVMValueRef value);
547
548 void lp_build_fetch_args(
549 struct lp_build_tgsi_context * bld_base,
550 struct lp_build_emit_data * emit_data);
551
552 LLVMValueRef
553 lp_build_tgsi_inst_llvm_aos(
554 struct lp_build_tgsi_context * bld_base,
555 const struct tgsi_full_instruction *inst);
556
557 void
558 lp_build_tgsi_intrinsic(
559 const struct lp_build_tgsi_action * action,
560 struct lp_build_tgsi_context * bld_base,
561 struct lp_build_emit_data * emit_data);
562
563 LLVMValueRef
564 lp_build_emit_llvm(
565 struct lp_build_tgsi_context *bld_base,
566 unsigned tgsi_opcode,
567 struct lp_build_emit_data * emit_data);
568
569 LLVMValueRef
570 lp_build_emit_llvm_unary(
571 struct lp_build_tgsi_context *bld_base,
572 unsigned tgsi_opcode,
573 LLVMValueRef arg0);
574
575 LLVMValueRef
576 lp_build_emit_llvm_binary(
577 struct lp_build_tgsi_context *bld_base,
578 unsigned tgsi_opcode,
579 LLVMValueRef arg0,
580 LLVMValueRef arg1);
581
582 LLVMValueRef
583 lp_build_emit_llvm_ternary(
584 struct lp_build_tgsi_context *bld_base,
585 unsigned tgsi_opcode,
586 LLVMValueRef arg0,
587 LLVMValueRef arg1,
588 LLVMValueRef arg2);
589
590 boolean
591 lp_build_tgsi_inst_llvm(
592 struct lp_build_tgsi_context * bld_base,
593 const struct tgsi_full_instruction *inst);
594
595 LLVMValueRef
596 lp_build_emit_fetch(
597 struct lp_build_tgsi_context *bld_base,
598 const struct tgsi_full_instruction *inst,
599 unsigned src_op,
600 const unsigned chan_index);
601
602
603 LLVMValueRef
604 lp_build_emit_fetch_texoffset(
605 struct lp_build_tgsi_context *bld_base,
606 const struct tgsi_full_instruction *inst,
607 unsigned tex_off_op,
608 const unsigned chan_index);
609
610 boolean
611 lp_build_tgsi_llvm(
612 struct lp_build_tgsi_context * bld_base,
613 const struct tgsi_token *tokens);
614
615 #endif /* LP_BLD_TGSI_H */