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