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