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