gallivm: Use llvm.fmuladd.*.
[mesa.git] / src / gallium / auxiliary / draw / draw_llvm.c
1 /**************************************************************************
2 *
3 * Copyright 2010 VMware, Inc.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28 #include "draw_llvm.h"
29
30 #include "draw_context.h"
31 #include "draw_vs.h"
32 #include "draw_gs.h"
33
34 #include "gallivm/lp_bld_arit.h"
35 #include "gallivm/lp_bld_arit_overflow.h"
36 #include "gallivm/lp_bld_logic.h"
37 #include "gallivm/lp_bld_const.h"
38 #include "gallivm/lp_bld_swizzle.h"
39 #include "gallivm/lp_bld_struct.h"
40 #include "gallivm/lp_bld_type.h"
41 #include "gallivm/lp_bld_flow.h"
42 #include "gallivm/lp_bld_debug.h"
43 #include "gallivm/lp_bld_tgsi.h"
44 #include "gallivm/lp_bld_printf.h"
45 #include "gallivm/lp_bld_intr.h"
46 #include "gallivm/lp_bld_init.h"
47 #include "gallivm/lp_bld_type.h"
48 #include "gallivm/lp_bld_pack.h"
49 #include "gallivm/lp_bld_format.h"
50
51 #include "tgsi/tgsi_exec.h"
52 #include "tgsi/tgsi_dump.h"
53
54 #include "util/u_math.h"
55 #include "util/u_pointer.h"
56 #include "util/u_string.h"
57 #include "util/simple_list.h"
58
59
60 #define DEBUG_STORE 0
61
62
63 static void
64 draw_llvm_generate(struct draw_llvm *llvm, struct draw_llvm_variant *var,
65 boolean elts);
66
67
68 struct draw_gs_llvm_iface {
69 struct lp_build_tgsi_gs_iface base;
70
71 struct draw_gs_llvm_variant *variant;
72 LLVMValueRef input;
73 };
74
75 static inline const struct draw_gs_llvm_iface *
76 draw_gs_llvm_iface(const struct lp_build_tgsi_gs_iface *iface)
77 {
78 return (const struct draw_gs_llvm_iface *)iface;
79 }
80
81 /**
82 * Create LLVM type for draw_vertex_buffer.
83 */
84 static LLVMTypeRef
85 create_jit_dvbuffer_type(struct gallivm_state *gallivm,
86 const char *struct_name)
87 {
88 LLVMTargetDataRef target = gallivm->target;
89 LLVMTypeRef dvbuffer_type;
90 LLVMTypeRef elem_types[DRAW_JIT_DVBUFFER_NUM_FIELDS];
91 LLVMTypeRef int32_type = LLVMInt32TypeInContext(gallivm->context);
92
93 elem_types[DRAW_JIT_DVBUFFER_MAP] =
94 LLVMPointerType(LLVMIntTypeInContext(gallivm->context, 8), 0);
95 elem_types[DRAW_JIT_DVBUFFER_SIZE] = int32_type;
96
97 dvbuffer_type = LLVMStructTypeInContext(gallivm->context, elem_types,
98 ARRAY_SIZE(elem_types), 0);
99
100 (void) target; /* silence unused var warning for non-debug build */
101 LP_CHECK_MEMBER_OFFSET(struct draw_vertex_buffer, map,
102 target, dvbuffer_type,
103 DRAW_JIT_DVBUFFER_MAP);
104 LP_CHECK_MEMBER_OFFSET(struct draw_vertex_buffer, size,
105 target, dvbuffer_type,
106 DRAW_JIT_DVBUFFER_SIZE);
107
108 return dvbuffer_type;
109 }
110
111 /**
112 * Create LLVM type for struct draw_jit_texture
113 */
114 static LLVMTypeRef
115 create_jit_texture_type(struct gallivm_state *gallivm, const char *struct_name)
116 {
117 LLVMTargetDataRef target = gallivm->target;
118 LLVMTypeRef texture_type;
119 LLVMTypeRef elem_types[DRAW_JIT_TEXTURE_NUM_FIELDS];
120 LLVMTypeRef int32_type = LLVMInt32TypeInContext(gallivm->context);
121
122 elem_types[DRAW_JIT_TEXTURE_WIDTH] =
123 elem_types[DRAW_JIT_TEXTURE_HEIGHT] =
124 elem_types[DRAW_JIT_TEXTURE_DEPTH] =
125 elem_types[DRAW_JIT_TEXTURE_FIRST_LEVEL] =
126 elem_types[DRAW_JIT_TEXTURE_LAST_LEVEL] = int32_type;
127 elem_types[DRAW_JIT_TEXTURE_BASE] =
128 LLVMPointerType(LLVMInt8TypeInContext(gallivm->context), 0);
129 elem_types[DRAW_JIT_TEXTURE_ROW_STRIDE] =
130 elem_types[DRAW_JIT_TEXTURE_IMG_STRIDE] =
131 elem_types[DRAW_JIT_TEXTURE_MIP_OFFSETS] =
132 LLVMArrayType(int32_type, PIPE_MAX_TEXTURE_LEVELS);
133
134 texture_type = LLVMStructTypeInContext(gallivm->context, elem_types,
135 ARRAY_SIZE(elem_types), 0);
136
137 (void) target; /* silence unused var warning for non-debug build */
138 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture, width,
139 target, texture_type,
140 DRAW_JIT_TEXTURE_WIDTH);
141 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture, height,
142 target, texture_type,
143 DRAW_JIT_TEXTURE_HEIGHT);
144 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture, depth,
145 target, texture_type,
146 DRAW_JIT_TEXTURE_DEPTH);
147 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture, first_level,
148 target, texture_type,
149 DRAW_JIT_TEXTURE_FIRST_LEVEL);
150 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture, last_level,
151 target, texture_type,
152 DRAW_JIT_TEXTURE_LAST_LEVEL);
153 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture, base,
154 target, texture_type,
155 DRAW_JIT_TEXTURE_BASE);
156 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture, row_stride,
157 target, texture_type,
158 DRAW_JIT_TEXTURE_ROW_STRIDE);
159 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture, img_stride,
160 target, texture_type,
161 DRAW_JIT_TEXTURE_IMG_STRIDE);
162 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture, mip_offsets,
163 target, texture_type,
164 DRAW_JIT_TEXTURE_MIP_OFFSETS);
165
166 LP_CHECK_STRUCT_SIZE(struct draw_jit_texture, target, texture_type);
167
168 return texture_type;
169 }
170
171
172 /**
173 * Create LLVM type for struct draw_jit_sampler
174 */
175 static LLVMTypeRef
176 create_jit_sampler_type(struct gallivm_state *gallivm, const char *struct_name)
177 {
178 LLVMTargetDataRef target = gallivm->target;
179 LLVMTypeRef sampler_type;
180 LLVMTypeRef elem_types[DRAW_JIT_SAMPLER_NUM_FIELDS];
181
182 elem_types[DRAW_JIT_SAMPLER_MIN_LOD] =
183 elem_types[DRAW_JIT_SAMPLER_MAX_LOD] =
184 elem_types[DRAW_JIT_SAMPLER_LOD_BIAS] = LLVMFloatTypeInContext(gallivm->context);
185 elem_types[DRAW_JIT_SAMPLER_BORDER_COLOR] =
186 LLVMArrayType(LLVMFloatTypeInContext(gallivm->context), 4);
187
188 sampler_type = LLVMStructTypeInContext(gallivm->context, elem_types,
189 ARRAY_SIZE(elem_types), 0);
190
191 (void) target; /* silence unused var warning for non-debug build */
192 LP_CHECK_MEMBER_OFFSET(struct draw_jit_sampler, min_lod,
193 target, sampler_type,
194 DRAW_JIT_SAMPLER_MIN_LOD);
195 LP_CHECK_MEMBER_OFFSET(struct draw_jit_sampler, max_lod,
196 target, sampler_type,
197 DRAW_JIT_SAMPLER_MAX_LOD);
198 LP_CHECK_MEMBER_OFFSET(struct draw_jit_sampler, lod_bias,
199 target, sampler_type,
200 DRAW_JIT_SAMPLER_LOD_BIAS);
201 LP_CHECK_MEMBER_OFFSET(struct draw_jit_sampler, border_color,
202 target, sampler_type,
203 DRAW_JIT_SAMPLER_BORDER_COLOR);
204
205 LP_CHECK_STRUCT_SIZE(struct draw_jit_sampler, target, sampler_type);
206
207 return sampler_type;
208 }
209
210
211 /**
212 * Create LLVM type for struct draw_jit_context
213 */
214 static LLVMTypeRef
215 create_jit_context_type(struct gallivm_state *gallivm,
216 LLVMTypeRef texture_type, LLVMTypeRef sampler_type,
217 const char *struct_name)
218 {
219 LLVMTargetDataRef target = gallivm->target;
220 LLVMTypeRef float_type = LLVMFloatTypeInContext(gallivm->context);
221 LLVMTypeRef int_type = LLVMInt32TypeInContext(gallivm->context);
222 LLVMTypeRef elem_types[DRAW_JIT_CTX_NUM_FIELDS];
223 LLVMTypeRef context_type;
224
225 elem_types[0] = LLVMArrayType(LLVMPointerType(float_type, 0), /* vs_constants */
226 LP_MAX_TGSI_CONST_BUFFERS);
227 elem_types[1] = LLVMArrayType(int_type, /* num_vs_constants */
228 LP_MAX_TGSI_CONST_BUFFERS);
229 elem_types[2] = LLVMPointerType(LLVMArrayType(LLVMArrayType(float_type, 4),
230 DRAW_TOTAL_CLIP_PLANES), 0);
231 elem_types[3] = LLVMPointerType(float_type, 0); /* viewports */
232 elem_types[4] = LLVMArrayType(texture_type,
233 PIPE_MAX_SHADER_SAMPLER_VIEWS); /* textures */
234 elem_types[5] = LLVMArrayType(sampler_type,
235 PIPE_MAX_SAMPLERS); /* samplers */
236 context_type = LLVMStructTypeInContext(gallivm->context, elem_types,
237 ARRAY_SIZE(elem_types), 0);
238
239 (void) target; /* silence unused var warning for non-debug build */
240 LP_CHECK_MEMBER_OFFSET(struct draw_jit_context, vs_constants,
241 target, context_type, DRAW_JIT_CTX_CONSTANTS);
242 LP_CHECK_MEMBER_OFFSET(struct draw_jit_context, num_vs_constants,
243 target, context_type, DRAW_JIT_CTX_NUM_CONSTANTS);
244 LP_CHECK_MEMBER_OFFSET(struct draw_jit_context, planes,
245 target, context_type, DRAW_JIT_CTX_PLANES);
246 LP_CHECK_MEMBER_OFFSET(struct draw_jit_context, viewports,
247 target, context_type, DRAW_JIT_CTX_VIEWPORT);
248 LP_CHECK_MEMBER_OFFSET(struct draw_jit_context, textures,
249 target, context_type,
250 DRAW_JIT_CTX_TEXTURES);
251 LP_CHECK_MEMBER_OFFSET(struct draw_jit_context, samplers,
252 target, context_type,
253 DRAW_JIT_CTX_SAMPLERS);
254 LP_CHECK_STRUCT_SIZE(struct draw_jit_context,
255 target, context_type);
256
257 return context_type;
258 }
259
260
261 /**
262 * Create LLVM type for struct draw_gs_jit_context
263 */
264 static LLVMTypeRef
265 create_gs_jit_context_type(struct gallivm_state *gallivm,
266 unsigned vector_length,
267 LLVMTypeRef texture_type, LLVMTypeRef sampler_type,
268 const char *struct_name)
269 {
270 LLVMTargetDataRef target = gallivm->target;
271 LLVMTypeRef float_type = LLVMFloatTypeInContext(gallivm->context);
272 LLVMTypeRef int_type = LLVMInt32TypeInContext(gallivm->context);
273 LLVMTypeRef elem_types[DRAW_GS_JIT_CTX_NUM_FIELDS];
274 LLVMTypeRef context_type;
275
276 elem_types[0] = LLVMArrayType(LLVMPointerType(float_type, 0), /* constants */
277 LP_MAX_TGSI_CONST_BUFFERS);
278 elem_types[1] = LLVMArrayType(int_type, /* num_constants */
279 LP_MAX_TGSI_CONST_BUFFERS);
280 elem_types[2] = LLVMPointerType(LLVMArrayType(LLVMArrayType(float_type, 4),
281 DRAW_TOTAL_CLIP_PLANES), 0);
282 elem_types[3] = LLVMPointerType(float_type, 0); /* viewports */
283
284 elem_types[4] = LLVMArrayType(texture_type,
285 PIPE_MAX_SHADER_SAMPLER_VIEWS); /* textures */
286 elem_types[5] = LLVMArrayType(sampler_type,
287 PIPE_MAX_SAMPLERS); /* samplers */
288
289 elem_types[6] = LLVMPointerType(LLVMPointerType(int_type, 0), 0);
290 elem_types[7] = LLVMPointerType(LLVMVectorType(int_type,
291 vector_length), 0);
292 elem_types[8] = LLVMPointerType(LLVMVectorType(int_type,
293 vector_length), 0);
294
295 context_type = LLVMStructTypeInContext(gallivm->context, elem_types,
296 ARRAY_SIZE(elem_types), 0);
297
298 (void) target; /* silence unused var warning for non-debug build */
299 LP_CHECK_MEMBER_OFFSET(struct draw_gs_jit_context, constants,
300 target, context_type, DRAW_GS_JIT_CTX_CONSTANTS);
301 LP_CHECK_MEMBER_OFFSET(struct draw_gs_jit_context, num_constants,
302 target, context_type, DRAW_GS_JIT_CTX_NUM_CONSTANTS);
303 LP_CHECK_MEMBER_OFFSET(struct draw_gs_jit_context, planes,
304 target, context_type, DRAW_GS_JIT_CTX_PLANES);
305 LP_CHECK_MEMBER_OFFSET(struct draw_gs_jit_context, viewports,
306 target, context_type, DRAW_GS_JIT_CTX_VIEWPORT);
307 LP_CHECK_MEMBER_OFFSET(struct draw_gs_jit_context, textures,
308 target, context_type,
309 DRAW_GS_JIT_CTX_TEXTURES);
310 LP_CHECK_MEMBER_OFFSET(struct draw_gs_jit_context, samplers,
311 target, context_type,
312 DRAW_GS_JIT_CTX_SAMPLERS);
313 LP_CHECK_MEMBER_OFFSET(struct draw_gs_jit_context, prim_lengths,
314 target, context_type,
315 DRAW_GS_JIT_CTX_PRIM_LENGTHS);
316 LP_CHECK_MEMBER_OFFSET(struct draw_gs_jit_context, emitted_vertices,
317 target, context_type,
318 DRAW_GS_JIT_CTX_EMITTED_VERTICES);
319 LP_CHECK_MEMBER_OFFSET(struct draw_gs_jit_context, emitted_prims,
320 target, context_type,
321 DRAW_GS_JIT_CTX_EMITTED_PRIMS);
322 LP_CHECK_STRUCT_SIZE(struct draw_gs_jit_context,
323 target, context_type);
324
325 return context_type;
326 }
327
328
329 static LLVMTypeRef
330 create_gs_jit_input_type(struct gallivm_state *gallivm)
331 {
332 LLVMTypeRef float_type = LLVMFloatTypeInContext(gallivm->context);
333 LLVMTypeRef input_array;
334
335 input_array = LLVMVectorType(float_type, TGSI_NUM_CHANNELS); /* num primitives */
336 input_array = LLVMArrayType(input_array, TGSI_NUM_CHANNELS); /* num channels */
337 input_array = LLVMArrayType(input_array, PIPE_MAX_SHADER_INPUTS); /* num attrs per vertex */
338 input_array = LLVMPointerType(input_array, 0); /* num vertices per prim */
339
340 return input_array;
341 }
342
343 /**
344 * Create LLVM type for struct pipe_vertex_buffer
345 */
346 static LLVMTypeRef
347 create_jit_vertex_buffer_type(struct gallivm_state *gallivm,
348 const char *struct_name)
349 {
350 LLVMTargetDataRef target = gallivm->target;
351 LLVMTypeRef elem_types[4];
352 LLVMTypeRef vb_type;
353
354 elem_types[0] =
355 elem_types[1] = LLVMInt32TypeInContext(gallivm->context);
356 elem_types[2] =
357 elem_types[3] = LLVMPointerType(LLVMInt8TypeInContext(gallivm->context), 0);
358
359 vb_type = LLVMStructTypeInContext(gallivm->context, elem_types,
360 ARRAY_SIZE(elem_types), 0);
361
362 (void) target; /* silence unused var warning for non-debug build */
363 LP_CHECK_MEMBER_OFFSET(struct pipe_vertex_buffer, stride,
364 target, vb_type, 0);
365 LP_CHECK_MEMBER_OFFSET(struct pipe_vertex_buffer, buffer_offset,
366 target, vb_type, 1);
367
368 LP_CHECK_STRUCT_SIZE(struct pipe_vertex_buffer, target, vb_type);
369
370 return vb_type;
371 }
372
373
374 /**
375 * Create LLVM type for struct vertex_header;
376 */
377 static LLVMTypeRef
378 create_jit_vertex_header(struct gallivm_state *gallivm, int data_elems)
379 {
380 LLVMTargetDataRef target = gallivm->target;
381 LLVMTypeRef elem_types[3];
382 LLVMTypeRef vertex_header;
383 char struct_name[24];
384
385 util_snprintf(struct_name, 23, "vertex_header%d", data_elems);
386
387 elem_types[DRAW_JIT_VERTEX_VERTEX_ID] = LLVMIntTypeInContext(gallivm->context, 32);
388 elem_types[DRAW_JIT_VERTEX_CLIP_POS] = LLVMArrayType(LLVMFloatTypeInContext(gallivm->context), 4);
389 elem_types[DRAW_JIT_VERTEX_DATA] = LLVMArrayType(elem_types[1], data_elems);
390
391 vertex_header = LLVMStructTypeInContext(gallivm->context, elem_types,
392 ARRAY_SIZE(elem_types), 0);
393
394 /* these are bit-fields and we can't take address of them
395 LP_CHECK_MEMBER_OFFSET(struct vertex_header, clipmask,
396 target, vertex_header,
397 DRAW_JIT_VERTEX_CLIPMASK);
398 LP_CHECK_MEMBER_OFFSET(struct vertex_header, edgeflag,
399 target, vertex_header,
400 DRAW_JIT_VERTEX_EDGEFLAG);
401 LP_CHECK_MEMBER_OFFSET(struct vertex_header, pad,
402 target, vertex_header,
403 DRAW_JIT_VERTEX_PAD);
404 LP_CHECK_MEMBER_OFFSET(struct vertex_header, vertex_id,
405 target, vertex_header,
406 DRAW_JIT_VERTEX_VERTEX_ID);
407 */
408 (void) target; /* silence unused var warning for non-debug build */
409 LP_CHECK_MEMBER_OFFSET(struct vertex_header, clip_pos,
410 target, vertex_header,
411 DRAW_JIT_VERTEX_CLIP_POS);
412 LP_CHECK_MEMBER_OFFSET(struct vertex_header, data,
413 target, vertex_header,
414 DRAW_JIT_VERTEX_DATA);
415
416 assert(LLVMABISizeOfType(target, vertex_header) ==
417 offsetof(struct vertex_header, data[data_elems]));
418
419 return vertex_header;
420 }
421
422
423 /**
424 * Create LLVM types for various structures.
425 */
426 static void
427 create_jit_types(struct draw_llvm_variant *variant)
428 {
429 struct gallivm_state *gallivm = variant->gallivm;
430 LLVMTypeRef texture_type, sampler_type, context_type, buffer_type,
431 vb_type;
432
433 texture_type = create_jit_texture_type(gallivm, "texture");
434 sampler_type = create_jit_sampler_type(gallivm, "sampler");
435
436 context_type = create_jit_context_type(gallivm, texture_type, sampler_type,
437 "draw_jit_context");
438 variant->context_ptr_type = LLVMPointerType(context_type, 0);
439
440 buffer_type = create_jit_dvbuffer_type(gallivm, "draw_vertex_buffer");
441 variant->buffer_ptr_type = LLVMPointerType(buffer_type, 0);
442
443 vb_type = create_jit_vertex_buffer_type(gallivm, "pipe_vertex_buffer");
444 variant->vb_ptr_type = LLVMPointerType(vb_type, 0);
445 }
446
447
448 static LLVMTypeRef
449 get_context_ptr_type(struct draw_llvm_variant *variant)
450 {
451 if (!variant->context_ptr_type)
452 create_jit_types(variant);
453 return variant->context_ptr_type;
454 }
455
456
457 static LLVMTypeRef
458 get_buffer_ptr_type(struct draw_llvm_variant *variant)
459 {
460 if (!variant->buffer_ptr_type)
461 create_jit_types(variant);
462 return variant->buffer_ptr_type;
463 }
464
465
466 static LLVMTypeRef
467 get_vb_ptr_type(struct draw_llvm_variant *variant)
468 {
469 if (!variant->vb_ptr_type)
470 create_jit_types(variant);
471 return variant->vb_ptr_type;
472 }
473
474 static LLVMTypeRef
475 get_vertex_header_ptr_type(struct draw_llvm_variant *variant)
476 {
477 if (!variant->vertex_header_ptr_type)
478 create_jit_types(variant);
479 return variant->vertex_header_ptr_type;
480 }
481
482
483 /**
484 * Create per-context LLVM info.
485 */
486 struct draw_llvm *
487 draw_llvm_create(struct draw_context *draw, LLVMContextRef context)
488 {
489 struct draw_llvm *llvm;
490
491 if (!lp_build_init())
492 return NULL;
493
494 llvm = CALLOC_STRUCT( draw_llvm );
495 if (!llvm)
496 return NULL;
497
498 llvm->draw = draw;
499
500 llvm->context = context;
501 if (!llvm->context) {
502 llvm->context = LLVMContextCreate();
503 llvm->context_owned = true;
504 }
505 if (!llvm->context)
506 goto fail;
507
508 llvm->nr_variants = 0;
509 make_empty_list(&llvm->vs_variants_list);
510
511 llvm->nr_gs_variants = 0;
512 make_empty_list(&llvm->gs_variants_list);
513
514 return llvm;
515
516 fail:
517 draw_llvm_destroy(llvm);
518 return NULL;
519 }
520
521
522 /**
523 * Free per-context LLVM info.
524 */
525 void
526 draw_llvm_destroy(struct draw_llvm *llvm)
527 {
528 if (llvm->context_owned)
529 LLVMContextDispose(llvm->context);
530 llvm->context = NULL;
531
532 /* XXX free other draw_llvm data? */
533 FREE(llvm);
534 }
535
536
537 /**
538 * Create LLVM-generated code for a vertex shader.
539 */
540 struct draw_llvm_variant *
541 draw_llvm_create_variant(struct draw_llvm *llvm,
542 unsigned num_inputs,
543 const struct draw_llvm_variant_key *key)
544 {
545 struct draw_llvm_variant *variant;
546 struct llvm_vertex_shader *shader =
547 llvm_vertex_shader(llvm->draw->vs.vertex_shader);
548 LLVMTypeRef vertex_header;
549 char module_name[64];
550
551 variant = MALLOC(sizeof *variant +
552 shader->variant_key_size -
553 sizeof variant->key);
554 if (!variant)
555 return NULL;
556
557 variant->llvm = llvm;
558 variant->shader = shader;
559
560 util_snprintf(module_name, sizeof(module_name), "draw_llvm_vs_variant%u",
561 variant->shader->variants_cached);
562
563 variant->gallivm = gallivm_create(module_name, llvm->context);
564
565 create_jit_types(variant);
566
567 memcpy(&variant->key, key, shader->variant_key_size);
568
569 if (gallivm_debug & (GALLIVM_DEBUG_TGSI | GALLIVM_DEBUG_IR)) {
570 tgsi_dump(llvm->draw->vs.vertex_shader->state.tokens, 0);
571 draw_llvm_dump_variant_key(&variant->key);
572 }
573
574 vertex_header = create_jit_vertex_header(variant->gallivm, num_inputs);
575
576 variant->vertex_header_ptr_type = LLVMPointerType(vertex_header, 0);
577
578 draw_llvm_generate(llvm, variant, FALSE); /* linear */
579 draw_llvm_generate(llvm, variant, TRUE); /* elts */
580
581 gallivm_compile_module(variant->gallivm);
582
583 variant->jit_func = (draw_jit_vert_func)
584 gallivm_jit_function(variant->gallivm, variant->function);
585
586 variant->jit_func_elts = (draw_jit_vert_func_elts)
587 gallivm_jit_function(variant->gallivm, variant->function_elts);
588
589 gallivm_free_ir(variant->gallivm);
590
591 variant->list_item_global.base = variant;
592 variant->list_item_local.base = variant;
593 /*variant->no = */shader->variants_created++;
594 variant->list_item_global.base = variant;
595
596 return variant;
597 }
598
599
600 static void
601 generate_vs(struct draw_llvm_variant *variant,
602 LLVMBuilderRef builder,
603 struct lp_type vs_type,
604 LLVMValueRef (*outputs)[TGSI_NUM_CHANNELS],
605 const LLVMValueRef (*inputs)[TGSI_NUM_CHANNELS],
606 const struct lp_bld_tgsi_system_values *system_values,
607 LLVMValueRef context_ptr,
608 struct lp_build_sampler_soa *draw_sampler,
609 boolean clamp_vertex_color)
610 {
611 struct draw_llvm *llvm = variant->llvm;
612 const struct tgsi_token *tokens = llvm->draw->vs.vertex_shader->state.tokens;
613 LLVMValueRef consts_ptr =
614 draw_jit_context_vs_constants(variant->gallivm, context_ptr);
615 LLVMValueRef num_consts_ptr =
616 draw_jit_context_num_vs_constants(variant->gallivm, context_ptr);
617
618 lp_build_tgsi_soa(variant->gallivm,
619 tokens,
620 vs_type,
621 NULL /*struct lp_build_mask_context *mask*/,
622 consts_ptr,
623 num_consts_ptr,
624 system_values,
625 inputs,
626 outputs,
627 context_ptr,
628 NULL,
629 draw_sampler,
630 &llvm->draw->vs.vertex_shader->info,
631 NULL);
632
633 {
634 LLVMValueRef out;
635 unsigned chan, attrib;
636 struct lp_build_context bld;
637 struct tgsi_shader_info* info = &llvm->draw->vs.vertex_shader->info;
638 lp_build_context_init(&bld, variant->gallivm, vs_type);
639
640 for (attrib = 0; attrib < info->num_outputs; ++attrib) {
641 for (chan = 0; chan < TGSI_NUM_CHANNELS; ++chan) {
642 if (outputs[attrib][chan]) {
643 switch (info->output_semantic_name[attrib]) {
644 case TGSI_SEMANTIC_COLOR:
645 case TGSI_SEMANTIC_BCOLOR:
646 if (clamp_vertex_color) {
647 out = LLVMBuildLoad(builder, outputs[attrib][chan], "");
648 out = lp_build_clamp(&bld, out, bld.zero, bld.one);
649 LLVMBuildStore(builder, out, outputs[attrib][chan]);
650 }
651 break;
652 }
653 }
654 }
655 }
656 }
657 }
658
659 static void
660 generate_fetch(struct gallivm_state *gallivm,
661 struct draw_context *draw,
662 LLVMValueRef vbuffers_ptr,
663 LLVMValueRef *res,
664 struct pipe_vertex_element *velem,
665 LLVMValueRef vbuf,
666 LLVMValueRef index,
667 LLVMValueRef instance_id,
668 LLVMValueRef start_instance)
669 {
670 const struct util_format_description *format_desc =
671 util_format_description(velem->src_format);
672 LLVMValueRef zero = LLVMConstNull(LLVMInt32TypeInContext(gallivm->context));
673 LLVMBuilderRef builder = gallivm->builder;
674 LLVMValueRef indices =
675 LLVMConstInt(LLVMInt64TypeInContext(gallivm->context),
676 velem->vertex_buffer_index, 0);
677 LLVMValueRef vbuffer_ptr = LLVMBuildGEP(builder, vbuffers_ptr,
678 &indices, 1, "");
679 LLVMValueRef vb_stride = draw_jit_vbuffer_stride(gallivm, vbuf);
680 LLVMValueRef vb_buffer_offset = draw_jit_vbuffer_offset(gallivm, vbuf);
681 LLVMValueRef map_ptr = draw_jit_dvbuffer_map(gallivm, vbuffer_ptr);
682 LLVMValueRef buffer_size = draw_jit_dvbuffer_size(gallivm, vbuffer_ptr);
683 LLVMValueRef stride;
684 LLVMValueRef buffer_overflowed;
685 LLVMValueRef needed_buffer_size;
686 LLVMValueRef temp_ptr =
687 lp_build_alloca(gallivm,
688 lp_build_vec_type(gallivm, lp_float32_vec4_type()), "");
689 LLVMValueRef ofbit = NULL;
690 struct lp_build_if_state if_ctx;
691
692 if (velem->instance_divisor) {
693 /* Index is equal to the start instance plus the number of current
694 * instance divided by the divisor. In this case we compute it as:
695 * index = start_instance + (instance_id / divisor)
696 */
697 LLVMValueRef current_instance;
698 current_instance = LLVMBuildUDiv(builder, instance_id,
699 lp_build_const_int32(gallivm, velem->instance_divisor),
700 "instance_divisor");
701 index = lp_build_uadd_overflow(gallivm, start_instance,
702 current_instance, &ofbit);
703 }
704
705 stride = lp_build_umul_overflow(gallivm, vb_stride, index, &ofbit);
706 stride = lp_build_uadd_overflow(gallivm, stride, vb_buffer_offset, &ofbit);
707 stride = lp_build_uadd_overflow(
708 gallivm, stride,
709 lp_build_const_int32(gallivm, velem->src_offset), &ofbit);
710 needed_buffer_size = lp_build_uadd_overflow(
711 gallivm, stride,
712 lp_build_const_int32(gallivm,
713 util_format_get_blocksize(velem->src_format)),
714 &ofbit);
715
716 buffer_overflowed = LLVMBuildICmp(builder, LLVMIntUGT,
717 needed_buffer_size, buffer_size,
718 "buffer_overflowed");
719 buffer_overflowed = LLVMBuildOr(builder, buffer_overflowed, ofbit, "");
720 #if 0
721 lp_build_printf(gallivm, "vbuf index = %u, vb_stride is %u\n",
722 index, vb_stride);
723 lp_build_printf(gallivm, " vb_buffer_offset = %u, src_offset is %u\n",
724 vb_buffer_offset,
725 lp_build_const_int32(gallivm, velem->src_offset));
726 lp_build_print_value(gallivm, " blocksize = ",
727 lp_build_const_int32(
728 gallivm,
729 util_format_get_blocksize(velem->src_format)));
730 lp_build_printf(gallivm, " instance_id = %u\n", instance_id);
731 lp_build_printf(gallivm, " stride = %u\n", stride);
732 lp_build_printf(gallivm, " buffer size = %u\n", buffer_size);
733 lp_build_printf(gallivm, " needed_buffer_size = %u\n", needed_buffer_size);
734 lp_build_print_value(gallivm, " buffer overflowed = ", buffer_overflowed);
735 #endif
736
737 lp_build_if(&if_ctx, gallivm, buffer_overflowed);
738 {
739 LLVMValueRef val =
740 lp_build_const_vec(gallivm, lp_float32_vec4_type(), 0);
741 LLVMBuildStore(builder, val, temp_ptr);
742 }
743 lp_build_else(&if_ctx);
744 {
745 LLVMValueRef val;
746 map_ptr = LLVMBuildGEP(builder, map_ptr, &stride, 1, "");
747
748 val = lp_build_fetch_rgba_aos(gallivm,
749 format_desc,
750 lp_float32_vec4_type(),
751 FALSE,
752 map_ptr,
753 zero, zero, zero,
754 NULL);
755 LLVMBuildStore(builder, val, temp_ptr);
756 }
757 lp_build_endif(&if_ctx);
758
759 *res = LLVMBuildLoad(builder, temp_ptr, "aos");
760 }
761
762 static void
763 convert_to_soa(struct gallivm_state *gallivm,
764 LLVMValueRef (*src_aos)[LP_MAX_VECTOR_WIDTH / 32],
765 LLVMValueRef (*dst_soa)[TGSI_NUM_CHANNELS],
766 unsigned num_attribs, const struct lp_type soa_type)
767 {
768 unsigned i, j, k;
769 struct lp_type aos_channel_type = soa_type;
770
771 debug_assert(TGSI_NUM_CHANNELS == 4);
772 debug_assert((soa_type.length % TGSI_NUM_CHANNELS) == 0);
773
774 aos_channel_type.length >>= 1;
775
776 for (i = 0; i < num_attribs; ++i) {
777 LLVMValueRef aos_channels[TGSI_NUM_CHANNELS];
778 unsigned pixels_per_channel = soa_type.length / TGSI_NUM_CHANNELS;
779
780 for (j = 0; j < TGSI_NUM_CHANNELS; ++j) {
781 LLVMValueRef channel[LP_MAX_VECTOR_LENGTH] = { 0 };
782
783 assert(pixels_per_channel <= LP_MAX_VECTOR_LENGTH);
784
785 for (k = 0; k < pixels_per_channel; ++k) {
786 channel[k] = src_aos[i][j + TGSI_NUM_CHANNELS * k];
787 }
788
789 aos_channels[j] = lp_build_concat(gallivm, channel, aos_channel_type, pixels_per_channel);
790 }
791
792 lp_build_transpose_aos(gallivm, soa_type, aos_channels, dst_soa[i]);
793 }
794 }
795
796
797 static void
798 store_aos(struct gallivm_state *gallivm,
799 LLVMValueRef io_ptr,
800 LLVMValueRef index,
801 LLVMValueRef value)
802 {
803 LLVMTypeRef data_ptr_type = LLVMPointerType(lp_build_vec_type(gallivm, lp_float32_vec4_type()), 0);
804 LLVMBuilderRef builder = gallivm->builder;
805 LLVMValueRef data_ptr = draw_jit_header_data(gallivm, io_ptr);
806 LLVMValueRef indices[3];
807
808 indices[0] = lp_build_const_int32(gallivm, 0);
809 indices[1] = index;
810 indices[2] = lp_build_const_int32(gallivm, 0);
811
812 data_ptr = LLVMBuildGEP(builder, data_ptr, indices, 3, "");
813 data_ptr = LLVMBuildPointerCast(builder, data_ptr, data_ptr_type, "");
814
815 #if DEBUG_STORE
816 lp_build_printf(gallivm, " ---- %p storing attribute %d (io = %p)\n", data_ptr, index, io_ptr);
817 #endif
818
819 /* Unaligned store due to the vertex header */
820 LLVMSetAlignment(LLVMBuildStore(builder, value, data_ptr), sizeof(float));
821 }
822
823 /**
824 * Adjust the mask to architecture endianess. The mask will the store in struct:
825 *
826 * struct vertex_header {
827 * unsigned clipmask:DRAW_TOTAL_CLIP_PLANES;
828 * unsigned edgeflag:1;
829 * unsigned pad:1;
830 * unsigned vertex_id:16;
831 * [...]
832 * }
833 *
834 * On little-endian machine nothing needs to done, however on bit-endian machine
835 * the mask's fields need to be adjusted with the algorithm:
836 *
837 * uint32_t reverse (uint32_t x)
838 * {
839 * return (x >> 16) | // vertex_id
840 * ((x & 0x3fff) << 18) | // clipmask
841 * ((x & 0x4000) << 3) | // pad
842 * ((x & 0x8000) << 1); // edgeflag
843 * }
844 */
845 static LLVMValueRef
846 adjust_mask(struct gallivm_state *gallivm,
847 LLVMValueRef mask)
848 {
849 #ifdef PIPE_ARCH_BIG_ENDIAN
850 LLVMBuilderRef builder = gallivm->builder;
851 LLVMValueRef vertex_id;
852 LLVMValueRef clipmask;
853 LLVMValueRef pad;
854 LLVMValueRef edgeflag;
855
856 vertex_id = LLVMBuildLShr(builder, mask, lp_build_const_int32(gallivm, 16), "");
857 clipmask = LLVMBuildAnd(builder, mask, lp_build_const_int32(gallivm, 0x3fff), "");
858 clipmask = LLVMBuildShl(builder, clipmask, lp_build_const_int32(gallivm, 18), "");
859 if (0) {
860 pad = LLVMBuildAnd(builder, mask, lp_build_const_int32(gallivm, 0x4000), "");
861 pad = LLVMBuildShl(builder, pad, lp_build_const_int32(gallivm, 3), "");
862 }
863 edgeflag = LLVMBuildAnd(builder, mask, lp_build_const_int32(gallivm, 0x8000), "");
864 edgeflag = LLVMBuildShl(builder, edgeflag, lp_build_const_int32(gallivm, 1), "");
865
866 mask = LLVMBuildOr(builder, vertex_id, clipmask, "");
867 if (0) {
868 mask = LLVMBuildOr(builder, mask, pad, "");
869 }
870 mask = LLVMBuildOr(builder, mask, edgeflag, "");
871 #endif
872 return mask;
873 }
874
875 static void
876 store_aos_array(struct gallivm_state *gallivm,
877 struct lp_type soa_type,
878 LLVMValueRef io_ptr,
879 LLVMValueRef *indices,
880 LLVMValueRef* aos,
881 int attrib,
882 int num_outputs,
883 LLVMValueRef clipmask,
884 boolean need_edgeflag)
885 {
886 LLVMBuilderRef builder = gallivm->builder;
887 LLVMValueRef attr_index = lp_build_const_int32(gallivm, attrib);
888 LLVMValueRef inds[LP_MAX_VECTOR_WIDTH / 32];
889 LLVMValueRef linear_inds[LP_MAX_VECTOR_WIDTH / 32];
890 LLVMValueRef io_ptrs[LP_MAX_VECTOR_WIDTH / 32];
891 int vector_length = soa_type.length;
892 int i;
893
894 debug_assert(TGSI_NUM_CHANNELS == 4);
895
896 for (i = 0; i < vector_length; i++) {
897 linear_inds[i] = lp_build_const_int32(gallivm, i);
898 if (indices) {
899 inds[i] = indices[i];
900 } else {
901 inds[i] = linear_inds[i];
902 }
903 io_ptrs[i] = LLVMBuildGEP(builder, io_ptr, &inds[i], 1, "");
904 }
905
906 if (attrib == 0) {
907 /* store vertex header for each of the n vertices */
908 LLVMValueRef val, cliptmp;
909 int vertex_id_pad_edgeflag;
910
911 /* If this assertion fails, it means we need to update the bit twidding
912 * code here. See struct vertex_header in draw_private.h.
913 */
914 assert(DRAW_TOTAL_CLIP_PLANES==14);
915 /* initialize vertex id:16 = 0xffff, pad:1 = 0, edgeflag:1 = 1 */
916 if (!need_edgeflag) {
917 vertex_id_pad_edgeflag = (0xffff << 16) | (1 << DRAW_TOTAL_CLIP_PLANES);
918 }
919 else {
920 vertex_id_pad_edgeflag = (0xffff << 16);
921 }
922 val = lp_build_const_int_vec(gallivm, lp_int_type(soa_type),
923 vertex_id_pad_edgeflag);
924 /* OR with the clipmask */
925 cliptmp = LLVMBuildOr(builder, val, clipmask, "");
926 for (i = 0; i < vector_length; i++) {
927 LLVMValueRef id_ptr = draw_jit_header_id(gallivm, io_ptrs[i]);
928 val = LLVMBuildExtractElement(builder, cliptmp, linear_inds[i], "");
929 val = adjust_mask(gallivm, val);
930 #if DEBUG_STORE
931 lp_build_printf(gallivm, "io = %p, index %d, clipmask = %x\n",
932 io_ptrs[i], inds[i], val);
933 #endif
934 LLVMBuildStore(builder, val, id_ptr);
935 }
936 }
937
938 /* store for each of the n vertices */
939 for (i = 0; i < vector_length; i++) {
940 store_aos(gallivm, io_ptrs[i], attr_index, aos[i]);
941 }
942 }
943
944
945 static void
946 convert_to_aos(struct gallivm_state *gallivm,
947 LLVMValueRef io,
948 LLVMValueRef *indices,
949 LLVMValueRef (*outputs)[TGSI_NUM_CHANNELS],
950 LLVMValueRef clipmask,
951 int num_outputs,
952 struct lp_type soa_type,
953 boolean need_edgeflag)
954 {
955 LLVMBuilderRef builder = gallivm->builder;
956 unsigned chan, attrib, i;
957
958 #if DEBUG_STORE
959 lp_build_printf(gallivm, " # storing begin\n");
960 #endif
961 for (attrib = 0; attrib < num_outputs; ++attrib) {
962 LLVMValueRef soa[TGSI_NUM_CHANNELS];
963 LLVMValueRef aos[LP_MAX_VECTOR_WIDTH / 32];
964 for (chan = 0; chan < TGSI_NUM_CHANNELS; ++chan) {
965 if (outputs[attrib][chan]) {
966 LLVMValueRef out = LLVMBuildLoad(builder, outputs[attrib][chan], "");
967 lp_build_name(out, "output%u.%c", attrib, "xyzw"[chan]);
968 #if DEBUG_STORE
969 lp_build_printf(gallivm, "output %d : %d ",
970 LLVMConstInt(LLVMInt32TypeInContext(gallivm->context),
971 attrib, 0),
972 LLVMConstInt(LLVMInt32TypeInContext(gallivm->context),
973 chan, 0));
974 lp_build_print_value(gallivm, "val = ", out);
975 {
976 LLVMValueRef iv =
977 LLVMBuildBitCast(builder, out, lp_build_int_vec_type(gallivm, soa_type), "");
978
979 lp_build_print_value(gallivm, " ival = ", iv);
980 }
981 #endif
982 soa[chan] = out;
983 }
984 else {
985 soa[chan] = 0;
986 }
987 }
988
989
990 if (soa_type.length == TGSI_NUM_CHANNELS) {
991 lp_build_transpose_aos(gallivm, soa_type, soa, aos);
992 } else {
993 lp_build_transpose_aos(gallivm, soa_type, soa, soa);
994
995 for (i = 0; i < soa_type.length; ++i) {
996 aos[i] = lp_build_extract_range(gallivm,
997 soa[i % TGSI_NUM_CHANNELS],
998 (i / TGSI_NUM_CHANNELS) * TGSI_NUM_CHANNELS,
999 TGSI_NUM_CHANNELS);
1000 }
1001 }
1002
1003 store_aos_array(gallivm,
1004 soa_type,
1005 io, indices,
1006 aos,
1007 attrib,
1008 num_outputs,
1009 clipmask,
1010 need_edgeflag);
1011 }
1012 #if DEBUG_STORE
1013 lp_build_printf(gallivm, " # storing end\n");
1014 #endif
1015 }
1016
1017
1018 /**
1019 * Stores original vertex positions in clip coordinates
1020 */
1021 static void
1022 store_clip(struct gallivm_state *gallivm,
1023 const struct lp_type vs_type,
1024 LLVMValueRef io_ptr,
1025 LLVMValueRef (*outputs)[TGSI_NUM_CHANNELS],
1026 int idx)
1027 {
1028 LLVMBuilderRef builder = gallivm->builder;
1029 LLVMValueRef soa[4];
1030 LLVMValueRef aos[LP_MAX_VECTOR_LENGTH];
1031 LLVMValueRef indices[2];
1032 LLVMValueRef io_ptrs[LP_MAX_VECTOR_WIDTH / 32];
1033 LLVMValueRef inds[LP_MAX_VECTOR_WIDTH / 32];
1034 LLVMValueRef clip_ptrs[LP_MAX_VECTOR_WIDTH / 32];
1035 LLVMTypeRef clip_ptr_type =
1036 LLVMPointerType(LLVMVectorType(LLVMFloatTypeInContext(gallivm->context),
1037 4), 0);
1038 int i, j;
1039
1040 indices[0] =
1041 indices[1] = lp_build_const_int32(gallivm, 0);
1042
1043 for (i = 0; i < vs_type.length; i++) {
1044 inds[i] = lp_build_const_int32(gallivm, i);
1045 io_ptrs[i] = LLVMBuildGEP(builder, io_ptr, &inds[i], 1, "");
1046 }
1047
1048 soa[0] = LLVMBuildLoad(builder, outputs[idx][0], ""); /*x0 x1 .. xn*/
1049 soa[1] = LLVMBuildLoad(builder, outputs[idx][1], ""); /*y0 y1 .. yn*/
1050 soa[2] = LLVMBuildLoad(builder, outputs[idx][2], ""); /*z0 z1 .. zn*/
1051 soa[3] = LLVMBuildLoad(builder, outputs[idx][3], ""); /*w0 w1 .. wn*/
1052
1053 for (i = 0; i < vs_type.length; i++) {
1054 clip_ptrs[i] = draw_jit_header_clip_pos(gallivm, io_ptrs[i]);
1055 }
1056
1057 lp_build_transpose_aos(gallivm, vs_type, soa, soa);
1058 for (i = 0; i < vs_type.length; ++i) {
1059 aos[i] = lp_build_extract_range(gallivm,
1060 soa[i % TGSI_NUM_CHANNELS],
1061 (i / TGSI_NUM_CHANNELS) * TGSI_NUM_CHANNELS,
1062 TGSI_NUM_CHANNELS);
1063 }
1064
1065 for (j = 0; j < vs_type.length; j++) {
1066 LLVMValueRef clip_ptr;
1067
1068 clip_ptr = LLVMBuildGEP(builder, clip_ptrs[j], indices, 2, "clipo");
1069 clip_ptr = LLVMBuildPointerCast(builder, clip_ptr, clip_ptr_type, "");
1070
1071 /* Unaligned store */
1072 LLVMSetAlignment(LLVMBuildStore(builder, aos[j], clip_ptr), sizeof(float));
1073 }
1074 }
1075
1076
1077 /**
1078 * Transforms the outputs for viewport mapping
1079 */
1080 static void
1081 generate_viewport(struct draw_llvm_variant *variant,
1082 LLVMBuilderRef builder,
1083 struct lp_type vs_type,
1084 LLVMValueRef (*outputs)[TGSI_NUM_CHANNELS],
1085 LLVMValueRef context_ptr)
1086 {
1087 int i;
1088 struct gallivm_state *gallivm = variant->gallivm;
1089 struct lp_type f32_type = vs_type;
1090 const unsigned pos = variant->llvm->draw->vs.position_output;
1091 LLVMTypeRef vs_type_llvm = lp_build_vec_type(gallivm, vs_type);
1092 LLVMValueRef out3 = LLVMBuildLoad(builder, outputs[pos][3], ""); /*w0 w1 .. wn*/
1093 LLVMValueRef const1 = lp_build_const_vec(gallivm, f32_type, 1.0); /*1.0 1.0 1.0 1.0*/
1094 LLVMValueRef vp_ptr = draw_jit_context_viewports(gallivm, context_ptr);
1095
1096 /* We treat pipe_viewport_state as a float array */
1097 const int scale_index_offset = offsetof(struct pipe_viewport_state, scale) / sizeof(float);
1098 const int trans_index_offset = offsetof(struct pipe_viewport_state, translate) / sizeof(float);
1099
1100 /* for 1/w convention*/
1101 out3 = LLVMBuildFDiv(builder, const1, out3, "");
1102 LLVMBuildStore(builder, out3, outputs[pos][3]);
1103
1104 /* Viewport Mapping */
1105 for (i=0; i<3; i++) {
1106 LLVMValueRef out = LLVMBuildLoad(builder, outputs[pos][i], ""); /*x0 x1 .. xn*/
1107 LLVMValueRef scale;
1108 LLVMValueRef trans;
1109 LLVMValueRef scale_i;
1110 LLVMValueRef trans_i;
1111 LLVMValueRef index;
1112
1113 index = lp_build_const_int32(gallivm, i + scale_index_offset);
1114 scale_i = LLVMBuildGEP(builder, vp_ptr, &index, 1, "");
1115
1116 index = lp_build_const_int32(gallivm, i + trans_index_offset);
1117 trans_i = LLVMBuildGEP(builder, vp_ptr, &index, 1, "");
1118
1119 scale = lp_build_broadcast(gallivm, vs_type_llvm,
1120 LLVMBuildLoad(builder, scale_i, "scale"));
1121 trans = lp_build_broadcast(gallivm, vs_type_llvm,
1122 LLVMBuildLoad(builder, trans_i, "trans"));
1123
1124 /* divide by w */
1125 out = LLVMBuildFMul(builder, out, out3, "");
1126 /* mult by scale, add translation */
1127 out = lp_build_fmuladd(builder, out, scale, trans);
1128
1129 /* store transformed outputs */
1130 LLVMBuildStore(builder, out, outputs[pos][i]);
1131 }
1132
1133 }
1134
1135
1136 /**
1137 * Returns clipmask as nxi32 bitmask for the n vertices
1138 */
1139 static LLVMValueRef
1140 generate_clipmask(struct draw_llvm *llvm,
1141 struct gallivm_state *gallivm,
1142 struct lp_type vs_type,
1143 LLVMValueRef (*outputs)[TGSI_NUM_CHANNELS],
1144 struct draw_llvm_variant_key *key,
1145 LLVMValueRef context_ptr,
1146 boolean *have_clipdist)
1147 {
1148 LLVMBuilderRef builder = gallivm->builder;
1149 LLVMValueRef mask; /* stores the <nxi32> clipmasks */
1150 LLVMValueRef test, temp;
1151 LLVMValueRef zero, shift;
1152 LLVMValueRef pos_x, pos_y, pos_z, pos_w;
1153 LLVMValueRef cv_x, cv_y, cv_z, cv_w;
1154 LLVMValueRef plane1, planes, plane_ptr, sum;
1155 struct lp_type f32_type = vs_type;
1156 struct lp_type i32_type = lp_int_type(vs_type);
1157 const unsigned pos = llvm->draw->vs.position_output;
1158 const unsigned cv = llvm->draw->vs.clipvertex_output;
1159 int num_written_clipdistance = llvm->draw->vs.vertex_shader->info.num_written_clipdistance;
1160 boolean have_cd = false;
1161 boolean clip_user = key->clip_user;
1162 unsigned ucp_enable = key->ucp_enable;
1163 unsigned cd[2];
1164
1165 cd[0] = llvm->draw->vs.ccdistance_output[0];
1166 cd[1] = llvm->draw->vs.ccdistance_output[1];
1167
1168 if (cd[0] != pos || cd[1] != pos)
1169 have_cd = true;
1170
1171 if (num_written_clipdistance && !clip_user) {
1172 clip_user = true;
1173 ucp_enable = (1 << num_written_clipdistance) - 1;
1174 }
1175
1176 mask = lp_build_const_int_vec(gallivm, i32_type, 0);
1177 temp = lp_build_const_int_vec(gallivm, i32_type, 0);
1178 zero = lp_build_const_vec(gallivm, f32_type, 0); /* 0.0f 0.0f 0.0f 0.0f */
1179 shift = lp_build_const_int_vec(gallivm, i32_type, 1); /* 1 1 1 1 */
1180
1181 /*
1182 * load clipvertex and position from correct locations.
1183 * if they are the same just load them once.
1184 */
1185 pos_x = LLVMBuildLoad(builder, outputs[pos][0], ""); /*x0 x1 .. xn */
1186 pos_y = LLVMBuildLoad(builder, outputs[pos][1], ""); /*y0 y1 .. yn */
1187 pos_z = LLVMBuildLoad(builder, outputs[pos][2], ""); /*z0 z1 .. zn */
1188 pos_w = LLVMBuildLoad(builder, outputs[pos][3], ""); /*w0 w1 .. wn */
1189
1190 if (clip_user && cv != pos) {
1191 cv_x = LLVMBuildLoad(builder, outputs[cv][0], ""); /*x0 x1 .. xn */
1192 cv_y = LLVMBuildLoad(builder, outputs[cv][1], ""); /*y0 y1 .. yn */
1193 cv_z = LLVMBuildLoad(builder, outputs[cv][2], ""); /*z0 z1 .. zn */
1194 cv_w = LLVMBuildLoad(builder, outputs[cv][3], ""); /*w0 w1 .. wn */
1195 } else {
1196 cv_x = pos_x;
1197 cv_y = pos_y;
1198 cv_z = pos_z;
1199 cv_w = pos_w;
1200 }
1201
1202 /*
1203 * Be careful with the comparisons and NaNs (using llvm's unordered
1204 * comparisons here).
1205 */
1206 /* Cliptest, for hardwired planes */
1207 /*
1208 * XXX should take guardband into account (currently not in key).
1209 * Otherwise might run the draw pipeline stages for nothing.
1210 */
1211 if (key->clip_xy) {
1212 /* plane 1 */
1213 test = lp_build_compare(gallivm, f32_type, PIPE_FUNC_GREATER, pos_x , pos_w);
1214 temp = shift;
1215 test = LLVMBuildAnd(builder, test, temp, "");
1216 mask = test;
1217
1218 /* plane 2 */
1219 test = LLVMBuildFAdd(builder, pos_x, pos_w, "");
1220 test = lp_build_compare(gallivm, f32_type, PIPE_FUNC_GREATER, zero, test);
1221 temp = LLVMBuildShl(builder, temp, shift, "");
1222 test = LLVMBuildAnd(builder, test, temp, "");
1223 mask = LLVMBuildOr(builder, mask, test, "");
1224
1225 /* plane 3 */
1226 test = lp_build_compare(gallivm, f32_type, PIPE_FUNC_GREATER, pos_y, pos_w);
1227 temp = LLVMBuildShl(builder, temp, shift, "");
1228 test = LLVMBuildAnd(builder, test, temp, "");
1229 mask = LLVMBuildOr(builder, mask, test, "");
1230
1231 /* plane 4 */
1232 test = LLVMBuildFAdd(builder, pos_y, pos_w, "");
1233 test = lp_build_compare(gallivm, f32_type, PIPE_FUNC_GREATER, zero, test);
1234 temp = LLVMBuildShl(builder, temp, shift, "");
1235 test = LLVMBuildAnd(builder, test, temp, "");
1236 mask = LLVMBuildOr(builder, mask, test, "");
1237 }
1238
1239 if (key->clip_z) {
1240 temp = lp_build_const_int_vec(gallivm, i32_type, 16);
1241 if (key->clip_halfz) {
1242 /* plane 5 */
1243 test = lp_build_compare(gallivm, f32_type, PIPE_FUNC_GREATER, zero, pos_z);
1244 test = LLVMBuildAnd(builder, test, temp, "");
1245 mask = LLVMBuildOr(builder, mask, test, "");
1246 }
1247 else {
1248 /* plane 5 */
1249 test = LLVMBuildFAdd(builder, pos_z, pos_w, "");
1250 test = lp_build_compare(gallivm, f32_type, PIPE_FUNC_GREATER, zero, test);
1251 test = LLVMBuildAnd(builder, test, temp, "");
1252 mask = LLVMBuildOr(builder, mask, test, "");
1253 }
1254 /* plane 6 */
1255 test = lp_build_compare(gallivm, f32_type, PIPE_FUNC_GREATER, pos_z, pos_w);
1256 temp = LLVMBuildShl(builder, temp, shift, "");
1257 test = LLVMBuildAnd(builder, test, temp, "");
1258 mask = LLVMBuildOr(builder, mask, test, "");
1259 }
1260
1261 if (clip_user) {
1262 LLVMValueRef planes_ptr = draw_jit_context_planes(gallivm, context_ptr);
1263 LLVMValueRef indices[3];
1264 LLVMValueRef is_nan_or_inf;
1265
1266 /* userclip planes */
1267 while (ucp_enable) {
1268 unsigned plane_idx = ffs(ucp_enable)-1;
1269 ucp_enable &= ~(1 << plane_idx);
1270 plane_idx += 6;
1271
1272 if (have_cd && num_written_clipdistance) {
1273 LLVMValueRef clipdist;
1274 int i;
1275 i = plane_idx - 6;
1276
1277 *have_clipdist = TRUE;
1278 if (i < 4) {
1279 clipdist = LLVMBuildLoad(builder, outputs[cd[0]][i], "");
1280 } else {
1281 clipdist = LLVMBuildLoad(builder, outputs[cd[1]][i-4], "");
1282 }
1283 test = lp_build_compare(gallivm, f32_type, PIPE_FUNC_GREATER, zero, clipdist);
1284 is_nan_or_inf = lp_build_is_inf_or_nan(gallivm, vs_type, clipdist);
1285 test = LLVMBuildOr(builder, test, is_nan_or_inf, "");
1286 temp = lp_build_const_int_vec(gallivm, i32_type, 1LL << plane_idx);
1287 test = LLVMBuildAnd(builder, test, temp, "");
1288 mask = LLVMBuildOr(builder, mask, test, "");
1289 } else {
1290 LLVMTypeRef vs_type_llvm = lp_build_vec_type(gallivm, vs_type);
1291 indices[0] = lp_build_const_int32(gallivm, 0);
1292 indices[1] = lp_build_const_int32(gallivm, plane_idx);
1293
1294 indices[2] = lp_build_const_int32(gallivm, 0);
1295 plane_ptr = LLVMBuildGEP(builder, planes_ptr, indices, 3, "");
1296 plane1 = LLVMBuildLoad(builder, plane_ptr, "plane_x");
1297 planes = lp_build_broadcast(gallivm, vs_type_llvm, plane1);
1298 sum = LLVMBuildFMul(builder, planes, cv_x, "");
1299
1300 indices[2] = lp_build_const_int32(gallivm, 1);
1301 plane_ptr = LLVMBuildGEP(builder, planes_ptr, indices, 3, "");
1302 plane1 = LLVMBuildLoad(builder, plane_ptr, "plane_y");
1303 planes = lp_build_broadcast(gallivm, vs_type_llvm, plane1);
1304 sum = lp_build_fmuladd(builder, planes, cv_y, sum);
1305
1306 indices[2] = lp_build_const_int32(gallivm, 2);
1307 plane_ptr = LLVMBuildGEP(builder, planes_ptr, indices, 3, "");
1308 plane1 = LLVMBuildLoad(builder, plane_ptr, "plane_z");
1309 planes = lp_build_broadcast(gallivm, vs_type_llvm, plane1);
1310 sum = lp_build_fmuladd(builder, planes, cv_z, sum);
1311
1312 indices[2] = lp_build_const_int32(gallivm, 3);
1313 plane_ptr = LLVMBuildGEP(builder, planes_ptr, indices, 3, "");
1314 plane1 = LLVMBuildLoad(builder, plane_ptr, "plane_w");
1315 planes = lp_build_broadcast(gallivm, vs_type_llvm, plane1);
1316 sum = lp_build_fmuladd(builder, planes, cv_w, sum);
1317
1318 test = lp_build_compare(gallivm, f32_type, PIPE_FUNC_GREATER, zero, sum);
1319 temp = lp_build_const_int_vec(gallivm, i32_type, 1LL << plane_idx);
1320 test = LLVMBuildAnd(builder, test, temp, "");
1321 mask = LLVMBuildOr(builder, mask, test, "");
1322 }
1323 }
1324 }
1325 if (key->need_edgeflags) {
1326 /*
1327 * This isn't really part of clipmask but stored the same in vertex
1328 * header later, so do it here.
1329 */
1330 unsigned edge_attr = llvm->draw->vs.edgeflag_output;
1331 LLVMValueRef one = lp_build_const_vec(gallivm, f32_type, 1.0);
1332 LLVMValueRef edgeflag = LLVMBuildLoad(builder, outputs[edge_attr][0], "");
1333 test = lp_build_compare(gallivm, f32_type, PIPE_FUNC_EQUAL, one, edgeflag);
1334 temp = lp_build_const_int_vec(gallivm, i32_type,
1335 1LL << DRAW_TOTAL_CLIP_PLANES);
1336 test = LLVMBuildAnd(builder, test, temp, "");
1337 mask = LLVMBuildOr(builder, mask, test, "");
1338 }
1339 return mask;
1340 }
1341
1342
1343 /**
1344 * Returns boolean if any clipping has occurred
1345 * Used zero/non-zero i32 value to represent boolean
1346 */
1347 static LLVMValueRef
1348 clipmask_booli32(struct gallivm_state *gallivm,
1349 const struct lp_type vs_type,
1350 LLVMValueRef clipmask_bool_ptr,
1351 boolean edgeflag_in_clipmask)
1352 {
1353 LLVMBuilderRef builder = gallivm->builder;
1354 LLVMTypeRef int32_type = LLVMInt32TypeInContext(gallivm->context);
1355 LLVMValueRef clipmask_bool = LLVMBuildLoad(builder, clipmask_bool_ptr, "");
1356 LLVMValueRef ret = LLVMConstNull(int32_type);
1357 LLVMValueRef temp;
1358 int i;
1359
1360 /*
1361 * We need to invert the edgeflag bit from the clipmask here
1362 * (because the result is really if we want to run the pipeline or not
1363 * and we (may) need it if edgeflag was 0).
1364 */
1365 if (edgeflag_in_clipmask) {
1366 struct lp_type i32_type = lp_int_type(vs_type);
1367 LLVMValueRef edge = lp_build_const_int_vec(gallivm, i32_type,
1368 1LL << DRAW_TOTAL_CLIP_PLANES);
1369 clipmask_bool = LLVMBuildXor(builder, clipmask_bool, edge, "");
1370 }
1371 /*
1372 * Could do much better with just cmp/movmskps.
1373 */
1374 for (i=0; i < vs_type.length; i++) {
1375 temp = LLVMBuildExtractElement(builder, clipmask_bool,
1376 lp_build_const_int32(gallivm, i) , "");
1377 ret = LLVMBuildOr(builder, ret, temp, "");
1378 }
1379 return ret;
1380 }
1381
1382 static LLVMValueRef
1383 draw_gs_llvm_fetch_input(const struct lp_build_tgsi_gs_iface *gs_iface,
1384 struct lp_build_tgsi_context * bld_base,
1385 boolean is_vindex_indirect,
1386 LLVMValueRef vertex_index,
1387 boolean is_aindex_indirect,
1388 LLVMValueRef attrib_index,
1389 LLVMValueRef swizzle_index)
1390 {
1391 const struct draw_gs_llvm_iface *gs = draw_gs_llvm_iface(gs_iface);
1392 struct gallivm_state *gallivm = bld_base->base.gallivm;
1393 LLVMBuilderRef builder = gallivm->builder;
1394 LLVMValueRef indices[3];
1395 LLVMValueRef res;
1396 struct lp_type type = bld_base->base.type;
1397
1398 if (is_vindex_indirect || is_aindex_indirect) {
1399 int i;
1400 res = bld_base->base.zero;
1401 for (i = 0; i < type.length; ++i) {
1402 LLVMValueRef idx = lp_build_const_int32(gallivm, i);
1403 LLVMValueRef vert_chan_index = vertex_index;
1404 LLVMValueRef attr_chan_index = attrib_index;
1405 LLVMValueRef channel_vec, value;
1406
1407 if (is_vindex_indirect) {
1408 vert_chan_index = LLVMBuildExtractElement(builder,
1409 vertex_index, idx, "");
1410 }
1411 if (is_aindex_indirect) {
1412 attr_chan_index = LLVMBuildExtractElement(builder,
1413 attrib_index, idx, "");
1414 }
1415
1416 indices[0] = vert_chan_index;
1417 indices[1] = attr_chan_index;
1418 indices[2] = swizzle_index;
1419
1420 channel_vec = LLVMBuildGEP(builder, gs->input, indices, 3, "");
1421 channel_vec = LLVMBuildLoad(builder, channel_vec, "");
1422 value = LLVMBuildExtractElement(builder, channel_vec, idx, "");
1423
1424 res = LLVMBuildInsertElement(builder, res, value, idx, "");
1425 }
1426 } else {
1427 indices[0] = vertex_index;
1428 indices[1] = attrib_index;
1429 indices[2] = swizzle_index;
1430
1431 res = LLVMBuildGEP(builder, gs->input, indices, 3, "");
1432 res = LLVMBuildLoad(builder, res, "");
1433 }
1434
1435 return res;
1436 }
1437
1438 static void
1439 draw_gs_llvm_emit_vertex(const struct lp_build_tgsi_gs_iface *gs_base,
1440 struct lp_build_tgsi_context * bld_base,
1441 LLVMValueRef (*outputs)[4],
1442 LLVMValueRef emitted_vertices_vec)
1443 {
1444 const struct draw_gs_llvm_iface *gs_iface = draw_gs_llvm_iface(gs_base);
1445 struct draw_gs_llvm_variant *variant = gs_iface->variant;
1446 struct gallivm_state *gallivm = variant->gallivm;
1447 LLVMBuilderRef builder = gallivm->builder;
1448 struct lp_type gs_type = bld_base->base.type;
1449 LLVMValueRef clipmask = lp_build_const_int_vec(gallivm,
1450 lp_int_type(gs_type), 0);
1451 LLVMValueRef indices[LP_MAX_VECTOR_LENGTH];
1452 LLVMValueRef next_prim_offset =
1453 lp_build_const_int32(gallivm, variant->shader->base.primitive_boundary);
1454 LLVMValueRef io = variant->io_ptr;
1455 unsigned i;
1456 const struct tgsi_shader_info *gs_info = &variant->shader->base.info;
1457
1458 for (i = 0; i < gs_type.length; ++i) {
1459 LLVMValueRef ind = lp_build_const_int32(gallivm, i);
1460 LLVMValueRef currently_emitted =
1461 LLVMBuildExtractElement(builder, emitted_vertices_vec, ind, "");
1462 indices[i] = LLVMBuildMul(builder, ind, next_prim_offset, "");
1463 indices[i] = LLVMBuildAdd(builder, indices[i], currently_emitted, "");
1464 }
1465
1466 convert_to_aos(gallivm, io, indices,
1467 outputs, clipmask,
1468 gs_info->num_outputs, gs_type,
1469 FALSE);
1470 }
1471
1472 static void
1473 draw_gs_llvm_end_primitive(const struct lp_build_tgsi_gs_iface *gs_base,
1474 struct lp_build_tgsi_context * bld_base,
1475 LLVMValueRef verts_per_prim_vec,
1476 LLVMValueRef emitted_prims_vec)
1477 {
1478 const struct draw_gs_llvm_iface *gs_iface = draw_gs_llvm_iface(gs_base);
1479 struct draw_gs_llvm_variant *variant = gs_iface->variant;
1480 struct gallivm_state *gallivm = variant->gallivm;
1481 LLVMBuilderRef builder = gallivm->builder;
1482 LLVMValueRef prim_lengts_ptr =
1483 draw_gs_jit_prim_lengths(variant->gallivm, variant->context_ptr);
1484 unsigned i;
1485
1486 for (i = 0; i < bld_base->base.type.length; ++i) {
1487 LLVMValueRef ind = lp_build_const_int32(gallivm, i);
1488 LLVMValueRef prims_emitted =
1489 LLVMBuildExtractElement(builder, emitted_prims_vec, ind, "");
1490 LLVMValueRef store_ptr;
1491 LLVMValueRef num_vertices =
1492 LLVMBuildExtractElement(builder, verts_per_prim_vec, ind, "");
1493
1494 store_ptr = LLVMBuildGEP(builder, prim_lengts_ptr, &prims_emitted, 1, "");
1495 store_ptr = LLVMBuildLoad(builder, store_ptr, "");
1496 store_ptr = LLVMBuildGEP(builder, store_ptr, &ind, 1, "");
1497 LLVMBuildStore(builder, num_vertices, store_ptr);
1498 }
1499 }
1500
1501 static void
1502 draw_gs_llvm_epilogue(const struct lp_build_tgsi_gs_iface *gs_base,
1503 struct lp_build_tgsi_context * bld_base,
1504 LLVMValueRef total_emitted_vertices_vec,
1505 LLVMValueRef emitted_prims_vec)
1506 {
1507 const struct draw_gs_llvm_iface *gs_iface = draw_gs_llvm_iface(gs_base);
1508 struct draw_gs_llvm_variant *variant = gs_iface->variant;
1509 struct gallivm_state *gallivm = variant->gallivm;
1510 LLVMBuilderRef builder = gallivm->builder;
1511 LLVMValueRef emitted_verts_ptr =
1512 draw_gs_jit_emitted_vertices(gallivm, variant->context_ptr);
1513 LLVMValueRef emitted_prims_ptr =
1514 draw_gs_jit_emitted_prims(gallivm, variant->context_ptr);
1515 LLVMValueRef zero = lp_build_const_int32(gallivm, 0);
1516
1517 emitted_verts_ptr = LLVMBuildGEP(builder, emitted_verts_ptr, &zero, 0, "");
1518 emitted_prims_ptr = LLVMBuildGEP(builder, emitted_prims_ptr, &zero, 0, "");
1519
1520 LLVMBuildStore(builder, total_emitted_vertices_vec, emitted_verts_ptr);
1521 LLVMBuildStore(builder, emitted_prims_vec, emitted_prims_ptr);
1522 }
1523
1524 static void
1525 draw_llvm_generate(struct draw_llvm *llvm, struct draw_llvm_variant *variant,
1526 boolean elts)
1527 {
1528 struct gallivm_state *gallivm = variant->gallivm;
1529 LLVMContextRef context = gallivm->context;
1530 LLVMTypeRef int32_type = LLVMInt32TypeInContext(context);
1531 LLVMTypeRef arg_types[11];
1532 unsigned num_arg_types =
1533 elts ? ARRAY_SIZE(arg_types) : ARRAY_SIZE(arg_types) - 1;
1534 LLVMTypeRef func_type;
1535 LLVMValueRef context_ptr;
1536 LLVMBasicBlockRef block;
1537 LLVMBuilderRef builder;
1538 char func_name[64];
1539 struct lp_type vs_type;
1540 LLVMValueRef end, start;
1541 LLVMValueRef count, fetch_elts, fetch_elt_max, fetch_count;
1542 LLVMValueRef vertex_id_offset, start_instance;
1543 LLVMValueRef stride, step, io_itr;
1544 LLVMValueRef io_ptr, vbuffers_ptr, vb_ptr;
1545 LLVMValueRef zero = lp_build_const_int32(gallivm, 0);
1546 LLVMValueRef one = lp_build_const_int32(gallivm, 1);
1547 struct draw_context *draw = llvm->draw;
1548 const struct tgsi_shader_info *vs_info = &draw->vs.vertex_shader->info;
1549 unsigned i, j;
1550 struct lp_build_context bld;
1551 struct lp_build_loop_state lp_loop;
1552 const int vector_length = lp_native_vector_width / 32;
1553 LLVMValueRef outputs[PIPE_MAX_SHADER_OUTPUTS][TGSI_NUM_CHANNELS];
1554 LLVMValueRef fetch_max;
1555 struct lp_build_sampler_soa *sampler = 0;
1556 LLVMValueRef ret, clipmask_bool_ptr;
1557 struct draw_llvm_variant_key *key = &variant->key;
1558 /* If geometry shader is present we need to skip both the viewport
1559 * transformation and clipping otherwise the inputs to the geometry
1560 * shader will be incorrect.
1561 * The code can't handle vp transform when vs writes vp index neither
1562 * (though this would be fixable here, but couldn't just broadcast
1563 * the values).
1564 */
1565 const boolean bypass_viewport = key->has_gs || key->bypass_viewport ||
1566 llvm->draw->vs.vertex_shader->info.writes_viewport_index;
1567 const boolean enable_cliptest = !key->has_gs && (key->clip_xy ||
1568 key->clip_z ||
1569 key->clip_user ||
1570 key->need_edgeflags);
1571 LLVMValueRef variant_func;
1572 const unsigned pos = llvm->draw->vs.position_output;
1573 const unsigned cv = llvm->draw->vs.clipvertex_output;
1574 boolean have_clipdist = FALSE;
1575 struct lp_bld_tgsi_system_values system_values;
1576
1577 memset(&system_values, 0, sizeof(system_values));
1578
1579 util_snprintf(func_name, sizeof(func_name), "draw_llvm_vs_variant%u_%s",
1580 variant->shader->variants_cached, elts ? "elts" : "linear");
1581
1582 i = 0;
1583 arg_types[i++] = get_context_ptr_type(variant); /* context */
1584 arg_types[i++] = get_vertex_header_ptr_type(variant); /* vertex_header */
1585 arg_types[i++] = get_buffer_ptr_type(variant); /* vbuffers */
1586 if (elts) {
1587 arg_types[i++] = LLVMPointerType(int32_type, 0);/* fetch_elts */
1588 arg_types[i++] = int32_type; /* fetch_elt_max */
1589 } else
1590 arg_types[i++] = int32_type; /* start */
1591 arg_types[i++] = int32_type; /* fetch_count / count */
1592 arg_types[i++] = int32_type; /* stride */
1593 arg_types[i++] = get_vb_ptr_type(variant); /* pipe_vertex_buffer's */
1594 arg_types[i++] = int32_type; /* instance_id */
1595 arg_types[i++] = int32_type; /* vertex_id_offset */
1596 arg_types[i++] = int32_type; /* start_instance */
1597
1598 func_type = LLVMFunctionType(int32_type, arg_types, num_arg_types, 0);
1599
1600 variant_func = LLVMAddFunction(gallivm->module, func_name, func_type);
1601
1602 if (elts)
1603 variant->function_elts = variant_func;
1604 else
1605 variant->function = variant_func;
1606
1607 LLVMSetFunctionCallConv(variant_func, LLVMCCallConv);
1608 for (i = 0; i < num_arg_types; ++i)
1609 if (LLVMGetTypeKind(arg_types[i]) == LLVMPointerTypeKind)
1610 LLVMAddAttribute(LLVMGetParam(variant_func, i),
1611 LLVMNoAliasAttribute);
1612
1613 context_ptr = LLVMGetParam(variant_func, 0);
1614 io_ptr = LLVMGetParam(variant_func, 1);
1615 vbuffers_ptr = LLVMGetParam(variant_func, 2);
1616 /*
1617 * XXX: stride is actually unused. The stride we use is strictly calculated
1618 * from the number of outputs (including the draw_extra outputs).
1619 * Should probably fix some day (we need a new vs just because of extra
1620 * outputs which the generated vs won't touch).
1621 */
1622 stride = LLVMGetParam(variant_func, 5 + (elts ? 1 : 0));
1623 vb_ptr = LLVMGetParam(variant_func, 6 + (elts ? 1 : 0));
1624 system_values.instance_id = LLVMGetParam(variant_func, 7 + (elts ? 1 : 0));
1625 vertex_id_offset = LLVMGetParam(variant_func, 8 + (elts ? 1 : 0));
1626 start_instance = LLVMGetParam(variant_func, 9 + (elts ? 1 : 0));
1627
1628 lp_build_name(context_ptr, "context");
1629 lp_build_name(io_ptr, "io");
1630 lp_build_name(vbuffers_ptr, "vbuffers");
1631 lp_build_name(stride, "stride");
1632 lp_build_name(vb_ptr, "vb");
1633 lp_build_name(system_values.instance_id, "instance_id");
1634 lp_build_name(vertex_id_offset, "vertex_id_offset");
1635 lp_build_name(start_instance, "start_instance");
1636
1637 if (elts) {
1638 fetch_elts = LLVMGetParam(variant_func, 3);
1639 fetch_elt_max = LLVMGetParam(variant_func, 4);
1640 fetch_count = LLVMGetParam(variant_func, 5);
1641 lp_build_name(fetch_elts, "fetch_elts");
1642 lp_build_name(fetch_elt_max, "fetch_elt_max");
1643 lp_build_name(fetch_count, "fetch_count");
1644 start = count = NULL;
1645 }
1646 else {
1647 start = LLVMGetParam(variant_func, 3);
1648 count = LLVMGetParam(variant_func, 4);
1649 lp_build_name(start, "start");
1650 lp_build_name(count, "count");
1651 fetch_elts = fetch_count = NULL;
1652 }
1653
1654 /*
1655 * Function body
1656 */
1657
1658 block = LLVMAppendBasicBlockInContext(gallivm->context, variant_func, "entry");
1659 builder = gallivm->builder;
1660 LLVMPositionBuilderAtEnd(builder, block);
1661
1662 lp_build_context_init(&bld, gallivm, lp_type_int(32));
1663
1664 memset(&vs_type, 0, sizeof vs_type);
1665 vs_type.floating = TRUE; /* floating point values */
1666 vs_type.sign = TRUE; /* values are signed */
1667 vs_type.norm = FALSE; /* values are not limited to [0,1] or [-1,1] */
1668 vs_type.width = 32; /* 32-bit float */
1669 vs_type.length = vector_length;
1670
1671 /* hold temporary "bool" clipmask */
1672 clipmask_bool_ptr = lp_build_alloca(gallivm, lp_build_int_vec_type(gallivm, vs_type), "");
1673 LLVMBuildStore(builder, lp_build_zero(gallivm, lp_int_type(vs_type)), clipmask_bool_ptr);
1674
1675 /* code generated texture sampling */
1676 sampler = draw_llvm_sampler_soa_create(draw_llvm_variant_key_samplers(key));
1677
1678 if (elts) {
1679 start = zero;
1680 end = fetch_count;
1681 count = fetch_count;
1682 }
1683 else {
1684 end = lp_build_add(&bld, start, count);
1685 }
1686
1687 step = lp_build_const_int32(gallivm, vector_length);
1688
1689 fetch_max = LLVMBuildSub(builder, end, one, "fetch_max");
1690
1691 lp_build_loop_begin(&lp_loop, gallivm, zero);
1692 {
1693 LLVMValueRef inputs[PIPE_MAX_SHADER_INPUTS][TGSI_NUM_CHANNELS];
1694 LLVMValueRef aos_attribs[PIPE_MAX_SHADER_INPUTS][LP_MAX_VECTOR_WIDTH / 32] = { { 0 } };
1695 LLVMValueRef io;
1696 LLVMValueRef clipmask; /* holds the clipmask value */
1697 LLVMValueRef true_index_array = lp_build_zero(gallivm,
1698 lp_type_uint_vec(32, 32*vector_length));
1699 const LLVMValueRef (*ptr_aos)[TGSI_NUM_CHANNELS];
1700
1701 io_itr = lp_loop.counter;
1702
1703 io = LLVMBuildGEP(builder, io_ptr, &io_itr, 1, "");
1704 #if DEBUG_STORE
1705 lp_build_printf(gallivm, " --- io %d = %p, loop counter %d\n",
1706 io_itr, io, lp_loop.counter);
1707 #endif
1708 for (i = 0; i < vector_length; ++i) {
1709 LLVMValueRef vert_index =
1710 LLVMBuildAdd(builder,
1711 lp_loop.counter,
1712 lp_build_const_int32(gallivm, i), "");
1713 LLVMValueRef true_index =
1714 LLVMBuildAdd(builder, start, vert_index, "");
1715
1716 /* make sure we're not out of bounds which can happen
1717 * if fetch_count % 4 != 0, because on the last iteration
1718 * a few of the 4 vertex fetches will be out of bounds */
1719 true_index = lp_build_min(&bld, true_index, fetch_max);
1720
1721 if (elts) {
1722 LLVMValueRef fetch_ptr;
1723 LLVMValueRef index_overflowed;
1724 LLVMValueRef index_ptr =
1725 lp_build_alloca(
1726 gallivm,
1727 lp_build_vec_type(gallivm, lp_type_int(32)), "");
1728 struct lp_build_if_state if_ctx;
1729 index_overflowed = LLVMBuildICmp(builder, LLVMIntUGT,
1730 true_index, fetch_elt_max,
1731 "index_overflowed");
1732
1733 lp_build_if(&if_ctx, gallivm, index_overflowed);
1734 {
1735 /* Generate maximum possible index so that
1736 * generate_fetch can treat it just like
1737 * any other overflow and return zeros.
1738 * We don't have to worry about the restart
1739 * primitive index because it has already been
1740 * handled
1741 */
1742 LLVMValueRef val =
1743 lp_build_const_int32(gallivm, 0xffffffff);
1744 LLVMBuildStore(builder, val, index_ptr);
1745 }
1746 lp_build_else(&if_ctx);
1747 {
1748 LLVMValueRef val;
1749 fetch_ptr = LLVMBuildGEP(builder, fetch_elts,
1750 &true_index, 1, "");
1751 val = LLVMBuildLoad(builder, fetch_ptr, "");
1752 LLVMBuildStore(builder, val, index_ptr);
1753 }
1754 lp_build_endif(&if_ctx);
1755 true_index = LLVMBuildLoad(builder, index_ptr, "true_index");
1756 }
1757 true_index_array = LLVMBuildInsertElement(
1758 gallivm->builder, true_index_array, true_index,
1759 lp_build_const_int32(gallivm, i), "");
1760
1761 for (j = 0; j < draw->pt.nr_vertex_elements; ++j) {
1762 struct pipe_vertex_element *velem = &draw->pt.vertex_element[j];
1763 LLVMValueRef vb_index =
1764 lp_build_const_int32(gallivm, velem->vertex_buffer_index);
1765 LLVMValueRef vb = LLVMBuildGEP(builder, vb_ptr, &vb_index, 1, "");
1766 generate_fetch(gallivm, draw, vbuffers_ptr,
1767 &aos_attribs[j][i], velem, vb, true_index,
1768 system_values.instance_id, start_instance);
1769 }
1770 }
1771 convert_to_soa(gallivm, aos_attribs, inputs,
1772 draw->pt.nr_vertex_elements, vs_type);
1773
1774 /* In the paths with elts vertex id has to be unaffected by the
1775 * index bias and because indices inside our elements array have
1776 * already had index bias applied we need to subtract it here to
1777 * get back to the original index.
1778 * in the linear paths vertex id has to be unaffected by the
1779 * original start index and because we abuse the 'start' variable
1780 * to either represent the actual start index or the index at which
1781 * the primitive was split (we split rendering into chunks of at
1782 * most 4095-vertices) we need to back out the original start
1783 * index out of our vertex id here.
1784 */
1785 system_values.basevertex = lp_build_broadcast(gallivm, lp_build_vec_type(gallivm,
1786 lp_type_uint_vec(32, 32*vector_length)),
1787 vertex_id_offset);
1788 system_values.vertex_id = true_index_array;
1789 system_values.vertex_id_nobase = LLVMBuildSub(builder, true_index_array,
1790 system_values.basevertex, "");
1791
1792 ptr_aos = (const LLVMValueRef (*)[TGSI_NUM_CHANNELS]) inputs;
1793 generate_vs(variant,
1794 builder,
1795 vs_type,
1796 outputs,
1797 ptr_aos,
1798 &system_values,
1799 context_ptr,
1800 sampler,
1801 key->clamp_vertex_color);
1802
1803 if (pos != -1 && cv != -1) {
1804 /* store original positions in clip before further manipulation */
1805 store_clip(gallivm, vs_type, io, outputs, pos);
1806
1807 /* do cliptest */
1808 if (enable_cliptest) {
1809 LLVMValueRef temp = LLVMBuildLoad(builder, clipmask_bool_ptr, "");
1810 /* allocate clipmask, assign it integer type */
1811 clipmask = generate_clipmask(llvm,
1812 gallivm,
1813 vs_type,
1814 outputs,
1815 key,
1816 context_ptr, &have_clipdist);
1817 temp = LLVMBuildOr(builder, clipmask, temp, "");
1818 /* store temporary clipping boolean value */
1819 LLVMBuildStore(builder, temp, clipmask_bool_ptr);
1820 }
1821 else {
1822 clipmask = lp_build_const_int_vec(gallivm, lp_int_type(vs_type), 0);
1823 }
1824
1825 /* do viewport mapping */
1826 if (!bypass_viewport) {
1827 generate_viewport(variant, builder, vs_type, outputs, context_ptr);
1828 }
1829 }
1830 else {
1831 clipmask = lp_build_const_int_vec(gallivm, lp_int_type(vs_type), 0);
1832 }
1833
1834 /* store clipmask in vertex header,
1835 * original positions in clip
1836 * and transformed positions in data
1837 */
1838 convert_to_aos(gallivm, io, NULL, outputs, clipmask,
1839 vs_info->num_outputs, vs_type,
1840 enable_cliptest && key->need_edgeflags);
1841 }
1842 lp_build_loop_end_cond(&lp_loop, count, step, LLVMIntUGE);
1843
1844 sampler->destroy(sampler);
1845
1846 /* return clipping boolean value for function */
1847 ret = clipmask_booli32(gallivm, vs_type, clipmask_bool_ptr,
1848 enable_cliptest && key->need_edgeflags);
1849
1850 LLVMBuildRet(builder, ret);
1851
1852 gallivm_verify_function(gallivm, variant_func);
1853 }
1854
1855
1856 struct draw_llvm_variant_key *
1857 draw_llvm_make_variant_key(struct draw_llvm *llvm, char *store)
1858 {
1859 unsigned i;
1860 struct draw_llvm_variant_key *key;
1861 struct draw_sampler_static_state *draw_sampler;
1862
1863 key = (struct draw_llvm_variant_key *)store;
1864
1865 memset(key, 0, offsetof(struct draw_llvm_variant_key, vertex_element[0]));
1866
1867 key->clamp_vertex_color = llvm->draw->rasterizer->clamp_vertex_color; /**/
1868
1869 /* Presumably all variants of the shader should have the same
1870 * number of vertex elements - ie the number of shader inputs.
1871 * NOTE: we NEED to store the needed number of needed inputs
1872 * here, not the number of provided elements to match keysize
1873 * (and the offset of sampler state in the key).
1874 */
1875 key->nr_vertex_elements = llvm->draw->vs.vertex_shader->info.file_max[TGSI_FILE_INPUT] + 1;
1876 assert(key->nr_vertex_elements <= llvm->draw->pt.nr_vertex_elements);
1877
1878 /* will have to rig this up properly later */
1879 key->clip_xy = llvm->draw->clip_xy;
1880 key->clip_z = llvm->draw->clip_z;
1881 key->clip_user = llvm->draw->clip_user;
1882 key->bypass_viewport = llvm->draw->bypass_viewport;
1883 key->clip_halfz = llvm->draw->rasterizer->clip_halfz;
1884 /* XXX assumes edgeflag output not at 0 */
1885 key->need_edgeflags = (llvm->draw->vs.edgeflag_output ? TRUE : FALSE);
1886 key->ucp_enable = llvm->draw->rasterizer->clip_plane_enable;
1887 key->has_gs = llvm->draw->gs.geometry_shader != NULL;
1888 key->num_outputs = draw_total_vs_outputs(llvm->draw);
1889
1890 /* All variants of this shader will have the same value for
1891 * nr_samplers. Not yet trying to compact away holes in the
1892 * sampler array.
1893 */
1894 key->nr_samplers = llvm->draw->vs.vertex_shader->info.file_max[TGSI_FILE_SAMPLER] + 1;
1895 if (llvm->draw->vs.vertex_shader->info.file_max[TGSI_FILE_SAMPLER_VIEW] != -1) {
1896 key->nr_sampler_views =
1897 llvm->draw->vs.vertex_shader->info.file_max[TGSI_FILE_SAMPLER_VIEW] + 1;
1898 }
1899 else {
1900 key->nr_sampler_views = key->nr_samplers;
1901 }
1902
1903 draw_sampler = draw_llvm_variant_key_samplers(key);
1904
1905 memcpy(key->vertex_element,
1906 llvm->draw->pt.vertex_element,
1907 sizeof(struct pipe_vertex_element) * key->nr_vertex_elements);
1908
1909 memset(draw_sampler, 0, MAX2(key->nr_samplers, key->nr_sampler_views) * sizeof *draw_sampler);
1910
1911 for (i = 0 ; i < key->nr_samplers; i++) {
1912 lp_sampler_static_sampler_state(&draw_sampler[i].sampler_state,
1913 llvm->draw->samplers[PIPE_SHADER_VERTEX][i]);
1914 }
1915 for (i = 0 ; i < key->nr_sampler_views; i++) {
1916 lp_sampler_static_texture_state(&draw_sampler[i].texture_state,
1917 llvm->draw->sampler_views[PIPE_SHADER_VERTEX][i]);
1918 }
1919
1920 return key;
1921 }
1922
1923
1924 void
1925 draw_llvm_dump_variant_key(struct draw_llvm_variant_key *key)
1926 {
1927 unsigned i;
1928 struct draw_sampler_static_state *sampler = draw_llvm_variant_key_samplers(key);
1929
1930 debug_printf("clamp_vertex_color = %u\n", key->clamp_vertex_color);
1931 debug_printf("clip_xy = %u\n", key->clip_xy);
1932 debug_printf("clip_z = %u\n", key->clip_z);
1933 debug_printf("clip_user = %u\n", key->clip_user);
1934 debug_printf("bypass_viewport = %u\n", key->bypass_viewport);
1935 debug_printf("clip_halfz = %u\n", key->clip_halfz);
1936 debug_printf("need_edgeflags = %u\n", key->need_edgeflags);
1937 debug_printf("has_gs = %u\n", key->has_gs);
1938 debug_printf("ucp_enable = %u\n", key->ucp_enable);
1939
1940 for (i = 0 ; i < key->nr_vertex_elements; i++) {
1941 debug_printf("vertex_element[%i].src_offset = %u\n", i, key->vertex_element[i].src_offset);
1942 debug_printf("vertex_element[%i].instance_divisor = %u\n", i, key->vertex_element[i].instance_divisor);
1943 debug_printf("vertex_element[%i].vertex_buffer_index = %u\n", i, key->vertex_element[i].vertex_buffer_index);
1944 debug_printf("vertex_element[%i].src_format = %s\n", i, util_format_name(key->vertex_element[i].src_format));
1945 }
1946
1947 for (i = 0 ; i < key->nr_sampler_views; i++) {
1948 debug_printf("sampler[%i].src_format = %s\n", i, util_format_name(sampler[i].texture_state.format));
1949 }
1950 }
1951
1952
1953 void
1954 draw_llvm_set_mapped_texture(struct draw_context *draw,
1955 unsigned shader_stage,
1956 unsigned sview_idx,
1957 uint32_t width, uint32_t height, uint32_t depth,
1958 uint32_t first_level, uint32_t last_level,
1959 const void *base_ptr,
1960 uint32_t row_stride[PIPE_MAX_TEXTURE_LEVELS],
1961 uint32_t img_stride[PIPE_MAX_TEXTURE_LEVELS],
1962 uint32_t mip_offsets[PIPE_MAX_TEXTURE_LEVELS])
1963 {
1964 unsigned j;
1965 struct draw_jit_texture *jit_tex;
1966
1967 assert(shader_stage == PIPE_SHADER_VERTEX ||
1968 shader_stage == PIPE_SHADER_GEOMETRY);
1969
1970 if (shader_stage == PIPE_SHADER_VERTEX) {
1971 assert(sview_idx < ARRAY_SIZE(draw->llvm->jit_context.textures));
1972
1973 jit_tex = &draw->llvm->jit_context.textures[sview_idx];
1974 } else if (shader_stage == PIPE_SHADER_GEOMETRY) {
1975 assert(sview_idx < ARRAY_SIZE(draw->llvm->gs_jit_context.textures));
1976
1977 jit_tex = &draw->llvm->gs_jit_context.textures[sview_idx];
1978 } else {
1979 assert(0);
1980 return;
1981 }
1982
1983 jit_tex->width = width;
1984 jit_tex->height = height;
1985 jit_tex->depth = depth;
1986 jit_tex->first_level = first_level;
1987 jit_tex->last_level = last_level;
1988 jit_tex->base = base_ptr;
1989
1990 for (j = first_level; j <= last_level; j++) {
1991 jit_tex->mip_offsets[j] = mip_offsets[j];
1992 jit_tex->row_stride[j] = row_stride[j];
1993 jit_tex->img_stride[j] = img_stride[j];
1994 }
1995 }
1996
1997
1998 void
1999 draw_llvm_set_sampler_state(struct draw_context *draw,
2000 unsigned shader_type)
2001 {
2002 unsigned i;
2003
2004 if (shader_type == PIPE_SHADER_VERTEX) {
2005 for (i = 0; i < draw->num_samplers[PIPE_SHADER_VERTEX]; i++) {
2006 struct draw_jit_sampler *jit_sam = &draw->llvm->jit_context.samplers[i];
2007
2008 if (draw->samplers[PIPE_SHADER_VERTEX][i]) {
2009 const struct pipe_sampler_state *s
2010 = draw->samplers[PIPE_SHADER_VERTEX][i];
2011 jit_sam->min_lod = s->min_lod;
2012 jit_sam->max_lod = s->max_lod;
2013 jit_sam->lod_bias = s->lod_bias;
2014 COPY_4V(jit_sam->border_color, s->border_color.f);
2015 }
2016 }
2017 } else if (shader_type == PIPE_SHADER_GEOMETRY) {
2018 for (i = 0; i < draw->num_samplers[PIPE_SHADER_GEOMETRY]; i++) {
2019 struct draw_jit_sampler *jit_sam = &draw->llvm->gs_jit_context.samplers[i];
2020
2021 if (draw->samplers[PIPE_SHADER_GEOMETRY][i]) {
2022 const struct pipe_sampler_state *s
2023 = draw->samplers[PIPE_SHADER_GEOMETRY][i];
2024 jit_sam->min_lod = s->min_lod;
2025 jit_sam->max_lod = s->max_lod;
2026 jit_sam->lod_bias = s->lod_bias;
2027 COPY_4V(jit_sam->border_color, s->border_color.f);
2028 }
2029 }
2030 }
2031 }
2032
2033
2034 void
2035 draw_llvm_destroy_variant(struct draw_llvm_variant *variant)
2036 {
2037 struct draw_llvm *llvm = variant->llvm;
2038
2039 gallivm_destroy(variant->gallivm);
2040
2041 remove_from_list(&variant->list_item_local);
2042 variant->shader->variants_cached--;
2043 remove_from_list(&variant->list_item_global);
2044 llvm->nr_variants--;
2045 FREE(variant);
2046 }
2047
2048
2049 /**
2050 * Create LLVM types for various structures.
2051 */
2052 static void
2053 create_gs_jit_types(struct draw_gs_llvm_variant *var)
2054 {
2055 struct gallivm_state *gallivm = var->gallivm;
2056 LLVMTypeRef texture_type, sampler_type, context_type;
2057
2058 texture_type = create_jit_texture_type(gallivm, "texture");
2059 sampler_type = create_jit_sampler_type(gallivm, "sampler");
2060
2061 context_type = create_gs_jit_context_type(gallivm,
2062 var->shader->base.vector_length,
2063 texture_type, sampler_type,
2064 "draw_gs_jit_context");
2065 var->context_ptr_type = LLVMPointerType(context_type, 0);
2066
2067 var->input_array_type = create_gs_jit_input_type(gallivm);
2068 }
2069
2070 static LLVMTypeRef
2071 get_gs_context_ptr_type(struct draw_gs_llvm_variant *variant)
2072 {
2073 if (!variant->context_ptr_type)
2074 create_gs_jit_types(variant);
2075 return variant->context_ptr_type;
2076 }
2077
2078 static LLVMValueRef
2079 generate_mask_value(struct draw_gs_llvm_variant *variant,
2080 struct lp_type gs_type)
2081 {
2082 struct gallivm_state *gallivm = variant->gallivm;
2083 LLVMBuilderRef builder = gallivm->builder;
2084 struct lp_type mask_type = lp_int_type(gs_type);
2085 LLVMValueRef num_prims;
2086 LLVMValueRef mask_val = lp_build_const_vec(gallivm, mask_type, 0);
2087 unsigned i;
2088
2089 num_prims = lp_build_broadcast(gallivm, lp_build_vec_type(gallivm, mask_type),
2090 variant->num_prims);
2091 for (i = 0; i < gs_type.length; i++) {
2092 LLVMValueRef idx = lp_build_const_int32(gallivm, i);
2093 mask_val = LLVMBuildInsertElement(builder, mask_val, idx, idx, "");
2094 }
2095 mask_val = lp_build_compare(gallivm, mask_type,
2096 PIPE_FUNC_GREATER, num_prims, mask_val);
2097
2098 return mask_val;
2099 }
2100
2101 static void
2102 draw_gs_llvm_generate(struct draw_llvm *llvm,
2103 struct draw_gs_llvm_variant *variant)
2104 {
2105 struct gallivm_state *gallivm = variant->gallivm;
2106 LLVMContextRef context = gallivm->context;
2107 LLVMTypeRef int32_type = LLVMInt32TypeInContext(context);
2108 LLVMTypeRef arg_types[7];
2109 LLVMTypeRef func_type;
2110 LLVMValueRef variant_func;
2111 LLVMValueRef context_ptr;
2112 LLVMValueRef prim_id_ptr;
2113 LLVMBasicBlockRef block;
2114 LLVMBuilderRef builder;
2115 LLVMValueRef io_ptr, input_array, num_prims, mask_val;
2116 struct lp_build_sampler_soa *sampler = 0;
2117 struct lp_build_context bld;
2118 struct lp_bld_tgsi_system_values system_values;
2119 char func_name[64];
2120 struct lp_type gs_type;
2121 unsigned i;
2122 struct draw_gs_llvm_iface gs_iface;
2123 const struct tgsi_token *tokens = variant->shader->base.state.tokens;
2124 LLVMValueRef consts_ptr, num_consts_ptr;
2125 LLVMValueRef outputs[PIPE_MAX_SHADER_OUTPUTS][TGSI_NUM_CHANNELS];
2126 struct lp_build_mask_context mask;
2127 const struct tgsi_shader_info *gs_info = &variant->shader->base.info;
2128 unsigned vector_length = variant->shader->base.vector_length;
2129
2130 memset(&system_values, 0, sizeof(system_values));
2131
2132 util_snprintf(func_name, sizeof(func_name), "draw_llvm_gs_variant%u",
2133 variant->shader->variants_cached);
2134
2135 assert(variant->vertex_header_ptr_type);
2136
2137 arg_types[0] = get_gs_context_ptr_type(variant); /* context */
2138 arg_types[1] = variant->input_array_type; /* input */
2139 arg_types[2] = variant->vertex_header_ptr_type; /* vertex_header */
2140 arg_types[3] = int32_type; /* num_prims */
2141 arg_types[4] = int32_type; /* instance_id */
2142 arg_types[5] = LLVMPointerType(
2143 LLVMVectorType(int32_type, vector_length), 0); /* prim_id_ptr */
2144 arg_types[6] = int32_type;
2145
2146 func_type = LLVMFunctionType(int32_type, arg_types, ARRAY_SIZE(arg_types), 0);
2147
2148 variant_func = LLVMAddFunction(gallivm->module, func_name, func_type);
2149
2150 variant->function = variant_func;
2151
2152 LLVMSetFunctionCallConv(variant_func, LLVMCCallConv);
2153
2154 for (i = 0; i < ARRAY_SIZE(arg_types); ++i)
2155 if (LLVMGetTypeKind(arg_types[i]) == LLVMPointerTypeKind)
2156 LLVMAddAttribute(LLVMGetParam(variant_func, i),
2157 LLVMNoAliasAttribute);
2158
2159 context_ptr = LLVMGetParam(variant_func, 0);
2160 input_array = LLVMGetParam(variant_func, 1);
2161 io_ptr = LLVMGetParam(variant_func, 2);
2162 num_prims = LLVMGetParam(variant_func, 3);
2163 system_values.instance_id = LLVMGetParam(variant_func, 4);
2164 prim_id_ptr = LLVMGetParam(variant_func, 5);
2165 system_values.invocation_id = LLVMGetParam(variant_func, 6);
2166
2167 lp_build_name(context_ptr, "context");
2168 lp_build_name(input_array, "input");
2169 lp_build_name(io_ptr, "io");
2170 lp_build_name(num_prims, "num_prims");
2171 lp_build_name(system_values.instance_id, "instance_id");
2172 lp_build_name(prim_id_ptr, "prim_id_ptr");
2173 lp_build_name(system_values.invocation_id, "invocation_id");
2174
2175 variant->context_ptr = context_ptr;
2176 variant->io_ptr = io_ptr;
2177 variant->num_prims = num_prims;
2178
2179 gs_iface.base.fetch_input = draw_gs_llvm_fetch_input;
2180 gs_iface.base.emit_vertex = draw_gs_llvm_emit_vertex;
2181 gs_iface.base.end_primitive = draw_gs_llvm_end_primitive;
2182 gs_iface.base.gs_epilogue = draw_gs_llvm_epilogue;
2183 gs_iface.input = input_array;
2184 gs_iface.variant = variant;
2185
2186 /*
2187 * Function body
2188 */
2189
2190 block = LLVMAppendBasicBlockInContext(gallivm->context, variant_func, "entry");
2191 builder = gallivm->builder;
2192 LLVMPositionBuilderAtEnd(builder, block);
2193
2194 lp_build_context_init(&bld, gallivm, lp_type_int(32));
2195
2196 memset(&gs_type, 0, sizeof gs_type);
2197 gs_type.floating = TRUE; /* floating point values */
2198 gs_type.sign = TRUE; /* values are signed */
2199 gs_type.norm = FALSE; /* values are not limited to [0,1] or [-1,1] */
2200 gs_type.width = 32; /* 32-bit float */
2201 gs_type.length = vector_length;
2202
2203 consts_ptr = draw_gs_jit_context_constants(variant->gallivm, context_ptr);
2204 num_consts_ptr =
2205 draw_gs_jit_context_num_constants(variant->gallivm, context_ptr);
2206
2207 /* code generated texture sampling */
2208 sampler = draw_llvm_sampler_soa_create(variant->key.samplers);
2209
2210 mask_val = generate_mask_value(variant, gs_type);
2211 lp_build_mask_begin(&mask, gallivm, gs_type, mask_val);
2212
2213 if (gs_info->uses_primid) {
2214 system_values.prim_id = LLVMBuildLoad(builder, prim_id_ptr, "prim_id");
2215 }
2216
2217 if (gallivm_debug & (GALLIVM_DEBUG_TGSI | GALLIVM_DEBUG_IR)) {
2218 tgsi_dump(tokens, 0);
2219 draw_gs_llvm_dump_variant_key(&variant->key);
2220 }
2221
2222 lp_build_tgsi_soa(variant->gallivm,
2223 tokens,
2224 gs_type,
2225 &mask,
2226 consts_ptr,
2227 num_consts_ptr,
2228 &system_values,
2229 NULL,
2230 outputs,
2231 context_ptr,
2232 NULL,
2233 sampler,
2234 &llvm->draw->gs.geometry_shader->info,
2235 (const struct lp_build_tgsi_gs_iface *)&gs_iface);
2236
2237 sampler->destroy(sampler);
2238
2239 lp_build_mask_end(&mask);
2240
2241 LLVMBuildRet(builder, lp_build_zero(gallivm, lp_type_uint(32)));
2242
2243 gallivm_verify_function(gallivm, variant_func);
2244 }
2245
2246
2247 struct draw_gs_llvm_variant *
2248 draw_gs_llvm_create_variant(struct draw_llvm *llvm,
2249 unsigned num_outputs,
2250 const struct draw_gs_llvm_variant_key *key)
2251 {
2252 struct draw_gs_llvm_variant *variant;
2253 struct llvm_geometry_shader *shader =
2254 llvm_geometry_shader(llvm->draw->gs.geometry_shader);
2255 LLVMTypeRef vertex_header;
2256 char module_name[64];
2257
2258 variant = MALLOC(sizeof *variant +
2259 shader->variant_key_size -
2260 sizeof variant->key);
2261 if (!variant)
2262 return NULL;
2263
2264 variant->llvm = llvm;
2265 variant->shader = shader;
2266
2267 util_snprintf(module_name, sizeof(module_name), "draw_llvm_gs_variant%u",
2268 variant->shader->variants_cached);
2269
2270 variant->gallivm = gallivm_create(module_name, llvm->context);
2271
2272 create_gs_jit_types(variant);
2273
2274 memcpy(&variant->key, key, shader->variant_key_size);
2275
2276 vertex_header = create_jit_vertex_header(variant->gallivm, num_outputs);
2277
2278 variant->vertex_header_ptr_type = LLVMPointerType(vertex_header, 0);
2279
2280 draw_gs_llvm_generate(llvm, variant);
2281
2282 gallivm_compile_module(variant->gallivm);
2283
2284 variant->jit_func = (draw_gs_jit_func)
2285 gallivm_jit_function(variant->gallivm, variant->function);
2286
2287 gallivm_free_ir(variant->gallivm);
2288
2289 variant->list_item_global.base = variant;
2290 variant->list_item_local.base = variant;
2291 /*variant->no = */shader->variants_created++;
2292 variant->list_item_global.base = variant;
2293
2294 return variant;
2295 }
2296
2297 void
2298 draw_gs_llvm_destroy_variant(struct draw_gs_llvm_variant *variant)
2299 {
2300 struct draw_llvm *llvm = variant->llvm;
2301
2302 gallivm_destroy(variant->gallivm);
2303
2304 remove_from_list(&variant->list_item_local);
2305 variant->shader->variants_cached--;
2306 remove_from_list(&variant->list_item_global);
2307 llvm->nr_gs_variants--;
2308 FREE(variant);
2309 }
2310
2311 struct draw_gs_llvm_variant_key *
2312 draw_gs_llvm_make_variant_key(struct draw_llvm *llvm, char *store)
2313 {
2314 unsigned i;
2315 struct draw_gs_llvm_variant_key *key;
2316 struct draw_sampler_static_state *draw_sampler;
2317
2318 key = (struct draw_gs_llvm_variant_key *)store;
2319
2320 memset(key, 0, offsetof(struct draw_gs_llvm_variant_key, samplers[0]));
2321
2322 key->num_outputs = draw_total_gs_outputs(llvm->draw);
2323
2324 /* All variants of this shader will have the same value for
2325 * nr_samplers. Not yet trying to compact away holes in the
2326 * sampler array.
2327 */
2328 key->nr_samplers = llvm->draw->gs.geometry_shader->info.file_max[TGSI_FILE_SAMPLER] + 1;
2329 if (llvm->draw->gs.geometry_shader->info.file_max[TGSI_FILE_SAMPLER_VIEW] != -1) {
2330 key->nr_sampler_views =
2331 llvm->draw->gs.geometry_shader->info.file_max[TGSI_FILE_SAMPLER_VIEW] + 1;
2332 }
2333 else {
2334 key->nr_sampler_views = key->nr_samplers;
2335 }
2336
2337 draw_sampler = key->samplers;
2338
2339 memset(draw_sampler, 0, MAX2(key->nr_samplers, key->nr_sampler_views) * sizeof *draw_sampler);
2340
2341 for (i = 0 ; i < key->nr_samplers; i++) {
2342 lp_sampler_static_sampler_state(&draw_sampler[i].sampler_state,
2343 llvm->draw->samplers[PIPE_SHADER_GEOMETRY][i]);
2344 }
2345 for (i = 0 ; i < key->nr_sampler_views; i++) {
2346 lp_sampler_static_texture_state(&draw_sampler[i].texture_state,
2347 llvm->draw->sampler_views[PIPE_SHADER_GEOMETRY][i]);
2348 }
2349
2350 return key;
2351 }
2352
2353 void
2354 draw_gs_llvm_dump_variant_key(struct draw_gs_llvm_variant_key *key)
2355 {
2356 unsigned i;
2357 struct draw_sampler_static_state *sampler = key->samplers;
2358
2359 for (i = 0 ; i < key->nr_sampler_views; i++) {
2360 debug_printf("sampler[%i].src_format = %s\n", i,
2361 util_format_name(sampler[i].texture_state.format));
2362 }
2363 }