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