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