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
);
92 draw_llvm_generate_elts(struct draw_llvm
*llvm
, struct draw_llvm_variant
*var
);
96 * Create LLVM type for struct draw_jit_texture
99 create_jit_texture_type(struct gallivm_state
*gallivm
, const char *struct_name
)
101 LLVMTargetDataRef target
= gallivm
->target
;
102 LLVMTypeRef texture_type
;
103 LLVMTypeRef elem_types
[DRAW_JIT_TEXTURE_NUM_FIELDS
];
104 LLVMTypeRef int32_type
= LLVMInt32TypeInContext(gallivm
->context
);
106 elem_types
[DRAW_JIT_TEXTURE_WIDTH
] =
107 elem_types
[DRAW_JIT_TEXTURE_HEIGHT
] =
108 elem_types
[DRAW_JIT_TEXTURE_DEPTH
] =
109 elem_types
[DRAW_JIT_TEXTURE_FIRST_LEVEL
] =
110 elem_types
[DRAW_JIT_TEXTURE_LAST_LEVEL
] = int32_type
;
111 elem_types
[DRAW_JIT_TEXTURE_ROW_STRIDE
] =
112 elem_types
[DRAW_JIT_TEXTURE_IMG_STRIDE
] =
113 LLVMArrayType(int32_type
, PIPE_MAX_TEXTURE_LEVELS
);
114 elem_types
[DRAW_JIT_TEXTURE_DATA
] =
115 LLVMArrayType(LLVMPointerType(LLVMInt8TypeInContext(gallivm
->context
), 0),
116 PIPE_MAX_TEXTURE_LEVELS
);
117 elem_types
[DRAW_JIT_TEXTURE_MIN_LOD
] =
118 elem_types
[DRAW_JIT_TEXTURE_MAX_LOD
] =
119 elem_types
[DRAW_JIT_TEXTURE_LOD_BIAS
] = LLVMFloatTypeInContext(gallivm
->context
);
120 elem_types
[DRAW_JIT_TEXTURE_BORDER_COLOR
] =
121 LLVMArrayType(LLVMFloatTypeInContext(gallivm
->context
), 4);
123 #if HAVE_LLVM >= 0x0300
124 texture_type
= LLVMStructCreateNamed(gallivm
->context
, struct_name
);
125 LLVMStructSetBody(texture_type
, elem_types
,
126 Elements(elem_types
), 0);
128 texture_type
= LLVMStructTypeInContext(gallivm
->context
, elem_types
,
129 Elements(elem_types
), 0);
131 LLVMAddTypeName(gallivm
->module
, struct_name
, texture_type
);
133 /* Make sure the target's struct layout cache doesn't return
134 * stale/invalid data.
136 LLVMInvalidateStructLayout(gallivm
->target
, texture_type
);
139 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture
, width
,
140 target
, texture_type
,
141 DRAW_JIT_TEXTURE_WIDTH
);
142 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture
, height
,
143 target
, texture_type
,
144 DRAW_JIT_TEXTURE_HEIGHT
);
145 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture
, depth
,
146 target
, texture_type
,
147 DRAW_JIT_TEXTURE_DEPTH
);
148 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture
, first_level
,
149 target
, texture_type
,
150 DRAW_JIT_TEXTURE_FIRST_LEVEL
);
151 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture
, last_level
,
152 target
, texture_type
,
153 DRAW_JIT_TEXTURE_LAST_LEVEL
);
154 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture
, row_stride
,
155 target
, texture_type
,
156 DRAW_JIT_TEXTURE_ROW_STRIDE
);
157 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture
, img_stride
,
158 target
, texture_type
,
159 DRAW_JIT_TEXTURE_IMG_STRIDE
);
160 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture
, data
,
161 target
, texture_type
,
162 DRAW_JIT_TEXTURE_DATA
);
163 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture
, min_lod
,
164 target
, texture_type
,
165 DRAW_JIT_TEXTURE_MIN_LOD
);
166 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture
, max_lod
,
167 target
, texture_type
,
168 DRAW_JIT_TEXTURE_MAX_LOD
);
169 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture
, lod_bias
,
170 target
, texture_type
,
171 DRAW_JIT_TEXTURE_LOD_BIAS
);
172 LP_CHECK_MEMBER_OFFSET(struct draw_jit_texture
, border_color
,
173 target
, texture_type
,
174 DRAW_JIT_TEXTURE_BORDER_COLOR
);
176 LP_CHECK_STRUCT_SIZE(struct draw_jit_texture
, target
, texture_type
);
183 * Create LLVM type for struct draw_jit_texture
186 create_jit_context_type(struct gallivm_state
*gallivm
,
187 LLVMTypeRef texture_type
, const char *struct_name
)
189 LLVMTargetDataRef target
= gallivm
->target
;
190 LLVMTypeRef float_type
= LLVMFloatTypeInContext(gallivm
->context
);
191 LLVMTypeRef elem_types
[5];
192 LLVMTypeRef context_type
;
194 elem_types
[0] = LLVMPointerType(float_type
, 0); /* vs_constants */
195 elem_types
[1] = LLVMPointerType(float_type
, 0); /* gs_constants */
196 elem_types
[2] = LLVMPointerType(LLVMArrayType(LLVMArrayType(float_type
, 4), 12), 0); /* planes */
197 elem_types
[3] = LLVMPointerType(float_type
, 0); /* viewport */
198 elem_types
[4] = LLVMArrayType(texture_type
,
199 PIPE_MAX_VERTEX_SAMPLERS
); /* textures */
200 #if HAVE_LLVM >= 0x0300
201 context_type
= LLVMStructCreateNamed(gallivm
->context
, struct_name
);
202 LLVMStructSetBody(context_type
, elem_types
,
203 Elements(elem_types
), 0);
205 context_type
= LLVMStructTypeInContext(gallivm
->context
, elem_types
,
206 Elements(elem_types
), 0);
207 LLVMAddTypeName(gallivm
->module
, struct_name
, context_type
);
209 LLVMInvalidateStructLayout(gallivm
->target
, context_type
);
212 LP_CHECK_MEMBER_OFFSET(struct draw_jit_context
, vs_constants
,
213 target
, context_type
, 0);
214 LP_CHECK_MEMBER_OFFSET(struct draw_jit_context
, gs_constants
,
215 target
, context_type
, 1);
216 LP_CHECK_MEMBER_OFFSET(struct draw_jit_context
, planes
,
217 target
, context_type
, 2);
218 LP_CHECK_MEMBER_OFFSET(struct draw_jit_context
, textures
,
219 target
, context_type
,
220 DRAW_JIT_CTX_TEXTURES
);
221 LP_CHECK_STRUCT_SIZE(struct draw_jit_context
,
222 target
, context_type
);
229 * Create LLVM type for struct pipe_vertex_buffer
232 create_jit_vertex_buffer_type(struct gallivm_state
*gallivm
, const char *struct_name
)
234 LLVMTargetDataRef target
= gallivm
->target
;
235 LLVMTypeRef elem_types
[3];
239 elem_types
[1] = LLVMInt32TypeInContext(gallivm
->context
);
240 elem_types
[2] = LLVMPointerType(LLVMInt8TypeInContext(gallivm
->context
), 0); /* vs_constants */
242 #if HAVE_LLVM >= 0x0300
243 vb_type
= LLVMStructCreateNamed(gallivm
->context
, struct_name
);
244 LLVMStructSetBody(vb_type
, elem_types
,
245 Elements(elem_types
), 0);
247 vb_type
= LLVMStructTypeInContext(gallivm
->context
, elem_types
,
248 Elements(elem_types
), 0);
249 LLVMAddTypeName(gallivm
->module
, struct_name
, vb_type
);
251 LLVMInvalidateStructLayout(gallivm
->target
, vb_type
);
254 LP_CHECK_MEMBER_OFFSET(struct pipe_vertex_buffer
, stride
,
256 LP_CHECK_MEMBER_OFFSET(struct pipe_vertex_buffer
, buffer_offset
,
259 LP_CHECK_STRUCT_SIZE(struct pipe_vertex_buffer
, target
, vb_type
);
266 * Create LLVM type for struct vertex_header;
269 create_jit_vertex_header(struct gallivm_state
*gallivm
, int data_elems
)
271 LLVMTargetDataRef target
= gallivm
->target
;
272 LLVMTypeRef elem_types
[3];
273 LLVMTypeRef vertex_header
;
274 char struct_name
[24];
276 util_snprintf(struct_name
, 23, "vertex_header%d", data_elems
);
278 elem_types
[0] = LLVMIntTypeInContext(gallivm
->context
, 32);
279 elem_types
[1] = LLVMArrayType(LLVMFloatTypeInContext(gallivm
->context
), 4);
280 elem_types
[2] = LLVMArrayType(elem_types
[1], data_elems
);
282 #if HAVE_LLVM >= 0x0300
283 vertex_header
= LLVMStructCreateNamed(gallivm
->context
, struct_name
);
284 LLVMStructSetBody(vertex_header
, elem_types
,
285 Elements(elem_types
), 0);
287 vertex_header
= LLVMStructTypeInContext(gallivm
->context
, elem_types
,
288 Elements(elem_types
), 0);
289 LLVMAddTypeName(gallivm
->module
, struct_name
, vertex_header
);
291 LLVMInvalidateStructLayout(gallivm
->target
, vertex_header
);
294 /* these are bit-fields and we can't take address of them
295 LP_CHECK_MEMBER_OFFSET(struct vertex_header, clipmask,
296 target, vertex_header,
297 DRAW_JIT_VERTEX_CLIPMASK);
298 LP_CHECK_MEMBER_OFFSET(struct vertex_header, edgeflag,
299 target, vertex_header,
300 DRAW_JIT_VERTEX_EDGEFLAG);
301 LP_CHECK_MEMBER_OFFSET(struct vertex_header, pad,
302 target, vertex_header,
303 DRAW_JIT_VERTEX_PAD);
304 LP_CHECK_MEMBER_OFFSET(struct vertex_header, vertex_id,
305 target, vertex_header,
306 DRAW_JIT_VERTEX_VERTEX_ID);
308 LP_CHECK_MEMBER_OFFSET(struct vertex_header
, clip
,
309 target
, vertex_header
,
310 DRAW_JIT_VERTEX_CLIP
);
311 LP_CHECK_MEMBER_OFFSET(struct vertex_header
, data
,
312 target
, vertex_header
,
313 DRAW_JIT_VERTEX_DATA
);
315 return vertex_header
;
320 * Create LLVM types for various structures.
323 create_jit_types(struct draw_llvm
*llvm
)
325 struct gallivm_state
*gallivm
= llvm
->gallivm
;
326 LLVMTypeRef texture_type
, context_type
, buffer_type
, vb_type
;
328 texture_type
= create_jit_texture_type(gallivm
, "texture");
330 context_type
= create_jit_context_type(gallivm
, texture_type
, "draw_jit_context");
331 llvm
->context_ptr_type
= LLVMPointerType(context_type
, 0);
333 buffer_type
= LLVMPointerType(LLVMIntTypeInContext(gallivm
->context
, 8), 0);
334 llvm
->buffer_ptr_type
= LLVMPointerType(buffer_type
, 0);
336 vb_type
= create_jit_vertex_buffer_type(gallivm
, "pipe_vertex_buffer");
337 llvm
->vb_ptr_type
= LLVMPointerType(vb_type
, 0);
342 get_context_ptr_type(struct draw_llvm
*llvm
)
344 if (!llvm
->context_ptr_type
)
345 create_jit_types(llvm
);
346 return llvm
->context_ptr_type
;
351 get_buffer_ptr_type(struct draw_llvm
*llvm
)
353 if (!llvm
->buffer_ptr_type
)
354 create_jit_types(llvm
);
355 return llvm
->buffer_ptr_type
;
360 get_vb_ptr_type(struct draw_llvm
*llvm
)
362 if (!llvm
->vb_ptr_type
)
363 create_jit_types(llvm
);
364 return llvm
->vb_ptr_type
;
368 get_vertex_header_ptr_type(struct draw_llvm
*llvm
)
370 if (!llvm
->vertex_header_ptr_type
)
371 create_jit_types(llvm
);
372 return llvm
->vertex_header_ptr_type
;
377 * Create per-context LLVM info.
380 draw_llvm_create(struct draw_context
*draw
, struct gallivm_state
*gallivm
)
382 struct draw_llvm
*llvm
;
384 llvm
= CALLOC_STRUCT( draw_llvm
);
391 llvm
->gallivm
= gallivm
;
393 if (gallivm_debug
& GALLIVM_DEBUG_IR
) {
394 LLVMDumpModule(llvm
->gallivm
->module
);
397 llvm
->nr_variants
= 0;
398 make_empty_list(&llvm
->vs_variants_list
);
400 gallivm_register_garbage_collector_callback(
401 draw_llvm_garbage_collect_callback
, llvm
);
408 * Free per-context LLVM info.
411 draw_llvm_destroy(struct draw_llvm
*llvm
)
413 gallivm_remove_garbage_collector_callback(
414 draw_llvm_garbage_collect_callback
, llvm
);
416 /* XXX free other draw_llvm data? */
422 * Create LLVM-generated code for a vertex shader.
424 struct draw_llvm_variant
*
425 draw_llvm_create_variant(struct draw_llvm
*llvm
,
427 const struct draw_llvm_variant_key
*key
)
429 struct draw_llvm_variant
*variant
;
430 struct llvm_vertex_shader
*shader
=
431 llvm_vertex_shader(llvm
->draw
->vs
.vertex_shader
);
432 LLVMTypeRef vertex_header
;
434 variant
= MALLOC(sizeof *variant
+
435 shader
->variant_key_size
-
436 sizeof variant
->key
);
440 variant
->llvm
= llvm
;
442 memcpy(&variant
->key
, key
, shader
->variant_key_size
);
444 vertex_header
= create_jit_vertex_header(llvm
->gallivm
, num_inputs
);
446 llvm
->vertex_header_ptr_type
= LLVMPointerType(vertex_header
, 0);
448 draw_llvm_generate(llvm
, variant
);
449 draw_llvm_generate_elts(llvm
, variant
);
451 variant
->shader
= shader
;
452 variant
->list_item_global
.base
= variant
;
453 variant
->list_item_local
.base
= variant
;
454 /*variant->no = */shader
->variants_created
++;
455 variant
->list_item_global
.base
= variant
;
462 generate_vs(struct draw_llvm
*llvm
,
463 LLVMBuilderRef builder
,
464 LLVMValueRef (*outputs
)[NUM_CHANNELS
],
465 const LLVMValueRef (*inputs
)[NUM_CHANNELS
],
466 LLVMValueRef system_values_array
,
467 LLVMValueRef context_ptr
,
468 struct lp_build_sampler_soa
*draw_sampler
,
469 boolean clamp_vertex_color
)
471 const struct tgsi_token
*tokens
= llvm
->draw
->vs
.vertex_shader
->state
.tokens
;
472 struct lp_type vs_type
;
473 LLVMValueRef consts_ptr
= draw_jit_context_vs_constants(llvm
->gallivm
, context_ptr
);
474 struct lp_build_sampler_soa
*sampler
= 0;
476 memset(&vs_type
, 0, sizeof vs_type
);
477 vs_type
.floating
= TRUE
; /* floating point values */
478 vs_type
.sign
= TRUE
; /* values are signed */
479 vs_type
.norm
= FALSE
; /* values are not limited to [0,1] or [-1,1] */
480 vs_type
.width
= 32; /* 32-bit float */
481 vs_type
.length
= 4; /* 4 elements per vector */
483 num_vs
= 4; /* number of vertices per block */
486 if (gallivm_debug
& GALLIVM_DEBUG_IR
) {
487 tgsi_dump(tokens
, 0);
490 if (llvm
->draw
->num_sampler_views
&& llvm
->draw
->num_samplers
)
491 sampler
= draw_sampler
;
493 lp_build_tgsi_soa(llvm
->gallivm
,
496 NULL
/*struct lp_build_mask_context *mask*/,
503 &llvm
->draw
->vs
.vertex_shader
->info
);
505 if (clamp_vertex_color
) {
507 unsigned chan
, attrib
;
508 struct lp_build_context bld
;
509 struct tgsi_shader_info
* info
= &llvm
->draw
->vs
.vertex_shader
->info
;
510 lp_build_context_init(&bld
, llvm
->gallivm
, vs_type
);
512 for (attrib
= 0; attrib
< info
->num_outputs
; ++attrib
) {
513 for (chan
= 0; chan
< NUM_CHANNELS
; ++chan
) {
514 if (outputs
[attrib
][chan
]) {
515 switch (info
->output_semantic_name
[attrib
]) {
516 case TGSI_SEMANTIC_COLOR
:
517 case TGSI_SEMANTIC_BCOLOR
:
518 out
= LLVMBuildLoad(builder
, outputs
[attrib
][chan
], "");
519 out
= lp_build_clamp(&bld
, out
, bld
.zero
, bld
.one
);
520 LLVMBuildStore(builder
, out
, outputs
[attrib
][chan
]);
531 static void print_vectorf(LLVMBuilderRef builder
,
535 val
[0] = LLVMBuildExtractElement(builder
, vec
,
536 lp_build_const_int32(gallivm
, 0), "");
537 val
[1] = LLVMBuildExtractElement(builder
, vec
,
538 lp_build_const_int32(gallivm
, 1), "");
539 val
[2] = LLVMBuildExtractElement(builder
, vec
,
540 lp_build_const_int32(gallivm
, 2), "");
541 val
[3] = LLVMBuildExtractElement(builder
, vec
,
542 lp_build_const_int32(gallivm
, 3), "");
543 lp_build_printf(builder
, "vector = [%f, %f, %f, %f]\n",
544 val
[0], val
[1], val
[2], val
[3]);
550 generate_fetch(struct gallivm_state
*gallivm
,
551 LLVMValueRef vbuffers_ptr
,
553 struct pipe_vertex_element
*velem
,
556 LLVMValueRef instance_id
)
558 LLVMBuilderRef builder
= gallivm
->builder
;
559 LLVMValueRef indices
=
560 LLVMConstInt(LLVMInt64TypeInContext(gallivm
->context
),
561 velem
->vertex_buffer_index
, 0);
562 LLVMValueRef vbuffer_ptr
= LLVMBuildGEP(builder
, vbuffers_ptr
,
564 LLVMValueRef vb_stride
= draw_jit_vbuffer_stride(gallivm
, vbuf
);
565 LLVMValueRef vb_buffer_offset
= draw_jit_vbuffer_offset(gallivm
, vbuf
);
568 if (velem
->instance_divisor
) {
569 /* array index = instance_id / instance_divisor */
570 index
= LLVMBuildUDiv(builder
, instance_id
,
571 lp_build_const_int32(gallivm
, velem
->instance_divisor
),
575 stride
= LLVMBuildMul(builder
, vb_stride
, index
, "");
577 vbuffer_ptr
= LLVMBuildLoad(builder
, vbuffer_ptr
, "vbuffer");
579 stride
= LLVMBuildAdd(builder
, stride
,
582 stride
= LLVMBuildAdd(builder
, stride
,
583 lp_build_const_int32(gallivm
, velem
->src_offset
),
586 /*lp_build_printf(builder, "vbuf index = %d, stride is %d\n", indices, stride);*/
587 vbuffer_ptr
= LLVMBuildGEP(builder
, vbuffer_ptr
, &stride
, 1, "");
589 *res
= draw_llvm_translate_from(gallivm
, vbuffer_ptr
, velem
->src_format
);
594 aos_to_soa(struct gallivm_state
*gallivm
,
599 LLVMValueRef channel
)
601 LLVMBuilderRef builder
= gallivm
->builder
;
602 LLVMValueRef ex
, res
;
604 ex
= LLVMBuildExtractElement(builder
, val0
,
606 res
= LLVMBuildInsertElement(builder
,
607 LLVMConstNull(LLVMTypeOf(val0
)),
609 lp_build_const_int32(gallivm
, 0),
612 ex
= LLVMBuildExtractElement(builder
, val1
,
614 res
= LLVMBuildInsertElement(builder
,
616 lp_build_const_int32(gallivm
, 1),
619 ex
= LLVMBuildExtractElement(builder
, val2
,
621 res
= LLVMBuildInsertElement(builder
,
623 lp_build_const_int32(gallivm
, 2),
626 ex
= LLVMBuildExtractElement(builder
, val3
,
628 res
= LLVMBuildInsertElement(builder
,
630 lp_build_const_int32(gallivm
, 3),
638 soa_to_aos(struct gallivm_state
*gallivm
,
639 LLVMValueRef soa
[NUM_CHANNELS
],
640 LLVMValueRef aos
[NUM_CHANNELS
])
642 LLVMBuilderRef builder
= gallivm
->builder
;
646 debug_assert(NUM_CHANNELS
== 4);
648 aos
[0] = LLVMConstNull(LLVMTypeOf(soa
[0]));
649 aos
[1] = aos
[2] = aos
[3] = aos
[0];
651 for (i
= 0; i
< NUM_CHANNELS
; ++i
) {
652 LLVMValueRef channel
= lp_build_const_int32(gallivm
, i
);
654 comp
= LLVMBuildExtractElement(builder
, soa
[i
],
655 lp_build_const_int32(gallivm
, 0), "");
656 aos
[0] = LLVMBuildInsertElement(builder
, aos
[0], comp
, channel
, "");
658 comp
= LLVMBuildExtractElement(builder
, soa
[i
],
659 lp_build_const_int32(gallivm
, 1), "");
660 aos
[1] = LLVMBuildInsertElement(builder
, aos
[1], comp
, channel
, "");
662 comp
= LLVMBuildExtractElement(builder
, soa
[i
],
663 lp_build_const_int32(gallivm
, 2), "");
664 aos
[2] = LLVMBuildInsertElement(builder
, aos
[2], comp
, channel
, "");
666 comp
= LLVMBuildExtractElement(builder
, soa
[i
],
667 lp_build_const_int32(gallivm
, 3), "");
668 aos
[3] = LLVMBuildInsertElement(builder
, aos
[3], comp
, channel
, "");
675 convert_to_soa(struct gallivm_state
*gallivm
,
676 LLVMValueRef (*aos
)[NUM_CHANNELS
],
677 LLVMValueRef (*soa
)[NUM_CHANNELS
],
682 debug_assert(NUM_CHANNELS
== 4);
684 for (i
= 0; i
< num_attribs
; ++i
) {
685 LLVMValueRef val0
= aos
[i
][0];
686 LLVMValueRef val1
= aos
[i
][1];
687 LLVMValueRef val2
= aos
[i
][2];
688 LLVMValueRef val3
= aos
[i
][3];
690 soa
[i
][0] = aos_to_soa(gallivm
, val0
, val1
, val2
, val3
,
691 lp_build_const_int32(gallivm
, 0));
692 soa
[i
][1] = aos_to_soa(gallivm
, val0
, val1
, val2
, val3
,
693 lp_build_const_int32(gallivm
, 1));
694 soa
[i
][2] = aos_to_soa(gallivm
, val0
, val1
, val2
, val3
,
695 lp_build_const_int32(gallivm
, 2));
696 soa
[i
][3] = aos_to_soa(gallivm
, val0
, val1
, val2
, val3
,
697 lp_build_const_int32(gallivm
, 3));
703 store_aos(struct gallivm_state
*gallivm
,
707 LLVMValueRef clipmask
)
709 LLVMBuilderRef builder
= gallivm
->builder
;
710 LLVMValueRef id_ptr
= draw_jit_header_id(gallivm
, io_ptr
);
711 LLVMValueRef data_ptr
= draw_jit_header_data(gallivm
, io_ptr
);
712 LLVMValueRef indices
[3];
713 LLVMValueRef val
, shift
;
715 indices
[0] = lp_build_const_int32(gallivm
, 0);
717 indices
[2] = lp_build_const_int32(gallivm
, 0);
719 /* initialize vertex id:16 = 0xffff, pad:3 = 0, edgeflag:1 = 1 */
720 val
= lp_build_const_int32(gallivm
, 0xffff1);
721 shift
= lp_build_const_int32(gallivm
, 12);
722 val
= LLVMBuildShl(builder
, val
, shift
, "");
723 /* add clipmask:12 */
724 val
= LLVMBuildOr(builder
, val
, clipmask
, "");
726 /* store vertex header */
727 LLVMBuildStore(builder
, val
, id_ptr
);
731 lp_build_printf(builder
, " ---- %p storing attribute %d (io = %p)\n", data_ptr
, index
, io_ptr
);
734 /*lp_build_printf(builder, " ---- %p storing at %d (%p) ", io_ptr, index, data_ptr);
735 print_vectorf(builder, value);*/
736 data_ptr
= LLVMBuildBitCast(builder
, data_ptr
,
737 LLVMPointerType(LLVMArrayType(LLVMVectorType(LLVMFloatTypeInContext(gallivm
->context
), 4), 0), 0),
739 data_ptr
= LLVMBuildGEP(builder
, data_ptr
, indices
, 2, "");
741 LLVMBuildStore(builder
, value
, data_ptr
);
744 LLVMValueRef x
, y
, z
, w
;
745 LLVMValueRef idx0
, idx1
, idx2
, idx3
;
746 LLVMValueRef gep0
, gep1
, gep2
, gep3
;
747 data_ptr
= LLVMBuildGEP(builder
, data_ptr
, indices
, 3, "");
749 idx0
= lp_build_const_int32(gallivm
, 0);
750 idx1
= lp_build_const_int32(gallivm
, 1);
751 idx2
= lp_build_const_int32(gallivm
, 2);
752 idx3
= lp_build_const_int32(gallivm
, 3);
754 x
= LLVMBuildExtractElement(builder
, value
,
756 y
= LLVMBuildExtractElement(builder
, value
,
758 z
= LLVMBuildExtractElement(builder
, value
,
760 w
= LLVMBuildExtractElement(builder
, value
,
763 gep0
= LLVMBuildGEP(builder
, data_ptr
, &idx0
, 1, "");
764 gep1
= LLVMBuildGEP(builder
, data_ptr
, &idx1
, 1, "");
765 gep2
= LLVMBuildGEP(builder
, data_ptr
, &idx2
, 1, "");
766 gep3
= LLVMBuildGEP(builder
, data_ptr
, &idx3
, 1, "");
768 /*lp_build_printf(builder, "##### x = %f (%p), y = %f (%p), z = %f (%p), w = %f (%p)\n",
769 x, gep0, y, gep1, z, gep2, w, gep3);*/
770 LLVMBuildStore(builder
, x
, gep0
);
771 LLVMBuildStore(builder
, y
, gep1
);
772 LLVMBuildStore(builder
, z
, gep2
);
773 LLVMBuildStore(builder
, w
, gep3
);
780 store_aos_array(struct gallivm_state
*gallivm
,
782 LLVMValueRef aos
[NUM_CHANNELS
],
785 LLVMValueRef clipmask
)
787 LLVMBuilderRef builder
= gallivm
->builder
;
788 LLVMValueRef attr_index
= lp_build_const_int32(gallivm
, attrib
);
789 LLVMValueRef ind0
= lp_build_const_int32(gallivm
, 0);
790 LLVMValueRef ind1
= lp_build_const_int32(gallivm
, 1);
791 LLVMValueRef ind2
= lp_build_const_int32(gallivm
, 2);
792 LLVMValueRef ind3
= lp_build_const_int32(gallivm
, 3);
793 LLVMValueRef io0_ptr
, io1_ptr
, io2_ptr
, io3_ptr
;
794 LLVMValueRef clipmask0
, clipmask1
, clipmask2
, clipmask3
;
796 debug_assert(NUM_CHANNELS
== 4);
798 io0_ptr
= LLVMBuildGEP(builder
, io_ptr
,
800 io1_ptr
= LLVMBuildGEP(builder
, io_ptr
,
802 io2_ptr
= LLVMBuildGEP(builder
, io_ptr
,
804 io3_ptr
= LLVMBuildGEP(builder
, io_ptr
,
807 clipmask0
= LLVMBuildExtractElement(builder
, clipmask
,
809 clipmask1
= LLVMBuildExtractElement(builder
, clipmask
,
811 clipmask2
= LLVMBuildExtractElement(builder
, clipmask
,
813 clipmask3
= LLVMBuildExtractElement(builder
, clipmask
,
817 lp_build_printf(builder
, "io = %p, indexes[%d, %d, %d, %d]\n, clipmask0 = %x, clipmask1 = %x, clipmask2 = %x, clipmask3 = %x\n",
818 io_ptr
, ind0
, ind1
, ind2
, ind3
, clipmask0
, clipmask1
, clipmask2
, clipmask3
);
820 /* store for each of the 4 vertices */
821 store_aos(gallivm
, io0_ptr
, attr_index
, aos
[0], clipmask0
);
822 store_aos(gallivm
, io1_ptr
, attr_index
, aos
[1], clipmask1
);
823 store_aos(gallivm
, io2_ptr
, attr_index
, aos
[2], clipmask2
);
824 store_aos(gallivm
, io3_ptr
, attr_index
, aos
[3], clipmask3
);
829 convert_to_aos(struct gallivm_state
*gallivm
,
831 LLVMValueRef (*outputs
)[NUM_CHANNELS
],
832 LLVMValueRef clipmask
,
836 LLVMBuilderRef builder
= gallivm
->builder
;
837 unsigned chan
, attrib
;
840 lp_build_printf(builder
, " # storing begin\n");
842 for (attrib
= 0; attrib
< num_outputs
; ++attrib
) {
845 for (chan
= 0; chan
< NUM_CHANNELS
; ++chan
) {
846 if (outputs
[attrib
][chan
]) {
847 LLVMValueRef out
= LLVMBuildLoad(builder
, outputs
[attrib
][chan
], "");
848 lp_build_name(out
, "output%u.%c", attrib
, "xyzw"[chan
]);
849 /*lp_build_printf(builder, "output %d : %d ",
850 LLVMConstInt(LLVMInt32Type(), attrib, 0),
851 LLVMConstInt(LLVMInt32Type(), chan, 0));
852 print_vectorf(builder, out);*/
859 soa_to_aos(gallivm
, soa
, aos
);
860 store_aos_array(gallivm
,
868 lp_build_printf(builder
, " # storing end\n");
874 * Stores original vertex positions in clip coordinates
875 * There is probably a more efficient way to do this, 4 floats at once
876 * rather than extracting each element one by one.
879 store_clip(struct gallivm_state
*gallivm
,
881 LLVMValueRef (*outputs
)[NUM_CHANNELS
])
883 LLVMBuilderRef builder
= gallivm
->builder
;
885 LLVMValueRef indices
[2];
886 LLVMValueRef io0_ptr
, io1_ptr
, io2_ptr
, io3_ptr
;
887 LLVMValueRef clip_ptr0
, clip_ptr1
, clip_ptr2
, clip_ptr3
;
888 LLVMValueRef clip0_ptr
, clip1_ptr
, clip2_ptr
, clip3_ptr
;
889 LLVMValueRef out0elem
, out1elem
, out2elem
, out3elem
;
892 LLVMValueRef ind0
= lp_build_const_int32(gallivm
, 0);
893 LLVMValueRef ind1
= lp_build_const_int32(gallivm
, 1);
894 LLVMValueRef ind2
= lp_build_const_int32(gallivm
, 2);
895 LLVMValueRef ind3
= lp_build_const_int32(gallivm
, 3);
898 indices
[1] = lp_build_const_int32(gallivm
, 0);
900 out
[0] = LLVMBuildLoad(builder
, outputs
[0][0], ""); /*x0 x1 x2 x3*/
901 out
[1] = LLVMBuildLoad(builder
, outputs
[0][1], ""); /*y0 y1 y2 y3*/
902 out
[2] = LLVMBuildLoad(builder
, outputs
[0][2], ""); /*z0 z1 z2 z3*/
903 out
[3] = LLVMBuildLoad(builder
, outputs
[0][3], ""); /*w0 w1 w2 w3*/
905 io0_ptr
= LLVMBuildGEP(builder
, io_ptr
, &ind0
, 1, "");
906 io1_ptr
= LLVMBuildGEP(builder
, io_ptr
, &ind1
, 1, "");
907 io2_ptr
= LLVMBuildGEP(builder
, io_ptr
, &ind2
, 1, "");
908 io3_ptr
= LLVMBuildGEP(builder
, io_ptr
, &ind3
, 1, "");
910 clip_ptr0
= draw_jit_header_clip(gallivm
, io0_ptr
);
911 clip_ptr1
= draw_jit_header_clip(gallivm
, io1_ptr
);
912 clip_ptr2
= draw_jit_header_clip(gallivm
, io2_ptr
);
913 clip_ptr3
= draw_jit_header_clip(gallivm
, io3_ptr
);
915 for (i
= 0; i
<4; i
++) {
916 clip0_ptr
= LLVMBuildGEP(builder
, clip_ptr0
, indices
, 2, ""); /* x0 */
917 clip1_ptr
= LLVMBuildGEP(builder
, clip_ptr1
, indices
, 2, ""); /* x1 */
918 clip2_ptr
= LLVMBuildGEP(builder
, clip_ptr2
, indices
, 2, ""); /* x2 */
919 clip3_ptr
= LLVMBuildGEP(builder
, clip_ptr3
, indices
, 2, ""); /* x3 */
921 out0elem
= LLVMBuildExtractElement(builder
, out
[i
], ind0
, ""); /* x0 */
922 out1elem
= LLVMBuildExtractElement(builder
, out
[i
], ind1
, ""); /* x1 */
923 out2elem
= LLVMBuildExtractElement(builder
, out
[i
], ind2
, ""); /* x2 */
924 out3elem
= LLVMBuildExtractElement(builder
, out
[i
], ind3
, ""); /* x3 */
926 LLVMBuildStore(builder
, out0elem
, clip0_ptr
);
927 LLVMBuildStore(builder
, out1elem
, clip1_ptr
);
928 LLVMBuildStore(builder
, out2elem
, clip2_ptr
);
929 LLVMBuildStore(builder
, out3elem
, clip3_ptr
);
931 indices
[1]= LLVMBuildAdd(builder
, indices
[1], ind1
, "");
938 * Equivalent of _mm_set1_ps(a)
941 vec4f_from_scalar(struct gallivm_state
*gallivm
,
945 LLVMTypeRef float_type
= LLVMFloatTypeInContext(gallivm
->context
);
946 LLVMValueRef res
= LLVMGetUndef(LLVMVectorType(float_type
, 4));
949 for (i
= 0; i
< 4; ++i
) {
950 LLVMValueRef index
= lp_build_const_int32(gallivm
, i
);
951 res
= LLVMBuildInsertElement(gallivm
->builder
, res
, a
,
952 index
, i
== 3 ? name
: "");
960 * Transforms the outputs for viewport mapping
963 generate_viewport(struct draw_llvm
*llvm
,
964 LLVMBuilderRef builder
,
965 LLVMValueRef (*outputs
)[NUM_CHANNELS
],
966 LLVMValueRef context_ptr
)
969 struct gallivm_state
*gallivm
= llvm
->gallivm
;
970 struct lp_type f32_type
= lp_type_float_vec(32);
971 LLVMValueRef out3
= LLVMBuildLoad(builder
, outputs
[0][3], ""); /*w0 w1 w2 w3*/
972 LLVMValueRef const1
= lp_build_const_vec(gallivm
, f32_type
, 1.0); /*1.0 1.0 1.0 1.0*/
973 LLVMValueRef vp_ptr
= draw_jit_context_viewport(gallivm
, context_ptr
);
975 /* for 1/w convention*/
976 out3
= LLVMBuildFDiv(builder
, const1
, out3
, "");
977 LLVMBuildStore(builder
, out3
, outputs
[0][3]);
979 /* Viewport Mapping */
980 for (i
=0; i
<3; i
++) {
981 LLVMValueRef out
= LLVMBuildLoad(builder
, outputs
[0][i
], ""); /*x0 x1 x2 x3*/
984 LLVMValueRef scale_i
;
985 LLVMValueRef trans_i
;
988 index
= lp_build_const_int32(gallivm
, i
);
989 scale_i
= LLVMBuildGEP(builder
, vp_ptr
, &index
, 1, "");
991 index
= lp_build_const_int32(gallivm
, i
+4);
992 trans_i
= LLVMBuildGEP(builder
, vp_ptr
, &index
, 1, "");
994 scale
= vec4f_from_scalar(gallivm
, LLVMBuildLoad(builder
, scale_i
, ""), "scale");
995 trans
= vec4f_from_scalar(gallivm
, LLVMBuildLoad(builder
, trans_i
, ""), "trans");
998 out
= LLVMBuildFMul(builder
, out
, out3
, "");
1000 out
= LLVMBuildFMul(builder
, out
, scale
, "");
1001 /* add translation */
1002 out
= LLVMBuildFAdd(builder
, out
, trans
, "");
1004 /* store transformed outputs */
1005 LLVMBuildStore(builder
, out
, outputs
[0][i
]);
1012 * Returns clipmask as 4xi32 bitmask for the 4 vertices
1015 generate_clipmask(struct gallivm_state
*gallivm
,
1016 LLVMValueRef (*outputs
)[NUM_CHANNELS
],
1022 LLVMValueRef context_ptr
)
1024 LLVMBuilderRef builder
= gallivm
->builder
;
1025 LLVMValueRef mask
; /* stores the <4xi32> clipmasks */
1026 LLVMValueRef test
, temp
;
1027 LLVMValueRef zero
, shift
;
1028 LLVMValueRef pos_x
, pos_y
, pos_z
, pos_w
;
1029 LLVMValueRef plane1
, planes
, plane_ptr
, sum
;
1031 struct lp_type f32_type
= lp_type_float_vec(32);
1033 mask
= lp_build_const_int_vec(gallivm
, lp_type_int_vec(32), 0);
1034 temp
= lp_build_const_int_vec(gallivm
, lp_type_int_vec(32), 0);
1035 zero
= lp_build_const_vec(gallivm
, f32_type
, 0); /* 0.0f 0.0f 0.0f 0.0f */
1036 shift
= lp_build_const_int_vec(gallivm
, lp_type_int_vec(32), 1); /* 1 1 1 1 */
1038 /* Assuming position stored at output[0] */
1039 pos_x
= LLVMBuildLoad(builder
, outputs
[0][0], ""); /*x0 x1 x2 x3*/
1040 pos_y
= LLVMBuildLoad(builder
, outputs
[0][1], ""); /*y0 y1 y2 y3*/
1041 pos_z
= LLVMBuildLoad(builder
, outputs
[0][2], ""); /*z0 z1 z2 z3*/
1042 pos_w
= LLVMBuildLoad(builder
, outputs
[0][3], ""); /*w0 w1 w2 w3*/
1044 /* Cliptest, for hardwired planes */
1047 test
= lp_build_compare(gallivm
, f32_type
, PIPE_FUNC_GREATER
, pos_x
, pos_w
);
1049 test
= LLVMBuildAnd(builder
, test
, temp
, "");
1053 test
= LLVMBuildFAdd(builder
, pos_x
, pos_w
, "");
1054 test
= lp_build_compare(gallivm
, f32_type
, PIPE_FUNC_GREATER
, zero
, test
);
1055 temp
= LLVMBuildShl(builder
, temp
, shift
, "");
1056 test
= LLVMBuildAnd(builder
, test
, temp
, "");
1057 mask
= LLVMBuildOr(builder
, mask
, test
, "");
1060 test
= lp_build_compare(gallivm
, f32_type
, PIPE_FUNC_GREATER
, pos_y
, pos_w
);
1061 temp
= LLVMBuildShl(builder
, temp
, shift
, "");
1062 test
= LLVMBuildAnd(builder
, test
, temp
, "");
1063 mask
= LLVMBuildOr(builder
, mask
, test
, "");
1066 test
= LLVMBuildFAdd(builder
, pos_y
, pos_w
, "");
1067 test
= lp_build_compare(gallivm
, f32_type
, PIPE_FUNC_GREATER
, zero
, test
);
1068 temp
= LLVMBuildShl(builder
, temp
, shift
, "");
1069 test
= LLVMBuildAnd(builder
, test
, temp
, "");
1070 mask
= LLVMBuildOr(builder
, mask
, test
, "");
1074 temp
= lp_build_const_int_vec(gallivm
, lp_type_int_vec(32), 16);
1077 test
= lp_build_compare(gallivm
, f32_type
, PIPE_FUNC_GREATER
, zero
, pos_z
);
1078 test
= LLVMBuildAnd(builder
, test
, temp
, "");
1079 mask
= LLVMBuildOr(builder
, mask
, test
, "");
1083 test
= LLVMBuildFAdd(builder
, pos_z
, pos_w
, "");
1084 test
= lp_build_compare(gallivm
, f32_type
, PIPE_FUNC_GREATER
, zero
, test
);
1085 test
= LLVMBuildAnd(builder
, test
, temp
, "");
1086 mask
= LLVMBuildOr(builder
, mask
, test
, "");
1089 test
= lp_build_compare(gallivm
, f32_type
, PIPE_FUNC_GREATER
, pos_z
, pos_w
);
1090 temp
= LLVMBuildShl(builder
, temp
, shift
, "");
1091 test
= LLVMBuildAnd(builder
, test
, temp
, "");
1092 mask
= LLVMBuildOr(builder
, mask
, test
, "");
1096 LLVMValueRef planes_ptr
= draw_jit_context_planes(gallivm
, context_ptr
);
1097 LLVMValueRef indices
[3];
1098 temp
= lp_build_const_int_vec(gallivm
, lp_type_int_vec(32), 32);
1100 /* userclip planes */
1101 for (i
= 6; i
< nr
; i
++) {
1102 indices
[0] = lp_build_const_int32(gallivm
, 0);
1103 indices
[1] = lp_build_const_int32(gallivm
, i
);
1105 indices
[2] = lp_build_const_int32(gallivm
, 0);
1106 plane_ptr
= LLVMBuildGEP(builder
, planes_ptr
, indices
, 3, "");
1107 plane1
= LLVMBuildLoad(builder
, plane_ptr
, "plane_x");
1108 planes
= vec4f_from_scalar(gallivm
, plane1
, "plane4_x");
1109 sum
= LLVMBuildFMul(builder
, planes
, pos_x
, "");
1111 indices
[2] = lp_build_const_int32(gallivm
, 1);
1112 plane_ptr
= LLVMBuildGEP(builder
, planes_ptr
, indices
, 3, "");
1113 plane1
= LLVMBuildLoad(builder
, plane_ptr
, "plane_y");
1114 planes
= vec4f_from_scalar(gallivm
, plane1
, "plane4_y");
1115 test
= LLVMBuildFMul(builder
, planes
, pos_y
, "");
1116 sum
= LLVMBuildFAdd(builder
, sum
, test
, "");
1118 indices
[2] = lp_build_const_int32(gallivm
, 2);
1119 plane_ptr
= LLVMBuildGEP(builder
, planes_ptr
, indices
, 3, "");
1120 plane1
= LLVMBuildLoad(builder
, plane_ptr
, "plane_z");
1121 planes
= vec4f_from_scalar(gallivm
, plane1
, "plane4_z");
1122 test
= LLVMBuildFMul(builder
, planes
, pos_z
, "");
1123 sum
= LLVMBuildFAdd(builder
, sum
, test
, "");
1125 indices
[2] = lp_build_const_int32(gallivm
, 3);
1126 plane_ptr
= LLVMBuildGEP(builder
, planes_ptr
, indices
, 3, "");
1127 plane1
= LLVMBuildLoad(builder
, plane_ptr
, "plane_w");
1128 planes
= vec4f_from_scalar(gallivm
, plane1
, "plane4_w");
1129 test
= LLVMBuildFMul(builder
, planes
, pos_w
, "");
1130 sum
= LLVMBuildFAdd(builder
, sum
, test
, "");
1132 test
= lp_build_compare(gallivm
, f32_type
, PIPE_FUNC_GREATER
, zero
, sum
);
1133 temp
= LLVMBuildShl(builder
, temp
, shift
, "");
1134 test
= LLVMBuildAnd(builder
, test
, temp
, "");
1135 mask
= LLVMBuildOr(builder
, mask
, test
, "");
1143 * Returns boolean if any clipping has occurred
1144 * Used zero/non-zero i32 value to represent boolean
1147 clipmask_bool(struct gallivm_state
*gallivm
,
1148 LLVMValueRef clipmask
,
1149 LLVMValueRef ret_ptr
)
1151 LLVMBuilderRef builder
= gallivm
->builder
;
1152 LLVMValueRef ret
= LLVMBuildLoad(builder
, ret_ptr
, "");
1156 for (i
=0; i
<4; i
++) {
1157 temp
= LLVMBuildExtractElement(builder
, clipmask
,
1158 lp_build_const_int32(gallivm
, i
) , "");
1159 ret
= LLVMBuildOr(builder
, ret
, temp
, "");
1162 LLVMBuildStore(builder
, ret
, ret_ptr
);
1167 draw_llvm_generate(struct draw_llvm
*llvm
, struct draw_llvm_variant
*variant
)
1169 struct gallivm_state
*gallivm
= llvm
->gallivm
;
1170 LLVMContextRef context
= gallivm
->context
;
1171 LLVMTypeRef int32_type
= LLVMInt32TypeInContext(context
);
1172 LLVMTypeRef arg_types
[8];
1173 LLVMTypeRef func_type
;
1174 LLVMValueRef context_ptr
;
1175 LLVMBasicBlockRef block
;
1176 LLVMBuilderRef builder
;
1177 LLVMValueRef start
, end
, count
, stride
, step
, io_itr
;
1178 LLVMValueRef io_ptr
, vbuffers_ptr
, vb_ptr
;
1179 LLVMValueRef instance_id
;
1180 LLVMValueRef system_values_array
;
1181 struct draw_context
*draw
= llvm
->draw
;
1182 const struct tgsi_shader_info
*vs_info
= &draw
->vs
.vertex_shader
->info
;
1184 struct lp_build_context bld
;
1185 struct lp_build_loop_state lp_loop
;
1186 const int max_vertices
= 4;
1187 LLVMValueRef outputs
[PIPE_MAX_SHADER_OUTPUTS
][NUM_CHANNELS
];
1188 LLVMValueRef fetch_max
;
1190 struct lp_build_sampler_soa
*sampler
= 0;
1191 LLVMValueRef ret
, ret_ptr
;
1192 boolean bypass_viewport
= variant
->key
.bypass_viewport
;
1193 boolean enable_cliptest
= variant
->key
.clip_xy
||
1194 variant
->key
.clip_z
||
1195 variant
->key
.clip_user
;
1197 arg_types
[0] = get_context_ptr_type(llvm
); /* context */
1198 arg_types
[1] = get_vertex_header_ptr_type(llvm
); /* vertex_header */
1199 arg_types
[2] = get_buffer_ptr_type(llvm
); /* vbuffers */
1200 arg_types
[3] = int32_type
; /* start */
1201 arg_types
[4] = int32_type
; /* count */
1202 arg_types
[5] = int32_type
; /* stride */
1203 arg_types
[6] = get_vb_ptr_type(llvm
); /* pipe_vertex_buffer's */
1204 arg_types
[7] = int32_type
; /* instance_id */
1206 func_type
= LLVMFunctionType(int32_type
, arg_types
, Elements(arg_types
), 0);
1208 variant
->function
= LLVMAddFunction(gallivm
->module
, "draw_llvm_shader",
1210 LLVMSetFunctionCallConv(variant
->function
, LLVMCCallConv
);
1211 for (i
= 0; i
< Elements(arg_types
); ++i
)
1212 if (LLVMGetTypeKind(arg_types
[i
]) == LLVMPointerTypeKind
)
1213 LLVMAddAttribute(LLVMGetParam(variant
->function
, i
), LLVMNoAliasAttribute
);
1215 context_ptr
= LLVMGetParam(variant
->function
, 0);
1216 io_ptr
= LLVMGetParam(variant
->function
, 1);
1217 vbuffers_ptr
= LLVMGetParam(variant
->function
, 2);
1218 start
= LLVMGetParam(variant
->function
, 3);
1219 count
= LLVMGetParam(variant
->function
, 4);
1220 stride
= LLVMGetParam(variant
->function
, 5);
1221 vb_ptr
= LLVMGetParam(variant
->function
, 6);
1222 instance_id
= LLVMGetParam(variant
->function
, 7);
1224 lp_build_name(context_ptr
, "context");
1225 lp_build_name(io_ptr
, "io");
1226 lp_build_name(vbuffers_ptr
, "vbuffers");
1227 lp_build_name(start
, "start");
1228 lp_build_name(count
, "count");
1229 lp_build_name(stride
, "stride");
1230 lp_build_name(vb_ptr
, "vb");
1231 lp_build_name(instance_id
, "instance_id");
1237 block
= LLVMAppendBasicBlockInContext(gallivm
->context
, variant
->function
, "entry");
1238 builder
= gallivm
->builder
;
1240 LLVMPositionBuilderAtEnd(builder
, block
);
1242 lp_build_context_init(&bld
, llvm
->gallivm
, lp_type_int(32));
1244 system_values_array
= lp_build_system_values_array(gallivm
, vs_info
,
1247 end
= lp_build_add(&bld
, start
, count
);
1249 step
= lp_build_const_int32(gallivm
, max_vertices
);
1251 /* function will return non-zero i32 value if any clipped vertices */
1252 ret_ptr
= lp_build_alloca(gallivm
, int32_type
, "");
1253 LLVMBuildStore(builder
, lp_build_const_int32(gallivm
, 0), ret_ptr
);
1255 /* code generated texture sampling */
1256 sampler
= draw_llvm_sampler_soa_create(
1257 draw_llvm_variant_key_samplers(&variant
->key
),
1260 /* fetch_max = start + count - 1 */
1261 fetch_max
= LLVMBuildSub(builder
, end
,
1262 lp_build_const_int32(gallivm
, 1),
1266 lp_build_printf(builder
, "start = %d, end = %d, step = %d\n",
1269 lp_build_loop_begin(&lp_loop
, llvm
->gallivm
, start
);
1271 LLVMValueRef inputs
[PIPE_MAX_SHADER_INPUTS
][NUM_CHANNELS
];
1272 LLVMValueRef aos_attribs
[PIPE_MAX_SHADER_INPUTS
][NUM_CHANNELS
] = { { 0 } };
1274 LLVMValueRef clipmask
; /* holds the clipmask value */
1275 const LLVMValueRef (*ptr_aos
)[NUM_CHANNELS
];
1277 io_itr
= LLVMBuildSub(builder
, lp_loop
.counter
, start
, "");
1278 io
= LLVMBuildGEP(builder
, io_ptr
, &io_itr
, 1, "");
1280 lp_build_printf(builder
, " --- io %d = %p, loop counter %d\n",
1281 io_itr
, io
, lp_loop
.counter
);
1283 for (i
= 0; i
< NUM_CHANNELS
; ++i
) {
1284 LLVMValueRef true_index
= LLVMBuildAdd(
1287 lp_build_const_int32(gallivm
, i
), "");
1289 /* make sure we're not out of bounds which can happen
1290 * if fetch_count % 4 != 0, because on the last iteration
1291 * a few of the 4 vertex fetches will be out of bounds */
1292 true_index
= lp_build_min(&bld
, true_index
, fetch_max
);
1294 for (j
= 0; j
< draw
->pt
.nr_vertex_elements
; ++j
) {
1295 struct pipe_vertex_element
*velem
= &draw
->pt
.vertex_element
[j
];
1296 LLVMValueRef vb_index
= lp_build_const_int32(gallivm
, velem
->vertex_buffer_index
);
1297 LLVMValueRef vb
= LLVMBuildGEP(builder
, vb_ptr
,
1299 generate_fetch(llvm
->gallivm
, vbuffers_ptr
,
1300 &aos_attribs
[j
][i
], velem
, vb
, true_index
,
1304 convert_to_soa(gallivm
, aos_attribs
, inputs
,
1305 draw
->pt
.nr_vertex_elements
);
1307 ptr_aos
= (const LLVMValueRef (*)[NUM_CHANNELS
]) inputs
;
1312 system_values_array
,
1315 variant
->key
.clamp_vertex_color
);
1317 /* store original positions in clip before further manipulation */
1318 store_clip(gallivm
, io
, outputs
);
1321 if (enable_cliptest
) {
1322 /* allocate clipmask, assign it integer type */
1323 clipmask
= generate_clipmask(gallivm
, outputs
,
1324 variant
->key
.clip_xy
,
1325 variant
->key
.clip_z
,
1326 variant
->key
.clip_user
,
1327 variant
->key
.clip_halfz
,
1328 variant
->key
.nr_planes
,
1330 /* return clipping boolean value for function */
1331 clipmask_bool(gallivm
, clipmask
, ret_ptr
);
1334 clipmask
= lp_build_const_int_vec(gallivm
, lp_type_int_vec(32), 0);
1337 /* do viewport mapping */
1338 if (!bypass_viewport
) {
1339 generate_viewport(llvm
, builder
, outputs
, context_ptr
);
1342 /* store clipmask in vertex header and positions in data */
1343 convert_to_aos(gallivm
, io
, outputs
, clipmask
,
1344 vs_info
->num_outputs
, max_vertices
);
1347 lp_build_loop_end_cond(&lp_loop
, end
, step
, LLVMIntUGE
);
1349 sampler
->destroy(sampler
);
1351 ret
= LLVMBuildLoad(builder
, ret_ptr
,"");
1352 LLVMBuildRet(builder
, ret
);
1355 * Translate the LLVM IR into machine code.
1358 if (LLVMVerifyFunction(variant
->function
, LLVMPrintMessageAction
)) {
1359 lp_debug_dump_value(variant
->function
);
1364 LLVMRunFunctionPassManager(gallivm
->passmgr
, variant
->function
);
1366 if (gallivm_debug
& GALLIVM_DEBUG_IR
) {
1367 lp_debug_dump_value(variant
->function
);
1371 code
= LLVMGetPointerToGlobal(gallivm
->engine
, variant
->function
);
1372 variant
->jit_func
= (draw_jit_vert_func
)pointer_to_func(code
);
1374 if (gallivm_debug
& GALLIVM_DEBUG_ASM
) {
1375 lp_disassemble(code
);
1377 lp_func_delete_body(variant
->function
);
1382 draw_llvm_generate_elts(struct draw_llvm
*llvm
, struct draw_llvm_variant
*variant
)
1384 struct gallivm_state
*gallivm
= llvm
->gallivm
;
1385 LLVMContextRef context
= gallivm
->context
;
1386 LLVMTypeRef int32_type
= LLVMInt32TypeInContext(context
);
1387 LLVMTypeRef arg_types
[8];
1388 LLVMTypeRef func_type
;
1389 LLVMValueRef context_ptr
;
1390 LLVMBasicBlockRef block
;
1391 LLVMBuilderRef builder
;
1392 LLVMValueRef fetch_elts
, fetch_count
, stride
, step
, io_itr
;
1393 LLVMValueRef io_ptr
, vbuffers_ptr
, vb_ptr
;
1394 LLVMValueRef instance_id
;
1395 LLVMValueRef system_values_array
;
1396 struct draw_context
*draw
= llvm
->draw
;
1397 const struct tgsi_shader_info
*vs_info
= &draw
->vs
.vertex_shader
->info
;
1399 struct lp_build_context bld
;
1400 struct lp_build_loop_state lp_loop
;
1401 const int max_vertices
= 4;
1402 LLVMValueRef outputs
[PIPE_MAX_SHADER_OUTPUTS
][NUM_CHANNELS
];
1403 LLVMValueRef fetch_max
;
1405 struct lp_build_sampler_soa
*sampler
= 0;
1406 LLVMValueRef ret
, ret_ptr
;
1407 boolean bypass_viewport
= variant
->key
.bypass_viewport
;
1408 boolean enable_cliptest
= variant
->key
.clip_xy
||
1409 variant
->key
.clip_z
||
1410 variant
->key
.clip_user
;
1412 arg_types
[0] = get_context_ptr_type(llvm
); /* context */
1413 arg_types
[1] = get_vertex_header_ptr_type(llvm
); /* vertex_header */
1414 arg_types
[2] = get_buffer_ptr_type(llvm
); /* vbuffers */
1415 arg_types
[3] = LLVMPointerType(int32_type
, 0); /* fetch_elts * */
1416 arg_types
[4] = int32_type
; /* fetch_count */
1417 arg_types
[5] = int32_type
; /* stride */
1418 arg_types
[6] = get_vb_ptr_type(llvm
); /* pipe_vertex_buffer's */
1419 arg_types
[7] = int32_type
; /* instance_id */
1421 func_type
= LLVMFunctionType(int32_type
, arg_types
, Elements(arg_types
), 0);
1423 variant
->function_elts
= LLVMAddFunction(gallivm
->module
, "draw_llvm_shader_elts", func_type
);
1424 LLVMSetFunctionCallConv(variant
->function_elts
, LLVMCCallConv
);
1425 for (i
= 0; i
< Elements(arg_types
); ++i
)
1426 if (LLVMGetTypeKind(arg_types
[i
]) == LLVMPointerTypeKind
)
1427 LLVMAddAttribute(LLVMGetParam(variant
->function_elts
, i
),
1428 LLVMNoAliasAttribute
);
1430 context_ptr
= LLVMGetParam(variant
->function_elts
, 0);
1431 io_ptr
= LLVMGetParam(variant
->function_elts
, 1);
1432 vbuffers_ptr
= LLVMGetParam(variant
->function_elts
, 2);
1433 fetch_elts
= LLVMGetParam(variant
->function_elts
, 3);
1434 fetch_count
= LLVMGetParam(variant
->function_elts
, 4);
1435 stride
= LLVMGetParam(variant
->function_elts
, 5);
1436 vb_ptr
= LLVMGetParam(variant
->function_elts
, 6);
1437 instance_id
= LLVMGetParam(variant
->function_elts
, 7);
1439 lp_build_name(context_ptr
, "context");
1440 lp_build_name(io_ptr
, "io");
1441 lp_build_name(vbuffers_ptr
, "vbuffers");
1442 lp_build_name(fetch_elts
, "fetch_elts");
1443 lp_build_name(fetch_count
, "fetch_count");
1444 lp_build_name(stride
, "stride");
1445 lp_build_name(vb_ptr
, "vb");
1446 lp_build_name(instance_id
, "instance_id");
1452 block
= LLVMAppendBasicBlockInContext(gallivm
->context
, variant
->function_elts
, "entry");
1453 builder
= gallivm
->builder
;
1454 LLVMPositionBuilderAtEnd(builder
, block
);
1456 lp_build_context_init(&bld
, gallivm
, lp_type_int(32));
1458 system_values_array
= lp_build_system_values_array(gallivm
, vs_info
,
1462 step
= lp_build_const_int32(gallivm
, max_vertices
);
1464 /* code generated texture sampling */
1465 sampler
= draw_llvm_sampler_soa_create(
1466 draw_llvm_variant_key_samplers(&variant
->key
),
1469 fetch_max
= LLVMBuildSub(builder
, fetch_count
,
1470 lp_build_const_int32(gallivm
, 1),
1473 /* function returns non-zero i32 value if any clipped vertices */
1474 ret_ptr
= lp_build_alloca(gallivm
, int32_type
, "");
1475 LLVMBuildStore(builder
, lp_build_const_int32(gallivm
, 0), ret_ptr
);
1477 lp_build_loop_begin(&lp_loop
, gallivm
, lp_build_const_int32(gallivm
, 0));
1479 LLVMValueRef inputs
[PIPE_MAX_SHADER_INPUTS
][NUM_CHANNELS
];
1480 LLVMValueRef aos_attribs
[PIPE_MAX_SHADER_INPUTS
][NUM_CHANNELS
] = { { 0 } };
1482 LLVMValueRef clipmask
; /* holds the clipmask value */
1483 const LLVMValueRef (*ptr_aos
)[NUM_CHANNELS
];
1485 io_itr
= lp_loop
.counter
;
1486 io
= LLVMBuildGEP(builder
, io_ptr
, &io_itr
, 1, "");
1488 lp_build_printf(builder
, " --- io %d = %p, loop counter %d\n",
1489 io_itr
, io
, lp_loop
.counter
);
1491 for (i
= 0; i
< NUM_CHANNELS
; ++i
) {
1492 LLVMValueRef true_index
= LLVMBuildAdd(
1495 lp_build_const_int32(gallivm
, i
), "");
1496 LLVMValueRef fetch_ptr
;
1498 /* make sure we're not out of bounds which can happen
1499 * if fetch_count % 4 != 0, because on the last iteration
1500 * a few of the 4 vertex fetches will be out of bounds */
1501 true_index
= lp_build_min(&bld
, true_index
, fetch_max
);
1503 fetch_ptr
= LLVMBuildGEP(builder
, fetch_elts
,
1504 &true_index
, 1, "");
1505 true_index
= LLVMBuildLoad(builder
, fetch_ptr
, "fetch_elt");
1506 for (j
= 0; j
< draw
->pt
.nr_vertex_elements
; ++j
) {
1507 struct pipe_vertex_element
*velem
= &draw
->pt
.vertex_element
[j
];
1508 LLVMValueRef vb_index
= lp_build_const_int32(gallivm
, velem
->vertex_buffer_index
);
1509 LLVMValueRef vb
= LLVMBuildGEP(builder
, vb_ptr
,
1511 generate_fetch(gallivm
, vbuffers_ptr
,
1512 &aos_attribs
[j
][i
], velem
, vb
, true_index
,
1516 convert_to_soa(gallivm
, aos_attribs
, inputs
,
1517 draw
->pt
.nr_vertex_elements
);
1519 ptr_aos
= (const LLVMValueRef (*)[NUM_CHANNELS
]) inputs
;
1524 system_values_array
,
1527 variant
->key
.clamp_vertex_color
);
1529 /* store original positions in clip before further manipulation */
1530 store_clip(gallivm
, io
, outputs
);
1533 if (enable_cliptest
) {
1534 /* allocate clipmask, assign it integer type */
1535 clipmask
= generate_clipmask(gallivm
, outputs
,
1536 variant
->key
.clip_xy
,
1537 variant
->key
.clip_z
,
1538 variant
->key
.clip_user
,
1539 variant
->key
.clip_halfz
,
1540 variant
->key
.nr_planes
,
1542 /* return clipping boolean value for function */
1543 clipmask_bool(gallivm
, clipmask
, ret_ptr
);
1546 clipmask
= lp_build_const_int_vec(gallivm
, lp_type_int_vec(32), 0);
1549 /* do viewport mapping */
1550 if (!bypass_viewport
) {
1551 generate_viewport(llvm
, builder
, outputs
, context_ptr
);
1554 /* store clipmask in vertex header,
1555 * original positions in clip
1556 * and transformed positions in data
1558 convert_to_aos(gallivm
, io
, outputs
, clipmask
,
1559 vs_info
->num_outputs
, max_vertices
);
1562 lp_build_loop_end_cond(&lp_loop
, fetch_count
, step
, LLVMIntUGE
);
1564 sampler
->destroy(sampler
);
1566 ret
= LLVMBuildLoad(builder
, ret_ptr
,"");
1567 LLVMBuildRet(builder
, ret
);
1570 * Translate the LLVM IR into machine code.
1573 if (LLVMVerifyFunction(variant
->function_elts
, LLVMPrintMessageAction
)) {
1574 lp_debug_dump_value(variant
->function_elts
);
1579 LLVMRunFunctionPassManager(gallivm
->passmgr
, variant
->function_elts
);
1581 if (gallivm_debug
& GALLIVM_DEBUG_IR
) {
1582 lp_debug_dump_value(variant
->function_elts
);
1586 code
= LLVMGetPointerToGlobal(gallivm
->engine
, variant
->function_elts
);
1587 variant
->jit_func_elts
= (draw_jit_vert_func_elts
)pointer_to_func(code
);
1589 if (gallivm_debug
& GALLIVM_DEBUG_ASM
) {
1590 lp_disassemble(code
);
1592 lp_func_delete_body(variant
->function_elts
);
1596 struct draw_llvm_variant_key
*
1597 draw_llvm_make_variant_key(struct draw_llvm
*llvm
, char *store
)
1600 struct draw_llvm_variant_key
*key
;
1601 struct lp_sampler_static_state
*sampler
;
1603 key
= (struct draw_llvm_variant_key
*)store
;
1605 key
->clamp_vertex_color
= llvm
->draw
->rasterizer
->clamp_vertex_color
; /**/
1607 /* Presumably all variants of the shader should have the same
1608 * number of vertex elements - ie the number of shader inputs.
1610 key
->nr_vertex_elements
= llvm
->draw
->pt
.nr_vertex_elements
;
1612 /* will have to rig this up properly later */
1613 key
->clip_xy
= llvm
->draw
->clip_xy
;
1614 key
->clip_z
= llvm
->draw
->clip_z
;
1615 key
->clip_user
= llvm
->draw
->clip_user
;
1616 key
->bypass_viewport
= llvm
->draw
->identity_viewport
;
1617 key
->clip_halfz
= !llvm
->draw
->rasterizer
->gl_rasterization_rules
;
1618 key
->need_edgeflags
= (llvm
->draw
->vs
.edgeflag_output
? TRUE
: FALSE
);
1619 key
->nr_planes
= llvm
->draw
->nr_planes
;
1622 /* All variants of this shader will have the same value for
1623 * nr_samplers. Not yet trying to compact away holes in the
1626 key
->nr_samplers
= llvm
->draw
->vs
.vertex_shader
->info
.file_max
[TGSI_FILE_SAMPLER
] + 1;
1628 sampler
= draw_llvm_variant_key_samplers(key
);
1630 memcpy(key
->vertex_element
,
1631 llvm
->draw
->pt
.vertex_element
,
1632 sizeof(struct pipe_vertex_element
) * key
->nr_vertex_elements
);
1634 memset(sampler
, 0, key
->nr_samplers
* sizeof *sampler
);
1636 for (i
= 0 ; i
< key
->nr_samplers
; i
++) {
1637 lp_sampler_static_state(&sampler
[i
],
1638 llvm
->draw
->sampler_views
[i
],
1639 llvm
->draw
->samplers
[i
]);
1647 draw_llvm_set_mapped_texture(struct draw_context
*draw
,
1648 unsigned sampler_idx
,
1649 uint32_t width
, uint32_t height
, uint32_t depth
,
1650 uint32_t first_level
, uint32_t last_level
,
1651 uint32_t row_stride
[PIPE_MAX_TEXTURE_LEVELS
],
1652 uint32_t img_stride
[PIPE_MAX_TEXTURE_LEVELS
],
1653 const void *data
[PIPE_MAX_TEXTURE_LEVELS
])
1656 struct draw_jit_texture
*jit_tex
;
1658 assert(sampler_idx
< PIPE_MAX_VERTEX_SAMPLERS
);
1660 jit_tex
= &draw
->llvm
->jit_context
.textures
[sampler_idx
];
1662 jit_tex
->width
= width
;
1663 jit_tex
->height
= height
;
1664 jit_tex
->depth
= depth
;
1665 jit_tex
->first_level
= first_level
;
1666 jit_tex
->last_level
= last_level
;
1668 for (j
= first_level
; j
<= last_level
; j
++) {
1669 jit_tex
->data
[j
] = data
[j
];
1670 jit_tex
->row_stride
[j
] = row_stride
[j
];
1671 jit_tex
->img_stride
[j
] = img_stride
[j
];
1677 draw_llvm_set_sampler_state(struct draw_context
*draw
)
1681 for (i
= 0; i
< draw
->num_samplers
; i
++) {
1682 struct draw_jit_texture
*jit_tex
= &draw
->llvm
->jit_context
.textures
[i
];
1684 if (draw
->samplers
[i
]) {
1685 jit_tex
->min_lod
= draw
->samplers
[i
]->min_lod
;
1686 jit_tex
->max_lod
= draw
->samplers
[i
]->max_lod
;
1687 jit_tex
->lod_bias
= draw
->samplers
[i
]->lod_bias
;
1688 COPY_4V(jit_tex
->border_color
, draw
->samplers
[i
]->border_color
);
1695 draw_llvm_destroy_variant(struct draw_llvm_variant
*variant
)
1697 struct draw_llvm
*llvm
= variant
->llvm
;
1699 if (variant
->function_elts
) {
1700 LLVMFreeMachineCodeForFunction(llvm
->gallivm
->engine
,
1701 variant
->function_elts
);
1702 LLVMDeleteFunction(variant
->function_elts
);
1705 if (variant
->function
) {
1706 LLVMFreeMachineCodeForFunction(llvm
->gallivm
->engine
,
1708 LLVMDeleteFunction(variant
->function
);
1711 remove_from_list(&variant
->list_item_local
);
1712 variant
->shader
->variants_cached
--;
1713 remove_from_list(&variant
->list_item_global
);
1714 llvm
->nr_variants
--;