1 /**************************************************************************
3 * Copyright 2010 VMware, Inc.
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
26 **************************************************************************/
28 #include "draw_llvm.h"
30 #include "draw_context.h"
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"
47 #include "tgsi/tgsi_exec.h"
48 #include "tgsi/tgsi_dump.h"
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"
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.
65 draw_llvm_garbage_collect_callback(void *cb_data
)
67 struct draw_llvm
*llvm
= (struct draw_llvm
*) cb_data
;
68 struct draw_llvm_variant_list_item
*li
;
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
);
78 /* Null-out these pointers so they get remade next time they're needed.
79 * See the accessor functions below.
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
;
89 draw_llvm_generate(struct draw_llvm
*llvm
, struct draw_llvm_variant
*var
,
94 * Create LLVM type for struct draw_jit_texture
97 create_jit_texture_type(struct gallivm_state
*gallivm
, const char *struct_name
)
99 LLVMTargetDataRef target
= gallivm
->target
;
100 LLVMTypeRef texture_type
;
101 LLVMTypeRef elem_types
[DRAW_JIT_TEXTURE_NUM_FIELDS
];
102 LLVMTypeRef int32_type
= LLVMInt32TypeInContext(gallivm
->context
);
104 elem_types
[DRAW_JIT_TEXTURE_WIDTH
] =
105 elem_types
[DRAW_JIT_TEXTURE_HEIGHT
] =
106 elem_types
[DRAW_JIT_TEXTURE_DEPTH
] =
107 elem_types
[DRAW_JIT_TEXTURE_FIRST_LEVEL
] =
108 elem_types
[DRAW_JIT_TEXTURE_LAST_LEVEL
] = int32_type
;
109 elem_types
[DRAW_JIT_TEXTURE_ROW_STRIDE
] =
110 elem_types
[DRAW_JIT_TEXTURE_IMG_STRIDE
] =
111 LLVMArrayType(int32_type
, PIPE_MAX_TEXTURE_LEVELS
);
112 elem_types
[DRAW_JIT_TEXTURE_DATA
] =
113 LLVMArrayType(LLVMPointerType(LLVMInt8TypeInContext(gallivm
->context
), 0),
114 PIPE_MAX_TEXTURE_LEVELS
);
115 elem_types
[DRAW_JIT_TEXTURE_MIN_LOD
] =
116 elem_types
[DRAW_JIT_TEXTURE_MAX_LOD
] =
117 elem_types
[DRAW_JIT_TEXTURE_LOD_BIAS
] = LLVMFloatTypeInContext(gallivm
->context
);
118 elem_types
[DRAW_JIT_TEXTURE_BORDER_COLOR
] =
119 LLVMArrayType(LLVMFloatTypeInContext(gallivm
->context
), 4);
121 #if HAVE_LLVM >= 0x0300
122 texture_type
= LLVMStructCreateNamed(gallivm
->context
, struct_name
);
123 LLVMStructSetBody(texture_type
, elem_types
,
124 Elements(elem_types
), 0);
126 texture_type
= LLVMStructTypeInContext(gallivm
->context
, elem_types
,
127 Elements(elem_types
), 0);
129 LLVMAddTypeName(gallivm
->module
, struct_name
, texture_type
);
131 /* Make sure the target's struct layout cache doesn't return
132 * stale/invalid data.
134 LLVMInvalidateStructLayout(gallivm
->target
, texture_type
);
137 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture
, width
,
138 target
, texture_type
,
139 DRAW_JIT_TEXTURE_WIDTH
);
140 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture
, height
,
141 target
, texture_type
,
142 DRAW_JIT_TEXTURE_HEIGHT
);
143 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture
, depth
,
144 target
, texture_type
,
145 DRAW_JIT_TEXTURE_DEPTH
);
146 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture
, first_level
,
147 target
, texture_type
,
148 DRAW_JIT_TEXTURE_FIRST_LEVEL
);
149 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture
, last_level
,
150 target
, texture_type
,
151 DRAW_JIT_TEXTURE_LAST_LEVEL
);
152 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture
, row_stride
,
153 target
, texture_type
,
154 DRAW_JIT_TEXTURE_ROW_STRIDE
);
155 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture
, img_stride
,
156 target
, texture_type
,
157 DRAW_JIT_TEXTURE_IMG_STRIDE
);
158 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture
, data
,
159 target
, texture_type
,
160 DRAW_JIT_TEXTURE_DATA
);
161 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture
, min_lod
,
162 target
, texture_type
,
163 DRAW_JIT_TEXTURE_MIN_LOD
);
164 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture
, max_lod
,
165 target
, texture_type
,
166 DRAW_JIT_TEXTURE_MAX_LOD
);
167 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture
, lod_bias
,
168 target
, texture_type
,
169 DRAW_JIT_TEXTURE_LOD_BIAS
);
170 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture
, border_color
,
171 target
, texture_type
,
172 DRAW_JIT_TEXTURE_BORDER_COLOR
);
174 LP_CHECK_STRUCT_SIZE(struct draw_jit_texture
, target
, texture_type
);
181 * Create LLVM type for struct draw_jit_texture
184 create_jit_context_type(struct gallivm_state
*gallivm
,
185 LLVMTypeRef texture_type
, const char *struct_name
)
187 LLVMTargetDataRef target
= gallivm
->target
;
188 LLVMTypeRef float_type
= LLVMFloatTypeInContext(gallivm
->context
);
189 LLVMTypeRef elem_types
[5];
190 LLVMTypeRef context_type
;
192 elem_types
[0] = LLVMPointerType(float_type
, 0); /* vs_constants */
193 elem_types
[1] = LLVMPointerType(float_type
, 0); /* gs_constants */
194 elem_types
[2] = LLVMPointerType(LLVMArrayType(LLVMArrayType(float_type
, 4),
195 DRAW_TOTAL_CLIP_PLANES
), 0);
196 elem_types
[3] = LLVMPointerType(float_type
, 0); /* viewport */
197 elem_types
[4] = LLVMArrayType(texture_type
,
198 PIPE_MAX_VERTEX_SAMPLERS
); /* textures */
199 #if HAVE_LLVM >= 0x0300
200 context_type
= LLVMStructCreateNamed(gallivm
->context
, struct_name
);
201 LLVMStructSetBody(context_type
, elem_types
,
202 Elements(elem_types
), 0);
204 context_type
= LLVMStructTypeInContext(gallivm
->context
, elem_types
,
205 Elements(elem_types
), 0);
206 LLVMAddTypeName(gallivm
->module
, struct_name
, context_type
);
208 LLVMInvalidateStructLayout(gallivm
->target
, context_type
);
211 LP_CHECK_MEMBER_OFFSET(struct draw_jit_context
, vs_constants
,
212 target
, context_type
, 0);
213 LP_CHECK_MEMBER_OFFSET(struct draw_jit_context
, gs_constants
,
214 target
, context_type
, 1);
215 LP_CHECK_MEMBER_OFFSET(struct draw_jit_context
, planes
,
216 target
, context_type
, 2);
217 LP_CHECK_MEMBER_OFFSET(struct draw_jit_context
, textures
,
218 target
, context_type
,
219 DRAW_JIT_CTX_TEXTURES
);
220 LP_CHECK_STRUCT_SIZE(struct draw_jit_context
,
221 target
, context_type
);
228 * Create LLVM type for struct pipe_vertex_buffer
231 create_jit_vertex_buffer_type(struct gallivm_state
*gallivm
, const char *struct_name
)
233 LLVMTargetDataRef target
= gallivm
->target
;
234 LLVMTypeRef elem_types
[3];
238 elem_types
[1] = LLVMInt32TypeInContext(gallivm
->context
);
239 elem_types
[2] = LLVMPointerType(LLVMInt8TypeInContext(gallivm
->context
), 0); /* vs_constants */
241 #if HAVE_LLVM >= 0x0300
242 vb_type
= LLVMStructCreateNamed(gallivm
->context
, struct_name
);
243 LLVMStructSetBody(vb_type
, elem_types
,
244 Elements(elem_types
), 0);
246 vb_type
= LLVMStructTypeInContext(gallivm
->context
, elem_types
,
247 Elements(elem_types
), 0);
248 LLVMAddTypeName(gallivm
->module
, struct_name
, vb_type
);
250 LLVMInvalidateStructLayout(gallivm
->target
, vb_type
);
253 LP_CHECK_MEMBER_OFFSET(struct pipe_vertex_buffer
, stride
,
255 LP_CHECK_MEMBER_OFFSET(struct pipe_vertex_buffer
, buffer_offset
,
258 LP_CHECK_STRUCT_SIZE(struct pipe_vertex_buffer
, target
, vb_type
);
265 * Create LLVM type for struct vertex_header;
268 create_jit_vertex_header(struct gallivm_state
*gallivm
, int data_elems
)
270 LLVMTargetDataRef target
= gallivm
->target
;
271 LLVMTypeRef elem_types
[3];
272 LLVMTypeRef vertex_header
;
273 char struct_name
[24];
275 util_snprintf(struct_name
, 23, "vertex_header%d", data_elems
);
277 elem_types
[0] = LLVMIntTypeInContext(gallivm
->context
, 32);
278 elem_types
[1] = LLVMArrayType(LLVMFloatTypeInContext(gallivm
->context
), 4);
279 elem_types
[2] = LLVMArrayType(elem_types
[1], data_elems
);
281 #if HAVE_LLVM >= 0x0300
282 vertex_header
= LLVMStructCreateNamed(gallivm
->context
, struct_name
);
283 LLVMStructSetBody(vertex_header
, elem_types
,
284 Elements(elem_types
), 0);
286 vertex_header
= LLVMStructTypeInContext(gallivm
->context
, elem_types
,
287 Elements(elem_types
), 0);
288 LLVMAddTypeName(gallivm
->module
, struct_name
, vertex_header
);
290 LLVMInvalidateStructLayout(gallivm
->target
, vertex_header
);
293 /* these are bit-fields and we can't take address of them
294 LP_CHECK_MEMBER_OFFSET(struct vertex_header, clipmask,
295 target, vertex_header,
296 DRAW_JIT_VERTEX_CLIPMASK);
297 LP_CHECK_MEMBER_OFFSET(struct vertex_header, edgeflag,
298 target, vertex_header,
299 DRAW_JIT_VERTEX_EDGEFLAG);
300 LP_CHECK_MEMBER_OFFSET(struct vertex_header, pad,
301 target, vertex_header,
302 DRAW_JIT_VERTEX_PAD);
303 LP_CHECK_MEMBER_OFFSET(struct vertex_header, vertex_id,
304 target, vertex_header,
305 DRAW_JIT_VERTEX_VERTEX_ID);
307 LP_CHECK_MEMBER_OFFSET(struct vertex_header
, clip
,
308 target
, vertex_header
,
309 DRAW_JIT_VERTEX_CLIP
);
310 LP_CHECK_MEMBER_OFFSET(struct vertex_header
, data
,
311 target
, vertex_header
,
312 DRAW_JIT_VERTEX_DATA
);
314 return vertex_header
;
319 * Create LLVM types for various structures.
322 create_jit_types(struct draw_llvm
*llvm
)
324 struct gallivm_state
*gallivm
= llvm
->gallivm
;
325 LLVMTypeRef texture_type
, context_type
, buffer_type
, vb_type
;
327 texture_type
= create_jit_texture_type(gallivm
, "texture");
329 context_type
= create_jit_context_type(gallivm
, texture_type
, "draw_jit_context");
330 llvm
->context_ptr_type
= LLVMPointerType(context_type
, 0);
332 buffer_type
= LLVMPointerType(LLVMIntTypeInContext(gallivm
->context
, 8), 0);
333 llvm
->buffer_ptr_type
= LLVMPointerType(buffer_type
, 0);
335 vb_type
= create_jit_vertex_buffer_type(gallivm
, "pipe_vertex_buffer");
336 llvm
->vb_ptr_type
= LLVMPointerType(vb_type
, 0);
341 get_context_ptr_type(struct draw_llvm
*llvm
)
343 if (!llvm
->context_ptr_type
)
344 create_jit_types(llvm
);
345 return llvm
->context_ptr_type
;
350 get_buffer_ptr_type(struct draw_llvm
*llvm
)
352 if (!llvm
->buffer_ptr_type
)
353 create_jit_types(llvm
);
354 return llvm
->buffer_ptr_type
;
359 get_vb_ptr_type(struct draw_llvm
*llvm
)
361 if (!llvm
->vb_ptr_type
)
362 create_jit_types(llvm
);
363 return llvm
->vb_ptr_type
;
367 get_vertex_header_ptr_type(struct draw_llvm
*llvm
)
369 if (!llvm
->vertex_header_ptr_type
)
370 create_jit_types(llvm
);
371 return llvm
->vertex_header_ptr_type
;
376 * Create per-context LLVM info.
379 draw_llvm_create(struct draw_context
*draw
, struct gallivm_state
*gallivm
)
381 struct draw_llvm
*llvm
;
383 llvm
= CALLOC_STRUCT( draw_llvm
);
390 llvm
->gallivm
= gallivm
;
392 if (gallivm_debug
& GALLIVM_DEBUG_IR
) {
393 LLVMDumpModule(llvm
->gallivm
->module
);
396 llvm
->nr_variants
= 0;
397 make_empty_list(&llvm
->vs_variants_list
);
399 gallivm_register_garbage_collector_callback(
400 draw_llvm_garbage_collect_callback
, llvm
);
407 * Free per-context LLVM info.
410 draw_llvm_destroy(struct draw_llvm
*llvm
)
412 gallivm_remove_garbage_collector_callback(
413 draw_llvm_garbage_collect_callback
, llvm
);
415 /* XXX free other draw_llvm data? */
421 * Create LLVM-generated code for a vertex shader.
423 struct draw_llvm_variant
*
424 draw_llvm_create_variant(struct draw_llvm
*llvm
,
426 const struct draw_llvm_variant_key
*key
)
428 struct draw_llvm_variant
*variant
;
429 struct llvm_vertex_shader
*shader
=
430 llvm_vertex_shader(llvm
->draw
->vs
.vertex_shader
);
431 LLVMTypeRef vertex_header
;
433 variant
= MALLOC(sizeof *variant
+
434 shader
->variant_key_size
-
435 sizeof variant
->key
);
439 variant
->llvm
= llvm
;
441 memcpy(&variant
->key
, key
, shader
->variant_key_size
);
443 vertex_header
= create_jit_vertex_header(llvm
->gallivm
, num_inputs
);
445 llvm
->vertex_header_ptr_type
= LLVMPointerType(vertex_header
, 0);
447 draw_llvm_generate(llvm
, variant
, FALSE
); /* linear */
448 draw_llvm_generate(llvm
, variant
, TRUE
); /* elts */
450 variant
->shader
= shader
;
451 variant
->list_item_global
.base
= variant
;
452 variant
->list_item_local
.base
= variant
;
453 /*variant->no = */shader
->variants_created
++;
454 variant
->list_item_global
.base
= variant
;
461 generate_vs(struct draw_llvm
*llvm
,
462 LLVMBuilderRef builder
,
463 LLVMValueRef (*outputs
)[NUM_CHANNELS
],
464 const LLVMValueRef (*inputs
)[NUM_CHANNELS
],
465 LLVMValueRef system_values_array
,
466 LLVMValueRef context_ptr
,
467 struct lp_build_sampler_soa
*draw_sampler
,
468 boolean clamp_vertex_color
)
470 const struct tgsi_token
*tokens
= llvm
->draw
->vs
.vertex_shader
->state
.tokens
;
471 struct lp_type vs_type
;
472 LLVMValueRef consts_ptr
= draw_jit_context_vs_constants(llvm
->gallivm
, context_ptr
);
473 struct lp_build_sampler_soa
*sampler
= 0;
475 memset(&vs_type
, 0, sizeof vs_type
);
476 vs_type
.floating
= TRUE
; /* floating point values */
477 vs_type
.sign
= TRUE
; /* values are signed */
478 vs_type
.norm
= FALSE
; /* values are not limited to [0,1] or [-1,1] */
479 vs_type
.width
= 32; /* 32-bit float */
480 vs_type
.length
= 4; /* 4 elements per vector */
482 num_vs
= 4; /* number of vertices per block */
485 if (gallivm_debug
& GALLIVM_DEBUG_IR
) {
486 tgsi_dump(tokens
, 0);
489 if (llvm
->draw
->num_sampler_views
&& llvm
->draw
->num_samplers
)
490 sampler
= draw_sampler
;
492 lp_build_tgsi_soa(llvm
->gallivm
,
495 NULL
/*struct lp_build_mask_context *mask*/,
502 &llvm
->draw
->vs
.vertex_shader
->info
);
504 if (clamp_vertex_color
) {
506 unsigned chan
, attrib
;
507 struct lp_build_context bld
;
508 struct tgsi_shader_info
* info
= &llvm
->draw
->vs
.vertex_shader
->info
;
509 lp_build_context_init(&bld
, llvm
->gallivm
, vs_type
);
511 for (attrib
= 0; attrib
< info
->num_outputs
; ++attrib
) {
512 for (chan
= 0; chan
< NUM_CHANNELS
; ++chan
) {
513 if (outputs
[attrib
][chan
]) {
514 switch (info
->output_semantic_name
[attrib
]) {
515 case TGSI_SEMANTIC_COLOR
:
516 case TGSI_SEMANTIC_BCOLOR
:
517 out
= LLVMBuildLoad(builder
, outputs
[attrib
][chan
], "");
518 out
= lp_build_clamp(&bld
, out
, bld
.zero
, bld
.one
);
519 LLVMBuildStore(builder
, out
, outputs
[attrib
][chan
]);
530 static void print_vectorf(LLVMBuilderRef builder
,
534 val
[0] = LLVMBuildExtractElement(builder
, vec
,
535 lp_build_const_int32(gallivm
, 0), "");
536 val
[1] = LLVMBuildExtractElement(builder
, vec
,
537 lp_build_const_int32(gallivm
, 1), "");
538 val
[2] = LLVMBuildExtractElement(builder
, vec
,
539 lp_build_const_int32(gallivm
, 2), "");
540 val
[3] = LLVMBuildExtractElement(builder
, vec
,
541 lp_build_const_int32(gallivm
, 3), "");
542 lp_build_printf(builder
, "vector = [%f, %f, %f, %f]\n",
543 val
[0], val
[1], val
[2], val
[3]);
549 generate_fetch(struct gallivm_state
*gallivm
,
550 LLVMValueRef vbuffers_ptr
,
552 struct pipe_vertex_element
*velem
,
555 LLVMValueRef instance_id
)
557 LLVMBuilderRef builder
= gallivm
->builder
;
558 LLVMValueRef indices
=
559 LLVMConstInt(LLVMInt64TypeInContext(gallivm
->context
),
560 velem
->vertex_buffer_index
, 0);
561 LLVMValueRef vbuffer_ptr
= LLVMBuildGEP(builder
, vbuffers_ptr
,
563 LLVMValueRef vb_stride
= draw_jit_vbuffer_stride(gallivm
, vbuf
);
564 LLVMValueRef vb_buffer_offset
= draw_jit_vbuffer_offset(gallivm
, vbuf
);
567 if (velem
->instance_divisor
) {
568 /* array index = instance_id / instance_divisor */
569 index
= LLVMBuildUDiv(builder
, instance_id
,
570 lp_build_const_int32(gallivm
, velem
->instance_divisor
),
574 stride
= LLVMBuildMul(builder
, vb_stride
, index
, "");
576 vbuffer_ptr
= LLVMBuildLoad(builder
, vbuffer_ptr
, "vbuffer");
578 stride
= LLVMBuildAdd(builder
, stride
,
581 stride
= LLVMBuildAdd(builder
, stride
,
582 lp_build_const_int32(gallivm
, velem
->src_offset
),
585 /*lp_build_printf(builder, "vbuf index = %d, stride is %d\n", indices, stride);*/
586 vbuffer_ptr
= LLVMBuildGEP(builder
, vbuffer_ptr
, &stride
, 1, "");
588 *res
= draw_llvm_translate_from(gallivm
, vbuffer_ptr
, velem
->src_format
);
593 aos_to_soa(struct gallivm_state
*gallivm
,
598 LLVMValueRef channel
)
600 LLVMBuilderRef builder
= gallivm
->builder
;
601 LLVMValueRef ex
, res
;
603 ex
= LLVMBuildExtractElement(builder
, val0
,
605 res
= LLVMBuildInsertElement(builder
,
606 LLVMConstNull(LLVMTypeOf(val0
)),
608 lp_build_const_int32(gallivm
, 0),
611 ex
= LLVMBuildExtractElement(builder
, val1
,
613 res
= LLVMBuildInsertElement(builder
,
615 lp_build_const_int32(gallivm
, 1),
618 ex
= LLVMBuildExtractElement(builder
, val2
,
620 res
= LLVMBuildInsertElement(builder
,
622 lp_build_const_int32(gallivm
, 2),
625 ex
= LLVMBuildExtractElement(builder
, val3
,
627 res
= LLVMBuildInsertElement(builder
,
629 lp_build_const_int32(gallivm
, 3),
637 soa_to_aos(struct gallivm_state
*gallivm
,
638 LLVMValueRef soa
[NUM_CHANNELS
],
639 LLVMValueRef aos
[NUM_CHANNELS
])
641 LLVMBuilderRef builder
= gallivm
->builder
;
645 debug_assert(NUM_CHANNELS
== 4);
647 aos
[0] = LLVMConstNull(LLVMTypeOf(soa
[0]));
648 aos
[1] = aos
[2] = aos
[3] = aos
[0];
650 for (i
= 0; i
< NUM_CHANNELS
; ++i
) {
651 LLVMValueRef channel
= lp_build_const_int32(gallivm
, i
);
653 comp
= LLVMBuildExtractElement(builder
, soa
[i
],
654 lp_build_const_int32(gallivm
, 0), "");
655 aos
[0] = LLVMBuildInsertElement(builder
, aos
[0], comp
, channel
, "");
657 comp
= LLVMBuildExtractElement(builder
, soa
[i
],
658 lp_build_const_int32(gallivm
, 1), "");
659 aos
[1] = LLVMBuildInsertElement(builder
, aos
[1], comp
, channel
, "");
661 comp
= LLVMBuildExtractElement(builder
, soa
[i
],
662 lp_build_const_int32(gallivm
, 2), "");
663 aos
[2] = LLVMBuildInsertElement(builder
, aos
[2], comp
, channel
, "");
665 comp
= LLVMBuildExtractElement(builder
, soa
[i
],
666 lp_build_const_int32(gallivm
, 3), "");
667 aos
[3] = LLVMBuildInsertElement(builder
, aos
[3], comp
, channel
, "");
674 convert_to_soa(struct gallivm_state
*gallivm
,
675 LLVMValueRef (*aos
)[NUM_CHANNELS
],
676 LLVMValueRef (*soa
)[NUM_CHANNELS
],
681 debug_assert(NUM_CHANNELS
== 4);
683 for (i
= 0; i
< num_attribs
; ++i
) {
684 LLVMValueRef val0
= aos
[i
][0];
685 LLVMValueRef val1
= aos
[i
][1];
686 LLVMValueRef val2
= aos
[i
][2];
687 LLVMValueRef val3
= aos
[i
][3];
689 soa
[i
][0] = aos_to_soa(gallivm
, val0
, val1
, val2
, val3
,
690 lp_build_const_int32(gallivm
, 0));
691 soa
[i
][1] = aos_to_soa(gallivm
, val0
, val1
, val2
, val3
,
692 lp_build_const_int32(gallivm
, 1));
693 soa
[i
][2] = aos_to_soa(gallivm
, val0
, val1
, val2
, val3
,
694 lp_build_const_int32(gallivm
, 2));
695 soa
[i
][3] = aos_to_soa(gallivm
, val0
, val1
, val2
, val3
,
696 lp_build_const_int32(gallivm
, 3));
702 store_aos(struct gallivm_state
*gallivm
,
706 LLVMValueRef clipmask
)
708 LLVMBuilderRef builder
= gallivm
->builder
;
709 LLVMValueRef id_ptr
= draw_jit_header_id(gallivm
, io_ptr
);
710 LLVMValueRef data_ptr
= draw_jit_header_data(gallivm
, io_ptr
);
711 LLVMValueRef indices
[3];
713 int vertex_id_pad_edgeflag
;
715 indices
[0] = lp_build_const_int32(gallivm
, 0);
717 indices
[2] = lp_build_const_int32(gallivm
, 0);
719 /* If this assertion fails, it means we need to update the bit twidding
720 * code here. See struct vertex_header in draw_private.h.
722 assert(DRAW_TOTAL_CLIP_PLANES
==14);
723 /* initialize vertex id:16 = 0xffff, pad:1 = 0, edgeflag:1 = 1 */
724 vertex_id_pad_edgeflag
= (0xffff << 16) | (1 << DRAW_TOTAL_CLIP_PLANES
);
725 val
= lp_build_const_int32(gallivm
, vertex_id_pad_edgeflag
);
726 /* OR with the clipmask */
727 val
= LLVMBuildOr(builder
, val
, clipmask
, "");
729 /* store vertex header */
730 LLVMBuildStore(builder
, val
, id_ptr
);
734 lp_build_printf(builder
, " ---- %p storing attribute %d (io = %p)\n", data_ptr
, index
, io_ptr
);
737 /*lp_build_printf(builder, " ---- %p storing at %d (%p) ", io_ptr, index, data_ptr);
738 print_vectorf(builder, value);*/
739 data_ptr
= LLVMBuildBitCast(builder
, data_ptr
,
740 LLVMPointerType(LLVMArrayType(LLVMVectorType(LLVMFloatTypeInContext(gallivm
->context
), 4), 0), 0),
742 data_ptr
= LLVMBuildGEP(builder
, data_ptr
, indices
, 2, "");
744 LLVMBuildStore(builder
, value
, data_ptr
);
747 LLVMValueRef x
, y
, z
, w
;
748 LLVMValueRef idx0
, idx1
, idx2
, idx3
;
749 LLVMValueRef gep0
, gep1
, gep2
, gep3
;
750 data_ptr
= LLVMBuildGEP(builder
, data_ptr
, indices
, 3, "");
752 idx0
= lp_build_const_int32(gallivm
, 0);
753 idx1
= lp_build_const_int32(gallivm
, 1);
754 idx2
= lp_build_const_int32(gallivm
, 2);
755 idx3
= lp_build_const_int32(gallivm
, 3);
757 x
= LLVMBuildExtractElement(builder
, value
,
759 y
= LLVMBuildExtractElement(builder
, value
,
761 z
= LLVMBuildExtractElement(builder
, value
,
763 w
= LLVMBuildExtractElement(builder
, value
,
766 gep0
= LLVMBuildGEP(builder
, data_ptr
, &idx0
, 1, "");
767 gep1
= LLVMBuildGEP(builder
, data_ptr
, &idx1
, 1, "");
768 gep2
= LLVMBuildGEP(builder
, data_ptr
, &idx2
, 1, "");
769 gep3
= LLVMBuildGEP(builder
, data_ptr
, &idx3
, 1, "");
771 /*lp_build_printf(builder, "##### x = %f (%p), y = %f (%p), z = %f (%p), w = %f (%p)\n",
772 x, gep0, y, gep1, z, gep2, w, gep3);*/
773 LLVMBuildStore(builder
, x
, gep0
);
774 LLVMBuildStore(builder
, y
, gep1
);
775 LLVMBuildStore(builder
, z
, gep2
);
776 LLVMBuildStore(builder
, w
, gep3
);
783 store_aos_array(struct gallivm_state
*gallivm
,
785 LLVMValueRef aos
[NUM_CHANNELS
],
788 LLVMValueRef clipmask
)
790 LLVMBuilderRef builder
= gallivm
->builder
;
791 LLVMValueRef attr_index
= lp_build_const_int32(gallivm
, attrib
);
792 LLVMValueRef ind0
= lp_build_const_int32(gallivm
, 0);
793 LLVMValueRef ind1
= lp_build_const_int32(gallivm
, 1);
794 LLVMValueRef ind2
= lp_build_const_int32(gallivm
, 2);
795 LLVMValueRef ind3
= lp_build_const_int32(gallivm
, 3);
796 LLVMValueRef io0_ptr
, io1_ptr
, io2_ptr
, io3_ptr
;
797 LLVMValueRef clipmask0
, clipmask1
, clipmask2
, clipmask3
;
799 debug_assert(NUM_CHANNELS
== 4);
801 io0_ptr
= LLVMBuildGEP(builder
, io_ptr
,
803 io1_ptr
= LLVMBuildGEP(builder
, io_ptr
,
805 io2_ptr
= LLVMBuildGEP(builder
, io_ptr
,
807 io3_ptr
= LLVMBuildGEP(builder
, io_ptr
,
810 clipmask0
= LLVMBuildExtractElement(builder
, clipmask
,
812 clipmask1
= LLVMBuildExtractElement(builder
, clipmask
,
814 clipmask2
= LLVMBuildExtractElement(builder
, clipmask
,
816 clipmask3
= LLVMBuildExtractElement(builder
, clipmask
,
820 lp_build_printf(builder
, "io = %p, indexes[%d, %d, %d, %d]\n, clipmask0 = %x, clipmask1 = %x, clipmask2 = %x, clipmask3 = %x\n",
821 io_ptr
, ind0
, ind1
, ind2
, ind3
, clipmask0
, clipmask1
, clipmask2
, clipmask3
);
823 /* store for each of the 4 vertices */
824 store_aos(gallivm
, io0_ptr
, attr_index
, aos
[0], clipmask0
);
825 store_aos(gallivm
, io1_ptr
, attr_index
, aos
[1], clipmask1
);
826 store_aos(gallivm
, io2_ptr
, attr_index
, aos
[2], clipmask2
);
827 store_aos(gallivm
, io3_ptr
, attr_index
, aos
[3], clipmask3
);
832 convert_to_aos(struct gallivm_state
*gallivm
,
834 LLVMValueRef (*outputs
)[NUM_CHANNELS
],
835 LLVMValueRef clipmask
,
839 LLVMBuilderRef builder
= gallivm
->builder
;
840 unsigned chan
, attrib
;
843 lp_build_printf(builder
, " # storing begin\n");
845 for (attrib
= 0; attrib
< num_outputs
; ++attrib
) {
848 for (chan
= 0; chan
< NUM_CHANNELS
; ++chan
) {
849 if (outputs
[attrib
][chan
]) {
850 LLVMValueRef out
= LLVMBuildLoad(builder
, outputs
[attrib
][chan
], "");
851 lp_build_name(out
, "output%u.%c", attrib
, "xyzw"[chan
]);
852 /*lp_build_printf(builder, "output %d : %d ",
853 LLVMConstInt(LLVMInt32Type(), attrib, 0),
854 LLVMConstInt(LLVMInt32Type(), chan, 0));
855 print_vectorf(builder, out);*/
862 soa_to_aos(gallivm
, soa
, aos
);
863 store_aos_array(gallivm
,
871 lp_build_printf(builder
, " # storing end\n");
877 * Stores original vertex positions in clip coordinates
878 * There is probably a more efficient way to do this, 4 floats at once
879 * rather than extracting each element one by one.
882 store_clip(struct gallivm_state
*gallivm
,
884 LLVMValueRef (*outputs
)[NUM_CHANNELS
])
886 LLVMBuilderRef builder
= gallivm
->builder
;
888 LLVMValueRef indices
[2];
889 LLVMValueRef io0_ptr
, io1_ptr
, io2_ptr
, io3_ptr
;
890 LLVMValueRef clip_ptr0
, clip_ptr1
, clip_ptr2
, clip_ptr3
;
891 LLVMValueRef clip0_ptr
, clip1_ptr
, clip2_ptr
, clip3_ptr
;
892 LLVMValueRef out0elem
, out1elem
, out2elem
, out3elem
;
895 LLVMValueRef ind0
= lp_build_const_int32(gallivm
, 0);
896 LLVMValueRef ind1
= lp_build_const_int32(gallivm
, 1);
897 LLVMValueRef ind2
= lp_build_const_int32(gallivm
, 2);
898 LLVMValueRef ind3
= lp_build_const_int32(gallivm
, 3);
901 indices
[1] = lp_build_const_int32(gallivm
, 0);
903 out
[0] = LLVMBuildLoad(builder
, outputs
[0][0], ""); /*x0 x1 x2 x3*/
904 out
[1] = LLVMBuildLoad(builder
, outputs
[0][1], ""); /*y0 y1 y2 y3*/
905 out
[2] = LLVMBuildLoad(builder
, outputs
[0][2], ""); /*z0 z1 z2 z3*/
906 out
[3] = LLVMBuildLoad(builder
, outputs
[0][3], ""); /*w0 w1 w2 w3*/
908 io0_ptr
= LLVMBuildGEP(builder
, io_ptr
, &ind0
, 1, "");
909 io1_ptr
= LLVMBuildGEP(builder
, io_ptr
, &ind1
, 1, "");
910 io2_ptr
= LLVMBuildGEP(builder
, io_ptr
, &ind2
, 1, "");
911 io3_ptr
= LLVMBuildGEP(builder
, io_ptr
, &ind3
, 1, "");
913 clip_ptr0
= draw_jit_header_clip(gallivm
, io0_ptr
);
914 clip_ptr1
= draw_jit_header_clip(gallivm
, io1_ptr
);
915 clip_ptr2
= draw_jit_header_clip(gallivm
, io2_ptr
);
916 clip_ptr3
= draw_jit_header_clip(gallivm
, io3_ptr
);
918 for (i
= 0; i
<4; i
++) {
919 clip0_ptr
= LLVMBuildGEP(builder
, clip_ptr0
, indices
, 2, ""); /* x0 */
920 clip1_ptr
= LLVMBuildGEP(builder
, clip_ptr1
, indices
, 2, ""); /* x1 */
921 clip2_ptr
= LLVMBuildGEP(builder
, clip_ptr2
, indices
, 2, ""); /* x2 */
922 clip3_ptr
= LLVMBuildGEP(builder
, clip_ptr3
, indices
, 2, ""); /* x3 */
924 out0elem
= LLVMBuildExtractElement(builder
, out
[i
], ind0
, ""); /* x0 */
925 out1elem
= LLVMBuildExtractElement(builder
, out
[i
], ind1
, ""); /* x1 */
926 out2elem
= LLVMBuildExtractElement(builder
, out
[i
], ind2
, ""); /* x2 */
927 out3elem
= LLVMBuildExtractElement(builder
, out
[i
], ind3
, ""); /* x3 */
929 LLVMBuildStore(builder
, out0elem
, clip0_ptr
);
930 LLVMBuildStore(builder
, out1elem
, clip1_ptr
);
931 LLVMBuildStore(builder
, out2elem
, clip2_ptr
);
932 LLVMBuildStore(builder
, out3elem
, clip3_ptr
);
934 indices
[1]= LLVMBuildAdd(builder
, indices
[1], ind1
, "");
941 * Equivalent of _mm_set1_ps(a)
944 vec4f_from_scalar(struct gallivm_state
*gallivm
,
948 LLVMTypeRef float_type
= LLVMFloatTypeInContext(gallivm
->context
);
949 LLVMValueRef res
= LLVMGetUndef(LLVMVectorType(float_type
, 4));
952 for (i
= 0; i
< 4; ++i
) {
953 LLVMValueRef index
= lp_build_const_int32(gallivm
, i
);
954 res
= LLVMBuildInsertElement(gallivm
->builder
, res
, a
,
955 index
, i
== 3 ? name
: "");
963 * Transforms the outputs for viewport mapping
966 generate_viewport(struct draw_llvm
*llvm
,
967 LLVMBuilderRef builder
,
968 LLVMValueRef (*outputs
)[NUM_CHANNELS
],
969 LLVMValueRef context_ptr
)
972 struct gallivm_state
*gallivm
= llvm
->gallivm
;
973 struct lp_type f32_type
= lp_type_float_vec(32);
974 LLVMValueRef out3
= LLVMBuildLoad(builder
, outputs
[0][3], ""); /*w0 w1 w2 w3*/
975 LLVMValueRef const1
= lp_build_const_vec(gallivm
, f32_type
, 1.0); /*1.0 1.0 1.0 1.0*/
976 LLVMValueRef vp_ptr
= draw_jit_context_viewport(gallivm
, context_ptr
);
978 /* for 1/w convention*/
979 out3
= LLVMBuildFDiv(builder
, const1
, out3
, "");
980 LLVMBuildStore(builder
, out3
, outputs
[0][3]);
982 /* Viewport Mapping */
983 for (i
=0; i
<3; i
++) {
984 LLVMValueRef out
= LLVMBuildLoad(builder
, outputs
[0][i
], ""); /*x0 x1 x2 x3*/
987 LLVMValueRef scale_i
;
988 LLVMValueRef trans_i
;
991 index
= lp_build_const_int32(gallivm
, i
);
992 scale_i
= LLVMBuildGEP(builder
, vp_ptr
, &index
, 1, "");
994 index
= lp_build_const_int32(gallivm
, i
+4);
995 trans_i
= LLVMBuildGEP(builder
, vp_ptr
, &index
, 1, "");
997 scale
= vec4f_from_scalar(gallivm
, LLVMBuildLoad(builder
, scale_i
, ""), "scale");
998 trans
= vec4f_from_scalar(gallivm
, LLVMBuildLoad(builder
, trans_i
, ""), "trans");
1001 out
= LLVMBuildFMul(builder
, out
, out3
, "");
1003 out
= LLVMBuildFMul(builder
, out
, scale
, "");
1004 /* add translation */
1005 out
= LLVMBuildFAdd(builder
, out
, trans
, "");
1007 /* store transformed outputs */
1008 LLVMBuildStore(builder
, out
, outputs
[0][i
]);
1015 * Returns clipmask as 4xi32 bitmask for the 4 vertices
1018 generate_clipmask(struct gallivm_state
*gallivm
,
1019 LLVMValueRef (*outputs
)[NUM_CHANNELS
],
1024 unsigned ucp_enable
,
1025 LLVMValueRef context_ptr
)
1027 LLVMBuilderRef builder
= gallivm
->builder
;
1028 LLVMValueRef mask
; /* stores the <4xi32> clipmasks */
1029 LLVMValueRef test
, temp
;
1030 LLVMValueRef zero
, shift
;
1031 LLVMValueRef pos_x
, pos_y
, pos_z
, pos_w
;
1032 LLVMValueRef plane1
, planes
, plane_ptr
, sum
;
1033 struct lp_type f32_type
= lp_type_float_vec(32);
1035 mask
= lp_build_const_int_vec(gallivm
, lp_type_int_vec(32), 0);
1036 temp
= lp_build_const_int_vec(gallivm
, lp_type_int_vec(32), 0);
1037 zero
= lp_build_const_vec(gallivm
, f32_type
, 0); /* 0.0f 0.0f 0.0f 0.0f */
1038 shift
= lp_build_const_int_vec(gallivm
, lp_type_int_vec(32), 1); /* 1 1 1 1 */
1040 /* Assuming position stored at output[0] */
1041 pos_x
= LLVMBuildLoad(builder
, outputs
[0][0], ""); /*x0 x1 x2 x3*/
1042 pos_y
= LLVMBuildLoad(builder
, outputs
[0][1], ""); /*y0 y1 y2 y3*/
1043 pos_z
= LLVMBuildLoad(builder
, outputs
[0][2], ""); /*z0 z1 z2 z3*/
1044 pos_w
= LLVMBuildLoad(builder
, outputs
[0][3], ""); /*w0 w1 w2 w3*/
1046 /* Cliptest, for hardwired planes */
1049 test
= lp_build_compare(gallivm
, f32_type
, PIPE_FUNC_GREATER
, pos_x
, pos_w
);
1051 test
= LLVMBuildAnd(builder
, test
, temp
, "");
1055 test
= LLVMBuildFAdd(builder
, pos_x
, pos_w
, "");
1056 test
= lp_build_compare(gallivm
, f32_type
, PIPE_FUNC_GREATER
, zero
, test
);
1057 temp
= LLVMBuildShl(builder
, temp
, shift
, "");
1058 test
= LLVMBuildAnd(builder
, test
, temp
, "");
1059 mask
= LLVMBuildOr(builder
, mask
, test
, "");
1062 test
= lp_build_compare(gallivm
, f32_type
, PIPE_FUNC_GREATER
, pos_y
, pos_w
);
1063 temp
= LLVMBuildShl(builder
, temp
, shift
, "");
1064 test
= LLVMBuildAnd(builder
, test
, temp
, "");
1065 mask
= LLVMBuildOr(builder
, mask
, test
, "");
1068 test
= LLVMBuildFAdd(builder
, pos_y
, pos_w
, "");
1069 test
= lp_build_compare(gallivm
, f32_type
, PIPE_FUNC_GREATER
, zero
, test
);
1070 temp
= LLVMBuildShl(builder
, temp
, shift
, "");
1071 test
= LLVMBuildAnd(builder
, test
, temp
, "");
1072 mask
= LLVMBuildOr(builder
, mask
, test
, "");
1076 temp
= lp_build_const_int_vec(gallivm
, lp_type_int_vec(32), 16);
1079 test
= lp_build_compare(gallivm
, f32_type
, PIPE_FUNC_GREATER
, zero
, pos_z
);
1080 test
= LLVMBuildAnd(builder
, test
, temp
, "");
1081 mask
= LLVMBuildOr(builder
, mask
, test
, "");
1085 test
= LLVMBuildFAdd(builder
, pos_z
, pos_w
, "");
1086 test
= lp_build_compare(gallivm
, f32_type
, PIPE_FUNC_GREATER
, zero
, test
);
1087 test
= LLVMBuildAnd(builder
, test
, temp
, "");
1088 mask
= LLVMBuildOr(builder
, mask
, test
, "");
1091 test
= lp_build_compare(gallivm
, f32_type
, PIPE_FUNC_GREATER
, pos_z
, pos_w
);
1092 temp
= LLVMBuildShl(builder
, temp
, shift
, "");
1093 test
= LLVMBuildAnd(builder
, test
, temp
, "");
1094 mask
= LLVMBuildOr(builder
, mask
, test
, "");
1098 LLVMValueRef planes_ptr
= draw_jit_context_planes(gallivm
, context_ptr
);
1099 LLVMValueRef indices
[3];
1101 /* userclip planes */
1102 while (ucp_enable
) {
1103 unsigned plane_idx
= ffs(ucp_enable
)-1;
1104 ucp_enable
&= ~(1 << plane_idx
);
1107 indices
[0] = lp_build_const_int32(gallivm
, 0);
1108 indices
[1] = lp_build_const_int32(gallivm
, plane_idx
);
1110 indices
[2] = lp_build_const_int32(gallivm
, 0);
1111 plane_ptr
= LLVMBuildGEP(builder
, planes_ptr
, indices
, 3, "");
1112 plane1
= LLVMBuildLoad(builder
, plane_ptr
, "plane_x");
1113 planes
= vec4f_from_scalar(gallivm
, plane1
, "plane4_x");
1114 sum
= LLVMBuildFMul(builder
, planes
, pos_x
, "");
1116 indices
[2] = lp_build_const_int32(gallivm
, 1);
1117 plane_ptr
= LLVMBuildGEP(builder
, planes_ptr
, indices
, 3, "");
1118 plane1
= LLVMBuildLoad(builder
, plane_ptr
, "plane_y");
1119 planes
= vec4f_from_scalar(gallivm
, plane1
, "plane4_y");
1120 test
= LLVMBuildFMul(builder
, planes
, pos_y
, "");
1121 sum
= LLVMBuildFAdd(builder
, sum
, test
, "");
1123 indices
[2] = lp_build_const_int32(gallivm
, 2);
1124 plane_ptr
= LLVMBuildGEP(builder
, planes_ptr
, indices
, 3, "");
1125 plane1
= LLVMBuildLoad(builder
, plane_ptr
, "plane_z");
1126 planes
= vec4f_from_scalar(gallivm
, plane1
, "plane4_z");
1127 test
= LLVMBuildFMul(builder
, planes
, pos_z
, "");
1128 sum
= LLVMBuildFAdd(builder
, sum
, test
, "");
1130 indices
[2] = lp_build_const_int32(gallivm
, 3);
1131 plane_ptr
= LLVMBuildGEP(builder
, planes_ptr
, indices
, 3, "");
1132 plane1
= LLVMBuildLoad(builder
, plane_ptr
, "plane_w");
1133 planes
= vec4f_from_scalar(gallivm
, plane1
, "plane4_w");
1134 test
= LLVMBuildFMul(builder
, planes
, pos_w
, "");
1135 sum
= LLVMBuildFAdd(builder
, sum
, test
, "");
1137 test
= lp_build_compare(gallivm
, f32_type
, PIPE_FUNC_GREATER
, zero
, sum
);
1138 temp
= lp_build_const_int_vec(gallivm
, lp_type_int_vec(32), 1 << plane_idx
);
1139 test
= LLVMBuildAnd(builder
, test
, temp
, "");
1140 mask
= LLVMBuildOr(builder
, mask
, test
, "");
1148 * Returns boolean if any clipping has occurred
1149 * Used zero/non-zero i32 value to represent boolean
1152 clipmask_bool(struct gallivm_state
*gallivm
,
1153 LLVMValueRef clipmask
,
1154 LLVMValueRef ret_ptr
)
1156 LLVMBuilderRef builder
= gallivm
->builder
;
1157 LLVMValueRef ret
= LLVMBuildLoad(builder
, ret_ptr
, "");
1161 for (i
=0; i
<4; i
++) {
1162 temp
= LLVMBuildExtractElement(builder
, clipmask
,
1163 lp_build_const_int32(gallivm
, i
) , "");
1164 ret
= LLVMBuildOr(builder
, ret
, temp
, "");
1167 LLVMBuildStore(builder
, ret
, ret_ptr
);
1172 draw_llvm_generate(struct draw_llvm
*llvm
, struct draw_llvm_variant
*variant
,
1175 struct gallivm_state
*gallivm
= llvm
->gallivm
;
1176 LLVMContextRef context
= gallivm
->context
;
1177 LLVMTypeRef int32_type
= LLVMInt32TypeInContext(context
);
1178 LLVMTypeRef arg_types
[8];
1179 LLVMTypeRef func_type
;
1180 LLVMValueRef context_ptr
;
1181 LLVMBasicBlockRef block
;
1182 LLVMBuilderRef builder
;
1183 LLVMValueRef end
, start
;
1184 LLVMValueRef count
, fetch_elts
, fetch_count
;
1185 LLVMValueRef stride
, step
, io_itr
;
1186 LLVMValueRef io_ptr
, vbuffers_ptr
, vb_ptr
;
1187 LLVMValueRef instance_id
;
1188 LLVMValueRef system_values_array
;
1189 LLVMValueRef zero
= lp_build_const_int32(gallivm
, 0);
1190 LLVMValueRef one
= lp_build_const_int32(gallivm
, 1);
1191 struct draw_context
*draw
= llvm
->draw
;
1192 const struct tgsi_shader_info
*vs_info
= &draw
->vs
.vertex_shader
->info
;
1194 struct lp_build_context bld
;
1195 struct lp_build_loop_state lp_loop
;
1196 const int max_vertices
= 4;
1197 LLVMValueRef outputs
[PIPE_MAX_SHADER_OUTPUTS
][NUM_CHANNELS
];
1198 LLVMValueRef fetch_max
;
1200 struct lp_build_sampler_soa
*sampler
= 0;
1201 LLVMValueRef ret
, ret_ptr
;
1202 const boolean bypass_viewport
= variant
->key
.bypass_viewport
;
1203 const boolean enable_cliptest
= variant
->key
.clip_xy
||
1204 variant
->key
.clip_z
||
1205 variant
->key
.clip_user
;
1206 LLVMValueRef variant_func
;
1208 arg_types
[0] = get_context_ptr_type(llvm
); /* context */
1209 arg_types
[1] = get_vertex_header_ptr_type(llvm
); /* vertex_header */
1210 arg_types
[2] = get_buffer_ptr_type(llvm
); /* vbuffers */
1212 arg_types
[3] = LLVMPointerType(int32_type
, 0);/* fetch_elts * */
1214 arg_types
[3] = int32_type
; /* start */
1215 arg_types
[4] = int32_type
; /* fetch_count / count */
1216 arg_types
[5] = int32_type
; /* stride */
1217 arg_types
[6] = get_vb_ptr_type(llvm
); /* pipe_vertex_buffer's */
1218 arg_types
[7] = int32_type
; /* instance_id */
1220 func_type
= LLVMFunctionType(int32_type
, arg_types
, Elements(arg_types
), 0);
1222 variant_func
= LLVMAddFunction(gallivm
->module
,
1223 elts
? "draw_llvm_shader_elts" : "draw_llvm_shader",
1227 variant
->function_elts
= variant_func
;
1229 variant
->function
= variant_func
;
1231 LLVMSetFunctionCallConv(variant_func
, LLVMCCallConv
);
1232 for (i
= 0; i
< Elements(arg_types
); ++i
)
1233 if (LLVMGetTypeKind(arg_types
[i
]) == LLVMPointerTypeKind
)
1234 LLVMAddAttribute(LLVMGetParam(variant_func
, i
),
1235 LLVMNoAliasAttribute
);
1237 context_ptr
= LLVMGetParam(variant_func
, 0);
1238 io_ptr
= LLVMGetParam(variant_func
, 1);
1239 vbuffers_ptr
= LLVMGetParam(variant_func
, 2);
1240 stride
= LLVMGetParam(variant_func
, 5);
1241 vb_ptr
= LLVMGetParam(variant_func
, 6);
1242 instance_id
= LLVMGetParam(variant_func
, 7);
1244 lp_build_name(context_ptr
, "context");
1245 lp_build_name(io_ptr
, "io");
1246 lp_build_name(vbuffers_ptr
, "vbuffers");
1247 lp_build_name(stride
, "stride");
1248 lp_build_name(vb_ptr
, "vb");
1249 lp_build_name(instance_id
, "instance_id");
1252 fetch_elts
= LLVMGetParam(variant_func
, 3);
1253 fetch_count
= LLVMGetParam(variant_func
, 4);
1254 lp_build_name(fetch_elts
, "fetch_elts");
1255 lp_build_name(fetch_count
, "fetch_count");
1256 start
= count
= NULL
;
1259 start
= LLVMGetParam(variant_func
, 3);
1260 count
= LLVMGetParam(variant_func
, 4);
1261 lp_build_name(start
, "start");
1262 lp_build_name(count
, "count");
1263 fetch_elts
= fetch_count
= NULL
;
1270 block
= LLVMAppendBasicBlockInContext(gallivm
->context
, variant_func
, "entry");
1271 builder
= gallivm
->builder
;
1272 LLVMPositionBuilderAtEnd(builder
, block
);
1274 lp_build_context_init(&bld
, gallivm
, lp_type_int(32));
1276 system_values_array
= lp_build_system_values_array(gallivm
, vs_info
,
1279 /* function will return non-zero i32 value if any clipped vertices */
1280 ret_ptr
= lp_build_alloca(gallivm
, int32_type
, "");
1281 LLVMBuildStore(builder
, zero
, ret_ptr
);
1283 /* code generated texture sampling */
1284 sampler
= draw_llvm_sampler_soa_create(
1285 draw_llvm_variant_key_samplers(&variant
->key
),
1293 end
= lp_build_add(&bld
, start
, count
);
1296 step
= lp_build_const_int32(gallivm
, max_vertices
);
1298 fetch_max
= LLVMBuildSub(builder
, end
, one
, "fetch_max");
1300 lp_build_loop_begin(&lp_loop
, gallivm
, start
);
1302 LLVMValueRef inputs
[PIPE_MAX_SHADER_INPUTS
][NUM_CHANNELS
];
1303 LLVMValueRef aos_attribs
[PIPE_MAX_SHADER_INPUTS
][NUM_CHANNELS
] = { { 0 } };
1305 LLVMValueRef clipmask
; /* holds the clipmask value */
1306 const LLVMValueRef (*ptr_aos
)[NUM_CHANNELS
];
1309 io_itr
= lp_loop
.counter
;
1311 io_itr
= LLVMBuildSub(builder
, lp_loop
.counter
, start
, "");
1313 io
= LLVMBuildGEP(builder
, io_ptr
, &io_itr
, 1, "");
1315 lp_build_printf(builder
, " --- io %d = %p, loop counter %d\n",
1316 io_itr
, io
, lp_loop
.counter
);
1318 for (i
= 0; i
< NUM_CHANNELS
; ++i
) {
1319 LLVMValueRef true_index
=
1320 LLVMBuildAdd(builder
,
1322 lp_build_const_int32(gallivm
, i
), "");
1324 /* make sure we're not out of bounds which can happen
1325 * if fetch_count % 4 != 0, because on the last iteration
1326 * a few of the 4 vertex fetches will be out of bounds */
1327 true_index
= lp_build_min(&bld
, true_index
, fetch_max
);
1330 LLVMValueRef fetch_ptr
;
1331 fetch_ptr
= LLVMBuildGEP(builder
, fetch_elts
,
1332 &true_index
, 1, "");
1333 true_index
= LLVMBuildLoad(builder
, fetch_ptr
, "fetch_elt");
1336 for (j
= 0; j
< draw
->pt
.nr_vertex_elements
; ++j
) {
1337 struct pipe_vertex_element
*velem
= &draw
->pt
.vertex_element
[j
];
1338 LLVMValueRef vb_index
=
1339 lp_build_const_int32(gallivm
, velem
->vertex_buffer_index
);
1340 LLVMValueRef vb
= LLVMBuildGEP(builder
, vb_ptr
, &vb_index
, 1, "");
1341 generate_fetch(gallivm
, vbuffers_ptr
,
1342 &aos_attribs
[j
][i
], velem
, vb
, true_index
,
1346 convert_to_soa(gallivm
, aos_attribs
, inputs
,
1347 draw
->pt
.nr_vertex_elements
);
1349 ptr_aos
= (const LLVMValueRef (*)[NUM_CHANNELS
]) inputs
;
1354 system_values_array
,
1357 variant
->key
.clamp_vertex_color
);
1359 /* store original positions in clip before further manipulation */
1360 store_clip(gallivm
, io
, outputs
);
1363 if (enable_cliptest
) {
1364 /* allocate clipmask, assign it integer type */
1365 clipmask
= generate_clipmask(gallivm
, outputs
,
1366 variant
->key
.clip_xy
,
1367 variant
->key
.clip_z
,
1368 variant
->key
.clip_user
,
1369 variant
->key
.clip_halfz
,
1370 variant
->key
.ucp_enable
,
1372 /* return clipping boolean value for function */
1373 clipmask_bool(gallivm
, clipmask
, ret_ptr
);
1376 clipmask
= lp_build_const_int_vec(gallivm
, lp_type_int_vec(32), 0);
1379 /* do viewport mapping */
1380 if (!bypass_viewport
) {
1381 generate_viewport(llvm
, builder
, outputs
, context_ptr
);
1384 /* store clipmask in vertex header,
1385 * original positions in clip
1386 * and transformed positions in data
1388 convert_to_aos(gallivm
, io
, outputs
, clipmask
,
1389 vs_info
->num_outputs
, max_vertices
);
1392 lp_build_loop_end_cond(&lp_loop
, end
, step
, LLVMIntUGE
);
1394 sampler
->destroy(sampler
);
1396 ret
= LLVMBuildLoad(builder
, ret_ptr
, "");
1397 LLVMBuildRet(builder
, ret
);
1400 * Translate the LLVM IR into machine code.
1403 if (LLVMVerifyFunction(variant_func
, LLVMPrintMessageAction
)) {
1404 lp_debug_dump_value(variant_func
);
1409 LLVMRunFunctionPassManager(gallivm
->passmgr
, variant_func
);
1411 if (gallivm_debug
& GALLIVM_DEBUG_IR
) {
1412 lp_debug_dump_value(variant_func
);
1416 code
= LLVMGetPointerToGlobal(gallivm
->engine
, variant_func
);
1418 variant
->jit_func_elts
= (draw_jit_vert_func_elts
) pointer_to_func(code
);
1420 variant
->jit_func
= (draw_jit_vert_func
) pointer_to_func(code
);
1422 if (gallivm_debug
& GALLIVM_DEBUG_ASM
) {
1423 lp_disassemble(code
);
1425 lp_func_delete_body(variant_func
);
1429 struct draw_llvm_variant_key
*
1430 draw_llvm_make_variant_key(struct draw_llvm
*llvm
, char *store
)
1433 struct draw_llvm_variant_key
*key
;
1434 struct lp_sampler_static_state
*sampler
;
1436 key
= (struct draw_llvm_variant_key
*)store
;
1438 key
->clamp_vertex_color
= llvm
->draw
->rasterizer
->clamp_vertex_color
; /**/
1440 /* Presumably all variants of the shader should have the same
1441 * number of vertex elements - ie the number of shader inputs.
1443 key
->nr_vertex_elements
= llvm
->draw
->pt
.nr_vertex_elements
;
1445 /* will have to rig this up properly later */
1446 key
->clip_xy
= llvm
->draw
->clip_xy
;
1447 key
->clip_z
= llvm
->draw
->clip_z
;
1448 key
->clip_user
= llvm
->draw
->clip_user
;
1449 key
->bypass_viewport
= llvm
->draw
->identity_viewport
;
1450 key
->clip_halfz
= !llvm
->draw
->rasterizer
->gl_rasterization_rules
;
1451 key
->need_edgeflags
= (llvm
->draw
->vs
.edgeflag_output
? TRUE
: FALSE
);
1452 key
->ucp_enable
= llvm
->draw
->rasterizer
->clip_plane_enable
;
1455 /* All variants of this shader will have the same value for
1456 * nr_samplers. Not yet trying to compact away holes in the
1459 key
->nr_samplers
= llvm
->draw
->vs
.vertex_shader
->info
.file_max
[TGSI_FILE_SAMPLER
] + 1;
1461 sampler
= draw_llvm_variant_key_samplers(key
);
1463 memcpy(key
->vertex_element
,
1464 llvm
->draw
->pt
.vertex_element
,
1465 sizeof(struct pipe_vertex_element
) * key
->nr_vertex_elements
);
1467 memset(sampler
, 0, key
->nr_samplers
* sizeof *sampler
);
1469 for (i
= 0 ; i
< key
->nr_samplers
; i
++) {
1470 lp_sampler_static_state(&sampler
[i
],
1471 llvm
->draw
->sampler_views
[i
],
1472 llvm
->draw
->samplers
[i
]);
1480 draw_llvm_set_mapped_texture(struct draw_context
*draw
,
1481 unsigned sampler_idx
,
1482 uint32_t width
, uint32_t height
, uint32_t depth
,
1483 uint32_t first_level
, uint32_t last_level
,
1484 uint32_t row_stride
[PIPE_MAX_TEXTURE_LEVELS
],
1485 uint32_t img_stride
[PIPE_MAX_TEXTURE_LEVELS
],
1486 const void *data
[PIPE_MAX_TEXTURE_LEVELS
])
1489 struct draw_jit_texture
*jit_tex
;
1491 assert(sampler_idx
< PIPE_MAX_VERTEX_SAMPLERS
);
1493 jit_tex
= &draw
->llvm
->jit_context
.textures
[sampler_idx
];
1495 jit_tex
->width
= width
;
1496 jit_tex
->height
= height
;
1497 jit_tex
->depth
= depth
;
1498 jit_tex
->first_level
= first_level
;
1499 jit_tex
->last_level
= last_level
;
1501 for (j
= first_level
; j
<= last_level
; j
++) {
1502 jit_tex
->data
[j
] = data
[j
];
1503 jit_tex
->row_stride
[j
] = row_stride
[j
];
1504 jit_tex
->img_stride
[j
] = img_stride
[j
];
1510 draw_llvm_set_sampler_state(struct draw_context
*draw
)
1514 for (i
= 0; i
< draw
->num_samplers
; i
++) {
1515 struct draw_jit_texture
*jit_tex
= &draw
->llvm
->jit_context
.textures
[i
];
1517 if (draw
->samplers
[i
]) {
1518 jit_tex
->min_lod
= draw
->samplers
[i
]->min_lod
;
1519 jit_tex
->max_lod
= draw
->samplers
[i
]->max_lod
;
1520 jit_tex
->lod_bias
= draw
->samplers
[i
]->lod_bias
;
1521 COPY_4V(jit_tex
->border_color
, draw
->samplers
[i
]->border_color
.f
);
1528 draw_llvm_destroy_variant(struct draw_llvm_variant
*variant
)
1530 struct draw_llvm
*llvm
= variant
->llvm
;
1532 if (variant
->function_elts
) {
1533 LLVMFreeMachineCodeForFunction(llvm
->gallivm
->engine
,
1534 variant
->function_elts
);
1535 LLVMDeleteFunction(variant
->function_elts
);
1538 if (variant
->function
) {
1539 LLVMFreeMachineCodeForFunction(llvm
->gallivm
->engine
,
1541 LLVMDeleteFunction(variant
->function
);
1544 remove_from_list(&variant
->list_item_local
);
1545 variant
->shader
->variants_cached
--;
1546 remove_from_list(&variant
->list_item_global
);
1547 llvm
->nr_variants
--;