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