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