draw/llvm: add clip distance support
[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
33 #include "gallivm/lp_bld_arit.h"
34 #include "gallivm/lp_bld_logic.h"
35 #include "gallivm/lp_bld_const.h"
36 #include "gallivm/lp_bld_swizzle.h"
37 #include "gallivm/lp_bld_struct.h"
38 #include "gallivm/lp_bld_type.h"
39 #include "gallivm/lp_bld_flow.h"
40 #include "gallivm/lp_bld_debug.h"
41 #include "gallivm/lp_bld_tgsi.h"
42 #include "gallivm/lp_bld_printf.h"
43 #include "gallivm/lp_bld_intr.h"
44 #include "gallivm/lp_bld_init.h"
45 #include "gallivm/lp_bld_type.h"
46
47 #include "tgsi/tgsi_exec.h"
48 #include "tgsi/tgsi_dump.h"
49
50 #include "util/u_math.h"
51 #include "util/u_pointer.h"
52 #include "util/u_string.h"
53 #include "util/u_simple_list.h"
54
55
56 #define DEBUG_STORE 0
57
58
59 /**
60 * This function is called by the gallivm "garbage collector" when
61 * the LLVM global data structures are freed. We must free all LLVM-related
62 * data. Specifically, all JIT'd shader variants.
63 */
64 static void
65 draw_llvm_garbage_collect_callback(void *cb_data)
66 {
67 struct draw_llvm *llvm = (struct draw_llvm *) cb_data;
68 struct draw_context *draw = llvm->draw;
69 struct draw_llvm_variant_list_item *li;
70
71 /* Ensure prepare will be run and shaders recompiled */
72 assert(!draw->suspend_flushing);
73 draw_do_flush(draw, DRAW_FLUSH_STATE_CHANGE);
74
75 /* free all shader variants */
76 li = first_elem(&llvm->vs_variants_list);
77 while (!at_end(&llvm->vs_variants_list, li)) {
78 struct draw_llvm_variant_list_item *next = next_elem(li);
79 draw_llvm_destroy_variant(li->base);
80 li = next;
81 }
82
83 /* Null-out these pointers so they get remade next time they're needed.
84 * See the accessor functions below.
85 */
86 llvm->context_ptr_type = NULL;
87 llvm->buffer_ptr_type = NULL;
88 llvm->vb_ptr_type = NULL;
89 llvm->vertex_header_ptr_type = NULL;
90 }
91
92
93 static void
94 draw_llvm_generate(struct draw_llvm *llvm, struct draw_llvm_variant *var,
95 boolean elts);
96
97
98 /**
99 * Create LLVM type for struct draw_jit_texture
100 */
101 static LLVMTypeRef
102 create_jit_texture_type(struct gallivm_state *gallivm, const char *struct_name)
103 {
104 LLVMTargetDataRef target = gallivm->target;
105 LLVMTypeRef texture_type;
106 LLVMTypeRef elem_types[DRAW_JIT_TEXTURE_NUM_FIELDS];
107 LLVMTypeRef int32_type = LLVMInt32TypeInContext(gallivm->context);
108
109 elem_types[DRAW_JIT_TEXTURE_WIDTH] =
110 elem_types[DRAW_JIT_TEXTURE_HEIGHT] =
111 elem_types[DRAW_JIT_TEXTURE_DEPTH] =
112 elem_types[DRAW_JIT_TEXTURE_FIRST_LEVEL] =
113 elem_types[DRAW_JIT_TEXTURE_LAST_LEVEL] = int32_type;
114 elem_types[DRAW_JIT_TEXTURE_ROW_STRIDE] =
115 elem_types[DRAW_JIT_TEXTURE_IMG_STRIDE] =
116 LLVMArrayType(int32_type, PIPE_MAX_TEXTURE_LEVELS);
117 elem_types[DRAW_JIT_TEXTURE_DATA] =
118 LLVMArrayType(LLVMPointerType(LLVMInt8TypeInContext(gallivm->context), 0),
119 PIPE_MAX_TEXTURE_LEVELS);
120 elem_types[DRAW_JIT_TEXTURE_MIN_LOD] =
121 elem_types[DRAW_JIT_TEXTURE_MAX_LOD] =
122 elem_types[DRAW_JIT_TEXTURE_LOD_BIAS] = LLVMFloatTypeInContext(gallivm->context);
123 elem_types[DRAW_JIT_TEXTURE_BORDER_COLOR] =
124 LLVMArrayType(LLVMFloatTypeInContext(gallivm->context), 4);
125
126 #if HAVE_LLVM >= 0x0300
127 texture_type = LLVMStructCreateNamed(gallivm->context, struct_name);
128 LLVMStructSetBody(texture_type, elem_types,
129 Elements(elem_types), 0);
130 #else
131 texture_type = LLVMStructTypeInContext(gallivm->context, elem_types,
132 Elements(elem_types), 0);
133
134 LLVMAddTypeName(gallivm->module, struct_name, texture_type);
135
136 /* Make sure the target's struct layout cache doesn't return
137 * stale/invalid data.
138 */
139 LLVMInvalidateStructLayout(gallivm->target, texture_type);
140 #endif
141
142 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture, width,
143 target, texture_type,
144 DRAW_JIT_TEXTURE_WIDTH);
145 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture, height,
146 target, texture_type,
147 DRAW_JIT_TEXTURE_HEIGHT);
148 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture, depth,
149 target, texture_type,
150 DRAW_JIT_TEXTURE_DEPTH);
151 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture, first_level,
152 target, texture_type,
153 DRAW_JIT_TEXTURE_FIRST_LEVEL);
154 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture, last_level,
155 target, texture_type,
156 DRAW_JIT_TEXTURE_LAST_LEVEL);
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, data,
164 target, texture_type,
165 DRAW_JIT_TEXTURE_DATA);
166 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture, min_lod,
167 target, texture_type,
168 DRAW_JIT_TEXTURE_MIN_LOD);
169 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture, max_lod,
170 target, texture_type,
171 DRAW_JIT_TEXTURE_MAX_LOD);
172 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture, lod_bias,
173 target, texture_type,
174 DRAW_JIT_TEXTURE_LOD_BIAS);
175 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture, border_color,
176 target, texture_type,
177 DRAW_JIT_TEXTURE_BORDER_COLOR);
178
179 LP_CHECK_STRUCT_SIZE(struct draw_jit_texture, target, texture_type);
180
181 return texture_type;
182 }
183
184
185 /**
186 * Create LLVM type for struct draw_jit_texture
187 */
188 static LLVMTypeRef
189 create_jit_context_type(struct gallivm_state *gallivm,
190 LLVMTypeRef texture_type, const char *struct_name)
191 {
192 LLVMTargetDataRef target = gallivm->target;
193 LLVMTypeRef float_type = LLVMFloatTypeInContext(gallivm->context);
194 LLVMTypeRef elem_types[5];
195 LLVMTypeRef context_type;
196
197 elem_types[0] = LLVMPointerType(float_type, 0); /* vs_constants */
198 elem_types[1] = LLVMPointerType(float_type, 0); /* gs_constants */
199 elem_types[2] = LLVMPointerType(LLVMArrayType(LLVMArrayType(float_type, 4),
200 DRAW_TOTAL_CLIP_PLANES), 0);
201 elem_types[3] = LLVMPointerType(float_type, 0); /* viewport */
202 elem_types[4] = LLVMArrayType(texture_type,
203 PIPE_MAX_VERTEX_SAMPLERS); /* textures */
204 #if HAVE_LLVM >= 0x0300
205 context_type = LLVMStructCreateNamed(gallivm->context, struct_name);
206 LLVMStructSetBody(context_type, elem_types,
207 Elements(elem_types), 0);
208 #else
209 context_type = LLVMStructTypeInContext(gallivm->context, elem_types,
210 Elements(elem_types), 0);
211 LLVMAddTypeName(gallivm->module, struct_name, context_type);
212
213 LLVMInvalidateStructLayout(gallivm->target, context_type);
214 #endif
215
216 LP_CHECK_MEMBER_OFFSET(struct draw_jit_context, vs_constants,
217 target, context_type, 0);
218 LP_CHECK_MEMBER_OFFSET(struct draw_jit_context, gs_constants,
219 target, context_type, 1);
220 LP_CHECK_MEMBER_OFFSET(struct draw_jit_context, planes,
221 target, context_type, 2);
222 LP_CHECK_MEMBER_OFFSET(struct draw_jit_context, textures,
223 target, context_type,
224 DRAW_JIT_CTX_TEXTURES);
225 LP_CHECK_STRUCT_SIZE(struct draw_jit_context,
226 target, context_type);
227
228 return context_type;
229 }
230
231
232 /**
233 * Create LLVM type for struct pipe_vertex_buffer
234 */
235 static LLVMTypeRef
236 create_jit_vertex_buffer_type(struct gallivm_state *gallivm, const char *struct_name)
237 {
238 LLVMTargetDataRef target = gallivm->target;
239 LLVMTypeRef elem_types[3];
240 LLVMTypeRef vb_type;
241
242 elem_types[0] =
243 elem_types[1] = LLVMInt32TypeInContext(gallivm->context);
244 elem_types[2] = LLVMPointerType(LLVMInt8TypeInContext(gallivm->context), 0); /* vs_constants */
245
246 #if HAVE_LLVM >= 0x0300
247 vb_type = LLVMStructCreateNamed(gallivm->context, struct_name);
248 LLVMStructSetBody(vb_type, elem_types,
249 Elements(elem_types), 0);
250 #else
251 vb_type = LLVMStructTypeInContext(gallivm->context, elem_types,
252 Elements(elem_types), 0);
253 LLVMAddTypeName(gallivm->module, struct_name, vb_type);
254
255 LLVMInvalidateStructLayout(gallivm->target, vb_type);
256 #endif
257
258 LP_CHECK_MEMBER_OFFSET(struct pipe_vertex_buffer, stride,
259 target, vb_type, 0);
260 LP_CHECK_MEMBER_OFFSET(struct pipe_vertex_buffer, buffer_offset,
261 target, vb_type, 1);
262
263 LP_CHECK_STRUCT_SIZE(struct pipe_vertex_buffer, target, vb_type);
264
265 return vb_type;
266 }
267
268
269 /**
270 * Create LLVM type for struct vertex_header;
271 */
272 static LLVMTypeRef
273 create_jit_vertex_header(struct gallivm_state *gallivm, int data_elems)
274 {
275 LLVMTargetDataRef target = gallivm->target;
276 LLVMTypeRef elem_types[4];
277 LLVMTypeRef vertex_header;
278 char struct_name[24];
279
280 util_snprintf(struct_name, 23, "vertex_header%d", data_elems);
281
282 elem_types[DRAW_JIT_VERTEX_VERTEX_ID] = LLVMIntTypeInContext(gallivm->context, 32);
283 elem_types[DRAW_JIT_VERTEX_CLIP] = LLVMArrayType(LLVMFloatTypeInContext(gallivm->context), 4);
284 elem_types[DRAW_JIT_VERTEX_PRE_CLIP_POS] = LLVMArrayType(LLVMFloatTypeInContext(gallivm->context), 4);
285 elem_types[DRAW_JIT_VERTEX_DATA] = LLVMArrayType(elem_types[1], data_elems);
286
287 #if HAVE_LLVM >= 0x0300
288 vertex_header = LLVMStructCreateNamed(gallivm->context, struct_name);
289 LLVMStructSetBody(vertex_header, elem_types,
290 Elements(elem_types), 0);
291 #else
292 vertex_header = LLVMStructTypeInContext(gallivm->context, elem_types,
293 Elements(elem_types), 0);
294 LLVMAddTypeName(gallivm->module, struct_name, vertex_header);
295
296 LLVMInvalidateStructLayout(gallivm->target, vertex_header);
297 #endif
298
299 /* these are bit-fields and we can't take address of them
300 LP_CHECK_MEMBER_OFFSET(struct vertex_header, clipmask,
301 target, vertex_header,
302 DRAW_JIT_VERTEX_CLIPMASK);
303 LP_CHECK_MEMBER_OFFSET(struct vertex_header, edgeflag,
304 target, vertex_header,
305 DRAW_JIT_VERTEX_EDGEFLAG);
306 LP_CHECK_MEMBER_OFFSET(struct vertex_header, pad,
307 target, vertex_header,
308 DRAW_JIT_VERTEX_PAD);
309 LP_CHECK_MEMBER_OFFSET(struct vertex_header, vertex_id,
310 target, vertex_header,
311 DRAW_JIT_VERTEX_VERTEX_ID);
312 */
313 LP_CHECK_MEMBER_OFFSET(struct vertex_header, clip,
314 target, vertex_header,
315 DRAW_JIT_VERTEX_CLIP);
316 LP_CHECK_MEMBER_OFFSET(struct vertex_header, pre_clip_pos,
317 target, vertex_header,
318 DRAW_JIT_VERTEX_PRE_CLIP_POS);
319 LP_CHECK_MEMBER_OFFSET(struct vertex_header, data,
320 target, vertex_header,
321 DRAW_JIT_VERTEX_DATA);
322
323 return vertex_header;
324 }
325
326
327 /**
328 * Create LLVM types for various structures.
329 */
330 static void
331 create_jit_types(struct draw_llvm *llvm)
332 {
333 struct gallivm_state *gallivm = llvm->gallivm;
334 LLVMTypeRef texture_type, context_type, buffer_type, vb_type;
335
336 texture_type = create_jit_texture_type(gallivm, "texture");
337
338 context_type = create_jit_context_type(gallivm, texture_type, "draw_jit_context");
339 llvm->context_ptr_type = LLVMPointerType(context_type, 0);
340
341 buffer_type = LLVMPointerType(LLVMIntTypeInContext(gallivm->context, 8), 0);
342 llvm->buffer_ptr_type = LLVMPointerType(buffer_type, 0);
343
344 vb_type = create_jit_vertex_buffer_type(gallivm, "pipe_vertex_buffer");
345 llvm->vb_ptr_type = LLVMPointerType(vb_type, 0);
346 }
347
348
349 static LLVMTypeRef
350 get_context_ptr_type(struct draw_llvm *llvm)
351 {
352 if (!llvm->context_ptr_type)
353 create_jit_types(llvm);
354 return llvm->context_ptr_type;
355 }
356
357
358 static LLVMTypeRef
359 get_buffer_ptr_type(struct draw_llvm *llvm)
360 {
361 if (!llvm->buffer_ptr_type)
362 create_jit_types(llvm);
363 return llvm->buffer_ptr_type;
364 }
365
366
367 static LLVMTypeRef
368 get_vb_ptr_type(struct draw_llvm *llvm)
369 {
370 if (!llvm->vb_ptr_type)
371 create_jit_types(llvm);
372 return llvm->vb_ptr_type;
373 }
374
375 static LLVMTypeRef
376 get_vertex_header_ptr_type(struct draw_llvm *llvm)
377 {
378 if (!llvm->vertex_header_ptr_type)
379 create_jit_types(llvm);
380 return llvm->vertex_header_ptr_type;
381 }
382
383
384 /**
385 * Create per-context LLVM info.
386 */
387 struct draw_llvm *
388 draw_llvm_create(struct draw_context *draw, struct gallivm_state *gallivm)
389 {
390 struct draw_llvm *llvm;
391
392 llvm = CALLOC_STRUCT( draw_llvm );
393 if (!llvm)
394 return NULL;
395
396 lp_build_init();
397
398 llvm->draw = draw;
399 llvm->gallivm = gallivm;
400
401 if (gallivm_debug & GALLIVM_DEBUG_IR) {
402 LLVMDumpModule(llvm->gallivm->module);
403 }
404
405 llvm->nr_variants = 0;
406 make_empty_list(&llvm->vs_variants_list);
407
408 gallivm_register_garbage_collector_callback(
409 draw_llvm_garbage_collect_callback, llvm);
410
411 return llvm;
412 }
413
414
415 /**
416 * Free per-context LLVM info.
417 */
418 void
419 draw_llvm_destroy(struct draw_llvm *llvm)
420 {
421 gallivm_remove_garbage_collector_callback(
422 draw_llvm_garbage_collect_callback, llvm);
423
424 /* XXX free other draw_llvm data? */
425 FREE(llvm);
426 }
427
428
429 /**
430 * Create LLVM-generated code for a vertex shader.
431 */
432 struct draw_llvm_variant *
433 draw_llvm_create_variant(struct draw_llvm *llvm,
434 unsigned num_inputs,
435 const struct draw_llvm_variant_key *key)
436 {
437 struct draw_llvm_variant *variant;
438 struct llvm_vertex_shader *shader =
439 llvm_vertex_shader(llvm->draw->vs.vertex_shader);
440 LLVMTypeRef vertex_header;
441
442 variant = MALLOC(sizeof *variant +
443 shader->variant_key_size -
444 sizeof variant->key);
445 if (variant == NULL)
446 return NULL;
447
448 variant->llvm = llvm;
449
450 memcpy(&variant->key, key, shader->variant_key_size);
451
452 vertex_header = create_jit_vertex_header(llvm->gallivm, num_inputs);
453
454 llvm->vertex_header_ptr_type = LLVMPointerType(vertex_header, 0);
455
456 draw_llvm_generate(llvm, variant, FALSE); /* linear */
457 draw_llvm_generate(llvm, variant, TRUE); /* elts */
458
459 variant->shader = shader;
460 variant->list_item_global.base = variant;
461 variant->list_item_local.base = variant;
462 /*variant->no = */shader->variants_created++;
463 variant->list_item_global.base = variant;
464
465 return variant;
466 }
467
468
469 static void
470 generate_vs(struct draw_llvm *llvm,
471 LLVMBuilderRef builder,
472 LLVMValueRef (*outputs)[TGSI_NUM_CHANNELS],
473 const LLVMValueRef (*inputs)[TGSI_NUM_CHANNELS],
474 LLVMValueRef system_values_array,
475 LLVMValueRef context_ptr,
476 struct lp_build_sampler_soa *draw_sampler,
477 boolean clamp_vertex_color)
478 {
479 const struct tgsi_token *tokens = llvm->draw->vs.vertex_shader->state.tokens;
480 struct lp_type vs_type;
481 LLVMValueRef consts_ptr = draw_jit_context_vs_constants(llvm->gallivm, context_ptr);
482 struct lp_build_sampler_soa *sampler = 0;
483
484 memset(&vs_type, 0, sizeof vs_type);
485 vs_type.floating = TRUE; /* floating point values */
486 vs_type.sign = TRUE; /* values are signed */
487 vs_type.norm = FALSE; /* values are not limited to [0,1] or [-1,1] */
488 vs_type.width = 32; /* 32-bit float */
489 vs_type.length = 4; /* 4 elements per vector */
490 #if 0
491 num_vs = 4; /* number of vertices per block */
492 #endif
493
494 if (gallivm_debug & GALLIVM_DEBUG_IR) {
495 tgsi_dump(tokens, 0);
496 }
497
498 if (llvm->draw->num_sampler_views && llvm->draw->num_samplers)
499 sampler = draw_sampler;
500
501 lp_build_tgsi_soa(llvm->gallivm,
502 tokens,
503 vs_type,
504 NULL /*struct lp_build_mask_context *mask*/,
505 consts_ptr,
506 system_values_array,
507 NULL /*pos*/,
508 inputs,
509 outputs,
510 sampler,
511 &llvm->draw->vs.vertex_shader->info);
512
513 {
514 LLVMValueRef out;
515 unsigned chan, attrib;
516 struct lp_build_context bld;
517 struct tgsi_shader_info* info = &llvm->draw->vs.vertex_shader->info;
518 lp_build_context_init(&bld, llvm->gallivm, vs_type);
519
520 for (attrib = 0; attrib < info->num_outputs; ++attrib) {
521 for (chan = 0; chan < TGSI_NUM_CHANNELS; ++chan) {
522 if (outputs[attrib][chan]) {
523 switch (info->output_semantic_name[attrib]) {
524 case TGSI_SEMANTIC_COLOR:
525 case TGSI_SEMANTIC_BCOLOR:
526 if (clamp_vertex_color) {
527 out = LLVMBuildLoad(builder, outputs[attrib][chan], "");
528 out = lp_build_clamp(&bld, out, bld.zero, bld.one);
529 LLVMBuildStore(builder, out, outputs[attrib][chan]);
530 }
531 break;
532 case TGSI_SEMANTIC_FOG:
533 if (chan == 1 || chan == 2)
534 LLVMBuildStore(builder, bld.zero, outputs[attrib][chan]);
535 else if (chan == 3)
536 LLVMBuildStore(builder, bld.one, outputs[attrib][chan]);
537 break;
538 }
539 }
540 }
541 }
542 }
543 }
544
545
546 #if DEBUG_STORE
547 static void print_vectorf(LLVMBuilderRef builder,
548 LLVMValueRef vec)
549 {
550 LLVMValueRef val[4];
551 val[0] = LLVMBuildExtractElement(builder, vec,
552 lp_build_const_int32(gallivm, 0), "");
553 val[1] = LLVMBuildExtractElement(builder, vec,
554 lp_build_const_int32(gallivm, 1), "");
555 val[2] = LLVMBuildExtractElement(builder, vec,
556 lp_build_const_int32(gallivm, 2), "");
557 val[3] = LLVMBuildExtractElement(builder, vec,
558 lp_build_const_int32(gallivm, 3), "");
559 lp_build_printf(builder, "vector = [%f, %f, %f, %f]\n",
560 val[0], val[1], val[2], val[3]);
561 }
562 #endif
563
564
565 static void
566 generate_fetch(struct gallivm_state *gallivm,
567 LLVMValueRef vbuffers_ptr,
568 LLVMValueRef *res,
569 struct pipe_vertex_element *velem,
570 LLVMValueRef vbuf,
571 LLVMValueRef index,
572 LLVMValueRef instance_id)
573 {
574 LLVMBuilderRef builder = gallivm->builder;
575 LLVMValueRef indices =
576 LLVMConstInt(LLVMInt64TypeInContext(gallivm->context),
577 velem->vertex_buffer_index, 0);
578 LLVMValueRef vbuffer_ptr = LLVMBuildGEP(builder, vbuffers_ptr,
579 &indices, 1, "");
580 LLVMValueRef vb_stride = draw_jit_vbuffer_stride(gallivm, vbuf);
581 LLVMValueRef vb_buffer_offset = draw_jit_vbuffer_offset(gallivm, vbuf);
582 LLVMValueRef stride;
583
584 if (velem->instance_divisor) {
585 /* array index = instance_id / instance_divisor */
586 index = LLVMBuildUDiv(builder, instance_id,
587 lp_build_const_int32(gallivm, velem->instance_divisor),
588 "instance_divisor");
589 }
590
591 stride = LLVMBuildMul(builder, vb_stride, index, "");
592
593 vbuffer_ptr = LLVMBuildLoad(builder, vbuffer_ptr, "vbuffer");
594
595 stride = LLVMBuildAdd(builder, stride,
596 vb_buffer_offset,
597 "");
598 stride = LLVMBuildAdd(builder, stride,
599 lp_build_const_int32(gallivm, velem->src_offset),
600 "");
601
602 /*lp_build_printf(builder, "vbuf index = %d, stride is %d\n", indices, stride);*/
603 vbuffer_ptr = LLVMBuildGEP(builder, vbuffer_ptr, &stride, 1, "");
604
605 *res = draw_llvm_translate_from(gallivm, vbuffer_ptr, velem->src_format);
606 }
607
608
609 static LLVMValueRef
610 aos_to_soa(struct gallivm_state *gallivm,
611 LLVMValueRef val0,
612 LLVMValueRef val1,
613 LLVMValueRef val2,
614 LLVMValueRef val3,
615 LLVMValueRef channel)
616 {
617 LLVMBuilderRef builder = gallivm->builder;
618 LLVMValueRef ex, res;
619
620 ex = LLVMBuildExtractElement(builder, val0,
621 channel, "");
622 res = LLVMBuildInsertElement(builder,
623 LLVMConstNull(LLVMTypeOf(val0)),
624 ex,
625 lp_build_const_int32(gallivm, 0),
626 "");
627
628 ex = LLVMBuildExtractElement(builder, val1,
629 channel, "");
630 res = LLVMBuildInsertElement(builder,
631 res, ex,
632 lp_build_const_int32(gallivm, 1),
633 "");
634
635 ex = LLVMBuildExtractElement(builder, val2,
636 channel, "");
637 res = LLVMBuildInsertElement(builder,
638 res, ex,
639 lp_build_const_int32(gallivm, 2),
640 "");
641
642 ex = LLVMBuildExtractElement(builder, val3,
643 channel, "");
644 res = LLVMBuildInsertElement(builder,
645 res, ex,
646 lp_build_const_int32(gallivm, 3),
647 "");
648
649 return res;
650 }
651
652
653 static void
654 soa_to_aos(struct gallivm_state *gallivm,
655 LLVMValueRef soa[TGSI_NUM_CHANNELS],
656 LLVMValueRef aos[TGSI_NUM_CHANNELS])
657 {
658 LLVMBuilderRef builder = gallivm->builder;
659 LLVMValueRef comp;
660 int i = 0;
661
662 debug_assert(TGSI_NUM_CHANNELS == 4);
663
664 aos[0] = LLVMConstNull(LLVMTypeOf(soa[0]));
665 aos[1] = aos[2] = aos[3] = aos[0];
666
667 for (i = 0; i < TGSI_NUM_CHANNELS; ++i) {
668 LLVMValueRef channel = lp_build_const_int32(gallivm, i);
669
670 comp = LLVMBuildExtractElement(builder, soa[i],
671 lp_build_const_int32(gallivm, 0), "");
672 aos[0] = LLVMBuildInsertElement(builder, aos[0], comp, channel, "");
673
674 comp = LLVMBuildExtractElement(builder, soa[i],
675 lp_build_const_int32(gallivm, 1), "");
676 aos[1] = LLVMBuildInsertElement(builder, aos[1], comp, channel, "");
677
678 comp = LLVMBuildExtractElement(builder, soa[i],
679 lp_build_const_int32(gallivm, 2), "");
680 aos[2] = LLVMBuildInsertElement(builder, aos[2], comp, channel, "");
681
682 comp = LLVMBuildExtractElement(builder, soa[i],
683 lp_build_const_int32(gallivm, 3), "");
684 aos[3] = LLVMBuildInsertElement(builder, aos[3], comp, channel, "");
685
686 }
687 }
688
689
690 static void
691 convert_to_soa(struct gallivm_state *gallivm,
692 LLVMValueRef (*aos)[TGSI_NUM_CHANNELS],
693 LLVMValueRef (*soa)[TGSI_NUM_CHANNELS],
694 int num_attribs)
695 {
696 int i;
697
698 debug_assert(TGSI_NUM_CHANNELS == 4);
699
700 for (i = 0; i < num_attribs; ++i) {
701 LLVMValueRef val0 = aos[i][0];
702 LLVMValueRef val1 = aos[i][1];
703 LLVMValueRef val2 = aos[i][2];
704 LLVMValueRef val3 = aos[i][3];
705
706 soa[i][0] = aos_to_soa(gallivm, val0, val1, val2, val3,
707 lp_build_const_int32(gallivm, 0));
708 soa[i][1] = aos_to_soa(gallivm, val0, val1, val2, val3,
709 lp_build_const_int32(gallivm, 1));
710 soa[i][2] = aos_to_soa(gallivm, val0, val1, val2, val3,
711 lp_build_const_int32(gallivm, 2));
712 soa[i][3] = aos_to_soa(gallivm, val0, val1, val2, val3,
713 lp_build_const_int32(gallivm, 3));
714 }
715 }
716
717
718 static void
719 store_aos(struct gallivm_state *gallivm,
720 LLVMValueRef io_ptr,
721 LLVMValueRef index,
722 LLVMValueRef value,
723 LLVMValueRef clipmask, boolean have_clipdist)
724 {
725 LLVMBuilderRef builder = gallivm->builder;
726 LLVMValueRef id_ptr = draw_jit_header_id(gallivm, io_ptr);
727 LLVMValueRef data_ptr = draw_jit_header_data(gallivm, io_ptr);
728 LLVMValueRef indices[3];
729 LLVMValueRef val;
730 int vertex_id_pad_edgeflag;
731
732 indices[0] = lp_build_const_int32(gallivm, 0);
733 indices[1] = index;
734 indices[2] = lp_build_const_int32(gallivm, 0);
735
736 /* If this assertion fails, it means we need to update the bit twidding
737 * code here. See struct vertex_header in draw_private.h.
738 */
739 assert(DRAW_TOTAL_CLIP_PLANES==14);
740 /* initialize vertex id:16 = 0xffff, have_clipdist:1 = 0, edgeflag:1 = 1 */
741 vertex_id_pad_edgeflag = (0xffff << 16) | (1 << DRAW_TOTAL_CLIP_PLANES);
742 if (have_clipdist)
743 vertex_id_pad_edgeflag |= 1 << (DRAW_TOTAL_CLIP_PLANES+1);
744 val = lp_build_const_int32(gallivm, vertex_id_pad_edgeflag);
745 /* OR with the clipmask */
746 val = LLVMBuildOr(builder, val, clipmask, "");
747
748 /* store vertex header */
749 LLVMBuildStore(builder, val, id_ptr);
750
751
752 #if DEBUG_STORE
753 lp_build_printf(builder, " ---- %p storing attribute %d (io = %p)\n", data_ptr, index, io_ptr);
754 #endif
755 #if 0
756 /*lp_build_printf(builder, " ---- %p storing at %d (%p) ", io_ptr, index, data_ptr);
757 print_vectorf(builder, value);*/
758 data_ptr = LLVMBuildBitCast(builder, data_ptr,
759 LLVMPointerType(LLVMArrayType(LLVMVectorType(LLVMFloatTypeInContext(gallivm->context), 4), 0), 0),
760 "datavec");
761 data_ptr = LLVMBuildGEP(builder, data_ptr, indices, 2, "");
762
763 LLVMBuildStore(builder, value, data_ptr);
764 #else
765 {
766 LLVMValueRef x, y, z, w;
767 LLVMValueRef idx0, idx1, idx2, idx3;
768 LLVMValueRef gep0, gep1, gep2, gep3;
769 data_ptr = LLVMBuildGEP(builder, data_ptr, indices, 3, "");
770
771 idx0 = lp_build_const_int32(gallivm, 0);
772 idx1 = lp_build_const_int32(gallivm, 1);
773 idx2 = lp_build_const_int32(gallivm, 2);
774 idx3 = lp_build_const_int32(gallivm, 3);
775
776 x = LLVMBuildExtractElement(builder, value,
777 idx0, "");
778 y = LLVMBuildExtractElement(builder, value,
779 idx1, "");
780 z = LLVMBuildExtractElement(builder, value,
781 idx2, "");
782 w = LLVMBuildExtractElement(builder, value,
783 idx3, "");
784
785 gep0 = LLVMBuildGEP(builder, data_ptr, &idx0, 1, "");
786 gep1 = LLVMBuildGEP(builder, data_ptr, &idx1, 1, "");
787 gep2 = LLVMBuildGEP(builder, data_ptr, &idx2, 1, "");
788 gep3 = LLVMBuildGEP(builder, data_ptr, &idx3, 1, "");
789
790 /*lp_build_printf(builder, "##### x = %f (%p), y = %f (%p), z = %f (%p), w = %f (%p)\n",
791 x, gep0, y, gep1, z, gep2, w, gep3);*/
792 LLVMBuildStore(builder, x, gep0);
793 LLVMBuildStore(builder, y, gep1);
794 LLVMBuildStore(builder, z, gep2);
795 LLVMBuildStore(builder, w, gep3);
796 }
797 #endif
798 }
799
800
801 static void
802 store_aos_array(struct gallivm_state *gallivm,
803 LLVMValueRef io_ptr,
804 LLVMValueRef aos[TGSI_NUM_CHANNELS],
805 int attrib,
806 int num_outputs,
807 LLVMValueRef clipmask,
808 boolean have_clipdist)
809 {
810 LLVMBuilderRef builder = gallivm->builder;
811 LLVMValueRef attr_index = lp_build_const_int32(gallivm, attrib);
812 LLVMValueRef ind0 = lp_build_const_int32(gallivm, 0);
813 LLVMValueRef ind1 = lp_build_const_int32(gallivm, 1);
814 LLVMValueRef ind2 = lp_build_const_int32(gallivm, 2);
815 LLVMValueRef ind3 = lp_build_const_int32(gallivm, 3);
816 LLVMValueRef io0_ptr, io1_ptr, io2_ptr, io3_ptr;
817 LLVMValueRef clipmask0, clipmask1, clipmask2, clipmask3;
818
819 debug_assert(TGSI_NUM_CHANNELS == 4);
820
821 io0_ptr = LLVMBuildGEP(builder, io_ptr,
822 &ind0, 1, "");
823 io1_ptr = LLVMBuildGEP(builder, io_ptr,
824 &ind1, 1, "");
825 io2_ptr = LLVMBuildGEP(builder, io_ptr,
826 &ind2, 1, "");
827 io3_ptr = LLVMBuildGEP(builder, io_ptr,
828 &ind3, 1, "");
829
830 clipmask0 = LLVMBuildExtractElement(builder, clipmask,
831 ind0, "");
832 clipmask1 = LLVMBuildExtractElement(builder, clipmask,
833 ind1, "");
834 clipmask2 = LLVMBuildExtractElement(builder, clipmask,
835 ind2, "");
836 clipmask3 = LLVMBuildExtractElement(builder, clipmask,
837 ind3, "");
838
839 #if DEBUG_STORE
840 lp_build_printf(builder, "io = %p, indexes[%d, %d, %d, %d]\n, clipmask0 = %x, clipmask1 = %x, clipmask2 = %x, clipmask3 = %x\n",
841 io_ptr, ind0, ind1, ind2, ind3, clipmask0, clipmask1, clipmask2, clipmask3);
842 #endif
843 /* store for each of the 4 vertices */
844 store_aos(gallivm, io0_ptr, attr_index, aos[0], clipmask0, have_clipdist);
845 store_aos(gallivm, io1_ptr, attr_index, aos[1], clipmask1, have_clipdist);
846 store_aos(gallivm, io2_ptr, attr_index, aos[2], clipmask2, have_clipdist);
847 store_aos(gallivm, io3_ptr, attr_index, aos[3], clipmask3, have_clipdist);
848 }
849
850
851 static void
852 convert_to_aos(struct gallivm_state *gallivm,
853 LLVMValueRef io,
854 LLVMValueRef (*outputs)[TGSI_NUM_CHANNELS],
855 LLVMValueRef clipmask,
856 int num_outputs,
857 int max_vertices, boolean have_clipdist)
858 {
859 LLVMBuilderRef builder = gallivm->builder;
860 unsigned chan, attrib;
861
862 #if DEBUG_STORE
863 lp_build_printf(builder, " # storing begin\n");
864 #endif
865 for (attrib = 0; attrib < num_outputs; ++attrib) {
866 LLVMValueRef soa[4];
867 LLVMValueRef aos[4];
868 for (chan = 0; chan < TGSI_NUM_CHANNELS; ++chan) {
869 if (outputs[attrib][chan]) {
870 LLVMValueRef out = LLVMBuildLoad(builder, outputs[attrib][chan], "");
871 lp_build_name(out, "output%u.%c", attrib, "xyzw"[chan]);
872 /*lp_build_printf(builder, "output %d : %d ",
873 LLVMConstInt(LLVMInt32Type(), attrib, 0),
874 LLVMConstInt(LLVMInt32Type(), chan, 0));
875 print_vectorf(builder, out);*/
876 soa[chan] = out;
877 }
878 else {
879 soa[chan] = 0;
880 }
881 }
882 soa_to_aos(gallivm, soa, aos);
883 store_aos_array(gallivm,
884 io,
885 aos,
886 attrib,
887 num_outputs,
888 clipmask, have_clipdist);
889 }
890 #if DEBUG_STORE
891 lp_build_printf(builder, " # storing end\n");
892 #endif
893 }
894
895
896 /**
897 * Stores original vertex positions in clip coordinates
898 * There is probably a more efficient way to do this, 4 floats at once
899 * rather than extracting each element one by one.
900 * idx is the output to store things too, if pre_clip_pos is set
901 * we store the pos to the idx, if not we store the clipvertex to it.
902 */
903 static void
904 store_clip(struct gallivm_state *gallivm,
905 LLVMValueRef io_ptr,
906 LLVMValueRef (*outputs)[TGSI_NUM_CHANNELS],
907 boolean pre_clip_pos, int idx)
908 {
909 LLVMBuilderRef builder = gallivm->builder;
910 LLVMValueRef out[4];
911 LLVMValueRef indices[2];
912 LLVMValueRef io0_ptr, io1_ptr, io2_ptr, io3_ptr;
913 LLVMValueRef clip_ptr0, clip_ptr1, clip_ptr2, clip_ptr3;
914 LLVMValueRef clip0_ptr, clip1_ptr, clip2_ptr, clip3_ptr;
915 LLVMValueRef out0elem, out1elem, out2elem, out3elem;
916 int i;
917
918 LLVMValueRef ind0 = lp_build_const_int32(gallivm, 0);
919 LLVMValueRef ind1 = lp_build_const_int32(gallivm, 1);
920 LLVMValueRef ind2 = lp_build_const_int32(gallivm, 2);
921 LLVMValueRef ind3 = lp_build_const_int32(gallivm, 3);
922
923 indices[0] =
924 indices[1] = lp_build_const_int32(gallivm, 0);
925
926 out[0] = LLVMBuildLoad(builder, outputs[idx][0], ""); /*x0 x1 x2 x3*/
927 out[1] = LLVMBuildLoad(builder, outputs[idx][1], ""); /*y0 y1 y2 y3*/
928 out[2] = LLVMBuildLoad(builder, outputs[idx][2], ""); /*z0 z1 z2 z3*/
929 out[3] = LLVMBuildLoad(builder, outputs[idx][3], ""); /*w0 w1 w2 w3*/
930
931 io0_ptr = LLVMBuildGEP(builder, io_ptr, &ind0, 1, "");
932 io1_ptr = LLVMBuildGEP(builder, io_ptr, &ind1, 1, "");
933 io2_ptr = LLVMBuildGEP(builder, io_ptr, &ind2, 1, "");
934 io3_ptr = LLVMBuildGEP(builder, io_ptr, &ind3, 1, "");
935
936 if (!pre_clip_pos) {
937 clip_ptr0 = draw_jit_header_clip(gallivm, io0_ptr);
938 clip_ptr1 = draw_jit_header_clip(gallivm, io1_ptr);
939 clip_ptr2 = draw_jit_header_clip(gallivm, io2_ptr);
940 clip_ptr3 = draw_jit_header_clip(gallivm, io3_ptr);
941 } else {
942 clip_ptr0 = draw_jit_header_pre_clip_pos(gallivm, io0_ptr);
943 clip_ptr1 = draw_jit_header_pre_clip_pos(gallivm, io1_ptr);
944 clip_ptr2 = draw_jit_header_pre_clip_pos(gallivm, io2_ptr);
945 clip_ptr3 = draw_jit_header_pre_clip_pos(gallivm, io3_ptr);
946 }
947
948 for (i = 0; i<4; i++) {
949 clip0_ptr = LLVMBuildGEP(builder, clip_ptr0, indices, 2, ""); /* x0 */
950 clip1_ptr = LLVMBuildGEP(builder, clip_ptr1, indices, 2, ""); /* x1 */
951 clip2_ptr = LLVMBuildGEP(builder, clip_ptr2, indices, 2, ""); /* x2 */
952 clip3_ptr = LLVMBuildGEP(builder, clip_ptr3, indices, 2, ""); /* x3 */
953
954 out0elem = LLVMBuildExtractElement(builder, out[i], ind0, ""); /* x0 */
955 out1elem = LLVMBuildExtractElement(builder, out[i], ind1, ""); /* x1 */
956 out2elem = LLVMBuildExtractElement(builder, out[i], ind2, ""); /* x2 */
957 out3elem = LLVMBuildExtractElement(builder, out[i], ind3, ""); /* x3 */
958
959 LLVMBuildStore(builder, out0elem, clip0_ptr);
960 LLVMBuildStore(builder, out1elem, clip1_ptr);
961 LLVMBuildStore(builder, out2elem, clip2_ptr);
962 LLVMBuildStore(builder, out3elem, clip3_ptr);
963
964 indices[1]= LLVMBuildAdd(builder, indices[1], ind1, "");
965 }
966
967 }
968
969
970 /**
971 * Equivalent of _mm_set1_ps(a)
972 */
973 static LLVMValueRef
974 vec4f_from_scalar(struct gallivm_state *gallivm,
975 LLVMValueRef a,
976 const char *name)
977 {
978 LLVMTypeRef float_type = LLVMFloatTypeInContext(gallivm->context);
979 LLVMValueRef res = LLVMGetUndef(LLVMVectorType(float_type, 4));
980 int i;
981
982 for (i = 0; i < 4; ++i) {
983 LLVMValueRef index = lp_build_const_int32(gallivm, i);
984 res = LLVMBuildInsertElement(gallivm->builder, res, a,
985 index, i == 3 ? name : "");
986 }
987
988 return res;
989 }
990
991
992 /**
993 * Transforms the outputs for viewport mapping
994 */
995 static void
996 generate_viewport(struct draw_llvm *llvm,
997 LLVMBuilderRef builder,
998 LLVMValueRef (*outputs)[TGSI_NUM_CHANNELS],
999 LLVMValueRef context_ptr)
1000 {
1001 int i;
1002 struct gallivm_state *gallivm = llvm->gallivm;
1003 struct lp_type f32_type = lp_type_float_vec(32);
1004 LLVMValueRef out3 = LLVMBuildLoad(builder, outputs[0][3], ""); /*w0 w1 w2 w3*/
1005 LLVMValueRef const1 = lp_build_const_vec(gallivm, f32_type, 1.0); /*1.0 1.0 1.0 1.0*/
1006 LLVMValueRef vp_ptr = draw_jit_context_viewport(gallivm, context_ptr);
1007
1008 /* for 1/w convention*/
1009 out3 = LLVMBuildFDiv(builder, const1, out3, "");
1010 LLVMBuildStore(builder, out3, outputs[0][3]);
1011
1012 /* Viewport Mapping */
1013 for (i=0; i<3; i++) {
1014 LLVMValueRef out = LLVMBuildLoad(builder, outputs[0][i], ""); /*x0 x1 x2 x3*/
1015 LLVMValueRef scale;
1016 LLVMValueRef trans;
1017 LLVMValueRef scale_i;
1018 LLVMValueRef trans_i;
1019 LLVMValueRef index;
1020
1021 index = lp_build_const_int32(gallivm, i);
1022 scale_i = LLVMBuildGEP(builder, vp_ptr, &index, 1, "");
1023
1024 index = lp_build_const_int32(gallivm, i+4);
1025 trans_i = LLVMBuildGEP(builder, vp_ptr, &index, 1, "");
1026
1027 scale = vec4f_from_scalar(gallivm, LLVMBuildLoad(builder, scale_i, ""), "scale");
1028 trans = vec4f_from_scalar(gallivm, LLVMBuildLoad(builder, trans_i, ""), "trans");
1029
1030 /* divide by w */
1031 out = LLVMBuildFMul(builder, out, out3, "");
1032 /* mult by scale */
1033 out = LLVMBuildFMul(builder, out, scale, "");
1034 /* add translation */
1035 out = LLVMBuildFAdd(builder, out, trans, "");
1036
1037 /* store transformed outputs */
1038 LLVMBuildStore(builder, out, outputs[0][i]);
1039 }
1040
1041 }
1042
1043
1044 /**
1045 * Returns clipmask as 4xi32 bitmask for the 4 vertices
1046 */
1047 static LLVMValueRef
1048 generate_clipmask(struct draw_llvm *llvm,
1049 LLVMValueRef (*outputs)[TGSI_NUM_CHANNELS],
1050 boolean clip_xy,
1051 boolean clip_z,
1052 boolean clip_user,
1053 boolean clip_halfz,
1054 unsigned ucp_enable,
1055 LLVMValueRef context_ptr,
1056 boolean *have_clipdist)
1057 {
1058 struct gallivm_state *gallivm = llvm->gallivm;
1059 LLVMBuilderRef builder = gallivm->builder;
1060 LLVMValueRef mask; /* stores the <4xi32> clipmasks */
1061 LLVMValueRef test, temp;
1062 LLVMValueRef zero, shift;
1063 LLVMValueRef pos_x, pos_y, pos_z, pos_w;
1064 LLVMValueRef cv_x, cv_y, cv_z, cv_w;
1065 LLVMValueRef plane1, planes, plane_ptr, sum;
1066 struct lp_type f32_type = lp_type_float_vec(32);
1067 const unsigned pos = draw_current_shader_position_output(llvm->draw);
1068 const unsigned cv = draw_current_shader_clipvertex_output(llvm->draw);
1069 int num_written_clipdistance = llvm->draw->vs.vertex_shader->info.num_written_clipdistance;
1070 bool have_cd = false;
1071 unsigned cd[2];
1072
1073 cd[0] = draw_current_shader_clipdistance_output(llvm->draw, 0);
1074 cd[1] = draw_current_shader_clipdistance_output(llvm->draw, 1);
1075
1076 if (cd[0] != pos || cd[1] != pos)
1077 have_cd = true;
1078
1079 mask = lp_build_const_int_vec(gallivm, lp_type_int_vec(32), 0);
1080 temp = lp_build_const_int_vec(gallivm, lp_type_int_vec(32), 0);
1081 zero = lp_build_const_vec(gallivm, f32_type, 0); /* 0.0f 0.0f 0.0f 0.0f */
1082 shift = lp_build_const_int_vec(gallivm, lp_type_int_vec(32), 1); /* 1 1 1 1 */
1083
1084 /*
1085 * load clipvertex and position from correct locations.
1086 * if they are the same just load them once.
1087 */
1088 pos_x = LLVMBuildLoad(builder, outputs[pos][0], ""); /*x0 x1 x2 x3*/
1089 pos_y = LLVMBuildLoad(builder, outputs[pos][1], ""); /*y0 y1 y2 y3*/
1090 pos_z = LLVMBuildLoad(builder, outputs[pos][2], ""); /*z0 z1 z2 z3*/
1091 pos_w = LLVMBuildLoad(builder, outputs[pos][3], ""); /*w0 w1 w2 w3*/
1092
1093 if (clip_user && cv != pos) {
1094 cv_x = LLVMBuildLoad(builder, outputs[cv][0], ""); /*x0 x1 x2 x3*/
1095 cv_y = LLVMBuildLoad(builder, outputs[cv][1], ""); /*y0 y1 y2 y3*/
1096 cv_z = LLVMBuildLoad(builder, outputs[cv][2], ""); /*z0 z1 z2 z3*/
1097 cv_w = LLVMBuildLoad(builder, outputs[cv][3], ""); /*w0 w1 w2 w3*/
1098 } else {
1099 cv_x = pos_x;
1100 cv_y = pos_y;
1101 cv_z = pos_z;
1102 cv_w = pos_w;
1103 }
1104
1105 /* Cliptest, for hardwired planes */
1106 if (clip_xy) {
1107 /* plane 1 */
1108 test = lp_build_compare(gallivm, f32_type, PIPE_FUNC_GREATER, pos_x , pos_w);
1109 temp = shift;
1110 test = LLVMBuildAnd(builder, test, temp, "");
1111 mask = test;
1112
1113 /* plane 2 */
1114 test = LLVMBuildFAdd(builder, pos_x, pos_w, "");
1115 test = lp_build_compare(gallivm, f32_type, PIPE_FUNC_GREATER, zero, test);
1116 temp = LLVMBuildShl(builder, temp, shift, "");
1117 test = LLVMBuildAnd(builder, test, temp, "");
1118 mask = LLVMBuildOr(builder, mask, test, "");
1119
1120 /* plane 3 */
1121 test = lp_build_compare(gallivm, f32_type, PIPE_FUNC_GREATER, pos_y, pos_w);
1122 temp = LLVMBuildShl(builder, temp, shift, "");
1123 test = LLVMBuildAnd(builder, test, temp, "");
1124 mask = LLVMBuildOr(builder, mask, test, "");
1125
1126 /* plane 4 */
1127 test = LLVMBuildFAdd(builder, pos_y, pos_w, "");
1128 test = lp_build_compare(gallivm, f32_type, PIPE_FUNC_GREATER, zero, test);
1129 temp = LLVMBuildShl(builder, temp, shift, "");
1130 test = LLVMBuildAnd(builder, test, temp, "");
1131 mask = LLVMBuildOr(builder, mask, test, "");
1132 }
1133
1134 if (clip_z) {
1135 temp = lp_build_const_int_vec(gallivm, lp_type_int_vec(32), 16);
1136 if (clip_halfz) {
1137 /* plane 5 */
1138 test = lp_build_compare(gallivm, f32_type, PIPE_FUNC_GREATER, zero, pos_z);
1139 test = LLVMBuildAnd(builder, test, temp, "");
1140 mask = LLVMBuildOr(builder, mask, test, "");
1141 }
1142 else {
1143 /* plane 5 */
1144 test = LLVMBuildFAdd(builder, pos_z, pos_w, "");
1145 test = lp_build_compare(gallivm, f32_type, PIPE_FUNC_GREATER, zero, test);
1146 test = LLVMBuildAnd(builder, test, temp, "");
1147 mask = LLVMBuildOr(builder, mask, test, "");
1148 }
1149 /* plane 6 */
1150 test = lp_build_compare(gallivm, f32_type, PIPE_FUNC_GREATER, pos_z, pos_w);
1151 temp = LLVMBuildShl(builder, temp, shift, "");
1152 test = LLVMBuildAnd(builder, test, temp, "");
1153 mask = LLVMBuildOr(builder, mask, test, "");
1154 }
1155
1156 if (clip_user) {
1157 LLVMValueRef planes_ptr = draw_jit_context_planes(gallivm, context_ptr);
1158 LLVMValueRef indices[3];
1159
1160 /* userclip planes */
1161 while (ucp_enable) {
1162 unsigned plane_idx = ffs(ucp_enable)-1;
1163 ucp_enable &= ~(1 << plane_idx);
1164 plane_idx += 6;
1165
1166 if (have_cd && num_written_clipdistance) {
1167 LLVMValueRef clipdist;
1168 int i;
1169 i = plane_idx - 6;
1170
1171 *have_clipdist = TRUE;
1172 if (i < 4) {
1173 clipdist = LLVMBuildLoad(builder, outputs[cd[0]][i], "");
1174 } else {
1175 clipdist = LLVMBuildLoad(builder, outputs[cd[1]][i-4], "");
1176 }
1177 test = lp_build_compare(gallivm, f32_type, PIPE_FUNC_GREATER, zero, clipdist);
1178 temp = lp_build_const_int_vec(gallivm, lp_type_int_vec(32), 1 << plane_idx);
1179 test = LLVMBuildAnd(builder, test, temp, "");
1180 mask = LLVMBuildOr(builder, mask, test, "");
1181 } else {
1182 indices[0] = lp_build_const_int32(gallivm, 0);
1183 indices[1] = lp_build_const_int32(gallivm, plane_idx);
1184
1185 indices[2] = lp_build_const_int32(gallivm, 0);
1186 plane_ptr = LLVMBuildGEP(builder, planes_ptr, indices, 3, "");
1187 plane1 = LLVMBuildLoad(builder, plane_ptr, "plane_x");
1188 planes = vec4f_from_scalar(gallivm, plane1, "plane4_x");
1189 sum = LLVMBuildFMul(builder, planes, cv_x, "");
1190
1191 indices[2] = lp_build_const_int32(gallivm, 1);
1192 plane_ptr = LLVMBuildGEP(builder, planes_ptr, indices, 3, "");
1193 plane1 = LLVMBuildLoad(builder, plane_ptr, "plane_y");
1194 planes = vec4f_from_scalar(gallivm, plane1, "plane4_y");
1195 test = LLVMBuildFMul(builder, planes, cv_y, "");
1196 sum = LLVMBuildFAdd(builder, sum, test, "");
1197
1198 indices[2] = lp_build_const_int32(gallivm, 2);
1199 plane_ptr = LLVMBuildGEP(builder, planes_ptr, indices, 3, "");
1200 plane1 = LLVMBuildLoad(builder, plane_ptr, "plane_z");
1201 planes = vec4f_from_scalar(gallivm, plane1, "plane4_z");
1202 test = LLVMBuildFMul(builder, planes, cv_z, "");
1203 sum = LLVMBuildFAdd(builder, sum, test, "");
1204
1205 indices[2] = lp_build_const_int32(gallivm, 3);
1206 plane_ptr = LLVMBuildGEP(builder, planes_ptr, indices, 3, "");
1207 plane1 = LLVMBuildLoad(builder, plane_ptr, "plane_w");
1208 planes = vec4f_from_scalar(gallivm, plane1, "plane4_w");
1209 test = LLVMBuildFMul(builder, planes, cv_w, "");
1210 sum = LLVMBuildFAdd(builder, sum, test, "");
1211
1212 test = lp_build_compare(gallivm, f32_type, PIPE_FUNC_GREATER, zero, sum);
1213 temp = lp_build_const_int_vec(gallivm, lp_type_int_vec(32), 1 << plane_idx);
1214 test = LLVMBuildAnd(builder, test, temp, "");
1215 mask = LLVMBuildOr(builder, mask, test, "");
1216 }
1217 }
1218 }
1219 return mask;
1220 }
1221
1222
1223 /**
1224 * Returns boolean if any clipping has occurred
1225 * Used zero/non-zero i32 value to represent boolean
1226 */
1227 static void
1228 clipmask_bool(struct gallivm_state *gallivm,
1229 LLVMValueRef clipmask,
1230 LLVMValueRef ret_ptr)
1231 {
1232 LLVMBuilderRef builder = gallivm->builder;
1233 LLVMValueRef ret = LLVMBuildLoad(builder, ret_ptr, "");
1234 LLVMValueRef temp;
1235 int i;
1236
1237 for (i=0; i<4; i++) {
1238 temp = LLVMBuildExtractElement(builder, clipmask,
1239 lp_build_const_int32(gallivm, i) , "");
1240 ret = LLVMBuildOr(builder, ret, temp, "");
1241 }
1242
1243 LLVMBuildStore(builder, ret, ret_ptr);
1244 }
1245
1246
1247 static void
1248 draw_llvm_generate(struct draw_llvm *llvm, struct draw_llvm_variant *variant,
1249 boolean elts)
1250 {
1251 struct gallivm_state *gallivm = llvm->gallivm;
1252 LLVMContextRef context = gallivm->context;
1253 LLVMTypeRef int32_type = LLVMInt32TypeInContext(context);
1254 LLVMTypeRef arg_types[8];
1255 LLVMTypeRef func_type;
1256 LLVMValueRef context_ptr;
1257 LLVMBasicBlockRef block;
1258 LLVMBuilderRef builder;
1259 LLVMValueRef end, start;
1260 LLVMValueRef count, fetch_elts, fetch_count;
1261 LLVMValueRef stride, step, io_itr;
1262 LLVMValueRef io_ptr, vbuffers_ptr, vb_ptr;
1263 LLVMValueRef instance_id;
1264 LLVMValueRef system_values_array;
1265 LLVMValueRef zero = lp_build_const_int32(gallivm, 0);
1266 LLVMValueRef one = lp_build_const_int32(gallivm, 1);
1267 struct draw_context *draw = llvm->draw;
1268 const struct tgsi_shader_info *vs_info = &draw->vs.vertex_shader->info;
1269 unsigned i, j;
1270 struct lp_build_context bld;
1271 struct lp_build_loop_state lp_loop;
1272 const int max_vertices = 4;
1273 LLVMValueRef outputs[PIPE_MAX_SHADER_OUTPUTS][TGSI_NUM_CHANNELS];
1274 LLVMValueRef fetch_max;
1275 void *code;
1276 struct lp_build_sampler_soa *sampler = 0;
1277 LLVMValueRef ret, ret_ptr;
1278 const boolean bypass_viewport = variant->key.bypass_viewport;
1279 const boolean enable_cliptest = variant->key.clip_xy ||
1280 variant->key.clip_z ||
1281 variant->key.clip_user;
1282 LLVMValueRef variant_func;
1283 const unsigned pos = draw_current_shader_position_output(llvm->draw);
1284 const unsigned cv = draw_current_shader_clipvertex_output(llvm->draw);
1285 boolean have_clipdist = FALSE;
1286
1287 arg_types[0] = get_context_ptr_type(llvm); /* context */
1288 arg_types[1] = get_vertex_header_ptr_type(llvm); /* vertex_header */
1289 arg_types[2] = get_buffer_ptr_type(llvm); /* vbuffers */
1290 if (elts)
1291 arg_types[3] = LLVMPointerType(int32_type, 0);/* fetch_elts * */
1292 else
1293 arg_types[3] = int32_type; /* start */
1294 arg_types[4] = int32_type; /* fetch_count / count */
1295 arg_types[5] = int32_type; /* stride */
1296 arg_types[6] = get_vb_ptr_type(llvm); /* pipe_vertex_buffer's */
1297 arg_types[7] = int32_type; /* instance_id */
1298
1299 func_type = LLVMFunctionType(int32_type, arg_types, Elements(arg_types), 0);
1300
1301 variant_func = LLVMAddFunction(gallivm->module,
1302 elts ? "draw_llvm_shader_elts" : "draw_llvm_shader",
1303 func_type);
1304
1305 if (elts)
1306 variant->function_elts = variant_func;
1307 else
1308 variant->function = variant_func;
1309
1310 LLVMSetFunctionCallConv(variant_func, LLVMCCallConv);
1311 for (i = 0; i < Elements(arg_types); ++i)
1312 if (LLVMGetTypeKind(arg_types[i]) == LLVMPointerTypeKind)
1313 LLVMAddAttribute(LLVMGetParam(variant_func, i),
1314 LLVMNoAliasAttribute);
1315
1316 context_ptr = LLVMGetParam(variant_func, 0);
1317 io_ptr = LLVMGetParam(variant_func, 1);
1318 vbuffers_ptr = LLVMGetParam(variant_func, 2);
1319 stride = LLVMGetParam(variant_func, 5);
1320 vb_ptr = LLVMGetParam(variant_func, 6);
1321 instance_id = LLVMGetParam(variant_func, 7);
1322
1323 lp_build_name(context_ptr, "context");
1324 lp_build_name(io_ptr, "io");
1325 lp_build_name(vbuffers_ptr, "vbuffers");
1326 lp_build_name(stride, "stride");
1327 lp_build_name(vb_ptr, "vb");
1328 lp_build_name(instance_id, "instance_id");
1329
1330 if (elts) {
1331 fetch_elts = LLVMGetParam(variant_func, 3);
1332 fetch_count = LLVMGetParam(variant_func, 4);
1333 lp_build_name(fetch_elts, "fetch_elts");
1334 lp_build_name(fetch_count, "fetch_count");
1335 start = count = NULL;
1336 }
1337 else {
1338 start = LLVMGetParam(variant_func, 3);
1339 count = LLVMGetParam(variant_func, 4);
1340 lp_build_name(start, "start");
1341 lp_build_name(count, "count");
1342 fetch_elts = fetch_count = NULL;
1343 }
1344
1345 /*
1346 * Function body
1347 */
1348
1349 block = LLVMAppendBasicBlockInContext(gallivm->context, variant_func, "entry");
1350 builder = gallivm->builder;
1351 LLVMPositionBuilderAtEnd(builder, block);
1352
1353 lp_build_context_init(&bld, gallivm, lp_type_int(32));
1354
1355 system_values_array = lp_build_system_values_array(gallivm, vs_info,
1356 instance_id, NULL);
1357
1358 /* function will return non-zero i32 value if any clipped vertices */
1359 ret_ptr = lp_build_alloca(gallivm, int32_type, "");
1360 LLVMBuildStore(builder, zero, ret_ptr);
1361
1362 /* code generated texture sampling */
1363 sampler = draw_llvm_sampler_soa_create(
1364 draw_llvm_variant_key_samplers(&variant->key),
1365 context_ptr);
1366
1367 if (elts) {
1368 start = zero;
1369 end = fetch_count;
1370 }
1371 else {
1372 end = lp_build_add(&bld, start, count);
1373 }
1374
1375 step = lp_build_const_int32(gallivm, max_vertices);
1376
1377 fetch_max = LLVMBuildSub(builder, end, one, "fetch_max");
1378
1379 lp_build_loop_begin(&lp_loop, gallivm, start);
1380 {
1381 LLVMValueRef inputs[PIPE_MAX_SHADER_INPUTS][TGSI_NUM_CHANNELS];
1382 LLVMValueRef aos_attribs[PIPE_MAX_SHADER_INPUTS][TGSI_NUM_CHANNELS] = { { 0 } };
1383 LLVMValueRef io;
1384 LLVMValueRef clipmask; /* holds the clipmask value */
1385 const LLVMValueRef (*ptr_aos)[TGSI_NUM_CHANNELS];
1386
1387 if (elts)
1388 io_itr = lp_loop.counter;
1389 else
1390 io_itr = LLVMBuildSub(builder, lp_loop.counter, start, "");
1391
1392 io = LLVMBuildGEP(builder, io_ptr, &io_itr, 1, "");
1393 #if DEBUG_STORE
1394 lp_build_printf(builder, " --- io %d = %p, loop counter %d\n",
1395 io_itr, io, lp_loop.counter);
1396 #endif
1397 for (i = 0; i < TGSI_NUM_CHANNELS; ++i) {
1398 LLVMValueRef true_index =
1399 LLVMBuildAdd(builder,
1400 lp_loop.counter,
1401 lp_build_const_int32(gallivm, i), "");
1402
1403 /* make sure we're not out of bounds which can happen
1404 * if fetch_count % 4 != 0, because on the last iteration
1405 * a few of the 4 vertex fetches will be out of bounds */
1406 true_index = lp_build_min(&bld, true_index, fetch_max);
1407
1408 if (elts) {
1409 LLVMValueRef fetch_ptr;
1410 fetch_ptr = LLVMBuildGEP(builder, fetch_elts,
1411 &true_index, 1, "");
1412 true_index = LLVMBuildLoad(builder, fetch_ptr, "fetch_elt");
1413 }
1414
1415 for (j = 0; j < draw->pt.nr_vertex_elements; ++j) {
1416 struct pipe_vertex_element *velem = &draw->pt.vertex_element[j];
1417 LLVMValueRef vb_index =
1418 lp_build_const_int32(gallivm, velem->vertex_buffer_index);
1419 LLVMValueRef vb = LLVMBuildGEP(builder, vb_ptr, &vb_index, 1, "");
1420 generate_fetch(gallivm, vbuffers_ptr,
1421 &aos_attribs[j][i], velem, vb, true_index,
1422 instance_id);
1423 }
1424 }
1425 convert_to_soa(gallivm, aos_attribs, inputs,
1426 draw->pt.nr_vertex_elements);
1427
1428 ptr_aos = (const LLVMValueRef (*)[TGSI_NUM_CHANNELS]) inputs;
1429 generate_vs(llvm,
1430 builder,
1431 outputs,
1432 ptr_aos,
1433 system_values_array,
1434 context_ptr,
1435 sampler,
1436 variant->key.clamp_vertex_color);
1437
1438 /* store original positions in clip before further manipulation */
1439 store_clip(gallivm, io, outputs, 0, cv);
1440 store_clip(gallivm, io, outputs, 1, pos);
1441
1442 /* do cliptest */
1443 if (enable_cliptest) {
1444 /* allocate clipmask, assign it integer type */
1445 clipmask = generate_clipmask(llvm, outputs,
1446 variant->key.clip_xy,
1447 variant->key.clip_z,
1448 variant->key.clip_user,
1449 variant->key.clip_halfz,
1450 variant->key.ucp_enable,
1451 context_ptr, &have_clipdist);
1452 /* return clipping boolean value for function */
1453 clipmask_bool(gallivm, clipmask, ret_ptr);
1454 }
1455 else {
1456 clipmask = lp_build_const_int_vec(gallivm, lp_type_int_vec(32), 0);
1457 }
1458
1459 /* do viewport mapping */
1460 if (!bypass_viewport) {
1461 generate_viewport(llvm, builder, outputs, context_ptr);
1462 }
1463
1464 /* store clipmask in vertex header,
1465 * original positions in clip
1466 * and transformed positions in data
1467 */
1468 convert_to_aos(gallivm, io, outputs, clipmask,
1469 vs_info->num_outputs, max_vertices, have_clipdist);
1470 }
1471
1472 lp_build_loop_end_cond(&lp_loop, end, step, LLVMIntUGE);
1473
1474 sampler->destroy(sampler);
1475
1476 ret = LLVMBuildLoad(builder, ret_ptr, "");
1477 LLVMBuildRet(builder, ret);
1478
1479 /*
1480 * Translate the LLVM IR into machine code.
1481 */
1482 #ifdef DEBUG
1483 if (LLVMVerifyFunction(variant_func, LLVMPrintMessageAction)) {
1484 lp_debug_dump_value(variant_func);
1485 assert(0);
1486 }
1487 #endif
1488
1489 LLVMRunFunctionPassManager(gallivm->passmgr, variant_func);
1490
1491 if (gallivm_debug & GALLIVM_DEBUG_IR) {
1492 lp_debug_dump_value(variant_func);
1493 debug_printf("\n");
1494 }
1495
1496 code = LLVMGetPointerToGlobal(gallivm->engine, variant_func);
1497 if (elts)
1498 variant->jit_func_elts = (draw_jit_vert_func_elts) pointer_to_func(code);
1499 else
1500 variant->jit_func = (draw_jit_vert_func) pointer_to_func(code);
1501
1502 if (gallivm_debug & GALLIVM_DEBUG_ASM) {
1503 lp_disassemble(code);
1504 }
1505 lp_func_delete_body(variant_func);
1506 }
1507
1508
1509 struct draw_llvm_variant_key *
1510 draw_llvm_make_variant_key(struct draw_llvm *llvm, char *store)
1511 {
1512 unsigned i;
1513 struct draw_llvm_variant_key *key;
1514 struct lp_sampler_static_state *sampler;
1515
1516 key = (struct draw_llvm_variant_key *)store;
1517
1518 key->clamp_vertex_color = llvm->draw->rasterizer->clamp_vertex_color; /**/
1519
1520 /* Presumably all variants of the shader should have the same
1521 * number of vertex elements - ie the number of shader inputs.
1522 */
1523 key->nr_vertex_elements = llvm->draw->pt.nr_vertex_elements;
1524
1525 /* will have to rig this up properly later */
1526 key->clip_xy = llvm->draw->clip_xy;
1527 key->clip_z = llvm->draw->clip_z;
1528 key->clip_user = llvm->draw->clip_user;
1529 key->bypass_viewport = llvm->draw->identity_viewport;
1530 key->clip_halfz = !llvm->draw->rasterizer->gl_rasterization_rules;
1531 key->need_edgeflags = (llvm->draw->vs.edgeflag_output ? TRUE : FALSE);
1532 key->ucp_enable = llvm->draw->rasterizer->clip_plane_enable;
1533 key->pad = 0;
1534
1535 /* All variants of this shader will have the same value for
1536 * nr_samplers. Not yet trying to compact away holes in the
1537 * sampler array.
1538 */
1539 key->nr_samplers = llvm->draw->vs.vertex_shader->info.file_max[TGSI_FILE_SAMPLER] + 1;
1540
1541 sampler = draw_llvm_variant_key_samplers(key);
1542
1543 memcpy(key->vertex_element,
1544 llvm->draw->pt.vertex_element,
1545 sizeof(struct pipe_vertex_element) * key->nr_vertex_elements);
1546
1547 memset(sampler, 0, key->nr_samplers * sizeof *sampler);
1548
1549 for (i = 0 ; i < key->nr_samplers; i++) {
1550 lp_sampler_static_state(&sampler[i],
1551 llvm->draw->sampler_views[i],
1552 llvm->draw->samplers[i]);
1553 }
1554
1555 return key;
1556 }
1557
1558
1559 void
1560 draw_llvm_set_mapped_texture(struct draw_context *draw,
1561 unsigned sampler_idx,
1562 uint32_t width, uint32_t height, uint32_t depth,
1563 uint32_t first_level, uint32_t last_level,
1564 uint32_t row_stride[PIPE_MAX_TEXTURE_LEVELS],
1565 uint32_t img_stride[PIPE_MAX_TEXTURE_LEVELS],
1566 const void *data[PIPE_MAX_TEXTURE_LEVELS])
1567 {
1568 unsigned j;
1569 struct draw_jit_texture *jit_tex;
1570
1571 assert(sampler_idx < PIPE_MAX_VERTEX_SAMPLERS);
1572
1573 jit_tex = &draw->llvm->jit_context.textures[sampler_idx];
1574
1575 jit_tex->width = width;
1576 jit_tex->height = height;
1577 jit_tex->depth = depth;
1578 jit_tex->first_level = first_level;
1579 jit_tex->last_level = last_level;
1580
1581 for (j = first_level; j <= last_level; j++) {
1582 jit_tex->data[j] = data[j];
1583 jit_tex->row_stride[j] = row_stride[j];
1584 jit_tex->img_stride[j] = img_stride[j];
1585 }
1586 }
1587
1588
1589 void
1590 draw_llvm_set_sampler_state(struct draw_context *draw)
1591 {
1592 unsigned i;
1593
1594 for (i = 0; i < draw->num_samplers; i++) {
1595 struct draw_jit_texture *jit_tex = &draw->llvm->jit_context.textures[i];
1596
1597 if (draw->samplers[i]) {
1598 jit_tex->min_lod = draw->samplers[i]->min_lod;
1599 jit_tex->max_lod = draw->samplers[i]->max_lod;
1600 jit_tex->lod_bias = draw->samplers[i]->lod_bias;
1601 COPY_4V(jit_tex->border_color, draw->samplers[i]->border_color.f);
1602 }
1603 }
1604 }
1605
1606
1607 void
1608 draw_llvm_destroy_variant(struct draw_llvm_variant *variant)
1609 {
1610 struct draw_llvm *llvm = variant->llvm;
1611
1612 if (variant->function_elts) {
1613 LLVMFreeMachineCodeForFunction(llvm->gallivm->engine,
1614 variant->function_elts);
1615 LLVMDeleteFunction(variant->function_elts);
1616 }
1617
1618 if (variant->function) {
1619 LLVMFreeMachineCodeForFunction(llvm->gallivm->engine,
1620 variant->function);
1621 LLVMDeleteFunction(variant->function);
1622 }
1623
1624 remove_from_list(&variant->list_item_local);
1625 variant->shader->variants_cached--;
1626 remove_from_list(&variant->list_item_global);
1627 llvm->nr_variants--;
1628 FREE(variant);
1629 }