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_context
*draw
= llvm
->draw
;
69 struct draw_llvm_variant_list_item
*li
;
71 /* Ensure prepare will be run and shaders recompiled */
72 assert(!draw
->suspend_flushing
);
73 draw_do_flush(draw
, DRAW_FLUSH_STATE_CHANGE
);
75 /* free all shader variants */
76 li
= first_elem(&llvm
->vs_variants_list
);
77 while (!at_end(&llvm
->vs_variants_list
, li
)) {
78 struct draw_llvm_variant_list_item
*next
= next_elem(li
);
79 draw_llvm_destroy_variant(li
->base
);
83 /* Null-out these pointers so they get remade next time they're needed.
84 * See the accessor functions below.
86 llvm
->context_ptr_type
= NULL
;
87 llvm
->buffer_ptr_type
= NULL
;
88 llvm
->vb_ptr_type
= NULL
;
89 llvm
->vertex_header_ptr_type
= NULL
;
94 draw_llvm_generate(struct draw_llvm
*llvm
, struct draw_llvm_variant
*var
,
99 * Create LLVM type for struct draw_jit_texture
102 create_jit_texture_type(struct gallivm_state
*gallivm
, const char *struct_name
)
104 LLVMTargetDataRef target
= gallivm
->target
;
105 LLVMTypeRef texture_type
;
106 LLVMTypeRef elem_types
[DRAW_JIT_TEXTURE_NUM_FIELDS
];
107 LLVMTypeRef int32_type
= LLVMInt32TypeInContext(gallivm
->context
);
109 elem_types
[DRAW_JIT_TEXTURE_WIDTH
] =
110 elem_types
[DRAW_JIT_TEXTURE_HEIGHT
] =
111 elem_types
[DRAW_JIT_TEXTURE_DEPTH
] =
112 elem_types
[DRAW_JIT_TEXTURE_FIRST_LEVEL
] =
113 elem_types
[DRAW_JIT_TEXTURE_LAST_LEVEL
] = int32_type
;
114 elem_types
[DRAW_JIT_TEXTURE_ROW_STRIDE
] =
115 elem_types
[DRAW_JIT_TEXTURE_IMG_STRIDE
] =
116 LLVMArrayType(int32_type
, PIPE_MAX_TEXTURE_LEVELS
);
117 elem_types
[DRAW_JIT_TEXTURE_DATA
] =
118 LLVMArrayType(LLVMPointerType(LLVMInt8TypeInContext(gallivm
->context
), 0),
119 PIPE_MAX_TEXTURE_LEVELS
);
120 elem_types
[DRAW_JIT_TEXTURE_MIN_LOD
] =
121 elem_types
[DRAW_JIT_TEXTURE_MAX_LOD
] =
122 elem_types
[DRAW_JIT_TEXTURE_LOD_BIAS
] = LLVMFloatTypeInContext(gallivm
->context
);
123 elem_types
[DRAW_JIT_TEXTURE_BORDER_COLOR
] =
124 LLVMArrayType(LLVMFloatTypeInContext(gallivm
->context
), 4);
126 #if HAVE_LLVM >= 0x0300
127 texture_type
= LLVMStructCreateNamed(gallivm
->context
, struct_name
);
128 LLVMStructSetBody(texture_type
, elem_types
,
129 Elements(elem_types
), 0);
131 texture_type
= LLVMStructTypeInContext(gallivm
->context
, elem_types
,
132 Elements(elem_types
), 0);
134 LLVMAddTypeName(gallivm
->module
, struct_name
, texture_type
);
136 /* Make sure the target's struct layout cache doesn't return
137 * stale/invalid data.
139 LLVMInvalidateStructLayout(gallivm
->target
, texture_type
);
142 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture
, width
,
143 target
, texture_type
,
144 DRAW_JIT_TEXTURE_WIDTH
);
145 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture
, height
,
146 target
, texture_type
,
147 DRAW_JIT_TEXTURE_HEIGHT
);
148 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture
, depth
,
149 target
, texture_type
,
150 DRAW_JIT_TEXTURE_DEPTH
);
151 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture
, first_level
,
152 target
, texture_type
,
153 DRAW_JIT_TEXTURE_FIRST_LEVEL
);
154 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture
, last_level
,
155 target
, texture_type
,
156 DRAW_JIT_TEXTURE_LAST_LEVEL
);
157 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture
, row_stride
,
158 target
, texture_type
,
159 DRAW_JIT_TEXTURE_ROW_STRIDE
);
160 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture
, img_stride
,
161 target
, texture_type
,
162 DRAW_JIT_TEXTURE_IMG_STRIDE
);
163 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture
, data
,
164 target
, texture_type
,
165 DRAW_JIT_TEXTURE_DATA
);
166 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture
, min_lod
,
167 target
, texture_type
,
168 DRAW_JIT_TEXTURE_MIN_LOD
);
169 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture
, max_lod
,
170 target
, texture_type
,
171 DRAW_JIT_TEXTURE_MAX_LOD
);
172 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture
, lod_bias
,
173 target
, texture_type
,
174 DRAW_JIT_TEXTURE_LOD_BIAS
);
175 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture
, border_color
,
176 target
, texture_type
,
177 DRAW_JIT_TEXTURE_BORDER_COLOR
);
179 LP_CHECK_STRUCT_SIZE(struct draw_jit_texture
, target
, texture_type
);
186 * Create LLVM type for struct draw_jit_texture
189 create_jit_context_type(struct gallivm_state
*gallivm
,
190 LLVMTypeRef texture_type
, const char *struct_name
)
192 LLVMTargetDataRef target
= gallivm
->target
;
193 LLVMTypeRef float_type
= LLVMFloatTypeInContext(gallivm
->context
);
194 LLVMTypeRef elem_types
[5];
195 LLVMTypeRef context_type
;
197 elem_types
[0] = LLVMPointerType(float_type
, 0); /* vs_constants */
198 elem_types
[1] = LLVMPointerType(float_type
, 0); /* gs_constants */
199 elem_types
[2] = LLVMPointerType(LLVMArrayType(LLVMArrayType(float_type
, 4),
200 DRAW_TOTAL_CLIP_PLANES
), 0);
201 elem_types
[3] = LLVMPointerType(float_type
, 0); /* viewport */
202 elem_types
[4] = LLVMArrayType(texture_type
,
203 PIPE_MAX_VERTEX_SAMPLERS
); /* textures */
204 #if HAVE_LLVM >= 0x0300
205 context_type
= LLVMStructCreateNamed(gallivm
->context
, struct_name
);
206 LLVMStructSetBody(context_type
, elem_types
,
207 Elements(elem_types
), 0);
209 context_type
= LLVMStructTypeInContext(gallivm
->context
, elem_types
,
210 Elements(elem_types
), 0);
211 LLVMAddTypeName(gallivm
->module
, struct_name
, context_type
);
213 LLVMInvalidateStructLayout(gallivm
->target
, context_type
);
216 LP_CHECK_MEMBER_OFFSET(struct draw_jit_context
, vs_constants
,
217 target
, context_type
, 0);
218 LP_CHECK_MEMBER_OFFSET(struct draw_jit_context
, gs_constants
,
219 target
, context_type
, 1);
220 LP_CHECK_MEMBER_OFFSET(struct draw_jit_context
, planes
,
221 target
, context_type
, 2);
222 LP_CHECK_MEMBER_OFFSET(struct draw_jit_context
, textures
,
223 target
, context_type
,
224 DRAW_JIT_CTX_TEXTURES
);
225 LP_CHECK_STRUCT_SIZE(struct draw_jit_context
,
226 target
, context_type
);
233 * Create LLVM type for struct pipe_vertex_buffer
236 create_jit_vertex_buffer_type(struct gallivm_state
*gallivm
, const char *struct_name
)
238 LLVMTargetDataRef target
= gallivm
->target
;
239 LLVMTypeRef elem_types
[3];
243 elem_types
[1] = LLVMInt32TypeInContext(gallivm
->context
);
244 elem_types
[2] = LLVMPointerType(LLVMInt8TypeInContext(gallivm
->context
), 0); /* vs_constants */
246 #if HAVE_LLVM >= 0x0300
247 vb_type
= LLVMStructCreateNamed(gallivm
->context
, struct_name
);
248 LLVMStructSetBody(vb_type
, elem_types
,
249 Elements(elem_types
), 0);
251 vb_type
= LLVMStructTypeInContext(gallivm
->context
, elem_types
,
252 Elements(elem_types
), 0);
253 LLVMAddTypeName(gallivm
->module
, struct_name
, vb_type
);
255 LLVMInvalidateStructLayout(gallivm
->target
, vb_type
);
258 LP_CHECK_MEMBER_OFFSET(struct pipe_vertex_buffer
, stride
,
260 LP_CHECK_MEMBER_OFFSET(struct pipe_vertex_buffer
, buffer_offset
,
263 LP_CHECK_STRUCT_SIZE(struct pipe_vertex_buffer
, target
, vb_type
);
270 * Create LLVM type for struct vertex_header;
273 create_jit_vertex_header(struct gallivm_state
*gallivm
, int data_elems
)
275 LLVMTargetDataRef target
= gallivm
->target
;
276 LLVMTypeRef elem_types
[4];
277 LLVMTypeRef vertex_header
;
278 char struct_name
[24];
280 util_snprintf(struct_name
, 23, "vertex_header%d", data_elems
);
282 elem_types
[DRAW_JIT_VERTEX_VERTEX_ID
] = LLVMIntTypeInContext(gallivm
->context
, 32);
283 elem_types
[DRAW_JIT_VERTEX_CLIP
] = LLVMArrayType(LLVMFloatTypeInContext(gallivm
->context
), 4);
284 elem_types
[DRAW_JIT_VERTEX_PRE_CLIP_POS
] = LLVMArrayType(LLVMFloatTypeInContext(gallivm
->context
), 4);
285 elem_types
[DRAW_JIT_VERTEX_DATA
] = LLVMArrayType(elem_types
[1], data_elems
);
287 #if HAVE_LLVM >= 0x0300
288 vertex_header
= LLVMStructCreateNamed(gallivm
->context
, struct_name
);
289 LLVMStructSetBody(vertex_header
, elem_types
,
290 Elements(elem_types
), 0);
292 vertex_header
= LLVMStructTypeInContext(gallivm
->context
, elem_types
,
293 Elements(elem_types
), 0);
294 LLVMAddTypeName(gallivm
->module
, struct_name
, vertex_header
);
296 LLVMInvalidateStructLayout(gallivm
->target
, vertex_header
);
299 /* these are bit-fields and we can't take address of them
300 LP_CHECK_MEMBER_OFFSET(struct vertex_header, clipmask,
301 target, vertex_header,
302 DRAW_JIT_VERTEX_CLIPMASK);
303 LP_CHECK_MEMBER_OFFSET(struct vertex_header, edgeflag,
304 target, vertex_header,
305 DRAW_JIT_VERTEX_EDGEFLAG);
306 LP_CHECK_MEMBER_OFFSET(struct vertex_header, pad,
307 target, vertex_header,
308 DRAW_JIT_VERTEX_PAD);
309 LP_CHECK_MEMBER_OFFSET(struct vertex_header, vertex_id,
310 target, vertex_header,
311 DRAW_JIT_VERTEX_VERTEX_ID);
313 LP_CHECK_MEMBER_OFFSET(struct vertex_header
, clip
,
314 target
, vertex_header
,
315 DRAW_JIT_VERTEX_CLIP
);
316 LP_CHECK_MEMBER_OFFSET(struct vertex_header
, pre_clip_pos
,
317 target
, vertex_header
,
318 DRAW_JIT_VERTEX_PRE_CLIP_POS
);
319 LP_CHECK_MEMBER_OFFSET(struct vertex_header
, data
,
320 target
, vertex_header
,
321 DRAW_JIT_VERTEX_DATA
);
323 return vertex_header
;
328 * Create LLVM types for various structures.
331 create_jit_types(struct draw_llvm
*llvm
)
333 struct gallivm_state
*gallivm
= llvm
->gallivm
;
334 LLVMTypeRef texture_type
, context_type
, buffer_type
, vb_type
;
336 texture_type
= create_jit_texture_type(gallivm
, "texture");
338 context_type
= create_jit_context_type(gallivm
, texture_type
, "draw_jit_context");
339 llvm
->context_ptr_type
= LLVMPointerType(context_type
, 0);
341 buffer_type
= LLVMPointerType(LLVMIntTypeInContext(gallivm
->context
, 8), 0);
342 llvm
->buffer_ptr_type
= LLVMPointerType(buffer_type
, 0);
344 vb_type
= create_jit_vertex_buffer_type(gallivm
, "pipe_vertex_buffer");
345 llvm
->vb_ptr_type
= LLVMPointerType(vb_type
, 0);
350 get_context_ptr_type(struct draw_llvm
*llvm
)
352 if (!llvm
->context_ptr_type
)
353 create_jit_types(llvm
);
354 return llvm
->context_ptr_type
;
359 get_buffer_ptr_type(struct draw_llvm
*llvm
)
361 if (!llvm
->buffer_ptr_type
)
362 create_jit_types(llvm
);
363 return llvm
->buffer_ptr_type
;
368 get_vb_ptr_type(struct draw_llvm
*llvm
)
370 if (!llvm
->vb_ptr_type
)
371 create_jit_types(llvm
);
372 return llvm
->vb_ptr_type
;
376 get_vertex_header_ptr_type(struct draw_llvm
*llvm
)
378 if (!llvm
->vertex_header_ptr_type
)
379 create_jit_types(llvm
);
380 return llvm
->vertex_header_ptr_type
;
385 * Create per-context LLVM info.
388 draw_llvm_create(struct draw_context
*draw
, struct gallivm_state
*gallivm
)
390 struct draw_llvm
*llvm
;
392 llvm
= CALLOC_STRUCT( draw_llvm
);
399 llvm
->gallivm
= gallivm
;
401 if (gallivm_debug
& GALLIVM_DEBUG_IR
) {
402 LLVMDumpModule(llvm
->gallivm
->module
);
405 llvm
->nr_variants
= 0;
406 make_empty_list(&llvm
->vs_variants_list
);
408 gallivm_register_garbage_collector_callback(
409 draw_llvm_garbage_collect_callback
, llvm
);
416 * Free per-context LLVM info.
419 draw_llvm_destroy(struct draw_llvm
*llvm
)
421 gallivm_remove_garbage_collector_callback(
422 draw_llvm_garbage_collect_callback
, llvm
);
424 /* XXX free other draw_llvm data? */
430 * Create LLVM-generated code for a vertex shader.
432 struct draw_llvm_variant
*
433 draw_llvm_create_variant(struct draw_llvm
*llvm
,
435 const struct draw_llvm_variant_key
*key
)
437 struct draw_llvm_variant
*variant
;
438 struct llvm_vertex_shader
*shader
=
439 llvm_vertex_shader(llvm
->draw
->vs
.vertex_shader
);
440 LLVMTypeRef vertex_header
;
442 variant
= MALLOC(sizeof *variant
+
443 shader
->variant_key_size
-
444 sizeof variant
->key
);
448 variant
->llvm
= llvm
;
450 memcpy(&variant
->key
, key
, shader
->variant_key_size
);
452 vertex_header
= create_jit_vertex_header(llvm
->gallivm
, num_inputs
);
454 llvm
->vertex_header_ptr_type
= LLVMPointerType(vertex_header
, 0);
456 draw_llvm_generate(llvm
, variant
, FALSE
); /* linear */
457 draw_llvm_generate(llvm
, variant
, TRUE
); /* elts */
459 variant
->shader
= shader
;
460 variant
->list_item_global
.base
= variant
;
461 variant
->list_item_local
.base
= variant
;
462 /*variant->no = */shader
->variants_created
++;
463 variant
->list_item_global
.base
= variant
;
470 generate_vs(struct draw_llvm
*llvm
,
471 LLVMBuilderRef builder
,
472 LLVMValueRef (*outputs
)[TGSI_NUM_CHANNELS
],
473 const LLVMValueRef (*inputs
)[TGSI_NUM_CHANNELS
],
474 LLVMValueRef system_values_array
,
475 LLVMValueRef context_ptr
,
476 struct lp_build_sampler_soa
*draw_sampler
,
477 boolean clamp_vertex_color
)
479 const struct tgsi_token
*tokens
= llvm
->draw
->vs
.vertex_shader
->state
.tokens
;
480 struct lp_type vs_type
;
481 LLVMValueRef consts_ptr
= draw_jit_context_vs_constants(llvm
->gallivm
, context_ptr
);
482 struct lp_build_sampler_soa
*sampler
= 0;
484 memset(&vs_type
, 0, sizeof vs_type
);
485 vs_type
.floating
= TRUE
; /* floating point values */
486 vs_type
.sign
= TRUE
; /* values are signed */
487 vs_type
.norm
= FALSE
; /* values are not limited to [0,1] or [-1,1] */
488 vs_type
.width
= 32; /* 32-bit float */
489 vs_type
.length
= 4; /* 4 elements per vector */
491 num_vs
= 4; /* number of vertices per block */
494 if (gallivm_debug
& GALLIVM_DEBUG_IR
) {
495 tgsi_dump(tokens
, 0);
498 if (llvm
->draw
->num_sampler_views
&& llvm
->draw
->num_samplers
)
499 sampler
= draw_sampler
;
501 lp_build_tgsi_soa(llvm
->gallivm
,
504 NULL
/*struct lp_build_mask_context *mask*/,
511 &llvm
->draw
->vs
.vertex_shader
->info
);
515 unsigned chan
, attrib
;
516 struct lp_build_context bld
;
517 struct tgsi_shader_info
* info
= &llvm
->draw
->vs
.vertex_shader
->info
;
518 lp_build_context_init(&bld
, llvm
->gallivm
, vs_type
);
520 for (attrib
= 0; attrib
< info
->num_outputs
; ++attrib
) {
521 for (chan
= 0; chan
< TGSI_NUM_CHANNELS
; ++chan
) {
522 if (outputs
[attrib
][chan
]) {
523 switch (info
->output_semantic_name
[attrib
]) {
524 case TGSI_SEMANTIC_COLOR
:
525 case TGSI_SEMANTIC_BCOLOR
:
526 if (clamp_vertex_color
) {
527 out
= LLVMBuildLoad(builder
, outputs
[attrib
][chan
], "");
528 out
= lp_build_clamp(&bld
, out
, bld
.zero
, bld
.one
);
529 LLVMBuildStore(builder
, out
, outputs
[attrib
][chan
]);
532 case TGSI_SEMANTIC_FOG
:
533 if (chan
== 1 || chan
== 2)
534 LLVMBuildStore(builder
, bld
.zero
, outputs
[attrib
][chan
]);
536 LLVMBuildStore(builder
, bld
.one
, outputs
[attrib
][chan
]);
547 static void print_vectorf(LLVMBuilderRef builder
,
551 val
[0] = LLVMBuildExtractElement(builder
, vec
,
552 lp_build_const_int32(gallivm
, 0), "");
553 val
[1] = LLVMBuildExtractElement(builder
, vec
,
554 lp_build_const_int32(gallivm
, 1), "");
555 val
[2] = LLVMBuildExtractElement(builder
, vec
,
556 lp_build_const_int32(gallivm
, 2), "");
557 val
[3] = LLVMBuildExtractElement(builder
, vec
,
558 lp_build_const_int32(gallivm
, 3), "");
559 lp_build_printf(builder
, "vector = [%f, %f, %f, %f]\n",
560 val
[0], val
[1], val
[2], val
[3]);
566 generate_fetch(struct gallivm_state
*gallivm
,
567 LLVMValueRef vbuffers_ptr
,
569 struct pipe_vertex_element
*velem
,
572 LLVMValueRef instance_id
)
574 LLVMBuilderRef builder
= gallivm
->builder
;
575 LLVMValueRef indices
=
576 LLVMConstInt(LLVMInt64TypeInContext(gallivm
->context
),
577 velem
->vertex_buffer_index
, 0);
578 LLVMValueRef vbuffer_ptr
= LLVMBuildGEP(builder
, vbuffers_ptr
,
580 LLVMValueRef vb_stride
= draw_jit_vbuffer_stride(gallivm
, vbuf
);
581 LLVMValueRef vb_buffer_offset
= draw_jit_vbuffer_offset(gallivm
, vbuf
);
584 if (velem
->instance_divisor
) {
585 /* array index = instance_id / instance_divisor */
586 index
= LLVMBuildUDiv(builder
, instance_id
,
587 lp_build_const_int32(gallivm
, velem
->instance_divisor
),
591 stride
= LLVMBuildMul(builder
, vb_stride
, index
, "");
593 vbuffer_ptr
= LLVMBuildLoad(builder
, vbuffer_ptr
, "vbuffer");
595 stride
= LLVMBuildAdd(builder
, stride
,
598 stride
= LLVMBuildAdd(builder
, stride
,
599 lp_build_const_int32(gallivm
, velem
->src_offset
),
602 /*lp_build_printf(builder, "vbuf index = %d, stride is %d\n", indices, stride);*/
603 vbuffer_ptr
= LLVMBuildGEP(builder
, vbuffer_ptr
, &stride
, 1, "");
605 *res
= draw_llvm_translate_from(gallivm
, vbuffer_ptr
, velem
->src_format
);
610 aos_to_soa(struct gallivm_state
*gallivm
,
615 LLVMValueRef channel
)
617 LLVMBuilderRef builder
= gallivm
->builder
;
618 LLVMValueRef ex
, res
;
620 ex
= LLVMBuildExtractElement(builder
, val0
,
622 res
= LLVMBuildInsertElement(builder
,
623 LLVMConstNull(LLVMTypeOf(val0
)),
625 lp_build_const_int32(gallivm
, 0),
628 ex
= LLVMBuildExtractElement(builder
, val1
,
630 res
= LLVMBuildInsertElement(builder
,
632 lp_build_const_int32(gallivm
, 1),
635 ex
= LLVMBuildExtractElement(builder
, val2
,
637 res
= LLVMBuildInsertElement(builder
,
639 lp_build_const_int32(gallivm
, 2),
642 ex
= LLVMBuildExtractElement(builder
, val3
,
644 res
= LLVMBuildInsertElement(builder
,
646 lp_build_const_int32(gallivm
, 3),
654 soa_to_aos(struct gallivm_state
*gallivm
,
655 LLVMValueRef soa
[TGSI_NUM_CHANNELS
],
656 LLVMValueRef aos
[TGSI_NUM_CHANNELS
])
658 LLVMBuilderRef builder
= gallivm
->builder
;
662 debug_assert(TGSI_NUM_CHANNELS
== 4);
664 aos
[0] = LLVMConstNull(LLVMTypeOf(soa
[0]));
665 aos
[1] = aos
[2] = aos
[3] = aos
[0];
667 for (i
= 0; i
< TGSI_NUM_CHANNELS
; ++i
) {
668 LLVMValueRef channel
= lp_build_const_int32(gallivm
, i
);
670 comp
= LLVMBuildExtractElement(builder
, soa
[i
],
671 lp_build_const_int32(gallivm
, 0), "");
672 aos
[0] = LLVMBuildInsertElement(builder
, aos
[0], comp
, channel
, "");
674 comp
= LLVMBuildExtractElement(builder
, soa
[i
],
675 lp_build_const_int32(gallivm
, 1), "");
676 aos
[1] = LLVMBuildInsertElement(builder
, aos
[1], comp
, channel
, "");
678 comp
= LLVMBuildExtractElement(builder
, soa
[i
],
679 lp_build_const_int32(gallivm
, 2), "");
680 aos
[2] = LLVMBuildInsertElement(builder
, aos
[2], comp
, channel
, "");
682 comp
= LLVMBuildExtractElement(builder
, soa
[i
],
683 lp_build_const_int32(gallivm
, 3), "");
684 aos
[3] = LLVMBuildInsertElement(builder
, aos
[3], comp
, channel
, "");
691 convert_to_soa(struct gallivm_state
*gallivm
,
692 LLVMValueRef (*aos
)[TGSI_NUM_CHANNELS
],
693 LLVMValueRef (*soa
)[TGSI_NUM_CHANNELS
],
698 debug_assert(TGSI_NUM_CHANNELS
== 4);
700 for (i
= 0; i
< num_attribs
; ++i
) {
701 LLVMValueRef val0
= aos
[i
][0];
702 LLVMValueRef val1
= aos
[i
][1];
703 LLVMValueRef val2
= aos
[i
][2];
704 LLVMValueRef val3
= aos
[i
][3];
706 soa
[i
][0] = aos_to_soa(gallivm
, val0
, val1
, val2
, val3
,
707 lp_build_const_int32(gallivm
, 0));
708 soa
[i
][1] = aos_to_soa(gallivm
, val0
, val1
, val2
, val3
,
709 lp_build_const_int32(gallivm
, 1));
710 soa
[i
][2] = aos_to_soa(gallivm
, val0
, val1
, val2
, val3
,
711 lp_build_const_int32(gallivm
, 2));
712 soa
[i
][3] = aos_to_soa(gallivm
, val0
, val1
, val2
, val3
,
713 lp_build_const_int32(gallivm
, 3));
719 store_aos(struct gallivm_state
*gallivm
,
723 LLVMValueRef clipmask
, boolean have_clipdist
)
725 LLVMBuilderRef builder
= gallivm
->builder
;
726 LLVMValueRef id_ptr
= draw_jit_header_id(gallivm
, io_ptr
);
727 LLVMValueRef data_ptr
= draw_jit_header_data(gallivm
, io_ptr
);
728 LLVMValueRef indices
[3];
730 int vertex_id_pad_edgeflag
;
732 indices
[0] = lp_build_const_int32(gallivm
, 0);
734 indices
[2] = lp_build_const_int32(gallivm
, 0);
736 /* If this assertion fails, it means we need to update the bit twidding
737 * code here. See struct vertex_header in draw_private.h.
739 assert(DRAW_TOTAL_CLIP_PLANES
==14);
740 /* initialize vertex id:16 = 0xffff, have_clipdist:1 = 0, edgeflag:1 = 1 */
741 vertex_id_pad_edgeflag
= (0xffff << 16) | (1 << DRAW_TOTAL_CLIP_PLANES
);
743 vertex_id_pad_edgeflag
|= 1 << (DRAW_TOTAL_CLIP_PLANES
+1);
744 val
= lp_build_const_int32(gallivm
, vertex_id_pad_edgeflag
);
745 /* OR with the clipmask */
746 val
= LLVMBuildOr(builder
, val
, clipmask
, "");
748 /* store vertex header */
749 LLVMBuildStore(builder
, val
, id_ptr
);
753 lp_build_printf(builder
, " ---- %p storing attribute %d (io = %p)\n", data_ptr
, index
, io_ptr
);
756 /*lp_build_printf(builder, " ---- %p storing at %d (%p) ", io_ptr, index, data_ptr);
757 print_vectorf(builder, value);*/
758 data_ptr
= LLVMBuildBitCast(builder
, data_ptr
,
759 LLVMPointerType(LLVMArrayType(LLVMVectorType(LLVMFloatTypeInContext(gallivm
->context
), 4), 0), 0),
761 data_ptr
= LLVMBuildGEP(builder
, data_ptr
, indices
, 2, "");
763 LLVMBuildStore(builder
, value
, data_ptr
);
766 LLVMValueRef x
, y
, z
, w
;
767 LLVMValueRef idx0
, idx1
, idx2
, idx3
;
768 LLVMValueRef gep0
, gep1
, gep2
, gep3
;
769 data_ptr
= LLVMBuildGEP(builder
, data_ptr
, indices
, 3, "");
771 idx0
= lp_build_const_int32(gallivm
, 0);
772 idx1
= lp_build_const_int32(gallivm
, 1);
773 idx2
= lp_build_const_int32(gallivm
, 2);
774 idx3
= lp_build_const_int32(gallivm
, 3);
776 x
= LLVMBuildExtractElement(builder
, value
,
778 y
= LLVMBuildExtractElement(builder
, value
,
780 z
= LLVMBuildExtractElement(builder
, value
,
782 w
= LLVMBuildExtractElement(builder
, value
,
785 gep0
= LLVMBuildGEP(builder
, data_ptr
, &idx0
, 1, "");
786 gep1
= LLVMBuildGEP(builder
, data_ptr
, &idx1
, 1, "");
787 gep2
= LLVMBuildGEP(builder
, data_ptr
, &idx2
, 1, "");
788 gep3
= LLVMBuildGEP(builder
, data_ptr
, &idx3
, 1, "");
790 /*lp_build_printf(builder, "##### x = %f (%p), y = %f (%p), z = %f (%p), w = %f (%p)\n",
791 x, gep0, y, gep1, z, gep2, w, gep3);*/
792 LLVMBuildStore(builder
, x
, gep0
);
793 LLVMBuildStore(builder
, y
, gep1
);
794 LLVMBuildStore(builder
, z
, gep2
);
795 LLVMBuildStore(builder
, w
, gep3
);
802 store_aos_array(struct gallivm_state
*gallivm
,
804 LLVMValueRef aos
[TGSI_NUM_CHANNELS
],
807 LLVMValueRef clipmask
,
808 boolean have_clipdist
)
810 LLVMBuilderRef builder
= gallivm
->builder
;
811 LLVMValueRef attr_index
= lp_build_const_int32(gallivm
, attrib
);
812 LLVMValueRef ind0
= lp_build_const_int32(gallivm
, 0);
813 LLVMValueRef ind1
= lp_build_const_int32(gallivm
, 1);
814 LLVMValueRef ind2
= lp_build_const_int32(gallivm
, 2);
815 LLVMValueRef ind3
= lp_build_const_int32(gallivm
, 3);
816 LLVMValueRef io0_ptr
, io1_ptr
, io2_ptr
, io3_ptr
;
817 LLVMValueRef clipmask0
, clipmask1
, clipmask2
, clipmask3
;
819 debug_assert(TGSI_NUM_CHANNELS
== 4);
821 io0_ptr
= LLVMBuildGEP(builder
, io_ptr
,
823 io1_ptr
= LLVMBuildGEP(builder
, io_ptr
,
825 io2_ptr
= LLVMBuildGEP(builder
, io_ptr
,
827 io3_ptr
= LLVMBuildGEP(builder
, io_ptr
,
830 clipmask0
= LLVMBuildExtractElement(builder
, clipmask
,
832 clipmask1
= LLVMBuildExtractElement(builder
, clipmask
,
834 clipmask2
= LLVMBuildExtractElement(builder
, clipmask
,
836 clipmask3
= LLVMBuildExtractElement(builder
, clipmask
,
840 lp_build_printf(builder
, "io = %p, indexes[%d, %d, %d, %d]\n, clipmask0 = %x, clipmask1 = %x, clipmask2 = %x, clipmask3 = %x\n",
841 io_ptr
, ind0
, ind1
, ind2
, ind3
, clipmask0
, clipmask1
, clipmask2
, clipmask3
);
843 /* store for each of the 4 vertices */
844 store_aos(gallivm
, io0_ptr
, attr_index
, aos
[0], clipmask0
, have_clipdist
);
845 store_aos(gallivm
, io1_ptr
, attr_index
, aos
[1], clipmask1
, have_clipdist
);
846 store_aos(gallivm
, io2_ptr
, attr_index
, aos
[2], clipmask2
, have_clipdist
);
847 store_aos(gallivm
, io3_ptr
, attr_index
, aos
[3], clipmask3
, have_clipdist
);
852 convert_to_aos(struct gallivm_state
*gallivm
,
854 LLVMValueRef (*outputs
)[TGSI_NUM_CHANNELS
],
855 LLVMValueRef clipmask
,
857 int max_vertices
, boolean have_clipdist
)
859 LLVMBuilderRef builder
= gallivm
->builder
;
860 unsigned chan
, attrib
;
863 lp_build_printf(builder
, " # storing begin\n");
865 for (attrib
= 0; attrib
< num_outputs
; ++attrib
) {
868 for (chan
= 0; chan
< TGSI_NUM_CHANNELS
; ++chan
) {
869 if (outputs
[attrib
][chan
]) {
870 LLVMValueRef out
= LLVMBuildLoad(builder
, outputs
[attrib
][chan
], "");
871 lp_build_name(out
, "output%u.%c", attrib
, "xyzw"[chan
]);
872 /*lp_build_printf(builder, "output %d : %d ",
873 LLVMConstInt(LLVMInt32Type(), attrib, 0),
874 LLVMConstInt(LLVMInt32Type(), chan, 0));
875 print_vectorf(builder, out);*/
882 soa_to_aos(gallivm
, soa
, aos
);
883 store_aos_array(gallivm
,
888 clipmask
, have_clipdist
);
891 lp_build_printf(builder
, " # storing end\n");
897 * Stores original vertex positions in clip coordinates
898 * There is probably a more efficient way to do this, 4 floats at once
899 * rather than extracting each element one by one.
900 * idx is the output to store things too, if pre_clip_pos is set
901 * we store the pos to the idx, if not we store the clipvertex to it.
904 store_clip(struct gallivm_state
*gallivm
,
906 LLVMValueRef (*outputs
)[TGSI_NUM_CHANNELS
],
907 boolean pre_clip_pos
, int idx
)
909 LLVMBuilderRef builder
= gallivm
->builder
;
911 LLVMValueRef indices
[2];
912 LLVMValueRef io0_ptr
, io1_ptr
, io2_ptr
, io3_ptr
;
913 LLVMValueRef clip_ptr0
, clip_ptr1
, clip_ptr2
, clip_ptr3
;
914 LLVMValueRef clip0_ptr
, clip1_ptr
, clip2_ptr
, clip3_ptr
;
915 LLVMValueRef out0elem
, out1elem
, out2elem
, out3elem
;
918 LLVMValueRef ind0
= lp_build_const_int32(gallivm
, 0);
919 LLVMValueRef ind1
= lp_build_const_int32(gallivm
, 1);
920 LLVMValueRef ind2
= lp_build_const_int32(gallivm
, 2);
921 LLVMValueRef ind3
= lp_build_const_int32(gallivm
, 3);
924 indices
[1] = lp_build_const_int32(gallivm
, 0);
926 out
[0] = LLVMBuildLoad(builder
, outputs
[idx
][0], ""); /*x0 x1 x2 x3*/
927 out
[1] = LLVMBuildLoad(builder
, outputs
[idx
][1], ""); /*y0 y1 y2 y3*/
928 out
[2] = LLVMBuildLoad(builder
, outputs
[idx
][2], ""); /*z0 z1 z2 z3*/
929 out
[3] = LLVMBuildLoad(builder
, outputs
[idx
][3], ""); /*w0 w1 w2 w3*/
931 io0_ptr
= LLVMBuildGEP(builder
, io_ptr
, &ind0
, 1, "");
932 io1_ptr
= LLVMBuildGEP(builder
, io_ptr
, &ind1
, 1, "");
933 io2_ptr
= LLVMBuildGEP(builder
, io_ptr
, &ind2
, 1, "");
934 io3_ptr
= LLVMBuildGEP(builder
, io_ptr
, &ind3
, 1, "");
937 clip_ptr0
= draw_jit_header_clip(gallivm
, io0_ptr
);
938 clip_ptr1
= draw_jit_header_clip(gallivm
, io1_ptr
);
939 clip_ptr2
= draw_jit_header_clip(gallivm
, io2_ptr
);
940 clip_ptr3
= draw_jit_header_clip(gallivm
, io3_ptr
);
942 clip_ptr0
= draw_jit_header_pre_clip_pos(gallivm
, io0_ptr
);
943 clip_ptr1
= draw_jit_header_pre_clip_pos(gallivm
, io1_ptr
);
944 clip_ptr2
= draw_jit_header_pre_clip_pos(gallivm
, io2_ptr
);
945 clip_ptr3
= draw_jit_header_pre_clip_pos(gallivm
, io3_ptr
);
948 for (i
= 0; i
<4; i
++) {
949 clip0_ptr
= LLVMBuildGEP(builder
, clip_ptr0
, indices
, 2, ""); /* x0 */
950 clip1_ptr
= LLVMBuildGEP(builder
, clip_ptr1
, indices
, 2, ""); /* x1 */
951 clip2_ptr
= LLVMBuildGEP(builder
, clip_ptr2
, indices
, 2, ""); /* x2 */
952 clip3_ptr
= LLVMBuildGEP(builder
, clip_ptr3
, indices
, 2, ""); /* x3 */
954 out0elem
= LLVMBuildExtractElement(builder
, out
[i
], ind0
, ""); /* x0 */
955 out1elem
= LLVMBuildExtractElement(builder
, out
[i
], ind1
, ""); /* x1 */
956 out2elem
= LLVMBuildExtractElement(builder
, out
[i
], ind2
, ""); /* x2 */
957 out3elem
= LLVMBuildExtractElement(builder
, out
[i
], ind3
, ""); /* x3 */
959 LLVMBuildStore(builder
, out0elem
, clip0_ptr
);
960 LLVMBuildStore(builder
, out1elem
, clip1_ptr
);
961 LLVMBuildStore(builder
, out2elem
, clip2_ptr
);
962 LLVMBuildStore(builder
, out3elem
, clip3_ptr
);
964 indices
[1]= LLVMBuildAdd(builder
, indices
[1], ind1
, "");
971 * Equivalent of _mm_set1_ps(a)
974 vec4f_from_scalar(struct gallivm_state
*gallivm
,
978 LLVMTypeRef float_type
= LLVMFloatTypeInContext(gallivm
->context
);
979 LLVMValueRef res
= LLVMGetUndef(LLVMVectorType(float_type
, 4));
982 for (i
= 0; i
< 4; ++i
) {
983 LLVMValueRef index
= lp_build_const_int32(gallivm
, i
);
984 res
= LLVMBuildInsertElement(gallivm
->builder
, res
, a
,
985 index
, i
== 3 ? name
: "");
993 * Transforms the outputs for viewport mapping
996 generate_viewport(struct draw_llvm
*llvm
,
997 LLVMBuilderRef builder
,
998 LLVMValueRef (*outputs
)[TGSI_NUM_CHANNELS
],
999 LLVMValueRef context_ptr
)
1002 struct gallivm_state
*gallivm
= llvm
->gallivm
;
1003 struct lp_type f32_type
= lp_type_float_vec(32);
1004 LLVMValueRef out3
= LLVMBuildLoad(builder
, outputs
[0][3], ""); /*w0 w1 w2 w3*/
1005 LLVMValueRef const1
= lp_build_const_vec(gallivm
, f32_type
, 1.0); /*1.0 1.0 1.0 1.0*/
1006 LLVMValueRef vp_ptr
= draw_jit_context_viewport(gallivm
, context_ptr
);
1008 /* for 1/w convention*/
1009 out3
= LLVMBuildFDiv(builder
, const1
, out3
, "");
1010 LLVMBuildStore(builder
, out3
, outputs
[0][3]);
1012 /* Viewport Mapping */
1013 for (i
=0; i
<3; i
++) {
1014 LLVMValueRef out
= LLVMBuildLoad(builder
, outputs
[0][i
], ""); /*x0 x1 x2 x3*/
1017 LLVMValueRef scale_i
;
1018 LLVMValueRef trans_i
;
1021 index
= lp_build_const_int32(gallivm
, i
);
1022 scale_i
= LLVMBuildGEP(builder
, vp_ptr
, &index
, 1, "");
1024 index
= lp_build_const_int32(gallivm
, i
+4);
1025 trans_i
= LLVMBuildGEP(builder
, vp_ptr
, &index
, 1, "");
1027 scale
= vec4f_from_scalar(gallivm
, LLVMBuildLoad(builder
, scale_i
, ""), "scale");
1028 trans
= vec4f_from_scalar(gallivm
, LLVMBuildLoad(builder
, trans_i
, ""), "trans");
1031 out
= LLVMBuildFMul(builder
, out
, out3
, "");
1033 out
= LLVMBuildFMul(builder
, out
, scale
, "");
1034 /* add translation */
1035 out
= LLVMBuildFAdd(builder
, out
, trans
, "");
1037 /* store transformed outputs */
1038 LLVMBuildStore(builder
, out
, outputs
[0][i
]);
1045 * Returns clipmask as 4xi32 bitmask for the 4 vertices
1048 generate_clipmask(struct draw_llvm
*llvm
,
1049 LLVMValueRef (*outputs
)[TGSI_NUM_CHANNELS
],
1054 unsigned ucp_enable
,
1055 LLVMValueRef context_ptr
,
1056 boolean
*have_clipdist
)
1058 struct gallivm_state
*gallivm
= llvm
->gallivm
;
1059 LLVMBuilderRef builder
= gallivm
->builder
;
1060 LLVMValueRef mask
; /* stores the <4xi32> clipmasks */
1061 LLVMValueRef test
, temp
;
1062 LLVMValueRef zero
, shift
;
1063 LLVMValueRef pos_x
, pos_y
, pos_z
, pos_w
;
1064 LLVMValueRef cv_x
, cv_y
, cv_z
, cv_w
;
1065 LLVMValueRef plane1
, planes
, plane_ptr
, sum
;
1066 struct lp_type f32_type
= lp_type_float_vec(32);
1067 const unsigned pos
= draw_current_shader_position_output(llvm
->draw
);
1068 const unsigned cv
= draw_current_shader_clipvertex_output(llvm
->draw
);
1069 int num_written_clipdistance
= llvm
->draw
->vs
.vertex_shader
->info
.num_written_clipdistance
;
1070 bool have_cd
= false;
1073 cd
[0] = draw_current_shader_clipdistance_output(llvm
->draw
, 0);
1074 cd
[1] = draw_current_shader_clipdistance_output(llvm
->draw
, 1);
1076 if (cd
[0] != pos
|| cd
[1] != pos
)
1079 mask
= lp_build_const_int_vec(gallivm
, lp_type_int_vec(32), 0);
1080 temp
= lp_build_const_int_vec(gallivm
, lp_type_int_vec(32), 0);
1081 zero
= lp_build_const_vec(gallivm
, f32_type
, 0); /* 0.0f 0.0f 0.0f 0.0f */
1082 shift
= lp_build_const_int_vec(gallivm
, lp_type_int_vec(32), 1); /* 1 1 1 1 */
1085 * load clipvertex and position from correct locations.
1086 * if they are the same just load them once.
1088 pos_x
= LLVMBuildLoad(builder
, outputs
[pos
][0], ""); /*x0 x1 x2 x3*/
1089 pos_y
= LLVMBuildLoad(builder
, outputs
[pos
][1], ""); /*y0 y1 y2 y3*/
1090 pos_z
= LLVMBuildLoad(builder
, outputs
[pos
][2], ""); /*z0 z1 z2 z3*/
1091 pos_w
= LLVMBuildLoad(builder
, outputs
[pos
][3], ""); /*w0 w1 w2 w3*/
1093 if (clip_user
&& cv
!= pos
) {
1094 cv_x
= LLVMBuildLoad(builder
, outputs
[cv
][0], ""); /*x0 x1 x2 x3*/
1095 cv_y
= LLVMBuildLoad(builder
, outputs
[cv
][1], ""); /*y0 y1 y2 y3*/
1096 cv_z
= LLVMBuildLoad(builder
, outputs
[cv
][2], ""); /*z0 z1 z2 z3*/
1097 cv_w
= LLVMBuildLoad(builder
, outputs
[cv
][3], ""); /*w0 w1 w2 w3*/
1105 /* Cliptest, for hardwired planes */
1108 test
= lp_build_compare(gallivm
, f32_type
, PIPE_FUNC_GREATER
, pos_x
, pos_w
);
1110 test
= LLVMBuildAnd(builder
, test
, temp
, "");
1114 test
= LLVMBuildFAdd(builder
, pos_x
, pos_w
, "");
1115 test
= lp_build_compare(gallivm
, f32_type
, PIPE_FUNC_GREATER
, zero
, test
);
1116 temp
= LLVMBuildShl(builder
, temp
, shift
, "");
1117 test
= LLVMBuildAnd(builder
, test
, temp
, "");
1118 mask
= LLVMBuildOr(builder
, mask
, test
, "");
1121 test
= lp_build_compare(gallivm
, f32_type
, PIPE_FUNC_GREATER
, pos_y
, pos_w
);
1122 temp
= LLVMBuildShl(builder
, temp
, shift
, "");
1123 test
= LLVMBuildAnd(builder
, test
, temp
, "");
1124 mask
= LLVMBuildOr(builder
, mask
, test
, "");
1127 test
= LLVMBuildFAdd(builder
, pos_y
, pos_w
, "");
1128 test
= lp_build_compare(gallivm
, f32_type
, PIPE_FUNC_GREATER
, zero
, test
);
1129 temp
= LLVMBuildShl(builder
, temp
, shift
, "");
1130 test
= LLVMBuildAnd(builder
, test
, temp
, "");
1131 mask
= LLVMBuildOr(builder
, mask
, test
, "");
1135 temp
= lp_build_const_int_vec(gallivm
, lp_type_int_vec(32), 16);
1138 test
= lp_build_compare(gallivm
, f32_type
, PIPE_FUNC_GREATER
, zero
, pos_z
);
1139 test
= LLVMBuildAnd(builder
, test
, temp
, "");
1140 mask
= LLVMBuildOr(builder
, mask
, test
, "");
1144 test
= LLVMBuildFAdd(builder
, pos_z
, pos_w
, "");
1145 test
= lp_build_compare(gallivm
, f32_type
, PIPE_FUNC_GREATER
, zero
, test
);
1146 test
= LLVMBuildAnd(builder
, test
, temp
, "");
1147 mask
= LLVMBuildOr(builder
, mask
, test
, "");
1150 test
= lp_build_compare(gallivm
, f32_type
, PIPE_FUNC_GREATER
, pos_z
, pos_w
);
1151 temp
= LLVMBuildShl(builder
, temp
, shift
, "");
1152 test
= LLVMBuildAnd(builder
, test
, temp
, "");
1153 mask
= LLVMBuildOr(builder
, mask
, test
, "");
1157 LLVMValueRef planes_ptr
= draw_jit_context_planes(gallivm
, context_ptr
);
1158 LLVMValueRef indices
[3];
1160 /* userclip planes */
1161 while (ucp_enable
) {
1162 unsigned plane_idx
= ffs(ucp_enable
)-1;
1163 ucp_enable
&= ~(1 << plane_idx
);
1166 if (have_cd
&& num_written_clipdistance
) {
1167 LLVMValueRef clipdist
;
1171 *have_clipdist
= TRUE
;
1173 clipdist
= LLVMBuildLoad(builder
, outputs
[cd
[0]][i
], "");
1175 clipdist
= LLVMBuildLoad(builder
, outputs
[cd
[1]][i
-4], "");
1177 test
= lp_build_compare(gallivm
, f32_type
, PIPE_FUNC_GREATER
, zero
, clipdist
);
1178 temp
= lp_build_const_int_vec(gallivm
, lp_type_int_vec(32), 1 << plane_idx
);
1179 test
= LLVMBuildAnd(builder
, test
, temp
, "");
1180 mask
= LLVMBuildOr(builder
, mask
, test
, "");
1182 indices
[0] = lp_build_const_int32(gallivm
, 0);
1183 indices
[1] = lp_build_const_int32(gallivm
, plane_idx
);
1185 indices
[2] = lp_build_const_int32(gallivm
, 0);
1186 plane_ptr
= LLVMBuildGEP(builder
, planes_ptr
, indices
, 3, "");
1187 plane1
= LLVMBuildLoad(builder
, plane_ptr
, "plane_x");
1188 planes
= vec4f_from_scalar(gallivm
, plane1
, "plane4_x");
1189 sum
= LLVMBuildFMul(builder
, planes
, cv_x
, "");
1191 indices
[2] = lp_build_const_int32(gallivm
, 1);
1192 plane_ptr
= LLVMBuildGEP(builder
, planes_ptr
, indices
, 3, "");
1193 plane1
= LLVMBuildLoad(builder
, plane_ptr
, "plane_y");
1194 planes
= vec4f_from_scalar(gallivm
, plane1
, "plane4_y");
1195 test
= LLVMBuildFMul(builder
, planes
, cv_y
, "");
1196 sum
= LLVMBuildFAdd(builder
, sum
, test
, "");
1198 indices
[2] = lp_build_const_int32(gallivm
, 2);
1199 plane_ptr
= LLVMBuildGEP(builder
, planes_ptr
, indices
, 3, "");
1200 plane1
= LLVMBuildLoad(builder
, plane_ptr
, "plane_z");
1201 planes
= vec4f_from_scalar(gallivm
, plane1
, "plane4_z");
1202 test
= LLVMBuildFMul(builder
, planes
, cv_z
, "");
1203 sum
= LLVMBuildFAdd(builder
, sum
, test
, "");
1205 indices
[2] = lp_build_const_int32(gallivm
, 3);
1206 plane_ptr
= LLVMBuildGEP(builder
, planes_ptr
, indices
, 3, "");
1207 plane1
= LLVMBuildLoad(builder
, plane_ptr
, "plane_w");
1208 planes
= vec4f_from_scalar(gallivm
, plane1
, "plane4_w");
1209 test
= LLVMBuildFMul(builder
, planes
, cv_w
, "");
1210 sum
= LLVMBuildFAdd(builder
, sum
, test
, "");
1212 test
= lp_build_compare(gallivm
, f32_type
, PIPE_FUNC_GREATER
, zero
, sum
);
1213 temp
= lp_build_const_int_vec(gallivm
, lp_type_int_vec(32), 1 << plane_idx
);
1214 test
= LLVMBuildAnd(builder
, test
, temp
, "");
1215 mask
= LLVMBuildOr(builder
, mask
, test
, "");
1224 * Returns boolean if any clipping has occurred
1225 * Used zero/non-zero i32 value to represent boolean
1228 clipmask_bool(struct gallivm_state
*gallivm
,
1229 LLVMValueRef clipmask
,
1230 LLVMValueRef ret_ptr
)
1232 LLVMBuilderRef builder
= gallivm
->builder
;
1233 LLVMValueRef ret
= LLVMBuildLoad(builder
, ret_ptr
, "");
1237 for (i
=0; i
<4; i
++) {
1238 temp
= LLVMBuildExtractElement(builder
, clipmask
,
1239 lp_build_const_int32(gallivm
, i
) , "");
1240 ret
= LLVMBuildOr(builder
, ret
, temp
, "");
1243 LLVMBuildStore(builder
, ret
, ret_ptr
);
1248 draw_llvm_generate(struct draw_llvm
*llvm
, struct draw_llvm_variant
*variant
,
1251 struct gallivm_state
*gallivm
= llvm
->gallivm
;
1252 LLVMContextRef context
= gallivm
->context
;
1253 LLVMTypeRef int32_type
= LLVMInt32TypeInContext(context
);
1254 LLVMTypeRef arg_types
[8];
1255 LLVMTypeRef func_type
;
1256 LLVMValueRef context_ptr
;
1257 LLVMBasicBlockRef block
;
1258 LLVMBuilderRef builder
;
1259 LLVMValueRef end
, start
;
1260 LLVMValueRef count
, fetch_elts
, fetch_count
;
1261 LLVMValueRef stride
, step
, io_itr
;
1262 LLVMValueRef io_ptr
, vbuffers_ptr
, vb_ptr
;
1263 LLVMValueRef instance_id
;
1264 LLVMValueRef system_values_array
;
1265 LLVMValueRef zero
= lp_build_const_int32(gallivm
, 0);
1266 LLVMValueRef one
= lp_build_const_int32(gallivm
, 1);
1267 struct draw_context
*draw
= llvm
->draw
;
1268 const struct tgsi_shader_info
*vs_info
= &draw
->vs
.vertex_shader
->info
;
1270 struct lp_build_context bld
;
1271 struct lp_build_loop_state lp_loop
;
1272 const int max_vertices
= 4;
1273 LLVMValueRef outputs
[PIPE_MAX_SHADER_OUTPUTS
][TGSI_NUM_CHANNELS
];
1274 LLVMValueRef fetch_max
;
1276 struct lp_build_sampler_soa
*sampler
= 0;
1277 LLVMValueRef ret
, ret_ptr
;
1278 const boolean bypass_viewport
= variant
->key
.bypass_viewport
;
1279 const boolean enable_cliptest
= variant
->key
.clip_xy
||
1280 variant
->key
.clip_z
||
1281 variant
->key
.clip_user
;
1282 LLVMValueRef variant_func
;
1283 const unsigned pos
= draw_current_shader_position_output(llvm
->draw
);
1284 const unsigned cv
= draw_current_shader_clipvertex_output(llvm
->draw
);
1285 boolean have_clipdist
= FALSE
;
1287 arg_types
[0] = get_context_ptr_type(llvm
); /* context */
1288 arg_types
[1] = get_vertex_header_ptr_type(llvm
); /* vertex_header */
1289 arg_types
[2] = get_buffer_ptr_type(llvm
); /* vbuffers */
1291 arg_types
[3] = LLVMPointerType(int32_type
, 0);/* fetch_elts * */
1293 arg_types
[3] = int32_type
; /* start */
1294 arg_types
[4] = int32_type
; /* fetch_count / count */
1295 arg_types
[5] = int32_type
; /* stride */
1296 arg_types
[6] = get_vb_ptr_type(llvm
); /* pipe_vertex_buffer's */
1297 arg_types
[7] = int32_type
; /* instance_id */
1299 func_type
= LLVMFunctionType(int32_type
, arg_types
, Elements(arg_types
), 0);
1301 variant_func
= LLVMAddFunction(gallivm
->module
,
1302 elts
? "draw_llvm_shader_elts" : "draw_llvm_shader",
1306 variant
->function_elts
= variant_func
;
1308 variant
->function
= variant_func
;
1310 LLVMSetFunctionCallConv(variant_func
, LLVMCCallConv
);
1311 for (i
= 0; i
< Elements(arg_types
); ++i
)
1312 if (LLVMGetTypeKind(arg_types
[i
]) == LLVMPointerTypeKind
)
1313 LLVMAddAttribute(LLVMGetParam(variant_func
, i
),
1314 LLVMNoAliasAttribute
);
1316 context_ptr
= LLVMGetParam(variant_func
, 0);
1317 io_ptr
= LLVMGetParam(variant_func
, 1);
1318 vbuffers_ptr
= LLVMGetParam(variant_func
, 2);
1319 stride
= LLVMGetParam(variant_func
, 5);
1320 vb_ptr
= LLVMGetParam(variant_func
, 6);
1321 instance_id
= LLVMGetParam(variant_func
, 7);
1323 lp_build_name(context_ptr
, "context");
1324 lp_build_name(io_ptr
, "io");
1325 lp_build_name(vbuffers_ptr
, "vbuffers");
1326 lp_build_name(stride
, "stride");
1327 lp_build_name(vb_ptr
, "vb");
1328 lp_build_name(instance_id
, "instance_id");
1331 fetch_elts
= LLVMGetParam(variant_func
, 3);
1332 fetch_count
= LLVMGetParam(variant_func
, 4);
1333 lp_build_name(fetch_elts
, "fetch_elts");
1334 lp_build_name(fetch_count
, "fetch_count");
1335 start
= count
= NULL
;
1338 start
= LLVMGetParam(variant_func
, 3);
1339 count
= LLVMGetParam(variant_func
, 4);
1340 lp_build_name(start
, "start");
1341 lp_build_name(count
, "count");
1342 fetch_elts
= fetch_count
= NULL
;
1349 block
= LLVMAppendBasicBlockInContext(gallivm
->context
, variant_func
, "entry");
1350 builder
= gallivm
->builder
;
1351 LLVMPositionBuilderAtEnd(builder
, block
);
1353 lp_build_context_init(&bld
, gallivm
, lp_type_int(32));
1355 system_values_array
= lp_build_system_values_array(gallivm
, vs_info
,
1358 /* function will return non-zero i32 value if any clipped vertices */
1359 ret_ptr
= lp_build_alloca(gallivm
, int32_type
, "");
1360 LLVMBuildStore(builder
, zero
, ret_ptr
);
1362 /* code generated texture sampling */
1363 sampler
= draw_llvm_sampler_soa_create(
1364 draw_llvm_variant_key_samplers(&variant
->key
),
1372 end
= lp_build_add(&bld
, start
, count
);
1375 step
= lp_build_const_int32(gallivm
, max_vertices
);
1377 fetch_max
= LLVMBuildSub(builder
, end
, one
, "fetch_max");
1379 lp_build_loop_begin(&lp_loop
, gallivm
, start
);
1381 LLVMValueRef inputs
[PIPE_MAX_SHADER_INPUTS
][TGSI_NUM_CHANNELS
];
1382 LLVMValueRef aos_attribs
[PIPE_MAX_SHADER_INPUTS
][TGSI_NUM_CHANNELS
] = { { 0 } };
1384 LLVMValueRef clipmask
; /* holds the clipmask value */
1385 const LLVMValueRef (*ptr_aos
)[TGSI_NUM_CHANNELS
];
1388 io_itr
= lp_loop
.counter
;
1390 io_itr
= LLVMBuildSub(builder
, lp_loop
.counter
, start
, "");
1392 io
= LLVMBuildGEP(builder
, io_ptr
, &io_itr
, 1, "");
1394 lp_build_printf(builder
, " --- io %d = %p, loop counter %d\n",
1395 io_itr
, io
, lp_loop
.counter
);
1397 for (i
= 0; i
< TGSI_NUM_CHANNELS
; ++i
) {
1398 LLVMValueRef true_index
=
1399 LLVMBuildAdd(builder
,
1401 lp_build_const_int32(gallivm
, i
), "");
1403 /* make sure we're not out of bounds which can happen
1404 * if fetch_count % 4 != 0, because on the last iteration
1405 * a few of the 4 vertex fetches will be out of bounds */
1406 true_index
= lp_build_min(&bld
, true_index
, fetch_max
);
1409 LLVMValueRef fetch_ptr
;
1410 fetch_ptr
= LLVMBuildGEP(builder
, fetch_elts
,
1411 &true_index
, 1, "");
1412 true_index
= LLVMBuildLoad(builder
, fetch_ptr
, "fetch_elt");
1415 for (j
= 0; j
< draw
->pt
.nr_vertex_elements
; ++j
) {
1416 struct pipe_vertex_element
*velem
= &draw
->pt
.vertex_element
[j
];
1417 LLVMValueRef vb_index
=
1418 lp_build_const_int32(gallivm
, velem
->vertex_buffer_index
);
1419 LLVMValueRef vb
= LLVMBuildGEP(builder
, vb_ptr
, &vb_index
, 1, "");
1420 generate_fetch(gallivm
, vbuffers_ptr
,
1421 &aos_attribs
[j
][i
], velem
, vb
, true_index
,
1425 convert_to_soa(gallivm
, aos_attribs
, inputs
,
1426 draw
->pt
.nr_vertex_elements
);
1428 ptr_aos
= (const LLVMValueRef (*)[TGSI_NUM_CHANNELS
]) inputs
;
1433 system_values_array
,
1436 variant
->key
.clamp_vertex_color
);
1438 /* store original positions in clip before further manipulation */
1439 store_clip(gallivm
, io
, outputs
, 0, cv
);
1440 store_clip(gallivm
, io
, outputs
, 1, pos
);
1443 if (enable_cliptest
) {
1444 /* allocate clipmask, assign it integer type */
1445 clipmask
= generate_clipmask(llvm
, outputs
,
1446 variant
->key
.clip_xy
,
1447 variant
->key
.clip_z
,
1448 variant
->key
.clip_user
,
1449 variant
->key
.clip_halfz
,
1450 variant
->key
.ucp_enable
,
1451 context_ptr
, &have_clipdist
);
1452 /* return clipping boolean value for function */
1453 clipmask_bool(gallivm
, clipmask
, ret_ptr
);
1456 clipmask
= lp_build_const_int_vec(gallivm
, lp_type_int_vec(32), 0);
1459 /* do viewport mapping */
1460 if (!bypass_viewport
) {
1461 generate_viewport(llvm
, builder
, outputs
, context_ptr
);
1464 /* store clipmask in vertex header,
1465 * original positions in clip
1466 * and transformed positions in data
1468 convert_to_aos(gallivm
, io
, outputs
, clipmask
,
1469 vs_info
->num_outputs
, max_vertices
, have_clipdist
);
1472 lp_build_loop_end_cond(&lp_loop
, end
, step
, LLVMIntUGE
);
1474 sampler
->destroy(sampler
);
1476 ret
= LLVMBuildLoad(builder
, ret_ptr
, "");
1477 LLVMBuildRet(builder
, ret
);
1480 * Translate the LLVM IR into machine code.
1483 if (LLVMVerifyFunction(variant_func
, LLVMPrintMessageAction
)) {
1484 lp_debug_dump_value(variant_func
);
1489 LLVMRunFunctionPassManager(gallivm
->passmgr
, variant_func
);
1491 if (gallivm_debug
& GALLIVM_DEBUG_IR
) {
1492 lp_debug_dump_value(variant_func
);
1496 code
= LLVMGetPointerToGlobal(gallivm
->engine
, variant_func
);
1498 variant
->jit_func_elts
= (draw_jit_vert_func_elts
) pointer_to_func(code
);
1500 variant
->jit_func
= (draw_jit_vert_func
) pointer_to_func(code
);
1502 if (gallivm_debug
& GALLIVM_DEBUG_ASM
) {
1503 lp_disassemble(code
);
1505 lp_func_delete_body(variant_func
);
1509 struct draw_llvm_variant_key
*
1510 draw_llvm_make_variant_key(struct draw_llvm
*llvm
, char *store
)
1513 struct draw_llvm_variant_key
*key
;
1514 struct lp_sampler_static_state
*sampler
;
1516 key
= (struct draw_llvm_variant_key
*)store
;
1518 key
->clamp_vertex_color
= llvm
->draw
->rasterizer
->clamp_vertex_color
; /**/
1520 /* Presumably all variants of the shader should have the same
1521 * number of vertex elements - ie the number of shader inputs.
1523 key
->nr_vertex_elements
= llvm
->draw
->pt
.nr_vertex_elements
;
1525 /* will have to rig this up properly later */
1526 key
->clip_xy
= llvm
->draw
->clip_xy
;
1527 key
->clip_z
= llvm
->draw
->clip_z
;
1528 key
->clip_user
= llvm
->draw
->clip_user
;
1529 key
->bypass_viewport
= llvm
->draw
->identity_viewport
;
1530 key
->clip_halfz
= !llvm
->draw
->rasterizer
->gl_rasterization_rules
;
1531 key
->need_edgeflags
= (llvm
->draw
->vs
.edgeflag_output
? TRUE
: FALSE
);
1532 key
->ucp_enable
= llvm
->draw
->rasterizer
->clip_plane_enable
;
1535 /* All variants of this shader will have the same value for
1536 * nr_samplers. Not yet trying to compact away holes in the
1539 key
->nr_samplers
= llvm
->draw
->vs
.vertex_shader
->info
.file_max
[TGSI_FILE_SAMPLER
] + 1;
1541 sampler
= draw_llvm_variant_key_samplers(key
);
1543 memcpy(key
->vertex_element
,
1544 llvm
->draw
->pt
.vertex_element
,
1545 sizeof(struct pipe_vertex_element
) * key
->nr_vertex_elements
);
1547 memset(sampler
, 0, key
->nr_samplers
* sizeof *sampler
);
1549 for (i
= 0 ; i
< key
->nr_samplers
; i
++) {
1550 lp_sampler_static_state(&sampler
[i
],
1551 llvm
->draw
->sampler_views
[i
],
1552 llvm
->draw
->samplers
[i
]);
1560 draw_llvm_set_mapped_texture(struct draw_context
*draw
,
1561 unsigned sampler_idx
,
1562 uint32_t width
, uint32_t height
, uint32_t depth
,
1563 uint32_t first_level
, uint32_t last_level
,
1564 uint32_t row_stride
[PIPE_MAX_TEXTURE_LEVELS
],
1565 uint32_t img_stride
[PIPE_MAX_TEXTURE_LEVELS
],
1566 const void *data
[PIPE_MAX_TEXTURE_LEVELS
])
1569 struct draw_jit_texture
*jit_tex
;
1571 assert(sampler_idx
< PIPE_MAX_VERTEX_SAMPLERS
);
1573 jit_tex
= &draw
->llvm
->jit_context
.textures
[sampler_idx
];
1575 jit_tex
->width
= width
;
1576 jit_tex
->height
= height
;
1577 jit_tex
->depth
= depth
;
1578 jit_tex
->first_level
= first_level
;
1579 jit_tex
->last_level
= last_level
;
1581 for (j
= first_level
; j
<= last_level
; j
++) {
1582 jit_tex
->data
[j
] = data
[j
];
1583 jit_tex
->row_stride
[j
] = row_stride
[j
];
1584 jit_tex
->img_stride
[j
] = img_stride
[j
];
1590 draw_llvm_set_sampler_state(struct draw_context
*draw
)
1594 for (i
= 0; i
< draw
->num_samplers
; i
++) {
1595 struct draw_jit_texture
*jit_tex
= &draw
->llvm
->jit_context
.textures
[i
];
1597 if (draw
->samplers
[i
]) {
1598 jit_tex
->min_lod
= draw
->samplers
[i
]->min_lod
;
1599 jit_tex
->max_lod
= draw
->samplers
[i
]->max_lod
;
1600 jit_tex
->lod_bias
= draw
->samplers
[i
]->lod_bias
;
1601 COPY_4V(jit_tex
->border_color
, draw
->samplers
[i
]->border_color
.f
);
1608 draw_llvm_destroy_variant(struct draw_llvm_variant
*variant
)
1610 struct draw_llvm
*llvm
= variant
->llvm
;
1612 if (variant
->function_elts
) {
1613 LLVMFreeMachineCodeForFunction(llvm
->gallivm
->engine
,
1614 variant
->function_elts
);
1615 LLVMDeleteFunction(variant
->function_elts
);
1618 if (variant
->function
) {
1619 LLVMFreeMachineCodeForFunction(llvm
->gallivm
->engine
,
1621 LLVMDeleteFunction(variant
->function
);
1624 remove_from_list(&variant
->list_item_local
);
1625 variant
->shader
->variants_cached
--;
1626 remove_from_list(&variant
->list_item_global
);
1627 llvm
->nr_variants
--;